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))
1659 if (! (_bfd_generic_link_add_one_symbol
1660 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1661 0, name, FALSE, collect, &bh)))
1663 hi = (struct elf_link_hash_entry *) bh;
1667 /* In this case the symbol named SHORTNAME is overriding the
1668 indirect symbol we want to add. We were planning on making
1669 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1670 is the name without a version. NAME is the fully versioned
1671 name, and it is the default version.
1673 Overriding means that we already saw a definition for the
1674 symbol SHORTNAME in a regular object, and it is overriding
1675 the symbol defined in the dynamic object.
1677 When this happens, we actually want to change NAME, the
1678 symbol we just added, to refer to SHORTNAME. This will cause
1679 references to NAME in the shared object to become references
1680 to SHORTNAME in the regular object. This is what we expect
1681 when we override a function in a shared object: that the
1682 references in the shared object will be mapped to the
1683 definition in the regular object. */
1685 while (hi->root.type == bfd_link_hash_indirect
1686 || hi->root.type == bfd_link_hash_warning)
1687 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1689 h->root.type = bfd_link_hash_indirect;
1690 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1694 hi->ref_dynamic = 1;
1698 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1703 /* Now set HI to H, so that the following code will set the
1704 other fields correctly. */
1708 /* Check if HI is a warning symbol. */
1709 if (hi->root.type == bfd_link_hash_warning)
1710 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1712 /* If there is a duplicate definition somewhere, then HI may not
1713 point to an indirect symbol. We will have reported an error to
1714 the user in that case. */
1716 if (hi->root.type == bfd_link_hash_indirect)
1718 struct elf_link_hash_entry *ht;
1720 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1721 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1723 /* A reference to the SHORTNAME symbol from a dynamic library
1724 will be satisfied by the versioned symbol at runtime. In
1725 effect, we have a reference to the versioned symbol. */
1726 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1727 hi->dynamic_def |= ht->dynamic_def;
1729 /* See if the new flags lead us to realize that the symbol must
1735 if (! info->executable
1742 if (hi->ref_regular)
1748 /* We also need to define an indirection from the nondefault version
1752 len = strlen (name);
1753 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1754 if (shortname == NULL)
1756 memcpy (shortname, name, shortlen);
1757 memcpy (shortname + shortlen, p + 1, len - shortlen);
1759 /* Once again, merge with any existing symbol. */
1760 type_change_ok = FALSE;
1761 size_change_ok = FALSE;
1763 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1764 &hi, poldbfd, NULL, NULL, &skip, &override,
1765 &type_change_ok, &size_change_ok))
1773 /* Here SHORTNAME is a versioned name, so we don't expect to see
1774 the type of override we do in the case above unless it is
1775 overridden by a versioned definition. */
1776 if (hi->root.type != bfd_link_hash_defined
1777 && hi->root.type != bfd_link_hash_defweak)
1778 (*_bfd_error_handler)
1779 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1785 if (! (_bfd_generic_link_add_one_symbol
1786 (info, abfd, shortname, BSF_INDIRECT,
1787 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1789 hi = (struct elf_link_hash_entry *) bh;
1791 /* If there is a duplicate definition somewhere, then HI may not
1792 point to an indirect symbol. We will have reported an error
1793 to the user in that case. */
1795 if (hi->root.type == bfd_link_hash_indirect)
1797 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1798 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1799 hi->dynamic_def |= h->dynamic_def;
1801 /* See if the new flags lead us to realize that the symbol
1807 if (! info->executable
1813 if (hi->ref_regular)
1823 /* This routine is used to export all defined symbols into the dynamic
1824 symbol table. It is called via elf_link_hash_traverse. */
1827 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1829 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1831 /* Ignore indirect symbols. These are added by the versioning code. */
1832 if (h->root.type == bfd_link_hash_indirect)
1835 /* Ignore this if we won't export it. */
1836 if (!eif->info->export_dynamic && !h->dynamic)
1839 if (h->dynindx == -1
1840 && (h->def_regular || h->ref_regular)
1841 && ! bfd_hide_sym_by_version (eif->info->version_info,
1842 h->root.root.string))
1844 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1854 /* Look through the symbols which are defined in other shared
1855 libraries and referenced here. Update the list of version
1856 dependencies. This will be put into the .gnu.version_r section.
1857 This function is called via elf_link_hash_traverse. */
1860 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1863 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
1864 Elf_Internal_Verneed *t;
1865 Elf_Internal_Vernaux *a;
1868 /* We only care about symbols defined in shared objects with version
1873 || h->verinfo.verdef == NULL
1874 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
1875 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
1878 /* See if we already know about this version. */
1879 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1883 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1886 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1887 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1893 /* This is a new version. Add it to tree we are building. */
1898 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
1901 rinfo->failed = TRUE;
1905 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1906 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
1907 elf_tdata (rinfo->info->output_bfd)->verref = t;
1911 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
1914 rinfo->failed = TRUE;
1918 /* Note that we are copying a string pointer here, and testing it
1919 above. If bfd_elf_string_from_elf_section is ever changed to
1920 discard the string data when low in memory, this will have to be
1922 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1924 a->vna_flags = h->verinfo.verdef->vd_flags;
1925 a->vna_nextptr = t->vn_auxptr;
1927 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1930 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1937 /* Figure out appropriate versions for all the symbols. We may not
1938 have the version number script until we have read all of the input
1939 files, so until that point we don't know which symbols should be
1940 local. This function is called via elf_link_hash_traverse. */
1943 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1945 struct elf_info_failed *sinfo;
1946 struct bfd_link_info *info;
1947 const struct elf_backend_data *bed;
1948 struct elf_info_failed eif;
1952 sinfo = (struct elf_info_failed *) data;
1955 /* Fix the symbol flags. */
1958 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1961 sinfo->failed = TRUE;
1965 /* We only need version numbers for symbols defined in regular
1967 if (!h->def_regular)
1970 bed = get_elf_backend_data (info->output_bfd);
1971 p = strchr (h->root.root.string, ELF_VER_CHR);
1972 if (p != NULL && h->verinfo.vertree == NULL)
1974 struct bfd_elf_version_tree *t;
1979 /* There are two consecutive ELF_VER_CHR characters if this is
1980 not a hidden symbol. */
1982 if (*p == ELF_VER_CHR)
1988 /* If there is no version string, we can just return out. */
1996 /* Look for the version. If we find it, it is no longer weak. */
1997 for (t = sinfo->info->version_info; t != NULL; t = t->next)
1999 if (strcmp (t->name, p) == 0)
2003 struct bfd_elf_version_expr *d;
2005 len = p - h->root.root.string;
2006 alc = (char *) bfd_malloc (len);
2009 sinfo->failed = TRUE;
2012 memcpy (alc, h->root.root.string, len - 1);
2013 alc[len - 1] = '\0';
2014 if (alc[len - 2] == ELF_VER_CHR)
2015 alc[len - 2] = '\0';
2017 h->verinfo.vertree = t;
2021 if (t->globals.list != NULL)
2022 d = (*t->match) (&t->globals, NULL, alc);
2024 /* See if there is anything to force this symbol to
2026 if (d == NULL && t->locals.list != NULL)
2028 d = (*t->match) (&t->locals, NULL, alc);
2031 && ! info->export_dynamic)
2032 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2040 /* If we are building an application, we need to create a
2041 version node for this version. */
2042 if (t == NULL && info->executable)
2044 struct bfd_elf_version_tree **pp;
2047 /* If we aren't going to export this symbol, we don't need
2048 to worry about it. */
2049 if (h->dynindx == -1)
2053 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2056 sinfo->failed = TRUE;
2061 t->name_indx = (unsigned int) -1;
2065 /* Don't count anonymous version tag. */
2066 if (sinfo->info->version_info != NULL
2067 && sinfo->info->version_info->vernum == 0)
2069 for (pp = &sinfo->info->version_info;
2073 t->vernum = version_index;
2077 h->verinfo.vertree = t;
2081 /* We could not find the version for a symbol when
2082 generating a shared archive. Return an error. */
2083 (*_bfd_error_handler)
2084 (_("%B: version node not found for symbol %s"),
2085 info->output_bfd, h->root.root.string);
2086 bfd_set_error (bfd_error_bad_value);
2087 sinfo->failed = TRUE;
2095 /* If we don't have a version for this symbol, see if we can find
2097 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2102 = bfd_find_version_for_sym (sinfo->info->version_info,
2103 h->root.root.string, &hide);
2104 if (h->verinfo.vertree != NULL && hide)
2105 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2111 /* Read and swap the relocs from the section indicated by SHDR. This
2112 may be either a REL or a RELA section. The relocations are
2113 translated into RELA relocations and stored in INTERNAL_RELOCS,
2114 which should have already been allocated to contain enough space.
2115 The EXTERNAL_RELOCS are a buffer where the external form of the
2116 relocations should be stored.
2118 Returns FALSE if something goes wrong. */
2121 elf_link_read_relocs_from_section (bfd *abfd,
2123 Elf_Internal_Shdr *shdr,
2124 void *external_relocs,
2125 Elf_Internal_Rela *internal_relocs)
2127 const struct elf_backend_data *bed;
2128 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2129 const bfd_byte *erela;
2130 const bfd_byte *erelaend;
2131 Elf_Internal_Rela *irela;
2132 Elf_Internal_Shdr *symtab_hdr;
2135 /* Position ourselves at the start of the section. */
2136 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2139 /* Read the relocations. */
2140 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2143 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2144 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2146 bed = get_elf_backend_data (abfd);
2148 /* Convert the external relocations to the internal format. */
2149 if (shdr->sh_entsize == bed->s->sizeof_rel)
2150 swap_in = bed->s->swap_reloc_in;
2151 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2152 swap_in = bed->s->swap_reloca_in;
2155 bfd_set_error (bfd_error_wrong_format);
2159 erela = (const bfd_byte *) external_relocs;
2160 erelaend = erela + shdr->sh_size;
2161 irela = internal_relocs;
2162 while (erela < erelaend)
2166 (*swap_in) (abfd, erela, irela);
2167 r_symndx = ELF32_R_SYM (irela->r_info);
2168 if (bed->s->arch_size == 64)
2172 if ((size_t) r_symndx >= nsyms)
2174 (*_bfd_error_handler)
2175 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2176 " for offset 0x%lx in section `%A'"),
2178 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2179 bfd_set_error (bfd_error_bad_value);
2183 else if (r_symndx != STN_UNDEF)
2185 (*_bfd_error_handler)
2186 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2187 " when the object file has no symbol table"),
2189 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2190 bfd_set_error (bfd_error_bad_value);
2193 irela += bed->s->int_rels_per_ext_rel;
2194 erela += shdr->sh_entsize;
2200 /* Read and swap the relocs for a section O. They may have been
2201 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2202 not NULL, they are used as buffers to read into. They are known to
2203 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2204 the return value is allocated using either malloc or bfd_alloc,
2205 according to the KEEP_MEMORY argument. If O has two relocation
2206 sections (both REL and RELA relocations), then the REL_HDR
2207 relocations will appear first in INTERNAL_RELOCS, followed by the
2208 RELA_HDR relocations. */
2211 _bfd_elf_link_read_relocs (bfd *abfd,
2213 void *external_relocs,
2214 Elf_Internal_Rela *internal_relocs,
2215 bfd_boolean keep_memory)
2217 void *alloc1 = NULL;
2218 Elf_Internal_Rela *alloc2 = NULL;
2219 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2220 struct bfd_elf_section_data *esdo = elf_section_data (o);
2221 Elf_Internal_Rela *internal_rela_relocs;
2223 if (esdo->relocs != NULL)
2224 return esdo->relocs;
2226 if (o->reloc_count == 0)
2229 if (internal_relocs == NULL)
2233 size = o->reloc_count;
2234 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2236 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2238 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2239 if (internal_relocs == NULL)
2243 if (external_relocs == NULL)
2245 bfd_size_type size = 0;
2248 size += esdo->rel.hdr->sh_size;
2250 size += esdo->rela.hdr->sh_size;
2252 alloc1 = bfd_malloc (size);
2255 external_relocs = alloc1;
2258 internal_rela_relocs = internal_relocs;
2261 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2265 external_relocs = (((bfd_byte *) external_relocs)
2266 + esdo->rel.hdr->sh_size);
2267 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2268 * bed->s->int_rels_per_ext_rel);
2272 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2274 internal_rela_relocs)))
2277 /* Cache the results for next time, if we can. */
2279 esdo->relocs = internal_relocs;
2284 /* Don't free alloc2, since if it was allocated we are passing it
2285 back (under the name of internal_relocs). */
2287 return internal_relocs;
2295 bfd_release (abfd, alloc2);
2302 /* Compute the size of, and allocate space for, REL_HDR which is the
2303 section header for a section containing relocations for O. */
2306 _bfd_elf_link_size_reloc_section (bfd *abfd,
2307 struct bfd_elf_section_reloc_data *reldata)
2309 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2311 /* That allows us to calculate the size of the section. */
2312 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2314 /* The contents field must last into write_object_contents, so we
2315 allocate it with bfd_alloc rather than malloc. Also since we
2316 cannot be sure that the contents will actually be filled in,
2317 we zero the allocated space. */
2318 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2319 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2322 if (reldata->hashes == NULL && reldata->count)
2324 struct elf_link_hash_entry **p;
2326 p = ((struct elf_link_hash_entry **)
2327 bfd_zmalloc (reldata->count * sizeof (*p)));
2331 reldata->hashes = p;
2337 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2338 originated from the section given by INPUT_REL_HDR) to the
2342 _bfd_elf_link_output_relocs (bfd *output_bfd,
2343 asection *input_section,
2344 Elf_Internal_Shdr *input_rel_hdr,
2345 Elf_Internal_Rela *internal_relocs,
2346 struct elf_link_hash_entry **rel_hash
2349 Elf_Internal_Rela *irela;
2350 Elf_Internal_Rela *irelaend;
2352 struct bfd_elf_section_reloc_data *output_reldata;
2353 asection *output_section;
2354 const struct elf_backend_data *bed;
2355 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2356 struct bfd_elf_section_data *esdo;
2358 output_section = input_section->output_section;
2360 bed = get_elf_backend_data (output_bfd);
2361 esdo = elf_section_data (output_section);
2362 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2364 output_reldata = &esdo->rel;
2365 swap_out = bed->s->swap_reloc_out;
2367 else if (esdo->rela.hdr
2368 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2370 output_reldata = &esdo->rela;
2371 swap_out = bed->s->swap_reloca_out;
2375 (*_bfd_error_handler)
2376 (_("%B: relocation size mismatch in %B section %A"),
2377 output_bfd, input_section->owner, input_section);
2378 bfd_set_error (bfd_error_wrong_format);
2382 erel = output_reldata->hdr->contents;
2383 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2384 irela = internal_relocs;
2385 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2386 * bed->s->int_rels_per_ext_rel);
2387 while (irela < irelaend)
2389 (*swap_out) (output_bfd, irela, erel);
2390 irela += bed->s->int_rels_per_ext_rel;
2391 erel += input_rel_hdr->sh_entsize;
2394 /* Bump the counter, so that we know where to add the next set of
2396 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2401 /* Make weak undefined symbols in PIE dynamic. */
2404 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2405 struct elf_link_hash_entry *h)
2409 && h->root.type == bfd_link_hash_undefweak)
2410 return bfd_elf_link_record_dynamic_symbol (info, h);
2415 /* Fix up the flags for a symbol. This handles various cases which
2416 can only be fixed after all the input files are seen. This is
2417 currently called by both adjust_dynamic_symbol and
2418 assign_sym_version, which is unnecessary but perhaps more robust in
2419 the face of future changes. */
2422 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2423 struct elf_info_failed *eif)
2425 const struct elf_backend_data *bed;
2427 /* If this symbol was mentioned in a non-ELF file, try to set
2428 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2429 permit a non-ELF file to correctly refer to a symbol defined in
2430 an ELF dynamic object. */
2433 while (h->root.type == bfd_link_hash_indirect)
2434 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2436 if (h->root.type != bfd_link_hash_defined
2437 && h->root.type != bfd_link_hash_defweak)
2440 h->ref_regular_nonweak = 1;
2444 if (h->root.u.def.section->owner != NULL
2445 && (bfd_get_flavour (h->root.u.def.section->owner)
2446 == bfd_target_elf_flavour))
2449 h->ref_regular_nonweak = 1;
2455 if (h->dynindx == -1
2459 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2468 /* Unfortunately, NON_ELF is only correct if the symbol
2469 was first seen in a non-ELF file. Fortunately, if the symbol
2470 was first seen in an ELF file, we're probably OK unless the
2471 symbol was defined in a non-ELF file. Catch that case here.
2472 FIXME: We're still in trouble if the symbol was first seen in
2473 a dynamic object, and then later in a non-ELF regular object. */
2474 if ((h->root.type == bfd_link_hash_defined
2475 || h->root.type == bfd_link_hash_defweak)
2477 && (h->root.u.def.section->owner != NULL
2478 ? (bfd_get_flavour (h->root.u.def.section->owner)
2479 != bfd_target_elf_flavour)
2480 : (bfd_is_abs_section (h->root.u.def.section)
2481 && !h->def_dynamic)))
2485 /* Backend specific symbol fixup. */
2486 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2487 if (bed->elf_backend_fixup_symbol
2488 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2491 /* If this is a final link, and the symbol was defined as a common
2492 symbol in a regular object file, and there was no definition in
2493 any dynamic object, then the linker will have allocated space for
2494 the symbol in a common section but the DEF_REGULAR
2495 flag will not have been set. */
2496 if (h->root.type == bfd_link_hash_defined
2500 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2503 /* If -Bsymbolic was used (which means to bind references to global
2504 symbols to the definition within the shared object), and this
2505 symbol was defined in a regular object, then it actually doesn't
2506 need a PLT entry. Likewise, if the symbol has non-default
2507 visibility. If the symbol has hidden or internal visibility, we
2508 will force it local. */
2510 && eif->info->shared
2511 && is_elf_hash_table (eif->info->hash)
2512 && (SYMBOLIC_BIND (eif->info, h)
2513 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2516 bfd_boolean force_local;
2518 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2519 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2520 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2523 /* If a weak undefined symbol has non-default visibility, we also
2524 hide it from the dynamic linker. */
2525 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2526 && h->root.type == bfd_link_hash_undefweak)
2527 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2529 /* If this is a weak defined symbol in a dynamic object, and we know
2530 the real definition in the dynamic object, copy interesting flags
2531 over to the real definition. */
2532 if (h->u.weakdef != NULL)
2534 /* If the real definition is defined by a regular object file,
2535 don't do anything special. See the longer description in
2536 _bfd_elf_adjust_dynamic_symbol, below. */
2537 if (h->u.weakdef->def_regular)
2538 h->u.weakdef = NULL;
2541 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2543 while (h->root.type == bfd_link_hash_indirect)
2544 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2546 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2547 || h->root.type == bfd_link_hash_defweak);
2548 BFD_ASSERT (weakdef->def_dynamic);
2549 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2550 || weakdef->root.type == bfd_link_hash_defweak);
2551 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2558 /* Make the backend pick a good value for a dynamic symbol. This is
2559 called via elf_link_hash_traverse, and also calls itself
2563 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2565 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2567 const struct elf_backend_data *bed;
2569 if (! is_elf_hash_table (eif->info->hash))
2572 /* Ignore indirect symbols. These are added by the versioning code. */
2573 if (h->root.type == bfd_link_hash_indirect)
2576 /* Fix the symbol flags. */
2577 if (! _bfd_elf_fix_symbol_flags (h, eif))
2580 /* If this symbol does not require a PLT entry, and it is not
2581 defined by a dynamic object, or is not referenced by a regular
2582 object, ignore it. We do have to handle a weak defined symbol,
2583 even if no regular object refers to it, if we decided to add it
2584 to the dynamic symbol table. FIXME: Do we normally need to worry
2585 about symbols which are defined by one dynamic object and
2586 referenced by another one? */
2588 && h->type != STT_GNU_IFUNC
2592 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2594 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2598 /* If we've already adjusted this symbol, don't do it again. This
2599 can happen via a recursive call. */
2600 if (h->dynamic_adjusted)
2603 /* Don't look at this symbol again. Note that we must set this
2604 after checking the above conditions, because we may look at a
2605 symbol once, decide not to do anything, and then get called
2606 recursively later after REF_REGULAR is set below. */
2607 h->dynamic_adjusted = 1;
2609 /* If this is a weak definition, and we know a real definition, and
2610 the real symbol is not itself defined by a regular object file,
2611 then get a good value for the real definition. We handle the
2612 real symbol first, for the convenience of the backend routine.
2614 Note that there is a confusing case here. If the real definition
2615 is defined by a regular object file, we don't get the real symbol
2616 from the dynamic object, but we do get the weak symbol. If the
2617 processor backend uses a COPY reloc, then if some routine in the
2618 dynamic object changes the real symbol, we will not see that
2619 change in the corresponding weak symbol. This is the way other
2620 ELF linkers work as well, and seems to be a result of the shared
2623 I will clarify this issue. Most SVR4 shared libraries define the
2624 variable _timezone and define timezone as a weak synonym. The
2625 tzset call changes _timezone. If you write
2626 extern int timezone;
2628 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2629 you might expect that, since timezone is a synonym for _timezone,
2630 the same number will print both times. However, if the processor
2631 backend uses a COPY reloc, then actually timezone will be copied
2632 into your process image, and, since you define _timezone
2633 yourself, _timezone will not. Thus timezone and _timezone will
2634 wind up at different memory locations. The tzset call will set
2635 _timezone, leaving timezone unchanged. */
2637 if (h->u.weakdef != NULL)
2639 /* If we get to this point, there is an implicit reference to
2640 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2641 h->u.weakdef->ref_regular = 1;
2643 /* Ensure that the backend adjust_dynamic_symbol function sees
2644 H->U.WEAKDEF before H by recursively calling ourselves. */
2645 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2649 /* If a symbol has no type and no size and does not require a PLT
2650 entry, then we are probably about to do the wrong thing here: we
2651 are probably going to create a COPY reloc for an empty object.
2652 This case can arise when a shared object is built with assembly
2653 code, and the assembly code fails to set the symbol type. */
2655 && h->type == STT_NOTYPE
2657 (*_bfd_error_handler)
2658 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2659 h->root.root.string);
2661 dynobj = elf_hash_table (eif->info)->dynobj;
2662 bed = get_elf_backend_data (dynobj);
2664 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2673 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2677 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2678 struct elf_link_hash_entry *h,
2681 unsigned int power_of_two;
2683 asection *sec = h->root.u.def.section;
2685 /* The section aligment of definition is the maximum alignment
2686 requirement of symbols defined in the section. Since we don't
2687 know the symbol alignment requirement, we start with the
2688 maximum alignment and check low bits of the symbol address
2689 for the minimum alignment. */
2690 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2691 mask = ((bfd_vma) 1 << power_of_two) - 1;
2692 while ((h->root.u.def.value & mask) != 0)
2698 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2701 /* Adjust the section alignment if needed. */
2702 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2707 /* We make sure that the symbol will be aligned properly. */
2708 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2710 /* Define the symbol as being at this point in DYNBSS. */
2711 h->root.u.def.section = dynbss;
2712 h->root.u.def.value = dynbss->size;
2714 /* Increment the size of DYNBSS to make room for the symbol. */
2715 dynbss->size += h->size;
2717 /* No error if extern_protected_data is true. */
2718 if (h->protected_def
2719 && (!info->extern_protected_data
2720 || (info->extern_protected_data < 0
2721 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2722 info->callbacks->einfo
2723 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2724 h->root.root.string);
2729 /* Adjust all external symbols pointing into SEC_MERGE sections
2730 to reflect the object merging within the sections. */
2733 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2737 if ((h->root.type == bfd_link_hash_defined
2738 || h->root.type == bfd_link_hash_defweak)
2739 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2740 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2742 bfd *output_bfd = (bfd *) data;
2744 h->root.u.def.value =
2745 _bfd_merged_section_offset (output_bfd,
2746 &h->root.u.def.section,
2747 elf_section_data (sec)->sec_info,
2748 h->root.u.def.value);
2754 /* Returns false if the symbol referred to by H should be considered
2755 to resolve local to the current module, and true if it should be
2756 considered to bind dynamically. */
2759 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2760 struct bfd_link_info *info,
2761 bfd_boolean not_local_protected)
2763 bfd_boolean binding_stays_local_p;
2764 const struct elf_backend_data *bed;
2765 struct elf_link_hash_table *hash_table;
2770 while (h->root.type == bfd_link_hash_indirect
2771 || h->root.type == bfd_link_hash_warning)
2772 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2774 /* If it was forced local, then clearly it's not dynamic. */
2775 if (h->dynindx == -1)
2777 if (h->forced_local)
2780 /* Identify the cases where name binding rules say that a
2781 visible symbol resolves locally. */
2782 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2784 switch (ELF_ST_VISIBILITY (h->other))
2791 hash_table = elf_hash_table (info);
2792 if (!is_elf_hash_table (hash_table))
2795 bed = get_elf_backend_data (hash_table->dynobj);
2797 /* Proper resolution for function pointer equality may require
2798 that these symbols perhaps be resolved dynamically, even though
2799 we should be resolving them to the current module. */
2800 if (!not_local_protected || !bed->is_function_type (h->type))
2801 binding_stays_local_p = TRUE;
2808 /* If it isn't defined locally, then clearly it's dynamic. */
2809 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2812 /* Otherwise, the symbol is dynamic if binding rules don't tell
2813 us that it remains local. */
2814 return !binding_stays_local_p;
2817 /* Return true if the symbol referred to by H should be considered
2818 to resolve local to the current module, and false otherwise. Differs
2819 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2820 undefined symbols. The two functions are virtually identical except
2821 for the place where forced_local and dynindx == -1 are tested. If
2822 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2823 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2824 the symbol is local only for defined symbols.
2825 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2826 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2827 treatment of undefined weak symbols. For those that do not make
2828 undefined weak symbols dynamic, both functions may return false. */
2831 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2832 struct bfd_link_info *info,
2833 bfd_boolean local_protected)
2835 const struct elf_backend_data *bed;
2836 struct elf_link_hash_table *hash_table;
2838 /* If it's a local sym, of course we resolve locally. */
2842 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2843 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2844 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2847 /* Common symbols that become definitions don't get the DEF_REGULAR
2848 flag set, so test it first, and don't bail out. */
2849 if (ELF_COMMON_DEF_P (h))
2851 /* If we don't have a definition in a regular file, then we can't
2852 resolve locally. The sym is either undefined or dynamic. */
2853 else if (!h->def_regular)
2856 /* Forced local symbols resolve locally. */
2857 if (h->forced_local)
2860 /* As do non-dynamic symbols. */
2861 if (h->dynindx == -1)
2864 /* At this point, we know the symbol is defined and dynamic. In an
2865 executable it must resolve locally, likewise when building symbolic
2866 shared libraries. */
2867 if (info->executable || SYMBOLIC_BIND (info, h))
2870 /* Now deal with defined dynamic symbols in shared libraries. Ones
2871 with default visibility might not resolve locally. */
2872 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2875 hash_table = elf_hash_table (info);
2876 if (!is_elf_hash_table (hash_table))
2879 bed = get_elf_backend_data (hash_table->dynobj);
2881 /* If extern_protected_data is false, STV_PROTECTED non-function
2882 symbols are local. */
2883 if ((!info->extern_protected_data
2884 || (info->extern_protected_data < 0
2885 && !bed->extern_protected_data))
2886 && !bed->is_function_type (h->type))
2889 /* Function pointer equality tests may require that STV_PROTECTED
2890 symbols be treated as dynamic symbols. If the address of a
2891 function not defined in an executable is set to that function's
2892 plt entry in the executable, then the address of the function in
2893 a shared library must also be the plt entry in the executable. */
2894 return local_protected;
2897 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2898 aligned. Returns the first TLS output section. */
2900 struct bfd_section *
2901 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2903 struct bfd_section *sec, *tls;
2904 unsigned int align = 0;
2906 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2907 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2911 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2912 if (sec->alignment_power > align)
2913 align = sec->alignment_power;
2915 elf_hash_table (info)->tls_sec = tls;
2917 /* Ensure the alignment of the first section is the largest alignment,
2918 so that the tls segment starts aligned. */
2920 tls->alignment_power = align;
2925 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2927 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2928 Elf_Internal_Sym *sym)
2930 const struct elf_backend_data *bed;
2932 /* Local symbols do not count, but target specific ones might. */
2933 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2934 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2937 bed = get_elf_backend_data (abfd);
2938 /* Function symbols do not count. */
2939 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2942 /* If the section is undefined, then so is the symbol. */
2943 if (sym->st_shndx == SHN_UNDEF)
2946 /* If the symbol is defined in the common section, then
2947 it is a common definition and so does not count. */
2948 if (bed->common_definition (sym))
2951 /* If the symbol is in a target specific section then we
2952 must rely upon the backend to tell us what it is. */
2953 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2954 /* FIXME - this function is not coded yet:
2956 return _bfd_is_global_symbol_definition (abfd, sym);
2958 Instead for now assume that the definition is not global,
2959 Even if this is wrong, at least the linker will behave
2960 in the same way that it used to do. */
2966 /* Search the symbol table of the archive element of the archive ABFD
2967 whose archive map contains a mention of SYMDEF, and determine if
2968 the symbol is defined in this element. */
2970 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2972 Elf_Internal_Shdr * hdr;
2973 bfd_size_type symcount;
2974 bfd_size_type extsymcount;
2975 bfd_size_type extsymoff;
2976 Elf_Internal_Sym *isymbuf;
2977 Elf_Internal_Sym *isym;
2978 Elf_Internal_Sym *isymend;
2981 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2985 /* Return FALSE if the object has been claimed by plugin. */
2986 if (abfd->plugin_format == bfd_plugin_yes)
2989 if (! bfd_check_format (abfd, bfd_object))
2992 /* Select the appropriate symbol table. */
2993 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2994 hdr = &elf_tdata (abfd)->symtab_hdr;
2996 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2998 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3000 /* The sh_info field of the symtab header tells us where the
3001 external symbols start. We don't care about the local symbols. */
3002 if (elf_bad_symtab (abfd))
3004 extsymcount = symcount;
3009 extsymcount = symcount - hdr->sh_info;
3010 extsymoff = hdr->sh_info;
3013 if (extsymcount == 0)
3016 /* Read in the symbol table. */
3017 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3019 if (isymbuf == NULL)
3022 /* Scan the symbol table looking for SYMDEF. */
3024 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3028 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3033 if (strcmp (name, symdef->name) == 0)
3035 result = is_global_data_symbol_definition (abfd, isym);
3045 /* Add an entry to the .dynamic table. */
3048 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3052 struct elf_link_hash_table *hash_table;
3053 const struct elf_backend_data *bed;
3055 bfd_size_type newsize;
3056 bfd_byte *newcontents;
3057 Elf_Internal_Dyn dyn;
3059 hash_table = elf_hash_table (info);
3060 if (! is_elf_hash_table (hash_table))
3063 bed = get_elf_backend_data (hash_table->dynobj);
3064 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3065 BFD_ASSERT (s != NULL);
3067 newsize = s->size + bed->s->sizeof_dyn;
3068 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3069 if (newcontents == NULL)
3073 dyn.d_un.d_val = val;
3074 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3077 s->contents = newcontents;
3082 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3083 otherwise just check whether one already exists. Returns -1 on error,
3084 1 if a DT_NEEDED tag already exists, and 0 on success. */
3087 elf_add_dt_needed_tag (bfd *abfd,
3088 struct bfd_link_info *info,
3092 struct elf_link_hash_table *hash_table;
3093 bfd_size_type strindex;
3095 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3098 hash_table = elf_hash_table (info);
3099 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3100 if (strindex == (bfd_size_type) -1)
3103 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3106 const struct elf_backend_data *bed;
3109 bed = get_elf_backend_data (hash_table->dynobj);
3110 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3112 for (extdyn = sdyn->contents;
3113 extdyn < sdyn->contents + sdyn->size;
3114 extdyn += bed->s->sizeof_dyn)
3116 Elf_Internal_Dyn dyn;
3118 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3119 if (dyn.d_tag == DT_NEEDED
3120 && dyn.d_un.d_val == strindex)
3122 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3130 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3133 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3137 /* We were just checking for existence of the tag. */
3138 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3144 on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3146 for (; needed != NULL; needed = needed->next)
3147 if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
3148 && strcmp (soname, needed->name) == 0)
3154 /* Sort symbol by value, section, and size. */
3156 elf_sort_symbol (const void *arg1, const void *arg2)
3158 const struct elf_link_hash_entry *h1;
3159 const struct elf_link_hash_entry *h2;
3160 bfd_signed_vma vdiff;
3162 h1 = *(const struct elf_link_hash_entry **) arg1;
3163 h2 = *(const struct elf_link_hash_entry **) arg2;
3164 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3166 return vdiff > 0 ? 1 : -1;
3169 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3171 return sdiff > 0 ? 1 : -1;
3173 vdiff = h1->size - h2->size;
3174 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3177 /* This function is used to adjust offsets into .dynstr for
3178 dynamic symbols. This is called via elf_link_hash_traverse. */
3181 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3183 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3185 if (h->dynindx != -1)
3186 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3190 /* Assign string offsets in .dynstr, update all structures referencing
3194 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3196 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3197 struct elf_link_local_dynamic_entry *entry;
3198 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3199 bfd *dynobj = hash_table->dynobj;
3202 const struct elf_backend_data *bed;
3205 _bfd_elf_strtab_finalize (dynstr);
3206 size = _bfd_elf_strtab_size (dynstr);
3208 bed = get_elf_backend_data (dynobj);
3209 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3210 BFD_ASSERT (sdyn != NULL);
3212 /* Update all .dynamic entries referencing .dynstr strings. */
3213 for (extdyn = sdyn->contents;
3214 extdyn < sdyn->contents + sdyn->size;
3215 extdyn += bed->s->sizeof_dyn)
3217 Elf_Internal_Dyn dyn;
3219 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3223 dyn.d_un.d_val = size;
3233 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3238 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3241 /* Now update local dynamic symbols. */
3242 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3243 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3244 entry->isym.st_name);
3246 /* And the rest of dynamic symbols. */
3247 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3249 /* Adjust version definitions. */
3250 if (elf_tdata (output_bfd)->cverdefs)
3255 Elf_Internal_Verdef def;
3256 Elf_Internal_Verdaux defaux;
3258 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3262 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3264 p += sizeof (Elf_External_Verdef);
3265 if (def.vd_aux != sizeof (Elf_External_Verdef))
3267 for (i = 0; i < def.vd_cnt; ++i)
3269 _bfd_elf_swap_verdaux_in (output_bfd,
3270 (Elf_External_Verdaux *) p, &defaux);
3271 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3273 _bfd_elf_swap_verdaux_out (output_bfd,
3274 &defaux, (Elf_External_Verdaux *) p);
3275 p += sizeof (Elf_External_Verdaux);
3278 while (def.vd_next);
3281 /* Adjust version references. */
3282 if (elf_tdata (output_bfd)->verref)
3287 Elf_Internal_Verneed need;
3288 Elf_Internal_Vernaux needaux;
3290 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3294 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3296 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3297 _bfd_elf_swap_verneed_out (output_bfd, &need,
3298 (Elf_External_Verneed *) p);
3299 p += sizeof (Elf_External_Verneed);
3300 for (i = 0; i < need.vn_cnt; ++i)
3302 _bfd_elf_swap_vernaux_in (output_bfd,
3303 (Elf_External_Vernaux *) p, &needaux);
3304 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3306 _bfd_elf_swap_vernaux_out (output_bfd,
3308 (Elf_External_Vernaux *) p);
3309 p += sizeof (Elf_External_Vernaux);
3312 while (need.vn_next);
3318 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3319 The default is to only match when the INPUT and OUTPUT are exactly
3323 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3324 const bfd_target *output)
3326 return input == output;
3329 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3330 This version is used when different targets for the same architecture
3331 are virtually identical. */
3334 _bfd_elf_relocs_compatible (const bfd_target *input,
3335 const bfd_target *output)
3337 const struct elf_backend_data *obed, *ibed;
3339 if (input == output)
3342 ibed = xvec_get_elf_backend_data (input);
3343 obed = xvec_get_elf_backend_data (output);
3345 if (ibed->arch != obed->arch)
3348 /* If both backends are using this function, deem them compatible. */
3349 return ibed->relocs_compatible == obed->relocs_compatible;
3352 /* Make a special call to the linker "notice" function to tell it that
3353 we are about to handle an as-needed lib, or have finished
3354 processing the lib. */
3357 _bfd_elf_notice_as_needed (bfd *ibfd,
3358 struct bfd_link_info *info,
3359 enum notice_asneeded_action act)
3361 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3364 /* Add symbols from an ELF object file to the linker hash table. */
3367 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3369 Elf_Internal_Ehdr *ehdr;
3370 Elf_Internal_Shdr *hdr;
3371 bfd_size_type symcount;
3372 bfd_size_type extsymcount;
3373 bfd_size_type extsymoff;
3374 struct elf_link_hash_entry **sym_hash;
3375 bfd_boolean dynamic;
3376 Elf_External_Versym *extversym = NULL;
3377 Elf_External_Versym *ever;
3378 struct elf_link_hash_entry *weaks;
3379 struct elf_link_hash_entry **nondeflt_vers = NULL;
3380 bfd_size_type nondeflt_vers_cnt = 0;
3381 Elf_Internal_Sym *isymbuf = NULL;
3382 Elf_Internal_Sym *isym;
3383 Elf_Internal_Sym *isymend;
3384 const struct elf_backend_data *bed;
3385 bfd_boolean add_needed;
3386 struct elf_link_hash_table *htab;
3388 void *alloc_mark = NULL;
3389 struct bfd_hash_entry **old_table = NULL;
3390 unsigned int old_size = 0;
3391 unsigned int old_count = 0;
3392 void *old_tab = NULL;
3394 struct bfd_link_hash_entry *old_undefs = NULL;
3395 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3396 long old_dynsymcount = 0;
3397 bfd_size_type old_dynstr_size = 0;
3400 bfd_boolean just_syms;
3402 htab = elf_hash_table (info);
3403 bed = get_elf_backend_data (abfd);
3405 if ((abfd->flags & DYNAMIC) == 0)
3411 /* You can't use -r against a dynamic object. Also, there's no
3412 hope of using a dynamic object which does not exactly match
3413 the format of the output file. */
3414 if (info->relocatable
3415 || !is_elf_hash_table (htab)
3416 || info->output_bfd->xvec != abfd->xvec)
3418 if (info->relocatable)
3419 bfd_set_error (bfd_error_invalid_operation);
3421 bfd_set_error (bfd_error_wrong_format);
3426 ehdr = elf_elfheader (abfd);
3427 if (info->warn_alternate_em
3428 && bed->elf_machine_code != ehdr->e_machine
3429 && ((bed->elf_machine_alt1 != 0
3430 && ehdr->e_machine == bed->elf_machine_alt1)
3431 || (bed->elf_machine_alt2 != 0
3432 && ehdr->e_machine == bed->elf_machine_alt2)))
3433 info->callbacks->einfo
3434 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3435 ehdr->e_machine, abfd, bed->elf_machine_code);
3437 /* As a GNU extension, any input sections which are named
3438 .gnu.warning.SYMBOL are treated as warning symbols for the given
3439 symbol. This differs from .gnu.warning sections, which generate
3440 warnings when they are included in an output file. */
3441 /* PR 12761: Also generate this warning when building shared libraries. */
3442 for (s = abfd->sections; s != NULL; s = s->next)
3446 name = bfd_get_section_name (abfd, s);
3447 if (CONST_STRNEQ (name, ".gnu.warning."))
3452 name += sizeof ".gnu.warning." - 1;
3454 /* If this is a shared object, then look up the symbol
3455 in the hash table. If it is there, and it is already
3456 been defined, then we will not be using the entry
3457 from this shared object, so we don't need to warn.
3458 FIXME: If we see the definition in a regular object
3459 later on, we will warn, but we shouldn't. The only
3460 fix is to keep track of what warnings we are supposed
3461 to emit, and then handle them all at the end of the
3465 struct elf_link_hash_entry *h;
3467 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3469 /* FIXME: What about bfd_link_hash_common? */
3471 && (h->root.type == bfd_link_hash_defined
3472 || h->root.type == bfd_link_hash_defweak))
3477 msg = (char *) bfd_alloc (abfd, sz + 1);
3481 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3486 if (! (_bfd_generic_link_add_one_symbol
3487 (info, abfd, name, BSF_WARNING, s, 0, msg,
3488 FALSE, bed->collect, NULL)))
3491 if (!info->relocatable && info->executable)
3493 /* Clobber the section size so that the warning does
3494 not get copied into the output file. */
3497 /* Also set SEC_EXCLUDE, so that symbols defined in
3498 the warning section don't get copied to the output. */
3499 s->flags |= SEC_EXCLUDE;
3504 just_syms = ((s = abfd->sections) != NULL
3505 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3510 /* If we are creating a shared library, create all the dynamic
3511 sections immediately. We need to attach them to something,
3512 so we attach them to this BFD, provided it is the right
3513 format and is not from ld --just-symbols. FIXME: If there
3514 are no input BFD's of the same format as the output, we can't
3515 make a shared library. */
3518 && is_elf_hash_table (htab)
3519 && info->output_bfd->xvec == abfd->xvec
3520 && !htab->dynamic_sections_created)
3522 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3526 else if (!is_elf_hash_table (htab))
3530 const char *soname = NULL;
3532 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3535 /* ld --just-symbols and dynamic objects don't mix very well.
3536 ld shouldn't allow it. */
3540 /* If this dynamic lib was specified on the command line with
3541 --as-needed in effect, then we don't want to add a DT_NEEDED
3542 tag unless the lib is actually used. Similary for libs brought
3543 in by another lib's DT_NEEDED. When --no-add-needed is used
3544 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3545 any dynamic library in DT_NEEDED tags in the dynamic lib at
3547 add_needed = (elf_dyn_lib_class (abfd)
3548 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3549 | DYN_NO_NEEDED)) == 0;
3551 s = bfd_get_section_by_name (abfd, ".dynamic");
3556 unsigned int elfsec;
3557 unsigned long shlink;
3559 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3566 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3567 if (elfsec == SHN_BAD)
3568 goto error_free_dyn;
3569 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3571 for (extdyn = dynbuf;
3572 extdyn < dynbuf + s->size;
3573 extdyn += bed->s->sizeof_dyn)
3575 Elf_Internal_Dyn dyn;
3577 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3578 if (dyn.d_tag == DT_SONAME)
3580 unsigned int tagv = dyn.d_un.d_val;
3581 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3583 goto error_free_dyn;
3585 if (dyn.d_tag == DT_NEEDED)
3587 struct bfd_link_needed_list *n, **pn;
3589 unsigned int tagv = dyn.d_un.d_val;
3591 amt = sizeof (struct bfd_link_needed_list);
3592 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3593 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3594 if (n == NULL || fnm == NULL)
3595 goto error_free_dyn;
3596 amt = strlen (fnm) + 1;
3597 anm = (char *) bfd_alloc (abfd, amt);
3599 goto error_free_dyn;
3600 memcpy (anm, fnm, amt);
3604 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3608 if (dyn.d_tag == DT_RUNPATH)
3610 struct bfd_link_needed_list *n, **pn;
3612 unsigned int tagv = dyn.d_un.d_val;
3614 amt = sizeof (struct bfd_link_needed_list);
3615 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3616 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3617 if (n == NULL || fnm == NULL)
3618 goto error_free_dyn;
3619 amt = strlen (fnm) + 1;
3620 anm = (char *) bfd_alloc (abfd, amt);
3622 goto error_free_dyn;
3623 memcpy (anm, fnm, amt);
3627 for (pn = & runpath;
3633 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3634 if (!runpath && dyn.d_tag == DT_RPATH)
3636 struct bfd_link_needed_list *n, **pn;
3638 unsigned int tagv = dyn.d_un.d_val;
3640 amt = sizeof (struct bfd_link_needed_list);
3641 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3642 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3643 if (n == NULL || fnm == NULL)
3644 goto error_free_dyn;
3645 amt = strlen (fnm) + 1;
3646 anm = (char *) bfd_alloc (abfd, amt);
3648 goto error_free_dyn;
3649 memcpy (anm, fnm, amt);
3659 if (dyn.d_tag == DT_AUDIT)
3661 unsigned int tagv = dyn.d_un.d_val;
3662 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3669 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3670 frees all more recently bfd_alloc'd blocks as well. */
3676 struct bfd_link_needed_list **pn;
3677 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3682 /* We do not want to include any of the sections in a dynamic
3683 object in the output file. We hack by simply clobbering the
3684 list of sections in the BFD. This could be handled more
3685 cleanly by, say, a new section flag; the existing
3686 SEC_NEVER_LOAD flag is not the one we want, because that one
3687 still implies that the section takes up space in the output
3689 bfd_section_list_clear (abfd);
3691 /* Find the name to use in a DT_NEEDED entry that refers to this
3692 object. If the object has a DT_SONAME entry, we use it.
3693 Otherwise, if the generic linker stuck something in
3694 elf_dt_name, we use that. Otherwise, we just use the file
3696 if (soname == NULL || *soname == '\0')
3698 soname = elf_dt_name (abfd);
3699 if (soname == NULL || *soname == '\0')
3700 soname = bfd_get_filename (abfd);
3703 /* Save the SONAME because sometimes the linker emulation code
3704 will need to know it. */
3705 elf_dt_name (abfd) = soname;
3707 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3711 /* If we have already included this dynamic object in the
3712 link, just ignore it. There is no reason to include a
3713 particular dynamic object more than once. */
3717 /* Save the DT_AUDIT entry for the linker emulation code. */
3718 elf_dt_audit (abfd) = audit;
3721 /* If this is a dynamic object, we always link against the .dynsym
3722 symbol table, not the .symtab symbol table. The dynamic linker
3723 will only see the .dynsym symbol table, so there is no reason to
3724 look at .symtab for a dynamic object. */
3726 if (! dynamic || elf_dynsymtab (abfd) == 0)
3727 hdr = &elf_tdata (abfd)->symtab_hdr;
3729 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3731 symcount = hdr->sh_size / bed->s->sizeof_sym;
3733 /* The sh_info field of the symtab header tells us where the
3734 external symbols start. We don't care about the local symbols at
3736 if (elf_bad_symtab (abfd))
3738 extsymcount = symcount;
3743 extsymcount = symcount - hdr->sh_info;
3744 extsymoff = hdr->sh_info;
3747 sym_hash = elf_sym_hashes (abfd);
3748 if (extsymcount != 0)
3750 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3752 if (isymbuf == NULL)
3755 if (sym_hash == NULL)
3757 /* We store a pointer to the hash table entry for each
3759 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3760 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3761 if (sym_hash == NULL)
3762 goto error_free_sym;
3763 elf_sym_hashes (abfd) = sym_hash;
3769 /* Read in any version definitions. */
3770 if (!_bfd_elf_slurp_version_tables (abfd,
3771 info->default_imported_symver))
3772 goto error_free_sym;
3774 /* Read in the symbol versions, but don't bother to convert them
3775 to internal format. */
3776 if (elf_dynversym (abfd) != 0)
3778 Elf_Internal_Shdr *versymhdr;
3780 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3781 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3782 if (extversym == NULL)
3783 goto error_free_sym;
3784 amt = versymhdr->sh_size;
3785 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3786 || bfd_bread (extversym, amt, abfd) != amt)
3787 goto error_free_vers;
3791 /* If we are loading an as-needed shared lib, save the symbol table
3792 state before we start adding symbols. If the lib turns out
3793 to be unneeded, restore the state. */
3794 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3799 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3801 struct bfd_hash_entry *p;
3802 struct elf_link_hash_entry *h;
3804 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3806 h = (struct elf_link_hash_entry *) p;
3807 entsize += htab->root.table.entsize;
3808 if (h->root.type == bfd_link_hash_warning)
3809 entsize += htab->root.table.entsize;
3813 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3814 old_tab = bfd_malloc (tabsize + entsize);
3815 if (old_tab == NULL)
3816 goto error_free_vers;
3818 /* Remember the current objalloc pointer, so that all mem for
3819 symbols added can later be reclaimed. */
3820 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3821 if (alloc_mark == NULL)
3822 goto error_free_vers;
3824 /* Make a special call to the linker "notice" function to
3825 tell it that we are about to handle an as-needed lib. */
3826 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
3827 goto error_free_vers;
3829 /* Clone the symbol table. Remember some pointers into the
3830 symbol table, and dynamic symbol count. */
3831 old_ent = (char *) old_tab + tabsize;
3832 memcpy (old_tab, htab->root.table.table, tabsize);
3833 old_undefs = htab->root.undefs;
3834 old_undefs_tail = htab->root.undefs_tail;
3835 old_table = htab->root.table.table;
3836 old_size = htab->root.table.size;
3837 old_count = htab->root.table.count;
3838 old_dynsymcount = htab->dynsymcount;
3839 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
3841 for (i = 0; i < htab->root.table.size; i++)
3843 struct bfd_hash_entry *p;
3844 struct elf_link_hash_entry *h;
3846 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3848 memcpy (old_ent, p, htab->root.table.entsize);
3849 old_ent = (char *) old_ent + htab->root.table.entsize;
3850 h = (struct elf_link_hash_entry *) p;
3851 if (h->root.type == bfd_link_hash_warning)
3853 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3854 old_ent = (char *) old_ent + htab->root.table.entsize;
3861 ever = extversym != NULL ? extversym + extsymoff : NULL;
3862 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3864 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3868 asection *sec, *new_sec;
3871 struct elf_link_hash_entry *h;
3872 struct elf_link_hash_entry *hi;
3873 bfd_boolean definition;
3874 bfd_boolean size_change_ok;
3875 bfd_boolean type_change_ok;
3876 bfd_boolean new_weakdef;
3877 bfd_boolean new_weak;
3878 bfd_boolean old_weak;
3879 bfd_boolean override;
3881 unsigned int old_alignment;
3886 flags = BSF_NO_FLAGS;
3888 value = isym->st_value;
3889 common = bed->common_definition (isym);
3891 bind = ELF_ST_BIND (isym->st_info);
3895 /* This should be impossible, since ELF requires that all
3896 global symbols follow all local symbols, and that sh_info
3897 point to the first global symbol. Unfortunately, Irix 5
3902 if (isym->st_shndx != SHN_UNDEF && !common)
3910 case STB_GNU_UNIQUE:
3911 flags = BSF_GNU_UNIQUE;
3915 /* Leave it up to the processor backend. */
3919 if (isym->st_shndx == SHN_UNDEF)
3920 sec = bfd_und_section_ptr;
3921 else if (isym->st_shndx == SHN_ABS)
3922 sec = bfd_abs_section_ptr;
3923 else if (isym->st_shndx == SHN_COMMON)
3925 sec = bfd_com_section_ptr;
3926 /* What ELF calls the size we call the value. What ELF
3927 calls the value we call the alignment. */
3928 value = isym->st_size;
3932 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3934 sec = bfd_abs_section_ptr;
3935 else if (discarded_section (sec))
3937 /* Symbols from discarded section are undefined. We keep
3939 sec = bfd_und_section_ptr;
3940 isym->st_shndx = SHN_UNDEF;
3942 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3946 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3949 goto error_free_vers;
3951 if (isym->st_shndx == SHN_COMMON
3952 && (abfd->flags & BFD_PLUGIN) != 0)
3954 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
3958 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
3960 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
3962 goto error_free_vers;
3966 else if (isym->st_shndx == SHN_COMMON
3967 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3968 && !info->relocatable)
3970 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3974 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
3975 | SEC_LINKER_CREATED);
3976 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3978 goto error_free_vers;
3982 else if (bed->elf_add_symbol_hook)
3984 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3986 goto error_free_vers;
3988 /* The hook function sets the name to NULL if this symbol
3989 should be skipped for some reason. */
3994 /* Sanity check that all possibilities were handled. */
3997 bfd_set_error (bfd_error_bad_value);
3998 goto error_free_vers;
4001 /* Silently discard TLS symbols from --just-syms. There's
4002 no way to combine a static TLS block with a new TLS block
4003 for this executable. */
4004 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4005 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4008 if (bfd_is_und_section (sec)
4009 || bfd_is_com_section (sec))
4014 size_change_ok = FALSE;
4015 type_change_ok = bed->type_change_ok;
4021 if (is_elf_hash_table (htab))
4023 Elf_Internal_Versym iver;
4024 unsigned int vernum = 0;
4029 if (info->default_imported_symver)
4030 /* Use the default symbol version created earlier. */
4031 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4036 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4038 vernum = iver.vs_vers & VERSYM_VERSION;
4040 /* If this is a hidden symbol, or if it is not version
4041 1, we append the version name to the symbol name.
4042 However, we do not modify a non-hidden absolute symbol
4043 if it is not a function, because it might be the version
4044 symbol itself. FIXME: What if it isn't? */
4045 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4047 && (!bfd_is_abs_section (sec)
4048 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4051 size_t namelen, verlen, newlen;
4054 if (isym->st_shndx != SHN_UNDEF)
4056 if (vernum > elf_tdata (abfd)->cverdefs)
4058 else if (vernum > 1)
4060 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4066 (*_bfd_error_handler)
4067 (_("%B: %s: invalid version %u (max %d)"),
4069 elf_tdata (abfd)->cverdefs);
4070 bfd_set_error (bfd_error_bad_value);
4071 goto error_free_vers;
4076 /* We cannot simply test for the number of
4077 entries in the VERNEED section since the
4078 numbers for the needed versions do not start
4080 Elf_Internal_Verneed *t;
4083 for (t = elf_tdata (abfd)->verref;
4087 Elf_Internal_Vernaux *a;
4089 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4091 if (a->vna_other == vernum)
4093 verstr = a->vna_nodename;
4102 (*_bfd_error_handler)
4103 (_("%B: %s: invalid needed version %d"),
4104 abfd, name, vernum);
4105 bfd_set_error (bfd_error_bad_value);
4106 goto error_free_vers;
4110 namelen = strlen (name);
4111 verlen = strlen (verstr);
4112 newlen = namelen + verlen + 2;
4113 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4114 && isym->st_shndx != SHN_UNDEF)
4117 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4118 if (newname == NULL)
4119 goto error_free_vers;
4120 memcpy (newname, name, namelen);
4121 p = newname + namelen;
4123 /* If this is a defined non-hidden version symbol,
4124 we add another @ to the name. This indicates the
4125 default version of the symbol. */
4126 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4127 && isym->st_shndx != SHN_UNDEF)
4129 memcpy (p, verstr, verlen + 1);
4134 /* If this symbol has default visibility and the user has
4135 requested we not re-export it, then mark it as hidden. */
4139 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4140 isym->st_other = (STV_HIDDEN
4141 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4143 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4144 sym_hash, &old_bfd, &old_weak,
4145 &old_alignment, &skip, &override,
4146 &type_change_ok, &size_change_ok))
4147 goto error_free_vers;
4156 while (h->root.type == bfd_link_hash_indirect
4157 || h->root.type == bfd_link_hash_warning)
4158 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4160 if (elf_tdata (abfd)->verdef != NULL
4163 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4166 if (! (_bfd_generic_link_add_one_symbol
4167 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4168 (struct bfd_link_hash_entry **) sym_hash)))
4169 goto error_free_vers;
4172 /* We need to make sure that indirect symbol dynamic flags are
4175 while (h->root.type == bfd_link_hash_indirect
4176 || h->root.type == bfd_link_hash_warning)
4177 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4181 new_weak = (flags & BSF_WEAK) != 0;
4182 new_weakdef = FALSE;
4186 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4187 && is_elf_hash_table (htab)
4188 && h->u.weakdef == NULL)
4190 /* Keep a list of all weak defined non function symbols from
4191 a dynamic object, using the weakdef field. Later in this
4192 function we will set the weakdef field to the correct
4193 value. We only put non-function symbols from dynamic
4194 objects on this list, because that happens to be the only
4195 time we need to know the normal symbol corresponding to a
4196 weak symbol, and the information is time consuming to
4197 figure out. If the weakdef field is not already NULL,
4198 then this symbol was already defined by some previous
4199 dynamic object, and we will be using that previous
4200 definition anyhow. */
4202 h->u.weakdef = weaks;
4207 /* Set the alignment of a common symbol. */
4208 if ((common || bfd_is_com_section (sec))
4209 && h->root.type == bfd_link_hash_common)
4214 align = bfd_log2 (isym->st_value);
4217 /* The new symbol is a common symbol in a shared object.
4218 We need to get the alignment from the section. */
4219 align = new_sec->alignment_power;
4221 if (align > old_alignment)
4222 h->root.u.c.p->alignment_power = align;
4224 h->root.u.c.p->alignment_power = old_alignment;
4227 if (is_elf_hash_table (htab))
4229 /* Set a flag in the hash table entry indicating the type of
4230 reference or definition we just found. A dynamic symbol
4231 is one which is referenced or defined by both a regular
4232 object and a shared object. */
4233 bfd_boolean dynsym = FALSE;
4235 /* Plugin symbols aren't normal. Don't set def_regular or
4236 ref_regular for them, or make them dynamic. */
4237 if ((abfd->flags & BFD_PLUGIN) != 0)
4244 if (bind != STB_WEAK)
4245 h->ref_regular_nonweak = 1;
4257 /* If the indirect symbol has been forced local, don't
4258 make the real symbol dynamic. */
4259 if ((h == hi || !hi->forced_local)
4260 && (! info->executable
4270 hi->ref_dynamic = 1;
4275 hi->def_dynamic = 1;
4278 /* If the indirect symbol has been forced local, don't
4279 make the real symbol dynamic. */
4280 if ((h == hi || !hi->forced_local)
4283 || (h->u.weakdef != NULL
4285 && h->u.weakdef->dynindx != -1)))
4289 /* Check to see if we need to add an indirect symbol for
4290 the default name. */
4292 || (!override && h->root.type == bfd_link_hash_common))
4293 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4294 sec, value, &old_bfd, &dynsym))
4295 goto error_free_vers;
4297 /* Check the alignment when a common symbol is involved. This
4298 can change when a common symbol is overridden by a normal
4299 definition or a common symbol is ignored due to the old
4300 normal definition. We need to make sure the maximum
4301 alignment is maintained. */
4302 if ((old_alignment || common)
4303 && h->root.type != bfd_link_hash_common)
4305 unsigned int common_align;
4306 unsigned int normal_align;
4307 unsigned int symbol_align;
4311 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4312 || h->root.type == bfd_link_hash_defweak);
4314 symbol_align = ffs (h->root.u.def.value) - 1;
4315 if (h->root.u.def.section->owner != NULL
4316 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4318 normal_align = h->root.u.def.section->alignment_power;
4319 if (normal_align > symbol_align)
4320 normal_align = symbol_align;
4323 normal_align = symbol_align;
4327 common_align = old_alignment;
4328 common_bfd = old_bfd;
4333 common_align = bfd_log2 (isym->st_value);
4335 normal_bfd = old_bfd;
4338 if (normal_align < common_align)
4340 /* PR binutils/2735 */
4341 if (normal_bfd == NULL)
4342 (*_bfd_error_handler)
4343 (_("Warning: alignment %u of common symbol `%s' in %B is"
4344 " greater than the alignment (%u) of its section %A"),
4345 common_bfd, h->root.u.def.section,
4346 1 << common_align, name, 1 << normal_align);
4348 (*_bfd_error_handler)
4349 (_("Warning: alignment %u of symbol `%s' in %B"
4350 " is smaller than %u in %B"),
4351 normal_bfd, common_bfd,
4352 1 << normal_align, name, 1 << common_align);
4356 /* Remember the symbol size if it isn't undefined. */
4357 if (isym->st_size != 0
4358 && isym->st_shndx != SHN_UNDEF
4359 && (definition || h->size == 0))
4362 && h->size != isym->st_size
4363 && ! size_change_ok)
4364 (*_bfd_error_handler)
4365 (_("Warning: size of symbol `%s' changed"
4366 " from %lu in %B to %lu in %B"),
4368 name, (unsigned long) h->size,
4369 (unsigned long) isym->st_size);
4371 h->size = isym->st_size;
4374 /* If this is a common symbol, then we always want H->SIZE
4375 to be the size of the common symbol. The code just above
4376 won't fix the size if a common symbol becomes larger. We
4377 don't warn about a size change here, because that is
4378 covered by --warn-common. Allow changes between different
4380 if (h->root.type == bfd_link_hash_common)
4381 h->size = h->root.u.c.size;
4383 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4384 && ((definition && !new_weak)
4385 || (old_weak && h->root.type == bfd_link_hash_common)
4386 || h->type == STT_NOTYPE))
4388 unsigned int type = ELF_ST_TYPE (isym->st_info);
4390 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4392 if (type == STT_GNU_IFUNC
4393 && (abfd->flags & DYNAMIC) != 0)
4396 if (h->type != type)
4398 if (h->type != STT_NOTYPE && ! type_change_ok)
4399 (*_bfd_error_handler)
4400 (_("Warning: type of symbol `%s' changed"
4401 " from %d to %d in %B"),
4402 abfd, name, h->type, type);
4408 /* Merge st_other field. */
4409 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4411 /* We don't want to make debug symbol dynamic. */
4412 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
4415 /* Nor should we make plugin symbols dynamic. */
4416 if ((abfd->flags & BFD_PLUGIN) != 0)
4421 h->target_internal = isym->st_target_internal;
4422 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4425 if (definition && !dynamic)
4427 char *p = strchr (name, ELF_VER_CHR);
4428 if (p != NULL && p[1] != ELF_VER_CHR)
4430 /* Queue non-default versions so that .symver x, x@FOO
4431 aliases can be checked. */
4434 amt = ((isymend - isym + 1)
4435 * sizeof (struct elf_link_hash_entry *));
4437 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4439 goto error_free_vers;
4441 nondeflt_vers[nondeflt_vers_cnt++] = h;
4445 if (dynsym && h->dynindx == -1)
4447 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4448 goto error_free_vers;
4449 if (h->u.weakdef != NULL
4451 && h->u.weakdef->dynindx == -1)
4453 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4454 goto error_free_vers;
4457 else if (dynsym && h->dynindx != -1)
4458 /* If the symbol already has a dynamic index, but
4459 visibility says it should not be visible, turn it into
4461 switch (ELF_ST_VISIBILITY (h->other))
4465 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4470 /* Don't add DT_NEEDED for references from the dummy bfd. */
4474 && h->ref_regular_nonweak
4476 || (old_bfd->flags & BFD_PLUGIN) == 0))
4477 || (h->ref_dynamic_nonweak
4478 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4479 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4482 const char *soname = elf_dt_name (abfd);
4484 info->callbacks->minfo ("%!", soname, old_bfd,
4485 h->root.root.string);
4487 /* A symbol from a library loaded via DT_NEEDED of some
4488 other library is referenced by a regular object.
4489 Add a DT_NEEDED entry for it. Issue an error if
4490 --no-add-needed is used and the reference was not
4493 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4495 (*_bfd_error_handler)
4496 (_("%B: undefined reference to symbol '%s'"),
4498 bfd_set_error (bfd_error_missing_dso);
4499 goto error_free_vers;
4502 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4503 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4506 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4508 goto error_free_vers;
4510 BFD_ASSERT (ret == 0);
4515 if (extversym != NULL)
4521 if (isymbuf != NULL)
4527 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4531 /* Restore the symbol table. */
4532 old_ent = (char *) old_tab + tabsize;
4533 memset (elf_sym_hashes (abfd), 0,
4534 extsymcount * sizeof (struct elf_link_hash_entry *));
4535 htab->root.table.table = old_table;
4536 htab->root.table.size = old_size;
4537 htab->root.table.count = old_count;
4538 memcpy (htab->root.table.table, old_tab, tabsize);
4539 htab->root.undefs = old_undefs;
4540 htab->root.undefs_tail = old_undefs_tail;
4541 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
4542 for (i = 0; i < htab->root.table.size; i++)
4544 struct bfd_hash_entry *p;
4545 struct elf_link_hash_entry *h;
4547 unsigned int alignment_power;
4549 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4551 h = (struct elf_link_hash_entry *) p;
4552 if (h->root.type == bfd_link_hash_warning)
4553 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4554 if (h->dynindx >= old_dynsymcount
4555 && h->dynstr_index < old_dynstr_size)
4556 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4558 /* Preserve the maximum alignment and size for common
4559 symbols even if this dynamic lib isn't on DT_NEEDED
4560 since it can still be loaded at run time by another
4562 if (h->root.type == bfd_link_hash_common)
4564 size = h->root.u.c.size;
4565 alignment_power = h->root.u.c.p->alignment_power;
4570 alignment_power = 0;
4572 memcpy (p, old_ent, htab->root.table.entsize);
4573 old_ent = (char *) old_ent + htab->root.table.entsize;
4574 h = (struct elf_link_hash_entry *) p;
4575 if (h->root.type == bfd_link_hash_warning)
4577 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4578 old_ent = (char *) old_ent + htab->root.table.entsize;
4579 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4581 if (h->root.type == bfd_link_hash_common)
4583 if (size > h->root.u.c.size)
4584 h->root.u.c.size = size;
4585 if (alignment_power > h->root.u.c.p->alignment_power)
4586 h->root.u.c.p->alignment_power = alignment_power;
4591 /* Make a special call to the linker "notice" function to
4592 tell it that symbols added for crefs may need to be removed. */
4593 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4594 goto error_free_vers;
4597 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4599 if (nondeflt_vers != NULL)
4600 free (nondeflt_vers);
4604 if (old_tab != NULL)
4606 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4607 goto error_free_vers;
4612 /* Now that all the symbols from this input file are created, handle
4613 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4614 if (nondeflt_vers != NULL)
4616 bfd_size_type cnt, symidx;
4618 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4620 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4621 char *shortname, *p;
4623 p = strchr (h->root.root.string, ELF_VER_CHR);
4625 || (h->root.type != bfd_link_hash_defined
4626 && h->root.type != bfd_link_hash_defweak))
4629 amt = p - h->root.root.string;
4630 shortname = (char *) bfd_malloc (amt + 1);
4632 goto error_free_vers;
4633 memcpy (shortname, h->root.root.string, amt);
4634 shortname[amt] = '\0';
4636 hi = (struct elf_link_hash_entry *)
4637 bfd_link_hash_lookup (&htab->root, shortname,
4638 FALSE, FALSE, FALSE);
4640 && hi->root.type == h->root.type
4641 && hi->root.u.def.value == h->root.u.def.value
4642 && hi->root.u.def.section == h->root.u.def.section)
4644 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4645 hi->root.type = bfd_link_hash_indirect;
4646 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4647 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4648 sym_hash = elf_sym_hashes (abfd);
4650 for (symidx = 0; symidx < extsymcount; ++symidx)
4651 if (sym_hash[symidx] == hi)
4653 sym_hash[symidx] = h;
4659 free (nondeflt_vers);
4660 nondeflt_vers = NULL;
4663 /* Now set the weakdefs field correctly for all the weak defined
4664 symbols we found. The only way to do this is to search all the
4665 symbols. Since we only need the information for non functions in
4666 dynamic objects, that's the only time we actually put anything on
4667 the list WEAKS. We need this information so that if a regular
4668 object refers to a symbol defined weakly in a dynamic object, the
4669 real symbol in the dynamic object is also put in the dynamic
4670 symbols; we also must arrange for both symbols to point to the
4671 same memory location. We could handle the general case of symbol
4672 aliasing, but a general symbol alias can only be generated in
4673 assembler code, handling it correctly would be very time
4674 consuming, and other ELF linkers don't handle general aliasing
4678 struct elf_link_hash_entry **hpp;
4679 struct elf_link_hash_entry **hppend;
4680 struct elf_link_hash_entry **sorted_sym_hash;
4681 struct elf_link_hash_entry *h;
4684 /* Since we have to search the whole symbol list for each weak
4685 defined symbol, search time for N weak defined symbols will be
4686 O(N^2). Binary search will cut it down to O(NlogN). */
4687 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4688 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4689 if (sorted_sym_hash == NULL)
4691 sym_hash = sorted_sym_hash;
4692 hpp = elf_sym_hashes (abfd);
4693 hppend = hpp + extsymcount;
4695 for (; hpp < hppend; hpp++)
4699 && h->root.type == bfd_link_hash_defined
4700 && !bed->is_function_type (h->type))
4708 qsort (sorted_sym_hash, sym_count,
4709 sizeof (struct elf_link_hash_entry *),
4712 while (weaks != NULL)
4714 struct elf_link_hash_entry *hlook;
4717 size_t i, j, idx = 0;
4720 weaks = hlook->u.weakdef;
4721 hlook->u.weakdef = NULL;
4723 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4724 || hlook->root.type == bfd_link_hash_defweak
4725 || hlook->root.type == bfd_link_hash_common
4726 || hlook->root.type == bfd_link_hash_indirect);
4727 slook = hlook->root.u.def.section;
4728 vlook = hlook->root.u.def.value;
4734 bfd_signed_vma vdiff;
4736 h = sorted_sym_hash[idx];
4737 vdiff = vlook - h->root.u.def.value;
4744 long sdiff = slook->id - h->root.u.def.section->id;
4754 /* We didn't find a value/section match. */
4758 /* With multiple aliases, or when the weak symbol is already
4759 strongly defined, we have multiple matching symbols and
4760 the binary search above may land on any of them. Step
4761 one past the matching symbol(s). */
4764 h = sorted_sym_hash[idx];
4765 if (h->root.u.def.section != slook
4766 || h->root.u.def.value != vlook)
4770 /* Now look back over the aliases. Since we sorted by size
4771 as well as value and section, we'll choose the one with
4772 the largest size. */
4775 h = sorted_sym_hash[idx];
4777 /* Stop if value or section doesn't match. */
4778 if (h->root.u.def.section != slook
4779 || h->root.u.def.value != vlook)
4781 else if (h != hlook)
4783 hlook->u.weakdef = h;
4785 /* If the weak definition is in the list of dynamic
4786 symbols, make sure the real definition is put
4788 if (hlook->dynindx != -1 && h->dynindx == -1)
4790 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4793 free (sorted_sym_hash);
4798 /* If the real definition is in the list of dynamic
4799 symbols, make sure the weak definition is put
4800 there as well. If we don't do this, then the
4801 dynamic loader might not merge the entries for the
4802 real definition and the weak definition. */
4803 if (h->dynindx != -1 && hlook->dynindx == -1)
4805 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4806 goto err_free_sym_hash;
4813 free (sorted_sym_hash);
4816 if (bed->check_directives
4817 && !(*bed->check_directives) (abfd, info))
4820 /* If this object is the same format as the output object, and it is
4821 not a shared library, then let the backend look through the
4824 This is required to build global offset table entries and to
4825 arrange for dynamic relocs. It is not required for the
4826 particular common case of linking non PIC code, even when linking
4827 against shared libraries, but unfortunately there is no way of
4828 knowing whether an object file has been compiled PIC or not.
4829 Looking through the relocs is not particularly time consuming.
4830 The problem is that we must either (1) keep the relocs in memory,
4831 which causes the linker to require additional runtime memory or
4832 (2) read the relocs twice from the input file, which wastes time.
4833 This would be a good case for using mmap.
4835 I have no idea how to handle linking PIC code into a file of a
4836 different format. It probably can't be done. */
4838 && is_elf_hash_table (htab)
4839 && bed->check_relocs != NULL
4840 && elf_object_id (abfd) == elf_hash_table_id (htab)
4841 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4845 for (o = abfd->sections; o != NULL; o = o->next)
4847 Elf_Internal_Rela *internal_relocs;
4850 if ((o->flags & SEC_RELOC) == 0
4851 || o->reloc_count == 0
4852 || ((info->strip == strip_all || info->strip == strip_debugger)
4853 && (o->flags & SEC_DEBUGGING) != 0)
4854 || bfd_is_abs_section (o->output_section))
4857 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4859 if (internal_relocs == NULL)
4862 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4864 if (elf_section_data (o)->relocs != internal_relocs)
4865 free (internal_relocs);
4872 /* If this is a non-traditional link, try to optimize the handling
4873 of the .stab/.stabstr sections. */
4875 && ! info->traditional_format
4876 && is_elf_hash_table (htab)
4877 && (info->strip != strip_all && info->strip != strip_debugger))
4881 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4882 if (stabstr != NULL)
4884 bfd_size_type string_offset = 0;
4887 for (stab = abfd->sections; stab; stab = stab->next)
4888 if (CONST_STRNEQ (stab->name, ".stab")
4889 && (!stab->name[5] ||
4890 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4891 && (stab->flags & SEC_MERGE) == 0
4892 && !bfd_is_abs_section (stab->output_section))
4894 struct bfd_elf_section_data *secdata;
4896 secdata = elf_section_data (stab);
4897 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4898 stabstr, &secdata->sec_info,
4901 if (secdata->sec_info)
4902 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4907 if (is_elf_hash_table (htab) && add_needed)
4909 /* Add this bfd to the loaded list. */
4910 struct elf_link_loaded_list *n;
4912 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4916 n->next = htab->loaded;
4923 if (old_tab != NULL)
4925 if (nondeflt_vers != NULL)
4926 free (nondeflt_vers);
4927 if (extversym != NULL)
4930 if (isymbuf != NULL)
4936 /* Return the linker hash table entry of a symbol that might be
4937 satisfied by an archive symbol. Return -1 on error. */
4939 struct elf_link_hash_entry *
4940 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4941 struct bfd_link_info *info,
4944 struct elf_link_hash_entry *h;
4948 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
4952 /* If this is a default version (the name contains @@), look up the
4953 symbol again with only one `@' as well as without the version.
4954 The effect is that references to the symbol with and without the
4955 version will be matched by the default symbol in the archive. */
4957 p = strchr (name, ELF_VER_CHR);
4958 if (p == NULL || p[1] != ELF_VER_CHR)
4961 /* First check with only one `@'. */
4962 len = strlen (name);
4963 copy = (char *) bfd_alloc (abfd, len);
4965 return (struct elf_link_hash_entry *) 0 - 1;
4967 first = p - name + 1;
4968 memcpy (copy, name, first);
4969 memcpy (copy + first, name + first + 1, len - first);
4971 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
4974 /* We also need to check references to the symbol without the
4976 copy[first - 1] = '\0';
4977 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4978 FALSE, FALSE, TRUE);
4981 bfd_release (abfd, copy);
4985 /* Add symbols from an ELF archive file to the linker hash table. We
4986 don't use _bfd_generic_link_add_archive_symbols because we need to
4987 handle versioned symbols.
4989 Fortunately, ELF archive handling is simpler than that done by
4990 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4991 oddities. In ELF, if we find a symbol in the archive map, and the
4992 symbol is currently undefined, we know that we must pull in that
4995 Unfortunately, we do have to make multiple passes over the symbol
4996 table until nothing further is resolved. */
4999 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5002 unsigned char *included = NULL;
5006 const struct elf_backend_data *bed;
5007 struct elf_link_hash_entry * (*archive_symbol_lookup)
5008 (bfd *, struct bfd_link_info *, const char *);
5010 if (! bfd_has_map (abfd))
5012 /* An empty archive is a special case. */
5013 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5015 bfd_set_error (bfd_error_no_armap);
5019 /* Keep track of all symbols we know to be already defined, and all
5020 files we know to be already included. This is to speed up the
5021 second and subsequent passes. */
5022 c = bfd_ardata (abfd)->symdef_count;
5026 amt *= sizeof (*included);
5027 included = (unsigned char *) bfd_zmalloc (amt);
5028 if (included == NULL)
5031 symdefs = bfd_ardata (abfd)->symdefs;
5032 bed = get_elf_backend_data (abfd);
5033 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5046 symdefend = symdef + c;
5047 for (i = 0; symdef < symdefend; symdef++, i++)
5049 struct elf_link_hash_entry *h;
5051 struct bfd_link_hash_entry *undefs_tail;
5056 if (symdef->file_offset == last)
5062 h = archive_symbol_lookup (abfd, info, symdef->name);
5063 if (h == (struct elf_link_hash_entry *) 0 - 1)
5069 if (h->root.type == bfd_link_hash_common)
5071 /* We currently have a common symbol. The archive map contains
5072 a reference to this symbol, so we may want to include it. We
5073 only want to include it however, if this archive element
5074 contains a definition of the symbol, not just another common
5077 Unfortunately some archivers (including GNU ar) will put
5078 declarations of common symbols into their archive maps, as
5079 well as real definitions, so we cannot just go by the archive
5080 map alone. Instead we must read in the element's symbol
5081 table and check that to see what kind of symbol definition
5083 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5086 else if (h->root.type != bfd_link_hash_undefined)
5088 if (h->root.type != bfd_link_hash_undefweak)
5089 /* Symbol must be defined. Don't check it again. */
5094 /* We need to include this archive member. */
5095 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5096 if (element == NULL)
5099 if (! bfd_check_format (element, bfd_object))
5102 undefs_tail = info->hash->undefs_tail;
5104 if (!(*info->callbacks
5105 ->add_archive_element) (info, element, symdef->name, &element))
5107 if (!bfd_link_add_symbols (element, info))
5110 /* If there are any new undefined symbols, we need to make
5111 another pass through the archive in order to see whether
5112 they can be defined. FIXME: This isn't perfect, because
5113 common symbols wind up on undefs_tail and because an
5114 undefined symbol which is defined later on in this pass
5115 does not require another pass. This isn't a bug, but it
5116 does make the code less efficient than it could be. */
5117 if (undefs_tail != info->hash->undefs_tail)
5120 /* Look backward to mark all symbols from this object file
5121 which we have already seen in this pass. */
5125 included[mark] = TRUE;
5130 while (symdefs[mark].file_offset == symdef->file_offset);
5132 /* We mark subsequent symbols from this object file as we go
5133 on through the loop. */
5134 last = symdef->file_offset;
5144 if (included != NULL)
5149 /* Given an ELF BFD, add symbols to the global hash table as
5153 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5155 switch (bfd_get_format (abfd))
5158 return elf_link_add_object_symbols (abfd, info);
5160 return elf_link_add_archive_symbols (abfd, info);
5162 bfd_set_error (bfd_error_wrong_format);
5167 struct hash_codes_info
5169 unsigned long *hashcodes;
5173 /* This function will be called though elf_link_hash_traverse to store
5174 all hash value of the exported symbols in an array. */
5177 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5179 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5185 /* Ignore indirect symbols. These are added by the versioning code. */
5186 if (h->dynindx == -1)
5189 name = h->root.root.string;
5190 p = strchr (name, ELF_VER_CHR);
5193 alc = (char *) bfd_malloc (p - name + 1);
5199 memcpy (alc, name, p - name);
5200 alc[p - name] = '\0';
5204 /* Compute the hash value. */
5205 ha = bfd_elf_hash (name);
5207 /* Store the found hash value in the array given as the argument. */
5208 *(inf->hashcodes)++ = ha;
5210 /* And store it in the struct so that we can put it in the hash table
5212 h->u.elf_hash_value = ha;
5220 struct collect_gnu_hash_codes
5223 const struct elf_backend_data *bed;
5224 unsigned long int nsyms;
5225 unsigned long int maskbits;
5226 unsigned long int *hashcodes;
5227 unsigned long int *hashval;
5228 unsigned long int *indx;
5229 unsigned long int *counts;
5232 long int min_dynindx;
5233 unsigned long int bucketcount;
5234 unsigned long int symindx;
5235 long int local_indx;
5236 long int shift1, shift2;
5237 unsigned long int mask;
5241 /* This function will be called though elf_link_hash_traverse to store
5242 all hash value of the exported symbols in an array. */
5245 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5247 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5253 /* Ignore indirect symbols. These are added by the versioning code. */
5254 if (h->dynindx == -1)
5257 /* Ignore also local symbols and undefined symbols. */
5258 if (! (*s->bed->elf_hash_symbol) (h))
5261 name = h->root.root.string;
5262 p = strchr (name, ELF_VER_CHR);
5265 alc = (char *) bfd_malloc (p - name + 1);
5271 memcpy (alc, name, p - name);
5272 alc[p - name] = '\0';
5276 /* Compute the hash value. */
5277 ha = bfd_elf_gnu_hash (name);
5279 /* Store the found hash value in the array for compute_bucket_count,
5280 and also for .dynsym reordering purposes. */
5281 s->hashcodes[s->nsyms] = ha;
5282 s->hashval[h->dynindx] = ha;
5284 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5285 s->min_dynindx = h->dynindx;
5293 /* This function will be called though elf_link_hash_traverse to do
5294 final dynaminc symbol renumbering. */
5297 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5299 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5300 unsigned long int bucket;
5301 unsigned long int val;
5303 /* Ignore indirect symbols. */
5304 if (h->dynindx == -1)
5307 /* Ignore also local symbols and undefined symbols. */
5308 if (! (*s->bed->elf_hash_symbol) (h))
5310 if (h->dynindx >= s->min_dynindx)
5311 h->dynindx = s->local_indx++;
5315 bucket = s->hashval[h->dynindx] % s->bucketcount;
5316 val = (s->hashval[h->dynindx] >> s->shift1)
5317 & ((s->maskbits >> s->shift1) - 1);
5318 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5320 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5321 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5322 if (s->counts[bucket] == 1)
5323 /* Last element terminates the chain. */
5325 bfd_put_32 (s->output_bfd, val,
5326 s->contents + (s->indx[bucket] - s->symindx) * 4);
5327 --s->counts[bucket];
5328 h->dynindx = s->indx[bucket]++;
5332 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5335 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5337 return !(h->forced_local
5338 || h->root.type == bfd_link_hash_undefined
5339 || h->root.type == bfd_link_hash_undefweak
5340 || ((h->root.type == bfd_link_hash_defined
5341 || h->root.type == bfd_link_hash_defweak)
5342 && h->root.u.def.section->output_section == NULL));
5345 /* Array used to determine the number of hash table buckets to use
5346 based on the number of symbols there are. If there are fewer than
5347 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5348 fewer than 37 we use 17 buckets, and so forth. We never use more
5349 than 32771 buckets. */
5351 static const size_t elf_buckets[] =
5353 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5357 /* Compute bucket count for hashing table. We do not use a static set
5358 of possible tables sizes anymore. Instead we determine for all
5359 possible reasonable sizes of the table the outcome (i.e., the
5360 number of collisions etc) and choose the best solution. The
5361 weighting functions are not too simple to allow the table to grow
5362 without bounds. Instead one of the weighting factors is the size.
5363 Therefore the result is always a good payoff between few collisions
5364 (= short chain lengths) and table size. */
5366 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5367 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5368 unsigned long int nsyms,
5371 size_t best_size = 0;
5372 unsigned long int i;
5374 /* We have a problem here. The following code to optimize the table
5375 size requires an integer type with more the 32 bits. If
5376 BFD_HOST_U_64_BIT is set we know about such a type. */
5377 #ifdef BFD_HOST_U_64_BIT
5382 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5383 bfd *dynobj = elf_hash_table (info)->dynobj;
5384 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5385 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5386 unsigned long int *counts;
5388 unsigned int no_improvement_count = 0;
5390 /* Possible optimization parameters: if we have NSYMS symbols we say
5391 that the hashing table must at least have NSYMS/4 and at most
5393 minsize = nsyms / 4;
5396 best_size = maxsize = nsyms * 2;
5401 if ((best_size & 31) == 0)
5405 /* Create array where we count the collisions in. We must use bfd_malloc
5406 since the size could be large. */
5408 amt *= sizeof (unsigned long int);
5409 counts = (unsigned long int *) bfd_malloc (amt);
5413 /* Compute the "optimal" size for the hash table. The criteria is a
5414 minimal chain length. The minor criteria is (of course) the size
5416 for (i = minsize; i < maxsize; ++i)
5418 /* Walk through the array of hashcodes and count the collisions. */
5419 BFD_HOST_U_64_BIT max;
5420 unsigned long int j;
5421 unsigned long int fact;
5423 if (gnu_hash && (i & 31) == 0)
5426 memset (counts, '\0', i * sizeof (unsigned long int));
5428 /* Determine how often each hash bucket is used. */
5429 for (j = 0; j < nsyms; ++j)
5430 ++counts[hashcodes[j] % i];
5432 /* For the weight function we need some information about the
5433 pagesize on the target. This is information need not be 100%
5434 accurate. Since this information is not available (so far) we
5435 define it here to a reasonable default value. If it is crucial
5436 to have a better value some day simply define this value. */
5437 # ifndef BFD_TARGET_PAGESIZE
5438 # define BFD_TARGET_PAGESIZE (4096)
5441 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5443 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5446 /* Variant 1: optimize for short chains. We add the squares
5447 of all the chain lengths (which favors many small chain
5448 over a few long chains). */
5449 for (j = 0; j < i; ++j)
5450 max += counts[j] * counts[j];
5452 /* This adds penalties for the overall size of the table. */
5453 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5456 /* Variant 2: Optimize a lot more for small table. Here we
5457 also add squares of the size but we also add penalties for
5458 empty slots (the +1 term). */
5459 for (j = 0; j < i; ++j)
5460 max += (1 + counts[j]) * (1 + counts[j]);
5462 /* The overall size of the table is considered, but not as
5463 strong as in variant 1, where it is squared. */
5464 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5468 /* Compare with current best results. */
5469 if (max < best_chlen)
5473 no_improvement_count = 0;
5475 /* PR 11843: Avoid futile long searches for the best bucket size
5476 when there are a large number of symbols. */
5477 else if (++no_improvement_count == 100)
5484 #endif /* defined (BFD_HOST_U_64_BIT) */
5486 /* This is the fallback solution if no 64bit type is available or if we
5487 are not supposed to spend much time on optimizations. We select the
5488 bucket count using a fixed set of numbers. */
5489 for (i = 0; elf_buckets[i] != 0; i++)
5491 best_size = elf_buckets[i];
5492 if (nsyms < elf_buckets[i + 1])
5495 if (gnu_hash && best_size < 2)
5502 /* Size any SHT_GROUP section for ld -r. */
5505 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5509 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5510 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5511 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5516 /* Set a default stack segment size. The value in INFO wins. If it
5517 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5518 undefined it is initialized. */
5521 bfd_elf_stack_segment_size (bfd *output_bfd,
5522 struct bfd_link_info *info,
5523 const char *legacy_symbol,
5524 bfd_vma default_size)
5526 struct elf_link_hash_entry *h = NULL;
5528 /* Look for legacy symbol. */
5530 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5531 FALSE, FALSE, FALSE);
5532 if (h && (h->root.type == bfd_link_hash_defined
5533 || h->root.type == bfd_link_hash_defweak)
5535 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5537 /* The symbol has no type if specified on the command line. */
5538 h->type = STT_OBJECT;
5539 if (info->stacksize)
5540 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5541 output_bfd, legacy_symbol);
5542 else if (h->root.u.def.section != bfd_abs_section_ptr)
5543 (*_bfd_error_handler) (_("%B: %s not absolute"),
5544 output_bfd, legacy_symbol);
5546 info->stacksize = h->root.u.def.value;
5549 if (!info->stacksize)
5550 /* If the user didn't set a size, or explicitly inhibit the
5551 size, set it now. */
5552 info->stacksize = default_size;
5554 /* Provide the legacy symbol, if it is referenced. */
5555 if (h && (h->root.type == bfd_link_hash_undefined
5556 || h->root.type == bfd_link_hash_undefweak))
5558 struct bfd_link_hash_entry *bh = NULL;
5560 if (!(_bfd_generic_link_add_one_symbol
5561 (info, output_bfd, legacy_symbol,
5562 BSF_GLOBAL, bfd_abs_section_ptr,
5563 info->stacksize >= 0 ? info->stacksize : 0,
5564 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5567 h = (struct elf_link_hash_entry *) bh;
5569 h->type = STT_OBJECT;
5575 /* Set up the sizes and contents of the ELF dynamic sections. This is
5576 called by the ELF linker emulation before_allocation routine. We
5577 must set the sizes of the sections before the linker sets the
5578 addresses of the various sections. */
5581 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5584 const char *filter_shlib,
5586 const char *depaudit,
5587 const char * const *auxiliary_filters,
5588 struct bfd_link_info *info,
5589 asection **sinterpptr)
5591 bfd_size_type soname_indx;
5593 const struct elf_backend_data *bed;
5594 struct elf_info_failed asvinfo;
5598 soname_indx = (bfd_size_type) -1;
5600 if (!is_elf_hash_table (info->hash))
5603 bed = get_elf_backend_data (output_bfd);
5605 /* Any syms created from now on start with -1 in
5606 got.refcount/offset and plt.refcount/offset. */
5607 elf_hash_table (info)->init_got_refcount
5608 = elf_hash_table (info)->init_got_offset;
5609 elf_hash_table (info)->init_plt_refcount
5610 = elf_hash_table (info)->init_plt_offset;
5612 if (info->relocatable
5613 && !_bfd_elf_size_group_sections (info))
5616 /* The backend may have to create some sections regardless of whether
5617 we're dynamic or not. */
5618 if (bed->elf_backend_always_size_sections
5619 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5622 /* Determine any GNU_STACK segment requirements, after the backend
5623 has had a chance to set a default segment size. */
5624 if (info->execstack)
5625 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5626 else if (info->noexecstack)
5627 elf_stack_flags (output_bfd) = PF_R | PF_W;
5631 asection *notesec = NULL;
5634 for (inputobj = info->input_bfds;
5636 inputobj = inputobj->link.next)
5641 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5643 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5646 if (s->flags & SEC_CODE)
5650 else if (bed->default_execstack)
5653 if (notesec || info->stacksize > 0)
5654 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5655 if (notesec && exec && info->relocatable
5656 && notesec->output_section != bfd_abs_section_ptr)
5657 notesec->output_section->flags |= SEC_CODE;
5660 dynobj = elf_hash_table (info)->dynobj;
5662 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5664 struct elf_info_failed eif;
5665 struct elf_link_hash_entry *h;
5667 struct bfd_elf_version_tree *t;
5668 struct bfd_elf_version_expr *d;
5670 bfd_boolean all_defined;
5672 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5673 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5677 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5679 if (soname_indx == (bfd_size_type) -1
5680 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5686 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5688 info->flags |= DF_SYMBOLIC;
5696 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5698 if (indx == (bfd_size_type) -1)
5701 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5702 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5706 if (filter_shlib != NULL)
5710 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5711 filter_shlib, TRUE);
5712 if (indx == (bfd_size_type) -1
5713 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5717 if (auxiliary_filters != NULL)
5719 const char * const *p;
5721 for (p = auxiliary_filters; *p != NULL; p++)
5725 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5727 if (indx == (bfd_size_type) -1
5728 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5737 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5739 if (indx == (bfd_size_type) -1
5740 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5744 if (depaudit != NULL)
5748 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5750 if (indx == (bfd_size_type) -1
5751 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5758 /* If we are supposed to export all symbols into the dynamic symbol
5759 table (this is not the normal case), then do so. */
5760 if (info->export_dynamic
5761 || (info->executable && info->dynamic))
5763 elf_link_hash_traverse (elf_hash_table (info),
5764 _bfd_elf_export_symbol,
5770 /* Make all global versions with definition. */
5771 for (t = info->version_info; t != NULL; t = t->next)
5772 for (d = t->globals.list; d != NULL; d = d->next)
5773 if (!d->symver && d->literal)
5775 const char *verstr, *name;
5776 size_t namelen, verlen, newlen;
5777 char *newname, *p, leading_char;
5778 struct elf_link_hash_entry *newh;
5780 leading_char = bfd_get_symbol_leading_char (output_bfd);
5782 namelen = strlen (name) + (leading_char != '\0');
5784 verlen = strlen (verstr);
5785 newlen = namelen + verlen + 3;
5787 newname = (char *) bfd_malloc (newlen);
5788 if (newname == NULL)
5790 newname[0] = leading_char;
5791 memcpy (newname + (leading_char != '\0'), name, namelen);
5793 /* Check the hidden versioned definition. */
5794 p = newname + namelen;
5796 memcpy (p, verstr, verlen + 1);
5797 newh = elf_link_hash_lookup (elf_hash_table (info),
5798 newname, FALSE, FALSE,
5801 || (newh->root.type != bfd_link_hash_defined
5802 && newh->root.type != bfd_link_hash_defweak))
5804 /* Check the default versioned definition. */
5806 memcpy (p, verstr, verlen + 1);
5807 newh = elf_link_hash_lookup (elf_hash_table (info),
5808 newname, FALSE, FALSE,
5813 /* Mark this version if there is a definition and it is
5814 not defined in a shared object. */
5816 && !newh->def_dynamic
5817 && (newh->root.type == bfd_link_hash_defined
5818 || newh->root.type == bfd_link_hash_defweak))
5822 /* Attach all the symbols to their version information. */
5823 asvinfo.info = info;
5824 asvinfo.failed = FALSE;
5826 elf_link_hash_traverse (elf_hash_table (info),
5827 _bfd_elf_link_assign_sym_version,
5832 if (!info->allow_undefined_version)
5834 /* Check if all global versions have a definition. */
5836 for (t = info->version_info; t != NULL; t = t->next)
5837 for (d = t->globals.list; d != NULL; d = d->next)
5838 if (d->literal && !d->symver && !d->script)
5840 (*_bfd_error_handler)
5841 (_("%s: undefined version: %s"),
5842 d->pattern, t->name);
5843 all_defined = FALSE;
5848 bfd_set_error (bfd_error_bad_value);
5853 /* Find all symbols which were defined in a dynamic object and make
5854 the backend pick a reasonable value for them. */
5855 elf_link_hash_traverse (elf_hash_table (info),
5856 _bfd_elf_adjust_dynamic_symbol,
5861 /* Add some entries to the .dynamic section. We fill in some of the
5862 values later, in bfd_elf_final_link, but we must add the entries
5863 now so that we know the final size of the .dynamic section. */
5865 /* If there are initialization and/or finalization functions to
5866 call then add the corresponding DT_INIT/DT_FINI entries. */
5867 h = (info->init_function
5868 ? elf_link_hash_lookup (elf_hash_table (info),
5869 info->init_function, FALSE,
5876 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5879 h = (info->fini_function
5880 ? elf_link_hash_lookup (elf_hash_table (info),
5881 info->fini_function, FALSE,
5888 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5892 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5893 if (s != NULL && s->linker_has_input)
5895 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5896 if (! info->executable)
5901 for (sub = info->input_bfds; sub != NULL;
5902 sub = sub->link.next)
5903 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5904 for (o = sub->sections; o != NULL; o = o->next)
5905 if (elf_section_data (o)->this_hdr.sh_type
5906 == SHT_PREINIT_ARRAY)
5908 (*_bfd_error_handler)
5909 (_("%B: .preinit_array section is not allowed in DSO"),
5914 bfd_set_error (bfd_error_nonrepresentable_section);
5918 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5919 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5922 s = bfd_get_section_by_name (output_bfd, ".init_array");
5923 if (s != NULL && s->linker_has_input)
5925 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5926 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5929 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5930 if (s != NULL && s->linker_has_input)
5932 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5933 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5937 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5938 /* If .dynstr is excluded from the link, we don't want any of
5939 these tags. Strictly, we should be checking each section
5940 individually; This quick check covers for the case where
5941 someone does a /DISCARD/ : { *(*) }. */
5942 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5944 bfd_size_type strsize;
5946 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5947 if ((info->emit_hash
5948 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5949 || (info->emit_gnu_hash
5950 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5951 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5952 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5953 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5954 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5955 bed->s->sizeof_sym))
5960 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5963 /* The backend must work out the sizes of all the other dynamic
5966 && bed->elf_backend_size_dynamic_sections != NULL
5967 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5970 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5972 unsigned long section_sym_count;
5973 struct bfd_elf_version_tree *verdefs;
5976 /* Set up the version definition section. */
5977 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5978 BFD_ASSERT (s != NULL);
5980 /* We may have created additional version definitions if we are
5981 just linking a regular application. */
5982 verdefs = info->version_info;
5984 /* Skip anonymous version tag. */
5985 if (verdefs != NULL && verdefs->vernum == 0)
5986 verdefs = verdefs->next;
5988 if (verdefs == NULL && !info->create_default_symver)
5989 s->flags |= SEC_EXCLUDE;
5994 struct bfd_elf_version_tree *t;
5996 Elf_Internal_Verdef def;
5997 Elf_Internal_Verdaux defaux;
5998 struct bfd_link_hash_entry *bh;
5999 struct elf_link_hash_entry *h;
6005 /* Make space for the base version. */
6006 size += sizeof (Elf_External_Verdef);
6007 size += sizeof (Elf_External_Verdaux);
6010 /* Make space for the default version. */
6011 if (info->create_default_symver)
6013 size += sizeof (Elf_External_Verdef);
6017 for (t = verdefs; t != NULL; t = t->next)
6019 struct bfd_elf_version_deps *n;
6021 /* Don't emit base version twice. */
6025 size += sizeof (Elf_External_Verdef);
6026 size += sizeof (Elf_External_Verdaux);
6029 for (n = t->deps; n != NULL; n = n->next)
6030 size += sizeof (Elf_External_Verdaux);
6034 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6035 if (s->contents == NULL && s->size != 0)
6038 /* Fill in the version definition section. */
6042 def.vd_version = VER_DEF_CURRENT;
6043 def.vd_flags = VER_FLG_BASE;
6046 if (info->create_default_symver)
6048 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6049 def.vd_next = sizeof (Elf_External_Verdef);
6053 def.vd_aux = sizeof (Elf_External_Verdef);
6054 def.vd_next = (sizeof (Elf_External_Verdef)
6055 + sizeof (Elf_External_Verdaux));
6058 if (soname_indx != (bfd_size_type) -1)
6060 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6062 def.vd_hash = bfd_elf_hash (soname);
6063 defaux.vda_name = soname_indx;
6070 name = lbasename (output_bfd->filename);
6071 def.vd_hash = bfd_elf_hash (name);
6072 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6074 if (indx == (bfd_size_type) -1)
6076 defaux.vda_name = indx;
6078 defaux.vda_next = 0;
6080 _bfd_elf_swap_verdef_out (output_bfd, &def,
6081 (Elf_External_Verdef *) p);
6082 p += sizeof (Elf_External_Verdef);
6083 if (info->create_default_symver)
6085 /* Add a symbol representing this version. */
6087 if (! (_bfd_generic_link_add_one_symbol
6088 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6090 get_elf_backend_data (dynobj)->collect, &bh)))
6092 h = (struct elf_link_hash_entry *) bh;
6095 h->type = STT_OBJECT;
6096 h->verinfo.vertree = NULL;
6098 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6101 /* Create a duplicate of the base version with the same
6102 aux block, but different flags. */
6105 def.vd_aux = sizeof (Elf_External_Verdef);
6107 def.vd_next = (sizeof (Elf_External_Verdef)
6108 + sizeof (Elf_External_Verdaux));
6111 _bfd_elf_swap_verdef_out (output_bfd, &def,
6112 (Elf_External_Verdef *) p);
6113 p += sizeof (Elf_External_Verdef);
6115 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6116 (Elf_External_Verdaux *) p);
6117 p += sizeof (Elf_External_Verdaux);
6119 for (t = verdefs; t != NULL; t = t->next)
6122 struct bfd_elf_version_deps *n;
6124 /* Don't emit the base version twice. */
6129 for (n = t->deps; n != NULL; n = n->next)
6132 /* Add a symbol representing this version. */
6134 if (! (_bfd_generic_link_add_one_symbol
6135 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6137 get_elf_backend_data (dynobj)->collect, &bh)))
6139 h = (struct elf_link_hash_entry *) bh;
6142 h->type = STT_OBJECT;
6143 h->verinfo.vertree = t;
6145 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6148 def.vd_version = VER_DEF_CURRENT;
6150 if (t->globals.list == NULL
6151 && t->locals.list == NULL
6153 def.vd_flags |= VER_FLG_WEAK;
6154 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6155 def.vd_cnt = cdeps + 1;
6156 def.vd_hash = bfd_elf_hash (t->name);
6157 def.vd_aux = sizeof (Elf_External_Verdef);
6160 /* If a basever node is next, it *must* be the last node in
6161 the chain, otherwise Verdef construction breaks. */
6162 if (t->next != NULL && t->next->vernum == 0)
6163 BFD_ASSERT (t->next->next == NULL);
6165 if (t->next != NULL && t->next->vernum != 0)
6166 def.vd_next = (sizeof (Elf_External_Verdef)
6167 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6169 _bfd_elf_swap_verdef_out (output_bfd, &def,
6170 (Elf_External_Verdef *) p);
6171 p += sizeof (Elf_External_Verdef);
6173 defaux.vda_name = h->dynstr_index;
6174 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6176 defaux.vda_next = 0;
6177 if (t->deps != NULL)
6178 defaux.vda_next = sizeof (Elf_External_Verdaux);
6179 t->name_indx = defaux.vda_name;
6181 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6182 (Elf_External_Verdaux *) p);
6183 p += sizeof (Elf_External_Verdaux);
6185 for (n = t->deps; n != NULL; n = n->next)
6187 if (n->version_needed == NULL)
6189 /* This can happen if there was an error in the
6191 defaux.vda_name = 0;
6195 defaux.vda_name = n->version_needed->name_indx;
6196 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6199 if (n->next == NULL)
6200 defaux.vda_next = 0;
6202 defaux.vda_next = sizeof (Elf_External_Verdaux);
6204 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6205 (Elf_External_Verdaux *) p);
6206 p += sizeof (Elf_External_Verdaux);
6210 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6211 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6214 elf_tdata (output_bfd)->cverdefs = cdefs;
6217 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6219 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6222 else if (info->flags & DF_BIND_NOW)
6224 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6230 if (info->executable)
6231 info->flags_1 &= ~ (DF_1_INITFIRST
6234 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6238 /* Work out the size of the version reference section. */
6240 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6241 BFD_ASSERT (s != NULL);
6243 struct elf_find_verdep_info sinfo;
6246 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6247 if (sinfo.vers == 0)
6249 sinfo.failed = FALSE;
6251 elf_link_hash_traverse (elf_hash_table (info),
6252 _bfd_elf_link_find_version_dependencies,
6257 if (elf_tdata (output_bfd)->verref == NULL)
6258 s->flags |= SEC_EXCLUDE;
6261 Elf_Internal_Verneed *t;
6266 /* Build the version dependency section. */
6269 for (t = elf_tdata (output_bfd)->verref;
6273 Elf_Internal_Vernaux *a;
6275 size += sizeof (Elf_External_Verneed);
6277 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6278 size += sizeof (Elf_External_Vernaux);
6282 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6283 if (s->contents == NULL)
6287 for (t = elf_tdata (output_bfd)->verref;
6292 Elf_Internal_Vernaux *a;
6296 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6299 t->vn_version = VER_NEED_CURRENT;
6301 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6302 elf_dt_name (t->vn_bfd) != NULL
6303 ? elf_dt_name (t->vn_bfd)
6304 : lbasename (t->vn_bfd->filename),
6306 if (indx == (bfd_size_type) -1)
6309 t->vn_aux = sizeof (Elf_External_Verneed);
6310 if (t->vn_nextref == NULL)
6313 t->vn_next = (sizeof (Elf_External_Verneed)
6314 + caux * sizeof (Elf_External_Vernaux));
6316 _bfd_elf_swap_verneed_out (output_bfd, t,
6317 (Elf_External_Verneed *) p);
6318 p += sizeof (Elf_External_Verneed);
6320 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6322 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6323 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6324 a->vna_nodename, FALSE);
6325 if (indx == (bfd_size_type) -1)
6328 if (a->vna_nextptr == NULL)
6331 a->vna_next = sizeof (Elf_External_Vernaux);
6333 _bfd_elf_swap_vernaux_out (output_bfd, a,
6334 (Elf_External_Vernaux *) p);
6335 p += sizeof (Elf_External_Vernaux);
6339 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6340 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6343 elf_tdata (output_bfd)->cverrefs = crefs;
6347 if ((elf_tdata (output_bfd)->cverrefs == 0
6348 && elf_tdata (output_bfd)->cverdefs == 0)
6349 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6350 §ion_sym_count) == 0)
6352 s = bfd_get_linker_section (dynobj, ".gnu.version");
6353 s->flags |= SEC_EXCLUDE;
6359 /* Find the first non-excluded output section. We'll use its
6360 section symbol for some emitted relocs. */
6362 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6366 for (s = output_bfd->sections; s != NULL; s = s->next)
6367 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6368 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6370 elf_hash_table (info)->text_index_section = s;
6375 /* Find two non-excluded output sections, one for code, one for data.
6376 We'll use their section symbols for some emitted relocs. */
6378 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6382 /* Data first, since setting text_index_section changes
6383 _bfd_elf_link_omit_section_dynsym. */
6384 for (s = output_bfd->sections; s != NULL; s = s->next)
6385 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6386 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6388 elf_hash_table (info)->data_index_section = s;
6392 for (s = output_bfd->sections; s != NULL; s = s->next)
6393 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6394 == (SEC_ALLOC | SEC_READONLY))
6395 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6397 elf_hash_table (info)->text_index_section = s;
6401 if (elf_hash_table (info)->text_index_section == NULL)
6402 elf_hash_table (info)->text_index_section
6403 = elf_hash_table (info)->data_index_section;
6407 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6409 const struct elf_backend_data *bed;
6411 if (!is_elf_hash_table (info->hash))
6414 bed = get_elf_backend_data (output_bfd);
6415 (*bed->elf_backend_init_index_section) (output_bfd, info);
6417 if (elf_hash_table (info)->dynamic_sections_created)
6421 bfd_size_type dynsymcount;
6422 unsigned long section_sym_count;
6423 unsigned int dtagcount;
6425 dynobj = elf_hash_table (info)->dynobj;
6427 /* Assign dynsym indicies. In a shared library we generate a
6428 section symbol for each output section, which come first.
6429 Next come all of the back-end allocated local dynamic syms,
6430 followed by the rest of the global symbols. */
6432 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6433 §ion_sym_count);
6435 /* Work out the size of the symbol version section. */
6436 s = bfd_get_linker_section (dynobj, ".gnu.version");
6437 BFD_ASSERT (s != NULL);
6438 if (dynsymcount != 0
6439 && (s->flags & SEC_EXCLUDE) == 0)
6441 s->size = dynsymcount * sizeof (Elf_External_Versym);
6442 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6443 if (s->contents == NULL)
6446 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6450 /* Set the size of the .dynsym and .hash sections. We counted
6451 the number of dynamic symbols in elf_link_add_object_symbols.
6452 We will build the contents of .dynsym and .hash when we build
6453 the final symbol table, because until then we do not know the
6454 correct value to give the symbols. We built the .dynstr
6455 section as we went along in elf_link_add_object_symbols. */
6456 s = bfd_get_linker_section (dynobj, ".dynsym");
6457 BFD_ASSERT (s != NULL);
6458 s->size = dynsymcount * bed->s->sizeof_sym;
6460 if (dynsymcount != 0)
6462 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6463 if (s->contents == NULL)
6466 /* The first entry in .dynsym is a dummy symbol.
6467 Clear all the section syms, in case we don't output them all. */
6468 ++section_sym_count;
6469 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6472 elf_hash_table (info)->bucketcount = 0;
6474 /* Compute the size of the hashing table. As a side effect this
6475 computes the hash values for all the names we export. */
6476 if (info->emit_hash)
6478 unsigned long int *hashcodes;
6479 struct hash_codes_info hashinf;
6481 unsigned long int nsyms;
6483 size_t hash_entry_size;
6485 /* Compute the hash values for all exported symbols. At the same
6486 time store the values in an array so that we could use them for
6488 amt = dynsymcount * sizeof (unsigned long int);
6489 hashcodes = (unsigned long int *) bfd_malloc (amt);
6490 if (hashcodes == NULL)
6492 hashinf.hashcodes = hashcodes;
6493 hashinf.error = FALSE;
6495 /* Put all hash values in HASHCODES. */
6496 elf_link_hash_traverse (elf_hash_table (info),
6497 elf_collect_hash_codes, &hashinf);
6504 nsyms = hashinf.hashcodes - hashcodes;
6506 = compute_bucket_count (info, hashcodes, nsyms, 0);
6509 if (bucketcount == 0)
6512 elf_hash_table (info)->bucketcount = bucketcount;
6514 s = bfd_get_linker_section (dynobj, ".hash");
6515 BFD_ASSERT (s != NULL);
6516 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6517 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6518 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6519 if (s->contents == NULL)
6522 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6523 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6524 s->contents + hash_entry_size);
6527 if (info->emit_gnu_hash)
6530 unsigned char *contents;
6531 struct collect_gnu_hash_codes cinfo;
6535 memset (&cinfo, 0, sizeof (cinfo));
6537 /* Compute the hash values for all exported symbols. At the same
6538 time store the values in an array so that we could use them for
6540 amt = dynsymcount * 2 * sizeof (unsigned long int);
6541 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6542 if (cinfo.hashcodes == NULL)
6545 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6546 cinfo.min_dynindx = -1;
6547 cinfo.output_bfd = output_bfd;
6550 /* Put all hash values in HASHCODES. */
6551 elf_link_hash_traverse (elf_hash_table (info),
6552 elf_collect_gnu_hash_codes, &cinfo);
6555 free (cinfo.hashcodes);
6560 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6562 if (bucketcount == 0)
6564 free (cinfo.hashcodes);
6568 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6569 BFD_ASSERT (s != NULL);
6571 if (cinfo.nsyms == 0)
6573 /* Empty .gnu.hash section is special. */
6574 BFD_ASSERT (cinfo.min_dynindx == -1);
6575 free (cinfo.hashcodes);
6576 s->size = 5 * 4 + bed->s->arch_size / 8;
6577 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6578 if (contents == NULL)
6580 s->contents = contents;
6581 /* 1 empty bucket. */
6582 bfd_put_32 (output_bfd, 1, contents);
6583 /* SYMIDX above the special symbol 0. */
6584 bfd_put_32 (output_bfd, 1, contents + 4);
6585 /* Just one word for bitmask. */
6586 bfd_put_32 (output_bfd, 1, contents + 8);
6587 /* Only hash fn bloom filter. */
6588 bfd_put_32 (output_bfd, 0, contents + 12);
6589 /* No hashes are valid - empty bitmask. */
6590 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6591 /* No hashes in the only bucket. */
6592 bfd_put_32 (output_bfd, 0,
6593 contents + 16 + bed->s->arch_size / 8);
6597 unsigned long int maskwords, maskbitslog2, x;
6598 BFD_ASSERT (cinfo.min_dynindx != -1);
6602 while ((x >>= 1) != 0)
6604 if (maskbitslog2 < 3)
6606 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6607 maskbitslog2 = maskbitslog2 + 3;
6609 maskbitslog2 = maskbitslog2 + 2;
6610 if (bed->s->arch_size == 64)
6612 if (maskbitslog2 == 5)
6618 cinfo.mask = (1 << cinfo.shift1) - 1;
6619 cinfo.shift2 = maskbitslog2;
6620 cinfo.maskbits = 1 << maskbitslog2;
6621 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6622 amt = bucketcount * sizeof (unsigned long int) * 2;
6623 amt += maskwords * sizeof (bfd_vma);
6624 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6625 if (cinfo.bitmask == NULL)
6627 free (cinfo.hashcodes);
6631 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6632 cinfo.indx = cinfo.counts + bucketcount;
6633 cinfo.symindx = dynsymcount - cinfo.nsyms;
6634 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6636 /* Determine how often each hash bucket is used. */
6637 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6638 for (i = 0; i < cinfo.nsyms; ++i)
6639 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6641 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6642 if (cinfo.counts[i] != 0)
6644 cinfo.indx[i] = cnt;
6645 cnt += cinfo.counts[i];
6647 BFD_ASSERT (cnt == dynsymcount);
6648 cinfo.bucketcount = bucketcount;
6649 cinfo.local_indx = cinfo.min_dynindx;
6651 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6652 s->size += cinfo.maskbits / 8;
6653 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6654 if (contents == NULL)
6656 free (cinfo.bitmask);
6657 free (cinfo.hashcodes);
6661 s->contents = contents;
6662 bfd_put_32 (output_bfd, bucketcount, contents);
6663 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6664 bfd_put_32 (output_bfd, maskwords, contents + 8);
6665 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6666 contents += 16 + cinfo.maskbits / 8;
6668 for (i = 0; i < bucketcount; ++i)
6670 if (cinfo.counts[i] == 0)
6671 bfd_put_32 (output_bfd, 0, contents);
6673 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6677 cinfo.contents = contents;
6679 /* Renumber dynamic symbols, populate .gnu.hash section. */
6680 elf_link_hash_traverse (elf_hash_table (info),
6681 elf_renumber_gnu_hash_syms, &cinfo);
6683 contents = s->contents + 16;
6684 for (i = 0; i < maskwords; ++i)
6686 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6688 contents += bed->s->arch_size / 8;
6691 free (cinfo.bitmask);
6692 free (cinfo.hashcodes);
6696 s = bfd_get_linker_section (dynobj, ".dynstr");
6697 BFD_ASSERT (s != NULL);
6699 elf_finalize_dynstr (output_bfd, info);
6701 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6703 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6704 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6711 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6714 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6717 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6718 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6721 /* Finish SHF_MERGE section merging. */
6724 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6729 if (!is_elf_hash_table (info->hash))
6732 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6733 if ((ibfd->flags & DYNAMIC) == 0)
6734 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6735 if ((sec->flags & SEC_MERGE) != 0
6736 && !bfd_is_abs_section (sec->output_section))
6738 struct bfd_elf_section_data *secdata;
6740 secdata = elf_section_data (sec);
6741 if (! _bfd_add_merge_section (abfd,
6742 &elf_hash_table (info)->merge_info,
6743 sec, &secdata->sec_info))
6745 else if (secdata->sec_info)
6746 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6749 if (elf_hash_table (info)->merge_info != NULL)
6750 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6751 merge_sections_remove_hook);
6755 /* Create an entry in an ELF linker hash table. */
6757 struct bfd_hash_entry *
6758 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6759 struct bfd_hash_table *table,
6762 /* Allocate the structure if it has not already been allocated by a
6766 entry = (struct bfd_hash_entry *)
6767 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6772 /* Call the allocation method of the superclass. */
6773 entry = _bfd_link_hash_newfunc (entry, table, string);
6776 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6777 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6779 /* Set local fields. */
6782 ret->got = htab->init_got_refcount;
6783 ret->plt = htab->init_plt_refcount;
6784 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6785 - offsetof (struct elf_link_hash_entry, size)));
6786 /* Assume that we have been called by a non-ELF symbol reader.
6787 This flag is then reset by the code which reads an ELF input
6788 file. This ensures that a symbol created by a non-ELF symbol
6789 reader will have the flag set correctly. */
6796 /* Copy data from an indirect symbol to its direct symbol, hiding the
6797 old indirect symbol. Also used for copying flags to a weakdef. */
6800 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6801 struct elf_link_hash_entry *dir,
6802 struct elf_link_hash_entry *ind)
6804 struct elf_link_hash_table *htab;
6806 /* Copy down any references that we may have already seen to the
6807 symbol which just became indirect. */
6809 dir->ref_dynamic |= ind->ref_dynamic;
6810 dir->ref_regular |= ind->ref_regular;
6811 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6812 dir->non_got_ref |= ind->non_got_ref;
6813 dir->needs_plt |= ind->needs_plt;
6814 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6816 if (ind->root.type != bfd_link_hash_indirect)
6819 /* Copy over the global and procedure linkage table refcount entries.
6820 These may have been already set up by a check_relocs routine. */
6821 htab = elf_hash_table (info);
6822 if (ind->got.refcount > htab->init_got_refcount.refcount)
6824 if (dir->got.refcount < 0)
6825 dir->got.refcount = 0;
6826 dir->got.refcount += ind->got.refcount;
6827 ind->got.refcount = htab->init_got_refcount.refcount;
6830 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6832 if (dir->plt.refcount < 0)
6833 dir->plt.refcount = 0;
6834 dir->plt.refcount += ind->plt.refcount;
6835 ind->plt.refcount = htab->init_plt_refcount.refcount;
6838 if (ind->dynindx != -1)
6840 if (dir->dynindx != -1)
6841 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6842 dir->dynindx = ind->dynindx;
6843 dir->dynstr_index = ind->dynstr_index;
6845 ind->dynstr_index = 0;
6850 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6851 struct elf_link_hash_entry *h,
6852 bfd_boolean force_local)
6854 /* STT_GNU_IFUNC symbol must go through PLT. */
6855 if (h->type != STT_GNU_IFUNC)
6857 h->plt = elf_hash_table (info)->init_plt_offset;
6862 h->forced_local = 1;
6863 if (h->dynindx != -1)
6866 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6872 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
6876 _bfd_elf_link_hash_table_init
6877 (struct elf_link_hash_table *table,
6879 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6880 struct bfd_hash_table *,
6882 unsigned int entsize,
6883 enum elf_target_id target_id)
6886 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6888 table->init_got_refcount.refcount = can_refcount - 1;
6889 table->init_plt_refcount.refcount = can_refcount - 1;
6890 table->init_got_offset.offset = -(bfd_vma) 1;
6891 table->init_plt_offset.offset = -(bfd_vma) 1;
6892 /* The first dynamic symbol is a dummy. */
6893 table->dynsymcount = 1;
6895 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6897 table->root.type = bfd_link_elf_hash_table;
6898 table->hash_table_id = target_id;
6903 /* Create an ELF linker hash table. */
6905 struct bfd_link_hash_table *
6906 _bfd_elf_link_hash_table_create (bfd *abfd)
6908 struct elf_link_hash_table *ret;
6909 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6911 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
6915 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
6916 sizeof (struct elf_link_hash_entry),
6922 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
6927 /* Destroy an ELF linker hash table. */
6930 _bfd_elf_link_hash_table_free (bfd *obfd)
6932 struct elf_link_hash_table *htab;
6934 htab = (struct elf_link_hash_table *) obfd->link.hash;
6935 if (htab->dynstr != NULL)
6936 _bfd_elf_strtab_free (htab->dynstr);
6937 _bfd_merge_sections_free (htab->merge_info);
6938 _bfd_generic_link_hash_table_free (obfd);
6941 /* This is a hook for the ELF emulation code in the generic linker to
6942 tell the backend linker what file name to use for the DT_NEEDED
6943 entry for a dynamic object. */
6946 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6948 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6949 && bfd_get_format (abfd) == bfd_object)
6950 elf_dt_name (abfd) = name;
6954 bfd_elf_get_dyn_lib_class (bfd *abfd)
6957 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6958 && bfd_get_format (abfd) == bfd_object)
6959 lib_class = elf_dyn_lib_class (abfd);
6966 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6968 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6969 && bfd_get_format (abfd) == bfd_object)
6970 elf_dyn_lib_class (abfd) = lib_class;
6973 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6974 the linker ELF emulation code. */
6976 struct bfd_link_needed_list *
6977 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6978 struct bfd_link_info *info)
6980 if (! is_elf_hash_table (info->hash))
6982 return elf_hash_table (info)->needed;
6985 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6986 hook for the linker ELF emulation code. */
6988 struct bfd_link_needed_list *
6989 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6990 struct bfd_link_info *info)
6992 if (! is_elf_hash_table (info->hash))
6994 return elf_hash_table (info)->runpath;
6997 /* Get the name actually used for a dynamic object for a link. This
6998 is the SONAME entry if there is one. Otherwise, it is the string
6999 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7002 bfd_elf_get_dt_soname (bfd *abfd)
7004 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7005 && bfd_get_format (abfd) == bfd_object)
7006 return elf_dt_name (abfd);
7010 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7011 the ELF linker emulation code. */
7014 bfd_elf_get_bfd_needed_list (bfd *abfd,
7015 struct bfd_link_needed_list **pneeded)
7018 bfd_byte *dynbuf = NULL;
7019 unsigned int elfsec;
7020 unsigned long shlink;
7021 bfd_byte *extdyn, *extdynend;
7023 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7027 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7028 || bfd_get_format (abfd) != bfd_object)
7031 s = bfd_get_section_by_name (abfd, ".dynamic");
7032 if (s == NULL || s->size == 0)
7035 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7038 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7039 if (elfsec == SHN_BAD)
7042 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7044 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7045 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7048 extdynend = extdyn + s->size;
7049 for (; extdyn < extdynend; extdyn += extdynsize)
7051 Elf_Internal_Dyn dyn;
7053 (*swap_dyn_in) (abfd, extdyn, &dyn);
7055 if (dyn.d_tag == DT_NULL)
7058 if (dyn.d_tag == DT_NEEDED)
7061 struct bfd_link_needed_list *l;
7062 unsigned int tagv = dyn.d_un.d_val;
7065 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7070 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7091 struct elf_symbuf_symbol
7093 unsigned long st_name; /* Symbol name, index in string tbl */
7094 unsigned char st_info; /* Type and binding attributes */
7095 unsigned char st_other; /* Visibilty, and target specific */
7098 struct elf_symbuf_head
7100 struct elf_symbuf_symbol *ssym;
7101 bfd_size_type count;
7102 unsigned int st_shndx;
7109 Elf_Internal_Sym *isym;
7110 struct elf_symbuf_symbol *ssym;
7115 /* Sort references to symbols by ascending section number. */
7118 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7120 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7121 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7123 return s1->st_shndx - s2->st_shndx;
7127 elf_sym_name_compare (const void *arg1, const void *arg2)
7129 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7130 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7131 return strcmp (s1->name, s2->name);
7134 static struct elf_symbuf_head *
7135 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7137 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7138 struct elf_symbuf_symbol *ssym;
7139 struct elf_symbuf_head *ssymbuf, *ssymhead;
7140 bfd_size_type i, shndx_count, total_size;
7142 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7146 for (ind = indbuf, i = 0; i < symcount; i++)
7147 if (isymbuf[i].st_shndx != SHN_UNDEF)
7148 *ind++ = &isymbuf[i];
7151 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7152 elf_sort_elf_symbol);
7155 if (indbufend > indbuf)
7156 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7157 if (ind[0]->st_shndx != ind[1]->st_shndx)
7160 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7161 + (indbufend - indbuf) * sizeof (*ssym));
7162 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7163 if (ssymbuf == NULL)
7169 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7170 ssymbuf->ssym = NULL;
7171 ssymbuf->count = shndx_count;
7172 ssymbuf->st_shndx = 0;
7173 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7175 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7178 ssymhead->ssym = ssym;
7179 ssymhead->count = 0;
7180 ssymhead->st_shndx = (*ind)->st_shndx;
7182 ssym->st_name = (*ind)->st_name;
7183 ssym->st_info = (*ind)->st_info;
7184 ssym->st_other = (*ind)->st_other;
7187 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7188 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7195 /* Check if 2 sections define the same set of local and global
7199 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7200 struct bfd_link_info *info)
7203 const struct elf_backend_data *bed1, *bed2;
7204 Elf_Internal_Shdr *hdr1, *hdr2;
7205 bfd_size_type symcount1, symcount2;
7206 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7207 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7208 Elf_Internal_Sym *isym, *isymend;
7209 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7210 bfd_size_type count1, count2, i;
7211 unsigned int shndx1, shndx2;
7217 /* Both sections have to be in ELF. */
7218 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7219 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7222 if (elf_section_type (sec1) != elf_section_type (sec2))
7225 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7226 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7227 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7230 bed1 = get_elf_backend_data (bfd1);
7231 bed2 = get_elf_backend_data (bfd2);
7232 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7233 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7234 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7235 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7237 if (symcount1 == 0 || symcount2 == 0)
7243 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7244 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7246 if (ssymbuf1 == NULL)
7248 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7250 if (isymbuf1 == NULL)
7253 if (!info->reduce_memory_overheads)
7254 elf_tdata (bfd1)->symbuf = ssymbuf1
7255 = elf_create_symbuf (symcount1, isymbuf1);
7258 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7260 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7262 if (isymbuf2 == NULL)
7265 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7266 elf_tdata (bfd2)->symbuf = ssymbuf2
7267 = elf_create_symbuf (symcount2, isymbuf2);
7270 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7272 /* Optimized faster version. */
7273 bfd_size_type lo, hi, mid;
7274 struct elf_symbol *symp;
7275 struct elf_symbuf_symbol *ssym, *ssymend;
7278 hi = ssymbuf1->count;
7283 mid = (lo + hi) / 2;
7284 if (shndx1 < ssymbuf1[mid].st_shndx)
7286 else if (shndx1 > ssymbuf1[mid].st_shndx)
7290 count1 = ssymbuf1[mid].count;
7297 hi = ssymbuf2->count;
7302 mid = (lo + hi) / 2;
7303 if (shndx2 < ssymbuf2[mid].st_shndx)
7305 else if (shndx2 > ssymbuf2[mid].st_shndx)
7309 count2 = ssymbuf2[mid].count;
7315 if (count1 == 0 || count2 == 0 || count1 != count2)
7319 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7321 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7322 if (symtable1 == NULL || symtable2 == NULL)
7326 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7327 ssym < ssymend; ssym++, symp++)
7329 symp->u.ssym = ssym;
7330 symp->name = bfd_elf_string_from_elf_section (bfd1,
7336 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7337 ssym < ssymend; ssym++, symp++)
7339 symp->u.ssym = ssym;
7340 symp->name = bfd_elf_string_from_elf_section (bfd2,
7345 /* Sort symbol by name. */
7346 qsort (symtable1, count1, sizeof (struct elf_symbol),
7347 elf_sym_name_compare);
7348 qsort (symtable2, count1, sizeof (struct elf_symbol),
7349 elf_sym_name_compare);
7351 for (i = 0; i < count1; i++)
7352 /* Two symbols must have the same binding, type and name. */
7353 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7354 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7355 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7362 symtable1 = (struct elf_symbol *)
7363 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7364 symtable2 = (struct elf_symbol *)
7365 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7366 if (symtable1 == NULL || symtable2 == NULL)
7369 /* Count definitions in the section. */
7371 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7372 if (isym->st_shndx == shndx1)
7373 symtable1[count1++].u.isym = isym;
7376 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7377 if (isym->st_shndx == shndx2)
7378 symtable2[count2++].u.isym = isym;
7380 if (count1 == 0 || count2 == 0 || count1 != count2)
7383 for (i = 0; i < count1; i++)
7385 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7386 symtable1[i].u.isym->st_name);
7388 for (i = 0; i < count2; i++)
7390 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7391 symtable2[i].u.isym->st_name);
7393 /* Sort symbol by name. */
7394 qsort (symtable1, count1, sizeof (struct elf_symbol),
7395 elf_sym_name_compare);
7396 qsort (symtable2, count1, sizeof (struct elf_symbol),
7397 elf_sym_name_compare);
7399 for (i = 0; i < count1; i++)
7400 /* Two symbols must have the same binding, type and name. */
7401 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7402 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7403 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7421 /* Return TRUE if 2 section types are compatible. */
7424 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7425 bfd *bbfd, const asection *bsec)
7429 || abfd->xvec->flavour != bfd_target_elf_flavour
7430 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7433 return elf_section_type (asec) == elf_section_type (bsec);
7436 /* Final phase of ELF linker. */
7438 /* A structure we use to avoid passing large numbers of arguments. */
7440 struct elf_final_link_info
7442 /* General link information. */
7443 struct bfd_link_info *info;
7446 /* Symbol string table. */
7447 struct elf_strtab_hash *symstrtab;
7448 /* .dynsym section. */
7449 asection *dynsym_sec;
7450 /* .hash section. */
7452 /* symbol version section (.gnu.version). */
7453 asection *symver_sec;
7454 /* Buffer large enough to hold contents of any section. */
7456 /* Buffer large enough to hold external relocs of any section. */
7457 void *external_relocs;
7458 /* Buffer large enough to hold internal relocs of any section. */
7459 Elf_Internal_Rela *internal_relocs;
7460 /* Buffer large enough to hold external local symbols of any input
7462 bfd_byte *external_syms;
7463 /* And a buffer for symbol section indices. */
7464 Elf_External_Sym_Shndx *locsym_shndx;
7465 /* Buffer large enough to hold internal local symbols of any input
7467 Elf_Internal_Sym *internal_syms;
7468 /* Array large enough to hold a symbol index for each local symbol
7469 of any input BFD. */
7471 /* Array large enough to hold a section pointer for each local
7472 symbol of any input BFD. */
7473 asection **sections;
7474 /* Buffer for SHT_SYMTAB_SHNDX section. */
7475 Elf_External_Sym_Shndx *symshndxbuf;
7476 /* Number of STT_FILE syms seen. */
7477 size_t filesym_count;
7480 /* This struct is used to pass information to elf_link_output_extsym. */
7482 struct elf_outext_info
7485 bfd_boolean localsyms;
7486 bfd_boolean file_sym_done;
7487 struct elf_final_link_info *flinfo;
7491 /* Support for evaluating a complex relocation.
7493 Complex relocations are generalized, self-describing relocations. The
7494 implementation of them consists of two parts: complex symbols, and the
7495 relocations themselves.
7497 The relocations are use a reserved elf-wide relocation type code (R_RELC
7498 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7499 information (start bit, end bit, word width, etc) into the addend. This
7500 information is extracted from CGEN-generated operand tables within gas.
7502 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7503 internal) representing prefix-notation expressions, including but not
7504 limited to those sorts of expressions normally encoded as addends in the
7505 addend field. The symbol mangling format is:
7508 | <unary-operator> ':' <node>
7509 | <binary-operator> ':' <node> ':' <node>
7512 <literal> := 's' <digits=N> ':' <N character symbol name>
7513 | 'S' <digits=N> ':' <N character section name>
7517 <binary-operator> := as in C
7518 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7521 set_symbol_value (bfd *bfd_with_globals,
7522 Elf_Internal_Sym *isymbuf,
7527 struct elf_link_hash_entry **sym_hashes;
7528 struct elf_link_hash_entry *h;
7529 size_t extsymoff = locsymcount;
7531 if (symidx < locsymcount)
7533 Elf_Internal_Sym *sym;
7535 sym = isymbuf + symidx;
7536 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7538 /* It is a local symbol: move it to the
7539 "absolute" section and give it a value. */
7540 sym->st_shndx = SHN_ABS;
7541 sym->st_value = val;
7544 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7548 /* It is a global symbol: set its link type
7549 to "defined" and give it a value. */
7551 sym_hashes = elf_sym_hashes (bfd_with_globals);
7552 h = sym_hashes [symidx - extsymoff];
7553 while (h->root.type == bfd_link_hash_indirect
7554 || h->root.type == bfd_link_hash_warning)
7555 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7556 h->root.type = bfd_link_hash_defined;
7557 h->root.u.def.value = val;
7558 h->root.u.def.section = bfd_abs_section_ptr;
7562 resolve_symbol (const char *name,
7564 struct elf_final_link_info *flinfo,
7566 Elf_Internal_Sym *isymbuf,
7569 Elf_Internal_Sym *sym;
7570 struct bfd_link_hash_entry *global_entry;
7571 const char *candidate = NULL;
7572 Elf_Internal_Shdr *symtab_hdr;
7575 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7577 for (i = 0; i < locsymcount; ++ i)
7581 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7584 candidate = bfd_elf_string_from_elf_section (input_bfd,
7585 symtab_hdr->sh_link,
7588 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7589 name, candidate, (unsigned long) sym->st_value);
7591 if (candidate && strcmp (candidate, name) == 0)
7593 asection *sec = flinfo->sections [i];
7595 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7596 *result += sec->output_offset + sec->output_section->vma;
7598 printf ("Found symbol with value %8.8lx\n",
7599 (unsigned long) *result);
7605 /* Hmm, haven't found it yet. perhaps it is a global. */
7606 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7607 FALSE, FALSE, TRUE);
7611 if (global_entry->type == bfd_link_hash_defined
7612 || global_entry->type == bfd_link_hash_defweak)
7614 *result = (global_entry->u.def.value
7615 + global_entry->u.def.section->output_section->vma
7616 + global_entry->u.def.section->output_offset);
7618 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7619 global_entry->root.string, (unsigned long) *result);
7628 resolve_section (const char *name,
7635 for (curr = sections; curr; curr = curr->next)
7636 if (strcmp (curr->name, name) == 0)
7638 *result = curr->vma;
7642 /* Hmm. still haven't found it. try pseudo-section names. */
7643 for (curr = sections; curr; curr = curr->next)
7645 len = strlen (curr->name);
7646 if (len > strlen (name))
7649 if (strncmp (curr->name, name, len) == 0)
7651 if (strncmp (".end", name + len, 4) == 0)
7653 *result = curr->vma + curr->size;
7657 /* Insert more pseudo-section names here, if you like. */
7665 undefined_reference (const char *reftype, const char *name)
7667 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7672 eval_symbol (bfd_vma *result,
7675 struct elf_final_link_info *flinfo,
7677 Elf_Internal_Sym *isymbuf,
7686 const char *sym = *symp;
7688 bfd_boolean symbol_is_section = FALSE;
7693 if (len < 1 || len > sizeof (symbuf))
7695 bfd_set_error (bfd_error_invalid_operation);
7708 *result = strtoul (sym, (char **) symp, 16);
7712 symbol_is_section = TRUE;
7715 symlen = strtol (sym, (char **) symp, 10);
7716 sym = *symp + 1; /* Skip the trailing ':'. */
7718 if (symend < sym || symlen + 1 > sizeof (symbuf))
7720 bfd_set_error (bfd_error_invalid_operation);
7724 memcpy (symbuf, sym, symlen);
7725 symbuf[symlen] = '\0';
7726 *symp = sym + symlen;
7728 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7729 the symbol as a section, or vice-versa. so we're pretty liberal in our
7730 interpretation here; section means "try section first", not "must be a
7731 section", and likewise with symbol. */
7733 if (symbol_is_section)
7735 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7736 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7737 isymbuf, locsymcount))
7739 undefined_reference ("section", symbuf);
7745 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7746 isymbuf, locsymcount)
7747 && !resolve_section (symbuf, flinfo->output_bfd->sections,
7750 undefined_reference ("symbol", symbuf);
7757 /* All that remains are operators. */
7759 #define UNARY_OP(op) \
7760 if (strncmp (sym, #op, strlen (#op)) == 0) \
7762 sym += strlen (#op); \
7766 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7767 isymbuf, locsymcount, signed_p)) \
7770 *result = op ((bfd_signed_vma) a); \
7776 #define BINARY_OP(op) \
7777 if (strncmp (sym, #op, strlen (#op)) == 0) \
7779 sym += strlen (#op); \
7783 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7784 isymbuf, locsymcount, signed_p)) \
7787 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
7788 isymbuf, locsymcount, signed_p)) \
7791 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7821 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7822 bfd_set_error (bfd_error_invalid_operation);
7828 put_value (bfd_vma size,
7829 unsigned long chunksz,
7834 location += (size - chunksz);
7836 for (; size; size -= chunksz, location -= chunksz)
7841 bfd_put_8 (input_bfd, x, location);
7845 bfd_put_16 (input_bfd, x, location);
7849 bfd_put_32 (input_bfd, x, location);
7850 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
7856 bfd_put_64 (input_bfd, x, location);
7857 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
7870 get_value (bfd_vma size,
7871 unsigned long chunksz,
7878 /* Sanity checks. */
7879 BFD_ASSERT (chunksz <= sizeof (x)
7882 && (size % chunksz) == 0
7883 && input_bfd != NULL
7884 && location != NULL);
7886 if (chunksz == sizeof (x))
7888 BFD_ASSERT (size == chunksz);
7890 /* Make sure that we do not perform an undefined shift operation.
7891 We know that size == chunksz so there will only be one iteration
7892 of the loop below. */
7896 shift = 8 * chunksz;
7898 for (; size; size -= chunksz, location += chunksz)
7903 x = (x << shift) | bfd_get_8 (input_bfd, location);
7906 x = (x << shift) | bfd_get_16 (input_bfd, location);
7909 x = (x << shift) | bfd_get_32 (input_bfd, location);
7913 x = (x << shift) | bfd_get_64 (input_bfd, location);
7924 decode_complex_addend (unsigned long *start, /* in bits */
7925 unsigned long *oplen, /* in bits */
7926 unsigned long *len, /* in bits */
7927 unsigned long *wordsz, /* in bytes */
7928 unsigned long *chunksz, /* in bytes */
7929 unsigned long *lsb0_p,
7930 unsigned long *signed_p,
7931 unsigned long *trunc_p,
7932 unsigned long encoded)
7934 * start = encoded & 0x3F;
7935 * len = (encoded >> 6) & 0x3F;
7936 * oplen = (encoded >> 12) & 0x3F;
7937 * wordsz = (encoded >> 18) & 0xF;
7938 * chunksz = (encoded >> 22) & 0xF;
7939 * lsb0_p = (encoded >> 27) & 1;
7940 * signed_p = (encoded >> 28) & 1;
7941 * trunc_p = (encoded >> 29) & 1;
7944 bfd_reloc_status_type
7945 bfd_elf_perform_complex_relocation (bfd *input_bfd,
7946 asection *input_section ATTRIBUTE_UNUSED,
7948 Elf_Internal_Rela *rel,
7951 bfd_vma shift, x, mask;
7952 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
7953 bfd_reloc_status_type r;
7955 /* Perform this reloc, since it is complex.
7956 (this is not to say that it necessarily refers to a complex
7957 symbol; merely that it is a self-describing CGEN based reloc.
7958 i.e. the addend has the complete reloc information (bit start, end,
7959 word size, etc) encoded within it.). */
7961 decode_complex_addend (&start, &oplen, &len, &wordsz,
7962 &chunksz, &lsb0_p, &signed_p,
7963 &trunc_p, rel->r_addend);
7965 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7968 shift = (start + 1) - len;
7970 shift = (8 * wordsz) - (start + len);
7972 /* FIXME: octets_per_byte. */
7973 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7976 printf ("Doing complex reloc: "
7977 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7978 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7979 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7980 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7981 oplen, (unsigned long) x, (unsigned long) mask,
7982 (unsigned long) relocation);
7987 /* Now do an overflow check. */
7988 r = bfd_check_overflow ((signed_p
7989 ? complain_overflow_signed
7990 : complain_overflow_unsigned),
7991 len, 0, (8 * wordsz),
7995 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7998 printf (" relocation: %8.8lx\n"
7999 " shifted mask: %8.8lx\n"
8000 " shifted/masked reloc: %8.8lx\n"
8001 " result: %8.8lx\n",
8002 (unsigned long) relocation, (unsigned long) (mask << shift),
8003 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8005 /* FIXME: octets_per_byte. */
8006 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
8010 /* qsort comparison functions sorting external relocs by r_offset. */
8013 cmp_ext32l_r_offset (const void *p, const void *q)
8020 const union aligned32 *a
8021 = (const union aligned32 *) ((const Elf32_External_Rel *) p)->r_offset;
8022 const union aligned32 *b
8023 = (const union aligned32 *) ((const Elf32_External_Rel *) q)->r_offset;
8025 uint32_t aval = ( (uint32_t) a->c[0]
8026 | (uint32_t) a->c[1] << 8
8027 | (uint32_t) a->c[2] << 16
8028 | (uint32_t) a->c[3] << 24);
8029 uint32_t bval = ( (uint32_t) b->c[0]
8030 | (uint32_t) b->c[1] << 8
8031 | (uint32_t) b->c[2] << 16
8032 | (uint32_t) b->c[3] << 24);
8035 else if (aval > bval)
8041 cmp_ext32b_r_offset (const void *p, const void *q)
8048 const union aligned32 *a
8049 = (const union aligned32 *) ((const Elf32_External_Rel *) p)->r_offset;
8050 const union aligned32 *b
8051 = (const union aligned32 *) ((const Elf32_External_Rel *) q)->r_offset;
8053 uint32_t aval = ( (uint32_t) a->c[0] << 24
8054 | (uint32_t) a->c[1] << 16
8055 | (uint32_t) a->c[2] << 8
8056 | (uint32_t) a->c[3]);
8057 uint32_t bval = ( (uint32_t) b->c[0] << 24
8058 | (uint32_t) b->c[1] << 16
8059 | (uint32_t) b->c[2] << 8
8060 | (uint32_t) b->c[3]);
8063 else if (aval > bval)
8068 #ifdef BFD_HOST_64_BIT
8070 cmp_ext64l_r_offset (const void *p, const void *q)
8077 const union aligned64 *a
8078 = (const union aligned64 *) ((const Elf64_External_Rel *) p)->r_offset;
8079 const union aligned64 *b
8080 = (const union aligned64 *) ((const Elf64_External_Rel *) q)->r_offset;
8082 uint64_t aval = ( (uint64_t) a->c[0]
8083 | (uint64_t) a->c[1] << 8
8084 | (uint64_t) a->c[2] << 16
8085 | (uint64_t) a->c[3] << 24
8086 | (uint64_t) a->c[4] << 32
8087 | (uint64_t) a->c[5] << 40
8088 | (uint64_t) a->c[6] << 48
8089 | (uint64_t) a->c[7] << 56);
8090 uint64_t bval = ( (uint64_t) b->c[0]
8091 | (uint64_t) b->c[1] << 8
8092 | (uint64_t) b->c[2] << 16
8093 | (uint64_t) b->c[3] << 24
8094 | (uint64_t) b->c[4] << 32
8095 | (uint64_t) b->c[5] << 40
8096 | (uint64_t) b->c[6] << 48
8097 | (uint64_t) b->c[7] << 56);
8100 else if (aval > bval)
8106 cmp_ext64b_r_offset (const void *p, const void *q)
8113 const union aligned64 *a
8114 = (const union aligned64 *) ((const Elf64_External_Rel *) p)->r_offset;
8115 const union aligned64 *b
8116 = (const union aligned64 *) ((const Elf64_External_Rel *) q)->r_offset;
8118 uint64_t aval = ( (uint64_t) a->c[0] << 56
8119 | (uint64_t) a->c[1] << 48
8120 | (uint64_t) a->c[2] << 40
8121 | (uint64_t) a->c[3] << 32
8122 | (uint64_t) a->c[4] << 24
8123 | (uint64_t) a->c[5] << 16
8124 | (uint64_t) a->c[6] << 8
8125 | (uint64_t) a->c[7]);
8126 uint64_t bval = ( (uint64_t) b->c[0] << 56
8127 | (uint64_t) b->c[1] << 48
8128 | (uint64_t) b->c[2] << 40
8129 | (uint64_t) b->c[3] << 32
8130 | (uint64_t) b->c[4] << 24
8131 | (uint64_t) b->c[5] << 16
8132 | (uint64_t) b->c[6] << 8
8133 | (uint64_t) b->c[7]);
8136 else if (aval > bval)
8142 /* When performing a relocatable link, the input relocations are
8143 preserved. But, if they reference global symbols, the indices
8144 referenced must be updated. Update all the relocations found in
8148 elf_link_adjust_relocs (bfd *abfd,
8149 struct bfd_elf_section_reloc_data *reldata,
8153 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8155 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8156 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8157 bfd_vma r_type_mask;
8159 unsigned int count = reldata->count;
8160 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8162 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8164 swap_in = bed->s->swap_reloc_in;
8165 swap_out = bed->s->swap_reloc_out;
8167 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8169 swap_in = bed->s->swap_reloca_in;
8170 swap_out = bed->s->swap_reloca_out;
8175 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8178 if (bed->s->arch_size == 32)
8185 r_type_mask = 0xffffffff;
8189 erela = reldata->hdr->contents;
8190 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8192 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8195 if (*rel_hash == NULL)
8198 BFD_ASSERT ((*rel_hash)->indx >= 0);
8200 (*swap_in) (abfd, erela, irela);
8201 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8202 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8203 | (irela[j].r_info & r_type_mask));
8204 (*swap_out) (abfd, irela, erela);
8209 int (*compare) (const void *, const void *);
8211 if (bed->s->arch_size == 32)
8213 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8214 compare = cmp_ext32l_r_offset;
8215 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8216 compare = cmp_ext32b_r_offset;
8222 #ifdef BFD_HOST_64_BIT
8223 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8224 compare = cmp_ext64l_r_offset;
8225 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8226 compare = cmp_ext64b_r_offset;
8231 qsort (reldata->hdr->contents, count, reldata->hdr->sh_entsize, compare);
8232 free (reldata->hashes);
8233 reldata->hashes = NULL;
8237 struct elf_link_sort_rela
8243 enum elf_reloc_type_class type;
8244 /* We use this as an array of size int_rels_per_ext_rel. */
8245 Elf_Internal_Rela rela[1];
8249 elf_link_sort_cmp1 (const void *A, const void *B)
8251 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8252 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8253 int relativea, relativeb;
8255 relativea = a->type == reloc_class_relative;
8256 relativeb = b->type == reloc_class_relative;
8258 if (relativea < relativeb)
8260 if (relativea > relativeb)
8262 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8264 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8266 if (a->rela->r_offset < b->rela->r_offset)
8268 if (a->rela->r_offset > b->rela->r_offset)
8274 elf_link_sort_cmp2 (const void *A, const void *B)
8276 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8277 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8279 if (a->type < b->type)
8281 if (a->type > b->type)
8283 if (a->u.offset < b->u.offset)
8285 if (a->u.offset > b->u.offset)
8287 if (a->rela->r_offset < b->rela->r_offset)
8289 if (a->rela->r_offset > b->rela->r_offset)
8295 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8297 asection *dynamic_relocs;
8300 bfd_size_type count, size;
8301 size_t i, ret, sort_elt, ext_size;
8302 bfd_byte *sort, *s_non_relative, *p;
8303 struct elf_link_sort_rela *sq;
8304 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8305 int i2e = bed->s->int_rels_per_ext_rel;
8306 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8307 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8308 struct bfd_link_order *lo;
8310 bfd_boolean use_rela;
8312 /* Find a dynamic reloc section. */
8313 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8314 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8315 if (rela_dyn != NULL && rela_dyn->size > 0
8316 && rel_dyn != NULL && rel_dyn->size > 0)
8318 bfd_boolean use_rela_initialised = FALSE;
8320 /* This is just here to stop gcc from complaining.
8321 It's initialization checking code is not perfect. */
8324 /* Both sections are present. Examine the sizes
8325 of the indirect sections to help us choose. */
8326 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8327 if (lo->type == bfd_indirect_link_order)
8329 asection *o = lo->u.indirect.section;
8331 if ((o->size % bed->s->sizeof_rela) == 0)
8333 if ((o->size % bed->s->sizeof_rel) == 0)
8334 /* Section size is divisible by both rel and rela sizes.
8335 It is of no help to us. */
8339 /* Section size is only divisible by rela. */
8340 if (use_rela_initialised && (use_rela == FALSE))
8343 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8344 bfd_set_error (bfd_error_invalid_operation);
8350 use_rela_initialised = TRUE;
8354 else if ((o->size % bed->s->sizeof_rel) == 0)
8356 /* Section size is only divisible by rel. */
8357 if (use_rela_initialised && (use_rela == TRUE))
8360 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8361 bfd_set_error (bfd_error_invalid_operation);
8367 use_rela_initialised = TRUE;
8372 /* The section size is not divisible by either - something is wrong. */
8374 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8375 bfd_set_error (bfd_error_invalid_operation);
8380 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8381 if (lo->type == bfd_indirect_link_order)
8383 asection *o = lo->u.indirect.section;
8385 if ((o->size % bed->s->sizeof_rela) == 0)
8387 if ((o->size % bed->s->sizeof_rel) == 0)
8388 /* Section size is divisible by both rel and rela sizes.
8389 It is of no help to us. */
8393 /* Section size is only divisible by rela. */
8394 if (use_rela_initialised && (use_rela == FALSE))
8397 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8398 bfd_set_error (bfd_error_invalid_operation);
8404 use_rela_initialised = TRUE;
8408 else if ((o->size % bed->s->sizeof_rel) == 0)
8410 /* Section size is only divisible by rel. */
8411 if (use_rela_initialised && (use_rela == TRUE))
8414 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8415 bfd_set_error (bfd_error_invalid_operation);
8421 use_rela_initialised = TRUE;
8426 /* The section size is not divisible by either - something is wrong. */
8428 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8429 bfd_set_error (bfd_error_invalid_operation);
8434 if (! use_rela_initialised)
8438 else if (rela_dyn != NULL && rela_dyn->size > 0)
8440 else if (rel_dyn != NULL && rel_dyn->size > 0)
8447 dynamic_relocs = rela_dyn;
8448 ext_size = bed->s->sizeof_rela;
8449 swap_in = bed->s->swap_reloca_in;
8450 swap_out = bed->s->swap_reloca_out;
8454 dynamic_relocs = rel_dyn;
8455 ext_size = bed->s->sizeof_rel;
8456 swap_in = bed->s->swap_reloc_in;
8457 swap_out = bed->s->swap_reloc_out;
8461 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8462 if (lo->type == bfd_indirect_link_order)
8463 size += lo->u.indirect.section->size;
8465 if (size != dynamic_relocs->size)
8468 sort_elt = (sizeof (struct elf_link_sort_rela)
8469 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8471 count = dynamic_relocs->size / ext_size;
8474 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8478 (*info->callbacks->warning)
8479 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8483 if (bed->s->arch_size == 32)
8484 r_sym_mask = ~(bfd_vma) 0xff;
8486 r_sym_mask = ~(bfd_vma) 0xffffffff;
8488 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8489 if (lo->type == bfd_indirect_link_order)
8491 bfd_byte *erel, *erelend;
8492 asection *o = lo->u.indirect.section;
8494 if (o->contents == NULL && o->size != 0)
8496 /* This is a reloc section that is being handled as a normal
8497 section. See bfd_section_from_shdr. We can't combine
8498 relocs in this case. */
8503 erelend = o->contents + o->size;
8504 /* FIXME: octets_per_byte. */
8505 p = sort + o->output_offset / ext_size * sort_elt;
8507 while (erel < erelend)
8509 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8511 (*swap_in) (abfd, erel, s->rela);
8512 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8513 s->u.sym_mask = r_sym_mask;
8519 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8521 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8523 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8524 if (s->type != reloc_class_relative)
8530 sq = (struct elf_link_sort_rela *) s_non_relative;
8531 for (; i < count; i++, p += sort_elt)
8533 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8534 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8536 sp->u.offset = sq->rela->r_offset;
8539 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8541 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8542 if (lo->type == bfd_indirect_link_order)
8544 bfd_byte *erel, *erelend;
8545 asection *o = lo->u.indirect.section;
8548 erelend = o->contents + o->size;
8549 /* FIXME: octets_per_byte. */
8550 p = sort + o->output_offset / ext_size * sort_elt;
8551 while (erel < erelend)
8553 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8554 (*swap_out) (abfd, s->rela, erel);
8561 *psec = dynamic_relocs;
8565 /* Add a symbol to the output symbol string table. */
8568 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8570 Elf_Internal_Sym *elfsym,
8571 asection *input_sec,
8572 struct elf_link_hash_entry *h)
8574 int (*output_symbol_hook)
8575 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8576 struct elf_link_hash_entry *);
8577 struct elf_link_hash_table *hash_table;
8578 const struct elf_backend_data *bed;
8579 bfd_size_type strtabsize;
8581 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8583 bed = get_elf_backend_data (flinfo->output_bfd);
8584 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8585 if (output_symbol_hook != NULL)
8587 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8594 || (input_sec->flags & SEC_EXCLUDE))
8595 elfsym->st_name = (unsigned long) -1;
8598 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8599 to get the final offset for st_name. */
8601 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8603 if (elfsym->st_name == (unsigned long) -1)
8607 hash_table = elf_hash_table (flinfo->info);
8608 strtabsize = hash_table->strtabsize;
8609 if (strtabsize <= hash_table->strtabcount)
8611 strtabsize += strtabsize;
8612 hash_table->strtabsize = strtabsize;
8613 strtabsize *= sizeof (*hash_table->strtab);
8615 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8617 if (hash_table->strtab == NULL)
8620 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8621 hash_table->strtab[hash_table->strtabcount].dest_index
8622 = hash_table->strtabcount;
8623 hash_table->strtab[hash_table->strtabcount].destshndx_index
8624 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8626 bfd_get_symcount (flinfo->output_bfd) += 1;
8627 hash_table->strtabcount += 1;
8632 /* Swap symbols out to the symbol table and flush the output symbols to
8636 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8638 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8639 bfd_size_type amt, i;
8640 const struct elf_backend_data *bed;
8642 Elf_Internal_Shdr *hdr;
8646 if (!hash_table->strtabcount)
8649 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8651 bed = get_elf_backend_data (flinfo->output_bfd);
8653 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8654 symbuf = (bfd_byte *) bfd_malloc (amt);
8658 if (flinfo->symshndxbuf)
8660 amt = (sizeof (Elf_External_Sym_Shndx)
8661 * (bfd_get_symcount (flinfo->output_bfd)));
8662 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8663 if (flinfo->symshndxbuf == NULL)
8670 for (i = 0; i < hash_table->strtabcount; i++)
8672 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8673 if (elfsym->sym.st_name == (unsigned long) -1)
8674 elfsym->sym.st_name = 0;
8677 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8678 elfsym->sym.st_name);
8679 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8680 ((bfd_byte *) symbuf
8681 + (elfsym->dest_index
8682 * bed->s->sizeof_sym)),
8683 (flinfo->symshndxbuf
8684 + elfsym->destshndx_index));
8687 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8688 pos = hdr->sh_offset + hdr->sh_size;
8689 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8690 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8691 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8693 hdr->sh_size += amt;
8701 free (hash_table->strtab);
8702 hash_table->strtab = NULL;
8707 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8710 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8712 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8713 && sym->st_shndx < SHN_LORESERVE)
8715 /* The gABI doesn't support dynamic symbols in output sections
8717 (*_bfd_error_handler)
8718 (_("%B: Too many sections: %d (>= %d)"),
8719 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8720 bfd_set_error (bfd_error_nonrepresentable_section);
8726 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8727 allowing an unsatisfied unversioned symbol in the DSO to match a
8728 versioned symbol that would normally require an explicit version.
8729 We also handle the case that a DSO references a hidden symbol
8730 which may be satisfied by a versioned symbol in another DSO. */
8733 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8734 const struct elf_backend_data *bed,
8735 struct elf_link_hash_entry *h)
8738 struct elf_link_loaded_list *loaded;
8740 if (!is_elf_hash_table (info->hash))
8743 /* Check indirect symbol. */
8744 while (h->root.type == bfd_link_hash_indirect)
8745 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8747 switch (h->root.type)
8753 case bfd_link_hash_undefined:
8754 case bfd_link_hash_undefweak:
8755 abfd = h->root.u.undef.abfd;
8756 if ((abfd->flags & DYNAMIC) == 0
8757 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8761 case bfd_link_hash_defined:
8762 case bfd_link_hash_defweak:
8763 abfd = h->root.u.def.section->owner;
8766 case bfd_link_hash_common:
8767 abfd = h->root.u.c.p->section->owner;
8770 BFD_ASSERT (abfd != NULL);
8772 for (loaded = elf_hash_table (info)->loaded;
8774 loaded = loaded->next)
8777 Elf_Internal_Shdr *hdr;
8778 bfd_size_type symcount;
8779 bfd_size_type extsymcount;
8780 bfd_size_type extsymoff;
8781 Elf_Internal_Shdr *versymhdr;
8782 Elf_Internal_Sym *isym;
8783 Elf_Internal_Sym *isymend;
8784 Elf_Internal_Sym *isymbuf;
8785 Elf_External_Versym *ever;
8786 Elf_External_Versym *extversym;
8788 input = loaded->abfd;
8790 /* We check each DSO for a possible hidden versioned definition. */
8792 || (input->flags & DYNAMIC) == 0
8793 || elf_dynversym (input) == 0)
8796 hdr = &elf_tdata (input)->dynsymtab_hdr;
8798 symcount = hdr->sh_size / bed->s->sizeof_sym;
8799 if (elf_bad_symtab (input))
8801 extsymcount = symcount;
8806 extsymcount = symcount - hdr->sh_info;
8807 extsymoff = hdr->sh_info;
8810 if (extsymcount == 0)
8813 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8815 if (isymbuf == NULL)
8818 /* Read in any version definitions. */
8819 versymhdr = &elf_tdata (input)->dynversym_hdr;
8820 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
8821 if (extversym == NULL)
8824 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8825 || (bfd_bread (extversym, versymhdr->sh_size, input)
8826 != versymhdr->sh_size))
8834 ever = extversym + extsymoff;
8835 isymend = isymbuf + extsymcount;
8836 for (isym = isymbuf; isym < isymend; isym++, ever++)
8839 Elf_Internal_Versym iver;
8840 unsigned short version_index;
8842 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8843 || isym->st_shndx == SHN_UNDEF)
8846 name = bfd_elf_string_from_elf_section (input,
8849 if (strcmp (name, h->root.root.string) != 0)
8852 _bfd_elf_swap_versym_in (input, ever, &iver);
8854 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8856 && h->forced_local))
8858 /* If we have a non-hidden versioned sym, then it should
8859 have provided a definition for the undefined sym unless
8860 it is defined in a non-shared object and forced local.
8865 version_index = iver.vs_vers & VERSYM_VERSION;
8866 if (version_index == 1 || version_index == 2)
8868 /* This is the base or first version. We can use it. */
8882 /* Add an external symbol to the symbol table. This is called from
8883 the hash table traversal routine. When generating a shared object,
8884 we go through the symbol table twice. The first time we output
8885 anything that might have been forced to local scope in a version
8886 script. The second time we output the symbols that are still
8890 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
8892 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
8893 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8894 struct elf_final_link_info *flinfo = eoinfo->flinfo;
8896 Elf_Internal_Sym sym;
8897 asection *input_sec;
8898 const struct elf_backend_data *bed;
8902 if (h->root.type == bfd_link_hash_warning)
8904 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8905 if (h->root.type == bfd_link_hash_new)
8909 /* Decide whether to output this symbol in this pass. */
8910 if (eoinfo->localsyms)
8912 if (!h->forced_local)
8917 if (h->forced_local)
8921 bed = get_elf_backend_data (flinfo->output_bfd);
8923 if (h->root.type == bfd_link_hash_undefined)
8925 /* If we have an undefined symbol reference here then it must have
8926 come from a shared library that is being linked in. (Undefined
8927 references in regular files have already been handled unless
8928 they are in unreferenced sections which are removed by garbage
8930 bfd_boolean ignore_undef = FALSE;
8932 /* Some symbols may be special in that the fact that they're
8933 undefined can be safely ignored - let backend determine that. */
8934 if (bed->elf_backend_ignore_undef_symbol)
8935 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8937 /* If we are reporting errors for this situation then do so now. */
8940 && (!h->ref_regular || flinfo->info->gc_sections)
8941 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
8942 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
8944 if (!(flinfo->info->callbacks->undefined_symbol
8945 (flinfo->info, h->root.root.string,
8946 h->ref_regular ? NULL : h->root.u.undef.abfd,
8948 (flinfo->info->unresolved_syms_in_shared_libs
8949 == RM_GENERATE_ERROR))))
8951 bfd_set_error (bfd_error_bad_value);
8952 eoinfo->failed = TRUE;
8958 /* We should also warn if a forced local symbol is referenced from
8959 shared libraries. */
8960 if (!flinfo->info->relocatable
8961 && flinfo->info->executable
8966 && h->ref_dynamic_nonweak
8967 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
8971 struct elf_link_hash_entry *hi = h;
8973 /* Check indirect symbol. */
8974 while (hi->root.type == bfd_link_hash_indirect)
8975 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
8977 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
8978 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
8979 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
8980 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
8982 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8983 def_bfd = flinfo->output_bfd;
8984 if (hi->root.u.def.section != bfd_abs_section_ptr)
8985 def_bfd = hi->root.u.def.section->owner;
8986 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
8987 h->root.root.string);
8988 bfd_set_error (bfd_error_bad_value);
8989 eoinfo->failed = TRUE;
8993 /* We don't want to output symbols that have never been mentioned by
8994 a regular file, or that we have been told to strip. However, if
8995 h->indx is set to -2, the symbol is used by a reloc and we must
9000 else if ((h->def_dynamic
9002 || h->root.type == bfd_link_hash_new)
9006 else if (flinfo->info->strip == strip_all)
9008 else if (flinfo->info->strip == strip_some
9009 && bfd_hash_lookup (flinfo->info->keep_hash,
9010 h->root.root.string, FALSE, FALSE) == NULL)
9012 else if ((h->root.type == bfd_link_hash_defined
9013 || h->root.type == bfd_link_hash_defweak)
9014 && ((flinfo->info->strip_discarded
9015 && discarded_section (h->root.u.def.section))
9016 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9017 && h->root.u.def.section->owner != NULL
9018 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9020 else if ((h->root.type == bfd_link_hash_undefined
9021 || h->root.type == bfd_link_hash_undefweak)
9022 && h->root.u.undef.abfd != NULL
9023 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9026 /* If we're stripping it, and it's not a dynamic symbol, there's
9027 nothing else to do. However, if it is a forced local symbol or
9028 an ifunc symbol we need to give the backend finish_dynamic_symbol
9029 function a chance to make it dynamic. */
9032 && h->type != STT_GNU_IFUNC
9033 && !h->forced_local)
9037 sym.st_size = h->size;
9038 sym.st_other = h->other;
9039 if (h->forced_local)
9041 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
9042 /* Turn off visibility on local symbol. */
9043 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9045 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9046 else if (h->unique_global && h->def_regular)
9047 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
9048 else if (h->root.type == bfd_link_hash_undefweak
9049 || h->root.type == bfd_link_hash_defweak)
9050 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
9052 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
9053 sym.st_target_internal = h->target_internal;
9055 switch (h->root.type)
9058 case bfd_link_hash_new:
9059 case bfd_link_hash_warning:
9063 case bfd_link_hash_undefined:
9064 case bfd_link_hash_undefweak:
9065 input_sec = bfd_und_section_ptr;
9066 sym.st_shndx = SHN_UNDEF;
9069 case bfd_link_hash_defined:
9070 case bfd_link_hash_defweak:
9072 input_sec = h->root.u.def.section;
9073 if (input_sec->output_section != NULL)
9076 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9077 input_sec->output_section);
9078 if (sym.st_shndx == SHN_BAD)
9080 (*_bfd_error_handler)
9081 (_("%B: could not find output section %A for input section %A"),
9082 flinfo->output_bfd, input_sec->output_section, input_sec);
9083 bfd_set_error (bfd_error_nonrepresentable_section);
9084 eoinfo->failed = TRUE;
9088 /* ELF symbols in relocatable files are section relative,
9089 but in nonrelocatable files they are virtual
9091 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9092 if (!flinfo->info->relocatable)
9094 sym.st_value += input_sec->output_section->vma;
9095 if (h->type == STT_TLS)
9097 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9098 if (tls_sec != NULL)
9099 sym.st_value -= tls_sec->vma;
9105 BFD_ASSERT (input_sec->owner == NULL
9106 || (input_sec->owner->flags & DYNAMIC) != 0);
9107 sym.st_shndx = SHN_UNDEF;
9108 input_sec = bfd_und_section_ptr;
9113 case bfd_link_hash_common:
9114 input_sec = h->root.u.c.p->section;
9115 sym.st_shndx = bed->common_section_index (input_sec);
9116 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9119 case bfd_link_hash_indirect:
9120 /* These symbols are created by symbol versioning. They point
9121 to the decorated version of the name. For example, if the
9122 symbol foo@@GNU_1.2 is the default, which should be used when
9123 foo is used with no version, then we add an indirect symbol
9124 foo which points to foo@@GNU_1.2. We ignore these symbols,
9125 since the indirected symbol is already in the hash table. */
9129 /* Give the processor backend a chance to tweak the symbol value,
9130 and also to finish up anything that needs to be done for this
9131 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9132 forced local syms when non-shared is due to a historical quirk.
9133 STT_GNU_IFUNC symbol must go through PLT. */
9134 if ((h->type == STT_GNU_IFUNC
9136 && !flinfo->info->relocatable)
9137 || ((h->dynindx != -1
9139 && ((flinfo->info->shared
9140 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9141 || h->root.type != bfd_link_hash_undefweak))
9142 || !h->forced_local)
9143 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9145 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9146 (flinfo->output_bfd, flinfo->info, h, &sym)))
9148 eoinfo->failed = TRUE;
9153 /* If we are marking the symbol as undefined, and there are no
9154 non-weak references to this symbol from a regular object, then
9155 mark the symbol as weak undefined; if there are non-weak
9156 references, mark the symbol as strong. We can't do this earlier,
9157 because it might not be marked as undefined until the
9158 finish_dynamic_symbol routine gets through with it. */
9159 if (sym.st_shndx == SHN_UNDEF
9161 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9162 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9165 unsigned int type = ELF_ST_TYPE (sym.st_info);
9167 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9168 if (type == STT_GNU_IFUNC)
9171 if (h->ref_regular_nonweak)
9172 bindtype = STB_GLOBAL;
9174 bindtype = STB_WEAK;
9175 sym.st_info = ELF_ST_INFO (bindtype, type);
9178 /* If this is a symbol defined in a dynamic library, don't use the
9179 symbol size from the dynamic library. Relinking an executable
9180 against a new library may introduce gratuitous changes in the
9181 executable's symbols if we keep the size. */
9182 if (sym.st_shndx == SHN_UNDEF
9187 /* If a non-weak symbol with non-default visibility is not defined
9188 locally, it is a fatal error. */
9189 if (!flinfo->info->relocatable
9190 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9191 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9192 && h->root.type == bfd_link_hash_undefined
9197 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9198 msg = _("%B: protected symbol `%s' isn't defined");
9199 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9200 msg = _("%B: internal symbol `%s' isn't defined");
9202 msg = _("%B: hidden symbol `%s' isn't defined");
9203 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9204 bfd_set_error (bfd_error_bad_value);
9205 eoinfo->failed = TRUE;
9209 /* If this symbol should be put in the .dynsym section, then put it
9210 there now. We already know the symbol index. We also fill in
9211 the entry in the .hash section. */
9212 if (flinfo->dynsym_sec != NULL
9214 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9218 /* Since there is no version information in the dynamic string,
9219 if there is no version info in symbol version section, we will
9220 have a run-time problem if not linking executable, referenced
9221 by shared library, or not locally defined. */
9222 if (h->verinfo.verdef == NULL
9223 && (!flinfo->info->executable
9225 || !h->def_regular))
9227 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9229 if (p && p [1] != '\0')
9231 (*_bfd_error_handler)
9232 (_("%B: No symbol version section for versioned symbol `%s'"),
9233 flinfo->output_bfd, h->root.root.string);
9234 eoinfo->failed = TRUE;
9239 sym.st_name = h->dynstr_index;
9240 esym = flinfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
9241 if (!check_dynsym (flinfo->output_bfd, &sym))
9243 eoinfo->failed = TRUE;
9246 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9248 if (flinfo->hash_sec != NULL)
9250 size_t hash_entry_size;
9251 bfd_byte *bucketpos;
9256 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9257 bucket = h->u.elf_hash_value % bucketcount;
9260 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9261 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9262 + (bucket + 2) * hash_entry_size);
9263 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9264 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9266 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9267 ((bfd_byte *) flinfo->hash_sec->contents
9268 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9271 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9273 Elf_Internal_Versym iversym;
9274 Elf_External_Versym *eversym;
9276 if (!h->def_regular)
9278 if (h->verinfo.verdef == NULL
9279 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9280 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9281 iversym.vs_vers = 0;
9283 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9287 if (h->verinfo.vertree == NULL)
9288 iversym.vs_vers = 1;
9290 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9291 if (flinfo->info->create_default_symver)
9296 iversym.vs_vers |= VERSYM_HIDDEN;
9298 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9299 eversym += h->dynindx;
9300 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9304 /* If the symbol is undefined, and we didn't output it to .dynsym,
9305 strip it from .symtab too. Obviously we can't do this for
9306 relocatable output or when needed for --emit-relocs. */
9307 else if (input_sec == bfd_und_section_ptr
9309 && !flinfo->info->relocatable)
9311 /* Also strip others that we couldn't earlier due to dynamic symbol
9315 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9318 /* Output a FILE symbol so that following locals are not associated
9319 with the wrong input file. We need one for forced local symbols
9320 if we've seen more than one FILE symbol or when we have exactly
9321 one FILE symbol but global symbols are present in a file other
9322 than the one with the FILE symbol. We also need one if linker
9323 defined symbols are present. In practice these conditions are
9324 always met, so just emit the FILE symbol unconditionally. */
9325 if (eoinfo->localsyms
9326 && !eoinfo->file_sym_done
9327 && eoinfo->flinfo->filesym_count != 0)
9329 Elf_Internal_Sym fsym;
9331 memset (&fsym, 0, sizeof (fsym));
9332 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9333 fsym.st_shndx = SHN_ABS;
9334 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9335 bfd_und_section_ptr, NULL))
9338 eoinfo->file_sym_done = TRUE;
9341 indx = bfd_get_symcount (flinfo->output_bfd);
9342 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9346 eoinfo->failed = TRUE;
9351 else if (h->indx == -2)
9357 /* Return TRUE if special handling is done for relocs in SEC against
9358 symbols defined in discarded sections. */
9361 elf_section_ignore_discarded_relocs (asection *sec)
9363 const struct elf_backend_data *bed;
9365 switch (sec->sec_info_type)
9367 case SEC_INFO_TYPE_STABS:
9368 case SEC_INFO_TYPE_EH_FRAME:
9369 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9375 bed = get_elf_backend_data (sec->owner);
9376 if (bed->elf_backend_ignore_discarded_relocs != NULL
9377 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9383 /* Return a mask saying how ld should treat relocations in SEC against
9384 symbols defined in discarded sections. If this function returns
9385 COMPLAIN set, ld will issue a warning message. If this function
9386 returns PRETEND set, and the discarded section was link-once and the
9387 same size as the kept link-once section, ld will pretend that the
9388 symbol was actually defined in the kept section. Otherwise ld will
9389 zero the reloc (at least that is the intent, but some cooperation by
9390 the target dependent code is needed, particularly for REL targets). */
9393 _bfd_elf_default_action_discarded (asection *sec)
9395 if (sec->flags & SEC_DEBUGGING)
9398 if (strcmp (".eh_frame", sec->name) == 0)
9401 if (strcmp (".gcc_except_table", sec->name) == 0)
9404 return COMPLAIN | PRETEND;
9407 /* Find a match between a section and a member of a section group. */
9410 match_group_member (asection *sec, asection *group,
9411 struct bfd_link_info *info)
9413 asection *first = elf_next_in_group (group);
9414 asection *s = first;
9418 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9421 s = elf_next_in_group (s);
9429 /* Check if the kept section of a discarded section SEC can be used
9430 to replace it. Return the replacement if it is OK. Otherwise return
9434 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9438 kept = sec->kept_section;
9441 if ((kept->flags & SEC_GROUP) != 0)
9442 kept = match_group_member (sec, kept, info);
9444 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9445 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9447 sec->kept_section = kept;
9452 /* Link an input file into the linker output file. This function
9453 handles all the sections and relocations of the input file at once.
9454 This is so that we only have to read the local symbols once, and
9455 don't have to keep them in memory. */
9458 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9460 int (*relocate_section)
9461 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9462 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9464 Elf_Internal_Shdr *symtab_hdr;
9467 Elf_Internal_Sym *isymbuf;
9468 Elf_Internal_Sym *isym;
9469 Elf_Internal_Sym *isymend;
9471 asection **ppsection;
9473 const struct elf_backend_data *bed;
9474 struct elf_link_hash_entry **sym_hashes;
9475 bfd_size_type address_size;
9476 bfd_vma r_type_mask;
9478 bfd_boolean have_file_sym = FALSE;
9480 output_bfd = flinfo->output_bfd;
9481 bed = get_elf_backend_data (output_bfd);
9482 relocate_section = bed->elf_backend_relocate_section;
9484 /* If this is a dynamic object, we don't want to do anything here:
9485 we don't want the local symbols, and we don't want the section
9487 if ((input_bfd->flags & DYNAMIC) != 0)
9490 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9491 if (elf_bad_symtab (input_bfd))
9493 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9498 locsymcount = symtab_hdr->sh_info;
9499 extsymoff = symtab_hdr->sh_info;
9502 /* Read the local symbols. */
9503 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9504 if (isymbuf == NULL && locsymcount != 0)
9506 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9507 flinfo->internal_syms,
9508 flinfo->external_syms,
9509 flinfo->locsym_shndx);
9510 if (isymbuf == NULL)
9514 /* Find local symbol sections and adjust values of symbols in
9515 SEC_MERGE sections. Write out those local symbols we know are
9516 going into the output file. */
9517 isymend = isymbuf + locsymcount;
9518 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9520 isym++, pindex++, ppsection++)
9524 Elf_Internal_Sym osym;
9530 if (elf_bad_symtab (input_bfd))
9532 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9539 if (isym->st_shndx == SHN_UNDEF)
9540 isec = bfd_und_section_ptr;
9541 else if (isym->st_shndx == SHN_ABS)
9542 isec = bfd_abs_section_ptr;
9543 else if (isym->st_shndx == SHN_COMMON)
9544 isec = bfd_com_section_ptr;
9547 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9550 /* Don't attempt to output symbols with st_shnx in the
9551 reserved range other than SHN_ABS and SHN_COMMON. */
9555 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9556 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9558 _bfd_merged_section_offset (output_bfd, &isec,
9559 elf_section_data (isec)->sec_info,
9565 /* Don't output the first, undefined, symbol. In fact, don't
9566 output any undefined local symbol. */
9567 if (isec == bfd_und_section_ptr)
9570 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9572 /* We never output section symbols. Instead, we use the
9573 section symbol of the corresponding section in the output
9578 /* If we are stripping all symbols, we don't want to output this
9580 if (flinfo->info->strip == strip_all)
9583 /* If we are discarding all local symbols, we don't want to
9584 output this one. If we are generating a relocatable output
9585 file, then some of the local symbols may be required by
9586 relocs; we output them below as we discover that they are
9588 if (flinfo->info->discard == discard_all)
9591 /* If this symbol is defined in a section which we are
9592 discarding, we don't need to keep it. */
9593 if (isym->st_shndx != SHN_UNDEF
9594 && isym->st_shndx < SHN_LORESERVE
9595 && bfd_section_removed_from_list (output_bfd,
9596 isec->output_section))
9599 /* Get the name of the symbol. */
9600 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9605 /* See if we are discarding symbols with this name. */
9606 if ((flinfo->info->strip == strip_some
9607 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9609 || (((flinfo->info->discard == discard_sec_merge
9610 && (isec->flags & SEC_MERGE) && !flinfo->info->relocatable)
9611 || flinfo->info->discard == discard_l)
9612 && bfd_is_local_label_name (input_bfd, name)))
9615 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9617 if (input_bfd->lto_output)
9618 /* -flto puts a temp file name here. This means builds
9619 are not reproducible. Discard the symbol. */
9621 have_file_sym = TRUE;
9622 flinfo->filesym_count += 1;
9626 /* In the absence of debug info, bfd_find_nearest_line uses
9627 FILE symbols to determine the source file for local
9628 function symbols. Provide a FILE symbol here if input
9629 files lack such, so that their symbols won't be
9630 associated with a previous input file. It's not the
9631 source file, but the best we can do. */
9632 have_file_sym = TRUE;
9633 flinfo->filesym_count += 1;
9634 memset (&osym, 0, sizeof (osym));
9635 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9636 osym.st_shndx = SHN_ABS;
9637 if (!elf_link_output_symstrtab (flinfo,
9638 (input_bfd->lto_output ? NULL
9639 : input_bfd->filename),
9640 &osym, bfd_abs_section_ptr,
9647 /* Adjust the section index for the output file. */
9648 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9649 isec->output_section);
9650 if (osym.st_shndx == SHN_BAD)
9653 /* ELF symbols in relocatable files are section relative, but
9654 in executable files they are virtual addresses. Note that
9655 this code assumes that all ELF sections have an associated
9656 BFD section with a reasonable value for output_offset; below
9657 we assume that they also have a reasonable value for
9658 output_section. Any special sections must be set up to meet
9659 these requirements. */
9660 osym.st_value += isec->output_offset;
9661 if (!flinfo->info->relocatable)
9663 osym.st_value += isec->output_section->vma;
9664 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9666 /* STT_TLS symbols are relative to PT_TLS segment base. */
9667 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9668 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
9672 indx = bfd_get_symcount (output_bfd);
9673 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
9680 if (bed->s->arch_size == 32)
9688 r_type_mask = 0xffffffff;
9693 /* Relocate the contents of each section. */
9694 sym_hashes = elf_sym_hashes (input_bfd);
9695 for (o = input_bfd->sections; o != NULL; o = o->next)
9699 if (! o->linker_mark)
9701 /* This section was omitted from the link. */
9705 if (flinfo->info->relocatable
9706 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9708 /* Deal with the group signature symbol. */
9709 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9710 unsigned long symndx = sec_data->this_hdr.sh_info;
9711 asection *osec = o->output_section;
9713 if (symndx >= locsymcount
9714 || (elf_bad_symtab (input_bfd)
9715 && flinfo->sections[symndx] == NULL))
9717 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9718 while (h->root.type == bfd_link_hash_indirect
9719 || h->root.type == bfd_link_hash_warning)
9720 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9721 /* Arrange for symbol to be output. */
9723 elf_section_data (osec)->this_hdr.sh_info = -2;
9725 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9727 /* We'll use the output section target_index. */
9728 asection *sec = flinfo->sections[symndx]->output_section;
9729 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9733 if (flinfo->indices[symndx] == -1)
9735 /* Otherwise output the local symbol now. */
9736 Elf_Internal_Sym sym = isymbuf[symndx];
9737 asection *sec = flinfo->sections[symndx]->output_section;
9742 name = bfd_elf_string_from_elf_section (input_bfd,
9743 symtab_hdr->sh_link,
9748 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9750 if (sym.st_shndx == SHN_BAD)
9753 sym.st_value += o->output_offset;
9755 indx = bfd_get_symcount (output_bfd);
9756 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
9761 flinfo->indices[symndx] = indx;
9765 elf_section_data (osec)->this_hdr.sh_info
9766 = flinfo->indices[symndx];
9770 if ((o->flags & SEC_HAS_CONTENTS) == 0
9771 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9774 if ((o->flags & SEC_LINKER_CREATED) != 0)
9776 /* Section was created by _bfd_elf_link_create_dynamic_sections
9781 /* Get the contents of the section. They have been cached by a
9782 relaxation routine. Note that o is a section in an input
9783 file, so the contents field will not have been set by any of
9784 the routines which work on output files. */
9785 if (elf_section_data (o)->this_hdr.contents != NULL)
9787 contents = elf_section_data (o)->this_hdr.contents;
9788 if (bed->caches_rawsize
9790 && o->rawsize < o->size)
9792 memcpy (flinfo->contents, contents, o->rawsize);
9793 contents = flinfo->contents;
9798 contents = flinfo->contents;
9799 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
9803 if ((o->flags & SEC_RELOC) != 0)
9805 Elf_Internal_Rela *internal_relocs;
9806 Elf_Internal_Rela *rel, *relend;
9807 int action_discarded;
9810 /* Get the swapped relocs. */
9812 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
9813 flinfo->internal_relocs, FALSE);
9814 if (internal_relocs == NULL
9815 && o->reloc_count > 0)
9818 /* We need to reverse-copy input .ctors/.dtors sections if
9819 they are placed in .init_array/.finit_array for output. */
9820 if (o->size > address_size
9821 && ((strncmp (o->name, ".ctors", 6) == 0
9822 && strcmp (o->output_section->name,
9823 ".init_array") == 0)
9824 || (strncmp (o->name, ".dtors", 6) == 0
9825 && strcmp (o->output_section->name,
9826 ".fini_array") == 0))
9827 && (o->name[6] == 0 || o->name[6] == '.'))
9829 if (o->size != o->reloc_count * address_size)
9831 (*_bfd_error_handler)
9832 (_("error: %B: size of section %A is not "
9833 "multiple of address size"),
9835 bfd_set_error (bfd_error_on_input);
9838 o->flags |= SEC_ELF_REVERSE_COPY;
9841 action_discarded = -1;
9842 if (!elf_section_ignore_discarded_relocs (o))
9843 action_discarded = (*bed->action_discarded) (o);
9845 /* Run through the relocs evaluating complex reloc symbols and
9846 looking for relocs against symbols from discarded sections
9847 or section symbols from removed link-once sections.
9848 Complain about relocs against discarded sections. Zero
9849 relocs against removed link-once sections. */
9851 rel = internal_relocs;
9852 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9853 for ( ; rel < relend; rel++)
9855 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9856 unsigned int s_type;
9857 asection **ps, *sec;
9858 struct elf_link_hash_entry *h = NULL;
9859 const char *sym_name;
9861 if (r_symndx == STN_UNDEF)
9864 if (r_symndx >= locsymcount
9865 || (elf_bad_symtab (input_bfd)
9866 && flinfo->sections[r_symndx] == NULL))
9868 h = sym_hashes[r_symndx - extsymoff];
9870 /* Badly formatted input files can contain relocs that
9871 reference non-existant symbols. Check here so that
9872 we do not seg fault. */
9877 sprintf_vma (buffer, rel->r_info);
9878 (*_bfd_error_handler)
9879 (_("error: %B contains a reloc (0x%s) for section %A "
9880 "that references a non-existent global symbol"),
9881 input_bfd, o, buffer);
9882 bfd_set_error (bfd_error_bad_value);
9886 while (h->root.type == bfd_link_hash_indirect
9887 || h->root.type == bfd_link_hash_warning)
9888 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9892 /* If a plugin symbol is referenced from a non-IR file,
9893 mark the symbol as undefined. Note that the
9894 linker may attach linker created dynamic sections
9895 to the plugin bfd. Symbols defined in linker
9896 created sections are not plugin symbols. */
9897 if (h->root.non_ir_ref
9898 && (h->root.type == bfd_link_hash_defined
9899 || h->root.type == bfd_link_hash_defweak)
9900 && (h->root.u.def.section->flags
9901 & SEC_LINKER_CREATED) == 0
9902 && h->root.u.def.section->owner != NULL
9903 && (h->root.u.def.section->owner->flags
9906 h->root.type = bfd_link_hash_undefined;
9907 h->root.u.undef.abfd = h->root.u.def.section->owner;
9911 if (h->root.type == bfd_link_hash_defined
9912 || h->root.type == bfd_link_hash_defweak)
9913 ps = &h->root.u.def.section;
9915 sym_name = h->root.root.string;
9919 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9921 s_type = ELF_ST_TYPE (sym->st_info);
9922 ps = &flinfo->sections[r_symndx];
9923 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9927 if ((s_type == STT_RELC || s_type == STT_SRELC)
9928 && !flinfo->info->relocatable)
9931 bfd_vma dot = (rel->r_offset
9932 + o->output_offset + o->output_section->vma);
9934 printf ("Encountered a complex symbol!");
9935 printf (" (input_bfd %s, section %s, reloc %ld\n",
9936 input_bfd->filename, o->name,
9937 (long) (rel - internal_relocs));
9938 printf (" symbol: idx %8.8lx, name %s\n",
9939 r_symndx, sym_name);
9940 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9941 (unsigned long) rel->r_info,
9942 (unsigned long) rel->r_offset);
9944 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
9945 isymbuf, locsymcount, s_type == STT_SRELC))
9948 /* Symbol evaluated OK. Update to absolute value. */
9949 set_symbol_value (input_bfd, isymbuf, locsymcount,
9954 if (action_discarded != -1 && ps != NULL)
9956 /* Complain if the definition comes from a
9957 discarded section. */
9958 if ((sec = *ps) != NULL && discarded_section (sec))
9960 BFD_ASSERT (r_symndx != STN_UNDEF);
9961 if (action_discarded & COMPLAIN)
9962 (*flinfo->info->callbacks->einfo)
9963 (_("%X`%s' referenced in section `%A' of %B: "
9964 "defined in discarded section `%A' of %B\n"),
9965 sym_name, o, input_bfd, sec, sec->owner);
9967 /* Try to do the best we can to support buggy old
9968 versions of gcc. Pretend that the symbol is
9969 really defined in the kept linkonce section.
9970 FIXME: This is quite broken. Modifying the
9971 symbol here means we will be changing all later
9972 uses of the symbol, not just in this section. */
9973 if (action_discarded & PRETEND)
9977 kept = _bfd_elf_check_kept_section (sec,
9989 /* Relocate the section by invoking a back end routine.
9991 The back end routine is responsible for adjusting the
9992 section contents as necessary, and (if using Rela relocs
9993 and generating a relocatable output file) adjusting the
9994 reloc addend as necessary.
9996 The back end routine does not have to worry about setting
9997 the reloc address or the reloc symbol index.
9999 The back end routine is given a pointer to the swapped in
10000 internal symbols, and can access the hash table entries
10001 for the external symbols via elf_sym_hashes (input_bfd).
10003 When generating relocatable output, the back end routine
10004 must handle STB_LOCAL/STT_SECTION symbols specially. The
10005 output symbol is going to be a section symbol
10006 corresponding to the output section, which will require
10007 the addend to be adjusted. */
10009 ret = (*relocate_section) (output_bfd, flinfo->info,
10010 input_bfd, o, contents,
10018 || flinfo->info->relocatable
10019 || flinfo->info->emitrelocations)
10021 Elf_Internal_Rela *irela;
10022 Elf_Internal_Rela *irelaend, *irelamid;
10023 bfd_vma last_offset;
10024 struct elf_link_hash_entry **rel_hash;
10025 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10026 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10027 unsigned int next_erel;
10028 bfd_boolean rela_normal;
10029 struct bfd_elf_section_data *esdi, *esdo;
10031 esdi = elf_section_data (o);
10032 esdo = elf_section_data (o->output_section);
10033 rela_normal = FALSE;
10035 /* Adjust the reloc addresses and symbol indices. */
10037 irela = internal_relocs;
10038 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10039 rel_hash = esdo->rel.hashes + esdo->rel.count;
10040 /* We start processing the REL relocs, if any. When we reach
10041 IRELAMID in the loop, we switch to the RELA relocs. */
10043 if (esdi->rel.hdr != NULL)
10044 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10045 * bed->s->int_rels_per_ext_rel);
10046 rel_hash_list = rel_hash;
10047 rela_hash_list = NULL;
10048 last_offset = o->output_offset;
10049 if (!flinfo->info->relocatable)
10050 last_offset += o->output_section->vma;
10051 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10053 unsigned long r_symndx;
10055 Elf_Internal_Sym sym;
10057 if (next_erel == bed->s->int_rels_per_ext_rel)
10063 if (irela == irelamid)
10065 rel_hash = esdo->rela.hashes + esdo->rela.count;
10066 rela_hash_list = rel_hash;
10067 rela_normal = bed->rela_normal;
10070 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10073 if (irela->r_offset >= (bfd_vma) -2)
10075 /* This is a reloc for a deleted entry or somesuch.
10076 Turn it into an R_*_NONE reloc, at the same
10077 offset as the last reloc. elf_eh_frame.c and
10078 bfd_elf_discard_info rely on reloc offsets
10080 irela->r_offset = last_offset;
10082 irela->r_addend = 0;
10086 irela->r_offset += o->output_offset;
10088 /* Relocs in an executable have to be virtual addresses. */
10089 if (!flinfo->info->relocatable)
10090 irela->r_offset += o->output_section->vma;
10092 last_offset = irela->r_offset;
10094 r_symndx = irela->r_info >> r_sym_shift;
10095 if (r_symndx == STN_UNDEF)
10098 if (r_symndx >= locsymcount
10099 || (elf_bad_symtab (input_bfd)
10100 && flinfo->sections[r_symndx] == NULL))
10102 struct elf_link_hash_entry *rh;
10103 unsigned long indx;
10105 /* This is a reloc against a global symbol. We
10106 have not yet output all the local symbols, so
10107 we do not know the symbol index of any global
10108 symbol. We set the rel_hash entry for this
10109 reloc to point to the global hash table entry
10110 for this symbol. The symbol index is then
10111 set at the end of bfd_elf_final_link. */
10112 indx = r_symndx - extsymoff;
10113 rh = elf_sym_hashes (input_bfd)[indx];
10114 while (rh->root.type == bfd_link_hash_indirect
10115 || rh->root.type == bfd_link_hash_warning)
10116 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10118 /* Setting the index to -2 tells
10119 elf_link_output_extsym that this symbol is
10120 used by a reloc. */
10121 BFD_ASSERT (rh->indx < 0);
10129 /* This is a reloc against a local symbol. */
10132 sym = isymbuf[r_symndx];
10133 sec = flinfo->sections[r_symndx];
10134 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10136 /* I suppose the backend ought to fill in the
10137 section of any STT_SECTION symbol against a
10138 processor specific section. */
10139 r_symndx = STN_UNDEF;
10140 if (bfd_is_abs_section (sec))
10142 else if (sec == NULL || sec->owner == NULL)
10144 bfd_set_error (bfd_error_bad_value);
10149 asection *osec = sec->output_section;
10151 /* If we have discarded a section, the output
10152 section will be the absolute section. In
10153 case of discarded SEC_MERGE sections, use
10154 the kept section. relocate_section should
10155 have already handled discarded linkonce
10157 if (bfd_is_abs_section (osec)
10158 && sec->kept_section != NULL
10159 && sec->kept_section->output_section != NULL)
10161 osec = sec->kept_section->output_section;
10162 irela->r_addend -= osec->vma;
10165 if (!bfd_is_abs_section (osec))
10167 r_symndx = osec->target_index;
10168 if (r_symndx == STN_UNDEF)
10170 irela->r_addend += osec->vma;
10171 osec = _bfd_nearby_section (output_bfd, osec,
10173 irela->r_addend -= osec->vma;
10174 r_symndx = osec->target_index;
10179 /* Adjust the addend according to where the
10180 section winds up in the output section. */
10182 irela->r_addend += sec->output_offset;
10186 if (flinfo->indices[r_symndx] == -1)
10188 unsigned long shlink;
10193 if (flinfo->info->strip == strip_all)
10195 /* You can't do ld -r -s. */
10196 bfd_set_error (bfd_error_invalid_operation);
10200 /* This symbol was skipped earlier, but
10201 since it is needed by a reloc, we
10202 must output it now. */
10203 shlink = symtab_hdr->sh_link;
10204 name = (bfd_elf_string_from_elf_section
10205 (input_bfd, shlink, sym.st_name));
10209 osec = sec->output_section;
10211 _bfd_elf_section_from_bfd_section (output_bfd,
10213 if (sym.st_shndx == SHN_BAD)
10216 sym.st_value += sec->output_offset;
10217 if (!flinfo->info->relocatable)
10219 sym.st_value += osec->vma;
10220 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10222 /* STT_TLS symbols are relative to PT_TLS
10224 BFD_ASSERT (elf_hash_table (flinfo->info)
10225 ->tls_sec != NULL);
10226 sym.st_value -= (elf_hash_table (flinfo->info)
10231 indx = bfd_get_symcount (output_bfd);
10232 ret = elf_link_output_symstrtab (flinfo, name,
10238 flinfo->indices[r_symndx] = indx;
10243 r_symndx = flinfo->indices[r_symndx];
10246 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10247 | (irela->r_info & r_type_mask));
10250 /* Swap out the relocs. */
10251 input_rel_hdr = esdi->rel.hdr;
10252 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10254 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10259 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10260 * bed->s->int_rels_per_ext_rel);
10261 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10264 input_rela_hdr = esdi->rela.hdr;
10265 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10267 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10276 /* Write out the modified section contents. */
10277 if (bed->elf_backend_write_section
10278 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10281 /* Section written out. */
10283 else switch (o->sec_info_type)
10285 case SEC_INFO_TYPE_STABS:
10286 if (! (_bfd_write_section_stabs
10288 &elf_hash_table (flinfo->info)->stab_info,
10289 o, &elf_section_data (o)->sec_info, contents)))
10292 case SEC_INFO_TYPE_MERGE:
10293 if (! _bfd_write_merged_section (output_bfd, o,
10294 elf_section_data (o)->sec_info))
10297 case SEC_INFO_TYPE_EH_FRAME:
10299 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10304 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10306 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10314 /* FIXME: octets_per_byte. */
10315 if (! (o->flags & SEC_EXCLUDE))
10317 file_ptr offset = (file_ptr) o->output_offset;
10318 bfd_size_type todo = o->size;
10319 if ((o->flags & SEC_ELF_REVERSE_COPY))
10321 /* Reverse-copy input section to output. */
10324 todo -= address_size;
10325 if (! bfd_set_section_contents (output_bfd,
10333 offset += address_size;
10337 else if (! bfd_set_section_contents (output_bfd,
10351 /* Generate a reloc when linking an ELF file. This is a reloc
10352 requested by the linker, and does not come from any input file. This
10353 is used to build constructor and destructor tables when linking
10357 elf_reloc_link_order (bfd *output_bfd,
10358 struct bfd_link_info *info,
10359 asection *output_section,
10360 struct bfd_link_order *link_order)
10362 reloc_howto_type *howto;
10366 struct bfd_elf_section_reloc_data *reldata;
10367 struct elf_link_hash_entry **rel_hash_ptr;
10368 Elf_Internal_Shdr *rel_hdr;
10369 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10370 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10373 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10375 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10378 bfd_set_error (bfd_error_bad_value);
10382 addend = link_order->u.reloc.p->addend;
10385 reldata = &esdo->rel;
10386 else if (esdo->rela.hdr)
10387 reldata = &esdo->rela;
10394 /* Figure out the symbol index. */
10395 rel_hash_ptr = reldata->hashes + reldata->count;
10396 if (link_order->type == bfd_section_reloc_link_order)
10398 indx = link_order->u.reloc.p->u.section->target_index;
10399 BFD_ASSERT (indx != 0);
10400 *rel_hash_ptr = NULL;
10404 struct elf_link_hash_entry *h;
10406 /* Treat a reloc against a defined symbol as though it were
10407 actually against the section. */
10408 h = ((struct elf_link_hash_entry *)
10409 bfd_wrapped_link_hash_lookup (output_bfd, info,
10410 link_order->u.reloc.p->u.name,
10411 FALSE, FALSE, TRUE));
10413 && (h->root.type == bfd_link_hash_defined
10414 || h->root.type == bfd_link_hash_defweak))
10418 section = h->root.u.def.section;
10419 indx = section->output_section->target_index;
10420 *rel_hash_ptr = NULL;
10421 /* It seems that we ought to add the symbol value to the
10422 addend here, but in practice it has already been added
10423 because it was passed to constructor_callback. */
10424 addend += section->output_section->vma + section->output_offset;
10426 else if (h != NULL)
10428 /* Setting the index to -2 tells elf_link_output_extsym that
10429 this symbol is used by a reloc. */
10436 if (! ((*info->callbacks->unattached_reloc)
10437 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10443 /* If this is an inplace reloc, we must write the addend into the
10445 if (howto->partial_inplace && addend != 0)
10447 bfd_size_type size;
10448 bfd_reloc_status_type rstat;
10451 const char *sym_name;
10453 size = (bfd_size_type) bfd_get_reloc_size (howto);
10454 buf = (bfd_byte *) bfd_zmalloc (size);
10455 if (buf == NULL && size != 0)
10457 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10464 case bfd_reloc_outofrange:
10467 case bfd_reloc_overflow:
10468 if (link_order->type == bfd_section_reloc_link_order)
10469 sym_name = bfd_section_name (output_bfd,
10470 link_order->u.reloc.p->u.section);
10472 sym_name = link_order->u.reloc.p->u.name;
10473 if (! ((*info->callbacks->reloc_overflow)
10474 (info, NULL, sym_name, howto->name, addend, NULL,
10475 NULL, (bfd_vma) 0)))
10482 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10483 link_order->offset, size);
10489 /* The address of a reloc is relative to the section in a
10490 relocatable file, and is a virtual address in an executable
10492 offset = link_order->offset;
10493 if (! info->relocatable)
10494 offset += output_section->vma;
10496 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10498 irel[i].r_offset = offset;
10499 irel[i].r_info = 0;
10500 irel[i].r_addend = 0;
10502 if (bed->s->arch_size == 32)
10503 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10505 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10507 rel_hdr = reldata->hdr;
10508 erel = rel_hdr->contents;
10509 if (rel_hdr->sh_type == SHT_REL)
10511 erel += reldata->count * bed->s->sizeof_rel;
10512 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10516 irel[0].r_addend = addend;
10517 erel += reldata->count * bed->s->sizeof_rela;
10518 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10527 /* Get the output vma of the section pointed to by the sh_link field. */
10530 elf_get_linked_section_vma (struct bfd_link_order *p)
10532 Elf_Internal_Shdr **elf_shdrp;
10536 s = p->u.indirect.section;
10537 elf_shdrp = elf_elfsections (s->owner);
10538 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10539 elfsec = elf_shdrp[elfsec]->sh_link;
10541 The Intel C compiler generates SHT_IA_64_UNWIND with
10542 SHF_LINK_ORDER. But it doesn't set the sh_link or
10543 sh_info fields. Hence we could get the situation
10544 where elfsec is 0. */
10547 const struct elf_backend_data *bed
10548 = get_elf_backend_data (s->owner);
10549 if (bed->link_order_error_handler)
10550 bed->link_order_error_handler
10551 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10556 s = elf_shdrp[elfsec]->bfd_section;
10557 return s->output_section->vma + s->output_offset;
10562 /* Compare two sections based on the locations of the sections they are
10563 linked to. Used by elf_fixup_link_order. */
10566 compare_link_order (const void * a, const void * b)
10571 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10572 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10575 return apos > bpos;
10579 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10580 order as their linked sections. Returns false if this could not be done
10581 because an output section includes both ordered and unordered
10582 sections. Ideally we'd do this in the linker proper. */
10585 elf_fixup_link_order (bfd *abfd, asection *o)
10587 int seen_linkorder;
10590 struct bfd_link_order *p;
10592 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10594 struct bfd_link_order **sections;
10595 asection *s, *other_sec, *linkorder_sec;
10599 linkorder_sec = NULL;
10601 seen_linkorder = 0;
10602 for (p = o->map_head.link_order; p != NULL; p = p->next)
10604 if (p->type == bfd_indirect_link_order)
10606 s = p->u.indirect.section;
10608 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10609 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10610 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10611 && elfsec < elf_numsections (sub)
10612 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10613 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10627 if (seen_other && seen_linkorder)
10629 if (other_sec && linkorder_sec)
10630 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10632 linkorder_sec->owner, other_sec,
10635 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10637 bfd_set_error (bfd_error_bad_value);
10642 if (!seen_linkorder)
10645 sections = (struct bfd_link_order **)
10646 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10647 if (sections == NULL)
10649 seen_linkorder = 0;
10651 for (p = o->map_head.link_order; p != NULL; p = p->next)
10653 sections[seen_linkorder++] = p;
10655 /* Sort the input sections in the order of their linked section. */
10656 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10657 compare_link_order);
10659 /* Change the offsets of the sections. */
10661 for (n = 0; n < seen_linkorder; n++)
10663 s = sections[n]->u.indirect.section;
10664 offset &= ~(bfd_vma) 0 << s->alignment_power;
10665 s->output_offset = offset;
10666 sections[n]->offset = offset;
10667 /* FIXME: octets_per_byte. */
10668 offset += sections[n]->size;
10676 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10680 if (flinfo->symstrtab != NULL)
10681 _bfd_elf_strtab_free (flinfo->symstrtab);
10682 if (flinfo->contents != NULL)
10683 free (flinfo->contents);
10684 if (flinfo->external_relocs != NULL)
10685 free (flinfo->external_relocs);
10686 if (flinfo->internal_relocs != NULL)
10687 free (flinfo->internal_relocs);
10688 if (flinfo->external_syms != NULL)
10689 free (flinfo->external_syms);
10690 if (flinfo->locsym_shndx != NULL)
10691 free (flinfo->locsym_shndx);
10692 if (flinfo->internal_syms != NULL)
10693 free (flinfo->internal_syms);
10694 if (flinfo->indices != NULL)
10695 free (flinfo->indices);
10696 if (flinfo->sections != NULL)
10697 free (flinfo->sections);
10698 if (flinfo->symshndxbuf != NULL)
10699 free (flinfo->symshndxbuf);
10700 for (o = obfd->sections; o != NULL; o = o->next)
10702 struct bfd_elf_section_data *esdo = elf_section_data (o);
10703 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10704 free (esdo->rel.hashes);
10705 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10706 free (esdo->rela.hashes);
10710 /* Do the final step of an ELF link. */
10713 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10715 bfd_boolean dynamic;
10716 bfd_boolean emit_relocs;
10718 struct elf_final_link_info flinfo;
10720 struct bfd_link_order *p;
10722 bfd_size_type max_contents_size;
10723 bfd_size_type max_external_reloc_size;
10724 bfd_size_type max_internal_reloc_count;
10725 bfd_size_type max_sym_count;
10726 bfd_size_type max_sym_shndx_count;
10727 Elf_Internal_Sym elfsym;
10729 Elf_Internal_Shdr *symtab_hdr;
10730 Elf_Internal_Shdr *symtab_shndx_hdr;
10731 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10732 struct elf_outext_info eoinfo;
10733 bfd_boolean merged;
10734 size_t relativecount = 0;
10735 asection *reldyn = 0;
10737 asection *attr_section = NULL;
10738 bfd_vma attr_size = 0;
10739 const char *std_attrs_section;
10741 if (! is_elf_hash_table (info->hash))
10745 abfd->flags |= DYNAMIC;
10747 dynamic = elf_hash_table (info)->dynamic_sections_created;
10748 dynobj = elf_hash_table (info)->dynobj;
10750 emit_relocs = (info->relocatable
10751 || info->emitrelocations);
10753 flinfo.info = info;
10754 flinfo.output_bfd = abfd;
10755 flinfo.symstrtab = _bfd_elf_strtab_init ();
10756 if (flinfo.symstrtab == NULL)
10761 flinfo.dynsym_sec = NULL;
10762 flinfo.hash_sec = NULL;
10763 flinfo.symver_sec = NULL;
10767 flinfo.dynsym_sec = bfd_get_linker_section (dynobj, ".dynsym");
10768 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
10769 /* Note that dynsym_sec can be NULL (on VMS). */
10770 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
10771 /* Note that it is OK if symver_sec is NULL. */
10774 flinfo.contents = NULL;
10775 flinfo.external_relocs = NULL;
10776 flinfo.internal_relocs = NULL;
10777 flinfo.external_syms = NULL;
10778 flinfo.locsym_shndx = NULL;
10779 flinfo.internal_syms = NULL;
10780 flinfo.indices = NULL;
10781 flinfo.sections = NULL;
10782 flinfo.symshndxbuf = NULL;
10783 flinfo.filesym_count = 0;
10785 /* The object attributes have been merged. Remove the input
10786 sections from the link, and set the contents of the output
10788 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10789 for (o = abfd->sections; o != NULL; o = o->next)
10791 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10792 || strcmp (o->name, ".gnu.attributes") == 0)
10794 for (p = o->map_head.link_order; p != NULL; p = p->next)
10796 asection *input_section;
10798 if (p->type != bfd_indirect_link_order)
10800 input_section = p->u.indirect.section;
10801 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10802 elf_link_input_bfd ignores this section. */
10803 input_section->flags &= ~SEC_HAS_CONTENTS;
10806 attr_size = bfd_elf_obj_attr_size (abfd);
10809 bfd_set_section_size (abfd, o, attr_size);
10811 /* Skip this section later on. */
10812 o->map_head.link_order = NULL;
10815 o->flags |= SEC_EXCLUDE;
10819 /* Count up the number of relocations we will output for each output
10820 section, so that we know the sizes of the reloc sections. We
10821 also figure out some maximum sizes. */
10822 max_contents_size = 0;
10823 max_external_reloc_size = 0;
10824 max_internal_reloc_count = 0;
10826 max_sym_shndx_count = 0;
10828 for (o = abfd->sections; o != NULL; o = o->next)
10830 struct bfd_elf_section_data *esdo = elf_section_data (o);
10831 o->reloc_count = 0;
10833 for (p = o->map_head.link_order; p != NULL; p = p->next)
10835 unsigned int reloc_count = 0;
10836 struct bfd_elf_section_data *esdi = NULL;
10838 if (p->type == bfd_section_reloc_link_order
10839 || p->type == bfd_symbol_reloc_link_order)
10841 else if (p->type == bfd_indirect_link_order)
10845 sec = p->u.indirect.section;
10846 esdi = elf_section_data (sec);
10848 /* Mark all sections which are to be included in the
10849 link. This will normally be every section. We need
10850 to do this so that we can identify any sections which
10851 the linker has decided to not include. */
10852 sec->linker_mark = TRUE;
10854 if (sec->flags & SEC_MERGE)
10857 if (esdo->this_hdr.sh_type == SHT_REL
10858 || esdo->this_hdr.sh_type == SHT_RELA)
10859 /* Some backends use reloc_count in relocation sections
10860 to count particular types of relocs. Of course,
10861 reloc sections themselves can't have relocations. */
10863 else if (info->relocatable || info->emitrelocations)
10864 reloc_count = sec->reloc_count;
10865 else if (bed->elf_backend_count_relocs)
10866 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
10868 if (sec->rawsize > max_contents_size)
10869 max_contents_size = sec->rawsize;
10870 if (sec->size > max_contents_size)
10871 max_contents_size = sec->size;
10873 /* We are interested in just local symbols, not all
10875 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10876 && (sec->owner->flags & DYNAMIC) == 0)
10880 if (elf_bad_symtab (sec->owner))
10881 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10882 / bed->s->sizeof_sym);
10884 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10886 if (sym_count > max_sym_count)
10887 max_sym_count = sym_count;
10889 if (sym_count > max_sym_shndx_count
10890 && elf_symtab_shndx (sec->owner) != 0)
10891 max_sym_shndx_count = sym_count;
10893 if ((sec->flags & SEC_RELOC) != 0)
10895 size_t ext_size = 0;
10897 if (esdi->rel.hdr != NULL)
10898 ext_size = esdi->rel.hdr->sh_size;
10899 if (esdi->rela.hdr != NULL)
10900 ext_size += esdi->rela.hdr->sh_size;
10902 if (ext_size > max_external_reloc_size)
10903 max_external_reloc_size = ext_size;
10904 if (sec->reloc_count > max_internal_reloc_count)
10905 max_internal_reloc_count = sec->reloc_count;
10910 if (reloc_count == 0)
10913 o->reloc_count += reloc_count;
10915 if (p->type == bfd_indirect_link_order
10916 && (info->relocatable || info->emitrelocations))
10919 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
10920 if (esdi->rela.hdr)
10921 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
10926 esdo->rela.count += reloc_count;
10928 esdo->rel.count += reloc_count;
10932 if (o->reloc_count > 0)
10933 o->flags |= SEC_RELOC;
10936 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10937 set it (this is probably a bug) and if it is set
10938 assign_section_numbers will create a reloc section. */
10939 o->flags &=~ SEC_RELOC;
10942 /* If the SEC_ALLOC flag is not set, force the section VMA to
10943 zero. This is done in elf_fake_sections as well, but forcing
10944 the VMA to 0 here will ensure that relocs against these
10945 sections are handled correctly. */
10946 if ((o->flags & SEC_ALLOC) == 0
10947 && ! o->user_set_vma)
10951 if (! info->relocatable && merged)
10952 elf_link_hash_traverse (elf_hash_table (info),
10953 _bfd_elf_link_sec_merge_syms, abfd);
10955 /* Figure out the file positions for everything but the symbol table
10956 and the relocs. We set symcount to force assign_section_numbers
10957 to create a symbol table. */
10958 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
10959 BFD_ASSERT (! abfd->output_has_begun);
10960 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10963 /* Set sizes, and assign file positions for reloc sections. */
10964 for (o = abfd->sections; o != NULL; o = o->next)
10966 struct bfd_elf_section_data *esdo = elf_section_data (o);
10967 if ((o->flags & SEC_RELOC) != 0)
10970 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
10974 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
10978 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10979 to count upwards while actually outputting the relocations. */
10980 esdo->rel.count = 0;
10981 esdo->rela.count = 0;
10983 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
10985 /* Cache the section contents so that they can be compressed
10986 later. Use bfd_malloc since it will be freed by
10987 bfd_compress_section_contents. */
10988 unsigned char *contents = esdo->this_hdr.contents;
10989 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
10992 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
10993 if (contents == NULL)
10995 esdo->this_hdr.contents = contents;
10999 /* We have now assigned file positions for all the sections except
11000 .symtab, .strtab, and non-loaded reloc sections. We start the
11001 .symtab section at the current file position, and write directly
11002 to it. We build the .strtab section in memory. */
11003 bfd_get_symcount (abfd) = 0;
11004 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11005 /* sh_name is set in prep_headers. */
11006 symtab_hdr->sh_type = SHT_SYMTAB;
11007 /* sh_flags, sh_addr and sh_size all start off zero. */
11008 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11009 /* sh_link is set in assign_section_numbers. */
11010 /* sh_info is set below. */
11011 /* sh_offset is set just below. */
11012 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11014 if (max_sym_count < 20)
11015 max_sym_count = 20;
11016 elf_hash_table (info)->strtabsize = max_sym_count;
11017 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11018 elf_hash_table (info)->strtab
11019 = (struct elf_sym_strtab *) bfd_malloc (amt);
11020 if (elf_hash_table (info)->strtab == NULL)
11022 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11024 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11025 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11027 if (info->strip != strip_all || emit_relocs)
11029 file_ptr off = elf_next_file_pos (abfd);
11031 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11033 /* Note that at this point elf_next_file_pos (abfd) is
11034 incorrect. We do not yet know the size of the .symtab section.
11035 We correct next_file_pos below, after we do know the size. */
11037 /* Start writing out the symbol table. The first symbol is always a
11039 elfsym.st_value = 0;
11040 elfsym.st_size = 0;
11041 elfsym.st_info = 0;
11042 elfsym.st_other = 0;
11043 elfsym.st_shndx = SHN_UNDEF;
11044 elfsym.st_target_internal = 0;
11045 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11046 bfd_und_section_ptr, NULL) != 1)
11049 /* Output a symbol for each section. We output these even if we are
11050 discarding local symbols, since they are used for relocs. These
11051 symbols have no names. We store the index of each one in the
11052 index field of the section, so that we can find it again when
11053 outputting relocs. */
11055 elfsym.st_size = 0;
11056 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11057 elfsym.st_other = 0;
11058 elfsym.st_value = 0;
11059 elfsym.st_target_internal = 0;
11060 for (i = 1; i < elf_numsections (abfd); i++)
11062 o = bfd_section_from_elf_index (abfd, i);
11065 o->target_index = bfd_get_symcount (abfd);
11066 elfsym.st_shndx = i;
11067 if (!info->relocatable)
11068 elfsym.st_value = o->vma;
11069 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11076 /* Allocate some memory to hold information read in from the input
11078 if (max_contents_size != 0)
11080 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11081 if (flinfo.contents == NULL)
11085 if (max_external_reloc_size != 0)
11087 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11088 if (flinfo.external_relocs == NULL)
11092 if (max_internal_reloc_count != 0)
11094 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11095 amt *= sizeof (Elf_Internal_Rela);
11096 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11097 if (flinfo.internal_relocs == NULL)
11101 if (max_sym_count != 0)
11103 amt = max_sym_count * bed->s->sizeof_sym;
11104 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11105 if (flinfo.external_syms == NULL)
11108 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11109 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11110 if (flinfo.internal_syms == NULL)
11113 amt = max_sym_count * sizeof (long);
11114 flinfo.indices = (long int *) bfd_malloc (amt);
11115 if (flinfo.indices == NULL)
11118 amt = max_sym_count * sizeof (asection *);
11119 flinfo.sections = (asection **) bfd_malloc (amt);
11120 if (flinfo.sections == NULL)
11124 if (max_sym_shndx_count != 0)
11126 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11127 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11128 if (flinfo.locsym_shndx == NULL)
11132 if (elf_hash_table (info)->tls_sec)
11134 bfd_vma base, end = 0;
11137 for (sec = elf_hash_table (info)->tls_sec;
11138 sec && (sec->flags & SEC_THREAD_LOCAL);
11141 bfd_size_type size = sec->size;
11144 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11146 struct bfd_link_order *ord = sec->map_tail.link_order;
11149 size = ord->offset + ord->size;
11151 end = sec->vma + size;
11153 base = elf_hash_table (info)->tls_sec->vma;
11154 /* Only align end of TLS section if static TLS doesn't have special
11155 alignment requirements. */
11156 if (bed->static_tls_alignment == 1)
11157 end = align_power (end,
11158 elf_hash_table (info)->tls_sec->alignment_power);
11159 elf_hash_table (info)->tls_size = end - base;
11162 /* Reorder SHF_LINK_ORDER sections. */
11163 for (o = abfd->sections; o != NULL; o = o->next)
11165 if (!elf_fixup_link_order (abfd, o))
11169 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11172 /* Since ELF permits relocations to be against local symbols, we
11173 must have the local symbols available when we do the relocations.
11174 Since we would rather only read the local symbols once, and we
11175 would rather not keep them in memory, we handle all the
11176 relocations for a single input file at the same time.
11178 Unfortunately, there is no way to know the total number of local
11179 symbols until we have seen all of them, and the local symbol
11180 indices precede the global symbol indices. This means that when
11181 we are generating relocatable output, and we see a reloc against
11182 a global symbol, we can not know the symbol index until we have
11183 finished examining all the local symbols to see which ones we are
11184 going to output. To deal with this, we keep the relocations in
11185 memory, and don't output them until the end of the link. This is
11186 an unfortunate waste of memory, but I don't see a good way around
11187 it. Fortunately, it only happens when performing a relocatable
11188 link, which is not the common case. FIXME: If keep_memory is set
11189 we could write the relocs out and then read them again; I don't
11190 know how bad the memory loss will be. */
11192 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11193 sub->output_has_begun = FALSE;
11194 for (o = abfd->sections; o != NULL; o = o->next)
11196 for (p = o->map_head.link_order; p != NULL; p = p->next)
11198 if (p->type == bfd_indirect_link_order
11199 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11200 == bfd_target_elf_flavour)
11201 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11203 if (! sub->output_has_begun)
11205 if (! elf_link_input_bfd (&flinfo, sub))
11207 sub->output_has_begun = TRUE;
11210 else if (p->type == bfd_section_reloc_link_order
11211 || p->type == bfd_symbol_reloc_link_order)
11213 if (! elf_reloc_link_order (abfd, info, o, p))
11218 if (! _bfd_default_link_order (abfd, info, o, p))
11220 if (p->type == bfd_indirect_link_order
11221 && (bfd_get_flavour (sub)
11222 == bfd_target_elf_flavour)
11223 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11224 != bed->s->elfclass))
11226 const char *iclass, *oclass;
11228 if (bed->s->elfclass == ELFCLASS64)
11230 iclass = "ELFCLASS32";
11231 oclass = "ELFCLASS64";
11235 iclass = "ELFCLASS64";
11236 oclass = "ELFCLASS32";
11239 bfd_set_error (bfd_error_wrong_format);
11240 (*_bfd_error_handler)
11241 (_("%B: file class %s incompatible with %s"),
11242 sub, iclass, oclass);
11251 /* Free symbol buffer if needed. */
11252 if (!info->reduce_memory_overheads)
11254 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11255 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11256 && elf_tdata (sub)->symbuf)
11258 free (elf_tdata (sub)->symbuf);
11259 elf_tdata (sub)->symbuf = NULL;
11263 /* Output any global symbols that got converted to local in a
11264 version script or due to symbol visibility. We do this in a
11265 separate step since ELF requires all local symbols to appear
11266 prior to any global symbols. FIXME: We should only do this if
11267 some global symbols were, in fact, converted to become local.
11268 FIXME: Will this work correctly with the Irix 5 linker? */
11269 eoinfo.failed = FALSE;
11270 eoinfo.flinfo = &flinfo;
11271 eoinfo.localsyms = TRUE;
11272 eoinfo.file_sym_done = FALSE;
11273 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11277 /* If backend needs to output some local symbols not present in the hash
11278 table, do it now. */
11279 if (bed->elf_backend_output_arch_local_syms
11280 && (info->strip != strip_all || emit_relocs))
11282 typedef int (*out_sym_func)
11283 (void *, const char *, Elf_Internal_Sym *, asection *,
11284 struct elf_link_hash_entry *);
11286 if (! ((*bed->elf_backend_output_arch_local_syms)
11287 (abfd, info, &flinfo,
11288 (out_sym_func) elf_link_output_symstrtab)))
11292 /* That wrote out all the local symbols. Finish up the symbol table
11293 with the global symbols. Even if we want to strip everything we
11294 can, we still need to deal with those global symbols that got
11295 converted to local in a version script. */
11297 /* The sh_info field records the index of the first non local symbol. */
11298 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11301 && flinfo.dynsym_sec != NULL
11302 && flinfo.dynsym_sec->output_section != bfd_abs_section_ptr)
11304 Elf_Internal_Sym sym;
11305 bfd_byte *dynsym = flinfo.dynsym_sec->contents;
11306 long last_local = 0;
11308 /* Write out the section symbols for the output sections. */
11309 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
11315 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11317 sym.st_target_internal = 0;
11319 for (s = abfd->sections; s != NULL; s = s->next)
11325 dynindx = elf_section_data (s)->dynindx;
11328 indx = elf_section_data (s)->this_idx;
11329 BFD_ASSERT (indx > 0);
11330 sym.st_shndx = indx;
11331 if (! check_dynsym (abfd, &sym))
11333 sym.st_value = s->vma;
11334 dest = dynsym + dynindx * bed->s->sizeof_sym;
11335 if (last_local < dynindx)
11336 last_local = dynindx;
11337 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11341 /* Write out the local dynsyms. */
11342 if (elf_hash_table (info)->dynlocal)
11344 struct elf_link_local_dynamic_entry *e;
11345 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11350 /* Copy the internal symbol and turn off visibility.
11351 Note that we saved a word of storage and overwrote
11352 the original st_name with the dynstr_index. */
11354 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11356 s = bfd_section_from_elf_index (e->input_bfd,
11361 elf_section_data (s->output_section)->this_idx;
11362 if (! check_dynsym (abfd, &sym))
11364 sym.st_value = (s->output_section->vma
11366 + e->isym.st_value);
11369 if (last_local < e->dynindx)
11370 last_local = e->dynindx;
11372 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11373 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11377 elf_section_data (flinfo.dynsym_sec->output_section)->this_hdr.sh_info =
11381 /* We get the global symbols from the hash table. */
11382 eoinfo.failed = FALSE;
11383 eoinfo.localsyms = FALSE;
11384 eoinfo.flinfo = &flinfo;
11385 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11389 /* If backend needs to output some symbols not present in the hash
11390 table, do it now. */
11391 if (bed->elf_backend_output_arch_syms
11392 && (info->strip != strip_all || emit_relocs))
11394 typedef int (*out_sym_func)
11395 (void *, const char *, Elf_Internal_Sym *, asection *,
11396 struct elf_link_hash_entry *);
11398 if (! ((*bed->elf_backend_output_arch_syms)
11399 (abfd, info, &flinfo,
11400 (out_sym_func) elf_link_output_symstrtab)))
11404 /* Finalize the .strtab section. */
11405 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11407 /* Swap out the .strtab section. */
11408 if (!elf_link_swap_symbols_out (&flinfo))
11411 /* Now we know the size of the symtab section. */
11412 if (bfd_get_symcount (abfd) > 0)
11414 /* Finish up and write out the symbol string table (.strtab)
11416 Elf_Internal_Shdr *symstrtab_hdr;
11417 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11419 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
11420 if (symtab_shndx_hdr->sh_name != 0)
11422 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11423 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11424 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11425 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11426 symtab_shndx_hdr->sh_size = amt;
11428 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11431 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11432 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11436 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11437 /* sh_name was set in prep_headers. */
11438 symstrtab_hdr->sh_type = SHT_STRTAB;
11439 symstrtab_hdr->sh_flags = 0;
11440 symstrtab_hdr->sh_addr = 0;
11441 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11442 symstrtab_hdr->sh_entsize = 0;
11443 symstrtab_hdr->sh_link = 0;
11444 symstrtab_hdr->sh_info = 0;
11445 /* sh_offset is set just below. */
11446 symstrtab_hdr->sh_addralign = 1;
11448 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11450 elf_next_file_pos (abfd) = off;
11452 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11453 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11457 /* Adjust the relocs to have the correct symbol indices. */
11458 for (o = abfd->sections; o != NULL; o = o->next)
11460 struct bfd_elf_section_data *esdo = elf_section_data (o);
11462 if ((o->flags & SEC_RELOC) == 0)
11465 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11466 if (esdo->rel.hdr != NULL)
11467 elf_link_adjust_relocs (abfd, &esdo->rel, sort);
11468 if (esdo->rela.hdr != NULL)
11469 elf_link_adjust_relocs (abfd, &esdo->rela, sort);
11471 /* Set the reloc_count field to 0 to prevent write_relocs from
11472 trying to swap the relocs out itself. */
11473 o->reloc_count = 0;
11476 if (dynamic && info->combreloc && dynobj != NULL)
11477 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11479 /* If we are linking against a dynamic object, or generating a
11480 shared library, finish up the dynamic linking information. */
11483 bfd_byte *dyncon, *dynconend;
11485 /* Fix up .dynamic entries. */
11486 o = bfd_get_linker_section (dynobj, ".dynamic");
11487 BFD_ASSERT (o != NULL);
11489 dyncon = o->contents;
11490 dynconend = o->contents + o->size;
11491 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11493 Elf_Internal_Dyn dyn;
11497 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11504 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11506 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11508 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11509 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11512 dyn.d_un.d_val = relativecount;
11519 name = info->init_function;
11522 name = info->fini_function;
11525 struct elf_link_hash_entry *h;
11527 h = elf_link_hash_lookup (elf_hash_table (info), name,
11528 FALSE, FALSE, TRUE);
11530 && (h->root.type == bfd_link_hash_defined
11531 || h->root.type == bfd_link_hash_defweak))
11533 dyn.d_un.d_ptr = h->root.u.def.value;
11534 o = h->root.u.def.section;
11535 if (o->output_section != NULL)
11536 dyn.d_un.d_ptr += (o->output_section->vma
11537 + o->output_offset);
11540 /* The symbol is imported from another shared
11541 library and does not apply to this one. */
11542 dyn.d_un.d_ptr = 0;
11549 case DT_PREINIT_ARRAYSZ:
11550 name = ".preinit_array";
11552 case DT_INIT_ARRAYSZ:
11553 name = ".init_array";
11555 case DT_FINI_ARRAYSZ:
11556 name = ".fini_array";
11558 o = bfd_get_section_by_name (abfd, name);
11561 (*_bfd_error_handler)
11562 (_("%B: could not find output section %s"), abfd, name);
11566 (*_bfd_error_handler)
11567 (_("warning: %s section has zero size"), name);
11568 dyn.d_un.d_val = o->size;
11571 case DT_PREINIT_ARRAY:
11572 name = ".preinit_array";
11574 case DT_INIT_ARRAY:
11575 name = ".init_array";
11577 case DT_FINI_ARRAY:
11578 name = ".fini_array";
11585 name = ".gnu.hash";
11594 name = ".gnu.version_d";
11597 name = ".gnu.version_r";
11600 name = ".gnu.version";
11602 o = bfd_get_section_by_name (abfd, name);
11605 (*_bfd_error_handler)
11606 (_("%B: could not find output section %s"), abfd, name);
11609 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11611 (*_bfd_error_handler)
11612 (_("warning: section '%s' is being made into a note"), name);
11613 bfd_set_error (bfd_error_nonrepresentable_section);
11616 dyn.d_un.d_ptr = o->vma;
11623 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11627 dyn.d_un.d_val = 0;
11628 dyn.d_un.d_ptr = 0;
11629 for (i = 1; i < elf_numsections (abfd); i++)
11631 Elf_Internal_Shdr *hdr;
11633 hdr = elf_elfsections (abfd)[i];
11634 if (hdr->sh_type == type
11635 && (hdr->sh_flags & SHF_ALLOC) != 0)
11637 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11638 dyn.d_un.d_val += hdr->sh_size;
11641 if (dyn.d_un.d_ptr == 0
11642 || hdr->sh_addr < dyn.d_un.d_ptr)
11643 dyn.d_un.d_ptr = hdr->sh_addr;
11649 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11653 /* If we have created any dynamic sections, then output them. */
11654 if (dynobj != NULL)
11656 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11659 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11660 if (((info->warn_shared_textrel && info->shared)
11661 || info->error_textrel)
11662 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
11664 bfd_byte *dyncon, *dynconend;
11666 dyncon = o->contents;
11667 dynconend = o->contents + o->size;
11668 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11670 Elf_Internal_Dyn dyn;
11672 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11674 if (dyn.d_tag == DT_TEXTREL)
11676 if (info->error_textrel)
11677 info->callbacks->einfo
11678 (_("%P%X: read-only segment has dynamic relocations.\n"));
11680 info->callbacks->einfo
11681 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11687 for (o = dynobj->sections; o != NULL; o = o->next)
11689 if ((o->flags & SEC_HAS_CONTENTS) == 0
11691 || o->output_section == bfd_abs_section_ptr)
11693 if ((o->flags & SEC_LINKER_CREATED) == 0)
11695 /* At this point, we are only interested in sections
11696 created by _bfd_elf_link_create_dynamic_sections. */
11699 if (elf_hash_table (info)->stab_info.stabstr == o)
11701 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11703 if (strcmp (o->name, ".dynstr") != 0)
11705 /* FIXME: octets_per_byte. */
11706 if (! bfd_set_section_contents (abfd, o->output_section,
11708 (file_ptr) o->output_offset,
11714 /* The contents of the .dynstr section are actually in a
11718 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11719 if (bfd_seek (abfd, off, SEEK_SET) != 0
11720 || ! _bfd_elf_strtab_emit (abfd,
11721 elf_hash_table (info)->dynstr))
11727 if (info->relocatable)
11729 bfd_boolean failed = FALSE;
11731 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11736 /* If we have optimized stabs strings, output them. */
11737 if (elf_hash_table (info)->stab_info.stabstr != NULL)
11739 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11743 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11746 elf_final_link_free (abfd, &flinfo);
11748 elf_linker (abfd) = TRUE;
11752 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
11753 if (contents == NULL)
11754 return FALSE; /* Bail out and fail. */
11755 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11756 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11763 elf_final_link_free (abfd, &flinfo);
11767 /* Initialize COOKIE for input bfd ABFD. */
11770 init_reloc_cookie (struct elf_reloc_cookie *cookie,
11771 struct bfd_link_info *info, bfd *abfd)
11773 Elf_Internal_Shdr *symtab_hdr;
11774 const struct elf_backend_data *bed;
11776 bed = get_elf_backend_data (abfd);
11777 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11779 cookie->abfd = abfd;
11780 cookie->sym_hashes = elf_sym_hashes (abfd);
11781 cookie->bad_symtab = elf_bad_symtab (abfd);
11782 if (cookie->bad_symtab)
11784 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11785 cookie->extsymoff = 0;
11789 cookie->locsymcount = symtab_hdr->sh_info;
11790 cookie->extsymoff = symtab_hdr->sh_info;
11793 if (bed->s->arch_size == 32)
11794 cookie->r_sym_shift = 8;
11796 cookie->r_sym_shift = 32;
11798 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11799 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11801 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11802 cookie->locsymcount, 0,
11804 if (cookie->locsyms == NULL)
11806 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11809 if (info->keep_memory)
11810 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11815 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11818 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11820 Elf_Internal_Shdr *symtab_hdr;
11822 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11823 if (cookie->locsyms != NULL
11824 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11825 free (cookie->locsyms);
11828 /* Initialize the relocation information in COOKIE for input section SEC
11829 of input bfd ABFD. */
11832 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11833 struct bfd_link_info *info, bfd *abfd,
11836 const struct elf_backend_data *bed;
11838 if (sec->reloc_count == 0)
11840 cookie->rels = NULL;
11841 cookie->relend = NULL;
11845 bed = get_elf_backend_data (abfd);
11847 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11848 info->keep_memory);
11849 if (cookie->rels == NULL)
11851 cookie->rel = cookie->rels;
11852 cookie->relend = (cookie->rels
11853 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11855 cookie->rel = cookie->rels;
11859 /* Free the memory allocated by init_reloc_cookie_rels,
11863 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11866 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11867 free (cookie->rels);
11870 /* Initialize the whole of COOKIE for input section SEC. */
11873 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11874 struct bfd_link_info *info,
11877 if (!init_reloc_cookie (cookie, info, sec->owner))
11879 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11884 fini_reloc_cookie (cookie, sec->owner);
11889 /* Free the memory allocated by init_reloc_cookie_for_section,
11893 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11896 fini_reloc_cookie_rels (cookie, sec);
11897 fini_reloc_cookie (cookie, sec->owner);
11900 /* Garbage collect unused sections. */
11902 /* Default gc_mark_hook. */
11905 _bfd_elf_gc_mark_hook (asection *sec,
11906 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11907 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11908 struct elf_link_hash_entry *h,
11909 Elf_Internal_Sym *sym)
11911 const char *sec_name;
11915 switch (h->root.type)
11917 case bfd_link_hash_defined:
11918 case bfd_link_hash_defweak:
11919 return h->root.u.def.section;
11921 case bfd_link_hash_common:
11922 return h->root.u.c.p->section;
11924 case bfd_link_hash_undefined:
11925 case bfd_link_hash_undefweak:
11926 /* To work around a glibc bug, keep all XXX input sections
11927 when there is an as yet undefined reference to __start_XXX
11928 or __stop_XXX symbols. The linker will later define such
11929 symbols for orphan input sections that have a name
11930 representable as a C identifier. */
11931 if (strncmp (h->root.root.string, "__start_", 8) == 0)
11932 sec_name = h->root.root.string + 8;
11933 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
11934 sec_name = h->root.root.string + 7;
11938 if (sec_name && *sec_name != '\0')
11942 for (i = info->input_bfds; i; i = i->link.next)
11944 sec = bfd_get_section_by_name (i, sec_name);
11946 sec->flags |= SEC_KEEP;
11956 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11961 /* COOKIE->rel describes a relocation against section SEC, which is
11962 a section we've decided to keep. Return the section that contains
11963 the relocation symbol, or NULL if no section contains it. */
11966 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11967 elf_gc_mark_hook_fn gc_mark_hook,
11968 struct elf_reloc_cookie *cookie)
11970 unsigned long r_symndx;
11971 struct elf_link_hash_entry *h;
11973 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
11974 if (r_symndx == STN_UNDEF)
11977 if (r_symndx >= cookie->locsymcount
11978 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11980 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11983 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
11987 while (h->root.type == bfd_link_hash_indirect
11988 || h->root.type == bfd_link_hash_warning)
11989 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11991 /* If this symbol is weak and there is a non-weak definition, we
11992 keep the non-weak definition because many backends put
11993 dynamic reloc info on the non-weak definition for code
11994 handling copy relocs. */
11995 if (h->u.weakdef != NULL)
11996 h->u.weakdef->mark = 1;
11997 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12000 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12001 &cookie->locsyms[r_symndx]);
12004 /* COOKIE->rel describes a relocation against section SEC, which is
12005 a section we've decided to keep. Mark the section that contains
12006 the relocation symbol. */
12009 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12011 elf_gc_mark_hook_fn gc_mark_hook,
12012 struct elf_reloc_cookie *cookie)
12016 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
12017 if (rsec && !rsec->gc_mark)
12019 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12020 || (rsec->owner->flags & DYNAMIC) != 0)
12022 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12028 /* The mark phase of garbage collection. For a given section, mark
12029 it and any sections in this section's group, and all the sections
12030 which define symbols to which it refers. */
12033 _bfd_elf_gc_mark (struct bfd_link_info *info,
12035 elf_gc_mark_hook_fn gc_mark_hook)
12038 asection *group_sec, *eh_frame;
12042 /* Mark all the sections in the group. */
12043 group_sec = elf_section_data (sec)->next_in_group;
12044 if (group_sec && !group_sec->gc_mark)
12045 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12048 /* Look through the section relocs. */
12050 eh_frame = elf_eh_frame_section (sec->owner);
12051 if ((sec->flags & SEC_RELOC) != 0
12052 && sec->reloc_count > 0
12053 && sec != eh_frame)
12055 struct elf_reloc_cookie cookie;
12057 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12061 for (; cookie.rel < cookie.relend; cookie.rel++)
12062 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12067 fini_reloc_cookie_for_section (&cookie, sec);
12071 if (ret && eh_frame && elf_fde_list (sec))
12073 struct elf_reloc_cookie cookie;
12075 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12079 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12080 gc_mark_hook, &cookie))
12082 fini_reloc_cookie_for_section (&cookie, eh_frame);
12086 eh_frame = elf_section_eh_frame_entry (sec);
12087 if (ret && eh_frame && !eh_frame->gc_mark)
12088 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12094 /* Scan and mark sections in a special or debug section group. */
12097 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12099 /* Point to first section of section group. */
12101 /* Used to iterate the section group. */
12104 bfd_boolean is_special_grp = TRUE;
12105 bfd_boolean is_debug_grp = TRUE;
12107 /* First scan to see if group contains any section other than debug
12108 and special section. */
12109 ssec = msec = elf_next_in_group (grp);
12112 if ((msec->flags & SEC_DEBUGGING) == 0)
12113 is_debug_grp = FALSE;
12115 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12116 is_special_grp = FALSE;
12118 msec = elf_next_in_group (msec);
12120 while (msec != ssec);
12122 /* If this is a pure debug section group or pure special section group,
12123 keep all sections in this group. */
12124 if (is_debug_grp || is_special_grp)
12129 msec = elf_next_in_group (msec);
12131 while (msec != ssec);
12135 /* Keep debug and special sections. */
12138 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12139 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12143 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12146 bfd_boolean some_kept;
12147 bfd_boolean debug_frag_seen;
12149 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12152 /* Ensure all linker created sections are kept,
12153 see if any other section is already marked,
12154 and note if we have any fragmented debug sections. */
12155 debug_frag_seen = some_kept = FALSE;
12156 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12158 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12160 else if (isec->gc_mark)
12163 if (debug_frag_seen == FALSE
12164 && (isec->flags & SEC_DEBUGGING)
12165 && CONST_STRNEQ (isec->name, ".debug_line."))
12166 debug_frag_seen = TRUE;
12169 /* If no section in this file will be kept, then we can
12170 toss out the debug and special sections. */
12174 /* Keep debug and special sections like .comment when they are
12175 not part of a group. Also keep section groups that contain
12176 just debug sections or special sections. */
12177 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12179 if ((isec->flags & SEC_GROUP) != 0)
12180 _bfd_elf_gc_mark_debug_special_section_group (isec);
12181 else if (((isec->flags & SEC_DEBUGGING) != 0
12182 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12183 && elf_next_in_group (isec) == NULL)
12187 if (! debug_frag_seen)
12190 /* Look for CODE sections which are going to be discarded,
12191 and find and discard any fragmented debug sections which
12192 are associated with that code section. */
12193 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12194 if ((isec->flags & SEC_CODE) != 0
12195 && isec->gc_mark == 0)
12200 ilen = strlen (isec->name);
12202 /* Association is determined by the name of the debug section
12203 containing the name of the code section as a suffix. For
12204 example .debug_line.text.foo is a debug section associated
12206 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12210 if (dsec->gc_mark == 0
12211 || (dsec->flags & SEC_DEBUGGING) == 0)
12214 dlen = strlen (dsec->name);
12217 && strncmp (dsec->name + (dlen - ilen),
12218 isec->name, ilen) == 0)
12228 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12230 struct elf_gc_sweep_symbol_info
12232 struct bfd_link_info *info;
12233 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12238 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12241 && (((h->root.type == bfd_link_hash_defined
12242 || h->root.type == bfd_link_hash_defweak)
12243 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12244 && h->root.u.def.section->gc_mark))
12245 || h->root.type == bfd_link_hash_undefined
12246 || h->root.type == bfd_link_hash_undefweak))
12248 struct elf_gc_sweep_symbol_info *inf;
12250 inf = (struct elf_gc_sweep_symbol_info *) data;
12251 (*inf->hide_symbol) (inf->info, h, TRUE);
12252 h->def_regular = 0;
12253 h->ref_regular = 0;
12254 h->ref_regular_nonweak = 0;
12260 /* The sweep phase of garbage collection. Remove all garbage sections. */
12262 typedef bfd_boolean (*gc_sweep_hook_fn)
12263 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12266 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12269 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12270 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12271 unsigned long section_sym_count;
12272 struct elf_gc_sweep_symbol_info sweep_info;
12274 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12278 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12279 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12282 for (o = sub->sections; o != NULL; o = o->next)
12284 /* When any section in a section group is kept, we keep all
12285 sections in the section group. If the first member of
12286 the section group is excluded, we will also exclude the
12288 if (o->flags & SEC_GROUP)
12290 asection *first = elf_next_in_group (o);
12291 o->gc_mark = first->gc_mark;
12297 /* Skip sweeping sections already excluded. */
12298 if (o->flags & SEC_EXCLUDE)
12301 /* Since this is early in the link process, it is simple
12302 to remove a section from the output. */
12303 o->flags |= SEC_EXCLUDE;
12305 if (info->print_gc_sections && o->size != 0)
12306 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12308 /* But we also have to update some of the relocation
12309 info we collected before. */
12311 && (o->flags & SEC_RELOC) != 0
12312 && o->reloc_count != 0
12313 && !((info->strip == strip_all || info->strip == strip_debugger)
12314 && (o->flags & SEC_DEBUGGING) != 0)
12315 && !bfd_is_abs_section (o->output_section))
12317 Elf_Internal_Rela *internal_relocs;
12321 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12322 info->keep_memory);
12323 if (internal_relocs == NULL)
12326 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12328 if (elf_section_data (o)->relocs != internal_relocs)
12329 free (internal_relocs);
12337 /* Remove the symbols that were in the swept sections from the dynamic
12338 symbol table. GCFIXME: Anyone know how to get them out of the
12339 static symbol table as well? */
12340 sweep_info.info = info;
12341 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12342 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12345 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
12349 /* Propagate collected vtable information. This is called through
12350 elf_link_hash_traverse. */
12353 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12355 /* Those that are not vtables. */
12356 if (h->vtable == NULL || h->vtable->parent == NULL)
12359 /* Those vtables that do not have parents, we cannot merge. */
12360 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12363 /* If we've already been done, exit. */
12364 if (h->vtable->used && h->vtable->used[-1])
12367 /* Make sure the parent's table is up to date. */
12368 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12370 if (h->vtable->used == NULL)
12372 /* None of this table's entries were referenced. Re-use the
12374 h->vtable->used = h->vtable->parent->vtable->used;
12375 h->vtable->size = h->vtable->parent->vtable->size;
12380 bfd_boolean *cu, *pu;
12382 /* Or the parent's entries into ours. */
12383 cu = h->vtable->used;
12385 pu = h->vtable->parent->vtable->used;
12388 const struct elf_backend_data *bed;
12389 unsigned int log_file_align;
12391 bed = get_elf_backend_data (h->root.u.def.section->owner);
12392 log_file_align = bed->s->log_file_align;
12393 n = h->vtable->parent->vtable->size >> log_file_align;
12408 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12411 bfd_vma hstart, hend;
12412 Elf_Internal_Rela *relstart, *relend, *rel;
12413 const struct elf_backend_data *bed;
12414 unsigned int log_file_align;
12416 /* Take care of both those symbols that do not describe vtables as
12417 well as those that are not loaded. */
12418 if (h->vtable == NULL || h->vtable->parent == NULL)
12421 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12422 || h->root.type == bfd_link_hash_defweak);
12424 sec = h->root.u.def.section;
12425 hstart = h->root.u.def.value;
12426 hend = hstart + h->size;
12428 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12430 return *(bfd_boolean *) okp = FALSE;
12431 bed = get_elf_backend_data (sec->owner);
12432 log_file_align = bed->s->log_file_align;
12434 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12436 for (rel = relstart; rel < relend; ++rel)
12437 if (rel->r_offset >= hstart && rel->r_offset < hend)
12439 /* If the entry is in use, do nothing. */
12440 if (h->vtable->used
12441 && (rel->r_offset - hstart) < h->vtable->size)
12443 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12444 if (h->vtable->used[entry])
12447 /* Otherwise, kill it. */
12448 rel->r_offset = rel->r_info = rel->r_addend = 0;
12454 /* Mark sections containing dynamically referenced symbols. When
12455 building shared libraries, we must assume that any visible symbol is
12459 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12461 struct bfd_link_info *info = (struct bfd_link_info *) inf;
12462 struct bfd_elf_dynamic_list *d = info->dynamic_list;
12464 if ((h->root.type == bfd_link_hash_defined
12465 || h->root.type == bfd_link_hash_defweak)
12467 || ((h->def_regular || ELF_COMMON_DEF_P (h))
12468 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12469 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12470 && (!info->executable
12471 || info->export_dynamic
12474 && (*d->match) (&d->head, NULL, h->root.root.string)))
12475 && (strchr (h->root.root.string, ELF_VER_CHR) != NULL
12476 || !bfd_hide_sym_by_version (info->version_info,
12477 h->root.root.string)))))
12478 h->root.u.def.section->flags |= SEC_KEEP;
12483 /* Keep all sections containing symbols undefined on the command-line,
12484 and the section containing the entry symbol. */
12487 _bfd_elf_gc_keep (struct bfd_link_info *info)
12489 struct bfd_sym_chain *sym;
12491 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12493 struct elf_link_hash_entry *h;
12495 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12496 FALSE, FALSE, FALSE);
12499 && (h->root.type == bfd_link_hash_defined
12500 || h->root.type == bfd_link_hash_defweak)
12501 && !bfd_is_abs_section (h->root.u.def.section))
12502 h->root.u.def.section->flags |= SEC_KEEP;
12507 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12508 struct bfd_link_info *info)
12510 bfd *ibfd = info->input_bfds;
12512 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12515 struct elf_reloc_cookie cookie;
12517 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12520 if (!init_reloc_cookie (&cookie, info, ibfd))
12523 for (sec = ibfd->sections; sec; sec = sec->next)
12525 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12526 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12528 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12529 fini_reloc_cookie_rels (&cookie, sec);
12536 /* Do mark and sweep of unused sections. */
12539 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12541 bfd_boolean ok = TRUE;
12543 elf_gc_mark_hook_fn gc_mark_hook;
12544 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12545 struct elf_link_hash_table *htab;
12547 if (!bed->can_gc_sections
12548 || !is_elf_hash_table (info->hash))
12550 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12554 bed->gc_keep (info);
12555 htab = elf_hash_table (info);
12557 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12558 at the .eh_frame section if we can mark the FDEs individually. */
12559 for (sub = info->input_bfds;
12560 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12561 sub = sub->link.next)
12564 struct elf_reloc_cookie cookie;
12566 sec = bfd_get_section_by_name (sub, ".eh_frame");
12567 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12569 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12570 if (elf_section_data (sec)->sec_info
12571 && (sec->flags & SEC_LINKER_CREATED) == 0)
12572 elf_eh_frame_section (sub) = sec;
12573 fini_reloc_cookie_for_section (&cookie, sec);
12574 sec = bfd_get_next_section_by_name (sec);
12578 /* Apply transitive closure to the vtable entry usage info. */
12579 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
12583 /* Kill the vtable relocations that were not used. */
12584 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
12588 /* Mark dynamically referenced symbols. */
12589 if (htab->dynamic_sections_created)
12590 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
12592 /* Grovel through relocs to find out who stays ... */
12593 gc_mark_hook = bed->gc_mark_hook;
12594 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12598 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12599 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12602 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12603 Also treat note sections as a root, if the section is not part
12605 for (o = sub->sections; o != NULL; o = o->next)
12607 && (o->flags & SEC_EXCLUDE) == 0
12608 && ((o->flags & SEC_KEEP) != 0
12609 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12610 && elf_next_in_group (o) == NULL )))
12612 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12617 /* Allow the backend to mark additional target specific sections. */
12618 bed->gc_mark_extra_sections (info, gc_mark_hook);
12620 /* ... and mark SEC_EXCLUDE for those that go. */
12621 return elf_gc_sweep (abfd, info);
12624 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12627 bfd_elf_gc_record_vtinherit (bfd *abfd,
12629 struct elf_link_hash_entry *h,
12632 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12633 struct elf_link_hash_entry **search, *child;
12634 bfd_size_type extsymcount;
12635 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12637 /* The sh_info field of the symtab header tells us where the
12638 external symbols start. We don't care about the local symbols at
12640 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12641 if (!elf_bad_symtab (abfd))
12642 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12644 sym_hashes = elf_sym_hashes (abfd);
12645 sym_hashes_end = sym_hashes + extsymcount;
12647 /* Hunt down the child symbol, which is in this section at the same
12648 offset as the relocation. */
12649 for (search = sym_hashes; search != sym_hashes_end; ++search)
12651 if ((child = *search) != NULL
12652 && (child->root.type == bfd_link_hash_defined
12653 || child->root.type == bfd_link_hash_defweak)
12654 && child->root.u.def.section == sec
12655 && child->root.u.def.value == offset)
12659 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12660 abfd, sec, (unsigned long) offset);
12661 bfd_set_error (bfd_error_invalid_operation);
12665 if (!child->vtable)
12667 child->vtable = ((struct elf_link_virtual_table_entry *)
12668 bfd_zalloc (abfd, sizeof (*child->vtable)));
12669 if (!child->vtable)
12674 /* This *should* only be the absolute section. It could potentially
12675 be that someone has defined a non-global vtable though, which
12676 would be bad. It isn't worth paging in the local symbols to be
12677 sure though; that case should simply be handled by the assembler. */
12679 child->vtable->parent = (struct elf_link_hash_entry *) -1;
12682 child->vtable->parent = h;
12687 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
12690 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12691 asection *sec ATTRIBUTE_UNUSED,
12692 struct elf_link_hash_entry *h,
12695 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12696 unsigned int log_file_align = bed->s->log_file_align;
12700 h->vtable = ((struct elf_link_virtual_table_entry *)
12701 bfd_zalloc (abfd, sizeof (*h->vtable)));
12706 if (addend >= h->vtable->size)
12708 size_t size, bytes, file_align;
12709 bfd_boolean *ptr = h->vtable->used;
12711 /* While the symbol is undefined, we have to be prepared to handle
12713 file_align = 1 << log_file_align;
12714 if (h->root.type == bfd_link_hash_undefined)
12715 size = addend + file_align;
12719 if (addend >= size)
12721 /* Oops! We've got a reference past the defined end of
12722 the table. This is probably a bug -- shall we warn? */
12723 size = addend + file_align;
12726 size = (size + file_align - 1) & -file_align;
12728 /* Allocate one extra entry for use as a "done" flag for the
12729 consolidation pass. */
12730 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12734 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
12740 oldbytes = (((h->vtable->size >> log_file_align) + 1)
12741 * sizeof (bfd_boolean));
12742 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12746 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
12751 /* And arrange for that done flag to be at index -1. */
12752 h->vtable->used = ptr + 1;
12753 h->vtable->size = size;
12756 h->vtable->used[addend >> log_file_align] = TRUE;
12761 /* Map an ELF section header flag to its corresponding string. */
12765 flagword flag_value;
12766 } elf_flags_to_name_table;
12768 static elf_flags_to_name_table elf_flags_to_names [] =
12770 { "SHF_WRITE", SHF_WRITE },
12771 { "SHF_ALLOC", SHF_ALLOC },
12772 { "SHF_EXECINSTR", SHF_EXECINSTR },
12773 { "SHF_MERGE", SHF_MERGE },
12774 { "SHF_STRINGS", SHF_STRINGS },
12775 { "SHF_INFO_LINK", SHF_INFO_LINK},
12776 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12777 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12778 { "SHF_GROUP", SHF_GROUP },
12779 { "SHF_TLS", SHF_TLS },
12780 { "SHF_MASKOS", SHF_MASKOS },
12781 { "SHF_EXCLUDE", SHF_EXCLUDE },
12784 /* Returns TRUE if the section is to be included, otherwise FALSE. */
12786 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
12787 struct flag_info *flaginfo,
12790 const bfd_vma sh_flags = elf_section_flags (section);
12792 if (!flaginfo->flags_initialized)
12794 bfd *obfd = info->output_bfd;
12795 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12796 struct flag_info_list *tf = flaginfo->flag_list;
12798 int without_hex = 0;
12800 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
12803 flagword (*lookup) (char *);
12805 lookup = bed->elf_backend_lookup_section_flags_hook;
12806 if (lookup != NULL)
12808 flagword hexval = (*lookup) ((char *) tf->name);
12812 if (tf->with == with_flags)
12813 with_hex |= hexval;
12814 else if (tf->with == without_flags)
12815 without_hex |= hexval;
12820 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
12822 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
12824 if (tf->with == with_flags)
12825 with_hex |= elf_flags_to_names[i].flag_value;
12826 else if (tf->with == without_flags)
12827 without_hex |= elf_flags_to_names[i].flag_value;
12834 info->callbacks->einfo
12835 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
12839 flaginfo->flags_initialized = TRUE;
12840 flaginfo->only_with_flags |= with_hex;
12841 flaginfo->not_with_flags |= without_hex;
12844 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
12847 if ((flaginfo->not_with_flags & sh_flags) != 0)
12853 struct alloc_got_off_arg {
12855 struct bfd_link_info *info;
12858 /* We need a special top-level link routine to convert got reference counts
12859 to real got offsets. */
12862 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
12864 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
12865 bfd *obfd = gofarg->info->output_bfd;
12866 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12868 if (h->got.refcount > 0)
12870 h->got.offset = gofarg->gotoff;
12871 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
12874 h->got.offset = (bfd_vma) -1;
12879 /* And an accompanying bit to work out final got entry offsets once
12880 we're done. Should be called from final_link. */
12883 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
12884 struct bfd_link_info *info)
12887 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12889 struct alloc_got_off_arg gofarg;
12891 BFD_ASSERT (abfd == info->output_bfd);
12893 if (! is_elf_hash_table (info->hash))
12896 /* The GOT offset is relative to the .got section, but the GOT header is
12897 put into the .got.plt section, if the backend uses it. */
12898 if (bed->want_got_plt)
12901 gotoff = bed->got_header_size;
12903 /* Do the local .got entries first. */
12904 for (i = info->input_bfds; i; i = i->link.next)
12906 bfd_signed_vma *local_got;
12907 bfd_size_type j, locsymcount;
12908 Elf_Internal_Shdr *symtab_hdr;
12910 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
12913 local_got = elf_local_got_refcounts (i);
12917 symtab_hdr = &elf_tdata (i)->symtab_hdr;
12918 if (elf_bad_symtab (i))
12919 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12921 locsymcount = symtab_hdr->sh_info;
12923 for (j = 0; j < locsymcount; ++j)
12925 if (local_got[j] > 0)
12927 local_got[j] = gotoff;
12928 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
12931 local_got[j] = (bfd_vma) -1;
12935 /* Then the global .got entries. .plt refcounts are handled by
12936 adjust_dynamic_symbol */
12937 gofarg.gotoff = gotoff;
12938 gofarg.info = info;
12939 elf_link_hash_traverse (elf_hash_table (info),
12940 elf_gc_allocate_got_offsets,
12945 /* Many folk need no more in the way of final link than this, once
12946 got entry reference counting is enabled. */
12949 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
12951 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
12954 /* Invoke the regular ELF backend linker to do all the work. */
12955 return bfd_elf_final_link (abfd, info);
12959 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
12961 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
12963 if (rcookie->bad_symtab)
12964 rcookie->rel = rcookie->rels;
12966 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
12968 unsigned long r_symndx;
12970 if (! rcookie->bad_symtab)
12971 if (rcookie->rel->r_offset > offset)
12973 if (rcookie->rel->r_offset != offset)
12976 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
12977 if (r_symndx == STN_UNDEF)
12980 if (r_symndx >= rcookie->locsymcount
12981 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12983 struct elf_link_hash_entry *h;
12985 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
12987 while (h->root.type == bfd_link_hash_indirect
12988 || h->root.type == bfd_link_hash_warning)
12989 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12991 if ((h->root.type == bfd_link_hash_defined
12992 || h->root.type == bfd_link_hash_defweak)
12993 && (h->root.u.def.section->owner != rcookie->abfd
12994 || h->root.u.def.section->kept_section != NULL
12995 || discarded_section (h->root.u.def.section)))
13000 /* It's not a relocation against a global symbol,
13001 but it could be a relocation against a local
13002 symbol for a discarded section. */
13004 Elf_Internal_Sym *isym;
13006 /* Need to: get the symbol; get the section. */
13007 isym = &rcookie->locsyms[r_symndx];
13008 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13010 && (isec->kept_section != NULL
13011 || discarded_section (isec)))
13019 /* Discard unneeded references to discarded sections.
13020 Returns -1 on error, 1 if any section's size was changed, 0 if
13021 nothing changed. This function assumes that the relocations are in
13022 sorted order, which is true for all known assemblers. */
13025 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13027 struct elf_reloc_cookie cookie;
13032 if (info->traditional_format
13033 || !is_elf_hash_table (info->hash))
13036 o = bfd_get_section_by_name (output_bfd, ".stab");
13041 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13044 || i->reloc_count == 0
13045 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13049 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13052 if (!init_reloc_cookie_for_section (&cookie, info, i))
13055 if (_bfd_discard_section_stabs (abfd, i,
13056 elf_section_data (i)->sec_info,
13057 bfd_elf_reloc_symbol_deleted_p,
13061 fini_reloc_cookie_for_section (&cookie, i);
13066 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13067 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13072 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13078 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13081 if (!init_reloc_cookie_for_section (&cookie, info, i))
13084 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13085 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13086 bfd_elf_reloc_symbol_deleted_p,
13090 fini_reloc_cookie_for_section (&cookie, i);
13094 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13096 const struct elf_backend_data *bed;
13098 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13101 bed = get_elf_backend_data (abfd);
13103 if (bed->elf_backend_discard_info != NULL)
13105 if (!init_reloc_cookie (&cookie, info, abfd))
13108 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13111 fini_reloc_cookie (&cookie, abfd);
13115 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13116 _bfd_elf_end_eh_frame_parsing (info);
13118 if (info->eh_frame_hdr_type
13119 && !info->relocatable
13120 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13127 _bfd_elf_section_already_linked (bfd *abfd,
13129 struct bfd_link_info *info)
13132 const char *name, *key;
13133 struct bfd_section_already_linked *l;
13134 struct bfd_section_already_linked_hash_entry *already_linked_list;
13136 if (sec->output_section == bfd_abs_section_ptr)
13139 flags = sec->flags;
13141 /* Return if it isn't a linkonce section. A comdat group section
13142 also has SEC_LINK_ONCE set. */
13143 if ((flags & SEC_LINK_ONCE) == 0)
13146 /* Don't put group member sections on our list of already linked
13147 sections. They are handled as a group via their group section. */
13148 if (elf_sec_group (sec) != NULL)
13151 /* For a SHT_GROUP section, use the group signature as the key. */
13153 if ((flags & SEC_GROUP) != 0
13154 && elf_next_in_group (sec) != NULL
13155 && elf_group_name (elf_next_in_group (sec)) != NULL)
13156 key = elf_group_name (elf_next_in_group (sec));
13159 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13160 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13161 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13164 /* Must be a user linkonce section that doesn't follow gcc's
13165 naming convention. In this case we won't be matching
13166 single member groups. */
13170 already_linked_list = bfd_section_already_linked_table_lookup (key);
13172 for (l = already_linked_list->entry; l != NULL; l = l->next)
13174 /* We may have 2 different types of sections on the list: group
13175 sections with a signature of <key> (<key> is some string),
13176 and linkonce sections named .gnu.linkonce.<type>.<key>.
13177 Match like sections. LTO plugin sections are an exception.
13178 They are always named .gnu.linkonce.t.<key> and match either
13179 type of section. */
13180 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13181 && ((flags & SEC_GROUP) != 0
13182 || strcmp (name, l->sec->name) == 0))
13183 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13185 /* The section has already been linked. See if we should
13186 issue a warning. */
13187 if (!_bfd_handle_already_linked (sec, l, info))
13190 if (flags & SEC_GROUP)
13192 asection *first = elf_next_in_group (sec);
13193 asection *s = first;
13197 s->output_section = bfd_abs_section_ptr;
13198 /* Record which group discards it. */
13199 s->kept_section = l->sec;
13200 s = elf_next_in_group (s);
13201 /* These lists are circular. */
13211 /* A single member comdat group section may be discarded by a
13212 linkonce section and vice versa. */
13213 if ((flags & SEC_GROUP) != 0)
13215 asection *first = elf_next_in_group (sec);
13217 if (first != NULL && elf_next_in_group (first) == first)
13218 /* Check this single member group against linkonce sections. */
13219 for (l = already_linked_list->entry; l != NULL; l = l->next)
13220 if ((l->sec->flags & SEC_GROUP) == 0
13221 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13223 first->output_section = bfd_abs_section_ptr;
13224 first->kept_section = l->sec;
13225 sec->output_section = bfd_abs_section_ptr;
13230 /* Check this linkonce section against single member groups. */
13231 for (l = already_linked_list->entry; l != NULL; l = l->next)
13232 if (l->sec->flags & SEC_GROUP)
13234 asection *first = elf_next_in_group (l->sec);
13237 && elf_next_in_group (first) == first
13238 && bfd_elf_match_symbols_in_sections (first, sec, info))
13240 sec->output_section = bfd_abs_section_ptr;
13241 sec->kept_section = first;
13246 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13247 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13248 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13249 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13250 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13251 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13252 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13253 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13254 The reverse order cannot happen as there is never a bfd with only the
13255 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13256 matter as here were are looking only for cross-bfd sections. */
13258 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13259 for (l = already_linked_list->entry; l != NULL; l = l->next)
13260 if ((l->sec->flags & SEC_GROUP) == 0
13261 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13263 if (abfd != l->sec->owner)
13264 sec->output_section = bfd_abs_section_ptr;
13268 /* This is the first section with this name. Record it. */
13269 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13270 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13271 return sec->output_section == bfd_abs_section_ptr;
13275 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13277 return sym->st_shndx == SHN_COMMON;
13281 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13287 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13289 return bfd_com_section_ptr;
13293 _bfd_elf_default_got_elt_size (bfd *abfd,
13294 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13295 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13296 bfd *ibfd ATTRIBUTE_UNUSED,
13297 unsigned long symndx ATTRIBUTE_UNUSED)
13299 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13300 return bed->s->arch_size / 8;
13303 /* Routines to support the creation of dynamic relocs. */
13305 /* Returns the name of the dynamic reloc section associated with SEC. */
13307 static const char *
13308 get_dynamic_reloc_section_name (bfd * abfd,
13310 bfd_boolean is_rela)
13313 const char *old_name = bfd_get_section_name (NULL, sec);
13314 const char *prefix = is_rela ? ".rela" : ".rel";
13316 if (old_name == NULL)
13319 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13320 sprintf (name, "%s%s", prefix, old_name);
13325 /* Returns the dynamic reloc section associated with SEC.
13326 If necessary compute the name of the dynamic reloc section based
13327 on SEC's name (looked up in ABFD's string table) and the setting
13331 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13333 bfd_boolean is_rela)
13335 asection * reloc_sec = elf_section_data (sec)->sreloc;
13337 if (reloc_sec == NULL)
13339 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13343 reloc_sec = bfd_get_linker_section (abfd, name);
13345 if (reloc_sec != NULL)
13346 elf_section_data (sec)->sreloc = reloc_sec;
13353 /* Returns the dynamic reloc section associated with SEC. If the
13354 section does not exist it is created and attached to the DYNOBJ
13355 bfd and stored in the SRELOC field of SEC's elf_section_data
13358 ALIGNMENT is the alignment for the newly created section and
13359 IS_RELA defines whether the name should be .rela.<SEC's name>
13360 or .rel.<SEC's name>. The section name is looked up in the
13361 string table associated with ABFD. */
13364 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13366 unsigned int alignment,
13368 bfd_boolean is_rela)
13370 asection * reloc_sec = elf_section_data (sec)->sreloc;
13372 if (reloc_sec == NULL)
13374 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13379 reloc_sec = bfd_get_linker_section (dynobj, name);
13381 if (reloc_sec == NULL)
13383 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13384 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13385 if ((sec->flags & SEC_ALLOC) != 0)
13386 flags |= SEC_ALLOC | SEC_LOAD;
13388 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13389 if (reloc_sec != NULL)
13391 /* _bfd_elf_get_sec_type_attr chooses a section type by
13392 name. Override as it may be wrong, eg. for a user
13393 section named "auto" we'll get ".relauto" which is
13394 seen to be a .rela section. */
13395 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13396 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13401 elf_section_data (sec)->sreloc = reloc_sec;
13407 /* Copy the ELF symbol type and other attributes for a linker script
13408 assignment from HSRC to HDEST. Generally this should be treated as
13409 if we found a strong non-dynamic definition for HDEST (except that
13410 ld ignores multiple definition errors). */
13412 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13413 struct bfd_link_hash_entry *hdest,
13414 struct bfd_link_hash_entry *hsrc)
13416 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13417 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13418 Elf_Internal_Sym isym;
13420 ehdest->type = ehsrc->type;
13421 ehdest->target_internal = ehsrc->target_internal;
13423 isym.st_other = ehsrc->other;
13424 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
13427 /* Append a RELA relocation REL to section S in BFD. */
13430 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13432 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13433 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13434 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13435 bed->s->swap_reloca_out (abfd, rel, loc);
13438 /* Append a REL relocation REL to section S in BFD. */
13441 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13443 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13444 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13445 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13446 bed->s->swap_reloc_out (abfd, rel, loc);