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 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
28 #include "safe-ctype.h"
29 #include "libiberty.h"
32 /* Define a symbol in a dynamic linkage section. */
34 struct elf_link_hash_entry *
35 _bfd_elf_define_linkage_sym (bfd *abfd,
36 struct bfd_link_info *info,
40 struct elf_link_hash_entry *h;
41 struct bfd_link_hash_entry *bh;
42 const struct elf_backend_data *bed;
44 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
47 /* Zap symbol defined in an as-needed lib that wasn't linked.
48 This is a symptom of a larger problem: Absolute symbols
49 defined in shared libraries can't be overridden, because we
50 lose the link to the bfd which is via the symbol section. */
51 h->root.type = bfd_link_hash_new;
55 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
57 get_elf_backend_data (abfd)->collect,
60 h = (struct elf_link_hash_entry *) bh;
63 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
65 bed = get_elf_backend_data (abfd);
66 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
71 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
75 struct elf_link_hash_entry *h;
76 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
79 /* This function may be called more than once. */
80 s = bfd_get_section_by_name (abfd, ".got");
81 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
84 switch (bed->s->arch_size)
95 bfd_set_error (bfd_error_bad_value);
99 flags = bed->dynamic_sec_flags;
101 s = bfd_make_section_with_flags (abfd, ".got", flags);
103 || !bfd_set_section_alignment (abfd, s, ptralign))
106 if (bed->want_got_plt)
108 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
110 || !bfd_set_section_alignment (abfd, s, ptralign))
114 if (bed->want_got_sym)
116 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
117 (or .got.plt) section. We don't do this in the linker script
118 because we don't want to define the symbol if we are not creating
119 a global offset table. */
120 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
121 elf_hash_table (info)->hgot = h;
126 /* The first bit of the global offset table is the header. */
127 s->size += bed->got_header_size;
132 /* Create a strtab to hold the dynamic symbol names. */
134 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
136 struct elf_link_hash_table *hash_table;
138 hash_table = elf_hash_table (info);
139 if (hash_table->dynobj == NULL)
140 hash_table->dynobj = abfd;
142 if (hash_table->dynstr == NULL)
144 hash_table->dynstr = _bfd_elf_strtab_init ();
145 if (hash_table->dynstr == NULL)
151 /* Create some sections which will be filled in with dynamic linking
152 information. ABFD is an input file which requires dynamic sections
153 to be created. The dynamic sections take up virtual memory space
154 when the final executable is run, so we need to create them before
155 addresses are assigned to the output sections. We work out the
156 actual contents and size of these sections later. */
159 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
162 register asection *s;
163 const struct elf_backend_data *bed;
165 if (! is_elf_hash_table (info->hash))
168 if (elf_hash_table (info)->dynamic_sections_created)
171 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
174 abfd = elf_hash_table (info)->dynobj;
175 bed = get_elf_backend_data (abfd);
177 flags = bed->dynamic_sec_flags;
179 /* A dynamically linked executable has a .interp section, but a
180 shared library does not. */
181 if (info->executable)
183 s = bfd_make_section_with_flags (abfd, ".interp",
184 flags | SEC_READONLY);
189 /* Create sections to hold version informations. These are removed
190 if they are not needed. */
191 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
192 flags | SEC_READONLY);
194 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
197 s = bfd_make_section_with_flags (abfd, ".gnu.version",
198 flags | SEC_READONLY);
200 || ! bfd_set_section_alignment (abfd, s, 1))
203 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
204 flags | SEC_READONLY);
206 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
209 s = bfd_make_section_with_flags (abfd, ".dynsym",
210 flags | SEC_READONLY);
212 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
215 s = bfd_make_section_with_flags (abfd, ".dynstr",
216 flags | SEC_READONLY);
220 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
222 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
225 /* The special symbol _DYNAMIC is always set to the start of the
226 .dynamic section. We could set _DYNAMIC in a linker script, but we
227 only want to define it if we are, in fact, creating a .dynamic
228 section. We don't want to define it if there is no .dynamic
229 section, since on some ELF platforms the start up code examines it
230 to decide how to initialize the process. */
231 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
236 s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
238 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
240 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
243 if (info->emit_gnu_hash)
245 s = bfd_make_section_with_flags (abfd, ".gnu.hash",
246 flags | SEC_READONLY);
248 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
250 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
251 4 32-bit words followed by variable count of 64-bit words, then
252 variable count of 32-bit words. */
253 if (bed->s->arch_size == 64)
254 elf_section_data (s)->this_hdr.sh_entsize = 0;
256 elf_section_data (s)->this_hdr.sh_entsize = 4;
259 /* Let the backend create the rest of the sections. This lets the
260 backend set the right flags. The backend will normally create
261 the .got and .plt sections. */
262 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
265 elf_hash_table (info)->dynamic_sections_created = TRUE;
270 /* Create dynamic sections when linking against a dynamic object. */
273 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
275 flagword flags, pltflags;
276 struct elf_link_hash_entry *h;
278 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
280 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
281 .rel[a].bss sections. */
282 flags = bed->dynamic_sec_flags;
285 if (bed->plt_not_loaded)
286 /* We do not clear SEC_ALLOC here because we still want the OS to
287 allocate space for the section; it's just that there's nothing
288 to read in from the object file. */
289 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
291 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
292 if (bed->plt_readonly)
293 pltflags |= SEC_READONLY;
295 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
297 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
300 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
302 if (bed->want_plt_sym)
304 h = _bfd_elf_define_linkage_sym (abfd, info, s,
305 "_PROCEDURE_LINKAGE_TABLE_");
306 elf_hash_table (info)->hplt = h;
311 s = bfd_make_section_with_flags (abfd,
312 (bed->default_use_rela_p
313 ? ".rela.plt" : ".rel.plt"),
314 flags | SEC_READONLY);
316 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
319 if (! _bfd_elf_create_got_section (abfd, info))
322 if (bed->want_dynbss)
324 /* The .dynbss section is a place to put symbols which are defined
325 by dynamic objects, are referenced by regular objects, and are
326 not functions. We must allocate space for them in the process
327 image and use a R_*_COPY reloc to tell the dynamic linker to
328 initialize them at run time. The linker script puts the .dynbss
329 section into the .bss section of the final image. */
330 s = bfd_make_section_with_flags (abfd, ".dynbss",
332 | SEC_LINKER_CREATED));
336 /* The .rel[a].bss section holds copy relocs. This section is not
337 normally needed. We need to create it here, though, so that the
338 linker will map it to an output section. We can't just create it
339 only if we need it, because we will not know whether we need it
340 until we have seen all the input files, and the first time the
341 main linker code calls BFD after examining all the input files
342 (size_dynamic_sections) the input sections have already been
343 mapped to the output sections. If the section turns out not to
344 be needed, we can discard it later. We will never need this
345 section when generating a shared object, since they do not use
349 s = bfd_make_section_with_flags (abfd,
350 (bed->default_use_rela_p
351 ? ".rela.bss" : ".rel.bss"),
352 flags | SEC_READONLY);
354 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
362 /* Record a new dynamic symbol. We record the dynamic symbols as we
363 read the input files, since we need to have a list of all of them
364 before we can determine the final sizes of the output sections.
365 Note that we may actually call this function even though we are not
366 going to output any dynamic symbols; in some cases we know that a
367 symbol should be in the dynamic symbol table, but only if there is
371 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
372 struct elf_link_hash_entry *h)
374 if (h->dynindx == -1)
376 struct elf_strtab_hash *dynstr;
381 /* XXX: The ABI draft says the linker must turn hidden and
382 internal symbols into STB_LOCAL symbols when producing the
383 DSO. However, if ld.so honors st_other in the dynamic table,
384 this would not be necessary. */
385 switch (ELF_ST_VISIBILITY (h->other))
389 if (h->root.type != bfd_link_hash_undefined
390 && h->root.type != bfd_link_hash_undefweak)
393 if (!elf_hash_table (info)->is_relocatable_executable)
401 h->dynindx = elf_hash_table (info)->dynsymcount;
402 ++elf_hash_table (info)->dynsymcount;
404 dynstr = elf_hash_table (info)->dynstr;
407 /* Create a strtab to hold the dynamic symbol names. */
408 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
413 /* We don't put any version information in the dynamic string
415 name = h->root.root.string;
416 p = strchr (name, ELF_VER_CHR);
418 /* We know that the p points into writable memory. In fact,
419 there are only a few symbols that have read-only names, being
420 those like _GLOBAL_OFFSET_TABLE_ that are created specially
421 by the backends. Most symbols will have names pointing into
422 an ELF string table read from a file, or to objalloc memory. */
425 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
430 if (indx == (bfd_size_type) -1)
432 h->dynstr_index = indx;
438 /* Mark a symbol dynamic. */
441 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
442 struct elf_link_hash_entry *h,
443 Elf_Internal_Sym *sym)
445 struct bfd_elf_dynamic_list *d = info->dynamic_list;
447 /* It may be called more than once on the same H. */
448 if(h->dynamic || info->relocatable)
451 if ((info->dynamic_data
452 && (h->type == STT_OBJECT
454 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
456 && h->root.type == bfd_link_hash_new
457 && (*d->match) (&d->head, NULL, h->root.root.string)))
461 /* Record an assignment to a symbol made by a linker script. We need
462 this in case some dynamic object refers to this symbol. */
465 bfd_elf_record_link_assignment (bfd *output_bfd,
466 struct bfd_link_info *info,
471 struct elf_link_hash_entry *h, *hv;
472 struct elf_link_hash_table *htab;
473 const struct elf_backend_data *bed;
475 if (!is_elf_hash_table (info->hash))
478 htab = elf_hash_table (info);
479 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
483 switch (h->root.type)
485 case bfd_link_hash_defined:
486 case bfd_link_hash_defweak:
487 case bfd_link_hash_common:
489 case bfd_link_hash_undefweak:
490 case bfd_link_hash_undefined:
491 /* Since we're defining the symbol, don't let it seem to have not
492 been defined. record_dynamic_symbol and size_dynamic_sections
493 may depend on this. */
494 h->root.type = bfd_link_hash_new;
495 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
496 bfd_link_repair_undef_list (&htab->root);
498 case bfd_link_hash_new:
499 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
502 case bfd_link_hash_indirect:
503 /* We had a versioned symbol in a dynamic library. We make the
504 the versioned symbol point to this one. */
505 bed = get_elf_backend_data (output_bfd);
507 while (hv->root.type == bfd_link_hash_indirect
508 || hv->root.type == bfd_link_hash_warning)
509 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
510 /* We don't need to update h->root.u since linker will set them
512 h->root.type = bfd_link_hash_undefined;
513 hv->root.type = bfd_link_hash_indirect;
514 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
515 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
517 case bfd_link_hash_warning:
522 /* If this symbol is being provided by the linker script, and it is
523 currently defined by a dynamic object, but not by a regular
524 object, then mark it as undefined so that the generic linker will
525 force the correct value. */
529 h->root.type = bfd_link_hash_undefined;
531 /* If this symbol is not being provided by the linker script, and it is
532 currently defined by a dynamic object, but not by a regular object,
533 then clear out any version information because the symbol will not be
534 associated with the dynamic object any more. */
538 h->verinfo.verdef = NULL;
542 if (provide && hidden)
544 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
546 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
547 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
550 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
552 if (!info->relocatable
554 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
555 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
561 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
564 if (! bfd_elf_link_record_dynamic_symbol (info, h))
567 /* If this is a weak defined symbol, and we know a corresponding
568 real symbol from the same dynamic object, make sure the real
569 symbol is also made into a dynamic symbol. */
570 if (h->u.weakdef != NULL
571 && h->u.weakdef->dynindx == -1)
573 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
581 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
582 success, and 2 on a failure caused by attempting to record a symbol
583 in a discarded section, eg. a discarded link-once section symbol. */
586 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
591 struct elf_link_local_dynamic_entry *entry;
592 struct elf_link_hash_table *eht;
593 struct elf_strtab_hash *dynstr;
594 unsigned long dynstr_index;
596 Elf_External_Sym_Shndx eshndx;
597 char esym[sizeof (Elf64_External_Sym)];
599 if (! is_elf_hash_table (info->hash))
602 /* See if the entry exists already. */
603 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
604 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
607 amt = sizeof (*entry);
608 entry = bfd_alloc (input_bfd, amt);
612 /* Go find the symbol, so that we can find it's name. */
613 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
614 1, input_indx, &entry->isym, esym, &eshndx))
616 bfd_release (input_bfd, entry);
620 if (entry->isym.st_shndx != SHN_UNDEF
621 && (entry->isym.st_shndx < SHN_LORESERVE
622 || entry->isym.st_shndx > SHN_HIRESERVE))
626 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
627 if (s == NULL || bfd_is_abs_section (s->output_section))
629 /* We can still bfd_release here as nothing has done another
630 bfd_alloc. We can't do this later in this function. */
631 bfd_release (input_bfd, entry);
636 name = (bfd_elf_string_from_elf_section
637 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
638 entry->isym.st_name));
640 dynstr = elf_hash_table (info)->dynstr;
643 /* Create a strtab to hold the dynamic symbol names. */
644 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
649 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
650 if (dynstr_index == (unsigned long) -1)
652 entry->isym.st_name = dynstr_index;
654 eht = elf_hash_table (info);
656 entry->next = eht->dynlocal;
657 eht->dynlocal = entry;
658 entry->input_bfd = input_bfd;
659 entry->input_indx = input_indx;
662 /* Whatever binding the symbol had before, it's now local. */
664 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
666 /* The dynindx will be set at the end of size_dynamic_sections. */
671 /* Return the dynindex of a local dynamic symbol. */
674 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
678 struct elf_link_local_dynamic_entry *e;
680 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
681 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
686 /* This function is used to renumber the dynamic symbols, if some of
687 them are removed because they are marked as local. This is called
688 via elf_link_hash_traverse. */
691 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
694 size_t *count = data;
696 if (h->root.type == bfd_link_hash_warning)
697 h = (struct elf_link_hash_entry *) h->root.u.i.link;
702 if (h->dynindx != -1)
703 h->dynindx = ++(*count);
709 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
710 STB_LOCAL binding. */
713 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
716 size_t *count = data;
718 if (h->root.type == bfd_link_hash_warning)
719 h = (struct elf_link_hash_entry *) h->root.u.i.link;
721 if (!h->forced_local)
724 if (h->dynindx != -1)
725 h->dynindx = ++(*count);
730 /* Return true if the dynamic symbol for a given section should be
731 omitted when creating a shared library. */
733 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
734 struct bfd_link_info *info,
737 struct elf_link_hash_table *htab;
739 switch (elf_section_data (p)->this_hdr.sh_type)
743 /* If sh_type is yet undecided, assume it could be
744 SHT_PROGBITS/SHT_NOBITS. */
746 htab = elf_hash_table (info);
747 if (p == htab->tls_sec)
750 if (htab->text_index_section != NULL)
751 return p != htab->text_index_section && p != htab->data_index_section;
753 if (strcmp (p->name, ".got") == 0
754 || strcmp (p->name, ".got.plt") == 0
755 || strcmp (p->name, ".plt") == 0)
759 if (htab->dynobj != NULL
760 && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
761 && (ip->flags & SEC_LINKER_CREATED)
762 && ip->output_section == p)
767 /* There shouldn't be section relative relocations
768 against any other section. */
774 /* Assign dynsym indices. In a shared library we generate a section
775 symbol for each output section, which come first. Next come symbols
776 which have been forced to local binding. Then all of the back-end
777 allocated local dynamic syms, followed by the rest of the global
781 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
782 struct bfd_link_info *info,
783 unsigned long *section_sym_count)
785 unsigned long dynsymcount = 0;
787 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
789 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
791 for (p = output_bfd->sections; p ; p = p->next)
792 if ((p->flags & SEC_EXCLUDE) == 0
793 && (p->flags & SEC_ALLOC) != 0
794 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
795 elf_section_data (p)->dynindx = ++dynsymcount;
797 elf_section_data (p)->dynindx = 0;
799 *section_sym_count = dynsymcount;
801 elf_link_hash_traverse (elf_hash_table (info),
802 elf_link_renumber_local_hash_table_dynsyms,
805 if (elf_hash_table (info)->dynlocal)
807 struct elf_link_local_dynamic_entry *p;
808 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
809 p->dynindx = ++dynsymcount;
812 elf_link_hash_traverse (elf_hash_table (info),
813 elf_link_renumber_hash_table_dynsyms,
816 /* There is an unused NULL entry at the head of the table which
817 we must account for in our count. Unless there weren't any
818 symbols, which means we'll have no table at all. */
819 if (dynsymcount != 0)
822 elf_hash_table (info)->dynsymcount = dynsymcount;
826 /* This function is called when we want to define a new symbol. It
827 handles the various cases which arise when we find a definition in
828 a dynamic object, or when there is already a definition in a
829 dynamic object. The new symbol is described by NAME, SYM, PSEC,
830 and PVALUE. We set SYM_HASH to the hash table entry. We set
831 OVERRIDE if the old symbol is overriding a new definition. We set
832 TYPE_CHANGE_OK if it is OK for the type to change. We set
833 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
834 change, we mean that we shouldn't warn if the type or size does
835 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
836 object is overridden by a regular object. */
839 _bfd_elf_merge_symbol (bfd *abfd,
840 struct bfd_link_info *info,
842 Elf_Internal_Sym *sym,
845 unsigned int *pold_alignment,
846 struct elf_link_hash_entry **sym_hash,
848 bfd_boolean *override,
849 bfd_boolean *type_change_ok,
850 bfd_boolean *size_change_ok)
852 asection *sec, *oldsec;
853 struct elf_link_hash_entry *h;
854 struct elf_link_hash_entry *flip;
857 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
858 bfd_boolean newweak, oldweak;
859 const struct elf_backend_data *bed;
865 bind = ELF_ST_BIND (sym->st_info);
867 /* Silently discard TLS symbols from --just-syms. There's no way to
868 combine a static TLS block with a new TLS block for this executable. */
869 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
870 && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
876 if (! bfd_is_und_section (sec))
877 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
879 h = ((struct elf_link_hash_entry *)
880 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
885 /* This code is for coping with dynamic objects, and is only useful
886 if we are doing an ELF link. */
887 if (info->hash->creator != abfd->xvec)
890 /* For merging, we only care about real symbols. */
892 while (h->root.type == bfd_link_hash_indirect
893 || h->root.type == bfd_link_hash_warning)
894 h = (struct elf_link_hash_entry *) h->root.u.i.link;
896 /* We have to check it for every instance since the first few may be
897 refereences and not all compilers emit symbol type for undefined
899 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
901 /* If we just created the symbol, mark it as being an ELF symbol.
902 Other than that, there is nothing to do--there is no merge issue
903 with a newly defined symbol--so we just return. */
905 if (h->root.type == bfd_link_hash_new)
911 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
914 switch (h->root.type)
921 case bfd_link_hash_undefined:
922 case bfd_link_hash_undefweak:
923 oldbfd = h->root.u.undef.abfd;
927 case bfd_link_hash_defined:
928 case bfd_link_hash_defweak:
929 oldbfd = h->root.u.def.section->owner;
930 oldsec = h->root.u.def.section;
933 case bfd_link_hash_common:
934 oldbfd = h->root.u.c.p->section->owner;
935 oldsec = h->root.u.c.p->section;
939 /* In cases involving weak versioned symbols, we may wind up trying
940 to merge a symbol with itself. Catch that here, to avoid the
941 confusion that results if we try to override a symbol with
942 itself. The additional tests catch cases like
943 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
944 dynamic object, which we do want to handle here. */
946 && ((abfd->flags & DYNAMIC) == 0
950 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
951 respectively, is from a dynamic object. */
953 newdyn = (abfd->flags & DYNAMIC) != 0;
957 olddyn = (oldbfd->flags & DYNAMIC) != 0;
958 else if (oldsec != NULL)
960 /* This handles the special SHN_MIPS_{TEXT,DATA} section
961 indices used by MIPS ELF. */
962 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
965 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
966 respectively, appear to be a definition rather than reference. */
968 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
970 olddef = (h->root.type != bfd_link_hash_undefined
971 && h->root.type != bfd_link_hash_undefweak
972 && h->root.type != bfd_link_hash_common);
974 bed = get_elf_backend_data (abfd);
975 /* When we try to create a default indirect symbol from the dynamic
976 definition with the default version, we skip it if its type and
977 the type of existing regular definition mismatch. We only do it
978 if the existing regular definition won't be dynamic. */
979 if (pold_alignment == NULL
981 && !info->export_dynamic
986 && (olddef || h->root.type == bfd_link_hash_common)
987 && ELF_ST_TYPE (sym->st_info) != h->type
988 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
989 && h->type != STT_NOTYPE
990 && !(bed->is_function_type (ELF_ST_TYPE (sym->st_info))
991 && bed->is_function_type (h->type)))
997 /* Check TLS symbol. We don't check undefined symbol introduced by
999 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
1000 && ELF_ST_TYPE (sym->st_info) != h->type
1004 bfd_boolean ntdef, tdef;
1005 asection *ntsec, *tsec;
1007 if (h->type == STT_TLS)
1027 (*_bfd_error_handler)
1028 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1029 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1030 else if (!tdef && !ntdef)
1031 (*_bfd_error_handler)
1032 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1033 tbfd, ntbfd, h->root.root.string);
1035 (*_bfd_error_handler)
1036 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1037 tbfd, tsec, ntbfd, h->root.root.string);
1039 (*_bfd_error_handler)
1040 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1041 tbfd, ntbfd, ntsec, h->root.root.string);
1043 bfd_set_error (bfd_error_bad_value);
1047 /* We need to remember if a symbol has a definition in a dynamic
1048 object or is weak in all dynamic objects. Internal and hidden
1049 visibility will make it unavailable to dynamic objects. */
1050 if (newdyn && !h->dynamic_def)
1052 if (!bfd_is_und_section (sec))
1056 /* Check if this symbol is weak in all dynamic objects. If it
1057 is the first time we see it in a dynamic object, we mark
1058 if it is weak. Otherwise, we clear it. */
1059 if (!h->ref_dynamic)
1061 if (bind == STB_WEAK)
1062 h->dynamic_weak = 1;
1064 else if (bind != STB_WEAK)
1065 h->dynamic_weak = 0;
1069 /* If the old symbol has non-default visibility, we ignore the new
1070 definition from a dynamic object. */
1072 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1073 && !bfd_is_und_section (sec))
1076 /* Make sure this symbol is dynamic. */
1078 /* A protected symbol has external availability. Make sure it is
1079 recorded as dynamic.
1081 FIXME: Should we check type and size for protected symbol? */
1082 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1083 return bfd_elf_link_record_dynamic_symbol (info, h);
1088 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1091 /* If the new symbol with non-default visibility comes from a
1092 relocatable file and the old definition comes from a dynamic
1093 object, we remove the old definition. */
1094 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1096 /* Handle the case where the old dynamic definition is
1097 default versioned. We need to copy the symbol info from
1098 the symbol with default version to the normal one if it
1099 was referenced before. */
1102 const struct elf_backend_data *bed
1103 = get_elf_backend_data (abfd);
1104 struct elf_link_hash_entry *vh = *sym_hash;
1105 vh->root.type = h->root.type;
1106 h->root.type = bfd_link_hash_indirect;
1107 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1108 /* Protected symbols will override the dynamic definition
1109 with default version. */
1110 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1112 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1113 vh->dynamic_def = 1;
1114 vh->ref_dynamic = 1;
1118 h->root.type = vh->root.type;
1119 vh->ref_dynamic = 0;
1120 /* We have to hide it here since it was made dynamic
1121 global with extra bits when the symbol info was
1122 copied from the old dynamic definition. */
1123 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1131 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1132 && bfd_is_und_section (sec))
1134 /* If the new symbol is undefined and the old symbol was
1135 also undefined before, we need to make sure
1136 _bfd_generic_link_add_one_symbol doesn't mess
1137 up the linker hash table undefs list. Since the old
1138 definition came from a dynamic object, it is still on the
1140 h->root.type = bfd_link_hash_undefined;
1141 h->root.u.undef.abfd = abfd;
1145 h->root.type = bfd_link_hash_new;
1146 h->root.u.undef.abfd = NULL;
1155 /* FIXME: Should we check type and size for protected symbol? */
1161 /* Differentiate strong and weak symbols. */
1162 newweak = bind == STB_WEAK;
1163 oldweak = (h->root.type == bfd_link_hash_defweak
1164 || h->root.type == bfd_link_hash_undefweak);
1166 /* If a new weak symbol definition comes from a regular file and the
1167 old symbol comes from a dynamic library, we treat the new one as
1168 strong. Similarly, an old weak symbol definition from a regular
1169 file is treated as strong when the new symbol comes from a dynamic
1170 library. Further, an old weak symbol from a dynamic library is
1171 treated as strong if the new symbol is from a dynamic library.
1172 This reflects the way glibc's ld.so works.
1174 Do this before setting *type_change_ok or *size_change_ok so that
1175 we warn properly when dynamic library symbols are overridden. */
1177 if (newdef && !newdyn && olddyn)
1179 if (olddef && newdyn)
1182 /* Allow changes between different types of funciton symbol. */
1183 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))
1184 && bed->is_function_type (h->type))
1185 *type_change_ok = TRUE;
1187 /* It's OK to change the type if either the existing symbol or the
1188 new symbol is weak. A type change is also OK if the old symbol
1189 is undefined and the new symbol is defined. */
1194 && h->root.type == bfd_link_hash_undefined))
1195 *type_change_ok = TRUE;
1197 /* It's OK to change the size if either the existing symbol or the
1198 new symbol is weak, or if the old symbol is undefined. */
1201 || h->root.type == bfd_link_hash_undefined)
1202 *size_change_ok = TRUE;
1204 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1205 symbol, respectively, appears to be a common symbol in a dynamic
1206 object. If a symbol appears in an uninitialized section, and is
1207 not weak, and is not a function, then it may be a common symbol
1208 which was resolved when the dynamic object was created. We want
1209 to treat such symbols specially, because they raise special
1210 considerations when setting the symbol size: if the symbol
1211 appears as a common symbol in a regular object, and the size in
1212 the regular object is larger, we must make sure that we use the
1213 larger size. This problematic case can always be avoided in C,
1214 but it must be handled correctly when using Fortran shared
1217 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1218 likewise for OLDDYNCOMMON and OLDDEF.
1220 Note that this test is just a heuristic, and that it is quite
1221 possible to have an uninitialized symbol in a shared object which
1222 is really a definition, rather than a common symbol. This could
1223 lead to some minor confusion when the symbol really is a common
1224 symbol in some regular object. However, I think it will be
1230 && (sec->flags & SEC_ALLOC) != 0
1231 && (sec->flags & SEC_LOAD) == 0
1233 && !bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
1234 newdyncommon = TRUE;
1236 newdyncommon = FALSE;
1240 && h->root.type == bfd_link_hash_defined
1242 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1243 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1245 && !bed->is_function_type (h->type))
1246 olddyncommon = TRUE;
1248 olddyncommon = FALSE;
1250 /* We now know everything about the old and new symbols. We ask the
1251 backend to check if we can merge them. */
1252 if (bed->merge_symbol
1253 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1254 pold_alignment, skip, override,
1255 type_change_ok, size_change_ok,
1256 &newdyn, &newdef, &newdyncommon, &newweak,
1258 &olddyn, &olddef, &olddyncommon, &oldweak,
1262 /* If both the old and the new symbols look like common symbols in a
1263 dynamic object, set the size of the symbol to the larger of the
1268 && sym->st_size != h->size)
1270 /* Since we think we have two common symbols, issue a multiple
1271 common warning if desired. Note that we only warn if the
1272 size is different. If the size is the same, we simply let
1273 the old symbol override the new one as normally happens with
1274 symbols defined in dynamic objects. */
1276 if (! ((*info->callbacks->multiple_common)
1277 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1278 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1281 if (sym->st_size > h->size)
1282 h->size = sym->st_size;
1284 *size_change_ok = TRUE;
1287 /* If we are looking at a dynamic object, and we have found a
1288 definition, we need to see if the symbol was already defined by
1289 some other object. If so, we want to use the existing
1290 definition, and we do not want to report a multiple symbol
1291 definition error; we do this by clobbering *PSEC to be
1292 bfd_und_section_ptr.
1294 We treat a common symbol as a definition if the symbol in the
1295 shared library is a function, since common symbols always
1296 represent variables; this can cause confusion in principle, but
1297 any such confusion would seem to indicate an erroneous program or
1298 shared library. We also permit a common symbol in a regular
1299 object to override a weak symbol in a shared object. */
1304 || (h->root.type == bfd_link_hash_common
1306 || bed->is_function_type (ELF_ST_TYPE (sym->st_info))))))
1310 newdyncommon = FALSE;
1312 *psec = sec = bfd_und_section_ptr;
1313 *size_change_ok = TRUE;
1315 /* If we get here when the old symbol is a common symbol, then
1316 we are explicitly letting it override a weak symbol or
1317 function in a dynamic object, and we don't want to warn about
1318 a type change. If the old symbol is a defined symbol, a type
1319 change warning may still be appropriate. */
1321 if (h->root.type == bfd_link_hash_common)
1322 *type_change_ok = TRUE;
1325 /* Handle the special case of an old common symbol merging with a
1326 new symbol which looks like a common symbol in a shared object.
1327 We change *PSEC and *PVALUE to make the new symbol look like a
1328 common symbol, and let _bfd_generic_link_add_one_symbol do the
1332 && h->root.type == bfd_link_hash_common)
1336 newdyncommon = FALSE;
1337 *pvalue = sym->st_size;
1338 *psec = sec = bed->common_section (oldsec);
1339 *size_change_ok = TRUE;
1342 /* Skip weak definitions of symbols that are already defined. */
1343 if (newdef && olddef && newweak)
1346 /* If the old symbol is from a dynamic object, and the new symbol is
1347 a definition which is not from a dynamic object, then the new
1348 symbol overrides the old symbol. Symbols from regular files
1349 always take precedence over symbols from dynamic objects, even if
1350 they are defined after the dynamic object in the link.
1352 As above, we again permit a common symbol in a regular object to
1353 override a definition in a shared object if the shared object
1354 symbol is a function or is weak. */
1359 || (bfd_is_com_section (sec)
1361 || bed->is_function_type (h->type))))
1366 /* Change the hash table entry to undefined, and let
1367 _bfd_generic_link_add_one_symbol do the right thing with the
1370 h->root.type = bfd_link_hash_undefined;
1371 h->root.u.undef.abfd = h->root.u.def.section->owner;
1372 *size_change_ok = TRUE;
1375 olddyncommon = FALSE;
1377 /* We again permit a type change when a common symbol may be
1378 overriding a function. */
1380 if (bfd_is_com_section (sec))
1381 *type_change_ok = TRUE;
1383 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1386 /* This union may have been set to be non-NULL when this symbol
1387 was seen in a dynamic object. We must force the union to be
1388 NULL, so that it is correct for a regular symbol. */
1389 h->verinfo.vertree = NULL;
1392 /* Handle the special case of a new common symbol merging with an
1393 old symbol that looks like it might be a common symbol defined in
1394 a shared object. Note that we have already handled the case in
1395 which a new common symbol should simply override the definition
1396 in the shared library. */
1399 && bfd_is_com_section (sec)
1402 /* It would be best if we could set the hash table entry to a
1403 common symbol, but we don't know what to use for the section
1404 or the alignment. */
1405 if (! ((*info->callbacks->multiple_common)
1406 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1407 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1410 /* If the presumed common symbol in the dynamic object is
1411 larger, pretend that the new symbol has its size. */
1413 if (h->size > *pvalue)
1416 /* We need to remember the alignment required by the symbol
1417 in the dynamic object. */
1418 BFD_ASSERT (pold_alignment);
1419 *pold_alignment = h->root.u.def.section->alignment_power;
1422 olddyncommon = FALSE;
1424 h->root.type = bfd_link_hash_undefined;
1425 h->root.u.undef.abfd = h->root.u.def.section->owner;
1427 *size_change_ok = TRUE;
1428 *type_change_ok = TRUE;
1430 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1433 h->verinfo.vertree = NULL;
1438 /* Handle the case where we had a versioned symbol in a dynamic
1439 library and now find a definition in a normal object. In this
1440 case, we make the versioned symbol point to the normal one. */
1441 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1442 flip->root.type = h->root.type;
1443 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1444 h->root.type = bfd_link_hash_indirect;
1445 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1446 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1450 flip->ref_dynamic = 1;
1457 /* This function is called to create an indirect symbol from the
1458 default for the symbol with the default version if needed. The
1459 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1460 set DYNSYM if the new indirect symbol is dynamic. */
1463 _bfd_elf_add_default_symbol (bfd *abfd,
1464 struct bfd_link_info *info,
1465 struct elf_link_hash_entry *h,
1467 Elf_Internal_Sym *sym,
1470 bfd_boolean *dynsym,
1471 bfd_boolean override)
1473 bfd_boolean type_change_ok;
1474 bfd_boolean size_change_ok;
1477 struct elf_link_hash_entry *hi;
1478 struct bfd_link_hash_entry *bh;
1479 const struct elf_backend_data *bed;
1480 bfd_boolean collect;
1481 bfd_boolean dynamic;
1483 size_t len, shortlen;
1486 /* If this symbol has a version, and it is the default version, we
1487 create an indirect symbol from the default name to the fully
1488 decorated name. This will cause external references which do not
1489 specify a version to be bound to this version of the symbol. */
1490 p = strchr (name, ELF_VER_CHR);
1491 if (p == NULL || p[1] != ELF_VER_CHR)
1496 /* We are overridden by an old definition. We need to check if we
1497 need to create the indirect symbol from the default name. */
1498 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1500 BFD_ASSERT (hi != NULL);
1503 while (hi->root.type == bfd_link_hash_indirect
1504 || hi->root.type == bfd_link_hash_warning)
1506 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1512 bed = get_elf_backend_data (abfd);
1513 collect = bed->collect;
1514 dynamic = (abfd->flags & DYNAMIC) != 0;
1516 shortlen = p - name;
1517 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1518 if (shortname == NULL)
1520 memcpy (shortname, name, shortlen);
1521 shortname[shortlen] = '\0';
1523 /* We are going to create a new symbol. Merge it with any existing
1524 symbol with this name. For the purposes of the merge, act as
1525 though we were defining the symbol we just defined, although we
1526 actually going to define an indirect symbol. */
1527 type_change_ok = FALSE;
1528 size_change_ok = FALSE;
1530 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1531 NULL, &hi, &skip, &override,
1532 &type_change_ok, &size_change_ok))
1541 if (! (_bfd_generic_link_add_one_symbol
1542 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1543 0, name, FALSE, collect, &bh)))
1545 hi = (struct elf_link_hash_entry *) bh;
1549 /* In this case the symbol named SHORTNAME is overriding the
1550 indirect symbol we want to add. We were planning on making
1551 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1552 is the name without a version. NAME is the fully versioned
1553 name, and it is the default version.
1555 Overriding means that we already saw a definition for the
1556 symbol SHORTNAME in a regular object, and it is overriding
1557 the symbol defined in the dynamic object.
1559 When this happens, we actually want to change NAME, the
1560 symbol we just added, to refer to SHORTNAME. This will cause
1561 references to NAME in the shared object to become references
1562 to SHORTNAME in the regular object. This is what we expect
1563 when we override a function in a shared object: that the
1564 references in the shared object will be mapped to the
1565 definition in the regular object. */
1567 while (hi->root.type == bfd_link_hash_indirect
1568 || hi->root.type == bfd_link_hash_warning)
1569 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1571 h->root.type = bfd_link_hash_indirect;
1572 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1576 hi->ref_dynamic = 1;
1580 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1585 /* Now set HI to H, so that the following code will set the
1586 other fields correctly. */
1590 /* Check if HI is a warning symbol. */
1591 if (hi->root.type == bfd_link_hash_warning)
1592 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1594 /* If there is a duplicate definition somewhere, then HI may not
1595 point to an indirect symbol. We will have reported an error to
1596 the user in that case. */
1598 if (hi->root.type == bfd_link_hash_indirect)
1600 struct elf_link_hash_entry *ht;
1602 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1603 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1605 /* See if the new flags lead us to realize that the symbol must
1617 if (hi->ref_regular)
1623 /* We also need to define an indirection from the nondefault version
1627 len = strlen (name);
1628 shortname = bfd_hash_allocate (&info->hash->table, len);
1629 if (shortname == NULL)
1631 memcpy (shortname, name, shortlen);
1632 memcpy (shortname + shortlen, p + 1, len - shortlen);
1634 /* Once again, merge with any existing symbol. */
1635 type_change_ok = FALSE;
1636 size_change_ok = FALSE;
1638 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1639 NULL, &hi, &skip, &override,
1640 &type_change_ok, &size_change_ok))
1648 /* Here SHORTNAME is a versioned name, so we don't expect to see
1649 the type of override we do in the case above unless it is
1650 overridden by a versioned definition. */
1651 if (hi->root.type != bfd_link_hash_defined
1652 && hi->root.type != bfd_link_hash_defweak)
1653 (*_bfd_error_handler)
1654 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1660 if (! (_bfd_generic_link_add_one_symbol
1661 (info, abfd, shortname, BSF_INDIRECT,
1662 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1664 hi = (struct elf_link_hash_entry *) bh;
1666 /* If there is a duplicate definition somewhere, then HI may not
1667 point to an indirect symbol. We will have reported an error
1668 to the user in that case. */
1670 if (hi->root.type == bfd_link_hash_indirect)
1672 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1674 /* See if the new flags lead us to realize that the symbol
1686 if (hi->ref_regular)
1696 /* This routine is used to export all defined symbols into the dynamic
1697 symbol table. It is called via elf_link_hash_traverse. */
1700 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1702 struct elf_info_failed *eif = data;
1704 /* Ignore this if we won't export it. */
1705 if (!eif->info->export_dynamic && !h->dynamic)
1708 /* Ignore indirect symbols. These are added by the versioning code. */
1709 if (h->root.type == bfd_link_hash_indirect)
1712 if (h->root.type == bfd_link_hash_warning)
1713 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1715 if (h->dynindx == -1
1719 struct bfd_elf_version_tree *t;
1720 struct bfd_elf_version_expr *d;
1722 for (t = eif->verdefs; t != NULL; t = t->next)
1724 if (t->globals.list != NULL)
1726 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1731 if (t->locals.list != NULL)
1733 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1742 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1753 /* Look through the symbols which are defined in other shared
1754 libraries and referenced here. Update the list of version
1755 dependencies. This will be put into the .gnu.version_r section.
1756 This function is called via elf_link_hash_traverse. */
1759 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1762 struct elf_find_verdep_info *rinfo = data;
1763 Elf_Internal_Verneed *t;
1764 Elf_Internal_Vernaux *a;
1767 if (h->root.type == bfd_link_hash_warning)
1768 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1770 /* We only care about symbols defined in shared objects with version
1775 || h->verinfo.verdef == NULL)
1778 /* See if we already know about this version. */
1779 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1781 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1784 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1785 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1791 /* This is a new version. Add it to tree we are building. */
1796 t = bfd_zalloc (rinfo->output_bfd, amt);
1799 rinfo->failed = TRUE;
1803 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1804 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1805 elf_tdata (rinfo->output_bfd)->verref = t;
1809 a = bfd_zalloc (rinfo->output_bfd, amt);
1811 /* Note that we are copying a string pointer here, and testing it
1812 above. If bfd_elf_string_from_elf_section is ever changed to
1813 discard the string data when low in memory, this will have to be
1815 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1817 a->vna_flags = h->verinfo.verdef->vd_flags;
1818 a->vna_nextptr = t->vn_auxptr;
1820 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1823 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1830 /* Figure out appropriate versions for all the symbols. We may not
1831 have the version number script until we have read all of the input
1832 files, so until that point we don't know which symbols should be
1833 local. This function is called via elf_link_hash_traverse. */
1836 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1838 struct elf_assign_sym_version_info *sinfo;
1839 struct bfd_link_info *info;
1840 const struct elf_backend_data *bed;
1841 struct elf_info_failed eif;
1848 if (h->root.type == bfd_link_hash_warning)
1849 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1851 /* Fix the symbol flags. */
1854 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1857 sinfo->failed = TRUE;
1861 /* We only need version numbers for symbols defined in regular
1863 if (!h->def_regular)
1866 bed = get_elf_backend_data (sinfo->output_bfd);
1867 p = strchr (h->root.root.string, ELF_VER_CHR);
1868 if (p != NULL && h->verinfo.vertree == NULL)
1870 struct bfd_elf_version_tree *t;
1875 /* There are two consecutive ELF_VER_CHR characters if this is
1876 not a hidden symbol. */
1878 if (*p == ELF_VER_CHR)
1884 /* If there is no version string, we can just return out. */
1892 /* Look for the version. If we find it, it is no longer weak. */
1893 for (t = sinfo->verdefs; t != NULL; t = t->next)
1895 if (strcmp (t->name, p) == 0)
1899 struct bfd_elf_version_expr *d;
1901 len = p - h->root.root.string;
1902 alc = bfd_malloc (len);
1905 memcpy (alc, h->root.root.string, len - 1);
1906 alc[len - 1] = '\0';
1907 if (alc[len - 2] == ELF_VER_CHR)
1908 alc[len - 2] = '\0';
1910 h->verinfo.vertree = t;
1914 if (t->globals.list != NULL)
1915 d = (*t->match) (&t->globals, NULL, alc);
1917 /* See if there is anything to force this symbol to
1919 if (d == NULL && t->locals.list != NULL)
1921 d = (*t->match) (&t->locals, NULL, alc);
1924 && ! info->export_dynamic)
1925 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1933 /* If we are building an application, we need to create a
1934 version node for this version. */
1935 if (t == NULL && info->executable)
1937 struct bfd_elf_version_tree **pp;
1940 /* If we aren't going to export this symbol, we don't need
1941 to worry about it. */
1942 if (h->dynindx == -1)
1946 t = bfd_zalloc (sinfo->output_bfd, amt);
1949 sinfo->failed = TRUE;
1954 t->name_indx = (unsigned int) -1;
1958 /* Don't count anonymous version tag. */
1959 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1961 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1963 t->vernum = version_index;
1967 h->verinfo.vertree = t;
1971 /* We could not find the version for a symbol when
1972 generating a shared archive. Return an error. */
1973 (*_bfd_error_handler)
1974 (_("%B: version node not found for symbol %s"),
1975 sinfo->output_bfd, h->root.root.string);
1976 bfd_set_error (bfd_error_bad_value);
1977 sinfo->failed = TRUE;
1985 /* If we don't have a version for this symbol, see if we can find
1987 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1989 struct bfd_elf_version_tree *t;
1990 struct bfd_elf_version_tree *local_ver;
1991 struct bfd_elf_version_expr *d;
1993 /* See if can find what version this symbol is in. If the
1994 symbol is supposed to be local, then don't actually register
1997 for (t = sinfo->verdefs; t != NULL; t = t->next)
1999 if (t->globals.list != NULL)
2001 bfd_boolean matched;
2005 while ((d = (*t->match) (&t->globals, d,
2006 h->root.root.string)) != NULL)
2011 /* There is a version without definition. Make
2012 the symbol the default definition for this
2014 h->verinfo.vertree = t;
2022 /* There is no undefined version for this symbol. Hide the
2024 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2027 if (t->locals.list != NULL)
2030 while ((d = (*t->match) (&t->locals, d,
2031 h->root.root.string)) != NULL)
2034 /* If the match is "*", keep looking for a more
2035 explicit, perhaps even global, match.
2036 XXX: Shouldn't this be !d->wildcard instead? */
2037 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
2046 if (local_ver != NULL)
2048 h->verinfo.vertree = local_ver;
2049 if (h->dynindx != -1
2050 && ! info->export_dynamic)
2052 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2060 /* Read and swap the relocs from the section indicated by SHDR. This
2061 may be either a REL or a RELA section. The relocations are
2062 translated into RELA relocations and stored in INTERNAL_RELOCS,
2063 which should have already been allocated to contain enough space.
2064 The EXTERNAL_RELOCS are a buffer where the external form of the
2065 relocations should be stored.
2067 Returns FALSE if something goes wrong. */
2070 elf_link_read_relocs_from_section (bfd *abfd,
2072 Elf_Internal_Shdr *shdr,
2073 void *external_relocs,
2074 Elf_Internal_Rela *internal_relocs)
2076 const struct elf_backend_data *bed;
2077 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2078 const bfd_byte *erela;
2079 const bfd_byte *erelaend;
2080 Elf_Internal_Rela *irela;
2081 Elf_Internal_Shdr *symtab_hdr;
2084 /* Position ourselves at the start of the section. */
2085 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2088 /* Read the relocations. */
2089 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2092 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2093 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
2095 bed = get_elf_backend_data (abfd);
2097 /* Convert the external relocations to the internal format. */
2098 if (shdr->sh_entsize == bed->s->sizeof_rel)
2099 swap_in = bed->s->swap_reloc_in;
2100 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2101 swap_in = bed->s->swap_reloca_in;
2104 bfd_set_error (bfd_error_wrong_format);
2108 erela = external_relocs;
2109 erelaend = erela + shdr->sh_size;
2110 irela = internal_relocs;
2111 while (erela < erelaend)
2115 (*swap_in) (abfd, erela, irela);
2116 r_symndx = ELF32_R_SYM (irela->r_info);
2117 if (bed->s->arch_size == 64)
2119 if ((size_t) r_symndx >= nsyms)
2121 (*_bfd_error_handler)
2122 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2123 " for offset 0x%lx in section `%A'"),
2125 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2126 bfd_set_error (bfd_error_bad_value);
2129 irela += bed->s->int_rels_per_ext_rel;
2130 erela += shdr->sh_entsize;
2136 /* Read and swap the relocs for a section O. They may have been
2137 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2138 not NULL, they are used as buffers to read into. They are known to
2139 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2140 the return value is allocated using either malloc or bfd_alloc,
2141 according to the KEEP_MEMORY argument. If O has two relocation
2142 sections (both REL and RELA relocations), then the REL_HDR
2143 relocations will appear first in INTERNAL_RELOCS, followed by the
2144 REL_HDR2 relocations. */
2147 _bfd_elf_link_read_relocs (bfd *abfd,
2149 void *external_relocs,
2150 Elf_Internal_Rela *internal_relocs,
2151 bfd_boolean keep_memory)
2153 Elf_Internal_Shdr *rel_hdr;
2154 void *alloc1 = NULL;
2155 Elf_Internal_Rela *alloc2 = NULL;
2156 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2158 if (elf_section_data (o)->relocs != NULL)
2159 return elf_section_data (o)->relocs;
2161 if (o->reloc_count == 0)
2164 rel_hdr = &elf_section_data (o)->rel_hdr;
2166 if (internal_relocs == NULL)
2170 size = o->reloc_count;
2171 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2173 internal_relocs = bfd_alloc (abfd, size);
2175 internal_relocs = alloc2 = bfd_malloc (size);
2176 if (internal_relocs == NULL)
2180 if (external_relocs == NULL)
2182 bfd_size_type size = rel_hdr->sh_size;
2184 if (elf_section_data (o)->rel_hdr2)
2185 size += elf_section_data (o)->rel_hdr2->sh_size;
2186 alloc1 = bfd_malloc (size);
2189 external_relocs = alloc1;
2192 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2196 if (elf_section_data (o)->rel_hdr2
2197 && (!elf_link_read_relocs_from_section
2199 elf_section_data (o)->rel_hdr2,
2200 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2201 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2202 * bed->s->int_rels_per_ext_rel))))
2205 /* Cache the results for next time, if we can. */
2207 elf_section_data (o)->relocs = internal_relocs;
2212 /* Don't free alloc2, since if it was allocated we are passing it
2213 back (under the name of internal_relocs). */
2215 return internal_relocs;
2225 /* Compute the size of, and allocate space for, REL_HDR which is the
2226 section header for a section containing relocations for O. */
2229 _bfd_elf_link_size_reloc_section (bfd *abfd,
2230 Elf_Internal_Shdr *rel_hdr,
2233 bfd_size_type reloc_count;
2234 bfd_size_type num_rel_hashes;
2236 /* Figure out how many relocations there will be. */
2237 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2238 reloc_count = elf_section_data (o)->rel_count;
2240 reloc_count = elf_section_data (o)->rel_count2;
2242 num_rel_hashes = o->reloc_count;
2243 if (num_rel_hashes < reloc_count)
2244 num_rel_hashes = reloc_count;
2246 /* That allows us to calculate the size of the section. */
2247 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2249 /* The contents field must last into write_object_contents, so we
2250 allocate it with bfd_alloc rather than malloc. Also since we
2251 cannot be sure that the contents will actually be filled in,
2252 we zero the allocated space. */
2253 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2254 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2257 /* We only allocate one set of hash entries, so we only do it the
2258 first time we are called. */
2259 if (elf_section_data (o)->rel_hashes == NULL
2262 struct elf_link_hash_entry **p;
2264 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2268 elf_section_data (o)->rel_hashes = p;
2274 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2275 originated from the section given by INPUT_REL_HDR) to the
2279 _bfd_elf_link_output_relocs (bfd *output_bfd,
2280 asection *input_section,
2281 Elf_Internal_Shdr *input_rel_hdr,
2282 Elf_Internal_Rela *internal_relocs,
2283 struct elf_link_hash_entry **rel_hash
2286 Elf_Internal_Rela *irela;
2287 Elf_Internal_Rela *irelaend;
2289 Elf_Internal_Shdr *output_rel_hdr;
2290 asection *output_section;
2291 unsigned int *rel_countp = NULL;
2292 const struct elf_backend_data *bed;
2293 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2295 output_section = input_section->output_section;
2296 output_rel_hdr = NULL;
2298 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2299 == input_rel_hdr->sh_entsize)
2301 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2302 rel_countp = &elf_section_data (output_section)->rel_count;
2304 else if (elf_section_data (output_section)->rel_hdr2
2305 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2306 == input_rel_hdr->sh_entsize))
2308 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2309 rel_countp = &elf_section_data (output_section)->rel_count2;
2313 (*_bfd_error_handler)
2314 (_("%B: relocation size mismatch in %B section %A"),
2315 output_bfd, input_section->owner, input_section);
2316 bfd_set_error (bfd_error_wrong_object_format);
2320 bed = get_elf_backend_data (output_bfd);
2321 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2322 swap_out = bed->s->swap_reloc_out;
2323 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2324 swap_out = bed->s->swap_reloca_out;
2328 erel = output_rel_hdr->contents;
2329 erel += *rel_countp * input_rel_hdr->sh_entsize;
2330 irela = internal_relocs;
2331 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2332 * bed->s->int_rels_per_ext_rel);
2333 while (irela < irelaend)
2335 (*swap_out) (output_bfd, irela, erel);
2336 irela += bed->s->int_rels_per_ext_rel;
2337 erel += input_rel_hdr->sh_entsize;
2340 /* Bump the counter, so that we know where to add the next set of
2342 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2347 /* Make weak undefined symbols in PIE dynamic. */
2350 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2351 struct elf_link_hash_entry *h)
2355 && h->root.type == bfd_link_hash_undefweak)
2356 return bfd_elf_link_record_dynamic_symbol (info, h);
2361 /* Fix up the flags for a symbol. This handles various cases which
2362 can only be fixed after all the input files are seen. This is
2363 currently called by both adjust_dynamic_symbol and
2364 assign_sym_version, which is unnecessary but perhaps more robust in
2365 the face of future changes. */
2368 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2369 struct elf_info_failed *eif)
2371 const struct elf_backend_data *bed = NULL;
2373 /* If this symbol was mentioned in a non-ELF file, try to set
2374 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2375 permit a non-ELF file to correctly refer to a symbol defined in
2376 an ELF dynamic object. */
2379 while (h->root.type == bfd_link_hash_indirect)
2380 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2382 if (h->root.type != bfd_link_hash_defined
2383 && h->root.type != bfd_link_hash_defweak)
2386 h->ref_regular_nonweak = 1;
2390 if (h->root.u.def.section->owner != NULL
2391 && (bfd_get_flavour (h->root.u.def.section->owner)
2392 == bfd_target_elf_flavour))
2395 h->ref_regular_nonweak = 1;
2401 if (h->dynindx == -1
2405 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2414 /* Unfortunately, NON_ELF is only correct if the symbol
2415 was first seen in a non-ELF file. Fortunately, if the symbol
2416 was first seen in an ELF file, we're probably OK unless the
2417 symbol was defined in a non-ELF file. Catch that case here.
2418 FIXME: We're still in trouble if the symbol was first seen in
2419 a dynamic object, and then later in a non-ELF regular object. */
2420 if ((h->root.type == bfd_link_hash_defined
2421 || h->root.type == bfd_link_hash_defweak)
2423 && (h->root.u.def.section->owner != NULL
2424 ? (bfd_get_flavour (h->root.u.def.section->owner)
2425 != bfd_target_elf_flavour)
2426 : (bfd_is_abs_section (h->root.u.def.section)
2427 && !h->def_dynamic)))
2431 /* Backend specific symbol fixup. */
2432 if (elf_hash_table (eif->info)->dynobj)
2434 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2435 if (bed->elf_backend_fixup_symbol
2436 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2440 /* If this is a final link, and the symbol was defined as a common
2441 symbol in a regular object file, and there was no definition in
2442 any dynamic object, then the linker will have allocated space for
2443 the symbol in a common section but the DEF_REGULAR
2444 flag will not have been set. */
2445 if (h->root.type == bfd_link_hash_defined
2449 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2452 /* If -Bsymbolic was used (which means to bind references to global
2453 symbols to the definition within the shared object), and this
2454 symbol was defined in a regular object, then it actually doesn't
2455 need a PLT entry. Likewise, if the symbol has non-default
2456 visibility. If the symbol has hidden or internal visibility, we
2457 will force it local. */
2459 && eif->info->shared
2460 && is_elf_hash_table (eif->info->hash)
2461 && (SYMBOLIC_BIND (eif->info, h)
2462 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2465 bfd_boolean force_local;
2467 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2468 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2469 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2472 /* If a weak undefined symbol has non-default visibility, we also
2473 hide it from the dynamic linker. */
2474 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2475 && h->root.type == bfd_link_hash_undefweak)
2477 const struct elf_backend_data *bed;
2478 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2479 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2482 /* If this is a weak defined symbol in a dynamic object, and we know
2483 the real definition in the dynamic object, copy interesting flags
2484 over to the real definition. */
2485 if (h->u.weakdef != NULL)
2487 struct elf_link_hash_entry *weakdef;
2489 weakdef = h->u.weakdef;
2490 if (h->root.type == bfd_link_hash_indirect)
2491 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2493 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2494 || h->root.type == bfd_link_hash_defweak);
2495 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2496 || weakdef->root.type == bfd_link_hash_defweak);
2497 BFD_ASSERT (weakdef->def_dynamic);
2499 /* If the real definition is defined by a regular object file,
2500 don't do anything special. See the longer description in
2501 _bfd_elf_adjust_dynamic_symbol, below. */
2502 if (weakdef->def_regular)
2503 h->u.weakdef = NULL;
2505 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef,
2512 /* Make the backend pick a good value for a dynamic symbol. This is
2513 called via elf_link_hash_traverse, and also calls itself
2517 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2519 struct elf_info_failed *eif = data;
2521 const struct elf_backend_data *bed;
2523 if (! is_elf_hash_table (eif->info->hash))
2526 if (h->root.type == bfd_link_hash_warning)
2528 h->got = elf_hash_table (eif->info)->init_got_offset;
2529 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2531 /* When warning symbols are created, they **replace** the "real"
2532 entry in the hash table, thus we never get to see the real
2533 symbol in a hash traversal. So look at it now. */
2534 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2537 /* Ignore indirect symbols. These are added by the versioning code. */
2538 if (h->root.type == bfd_link_hash_indirect)
2541 /* Fix the symbol flags. */
2542 if (! _bfd_elf_fix_symbol_flags (h, eif))
2545 /* If this symbol does not require a PLT entry, and it is not
2546 defined by a dynamic object, or is not referenced by a regular
2547 object, ignore it. We do have to handle a weak defined symbol,
2548 even if no regular object refers to it, if we decided to add it
2549 to the dynamic symbol table. FIXME: Do we normally need to worry
2550 about symbols which are defined by one dynamic object and
2551 referenced by another one? */
2556 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2558 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2562 /* If we've already adjusted this symbol, don't do it again. This
2563 can happen via a recursive call. */
2564 if (h->dynamic_adjusted)
2567 /* Don't look at this symbol again. Note that we must set this
2568 after checking the above conditions, because we may look at a
2569 symbol once, decide not to do anything, and then get called
2570 recursively later after REF_REGULAR is set below. */
2571 h->dynamic_adjusted = 1;
2573 /* If this is a weak definition, and we know a real definition, and
2574 the real symbol is not itself defined by a regular object file,
2575 then get a good value for the real definition. We handle the
2576 real symbol first, for the convenience of the backend routine.
2578 Note that there is a confusing case here. If the real definition
2579 is defined by a regular object file, we don't get the real symbol
2580 from the dynamic object, but we do get the weak symbol. If the
2581 processor backend uses a COPY reloc, then if some routine in the
2582 dynamic object changes the real symbol, we will not see that
2583 change in the corresponding weak symbol. This is the way other
2584 ELF linkers work as well, and seems to be a result of the shared
2587 I will clarify this issue. Most SVR4 shared libraries define the
2588 variable _timezone and define timezone as a weak synonym. The
2589 tzset call changes _timezone. If you write
2590 extern int timezone;
2592 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2593 you might expect that, since timezone is a synonym for _timezone,
2594 the same number will print both times. However, if the processor
2595 backend uses a COPY reloc, then actually timezone will be copied
2596 into your process image, and, since you define _timezone
2597 yourself, _timezone will not. Thus timezone and _timezone will
2598 wind up at different memory locations. The tzset call will set
2599 _timezone, leaving timezone unchanged. */
2601 if (h->u.weakdef != NULL)
2603 /* If we get to this point, we know there is an implicit
2604 reference by a regular object file via the weak symbol H.
2605 FIXME: Is this really true? What if the traversal finds
2606 H->U.WEAKDEF before it finds H? */
2607 h->u.weakdef->ref_regular = 1;
2609 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2613 /* If a symbol has no type and no size and does not require a PLT
2614 entry, then we are probably about to do the wrong thing here: we
2615 are probably going to create a COPY reloc for an empty object.
2616 This case can arise when a shared object is built with assembly
2617 code, and the assembly code fails to set the symbol type. */
2619 && h->type == STT_NOTYPE
2621 (*_bfd_error_handler)
2622 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2623 h->root.root.string);
2625 dynobj = elf_hash_table (eif->info)->dynobj;
2626 bed = get_elf_backend_data (dynobj);
2627 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2636 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2640 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2643 unsigned int power_of_two;
2645 asection *sec = h->root.u.def.section;
2647 /* The section aligment of definition is the maximum alignment
2648 requirement of symbols defined in the section. Since we don't
2649 know the symbol alignment requirement, we start with the
2650 maximum alignment and check low bits of the symbol address
2651 for the minimum alignment. */
2652 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2653 mask = ((bfd_vma) 1 << power_of_two) - 1;
2654 while ((h->root.u.def.value & mask) != 0)
2660 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2663 /* Adjust the section alignment if needed. */
2664 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2669 /* We make sure that the symbol will be aligned properly. */
2670 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2672 /* Define the symbol as being at this point in DYNBSS. */
2673 h->root.u.def.section = dynbss;
2674 h->root.u.def.value = dynbss->size;
2676 /* Increment the size of DYNBSS to make room for the symbol. */
2677 dynbss->size += h->size;
2682 /* Adjust all external symbols pointing into SEC_MERGE sections
2683 to reflect the object merging within the sections. */
2686 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2690 if (h->root.type == bfd_link_hash_warning)
2691 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2693 if ((h->root.type == bfd_link_hash_defined
2694 || h->root.type == bfd_link_hash_defweak)
2695 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2696 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2698 bfd *output_bfd = data;
2700 h->root.u.def.value =
2701 _bfd_merged_section_offset (output_bfd,
2702 &h->root.u.def.section,
2703 elf_section_data (sec)->sec_info,
2704 h->root.u.def.value);
2710 /* Returns false if the symbol referred to by H should be considered
2711 to resolve local to the current module, and true if it should be
2712 considered to bind dynamically. */
2715 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2716 struct bfd_link_info *info,
2717 bfd_boolean ignore_protected)
2719 bfd_boolean binding_stays_local_p;
2720 const struct elf_backend_data *bed;
2721 struct elf_link_hash_table *hash_table;
2726 while (h->root.type == bfd_link_hash_indirect
2727 || h->root.type == bfd_link_hash_warning)
2728 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2730 /* If it was forced local, then clearly it's not dynamic. */
2731 if (h->dynindx == -1)
2733 if (h->forced_local)
2736 /* Identify the cases where name binding rules say that a
2737 visible symbol resolves locally. */
2738 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2740 switch (ELF_ST_VISIBILITY (h->other))
2747 hash_table = elf_hash_table (info);
2748 if (!is_elf_hash_table (hash_table))
2751 bed = get_elf_backend_data (hash_table->dynobj);
2753 /* Proper resolution for function pointer equality may require
2754 that these symbols perhaps be resolved dynamically, even though
2755 we should be resolving them to the current module. */
2756 if (!ignore_protected || !bed->is_function_type (h->type))
2757 binding_stays_local_p = TRUE;
2764 /* If it isn't defined locally, then clearly it's dynamic. */
2765 if (!h->def_regular)
2768 /* Otherwise, the symbol is dynamic if binding rules don't tell
2769 us that it remains local. */
2770 return !binding_stays_local_p;
2773 /* Return true if the symbol referred to by H should be considered
2774 to resolve local to the current module, and false otherwise. Differs
2775 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2776 undefined symbols and weak symbols. */
2779 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2780 struct bfd_link_info *info,
2781 bfd_boolean local_protected)
2783 const struct elf_backend_data *bed;
2784 struct elf_link_hash_table *hash_table;
2786 /* If it's a local sym, of course we resolve locally. */
2790 /* Common symbols that become definitions don't get the DEF_REGULAR
2791 flag set, so test it first, and don't bail out. */
2792 if (ELF_COMMON_DEF_P (h))
2794 /* If we don't have a definition in a regular file, then we can't
2795 resolve locally. The sym is either undefined or dynamic. */
2796 else if (!h->def_regular)
2799 /* Forced local symbols resolve locally. */
2800 if (h->forced_local)
2803 /* As do non-dynamic symbols. */
2804 if (h->dynindx == -1)
2807 /* At this point, we know the symbol is defined and dynamic. In an
2808 executable it must resolve locally, likewise when building symbolic
2809 shared libraries. */
2810 if (info->executable || SYMBOLIC_BIND (info, h))
2813 /* Now deal with defined dynamic symbols in shared libraries. Ones
2814 with default visibility might not resolve locally. */
2815 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2818 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2819 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2822 hash_table = elf_hash_table (info);
2823 if (!is_elf_hash_table (hash_table))
2826 bed = get_elf_backend_data (hash_table->dynobj);
2828 /* STV_PROTECTED non-function symbols are local. */
2829 if (!bed->is_function_type (h->type))
2832 /* Function pointer equality tests may require that STV_PROTECTED
2833 symbols be treated as dynamic symbols, even when we know that the
2834 dynamic linker will resolve them locally. */
2835 return local_protected;
2838 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2839 aligned. Returns the first TLS output section. */
2841 struct bfd_section *
2842 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2844 struct bfd_section *sec, *tls;
2845 unsigned int align = 0;
2847 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2848 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2852 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2853 if (sec->alignment_power > align)
2854 align = sec->alignment_power;
2856 elf_hash_table (info)->tls_sec = tls;
2858 /* Ensure the alignment of the first section is the largest alignment,
2859 so that the tls segment starts aligned. */
2861 tls->alignment_power = align;
2866 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2868 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2869 Elf_Internal_Sym *sym)
2871 const struct elf_backend_data *bed;
2873 /* Local symbols do not count, but target specific ones might. */
2874 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2875 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2878 bed = get_elf_backend_data (abfd);
2879 /* Function symbols do not count. */
2880 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2883 /* If the section is undefined, then so is the symbol. */
2884 if (sym->st_shndx == SHN_UNDEF)
2887 /* If the symbol is defined in the common section, then
2888 it is a common definition and so does not count. */
2889 if (bed->common_definition (sym))
2892 /* If the symbol is in a target specific section then we
2893 must rely upon the backend to tell us what it is. */
2894 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2895 /* FIXME - this function is not coded yet:
2897 return _bfd_is_global_symbol_definition (abfd, sym);
2899 Instead for now assume that the definition is not global,
2900 Even if this is wrong, at least the linker will behave
2901 in the same way that it used to do. */
2907 /* Search the symbol table of the archive element of the archive ABFD
2908 whose archive map contains a mention of SYMDEF, and determine if
2909 the symbol is defined in this element. */
2911 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2913 Elf_Internal_Shdr * hdr;
2914 bfd_size_type symcount;
2915 bfd_size_type extsymcount;
2916 bfd_size_type extsymoff;
2917 Elf_Internal_Sym *isymbuf;
2918 Elf_Internal_Sym *isym;
2919 Elf_Internal_Sym *isymend;
2922 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2926 if (! bfd_check_format (abfd, bfd_object))
2929 /* If we have already included the element containing this symbol in the
2930 link then we do not need to include it again. Just claim that any symbol
2931 it contains is not a definition, so that our caller will not decide to
2932 (re)include this element. */
2933 if (abfd->archive_pass)
2936 /* Select the appropriate symbol table. */
2937 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2938 hdr = &elf_tdata (abfd)->symtab_hdr;
2940 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2942 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2944 /* The sh_info field of the symtab header tells us where the
2945 external symbols start. We don't care about the local symbols. */
2946 if (elf_bad_symtab (abfd))
2948 extsymcount = symcount;
2953 extsymcount = symcount - hdr->sh_info;
2954 extsymoff = hdr->sh_info;
2957 if (extsymcount == 0)
2960 /* Read in the symbol table. */
2961 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2963 if (isymbuf == NULL)
2966 /* Scan the symbol table looking for SYMDEF. */
2968 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2972 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2977 if (strcmp (name, symdef->name) == 0)
2979 result = is_global_data_symbol_definition (abfd, isym);
2989 /* Add an entry to the .dynamic table. */
2992 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2996 struct elf_link_hash_table *hash_table;
2997 const struct elf_backend_data *bed;
2999 bfd_size_type newsize;
3000 bfd_byte *newcontents;
3001 Elf_Internal_Dyn dyn;
3003 hash_table = elf_hash_table (info);
3004 if (! is_elf_hash_table (hash_table))
3007 bed = get_elf_backend_data (hash_table->dynobj);
3008 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3009 BFD_ASSERT (s != NULL);
3011 newsize = s->size + bed->s->sizeof_dyn;
3012 newcontents = bfd_realloc (s->contents, newsize);
3013 if (newcontents == NULL)
3017 dyn.d_un.d_val = val;
3018 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3021 s->contents = newcontents;
3026 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3027 otherwise just check whether one already exists. Returns -1 on error,
3028 1 if a DT_NEEDED tag already exists, and 0 on success. */
3031 elf_add_dt_needed_tag (bfd *abfd,
3032 struct bfd_link_info *info,
3036 struct elf_link_hash_table *hash_table;
3037 bfd_size_type oldsize;
3038 bfd_size_type strindex;
3040 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3043 hash_table = elf_hash_table (info);
3044 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3045 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3046 if (strindex == (bfd_size_type) -1)
3049 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3052 const struct elf_backend_data *bed;
3055 bed = get_elf_backend_data (hash_table->dynobj);
3056 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3058 for (extdyn = sdyn->contents;
3059 extdyn < sdyn->contents + sdyn->size;
3060 extdyn += bed->s->sizeof_dyn)
3062 Elf_Internal_Dyn dyn;
3064 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3065 if (dyn.d_tag == DT_NEEDED
3066 && dyn.d_un.d_val == strindex)
3068 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3076 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3079 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3083 /* We were just checking for existence of the tag. */
3084 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3089 /* Sort symbol by value and section. */
3091 elf_sort_symbol (const void *arg1, const void *arg2)
3093 const struct elf_link_hash_entry *h1;
3094 const struct elf_link_hash_entry *h2;
3095 bfd_signed_vma vdiff;
3097 h1 = *(const struct elf_link_hash_entry **) arg1;
3098 h2 = *(const struct elf_link_hash_entry **) arg2;
3099 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3101 return vdiff > 0 ? 1 : -1;
3104 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3106 return sdiff > 0 ? 1 : -1;
3111 /* This function is used to adjust offsets into .dynstr for
3112 dynamic symbols. This is called via elf_link_hash_traverse. */
3115 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3117 struct elf_strtab_hash *dynstr = data;
3119 if (h->root.type == bfd_link_hash_warning)
3120 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3122 if (h->dynindx != -1)
3123 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3127 /* Assign string offsets in .dynstr, update all structures referencing
3131 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3133 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3134 struct elf_link_local_dynamic_entry *entry;
3135 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3136 bfd *dynobj = hash_table->dynobj;
3139 const struct elf_backend_data *bed;
3142 _bfd_elf_strtab_finalize (dynstr);
3143 size = _bfd_elf_strtab_size (dynstr);
3145 bed = get_elf_backend_data (dynobj);
3146 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3147 BFD_ASSERT (sdyn != NULL);
3149 /* Update all .dynamic entries referencing .dynstr strings. */
3150 for (extdyn = sdyn->contents;
3151 extdyn < sdyn->contents + sdyn->size;
3152 extdyn += bed->s->sizeof_dyn)
3154 Elf_Internal_Dyn dyn;
3156 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3160 dyn.d_un.d_val = size;
3168 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3173 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3176 /* Now update local dynamic symbols. */
3177 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3178 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3179 entry->isym.st_name);
3181 /* And the rest of dynamic symbols. */
3182 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3184 /* Adjust version definitions. */
3185 if (elf_tdata (output_bfd)->cverdefs)
3190 Elf_Internal_Verdef def;
3191 Elf_Internal_Verdaux defaux;
3193 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3197 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3199 p += sizeof (Elf_External_Verdef);
3200 if (def.vd_aux != sizeof (Elf_External_Verdef))
3202 for (i = 0; i < def.vd_cnt; ++i)
3204 _bfd_elf_swap_verdaux_in (output_bfd,
3205 (Elf_External_Verdaux *) p, &defaux);
3206 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3208 _bfd_elf_swap_verdaux_out (output_bfd,
3209 &defaux, (Elf_External_Verdaux *) p);
3210 p += sizeof (Elf_External_Verdaux);
3213 while (def.vd_next);
3216 /* Adjust version references. */
3217 if (elf_tdata (output_bfd)->verref)
3222 Elf_Internal_Verneed need;
3223 Elf_Internal_Vernaux needaux;
3225 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3229 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3231 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3232 _bfd_elf_swap_verneed_out (output_bfd, &need,
3233 (Elf_External_Verneed *) p);
3234 p += sizeof (Elf_External_Verneed);
3235 for (i = 0; i < need.vn_cnt; ++i)
3237 _bfd_elf_swap_vernaux_in (output_bfd,
3238 (Elf_External_Vernaux *) p, &needaux);
3239 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3241 _bfd_elf_swap_vernaux_out (output_bfd,
3243 (Elf_External_Vernaux *) p);
3244 p += sizeof (Elf_External_Vernaux);
3247 while (need.vn_next);
3253 /* Add symbols from an ELF object file to the linker hash table. */
3256 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3258 Elf_Internal_Shdr *hdr;
3259 bfd_size_type symcount;
3260 bfd_size_type extsymcount;
3261 bfd_size_type extsymoff;
3262 struct elf_link_hash_entry **sym_hash;
3263 bfd_boolean dynamic;
3264 Elf_External_Versym *extversym = NULL;
3265 Elf_External_Versym *ever;
3266 struct elf_link_hash_entry *weaks;
3267 struct elf_link_hash_entry **nondeflt_vers = NULL;
3268 bfd_size_type nondeflt_vers_cnt = 0;
3269 Elf_Internal_Sym *isymbuf = NULL;
3270 Elf_Internal_Sym *isym;
3271 Elf_Internal_Sym *isymend;
3272 const struct elf_backend_data *bed;
3273 bfd_boolean add_needed;
3274 struct elf_link_hash_table *htab;
3276 void *alloc_mark = NULL;
3277 struct bfd_hash_entry **old_table = NULL;
3278 unsigned int old_size = 0;
3279 unsigned int old_count = 0;
3280 void *old_tab = NULL;
3283 struct bfd_link_hash_entry *old_undefs = NULL;
3284 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3285 long old_dynsymcount = 0;
3287 size_t hashsize = 0;
3289 htab = elf_hash_table (info);
3290 bed = get_elf_backend_data (abfd);
3292 if ((abfd->flags & DYNAMIC) == 0)
3298 /* You can't use -r against a dynamic object. Also, there's no
3299 hope of using a dynamic object which does not exactly match
3300 the format of the output file. */
3301 if (info->relocatable
3302 || !is_elf_hash_table (htab)
3303 || htab->root.creator != abfd->xvec)
3305 if (info->relocatable)
3306 bfd_set_error (bfd_error_invalid_operation);
3308 bfd_set_error (bfd_error_wrong_format);
3313 /* As a GNU extension, any input sections which are named
3314 .gnu.warning.SYMBOL are treated as warning symbols for the given
3315 symbol. This differs from .gnu.warning sections, which generate
3316 warnings when they are included in an output file. */
3317 if (info->executable)
3321 for (s = abfd->sections; s != NULL; s = s->next)
3325 name = bfd_get_section_name (abfd, s);
3326 if (CONST_STRNEQ (name, ".gnu.warning."))
3331 name += sizeof ".gnu.warning." - 1;
3333 /* If this is a shared object, then look up the symbol
3334 in the hash table. If it is there, and it is already
3335 been defined, then we will not be using the entry
3336 from this shared object, so we don't need to warn.
3337 FIXME: If we see the definition in a regular object
3338 later on, we will warn, but we shouldn't. The only
3339 fix is to keep track of what warnings we are supposed
3340 to emit, and then handle them all at the end of the
3344 struct elf_link_hash_entry *h;
3346 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3348 /* FIXME: What about bfd_link_hash_common? */
3350 && (h->root.type == bfd_link_hash_defined
3351 || h->root.type == bfd_link_hash_defweak))
3353 /* We don't want to issue this warning. Clobber
3354 the section size so that the warning does not
3355 get copied into the output file. */
3362 msg = bfd_alloc (abfd, sz + 1);
3366 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3371 if (! (_bfd_generic_link_add_one_symbol
3372 (info, abfd, name, BSF_WARNING, s, 0, msg,
3373 FALSE, bed->collect, NULL)))
3376 if (! info->relocatable)
3378 /* Clobber the section size so that the warning does
3379 not get copied into the output file. */
3382 /* Also set SEC_EXCLUDE, so that symbols defined in
3383 the warning section don't get copied to the output. */
3384 s->flags |= SEC_EXCLUDE;
3393 /* If we are creating a shared library, create all the dynamic
3394 sections immediately. We need to attach them to something,
3395 so we attach them to this BFD, provided it is the right
3396 format. FIXME: If there are no input BFD's of the same
3397 format as the output, we can't make a shared library. */
3399 && is_elf_hash_table (htab)
3400 && htab->root.creator == abfd->xvec
3401 && !htab->dynamic_sections_created)
3403 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3407 else if (!is_elf_hash_table (htab))
3412 const char *soname = NULL;
3413 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3416 /* ld --just-symbols and dynamic objects don't mix very well.
3417 ld shouldn't allow it. */
3418 if ((s = abfd->sections) != NULL
3419 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3422 /* If this dynamic lib was specified on the command line with
3423 --as-needed in effect, then we don't want to add a DT_NEEDED
3424 tag unless the lib is actually used. Similary for libs brought
3425 in by another lib's DT_NEEDED. When --no-add-needed is used
3426 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3427 any dynamic library in DT_NEEDED tags in the dynamic lib at
3429 add_needed = (elf_dyn_lib_class (abfd)
3430 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3431 | DYN_NO_NEEDED)) == 0;
3433 s = bfd_get_section_by_name (abfd, ".dynamic");
3439 unsigned long shlink;
3441 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3442 goto error_free_dyn;
3444 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3446 goto error_free_dyn;
3447 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3449 for (extdyn = dynbuf;
3450 extdyn < dynbuf + s->size;
3451 extdyn += bed->s->sizeof_dyn)
3453 Elf_Internal_Dyn dyn;
3455 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3456 if (dyn.d_tag == DT_SONAME)
3458 unsigned int tagv = dyn.d_un.d_val;
3459 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3461 goto error_free_dyn;
3463 if (dyn.d_tag == DT_NEEDED)
3465 struct bfd_link_needed_list *n, **pn;
3467 unsigned int tagv = dyn.d_un.d_val;
3469 amt = sizeof (struct bfd_link_needed_list);
3470 n = bfd_alloc (abfd, amt);
3471 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3472 if (n == NULL || fnm == NULL)
3473 goto error_free_dyn;
3474 amt = strlen (fnm) + 1;
3475 anm = bfd_alloc (abfd, amt);
3477 goto error_free_dyn;
3478 memcpy (anm, fnm, amt);
3482 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3486 if (dyn.d_tag == DT_RUNPATH)
3488 struct bfd_link_needed_list *n, **pn;
3490 unsigned int tagv = dyn.d_un.d_val;
3492 amt = sizeof (struct bfd_link_needed_list);
3493 n = bfd_alloc (abfd, amt);
3494 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3495 if (n == NULL || fnm == NULL)
3496 goto error_free_dyn;
3497 amt = strlen (fnm) + 1;
3498 anm = bfd_alloc (abfd, amt);
3500 goto error_free_dyn;
3501 memcpy (anm, fnm, amt);
3505 for (pn = & runpath;
3511 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3512 if (!runpath && dyn.d_tag == DT_RPATH)
3514 struct bfd_link_needed_list *n, **pn;
3516 unsigned int tagv = dyn.d_un.d_val;
3518 amt = sizeof (struct bfd_link_needed_list);
3519 n = bfd_alloc (abfd, amt);
3520 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3521 if (n == NULL || fnm == NULL)
3522 goto error_free_dyn;
3523 amt = strlen (fnm) + 1;
3524 anm = bfd_alloc (abfd, amt);
3531 memcpy (anm, fnm, amt);
3546 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3547 frees all more recently bfd_alloc'd blocks as well. */
3553 struct bfd_link_needed_list **pn;
3554 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3559 /* We do not want to include any of the sections in a dynamic
3560 object in the output file. We hack by simply clobbering the
3561 list of sections in the BFD. This could be handled more
3562 cleanly by, say, a new section flag; the existing
3563 SEC_NEVER_LOAD flag is not the one we want, because that one
3564 still implies that the section takes up space in the output
3566 bfd_section_list_clear (abfd);
3568 /* Find the name to use in a DT_NEEDED entry that refers to this
3569 object. If the object has a DT_SONAME entry, we use it.
3570 Otherwise, if the generic linker stuck something in
3571 elf_dt_name, we use that. Otherwise, we just use the file
3573 if (soname == NULL || *soname == '\0')
3575 soname = elf_dt_name (abfd);
3576 if (soname == NULL || *soname == '\0')
3577 soname = bfd_get_filename (abfd);
3580 /* Save the SONAME because sometimes the linker emulation code
3581 will need to know it. */
3582 elf_dt_name (abfd) = soname;
3584 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3588 /* If we have already included this dynamic object in the
3589 link, just ignore it. There is no reason to include a
3590 particular dynamic object more than once. */
3595 /* If this is a dynamic object, we always link against the .dynsym
3596 symbol table, not the .symtab symbol table. The dynamic linker
3597 will only see the .dynsym symbol table, so there is no reason to
3598 look at .symtab for a dynamic object. */
3600 if (! dynamic || elf_dynsymtab (abfd) == 0)
3601 hdr = &elf_tdata (abfd)->symtab_hdr;
3603 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3605 symcount = hdr->sh_size / bed->s->sizeof_sym;
3607 /* The sh_info field of the symtab header tells us where the
3608 external symbols start. We don't care about the local symbols at
3610 if (elf_bad_symtab (abfd))
3612 extsymcount = symcount;
3617 extsymcount = symcount - hdr->sh_info;
3618 extsymoff = hdr->sh_info;
3622 if (extsymcount != 0)
3624 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3626 if (isymbuf == NULL)
3629 /* We store a pointer to the hash table entry for each external
3631 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3632 sym_hash = bfd_alloc (abfd, amt);
3633 if (sym_hash == NULL)
3634 goto error_free_sym;
3635 elf_sym_hashes (abfd) = sym_hash;
3640 /* Read in any version definitions. */
3641 if (!_bfd_elf_slurp_version_tables (abfd,
3642 info->default_imported_symver))
3643 goto error_free_sym;
3645 /* Read in the symbol versions, but don't bother to convert them
3646 to internal format. */
3647 if (elf_dynversym (abfd) != 0)
3649 Elf_Internal_Shdr *versymhdr;
3651 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3652 extversym = bfd_malloc (versymhdr->sh_size);
3653 if (extversym == NULL)
3654 goto error_free_sym;
3655 amt = versymhdr->sh_size;
3656 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3657 || bfd_bread (extversym, amt, abfd) != amt)
3658 goto error_free_vers;
3662 /* If we are loading an as-needed shared lib, save the symbol table
3663 state before we start adding symbols. If the lib turns out
3664 to be unneeded, restore the state. */
3665 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3670 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3672 struct bfd_hash_entry *p;
3673 struct elf_link_hash_entry *h;
3675 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3677 h = (struct elf_link_hash_entry *) p;
3678 entsize += htab->root.table.entsize;
3679 if (h->root.type == bfd_link_hash_warning)
3680 entsize += htab->root.table.entsize;
3684 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3685 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3686 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3687 if (old_tab == NULL)
3688 goto error_free_vers;
3690 /* Remember the current objalloc pointer, so that all mem for
3691 symbols added can later be reclaimed. */
3692 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3693 if (alloc_mark == NULL)
3694 goto error_free_vers;
3696 /* Make a special call to the linker "notice" function to
3697 tell it that we are about to handle an as-needed lib. */
3698 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3700 goto error_free_vers;
3702 /* Clone the symbol table and sym hashes. Remember some
3703 pointers into the symbol table, and dynamic symbol count. */
3704 old_hash = (char *) old_tab + tabsize;
3705 old_ent = (char *) old_hash + hashsize;
3706 memcpy (old_tab, htab->root.table.table, tabsize);
3707 memcpy (old_hash, sym_hash, hashsize);
3708 old_undefs = htab->root.undefs;
3709 old_undefs_tail = htab->root.undefs_tail;
3710 old_table = htab->root.table.table;
3711 old_size = htab->root.table.size;
3712 old_count = htab->root.table.count;
3713 old_dynsymcount = htab->dynsymcount;
3715 for (i = 0; i < htab->root.table.size; i++)
3717 struct bfd_hash_entry *p;
3718 struct elf_link_hash_entry *h;
3720 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3722 memcpy (old_ent, p, htab->root.table.entsize);
3723 old_ent = (char *) old_ent + htab->root.table.entsize;
3724 h = (struct elf_link_hash_entry *) p;
3725 if (h->root.type == bfd_link_hash_warning)
3727 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3728 old_ent = (char *) old_ent + htab->root.table.entsize;
3735 ever = extversym != NULL ? extversym + extsymoff : NULL;
3736 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3738 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3742 asection *sec, *new_sec;
3745 struct elf_link_hash_entry *h;
3746 bfd_boolean definition;
3747 bfd_boolean size_change_ok;
3748 bfd_boolean type_change_ok;
3749 bfd_boolean new_weakdef;
3750 bfd_boolean override;
3752 unsigned int old_alignment;
3757 flags = BSF_NO_FLAGS;
3759 value = isym->st_value;
3761 common = bed->common_definition (isym);
3763 bind = ELF_ST_BIND (isym->st_info);
3764 if (bind == STB_LOCAL)
3766 /* This should be impossible, since ELF requires that all
3767 global symbols follow all local symbols, and that sh_info
3768 point to the first global symbol. Unfortunately, Irix 5
3772 else if (bind == STB_GLOBAL)
3774 if (isym->st_shndx != SHN_UNDEF && !common)
3777 else if (bind == STB_WEAK)
3781 /* Leave it up to the processor backend. */
3784 if (isym->st_shndx == SHN_UNDEF)
3785 sec = bfd_und_section_ptr;
3786 else if (isym->st_shndx < SHN_LORESERVE
3787 || isym->st_shndx > SHN_HIRESERVE)
3789 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3791 sec = bfd_abs_section_ptr;
3792 else if (sec->kept_section)
3794 /* Symbols from discarded section are undefined. We keep
3796 sec = bfd_und_section_ptr;
3797 isym->st_shndx = SHN_UNDEF;
3799 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3802 else if (isym->st_shndx == SHN_ABS)
3803 sec = bfd_abs_section_ptr;
3804 else if (isym->st_shndx == SHN_COMMON)
3806 sec = bfd_com_section_ptr;
3807 /* What ELF calls the size we call the value. What ELF
3808 calls the value we call the alignment. */
3809 value = isym->st_size;
3813 /* Leave it up to the processor backend. */
3816 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3819 goto error_free_vers;
3821 if (isym->st_shndx == SHN_COMMON
3822 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3823 && !info->relocatable)
3825 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3829 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3832 | SEC_LINKER_CREATED
3833 | SEC_THREAD_LOCAL));
3835 goto error_free_vers;
3839 else if (bed->elf_add_symbol_hook)
3841 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3843 goto error_free_vers;
3845 /* The hook function sets the name to NULL if this symbol
3846 should be skipped for some reason. */
3851 /* Sanity check that all possibilities were handled. */
3854 bfd_set_error (bfd_error_bad_value);
3855 goto error_free_vers;
3858 if (bfd_is_und_section (sec)
3859 || bfd_is_com_section (sec))
3864 size_change_ok = FALSE;
3865 type_change_ok = bed->type_change_ok;
3870 if (is_elf_hash_table (htab))
3872 Elf_Internal_Versym iver;
3873 unsigned int vernum = 0;
3878 if (info->default_imported_symver)
3879 /* Use the default symbol version created earlier. */
3880 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3885 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3887 vernum = iver.vs_vers & VERSYM_VERSION;
3889 /* If this is a hidden symbol, or if it is not version
3890 1, we append the version name to the symbol name.
3891 However, we do not modify a non-hidden absolute symbol
3892 if it is not a function, because it might be the version
3893 symbol itself. FIXME: What if it isn't? */
3894 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3896 && (!bfd_is_abs_section (sec)
3897 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
3900 size_t namelen, verlen, newlen;
3903 if (isym->st_shndx != SHN_UNDEF)
3905 if (vernum > elf_tdata (abfd)->cverdefs)
3907 else if (vernum > 1)
3909 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3915 (*_bfd_error_handler)
3916 (_("%B: %s: invalid version %u (max %d)"),
3918 elf_tdata (abfd)->cverdefs);
3919 bfd_set_error (bfd_error_bad_value);
3920 goto error_free_vers;
3925 /* We cannot simply test for the number of
3926 entries in the VERNEED section since the
3927 numbers for the needed versions do not start
3929 Elf_Internal_Verneed *t;
3932 for (t = elf_tdata (abfd)->verref;
3936 Elf_Internal_Vernaux *a;
3938 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3940 if (a->vna_other == vernum)
3942 verstr = a->vna_nodename;
3951 (*_bfd_error_handler)
3952 (_("%B: %s: invalid needed version %d"),
3953 abfd, name, vernum);
3954 bfd_set_error (bfd_error_bad_value);
3955 goto error_free_vers;
3959 namelen = strlen (name);
3960 verlen = strlen (verstr);
3961 newlen = namelen + verlen + 2;
3962 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3963 && isym->st_shndx != SHN_UNDEF)
3966 newname = bfd_hash_allocate (&htab->root.table, newlen);
3967 if (newname == NULL)
3968 goto error_free_vers;
3969 memcpy (newname, name, namelen);
3970 p = newname + namelen;
3972 /* If this is a defined non-hidden version symbol,
3973 we add another @ to the name. This indicates the
3974 default version of the symbol. */
3975 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3976 && isym->st_shndx != SHN_UNDEF)
3978 memcpy (p, verstr, verlen + 1);
3983 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3984 &value, &old_alignment,
3985 sym_hash, &skip, &override,
3986 &type_change_ok, &size_change_ok))
3987 goto error_free_vers;
3996 while (h->root.type == bfd_link_hash_indirect
3997 || h->root.type == bfd_link_hash_warning)
3998 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4000 /* Remember the old alignment if this is a common symbol, so
4001 that we don't reduce the alignment later on. We can't
4002 check later, because _bfd_generic_link_add_one_symbol
4003 will set a default for the alignment which we want to
4004 override. We also remember the old bfd where the existing
4005 definition comes from. */
4006 switch (h->root.type)
4011 case bfd_link_hash_defined:
4012 case bfd_link_hash_defweak:
4013 old_bfd = h->root.u.def.section->owner;
4016 case bfd_link_hash_common:
4017 old_bfd = h->root.u.c.p->section->owner;
4018 old_alignment = h->root.u.c.p->alignment_power;
4022 if (elf_tdata (abfd)->verdef != NULL
4026 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4029 if (! (_bfd_generic_link_add_one_symbol
4030 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4031 (struct bfd_link_hash_entry **) sym_hash)))
4032 goto error_free_vers;
4035 while (h->root.type == bfd_link_hash_indirect
4036 || h->root.type == bfd_link_hash_warning)
4037 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4040 new_weakdef = FALSE;
4043 && (flags & BSF_WEAK) != 0
4044 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4045 && is_elf_hash_table (htab)
4046 && h->u.weakdef == NULL)
4048 /* Keep a list of all weak defined non function symbols from
4049 a dynamic object, using the weakdef field. Later in this
4050 function we will set the weakdef field to the correct
4051 value. We only put non-function symbols from dynamic
4052 objects on this list, because that happens to be the only
4053 time we need to know the normal symbol corresponding to a
4054 weak symbol, and the information is time consuming to
4055 figure out. If the weakdef field is not already NULL,
4056 then this symbol was already defined by some previous
4057 dynamic object, and we will be using that previous
4058 definition anyhow. */
4060 h->u.weakdef = weaks;
4065 /* Set the alignment of a common symbol. */
4066 if ((common || bfd_is_com_section (sec))
4067 && h->root.type == bfd_link_hash_common)
4072 align = bfd_log2 (isym->st_value);
4075 /* The new symbol is a common symbol in a shared object.
4076 We need to get the alignment from the section. */
4077 align = new_sec->alignment_power;
4079 if (align > old_alignment
4080 /* Permit an alignment power of zero if an alignment of one
4081 is specified and no other alignments have been specified. */
4082 || (isym->st_value == 1 && old_alignment == 0))
4083 h->root.u.c.p->alignment_power = align;
4085 h->root.u.c.p->alignment_power = old_alignment;
4088 if (is_elf_hash_table (htab))
4092 /* Check the alignment when a common symbol is involved. This
4093 can change when a common symbol is overridden by a normal
4094 definition or a common symbol is ignored due to the old
4095 normal definition. We need to make sure the maximum
4096 alignment is maintained. */
4097 if ((old_alignment || common)
4098 && h->root.type != bfd_link_hash_common)
4100 unsigned int common_align;
4101 unsigned int normal_align;
4102 unsigned int symbol_align;
4106 symbol_align = ffs (h->root.u.def.value) - 1;
4107 if (h->root.u.def.section->owner != NULL
4108 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4110 normal_align = h->root.u.def.section->alignment_power;
4111 if (normal_align > symbol_align)
4112 normal_align = symbol_align;
4115 normal_align = symbol_align;
4119 common_align = old_alignment;
4120 common_bfd = old_bfd;
4125 common_align = bfd_log2 (isym->st_value);
4127 normal_bfd = old_bfd;
4130 if (normal_align < common_align)
4132 /* PR binutils/2735 */
4133 if (normal_bfd == NULL)
4134 (*_bfd_error_handler)
4135 (_("Warning: alignment %u of common symbol `%s' in %B"
4136 " is greater than the alignment (%u) of its section %A"),
4137 common_bfd, h->root.u.def.section,
4138 1 << common_align, name, 1 << normal_align);
4140 (*_bfd_error_handler)
4141 (_("Warning: alignment %u of symbol `%s' in %B"
4142 " is smaller than %u in %B"),
4143 normal_bfd, common_bfd,
4144 1 << normal_align, name, 1 << common_align);
4148 /* Remember the symbol size if it isn't undefined. */
4149 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4150 && (definition || h->size == 0))
4153 && h->size != isym->st_size
4154 && ! size_change_ok)
4155 (*_bfd_error_handler)
4156 (_("Warning: size of symbol `%s' changed"
4157 " from %lu in %B to %lu in %B"),
4159 name, (unsigned long) h->size,
4160 (unsigned long) isym->st_size);
4162 h->size = isym->st_size;
4165 /* If this is a common symbol, then we always want H->SIZE
4166 to be the size of the common symbol. The code just above
4167 won't fix the size if a common symbol becomes larger. We
4168 don't warn about a size change here, because that is
4169 covered by --warn-common. Allow changed between different
4171 if (h->root.type == bfd_link_hash_common)
4172 h->size = h->root.u.c.size;
4174 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4175 && (definition || h->type == STT_NOTYPE))
4177 if (h->type != STT_NOTYPE
4178 && h->type != ELF_ST_TYPE (isym->st_info)
4179 && ! type_change_ok)
4180 (*_bfd_error_handler)
4181 (_("Warning: type of symbol `%s' changed"
4182 " from %d to %d in %B"),
4183 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
4185 h->type = ELF_ST_TYPE (isym->st_info);
4188 /* If st_other has a processor-specific meaning, specific
4189 code might be needed here. We never merge the visibility
4190 attribute with the one from a dynamic object. */
4191 if (bed->elf_backend_merge_symbol_attribute)
4192 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
4195 /* If this symbol has default visibility and the user has requested
4196 we not re-export it, then mark it as hidden. */
4197 if (definition && !dynamic
4199 || (abfd->my_archive && abfd->my_archive->no_export))
4200 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4201 isym->st_other = (STV_HIDDEN
4202 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4204 if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic)
4206 unsigned char hvis, symvis, other, nvis;
4208 /* Only merge the visibility. Leave the remainder of the
4209 st_other field to elf_backend_merge_symbol_attribute. */
4210 other = h->other & ~ELF_ST_VISIBILITY (-1);
4212 /* Combine visibilities, using the most constraining one. */
4213 hvis = ELF_ST_VISIBILITY (h->other);
4214 symvis = ELF_ST_VISIBILITY (isym->st_other);
4220 nvis = hvis < symvis ? hvis : symvis;
4222 h->other = other | nvis;
4225 /* Set a flag in the hash table entry indicating the type of
4226 reference or definition we just found. Keep a count of
4227 the number of dynamic symbols we find. A dynamic symbol
4228 is one which is referenced or defined by both a regular
4229 object and a shared object. */
4236 if (bind != STB_WEAK)
4237 h->ref_regular_nonweak = 1;
4241 if (! info->executable
4254 || (h->u.weakdef != NULL
4256 && h->u.weakdef->dynindx != -1))
4260 if (definition && (sec->flags & SEC_DEBUGGING))
4262 /* We don't want to make debug symbol dynamic. */
4263 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4267 /* Check to see if we need to add an indirect symbol for
4268 the default name. */
4269 if (definition || h->root.type == bfd_link_hash_common)
4270 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4271 &sec, &value, &dynsym,
4273 goto error_free_vers;
4275 if (definition && !dynamic)
4277 char *p = strchr (name, ELF_VER_CHR);
4278 if (p != NULL && p[1] != ELF_VER_CHR)
4280 /* Queue non-default versions so that .symver x, x@FOO
4281 aliases can be checked. */
4284 amt = ((isymend - isym + 1)
4285 * sizeof (struct elf_link_hash_entry *));
4286 nondeflt_vers = bfd_malloc (amt);
4288 nondeflt_vers[nondeflt_vers_cnt++] = h;
4292 if (dynsym && h->dynindx == -1)
4294 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4295 goto error_free_vers;
4296 if (h->u.weakdef != NULL
4298 && h->u.weakdef->dynindx == -1)
4300 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4301 goto error_free_vers;
4304 else if (dynsym && h->dynindx != -1)
4305 /* If the symbol already has a dynamic index, but
4306 visibility says it should not be visible, turn it into
4308 switch (ELF_ST_VISIBILITY (h->other))
4312 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4323 const char *soname = elf_dt_name (abfd);
4325 /* A symbol from a library loaded via DT_NEEDED of some
4326 other library is referenced by a regular object.
4327 Add a DT_NEEDED entry for it. Issue an error if
4328 --no-add-needed is used. */
4329 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4331 (*_bfd_error_handler)
4332 (_("%s: invalid DSO for symbol `%s' definition"),
4334 bfd_set_error (bfd_error_bad_value);
4335 goto error_free_vers;
4338 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4341 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4343 goto error_free_vers;
4345 BFD_ASSERT (ret == 0);
4350 if (extversym != NULL)
4356 if (isymbuf != NULL)
4362 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4366 /* Restore the symbol table. */
4367 if (bed->as_needed_cleanup)
4368 (*bed->as_needed_cleanup) (abfd, info);
4369 old_hash = (char *) old_tab + tabsize;
4370 old_ent = (char *) old_hash + hashsize;
4371 sym_hash = elf_sym_hashes (abfd);
4372 htab->root.table.table = old_table;
4373 htab->root.table.size = old_size;
4374 htab->root.table.count = old_count;
4375 memcpy (htab->root.table.table, old_tab, tabsize);
4376 memcpy (sym_hash, old_hash, hashsize);
4377 htab->root.undefs = old_undefs;
4378 htab->root.undefs_tail = old_undefs_tail;
4379 for (i = 0; i < htab->root.table.size; i++)
4381 struct bfd_hash_entry *p;
4382 struct elf_link_hash_entry *h;
4384 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4386 h = (struct elf_link_hash_entry *) p;
4387 if (h->root.type == bfd_link_hash_warning)
4388 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4389 if (h->dynindx >= old_dynsymcount)
4390 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4392 memcpy (p, old_ent, htab->root.table.entsize);
4393 old_ent = (char *) old_ent + htab->root.table.entsize;
4394 h = (struct elf_link_hash_entry *) p;
4395 if (h->root.type == bfd_link_hash_warning)
4397 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4398 old_ent = (char *) old_ent + htab->root.table.entsize;
4403 /* Make a special call to the linker "notice" function to
4404 tell it that symbols added for crefs may need to be removed. */
4405 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4407 goto error_free_vers;
4410 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4412 if (nondeflt_vers != NULL)
4413 free (nondeflt_vers);
4417 if (old_tab != NULL)
4419 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4421 goto error_free_vers;
4426 /* Now that all the symbols from this input file are created, handle
4427 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4428 if (nondeflt_vers != NULL)
4430 bfd_size_type cnt, symidx;
4432 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4434 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4435 char *shortname, *p;
4437 p = strchr (h->root.root.string, ELF_VER_CHR);
4439 || (h->root.type != bfd_link_hash_defined
4440 && h->root.type != bfd_link_hash_defweak))
4443 amt = p - h->root.root.string;
4444 shortname = bfd_malloc (amt + 1);
4445 memcpy (shortname, h->root.root.string, amt);
4446 shortname[amt] = '\0';
4448 hi = (struct elf_link_hash_entry *)
4449 bfd_link_hash_lookup (&htab->root, shortname,
4450 FALSE, FALSE, FALSE);
4452 && hi->root.type == h->root.type
4453 && hi->root.u.def.value == h->root.u.def.value
4454 && hi->root.u.def.section == h->root.u.def.section)
4456 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4457 hi->root.type = bfd_link_hash_indirect;
4458 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4459 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4460 sym_hash = elf_sym_hashes (abfd);
4462 for (symidx = 0; symidx < extsymcount; ++symidx)
4463 if (sym_hash[symidx] == hi)
4465 sym_hash[symidx] = h;
4471 free (nondeflt_vers);
4472 nondeflt_vers = NULL;
4475 /* Now set the weakdefs field correctly for all the weak defined
4476 symbols we found. The only way to do this is to search all the
4477 symbols. Since we only need the information for non functions in
4478 dynamic objects, that's the only time we actually put anything on
4479 the list WEAKS. We need this information so that if a regular
4480 object refers to a symbol defined weakly in a dynamic object, the
4481 real symbol in the dynamic object is also put in the dynamic
4482 symbols; we also must arrange for both symbols to point to the
4483 same memory location. We could handle the general case of symbol
4484 aliasing, but a general symbol alias can only be generated in
4485 assembler code, handling it correctly would be very time
4486 consuming, and other ELF linkers don't handle general aliasing
4490 struct elf_link_hash_entry **hpp;
4491 struct elf_link_hash_entry **hppend;
4492 struct elf_link_hash_entry **sorted_sym_hash;
4493 struct elf_link_hash_entry *h;
4496 /* Since we have to search the whole symbol list for each weak
4497 defined symbol, search time for N weak defined symbols will be
4498 O(N^2). Binary search will cut it down to O(NlogN). */
4499 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4500 sorted_sym_hash = bfd_malloc (amt);
4501 if (sorted_sym_hash == NULL)
4503 sym_hash = sorted_sym_hash;
4504 hpp = elf_sym_hashes (abfd);
4505 hppend = hpp + extsymcount;
4507 for (; hpp < hppend; hpp++)
4511 && h->root.type == bfd_link_hash_defined
4512 && !bed->is_function_type (h->type))
4520 qsort (sorted_sym_hash, sym_count,
4521 sizeof (struct elf_link_hash_entry *),
4524 while (weaks != NULL)
4526 struct elf_link_hash_entry *hlook;
4533 weaks = hlook->u.weakdef;
4534 hlook->u.weakdef = NULL;
4536 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4537 || hlook->root.type == bfd_link_hash_defweak
4538 || hlook->root.type == bfd_link_hash_common
4539 || hlook->root.type == bfd_link_hash_indirect);
4540 slook = hlook->root.u.def.section;
4541 vlook = hlook->root.u.def.value;
4548 bfd_signed_vma vdiff;
4550 h = sorted_sym_hash [idx];
4551 vdiff = vlook - h->root.u.def.value;
4558 long sdiff = slook->id - h->root.u.def.section->id;
4571 /* We didn't find a value/section match. */
4575 for (i = ilook; i < sym_count; i++)
4577 h = sorted_sym_hash [i];
4579 /* Stop if value or section doesn't match. */
4580 if (h->root.u.def.value != vlook
4581 || h->root.u.def.section != slook)
4583 else if (h != hlook)
4585 hlook->u.weakdef = h;
4587 /* If the weak definition is in the list of dynamic
4588 symbols, make sure the real definition is put
4590 if (hlook->dynindx != -1 && h->dynindx == -1)
4592 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4596 /* If the real definition is in the list of dynamic
4597 symbols, make sure the weak definition is put
4598 there as well. If we don't do this, then the
4599 dynamic loader might not merge the entries for the
4600 real definition and the weak definition. */
4601 if (h->dynindx != -1 && hlook->dynindx == -1)
4603 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4611 free (sorted_sym_hash);
4614 if (bed->check_directives)
4615 (*bed->check_directives) (abfd, info);
4617 /* If this object is the same format as the output object, and it is
4618 not a shared library, then let the backend look through the
4621 This is required to build global offset table entries and to
4622 arrange for dynamic relocs. It is not required for the
4623 particular common case of linking non PIC code, even when linking
4624 against shared libraries, but unfortunately there is no way of
4625 knowing whether an object file has been compiled PIC or not.
4626 Looking through the relocs is not particularly time consuming.
4627 The problem is that we must either (1) keep the relocs in memory,
4628 which causes the linker to require additional runtime memory or
4629 (2) read the relocs twice from the input file, which wastes time.
4630 This would be a good case for using mmap.
4632 I have no idea how to handle linking PIC code into a file of a
4633 different format. It probably can't be done. */
4635 && is_elf_hash_table (htab)
4636 && htab->root.creator == abfd->xvec
4637 && bed->check_relocs != NULL)
4641 for (o = abfd->sections; o != NULL; o = o->next)
4643 Elf_Internal_Rela *internal_relocs;
4646 if ((o->flags & SEC_RELOC) == 0
4647 || o->reloc_count == 0
4648 || ((info->strip == strip_all || info->strip == strip_debugger)
4649 && (o->flags & SEC_DEBUGGING) != 0)
4650 || bfd_is_abs_section (o->output_section))
4653 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4655 if (internal_relocs == NULL)
4658 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4660 if (elf_section_data (o)->relocs != internal_relocs)
4661 free (internal_relocs);
4668 /* If this is a non-traditional link, try to optimize the handling
4669 of the .stab/.stabstr sections. */
4671 && ! info->traditional_format
4672 && is_elf_hash_table (htab)
4673 && (info->strip != strip_all && info->strip != strip_debugger))
4677 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4678 if (stabstr != NULL)
4680 bfd_size_type string_offset = 0;
4683 for (stab = abfd->sections; stab; stab = stab->next)
4684 if (CONST_STRNEQ (stab->name, ".stab")
4685 && (!stab->name[5] ||
4686 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4687 && (stab->flags & SEC_MERGE) == 0
4688 && !bfd_is_abs_section (stab->output_section))
4690 struct bfd_elf_section_data *secdata;
4692 secdata = elf_section_data (stab);
4693 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4694 stabstr, &secdata->sec_info,
4697 if (secdata->sec_info)
4698 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4703 if (is_elf_hash_table (htab) && add_needed)
4705 /* Add this bfd to the loaded list. */
4706 struct elf_link_loaded_list *n;
4708 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4712 n->next = htab->loaded;
4719 if (old_tab != NULL)
4721 if (nondeflt_vers != NULL)
4722 free (nondeflt_vers);
4723 if (extversym != NULL)
4726 if (isymbuf != NULL)
4732 /* Return the linker hash table entry of a symbol that might be
4733 satisfied by an archive symbol. Return -1 on error. */
4735 struct elf_link_hash_entry *
4736 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4737 struct bfd_link_info *info,
4740 struct elf_link_hash_entry *h;
4744 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4748 /* If this is a default version (the name contains @@), look up the
4749 symbol again with only one `@' as well as without the version.
4750 The effect is that references to the symbol with and without the
4751 version will be matched by the default symbol in the archive. */
4753 p = strchr (name, ELF_VER_CHR);
4754 if (p == NULL || p[1] != ELF_VER_CHR)
4757 /* First check with only one `@'. */
4758 len = strlen (name);
4759 copy = bfd_alloc (abfd, len);
4761 return (struct elf_link_hash_entry *) 0 - 1;
4763 first = p - name + 1;
4764 memcpy (copy, name, first);
4765 memcpy (copy + first, name + first + 1, len - first);
4767 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4770 /* We also need to check references to the symbol without the
4772 copy[first - 1] = '\0';
4773 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4774 FALSE, FALSE, FALSE);
4777 bfd_release (abfd, copy);
4781 /* Add symbols from an ELF archive file to the linker hash table. We
4782 don't use _bfd_generic_link_add_archive_symbols because of a
4783 problem which arises on UnixWare. The UnixWare libc.so is an
4784 archive which includes an entry libc.so.1 which defines a bunch of
4785 symbols. The libc.so archive also includes a number of other
4786 object files, which also define symbols, some of which are the same
4787 as those defined in libc.so.1. Correct linking requires that we
4788 consider each object file in turn, and include it if it defines any
4789 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4790 this; it looks through the list of undefined symbols, and includes
4791 any object file which defines them. When this algorithm is used on
4792 UnixWare, it winds up pulling in libc.so.1 early and defining a
4793 bunch of symbols. This means that some of the other objects in the
4794 archive are not included in the link, which is incorrect since they
4795 precede libc.so.1 in the archive.
4797 Fortunately, ELF archive handling is simpler than that done by
4798 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4799 oddities. In ELF, if we find a symbol in the archive map, and the
4800 symbol is currently undefined, we know that we must pull in that
4803 Unfortunately, we do have to make multiple passes over the symbol
4804 table until nothing further is resolved. */
4807 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4810 bfd_boolean *defined = NULL;
4811 bfd_boolean *included = NULL;
4815 const struct elf_backend_data *bed;
4816 struct elf_link_hash_entry * (*archive_symbol_lookup)
4817 (bfd *, struct bfd_link_info *, const char *);
4819 if (! bfd_has_map (abfd))
4821 /* An empty archive is a special case. */
4822 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4824 bfd_set_error (bfd_error_no_armap);
4828 /* Keep track of all symbols we know to be already defined, and all
4829 files we know to be already included. This is to speed up the
4830 second and subsequent passes. */
4831 c = bfd_ardata (abfd)->symdef_count;
4835 amt *= sizeof (bfd_boolean);
4836 defined = bfd_zmalloc (amt);
4837 included = bfd_zmalloc (amt);
4838 if (defined == NULL || included == NULL)
4841 symdefs = bfd_ardata (abfd)->symdefs;
4842 bed = get_elf_backend_data (abfd);
4843 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4856 symdefend = symdef + c;
4857 for (i = 0; symdef < symdefend; symdef++, i++)
4859 struct elf_link_hash_entry *h;
4861 struct bfd_link_hash_entry *undefs_tail;
4864 if (defined[i] || included[i])
4866 if (symdef->file_offset == last)
4872 h = archive_symbol_lookup (abfd, info, symdef->name);
4873 if (h == (struct elf_link_hash_entry *) 0 - 1)
4879 if (h->root.type == bfd_link_hash_common)
4881 /* We currently have a common symbol. The archive map contains
4882 a reference to this symbol, so we may want to include it. We
4883 only want to include it however, if this archive element
4884 contains a definition of the symbol, not just another common
4887 Unfortunately some archivers (including GNU ar) will put
4888 declarations of common symbols into their archive maps, as
4889 well as real definitions, so we cannot just go by the archive
4890 map alone. Instead we must read in the element's symbol
4891 table and check that to see what kind of symbol definition
4893 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4896 else if (h->root.type != bfd_link_hash_undefined)
4898 if (h->root.type != bfd_link_hash_undefweak)
4903 /* We need to include this archive member. */
4904 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4905 if (element == NULL)
4908 if (! bfd_check_format (element, bfd_object))
4911 /* Doublecheck that we have not included this object
4912 already--it should be impossible, but there may be
4913 something wrong with the archive. */
4914 if (element->archive_pass != 0)
4916 bfd_set_error (bfd_error_bad_value);
4919 element->archive_pass = 1;
4921 undefs_tail = info->hash->undefs_tail;
4923 if (! (*info->callbacks->add_archive_element) (info, element,
4926 if (! bfd_link_add_symbols (element, info))
4929 /* If there are any new undefined symbols, we need to make
4930 another pass through the archive in order to see whether
4931 they can be defined. FIXME: This isn't perfect, because
4932 common symbols wind up on undefs_tail and because an
4933 undefined symbol which is defined later on in this pass
4934 does not require another pass. This isn't a bug, but it
4935 does make the code less efficient than it could be. */
4936 if (undefs_tail != info->hash->undefs_tail)
4939 /* Look backward to mark all symbols from this object file
4940 which we have already seen in this pass. */
4944 included[mark] = TRUE;
4949 while (symdefs[mark].file_offset == symdef->file_offset);
4951 /* We mark subsequent symbols from this object file as we go
4952 on through the loop. */
4953 last = symdef->file_offset;
4964 if (defined != NULL)
4966 if (included != NULL)
4971 /* Given an ELF BFD, add symbols to the global hash table as
4975 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4977 switch (bfd_get_format (abfd))
4980 return elf_link_add_object_symbols (abfd, info);
4982 return elf_link_add_archive_symbols (abfd, info);
4984 bfd_set_error (bfd_error_wrong_format);
4989 /* This function will be called though elf_link_hash_traverse to store
4990 all hash value of the exported symbols in an array. */
4993 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4995 unsigned long **valuep = data;
5001 if (h->root.type == bfd_link_hash_warning)
5002 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5004 /* Ignore indirect symbols. These are added by the versioning code. */
5005 if (h->dynindx == -1)
5008 name = h->root.root.string;
5009 p = strchr (name, ELF_VER_CHR);
5012 alc = bfd_malloc (p - name + 1);
5013 memcpy (alc, name, p - name);
5014 alc[p - name] = '\0';
5018 /* Compute the hash value. */
5019 ha = bfd_elf_hash (name);
5021 /* Store the found hash value in the array given as the argument. */
5024 /* And store it in the struct so that we can put it in the hash table
5026 h->u.elf_hash_value = ha;
5034 struct collect_gnu_hash_codes
5037 const struct elf_backend_data *bed;
5038 unsigned long int nsyms;
5039 unsigned long int maskbits;
5040 unsigned long int *hashcodes;
5041 unsigned long int *hashval;
5042 unsigned long int *indx;
5043 unsigned long int *counts;
5046 long int min_dynindx;
5047 unsigned long int bucketcount;
5048 unsigned long int symindx;
5049 long int local_indx;
5050 long int shift1, shift2;
5051 unsigned long int mask;
5054 /* This function will be called though elf_link_hash_traverse to store
5055 all hash value of the exported symbols in an array. */
5058 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5060 struct collect_gnu_hash_codes *s = data;
5066 if (h->root.type == bfd_link_hash_warning)
5067 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5069 /* Ignore indirect symbols. These are added by the versioning code. */
5070 if (h->dynindx == -1)
5073 /* Ignore also local symbols and undefined symbols. */
5074 if (! (*s->bed->elf_hash_symbol) (h))
5077 name = h->root.root.string;
5078 p = strchr (name, ELF_VER_CHR);
5081 alc = bfd_malloc (p - name + 1);
5082 memcpy (alc, name, p - name);
5083 alc[p - name] = '\0';
5087 /* Compute the hash value. */
5088 ha = bfd_elf_gnu_hash (name);
5090 /* Store the found hash value in the array for compute_bucket_count,
5091 and also for .dynsym reordering purposes. */
5092 s->hashcodes[s->nsyms] = ha;
5093 s->hashval[h->dynindx] = ha;
5095 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5096 s->min_dynindx = h->dynindx;
5104 /* This function will be called though elf_link_hash_traverse to do
5105 final dynaminc symbol renumbering. */
5108 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5110 struct collect_gnu_hash_codes *s = data;
5111 unsigned long int bucket;
5112 unsigned long int val;
5114 if (h->root.type == bfd_link_hash_warning)
5115 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5117 /* Ignore indirect symbols. */
5118 if (h->dynindx == -1)
5121 /* Ignore also local symbols and undefined symbols. */
5122 if (! (*s->bed->elf_hash_symbol) (h))
5124 if (h->dynindx >= s->min_dynindx)
5125 h->dynindx = s->local_indx++;
5129 bucket = s->hashval[h->dynindx] % s->bucketcount;
5130 val = (s->hashval[h->dynindx] >> s->shift1)
5131 & ((s->maskbits >> s->shift1) - 1);
5132 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5134 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5135 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5136 if (s->counts[bucket] == 1)
5137 /* Last element terminates the chain. */
5139 bfd_put_32 (s->output_bfd, val,
5140 s->contents + (s->indx[bucket] - s->symindx) * 4);
5141 --s->counts[bucket];
5142 h->dynindx = s->indx[bucket]++;
5146 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5149 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5151 return !(h->forced_local
5152 || h->root.type == bfd_link_hash_undefined
5153 || h->root.type == bfd_link_hash_undefweak
5154 || ((h->root.type == bfd_link_hash_defined
5155 || h->root.type == bfd_link_hash_defweak)
5156 && h->root.u.def.section->output_section == NULL));
5159 /* Array used to determine the number of hash table buckets to use
5160 based on the number of symbols there are. If there are fewer than
5161 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5162 fewer than 37 we use 17 buckets, and so forth. We never use more
5163 than 32771 buckets. */
5165 static const size_t elf_buckets[] =
5167 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5171 /* Compute bucket count for hashing table. We do not use a static set
5172 of possible tables sizes anymore. Instead we determine for all
5173 possible reasonable sizes of the table the outcome (i.e., the
5174 number of collisions etc) and choose the best solution. The
5175 weighting functions are not too simple to allow the table to grow
5176 without bounds. Instead one of the weighting factors is the size.
5177 Therefore the result is always a good payoff between few collisions
5178 (= short chain lengths) and table size. */
5180 compute_bucket_count (struct bfd_link_info *info, unsigned long int *hashcodes,
5181 unsigned long int nsyms, int gnu_hash)
5183 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5184 size_t best_size = 0;
5185 unsigned long int i;
5188 /* We have a problem here. The following code to optimize the table
5189 size requires an integer type with more the 32 bits. If
5190 BFD_HOST_U_64_BIT is set we know about such a type. */
5191 #ifdef BFD_HOST_U_64_BIT
5196 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5197 bfd *dynobj = elf_hash_table (info)->dynobj;
5198 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5199 unsigned long int *counts;
5201 /* Possible optimization parameters: if we have NSYMS symbols we say
5202 that the hashing table must at least have NSYMS/4 and at most
5204 minsize = nsyms / 4;
5207 best_size = maxsize = nsyms * 2;
5212 if ((best_size & 31) == 0)
5216 /* Create array where we count the collisions in. We must use bfd_malloc
5217 since the size could be large. */
5219 amt *= sizeof (unsigned long int);
5220 counts = bfd_malloc (amt);
5224 /* Compute the "optimal" size for the hash table. The criteria is a
5225 minimal chain length. The minor criteria is (of course) the size
5227 for (i = minsize; i < maxsize; ++i)
5229 /* Walk through the array of hashcodes and count the collisions. */
5230 BFD_HOST_U_64_BIT max;
5231 unsigned long int j;
5232 unsigned long int fact;
5234 if (gnu_hash && (i & 31) == 0)
5237 memset (counts, '\0', i * sizeof (unsigned long int));
5239 /* Determine how often each hash bucket is used. */
5240 for (j = 0; j < nsyms; ++j)
5241 ++counts[hashcodes[j] % i];
5243 /* For the weight function we need some information about the
5244 pagesize on the target. This is information need not be 100%
5245 accurate. Since this information is not available (so far) we
5246 define it here to a reasonable default value. If it is crucial
5247 to have a better value some day simply define this value. */
5248 # ifndef BFD_TARGET_PAGESIZE
5249 # define BFD_TARGET_PAGESIZE (4096)
5252 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5254 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5257 /* Variant 1: optimize for short chains. We add the squares
5258 of all the chain lengths (which favors many small chain
5259 over a few long chains). */
5260 for (j = 0; j < i; ++j)
5261 max += counts[j] * counts[j];
5263 /* This adds penalties for the overall size of the table. */
5264 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5267 /* Variant 2: Optimize a lot more for small table. Here we
5268 also add squares of the size but we also add penalties for
5269 empty slots (the +1 term). */
5270 for (j = 0; j < i; ++j)
5271 max += (1 + counts[j]) * (1 + counts[j]);
5273 /* The overall size of the table is considered, but not as
5274 strong as in variant 1, where it is squared. */
5275 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5279 /* Compare with current best results. */
5280 if (max < best_chlen)
5290 #endif /* defined (BFD_HOST_U_64_BIT) */
5292 /* This is the fallback solution if no 64bit type is available or if we
5293 are not supposed to spend much time on optimizations. We select the
5294 bucket count using a fixed set of numbers. */
5295 for (i = 0; elf_buckets[i] != 0; i++)
5297 best_size = elf_buckets[i];
5298 if (nsyms < elf_buckets[i + 1])
5301 if (gnu_hash && best_size < 2)
5308 /* Set up the sizes and contents of the ELF dynamic sections. This is
5309 called by the ELF linker emulation before_allocation routine. We
5310 must set the sizes of the sections before the linker sets the
5311 addresses of the various sections. */
5314 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5317 const char *filter_shlib,
5318 const char * const *auxiliary_filters,
5319 struct bfd_link_info *info,
5320 asection **sinterpptr,
5321 struct bfd_elf_version_tree *verdefs)
5323 bfd_size_type soname_indx;
5325 const struct elf_backend_data *bed;
5326 struct elf_assign_sym_version_info asvinfo;
5330 soname_indx = (bfd_size_type) -1;
5332 if (!is_elf_hash_table (info->hash))
5335 bed = get_elf_backend_data (output_bfd);
5336 elf_tdata (output_bfd)->relro = info->relro;
5337 if (info->execstack)
5338 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5339 else if (info->noexecstack)
5340 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5344 asection *notesec = NULL;
5347 for (inputobj = info->input_bfds;
5349 inputobj = inputobj->link_next)
5353 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
5355 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5358 if (s->flags & SEC_CODE)
5362 else if (bed->default_execstack)
5367 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5368 if (exec && info->relocatable
5369 && notesec->output_section != bfd_abs_section_ptr)
5370 notesec->output_section->flags |= SEC_CODE;
5374 /* Any syms created from now on start with -1 in
5375 got.refcount/offset and plt.refcount/offset. */
5376 elf_hash_table (info)->init_got_refcount
5377 = elf_hash_table (info)->init_got_offset;
5378 elf_hash_table (info)->init_plt_refcount
5379 = elf_hash_table (info)->init_plt_offset;
5381 /* The backend may have to create some sections regardless of whether
5382 we're dynamic or not. */
5383 if (bed->elf_backend_always_size_sections
5384 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5387 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5390 dynobj = elf_hash_table (info)->dynobj;
5392 /* If there were no dynamic objects in the link, there is nothing to
5397 if (elf_hash_table (info)->dynamic_sections_created)
5399 struct elf_info_failed eif;
5400 struct elf_link_hash_entry *h;
5402 struct bfd_elf_version_tree *t;
5403 struct bfd_elf_version_expr *d;
5405 bfd_boolean all_defined;
5407 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5408 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5412 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5414 if (soname_indx == (bfd_size_type) -1
5415 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5421 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5423 info->flags |= DF_SYMBOLIC;
5430 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5432 if (indx == (bfd_size_type) -1
5433 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5436 if (info->new_dtags)
5438 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5439 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5444 if (filter_shlib != NULL)
5448 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5449 filter_shlib, TRUE);
5450 if (indx == (bfd_size_type) -1
5451 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5455 if (auxiliary_filters != NULL)
5457 const char * const *p;
5459 for (p = auxiliary_filters; *p != NULL; p++)
5463 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5465 if (indx == (bfd_size_type) -1
5466 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5472 eif.verdefs = verdefs;
5475 /* If we are supposed to export all symbols into the dynamic symbol
5476 table (this is not the normal case), then do so. */
5477 if (info->export_dynamic
5478 || (info->executable && info->dynamic))
5480 elf_link_hash_traverse (elf_hash_table (info),
5481 _bfd_elf_export_symbol,
5487 /* Make all global versions with definition. */
5488 for (t = verdefs; t != NULL; t = t->next)
5489 for (d = t->globals.list; d != NULL; d = d->next)
5490 if (!d->symver && d->symbol)
5492 const char *verstr, *name;
5493 size_t namelen, verlen, newlen;
5495 struct elf_link_hash_entry *newh;
5498 namelen = strlen (name);
5500 verlen = strlen (verstr);
5501 newlen = namelen + verlen + 3;
5503 newname = bfd_malloc (newlen);
5504 if (newname == NULL)
5506 memcpy (newname, name, namelen);
5508 /* Check the hidden versioned definition. */
5509 p = newname + namelen;
5511 memcpy (p, verstr, verlen + 1);
5512 newh = elf_link_hash_lookup (elf_hash_table (info),
5513 newname, FALSE, FALSE,
5516 || (newh->root.type != bfd_link_hash_defined
5517 && newh->root.type != bfd_link_hash_defweak))
5519 /* Check the default versioned definition. */
5521 memcpy (p, verstr, verlen + 1);
5522 newh = elf_link_hash_lookup (elf_hash_table (info),
5523 newname, FALSE, FALSE,
5528 /* Mark this version if there is a definition and it is
5529 not defined in a shared object. */
5531 && !newh->def_dynamic
5532 && (newh->root.type == bfd_link_hash_defined
5533 || newh->root.type == bfd_link_hash_defweak))
5537 /* Attach all the symbols to their version information. */
5538 asvinfo.output_bfd = output_bfd;
5539 asvinfo.info = info;
5540 asvinfo.verdefs = verdefs;
5541 asvinfo.failed = FALSE;
5543 elf_link_hash_traverse (elf_hash_table (info),
5544 _bfd_elf_link_assign_sym_version,
5549 if (!info->allow_undefined_version)
5551 /* Check if all global versions have a definition. */
5553 for (t = verdefs; t != NULL; t = t->next)
5554 for (d = t->globals.list; d != NULL; d = d->next)
5555 if (!d->symver && !d->script)
5557 (*_bfd_error_handler)
5558 (_("%s: undefined version: %s"),
5559 d->pattern, t->name);
5560 all_defined = FALSE;
5565 bfd_set_error (bfd_error_bad_value);
5570 /* Find all symbols which were defined in a dynamic object and make
5571 the backend pick a reasonable value for them. */
5572 elf_link_hash_traverse (elf_hash_table (info),
5573 _bfd_elf_adjust_dynamic_symbol,
5578 /* Add some entries to the .dynamic section. We fill in some of the
5579 values later, in bfd_elf_final_link, but we must add the entries
5580 now so that we know the final size of the .dynamic section. */
5582 /* If there are initialization and/or finalization functions to
5583 call then add the corresponding DT_INIT/DT_FINI entries. */
5584 h = (info->init_function
5585 ? elf_link_hash_lookup (elf_hash_table (info),
5586 info->init_function, FALSE,
5593 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5596 h = (info->fini_function
5597 ? elf_link_hash_lookup (elf_hash_table (info),
5598 info->fini_function, FALSE,
5605 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5609 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5610 if (s != NULL && s->linker_has_input)
5612 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5613 if (! info->executable)
5618 for (sub = info->input_bfds; sub != NULL;
5619 sub = sub->link_next)
5620 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5621 for (o = sub->sections; o != NULL; o = o->next)
5622 if (elf_section_data (o)->this_hdr.sh_type
5623 == SHT_PREINIT_ARRAY)
5625 (*_bfd_error_handler)
5626 (_("%B: .preinit_array section is not allowed in DSO"),
5631 bfd_set_error (bfd_error_nonrepresentable_section);
5635 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5636 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5639 s = bfd_get_section_by_name (output_bfd, ".init_array");
5640 if (s != NULL && s->linker_has_input)
5642 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5643 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5646 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5647 if (s != NULL && s->linker_has_input)
5649 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5650 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5654 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5655 /* If .dynstr is excluded from the link, we don't want any of
5656 these tags. Strictly, we should be checking each section
5657 individually; This quick check covers for the case where
5658 someone does a /DISCARD/ : { *(*) }. */
5659 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5661 bfd_size_type strsize;
5663 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5664 if ((info->emit_hash
5665 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5666 || (info->emit_gnu_hash
5667 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5668 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5669 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5670 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5671 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5672 bed->s->sizeof_sym))
5677 /* The backend must work out the sizes of all the other dynamic
5679 if (bed->elf_backend_size_dynamic_sections
5680 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5683 if (elf_hash_table (info)->dynamic_sections_created)
5685 unsigned long section_sym_count;
5688 /* Set up the version definition section. */
5689 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5690 BFD_ASSERT (s != NULL);
5692 /* We may have created additional version definitions if we are
5693 just linking a regular application. */
5694 verdefs = asvinfo.verdefs;
5696 /* Skip anonymous version tag. */
5697 if (verdefs != NULL && verdefs->vernum == 0)
5698 verdefs = verdefs->next;
5700 if (verdefs == NULL && !info->create_default_symver)
5701 s->flags |= SEC_EXCLUDE;
5706 struct bfd_elf_version_tree *t;
5708 Elf_Internal_Verdef def;
5709 Elf_Internal_Verdaux defaux;
5710 struct bfd_link_hash_entry *bh;
5711 struct elf_link_hash_entry *h;
5717 /* Make space for the base version. */
5718 size += sizeof (Elf_External_Verdef);
5719 size += sizeof (Elf_External_Verdaux);
5722 /* Make space for the default version. */
5723 if (info->create_default_symver)
5725 size += sizeof (Elf_External_Verdef);
5729 for (t = verdefs; t != NULL; t = t->next)
5731 struct bfd_elf_version_deps *n;
5733 size += sizeof (Elf_External_Verdef);
5734 size += sizeof (Elf_External_Verdaux);
5737 for (n = t->deps; n != NULL; n = n->next)
5738 size += sizeof (Elf_External_Verdaux);
5742 s->contents = bfd_alloc (output_bfd, s->size);
5743 if (s->contents == NULL && s->size != 0)
5746 /* Fill in the version definition section. */
5750 def.vd_version = VER_DEF_CURRENT;
5751 def.vd_flags = VER_FLG_BASE;
5754 if (info->create_default_symver)
5756 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5757 def.vd_next = sizeof (Elf_External_Verdef);
5761 def.vd_aux = sizeof (Elf_External_Verdef);
5762 def.vd_next = (sizeof (Elf_External_Verdef)
5763 + sizeof (Elf_External_Verdaux));
5766 if (soname_indx != (bfd_size_type) -1)
5768 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5770 def.vd_hash = bfd_elf_hash (soname);
5771 defaux.vda_name = soname_indx;
5778 name = lbasename (output_bfd->filename);
5779 def.vd_hash = bfd_elf_hash (name);
5780 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5782 if (indx == (bfd_size_type) -1)
5784 defaux.vda_name = indx;
5786 defaux.vda_next = 0;
5788 _bfd_elf_swap_verdef_out (output_bfd, &def,
5789 (Elf_External_Verdef *) p);
5790 p += sizeof (Elf_External_Verdef);
5791 if (info->create_default_symver)
5793 /* Add a symbol representing this version. */
5795 if (! (_bfd_generic_link_add_one_symbol
5796 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5798 get_elf_backend_data (dynobj)->collect, &bh)))
5800 h = (struct elf_link_hash_entry *) bh;
5803 h->type = STT_OBJECT;
5804 h->verinfo.vertree = NULL;
5806 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5809 /* Create a duplicate of the base version with the same
5810 aux block, but different flags. */
5813 def.vd_aux = sizeof (Elf_External_Verdef);
5815 def.vd_next = (sizeof (Elf_External_Verdef)
5816 + sizeof (Elf_External_Verdaux));
5819 _bfd_elf_swap_verdef_out (output_bfd, &def,
5820 (Elf_External_Verdef *) p);
5821 p += sizeof (Elf_External_Verdef);
5823 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5824 (Elf_External_Verdaux *) p);
5825 p += sizeof (Elf_External_Verdaux);
5827 for (t = verdefs; t != NULL; t = t->next)
5830 struct bfd_elf_version_deps *n;
5833 for (n = t->deps; n != NULL; n = n->next)
5836 /* Add a symbol representing this version. */
5838 if (! (_bfd_generic_link_add_one_symbol
5839 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5841 get_elf_backend_data (dynobj)->collect, &bh)))
5843 h = (struct elf_link_hash_entry *) bh;
5846 h->type = STT_OBJECT;
5847 h->verinfo.vertree = t;
5849 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5852 def.vd_version = VER_DEF_CURRENT;
5854 if (t->globals.list == NULL
5855 && t->locals.list == NULL
5857 def.vd_flags |= VER_FLG_WEAK;
5858 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5859 def.vd_cnt = cdeps + 1;
5860 def.vd_hash = bfd_elf_hash (t->name);
5861 def.vd_aux = sizeof (Elf_External_Verdef);
5863 if (t->next != NULL)
5864 def.vd_next = (sizeof (Elf_External_Verdef)
5865 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5867 _bfd_elf_swap_verdef_out (output_bfd, &def,
5868 (Elf_External_Verdef *) p);
5869 p += sizeof (Elf_External_Verdef);
5871 defaux.vda_name = h->dynstr_index;
5872 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5874 defaux.vda_next = 0;
5875 if (t->deps != NULL)
5876 defaux.vda_next = sizeof (Elf_External_Verdaux);
5877 t->name_indx = defaux.vda_name;
5879 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5880 (Elf_External_Verdaux *) p);
5881 p += sizeof (Elf_External_Verdaux);
5883 for (n = t->deps; n != NULL; n = n->next)
5885 if (n->version_needed == NULL)
5887 /* This can happen if there was an error in the
5889 defaux.vda_name = 0;
5893 defaux.vda_name = n->version_needed->name_indx;
5894 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5897 if (n->next == NULL)
5898 defaux.vda_next = 0;
5900 defaux.vda_next = sizeof (Elf_External_Verdaux);
5902 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5903 (Elf_External_Verdaux *) p);
5904 p += sizeof (Elf_External_Verdaux);
5908 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5909 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5912 elf_tdata (output_bfd)->cverdefs = cdefs;
5915 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5917 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5920 else if (info->flags & DF_BIND_NOW)
5922 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5928 if (info->executable)
5929 info->flags_1 &= ~ (DF_1_INITFIRST
5932 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5936 /* Work out the size of the version reference section. */
5938 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5939 BFD_ASSERT (s != NULL);
5941 struct elf_find_verdep_info sinfo;
5943 sinfo.output_bfd = output_bfd;
5945 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5946 if (sinfo.vers == 0)
5948 sinfo.failed = FALSE;
5950 elf_link_hash_traverse (elf_hash_table (info),
5951 _bfd_elf_link_find_version_dependencies,
5954 if (elf_tdata (output_bfd)->verref == NULL)
5955 s->flags |= SEC_EXCLUDE;
5958 Elf_Internal_Verneed *t;
5963 /* Build the version definition section. */
5966 for (t = elf_tdata (output_bfd)->verref;
5970 Elf_Internal_Vernaux *a;
5972 size += sizeof (Elf_External_Verneed);
5974 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5975 size += sizeof (Elf_External_Vernaux);
5979 s->contents = bfd_alloc (output_bfd, s->size);
5980 if (s->contents == NULL)
5984 for (t = elf_tdata (output_bfd)->verref;
5989 Elf_Internal_Vernaux *a;
5993 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5996 t->vn_version = VER_NEED_CURRENT;
5998 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5999 elf_dt_name (t->vn_bfd) != NULL
6000 ? elf_dt_name (t->vn_bfd)
6001 : lbasename (t->vn_bfd->filename),
6003 if (indx == (bfd_size_type) -1)
6006 t->vn_aux = sizeof (Elf_External_Verneed);
6007 if (t->vn_nextref == NULL)
6010 t->vn_next = (sizeof (Elf_External_Verneed)
6011 + caux * sizeof (Elf_External_Vernaux));
6013 _bfd_elf_swap_verneed_out (output_bfd, t,
6014 (Elf_External_Verneed *) p);
6015 p += sizeof (Elf_External_Verneed);
6017 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6019 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6020 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6021 a->vna_nodename, FALSE);
6022 if (indx == (bfd_size_type) -1)
6025 if (a->vna_nextptr == NULL)
6028 a->vna_next = sizeof (Elf_External_Vernaux);
6030 _bfd_elf_swap_vernaux_out (output_bfd, a,
6031 (Elf_External_Vernaux *) p);
6032 p += sizeof (Elf_External_Vernaux);
6036 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6037 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6040 elf_tdata (output_bfd)->cverrefs = crefs;
6044 if ((elf_tdata (output_bfd)->cverrefs == 0
6045 && elf_tdata (output_bfd)->cverdefs == 0)
6046 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6047 §ion_sym_count) == 0)
6049 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6050 s->flags |= SEC_EXCLUDE;
6056 /* Find the first non-excluded output section. We'll use its
6057 section symbol for some emitted relocs. */
6059 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6063 for (s = output_bfd->sections; s != NULL; s = s->next)
6064 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6065 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6067 elf_hash_table (info)->text_index_section = s;
6072 /* Find two non-excluded output sections, one for code, one for data.
6073 We'll use their section symbols for some emitted relocs. */
6075 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6079 for (s = output_bfd->sections; s != NULL; s = s->next)
6080 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6081 == (SEC_ALLOC | SEC_READONLY))
6082 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6084 elf_hash_table (info)->text_index_section = s;
6088 for (s = output_bfd->sections; s != NULL; s = s->next)
6089 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6090 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6092 elf_hash_table (info)->data_index_section = s;
6096 if (elf_hash_table (info)->text_index_section == NULL)
6097 elf_hash_table (info)->text_index_section
6098 = elf_hash_table (info)->data_index_section;
6102 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6104 const struct elf_backend_data *bed;
6106 if (!is_elf_hash_table (info->hash))
6109 bed = get_elf_backend_data (output_bfd);
6110 (*bed->elf_backend_init_index_section) (output_bfd, info);
6112 if (elf_hash_table (info)->dynamic_sections_created)
6116 bfd_size_type dynsymcount;
6117 unsigned long section_sym_count;
6118 unsigned int dtagcount;
6120 dynobj = elf_hash_table (info)->dynobj;
6122 /* Assign dynsym indicies. In a shared library we generate a
6123 section symbol for each output section, which come first.
6124 Next come all of the back-end allocated local dynamic syms,
6125 followed by the rest of the global symbols. */
6127 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6128 §ion_sym_count);
6130 /* Work out the size of the symbol version section. */
6131 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6132 BFD_ASSERT (s != NULL);
6133 if (dynsymcount != 0
6134 && (s->flags & SEC_EXCLUDE) == 0)
6136 s->size = dynsymcount * sizeof (Elf_External_Versym);
6137 s->contents = bfd_zalloc (output_bfd, s->size);
6138 if (s->contents == NULL)
6141 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6145 /* Set the size of the .dynsym and .hash sections. We counted
6146 the number of dynamic symbols in elf_link_add_object_symbols.
6147 We will build the contents of .dynsym and .hash when we build
6148 the final symbol table, because until then we do not know the
6149 correct value to give the symbols. We built the .dynstr
6150 section as we went along in elf_link_add_object_symbols. */
6151 s = bfd_get_section_by_name (dynobj, ".dynsym");
6152 BFD_ASSERT (s != NULL);
6153 s->size = dynsymcount * bed->s->sizeof_sym;
6155 if (dynsymcount != 0)
6157 s->contents = bfd_alloc (output_bfd, s->size);
6158 if (s->contents == NULL)
6161 /* The first entry in .dynsym is a dummy symbol.
6162 Clear all the section syms, in case we don't output them all. */
6163 ++section_sym_count;
6164 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6167 elf_hash_table (info)->bucketcount = 0;
6169 /* Compute the size of the hashing table. As a side effect this
6170 computes the hash values for all the names we export. */
6171 if (info->emit_hash)
6173 unsigned long int *hashcodes;
6174 unsigned long int *hashcodesp;
6176 unsigned long int nsyms;
6178 size_t hash_entry_size;
6180 /* Compute the hash values for all exported symbols. At the same
6181 time store the values in an array so that we could use them for
6183 amt = dynsymcount * sizeof (unsigned long int);
6184 hashcodes = bfd_malloc (amt);
6185 if (hashcodes == NULL)
6187 hashcodesp = hashcodes;
6189 /* Put all hash values in HASHCODES. */
6190 elf_link_hash_traverse (elf_hash_table (info),
6191 elf_collect_hash_codes, &hashcodesp);
6193 nsyms = hashcodesp - hashcodes;
6195 = compute_bucket_count (info, hashcodes, nsyms, 0);
6198 if (bucketcount == 0)
6201 elf_hash_table (info)->bucketcount = bucketcount;
6203 s = bfd_get_section_by_name (dynobj, ".hash");
6204 BFD_ASSERT (s != NULL);
6205 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6206 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6207 s->contents = bfd_zalloc (output_bfd, s->size);
6208 if (s->contents == NULL)
6211 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6212 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6213 s->contents + hash_entry_size);
6216 if (info->emit_gnu_hash)
6219 unsigned char *contents;
6220 struct collect_gnu_hash_codes cinfo;
6224 memset (&cinfo, 0, sizeof (cinfo));
6226 /* Compute the hash values for all exported symbols. At the same
6227 time store the values in an array so that we could use them for
6229 amt = dynsymcount * 2 * sizeof (unsigned long int);
6230 cinfo.hashcodes = bfd_malloc (amt);
6231 if (cinfo.hashcodes == NULL)
6234 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6235 cinfo.min_dynindx = -1;
6236 cinfo.output_bfd = output_bfd;
6239 /* Put all hash values in HASHCODES. */
6240 elf_link_hash_traverse (elf_hash_table (info),
6241 elf_collect_gnu_hash_codes, &cinfo);
6244 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6246 if (bucketcount == 0)
6248 free (cinfo.hashcodes);
6252 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6253 BFD_ASSERT (s != NULL);
6255 if (cinfo.nsyms == 0)
6257 /* Empty .gnu.hash section is special. */
6258 BFD_ASSERT (cinfo.min_dynindx == -1);
6259 free (cinfo.hashcodes);
6260 s->size = 5 * 4 + bed->s->arch_size / 8;
6261 contents = bfd_zalloc (output_bfd, s->size);
6262 if (contents == NULL)
6264 s->contents = contents;
6265 /* 1 empty bucket. */
6266 bfd_put_32 (output_bfd, 1, contents);
6267 /* SYMIDX above the special symbol 0. */
6268 bfd_put_32 (output_bfd, 1, contents + 4);
6269 /* Just one word for bitmask. */
6270 bfd_put_32 (output_bfd, 1, contents + 8);
6271 /* Only hash fn bloom filter. */
6272 bfd_put_32 (output_bfd, 0, contents + 12);
6273 /* No hashes are valid - empty bitmask. */
6274 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6275 /* No hashes in the only bucket. */
6276 bfd_put_32 (output_bfd, 0,
6277 contents + 16 + bed->s->arch_size / 8);
6281 unsigned long int maskwords, maskbitslog2;
6282 BFD_ASSERT (cinfo.min_dynindx != -1);
6284 maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
6285 if (maskbitslog2 < 3)
6287 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6288 maskbitslog2 = maskbitslog2 + 3;
6290 maskbitslog2 = maskbitslog2 + 2;
6291 if (bed->s->arch_size == 64)
6293 if (maskbitslog2 == 5)
6299 cinfo.mask = (1 << cinfo.shift1) - 1;
6300 cinfo.shift2 = maskbitslog2;
6301 cinfo.maskbits = 1 << maskbitslog2;
6302 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6303 amt = bucketcount * sizeof (unsigned long int) * 2;
6304 amt += maskwords * sizeof (bfd_vma);
6305 cinfo.bitmask = bfd_malloc (amt);
6306 if (cinfo.bitmask == NULL)
6308 free (cinfo.hashcodes);
6312 cinfo.counts = (void *) (cinfo.bitmask + maskwords);
6313 cinfo.indx = cinfo.counts + bucketcount;
6314 cinfo.symindx = dynsymcount - cinfo.nsyms;
6315 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6317 /* Determine how often each hash bucket is used. */
6318 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6319 for (i = 0; i < cinfo.nsyms; ++i)
6320 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6322 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6323 if (cinfo.counts[i] != 0)
6325 cinfo.indx[i] = cnt;
6326 cnt += cinfo.counts[i];
6328 BFD_ASSERT (cnt == dynsymcount);
6329 cinfo.bucketcount = bucketcount;
6330 cinfo.local_indx = cinfo.min_dynindx;
6332 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6333 s->size += cinfo.maskbits / 8;
6334 contents = bfd_zalloc (output_bfd, s->size);
6335 if (contents == NULL)
6337 free (cinfo.bitmask);
6338 free (cinfo.hashcodes);
6342 s->contents = contents;
6343 bfd_put_32 (output_bfd, bucketcount, contents);
6344 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6345 bfd_put_32 (output_bfd, maskwords, contents + 8);
6346 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6347 contents += 16 + cinfo.maskbits / 8;
6349 for (i = 0; i < bucketcount; ++i)
6351 if (cinfo.counts[i] == 0)
6352 bfd_put_32 (output_bfd, 0, contents);
6354 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6358 cinfo.contents = contents;
6360 /* Renumber dynamic symbols, populate .gnu.hash section. */
6361 elf_link_hash_traverse (elf_hash_table (info),
6362 elf_renumber_gnu_hash_syms, &cinfo);
6364 contents = s->contents + 16;
6365 for (i = 0; i < maskwords; ++i)
6367 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6369 contents += bed->s->arch_size / 8;
6372 free (cinfo.bitmask);
6373 free (cinfo.hashcodes);
6377 s = bfd_get_section_by_name (dynobj, ".dynstr");
6378 BFD_ASSERT (s != NULL);
6380 elf_finalize_dynstr (output_bfd, info);
6382 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6384 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6385 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6392 /* Final phase of ELF linker. */
6394 /* A structure we use to avoid passing large numbers of arguments. */
6396 struct elf_final_link_info
6398 /* General link information. */
6399 struct bfd_link_info *info;
6402 /* Symbol string table. */
6403 struct bfd_strtab_hash *symstrtab;
6404 /* .dynsym section. */
6405 asection *dynsym_sec;
6406 /* .hash section. */
6408 /* symbol version section (.gnu.version). */
6409 asection *symver_sec;
6410 /* Buffer large enough to hold contents of any section. */
6412 /* Buffer large enough to hold external relocs of any section. */
6413 void *external_relocs;
6414 /* Buffer large enough to hold internal relocs of any section. */
6415 Elf_Internal_Rela *internal_relocs;
6416 /* Buffer large enough to hold external local symbols of any input
6418 bfd_byte *external_syms;
6419 /* And a buffer for symbol section indices. */
6420 Elf_External_Sym_Shndx *locsym_shndx;
6421 /* Buffer large enough to hold internal local symbols of any input
6423 Elf_Internal_Sym *internal_syms;
6424 /* Array large enough to hold a symbol index for each local symbol
6425 of any input BFD. */
6427 /* Array large enough to hold a section pointer for each local
6428 symbol of any input BFD. */
6429 asection **sections;
6430 /* Buffer to hold swapped out symbols. */
6432 /* And one for symbol section indices. */
6433 Elf_External_Sym_Shndx *symshndxbuf;
6434 /* Number of swapped out symbols in buffer. */
6435 size_t symbuf_count;
6436 /* Number of symbols which fit in symbuf. */
6438 /* And same for symshndxbuf. */
6439 size_t shndxbuf_size;
6442 /* This struct is used to pass information to elf_link_output_extsym. */
6444 struct elf_outext_info
6447 bfd_boolean localsyms;
6448 struct elf_final_link_info *finfo;
6452 /* Support for evaluating a complex relocation.
6454 Complex relocations are generalized, self-describing relocations. The
6455 implementation of them consists of two parts: complex symbols, and the
6456 relocations themselves.
6458 The relocations are use a reserved elf-wide relocation type code (R_RELC
6459 external / BFD_RELOC_RELC internal) and an encoding of relocation field
6460 information (start bit, end bit, word width, etc) into the addend. This
6461 information is extracted from CGEN-generated operand tables within gas.
6463 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
6464 internal) representing prefix-notation expressions, including but not
6465 limited to those sorts of expressions normally encoded as addends in the
6466 addend field. The symbol mangling format is:
6469 | <unary-operator> ':' <node>
6470 | <binary-operator> ':' <node> ':' <node>
6473 <literal> := 's' <digits=N> ':' <N character symbol name>
6474 | 'S' <digits=N> ':' <N character section name>
6478 <binary-operator> := as in C
6479 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
6482 set_symbol_value (bfd * bfd_with_globals,
6483 struct elf_final_link_info * finfo,
6487 bfd_boolean is_local;
6488 Elf_Internal_Sym * sym;
6489 struct elf_link_hash_entry ** sym_hashes;
6490 struct elf_link_hash_entry * h;
6492 sym_hashes = elf_sym_hashes (bfd_with_globals);
6493 sym = finfo->internal_syms + symidx;
6494 is_local = ELF_ST_BIND(sym->st_info) == STB_LOCAL;
6498 /* It is a local symbol: move it to the
6499 "absolute" section and give it a value. */
6500 sym->st_shndx = SHN_ABS;
6501 sym->st_value = val;
6505 /* It is a global symbol: set its link type
6506 to "defined" and give it a value. */
6507 h = sym_hashes [symidx];
6508 while (h->root.type == bfd_link_hash_indirect
6509 || h->root.type == bfd_link_hash_warning)
6510 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6511 h->root.type = bfd_link_hash_defined;
6512 h->root.u.def.value = val;
6513 h->root.u.def.section = bfd_abs_section_ptr;
6518 resolve_symbol (const char * name,
6520 struct elf_final_link_info * finfo,
6524 Elf_Internal_Sym * sym;
6525 struct bfd_link_hash_entry * global_entry;
6526 const char * candidate = NULL;
6527 Elf_Internal_Shdr * symtab_hdr;
6528 asection * sec = NULL;
6531 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
6533 for (i = 0; i < locsymcount; ++ i)
6535 sym = finfo->internal_syms + i;
6536 sec = finfo->sections [i];
6538 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
6541 candidate = bfd_elf_string_from_elf_section (input_bfd,
6542 symtab_hdr->sh_link,
6545 printf ("Comparing string: '%s' vs. '%s' = 0x%x\n",
6546 name, candidate, (unsigned int)sym->st_value);
6548 if (candidate && strcmp (candidate, name) == 0)
6550 * result = sym->st_value;
6552 if (sym->st_shndx > SHN_UNDEF &&
6553 sym->st_shndx < SHN_LORESERVE)
6556 printf ("adjusting for sec '%s' @ 0x%x + 0x%x\n",
6557 sec->output_section->name,
6558 (unsigned int)sec->output_section->vma,
6559 (unsigned int)sec->output_offset);
6561 * result += sec->output_offset + sec->output_section->vma;
6564 printf ("Found symbol with effective value %8.8x\n", (unsigned int)* result);
6570 /* Hmm, haven't found it yet. perhaps it is a global. */
6571 global_entry = bfd_link_hash_lookup (finfo->info->hash, name, FALSE, FALSE, TRUE);
6575 if (global_entry->type == bfd_link_hash_defined
6576 || global_entry->type == bfd_link_hash_defweak)
6578 * result = global_entry->u.def.value
6579 + global_entry->u.def.section->output_section->vma
6580 + global_entry->u.def.section->output_offset;
6582 printf ("Found GLOBAL symbol '%s' with value %8.8x\n",
6583 global_entry->root.string, (unsigned int)*result);
6588 if (global_entry->type == bfd_link_hash_common)
6590 *result = global_entry->u.def.value +
6591 bfd_com_section_ptr->output_section->vma +
6592 bfd_com_section_ptr->output_offset;
6594 printf ("Found COMMON symbol '%s' with value %8.8x\n",
6595 global_entry->root.string, (unsigned int)*result);
6604 resolve_section (const char * name,
6605 asection * sections,
6611 for (curr = sections; curr; curr = curr->next)
6612 if (strcmp (curr->name, name) == 0)
6614 *result = curr->vma;
6618 /* Hmm. still haven't found it. try pseudo-section names. */
6619 for (curr = sections; curr; curr = curr->next)
6621 len = strlen (curr->name);
6622 if (len > strlen (name))
6625 if (strncmp (curr->name, name, len) == 0)
6627 if (strncmp (".end", name + len, 4) == 0)
6629 *result = curr->vma + curr->size;
6633 /* Insert more pseudo-section names here, if you like. */
6641 undefined_reference (const char * reftype,
6644 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), reftype, name);
6648 eval_symbol (bfd_vma * result,
6652 struct elf_final_link_info * finfo,
6654 bfd_vma section_offset,
6662 const int bufsz = 4096;
6663 char symbuf [bufsz];
6664 const char * symend;
6665 bfd_boolean symbol_is_section = FALSE;
6670 if (len < 1 || len > bufsz)
6672 bfd_set_error (bfd_error_invalid_operation);
6679 * result = addr + section_offset;
6680 * advanced = sym + 1;
6685 * result = strtoul (sym, advanced, 16);
6689 symbol_is_section = TRUE;
6692 symlen = strtol (sym, &sym, 10);
6693 ++ sym; /* Skip the trailing ':'. */
6695 if ((symend < sym) || ((symlen + 1) > bufsz))
6697 bfd_set_error (bfd_error_invalid_operation);
6701 memcpy (symbuf, sym, symlen);
6702 symbuf [symlen] = '\0';
6703 * advanced = sym + symlen;
6705 /* Is it always possible, with complex symbols, that gas "mis-guessed"
6706 the symbol as a section, or vice-versa. so we're pretty liberal in our
6707 interpretation here; section means "try section first", not "must be a
6708 section", and likewise with symbol. */
6710 if (symbol_is_section)
6712 if ((resolve_section (symbuf, finfo->output_bfd->sections, result) != TRUE)
6713 && (resolve_symbol (symbuf, input_bfd, finfo, result, locsymcount) != TRUE))
6715 undefined_reference ("section", symbuf);
6721 if ((resolve_symbol (symbuf, input_bfd, finfo, result, locsymcount) != TRUE)
6722 && (resolve_section (symbuf, finfo->output_bfd->sections,
6725 undefined_reference ("symbol", symbuf);
6732 /* All that remains are operators. */
6734 #define UNARY_OP(op) \
6735 if (strncmp (sym, #op, strlen (#op)) == 0) \
6737 sym += strlen (#op); \
6740 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6741 section_offset, locsymcount, signed_p) \
6745 * result = op ((signed)a); \
6752 #define BINARY_OP(op) \
6753 if (strncmp (sym, #op, strlen (#op)) == 0) \
6755 sym += strlen (#op); \
6758 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
6759 section_offset, locsymcount, signed_p) \
6763 if (eval_symbol (& b, sym, & sym, input_bfd, finfo, addr, \
6764 section_offset, locsymcount, signed_p) \
6768 * result = ((signed) a) op ((signed) b); \
6770 * result = a op b; \
6799 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
6800 bfd_set_error (bfd_error_invalid_operation);
6805 /* Entry point to evaluator, called from elf_link_input_bfd. */
6808 evaluate_complex_relocation_symbols (bfd * input_bfd,
6809 struct elf_final_link_info * finfo,
6812 const struct elf_backend_data * bed;
6813 Elf_Internal_Shdr * symtab_hdr;
6814 struct elf_link_hash_entry ** sym_hashes;
6815 asection * reloc_sec;
6816 bfd_boolean result = TRUE;
6818 /* For each section, we're going to check and see if it has any
6819 complex relocations, and we're going to evaluate any of them
6822 if (finfo->info->relocatable)
6825 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
6826 sym_hashes = elf_sym_hashes (input_bfd);
6827 bed = get_elf_backend_data (input_bfd);
6829 for (reloc_sec = input_bfd->sections; reloc_sec; reloc_sec = reloc_sec->next)
6831 Elf_Internal_Rela * internal_relocs;
6834 /* This section was omitted from the link. */
6835 if (! reloc_sec->linker_mark)
6838 /* Only process sections containing relocs. */
6839 if ((reloc_sec->flags & SEC_RELOC) == 0)
6842 if (reloc_sec->reloc_count == 0)
6845 /* Read in the relocs for this section. */
6847 = _bfd_elf_link_read_relocs (input_bfd, reloc_sec, NULL,
6848 (Elf_Internal_Rela *) NULL,
6850 if (internal_relocs == NULL)
6853 for (i = reloc_sec->reloc_count; i--;)
6855 Elf_Internal_Rela * rel;
6858 Elf_Internal_Sym * sym;
6860 bfd_vma section_offset;
6864 rel = internal_relocs + i;
6865 section_offset = reloc_sec->output_section->vma
6866 + reloc_sec->output_offset;
6867 addr = rel->r_offset;
6869 index = ELF32_R_SYM (rel->r_info);
6870 if (bed->s->arch_size == 64)
6873 if (index == STN_UNDEF)
6876 if (index < locsymcount)
6878 /* The symbol is local. */
6879 sym = finfo->internal_syms + index;
6881 /* We're only processing STT_RELC or STT_SRELC type symbols. */
6882 if ((ELF_ST_TYPE (sym->st_info) != STT_RELC) &&
6883 (ELF_ST_TYPE (sym->st_info) != STT_SRELC))
6886 sym_name = bfd_elf_string_from_elf_section
6887 (input_bfd, symtab_hdr->sh_link, sym->st_name);
6889 signed_p = (ELF_ST_TYPE (sym->st_info) == STT_SRELC);
6893 /* The symbol is global. */
6894 struct elf_link_hash_entry * h;
6896 if (elf_bad_symtab (input_bfd))
6899 h = sym_hashes [index - locsymcount];
6900 while ( h->root.type == bfd_link_hash_indirect
6901 || h->root.type == bfd_link_hash_warning)
6902 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6904 if (h->type != STT_RELC && h->type != STT_SRELC)
6907 signed_p = (h->type == STT_SRELC);
6908 sym_name = (char *) h->root.root.string;
6911 printf ("Encountered a complex symbol!");
6912 printf (" (input_bfd %s, section %s, reloc %ld\n",
6913 input_bfd->filename, reloc_sec->name, i);
6914 printf (" symbol: idx %8.8lx, name %s\n",
6916 printf (" reloc : info %8.8lx, addr %8.8lx\n",
6918 printf (" Evaluating '%s' ...\n ", sym_name);
6920 if (eval_symbol (& result, sym_name, & sym_name, input_bfd,
6921 finfo, addr, section_offset, locsymcount,
6923 /* Symbol evaluated OK. Update to absolute value. */
6924 set_symbol_value (input_bfd, finfo, index, result);
6930 if (internal_relocs != elf_section_data (reloc_sec)->relocs)
6931 free (internal_relocs);
6934 /* If nothing went wrong, then we adjusted
6935 everything we wanted to adjust. */
6940 put_value (bfd_vma size,
6941 unsigned long chunksz,
6944 bfd_byte * location)
6946 location += (size - chunksz);
6948 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
6956 bfd_put_8 (input_bfd, x, location);
6959 bfd_put_16 (input_bfd, x, location);
6962 bfd_put_32 (input_bfd, x, location);
6966 bfd_put_64 (input_bfd, x, location);
6976 get_value (bfd_vma size,
6977 unsigned long chunksz,
6979 bfd_byte * location)
6983 for (; size; size -= chunksz, location += chunksz)
6991 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
6994 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
6997 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7001 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7012 decode_complex_addend
7013 (unsigned long * start, /* in bits */
7014 unsigned long * oplen, /* in bits */
7015 unsigned long * len, /* in bits */
7016 unsigned long * wordsz, /* in bytes */
7017 unsigned long * chunksz, /* in bytes */
7018 unsigned long * lsb0_p,
7019 unsigned long * signed_p,
7020 unsigned long * trunc_p,
7021 unsigned long encoded)
7023 * start = encoded & 0x3F;
7024 * len = (encoded >> 6) & 0x3F;
7025 * oplen = (encoded >> 12) & 0x3F;
7026 * wordsz = (encoded >> 18) & 0xF;
7027 * chunksz = (encoded >> 22) & 0xF;
7028 * lsb0_p = (encoded >> 27) & 1;
7029 * signed_p = (encoded >> 28) & 1;
7030 * trunc_p = (encoded >> 29) & 1;
7034 bfd_elf_perform_complex_relocation
7035 (bfd * output_bfd ATTRIBUTE_UNUSED,
7036 struct bfd_link_info * info,
7038 asection * input_section,
7039 bfd_byte * contents,
7040 Elf_Internal_Rela * rel,
7041 Elf_Internal_Sym * local_syms,
7042 asection ** local_sections)
7044 const struct elf_backend_data * bed;
7045 Elf_Internal_Shdr * symtab_hdr;
7047 bfd_vma relocation = 0, shift, x;
7050 unsigned long start, oplen, len, wordsz,
7051 chunksz, lsb0_p, signed_p, trunc_p;
7053 /* Perform this reloc, since it is complex.
7054 (this is not to say that it necessarily refers to a complex
7055 symbol; merely that it is a self-describing CGEN based reloc.
7056 i.e. the addend has the complete reloc information (bit start, end,
7057 word size, etc) encoded within it.). */
7058 r_symndx = ELF32_R_SYM (rel->r_info);
7059 bed = get_elf_backend_data (input_bfd);
7060 if (bed->s->arch_size == 64)
7064 printf ("Performing complex relocation %ld...\n", r_symndx);
7067 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7068 if (r_symndx < symtab_hdr->sh_info)
7070 /* The symbol is local. */
7071 Elf_Internal_Sym * sym;
7073 sym = local_syms + r_symndx;
7074 sec = local_sections [r_symndx];
7075 relocation = sym->st_value;
7076 if (sym->st_shndx > SHN_UNDEF &&
7077 sym->st_shndx < SHN_LORESERVE)
7078 relocation += (sec->output_offset +
7079 sec->output_section->vma);
7083 /* The symbol is global. */
7084 struct elf_link_hash_entry **sym_hashes;
7085 struct elf_link_hash_entry * h;
7087 sym_hashes = elf_sym_hashes (input_bfd);
7088 h = sym_hashes [r_symndx];
7090 while (h->root.type == bfd_link_hash_indirect
7091 || h->root.type == bfd_link_hash_warning)
7092 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7094 if (h->root.type == bfd_link_hash_defined
7095 || h->root.type == bfd_link_hash_defweak)
7097 sec = h->root.u.def.section;
7098 relocation = h->root.u.def.value;
7100 if (! bfd_is_abs_section (sec))
7101 relocation += (sec->output_section->vma
7102 + sec->output_offset);
7104 if (h->root.type == bfd_link_hash_undefined
7105 && !((*info->callbacks->undefined_symbol)
7106 (info, h->root.root.string, input_bfd,
7107 input_section, rel->r_offset,
7108 info->unresolved_syms_in_objects == RM_GENERATE_ERROR
7109 || ELF_ST_VISIBILITY (h->other))))
7113 decode_complex_addend (& start, & oplen, & len, & wordsz,
7114 & chunksz, & lsb0_p, & signed_p,
7115 & trunc_p, rel->r_addend);
7117 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7120 shift = (start + 1) - len;
7122 shift = (8 * wordsz) - (start + len);
7124 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7127 printf ("Doing complex reloc: "
7128 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7129 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7130 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7131 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7132 oplen, x, mask, relocation);
7137 /* Now do an overflow check. */
7138 if (bfd_check_overflow ((signed_p ?
7139 complain_overflow_signed :
7140 complain_overflow_unsigned),
7141 len, 0, (8 * wordsz),
7142 relocation) == bfd_reloc_overflow)
7143 (*_bfd_error_handler)
7144 ("%s (%s + 0x%lx): relocation overflow: 0x%lx %sdoes not fit "
7146 input_bfd->filename, input_section->name, rel->r_offset,
7147 relocation, (signed_p ? "(signed) " : ""), mask);
7151 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7154 printf (" relocation: %8.8lx\n"
7155 " shifted mask: %8.8lx\n"
7156 " shifted/masked reloc: %8.8lx\n"
7157 " result: %8.8lx\n",
7158 relocation, (mask << shift),
7159 ((relocation & mask) << shift), x);
7161 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
7164 /* When performing a relocatable link, the input relocations are
7165 preserved. But, if they reference global symbols, the indices
7166 referenced must be updated. Update all the relocations in
7167 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7170 elf_link_adjust_relocs (bfd *abfd,
7171 Elf_Internal_Shdr *rel_hdr,
7173 struct elf_link_hash_entry **rel_hash)
7176 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7178 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7179 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7180 bfd_vma r_type_mask;
7183 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
7185 swap_in = bed->s->swap_reloc_in;
7186 swap_out = bed->s->swap_reloc_out;
7188 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
7190 swap_in = bed->s->swap_reloca_in;
7191 swap_out = bed->s->swap_reloca_out;
7196 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7199 if (bed->s->arch_size == 32)
7206 r_type_mask = 0xffffffff;
7210 erela = rel_hdr->contents;
7211 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
7213 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7216 if (*rel_hash == NULL)
7219 BFD_ASSERT ((*rel_hash)->indx >= 0);
7221 (*swap_in) (abfd, erela, irela);
7222 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7223 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7224 | (irela[j].r_info & r_type_mask));
7225 (*swap_out) (abfd, irela, erela);
7229 struct elf_link_sort_rela
7235 enum elf_reloc_type_class type;
7236 /* We use this as an array of size int_rels_per_ext_rel. */
7237 Elf_Internal_Rela rela[1];
7241 elf_link_sort_cmp1 (const void *A, const void *B)
7243 const struct elf_link_sort_rela *a = A;
7244 const struct elf_link_sort_rela *b = B;
7245 int relativea, relativeb;
7247 relativea = a->type == reloc_class_relative;
7248 relativeb = b->type == reloc_class_relative;
7250 if (relativea < relativeb)
7252 if (relativea > relativeb)
7254 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
7256 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
7258 if (a->rela->r_offset < b->rela->r_offset)
7260 if (a->rela->r_offset > b->rela->r_offset)
7266 elf_link_sort_cmp2 (const void *A, const void *B)
7268 const struct elf_link_sort_rela *a = A;
7269 const struct elf_link_sort_rela *b = B;
7272 if (a->u.offset < b->u.offset)
7274 if (a->u.offset > b->u.offset)
7276 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
7277 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
7282 if (a->rela->r_offset < b->rela->r_offset)
7284 if (a->rela->r_offset > b->rela->r_offset)
7290 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
7292 asection *dynamic_relocs;
7295 bfd_size_type count, size;
7296 size_t i, ret, sort_elt, ext_size;
7297 bfd_byte *sort, *s_non_relative, *p;
7298 struct elf_link_sort_rela *sq;
7299 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7300 int i2e = bed->s->int_rels_per_ext_rel;
7301 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7302 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7303 struct bfd_link_order *lo;
7305 bfd_boolean use_rela;
7307 /* Find a dynamic reloc section. */
7308 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
7309 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
7310 if (rela_dyn != NULL && rela_dyn->size > 0
7311 && rel_dyn != NULL && rel_dyn->size > 0)
7313 bfd_boolean use_rela_initialised = FALSE;
7315 /* This is just here to stop gcc from complaining.
7316 It's initialization checking code is not perfect. */
7319 /* Both sections are present. Examine the sizes
7320 of the indirect sections to help us choose. */
7321 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7322 if (lo->type == bfd_indirect_link_order)
7324 asection *o = lo->u.indirect.section;
7326 if ((o->size % bed->s->sizeof_rela) == 0)
7328 if ((o->size % bed->s->sizeof_rel) == 0)
7329 /* Section size is divisible by both rel and rela sizes.
7330 It is of no help to us. */
7334 /* Section size is only divisible by rela. */
7335 if (use_rela_initialised && (use_rela == FALSE))
7338 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7339 bfd_set_error (bfd_error_invalid_operation);
7345 use_rela_initialised = TRUE;
7349 else if ((o->size % bed->s->sizeof_rel) == 0)
7351 /* Section size is only divisible by rel. */
7352 if (use_rela_initialised && (use_rela == TRUE))
7355 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7356 bfd_set_error (bfd_error_invalid_operation);
7362 use_rela_initialised = TRUE;
7367 /* The section size is not divisible by either - something is wrong. */
7369 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
7370 bfd_set_error (bfd_error_invalid_operation);
7375 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7376 if (lo->type == bfd_indirect_link_order)
7378 asection *o = lo->u.indirect.section;
7380 if ((o->size % bed->s->sizeof_rela) == 0)
7382 if ((o->size % bed->s->sizeof_rel) == 0)
7383 /* Section size is divisible by both rel and rela sizes.
7384 It is of no help to us. */
7388 /* Section size is only divisible by rela. */
7389 if (use_rela_initialised && (use_rela == FALSE))
7392 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7393 bfd_set_error (bfd_error_invalid_operation);
7399 use_rela_initialised = TRUE;
7403 else if ((o->size % bed->s->sizeof_rel) == 0)
7405 /* Section size is only divisible by rel. */
7406 if (use_rela_initialised && (use_rela == TRUE))
7409 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7410 bfd_set_error (bfd_error_invalid_operation);
7416 use_rela_initialised = TRUE;
7421 /* The section size is not divisible by either - something is wrong. */
7423 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
7424 bfd_set_error (bfd_error_invalid_operation);
7429 if (! use_rela_initialised)
7433 else if (rela_dyn != NULL && rela_dyn->size > 0)
7435 else if (rel_dyn != NULL && rel_dyn->size > 0)
7442 dynamic_relocs = rela_dyn;
7443 ext_size = bed->s->sizeof_rela;
7444 swap_in = bed->s->swap_reloca_in;
7445 swap_out = bed->s->swap_reloca_out;
7449 dynamic_relocs = rel_dyn;
7450 ext_size = bed->s->sizeof_rel;
7451 swap_in = bed->s->swap_reloc_in;
7452 swap_out = bed->s->swap_reloc_out;
7456 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
7457 if (lo->type == bfd_indirect_link_order)
7458 size += lo->u.indirect.section->size;
7460 if (size != dynamic_relocs->size)
7463 sort_elt = (sizeof (struct elf_link_sort_rela)
7464 + (i2e - 1) * sizeof (Elf_Internal_Rela));
7466 count = dynamic_relocs->size / ext_size;
7467 sort = bfd_zmalloc (sort_elt * count);
7471 (*info->callbacks->warning)
7472 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
7476 if (bed->s->arch_size == 32)
7477 r_sym_mask = ~(bfd_vma) 0xff;
7479 r_sym_mask = ~(bfd_vma) 0xffffffff;
7481 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
7482 if (lo->type == bfd_indirect_link_order)
7484 bfd_byte *erel, *erelend;
7485 asection *o = lo->u.indirect.section;
7487 if (o->contents == NULL && o->size != 0)
7489 /* This is a reloc section that is being handled as a normal
7490 section. See bfd_section_from_shdr. We can't combine
7491 relocs in this case. */
7496 erelend = o->contents + o->size;
7497 p = sort + o->output_offset / ext_size * sort_elt;
7499 while (erel < erelend)
7501 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
7503 (*swap_in) (abfd, erel, s->rela);
7504 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
7505 s->u.sym_mask = r_sym_mask;
7511 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
7513 for (i = 0, p = sort; i < count; i++, p += sort_elt)
7515 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
7516 if (s->type != reloc_class_relative)
7522 sq = (struct elf_link_sort_rela *) s_non_relative;
7523 for (; i < count; i++, p += sort_elt)
7525 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
7526 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
7528 sp->u.offset = sq->rela->r_offset;
7531 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
7533 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
7534 if (lo->type == bfd_indirect_link_order)
7536 bfd_byte *erel, *erelend;
7537 asection *o = lo->u.indirect.section;
7540 erelend = o->contents + o->size;
7541 p = sort + o->output_offset / ext_size * sort_elt;
7542 while (erel < erelend)
7544 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
7545 (*swap_out) (abfd, s->rela, erel);
7552 *psec = dynamic_relocs;
7556 /* Flush the output symbols to the file. */
7559 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
7560 const struct elf_backend_data *bed)
7562 if (finfo->symbuf_count > 0)
7564 Elf_Internal_Shdr *hdr;
7568 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
7569 pos = hdr->sh_offset + hdr->sh_size;
7570 amt = finfo->symbuf_count * bed->s->sizeof_sym;
7571 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
7572 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
7575 hdr->sh_size += amt;
7576 finfo->symbuf_count = 0;
7582 /* Add a symbol to the output symbol table. */
7585 elf_link_output_sym (struct elf_final_link_info *finfo,
7587 Elf_Internal_Sym *elfsym,
7588 asection *input_sec,
7589 struct elf_link_hash_entry *h)
7592 Elf_External_Sym_Shndx *destshndx;
7593 bfd_boolean (*output_symbol_hook)
7594 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
7595 struct elf_link_hash_entry *);
7596 const struct elf_backend_data *bed;
7598 bed = get_elf_backend_data (finfo->output_bfd);
7599 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
7600 if (output_symbol_hook != NULL)
7602 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
7606 if (name == NULL || *name == '\0')
7607 elfsym->st_name = 0;
7608 else if (input_sec->flags & SEC_EXCLUDE)
7609 elfsym->st_name = 0;
7612 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
7614 if (elfsym->st_name == (unsigned long) -1)
7618 if (finfo->symbuf_count >= finfo->symbuf_size)
7620 if (! elf_link_flush_output_syms (finfo, bed))
7624 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
7625 destshndx = finfo->symshndxbuf;
7626 if (destshndx != NULL)
7628 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
7632 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
7633 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
7634 if (destshndx == NULL)
7636 memset ((char *) destshndx + amt, 0, amt);
7637 finfo->shndxbuf_size *= 2;
7639 destshndx += bfd_get_symcount (finfo->output_bfd);
7642 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
7643 finfo->symbuf_count += 1;
7644 bfd_get_symcount (finfo->output_bfd) += 1;
7649 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
7652 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
7654 if (sym->st_shndx > SHN_HIRESERVE)
7656 /* The gABI doesn't support dynamic symbols in output sections
7658 (*_bfd_error_handler)
7659 (_("%B: Too many sections: %d (>= %d)"),
7660 abfd, bfd_count_sections (abfd), SHN_LORESERVE);
7661 bfd_set_error (bfd_error_nonrepresentable_section);
7667 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
7668 allowing an unsatisfied unversioned symbol in the DSO to match a
7669 versioned symbol that would normally require an explicit version.
7670 We also handle the case that a DSO references a hidden symbol
7671 which may be satisfied by a versioned symbol in another DSO. */
7674 elf_link_check_versioned_symbol (struct bfd_link_info *info,
7675 const struct elf_backend_data *bed,
7676 struct elf_link_hash_entry *h)
7679 struct elf_link_loaded_list *loaded;
7681 if (!is_elf_hash_table (info->hash))
7684 switch (h->root.type)
7690 case bfd_link_hash_undefined:
7691 case bfd_link_hash_undefweak:
7692 abfd = h->root.u.undef.abfd;
7693 if ((abfd->flags & DYNAMIC) == 0
7694 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
7698 case bfd_link_hash_defined:
7699 case bfd_link_hash_defweak:
7700 abfd = h->root.u.def.section->owner;
7703 case bfd_link_hash_common:
7704 abfd = h->root.u.c.p->section->owner;
7707 BFD_ASSERT (abfd != NULL);
7709 for (loaded = elf_hash_table (info)->loaded;
7711 loaded = loaded->next)
7714 Elf_Internal_Shdr *hdr;
7715 bfd_size_type symcount;
7716 bfd_size_type extsymcount;
7717 bfd_size_type extsymoff;
7718 Elf_Internal_Shdr *versymhdr;
7719 Elf_Internal_Sym *isym;
7720 Elf_Internal_Sym *isymend;
7721 Elf_Internal_Sym *isymbuf;
7722 Elf_External_Versym *ever;
7723 Elf_External_Versym *extversym;
7725 input = loaded->abfd;
7727 /* We check each DSO for a possible hidden versioned definition. */
7729 || (input->flags & DYNAMIC) == 0
7730 || elf_dynversym (input) == 0)
7733 hdr = &elf_tdata (input)->dynsymtab_hdr;
7735 symcount = hdr->sh_size / bed->s->sizeof_sym;
7736 if (elf_bad_symtab (input))
7738 extsymcount = symcount;
7743 extsymcount = symcount - hdr->sh_info;
7744 extsymoff = hdr->sh_info;
7747 if (extsymcount == 0)
7750 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
7752 if (isymbuf == NULL)
7755 /* Read in any version definitions. */
7756 versymhdr = &elf_tdata (input)->dynversym_hdr;
7757 extversym = bfd_malloc (versymhdr->sh_size);
7758 if (extversym == NULL)
7761 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
7762 || (bfd_bread (extversym, versymhdr->sh_size, input)
7763 != versymhdr->sh_size))
7771 ever = extversym + extsymoff;
7772 isymend = isymbuf + extsymcount;
7773 for (isym = isymbuf; isym < isymend; isym++, ever++)
7776 Elf_Internal_Versym iver;
7777 unsigned short version_index;
7779 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
7780 || isym->st_shndx == SHN_UNDEF)
7783 name = bfd_elf_string_from_elf_section (input,
7786 if (strcmp (name, h->root.root.string) != 0)
7789 _bfd_elf_swap_versym_in (input, ever, &iver);
7791 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
7793 /* If we have a non-hidden versioned sym, then it should
7794 have provided a definition for the undefined sym. */
7798 version_index = iver.vs_vers & VERSYM_VERSION;
7799 if (version_index == 1 || version_index == 2)
7801 /* This is the base or first version. We can use it. */
7815 /* Add an external symbol to the symbol table. This is called from
7816 the hash table traversal routine. When generating a shared object,
7817 we go through the symbol table twice. The first time we output
7818 anything that might have been forced to local scope in a version
7819 script. The second time we output the symbols that are still
7823 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
7825 struct elf_outext_info *eoinfo = data;
7826 struct elf_final_link_info *finfo = eoinfo->finfo;
7828 Elf_Internal_Sym sym;
7829 asection *input_sec;
7830 const struct elf_backend_data *bed;
7832 if (h->root.type == bfd_link_hash_warning)
7834 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7835 if (h->root.type == bfd_link_hash_new)
7839 /* Decide whether to output this symbol in this pass. */
7840 if (eoinfo->localsyms)
7842 if (!h->forced_local)
7847 if (h->forced_local)
7851 bed = get_elf_backend_data (finfo->output_bfd);
7853 if (h->root.type == bfd_link_hash_undefined)
7855 /* If we have an undefined symbol reference here then it must have
7856 come from a shared library that is being linked in. (Undefined
7857 references in regular files have already been handled). */
7858 bfd_boolean ignore_undef = FALSE;
7860 /* Some symbols may be special in that the fact that they're
7861 undefined can be safely ignored - let backend determine that. */
7862 if (bed->elf_backend_ignore_undef_symbol)
7863 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
7865 /* If we are reporting errors for this situation then do so now. */
7866 if (ignore_undef == FALSE
7869 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
7870 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
7872 if (! (finfo->info->callbacks->undefined_symbol
7873 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
7874 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
7876 eoinfo->failed = TRUE;
7882 /* We should also warn if a forced local symbol is referenced from
7883 shared libraries. */
7884 if (! finfo->info->relocatable
7885 && (! finfo->info->shared)
7890 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
7892 (*_bfd_error_handler)
7893 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
7895 h->root.u.def.section == bfd_abs_section_ptr
7896 ? finfo->output_bfd : h->root.u.def.section->owner,
7897 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
7899 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
7900 ? "hidden" : "local",
7901 h->root.root.string);
7902 eoinfo->failed = TRUE;
7906 /* We don't want to output symbols that have never been mentioned by
7907 a regular file, or that we have been told to strip. However, if
7908 h->indx is set to -2, the symbol is used by a reloc and we must
7912 else if ((h->def_dynamic
7914 || h->root.type == bfd_link_hash_new)
7918 else if (finfo->info->strip == strip_all)
7920 else if (finfo->info->strip == strip_some
7921 && bfd_hash_lookup (finfo->info->keep_hash,
7922 h->root.root.string, FALSE, FALSE) == NULL)
7924 else if (finfo->info->strip_discarded
7925 && (h->root.type == bfd_link_hash_defined
7926 || h->root.type == bfd_link_hash_defweak)
7927 && elf_discarded_section (h->root.u.def.section))
7932 /* If we're stripping it, and it's not a dynamic symbol, there's
7933 nothing else to do unless it is a forced local symbol. */
7936 && !h->forced_local)
7940 sym.st_size = h->size;
7941 sym.st_other = h->other;
7942 if (h->forced_local)
7943 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
7944 else if (h->root.type == bfd_link_hash_undefweak
7945 || h->root.type == bfd_link_hash_defweak)
7946 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
7948 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
7950 switch (h->root.type)
7953 case bfd_link_hash_new:
7954 case bfd_link_hash_warning:
7958 case bfd_link_hash_undefined:
7959 case bfd_link_hash_undefweak:
7960 input_sec = bfd_und_section_ptr;
7961 sym.st_shndx = SHN_UNDEF;
7964 case bfd_link_hash_defined:
7965 case bfd_link_hash_defweak:
7967 input_sec = h->root.u.def.section;
7968 if (input_sec->output_section != NULL)
7971 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
7972 input_sec->output_section);
7973 if (sym.st_shndx == SHN_BAD)
7975 (*_bfd_error_handler)
7976 (_("%B: could not find output section %A for input section %A"),
7977 finfo->output_bfd, input_sec->output_section, input_sec);
7978 eoinfo->failed = TRUE;
7982 /* ELF symbols in relocatable files are section relative,
7983 but in nonrelocatable files they are virtual
7985 sym.st_value = h->root.u.def.value + input_sec->output_offset;
7986 if (! finfo->info->relocatable)
7988 sym.st_value += input_sec->output_section->vma;
7989 if (h->type == STT_TLS)
7991 /* STT_TLS symbols are relative to PT_TLS segment
7993 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
7994 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
8000 BFD_ASSERT (input_sec->owner == NULL
8001 || (input_sec->owner->flags & DYNAMIC) != 0);
8002 sym.st_shndx = SHN_UNDEF;
8003 input_sec = bfd_und_section_ptr;
8008 case bfd_link_hash_common:
8009 input_sec = h->root.u.c.p->section;
8010 sym.st_shndx = bed->common_section_index (input_sec);
8011 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8014 case bfd_link_hash_indirect:
8015 /* These symbols are created by symbol versioning. They point
8016 to the decorated version of the name. For example, if the
8017 symbol foo@@GNU_1.2 is the default, which should be used when
8018 foo is used with no version, then we add an indirect symbol
8019 foo which points to foo@@GNU_1.2. We ignore these symbols,
8020 since the indirected symbol is already in the hash table. */
8024 /* Give the processor backend a chance to tweak the symbol value,
8025 and also to finish up anything that needs to be done for this
8026 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8027 forced local syms when non-shared is due to a historical quirk. */
8028 if ((h->dynindx != -1
8030 && ((finfo->info->shared
8031 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8032 || h->root.type != bfd_link_hash_undefweak))
8033 || !h->forced_local)
8034 && elf_hash_table (finfo->info)->dynamic_sections_created)
8036 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8037 (finfo->output_bfd, finfo->info, h, &sym)))
8039 eoinfo->failed = TRUE;
8044 /* If we are marking the symbol as undefined, and there are no
8045 non-weak references to this symbol from a regular object, then
8046 mark the symbol as weak undefined; if there are non-weak
8047 references, mark the symbol as strong. We can't do this earlier,
8048 because it might not be marked as undefined until the
8049 finish_dynamic_symbol routine gets through with it. */
8050 if (sym.st_shndx == SHN_UNDEF
8052 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8053 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8057 if (h->ref_regular_nonweak)
8058 bindtype = STB_GLOBAL;
8060 bindtype = STB_WEAK;
8061 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
8064 /* If a non-weak symbol with non-default visibility is not defined
8065 locally, it is a fatal error. */
8066 if (! finfo->info->relocatable
8067 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8068 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8069 && h->root.type == bfd_link_hash_undefined
8072 (*_bfd_error_handler)
8073 (_("%B: %s symbol `%s' isn't defined"),
8075 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
8077 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
8078 ? "internal" : "hidden",
8079 h->root.root.string);
8080 eoinfo->failed = TRUE;
8084 /* If this symbol should be put in the .dynsym section, then put it
8085 there now. We already know the symbol index. We also fill in
8086 the entry in the .hash section. */
8087 if (h->dynindx != -1
8088 && elf_hash_table (finfo->info)->dynamic_sections_created)
8092 sym.st_name = h->dynstr_index;
8093 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
8094 if (! check_dynsym (finfo->output_bfd, &sym))
8096 eoinfo->failed = TRUE;
8099 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8101 if (finfo->hash_sec != NULL)
8103 size_t hash_entry_size;
8104 bfd_byte *bucketpos;
8109 bucketcount = elf_hash_table (finfo->info)->bucketcount;
8110 bucket = h->u.elf_hash_value % bucketcount;
8113 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8114 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8115 + (bucket + 2) * hash_entry_size);
8116 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8117 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8118 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8119 ((bfd_byte *) finfo->hash_sec->contents
8120 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8123 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8125 Elf_Internal_Versym iversym;
8126 Elf_External_Versym *eversym;
8128 if (!h->def_regular)
8130 if (h->verinfo.verdef == NULL)
8131 iversym.vs_vers = 0;
8133 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8137 if (h->verinfo.vertree == NULL)
8138 iversym.vs_vers = 1;
8140 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8141 if (finfo->info->create_default_symver)
8146 iversym.vs_vers |= VERSYM_HIDDEN;
8148 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8149 eversym += h->dynindx;
8150 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8154 /* If we're stripping it, then it was just a dynamic symbol, and
8155 there's nothing else to do. */
8156 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8159 h->indx = bfd_get_symcount (finfo->output_bfd);
8161 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
8163 eoinfo->failed = TRUE;
8170 /* Return TRUE if special handling is done for relocs in SEC against
8171 symbols defined in discarded sections. */
8174 elf_section_ignore_discarded_relocs (asection *sec)
8176 const struct elf_backend_data *bed;
8178 switch (sec->sec_info_type)
8180 case ELF_INFO_TYPE_STABS:
8181 case ELF_INFO_TYPE_EH_FRAME:
8187 bed = get_elf_backend_data (sec->owner);
8188 if (bed->elf_backend_ignore_discarded_relocs != NULL
8189 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8195 /* Return a mask saying how ld should treat relocations in SEC against
8196 symbols defined in discarded sections. If this function returns
8197 COMPLAIN set, ld will issue a warning message. If this function
8198 returns PRETEND set, and the discarded section was link-once and the
8199 same size as the kept link-once section, ld will pretend that the
8200 symbol was actually defined in the kept section. Otherwise ld will
8201 zero the reloc (at least that is the intent, but some cooperation by
8202 the target dependent code is needed, particularly for REL targets). */
8205 _bfd_elf_default_action_discarded (asection *sec)
8207 if (sec->flags & SEC_DEBUGGING)
8210 if (strcmp (".eh_frame", sec->name) == 0)
8213 if (strcmp (".gcc_except_table", sec->name) == 0)
8216 return COMPLAIN | PRETEND;
8219 /* Find a match between a section and a member of a section group. */
8222 match_group_member (asection *sec, asection *group,
8223 struct bfd_link_info *info)
8225 asection *first = elf_next_in_group (group);
8226 asection *s = first;
8230 if (bfd_elf_match_symbols_in_sections (s, sec, info))
8233 s = elf_next_in_group (s);
8241 /* Check if the kept section of a discarded section SEC can be used
8242 to replace it. Return the replacement if it is OK. Otherwise return
8246 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
8250 kept = sec->kept_section;
8253 if ((kept->flags & SEC_GROUP) != 0)
8254 kept = match_group_member (sec, kept, info);
8255 if (kept != NULL && sec->size != kept->size)
8257 sec->kept_section = kept;
8262 /* Link an input file into the linker output file. This function
8263 handles all the sections and relocations of the input file at once.
8264 This is so that we only have to read the local symbols once, and
8265 don't have to keep them in memory. */
8268 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
8270 int (*relocate_section)
8271 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
8272 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
8274 Elf_Internal_Shdr *symtab_hdr;
8277 Elf_Internal_Sym *isymbuf;
8278 Elf_Internal_Sym *isym;
8279 Elf_Internal_Sym *isymend;
8281 asection **ppsection;
8283 const struct elf_backend_data *bed;
8284 struct elf_link_hash_entry **sym_hashes;
8286 output_bfd = finfo->output_bfd;
8287 bed = get_elf_backend_data (output_bfd);
8288 relocate_section = bed->elf_backend_relocate_section;
8290 /* If this is a dynamic object, we don't want to do anything here:
8291 we don't want the local symbols, and we don't want the section
8293 if ((input_bfd->flags & DYNAMIC) != 0)
8296 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8297 if (elf_bad_symtab (input_bfd))
8299 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8304 locsymcount = symtab_hdr->sh_info;
8305 extsymoff = symtab_hdr->sh_info;
8308 /* Read the local symbols. */
8309 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8310 if (isymbuf == NULL && locsymcount != 0)
8312 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8313 finfo->internal_syms,
8314 finfo->external_syms,
8315 finfo->locsym_shndx);
8316 if (isymbuf == NULL)
8319 /* evaluate_complex_relocation_symbols looks for symbols in
8320 finfo->internal_syms. */
8321 else if (isymbuf != NULL && locsymcount != 0)
8323 bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8324 finfo->internal_syms,
8325 finfo->external_syms,
8326 finfo->locsym_shndx);
8329 /* Find local symbol sections and adjust values of symbols in
8330 SEC_MERGE sections. Write out those local symbols we know are
8331 going into the output file. */
8332 isymend = isymbuf + locsymcount;
8333 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
8335 isym++, pindex++, ppsection++)
8339 Elf_Internal_Sym osym;
8343 if (elf_bad_symtab (input_bfd))
8345 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
8352 if (isym->st_shndx == SHN_UNDEF)
8353 isec = bfd_und_section_ptr;
8354 else if (isym->st_shndx < SHN_LORESERVE
8355 || isym->st_shndx > SHN_HIRESERVE)
8357 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
8359 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
8360 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
8362 _bfd_merged_section_offset (output_bfd, &isec,
8363 elf_section_data (isec)->sec_info,
8366 else if (isym->st_shndx == SHN_ABS)
8367 isec = bfd_abs_section_ptr;
8368 else if (isym->st_shndx == SHN_COMMON)
8369 isec = bfd_com_section_ptr;
8372 /* Don't attempt to output symbols with st_shnx in the
8373 reserved range other than SHN_ABS and SHN_COMMON. */
8380 /* Don't output the first, undefined, symbol. */
8381 if (ppsection == finfo->sections)
8384 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
8386 /* We never output section symbols. Instead, we use the
8387 section symbol of the corresponding section in the output
8392 /* If we are stripping all symbols, we don't want to output this
8394 if (finfo->info->strip == strip_all)
8397 /* If we are discarding all local symbols, we don't want to
8398 output this one. If we are generating a relocatable output
8399 file, then some of the local symbols may be required by
8400 relocs; we output them below as we discover that they are
8402 if (finfo->info->discard == discard_all)
8405 /* If this symbol is defined in a section which we are
8406 discarding, we don't need to keep it. */
8407 if (isym->st_shndx != SHN_UNDEF
8408 && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
8410 || bfd_section_removed_from_list (output_bfd,
8411 isec->output_section)))
8414 /* Get the name of the symbol. */
8415 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
8420 /* See if we are discarding symbols with this name. */
8421 if ((finfo->info->strip == strip_some
8422 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
8424 || (((finfo->info->discard == discard_sec_merge
8425 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
8426 || finfo->info->discard == discard_l)
8427 && bfd_is_local_label_name (input_bfd, name)))
8430 /* If we get here, we are going to output this symbol. */
8434 /* Adjust the section index for the output file. */
8435 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
8436 isec->output_section);
8437 if (osym.st_shndx == SHN_BAD)
8440 *pindex = bfd_get_symcount (output_bfd);
8442 /* ELF symbols in relocatable files are section relative, but
8443 in executable files they are virtual addresses. Note that
8444 this code assumes that all ELF sections have an associated
8445 BFD section with a reasonable value for output_offset; below
8446 we assume that they also have a reasonable value for
8447 output_section. Any special sections must be set up to meet
8448 these requirements. */
8449 osym.st_value += isec->output_offset;
8450 if (! finfo->info->relocatable)
8452 osym.st_value += isec->output_section->vma;
8453 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
8455 /* STT_TLS symbols are relative to PT_TLS segment base. */
8456 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
8457 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
8461 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
8465 if (! evaluate_complex_relocation_symbols (input_bfd, finfo, locsymcount))
8468 /* Relocate the contents of each section. */
8469 sym_hashes = elf_sym_hashes (input_bfd);
8470 for (o = input_bfd->sections; o != NULL; o = o->next)
8474 if (! o->linker_mark)
8476 /* This section was omitted from the link. */
8480 if ((o->flags & SEC_HAS_CONTENTS) == 0
8481 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
8484 if ((o->flags & SEC_LINKER_CREATED) != 0)
8486 /* Section was created by _bfd_elf_link_create_dynamic_sections
8491 /* Get the contents of the section. They have been cached by a
8492 relaxation routine. Note that o is a section in an input
8493 file, so the contents field will not have been set by any of
8494 the routines which work on output files. */
8495 if (elf_section_data (o)->this_hdr.contents != NULL)
8496 contents = elf_section_data (o)->this_hdr.contents;
8499 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
8501 contents = finfo->contents;
8502 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
8506 if ((o->flags & SEC_RELOC) != 0)
8508 Elf_Internal_Rela *internal_relocs;
8509 bfd_vma r_type_mask;
8513 /* Get the swapped relocs. */
8515 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
8516 finfo->internal_relocs, FALSE);
8517 if (internal_relocs == NULL
8518 && o->reloc_count > 0)
8521 if (bed->s->arch_size == 32)
8528 r_type_mask = 0xffffffff;
8532 /* Run through the relocs looking for any against symbols
8533 from discarded sections and section symbols from
8534 removed link-once sections. Complain about relocs
8535 against discarded sections. Zero relocs against removed
8536 link-once sections. */
8537 if (!elf_section_ignore_discarded_relocs (o))
8539 Elf_Internal_Rela *rel, *relend;
8540 unsigned int action = (*bed->action_discarded) (o);
8542 rel = internal_relocs;
8543 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
8544 for ( ; rel < relend; rel++)
8546 unsigned long r_symndx = rel->r_info >> r_sym_shift;
8547 asection **ps, *sec;
8548 struct elf_link_hash_entry *h = NULL;
8549 const char *sym_name;
8551 if (r_symndx == STN_UNDEF)
8554 if (r_symndx >= locsymcount
8555 || (elf_bad_symtab (input_bfd)
8556 && finfo->sections[r_symndx] == NULL))
8558 h = sym_hashes[r_symndx - extsymoff];
8560 /* Badly formatted input files can contain relocs that
8561 reference non-existant symbols. Check here so that
8562 we do not seg fault. */
8567 sprintf_vma (buffer, rel->r_info);
8568 (*_bfd_error_handler)
8569 (_("error: %B contains a reloc (0x%s) for section %A "
8570 "that references a non-existent global symbol"),
8571 input_bfd, o, buffer);
8572 bfd_set_error (bfd_error_bad_value);
8576 while (h->root.type == bfd_link_hash_indirect
8577 || h->root.type == bfd_link_hash_warning)
8578 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8580 if (h->root.type != bfd_link_hash_defined
8581 && h->root.type != bfd_link_hash_defweak)
8584 ps = &h->root.u.def.section;
8585 sym_name = h->root.root.string;
8589 Elf_Internal_Sym *sym = isymbuf + r_symndx;
8590 ps = &finfo->sections[r_symndx];
8591 sym_name = bfd_elf_sym_name (input_bfd,
8596 /* Complain if the definition comes from a
8597 discarded section. */
8598 if ((sec = *ps) != NULL && elf_discarded_section (sec))
8600 BFD_ASSERT (r_symndx != 0);
8601 if (action & COMPLAIN)
8602 (*finfo->info->callbacks->einfo)
8603 (_("%X`%s' referenced in section `%A' of %B: "
8604 "defined in discarded section `%A' of %B\n"),
8605 sym_name, o, input_bfd, sec, sec->owner);
8607 /* Try to do the best we can to support buggy old
8608 versions of gcc. Pretend that the symbol is
8609 really defined in the kept linkonce section.
8610 FIXME: This is quite broken. Modifying the
8611 symbol here means we will be changing all later
8612 uses of the symbol, not just in this section. */
8613 if (action & PRETEND)
8617 kept = _bfd_elf_check_kept_section (sec,
8629 /* Relocate the section by invoking a back end routine.
8631 The back end routine is responsible for adjusting the
8632 section contents as necessary, and (if using Rela relocs
8633 and generating a relocatable output file) adjusting the
8634 reloc addend as necessary.
8636 The back end routine does not have to worry about setting
8637 the reloc address or the reloc symbol index.
8639 The back end routine is given a pointer to the swapped in
8640 internal symbols, and can access the hash table entries
8641 for the external symbols via elf_sym_hashes (input_bfd).
8643 When generating relocatable output, the back end routine
8644 must handle STB_LOCAL/STT_SECTION symbols specially. The
8645 output symbol is going to be a section symbol
8646 corresponding to the output section, which will require
8647 the addend to be adjusted. */
8649 ret = (*relocate_section) (output_bfd, finfo->info,
8650 input_bfd, o, contents,
8658 || finfo->info->relocatable
8659 || finfo->info->emitrelocations)
8661 Elf_Internal_Rela *irela;
8662 Elf_Internal_Rela *irelaend;
8663 bfd_vma last_offset;
8664 struct elf_link_hash_entry **rel_hash;
8665 struct elf_link_hash_entry **rel_hash_list;
8666 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
8667 unsigned int next_erel;
8668 bfd_boolean rela_normal;
8670 input_rel_hdr = &elf_section_data (o)->rel_hdr;
8671 rela_normal = (bed->rela_normal
8672 && (input_rel_hdr->sh_entsize
8673 == bed->s->sizeof_rela));
8675 /* Adjust the reloc addresses and symbol indices. */
8677 irela = internal_relocs;
8678 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
8679 rel_hash = (elf_section_data (o->output_section)->rel_hashes
8680 + elf_section_data (o->output_section)->rel_count
8681 + elf_section_data (o->output_section)->rel_count2);
8682 rel_hash_list = rel_hash;
8683 last_offset = o->output_offset;
8684 if (!finfo->info->relocatable)
8685 last_offset += o->output_section->vma;
8686 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
8688 unsigned long r_symndx;
8690 Elf_Internal_Sym sym;
8692 if (next_erel == bed->s->int_rels_per_ext_rel)
8698 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8701 if (irela->r_offset >= (bfd_vma) -2)
8703 /* This is a reloc for a deleted entry or somesuch.
8704 Turn it into an R_*_NONE reloc, at the same
8705 offset as the last reloc. elf_eh_frame.c and
8706 bfd_elf_discard_info rely on reloc offsets
8708 irela->r_offset = last_offset;
8710 irela->r_addend = 0;
8714 irela->r_offset += o->output_offset;
8716 /* Relocs in an executable have to be virtual addresses. */
8717 if (!finfo->info->relocatable)
8718 irela->r_offset += o->output_section->vma;
8720 last_offset = irela->r_offset;
8722 r_symndx = irela->r_info >> r_sym_shift;
8723 if (r_symndx == STN_UNDEF)
8726 if (r_symndx >= locsymcount
8727 || (elf_bad_symtab (input_bfd)
8728 && finfo->sections[r_symndx] == NULL))
8730 struct elf_link_hash_entry *rh;
8733 /* This is a reloc against a global symbol. We
8734 have not yet output all the local symbols, so
8735 we do not know the symbol index of any global
8736 symbol. We set the rel_hash entry for this
8737 reloc to point to the global hash table entry
8738 for this symbol. The symbol index is then
8739 set at the end of bfd_elf_final_link. */
8740 indx = r_symndx - extsymoff;
8741 rh = elf_sym_hashes (input_bfd)[indx];
8742 while (rh->root.type == bfd_link_hash_indirect
8743 || rh->root.type == bfd_link_hash_warning)
8744 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
8746 /* Setting the index to -2 tells
8747 elf_link_output_extsym that this symbol is
8749 BFD_ASSERT (rh->indx < 0);
8757 /* This is a reloc against a local symbol. */
8760 sym = isymbuf[r_symndx];
8761 sec = finfo->sections[r_symndx];
8762 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
8764 /* I suppose the backend ought to fill in the
8765 section of any STT_SECTION symbol against a
8766 processor specific section. */
8768 if (bfd_is_abs_section (sec))
8770 else if (sec == NULL || sec->owner == NULL)
8772 bfd_set_error (bfd_error_bad_value);
8777 asection *osec = sec->output_section;
8779 /* If we have discarded a section, the output
8780 section will be the absolute section. In
8781 case of discarded SEC_MERGE sections, use
8782 the kept section. relocate_section should
8783 have already handled discarded linkonce
8785 if (bfd_is_abs_section (osec)
8786 && sec->kept_section != NULL
8787 && sec->kept_section->output_section != NULL)
8789 osec = sec->kept_section->output_section;
8790 irela->r_addend -= osec->vma;
8793 if (!bfd_is_abs_section (osec))
8795 r_symndx = osec->target_index;
8798 struct elf_link_hash_table *htab;
8801 htab = elf_hash_table (finfo->info);
8802 oi = htab->text_index_section;
8803 if ((osec->flags & SEC_READONLY) == 0
8804 && htab->data_index_section != NULL)
8805 oi = htab->data_index_section;
8809 irela->r_addend += osec->vma - oi->vma;
8810 r_symndx = oi->target_index;
8814 BFD_ASSERT (r_symndx != 0);
8818 /* Adjust the addend according to where the
8819 section winds up in the output section. */
8821 irela->r_addend += sec->output_offset;
8825 if (finfo->indices[r_symndx] == -1)
8827 unsigned long shlink;
8831 if (finfo->info->strip == strip_all)
8833 /* You can't do ld -r -s. */
8834 bfd_set_error (bfd_error_invalid_operation);
8838 /* This symbol was skipped earlier, but
8839 since it is needed by a reloc, we
8840 must output it now. */
8841 shlink = symtab_hdr->sh_link;
8842 name = (bfd_elf_string_from_elf_section
8843 (input_bfd, shlink, sym.st_name));
8847 osec = sec->output_section;
8849 _bfd_elf_section_from_bfd_section (output_bfd,
8851 if (sym.st_shndx == SHN_BAD)
8854 sym.st_value += sec->output_offset;
8855 if (! finfo->info->relocatable)
8857 sym.st_value += osec->vma;
8858 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
8860 /* STT_TLS symbols are relative to PT_TLS
8862 BFD_ASSERT (elf_hash_table (finfo->info)
8864 sym.st_value -= (elf_hash_table (finfo->info)
8869 finfo->indices[r_symndx]
8870 = bfd_get_symcount (output_bfd);
8872 if (! elf_link_output_sym (finfo, name, &sym, sec,
8877 r_symndx = finfo->indices[r_symndx];
8880 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
8881 | (irela->r_info & r_type_mask));
8884 /* Swap out the relocs. */
8885 if (input_rel_hdr->sh_size != 0
8886 && !bed->elf_backend_emit_relocs (output_bfd, o,
8892 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
8893 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
8895 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
8896 * bed->s->int_rels_per_ext_rel);
8897 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
8898 if (!bed->elf_backend_emit_relocs (output_bfd, o,
8907 /* Write out the modified section contents. */
8908 if (bed->elf_backend_write_section
8909 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
8912 /* Section written out. */
8914 else switch (o->sec_info_type)
8916 case ELF_INFO_TYPE_STABS:
8917 if (! (_bfd_write_section_stabs
8919 &elf_hash_table (finfo->info)->stab_info,
8920 o, &elf_section_data (o)->sec_info, contents)))
8923 case ELF_INFO_TYPE_MERGE:
8924 if (! _bfd_write_merged_section (output_bfd, o,
8925 elf_section_data (o)->sec_info))
8928 case ELF_INFO_TYPE_EH_FRAME:
8930 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
8937 if (! (o->flags & SEC_EXCLUDE)
8938 && ! bfd_set_section_contents (output_bfd, o->output_section,
8940 (file_ptr) o->output_offset,
8951 /* Generate a reloc when linking an ELF file. This is a reloc
8952 requested by the linker, and does not come from any input file. This
8953 is used to build constructor and destructor tables when linking
8957 elf_reloc_link_order (bfd *output_bfd,
8958 struct bfd_link_info *info,
8959 asection *output_section,
8960 struct bfd_link_order *link_order)
8962 reloc_howto_type *howto;
8966 struct elf_link_hash_entry **rel_hash_ptr;
8967 Elf_Internal_Shdr *rel_hdr;
8968 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
8969 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
8973 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
8976 bfd_set_error (bfd_error_bad_value);
8980 addend = link_order->u.reloc.p->addend;
8982 /* Figure out the symbol index. */
8983 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
8984 + elf_section_data (output_section)->rel_count
8985 + elf_section_data (output_section)->rel_count2);
8986 if (link_order->type == bfd_section_reloc_link_order)
8988 indx = link_order->u.reloc.p->u.section->target_index;
8989 BFD_ASSERT (indx != 0);
8990 *rel_hash_ptr = NULL;
8994 struct elf_link_hash_entry *h;
8996 /* Treat a reloc against a defined symbol as though it were
8997 actually against the section. */
8998 h = ((struct elf_link_hash_entry *)
8999 bfd_wrapped_link_hash_lookup (output_bfd, info,
9000 link_order->u.reloc.p->u.name,
9001 FALSE, FALSE, TRUE));
9003 && (h->root.type == bfd_link_hash_defined
9004 || h->root.type == bfd_link_hash_defweak))
9008 section = h->root.u.def.section;
9009 indx = section->output_section->target_index;
9010 *rel_hash_ptr = NULL;
9011 /* It seems that we ought to add the symbol value to the
9012 addend here, but in practice it has already been added
9013 because it was passed to constructor_callback. */
9014 addend += section->output_section->vma + section->output_offset;
9018 /* Setting the index to -2 tells elf_link_output_extsym that
9019 this symbol is used by a reloc. */
9026 if (! ((*info->callbacks->unattached_reloc)
9027 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
9033 /* If this is an inplace reloc, we must write the addend into the
9035 if (howto->partial_inplace && addend != 0)
9038 bfd_reloc_status_type rstat;
9041 const char *sym_name;
9043 size = bfd_get_reloc_size (howto);
9044 buf = bfd_zmalloc (size);
9047 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
9054 case bfd_reloc_outofrange:
9057 case bfd_reloc_overflow:
9058 if (link_order->type == bfd_section_reloc_link_order)
9059 sym_name = bfd_section_name (output_bfd,
9060 link_order->u.reloc.p->u.section);
9062 sym_name = link_order->u.reloc.p->u.name;
9063 if (! ((*info->callbacks->reloc_overflow)
9064 (info, NULL, sym_name, howto->name, addend, NULL,
9065 NULL, (bfd_vma) 0)))
9072 ok = bfd_set_section_contents (output_bfd, output_section, buf,
9073 link_order->offset, size);
9079 /* The address of a reloc is relative to the section in a
9080 relocatable file, and is a virtual address in an executable
9082 offset = link_order->offset;
9083 if (! info->relocatable)
9084 offset += output_section->vma;
9086 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
9088 irel[i].r_offset = offset;
9090 irel[i].r_addend = 0;
9092 if (bed->s->arch_size == 32)
9093 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
9095 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
9097 rel_hdr = &elf_section_data (output_section)->rel_hdr;
9098 erel = rel_hdr->contents;
9099 if (rel_hdr->sh_type == SHT_REL)
9101 erel += (elf_section_data (output_section)->rel_count
9102 * bed->s->sizeof_rel);
9103 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
9107 irel[0].r_addend = addend;
9108 erel += (elf_section_data (output_section)->rel_count
9109 * bed->s->sizeof_rela);
9110 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
9113 ++elf_section_data (output_section)->rel_count;
9119 /* Get the output vma of the section pointed to by the sh_link field. */
9122 elf_get_linked_section_vma (struct bfd_link_order *p)
9124 Elf_Internal_Shdr **elf_shdrp;
9128 s = p->u.indirect.section;
9129 elf_shdrp = elf_elfsections (s->owner);
9130 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
9131 elfsec = elf_shdrp[elfsec]->sh_link;
9133 The Intel C compiler generates SHT_IA_64_UNWIND with
9134 SHF_LINK_ORDER. But it doesn't set the sh_link or
9135 sh_info fields. Hence we could get the situation
9136 where elfsec is 0. */
9139 const struct elf_backend_data *bed
9140 = get_elf_backend_data (s->owner);
9141 if (bed->link_order_error_handler)
9142 bed->link_order_error_handler
9143 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
9148 s = elf_shdrp[elfsec]->bfd_section;
9149 return s->output_section->vma + s->output_offset;
9154 /* Compare two sections based on the locations of the sections they are
9155 linked to. Used by elf_fixup_link_order. */
9158 compare_link_order (const void * a, const void * b)
9163 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
9164 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
9171 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9172 order as their linked sections. Returns false if this could not be done
9173 because an output section includes both ordered and unordered
9174 sections. Ideally we'd do this in the linker proper. */
9177 elf_fixup_link_order (bfd *abfd, asection *o)
9182 struct bfd_link_order *p;
9184 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9186 struct bfd_link_order **sections;
9187 asection *s, *other_sec, *linkorder_sec;
9191 linkorder_sec = NULL;
9194 for (p = o->map_head.link_order; p != NULL; p = p->next)
9196 if (p->type == bfd_indirect_link_order)
9198 s = p->u.indirect.section;
9200 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
9201 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
9202 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
9203 && elfsec < elf_numsections (sub)
9204 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
9218 if (seen_other && seen_linkorder)
9220 if (other_sec && linkorder_sec)
9221 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9223 linkorder_sec->owner, other_sec,
9226 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
9228 bfd_set_error (bfd_error_bad_value);
9233 if (!seen_linkorder)
9236 sections = (struct bfd_link_order **)
9237 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
9240 for (p = o->map_head.link_order; p != NULL; p = p->next)
9242 sections[seen_linkorder++] = p;
9244 /* Sort the input sections in the order of their linked section. */
9245 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
9246 compare_link_order);
9248 /* Change the offsets of the sections. */
9250 for (n = 0; n < seen_linkorder; n++)
9252 s = sections[n]->u.indirect.section;
9253 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
9254 s->output_offset = offset;
9255 sections[n]->offset = offset;
9256 offset += sections[n]->size;
9263 /* Do the final step of an ELF link. */
9266 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
9268 bfd_boolean dynamic;
9269 bfd_boolean emit_relocs;
9271 struct elf_final_link_info finfo;
9272 register asection *o;
9273 register struct bfd_link_order *p;
9275 bfd_size_type max_contents_size;
9276 bfd_size_type max_external_reloc_size;
9277 bfd_size_type max_internal_reloc_count;
9278 bfd_size_type max_sym_count;
9279 bfd_size_type max_sym_shndx_count;
9281 Elf_Internal_Sym elfsym;
9283 Elf_Internal_Shdr *symtab_hdr;
9284 Elf_Internal_Shdr *symtab_shndx_hdr;
9285 Elf_Internal_Shdr *symstrtab_hdr;
9286 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9287 struct elf_outext_info eoinfo;
9289 size_t relativecount = 0;
9290 asection *reldyn = 0;
9292 asection *attr_section = NULL;
9293 bfd_vma attr_size = 0;
9294 const char *std_attrs_section;
9296 if (! is_elf_hash_table (info->hash))
9300 abfd->flags |= DYNAMIC;
9302 dynamic = elf_hash_table (info)->dynamic_sections_created;
9303 dynobj = elf_hash_table (info)->dynobj;
9305 emit_relocs = (info->relocatable
9306 || info->emitrelocations);
9309 finfo.output_bfd = abfd;
9310 finfo.symstrtab = _bfd_elf_stringtab_init ();
9311 if (finfo.symstrtab == NULL)
9316 finfo.dynsym_sec = NULL;
9317 finfo.hash_sec = NULL;
9318 finfo.symver_sec = NULL;
9322 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
9323 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
9324 BFD_ASSERT (finfo.dynsym_sec != NULL);
9325 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
9326 /* Note that it is OK if symver_sec is NULL. */
9329 finfo.contents = NULL;
9330 finfo.external_relocs = NULL;
9331 finfo.internal_relocs = NULL;
9332 finfo.external_syms = NULL;
9333 finfo.locsym_shndx = NULL;
9334 finfo.internal_syms = NULL;
9335 finfo.indices = NULL;
9336 finfo.sections = NULL;
9337 finfo.symbuf = NULL;
9338 finfo.symshndxbuf = NULL;
9339 finfo.symbuf_count = 0;
9340 finfo.shndxbuf_size = 0;
9342 /* The object attributes have been merged. Remove the input
9343 sections from the link, and set the contents of the output
9345 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
9346 for (o = abfd->sections; o != NULL; o = o->next)
9348 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
9349 || strcmp (o->name, ".gnu.attributes") == 0)
9351 for (p = o->map_head.link_order; p != NULL; p = p->next)
9353 asection *input_section;
9355 if (p->type != bfd_indirect_link_order)
9357 input_section = p->u.indirect.section;
9358 /* Hack: reset the SEC_HAS_CONTENTS flag so that
9359 elf_link_input_bfd ignores this section. */
9360 input_section->flags &= ~SEC_HAS_CONTENTS;
9363 attr_size = bfd_elf_obj_attr_size (abfd);
9366 bfd_set_section_size (abfd, o, attr_size);
9368 /* Skip this section later on. */
9369 o->map_head.link_order = NULL;
9372 o->flags |= SEC_EXCLUDE;
9376 /* Count up the number of relocations we will output for each output
9377 section, so that we know the sizes of the reloc sections. We
9378 also figure out some maximum sizes. */
9379 max_contents_size = 0;
9380 max_external_reloc_size = 0;
9381 max_internal_reloc_count = 0;
9383 max_sym_shndx_count = 0;
9385 for (o = abfd->sections; o != NULL; o = o->next)
9387 struct bfd_elf_section_data *esdo = elf_section_data (o);
9390 for (p = o->map_head.link_order; p != NULL; p = p->next)
9392 unsigned int reloc_count = 0;
9393 struct bfd_elf_section_data *esdi = NULL;
9394 unsigned int *rel_count1;
9396 if (p->type == bfd_section_reloc_link_order
9397 || p->type == bfd_symbol_reloc_link_order)
9399 else if (p->type == bfd_indirect_link_order)
9403 sec = p->u.indirect.section;
9404 esdi = elf_section_data (sec);
9406 /* Mark all sections which are to be included in the
9407 link. This will normally be every section. We need
9408 to do this so that we can identify any sections which
9409 the linker has decided to not include. */
9410 sec->linker_mark = TRUE;
9412 if (sec->flags & SEC_MERGE)
9415 if (info->relocatable || info->emitrelocations)
9416 reloc_count = sec->reloc_count;
9417 else if (bed->elf_backend_count_relocs)
9419 Elf_Internal_Rela * relocs;
9421 relocs = _bfd_elf_link_read_relocs (sec->owner, sec,
9428 = (*bed->elf_backend_count_relocs) (sec, relocs);
9430 if (elf_section_data (sec)->relocs != relocs)
9435 if (sec->rawsize > max_contents_size)
9436 max_contents_size = sec->rawsize;
9437 if (sec->size > max_contents_size)
9438 max_contents_size = sec->size;
9440 /* We are interested in just local symbols, not all
9442 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
9443 && (sec->owner->flags & DYNAMIC) == 0)
9447 if (elf_bad_symtab (sec->owner))
9448 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
9449 / bed->s->sizeof_sym);
9451 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
9453 if (sym_count > max_sym_count)
9454 max_sym_count = sym_count;
9456 if (sym_count > max_sym_shndx_count
9457 && elf_symtab_shndx (sec->owner) != 0)
9458 max_sym_shndx_count = sym_count;
9460 if ((sec->flags & SEC_RELOC) != 0)
9464 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
9465 if (ext_size > max_external_reloc_size)
9466 max_external_reloc_size = ext_size;
9467 if (sec->reloc_count > max_internal_reloc_count)
9468 max_internal_reloc_count = sec->reloc_count;
9473 if (reloc_count == 0)
9476 o->reloc_count += reloc_count;
9478 /* MIPS may have a mix of REL and RELA relocs on sections.
9479 To support this curious ABI we keep reloc counts in
9480 elf_section_data too. We must be careful to add the
9481 relocations from the input section to the right output
9482 count. FIXME: Get rid of one count. We have
9483 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
9484 rel_count1 = &esdo->rel_count;
9487 bfd_boolean same_size;
9488 bfd_size_type entsize1;
9490 entsize1 = esdi->rel_hdr.sh_entsize;
9491 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
9492 || entsize1 == bed->s->sizeof_rela);
9493 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
9496 rel_count1 = &esdo->rel_count2;
9498 if (esdi->rel_hdr2 != NULL)
9500 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
9501 unsigned int alt_count;
9502 unsigned int *rel_count2;
9504 BFD_ASSERT (entsize2 != entsize1
9505 && (entsize2 == bed->s->sizeof_rel
9506 || entsize2 == bed->s->sizeof_rela));
9508 rel_count2 = &esdo->rel_count2;
9510 rel_count2 = &esdo->rel_count;
9512 /* The following is probably too simplistic if the
9513 backend counts output relocs unusually. */
9514 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
9515 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
9516 *rel_count2 += alt_count;
9517 reloc_count -= alt_count;
9520 *rel_count1 += reloc_count;
9523 if (o->reloc_count > 0)
9524 o->flags |= SEC_RELOC;
9527 /* Explicitly clear the SEC_RELOC flag. The linker tends to
9528 set it (this is probably a bug) and if it is set
9529 assign_section_numbers will create a reloc section. */
9530 o->flags &=~ SEC_RELOC;
9533 /* If the SEC_ALLOC flag is not set, force the section VMA to
9534 zero. This is done in elf_fake_sections as well, but forcing
9535 the VMA to 0 here will ensure that relocs against these
9536 sections are handled correctly. */
9537 if ((o->flags & SEC_ALLOC) == 0
9538 && ! o->user_set_vma)
9542 if (! info->relocatable && merged)
9543 elf_link_hash_traverse (elf_hash_table (info),
9544 _bfd_elf_link_sec_merge_syms, abfd);
9546 /* Figure out the file positions for everything but the symbol table
9547 and the relocs. We set symcount to force assign_section_numbers
9548 to create a symbol table. */
9549 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
9550 BFD_ASSERT (! abfd->output_has_begun);
9551 if (! _bfd_elf_compute_section_file_positions (abfd, info))
9554 /* Set sizes, and assign file positions for reloc sections. */
9555 for (o = abfd->sections; o != NULL; o = o->next)
9557 if ((o->flags & SEC_RELOC) != 0)
9559 if (!(_bfd_elf_link_size_reloc_section
9560 (abfd, &elf_section_data (o)->rel_hdr, o)))
9563 if (elf_section_data (o)->rel_hdr2
9564 && !(_bfd_elf_link_size_reloc_section
9565 (abfd, elf_section_data (o)->rel_hdr2, o)))
9569 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
9570 to count upwards while actually outputting the relocations. */
9571 elf_section_data (o)->rel_count = 0;
9572 elf_section_data (o)->rel_count2 = 0;
9575 _bfd_elf_assign_file_positions_for_relocs (abfd);
9577 /* We have now assigned file positions for all the sections except
9578 .symtab and .strtab. We start the .symtab section at the current
9579 file position, and write directly to it. We build the .strtab
9580 section in memory. */
9581 bfd_get_symcount (abfd) = 0;
9582 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9583 /* sh_name is set in prep_headers. */
9584 symtab_hdr->sh_type = SHT_SYMTAB;
9585 /* sh_flags, sh_addr and sh_size all start off zero. */
9586 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
9587 /* sh_link is set in assign_section_numbers. */
9588 /* sh_info is set below. */
9589 /* sh_offset is set just below. */
9590 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
9592 off = elf_tdata (abfd)->next_file_pos;
9593 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
9595 /* Note that at this point elf_tdata (abfd)->next_file_pos is
9596 incorrect. We do not yet know the size of the .symtab section.
9597 We correct next_file_pos below, after we do know the size. */
9599 /* Allocate a buffer to hold swapped out symbols. This is to avoid
9600 continuously seeking to the right position in the file. */
9601 if (! info->keep_memory || max_sym_count < 20)
9602 finfo.symbuf_size = 20;
9604 finfo.symbuf_size = max_sym_count;
9605 amt = finfo.symbuf_size;
9606 amt *= bed->s->sizeof_sym;
9607 finfo.symbuf = bfd_malloc (amt);
9608 if (finfo.symbuf == NULL)
9610 if (elf_numsections (abfd) > SHN_LORESERVE)
9612 /* Wild guess at number of output symbols. realloc'd as needed. */
9613 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
9614 finfo.shndxbuf_size = amt;
9615 amt *= sizeof (Elf_External_Sym_Shndx);
9616 finfo.symshndxbuf = bfd_zmalloc (amt);
9617 if (finfo.symshndxbuf == NULL)
9621 /* Start writing out the symbol table. The first symbol is always a
9623 if (info->strip != strip_all
9626 elfsym.st_value = 0;
9629 elfsym.st_other = 0;
9630 elfsym.st_shndx = SHN_UNDEF;
9631 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
9636 /* Output a symbol for each section. We output these even if we are
9637 discarding local symbols, since they are used for relocs. These
9638 symbols have no names. We store the index of each one in the
9639 index field of the section, so that we can find it again when
9640 outputting relocs. */
9641 if (info->strip != strip_all
9645 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9646 elfsym.st_other = 0;
9647 elfsym.st_value = 0;
9648 for (i = 1; i < elf_numsections (abfd); i++)
9650 o = bfd_section_from_elf_index (abfd, i);
9653 o->target_index = bfd_get_symcount (abfd);
9654 elfsym.st_shndx = i;
9655 if (!info->relocatable)
9656 elfsym.st_value = o->vma;
9657 if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
9660 if (i == SHN_LORESERVE - 1)
9661 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
9665 /* Allocate some memory to hold information read in from the input
9667 if (max_contents_size != 0)
9669 finfo.contents = bfd_malloc (max_contents_size);
9670 if (finfo.contents == NULL)
9674 if (max_external_reloc_size != 0)
9676 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
9677 if (finfo.external_relocs == NULL)
9681 if (max_internal_reloc_count != 0)
9683 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
9684 amt *= sizeof (Elf_Internal_Rela);
9685 finfo.internal_relocs = bfd_malloc (amt);
9686 if (finfo.internal_relocs == NULL)
9690 if (max_sym_count != 0)
9692 amt = max_sym_count * bed->s->sizeof_sym;
9693 finfo.external_syms = bfd_malloc (amt);
9694 if (finfo.external_syms == NULL)
9697 amt = max_sym_count * sizeof (Elf_Internal_Sym);
9698 finfo.internal_syms = bfd_malloc (amt);
9699 if (finfo.internal_syms == NULL)
9702 amt = max_sym_count * sizeof (long);
9703 finfo.indices = bfd_malloc (amt);
9704 if (finfo.indices == NULL)
9707 amt = max_sym_count * sizeof (asection *);
9708 finfo.sections = bfd_malloc (amt);
9709 if (finfo.sections == NULL)
9713 if (max_sym_shndx_count != 0)
9715 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
9716 finfo.locsym_shndx = bfd_malloc (amt);
9717 if (finfo.locsym_shndx == NULL)
9721 if (elf_hash_table (info)->tls_sec)
9723 bfd_vma base, end = 0;
9726 for (sec = elf_hash_table (info)->tls_sec;
9727 sec && (sec->flags & SEC_THREAD_LOCAL);
9730 bfd_size_type size = sec->size;
9733 && (sec->flags & SEC_HAS_CONTENTS) == 0)
9735 struct bfd_link_order *o = sec->map_tail.link_order;
9737 size = o->offset + o->size;
9739 end = sec->vma + size;
9741 base = elf_hash_table (info)->tls_sec->vma;
9742 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
9743 elf_hash_table (info)->tls_size = end - base;
9746 /* Reorder SHF_LINK_ORDER sections. */
9747 for (o = abfd->sections; o != NULL; o = o->next)
9749 if (!elf_fixup_link_order (abfd, o))
9753 /* Since ELF permits relocations to be against local symbols, we
9754 must have the local symbols available when we do the relocations.
9755 Since we would rather only read the local symbols once, and we
9756 would rather not keep them in memory, we handle all the
9757 relocations for a single input file at the same time.
9759 Unfortunately, there is no way to know the total number of local
9760 symbols until we have seen all of them, and the local symbol
9761 indices precede the global symbol indices. This means that when
9762 we are generating relocatable output, and we see a reloc against
9763 a global symbol, we can not know the symbol index until we have
9764 finished examining all the local symbols to see which ones we are
9765 going to output. To deal with this, we keep the relocations in
9766 memory, and don't output them until the end of the link. This is
9767 an unfortunate waste of memory, but I don't see a good way around
9768 it. Fortunately, it only happens when performing a relocatable
9769 link, which is not the common case. FIXME: If keep_memory is set
9770 we could write the relocs out and then read them again; I don't
9771 know how bad the memory loss will be. */
9773 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9774 sub->output_has_begun = FALSE;
9775 for (o = abfd->sections; o != NULL; o = o->next)
9777 for (p = o->map_head.link_order; p != NULL; p = p->next)
9779 if (p->type == bfd_indirect_link_order
9780 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
9781 == bfd_target_elf_flavour)
9782 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
9784 if (! sub->output_has_begun)
9786 if (! elf_link_input_bfd (&finfo, sub))
9788 sub->output_has_begun = TRUE;
9791 else if (p->type == bfd_section_reloc_link_order
9792 || p->type == bfd_symbol_reloc_link_order)
9794 if (! elf_reloc_link_order (abfd, info, o, p))
9799 if (! _bfd_default_link_order (abfd, info, o, p))
9805 /* Free symbol buffer if needed. */
9806 if (!info->reduce_memory_overheads)
9808 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9809 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
9810 && elf_tdata (sub)->symbuf)
9812 free (elf_tdata (sub)->symbuf);
9813 elf_tdata (sub)->symbuf = NULL;
9817 /* Output any global symbols that got converted to local in a
9818 version script or due to symbol visibility. We do this in a
9819 separate step since ELF requires all local symbols to appear
9820 prior to any global symbols. FIXME: We should only do this if
9821 some global symbols were, in fact, converted to become local.
9822 FIXME: Will this work correctly with the Irix 5 linker? */
9823 eoinfo.failed = FALSE;
9824 eoinfo.finfo = &finfo;
9825 eoinfo.localsyms = TRUE;
9826 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
9831 /* If backend needs to output some local symbols not present in the hash
9832 table, do it now. */
9833 if (bed->elf_backend_output_arch_local_syms)
9835 typedef bfd_boolean (*out_sym_func)
9836 (void *, const char *, Elf_Internal_Sym *, asection *,
9837 struct elf_link_hash_entry *);
9839 if (! ((*bed->elf_backend_output_arch_local_syms)
9840 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
9844 /* That wrote out all the local symbols. Finish up the symbol table
9845 with the global symbols. Even if we want to strip everything we
9846 can, we still need to deal with those global symbols that got
9847 converted to local in a version script. */
9849 /* The sh_info field records the index of the first non local symbol. */
9850 symtab_hdr->sh_info = bfd_get_symcount (abfd);
9853 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
9855 Elf_Internal_Sym sym;
9856 bfd_byte *dynsym = finfo.dynsym_sec->contents;
9857 long last_local = 0;
9859 /* Write out the section symbols for the output sections. */
9860 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
9866 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9869 for (s = abfd->sections; s != NULL; s = s->next)
9875 dynindx = elf_section_data (s)->dynindx;
9878 indx = elf_section_data (s)->this_idx;
9879 BFD_ASSERT (indx > 0);
9880 sym.st_shndx = indx;
9881 if (! check_dynsym (abfd, &sym))
9883 sym.st_value = s->vma;
9884 dest = dynsym + dynindx * bed->s->sizeof_sym;
9885 if (last_local < dynindx)
9886 last_local = dynindx;
9887 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
9891 /* Write out the local dynsyms. */
9892 if (elf_hash_table (info)->dynlocal)
9894 struct elf_link_local_dynamic_entry *e;
9895 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
9900 sym.st_size = e->isym.st_size;
9901 sym.st_other = e->isym.st_other;
9903 /* Copy the internal symbol as is.
9904 Note that we saved a word of storage and overwrote
9905 the original st_name with the dynstr_index. */
9908 if (e->isym.st_shndx != SHN_UNDEF
9909 && (e->isym.st_shndx < SHN_LORESERVE
9910 || e->isym.st_shndx > SHN_HIRESERVE))
9912 s = bfd_section_from_elf_index (e->input_bfd,
9916 elf_section_data (s->output_section)->this_idx;
9917 if (! check_dynsym (abfd, &sym))
9919 sym.st_value = (s->output_section->vma
9921 + e->isym.st_value);
9924 if (last_local < e->dynindx)
9925 last_local = e->dynindx;
9927 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
9928 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
9932 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
9936 /* We get the global symbols from the hash table. */
9937 eoinfo.failed = FALSE;
9938 eoinfo.localsyms = FALSE;
9939 eoinfo.finfo = &finfo;
9940 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
9945 /* If backend needs to output some symbols not present in the hash
9946 table, do it now. */
9947 if (bed->elf_backend_output_arch_syms)
9949 typedef bfd_boolean (*out_sym_func)
9950 (void *, const char *, Elf_Internal_Sym *, asection *,
9951 struct elf_link_hash_entry *);
9953 if (! ((*bed->elf_backend_output_arch_syms)
9954 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
9958 /* Flush all symbols to the file. */
9959 if (! elf_link_flush_output_syms (&finfo, bed))
9962 /* Now we know the size of the symtab section. */
9963 off += symtab_hdr->sh_size;
9965 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
9966 if (symtab_shndx_hdr->sh_name != 0)
9968 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
9969 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
9970 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
9971 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
9972 symtab_shndx_hdr->sh_size = amt;
9974 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
9977 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
9978 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
9983 /* Finish up and write out the symbol string table (.strtab)
9985 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
9986 /* sh_name was set in prep_headers. */
9987 symstrtab_hdr->sh_type = SHT_STRTAB;
9988 symstrtab_hdr->sh_flags = 0;
9989 symstrtab_hdr->sh_addr = 0;
9990 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
9991 symstrtab_hdr->sh_entsize = 0;
9992 symstrtab_hdr->sh_link = 0;
9993 symstrtab_hdr->sh_info = 0;
9994 /* sh_offset is set just below. */
9995 symstrtab_hdr->sh_addralign = 1;
9997 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
9998 elf_tdata (abfd)->next_file_pos = off;
10000 if (bfd_get_symcount (abfd) > 0)
10002 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10003 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10007 /* Adjust the relocs to have the correct symbol indices. */
10008 for (o = abfd->sections; o != NULL; o = o->next)
10010 if ((o->flags & SEC_RELOC) == 0)
10013 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
10014 elf_section_data (o)->rel_count,
10015 elf_section_data (o)->rel_hashes);
10016 if (elf_section_data (o)->rel_hdr2 != NULL)
10017 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
10018 elf_section_data (o)->rel_count2,
10019 (elf_section_data (o)->rel_hashes
10020 + elf_section_data (o)->rel_count));
10022 /* Set the reloc_count field to 0 to prevent write_relocs from
10023 trying to swap the relocs out itself. */
10024 o->reloc_count = 0;
10027 if (dynamic && info->combreloc && dynobj != NULL)
10028 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
10030 /* If we are linking against a dynamic object, or generating a
10031 shared library, finish up the dynamic linking information. */
10034 bfd_byte *dyncon, *dynconend;
10036 /* Fix up .dynamic entries. */
10037 o = bfd_get_section_by_name (dynobj, ".dynamic");
10038 BFD_ASSERT (o != NULL);
10040 dyncon = o->contents;
10041 dynconend = o->contents + o->size;
10042 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10044 Elf_Internal_Dyn dyn;
10048 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10055 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
10057 switch (elf_section_data (reldyn)->this_hdr.sh_type)
10059 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
10060 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
10063 dyn.d_un.d_val = relativecount;
10070 name = info->init_function;
10073 name = info->fini_function;
10076 struct elf_link_hash_entry *h;
10078 h = elf_link_hash_lookup (elf_hash_table (info), name,
10079 FALSE, FALSE, TRUE);
10081 && (h->root.type == bfd_link_hash_defined
10082 || h->root.type == bfd_link_hash_defweak))
10084 dyn.d_un.d_val = h->root.u.def.value;
10085 o = h->root.u.def.section;
10086 if (o->output_section != NULL)
10087 dyn.d_un.d_val += (o->output_section->vma
10088 + o->output_offset);
10091 /* The symbol is imported from another shared
10092 library and does not apply to this one. */
10093 dyn.d_un.d_val = 0;
10100 case DT_PREINIT_ARRAYSZ:
10101 name = ".preinit_array";
10103 case DT_INIT_ARRAYSZ:
10104 name = ".init_array";
10106 case DT_FINI_ARRAYSZ:
10107 name = ".fini_array";
10109 o = bfd_get_section_by_name (abfd, name);
10112 (*_bfd_error_handler)
10113 (_("%B: could not find output section %s"), abfd, name);
10117 (*_bfd_error_handler)
10118 (_("warning: %s section has zero size"), name);
10119 dyn.d_un.d_val = o->size;
10122 case DT_PREINIT_ARRAY:
10123 name = ".preinit_array";
10125 case DT_INIT_ARRAY:
10126 name = ".init_array";
10128 case DT_FINI_ARRAY:
10129 name = ".fini_array";
10136 name = ".gnu.hash";
10145 name = ".gnu.version_d";
10148 name = ".gnu.version_r";
10151 name = ".gnu.version";
10153 o = bfd_get_section_by_name (abfd, name);
10156 (*_bfd_error_handler)
10157 (_("%B: could not find output section %s"), abfd, name);
10160 dyn.d_un.d_ptr = o->vma;
10167 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
10171 dyn.d_un.d_val = 0;
10172 for (i = 1; i < elf_numsections (abfd); i++)
10174 Elf_Internal_Shdr *hdr;
10176 hdr = elf_elfsections (abfd)[i];
10177 if (hdr->sh_type == type
10178 && (hdr->sh_flags & SHF_ALLOC) != 0)
10180 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
10181 dyn.d_un.d_val += hdr->sh_size;
10184 if (dyn.d_un.d_val == 0
10185 || hdr->sh_addr < dyn.d_un.d_val)
10186 dyn.d_un.d_val = hdr->sh_addr;
10192 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
10196 /* If we have created any dynamic sections, then output them. */
10197 if (dynobj != NULL)
10199 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
10202 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10203 if (info->warn_shared_textrel && info->shared)
10205 bfd_byte *dyncon, *dynconend;
10207 /* Fix up .dynamic entries. */
10208 o = bfd_get_section_by_name (dynobj, ".dynamic");
10209 BFD_ASSERT (o != NULL);
10211 dyncon = o->contents;
10212 dynconend = o->contents + o->size;
10213 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10215 Elf_Internal_Dyn dyn;
10217 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10219 if (dyn.d_tag == DT_TEXTREL)
10221 info->callbacks->einfo
10222 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
10228 for (o = dynobj->sections; o != NULL; o = o->next)
10230 if ((o->flags & SEC_HAS_CONTENTS) == 0
10232 || o->output_section == bfd_abs_section_ptr)
10234 if ((o->flags & SEC_LINKER_CREATED) == 0)
10236 /* At this point, we are only interested in sections
10237 created by _bfd_elf_link_create_dynamic_sections. */
10240 if (elf_hash_table (info)->stab_info.stabstr == o)
10242 if (elf_hash_table (info)->eh_info.hdr_sec == o)
10244 if ((elf_section_data (o->output_section)->this_hdr.sh_type
10246 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
10248 if (! bfd_set_section_contents (abfd, o->output_section,
10250 (file_ptr) o->output_offset,
10256 /* The contents of the .dynstr section are actually in a
10258 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
10259 if (bfd_seek (abfd, off, SEEK_SET) != 0
10260 || ! _bfd_elf_strtab_emit (abfd,
10261 elf_hash_table (info)->dynstr))
10267 if (info->relocatable)
10269 bfd_boolean failed = FALSE;
10271 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
10276 /* If we have optimized stabs strings, output them. */
10277 if (elf_hash_table (info)->stab_info.stabstr != NULL)
10279 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
10283 if (info->eh_frame_hdr)
10285 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
10289 if (finfo.symstrtab != NULL)
10290 _bfd_stringtab_free (finfo.symstrtab);
10291 if (finfo.contents != NULL)
10292 free (finfo.contents);
10293 if (finfo.external_relocs != NULL)
10294 free (finfo.external_relocs);
10295 if (finfo.internal_relocs != NULL)
10296 free (finfo.internal_relocs);
10297 if (finfo.external_syms != NULL)
10298 free (finfo.external_syms);
10299 if (finfo.locsym_shndx != NULL)
10300 free (finfo.locsym_shndx);
10301 if (finfo.internal_syms != NULL)
10302 free (finfo.internal_syms);
10303 if (finfo.indices != NULL)
10304 free (finfo.indices);
10305 if (finfo.sections != NULL)
10306 free (finfo.sections);
10307 if (finfo.symbuf != NULL)
10308 free (finfo.symbuf);
10309 if (finfo.symshndxbuf != NULL)
10310 free (finfo.symshndxbuf);
10311 for (o = abfd->sections; o != NULL; o = o->next)
10313 if ((o->flags & SEC_RELOC) != 0
10314 && elf_section_data (o)->rel_hashes != NULL)
10315 free (elf_section_data (o)->rel_hashes);
10318 elf_tdata (abfd)->linker = TRUE;
10322 bfd_byte *contents = bfd_malloc (attr_size);
10323 if (contents == NULL)
10325 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
10326 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
10333 if (finfo.symstrtab != NULL)
10334 _bfd_stringtab_free (finfo.symstrtab);
10335 if (finfo.contents != NULL)
10336 free (finfo.contents);
10337 if (finfo.external_relocs != NULL)
10338 free (finfo.external_relocs);
10339 if (finfo.internal_relocs != NULL)
10340 free (finfo.internal_relocs);
10341 if (finfo.external_syms != NULL)
10342 free (finfo.external_syms);
10343 if (finfo.locsym_shndx != NULL)
10344 free (finfo.locsym_shndx);
10345 if (finfo.internal_syms != NULL)
10346 free (finfo.internal_syms);
10347 if (finfo.indices != NULL)
10348 free (finfo.indices);
10349 if (finfo.sections != NULL)
10350 free (finfo.sections);
10351 if (finfo.symbuf != NULL)
10352 free (finfo.symbuf);
10353 if (finfo.symshndxbuf != NULL)
10354 free (finfo.symshndxbuf);
10355 for (o = abfd->sections; o != NULL; o = o->next)
10357 if ((o->flags & SEC_RELOC) != 0
10358 && elf_section_data (o)->rel_hashes != NULL)
10359 free (elf_section_data (o)->rel_hashes);
10365 /* Garbage collect unused sections. */
10367 /* Default gc_mark_hook. */
10370 _bfd_elf_gc_mark_hook (asection *sec,
10371 struct bfd_link_info *info ATTRIBUTE_UNUSED,
10372 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
10373 struct elf_link_hash_entry *h,
10374 Elf_Internal_Sym *sym)
10378 switch (h->root.type)
10380 case bfd_link_hash_defined:
10381 case bfd_link_hash_defweak:
10382 return h->root.u.def.section;
10384 case bfd_link_hash_common:
10385 return h->root.u.c.p->section;
10392 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
10397 /* The mark phase of garbage collection. For a given section, mark
10398 it and any sections in this section's group, and all the sections
10399 which define symbols to which it refers. */
10402 _bfd_elf_gc_mark (struct bfd_link_info *info,
10404 elf_gc_mark_hook_fn gc_mark_hook)
10408 asection *group_sec;
10412 /* Mark all the sections in the group. */
10413 group_sec = elf_section_data (sec)->next_in_group;
10414 if (group_sec && !group_sec->gc_mark)
10415 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
10418 /* Look through the section relocs. */
10420 is_eh = strcmp (sec->name, ".eh_frame") == 0;
10421 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
10423 Elf_Internal_Rela *relstart, *rel, *relend;
10424 Elf_Internal_Shdr *symtab_hdr;
10425 struct elf_link_hash_entry **sym_hashes;
10428 bfd *input_bfd = sec->owner;
10429 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
10430 Elf_Internal_Sym *isym = NULL;
10433 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10434 sym_hashes = elf_sym_hashes (input_bfd);
10436 /* Read the local symbols. */
10437 if (elf_bad_symtab (input_bfd))
10439 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
10443 extsymoff = nlocsyms = symtab_hdr->sh_info;
10445 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
10446 if (isym == NULL && nlocsyms != 0)
10448 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
10454 /* Read the relocations. */
10455 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
10456 info->keep_memory);
10457 if (relstart == NULL)
10462 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
10464 if (bed->s->arch_size == 32)
10469 for (rel = relstart; rel < relend; rel++)
10471 unsigned long r_symndx;
10473 struct elf_link_hash_entry *h;
10475 r_symndx = rel->r_info >> r_sym_shift;
10479 if (r_symndx >= nlocsyms
10480 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
10482 h = sym_hashes[r_symndx - extsymoff];
10483 while (h->root.type == bfd_link_hash_indirect
10484 || h->root.type == bfd_link_hash_warning)
10485 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10486 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
10490 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
10493 if (rsec && !rsec->gc_mark)
10495 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
10498 rsec->gc_mark_from_eh = 1;
10499 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
10508 if (elf_section_data (sec)->relocs != relstart)
10511 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
10513 if (! info->keep_memory)
10516 symtab_hdr->contents = (unsigned char *) isym;
10523 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
10525 struct elf_gc_sweep_symbol_info
10527 struct bfd_link_info *info;
10528 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
10533 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
10535 if (h->root.type == bfd_link_hash_warning)
10536 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10538 if ((h->root.type == bfd_link_hash_defined
10539 || h->root.type == bfd_link_hash_defweak)
10540 && !h->root.u.def.section->gc_mark
10541 && !(h->root.u.def.section->owner->flags & DYNAMIC))
10543 struct elf_gc_sweep_symbol_info *inf = data;
10544 (*inf->hide_symbol) (inf->info, h, TRUE);
10550 /* The sweep phase of garbage collection. Remove all garbage sections. */
10552 typedef bfd_boolean (*gc_sweep_hook_fn)
10553 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
10556 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
10559 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10560 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
10561 unsigned long section_sym_count;
10562 struct elf_gc_sweep_symbol_info sweep_info;
10564 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10568 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
10571 for (o = sub->sections; o != NULL; o = o->next)
10573 /* Keep debug and special sections. */
10574 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
10575 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
10581 /* Skip sweeping sections already excluded. */
10582 if (o->flags & SEC_EXCLUDE)
10585 /* Since this is early in the link process, it is simple
10586 to remove a section from the output. */
10587 o->flags |= SEC_EXCLUDE;
10589 if (info->print_gc_sections && o->size != 0)
10590 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
10592 /* But we also have to update some of the relocation
10593 info we collected before. */
10595 && (o->flags & SEC_RELOC) != 0
10596 && o->reloc_count > 0
10597 && !bfd_is_abs_section (o->output_section))
10599 Elf_Internal_Rela *internal_relocs;
10603 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
10604 info->keep_memory);
10605 if (internal_relocs == NULL)
10608 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
10610 if (elf_section_data (o)->relocs != internal_relocs)
10611 free (internal_relocs);
10619 /* Remove the symbols that were in the swept sections from the dynamic
10620 symbol table. GCFIXME: Anyone know how to get them out of the
10621 static symbol table as well? */
10622 sweep_info.info = info;
10623 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
10624 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
10627 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
10631 /* Propagate collected vtable information. This is called through
10632 elf_link_hash_traverse. */
10635 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
10637 if (h->root.type == bfd_link_hash_warning)
10638 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10640 /* Those that are not vtables. */
10641 if (h->vtable == NULL || h->vtable->parent == NULL)
10644 /* Those vtables that do not have parents, we cannot merge. */
10645 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
10648 /* If we've already been done, exit. */
10649 if (h->vtable->used && h->vtable->used[-1])
10652 /* Make sure the parent's table is up to date. */
10653 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
10655 if (h->vtable->used == NULL)
10657 /* None of this table's entries were referenced. Re-use the
10659 h->vtable->used = h->vtable->parent->vtable->used;
10660 h->vtable->size = h->vtable->parent->vtable->size;
10665 bfd_boolean *cu, *pu;
10667 /* Or the parent's entries into ours. */
10668 cu = h->vtable->used;
10670 pu = h->vtable->parent->vtable->used;
10673 const struct elf_backend_data *bed;
10674 unsigned int log_file_align;
10676 bed = get_elf_backend_data (h->root.u.def.section->owner);
10677 log_file_align = bed->s->log_file_align;
10678 n = h->vtable->parent->vtable->size >> log_file_align;
10693 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
10696 bfd_vma hstart, hend;
10697 Elf_Internal_Rela *relstart, *relend, *rel;
10698 const struct elf_backend_data *bed;
10699 unsigned int log_file_align;
10701 if (h->root.type == bfd_link_hash_warning)
10702 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10704 /* Take care of both those symbols that do not describe vtables as
10705 well as those that are not loaded. */
10706 if (h->vtable == NULL || h->vtable->parent == NULL)
10709 BFD_ASSERT (h->root.type == bfd_link_hash_defined
10710 || h->root.type == bfd_link_hash_defweak);
10712 sec = h->root.u.def.section;
10713 hstart = h->root.u.def.value;
10714 hend = hstart + h->size;
10716 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
10718 return *(bfd_boolean *) okp = FALSE;
10719 bed = get_elf_backend_data (sec->owner);
10720 log_file_align = bed->s->log_file_align;
10722 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
10724 for (rel = relstart; rel < relend; ++rel)
10725 if (rel->r_offset >= hstart && rel->r_offset < hend)
10727 /* If the entry is in use, do nothing. */
10728 if (h->vtable->used
10729 && (rel->r_offset - hstart) < h->vtable->size)
10731 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
10732 if (h->vtable->used[entry])
10735 /* Otherwise, kill it. */
10736 rel->r_offset = rel->r_info = rel->r_addend = 0;
10742 /* Mark sections containing dynamically referenced symbols. When
10743 building shared libraries, we must assume that any visible symbol is
10747 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
10749 struct bfd_link_info *info = (struct bfd_link_info *) inf;
10751 if (h->root.type == bfd_link_hash_warning)
10752 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10754 if ((h->root.type == bfd_link_hash_defined
10755 || h->root.type == bfd_link_hash_defweak)
10757 || (!info->executable
10759 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
10760 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
10761 h->root.u.def.section->flags |= SEC_KEEP;
10766 /* Do mark and sweep of unused sections. */
10769 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
10771 bfd_boolean ok = TRUE;
10773 elf_gc_mark_hook_fn gc_mark_hook;
10774 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10776 if (!bed->can_gc_sections
10777 || info->relocatable
10778 || info->emitrelocations
10779 || !is_elf_hash_table (info->hash))
10781 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
10785 /* Apply transitive closure to the vtable entry usage info. */
10786 elf_link_hash_traverse (elf_hash_table (info),
10787 elf_gc_propagate_vtable_entries_used,
10792 /* Kill the vtable relocations that were not used. */
10793 elf_link_hash_traverse (elf_hash_table (info),
10794 elf_gc_smash_unused_vtentry_relocs,
10799 /* Mark dynamically referenced symbols. */
10800 if (elf_hash_table (info)->dynamic_sections_created)
10801 elf_link_hash_traverse (elf_hash_table (info),
10802 bed->gc_mark_dynamic_ref,
10805 /* Grovel through relocs to find out who stays ... */
10806 gc_mark_hook = bed->gc_mark_hook;
10807 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10811 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
10814 for (o = sub->sections; o != NULL; o = o->next)
10815 if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark)
10816 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
10820 /* Allow the backend to mark additional target specific sections. */
10821 if (bed->gc_mark_extra_sections)
10822 bed->gc_mark_extra_sections(info, gc_mark_hook);
10824 /* ... again for sections marked from eh_frame. */
10825 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10829 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
10832 /* Keep .gcc_except_table.* if the associated .text.* (or the
10833 associated .gnu.linkonce.t.* if .text.* doesn't exist) is
10834 marked. This isn't very nice, but the proper solution,
10835 splitting .eh_frame up and using comdat doesn't pan out
10836 easily due to needing special relocs to handle the
10837 difference of two symbols in separate sections.
10838 Don't keep code sections referenced by .eh_frame. */
10839 #define TEXT_PREFIX ".text."
10840 #define TEXT_PREFIX2 ".gnu.linkonce.t."
10841 #define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table."
10842 for (o = sub->sections; o != NULL; o = o->next)
10843 if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
10845 if (CONST_STRNEQ (o->name, GCC_EXCEPT_TABLE_PREFIX))
10848 const char *sec_name;
10850 unsigned o_name_prefix_len , fn_name_prefix_len, tmp;
10852 o_name_prefix_len = strlen (GCC_EXCEPT_TABLE_PREFIX);
10853 sec_name = o->name + o_name_prefix_len;
10854 fn_name_prefix_len = strlen (TEXT_PREFIX);
10855 tmp = strlen (TEXT_PREFIX2);
10856 if (tmp > fn_name_prefix_len)
10857 fn_name_prefix_len = tmp;
10859 = bfd_malloc (fn_name_prefix_len + strlen (sec_name) + 1);
10860 if (fn_name == NULL)
10863 /* Try the first prefix. */
10864 sprintf (fn_name, "%s%s", TEXT_PREFIX, sec_name);
10865 fn_text = bfd_get_section_by_name (sub, fn_name);
10867 /* Try the second prefix. */
10868 if (fn_text == NULL)
10870 sprintf (fn_name, "%s%s", TEXT_PREFIX2, sec_name);
10871 fn_text = bfd_get_section_by_name (sub, fn_name);
10875 if (fn_text == NULL || !fn_text->gc_mark)
10879 /* If not using specially named exception table section,
10880 then keep whatever we are using. */
10881 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
10886 /* ... and mark SEC_EXCLUDE for those that go. */
10887 return elf_gc_sweep (abfd, info);
10890 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
10893 bfd_elf_gc_record_vtinherit (bfd *abfd,
10895 struct elf_link_hash_entry *h,
10898 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
10899 struct elf_link_hash_entry **search, *child;
10900 bfd_size_type extsymcount;
10901 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10903 /* The sh_info field of the symtab header tells us where the
10904 external symbols start. We don't care about the local symbols at
10906 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
10907 if (!elf_bad_symtab (abfd))
10908 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
10910 sym_hashes = elf_sym_hashes (abfd);
10911 sym_hashes_end = sym_hashes + extsymcount;
10913 /* Hunt down the child symbol, which is in this section at the same
10914 offset as the relocation. */
10915 for (search = sym_hashes; search != sym_hashes_end; ++search)
10917 if ((child = *search) != NULL
10918 && (child->root.type == bfd_link_hash_defined
10919 || child->root.type == bfd_link_hash_defweak)
10920 && child->root.u.def.section == sec
10921 && child->root.u.def.value == offset)
10925 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
10926 abfd, sec, (unsigned long) offset);
10927 bfd_set_error (bfd_error_invalid_operation);
10931 if (!child->vtable)
10933 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
10934 if (!child->vtable)
10939 /* This *should* only be the absolute section. It could potentially
10940 be that someone has defined a non-global vtable though, which
10941 would be bad. It isn't worth paging in the local symbols to be
10942 sure though; that case should simply be handled by the assembler. */
10944 child->vtable->parent = (struct elf_link_hash_entry *) -1;
10947 child->vtable->parent = h;
10952 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
10955 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
10956 asection *sec ATTRIBUTE_UNUSED,
10957 struct elf_link_hash_entry *h,
10960 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10961 unsigned int log_file_align = bed->s->log_file_align;
10965 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
10970 if (addend >= h->vtable->size)
10972 size_t size, bytes, file_align;
10973 bfd_boolean *ptr = h->vtable->used;
10975 /* While the symbol is undefined, we have to be prepared to handle
10977 file_align = 1 << log_file_align;
10978 if (h->root.type == bfd_link_hash_undefined)
10979 size = addend + file_align;
10983 if (addend >= size)
10985 /* Oops! We've got a reference past the defined end of
10986 the table. This is probably a bug -- shall we warn? */
10987 size = addend + file_align;
10990 size = (size + file_align - 1) & -file_align;
10992 /* Allocate one extra entry for use as a "done" flag for the
10993 consolidation pass. */
10994 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
10998 ptr = bfd_realloc (ptr - 1, bytes);
11004 oldbytes = (((h->vtable->size >> log_file_align) + 1)
11005 * sizeof (bfd_boolean));
11006 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
11010 ptr = bfd_zmalloc (bytes);
11015 /* And arrange for that done flag to be at index -1. */
11016 h->vtable->used = ptr + 1;
11017 h->vtable->size = size;
11020 h->vtable->used[addend >> log_file_align] = TRUE;
11025 struct alloc_got_off_arg {
11027 unsigned int got_elt_size;
11030 /* We need a special top-level link routine to convert got reference counts
11031 to real got offsets. */
11034 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
11036 struct alloc_got_off_arg *gofarg = arg;
11038 if (h->root.type == bfd_link_hash_warning)
11039 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11041 if (h->got.refcount > 0)
11043 h->got.offset = gofarg->gotoff;
11044 gofarg->gotoff += gofarg->got_elt_size;
11047 h->got.offset = (bfd_vma) -1;
11052 /* And an accompanying bit to work out final got entry offsets once
11053 we're done. Should be called from final_link. */
11056 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
11057 struct bfd_link_info *info)
11060 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11062 unsigned int got_elt_size = bed->s->arch_size / 8;
11063 struct alloc_got_off_arg gofarg;
11065 if (! is_elf_hash_table (info->hash))
11068 /* The GOT offset is relative to the .got section, but the GOT header is
11069 put into the .got.plt section, if the backend uses it. */
11070 if (bed->want_got_plt)
11073 gotoff = bed->got_header_size;
11075 /* Do the local .got entries first. */
11076 for (i = info->input_bfds; i; i = i->link_next)
11078 bfd_signed_vma *local_got;
11079 bfd_size_type j, locsymcount;
11080 Elf_Internal_Shdr *symtab_hdr;
11082 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
11085 local_got = elf_local_got_refcounts (i);
11089 symtab_hdr = &elf_tdata (i)->symtab_hdr;
11090 if (elf_bad_symtab (i))
11091 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11093 locsymcount = symtab_hdr->sh_info;
11095 for (j = 0; j < locsymcount; ++j)
11097 if (local_got[j] > 0)
11099 local_got[j] = gotoff;
11100 gotoff += got_elt_size;
11103 local_got[j] = (bfd_vma) -1;
11107 /* Then the global .got entries. .plt refcounts are handled by
11108 adjust_dynamic_symbol */
11109 gofarg.gotoff = gotoff;
11110 gofarg.got_elt_size = got_elt_size;
11111 elf_link_hash_traverse (elf_hash_table (info),
11112 elf_gc_allocate_got_offsets,
11117 /* Many folk need no more in the way of final link than this, once
11118 got entry reference counting is enabled. */
11121 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
11123 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
11126 /* Invoke the regular ELF backend linker to do all the work. */
11127 return bfd_elf_final_link (abfd, info);
11131 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
11133 struct elf_reloc_cookie *rcookie = cookie;
11135 if (rcookie->bad_symtab)
11136 rcookie->rel = rcookie->rels;
11138 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
11140 unsigned long r_symndx;
11142 if (! rcookie->bad_symtab)
11143 if (rcookie->rel->r_offset > offset)
11145 if (rcookie->rel->r_offset != offset)
11148 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
11149 if (r_symndx == SHN_UNDEF)
11152 if (r_symndx >= rcookie->locsymcount
11153 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11155 struct elf_link_hash_entry *h;
11157 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
11159 while (h->root.type == bfd_link_hash_indirect
11160 || h->root.type == bfd_link_hash_warning)
11161 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11163 if ((h->root.type == bfd_link_hash_defined
11164 || h->root.type == bfd_link_hash_defweak)
11165 && elf_discarded_section (h->root.u.def.section))
11172 /* It's not a relocation against a global symbol,
11173 but it could be a relocation against a local
11174 symbol for a discarded section. */
11176 Elf_Internal_Sym *isym;
11178 /* Need to: get the symbol; get the section. */
11179 isym = &rcookie->locsyms[r_symndx];
11180 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
11182 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
11183 if (isec != NULL && elf_discarded_section (isec))
11192 /* Discard unneeded references to discarded sections.
11193 Returns TRUE if any section's size was changed. */
11194 /* This function assumes that the relocations are in sorted order,
11195 which is true for all known assemblers. */
11198 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
11200 struct elf_reloc_cookie cookie;
11201 asection *stab, *eh;
11202 Elf_Internal_Shdr *symtab_hdr;
11203 const struct elf_backend_data *bed;
11205 unsigned int count;
11206 bfd_boolean ret = FALSE;
11208 if (info->traditional_format
11209 || !is_elf_hash_table (info->hash))
11212 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
11214 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
11217 bed = get_elf_backend_data (abfd);
11219 if ((abfd->flags & DYNAMIC) != 0)
11223 if (!info->relocatable)
11225 eh = bfd_get_section_by_name (abfd, ".eh_frame");
11228 || bfd_is_abs_section (eh->output_section)))
11232 stab = bfd_get_section_by_name (abfd, ".stab");
11234 && (stab->size == 0
11235 || bfd_is_abs_section (stab->output_section)
11236 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
11241 && bed->elf_backend_discard_info == NULL)
11244 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11245 cookie.abfd = abfd;
11246 cookie.sym_hashes = elf_sym_hashes (abfd);
11247 cookie.bad_symtab = elf_bad_symtab (abfd);
11248 if (cookie.bad_symtab)
11250 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11251 cookie.extsymoff = 0;
11255 cookie.locsymcount = symtab_hdr->sh_info;
11256 cookie.extsymoff = symtab_hdr->sh_info;
11259 if (bed->s->arch_size == 32)
11260 cookie.r_sym_shift = 8;
11262 cookie.r_sym_shift = 32;
11264 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11265 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
11267 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11268 cookie.locsymcount, 0,
11270 if (cookie.locsyms == NULL)
11272 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11279 cookie.rels = NULL;
11280 count = stab->reloc_count;
11282 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
11283 info->keep_memory);
11284 if (cookie.rels != NULL)
11286 cookie.rel = cookie.rels;
11287 cookie.relend = cookie.rels;
11288 cookie.relend += count * bed->s->int_rels_per_ext_rel;
11289 if (_bfd_discard_section_stabs (abfd, stab,
11290 elf_section_data (stab)->sec_info,
11291 bfd_elf_reloc_symbol_deleted_p,
11294 if (elf_section_data (stab)->relocs != cookie.rels)
11295 free (cookie.rels);
11301 cookie.rels = NULL;
11302 count = eh->reloc_count;
11304 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
11305 info->keep_memory);
11306 cookie.rel = cookie.rels;
11307 cookie.relend = cookie.rels;
11308 if (cookie.rels != NULL)
11309 cookie.relend += count * bed->s->int_rels_per_ext_rel;
11311 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
11312 bfd_elf_reloc_symbol_deleted_p,
11316 if (cookie.rels != NULL
11317 && elf_section_data (eh)->relocs != cookie.rels)
11318 free (cookie.rels);
11321 if (bed->elf_backend_discard_info != NULL
11322 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
11325 if (cookie.locsyms != NULL
11326 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
11328 if (! info->keep_memory)
11329 free (cookie.locsyms);
11331 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
11335 if (info->eh_frame_hdr
11336 && !info->relocatable
11337 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
11344 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section *sec,
11345 struct bfd_link_info *info)
11348 const char *name, *p;
11349 struct bfd_section_already_linked *l;
11350 struct bfd_section_already_linked_hash_entry *already_linked_list;
11352 if (sec->output_section == bfd_abs_section_ptr)
11355 flags = sec->flags;
11357 /* Return if it isn't a linkonce section. A comdat group section
11358 also has SEC_LINK_ONCE set. */
11359 if ((flags & SEC_LINK_ONCE) == 0)
11362 /* Don't put group member sections on our list of already linked
11363 sections. They are handled as a group via their group section. */
11364 if (elf_sec_group (sec) != NULL)
11367 /* FIXME: When doing a relocatable link, we may have trouble
11368 copying relocations in other sections that refer to local symbols
11369 in the section being discarded. Those relocations will have to
11370 be converted somehow; as of this writing I'm not sure that any of
11371 the backends handle that correctly.
11373 It is tempting to instead not discard link once sections when
11374 doing a relocatable link (technically, they should be discarded
11375 whenever we are building constructors). However, that fails,
11376 because the linker winds up combining all the link once sections
11377 into a single large link once section, which defeats the purpose
11378 of having link once sections in the first place.
11380 Also, not merging link once sections in a relocatable link
11381 causes trouble for MIPS ELF, which relies on link once semantics
11382 to handle the .reginfo section correctly. */
11384 name = bfd_get_section_name (abfd, sec);
11386 if (CONST_STRNEQ (name, ".gnu.linkonce.")
11387 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
11392 already_linked_list = bfd_section_already_linked_table_lookup (p);
11394 for (l = already_linked_list->entry; l != NULL; l = l->next)
11396 /* We may have 2 different types of sections on the list: group
11397 sections and linkonce sections. Match like sections. */
11398 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
11399 && strcmp (name, l->sec->name) == 0
11400 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
11402 /* The section has already been linked. See if we should
11403 issue a warning. */
11404 switch (flags & SEC_LINK_DUPLICATES)
11409 case SEC_LINK_DUPLICATES_DISCARD:
11412 case SEC_LINK_DUPLICATES_ONE_ONLY:
11413 (*_bfd_error_handler)
11414 (_("%B: ignoring duplicate section `%A'"),
11418 case SEC_LINK_DUPLICATES_SAME_SIZE:
11419 if (sec->size != l->sec->size)
11420 (*_bfd_error_handler)
11421 (_("%B: duplicate section `%A' has different size"),
11425 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
11426 if (sec->size != l->sec->size)
11427 (*_bfd_error_handler)
11428 (_("%B: duplicate section `%A' has different size"),
11430 else if (sec->size != 0)
11432 bfd_byte *sec_contents, *l_sec_contents;
11434 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
11435 (*_bfd_error_handler)
11436 (_("%B: warning: could not read contents of section `%A'"),
11438 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
11440 (*_bfd_error_handler)
11441 (_("%B: warning: could not read contents of section `%A'"),
11442 l->sec->owner, l->sec);
11443 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
11444 (*_bfd_error_handler)
11445 (_("%B: warning: duplicate section `%A' has different contents"),
11449 free (sec_contents);
11450 if (l_sec_contents)
11451 free (l_sec_contents);
11456 /* Set the output_section field so that lang_add_section
11457 does not create a lang_input_section structure for this
11458 section. Since there might be a symbol in the section
11459 being discarded, we must retain a pointer to the section
11460 which we are really going to use. */
11461 sec->output_section = bfd_abs_section_ptr;
11462 sec->kept_section = l->sec;
11464 if (flags & SEC_GROUP)
11466 asection *first = elf_next_in_group (sec);
11467 asection *s = first;
11471 s->output_section = bfd_abs_section_ptr;
11472 /* Record which group discards it. */
11473 s->kept_section = l->sec;
11474 s = elf_next_in_group (s);
11475 /* These lists are circular. */
11485 /* A single member comdat group section may be discarded by a
11486 linkonce section and vice versa. */
11488 if ((flags & SEC_GROUP) != 0)
11490 asection *first = elf_next_in_group (sec);
11492 if (first != NULL && elf_next_in_group (first) == first)
11493 /* Check this single member group against linkonce sections. */
11494 for (l = already_linked_list->entry; l != NULL; l = l->next)
11495 if ((l->sec->flags & SEC_GROUP) == 0
11496 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
11497 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
11499 first->output_section = bfd_abs_section_ptr;
11500 first->kept_section = l->sec;
11501 sec->output_section = bfd_abs_section_ptr;
11506 /* Check this linkonce section against single member groups. */
11507 for (l = already_linked_list->entry; l != NULL; l = l->next)
11508 if (l->sec->flags & SEC_GROUP)
11510 asection *first = elf_next_in_group (l->sec);
11513 && elf_next_in_group (first) == first
11514 && bfd_elf_match_symbols_in_sections (first, sec, info))
11516 sec->output_section = bfd_abs_section_ptr;
11517 sec->kept_section = first;
11522 /* This is the first section with this name. Record it. */
11523 bfd_section_already_linked_table_insert (already_linked_list, sec);
11527 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
11529 return sym->st_shndx == SHN_COMMON;
11533 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
11539 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
11541 return bfd_com_section_ptr;