1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007 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 2 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, MA 02110-1301, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
31 /* Define a symbol in a dynamic linkage section. */
33 struct elf_link_hash_entry *
34 _bfd_elf_define_linkage_sym (bfd *abfd,
35 struct bfd_link_info *info,
39 struct elf_link_hash_entry *h;
40 struct bfd_link_hash_entry *bh;
41 const struct elf_backend_data *bed;
43 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
46 /* Zap symbol defined in an as-needed lib that wasn't linked.
47 This is a symptom of a larger problem: Absolute symbols
48 defined in shared libraries can't be overridden, because we
49 lose the link to the bfd which is via the symbol section. */
50 h->root.type = bfd_link_hash_new;
54 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
56 get_elf_backend_data (abfd)->collect,
59 h = (struct elf_link_hash_entry *) bh;
62 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
64 bed = get_elf_backend_data (abfd);
65 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
70 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
74 struct elf_link_hash_entry *h;
75 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
78 /* This function may be called more than once. */
79 s = bfd_get_section_by_name (abfd, ".got");
80 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
83 switch (bed->s->arch_size)
94 bfd_set_error (bfd_error_bad_value);
98 flags = bed->dynamic_sec_flags;
100 s = bfd_make_section_with_flags (abfd, ".got", flags);
102 || !bfd_set_section_alignment (abfd, s, ptralign))
105 if (bed->want_got_plt)
107 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
109 || !bfd_set_section_alignment (abfd, s, ptralign))
113 if (bed->want_got_sym)
115 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
116 (or .got.plt) section. We don't do this in the linker script
117 because we don't want to define the symbol if we are not creating
118 a global offset table. */
119 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
120 elf_hash_table (info)->hgot = h;
125 /* The first bit of the global offset table is the header. */
126 s->size += bed->got_header_size;
131 /* Create a strtab to hold the dynamic symbol names. */
133 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
135 struct elf_link_hash_table *hash_table;
137 hash_table = elf_hash_table (info);
138 if (hash_table->dynobj == NULL)
139 hash_table->dynobj = abfd;
141 if (hash_table->dynstr == NULL)
143 hash_table->dynstr = _bfd_elf_strtab_init ();
144 if (hash_table->dynstr == NULL)
150 /* Create some sections which will be filled in with dynamic linking
151 information. ABFD is an input file which requires dynamic sections
152 to be created. The dynamic sections take up virtual memory space
153 when the final executable is run, so we need to create them before
154 addresses are assigned to the output sections. We work out the
155 actual contents and size of these sections later. */
158 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
161 register asection *s;
162 const struct elf_backend_data *bed;
164 if (! is_elf_hash_table (info->hash))
167 if (elf_hash_table (info)->dynamic_sections_created)
170 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
173 abfd = elf_hash_table (info)->dynobj;
174 bed = get_elf_backend_data (abfd);
176 flags = bed->dynamic_sec_flags;
178 /* A dynamically linked executable has a .interp section, but a
179 shared library does not. */
180 if (info->executable)
182 s = bfd_make_section_with_flags (abfd, ".interp",
183 flags | SEC_READONLY);
188 /* Create sections to hold version informations. These are removed
189 if they are not needed. */
190 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
191 flags | SEC_READONLY);
193 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
196 s = bfd_make_section_with_flags (abfd, ".gnu.version",
197 flags | SEC_READONLY);
199 || ! bfd_set_section_alignment (abfd, s, 1))
202 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
203 flags | SEC_READONLY);
205 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
208 s = bfd_make_section_with_flags (abfd, ".dynsym",
209 flags | SEC_READONLY);
211 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
214 s = bfd_make_section_with_flags (abfd, ".dynstr",
215 flags | SEC_READONLY);
219 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
221 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
224 /* The special symbol _DYNAMIC is always set to the start of the
225 .dynamic section. We could set _DYNAMIC in a linker script, but we
226 only want to define it if we are, in fact, creating a .dynamic
227 section. We don't want to define it if there is no .dynamic
228 section, since on some ELF platforms the start up code examines it
229 to decide how to initialize the process. */
230 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
235 s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
237 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
239 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
242 if (info->emit_gnu_hash)
244 s = bfd_make_section_with_flags (abfd, ".gnu.hash",
245 flags | SEC_READONLY);
247 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
249 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
250 4 32-bit words followed by variable count of 64-bit words, then
251 variable count of 32-bit words. */
252 if (bed->s->arch_size == 64)
253 elf_section_data (s)->this_hdr.sh_entsize = 0;
255 elf_section_data (s)->this_hdr.sh_entsize = 4;
258 /* Let the backend create the rest of the sections. This lets the
259 backend set the right flags. The backend will normally create
260 the .got and .plt sections. */
261 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
264 elf_hash_table (info)->dynamic_sections_created = TRUE;
269 /* Create dynamic sections when linking against a dynamic object. */
272 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
274 flagword flags, pltflags;
275 struct elf_link_hash_entry *h;
277 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
279 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
280 .rel[a].bss sections. */
281 flags = bed->dynamic_sec_flags;
284 if (bed->plt_not_loaded)
285 /* We do not clear SEC_ALLOC here because we still want the OS to
286 allocate space for the section; it's just that there's nothing
287 to read in from the object file. */
288 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
290 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
291 if (bed->plt_readonly)
292 pltflags |= SEC_READONLY;
294 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
296 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
299 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
301 if (bed->want_plt_sym)
303 h = _bfd_elf_define_linkage_sym (abfd, info, s,
304 "_PROCEDURE_LINKAGE_TABLE_");
305 elf_hash_table (info)->hplt = h;
310 s = bfd_make_section_with_flags (abfd,
311 (bed->default_use_rela_p
312 ? ".rela.plt" : ".rel.plt"),
313 flags | SEC_READONLY);
315 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
318 if (! _bfd_elf_create_got_section (abfd, info))
321 if (bed->want_dynbss)
323 /* The .dynbss section is a place to put symbols which are defined
324 by dynamic objects, are referenced by regular objects, and are
325 not functions. We must allocate space for them in the process
326 image and use a R_*_COPY reloc to tell the dynamic linker to
327 initialize them at run time. The linker script puts the .dynbss
328 section into the .bss section of the final image. */
329 s = bfd_make_section_with_flags (abfd, ".dynbss",
331 | SEC_LINKER_CREATED));
335 /* The .rel[a].bss section holds copy relocs. This section is not
336 normally needed. We need to create it here, though, so that the
337 linker will map it to an output section. We can't just create it
338 only if we need it, because we will not know whether we need it
339 until we have seen all the input files, and the first time the
340 main linker code calls BFD after examining all the input files
341 (size_dynamic_sections) the input sections have already been
342 mapped to the output sections. If the section turns out not to
343 be needed, we can discard it later. We will never need this
344 section when generating a shared object, since they do not use
348 s = bfd_make_section_with_flags (abfd,
349 (bed->default_use_rela_p
350 ? ".rela.bss" : ".rel.bss"),
351 flags | SEC_READONLY);
353 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
361 /* Record a new dynamic symbol. We record the dynamic symbols as we
362 read the input files, since we need to have a list of all of them
363 before we can determine the final sizes of the output sections.
364 Note that we may actually call this function even though we are not
365 going to output any dynamic symbols; in some cases we know that a
366 symbol should be in the dynamic symbol table, but only if there is
370 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
371 struct elf_link_hash_entry *h)
373 if (h->dynindx == -1)
375 struct elf_strtab_hash *dynstr;
380 /* XXX: The ABI draft says the linker must turn hidden and
381 internal symbols into STB_LOCAL symbols when producing the
382 DSO. However, if ld.so honors st_other in the dynamic table,
383 this would not be necessary. */
384 switch (ELF_ST_VISIBILITY (h->other))
388 if (h->root.type != bfd_link_hash_undefined
389 && h->root.type != bfd_link_hash_undefweak)
392 if (!elf_hash_table (info)->is_relocatable_executable)
400 h->dynindx = elf_hash_table (info)->dynsymcount;
401 ++elf_hash_table (info)->dynsymcount;
403 dynstr = elf_hash_table (info)->dynstr;
406 /* Create a strtab to hold the dynamic symbol names. */
407 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
412 /* We don't put any version information in the dynamic string
414 name = h->root.root.string;
415 p = strchr (name, ELF_VER_CHR);
417 /* We know that the p points into writable memory. In fact,
418 there are only a few symbols that have read-only names, being
419 those like _GLOBAL_OFFSET_TABLE_ that are created specially
420 by the backends. Most symbols will have names pointing into
421 an ELF string table read from a file, or to objalloc memory. */
424 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
429 if (indx == (bfd_size_type) -1)
431 h->dynstr_index = indx;
437 /* Mark a symbol dynamic. */
440 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
441 struct elf_link_hash_entry *h,
442 Elf_Internal_Sym *sym)
444 struct bfd_elf_dynamic_list *d = info->dynamic_list;
446 /* It may be called more than once on the same H. */
447 if(h->dynamic || info->relocatable)
450 if ((info->dynamic_data
451 && (h->type == STT_OBJECT
453 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
455 && h->root.type == bfd_link_hash_new
456 && (*d->match) (&d->head, NULL, h->root.root.string)))
460 /* Record an assignment to a symbol made by a linker script. We need
461 this in case some dynamic object refers to this symbol. */
464 bfd_elf_record_link_assignment (bfd *output_bfd,
465 struct bfd_link_info *info,
470 struct elf_link_hash_entry *h;
471 struct elf_link_hash_table *htab;
473 if (!is_elf_hash_table (info->hash))
476 htab = elf_hash_table (info);
477 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
481 /* Since we're defining the symbol, don't let it seem to have not
482 been defined. record_dynamic_symbol and size_dynamic_sections
483 may depend on this. */
484 if (h->root.type == bfd_link_hash_undefweak
485 || h->root.type == bfd_link_hash_undefined)
487 h->root.type = bfd_link_hash_new;
488 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
489 bfd_link_repair_undef_list (&htab->root);
491 else if (h->root.type == bfd_link_hash_new)
493 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
496 else if (h->root.type == bfd_link_hash_indirect)
498 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
499 struct elf_link_hash_entry *hv = h;
501 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
502 while (hv->root.type == bfd_link_hash_indirect
503 || hv->root.type == bfd_link_hash_warning);
504 h->root.type = bfd_link_hash_undefined;
505 hv->root.type = bfd_link_hash_indirect;
506 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
507 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
509 else if (h->root.type == bfd_link_hash_warning)
514 /* If this symbol is being provided by the linker script, and it is
515 currently defined by a dynamic object, but not by a regular
516 object, then mark it as undefined so that the generic linker will
517 force the correct value. */
521 h->root.type = bfd_link_hash_undefined;
523 /* If this symbol is not being provided by the linker script, and it is
524 currently defined by a dynamic object, but not by a regular object,
525 then clear out any version information because the symbol will not be
526 associated with the dynamic object any more. */
530 h->verinfo.verdef = NULL;
534 if (provide && hidden)
536 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
538 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
539 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
542 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
544 if (!info->relocatable
546 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
547 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
553 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
556 if (! bfd_elf_link_record_dynamic_symbol (info, h))
559 /* If this is a weak defined symbol, and we know a corresponding
560 real symbol from the same dynamic object, make sure the real
561 symbol is also made into a dynamic symbol. */
562 if (h->u.weakdef != NULL
563 && h->u.weakdef->dynindx == -1)
565 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
573 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
574 success, and 2 on a failure caused by attempting to record a symbol
575 in a discarded section, eg. a discarded link-once section symbol. */
578 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
583 struct elf_link_local_dynamic_entry *entry;
584 struct elf_link_hash_table *eht;
585 struct elf_strtab_hash *dynstr;
586 unsigned long dynstr_index;
588 Elf_External_Sym_Shndx eshndx;
589 char esym[sizeof (Elf64_External_Sym)];
591 if (! is_elf_hash_table (info->hash))
594 /* See if the entry exists already. */
595 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
596 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
599 amt = sizeof (*entry);
600 entry = bfd_alloc (input_bfd, amt);
604 /* Go find the symbol, so that we can find it's name. */
605 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
606 1, input_indx, &entry->isym, esym, &eshndx))
608 bfd_release (input_bfd, entry);
612 if (entry->isym.st_shndx != SHN_UNDEF
613 && (entry->isym.st_shndx < SHN_LORESERVE
614 || entry->isym.st_shndx > SHN_HIRESERVE))
618 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
619 if (s == NULL || bfd_is_abs_section (s->output_section))
621 /* We can still bfd_release here as nothing has done another
622 bfd_alloc. We can't do this later in this function. */
623 bfd_release (input_bfd, entry);
628 name = (bfd_elf_string_from_elf_section
629 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
630 entry->isym.st_name));
632 dynstr = elf_hash_table (info)->dynstr;
635 /* Create a strtab to hold the dynamic symbol names. */
636 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
641 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
642 if (dynstr_index == (unsigned long) -1)
644 entry->isym.st_name = dynstr_index;
646 eht = elf_hash_table (info);
648 entry->next = eht->dynlocal;
649 eht->dynlocal = entry;
650 entry->input_bfd = input_bfd;
651 entry->input_indx = input_indx;
654 /* Whatever binding the symbol had before, it's now local. */
656 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
658 /* The dynindx will be set at the end of size_dynamic_sections. */
663 /* Return the dynindex of a local dynamic symbol. */
666 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
670 struct elf_link_local_dynamic_entry *e;
672 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
673 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
678 /* This function is used to renumber the dynamic symbols, if some of
679 them are removed because they are marked as local. This is called
680 via elf_link_hash_traverse. */
683 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
686 size_t *count = data;
688 if (h->root.type == bfd_link_hash_warning)
689 h = (struct elf_link_hash_entry *) h->root.u.i.link;
694 if (h->dynindx != -1)
695 h->dynindx = ++(*count);
701 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
702 STB_LOCAL binding. */
705 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
708 size_t *count = data;
710 if (h->root.type == bfd_link_hash_warning)
711 h = (struct elf_link_hash_entry *) h->root.u.i.link;
713 if (!h->forced_local)
716 if (h->dynindx != -1)
717 h->dynindx = ++(*count);
722 /* Return true if the dynamic symbol for a given section should be
723 omitted when creating a shared library. */
725 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
726 struct bfd_link_info *info,
729 struct elf_link_hash_table *htab;
731 switch (elf_section_data (p)->this_hdr.sh_type)
735 /* If sh_type is yet undecided, assume it could be
736 SHT_PROGBITS/SHT_NOBITS. */
738 htab = elf_hash_table (info);
739 if (p == htab->tls_sec)
742 if (htab->text_index_section != NULL)
743 return p != htab->text_index_section && p != htab->data_index_section;
745 if (strcmp (p->name, ".got") == 0
746 || strcmp (p->name, ".got.plt") == 0
747 || strcmp (p->name, ".plt") == 0)
751 if (htab->dynobj != NULL
752 && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
753 && (ip->flags & SEC_LINKER_CREATED)
754 && ip->output_section == p)
759 /* There shouldn't be section relative relocations
760 against any other section. */
766 /* Assign dynsym indices. In a shared library we generate a section
767 symbol for each output section, which come first. Next come symbols
768 which have been forced to local binding. Then all of the back-end
769 allocated local dynamic syms, followed by the rest of the global
773 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
774 struct bfd_link_info *info,
775 unsigned long *section_sym_count)
777 unsigned long dynsymcount = 0;
779 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
781 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
783 for (p = output_bfd->sections; p ; p = p->next)
784 if ((p->flags & SEC_EXCLUDE) == 0
785 && (p->flags & SEC_ALLOC) != 0
786 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
787 elf_section_data (p)->dynindx = ++dynsymcount;
789 elf_section_data (p)->dynindx = 0;
791 *section_sym_count = dynsymcount;
793 elf_link_hash_traverse (elf_hash_table (info),
794 elf_link_renumber_local_hash_table_dynsyms,
797 if (elf_hash_table (info)->dynlocal)
799 struct elf_link_local_dynamic_entry *p;
800 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
801 p->dynindx = ++dynsymcount;
804 elf_link_hash_traverse (elf_hash_table (info),
805 elf_link_renumber_hash_table_dynsyms,
808 /* There is an unused NULL entry at the head of the table which
809 we must account for in our count. Unless there weren't any
810 symbols, which means we'll have no table at all. */
811 if (dynsymcount != 0)
814 elf_hash_table (info)->dynsymcount = dynsymcount;
818 /* This function is called when we want to define a new symbol. It
819 handles the various cases which arise when we find a definition in
820 a dynamic object, or when there is already a definition in a
821 dynamic object. The new symbol is described by NAME, SYM, PSEC,
822 and PVALUE. We set SYM_HASH to the hash table entry. We set
823 OVERRIDE if the old symbol is overriding a new definition. We set
824 TYPE_CHANGE_OK if it is OK for the type to change. We set
825 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
826 change, we mean that we shouldn't warn if the type or size does
827 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
828 object is overridden by a regular object. */
831 _bfd_elf_merge_symbol (bfd *abfd,
832 struct bfd_link_info *info,
834 Elf_Internal_Sym *sym,
837 unsigned int *pold_alignment,
838 struct elf_link_hash_entry **sym_hash,
840 bfd_boolean *override,
841 bfd_boolean *type_change_ok,
842 bfd_boolean *size_change_ok)
844 asection *sec, *oldsec;
845 struct elf_link_hash_entry *h;
846 struct elf_link_hash_entry *flip;
849 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
850 bfd_boolean newweak, oldweak;
851 const struct elf_backend_data *bed;
857 bind = ELF_ST_BIND (sym->st_info);
859 /* Silently discard TLS symbols from --just-syms. There's no way to
860 combine a static TLS block with a new TLS block for this executable. */
861 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
862 && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
868 if (! bfd_is_und_section (sec))
869 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
871 h = ((struct elf_link_hash_entry *)
872 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
877 bed = get_elf_backend_data (abfd);
879 /* This code is for coping with dynamic objects, and is only useful
880 if we are doing an ELF link. */
881 if (!(*bed->relocs_compatible) (abfd->xvec, info->hash->creator))
884 /* For merging, we only care about real symbols. */
886 while (h->root.type == bfd_link_hash_indirect
887 || h->root.type == bfd_link_hash_warning)
888 h = (struct elf_link_hash_entry *) h->root.u.i.link;
890 /* We have to check it for every instance since the first few may be
891 refereences and not all compilers emit symbol type for undefined
893 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
895 /* If we just created the symbol, mark it as being an ELF symbol.
896 Other than that, there is nothing to do--there is no merge issue
897 with a newly defined symbol--so we just return. */
899 if (h->root.type == bfd_link_hash_new)
905 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
908 switch (h->root.type)
915 case bfd_link_hash_undefined:
916 case bfd_link_hash_undefweak:
917 oldbfd = h->root.u.undef.abfd;
921 case bfd_link_hash_defined:
922 case bfd_link_hash_defweak:
923 oldbfd = h->root.u.def.section->owner;
924 oldsec = h->root.u.def.section;
927 case bfd_link_hash_common:
928 oldbfd = h->root.u.c.p->section->owner;
929 oldsec = h->root.u.c.p->section;
933 /* In cases involving weak versioned symbols, we may wind up trying
934 to merge a symbol with itself. Catch that here, to avoid the
935 confusion that results if we try to override a symbol with
936 itself. The additional tests catch cases like
937 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
938 dynamic object, which we do want to handle here. */
940 && ((abfd->flags & DYNAMIC) == 0
944 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
945 respectively, is from a dynamic object. */
947 newdyn = (abfd->flags & DYNAMIC) != 0;
951 olddyn = (oldbfd->flags & DYNAMIC) != 0;
952 else if (oldsec != NULL)
954 /* This handles the special SHN_MIPS_{TEXT,DATA} section
955 indices used by MIPS ELF. */
956 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
959 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
960 respectively, appear to be a definition rather than reference. */
962 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
964 olddef = (h->root.type != bfd_link_hash_undefined
965 && h->root.type != bfd_link_hash_undefweak
966 && h->root.type != bfd_link_hash_common);
968 /* When we try to create a default indirect symbol from the dynamic
969 definition with the default version, we skip it if its type and
970 the type of existing regular definition mismatch. We only do it
971 if the existing regular definition won't be dynamic. */
972 if (pold_alignment == NULL
974 && !info->export_dynamic
979 && (olddef || h->root.type == bfd_link_hash_common)
980 && ELF_ST_TYPE (sym->st_info) != h->type
981 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
982 && h->type != STT_NOTYPE
983 && !(bed->is_function_type (ELF_ST_TYPE (sym->st_info))
984 && bed->is_function_type (h->type)))
990 /* Check TLS symbol. We don't check undefined symbol introduced by
992 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
993 && ELF_ST_TYPE (sym->st_info) != h->type
997 bfd_boolean ntdef, tdef;
998 asection *ntsec, *tsec;
1000 if (h->type == STT_TLS)
1020 (*_bfd_error_handler)
1021 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1022 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1023 else if (!tdef && !ntdef)
1024 (*_bfd_error_handler)
1025 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1026 tbfd, ntbfd, h->root.root.string);
1028 (*_bfd_error_handler)
1029 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1030 tbfd, tsec, ntbfd, h->root.root.string);
1032 (*_bfd_error_handler)
1033 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1034 tbfd, ntbfd, ntsec, h->root.root.string);
1036 bfd_set_error (bfd_error_bad_value);
1040 /* We need to remember if a symbol has a definition in a dynamic
1041 object or is weak in all dynamic objects. Internal and hidden
1042 visibility will make it unavailable to dynamic objects. */
1043 if (newdyn && !h->dynamic_def)
1045 if (!bfd_is_und_section (sec))
1049 /* Check if this symbol is weak in all dynamic objects. If it
1050 is the first time we see it in a dynamic object, we mark
1051 if it is weak. Otherwise, we clear it. */
1052 if (!h->ref_dynamic)
1054 if (bind == STB_WEAK)
1055 h->dynamic_weak = 1;
1057 else if (bind != STB_WEAK)
1058 h->dynamic_weak = 0;
1062 /* If the old symbol has non-default visibility, we ignore the new
1063 definition from a dynamic object. */
1065 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1066 && !bfd_is_und_section (sec))
1069 /* Make sure this symbol is dynamic. */
1071 /* A protected symbol has external availability. Make sure it is
1072 recorded as dynamic.
1074 FIXME: Should we check type and size for protected symbol? */
1075 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1076 return bfd_elf_link_record_dynamic_symbol (info, h);
1081 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1084 /* If the new symbol with non-default visibility comes from a
1085 relocatable file and the old definition comes from a dynamic
1086 object, we remove the old definition. */
1087 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1089 /* Handle the case where the old dynamic definition is
1090 default versioned. We need to copy the symbol info from
1091 the symbol with default version to the normal one if it
1092 was referenced before. */
1095 const struct elf_backend_data *bed
1096 = get_elf_backend_data (abfd);
1097 struct elf_link_hash_entry *vh = *sym_hash;
1098 vh->root.type = h->root.type;
1099 h->root.type = bfd_link_hash_indirect;
1100 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1101 /* Protected symbols will override the dynamic definition
1102 with default version. */
1103 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1105 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1106 vh->dynamic_def = 1;
1107 vh->ref_dynamic = 1;
1111 h->root.type = vh->root.type;
1112 vh->ref_dynamic = 0;
1113 /* We have to hide it here since it was made dynamic
1114 global with extra bits when the symbol info was
1115 copied from the old dynamic definition. */
1116 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1124 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1125 && bfd_is_und_section (sec))
1127 /* If the new symbol is undefined and the old symbol was
1128 also undefined before, we need to make sure
1129 _bfd_generic_link_add_one_symbol doesn't mess
1130 up the linker hash table undefs list. Since the old
1131 definition came from a dynamic object, it is still on the
1133 h->root.type = bfd_link_hash_undefined;
1134 h->root.u.undef.abfd = abfd;
1138 h->root.type = bfd_link_hash_new;
1139 h->root.u.undef.abfd = NULL;
1148 /* FIXME: Should we check type and size for protected symbol? */
1154 /* Differentiate strong and weak symbols. */
1155 newweak = bind == STB_WEAK;
1156 oldweak = (h->root.type == bfd_link_hash_defweak
1157 || h->root.type == bfd_link_hash_undefweak);
1159 /* If a new weak symbol definition comes from a regular file and the
1160 old symbol comes from a dynamic library, we treat the new one as
1161 strong. Similarly, an old weak symbol definition from a regular
1162 file is treated as strong when the new symbol comes from a dynamic
1163 library. Further, an old weak symbol from a dynamic library is
1164 treated as strong if the new symbol is from a dynamic library.
1165 This reflects the way glibc's ld.so works.
1167 Do this before setting *type_change_ok or *size_change_ok so that
1168 we warn properly when dynamic library symbols are overridden. */
1170 if (newdef && !newdyn && olddyn)
1172 if (olddef && newdyn)
1175 /* Allow changes between different types of funciton symbol. */
1176 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))
1177 && bed->is_function_type (h->type))
1178 *type_change_ok = TRUE;
1180 /* It's OK to change the type if either the existing symbol or the
1181 new symbol is weak. A type change is also OK if the old symbol
1182 is undefined and the new symbol is defined. */
1187 && h->root.type == bfd_link_hash_undefined))
1188 *type_change_ok = TRUE;
1190 /* It's OK to change the size if either the existing symbol or the
1191 new symbol is weak, or if the old symbol is undefined. */
1194 || h->root.type == bfd_link_hash_undefined)
1195 *size_change_ok = TRUE;
1197 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1198 symbol, respectively, appears to be a common symbol in a dynamic
1199 object. If a symbol appears in an uninitialized section, and is
1200 not weak, and is not a function, then it may be a common symbol
1201 which was resolved when the dynamic object was created. We want
1202 to treat such symbols specially, because they raise special
1203 considerations when setting the symbol size: if the symbol
1204 appears as a common symbol in a regular object, and the size in
1205 the regular object is larger, we must make sure that we use the
1206 larger size. This problematic case can always be avoided in C,
1207 but it must be handled correctly when using Fortran shared
1210 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1211 likewise for OLDDYNCOMMON and OLDDEF.
1213 Note that this test is just a heuristic, and that it is quite
1214 possible to have an uninitialized symbol in a shared object which
1215 is really a definition, rather than a common symbol. This could
1216 lead to some minor confusion when the symbol really is a common
1217 symbol in some regular object. However, I think it will be
1223 && (sec->flags & SEC_ALLOC) != 0
1224 && (sec->flags & SEC_LOAD) == 0
1226 && !bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
1227 newdyncommon = TRUE;
1229 newdyncommon = FALSE;
1233 && h->root.type == bfd_link_hash_defined
1235 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1236 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1238 && !bed->is_function_type (h->type))
1239 olddyncommon = TRUE;
1241 olddyncommon = FALSE;
1243 /* We now know everything about the old and new symbols. We ask the
1244 backend to check if we can merge them. */
1245 if (bed->merge_symbol
1246 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1247 pold_alignment, skip, override,
1248 type_change_ok, size_change_ok,
1249 &newdyn, &newdef, &newdyncommon, &newweak,
1251 &olddyn, &olddef, &olddyncommon, &oldweak,
1255 /* If both the old and the new symbols look like common symbols in a
1256 dynamic object, set the size of the symbol to the larger of the
1261 && sym->st_size != h->size)
1263 /* Since we think we have two common symbols, issue a multiple
1264 common warning if desired. Note that we only warn if the
1265 size is different. If the size is the same, we simply let
1266 the old symbol override the new one as normally happens with
1267 symbols defined in dynamic objects. */
1269 if (! ((*info->callbacks->multiple_common)
1270 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1271 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1274 if (sym->st_size > h->size)
1275 h->size = sym->st_size;
1277 *size_change_ok = TRUE;
1280 /* If we are looking at a dynamic object, and we have found a
1281 definition, we need to see if the symbol was already defined by
1282 some other object. If so, we want to use the existing
1283 definition, and we do not want to report a multiple symbol
1284 definition error; we do this by clobbering *PSEC to be
1285 bfd_und_section_ptr.
1287 We treat a common symbol as a definition if the symbol in the
1288 shared library is a function, since common symbols always
1289 represent variables; this can cause confusion in principle, but
1290 any such confusion would seem to indicate an erroneous program or
1291 shared library. We also permit a common symbol in a regular
1292 object to override a weak symbol in a shared object. */
1297 || (h->root.type == bfd_link_hash_common
1299 || bed->is_function_type (ELF_ST_TYPE (sym->st_info))))))
1303 newdyncommon = FALSE;
1305 *psec = sec = bfd_und_section_ptr;
1306 *size_change_ok = TRUE;
1308 /* If we get here when the old symbol is a common symbol, then
1309 we are explicitly letting it override a weak symbol or
1310 function in a dynamic object, and we don't want to warn about
1311 a type change. If the old symbol is a defined symbol, a type
1312 change warning may still be appropriate. */
1314 if (h->root.type == bfd_link_hash_common)
1315 *type_change_ok = TRUE;
1318 /* Handle the special case of an old common symbol merging with a
1319 new symbol which looks like a common symbol in a shared object.
1320 We change *PSEC and *PVALUE to make the new symbol look like a
1321 common symbol, and let _bfd_generic_link_add_one_symbol do the
1325 && h->root.type == bfd_link_hash_common)
1329 newdyncommon = FALSE;
1330 *pvalue = sym->st_size;
1331 *psec = sec = bed->common_section (oldsec);
1332 *size_change_ok = TRUE;
1335 /* Skip weak definitions of symbols that are already defined. */
1336 if (newdef && olddef && newweak)
1339 /* If the old symbol is from a dynamic object, and the new symbol is
1340 a definition which is not from a dynamic object, then the new
1341 symbol overrides the old symbol. Symbols from regular files
1342 always take precedence over symbols from dynamic objects, even if
1343 they are defined after the dynamic object in the link.
1345 As above, we again permit a common symbol in a regular object to
1346 override a definition in a shared object if the shared object
1347 symbol is a function or is weak. */
1352 || (bfd_is_com_section (sec)
1354 || bed->is_function_type (h->type))))
1359 /* Change the hash table entry to undefined, and let
1360 _bfd_generic_link_add_one_symbol do the right thing with the
1363 h->root.type = bfd_link_hash_undefined;
1364 h->root.u.undef.abfd = h->root.u.def.section->owner;
1365 *size_change_ok = TRUE;
1368 olddyncommon = FALSE;
1370 /* We again permit a type change when a common symbol may be
1371 overriding a function. */
1373 if (bfd_is_com_section (sec))
1374 *type_change_ok = TRUE;
1376 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1379 /* This union may have been set to be non-NULL when this symbol
1380 was seen in a dynamic object. We must force the union to be
1381 NULL, so that it is correct for a regular symbol. */
1382 h->verinfo.vertree = NULL;
1385 /* Handle the special case of a new common symbol merging with an
1386 old symbol that looks like it might be a common symbol defined in
1387 a shared object. Note that we have already handled the case in
1388 which a new common symbol should simply override the definition
1389 in the shared library. */
1392 && bfd_is_com_section (sec)
1395 /* It would be best if we could set the hash table entry to a
1396 common symbol, but we don't know what to use for the section
1397 or the alignment. */
1398 if (! ((*info->callbacks->multiple_common)
1399 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1400 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1403 /* If the presumed common symbol in the dynamic object is
1404 larger, pretend that the new symbol has its size. */
1406 if (h->size > *pvalue)
1409 /* We need to remember the alignment required by the symbol
1410 in the dynamic object. */
1411 BFD_ASSERT (pold_alignment);
1412 *pold_alignment = h->root.u.def.section->alignment_power;
1415 olddyncommon = FALSE;
1417 h->root.type = bfd_link_hash_undefined;
1418 h->root.u.undef.abfd = h->root.u.def.section->owner;
1420 *size_change_ok = TRUE;
1421 *type_change_ok = TRUE;
1423 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1426 h->verinfo.vertree = NULL;
1431 /* Handle the case where we had a versioned symbol in a dynamic
1432 library and now find a definition in a normal object. In this
1433 case, we make the versioned symbol point to the normal one. */
1434 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1435 flip->root.type = h->root.type;
1436 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1437 h->root.type = bfd_link_hash_indirect;
1438 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1439 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1443 flip->ref_dynamic = 1;
1450 /* This function is called to create an indirect symbol from the
1451 default for the symbol with the default version if needed. The
1452 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1453 set DYNSYM if the new indirect symbol is dynamic. */
1456 _bfd_elf_add_default_symbol (bfd *abfd,
1457 struct bfd_link_info *info,
1458 struct elf_link_hash_entry *h,
1460 Elf_Internal_Sym *sym,
1463 bfd_boolean *dynsym,
1464 bfd_boolean override)
1466 bfd_boolean type_change_ok;
1467 bfd_boolean size_change_ok;
1470 struct elf_link_hash_entry *hi;
1471 struct bfd_link_hash_entry *bh;
1472 const struct elf_backend_data *bed;
1473 bfd_boolean collect;
1474 bfd_boolean dynamic;
1476 size_t len, shortlen;
1479 /* If this symbol has a version, and it is the default version, we
1480 create an indirect symbol from the default name to the fully
1481 decorated name. This will cause external references which do not
1482 specify a version to be bound to this version of the symbol. */
1483 p = strchr (name, ELF_VER_CHR);
1484 if (p == NULL || p[1] != ELF_VER_CHR)
1489 /* We are overridden by an old definition. We need to check if we
1490 need to create the indirect symbol from the default name. */
1491 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1493 BFD_ASSERT (hi != NULL);
1496 while (hi->root.type == bfd_link_hash_indirect
1497 || hi->root.type == bfd_link_hash_warning)
1499 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1505 bed = get_elf_backend_data (abfd);
1506 collect = bed->collect;
1507 dynamic = (abfd->flags & DYNAMIC) != 0;
1509 shortlen = p - name;
1510 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1511 if (shortname == NULL)
1513 memcpy (shortname, name, shortlen);
1514 shortname[shortlen] = '\0';
1516 /* We are going to create a new symbol. Merge it with any existing
1517 symbol with this name. For the purposes of the merge, act as
1518 though we were defining the symbol we just defined, although we
1519 actually going to define an indirect symbol. */
1520 type_change_ok = FALSE;
1521 size_change_ok = FALSE;
1523 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1524 NULL, &hi, &skip, &override,
1525 &type_change_ok, &size_change_ok))
1534 if (! (_bfd_generic_link_add_one_symbol
1535 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1536 0, name, FALSE, collect, &bh)))
1538 hi = (struct elf_link_hash_entry *) bh;
1542 /* In this case the symbol named SHORTNAME is overriding the
1543 indirect symbol we want to add. We were planning on making
1544 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1545 is the name without a version. NAME is the fully versioned
1546 name, and it is the default version.
1548 Overriding means that we already saw a definition for the
1549 symbol SHORTNAME in a regular object, and it is overriding
1550 the symbol defined in the dynamic object.
1552 When this happens, we actually want to change NAME, the
1553 symbol we just added, to refer to SHORTNAME. This will cause
1554 references to NAME in the shared object to become references
1555 to SHORTNAME in the regular object. This is what we expect
1556 when we override a function in a shared object: that the
1557 references in the shared object will be mapped to the
1558 definition in the regular object. */
1560 while (hi->root.type == bfd_link_hash_indirect
1561 || hi->root.type == bfd_link_hash_warning)
1562 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1564 h->root.type = bfd_link_hash_indirect;
1565 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1569 hi->ref_dynamic = 1;
1573 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1578 /* Now set HI to H, so that the following code will set the
1579 other fields correctly. */
1583 /* Check if HI is a warning symbol. */
1584 if (hi->root.type == bfd_link_hash_warning)
1585 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1587 /* If there is a duplicate definition somewhere, then HI may not
1588 point to an indirect symbol. We will have reported an error to
1589 the user in that case. */
1591 if (hi->root.type == bfd_link_hash_indirect)
1593 struct elf_link_hash_entry *ht;
1595 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1596 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1598 /* See if the new flags lead us to realize that the symbol must
1610 if (hi->ref_regular)
1616 /* We also need to define an indirection from the nondefault version
1620 len = strlen (name);
1621 shortname = bfd_hash_allocate (&info->hash->table, len);
1622 if (shortname == NULL)
1624 memcpy (shortname, name, shortlen);
1625 memcpy (shortname + shortlen, p + 1, len - shortlen);
1627 /* Once again, merge with any existing symbol. */
1628 type_change_ok = FALSE;
1629 size_change_ok = FALSE;
1631 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1632 NULL, &hi, &skip, &override,
1633 &type_change_ok, &size_change_ok))
1641 /* Here SHORTNAME is a versioned name, so we don't expect to see
1642 the type of override we do in the case above unless it is
1643 overridden by a versioned definition. */
1644 if (hi->root.type != bfd_link_hash_defined
1645 && hi->root.type != bfd_link_hash_defweak)
1646 (*_bfd_error_handler)
1647 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1653 if (! (_bfd_generic_link_add_one_symbol
1654 (info, abfd, shortname, BSF_INDIRECT,
1655 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1657 hi = (struct elf_link_hash_entry *) bh;
1659 /* If there is a duplicate definition somewhere, then HI may not
1660 point to an indirect symbol. We will have reported an error
1661 to the user in that case. */
1663 if (hi->root.type == bfd_link_hash_indirect)
1665 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1667 /* See if the new flags lead us to realize that the symbol
1679 if (hi->ref_regular)
1689 /* This routine is used to export all defined symbols into the dynamic
1690 symbol table. It is called via elf_link_hash_traverse. */
1693 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1695 struct elf_info_failed *eif = data;
1697 /* Ignore this if we won't export it. */
1698 if (!eif->info->export_dynamic && !h->dynamic)
1701 /* Ignore indirect symbols. These are added by the versioning code. */
1702 if (h->root.type == bfd_link_hash_indirect)
1705 if (h->root.type == bfd_link_hash_warning)
1706 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1708 if (h->dynindx == -1
1712 struct bfd_elf_version_tree *t;
1713 struct bfd_elf_version_expr *d;
1715 for (t = eif->verdefs; t != NULL; t = t->next)
1717 if (t->globals.list != NULL)
1719 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1724 if (t->locals.list != NULL)
1726 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1735 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1746 /* Look through the symbols which are defined in other shared
1747 libraries and referenced here. Update the list of version
1748 dependencies. This will be put into the .gnu.version_r section.
1749 This function is called via elf_link_hash_traverse. */
1752 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1755 struct elf_find_verdep_info *rinfo = data;
1756 Elf_Internal_Verneed *t;
1757 Elf_Internal_Vernaux *a;
1760 if (h->root.type == bfd_link_hash_warning)
1761 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1763 /* We only care about symbols defined in shared objects with version
1768 || h->verinfo.verdef == NULL)
1771 /* See if we already know about this version. */
1772 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1774 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1777 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1778 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1784 /* This is a new version. Add it to tree we are building. */
1789 t = bfd_zalloc (rinfo->output_bfd, amt);
1792 rinfo->failed = TRUE;
1796 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1797 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1798 elf_tdata (rinfo->output_bfd)->verref = t;
1802 a = bfd_zalloc (rinfo->output_bfd, amt);
1804 /* Note that we are copying a string pointer here, and testing it
1805 above. If bfd_elf_string_from_elf_section is ever changed to
1806 discard the string data when low in memory, this will have to be
1808 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1810 a->vna_flags = h->verinfo.verdef->vd_flags;
1811 a->vna_nextptr = t->vn_auxptr;
1813 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1816 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1823 /* Figure out appropriate versions for all the symbols. We may not
1824 have the version number script until we have read all of the input
1825 files, so until that point we don't know which symbols should be
1826 local. This function is called via elf_link_hash_traverse. */
1829 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1831 struct elf_assign_sym_version_info *sinfo;
1832 struct bfd_link_info *info;
1833 const struct elf_backend_data *bed;
1834 struct elf_info_failed eif;
1841 if (h->root.type == bfd_link_hash_warning)
1842 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1844 /* Fix the symbol flags. */
1847 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1850 sinfo->failed = TRUE;
1854 /* We only need version numbers for symbols defined in regular
1856 if (!h->def_regular)
1859 bed = get_elf_backend_data (sinfo->output_bfd);
1860 p = strchr (h->root.root.string, ELF_VER_CHR);
1861 if (p != NULL && h->verinfo.vertree == NULL)
1863 struct bfd_elf_version_tree *t;
1868 /* There are two consecutive ELF_VER_CHR characters if this is
1869 not a hidden symbol. */
1871 if (*p == ELF_VER_CHR)
1877 /* If there is no version string, we can just return out. */
1885 /* Look for the version. If we find it, it is no longer weak. */
1886 for (t = sinfo->verdefs; t != NULL; t = t->next)
1888 if (strcmp (t->name, p) == 0)
1892 struct bfd_elf_version_expr *d;
1894 len = p - h->root.root.string;
1895 alc = bfd_malloc (len);
1898 memcpy (alc, h->root.root.string, len - 1);
1899 alc[len - 1] = '\0';
1900 if (alc[len - 2] == ELF_VER_CHR)
1901 alc[len - 2] = '\0';
1903 h->verinfo.vertree = t;
1907 if (t->globals.list != NULL)
1908 d = (*t->match) (&t->globals, NULL, alc);
1910 /* See if there is anything to force this symbol to
1912 if (d == NULL && t->locals.list != NULL)
1914 d = (*t->match) (&t->locals, NULL, alc);
1917 && ! info->export_dynamic)
1918 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1926 /* If we are building an application, we need to create a
1927 version node for this version. */
1928 if (t == NULL && info->executable)
1930 struct bfd_elf_version_tree **pp;
1933 /* If we aren't going to export this symbol, we don't need
1934 to worry about it. */
1935 if (h->dynindx == -1)
1939 t = bfd_zalloc (sinfo->output_bfd, amt);
1942 sinfo->failed = TRUE;
1947 t->name_indx = (unsigned int) -1;
1951 /* Don't count anonymous version tag. */
1952 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1954 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1956 t->vernum = version_index;
1960 h->verinfo.vertree = t;
1964 /* We could not find the version for a symbol when
1965 generating a shared archive. Return an error. */
1966 (*_bfd_error_handler)
1967 (_("%B: version node not found for symbol %s"),
1968 sinfo->output_bfd, h->root.root.string);
1969 bfd_set_error (bfd_error_bad_value);
1970 sinfo->failed = TRUE;
1978 /* If we don't have a version for this symbol, see if we can find
1980 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1982 struct bfd_elf_version_tree *t;
1983 struct bfd_elf_version_tree *local_ver;
1984 struct bfd_elf_version_expr *d;
1986 /* See if can find what version this symbol is in. If the
1987 symbol is supposed to be local, then don't actually register
1990 for (t = sinfo->verdefs; t != NULL; t = t->next)
1992 if (t->globals.list != NULL)
1994 bfd_boolean matched;
1998 while ((d = (*t->match) (&t->globals, d,
1999 h->root.root.string)) != NULL)
2004 /* There is a version without definition. Make
2005 the symbol the default definition for this
2007 h->verinfo.vertree = t;
2015 /* There is no undefined version for this symbol. Hide the
2017 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2020 if (t->locals.list != NULL)
2023 while ((d = (*t->match) (&t->locals, d,
2024 h->root.root.string)) != NULL)
2027 /* If the match is "*", keep looking for a more
2028 explicit, perhaps even global, match.
2029 XXX: Shouldn't this be !d->wildcard instead? */
2030 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
2039 if (local_ver != NULL)
2041 h->verinfo.vertree = local_ver;
2042 if (h->dynindx != -1
2043 && ! info->export_dynamic)
2045 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2053 /* Read and swap the relocs from the section indicated by SHDR. This
2054 may be either a REL or a RELA section. The relocations are
2055 translated into RELA relocations and stored in INTERNAL_RELOCS,
2056 which should have already been allocated to contain enough space.
2057 The EXTERNAL_RELOCS are a buffer where the external form of the
2058 relocations should be stored.
2060 Returns FALSE if something goes wrong. */
2063 elf_link_read_relocs_from_section (bfd *abfd,
2065 Elf_Internal_Shdr *shdr,
2066 void *external_relocs,
2067 Elf_Internal_Rela *internal_relocs)
2069 const struct elf_backend_data *bed;
2070 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2071 const bfd_byte *erela;
2072 const bfd_byte *erelaend;
2073 Elf_Internal_Rela *irela;
2074 Elf_Internal_Shdr *symtab_hdr;
2077 /* Position ourselves at the start of the section. */
2078 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2081 /* Read the relocations. */
2082 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2085 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2086 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
2088 bed = get_elf_backend_data (abfd);
2090 /* Convert the external relocations to the internal format. */
2091 if (shdr->sh_entsize == bed->s->sizeof_rel)
2092 swap_in = bed->s->swap_reloc_in;
2093 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2094 swap_in = bed->s->swap_reloca_in;
2097 bfd_set_error (bfd_error_wrong_format);
2101 erela = external_relocs;
2102 erelaend = erela + shdr->sh_size;
2103 irela = internal_relocs;
2104 while (erela < erelaend)
2108 (*swap_in) (abfd, erela, irela);
2109 r_symndx = ELF32_R_SYM (irela->r_info);
2110 if (bed->s->arch_size == 64)
2112 if ((size_t) r_symndx >= nsyms)
2114 (*_bfd_error_handler)
2115 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2116 " for offset 0x%lx in section `%A'"),
2118 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2119 bfd_set_error (bfd_error_bad_value);
2122 irela += bed->s->int_rels_per_ext_rel;
2123 erela += shdr->sh_entsize;
2129 /* Read and swap the relocs for a section O. They may have been
2130 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2131 not NULL, they are used as buffers to read into. They are known to
2132 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2133 the return value is allocated using either malloc or bfd_alloc,
2134 according to the KEEP_MEMORY argument. If O has two relocation
2135 sections (both REL and RELA relocations), then the REL_HDR
2136 relocations will appear first in INTERNAL_RELOCS, followed by the
2137 REL_HDR2 relocations. */
2140 _bfd_elf_link_read_relocs (bfd *abfd,
2142 void *external_relocs,
2143 Elf_Internal_Rela *internal_relocs,
2144 bfd_boolean keep_memory)
2146 Elf_Internal_Shdr *rel_hdr;
2147 void *alloc1 = NULL;
2148 Elf_Internal_Rela *alloc2 = NULL;
2149 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2151 if (elf_section_data (o)->relocs != NULL)
2152 return elf_section_data (o)->relocs;
2154 if (o->reloc_count == 0)
2157 rel_hdr = &elf_section_data (o)->rel_hdr;
2159 if (internal_relocs == NULL)
2163 size = o->reloc_count;
2164 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2166 internal_relocs = bfd_alloc (abfd, size);
2168 internal_relocs = alloc2 = bfd_malloc (size);
2169 if (internal_relocs == NULL)
2173 if (external_relocs == NULL)
2175 bfd_size_type size = rel_hdr->sh_size;
2177 if (elf_section_data (o)->rel_hdr2)
2178 size += elf_section_data (o)->rel_hdr2->sh_size;
2179 alloc1 = bfd_malloc (size);
2182 external_relocs = alloc1;
2185 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2189 if (elf_section_data (o)->rel_hdr2
2190 && (!elf_link_read_relocs_from_section
2192 elf_section_data (o)->rel_hdr2,
2193 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2194 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2195 * bed->s->int_rels_per_ext_rel))))
2198 /* Cache the results for next time, if we can. */
2200 elf_section_data (o)->relocs = internal_relocs;
2205 /* Don't free alloc2, since if it was allocated we are passing it
2206 back (under the name of internal_relocs). */
2208 return internal_relocs;
2218 /* Compute the size of, and allocate space for, REL_HDR which is the
2219 section header for a section containing relocations for O. */
2222 _bfd_elf_link_size_reloc_section (bfd *abfd,
2223 Elf_Internal_Shdr *rel_hdr,
2226 bfd_size_type reloc_count;
2227 bfd_size_type num_rel_hashes;
2229 /* Figure out how many relocations there will be. */
2230 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2231 reloc_count = elf_section_data (o)->rel_count;
2233 reloc_count = elf_section_data (o)->rel_count2;
2235 num_rel_hashes = o->reloc_count;
2236 if (num_rel_hashes < reloc_count)
2237 num_rel_hashes = reloc_count;
2239 /* That allows us to calculate the size of the section. */
2240 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2242 /* The contents field must last into write_object_contents, so we
2243 allocate it with bfd_alloc rather than malloc. Also since we
2244 cannot be sure that the contents will actually be filled in,
2245 we zero the allocated space. */
2246 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2247 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2250 /* We only allocate one set of hash entries, so we only do it the
2251 first time we are called. */
2252 if (elf_section_data (o)->rel_hashes == NULL
2255 struct elf_link_hash_entry **p;
2257 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2261 elf_section_data (o)->rel_hashes = p;
2267 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2268 originated from the section given by INPUT_REL_HDR) to the
2272 _bfd_elf_link_output_relocs (bfd *output_bfd,
2273 asection *input_section,
2274 Elf_Internal_Shdr *input_rel_hdr,
2275 Elf_Internal_Rela *internal_relocs,
2276 struct elf_link_hash_entry **rel_hash
2279 Elf_Internal_Rela *irela;
2280 Elf_Internal_Rela *irelaend;
2282 Elf_Internal_Shdr *output_rel_hdr;
2283 asection *output_section;
2284 unsigned int *rel_countp = NULL;
2285 const struct elf_backend_data *bed;
2286 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2288 output_section = input_section->output_section;
2289 output_rel_hdr = NULL;
2291 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2292 == input_rel_hdr->sh_entsize)
2294 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2295 rel_countp = &elf_section_data (output_section)->rel_count;
2297 else if (elf_section_data (output_section)->rel_hdr2
2298 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2299 == input_rel_hdr->sh_entsize))
2301 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2302 rel_countp = &elf_section_data (output_section)->rel_count2;
2306 (*_bfd_error_handler)
2307 (_("%B: relocation size mismatch in %B section %A"),
2308 output_bfd, input_section->owner, input_section);
2309 bfd_set_error (bfd_error_wrong_object_format);
2313 bed = get_elf_backend_data (output_bfd);
2314 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2315 swap_out = bed->s->swap_reloc_out;
2316 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2317 swap_out = bed->s->swap_reloca_out;
2321 erel = output_rel_hdr->contents;
2322 erel += *rel_countp * input_rel_hdr->sh_entsize;
2323 irela = internal_relocs;
2324 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2325 * bed->s->int_rels_per_ext_rel);
2326 while (irela < irelaend)
2328 (*swap_out) (output_bfd, irela, erel);
2329 irela += bed->s->int_rels_per_ext_rel;
2330 erel += input_rel_hdr->sh_entsize;
2333 /* Bump the counter, so that we know where to add the next set of
2335 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2340 /* Make weak undefined symbols in PIE dynamic. */
2343 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2344 struct elf_link_hash_entry *h)
2348 && h->root.type == bfd_link_hash_undefweak)
2349 return bfd_elf_link_record_dynamic_symbol (info, h);
2354 /* Fix up the flags for a symbol. This handles various cases which
2355 can only be fixed after all the input files are seen. This is
2356 currently called by both adjust_dynamic_symbol and
2357 assign_sym_version, which is unnecessary but perhaps more robust in
2358 the face of future changes. */
2361 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2362 struct elf_info_failed *eif)
2364 const struct elf_backend_data *bed = NULL;
2366 /* If this symbol was mentioned in a non-ELF file, try to set
2367 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2368 permit a non-ELF file to correctly refer to a symbol defined in
2369 an ELF dynamic object. */
2372 while (h->root.type == bfd_link_hash_indirect)
2373 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2375 if (h->root.type != bfd_link_hash_defined
2376 && h->root.type != bfd_link_hash_defweak)
2379 h->ref_regular_nonweak = 1;
2383 if (h->root.u.def.section->owner != NULL
2384 && (bfd_get_flavour (h->root.u.def.section->owner)
2385 == bfd_target_elf_flavour))
2388 h->ref_regular_nonweak = 1;
2394 if (h->dynindx == -1
2398 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2407 /* Unfortunately, NON_ELF is only correct if the symbol
2408 was first seen in a non-ELF file. Fortunately, if the symbol
2409 was first seen in an ELF file, we're probably OK unless the
2410 symbol was defined in a non-ELF file. Catch that case here.
2411 FIXME: We're still in trouble if the symbol was first seen in
2412 a dynamic object, and then later in a non-ELF regular object. */
2413 if ((h->root.type == bfd_link_hash_defined
2414 || h->root.type == bfd_link_hash_defweak)
2416 && (h->root.u.def.section->owner != NULL
2417 ? (bfd_get_flavour (h->root.u.def.section->owner)
2418 != bfd_target_elf_flavour)
2419 : (bfd_is_abs_section (h->root.u.def.section)
2420 && !h->def_dynamic)))
2424 /* Backend specific symbol fixup. */
2425 if (elf_hash_table (eif->info)->dynobj)
2427 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2428 if (bed->elf_backend_fixup_symbol
2429 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2433 /* If this is a final link, and the symbol was defined as a common
2434 symbol in a regular object file, and there was no definition in
2435 any dynamic object, then the linker will have allocated space for
2436 the symbol in a common section but the DEF_REGULAR
2437 flag will not have been set. */
2438 if (h->root.type == bfd_link_hash_defined
2442 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2445 /* If -Bsymbolic was used (which means to bind references to global
2446 symbols to the definition within the shared object), and this
2447 symbol was defined in a regular object, then it actually doesn't
2448 need a PLT entry. Likewise, if the symbol has non-default
2449 visibility. If the symbol has hidden or internal visibility, we
2450 will force it local. */
2452 && eif->info->shared
2453 && is_elf_hash_table (eif->info->hash)
2454 && (SYMBOLIC_BIND (eif->info, h)
2455 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2458 bfd_boolean force_local;
2460 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2461 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2462 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2465 /* If a weak undefined symbol has non-default visibility, we also
2466 hide it from the dynamic linker. */
2467 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2468 && h->root.type == bfd_link_hash_undefweak)
2470 const struct elf_backend_data *bed;
2471 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2472 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2475 /* If this is a weak defined symbol in a dynamic object, and we know
2476 the real definition in the dynamic object, copy interesting flags
2477 over to the real definition. */
2478 if (h->u.weakdef != NULL)
2480 struct elf_link_hash_entry *weakdef;
2482 weakdef = h->u.weakdef;
2483 if (h->root.type == bfd_link_hash_indirect)
2484 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2486 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2487 || h->root.type == bfd_link_hash_defweak);
2488 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2489 || weakdef->root.type == bfd_link_hash_defweak);
2490 BFD_ASSERT (weakdef->def_dynamic);
2492 /* If the real definition is defined by a regular object file,
2493 don't do anything special. See the longer description in
2494 _bfd_elf_adjust_dynamic_symbol, below. */
2495 if (weakdef->def_regular)
2496 h->u.weakdef = NULL;
2498 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef,
2505 /* Make the backend pick a good value for a dynamic symbol. This is
2506 called via elf_link_hash_traverse, and also calls itself
2510 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2512 struct elf_info_failed *eif = data;
2514 const struct elf_backend_data *bed;
2516 if (! is_elf_hash_table (eif->info->hash))
2519 if (h->root.type == bfd_link_hash_warning)
2521 h->got = elf_hash_table (eif->info)->init_got_offset;
2522 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2524 /* When warning symbols are created, they **replace** the "real"
2525 entry in the hash table, thus we never get to see the real
2526 symbol in a hash traversal. So look at it now. */
2527 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2530 /* Ignore indirect symbols. These are added by the versioning code. */
2531 if (h->root.type == bfd_link_hash_indirect)
2534 /* Fix the symbol flags. */
2535 if (! _bfd_elf_fix_symbol_flags (h, eif))
2538 /* If this symbol does not require a PLT entry, and it is not
2539 defined by a dynamic object, or is not referenced by a regular
2540 object, ignore it. We do have to handle a weak defined symbol,
2541 even if no regular object refers to it, if we decided to add it
2542 to the dynamic symbol table. FIXME: Do we normally need to worry
2543 about symbols which are defined by one dynamic object and
2544 referenced by another one? */
2549 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2551 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2555 /* If we've already adjusted this symbol, don't do it again. This
2556 can happen via a recursive call. */
2557 if (h->dynamic_adjusted)
2560 /* Don't look at this symbol again. Note that we must set this
2561 after checking the above conditions, because we may look at a
2562 symbol once, decide not to do anything, and then get called
2563 recursively later after REF_REGULAR is set below. */
2564 h->dynamic_adjusted = 1;
2566 /* If this is a weak definition, and we know a real definition, and
2567 the real symbol is not itself defined by a regular object file,
2568 then get a good value for the real definition. We handle the
2569 real symbol first, for the convenience of the backend routine.
2571 Note that there is a confusing case here. If the real definition
2572 is defined by a regular object file, we don't get the real symbol
2573 from the dynamic object, but we do get the weak symbol. If the
2574 processor backend uses a COPY reloc, then if some routine in the
2575 dynamic object changes the real symbol, we will not see that
2576 change in the corresponding weak symbol. This is the way other
2577 ELF linkers work as well, and seems to be a result of the shared
2580 I will clarify this issue. Most SVR4 shared libraries define the
2581 variable _timezone and define timezone as a weak synonym. The
2582 tzset call changes _timezone. If you write
2583 extern int timezone;
2585 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2586 you might expect that, since timezone is a synonym for _timezone,
2587 the same number will print both times. However, if the processor
2588 backend uses a COPY reloc, then actually timezone will be copied
2589 into your process image, and, since you define _timezone
2590 yourself, _timezone will not. Thus timezone and _timezone will
2591 wind up at different memory locations. The tzset call will set
2592 _timezone, leaving timezone unchanged. */
2594 if (h->u.weakdef != NULL)
2596 /* If we get to this point, we know there is an implicit
2597 reference by a regular object file via the weak symbol H.
2598 FIXME: Is this really true? What if the traversal finds
2599 H->U.WEAKDEF before it finds H? */
2600 h->u.weakdef->ref_regular = 1;
2602 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2606 /* If a symbol has no type and no size and does not require a PLT
2607 entry, then we are probably about to do the wrong thing here: we
2608 are probably going to create a COPY reloc for an empty object.
2609 This case can arise when a shared object is built with assembly
2610 code, and the assembly code fails to set the symbol type. */
2612 && h->type == STT_NOTYPE
2614 (*_bfd_error_handler)
2615 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2616 h->root.root.string);
2618 dynobj = elf_hash_table (eif->info)->dynobj;
2619 bed = get_elf_backend_data (dynobj);
2620 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2629 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2633 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2636 unsigned int power_of_two;
2638 asection *sec = h->root.u.def.section;
2640 /* The section aligment of definition is the maximum alignment
2641 requirement of symbols defined in the section. Since we don't
2642 know the symbol alignment requirement, we start with the
2643 maximum alignment and check low bits of the symbol address
2644 for the minimum alignment. */
2645 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2646 mask = ((bfd_vma) 1 << power_of_two) - 1;
2647 while ((h->root.u.def.value & mask) != 0)
2653 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2656 /* Adjust the section alignment if needed. */
2657 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2662 /* We make sure that the symbol will be aligned properly. */
2663 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2665 /* Define the symbol as being at this point in DYNBSS. */
2666 h->root.u.def.section = dynbss;
2667 h->root.u.def.value = dynbss->size;
2669 /* Increment the size of DYNBSS to make room for the symbol. */
2670 dynbss->size += h->size;
2675 /* Adjust all external symbols pointing into SEC_MERGE sections
2676 to reflect the object merging within the sections. */
2679 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2683 if (h->root.type == bfd_link_hash_warning)
2684 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2686 if ((h->root.type == bfd_link_hash_defined
2687 || h->root.type == bfd_link_hash_defweak)
2688 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2689 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2691 bfd *output_bfd = data;
2693 h->root.u.def.value =
2694 _bfd_merged_section_offset (output_bfd,
2695 &h->root.u.def.section,
2696 elf_section_data (sec)->sec_info,
2697 h->root.u.def.value);
2703 /* Returns false if the symbol referred to by H should be considered
2704 to resolve local to the current module, and true if it should be
2705 considered to bind dynamically. */
2708 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2709 struct bfd_link_info *info,
2710 bfd_boolean ignore_protected)
2712 bfd_boolean binding_stays_local_p;
2713 const struct elf_backend_data *bed;
2714 struct elf_link_hash_table *hash_table;
2719 while (h->root.type == bfd_link_hash_indirect
2720 || h->root.type == bfd_link_hash_warning)
2721 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2723 /* If it was forced local, then clearly it's not dynamic. */
2724 if (h->dynindx == -1)
2726 if (h->forced_local)
2729 /* Identify the cases where name binding rules say that a
2730 visible symbol resolves locally. */
2731 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2733 switch (ELF_ST_VISIBILITY (h->other))
2740 hash_table = elf_hash_table (info);
2741 if (!is_elf_hash_table (hash_table))
2744 bed = get_elf_backend_data (hash_table->dynobj);
2746 /* Proper resolution for function pointer equality may require
2747 that these symbols perhaps be resolved dynamically, even though
2748 we should be resolving them to the current module. */
2749 if (!ignore_protected || !bed->is_function_type (h->type))
2750 binding_stays_local_p = TRUE;
2757 /* If it isn't defined locally, then clearly it's dynamic. */
2758 if (!h->def_regular)
2761 /* Otherwise, the symbol is dynamic if binding rules don't tell
2762 us that it remains local. */
2763 return !binding_stays_local_p;
2766 /* Return true if the symbol referred to by H should be considered
2767 to resolve local to the current module, and false otherwise. Differs
2768 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2769 undefined symbols and weak symbols. */
2772 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2773 struct bfd_link_info *info,
2774 bfd_boolean local_protected)
2776 const struct elf_backend_data *bed;
2777 struct elf_link_hash_table *hash_table;
2779 /* If it's a local sym, of course we resolve locally. */
2783 /* Common symbols that become definitions don't get the DEF_REGULAR
2784 flag set, so test it first, and don't bail out. */
2785 if (ELF_COMMON_DEF_P (h))
2787 /* If we don't have a definition in a regular file, then we can't
2788 resolve locally. The sym is either undefined or dynamic. */
2789 else if (!h->def_regular)
2792 /* Forced local symbols resolve locally. */
2793 if (h->forced_local)
2796 /* As do non-dynamic symbols. */
2797 if (h->dynindx == -1)
2800 /* At this point, we know the symbol is defined and dynamic. In an
2801 executable it must resolve locally, likewise when building symbolic
2802 shared libraries. */
2803 if (info->executable || SYMBOLIC_BIND (info, h))
2806 /* Now deal with defined dynamic symbols in shared libraries. Ones
2807 with default visibility might not resolve locally. */
2808 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2811 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2812 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2815 hash_table = elf_hash_table (info);
2816 if (!is_elf_hash_table (hash_table))
2819 bed = get_elf_backend_data (hash_table->dynobj);
2821 /* STV_PROTECTED non-function symbols are local. */
2822 if (!bed->is_function_type (h->type))
2825 /* Function pointer equality tests may require that STV_PROTECTED
2826 symbols be treated as dynamic symbols, even when we know that the
2827 dynamic linker will resolve them locally. */
2828 return local_protected;
2831 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2832 aligned. Returns the first TLS output section. */
2834 struct bfd_section *
2835 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2837 struct bfd_section *sec, *tls;
2838 unsigned int align = 0;
2840 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2841 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2845 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2846 if (sec->alignment_power > align)
2847 align = sec->alignment_power;
2849 elf_hash_table (info)->tls_sec = tls;
2851 /* Ensure the alignment of the first section is the largest alignment,
2852 so that the tls segment starts aligned. */
2854 tls->alignment_power = align;
2859 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2861 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2862 Elf_Internal_Sym *sym)
2864 const struct elf_backend_data *bed;
2866 /* Local symbols do not count, but target specific ones might. */
2867 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2868 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2871 bed = get_elf_backend_data (abfd);
2872 /* Function symbols do not count. */
2873 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2876 /* If the section is undefined, then so is the symbol. */
2877 if (sym->st_shndx == SHN_UNDEF)
2880 /* If the symbol is defined in the common section, then
2881 it is a common definition and so does not count. */
2882 if (bed->common_definition (sym))
2885 /* If the symbol is in a target specific section then we
2886 must rely upon the backend to tell us what it is. */
2887 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2888 /* FIXME - this function is not coded yet:
2890 return _bfd_is_global_symbol_definition (abfd, sym);
2892 Instead for now assume that the definition is not global,
2893 Even if this is wrong, at least the linker will behave
2894 in the same way that it used to do. */
2900 /* Search the symbol table of the archive element of the archive ABFD
2901 whose archive map contains a mention of SYMDEF, and determine if
2902 the symbol is defined in this element. */
2904 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2906 Elf_Internal_Shdr * hdr;
2907 bfd_size_type symcount;
2908 bfd_size_type extsymcount;
2909 bfd_size_type extsymoff;
2910 Elf_Internal_Sym *isymbuf;
2911 Elf_Internal_Sym *isym;
2912 Elf_Internal_Sym *isymend;
2915 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2919 if (! bfd_check_format (abfd, bfd_object))
2922 /* If we have already included the element containing this symbol in the
2923 link then we do not need to include it again. Just claim that any symbol
2924 it contains is not a definition, so that our caller will not decide to
2925 (re)include this element. */
2926 if (abfd->archive_pass)
2929 /* Select the appropriate symbol table. */
2930 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2931 hdr = &elf_tdata (abfd)->symtab_hdr;
2933 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2935 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2937 /* The sh_info field of the symtab header tells us where the
2938 external symbols start. We don't care about the local symbols. */
2939 if (elf_bad_symtab (abfd))
2941 extsymcount = symcount;
2946 extsymcount = symcount - hdr->sh_info;
2947 extsymoff = hdr->sh_info;
2950 if (extsymcount == 0)
2953 /* Read in the symbol table. */
2954 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2956 if (isymbuf == NULL)
2959 /* Scan the symbol table looking for SYMDEF. */
2961 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2965 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2970 if (strcmp (name, symdef->name) == 0)
2972 result = is_global_data_symbol_definition (abfd, isym);
2982 /* Add an entry to the .dynamic table. */
2985 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2989 struct elf_link_hash_table *hash_table;
2990 const struct elf_backend_data *bed;
2992 bfd_size_type newsize;
2993 bfd_byte *newcontents;
2994 Elf_Internal_Dyn dyn;
2996 hash_table = elf_hash_table (info);
2997 if (! is_elf_hash_table (hash_table))
3000 bed = get_elf_backend_data (hash_table->dynobj);
3001 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3002 BFD_ASSERT (s != NULL);
3004 newsize = s->size + bed->s->sizeof_dyn;
3005 newcontents = bfd_realloc (s->contents, newsize);
3006 if (newcontents == NULL)
3010 dyn.d_un.d_val = val;
3011 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3014 s->contents = newcontents;
3019 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3020 otherwise just check whether one already exists. Returns -1 on error,
3021 1 if a DT_NEEDED tag already exists, and 0 on success. */
3024 elf_add_dt_needed_tag (bfd *abfd,
3025 struct bfd_link_info *info,
3029 struct elf_link_hash_table *hash_table;
3030 bfd_size_type oldsize;
3031 bfd_size_type strindex;
3033 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3036 hash_table = elf_hash_table (info);
3037 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3038 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3039 if (strindex == (bfd_size_type) -1)
3042 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3045 const struct elf_backend_data *bed;
3048 bed = get_elf_backend_data (hash_table->dynobj);
3049 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3051 for (extdyn = sdyn->contents;
3052 extdyn < sdyn->contents + sdyn->size;
3053 extdyn += bed->s->sizeof_dyn)
3055 Elf_Internal_Dyn dyn;
3057 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3058 if (dyn.d_tag == DT_NEEDED
3059 && dyn.d_un.d_val == strindex)
3061 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3069 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3072 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3076 /* We were just checking for existence of the tag. */
3077 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3082 /* Sort symbol by value and section. */
3084 elf_sort_symbol (const void *arg1, const void *arg2)
3086 const struct elf_link_hash_entry *h1;
3087 const struct elf_link_hash_entry *h2;
3088 bfd_signed_vma vdiff;
3090 h1 = *(const struct elf_link_hash_entry **) arg1;
3091 h2 = *(const struct elf_link_hash_entry **) arg2;
3092 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3094 return vdiff > 0 ? 1 : -1;
3097 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3099 return sdiff > 0 ? 1 : -1;
3104 /* This function is used to adjust offsets into .dynstr for
3105 dynamic symbols. This is called via elf_link_hash_traverse. */
3108 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3110 struct elf_strtab_hash *dynstr = data;
3112 if (h->root.type == bfd_link_hash_warning)
3113 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3115 if (h->dynindx != -1)
3116 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3120 /* Assign string offsets in .dynstr, update all structures referencing
3124 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3126 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3127 struct elf_link_local_dynamic_entry *entry;
3128 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3129 bfd *dynobj = hash_table->dynobj;
3132 const struct elf_backend_data *bed;
3135 _bfd_elf_strtab_finalize (dynstr);
3136 size = _bfd_elf_strtab_size (dynstr);
3138 bed = get_elf_backend_data (dynobj);
3139 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3140 BFD_ASSERT (sdyn != NULL);
3142 /* Update all .dynamic entries referencing .dynstr strings. */
3143 for (extdyn = sdyn->contents;
3144 extdyn < sdyn->contents + sdyn->size;
3145 extdyn += bed->s->sizeof_dyn)
3147 Elf_Internal_Dyn dyn;
3149 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3153 dyn.d_un.d_val = size;
3161 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3166 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3169 /* Now update local dynamic symbols. */
3170 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3171 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3172 entry->isym.st_name);
3174 /* And the rest of dynamic symbols. */
3175 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3177 /* Adjust version definitions. */
3178 if (elf_tdata (output_bfd)->cverdefs)
3183 Elf_Internal_Verdef def;
3184 Elf_Internal_Verdaux defaux;
3186 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3190 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3192 p += sizeof (Elf_External_Verdef);
3193 if (def.vd_aux != sizeof (Elf_External_Verdef))
3195 for (i = 0; i < def.vd_cnt; ++i)
3197 _bfd_elf_swap_verdaux_in (output_bfd,
3198 (Elf_External_Verdaux *) p, &defaux);
3199 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3201 _bfd_elf_swap_verdaux_out (output_bfd,
3202 &defaux, (Elf_External_Verdaux *) p);
3203 p += sizeof (Elf_External_Verdaux);
3206 while (def.vd_next);
3209 /* Adjust version references. */
3210 if (elf_tdata (output_bfd)->verref)
3215 Elf_Internal_Verneed need;
3216 Elf_Internal_Vernaux needaux;
3218 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3222 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3224 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3225 _bfd_elf_swap_verneed_out (output_bfd, &need,
3226 (Elf_External_Verneed *) p);
3227 p += sizeof (Elf_External_Verneed);
3228 for (i = 0; i < need.vn_cnt; ++i)
3230 _bfd_elf_swap_vernaux_in (output_bfd,
3231 (Elf_External_Vernaux *) p, &needaux);
3232 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3234 _bfd_elf_swap_vernaux_out (output_bfd,
3236 (Elf_External_Vernaux *) p);
3237 p += sizeof (Elf_External_Vernaux);
3240 while (need.vn_next);
3246 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3247 The default is to only match when the INPUT and OUTPUT are exactly
3251 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3252 const bfd_target *output)
3254 return input == output;
3257 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3258 This version is used when different targets for the same architecture
3259 are virtually identical. */
3262 _bfd_elf_relocs_compatible (const bfd_target *input,
3263 const bfd_target *output)
3265 const struct elf_backend_data *obed, *ibed;
3267 if (input == output)
3270 ibed = xvec_get_elf_backend_data (input);
3271 obed = xvec_get_elf_backend_data (output);
3273 if (ibed->arch != obed->arch)
3276 /* If both backends are using this function, deem them compatible. */
3277 return ibed->relocs_compatible == obed->relocs_compatible;
3280 /* Add symbols from an ELF object file to the linker hash table. */
3283 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3285 Elf_Internal_Shdr *hdr;
3286 bfd_size_type symcount;
3287 bfd_size_type extsymcount;
3288 bfd_size_type extsymoff;
3289 struct elf_link_hash_entry **sym_hash;
3290 bfd_boolean dynamic;
3291 Elf_External_Versym *extversym = NULL;
3292 Elf_External_Versym *ever;
3293 struct elf_link_hash_entry *weaks;
3294 struct elf_link_hash_entry **nondeflt_vers = NULL;
3295 bfd_size_type nondeflt_vers_cnt = 0;
3296 Elf_Internal_Sym *isymbuf = NULL;
3297 Elf_Internal_Sym *isym;
3298 Elf_Internal_Sym *isymend;
3299 const struct elf_backend_data *bed;
3300 bfd_boolean add_needed;
3301 struct elf_link_hash_table *htab;
3303 void *alloc_mark = NULL;
3304 struct bfd_hash_entry **old_table = NULL;
3305 unsigned int old_size = 0;
3306 unsigned int old_count = 0;
3307 void *old_tab = NULL;
3310 struct bfd_link_hash_entry *old_undefs = NULL;
3311 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3312 long old_dynsymcount = 0;
3314 size_t hashsize = 0;
3316 htab = elf_hash_table (info);
3317 bed = get_elf_backend_data (abfd);
3319 if ((abfd->flags & DYNAMIC) == 0)
3325 /* You can't use -r against a dynamic object. Also, there's no
3326 hope of using a dynamic object which does not exactly match
3327 the format of the output file. */
3328 if (info->relocatable
3329 || !is_elf_hash_table (htab)
3330 || htab->root.creator != abfd->xvec)
3332 if (info->relocatable)
3333 bfd_set_error (bfd_error_invalid_operation);
3335 bfd_set_error (bfd_error_wrong_format);
3340 /* As a GNU extension, any input sections which are named
3341 .gnu.warning.SYMBOL are treated as warning symbols for the given
3342 symbol. This differs from .gnu.warning sections, which generate
3343 warnings when they are included in an output file. */
3344 if (info->executable)
3348 for (s = abfd->sections; s != NULL; s = s->next)
3352 name = bfd_get_section_name (abfd, s);
3353 if (CONST_STRNEQ (name, ".gnu.warning."))
3358 name += sizeof ".gnu.warning." - 1;
3360 /* If this is a shared object, then look up the symbol
3361 in the hash table. If it is there, and it is already
3362 been defined, then we will not be using the entry
3363 from this shared object, so we don't need to warn.
3364 FIXME: If we see the definition in a regular object
3365 later on, we will warn, but we shouldn't. The only
3366 fix is to keep track of what warnings we are supposed
3367 to emit, and then handle them all at the end of the
3371 struct elf_link_hash_entry *h;
3373 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3375 /* FIXME: What about bfd_link_hash_common? */
3377 && (h->root.type == bfd_link_hash_defined
3378 || h->root.type == bfd_link_hash_defweak))
3380 /* We don't want to issue this warning. Clobber
3381 the section size so that the warning does not
3382 get copied into the output file. */
3389 msg = bfd_alloc (abfd, sz + 1);
3393 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3398 if (! (_bfd_generic_link_add_one_symbol
3399 (info, abfd, name, BSF_WARNING, s, 0, msg,
3400 FALSE, bed->collect, NULL)))
3403 if (! info->relocatable)
3405 /* Clobber the section size so that the warning does
3406 not get copied into the output file. */
3409 /* Also set SEC_EXCLUDE, so that symbols defined in
3410 the warning section don't get copied to the output. */
3411 s->flags |= SEC_EXCLUDE;
3420 /* If we are creating a shared library, create all the dynamic
3421 sections immediately. We need to attach them to something,
3422 so we attach them to this BFD, provided it is the right
3423 format. FIXME: If there are no input BFD's of the same
3424 format as the output, we can't make a shared library. */
3426 && is_elf_hash_table (htab)
3427 && htab->root.creator == abfd->xvec
3428 && !htab->dynamic_sections_created)
3430 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3434 else if (!is_elf_hash_table (htab))
3439 const char *soname = NULL;
3440 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3443 /* ld --just-symbols and dynamic objects don't mix very well.
3444 ld shouldn't allow it. */
3445 if ((s = abfd->sections) != NULL
3446 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3449 /* If this dynamic lib was specified on the command line with
3450 --as-needed in effect, then we don't want to add a DT_NEEDED
3451 tag unless the lib is actually used. Similary for libs brought
3452 in by another lib's DT_NEEDED. When --no-add-needed is used
3453 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3454 any dynamic library in DT_NEEDED tags in the dynamic lib at
3456 add_needed = (elf_dyn_lib_class (abfd)
3457 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3458 | DYN_NO_NEEDED)) == 0;
3460 s = bfd_get_section_by_name (abfd, ".dynamic");
3466 unsigned long shlink;
3468 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3469 goto error_free_dyn;
3471 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3473 goto error_free_dyn;
3474 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3476 for (extdyn = dynbuf;
3477 extdyn < dynbuf + s->size;
3478 extdyn += bed->s->sizeof_dyn)
3480 Elf_Internal_Dyn dyn;
3482 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3483 if (dyn.d_tag == DT_SONAME)
3485 unsigned int tagv = dyn.d_un.d_val;
3486 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3488 goto error_free_dyn;
3490 if (dyn.d_tag == DT_NEEDED)
3492 struct bfd_link_needed_list *n, **pn;
3494 unsigned int tagv = dyn.d_un.d_val;
3496 amt = sizeof (struct bfd_link_needed_list);
3497 n = bfd_alloc (abfd, amt);
3498 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3499 if (n == NULL || fnm == NULL)
3500 goto error_free_dyn;
3501 amt = strlen (fnm) + 1;
3502 anm = bfd_alloc (abfd, amt);
3504 goto error_free_dyn;
3505 memcpy (anm, fnm, amt);
3509 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3513 if (dyn.d_tag == DT_RUNPATH)
3515 struct bfd_link_needed_list *n, **pn;
3517 unsigned int tagv = dyn.d_un.d_val;
3519 amt = sizeof (struct bfd_link_needed_list);
3520 n = bfd_alloc (abfd, amt);
3521 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3522 if (n == NULL || fnm == NULL)
3523 goto error_free_dyn;
3524 amt = strlen (fnm) + 1;
3525 anm = bfd_alloc (abfd, amt);
3527 goto error_free_dyn;
3528 memcpy (anm, fnm, amt);
3532 for (pn = & runpath;
3538 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3539 if (!runpath && dyn.d_tag == DT_RPATH)
3541 struct bfd_link_needed_list *n, **pn;
3543 unsigned int tagv = dyn.d_un.d_val;
3545 amt = sizeof (struct bfd_link_needed_list);
3546 n = bfd_alloc (abfd, amt);
3547 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3548 if (n == NULL || fnm == NULL)
3549 goto error_free_dyn;
3550 amt = strlen (fnm) + 1;
3551 anm = bfd_alloc (abfd, amt);
3558 memcpy (anm, fnm, amt);
3573 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3574 frees all more recently bfd_alloc'd blocks as well. */
3580 struct bfd_link_needed_list **pn;
3581 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3586 /* We do not want to include any of the sections in a dynamic
3587 object in the output file. We hack by simply clobbering the
3588 list of sections in the BFD. This could be handled more
3589 cleanly by, say, a new section flag; the existing
3590 SEC_NEVER_LOAD flag is not the one we want, because that one
3591 still implies that the section takes up space in the output
3593 bfd_section_list_clear (abfd);
3595 /* Find the name to use in a DT_NEEDED entry that refers to this
3596 object. If the object has a DT_SONAME entry, we use it.
3597 Otherwise, if the generic linker stuck something in
3598 elf_dt_name, we use that. Otherwise, we just use the file
3600 if (soname == NULL || *soname == '\0')
3602 soname = elf_dt_name (abfd);
3603 if (soname == NULL || *soname == '\0')
3604 soname = bfd_get_filename (abfd);
3607 /* Save the SONAME because sometimes the linker emulation code
3608 will need to know it. */
3609 elf_dt_name (abfd) = soname;
3611 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3615 /* If we have already included this dynamic object in the
3616 link, just ignore it. There is no reason to include a
3617 particular dynamic object more than once. */
3622 /* If this is a dynamic object, we always link against the .dynsym
3623 symbol table, not the .symtab symbol table. The dynamic linker
3624 will only see the .dynsym symbol table, so there is no reason to
3625 look at .symtab for a dynamic object. */
3627 if (! dynamic || elf_dynsymtab (abfd) == 0)
3628 hdr = &elf_tdata (abfd)->symtab_hdr;
3630 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3632 symcount = hdr->sh_size / bed->s->sizeof_sym;
3634 /* The sh_info field of the symtab header tells us where the
3635 external symbols start. We don't care about the local symbols at
3637 if (elf_bad_symtab (abfd))
3639 extsymcount = symcount;
3644 extsymcount = symcount - hdr->sh_info;
3645 extsymoff = hdr->sh_info;
3649 if (extsymcount != 0)
3651 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3653 if (isymbuf == NULL)
3656 /* We store a pointer to the hash table entry for each external
3658 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3659 sym_hash = bfd_alloc (abfd, amt);
3660 if (sym_hash == NULL)
3661 goto error_free_sym;
3662 elf_sym_hashes (abfd) = sym_hash;
3667 /* Read in any version definitions. */
3668 if (!_bfd_elf_slurp_version_tables (abfd,
3669 info->default_imported_symver))
3670 goto error_free_sym;
3672 /* Read in the symbol versions, but don't bother to convert them
3673 to internal format. */
3674 if (elf_dynversym (abfd) != 0)
3676 Elf_Internal_Shdr *versymhdr;
3678 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3679 extversym = bfd_malloc (versymhdr->sh_size);
3680 if (extversym == NULL)
3681 goto error_free_sym;
3682 amt = versymhdr->sh_size;
3683 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3684 || bfd_bread (extversym, amt, abfd) != amt)
3685 goto error_free_vers;
3689 /* If we are loading an as-needed shared lib, save the symbol table
3690 state before we start adding symbols. If the lib turns out
3691 to be unneeded, restore the state. */
3692 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3697 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3699 struct bfd_hash_entry *p;
3700 struct elf_link_hash_entry *h;
3702 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3704 h = (struct elf_link_hash_entry *) p;
3705 entsize += htab->root.table.entsize;
3706 if (h->root.type == bfd_link_hash_warning)
3707 entsize += htab->root.table.entsize;
3711 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3712 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3713 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3714 if (old_tab == NULL)
3715 goto error_free_vers;
3717 /* Remember the current objalloc pointer, so that all mem for
3718 symbols added can later be reclaimed. */
3719 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3720 if (alloc_mark == NULL)
3721 goto error_free_vers;
3723 /* Make a special call to the linker "notice" function to
3724 tell it that we are about to handle an as-needed lib. */
3725 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3730 /* Clone the symbol table and sym hashes. Remember some
3731 pointers into the symbol table, and dynamic symbol count. */
3732 old_hash = (char *) old_tab + tabsize;
3733 old_ent = (char *) old_hash + hashsize;
3734 memcpy (old_tab, htab->root.table.table, tabsize);
3735 memcpy (old_hash, sym_hash, hashsize);
3736 old_undefs = htab->root.undefs;
3737 old_undefs_tail = htab->root.undefs_tail;
3738 old_table = htab->root.table.table;
3739 old_size = htab->root.table.size;
3740 old_count = htab->root.table.count;
3741 old_dynsymcount = htab->dynsymcount;
3743 for (i = 0; i < htab->root.table.size; i++)
3745 struct bfd_hash_entry *p;
3746 struct elf_link_hash_entry *h;
3748 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3750 memcpy (old_ent, p, htab->root.table.entsize);
3751 old_ent = (char *) old_ent + htab->root.table.entsize;
3752 h = (struct elf_link_hash_entry *) p;
3753 if (h->root.type == bfd_link_hash_warning)
3755 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3756 old_ent = (char *) old_ent + htab->root.table.entsize;
3763 ever = extversym != NULL ? extversym + extsymoff : NULL;
3764 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3766 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3770 asection *sec, *new_sec;
3773 struct elf_link_hash_entry *h;
3774 bfd_boolean definition;
3775 bfd_boolean size_change_ok;
3776 bfd_boolean type_change_ok;
3777 bfd_boolean new_weakdef;
3778 bfd_boolean override;
3780 unsigned int old_alignment;
3785 flags = BSF_NO_FLAGS;
3787 value = isym->st_value;
3789 common = bed->common_definition (isym);
3791 bind = ELF_ST_BIND (isym->st_info);
3792 if (bind == STB_LOCAL)
3794 /* This should be impossible, since ELF requires that all
3795 global symbols follow all local symbols, and that sh_info
3796 point to the first global symbol. Unfortunately, Irix 5
3800 else if (bind == STB_GLOBAL)
3802 if (isym->st_shndx != SHN_UNDEF && !common)
3805 else if (bind == STB_WEAK)
3809 /* Leave it up to the processor backend. */
3812 if (isym->st_shndx == SHN_UNDEF)
3813 sec = bfd_und_section_ptr;
3814 else if (isym->st_shndx < SHN_LORESERVE
3815 || isym->st_shndx > SHN_HIRESERVE)
3817 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3819 sec = bfd_abs_section_ptr;
3820 else if (sec->kept_section)
3822 /* Symbols from discarded section are undefined. We keep
3824 sec = bfd_und_section_ptr;
3825 isym->st_shndx = SHN_UNDEF;
3827 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3830 else if (isym->st_shndx == SHN_ABS)
3831 sec = bfd_abs_section_ptr;
3832 else if (isym->st_shndx == SHN_COMMON)
3834 sec = bfd_com_section_ptr;
3835 /* What ELF calls the size we call the value. What ELF
3836 calls the value we call the alignment. */
3837 value = isym->st_size;
3841 /* Leave it up to the processor backend. */
3844 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3847 goto error_free_vers;
3849 if (isym->st_shndx == SHN_COMMON
3850 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3851 && !info->relocatable)
3853 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3857 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3860 | SEC_LINKER_CREATED
3861 | SEC_THREAD_LOCAL));
3863 goto error_free_vers;
3867 else if (bed->elf_add_symbol_hook)
3869 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3871 goto error_free_vers;
3873 /* The hook function sets the name to NULL if this symbol
3874 should be skipped for some reason. */
3879 /* Sanity check that all possibilities were handled. */
3882 bfd_set_error (bfd_error_bad_value);
3883 goto error_free_vers;
3886 if (bfd_is_und_section (sec)
3887 || bfd_is_com_section (sec))
3892 size_change_ok = FALSE;
3893 type_change_ok = bed->type_change_ok;
3898 if (is_elf_hash_table (htab))
3900 Elf_Internal_Versym iver;
3901 unsigned int vernum = 0;
3906 if (info->default_imported_symver)
3907 /* Use the default symbol version created earlier. */
3908 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3913 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3915 vernum = iver.vs_vers & VERSYM_VERSION;
3917 /* If this is a hidden symbol, or if it is not version
3918 1, we append the version name to the symbol name.
3919 However, we do not modify a non-hidden absolute symbol
3920 if it is not a function, because it might be the version
3921 symbol itself. FIXME: What if it isn't? */
3922 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3924 && (!bfd_is_abs_section (sec)
3925 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
3928 size_t namelen, verlen, newlen;
3931 if (isym->st_shndx != SHN_UNDEF)
3933 if (vernum > elf_tdata (abfd)->cverdefs)
3935 else if (vernum > 1)
3937 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3943 (*_bfd_error_handler)
3944 (_("%B: %s: invalid version %u (max %d)"),
3946 elf_tdata (abfd)->cverdefs);
3947 bfd_set_error (bfd_error_bad_value);
3948 goto error_free_vers;
3953 /* We cannot simply test for the number of
3954 entries in the VERNEED section since the
3955 numbers for the needed versions do not start
3957 Elf_Internal_Verneed *t;
3960 for (t = elf_tdata (abfd)->verref;
3964 Elf_Internal_Vernaux *a;
3966 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3968 if (a->vna_other == vernum)
3970 verstr = a->vna_nodename;
3979 (*_bfd_error_handler)
3980 (_("%B: %s: invalid needed version %d"),
3981 abfd, name, vernum);
3982 bfd_set_error (bfd_error_bad_value);
3983 goto error_free_vers;
3987 namelen = strlen (name);
3988 verlen = strlen (verstr);
3989 newlen = namelen + verlen + 2;
3990 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3991 && isym->st_shndx != SHN_UNDEF)
3994 newname = bfd_hash_allocate (&htab->root.table, newlen);
3995 if (newname == NULL)
3996 goto error_free_vers;
3997 memcpy (newname, name, namelen);
3998 p = newname + namelen;
4000 /* If this is a defined non-hidden version symbol,
4001 we add another @ to the name. This indicates the
4002 default version of the symbol. */
4003 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4004 && isym->st_shndx != SHN_UNDEF)
4006 memcpy (p, verstr, verlen + 1);
4011 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4012 &value, &old_alignment,
4013 sym_hash, &skip, &override,
4014 &type_change_ok, &size_change_ok))
4015 goto error_free_vers;
4024 while (h->root.type == bfd_link_hash_indirect
4025 || h->root.type == bfd_link_hash_warning)
4026 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4028 /* Remember the old alignment if this is a common symbol, so
4029 that we don't reduce the alignment later on. We can't
4030 check later, because _bfd_generic_link_add_one_symbol
4031 will set a default for the alignment which we want to
4032 override. We also remember the old bfd where the existing
4033 definition comes from. */
4034 switch (h->root.type)
4039 case bfd_link_hash_defined:
4040 case bfd_link_hash_defweak:
4041 old_bfd = h->root.u.def.section->owner;
4044 case bfd_link_hash_common:
4045 old_bfd = h->root.u.c.p->section->owner;
4046 old_alignment = h->root.u.c.p->alignment_power;
4050 if (elf_tdata (abfd)->verdef != NULL
4054 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4057 if (! (_bfd_generic_link_add_one_symbol
4058 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4059 (struct bfd_link_hash_entry **) sym_hash)))
4060 goto error_free_vers;
4063 while (h->root.type == bfd_link_hash_indirect
4064 || h->root.type == bfd_link_hash_warning)
4065 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4068 new_weakdef = FALSE;
4071 && (flags & BSF_WEAK) != 0
4072 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4073 && is_elf_hash_table (htab)
4074 && h->u.weakdef == NULL)
4076 /* Keep a list of all weak defined non function symbols from
4077 a dynamic object, using the weakdef field. Later in this
4078 function we will set the weakdef field to the correct
4079 value. We only put non-function symbols from dynamic
4080 objects on this list, because that happens to be the only
4081 time we need to know the normal symbol corresponding to a
4082 weak symbol, and the information is time consuming to
4083 figure out. If the weakdef field is not already NULL,
4084 then this symbol was already defined by some previous
4085 dynamic object, and we will be using that previous
4086 definition anyhow. */
4088 h->u.weakdef = weaks;
4093 /* Set the alignment of a common symbol. */
4094 if ((common || bfd_is_com_section (sec))
4095 && h->root.type == bfd_link_hash_common)
4100 align = bfd_log2 (isym->st_value);
4103 /* The new symbol is a common symbol in a shared object.
4104 We need to get the alignment from the section. */
4105 align = new_sec->alignment_power;
4107 if (align > old_alignment
4108 /* Permit an alignment power of zero if an alignment of one
4109 is specified and no other alignments have been specified. */
4110 || (isym->st_value == 1 && old_alignment == 0))
4111 h->root.u.c.p->alignment_power = align;
4113 h->root.u.c.p->alignment_power = old_alignment;
4116 if (is_elf_hash_table (htab))
4120 /* Check the alignment when a common symbol is involved. This
4121 can change when a common symbol is overridden by a normal
4122 definition or a common symbol is ignored due to the old
4123 normal definition. We need to make sure the maximum
4124 alignment is maintained. */
4125 if ((old_alignment || common)
4126 && h->root.type != bfd_link_hash_common)
4128 unsigned int common_align;
4129 unsigned int normal_align;
4130 unsigned int symbol_align;
4134 symbol_align = ffs (h->root.u.def.value) - 1;
4135 if (h->root.u.def.section->owner != NULL
4136 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4138 normal_align = h->root.u.def.section->alignment_power;
4139 if (normal_align > symbol_align)
4140 normal_align = symbol_align;
4143 normal_align = symbol_align;
4147 common_align = old_alignment;
4148 common_bfd = old_bfd;
4153 common_align = bfd_log2 (isym->st_value);
4155 normal_bfd = old_bfd;
4158 if (normal_align < common_align)
4160 /* PR binutils/2735 */
4161 if (normal_bfd == NULL)
4162 (*_bfd_error_handler)
4163 (_("Warning: alignment %u of common symbol `%s' in %B"
4164 " is greater than the alignment (%u) of its section %A"),
4165 common_bfd, h->root.u.def.section,
4166 1 << common_align, name, 1 << normal_align);
4168 (*_bfd_error_handler)
4169 (_("Warning: alignment %u of symbol `%s' in %B"
4170 " is smaller than %u in %B"),
4171 normal_bfd, common_bfd,
4172 1 << normal_align, name, 1 << common_align);
4176 /* Remember the symbol size if it isn't undefined. */
4177 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4178 && (definition || h->size == 0))
4181 && h->size != isym->st_size
4182 && ! size_change_ok)
4183 (*_bfd_error_handler)
4184 (_("Warning: size of symbol `%s' changed"
4185 " from %lu in %B to %lu in %B"),
4187 name, (unsigned long) h->size,
4188 (unsigned long) isym->st_size);
4190 h->size = isym->st_size;
4193 /* If this is a common symbol, then we always want H->SIZE
4194 to be the size of the common symbol. The code just above
4195 won't fix the size if a common symbol becomes larger. We
4196 don't warn about a size change here, because that is
4197 covered by --warn-common. Allow changed between different
4199 if (h->root.type == bfd_link_hash_common)
4200 h->size = h->root.u.c.size;
4202 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4203 && (definition || h->type == STT_NOTYPE))
4205 if (h->type != STT_NOTYPE
4206 && h->type != ELF_ST_TYPE (isym->st_info)
4207 && ! type_change_ok)
4208 (*_bfd_error_handler)
4209 (_("Warning: type of symbol `%s' changed"
4210 " from %d to %d in %B"),
4211 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
4213 h->type = ELF_ST_TYPE (isym->st_info);
4216 /* If st_other has a processor-specific meaning, specific
4217 code might be needed here. We never merge the visibility
4218 attribute with the one from a dynamic object. */
4219 if (bed->elf_backend_merge_symbol_attribute)
4220 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
4223 /* If this symbol has default visibility and the user has requested
4224 we not re-export it, then mark it as hidden. */
4225 if (definition && !dynamic
4227 || (abfd->my_archive && abfd->my_archive->no_export))
4228 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4229 isym->st_other = (STV_HIDDEN
4230 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4232 if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic)
4234 unsigned char hvis, symvis, other, nvis;
4236 /* Only merge the visibility. Leave the remainder of the
4237 st_other field to elf_backend_merge_symbol_attribute. */
4238 other = h->other & ~ELF_ST_VISIBILITY (-1);
4240 /* Combine visibilities, using the most constraining one. */
4241 hvis = ELF_ST_VISIBILITY (h->other);
4242 symvis = ELF_ST_VISIBILITY (isym->st_other);
4248 nvis = hvis < symvis ? hvis : symvis;
4250 h->other = other | nvis;
4253 /* Set a flag in the hash table entry indicating the type of
4254 reference or definition we just found. Keep a count of
4255 the number of dynamic symbols we find. A dynamic symbol
4256 is one which is referenced or defined by both a regular
4257 object and a shared object. */
4264 if (bind != STB_WEAK)
4265 h->ref_regular_nonweak = 1;
4269 if (! info->executable
4282 || (h->u.weakdef != NULL
4284 && h->u.weakdef->dynindx != -1))
4288 if (definition && (sec->flags & SEC_DEBUGGING))
4290 /* We don't want to make debug symbol dynamic. */
4291 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4295 /* Check to see if we need to add an indirect symbol for
4296 the default name. */
4297 if (definition || h->root.type == bfd_link_hash_common)
4298 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4299 &sec, &value, &dynsym,
4301 goto error_free_vers;
4303 if (definition && !dynamic)
4305 char *p = strchr (name, ELF_VER_CHR);
4306 if (p != NULL && p[1] != ELF_VER_CHR)
4308 /* Queue non-default versions so that .symver x, x@FOO
4309 aliases can be checked. */
4312 amt = ((isymend - isym + 1)
4313 * sizeof (struct elf_link_hash_entry *));
4314 nondeflt_vers = bfd_malloc (amt);
4316 nondeflt_vers[nondeflt_vers_cnt++] = h;
4320 if (dynsym && h->dynindx == -1)
4322 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4323 goto error_free_vers;
4324 if (h->u.weakdef != NULL
4326 && h->u.weakdef->dynindx == -1)
4328 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4329 goto error_free_vers;
4332 else if (dynsym && h->dynindx != -1)
4333 /* If the symbol already has a dynamic index, but
4334 visibility says it should not be visible, turn it into
4336 switch (ELF_ST_VISIBILITY (h->other))
4340 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4351 const char *soname = elf_dt_name (abfd);
4353 /* A symbol from a library loaded via DT_NEEDED of some
4354 other library is referenced by a regular object.
4355 Add a DT_NEEDED entry for it. Issue an error if
4356 --no-add-needed is used. */
4357 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4359 bfd_boolean looks_soish;
4360 const char *print_name;
4362 size_t len, lend = 0;
4364 looks_soish = FALSE;
4365 print_name = soname;
4366 print_len = strlen(soname);
4367 if (strncmp(soname, "lib", 3) == 0)
4370 if (len > 5 && strcmp(soname + len - 2, ".a") == 0)
4374 while (len > 6 && (ISDIGIT(soname[len - 1]) ||
4375 soname[len - 1] == '.'))
4377 if (strncmp(soname + len - 3, ".so", 3) == 0)
4382 print_name = soname + 3;
4388 (*_bfd_error_handler)
4389 (_("undefined reference to symbol `%s' (try adding -l%s%.*s)"),
4390 name, looks_soish? "" : ":", print_len, print_name);
4391 bfd_set_error (bfd_error_bad_value);
4392 goto error_free_vers;
4395 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4398 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4400 goto error_free_vers;
4402 BFD_ASSERT (ret == 0);
4407 if (extversym != NULL)
4413 if (isymbuf != NULL)
4419 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4423 /* Restore the symbol table. */
4424 if (bed->as_needed_cleanup)
4425 (*bed->as_needed_cleanup) (abfd, info);
4426 old_hash = (char *) old_tab + tabsize;
4427 old_ent = (char *) old_hash + hashsize;
4428 sym_hash = elf_sym_hashes (abfd);
4429 htab->root.table.table = old_table;
4430 htab->root.table.size = old_size;
4431 htab->root.table.count = old_count;
4432 memcpy (htab->root.table.table, old_tab, tabsize);
4433 memcpy (sym_hash, old_hash, hashsize);
4434 htab->root.undefs = old_undefs;
4435 htab->root.undefs_tail = old_undefs_tail;
4436 for (i = 0; i < htab->root.table.size; i++)
4438 struct bfd_hash_entry *p;
4439 struct elf_link_hash_entry *h;
4441 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4443 h = (struct elf_link_hash_entry *) p;
4444 if (h->root.type == bfd_link_hash_warning)
4445 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4446 if (h->dynindx >= old_dynsymcount)
4447 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4449 memcpy (p, old_ent, htab->root.table.entsize);
4450 old_ent = (char *) old_ent + htab->root.table.entsize;
4451 h = (struct elf_link_hash_entry *) p;
4452 if (h->root.type == bfd_link_hash_warning)
4454 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4455 old_ent = (char *) old_ent + htab->root.table.entsize;
4460 /* Make a special call to the linker "notice" function to
4461 tell it that symbols added for crefs may need to be removed. */
4462 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4467 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4469 if (nondeflt_vers != NULL)
4470 free (nondeflt_vers);
4474 if (old_tab != NULL)
4476 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4483 /* Now that all the symbols from this input file are created, handle
4484 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4485 if (nondeflt_vers != NULL)
4487 bfd_size_type cnt, symidx;
4489 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4491 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4492 char *shortname, *p;
4494 p = strchr (h->root.root.string, ELF_VER_CHR);
4496 || (h->root.type != bfd_link_hash_defined
4497 && h->root.type != bfd_link_hash_defweak))
4500 amt = p - h->root.root.string;
4501 shortname = bfd_malloc (amt + 1);
4502 memcpy (shortname, h->root.root.string, amt);
4503 shortname[amt] = '\0';
4505 hi = (struct elf_link_hash_entry *)
4506 bfd_link_hash_lookup (&htab->root, shortname,
4507 FALSE, FALSE, FALSE);
4509 && hi->root.type == h->root.type
4510 && hi->root.u.def.value == h->root.u.def.value
4511 && hi->root.u.def.section == h->root.u.def.section)
4513 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4514 hi->root.type = bfd_link_hash_indirect;
4515 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4516 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4517 sym_hash = elf_sym_hashes (abfd);
4519 for (symidx = 0; symidx < extsymcount; ++symidx)
4520 if (sym_hash[symidx] == hi)
4522 sym_hash[symidx] = h;
4528 free (nondeflt_vers);
4529 nondeflt_vers = NULL;
4532 /* Now set the weakdefs field correctly for all the weak defined
4533 symbols we found. The only way to do this is to search all the
4534 symbols. Since we only need the information for non functions in
4535 dynamic objects, that's the only time we actually put anything on
4536 the list WEAKS. We need this information so that if a regular
4537 object refers to a symbol defined weakly in a dynamic object, the
4538 real symbol in the dynamic object is also put in the dynamic
4539 symbols; we also must arrange for both symbols to point to the
4540 same memory location. We could handle the general case of symbol
4541 aliasing, but a general symbol alias can only be generated in
4542 assembler code, handling it correctly would be very time
4543 consuming, and other ELF linkers don't handle general aliasing
4547 struct elf_link_hash_entry **hpp;
4548 struct elf_link_hash_entry **hppend;
4549 struct elf_link_hash_entry **sorted_sym_hash;
4550 struct elf_link_hash_entry *h;
4553 /* Since we have to search the whole symbol list for each weak
4554 defined symbol, search time for N weak defined symbols will be
4555 O(N^2). Binary search will cut it down to O(NlogN). */
4556 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4557 sorted_sym_hash = bfd_malloc (amt);
4558 if (sorted_sym_hash == NULL)
4560 sym_hash = sorted_sym_hash;
4561 hpp = elf_sym_hashes (abfd);
4562 hppend = hpp + extsymcount;
4564 for (; hpp < hppend; hpp++)
4568 && h->root.type == bfd_link_hash_defined
4569 && !bed->is_function_type (h->type))
4577 qsort (sorted_sym_hash, sym_count,
4578 sizeof (struct elf_link_hash_entry *),
4581 while (weaks != NULL)
4583 struct elf_link_hash_entry *hlook;
4590 weaks = hlook->u.weakdef;
4591 hlook->u.weakdef = NULL;
4593 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4594 || hlook->root.type == bfd_link_hash_defweak
4595 || hlook->root.type == bfd_link_hash_common
4596 || hlook->root.type == bfd_link_hash_indirect);
4597 slook = hlook->root.u.def.section;
4598 vlook = hlook->root.u.def.value;
4605 bfd_signed_vma vdiff;
4607 h = sorted_sym_hash [idx];
4608 vdiff = vlook - h->root.u.def.value;
4615 long sdiff = slook->id - h->root.u.def.section->id;
4628 /* We didn't find a value/section match. */
4632 for (i = ilook; i < sym_count; i++)
4634 h = sorted_sym_hash [i];
4636 /* Stop if value or section doesn't match. */
4637 if (h->root.u.def.value != vlook
4638 || h->root.u.def.section != slook)
4640 else if (h != hlook)
4642 hlook->u.weakdef = h;
4644 /* If the weak definition is in the list of dynamic
4645 symbols, make sure the real definition is put
4647 if (hlook->dynindx != -1 && h->dynindx == -1)
4649 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4653 /* If the real definition is in the list of dynamic
4654 symbols, make sure the weak definition is put
4655 there as well. If we don't do this, then the
4656 dynamic loader might not merge the entries for the
4657 real definition and the weak definition. */
4658 if (h->dynindx != -1 && hlook->dynindx == -1)
4660 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4668 free (sorted_sym_hash);
4671 if (bed->check_directives)
4672 (*bed->check_directives) (abfd, info);
4674 /* If this object is the same format as the output object, and it is
4675 not a shared library, then let the backend look through the
4678 This is required to build global offset table entries and to
4679 arrange for dynamic relocs. It is not required for the
4680 particular common case of linking non PIC code, even when linking
4681 against shared libraries, but unfortunately there is no way of
4682 knowing whether an object file has been compiled PIC or not.
4683 Looking through the relocs is not particularly time consuming.
4684 The problem is that we must either (1) keep the relocs in memory,
4685 which causes the linker to require additional runtime memory or
4686 (2) read the relocs twice from the input file, which wastes time.
4687 This would be a good case for using mmap.
4689 I have no idea how to handle linking PIC code into a file of a
4690 different format. It probably can't be done. */
4692 && is_elf_hash_table (htab)
4693 && bed->check_relocs != NULL
4694 && (*bed->relocs_compatible) (abfd->xvec, htab->root.creator))
4698 for (o = abfd->sections; o != NULL; o = o->next)
4700 Elf_Internal_Rela *internal_relocs;
4703 if ((o->flags & SEC_RELOC) == 0
4704 || o->reloc_count == 0
4705 || ((info->strip == strip_all || info->strip == strip_debugger)
4706 && (o->flags & SEC_DEBUGGING) != 0)
4707 || bfd_is_abs_section (o->output_section))
4710 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4712 if (internal_relocs == NULL)
4715 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4717 if (elf_section_data (o)->relocs != internal_relocs)
4718 free (internal_relocs);
4725 /* If this is a non-traditional link, try to optimize the handling
4726 of the .stab/.stabstr sections. */
4728 && ! info->traditional_format
4729 && is_elf_hash_table (htab)
4730 && (info->strip != strip_all && info->strip != strip_debugger))
4734 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4735 if (stabstr != NULL)
4737 bfd_size_type string_offset = 0;
4740 for (stab = abfd->sections; stab; stab = stab->next)
4741 if (CONST_STRNEQ (stab->name, ".stab")
4742 && (!stab->name[5] ||
4743 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4744 && (stab->flags & SEC_MERGE) == 0
4745 && !bfd_is_abs_section (stab->output_section))
4747 struct bfd_elf_section_data *secdata;
4749 secdata = elf_section_data (stab);
4750 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4751 stabstr, &secdata->sec_info,
4754 if (secdata->sec_info)
4755 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4760 if (is_elf_hash_table (htab) && add_needed)
4762 /* Add this bfd to the loaded list. */
4763 struct elf_link_loaded_list *n;
4765 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4769 n->next = htab->loaded;
4776 if (old_tab != NULL)
4778 if (nondeflt_vers != NULL)
4779 free (nondeflt_vers);
4780 if (extversym != NULL)
4783 if (isymbuf != NULL)
4789 /* Return the linker hash table entry of a symbol that might be
4790 satisfied by an archive symbol. Return -1 on error. */
4792 struct elf_link_hash_entry *
4793 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4794 struct bfd_link_info *info,
4797 struct elf_link_hash_entry *h;
4801 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4805 /* If this is a default version (the name contains @@), look up the
4806 symbol again with only one `@' as well as without the version.
4807 The effect is that references to the symbol with and without the
4808 version will be matched by the default symbol in the archive. */
4810 p = strchr (name, ELF_VER_CHR);
4811 if (p == NULL || p[1] != ELF_VER_CHR)
4814 /* First check with only one `@'. */
4815 len = strlen (name);
4816 copy = bfd_alloc (abfd, len);
4818 return (struct elf_link_hash_entry *) 0 - 1;
4820 first = p - name + 1;
4821 memcpy (copy, name, first);
4822 memcpy (copy + first, name + first + 1, len - first);
4824 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4827 /* We also need to check references to the symbol without the
4829 copy[first - 1] = '\0';
4830 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4831 FALSE, FALSE, FALSE);
4834 bfd_release (abfd, copy);
4838 /* Add symbols from an ELF archive file to the linker hash table. We
4839 don't use _bfd_generic_link_add_archive_symbols because of a
4840 problem which arises on UnixWare. The UnixWare libc.so is an
4841 archive which includes an entry libc.so.1 which defines a bunch of
4842 symbols. The libc.so archive also includes a number of other
4843 object files, which also define symbols, some of which are the same
4844 as those defined in libc.so.1. Correct linking requires that we
4845 consider each object file in turn, and include it if it defines any
4846 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4847 this; it looks through the list of undefined symbols, and includes
4848 any object file which defines them. When this algorithm is used on
4849 UnixWare, it winds up pulling in libc.so.1 early and defining a
4850 bunch of symbols. This means that some of the other objects in the
4851 archive are not included in the link, which is incorrect since they
4852 precede libc.so.1 in the archive.
4854 Fortunately, ELF archive handling is simpler than that done by
4855 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4856 oddities. In ELF, if we find a symbol in the archive map, and the
4857 symbol is currently undefined, we know that we must pull in that
4860 Unfortunately, we do have to make multiple passes over the symbol
4861 table until nothing further is resolved. */
4864 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4867 bfd_boolean *defined = NULL;
4868 bfd_boolean *included = NULL;
4872 const struct elf_backend_data *bed;
4873 struct elf_link_hash_entry * (*archive_symbol_lookup)
4874 (bfd *, struct bfd_link_info *, const char *);
4876 if (! bfd_has_map (abfd))
4878 /* An empty archive is a special case. */
4879 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4881 bfd_set_error (bfd_error_no_armap);
4885 /* Keep track of all symbols we know to be already defined, and all
4886 files we know to be already included. This is to speed up the
4887 second and subsequent passes. */
4888 c = bfd_ardata (abfd)->symdef_count;
4892 amt *= sizeof (bfd_boolean);
4893 defined = bfd_zmalloc (amt);
4894 included = bfd_zmalloc (amt);
4895 if (defined == NULL || included == NULL)
4898 symdefs = bfd_ardata (abfd)->symdefs;
4899 bed = get_elf_backend_data (abfd);
4900 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4913 symdefend = symdef + c;
4914 for (i = 0; symdef < symdefend; symdef++, i++)
4916 struct elf_link_hash_entry *h;
4918 struct bfd_link_hash_entry *undefs_tail;
4921 if (defined[i] || included[i])
4923 if (symdef->file_offset == last)
4929 h = archive_symbol_lookup (abfd, info, symdef->name);
4930 if (h == (struct elf_link_hash_entry *) 0 - 1)
4936 if (h->root.type == bfd_link_hash_common)
4938 /* We currently have a common symbol. The archive map contains
4939 a reference to this symbol, so we may want to include it. We
4940 only want to include it however, if this archive element
4941 contains a definition of the symbol, not just another common
4944 Unfortunately some archivers (including GNU ar) will put
4945 declarations of common symbols into their archive maps, as
4946 well as real definitions, so we cannot just go by the archive
4947 map alone. Instead we must read in the element's symbol
4948 table and check that to see what kind of symbol definition
4950 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4953 else if (h->root.type != bfd_link_hash_undefined)
4955 if (h->root.type != bfd_link_hash_undefweak)
4960 /* We need to include this archive member. */
4961 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4962 if (element == NULL)
4965 if (! bfd_check_format (element, bfd_object))
4968 /* Doublecheck that we have not included this object
4969 already--it should be impossible, but there may be
4970 something wrong with the archive. */
4971 if (element->archive_pass != 0)
4973 bfd_set_error (bfd_error_bad_value);
4976 element->archive_pass = 1;
4978 undefs_tail = info->hash->undefs_tail;
4980 if (! (*info->callbacks->add_archive_element) (info, element,
4983 if (! bfd_link_add_symbols (element, info))
4986 /* If there are any new undefined symbols, we need to make
4987 another pass through the archive in order to see whether
4988 they can be defined. FIXME: This isn't perfect, because
4989 common symbols wind up on undefs_tail and because an
4990 undefined symbol which is defined later on in this pass
4991 does not require another pass. This isn't a bug, but it
4992 does make the code less efficient than it could be. */
4993 if (undefs_tail != info->hash->undefs_tail)
4996 /* Look backward to mark all symbols from this object file
4997 which we have already seen in this pass. */
5001 included[mark] = TRUE;
5006 while (symdefs[mark].file_offset == symdef->file_offset);
5008 /* We mark subsequent symbols from this object file as we go
5009 on through the loop. */
5010 last = symdef->file_offset;
5021 if (defined != NULL)
5023 if (included != NULL)
5028 /* Given an ELF BFD, add symbols to the global hash table as
5032 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5034 switch (bfd_get_format (abfd))
5037 return elf_link_add_object_symbols (abfd, info);
5039 return elf_link_add_archive_symbols (abfd, info);
5041 bfd_set_error (bfd_error_wrong_format);
5046 /* This function will be called though elf_link_hash_traverse to store
5047 all hash value of the exported symbols in an array. */
5050 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5052 unsigned long **valuep = data;
5058 if (h->root.type == bfd_link_hash_warning)
5059 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5061 /* Ignore indirect symbols. These are added by the versioning code. */
5062 if (h->dynindx == -1)
5065 name = h->root.root.string;
5066 p = strchr (name, ELF_VER_CHR);
5069 alc = bfd_malloc (p - name + 1);
5070 memcpy (alc, name, p - name);
5071 alc[p - name] = '\0';
5075 /* Compute the hash value. */
5076 ha = bfd_elf_hash (name);
5078 /* Store the found hash value in the array given as the argument. */
5081 /* And store it in the struct so that we can put it in the hash table
5083 h->u.elf_hash_value = ha;
5091 struct collect_gnu_hash_codes
5094 const struct elf_backend_data *bed;
5095 unsigned long int nsyms;
5096 unsigned long int maskbits;
5097 unsigned long int *hashcodes;
5098 unsigned long int *hashval;
5099 unsigned long int *indx;
5100 unsigned long int *counts;
5103 long int min_dynindx;
5104 unsigned long int bucketcount;
5105 unsigned long int symindx;
5106 long int local_indx;
5107 long int shift1, shift2;
5108 unsigned long int mask;
5111 /* This function will be called though elf_link_hash_traverse to store
5112 all hash value of the exported symbols in an array. */
5115 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5117 struct collect_gnu_hash_codes *s = data;
5123 if (h->root.type == bfd_link_hash_warning)
5124 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5126 /* Ignore indirect symbols. These are added by the versioning code. */
5127 if (h->dynindx == -1)
5130 /* Ignore also local symbols and undefined symbols. */
5131 if (! (*s->bed->elf_hash_symbol) (h))
5134 name = h->root.root.string;
5135 p = strchr (name, ELF_VER_CHR);
5138 alc = bfd_malloc (p - name + 1);
5139 memcpy (alc, name, p - name);
5140 alc[p - name] = '\0';
5144 /* Compute the hash value. */
5145 ha = bfd_elf_gnu_hash (name);
5147 /* Store the found hash value in the array for compute_bucket_count,
5148 and also for .dynsym reordering purposes. */
5149 s->hashcodes[s->nsyms] = ha;
5150 s->hashval[h->dynindx] = ha;
5152 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5153 s->min_dynindx = h->dynindx;
5161 /* This function will be called though elf_link_hash_traverse to do
5162 final dynaminc symbol renumbering. */
5165 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5167 struct collect_gnu_hash_codes *s = data;
5168 unsigned long int bucket;
5169 unsigned long int val;
5171 if (h->root.type == bfd_link_hash_warning)
5172 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5174 /* Ignore indirect symbols. */
5175 if (h->dynindx == -1)
5178 /* Ignore also local symbols and undefined symbols. */
5179 if (! (*s->bed->elf_hash_symbol) (h))
5181 if (h->dynindx >= s->min_dynindx)
5182 h->dynindx = s->local_indx++;
5186 bucket = s->hashval[h->dynindx] % s->bucketcount;
5187 val = (s->hashval[h->dynindx] >> s->shift1)
5188 & ((s->maskbits >> s->shift1) - 1);
5189 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5191 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5192 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5193 if (s->counts[bucket] == 1)
5194 /* Last element terminates the chain. */
5196 bfd_put_32 (s->output_bfd, val,
5197 s->contents + (s->indx[bucket] - s->symindx) * 4);
5198 --s->counts[bucket];
5199 h->dynindx = s->indx[bucket]++;
5203 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5206 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5208 return !(h->forced_local
5209 || h->root.type == bfd_link_hash_undefined
5210 || h->root.type == bfd_link_hash_undefweak
5211 || ((h->root.type == bfd_link_hash_defined
5212 || h->root.type == bfd_link_hash_defweak)
5213 && h->root.u.def.section->output_section == NULL));
5216 /* Array used to determine the number of hash table buckets to use
5217 based on the number of symbols there are. If there are fewer than
5218 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5219 fewer than 37 we use 17 buckets, and so forth. We never use more
5220 than 32771 buckets. */
5222 static const size_t elf_buckets[] =
5224 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5228 /* Compute bucket count for hashing table. We do not use a static set
5229 of possible tables sizes anymore. Instead we determine for all
5230 possible reasonable sizes of the table the outcome (i.e., the
5231 number of collisions etc) and choose the best solution. The
5232 weighting functions are not too simple to allow the table to grow
5233 without bounds. Instead one of the weighting factors is the size.
5234 Therefore the result is always a good payoff between few collisions
5235 (= short chain lengths) and table size. */
5237 compute_bucket_count (struct bfd_link_info *info, unsigned long int *hashcodes,
5238 unsigned long int nsyms, int gnu_hash)
5240 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5241 size_t best_size = 0;
5242 unsigned long int i;
5245 /* We have a problem here. The following code to optimize the table
5246 size requires an integer type with more the 32 bits. If
5247 BFD_HOST_U_64_BIT is set we know about such a type. */
5248 #ifdef BFD_HOST_U_64_BIT
5253 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5254 bfd *dynobj = elf_hash_table (info)->dynobj;
5255 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5256 unsigned long int *counts;
5258 /* Possible optimization parameters: if we have NSYMS symbols we say
5259 that the hashing table must at least have NSYMS/4 and at most
5261 minsize = nsyms / 4;
5264 best_size = maxsize = nsyms * 2;
5269 if ((best_size & 31) == 0)
5273 /* Create array where we count the collisions in. We must use bfd_malloc
5274 since the size could be large. */
5276 amt *= sizeof (unsigned long int);
5277 counts = bfd_malloc (amt);
5281 /* Compute the "optimal" size for the hash table. The criteria is a
5282 minimal chain length. The minor criteria is (of course) the size
5284 for (i = minsize; i < maxsize; ++i)
5286 /* Walk through the array of hashcodes and count the collisions. */
5287 BFD_HOST_U_64_BIT max;
5288 unsigned long int j;
5289 unsigned long int fact;
5291 if (gnu_hash && (i & 31) == 0)
5294 memset (counts, '\0', i * sizeof (unsigned long int));
5296 /* Determine how often each hash bucket is used. */
5297 for (j = 0; j < nsyms; ++j)
5298 ++counts[hashcodes[j] % i];
5300 /* For the weight function we need some information about the
5301 pagesize on the target. This is information need not be 100%
5302 accurate. Since this information is not available (so far) we
5303 define it here to a reasonable default value. If it is crucial
5304 to have a better value some day simply define this value. */
5305 # ifndef BFD_TARGET_PAGESIZE
5306 # define BFD_TARGET_PAGESIZE (4096)
5309 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5311 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5314 /* Variant 1: optimize for short chains. We add the squares
5315 of all the chain lengths (which favors many small chain
5316 over a few long chains). */
5317 for (j = 0; j < i; ++j)
5318 max += counts[j] * counts[j];
5320 /* This adds penalties for the overall size of the table. */
5321 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5324 /* Variant 2: Optimize a lot more for small table. Here we
5325 also add squares of the size but we also add penalties for
5326 empty slots (the +1 term). */
5327 for (j = 0; j < i; ++j)
5328 max += (1 + counts[j]) * (1 + counts[j]);
5330 /* The overall size of the table is considered, but not as
5331 strong as in variant 1, where it is squared. */
5332 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5336 /* Compare with current best results. */
5337 if (max < best_chlen)
5347 #endif /* defined (BFD_HOST_U_64_BIT) */
5349 /* This is the fallback solution if no 64bit type is available or if we
5350 are not supposed to spend much time on optimizations. We select the
5351 bucket count using a fixed set of numbers. */
5352 for (i = 0; elf_buckets[i] != 0; i++)
5354 best_size = elf_buckets[i];
5355 if (nsyms < elf_buckets[i + 1])
5358 if (gnu_hash && best_size < 2)
5365 /* Set up the sizes and contents of the ELF dynamic sections. This is
5366 called by the ELF linker emulation before_allocation routine. We
5367 must set the sizes of the sections before the linker sets the
5368 addresses of the various sections. */
5371 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5374 const char *filter_shlib,
5375 const char * const *auxiliary_filters,
5376 struct bfd_link_info *info,
5377 asection **sinterpptr,
5378 struct bfd_elf_version_tree *verdefs)
5380 bfd_size_type soname_indx;
5382 const struct elf_backend_data *bed;
5383 struct elf_assign_sym_version_info asvinfo;
5387 soname_indx = (bfd_size_type) -1;
5389 if (!is_elf_hash_table (info->hash))
5392 bed = get_elf_backend_data (output_bfd);
5393 elf_tdata (output_bfd)->relro = info->relro;
5394 if (info->execstack)
5395 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5396 else if (info->noexecstack)
5397 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5401 asection *notesec = NULL;
5404 for (inputobj = info->input_bfds;
5406 inputobj = inputobj->link_next)
5410 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
5412 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5415 if (s->flags & SEC_CODE)
5419 else if (bed->default_execstack)
5424 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5425 if (exec && info->relocatable
5426 && notesec->output_section != bfd_abs_section_ptr)
5427 notesec->output_section->flags |= SEC_CODE;
5431 /* Any syms created from now on start with -1 in
5432 got.refcount/offset and plt.refcount/offset. */
5433 elf_hash_table (info)->init_got_refcount
5434 = elf_hash_table (info)->init_got_offset;
5435 elf_hash_table (info)->init_plt_refcount
5436 = elf_hash_table (info)->init_plt_offset;
5438 /* The backend may have to create some sections regardless of whether
5439 we're dynamic or not. */
5440 if (bed->elf_backend_always_size_sections
5441 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5444 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5447 dynobj = elf_hash_table (info)->dynobj;
5449 /* If there were no dynamic objects in the link, there is nothing to
5454 if (elf_hash_table (info)->dynamic_sections_created)
5456 struct elf_info_failed eif;
5457 struct elf_link_hash_entry *h;
5459 struct bfd_elf_version_tree *t;
5460 struct bfd_elf_version_expr *d;
5462 bfd_boolean all_defined;
5464 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5465 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5469 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5471 if (soname_indx == (bfd_size_type) -1
5472 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5478 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5480 info->flags |= DF_SYMBOLIC;
5487 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5489 if (indx == (bfd_size_type) -1
5490 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5493 if (info->new_dtags)
5495 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5496 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5501 if (filter_shlib != NULL)
5505 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5506 filter_shlib, TRUE);
5507 if (indx == (bfd_size_type) -1
5508 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5512 if (auxiliary_filters != NULL)
5514 const char * const *p;
5516 for (p = auxiliary_filters; *p != NULL; p++)
5520 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5522 if (indx == (bfd_size_type) -1
5523 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5529 eif.verdefs = verdefs;
5532 /* If we are supposed to export all symbols into the dynamic symbol
5533 table (this is not the normal case), then do so. */
5534 if (info->export_dynamic
5535 || (info->executable && info->dynamic))
5537 elf_link_hash_traverse (elf_hash_table (info),
5538 _bfd_elf_export_symbol,
5544 /* Make all global versions with definition. */
5545 for (t = verdefs; t != NULL; t = t->next)
5546 for (d = t->globals.list; d != NULL; d = d->next)
5547 if (!d->symver && d->symbol)
5549 const char *verstr, *name;
5550 size_t namelen, verlen, newlen;
5552 struct elf_link_hash_entry *newh;
5555 namelen = strlen (name);
5557 verlen = strlen (verstr);
5558 newlen = namelen + verlen + 3;
5560 newname = bfd_malloc (newlen);
5561 if (newname == NULL)
5563 memcpy (newname, name, namelen);
5565 /* Check the hidden versioned definition. */
5566 p = newname + namelen;
5568 memcpy (p, verstr, verlen + 1);
5569 newh = elf_link_hash_lookup (elf_hash_table (info),
5570 newname, FALSE, FALSE,
5573 || (newh->root.type != bfd_link_hash_defined
5574 && newh->root.type != bfd_link_hash_defweak))
5576 /* Check the default versioned definition. */
5578 memcpy (p, verstr, verlen + 1);
5579 newh = elf_link_hash_lookup (elf_hash_table (info),
5580 newname, FALSE, FALSE,
5585 /* Mark this version if there is a definition and it is
5586 not defined in a shared object. */
5588 && !newh->def_dynamic
5589 && (newh->root.type == bfd_link_hash_defined
5590 || newh->root.type == bfd_link_hash_defweak))
5594 /* Attach all the symbols to their version information. */
5595 asvinfo.output_bfd = output_bfd;
5596 asvinfo.info = info;
5597 asvinfo.verdefs = verdefs;
5598 asvinfo.failed = FALSE;
5600 elf_link_hash_traverse (elf_hash_table (info),
5601 _bfd_elf_link_assign_sym_version,
5606 if (!info->allow_undefined_version)
5608 /* Check if all global versions have a definition. */
5610 for (t = verdefs; t != NULL; t = t->next)
5611 for (d = t->globals.list; d != NULL; d = d->next)
5612 if (!d->symver && !d->script)
5614 (*_bfd_error_handler)
5615 (_("%s: undefined version: %s"),
5616 d->pattern, t->name);
5617 all_defined = FALSE;
5622 bfd_set_error (bfd_error_bad_value);
5627 /* Find all symbols which were defined in a dynamic object and make
5628 the backend pick a reasonable value for them. */
5629 elf_link_hash_traverse (elf_hash_table (info),
5630 _bfd_elf_adjust_dynamic_symbol,
5635 /* Add some entries to the .dynamic section. We fill in some of the
5636 values later, in bfd_elf_final_link, but we must add the entries
5637 now so that we know the final size of the .dynamic section. */
5639 /* If there are initialization and/or finalization functions to
5640 call then add the corresponding DT_INIT/DT_FINI entries. */
5641 h = (info->init_function
5642 ? elf_link_hash_lookup (elf_hash_table (info),
5643 info->init_function, FALSE,
5650 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5653 h = (info->fini_function
5654 ? elf_link_hash_lookup (elf_hash_table (info),
5655 info->fini_function, FALSE,
5662 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5666 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5667 if (s != NULL && s->linker_has_input)
5669 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5670 if (! info->executable)
5675 for (sub = info->input_bfds; sub != NULL;
5676 sub = sub->link_next)
5677 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5678 for (o = sub->sections; o != NULL; o = o->next)
5679 if (elf_section_data (o)->this_hdr.sh_type
5680 == SHT_PREINIT_ARRAY)
5682 (*_bfd_error_handler)
5683 (_("%B: .preinit_array section is not allowed in DSO"),
5688 bfd_set_error (bfd_error_nonrepresentable_section);
5692 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5693 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5696 s = bfd_get_section_by_name (output_bfd, ".init_array");
5697 if (s != NULL && s->linker_has_input)
5699 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5700 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5703 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5704 if (s != NULL && s->linker_has_input)
5706 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5707 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5711 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5712 /* If .dynstr is excluded from the link, we don't want any of
5713 these tags. Strictly, we should be checking each section
5714 individually; This quick check covers for the case where
5715 someone does a /DISCARD/ : { *(*) }. */
5716 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5718 bfd_size_type strsize;
5720 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5721 if ((info->emit_hash
5722 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5723 || (info->emit_gnu_hash
5724 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5725 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5726 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5727 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5728 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5729 bed->s->sizeof_sym))
5734 /* The backend must work out the sizes of all the other dynamic
5736 if (bed->elf_backend_size_dynamic_sections
5737 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5740 if (elf_hash_table (info)->dynamic_sections_created)
5742 unsigned long section_sym_count;
5745 /* Set up the version definition section. */
5746 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5747 BFD_ASSERT (s != NULL);
5749 /* We may have created additional version definitions if we are
5750 just linking a regular application. */
5751 verdefs = asvinfo.verdefs;
5753 /* Skip anonymous version tag. */
5754 if (verdefs != NULL && verdefs->vernum == 0)
5755 verdefs = verdefs->next;
5757 if (verdefs == NULL && !info->create_default_symver)
5758 s->flags |= SEC_EXCLUDE;
5763 struct bfd_elf_version_tree *t;
5765 Elf_Internal_Verdef def;
5766 Elf_Internal_Verdaux defaux;
5767 struct bfd_link_hash_entry *bh;
5768 struct elf_link_hash_entry *h;
5774 /* Make space for the base version. */
5775 size += sizeof (Elf_External_Verdef);
5776 size += sizeof (Elf_External_Verdaux);
5779 /* Make space for the default version. */
5780 if (info->create_default_symver)
5782 size += sizeof (Elf_External_Verdef);
5786 for (t = verdefs; t != NULL; t = t->next)
5788 struct bfd_elf_version_deps *n;
5790 size += sizeof (Elf_External_Verdef);
5791 size += sizeof (Elf_External_Verdaux);
5794 for (n = t->deps; n != NULL; n = n->next)
5795 size += sizeof (Elf_External_Verdaux);
5799 s->contents = bfd_alloc (output_bfd, s->size);
5800 if (s->contents == NULL && s->size != 0)
5803 /* Fill in the version definition section. */
5807 def.vd_version = VER_DEF_CURRENT;
5808 def.vd_flags = VER_FLG_BASE;
5811 if (info->create_default_symver)
5813 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5814 def.vd_next = sizeof (Elf_External_Verdef);
5818 def.vd_aux = sizeof (Elf_External_Verdef);
5819 def.vd_next = (sizeof (Elf_External_Verdef)
5820 + sizeof (Elf_External_Verdaux));
5823 if (soname_indx != (bfd_size_type) -1)
5825 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5827 def.vd_hash = bfd_elf_hash (soname);
5828 defaux.vda_name = soname_indx;
5835 name = lbasename (output_bfd->filename);
5836 def.vd_hash = bfd_elf_hash (name);
5837 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5839 if (indx == (bfd_size_type) -1)
5841 defaux.vda_name = indx;
5843 defaux.vda_next = 0;
5845 _bfd_elf_swap_verdef_out (output_bfd, &def,
5846 (Elf_External_Verdef *) p);
5847 p += sizeof (Elf_External_Verdef);
5848 if (info->create_default_symver)
5850 /* Add a symbol representing this version. */
5852 if (! (_bfd_generic_link_add_one_symbol
5853 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5855 get_elf_backend_data (dynobj)->collect, &bh)))
5857 h = (struct elf_link_hash_entry *) bh;
5860 h->type = STT_OBJECT;
5861 h->verinfo.vertree = NULL;
5863 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5866 /* Create a duplicate of the base version with the same
5867 aux block, but different flags. */
5870 def.vd_aux = sizeof (Elf_External_Verdef);
5872 def.vd_next = (sizeof (Elf_External_Verdef)
5873 + sizeof (Elf_External_Verdaux));
5876 _bfd_elf_swap_verdef_out (output_bfd, &def,
5877 (Elf_External_Verdef *) p);
5878 p += sizeof (Elf_External_Verdef);
5880 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5881 (Elf_External_Verdaux *) p);
5882 p += sizeof (Elf_External_Verdaux);
5884 for (t = verdefs; t != NULL; t = t->next)
5887 struct bfd_elf_version_deps *n;
5890 for (n = t->deps; n != NULL; n = n->next)
5893 /* Add a symbol representing this version. */
5895 if (! (_bfd_generic_link_add_one_symbol
5896 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5898 get_elf_backend_data (dynobj)->collect, &bh)))
5900 h = (struct elf_link_hash_entry *) bh;
5903 h->type = STT_OBJECT;
5904 h->verinfo.vertree = t;
5906 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5909 def.vd_version = VER_DEF_CURRENT;
5911 if (t->globals.list == NULL
5912 && t->locals.list == NULL
5914 def.vd_flags |= VER_FLG_WEAK;
5915 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5916 def.vd_cnt = cdeps + 1;
5917 def.vd_hash = bfd_elf_hash (t->name);
5918 def.vd_aux = sizeof (Elf_External_Verdef);
5920 if (t->next != NULL)
5921 def.vd_next = (sizeof (Elf_External_Verdef)
5922 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5924 _bfd_elf_swap_verdef_out (output_bfd, &def,
5925 (Elf_External_Verdef *) p);
5926 p += sizeof (Elf_External_Verdef);
5928 defaux.vda_name = h->dynstr_index;
5929 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5931 defaux.vda_next = 0;
5932 if (t->deps != NULL)
5933 defaux.vda_next = sizeof (Elf_External_Verdaux);
5934 t->name_indx = defaux.vda_name;
5936 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5937 (Elf_External_Verdaux *) p);
5938 p += sizeof (Elf_External_Verdaux);
5940 for (n = t->deps; n != NULL; n = n->next)
5942 if (n->version_needed == NULL)
5944 /* This can happen if there was an error in the
5946 defaux.vda_name = 0;
5950 defaux.vda_name = n->version_needed->name_indx;
5951 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5954 if (n->next == NULL)
5955 defaux.vda_next = 0;
5957 defaux.vda_next = sizeof (Elf_External_Verdaux);
5959 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5960 (Elf_External_Verdaux *) p);
5961 p += sizeof (Elf_External_Verdaux);
5965 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5966 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5969 elf_tdata (output_bfd)->cverdefs = cdefs;
5972 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5974 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5977 else if (info->flags & DF_BIND_NOW)
5979 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5985 if (info->executable)
5986 info->flags_1 &= ~ (DF_1_INITFIRST
5989 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5993 /* Work out the size of the version reference section. */
5995 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5996 BFD_ASSERT (s != NULL);
5998 struct elf_find_verdep_info sinfo;
6000 sinfo.output_bfd = output_bfd;
6002 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6003 if (sinfo.vers == 0)
6005 sinfo.failed = FALSE;
6007 elf_link_hash_traverse (elf_hash_table (info),
6008 _bfd_elf_link_find_version_dependencies,
6011 if (elf_tdata (output_bfd)->verref == NULL)
6012 s->flags |= SEC_EXCLUDE;
6015 Elf_Internal_Verneed *t;
6020 /* Build the version definition section. */
6023 for (t = elf_tdata (output_bfd)->verref;
6027 Elf_Internal_Vernaux *a;
6029 size += sizeof (Elf_External_Verneed);
6031 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6032 size += sizeof (Elf_External_Vernaux);
6036 s->contents = bfd_alloc (output_bfd, s->size);
6037 if (s->contents == NULL)
6041 for (t = elf_tdata (output_bfd)->verref;
6046 Elf_Internal_Vernaux *a;
6050 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6053 t->vn_version = VER_NEED_CURRENT;
6055 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6056 elf_dt_name (t->vn_bfd) != NULL
6057 ? elf_dt_name (t->vn_bfd)
6058 : lbasename (t->vn_bfd->filename),
6060 if (indx == (bfd_size_type) -1)
6063 t->vn_aux = sizeof (Elf_External_Verneed);
6064 if (t->vn_nextref == NULL)
6067 t->vn_next = (sizeof (Elf_External_Verneed)
6068 + caux * sizeof (Elf_External_Vernaux));
6070 _bfd_elf_swap_verneed_out (output_bfd, t,
6071 (Elf_External_Verneed *) p);
6072 p += sizeof (Elf_External_Verneed);
6074 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6076 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6077 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6078 a->vna_nodename, FALSE);
6079 if (indx == (bfd_size_type) -1)
6082 if (a->vna_nextptr == NULL)
6085 a->vna_next = sizeof (Elf_External_Vernaux);
6087 _bfd_elf_swap_vernaux_out (output_bfd, a,
6088 (Elf_External_Vernaux *) p);
6089 p += sizeof (Elf_External_Vernaux);
6093 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6094 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6097 elf_tdata (output_bfd)->cverrefs = crefs;
6101 if ((elf_tdata (output_bfd)->cverrefs == 0
6102 && elf_tdata (output_bfd)->cverdefs == 0)
6103 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6104 §ion_sym_count) == 0)
6106 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6107 s->flags |= SEC_EXCLUDE;
6113 /* Find the first non-excluded output section. We'll use its
6114 section symbol for some emitted relocs. */
6116 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6120 for (s = output_bfd->sections; s != NULL; s = s->next)
6121 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6122 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6124 elf_hash_table (info)->text_index_section = s;
6129 /* Find two non-excluded output sections, one for code, one for data.
6130 We'll use their section symbols for some emitted relocs. */
6132 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6136 for (s = output_bfd->sections; s != NULL; s = s->next)
6137 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6138 == (SEC_ALLOC | SEC_READONLY))
6139 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6141 elf_hash_table (info)->text_index_section = s;
6145 for (s = output_bfd->sections; s != NULL; s = s->next)
6146 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6147 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6149 elf_hash_table (info)->data_index_section = s;
6153 if (elf_hash_table (info)->text_index_section == NULL)
6154 elf_hash_table (info)->text_index_section
6155 = elf_hash_table (info)->data_index_section;
6159 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6161 const struct elf_backend_data *bed;
6163 if (!is_elf_hash_table (info->hash))
6166 bed = get_elf_backend_data (output_bfd);
6167 (*bed->elf_backend_init_index_section) (output_bfd, info);
6169 if (elf_hash_table (info)->dynamic_sections_created)
6173 bfd_size_type dynsymcount;
6174 unsigned long section_sym_count;
6175 unsigned int dtagcount;
6177 dynobj = elf_hash_table (info)->dynobj;
6179 /* Assign dynsym indicies. In a shared library we generate a
6180 section symbol for each output section, which come first.
6181 Next come all of the back-end allocated local dynamic syms,
6182 followed by the rest of the global symbols. */
6184 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6185 §ion_sym_count);
6187 /* Work out the size of the symbol version section. */
6188 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6189 BFD_ASSERT (s != NULL);
6190 if (dynsymcount != 0
6191 && (s->flags & SEC_EXCLUDE) == 0)
6193 s->size = dynsymcount * sizeof (Elf_External_Versym);
6194 s->contents = bfd_zalloc (output_bfd, s->size);
6195 if (s->contents == NULL)
6198 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6202 /* Set the size of the .dynsym and .hash sections. We counted
6203 the number of dynamic symbols in elf_link_add_object_symbols.
6204 We will build the contents of .dynsym and .hash when we build
6205 the final symbol table, because until then we do not know the
6206 correct value to give the symbols. We built the .dynstr
6207 section as we went along in elf_link_add_object_symbols. */
6208 s = bfd_get_section_by_name (dynobj, ".dynsym");
6209 BFD_ASSERT (s != NULL);
6210 s->size = dynsymcount * bed->s->sizeof_sym;
6212 if (dynsymcount != 0)
6214 s->contents = bfd_alloc (output_bfd, s->size);
6215 if (s->contents == NULL)
6218 /* The first entry in .dynsym is a dummy symbol.
6219 Clear all the section syms, in case we don't output them all. */
6220 ++section_sym_count;
6221 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6224 elf_hash_table (info)->bucketcount = 0;
6226 /* Compute the size of the hashing table. As a side effect this
6227 computes the hash values for all the names we export. */
6228 if (info->emit_hash)
6230 unsigned long int *hashcodes;
6231 unsigned long int *hashcodesp;
6233 unsigned long int nsyms;
6235 size_t hash_entry_size;
6237 /* Compute the hash values for all exported symbols. At the same
6238 time store the values in an array so that we could use them for
6240 amt = dynsymcount * sizeof (unsigned long int);
6241 hashcodes = bfd_malloc (amt);
6242 if (hashcodes == NULL)
6244 hashcodesp = hashcodes;
6246 /* Put all hash values in HASHCODES. */
6247 elf_link_hash_traverse (elf_hash_table (info),
6248 elf_collect_hash_codes, &hashcodesp);
6250 nsyms = hashcodesp - hashcodes;
6252 = compute_bucket_count (info, hashcodes, nsyms, 0);
6255 if (bucketcount == 0)
6258 elf_hash_table (info)->bucketcount = bucketcount;
6260 s = bfd_get_section_by_name (dynobj, ".hash");
6261 BFD_ASSERT (s != NULL);
6262 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6263 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6264 s->contents = bfd_zalloc (output_bfd, s->size);
6265 if (s->contents == NULL)
6268 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6269 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6270 s->contents + hash_entry_size);
6273 if (info->emit_gnu_hash)
6276 unsigned char *contents;
6277 struct collect_gnu_hash_codes cinfo;
6281 memset (&cinfo, 0, sizeof (cinfo));
6283 /* Compute the hash values for all exported symbols. At the same
6284 time store the values in an array so that we could use them for
6286 amt = dynsymcount * 2 * sizeof (unsigned long int);
6287 cinfo.hashcodes = bfd_malloc (amt);
6288 if (cinfo.hashcodes == NULL)
6291 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6292 cinfo.min_dynindx = -1;
6293 cinfo.output_bfd = output_bfd;
6296 /* Put all hash values in HASHCODES. */
6297 elf_link_hash_traverse (elf_hash_table (info),
6298 elf_collect_gnu_hash_codes, &cinfo);
6301 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6303 if (bucketcount == 0)
6305 free (cinfo.hashcodes);
6309 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6310 BFD_ASSERT (s != NULL);
6312 if (cinfo.nsyms == 0)
6314 /* Empty .gnu.hash section is special. */
6315 BFD_ASSERT (cinfo.min_dynindx == -1);
6316 free (cinfo.hashcodes);
6317 s->size = 5 * 4 + bed->s->arch_size / 8;
6318 contents = bfd_zalloc (output_bfd, s->size);
6319 if (contents == NULL)
6321 s->contents = contents;
6322 /* 1 empty bucket. */
6323 bfd_put_32 (output_bfd, 1, contents);
6324 /* SYMIDX above the special symbol 0. */
6325 bfd_put_32 (output_bfd, 1, contents + 4);
6326 /* Just one word for bitmask. */
6327 bfd_put_32 (output_bfd, 1, contents + 8);
6328 /* Only hash fn bloom filter. */
6329 bfd_put_32 (output_bfd, 0, contents + 12);
6330 /* No hashes are valid - empty bitmask. */
6331 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6332 /* No hashes in the only bucket. */
6333 bfd_put_32 (output_bfd, 0,
6334 contents + 16 + bed->s->arch_size / 8);
6338 unsigned long int maskwords, maskbitslog2;
6339 BFD_ASSERT (cinfo.min_dynindx != -1);
6341 maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
6342 if (maskbitslog2 < 3)
6344 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6345 maskbitslog2 = maskbitslog2 + 3;
6347 maskbitslog2 = maskbitslog2 + 2;
6348 if (bed->s->arch_size == 64)
6350 if (maskbitslog2 == 5)
6356 cinfo.mask = (1 << cinfo.shift1) - 1;
6357 cinfo.shift2 = maskbitslog2;
6358 cinfo.maskbits = 1 << maskbitslog2;
6359 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6360 amt = bucketcount * sizeof (unsigned long int) * 2;
6361 amt += maskwords * sizeof (bfd_vma);
6362 cinfo.bitmask = bfd_malloc (amt);
6363 if (cinfo.bitmask == NULL)
6365 free (cinfo.hashcodes);
6369 cinfo.counts = (void *) (cinfo.bitmask + maskwords);
6370 cinfo.indx = cinfo.counts + bucketcount;
6371 cinfo.symindx = dynsymcount - cinfo.nsyms;
6372 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6374 /* Determine how often each hash bucket is used. */
6375 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6376 for (i = 0; i < cinfo.nsyms; ++i)
6377 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6379 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6380 if (cinfo.counts[i] != 0)
6382 cinfo.indx[i] = cnt;
6383 cnt += cinfo.counts[i];
6385 BFD_ASSERT (cnt == dynsymcount);
6386 cinfo.bucketcount = bucketcount;
6387 cinfo.local_indx = cinfo.min_dynindx;
6389 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6390 s->size += cinfo.maskbits / 8;
6391 contents = bfd_zalloc (output_bfd, s->size);
6392 if (contents == NULL)
6394 free (cinfo.bitmask);
6395 free (cinfo.hashcodes);
6399 s->contents = contents;
6400 bfd_put_32 (output_bfd, bucketcount, contents);
6401 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6402 bfd_put_32 (output_bfd, maskwords, contents + 8);
6403 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6404 contents += 16 + cinfo.maskbits / 8;
6406 for (i = 0; i < bucketcount; ++i)
6408 if (cinfo.counts[i] == 0)
6409 bfd_put_32 (output_bfd, 0, contents);
6411 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6415 cinfo.contents = contents;
6417 /* Renumber dynamic symbols, populate .gnu.hash section. */
6418 elf_link_hash_traverse (elf_hash_table (info),
6419 elf_renumber_gnu_hash_syms, &cinfo);
6421 contents = s->contents + 16;
6422 for (i = 0; i < maskwords; ++i)
6424 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6426 contents += bed->s->arch_size / 8;
6429 free (cinfo.bitmask);
6430 free (cinfo.hashcodes);
6434 s = bfd_get_section_by_name (dynobj, ".dynstr");
6435 BFD_ASSERT (s != NULL);
6437 elf_finalize_dynstr (output_bfd, info);
6439 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6441 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6442 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6449 /* Final phase of ELF linker. */
6451 /* A structure we use to avoid passing large numbers of arguments. */
6453 struct elf_final_link_info
6455 /* General link information. */
6456 struct bfd_link_info *info;
6459 /* Symbol string table. */
6460 struct bfd_strtab_hash *symstrtab;
6461 /* .dynsym section. */
6462 asection *dynsym_sec;
6463 /* .hash section. */
6465 /* symbol version section (.gnu.version). */
6466 asection *symver_sec;
6467 /* Buffer large enough to hold contents of any section. */
6469 /* Buffer large enough to hold external relocs of any section. */
6470 void *external_relocs;
6471 /* Buffer large enough to hold internal relocs of any section. */
6472 Elf_Internal_Rela *internal_relocs;
6473 /* Buffer large enough to hold external local symbols of any input
6475 bfd_byte *external_syms;
6476 /* And a buffer for symbol section indices. */
6477 Elf_External_Sym_Shndx *locsym_shndx;
6478 /* Buffer large enough to hold internal local symbols of any input
6480 Elf_Internal_Sym *internal_syms;
6481 /* Array large enough to hold a symbol index for each local symbol
6482 of any input BFD. */
6484 /* Array large enough to hold a section pointer for each local
6485 symbol of any input BFD. */
6486 asection **sections;
6487 /* Buffer to hold swapped out symbols. */
6489 /* And one for symbol section indices. */
6490 Elf_External_Sym_Shndx *symshndxbuf;
6491 /* Number of swapped out symbols in buffer. */
6492 size_t symbuf_count;
6493 /* Number of symbols which fit in symbuf. */
6495 /* And same for symshndxbuf. */
6496 size_t shndxbuf_size;
6499 /* This struct is used to pass information to elf_link_output_extsym. */
6501 struct elf_outext_info
6504 bfd_boolean localsyms;
6505 struct elf_final_link_info *finfo;
6509 /* Support for evaluating a complex relocation.
6511 Complex relocations are generalized, self-describing relocations. The
6512 implementation of them consists of two parts: complex symbols, and the
6513 relocations themselves.
6515 The relocations are use a reserved elf-wide relocation type code (R_RELC
6516 external / BFD_RELOC_RELC internal) and an encoding of relocation field
6517 information (start bit, end bit, word width, etc) into the addend. This
6518 information is extracted from CGEN-generated operand tables within gas.
6520 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
6521 internal) representing prefix-notation expressions, including but not
6522 limited to those sorts of expressions normally encoded as addends in the
6523 addend field. The symbol mangling format is:
6526 | <unary-operator> ':' <node>
6527 | <binary-operator> ':' <node> ':' <node>
6530 <literal> := 's' <digits=N> ':' <N character symbol name>
6531 | 'S' <digits=N> ':' <N character section name>
6535 <binary-operator> := as in C
6536 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
6539 set_symbol_value (bfd * bfd_with_globals,
6540 struct elf_final_link_info * finfo,
6544 bfd_boolean is_local;
6545 Elf_Internal_Sym * sym;
6546 struct elf_link_hash_entry ** sym_hashes;
6547 struct elf_link_hash_entry * h;
6549 sym_hashes = elf_sym_hashes (bfd_with_globals);
6550 sym = finfo->internal_syms + symidx;
6551 is_local = ELF_ST_BIND(sym->st_info) == STB_LOCAL;
6555 /* It is a local symbol: move it to the
6556 "absolute" section and give it a value. */
6557 sym->st_shndx = SHN_ABS;
6558 sym->st_value = val;
6562 /* It is a global symbol: set its link type
6563 to "defined" and give it a value. */
6564 h = sym_hashes [symidx];
6565 while (h->root.type == bfd_link_hash_indirect
6566 || h->root.type == bfd_link_hash_warning)
6567 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6568 h->root.type = bfd_link_hash_defined;
6569 h->root.u.def.value = val;
6570 h->root.u.def.section = bfd_abs_section_ptr;
6575 resolve_symbol (const char * name,
6577 struct elf_final_link_info * finfo,
6581 Elf_Internal_Sym * sym;
6582 struct bfd_link_hash_entry * global_entry;
6583 const char * candidate = NULL;
6584 Elf_Internal_Shdr * symtab_hdr;
6585 asection * sec = NULL;
6588 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
6590 for (i = 0; i < locsymcount; ++ i)
6592 sym = finfo->internal_syms + i;
6593 sec = finfo->sections [i];
6595 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
6598 candidate = bfd_elf_string_from_elf_section (input_bfd,
6599 symtab_hdr->sh_link,
6602 printf ("Comparing string: '%s' vs. '%s' = 0x%x\n",
6603 name, candidate, (unsigned int)sym->st_value);
6605 if (candidate && strcmp (candidate, name) == 0)
6607 * result = sym->st_value;
6609 if (sym->st_shndx > SHN_UNDEF &&
6610 sym->st_shndx < SHN_LORESERVE)
6613 printf ("adjusting for sec '%s' @ 0x%x + 0x%x\n",
6614 sec->output_section->name,
6615 (unsigned int)sec->output_section->vma,
6616 (unsigned int)sec->output_offset);
6618 * result += sec->output_offset + sec->output_section->vma;
6621 printf ("Found symbol with effective value %8.8x\n", (unsigned int)* result);
6627 /* Hmm, haven't found it yet. perhaps it is a global. */
6628 global_entry = bfd_link_hash_lookup (finfo->info->hash, name, FALSE, FALSE, TRUE);
6632 if (global_entry->type == bfd_link_hash_defined
6633 || global_entry->type == bfd_link_hash_defweak)
6635 * result = global_entry->u.def.value
6636 + global_entry->u.def.section->output_section->vma
6637 + global_entry->u.def.section->output_offset;
6639 printf ("Found GLOBAL symbol '%s' with value %8.8x\n",
6640 global_entry->root.string, (unsigned int)*result);
6645 if (global_entry->type == bfd_link_hash_common)
6647 *result = global_entry->u.def.value +
6648 bfd_com_section_ptr->output_section->vma +
6649 bfd_com_section_ptr->output_offset;
6651 printf ("Found COMMON symbol '%s' with value %8.8x\n",
6652 global_entry->root.string, (unsigned int)*result);
6661 resolve_section (const char * name,
6662 asection * sections,
6668 for (curr = sections; curr; curr = curr->next)
6669 if (strcmp (curr->name, name) == 0)
6671 *result = curr->vma;
6675 /* Hmm. still haven't found it. try pseudo-section names. */
6676 for (curr = sections; curr; curr = curr->next)
6678 len = strlen (curr->name);
6679 if (len > strlen (name))
6682 if (strncmp (curr->name, name, len) == 0)
6684 if (strncmp (".end", name + len, 4) == 0)
6686 *result = curr->vma + curr->size;
6690 /* Insert more pseudo-section names here, if you like. */
6698 undefined_reference (const char * reftype,
6701 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), reftype, name);
6705 eval_symbol (bfd_vma * result,
6709 struct elf_final_link_info * finfo,
6711 bfd_vma section_offset,
6719 const int bufsz = 4096;
6720 char symbuf [bufsz];
6721 const char * symend;
6722 bfd_boolean symbol_is_section = FALSE;
6727 if (len < 1 || len > bufsz)
6729 bfd_set_error (bfd_error_invalid_operation);
6736 * result = addr + section_offset;
6737 * advanced = sym + 1;
6742 * result = strtoul (sym, advanced, 16);
6746 symbol_is_section = TRUE;
6749 symlen = strtol (sym, &sym, 10);
6750 ++ sym; /* Skip the trailing ':'. */
6752 if ((symend < sym) || ((symlen + 1) > bufsz))
6754 bfd_set_error (bfd_error_invalid_operation);
6758 memcpy (symbuf, sym, symlen);
6759 symbuf [symlen] = '\0';
6760 * advanced = sym + symlen;
6762 /* Is it always possible, with complex symbols, that gas "mis-guessed"
6763 the symbol as a section, or vice-versa. so we're pretty liberal in our
6764 interpretation here; section means "try section first", not "must be a
6765 section", and likewise with symbol. */
6767 if (symbol_is_section)
6769 if ((resolve_section (symbuf, finfo->output_bfd->sections, result) != TRUE)
6770 && (resolve_symbol (symbuf, input_bfd, finfo, result, locsymcount) != TRUE))
6772 undefined_reference ("section", symbuf);
6778 if ((resolve_symbol (symbuf, input_bfd, finfo, result, locsymcount) != TRUE)
6779 && (resolve_section (symbuf, finfo->output_bfd->sections,
6782 undefined_reference ("symbol", symbuf);
6789 /* All that remains are operators. */
6791 #define UNARY_OP(op) \
6792 if (strncmp (sym, #op, strlen (#op)) == 0) \
6794 sym += strlen (#op); \
6797 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6798 section_offset, locsymcount, signed_p) \
6802 * result = op ((signed)a); \
6809 #define BINARY_OP(op) \
6810 if (strncmp (sym, #op, strlen (#op)) == 0) \
6812 sym += strlen (#op); \
6815 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6816 section_offset, locsymcount, signed_p) \
6820 if (eval_symbol (& b, sym, & sym, input_bfd, finfo, addr, \
6821 section_offset, locsymcount, signed_p) \
6825 * result = ((signed) a) op ((signed) b); \
6827 * result = a op b; \
6856 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
6857 bfd_set_error (bfd_error_invalid_operation);
6862 /* Entry point to evaluator, called from elf_link_input_bfd. */
6865 evaluate_complex_relocation_symbols (bfd * input_bfd,
6866 struct elf_final_link_info * finfo,
6869 const struct elf_backend_data * bed;
6870 Elf_Internal_Shdr * symtab_hdr;
6871 struct elf_link_hash_entry ** sym_hashes;
6872 asection * reloc_sec;
6873 bfd_boolean result = TRUE;
6875 /* For each section, we're going to check and see if it has any
6876 complex relocations, and we're going to evaluate any of them
6879 if (finfo->info->relocatable)
6882 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
6883 sym_hashes = elf_sym_hashes (input_bfd);
6884 bed = get_elf_backend_data (input_bfd);
6886 for (reloc_sec = input_bfd->sections; reloc_sec; reloc_sec = reloc_sec->next)
6888 Elf_Internal_Rela * internal_relocs;
6891 /* This section was omitted from the link. */
6892 if (! reloc_sec->linker_mark)
6895 /* Only process sections containing relocs. */
6896 if ((reloc_sec->flags & SEC_RELOC) == 0)
6899 if (reloc_sec->reloc_count == 0)
6902 /* Read in the relocs for this section. */
6904 = _bfd_elf_link_read_relocs (input_bfd, reloc_sec, NULL,
6905 (Elf_Internal_Rela *) NULL,
6907 if (internal_relocs == NULL)
6910 for (i = reloc_sec->reloc_count; i--;)
6912 Elf_Internal_Rela * rel;
6915 Elf_Internal_Sym * sym;
6917 bfd_vma section_offset;
6921 rel = internal_relocs + i;
6922 section_offset = reloc_sec->output_section->vma
6923 + reloc_sec->output_offset;
6924 addr = rel->r_offset;
6926 index = ELF32_R_SYM (rel->r_info);
6927 if (bed->s->arch_size == 64)
6930 if (index == STN_UNDEF)
6933 if (index < locsymcount)
6935 /* The symbol is local. */
6936 sym = finfo->internal_syms + index;
6938 /* We're only processing STT_RELC or STT_SRELC type symbols. */
6939 if ((ELF_ST_TYPE (sym->st_info) != STT_RELC) &&
6940 (ELF_ST_TYPE (sym->st_info) != STT_SRELC))
6943 sym_name = bfd_elf_string_from_elf_section
6944 (input_bfd, symtab_hdr->sh_link, sym->st_name);
6946 signed_p = (ELF_ST_TYPE (sym->st_info) == STT_SRELC);
6950 /* The symbol is global. */
6951 struct elf_link_hash_entry * h;
6953 if (elf_bad_symtab (input_bfd))
6956 h = sym_hashes [index - locsymcount];
6957 while ( h->root.type == bfd_link_hash_indirect
6958 || h->root.type == bfd_link_hash_warning)
6959 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6961 if (h->type != STT_RELC && h->type != STT_SRELC)
6964 signed_p = (h->type == STT_SRELC);
6965 sym_name = (char *) h->root.root.string;
6968 printf ("Encountered a complex symbol!");
6969 printf (" (input_bfd %s, section %s, reloc %ld\n",
6970 input_bfd->filename, reloc_sec->name, i);
6971 printf (" symbol: idx %8.8lx, name %s\n",
6973 printf (" reloc : info %8.8lx, addr %8.8lx\n",
6975 printf (" Evaluating '%s' ...\n ", sym_name);
6977 if (eval_symbol (& result, sym_name, & sym_name, input_bfd,
6978 finfo, addr, section_offset, locsymcount,
6980 /* Symbol evaluated OK. Update to absolute value. */
6981 set_symbol_value (input_bfd, finfo, index, result);
6987 if (internal_relocs != elf_section_data (reloc_sec)->relocs)
6988 free (internal_relocs);
6991 /* If nothing went wrong, then we adjusted
6992 everything we wanted to adjust. */
6997 put_value (bfd_vma size,
6998 unsigned long chunksz,
7001 bfd_byte * location)
7003 location += (size - chunksz);
7005 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
7013 bfd_put_8 (input_bfd, x, location);
7016 bfd_put_16 (input_bfd, x, location);
7019 bfd_put_32 (input_bfd, x, location);
7023 bfd_put_64 (input_bfd, x, location);
7033 get_value (bfd_vma size,
7034 unsigned long chunksz,
7036 bfd_byte * location)
7040 for (; size; size -= chunksz, location += chunksz)
7048 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7051 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7054 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7058 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7069 decode_complex_addend
7070 (unsigned long * start, /* in bits */
7071 unsigned long * oplen, /* in bits */
7072 unsigned long * len, /* in bits */
7073 unsigned long * wordsz, /* in bytes */
7074 unsigned long * chunksz, /* in bytes */
7075 unsigned long * lsb0_p,
7076 unsigned long * signed_p,
7077 unsigned long * trunc_p,
7078 unsigned long encoded)
7080 * start = encoded & 0x3F;
7081 * len = (encoded >> 6) & 0x3F;
7082 * oplen = (encoded >> 12) & 0x3F;
7083 * wordsz = (encoded >> 18) & 0xF;
7084 * chunksz = (encoded >> 22) & 0xF;
7085 * lsb0_p = (encoded >> 27) & 1;
7086 * signed_p = (encoded >> 28) & 1;
7087 * trunc_p = (encoded >> 29) & 1;
7091 bfd_elf_perform_complex_relocation
7092 (bfd * output_bfd ATTRIBUTE_UNUSED,
7093 struct bfd_link_info * info,
7095 asection * input_section,
7096 bfd_byte * contents,
7097 Elf_Internal_Rela * rel,
7098 Elf_Internal_Sym * local_syms,
7099 asection ** local_sections)
7101 const struct elf_backend_data * bed;
7102 Elf_Internal_Shdr * symtab_hdr;
7104 bfd_vma relocation = 0, shift, x;
7107 unsigned long start, oplen, len, wordsz,
7108 chunksz, lsb0_p, signed_p, trunc_p;
7110 /* Perform this reloc, since it is complex.
7111 (this is not to say that it necessarily refers to a complex
7112 symbol; merely that it is a self-describing CGEN based reloc.
7113 i.e. the addend has the complete reloc information (bit start, end,
7114 word size, etc) encoded within it.). */
7115 r_symndx = ELF32_R_SYM (rel->r_info);
7116 bed = get_elf_backend_data (input_bfd);
7117 if (bed->s->arch_size == 64)
7121 printf ("Performing complex relocation %ld...\n", r_symndx);
7124 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7125 if (r_symndx < symtab_hdr->sh_info)
7127 /* The symbol is local. */
7128 Elf_Internal_Sym * sym;
7130 sym = local_syms + r_symndx;
7131 sec = local_sections [r_symndx];
7132 relocation = sym->st_value;
7133 if (sym->st_shndx > SHN_UNDEF &&
7134 sym->st_shndx < SHN_LORESERVE)
7135 relocation += (sec->output_offset +
7136 sec->output_section->vma);
7140 /* The symbol is global. */
7141 struct elf_link_hash_entry **sym_hashes;
7142 struct elf_link_hash_entry * h;
7144 sym_hashes = elf_sym_hashes (input_bfd);
7145 h = sym_hashes [r_symndx];
7147 while (h->root.type == bfd_link_hash_indirect
7148 || h->root.type == bfd_link_hash_warning)
7149 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7151 if (h->root.type == bfd_link_hash_defined
7152 || h->root.type == bfd_link_hash_defweak)
7154 sec = h->root.u.def.section;
7155 relocation = h->root.u.def.value;
7157 if (! bfd_is_abs_section (sec))
7158 relocation += (sec->output_section->vma
7159 + sec->output_offset);
7161 if (h->root.type == bfd_link_hash_undefined
7162 && !((*info->callbacks->undefined_symbol)
7163 (info, h->root.root.string, input_bfd,
7164 input_section, rel->r_offset,
7165 info->unresolved_syms_in_objects == RM_GENERATE_ERROR
7166 || ELF_ST_VISIBILITY (h->other))))
7170 decode_complex_addend (& start, & oplen, & len, & wordsz,
7171 & chunksz, & lsb0_p, & signed_p,
7172 & trunc_p, rel->r_addend);
7174 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7177 shift = (start + 1) - len;
7179 shift = (8 * wordsz) - (start + len);
7181 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7184 printf ("Doing complex reloc: "
7185 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7186 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7187 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7188 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7189 oplen, x, mask, relocation);
7194 /* Now do an overflow check. */
7195 if (bfd_check_overflow ((signed_p ?
7196 complain_overflow_signed :
7197 complain_overflow_unsigned),
7198 len, 0, (8 * wordsz),
7199 relocation) == bfd_reloc_overflow)
7200 (*_bfd_error_handler)
7201 ("%s (%s + 0x%lx): relocation overflow: 0x%lx %sdoes not fit "
7203 input_bfd->filename, input_section->name, rel->r_offset,
7204 relocation, (signed_p ? "(signed) " : ""), mask);
7208 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7211 printf (" relocation: %8.8lx\n"
7212 " shifted mask: %8.8lx\n"
7213 " shifted/masked reloc: %8.8lx\n"
7214 " result: %8.8lx\n",
7215 relocation, (mask << shift),
7216 ((relocation & mask) << shift), x);
7218 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
7221 /* When performing a relocatable link, the input relocations are
7222 preserved. But, if they reference global symbols, the indices
7223 referenced must be updated. Update all the relocations in
7224 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7227 elf_link_adjust_relocs (bfd *abfd,
7228 Elf_Internal_Shdr *rel_hdr,
7230 struct elf_link_hash_entry **rel_hash)
7233 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7235 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7236 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7237 bfd_vma r_type_mask;
7240 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
7242 swap_in = bed->s->swap_reloc_in;
7243 swap_out = bed->s->swap_reloc_out;
7245 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
7247 swap_in = bed->s->swap_reloca_in;
7248 swap_out = bed->s->swap_reloca_out;
7253 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7256 if (bed->s->arch_size == 32)
7263 r_type_mask = 0xffffffff;
7267 erela = rel_hdr->contents;
7268 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
7270 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7273 if (*rel_hash == NULL)
7276 BFD_ASSERT ((*rel_hash)->indx >= 0);
7278 (*swap_in) (abfd, erela, irela);
7279 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7280 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7281 | (irela[j].r_info & r_type_mask));
7282 (*swap_out) (abfd, irela, erela);
7286 struct elf_link_sort_rela
7292 enum elf_reloc_type_class type;
7293 /* We use this as an array of size int_rels_per_ext_rel. */
7294 Elf_Internal_Rela rela[1];
7298 elf_link_sort_cmp1 (const void *A, const void *B)
7300 const struct elf_link_sort_rela *a = A;
7301 const struct elf_link_sort_rela *b = B;
7302 int relativea, relativeb;
7304 relativea = a->type == reloc_class_relative;
7305 relativeb = b->type == reloc_class_relative;
7307 if (relativea < relativeb)
7309 if (relativea > relativeb)
7311 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
7313 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
7315 if (a->rela->r_offset < b->rela->r_offset)
7317 if (a->rela->r_offset > b->rela->r_offset)
7323 elf_link_sort_cmp2 (const void *A, const void *B)
7325 const struct elf_link_sort_rela *a = A;
7326 const struct elf_link_sort_rela *b = B;
7329 if (a->u.offset < b->u.offset)
7331 if (a->u.offset > b->u.offset)
7333 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
7334 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
7339 if (a->rela->r_offset < b->rela->r_offset)
7341 if (a->rela->r_offset > b->rela->r_offset)
7347 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
7349 asection *dynamic_relocs;
7352 bfd_size_type count, size;
7353 size_t i, ret, sort_elt, ext_size;
7354 bfd_byte *sort, *s_non_relative, *p;
7355 struct elf_link_sort_rela *sq;
7356 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7357 int i2e = bed->s->int_rels_per_ext_rel;
7358 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7359 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7360 struct bfd_link_order *lo;
7362 bfd_boolean use_rela;
7364 /* Find a dynamic reloc section. */
7365 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
7366 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
7367 if (rela_dyn != NULL && rela_dyn->size > 0
7368 && rel_dyn != NULL && rel_dyn->size > 0)
7370 bfd_boolean use_rela_initialised = FALSE;
7372 /* This is just here to stop gcc from complaining.
7373 It's initialization checking code is not perfect. */
7376 /* Both sections are present. Examine the sizes
7377 of the indirect sections to help us choose. */
7378 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7379 if (lo->type == bfd_indirect_link_order)
7381 asection *o = lo->u.indirect.section;
7383 if ((o->size % bed->s->sizeof_rela) == 0)
7385 if ((o->size % bed->s->sizeof_rel) == 0)
7386 /* Section size is divisible by both rel and rela sizes.
7387 It is of no help to us. */
7391 /* Section size is only divisible by rela. */
7392 if (use_rela_initialised && (use_rela == FALSE))
7395 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7396 bfd_set_error (bfd_error_invalid_operation);
7402 use_rela_initialised = TRUE;
7406 else if ((o->size % bed->s->sizeof_rel) == 0)
7408 /* Section size is only divisible by rel. */
7409 if (use_rela_initialised && (use_rela == TRUE))
7412 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7413 bfd_set_error (bfd_error_invalid_operation);
7419 use_rela_initialised = TRUE;
7424 /* The section size is not divisible by either - something is wrong. */
7426 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
7427 bfd_set_error (bfd_error_invalid_operation);
7432 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7433 if (lo->type == bfd_indirect_link_order)
7435 asection *o = lo->u.indirect.section;
7437 if ((o->size % bed->s->sizeof_rela) == 0)
7439 if ((o->size % bed->s->sizeof_rel) == 0)
7440 /* Section size is divisible by both rel and rela sizes.
7441 It is of no help to us. */
7445 /* Section size is only divisible by rela. */
7446 if (use_rela_initialised && (use_rela == FALSE))
7449 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7450 bfd_set_error (bfd_error_invalid_operation);
7456 use_rela_initialised = TRUE;
7460 else if ((o->size % bed->s->sizeof_rel) == 0)
7462 /* Section size is only divisible by rel. */
7463 if (use_rela_initialised && (use_rela == TRUE))
7466 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7467 bfd_set_error (bfd_error_invalid_operation);
7473 use_rela_initialised = TRUE;
7478 /* The section size is not divisible by either - something is wrong. */
7480 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
7481 bfd_set_error (bfd_error_invalid_operation);
7486 if (! use_rela_initialised)
7490 else if (rela_dyn != NULL && rela_dyn->size > 0)
7492 else if (rel_dyn != NULL && rel_dyn->size > 0)
7499 dynamic_relocs = rela_dyn;
7500 ext_size = bed->s->sizeof_rela;
7501 swap_in = bed->s->swap_reloca_in;
7502 swap_out = bed->s->swap_reloca_out;
7506 dynamic_relocs = rel_dyn;
7507 ext_size = bed->s->sizeof_rel;
7508 swap_in = bed->s->swap_reloc_in;
7509 swap_out = bed->s->swap_reloc_out;
7513 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
7514 if (lo->type == bfd_indirect_link_order)
7515 size += lo->u.indirect.section->size;
7517 if (size != dynamic_relocs->size)
7520 sort_elt = (sizeof (struct elf_link_sort_rela)
7521 + (i2e - 1) * sizeof (Elf_Internal_Rela));
7523 count = dynamic_relocs->size / ext_size;
7524 sort = bfd_zmalloc (sort_elt * count);
7528 (*info->callbacks->warning)
7529 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
7533 if (bed->s->arch_size == 32)
7534 r_sym_mask = ~(bfd_vma) 0xff;
7536 r_sym_mask = ~(bfd_vma) 0xffffffff;
7538 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
7539 if (lo->type == bfd_indirect_link_order)
7541 bfd_byte *erel, *erelend;
7542 asection *o = lo->u.indirect.section;
7544 if (o->contents == NULL && o->size != 0)
7546 /* This is a reloc section that is being handled as a normal
7547 section. See bfd_section_from_shdr. We can't combine
7548 relocs in this case. */
7553 erelend = o->contents + o->size;
7554 p = sort + o->output_offset / ext_size * sort_elt;
7556 while (erel < erelend)
7558 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
7560 (*swap_in) (abfd, erel, s->rela);
7561 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
7562 s->u.sym_mask = r_sym_mask;
7568 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
7570 for (i = 0, p = sort; i < count; i++, p += sort_elt)
7572 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
7573 if (s->type != reloc_class_relative)
7579 sq = (struct elf_link_sort_rela *) s_non_relative;
7580 for (; i < count; i++, p += sort_elt)
7582 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
7583 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
7585 sp->u.offset = sq->rela->r_offset;
7588 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
7590 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
7591 if (lo->type == bfd_indirect_link_order)
7593 bfd_byte *erel, *erelend;
7594 asection *o = lo->u.indirect.section;
7597 erelend = o->contents + o->size;
7598 p = sort + o->output_offset / ext_size * sort_elt;
7599 while (erel < erelend)
7601 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
7602 (*swap_out) (abfd, s->rela, erel);
7609 *psec = dynamic_relocs;
7613 /* Flush the output symbols to the file. */
7616 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
7617 const struct elf_backend_data *bed)
7619 if (finfo->symbuf_count > 0)
7621 Elf_Internal_Shdr *hdr;
7625 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
7626 pos = hdr->sh_offset + hdr->sh_size;
7627 amt = finfo->symbuf_count * bed->s->sizeof_sym;
7628 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
7629 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
7632 hdr->sh_size += amt;
7633 finfo->symbuf_count = 0;
7639 /* Add a symbol to the output symbol table. */
7642 elf_link_output_sym (struct elf_final_link_info *finfo,
7644 Elf_Internal_Sym *elfsym,
7645 asection *input_sec,
7646 struct elf_link_hash_entry *h)
7649 Elf_External_Sym_Shndx *destshndx;
7650 bfd_boolean (*output_symbol_hook)
7651 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
7652 struct elf_link_hash_entry *);
7653 const struct elf_backend_data *bed;
7655 bed = get_elf_backend_data (finfo->output_bfd);
7656 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
7657 if (output_symbol_hook != NULL)
7659 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
7663 if (name == NULL || *name == '\0')
7664 elfsym->st_name = 0;
7665 else if (input_sec->flags & SEC_EXCLUDE)
7666 elfsym->st_name = 0;
7669 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
7671 if (elfsym->st_name == (unsigned long) -1)
7675 if (finfo->symbuf_count >= finfo->symbuf_size)
7677 if (! elf_link_flush_output_syms (finfo, bed))
7681 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
7682 destshndx = finfo->symshndxbuf;
7683 if (destshndx != NULL)
7685 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
7689 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
7690 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
7691 if (destshndx == NULL)
7693 memset ((char *) destshndx + amt, 0, amt);
7694 finfo->shndxbuf_size *= 2;
7696 destshndx += bfd_get_symcount (finfo->output_bfd);
7699 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
7700 finfo->symbuf_count += 1;
7701 bfd_get_symcount (finfo->output_bfd) += 1;
7706 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
7709 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
7711 if (sym->st_shndx > SHN_HIRESERVE)
7713 /* The gABI doesn't support dynamic symbols in output sections
7715 (*_bfd_error_handler)
7716 (_("%B: Too many sections: %d (>= %d)"),
7717 abfd, bfd_count_sections (abfd), SHN_LORESERVE);
7718 bfd_set_error (bfd_error_nonrepresentable_section);
7724 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
7725 allowing an unsatisfied unversioned symbol in the DSO to match a
7726 versioned symbol that would normally require an explicit version.
7727 We also handle the case that a DSO references a hidden symbol
7728 which may be satisfied by a versioned symbol in another DSO. */
7731 elf_link_check_versioned_symbol (struct bfd_link_info *info,
7732 const struct elf_backend_data *bed,
7733 struct elf_link_hash_entry *h)
7736 struct elf_link_loaded_list *loaded;
7738 if (!is_elf_hash_table (info->hash))
7741 switch (h->root.type)
7747 case bfd_link_hash_undefined:
7748 case bfd_link_hash_undefweak:
7749 abfd = h->root.u.undef.abfd;
7750 if ((abfd->flags & DYNAMIC) == 0
7751 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
7755 case bfd_link_hash_defined:
7756 case bfd_link_hash_defweak:
7757 abfd = h->root.u.def.section->owner;
7760 case bfd_link_hash_common:
7761 abfd = h->root.u.c.p->section->owner;
7764 BFD_ASSERT (abfd != NULL);
7766 for (loaded = elf_hash_table (info)->loaded;
7768 loaded = loaded->next)
7771 Elf_Internal_Shdr *hdr;
7772 bfd_size_type symcount;
7773 bfd_size_type extsymcount;
7774 bfd_size_type extsymoff;
7775 Elf_Internal_Shdr *versymhdr;
7776 Elf_Internal_Sym *isym;
7777 Elf_Internal_Sym *isymend;
7778 Elf_Internal_Sym *isymbuf;
7779 Elf_External_Versym *ever;
7780 Elf_External_Versym *extversym;
7782 input = loaded->abfd;
7784 /* We check each DSO for a possible hidden versioned definition. */
7786 || (input->flags & DYNAMIC) == 0
7787 || elf_dynversym (input) == 0)
7790 hdr = &elf_tdata (input)->dynsymtab_hdr;
7792 symcount = hdr->sh_size / bed->s->sizeof_sym;
7793 if (elf_bad_symtab (input))
7795 extsymcount = symcount;
7800 extsymcount = symcount - hdr->sh_info;
7801 extsymoff = hdr->sh_info;
7804 if (extsymcount == 0)
7807 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
7809 if (isymbuf == NULL)
7812 /* Read in any version definitions. */
7813 versymhdr = &elf_tdata (input)->dynversym_hdr;
7814 extversym = bfd_malloc (versymhdr->sh_size);
7815 if (extversym == NULL)
7818 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
7819 || (bfd_bread (extversym, versymhdr->sh_size, input)
7820 != versymhdr->sh_size))
7828 ever = extversym + extsymoff;
7829 isymend = isymbuf + extsymcount;
7830 for (isym = isymbuf; isym < isymend; isym++, ever++)
7833 Elf_Internal_Versym iver;
7834 unsigned short version_index;
7836 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
7837 || isym->st_shndx == SHN_UNDEF)
7840 name = bfd_elf_string_from_elf_section (input,
7843 if (strcmp (name, h->root.root.string) != 0)
7846 _bfd_elf_swap_versym_in (input, ever, &iver);
7848 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
7850 /* If we have a non-hidden versioned sym, then it should
7851 have provided a definition for the undefined sym. */
7855 version_index = iver.vs_vers & VERSYM_VERSION;
7856 if (version_index == 1 || version_index == 2)
7858 /* This is the base or first version. We can use it. */
7872 /* Add an external symbol to the symbol table. This is called from
7873 the hash table traversal routine. When generating a shared object,
7874 we go through the symbol table twice. The first time we output
7875 anything that might have been forced to local scope in a version
7876 script. The second time we output the symbols that are still
7880 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
7882 struct elf_outext_info *eoinfo = data;
7883 struct elf_final_link_info *finfo = eoinfo->finfo;
7885 Elf_Internal_Sym sym;
7886 asection *input_sec;
7887 const struct elf_backend_data *bed;
7889 if (h->root.type == bfd_link_hash_warning)
7891 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7892 if (h->root.type == bfd_link_hash_new)
7896 /* Decide whether to output this symbol in this pass. */
7897 if (eoinfo->localsyms)
7899 if (!h->forced_local)
7904 if (h->forced_local)
7908 bed = get_elf_backend_data (finfo->output_bfd);
7910 if (h->root.type == bfd_link_hash_undefined)
7912 /* If we have an undefined symbol reference here then it must have
7913 come from a shared library that is being linked in. (Undefined
7914 references in regular files have already been handled). */
7915 bfd_boolean ignore_undef = FALSE;
7917 /* Some symbols may be special in that the fact that they're
7918 undefined can be safely ignored - let backend determine that. */
7919 if (bed->elf_backend_ignore_undef_symbol)
7920 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
7922 /* If we are reporting errors for this situation then do so now. */
7923 if (ignore_undef == FALSE
7926 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
7927 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
7929 if (! (finfo->info->callbacks->undefined_symbol
7930 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
7931 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
7933 eoinfo->failed = TRUE;
7939 /* We should also warn if a forced local symbol is referenced from
7940 shared libraries. */
7941 if (! finfo->info->relocatable
7942 && (! finfo->info->shared)
7947 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
7949 (*_bfd_error_handler)
7950 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
7952 h->root.u.def.section == bfd_abs_section_ptr
7953 ? finfo->output_bfd : h->root.u.def.section->owner,
7954 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
7956 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
7957 ? "hidden" : "local",
7958 h->root.root.string);
7959 eoinfo->failed = TRUE;
7963 /* We don't want to output symbols that have never been mentioned by
7964 a regular file, or that we have been told to strip. However, if
7965 h->indx is set to -2, the symbol is used by a reloc and we must
7969 else if ((h->def_dynamic
7971 || h->root.type == bfd_link_hash_new)
7975 else if (finfo->info->strip == strip_all)
7977 else if (finfo->info->strip == strip_some
7978 && bfd_hash_lookup (finfo->info->keep_hash,
7979 h->root.root.string, FALSE, FALSE) == NULL)
7981 else if (finfo->info->strip_discarded
7982 && (h->root.type == bfd_link_hash_defined
7983 || h->root.type == bfd_link_hash_defweak)
7984 && elf_discarded_section (h->root.u.def.section))
7989 /* If we're stripping it, and it's not a dynamic symbol, there's
7990 nothing else to do unless it is a forced local symbol. */
7993 && !h->forced_local)
7997 sym.st_size = h->size;
7998 sym.st_other = h->other;
7999 if (h->forced_local)
8000 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8001 else if (h->root.type == bfd_link_hash_undefweak
8002 || h->root.type == bfd_link_hash_defweak)
8003 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8005 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
8007 switch (h->root.type)
8010 case bfd_link_hash_new:
8011 case bfd_link_hash_warning:
8015 case bfd_link_hash_undefined:
8016 case bfd_link_hash_undefweak:
8017 input_sec = bfd_und_section_ptr;
8018 sym.st_shndx = SHN_UNDEF;
8021 case bfd_link_hash_defined:
8022 case bfd_link_hash_defweak:
8024 input_sec = h->root.u.def.section;
8025 if (input_sec->output_section != NULL)
8028 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
8029 input_sec->output_section);
8030 if (sym.st_shndx == SHN_BAD)
8032 (*_bfd_error_handler)
8033 (_("%B: could not find output section %A for input section %A"),
8034 finfo->output_bfd, input_sec->output_section, input_sec);
8035 eoinfo->failed = TRUE;
8039 /* ELF symbols in relocatable files are section relative,
8040 but in nonrelocatable files they are virtual
8042 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8043 if (! finfo->info->relocatable)
8045 sym.st_value += input_sec->output_section->vma;
8046 if (h->type == STT_TLS)
8048 /* STT_TLS symbols are relative to PT_TLS segment
8050 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
8051 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
8057 BFD_ASSERT (input_sec->owner == NULL
8058 || (input_sec->owner->flags & DYNAMIC) != 0);
8059 sym.st_shndx = SHN_UNDEF;
8060 input_sec = bfd_und_section_ptr;
8065 case bfd_link_hash_common:
8066 input_sec = h->root.u.c.p->section;
8067 sym.st_shndx = bed->common_section_index (input_sec);
8068 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8071 case bfd_link_hash_indirect:
8072 /* These symbols are created by symbol versioning. They point
8073 to the decorated version of the name. For example, if the
8074 symbol foo@@GNU_1.2 is the default, which should be used when
8075 foo is used with no version, then we add an indirect symbol
8076 foo which points to foo@@GNU_1.2. We ignore these symbols,
8077 since the indirected symbol is already in the hash table. */
8081 /* Give the processor backend a chance to tweak the symbol value,
8082 and also to finish up anything that needs to be done for this
8083 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8084 forced local syms when non-shared is due to a historical quirk. */
8085 if ((h->dynindx != -1
8087 && ((finfo->info->shared
8088 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8089 || h->root.type != bfd_link_hash_undefweak))
8090 || !h->forced_local)
8091 && elf_hash_table (finfo->info)->dynamic_sections_created)
8093 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8094 (finfo->output_bfd, finfo->info, h, &sym)))
8096 eoinfo->failed = TRUE;
8101 /* If we are marking the symbol as undefined, and there are no
8102 non-weak references to this symbol from a regular object, then
8103 mark the symbol as weak undefined; if there are non-weak
8104 references, mark the symbol as strong. We can't do this earlier,
8105 because it might not be marked as undefined until the
8106 finish_dynamic_symbol routine gets through with it. */
8107 if (sym.st_shndx == SHN_UNDEF
8109 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8110 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8114 if (h->ref_regular_nonweak)
8115 bindtype = STB_GLOBAL;
8117 bindtype = STB_WEAK;
8118 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
8121 /* If a non-weak symbol with non-default visibility is not defined
8122 locally, it is a fatal error. */
8123 if (! finfo->info->relocatable
8124 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8125 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8126 && h->root.type == bfd_link_hash_undefined
8129 (*_bfd_error_handler)
8130 (_("%B: %s symbol `%s' isn't defined"),
8132 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
8134 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
8135 ? "internal" : "hidden",
8136 h->root.root.string);
8137 eoinfo->failed = TRUE;
8141 /* If this symbol should be put in the .dynsym section, then put it
8142 there now. We already know the symbol index. We also fill in
8143 the entry in the .hash section. */
8144 if (h->dynindx != -1
8145 && elf_hash_table (finfo->info)->dynamic_sections_created)
8149 sym.st_name = h->dynstr_index;
8150 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
8151 if (! check_dynsym (finfo->output_bfd, &sym))
8153 eoinfo->failed = TRUE;
8156 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8158 if (finfo->hash_sec != NULL)
8160 size_t hash_entry_size;
8161 bfd_byte *bucketpos;
8166 bucketcount = elf_hash_table (finfo->info)->bucketcount;
8167 bucket = h->u.elf_hash_value % bucketcount;
8170 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8171 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8172 + (bucket + 2) * hash_entry_size);
8173 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8174 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8175 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8176 ((bfd_byte *) finfo->hash_sec->contents
8177 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8180 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8182 Elf_Internal_Versym iversym;
8183 Elf_External_Versym *eversym;
8185 if (!h->def_regular)
8187 if (h->verinfo.verdef == NULL)
8188 iversym.vs_vers = 0;
8190 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8194 if (h->verinfo.vertree == NULL)
8195 iversym.vs_vers = 1;
8197 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8198 if (finfo->info->create_default_symver)
8203 iversym.vs_vers |= VERSYM_HIDDEN;
8205 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8206 eversym += h->dynindx;
8207 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8211 /* If we're stripping it, then it was just a dynamic symbol, and
8212 there's nothing else to do. */
8213 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8216 h->indx = bfd_get_symcount (finfo->output_bfd);
8218 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
8220 eoinfo->failed = TRUE;
8227 /* Return TRUE if special handling is done for relocs in SEC against
8228 symbols defined in discarded sections. */
8231 elf_section_ignore_discarded_relocs (asection *sec)
8233 const struct elf_backend_data *bed;
8235 switch (sec->sec_info_type)
8237 case ELF_INFO_TYPE_STABS:
8238 case ELF_INFO_TYPE_EH_FRAME:
8244 bed = get_elf_backend_data (sec->owner);
8245 if (bed->elf_backend_ignore_discarded_relocs != NULL
8246 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8252 /* Return a mask saying how ld should treat relocations in SEC against
8253 symbols defined in discarded sections. If this function returns
8254 COMPLAIN set, ld will issue a warning message. If this function
8255 returns PRETEND set, and the discarded section was link-once and the
8256 same size as the kept link-once section, ld will pretend that the
8257 symbol was actually defined in the kept section. Otherwise ld will
8258 zero the reloc (at least that is the intent, but some cooperation by
8259 the target dependent code is needed, particularly for REL targets). */
8262 _bfd_elf_default_action_discarded (asection *sec)
8264 if (sec->flags & SEC_DEBUGGING)
8267 if (strcmp (".eh_frame", sec->name) == 0)
8270 if (strcmp (".gcc_except_table", sec->name) == 0)
8273 return COMPLAIN | PRETEND;
8276 /* Find a match between a section and a member of a section group. */
8279 match_group_member (asection *sec, asection *group,
8280 struct bfd_link_info *info)
8282 asection *first = elf_next_in_group (group);
8283 asection *s = first;
8287 if (bfd_elf_match_symbols_in_sections (s, sec, info))
8290 s = elf_next_in_group (s);
8298 /* Check if the kept section of a discarded section SEC can be used
8299 to replace it. Return the replacement if it is OK. Otherwise return
8303 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
8307 kept = sec->kept_section;
8310 if ((kept->flags & SEC_GROUP) != 0)
8311 kept = match_group_member (sec, kept, info);
8312 if (kept != NULL && sec->size != kept->size)
8314 sec->kept_section = kept;
8319 /* Link an input file into the linker output file. This function
8320 handles all the sections and relocations of the input file at once.
8321 This is so that we only have to read the local symbols once, and
8322 don't have to keep them in memory. */
8325 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
8327 int (*relocate_section)
8328 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
8329 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
8331 Elf_Internal_Shdr *symtab_hdr;
8334 Elf_Internal_Sym *isymbuf;
8335 Elf_Internal_Sym *isym;
8336 Elf_Internal_Sym *isymend;
8338 asection **ppsection;
8340 const struct elf_backend_data *bed;
8341 struct elf_link_hash_entry **sym_hashes;
8343 output_bfd = finfo->output_bfd;
8344 bed = get_elf_backend_data (output_bfd);
8345 relocate_section = bed->elf_backend_relocate_section;
8347 /* If this is a dynamic object, we don't want to do anything here:
8348 we don't want the local symbols, and we don't want the section
8350 if ((input_bfd->flags & DYNAMIC) != 0)
8353 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8354 if (elf_bad_symtab (input_bfd))
8356 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8361 locsymcount = symtab_hdr->sh_info;
8362 extsymoff = symtab_hdr->sh_info;
8365 /* Read the local symbols. */
8366 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8367 if (isymbuf == NULL && locsymcount != 0)
8369 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8370 finfo->internal_syms,
8371 finfo->external_syms,
8372 finfo->locsym_shndx);
8373 if (isymbuf == NULL)
8376 /* evaluate_complex_relocation_symbols looks for symbols in
8377 finfo->internal_syms. */
8378 else if (isymbuf != NULL && locsymcount != 0)
8380 bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8381 finfo->internal_syms,
8382 finfo->external_syms,
8383 finfo->locsym_shndx);
8386 /* Find local symbol sections and adjust values of symbols in
8387 SEC_MERGE sections. Write out those local symbols we know are
8388 going into the output file. */
8389 isymend = isymbuf + locsymcount;
8390 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
8392 isym++, pindex++, ppsection++)
8396 Elf_Internal_Sym osym;
8400 if (elf_bad_symtab (input_bfd))
8402 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
8409 if (isym->st_shndx == SHN_UNDEF)
8410 isec = bfd_und_section_ptr;
8411 else if (isym->st_shndx < SHN_LORESERVE
8412 || isym->st_shndx > SHN_HIRESERVE)
8414 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
8416 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
8417 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
8419 _bfd_merged_section_offset (output_bfd, &isec,
8420 elf_section_data (isec)->sec_info,
8423 else if (isym->st_shndx == SHN_ABS)
8424 isec = bfd_abs_section_ptr;
8425 else if (isym->st_shndx == SHN_COMMON)
8426 isec = bfd_com_section_ptr;
8429 /* Don't attempt to output symbols with st_shnx in the
8430 reserved range other than SHN_ABS and SHN_COMMON. */
8437 /* Don't output the first, undefined, symbol. */
8438 if (ppsection == finfo->sections)
8441 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
8443 /* We never output section symbols. Instead, we use the
8444 section symbol of the corresponding section in the output
8449 /* If we are stripping all symbols, we don't want to output this
8451 if (finfo->info->strip == strip_all)
8454 /* If we are discarding all local symbols, we don't want to
8455 output this one. If we are generating a relocatable output
8456 file, then some of the local symbols may be required by
8457 relocs; we output them below as we discover that they are
8459 if (finfo->info->discard == discard_all)
8462 /* If this symbol is defined in a section which we are
8463 discarding, we don't need to keep it. */
8464 if (isym->st_shndx != SHN_UNDEF
8465 && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
8467 || bfd_section_removed_from_list (output_bfd,
8468 isec->output_section)))
8471 /* Get the name of the symbol. */
8472 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
8477 /* See if we are discarding symbols with this name. */
8478 if ((finfo->info->strip == strip_some
8479 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
8481 || (((finfo->info->discard == discard_sec_merge
8482 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
8483 || finfo->info->discard == discard_l)
8484 && bfd_is_local_label_name (input_bfd, name)))
8487 /* If we get here, we are going to output this symbol. */
8491 /* Adjust the section index for the output file. */
8492 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
8493 isec->output_section);
8494 if (osym.st_shndx == SHN_BAD)
8497 *pindex = bfd_get_symcount (output_bfd);
8499 /* ELF symbols in relocatable files are section relative, but
8500 in executable files they are virtual addresses. Note that
8501 this code assumes that all ELF sections have an associated
8502 BFD section with a reasonable value for output_offset; below
8503 we assume that they also have a reasonable value for
8504 output_section. Any special sections must be set up to meet
8505 these requirements. */
8506 osym.st_value += isec->output_offset;
8507 if (! finfo->info->relocatable)
8509 osym.st_value += isec->output_section->vma;
8510 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
8512 /* STT_TLS symbols are relative to PT_TLS segment base. */
8513 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
8514 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
8518 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
8522 if (! evaluate_complex_relocation_symbols (input_bfd, finfo, locsymcount))
8525 /* Relocate the contents of each section. */
8526 sym_hashes = elf_sym_hashes (input_bfd);
8527 for (o = input_bfd->sections; o != NULL; o = o->next)
8531 if (! o->linker_mark)
8533 /* This section was omitted from the link. */
8537 if ((o->flags & SEC_HAS_CONTENTS) == 0
8538 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
8541 if ((o->flags & SEC_LINKER_CREATED) != 0)
8543 /* Section was created by _bfd_elf_link_create_dynamic_sections
8548 /* Get the contents of the section. They have been cached by a
8549 relaxation routine. Note that o is a section in an input
8550 file, so the contents field will not have been set by any of
8551 the routines which work on output files. */
8552 if (elf_section_data (o)->this_hdr.contents != NULL)
8553 contents = elf_section_data (o)->this_hdr.contents;
8556 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
8558 contents = finfo->contents;
8559 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
8563 if ((o->flags & SEC_RELOC) != 0)
8565 Elf_Internal_Rela *internal_relocs;
8566 bfd_vma r_type_mask;
8570 /* Get the swapped relocs. */
8572 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
8573 finfo->internal_relocs, FALSE);
8574 if (internal_relocs == NULL
8575 && o->reloc_count > 0)
8578 if (bed->s->arch_size == 32)
8585 r_type_mask = 0xffffffff;
8589 /* Run through the relocs looking for any against symbols
8590 from discarded sections and section symbols from
8591 removed link-once sections. Complain about relocs
8592 against discarded sections. Zero relocs against removed
8593 link-once sections. */
8594 if (!elf_section_ignore_discarded_relocs (o))
8596 Elf_Internal_Rela *rel, *relend;
8597 unsigned int action = (*bed->action_discarded) (o);
8599 rel = internal_relocs;
8600 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
8601 for ( ; rel < relend; rel++)
8603 unsigned long r_symndx = rel->r_info >> r_sym_shift;
8604 asection **ps, *sec;
8605 struct elf_link_hash_entry *h = NULL;
8606 const char *sym_name;
8608 if (r_symndx == STN_UNDEF)
8611 if (r_symndx >= locsymcount
8612 || (elf_bad_symtab (input_bfd)
8613 && finfo->sections[r_symndx] == NULL))
8615 h = sym_hashes[r_symndx - extsymoff];
8617 /* Badly formatted input files can contain relocs that
8618 reference non-existant symbols. Check here so that
8619 we do not seg fault. */
8624 sprintf_vma (buffer, rel->r_info);
8625 (*_bfd_error_handler)
8626 (_("error: %B contains a reloc (0x%s) for section %A "
8627 "that references a non-existent global symbol"),
8628 input_bfd, o, buffer);
8629 bfd_set_error (bfd_error_bad_value);
8633 while (h->root.type == bfd_link_hash_indirect
8634 || h->root.type == bfd_link_hash_warning)
8635 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8637 if (h->root.type != bfd_link_hash_defined
8638 && h->root.type != bfd_link_hash_defweak)
8641 ps = &h->root.u.def.section;
8642 sym_name = h->root.root.string;
8646 Elf_Internal_Sym *sym = isymbuf + r_symndx;
8647 ps = &finfo->sections[r_symndx];
8648 sym_name = bfd_elf_sym_name (input_bfd,
8653 /* Complain if the definition comes from a
8654 discarded section. */
8655 if ((sec = *ps) != NULL && elf_discarded_section (sec))
8657 BFD_ASSERT (r_symndx != 0);
8658 if (action & COMPLAIN)
8659 (*finfo->info->callbacks->einfo)
8660 (_("%X`%s' referenced in section `%A' of %B: "
8661 "defined in discarded section `%A' of %B\n"),
8662 sym_name, o, input_bfd, sec, sec->owner);
8664 /* Try to do the best we can to support buggy old
8665 versions of gcc. Pretend that the symbol is
8666 really defined in the kept linkonce section.
8667 FIXME: This is quite broken. Modifying the
8668 symbol here means we will be changing all later
8669 uses of the symbol, not just in this section. */
8670 if (action & PRETEND)
8674 kept = _bfd_elf_check_kept_section (sec,
8686 /* Relocate the section by invoking a back end routine.
8688 The back end routine is responsible for adjusting the
8689 section contents as necessary, and (if using Rela relocs
8690 and generating a relocatable output file) adjusting the
8691 reloc addend as necessary.
8693 The back end routine does not have to worry about setting
8694 the reloc address or the reloc symbol index.
8696 The back end routine is given a pointer to the swapped in
8697 internal symbols, and can access the hash table entries
8698 for the external symbols via elf_sym_hashes (input_bfd).
8700 When generating relocatable output, the back end routine
8701 must handle STB_LOCAL/STT_SECTION symbols specially. The
8702 output symbol is going to be a section symbol
8703 corresponding to the output section, which will require
8704 the addend to be adjusted. */
8706 ret = (*relocate_section) (output_bfd, finfo->info,
8707 input_bfd, o, contents,
8715 || finfo->info->relocatable
8716 || finfo->info->emitrelocations)
8718 Elf_Internal_Rela *irela;
8719 Elf_Internal_Rela *irelaend;
8720 bfd_vma last_offset;
8721 struct elf_link_hash_entry **rel_hash;
8722 struct elf_link_hash_entry **rel_hash_list;
8723 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
8724 unsigned int next_erel;
8725 bfd_boolean rela_normal;
8727 input_rel_hdr = &elf_section_data (o)->rel_hdr;
8728 rela_normal = (bed->rela_normal
8729 && (input_rel_hdr->sh_entsize
8730 == bed->s->sizeof_rela));
8732 /* Adjust the reloc addresses and symbol indices. */
8734 irela = internal_relocs;
8735 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
8736 rel_hash = (elf_section_data (o->output_section)->rel_hashes
8737 + elf_section_data (o->output_section)->rel_count
8738 + elf_section_data (o->output_section)->rel_count2);
8739 rel_hash_list = rel_hash;
8740 last_offset = o->output_offset;
8741 if (!finfo->info->relocatable)
8742 last_offset += o->output_section->vma;
8743 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
8745 unsigned long r_symndx;
8747 Elf_Internal_Sym sym;
8749 if (next_erel == bed->s->int_rels_per_ext_rel)
8755 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8758 if (irela->r_offset >= (bfd_vma) -2)
8760 /* This is a reloc for a deleted entry or somesuch.
8761 Turn it into an R_*_NONE reloc, at the same
8762 offset as the last reloc. elf_eh_frame.c and
8763 bfd_elf_discard_info rely on reloc offsets
8765 irela->r_offset = last_offset;
8767 irela->r_addend = 0;
8771 irela->r_offset += o->output_offset;
8773 /* Relocs in an executable have to be virtual addresses. */
8774 if (!finfo->info->relocatable)
8775 irela->r_offset += o->output_section->vma;
8777 last_offset = irela->r_offset;
8779 r_symndx = irela->r_info >> r_sym_shift;
8780 if (r_symndx == STN_UNDEF)
8783 if (r_symndx >= locsymcount
8784 || (elf_bad_symtab (input_bfd)
8785 && finfo->sections[r_symndx] == NULL))
8787 struct elf_link_hash_entry *rh;
8790 /* This is a reloc against a global symbol. We
8791 have not yet output all the local symbols, so
8792 we do not know the symbol index of any global
8793 symbol. We set the rel_hash entry for this
8794 reloc to point to the global hash table entry
8795 for this symbol. The symbol index is then
8796 set at the end of bfd_elf_final_link. */
8797 indx = r_symndx - extsymoff;
8798 rh = elf_sym_hashes (input_bfd)[indx];
8799 while (rh->root.type == bfd_link_hash_indirect
8800 || rh->root.type == bfd_link_hash_warning)
8801 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
8803 /* Setting the index to -2 tells
8804 elf_link_output_extsym that this symbol is
8806 BFD_ASSERT (rh->indx < 0);
8814 /* This is a reloc against a local symbol. */
8817 sym = isymbuf[r_symndx];
8818 sec = finfo->sections[r_symndx];
8819 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
8821 /* I suppose the backend ought to fill in the
8822 section of any STT_SECTION symbol against a
8823 processor specific section. */
8825 if (bfd_is_abs_section (sec))
8827 else if (sec == NULL || sec->owner == NULL)
8829 bfd_set_error (bfd_error_bad_value);
8834 asection *osec = sec->output_section;
8836 /* If we have discarded a section, the output
8837 section will be the absolute section. In
8838 case of discarded SEC_MERGE sections, use
8839 the kept section. relocate_section should
8840 have already handled discarded linkonce
8842 if (bfd_is_abs_section (osec)
8843 && sec->kept_section != NULL
8844 && sec->kept_section->output_section != NULL)
8846 osec = sec->kept_section->output_section;
8847 irela->r_addend -= osec->vma;
8850 if (!bfd_is_abs_section (osec))
8852 r_symndx = osec->target_index;
8855 struct elf_link_hash_table *htab;
8858 htab = elf_hash_table (finfo->info);
8859 oi = htab->text_index_section;
8860 if ((osec->flags & SEC_READONLY) == 0
8861 && htab->data_index_section != NULL)
8862 oi = htab->data_index_section;
8866 irela->r_addend += osec->vma - oi->vma;
8867 r_symndx = oi->target_index;
8871 BFD_ASSERT (r_symndx != 0);
8875 /* Adjust the addend according to where the
8876 section winds up in the output section. */
8878 irela->r_addend += sec->output_offset;
8882 if (finfo->indices[r_symndx] == -1)
8884 unsigned long shlink;
8888 if (finfo->info->strip == strip_all)
8890 /* You can't do ld -r -s. */
8891 bfd_set_error (bfd_error_invalid_operation);
8895 /* This symbol was skipped earlier, but
8896 since it is needed by a reloc, we
8897 must output it now. */
8898 shlink = symtab_hdr->sh_link;
8899 name = (bfd_elf_string_from_elf_section
8900 (input_bfd, shlink, sym.st_name));
8904 osec = sec->output_section;
8906 _bfd_elf_section_from_bfd_section (output_bfd,
8908 if (sym.st_shndx == SHN_BAD)
8911 sym.st_value += sec->output_offset;
8912 if (! finfo->info->relocatable)
8914 sym.st_value += osec->vma;
8915 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
8917 /* STT_TLS symbols are relative to PT_TLS
8919 BFD_ASSERT (elf_hash_table (finfo->info)
8921 sym.st_value -= (elf_hash_table (finfo->info)
8926 finfo->indices[r_symndx]
8927 = bfd_get_symcount (output_bfd);
8929 if (! elf_link_output_sym (finfo, name, &sym, sec,
8934 r_symndx = finfo->indices[r_symndx];
8937 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
8938 | (irela->r_info & r_type_mask));
8941 /* Swap out the relocs. */
8942 if (input_rel_hdr->sh_size != 0
8943 && !bed->elf_backend_emit_relocs (output_bfd, o,
8949 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
8950 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
8952 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
8953 * bed->s->int_rels_per_ext_rel);
8954 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
8955 if (!bed->elf_backend_emit_relocs (output_bfd, o,
8964 /* Write out the modified section contents. */
8965 if (bed->elf_backend_write_section
8966 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
8969 /* Section written out. */
8971 else switch (o->sec_info_type)
8973 case ELF_INFO_TYPE_STABS:
8974 if (! (_bfd_write_section_stabs
8976 &elf_hash_table (finfo->info)->stab_info,
8977 o, &elf_section_data (o)->sec_info, contents)))
8980 case ELF_INFO_TYPE_MERGE:
8981 if (! _bfd_write_merged_section (output_bfd, o,
8982 elf_section_data (o)->sec_info))
8985 case ELF_INFO_TYPE_EH_FRAME:
8987 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
8994 if (! (o->flags & SEC_EXCLUDE)
8995 && ! bfd_set_section_contents (output_bfd, o->output_section,
8997 (file_ptr) o->output_offset,
9008 /* Generate a reloc when linking an ELF file. This is a reloc
9009 requested by the linker, and does not come from any input file. This
9010 is used to build constructor and destructor tables when linking
9014 elf_reloc_link_order (bfd *output_bfd,
9015 struct bfd_link_info *info,
9016 asection *output_section,
9017 struct bfd_link_order *link_order)
9019 reloc_howto_type *howto;
9023 struct elf_link_hash_entry **rel_hash_ptr;
9024 Elf_Internal_Shdr *rel_hdr;
9025 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9026 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9030 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9033 bfd_set_error (bfd_error_bad_value);
9037 addend = link_order->u.reloc.p->addend;
9039 /* Figure out the symbol index. */
9040 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
9041 + elf_section_data (output_section)->rel_count
9042 + elf_section_data (output_section)->rel_count2);
9043 if (link_order->type == bfd_section_reloc_link_order)
9045 indx = link_order->u.reloc.p->u.section->target_index;
9046 BFD_ASSERT (indx != 0);
9047 *rel_hash_ptr = NULL;
9051 struct elf_link_hash_entry *h;
9053 /* Treat a reloc against a defined symbol as though it were
9054 actually against the section. */
9055 h = ((struct elf_link_hash_entry *)
9056 bfd_wrapped_link_hash_lookup (output_bfd, info,
9057 link_order->u.reloc.p->u.name,
9058 FALSE, FALSE, TRUE));
9060 && (h->root.type == bfd_link_hash_defined
9061 || h->root.type == bfd_link_hash_defweak))
9065 section = h->root.u.def.section;
9066 indx = section->output_section->target_index;
9067 *rel_hash_ptr = NULL;
9068 /* It seems that we ought to add the symbol value to the
9069 addend here, but in practice it has already been added
9070 because it was passed to constructor_callback. */
9071 addend += section->output_section->vma + section->output_offset;
9075 /* Setting the index to -2 tells elf_link_output_extsym that
9076 this symbol is used by a reloc. */
9083 if (! ((*info->callbacks->unattached_reloc)
9084 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
9090 /* If this is an inplace reloc, we must write the addend into the
9092 if (howto->partial_inplace && addend != 0)
9095 bfd_reloc_status_type rstat;
9098 const char *sym_name;
9100 size = bfd_get_reloc_size (howto);
9101 buf = bfd_zmalloc (size);
9104 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
9111 case bfd_reloc_outofrange:
9114 case bfd_reloc_overflow:
9115 if (link_order->type == bfd_section_reloc_link_order)
9116 sym_name = bfd_section_name (output_bfd,
9117 link_order->u.reloc.p->u.section);
9119 sym_name = link_order->u.reloc.p->u.name;
9120 if (! ((*info->callbacks->reloc_overflow)
9121 (info, NULL, sym_name, howto->name, addend, NULL,
9122 NULL, (bfd_vma) 0)))
9129 ok = bfd_set_section_contents (output_bfd, output_section, buf,
9130 link_order->offset, size);
9136 /* The address of a reloc is relative to the section in a
9137 relocatable file, and is a virtual address in an executable
9139 offset = link_order->offset;
9140 if (! info->relocatable)
9141 offset += output_section->vma;
9143 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
9145 irel[i].r_offset = offset;
9147 irel[i].r_addend = 0;
9149 if (bed->s->arch_size == 32)
9150 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
9152 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
9154 rel_hdr = &elf_section_data (output_section)->rel_hdr;
9155 erel = rel_hdr->contents;
9156 if (rel_hdr->sh_type == SHT_REL)
9158 erel += (elf_section_data (output_section)->rel_count
9159 * bed->s->sizeof_rel);
9160 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
9164 irel[0].r_addend = addend;
9165 erel += (elf_section_data (output_section)->rel_count
9166 * bed->s->sizeof_rela);
9167 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
9170 ++elf_section_data (output_section)->rel_count;
9176 /* Get the output vma of the section pointed to by the sh_link field. */
9179 elf_get_linked_section_vma (struct bfd_link_order *p)
9181 Elf_Internal_Shdr **elf_shdrp;
9185 s = p->u.indirect.section;
9186 elf_shdrp = elf_elfsections (s->owner);
9187 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
9188 elfsec = elf_shdrp[elfsec]->sh_link;
9190 The Intel C compiler generates SHT_IA_64_UNWIND with
9191 SHF_LINK_ORDER. But it doesn't set the sh_link or
9192 sh_info fields. Hence we could get the situation
9193 where elfsec is 0. */
9196 const struct elf_backend_data *bed
9197 = get_elf_backend_data (s->owner);
9198 if (bed->link_order_error_handler)
9199 bed->link_order_error_handler
9200 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
9205 s = elf_shdrp[elfsec]->bfd_section;
9206 return s->output_section->vma + s->output_offset;
9211 /* Compare two sections based on the locations of the sections they are
9212 linked to. Used by elf_fixup_link_order. */
9215 compare_link_order (const void * a, const void * b)
9220 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
9221 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
9228 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9229 order as their linked sections. Returns false if this could not be done
9230 because an output section includes both ordered and unordered
9231 sections. Ideally we'd do this in the linker proper. */
9234 elf_fixup_link_order (bfd *abfd, asection *o)
9239 struct bfd_link_order *p;
9241 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9243 struct bfd_link_order **sections;
9244 asection *s, *other_sec, *linkorder_sec;
9248 linkorder_sec = NULL;
9251 for (p = o->map_head.link_order; p != NULL; p = p->next)
9253 if (p->type == bfd_indirect_link_order)
9255 s = p->u.indirect.section;
9257 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
9258 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
9259 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
9260 && elfsec < elf_numsections (sub)
9261 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
9275 if (seen_other && seen_linkorder)
9277 if (other_sec && linkorder_sec)
9278 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9280 linkorder_sec->owner, other_sec,
9283 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
9285 bfd_set_error (bfd_error_bad_value);
9290 if (!seen_linkorder)
9293 sections = (struct bfd_link_order **)
9294 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
9297 for (p = o->map_head.link_order; p != NULL; p = p->next)
9299 sections[seen_linkorder++] = p;
9301 /* Sort the input sections in the order of their linked section. */
9302 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
9303 compare_link_order);
9305 /* Change the offsets of the sections. */
9307 for (n = 0; n < seen_linkorder; n++)
9309 s = sections[n]->u.indirect.section;
9310 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
9311 s->output_offset = offset;
9312 sections[n]->offset = offset;
9313 offset += sections[n]->size;
9320 /* Do the final step of an ELF link. */
9323 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
9325 bfd_boolean dynamic;
9326 bfd_boolean emit_relocs;
9328 struct elf_final_link_info finfo;
9329 register asection *o;
9330 register struct bfd_link_order *p;
9332 bfd_size_type max_contents_size;
9333 bfd_size_type max_external_reloc_size;
9334 bfd_size_type max_internal_reloc_count;
9335 bfd_size_type max_sym_count;
9336 bfd_size_type max_sym_shndx_count;
9338 Elf_Internal_Sym elfsym;
9340 Elf_Internal_Shdr *symtab_hdr;
9341 Elf_Internal_Shdr *symtab_shndx_hdr;
9342 Elf_Internal_Shdr *symstrtab_hdr;
9343 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9344 struct elf_outext_info eoinfo;
9346 size_t relativecount = 0;
9347 asection *reldyn = 0;
9349 asection *attr_section = NULL;
9350 bfd_vma attr_size = 0;
9351 const char *std_attrs_section;
9353 if (! is_elf_hash_table (info->hash))
9357 abfd->flags |= DYNAMIC;
9359 dynamic = elf_hash_table (info)->dynamic_sections_created;
9360 dynobj = elf_hash_table (info)->dynobj;
9362 emit_relocs = (info->relocatable
9363 || info->emitrelocations);
9366 finfo.output_bfd = abfd;
9367 finfo.symstrtab = _bfd_elf_stringtab_init ();
9368 if (finfo.symstrtab == NULL)
9373 finfo.dynsym_sec = NULL;
9374 finfo.hash_sec = NULL;
9375 finfo.symver_sec = NULL;
9379 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
9380 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
9381 BFD_ASSERT (finfo.dynsym_sec != NULL);
9382 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
9383 /* Note that it is OK if symver_sec is NULL. */
9386 finfo.contents = NULL;
9387 finfo.external_relocs = NULL;
9388 finfo.internal_relocs = NULL;
9389 finfo.external_syms = NULL;
9390 finfo.locsym_shndx = NULL;
9391 finfo.internal_syms = NULL;
9392 finfo.indices = NULL;
9393 finfo.sections = NULL;
9394 finfo.symbuf = NULL;
9395 finfo.symshndxbuf = NULL;
9396 finfo.symbuf_count = 0;
9397 finfo.shndxbuf_size = 0;
9399 /* The object attributes have been merged. Remove the input
9400 sections from the link, and set the contents of the output
9402 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
9403 for (o = abfd->sections; o != NULL; o = o->next)
9405 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
9406 || strcmp (o->name, ".gnu.attributes") == 0)
9408 for (p = o->map_head.link_order; p != NULL; p = p->next)
9410 asection *input_section;
9412 if (p->type != bfd_indirect_link_order)
9414 input_section = p->u.indirect.section;
9415 /* Hack: reset the SEC_HAS_CONTENTS flag so that
9416 elf_link_input_bfd ignores this section. */
9417 input_section->flags &= ~SEC_HAS_CONTENTS;
9420 attr_size = bfd_elf_obj_attr_size (abfd);
9423 bfd_set_section_size (abfd, o, attr_size);
9425 /* Skip this section later on. */
9426 o->map_head.link_order = NULL;
9429 o->flags |= SEC_EXCLUDE;
9433 /* Count up the number of relocations we will output for each output
9434 section, so that we know the sizes of the reloc sections. We
9435 also figure out some maximum sizes. */
9436 max_contents_size = 0;
9437 max_external_reloc_size = 0;
9438 max_internal_reloc_count = 0;
9440 max_sym_shndx_count = 0;
9442 for (o = abfd->sections; o != NULL; o = o->next)
9444 struct bfd_elf_section_data *esdo = elf_section_data (o);
9447 for (p = o->map_head.link_order; p != NULL; p = p->next)
9449 unsigned int reloc_count = 0;
9450 struct bfd_elf_section_data *esdi = NULL;
9451 unsigned int *rel_count1;
9453 if (p->type == bfd_section_reloc_link_order
9454 || p->type == bfd_symbol_reloc_link_order)
9456 else if (p->type == bfd_indirect_link_order)
9460 sec = p->u.indirect.section;
9461 esdi = elf_section_data (sec);
9463 /* Mark all sections which are to be included in the
9464 link. This will normally be every section. We need
9465 to do this so that we can identify any sections which
9466 the linker has decided to not include. */
9467 sec->linker_mark = TRUE;
9469 if (sec->flags & SEC_MERGE)
9472 if (info->relocatable || info->emitrelocations)
9473 reloc_count = sec->reloc_count;
9474 else if (bed->elf_backend_count_relocs)
9476 Elf_Internal_Rela * relocs;
9478 relocs = _bfd_elf_link_read_relocs (sec->owner, sec,
9485 = (*bed->elf_backend_count_relocs) (sec, relocs);
9487 if (elf_section_data (sec)->relocs != relocs)
9492 if (sec->rawsize > max_contents_size)
9493 max_contents_size = sec->rawsize;
9494 if (sec->size > max_contents_size)
9495 max_contents_size = sec->size;
9497 /* We are interested in just local symbols, not all
9499 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
9500 && (sec->owner->flags & DYNAMIC) == 0)
9504 if (elf_bad_symtab (sec->owner))
9505 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
9506 / bed->s->sizeof_sym);
9508 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
9510 if (sym_count > max_sym_count)
9511 max_sym_count = sym_count;
9513 if (sym_count > max_sym_shndx_count
9514 && elf_symtab_shndx (sec->owner) != 0)
9515 max_sym_shndx_count = sym_count;
9517 if ((sec->flags & SEC_RELOC) != 0)
9521 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
9522 if (ext_size > max_external_reloc_size)
9523 max_external_reloc_size = ext_size;
9524 if (sec->reloc_count > max_internal_reloc_count)
9525 max_internal_reloc_count = sec->reloc_count;
9530 if (reloc_count == 0)
9533 o->reloc_count += reloc_count;
9535 /* MIPS may have a mix of REL and RELA relocs on sections.
9536 To support this curious ABI we keep reloc counts in
9537 elf_section_data too. We must be careful to add the
9538 relocations from the input section to the right output
9539 count. FIXME: Get rid of one count. We have
9540 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
9541 rel_count1 = &esdo->rel_count;
9544 bfd_boolean same_size;
9545 bfd_size_type entsize1;
9547 entsize1 = esdi->rel_hdr.sh_entsize;
9548 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
9549 || entsize1 == bed->s->sizeof_rela);
9550 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
9553 rel_count1 = &esdo->rel_count2;
9555 if (esdi->rel_hdr2 != NULL)
9557 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
9558 unsigned int alt_count;
9559 unsigned int *rel_count2;
9561 BFD_ASSERT (entsize2 != entsize1
9562 && (entsize2 == bed->s->sizeof_rel
9563 || entsize2 == bed->s->sizeof_rela));
9565 rel_count2 = &esdo->rel_count2;
9567 rel_count2 = &esdo->rel_count;
9569 /* The following is probably too simplistic if the
9570 backend counts output relocs unusually. */
9571 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
9572 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
9573 *rel_count2 += alt_count;
9574 reloc_count -= alt_count;
9577 *rel_count1 += reloc_count;
9580 if (o->reloc_count > 0)
9581 o->flags |= SEC_RELOC;
9584 /* Explicitly clear the SEC_RELOC flag. The linker tends to
9585 set it (this is probably a bug) and if it is set
9586 assign_section_numbers will create a reloc section. */
9587 o->flags &=~ SEC_RELOC;
9590 /* If the SEC_ALLOC flag is not set, force the section VMA to
9591 zero. This is done in elf_fake_sections as well, but forcing
9592 the VMA to 0 here will ensure that relocs against these
9593 sections are handled correctly. */
9594 if ((o->flags & SEC_ALLOC) == 0
9595 && ! o->user_set_vma)
9599 if (! info->relocatable && merged)
9600 elf_link_hash_traverse (elf_hash_table (info),
9601 _bfd_elf_link_sec_merge_syms, abfd);
9603 /* Figure out the file positions for everything but the symbol table
9604 and the relocs. We set symcount to force assign_section_numbers
9605 to create a symbol table. */
9606 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
9607 BFD_ASSERT (! abfd->output_has_begun);
9608 if (! _bfd_elf_compute_section_file_positions (abfd, info))
9611 /* Set sizes, and assign file positions for reloc sections. */
9612 for (o = abfd->sections; o != NULL; o = o->next)
9614 if ((o->flags & SEC_RELOC) != 0)
9616 if (!(_bfd_elf_link_size_reloc_section
9617 (abfd, &elf_section_data (o)->rel_hdr, o)))
9620 if (elf_section_data (o)->rel_hdr2
9621 && !(_bfd_elf_link_size_reloc_section
9622 (abfd, elf_section_data (o)->rel_hdr2, o)))
9626 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
9627 to count upwards while actually outputting the relocations. */
9628 elf_section_data (o)->rel_count = 0;
9629 elf_section_data (o)->rel_count2 = 0;
9632 _bfd_elf_assign_file_positions_for_relocs (abfd);
9634 /* We have now assigned file positions for all the sections except
9635 .symtab and .strtab. We start the .symtab section at the current
9636 file position, and write directly to it. We build the .strtab
9637 section in memory. */
9638 bfd_get_symcount (abfd) = 0;
9639 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9640 /* sh_name is set in prep_headers. */
9641 symtab_hdr->sh_type = SHT_SYMTAB;
9642 /* sh_flags, sh_addr and sh_size all start off zero. */
9643 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
9644 /* sh_link is set in assign_section_numbers. */
9645 /* sh_info is set below. */
9646 /* sh_offset is set just below. */
9647 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
9649 off = elf_tdata (abfd)->next_file_pos;
9650 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
9652 /* Note that at this point elf_tdata (abfd)->next_file_pos is
9653 incorrect. We do not yet know the size of the .symtab section.
9654 We correct next_file_pos below, after we do know the size. */
9656 /* Allocate a buffer to hold swapped out symbols. This is to avoid
9657 continuously seeking to the right position in the file. */
9658 if (! info->keep_memory || max_sym_count < 20)
9659 finfo.symbuf_size = 20;
9661 finfo.symbuf_size = max_sym_count;
9662 amt = finfo.symbuf_size;
9663 amt *= bed->s->sizeof_sym;
9664 finfo.symbuf = bfd_malloc (amt);
9665 if (finfo.symbuf == NULL)
9667 if (elf_numsections (abfd) > SHN_LORESERVE)
9669 /* Wild guess at number of output symbols. realloc'd as needed. */
9670 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
9671 finfo.shndxbuf_size = amt;
9672 amt *= sizeof (Elf_External_Sym_Shndx);
9673 finfo.symshndxbuf = bfd_zmalloc (amt);
9674 if (finfo.symshndxbuf == NULL)
9678 /* Start writing out the symbol table. The first symbol is always a
9680 if (info->strip != strip_all
9683 elfsym.st_value = 0;
9686 elfsym.st_other = 0;
9687 elfsym.st_shndx = SHN_UNDEF;
9688 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
9693 /* Output a symbol for each section. We output these even if we are
9694 discarding local symbols, since they are used for relocs. These
9695 symbols have no names. We store the index of each one in the
9696 index field of the section, so that we can find it again when
9697 outputting relocs. */
9698 if (info->strip != strip_all
9702 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9703 elfsym.st_other = 0;
9704 elfsym.st_value = 0;
9705 for (i = 1; i < elf_numsections (abfd); i++)
9707 o = bfd_section_from_elf_index (abfd, i);
9710 o->target_index = bfd_get_symcount (abfd);
9711 elfsym.st_shndx = i;
9712 if (!info->relocatable)
9713 elfsym.st_value = o->vma;
9714 if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
9717 if (i == SHN_LORESERVE - 1)
9718 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
9722 /* Allocate some memory to hold information read in from the input
9724 if (max_contents_size != 0)
9726 finfo.contents = bfd_malloc (max_contents_size);
9727 if (finfo.contents == NULL)
9731 if (max_external_reloc_size != 0)
9733 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
9734 if (finfo.external_relocs == NULL)
9738 if (max_internal_reloc_count != 0)
9740 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
9741 amt *= sizeof (Elf_Internal_Rela);
9742 finfo.internal_relocs = bfd_malloc (amt);
9743 if (finfo.internal_relocs == NULL)
9747 if (max_sym_count != 0)
9749 amt = max_sym_count * bed->s->sizeof_sym;
9750 finfo.external_syms = bfd_malloc (amt);
9751 if (finfo.external_syms == NULL)
9754 amt = max_sym_count * sizeof (Elf_Internal_Sym);
9755 finfo.internal_syms = bfd_malloc (amt);
9756 if (finfo.internal_syms == NULL)
9759 amt = max_sym_count * sizeof (long);
9760 finfo.indices = bfd_malloc (amt);
9761 if (finfo.indices == NULL)
9764 amt = max_sym_count * sizeof (asection *);
9765 finfo.sections = bfd_malloc (amt);
9766 if (finfo.sections == NULL)
9770 if (max_sym_shndx_count != 0)
9772 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
9773 finfo.locsym_shndx = bfd_malloc (amt);
9774 if (finfo.locsym_shndx == NULL)
9778 if (elf_hash_table (info)->tls_sec)
9780 bfd_vma base, end = 0;
9783 for (sec = elf_hash_table (info)->tls_sec;
9784 sec && (sec->flags & SEC_THREAD_LOCAL);
9787 bfd_size_type size = sec->size;
9790 && (sec->flags & SEC_HAS_CONTENTS) == 0)
9792 struct bfd_link_order *o = sec->map_tail.link_order;
9794 size = o->offset + o->size;
9796 end = sec->vma + size;
9798 base = elf_hash_table (info)->tls_sec->vma;
9799 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
9800 elf_hash_table (info)->tls_size = end - base;
9803 /* Reorder SHF_LINK_ORDER sections. */
9804 for (o = abfd->sections; o != NULL; o = o->next)
9806 if (!elf_fixup_link_order (abfd, o))
9810 /* Since ELF permits relocations to be against local symbols, we
9811 must have the local symbols available when we do the relocations.
9812 Since we would rather only read the local symbols once, and we
9813 would rather not keep them in memory, we handle all the
9814 relocations for a single input file at the same time.
9816 Unfortunately, there is no way to know the total number of local
9817 symbols until we have seen all of them, and the local symbol
9818 indices precede the global symbol indices. This means that when
9819 we are generating relocatable output, and we see a reloc against
9820 a global symbol, we can not know the symbol index until we have
9821 finished examining all the local symbols to see which ones we are
9822 going to output. To deal with this, we keep the relocations in
9823 memory, and don't output them until the end of the link. This is
9824 an unfortunate waste of memory, but I don't see a good way around
9825 it. Fortunately, it only happens when performing a relocatable
9826 link, which is not the common case. FIXME: If keep_memory is set
9827 we could write the relocs out and then read them again; I don't
9828 know how bad the memory loss will be. */
9830 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9831 sub->output_has_begun = FALSE;
9832 for (o = abfd->sections; o != NULL; o = o->next)
9834 for (p = o->map_head.link_order; p != NULL; p = p->next)
9836 if (p->type == bfd_indirect_link_order
9837 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
9838 == bfd_target_elf_flavour)
9839 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
9841 if (! sub->output_has_begun)
9843 if (! elf_link_input_bfd (&finfo, sub))
9845 sub->output_has_begun = TRUE;
9848 else if (p->type == bfd_section_reloc_link_order
9849 || p->type == bfd_symbol_reloc_link_order)
9851 if (! elf_reloc_link_order (abfd, info, o, p))
9856 if (! _bfd_default_link_order (abfd, info, o, p))
9862 /* Free symbol buffer if needed. */
9863 if (!info->reduce_memory_overheads)
9865 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9866 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
9867 && elf_tdata (sub)->symbuf)
9869 free (elf_tdata (sub)->symbuf);
9870 elf_tdata (sub)->symbuf = NULL;
9874 /* Output any global symbols that got converted to local in a
9875 version script or due to symbol visibility. We do this in a
9876 separate step since ELF requires all local symbols to appear
9877 prior to any global symbols. FIXME: We should only do this if
9878 some global symbols were, in fact, converted to become local.
9879 FIXME: Will this work correctly with the Irix 5 linker? */
9880 eoinfo.failed = FALSE;
9881 eoinfo.finfo = &finfo;
9882 eoinfo.localsyms = TRUE;
9883 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
9888 /* If backend needs to output some local symbols not present in the hash
9889 table, do it now. */
9890 if (bed->elf_backend_output_arch_local_syms)
9892 typedef bfd_boolean (*out_sym_func)
9893 (void *, const char *, Elf_Internal_Sym *, asection *,
9894 struct elf_link_hash_entry *);
9896 if (! ((*bed->elf_backend_output_arch_local_syms)
9897 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
9901 /* That wrote out all the local symbols. Finish up the symbol table
9902 with the global symbols. Even if we want to strip everything we
9903 can, we still need to deal with those global symbols that got
9904 converted to local in a version script. */
9906 /* The sh_info field records the index of the first non local symbol. */
9907 symtab_hdr->sh_info = bfd_get_symcount (abfd);
9910 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
9912 Elf_Internal_Sym sym;
9913 bfd_byte *dynsym = finfo.dynsym_sec->contents;
9914 long last_local = 0;
9916 /* Write out the section symbols for the output sections. */
9917 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
9923 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9926 for (s = abfd->sections; s != NULL; s = s->next)
9932 dynindx = elf_section_data (s)->dynindx;
9935 indx = elf_section_data (s)->this_idx;
9936 BFD_ASSERT (indx > 0);
9937 sym.st_shndx = indx;
9938 if (! check_dynsym (abfd, &sym))
9940 sym.st_value = s->vma;
9941 dest = dynsym + dynindx * bed->s->sizeof_sym;
9942 if (last_local < dynindx)
9943 last_local = dynindx;
9944 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
9948 /* Write out the local dynsyms. */
9949 if (elf_hash_table (info)->dynlocal)
9951 struct elf_link_local_dynamic_entry *e;
9952 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
9957 sym.st_size = e->isym.st_size;
9958 sym.st_other = e->isym.st_other;
9960 /* Copy the internal symbol as is.
9961 Note that we saved a word of storage and overwrote
9962 the original st_name with the dynstr_index. */
9965 if (e->isym.st_shndx != SHN_UNDEF
9966 && (e->isym.st_shndx < SHN_LORESERVE
9967 || e->isym.st_shndx > SHN_HIRESERVE))
9969 s = bfd_section_from_elf_index (e->input_bfd,
9973 elf_section_data (s->output_section)->this_idx;
9974 if (! check_dynsym (abfd, &sym))
9976 sym.st_value = (s->output_section->vma
9978 + e->isym.st_value);
9981 if (last_local < e->dynindx)
9982 last_local = e->dynindx;
9984 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
9985 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
9989 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
9993 /* We get the global symbols from the hash table. */
9994 eoinfo.failed = FALSE;
9995 eoinfo.localsyms = FALSE;
9996 eoinfo.finfo = &finfo;
9997 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10002 /* If backend needs to output some symbols not present in the hash
10003 table, do it now. */
10004 if (bed->elf_backend_output_arch_syms)
10006 typedef bfd_boolean (*out_sym_func)
10007 (void *, const char *, Elf_Internal_Sym *, asection *,
10008 struct elf_link_hash_entry *);
10010 if (! ((*bed->elf_backend_output_arch_syms)
10011 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10015 /* Flush all symbols to the file. */
10016 if (! elf_link_flush_output_syms (&finfo, bed))
10019 /* Now we know the size of the symtab section. */
10020 off += symtab_hdr->sh_size;
10022 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
10023 if (symtab_shndx_hdr->sh_name != 0)
10025 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
10026 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
10027 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
10028 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
10029 symtab_shndx_hdr->sh_size = amt;
10031 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
10034 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
10035 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
10040 /* Finish up and write out the symbol string table (.strtab)
10042 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
10043 /* sh_name was set in prep_headers. */
10044 symstrtab_hdr->sh_type = SHT_STRTAB;
10045 symstrtab_hdr->sh_flags = 0;
10046 symstrtab_hdr->sh_addr = 0;
10047 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10048 symstrtab_hdr->sh_entsize = 0;
10049 symstrtab_hdr->sh_link = 0;
10050 symstrtab_hdr->sh_info = 0;
10051 /* sh_offset is set just below. */
10052 symstrtab_hdr->sh_addralign = 1;
10054 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10055 elf_tdata (abfd)->next_file_pos = off;
10057 if (bfd_get_symcount (abfd) > 0)
10059 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10060 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10064 /* Adjust the relocs to have the correct symbol indices. */
10065 for (o = abfd->sections; o != NULL; o = o->next)
10067 if ((o->flags & SEC_RELOC) == 0)
10070 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
10071 elf_section_data (o)->rel_count,
10072 elf_section_data (o)->rel_hashes);
10073 if (elf_section_data (o)->rel_hdr2 != NULL)
10074 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
10075 elf_section_data (o)->rel_count2,
10076 (elf_section_data (o)->rel_hashes
10077 + elf_section_data (o)->rel_count));
10079 /* Set the reloc_count field to 0 to prevent write_relocs from
10080 trying to swap the relocs out itself. */
10081 o->reloc_count = 0;
10084 if (dynamic && info->combreloc && dynobj != NULL)
10085 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
10087 /* If we are linking against a dynamic object, or generating a
10088 shared library, finish up the dynamic linking information. */
10091 bfd_byte *dyncon, *dynconend;
10093 /* Fix up .dynamic entries. */
10094 o = bfd_get_section_by_name (dynobj, ".dynamic");
10095 BFD_ASSERT (o != NULL);
10097 dyncon = o->contents;
10098 dynconend = o->contents + o->size;
10099 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10101 Elf_Internal_Dyn dyn;
10105 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10112 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
10114 switch (elf_section_data (reldyn)->this_hdr.sh_type)
10116 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
10117 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
10120 dyn.d_un.d_val = relativecount;
10127 name = info->init_function;
10130 name = info->fini_function;
10133 struct elf_link_hash_entry *h;
10135 h = elf_link_hash_lookup (elf_hash_table (info), name,
10136 FALSE, FALSE, TRUE);
10138 && (h->root.type == bfd_link_hash_defined
10139 || h->root.type == bfd_link_hash_defweak))
10141 dyn.d_un.d_val = h->root.u.def.value;
10142 o = h->root.u.def.section;
10143 if (o->output_section != NULL)
10144 dyn.d_un.d_val += (o->output_section->vma
10145 + o->output_offset);
10148 /* The symbol is imported from another shared
10149 library and does not apply to this one. */
10150 dyn.d_un.d_val = 0;
10157 case DT_PREINIT_ARRAYSZ:
10158 name = ".preinit_array";
10160 case DT_INIT_ARRAYSZ:
10161 name = ".init_array";
10163 case DT_FINI_ARRAYSZ:
10164 name = ".fini_array";
10166 o = bfd_get_section_by_name (abfd, name);
10169 (*_bfd_error_handler)
10170 (_("%B: could not find output section %s"), abfd, name);
10174 (*_bfd_error_handler)
10175 (_("warning: %s section has zero size"), name);
10176 dyn.d_un.d_val = o->size;
10179 case DT_PREINIT_ARRAY:
10180 name = ".preinit_array";
10182 case DT_INIT_ARRAY:
10183 name = ".init_array";
10185 case DT_FINI_ARRAY:
10186 name = ".fini_array";
10193 name = ".gnu.hash";
10202 name = ".gnu.version_d";
10205 name = ".gnu.version_r";
10208 name = ".gnu.version";
10210 o = bfd_get_section_by_name (abfd, name);
10213 (*_bfd_error_handler)
10214 (_("%B: could not find output section %s"), abfd, name);
10217 dyn.d_un.d_ptr = o->vma;
10224 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
10228 dyn.d_un.d_val = 0;
10229 for (i = 1; i < elf_numsections (abfd); i++)
10231 Elf_Internal_Shdr *hdr;
10233 hdr = elf_elfsections (abfd)[i];
10234 if (hdr->sh_type == type
10235 && (hdr->sh_flags & SHF_ALLOC) != 0)
10237 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
10238 dyn.d_un.d_val += hdr->sh_size;
10241 if (dyn.d_un.d_val == 0
10242 || hdr->sh_addr < dyn.d_un.d_val)
10243 dyn.d_un.d_val = hdr->sh_addr;
10249 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
10253 /* If we have created any dynamic sections, then output them. */
10254 if (dynobj != NULL)
10256 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
10259 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10260 if (info->warn_shared_textrel && info->shared)
10262 bfd_byte *dyncon, *dynconend;
10264 /* Fix up .dynamic entries. */
10265 o = bfd_get_section_by_name (dynobj, ".dynamic");
10266 BFD_ASSERT (o != NULL);
10268 dyncon = o->contents;
10269 dynconend = o->contents + o->size;
10270 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10272 Elf_Internal_Dyn dyn;
10274 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10276 if (dyn.d_tag == DT_TEXTREL)
10278 info->callbacks->einfo
10279 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
10285 for (o = dynobj->sections; o != NULL; o = o->next)
10287 if ((o->flags & SEC_HAS_CONTENTS) == 0
10289 || o->output_section == bfd_abs_section_ptr)
10291 if ((o->flags & SEC_LINKER_CREATED) == 0)
10293 /* At this point, we are only interested in sections
10294 created by _bfd_elf_link_create_dynamic_sections. */
10297 if (elf_hash_table (info)->stab_info.stabstr == o)
10299 if (elf_hash_table (info)->eh_info.hdr_sec == o)
10301 if ((elf_section_data (o->output_section)->this_hdr.sh_type
10303 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
10305 if (! bfd_set_section_contents (abfd, o->output_section,
10307 (file_ptr) o->output_offset,
10313 /* The contents of the .dynstr section are actually in a
10315 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
10316 if (bfd_seek (abfd, off, SEEK_SET) != 0
10317 || ! _bfd_elf_strtab_emit (abfd,
10318 elf_hash_table (info)->dynstr))
10324 if (info->relocatable)
10326 bfd_boolean failed = FALSE;
10328 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
10333 /* If we have optimized stabs strings, output them. */
10334 if (elf_hash_table (info)->stab_info.stabstr != NULL)
10336 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
10340 if (info->eh_frame_hdr)
10342 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
10346 if (finfo.symstrtab != NULL)
10347 _bfd_stringtab_free (finfo.symstrtab);
10348 if (finfo.contents != NULL)
10349 free (finfo.contents);
10350 if (finfo.external_relocs != NULL)
10351 free (finfo.external_relocs);
10352 if (finfo.internal_relocs != NULL)
10353 free (finfo.internal_relocs);
10354 if (finfo.external_syms != NULL)
10355 free (finfo.external_syms);
10356 if (finfo.locsym_shndx != NULL)
10357 free (finfo.locsym_shndx);
10358 if (finfo.internal_syms != NULL)
10359 free (finfo.internal_syms);
10360 if (finfo.indices != NULL)
10361 free (finfo.indices);
10362 if (finfo.sections != NULL)
10363 free (finfo.sections);
10364 if (finfo.symbuf != NULL)
10365 free (finfo.symbuf);
10366 if (finfo.symshndxbuf != NULL)
10367 free (finfo.symshndxbuf);
10368 for (o = abfd->sections; o != NULL; o = o->next)
10370 if ((o->flags & SEC_RELOC) != 0
10371 && elf_section_data (o)->rel_hashes != NULL)
10372 free (elf_section_data (o)->rel_hashes);
10375 elf_tdata (abfd)->linker = TRUE;
10379 bfd_byte *contents = bfd_malloc (attr_size);
10380 if (contents == NULL)
10382 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
10383 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
10390 if (finfo.symstrtab != NULL)
10391 _bfd_stringtab_free (finfo.symstrtab);
10392 if (finfo.contents != NULL)
10393 free (finfo.contents);
10394 if (finfo.external_relocs != NULL)
10395 free (finfo.external_relocs);
10396 if (finfo.internal_relocs != NULL)
10397 free (finfo.internal_relocs);
10398 if (finfo.external_syms != NULL)
10399 free (finfo.external_syms);
10400 if (finfo.locsym_shndx != NULL)
10401 free (finfo.locsym_shndx);
10402 if (finfo.internal_syms != NULL)
10403 free (finfo.internal_syms);
10404 if (finfo.indices != NULL)
10405 free (finfo.indices);
10406 if (finfo.sections != NULL)
10407 free (finfo.sections);
10408 if (finfo.symbuf != NULL)
10409 free (finfo.symbuf);
10410 if (finfo.symshndxbuf != NULL)
10411 free (finfo.symshndxbuf);
10412 for (o = abfd->sections; o != NULL; o = o->next)
10414 if ((o->flags & SEC_RELOC) != 0
10415 && elf_section_data (o)->rel_hashes != NULL)
10416 free (elf_section_data (o)->rel_hashes);
10422 /* Garbage collect unused sections. */
10424 /* Default gc_mark_hook. */
10427 _bfd_elf_gc_mark_hook (asection *sec,
10428 struct bfd_link_info *info ATTRIBUTE_UNUSED,
10429 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
10430 struct elf_link_hash_entry *h,
10431 Elf_Internal_Sym *sym)
10435 switch (h->root.type)
10437 case bfd_link_hash_defined:
10438 case bfd_link_hash_defweak:
10439 return h->root.u.def.section;
10441 case bfd_link_hash_common:
10442 return h->root.u.c.p->section;
10449 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
10454 /* The mark phase of garbage collection. For a given section, mark
10455 it and any sections in this section's group, and all the sections
10456 which define symbols to which it refers. */
10459 _bfd_elf_gc_mark (struct bfd_link_info *info,
10461 elf_gc_mark_hook_fn gc_mark_hook)
10465 asection *group_sec;
10469 /* Mark all the sections in the group. */
10470 group_sec = elf_section_data (sec)->next_in_group;
10471 if (group_sec && !group_sec->gc_mark)
10472 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
10475 /* Look through the section relocs. */
10477 is_eh = strcmp (sec->name, ".eh_frame") == 0;
10478 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
10480 Elf_Internal_Rela *relstart, *rel, *relend;
10481 Elf_Internal_Shdr *symtab_hdr;
10482 struct elf_link_hash_entry **sym_hashes;
10485 bfd *input_bfd = sec->owner;
10486 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
10487 Elf_Internal_Sym *isym = NULL;
10490 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10491 sym_hashes = elf_sym_hashes (input_bfd);
10493 /* Read the local symbols. */
10494 if (elf_bad_symtab (input_bfd))
10496 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
10500 extsymoff = nlocsyms = symtab_hdr->sh_info;
10502 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
10503 if (isym == NULL && nlocsyms != 0)
10505 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
10511 /* Read the relocations. */
10512 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
10513 info->keep_memory);
10514 if (relstart == NULL)
10519 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
10521 if (bed->s->arch_size == 32)
10526 for (rel = relstart; rel < relend; rel++)
10528 unsigned long r_symndx;
10530 struct elf_link_hash_entry *h;
10532 r_symndx = rel->r_info >> r_sym_shift;
10536 if (r_symndx >= nlocsyms
10537 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
10539 h = sym_hashes[r_symndx - extsymoff];
10540 while (h->root.type == bfd_link_hash_indirect
10541 || h->root.type == bfd_link_hash_warning)
10542 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10543 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
10547 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
10550 if (rsec && !rsec->gc_mark)
10552 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
10555 rsec->gc_mark_from_eh = 1;
10556 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
10565 if (elf_section_data (sec)->relocs != relstart)
10568 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
10570 if (! info->keep_memory)
10573 symtab_hdr->contents = (unsigned char *) isym;
10580 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
10582 struct elf_gc_sweep_symbol_info
10584 struct bfd_link_info *info;
10585 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
10590 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
10592 if (h->root.type == bfd_link_hash_warning)
10593 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10595 if ((h->root.type == bfd_link_hash_defined
10596 || h->root.type == bfd_link_hash_defweak)
10597 && !h->root.u.def.section->gc_mark
10598 && !(h->root.u.def.section->owner->flags & DYNAMIC))
10600 struct elf_gc_sweep_symbol_info *inf = data;
10601 (*inf->hide_symbol) (inf->info, h, TRUE);
10607 /* The sweep phase of garbage collection. Remove all garbage sections. */
10609 typedef bfd_boolean (*gc_sweep_hook_fn)
10610 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
10613 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
10616 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10617 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
10618 unsigned long section_sym_count;
10619 struct elf_gc_sweep_symbol_info sweep_info;
10621 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10625 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
10628 for (o = sub->sections; o != NULL; o = o->next)
10630 /* Keep debug and special sections. */
10631 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
10632 || elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
10633 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
10639 /* Skip sweeping sections already excluded. */
10640 if (o->flags & SEC_EXCLUDE)
10643 /* Since this is early in the link process, it is simple
10644 to remove a section from the output. */
10645 o->flags |= SEC_EXCLUDE;
10647 if (info->print_gc_sections && o->size != 0)
10648 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
10650 /* But we also have to update some of the relocation
10651 info we collected before. */
10653 && (o->flags & SEC_RELOC) != 0
10654 && o->reloc_count > 0
10655 && !bfd_is_abs_section (o->output_section))
10657 Elf_Internal_Rela *internal_relocs;
10661 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
10662 info->keep_memory);
10663 if (internal_relocs == NULL)
10666 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
10668 if (elf_section_data (o)->relocs != internal_relocs)
10669 free (internal_relocs);
10677 /* Remove the symbols that were in the swept sections from the dynamic
10678 symbol table. GCFIXME: Anyone know how to get them out of the
10679 static symbol table as well? */
10680 sweep_info.info = info;
10681 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
10682 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
10685 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
10689 /* Propagate collected vtable information. This is called through
10690 elf_link_hash_traverse. */
10693 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
10695 if (h->root.type == bfd_link_hash_warning)
10696 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10698 /* Those that are not vtables. */
10699 if (h->vtable == NULL || h->vtable->parent == NULL)
10702 /* Those vtables that do not have parents, we cannot merge. */
10703 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
10706 /* If we've already been done, exit. */
10707 if (h->vtable->used && h->vtable->used[-1])
10710 /* Make sure the parent's table is up to date. */
10711 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
10713 if (h->vtable->used == NULL)
10715 /* None of this table's entries were referenced. Re-use the
10717 h->vtable->used = h->vtable->parent->vtable->used;
10718 h->vtable->size = h->vtable->parent->vtable->size;
10723 bfd_boolean *cu, *pu;
10725 /* Or the parent's entries into ours. */
10726 cu = h->vtable->used;
10728 pu = h->vtable->parent->vtable->used;
10731 const struct elf_backend_data *bed;
10732 unsigned int log_file_align;
10734 bed = get_elf_backend_data (h->root.u.def.section->owner);
10735 log_file_align = bed->s->log_file_align;
10736 n = h->vtable->parent->vtable->size >> log_file_align;
10751 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
10754 bfd_vma hstart, hend;
10755 Elf_Internal_Rela *relstart, *relend, *rel;
10756 const struct elf_backend_data *bed;
10757 unsigned int log_file_align;
10759 if (h->root.type == bfd_link_hash_warning)
10760 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10762 /* Take care of both those symbols that do not describe vtables as
10763 well as those that are not loaded. */
10764 if (h->vtable == NULL || h->vtable->parent == NULL)
10767 BFD_ASSERT (h->root.type == bfd_link_hash_defined
10768 || h->root.type == bfd_link_hash_defweak);
10770 sec = h->root.u.def.section;
10771 hstart = h->root.u.def.value;
10772 hend = hstart + h->size;
10774 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
10776 return *(bfd_boolean *) okp = FALSE;
10777 bed = get_elf_backend_data (sec->owner);
10778 log_file_align = bed->s->log_file_align;
10780 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
10782 for (rel = relstart; rel < relend; ++rel)
10783 if (rel->r_offset >= hstart && rel->r_offset < hend)
10785 /* If the entry is in use, do nothing. */
10786 if (h->vtable->used
10787 && (rel->r_offset - hstart) < h->vtable->size)
10789 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
10790 if (h->vtable->used[entry])
10793 /* Otherwise, kill it. */
10794 rel->r_offset = rel->r_info = rel->r_addend = 0;
10800 /* Mark sections containing dynamically referenced symbols. When
10801 building shared libraries, we must assume that any visible symbol is
10805 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
10807 struct bfd_link_info *info = (struct bfd_link_info *) inf;
10809 if (h->root.type == bfd_link_hash_warning)
10810 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10812 if ((h->root.type == bfd_link_hash_defined
10813 || h->root.type == bfd_link_hash_defweak)
10815 || (!info->executable
10817 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
10818 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
10819 h->root.u.def.section->flags |= SEC_KEEP;
10824 /* Do mark and sweep of unused sections. */
10827 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
10829 bfd_boolean ok = TRUE;
10831 elf_gc_mark_hook_fn gc_mark_hook;
10832 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10834 if (!bed->can_gc_sections
10835 || info->relocatable
10836 || info->emitrelocations
10837 || !is_elf_hash_table (info->hash))
10839 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
10843 /* Apply transitive closure to the vtable entry usage info. */
10844 elf_link_hash_traverse (elf_hash_table (info),
10845 elf_gc_propagate_vtable_entries_used,
10850 /* Kill the vtable relocations that were not used. */
10851 elf_link_hash_traverse (elf_hash_table (info),
10852 elf_gc_smash_unused_vtentry_relocs,
10857 /* Mark dynamically referenced symbols. */
10858 if (elf_hash_table (info)->dynamic_sections_created)
10859 elf_link_hash_traverse (elf_hash_table (info),
10860 bed->gc_mark_dynamic_ref,
10863 /* Grovel through relocs to find out who stays ... */
10864 gc_mark_hook = bed->gc_mark_hook;
10865 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10869 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
10872 for (o = sub->sections; o != NULL; o = o->next)
10873 if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark)
10874 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
10878 /* Allow the backend to mark additional target specific sections. */
10879 if (bed->gc_mark_extra_sections)
10880 bed->gc_mark_extra_sections(info, gc_mark_hook);
10882 /* ... again for sections marked from eh_frame. */
10883 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10887 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
10890 /* Keep .gcc_except_table.* if the associated .text.* (or the
10891 associated .gnu.linkonce.t.* if .text.* doesn't exist) is
10892 marked. This isn't very nice, but the proper solution,
10893 splitting .eh_frame up and using comdat doesn't pan out
10894 easily due to needing special relocs to handle the
10895 difference of two symbols in separate sections.
10896 Don't keep code sections referenced by .eh_frame. */
10897 #define TEXT_PREFIX ".text."
10898 #define TEXT_PREFIX2 ".gnu.linkonce.t."
10899 #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table."
10900 for (o = sub->sections; o != NULL; o = o->next)
10901 if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
10903 if (CONST_STRNEQ (o->name, GCC_EXCEPT_TABLE_PREFIX))
10906 const char *sec_name;
10908 unsigned o_name_prefix_len , fn_name_prefix_len, tmp;
10910 o_name_prefix_len = strlen (GCC_EXCEPT_TABLE_PREFIX);
10911 sec_name = o->name + o_name_prefix_len;
10912 fn_name_prefix_len = strlen (TEXT_PREFIX);
10913 tmp = strlen (TEXT_PREFIX2);
10914 if (tmp > fn_name_prefix_len)
10915 fn_name_prefix_len = tmp;
10917 = bfd_malloc (fn_name_prefix_len + strlen (sec_name) + 1);
10918 if (fn_name == NULL)
10921 /* Try the first prefix. */
10922 sprintf (fn_name, "%s%s", TEXT_PREFIX, sec_name);
10923 fn_text = bfd_get_section_by_name (sub, fn_name);
10925 /* Try the second prefix. */
10926 if (fn_text == NULL)
10928 sprintf (fn_name, "%s%s", TEXT_PREFIX2, sec_name);
10929 fn_text = bfd_get_section_by_name (sub, fn_name);
10933 if (fn_text == NULL || !fn_text->gc_mark)
10937 /* If not using specially named exception table section,
10938 then keep whatever we are using. */
10939 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
10944 /* ... and mark SEC_EXCLUDE for those that go. */
10945 return elf_gc_sweep (abfd, info);
10948 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
10951 bfd_elf_gc_record_vtinherit (bfd *abfd,
10953 struct elf_link_hash_entry *h,
10956 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
10957 struct elf_link_hash_entry **search, *child;
10958 bfd_size_type extsymcount;
10959 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10961 /* The sh_info field of the symtab header tells us where the
10962 external symbols start. We don't care about the local symbols at
10964 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
10965 if (!elf_bad_symtab (abfd))
10966 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
10968 sym_hashes = elf_sym_hashes (abfd);
10969 sym_hashes_end = sym_hashes + extsymcount;
10971 /* Hunt down the child symbol, which is in this section at the same
10972 offset as the relocation. */
10973 for (search = sym_hashes; search != sym_hashes_end; ++search)
10975 if ((child = *search) != NULL
10976 && (child->root.type == bfd_link_hash_defined
10977 || child->root.type == bfd_link_hash_defweak)
10978 && child->root.u.def.section == sec
10979 && child->root.u.def.value == offset)
10983 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
10984 abfd, sec, (unsigned long) offset);
10985 bfd_set_error (bfd_error_invalid_operation);
10989 if (!child->vtable)
10991 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
10992 if (!child->vtable)
10997 /* This *should* only be the absolute section. It could potentially
10998 be that someone has defined a non-global vtable though, which
10999 would be bad. It isn't worth paging in the local symbols to be
11000 sure though; that case should simply be handled by the assembler. */
11002 child->vtable->parent = (struct elf_link_hash_entry *) -1;
11005 child->vtable->parent = h;
11010 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
11013 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
11014 asection *sec ATTRIBUTE_UNUSED,
11015 struct elf_link_hash_entry *h,
11018 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11019 unsigned int log_file_align = bed->s->log_file_align;
11023 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
11028 if (addend >= h->vtable->size)
11030 size_t size, bytes, file_align;
11031 bfd_boolean *ptr = h->vtable->used;
11033 /* While the symbol is undefined, we have to be prepared to handle
11035 file_align = 1 << log_file_align;
11036 if (h->root.type == bfd_link_hash_undefined)
11037 size = addend + file_align;
11041 if (addend >= size)
11043 /* Oops! We've got a reference past the defined end of
11044 the table. This is probably a bug -- shall we warn? */
11045 size = addend + file_align;
11048 size = (size + file_align - 1) & -file_align;
11050 /* Allocate one extra entry for use as a "done" flag for the
11051 consolidation pass. */
11052 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
11056 ptr = bfd_realloc (ptr - 1, bytes);
11062 oldbytes = (((h->vtable->size >> log_file_align) + 1)
11063 * sizeof (bfd_boolean));
11064 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
11068 ptr = bfd_zmalloc (bytes);
11073 /* And arrange for that done flag to be at index -1. */
11074 h->vtable->used = ptr + 1;
11075 h->vtable->size = size;
11078 h->vtable->used[addend >> log_file_align] = TRUE;
11083 struct alloc_got_off_arg {
11085 unsigned int got_elt_size;
11088 /* We need a special top-level link routine to convert got reference counts
11089 to real got offsets. */
11092 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
11094 struct alloc_got_off_arg *gofarg = arg;
11096 if (h->root.type == bfd_link_hash_warning)
11097 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11099 if (h->got.refcount > 0)
11101 h->got.offset = gofarg->gotoff;
11102 gofarg->gotoff += gofarg->got_elt_size;
11105 h->got.offset = (bfd_vma) -1;
11110 /* And an accompanying bit to work out final got entry offsets once
11111 we're done. Should be called from final_link. */
11114 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
11115 struct bfd_link_info *info)
11118 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11120 unsigned int got_elt_size = bed->s->arch_size / 8;
11121 struct alloc_got_off_arg gofarg;
11123 if (! is_elf_hash_table (info->hash))
11126 /* The GOT offset is relative to the .got section, but the GOT header is
11127 put into the .got.plt section, if the backend uses it. */
11128 if (bed->want_got_plt)
11131 gotoff = bed->got_header_size;
11133 /* Do the local .got entries first. */
11134 for (i = info->input_bfds; i; i = i->link_next)
11136 bfd_signed_vma *local_got;
11137 bfd_size_type j, locsymcount;
11138 Elf_Internal_Shdr *symtab_hdr;
11140 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
11143 local_got = elf_local_got_refcounts (i);
11147 symtab_hdr = &elf_tdata (i)->symtab_hdr;
11148 if (elf_bad_symtab (i))
11149 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11151 locsymcount = symtab_hdr->sh_info;
11153 for (j = 0; j < locsymcount; ++j)
11155 if (local_got[j] > 0)
11157 local_got[j] = gotoff;
11158 gotoff += got_elt_size;
11161 local_got[j] = (bfd_vma) -1;
11165 /* Then the global .got entries. .plt refcounts are handled by
11166 adjust_dynamic_symbol */
11167 gofarg.gotoff = gotoff;
11168 gofarg.got_elt_size = got_elt_size;
11169 elf_link_hash_traverse (elf_hash_table (info),
11170 elf_gc_allocate_got_offsets,
11175 /* Many folk need no more in the way of final link than this, once
11176 got entry reference counting is enabled. */
11179 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
11181 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
11184 /* Invoke the regular ELF backend linker to do all the work. */
11185 return bfd_elf_final_link (abfd, info);
11189 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
11191 struct elf_reloc_cookie *rcookie = cookie;
11193 if (rcookie->bad_symtab)
11194 rcookie->rel = rcookie->rels;
11196 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
11198 unsigned long r_symndx;
11200 if (! rcookie->bad_symtab)
11201 if (rcookie->rel->r_offset > offset)
11203 if (rcookie->rel->r_offset != offset)
11206 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
11207 if (r_symndx == SHN_UNDEF)
11210 if (r_symndx >= rcookie->locsymcount
11211 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11213 struct elf_link_hash_entry *h;
11215 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
11217 while (h->root.type == bfd_link_hash_indirect
11218 || h->root.type == bfd_link_hash_warning)
11219 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11221 if ((h->root.type == bfd_link_hash_defined
11222 || h->root.type == bfd_link_hash_defweak)
11223 && elf_discarded_section (h->root.u.def.section))
11230 /* It's not a relocation against a global symbol,
11231 but it could be a relocation against a local
11232 symbol for a discarded section. */
11234 Elf_Internal_Sym *isym;
11236 /* Need to: get the symbol; get the section. */
11237 isym = &rcookie->locsyms[r_symndx];
11238 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
11240 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
11241 if (isec != NULL && elf_discarded_section (isec))
11250 /* Discard unneeded references to discarded sections.
11251 Returns TRUE if any section's size was changed. */
11252 /* This function assumes that the relocations are in sorted order,
11253 which is true for all known assemblers. */
11256 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
11258 struct elf_reloc_cookie cookie;
11259 asection *stab, *eh;
11260 Elf_Internal_Shdr *symtab_hdr;
11261 const struct elf_backend_data *bed;
11263 unsigned int count;
11264 bfd_boolean ret = FALSE;
11266 if (info->traditional_format
11267 || !is_elf_hash_table (info->hash))
11270 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
11272 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
11275 bed = get_elf_backend_data (abfd);
11277 if ((abfd->flags & DYNAMIC) != 0)
11281 if (!info->relocatable)
11283 eh = bfd_get_section_by_name (abfd, ".eh_frame");
11286 || bfd_is_abs_section (eh->output_section)))
11290 stab = bfd_get_section_by_name (abfd, ".stab");
11292 && (stab->size == 0
11293 || bfd_is_abs_section (stab->output_section)
11294 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
11299 && bed->elf_backend_discard_info == NULL)
11302 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11303 cookie.abfd = abfd;
11304 cookie.sym_hashes = elf_sym_hashes (abfd);
11305 cookie.bad_symtab = elf_bad_symtab (abfd);
11306 if (cookie.bad_symtab)
11308 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11309 cookie.extsymoff = 0;
11313 cookie.locsymcount = symtab_hdr->sh_info;
11314 cookie.extsymoff = symtab_hdr->sh_info;
11317 if (bed->s->arch_size == 32)
11318 cookie.r_sym_shift = 8;
11320 cookie.r_sym_shift = 32;
11322 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11323 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
11325 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11326 cookie.locsymcount, 0,
11328 if (cookie.locsyms == NULL)
11330 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11337 cookie.rels = NULL;
11338 count = stab->reloc_count;
11340 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
11341 info->keep_memory);
11342 if (cookie.rels != NULL)
11344 cookie.rel = cookie.rels;
11345 cookie.relend = cookie.rels;
11346 cookie.relend += count * bed->s->int_rels_per_ext_rel;
11347 if (_bfd_discard_section_stabs (abfd, stab,
11348 elf_section_data (stab)->sec_info,
11349 bfd_elf_reloc_symbol_deleted_p,
11352 if (elf_section_data (stab)->relocs != cookie.rels)
11353 free (cookie.rels);
11359 cookie.rels = NULL;
11360 count = eh->reloc_count;
11362 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
11363 info->keep_memory);
11364 cookie.rel = cookie.rels;
11365 cookie.relend = cookie.rels;
11366 if (cookie.rels != NULL)
11367 cookie.relend += count * bed->s->int_rels_per_ext_rel;
11369 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
11370 bfd_elf_reloc_symbol_deleted_p,
11374 if (cookie.rels != NULL
11375 && elf_section_data (eh)->relocs != cookie.rels)
11376 free (cookie.rels);
11379 if (bed->elf_backend_discard_info != NULL
11380 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
11383 if (cookie.locsyms != NULL
11384 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
11386 if (! info->keep_memory)
11387 free (cookie.locsyms);
11389 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
11393 if (info->eh_frame_hdr
11394 && !info->relocatable
11395 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
11402 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section *sec,
11403 struct bfd_link_info *info)
11406 const char *name, *p;
11407 struct bfd_section_already_linked *l;
11408 struct bfd_section_already_linked_hash_entry *already_linked_list;
11410 if (sec->output_section == bfd_abs_section_ptr)
11413 flags = sec->flags;
11415 /* Return if it isn't a linkonce section. A comdat group section
11416 also has SEC_LINK_ONCE set. */
11417 if ((flags & SEC_LINK_ONCE) == 0)
11420 /* Don't put group member sections on our list of already linked
11421 sections. They are handled as a group via their group section. */
11422 if (elf_sec_group (sec) != NULL)
11425 /* FIXME: When doing a relocatable link, we may have trouble
11426 copying relocations in other sections that refer to local symbols
11427 in the section being discarded. Those relocations will have to
11428 be converted somehow; as of this writing I'm not sure that any of
11429 the backends handle that correctly.
11431 It is tempting to instead not discard link once sections when
11432 doing a relocatable link (technically, they should be discarded
11433 whenever we are building constructors). However, that fails,
11434 because the linker winds up combining all the link once sections
11435 into a single large link once section, which defeats the purpose
11436 of having link once sections in the first place.
11438 Also, not merging link once sections in a relocatable link
11439 causes trouble for MIPS ELF, which relies on link once semantics
11440 to handle the .reginfo section correctly. */
11442 name = bfd_get_section_name (abfd, sec);
11444 if (CONST_STRNEQ (name, ".gnu.linkonce.")
11445 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
11450 already_linked_list = bfd_section_already_linked_table_lookup (p);
11452 for (l = already_linked_list->entry; l != NULL; l = l->next)
11454 /* We may have 2 different types of sections on the list: group
11455 sections and linkonce sections. Match like sections. */
11456 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
11457 && strcmp (name, l->sec->name) == 0
11458 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
11460 /* The section has already been linked. See if we should
11461 issue a warning. */
11462 switch (flags & SEC_LINK_DUPLICATES)
11467 case SEC_LINK_DUPLICATES_DISCARD:
11470 case SEC_LINK_DUPLICATES_ONE_ONLY:
11471 (*_bfd_error_handler)
11472 (_("%B: ignoring duplicate section `%A'"),
11476 case SEC_LINK_DUPLICATES_SAME_SIZE:
11477 if (sec->size != l->sec->size)
11478 (*_bfd_error_handler)
11479 (_("%B: duplicate section `%A' has different size"),
11483 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
11484 if (sec->size != l->sec->size)
11485 (*_bfd_error_handler)
11486 (_("%B: duplicate section `%A' has different size"),
11488 else if (sec->size != 0)
11490 bfd_byte *sec_contents, *l_sec_contents;
11492 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
11493 (*_bfd_error_handler)
11494 (_("%B: warning: could not read contents of section `%A'"),
11496 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
11498 (*_bfd_error_handler)
11499 (_("%B: warning: could not read contents of section `%A'"),
11500 l->sec->owner, l->sec);
11501 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
11502 (*_bfd_error_handler)
11503 (_("%B: warning: duplicate section `%A' has different contents"),
11507 free (sec_contents);
11508 if (l_sec_contents)
11509 free (l_sec_contents);
11514 /* Set the output_section field so that lang_add_section
11515 does not create a lang_input_section structure for this
11516 section. Since there might be a symbol in the section
11517 being discarded, we must retain a pointer to the section
11518 which we are really going to use. */
11519 sec->output_section = bfd_abs_section_ptr;
11520 sec->kept_section = l->sec;
11522 if (flags & SEC_GROUP)
11524 asection *first = elf_next_in_group (sec);
11525 asection *s = first;
11529 s->output_section = bfd_abs_section_ptr;
11530 /* Record which group discards it. */
11531 s->kept_section = l->sec;
11532 s = elf_next_in_group (s);
11533 /* These lists are circular. */
11543 /* A single member comdat group section may be discarded by a
11544 linkonce section and vice versa. */
11546 if ((flags & SEC_GROUP) != 0)
11548 asection *first = elf_next_in_group (sec);
11550 if (first != NULL && elf_next_in_group (first) == first)
11551 /* Check this single member group against linkonce sections. */
11552 for (l = already_linked_list->entry; l != NULL; l = l->next)
11553 if ((l->sec->flags & SEC_GROUP) == 0
11554 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
11555 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
11557 first->output_section = bfd_abs_section_ptr;
11558 first->kept_section = l->sec;
11559 sec->output_section = bfd_abs_section_ptr;
11564 /* Check this linkonce section against single member groups. */
11565 for (l = already_linked_list->entry; l != NULL; l = l->next)
11566 if (l->sec->flags & SEC_GROUP)
11568 asection *first = elf_next_in_group (l->sec);
11571 && elf_next_in_group (first) == first
11572 && bfd_elf_match_symbols_in_sections (first, sec, info))
11574 sec->output_section = bfd_abs_section_ptr;
11575 sec->kept_section = first;
11580 /* This is the first section with this name. Record it. */
11581 bfd_section_already_linked_table_insert (already_linked_list, sec);
11585 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
11587 return sym->st_shndx == SHN_COMMON;
11591 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
11597 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
11599 return bfd_com_section_ptr;