1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2016 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "bfd_stdint.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
32 /* This struct is used to pass information to routines called via
33 elf_link_hash_traverse which must return failure. */
35 struct elf_info_failed
37 struct bfd_link_info *info;
41 /* This structure is used to pass information to
42 _bfd_elf_link_find_version_dependencies. */
44 struct elf_find_verdep_info
46 /* General link information. */
47 struct bfd_link_info *info;
48 /* The number of dependencies. */
50 /* Whether we had a failure. */
54 static bfd_boolean _bfd_elf_fix_symbol_flags
55 (struct elf_link_hash_entry *, struct elf_info_failed *);
58 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
59 unsigned long r_symndx,
62 if (r_symndx >= cookie->locsymcount
63 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
65 struct elf_link_hash_entry *h;
67 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
69 while (h->root.type == bfd_link_hash_indirect
70 || h->root.type == bfd_link_hash_warning)
71 h = (struct elf_link_hash_entry *) h->root.u.i.link;
73 if ((h->root.type == bfd_link_hash_defined
74 || h->root.type == bfd_link_hash_defweak)
75 && discarded_section (h->root.u.def.section))
76 return h->root.u.def.section;
82 /* It's not a relocation against a global symbol,
83 but it could be a relocation against a local
84 symbol for a discarded section. */
86 Elf_Internal_Sym *isym;
88 /* Need to: get the symbol; get the section. */
89 isym = &cookie->locsyms[r_symndx];
90 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
92 && discard ? discarded_section (isec) : 1)
98 /* Define a symbol in a dynamic linkage section. */
100 struct elf_link_hash_entry *
101 _bfd_elf_define_linkage_sym (bfd *abfd,
102 struct bfd_link_info *info,
106 struct elf_link_hash_entry *h;
107 struct bfd_link_hash_entry *bh;
108 const struct elf_backend_data *bed;
110 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
113 /* Zap symbol defined in an as-needed lib that wasn't linked.
114 This is a symptom of a larger problem: Absolute symbols
115 defined in shared libraries can't be overridden, because we
116 lose the link to the bfd which is via the symbol section. */
117 h->root.type = bfd_link_hash_new;
121 bed = get_elf_backend_data (abfd);
122 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
123 sec, 0, NULL, FALSE, bed->collect,
126 h = (struct elf_link_hash_entry *) bh;
129 h->root.linker_def = 1;
130 h->type = STT_OBJECT;
131 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
132 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
134 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
139 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
143 struct elf_link_hash_entry *h;
144 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
145 struct elf_link_hash_table *htab = elf_hash_table (info);
147 /* This function may be called more than once. */
148 s = bfd_get_linker_section (abfd, ".got");
152 flags = bed->dynamic_sec_flags;
154 s = bfd_make_section_anyway_with_flags (abfd,
155 (bed->rela_plts_and_copies_p
156 ? ".rela.got" : ".rel.got"),
157 (bed->dynamic_sec_flags
160 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
164 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
166 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 if (bed->want_got_plt)
172 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
174 || !bfd_set_section_alignment (abfd, s,
175 bed->s->log_file_align))
180 /* The first bit of the global offset table is the header. */
181 s->size += bed->got_header_size;
183 if (bed->want_got_sym)
185 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
186 (or .got.plt) section. We don't do this in the linker script
187 because we don't want to define the symbol if we are not creating
188 a global offset table. */
189 h = _bfd_elf_define_linkage_sym (abfd, info, s,
190 "_GLOBAL_OFFSET_TABLE_");
191 elf_hash_table (info)->hgot = h;
199 /* Create a strtab to hold the dynamic symbol names. */
201 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
203 struct elf_link_hash_table *hash_table;
205 hash_table = elf_hash_table (info);
206 if (hash_table->dynobj == NULL)
207 hash_table->dynobj = abfd;
209 if (hash_table->dynstr == NULL)
211 hash_table->dynstr = _bfd_elf_strtab_init ();
212 if (hash_table->dynstr == NULL)
218 /* Create some sections which will be filled in with dynamic linking
219 information. ABFD is an input file which requires dynamic sections
220 to be created. The dynamic sections take up virtual memory space
221 when the final executable is run, so we need to create them before
222 addresses are assigned to the output sections. We work out the
223 actual contents and size of these sections later. */
226 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
230 const struct elf_backend_data *bed;
231 struct elf_link_hash_entry *h;
233 if (! is_elf_hash_table (info->hash))
236 if (elf_hash_table (info)->dynamic_sections_created)
239 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
242 abfd = elf_hash_table (info)->dynobj;
243 bed = get_elf_backend_data (abfd);
245 flags = bed->dynamic_sec_flags;
247 /* A dynamically linked executable has a .interp section, but a
248 shared library does not. */
249 if (bfd_link_executable (info) && !info->nointerp)
251 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
252 flags | SEC_READONLY);
257 /* Create sections to hold version informations. These are removed
258 if they are not needed. */
259 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
260 flags | SEC_READONLY);
262 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
265 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
266 flags | SEC_READONLY);
268 || ! bfd_set_section_alignment (abfd, s, 1))
271 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
272 flags | SEC_READONLY);
274 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
277 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
278 flags | SEC_READONLY);
280 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
282 elf_hash_table (info)->dynsym = s;
284 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
285 flags | SEC_READONLY);
289 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
291 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
294 /* The special symbol _DYNAMIC is always set to the start of the
295 .dynamic section. We could set _DYNAMIC in a linker script, but we
296 only want to define it if we are, in fact, creating a .dynamic
297 section. We don't want to define it if there is no .dynamic
298 section, since on some ELF platforms the start up code examines it
299 to decide how to initialize the process. */
300 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
301 elf_hash_table (info)->hdynamic = h;
307 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
308 flags | SEC_READONLY);
310 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
312 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
315 if (info->emit_gnu_hash)
317 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
318 flags | SEC_READONLY);
320 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
322 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
323 4 32-bit words followed by variable count of 64-bit words, then
324 variable count of 32-bit words. */
325 if (bed->s->arch_size == 64)
326 elf_section_data (s)->this_hdr.sh_entsize = 0;
328 elf_section_data (s)->this_hdr.sh_entsize = 4;
331 /* Let the backend create the rest of the sections. This lets the
332 backend set the right flags. The backend will normally create
333 the .got and .plt sections. */
334 if (bed->elf_backend_create_dynamic_sections == NULL
335 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
338 elf_hash_table (info)->dynamic_sections_created = TRUE;
343 /* Create dynamic sections when linking against a dynamic object. */
346 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
348 flagword flags, pltflags;
349 struct elf_link_hash_entry *h;
351 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
352 struct elf_link_hash_table *htab = elf_hash_table (info);
354 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
355 .rel[a].bss sections. */
356 flags = bed->dynamic_sec_flags;
359 if (bed->plt_not_loaded)
360 /* We do not clear SEC_ALLOC here because we still want the OS to
361 allocate space for the section; it's just that there's nothing
362 to read in from the object file. */
363 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
365 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
366 if (bed->plt_readonly)
367 pltflags |= SEC_READONLY;
369 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
371 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
375 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
377 if (bed->want_plt_sym)
379 h = _bfd_elf_define_linkage_sym (abfd, info, s,
380 "_PROCEDURE_LINKAGE_TABLE_");
381 elf_hash_table (info)->hplt = h;
386 s = bfd_make_section_anyway_with_flags (abfd,
387 (bed->rela_plts_and_copies_p
388 ? ".rela.plt" : ".rel.plt"),
389 flags | SEC_READONLY);
391 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
395 if (! _bfd_elf_create_got_section (abfd, info))
398 if (bed->want_dynbss)
400 /* The .dynbss section is a place to put symbols which are defined
401 by dynamic objects, are referenced by regular objects, and are
402 not functions. We must allocate space for them in the process
403 image and use a R_*_COPY reloc to tell the dynamic linker to
404 initialize them at run time. The linker script puts the .dynbss
405 section into the .bss section of the final image. */
406 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
407 (SEC_ALLOC | SEC_LINKER_CREATED));
411 /* The .rel[a].bss section holds copy relocs. This section is not
412 normally needed. We need to create it here, though, so that the
413 linker will map it to an output section. We can't just create it
414 only if we need it, because we will not know whether we need it
415 until we have seen all the input files, and the first time the
416 main linker code calls BFD after examining all the input files
417 (size_dynamic_sections) the input sections have already been
418 mapped to the output sections. If the section turns out not to
419 be needed, we can discard it later. We will never need this
420 section when generating a shared object, since they do not use
422 if (! bfd_link_pic (info))
424 s = bfd_make_section_anyway_with_flags (abfd,
425 (bed->rela_plts_and_copies_p
426 ? ".rela.bss" : ".rel.bss"),
427 flags | SEC_READONLY);
429 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
437 /* Record a new dynamic symbol. We record the dynamic symbols as we
438 read the input files, since we need to have a list of all of them
439 before we can determine the final sizes of the output sections.
440 Note that we may actually call this function even though we are not
441 going to output any dynamic symbols; in some cases we know that a
442 symbol should be in the dynamic symbol table, but only if there is
446 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
447 struct elf_link_hash_entry *h)
449 if (h->dynindx == -1)
451 struct elf_strtab_hash *dynstr;
456 /* XXX: The ABI draft says the linker must turn hidden and
457 internal symbols into STB_LOCAL symbols when producing the
458 DSO. However, if ld.so honors st_other in the dynamic table,
459 this would not be necessary. */
460 switch (ELF_ST_VISIBILITY (h->other))
464 if (h->root.type != bfd_link_hash_undefined
465 && h->root.type != bfd_link_hash_undefweak)
468 if (!elf_hash_table (info)->is_relocatable_executable)
476 h->dynindx = elf_hash_table (info)->dynsymcount;
477 ++elf_hash_table (info)->dynsymcount;
479 dynstr = elf_hash_table (info)->dynstr;
482 /* Create a strtab to hold the dynamic symbol names. */
483 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
488 /* We don't put any version information in the dynamic string
490 name = h->root.root.string;
491 p = strchr (name, ELF_VER_CHR);
493 /* We know that the p points into writable memory. In fact,
494 there are only a few symbols that have read-only names, being
495 those like _GLOBAL_OFFSET_TABLE_ that are created specially
496 by the backends. Most symbols will have names pointing into
497 an ELF string table read from a file, or to objalloc memory. */
500 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
505 if (indx == (bfd_size_type) -1)
507 h->dynstr_index = indx;
513 /* Mark a symbol dynamic. */
516 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
517 struct elf_link_hash_entry *h,
518 Elf_Internal_Sym *sym)
520 struct bfd_elf_dynamic_list *d = info->dynamic_list;
522 /* It may be called more than once on the same H. */
523 if(h->dynamic || bfd_link_relocatable (info))
526 if ((info->dynamic_data
527 && (h->type == STT_OBJECT
529 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
531 && h->root.type == bfd_link_hash_new
532 && (*d->match) (&d->head, NULL, h->root.root.string)))
536 /* Record an assignment to a symbol made by a linker script. We need
537 this in case some dynamic object refers to this symbol. */
540 bfd_elf_record_link_assignment (bfd *output_bfd,
541 struct bfd_link_info *info,
546 struct elf_link_hash_entry *h, *hv;
547 struct elf_link_hash_table *htab;
548 const struct elf_backend_data *bed;
550 if (!is_elf_hash_table (info->hash))
553 htab = elf_hash_table (info);
554 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
558 switch (h->root.type)
560 case bfd_link_hash_defined:
561 case bfd_link_hash_defweak:
562 case bfd_link_hash_common:
564 case bfd_link_hash_undefweak:
565 case bfd_link_hash_undefined:
566 /* Since we're defining the symbol, don't let it seem to have not
567 been defined. record_dynamic_symbol and size_dynamic_sections
568 may depend on this. */
569 h->root.type = bfd_link_hash_new;
570 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
571 bfd_link_repair_undef_list (&htab->root);
573 case bfd_link_hash_new:
574 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
577 case bfd_link_hash_indirect:
578 /* We had a versioned symbol in a dynamic library. We make the
579 the versioned symbol point to this one. */
580 bed = get_elf_backend_data (output_bfd);
582 while (hv->root.type == bfd_link_hash_indirect
583 || hv->root.type == bfd_link_hash_warning)
584 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
585 /* We don't need to update h->root.u since linker will set them
587 h->root.type = bfd_link_hash_undefined;
588 hv->root.type = bfd_link_hash_indirect;
589 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
590 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
592 case bfd_link_hash_warning:
597 /* If this symbol is being provided by the linker script, and it is
598 currently defined by a dynamic object, but not by a regular
599 object, then mark it as undefined so that the generic linker will
600 force the correct value. */
604 h->root.type = bfd_link_hash_undefined;
606 /* If this symbol is not being provided by the linker script, and it is
607 currently defined by a dynamic object, but not by a regular object,
608 then clear out any version information because the symbol will not be
609 associated with the dynamic object any more. */
613 h->verinfo.verdef = NULL;
619 bed = get_elf_backend_data (output_bfd);
620 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
621 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
622 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
625 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
627 if (!bfd_link_relocatable (info)
629 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
630 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
635 || bfd_link_pic (info)
636 || (bfd_link_pde (info)
637 && elf_hash_table (info)->is_relocatable_executable))
640 if (! bfd_elf_link_record_dynamic_symbol (info, h))
643 /* If this is a weak defined symbol, and we know a corresponding
644 real symbol from the same dynamic object, make sure the real
645 symbol is also made into a dynamic symbol. */
646 if (h->u.weakdef != NULL
647 && h->u.weakdef->dynindx == -1)
649 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
657 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
658 success, and 2 on a failure caused by attempting to record a symbol
659 in a discarded section, eg. a discarded link-once section symbol. */
662 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
667 struct elf_link_local_dynamic_entry *entry;
668 struct elf_link_hash_table *eht;
669 struct elf_strtab_hash *dynstr;
670 unsigned long dynstr_index;
672 Elf_External_Sym_Shndx eshndx;
673 char esym[sizeof (Elf64_External_Sym)];
675 if (! is_elf_hash_table (info->hash))
678 /* See if the entry exists already. */
679 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
680 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
683 amt = sizeof (*entry);
684 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
688 /* Go find the symbol, so that we can find it's name. */
689 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
690 1, input_indx, &entry->isym, esym, &eshndx))
692 bfd_release (input_bfd, entry);
696 if (entry->isym.st_shndx != SHN_UNDEF
697 && entry->isym.st_shndx < SHN_LORESERVE)
701 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
702 if (s == NULL || bfd_is_abs_section (s->output_section))
704 /* We can still bfd_release here as nothing has done another
705 bfd_alloc. We can't do this later in this function. */
706 bfd_release (input_bfd, entry);
711 name = (bfd_elf_string_from_elf_section
712 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
713 entry->isym.st_name));
715 dynstr = elf_hash_table (info)->dynstr;
718 /* Create a strtab to hold the dynamic symbol names. */
719 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
724 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
725 if (dynstr_index == (unsigned long) -1)
727 entry->isym.st_name = dynstr_index;
729 eht = elf_hash_table (info);
731 entry->next = eht->dynlocal;
732 eht->dynlocal = entry;
733 entry->input_bfd = input_bfd;
734 entry->input_indx = input_indx;
737 /* Whatever binding the symbol had before, it's now local. */
739 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
741 /* The dynindx will be set at the end of size_dynamic_sections. */
746 /* Return the dynindex of a local dynamic symbol. */
749 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
753 struct elf_link_local_dynamic_entry *e;
755 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
756 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
761 /* This function is used to renumber the dynamic symbols, if some of
762 them are removed because they are marked as local. This is called
763 via elf_link_hash_traverse. */
766 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
769 size_t *count = (size_t *) data;
774 if (h->dynindx != -1)
775 h->dynindx = ++(*count);
781 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
782 STB_LOCAL binding. */
785 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
788 size_t *count = (size_t *) data;
790 if (!h->forced_local)
793 if (h->dynindx != -1)
794 h->dynindx = ++(*count);
799 /* Return true if the dynamic symbol for a given section should be
800 omitted when creating a shared library. */
802 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
803 struct bfd_link_info *info,
806 struct elf_link_hash_table *htab;
809 switch (elf_section_data (p)->this_hdr.sh_type)
813 /* If sh_type is yet undecided, assume it could be
814 SHT_PROGBITS/SHT_NOBITS. */
816 htab = elf_hash_table (info);
817 if (p == htab->tls_sec)
820 if (htab->text_index_section != NULL)
821 return p != htab->text_index_section && p != htab->data_index_section;
823 return (htab->dynobj != NULL
824 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
825 && ip->output_section == p);
827 /* There shouldn't be section relative relocations
828 against any other section. */
834 /* Assign dynsym indices. In a shared library we generate a section
835 symbol for each output section, which come first. Next come symbols
836 which have been forced to local binding. Then all of the back-end
837 allocated local dynamic syms, followed by the rest of the global
841 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
842 struct bfd_link_info *info,
843 unsigned long *section_sym_count)
845 unsigned long dynsymcount = 0;
847 if (bfd_link_pic (info)
848 || elf_hash_table (info)->is_relocatable_executable)
850 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
852 for (p = output_bfd->sections; p ; p = p->next)
853 if ((p->flags & SEC_EXCLUDE) == 0
854 && (p->flags & SEC_ALLOC) != 0
855 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
856 elf_section_data (p)->dynindx = ++dynsymcount;
858 elf_section_data (p)->dynindx = 0;
860 *section_sym_count = dynsymcount;
862 elf_link_hash_traverse (elf_hash_table (info),
863 elf_link_renumber_local_hash_table_dynsyms,
866 if (elf_hash_table (info)->dynlocal)
868 struct elf_link_local_dynamic_entry *p;
869 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
870 p->dynindx = ++dynsymcount;
873 elf_link_hash_traverse (elf_hash_table (info),
874 elf_link_renumber_hash_table_dynsyms,
877 /* There is an unused NULL entry at the head of the table which
878 we must account for in our count. Unless there weren't any
879 symbols, which means we'll have no table at all. */
880 if (dynsymcount != 0)
883 elf_hash_table (info)->dynsymcount = dynsymcount;
887 /* Merge st_other field. */
890 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
891 const Elf_Internal_Sym *isym, asection *sec,
892 bfd_boolean definition, bfd_boolean dynamic)
894 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
896 /* If st_other has a processor-specific meaning, specific
897 code might be needed here. */
898 if (bed->elf_backend_merge_symbol_attribute)
899 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
904 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
905 unsigned hvis = ELF_ST_VISIBILITY (h->other);
907 /* Keep the most constraining visibility. Leave the remainder
908 of the st_other field to elf_backend_merge_symbol_attribute. */
909 if (symvis - 1 < hvis - 1)
910 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
913 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
914 && (sec->flags & SEC_READONLY) == 0)
915 h->protected_def = 1;
918 /* This function is called when we want to merge a new symbol with an
919 existing symbol. It handles the various cases which arise when we
920 find a definition in a dynamic object, or when there is already a
921 definition in a dynamic object. The new symbol is described by
922 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
923 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
924 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
925 of an old common symbol. We set OVERRIDE if the old symbol is
926 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
927 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
928 to change. By OK to change, we mean that we shouldn't warn if the
929 type or size does change. */
932 _bfd_elf_merge_symbol (bfd *abfd,
933 struct bfd_link_info *info,
935 Elf_Internal_Sym *sym,
938 struct elf_link_hash_entry **sym_hash,
940 bfd_boolean *pold_weak,
941 unsigned int *pold_alignment,
943 bfd_boolean *override,
944 bfd_boolean *type_change_ok,
945 bfd_boolean *size_change_ok,
946 bfd_boolean *matched)
948 asection *sec, *oldsec;
949 struct elf_link_hash_entry *h;
950 struct elf_link_hash_entry *hi;
951 struct elf_link_hash_entry *flip;
954 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
955 bfd_boolean newweak, oldweak, newfunc, oldfunc;
956 const struct elf_backend_data *bed;
963 bind = ELF_ST_BIND (sym->st_info);
965 if (! bfd_is_und_section (sec))
966 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
968 h = ((struct elf_link_hash_entry *)
969 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
974 bed = get_elf_backend_data (abfd);
976 /* NEW_VERSION is the symbol version of the new symbol. */
977 if (h->versioned != unversioned)
979 /* Symbol version is unknown or versioned. */
980 new_version = strrchr (name, ELF_VER_CHR);
983 if (h->versioned == unknown)
985 if (new_version > name && new_version[-1] != ELF_VER_CHR)
986 h->versioned = versioned_hidden;
988 h->versioned = versioned;
991 if (new_version[0] == '\0')
995 h->versioned = unversioned;
1000 /* For merging, we only care about real symbols. But we need to make
1001 sure that indirect symbol dynamic flags are updated. */
1003 while (h->root.type == bfd_link_hash_indirect
1004 || h->root.type == bfd_link_hash_warning)
1005 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1009 if (hi == h || h->root.type == bfd_link_hash_new)
1013 /* OLD_HIDDEN is true if the existing symbol is only visible
1014 to the symbol with the same symbol version. NEW_HIDDEN is
1015 true if the new symbol is only visible to the symbol with
1016 the same symbol version. */
1017 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1018 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1019 if (!old_hidden && !new_hidden)
1020 /* The new symbol matches the existing symbol if both
1025 /* OLD_VERSION is the symbol version of the existing
1029 if (h->versioned >= versioned)
1030 old_version = strrchr (h->root.root.string,
1035 /* The new symbol matches the existing symbol if they
1036 have the same symbol version. */
1037 *matched = (old_version == new_version
1038 || (old_version != NULL
1039 && new_version != NULL
1040 && strcmp (old_version, new_version) == 0));
1045 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1050 switch (h->root.type)
1055 case bfd_link_hash_undefined:
1056 case bfd_link_hash_undefweak:
1057 oldbfd = h->root.u.undef.abfd;
1060 case bfd_link_hash_defined:
1061 case bfd_link_hash_defweak:
1062 oldbfd = h->root.u.def.section->owner;
1063 oldsec = h->root.u.def.section;
1066 case bfd_link_hash_common:
1067 oldbfd = h->root.u.c.p->section->owner;
1068 oldsec = h->root.u.c.p->section;
1070 *pold_alignment = h->root.u.c.p->alignment_power;
1073 if (poldbfd && *poldbfd == NULL)
1076 /* Differentiate strong and weak symbols. */
1077 newweak = bind == STB_WEAK;
1078 oldweak = (h->root.type == bfd_link_hash_defweak
1079 || h->root.type == bfd_link_hash_undefweak);
1081 *pold_weak = oldweak;
1083 /* This code is for coping with dynamic objects, and is only useful
1084 if we are doing an ELF link. */
1085 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1088 /* We have to check it for every instance since the first few may be
1089 references and not all compilers emit symbol type for undefined
1091 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1093 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1094 respectively, is from a dynamic object. */
1096 newdyn = (abfd->flags & DYNAMIC) != 0;
1098 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1099 syms and defined syms in dynamic libraries respectively.
1100 ref_dynamic on the other hand can be set for a symbol defined in
1101 a dynamic library, and def_dynamic may not be set; When the
1102 definition in a dynamic lib is overridden by a definition in the
1103 executable use of the symbol in the dynamic lib becomes a
1104 reference to the executable symbol. */
1107 if (bfd_is_und_section (sec))
1109 if (bind != STB_WEAK)
1111 h->ref_dynamic_nonweak = 1;
1112 hi->ref_dynamic_nonweak = 1;
1117 /* Update the existing symbol only if they match. */
1120 hi->dynamic_def = 1;
1124 /* If we just created the symbol, mark it as being an ELF symbol.
1125 Other than that, there is nothing to do--there is no merge issue
1126 with a newly defined symbol--so we just return. */
1128 if (h->root.type == bfd_link_hash_new)
1134 /* In cases involving weak versioned symbols, we may wind up trying
1135 to merge a symbol with itself. Catch that here, to avoid the
1136 confusion that results if we try to override a symbol with
1137 itself. The additional tests catch cases like
1138 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1139 dynamic object, which we do want to handle here. */
1141 && (newweak || oldweak)
1142 && ((abfd->flags & DYNAMIC) == 0
1143 || !h->def_regular))
1148 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1149 else if (oldsec != NULL)
1151 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1152 indices used by MIPS ELF. */
1153 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1156 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1157 respectively, appear to be a definition rather than reference. */
1159 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1161 olddef = (h->root.type != bfd_link_hash_undefined
1162 && h->root.type != bfd_link_hash_undefweak
1163 && h->root.type != bfd_link_hash_common);
1165 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1166 respectively, appear to be a function. */
1168 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1169 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1171 oldfunc = (h->type != STT_NOTYPE
1172 && bed->is_function_type (h->type));
1174 /* When we try to create a default indirect symbol from the dynamic
1175 definition with the default version, we skip it if its type and
1176 the type of existing regular definition mismatch. */
1177 if (pold_alignment == NULL
1181 && (((olddef || h->root.type == bfd_link_hash_common)
1182 && ELF_ST_TYPE (sym->st_info) != h->type
1183 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1184 && h->type != STT_NOTYPE
1185 && !(newfunc && oldfunc))
1187 && ((h->type == STT_GNU_IFUNC)
1188 != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
1194 /* Check TLS symbols. We don't check undefined symbols introduced
1195 by "ld -u" which have no type (and oldbfd NULL), and we don't
1196 check symbols from plugins because they also have no type. */
1198 && (oldbfd->flags & BFD_PLUGIN) == 0
1199 && (abfd->flags & BFD_PLUGIN) == 0
1200 && ELF_ST_TYPE (sym->st_info) != h->type
1201 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1204 bfd_boolean ntdef, tdef;
1205 asection *ntsec, *tsec;
1207 if (h->type == STT_TLS)
1227 (*_bfd_error_handler)
1228 (_("%s: TLS definition in %B section %A "
1229 "mismatches non-TLS definition in %B section %A"),
1230 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1231 else if (!tdef && !ntdef)
1232 (*_bfd_error_handler)
1233 (_("%s: TLS reference in %B "
1234 "mismatches non-TLS reference in %B"),
1235 tbfd, ntbfd, h->root.root.string);
1237 (*_bfd_error_handler)
1238 (_("%s: TLS definition in %B section %A "
1239 "mismatches non-TLS reference in %B"),
1240 tbfd, tsec, ntbfd, h->root.root.string);
1242 (*_bfd_error_handler)
1243 (_("%s: TLS reference in %B "
1244 "mismatches non-TLS definition in %B section %A"),
1245 tbfd, ntbfd, ntsec, h->root.root.string);
1247 bfd_set_error (bfd_error_bad_value);
1251 /* If the old symbol has non-default visibility, we ignore the new
1252 definition from a dynamic object. */
1254 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1255 && !bfd_is_und_section (sec))
1258 /* Make sure this symbol is dynamic. */
1260 hi->ref_dynamic = 1;
1261 /* A protected symbol has external availability. Make sure it is
1262 recorded as dynamic.
1264 FIXME: Should we check type and size for protected symbol? */
1265 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1266 return bfd_elf_link_record_dynamic_symbol (info, h);
1271 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1274 /* If the new symbol with non-default visibility comes from a
1275 relocatable file and the old definition comes from a dynamic
1276 object, we remove the old definition. */
1277 if (hi->root.type == bfd_link_hash_indirect)
1279 /* Handle the case where the old dynamic definition is
1280 default versioned. We need to copy the symbol info from
1281 the symbol with default version to the normal one if it
1282 was referenced before. */
1285 hi->root.type = h->root.type;
1286 h->root.type = bfd_link_hash_indirect;
1287 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1289 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1290 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1292 /* If the new symbol is hidden or internal, completely undo
1293 any dynamic link state. */
1294 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1295 h->forced_local = 0;
1302 /* FIXME: Should we check type and size for protected symbol? */
1312 /* If the old symbol was undefined before, then it will still be
1313 on the undefs list. If the new symbol is undefined or
1314 common, we can't make it bfd_link_hash_new here, because new
1315 undefined or common symbols will be added to the undefs list
1316 by _bfd_generic_link_add_one_symbol. Symbols may not be
1317 added twice to the undefs list. Also, if the new symbol is
1318 undefweak then we don't want to lose the strong undef. */
1319 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1321 h->root.type = bfd_link_hash_undefined;
1322 h->root.u.undef.abfd = abfd;
1326 h->root.type = bfd_link_hash_new;
1327 h->root.u.undef.abfd = NULL;
1330 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1332 /* If the new symbol is hidden or internal, completely undo
1333 any dynamic link state. */
1334 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1335 h->forced_local = 0;
1341 /* FIXME: Should we check type and size for protected symbol? */
1347 /* If a new weak symbol definition comes from a regular file and the
1348 old symbol comes from a dynamic library, we treat the new one as
1349 strong. Similarly, an old weak symbol definition from a regular
1350 file is treated as strong when the new symbol comes from a dynamic
1351 library. Further, an old weak symbol from a dynamic library is
1352 treated as strong if the new symbol is from a dynamic library.
1353 This reflects the way glibc's ld.so works.
1355 Do this before setting *type_change_ok or *size_change_ok so that
1356 we warn properly when dynamic library symbols are overridden. */
1358 if (newdef && !newdyn && olddyn)
1360 if (olddef && newdyn)
1363 /* Allow changes between different types of function symbol. */
1364 if (newfunc && oldfunc)
1365 *type_change_ok = TRUE;
1367 /* It's OK to change the type if either the existing symbol or the
1368 new symbol is weak. A type change is also OK if the old symbol
1369 is undefined and the new symbol is defined. */
1374 && h->root.type == bfd_link_hash_undefined))
1375 *type_change_ok = TRUE;
1377 /* It's OK to change the size if either the existing symbol or the
1378 new symbol is weak, or if the old symbol is undefined. */
1381 || h->root.type == bfd_link_hash_undefined)
1382 *size_change_ok = TRUE;
1384 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1385 symbol, respectively, appears to be a common symbol in a dynamic
1386 object. If a symbol appears in an uninitialized section, and is
1387 not weak, and is not a function, then it may be a common symbol
1388 which was resolved when the dynamic object was created. We want
1389 to treat such symbols specially, because they raise special
1390 considerations when setting the symbol size: if the symbol
1391 appears as a common symbol in a regular object, and the size in
1392 the regular object is larger, we must make sure that we use the
1393 larger size. This problematic case can always be avoided in C,
1394 but it must be handled correctly when using Fortran shared
1397 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1398 likewise for OLDDYNCOMMON and OLDDEF.
1400 Note that this test is just a heuristic, and that it is quite
1401 possible to have an uninitialized symbol in a shared object which
1402 is really a definition, rather than a common symbol. This could
1403 lead to some minor confusion when the symbol really is a common
1404 symbol in some regular object. However, I think it will be
1410 && (sec->flags & SEC_ALLOC) != 0
1411 && (sec->flags & SEC_LOAD) == 0
1414 newdyncommon = TRUE;
1416 newdyncommon = FALSE;
1420 && h->root.type == bfd_link_hash_defined
1422 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1423 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1426 olddyncommon = TRUE;
1428 olddyncommon = FALSE;
1430 /* We now know everything about the old and new symbols. We ask the
1431 backend to check if we can merge them. */
1432 if (bed->merge_symbol != NULL)
1434 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1439 /* If both the old and the new symbols look like common symbols in a
1440 dynamic object, set the size of the symbol to the larger of the
1445 && sym->st_size != h->size)
1447 /* Since we think we have two common symbols, issue a multiple
1448 common warning if desired. Note that we only warn if the
1449 size is different. If the size is the same, we simply let
1450 the old symbol override the new one as normally happens with
1451 symbols defined in dynamic objects. */
1453 if (! ((*info->callbacks->multiple_common)
1454 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1457 if (sym->st_size > h->size)
1458 h->size = sym->st_size;
1460 *size_change_ok = TRUE;
1463 /* If we are looking at a dynamic object, and we have found a
1464 definition, we need to see if the symbol was already defined by
1465 some other object. If so, we want to use the existing
1466 definition, and we do not want to report a multiple symbol
1467 definition error; we do this by clobbering *PSEC to be
1468 bfd_und_section_ptr.
1470 We treat a common symbol as a definition if the symbol in the
1471 shared library is a function, since common symbols always
1472 represent variables; this can cause confusion in principle, but
1473 any such confusion would seem to indicate an erroneous program or
1474 shared library. We also permit a common symbol in a regular
1475 object to override a weak symbol in a shared object. */
1480 || (h->root.type == bfd_link_hash_common
1481 && (newweak || newfunc))))
1485 newdyncommon = FALSE;
1487 *psec = sec = bfd_und_section_ptr;
1488 *size_change_ok = TRUE;
1490 /* If we get here when the old symbol is a common symbol, then
1491 we are explicitly letting it override a weak symbol or
1492 function in a dynamic object, and we don't want to warn about
1493 a type change. If the old symbol is a defined symbol, a type
1494 change warning may still be appropriate. */
1496 if (h->root.type == bfd_link_hash_common)
1497 *type_change_ok = TRUE;
1500 /* Handle the special case of an old common symbol merging with a
1501 new symbol which looks like a common symbol in a shared object.
1502 We change *PSEC and *PVALUE to make the new symbol look like a
1503 common symbol, and let _bfd_generic_link_add_one_symbol do the
1507 && h->root.type == bfd_link_hash_common)
1511 newdyncommon = FALSE;
1512 *pvalue = sym->st_size;
1513 *psec = sec = bed->common_section (oldsec);
1514 *size_change_ok = TRUE;
1517 /* Skip weak definitions of symbols that are already defined. */
1518 if (newdef && olddef && newweak)
1520 /* Don't skip new non-IR weak syms. */
1521 if (!(oldbfd != NULL
1522 && (oldbfd->flags & BFD_PLUGIN) != 0
1523 && (abfd->flags & BFD_PLUGIN) == 0))
1529 /* Merge st_other. If the symbol already has a dynamic index,
1530 but visibility says it should not be visible, turn it into a
1532 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1533 if (h->dynindx != -1)
1534 switch (ELF_ST_VISIBILITY (h->other))
1538 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1543 /* If the old symbol is from a dynamic object, and the new symbol is
1544 a definition which is not from a dynamic object, then the new
1545 symbol overrides the old symbol. Symbols from regular files
1546 always take precedence over symbols from dynamic objects, even if
1547 they are defined after the dynamic object in the link.
1549 As above, we again permit a common symbol in a regular object to
1550 override a definition in a shared object if the shared object
1551 symbol is a function or is weak. */
1556 || (bfd_is_com_section (sec)
1557 && (oldweak || oldfunc)))
1562 /* Change the hash table entry to undefined, and let
1563 _bfd_generic_link_add_one_symbol do the right thing with the
1566 h->root.type = bfd_link_hash_undefined;
1567 h->root.u.undef.abfd = h->root.u.def.section->owner;
1568 *size_change_ok = TRUE;
1571 olddyncommon = FALSE;
1573 /* We again permit a type change when a common symbol may be
1574 overriding a function. */
1576 if (bfd_is_com_section (sec))
1580 /* If a common symbol overrides a function, make sure
1581 that it isn't defined dynamically nor has type
1584 h->type = STT_NOTYPE;
1586 *type_change_ok = TRUE;
1589 if (hi->root.type == bfd_link_hash_indirect)
1592 /* This union may have been set to be non-NULL when this symbol
1593 was seen in a dynamic object. We must force the union to be
1594 NULL, so that it is correct for a regular symbol. */
1595 h->verinfo.vertree = NULL;
1598 /* Handle the special case of a new common symbol merging with an
1599 old symbol that looks like it might be a common symbol defined in
1600 a shared object. Note that we have already handled the case in
1601 which a new common symbol should simply override the definition
1602 in the shared library. */
1605 && bfd_is_com_section (sec)
1608 /* It would be best if we could set the hash table entry to a
1609 common symbol, but we don't know what to use for the section
1610 or the alignment. */
1611 if (! ((*info->callbacks->multiple_common)
1612 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1615 /* If the presumed common symbol in the dynamic object is
1616 larger, pretend that the new symbol has its size. */
1618 if (h->size > *pvalue)
1621 /* We need to remember the alignment required by the symbol
1622 in the dynamic object. */
1623 BFD_ASSERT (pold_alignment);
1624 *pold_alignment = h->root.u.def.section->alignment_power;
1627 olddyncommon = FALSE;
1629 h->root.type = bfd_link_hash_undefined;
1630 h->root.u.undef.abfd = h->root.u.def.section->owner;
1632 *size_change_ok = TRUE;
1633 *type_change_ok = TRUE;
1635 if (hi->root.type == bfd_link_hash_indirect)
1638 h->verinfo.vertree = NULL;
1643 /* Handle the case where we had a versioned symbol in a dynamic
1644 library and now find a definition in a normal object. In this
1645 case, we make the versioned symbol point to the normal one. */
1646 flip->root.type = h->root.type;
1647 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1648 h->root.type = bfd_link_hash_indirect;
1649 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1650 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1654 flip->ref_dynamic = 1;
1661 /* This function is called to create an indirect symbol from the
1662 default for the symbol with the default version if needed. The
1663 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1664 set DYNSYM if the new indirect symbol is dynamic. */
1667 _bfd_elf_add_default_symbol (bfd *abfd,
1668 struct bfd_link_info *info,
1669 struct elf_link_hash_entry *h,
1671 Elf_Internal_Sym *sym,
1675 bfd_boolean *dynsym)
1677 bfd_boolean type_change_ok;
1678 bfd_boolean size_change_ok;
1681 struct elf_link_hash_entry *hi;
1682 struct bfd_link_hash_entry *bh;
1683 const struct elf_backend_data *bed;
1684 bfd_boolean collect;
1685 bfd_boolean dynamic;
1686 bfd_boolean override;
1688 size_t len, shortlen;
1690 bfd_boolean matched;
1692 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1695 /* If this symbol has a version, and it is the default version, we
1696 create an indirect symbol from the default name to the fully
1697 decorated name. This will cause external references which do not
1698 specify a version to be bound to this version of the symbol. */
1699 p = strchr (name, ELF_VER_CHR);
1700 if (h->versioned == unknown)
1704 h->versioned = unversioned;
1709 if (p[1] != ELF_VER_CHR)
1711 h->versioned = versioned_hidden;
1715 h->versioned = versioned;
1720 /* PR ld/19073: We may see an unversioned definition after the
1726 bed = get_elf_backend_data (abfd);
1727 collect = bed->collect;
1728 dynamic = (abfd->flags & DYNAMIC) != 0;
1730 shortlen = p - name;
1731 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1732 if (shortname == NULL)
1734 memcpy (shortname, name, shortlen);
1735 shortname[shortlen] = '\0';
1737 /* We are going to create a new symbol. Merge it with any existing
1738 symbol with this name. For the purposes of the merge, act as
1739 though we were defining the symbol we just defined, although we
1740 actually going to define an indirect symbol. */
1741 type_change_ok = FALSE;
1742 size_change_ok = FALSE;
1745 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1746 &hi, poldbfd, NULL, NULL, &skip, &override,
1747 &type_change_ok, &size_change_ok, &matched))
1755 /* Add the default symbol if not performing a relocatable link. */
1756 if (! bfd_link_relocatable (info))
1759 if (! (_bfd_generic_link_add_one_symbol
1760 (info, abfd, shortname, BSF_INDIRECT,
1761 bfd_ind_section_ptr,
1762 0, name, FALSE, collect, &bh)))
1764 hi = (struct elf_link_hash_entry *) bh;
1769 /* In this case the symbol named SHORTNAME is overriding the
1770 indirect symbol we want to add. We were planning on making
1771 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1772 is the name without a version. NAME is the fully versioned
1773 name, and it is the default version.
1775 Overriding means that we already saw a definition for the
1776 symbol SHORTNAME in a regular object, and it is overriding
1777 the symbol defined in the dynamic object.
1779 When this happens, we actually want to change NAME, the
1780 symbol we just added, to refer to SHORTNAME. This will cause
1781 references to NAME in the shared object to become references
1782 to SHORTNAME in the regular object. This is what we expect
1783 when we override a function in a shared object: that the
1784 references in the shared object will be mapped to the
1785 definition in the regular object. */
1787 while (hi->root.type == bfd_link_hash_indirect
1788 || hi->root.type == bfd_link_hash_warning)
1789 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1791 h->root.type = bfd_link_hash_indirect;
1792 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1796 hi->ref_dynamic = 1;
1800 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1805 /* Now set HI to H, so that the following code will set the
1806 other fields correctly. */
1810 /* Check if HI is a warning symbol. */
1811 if (hi->root.type == bfd_link_hash_warning)
1812 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1814 /* If there is a duplicate definition somewhere, then HI may not
1815 point to an indirect symbol. We will have reported an error to
1816 the user in that case. */
1818 if (hi->root.type == bfd_link_hash_indirect)
1820 struct elf_link_hash_entry *ht;
1822 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1823 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1825 /* A reference to the SHORTNAME symbol from a dynamic library
1826 will be satisfied by the versioned symbol at runtime. In
1827 effect, we have a reference to the versioned symbol. */
1828 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1829 hi->dynamic_def |= ht->dynamic_def;
1831 /* See if the new flags lead us to realize that the symbol must
1837 if (! bfd_link_executable (info)
1844 if (hi->ref_regular)
1850 /* We also need to define an indirection from the nondefault version
1854 len = strlen (name);
1855 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1856 if (shortname == NULL)
1858 memcpy (shortname, name, shortlen);
1859 memcpy (shortname + shortlen, p + 1, len - shortlen);
1861 /* Once again, merge with any existing symbol. */
1862 type_change_ok = FALSE;
1863 size_change_ok = FALSE;
1865 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1866 &hi, poldbfd, NULL, NULL, &skip, &override,
1867 &type_change_ok, &size_change_ok, &matched))
1875 /* Here SHORTNAME is a versioned name, so we don't expect to see
1876 the type of override we do in the case above unless it is
1877 overridden by a versioned definition. */
1878 if (hi->root.type != bfd_link_hash_defined
1879 && hi->root.type != bfd_link_hash_defweak)
1880 (*_bfd_error_handler)
1881 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1887 if (! (_bfd_generic_link_add_one_symbol
1888 (info, abfd, shortname, BSF_INDIRECT,
1889 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1891 hi = (struct elf_link_hash_entry *) bh;
1893 /* If there is a duplicate definition somewhere, then HI may not
1894 point to an indirect symbol. We will have reported an error
1895 to the user in that case. */
1897 if (hi->root.type == bfd_link_hash_indirect)
1899 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1900 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1901 hi->dynamic_def |= h->dynamic_def;
1903 /* See if the new flags lead us to realize that the symbol
1909 if (! bfd_link_executable (info)
1915 if (hi->ref_regular)
1925 /* This routine is used to export all defined symbols into the dynamic
1926 symbol table. It is called via elf_link_hash_traverse. */
1929 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1931 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1933 /* Ignore indirect symbols. These are added by the versioning code. */
1934 if (h->root.type == bfd_link_hash_indirect)
1937 /* Ignore this if we won't export it. */
1938 if (!eif->info->export_dynamic && !h->dynamic)
1941 if (h->dynindx == -1
1942 && (h->def_regular || h->ref_regular)
1943 && ! bfd_hide_sym_by_version (eif->info->version_info,
1944 h->root.root.string))
1946 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1956 /* Look through the symbols which are defined in other shared
1957 libraries and referenced here. Update the list of version
1958 dependencies. This will be put into the .gnu.version_r section.
1959 This function is called via elf_link_hash_traverse. */
1962 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1965 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
1966 Elf_Internal_Verneed *t;
1967 Elf_Internal_Vernaux *a;
1970 /* We only care about symbols defined in shared objects with version
1975 || h->verinfo.verdef == NULL
1976 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
1977 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
1980 /* See if we already know about this version. */
1981 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1985 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1988 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1989 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1995 /* This is a new version. Add it to tree we are building. */
2000 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2003 rinfo->failed = TRUE;
2007 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2008 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2009 elf_tdata (rinfo->info->output_bfd)->verref = t;
2013 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2016 rinfo->failed = TRUE;
2020 /* Note that we are copying a string pointer here, and testing it
2021 above. If bfd_elf_string_from_elf_section is ever changed to
2022 discard the string data when low in memory, this will have to be
2024 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2026 a->vna_flags = h->verinfo.verdef->vd_flags;
2027 a->vna_nextptr = t->vn_auxptr;
2029 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2032 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2039 /* Figure out appropriate versions for all the symbols. We may not
2040 have the version number script until we have read all of the input
2041 files, so until that point we don't know which symbols should be
2042 local. This function is called via elf_link_hash_traverse. */
2045 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2047 struct elf_info_failed *sinfo;
2048 struct bfd_link_info *info;
2049 const struct elf_backend_data *bed;
2050 struct elf_info_failed eif;
2054 sinfo = (struct elf_info_failed *) data;
2057 /* Fix the symbol flags. */
2060 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2063 sinfo->failed = TRUE;
2067 /* We only need version numbers for symbols defined in regular
2069 if (!h->def_regular)
2072 bed = get_elf_backend_data (info->output_bfd);
2073 p = strchr (h->root.root.string, ELF_VER_CHR);
2074 if (p != NULL && h->verinfo.vertree == NULL)
2076 struct bfd_elf_version_tree *t;
2079 if (*p == ELF_VER_CHR)
2082 /* If there is no version string, we can just return out. */
2086 /* Look for the version. If we find it, it is no longer weak. */
2087 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2089 if (strcmp (t->name, p) == 0)
2093 struct bfd_elf_version_expr *d;
2095 len = p - h->root.root.string;
2096 alc = (char *) bfd_malloc (len);
2099 sinfo->failed = TRUE;
2102 memcpy (alc, h->root.root.string, len - 1);
2103 alc[len - 1] = '\0';
2104 if (alc[len - 2] == ELF_VER_CHR)
2105 alc[len - 2] = '\0';
2107 h->verinfo.vertree = t;
2111 if (t->globals.list != NULL)
2112 d = (*t->match) (&t->globals, NULL, alc);
2114 /* See if there is anything to force this symbol to
2116 if (d == NULL && t->locals.list != NULL)
2118 d = (*t->match) (&t->locals, NULL, alc);
2121 && ! info->export_dynamic)
2122 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2130 /* If we are building an application, we need to create a
2131 version node for this version. */
2132 if (t == NULL && bfd_link_executable (info))
2134 struct bfd_elf_version_tree **pp;
2137 /* If we aren't going to export this symbol, we don't need
2138 to worry about it. */
2139 if (h->dynindx == -1)
2143 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2146 sinfo->failed = TRUE;
2151 t->name_indx = (unsigned int) -1;
2155 /* Don't count anonymous version tag. */
2156 if (sinfo->info->version_info != NULL
2157 && sinfo->info->version_info->vernum == 0)
2159 for (pp = &sinfo->info->version_info;
2163 t->vernum = version_index;
2167 h->verinfo.vertree = t;
2171 /* We could not find the version for a symbol when
2172 generating a shared archive. Return an error. */
2173 (*_bfd_error_handler)
2174 (_("%B: version node not found for symbol %s"),
2175 info->output_bfd, h->root.root.string);
2176 bfd_set_error (bfd_error_bad_value);
2177 sinfo->failed = TRUE;
2182 /* If we don't have a version for this symbol, see if we can find
2184 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2189 = bfd_find_version_for_sym (sinfo->info->version_info,
2190 h->root.root.string, &hide);
2191 if (h->verinfo.vertree != NULL && hide)
2192 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2198 /* Read and swap the relocs from the section indicated by SHDR. This
2199 may be either a REL or a RELA section. The relocations are
2200 translated into RELA relocations and stored in INTERNAL_RELOCS,
2201 which should have already been allocated to contain enough space.
2202 The EXTERNAL_RELOCS are a buffer where the external form of the
2203 relocations should be stored.
2205 Returns FALSE if something goes wrong. */
2208 elf_link_read_relocs_from_section (bfd *abfd,
2210 Elf_Internal_Shdr *shdr,
2211 void *external_relocs,
2212 Elf_Internal_Rela *internal_relocs)
2214 const struct elf_backend_data *bed;
2215 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2216 const bfd_byte *erela;
2217 const bfd_byte *erelaend;
2218 Elf_Internal_Rela *irela;
2219 Elf_Internal_Shdr *symtab_hdr;
2222 /* Position ourselves at the start of the section. */
2223 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2226 /* Read the relocations. */
2227 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2230 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2231 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2233 bed = get_elf_backend_data (abfd);
2235 /* Convert the external relocations to the internal format. */
2236 if (shdr->sh_entsize == bed->s->sizeof_rel)
2237 swap_in = bed->s->swap_reloc_in;
2238 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2239 swap_in = bed->s->swap_reloca_in;
2242 bfd_set_error (bfd_error_wrong_format);
2246 erela = (const bfd_byte *) external_relocs;
2247 erelaend = erela + shdr->sh_size;
2248 irela = internal_relocs;
2249 while (erela < erelaend)
2253 (*swap_in) (abfd, erela, irela);
2254 r_symndx = ELF32_R_SYM (irela->r_info);
2255 if (bed->s->arch_size == 64)
2259 if ((size_t) r_symndx >= nsyms)
2261 (*_bfd_error_handler)
2262 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2263 " for offset 0x%lx in section `%A'"),
2265 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2266 bfd_set_error (bfd_error_bad_value);
2270 else if (r_symndx != STN_UNDEF)
2272 (*_bfd_error_handler)
2273 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2274 " when the object file has no symbol table"),
2276 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2277 bfd_set_error (bfd_error_bad_value);
2280 irela += bed->s->int_rels_per_ext_rel;
2281 erela += shdr->sh_entsize;
2287 /* Read and swap the relocs for a section O. They may have been
2288 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2289 not NULL, they are used as buffers to read into. They are known to
2290 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2291 the return value is allocated using either malloc or bfd_alloc,
2292 according to the KEEP_MEMORY argument. If O has two relocation
2293 sections (both REL and RELA relocations), then the REL_HDR
2294 relocations will appear first in INTERNAL_RELOCS, followed by the
2295 RELA_HDR relocations. */
2298 _bfd_elf_link_read_relocs (bfd *abfd,
2300 void *external_relocs,
2301 Elf_Internal_Rela *internal_relocs,
2302 bfd_boolean keep_memory)
2304 void *alloc1 = NULL;
2305 Elf_Internal_Rela *alloc2 = NULL;
2306 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2307 struct bfd_elf_section_data *esdo = elf_section_data (o);
2308 Elf_Internal_Rela *internal_rela_relocs;
2310 if (esdo->relocs != NULL)
2311 return esdo->relocs;
2313 if (o->reloc_count == 0)
2316 if (internal_relocs == NULL)
2320 size = o->reloc_count;
2321 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2323 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2325 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2326 if (internal_relocs == NULL)
2330 if (external_relocs == NULL)
2332 bfd_size_type size = 0;
2335 size += esdo->rel.hdr->sh_size;
2337 size += esdo->rela.hdr->sh_size;
2339 alloc1 = bfd_malloc (size);
2342 external_relocs = alloc1;
2345 internal_rela_relocs = internal_relocs;
2348 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2352 external_relocs = (((bfd_byte *) external_relocs)
2353 + esdo->rel.hdr->sh_size);
2354 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2355 * bed->s->int_rels_per_ext_rel);
2359 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2361 internal_rela_relocs)))
2364 /* Cache the results for next time, if we can. */
2366 esdo->relocs = internal_relocs;
2371 /* Don't free alloc2, since if it was allocated we are passing it
2372 back (under the name of internal_relocs). */
2374 return internal_relocs;
2382 bfd_release (abfd, alloc2);
2389 /* Compute the size of, and allocate space for, REL_HDR which is the
2390 section header for a section containing relocations for O. */
2393 _bfd_elf_link_size_reloc_section (bfd *abfd,
2394 struct bfd_elf_section_reloc_data *reldata)
2396 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2398 /* That allows us to calculate the size of the section. */
2399 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2401 /* The contents field must last into write_object_contents, so we
2402 allocate it with bfd_alloc rather than malloc. Also since we
2403 cannot be sure that the contents will actually be filled in,
2404 we zero the allocated space. */
2405 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2406 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2409 if (reldata->hashes == NULL && reldata->count)
2411 struct elf_link_hash_entry **p;
2413 p = ((struct elf_link_hash_entry **)
2414 bfd_zmalloc (reldata->count * sizeof (*p)));
2418 reldata->hashes = p;
2424 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2425 originated from the section given by INPUT_REL_HDR) to the
2429 _bfd_elf_link_output_relocs (bfd *output_bfd,
2430 asection *input_section,
2431 Elf_Internal_Shdr *input_rel_hdr,
2432 Elf_Internal_Rela *internal_relocs,
2433 struct elf_link_hash_entry **rel_hash
2436 Elf_Internal_Rela *irela;
2437 Elf_Internal_Rela *irelaend;
2439 struct bfd_elf_section_reloc_data *output_reldata;
2440 asection *output_section;
2441 const struct elf_backend_data *bed;
2442 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2443 struct bfd_elf_section_data *esdo;
2445 output_section = input_section->output_section;
2447 bed = get_elf_backend_data (output_bfd);
2448 esdo = elf_section_data (output_section);
2449 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2451 output_reldata = &esdo->rel;
2452 swap_out = bed->s->swap_reloc_out;
2454 else if (esdo->rela.hdr
2455 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2457 output_reldata = &esdo->rela;
2458 swap_out = bed->s->swap_reloca_out;
2462 (*_bfd_error_handler)
2463 (_("%B: relocation size mismatch in %B section %A"),
2464 output_bfd, input_section->owner, input_section);
2465 bfd_set_error (bfd_error_wrong_format);
2469 erel = output_reldata->hdr->contents;
2470 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2471 irela = internal_relocs;
2472 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2473 * bed->s->int_rels_per_ext_rel);
2474 while (irela < irelaend)
2476 (*swap_out) (output_bfd, irela, erel);
2477 irela += bed->s->int_rels_per_ext_rel;
2478 erel += input_rel_hdr->sh_entsize;
2481 /* Bump the counter, so that we know where to add the next set of
2483 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2488 /* Make weak undefined symbols in PIE dynamic. */
2491 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2492 struct elf_link_hash_entry *h)
2494 if (bfd_link_pie (info)
2496 && h->root.type == bfd_link_hash_undefweak)
2497 return bfd_elf_link_record_dynamic_symbol (info, h);
2502 /* Fix up the flags for a symbol. This handles various cases which
2503 can only be fixed after all the input files are seen. This is
2504 currently called by both adjust_dynamic_symbol and
2505 assign_sym_version, which is unnecessary but perhaps more robust in
2506 the face of future changes. */
2509 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2510 struct elf_info_failed *eif)
2512 const struct elf_backend_data *bed;
2514 /* If this symbol was mentioned in a non-ELF file, try to set
2515 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2516 permit a non-ELF file to correctly refer to a symbol defined in
2517 an ELF dynamic object. */
2520 while (h->root.type == bfd_link_hash_indirect)
2521 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2523 if (h->root.type != bfd_link_hash_defined
2524 && h->root.type != bfd_link_hash_defweak)
2527 h->ref_regular_nonweak = 1;
2531 if (h->root.u.def.section->owner != NULL
2532 && (bfd_get_flavour (h->root.u.def.section->owner)
2533 == bfd_target_elf_flavour))
2536 h->ref_regular_nonweak = 1;
2542 if (h->dynindx == -1
2546 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2555 /* Unfortunately, NON_ELF is only correct if the symbol
2556 was first seen in a non-ELF file. Fortunately, if the symbol
2557 was first seen in an ELF file, we're probably OK unless the
2558 symbol was defined in a non-ELF file. Catch that case here.
2559 FIXME: We're still in trouble if the symbol was first seen in
2560 a dynamic object, and then later in a non-ELF regular object. */
2561 if ((h->root.type == bfd_link_hash_defined
2562 || h->root.type == bfd_link_hash_defweak)
2564 && (h->root.u.def.section->owner != NULL
2565 ? (bfd_get_flavour (h->root.u.def.section->owner)
2566 != bfd_target_elf_flavour)
2567 : (bfd_is_abs_section (h->root.u.def.section)
2568 && !h->def_dynamic)))
2572 /* Backend specific symbol fixup. */
2573 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2574 if (bed->elf_backend_fixup_symbol
2575 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2578 /* If this is a final link, and the symbol was defined as a common
2579 symbol in a regular object file, and there was no definition in
2580 any dynamic object, then the linker will have allocated space for
2581 the symbol in a common section but the DEF_REGULAR
2582 flag will not have been set. */
2583 if (h->root.type == bfd_link_hash_defined
2587 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2590 /* If -Bsymbolic was used (which means to bind references to global
2591 symbols to the definition within the shared object), and this
2592 symbol was defined in a regular object, then it actually doesn't
2593 need a PLT entry. Likewise, if the symbol has non-default
2594 visibility. If the symbol has hidden or internal visibility, we
2595 will force it local. */
2597 && bfd_link_pic (eif->info)
2598 && is_elf_hash_table (eif->info->hash)
2599 && (SYMBOLIC_BIND (eif->info, h)
2600 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2603 bfd_boolean force_local;
2605 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2606 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2607 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2610 /* If a weak undefined symbol has non-default visibility, we also
2611 hide it from the dynamic linker. */
2612 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2613 && h->root.type == bfd_link_hash_undefweak)
2614 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2616 /* If this is a weak defined symbol in a dynamic object, and we know
2617 the real definition in the dynamic object, copy interesting flags
2618 over to the real definition. */
2619 if (h->u.weakdef != NULL)
2621 /* If the real definition is defined by a regular object file,
2622 don't do anything special. See the longer description in
2623 _bfd_elf_adjust_dynamic_symbol, below. */
2624 if (h->u.weakdef->def_regular)
2625 h->u.weakdef = NULL;
2628 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2630 while (h->root.type == bfd_link_hash_indirect)
2631 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2633 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2634 || h->root.type == bfd_link_hash_defweak);
2635 BFD_ASSERT (weakdef->def_dynamic);
2636 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2637 || weakdef->root.type == bfd_link_hash_defweak);
2638 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2645 /* Make the backend pick a good value for a dynamic symbol. This is
2646 called via elf_link_hash_traverse, and also calls itself
2650 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2652 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2654 const struct elf_backend_data *bed;
2656 if (! is_elf_hash_table (eif->info->hash))
2659 /* Ignore indirect symbols. These are added by the versioning code. */
2660 if (h->root.type == bfd_link_hash_indirect)
2663 /* Fix the symbol flags. */
2664 if (! _bfd_elf_fix_symbol_flags (h, eif))
2667 /* If this symbol does not require a PLT entry, and it is not
2668 defined by a dynamic object, or is not referenced by a regular
2669 object, ignore it. We do have to handle a weak defined symbol,
2670 even if no regular object refers to it, if we decided to add it
2671 to the dynamic symbol table. FIXME: Do we normally need to worry
2672 about symbols which are defined by one dynamic object and
2673 referenced by another one? */
2675 && h->type != STT_GNU_IFUNC
2679 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2681 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2685 /* If we've already adjusted this symbol, don't do it again. This
2686 can happen via a recursive call. */
2687 if (h->dynamic_adjusted)
2690 /* Don't look at this symbol again. Note that we must set this
2691 after checking the above conditions, because we may look at a
2692 symbol once, decide not to do anything, and then get called
2693 recursively later after REF_REGULAR is set below. */
2694 h->dynamic_adjusted = 1;
2696 /* If this is a weak definition, and we know a real definition, and
2697 the real symbol is not itself defined by a regular object file,
2698 then get a good value for the real definition. We handle the
2699 real symbol first, for the convenience of the backend routine.
2701 Note that there is a confusing case here. If the real definition
2702 is defined by a regular object file, we don't get the real symbol
2703 from the dynamic object, but we do get the weak symbol. If the
2704 processor backend uses a COPY reloc, then if some routine in the
2705 dynamic object changes the real symbol, we will not see that
2706 change in the corresponding weak symbol. This is the way other
2707 ELF linkers work as well, and seems to be a result of the shared
2710 I will clarify this issue. Most SVR4 shared libraries define the
2711 variable _timezone and define timezone as a weak synonym. The
2712 tzset call changes _timezone. If you write
2713 extern int timezone;
2715 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2716 you might expect that, since timezone is a synonym for _timezone,
2717 the same number will print both times. However, if the processor
2718 backend uses a COPY reloc, then actually timezone will be copied
2719 into your process image, and, since you define _timezone
2720 yourself, _timezone will not. Thus timezone and _timezone will
2721 wind up at different memory locations. The tzset call will set
2722 _timezone, leaving timezone unchanged. */
2724 if (h->u.weakdef != NULL)
2726 /* If we get to this point, there is an implicit reference to
2727 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2728 h->u.weakdef->ref_regular = 1;
2730 /* Ensure that the backend adjust_dynamic_symbol function sees
2731 H->U.WEAKDEF before H by recursively calling ourselves. */
2732 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2736 /* If a symbol has no type and no size and does not require a PLT
2737 entry, then we are probably about to do the wrong thing here: we
2738 are probably going to create a COPY reloc for an empty object.
2739 This case can arise when a shared object is built with assembly
2740 code, and the assembly code fails to set the symbol type. */
2742 && h->type == STT_NOTYPE
2744 (*_bfd_error_handler)
2745 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2746 h->root.root.string);
2748 dynobj = elf_hash_table (eif->info)->dynobj;
2749 bed = get_elf_backend_data (dynobj);
2751 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2760 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2764 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2765 struct elf_link_hash_entry *h,
2768 unsigned int power_of_two;
2770 asection *sec = h->root.u.def.section;
2772 /* The section aligment of definition is the maximum alignment
2773 requirement of symbols defined in the section. Since we don't
2774 know the symbol alignment requirement, we start with the
2775 maximum alignment and check low bits of the symbol address
2776 for the minimum alignment. */
2777 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2778 mask = ((bfd_vma) 1 << power_of_two) - 1;
2779 while ((h->root.u.def.value & mask) != 0)
2785 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2788 /* Adjust the section alignment if needed. */
2789 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2794 /* We make sure that the symbol will be aligned properly. */
2795 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2797 /* Define the symbol as being at this point in DYNBSS. */
2798 h->root.u.def.section = dynbss;
2799 h->root.u.def.value = dynbss->size;
2801 /* Increment the size of DYNBSS to make room for the symbol. */
2802 dynbss->size += h->size;
2804 /* No error if extern_protected_data is true. */
2805 if (h->protected_def
2806 && (!info->extern_protected_data
2807 || (info->extern_protected_data < 0
2808 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2809 info->callbacks->einfo
2810 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2811 h->root.root.string);
2816 /* Adjust all external symbols pointing into SEC_MERGE sections
2817 to reflect the object merging within the sections. */
2820 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2824 if ((h->root.type == bfd_link_hash_defined
2825 || h->root.type == bfd_link_hash_defweak)
2826 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2827 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2829 bfd *output_bfd = (bfd *) data;
2831 h->root.u.def.value =
2832 _bfd_merged_section_offset (output_bfd,
2833 &h->root.u.def.section,
2834 elf_section_data (sec)->sec_info,
2835 h->root.u.def.value);
2841 /* Returns false if the symbol referred to by H should be considered
2842 to resolve local to the current module, and true if it should be
2843 considered to bind dynamically. */
2846 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2847 struct bfd_link_info *info,
2848 bfd_boolean not_local_protected)
2850 bfd_boolean binding_stays_local_p;
2851 const struct elf_backend_data *bed;
2852 struct elf_link_hash_table *hash_table;
2857 while (h->root.type == bfd_link_hash_indirect
2858 || h->root.type == bfd_link_hash_warning)
2859 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2861 /* If it was forced local, then clearly it's not dynamic. */
2862 if (h->dynindx == -1)
2864 if (h->forced_local)
2867 /* Identify the cases where name binding rules say that a
2868 visible symbol resolves locally. */
2869 binding_stays_local_p = (bfd_link_executable (info)
2870 || SYMBOLIC_BIND (info, h));
2872 switch (ELF_ST_VISIBILITY (h->other))
2879 hash_table = elf_hash_table (info);
2880 if (!is_elf_hash_table (hash_table))
2883 bed = get_elf_backend_data (hash_table->dynobj);
2885 /* Proper resolution for function pointer equality may require
2886 that these symbols perhaps be resolved dynamically, even though
2887 we should be resolving them to the current module. */
2888 if (!not_local_protected || !bed->is_function_type (h->type))
2889 binding_stays_local_p = TRUE;
2896 /* If it isn't defined locally, then clearly it's dynamic. */
2897 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2900 /* Otherwise, the symbol is dynamic if binding rules don't tell
2901 us that it remains local. */
2902 return !binding_stays_local_p;
2905 /* Return true if the symbol referred to by H should be considered
2906 to resolve local to the current module, and false otherwise. Differs
2907 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2908 undefined symbols. The two functions are virtually identical except
2909 for the place where forced_local and dynindx == -1 are tested. If
2910 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2911 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2912 the symbol is local only for defined symbols.
2913 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2914 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2915 treatment of undefined weak symbols. For those that do not make
2916 undefined weak symbols dynamic, both functions may return false. */
2919 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2920 struct bfd_link_info *info,
2921 bfd_boolean local_protected)
2923 const struct elf_backend_data *bed;
2924 struct elf_link_hash_table *hash_table;
2926 /* If it's a local sym, of course we resolve locally. */
2930 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2931 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2932 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2935 /* Common symbols that become definitions don't get the DEF_REGULAR
2936 flag set, so test it first, and don't bail out. */
2937 if (ELF_COMMON_DEF_P (h))
2939 /* If we don't have a definition in a regular file, then we can't
2940 resolve locally. The sym is either undefined or dynamic. */
2941 else if (!h->def_regular)
2944 /* Forced local symbols resolve locally. */
2945 if (h->forced_local)
2948 /* As do non-dynamic symbols. */
2949 if (h->dynindx == -1)
2952 /* At this point, we know the symbol is defined and dynamic. In an
2953 executable it must resolve locally, likewise when building symbolic
2954 shared libraries. */
2955 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
2958 /* Now deal with defined dynamic symbols in shared libraries. Ones
2959 with default visibility might not resolve locally. */
2960 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2963 hash_table = elf_hash_table (info);
2964 if (!is_elf_hash_table (hash_table))
2967 bed = get_elf_backend_data (hash_table->dynobj);
2969 /* If extern_protected_data is false, STV_PROTECTED non-function
2970 symbols are local. */
2971 if ((!info->extern_protected_data
2972 || (info->extern_protected_data < 0
2973 && !bed->extern_protected_data))
2974 && !bed->is_function_type (h->type))
2977 /* Function pointer equality tests may require that STV_PROTECTED
2978 symbols be treated as dynamic symbols. If the address of a
2979 function not defined in an executable is set to that function's
2980 plt entry in the executable, then the address of the function in
2981 a shared library must also be the plt entry in the executable. */
2982 return local_protected;
2985 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2986 aligned. Returns the first TLS output section. */
2988 struct bfd_section *
2989 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2991 struct bfd_section *sec, *tls;
2992 unsigned int align = 0;
2994 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2995 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2999 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3000 if (sec->alignment_power > align)
3001 align = sec->alignment_power;
3003 elf_hash_table (info)->tls_sec = tls;
3005 /* Ensure the alignment of the first section is the largest alignment,
3006 so that the tls segment starts aligned. */
3008 tls->alignment_power = align;
3013 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3015 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3016 Elf_Internal_Sym *sym)
3018 const struct elf_backend_data *bed;
3020 /* Local symbols do not count, but target specific ones might. */
3021 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3022 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3025 bed = get_elf_backend_data (abfd);
3026 /* Function symbols do not count. */
3027 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3030 /* If the section is undefined, then so is the symbol. */
3031 if (sym->st_shndx == SHN_UNDEF)
3034 /* If the symbol is defined in the common section, then
3035 it is a common definition and so does not count. */
3036 if (bed->common_definition (sym))
3039 /* If the symbol is in a target specific section then we
3040 must rely upon the backend to tell us what it is. */
3041 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3042 /* FIXME - this function is not coded yet:
3044 return _bfd_is_global_symbol_definition (abfd, sym);
3046 Instead for now assume that the definition is not global,
3047 Even if this is wrong, at least the linker will behave
3048 in the same way that it used to do. */
3054 /* Search the symbol table of the archive element of the archive ABFD
3055 whose archive map contains a mention of SYMDEF, and determine if
3056 the symbol is defined in this element. */
3058 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3060 Elf_Internal_Shdr * hdr;
3061 bfd_size_type symcount;
3062 bfd_size_type extsymcount;
3063 bfd_size_type extsymoff;
3064 Elf_Internal_Sym *isymbuf;
3065 Elf_Internal_Sym *isym;
3066 Elf_Internal_Sym *isymend;
3069 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3073 /* Return FALSE if the object has been claimed by plugin. */
3074 if (abfd->plugin_format == bfd_plugin_yes)
3077 if (! bfd_check_format (abfd, bfd_object))
3080 /* Select the appropriate symbol table. */
3081 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3082 hdr = &elf_tdata (abfd)->symtab_hdr;
3084 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3086 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3088 /* The sh_info field of the symtab header tells us where the
3089 external symbols start. We don't care about the local symbols. */
3090 if (elf_bad_symtab (abfd))
3092 extsymcount = symcount;
3097 extsymcount = symcount - hdr->sh_info;
3098 extsymoff = hdr->sh_info;
3101 if (extsymcount == 0)
3104 /* Read in the symbol table. */
3105 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3107 if (isymbuf == NULL)
3110 /* Scan the symbol table looking for SYMDEF. */
3112 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3116 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3121 if (strcmp (name, symdef->name) == 0)
3123 result = is_global_data_symbol_definition (abfd, isym);
3133 /* Add an entry to the .dynamic table. */
3136 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3140 struct elf_link_hash_table *hash_table;
3141 const struct elf_backend_data *bed;
3143 bfd_size_type newsize;
3144 bfd_byte *newcontents;
3145 Elf_Internal_Dyn dyn;
3147 hash_table = elf_hash_table (info);
3148 if (! is_elf_hash_table (hash_table))
3151 bed = get_elf_backend_data (hash_table->dynobj);
3152 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3153 BFD_ASSERT (s != NULL);
3155 newsize = s->size + bed->s->sizeof_dyn;
3156 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3157 if (newcontents == NULL)
3161 dyn.d_un.d_val = val;
3162 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3165 s->contents = newcontents;
3170 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3171 otherwise just check whether one already exists. Returns -1 on error,
3172 1 if a DT_NEEDED tag already exists, and 0 on success. */
3175 elf_add_dt_needed_tag (bfd *abfd,
3176 struct bfd_link_info *info,
3180 struct elf_link_hash_table *hash_table;
3181 bfd_size_type strindex;
3183 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3186 hash_table = elf_hash_table (info);
3187 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3188 if (strindex == (bfd_size_type) -1)
3191 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3194 const struct elf_backend_data *bed;
3197 bed = get_elf_backend_data (hash_table->dynobj);
3198 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3200 for (extdyn = sdyn->contents;
3201 extdyn < sdyn->contents + sdyn->size;
3202 extdyn += bed->s->sizeof_dyn)
3204 Elf_Internal_Dyn dyn;
3206 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3207 if (dyn.d_tag == DT_NEEDED
3208 && dyn.d_un.d_val == strindex)
3210 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3218 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3221 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3225 /* We were just checking for existence of the tag. */
3226 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3232 on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3234 for (; needed != NULL; needed = needed->next)
3235 if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
3236 && strcmp (soname, needed->name) == 0)
3242 /* Sort symbol by value, section, and size. */
3244 elf_sort_symbol (const void *arg1, const void *arg2)
3246 const struct elf_link_hash_entry *h1;
3247 const struct elf_link_hash_entry *h2;
3248 bfd_signed_vma vdiff;
3250 h1 = *(const struct elf_link_hash_entry **) arg1;
3251 h2 = *(const struct elf_link_hash_entry **) arg2;
3252 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3254 return vdiff > 0 ? 1 : -1;
3257 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3259 return sdiff > 0 ? 1 : -1;
3261 vdiff = h1->size - h2->size;
3262 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3265 /* This function is used to adjust offsets into .dynstr for
3266 dynamic symbols. This is called via elf_link_hash_traverse. */
3269 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3271 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3273 if (h->dynindx != -1)
3274 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3278 /* Assign string offsets in .dynstr, update all structures referencing
3282 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3284 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3285 struct elf_link_local_dynamic_entry *entry;
3286 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3287 bfd *dynobj = hash_table->dynobj;
3290 const struct elf_backend_data *bed;
3293 _bfd_elf_strtab_finalize (dynstr);
3294 size = _bfd_elf_strtab_size (dynstr);
3296 bed = get_elf_backend_data (dynobj);
3297 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3298 BFD_ASSERT (sdyn != NULL);
3300 /* Update all .dynamic entries referencing .dynstr strings. */
3301 for (extdyn = sdyn->contents;
3302 extdyn < sdyn->contents + sdyn->size;
3303 extdyn += bed->s->sizeof_dyn)
3305 Elf_Internal_Dyn dyn;
3307 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3311 dyn.d_un.d_val = size;
3321 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3326 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3329 /* Now update local dynamic symbols. */
3330 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3331 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3332 entry->isym.st_name);
3334 /* And the rest of dynamic symbols. */
3335 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3337 /* Adjust version definitions. */
3338 if (elf_tdata (output_bfd)->cverdefs)
3343 Elf_Internal_Verdef def;
3344 Elf_Internal_Verdaux defaux;
3346 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3350 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3352 p += sizeof (Elf_External_Verdef);
3353 if (def.vd_aux != sizeof (Elf_External_Verdef))
3355 for (i = 0; i < def.vd_cnt; ++i)
3357 _bfd_elf_swap_verdaux_in (output_bfd,
3358 (Elf_External_Verdaux *) p, &defaux);
3359 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3361 _bfd_elf_swap_verdaux_out (output_bfd,
3362 &defaux, (Elf_External_Verdaux *) p);
3363 p += sizeof (Elf_External_Verdaux);
3366 while (def.vd_next);
3369 /* Adjust version references. */
3370 if (elf_tdata (output_bfd)->verref)
3375 Elf_Internal_Verneed need;
3376 Elf_Internal_Vernaux needaux;
3378 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3382 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3384 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3385 _bfd_elf_swap_verneed_out (output_bfd, &need,
3386 (Elf_External_Verneed *) p);
3387 p += sizeof (Elf_External_Verneed);
3388 for (i = 0; i < need.vn_cnt; ++i)
3390 _bfd_elf_swap_vernaux_in (output_bfd,
3391 (Elf_External_Vernaux *) p, &needaux);
3392 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3394 _bfd_elf_swap_vernaux_out (output_bfd,
3396 (Elf_External_Vernaux *) p);
3397 p += sizeof (Elf_External_Vernaux);
3400 while (need.vn_next);
3406 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3407 The default is to only match when the INPUT and OUTPUT are exactly
3411 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3412 const bfd_target *output)
3414 return input == output;
3417 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3418 This version is used when different targets for the same architecture
3419 are virtually identical. */
3422 _bfd_elf_relocs_compatible (const bfd_target *input,
3423 const bfd_target *output)
3425 const struct elf_backend_data *obed, *ibed;
3427 if (input == output)
3430 ibed = xvec_get_elf_backend_data (input);
3431 obed = xvec_get_elf_backend_data (output);
3433 if (ibed->arch != obed->arch)
3436 /* If both backends are using this function, deem them compatible. */
3437 return ibed->relocs_compatible == obed->relocs_compatible;
3440 /* Make a special call to the linker "notice" function to tell it that
3441 we are about to handle an as-needed lib, or have finished
3442 processing the lib. */
3445 _bfd_elf_notice_as_needed (bfd *ibfd,
3446 struct bfd_link_info *info,
3447 enum notice_asneeded_action act)
3449 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3452 /* Add symbols from an ELF object file to the linker hash table. */
3455 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3457 Elf_Internal_Ehdr *ehdr;
3458 Elf_Internal_Shdr *hdr;
3459 bfd_size_type symcount;
3460 bfd_size_type extsymcount;
3461 bfd_size_type extsymoff;
3462 struct elf_link_hash_entry **sym_hash;
3463 bfd_boolean dynamic;
3464 Elf_External_Versym *extversym = NULL;
3465 Elf_External_Versym *ever;
3466 struct elf_link_hash_entry *weaks;
3467 struct elf_link_hash_entry **nondeflt_vers = NULL;
3468 bfd_size_type nondeflt_vers_cnt = 0;
3469 Elf_Internal_Sym *isymbuf = NULL;
3470 Elf_Internal_Sym *isym;
3471 Elf_Internal_Sym *isymend;
3472 const struct elf_backend_data *bed;
3473 bfd_boolean add_needed;
3474 struct elf_link_hash_table *htab;
3476 void *alloc_mark = NULL;
3477 struct bfd_hash_entry **old_table = NULL;
3478 unsigned int old_size = 0;
3479 unsigned int old_count = 0;
3480 void *old_tab = NULL;
3482 struct bfd_link_hash_entry *old_undefs = NULL;
3483 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3484 long old_dynsymcount = 0;
3485 bfd_size_type old_dynstr_size = 0;
3488 bfd_boolean just_syms;
3490 htab = elf_hash_table (info);
3491 bed = get_elf_backend_data (abfd);
3493 if ((abfd->flags & DYNAMIC) == 0)
3499 /* You can't use -r against a dynamic object. Also, there's no
3500 hope of using a dynamic object which does not exactly match
3501 the format of the output file. */
3502 if (bfd_link_relocatable (info)
3503 || !is_elf_hash_table (htab)
3504 || info->output_bfd->xvec != abfd->xvec)
3506 if (bfd_link_relocatable (info))
3507 bfd_set_error (bfd_error_invalid_operation);
3509 bfd_set_error (bfd_error_wrong_format);
3514 ehdr = elf_elfheader (abfd);
3515 if (info->warn_alternate_em
3516 && bed->elf_machine_code != ehdr->e_machine
3517 && ((bed->elf_machine_alt1 != 0
3518 && ehdr->e_machine == bed->elf_machine_alt1)
3519 || (bed->elf_machine_alt2 != 0
3520 && ehdr->e_machine == bed->elf_machine_alt2)))
3521 info->callbacks->einfo
3522 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3523 ehdr->e_machine, abfd, bed->elf_machine_code);
3525 /* As a GNU extension, any input sections which are named
3526 .gnu.warning.SYMBOL are treated as warning symbols for the given
3527 symbol. This differs from .gnu.warning sections, which generate
3528 warnings when they are included in an output file. */
3529 /* PR 12761: Also generate this warning when building shared libraries. */
3530 for (s = abfd->sections; s != NULL; s = s->next)
3534 name = bfd_get_section_name (abfd, s);
3535 if (CONST_STRNEQ (name, ".gnu.warning."))
3540 name += sizeof ".gnu.warning." - 1;
3542 /* If this is a shared object, then look up the symbol
3543 in the hash table. If it is there, and it is already
3544 been defined, then we will not be using the entry
3545 from this shared object, so we don't need to warn.
3546 FIXME: If we see the definition in a regular object
3547 later on, we will warn, but we shouldn't. The only
3548 fix is to keep track of what warnings we are supposed
3549 to emit, and then handle them all at the end of the
3553 struct elf_link_hash_entry *h;
3555 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3557 /* FIXME: What about bfd_link_hash_common? */
3559 && (h->root.type == bfd_link_hash_defined
3560 || h->root.type == bfd_link_hash_defweak))
3565 msg = (char *) bfd_alloc (abfd, sz + 1);
3569 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3574 if (! (_bfd_generic_link_add_one_symbol
3575 (info, abfd, name, BSF_WARNING, s, 0, msg,
3576 FALSE, bed->collect, NULL)))
3579 if (bfd_link_executable (info))
3581 /* Clobber the section size so that the warning does
3582 not get copied into the output file. */
3585 /* Also set SEC_EXCLUDE, so that symbols defined in
3586 the warning section don't get copied to the output. */
3587 s->flags |= SEC_EXCLUDE;
3592 just_syms = ((s = abfd->sections) != NULL
3593 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3598 /* If we are creating a shared library, create all the dynamic
3599 sections immediately. We need to attach them to something,
3600 so we attach them to this BFD, provided it is the right
3601 format and is not from ld --just-symbols. FIXME: If there
3602 are no input BFD's of the same format as the output, we can't
3603 make a shared library. */
3605 && bfd_link_pic (info)
3606 && is_elf_hash_table (htab)
3607 && info->output_bfd->xvec == abfd->xvec
3608 && !htab->dynamic_sections_created)
3610 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3614 else if (!is_elf_hash_table (htab))
3618 const char *soname = NULL;
3620 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3623 /* ld --just-symbols and dynamic objects don't mix very well.
3624 ld shouldn't allow it. */
3628 /* If this dynamic lib was specified on the command line with
3629 --as-needed in effect, then we don't want to add a DT_NEEDED
3630 tag unless the lib is actually used. Similary for libs brought
3631 in by another lib's DT_NEEDED. When --no-add-needed is used
3632 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3633 any dynamic library in DT_NEEDED tags in the dynamic lib at
3635 add_needed = (elf_dyn_lib_class (abfd)
3636 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3637 | DYN_NO_NEEDED)) == 0;
3639 s = bfd_get_section_by_name (abfd, ".dynamic");
3644 unsigned int elfsec;
3645 unsigned long shlink;
3647 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3654 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3655 if (elfsec == SHN_BAD)
3656 goto error_free_dyn;
3657 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3659 for (extdyn = dynbuf;
3660 extdyn < dynbuf + s->size;
3661 extdyn += bed->s->sizeof_dyn)
3663 Elf_Internal_Dyn dyn;
3665 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3666 if (dyn.d_tag == DT_SONAME)
3668 unsigned int tagv = dyn.d_un.d_val;
3669 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3671 goto error_free_dyn;
3673 if (dyn.d_tag == DT_NEEDED)
3675 struct bfd_link_needed_list *n, **pn;
3677 unsigned int tagv = dyn.d_un.d_val;
3679 amt = sizeof (struct bfd_link_needed_list);
3680 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3681 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3682 if (n == NULL || fnm == NULL)
3683 goto error_free_dyn;
3684 amt = strlen (fnm) + 1;
3685 anm = (char *) bfd_alloc (abfd, amt);
3687 goto error_free_dyn;
3688 memcpy (anm, fnm, amt);
3692 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3696 if (dyn.d_tag == DT_RUNPATH)
3698 struct bfd_link_needed_list *n, **pn;
3700 unsigned int tagv = dyn.d_un.d_val;
3702 amt = sizeof (struct bfd_link_needed_list);
3703 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3704 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3705 if (n == NULL || fnm == NULL)
3706 goto error_free_dyn;
3707 amt = strlen (fnm) + 1;
3708 anm = (char *) bfd_alloc (abfd, amt);
3710 goto error_free_dyn;
3711 memcpy (anm, fnm, amt);
3715 for (pn = & runpath;
3721 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3722 if (!runpath && dyn.d_tag == DT_RPATH)
3724 struct bfd_link_needed_list *n, **pn;
3726 unsigned int tagv = dyn.d_un.d_val;
3728 amt = sizeof (struct bfd_link_needed_list);
3729 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3730 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3731 if (n == NULL || fnm == NULL)
3732 goto error_free_dyn;
3733 amt = strlen (fnm) + 1;
3734 anm = (char *) bfd_alloc (abfd, amt);
3736 goto error_free_dyn;
3737 memcpy (anm, fnm, amt);
3747 if (dyn.d_tag == DT_AUDIT)
3749 unsigned int tagv = dyn.d_un.d_val;
3750 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3757 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3758 frees all more recently bfd_alloc'd blocks as well. */
3764 struct bfd_link_needed_list **pn;
3765 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3770 /* We do not want to include any of the sections in a dynamic
3771 object in the output file. We hack by simply clobbering the
3772 list of sections in the BFD. This could be handled more
3773 cleanly by, say, a new section flag; the existing
3774 SEC_NEVER_LOAD flag is not the one we want, because that one
3775 still implies that the section takes up space in the output
3777 bfd_section_list_clear (abfd);
3779 /* Find the name to use in a DT_NEEDED entry that refers to this
3780 object. If the object has a DT_SONAME entry, we use it.
3781 Otherwise, if the generic linker stuck something in
3782 elf_dt_name, we use that. Otherwise, we just use the file
3784 if (soname == NULL || *soname == '\0')
3786 soname = elf_dt_name (abfd);
3787 if (soname == NULL || *soname == '\0')
3788 soname = bfd_get_filename (abfd);
3791 /* Save the SONAME because sometimes the linker emulation code
3792 will need to know it. */
3793 elf_dt_name (abfd) = soname;
3795 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3799 /* If we have already included this dynamic object in the
3800 link, just ignore it. There is no reason to include a
3801 particular dynamic object more than once. */
3805 /* Save the DT_AUDIT entry for the linker emulation code. */
3806 elf_dt_audit (abfd) = audit;
3809 /* If this is a dynamic object, we always link against the .dynsym
3810 symbol table, not the .symtab symbol table. The dynamic linker
3811 will only see the .dynsym symbol table, so there is no reason to
3812 look at .symtab for a dynamic object. */
3814 if (! dynamic || elf_dynsymtab (abfd) == 0)
3815 hdr = &elf_tdata (abfd)->symtab_hdr;
3817 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3819 symcount = hdr->sh_size / bed->s->sizeof_sym;
3821 /* The sh_info field of the symtab header tells us where the
3822 external symbols start. We don't care about the local symbols at
3824 if (elf_bad_symtab (abfd))
3826 extsymcount = symcount;
3831 extsymcount = symcount - hdr->sh_info;
3832 extsymoff = hdr->sh_info;
3835 sym_hash = elf_sym_hashes (abfd);
3836 if (extsymcount != 0)
3838 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3840 if (isymbuf == NULL)
3843 if (sym_hash == NULL)
3845 /* We store a pointer to the hash table entry for each
3847 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3848 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3849 if (sym_hash == NULL)
3850 goto error_free_sym;
3851 elf_sym_hashes (abfd) = sym_hash;
3857 /* Read in any version definitions. */
3858 if (!_bfd_elf_slurp_version_tables (abfd,
3859 info->default_imported_symver))
3860 goto error_free_sym;
3862 /* Read in the symbol versions, but don't bother to convert them
3863 to internal format. */
3864 if (elf_dynversym (abfd) != 0)
3866 Elf_Internal_Shdr *versymhdr;
3868 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3869 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3870 if (extversym == NULL)
3871 goto error_free_sym;
3872 amt = versymhdr->sh_size;
3873 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3874 || bfd_bread (extversym, amt, abfd) != amt)
3875 goto error_free_vers;
3879 /* If we are loading an as-needed shared lib, save the symbol table
3880 state before we start adding symbols. If the lib turns out
3881 to be unneeded, restore the state. */
3882 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3887 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3889 struct bfd_hash_entry *p;
3890 struct elf_link_hash_entry *h;
3892 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3894 h = (struct elf_link_hash_entry *) p;
3895 entsize += htab->root.table.entsize;
3896 if (h->root.type == bfd_link_hash_warning)
3897 entsize += htab->root.table.entsize;
3901 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3902 old_tab = bfd_malloc (tabsize + entsize);
3903 if (old_tab == NULL)
3904 goto error_free_vers;
3906 /* Remember the current objalloc pointer, so that all mem for
3907 symbols added can later be reclaimed. */
3908 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3909 if (alloc_mark == NULL)
3910 goto error_free_vers;
3912 /* Make a special call to the linker "notice" function to
3913 tell it that we are about to handle an as-needed lib. */
3914 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
3915 goto error_free_vers;
3917 /* Clone the symbol table. Remember some pointers into the
3918 symbol table, and dynamic symbol count. */
3919 old_ent = (char *) old_tab + tabsize;
3920 memcpy (old_tab, htab->root.table.table, tabsize);
3921 old_undefs = htab->root.undefs;
3922 old_undefs_tail = htab->root.undefs_tail;
3923 old_table = htab->root.table.table;
3924 old_size = htab->root.table.size;
3925 old_count = htab->root.table.count;
3926 old_dynsymcount = htab->dynsymcount;
3927 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
3929 for (i = 0; i < htab->root.table.size; i++)
3931 struct bfd_hash_entry *p;
3932 struct elf_link_hash_entry *h;
3934 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3936 memcpy (old_ent, p, htab->root.table.entsize);
3937 old_ent = (char *) old_ent + htab->root.table.entsize;
3938 h = (struct elf_link_hash_entry *) p;
3939 if (h->root.type == bfd_link_hash_warning)
3941 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3942 old_ent = (char *) old_ent + htab->root.table.entsize;
3949 ever = extversym != NULL ? extversym + extsymoff : NULL;
3950 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3952 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3956 asection *sec, *new_sec;
3959 struct elf_link_hash_entry *h;
3960 struct elf_link_hash_entry *hi;
3961 bfd_boolean definition;
3962 bfd_boolean size_change_ok;
3963 bfd_boolean type_change_ok;
3964 bfd_boolean new_weakdef;
3965 bfd_boolean new_weak;
3966 bfd_boolean old_weak;
3967 bfd_boolean override;
3969 unsigned int old_alignment;
3971 bfd_boolean matched;
3975 flags = BSF_NO_FLAGS;
3977 value = isym->st_value;
3978 common = bed->common_definition (isym);
3980 bind = ELF_ST_BIND (isym->st_info);
3984 /* This should be impossible, since ELF requires that all
3985 global symbols follow all local symbols, and that sh_info
3986 point to the first global symbol. Unfortunately, Irix 5
3991 if (isym->st_shndx != SHN_UNDEF && !common)
3999 case STB_GNU_UNIQUE:
4000 flags = BSF_GNU_UNIQUE;
4004 /* Leave it up to the processor backend. */
4008 if (isym->st_shndx == SHN_UNDEF)
4009 sec = bfd_und_section_ptr;
4010 else if (isym->st_shndx == SHN_ABS)
4011 sec = bfd_abs_section_ptr;
4012 else if (isym->st_shndx == SHN_COMMON)
4014 sec = bfd_com_section_ptr;
4015 /* What ELF calls the size we call the value. What ELF
4016 calls the value we call the alignment. */
4017 value = isym->st_size;
4021 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4023 sec = bfd_abs_section_ptr;
4024 else if (discarded_section (sec))
4026 /* Symbols from discarded section are undefined. We keep
4028 sec = bfd_und_section_ptr;
4029 isym->st_shndx = SHN_UNDEF;
4031 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4035 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4038 goto error_free_vers;
4040 if (isym->st_shndx == SHN_COMMON
4041 && (abfd->flags & BFD_PLUGIN) != 0)
4043 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4047 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4049 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4051 goto error_free_vers;
4055 else if (isym->st_shndx == SHN_COMMON
4056 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4057 && !bfd_link_relocatable (info))
4059 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4063 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4064 | SEC_LINKER_CREATED);
4065 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4067 goto error_free_vers;
4071 else if (bed->elf_add_symbol_hook)
4073 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4075 goto error_free_vers;
4077 /* The hook function sets the name to NULL if this symbol
4078 should be skipped for some reason. */
4083 /* Sanity check that all possibilities were handled. */
4086 bfd_set_error (bfd_error_bad_value);
4087 goto error_free_vers;
4090 /* Silently discard TLS symbols from --just-syms. There's
4091 no way to combine a static TLS block with a new TLS block
4092 for this executable. */
4093 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4094 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4097 if (bfd_is_und_section (sec)
4098 || bfd_is_com_section (sec))
4103 size_change_ok = FALSE;
4104 type_change_ok = bed->type_change_ok;
4111 if (is_elf_hash_table (htab))
4113 Elf_Internal_Versym iver;
4114 unsigned int vernum = 0;
4119 if (info->default_imported_symver)
4120 /* Use the default symbol version created earlier. */
4121 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4126 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4128 vernum = iver.vs_vers & VERSYM_VERSION;
4130 /* If this is a hidden symbol, or if it is not version
4131 1, we append the version name to the symbol name.
4132 However, we do not modify a non-hidden absolute symbol
4133 if it is not a function, because it might be the version
4134 symbol itself. FIXME: What if it isn't? */
4135 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4137 && (!bfd_is_abs_section (sec)
4138 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4141 size_t namelen, verlen, newlen;
4144 if (isym->st_shndx != SHN_UNDEF)
4146 if (vernum > elf_tdata (abfd)->cverdefs)
4148 else if (vernum > 1)
4150 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4156 (*_bfd_error_handler)
4157 (_("%B: %s: invalid version %u (max %d)"),
4159 elf_tdata (abfd)->cverdefs);
4160 bfd_set_error (bfd_error_bad_value);
4161 goto error_free_vers;
4166 /* We cannot simply test for the number of
4167 entries in the VERNEED section since the
4168 numbers for the needed versions do not start
4170 Elf_Internal_Verneed *t;
4173 for (t = elf_tdata (abfd)->verref;
4177 Elf_Internal_Vernaux *a;
4179 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4181 if (a->vna_other == vernum)
4183 verstr = a->vna_nodename;
4192 (*_bfd_error_handler)
4193 (_("%B: %s: invalid needed version %d"),
4194 abfd, name, vernum);
4195 bfd_set_error (bfd_error_bad_value);
4196 goto error_free_vers;
4200 namelen = strlen (name);
4201 verlen = strlen (verstr);
4202 newlen = namelen + verlen + 2;
4203 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4204 && isym->st_shndx != SHN_UNDEF)
4207 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4208 if (newname == NULL)
4209 goto error_free_vers;
4210 memcpy (newname, name, namelen);
4211 p = newname + namelen;
4213 /* If this is a defined non-hidden version symbol,
4214 we add another @ to the name. This indicates the
4215 default version of the symbol. */
4216 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4217 && isym->st_shndx != SHN_UNDEF)
4219 memcpy (p, verstr, verlen + 1);
4224 /* If this symbol has default visibility and the user has
4225 requested we not re-export it, then mark it as hidden. */
4226 if (!bfd_is_und_section (sec)
4229 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4230 isym->st_other = (STV_HIDDEN
4231 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4233 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4234 sym_hash, &old_bfd, &old_weak,
4235 &old_alignment, &skip, &override,
4236 &type_change_ok, &size_change_ok,
4238 goto error_free_vers;
4243 /* Override a definition only if the new symbol matches the
4245 if (override && matched)
4249 while (h->root.type == bfd_link_hash_indirect
4250 || h->root.type == bfd_link_hash_warning)
4251 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4253 if (elf_tdata (abfd)->verdef != NULL
4256 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4259 if (! (_bfd_generic_link_add_one_symbol
4260 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4261 (struct bfd_link_hash_entry **) sym_hash)))
4262 goto error_free_vers;
4265 /* We need to make sure that indirect symbol dynamic flags are
4268 while (h->root.type == bfd_link_hash_indirect
4269 || h->root.type == bfd_link_hash_warning)
4270 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4274 new_weak = (flags & BSF_WEAK) != 0;
4275 new_weakdef = FALSE;
4279 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4280 && is_elf_hash_table (htab)
4281 && h->u.weakdef == NULL)
4283 /* Keep a list of all weak defined non function symbols from
4284 a dynamic object, using the weakdef field. Later in this
4285 function we will set the weakdef field to the correct
4286 value. We only put non-function symbols from dynamic
4287 objects on this list, because that happens to be the only
4288 time we need to know the normal symbol corresponding to a
4289 weak symbol, and the information is time consuming to
4290 figure out. If the weakdef field is not already NULL,
4291 then this symbol was already defined by some previous
4292 dynamic object, and we will be using that previous
4293 definition anyhow. */
4295 h->u.weakdef = weaks;
4300 /* Set the alignment of a common symbol. */
4301 if ((common || bfd_is_com_section (sec))
4302 && h->root.type == bfd_link_hash_common)
4307 align = bfd_log2 (isym->st_value);
4310 /* The new symbol is a common symbol in a shared object.
4311 We need to get the alignment from the section. */
4312 align = new_sec->alignment_power;
4314 if (align > old_alignment)
4315 h->root.u.c.p->alignment_power = align;
4317 h->root.u.c.p->alignment_power = old_alignment;
4320 if (is_elf_hash_table (htab))
4322 /* Set a flag in the hash table entry indicating the type of
4323 reference or definition we just found. A dynamic symbol
4324 is one which is referenced or defined by both a regular
4325 object and a shared object. */
4326 bfd_boolean dynsym = FALSE;
4328 /* Plugin symbols aren't normal. Don't set def_regular or
4329 ref_regular for them, or make them dynamic. */
4330 if ((abfd->flags & BFD_PLUGIN) != 0)
4337 if (bind != STB_WEAK)
4338 h->ref_regular_nonweak = 1;
4350 /* If the indirect symbol has been forced local, don't
4351 make the real symbol dynamic. */
4352 if ((h == hi || !hi->forced_local)
4353 && (bfd_link_dll (info)
4363 hi->ref_dynamic = 1;
4368 hi->def_dynamic = 1;
4371 /* If the indirect symbol has been forced local, don't
4372 make the real symbol dynamic. */
4373 if ((h == hi || !hi->forced_local)
4376 || (h->u.weakdef != NULL
4378 && h->u.weakdef->dynindx != -1)))
4382 /* Check to see if we need to add an indirect symbol for
4383 the default name. */
4385 || (!override && h->root.type == bfd_link_hash_common))
4386 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4387 sec, value, &old_bfd, &dynsym))
4388 goto error_free_vers;
4390 /* Check the alignment when a common symbol is involved. This
4391 can change when a common symbol is overridden by a normal
4392 definition or a common symbol is ignored due to the old
4393 normal definition. We need to make sure the maximum
4394 alignment is maintained. */
4395 if ((old_alignment || common)
4396 && h->root.type != bfd_link_hash_common)
4398 unsigned int common_align;
4399 unsigned int normal_align;
4400 unsigned int symbol_align;
4404 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4405 || h->root.type == bfd_link_hash_defweak);
4407 symbol_align = ffs (h->root.u.def.value) - 1;
4408 if (h->root.u.def.section->owner != NULL
4409 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4411 normal_align = h->root.u.def.section->alignment_power;
4412 if (normal_align > symbol_align)
4413 normal_align = symbol_align;
4416 normal_align = symbol_align;
4420 common_align = old_alignment;
4421 common_bfd = old_bfd;
4426 common_align = bfd_log2 (isym->st_value);
4428 normal_bfd = old_bfd;
4431 if (normal_align < common_align)
4433 /* PR binutils/2735 */
4434 if (normal_bfd == NULL)
4435 (*_bfd_error_handler)
4436 (_("Warning: alignment %u of common symbol `%s' in %B is"
4437 " greater than the alignment (%u) of its section %A"),
4438 common_bfd, h->root.u.def.section,
4439 1 << common_align, name, 1 << normal_align);
4441 (*_bfd_error_handler)
4442 (_("Warning: alignment %u of symbol `%s' in %B"
4443 " is smaller than %u in %B"),
4444 normal_bfd, common_bfd,
4445 1 << normal_align, name, 1 << common_align);
4449 /* Remember the symbol size if it isn't undefined. */
4450 if (isym->st_size != 0
4451 && isym->st_shndx != SHN_UNDEF
4452 && (definition || h->size == 0))
4455 && h->size != isym->st_size
4456 && ! size_change_ok)
4457 (*_bfd_error_handler)
4458 (_("Warning: size of symbol `%s' changed"
4459 " from %lu in %B to %lu in %B"),
4461 name, (unsigned long) h->size,
4462 (unsigned long) isym->st_size);
4464 h->size = isym->st_size;
4467 /* If this is a common symbol, then we always want H->SIZE
4468 to be the size of the common symbol. The code just above
4469 won't fix the size if a common symbol becomes larger. We
4470 don't warn about a size change here, because that is
4471 covered by --warn-common. Allow changes between different
4473 if (h->root.type == bfd_link_hash_common)
4474 h->size = h->root.u.c.size;
4476 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4477 && ((definition && !new_weak)
4478 || (old_weak && h->root.type == bfd_link_hash_common)
4479 || h->type == STT_NOTYPE))
4481 unsigned int type = ELF_ST_TYPE (isym->st_info);
4483 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4485 if (type == STT_GNU_IFUNC
4486 && (abfd->flags & DYNAMIC) != 0)
4489 if (h->type != type)
4491 if (h->type != STT_NOTYPE && ! type_change_ok)
4492 (*_bfd_error_handler)
4493 (_("Warning: type of symbol `%s' changed"
4494 " from %d to %d in %B"),
4495 abfd, name, h->type, type);
4501 /* Merge st_other field. */
4502 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4504 /* We don't want to make debug symbol dynamic. */
4506 && (sec->flags & SEC_DEBUGGING)
4507 && !bfd_link_relocatable (info))
4510 /* Nor should we make plugin symbols dynamic. */
4511 if ((abfd->flags & BFD_PLUGIN) != 0)
4516 h->target_internal = isym->st_target_internal;
4517 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4520 if (definition && !dynamic)
4522 char *p = strchr (name, ELF_VER_CHR);
4523 if (p != NULL && p[1] != ELF_VER_CHR)
4525 /* Queue non-default versions so that .symver x, x@FOO
4526 aliases can be checked. */
4529 amt = ((isymend - isym + 1)
4530 * sizeof (struct elf_link_hash_entry *));
4532 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4534 goto error_free_vers;
4536 nondeflt_vers[nondeflt_vers_cnt++] = h;
4540 if (dynsym && h->dynindx == -1)
4542 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4543 goto error_free_vers;
4544 if (h->u.weakdef != NULL
4546 && h->u.weakdef->dynindx == -1)
4548 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4549 goto error_free_vers;
4552 else if (dynsym && h->dynindx != -1)
4553 /* If the symbol already has a dynamic index, but
4554 visibility says it should not be visible, turn it into
4556 switch (ELF_ST_VISIBILITY (h->other))
4560 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4565 /* Don't add DT_NEEDED for references from the dummy bfd. */
4569 && h->ref_regular_nonweak
4571 || (old_bfd->flags & BFD_PLUGIN) == 0))
4572 || (h->ref_dynamic_nonweak
4573 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4574 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4577 const char *soname = elf_dt_name (abfd);
4579 info->callbacks->minfo ("%!", soname, old_bfd,
4580 h->root.root.string);
4582 /* A symbol from a library loaded via DT_NEEDED of some
4583 other library is referenced by a regular object.
4584 Add a DT_NEEDED entry for it. Issue an error if
4585 --no-add-needed is used and the reference was not
4588 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4590 (*_bfd_error_handler)
4591 (_("%B: undefined reference to symbol '%s'"),
4593 bfd_set_error (bfd_error_missing_dso);
4594 goto error_free_vers;
4597 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4598 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4601 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4603 goto error_free_vers;
4605 BFD_ASSERT (ret == 0);
4610 if (extversym != NULL)
4616 if (isymbuf != NULL)
4622 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4626 /* Restore the symbol table. */
4627 old_ent = (char *) old_tab + tabsize;
4628 memset (elf_sym_hashes (abfd), 0,
4629 extsymcount * sizeof (struct elf_link_hash_entry *));
4630 htab->root.table.table = old_table;
4631 htab->root.table.size = old_size;
4632 htab->root.table.count = old_count;
4633 memcpy (htab->root.table.table, old_tab, tabsize);
4634 htab->root.undefs = old_undefs;
4635 htab->root.undefs_tail = old_undefs_tail;
4636 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
4637 for (i = 0; i < htab->root.table.size; i++)
4639 struct bfd_hash_entry *p;
4640 struct elf_link_hash_entry *h;
4642 unsigned int alignment_power;
4644 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4646 h = (struct elf_link_hash_entry *) p;
4647 if (h->root.type == bfd_link_hash_warning)
4648 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4649 if (h->dynindx >= old_dynsymcount
4650 && h->dynstr_index < old_dynstr_size)
4651 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4653 /* Preserve the maximum alignment and size for common
4654 symbols even if this dynamic lib isn't on DT_NEEDED
4655 since it can still be loaded at run time by another
4657 if (h->root.type == bfd_link_hash_common)
4659 size = h->root.u.c.size;
4660 alignment_power = h->root.u.c.p->alignment_power;
4665 alignment_power = 0;
4667 memcpy (p, old_ent, htab->root.table.entsize);
4668 old_ent = (char *) old_ent + htab->root.table.entsize;
4669 h = (struct elf_link_hash_entry *) p;
4670 if (h->root.type == bfd_link_hash_warning)
4672 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4673 old_ent = (char *) old_ent + htab->root.table.entsize;
4674 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4676 if (h->root.type == bfd_link_hash_common)
4678 if (size > h->root.u.c.size)
4679 h->root.u.c.size = size;
4680 if (alignment_power > h->root.u.c.p->alignment_power)
4681 h->root.u.c.p->alignment_power = alignment_power;
4686 /* Make a special call to the linker "notice" function to
4687 tell it that symbols added for crefs may need to be removed. */
4688 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4689 goto error_free_vers;
4692 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4694 if (nondeflt_vers != NULL)
4695 free (nondeflt_vers);
4699 if (old_tab != NULL)
4701 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4702 goto error_free_vers;
4707 /* Now that all the symbols from this input file are created, if
4708 not performing a relocatable link, handle .symver foo, foo@BAR
4709 such that any relocs against foo become foo@BAR. */
4710 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4712 bfd_size_type cnt, symidx;
4714 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4716 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4717 char *shortname, *p;
4719 p = strchr (h->root.root.string, ELF_VER_CHR);
4721 || (h->root.type != bfd_link_hash_defined
4722 && h->root.type != bfd_link_hash_defweak))
4725 amt = p - h->root.root.string;
4726 shortname = (char *) bfd_malloc (amt + 1);
4728 goto error_free_vers;
4729 memcpy (shortname, h->root.root.string, amt);
4730 shortname[amt] = '\0';
4732 hi = (struct elf_link_hash_entry *)
4733 bfd_link_hash_lookup (&htab->root, shortname,
4734 FALSE, FALSE, FALSE);
4736 && hi->root.type == h->root.type
4737 && hi->root.u.def.value == h->root.u.def.value
4738 && hi->root.u.def.section == h->root.u.def.section)
4740 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4741 hi->root.type = bfd_link_hash_indirect;
4742 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4743 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4744 sym_hash = elf_sym_hashes (abfd);
4746 for (symidx = 0; symidx < extsymcount; ++symidx)
4747 if (sym_hash[symidx] == hi)
4749 sym_hash[symidx] = h;
4755 free (nondeflt_vers);
4756 nondeflt_vers = NULL;
4759 /* Now set the weakdefs field correctly for all the weak defined
4760 symbols we found. The only way to do this is to search all the
4761 symbols. Since we only need the information for non functions in
4762 dynamic objects, that's the only time we actually put anything on
4763 the list WEAKS. We need this information so that if a regular
4764 object refers to a symbol defined weakly in a dynamic object, the
4765 real symbol in the dynamic object is also put in the dynamic
4766 symbols; we also must arrange for both symbols to point to the
4767 same memory location. We could handle the general case of symbol
4768 aliasing, but a general symbol alias can only be generated in
4769 assembler code, handling it correctly would be very time
4770 consuming, and other ELF linkers don't handle general aliasing
4774 struct elf_link_hash_entry **hpp;
4775 struct elf_link_hash_entry **hppend;
4776 struct elf_link_hash_entry **sorted_sym_hash;
4777 struct elf_link_hash_entry *h;
4780 /* Since we have to search the whole symbol list for each weak
4781 defined symbol, search time for N weak defined symbols will be
4782 O(N^2). Binary search will cut it down to O(NlogN). */
4783 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4784 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4785 if (sorted_sym_hash == NULL)
4787 sym_hash = sorted_sym_hash;
4788 hpp = elf_sym_hashes (abfd);
4789 hppend = hpp + extsymcount;
4791 for (; hpp < hppend; hpp++)
4795 && h->root.type == bfd_link_hash_defined
4796 && !bed->is_function_type (h->type))
4804 qsort (sorted_sym_hash, sym_count,
4805 sizeof (struct elf_link_hash_entry *),
4808 while (weaks != NULL)
4810 struct elf_link_hash_entry *hlook;
4813 size_t i, j, idx = 0;
4816 weaks = hlook->u.weakdef;
4817 hlook->u.weakdef = NULL;
4819 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4820 || hlook->root.type == bfd_link_hash_defweak
4821 || hlook->root.type == bfd_link_hash_common
4822 || hlook->root.type == bfd_link_hash_indirect);
4823 slook = hlook->root.u.def.section;
4824 vlook = hlook->root.u.def.value;
4830 bfd_signed_vma vdiff;
4832 h = sorted_sym_hash[idx];
4833 vdiff = vlook - h->root.u.def.value;
4840 int sdiff = slook->id - h->root.u.def.section->id;
4850 /* We didn't find a value/section match. */
4854 /* With multiple aliases, or when the weak symbol is already
4855 strongly defined, we have multiple matching symbols and
4856 the binary search above may land on any of them. Step
4857 one past the matching symbol(s). */
4860 h = sorted_sym_hash[idx];
4861 if (h->root.u.def.section != slook
4862 || h->root.u.def.value != vlook)
4866 /* Now look back over the aliases. Since we sorted by size
4867 as well as value and section, we'll choose the one with
4868 the largest size. */
4871 h = sorted_sym_hash[idx];
4873 /* Stop if value or section doesn't match. */
4874 if (h->root.u.def.section != slook
4875 || h->root.u.def.value != vlook)
4877 else if (h != hlook)
4879 hlook->u.weakdef = h;
4881 /* If the weak definition is in the list of dynamic
4882 symbols, make sure the real definition is put
4884 if (hlook->dynindx != -1 && h->dynindx == -1)
4886 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4889 free (sorted_sym_hash);
4894 /* If the real definition is in the list of dynamic
4895 symbols, make sure the weak definition is put
4896 there as well. If we don't do this, then the
4897 dynamic loader might not merge the entries for the
4898 real definition and the weak definition. */
4899 if (h->dynindx != -1 && hlook->dynindx == -1)
4901 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4902 goto err_free_sym_hash;
4909 free (sorted_sym_hash);
4912 if (bed->check_directives
4913 && !(*bed->check_directives) (abfd, info))
4916 /* If this object is the same format as the output object, and it is
4917 not a shared library, then let the backend look through the
4920 This is required to build global offset table entries and to
4921 arrange for dynamic relocs. It is not required for the
4922 particular common case of linking non PIC code, even when linking
4923 against shared libraries, but unfortunately there is no way of
4924 knowing whether an object file has been compiled PIC or not.
4925 Looking through the relocs is not particularly time consuming.
4926 The problem is that we must either (1) keep the relocs in memory,
4927 which causes the linker to require additional runtime memory or
4928 (2) read the relocs twice from the input file, which wastes time.
4929 This would be a good case for using mmap.
4931 I have no idea how to handle linking PIC code into a file of a
4932 different format. It probably can't be done. */
4934 && is_elf_hash_table (htab)
4935 && bed->check_relocs != NULL
4936 && elf_object_id (abfd) == elf_hash_table_id (htab)
4937 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4941 for (o = abfd->sections; o != NULL; o = o->next)
4943 Elf_Internal_Rela *internal_relocs;
4946 if ((o->flags & SEC_RELOC) == 0
4947 || o->reloc_count == 0
4948 || ((info->strip == strip_all || info->strip == strip_debugger)
4949 && (o->flags & SEC_DEBUGGING) != 0)
4950 || bfd_is_abs_section (o->output_section))
4953 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4955 if (internal_relocs == NULL)
4958 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4960 if (elf_section_data (o)->relocs != internal_relocs)
4961 free (internal_relocs);
4968 /* If this is a non-traditional link, try to optimize the handling
4969 of the .stab/.stabstr sections. */
4971 && ! info->traditional_format
4972 && is_elf_hash_table (htab)
4973 && (info->strip != strip_all && info->strip != strip_debugger))
4977 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4978 if (stabstr != NULL)
4980 bfd_size_type string_offset = 0;
4983 for (stab = abfd->sections; stab; stab = stab->next)
4984 if (CONST_STRNEQ (stab->name, ".stab")
4985 && (!stab->name[5] ||
4986 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4987 && (stab->flags & SEC_MERGE) == 0
4988 && !bfd_is_abs_section (stab->output_section))
4990 struct bfd_elf_section_data *secdata;
4992 secdata = elf_section_data (stab);
4993 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4994 stabstr, &secdata->sec_info,
4997 if (secdata->sec_info)
4998 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5003 if (is_elf_hash_table (htab) && add_needed)
5005 /* Add this bfd to the loaded list. */
5006 struct elf_link_loaded_list *n;
5008 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5012 n->next = htab->loaded;
5019 if (old_tab != NULL)
5021 if (nondeflt_vers != NULL)
5022 free (nondeflt_vers);
5023 if (extversym != NULL)
5026 if (isymbuf != NULL)
5032 /* Return the linker hash table entry of a symbol that might be
5033 satisfied by an archive symbol. Return -1 on error. */
5035 struct elf_link_hash_entry *
5036 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5037 struct bfd_link_info *info,
5040 struct elf_link_hash_entry *h;
5044 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5048 /* If this is a default version (the name contains @@), look up the
5049 symbol again with only one `@' as well as without the version.
5050 The effect is that references to the symbol with and without the
5051 version will be matched by the default symbol in the archive. */
5053 p = strchr (name, ELF_VER_CHR);
5054 if (p == NULL || p[1] != ELF_VER_CHR)
5057 /* First check with only one `@'. */
5058 len = strlen (name);
5059 copy = (char *) bfd_alloc (abfd, len);
5061 return (struct elf_link_hash_entry *) 0 - 1;
5063 first = p - name + 1;
5064 memcpy (copy, name, first);
5065 memcpy (copy + first, name + first + 1, len - first);
5067 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5070 /* We also need to check references to the symbol without the
5072 copy[first - 1] = '\0';
5073 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5074 FALSE, FALSE, TRUE);
5077 bfd_release (abfd, copy);
5081 /* Add symbols from an ELF archive file to the linker hash table. We
5082 don't use _bfd_generic_link_add_archive_symbols because we need to
5083 handle versioned symbols.
5085 Fortunately, ELF archive handling is simpler than that done by
5086 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5087 oddities. In ELF, if we find a symbol in the archive map, and the
5088 symbol is currently undefined, we know that we must pull in that
5091 Unfortunately, we do have to make multiple passes over the symbol
5092 table until nothing further is resolved. */
5095 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5098 unsigned char *included = NULL;
5102 const struct elf_backend_data *bed;
5103 struct elf_link_hash_entry * (*archive_symbol_lookup)
5104 (bfd *, struct bfd_link_info *, const char *);
5106 if (! bfd_has_map (abfd))
5108 /* An empty archive is a special case. */
5109 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5111 bfd_set_error (bfd_error_no_armap);
5115 /* Keep track of all symbols we know to be already defined, and all
5116 files we know to be already included. This is to speed up the
5117 second and subsequent passes. */
5118 c = bfd_ardata (abfd)->symdef_count;
5122 amt *= sizeof (*included);
5123 included = (unsigned char *) bfd_zmalloc (amt);
5124 if (included == NULL)
5127 symdefs = bfd_ardata (abfd)->symdefs;
5128 bed = get_elf_backend_data (abfd);
5129 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5142 symdefend = symdef + c;
5143 for (i = 0; symdef < symdefend; symdef++, i++)
5145 struct elf_link_hash_entry *h;
5147 struct bfd_link_hash_entry *undefs_tail;
5152 if (symdef->file_offset == last)
5158 h = archive_symbol_lookup (abfd, info, symdef->name);
5159 if (h == (struct elf_link_hash_entry *) 0 - 1)
5165 if (h->root.type == bfd_link_hash_common)
5167 /* We currently have a common symbol. The archive map contains
5168 a reference to this symbol, so we may want to include it. We
5169 only want to include it however, if this archive element
5170 contains a definition of the symbol, not just another common
5173 Unfortunately some archivers (including GNU ar) will put
5174 declarations of common symbols into their archive maps, as
5175 well as real definitions, so we cannot just go by the archive
5176 map alone. Instead we must read in the element's symbol
5177 table and check that to see what kind of symbol definition
5179 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5182 else if (h->root.type != bfd_link_hash_undefined)
5184 if (h->root.type != bfd_link_hash_undefweak)
5185 /* Symbol must be defined. Don't check it again. */
5190 /* We need to include this archive member. */
5191 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5192 if (element == NULL)
5195 if (! bfd_check_format (element, bfd_object))
5198 undefs_tail = info->hash->undefs_tail;
5200 if (!(*info->callbacks
5201 ->add_archive_element) (info, element, symdef->name, &element))
5203 if (!bfd_link_add_symbols (element, info))
5206 /* If there are any new undefined symbols, we need to make
5207 another pass through the archive in order to see whether
5208 they can be defined. FIXME: This isn't perfect, because
5209 common symbols wind up on undefs_tail and because an
5210 undefined symbol which is defined later on in this pass
5211 does not require another pass. This isn't a bug, but it
5212 does make the code less efficient than it could be. */
5213 if (undefs_tail != info->hash->undefs_tail)
5216 /* Look backward to mark all symbols from this object file
5217 which we have already seen in this pass. */
5221 included[mark] = TRUE;
5226 while (symdefs[mark].file_offset == symdef->file_offset);
5228 /* We mark subsequent symbols from this object file as we go
5229 on through the loop. */
5230 last = symdef->file_offset;
5240 if (included != NULL)
5245 /* Given an ELF BFD, add symbols to the global hash table as
5249 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5251 switch (bfd_get_format (abfd))
5254 return elf_link_add_object_symbols (abfd, info);
5256 return elf_link_add_archive_symbols (abfd, info);
5258 bfd_set_error (bfd_error_wrong_format);
5263 struct hash_codes_info
5265 unsigned long *hashcodes;
5269 /* This function will be called though elf_link_hash_traverse to store
5270 all hash value of the exported symbols in an array. */
5273 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5275 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5280 /* Ignore indirect symbols. These are added by the versioning code. */
5281 if (h->dynindx == -1)
5284 name = h->root.root.string;
5285 if (h->versioned >= versioned)
5287 char *p = strchr (name, ELF_VER_CHR);
5290 alc = (char *) bfd_malloc (p - name + 1);
5296 memcpy (alc, name, p - name);
5297 alc[p - name] = '\0';
5302 /* Compute the hash value. */
5303 ha = bfd_elf_hash (name);
5305 /* Store the found hash value in the array given as the argument. */
5306 *(inf->hashcodes)++ = ha;
5308 /* And store it in the struct so that we can put it in the hash table
5310 h->u.elf_hash_value = ha;
5318 struct collect_gnu_hash_codes
5321 const struct elf_backend_data *bed;
5322 unsigned long int nsyms;
5323 unsigned long int maskbits;
5324 unsigned long int *hashcodes;
5325 unsigned long int *hashval;
5326 unsigned long int *indx;
5327 unsigned long int *counts;
5330 long int min_dynindx;
5331 unsigned long int bucketcount;
5332 unsigned long int symindx;
5333 long int local_indx;
5334 long int shift1, shift2;
5335 unsigned long int mask;
5339 /* This function will be called though elf_link_hash_traverse to store
5340 all hash value of the exported symbols in an array. */
5343 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5345 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5350 /* Ignore indirect symbols. These are added by the versioning code. */
5351 if (h->dynindx == -1)
5354 /* Ignore also local symbols and undefined symbols. */
5355 if (! (*s->bed->elf_hash_symbol) (h))
5358 name = h->root.root.string;
5359 if (h->versioned >= versioned)
5361 char *p = strchr (name, ELF_VER_CHR);
5364 alc = (char *) bfd_malloc (p - name + 1);
5370 memcpy (alc, name, p - name);
5371 alc[p - name] = '\0';
5376 /* Compute the hash value. */
5377 ha = bfd_elf_gnu_hash (name);
5379 /* Store the found hash value in the array for compute_bucket_count,
5380 and also for .dynsym reordering purposes. */
5381 s->hashcodes[s->nsyms] = ha;
5382 s->hashval[h->dynindx] = ha;
5384 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5385 s->min_dynindx = h->dynindx;
5393 /* This function will be called though elf_link_hash_traverse to do
5394 final dynaminc symbol renumbering. */
5397 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5399 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5400 unsigned long int bucket;
5401 unsigned long int val;
5403 /* Ignore indirect symbols. */
5404 if (h->dynindx == -1)
5407 /* Ignore also local symbols and undefined symbols. */
5408 if (! (*s->bed->elf_hash_symbol) (h))
5410 if (h->dynindx >= s->min_dynindx)
5411 h->dynindx = s->local_indx++;
5415 bucket = s->hashval[h->dynindx] % s->bucketcount;
5416 val = (s->hashval[h->dynindx] >> s->shift1)
5417 & ((s->maskbits >> s->shift1) - 1);
5418 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5420 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5421 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5422 if (s->counts[bucket] == 1)
5423 /* Last element terminates the chain. */
5425 bfd_put_32 (s->output_bfd, val,
5426 s->contents + (s->indx[bucket] - s->symindx) * 4);
5427 --s->counts[bucket];
5428 h->dynindx = s->indx[bucket]++;
5432 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5435 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5437 return !(h->forced_local
5438 || h->root.type == bfd_link_hash_undefined
5439 || h->root.type == bfd_link_hash_undefweak
5440 || ((h->root.type == bfd_link_hash_defined
5441 || h->root.type == bfd_link_hash_defweak)
5442 && h->root.u.def.section->output_section == NULL));
5445 /* Array used to determine the number of hash table buckets to use
5446 based on the number of symbols there are. If there are fewer than
5447 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5448 fewer than 37 we use 17 buckets, and so forth. We never use more
5449 than 32771 buckets. */
5451 static const size_t elf_buckets[] =
5453 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5457 /* Compute bucket count for hashing table. We do not use a static set
5458 of possible tables sizes anymore. Instead we determine for all
5459 possible reasonable sizes of the table the outcome (i.e., the
5460 number of collisions etc) and choose the best solution. The
5461 weighting functions are not too simple to allow the table to grow
5462 without bounds. Instead one of the weighting factors is the size.
5463 Therefore the result is always a good payoff between few collisions
5464 (= short chain lengths) and table size. */
5466 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5467 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5468 unsigned long int nsyms,
5471 size_t best_size = 0;
5472 unsigned long int i;
5474 /* We have a problem here. The following code to optimize the table
5475 size requires an integer type with more the 32 bits. If
5476 BFD_HOST_U_64_BIT is set we know about such a type. */
5477 #ifdef BFD_HOST_U_64_BIT
5482 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5483 bfd *dynobj = elf_hash_table (info)->dynobj;
5484 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5485 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5486 unsigned long int *counts;
5488 unsigned int no_improvement_count = 0;
5490 /* Possible optimization parameters: if we have NSYMS symbols we say
5491 that the hashing table must at least have NSYMS/4 and at most
5493 minsize = nsyms / 4;
5496 best_size = maxsize = nsyms * 2;
5501 if ((best_size & 31) == 0)
5505 /* Create array where we count the collisions in. We must use bfd_malloc
5506 since the size could be large. */
5508 amt *= sizeof (unsigned long int);
5509 counts = (unsigned long int *) bfd_malloc (amt);
5513 /* Compute the "optimal" size for the hash table. The criteria is a
5514 minimal chain length. The minor criteria is (of course) the size
5516 for (i = minsize; i < maxsize; ++i)
5518 /* Walk through the array of hashcodes and count the collisions. */
5519 BFD_HOST_U_64_BIT max;
5520 unsigned long int j;
5521 unsigned long int fact;
5523 if (gnu_hash && (i & 31) == 0)
5526 memset (counts, '\0', i * sizeof (unsigned long int));
5528 /* Determine how often each hash bucket is used. */
5529 for (j = 0; j < nsyms; ++j)
5530 ++counts[hashcodes[j] % i];
5532 /* For the weight function we need some information about the
5533 pagesize on the target. This is information need not be 100%
5534 accurate. Since this information is not available (so far) we
5535 define it here to a reasonable default value. If it is crucial
5536 to have a better value some day simply define this value. */
5537 # ifndef BFD_TARGET_PAGESIZE
5538 # define BFD_TARGET_PAGESIZE (4096)
5541 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5543 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5546 /* Variant 1: optimize for short chains. We add the squares
5547 of all the chain lengths (which favors many small chain
5548 over a few long chains). */
5549 for (j = 0; j < i; ++j)
5550 max += counts[j] * counts[j];
5552 /* This adds penalties for the overall size of the table. */
5553 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5556 /* Variant 2: Optimize a lot more for small table. Here we
5557 also add squares of the size but we also add penalties for
5558 empty slots (the +1 term). */
5559 for (j = 0; j < i; ++j)
5560 max += (1 + counts[j]) * (1 + counts[j]);
5562 /* The overall size of the table is considered, but not as
5563 strong as in variant 1, where it is squared. */
5564 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5568 /* Compare with current best results. */
5569 if (max < best_chlen)
5573 no_improvement_count = 0;
5575 /* PR 11843: Avoid futile long searches for the best bucket size
5576 when there are a large number of symbols. */
5577 else if (++no_improvement_count == 100)
5584 #endif /* defined (BFD_HOST_U_64_BIT) */
5586 /* This is the fallback solution if no 64bit type is available or if we
5587 are not supposed to spend much time on optimizations. We select the
5588 bucket count using a fixed set of numbers. */
5589 for (i = 0; elf_buckets[i] != 0; i++)
5591 best_size = elf_buckets[i];
5592 if (nsyms < elf_buckets[i + 1])
5595 if (gnu_hash && best_size < 2)
5602 /* Size any SHT_GROUP section for ld -r. */
5605 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5609 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5610 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5611 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5616 /* Set a default stack segment size. The value in INFO wins. If it
5617 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5618 undefined it is initialized. */
5621 bfd_elf_stack_segment_size (bfd *output_bfd,
5622 struct bfd_link_info *info,
5623 const char *legacy_symbol,
5624 bfd_vma default_size)
5626 struct elf_link_hash_entry *h = NULL;
5628 /* Look for legacy symbol. */
5630 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5631 FALSE, FALSE, FALSE);
5632 if (h && (h->root.type == bfd_link_hash_defined
5633 || h->root.type == bfd_link_hash_defweak)
5635 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5637 /* The symbol has no type if specified on the command line. */
5638 h->type = STT_OBJECT;
5639 if (info->stacksize)
5640 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5641 output_bfd, legacy_symbol);
5642 else if (h->root.u.def.section != bfd_abs_section_ptr)
5643 (*_bfd_error_handler) (_("%B: %s not absolute"),
5644 output_bfd, legacy_symbol);
5646 info->stacksize = h->root.u.def.value;
5649 if (!info->stacksize)
5650 /* If the user didn't set a size, or explicitly inhibit the
5651 size, set it now. */
5652 info->stacksize = default_size;
5654 /* Provide the legacy symbol, if it is referenced. */
5655 if (h && (h->root.type == bfd_link_hash_undefined
5656 || h->root.type == bfd_link_hash_undefweak))
5658 struct bfd_link_hash_entry *bh = NULL;
5660 if (!(_bfd_generic_link_add_one_symbol
5661 (info, output_bfd, legacy_symbol,
5662 BSF_GLOBAL, bfd_abs_section_ptr,
5663 info->stacksize >= 0 ? info->stacksize : 0,
5664 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5667 h = (struct elf_link_hash_entry *) bh;
5669 h->type = STT_OBJECT;
5675 /* Set up the sizes and contents of the ELF dynamic sections. This is
5676 called by the ELF linker emulation before_allocation routine. We
5677 must set the sizes of the sections before the linker sets the
5678 addresses of the various sections. */
5681 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5684 const char *filter_shlib,
5686 const char *depaudit,
5687 const char * const *auxiliary_filters,
5688 struct bfd_link_info *info,
5689 asection **sinterpptr)
5691 bfd_size_type soname_indx;
5693 const struct elf_backend_data *bed;
5694 struct elf_info_failed asvinfo;
5698 soname_indx = (bfd_size_type) -1;
5700 if (!is_elf_hash_table (info->hash))
5703 bed = get_elf_backend_data (output_bfd);
5705 /* Any syms created from now on start with -1 in
5706 got.refcount/offset and plt.refcount/offset. */
5707 elf_hash_table (info)->init_got_refcount
5708 = elf_hash_table (info)->init_got_offset;
5709 elf_hash_table (info)->init_plt_refcount
5710 = elf_hash_table (info)->init_plt_offset;
5712 if (bfd_link_relocatable (info)
5713 && !_bfd_elf_size_group_sections (info))
5716 /* The backend may have to create some sections regardless of whether
5717 we're dynamic or not. */
5718 if (bed->elf_backend_always_size_sections
5719 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5722 /* Determine any GNU_STACK segment requirements, after the backend
5723 has had a chance to set a default segment size. */
5724 if (info->execstack)
5725 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5726 else if (info->noexecstack)
5727 elf_stack_flags (output_bfd) = PF_R | PF_W;
5731 asection *notesec = NULL;
5734 for (inputobj = info->input_bfds;
5736 inputobj = inputobj->link.next)
5741 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5743 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5746 if (s->flags & SEC_CODE)
5750 else if (bed->default_execstack)
5753 if (notesec || info->stacksize > 0)
5754 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5755 if (notesec && exec && bfd_link_relocatable (info)
5756 && notesec->output_section != bfd_abs_section_ptr)
5757 notesec->output_section->flags |= SEC_CODE;
5760 dynobj = elf_hash_table (info)->dynobj;
5762 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5764 struct elf_info_failed eif;
5765 struct elf_link_hash_entry *h;
5767 struct bfd_elf_version_tree *t;
5768 struct bfd_elf_version_expr *d;
5770 bfd_boolean all_defined;
5772 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5773 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5777 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5779 if (soname_indx == (bfd_size_type) -1
5780 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5786 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5788 info->flags |= DF_SYMBOLIC;
5796 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5798 if (indx == (bfd_size_type) -1)
5801 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5802 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5806 if (filter_shlib != NULL)
5810 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5811 filter_shlib, TRUE);
5812 if (indx == (bfd_size_type) -1
5813 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5817 if (auxiliary_filters != NULL)
5819 const char * const *p;
5821 for (p = auxiliary_filters; *p != NULL; p++)
5825 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5827 if (indx == (bfd_size_type) -1
5828 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5837 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5839 if (indx == (bfd_size_type) -1
5840 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5844 if (depaudit != NULL)
5848 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5850 if (indx == (bfd_size_type) -1
5851 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5858 /* If we are supposed to export all symbols into the dynamic symbol
5859 table (this is not the normal case), then do so. */
5860 if (info->export_dynamic
5861 || (bfd_link_executable (info) && info->dynamic))
5863 elf_link_hash_traverse (elf_hash_table (info),
5864 _bfd_elf_export_symbol,
5870 /* Make all global versions with definition. */
5871 for (t = info->version_info; t != NULL; t = t->next)
5872 for (d = t->globals.list; d != NULL; d = d->next)
5873 if (!d->symver && d->literal)
5875 const char *verstr, *name;
5876 size_t namelen, verlen, newlen;
5877 char *newname, *p, leading_char;
5878 struct elf_link_hash_entry *newh;
5880 leading_char = bfd_get_symbol_leading_char (output_bfd);
5882 namelen = strlen (name) + (leading_char != '\0');
5884 verlen = strlen (verstr);
5885 newlen = namelen + verlen + 3;
5887 newname = (char *) bfd_malloc (newlen);
5888 if (newname == NULL)
5890 newname[0] = leading_char;
5891 memcpy (newname + (leading_char != '\0'), name, namelen);
5893 /* Check the hidden versioned definition. */
5894 p = newname + namelen;
5896 memcpy (p, verstr, verlen + 1);
5897 newh = elf_link_hash_lookup (elf_hash_table (info),
5898 newname, FALSE, FALSE,
5901 || (newh->root.type != bfd_link_hash_defined
5902 && newh->root.type != bfd_link_hash_defweak))
5904 /* Check the default versioned definition. */
5906 memcpy (p, verstr, verlen + 1);
5907 newh = elf_link_hash_lookup (elf_hash_table (info),
5908 newname, FALSE, FALSE,
5913 /* Mark this version if there is a definition and it is
5914 not defined in a shared object. */
5916 && !newh->def_dynamic
5917 && (newh->root.type == bfd_link_hash_defined
5918 || newh->root.type == bfd_link_hash_defweak))
5922 /* Attach all the symbols to their version information. */
5923 asvinfo.info = info;
5924 asvinfo.failed = FALSE;
5926 elf_link_hash_traverse (elf_hash_table (info),
5927 _bfd_elf_link_assign_sym_version,
5932 if (!info->allow_undefined_version)
5934 /* Check if all global versions have a definition. */
5936 for (t = info->version_info; t != NULL; t = t->next)
5937 for (d = t->globals.list; d != NULL; d = d->next)
5938 if (d->literal && !d->symver && !d->script)
5940 (*_bfd_error_handler)
5941 (_("%s: undefined version: %s"),
5942 d->pattern, t->name);
5943 all_defined = FALSE;
5948 bfd_set_error (bfd_error_bad_value);
5953 /* Find all symbols which were defined in a dynamic object and make
5954 the backend pick a reasonable value for them. */
5955 elf_link_hash_traverse (elf_hash_table (info),
5956 _bfd_elf_adjust_dynamic_symbol,
5961 /* Add some entries to the .dynamic section. We fill in some of the
5962 values later, in bfd_elf_final_link, but we must add the entries
5963 now so that we know the final size of the .dynamic section. */
5965 /* If there are initialization and/or finalization functions to
5966 call then add the corresponding DT_INIT/DT_FINI entries. */
5967 h = (info->init_function
5968 ? elf_link_hash_lookup (elf_hash_table (info),
5969 info->init_function, FALSE,
5976 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5979 h = (info->fini_function
5980 ? elf_link_hash_lookup (elf_hash_table (info),
5981 info->fini_function, FALSE,
5988 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5992 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5993 if (s != NULL && s->linker_has_input)
5995 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5996 if (! bfd_link_executable (info))
6001 for (sub = info->input_bfds; sub != NULL;
6002 sub = sub->link.next)
6003 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6004 for (o = sub->sections; o != NULL; o = o->next)
6005 if (elf_section_data (o)->this_hdr.sh_type
6006 == SHT_PREINIT_ARRAY)
6008 (*_bfd_error_handler)
6009 (_("%B: .preinit_array section is not allowed in DSO"),
6014 bfd_set_error (bfd_error_nonrepresentable_section);
6018 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6019 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6022 s = bfd_get_section_by_name (output_bfd, ".init_array");
6023 if (s != NULL && s->linker_has_input)
6025 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6026 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6029 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6030 if (s != NULL && s->linker_has_input)
6032 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6033 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6037 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6038 /* If .dynstr is excluded from the link, we don't want any of
6039 these tags. Strictly, we should be checking each section
6040 individually; This quick check covers for the case where
6041 someone does a /DISCARD/ : { *(*) }. */
6042 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6044 bfd_size_type strsize;
6046 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6047 if ((info->emit_hash
6048 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6049 || (info->emit_gnu_hash
6050 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6051 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6052 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6053 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6054 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6055 bed->s->sizeof_sym))
6060 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6063 /* The backend must work out the sizes of all the other dynamic
6066 && bed->elf_backend_size_dynamic_sections != NULL
6067 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6070 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6072 unsigned long section_sym_count;
6073 struct bfd_elf_version_tree *verdefs;
6076 /* Set up the version definition section. */
6077 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6078 BFD_ASSERT (s != NULL);
6080 /* We may have created additional version definitions if we are
6081 just linking a regular application. */
6082 verdefs = info->version_info;
6084 /* Skip anonymous version tag. */
6085 if (verdefs != NULL && verdefs->vernum == 0)
6086 verdefs = verdefs->next;
6088 if (verdefs == NULL && !info->create_default_symver)
6089 s->flags |= SEC_EXCLUDE;
6094 struct bfd_elf_version_tree *t;
6096 Elf_Internal_Verdef def;
6097 Elf_Internal_Verdaux defaux;
6098 struct bfd_link_hash_entry *bh;
6099 struct elf_link_hash_entry *h;
6105 /* Make space for the base version. */
6106 size += sizeof (Elf_External_Verdef);
6107 size += sizeof (Elf_External_Verdaux);
6110 /* Make space for the default version. */
6111 if (info->create_default_symver)
6113 size += sizeof (Elf_External_Verdef);
6117 for (t = verdefs; t != NULL; t = t->next)
6119 struct bfd_elf_version_deps *n;
6121 /* Don't emit base version twice. */
6125 size += sizeof (Elf_External_Verdef);
6126 size += sizeof (Elf_External_Verdaux);
6129 for (n = t->deps; n != NULL; n = n->next)
6130 size += sizeof (Elf_External_Verdaux);
6134 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6135 if (s->contents == NULL && s->size != 0)
6138 /* Fill in the version definition section. */
6142 def.vd_version = VER_DEF_CURRENT;
6143 def.vd_flags = VER_FLG_BASE;
6146 if (info->create_default_symver)
6148 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6149 def.vd_next = sizeof (Elf_External_Verdef);
6153 def.vd_aux = sizeof (Elf_External_Verdef);
6154 def.vd_next = (sizeof (Elf_External_Verdef)
6155 + sizeof (Elf_External_Verdaux));
6158 if (soname_indx != (bfd_size_type) -1)
6160 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6162 def.vd_hash = bfd_elf_hash (soname);
6163 defaux.vda_name = soname_indx;
6170 name = lbasename (output_bfd->filename);
6171 def.vd_hash = bfd_elf_hash (name);
6172 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6174 if (indx == (bfd_size_type) -1)
6176 defaux.vda_name = indx;
6178 defaux.vda_next = 0;
6180 _bfd_elf_swap_verdef_out (output_bfd, &def,
6181 (Elf_External_Verdef *) p);
6182 p += sizeof (Elf_External_Verdef);
6183 if (info->create_default_symver)
6185 /* Add a symbol representing this version. */
6187 if (! (_bfd_generic_link_add_one_symbol
6188 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6190 get_elf_backend_data (dynobj)->collect, &bh)))
6192 h = (struct elf_link_hash_entry *) bh;
6195 h->type = STT_OBJECT;
6196 h->verinfo.vertree = NULL;
6198 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6201 /* Create a duplicate of the base version with the same
6202 aux block, but different flags. */
6205 def.vd_aux = sizeof (Elf_External_Verdef);
6207 def.vd_next = (sizeof (Elf_External_Verdef)
6208 + sizeof (Elf_External_Verdaux));
6211 _bfd_elf_swap_verdef_out (output_bfd, &def,
6212 (Elf_External_Verdef *) p);
6213 p += sizeof (Elf_External_Verdef);
6215 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6216 (Elf_External_Verdaux *) p);
6217 p += sizeof (Elf_External_Verdaux);
6219 for (t = verdefs; t != NULL; t = t->next)
6222 struct bfd_elf_version_deps *n;
6224 /* Don't emit the base version twice. */
6229 for (n = t->deps; n != NULL; n = n->next)
6232 /* Add a symbol representing this version. */
6234 if (! (_bfd_generic_link_add_one_symbol
6235 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6237 get_elf_backend_data (dynobj)->collect, &bh)))
6239 h = (struct elf_link_hash_entry *) bh;
6242 h->type = STT_OBJECT;
6243 h->verinfo.vertree = t;
6245 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6248 def.vd_version = VER_DEF_CURRENT;
6250 if (t->globals.list == NULL
6251 && t->locals.list == NULL
6253 def.vd_flags |= VER_FLG_WEAK;
6254 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6255 def.vd_cnt = cdeps + 1;
6256 def.vd_hash = bfd_elf_hash (t->name);
6257 def.vd_aux = sizeof (Elf_External_Verdef);
6260 /* If a basever node is next, it *must* be the last node in
6261 the chain, otherwise Verdef construction breaks. */
6262 if (t->next != NULL && t->next->vernum == 0)
6263 BFD_ASSERT (t->next->next == NULL);
6265 if (t->next != NULL && t->next->vernum != 0)
6266 def.vd_next = (sizeof (Elf_External_Verdef)
6267 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6269 _bfd_elf_swap_verdef_out (output_bfd, &def,
6270 (Elf_External_Verdef *) p);
6271 p += sizeof (Elf_External_Verdef);
6273 defaux.vda_name = h->dynstr_index;
6274 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6276 defaux.vda_next = 0;
6277 if (t->deps != NULL)
6278 defaux.vda_next = sizeof (Elf_External_Verdaux);
6279 t->name_indx = defaux.vda_name;
6281 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6282 (Elf_External_Verdaux *) p);
6283 p += sizeof (Elf_External_Verdaux);
6285 for (n = t->deps; n != NULL; n = n->next)
6287 if (n->version_needed == NULL)
6289 /* This can happen if there was an error in the
6291 defaux.vda_name = 0;
6295 defaux.vda_name = n->version_needed->name_indx;
6296 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6299 if (n->next == NULL)
6300 defaux.vda_next = 0;
6302 defaux.vda_next = sizeof (Elf_External_Verdaux);
6304 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6305 (Elf_External_Verdaux *) p);
6306 p += sizeof (Elf_External_Verdaux);
6310 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6311 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6314 elf_tdata (output_bfd)->cverdefs = cdefs;
6317 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6319 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6322 else if (info->flags & DF_BIND_NOW)
6324 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6330 if (bfd_link_executable (info))
6331 info->flags_1 &= ~ (DF_1_INITFIRST
6334 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6338 /* Work out the size of the version reference section. */
6340 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6341 BFD_ASSERT (s != NULL);
6343 struct elf_find_verdep_info sinfo;
6346 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6347 if (sinfo.vers == 0)
6349 sinfo.failed = FALSE;
6351 elf_link_hash_traverse (elf_hash_table (info),
6352 _bfd_elf_link_find_version_dependencies,
6357 if (elf_tdata (output_bfd)->verref == NULL)
6358 s->flags |= SEC_EXCLUDE;
6361 Elf_Internal_Verneed *t;
6366 /* Build the version dependency section. */
6369 for (t = elf_tdata (output_bfd)->verref;
6373 Elf_Internal_Vernaux *a;
6375 size += sizeof (Elf_External_Verneed);
6377 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6378 size += sizeof (Elf_External_Vernaux);
6382 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6383 if (s->contents == NULL)
6387 for (t = elf_tdata (output_bfd)->verref;
6392 Elf_Internal_Vernaux *a;
6396 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6399 t->vn_version = VER_NEED_CURRENT;
6401 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6402 elf_dt_name (t->vn_bfd) != NULL
6403 ? elf_dt_name (t->vn_bfd)
6404 : lbasename (t->vn_bfd->filename),
6406 if (indx == (bfd_size_type) -1)
6409 t->vn_aux = sizeof (Elf_External_Verneed);
6410 if (t->vn_nextref == NULL)
6413 t->vn_next = (sizeof (Elf_External_Verneed)
6414 + caux * sizeof (Elf_External_Vernaux));
6416 _bfd_elf_swap_verneed_out (output_bfd, t,
6417 (Elf_External_Verneed *) p);
6418 p += sizeof (Elf_External_Verneed);
6420 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6422 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6423 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6424 a->vna_nodename, FALSE);
6425 if (indx == (bfd_size_type) -1)
6428 if (a->vna_nextptr == NULL)
6431 a->vna_next = sizeof (Elf_External_Vernaux);
6433 _bfd_elf_swap_vernaux_out (output_bfd, a,
6434 (Elf_External_Vernaux *) p);
6435 p += sizeof (Elf_External_Vernaux);
6439 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6440 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6443 elf_tdata (output_bfd)->cverrefs = crefs;
6447 if ((elf_tdata (output_bfd)->cverrefs == 0
6448 && elf_tdata (output_bfd)->cverdefs == 0)
6449 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6450 §ion_sym_count) == 0)
6452 s = bfd_get_linker_section (dynobj, ".gnu.version");
6453 s->flags |= SEC_EXCLUDE;
6459 /* Find the first non-excluded output section. We'll use its
6460 section symbol for some emitted relocs. */
6462 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6466 for (s = output_bfd->sections; s != NULL; s = s->next)
6467 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6468 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6470 elf_hash_table (info)->text_index_section = s;
6475 /* Find two non-excluded output sections, one for code, one for data.
6476 We'll use their section symbols for some emitted relocs. */
6478 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6482 /* Data first, since setting text_index_section changes
6483 _bfd_elf_link_omit_section_dynsym. */
6484 for (s = output_bfd->sections; s != NULL; s = s->next)
6485 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6486 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6488 elf_hash_table (info)->data_index_section = s;
6492 for (s = output_bfd->sections; s != NULL; s = s->next)
6493 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6494 == (SEC_ALLOC | SEC_READONLY))
6495 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6497 elf_hash_table (info)->text_index_section = s;
6501 if (elf_hash_table (info)->text_index_section == NULL)
6502 elf_hash_table (info)->text_index_section
6503 = elf_hash_table (info)->data_index_section;
6507 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6509 const struct elf_backend_data *bed;
6511 if (!is_elf_hash_table (info->hash))
6514 bed = get_elf_backend_data (output_bfd);
6515 (*bed->elf_backend_init_index_section) (output_bfd, info);
6517 if (elf_hash_table (info)->dynamic_sections_created)
6521 bfd_size_type dynsymcount;
6522 unsigned long section_sym_count;
6523 unsigned int dtagcount;
6525 dynobj = elf_hash_table (info)->dynobj;
6527 /* Assign dynsym indicies. In a shared library we generate a
6528 section symbol for each output section, which come first.
6529 Next come all of the back-end allocated local dynamic syms,
6530 followed by the rest of the global symbols. */
6532 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6533 §ion_sym_count);
6535 /* Work out the size of the symbol version section. */
6536 s = bfd_get_linker_section (dynobj, ".gnu.version");
6537 BFD_ASSERT (s != NULL);
6538 if (dynsymcount != 0
6539 && (s->flags & SEC_EXCLUDE) == 0)
6541 s->size = dynsymcount * sizeof (Elf_External_Versym);
6542 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6543 if (s->contents == NULL)
6546 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6550 /* Set the size of the .dynsym and .hash sections. We counted
6551 the number of dynamic symbols in elf_link_add_object_symbols.
6552 We will build the contents of .dynsym and .hash when we build
6553 the final symbol table, because until then we do not know the
6554 correct value to give the symbols. We built the .dynstr
6555 section as we went along in elf_link_add_object_symbols. */
6556 s = elf_hash_table (info)->dynsym;
6557 BFD_ASSERT (s != NULL);
6558 s->size = dynsymcount * bed->s->sizeof_sym;
6560 if (dynsymcount != 0)
6562 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6563 if (s->contents == NULL)
6566 /* The first entry in .dynsym is a dummy symbol.
6567 Clear all the section syms, in case we don't output them all. */
6568 ++section_sym_count;
6569 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6572 elf_hash_table (info)->bucketcount = 0;
6574 /* Compute the size of the hashing table. As a side effect this
6575 computes the hash values for all the names we export. */
6576 if (info->emit_hash)
6578 unsigned long int *hashcodes;
6579 struct hash_codes_info hashinf;
6581 unsigned long int nsyms;
6583 size_t hash_entry_size;
6585 /* Compute the hash values for all exported symbols. At the same
6586 time store the values in an array so that we could use them for
6588 amt = dynsymcount * sizeof (unsigned long int);
6589 hashcodes = (unsigned long int *) bfd_malloc (amt);
6590 if (hashcodes == NULL)
6592 hashinf.hashcodes = hashcodes;
6593 hashinf.error = FALSE;
6595 /* Put all hash values in HASHCODES. */
6596 elf_link_hash_traverse (elf_hash_table (info),
6597 elf_collect_hash_codes, &hashinf);
6604 nsyms = hashinf.hashcodes - hashcodes;
6606 = compute_bucket_count (info, hashcodes, nsyms, 0);
6609 if (bucketcount == 0)
6612 elf_hash_table (info)->bucketcount = bucketcount;
6614 s = bfd_get_linker_section (dynobj, ".hash");
6615 BFD_ASSERT (s != NULL);
6616 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6617 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6618 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6619 if (s->contents == NULL)
6622 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6623 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6624 s->contents + hash_entry_size);
6627 if (info->emit_gnu_hash)
6630 unsigned char *contents;
6631 struct collect_gnu_hash_codes cinfo;
6635 memset (&cinfo, 0, sizeof (cinfo));
6637 /* Compute the hash values for all exported symbols. At the same
6638 time store the values in an array so that we could use them for
6640 amt = dynsymcount * 2 * sizeof (unsigned long int);
6641 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6642 if (cinfo.hashcodes == NULL)
6645 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6646 cinfo.min_dynindx = -1;
6647 cinfo.output_bfd = output_bfd;
6650 /* Put all hash values in HASHCODES. */
6651 elf_link_hash_traverse (elf_hash_table (info),
6652 elf_collect_gnu_hash_codes, &cinfo);
6655 free (cinfo.hashcodes);
6660 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6662 if (bucketcount == 0)
6664 free (cinfo.hashcodes);
6668 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6669 BFD_ASSERT (s != NULL);
6671 if (cinfo.nsyms == 0)
6673 /* Empty .gnu.hash section is special. */
6674 BFD_ASSERT (cinfo.min_dynindx == -1);
6675 free (cinfo.hashcodes);
6676 s->size = 5 * 4 + bed->s->arch_size / 8;
6677 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6678 if (contents == NULL)
6680 s->contents = contents;
6681 /* 1 empty bucket. */
6682 bfd_put_32 (output_bfd, 1, contents);
6683 /* SYMIDX above the special symbol 0. */
6684 bfd_put_32 (output_bfd, 1, contents + 4);
6685 /* Just one word for bitmask. */
6686 bfd_put_32 (output_bfd, 1, contents + 8);
6687 /* Only hash fn bloom filter. */
6688 bfd_put_32 (output_bfd, 0, contents + 12);
6689 /* No hashes are valid - empty bitmask. */
6690 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6691 /* No hashes in the only bucket. */
6692 bfd_put_32 (output_bfd, 0,
6693 contents + 16 + bed->s->arch_size / 8);
6697 unsigned long int maskwords, maskbitslog2, x;
6698 BFD_ASSERT (cinfo.min_dynindx != -1);
6702 while ((x >>= 1) != 0)
6704 if (maskbitslog2 < 3)
6706 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6707 maskbitslog2 = maskbitslog2 + 3;
6709 maskbitslog2 = maskbitslog2 + 2;
6710 if (bed->s->arch_size == 64)
6712 if (maskbitslog2 == 5)
6718 cinfo.mask = (1 << cinfo.shift1) - 1;
6719 cinfo.shift2 = maskbitslog2;
6720 cinfo.maskbits = 1 << maskbitslog2;
6721 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6722 amt = bucketcount * sizeof (unsigned long int) * 2;
6723 amt += maskwords * sizeof (bfd_vma);
6724 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6725 if (cinfo.bitmask == NULL)
6727 free (cinfo.hashcodes);
6731 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6732 cinfo.indx = cinfo.counts + bucketcount;
6733 cinfo.symindx = dynsymcount - cinfo.nsyms;
6734 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6736 /* Determine how often each hash bucket is used. */
6737 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6738 for (i = 0; i < cinfo.nsyms; ++i)
6739 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6741 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6742 if (cinfo.counts[i] != 0)
6744 cinfo.indx[i] = cnt;
6745 cnt += cinfo.counts[i];
6747 BFD_ASSERT (cnt == dynsymcount);
6748 cinfo.bucketcount = bucketcount;
6749 cinfo.local_indx = cinfo.min_dynindx;
6751 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6752 s->size += cinfo.maskbits / 8;
6753 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6754 if (contents == NULL)
6756 free (cinfo.bitmask);
6757 free (cinfo.hashcodes);
6761 s->contents = contents;
6762 bfd_put_32 (output_bfd, bucketcount, contents);
6763 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6764 bfd_put_32 (output_bfd, maskwords, contents + 8);
6765 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6766 contents += 16 + cinfo.maskbits / 8;
6768 for (i = 0; i < bucketcount; ++i)
6770 if (cinfo.counts[i] == 0)
6771 bfd_put_32 (output_bfd, 0, contents);
6773 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6777 cinfo.contents = contents;
6779 /* Renumber dynamic symbols, populate .gnu.hash section. */
6780 elf_link_hash_traverse (elf_hash_table (info),
6781 elf_renumber_gnu_hash_syms, &cinfo);
6783 contents = s->contents + 16;
6784 for (i = 0; i < maskwords; ++i)
6786 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6788 contents += bed->s->arch_size / 8;
6791 free (cinfo.bitmask);
6792 free (cinfo.hashcodes);
6796 s = bfd_get_linker_section (dynobj, ".dynstr");
6797 BFD_ASSERT (s != NULL);
6799 elf_finalize_dynstr (output_bfd, info);
6801 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6803 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6804 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6811 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6814 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6817 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6818 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6821 /* Finish SHF_MERGE section merging. */
6824 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
6829 if (!is_elf_hash_table (info->hash))
6832 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6833 if ((ibfd->flags & DYNAMIC) == 0
6834 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6835 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6836 == get_elf_backend_data (obfd)->s->elfclass))
6837 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6838 if ((sec->flags & SEC_MERGE) != 0
6839 && !bfd_is_abs_section (sec->output_section))
6841 struct bfd_elf_section_data *secdata;
6843 secdata = elf_section_data (sec);
6844 if (! _bfd_add_merge_section (obfd,
6845 &elf_hash_table (info)->merge_info,
6846 sec, &secdata->sec_info))
6848 else if (secdata->sec_info)
6849 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6852 if (elf_hash_table (info)->merge_info != NULL)
6853 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
6854 merge_sections_remove_hook);
6858 /* Create an entry in an ELF linker hash table. */
6860 struct bfd_hash_entry *
6861 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6862 struct bfd_hash_table *table,
6865 /* Allocate the structure if it has not already been allocated by a
6869 entry = (struct bfd_hash_entry *)
6870 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6875 /* Call the allocation method of the superclass. */
6876 entry = _bfd_link_hash_newfunc (entry, table, string);
6879 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6880 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6882 /* Set local fields. */
6885 ret->got = htab->init_got_refcount;
6886 ret->plt = htab->init_plt_refcount;
6887 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6888 - offsetof (struct elf_link_hash_entry, size)));
6889 /* Assume that we have been called by a non-ELF symbol reader.
6890 This flag is then reset by the code which reads an ELF input
6891 file. This ensures that a symbol created by a non-ELF symbol
6892 reader will have the flag set correctly. */
6899 /* Copy data from an indirect symbol to its direct symbol, hiding the
6900 old indirect symbol. Also used for copying flags to a weakdef. */
6903 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6904 struct elf_link_hash_entry *dir,
6905 struct elf_link_hash_entry *ind)
6907 struct elf_link_hash_table *htab;
6909 /* Copy down any references that we may have already seen to the
6910 symbol which just became indirect if DIR isn't a hidden versioned
6913 if (dir->versioned != versioned_hidden)
6915 dir->ref_dynamic |= ind->ref_dynamic;
6916 dir->ref_regular |= ind->ref_regular;
6917 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6918 dir->non_got_ref |= ind->non_got_ref;
6919 dir->needs_plt |= ind->needs_plt;
6920 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6923 if (ind->root.type != bfd_link_hash_indirect)
6926 /* Copy over the global and procedure linkage table refcount entries.
6927 These may have been already set up by a check_relocs routine. */
6928 htab = elf_hash_table (info);
6929 if (ind->got.refcount > htab->init_got_refcount.refcount)
6931 if (dir->got.refcount < 0)
6932 dir->got.refcount = 0;
6933 dir->got.refcount += ind->got.refcount;
6934 ind->got.refcount = htab->init_got_refcount.refcount;
6937 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6939 if (dir->plt.refcount < 0)
6940 dir->plt.refcount = 0;
6941 dir->plt.refcount += ind->plt.refcount;
6942 ind->plt.refcount = htab->init_plt_refcount.refcount;
6945 if (ind->dynindx != -1)
6947 if (dir->dynindx != -1)
6948 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6949 dir->dynindx = ind->dynindx;
6950 dir->dynstr_index = ind->dynstr_index;
6952 ind->dynstr_index = 0;
6957 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6958 struct elf_link_hash_entry *h,
6959 bfd_boolean force_local)
6961 /* STT_GNU_IFUNC symbol must go through PLT. */
6962 if (h->type != STT_GNU_IFUNC)
6964 h->plt = elf_hash_table (info)->init_plt_offset;
6969 h->forced_local = 1;
6970 if (h->dynindx != -1)
6973 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6979 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
6983 _bfd_elf_link_hash_table_init
6984 (struct elf_link_hash_table *table,
6986 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6987 struct bfd_hash_table *,
6989 unsigned int entsize,
6990 enum elf_target_id target_id)
6993 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6995 table->init_got_refcount.refcount = can_refcount - 1;
6996 table->init_plt_refcount.refcount = can_refcount - 1;
6997 table->init_got_offset.offset = -(bfd_vma) 1;
6998 table->init_plt_offset.offset = -(bfd_vma) 1;
6999 /* The first dynamic symbol is a dummy. */
7000 table->dynsymcount = 1;
7002 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7004 table->root.type = bfd_link_elf_hash_table;
7005 table->hash_table_id = target_id;
7010 /* Create an ELF linker hash table. */
7012 struct bfd_link_hash_table *
7013 _bfd_elf_link_hash_table_create (bfd *abfd)
7015 struct elf_link_hash_table *ret;
7016 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7018 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7022 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7023 sizeof (struct elf_link_hash_entry),
7029 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7034 /* Destroy an ELF linker hash table. */
7037 _bfd_elf_link_hash_table_free (bfd *obfd)
7039 struct elf_link_hash_table *htab;
7041 htab = (struct elf_link_hash_table *) obfd->link.hash;
7042 if (htab->dynstr != NULL)
7043 _bfd_elf_strtab_free (htab->dynstr);
7044 _bfd_merge_sections_free (htab->merge_info);
7045 _bfd_generic_link_hash_table_free (obfd);
7048 /* This is a hook for the ELF emulation code in the generic linker to
7049 tell the backend linker what file name to use for the DT_NEEDED
7050 entry for a dynamic object. */
7053 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7055 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7056 && bfd_get_format (abfd) == bfd_object)
7057 elf_dt_name (abfd) = name;
7061 bfd_elf_get_dyn_lib_class (bfd *abfd)
7064 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7065 && bfd_get_format (abfd) == bfd_object)
7066 lib_class = elf_dyn_lib_class (abfd);
7073 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7075 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7076 && bfd_get_format (abfd) == bfd_object)
7077 elf_dyn_lib_class (abfd) = lib_class;
7080 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7081 the linker ELF emulation code. */
7083 struct bfd_link_needed_list *
7084 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7085 struct bfd_link_info *info)
7087 if (! is_elf_hash_table (info->hash))
7089 return elf_hash_table (info)->needed;
7092 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7093 hook for the linker ELF emulation code. */
7095 struct bfd_link_needed_list *
7096 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7097 struct bfd_link_info *info)
7099 if (! is_elf_hash_table (info->hash))
7101 return elf_hash_table (info)->runpath;
7104 /* Get the name actually used for a dynamic object for a link. This
7105 is the SONAME entry if there is one. Otherwise, it is the string
7106 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7109 bfd_elf_get_dt_soname (bfd *abfd)
7111 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7112 && bfd_get_format (abfd) == bfd_object)
7113 return elf_dt_name (abfd);
7117 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7118 the ELF linker emulation code. */
7121 bfd_elf_get_bfd_needed_list (bfd *abfd,
7122 struct bfd_link_needed_list **pneeded)
7125 bfd_byte *dynbuf = NULL;
7126 unsigned int elfsec;
7127 unsigned long shlink;
7128 bfd_byte *extdyn, *extdynend;
7130 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7134 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7135 || bfd_get_format (abfd) != bfd_object)
7138 s = bfd_get_section_by_name (abfd, ".dynamic");
7139 if (s == NULL || s->size == 0)
7142 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7145 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7146 if (elfsec == SHN_BAD)
7149 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7151 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7152 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7155 extdynend = extdyn + s->size;
7156 for (; extdyn < extdynend; extdyn += extdynsize)
7158 Elf_Internal_Dyn dyn;
7160 (*swap_dyn_in) (abfd, extdyn, &dyn);
7162 if (dyn.d_tag == DT_NULL)
7165 if (dyn.d_tag == DT_NEEDED)
7168 struct bfd_link_needed_list *l;
7169 unsigned int tagv = dyn.d_un.d_val;
7172 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7177 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7198 struct elf_symbuf_symbol
7200 unsigned long st_name; /* Symbol name, index in string tbl */
7201 unsigned char st_info; /* Type and binding attributes */
7202 unsigned char st_other; /* Visibilty, and target specific */
7205 struct elf_symbuf_head
7207 struct elf_symbuf_symbol *ssym;
7208 bfd_size_type count;
7209 unsigned int st_shndx;
7216 Elf_Internal_Sym *isym;
7217 struct elf_symbuf_symbol *ssym;
7222 /* Sort references to symbols by ascending section number. */
7225 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7227 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7228 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7230 return s1->st_shndx - s2->st_shndx;
7234 elf_sym_name_compare (const void *arg1, const void *arg2)
7236 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7237 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7238 return strcmp (s1->name, s2->name);
7241 static struct elf_symbuf_head *
7242 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7244 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7245 struct elf_symbuf_symbol *ssym;
7246 struct elf_symbuf_head *ssymbuf, *ssymhead;
7247 bfd_size_type i, shndx_count, total_size;
7249 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7253 for (ind = indbuf, i = 0; i < symcount; i++)
7254 if (isymbuf[i].st_shndx != SHN_UNDEF)
7255 *ind++ = &isymbuf[i];
7258 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7259 elf_sort_elf_symbol);
7262 if (indbufend > indbuf)
7263 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7264 if (ind[0]->st_shndx != ind[1]->st_shndx)
7267 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7268 + (indbufend - indbuf) * sizeof (*ssym));
7269 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7270 if (ssymbuf == NULL)
7276 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7277 ssymbuf->ssym = NULL;
7278 ssymbuf->count = shndx_count;
7279 ssymbuf->st_shndx = 0;
7280 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7282 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7285 ssymhead->ssym = ssym;
7286 ssymhead->count = 0;
7287 ssymhead->st_shndx = (*ind)->st_shndx;
7289 ssym->st_name = (*ind)->st_name;
7290 ssym->st_info = (*ind)->st_info;
7291 ssym->st_other = (*ind)->st_other;
7294 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7295 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7302 /* Check if 2 sections define the same set of local and global
7306 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7307 struct bfd_link_info *info)
7310 const struct elf_backend_data *bed1, *bed2;
7311 Elf_Internal_Shdr *hdr1, *hdr2;
7312 bfd_size_type symcount1, symcount2;
7313 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7314 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7315 Elf_Internal_Sym *isym, *isymend;
7316 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7317 bfd_size_type count1, count2, i;
7318 unsigned int shndx1, shndx2;
7324 /* Both sections have to be in ELF. */
7325 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7326 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7329 if (elf_section_type (sec1) != elf_section_type (sec2))
7332 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7333 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7334 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7337 bed1 = get_elf_backend_data (bfd1);
7338 bed2 = get_elf_backend_data (bfd2);
7339 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7340 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7341 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7342 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7344 if (symcount1 == 0 || symcount2 == 0)
7350 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7351 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7353 if (ssymbuf1 == NULL)
7355 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7357 if (isymbuf1 == NULL)
7360 if (!info->reduce_memory_overheads)
7361 elf_tdata (bfd1)->symbuf = ssymbuf1
7362 = elf_create_symbuf (symcount1, isymbuf1);
7365 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7367 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7369 if (isymbuf2 == NULL)
7372 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7373 elf_tdata (bfd2)->symbuf = ssymbuf2
7374 = elf_create_symbuf (symcount2, isymbuf2);
7377 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7379 /* Optimized faster version. */
7380 bfd_size_type lo, hi, mid;
7381 struct elf_symbol *symp;
7382 struct elf_symbuf_symbol *ssym, *ssymend;
7385 hi = ssymbuf1->count;
7390 mid = (lo + hi) / 2;
7391 if (shndx1 < ssymbuf1[mid].st_shndx)
7393 else if (shndx1 > ssymbuf1[mid].st_shndx)
7397 count1 = ssymbuf1[mid].count;
7404 hi = ssymbuf2->count;
7409 mid = (lo + hi) / 2;
7410 if (shndx2 < ssymbuf2[mid].st_shndx)
7412 else if (shndx2 > ssymbuf2[mid].st_shndx)
7416 count2 = ssymbuf2[mid].count;
7422 if (count1 == 0 || count2 == 0 || count1 != count2)
7426 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7428 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7429 if (symtable1 == NULL || symtable2 == NULL)
7433 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7434 ssym < ssymend; ssym++, symp++)
7436 symp->u.ssym = ssym;
7437 symp->name = bfd_elf_string_from_elf_section (bfd1,
7443 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7444 ssym < ssymend; ssym++, symp++)
7446 symp->u.ssym = ssym;
7447 symp->name = bfd_elf_string_from_elf_section (bfd2,
7452 /* Sort symbol by name. */
7453 qsort (symtable1, count1, sizeof (struct elf_symbol),
7454 elf_sym_name_compare);
7455 qsort (symtable2, count1, sizeof (struct elf_symbol),
7456 elf_sym_name_compare);
7458 for (i = 0; i < count1; i++)
7459 /* Two symbols must have the same binding, type and name. */
7460 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7461 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7462 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7469 symtable1 = (struct elf_symbol *)
7470 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7471 symtable2 = (struct elf_symbol *)
7472 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7473 if (symtable1 == NULL || symtable2 == NULL)
7476 /* Count definitions in the section. */
7478 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7479 if (isym->st_shndx == shndx1)
7480 symtable1[count1++].u.isym = isym;
7483 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7484 if (isym->st_shndx == shndx2)
7485 symtable2[count2++].u.isym = isym;
7487 if (count1 == 0 || count2 == 0 || count1 != count2)
7490 for (i = 0; i < count1; i++)
7492 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7493 symtable1[i].u.isym->st_name);
7495 for (i = 0; i < count2; i++)
7497 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7498 symtable2[i].u.isym->st_name);
7500 /* Sort symbol by name. */
7501 qsort (symtable1, count1, sizeof (struct elf_symbol),
7502 elf_sym_name_compare);
7503 qsort (symtable2, count1, sizeof (struct elf_symbol),
7504 elf_sym_name_compare);
7506 for (i = 0; i < count1; i++)
7507 /* Two symbols must have the same binding, type and name. */
7508 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7509 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7510 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7528 /* Return TRUE if 2 section types are compatible. */
7531 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7532 bfd *bbfd, const asection *bsec)
7536 || abfd->xvec->flavour != bfd_target_elf_flavour
7537 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7540 return elf_section_type (asec) == elf_section_type (bsec);
7543 /* Final phase of ELF linker. */
7545 /* A structure we use to avoid passing large numbers of arguments. */
7547 struct elf_final_link_info
7549 /* General link information. */
7550 struct bfd_link_info *info;
7553 /* Symbol string table. */
7554 struct elf_strtab_hash *symstrtab;
7555 /* .hash section. */
7557 /* symbol version section (.gnu.version). */
7558 asection *symver_sec;
7559 /* Buffer large enough to hold contents of any section. */
7561 /* Buffer large enough to hold external relocs of any section. */
7562 void *external_relocs;
7563 /* Buffer large enough to hold internal relocs of any section. */
7564 Elf_Internal_Rela *internal_relocs;
7565 /* Buffer large enough to hold external local symbols of any input
7567 bfd_byte *external_syms;
7568 /* And a buffer for symbol section indices. */
7569 Elf_External_Sym_Shndx *locsym_shndx;
7570 /* Buffer large enough to hold internal local symbols of any input
7572 Elf_Internal_Sym *internal_syms;
7573 /* Array large enough to hold a symbol index for each local symbol
7574 of any input BFD. */
7576 /* Array large enough to hold a section pointer for each local
7577 symbol of any input BFD. */
7578 asection **sections;
7579 /* Buffer for SHT_SYMTAB_SHNDX section. */
7580 Elf_External_Sym_Shndx *symshndxbuf;
7581 /* Number of STT_FILE syms seen. */
7582 size_t filesym_count;
7585 /* This struct is used to pass information to elf_link_output_extsym. */
7587 struct elf_outext_info
7590 bfd_boolean localsyms;
7591 bfd_boolean file_sym_done;
7592 struct elf_final_link_info *flinfo;
7596 /* Support for evaluating a complex relocation.
7598 Complex relocations are generalized, self-describing relocations. The
7599 implementation of them consists of two parts: complex symbols, and the
7600 relocations themselves.
7602 The relocations are use a reserved elf-wide relocation type code (R_RELC
7603 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7604 information (start bit, end bit, word width, etc) into the addend. This
7605 information is extracted from CGEN-generated operand tables within gas.
7607 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7608 internal) representing prefix-notation expressions, including but not
7609 limited to those sorts of expressions normally encoded as addends in the
7610 addend field. The symbol mangling format is:
7613 | <unary-operator> ':' <node>
7614 | <binary-operator> ':' <node> ':' <node>
7617 <literal> := 's' <digits=N> ':' <N character symbol name>
7618 | 'S' <digits=N> ':' <N character section name>
7622 <binary-operator> := as in C
7623 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7626 set_symbol_value (bfd *bfd_with_globals,
7627 Elf_Internal_Sym *isymbuf,
7632 struct elf_link_hash_entry **sym_hashes;
7633 struct elf_link_hash_entry *h;
7634 size_t extsymoff = locsymcount;
7636 if (symidx < locsymcount)
7638 Elf_Internal_Sym *sym;
7640 sym = isymbuf + symidx;
7641 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7643 /* It is a local symbol: move it to the
7644 "absolute" section and give it a value. */
7645 sym->st_shndx = SHN_ABS;
7646 sym->st_value = val;
7649 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7653 /* It is a global symbol: set its link type
7654 to "defined" and give it a value. */
7656 sym_hashes = elf_sym_hashes (bfd_with_globals);
7657 h = sym_hashes [symidx - extsymoff];
7658 while (h->root.type == bfd_link_hash_indirect
7659 || h->root.type == bfd_link_hash_warning)
7660 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7661 h->root.type = bfd_link_hash_defined;
7662 h->root.u.def.value = val;
7663 h->root.u.def.section = bfd_abs_section_ptr;
7667 resolve_symbol (const char *name,
7669 struct elf_final_link_info *flinfo,
7671 Elf_Internal_Sym *isymbuf,
7674 Elf_Internal_Sym *sym;
7675 struct bfd_link_hash_entry *global_entry;
7676 const char *candidate = NULL;
7677 Elf_Internal_Shdr *symtab_hdr;
7680 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7682 for (i = 0; i < locsymcount; ++ i)
7686 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7689 candidate = bfd_elf_string_from_elf_section (input_bfd,
7690 symtab_hdr->sh_link,
7693 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7694 name, candidate, (unsigned long) sym->st_value);
7696 if (candidate && strcmp (candidate, name) == 0)
7698 asection *sec = flinfo->sections [i];
7700 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7701 *result += sec->output_offset + sec->output_section->vma;
7703 printf ("Found symbol with value %8.8lx\n",
7704 (unsigned long) *result);
7710 /* Hmm, haven't found it yet. perhaps it is a global. */
7711 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7712 FALSE, FALSE, TRUE);
7716 if (global_entry->type == bfd_link_hash_defined
7717 || global_entry->type == bfd_link_hash_defweak)
7719 *result = (global_entry->u.def.value
7720 + global_entry->u.def.section->output_section->vma
7721 + global_entry->u.def.section->output_offset);
7723 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7724 global_entry->root.string, (unsigned long) *result);
7733 resolve_section (const char *name,
7740 for (curr = sections; curr; curr = curr->next)
7741 if (strcmp (curr->name, name) == 0)
7743 *result = curr->vma;
7747 /* Hmm. still haven't found it. try pseudo-section names. */
7748 for (curr = sections; curr; curr = curr->next)
7750 len = strlen (curr->name);
7751 if (len > strlen (name))
7754 if (strncmp (curr->name, name, len) == 0)
7756 if (strncmp (".end", name + len, 4) == 0)
7758 *result = curr->vma + curr->size;
7762 /* Insert more pseudo-section names here, if you like. */
7770 undefined_reference (const char *reftype, const char *name)
7772 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7777 eval_symbol (bfd_vma *result,
7780 struct elf_final_link_info *flinfo,
7782 Elf_Internal_Sym *isymbuf,
7791 const char *sym = *symp;
7793 bfd_boolean symbol_is_section = FALSE;
7798 if (len < 1 || len > sizeof (symbuf))
7800 bfd_set_error (bfd_error_invalid_operation);
7813 *result = strtoul (sym, (char **) symp, 16);
7817 symbol_is_section = TRUE;
7820 symlen = strtol (sym, (char **) symp, 10);
7821 sym = *symp + 1; /* Skip the trailing ':'. */
7823 if (symend < sym || symlen + 1 > sizeof (symbuf))
7825 bfd_set_error (bfd_error_invalid_operation);
7829 memcpy (symbuf, sym, symlen);
7830 symbuf[symlen] = '\0';
7831 *symp = sym + symlen;
7833 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7834 the symbol as a section, or vice-versa. so we're pretty liberal in our
7835 interpretation here; section means "try section first", not "must be a
7836 section", and likewise with symbol. */
7838 if (symbol_is_section)
7840 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7841 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7842 isymbuf, locsymcount))
7844 undefined_reference ("section", symbuf);
7850 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7851 isymbuf, locsymcount)
7852 && !resolve_section (symbuf, flinfo->output_bfd->sections,
7855 undefined_reference ("symbol", symbuf);
7862 /* All that remains are operators. */
7864 #define UNARY_OP(op) \
7865 if (strncmp (sym, #op, strlen (#op)) == 0) \
7867 sym += strlen (#op); \
7871 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7872 isymbuf, locsymcount, signed_p)) \
7875 *result = op ((bfd_signed_vma) a); \
7881 #define BINARY_OP(op) \
7882 if (strncmp (sym, #op, strlen (#op)) == 0) \
7884 sym += strlen (#op); \
7888 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7889 isymbuf, locsymcount, signed_p)) \
7892 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
7893 isymbuf, locsymcount, signed_p)) \
7896 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7926 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7927 bfd_set_error (bfd_error_invalid_operation);
7933 put_value (bfd_vma size,
7934 unsigned long chunksz,
7939 location += (size - chunksz);
7941 for (; size; size -= chunksz, location -= chunksz)
7946 bfd_put_8 (input_bfd, x, location);
7950 bfd_put_16 (input_bfd, x, location);
7954 bfd_put_32 (input_bfd, x, location);
7955 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
7961 bfd_put_64 (input_bfd, x, location);
7962 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
7975 get_value (bfd_vma size,
7976 unsigned long chunksz,
7983 /* Sanity checks. */
7984 BFD_ASSERT (chunksz <= sizeof (x)
7987 && (size % chunksz) == 0
7988 && input_bfd != NULL
7989 && location != NULL);
7991 if (chunksz == sizeof (x))
7993 BFD_ASSERT (size == chunksz);
7995 /* Make sure that we do not perform an undefined shift operation.
7996 We know that size == chunksz so there will only be one iteration
7997 of the loop below. */
8001 shift = 8 * chunksz;
8003 for (; size; size -= chunksz, location += chunksz)
8008 x = (x << shift) | bfd_get_8 (input_bfd, location);
8011 x = (x << shift) | bfd_get_16 (input_bfd, location);
8014 x = (x << shift) | bfd_get_32 (input_bfd, location);
8018 x = (x << shift) | bfd_get_64 (input_bfd, location);
8029 decode_complex_addend (unsigned long *start, /* in bits */
8030 unsigned long *oplen, /* in bits */
8031 unsigned long *len, /* in bits */
8032 unsigned long *wordsz, /* in bytes */
8033 unsigned long *chunksz, /* in bytes */
8034 unsigned long *lsb0_p,
8035 unsigned long *signed_p,
8036 unsigned long *trunc_p,
8037 unsigned long encoded)
8039 * start = encoded & 0x3F;
8040 * len = (encoded >> 6) & 0x3F;
8041 * oplen = (encoded >> 12) & 0x3F;
8042 * wordsz = (encoded >> 18) & 0xF;
8043 * chunksz = (encoded >> 22) & 0xF;
8044 * lsb0_p = (encoded >> 27) & 1;
8045 * signed_p = (encoded >> 28) & 1;
8046 * trunc_p = (encoded >> 29) & 1;
8049 bfd_reloc_status_type
8050 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8051 asection *input_section ATTRIBUTE_UNUSED,
8053 Elf_Internal_Rela *rel,
8056 bfd_vma shift, x, mask;
8057 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8058 bfd_reloc_status_type r;
8060 /* Perform this reloc, since it is complex.
8061 (this is not to say that it necessarily refers to a complex
8062 symbol; merely that it is a self-describing CGEN based reloc.
8063 i.e. the addend has the complete reloc information (bit start, end,
8064 word size, etc) encoded within it.). */
8066 decode_complex_addend (&start, &oplen, &len, &wordsz,
8067 &chunksz, &lsb0_p, &signed_p,
8068 &trunc_p, rel->r_addend);
8070 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8073 shift = (start + 1) - len;
8075 shift = (8 * wordsz) - (start + len);
8077 /* FIXME: octets_per_byte. */
8078 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
8081 printf ("Doing complex reloc: "
8082 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8083 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8084 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8085 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8086 oplen, (unsigned long) x, (unsigned long) mask,
8087 (unsigned long) relocation);
8092 /* Now do an overflow check. */
8093 r = bfd_check_overflow ((signed_p
8094 ? complain_overflow_signed
8095 : complain_overflow_unsigned),
8096 len, 0, (8 * wordsz),
8100 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8103 printf (" relocation: %8.8lx\n"
8104 " shifted mask: %8.8lx\n"
8105 " shifted/masked reloc: %8.8lx\n"
8106 " result: %8.8lx\n",
8107 (unsigned long) relocation, (unsigned long) (mask << shift),
8108 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8110 /* FIXME: octets_per_byte. */
8111 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
8115 /* Functions to read r_offset from external (target order) reloc
8116 entry. Faster than bfd_getl32 et al, because we let the compiler
8117 know the value is aligned. */
8120 ext32l_r_offset (const void *p)
8127 const union aligned32 *a
8128 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8130 uint32_t aval = ( (uint32_t) a->c[0]
8131 | (uint32_t) a->c[1] << 8
8132 | (uint32_t) a->c[2] << 16
8133 | (uint32_t) a->c[3] << 24);
8138 ext32b_r_offset (const void *p)
8145 const union aligned32 *a
8146 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8148 uint32_t aval = ( (uint32_t) a->c[0] << 24
8149 | (uint32_t) a->c[1] << 16
8150 | (uint32_t) a->c[2] << 8
8151 | (uint32_t) a->c[3]);
8155 #ifdef BFD_HOST_64_BIT
8157 ext64l_r_offset (const void *p)
8164 const union aligned64 *a
8165 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8167 uint64_t aval = ( (uint64_t) a->c[0]
8168 | (uint64_t) a->c[1] << 8
8169 | (uint64_t) a->c[2] << 16
8170 | (uint64_t) a->c[3] << 24
8171 | (uint64_t) a->c[4] << 32
8172 | (uint64_t) a->c[5] << 40
8173 | (uint64_t) a->c[6] << 48
8174 | (uint64_t) a->c[7] << 56);
8179 ext64b_r_offset (const void *p)
8186 const union aligned64 *a
8187 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8189 uint64_t aval = ( (uint64_t) a->c[0] << 56
8190 | (uint64_t) a->c[1] << 48
8191 | (uint64_t) a->c[2] << 40
8192 | (uint64_t) a->c[3] << 32
8193 | (uint64_t) a->c[4] << 24
8194 | (uint64_t) a->c[5] << 16
8195 | (uint64_t) a->c[6] << 8
8196 | (uint64_t) a->c[7]);
8201 /* When performing a relocatable link, the input relocations are
8202 preserved. But, if they reference global symbols, the indices
8203 referenced must be updated. Update all the relocations found in
8207 elf_link_adjust_relocs (bfd *abfd,
8208 struct bfd_elf_section_reloc_data *reldata,
8212 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8214 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8215 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8216 bfd_vma r_type_mask;
8218 unsigned int count = reldata->count;
8219 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8221 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8223 swap_in = bed->s->swap_reloc_in;
8224 swap_out = bed->s->swap_reloc_out;
8226 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8228 swap_in = bed->s->swap_reloca_in;
8229 swap_out = bed->s->swap_reloca_out;
8234 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8237 if (bed->s->arch_size == 32)
8244 r_type_mask = 0xffffffff;
8248 erela = reldata->hdr->contents;
8249 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8251 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8254 if (*rel_hash == NULL)
8257 BFD_ASSERT ((*rel_hash)->indx >= 0);
8259 (*swap_in) (abfd, erela, irela);
8260 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8261 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8262 | (irela[j].r_info & r_type_mask));
8263 (*swap_out) (abfd, irela, erela);
8266 if (sort && count != 0)
8268 bfd_vma (*ext_r_off) (const void *);
8271 bfd_byte *base, *end, *p, *loc;
8272 bfd_byte *buf = NULL;
8274 if (bed->s->arch_size == 32)
8276 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8277 ext_r_off = ext32l_r_offset;
8278 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8279 ext_r_off = ext32b_r_offset;
8285 #ifdef BFD_HOST_64_BIT
8286 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8287 ext_r_off = ext64l_r_offset;
8288 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8289 ext_r_off = ext64b_r_offset;
8295 /* Must use a stable sort here. A modified insertion sort,
8296 since the relocs are mostly sorted already. */
8297 elt_size = reldata->hdr->sh_entsize;
8298 base = reldata->hdr->contents;
8299 end = base + count * elt_size;
8300 if (elt_size > sizeof (Elf64_External_Rela))
8303 /* Ensure the first element is lowest. This acts as a sentinel,
8304 speeding the main loop below. */
8305 r_off = (*ext_r_off) (base);
8306 for (p = loc = base; (p += elt_size) < end; )
8308 bfd_vma r_off2 = (*ext_r_off) (p);
8317 /* Don't just swap *base and *loc as that changes the order
8318 of the original base[0] and base[1] if they happen to
8319 have the same r_offset. */
8320 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8321 memcpy (onebuf, loc, elt_size);
8322 memmove (base + elt_size, base, loc - base);
8323 memcpy (base, onebuf, elt_size);
8326 for (p = base + elt_size; (p += elt_size) < end; )
8328 /* base to p is sorted, *p is next to insert. */
8329 r_off = (*ext_r_off) (p);
8330 /* Search the sorted region for location to insert. */
8332 while (r_off < (*ext_r_off) (loc))
8337 /* Chances are there is a run of relocs to insert here,
8338 from one of more input files. Files are not always
8339 linked in order due to the way elf_link_input_bfd is
8340 called. See pr17666. */
8341 size_t sortlen = p - loc;
8342 bfd_vma r_off2 = (*ext_r_off) (loc);
8343 size_t runlen = elt_size;
8344 size_t buf_size = 96 * 1024;
8345 while (p + runlen < end
8346 && (sortlen <= buf_size
8347 || runlen + elt_size <= buf_size)
8348 && r_off2 > (*ext_r_off) (p + runlen))
8352 buf = bfd_malloc (buf_size);
8356 if (runlen < sortlen)
8358 memcpy (buf, p, runlen);
8359 memmove (loc + runlen, loc, sortlen);
8360 memcpy (loc, buf, runlen);
8364 memcpy (buf, loc, sortlen);
8365 memmove (loc, p, runlen);
8366 memcpy (loc + runlen, buf, sortlen);
8368 p += runlen - elt_size;
8371 /* Hashes are no longer valid. */
8372 free (reldata->hashes);
8373 reldata->hashes = NULL;
8379 struct elf_link_sort_rela
8385 enum elf_reloc_type_class type;
8386 /* We use this as an array of size int_rels_per_ext_rel. */
8387 Elf_Internal_Rela rela[1];
8391 elf_link_sort_cmp1 (const void *A, const void *B)
8393 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8394 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8395 int relativea, relativeb;
8397 relativea = a->type == reloc_class_relative;
8398 relativeb = b->type == reloc_class_relative;
8400 if (relativea < relativeb)
8402 if (relativea > relativeb)
8404 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8406 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8408 if (a->rela->r_offset < b->rela->r_offset)
8410 if (a->rela->r_offset > b->rela->r_offset)
8416 elf_link_sort_cmp2 (const void *A, const void *B)
8418 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8419 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8421 if (a->type < b->type)
8423 if (a->type > b->type)
8425 if (a->u.offset < b->u.offset)
8427 if (a->u.offset > b->u.offset)
8429 if (a->rela->r_offset < b->rela->r_offset)
8431 if (a->rela->r_offset > b->rela->r_offset)
8437 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8439 asection *dynamic_relocs;
8442 bfd_size_type count, size;
8443 size_t i, ret, sort_elt, ext_size;
8444 bfd_byte *sort, *s_non_relative, *p;
8445 struct elf_link_sort_rela *sq;
8446 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8447 int i2e = bed->s->int_rels_per_ext_rel;
8448 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8449 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8450 struct bfd_link_order *lo;
8452 bfd_boolean use_rela;
8454 /* Find a dynamic reloc section. */
8455 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8456 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8457 if (rela_dyn != NULL && rela_dyn->size > 0
8458 && rel_dyn != NULL && rel_dyn->size > 0)
8460 bfd_boolean use_rela_initialised = FALSE;
8462 /* This is just here to stop gcc from complaining.
8463 It's initialization checking code is not perfect. */
8466 /* Both sections are present. Examine the sizes
8467 of the indirect sections to help us choose. */
8468 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8469 if (lo->type == bfd_indirect_link_order)
8471 asection *o = lo->u.indirect.section;
8473 if ((o->size % bed->s->sizeof_rela) == 0)
8475 if ((o->size % bed->s->sizeof_rel) == 0)
8476 /* Section size is divisible by both rel and rela sizes.
8477 It is of no help to us. */
8481 /* Section size is only divisible by rela. */
8482 if (use_rela_initialised && (use_rela == FALSE))
8485 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8486 bfd_set_error (bfd_error_invalid_operation);
8492 use_rela_initialised = TRUE;
8496 else if ((o->size % bed->s->sizeof_rel) == 0)
8498 /* Section size is only divisible by rel. */
8499 if (use_rela_initialised && (use_rela == TRUE))
8502 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8503 bfd_set_error (bfd_error_invalid_operation);
8509 use_rela_initialised = TRUE;
8514 /* The section size is not divisible by either - something is wrong. */
8516 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8517 bfd_set_error (bfd_error_invalid_operation);
8522 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8523 if (lo->type == bfd_indirect_link_order)
8525 asection *o = lo->u.indirect.section;
8527 if ((o->size % bed->s->sizeof_rela) == 0)
8529 if ((o->size % bed->s->sizeof_rel) == 0)
8530 /* Section size is divisible by both rel and rela sizes.
8531 It is of no help to us. */
8535 /* Section size is only divisible by rela. */
8536 if (use_rela_initialised && (use_rela == FALSE))
8539 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8540 bfd_set_error (bfd_error_invalid_operation);
8546 use_rela_initialised = TRUE;
8550 else if ((o->size % bed->s->sizeof_rel) == 0)
8552 /* Section size is only divisible by rel. */
8553 if (use_rela_initialised && (use_rela == TRUE))
8556 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8557 bfd_set_error (bfd_error_invalid_operation);
8563 use_rela_initialised = TRUE;
8568 /* The section size is not divisible by either - something is wrong. */
8570 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8571 bfd_set_error (bfd_error_invalid_operation);
8576 if (! use_rela_initialised)
8580 else if (rela_dyn != NULL && rela_dyn->size > 0)
8582 else if (rel_dyn != NULL && rel_dyn->size > 0)
8589 dynamic_relocs = rela_dyn;
8590 ext_size = bed->s->sizeof_rela;
8591 swap_in = bed->s->swap_reloca_in;
8592 swap_out = bed->s->swap_reloca_out;
8596 dynamic_relocs = rel_dyn;
8597 ext_size = bed->s->sizeof_rel;
8598 swap_in = bed->s->swap_reloc_in;
8599 swap_out = bed->s->swap_reloc_out;
8603 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8604 if (lo->type == bfd_indirect_link_order)
8605 size += lo->u.indirect.section->size;
8607 if (size != dynamic_relocs->size)
8610 sort_elt = (sizeof (struct elf_link_sort_rela)
8611 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8613 count = dynamic_relocs->size / ext_size;
8616 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8620 (*info->callbacks->warning)
8621 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8625 if (bed->s->arch_size == 32)
8626 r_sym_mask = ~(bfd_vma) 0xff;
8628 r_sym_mask = ~(bfd_vma) 0xffffffff;
8630 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8631 if (lo->type == bfd_indirect_link_order)
8633 bfd_byte *erel, *erelend;
8634 asection *o = lo->u.indirect.section;
8636 if (o->contents == NULL && o->size != 0)
8638 /* This is a reloc section that is being handled as a normal
8639 section. See bfd_section_from_shdr. We can't combine
8640 relocs in this case. */
8645 erelend = o->contents + o->size;
8646 /* FIXME: octets_per_byte. */
8647 p = sort + o->output_offset / ext_size * sort_elt;
8649 while (erel < erelend)
8651 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8653 (*swap_in) (abfd, erel, s->rela);
8654 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8655 s->u.sym_mask = r_sym_mask;
8661 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8663 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8665 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8666 if (s->type != reloc_class_relative)
8672 sq = (struct elf_link_sort_rela *) s_non_relative;
8673 for (; i < count; i++, p += sort_elt)
8675 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8676 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8678 sp->u.offset = sq->rela->r_offset;
8681 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8683 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8684 if (lo->type == bfd_indirect_link_order)
8686 bfd_byte *erel, *erelend;
8687 asection *o = lo->u.indirect.section;
8690 erelend = o->contents + o->size;
8691 /* FIXME: octets_per_byte. */
8692 p = sort + o->output_offset / ext_size * sort_elt;
8693 while (erel < erelend)
8695 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8696 (*swap_out) (abfd, s->rela, erel);
8703 *psec = dynamic_relocs;
8707 /* Add a symbol to the output symbol string table. */
8710 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8712 Elf_Internal_Sym *elfsym,
8713 asection *input_sec,
8714 struct elf_link_hash_entry *h)
8716 int (*output_symbol_hook)
8717 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8718 struct elf_link_hash_entry *);
8719 struct elf_link_hash_table *hash_table;
8720 const struct elf_backend_data *bed;
8721 bfd_size_type strtabsize;
8723 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8725 bed = get_elf_backend_data (flinfo->output_bfd);
8726 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8727 if (output_symbol_hook != NULL)
8729 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8736 || (input_sec->flags & SEC_EXCLUDE))
8737 elfsym->st_name = (unsigned long) -1;
8740 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8741 to get the final offset for st_name. */
8743 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8745 if (elfsym->st_name == (unsigned long) -1)
8749 hash_table = elf_hash_table (flinfo->info);
8750 strtabsize = hash_table->strtabsize;
8751 if (strtabsize <= hash_table->strtabcount)
8753 strtabsize += strtabsize;
8754 hash_table->strtabsize = strtabsize;
8755 strtabsize *= sizeof (*hash_table->strtab);
8757 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8759 if (hash_table->strtab == NULL)
8762 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8763 hash_table->strtab[hash_table->strtabcount].dest_index
8764 = hash_table->strtabcount;
8765 hash_table->strtab[hash_table->strtabcount].destshndx_index
8766 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8768 bfd_get_symcount (flinfo->output_bfd) += 1;
8769 hash_table->strtabcount += 1;
8774 /* Swap symbols out to the symbol table and flush the output symbols to
8778 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8780 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8781 bfd_size_type amt, i;
8782 const struct elf_backend_data *bed;
8784 Elf_Internal_Shdr *hdr;
8788 if (!hash_table->strtabcount)
8791 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8793 bed = get_elf_backend_data (flinfo->output_bfd);
8795 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8796 symbuf = (bfd_byte *) bfd_malloc (amt);
8800 if (flinfo->symshndxbuf)
8802 amt = (sizeof (Elf_External_Sym_Shndx)
8803 * (bfd_get_symcount (flinfo->output_bfd)));
8804 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8805 if (flinfo->symshndxbuf == NULL)
8812 for (i = 0; i < hash_table->strtabcount; i++)
8814 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8815 if (elfsym->sym.st_name == (unsigned long) -1)
8816 elfsym->sym.st_name = 0;
8819 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8820 elfsym->sym.st_name);
8821 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8822 ((bfd_byte *) symbuf
8823 + (elfsym->dest_index
8824 * bed->s->sizeof_sym)),
8825 (flinfo->symshndxbuf
8826 + elfsym->destshndx_index));
8829 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8830 pos = hdr->sh_offset + hdr->sh_size;
8831 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8832 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8833 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8835 hdr->sh_size += amt;
8843 free (hash_table->strtab);
8844 hash_table->strtab = NULL;
8849 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8852 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8854 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8855 && sym->st_shndx < SHN_LORESERVE)
8857 /* The gABI doesn't support dynamic symbols in output sections
8859 (*_bfd_error_handler)
8860 (_("%B: Too many sections: %d (>= %d)"),
8861 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8862 bfd_set_error (bfd_error_nonrepresentable_section);
8868 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8869 allowing an unsatisfied unversioned symbol in the DSO to match a
8870 versioned symbol that would normally require an explicit version.
8871 We also handle the case that a DSO references a hidden symbol
8872 which may be satisfied by a versioned symbol in another DSO. */
8875 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8876 const struct elf_backend_data *bed,
8877 struct elf_link_hash_entry *h)
8880 struct elf_link_loaded_list *loaded;
8882 if (!is_elf_hash_table (info->hash))
8885 /* Check indirect symbol. */
8886 while (h->root.type == bfd_link_hash_indirect)
8887 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8889 switch (h->root.type)
8895 case bfd_link_hash_undefined:
8896 case bfd_link_hash_undefweak:
8897 abfd = h->root.u.undef.abfd;
8898 if ((abfd->flags & DYNAMIC) == 0
8899 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8903 case bfd_link_hash_defined:
8904 case bfd_link_hash_defweak:
8905 abfd = h->root.u.def.section->owner;
8908 case bfd_link_hash_common:
8909 abfd = h->root.u.c.p->section->owner;
8912 BFD_ASSERT (abfd != NULL);
8914 for (loaded = elf_hash_table (info)->loaded;
8916 loaded = loaded->next)
8919 Elf_Internal_Shdr *hdr;
8920 bfd_size_type symcount;
8921 bfd_size_type extsymcount;
8922 bfd_size_type extsymoff;
8923 Elf_Internal_Shdr *versymhdr;
8924 Elf_Internal_Sym *isym;
8925 Elf_Internal_Sym *isymend;
8926 Elf_Internal_Sym *isymbuf;
8927 Elf_External_Versym *ever;
8928 Elf_External_Versym *extversym;
8930 input = loaded->abfd;
8932 /* We check each DSO for a possible hidden versioned definition. */
8934 || (input->flags & DYNAMIC) == 0
8935 || elf_dynversym (input) == 0)
8938 hdr = &elf_tdata (input)->dynsymtab_hdr;
8940 symcount = hdr->sh_size / bed->s->sizeof_sym;
8941 if (elf_bad_symtab (input))
8943 extsymcount = symcount;
8948 extsymcount = symcount - hdr->sh_info;
8949 extsymoff = hdr->sh_info;
8952 if (extsymcount == 0)
8955 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8957 if (isymbuf == NULL)
8960 /* Read in any version definitions. */
8961 versymhdr = &elf_tdata (input)->dynversym_hdr;
8962 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
8963 if (extversym == NULL)
8966 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8967 || (bfd_bread (extversym, versymhdr->sh_size, input)
8968 != versymhdr->sh_size))
8976 ever = extversym + extsymoff;
8977 isymend = isymbuf + extsymcount;
8978 for (isym = isymbuf; isym < isymend; isym++, ever++)
8981 Elf_Internal_Versym iver;
8982 unsigned short version_index;
8984 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8985 || isym->st_shndx == SHN_UNDEF)
8988 name = bfd_elf_string_from_elf_section (input,
8991 if (strcmp (name, h->root.root.string) != 0)
8994 _bfd_elf_swap_versym_in (input, ever, &iver);
8996 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8998 && h->forced_local))
9000 /* If we have a non-hidden versioned sym, then it should
9001 have provided a definition for the undefined sym unless
9002 it is defined in a non-shared object and forced local.
9007 version_index = iver.vs_vers & VERSYM_VERSION;
9008 if (version_index == 1 || version_index == 2)
9010 /* This is the base or first version. We can use it. */
9024 /* Add an external symbol to the symbol table. This is called from
9025 the hash table traversal routine. When generating a shared object,
9026 we go through the symbol table twice. The first time we output
9027 anything that might have been forced to local scope in a version
9028 script. The second time we output the symbols that are still
9032 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9034 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9035 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9036 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9038 Elf_Internal_Sym sym;
9039 asection *input_sec;
9040 const struct elf_backend_data *bed;
9043 /* A symbol is bound locally if it is forced local or it is locally
9044 defined, hidden versioned, not referenced by shared library and
9045 not exported when linking executable. */
9046 bfd_boolean local_bind = (h->forced_local
9047 || (bfd_link_executable (flinfo->info)
9048 && !flinfo->info->export_dynamic
9052 && h->versioned == versioned_hidden));
9054 if (h->root.type == bfd_link_hash_warning)
9056 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9057 if (h->root.type == bfd_link_hash_new)
9061 /* Decide whether to output this symbol in this pass. */
9062 if (eoinfo->localsyms)
9073 bed = get_elf_backend_data (flinfo->output_bfd);
9075 if (h->root.type == bfd_link_hash_undefined)
9077 /* If we have an undefined symbol reference here then it must have
9078 come from a shared library that is being linked in. (Undefined
9079 references in regular files have already been handled unless
9080 they are in unreferenced sections which are removed by garbage
9082 bfd_boolean ignore_undef = FALSE;
9084 /* Some symbols may be special in that the fact that they're
9085 undefined can be safely ignored - let backend determine that. */
9086 if (bed->elf_backend_ignore_undef_symbol)
9087 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9089 /* If we are reporting errors for this situation then do so now. */
9092 && (!h->ref_regular || flinfo->info->gc_sections)
9093 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9094 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9096 if (!(flinfo->info->callbacks->undefined_symbol
9097 (flinfo->info, h->root.root.string,
9098 h->ref_regular ? NULL : h->root.u.undef.abfd,
9100 (flinfo->info->unresolved_syms_in_shared_libs
9101 == RM_GENERATE_ERROR))))
9103 bfd_set_error (bfd_error_bad_value);
9104 eoinfo->failed = TRUE;
9110 /* We should also warn if a forced local symbol is referenced from
9111 shared libraries. */
9112 if (bfd_link_executable (flinfo->info)
9117 && h->ref_dynamic_nonweak
9118 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9122 struct elf_link_hash_entry *hi = h;
9124 /* Check indirect symbol. */
9125 while (hi->root.type == bfd_link_hash_indirect)
9126 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9128 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9129 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9130 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9131 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9133 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9134 def_bfd = flinfo->output_bfd;
9135 if (hi->root.u.def.section != bfd_abs_section_ptr)
9136 def_bfd = hi->root.u.def.section->owner;
9137 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
9138 h->root.root.string);
9139 bfd_set_error (bfd_error_bad_value);
9140 eoinfo->failed = TRUE;
9144 /* We don't want to output symbols that have never been mentioned by
9145 a regular file, or that we have been told to strip. However, if
9146 h->indx is set to -2, the symbol is used by a reloc and we must
9151 else if ((h->def_dynamic
9153 || h->root.type == bfd_link_hash_new)
9157 else if (flinfo->info->strip == strip_all)
9159 else if (flinfo->info->strip == strip_some
9160 && bfd_hash_lookup (flinfo->info->keep_hash,
9161 h->root.root.string, FALSE, FALSE) == NULL)
9163 else if ((h->root.type == bfd_link_hash_defined
9164 || h->root.type == bfd_link_hash_defweak)
9165 && ((flinfo->info->strip_discarded
9166 && discarded_section (h->root.u.def.section))
9167 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9168 && h->root.u.def.section->owner != NULL
9169 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9171 else if ((h->root.type == bfd_link_hash_undefined
9172 || h->root.type == bfd_link_hash_undefweak)
9173 && h->root.u.undef.abfd != NULL
9174 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9177 /* If we're stripping it, and it's not a dynamic symbol, there's
9178 nothing else to do. However, if it is a forced local symbol or
9179 an ifunc symbol we need to give the backend finish_dynamic_symbol
9180 function a chance to make it dynamic. */
9183 && h->type != STT_GNU_IFUNC
9184 && !h->forced_local)
9188 sym.st_size = h->size;
9189 sym.st_other = h->other;
9192 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
9193 /* Turn off visibility on local symbol. */
9194 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9196 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9197 else if (h->unique_global && h->def_regular)
9198 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
9199 else if (h->root.type == bfd_link_hash_undefweak
9200 || h->root.type == bfd_link_hash_defweak)
9201 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
9203 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
9204 sym.st_target_internal = h->target_internal;
9206 switch (h->root.type)
9209 case bfd_link_hash_new:
9210 case bfd_link_hash_warning:
9214 case bfd_link_hash_undefined:
9215 case bfd_link_hash_undefweak:
9216 input_sec = bfd_und_section_ptr;
9217 sym.st_shndx = SHN_UNDEF;
9220 case bfd_link_hash_defined:
9221 case bfd_link_hash_defweak:
9223 input_sec = h->root.u.def.section;
9224 if (input_sec->output_section != NULL)
9227 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9228 input_sec->output_section);
9229 if (sym.st_shndx == SHN_BAD)
9231 (*_bfd_error_handler)
9232 (_("%B: could not find output section %A for input section %A"),
9233 flinfo->output_bfd, input_sec->output_section, input_sec);
9234 bfd_set_error (bfd_error_nonrepresentable_section);
9235 eoinfo->failed = TRUE;
9239 /* ELF symbols in relocatable files are section relative,
9240 but in nonrelocatable files they are virtual
9242 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9243 if (!bfd_link_relocatable (flinfo->info))
9245 sym.st_value += input_sec->output_section->vma;
9246 if (h->type == STT_TLS)
9248 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9249 if (tls_sec != NULL)
9250 sym.st_value -= tls_sec->vma;
9256 BFD_ASSERT (input_sec->owner == NULL
9257 || (input_sec->owner->flags & DYNAMIC) != 0);
9258 sym.st_shndx = SHN_UNDEF;
9259 input_sec = bfd_und_section_ptr;
9264 case bfd_link_hash_common:
9265 input_sec = h->root.u.c.p->section;
9266 sym.st_shndx = bed->common_section_index (input_sec);
9267 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9270 case bfd_link_hash_indirect:
9271 /* These symbols are created by symbol versioning. They point
9272 to the decorated version of the name. For example, if the
9273 symbol foo@@GNU_1.2 is the default, which should be used when
9274 foo is used with no version, then we add an indirect symbol
9275 foo which points to foo@@GNU_1.2. We ignore these symbols,
9276 since the indirected symbol is already in the hash table. */
9280 /* Give the processor backend a chance to tweak the symbol value,
9281 and also to finish up anything that needs to be done for this
9282 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9283 forced local syms when non-shared is due to a historical quirk.
9284 STT_GNU_IFUNC symbol must go through PLT. */
9285 if ((h->type == STT_GNU_IFUNC
9287 && !bfd_link_relocatable (flinfo->info))
9288 || ((h->dynindx != -1
9290 && ((bfd_link_pic (flinfo->info)
9291 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9292 || h->root.type != bfd_link_hash_undefweak))
9293 || !h->forced_local)
9294 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9296 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9297 (flinfo->output_bfd, flinfo->info, h, &sym)))
9299 eoinfo->failed = TRUE;
9304 /* If we are marking the symbol as undefined, and there are no
9305 non-weak references to this symbol from a regular object, then
9306 mark the symbol as weak undefined; if there are non-weak
9307 references, mark the symbol as strong. We can't do this earlier,
9308 because it might not be marked as undefined until the
9309 finish_dynamic_symbol routine gets through with it. */
9310 if (sym.st_shndx == SHN_UNDEF
9312 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9313 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9316 unsigned int type = ELF_ST_TYPE (sym.st_info);
9318 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9319 if (type == STT_GNU_IFUNC)
9322 if (h->ref_regular_nonweak)
9323 bindtype = STB_GLOBAL;
9325 bindtype = STB_WEAK;
9326 sym.st_info = ELF_ST_INFO (bindtype, type);
9329 /* If this is a symbol defined in a dynamic library, don't use the
9330 symbol size from the dynamic library. Relinking an executable
9331 against a new library may introduce gratuitous changes in the
9332 executable's symbols if we keep the size. */
9333 if (sym.st_shndx == SHN_UNDEF
9338 /* If a non-weak symbol with non-default visibility is not defined
9339 locally, it is a fatal error. */
9340 if (!bfd_link_relocatable (flinfo->info)
9341 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9342 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9343 && h->root.type == bfd_link_hash_undefined
9348 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9349 msg = _("%B: protected symbol `%s' isn't defined");
9350 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9351 msg = _("%B: internal symbol `%s' isn't defined");
9353 msg = _("%B: hidden symbol `%s' isn't defined");
9354 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9355 bfd_set_error (bfd_error_bad_value);
9356 eoinfo->failed = TRUE;
9360 /* If this symbol should be put in the .dynsym section, then put it
9361 there now. We already know the symbol index. We also fill in
9362 the entry in the .hash section. */
9363 if (elf_hash_table (flinfo->info)->dynsym != NULL
9365 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9369 /* Since there is no version information in the dynamic string,
9370 if there is no version info in symbol version section, we will
9371 have a run-time problem if not linking executable, referenced
9372 by shared library, not locally defined, or not bound locally.
9374 if (h->verinfo.verdef == NULL
9376 && (!bfd_link_executable (flinfo->info)
9378 || !h->def_regular))
9380 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9382 if (p && p [1] != '\0')
9384 (*_bfd_error_handler)
9385 (_("%B: No symbol version section for versioned symbol `%s'"),
9386 flinfo->output_bfd, h->root.root.string);
9387 eoinfo->failed = TRUE;
9392 sym.st_name = h->dynstr_index;
9393 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9394 + h->dynindx * bed->s->sizeof_sym);
9395 if (!check_dynsym (flinfo->output_bfd, &sym))
9397 eoinfo->failed = TRUE;
9400 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9402 if (flinfo->hash_sec != NULL)
9404 size_t hash_entry_size;
9405 bfd_byte *bucketpos;
9410 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9411 bucket = h->u.elf_hash_value % bucketcount;
9414 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9415 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9416 + (bucket + 2) * hash_entry_size);
9417 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9418 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9420 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9421 ((bfd_byte *) flinfo->hash_sec->contents
9422 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9425 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9427 Elf_Internal_Versym iversym;
9428 Elf_External_Versym *eversym;
9430 if (!h->def_regular)
9432 if (h->verinfo.verdef == NULL
9433 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9434 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9435 iversym.vs_vers = 0;
9437 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9441 if (h->verinfo.vertree == NULL)
9442 iversym.vs_vers = 1;
9444 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9445 if (flinfo->info->create_default_symver)
9449 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9451 if (h->versioned == versioned_hidden && h->def_regular)
9452 iversym.vs_vers |= VERSYM_HIDDEN;
9454 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9455 eversym += h->dynindx;
9456 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9460 /* If the symbol is undefined, and we didn't output it to .dynsym,
9461 strip it from .symtab too. Obviously we can't do this for
9462 relocatable output or when needed for --emit-relocs. */
9463 else if (input_sec == bfd_und_section_ptr
9465 && !bfd_link_relocatable (flinfo->info))
9467 /* Also strip others that we couldn't earlier due to dynamic symbol
9471 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9474 /* Output a FILE symbol so that following locals are not associated
9475 with the wrong input file. We need one for forced local symbols
9476 if we've seen more than one FILE symbol or when we have exactly
9477 one FILE symbol but global symbols are present in a file other
9478 than the one with the FILE symbol. We also need one if linker
9479 defined symbols are present. In practice these conditions are
9480 always met, so just emit the FILE symbol unconditionally. */
9481 if (eoinfo->localsyms
9482 && !eoinfo->file_sym_done
9483 && eoinfo->flinfo->filesym_count != 0)
9485 Elf_Internal_Sym fsym;
9487 memset (&fsym, 0, sizeof (fsym));
9488 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9489 fsym.st_shndx = SHN_ABS;
9490 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9491 bfd_und_section_ptr, NULL))
9494 eoinfo->file_sym_done = TRUE;
9497 indx = bfd_get_symcount (flinfo->output_bfd);
9498 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9502 eoinfo->failed = TRUE;
9507 else if (h->indx == -2)
9513 /* Return TRUE if special handling is done for relocs in SEC against
9514 symbols defined in discarded sections. */
9517 elf_section_ignore_discarded_relocs (asection *sec)
9519 const struct elf_backend_data *bed;
9521 switch (sec->sec_info_type)
9523 case SEC_INFO_TYPE_STABS:
9524 case SEC_INFO_TYPE_EH_FRAME:
9525 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9531 bed = get_elf_backend_data (sec->owner);
9532 if (bed->elf_backend_ignore_discarded_relocs != NULL
9533 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9539 /* Return a mask saying how ld should treat relocations in SEC against
9540 symbols defined in discarded sections. If this function returns
9541 COMPLAIN set, ld will issue a warning message. If this function
9542 returns PRETEND set, and the discarded section was link-once and the
9543 same size as the kept link-once section, ld will pretend that the
9544 symbol was actually defined in the kept section. Otherwise ld will
9545 zero the reloc (at least that is the intent, but some cooperation by
9546 the target dependent code is needed, particularly for REL targets). */
9549 _bfd_elf_default_action_discarded (asection *sec)
9551 if (sec->flags & SEC_DEBUGGING)
9554 if (strcmp (".eh_frame", sec->name) == 0)
9557 if (strcmp (".gcc_except_table", sec->name) == 0)
9560 return COMPLAIN | PRETEND;
9563 /* Find a match between a section and a member of a section group. */
9566 match_group_member (asection *sec, asection *group,
9567 struct bfd_link_info *info)
9569 asection *first = elf_next_in_group (group);
9570 asection *s = first;
9574 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9577 s = elf_next_in_group (s);
9585 /* Check if the kept section of a discarded section SEC can be used
9586 to replace it. Return the replacement if it is OK. Otherwise return
9590 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9594 kept = sec->kept_section;
9597 if ((kept->flags & SEC_GROUP) != 0)
9598 kept = match_group_member (sec, kept, info);
9600 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9601 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9603 sec->kept_section = kept;
9608 /* Link an input file into the linker output file. This function
9609 handles all the sections and relocations of the input file at once.
9610 This is so that we only have to read the local symbols once, and
9611 don't have to keep them in memory. */
9614 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9616 int (*relocate_section)
9617 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9618 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9620 Elf_Internal_Shdr *symtab_hdr;
9623 Elf_Internal_Sym *isymbuf;
9624 Elf_Internal_Sym *isym;
9625 Elf_Internal_Sym *isymend;
9627 asection **ppsection;
9629 const struct elf_backend_data *bed;
9630 struct elf_link_hash_entry **sym_hashes;
9631 bfd_size_type address_size;
9632 bfd_vma r_type_mask;
9634 bfd_boolean have_file_sym = FALSE;
9636 output_bfd = flinfo->output_bfd;
9637 bed = get_elf_backend_data (output_bfd);
9638 relocate_section = bed->elf_backend_relocate_section;
9640 /* If this is a dynamic object, we don't want to do anything here:
9641 we don't want the local symbols, and we don't want the section
9643 if ((input_bfd->flags & DYNAMIC) != 0)
9646 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9647 if (elf_bad_symtab (input_bfd))
9649 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9654 locsymcount = symtab_hdr->sh_info;
9655 extsymoff = symtab_hdr->sh_info;
9658 /* Read the local symbols. */
9659 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9660 if (isymbuf == NULL && locsymcount != 0)
9662 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9663 flinfo->internal_syms,
9664 flinfo->external_syms,
9665 flinfo->locsym_shndx);
9666 if (isymbuf == NULL)
9670 /* Find local symbol sections and adjust values of symbols in
9671 SEC_MERGE sections. Write out those local symbols we know are
9672 going into the output file. */
9673 isymend = isymbuf + locsymcount;
9674 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9676 isym++, pindex++, ppsection++)
9680 Elf_Internal_Sym osym;
9686 if (elf_bad_symtab (input_bfd))
9688 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9695 if (isym->st_shndx == SHN_UNDEF)
9696 isec = bfd_und_section_ptr;
9697 else if (isym->st_shndx == SHN_ABS)
9698 isec = bfd_abs_section_ptr;
9699 else if (isym->st_shndx == SHN_COMMON)
9700 isec = bfd_com_section_ptr;
9703 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9706 /* Don't attempt to output symbols with st_shnx in the
9707 reserved range other than SHN_ABS and SHN_COMMON. */
9711 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9712 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9714 _bfd_merged_section_offset (output_bfd, &isec,
9715 elf_section_data (isec)->sec_info,
9721 /* Don't output the first, undefined, symbol. In fact, don't
9722 output any undefined local symbol. */
9723 if (isec == bfd_und_section_ptr)
9726 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9728 /* We never output section symbols. Instead, we use the
9729 section symbol of the corresponding section in the output
9734 /* If we are stripping all symbols, we don't want to output this
9736 if (flinfo->info->strip == strip_all)
9739 /* If we are discarding all local symbols, we don't want to
9740 output this one. If we are generating a relocatable output
9741 file, then some of the local symbols may be required by
9742 relocs; we output them below as we discover that they are
9744 if (flinfo->info->discard == discard_all)
9747 /* If this symbol is defined in a section which we are
9748 discarding, we don't need to keep it. */
9749 if (isym->st_shndx != SHN_UNDEF
9750 && isym->st_shndx < SHN_LORESERVE
9751 && bfd_section_removed_from_list (output_bfd,
9752 isec->output_section))
9755 /* Get the name of the symbol. */
9756 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9761 /* See if we are discarding symbols with this name. */
9762 if ((flinfo->info->strip == strip_some
9763 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9765 || (((flinfo->info->discard == discard_sec_merge
9766 && (isec->flags & SEC_MERGE)
9767 && !bfd_link_relocatable (flinfo->info))
9768 || flinfo->info->discard == discard_l)
9769 && bfd_is_local_label_name (input_bfd, name)))
9772 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9774 if (input_bfd->lto_output)
9775 /* -flto puts a temp file name here. This means builds
9776 are not reproducible. Discard the symbol. */
9778 have_file_sym = TRUE;
9779 flinfo->filesym_count += 1;
9783 /* In the absence of debug info, bfd_find_nearest_line uses
9784 FILE symbols to determine the source file for local
9785 function symbols. Provide a FILE symbol here if input
9786 files lack such, so that their symbols won't be
9787 associated with a previous input file. It's not the
9788 source file, but the best we can do. */
9789 have_file_sym = TRUE;
9790 flinfo->filesym_count += 1;
9791 memset (&osym, 0, sizeof (osym));
9792 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9793 osym.st_shndx = SHN_ABS;
9794 if (!elf_link_output_symstrtab (flinfo,
9795 (input_bfd->lto_output ? NULL
9796 : input_bfd->filename),
9797 &osym, bfd_abs_section_ptr,
9804 /* Adjust the section index for the output file. */
9805 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9806 isec->output_section);
9807 if (osym.st_shndx == SHN_BAD)
9810 /* ELF symbols in relocatable files are section relative, but
9811 in executable files they are virtual addresses. Note that
9812 this code assumes that all ELF sections have an associated
9813 BFD section with a reasonable value for output_offset; below
9814 we assume that they also have a reasonable value for
9815 output_section. Any special sections must be set up to meet
9816 these requirements. */
9817 osym.st_value += isec->output_offset;
9818 if (!bfd_link_relocatable (flinfo->info))
9820 osym.st_value += isec->output_section->vma;
9821 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9823 /* STT_TLS symbols are relative to PT_TLS segment base. */
9824 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9825 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
9829 indx = bfd_get_symcount (output_bfd);
9830 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
9837 if (bed->s->arch_size == 32)
9845 r_type_mask = 0xffffffff;
9850 /* Relocate the contents of each section. */
9851 sym_hashes = elf_sym_hashes (input_bfd);
9852 for (o = input_bfd->sections; o != NULL; o = o->next)
9856 if (! o->linker_mark)
9858 /* This section was omitted from the link. */
9862 if (bfd_link_relocatable (flinfo->info)
9863 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9865 /* Deal with the group signature symbol. */
9866 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9867 unsigned long symndx = sec_data->this_hdr.sh_info;
9868 asection *osec = o->output_section;
9870 if (symndx >= locsymcount
9871 || (elf_bad_symtab (input_bfd)
9872 && flinfo->sections[symndx] == NULL))
9874 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9875 while (h->root.type == bfd_link_hash_indirect
9876 || h->root.type == bfd_link_hash_warning)
9877 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9878 /* Arrange for symbol to be output. */
9880 elf_section_data (osec)->this_hdr.sh_info = -2;
9882 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9884 /* We'll use the output section target_index. */
9885 asection *sec = flinfo->sections[symndx]->output_section;
9886 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9890 if (flinfo->indices[symndx] == -1)
9892 /* Otherwise output the local symbol now. */
9893 Elf_Internal_Sym sym = isymbuf[symndx];
9894 asection *sec = flinfo->sections[symndx]->output_section;
9899 name = bfd_elf_string_from_elf_section (input_bfd,
9900 symtab_hdr->sh_link,
9905 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9907 if (sym.st_shndx == SHN_BAD)
9910 sym.st_value += o->output_offset;
9912 indx = bfd_get_symcount (output_bfd);
9913 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
9918 flinfo->indices[symndx] = indx;
9922 elf_section_data (osec)->this_hdr.sh_info
9923 = flinfo->indices[symndx];
9927 if ((o->flags & SEC_HAS_CONTENTS) == 0
9928 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9931 if ((o->flags & SEC_LINKER_CREATED) != 0)
9933 /* Section was created by _bfd_elf_link_create_dynamic_sections
9938 /* Get the contents of the section. They have been cached by a
9939 relaxation routine. Note that o is a section in an input
9940 file, so the contents field will not have been set by any of
9941 the routines which work on output files. */
9942 if (elf_section_data (o)->this_hdr.contents != NULL)
9944 contents = elf_section_data (o)->this_hdr.contents;
9945 if (bed->caches_rawsize
9947 && o->rawsize < o->size)
9949 memcpy (flinfo->contents, contents, o->rawsize);
9950 contents = flinfo->contents;
9955 contents = flinfo->contents;
9956 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
9960 if ((o->flags & SEC_RELOC) != 0)
9962 Elf_Internal_Rela *internal_relocs;
9963 Elf_Internal_Rela *rel, *relend;
9964 int action_discarded;
9967 /* Get the swapped relocs. */
9969 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
9970 flinfo->internal_relocs, FALSE);
9971 if (internal_relocs == NULL
9972 && o->reloc_count > 0)
9975 /* We need to reverse-copy input .ctors/.dtors sections if
9976 they are placed in .init_array/.finit_array for output. */
9977 if (o->size > address_size
9978 && ((strncmp (o->name, ".ctors", 6) == 0
9979 && strcmp (o->output_section->name,
9980 ".init_array") == 0)
9981 || (strncmp (o->name, ".dtors", 6) == 0
9982 && strcmp (o->output_section->name,
9983 ".fini_array") == 0))
9984 && (o->name[6] == 0 || o->name[6] == '.'))
9986 if (o->size != o->reloc_count * address_size)
9988 (*_bfd_error_handler)
9989 (_("error: %B: size of section %A is not "
9990 "multiple of address size"),
9992 bfd_set_error (bfd_error_on_input);
9995 o->flags |= SEC_ELF_REVERSE_COPY;
9998 action_discarded = -1;
9999 if (!elf_section_ignore_discarded_relocs (o))
10000 action_discarded = (*bed->action_discarded) (o);
10002 /* Run through the relocs evaluating complex reloc symbols and
10003 looking for relocs against symbols from discarded sections
10004 or section symbols from removed link-once sections.
10005 Complain about relocs against discarded sections. Zero
10006 relocs against removed link-once sections. */
10008 rel = internal_relocs;
10009 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10010 for ( ; rel < relend; rel++)
10012 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10013 unsigned int s_type;
10014 asection **ps, *sec;
10015 struct elf_link_hash_entry *h = NULL;
10016 const char *sym_name;
10018 if (r_symndx == STN_UNDEF)
10021 if (r_symndx >= locsymcount
10022 || (elf_bad_symtab (input_bfd)
10023 && flinfo->sections[r_symndx] == NULL))
10025 h = sym_hashes[r_symndx - extsymoff];
10027 /* Badly formatted input files can contain relocs that
10028 reference non-existant symbols. Check here so that
10029 we do not seg fault. */
10034 sprintf_vma (buffer, rel->r_info);
10035 (*_bfd_error_handler)
10036 (_("error: %B contains a reloc (0x%s) for section %A "
10037 "that references a non-existent global symbol"),
10038 input_bfd, o, buffer);
10039 bfd_set_error (bfd_error_bad_value);
10043 while (h->root.type == bfd_link_hash_indirect
10044 || h->root.type == bfd_link_hash_warning)
10045 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10049 /* If a plugin symbol is referenced from a non-IR file,
10050 mark the symbol as undefined. Note that the
10051 linker may attach linker created dynamic sections
10052 to the plugin bfd. Symbols defined in linker
10053 created sections are not plugin symbols. */
10054 if (h->root.non_ir_ref
10055 && (h->root.type == bfd_link_hash_defined
10056 || h->root.type == bfd_link_hash_defweak)
10057 && (h->root.u.def.section->flags
10058 & SEC_LINKER_CREATED) == 0
10059 && h->root.u.def.section->owner != NULL
10060 && (h->root.u.def.section->owner->flags
10061 & BFD_PLUGIN) != 0)
10063 h->root.type = bfd_link_hash_undefined;
10064 h->root.u.undef.abfd = h->root.u.def.section->owner;
10068 if (h->root.type == bfd_link_hash_defined
10069 || h->root.type == bfd_link_hash_defweak)
10070 ps = &h->root.u.def.section;
10072 sym_name = h->root.root.string;
10076 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10078 s_type = ELF_ST_TYPE (sym->st_info);
10079 ps = &flinfo->sections[r_symndx];
10080 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10084 if ((s_type == STT_RELC || s_type == STT_SRELC)
10085 && !bfd_link_relocatable (flinfo->info))
10088 bfd_vma dot = (rel->r_offset
10089 + o->output_offset + o->output_section->vma);
10091 printf ("Encountered a complex symbol!");
10092 printf (" (input_bfd %s, section %s, reloc %ld\n",
10093 input_bfd->filename, o->name,
10094 (long) (rel - internal_relocs));
10095 printf (" symbol: idx %8.8lx, name %s\n",
10096 r_symndx, sym_name);
10097 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10098 (unsigned long) rel->r_info,
10099 (unsigned long) rel->r_offset);
10101 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10102 isymbuf, locsymcount, s_type == STT_SRELC))
10105 /* Symbol evaluated OK. Update to absolute value. */
10106 set_symbol_value (input_bfd, isymbuf, locsymcount,
10111 if (action_discarded != -1 && ps != NULL)
10113 /* Complain if the definition comes from a
10114 discarded section. */
10115 if ((sec = *ps) != NULL && discarded_section (sec))
10117 BFD_ASSERT (r_symndx != STN_UNDEF);
10118 if (action_discarded & COMPLAIN)
10119 (*flinfo->info->callbacks->einfo)
10120 (_("%X`%s' referenced in section `%A' of %B: "
10121 "defined in discarded section `%A' of %B\n"),
10122 sym_name, o, input_bfd, sec, sec->owner);
10124 /* Try to do the best we can to support buggy old
10125 versions of gcc. Pretend that the symbol is
10126 really defined in the kept linkonce section.
10127 FIXME: This is quite broken. Modifying the
10128 symbol here means we will be changing all later
10129 uses of the symbol, not just in this section. */
10130 if (action_discarded & PRETEND)
10134 kept = _bfd_elf_check_kept_section (sec,
10146 /* Relocate the section by invoking a back end routine.
10148 The back end routine is responsible for adjusting the
10149 section contents as necessary, and (if using Rela relocs
10150 and generating a relocatable output file) adjusting the
10151 reloc addend as necessary.
10153 The back end routine does not have to worry about setting
10154 the reloc address or the reloc symbol index.
10156 The back end routine is given a pointer to the swapped in
10157 internal symbols, and can access the hash table entries
10158 for the external symbols via elf_sym_hashes (input_bfd).
10160 When generating relocatable output, the back end routine
10161 must handle STB_LOCAL/STT_SECTION symbols specially. The
10162 output symbol is going to be a section symbol
10163 corresponding to the output section, which will require
10164 the addend to be adjusted. */
10166 ret = (*relocate_section) (output_bfd, flinfo->info,
10167 input_bfd, o, contents,
10175 || bfd_link_relocatable (flinfo->info)
10176 || flinfo->info->emitrelocations)
10178 Elf_Internal_Rela *irela;
10179 Elf_Internal_Rela *irelaend, *irelamid;
10180 bfd_vma last_offset;
10181 struct elf_link_hash_entry **rel_hash;
10182 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10183 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10184 unsigned int next_erel;
10185 bfd_boolean rela_normal;
10186 struct bfd_elf_section_data *esdi, *esdo;
10188 esdi = elf_section_data (o);
10189 esdo = elf_section_data (o->output_section);
10190 rela_normal = FALSE;
10192 /* Adjust the reloc addresses and symbol indices. */
10194 irela = internal_relocs;
10195 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10196 rel_hash = esdo->rel.hashes + esdo->rel.count;
10197 /* We start processing the REL relocs, if any. When we reach
10198 IRELAMID in the loop, we switch to the RELA relocs. */
10200 if (esdi->rel.hdr != NULL)
10201 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10202 * bed->s->int_rels_per_ext_rel);
10203 rel_hash_list = rel_hash;
10204 rela_hash_list = NULL;
10205 last_offset = o->output_offset;
10206 if (!bfd_link_relocatable (flinfo->info))
10207 last_offset += o->output_section->vma;
10208 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10210 unsigned long r_symndx;
10212 Elf_Internal_Sym sym;
10214 if (next_erel == bed->s->int_rels_per_ext_rel)
10220 if (irela == irelamid)
10222 rel_hash = esdo->rela.hashes + esdo->rela.count;
10223 rela_hash_list = rel_hash;
10224 rela_normal = bed->rela_normal;
10227 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10230 if (irela->r_offset >= (bfd_vma) -2)
10232 /* This is a reloc for a deleted entry or somesuch.
10233 Turn it into an R_*_NONE reloc, at the same
10234 offset as the last reloc. elf_eh_frame.c and
10235 bfd_elf_discard_info rely on reloc offsets
10237 irela->r_offset = last_offset;
10239 irela->r_addend = 0;
10243 irela->r_offset += o->output_offset;
10245 /* Relocs in an executable have to be virtual addresses. */
10246 if (!bfd_link_relocatable (flinfo->info))
10247 irela->r_offset += o->output_section->vma;
10249 last_offset = irela->r_offset;
10251 r_symndx = irela->r_info >> r_sym_shift;
10252 if (r_symndx == STN_UNDEF)
10255 if (r_symndx >= locsymcount
10256 || (elf_bad_symtab (input_bfd)
10257 && flinfo->sections[r_symndx] == NULL))
10259 struct elf_link_hash_entry *rh;
10260 unsigned long indx;
10262 /* This is a reloc against a global symbol. We
10263 have not yet output all the local symbols, so
10264 we do not know the symbol index of any global
10265 symbol. We set the rel_hash entry for this
10266 reloc to point to the global hash table entry
10267 for this symbol. The symbol index is then
10268 set at the end of bfd_elf_final_link. */
10269 indx = r_symndx - extsymoff;
10270 rh = elf_sym_hashes (input_bfd)[indx];
10271 while (rh->root.type == bfd_link_hash_indirect
10272 || rh->root.type == bfd_link_hash_warning)
10273 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10275 /* Setting the index to -2 tells
10276 elf_link_output_extsym that this symbol is
10277 used by a reloc. */
10278 BFD_ASSERT (rh->indx < 0);
10286 /* This is a reloc against a local symbol. */
10289 sym = isymbuf[r_symndx];
10290 sec = flinfo->sections[r_symndx];
10291 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10293 /* I suppose the backend ought to fill in the
10294 section of any STT_SECTION symbol against a
10295 processor specific section. */
10296 r_symndx = STN_UNDEF;
10297 if (bfd_is_abs_section (sec))
10299 else if (sec == NULL || sec->owner == NULL)
10301 bfd_set_error (bfd_error_bad_value);
10306 asection *osec = sec->output_section;
10308 /* If we have discarded a section, the output
10309 section will be the absolute section. In
10310 case of discarded SEC_MERGE sections, use
10311 the kept section. relocate_section should
10312 have already handled discarded linkonce
10314 if (bfd_is_abs_section (osec)
10315 && sec->kept_section != NULL
10316 && sec->kept_section->output_section != NULL)
10318 osec = sec->kept_section->output_section;
10319 irela->r_addend -= osec->vma;
10322 if (!bfd_is_abs_section (osec))
10324 r_symndx = osec->target_index;
10325 if (r_symndx == STN_UNDEF)
10327 irela->r_addend += osec->vma;
10328 osec = _bfd_nearby_section (output_bfd, osec,
10330 irela->r_addend -= osec->vma;
10331 r_symndx = osec->target_index;
10336 /* Adjust the addend according to where the
10337 section winds up in the output section. */
10339 irela->r_addend += sec->output_offset;
10343 if (flinfo->indices[r_symndx] == -1)
10345 unsigned long shlink;
10350 if (flinfo->info->strip == strip_all)
10352 /* You can't do ld -r -s. */
10353 bfd_set_error (bfd_error_invalid_operation);
10357 /* This symbol was skipped earlier, but
10358 since it is needed by a reloc, we
10359 must output it now. */
10360 shlink = symtab_hdr->sh_link;
10361 name = (bfd_elf_string_from_elf_section
10362 (input_bfd, shlink, sym.st_name));
10366 osec = sec->output_section;
10368 _bfd_elf_section_from_bfd_section (output_bfd,
10370 if (sym.st_shndx == SHN_BAD)
10373 sym.st_value += sec->output_offset;
10374 if (!bfd_link_relocatable (flinfo->info))
10376 sym.st_value += osec->vma;
10377 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10379 /* STT_TLS symbols are relative to PT_TLS
10381 BFD_ASSERT (elf_hash_table (flinfo->info)
10382 ->tls_sec != NULL);
10383 sym.st_value -= (elf_hash_table (flinfo->info)
10388 indx = bfd_get_symcount (output_bfd);
10389 ret = elf_link_output_symstrtab (flinfo, name,
10395 flinfo->indices[r_symndx] = indx;
10400 r_symndx = flinfo->indices[r_symndx];
10403 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10404 | (irela->r_info & r_type_mask));
10407 /* Swap out the relocs. */
10408 input_rel_hdr = esdi->rel.hdr;
10409 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10411 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10416 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10417 * bed->s->int_rels_per_ext_rel);
10418 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10421 input_rela_hdr = esdi->rela.hdr;
10422 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10424 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10433 /* Write out the modified section contents. */
10434 if (bed->elf_backend_write_section
10435 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10438 /* Section written out. */
10440 else switch (o->sec_info_type)
10442 case SEC_INFO_TYPE_STABS:
10443 if (! (_bfd_write_section_stabs
10445 &elf_hash_table (flinfo->info)->stab_info,
10446 o, &elf_section_data (o)->sec_info, contents)))
10449 case SEC_INFO_TYPE_MERGE:
10450 if (! _bfd_write_merged_section (output_bfd, o,
10451 elf_section_data (o)->sec_info))
10454 case SEC_INFO_TYPE_EH_FRAME:
10456 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10461 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10463 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10471 /* FIXME: octets_per_byte. */
10472 if (! (o->flags & SEC_EXCLUDE))
10474 file_ptr offset = (file_ptr) o->output_offset;
10475 bfd_size_type todo = o->size;
10476 if ((o->flags & SEC_ELF_REVERSE_COPY))
10478 /* Reverse-copy input section to output. */
10481 todo -= address_size;
10482 if (! bfd_set_section_contents (output_bfd,
10490 offset += address_size;
10494 else if (! bfd_set_section_contents (output_bfd,
10508 /* Generate a reloc when linking an ELF file. This is a reloc
10509 requested by the linker, and does not come from any input file. This
10510 is used to build constructor and destructor tables when linking
10514 elf_reloc_link_order (bfd *output_bfd,
10515 struct bfd_link_info *info,
10516 asection *output_section,
10517 struct bfd_link_order *link_order)
10519 reloc_howto_type *howto;
10523 struct bfd_elf_section_reloc_data *reldata;
10524 struct elf_link_hash_entry **rel_hash_ptr;
10525 Elf_Internal_Shdr *rel_hdr;
10526 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10527 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10530 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10532 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10535 bfd_set_error (bfd_error_bad_value);
10539 addend = link_order->u.reloc.p->addend;
10542 reldata = &esdo->rel;
10543 else if (esdo->rela.hdr)
10544 reldata = &esdo->rela;
10551 /* Figure out the symbol index. */
10552 rel_hash_ptr = reldata->hashes + reldata->count;
10553 if (link_order->type == bfd_section_reloc_link_order)
10555 indx = link_order->u.reloc.p->u.section->target_index;
10556 BFD_ASSERT (indx != 0);
10557 *rel_hash_ptr = NULL;
10561 struct elf_link_hash_entry *h;
10563 /* Treat a reloc against a defined symbol as though it were
10564 actually against the section. */
10565 h = ((struct elf_link_hash_entry *)
10566 bfd_wrapped_link_hash_lookup (output_bfd, info,
10567 link_order->u.reloc.p->u.name,
10568 FALSE, FALSE, TRUE));
10570 && (h->root.type == bfd_link_hash_defined
10571 || h->root.type == bfd_link_hash_defweak))
10575 section = h->root.u.def.section;
10576 indx = section->output_section->target_index;
10577 *rel_hash_ptr = NULL;
10578 /* It seems that we ought to add the symbol value to the
10579 addend here, but in practice it has already been added
10580 because it was passed to constructor_callback. */
10581 addend += section->output_section->vma + section->output_offset;
10583 else if (h != NULL)
10585 /* Setting the index to -2 tells elf_link_output_extsym that
10586 this symbol is used by a reloc. */
10593 if (! ((*info->callbacks->unattached_reloc)
10594 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10600 /* If this is an inplace reloc, we must write the addend into the
10602 if (howto->partial_inplace && addend != 0)
10604 bfd_size_type size;
10605 bfd_reloc_status_type rstat;
10608 const char *sym_name;
10610 size = (bfd_size_type) bfd_get_reloc_size (howto);
10611 buf = (bfd_byte *) bfd_zmalloc (size);
10612 if (buf == NULL && size != 0)
10614 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10621 case bfd_reloc_outofrange:
10624 case bfd_reloc_overflow:
10625 if (link_order->type == bfd_section_reloc_link_order)
10626 sym_name = bfd_section_name (output_bfd,
10627 link_order->u.reloc.p->u.section);
10629 sym_name = link_order->u.reloc.p->u.name;
10630 if (! ((*info->callbacks->reloc_overflow)
10631 (info, NULL, sym_name, howto->name, addend, NULL,
10632 NULL, (bfd_vma) 0)))
10639 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10640 link_order->offset, size);
10646 /* The address of a reloc is relative to the section in a
10647 relocatable file, and is a virtual address in an executable
10649 offset = link_order->offset;
10650 if (! bfd_link_relocatable (info))
10651 offset += output_section->vma;
10653 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10655 irel[i].r_offset = offset;
10656 irel[i].r_info = 0;
10657 irel[i].r_addend = 0;
10659 if (bed->s->arch_size == 32)
10660 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10662 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10664 rel_hdr = reldata->hdr;
10665 erel = rel_hdr->contents;
10666 if (rel_hdr->sh_type == SHT_REL)
10668 erel += reldata->count * bed->s->sizeof_rel;
10669 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10673 irel[0].r_addend = addend;
10674 erel += reldata->count * bed->s->sizeof_rela;
10675 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10684 /* Get the output vma of the section pointed to by the sh_link field. */
10687 elf_get_linked_section_vma (struct bfd_link_order *p)
10689 Elf_Internal_Shdr **elf_shdrp;
10693 s = p->u.indirect.section;
10694 elf_shdrp = elf_elfsections (s->owner);
10695 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10696 elfsec = elf_shdrp[elfsec]->sh_link;
10698 The Intel C compiler generates SHT_IA_64_UNWIND with
10699 SHF_LINK_ORDER. But it doesn't set the sh_link or
10700 sh_info fields. Hence we could get the situation
10701 where elfsec is 0. */
10704 const struct elf_backend_data *bed
10705 = get_elf_backend_data (s->owner);
10706 if (bed->link_order_error_handler)
10707 bed->link_order_error_handler
10708 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10713 s = elf_shdrp[elfsec]->bfd_section;
10714 return s->output_section->vma + s->output_offset;
10719 /* Compare two sections based on the locations of the sections they are
10720 linked to. Used by elf_fixup_link_order. */
10723 compare_link_order (const void * a, const void * b)
10728 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10729 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10732 return apos > bpos;
10736 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10737 order as their linked sections. Returns false if this could not be done
10738 because an output section includes both ordered and unordered
10739 sections. Ideally we'd do this in the linker proper. */
10742 elf_fixup_link_order (bfd *abfd, asection *o)
10744 int seen_linkorder;
10747 struct bfd_link_order *p;
10749 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10751 struct bfd_link_order **sections;
10752 asection *s, *other_sec, *linkorder_sec;
10756 linkorder_sec = NULL;
10758 seen_linkorder = 0;
10759 for (p = o->map_head.link_order; p != NULL; p = p->next)
10761 if (p->type == bfd_indirect_link_order)
10763 s = p->u.indirect.section;
10765 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10766 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10767 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10768 && elfsec < elf_numsections (sub)
10769 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10770 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10784 if (seen_other && seen_linkorder)
10786 if (other_sec && linkorder_sec)
10787 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10789 linkorder_sec->owner, other_sec,
10792 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10794 bfd_set_error (bfd_error_bad_value);
10799 if (!seen_linkorder)
10802 sections = (struct bfd_link_order **)
10803 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10804 if (sections == NULL)
10806 seen_linkorder = 0;
10808 for (p = o->map_head.link_order; p != NULL; p = p->next)
10810 sections[seen_linkorder++] = p;
10812 /* Sort the input sections in the order of their linked section. */
10813 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10814 compare_link_order);
10816 /* Change the offsets of the sections. */
10818 for (n = 0; n < seen_linkorder; n++)
10820 s = sections[n]->u.indirect.section;
10821 offset &= ~(bfd_vma) 0 << s->alignment_power;
10822 s->output_offset = offset;
10823 sections[n]->offset = offset;
10824 /* FIXME: octets_per_byte. */
10825 offset += sections[n]->size;
10833 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10837 if (flinfo->symstrtab != NULL)
10838 _bfd_elf_strtab_free (flinfo->symstrtab);
10839 if (flinfo->contents != NULL)
10840 free (flinfo->contents);
10841 if (flinfo->external_relocs != NULL)
10842 free (flinfo->external_relocs);
10843 if (flinfo->internal_relocs != NULL)
10844 free (flinfo->internal_relocs);
10845 if (flinfo->external_syms != NULL)
10846 free (flinfo->external_syms);
10847 if (flinfo->locsym_shndx != NULL)
10848 free (flinfo->locsym_shndx);
10849 if (flinfo->internal_syms != NULL)
10850 free (flinfo->internal_syms);
10851 if (flinfo->indices != NULL)
10852 free (flinfo->indices);
10853 if (flinfo->sections != NULL)
10854 free (flinfo->sections);
10855 if (flinfo->symshndxbuf != NULL)
10856 free (flinfo->symshndxbuf);
10857 for (o = obfd->sections; o != NULL; o = o->next)
10859 struct bfd_elf_section_data *esdo = elf_section_data (o);
10860 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10861 free (esdo->rel.hashes);
10862 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10863 free (esdo->rela.hashes);
10867 /* Do the final step of an ELF link. */
10870 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10872 bfd_boolean dynamic;
10873 bfd_boolean emit_relocs;
10875 struct elf_final_link_info flinfo;
10877 struct bfd_link_order *p;
10879 bfd_size_type max_contents_size;
10880 bfd_size_type max_external_reloc_size;
10881 bfd_size_type max_internal_reloc_count;
10882 bfd_size_type max_sym_count;
10883 bfd_size_type max_sym_shndx_count;
10884 Elf_Internal_Sym elfsym;
10886 Elf_Internal_Shdr *symtab_hdr;
10887 Elf_Internal_Shdr *symtab_shndx_hdr;
10888 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10889 struct elf_outext_info eoinfo;
10890 bfd_boolean merged;
10891 size_t relativecount = 0;
10892 asection *reldyn = 0;
10894 asection *attr_section = NULL;
10895 bfd_vma attr_size = 0;
10896 const char *std_attrs_section;
10898 if (! is_elf_hash_table (info->hash))
10901 if (bfd_link_pic (info))
10902 abfd->flags |= DYNAMIC;
10904 dynamic = elf_hash_table (info)->dynamic_sections_created;
10905 dynobj = elf_hash_table (info)->dynobj;
10907 emit_relocs = (bfd_link_relocatable (info)
10908 || info->emitrelocations);
10910 flinfo.info = info;
10911 flinfo.output_bfd = abfd;
10912 flinfo.symstrtab = _bfd_elf_strtab_init ();
10913 if (flinfo.symstrtab == NULL)
10918 flinfo.hash_sec = NULL;
10919 flinfo.symver_sec = NULL;
10923 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
10924 /* Note that dynsym_sec can be NULL (on VMS). */
10925 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
10926 /* Note that it is OK if symver_sec is NULL. */
10929 flinfo.contents = NULL;
10930 flinfo.external_relocs = NULL;
10931 flinfo.internal_relocs = NULL;
10932 flinfo.external_syms = NULL;
10933 flinfo.locsym_shndx = NULL;
10934 flinfo.internal_syms = NULL;
10935 flinfo.indices = NULL;
10936 flinfo.sections = NULL;
10937 flinfo.symshndxbuf = NULL;
10938 flinfo.filesym_count = 0;
10940 /* The object attributes have been merged. Remove the input
10941 sections from the link, and set the contents of the output
10943 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10944 for (o = abfd->sections; o != NULL; o = o->next)
10946 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10947 || strcmp (o->name, ".gnu.attributes") == 0)
10949 for (p = o->map_head.link_order; p != NULL; p = p->next)
10951 asection *input_section;
10953 if (p->type != bfd_indirect_link_order)
10955 input_section = p->u.indirect.section;
10956 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10957 elf_link_input_bfd ignores this section. */
10958 input_section->flags &= ~SEC_HAS_CONTENTS;
10961 attr_size = bfd_elf_obj_attr_size (abfd);
10964 bfd_set_section_size (abfd, o, attr_size);
10966 /* Skip this section later on. */
10967 o->map_head.link_order = NULL;
10970 o->flags |= SEC_EXCLUDE;
10974 /* Count up the number of relocations we will output for each output
10975 section, so that we know the sizes of the reloc sections. We
10976 also figure out some maximum sizes. */
10977 max_contents_size = 0;
10978 max_external_reloc_size = 0;
10979 max_internal_reloc_count = 0;
10981 max_sym_shndx_count = 0;
10983 for (o = abfd->sections; o != NULL; o = o->next)
10985 struct bfd_elf_section_data *esdo = elf_section_data (o);
10986 o->reloc_count = 0;
10988 for (p = o->map_head.link_order; p != NULL; p = p->next)
10990 unsigned int reloc_count = 0;
10991 unsigned int additional_reloc_count = 0;
10992 struct bfd_elf_section_data *esdi = NULL;
10994 if (p->type == bfd_section_reloc_link_order
10995 || p->type == bfd_symbol_reloc_link_order)
10997 else if (p->type == bfd_indirect_link_order)
11001 sec = p->u.indirect.section;
11002 esdi = elf_section_data (sec);
11004 /* Mark all sections which are to be included in the
11005 link. This will normally be every section. We need
11006 to do this so that we can identify any sections which
11007 the linker has decided to not include. */
11008 sec->linker_mark = TRUE;
11010 if (sec->flags & SEC_MERGE)
11013 if (esdo->this_hdr.sh_type == SHT_REL
11014 || esdo->this_hdr.sh_type == SHT_RELA)
11015 /* Some backends use reloc_count in relocation sections
11016 to count particular types of relocs. Of course,
11017 reloc sections themselves can't have relocations. */
11019 else if (emit_relocs)
11021 reloc_count = sec->reloc_count;
11022 if (bed->elf_backend_count_additional_relocs)
11025 c = (*bed->elf_backend_count_additional_relocs) (sec);
11026 additional_reloc_count += c;
11029 else if (bed->elf_backend_count_relocs)
11030 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11032 if (sec->rawsize > max_contents_size)
11033 max_contents_size = sec->rawsize;
11034 if (sec->size > max_contents_size)
11035 max_contents_size = sec->size;
11037 /* We are interested in just local symbols, not all
11039 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11040 && (sec->owner->flags & DYNAMIC) == 0)
11044 if (elf_bad_symtab (sec->owner))
11045 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11046 / bed->s->sizeof_sym);
11048 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11050 if (sym_count > max_sym_count)
11051 max_sym_count = sym_count;
11053 if (sym_count > max_sym_shndx_count
11054 && elf_symtab_shndx_list (sec->owner) != NULL)
11055 max_sym_shndx_count = sym_count;
11057 if ((sec->flags & SEC_RELOC) != 0)
11059 size_t ext_size = 0;
11061 if (esdi->rel.hdr != NULL)
11062 ext_size = esdi->rel.hdr->sh_size;
11063 if (esdi->rela.hdr != NULL)
11064 ext_size += esdi->rela.hdr->sh_size;
11066 if (ext_size > max_external_reloc_size)
11067 max_external_reloc_size = ext_size;
11068 if (sec->reloc_count > max_internal_reloc_count)
11069 max_internal_reloc_count = sec->reloc_count;
11074 if (reloc_count == 0)
11077 reloc_count += additional_reloc_count;
11078 o->reloc_count += reloc_count;
11080 if (p->type == bfd_indirect_link_order && emit_relocs)
11084 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11085 esdo->rel.count += additional_reloc_count;
11087 if (esdi->rela.hdr)
11089 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11090 esdo->rela.count += additional_reloc_count;
11096 esdo->rela.count += reloc_count;
11098 esdo->rel.count += reloc_count;
11102 if (o->reloc_count > 0)
11103 o->flags |= SEC_RELOC;
11106 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11107 set it (this is probably a bug) and if it is set
11108 assign_section_numbers will create a reloc section. */
11109 o->flags &=~ SEC_RELOC;
11112 /* If the SEC_ALLOC flag is not set, force the section VMA to
11113 zero. This is done in elf_fake_sections as well, but forcing
11114 the VMA to 0 here will ensure that relocs against these
11115 sections are handled correctly. */
11116 if ((o->flags & SEC_ALLOC) == 0
11117 && ! o->user_set_vma)
11121 if (! bfd_link_relocatable (info) && merged)
11122 elf_link_hash_traverse (elf_hash_table (info),
11123 _bfd_elf_link_sec_merge_syms, abfd);
11125 /* Figure out the file positions for everything but the symbol table
11126 and the relocs. We set symcount to force assign_section_numbers
11127 to create a symbol table. */
11128 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11129 BFD_ASSERT (! abfd->output_has_begun);
11130 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11133 /* Set sizes, and assign file positions for reloc sections. */
11134 for (o = abfd->sections; o != NULL; o = o->next)
11136 struct bfd_elf_section_data *esdo = elf_section_data (o);
11137 if ((o->flags & SEC_RELOC) != 0)
11140 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11144 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11148 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11149 to count upwards while actually outputting the relocations. */
11150 esdo->rel.count = 0;
11151 esdo->rela.count = 0;
11153 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11155 /* Cache the section contents so that they can be compressed
11156 later. Use bfd_malloc since it will be freed by
11157 bfd_compress_section_contents. */
11158 unsigned char *contents = esdo->this_hdr.contents;
11159 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11162 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11163 if (contents == NULL)
11165 esdo->this_hdr.contents = contents;
11169 /* We have now assigned file positions for all the sections except
11170 .symtab, .strtab, and non-loaded reloc sections. We start the
11171 .symtab section at the current file position, and write directly
11172 to it. We build the .strtab section in memory. */
11173 bfd_get_symcount (abfd) = 0;
11174 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11175 /* sh_name is set in prep_headers. */
11176 symtab_hdr->sh_type = SHT_SYMTAB;
11177 /* sh_flags, sh_addr and sh_size all start off zero. */
11178 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11179 /* sh_link is set in assign_section_numbers. */
11180 /* sh_info is set below. */
11181 /* sh_offset is set just below. */
11182 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11184 if (max_sym_count < 20)
11185 max_sym_count = 20;
11186 elf_hash_table (info)->strtabsize = max_sym_count;
11187 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11188 elf_hash_table (info)->strtab
11189 = (struct elf_sym_strtab *) bfd_malloc (amt);
11190 if (elf_hash_table (info)->strtab == NULL)
11192 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11194 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11195 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11197 if (info->strip != strip_all || emit_relocs)
11199 file_ptr off = elf_next_file_pos (abfd);
11201 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11203 /* Note that at this point elf_next_file_pos (abfd) is
11204 incorrect. We do not yet know the size of the .symtab section.
11205 We correct next_file_pos below, after we do know the size. */
11207 /* Start writing out the symbol table. The first symbol is always a
11209 elfsym.st_value = 0;
11210 elfsym.st_size = 0;
11211 elfsym.st_info = 0;
11212 elfsym.st_other = 0;
11213 elfsym.st_shndx = SHN_UNDEF;
11214 elfsym.st_target_internal = 0;
11215 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11216 bfd_und_section_ptr, NULL) != 1)
11219 /* Output a symbol for each section. We output these even if we are
11220 discarding local symbols, since they are used for relocs. These
11221 symbols have no names. We store the index of each one in the
11222 index field of the section, so that we can find it again when
11223 outputting relocs. */
11225 elfsym.st_size = 0;
11226 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11227 elfsym.st_other = 0;
11228 elfsym.st_value = 0;
11229 elfsym.st_target_internal = 0;
11230 for (i = 1; i < elf_numsections (abfd); i++)
11232 o = bfd_section_from_elf_index (abfd, i);
11235 o->target_index = bfd_get_symcount (abfd);
11236 elfsym.st_shndx = i;
11237 if (!bfd_link_relocatable (info))
11238 elfsym.st_value = o->vma;
11239 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11246 /* Allocate some memory to hold information read in from the input
11248 if (max_contents_size != 0)
11250 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11251 if (flinfo.contents == NULL)
11255 if (max_external_reloc_size != 0)
11257 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11258 if (flinfo.external_relocs == NULL)
11262 if (max_internal_reloc_count != 0)
11264 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11265 amt *= sizeof (Elf_Internal_Rela);
11266 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11267 if (flinfo.internal_relocs == NULL)
11271 if (max_sym_count != 0)
11273 amt = max_sym_count * bed->s->sizeof_sym;
11274 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11275 if (flinfo.external_syms == NULL)
11278 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11279 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11280 if (flinfo.internal_syms == NULL)
11283 amt = max_sym_count * sizeof (long);
11284 flinfo.indices = (long int *) bfd_malloc (amt);
11285 if (flinfo.indices == NULL)
11288 amt = max_sym_count * sizeof (asection *);
11289 flinfo.sections = (asection **) bfd_malloc (amt);
11290 if (flinfo.sections == NULL)
11294 if (max_sym_shndx_count != 0)
11296 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11297 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11298 if (flinfo.locsym_shndx == NULL)
11302 if (elf_hash_table (info)->tls_sec)
11304 bfd_vma base, end = 0;
11307 for (sec = elf_hash_table (info)->tls_sec;
11308 sec && (sec->flags & SEC_THREAD_LOCAL);
11311 bfd_size_type size = sec->size;
11314 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11316 struct bfd_link_order *ord = sec->map_tail.link_order;
11319 size = ord->offset + ord->size;
11321 end = sec->vma + size;
11323 base = elf_hash_table (info)->tls_sec->vma;
11324 /* Only align end of TLS section if static TLS doesn't have special
11325 alignment requirements. */
11326 if (bed->static_tls_alignment == 1)
11327 end = align_power (end,
11328 elf_hash_table (info)->tls_sec->alignment_power);
11329 elf_hash_table (info)->tls_size = end - base;
11332 /* Reorder SHF_LINK_ORDER sections. */
11333 for (o = abfd->sections; o != NULL; o = o->next)
11335 if (!elf_fixup_link_order (abfd, o))
11339 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11342 /* Since ELF permits relocations to be against local symbols, we
11343 must have the local symbols available when we do the relocations.
11344 Since we would rather only read the local symbols once, and we
11345 would rather not keep them in memory, we handle all the
11346 relocations for a single input file at the same time.
11348 Unfortunately, there is no way to know the total number of local
11349 symbols until we have seen all of them, and the local symbol
11350 indices precede the global symbol indices. This means that when
11351 we are generating relocatable output, and we see a reloc against
11352 a global symbol, we can not know the symbol index until we have
11353 finished examining all the local symbols to see which ones we are
11354 going to output. To deal with this, we keep the relocations in
11355 memory, and don't output them until the end of the link. This is
11356 an unfortunate waste of memory, but I don't see a good way around
11357 it. Fortunately, it only happens when performing a relocatable
11358 link, which is not the common case. FIXME: If keep_memory is set
11359 we could write the relocs out and then read them again; I don't
11360 know how bad the memory loss will be. */
11362 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11363 sub->output_has_begun = FALSE;
11364 for (o = abfd->sections; o != NULL; o = o->next)
11366 for (p = o->map_head.link_order; p != NULL; p = p->next)
11368 if (p->type == bfd_indirect_link_order
11369 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11370 == bfd_target_elf_flavour)
11371 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11373 if (! sub->output_has_begun)
11375 if (! elf_link_input_bfd (&flinfo, sub))
11377 sub->output_has_begun = TRUE;
11380 else if (p->type == bfd_section_reloc_link_order
11381 || p->type == bfd_symbol_reloc_link_order)
11383 if (! elf_reloc_link_order (abfd, info, o, p))
11388 if (! _bfd_default_link_order (abfd, info, o, p))
11390 if (p->type == bfd_indirect_link_order
11391 && (bfd_get_flavour (sub)
11392 == bfd_target_elf_flavour)
11393 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11394 != bed->s->elfclass))
11396 const char *iclass, *oclass;
11398 if (bed->s->elfclass == ELFCLASS64)
11400 iclass = "ELFCLASS32";
11401 oclass = "ELFCLASS64";
11405 iclass = "ELFCLASS64";
11406 oclass = "ELFCLASS32";
11409 bfd_set_error (bfd_error_wrong_format);
11410 (*_bfd_error_handler)
11411 (_("%B: file class %s incompatible with %s"),
11412 sub, iclass, oclass);
11421 /* Free symbol buffer if needed. */
11422 if (!info->reduce_memory_overheads)
11424 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11425 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11426 && elf_tdata (sub)->symbuf)
11428 free (elf_tdata (sub)->symbuf);
11429 elf_tdata (sub)->symbuf = NULL;
11433 /* Output any global symbols that got converted to local in a
11434 version script or due to symbol visibility. We do this in a
11435 separate step since ELF requires all local symbols to appear
11436 prior to any global symbols. FIXME: We should only do this if
11437 some global symbols were, in fact, converted to become local.
11438 FIXME: Will this work correctly with the Irix 5 linker? */
11439 eoinfo.failed = FALSE;
11440 eoinfo.flinfo = &flinfo;
11441 eoinfo.localsyms = TRUE;
11442 eoinfo.file_sym_done = FALSE;
11443 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11447 /* If backend needs to output some local symbols not present in the hash
11448 table, do it now. */
11449 if (bed->elf_backend_output_arch_local_syms
11450 && (info->strip != strip_all || emit_relocs))
11452 typedef int (*out_sym_func)
11453 (void *, const char *, Elf_Internal_Sym *, asection *,
11454 struct elf_link_hash_entry *);
11456 if (! ((*bed->elf_backend_output_arch_local_syms)
11457 (abfd, info, &flinfo,
11458 (out_sym_func) elf_link_output_symstrtab)))
11462 /* That wrote out all the local symbols. Finish up the symbol table
11463 with the global symbols. Even if we want to strip everything we
11464 can, we still need to deal with those global symbols that got
11465 converted to local in a version script. */
11467 /* The sh_info field records the index of the first non local symbol. */
11468 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11471 && elf_hash_table (info)->dynsym != NULL
11472 && (elf_hash_table (info)->dynsym->output_section
11473 != bfd_abs_section_ptr))
11475 Elf_Internal_Sym sym;
11476 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
11477 long last_local = 0;
11479 /* Write out the section symbols for the output sections. */
11480 if (bfd_link_pic (info)
11481 || elf_hash_table (info)->is_relocatable_executable)
11487 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11489 sym.st_target_internal = 0;
11491 for (s = abfd->sections; s != NULL; s = s->next)
11497 dynindx = elf_section_data (s)->dynindx;
11500 indx = elf_section_data (s)->this_idx;
11501 BFD_ASSERT (indx > 0);
11502 sym.st_shndx = indx;
11503 if (! check_dynsym (abfd, &sym))
11505 sym.st_value = s->vma;
11506 dest = dynsym + dynindx * bed->s->sizeof_sym;
11507 if (last_local < dynindx)
11508 last_local = dynindx;
11509 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11513 /* Write out the local dynsyms. */
11514 if (elf_hash_table (info)->dynlocal)
11516 struct elf_link_local_dynamic_entry *e;
11517 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11522 /* Copy the internal symbol and turn off visibility.
11523 Note that we saved a word of storage and overwrote
11524 the original st_name with the dynstr_index. */
11526 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11528 s = bfd_section_from_elf_index (e->input_bfd,
11533 elf_section_data (s->output_section)->this_idx;
11534 if (! check_dynsym (abfd, &sym))
11536 sym.st_value = (s->output_section->vma
11538 + e->isym.st_value);
11541 if (last_local < e->dynindx)
11542 last_local = e->dynindx;
11544 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11545 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11549 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
11553 /* We get the global symbols from the hash table. */
11554 eoinfo.failed = FALSE;
11555 eoinfo.localsyms = FALSE;
11556 eoinfo.flinfo = &flinfo;
11557 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11561 /* If backend needs to output some symbols not present in the hash
11562 table, do it now. */
11563 if (bed->elf_backend_output_arch_syms
11564 && (info->strip != strip_all || emit_relocs))
11566 typedef int (*out_sym_func)
11567 (void *, const char *, Elf_Internal_Sym *, asection *,
11568 struct elf_link_hash_entry *);
11570 if (! ((*bed->elf_backend_output_arch_syms)
11571 (abfd, info, &flinfo,
11572 (out_sym_func) elf_link_output_symstrtab)))
11576 /* Finalize the .strtab section. */
11577 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11579 /* Swap out the .strtab section. */
11580 if (!elf_link_swap_symbols_out (&flinfo))
11583 /* Now we know the size of the symtab section. */
11584 if (bfd_get_symcount (abfd) > 0)
11586 /* Finish up and write out the symbol string table (.strtab)
11588 Elf_Internal_Shdr *symstrtab_hdr;
11589 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11591 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11592 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11594 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11595 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11596 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11597 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11598 symtab_shndx_hdr->sh_size = amt;
11600 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11603 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11604 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11608 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11609 /* sh_name was set in prep_headers. */
11610 symstrtab_hdr->sh_type = SHT_STRTAB;
11611 symstrtab_hdr->sh_flags = 0;
11612 symstrtab_hdr->sh_addr = 0;
11613 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11614 symstrtab_hdr->sh_entsize = 0;
11615 symstrtab_hdr->sh_link = 0;
11616 symstrtab_hdr->sh_info = 0;
11617 /* sh_offset is set just below. */
11618 symstrtab_hdr->sh_addralign = 1;
11620 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11622 elf_next_file_pos (abfd) = off;
11624 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11625 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11629 /* Adjust the relocs to have the correct symbol indices. */
11630 for (o = abfd->sections; o != NULL; o = o->next)
11632 struct bfd_elf_section_data *esdo = elf_section_data (o);
11634 if ((o->flags & SEC_RELOC) == 0)
11637 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11638 if (esdo->rel.hdr != NULL
11639 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11641 if (esdo->rela.hdr != NULL
11642 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11645 /* Set the reloc_count field to 0 to prevent write_relocs from
11646 trying to swap the relocs out itself. */
11647 o->reloc_count = 0;
11650 if (dynamic && info->combreloc && dynobj != NULL)
11651 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11653 /* If we are linking against a dynamic object, or generating a
11654 shared library, finish up the dynamic linking information. */
11657 bfd_byte *dyncon, *dynconend;
11659 /* Fix up .dynamic entries. */
11660 o = bfd_get_linker_section (dynobj, ".dynamic");
11661 BFD_ASSERT (o != NULL);
11663 dyncon = o->contents;
11664 dynconend = o->contents + o->size;
11665 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11667 Elf_Internal_Dyn dyn;
11671 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11678 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11680 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11682 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11683 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11686 dyn.d_un.d_val = relativecount;
11693 name = info->init_function;
11696 name = info->fini_function;
11699 struct elf_link_hash_entry *h;
11701 h = elf_link_hash_lookup (elf_hash_table (info), name,
11702 FALSE, FALSE, TRUE);
11704 && (h->root.type == bfd_link_hash_defined
11705 || h->root.type == bfd_link_hash_defweak))
11707 dyn.d_un.d_ptr = h->root.u.def.value;
11708 o = h->root.u.def.section;
11709 if (o->output_section != NULL)
11710 dyn.d_un.d_ptr += (o->output_section->vma
11711 + o->output_offset);
11714 /* The symbol is imported from another shared
11715 library and does not apply to this one. */
11716 dyn.d_un.d_ptr = 0;
11723 case DT_PREINIT_ARRAYSZ:
11724 name = ".preinit_array";
11726 case DT_INIT_ARRAYSZ:
11727 name = ".init_array";
11729 case DT_FINI_ARRAYSZ:
11730 name = ".fini_array";
11732 o = bfd_get_section_by_name (abfd, name);
11735 (*_bfd_error_handler)
11736 (_("%B: could not find output section %s"), abfd, name);
11740 (*_bfd_error_handler)
11741 (_("warning: %s section has zero size"), name);
11742 dyn.d_un.d_val = o->size;
11745 case DT_PREINIT_ARRAY:
11746 name = ".preinit_array";
11748 case DT_INIT_ARRAY:
11749 name = ".init_array";
11751 case DT_FINI_ARRAY:
11752 name = ".fini_array";
11759 name = ".gnu.hash";
11768 name = ".gnu.version_d";
11771 name = ".gnu.version_r";
11774 name = ".gnu.version";
11776 o = bfd_get_section_by_name (abfd, name);
11779 (*_bfd_error_handler)
11780 (_("%B: could not find output section %s"), abfd, name);
11783 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11785 (*_bfd_error_handler)
11786 (_("warning: section '%s' is being made into a note"), name);
11787 bfd_set_error (bfd_error_nonrepresentable_section);
11790 dyn.d_un.d_ptr = o->vma;
11797 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11801 dyn.d_un.d_val = 0;
11802 dyn.d_un.d_ptr = 0;
11803 for (i = 1; i < elf_numsections (abfd); i++)
11805 Elf_Internal_Shdr *hdr;
11807 hdr = elf_elfsections (abfd)[i];
11808 if (hdr->sh_type == type
11809 && (hdr->sh_flags & SHF_ALLOC) != 0)
11811 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11812 dyn.d_un.d_val += hdr->sh_size;
11815 if (dyn.d_un.d_ptr == 0
11816 || hdr->sh_addr < dyn.d_un.d_ptr)
11817 dyn.d_un.d_ptr = hdr->sh_addr;
11823 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11827 /* If we have created any dynamic sections, then output them. */
11828 if (dynobj != NULL)
11830 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11833 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11834 if (((info->warn_shared_textrel && bfd_link_pic (info))
11835 || info->error_textrel)
11836 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
11838 bfd_byte *dyncon, *dynconend;
11840 dyncon = o->contents;
11841 dynconend = o->contents + o->size;
11842 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11844 Elf_Internal_Dyn dyn;
11846 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11848 if (dyn.d_tag == DT_TEXTREL)
11850 if (info->error_textrel)
11851 info->callbacks->einfo
11852 (_("%P%X: read-only segment has dynamic relocations.\n"));
11854 info->callbacks->einfo
11855 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11861 for (o = dynobj->sections; o != NULL; o = o->next)
11863 if ((o->flags & SEC_HAS_CONTENTS) == 0
11865 || o->output_section == bfd_abs_section_ptr)
11867 if ((o->flags & SEC_LINKER_CREATED) == 0)
11869 /* At this point, we are only interested in sections
11870 created by _bfd_elf_link_create_dynamic_sections. */
11873 if (elf_hash_table (info)->stab_info.stabstr == o)
11875 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11877 if (strcmp (o->name, ".dynstr") != 0)
11879 /* FIXME: octets_per_byte. */
11880 if (! bfd_set_section_contents (abfd, o->output_section,
11882 (file_ptr) o->output_offset,
11888 /* The contents of the .dynstr section are actually in a
11892 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11893 if (bfd_seek (abfd, off, SEEK_SET) != 0
11894 || ! _bfd_elf_strtab_emit (abfd,
11895 elf_hash_table (info)->dynstr))
11901 if (bfd_link_relocatable (info))
11903 bfd_boolean failed = FALSE;
11905 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11910 /* If we have optimized stabs strings, output them. */
11911 if (elf_hash_table (info)->stab_info.stabstr != NULL)
11913 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11917 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11920 elf_final_link_free (abfd, &flinfo);
11922 elf_linker (abfd) = TRUE;
11926 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
11927 if (contents == NULL)
11928 return FALSE; /* Bail out and fail. */
11929 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11930 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11937 elf_final_link_free (abfd, &flinfo);
11941 /* Initialize COOKIE for input bfd ABFD. */
11944 init_reloc_cookie (struct elf_reloc_cookie *cookie,
11945 struct bfd_link_info *info, bfd *abfd)
11947 Elf_Internal_Shdr *symtab_hdr;
11948 const struct elf_backend_data *bed;
11950 bed = get_elf_backend_data (abfd);
11951 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11953 cookie->abfd = abfd;
11954 cookie->sym_hashes = elf_sym_hashes (abfd);
11955 cookie->bad_symtab = elf_bad_symtab (abfd);
11956 if (cookie->bad_symtab)
11958 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11959 cookie->extsymoff = 0;
11963 cookie->locsymcount = symtab_hdr->sh_info;
11964 cookie->extsymoff = symtab_hdr->sh_info;
11967 if (bed->s->arch_size == 32)
11968 cookie->r_sym_shift = 8;
11970 cookie->r_sym_shift = 32;
11972 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11973 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11975 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11976 cookie->locsymcount, 0,
11978 if (cookie->locsyms == NULL)
11980 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11983 if (info->keep_memory)
11984 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11989 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11992 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11994 Elf_Internal_Shdr *symtab_hdr;
11996 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11997 if (cookie->locsyms != NULL
11998 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11999 free (cookie->locsyms);
12002 /* Initialize the relocation information in COOKIE for input section SEC
12003 of input bfd ABFD. */
12006 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12007 struct bfd_link_info *info, bfd *abfd,
12010 const struct elf_backend_data *bed;
12012 if (sec->reloc_count == 0)
12014 cookie->rels = NULL;
12015 cookie->relend = NULL;
12019 bed = get_elf_backend_data (abfd);
12021 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12022 info->keep_memory);
12023 if (cookie->rels == NULL)
12025 cookie->rel = cookie->rels;
12026 cookie->relend = (cookie->rels
12027 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12029 cookie->rel = cookie->rels;
12033 /* Free the memory allocated by init_reloc_cookie_rels,
12037 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12040 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12041 free (cookie->rels);
12044 /* Initialize the whole of COOKIE for input section SEC. */
12047 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12048 struct bfd_link_info *info,
12051 if (!init_reloc_cookie (cookie, info, sec->owner))
12053 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12058 fini_reloc_cookie (cookie, sec->owner);
12063 /* Free the memory allocated by init_reloc_cookie_for_section,
12067 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12070 fini_reloc_cookie_rels (cookie, sec);
12071 fini_reloc_cookie (cookie, sec->owner);
12074 /* Garbage collect unused sections. */
12076 /* Default gc_mark_hook. */
12079 _bfd_elf_gc_mark_hook (asection *sec,
12080 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12081 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12082 struct elf_link_hash_entry *h,
12083 Elf_Internal_Sym *sym)
12087 switch (h->root.type)
12089 case bfd_link_hash_defined:
12090 case bfd_link_hash_defweak:
12091 return h->root.u.def.section;
12093 case bfd_link_hash_common:
12094 return h->root.u.c.p->section;
12101 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12106 /* COOKIE->rel describes a relocation against section SEC, which is
12107 a section we've decided to keep. Return the section that contains
12108 the relocation symbol, or NULL if no section contains it. */
12111 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12112 elf_gc_mark_hook_fn gc_mark_hook,
12113 struct elf_reloc_cookie *cookie,
12114 bfd_boolean *start_stop)
12116 unsigned long r_symndx;
12117 struct elf_link_hash_entry *h;
12119 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12120 if (r_symndx == STN_UNDEF)
12123 if (r_symndx >= cookie->locsymcount
12124 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12126 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12129 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12133 while (h->root.type == bfd_link_hash_indirect
12134 || h->root.type == bfd_link_hash_warning)
12135 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12137 /* If this symbol is weak and there is a non-weak definition, we
12138 keep the non-weak definition because many backends put
12139 dynamic reloc info on the non-weak definition for code
12140 handling copy relocs. */
12141 if (h->u.weakdef != NULL)
12142 h->u.weakdef->mark = 1;
12144 if (start_stop != NULL
12145 && (h->root.type == bfd_link_hash_undefined
12146 || h->root.type == bfd_link_hash_undefweak))
12148 /* To work around a glibc bug, mark all XXX input sections
12149 when there is an as yet undefined reference to __start_XXX
12150 or __stop_XXX symbols. The linker will later define such
12151 symbols for orphan input sections that have a name
12152 representable as a C identifier. */
12153 const char *sec_name = NULL;
12154 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12155 sec_name = h->root.root.string + 8;
12156 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12157 sec_name = h->root.root.string + 7;
12159 if (sec_name != NULL && *sec_name != '\0')
12163 for (i = info->input_bfds; i != NULL; i = i->link.next)
12165 asection *s = bfd_get_section_by_name (i, sec_name);
12166 if (s != NULL && !s->gc_mark)
12168 *start_stop = TRUE;
12175 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12178 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12179 &cookie->locsyms[r_symndx]);
12182 /* COOKIE->rel describes a relocation against section SEC, which is
12183 a section we've decided to keep. Mark the section that contains
12184 the relocation symbol. */
12187 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12189 elf_gc_mark_hook_fn gc_mark_hook,
12190 struct elf_reloc_cookie *cookie)
12193 bfd_boolean start_stop = FALSE;
12195 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12196 while (rsec != NULL)
12198 if (!rsec->gc_mark)
12200 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12201 || (rsec->owner->flags & DYNAMIC) != 0)
12203 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12208 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12213 /* The mark phase of garbage collection. For a given section, mark
12214 it and any sections in this section's group, and all the sections
12215 which define symbols to which it refers. */
12218 _bfd_elf_gc_mark (struct bfd_link_info *info,
12220 elf_gc_mark_hook_fn gc_mark_hook)
12223 asection *group_sec, *eh_frame;
12227 /* Mark all the sections in the group. */
12228 group_sec = elf_section_data (sec)->next_in_group;
12229 if (group_sec && !group_sec->gc_mark)
12230 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12233 /* Look through the section relocs. */
12235 eh_frame = elf_eh_frame_section (sec->owner);
12236 if ((sec->flags & SEC_RELOC) != 0
12237 && sec->reloc_count > 0
12238 && sec != eh_frame)
12240 struct elf_reloc_cookie cookie;
12242 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12246 for (; cookie.rel < cookie.relend; cookie.rel++)
12247 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12252 fini_reloc_cookie_for_section (&cookie, sec);
12256 if (ret && eh_frame && elf_fde_list (sec))
12258 struct elf_reloc_cookie cookie;
12260 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12264 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12265 gc_mark_hook, &cookie))
12267 fini_reloc_cookie_for_section (&cookie, eh_frame);
12271 eh_frame = elf_section_eh_frame_entry (sec);
12272 if (ret && eh_frame && !eh_frame->gc_mark)
12273 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12279 /* Scan and mark sections in a special or debug section group. */
12282 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12284 /* Point to first section of section group. */
12286 /* Used to iterate the section group. */
12289 bfd_boolean is_special_grp = TRUE;
12290 bfd_boolean is_debug_grp = TRUE;
12292 /* First scan to see if group contains any section other than debug
12293 and special section. */
12294 ssec = msec = elf_next_in_group (grp);
12297 if ((msec->flags & SEC_DEBUGGING) == 0)
12298 is_debug_grp = FALSE;
12300 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12301 is_special_grp = FALSE;
12303 msec = elf_next_in_group (msec);
12305 while (msec != ssec);
12307 /* If this is a pure debug section group or pure special section group,
12308 keep all sections in this group. */
12309 if (is_debug_grp || is_special_grp)
12314 msec = elf_next_in_group (msec);
12316 while (msec != ssec);
12320 /* Keep debug and special sections. */
12323 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12324 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12328 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12331 bfd_boolean some_kept;
12332 bfd_boolean debug_frag_seen;
12334 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12337 /* Ensure all linker created sections are kept,
12338 see if any other section is already marked,
12339 and note if we have any fragmented debug sections. */
12340 debug_frag_seen = some_kept = FALSE;
12341 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12343 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12345 else if (isec->gc_mark)
12348 if (debug_frag_seen == FALSE
12349 && (isec->flags & SEC_DEBUGGING)
12350 && CONST_STRNEQ (isec->name, ".debug_line."))
12351 debug_frag_seen = TRUE;
12354 /* If no section in this file will be kept, then we can
12355 toss out the debug and special sections. */
12359 /* Keep debug and special sections like .comment when they are
12360 not part of a group. Also keep section groups that contain
12361 just debug sections or special sections. */
12362 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12364 if ((isec->flags & SEC_GROUP) != 0)
12365 _bfd_elf_gc_mark_debug_special_section_group (isec);
12366 else if (((isec->flags & SEC_DEBUGGING) != 0
12367 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12368 && elf_next_in_group (isec) == NULL)
12372 if (! debug_frag_seen)
12375 /* Look for CODE sections which are going to be discarded,
12376 and find and discard any fragmented debug sections which
12377 are associated with that code section. */
12378 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12379 if ((isec->flags & SEC_CODE) != 0
12380 && isec->gc_mark == 0)
12385 ilen = strlen (isec->name);
12387 /* Association is determined by the name of the debug section
12388 containing the name of the code section as a suffix. For
12389 example .debug_line.text.foo is a debug section associated
12391 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12395 if (dsec->gc_mark == 0
12396 || (dsec->flags & SEC_DEBUGGING) == 0)
12399 dlen = strlen (dsec->name);
12402 && strncmp (dsec->name + (dlen - ilen),
12403 isec->name, ilen) == 0)
12413 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12415 struct elf_gc_sweep_symbol_info
12417 struct bfd_link_info *info;
12418 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12423 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12426 && (((h->root.type == bfd_link_hash_defined
12427 || h->root.type == bfd_link_hash_defweak)
12428 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12429 && h->root.u.def.section->gc_mark))
12430 || h->root.type == bfd_link_hash_undefined
12431 || h->root.type == bfd_link_hash_undefweak))
12433 struct elf_gc_sweep_symbol_info *inf;
12435 inf = (struct elf_gc_sweep_symbol_info *) data;
12436 (*inf->hide_symbol) (inf->info, h, TRUE);
12437 h->def_regular = 0;
12438 h->ref_regular = 0;
12439 h->ref_regular_nonweak = 0;
12445 /* The sweep phase of garbage collection. Remove all garbage sections. */
12447 typedef bfd_boolean (*gc_sweep_hook_fn)
12448 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12451 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12454 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12455 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12456 unsigned long section_sym_count;
12457 struct elf_gc_sweep_symbol_info sweep_info;
12459 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12463 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12464 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12467 for (o = sub->sections; o != NULL; o = o->next)
12469 /* When any section in a section group is kept, we keep all
12470 sections in the section group. If the first member of
12471 the section group is excluded, we will also exclude the
12473 if (o->flags & SEC_GROUP)
12475 asection *first = elf_next_in_group (o);
12476 o->gc_mark = first->gc_mark;
12482 /* Skip sweeping sections already excluded. */
12483 if (o->flags & SEC_EXCLUDE)
12486 /* Since this is early in the link process, it is simple
12487 to remove a section from the output. */
12488 o->flags |= SEC_EXCLUDE;
12490 if (info->print_gc_sections && o->size != 0)
12491 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12493 /* But we also have to update some of the relocation
12494 info we collected before. */
12496 && (o->flags & SEC_RELOC) != 0
12497 && o->reloc_count != 0
12498 && !((info->strip == strip_all || info->strip == strip_debugger)
12499 && (o->flags & SEC_DEBUGGING) != 0)
12500 && !bfd_is_abs_section (o->output_section))
12502 Elf_Internal_Rela *internal_relocs;
12506 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12507 info->keep_memory);
12508 if (internal_relocs == NULL)
12511 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12513 if (elf_section_data (o)->relocs != internal_relocs)
12514 free (internal_relocs);
12522 /* Remove the symbols that were in the swept sections from the dynamic
12523 symbol table. GCFIXME: Anyone know how to get them out of the
12524 static symbol table as well? */
12525 sweep_info.info = info;
12526 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12527 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12530 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
12534 /* Propagate collected vtable information. This is called through
12535 elf_link_hash_traverse. */
12538 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12540 /* Those that are not vtables. */
12541 if (h->vtable == NULL || h->vtable->parent == NULL)
12544 /* Those vtables that do not have parents, we cannot merge. */
12545 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12548 /* If we've already been done, exit. */
12549 if (h->vtable->used && h->vtable->used[-1])
12552 /* Make sure the parent's table is up to date. */
12553 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12555 if (h->vtable->used == NULL)
12557 /* None of this table's entries were referenced. Re-use the
12559 h->vtable->used = h->vtable->parent->vtable->used;
12560 h->vtable->size = h->vtable->parent->vtable->size;
12565 bfd_boolean *cu, *pu;
12567 /* Or the parent's entries into ours. */
12568 cu = h->vtable->used;
12570 pu = h->vtable->parent->vtable->used;
12573 const struct elf_backend_data *bed;
12574 unsigned int log_file_align;
12576 bed = get_elf_backend_data (h->root.u.def.section->owner);
12577 log_file_align = bed->s->log_file_align;
12578 n = h->vtable->parent->vtable->size >> log_file_align;
12593 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12596 bfd_vma hstart, hend;
12597 Elf_Internal_Rela *relstart, *relend, *rel;
12598 const struct elf_backend_data *bed;
12599 unsigned int log_file_align;
12601 /* Take care of both those symbols that do not describe vtables as
12602 well as those that are not loaded. */
12603 if (h->vtable == NULL || h->vtable->parent == NULL)
12606 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12607 || h->root.type == bfd_link_hash_defweak);
12609 sec = h->root.u.def.section;
12610 hstart = h->root.u.def.value;
12611 hend = hstart + h->size;
12613 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12615 return *(bfd_boolean *) okp = FALSE;
12616 bed = get_elf_backend_data (sec->owner);
12617 log_file_align = bed->s->log_file_align;
12619 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12621 for (rel = relstart; rel < relend; ++rel)
12622 if (rel->r_offset >= hstart && rel->r_offset < hend)
12624 /* If the entry is in use, do nothing. */
12625 if (h->vtable->used
12626 && (rel->r_offset - hstart) < h->vtable->size)
12628 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12629 if (h->vtable->used[entry])
12632 /* Otherwise, kill it. */
12633 rel->r_offset = rel->r_info = rel->r_addend = 0;
12639 /* Mark sections containing dynamically referenced symbols. When
12640 building shared libraries, we must assume that any visible symbol is
12644 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12646 struct bfd_link_info *info = (struct bfd_link_info *) inf;
12647 struct bfd_elf_dynamic_list *d = info->dynamic_list;
12649 if ((h->root.type == bfd_link_hash_defined
12650 || h->root.type == bfd_link_hash_defweak)
12652 || ((h->def_regular || ELF_COMMON_DEF_P (h))
12653 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12654 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12655 && (!bfd_link_executable (info)
12656 || info->export_dynamic
12659 && (*d->match) (&d->head, NULL, h->root.root.string)))
12660 && (h->versioned >= versioned
12661 || !bfd_hide_sym_by_version (info->version_info,
12662 h->root.root.string)))))
12663 h->root.u.def.section->flags |= SEC_KEEP;
12668 /* Keep all sections containing symbols undefined on the command-line,
12669 and the section containing the entry symbol. */
12672 _bfd_elf_gc_keep (struct bfd_link_info *info)
12674 struct bfd_sym_chain *sym;
12676 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12678 struct elf_link_hash_entry *h;
12680 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12681 FALSE, FALSE, FALSE);
12684 && (h->root.type == bfd_link_hash_defined
12685 || h->root.type == bfd_link_hash_defweak)
12686 && !bfd_is_abs_section (h->root.u.def.section))
12687 h->root.u.def.section->flags |= SEC_KEEP;
12692 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12693 struct bfd_link_info *info)
12695 bfd *ibfd = info->input_bfds;
12697 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12700 struct elf_reloc_cookie cookie;
12702 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12705 if (!init_reloc_cookie (&cookie, info, ibfd))
12708 for (sec = ibfd->sections; sec; sec = sec->next)
12710 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12711 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12713 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12714 fini_reloc_cookie_rels (&cookie, sec);
12721 /* Do mark and sweep of unused sections. */
12724 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12726 bfd_boolean ok = TRUE;
12728 elf_gc_mark_hook_fn gc_mark_hook;
12729 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12730 struct elf_link_hash_table *htab;
12732 if (!bed->can_gc_sections
12733 || !is_elf_hash_table (info->hash))
12735 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12739 bed->gc_keep (info);
12740 htab = elf_hash_table (info);
12742 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12743 at the .eh_frame section if we can mark the FDEs individually. */
12744 for (sub = info->input_bfds;
12745 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12746 sub = sub->link.next)
12749 struct elf_reloc_cookie cookie;
12751 sec = bfd_get_section_by_name (sub, ".eh_frame");
12752 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12754 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12755 if (elf_section_data (sec)->sec_info
12756 && (sec->flags & SEC_LINKER_CREATED) == 0)
12757 elf_eh_frame_section (sub) = sec;
12758 fini_reloc_cookie_for_section (&cookie, sec);
12759 sec = bfd_get_next_section_by_name (NULL, sec);
12763 /* Apply transitive closure to the vtable entry usage info. */
12764 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
12768 /* Kill the vtable relocations that were not used. */
12769 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
12773 /* Mark dynamically referenced symbols. */
12774 if (htab->dynamic_sections_created)
12775 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
12777 /* Grovel through relocs to find out who stays ... */
12778 gc_mark_hook = bed->gc_mark_hook;
12779 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12783 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12784 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12787 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12788 Also treat note sections as a root, if the section is not part
12790 for (o = sub->sections; o != NULL; o = o->next)
12792 && (o->flags & SEC_EXCLUDE) == 0
12793 && ((o->flags & SEC_KEEP) != 0
12794 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12795 && elf_next_in_group (o) == NULL )))
12797 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12802 /* Allow the backend to mark additional target specific sections. */
12803 bed->gc_mark_extra_sections (info, gc_mark_hook);
12805 /* ... and mark SEC_EXCLUDE for those that go. */
12806 return elf_gc_sweep (abfd, info);
12809 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12812 bfd_elf_gc_record_vtinherit (bfd *abfd,
12814 struct elf_link_hash_entry *h,
12817 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12818 struct elf_link_hash_entry **search, *child;
12819 bfd_size_type extsymcount;
12820 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12822 /* The sh_info field of the symtab header tells us where the
12823 external symbols start. We don't care about the local symbols at
12825 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12826 if (!elf_bad_symtab (abfd))
12827 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12829 sym_hashes = elf_sym_hashes (abfd);
12830 sym_hashes_end = sym_hashes + extsymcount;
12832 /* Hunt down the child symbol, which is in this section at the same
12833 offset as the relocation. */
12834 for (search = sym_hashes; search != sym_hashes_end; ++search)
12836 if ((child = *search) != NULL
12837 && (child->root.type == bfd_link_hash_defined
12838 || child->root.type == bfd_link_hash_defweak)
12839 && child->root.u.def.section == sec
12840 && child->root.u.def.value == offset)
12844 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12845 abfd, sec, (unsigned long) offset);
12846 bfd_set_error (bfd_error_invalid_operation);
12850 if (!child->vtable)
12852 child->vtable = ((struct elf_link_virtual_table_entry *)
12853 bfd_zalloc (abfd, sizeof (*child->vtable)));
12854 if (!child->vtable)
12859 /* This *should* only be the absolute section. It could potentially
12860 be that someone has defined a non-global vtable though, which
12861 would be bad. It isn't worth paging in the local symbols to be
12862 sure though; that case should simply be handled by the assembler. */
12864 child->vtable->parent = (struct elf_link_hash_entry *) -1;
12867 child->vtable->parent = h;
12872 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
12875 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12876 asection *sec ATTRIBUTE_UNUSED,
12877 struct elf_link_hash_entry *h,
12880 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12881 unsigned int log_file_align = bed->s->log_file_align;
12885 h->vtable = ((struct elf_link_virtual_table_entry *)
12886 bfd_zalloc (abfd, sizeof (*h->vtable)));
12891 if (addend >= h->vtable->size)
12893 size_t size, bytes, file_align;
12894 bfd_boolean *ptr = h->vtable->used;
12896 /* While the symbol is undefined, we have to be prepared to handle
12898 file_align = 1 << log_file_align;
12899 if (h->root.type == bfd_link_hash_undefined)
12900 size = addend + file_align;
12904 if (addend >= size)
12906 /* Oops! We've got a reference past the defined end of
12907 the table. This is probably a bug -- shall we warn? */
12908 size = addend + file_align;
12911 size = (size + file_align - 1) & -file_align;
12913 /* Allocate one extra entry for use as a "done" flag for the
12914 consolidation pass. */
12915 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12919 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
12925 oldbytes = (((h->vtable->size >> log_file_align) + 1)
12926 * sizeof (bfd_boolean));
12927 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12931 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
12936 /* And arrange for that done flag to be at index -1. */
12937 h->vtable->used = ptr + 1;
12938 h->vtable->size = size;
12941 h->vtable->used[addend >> log_file_align] = TRUE;
12946 /* Map an ELF section header flag to its corresponding string. */
12950 flagword flag_value;
12951 } elf_flags_to_name_table;
12953 static elf_flags_to_name_table elf_flags_to_names [] =
12955 { "SHF_WRITE", SHF_WRITE },
12956 { "SHF_ALLOC", SHF_ALLOC },
12957 { "SHF_EXECINSTR", SHF_EXECINSTR },
12958 { "SHF_MERGE", SHF_MERGE },
12959 { "SHF_STRINGS", SHF_STRINGS },
12960 { "SHF_INFO_LINK", SHF_INFO_LINK},
12961 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12962 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12963 { "SHF_GROUP", SHF_GROUP },
12964 { "SHF_TLS", SHF_TLS },
12965 { "SHF_MASKOS", SHF_MASKOS },
12966 { "SHF_EXCLUDE", SHF_EXCLUDE },
12969 /* Returns TRUE if the section is to be included, otherwise FALSE. */
12971 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
12972 struct flag_info *flaginfo,
12975 const bfd_vma sh_flags = elf_section_flags (section);
12977 if (!flaginfo->flags_initialized)
12979 bfd *obfd = info->output_bfd;
12980 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12981 struct flag_info_list *tf = flaginfo->flag_list;
12983 int without_hex = 0;
12985 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
12988 flagword (*lookup) (char *);
12990 lookup = bed->elf_backend_lookup_section_flags_hook;
12991 if (lookup != NULL)
12993 flagword hexval = (*lookup) ((char *) tf->name);
12997 if (tf->with == with_flags)
12998 with_hex |= hexval;
12999 else if (tf->with == without_flags)
13000 without_hex |= hexval;
13005 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13007 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13009 if (tf->with == with_flags)
13010 with_hex |= elf_flags_to_names[i].flag_value;
13011 else if (tf->with == without_flags)
13012 without_hex |= elf_flags_to_names[i].flag_value;
13019 info->callbacks->einfo
13020 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13024 flaginfo->flags_initialized = TRUE;
13025 flaginfo->only_with_flags |= with_hex;
13026 flaginfo->not_with_flags |= without_hex;
13029 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13032 if ((flaginfo->not_with_flags & sh_flags) != 0)
13038 struct alloc_got_off_arg {
13040 struct bfd_link_info *info;
13043 /* We need a special top-level link routine to convert got reference counts
13044 to real got offsets. */
13047 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13049 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13050 bfd *obfd = gofarg->info->output_bfd;
13051 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13053 if (h->got.refcount > 0)
13055 h->got.offset = gofarg->gotoff;
13056 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13059 h->got.offset = (bfd_vma) -1;
13064 /* And an accompanying bit to work out final got entry offsets once
13065 we're done. Should be called from final_link. */
13068 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13069 struct bfd_link_info *info)
13072 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13074 struct alloc_got_off_arg gofarg;
13076 BFD_ASSERT (abfd == info->output_bfd);
13078 if (! is_elf_hash_table (info->hash))
13081 /* The GOT offset is relative to the .got section, but the GOT header is
13082 put into the .got.plt section, if the backend uses it. */
13083 if (bed->want_got_plt)
13086 gotoff = bed->got_header_size;
13088 /* Do the local .got entries first. */
13089 for (i = info->input_bfds; i; i = i->link.next)
13091 bfd_signed_vma *local_got;
13092 bfd_size_type j, locsymcount;
13093 Elf_Internal_Shdr *symtab_hdr;
13095 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13098 local_got = elf_local_got_refcounts (i);
13102 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13103 if (elf_bad_symtab (i))
13104 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13106 locsymcount = symtab_hdr->sh_info;
13108 for (j = 0; j < locsymcount; ++j)
13110 if (local_got[j] > 0)
13112 local_got[j] = gotoff;
13113 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13116 local_got[j] = (bfd_vma) -1;
13120 /* Then the global .got entries. .plt refcounts are handled by
13121 adjust_dynamic_symbol */
13122 gofarg.gotoff = gotoff;
13123 gofarg.info = info;
13124 elf_link_hash_traverse (elf_hash_table (info),
13125 elf_gc_allocate_got_offsets,
13130 /* Many folk need no more in the way of final link than this, once
13131 got entry reference counting is enabled. */
13134 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13136 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13139 /* Invoke the regular ELF backend linker to do all the work. */
13140 return bfd_elf_final_link (abfd, info);
13144 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13146 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13148 if (rcookie->bad_symtab)
13149 rcookie->rel = rcookie->rels;
13151 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13153 unsigned long r_symndx;
13155 if (! rcookie->bad_symtab)
13156 if (rcookie->rel->r_offset > offset)
13158 if (rcookie->rel->r_offset != offset)
13161 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13162 if (r_symndx == STN_UNDEF)
13165 if (r_symndx >= rcookie->locsymcount
13166 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13168 struct elf_link_hash_entry *h;
13170 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13172 while (h->root.type == bfd_link_hash_indirect
13173 || h->root.type == bfd_link_hash_warning)
13174 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13176 if ((h->root.type == bfd_link_hash_defined
13177 || h->root.type == bfd_link_hash_defweak)
13178 && (h->root.u.def.section->owner != rcookie->abfd
13179 || h->root.u.def.section->kept_section != NULL
13180 || discarded_section (h->root.u.def.section)))
13185 /* It's not a relocation against a global symbol,
13186 but it could be a relocation against a local
13187 symbol for a discarded section. */
13189 Elf_Internal_Sym *isym;
13191 /* Need to: get the symbol; get the section. */
13192 isym = &rcookie->locsyms[r_symndx];
13193 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13195 && (isec->kept_section != NULL
13196 || discarded_section (isec)))
13204 /* Discard unneeded references to discarded sections.
13205 Returns -1 on error, 1 if any section's size was changed, 0 if
13206 nothing changed. This function assumes that the relocations are in
13207 sorted order, which is true for all known assemblers. */
13210 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13212 struct elf_reloc_cookie cookie;
13217 if (info->traditional_format
13218 || !is_elf_hash_table (info->hash))
13221 o = bfd_get_section_by_name (output_bfd, ".stab");
13226 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13229 || i->reloc_count == 0
13230 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13234 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13237 if (!init_reloc_cookie_for_section (&cookie, info, i))
13240 if (_bfd_discard_section_stabs (abfd, i,
13241 elf_section_data (i)->sec_info,
13242 bfd_elf_reloc_symbol_deleted_p,
13246 fini_reloc_cookie_for_section (&cookie, i);
13251 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13252 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13257 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13263 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13266 if (!init_reloc_cookie_for_section (&cookie, info, i))
13269 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13270 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13271 bfd_elf_reloc_symbol_deleted_p,
13275 fini_reloc_cookie_for_section (&cookie, i);
13279 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13281 const struct elf_backend_data *bed;
13283 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13286 bed = get_elf_backend_data (abfd);
13288 if (bed->elf_backend_discard_info != NULL)
13290 if (!init_reloc_cookie (&cookie, info, abfd))
13293 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13296 fini_reloc_cookie (&cookie, abfd);
13300 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13301 _bfd_elf_end_eh_frame_parsing (info);
13303 if (info->eh_frame_hdr_type
13304 && !bfd_link_relocatable (info)
13305 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13312 _bfd_elf_section_already_linked (bfd *abfd,
13314 struct bfd_link_info *info)
13317 const char *name, *key;
13318 struct bfd_section_already_linked *l;
13319 struct bfd_section_already_linked_hash_entry *already_linked_list;
13321 if (sec->output_section == bfd_abs_section_ptr)
13324 flags = sec->flags;
13326 /* Return if it isn't a linkonce section. A comdat group section
13327 also has SEC_LINK_ONCE set. */
13328 if ((flags & SEC_LINK_ONCE) == 0)
13331 /* Don't put group member sections on our list of already linked
13332 sections. They are handled as a group via their group section. */
13333 if (elf_sec_group (sec) != NULL)
13336 /* For a SHT_GROUP section, use the group signature as the key. */
13338 if ((flags & SEC_GROUP) != 0
13339 && elf_next_in_group (sec) != NULL
13340 && elf_group_name (elf_next_in_group (sec)) != NULL)
13341 key = elf_group_name (elf_next_in_group (sec));
13344 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13345 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13346 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13349 /* Must be a user linkonce section that doesn't follow gcc's
13350 naming convention. In this case we won't be matching
13351 single member groups. */
13355 already_linked_list = bfd_section_already_linked_table_lookup (key);
13357 for (l = already_linked_list->entry; l != NULL; l = l->next)
13359 /* We may have 2 different types of sections on the list: group
13360 sections with a signature of <key> (<key> is some string),
13361 and linkonce sections named .gnu.linkonce.<type>.<key>.
13362 Match like sections. LTO plugin sections are an exception.
13363 They are always named .gnu.linkonce.t.<key> and match either
13364 type of section. */
13365 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13366 && ((flags & SEC_GROUP) != 0
13367 || strcmp (name, l->sec->name) == 0))
13368 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13370 /* The section has already been linked. See if we should
13371 issue a warning. */
13372 if (!_bfd_handle_already_linked (sec, l, info))
13375 if (flags & SEC_GROUP)
13377 asection *first = elf_next_in_group (sec);
13378 asection *s = first;
13382 s->output_section = bfd_abs_section_ptr;
13383 /* Record which group discards it. */
13384 s->kept_section = l->sec;
13385 s = elf_next_in_group (s);
13386 /* These lists are circular. */
13396 /* A single member comdat group section may be discarded by a
13397 linkonce section and vice versa. */
13398 if ((flags & SEC_GROUP) != 0)
13400 asection *first = elf_next_in_group (sec);
13402 if (first != NULL && elf_next_in_group (first) == first)
13403 /* Check this single member group against linkonce sections. */
13404 for (l = already_linked_list->entry; l != NULL; l = l->next)
13405 if ((l->sec->flags & SEC_GROUP) == 0
13406 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13408 first->output_section = bfd_abs_section_ptr;
13409 first->kept_section = l->sec;
13410 sec->output_section = bfd_abs_section_ptr;
13415 /* Check this linkonce section against single member groups. */
13416 for (l = already_linked_list->entry; l != NULL; l = l->next)
13417 if (l->sec->flags & SEC_GROUP)
13419 asection *first = elf_next_in_group (l->sec);
13422 && elf_next_in_group (first) == first
13423 && bfd_elf_match_symbols_in_sections (first, sec, info))
13425 sec->output_section = bfd_abs_section_ptr;
13426 sec->kept_section = first;
13431 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13432 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13433 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13434 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13435 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13436 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13437 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13438 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13439 The reverse order cannot happen as there is never a bfd with only the
13440 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13441 matter as here were are looking only for cross-bfd sections. */
13443 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13444 for (l = already_linked_list->entry; l != NULL; l = l->next)
13445 if ((l->sec->flags & SEC_GROUP) == 0
13446 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13448 if (abfd != l->sec->owner)
13449 sec->output_section = bfd_abs_section_ptr;
13453 /* This is the first section with this name. Record it. */
13454 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13455 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13456 return sec->output_section == bfd_abs_section_ptr;
13460 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13462 return sym->st_shndx == SHN_COMMON;
13466 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13472 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13474 return bfd_com_section_ptr;
13478 _bfd_elf_default_got_elt_size (bfd *abfd,
13479 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13480 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13481 bfd *ibfd ATTRIBUTE_UNUSED,
13482 unsigned long symndx ATTRIBUTE_UNUSED)
13484 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13485 return bed->s->arch_size / 8;
13488 /* Routines to support the creation of dynamic relocs. */
13490 /* Returns the name of the dynamic reloc section associated with SEC. */
13492 static const char *
13493 get_dynamic_reloc_section_name (bfd * abfd,
13495 bfd_boolean is_rela)
13498 const char *old_name = bfd_get_section_name (NULL, sec);
13499 const char *prefix = is_rela ? ".rela" : ".rel";
13501 if (old_name == NULL)
13504 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13505 sprintf (name, "%s%s", prefix, old_name);
13510 /* Returns the dynamic reloc section associated with SEC.
13511 If necessary compute the name of the dynamic reloc section based
13512 on SEC's name (looked up in ABFD's string table) and the setting
13516 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13518 bfd_boolean is_rela)
13520 asection * reloc_sec = elf_section_data (sec)->sreloc;
13522 if (reloc_sec == NULL)
13524 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13528 reloc_sec = bfd_get_linker_section (abfd, name);
13530 if (reloc_sec != NULL)
13531 elf_section_data (sec)->sreloc = reloc_sec;
13538 /* Returns the dynamic reloc section associated with SEC. If the
13539 section does not exist it is created and attached to the DYNOBJ
13540 bfd and stored in the SRELOC field of SEC's elf_section_data
13543 ALIGNMENT is the alignment for the newly created section and
13544 IS_RELA defines whether the name should be .rela.<SEC's name>
13545 or .rel.<SEC's name>. The section name is looked up in the
13546 string table associated with ABFD. */
13549 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13551 unsigned int alignment,
13553 bfd_boolean is_rela)
13555 asection * reloc_sec = elf_section_data (sec)->sreloc;
13557 if (reloc_sec == NULL)
13559 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13564 reloc_sec = bfd_get_linker_section (dynobj, name);
13566 if (reloc_sec == NULL)
13568 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13569 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13570 if ((sec->flags & SEC_ALLOC) != 0)
13571 flags |= SEC_ALLOC | SEC_LOAD;
13573 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13574 if (reloc_sec != NULL)
13576 /* _bfd_elf_get_sec_type_attr chooses a section type by
13577 name. Override as it may be wrong, eg. for a user
13578 section named "auto" we'll get ".relauto" which is
13579 seen to be a .rela section. */
13580 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13581 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13586 elf_section_data (sec)->sreloc = reloc_sec;
13592 /* Copy the ELF symbol type and other attributes for a linker script
13593 assignment from HSRC to HDEST. Generally this should be treated as
13594 if we found a strong non-dynamic definition for HDEST (except that
13595 ld ignores multiple definition errors). */
13597 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13598 struct bfd_link_hash_entry *hdest,
13599 struct bfd_link_hash_entry *hsrc)
13601 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13602 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13603 Elf_Internal_Sym isym;
13605 ehdest->type = ehsrc->type;
13606 ehdest->target_internal = ehsrc->target_internal;
13608 isym.st_other = ehsrc->other;
13609 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
13612 /* Append a RELA relocation REL to section S in BFD. */
13615 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13617 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13618 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13619 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13620 bed->s->swap_reloca_out (abfd, rel, loc);
13623 /* Append a REL relocation REL to section S in BFD. */
13626 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13628 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13629 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13630 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13631 bed->s->swap_reloc_out (abfd, rel, loc);