1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
28 #include "safe-ctype.h"
29 #include "libiberty.h"
32 /* Define a symbol in a dynamic linkage section. */
34 struct elf_link_hash_entry *
35 _bfd_elf_define_linkage_sym (bfd *abfd,
36 struct bfd_link_info *info,
40 struct elf_link_hash_entry *h;
41 struct bfd_link_hash_entry *bh;
42 const struct elf_backend_data *bed;
44 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
47 /* Zap symbol defined in an as-needed lib that wasn't linked.
48 This is a symptom of a larger problem: Absolute symbols
49 defined in shared libraries can't be overridden, because we
50 lose the link to the bfd which is via the symbol section. */
51 h->root.type = bfd_link_hash_new;
55 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
57 get_elf_backend_data (abfd)->collect,
60 h = (struct elf_link_hash_entry *) bh;
63 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
65 bed = get_elf_backend_data (abfd);
66 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
71 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
75 struct elf_link_hash_entry *h;
76 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
79 /* This function may be called more than once. */
80 s = bfd_get_section_by_name (abfd, ".got");
81 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
84 switch (bed->s->arch_size)
95 bfd_set_error (bfd_error_bad_value);
99 flags = bed->dynamic_sec_flags;
101 s = bfd_make_section_with_flags (abfd, ".got", flags);
103 || !bfd_set_section_alignment (abfd, s, ptralign))
106 if (bed->want_got_plt)
108 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
110 || !bfd_set_section_alignment (abfd, s, ptralign))
114 if (bed->want_got_sym)
116 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
117 (or .got.plt) section. We don't do this in the linker script
118 because we don't want to define the symbol if we are not creating
119 a global offset table. */
120 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
121 elf_hash_table (info)->hgot = h;
126 /* The first bit of the global offset table is the header. */
127 s->size += bed->got_header_size;
132 /* Create a strtab to hold the dynamic symbol names. */
134 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
136 struct elf_link_hash_table *hash_table;
138 hash_table = elf_hash_table (info);
139 if (hash_table->dynobj == NULL)
140 hash_table->dynobj = abfd;
142 if (hash_table->dynstr == NULL)
144 hash_table->dynstr = _bfd_elf_strtab_init ();
145 if (hash_table->dynstr == NULL)
151 /* Create some sections which will be filled in with dynamic linking
152 information. ABFD is an input file which requires dynamic sections
153 to be created. The dynamic sections take up virtual memory space
154 when the final executable is run, so we need to create them before
155 addresses are assigned to the output sections. We work out the
156 actual contents and size of these sections later. */
159 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
162 register asection *s;
163 const struct elf_backend_data *bed;
165 if (! is_elf_hash_table (info->hash))
168 if (elf_hash_table (info)->dynamic_sections_created)
171 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
174 abfd = elf_hash_table (info)->dynobj;
175 bed = get_elf_backend_data (abfd);
177 flags = bed->dynamic_sec_flags;
179 /* A dynamically linked executable has a .interp section, but a
180 shared library does not. */
181 if (info->executable)
183 s = bfd_make_section_with_flags (abfd, ".interp",
184 flags | SEC_READONLY);
189 /* Create sections to hold version informations. These are removed
190 if they are not needed. */
191 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
192 flags | SEC_READONLY);
194 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
197 s = bfd_make_section_with_flags (abfd, ".gnu.version",
198 flags | SEC_READONLY);
200 || ! bfd_set_section_alignment (abfd, s, 1))
203 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
204 flags | SEC_READONLY);
206 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
209 s = bfd_make_section_with_flags (abfd, ".dynsym",
210 flags | SEC_READONLY);
212 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
215 s = bfd_make_section_with_flags (abfd, ".dynstr",
216 flags | SEC_READONLY);
220 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
222 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
225 /* The special symbol _DYNAMIC is always set to the start of the
226 .dynamic section. We could set _DYNAMIC in a linker script, but we
227 only want to define it if we are, in fact, creating a .dynamic
228 section. We don't want to define it if there is no .dynamic
229 section, since on some ELF platforms the start up code examines it
230 to decide how to initialize the process. */
231 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
236 s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
238 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
240 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
243 if (info->emit_gnu_hash)
245 s = bfd_make_section_with_flags (abfd, ".gnu.hash",
246 flags | SEC_READONLY);
248 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
250 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
251 4 32-bit words followed by variable count of 64-bit words, then
252 variable count of 32-bit words. */
253 if (bed->s->arch_size == 64)
254 elf_section_data (s)->this_hdr.sh_entsize = 0;
256 elf_section_data (s)->this_hdr.sh_entsize = 4;
259 /* Let the backend create the rest of the sections. This lets the
260 backend set the right flags. The backend will normally create
261 the .got and .plt sections. */
262 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
265 elf_hash_table (info)->dynamic_sections_created = TRUE;
270 /* Create dynamic sections when linking against a dynamic object. */
273 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
275 flagword flags, pltflags;
276 struct elf_link_hash_entry *h;
278 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
280 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
281 .rel[a].bss sections. */
282 flags = bed->dynamic_sec_flags;
285 if (bed->plt_not_loaded)
286 /* We do not clear SEC_ALLOC here because we still want the OS to
287 allocate space for the section; it's just that there's nothing
288 to read in from the object file. */
289 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
291 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
292 if (bed->plt_readonly)
293 pltflags |= SEC_READONLY;
295 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
297 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
300 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
302 if (bed->want_plt_sym)
304 h = _bfd_elf_define_linkage_sym (abfd, info, s,
305 "_PROCEDURE_LINKAGE_TABLE_");
306 elf_hash_table (info)->hplt = h;
311 s = bfd_make_section_with_flags (abfd,
312 (bed->rela_plts_and_copies_p
313 ? ".rela.plt" : ".rel.plt"),
314 flags | SEC_READONLY);
316 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
319 if (! _bfd_elf_create_got_section (abfd, info))
322 if (bed->want_dynbss)
324 /* The .dynbss section is a place to put symbols which are defined
325 by dynamic objects, are referenced by regular objects, and are
326 not functions. We must allocate space for them in the process
327 image and use a R_*_COPY reloc to tell the dynamic linker to
328 initialize them at run time. The linker script puts the .dynbss
329 section into the .bss section of the final image. */
330 s = bfd_make_section_with_flags (abfd, ".dynbss",
332 | SEC_LINKER_CREATED));
336 /* The .rel[a].bss section holds copy relocs. This section is not
337 normally needed. We need to create it here, though, so that the
338 linker will map it to an output section. We can't just create it
339 only if we need it, because we will not know whether we need it
340 until we have seen all the input files, and the first time the
341 main linker code calls BFD after examining all the input files
342 (size_dynamic_sections) the input sections have already been
343 mapped to the output sections. If the section turns out not to
344 be needed, we can discard it later. We will never need this
345 section when generating a shared object, since they do not use
349 s = bfd_make_section_with_flags (abfd,
350 (bed->rela_plts_and_copies_p
351 ? ".rela.bss" : ".rel.bss"),
352 flags | SEC_READONLY);
354 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
362 /* Record a new dynamic symbol. We record the dynamic symbols as we
363 read the input files, since we need to have a list of all of them
364 before we can determine the final sizes of the output sections.
365 Note that we may actually call this function even though we are not
366 going to output any dynamic symbols; in some cases we know that a
367 symbol should be in the dynamic symbol table, but only if there is
371 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
372 struct elf_link_hash_entry *h)
374 if (h->dynindx == -1)
376 struct elf_strtab_hash *dynstr;
381 /* XXX: The ABI draft says the linker must turn hidden and
382 internal symbols into STB_LOCAL symbols when producing the
383 DSO. However, if ld.so honors st_other in the dynamic table,
384 this would not be necessary. */
385 switch (ELF_ST_VISIBILITY (h->other))
389 if (h->root.type != bfd_link_hash_undefined
390 && h->root.type != bfd_link_hash_undefweak)
393 if (!elf_hash_table (info)->is_relocatable_executable)
401 h->dynindx = elf_hash_table (info)->dynsymcount;
402 ++elf_hash_table (info)->dynsymcount;
404 dynstr = elf_hash_table (info)->dynstr;
407 /* Create a strtab to hold the dynamic symbol names. */
408 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
413 /* We don't put any version information in the dynamic string
415 name = h->root.root.string;
416 p = strchr (name, ELF_VER_CHR);
418 /* We know that the p points into writable memory. In fact,
419 there are only a few symbols that have read-only names, being
420 those like _GLOBAL_OFFSET_TABLE_ that are created specially
421 by the backends. Most symbols will have names pointing into
422 an ELF string table read from a file, or to objalloc memory. */
425 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
430 if (indx == (bfd_size_type) -1)
432 h->dynstr_index = indx;
438 /* Mark a symbol dynamic. */
441 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
442 struct elf_link_hash_entry *h,
443 Elf_Internal_Sym *sym)
445 struct bfd_elf_dynamic_list *d = info->dynamic_list;
447 /* It may be called more than once on the same H. */
448 if(h->dynamic || info->relocatable)
451 if ((info->dynamic_data
452 && (h->type == STT_OBJECT
454 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
456 && h->root.type == bfd_link_hash_new
457 && (*d->match) (&d->head, NULL, h->root.root.string)))
461 /* Record an assignment to a symbol made by a linker script. We need
462 this in case some dynamic object refers to this symbol. */
465 bfd_elf_record_link_assignment (bfd *output_bfd,
466 struct bfd_link_info *info,
471 struct elf_link_hash_entry *h, *hv;
472 struct elf_link_hash_table *htab;
473 const struct elf_backend_data *bed;
475 if (!is_elf_hash_table (info->hash))
478 htab = elf_hash_table (info);
479 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
483 switch (h->root.type)
485 case bfd_link_hash_defined:
486 case bfd_link_hash_defweak:
487 case bfd_link_hash_common:
489 case bfd_link_hash_undefweak:
490 case bfd_link_hash_undefined:
491 /* Since we're defining the symbol, don't let it seem to have not
492 been defined. record_dynamic_symbol and size_dynamic_sections
493 may depend on this. */
494 h->root.type = bfd_link_hash_new;
495 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
496 bfd_link_repair_undef_list (&htab->root);
498 case bfd_link_hash_new:
499 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
502 case bfd_link_hash_indirect:
503 /* We had a versioned symbol in a dynamic library. We make the
504 the versioned symbol point to this one. */
505 bed = get_elf_backend_data (output_bfd);
507 while (hv->root.type == bfd_link_hash_indirect
508 || hv->root.type == bfd_link_hash_warning)
509 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
510 /* We don't need to update h->root.u since linker will set them
512 h->root.type = bfd_link_hash_undefined;
513 hv->root.type = bfd_link_hash_indirect;
514 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
515 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
517 case bfd_link_hash_warning:
522 /* If this symbol is being provided by the linker script, and it is
523 currently defined by a dynamic object, but not by a regular
524 object, then mark it as undefined so that the generic linker will
525 force the correct value. */
529 h->root.type = bfd_link_hash_undefined;
531 /* If this symbol is not being provided by the linker script, and it is
532 currently defined by a dynamic object, but not by a regular object,
533 then clear out any version information because the symbol will not be
534 associated with the dynamic object any more. */
538 h->verinfo.verdef = NULL;
542 if (provide && hidden)
544 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
546 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
547 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
550 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
552 if (!info->relocatable
554 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
555 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
561 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
564 if (! bfd_elf_link_record_dynamic_symbol (info, h))
567 /* If this is a weak defined symbol, and we know a corresponding
568 real symbol from the same dynamic object, make sure the real
569 symbol is also made into a dynamic symbol. */
570 if (h->u.weakdef != NULL
571 && h->u.weakdef->dynindx == -1)
573 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
581 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
582 success, and 2 on a failure caused by attempting to record a symbol
583 in a discarded section, eg. a discarded link-once section symbol. */
586 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
591 struct elf_link_local_dynamic_entry *entry;
592 struct elf_link_hash_table *eht;
593 struct elf_strtab_hash *dynstr;
594 unsigned long dynstr_index;
596 Elf_External_Sym_Shndx eshndx;
597 char esym[sizeof (Elf64_External_Sym)];
599 if (! is_elf_hash_table (info->hash))
602 /* See if the entry exists already. */
603 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
604 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
607 amt = sizeof (*entry);
608 entry = bfd_alloc (input_bfd, amt);
612 /* Go find the symbol, so that we can find it's name. */
613 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
614 1, input_indx, &entry->isym, esym, &eshndx))
616 bfd_release (input_bfd, entry);
620 if (entry->isym.st_shndx != SHN_UNDEF
621 && entry->isym.st_shndx < SHN_LORESERVE)
625 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
626 if (s == NULL || bfd_is_abs_section (s->output_section))
628 /* We can still bfd_release here as nothing has done another
629 bfd_alloc. We can't do this later in this function. */
630 bfd_release (input_bfd, entry);
635 name = (bfd_elf_string_from_elf_section
636 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
637 entry->isym.st_name));
639 dynstr = elf_hash_table (info)->dynstr;
642 /* Create a strtab to hold the dynamic symbol names. */
643 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
648 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
649 if (dynstr_index == (unsigned long) -1)
651 entry->isym.st_name = dynstr_index;
653 eht = elf_hash_table (info);
655 entry->next = eht->dynlocal;
656 eht->dynlocal = entry;
657 entry->input_bfd = input_bfd;
658 entry->input_indx = input_indx;
661 /* Whatever binding the symbol had before, it's now local. */
663 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
665 /* The dynindx will be set at the end of size_dynamic_sections. */
670 /* Return the dynindex of a local dynamic symbol. */
673 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
677 struct elf_link_local_dynamic_entry *e;
679 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
680 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
685 /* This function is used to renumber the dynamic symbols, if some of
686 them are removed because they are marked as local. This is called
687 via elf_link_hash_traverse. */
690 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
693 size_t *count = data;
695 if (h->root.type == bfd_link_hash_warning)
696 h = (struct elf_link_hash_entry *) h->root.u.i.link;
701 if (h->dynindx != -1)
702 h->dynindx = ++(*count);
708 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
709 STB_LOCAL binding. */
712 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
715 size_t *count = data;
717 if (h->root.type == bfd_link_hash_warning)
718 h = (struct elf_link_hash_entry *) h->root.u.i.link;
720 if (!h->forced_local)
723 if (h->dynindx != -1)
724 h->dynindx = ++(*count);
729 /* Return true if the dynamic symbol for a given section should be
730 omitted when creating a shared library. */
732 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
733 struct bfd_link_info *info,
736 struct elf_link_hash_table *htab;
738 switch (elf_section_data (p)->this_hdr.sh_type)
742 /* If sh_type is yet undecided, assume it could be
743 SHT_PROGBITS/SHT_NOBITS. */
745 htab = elf_hash_table (info);
746 if (p == htab->tls_sec)
749 if (htab->text_index_section != NULL)
750 return p != htab->text_index_section && p != htab->data_index_section;
752 if (strcmp (p->name, ".got") == 0
753 || strcmp (p->name, ".got.plt") == 0
754 || strcmp (p->name, ".plt") == 0)
758 if (htab->dynobj != NULL
759 && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
760 && (ip->flags & SEC_LINKER_CREATED)
761 && ip->output_section == p)
766 /* There shouldn't be section relative relocations
767 against any other section. */
773 /* Assign dynsym indices. In a shared library we generate a section
774 symbol for each output section, which come first. Next come symbols
775 which have been forced to local binding. Then all of the back-end
776 allocated local dynamic syms, followed by the rest of the global
780 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
781 struct bfd_link_info *info,
782 unsigned long *section_sym_count)
784 unsigned long dynsymcount = 0;
786 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
788 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
790 for (p = output_bfd->sections; p ; p = p->next)
791 if ((p->flags & SEC_EXCLUDE) == 0
792 && (p->flags & SEC_ALLOC) != 0
793 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
794 elf_section_data (p)->dynindx = ++dynsymcount;
796 elf_section_data (p)->dynindx = 0;
798 *section_sym_count = dynsymcount;
800 elf_link_hash_traverse (elf_hash_table (info),
801 elf_link_renumber_local_hash_table_dynsyms,
804 if (elf_hash_table (info)->dynlocal)
806 struct elf_link_local_dynamic_entry *p;
807 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
808 p->dynindx = ++dynsymcount;
811 elf_link_hash_traverse (elf_hash_table (info),
812 elf_link_renumber_hash_table_dynsyms,
815 /* There is an unused NULL entry at the head of the table which
816 we must account for in our count. Unless there weren't any
817 symbols, which means we'll have no table at all. */
818 if (dynsymcount != 0)
821 elf_hash_table (info)->dynsymcount = dynsymcount;
825 /* Merge st_other field. */
828 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
829 Elf_Internal_Sym *isym, bfd_boolean definition,
832 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
834 /* If st_other has a processor-specific meaning, specific
835 code might be needed here. We never merge the visibility
836 attribute with the one from a dynamic object. */
837 if (bed->elf_backend_merge_symbol_attribute)
838 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
841 /* If this symbol has default visibility and the user has requested
842 we not re-export it, then mark it as hidden. */
846 || (abfd->my_archive && abfd->my_archive->no_export))
847 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
848 isym->st_other = (STV_HIDDEN
849 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
851 if (!dynamic && ELF_ST_VISIBILITY (isym->st_other) != 0)
853 unsigned char hvis, symvis, other, nvis;
855 /* Only merge the visibility. Leave the remainder of the
856 st_other field to elf_backend_merge_symbol_attribute. */
857 other = h->other & ~ELF_ST_VISIBILITY (-1);
859 /* Combine visibilities, using the most constraining one. */
860 hvis = ELF_ST_VISIBILITY (h->other);
861 symvis = ELF_ST_VISIBILITY (isym->st_other);
867 nvis = hvis < symvis ? hvis : symvis;
869 h->other = other | nvis;
873 /* This function is called when we want to define a new symbol. It
874 handles the various cases which arise when we find a definition in
875 a dynamic object, or when there is already a definition in a
876 dynamic object. The new symbol is described by NAME, SYM, PSEC,
877 and PVALUE. We set SYM_HASH to the hash table entry. We set
878 OVERRIDE if the old symbol is overriding a new definition. We set
879 TYPE_CHANGE_OK if it is OK for the type to change. We set
880 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
881 change, we mean that we shouldn't warn if the type or size does
882 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
883 object is overridden by a regular object. */
886 _bfd_elf_merge_symbol (bfd *abfd,
887 struct bfd_link_info *info,
889 Elf_Internal_Sym *sym,
892 unsigned int *pold_alignment,
893 struct elf_link_hash_entry **sym_hash,
895 bfd_boolean *override,
896 bfd_boolean *type_change_ok,
897 bfd_boolean *size_change_ok)
899 asection *sec, *oldsec;
900 struct elf_link_hash_entry *h;
901 struct elf_link_hash_entry *flip;
904 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
905 bfd_boolean newweak, oldweak, newfunc, oldfunc;
906 const struct elf_backend_data *bed;
912 bind = ELF_ST_BIND (sym->st_info);
914 /* Silently discard TLS symbols from --just-syms. There's no way to
915 combine a static TLS block with a new TLS block for this executable. */
916 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
917 && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
923 if (! bfd_is_und_section (sec))
924 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
926 h = ((struct elf_link_hash_entry *)
927 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
932 bed = get_elf_backend_data (abfd);
934 /* This code is for coping with dynamic objects, and is only useful
935 if we are doing an ELF link. */
936 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
939 /* For merging, we only care about real symbols. */
941 while (h->root.type == bfd_link_hash_indirect
942 || h->root.type == bfd_link_hash_warning)
943 h = (struct elf_link_hash_entry *) h->root.u.i.link;
945 /* We have to check it for every instance since the first few may be
946 refereences and not all compilers emit symbol type for undefined
948 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
950 /* If we just created the symbol, mark it as being an ELF symbol.
951 Other than that, there is nothing to do--there is no merge issue
952 with a newly defined symbol--so we just return. */
954 if (h->root.type == bfd_link_hash_new)
960 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
963 switch (h->root.type)
970 case bfd_link_hash_undefined:
971 case bfd_link_hash_undefweak:
972 oldbfd = h->root.u.undef.abfd;
976 case bfd_link_hash_defined:
977 case bfd_link_hash_defweak:
978 oldbfd = h->root.u.def.section->owner;
979 oldsec = h->root.u.def.section;
982 case bfd_link_hash_common:
983 oldbfd = h->root.u.c.p->section->owner;
984 oldsec = h->root.u.c.p->section;
988 /* In cases involving weak versioned symbols, we may wind up trying
989 to merge a symbol with itself. Catch that here, to avoid the
990 confusion that results if we try to override a symbol with
991 itself. The additional tests catch cases like
992 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
993 dynamic object, which we do want to handle here. */
995 && ((abfd->flags & DYNAMIC) == 0
999 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1000 respectively, is from a dynamic object. */
1002 newdyn = (abfd->flags & DYNAMIC) != 0;
1006 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1007 else if (oldsec != NULL)
1009 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1010 indices used by MIPS ELF. */
1011 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1014 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1015 respectively, appear to be a definition rather than reference. */
1017 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1019 olddef = (h->root.type != bfd_link_hash_undefined
1020 && h->root.type != bfd_link_hash_undefweak
1021 && h->root.type != bfd_link_hash_common);
1023 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1024 respectively, appear to be a function. */
1026 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1027 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1029 oldfunc = (h->type != STT_NOTYPE
1030 && bed->is_function_type (h->type));
1032 /* When we try to create a default indirect symbol from the dynamic
1033 definition with the default version, we skip it if its type and
1034 the type of existing regular definition mismatch. We only do it
1035 if the existing regular definition won't be dynamic. */
1036 if (pold_alignment == NULL
1038 && !info->export_dynamic
1043 && (olddef || h->root.type == bfd_link_hash_common)
1044 && ELF_ST_TYPE (sym->st_info) != h->type
1045 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1046 && h->type != STT_NOTYPE
1047 && !(newfunc && oldfunc))
1053 /* Check TLS symbol. We don't check undefined symbol introduced by
1055 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
1056 && ELF_ST_TYPE (sym->st_info) != h->type
1060 bfd_boolean ntdef, tdef;
1061 asection *ntsec, *tsec;
1063 if (h->type == STT_TLS)
1083 (*_bfd_error_handler)
1084 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1085 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1086 else if (!tdef && !ntdef)
1087 (*_bfd_error_handler)
1088 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1089 tbfd, ntbfd, h->root.root.string);
1091 (*_bfd_error_handler)
1092 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1093 tbfd, tsec, ntbfd, h->root.root.string);
1095 (*_bfd_error_handler)
1096 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1097 tbfd, ntbfd, ntsec, h->root.root.string);
1099 bfd_set_error (bfd_error_bad_value);
1103 /* We need to remember if a symbol has a definition in a dynamic
1104 object or is weak in all dynamic objects. Internal and hidden
1105 visibility will make it unavailable to dynamic objects. */
1106 if (newdyn && !h->dynamic_def)
1108 if (!bfd_is_und_section (sec))
1112 /* Check if this symbol is weak in all dynamic objects. If it
1113 is the first time we see it in a dynamic object, we mark
1114 if it is weak. Otherwise, we clear it. */
1115 if (!h->ref_dynamic)
1117 if (bind == STB_WEAK)
1118 h->dynamic_weak = 1;
1120 else if (bind != STB_WEAK)
1121 h->dynamic_weak = 0;
1125 /* If the old symbol has non-default visibility, we ignore the new
1126 definition from a dynamic object. */
1128 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1129 && !bfd_is_und_section (sec))
1132 /* Make sure this symbol is dynamic. */
1134 /* A protected symbol has external availability. Make sure it is
1135 recorded as dynamic.
1137 FIXME: Should we check type and size for protected symbol? */
1138 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1139 return bfd_elf_link_record_dynamic_symbol (info, h);
1144 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1147 /* If the new symbol with non-default visibility comes from a
1148 relocatable file and the old definition comes from a dynamic
1149 object, we remove the old definition. */
1150 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1152 /* Handle the case where the old dynamic definition is
1153 default versioned. We need to copy the symbol info from
1154 the symbol with default version to the normal one if it
1155 was referenced before. */
1158 const struct elf_backend_data *bed
1159 = get_elf_backend_data (abfd);
1160 struct elf_link_hash_entry *vh = *sym_hash;
1161 vh->root.type = h->root.type;
1162 h->root.type = bfd_link_hash_indirect;
1163 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1164 /* Protected symbols will override the dynamic definition
1165 with default version. */
1166 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1168 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1169 vh->dynamic_def = 1;
1170 vh->ref_dynamic = 1;
1174 h->root.type = vh->root.type;
1175 vh->ref_dynamic = 0;
1176 /* We have to hide it here since it was made dynamic
1177 global with extra bits when the symbol info was
1178 copied from the old dynamic definition. */
1179 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1187 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1188 && bfd_is_und_section (sec))
1190 /* If the new symbol is undefined and the old symbol was
1191 also undefined before, we need to make sure
1192 _bfd_generic_link_add_one_symbol doesn't mess
1193 up the linker hash table undefs list. Since the old
1194 definition came from a dynamic object, it is still on the
1196 h->root.type = bfd_link_hash_undefined;
1197 h->root.u.undef.abfd = abfd;
1201 h->root.type = bfd_link_hash_new;
1202 h->root.u.undef.abfd = NULL;
1211 /* FIXME: Should we check type and size for protected symbol? */
1217 /* Differentiate strong and weak symbols. */
1218 newweak = bind == STB_WEAK;
1219 oldweak = (h->root.type == bfd_link_hash_defweak
1220 || h->root.type == bfd_link_hash_undefweak);
1222 /* If a new weak symbol definition comes from a regular file and the
1223 old symbol comes from a dynamic library, we treat the new one as
1224 strong. Similarly, an old weak symbol definition from a regular
1225 file is treated as strong when the new symbol comes from a dynamic
1226 library. Further, an old weak symbol from a dynamic library is
1227 treated as strong if the new symbol is from a dynamic library.
1228 This reflects the way glibc's ld.so works.
1230 Do this before setting *type_change_ok or *size_change_ok so that
1231 we warn properly when dynamic library symbols are overridden. */
1233 if (newdef && !newdyn && olddyn)
1235 if (olddef && newdyn)
1238 /* Allow changes between different types of funciton symbol. */
1239 if (newfunc && oldfunc)
1240 *type_change_ok = TRUE;
1242 /* It's OK to change the type if either the existing symbol or the
1243 new symbol is weak. A type change is also OK if the old symbol
1244 is undefined and the new symbol is defined. */
1249 && h->root.type == bfd_link_hash_undefined))
1250 *type_change_ok = TRUE;
1252 /* It's OK to change the size if either the existing symbol or the
1253 new symbol is weak, or if the old symbol is undefined. */
1256 || h->root.type == bfd_link_hash_undefined)
1257 *size_change_ok = TRUE;
1259 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1260 symbol, respectively, appears to be a common symbol in a dynamic
1261 object. If a symbol appears in an uninitialized section, and is
1262 not weak, and is not a function, then it may be a common symbol
1263 which was resolved when the dynamic object was created. We want
1264 to treat such symbols specially, because they raise special
1265 considerations when setting the symbol size: if the symbol
1266 appears as a common symbol in a regular object, and the size in
1267 the regular object is larger, we must make sure that we use the
1268 larger size. This problematic case can always be avoided in C,
1269 but it must be handled correctly when using Fortran shared
1272 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1273 likewise for OLDDYNCOMMON and OLDDEF.
1275 Note that this test is just a heuristic, and that it is quite
1276 possible to have an uninitialized symbol in a shared object which
1277 is really a definition, rather than a common symbol. This could
1278 lead to some minor confusion when the symbol really is a common
1279 symbol in some regular object. However, I think it will be
1285 && (sec->flags & SEC_ALLOC) != 0
1286 && (sec->flags & SEC_LOAD) == 0
1289 newdyncommon = TRUE;
1291 newdyncommon = FALSE;
1295 && h->root.type == bfd_link_hash_defined
1297 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1298 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1301 olddyncommon = TRUE;
1303 olddyncommon = FALSE;
1305 /* We now know everything about the old and new symbols. We ask the
1306 backend to check if we can merge them. */
1307 if (bed->merge_symbol
1308 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1309 pold_alignment, skip, override,
1310 type_change_ok, size_change_ok,
1311 &newdyn, &newdef, &newdyncommon, &newweak,
1313 &olddyn, &olddef, &olddyncommon, &oldweak,
1317 /* If both the old and the new symbols look like common symbols in a
1318 dynamic object, set the size of the symbol to the larger of the
1323 && sym->st_size != h->size)
1325 /* Since we think we have two common symbols, issue a multiple
1326 common warning if desired. Note that we only warn if the
1327 size is different. If the size is the same, we simply let
1328 the old symbol override the new one as normally happens with
1329 symbols defined in dynamic objects. */
1331 if (! ((*info->callbacks->multiple_common)
1332 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1333 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1336 if (sym->st_size > h->size)
1337 h->size = sym->st_size;
1339 *size_change_ok = TRUE;
1342 /* If we are looking at a dynamic object, and we have found a
1343 definition, we need to see if the symbol was already defined by
1344 some other object. If so, we want to use the existing
1345 definition, and we do not want to report a multiple symbol
1346 definition error; we do this by clobbering *PSEC to be
1347 bfd_und_section_ptr.
1349 We treat a common symbol as a definition if the symbol in the
1350 shared library is a function, since common symbols always
1351 represent variables; this can cause confusion in principle, but
1352 any such confusion would seem to indicate an erroneous program or
1353 shared library. We also permit a common symbol in a regular
1354 object to override a weak symbol in a shared object. */
1359 || (h->root.type == bfd_link_hash_common
1360 && (newweak || newfunc))))
1364 newdyncommon = FALSE;
1366 *psec = sec = bfd_und_section_ptr;
1367 *size_change_ok = TRUE;
1369 /* If we get here when the old symbol is a common symbol, then
1370 we are explicitly letting it override a weak symbol or
1371 function in a dynamic object, and we don't want to warn about
1372 a type change. If the old symbol is a defined symbol, a type
1373 change warning may still be appropriate. */
1375 if (h->root.type == bfd_link_hash_common)
1376 *type_change_ok = TRUE;
1379 /* Handle the special case of an old common symbol merging with a
1380 new symbol which looks like a common symbol in a shared object.
1381 We change *PSEC and *PVALUE to make the new symbol look like a
1382 common symbol, and let _bfd_generic_link_add_one_symbol do the
1386 && h->root.type == bfd_link_hash_common)
1390 newdyncommon = FALSE;
1391 *pvalue = sym->st_size;
1392 *psec = sec = bed->common_section (oldsec);
1393 *size_change_ok = TRUE;
1396 /* Skip weak definitions of symbols that are already defined. */
1397 if (newdef && olddef && newweak)
1401 /* Merge st_other. If the symbol already has a dynamic index,
1402 but visibility says it should not be visible, turn it into a
1404 elf_merge_st_other (abfd, h, sym, newdef, newdyn);
1405 if (h->dynindx != -1)
1406 switch (ELF_ST_VISIBILITY (h->other))
1410 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1415 /* If the old symbol is from a dynamic object, and the new symbol is
1416 a definition which is not from a dynamic object, then the new
1417 symbol overrides the old symbol. Symbols from regular files
1418 always take precedence over symbols from dynamic objects, even if
1419 they are defined after the dynamic object in the link.
1421 As above, we again permit a common symbol in a regular object to
1422 override a definition in a shared object if the shared object
1423 symbol is a function or is weak. */
1428 || (bfd_is_com_section (sec)
1429 && (oldweak || oldfunc)))
1434 /* Change the hash table entry to undefined, and let
1435 _bfd_generic_link_add_one_symbol do the right thing with the
1438 h->root.type = bfd_link_hash_undefined;
1439 h->root.u.undef.abfd = h->root.u.def.section->owner;
1440 *size_change_ok = TRUE;
1443 olddyncommon = FALSE;
1445 /* We again permit a type change when a common symbol may be
1446 overriding a function. */
1448 if (bfd_is_com_section (sec))
1452 /* If a common symbol overrides a function, make sure
1453 that it isn't defined dynamically nor has type
1456 h->type = STT_NOTYPE;
1458 *type_change_ok = TRUE;
1461 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1464 /* This union may have been set to be non-NULL when this symbol
1465 was seen in a dynamic object. We must force the union to be
1466 NULL, so that it is correct for a regular symbol. */
1467 h->verinfo.vertree = NULL;
1470 /* Handle the special case of a new common symbol merging with an
1471 old symbol that looks like it might be a common symbol defined in
1472 a shared object. Note that we have already handled the case in
1473 which a new common symbol should simply override the definition
1474 in the shared library. */
1477 && bfd_is_com_section (sec)
1480 /* It would be best if we could set the hash table entry to a
1481 common symbol, but we don't know what to use for the section
1482 or the alignment. */
1483 if (! ((*info->callbacks->multiple_common)
1484 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1485 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1488 /* If the presumed common symbol in the dynamic object is
1489 larger, pretend that the new symbol has its size. */
1491 if (h->size > *pvalue)
1494 /* We need to remember the alignment required by the symbol
1495 in the dynamic object. */
1496 BFD_ASSERT (pold_alignment);
1497 *pold_alignment = h->root.u.def.section->alignment_power;
1500 olddyncommon = FALSE;
1502 h->root.type = bfd_link_hash_undefined;
1503 h->root.u.undef.abfd = h->root.u.def.section->owner;
1505 *size_change_ok = TRUE;
1506 *type_change_ok = TRUE;
1508 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1511 h->verinfo.vertree = NULL;
1516 /* Handle the case where we had a versioned symbol in a dynamic
1517 library and now find a definition in a normal object. In this
1518 case, we make the versioned symbol point to the normal one. */
1519 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1520 flip->root.type = h->root.type;
1521 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1522 h->root.type = bfd_link_hash_indirect;
1523 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1524 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1528 flip->ref_dynamic = 1;
1535 /* This function is called to create an indirect symbol from the
1536 default for the symbol with the default version if needed. The
1537 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1538 set DYNSYM if the new indirect symbol is dynamic. */
1541 _bfd_elf_add_default_symbol (bfd *abfd,
1542 struct bfd_link_info *info,
1543 struct elf_link_hash_entry *h,
1545 Elf_Internal_Sym *sym,
1548 bfd_boolean *dynsym,
1549 bfd_boolean override)
1551 bfd_boolean type_change_ok;
1552 bfd_boolean size_change_ok;
1555 struct elf_link_hash_entry *hi;
1556 struct bfd_link_hash_entry *bh;
1557 const struct elf_backend_data *bed;
1558 bfd_boolean collect;
1559 bfd_boolean dynamic;
1561 size_t len, shortlen;
1564 /* If this symbol has a version, and it is the default version, we
1565 create an indirect symbol from the default name to the fully
1566 decorated name. This will cause external references which do not
1567 specify a version to be bound to this version of the symbol. */
1568 p = strchr (name, ELF_VER_CHR);
1569 if (p == NULL || p[1] != ELF_VER_CHR)
1574 /* We are overridden by an old definition. We need to check if we
1575 need to create the indirect symbol from the default name. */
1576 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1578 BFD_ASSERT (hi != NULL);
1581 while (hi->root.type == bfd_link_hash_indirect
1582 || hi->root.type == bfd_link_hash_warning)
1584 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1590 bed = get_elf_backend_data (abfd);
1591 collect = bed->collect;
1592 dynamic = (abfd->flags & DYNAMIC) != 0;
1594 shortlen = p - name;
1595 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1596 if (shortname == NULL)
1598 memcpy (shortname, name, shortlen);
1599 shortname[shortlen] = '\0';
1601 /* We are going to create a new symbol. Merge it with any existing
1602 symbol with this name. For the purposes of the merge, act as
1603 though we were defining the symbol we just defined, although we
1604 actually going to define an indirect symbol. */
1605 type_change_ok = FALSE;
1606 size_change_ok = FALSE;
1608 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1609 NULL, &hi, &skip, &override,
1610 &type_change_ok, &size_change_ok))
1619 if (! (_bfd_generic_link_add_one_symbol
1620 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1621 0, name, FALSE, collect, &bh)))
1623 hi = (struct elf_link_hash_entry *) bh;
1627 /* In this case the symbol named SHORTNAME is overriding the
1628 indirect symbol we want to add. We were planning on making
1629 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1630 is the name without a version. NAME is the fully versioned
1631 name, and it is the default version.
1633 Overriding means that we already saw a definition for the
1634 symbol SHORTNAME in a regular object, and it is overriding
1635 the symbol defined in the dynamic object.
1637 When this happens, we actually want to change NAME, the
1638 symbol we just added, to refer to SHORTNAME. This will cause
1639 references to NAME in the shared object to become references
1640 to SHORTNAME in the regular object. This is what we expect
1641 when we override a function in a shared object: that the
1642 references in the shared object will be mapped to the
1643 definition in the regular object. */
1645 while (hi->root.type == bfd_link_hash_indirect
1646 || hi->root.type == bfd_link_hash_warning)
1647 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1649 h->root.type = bfd_link_hash_indirect;
1650 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1654 hi->ref_dynamic = 1;
1658 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1663 /* Now set HI to H, so that the following code will set the
1664 other fields correctly. */
1668 /* Check if HI is a warning symbol. */
1669 if (hi->root.type == bfd_link_hash_warning)
1670 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1672 /* If there is a duplicate definition somewhere, then HI may not
1673 point to an indirect symbol. We will have reported an error to
1674 the user in that case. */
1676 if (hi->root.type == bfd_link_hash_indirect)
1678 struct elf_link_hash_entry *ht;
1680 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1681 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1683 /* See if the new flags lead us to realize that the symbol must
1695 if (hi->ref_regular)
1701 /* We also need to define an indirection from the nondefault version
1705 len = strlen (name);
1706 shortname = bfd_hash_allocate (&info->hash->table, len);
1707 if (shortname == NULL)
1709 memcpy (shortname, name, shortlen);
1710 memcpy (shortname + shortlen, p + 1, len - shortlen);
1712 /* Once again, merge with any existing symbol. */
1713 type_change_ok = FALSE;
1714 size_change_ok = FALSE;
1716 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1717 NULL, &hi, &skip, &override,
1718 &type_change_ok, &size_change_ok))
1726 /* Here SHORTNAME is a versioned name, so we don't expect to see
1727 the type of override we do in the case above unless it is
1728 overridden by a versioned definition. */
1729 if (hi->root.type != bfd_link_hash_defined
1730 && hi->root.type != bfd_link_hash_defweak)
1731 (*_bfd_error_handler)
1732 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1738 if (! (_bfd_generic_link_add_one_symbol
1739 (info, abfd, shortname, BSF_INDIRECT,
1740 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1742 hi = (struct elf_link_hash_entry *) bh;
1744 /* If there is a duplicate definition somewhere, then HI may not
1745 point to an indirect symbol. We will have reported an error
1746 to the user in that case. */
1748 if (hi->root.type == bfd_link_hash_indirect)
1750 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1752 /* See if the new flags lead us to realize that the symbol
1764 if (hi->ref_regular)
1774 /* This routine is used to export all defined symbols into the dynamic
1775 symbol table. It is called via elf_link_hash_traverse. */
1778 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1780 struct elf_info_failed *eif = data;
1782 /* Ignore this if we won't export it. */
1783 if (!eif->info->export_dynamic && !h->dynamic)
1786 /* Ignore indirect symbols. These are added by the versioning code. */
1787 if (h->root.type == bfd_link_hash_indirect)
1790 if (h->root.type == bfd_link_hash_warning)
1791 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1793 if (h->dynindx == -1
1797 struct bfd_elf_version_tree *t;
1798 struct bfd_elf_version_expr *d;
1800 for (t = eif->verdefs; t != NULL; t = t->next)
1802 if (t->globals.list != NULL)
1804 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1809 if (t->locals.list != NULL)
1811 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1820 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1831 /* Look through the symbols which are defined in other shared
1832 libraries and referenced here. Update the list of version
1833 dependencies. This will be put into the .gnu.version_r section.
1834 This function is called via elf_link_hash_traverse. */
1837 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1840 struct elf_find_verdep_info *rinfo = data;
1841 Elf_Internal_Verneed *t;
1842 Elf_Internal_Vernaux *a;
1845 if (h->root.type == bfd_link_hash_warning)
1846 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1848 /* We only care about symbols defined in shared objects with version
1853 || h->verinfo.verdef == NULL)
1856 /* See if we already know about this version. */
1857 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1859 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1862 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1863 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1869 /* This is a new version. Add it to tree we are building. */
1874 t = bfd_zalloc (rinfo->output_bfd, amt);
1877 rinfo->failed = TRUE;
1881 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1882 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1883 elf_tdata (rinfo->output_bfd)->verref = t;
1887 a = bfd_zalloc (rinfo->output_bfd, amt);
1890 rinfo->failed = TRUE;
1894 /* Note that we are copying a string pointer here, and testing it
1895 above. If bfd_elf_string_from_elf_section is ever changed to
1896 discard the string data when low in memory, this will have to be
1898 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1900 a->vna_flags = h->verinfo.verdef->vd_flags;
1901 a->vna_nextptr = t->vn_auxptr;
1903 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1906 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1913 /* Figure out appropriate versions for all the symbols. We may not
1914 have the version number script until we have read all of the input
1915 files, so until that point we don't know which symbols should be
1916 local. This function is called via elf_link_hash_traverse. */
1919 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1921 struct elf_assign_sym_version_info *sinfo;
1922 struct bfd_link_info *info;
1923 const struct elf_backend_data *bed;
1924 struct elf_info_failed eif;
1931 if (h->root.type == bfd_link_hash_warning)
1932 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1934 /* Fix the symbol flags. */
1937 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1940 sinfo->failed = TRUE;
1944 /* We only need version numbers for symbols defined in regular
1946 if (!h->def_regular)
1949 bed = get_elf_backend_data (sinfo->output_bfd);
1950 p = strchr (h->root.root.string, ELF_VER_CHR);
1951 if (p != NULL && h->verinfo.vertree == NULL)
1953 struct bfd_elf_version_tree *t;
1958 /* There are two consecutive ELF_VER_CHR characters if this is
1959 not a hidden symbol. */
1961 if (*p == ELF_VER_CHR)
1967 /* If there is no version string, we can just return out. */
1975 /* Look for the version. If we find it, it is no longer weak. */
1976 for (t = sinfo->verdefs; t != NULL; t = t->next)
1978 if (strcmp (t->name, p) == 0)
1982 struct bfd_elf_version_expr *d;
1984 len = p - h->root.root.string;
1985 alc = bfd_malloc (len);
1988 sinfo->failed = TRUE;
1991 memcpy (alc, h->root.root.string, len - 1);
1992 alc[len - 1] = '\0';
1993 if (alc[len - 2] == ELF_VER_CHR)
1994 alc[len - 2] = '\0';
1996 h->verinfo.vertree = t;
2000 if (t->globals.list != NULL)
2001 d = (*t->match) (&t->globals, NULL, alc);
2003 /* See if there is anything to force this symbol to
2005 if (d == NULL && t->locals.list != NULL)
2007 d = (*t->match) (&t->locals, NULL, alc);
2010 && ! info->export_dynamic)
2011 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2019 /* If we are building an application, we need to create a
2020 version node for this version. */
2021 if (t == NULL && info->executable)
2023 struct bfd_elf_version_tree **pp;
2026 /* If we aren't going to export this symbol, we don't need
2027 to worry about it. */
2028 if (h->dynindx == -1)
2032 t = bfd_zalloc (sinfo->output_bfd, amt);
2035 sinfo->failed = TRUE;
2040 t->name_indx = (unsigned int) -1;
2044 /* Don't count anonymous version tag. */
2045 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
2047 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
2049 t->vernum = version_index;
2053 h->verinfo.vertree = t;
2057 /* We could not find the version for a symbol when
2058 generating a shared archive. Return an error. */
2059 (*_bfd_error_handler)
2060 (_("%B: version node not found for symbol %s"),
2061 sinfo->output_bfd, h->root.root.string);
2062 bfd_set_error (bfd_error_bad_value);
2063 sinfo->failed = TRUE;
2071 /* If we don't have a version for this symbol, see if we can find
2073 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
2075 struct bfd_elf_version_tree *t;
2076 struct bfd_elf_version_tree *local_ver, *global_ver, *exist_ver;
2077 struct bfd_elf_version_expr *d;
2079 /* See if can find what version this symbol is in. If the
2080 symbol is supposed to be local, then don't actually register
2085 for (t = sinfo->verdefs; t != NULL; t = t->next)
2087 if (t->globals.list != NULL)
2090 while ((d = (*t->match) (&t->globals, d,
2091 h->root.root.string)) != NULL)
2098 /* If the match is a wildcard pattern, keep looking for
2099 a more explicit, perhaps even local, match. */
2108 if (t->locals.list != NULL)
2111 while ((d = (*t->match) (&t->locals, d,
2112 h->root.root.string)) != NULL)
2115 /* If the match is a wildcard pattern, keep looking for
2116 a more explicit, perhaps even global, match. */
2119 /* An exact match overrides a global wildcard. */
2130 if (global_ver != NULL)
2132 h->verinfo.vertree = global_ver;
2133 /* If we already have a versioned symbol that matches the
2134 node for this symbol, then we don't want to create a
2135 duplicate from the unversioned symbol. Instead hide the
2136 unversioned symbol. */
2137 if (exist_ver == global_ver)
2138 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2140 else if (local_ver != NULL)
2142 h->verinfo.vertree = local_ver;
2143 if (!info->export_dynamic
2144 || exist_ver == local_ver)
2145 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2152 /* Read and swap the relocs from the section indicated by SHDR. This
2153 may be either a REL or a RELA section. The relocations are
2154 translated into RELA relocations and stored in INTERNAL_RELOCS,
2155 which should have already been allocated to contain enough space.
2156 The EXTERNAL_RELOCS are a buffer where the external form of the
2157 relocations should be stored.
2159 Returns FALSE if something goes wrong. */
2162 elf_link_read_relocs_from_section (bfd *abfd,
2164 Elf_Internal_Shdr *shdr,
2165 void *external_relocs,
2166 Elf_Internal_Rela *internal_relocs)
2168 const struct elf_backend_data *bed;
2169 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2170 const bfd_byte *erela;
2171 const bfd_byte *erelaend;
2172 Elf_Internal_Rela *irela;
2173 Elf_Internal_Shdr *symtab_hdr;
2176 /* Position ourselves at the start of the section. */
2177 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2180 /* Read the relocations. */
2181 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2184 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2185 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
2187 bed = get_elf_backend_data (abfd);
2189 /* Convert the external relocations to the internal format. */
2190 if (shdr->sh_entsize == bed->s->sizeof_rel)
2191 swap_in = bed->s->swap_reloc_in;
2192 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2193 swap_in = bed->s->swap_reloca_in;
2196 bfd_set_error (bfd_error_wrong_format);
2200 erela = external_relocs;
2201 erelaend = erela + shdr->sh_size;
2202 irela = internal_relocs;
2203 while (erela < erelaend)
2207 (*swap_in) (abfd, erela, irela);
2208 r_symndx = ELF32_R_SYM (irela->r_info);
2209 if (bed->s->arch_size == 64)
2211 if ((size_t) r_symndx >= nsyms)
2213 (*_bfd_error_handler)
2214 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2215 " for offset 0x%lx in section `%A'"),
2217 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2218 bfd_set_error (bfd_error_bad_value);
2221 irela += bed->s->int_rels_per_ext_rel;
2222 erela += shdr->sh_entsize;
2228 /* Read and swap the relocs for a section O. They may have been
2229 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2230 not NULL, they are used as buffers to read into. They are known to
2231 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2232 the return value is allocated using either malloc or bfd_alloc,
2233 according to the KEEP_MEMORY argument. If O has two relocation
2234 sections (both REL and RELA relocations), then the REL_HDR
2235 relocations will appear first in INTERNAL_RELOCS, followed by the
2236 REL_HDR2 relocations. */
2239 _bfd_elf_link_read_relocs (bfd *abfd,
2241 void *external_relocs,
2242 Elf_Internal_Rela *internal_relocs,
2243 bfd_boolean keep_memory)
2245 Elf_Internal_Shdr *rel_hdr;
2246 void *alloc1 = NULL;
2247 Elf_Internal_Rela *alloc2 = NULL;
2248 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2250 if (elf_section_data (o)->relocs != NULL)
2251 return elf_section_data (o)->relocs;
2253 if (o->reloc_count == 0)
2256 rel_hdr = &elf_section_data (o)->rel_hdr;
2258 if (internal_relocs == NULL)
2262 size = o->reloc_count;
2263 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2265 internal_relocs = alloc2 = bfd_alloc (abfd, size);
2267 internal_relocs = alloc2 = bfd_malloc (size);
2268 if (internal_relocs == NULL)
2272 if (external_relocs == NULL)
2274 bfd_size_type size = rel_hdr->sh_size;
2276 if (elf_section_data (o)->rel_hdr2)
2277 size += elf_section_data (o)->rel_hdr2->sh_size;
2278 alloc1 = bfd_malloc (size);
2281 external_relocs = alloc1;
2284 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2288 if (elf_section_data (o)->rel_hdr2
2289 && (!elf_link_read_relocs_from_section
2291 elf_section_data (o)->rel_hdr2,
2292 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2293 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2294 * bed->s->int_rels_per_ext_rel))))
2297 /* Cache the results for next time, if we can. */
2299 elf_section_data (o)->relocs = internal_relocs;
2304 /* Don't free alloc2, since if it was allocated we are passing it
2305 back (under the name of internal_relocs). */
2307 return internal_relocs;
2315 bfd_release (abfd, alloc2);
2322 /* Compute the size of, and allocate space for, REL_HDR which is the
2323 section header for a section containing relocations for O. */
2326 _bfd_elf_link_size_reloc_section (bfd *abfd,
2327 Elf_Internal_Shdr *rel_hdr,
2330 bfd_size_type reloc_count;
2331 bfd_size_type num_rel_hashes;
2333 /* Figure out how many relocations there will be. */
2334 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2335 reloc_count = elf_section_data (o)->rel_count;
2337 reloc_count = elf_section_data (o)->rel_count2;
2339 num_rel_hashes = o->reloc_count;
2340 if (num_rel_hashes < reloc_count)
2341 num_rel_hashes = reloc_count;
2343 /* That allows us to calculate the size of the section. */
2344 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2346 /* The contents field must last into write_object_contents, so we
2347 allocate it with bfd_alloc rather than malloc. Also since we
2348 cannot be sure that the contents will actually be filled in,
2349 we zero the allocated space. */
2350 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2351 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2354 /* We only allocate one set of hash entries, so we only do it the
2355 first time we are called. */
2356 if (elf_section_data (o)->rel_hashes == NULL
2359 struct elf_link_hash_entry **p;
2361 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2365 elf_section_data (o)->rel_hashes = p;
2371 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2372 originated from the section given by INPUT_REL_HDR) to the
2376 _bfd_elf_link_output_relocs (bfd *output_bfd,
2377 asection *input_section,
2378 Elf_Internal_Shdr *input_rel_hdr,
2379 Elf_Internal_Rela *internal_relocs,
2380 struct elf_link_hash_entry **rel_hash
2383 Elf_Internal_Rela *irela;
2384 Elf_Internal_Rela *irelaend;
2386 Elf_Internal_Shdr *output_rel_hdr;
2387 asection *output_section;
2388 unsigned int *rel_countp = NULL;
2389 const struct elf_backend_data *bed;
2390 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2392 output_section = input_section->output_section;
2393 output_rel_hdr = NULL;
2395 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2396 == input_rel_hdr->sh_entsize)
2398 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2399 rel_countp = &elf_section_data (output_section)->rel_count;
2401 else if (elf_section_data (output_section)->rel_hdr2
2402 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2403 == input_rel_hdr->sh_entsize))
2405 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2406 rel_countp = &elf_section_data (output_section)->rel_count2;
2410 (*_bfd_error_handler)
2411 (_("%B: relocation size mismatch in %B section %A"),
2412 output_bfd, input_section->owner, input_section);
2413 bfd_set_error (bfd_error_wrong_format);
2417 bed = get_elf_backend_data (output_bfd);
2418 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2419 swap_out = bed->s->swap_reloc_out;
2420 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2421 swap_out = bed->s->swap_reloca_out;
2425 erel = output_rel_hdr->contents;
2426 erel += *rel_countp * input_rel_hdr->sh_entsize;
2427 irela = internal_relocs;
2428 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2429 * bed->s->int_rels_per_ext_rel);
2430 while (irela < irelaend)
2432 (*swap_out) (output_bfd, irela, erel);
2433 irela += bed->s->int_rels_per_ext_rel;
2434 erel += input_rel_hdr->sh_entsize;
2437 /* Bump the counter, so that we know where to add the next set of
2439 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2444 /* Make weak undefined symbols in PIE dynamic. */
2447 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2448 struct elf_link_hash_entry *h)
2452 && h->root.type == bfd_link_hash_undefweak)
2453 return bfd_elf_link_record_dynamic_symbol (info, h);
2458 /* Fix up the flags for a symbol. This handles various cases which
2459 can only be fixed after all the input files are seen. This is
2460 currently called by both adjust_dynamic_symbol and
2461 assign_sym_version, which is unnecessary but perhaps more robust in
2462 the face of future changes. */
2465 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2466 struct elf_info_failed *eif)
2468 const struct elf_backend_data *bed;
2470 /* If this symbol was mentioned in a non-ELF file, try to set
2471 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2472 permit a non-ELF file to correctly refer to a symbol defined in
2473 an ELF dynamic object. */
2476 while (h->root.type == bfd_link_hash_indirect)
2477 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2479 if (h->root.type != bfd_link_hash_defined
2480 && h->root.type != bfd_link_hash_defweak)
2483 h->ref_regular_nonweak = 1;
2487 if (h->root.u.def.section->owner != NULL
2488 && (bfd_get_flavour (h->root.u.def.section->owner)
2489 == bfd_target_elf_flavour))
2492 h->ref_regular_nonweak = 1;
2498 if (h->dynindx == -1
2502 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2511 /* Unfortunately, NON_ELF is only correct if the symbol
2512 was first seen in a non-ELF file. Fortunately, if the symbol
2513 was first seen in an ELF file, we're probably OK unless the
2514 symbol was defined in a non-ELF file. Catch that case here.
2515 FIXME: We're still in trouble if the symbol was first seen in
2516 a dynamic object, and then later in a non-ELF regular object. */
2517 if ((h->root.type == bfd_link_hash_defined
2518 || h->root.type == bfd_link_hash_defweak)
2520 && (h->root.u.def.section->owner != NULL
2521 ? (bfd_get_flavour (h->root.u.def.section->owner)
2522 != bfd_target_elf_flavour)
2523 : (bfd_is_abs_section (h->root.u.def.section)
2524 && !h->def_dynamic)))
2528 /* Backend specific symbol fixup. */
2529 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2530 if (bed->elf_backend_fixup_symbol
2531 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2534 /* If this is a final link, and the symbol was defined as a common
2535 symbol in a regular object file, and there was no definition in
2536 any dynamic object, then the linker will have allocated space for
2537 the symbol in a common section but the DEF_REGULAR
2538 flag will not have been set. */
2539 if (h->root.type == bfd_link_hash_defined
2543 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2546 /* If -Bsymbolic was used (which means to bind references to global
2547 symbols to the definition within the shared object), and this
2548 symbol was defined in a regular object, then it actually doesn't
2549 need a PLT entry. Likewise, if the symbol has non-default
2550 visibility. If the symbol has hidden or internal visibility, we
2551 will force it local. */
2553 && eif->info->shared
2554 && is_elf_hash_table (eif->info->hash)
2555 && (SYMBOLIC_BIND (eif->info, h)
2556 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2559 bfd_boolean force_local;
2561 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2562 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2563 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2566 /* If a weak undefined symbol has non-default visibility, we also
2567 hide it from the dynamic linker. */
2568 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2569 && h->root.type == bfd_link_hash_undefweak)
2570 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2572 /* If this is a weak defined symbol in a dynamic object, and we know
2573 the real definition in the dynamic object, copy interesting flags
2574 over to the real definition. */
2575 if (h->u.weakdef != NULL)
2577 struct elf_link_hash_entry *weakdef;
2579 weakdef = h->u.weakdef;
2580 if (h->root.type == bfd_link_hash_indirect)
2581 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2583 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2584 || h->root.type == bfd_link_hash_defweak);
2585 BFD_ASSERT (weakdef->def_dynamic);
2587 /* If the real definition is defined by a regular object file,
2588 don't do anything special. See the longer description in
2589 _bfd_elf_adjust_dynamic_symbol, below. */
2590 if (weakdef->def_regular)
2591 h->u.weakdef = NULL;
2594 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2595 || weakdef->root.type == bfd_link_hash_defweak);
2596 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2603 /* Make the backend pick a good value for a dynamic symbol. This is
2604 called via elf_link_hash_traverse, and also calls itself
2608 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2610 struct elf_info_failed *eif = data;
2612 const struct elf_backend_data *bed;
2614 if (! is_elf_hash_table (eif->info->hash))
2617 if (h->root.type == bfd_link_hash_warning)
2619 h->got = elf_hash_table (eif->info)->init_got_offset;
2620 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2622 /* When warning symbols are created, they **replace** the "real"
2623 entry in the hash table, thus we never get to see the real
2624 symbol in a hash traversal. So look at it now. */
2625 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2628 /* Ignore indirect symbols. These are added by the versioning code. */
2629 if (h->root.type == bfd_link_hash_indirect)
2632 /* Fix the symbol flags. */
2633 if (! _bfd_elf_fix_symbol_flags (h, eif))
2636 /* If this symbol does not require a PLT entry, and it is not
2637 defined by a dynamic object, or is not referenced by a regular
2638 object, ignore it. We do have to handle a weak defined symbol,
2639 even if no regular object refers to it, if we decided to add it
2640 to the dynamic symbol table. FIXME: Do we normally need to worry
2641 about symbols which are defined by one dynamic object and
2642 referenced by another one? */
2647 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2649 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2653 /* If we've already adjusted this symbol, don't do it again. This
2654 can happen via a recursive call. */
2655 if (h->dynamic_adjusted)
2658 /* Don't look at this symbol again. Note that we must set this
2659 after checking the above conditions, because we may look at a
2660 symbol once, decide not to do anything, and then get called
2661 recursively later after REF_REGULAR is set below. */
2662 h->dynamic_adjusted = 1;
2664 /* If this is a weak definition, and we know a real definition, and
2665 the real symbol is not itself defined by a regular object file,
2666 then get a good value for the real definition. We handle the
2667 real symbol first, for the convenience of the backend routine.
2669 Note that there is a confusing case here. If the real definition
2670 is defined by a regular object file, we don't get the real symbol
2671 from the dynamic object, but we do get the weak symbol. If the
2672 processor backend uses a COPY reloc, then if some routine in the
2673 dynamic object changes the real symbol, we will not see that
2674 change in the corresponding weak symbol. This is the way other
2675 ELF linkers work as well, and seems to be a result of the shared
2678 I will clarify this issue. Most SVR4 shared libraries define the
2679 variable _timezone and define timezone as a weak synonym. The
2680 tzset call changes _timezone. If you write
2681 extern int timezone;
2683 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2684 you might expect that, since timezone is a synonym for _timezone,
2685 the same number will print both times. However, if the processor
2686 backend uses a COPY reloc, then actually timezone will be copied
2687 into your process image, and, since you define _timezone
2688 yourself, _timezone will not. Thus timezone and _timezone will
2689 wind up at different memory locations. The tzset call will set
2690 _timezone, leaving timezone unchanged. */
2692 if (h->u.weakdef != NULL)
2694 /* If we get to this point, we know there is an implicit
2695 reference by a regular object file via the weak symbol H.
2696 FIXME: Is this really true? What if the traversal finds
2697 H->U.WEAKDEF before it finds H? */
2698 h->u.weakdef->ref_regular = 1;
2700 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2704 /* If a symbol has no type and no size and does not require a PLT
2705 entry, then we are probably about to do the wrong thing here: we
2706 are probably going to create a COPY reloc for an empty object.
2707 This case can arise when a shared object is built with assembly
2708 code, and the assembly code fails to set the symbol type. */
2710 && h->type == STT_NOTYPE
2712 (*_bfd_error_handler)
2713 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2714 h->root.root.string);
2716 dynobj = elf_hash_table (eif->info)->dynobj;
2717 bed = get_elf_backend_data (dynobj);
2719 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2728 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2732 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2735 unsigned int power_of_two;
2737 asection *sec = h->root.u.def.section;
2739 /* The section aligment of definition is the maximum alignment
2740 requirement of symbols defined in the section. Since we don't
2741 know the symbol alignment requirement, we start with the
2742 maximum alignment and check low bits of the symbol address
2743 for the minimum alignment. */
2744 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2745 mask = ((bfd_vma) 1 << power_of_two) - 1;
2746 while ((h->root.u.def.value & mask) != 0)
2752 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2755 /* Adjust the section alignment if needed. */
2756 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2761 /* We make sure that the symbol will be aligned properly. */
2762 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2764 /* Define the symbol as being at this point in DYNBSS. */
2765 h->root.u.def.section = dynbss;
2766 h->root.u.def.value = dynbss->size;
2768 /* Increment the size of DYNBSS to make room for the symbol. */
2769 dynbss->size += h->size;
2774 /* Adjust all external symbols pointing into SEC_MERGE sections
2775 to reflect the object merging within the sections. */
2778 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2782 if (h->root.type == bfd_link_hash_warning)
2783 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2785 if ((h->root.type == bfd_link_hash_defined
2786 || h->root.type == bfd_link_hash_defweak)
2787 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2788 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2790 bfd *output_bfd = data;
2792 h->root.u.def.value =
2793 _bfd_merged_section_offset (output_bfd,
2794 &h->root.u.def.section,
2795 elf_section_data (sec)->sec_info,
2796 h->root.u.def.value);
2802 /* Returns false if the symbol referred to by H should be considered
2803 to resolve local to the current module, and true if it should be
2804 considered to bind dynamically. */
2807 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2808 struct bfd_link_info *info,
2809 bfd_boolean ignore_protected)
2811 bfd_boolean binding_stays_local_p;
2812 const struct elf_backend_data *bed;
2813 struct elf_link_hash_table *hash_table;
2818 while (h->root.type == bfd_link_hash_indirect
2819 || h->root.type == bfd_link_hash_warning)
2820 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2822 /* If it was forced local, then clearly it's not dynamic. */
2823 if (h->dynindx == -1)
2825 if (h->forced_local)
2828 /* Identify the cases where name binding rules say that a
2829 visible symbol resolves locally. */
2830 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2832 switch (ELF_ST_VISIBILITY (h->other))
2839 hash_table = elf_hash_table (info);
2840 if (!is_elf_hash_table (hash_table))
2843 bed = get_elf_backend_data (hash_table->dynobj);
2845 /* Proper resolution for function pointer equality may require
2846 that these symbols perhaps be resolved dynamically, even though
2847 we should be resolving them to the current module. */
2848 if (!ignore_protected || !bed->is_function_type (h->type))
2849 binding_stays_local_p = TRUE;
2856 /* If it isn't defined locally, then clearly it's dynamic. */
2857 if (!h->def_regular)
2860 /* Otherwise, the symbol is dynamic if binding rules don't tell
2861 us that it remains local. */
2862 return !binding_stays_local_p;
2865 /* Return true if the symbol referred to by H should be considered
2866 to resolve local to the current module, and false otherwise. Differs
2867 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2868 undefined symbols and weak symbols. */
2871 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2872 struct bfd_link_info *info,
2873 bfd_boolean local_protected)
2875 const struct elf_backend_data *bed;
2876 struct elf_link_hash_table *hash_table;
2878 /* If it's a local sym, of course we resolve locally. */
2882 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2883 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2884 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2887 /* Common symbols that become definitions don't get the DEF_REGULAR
2888 flag set, so test it first, and don't bail out. */
2889 if (ELF_COMMON_DEF_P (h))
2891 /* If we don't have a definition in a regular file, then we can't
2892 resolve locally. The sym is either undefined or dynamic. */
2893 else if (!h->def_regular)
2896 /* Forced local symbols resolve locally. */
2897 if (h->forced_local)
2900 /* As do non-dynamic symbols. */
2901 if (h->dynindx == -1)
2904 /* At this point, we know the symbol is defined and dynamic. In an
2905 executable it must resolve locally, likewise when building symbolic
2906 shared libraries. */
2907 if (info->executable || SYMBOLIC_BIND (info, h))
2910 /* Now deal with defined dynamic symbols in shared libraries. Ones
2911 with default visibility might not resolve locally. */
2912 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2915 hash_table = elf_hash_table (info);
2916 if (!is_elf_hash_table (hash_table))
2919 bed = get_elf_backend_data (hash_table->dynobj);
2921 /* STV_PROTECTED non-function symbols are local. */
2922 if (!bed->is_function_type (h->type))
2925 /* Function pointer equality tests may require that STV_PROTECTED
2926 symbols be treated as dynamic symbols, even when we know that the
2927 dynamic linker will resolve them locally. */
2928 return local_protected;
2931 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2932 aligned. Returns the first TLS output section. */
2934 struct bfd_section *
2935 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2937 struct bfd_section *sec, *tls;
2938 unsigned int align = 0;
2940 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2941 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2945 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2946 if (sec->alignment_power > align)
2947 align = sec->alignment_power;
2949 elf_hash_table (info)->tls_sec = tls;
2951 /* Ensure the alignment of the first section is the largest alignment,
2952 so that the tls segment starts aligned. */
2954 tls->alignment_power = align;
2959 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2961 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2962 Elf_Internal_Sym *sym)
2964 const struct elf_backend_data *bed;
2966 /* Local symbols do not count, but target specific ones might. */
2967 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2968 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2971 bed = get_elf_backend_data (abfd);
2972 /* Function symbols do not count. */
2973 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2976 /* If the section is undefined, then so is the symbol. */
2977 if (sym->st_shndx == SHN_UNDEF)
2980 /* If the symbol is defined in the common section, then
2981 it is a common definition and so does not count. */
2982 if (bed->common_definition (sym))
2985 /* If the symbol is in a target specific section then we
2986 must rely upon the backend to tell us what it is. */
2987 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2988 /* FIXME - this function is not coded yet:
2990 return _bfd_is_global_symbol_definition (abfd, sym);
2992 Instead for now assume that the definition is not global,
2993 Even if this is wrong, at least the linker will behave
2994 in the same way that it used to do. */
3000 /* Search the symbol table of the archive element of the archive ABFD
3001 whose archive map contains a mention of SYMDEF, and determine if
3002 the symbol is defined in this element. */
3004 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3006 Elf_Internal_Shdr * hdr;
3007 bfd_size_type symcount;
3008 bfd_size_type extsymcount;
3009 bfd_size_type extsymoff;
3010 Elf_Internal_Sym *isymbuf;
3011 Elf_Internal_Sym *isym;
3012 Elf_Internal_Sym *isymend;
3015 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3019 if (! bfd_check_format (abfd, bfd_object))
3022 /* If we have already included the element containing this symbol in the
3023 link then we do not need to include it again. Just claim that any symbol
3024 it contains is not a definition, so that our caller will not decide to
3025 (re)include this element. */
3026 if (abfd->archive_pass)
3029 /* Select the appropriate symbol table. */
3030 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3031 hdr = &elf_tdata (abfd)->symtab_hdr;
3033 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3035 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3037 /* The sh_info field of the symtab header tells us where the
3038 external symbols start. We don't care about the local symbols. */
3039 if (elf_bad_symtab (abfd))
3041 extsymcount = symcount;
3046 extsymcount = symcount - hdr->sh_info;
3047 extsymoff = hdr->sh_info;
3050 if (extsymcount == 0)
3053 /* Read in the symbol table. */
3054 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3056 if (isymbuf == NULL)
3059 /* Scan the symbol table looking for SYMDEF. */
3061 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3065 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3070 if (strcmp (name, symdef->name) == 0)
3072 result = is_global_data_symbol_definition (abfd, isym);
3082 /* Add an entry to the .dynamic table. */
3085 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3089 struct elf_link_hash_table *hash_table;
3090 const struct elf_backend_data *bed;
3092 bfd_size_type newsize;
3093 bfd_byte *newcontents;
3094 Elf_Internal_Dyn dyn;
3096 hash_table = elf_hash_table (info);
3097 if (! is_elf_hash_table (hash_table))
3100 bed = get_elf_backend_data (hash_table->dynobj);
3101 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3102 BFD_ASSERT (s != NULL);
3104 newsize = s->size + bed->s->sizeof_dyn;
3105 newcontents = bfd_realloc (s->contents, newsize);
3106 if (newcontents == NULL)
3110 dyn.d_un.d_val = val;
3111 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3114 s->contents = newcontents;
3119 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3120 otherwise just check whether one already exists. Returns -1 on error,
3121 1 if a DT_NEEDED tag already exists, and 0 on success. */
3124 elf_add_dt_needed_tag (bfd *abfd,
3125 struct bfd_link_info *info,
3129 struct elf_link_hash_table *hash_table;
3130 bfd_size_type oldsize;
3131 bfd_size_type strindex;
3133 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3136 hash_table = elf_hash_table (info);
3137 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3138 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3139 if (strindex == (bfd_size_type) -1)
3142 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3145 const struct elf_backend_data *bed;
3148 bed = get_elf_backend_data (hash_table->dynobj);
3149 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3151 for (extdyn = sdyn->contents;
3152 extdyn < sdyn->contents + sdyn->size;
3153 extdyn += bed->s->sizeof_dyn)
3155 Elf_Internal_Dyn dyn;
3157 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3158 if (dyn.d_tag == DT_NEEDED
3159 && dyn.d_un.d_val == strindex)
3161 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3169 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3172 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3176 /* We were just checking for existence of the tag. */
3177 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3182 /* Sort symbol by value and section. */
3184 elf_sort_symbol (const void *arg1, const void *arg2)
3186 const struct elf_link_hash_entry *h1;
3187 const struct elf_link_hash_entry *h2;
3188 bfd_signed_vma vdiff;
3190 h1 = *(const struct elf_link_hash_entry **) arg1;
3191 h2 = *(const struct elf_link_hash_entry **) arg2;
3192 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3194 return vdiff > 0 ? 1 : -1;
3197 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3199 return sdiff > 0 ? 1 : -1;
3204 /* This function is used to adjust offsets into .dynstr for
3205 dynamic symbols. This is called via elf_link_hash_traverse. */
3208 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3210 struct elf_strtab_hash *dynstr = data;
3212 if (h->root.type == bfd_link_hash_warning)
3213 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3215 if (h->dynindx != -1)
3216 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3220 /* Assign string offsets in .dynstr, update all structures referencing
3224 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3226 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3227 struct elf_link_local_dynamic_entry *entry;
3228 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3229 bfd *dynobj = hash_table->dynobj;
3232 const struct elf_backend_data *bed;
3235 _bfd_elf_strtab_finalize (dynstr);
3236 size = _bfd_elf_strtab_size (dynstr);
3238 bed = get_elf_backend_data (dynobj);
3239 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3240 BFD_ASSERT (sdyn != NULL);
3242 /* Update all .dynamic entries referencing .dynstr strings. */
3243 for (extdyn = sdyn->contents;
3244 extdyn < sdyn->contents + sdyn->size;
3245 extdyn += bed->s->sizeof_dyn)
3247 Elf_Internal_Dyn dyn;
3249 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3253 dyn.d_un.d_val = size;
3261 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3266 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3269 /* Now update local dynamic symbols. */
3270 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3271 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3272 entry->isym.st_name);
3274 /* And the rest of dynamic symbols. */
3275 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3277 /* Adjust version definitions. */
3278 if (elf_tdata (output_bfd)->cverdefs)
3283 Elf_Internal_Verdef def;
3284 Elf_Internal_Verdaux defaux;
3286 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3290 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3292 p += sizeof (Elf_External_Verdef);
3293 if (def.vd_aux != sizeof (Elf_External_Verdef))
3295 for (i = 0; i < def.vd_cnt; ++i)
3297 _bfd_elf_swap_verdaux_in (output_bfd,
3298 (Elf_External_Verdaux *) p, &defaux);
3299 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3301 _bfd_elf_swap_verdaux_out (output_bfd,
3302 &defaux, (Elf_External_Verdaux *) p);
3303 p += sizeof (Elf_External_Verdaux);
3306 while (def.vd_next);
3309 /* Adjust version references. */
3310 if (elf_tdata (output_bfd)->verref)
3315 Elf_Internal_Verneed need;
3316 Elf_Internal_Vernaux needaux;
3318 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3322 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3324 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3325 _bfd_elf_swap_verneed_out (output_bfd, &need,
3326 (Elf_External_Verneed *) p);
3327 p += sizeof (Elf_External_Verneed);
3328 for (i = 0; i < need.vn_cnt; ++i)
3330 _bfd_elf_swap_vernaux_in (output_bfd,
3331 (Elf_External_Vernaux *) p, &needaux);
3332 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3334 _bfd_elf_swap_vernaux_out (output_bfd,
3336 (Elf_External_Vernaux *) p);
3337 p += sizeof (Elf_External_Vernaux);
3340 while (need.vn_next);
3346 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3347 The default is to only match when the INPUT and OUTPUT are exactly
3351 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3352 const bfd_target *output)
3354 return input == output;
3357 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3358 This version is used when different targets for the same architecture
3359 are virtually identical. */
3362 _bfd_elf_relocs_compatible (const bfd_target *input,
3363 const bfd_target *output)
3365 const struct elf_backend_data *obed, *ibed;
3367 if (input == output)
3370 ibed = xvec_get_elf_backend_data (input);
3371 obed = xvec_get_elf_backend_data (output);
3373 if (ibed->arch != obed->arch)
3376 /* If both backends are using this function, deem them compatible. */
3377 return ibed->relocs_compatible == obed->relocs_compatible;
3380 /* Add symbols from an ELF object file to the linker hash table. */
3383 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3385 Elf_Internal_Shdr *hdr;
3386 bfd_size_type symcount;
3387 bfd_size_type extsymcount;
3388 bfd_size_type extsymoff;
3389 struct elf_link_hash_entry **sym_hash;
3390 bfd_boolean dynamic;
3391 Elf_External_Versym *extversym = NULL;
3392 Elf_External_Versym *ever;
3393 struct elf_link_hash_entry *weaks;
3394 struct elf_link_hash_entry **nondeflt_vers = NULL;
3395 bfd_size_type nondeflt_vers_cnt = 0;
3396 Elf_Internal_Sym *isymbuf = NULL;
3397 Elf_Internal_Sym *isym;
3398 Elf_Internal_Sym *isymend;
3399 const struct elf_backend_data *bed;
3400 bfd_boolean add_needed;
3401 struct elf_link_hash_table *htab;
3403 void *alloc_mark = NULL;
3404 struct bfd_hash_entry **old_table = NULL;
3405 unsigned int old_size = 0;
3406 unsigned int old_count = 0;
3407 void *old_tab = NULL;
3410 struct bfd_link_hash_entry *old_undefs = NULL;
3411 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3412 long old_dynsymcount = 0;
3414 size_t hashsize = 0;
3416 htab = elf_hash_table (info);
3417 bed = get_elf_backend_data (abfd);
3419 if ((abfd->flags & DYNAMIC) == 0)
3425 /* You can't use -r against a dynamic object. Also, there's no
3426 hope of using a dynamic object which does not exactly match
3427 the format of the output file. */
3428 if (info->relocatable
3429 || !is_elf_hash_table (htab)
3430 || info->output_bfd->xvec != abfd->xvec)
3432 if (info->relocatable)
3433 bfd_set_error (bfd_error_invalid_operation);
3435 bfd_set_error (bfd_error_wrong_format);
3440 /* As a GNU extension, any input sections which are named
3441 .gnu.warning.SYMBOL are treated as warning symbols for the given
3442 symbol. This differs from .gnu.warning sections, which generate
3443 warnings when they are included in an output file. */
3444 if (info->executable)
3448 for (s = abfd->sections; s != NULL; s = s->next)
3452 name = bfd_get_section_name (abfd, s);
3453 if (CONST_STRNEQ (name, ".gnu.warning."))
3458 name += sizeof ".gnu.warning." - 1;
3460 /* If this is a shared object, then look up the symbol
3461 in the hash table. If it is there, and it is already
3462 been defined, then we will not be using the entry
3463 from this shared object, so we don't need to warn.
3464 FIXME: If we see the definition in a regular object
3465 later on, we will warn, but we shouldn't. The only
3466 fix is to keep track of what warnings we are supposed
3467 to emit, and then handle them all at the end of the
3471 struct elf_link_hash_entry *h;
3473 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3475 /* FIXME: What about bfd_link_hash_common? */
3477 && (h->root.type == bfd_link_hash_defined
3478 || h->root.type == bfd_link_hash_defweak))
3480 /* We don't want to issue this warning. Clobber
3481 the section size so that the warning does not
3482 get copied into the output file. */
3489 msg = bfd_alloc (abfd, sz + 1);
3493 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3498 if (! (_bfd_generic_link_add_one_symbol
3499 (info, abfd, name, BSF_WARNING, s, 0, msg,
3500 FALSE, bed->collect, NULL)))
3503 if (! info->relocatable)
3505 /* Clobber the section size so that the warning does
3506 not get copied into the output file. */
3509 /* Also set SEC_EXCLUDE, so that symbols defined in
3510 the warning section don't get copied to the output. */
3511 s->flags |= SEC_EXCLUDE;
3520 /* If we are creating a shared library, create all the dynamic
3521 sections immediately. We need to attach them to something,
3522 so we attach them to this BFD, provided it is the right
3523 format. FIXME: If there are no input BFD's of the same
3524 format as the output, we can't make a shared library. */
3526 && is_elf_hash_table (htab)
3527 && info->output_bfd->xvec == abfd->xvec
3528 && !htab->dynamic_sections_created)
3530 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3534 else if (!is_elf_hash_table (htab))
3539 const char *soname = NULL;
3540 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3543 /* ld --just-symbols and dynamic objects don't mix very well.
3544 ld shouldn't allow it. */
3545 if ((s = abfd->sections) != NULL
3546 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3549 /* If this dynamic lib was specified on the command line with
3550 --as-needed in effect, then we don't want to add a DT_NEEDED
3551 tag unless the lib is actually used. Similary for libs brought
3552 in by another lib's DT_NEEDED. When --no-add-needed is used
3553 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3554 any dynamic library in DT_NEEDED tags in the dynamic lib at
3556 add_needed = (elf_dyn_lib_class (abfd)
3557 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3558 | DYN_NO_NEEDED)) == 0;
3560 s = bfd_get_section_by_name (abfd, ".dynamic");
3565 unsigned int elfsec;
3566 unsigned long shlink;
3568 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3569 goto error_free_dyn;
3571 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3572 if (elfsec == SHN_BAD)
3573 goto error_free_dyn;
3574 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3576 for (extdyn = dynbuf;
3577 extdyn < dynbuf + s->size;
3578 extdyn += bed->s->sizeof_dyn)
3580 Elf_Internal_Dyn dyn;
3582 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3583 if (dyn.d_tag == DT_SONAME)
3585 unsigned int tagv = dyn.d_un.d_val;
3586 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3588 goto error_free_dyn;
3590 if (dyn.d_tag == DT_NEEDED)
3592 struct bfd_link_needed_list *n, **pn;
3594 unsigned int tagv = dyn.d_un.d_val;
3596 amt = sizeof (struct bfd_link_needed_list);
3597 n = bfd_alloc (abfd, amt);
3598 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3599 if (n == NULL || fnm == NULL)
3600 goto error_free_dyn;
3601 amt = strlen (fnm) + 1;
3602 anm = bfd_alloc (abfd, amt);
3604 goto error_free_dyn;
3605 memcpy (anm, fnm, amt);
3609 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3613 if (dyn.d_tag == DT_RUNPATH)
3615 struct bfd_link_needed_list *n, **pn;
3617 unsigned int tagv = dyn.d_un.d_val;
3619 amt = sizeof (struct bfd_link_needed_list);
3620 n = bfd_alloc (abfd, amt);
3621 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3622 if (n == NULL || fnm == NULL)
3623 goto error_free_dyn;
3624 amt = strlen (fnm) + 1;
3625 anm = bfd_alloc (abfd, amt);
3627 goto error_free_dyn;
3628 memcpy (anm, fnm, amt);
3632 for (pn = & runpath;
3638 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3639 if (!runpath && dyn.d_tag == DT_RPATH)
3641 struct bfd_link_needed_list *n, **pn;
3643 unsigned int tagv = dyn.d_un.d_val;
3645 amt = sizeof (struct bfd_link_needed_list);
3646 n = bfd_alloc (abfd, amt);
3647 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3648 if (n == NULL || fnm == NULL)
3649 goto error_free_dyn;
3650 amt = strlen (fnm) + 1;
3651 anm = bfd_alloc (abfd, amt);
3658 memcpy (anm, fnm, amt);
3673 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3674 frees all more recently bfd_alloc'd blocks as well. */
3680 struct bfd_link_needed_list **pn;
3681 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3686 /* We do not want to include any of the sections in a dynamic
3687 object in the output file. We hack by simply clobbering the
3688 list of sections in the BFD. This could be handled more
3689 cleanly by, say, a new section flag; the existing
3690 SEC_NEVER_LOAD flag is not the one we want, because that one
3691 still implies that the section takes up space in the output
3693 bfd_section_list_clear (abfd);
3695 /* Find the name to use in a DT_NEEDED entry that refers to this
3696 object. If the object has a DT_SONAME entry, we use it.
3697 Otherwise, if the generic linker stuck something in
3698 elf_dt_name, we use that. Otherwise, we just use the file
3700 if (soname == NULL || *soname == '\0')
3702 soname = elf_dt_name (abfd);
3703 if (soname == NULL || *soname == '\0')
3704 soname = bfd_get_filename (abfd);
3707 /* Save the SONAME because sometimes the linker emulation code
3708 will need to know it. */
3709 elf_dt_name (abfd) = soname;
3711 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3715 /* If we have already included this dynamic object in the
3716 link, just ignore it. There is no reason to include a
3717 particular dynamic object more than once. */
3722 /* If this is a dynamic object, we always link against the .dynsym
3723 symbol table, not the .symtab symbol table. The dynamic linker
3724 will only see the .dynsym symbol table, so there is no reason to
3725 look at .symtab for a dynamic object. */
3727 if (! dynamic || elf_dynsymtab (abfd) == 0)
3728 hdr = &elf_tdata (abfd)->symtab_hdr;
3730 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3732 symcount = hdr->sh_size / bed->s->sizeof_sym;
3734 /* The sh_info field of the symtab header tells us where the
3735 external symbols start. We don't care about the local symbols at
3737 if (elf_bad_symtab (abfd))
3739 extsymcount = symcount;
3744 extsymcount = symcount - hdr->sh_info;
3745 extsymoff = hdr->sh_info;
3749 if (extsymcount != 0)
3751 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3753 if (isymbuf == NULL)
3756 /* We store a pointer to the hash table entry for each external
3758 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3759 sym_hash = bfd_alloc (abfd, amt);
3760 if (sym_hash == NULL)
3761 goto error_free_sym;
3762 elf_sym_hashes (abfd) = sym_hash;
3767 /* Read in any version definitions. */
3768 if (!_bfd_elf_slurp_version_tables (abfd,
3769 info->default_imported_symver))
3770 goto error_free_sym;
3772 /* Read in the symbol versions, but don't bother to convert them
3773 to internal format. */
3774 if (elf_dynversym (abfd) != 0)
3776 Elf_Internal_Shdr *versymhdr;
3778 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3779 extversym = bfd_malloc (versymhdr->sh_size);
3780 if (extversym == NULL)
3781 goto error_free_sym;
3782 amt = versymhdr->sh_size;
3783 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3784 || bfd_bread (extversym, amt, abfd) != amt)
3785 goto error_free_vers;
3789 /* If we are loading an as-needed shared lib, save the symbol table
3790 state before we start adding symbols. If the lib turns out
3791 to be unneeded, restore the state. */
3792 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3797 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3799 struct bfd_hash_entry *p;
3800 struct elf_link_hash_entry *h;
3802 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3804 h = (struct elf_link_hash_entry *) p;
3805 entsize += htab->root.table.entsize;
3806 if (h->root.type == bfd_link_hash_warning)
3807 entsize += htab->root.table.entsize;
3811 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3812 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3813 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3814 if (old_tab == NULL)
3815 goto error_free_vers;
3817 /* Remember the current objalloc pointer, so that all mem for
3818 symbols added can later be reclaimed. */
3819 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3820 if (alloc_mark == NULL)
3821 goto error_free_vers;
3823 /* Make a special call to the linker "notice" function to
3824 tell it that we are about to handle an as-needed lib. */
3825 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3827 goto error_free_vers;
3829 /* Clone the symbol table and sym hashes. Remember some
3830 pointers into the symbol table, and dynamic symbol count. */
3831 old_hash = (char *) old_tab + tabsize;
3832 old_ent = (char *) old_hash + hashsize;
3833 memcpy (old_tab, htab->root.table.table, tabsize);
3834 memcpy (old_hash, sym_hash, hashsize);
3835 old_undefs = htab->root.undefs;
3836 old_undefs_tail = htab->root.undefs_tail;
3837 old_table = htab->root.table.table;
3838 old_size = htab->root.table.size;
3839 old_count = htab->root.table.count;
3840 old_dynsymcount = htab->dynsymcount;
3842 for (i = 0; i < htab->root.table.size; i++)
3844 struct bfd_hash_entry *p;
3845 struct elf_link_hash_entry *h;
3847 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3849 memcpy (old_ent, p, htab->root.table.entsize);
3850 old_ent = (char *) old_ent + htab->root.table.entsize;
3851 h = (struct elf_link_hash_entry *) p;
3852 if (h->root.type == bfd_link_hash_warning)
3854 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3855 old_ent = (char *) old_ent + htab->root.table.entsize;
3862 ever = extversym != NULL ? extversym + extsymoff : NULL;
3863 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3865 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3869 asection *sec, *new_sec;
3872 struct elf_link_hash_entry *h;
3873 bfd_boolean definition;
3874 bfd_boolean size_change_ok;
3875 bfd_boolean type_change_ok;
3876 bfd_boolean new_weakdef;
3877 bfd_boolean override;
3879 unsigned int old_alignment;
3884 flags = BSF_NO_FLAGS;
3886 value = isym->st_value;
3888 common = bed->common_definition (isym);
3890 bind = ELF_ST_BIND (isym->st_info);
3891 if (bind == STB_LOCAL)
3893 /* This should be impossible, since ELF requires that all
3894 global symbols follow all local symbols, and that sh_info
3895 point to the first global symbol. Unfortunately, Irix 5
3899 else if (bind == STB_GLOBAL)
3901 if (isym->st_shndx != SHN_UNDEF && !common)
3904 else if (bind == STB_WEAK)
3908 /* Leave it up to the processor backend. */
3911 if (isym->st_shndx == SHN_UNDEF)
3912 sec = bfd_und_section_ptr;
3913 else if (isym->st_shndx == SHN_ABS)
3914 sec = bfd_abs_section_ptr;
3915 else if (isym->st_shndx == SHN_COMMON)
3917 sec = bfd_com_section_ptr;
3918 /* What ELF calls the size we call the value. What ELF
3919 calls the value we call the alignment. */
3920 value = isym->st_size;
3924 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3926 sec = bfd_abs_section_ptr;
3927 else if (sec->kept_section)
3929 /* Symbols from discarded section are undefined. We keep
3931 sec = bfd_und_section_ptr;
3932 isym->st_shndx = SHN_UNDEF;
3934 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3938 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3941 goto error_free_vers;
3943 if (isym->st_shndx == SHN_COMMON
3944 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3945 && !info->relocatable)
3947 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3951 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3954 | SEC_LINKER_CREATED
3955 | SEC_THREAD_LOCAL));
3957 goto error_free_vers;
3961 else if (bed->elf_add_symbol_hook)
3963 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3965 goto error_free_vers;
3967 /* The hook function sets the name to NULL if this symbol
3968 should be skipped for some reason. */
3973 /* Sanity check that all possibilities were handled. */
3976 bfd_set_error (bfd_error_bad_value);
3977 goto error_free_vers;
3980 if (bfd_is_und_section (sec)
3981 || bfd_is_com_section (sec))
3986 size_change_ok = FALSE;
3987 type_change_ok = bed->type_change_ok;
3992 if (is_elf_hash_table (htab))
3994 Elf_Internal_Versym iver;
3995 unsigned int vernum = 0;
4000 if (info->default_imported_symver)
4001 /* Use the default symbol version created earlier. */
4002 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4007 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4009 vernum = iver.vs_vers & VERSYM_VERSION;
4011 /* If this is a hidden symbol, or if it is not version
4012 1, we append the version name to the symbol name.
4013 However, we do not modify a non-hidden absolute symbol
4014 if it is not a function, because it might be the version
4015 symbol itself. FIXME: What if it isn't? */
4016 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4018 && (!bfd_is_abs_section (sec)
4019 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4022 size_t namelen, verlen, newlen;
4025 if (isym->st_shndx != SHN_UNDEF)
4027 if (vernum > elf_tdata (abfd)->cverdefs)
4029 else if (vernum > 1)
4031 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4037 (*_bfd_error_handler)
4038 (_("%B: %s: invalid version %u (max %d)"),
4040 elf_tdata (abfd)->cverdefs);
4041 bfd_set_error (bfd_error_bad_value);
4042 goto error_free_vers;
4047 /* We cannot simply test for the number of
4048 entries in the VERNEED section since the
4049 numbers for the needed versions do not start
4051 Elf_Internal_Verneed *t;
4054 for (t = elf_tdata (abfd)->verref;
4058 Elf_Internal_Vernaux *a;
4060 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4062 if (a->vna_other == vernum)
4064 verstr = a->vna_nodename;
4073 (*_bfd_error_handler)
4074 (_("%B: %s: invalid needed version %d"),
4075 abfd, name, vernum);
4076 bfd_set_error (bfd_error_bad_value);
4077 goto error_free_vers;
4081 namelen = strlen (name);
4082 verlen = strlen (verstr);
4083 newlen = namelen + verlen + 2;
4084 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4085 && isym->st_shndx != SHN_UNDEF)
4088 newname = bfd_hash_allocate (&htab->root.table, newlen);
4089 if (newname == NULL)
4090 goto error_free_vers;
4091 memcpy (newname, name, namelen);
4092 p = newname + namelen;
4094 /* If this is a defined non-hidden version symbol,
4095 we add another @ to the name. This indicates the
4096 default version of the symbol. */
4097 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4098 && isym->st_shndx != SHN_UNDEF)
4100 memcpy (p, verstr, verlen + 1);
4105 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4106 &value, &old_alignment,
4107 sym_hash, &skip, &override,
4108 &type_change_ok, &size_change_ok))
4109 goto error_free_vers;
4118 while (h->root.type == bfd_link_hash_indirect
4119 || h->root.type == bfd_link_hash_warning)
4120 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4122 /* Remember the old alignment if this is a common symbol, so
4123 that we don't reduce the alignment later on. We can't
4124 check later, because _bfd_generic_link_add_one_symbol
4125 will set a default for the alignment which we want to
4126 override. We also remember the old bfd where the existing
4127 definition comes from. */
4128 switch (h->root.type)
4133 case bfd_link_hash_defined:
4134 case bfd_link_hash_defweak:
4135 old_bfd = h->root.u.def.section->owner;
4138 case bfd_link_hash_common:
4139 old_bfd = h->root.u.c.p->section->owner;
4140 old_alignment = h->root.u.c.p->alignment_power;
4144 if (elf_tdata (abfd)->verdef != NULL
4148 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4151 if (! (_bfd_generic_link_add_one_symbol
4152 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4153 (struct bfd_link_hash_entry **) sym_hash)))
4154 goto error_free_vers;
4157 while (h->root.type == bfd_link_hash_indirect
4158 || h->root.type == bfd_link_hash_warning)
4159 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4162 new_weakdef = FALSE;
4165 && (flags & BSF_WEAK) != 0
4166 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4167 && is_elf_hash_table (htab)
4168 && h->u.weakdef == NULL)
4170 /* Keep a list of all weak defined non function symbols from
4171 a dynamic object, using the weakdef field. Later in this
4172 function we will set the weakdef field to the correct
4173 value. We only put non-function symbols from dynamic
4174 objects on this list, because that happens to be the only
4175 time we need to know the normal symbol corresponding to a
4176 weak symbol, and the information is time consuming to
4177 figure out. If the weakdef field is not already NULL,
4178 then this symbol was already defined by some previous
4179 dynamic object, and we will be using that previous
4180 definition anyhow. */
4182 h->u.weakdef = weaks;
4187 /* Set the alignment of a common symbol. */
4188 if ((common || bfd_is_com_section (sec))
4189 && h->root.type == bfd_link_hash_common)
4194 align = bfd_log2 (isym->st_value);
4197 /* The new symbol is a common symbol in a shared object.
4198 We need to get the alignment from the section. */
4199 align = new_sec->alignment_power;
4201 if (align > old_alignment
4202 /* Permit an alignment power of zero if an alignment of one
4203 is specified and no other alignments have been specified. */
4204 || (isym->st_value == 1 && old_alignment == 0))
4205 h->root.u.c.p->alignment_power = align;
4207 h->root.u.c.p->alignment_power = old_alignment;
4210 if (is_elf_hash_table (htab))
4214 /* Check the alignment when a common symbol is involved. This
4215 can change when a common symbol is overridden by a normal
4216 definition or a common symbol is ignored due to the old
4217 normal definition. We need to make sure the maximum
4218 alignment is maintained. */
4219 if ((old_alignment || common)
4220 && h->root.type != bfd_link_hash_common)
4222 unsigned int common_align;
4223 unsigned int normal_align;
4224 unsigned int symbol_align;
4228 symbol_align = ffs (h->root.u.def.value) - 1;
4229 if (h->root.u.def.section->owner != NULL
4230 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4232 normal_align = h->root.u.def.section->alignment_power;
4233 if (normal_align > symbol_align)
4234 normal_align = symbol_align;
4237 normal_align = symbol_align;
4241 common_align = old_alignment;
4242 common_bfd = old_bfd;
4247 common_align = bfd_log2 (isym->st_value);
4249 normal_bfd = old_bfd;
4252 if (normal_align < common_align)
4254 /* PR binutils/2735 */
4255 if (normal_bfd == NULL)
4256 (*_bfd_error_handler)
4257 (_("Warning: alignment %u of common symbol `%s' in %B"
4258 " is greater than the alignment (%u) of its section %A"),
4259 common_bfd, h->root.u.def.section,
4260 1 << common_align, name, 1 << normal_align);
4262 (*_bfd_error_handler)
4263 (_("Warning: alignment %u of symbol `%s' in %B"
4264 " is smaller than %u in %B"),
4265 normal_bfd, common_bfd,
4266 1 << normal_align, name, 1 << common_align);
4270 /* Remember the symbol size if it isn't undefined. */
4271 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4272 && (definition || h->size == 0))
4275 && h->size != isym->st_size
4276 && ! size_change_ok)
4277 (*_bfd_error_handler)
4278 (_("Warning: size of symbol `%s' changed"
4279 " from %lu in %B to %lu in %B"),
4281 name, (unsigned long) h->size,
4282 (unsigned long) isym->st_size);
4284 h->size = isym->st_size;
4287 /* If this is a common symbol, then we always want H->SIZE
4288 to be the size of the common symbol. The code just above
4289 won't fix the size if a common symbol becomes larger. We
4290 don't warn about a size change here, because that is
4291 covered by --warn-common. Allow changed between different
4293 if (h->root.type == bfd_link_hash_common)
4294 h->size = h->root.u.c.size;
4296 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4297 && (definition || h->type == STT_NOTYPE))
4299 if (h->type != STT_NOTYPE
4300 && h->type != ELF_ST_TYPE (isym->st_info)
4301 && ! type_change_ok)
4302 (*_bfd_error_handler)
4303 (_("Warning: type of symbol `%s' changed"
4304 " from %d to %d in %B"),
4305 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
4307 h->type = ELF_ST_TYPE (isym->st_info);
4310 /* Merge st_other field. */
4311 elf_merge_st_other (abfd, h, isym, definition, dynamic);
4313 /* Set a flag in the hash table entry indicating the type of
4314 reference or definition we just found. Keep a count of
4315 the number of dynamic symbols we find. A dynamic symbol
4316 is one which is referenced or defined by both a regular
4317 object and a shared object. */
4324 if (bind != STB_WEAK)
4325 h->ref_regular_nonweak = 1;
4337 if (! info->executable
4350 || (h->u.weakdef != NULL
4352 && h->u.weakdef->dynindx != -1))
4356 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
4358 /* We don't want to make debug symbol dynamic. */
4359 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4363 /* Check to see if we need to add an indirect symbol for
4364 the default name. */
4365 if (definition || h->root.type == bfd_link_hash_common)
4366 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4367 &sec, &value, &dynsym,
4369 goto error_free_vers;
4371 if (definition && !dynamic)
4373 char *p = strchr (name, ELF_VER_CHR);
4374 if (p != NULL && p[1] != ELF_VER_CHR)
4376 /* Queue non-default versions so that .symver x, x@FOO
4377 aliases can be checked. */
4380 amt = ((isymend - isym + 1)
4381 * sizeof (struct elf_link_hash_entry *));
4382 nondeflt_vers = bfd_malloc (amt);
4384 goto error_free_vers;
4386 nondeflt_vers[nondeflt_vers_cnt++] = h;
4390 if (dynsym && h->dynindx == -1)
4392 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4393 goto error_free_vers;
4394 if (h->u.weakdef != NULL
4396 && h->u.weakdef->dynindx == -1)
4398 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4399 goto error_free_vers;
4402 else if (dynsym && h->dynindx != -1)
4403 /* If the symbol already has a dynamic index, but
4404 visibility says it should not be visible, turn it into
4406 switch (ELF_ST_VISIBILITY (h->other))
4410 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4421 const char *soname = elf_dt_name (abfd);
4423 /* A symbol from a library loaded via DT_NEEDED of some
4424 other library is referenced by a regular object.
4425 Add a DT_NEEDED entry for it. Issue an error if
4426 --no-add-needed is used. */
4427 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4429 (*_bfd_error_handler)
4430 (_("%s: invalid DSO for symbol `%s' definition"),
4432 bfd_set_error (bfd_error_bad_value);
4433 goto error_free_vers;
4436 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4439 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4441 goto error_free_vers;
4443 BFD_ASSERT (ret == 0);
4448 if (extversym != NULL)
4454 if (isymbuf != NULL)
4460 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4464 /* Restore the symbol table. */
4465 if (bed->as_needed_cleanup)
4466 (*bed->as_needed_cleanup) (abfd, info);
4467 old_hash = (char *) old_tab + tabsize;
4468 old_ent = (char *) old_hash + hashsize;
4469 sym_hash = elf_sym_hashes (abfd);
4470 htab->root.table.table = old_table;
4471 htab->root.table.size = old_size;
4472 htab->root.table.count = old_count;
4473 memcpy (htab->root.table.table, old_tab, tabsize);
4474 memcpy (sym_hash, old_hash, hashsize);
4475 htab->root.undefs = old_undefs;
4476 htab->root.undefs_tail = old_undefs_tail;
4477 for (i = 0; i < htab->root.table.size; i++)
4479 struct bfd_hash_entry *p;
4480 struct elf_link_hash_entry *h;
4482 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4484 h = (struct elf_link_hash_entry *) p;
4485 if (h->root.type == bfd_link_hash_warning)
4486 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4487 if (h->dynindx >= old_dynsymcount)
4488 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4490 memcpy (p, old_ent, htab->root.table.entsize);
4491 old_ent = (char *) old_ent + htab->root.table.entsize;
4492 h = (struct elf_link_hash_entry *) p;
4493 if (h->root.type == bfd_link_hash_warning)
4495 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4496 old_ent = (char *) old_ent + htab->root.table.entsize;
4501 /* Make a special call to the linker "notice" function to
4502 tell it that symbols added for crefs may need to be removed. */
4503 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4505 goto error_free_vers;
4508 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4510 if (nondeflt_vers != NULL)
4511 free (nondeflt_vers);
4515 if (old_tab != NULL)
4517 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4519 goto error_free_vers;
4524 /* Now that all the symbols from this input file are created, handle
4525 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4526 if (nondeflt_vers != NULL)
4528 bfd_size_type cnt, symidx;
4530 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4532 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4533 char *shortname, *p;
4535 p = strchr (h->root.root.string, ELF_VER_CHR);
4537 || (h->root.type != bfd_link_hash_defined
4538 && h->root.type != bfd_link_hash_defweak))
4541 amt = p - h->root.root.string;
4542 shortname = bfd_malloc (amt + 1);
4544 goto error_free_vers;
4545 memcpy (shortname, h->root.root.string, amt);
4546 shortname[amt] = '\0';
4548 hi = (struct elf_link_hash_entry *)
4549 bfd_link_hash_lookup (&htab->root, shortname,
4550 FALSE, FALSE, FALSE);
4552 && hi->root.type == h->root.type
4553 && hi->root.u.def.value == h->root.u.def.value
4554 && hi->root.u.def.section == h->root.u.def.section)
4556 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4557 hi->root.type = bfd_link_hash_indirect;
4558 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4559 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4560 sym_hash = elf_sym_hashes (abfd);
4562 for (symidx = 0; symidx < extsymcount; ++symidx)
4563 if (sym_hash[symidx] == hi)
4565 sym_hash[symidx] = h;
4571 free (nondeflt_vers);
4572 nondeflt_vers = NULL;
4575 /* Now set the weakdefs field correctly for all the weak defined
4576 symbols we found. The only way to do this is to search all the
4577 symbols. Since we only need the information for non functions in
4578 dynamic objects, that's the only time we actually put anything on
4579 the list WEAKS. We need this information so that if a regular
4580 object refers to a symbol defined weakly in a dynamic object, the
4581 real symbol in the dynamic object is also put in the dynamic
4582 symbols; we also must arrange for both symbols to point to the
4583 same memory location. We could handle the general case of symbol
4584 aliasing, but a general symbol alias can only be generated in
4585 assembler code, handling it correctly would be very time
4586 consuming, and other ELF linkers don't handle general aliasing
4590 struct elf_link_hash_entry **hpp;
4591 struct elf_link_hash_entry **hppend;
4592 struct elf_link_hash_entry **sorted_sym_hash;
4593 struct elf_link_hash_entry *h;
4596 /* Since we have to search the whole symbol list for each weak
4597 defined symbol, search time for N weak defined symbols will be
4598 O(N^2). Binary search will cut it down to O(NlogN). */
4599 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4600 sorted_sym_hash = bfd_malloc (amt);
4601 if (sorted_sym_hash == NULL)
4603 sym_hash = sorted_sym_hash;
4604 hpp = elf_sym_hashes (abfd);
4605 hppend = hpp + extsymcount;
4607 for (; hpp < hppend; hpp++)
4611 && h->root.type == bfd_link_hash_defined
4612 && !bed->is_function_type (h->type))
4620 qsort (sorted_sym_hash, sym_count,
4621 sizeof (struct elf_link_hash_entry *),
4624 while (weaks != NULL)
4626 struct elf_link_hash_entry *hlook;
4633 weaks = hlook->u.weakdef;
4634 hlook->u.weakdef = NULL;
4636 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4637 || hlook->root.type == bfd_link_hash_defweak
4638 || hlook->root.type == bfd_link_hash_common
4639 || hlook->root.type == bfd_link_hash_indirect);
4640 slook = hlook->root.u.def.section;
4641 vlook = hlook->root.u.def.value;
4648 bfd_signed_vma vdiff;
4650 h = sorted_sym_hash [idx];
4651 vdiff = vlook - h->root.u.def.value;
4658 long sdiff = slook->id - h->root.u.def.section->id;
4671 /* We didn't find a value/section match. */
4675 for (i = ilook; i < sym_count; i++)
4677 h = sorted_sym_hash [i];
4679 /* Stop if value or section doesn't match. */
4680 if (h->root.u.def.value != vlook
4681 || h->root.u.def.section != slook)
4683 else if (h != hlook)
4685 hlook->u.weakdef = h;
4687 /* If the weak definition is in the list of dynamic
4688 symbols, make sure the real definition is put
4690 if (hlook->dynindx != -1 && h->dynindx == -1)
4692 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4695 free (sorted_sym_hash);
4700 /* If the real definition is in the list of dynamic
4701 symbols, make sure the weak definition is put
4702 there as well. If we don't do this, then the
4703 dynamic loader might not merge the entries for the
4704 real definition and the weak definition. */
4705 if (h->dynindx != -1 && hlook->dynindx == -1)
4707 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4708 goto err_free_sym_hash;
4715 free (sorted_sym_hash);
4718 if (bed->check_directives
4719 && !(*bed->check_directives) (abfd, info))
4722 /* If this object is the same format as the output object, and it is
4723 not a shared library, then let the backend look through the
4726 This is required to build global offset table entries and to
4727 arrange for dynamic relocs. It is not required for the
4728 particular common case of linking non PIC code, even when linking
4729 against shared libraries, but unfortunately there is no way of
4730 knowing whether an object file has been compiled PIC or not.
4731 Looking through the relocs is not particularly time consuming.
4732 The problem is that we must either (1) keep the relocs in memory,
4733 which causes the linker to require additional runtime memory or
4734 (2) read the relocs twice from the input file, which wastes time.
4735 This would be a good case for using mmap.
4737 I have no idea how to handle linking PIC code into a file of a
4738 different format. It probably can't be done. */
4740 && is_elf_hash_table (htab)
4741 && bed->check_relocs != NULL
4742 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4746 for (o = abfd->sections; o != NULL; o = o->next)
4748 Elf_Internal_Rela *internal_relocs;
4751 if ((o->flags & SEC_RELOC) == 0
4752 || o->reloc_count == 0
4753 || ((info->strip == strip_all || info->strip == strip_debugger)
4754 && (o->flags & SEC_DEBUGGING) != 0)
4755 || bfd_is_abs_section (o->output_section))
4758 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4760 if (internal_relocs == NULL)
4763 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4765 if (elf_section_data (o)->relocs != internal_relocs)
4766 free (internal_relocs);
4773 /* If this is a non-traditional link, try to optimize the handling
4774 of the .stab/.stabstr sections. */
4776 && ! info->traditional_format
4777 && is_elf_hash_table (htab)
4778 && (info->strip != strip_all && info->strip != strip_debugger))
4782 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4783 if (stabstr != NULL)
4785 bfd_size_type string_offset = 0;
4788 for (stab = abfd->sections; stab; stab = stab->next)
4789 if (CONST_STRNEQ (stab->name, ".stab")
4790 && (!stab->name[5] ||
4791 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4792 && (stab->flags & SEC_MERGE) == 0
4793 && !bfd_is_abs_section (stab->output_section))
4795 struct bfd_elf_section_data *secdata;
4797 secdata = elf_section_data (stab);
4798 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4799 stabstr, &secdata->sec_info,
4802 if (secdata->sec_info)
4803 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4808 if (is_elf_hash_table (htab) && add_needed)
4810 /* Add this bfd to the loaded list. */
4811 struct elf_link_loaded_list *n;
4813 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4817 n->next = htab->loaded;
4824 if (old_tab != NULL)
4826 if (nondeflt_vers != NULL)
4827 free (nondeflt_vers);
4828 if (extversym != NULL)
4831 if (isymbuf != NULL)
4837 /* Return the linker hash table entry of a symbol that might be
4838 satisfied by an archive symbol. Return -1 on error. */
4840 struct elf_link_hash_entry *
4841 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4842 struct bfd_link_info *info,
4845 struct elf_link_hash_entry *h;
4849 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4853 /* If this is a default version (the name contains @@), look up the
4854 symbol again with only one `@' as well as without the version.
4855 The effect is that references to the symbol with and without the
4856 version will be matched by the default symbol in the archive. */
4858 p = strchr (name, ELF_VER_CHR);
4859 if (p == NULL || p[1] != ELF_VER_CHR)
4862 /* First check with only one `@'. */
4863 len = strlen (name);
4864 copy = bfd_alloc (abfd, len);
4866 return (struct elf_link_hash_entry *) 0 - 1;
4868 first = p - name + 1;
4869 memcpy (copy, name, first);
4870 memcpy (copy + first, name + first + 1, len - first);
4872 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4875 /* We also need to check references to the symbol without the
4877 copy[first - 1] = '\0';
4878 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4879 FALSE, FALSE, FALSE);
4882 bfd_release (abfd, copy);
4886 /* Add symbols from an ELF archive file to the linker hash table. We
4887 don't use _bfd_generic_link_add_archive_symbols because of a
4888 problem which arises on UnixWare. The UnixWare libc.so is an
4889 archive which includes an entry libc.so.1 which defines a bunch of
4890 symbols. The libc.so archive also includes a number of other
4891 object files, which also define symbols, some of which are the same
4892 as those defined in libc.so.1. Correct linking requires that we
4893 consider each object file in turn, and include it if it defines any
4894 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4895 this; it looks through the list of undefined symbols, and includes
4896 any object file which defines them. When this algorithm is used on
4897 UnixWare, it winds up pulling in libc.so.1 early and defining a
4898 bunch of symbols. This means that some of the other objects in the
4899 archive are not included in the link, which is incorrect since they
4900 precede libc.so.1 in the archive.
4902 Fortunately, ELF archive handling is simpler than that done by
4903 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4904 oddities. In ELF, if we find a symbol in the archive map, and the
4905 symbol is currently undefined, we know that we must pull in that
4908 Unfortunately, we do have to make multiple passes over the symbol
4909 table until nothing further is resolved. */
4912 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4915 bfd_boolean *defined = NULL;
4916 bfd_boolean *included = NULL;
4920 const struct elf_backend_data *bed;
4921 struct elf_link_hash_entry * (*archive_symbol_lookup)
4922 (bfd *, struct bfd_link_info *, const char *);
4924 if (! bfd_has_map (abfd))
4926 /* An empty archive is a special case. */
4927 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4929 bfd_set_error (bfd_error_no_armap);
4933 /* Keep track of all symbols we know to be already defined, and all
4934 files we know to be already included. This is to speed up the
4935 second and subsequent passes. */
4936 c = bfd_ardata (abfd)->symdef_count;
4940 amt *= sizeof (bfd_boolean);
4941 defined = bfd_zmalloc (amt);
4942 included = bfd_zmalloc (amt);
4943 if (defined == NULL || included == NULL)
4946 symdefs = bfd_ardata (abfd)->symdefs;
4947 bed = get_elf_backend_data (abfd);
4948 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4961 symdefend = symdef + c;
4962 for (i = 0; symdef < symdefend; symdef++, i++)
4964 struct elf_link_hash_entry *h;
4966 struct bfd_link_hash_entry *undefs_tail;
4969 if (defined[i] || included[i])
4971 if (symdef->file_offset == last)
4977 h = archive_symbol_lookup (abfd, info, symdef->name);
4978 if (h == (struct elf_link_hash_entry *) 0 - 1)
4984 if (h->root.type == bfd_link_hash_common)
4986 /* We currently have a common symbol. The archive map contains
4987 a reference to this symbol, so we may want to include it. We
4988 only want to include it however, if this archive element
4989 contains a definition of the symbol, not just another common
4992 Unfortunately some archivers (including GNU ar) will put
4993 declarations of common symbols into their archive maps, as
4994 well as real definitions, so we cannot just go by the archive
4995 map alone. Instead we must read in the element's symbol
4996 table and check that to see what kind of symbol definition
4998 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5001 else if (h->root.type != bfd_link_hash_undefined)
5003 if (h->root.type != bfd_link_hash_undefweak)
5008 /* We need to include this archive member. */
5009 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5010 if (element == NULL)
5013 if (! bfd_check_format (element, bfd_object))
5016 /* Doublecheck that we have not included this object
5017 already--it should be impossible, but there may be
5018 something wrong with the archive. */
5019 if (element->archive_pass != 0)
5021 bfd_set_error (bfd_error_bad_value);
5024 element->archive_pass = 1;
5026 undefs_tail = info->hash->undefs_tail;
5028 if (! (*info->callbacks->add_archive_element) (info, element,
5031 if (! bfd_link_add_symbols (element, info))
5034 /* If there are any new undefined symbols, we need to make
5035 another pass through the archive in order to see whether
5036 they can be defined. FIXME: This isn't perfect, because
5037 common symbols wind up on undefs_tail and because an
5038 undefined symbol which is defined later on in this pass
5039 does not require another pass. This isn't a bug, but it
5040 does make the code less efficient than it could be. */
5041 if (undefs_tail != info->hash->undefs_tail)
5044 /* Look backward to mark all symbols from this object file
5045 which we have already seen in this pass. */
5049 included[mark] = TRUE;
5054 while (symdefs[mark].file_offset == symdef->file_offset);
5056 /* We mark subsequent symbols from this object file as we go
5057 on through the loop. */
5058 last = symdef->file_offset;
5069 if (defined != NULL)
5071 if (included != NULL)
5076 /* Given an ELF BFD, add symbols to the global hash table as
5080 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5082 switch (bfd_get_format (abfd))
5085 return elf_link_add_object_symbols (abfd, info);
5087 return elf_link_add_archive_symbols (abfd, info);
5089 bfd_set_error (bfd_error_wrong_format);
5094 struct hash_codes_info
5096 unsigned long *hashcodes;
5100 /* This function will be called though elf_link_hash_traverse to store
5101 all hash value of the exported symbols in an array. */
5104 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5106 struct hash_codes_info *inf = data;
5112 if (h->root.type == bfd_link_hash_warning)
5113 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5115 /* Ignore indirect symbols. These are added by the versioning code. */
5116 if (h->dynindx == -1)
5119 name = h->root.root.string;
5120 p = strchr (name, ELF_VER_CHR);
5123 alc = bfd_malloc (p - name + 1);
5129 memcpy (alc, name, p - name);
5130 alc[p - name] = '\0';
5134 /* Compute the hash value. */
5135 ha = bfd_elf_hash (name);
5137 /* Store the found hash value in the array given as the argument. */
5138 *(inf->hashcodes)++ = ha;
5140 /* And store it in the struct so that we can put it in the hash table
5142 h->u.elf_hash_value = ha;
5150 struct collect_gnu_hash_codes
5153 const struct elf_backend_data *bed;
5154 unsigned long int nsyms;
5155 unsigned long int maskbits;
5156 unsigned long int *hashcodes;
5157 unsigned long int *hashval;
5158 unsigned long int *indx;
5159 unsigned long int *counts;
5162 long int min_dynindx;
5163 unsigned long int bucketcount;
5164 unsigned long int symindx;
5165 long int local_indx;
5166 long int shift1, shift2;
5167 unsigned long int mask;
5171 /* This function will be called though elf_link_hash_traverse to store
5172 all hash value of the exported symbols in an array. */
5175 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5177 struct collect_gnu_hash_codes *s = data;
5183 if (h->root.type == bfd_link_hash_warning)
5184 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5186 /* Ignore indirect symbols. These are added by the versioning code. */
5187 if (h->dynindx == -1)
5190 /* Ignore also local symbols and undefined symbols. */
5191 if (! (*s->bed->elf_hash_symbol) (h))
5194 name = h->root.root.string;
5195 p = strchr (name, ELF_VER_CHR);
5198 alc = bfd_malloc (p - name + 1);
5204 memcpy (alc, name, p - name);
5205 alc[p - name] = '\0';
5209 /* Compute the hash value. */
5210 ha = bfd_elf_gnu_hash (name);
5212 /* Store the found hash value in the array for compute_bucket_count,
5213 and also for .dynsym reordering purposes. */
5214 s->hashcodes[s->nsyms] = ha;
5215 s->hashval[h->dynindx] = ha;
5217 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5218 s->min_dynindx = h->dynindx;
5226 /* This function will be called though elf_link_hash_traverse to do
5227 final dynaminc symbol renumbering. */
5230 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5232 struct collect_gnu_hash_codes *s = data;
5233 unsigned long int bucket;
5234 unsigned long int val;
5236 if (h->root.type == bfd_link_hash_warning)
5237 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5239 /* Ignore indirect symbols. */
5240 if (h->dynindx == -1)
5243 /* Ignore also local symbols and undefined symbols. */
5244 if (! (*s->bed->elf_hash_symbol) (h))
5246 if (h->dynindx >= s->min_dynindx)
5247 h->dynindx = s->local_indx++;
5251 bucket = s->hashval[h->dynindx] % s->bucketcount;
5252 val = (s->hashval[h->dynindx] >> s->shift1)
5253 & ((s->maskbits >> s->shift1) - 1);
5254 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5256 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5257 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5258 if (s->counts[bucket] == 1)
5259 /* Last element terminates the chain. */
5261 bfd_put_32 (s->output_bfd, val,
5262 s->contents + (s->indx[bucket] - s->symindx) * 4);
5263 --s->counts[bucket];
5264 h->dynindx = s->indx[bucket]++;
5268 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5271 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5273 return !(h->forced_local
5274 || h->root.type == bfd_link_hash_undefined
5275 || h->root.type == bfd_link_hash_undefweak
5276 || ((h->root.type == bfd_link_hash_defined
5277 || h->root.type == bfd_link_hash_defweak)
5278 && h->root.u.def.section->output_section == NULL));
5281 /* Array used to determine the number of hash table buckets to use
5282 based on the number of symbols there are. If there are fewer than
5283 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5284 fewer than 37 we use 17 buckets, and so forth. We never use more
5285 than 32771 buckets. */
5287 static const size_t elf_buckets[] =
5289 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5293 /* Compute bucket count for hashing table. We do not use a static set
5294 of possible tables sizes anymore. Instead we determine for all
5295 possible reasonable sizes of the table the outcome (i.e., the
5296 number of collisions etc) and choose the best solution. The
5297 weighting functions are not too simple to allow the table to grow
5298 without bounds. Instead one of the weighting factors is the size.
5299 Therefore the result is always a good payoff between few collisions
5300 (= short chain lengths) and table size. */
5302 compute_bucket_count (struct bfd_link_info *info,
5303 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5304 unsigned long int nsyms,
5307 size_t best_size = 0;
5308 unsigned long int i;
5310 /* We have a problem here. The following code to optimize the table
5311 size requires an integer type with more the 32 bits. If
5312 BFD_HOST_U_64_BIT is set we know about such a type. */
5313 #ifdef BFD_HOST_U_64_BIT
5318 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5319 bfd *dynobj = elf_hash_table (info)->dynobj;
5320 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5321 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5322 unsigned long int *counts;
5325 /* Possible optimization parameters: if we have NSYMS symbols we say
5326 that the hashing table must at least have NSYMS/4 and at most
5328 minsize = nsyms / 4;
5331 best_size = maxsize = nsyms * 2;
5336 if ((best_size & 31) == 0)
5340 /* Create array where we count the collisions in. We must use bfd_malloc
5341 since the size could be large. */
5343 amt *= sizeof (unsigned long int);
5344 counts = bfd_malloc (amt);
5348 /* Compute the "optimal" size for the hash table. The criteria is a
5349 minimal chain length. The minor criteria is (of course) the size
5351 for (i = minsize; i < maxsize; ++i)
5353 /* Walk through the array of hashcodes and count the collisions. */
5354 BFD_HOST_U_64_BIT max;
5355 unsigned long int j;
5356 unsigned long int fact;
5358 if (gnu_hash && (i & 31) == 0)
5361 memset (counts, '\0', i * sizeof (unsigned long int));
5363 /* Determine how often each hash bucket is used. */
5364 for (j = 0; j < nsyms; ++j)
5365 ++counts[hashcodes[j] % i];
5367 /* For the weight function we need some information about the
5368 pagesize on the target. This is information need not be 100%
5369 accurate. Since this information is not available (so far) we
5370 define it here to a reasonable default value. If it is crucial
5371 to have a better value some day simply define this value. */
5372 # ifndef BFD_TARGET_PAGESIZE
5373 # define BFD_TARGET_PAGESIZE (4096)
5376 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5378 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5381 /* Variant 1: optimize for short chains. We add the squares
5382 of all the chain lengths (which favors many small chain
5383 over a few long chains). */
5384 for (j = 0; j < i; ++j)
5385 max += counts[j] * counts[j];
5387 /* This adds penalties for the overall size of the table. */
5388 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5391 /* Variant 2: Optimize a lot more for small table. Here we
5392 also add squares of the size but we also add penalties for
5393 empty slots (the +1 term). */
5394 for (j = 0; j < i; ++j)
5395 max += (1 + counts[j]) * (1 + counts[j]);
5397 /* The overall size of the table is considered, but not as
5398 strong as in variant 1, where it is squared. */
5399 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5403 /* Compare with current best results. */
5404 if (max < best_chlen)
5414 #endif /* defined (BFD_HOST_U_64_BIT) */
5416 /* This is the fallback solution if no 64bit type is available or if we
5417 are not supposed to spend much time on optimizations. We select the
5418 bucket count using a fixed set of numbers. */
5419 for (i = 0; elf_buckets[i] != 0; i++)
5421 best_size = elf_buckets[i];
5422 if (nsyms < elf_buckets[i + 1])
5425 if (gnu_hash && best_size < 2)
5432 /* Set up the sizes and contents of the ELF dynamic sections. This is
5433 called by the ELF linker emulation before_allocation routine. We
5434 must set the sizes of the sections before the linker sets the
5435 addresses of the various sections. */
5438 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5441 const char *filter_shlib,
5442 const char * const *auxiliary_filters,
5443 struct bfd_link_info *info,
5444 asection **sinterpptr,
5445 struct bfd_elf_version_tree *verdefs)
5447 bfd_size_type soname_indx;
5449 const struct elf_backend_data *bed;
5450 struct elf_assign_sym_version_info asvinfo;
5454 soname_indx = (bfd_size_type) -1;
5456 if (!is_elf_hash_table (info->hash))
5459 bed = get_elf_backend_data (output_bfd);
5460 if (info->execstack)
5461 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5462 else if (info->noexecstack)
5463 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5467 asection *notesec = NULL;
5470 for (inputobj = info->input_bfds;
5472 inputobj = inputobj->link_next)
5476 if (inputobj->flags & (DYNAMIC | EXEC_P | BFD_LINKER_CREATED))
5478 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5481 if (s->flags & SEC_CODE)
5485 else if (bed->default_execstack)
5490 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5491 if (exec && info->relocatable
5492 && notesec->output_section != bfd_abs_section_ptr)
5493 notesec->output_section->flags |= SEC_CODE;
5497 /* Any syms created from now on start with -1 in
5498 got.refcount/offset and plt.refcount/offset. */
5499 elf_hash_table (info)->init_got_refcount
5500 = elf_hash_table (info)->init_got_offset;
5501 elf_hash_table (info)->init_plt_refcount
5502 = elf_hash_table (info)->init_plt_offset;
5504 /* The backend may have to create some sections regardless of whether
5505 we're dynamic or not. */
5506 if (bed->elf_backend_always_size_sections
5507 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5510 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5513 dynobj = elf_hash_table (info)->dynobj;
5515 /* If there were no dynamic objects in the link, there is nothing to
5520 if (elf_hash_table (info)->dynamic_sections_created)
5522 struct elf_info_failed eif;
5523 struct elf_link_hash_entry *h;
5525 struct bfd_elf_version_tree *t;
5526 struct bfd_elf_version_expr *d;
5528 bfd_boolean all_defined;
5530 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5531 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5535 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5537 if (soname_indx == (bfd_size_type) -1
5538 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5544 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5546 info->flags |= DF_SYMBOLIC;
5553 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5555 if (indx == (bfd_size_type) -1
5556 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5559 if (info->new_dtags)
5561 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5562 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5567 if (filter_shlib != NULL)
5571 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5572 filter_shlib, TRUE);
5573 if (indx == (bfd_size_type) -1
5574 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5578 if (auxiliary_filters != NULL)
5580 const char * const *p;
5582 for (p = auxiliary_filters; *p != NULL; p++)
5586 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5588 if (indx == (bfd_size_type) -1
5589 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5595 eif.verdefs = verdefs;
5598 /* If we are supposed to export all symbols into the dynamic symbol
5599 table (this is not the normal case), then do so. */
5600 if (info->export_dynamic
5601 || (info->executable && info->dynamic))
5603 elf_link_hash_traverse (elf_hash_table (info),
5604 _bfd_elf_export_symbol,
5610 /* Make all global versions with definition. */
5611 for (t = verdefs; t != NULL; t = t->next)
5612 for (d = t->globals.list; d != NULL; d = d->next)
5613 if (!d->symver && d->literal)
5615 const char *verstr, *name;
5616 size_t namelen, verlen, newlen;
5618 struct elf_link_hash_entry *newh;
5621 namelen = strlen (name);
5623 verlen = strlen (verstr);
5624 newlen = namelen + verlen + 3;
5626 newname = bfd_malloc (newlen);
5627 if (newname == NULL)
5629 memcpy (newname, name, namelen);
5631 /* Check the hidden versioned definition. */
5632 p = newname + namelen;
5634 memcpy (p, verstr, verlen + 1);
5635 newh = elf_link_hash_lookup (elf_hash_table (info),
5636 newname, FALSE, FALSE,
5639 || (newh->root.type != bfd_link_hash_defined
5640 && newh->root.type != bfd_link_hash_defweak))
5642 /* Check the default versioned definition. */
5644 memcpy (p, verstr, verlen + 1);
5645 newh = elf_link_hash_lookup (elf_hash_table (info),
5646 newname, FALSE, FALSE,
5651 /* Mark this version if there is a definition and it is
5652 not defined in a shared object. */
5654 && !newh->def_dynamic
5655 && (newh->root.type == bfd_link_hash_defined
5656 || newh->root.type == bfd_link_hash_defweak))
5660 /* Attach all the symbols to their version information. */
5661 asvinfo.output_bfd = output_bfd;
5662 asvinfo.info = info;
5663 asvinfo.verdefs = verdefs;
5664 asvinfo.failed = FALSE;
5666 elf_link_hash_traverse (elf_hash_table (info),
5667 _bfd_elf_link_assign_sym_version,
5672 if (!info->allow_undefined_version)
5674 /* Check if all global versions have a definition. */
5676 for (t = verdefs; t != NULL; t = t->next)
5677 for (d = t->globals.list; d != NULL; d = d->next)
5678 if (d->literal && !d->symver && !d->script)
5680 (*_bfd_error_handler)
5681 (_("%s: undefined version: %s"),
5682 d->pattern, t->name);
5683 all_defined = FALSE;
5688 bfd_set_error (bfd_error_bad_value);
5693 /* Find all symbols which were defined in a dynamic object and make
5694 the backend pick a reasonable value for them. */
5695 elf_link_hash_traverse (elf_hash_table (info),
5696 _bfd_elf_adjust_dynamic_symbol,
5701 /* Add some entries to the .dynamic section. We fill in some of the
5702 values later, in bfd_elf_final_link, but we must add the entries
5703 now so that we know the final size of the .dynamic section. */
5705 /* If there are initialization and/or finalization functions to
5706 call then add the corresponding DT_INIT/DT_FINI entries. */
5707 h = (info->init_function
5708 ? elf_link_hash_lookup (elf_hash_table (info),
5709 info->init_function, FALSE,
5716 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5719 h = (info->fini_function
5720 ? elf_link_hash_lookup (elf_hash_table (info),
5721 info->fini_function, FALSE,
5728 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5732 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5733 if (s != NULL && s->linker_has_input)
5735 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5736 if (! info->executable)
5741 for (sub = info->input_bfds; sub != NULL;
5742 sub = sub->link_next)
5743 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5744 for (o = sub->sections; o != NULL; o = o->next)
5745 if (elf_section_data (o)->this_hdr.sh_type
5746 == SHT_PREINIT_ARRAY)
5748 (*_bfd_error_handler)
5749 (_("%B: .preinit_array section is not allowed in DSO"),
5754 bfd_set_error (bfd_error_nonrepresentable_section);
5758 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5759 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5762 s = bfd_get_section_by_name (output_bfd, ".init_array");
5763 if (s != NULL && s->linker_has_input)
5765 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5766 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5769 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5770 if (s != NULL && s->linker_has_input)
5772 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5773 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5777 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5778 /* If .dynstr is excluded from the link, we don't want any of
5779 these tags. Strictly, we should be checking each section
5780 individually; This quick check covers for the case where
5781 someone does a /DISCARD/ : { *(*) }. */
5782 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5784 bfd_size_type strsize;
5786 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5787 if ((info->emit_hash
5788 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5789 || (info->emit_gnu_hash
5790 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5791 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5792 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5793 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5794 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5795 bed->s->sizeof_sym))
5800 /* The backend must work out the sizes of all the other dynamic
5802 if (bed->elf_backend_size_dynamic_sections
5803 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5806 if (elf_hash_table (info)->dynamic_sections_created)
5808 unsigned long section_sym_count;
5811 /* Set up the version definition section. */
5812 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5813 BFD_ASSERT (s != NULL);
5815 /* We may have created additional version definitions if we are
5816 just linking a regular application. */
5817 verdefs = asvinfo.verdefs;
5819 /* Skip anonymous version tag. */
5820 if (verdefs != NULL && verdefs->vernum == 0)
5821 verdefs = verdefs->next;
5823 if (verdefs == NULL && !info->create_default_symver)
5824 s->flags |= SEC_EXCLUDE;
5829 struct bfd_elf_version_tree *t;
5831 Elf_Internal_Verdef def;
5832 Elf_Internal_Verdaux defaux;
5833 struct bfd_link_hash_entry *bh;
5834 struct elf_link_hash_entry *h;
5840 /* Make space for the base version. */
5841 size += sizeof (Elf_External_Verdef);
5842 size += sizeof (Elf_External_Verdaux);
5845 /* Make space for the default version. */
5846 if (info->create_default_symver)
5848 size += sizeof (Elf_External_Verdef);
5852 for (t = verdefs; t != NULL; t = t->next)
5854 struct bfd_elf_version_deps *n;
5856 size += sizeof (Elf_External_Verdef);
5857 size += sizeof (Elf_External_Verdaux);
5860 for (n = t->deps; n != NULL; n = n->next)
5861 size += sizeof (Elf_External_Verdaux);
5865 s->contents = bfd_alloc (output_bfd, s->size);
5866 if (s->contents == NULL && s->size != 0)
5869 /* Fill in the version definition section. */
5873 def.vd_version = VER_DEF_CURRENT;
5874 def.vd_flags = VER_FLG_BASE;
5877 if (info->create_default_symver)
5879 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5880 def.vd_next = sizeof (Elf_External_Verdef);
5884 def.vd_aux = sizeof (Elf_External_Verdef);
5885 def.vd_next = (sizeof (Elf_External_Verdef)
5886 + sizeof (Elf_External_Verdaux));
5889 if (soname_indx != (bfd_size_type) -1)
5891 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5893 def.vd_hash = bfd_elf_hash (soname);
5894 defaux.vda_name = soname_indx;
5901 name = lbasename (output_bfd->filename);
5902 def.vd_hash = bfd_elf_hash (name);
5903 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5905 if (indx == (bfd_size_type) -1)
5907 defaux.vda_name = indx;
5909 defaux.vda_next = 0;
5911 _bfd_elf_swap_verdef_out (output_bfd, &def,
5912 (Elf_External_Verdef *) p);
5913 p += sizeof (Elf_External_Verdef);
5914 if (info->create_default_symver)
5916 /* Add a symbol representing this version. */
5918 if (! (_bfd_generic_link_add_one_symbol
5919 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5921 get_elf_backend_data (dynobj)->collect, &bh)))
5923 h = (struct elf_link_hash_entry *) bh;
5926 h->type = STT_OBJECT;
5927 h->verinfo.vertree = NULL;
5929 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5932 /* Create a duplicate of the base version with the same
5933 aux block, but different flags. */
5936 def.vd_aux = sizeof (Elf_External_Verdef);
5938 def.vd_next = (sizeof (Elf_External_Verdef)
5939 + sizeof (Elf_External_Verdaux));
5942 _bfd_elf_swap_verdef_out (output_bfd, &def,
5943 (Elf_External_Verdef *) p);
5944 p += sizeof (Elf_External_Verdef);
5946 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5947 (Elf_External_Verdaux *) p);
5948 p += sizeof (Elf_External_Verdaux);
5950 for (t = verdefs; t != NULL; t = t->next)
5953 struct bfd_elf_version_deps *n;
5956 for (n = t->deps; n != NULL; n = n->next)
5959 /* Add a symbol representing this version. */
5961 if (! (_bfd_generic_link_add_one_symbol
5962 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5964 get_elf_backend_data (dynobj)->collect, &bh)))
5966 h = (struct elf_link_hash_entry *) bh;
5969 h->type = STT_OBJECT;
5970 h->verinfo.vertree = t;
5972 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5975 def.vd_version = VER_DEF_CURRENT;
5977 if (t->globals.list == NULL
5978 && t->locals.list == NULL
5980 def.vd_flags |= VER_FLG_WEAK;
5981 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5982 def.vd_cnt = cdeps + 1;
5983 def.vd_hash = bfd_elf_hash (t->name);
5984 def.vd_aux = sizeof (Elf_External_Verdef);
5986 if (t->next != NULL)
5987 def.vd_next = (sizeof (Elf_External_Verdef)
5988 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5990 _bfd_elf_swap_verdef_out (output_bfd, &def,
5991 (Elf_External_Verdef *) p);
5992 p += sizeof (Elf_External_Verdef);
5994 defaux.vda_name = h->dynstr_index;
5995 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5997 defaux.vda_next = 0;
5998 if (t->deps != NULL)
5999 defaux.vda_next = sizeof (Elf_External_Verdaux);
6000 t->name_indx = defaux.vda_name;
6002 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6003 (Elf_External_Verdaux *) p);
6004 p += sizeof (Elf_External_Verdaux);
6006 for (n = t->deps; n != NULL; n = n->next)
6008 if (n->version_needed == NULL)
6010 /* This can happen if there was an error in the
6012 defaux.vda_name = 0;
6016 defaux.vda_name = n->version_needed->name_indx;
6017 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6020 if (n->next == NULL)
6021 defaux.vda_next = 0;
6023 defaux.vda_next = sizeof (Elf_External_Verdaux);
6025 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6026 (Elf_External_Verdaux *) p);
6027 p += sizeof (Elf_External_Verdaux);
6031 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6032 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6035 elf_tdata (output_bfd)->cverdefs = cdefs;
6038 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6040 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6043 else if (info->flags & DF_BIND_NOW)
6045 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6051 if (info->executable)
6052 info->flags_1 &= ~ (DF_1_INITFIRST
6055 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6059 /* Work out the size of the version reference section. */
6061 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
6062 BFD_ASSERT (s != NULL);
6064 struct elf_find_verdep_info sinfo;
6066 sinfo.output_bfd = output_bfd;
6068 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6069 if (sinfo.vers == 0)
6071 sinfo.failed = FALSE;
6073 elf_link_hash_traverse (elf_hash_table (info),
6074 _bfd_elf_link_find_version_dependencies,
6079 if (elf_tdata (output_bfd)->verref == NULL)
6080 s->flags |= SEC_EXCLUDE;
6083 Elf_Internal_Verneed *t;
6088 /* Build the version definition section. */
6091 for (t = elf_tdata (output_bfd)->verref;
6095 Elf_Internal_Vernaux *a;
6097 size += sizeof (Elf_External_Verneed);
6099 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6100 size += sizeof (Elf_External_Vernaux);
6104 s->contents = bfd_alloc (output_bfd, s->size);
6105 if (s->contents == NULL)
6109 for (t = elf_tdata (output_bfd)->verref;
6114 Elf_Internal_Vernaux *a;
6118 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6121 t->vn_version = VER_NEED_CURRENT;
6123 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6124 elf_dt_name (t->vn_bfd) != NULL
6125 ? elf_dt_name (t->vn_bfd)
6126 : lbasename (t->vn_bfd->filename),
6128 if (indx == (bfd_size_type) -1)
6131 t->vn_aux = sizeof (Elf_External_Verneed);
6132 if (t->vn_nextref == NULL)
6135 t->vn_next = (sizeof (Elf_External_Verneed)
6136 + caux * sizeof (Elf_External_Vernaux));
6138 _bfd_elf_swap_verneed_out (output_bfd, t,
6139 (Elf_External_Verneed *) p);
6140 p += sizeof (Elf_External_Verneed);
6142 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6144 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6145 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6146 a->vna_nodename, FALSE);
6147 if (indx == (bfd_size_type) -1)
6150 if (a->vna_nextptr == NULL)
6153 a->vna_next = sizeof (Elf_External_Vernaux);
6155 _bfd_elf_swap_vernaux_out (output_bfd, a,
6156 (Elf_External_Vernaux *) p);
6157 p += sizeof (Elf_External_Vernaux);
6161 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6162 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6165 elf_tdata (output_bfd)->cverrefs = crefs;
6169 if ((elf_tdata (output_bfd)->cverrefs == 0
6170 && elf_tdata (output_bfd)->cverdefs == 0)
6171 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6172 §ion_sym_count) == 0)
6174 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6175 s->flags |= SEC_EXCLUDE;
6181 /* Find the first non-excluded output section. We'll use its
6182 section symbol for some emitted relocs. */
6184 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6188 for (s = output_bfd->sections; s != NULL; s = s->next)
6189 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6190 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6192 elf_hash_table (info)->text_index_section = s;
6197 /* Find two non-excluded output sections, one for code, one for data.
6198 We'll use their section symbols for some emitted relocs. */
6200 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6204 /* Data first, since setting text_index_section changes
6205 _bfd_elf_link_omit_section_dynsym. */
6206 for (s = output_bfd->sections; s != NULL; s = s->next)
6207 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6208 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6210 elf_hash_table (info)->data_index_section = s;
6214 for (s = output_bfd->sections; s != NULL; s = s->next)
6215 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6216 == (SEC_ALLOC | SEC_READONLY))
6217 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6219 elf_hash_table (info)->text_index_section = s;
6223 if (elf_hash_table (info)->text_index_section == NULL)
6224 elf_hash_table (info)->text_index_section
6225 = elf_hash_table (info)->data_index_section;
6229 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6231 const struct elf_backend_data *bed;
6233 if (!is_elf_hash_table (info->hash))
6236 bed = get_elf_backend_data (output_bfd);
6237 (*bed->elf_backend_init_index_section) (output_bfd, info);
6239 if (elf_hash_table (info)->dynamic_sections_created)
6243 bfd_size_type dynsymcount;
6244 unsigned long section_sym_count;
6245 unsigned int dtagcount;
6247 dynobj = elf_hash_table (info)->dynobj;
6249 /* Assign dynsym indicies. In a shared library we generate a
6250 section symbol for each output section, which come first.
6251 Next come all of the back-end allocated local dynamic syms,
6252 followed by the rest of the global symbols. */
6254 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6255 §ion_sym_count);
6257 /* Work out the size of the symbol version section. */
6258 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6259 BFD_ASSERT (s != NULL);
6260 if (dynsymcount != 0
6261 && (s->flags & SEC_EXCLUDE) == 0)
6263 s->size = dynsymcount * sizeof (Elf_External_Versym);
6264 s->contents = bfd_zalloc (output_bfd, s->size);
6265 if (s->contents == NULL)
6268 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6272 /* Set the size of the .dynsym and .hash sections. We counted
6273 the number of dynamic symbols in elf_link_add_object_symbols.
6274 We will build the contents of .dynsym and .hash when we build
6275 the final symbol table, because until then we do not know the
6276 correct value to give the symbols. We built the .dynstr
6277 section as we went along in elf_link_add_object_symbols. */
6278 s = bfd_get_section_by_name (dynobj, ".dynsym");
6279 BFD_ASSERT (s != NULL);
6280 s->size = dynsymcount * bed->s->sizeof_sym;
6282 if (dynsymcount != 0)
6284 s->contents = bfd_alloc (output_bfd, s->size);
6285 if (s->contents == NULL)
6288 /* The first entry in .dynsym is a dummy symbol.
6289 Clear all the section syms, in case we don't output them all. */
6290 ++section_sym_count;
6291 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6294 elf_hash_table (info)->bucketcount = 0;
6296 /* Compute the size of the hashing table. As a side effect this
6297 computes the hash values for all the names we export. */
6298 if (info->emit_hash)
6300 unsigned long int *hashcodes;
6301 struct hash_codes_info hashinf;
6303 unsigned long int nsyms;
6305 size_t hash_entry_size;
6307 /* Compute the hash values for all exported symbols. At the same
6308 time store the values in an array so that we could use them for
6310 amt = dynsymcount * sizeof (unsigned long int);
6311 hashcodes = bfd_malloc (amt);
6312 if (hashcodes == NULL)
6314 hashinf.hashcodes = hashcodes;
6315 hashinf.error = FALSE;
6317 /* Put all hash values in HASHCODES. */
6318 elf_link_hash_traverse (elf_hash_table (info),
6319 elf_collect_hash_codes, &hashinf);
6326 nsyms = hashinf.hashcodes - hashcodes;
6328 = compute_bucket_count (info, hashcodes, nsyms, 0);
6331 if (bucketcount == 0)
6334 elf_hash_table (info)->bucketcount = bucketcount;
6336 s = bfd_get_section_by_name (dynobj, ".hash");
6337 BFD_ASSERT (s != NULL);
6338 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6339 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6340 s->contents = bfd_zalloc (output_bfd, s->size);
6341 if (s->contents == NULL)
6344 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6345 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6346 s->contents + hash_entry_size);
6349 if (info->emit_gnu_hash)
6352 unsigned char *contents;
6353 struct collect_gnu_hash_codes cinfo;
6357 memset (&cinfo, 0, sizeof (cinfo));
6359 /* Compute the hash values for all exported symbols. At the same
6360 time store the values in an array so that we could use them for
6362 amt = dynsymcount * 2 * sizeof (unsigned long int);
6363 cinfo.hashcodes = bfd_malloc (amt);
6364 if (cinfo.hashcodes == NULL)
6367 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6368 cinfo.min_dynindx = -1;
6369 cinfo.output_bfd = output_bfd;
6372 /* Put all hash values in HASHCODES. */
6373 elf_link_hash_traverse (elf_hash_table (info),
6374 elf_collect_gnu_hash_codes, &cinfo);
6377 free (cinfo.hashcodes);
6382 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6384 if (bucketcount == 0)
6386 free (cinfo.hashcodes);
6390 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6391 BFD_ASSERT (s != NULL);
6393 if (cinfo.nsyms == 0)
6395 /* Empty .gnu.hash section is special. */
6396 BFD_ASSERT (cinfo.min_dynindx == -1);
6397 free (cinfo.hashcodes);
6398 s->size = 5 * 4 + bed->s->arch_size / 8;
6399 contents = bfd_zalloc (output_bfd, s->size);
6400 if (contents == NULL)
6402 s->contents = contents;
6403 /* 1 empty bucket. */
6404 bfd_put_32 (output_bfd, 1, contents);
6405 /* SYMIDX above the special symbol 0. */
6406 bfd_put_32 (output_bfd, 1, contents + 4);
6407 /* Just one word for bitmask. */
6408 bfd_put_32 (output_bfd, 1, contents + 8);
6409 /* Only hash fn bloom filter. */
6410 bfd_put_32 (output_bfd, 0, contents + 12);
6411 /* No hashes are valid - empty bitmask. */
6412 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6413 /* No hashes in the only bucket. */
6414 bfd_put_32 (output_bfd, 0,
6415 contents + 16 + bed->s->arch_size / 8);
6419 unsigned long int maskwords, maskbitslog2;
6420 BFD_ASSERT (cinfo.min_dynindx != -1);
6422 maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
6423 if (maskbitslog2 < 3)
6425 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6426 maskbitslog2 = maskbitslog2 + 3;
6428 maskbitslog2 = maskbitslog2 + 2;
6429 if (bed->s->arch_size == 64)
6431 if (maskbitslog2 == 5)
6437 cinfo.mask = (1 << cinfo.shift1) - 1;
6438 cinfo.shift2 = maskbitslog2;
6439 cinfo.maskbits = 1 << maskbitslog2;
6440 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6441 amt = bucketcount * sizeof (unsigned long int) * 2;
6442 amt += maskwords * sizeof (bfd_vma);
6443 cinfo.bitmask = bfd_malloc (amt);
6444 if (cinfo.bitmask == NULL)
6446 free (cinfo.hashcodes);
6450 cinfo.counts = (void *) (cinfo.bitmask + maskwords);
6451 cinfo.indx = cinfo.counts + bucketcount;
6452 cinfo.symindx = dynsymcount - cinfo.nsyms;
6453 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6455 /* Determine how often each hash bucket is used. */
6456 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6457 for (i = 0; i < cinfo.nsyms; ++i)
6458 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6460 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6461 if (cinfo.counts[i] != 0)
6463 cinfo.indx[i] = cnt;
6464 cnt += cinfo.counts[i];
6466 BFD_ASSERT (cnt == dynsymcount);
6467 cinfo.bucketcount = bucketcount;
6468 cinfo.local_indx = cinfo.min_dynindx;
6470 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6471 s->size += cinfo.maskbits / 8;
6472 contents = bfd_zalloc (output_bfd, s->size);
6473 if (contents == NULL)
6475 free (cinfo.bitmask);
6476 free (cinfo.hashcodes);
6480 s->contents = contents;
6481 bfd_put_32 (output_bfd, bucketcount, contents);
6482 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6483 bfd_put_32 (output_bfd, maskwords, contents + 8);
6484 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6485 contents += 16 + cinfo.maskbits / 8;
6487 for (i = 0; i < bucketcount; ++i)
6489 if (cinfo.counts[i] == 0)
6490 bfd_put_32 (output_bfd, 0, contents);
6492 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6496 cinfo.contents = contents;
6498 /* Renumber dynamic symbols, populate .gnu.hash section. */
6499 elf_link_hash_traverse (elf_hash_table (info),
6500 elf_renumber_gnu_hash_syms, &cinfo);
6502 contents = s->contents + 16;
6503 for (i = 0; i < maskwords; ++i)
6505 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6507 contents += bed->s->arch_size / 8;
6510 free (cinfo.bitmask);
6511 free (cinfo.hashcodes);
6515 s = bfd_get_section_by_name (dynobj, ".dynstr");
6516 BFD_ASSERT (s != NULL);
6518 elf_finalize_dynstr (output_bfd, info);
6520 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6522 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6523 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6530 /* Indicate that we are only retrieving symbol values from this
6534 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
6536 if (is_elf_hash_table (info->hash))
6537 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
6538 _bfd_generic_link_just_syms (sec, info);
6541 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6544 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6547 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
6548 sec->sec_info_type = ELF_INFO_TYPE_NONE;
6551 /* Finish SHF_MERGE section merging. */
6554 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6559 if (!is_elf_hash_table (info->hash))
6562 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6563 if ((ibfd->flags & DYNAMIC) == 0)
6564 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6565 if ((sec->flags & SEC_MERGE) != 0
6566 && !bfd_is_abs_section (sec->output_section))
6568 struct bfd_elf_section_data *secdata;
6570 secdata = elf_section_data (sec);
6571 if (! _bfd_add_merge_section (abfd,
6572 &elf_hash_table (info)->merge_info,
6573 sec, &secdata->sec_info))
6575 else if (secdata->sec_info)
6576 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
6579 if (elf_hash_table (info)->merge_info != NULL)
6580 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6581 merge_sections_remove_hook);
6585 /* Create an entry in an ELF linker hash table. */
6587 struct bfd_hash_entry *
6588 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6589 struct bfd_hash_table *table,
6592 /* Allocate the structure if it has not already been allocated by a
6596 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6601 /* Call the allocation method of the superclass. */
6602 entry = _bfd_link_hash_newfunc (entry, table, string);
6605 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6606 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6608 /* Set local fields. */
6611 ret->got = htab->init_got_refcount;
6612 ret->plt = htab->init_plt_refcount;
6613 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6614 - offsetof (struct elf_link_hash_entry, size)));
6615 /* Assume that we have been called by a non-ELF symbol reader.
6616 This flag is then reset by the code which reads an ELF input
6617 file. This ensures that a symbol created by a non-ELF symbol
6618 reader will have the flag set correctly. */
6625 /* Copy data from an indirect symbol to its direct symbol, hiding the
6626 old indirect symbol. Also used for copying flags to a weakdef. */
6629 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6630 struct elf_link_hash_entry *dir,
6631 struct elf_link_hash_entry *ind)
6633 struct elf_link_hash_table *htab;
6635 /* Copy down any references that we may have already seen to the
6636 symbol which just became indirect. */
6638 dir->ref_dynamic |= ind->ref_dynamic;
6639 dir->ref_regular |= ind->ref_regular;
6640 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6641 dir->non_got_ref |= ind->non_got_ref;
6642 dir->needs_plt |= ind->needs_plt;
6643 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6645 if (ind->root.type != bfd_link_hash_indirect)
6648 /* Copy over the global and procedure linkage table refcount entries.
6649 These may have been already set up by a check_relocs routine. */
6650 htab = elf_hash_table (info);
6651 if (ind->got.refcount > htab->init_got_refcount.refcount)
6653 if (dir->got.refcount < 0)
6654 dir->got.refcount = 0;
6655 dir->got.refcount += ind->got.refcount;
6656 ind->got.refcount = htab->init_got_refcount.refcount;
6659 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6661 if (dir->plt.refcount < 0)
6662 dir->plt.refcount = 0;
6663 dir->plt.refcount += ind->plt.refcount;
6664 ind->plt.refcount = htab->init_plt_refcount.refcount;
6667 if (ind->dynindx != -1)
6669 if (dir->dynindx != -1)
6670 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6671 dir->dynindx = ind->dynindx;
6672 dir->dynstr_index = ind->dynstr_index;
6674 ind->dynstr_index = 0;
6679 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6680 struct elf_link_hash_entry *h,
6681 bfd_boolean force_local)
6683 h->plt = elf_hash_table (info)->init_plt_offset;
6687 h->forced_local = 1;
6688 if (h->dynindx != -1)
6691 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6697 /* Initialize an ELF linker hash table. */
6700 _bfd_elf_link_hash_table_init
6701 (struct elf_link_hash_table *table,
6703 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6704 struct bfd_hash_table *,
6706 unsigned int entsize)
6709 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6711 memset (table, 0, sizeof * table);
6712 table->init_got_refcount.refcount = can_refcount - 1;
6713 table->init_plt_refcount.refcount = can_refcount - 1;
6714 table->init_got_offset.offset = -(bfd_vma) 1;
6715 table->init_plt_offset.offset = -(bfd_vma) 1;
6716 /* The first dynamic symbol is a dummy. */
6717 table->dynsymcount = 1;
6719 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6720 table->root.type = bfd_link_elf_hash_table;
6725 /* Create an ELF linker hash table. */
6727 struct bfd_link_hash_table *
6728 _bfd_elf_link_hash_table_create (bfd *abfd)
6730 struct elf_link_hash_table *ret;
6731 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6733 ret = bfd_malloc (amt);
6737 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
6738 sizeof (struct elf_link_hash_entry)))
6747 /* This is a hook for the ELF emulation code in the generic linker to
6748 tell the backend linker what file name to use for the DT_NEEDED
6749 entry for a dynamic object. */
6752 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6754 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6755 && bfd_get_format (abfd) == bfd_object)
6756 elf_dt_name (abfd) = name;
6760 bfd_elf_get_dyn_lib_class (bfd *abfd)
6763 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6764 && bfd_get_format (abfd) == bfd_object)
6765 lib_class = elf_dyn_lib_class (abfd);
6772 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6774 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6775 && bfd_get_format (abfd) == bfd_object)
6776 elf_dyn_lib_class (abfd) = lib_class;
6779 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6780 the linker ELF emulation code. */
6782 struct bfd_link_needed_list *
6783 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6784 struct bfd_link_info *info)
6786 if (! is_elf_hash_table (info->hash))
6788 return elf_hash_table (info)->needed;
6791 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6792 hook for the linker ELF emulation code. */
6794 struct bfd_link_needed_list *
6795 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6796 struct bfd_link_info *info)
6798 if (! is_elf_hash_table (info->hash))
6800 return elf_hash_table (info)->runpath;
6803 /* Get the name actually used for a dynamic object for a link. This
6804 is the SONAME entry if there is one. Otherwise, it is the string
6805 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6808 bfd_elf_get_dt_soname (bfd *abfd)
6810 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6811 && bfd_get_format (abfd) == bfd_object)
6812 return elf_dt_name (abfd);
6816 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6817 the ELF linker emulation code. */
6820 bfd_elf_get_bfd_needed_list (bfd *abfd,
6821 struct bfd_link_needed_list **pneeded)
6824 bfd_byte *dynbuf = NULL;
6825 unsigned int elfsec;
6826 unsigned long shlink;
6827 bfd_byte *extdyn, *extdynend;
6829 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6833 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6834 || bfd_get_format (abfd) != bfd_object)
6837 s = bfd_get_section_by_name (abfd, ".dynamic");
6838 if (s == NULL || s->size == 0)
6841 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6844 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
6845 if (elfsec == SHN_BAD)
6848 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
6850 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
6851 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
6854 extdynend = extdyn + s->size;
6855 for (; extdyn < extdynend; extdyn += extdynsize)
6857 Elf_Internal_Dyn dyn;
6859 (*swap_dyn_in) (abfd, extdyn, &dyn);
6861 if (dyn.d_tag == DT_NULL)
6864 if (dyn.d_tag == DT_NEEDED)
6867 struct bfd_link_needed_list *l;
6868 unsigned int tagv = dyn.d_un.d_val;
6871 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
6876 l = bfd_alloc (abfd, amt);
6897 struct elf_symbuf_symbol
6899 unsigned long st_name; /* Symbol name, index in string tbl */
6900 unsigned char st_info; /* Type and binding attributes */
6901 unsigned char st_other; /* Visibilty, and target specific */
6904 struct elf_symbuf_head
6906 struct elf_symbuf_symbol *ssym;
6907 bfd_size_type count;
6908 unsigned int st_shndx;
6915 Elf_Internal_Sym *isym;
6916 struct elf_symbuf_symbol *ssym;
6921 /* Sort references to symbols by ascending section number. */
6924 elf_sort_elf_symbol (const void *arg1, const void *arg2)
6926 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
6927 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
6929 return s1->st_shndx - s2->st_shndx;
6933 elf_sym_name_compare (const void *arg1, const void *arg2)
6935 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
6936 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
6937 return strcmp (s1->name, s2->name);
6940 static struct elf_symbuf_head *
6941 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
6943 Elf_Internal_Sym **ind, **indbufend, **indbuf;
6944 struct elf_symbuf_symbol *ssym;
6945 struct elf_symbuf_head *ssymbuf, *ssymhead;
6946 bfd_size_type i, shndx_count, total_size;
6948 indbuf = bfd_malloc2 (symcount, sizeof (*indbuf));
6952 for (ind = indbuf, i = 0; i < symcount; i++)
6953 if (isymbuf[i].st_shndx != SHN_UNDEF)
6954 *ind++ = &isymbuf[i];
6957 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
6958 elf_sort_elf_symbol);
6961 if (indbufend > indbuf)
6962 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
6963 if (ind[0]->st_shndx != ind[1]->st_shndx)
6966 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
6967 + (indbufend - indbuf) * sizeof (*ssym));
6968 ssymbuf = bfd_malloc (total_size);
6969 if (ssymbuf == NULL)
6975 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
6976 ssymbuf->ssym = NULL;
6977 ssymbuf->count = shndx_count;
6978 ssymbuf->st_shndx = 0;
6979 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
6981 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
6984 ssymhead->ssym = ssym;
6985 ssymhead->count = 0;
6986 ssymhead->st_shndx = (*ind)->st_shndx;
6988 ssym->st_name = (*ind)->st_name;
6989 ssym->st_info = (*ind)->st_info;
6990 ssym->st_other = (*ind)->st_other;
6993 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
6994 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7001 /* Check if 2 sections define the same set of local and global
7005 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7006 struct bfd_link_info *info)
7009 const struct elf_backend_data *bed1, *bed2;
7010 Elf_Internal_Shdr *hdr1, *hdr2;
7011 bfd_size_type symcount1, symcount2;
7012 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7013 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7014 Elf_Internal_Sym *isym, *isymend;
7015 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7016 bfd_size_type count1, count2, i;
7017 unsigned int shndx1, shndx2;
7023 /* Both sections have to be in ELF. */
7024 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7025 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7028 if (elf_section_type (sec1) != elf_section_type (sec2))
7031 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7032 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7033 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7036 bed1 = get_elf_backend_data (bfd1);
7037 bed2 = get_elf_backend_data (bfd2);
7038 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7039 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7040 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7041 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7043 if (symcount1 == 0 || symcount2 == 0)
7049 ssymbuf1 = elf_tdata (bfd1)->symbuf;
7050 ssymbuf2 = elf_tdata (bfd2)->symbuf;
7052 if (ssymbuf1 == NULL)
7054 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7056 if (isymbuf1 == NULL)
7059 if (!info->reduce_memory_overheads)
7060 elf_tdata (bfd1)->symbuf = ssymbuf1
7061 = elf_create_symbuf (symcount1, isymbuf1);
7064 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7066 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7068 if (isymbuf2 == NULL)
7071 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7072 elf_tdata (bfd2)->symbuf = ssymbuf2
7073 = elf_create_symbuf (symcount2, isymbuf2);
7076 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7078 /* Optimized faster version. */
7079 bfd_size_type lo, hi, mid;
7080 struct elf_symbol *symp;
7081 struct elf_symbuf_symbol *ssym, *ssymend;
7084 hi = ssymbuf1->count;
7089 mid = (lo + hi) / 2;
7090 if (shndx1 < ssymbuf1[mid].st_shndx)
7092 else if (shndx1 > ssymbuf1[mid].st_shndx)
7096 count1 = ssymbuf1[mid].count;
7103 hi = ssymbuf2->count;
7108 mid = (lo + hi) / 2;
7109 if (shndx2 < ssymbuf2[mid].st_shndx)
7111 else if (shndx2 > ssymbuf2[mid].st_shndx)
7115 count2 = ssymbuf2[mid].count;
7121 if (count1 == 0 || count2 == 0 || count1 != count2)
7124 symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol));
7125 symtable2 = bfd_malloc (count2 * sizeof (struct elf_symbol));
7126 if (symtable1 == NULL || symtable2 == NULL)
7130 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7131 ssym < ssymend; ssym++, symp++)
7133 symp->u.ssym = ssym;
7134 symp->name = bfd_elf_string_from_elf_section (bfd1,
7140 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7141 ssym < ssymend; ssym++, symp++)
7143 symp->u.ssym = ssym;
7144 symp->name = bfd_elf_string_from_elf_section (bfd2,
7149 /* Sort symbol by name. */
7150 qsort (symtable1, count1, sizeof (struct elf_symbol),
7151 elf_sym_name_compare);
7152 qsort (symtable2, count1, sizeof (struct elf_symbol),
7153 elf_sym_name_compare);
7155 for (i = 0; i < count1; i++)
7156 /* Two symbols must have the same binding, type and name. */
7157 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7158 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7159 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7166 symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7167 symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7168 if (symtable1 == NULL || symtable2 == NULL)
7171 /* Count definitions in the section. */
7173 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7174 if (isym->st_shndx == shndx1)
7175 symtable1[count1++].u.isym = isym;
7178 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7179 if (isym->st_shndx == shndx2)
7180 symtable2[count2++].u.isym = isym;
7182 if (count1 == 0 || count2 == 0 || count1 != count2)
7185 for (i = 0; i < count1; i++)
7187 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7188 symtable1[i].u.isym->st_name);
7190 for (i = 0; i < count2; i++)
7192 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7193 symtable2[i].u.isym->st_name);
7195 /* Sort symbol by name. */
7196 qsort (symtable1, count1, sizeof (struct elf_symbol),
7197 elf_sym_name_compare);
7198 qsort (symtable2, count1, sizeof (struct elf_symbol),
7199 elf_sym_name_compare);
7201 for (i = 0; i < count1; i++)
7202 /* Two symbols must have the same binding, type and name. */
7203 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7204 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7205 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7223 /* Return TRUE if 2 section types are compatible. */
7226 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7227 bfd *bbfd, const asection *bsec)
7231 || abfd->xvec->flavour != bfd_target_elf_flavour
7232 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7235 return elf_section_type (asec) == elf_section_type (bsec);
7238 /* Final phase of ELF linker. */
7240 /* A structure we use to avoid passing large numbers of arguments. */
7242 struct elf_final_link_info
7244 /* General link information. */
7245 struct bfd_link_info *info;
7248 /* Symbol string table. */
7249 struct bfd_strtab_hash *symstrtab;
7250 /* .dynsym section. */
7251 asection *dynsym_sec;
7252 /* .hash section. */
7254 /* symbol version section (.gnu.version). */
7255 asection *symver_sec;
7256 /* Buffer large enough to hold contents of any section. */
7258 /* Buffer large enough to hold external relocs of any section. */
7259 void *external_relocs;
7260 /* Buffer large enough to hold internal relocs of any section. */
7261 Elf_Internal_Rela *internal_relocs;
7262 /* Buffer large enough to hold external local symbols of any input
7264 bfd_byte *external_syms;
7265 /* And a buffer for symbol section indices. */
7266 Elf_External_Sym_Shndx *locsym_shndx;
7267 /* Buffer large enough to hold internal local symbols of any input
7269 Elf_Internal_Sym *internal_syms;
7270 /* Array large enough to hold a symbol index for each local symbol
7271 of any input BFD. */
7273 /* Array large enough to hold a section pointer for each local
7274 symbol of any input BFD. */
7275 asection **sections;
7276 /* Buffer to hold swapped out symbols. */
7278 /* And one for symbol section indices. */
7279 Elf_External_Sym_Shndx *symshndxbuf;
7280 /* Number of swapped out symbols in buffer. */
7281 size_t symbuf_count;
7282 /* Number of symbols which fit in symbuf. */
7284 /* And same for symshndxbuf. */
7285 size_t shndxbuf_size;
7288 /* This struct is used to pass information to elf_link_output_extsym. */
7290 struct elf_outext_info
7293 bfd_boolean localsyms;
7294 struct elf_final_link_info *finfo;
7298 /* Support for evaluating a complex relocation.
7300 Complex relocations are generalized, self-describing relocations. The
7301 implementation of them consists of two parts: complex symbols, and the
7302 relocations themselves.
7304 The relocations are use a reserved elf-wide relocation type code (R_RELC
7305 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7306 information (start bit, end bit, word width, etc) into the addend. This
7307 information is extracted from CGEN-generated operand tables within gas.
7309 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7310 internal) representing prefix-notation expressions, including but not
7311 limited to those sorts of expressions normally encoded as addends in the
7312 addend field. The symbol mangling format is:
7315 | <unary-operator> ':' <node>
7316 | <binary-operator> ':' <node> ':' <node>
7319 <literal> := 's' <digits=N> ':' <N character symbol name>
7320 | 'S' <digits=N> ':' <N character section name>
7324 <binary-operator> := as in C
7325 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7328 set_symbol_value (bfd *bfd_with_globals,
7329 Elf_Internal_Sym *isymbuf,
7334 struct elf_link_hash_entry **sym_hashes;
7335 struct elf_link_hash_entry *h;
7336 size_t extsymoff = locsymcount;
7338 if (symidx < locsymcount)
7340 Elf_Internal_Sym *sym;
7342 sym = isymbuf + symidx;
7343 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7345 /* It is a local symbol: move it to the
7346 "absolute" section and give it a value. */
7347 sym->st_shndx = SHN_ABS;
7348 sym->st_value = val;
7351 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7355 /* It is a global symbol: set its link type
7356 to "defined" and give it a value. */
7358 sym_hashes = elf_sym_hashes (bfd_with_globals);
7359 h = sym_hashes [symidx - extsymoff];
7360 while (h->root.type == bfd_link_hash_indirect
7361 || h->root.type == bfd_link_hash_warning)
7362 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7363 h->root.type = bfd_link_hash_defined;
7364 h->root.u.def.value = val;
7365 h->root.u.def.section = bfd_abs_section_ptr;
7369 resolve_symbol (const char *name,
7371 struct elf_final_link_info *finfo,
7373 Elf_Internal_Sym *isymbuf,
7376 Elf_Internal_Sym *sym;
7377 struct bfd_link_hash_entry *global_entry;
7378 const char *candidate = NULL;
7379 Elf_Internal_Shdr *symtab_hdr;
7382 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7384 for (i = 0; i < locsymcount; ++ i)
7388 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7391 candidate = bfd_elf_string_from_elf_section (input_bfd,
7392 symtab_hdr->sh_link,
7395 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7396 name, candidate, (unsigned long) sym->st_value);
7398 if (candidate && strcmp (candidate, name) == 0)
7400 asection *sec = finfo->sections [i];
7402 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7403 *result += sec->output_offset + sec->output_section->vma;
7405 printf ("Found symbol with value %8.8lx\n",
7406 (unsigned long) *result);
7412 /* Hmm, haven't found it yet. perhaps it is a global. */
7413 global_entry = bfd_link_hash_lookup (finfo->info->hash, name,
7414 FALSE, FALSE, TRUE);
7418 if (global_entry->type == bfd_link_hash_defined
7419 || global_entry->type == bfd_link_hash_defweak)
7421 *result = (global_entry->u.def.value
7422 + global_entry->u.def.section->output_section->vma
7423 + global_entry->u.def.section->output_offset);
7425 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7426 global_entry->root.string, (unsigned long) *result);
7435 resolve_section (const char *name,
7442 for (curr = sections; curr; curr = curr->next)
7443 if (strcmp (curr->name, name) == 0)
7445 *result = curr->vma;
7449 /* Hmm. still haven't found it. try pseudo-section names. */
7450 for (curr = sections; curr; curr = curr->next)
7452 len = strlen (curr->name);
7453 if (len > strlen (name))
7456 if (strncmp (curr->name, name, len) == 0)
7458 if (strncmp (".end", name + len, 4) == 0)
7460 *result = curr->vma + curr->size;
7464 /* Insert more pseudo-section names here, if you like. */
7472 undefined_reference (const char *reftype, const char *name)
7474 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7479 eval_symbol (bfd_vma *result,
7482 struct elf_final_link_info *finfo,
7484 Elf_Internal_Sym *isymbuf,
7493 const char *sym = *symp;
7495 bfd_boolean symbol_is_section = FALSE;
7500 if (len < 1 || len > sizeof (symbuf))
7502 bfd_set_error (bfd_error_invalid_operation);
7515 *result = strtoul (sym, (char **) symp, 16);
7519 symbol_is_section = TRUE;
7522 symlen = strtol (sym, (char **) symp, 10);
7523 sym = *symp + 1; /* Skip the trailing ':'. */
7525 if (symend < sym || symlen + 1 > sizeof (symbuf))
7527 bfd_set_error (bfd_error_invalid_operation);
7531 memcpy (symbuf, sym, symlen);
7532 symbuf[symlen] = '\0';
7533 *symp = sym + symlen;
7535 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7536 the symbol as a section, or vice-versa. so we're pretty liberal in our
7537 interpretation here; section means "try section first", not "must be a
7538 section", and likewise with symbol. */
7540 if (symbol_is_section)
7542 if (!resolve_section (symbuf, finfo->output_bfd->sections, result)
7543 && !resolve_symbol (symbuf, input_bfd, finfo, result,
7544 isymbuf, locsymcount))
7546 undefined_reference ("section", symbuf);
7552 if (!resolve_symbol (symbuf, input_bfd, finfo, result,
7553 isymbuf, locsymcount)
7554 && !resolve_section (symbuf, finfo->output_bfd->sections,
7557 undefined_reference ("symbol", symbuf);
7564 /* All that remains are operators. */
7566 #define UNARY_OP(op) \
7567 if (strncmp (sym, #op, strlen (#op)) == 0) \
7569 sym += strlen (#op); \
7573 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7574 isymbuf, locsymcount, signed_p)) \
7577 *result = op ((bfd_signed_vma) a); \
7583 #define BINARY_OP(op) \
7584 if (strncmp (sym, #op, strlen (#op)) == 0) \
7586 sym += strlen (#op); \
7590 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7591 isymbuf, locsymcount, signed_p)) \
7594 if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \
7595 isymbuf, locsymcount, signed_p)) \
7598 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7628 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7629 bfd_set_error (bfd_error_invalid_operation);
7635 put_value (bfd_vma size,
7636 unsigned long chunksz,
7641 location += (size - chunksz);
7643 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
7651 bfd_put_8 (input_bfd, x, location);
7654 bfd_put_16 (input_bfd, x, location);
7657 bfd_put_32 (input_bfd, x, location);
7661 bfd_put_64 (input_bfd, x, location);
7671 get_value (bfd_vma size,
7672 unsigned long chunksz,
7678 for (; size; size -= chunksz, location += chunksz)
7686 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7689 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7692 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7696 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7707 decode_complex_addend (unsigned long *start, /* in bits */
7708 unsigned long *oplen, /* in bits */
7709 unsigned long *len, /* in bits */
7710 unsigned long *wordsz, /* in bytes */
7711 unsigned long *chunksz, /* in bytes */
7712 unsigned long *lsb0_p,
7713 unsigned long *signed_p,
7714 unsigned long *trunc_p,
7715 unsigned long encoded)
7717 * start = encoded & 0x3F;
7718 * len = (encoded >> 6) & 0x3F;
7719 * oplen = (encoded >> 12) & 0x3F;
7720 * wordsz = (encoded >> 18) & 0xF;
7721 * chunksz = (encoded >> 22) & 0xF;
7722 * lsb0_p = (encoded >> 27) & 1;
7723 * signed_p = (encoded >> 28) & 1;
7724 * trunc_p = (encoded >> 29) & 1;
7727 bfd_reloc_status_type
7728 bfd_elf_perform_complex_relocation (bfd *input_bfd,
7729 asection *input_section ATTRIBUTE_UNUSED,
7731 Elf_Internal_Rela *rel,
7734 bfd_vma shift, x, mask;
7735 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
7736 bfd_reloc_status_type r;
7738 /* Perform this reloc, since it is complex.
7739 (this is not to say that it necessarily refers to a complex
7740 symbol; merely that it is a self-describing CGEN based reloc.
7741 i.e. the addend has the complete reloc information (bit start, end,
7742 word size, etc) encoded within it.). */
7744 decode_complex_addend (&start, &oplen, &len, &wordsz,
7745 &chunksz, &lsb0_p, &signed_p,
7746 &trunc_p, rel->r_addend);
7748 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7751 shift = (start + 1) - len;
7753 shift = (8 * wordsz) - (start + len);
7755 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7758 printf ("Doing complex reloc: "
7759 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7760 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7761 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7762 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7763 oplen, x, mask, relocation);
7768 /* Now do an overflow check. */
7769 r = bfd_check_overflow ((signed_p
7770 ? complain_overflow_signed
7771 : complain_overflow_unsigned),
7772 len, 0, (8 * wordsz),
7776 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7779 printf (" relocation: %8.8lx\n"
7780 " shifted mask: %8.8lx\n"
7781 " shifted/masked reloc: %8.8lx\n"
7782 " result: %8.8lx\n",
7783 relocation, (mask << shift),
7784 ((relocation & mask) << shift), x);
7786 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
7790 /* When performing a relocatable link, the input relocations are
7791 preserved. But, if they reference global symbols, the indices
7792 referenced must be updated. Update all the relocations in
7793 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7796 elf_link_adjust_relocs (bfd *abfd,
7797 Elf_Internal_Shdr *rel_hdr,
7799 struct elf_link_hash_entry **rel_hash)
7802 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7804 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7805 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7806 bfd_vma r_type_mask;
7809 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
7811 swap_in = bed->s->swap_reloc_in;
7812 swap_out = bed->s->swap_reloc_out;
7814 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
7816 swap_in = bed->s->swap_reloca_in;
7817 swap_out = bed->s->swap_reloca_out;
7822 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7825 if (bed->s->arch_size == 32)
7832 r_type_mask = 0xffffffff;
7836 erela = rel_hdr->contents;
7837 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
7839 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7842 if (*rel_hash == NULL)
7845 BFD_ASSERT ((*rel_hash)->indx >= 0);
7847 (*swap_in) (abfd, erela, irela);
7848 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7849 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7850 | (irela[j].r_info & r_type_mask));
7851 (*swap_out) (abfd, irela, erela);
7855 struct elf_link_sort_rela
7861 enum elf_reloc_type_class type;
7862 /* We use this as an array of size int_rels_per_ext_rel. */
7863 Elf_Internal_Rela rela[1];
7867 elf_link_sort_cmp1 (const void *A, const void *B)
7869 const struct elf_link_sort_rela *a = A;
7870 const struct elf_link_sort_rela *b = B;
7871 int relativea, relativeb;
7873 relativea = a->type == reloc_class_relative;
7874 relativeb = b->type == reloc_class_relative;
7876 if (relativea < relativeb)
7878 if (relativea > relativeb)
7880 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
7882 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
7884 if (a->rela->r_offset < b->rela->r_offset)
7886 if (a->rela->r_offset > b->rela->r_offset)
7892 elf_link_sort_cmp2 (const void *A, const void *B)
7894 const struct elf_link_sort_rela *a = A;
7895 const struct elf_link_sort_rela *b = B;
7898 if (a->u.offset < b->u.offset)
7900 if (a->u.offset > b->u.offset)
7902 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
7903 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
7908 if (a->rela->r_offset < b->rela->r_offset)
7910 if (a->rela->r_offset > b->rela->r_offset)
7916 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
7918 asection *dynamic_relocs;
7921 bfd_size_type count, size;
7922 size_t i, ret, sort_elt, ext_size;
7923 bfd_byte *sort, *s_non_relative, *p;
7924 struct elf_link_sort_rela *sq;
7925 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7926 int i2e = bed->s->int_rels_per_ext_rel;
7927 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7928 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7929 struct bfd_link_order *lo;
7931 bfd_boolean use_rela;
7933 /* Find a dynamic reloc section. */
7934 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
7935 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
7936 if (rela_dyn != NULL && rela_dyn->size > 0
7937 && rel_dyn != NULL && rel_dyn->size > 0)
7939 bfd_boolean use_rela_initialised = FALSE;
7941 /* This is just here to stop gcc from complaining.
7942 It's initialization checking code is not perfect. */
7945 /* Both sections are present. Examine the sizes
7946 of the indirect sections to help us choose. */
7947 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7948 if (lo->type == bfd_indirect_link_order)
7950 asection *o = lo->u.indirect.section;
7952 if ((o->size % bed->s->sizeof_rela) == 0)
7954 if ((o->size % bed->s->sizeof_rel) == 0)
7955 /* Section size is divisible by both rel and rela sizes.
7956 It is of no help to us. */
7960 /* Section size is only divisible by rela. */
7961 if (use_rela_initialised && (use_rela == FALSE))
7964 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7965 bfd_set_error (bfd_error_invalid_operation);
7971 use_rela_initialised = TRUE;
7975 else if ((o->size % bed->s->sizeof_rel) == 0)
7977 /* Section size is only divisible by rel. */
7978 if (use_rela_initialised && (use_rela == TRUE))
7981 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7982 bfd_set_error (bfd_error_invalid_operation);
7988 use_rela_initialised = TRUE;
7993 /* The section size is not divisible by either - something is wrong. */
7995 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
7996 bfd_set_error (bfd_error_invalid_operation);
8001 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8002 if (lo->type == bfd_indirect_link_order)
8004 asection *o = lo->u.indirect.section;
8006 if ((o->size % bed->s->sizeof_rela) == 0)
8008 if ((o->size % bed->s->sizeof_rel) == 0)
8009 /* Section size is divisible by both rel and rela sizes.
8010 It is of no help to us. */
8014 /* Section size is only divisible by rela. */
8015 if (use_rela_initialised && (use_rela == FALSE))
8018 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8019 bfd_set_error (bfd_error_invalid_operation);
8025 use_rela_initialised = TRUE;
8029 else if ((o->size % bed->s->sizeof_rel) == 0)
8031 /* Section size is only divisible by rel. */
8032 if (use_rela_initialised && (use_rela == TRUE))
8035 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8036 bfd_set_error (bfd_error_invalid_operation);
8042 use_rela_initialised = TRUE;
8047 /* The section size is not divisible by either - something is wrong. */
8049 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8050 bfd_set_error (bfd_error_invalid_operation);
8055 if (! use_rela_initialised)
8059 else if (rela_dyn != NULL && rela_dyn->size > 0)
8061 else if (rel_dyn != NULL && rel_dyn->size > 0)
8068 dynamic_relocs = rela_dyn;
8069 ext_size = bed->s->sizeof_rela;
8070 swap_in = bed->s->swap_reloca_in;
8071 swap_out = bed->s->swap_reloca_out;
8075 dynamic_relocs = rel_dyn;
8076 ext_size = bed->s->sizeof_rel;
8077 swap_in = bed->s->swap_reloc_in;
8078 swap_out = bed->s->swap_reloc_out;
8082 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8083 if (lo->type == bfd_indirect_link_order)
8084 size += lo->u.indirect.section->size;
8086 if (size != dynamic_relocs->size)
8089 sort_elt = (sizeof (struct elf_link_sort_rela)
8090 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8092 count = dynamic_relocs->size / ext_size;
8093 sort = bfd_zmalloc (sort_elt * count);
8097 (*info->callbacks->warning)
8098 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8102 if (bed->s->arch_size == 32)
8103 r_sym_mask = ~(bfd_vma) 0xff;
8105 r_sym_mask = ~(bfd_vma) 0xffffffff;
8107 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8108 if (lo->type == bfd_indirect_link_order)
8110 bfd_byte *erel, *erelend;
8111 asection *o = lo->u.indirect.section;
8113 if (o->contents == NULL && o->size != 0)
8115 /* This is a reloc section that is being handled as a normal
8116 section. See bfd_section_from_shdr. We can't combine
8117 relocs in this case. */
8122 erelend = o->contents + o->size;
8123 p = sort + o->output_offset / ext_size * sort_elt;
8125 while (erel < erelend)
8127 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8129 (*swap_in) (abfd, erel, s->rela);
8130 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8131 s->u.sym_mask = r_sym_mask;
8137 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8139 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8141 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8142 if (s->type != reloc_class_relative)
8148 sq = (struct elf_link_sort_rela *) s_non_relative;
8149 for (; i < count; i++, p += sort_elt)
8151 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8152 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8154 sp->u.offset = sq->rela->r_offset;
8157 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8159 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8160 if (lo->type == bfd_indirect_link_order)
8162 bfd_byte *erel, *erelend;
8163 asection *o = lo->u.indirect.section;
8166 erelend = o->contents + o->size;
8167 p = sort + o->output_offset / ext_size * sort_elt;
8168 while (erel < erelend)
8170 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8171 (*swap_out) (abfd, s->rela, erel);
8178 *psec = dynamic_relocs;
8182 /* Flush the output symbols to the file. */
8185 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
8186 const struct elf_backend_data *bed)
8188 if (finfo->symbuf_count > 0)
8190 Elf_Internal_Shdr *hdr;
8194 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
8195 pos = hdr->sh_offset + hdr->sh_size;
8196 amt = finfo->symbuf_count * bed->s->sizeof_sym;
8197 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
8198 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
8201 hdr->sh_size += amt;
8202 finfo->symbuf_count = 0;
8208 /* Add a symbol to the output symbol table. */
8211 elf_link_output_sym (struct elf_final_link_info *finfo,
8213 Elf_Internal_Sym *elfsym,
8214 asection *input_sec,
8215 struct elf_link_hash_entry *h)
8218 Elf_External_Sym_Shndx *destshndx;
8219 bfd_boolean (*output_symbol_hook)
8220 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8221 struct elf_link_hash_entry *);
8222 const struct elf_backend_data *bed;
8224 bed = get_elf_backend_data (finfo->output_bfd);
8225 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8226 if (output_symbol_hook != NULL)
8228 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
8232 if (name == NULL || *name == '\0')
8233 elfsym->st_name = 0;
8234 else if (input_sec->flags & SEC_EXCLUDE)
8235 elfsym->st_name = 0;
8238 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
8240 if (elfsym->st_name == (unsigned long) -1)
8244 if (finfo->symbuf_count >= finfo->symbuf_size)
8246 if (! elf_link_flush_output_syms (finfo, bed))
8250 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
8251 destshndx = finfo->symshndxbuf;
8252 if (destshndx != NULL)
8254 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
8258 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
8259 destshndx = bfd_realloc (destshndx, amt * 2);
8260 if (destshndx == NULL)
8262 finfo->symshndxbuf = destshndx;
8263 memset ((char *) destshndx + amt, 0, amt);
8264 finfo->shndxbuf_size *= 2;
8266 destshndx += bfd_get_symcount (finfo->output_bfd);
8269 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
8270 finfo->symbuf_count += 1;
8271 bfd_get_symcount (finfo->output_bfd) += 1;
8276 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8279 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8281 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8282 && sym->st_shndx < SHN_LORESERVE)
8284 /* The gABI doesn't support dynamic symbols in output sections
8286 (*_bfd_error_handler)
8287 (_("%B: Too many sections: %d (>= %d)"),
8288 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8289 bfd_set_error (bfd_error_nonrepresentable_section);
8295 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8296 allowing an unsatisfied unversioned symbol in the DSO to match a
8297 versioned symbol that would normally require an explicit version.
8298 We also handle the case that a DSO references a hidden symbol
8299 which may be satisfied by a versioned symbol in another DSO. */
8302 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8303 const struct elf_backend_data *bed,
8304 struct elf_link_hash_entry *h)
8307 struct elf_link_loaded_list *loaded;
8309 if (!is_elf_hash_table (info->hash))
8312 switch (h->root.type)
8318 case bfd_link_hash_undefined:
8319 case bfd_link_hash_undefweak:
8320 abfd = h->root.u.undef.abfd;
8321 if ((abfd->flags & DYNAMIC) == 0
8322 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8326 case bfd_link_hash_defined:
8327 case bfd_link_hash_defweak:
8328 abfd = h->root.u.def.section->owner;
8331 case bfd_link_hash_common:
8332 abfd = h->root.u.c.p->section->owner;
8335 BFD_ASSERT (abfd != NULL);
8337 for (loaded = elf_hash_table (info)->loaded;
8339 loaded = loaded->next)
8342 Elf_Internal_Shdr *hdr;
8343 bfd_size_type symcount;
8344 bfd_size_type extsymcount;
8345 bfd_size_type extsymoff;
8346 Elf_Internal_Shdr *versymhdr;
8347 Elf_Internal_Sym *isym;
8348 Elf_Internal_Sym *isymend;
8349 Elf_Internal_Sym *isymbuf;
8350 Elf_External_Versym *ever;
8351 Elf_External_Versym *extversym;
8353 input = loaded->abfd;
8355 /* We check each DSO for a possible hidden versioned definition. */
8357 || (input->flags & DYNAMIC) == 0
8358 || elf_dynversym (input) == 0)
8361 hdr = &elf_tdata (input)->dynsymtab_hdr;
8363 symcount = hdr->sh_size / bed->s->sizeof_sym;
8364 if (elf_bad_symtab (input))
8366 extsymcount = symcount;
8371 extsymcount = symcount - hdr->sh_info;
8372 extsymoff = hdr->sh_info;
8375 if (extsymcount == 0)
8378 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8380 if (isymbuf == NULL)
8383 /* Read in any version definitions. */
8384 versymhdr = &elf_tdata (input)->dynversym_hdr;
8385 extversym = bfd_malloc (versymhdr->sh_size);
8386 if (extversym == NULL)
8389 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8390 || (bfd_bread (extversym, versymhdr->sh_size, input)
8391 != versymhdr->sh_size))
8399 ever = extversym + extsymoff;
8400 isymend = isymbuf + extsymcount;
8401 for (isym = isymbuf; isym < isymend; isym++, ever++)
8404 Elf_Internal_Versym iver;
8405 unsigned short version_index;
8407 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8408 || isym->st_shndx == SHN_UNDEF)
8411 name = bfd_elf_string_from_elf_section (input,
8414 if (strcmp (name, h->root.root.string) != 0)
8417 _bfd_elf_swap_versym_in (input, ever, &iver);
8419 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
8421 /* If we have a non-hidden versioned sym, then it should
8422 have provided a definition for the undefined sym. */
8426 version_index = iver.vs_vers & VERSYM_VERSION;
8427 if (version_index == 1 || version_index == 2)
8429 /* This is the base or first version. We can use it. */
8443 /* Add an external symbol to the symbol table. This is called from
8444 the hash table traversal routine. When generating a shared object,
8445 we go through the symbol table twice. The first time we output
8446 anything that might have been forced to local scope in a version
8447 script. The second time we output the symbols that are still
8451 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
8453 struct elf_outext_info *eoinfo = data;
8454 struct elf_final_link_info *finfo = eoinfo->finfo;
8456 Elf_Internal_Sym sym;
8457 asection *input_sec;
8458 const struct elf_backend_data *bed;
8460 if (h->root.type == bfd_link_hash_warning)
8462 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8463 if (h->root.type == bfd_link_hash_new)
8467 /* Decide whether to output this symbol in this pass. */
8468 if (eoinfo->localsyms)
8470 if (!h->forced_local)
8475 if (h->forced_local)
8479 bed = get_elf_backend_data (finfo->output_bfd);
8481 if (h->root.type == bfd_link_hash_undefined)
8483 /* If we have an undefined symbol reference here then it must have
8484 come from a shared library that is being linked in. (Undefined
8485 references in regular files have already been handled). */
8486 bfd_boolean ignore_undef = FALSE;
8488 /* Some symbols may be special in that the fact that they're
8489 undefined can be safely ignored - let backend determine that. */
8490 if (bed->elf_backend_ignore_undef_symbol)
8491 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8493 /* If we are reporting errors for this situation then do so now. */
8494 if (ignore_undef == FALSE
8497 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
8498 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
8500 if (! (finfo->info->callbacks->undefined_symbol
8501 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
8502 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
8504 eoinfo->failed = TRUE;
8510 /* We should also warn if a forced local symbol is referenced from
8511 shared libraries. */
8512 if (! finfo->info->relocatable
8513 && (! finfo->info->shared)
8518 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
8520 (*_bfd_error_handler)
8521 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
8523 h->root.u.def.section == bfd_abs_section_ptr
8524 ? finfo->output_bfd : h->root.u.def.section->owner,
8525 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
8527 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
8528 ? "hidden" : "local",
8529 h->root.root.string);
8530 eoinfo->failed = TRUE;
8534 /* We don't want to output symbols that have never been mentioned by
8535 a regular file, or that we have been told to strip. However, if
8536 h->indx is set to -2, the symbol is used by a reloc and we must
8540 else if ((h->def_dynamic
8542 || h->root.type == bfd_link_hash_new)
8546 else if (finfo->info->strip == strip_all)
8548 else if (finfo->info->strip == strip_some
8549 && bfd_hash_lookup (finfo->info->keep_hash,
8550 h->root.root.string, FALSE, FALSE) == NULL)
8552 else if (finfo->info->strip_discarded
8553 && (h->root.type == bfd_link_hash_defined
8554 || h->root.type == bfd_link_hash_defweak)
8555 && elf_discarded_section (h->root.u.def.section))
8560 /* If we're stripping it, and it's not a dynamic symbol, there's
8561 nothing else to do unless it is a forced local symbol. */
8564 && !h->forced_local)
8568 sym.st_size = h->size;
8569 sym.st_other = h->other;
8570 if (h->forced_local)
8571 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8572 else if (h->root.type == bfd_link_hash_undefweak
8573 || h->root.type == bfd_link_hash_defweak)
8574 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8576 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
8578 switch (h->root.type)
8581 case bfd_link_hash_new:
8582 case bfd_link_hash_warning:
8586 case bfd_link_hash_undefined:
8587 case bfd_link_hash_undefweak:
8588 input_sec = bfd_und_section_ptr;
8589 sym.st_shndx = SHN_UNDEF;
8592 case bfd_link_hash_defined:
8593 case bfd_link_hash_defweak:
8595 input_sec = h->root.u.def.section;
8596 if (input_sec->output_section != NULL)
8599 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
8600 input_sec->output_section);
8601 if (sym.st_shndx == SHN_BAD)
8603 (*_bfd_error_handler)
8604 (_("%B: could not find output section %A for input section %A"),
8605 finfo->output_bfd, input_sec->output_section, input_sec);
8606 eoinfo->failed = TRUE;
8610 /* ELF symbols in relocatable files are section relative,
8611 but in nonrelocatable files they are virtual
8613 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8614 if (! finfo->info->relocatable)
8616 sym.st_value += input_sec->output_section->vma;
8617 if (h->type == STT_TLS)
8619 asection *tls_sec = elf_hash_table (finfo->info)->tls_sec;
8620 if (tls_sec != NULL)
8621 sym.st_value -= tls_sec->vma;
8624 /* The TLS section may have been garbage collected. */
8625 BFD_ASSERT (finfo->info->gc_sections
8626 && !input_sec->gc_mark);
8633 BFD_ASSERT (input_sec->owner == NULL
8634 || (input_sec->owner->flags & DYNAMIC) != 0);
8635 sym.st_shndx = SHN_UNDEF;
8636 input_sec = bfd_und_section_ptr;
8641 case bfd_link_hash_common:
8642 input_sec = h->root.u.c.p->section;
8643 sym.st_shndx = bed->common_section_index (input_sec);
8644 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8647 case bfd_link_hash_indirect:
8648 /* These symbols are created by symbol versioning. They point
8649 to the decorated version of the name. For example, if the
8650 symbol foo@@GNU_1.2 is the default, which should be used when
8651 foo is used with no version, then we add an indirect symbol
8652 foo which points to foo@@GNU_1.2. We ignore these symbols,
8653 since the indirected symbol is already in the hash table. */
8657 /* Give the processor backend a chance to tweak the symbol value,
8658 and also to finish up anything that needs to be done for this
8659 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8660 forced local syms when non-shared is due to a historical quirk. */
8661 if ((h->dynindx != -1
8663 && ((finfo->info->shared
8664 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8665 || h->root.type != bfd_link_hash_undefweak))
8666 || !h->forced_local)
8667 && elf_hash_table (finfo->info)->dynamic_sections_created)
8669 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8670 (finfo->output_bfd, finfo->info, h, &sym)))
8672 eoinfo->failed = TRUE;
8677 /* If we are marking the symbol as undefined, and there are no
8678 non-weak references to this symbol from a regular object, then
8679 mark the symbol as weak undefined; if there are non-weak
8680 references, mark the symbol as strong. We can't do this earlier,
8681 because it might not be marked as undefined until the
8682 finish_dynamic_symbol routine gets through with it. */
8683 if (sym.st_shndx == SHN_UNDEF
8685 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8686 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8690 if (h->ref_regular_nonweak)
8691 bindtype = STB_GLOBAL;
8693 bindtype = STB_WEAK;
8694 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
8697 /* If this is a symbol defined in a dynamic library, don't use the
8698 symbol size from the dynamic library. Relinking an executable
8699 against a new library may introduce gratuitous changes in the
8700 executable's symbols if we keep the size. */
8701 if (sym.st_shndx == SHN_UNDEF
8706 /* If a non-weak symbol with non-default visibility is not defined
8707 locally, it is a fatal error. */
8708 if (! finfo->info->relocatable
8709 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8710 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8711 && h->root.type == bfd_link_hash_undefined
8714 (*_bfd_error_handler)
8715 (_("%B: %s symbol `%s' isn't defined"),
8717 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
8719 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
8720 ? "internal" : "hidden",
8721 h->root.root.string);
8722 eoinfo->failed = TRUE;
8726 /* If this symbol should be put in the .dynsym section, then put it
8727 there now. We already know the symbol index. We also fill in
8728 the entry in the .hash section. */
8729 if (h->dynindx != -1
8730 && elf_hash_table (finfo->info)->dynamic_sections_created)
8734 sym.st_name = h->dynstr_index;
8735 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
8736 if (! check_dynsym (finfo->output_bfd, &sym))
8738 eoinfo->failed = TRUE;
8741 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8743 if (finfo->hash_sec != NULL)
8745 size_t hash_entry_size;
8746 bfd_byte *bucketpos;
8751 bucketcount = elf_hash_table (finfo->info)->bucketcount;
8752 bucket = h->u.elf_hash_value % bucketcount;
8755 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8756 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8757 + (bucket + 2) * hash_entry_size);
8758 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8759 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8760 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8761 ((bfd_byte *) finfo->hash_sec->contents
8762 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8765 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8767 Elf_Internal_Versym iversym;
8768 Elf_External_Versym *eversym;
8770 if (!h->def_regular)
8772 if (h->verinfo.verdef == NULL)
8773 iversym.vs_vers = 0;
8775 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8779 if (h->verinfo.vertree == NULL)
8780 iversym.vs_vers = 1;
8782 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8783 if (finfo->info->create_default_symver)
8788 iversym.vs_vers |= VERSYM_HIDDEN;
8790 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8791 eversym += h->dynindx;
8792 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8796 /* If we're stripping it, then it was just a dynamic symbol, and
8797 there's nothing else to do. */
8798 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8801 h->indx = bfd_get_symcount (finfo->output_bfd);
8803 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
8805 eoinfo->failed = TRUE;
8812 /* Return TRUE if special handling is done for relocs in SEC against
8813 symbols defined in discarded sections. */
8816 elf_section_ignore_discarded_relocs (asection *sec)
8818 const struct elf_backend_data *bed;
8820 switch (sec->sec_info_type)
8822 case ELF_INFO_TYPE_STABS:
8823 case ELF_INFO_TYPE_EH_FRAME:
8829 bed = get_elf_backend_data (sec->owner);
8830 if (bed->elf_backend_ignore_discarded_relocs != NULL
8831 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8837 /* Return a mask saying how ld should treat relocations in SEC against
8838 symbols defined in discarded sections. If this function returns
8839 COMPLAIN set, ld will issue a warning message. If this function
8840 returns PRETEND set, and the discarded section was link-once and the
8841 same size as the kept link-once section, ld will pretend that the
8842 symbol was actually defined in the kept section. Otherwise ld will
8843 zero the reloc (at least that is the intent, but some cooperation by
8844 the target dependent code is needed, particularly for REL targets). */
8847 _bfd_elf_default_action_discarded (asection *sec)
8849 if (sec->flags & SEC_DEBUGGING)
8852 if (strcmp (".eh_frame", sec->name) == 0)
8855 if (strcmp (".gcc_except_table", sec->name) == 0)
8858 return COMPLAIN | PRETEND;
8861 /* Find a match between a section and a member of a section group. */
8864 match_group_member (asection *sec, asection *group,
8865 struct bfd_link_info *info)
8867 asection *first = elf_next_in_group (group);
8868 asection *s = first;
8872 if (bfd_elf_match_symbols_in_sections (s, sec, info))
8875 s = elf_next_in_group (s);
8883 /* Check if the kept section of a discarded section SEC can be used
8884 to replace it. Return the replacement if it is OK. Otherwise return
8888 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
8892 kept = sec->kept_section;
8895 if ((kept->flags & SEC_GROUP) != 0)
8896 kept = match_group_member (sec, kept, info);
8898 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
8899 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
8901 sec->kept_section = kept;
8906 /* Link an input file into the linker output file. This function
8907 handles all the sections and relocations of the input file at once.
8908 This is so that we only have to read the local symbols once, and
8909 don't have to keep them in memory. */
8912 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
8914 int (*relocate_section)
8915 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
8916 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
8918 Elf_Internal_Shdr *symtab_hdr;
8921 Elf_Internal_Sym *isymbuf;
8922 Elf_Internal_Sym *isym;
8923 Elf_Internal_Sym *isymend;
8925 asection **ppsection;
8927 const struct elf_backend_data *bed;
8928 struct elf_link_hash_entry **sym_hashes;
8930 output_bfd = finfo->output_bfd;
8931 bed = get_elf_backend_data (output_bfd);
8932 relocate_section = bed->elf_backend_relocate_section;
8934 /* If this is a dynamic object, we don't want to do anything here:
8935 we don't want the local symbols, and we don't want the section
8937 if ((input_bfd->flags & DYNAMIC) != 0)
8940 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8941 if (elf_bad_symtab (input_bfd))
8943 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8948 locsymcount = symtab_hdr->sh_info;
8949 extsymoff = symtab_hdr->sh_info;
8952 /* Read the local symbols. */
8953 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8954 if (isymbuf == NULL && locsymcount != 0)
8956 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8957 finfo->internal_syms,
8958 finfo->external_syms,
8959 finfo->locsym_shndx);
8960 if (isymbuf == NULL)
8964 /* Find local symbol sections and adjust values of symbols in
8965 SEC_MERGE sections. Write out those local symbols we know are
8966 going into the output file. */
8967 isymend = isymbuf + locsymcount;
8968 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
8970 isym++, pindex++, ppsection++)
8974 Elf_Internal_Sym osym;
8978 if (elf_bad_symtab (input_bfd))
8980 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
8987 if (isym->st_shndx == SHN_UNDEF)
8988 isec = bfd_und_section_ptr;
8989 else if (isym->st_shndx == SHN_ABS)
8990 isec = bfd_abs_section_ptr;
8991 else if (isym->st_shndx == SHN_COMMON)
8992 isec = bfd_com_section_ptr;
8995 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
8998 /* Don't attempt to output symbols with st_shnx in the
8999 reserved range other than SHN_ABS and SHN_COMMON. */
9003 else if (isec->sec_info_type == ELF_INFO_TYPE_MERGE
9004 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9006 _bfd_merged_section_offset (output_bfd, &isec,
9007 elf_section_data (isec)->sec_info,
9013 /* Don't output the first, undefined, symbol. */
9014 if (ppsection == finfo->sections)
9017 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9019 /* We never output section symbols. Instead, we use the
9020 section symbol of the corresponding section in the output
9025 /* If we are stripping all symbols, we don't want to output this
9027 if (finfo->info->strip == strip_all)
9030 /* If we are discarding all local symbols, we don't want to
9031 output this one. If we are generating a relocatable output
9032 file, then some of the local symbols may be required by
9033 relocs; we output them below as we discover that they are
9035 if (finfo->info->discard == discard_all)
9038 /* If this symbol is defined in a section which we are
9039 discarding, we don't need to keep it. */
9040 if (isym->st_shndx != SHN_UNDEF
9041 && isym->st_shndx < SHN_LORESERVE
9042 && bfd_section_removed_from_list (output_bfd,
9043 isec->output_section))
9046 /* Get the name of the symbol. */
9047 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9052 /* See if we are discarding symbols with this name. */
9053 if ((finfo->info->strip == strip_some
9054 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
9056 || (((finfo->info->discard == discard_sec_merge
9057 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
9058 || finfo->info->discard == discard_l)
9059 && bfd_is_local_label_name (input_bfd, name)))
9062 /* If we get here, we are going to output this symbol. */
9066 /* Adjust the section index for the output file. */
9067 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9068 isec->output_section);
9069 if (osym.st_shndx == SHN_BAD)
9072 *pindex = bfd_get_symcount (output_bfd);
9074 /* ELF symbols in relocatable files are section relative, but
9075 in executable files they are virtual addresses. Note that
9076 this code assumes that all ELF sections have an associated
9077 BFD section with a reasonable value for output_offset; below
9078 we assume that they also have a reasonable value for
9079 output_section. Any special sections must be set up to meet
9080 these requirements. */
9081 osym.st_value += isec->output_offset;
9082 if (! finfo->info->relocatable)
9084 osym.st_value += isec->output_section->vma;
9085 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9087 /* STT_TLS symbols are relative to PT_TLS segment base. */
9088 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
9089 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
9093 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
9097 /* Relocate the contents of each section. */
9098 sym_hashes = elf_sym_hashes (input_bfd);
9099 for (o = input_bfd->sections; o != NULL; o = o->next)
9103 if (! o->linker_mark)
9105 /* This section was omitted from the link. */
9109 if (finfo->info->relocatable
9110 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9112 /* Deal with the group signature symbol. */
9113 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9114 unsigned long symndx = sec_data->this_hdr.sh_info;
9115 asection *osec = o->output_section;
9117 if (symndx >= locsymcount
9118 || (elf_bad_symtab (input_bfd)
9119 && finfo->sections[symndx] == NULL))
9121 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9122 while (h->root.type == bfd_link_hash_indirect
9123 || h->root.type == bfd_link_hash_warning)
9124 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9125 /* Arrange for symbol to be output. */
9127 elf_section_data (osec)->this_hdr.sh_info = -2;
9129 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9131 /* We'll use the output section target_index. */
9132 asection *sec = finfo->sections[symndx]->output_section;
9133 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9137 if (finfo->indices[symndx] == -1)
9139 /* Otherwise output the local symbol now. */
9140 Elf_Internal_Sym sym = isymbuf[symndx];
9141 asection *sec = finfo->sections[symndx]->output_section;
9144 name = bfd_elf_string_from_elf_section (input_bfd,
9145 symtab_hdr->sh_link,
9150 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9152 if (sym.st_shndx == SHN_BAD)
9155 sym.st_value += o->output_offset;
9157 finfo->indices[symndx] = bfd_get_symcount (output_bfd);
9158 if (! elf_link_output_sym (finfo, name, &sym, o, NULL))
9161 elf_section_data (osec)->this_hdr.sh_info
9162 = finfo->indices[symndx];
9166 if ((o->flags & SEC_HAS_CONTENTS) == 0
9167 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9170 if ((o->flags & SEC_LINKER_CREATED) != 0)
9172 /* Section was created by _bfd_elf_link_create_dynamic_sections
9177 /* Get the contents of the section. They have been cached by a
9178 relaxation routine. Note that o is a section in an input
9179 file, so the contents field will not have been set by any of
9180 the routines which work on output files. */
9181 if (elf_section_data (o)->this_hdr.contents != NULL)
9182 contents = elf_section_data (o)->this_hdr.contents;
9185 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
9187 contents = finfo->contents;
9188 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
9192 if ((o->flags & SEC_RELOC) != 0)
9194 Elf_Internal_Rela *internal_relocs;
9195 Elf_Internal_Rela *rel, *relend;
9196 bfd_vma r_type_mask;
9198 int action_discarded;
9201 /* Get the swapped relocs. */
9203 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
9204 finfo->internal_relocs, FALSE);
9205 if (internal_relocs == NULL
9206 && o->reloc_count > 0)
9209 if (bed->s->arch_size == 32)
9216 r_type_mask = 0xffffffff;
9220 action_discarded = -1;
9221 if (!elf_section_ignore_discarded_relocs (o))
9222 action_discarded = (*bed->action_discarded) (o);
9224 /* Run through the relocs evaluating complex reloc symbols and
9225 looking for relocs against symbols from discarded sections
9226 or section symbols from removed link-once sections.
9227 Complain about relocs against discarded sections. Zero
9228 relocs against removed link-once sections. */
9230 rel = internal_relocs;
9231 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9232 for ( ; rel < relend; rel++)
9234 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9235 unsigned int s_type;
9236 asection **ps, *sec;
9237 struct elf_link_hash_entry *h = NULL;
9238 const char *sym_name;
9240 if (r_symndx == STN_UNDEF)
9243 if (r_symndx >= locsymcount
9244 || (elf_bad_symtab (input_bfd)
9245 && finfo->sections[r_symndx] == NULL))
9247 h = sym_hashes[r_symndx - extsymoff];
9249 /* Badly formatted input files can contain relocs that
9250 reference non-existant symbols. Check here so that
9251 we do not seg fault. */
9256 sprintf_vma (buffer, rel->r_info);
9257 (*_bfd_error_handler)
9258 (_("error: %B contains a reloc (0x%s) for section %A "
9259 "that references a non-existent global symbol"),
9260 input_bfd, o, buffer);
9261 bfd_set_error (bfd_error_bad_value);
9265 while (h->root.type == bfd_link_hash_indirect
9266 || h->root.type == bfd_link_hash_warning)
9267 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9272 if (h->root.type == bfd_link_hash_defined
9273 || h->root.type == bfd_link_hash_defweak)
9274 ps = &h->root.u.def.section;
9276 sym_name = h->root.root.string;
9280 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9282 s_type = ELF_ST_TYPE (sym->st_info);
9283 ps = &finfo->sections[r_symndx];
9284 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9288 if (s_type == STT_RELC || s_type == STT_SRELC)
9291 bfd_vma dot = (rel->r_offset
9292 + o->output_offset + o->output_section->vma);
9294 printf ("Encountered a complex symbol!");
9295 printf (" (input_bfd %s, section %s, reloc %ld\n",
9296 input_bfd->filename, o->name, rel - internal_relocs);
9297 printf (" symbol: idx %8.8lx, name %s\n",
9298 r_symndx, sym_name);
9299 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9300 (unsigned long) rel->r_info,
9301 (unsigned long) rel->r_offset);
9303 if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot,
9304 isymbuf, locsymcount, s_type == STT_SRELC))
9307 /* Symbol evaluated OK. Update to absolute value. */
9308 set_symbol_value (input_bfd, isymbuf, locsymcount,
9313 if (action_discarded != -1 && ps != NULL)
9315 /* Complain if the definition comes from a
9316 discarded section. */
9317 if ((sec = *ps) != NULL && elf_discarded_section (sec))
9319 BFD_ASSERT (r_symndx != 0);
9320 if (action_discarded & COMPLAIN)
9321 (*finfo->info->callbacks->einfo)
9322 (_("%X`%s' referenced in section `%A' of %B: "
9323 "defined in discarded section `%A' of %B\n"),
9324 sym_name, o, input_bfd, sec, sec->owner);
9326 /* Try to do the best we can to support buggy old
9327 versions of gcc. Pretend that the symbol is
9328 really defined in the kept linkonce section.
9329 FIXME: This is quite broken. Modifying the
9330 symbol here means we will be changing all later
9331 uses of the symbol, not just in this section. */
9332 if (action_discarded & PRETEND)
9336 kept = _bfd_elf_check_kept_section (sec,
9348 /* Relocate the section by invoking a back end routine.
9350 The back end routine is responsible for adjusting the
9351 section contents as necessary, and (if using Rela relocs
9352 and generating a relocatable output file) adjusting the
9353 reloc addend as necessary.
9355 The back end routine does not have to worry about setting
9356 the reloc address or the reloc symbol index.
9358 The back end routine is given a pointer to the swapped in
9359 internal symbols, and can access the hash table entries
9360 for the external symbols via elf_sym_hashes (input_bfd).
9362 When generating relocatable output, the back end routine
9363 must handle STB_LOCAL/STT_SECTION symbols specially. The
9364 output symbol is going to be a section symbol
9365 corresponding to the output section, which will require
9366 the addend to be adjusted. */
9368 ret = (*relocate_section) (output_bfd, finfo->info,
9369 input_bfd, o, contents,
9377 || finfo->info->relocatable
9378 || finfo->info->emitrelocations)
9380 Elf_Internal_Rela *irela;
9381 Elf_Internal_Rela *irelaend;
9382 bfd_vma last_offset;
9383 struct elf_link_hash_entry **rel_hash;
9384 struct elf_link_hash_entry **rel_hash_list;
9385 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
9386 unsigned int next_erel;
9387 bfd_boolean rela_normal;
9389 input_rel_hdr = &elf_section_data (o)->rel_hdr;
9390 rela_normal = (bed->rela_normal
9391 && (input_rel_hdr->sh_entsize
9392 == bed->s->sizeof_rela));
9394 /* Adjust the reloc addresses and symbol indices. */
9396 irela = internal_relocs;
9397 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
9398 rel_hash = (elf_section_data (o->output_section)->rel_hashes
9399 + elf_section_data (o->output_section)->rel_count
9400 + elf_section_data (o->output_section)->rel_count2);
9401 rel_hash_list = rel_hash;
9402 last_offset = o->output_offset;
9403 if (!finfo->info->relocatable)
9404 last_offset += o->output_section->vma;
9405 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9407 unsigned long r_symndx;
9409 Elf_Internal_Sym sym;
9411 if (next_erel == bed->s->int_rels_per_ext_rel)
9417 irela->r_offset = _bfd_elf_section_offset (output_bfd,
9420 if (irela->r_offset >= (bfd_vma) -2)
9422 /* This is a reloc for a deleted entry or somesuch.
9423 Turn it into an R_*_NONE reloc, at the same
9424 offset as the last reloc. elf_eh_frame.c and
9425 bfd_elf_discard_info rely on reloc offsets
9427 irela->r_offset = last_offset;
9429 irela->r_addend = 0;
9433 irela->r_offset += o->output_offset;
9435 /* Relocs in an executable have to be virtual addresses. */
9436 if (!finfo->info->relocatable)
9437 irela->r_offset += o->output_section->vma;
9439 last_offset = irela->r_offset;
9441 r_symndx = irela->r_info >> r_sym_shift;
9442 if (r_symndx == STN_UNDEF)
9445 if (r_symndx >= locsymcount
9446 || (elf_bad_symtab (input_bfd)
9447 && finfo->sections[r_symndx] == NULL))
9449 struct elf_link_hash_entry *rh;
9452 /* This is a reloc against a global symbol. We
9453 have not yet output all the local symbols, so
9454 we do not know the symbol index of any global
9455 symbol. We set the rel_hash entry for this
9456 reloc to point to the global hash table entry
9457 for this symbol. The symbol index is then
9458 set at the end of bfd_elf_final_link. */
9459 indx = r_symndx - extsymoff;
9460 rh = elf_sym_hashes (input_bfd)[indx];
9461 while (rh->root.type == bfd_link_hash_indirect
9462 || rh->root.type == bfd_link_hash_warning)
9463 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9465 /* Setting the index to -2 tells
9466 elf_link_output_extsym that this symbol is
9468 BFD_ASSERT (rh->indx < 0);
9476 /* This is a reloc against a local symbol. */
9479 sym = isymbuf[r_symndx];
9480 sec = finfo->sections[r_symndx];
9481 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9483 /* I suppose the backend ought to fill in the
9484 section of any STT_SECTION symbol against a
9485 processor specific section. */
9487 if (bfd_is_abs_section (sec))
9489 else if (sec == NULL || sec->owner == NULL)
9491 bfd_set_error (bfd_error_bad_value);
9496 asection *osec = sec->output_section;
9498 /* If we have discarded a section, the output
9499 section will be the absolute section. In
9500 case of discarded SEC_MERGE sections, use
9501 the kept section. relocate_section should
9502 have already handled discarded linkonce
9504 if (bfd_is_abs_section (osec)
9505 && sec->kept_section != NULL
9506 && sec->kept_section->output_section != NULL)
9508 osec = sec->kept_section->output_section;
9509 irela->r_addend -= osec->vma;
9512 if (!bfd_is_abs_section (osec))
9514 r_symndx = osec->target_index;
9517 struct elf_link_hash_table *htab;
9520 htab = elf_hash_table (finfo->info);
9521 oi = htab->text_index_section;
9522 if ((osec->flags & SEC_READONLY) == 0
9523 && htab->data_index_section != NULL)
9524 oi = htab->data_index_section;
9528 irela->r_addend += osec->vma - oi->vma;
9529 r_symndx = oi->target_index;
9533 BFD_ASSERT (r_symndx != 0);
9537 /* Adjust the addend according to where the
9538 section winds up in the output section. */
9540 irela->r_addend += sec->output_offset;
9544 if (finfo->indices[r_symndx] == -1)
9546 unsigned long shlink;
9550 if (finfo->info->strip == strip_all)
9552 /* You can't do ld -r -s. */
9553 bfd_set_error (bfd_error_invalid_operation);
9557 /* This symbol was skipped earlier, but
9558 since it is needed by a reloc, we
9559 must output it now. */
9560 shlink = symtab_hdr->sh_link;
9561 name = (bfd_elf_string_from_elf_section
9562 (input_bfd, shlink, sym.st_name));
9566 osec = sec->output_section;
9568 _bfd_elf_section_from_bfd_section (output_bfd,
9570 if (sym.st_shndx == SHN_BAD)
9573 sym.st_value += sec->output_offset;
9574 if (! finfo->info->relocatable)
9576 sym.st_value += osec->vma;
9577 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9579 /* STT_TLS symbols are relative to PT_TLS
9581 BFD_ASSERT (elf_hash_table (finfo->info)
9583 sym.st_value -= (elf_hash_table (finfo->info)
9588 finfo->indices[r_symndx]
9589 = bfd_get_symcount (output_bfd);
9591 if (! elf_link_output_sym (finfo, name, &sym, sec,
9596 r_symndx = finfo->indices[r_symndx];
9599 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9600 | (irela->r_info & r_type_mask));
9603 /* Swap out the relocs. */
9604 if (input_rel_hdr->sh_size != 0
9605 && !bed->elf_backend_emit_relocs (output_bfd, o,
9611 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
9612 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
9614 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9615 * bed->s->int_rels_per_ext_rel);
9616 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
9617 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9626 /* Write out the modified section contents. */
9627 if (bed->elf_backend_write_section
9628 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
9631 /* Section written out. */
9633 else switch (o->sec_info_type)
9635 case ELF_INFO_TYPE_STABS:
9636 if (! (_bfd_write_section_stabs
9638 &elf_hash_table (finfo->info)->stab_info,
9639 o, &elf_section_data (o)->sec_info, contents)))
9642 case ELF_INFO_TYPE_MERGE:
9643 if (! _bfd_write_merged_section (output_bfd, o,
9644 elf_section_data (o)->sec_info))
9647 case ELF_INFO_TYPE_EH_FRAME:
9649 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
9656 if (! (o->flags & SEC_EXCLUDE)
9657 && ! (o->output_section->flags & SEC_NEVER_LOAD)
9658 && ! bfd_set_section_contents (output_bfd, o->output_section,
9660 (file_ptr) o->output_offset,
9671 /* Generate a reloc when linking an ELF file. This is a reloc
9672 requested by the linker, and does not come from any input file. This
9673 is used to build constructor and destructor tables when linking
9677 elf_reloc_link_order (bfd *output_bfd,
9678 struct bfd_link_info *info,
9679 asection *output_section,
9680 struct bfd_link_order *link_order)
9682 reloc_howto_type *howto;
9686 struct elf_link_hash_entry **rel_hash_ptr;
9687 Elf_Internal_Shdr *rel_hdr;
9688 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9689 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9693 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9696 bfd_set_error (bfd_error_bad_value);
9700 addend = link_order->u.reloc.p->addend;
9702 /* Figure out the symbol index. */
9703 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
9704 + elf_section_data (output_section)->rel_count
9705 + elf_section_data (output_section)->rel_count2);
9706 if (link_order->type == bfd_section_reloc_link_order)
9708 indx = link_order->u.reloc.p->u.section->target_index;
9709 BFD_ASSERT (indx != 0);
9710 *rel_hash_ptr = NULL;
9714 struct elf_link_hash_entry *h;
9716 /* Treat a reloc against a defined symbol as though it were
9717 actually against the section. */
9718 h = ((struct elf_link_hash_entry *)
9719 bfd_wrapped_link_hash_lookup (output_bfd, info,
9720 link_order->u.reloc.p->u.name,
9721 FALSE, FALSE, TRUE));
9723 && (h->root.type == bfd_link_hash_defined
9724 || h->root.type == bfd_link_hash_defweak))
9728 section = h->root.u.def.section;
9729 indx = section->output_section->target_index;
9730 *rel_hash_ptr = NULL;
9731 /* It seems that we ought to add the symbol value to the
9732 addend here, but in practice it has already been added
9733 because it was passed to constructor_callback. */
9734 addend += section->output_section->vma + section->output_offset;
9738 /* Setting the index to -2 tells elf_link_output_extsym that
9739 this symbol is used by a reloc. */
9746 if (! ((*info->callbacks->unattached_reloc)
9747 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
9753 /* If this is an inplace reloc, we must write the addend into the
9755 if (howto->partial_inplace && addend != 0)
9758 bfd_reloc_status_type rstat;
9761 const char *sym_name;
9763 size = bfd_get_reloc_size (howto);
9764 buf = bfd_zmalloc (size);
9767 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
9774 case bfd_reloc_outofrange:
9777 case bfd_reloc_overflow:
9778 if (link_order->type == bfd_section_reloc_link_order)
9779 sym_name = bfd_section_name (output_bfd,
9780 link_order->u.reloc.p->u.section);
9782 sym_name = link_order->u.reloc.p->u.name;
9783 if (! ((*info->callbacks->reloc_overflow)
9784 (info, NULL, sym_name, howto->name, addend, NULL,
9785 NULL, (bfd_vma) 0)))
9792 ok = bfd_set_section_contents (output_bfd, output_section, buf,
9793 link_order->offset, size);
9799 /* The address of a reloc is relative to the section in a
9800 relocatable file, and is a virtual address in an executable
9802 offset = link_order->offset;
9803 if (! info->relocatable)
9804 offset += output_section->vma;
9806 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
9808 irel[i].r_offset = offset;
9810 irel[i].r_addend = 0;
9812 if (bed->s->arch_size == 32)
9813 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
9815 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
9817 rel_hdr = &elf_section_data (output_section)->rel_hdr;
9818 erel = rel_hdr->contents;
9819 if (rel_hdr->sh_type == SHT_REL)
9821 erel += (elf_section_data (output_section)->rel_count
9822 * bed->s->sizeof_rel);
9823 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
9827 irel[0].r_addend = addend;
9828 erel += (elf_section_data (output_section)->rel_count
9829 * bed->s->sizeof_rela);
9830 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
9833 ++elf_section_data (output_section)->rel_count;
9839 /* Get the output vma of the section pointed to by the sh_link field. */
9842 elf_get_linked_section_vma (struct bfd_link_order *p)
9844 Elf_Internal_Shdr **elf_shdrp;
9848 s = p->u.indirect.section;
9849 elf_shdrp = elf_elfsections (s->owner);
9850 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
9851 elfsec = elf_shdrp[elfsec]->sh_link;
9853 The Intel C compiler generates SHT_IA_64_UNWIND with
9854 SHF_LINK_ORDER. But it doesn't set the sh_link or
9855 sh_info fields. Hence we could get the situation
9856 where elfsec is 0. */
9859 const struct elf_backend_data *bed
9860 = get_elf_backend_data (s->owner);
9861 if (bed->link_order_error_handler)
9862 bed->link_order_error_handler
9863 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
9868 s = elf_shdrp[elfsec]->bfd_section;
9869 return s->output_section->vma + s->output_offset;
9874 /* Compare two sections based on the locations of the sections they are
9875 linked to. Used by elf_fixup_link_order. */
9878 compare_link_order (const void * a, const void * b)
9883 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
9884 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
9891 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9892 order as their linked sections. Returns false if this could not be done
9893 because an output section includes both ordered and unordered
9894 sections. Ideally we'd do this in the linker proper. */
9897 elf_fixup_link_order (bfd *abfd, asection *o)
9902 struct bfd_link_order *p;
9904 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9906 struct bfd_link_order **sections;
9907 asection *s, *other_sec, *linkorder_sec;
9911 linkorder_sec = NULL;
9914 for (p = o->map_head.link_order; p != NULL; p = p->next)
9916 if (p->type == bfd_indirect_link_order)
9918 s = p->u.indirect.section;
9920 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
9921 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
9922 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
9923 && elfsec < elf_numsections (sub)
9924 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
9925 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
9939 if (seen_other && seen_linkorder)
9941 if (other_sec && linkorder_sec)
9942 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9944 linkorder_sec->owner, other_sec,
9947 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
9949 bfd_set_error (bfd_error_bad_value);
9954 if (!seen_linkorder)
9957 sections = (struct bfd_link_order **)
9958 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
9959 if (sections == NULL)
9963 for (p = o->map_head.link_order; p != NULL; p = p->next)
9965 sections[seen_linkorder++] = p;
9967 /* Sort the input sections in the order of their linked section. */
9968 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
9969 compare_link_order);
9971 /* Change the offsets of the sections. */
9973 for (n = 0; n < seen_linkorder; n++)
9975 s = sections[n]->u.indirect.section;
9976 offset &= ~(bfd_vma) 0 << s->alignment_power;
9977 s->output_offset = offset;
9978 sections[n]->offset = offset;
9979 offset += sections[n]->size;
9987 /* Do the final step of an ELF link. */
9990 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
9992 bfd_boolean dynamic;
9993 bfd_boolean emit_relocs;
9995 struct elf_final_link_info finfo;
9996 register asection *o;
9997 register struct bfd_link_order *p;
9999 bfd_size_type max_contents_size;
10000 bfd_size_type max_external_reloc_size;
10001 bfd_size_type max_internal_reloc_count;
10002 bfd_size_type max_sym_count;
10003 bfd_size_type max_sym_shndx_count;
10005 Elf_Internal_Sym elfsym;
10007 Elf_Internal_Shdr *symtab_hdr;
10008 Elf_Internal_Shdr *symtab_shndx_hdr;
10009 Elf_Internal_Shdr *symstrtab_hdr;
10010 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10011 struct elf_outext_info eoinfo;
10012 bfd_boolean merged;
10013 size_t relativecount = 0;
10014 asection *reldyn = 0;
10016 asection *attr_section = NULL;
10017 bfd_vma attr_size = 0;
10018 const char *std_attrs_section;
10020 if (! is_elf_hash_table (info->hash))
10024 abfd->flags |= DYNAMIC;
10026 dynamic = elf_hash_table (info)->dynamic_sections_created;
10027 dynobj = elf_hash_table (info)->dynobj;
10029 emit_relocs = (info->relocatable
10030 || info->emitrelocations);
10033 finfo.output_bfd = abfd;
10034 finfo.symstrtab = _bfd_elf_stringtab_init ();
10035 if (finfo.symstrtab == NULL)
10040 finfo.dynsym_sec = NULL;
10041 finfo.hash_sec = NULL;
10042 finfo.symver_sec = NULL;
10046 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
10047 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
10048 BFD_ASSERT (finfo.dynsym_sec != NULL);
10049 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
10050 /* Note that it is OK if symver_sec is NULL. */
10053 finfo.contents = NULL;
10054 finfo.external_relocs = NULL;
10055 finfo.internal_relocs = NULL;
10056 finfo.external_syms = NULL;
10057 finfo.locsym_shndx = NULL;
10058 finfo.internal_syms = NULL;
10059 finfo.indices = NULL;
10060 finfo.sections = NULL;
10061 finfo.symbuf = NULL;
10062 finfo.symshndxbuf = NULL;
10063 finfo.symbuf_count = 0;
10064 finfo.shndxbuf_size = 0;
10066 /* The object attributes have been merged. Remove the input
10067 sections from the link, and set the contents of the output
10069 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10070 for (o = abfd->sections; o != NULL; o = o->next)
10072 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10073 || strcmp (o->name, ".gnu.attributes") == 0)
10075 for (p = o->map_head.link_order; p != NULL; p = p->next)
10077 asection *input_section;
10079 if (p->type != bfd_indirect_link_order)
10081 input_section = p->u.indirect.section;
10082 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10083 elf_link_input_bfd ignores this section. */
10084 input_section->flags &= ~SEC_HAS_CONTENTS;
10087 attr_size = bfd_elf_obj_attr_size (abfd);
10090 bfd_set_section_size (abfd, o, attr_size);
10092 /* Skip this section later on. */
10093 o->map_head.link_order = NULL;
10096 o->flags |= SEC_EXCLUDE;
10100 /* Count up the number of relocations we will output for each output
10101 section, so that we know the sizes of the reloc sections. We
10102 also figure out some maximum sizes. */
10103 max_contents_size = 0;
10104 max_external_reloc_size = 0;
10105 max_internal_reloc_count = 0;
10107 max_sym_shndx_count = 0;
10109 for (o = abfd->sections; o != NULL; o = o->next)
10111 struct bfd_elf_section_data *esdo = elf_section_data (o);
10112 o->reloc_count = 0;
10114 for (p = o->map_head.link_order; p != NULL; p = p->next)
10116 unsigned int reloc_count = 0;
10117 struct bfd_elf_section_data *esdi = NULL;
10118 unsigned int *rel_count1;
10120 if (p->type == bfd_section_reloc_link_order
10121 || p->type == bfd_symbol_reloc_link_order)
10123 else if (p->type == bfd_indirect_link_order)
10127 sec = p->u.indirect.section;
10128 esdi = elf_section_data (sec);
10130 /* Mark all sections which are to be included in the
10131 link. This will normally be every section. We need
10132 to do this so that we can identify any sections which
10133 the linker has decided to not include. */
10134 sec->linker_mark = TRUE;
10136 if (sec->flags & SEC_MERGE)
10139 if (info->relocatable || info->emitrelocations)
10140 reloc_count = sec->reloc_count;
10141 else if (bed->elf_backend_count_relocs)
10142 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
10144 if (sec->rawsize > max_contents_size)
10145 max_contents_size = sec->rawsize;
10146 if (sec->size > max_contents_size)
10147 max_contents_size = sec->size;
10149 /* We are interested in just local symbols, not all
10151 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10152 && (sec->owner->flags & DYNAMIC) == 0)
10156 if (elf_bad_symtab (sec->owner))
10157 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10158 / bed->s->sizeof_sym);
10160 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10162 if (sym_count > max_sym_count)
10163 max_sym_count = sym_count;
10165 if (sym_count > max_sym_shndx_count
10166 && elf_symtab_shndx (sec->owner) != 0)
10167 max_sym_shndx_count = sym_count;
10169 if ((sec->flags & SEC_RELOC) != 0)
10173 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
10174 if (ext_size > max_external_reloc_size)
10175 max_external_reloc_size = ext_size;
10176 if (sec->reloc_count > max_internal_reloc_count)
10177 max_internal_reloc_count = sec->reloc_count;
10182 if (reloc_count == 0)
10185 o->reloc_count += reloc_count;
10187 /* MIPS may have a mix of REL and RELA relocs on sections.
10188 To support this curious ABI we keep reloc counts in
10189 elf_section_data too. We must be careful to add the
10190 relocations from the input section to the right output
10191 count. FIXME: Get rid of one count. We have
10192 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
10193 rel_count1 = &esdo->rel_count;
10196 bfd_boolean same_size;
10197 bfd_size_type entsize1;
10199 entsize1 = esdi->rel_hdr.sh_entsize;
10200 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
10201 || entsize1 == bed->s->sizeof_rela);
10202 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
10205 rel_count1 = &esdo->rel_count2;
10207 if (esdi->rel_hdr2 != NULL)
10209 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
10210 unsigned int alt_count;
10211 unsigned int *rel_count2;
10213 BFD_ASSERT (entsize2 != entsize1
10214 && (entsize2 == bed->s->sizeof_rel
10215 || entsize2 == bed->s->sizeof_rela));
10217 rel_count2 = &esdo->rel_count2;
10219 rel_count2 = &esdo->rel_count;
10221 /* The following is probably too simplistic if the
10222 backend counts output relocs unusually. */
10223 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
10224 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
10225 *rel_count2 += alt_count;
10226 reloc_count -= alt_count;
10229 *rel_count1 += reloc_count;
10232 if (o->reloc_count > 0)
10233 o->flags |= SEC_RELOC;
10236 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10237 set it (this is probably a bug) and if it is set
10238 assign_section_numbers will create a reloc section. */
10239 o->flags &=~ SEC_RELOC;
10242 /* If the SEC_ALLOC flag is not set, force the section VMA to
10243 zero. This is done in elf_fake_sections as well, but forcing
10244 the VMA to 0 here will ensure that relocs against these
10245 sections are handled correctly. */
10246 if ((o->flags & SEC_ALLOC) == 0
10247 && ! o->user_set_vma)
10251 if (! info->relocatable && merged)
10252 elf_link_hash_traverse (elf_hash_table (info),
10253 _bfd_elf_link_sec_merge_syms, abfd);
10255 /* Figure out the file positions for everything but the symbol table
10256 and the relocs. We set symcount to force assign_section_numbers
10257 to create a symbol table. */
10258 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10259 BFD_ASSERT (! abfd->output_has_begun);
10260 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10263 /* Set sizes, and assign file positions for reloc sections. */
10264 for (o = abfd->sections; o != NULL; o = o->next)
10266 if ((o->flags & SEC_RELOC) != 0)
10268 if (!(_bfd_elf_link_size_reloc_section
10269 (abfd, &elf_section_data (o)->rel_hdr, o)))
10272 if (elf_section_data (o)->rel_hdr2
10273 && !(_bfd_elf_link_size_reloc_section
10274 (abfd, elf_section_data (o)->rel_hdr2, o)))
10278 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10279 to count upwards while actually outputting the relocations. */
10280 elf_section_data (o)->rel_count = 0;
10281 elf_section_data (o)->rel_count2 = 0;
10284 _bfd_elf_assign_file_positions_for_relocs (abfd);
10286 /* We have now assigned file positions for all the sections except
10287 .symtab and .strtab. We start the .symtab section at the current
10288 file position, and write directly to it. We build the .strtab
10289 section in memory. */
10290 bfd_get_symcount (abfd) = 0;
10291 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10292 /* sh_name is set in prep_headers. */
10293 symtab_hdr->sh_type = SHT_SYMTAB;
10294 /* sh_flags, sh_addr and sh_size all start off zero. */
10295 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10296 /* sh_link is set in assign_section_numbers. */
10297 /* sh_info is set below. */
10298 /* sh_offset is set just below. */
10299 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
10301 off = elf_tdata (abfd)->next_file_pos;
10302 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10304 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10305 incorrect. We do not yet know the size of the .symtab section.
10306 We correct next_file_pos below, after we do know the size. */
10308 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10309 continuously seeking to the right position in the file. */
10310 if (! info->keep_memory || max_sym_count < 20)
10311 finfo.symbuf_size = 20;
10313 finfo.symbuf_size = max_sym_count;
10314 amt = finfo.symbuf_size;
10315 amt *= bed->s->sizeof_sym;
10316 finfo.symbuf = bfd_malloc (amt);
10317 if (finfo.symbuf == NULL)
10319 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
10321 /* Wild guess at number of output symbols. realloc'd as needed. */
10322 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10323 finfo.shndxbuf_size = amt;
10324 amt *= sizeof (Elf_External_Sym_Shndx);
10325 finfo.symshndxbuf = bfd_zmalloc (amt);
10326 if (finfo.symshndxbuf == NULL)
10330 /* Start writing out the symbol table. The first symbol is always a
10332 if (info->strip != strip_all
10335 elfsym.st_value = 0;
10336 elfsym.st_size = 0;
10337 elfsym.st_info = 0;
10338 elfsym.st_other = 0;
10339 elfsym.st_shndx = SHN_UNDEF;
10340 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
10345 /* Output a symbol for each section. We output these even if we are
10346 discarding local symbols, since they are used for relocs. These
10347 symbols have no names. We store the index of each one in the
10348 index field of the section, so that we can find it again when
10349 outputting relocs. */
10350 if (info->strip != strip_all
10353 elfsym.st_size = 0;
10354 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10355 elfsym.st_other = 0;
10356 elfsym.st_value = 0;
10357 for (i = 1; i < elf_numsections (abfd); i++)
10359 o = bfd_section_from_elf_index (abfd, i);
10362 o->target_index = bfd_get_symcount (abfd);
10363 elfsym.st_shndx = i;
10364 if (!info->relocatable)
10365 elfsym.st_value = o->vma;
10366 if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
10372 /* Allocate some memory to hold information read in from the input
10374 if (max_contents_size != 0)
10376 finfo.contents = bfd_malloc (max_contents_size);
10377 if (finfo.contents == NULL)
10381 if (max_external_reloc_size != 0)
10383 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
10384 if (finfo.external_relocs == NULL)
10388 if (max_internal_reloc_count != 0)
10390 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10391 amt *= sizeof (Elf_Internal_Rela);
10392 finfo.internal_relocs = bfd_malloc (amt);
10393 if (finfo.internal_relocs == NULL)
10397 if (max_sym_count != 0)
10399 amt = max_sym_count * bed->s->sizeof_sym;
10400 finfo.external_syms = bfd_malloc (amt);
10401 if (finfo.external_syms == NULL)
10404 amt = max_sym_count * sizeof (Elf_Internal_Sym);
10405 finfo.internal_syms = bfd_malloc (amt);
10406 if (finfo.internal_syms == NULL)
10409 amt = max_sym_count * sizeof (long);
10410 finfo.indices = bfd_malloc (amt);
10411 if (finfo.indices == NULL)
10414 amt = max_sym_count * sizeof (asection *);
10415 finfo.sections = bfd_malloc (amt);
10416 if (finfo.sections == NULL)
10420 if (max_sym_shndx_count != 0)
10422 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
10423 finfo.locsym_shndx = bfd_malloc (amt);
10424 if (finfo.locsym_shndx == NULL)
10428 if (elf_hash_table (info)->tls_sec)
10430 bfd_vma base, end = 0;
10433 for (sec = elf_hash_table (info)->tls_sec;
10434 sec && (sec->flags & SEC_THREAD_LOCAL);
10437 bfd_size_type size = sec->size;
10440 && (sec->flags & SEC_HAS_CONTENTS) == 0)
10442 struct bfd_link_order *o = sec->map_tail.link_order;
10444 size = o->offset + o->size;
10446 end = sec->vma + size;
10448 base = elf_hash_table (info)->tls_sec->vma;
10449 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
10450 elf_hash_table (info)->tls_size = end - base;
10453 /* Reorder SHF_LINK_ORDER sections. */
10454 for (o = abfd->sections; o != NULL; o = o->next)
10456 if (!elf_fixup_link_order (abfd, o))
10460 /* Since ELF permits relocations to be against local symbols, we
10461 must have the local symbols available when we do the relocations.
10462 Since we would rather only read the local symbols once, and we
10463 would rather not keep them in memory, we handle all the
10464 relocations for a single input file at the same time.
10466 Unfortunately, there is no way to know the total number of local
10467 symbols until we have seen all of them, and the local symbol
10468 indices precede the global symbol indices. This means that when
10469 we are generating relocatable output, and we see a reloc against
10470 a global symbol, we can not know the symbol index until we have
10471 finished examining all the local symbols to see which ones we are
10472 going to output. To deal with this, we keep the relocations in
10473 memory, and don't output them until the end of the link. This is
10474 an unfortunate waste of memory, but I don't see a good way around
10475 it. Fortunately, it only happens when performing a relocatable
10476 link, which is not the common case. FIXME: If keep_memory is set
10477 we could write the relocs out and then read them again; I don't
10478 know how bad the memory loss will be. */
10480 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10481 sub->output_has_begun = FALSE;
10482 for (o = abfd->sections; o != NULL; o = o->next)
10484 for (p = o->map_head.link_order; p != NULL; p = p->next)
10486 if (p->type == bfd_indirect_link_order
10487 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10488 == bfd_target_elf_flavour)
10489 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10491 if (! sub->output_has_begun)
10493 if (! elf_link_input_bfd (&finfo, sub))
10495 sub->output_has_begun = TRUE;
10498 else if (p->type == bfd_section_reloc_link_order
10499 || p->type == bfd_symbol_reloc_link_order)
10501 if (! elf_reloc_link_order (abfd, info, o, p))
10506 if (! _bfd_default_link_order (abfd, info, o, p))
10512 /* Free symbol buffer if needed. */
10513 if (!info->reduce_memory_overheads)
10515 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10516 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10517 && elf_tdata (sub)->symbuf)
10519 free (elf_tdata (sub)->symbuf);
10520 elf_tdata (sub)->symbuf = NULL;
10524 /* Output any global symbols that got converted to local in a
10525 version script or due to symbol visibility. We do this in a
10526 separate step since ELF requires all local symbols to appear
10527 prior to any global symbols. FIXME: We should only do this if
10528 some global symbols were, in fact, converted to become local.
10529 FIXME: Will this work correctly with the Irix 5 linker? */
10530 eoinfo.failed = FALSE;
10531 eoinfo.finfo = &finfo;
10532 eoinfo.localsyms = TRUE;
10533 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10538 /* If backend needs to output some local symbols not present in the hash
10539 table, do it now. */
10540 if (bed->elf_backend_output_arch_local_syms)
10542 typedef bfd_boolean (*out_sym_func)
10543 (void *, const char *, Elf_Internal_Sym *, asection *,
10544 struct elf_link_hash_entry *);
10546 if (! ((*bed->elf_backend_output_arch_local_syms)
10547 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10551 /* That wrote out all the local symbols. Finish up the symbol table
10552 with the global symbols. Even if we want to strip everything we
10553 can, we still need to deal with those global symbols that got
10554 converted to local in a version script. */
10556 /* The sh_info field records the index of the first non local symbol. */
10557 symtab_hdr->sh_info = bfd_get_symcount (abfd);
10560 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10562 Elf_Internal_Sym sym;
10563 bfd_byte *dynsym = finfo.dynsym_sec->contents;
10564 long last_local = 0;
10566 /* Write out the section symbols for the output sections. */
10567 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
10573 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10576 for (s = abfd->sections; s != NULL; s = s->next)
10582 dynindx = elf_section_data (s)->dynindx;
10585 indx = elf_section_data (s)->this_idx;
10586 BFD_ASSERT (indx > 0);
10587 sym.st_shndx = indx;
10588 if (! check_dynsym (abfd, &sym))
10590 sym.st_value = s->vma;
10591 dest = dynsym + dynindx * bed->s->sizeof_sym;
10592 if (last_local < dynindx)
10593 last_local = dynindx;
10594 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10598 /* Write out the local dynsyms. */
10599 if (elf_hash_table (info)->dynlocal)
10601 struct elf_link_local_dynamic_entry *e;
10602 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
10607 sym.st_size = e->isym.st_size;
10608 sym.st_other = e->isym.st_other;
10610 /* Copy the internal symbol as is.
10611 Note that we saved a word of storage and overwrote
10612 the original st_name with the dynstr_index. */
10615 s = bfd_section_from_elf_index (e->input_bfd,
10620 elf_section_data (s->output_section)->this_idx;
10621 if (! check_dynsym (abfd, &sym))
10623 sym.st_value = (s->output_section->vma
10625 + e->isym.st_value);
10628 if (last_local < e->dynindx)
10629 last_local = e->dynindx;
10631 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
10632 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10636 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
10640 /* We get the global symbols from the hash table. */
10641 eoinfo.failed = FALSE;
10642 eoinfo.localsyms = FALSE;
10643 eoinfo.finfo = &finfo;
10644 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10649 /* If backend needs to output some symbols not present in the hash
10650 table, do it now. */
10651 if (bed->elf_backend_output_arch_syms)
10653 typedef bfd_boolean (*out_sym_func)
10654 (void *, const char *, Elf_Internal_Sym *, asection *,
10655 struct elf_link_hash_entry *);
10657 if (! ((*bed->elf_backend_output_arch_syms)
10658 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10662 /* Flush all symbols to the file. */
10663 if (! elf_link_flush_output_syms (&finfo, bed))
10666 /* Now we know the size of the symtab section. */
10667 off += symtab_hdr->sh_size;
10669 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
10670 if (symtab_shndx_hdr->sh_name != 0)
10672 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
10673 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
10674 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
10675 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
10676 symtab_shndx_hdr->sh_size = amt;
10678 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
10681 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
10682 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
10687 /* Finish up and write out the symbol string table (.strtab)
10689 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
10690 /* sh_name was set in prep_headers. */
10691 symstrtab_hdr->sh_type = SHT_STRTAB;
10692 symstrtab_hdr->sh_flags = 0;
10693 symstrtab_hdr->sh_addr = 0;
10694 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10695 symstrtab_hdr->sh_entsize = 0;
10696 symstrtab_hdr->sh_link = 0;
10697 symstrtab_hdr->sh_info = 0;
10698 /* sh_offset is set just below. */
10699 symstrtab_hdr->sh_addralign = 1;
10701 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10702 elf_tdata (abfd)->next_file_pos = off;
10704 if (bfd_get_symcount (abfd) > 0)
10706 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10707 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10711 /* Adjust the relocs to have the correct symbol indices. */
10712 for (o = abfd->sections; o != NULL; o = o->next)
10714 if ((o->flags & SEC_RELOC) == 0)
10717 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
10718 elf_section_data (o)->rel_count,
10719 elf_section_data (o)->rel_hashes);
10720 if (elf_section_data (o)->rel_hdr2 != NULL)
10721 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
10722 elf_section_data (o)->rel_count2,
10723 (elf_section_data (o)->rel_hashes
10724 + elf_section_data (o)->rel_count));
10726 /* Set the reloc_count field to 0 to prevent write_relocs from
10727 trying to swap the relocs out itself. */
10728 o->reloc_count = 0;
10731 if (dynamic && info->combreloc && dynobj != NULL)
10732 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
10734 /* If we are linking against a dynamic object, or generating a
10735 shared library, finish up the dynamic linking information. */
10738 bfd_byte *dyncon, *dynconend;
10740 /* Fix up .dynamic entries. */
10741 o = bfd_get_section_by_name (dynobj, ".dynamic");
10742 BFD_ASSERT (o != NULL);
10744 dyncon = o->contents;
10745 dynconend = o->contents + o->size;
10746 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10748 Elf_Internal_Dyn dyn;
10752 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10759 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
10761 switch (elf_section_data (reldyn)->this_hdr.sh_type)
10763 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
10764 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
10767 dyn.d_un.d_val = relativecount;
10774 name = info->init_function;
10777 name = info->fini_function;
10780 struct elf_link_hash_entry *h;
10782 h = elf_link_hash_lookup (elf_hash_table (info), name,
10783 FALSE, FALSE, TRUE);
10785 && (h->root.type == bfd_link_hash_defined
10786 || h->root.type == bfd_link_hash_defweak))
10788 dyn.d_un.d_ptr = h->root.u.def.value;
10789 o = h->root.u.def.section;
10790 if (o->output_section != NULL)
10791 dyn.d_un.d_ptr += (o->output_section->vma
10792 + o->output_offset);
10795 /* The symbol is imported from another shared
10796 library and does not apply to this one. */
10797 dyn.d_un.d_ptr = 0;
10804 case DT_PREINIT_ARRAYSZ:
10805 name = ".preinit_array";
10807 case DT_INIT_ARRAYSZ:
10808 name = ".init_array";
10810 case DT_FINI_ARRAYSZ:
10811 name = ".fini_array";
10813 o = bfd_get_section_by_name (abfd, name);
10816 (*_bfd_error_handler)
10817 (_("%B: could not find output section %s"), abfd, name);
10821 (*_bfd_error_handler)
10822 (_("warning: %s section has zero size"), name);
10823 dyn.d_un.d_val = o->size;
10826 case DT_PREINIT_ARRAY:
10827 name = ".preinit_array";
10829 case DT_INIT_ARRAY:
10830 name = ".init_array";
10832 case DT_FINI_ARRAY:
10833 name = ".fini_array";
10840 name = ".gnu.hash";
10849 name = ".gnu.version_d";
10852 name = ".gnu.version_r";
10855 name = ".gnu.version";
10857 o = bfd_get_section_by_name (abfd, name);
10860 (*_bfd_error_handler)
10861 (_("%B: could not find output section %s"), abfd, name);
10864 dyn.d_un.d_ptr = o->vma;
10871 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
10875 dyn.d_un.d_val = 0;
10876 dyn.d_un.d_ptr = 0;
10877 for (i = 1; i < elf_numsections (abfd); i++)
10879 Elf_Internal_Shdr *hdr;
10881 hdr = elf_elfsections (abfd)[i];
10882 if (hdr->sh_type == type
10883 && (hdr->sh_flags & SHF_ALLOC) != 0)
10885 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
10886 dyn.d_un.d_val += hdr->sh_size;
10889 if (dyn.d_un.d_ptr == 0
10890 || hdr->sh_addr < dyn.d_un.d_ptr)
10891 dyn.d_un.d_ptr = hdr->sh_addr;
10897 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
10901 /* If we have created any dynamic sections, then output them. */
10902 if (dynobj != NULL)
10904 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
10907 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10908 if (info->warn_shared_textrel && info->shared)
10910 bfd_byte *dyncon, *dynconend;
10912 /* Fix up .dynamic entries. */
10913 o = bfd_get_section_by_name (dynobj, ".dynamic");
10914 BFD_ASSERT (o != NULL);
10916 dyncon = o->contents;
10917 dynconend = o->contents + o->size;
10918 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10920 Elf_Internal_Dyn dyn;
10922 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10924 if (dyn.d_tag == DT_TEXTREL)
10926 info->callbacks->einfo
10927 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
10933 for (o = dynobj->sections; o != NULL; o = o->next)
10935 if ((o->flags & SEC_HAS_CONTENTS) == 0
10937 || o->output_section == bfd_abs_section_ptr)
10939 if ((o->flags & SEC_LINKER_CREATED) == 0)
10941 /* At this point, we are only interested in sections
10942 created by _bfd_elf_link_create_dynamic_sections. */
10945 if (elf_hash_table (info)->stab_info.stabstr == o)
10947 if (elf_hash_table (info)->eh_info.hdr_sec == o)
10949 if ((elf_section_data (o->output_section)->this_hdr.sh_type
10951 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
10953 if (! bfd_set_section_contents (abfd, o->output_section,
10955 (file_ptr) o->output_offset,
10961 /* The contents of the .dynstr section are actually in a
10963 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
10964 if (bfd_seek (abfd, off, SEEK_SET) != 0
10965 || ! _bfd_elf_strtab_emit (abfd,
10966 elf_hash_table (info)->dynstr))
10972 if (info->relocatable)
10974 bfd_boolean failed = FALSE;
10976 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
10981 /* If we have optimized stabs strings, output them. */
10982 if (elf_hash_table (info)->stab_info.stabstr != NULL)
10984 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
10988 if (info->eh_frame_hdr)
10990 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
10994 if (finfo.symstrtab != NULL)
10995 _bfd_stringtab_free (finfo.symstrtab);
10996 if (finfo.contents != NULL)
10997 free (finfo.contents);
10998 if (finfo.external_relocs != NULL)
10999 free (finfo.external_relocs);
11000 if (finfo.internal_relocs != NULL)
11001 free (finfo.internal_relocs);
11002 if (finfo.external_syms != NULL)
11003 free (finfo.external_syms);
11004 if (finfo.locsym_shndx != NULL)
11005 free (finfo.locsym_shndx);
11006 if (finfo.internal_syms != NULL)
11007 free (finfo.internal_syms);
11008 if (finfo.indices != NULL)
11009 free (finfo.indices);
11010 if (finfo.sections != NULL)
11011 free (finfo.sections);
11012 if (finfo.symbuf != NULL)
11013 free (finfo.symbuf);
11014 if (finfo.symshndxbuf != NULL)
11015 free (finfo.symshndxbuf);
11016 for (o = abfd->sections; o != NULL; o = o->next)
11018 if ((o->flags & SEC_RELOC) != 0
11019 && elf_section_data (o)->rel_hashes != NULL)
11020 free (elf_section_data (o)->rel_hashes);
11023 elf_tdata (abfd)->linker = TRUE;
11027 bfd_byte *contents = bfd_malloc (attr_size);
11028 if (contents == NULL)
11029 return FALSE; /* Bail out and fail. */
11030 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11031 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11038 if (finfo.symstrtab != NULL)
11039 _bfd_stringtab_free (finfo.symstrtab);
11040 if (finfo.contents != NULL)
11041 free (finfo.contents);
11042 if (finfo.external_relocs != NULL)
11043 free (finfo.external_relocs);
11044 if (finfo.internal_relocs != NULL)
11045 free (finfo.internal_relocs);
11046 if (finfo.external_syms != NULL)
11047 free (finfo.external_syms);
11048 if (finfo.locsym_shndx != NULL)
11049 free (finfo.locsym_shndx);
11050 if (finfo.internal_syms != NULL)
11051 free (finfo.internal_syms);
11052 if (finfo.indices != NULL)
11053 free (finfo.indices);
11054 if (finfo.sections != NULL)
11055 free (finfo.sections);
11056 if (finfo.symbuf != NULL)
11057 free (finfo.symbuf);
11058 if (finfo.symshndxbuf != NULL)
11059 free (finfo.symshndxbuf);
11060 for (o = abfd->sections; o != NULL; o = o->next)
11062 if ((o->flags & SEC_RELOC) != 0
11063 && elf_section_data (o)->rel_hashes != NULL)
11064 free (elf_section_data (o)->rel_hashes);
11070 /* Initialize COOKIE for input bfd ABFD. */
11073 init_reloc_cookie (struct elf_reloc_cookie *cookie,
11074 struct bfd_link_info *info, bfd *abfd)
11076 Elf_Internal_Shdr *symtab_hdr;
11077 const struct elf_backend_data *bed;
11079 bed = get_elf_backend_data (abfd);
11080 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11082 cookie->abfd = abfd;
11083 cookie->sym_hashes = elf_sym_hashes (abfd);
11084 cookie->bad_symtab = elf_bad_symtab (abfd);
11085 if (cookie->bad_symtab)
11087 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11088 cookie->extsymoff = 0;
11092 cookie->locsymcount = symtab_hdr->sh_info;
11093 cookie->extsymoff = symtab_hdr->sh_info;
11096 if (bed->s->arch_size == 32)
11097 cookie->r_sym_shift = 8;
11099 cookie->r_sym_shift = 32;
11101 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11102 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11104 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11105 cookie->locsymcount, 0,
11107 if (cookie->locsyms == NULL)
11109 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11112 if (info->keep_memory)
11113 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11118 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11121 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11123 Elf_Internal_Shdr *symtab_hdr;
11125 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11126 if (cookie->locsyms != NULL
11127 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11128 free (cookie->locsyms);
11131 /* Initialize the relocation information in COOKIE for input section SEC
11132 of input bfd ABFD. */
11135 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11136 struct bfd_link_info *info, bfd *abfd,
11139 const struct elf_backend_data *bed;
11141 if (sec->reloc_count == 0)
11143 cookie->rels = NULL;
11144 cookie->relend = NULL;
11148 bed = get_elf_backend_data (abfd);
11150 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11151 info->keep_memory);
11152 if (cookie->rels == NULL)
11154 cookie->rel = cookie->rels;
11155 cookie->relend = (cookie->rels
11156 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11158 cookie->rel = cookie->rels;
11162 /* Free the memory allocated by init_reloc_cookie_rels,
11166 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11169 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11170 free (cookie->rels);
11173 /* Initialize the whole of COOKIE for input section SEC. */
11176 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11177 struct bfd_link_info *info,
11180 if (!init_reloc_cookie (cookie, info, sec->owner))
11182 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11187 fini_reloc_cookie (cookie, sec->owner);
11192 /* Free the memory allocated by init_reloc_cookie_for_section,
11196 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11199 fini_reloc_cookie_rels (cookie, sec);
11200 fini_reloc_cookie (cookie, sec->owner);
11203 /* Garbage collect unused sections. */
11205 /* Default gc_mark_hook. */
11208 _bfd_elf_gc_mark_hook (asection *sec,
11209 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11210 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11211 struct elf_link_hash_entry *h,
11212 Elf_Internal_Sym *sym)
11216 switch (h->root.type)
11218 case bfd_link_hash_defined:
11219 case bfd_link_hash_defweak:
11220 return h->root.u.def.section;
11222 case bfd_link_hash_common:
11223 return h->root.u.c.p->section;
11230 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11235 /* COOKIE->rel describes a relocation against section SEC, which is
11236 a section we've decided to keep. Return the section that contains
11237 the relocation symbol, or NULL if no section contains it. */
11240 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11241 elf_gc_mark_hook_fn gc_mark_hook,
11242 struct elf_reloc_cookie *cookie)
11244 unsigned long r_symndx;
11245 struct elf_link_hash_entry *h;
11247 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
11251 if (r_symndx >= cookie->locsymcount
11252 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11254 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11255 while (h->root.type == bfd_link_hash_indirect
11256 || h->root.type == bfd_link_hash_warning)
11257 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11258 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11261 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11262 &cookie->locsyms[r_symndx]);
11265 /* COOKIE->rel describes a relocation against section SEC, which is
11266 a section we've decided to keep. Mark the section that contains
11267 the relocation symbol. */
11270 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11272 elf_gc_mark_hook_fn gc_mark_hook,
11273 struct elf_reloc_cookie *cookie)
11277 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11278 if (rsec && !rsec->gc_mark)
11280 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
11282 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11288 /* The mark phase of garbage collection. For a given section, mark
11289 it and any sections in this section's group, and all the sections
11290 which define symbols to which it refers. */
11293 _bfd_elf_gc_mark (struct bfd_link_info *info,
11295 elf_gc_mark_hook_fn gc_mark_hook)
11298 asection *group_sec, *eh_frame;
11302 /* Mark all the sections in the group. */
11303 group_sec = elf_section_data (sec)->next_in_group;
11304 if (group_sec && !group_sec->gc_mark)
11305 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
11308 /* Look through the section relocs. */
11310 eh_frame = elf_eh_frame_section (sec->owner);
11311 if ((sec->flags & SEC_RELOC) != 0
11312 && sec->reloc_count > 0
11313 && sec != eh_frame)
11315 struct elf_reloc_cookie cookie;
11317 if (!init_reloc_cookie_for_section (&cookie, info, sec))
11321 for (; cookie.rel < cookie.relend; cookie.rel++)
11322 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
11327 fini_reloc_cookie_for_section (&cookie, sec);
11331 if (ret && eh_frame && elf_fde_list (sec))
11333 struct elf_reloc_cookie cookie;
11335 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11339 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11340 gc_mark_hook, &cookie))
11342 fini_reloc_cookie_for_section (&cookie, eh_frame);
11349 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11351 struct elf_gc_sweep_symbol_info
11353 struct bfd_link_info *info;
11354 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11359 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
11361 if (h->root.type == bfd_link_hash_warning)
11362 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11364 if ((h->root.type == bfd_link_hash_defined
11365 || h->root.type == bfd_link_hash_defweak)
11366 && !h->root.u.def.section->gc_mark
11367 && !(h->root.u.def.section->owner->flags & DYNAMIC))
11369 struct elf_gc_sweep_symbol_info *inf = data;
11370 (*inf->hide_symbol) (inf->info, h, TRUE);
11376 /* The sweep phase of garbage collection. Remove all garbage sections. */
11378 typedef bfd_boolean (*gc_sweep_hook_fn)
11379 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11382 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
11385 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11386 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11387 unsigned long section_sym_count;
11388 struct elf_gc_sweep_symbol_info sweep_info;
11390 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11394 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11397 for (o = sub->sections; o != NULL; o = o->next)
11399 /* Keep debug and special sections. */
11400 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
11401 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
11407 /* Skip sweeping sections already excluded. */
11408 if (o->flags & SEC_EXCLUDE)
11411 /* Since this is early in the link process, it is simple
11412 to remove a section from the output. */
11413 o->flags |= SEC_EXCLUDE;
11415 if (info->print_gc_sections && o->size != 0)
11416 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11418 /* But we also have to update some of the relocation
11419 info we collected before. */
11421 && (o->flags & SEC_RELOC) != 0
11422 && o->reloc_count > 0
11423 && !bfd_is_abs_section (o->output_section))
11425 Elf_Internal_Rela *internal_relocs;
11429 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11430 info->keep_memory);
11431 if (internal_relocs == NULL)
11434 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11436 if (elf_section_data (o)->relocs != internal_relocs)
11437 free (internal_relocs);
11445 /* Remove the symbols that were in the swept sections from the dynamic
11446 symbol table. GCFIXME: Anyone know how to get them out of the
11447 static symbol table as well? */
11448 sweep_info.info = info;
11449 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11450 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11453 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
11457 /* Propagate collected vtable information. This is called through
11458 elf_link_hash_traverse. */
11461 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11463 if (h->root.type == bfd_link_hash_warning)
11464 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11466 /* Those that are not vtables. */
11467 if (h->vtable == NULL || h->vtable->parent == NULL)
11470 /* Those vtables that do not have parents, we cannot merge. */
11471 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
11474 /* If we've already been done, exit. */
11475 if (h->vtable->used && h->vtable->used[-1])
11478 /* Make sure the parent's table is up to date. */
11479 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
11481 if (h->vtable->used == NULL)
11483 /* None of this table's entries were referenced. Re-use the
11485 h->vtable->used = h->vtable->parent->vtable->used;
11486 h->vtable->size = h->vtable->parent->vtable->size;
11491 bfd_boolean *cu, *pu;
11493 /* Or the parent's entries into ours. */
11494 cu = h->vtable->used;
11496 pu = h->vtable->parent->vtable->used;
11499 const struct elf_backend_data *bed;
11500 unsigned int log_file_align;
11502 bed = get_elf_backend_data (h->root.u.def.section->owner);
11503 log_file_align = bed->s->log_file_align;
11504 n = h->vtable->parent->vtable->size >> log_file_align;
11519 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
11522 bfd_vma hstart, hend;
11523 Elf_Internal_Rela *relstart, *relend, *rel;
11524 const struct elf_backend_data *bed;
11525 unsigned int log_file_align;
11527 if (h->root.type == bfd_link_hash_warning)
11528 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11530 /* Take care of both those symbols that do not describe vtables as
11531 well as those that are not loaded. */
11532 if (h->vtable == NULL || h->vtable->parent == NULL)
11535 BFD_ASSERT (h->root.type == bfd_link_hash_defined
11536 || h->root.type == bfd_link_hash_defweak);
11538 sec = h->root.u.def.section;
11539 hstart = h->root.u.def.value;
11540 hend = hstart + h->size;
11542 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
11544 return *(bfd_boolean *) okp = FALSE;
11545 bed = get_elf_backend_data (sec->owner);
11546 log_file_align = bed->s->log_file_align;
11548 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11550 for (rel = relstart; rel < relend; ++rel)
11551 if (rel->r_offset >= hstart && rel->r_offset < hend)
11553 /* If the entry is in use, do nothing. */
11554 if (h->vtable->used
11555 && (rel->r_offset - hstart) < h->vtable->size)
11557 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
11558 if (h->vtable->used[entry])
11561 /* Otherwise, kill it. */
11562 rel->r_offset = rel->r_info = rel->r_addend = 0;
11568 /* Mark sections containing dynamically referenced symbols. When
11569 building shared libraries, we must assume that any visible symbol is
11573 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
11575 struct bfd_link_info *info = (struct bfd_link_info *) inf;
11577 if (h->root.type == bfd_link_hash_warning)
11578 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11580 if ((h->root.type == bfd_link_hash_defined
11581 || h->root.type == bfd_link_hash_defweak)
11583 || (!info->executable
11585 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
11586 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
11587 h->root.u.def.section->flags |= SEC_KEEP;
11592 /* Keep all sections containing symbols undefined on the command-line,
11593 and the section containing the entry symbol. */
11596 _bfd_elf_gc_keep (struct bfd_link_info *info)
11598 struct bfd_sym_chain *sym;
11600 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
11602 struct elf_link_hash_entry *h;
11604 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
11605 FALSE, FALSE, FALSE);
11608 && (h->root.type == bfd_link_hash_defined
11609 || h->root.type == bfd_link_hash_defweak)
11610 && !bfd_is_abs_section (h->root.u.def.section))
11611 h->root.u.def.section->flags |= SEC_KEEP;
11615 /* Do mark and sweep of unused sections. */
11618 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
11620 bfd_boolean ok = TRUE;
11622 elf_gc_mark_hook_fn gc_mark_hook;
11623 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11625 if (!bed->can_gc_sections
11626 || !is_elf_hash_table (info->hash))
11628 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
11632 bed->gc_keep (info);
11634 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
11635 at the .eh_frame section if we can mark the FDEs individually. */
11636 _bfd_elf_begin_eh_frame_parsing (info);
11637 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11640 struct elf_reloc_cookie cookie;
11642 sec = bfd_get_section_by_name (sub, ".eh_frame");
11643 if (sec && init_reloc_cookie_for_section (&cookie, info, sec))
11645 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
11646 if (elf_section_data (sec)->sec_info)
11647 elf_eh_frame_section (sub) = sec;
11648 fini_reloc_cookie_for_section (&cookie, sec);
11651 _bfd_elf_end_eh_frame_parsing (info);
11653 /* Apply transitive closure to the vtable entry usage info. */
11654 elf_link_hash_traverse (elf_hash_table (info),
11655 elf_gc_propagate_vtable_entries_used,
11660 /* Kill the vtable relocations that were not used. */
11661 elf_link_hash_traverse (elf_hash_table (info),
11662 elf_gc_smash_unused_vtentry_relocs,
11667 /* Mark dynamically referenced symbols. */
11668 if (elf_hash_table (info)->dynamic_sections_created)
11669 elf_link_hash_traverse (elf_hash_table (info),
11670 bed->gc_mark_dynamic_ref,
11673 /* Grovel through relocs to find out who stays ... */
11674 gc_mark_hook = bed->gc_mark_hook;
11675 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11679 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11682 for (o = sub->sections; o != NULL; o = o->next)
11683 if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark)
11684 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
11688 /* Allow the backend to mark additional target specific sections. */
11689 if (bed->gc_mark_extra_sections)
11690 bed->gc_mark_extra_sections (info, gc_mark_hook);
11692 /* ... and mark SEC_EXCLUDE for those that go. */
11693 return elf_gc_sweep (abfd, info);
11696 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
11699 bfd_elf_gc_record_vtinherit (bfd *abfd,
11701 struct elf_link_hash_entry *h,
11704 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
11705 struct elf_link_hash_entry **search, *child;
11706 bfd_size_type extsymcount;
11707 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11709 /* The sh_info field of the symtab header tells us where the
11710 external symbols start. We don't care about the local symbols at
11712 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
11713 if (!elf_bad_symtab (abfd))
11714 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
11716 sym_hashes = elf_sym_hashes (abfd);
11717 sym_hashes_end = sym_hashes + extsymcount;
11719 /* Hunt down the child symbol, which is in this section at the same
11720 offset as the relocation. */
11721 for (search = sym_hashes; search != sym_hashes_end; ++search)
11723 if ((child = *search) != NULL
11724 && (child->root.type == bfd_link_hash_defined
11725 || child->root.type == bfd_link_hash_defweak)
11726 && child->root.u.def.section == sec
11727 && child->root.u.def.value == offset)
11731 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
11732 abfd, sec, (unsigned long) offset);
11733 bfd_set_error (bfd_error_invalid_operation);
11737 if (!child->vtable)
11739 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
11740 if (!child->vtable)
11745 /* This *should* only be the absolute section. It could potentially
11746 be that someone has defined a non-global vtable though, which
11747 would be bad. It isn't worth paging in the local symbols to be
11748 sure though; that case should simply be handled by the assembler. */
11750 child->vtable->parent = (struct elf_link_hash_entry *) -1;
11753 child->vtable->parent = h;
11758 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
11761 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
11762 asection *sec ATTRIBUTE_UNUSED,
11763 struct elf_link_hash_entry *h,
11766 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11767 unsigned int log_file_align = bed->s->log_file_align;
11771 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
11776 if (addend >= h->vtable->size)
11778 size_t size, bytes, file_align;
11779 bfd_boolean *ptr = h->vtable->used;
11781 /* While the symbol is undefined, we have to be prepared to handle
11783 file_align = 1 << log_file_align;
11784 if (h->root.type == bfd_link_hash_undefined)
11785 size = addend + file_align;
11789 if (addend >= size)
11791 /* Oops! We've got a reference past the defined end of
11792 the table. This is probably a bug -- shall we warn? */
11793 size = addend + file_align;
11796 size = (size + file_align - 1) & -file_align;
11798 /* Allocate one extra entry for use as a "done" flag for the
11799 consolidation pass. */
11800 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
11804 ptr = bfd_realloc (ptr - 1, bytes);
11810 oldbytes = (((h->vtable->size >> log_file_align) + 1)
11811 * sizeof (bfd_boolean));
11812 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
11816 ptr = bfd_zmalloc (bytes);
11821 /* And arrange for that done flag to be at index -1. */
11822 h->vtable->used = ptr + 1;
11823 h->vtable->size = size;
11826 h->vtable->used[addend >> log_file_align] = TRUE;
11831 struct alloc_got_off_arg {
11833 struct bfd_link_info *info;
11836 /* We need a special top-level link routine to convert got reference counts
11837 to real got offsets. */
11840 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
11842 struct alloc_got_off_arg *gofarg = arg;
11843 bfd *obfd = gofarg->info->output_bfd;
11844 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
11846 if (h->root.type == bfd_link_hash_warning)
11847 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11849 if (h->got.refcount > 0)
11851 h->got.offset = gofarg->gotoff;
11852 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
11855 h->got.offset = (bfd_vma) -1;
11860 /* And an accompanying bit to work out final got entry offsets once
11861 we're done. Should be called from final_link. */
11864 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
11865 struct bfd_link_info *info)
11868 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11870 struct alloc_got_off_arg gofarg;
11872 BFD_ASSERT (abfd == info->output_bfd);
11874 if (! is_elf_hash_table (info->hash))
11877 /* The GOT offset is relative to the .got section, but the GOT header is
11878 put into the .got.plt section, if the backend uses it. */
11879 if (bed->want_got_plt)
11882 gotoff = bed->got_header_size;
11884 /* Do the local .got entries first. */
11885 for (i = info->input_bfds; i; i = i->link_next)
11887 bfd_signed_vma *local_got;
11888 bfd_size_type j, locsymcount;
11889 Elf_Internal_Shdr *symtab_hdr;
11891 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
11894 local_got = elf_local_got_refcounts (i);
11898 symtab_hdr = &elf_tdata (i)->symtab_hdr;
11899 if (elf_bad_symtab (i))
11900 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11902 locsymcount = symtab_hdr->sh_info;
11904 for (j = 0; j < locsymcount; ++j)
11906 if (local_got[j] > 0)
11908 local_got[j] = gotoff;
11909 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
11912 local_got[j] = (bfd_vma) -1;
11916 /* Then the global .got entries. .plt refcounts are handled by
11917 adjust_dynamic_symbol */
11918 gofarg.gotoff = gotoff;
11919 gofarg.info = info;
11920 elf_link_hash_traverse (elf_hash_table (info),
11921 elf_gc_allocate_got_offsets,
11926 /* Many folk need no more in the way of final link than this, once
11927 got entry reference counting is enabled. */
11930 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
11932 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
11935 /* Invoke the regular ELF backend linker to do all the work. */
11936 return bfd_elf_final_link (abfd, info);
11940 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
11942 struct elf_reloc_cookie *rcookie = cookie;
11944 if (rcookie->bad_symtab)
11945 rcookie->rel = rcookie->rels;
11947 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
11949 unsigned long r_symndx;
11951 if (! rcookie->bad_symtab)
11952 if (rcookie->rel->r_offset > offset)
11954 if (rcookie->rel->r_offset != offset)
11957 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
11958 if (r_symndx == SHN_UNDEF)
11961 if (r_symndx >= rcookie->locsymcount
11962 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11964 struct elf_link_hash_entry *h;
11966 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
11968 while (h->root.type == bfd_link_hash_indirect
11969 || h->root.type == bfd_link_hash_warning)
11970 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11972 if ((h->root.type == bfd_link_hash_defined
11973 || h->root.type == bfd_link_hash_defweak)
11974 && elf_discarded_section (h->root.u.def.section))
11981 /* It's not a relocation against a global symbol,
11982 but it could be a relocation against a local
11983 symbol for a discarded section. */
11985 Elf_Internal_Sym *isym;
11987 /* Need to: get the symbol; get the section. */
11988 isym = &rcookie->locsyms[r_symndx];
11989 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
11990 if (isec != NULL && elf_discarded_section (isec))
11998 /* Discard unneeded references to discarded sections.
11999 Returns TRUE if any section's size was changed. */
12000 /* This function assumes that the relocations are in sorted order,
12001 which is true for all known assemblers. */
12004 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
12006 struct elf_reloc_cookie cookie;
12007 asection *stab, *eh;
12008 const struct elf_backend_data *bed;
12010 bfd_boolean ret = FALSE;
12012 if (info->traditional_format
12013 || !is_elf_hash_table (info->hash))
12016 _bfd_elf_begin_eh_frame_parsing (info);
12017 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
12019 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
12022 bed = get_elf_backend_data (abfd);
12024 if ((abfd->flags & DYNAMIC) != 0)
12028 if (!info->relocatable)
12030 eh = bfd_get_section_by_name (abfd, ".eh_frame");
12033 || bfd_is_abs_section (eh->output_section)))
12037 stab = bfd_get_section_by_name (abfd, ".stab");
12039 && (stab->size == 0
12040 || bfd_is_abs_section (stab->output_section)
12041 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
12046 && bed->elf_backend_discard_info == NULL)
12049 if (!init_reloc_cookie (&cookie, info, abfd))
12053 && stab->reloc_count > 0
12054 && init_reloc_cookie_rels (&cookie, info, abfd, stab))
12056 if (_bfd_discard_section_stabs (abfd, stab,
12057 elf_section_data (stab)->sec_info,
12058 bfd_elf_reloc_symbol_deleted_p,
12061 fini_reloc_cookie_rels (&cookie, stab);
12065 && init_reloc_cookie_rels (&cookie, info, abfd, eh))
12067 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
12068 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
12069 bfd_elf_reloc_symbol_deleted_p,
12072 fini_reloc_cookie_rels (&cookie, eh);
12075 if (bed->elf_backend_discard_info != NULL
12076 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
12079 fini_reloc_cookie (&cookie, abfd);
12081 _bfd_elf_end_eh_frame_parsing (info);
12083 if (info->eh_frame_hdr
12084 && !info->relocatable
12085 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12091 /* For a SHT_GROUP section, return the group signature. For other
12092 sections, return the normal section name. */
12094 static const char *
12095 section_signature (asection *sec)
12097 if ((sec->flags & SEC_GROUP) != 0
12098 && elf_next_in_group (sec) != NULL
12099 && elf_group_name (elf_next_in_group (sec)) != NULL)
12100 return elf_group_name (elf_next_in_group (sec));
12105 _bfd_elf_section_already_linked (bfd *abfd, asection *sec,
12106 struct bfd_link_info *info)
12109 const char *name, *p;
12110 struct bfd_section_already_linked *l;
12111 struct bfd_section_already_linked_hash_entry *already_linked_list;
12113 if (sec->output_section == bfd_abs_section_ptr)
12116 flags = sec->flags;
12118 /* Return if it isn't a linkonce section. A comdat group section
12119 also has SEC_LINK_ONCE set. */
12120 if ((flags & SEC_LINK_ONCE) == 0)
12123 /* Don't put group member sections on our list of already linked
12124 sections. They are handled as a group via their group section. */
12125 if (elf_sec_group (sec) != NULL)
12128 /* FIXME: When doing a relocatable link, we may have trouble
12129 copying relocations in other sections that refer to local symbols
12130 in the section being discarded. Those relocations will have to
12131 be converted somehow; as of this writing I'm not sure that any of
12132 the backends handle that correctly.
12134 It is tempting to instead not discard link once sections when
12135 doing a relocatable link (technically, they should be discarded
12136 whenever we are building constructors). However, that fails,
12137 because the linker winds up combining all the link once sections
12138 into a single large link once section, which defeats the purpose
12139 of having link once sections in the first place.
12141 Also, not merging link once sections in a relocatable link
12142 causes trouble for MIPS ELF, which relies on link once semantics
12143 to handle the .reginfo section correctly. */
12145 name = section_signature (sec);
12147 if (CONST_STRNEQ (name, ".gnu.linkonce.")
12148 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12153 already_linked_list = bfd_section_already_linked_table_lookup (p);
12155 for (l = already_linked_list->entry; l != NULL; l = l->next)
12157 /* We may have 2 different types of sections on the list: group
12158 sections and linkonce sections. Match like sections. */
12159 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
12160 && strcmp (name, section_signature (l->sec)) == 0
12161 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
12163 /* The section has already been linked. See if we should
12164 issue a warning. */
12165 switch (flags & SEC_LINK_DUPLICATES)
12170 case SEC_LINK_DUPLICATES_DISCARD:
12173 case SEC_LINK_DUPLICATES_ONE_ONLY:
12174 (*_bfd_error_handler)
12175 (_("%B: ignoring duplicate section `%A'"),
12179 case SEC_LINK_DUPLICATES_SAME_SIZE:
12180 if (sec->size != l->sec->size)
12181 (*_bfd_error_handler)
12182 (_("%B: duplicate section `%A' has different size"),
12186 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
12187 if (sec->size != l->sec->size)
12188 (*_bfd_error_handler)
12189 (_("%B: duplicate section `%A' has different size"),
12191 else if (sec->size != 0)
12193 bfd_byte *sec_contents, *l_sec_contents;
12195 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
12196 (*_bfd_error_handler)
12197 (_("%B: warning: could not read contents of section `%A'"),
12199 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
12201 (*_bfd_error_handler)
12202 (_("%B: warning: could not read contents of section `%A'"),
12203 l->sec->owner, l->sec);
12204 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
12205 (*_bfd_error_handler)
12206 (_("%B: warning: duplicate section `%A' has different contents"),
12210 free (sec_contents);
12211 if (l_sec_contents)
12212 free (l_sec_contents);
12217 /* Set the output_section field so that lang_add_section
12218 does not create a lang_input_section structure for this
12219 section. Since there might be a symbol in the section
12220 being discarded, we must retain a pointer to the section
12221 which we are really going to use. */
12222 sec->output_section = bfd_abs_section_ptr;
12223 sec->kept_section = l->sec;
12225 if (flags & SEC_GROUP)
12227 asection *first = elf_next_in_group (sec);
12228 asection *s = first;
12232 s->output_section = bfd_abs_section_ptr;
12233 /* Record which group discards it. */
12234 s->kept_section = l->sec;
12235 s = elf_next_in_group (s);
12236 /* These lists are circular. */
12246 /* A single member comdat group section may be discarded by a
12247 linkonce section and vice versa. */
12249 if ((flags & SEC_GROUP) != 0)
12251 asection *first = elf_next_in_group (sec);
12253 if (first != NULL && elf_next_in_group (first) == first)
12254 /* Check this single member group against linkonce sections. */
12255 for (l = already_linked_list->entry; l != NULL; l = l->next)
12256 if ((l->sec->flags & SEC_GROUP) == 0
12257 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
12258 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12260 first->output_section = bfd_abs_section_ptr;
12261 first->kept_section = l->sec;
12262 sec->output_section = bfd_abs_section_ptr;
12267 /* Check this linkonce section against single member groups. */
12268 for (l = already_linked_list->entry; l != NULL; l = l->next)
12269 if (l->sec->flags & SEC_GROUP)
12271 asection *first = elf_next_in_group (l->sec);
12274 && elf_next_in_group (first) == first
12275 && bfd_elf_match_symbols_in_sections (first, sec, info))
12277 sec->output_section = bfd_abs_section_ptr;
12278 sec->kept_section = first;
12283 /* This is the first section with this name. Record it. */
12284 if (! bfd_section_already_linked_table_insert (already_linked_list, sec))
12285 info->callbacks->einfo (_("%F%P: already_linked_table: %E"));
12289 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
12291 return sym->st_shndx == SHN_COMMON;
12295 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12301 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12303 return bfd_com_section_ptr;
12307 _bfd_elf_default_got_elt_size (bfd *abfd,
12308 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12309 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
12310 bfd *ibfd ATTRIBUTE_UNUSED,
12311 unsigned long symndx ATTRIBUTE_UNUSED)
12313 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12314 return bed->s->arch_size / 8;
12317 /* Routines to support the creation of dynamic relocs. */
12319 /* Return true if NAME is a name of a relocation
12320 section associated with section S. */
12323 is_reloc_section (bfd_boolean rela, const char * name, asection * s)
12326 return CONST_STRNEQ (name, ".rela")
12327 && strcmp (bfd_get_section_name (NULL, s), name + 5) == 0;
12329 return CONST_STRNEQ (name, ".rel")
12330 && strcmp (bfd_get_section_name (NULL, s), name + 4) == 0;
12333 /* Returns the name of the dynamic reloc section associated with SEC. */
12335 static const char *
12336 get_dynamic_reloc_section_name (bfd * abfd,
12338 bfd_boolean is_rela)
12341 unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
12342 unsigned int shnam = elf_section_data (sec)->rel_hdr.sh_name;
12344 name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
12348 if (! is_reloc_section (is_rela, name, sec))
12350 static bfd_boolean complained = FALSE;
12354 (*_bfd_error_handler)
12355 (_("%B: bad relocation section name `%s\'"), abfd, name);
12364 /* Returns the dynamic reloc section associated with SEC.
12365 If necessary compute the name of the dynamic reloc section based
12366 on SEC's name (looked up in ABFD's string table) and the setting
12370 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
12372 bfd_boolean is_rela)
12374 asection * reloc_sec = elf_section_data (sec)->sreloc;
12376 if (reloc_sec == NULL)
12378 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12382 reloc_sec = bfd_get_section_by_name (abfd, name);
12384 if (reloc_sec != NULL)
12385 elf_section_data (sec)->sreloc = reloc_sec;
12392 /* Returns the dynamic reloc section associated with SEC. If the
12393 section does not exist it is created and attached to the DYNOBJ
12394 bfd and stored in the SRELOC field of SEC's elf_section_data
12397 ALIGNMENT is the alignment for the newly created section and
12398 IS_RELA defines whether the name should be .rela.<SEC's name>
12399 or .rel.<SEC's name>. The section name is looked up in the
12400 string table associated with ABFD. */
12403 _bfd_elf_make_dynamic_reloc_section (asection * sec,
12405 unsigned int alignment,
12407 bfd_boolean is_rela)
12409 asection * reloc_sec = elf_section_data (sec)->sreloc;
12411 if (reloc_sec == NULL)
12413 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12418 reloc_sec = bfd_get_section_by_name (dynobj, name);
12420 if (reloc_sec == NULL)
12424 flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_IN_MEMORY | SEC_LINKER_CREATED);
12425 if ((sec->flags & SEC_ALLOC) != 0)
12426 flags |= SEC_ALLOC | SEC_LOAD;
12428 reloc_sec = bfd_make_section_with_flags (dynobj, name, flags);
12429 if (reloc_sec != NULL)
12431 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
12436 elf_section_data (sec)->sreloc = reloc_sec;