1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
28 #include "safe-ctype.h"
29 #include "libiberty.h"
32 /* Define a symbol in a dynamic linkage section. */
34 struct elf_link_hash_entry *
35 _bfd_elf_define_linkage_sym (bfd *abfd,
36 struct bfd_link_info *info,
40 struct elf_link_hash_entry *h;
41 struct bfd_link_hash_entry *bh;
42 const struct elf_backend_data *bed;
44 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
47 /* Zap symbol defined in an as-needed lib that wasn't linked.
48 This is a symptom of a larger problem: Absolute symbols
49 defined in shared libraries can't be overridden, because we
50 lose the link to the bfd which is via the symbol section. */
51 h->root.type = bfd_link_hash_new;
55 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
57 get_elf_backend_data (abfd)->collect,
60 h = (struct elf_link_hash_entry *) bh;
63 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
65 bed = get_elf_backend_data (abfd);
66 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
71 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
75 struct elf_link_hash_entry *h;
76 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
79 /* This function may be called more than once. */
80 s = bfd_get_section_by_name (abfd, ".got");
81 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
84 switch (bed->s->arch_size)
95 bfd_set_error (bfd_error_bad_value);
99 flags = bed->dynamic_sec_flags;
101 s = bfd_make_section_with_flags (abfd, ".got", flags);
103 || !bfd_set_section_alignment (abfd, s, ptralign))
106 if (bed->want_got_plt)
108 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
110 || !bfd_set_section_alignment (abfd, s, ptralign))
114 if (bed->want_got_sym)
116 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
117 (or .got.plt) section. We don't do this in the linker script
118 because we don't want to define the symbol if we are not creating
119 a global offset table. */
120 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
121 elf_hash_table (info)->hgot = h;
126 /* The first bit of the global offset table is the header. */
127 s->size += bed->got_header_size;
132 /* Create a strtab to hold the dynamic symbol names. */
134 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
136 struct elf_link_hash_table *hash_table;
138 hash_table = elf_hash_table (info);
139 if (hash_table->dynobj == NULL)
140 hash_table->dynobj = abfd;
142 if (hash_table->dynstr == NULL)
144 hash_table->dynstr = _bfd_elf_strtab_init ();
145 if (hash_table->dynstr == NULL)
151 /* Create some sections which will be filled in with dynamic linking
152 information. ABFD is an input file which requires dynamic sections
153 to be created. The dynamic sections take up virtual memory space
154 when the final executable is run, so we need to create them before
155 addresses are assigned to the output sections. We work out the
156 actual contents and size of these sections later. */
159 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
162 register asection *s;
163 const struct elf_backend_data *bed;
165 if (! is_elf_hash_table (info->hash))
168 if (elf_hash_table (info)->dynamic_sections_created)
171 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
174 abfd = elf_hash_table (info)->dynobj;
175 bed = get_elf_backend_data (abfd);
177 flags = bed->dynamic_sec_flags;
179 /* A dynamically linked executable has a .interp section, but a
180 shared library does not. */
181 if (info->executable)
183 s = bfd_make_section_with_flags (abfd, ".interp",
184 flags | SEC_READONLY);
189 /* Create sections to hold version informations. These are removed
190 if they are not needed. */
191 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
192 flags | SEC_READONLY);
194 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
197 s = bfd_make_section_with_flags (abfd, ".gnu.version",
198 flags | SEC_READONLY);
200 || ! bfd_set_section_alignment (abfd, s, 1))
203 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
204 flags | SEC_READONLY);
206 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
209 s = bfd_make_section_with_flags (abfd, ".dynsym",
210 flags | SEC_READONLY);
212 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
215 s = bfd_make_section_with_flags (abfd, ".dynstr",
216 flags | SEC_READONLY);
220 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
222 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
225 /* The special symbol _DYNAMIC is always set to the start of the
226 .dynamic section. We could set _DYNAMIC in a linker script, but we
227 only want to define it if we are, in fact, creating a .dynamic
228 section. We don't want to define it if there is no .dynamic
229 section, since on some ELF platforms the start up code examines it
230 to decide how to initialize the process. */
231 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
236 s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
238 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
240 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
243 if (info->emit_gnu_hash)
245 s = bfd_make_section_with_flags (abfd, ".gnu.hash",
246 flags | SEC_READONLY);
248 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
250 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
251 4 32-bit words followed by variable count of 64-bit words, then
252 variable count of 32-bit words. */
253 if (bed->s->arch_size == 64)
254 elf_section_data (s)->this_hdr.sh_entsize = 0;
256 elf_section_data (s)->this_hdr.sh_entsize = 4;
259 /* Let the backend create the rest of the sections. This lets the
260 backend set the right flags. The backend will normally create
261 the .got and .plt sections. */
262 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
265 elf_hash_table (info)->dynamic_sections_created = TRUE;
270 /* Create dynamic sections when linking against a dynamic object. */
273 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
275 flagword flags, pltflags;
276 struct elf_link_hash_entry *h;
278 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
280 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
281 .rel[a].bss sections. */
282 flags = bed->dynamic_sec_flags;
285 if (bed->plt_not_loaded)
286 /* We do not clear SEC_ALLOC here because we still want the OS to
287 allocate space for the section; it's just that there's nothing
288 to read in from the object file. */
289 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
291 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
292 if (bed->plt_readonly)
293 pltflags |= SEC_READONLY;
295 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
297 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
300 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
302 if (bed->want_plt_sym)
304 h = _bfd_elf_define_linkage_sym (abfd, info, s,
305 "_PROCEDURE_LINKAGE_TABLE_");
306 elf_hash_table (info)->hplt = h;
311 s = bfd_make_section_with_flags (abfd,
312 (bed->rela_plts_and_copies_p
313 ? ".rela.plt" : ".rel.plt"),
314 flags | SEC_READONLY);
316 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
319 if (! _bfd_elf_create_got_section (abfd, info))
322 if (bed->want_dynbss)
324 /* The .dynbss section is a place to put symbols which are defined
325 by dynamic objects, are referenced by regular objects, and are
326 not functions. We must allocate space for them in the process
327 image and use a R_*_COPY reloc to tell the dynamic linker to
328 initialize them at run time. The linker script puts the .dynbss
329 section into the .bss section of the final image. */
330 s = bfd_make_section_with_flags (abfd, ".dynbss",
332 | SEC_LINKER_CREATED));
336 /* The .rel[a].bss section holds copy relocs. This section is not
337 normally needed. We need to create it here, though, so that the
338 linker will map it to an output section. We can't just create it
339 only if we need it, because we will not know whether we need it
340 until we have seen all the input files, and the first time the
341 main linker code calls BFD after examining all the input files
342 (size_dynamic_sections) the input sections have already been
343 mapped to the output sections. If the section turns out not to
344 be needed, we can discard it later. We will never need this
345 section when generating a shared object, since they do not use
349 s = bfd_make_section_with_flags (abfd,
350 (bed->rela_plts_and_copies_p
351 ? ".rela.bss" : ".rel.bss"),
352 flags | SEC_READONLY);
354 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
362 /* Record a new dynamic symbol. We record the dynamic symbols as we
363 read the input files, since we need to have a list of all of them
364 before we can determine the final sizes of the output sections.
365 Note that we may actually call this function even though we are not
366 going to output any dynamic symbols; in some cases we know that a
367 symbol should be in the dynamic symbol table, but only if there is
371 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
372 struct elf_link_hash_entry *h)
374 if (h->dynindx == -1)
376 struct elf_strtab_hash *dynstr;
381 /* XXX: The ABI draft says the linker must turn hidden and
382 internal symbols into STB_LOCAL symbols when producing the
383 DSO. However, if ld.so honors st_other in the dynamic table,
384 this would not be necessary. */
385 switch (ELF_ST_VISIBILITY (h->other))
389 if (h->root.type != bfd_link_hash_undefined
390 && h->root.type != bfd_link_hash_undefweak)
393 if (!elf_hash_table (info)->is_relocatable_executable)
401 h->dynindx = elf_hash_table (info)->dynsymcount;
402 ++elf_hash_table (info)->dynsymcount;
404 dynstr = elf_hash_table (info)->dynstr;
407 /* Create a strtab to hold the dynamic symbol names. */
408 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
413 /* We don't put any version information in the dynamic string
415 name = h->root.root.string;
416 p = strchr (name, ELF_VER_CHR);
418 /* We know that the p points into writable memory. In fact,
419 there are only a few symbols that have read-only names, being
420 those like _GLOBAL_OFFSET_TABLE_ that are created specially
421 by the backends. Most symbols will have names pointing into
422 an ELF string table read from a file, or to objalloc memory. */
425 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
430 if (indx == (bfd_size_type) -1)
432 h->dynstr_index = indx;
438 /* Mark a symbol dynamic. */
441 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
442 struct elf_link_hash_entry *h,
443 Elf_Internal_Sym *sym)
445 struct bfd_elf_dynamic_list *d = info->dynamic_list;
447 /* It may be called more than once on the same H. */
448 if(h->dynamic || info->relocatable)
451 if ((info->dynamic_data
452 && (h->type == STT_OBJECT
454 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
456 && h->root.type == bfd_link_hash_new
457 && (*d->match) (&d->head, NULL, h->root.root.string)))
461 /* Record an assignment to a symbol made by a linker script. We need
462 this in case some dynamic object refers to this symbol. */
465 bfd_elf_record_link_assignment (bfd *output_bfd,
466 struct bfd_link_info *info,
471 struct elf_link_hash_entry *h, *hv;
472 struct elf_link_hash_table *htab;
473 const struct elf_backend_data *bed;
475 if (!is_elf_hash_table (info->hash))
478 htab = elf_hash_table (info);
479 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
483 switch (h->root.type)
485 case bfd_link_hash_defined:
486 case bfd_link_hash_defweak:
487 case bfd_link_hash_common:
489 case bfd_link_hash_undefweak:
490 case bfd_link_hash_undefined:
491 /* Since we're defining the symbol, don't let it seem to have not
492 been defined. record_dynamic_symbol and size_dynamic_sections
493 may depend on this. */
494 h->root.type = bfd_link_hash_new;
495 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
496 bfd_link_repair_undef_list (&htab->root);
498 case bfd_link_hash_new:
499 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
502 case bfd_link_hash_indirect:
503 /* We had a versioned symbol in a dynamic library. We make the
504 the versioned symbol point to this one. */
505 bed = get_elf_backend_data (output_bfd);
507 while (hv->root.type == bfd_link_hash_indirect
508 || hv->root.type == bfd_link_hash_warning)
509 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
510 /* We don't need to update h->root.u since linker will set them
512 h->root.type = bfd_link_hash_undefined;
513 hv->root.type = bfd_link_hash_indirect;
514 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
515 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
517 case bfd_link_hash_warning:
522 /* If this symbol is being provided by the linker script, and it is
523 currently defined by a dynamic object, but not by a regular
524 object, then mark it as undefined so that the generic linker will
525 force the correct value. */
529 h->root.type = bfd_link_hash_undefined;
531 /* If this symbol is not being provided by the linker script, and it is
532 currently defined by a dynamic object, but not by a regular object,
533 then clear out any version information because the symbol will not be
534 associated with the dynamic object any more. */
538 h->verinfo.verdef = NULL;
542 if (provide && hidden)
544 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
546 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
547 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
550 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
552 if (!info->relocatable
554 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
555 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
561 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
564 if (! bfd_elf_link_record_dynamic_symbol (info, h))
567 /* If this is a weak defined symbol, and we know a corresponding
568 real symbol from the same dynamic object, make sure the real
569 symbol is also made into a dynamic symbol. */
570 if (h->u.weakdef != NULL
571 && h->u.weakdef->dynindx == -1)
573 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
581 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
582 success, and 2 on a failure caused by attempting to record a symbol
583 in a discarded section, eg. a discarded link-once section symbol. */
586 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
591 struct elf_link_local_dynamic_entry *entry;
592 struct elf_link_hash_table *eht;
593 struct elf_strtab_hash *dynstr;
594 unsigned long dynstr_index;
596 Elf_External_Sym_Shndx eshndx;
597 char esym[sizeof (Elf64_External_Sym)];
599 if (! is_elf_hash_table (info->hash))
602 /* See if the entry exists already. */
603 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
604 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
607 amt = sizeof (*entry);
608 entry = bfd_alloc (input_bfd, amt);
612 /* Go find the symbol, so that we can find it's name. */
613 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
614 1, input_indx, &entry->isym, esym, &eshndx))
616 bfd_release (input_bfd, entry);
620 if (entry->isym.st_shndx != SHN_UNDEF
621 && entry->isym.st_shndx < SHN_LORESERVE)
625 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
626 if (s == NULL || bfd_is_abs_section (s->output_section))
628 /* We can still bfd_release here as nothing has done another
629 bfd_alloc. We can't do this later in this function. */
630 bfd_release (input_bfd, entry);
635 name = (bfd_elf_string_from_elf_section
636 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
637 entry->isym.st_name));
639 dynstr = elf_hash_table (info)->dynstr;
642 /* Create a strtab to hold the dynamic symbol names. */
643 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
648 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
649 if (dynstr_index == (unsigned long) -1)
651 entry->isym.st_name = dynstr_index;
653 eht = elf_hash_table (info);
655 entry->next = eht->dynlocal;
656 eht->dynlocal = entry;
657 entry->input_bfd = input_bfd;
658 entry->input_indx = input_indx;
661 /* Whatever binding the symbol had before, it's now local. */
663 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
665 /* The dynindx will be set at the end of size_dynamic_sections. */
670 /* Return the dynindex of a local dynamic symbol. */
673 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
677 struct elf_link_local_dynamic_entry *e;
679 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
680 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
685 /* This function is used to renumber the dynamic symbols, if some of
686 them are removed because they are marked as local. This is called
687 via elf_link_hash_traverse. */
690 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
693 size_t *count = data;
695 if (h->root.type == bfd_link_hash_warning)
696 h = (struct elf_link_hash_entry *) h->root.u.i.link;
701 if (h->dynindx != -1)
702 h->dynindx = ++(*count);
708 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
709 STB_LOCAL binding. */
712 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
715 size_t *count = data;
717 if (h->root.type == bfd_link_hash_warning)
718 h = (struct elf_link_hash_entry *) h->root.u.i.link;
720 if (!h->forced_local)
723 if (h->dynindx != -1)
724 h->dynindx = ++(*count);
729 /* Return true if the dynamic symbol for a given section should be
730 omitted when creating a shared library. */
732 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
733 struct bfd_link_info *info,
736 struct elf_link_hash_table *htab;
738 switch (elf_section_data (p)->this_hdr.sh_type)
742 /* If sh_type is yet undecided, assume it could be
743 SHT_PROGBITS/SHT_NOBITS. */
745 htab = elf_hash_table (info);
746 if (p == htab->tls_sec)
749 if (htab->text_index_section != NULL)
750 return p != htab->text_index_section && p != htab->data_index_section;
752 if (strcmp (p->name, ".got") == 0
753 || strcmp (p->name, ".got.plt") == 0
754 || strcmp (p->name, ".plt") == 0)
758 if (htab->dynobj != NULL
759 && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
760 && (ip->flags & SEC_LINKER_CREATED)
761 && ip->output_section == p)
766 /* There shouldn't be section relative relocations
767 against any other section. */
773 /* Assign dynsym indices. In a shared library we generate a section
774 symbol for each output section, which come first. Next come symbols
775 which have been forced to local binding. Then all of the back-end
776 allocated local dynamic syms, followed by the rest of the global
780 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
781 struct bfd_link_info *info,
782 unsigned long *section_sym_count)
784 unsigned long dynsymcount = 0;
786 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
788 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
790 for (p = output_bfd->sections; p ; p = p->next)
791 if ((p->flags & SEC_EXCLUDE) == 0
792 && (p->flags & SEC_ALLOC) != 0
793 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
794 elf_section_data (p)->dynindx = ++dynsymcount;
796 elf_section_data (p)->dynindx = 0;
798 *section_sym_count = dynsymcount;
800 elf_link_hash_traverse (elf_hash_table (info),
801 elf_link_renumber_local_hash_table_dynsyms,
804 if (elf_hash_table (info)->dynlocal)
806 struct elf_link_local_dynamic_entry *p;
807 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
808 p->dynindx = ++dynsymcount;
811 elf_link_hash_traverse (elf_hash_table (info),
812 elf_link_renumber_hash_table_dynsyms,
815 /* There is an unused NULL entry at the head of the table which
816 we must account for in our count. Unless there weren't any
817 symbols, which means we'll have no table at all. */
818 if (dynsymcount != 0)
821 elf_hash_table (info)->dynsymcount = dynsymcount;
825 /* This function is called when we want to define a new symbol. It
826 handles the various cases which arise when we find a definition in
827 a dynamic object, or when there is already a definition in a
828 dynamic object. The new symbol is described by NAME, SYM, PSEC,
829 and PVALUE. We set SYM_HASH to the hash table entry. We set
830 OVERRIDE if the old symbol is overriding a new definition. We set
831 TYPE_CHANGE_OK if it is OK for the type to change. We set
832 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
833 change, we mean that we shouldn't warn if the type or size does
834 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
835 object is overridden by a regular object. */
838 _bfd_elf_merge_symbol (bfd *abfd,
839 struct bfd_link_info *info,
841 Elf_Internal_Sym *sym,
844 unsigned int *pold_alignment,
845 struct elf_link_hash_entry **sym_hash,
847 bfd_boolean *override,
848 bfd_boolean *type_change_ok,
849 bfd_boolean *size_change_ok)
851 asection *sec, *oldsec;
852 struct elf_link_hash_entry *h;
853 struct elf_link_hash_entry *flip;
856 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
857 bfd_boolean newweak, oldweak, newfunc, oldfunc;
858 const struct elf_backend_data *bed;
864 bind = ELF_ST_BIND (sym->st_info);
866 /* Silently discard TLS symbols from --just-syms. There's no way to
867 combine a static TLS block with a new TLS block for this executable. */
868 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
869 && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
875 if (! bfd_is_und_section (sec))
876 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
878 h = ((struct elf_link_hash_entry *)
879 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
884 bed = get_elf_backend_data (abfd);
886 /* This code is for coping with dynamic objects, and is only useful
887 if we are doing an ELF link. */
888 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
891 /* For merging, we only care about real symbols. */
893 while (h->root.type == bfd_link_hash_indirect
894 || h->root.type == bfd_link_hash_warning)
895 h = (struct elf_link_hash_entry *) h->root.u.i.link;
897 /* We have to check it for every instance since the first few may be
898 refereences and not all compilers emit symbol type for undefined
900 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
902 /* If we just created the symbol, mark it as being an ELF symbol.
903 Other than that, there is nothing to do--there is no merge issue
904 with a newly defined symbol--so we just return. */
906 if (h->root.type == bfd_link_hash_new)
912 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
915 switch (h->root.type)
922 case bfd_link_hash_undefined:
923 case bfd_link_hash_undefweak:
924 oldbfd = h->root.u.undef.abfd;
928 case bfd_link_hash_defined:
929 case bfd_link_hash_defweak:
930 oldbfd = h->root.u.def.section->owner;
931 oldsec = h->root.u.def.section;
934 case bfd_link_hash_common:
935 oldbfd = h->root.u.c.p->section->owner;
936 oldsec = h->root.u.c.p->section;
940 /* In cases involving weak versioned symbols, we may wind up trying
941 to merge a symbol with itself. Catch that here, to avoid the
942 confusion that results if we try to override a symbol with
943 itself. The additional tests catch cases like
944 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
945 dynamic object, which we do want to handle here. */
947 && ((abfd->flags & DYNAMIC) == 0
951 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
952 respectively, is from a dynamic object. */
954 newdyn = (abfd->flags & DYNAMIC) != 0;
958 olddyn = (oldbfd->flags & DYNAMIC) != 0;
959 else if (oldsec != NULL)
961 /* This handles the special SHN_MIPS_{TEXT,DATA} section
962 indices used by MIPS ELF. */
963 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
966 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
967 respectively, appear to be a definition rather than reference. */
969 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
971 olddef = (h->root.type != bfd_link_hash_undefined
972 && h->root.type != bfd_link_hash_undefweak
973 && h->root.type != bfd_link_hash_common);
975 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
976 respectively, appear to be a function. */
978 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
979 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
981 oldfunc = (h->type != STT_NOTYPE
982 && bed->is_function_type (h->type));
984 /* When we try to create a default indirect symbol from the dynamic
985 definition with the default version, we skip it if its type and
986 the type of existing regular definition mismatch. We only do it
987 if the existing regular definition won't be dynamic. */
988 if (pold_alignment == NULL
990 && !info->export_dynamic
995 && (olddef || h->root.type == bfd_link_hash_common)
996 && ELF_ST_TYPE (sym->st_info) != h->type
997 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
998 && h->type != STT_NOTYPE
999 && !(newfunc && oldfunc))
1005 /* Check TLS symbol. We don't check undefined symbol introduced by
1007 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
1008 && ELF_ST_TYPE (sym->st_info) != h->type
1012 bfd_boolean ntdef, tdef;
1013 asection *ntsec, *tsec;
1015 if (h->type == STT_TLS)
1035 (*_bfd_error_handler)
1036 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1037 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1038 else if (!tdef && !ntdef)
1039 (*_bfd_error_handler)
1040 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1041 tbfd, ntbfd, h->root.root.string);
1043 (*_bfd_error_handler)
1044 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1045 tbfd, tsec, ntbfd, h->root.root.string);
1047 (*_bfd_error_handler)
1048 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1049 tbfd, ntbfd, ntsec, h->root.root.string);
1051 bfd_set_error (bfd_error_bad_value);
1055 /* We need to remember if a symbol has a definition in a dynamic
1056 object or is weak in all dynamic objects. Internal and hidden
1057 visibility will make it unavailable to dynamic objects. */
1058 if (newdyn && !h->dynamic_def)
1060 if (!bfd_is_und_section (sec))
1064 /* Check if this symbol is weak in all dynamic objects. If it
1065 is the first time we see it in a dynamic object, we mark
1066 if it is weak. Otherwise, we clear it. */
1067 if (!h->ref_dynamic)
1069 if (bind == STB_WEAK)
1070 h->dynamic_weak = 1;
1072 else if (bind != STB_WEAK)
1073 h->dynamic_weak = 0;
1077 /* If the old symbol has non-default visibility, we ignore the new
1078 definition from a dynamic object. */
1080 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1081 && !bfd_is_und_section (sec))
1084 /* Make sure this symbol is dynamic. */
1086 /* A protected symbol has external availability. Make sure it is
1087 recorded as dynamic.
1089 FIXME: Should we check type and size for protected symbol? */
1090 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1091 return bfd_elf_link_record_dynamic_symbol (info, h);
1096 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1099 /* If the new symbol with non-default visibility comes from a
1100 relocatable file and the old definition comes from a dynamic
1101 object, we remove the old definition. */
1102 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1104 /* Handle the case where the old dynamic definition is
1105 default versioned. We need to copy the symbol info from
1106 the symbol with default version to the normal one if it
1107 was referenced before. */
1110 const struct elf_backend_data *bed
1111 = get_elf_backend_data (abfd);
1112 struct elf_link_hash_entry *vh = *sym_hash;
1113 vh->root.type = h->root.type;
1114 h->root.type = bfd_link_hash_indirect;
1115 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1116 /* Protected symbols will override the dynamic definition
1117 with default version. */
1118 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1120 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1121 vh->dynamic_def = 1;
1122 vh->ref_dynamic = 1;
1126 h->root.type = vh->root.type;
1127 vh->ref_dynamic = 0;
1128 /* We have to hide it here since it was made dynamic
1129 global with extra bits when the symbol info was
1130 copied from the old dynamic definition. */
1131 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1139 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1140 && bfd_is_und_section (sec))
1142 /* If the new symbol is undefined and the old symbol was
1143 also undefined before, we need to make sure
1144 _bfd_generic_link_add_one_symbol doesn't mess
1145 up the linker hash table undefs list. Since the old
1146 definition came from a dynamic object, it is still on the
1148 h->root.type = bfd_link_hash_undefined;
1149 h->root.u.undef.abfd = abfd;
1153 h->root.type = bfd_link_hash_new;
1154 h->root.u.undef.abfd = NULL;
1163 /* FIXME: Should we check type and size for protected symbol? */
1169 /* Differentiate strong and weak symbols. */
1170 newweak = bind == STB_WEAK;
1171 oldweak = (h->root.type == bfd_link_hash_defweak
1172 || h->root.type == bfd_link_hash_undefweak);
1174 /* If a new weak symbol definition comes from a regular file and the
1175 old symbol comes from a dynamic library, we treat the new one as
1176 strong. Similarly, an old weak symbol definition from a regular
1177 file is treated as strong when the new symbol comes from a dynamic
1178 library. Further, an old weak symbol from a dynamic library is
1179 treated as strong if the new symbol is from a dynamic library.
1180 This reflects the way glibc's ld.so works.
1182 Do this before setting *type_change_ok or *size_change_ok so that
1183 we warn properly when dynamic library symbols are overridden. */
1185 if (newdef && !newdyn && olddyn)
1187 if (olddef && newdyn)
1190 /* Allow changes between different types of funciton symbol. */
1191 if (newfunc && oldfunc)
1192 *type_change_ok = TRUE;
1194 /* It's OK to change the type if either the existing symbol or the
1195 new symbol is weak. A type change is also OK if the old symbol
1196 is undefined and the new symbol is defined. */
1201 && h->root.type == bfd_link_hash_undefined))
1202 *type_change_ok = TRUE;
1204 /* It's OK to change the size if either the existing symbol or the
1205 new symbol is weak, or if the old symbol is undefined. */
1208 || h->root.type == bfd_link_hash_undefined)
1209 *size_change_ok = TRUE;
1211 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1212 symbol, respectively, appears to be a common symbol in a dynamic
1213 object. If a symbol appears in an uninitialized section, and is
1214 not weak, and is not a function, then it may be a common symbol
1215 which was resolved when the dynamic object was created. We want
1216 to treat such symbols specially, because they raise special
1217 considerations when setting the symbol size: if the symbol
1218 appears as a common symbol in a regular object, and the size in
1219 the regular object is larger, we must make sure that we use the
1220 larger size. This problematic case can always be avoided in C,
1221 but it must be handled correctly when using Fortran shared
1224 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1225 likewise for OLDDYNCOMMON and OLDDEF.
1227 Note that this test is just a heuristic, and that it is quite
1228 possible to have an uninitialized symbol in a shared object which
1229 is really a definition, rather than a common symbol. This could
1230 lead to some minor confusion when the symbol really is a common
1231 symbol in some regular object. However, I think it will be
1237 && (sec->flags & SEC_ALLOC) != 0
1238 && (sec->flags & SEC_LOAD) == 0
1241 newdyncommon = TRUE;
1243 newdyncommon = FALSE;
1247 && h->root.type == bfd_link_hash_defined
1249 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1250 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1253 olddyncommon = TRUE;
1255 olddyncommon = FALSE;
1257 /* We now know everything about the old and new symbols. We ask the
1258 backend to check if we can merge them. */
1259 if (bed->merge_symbol
1260 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1261 pold_alignment, skip, override,
1262 type_change_ok, size_change_ok,
1263 &newdyn, &newdef, &newdyncommon, &newweak,
1265 &olddyn, &olddef, &olddyncommon, &oldweak,
1269 /* If both the old and the new symbols look like common symbols in a
1270 dynamic object, set the size of the symbol to the larger of the
1275 && sym->st_size != h->size)
1277 /* Since we think we have two common symbols, issue a multiple
1278 common warning if desired. Note that we only warn if the
1279 size is different. If the size is the same, we simply let
1280 the old symbol override the new one as normally happens with
1281 symbols defined in dynamic objects. */
1283 if (! ((*info->callbacks->multiple_common)
1284 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1285 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1288 if (sym->st_size > h->size)
1289 h->size = sym->st_size;
1291 *size_change_ok = TRUE;
1294 /* If we are looking at a dynamic object, and we have found a
1295 definition, we need to see if the symbol was already defined by
1296 some other object. If so, we want to use the existing
1297 definition, and we do not want to report a multiple symbol
1298 definition error; we do this by clobbering *PSEC to be
1299 bfd_und_section_ptr.
1301 We treat a common symbol as a definition if the symbol in the
1302 shared library is a function, since common symbols always
1303 represent variables; this can cause confusion in principle, but
1304 any such confusion would seem to indicate an erroneous program or
1305 shared library. We also permit a common symbol in a regular
1306 object to override a weak symbol in a shared object. */
1311 || (h->root.type == bfd_link_hash_common
1312 && (newweak || newfunc))))
1316 newdyncommon = FALSE;
1318 *psec = sec = bfd_und_section_ptr;
1319 *size_change_ok = TRUE;
1321 /* If we get here when the old symbol is a common symbol, then
1322 we are explicitly letting it override a weak symbol or
1323 function in a dynamic object, and we don't want to warn about
1324 a type change. If the old symbol is a defined symbol, a type
1325 change warning may still be appropriate. */
1327 if (h->root.type == bfd_link_hash_common)
1328 *type_change_ok = TRUE;
1331 /* Handle the special case of an old common symbol merging with a
1332 new symbol which looks like a common symbol in a shared object.
1333 We change *PSEC and *PVALUE to make the new symbol look like a
1334 common symbol, and let _bfd_generic_link_add_one_symbol do the
1338 && h->root.type == bfd_link_hash_common)
1342 newdyncommon = FALSE;
1343 *pvalue = sym->st_size;
1344 *psec = sec = bed->common_section (oldsec);
1345 *size_change_ok = TRUE;
1348 /* Skip weak definitions of symbols that are already defined. */
1349 if (newdef && olddef && newweak)
1352 /* If the old symbol is from a dynamic object, and the new symbol is
1353 a definition which is not from a dynamic object, then the new
1354 symbol overrides the old symbol. Symbols from regular files
1355 always take precedence over symbols from dynamic objects, even if
1356 they are defined after the dynamic object in the link.
1358 As above, we again permit a common symbol in a regular object to
1359 override a definition in a shared object if the shared object
1360 symbol is a function or is weak. */
1365 || (bfd_is_com_section (sec)
1366 && (oldweak || oldfunc)))
1371 /* Change the hash table entry to undefined, and let
1372 _bfd_generic_link_add_one_symbol do the right thing with the
1375 h->root.type = bfd_link_hash_undefined;
1376 h->root.u.undef.abfd = h->root.u.def.section->owner;
1377 *size_change_ok = TRUE;
1380 olddyncommon = FALSE;
1382 /* We again permit a type change when a common symbol may be
1383 overriding a function. */
1385 if (bfd_is_com_section (sec))
1389 /* If a common symbol overrides a function, make sure
1390 that it isn't defined dynamically nor has type
1393 h->type = STT_NOTYPE;
1395 *type_change_ok = TRUE;
1398 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1401 /* This union may have been set to be non-NULL when this symbol
1402 was seen in a dynamic object. We must force the union to be
1403 NULL, so that it is correct for a regular symbol. */
1404 h->verinfo.vertree = NULL;
1407 /* Handle the special case of a new common symbol merging with an
1408 old symbol that looks like it might be a common symbol defined in
1409 a shared object. Note that we have already handled the case in
1410 which a new common symbol should simply override the definition
1411 in the shared library. */
1414 && bfd_is_com_section (sec)
1417 /* It would be best if we could set the hash table entry to a
1418 common symbol, but we don't know what to use for the section
1419 or the alignment. */
1420 if (! ((*info->callbacks->multiple_common)
1421 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1422 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1425 /* If the presumed common symbol in the dynamic object is
1426 larger, pretend that the new symbol has its size. */
1428 if (h->size > *pvalue)
1431 /* We need to remember the alignment required by the symbol
1432 in the dynamic object. */
1433 BFD_ASSERT (pold_alignment);
1434 *pold_alignment = h->root.u.def.section->alignment_power;
1437 olddyncommon = FALSE;
1439 h->root.type = bfd_link_hash_undefined;
1440 h->root.u.undef.abfd = h->root.u.def.section->owner;
1442 *size_change_ok = TRUE;
1443 *type_change_ok = TRUE;
1445 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1448 h->verinfo.vertree = NULL;
1453 /* Handle the case where we had a versioned symbol in a dynamic
1454 library and now find a definition in a normal object. In this
1455 case, we make the versioned symbol point to the normal one. */
1456 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1457 flip->root.type = h->root.type;
1458 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1459 h->root.type = bfd_link_hash_indirect;
1460 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1461 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1465 flip->ref_dynamic = 1;
1472 /* This function is called to create an indirect symbol from the
1473 default for the symbol with the default version if needed. The
1474 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1475 set DYNSYM if the new indirect symbol is dynamic. */
1478 _bfd_elf_add_default_symbol (bfd *abfd,
1479 struct bfd_link_info *info,
1480 struct elf_link_hash_entry *h,
1482 Elf_Internal_Sym *sym,
1485 bfd_boolean *dynsym,
1486 bfd_boolean override)
1488 bfd_boolean type_change_ok;
1489 bfd_boolean size_change_ok;
1492 struct elf_link_hash_entry *hi;
1493 struct bfd_link_hash_entry *bh;
1494 const struct elf_backend_data *bed;
1495 bfd_boolean collect;
1496 bfd_boolean dynamic;
1498 size_t len, shortlen;
1501 /* If this symbol has a version, and it is the default version, we
1502 create an indirect symbol from the default name to the fully
1503 decorated name. This will cause external references which do not
1504 specify a version to be bound to this version of the symbol. */
1505 p = strchr (name, ELF_VER_CHR);
1506 if (p == NULL || p[1] != ELF_VER_CHR)
1511 /* We are overridden by an old definition. We need to check if we
1512 need to create the indirect symbol from the default name. */
1513 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1515 BFD_ASSERT (hi != NULL);
1518 while (hi->root.type == bfd_link_hash_indirect
1519 || hi->root.type == bfd_link_hash_warning)
1521 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1527 bed = get_elf_backend_data (abfd);
1528 collect = bed->collect;
1529 dynamic = (abfd->flags & DYNAMIC) != 0;
1531 shortlen = p - name;
1532 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1533 if (shortname == NULL)
1535 memcpy (shortname, name, shortlen);
1536 shortname[shortlen] = '\0';
1538 /* We are going to create a new symbol. Merge it with any existing
1539 symbol with this name. For the purposes of the merge, act as
1540 though we were defining the symbol we just defined, although we
1541 actually going to define an indirect symbol. */
1542 type_change_ok = FALSE;
1543 size_change_ok = FALSE;
1545 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1546 NULL, &hi, &skip, &override,
1547 &type_change_ok, &size_change_ok))
1556 if (! (_bfd_generic_link_add_one_symbol
1557 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1558 0, name, FALSE, collect, &bh)))
1560 hi = (struct elf_link_hash_entry *) bh;
1564 /* In this case the symbol named SHORTNAME is overriding the
1565 indirect symbol we want to add. We were planning on making
1566 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1567 is the name without a version. NAME is the fully versioned
1568 name, and it is the default version.
1570 Overriding means that we already saw a definition for the
1571 symbol SHORTNAME in a regular object, and it is overriding
1572 the symbol defined in the dynamic object.
1574 When this happens, we actually want to change NAME, the
1575 symbol we just added, to refer to SHORTNAME. This will cause
1576 references to NAME in the shared object to become references
1577 to SHORTNAME in the regular object. This is what we expect
1578 when we override a function in a shared object: that the
1579 references in the shared object will be mapped to the
1580 definition in the regular object. */
1582 while (hi->root.type == bfd_link_hash_indirect
1583 || hi->root.type == bfd_link_hash_warning)
1584 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1586 h->root.type = bfd_link_hash_indirect;
1587 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1591 hi->ref_dynamic = 1;
1595 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1600 /* Now set HI to H, so that the following code will set the
1601 other fields correctly. */
1605 /* Check if HI is a warning symbol. */
1606 if (hi->root.type == bfd_link_hash_warning)
1607 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1609 /* If there is a duplicate definition somewhere, then HI may not
1610 point to an indirect symbol. We will have reported an error to
1611 the user in that case. */
1613 if (hi->root.type == bfd_link_hash_indirect)
1615 struct elf_link_hash_entry *ht;
1617 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1618 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1620 /* See if the new flags lead us to realize that the symbol must
1632 if (hi->ref_regular)
1638 /* We also need to define an indirection from the nondefault version
1642 len = strlen (name);
1643 shortname = bfd_hash_allocate (&info->hash->table, len);
1644 if (shortname == NULL)
1646 memcpy (shortname, name, shortlen);
1647 memcpy (shortname + shortlen, p + 1, len - shortlen);
1649 /* Once again, merge with any existing symbol. */
1650 type_change_ok = FALSE;
1651 size_change_ok = FALSE;
1653 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1654 NULL, &hi, &skip, &override,
1655 &type_change_ok, &size_change_ok))
1663 /* Here SHORTNAME is a versioned name, so we don't expect to see
1664 the type of override we do in the case above unless it is
1665 overridden by a versioned definition. */
1666 if (hi->root.type != bfd_link_hash_defined
1667 && hi->root.type != bfd_link_hash_defweak)
1668 (*_bfd_error_handler)
1669 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1675 if (! (_bfd_generic_link_add_one_symbol
1676 (info, abfd, shortname, BSF_INDIRECT,
1677 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1679 hi = (struct elf_link_hash_entry *) bh;
1681 /* If there is a duplicate definition somewhere, then HI may not
1682 point to an indirect symbol. We will have reported an error
1683 to the user in that case. */
1685 if (hi->root.type == bfd_link_hash_indirect)
1687 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1689 /* See if the new flags lead us to realize that the symbol
1701 if (hi->ref_regular)
1711 /* This routine is used to export all defined symbols into the dynamic
1712 symbol table. It is called via elf_link_hash_traverse. */
1715 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1717 struct elf_info_failed *eif = data;
1719 /* Ignore this if we won't export it. */
1720 if (!eif->info->export_dynamic && !h->dynamic)
1723 /* Ignore indirect symbols. These are added by the versioning code. */
1724 if (h->root.type == bfd_link_hash_indirect)
1727 if (h->root.type == bfd_link_hash_warning)
1728 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1730 if (h->dynindx == -1
1734 struct bfd_elf_version_tree *t;
1735 struct bfd_elf_version_expr *d;
1737 for (t = eif->verdefs; t != NULL; t = t->next)
1739 if (t->globals.list != NULL)
1741 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1746 if (t->locals.list != NULL)
1748 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1757 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1768 /* Look through the symbols which are defined in other shared
1769 libraries and referenced here. Update the list of version
1770 dependencies. This will be put into the .gnu.version_r section.
1771 This function is called via elf_link_hash_traverse. */
1774 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1777 struct elf_find_verdep_info *rinfo = data;
1778 Elf_Internal_Verneed *t;
1779 Elf_Internal_Vernaux *a;
1782 if (h->root.type == bfd_link_hash_warning)
1783 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1785 /* We only care about symbols defined in shared objects with version
1790 || h->verinfo.verdef == NULL)
1793 /* See if we already know about this version. */
1794 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1796 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1799 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1800 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1806 /* This is a new version. Add it to tree we are building. */
1811 t = bfd_zalloc (rinfo->output_bfd, amt);
1814 rinfo->failed = TRUE;
1818 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1819 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1820 elf_tdata (rinfo->output_bfd)->verref = t;
1824 a = bfd_zalloc (rinfo->output_bfd, amt);
1827 rinfo->failed = TRUE;
1831 /* Note that we are copying a string pointer here, and testing it
1832 above. If bfd_elf_string_from_elf_section is ever changed to
1833 discard the string data when low in memory, this will have to be
1835 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1837 a->vna_flags = h->verinfo.verdef->vd_flags;
1838 a->vna_nextptr = t->vn_auxptr;
1840 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1843 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1850 /* Figure out appropriate versions for all the symbols. We may not
1851 have the version number script until we have read all of the input
1852 files, so until that point we don't know which symbols should be
1853 local. This function is called via elf_link_hash_traverse. */
1856 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1858 struct elf_assign_sym_version_info *sinfo;
1859 struct bfd_link_info *info;
1860 const struct elf_backend_data *bed;
1861 struct elf_info_failed eif;
1868 if (h->root.type == bfd_link_hash_warning)
1869 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1871 /* Fix the symbol flags. */
1874 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1877 sinfo->failed = TRUE;
1881 /* We only need version numbers for symbols defined in regular
1883 if (!h->def_regular)
1886 bed = get_elf_backend_data (sinfo->output_bfd);
1887 p = strchr (h->root.root.string, ELF_VER_CHR);
1888 if (p != NULL && h->verinfo.vertree == NULL)
1890 struct bfd_elf_version_tree *t;
1895 /* There are two consecutive ELF_VER_CHR characters if this is
1896 not a hidden symbol. */
1898 if (*p == ELF_VER_CHR)
1904 /* If there is no version string, we can just return out. */
1912 /* Look for the version. If we find it, it is no longer weak. */
1913 for (t = sinfo->verdefs; t != NULL; t = t->next)
1915 if (strcmp (t->name, p) == 0)
1919 struct bfd_elf_version_expr *d;
1921 len = p - h->root.root.string;
1922 alc = bfd_malloc (len);
1925 sinfo->failed = TRUE;
1928 memcpy (alc, h->root.root.string, len - 1);
1929 alc[len - 1] = '\0';
1930 if (alc[len - 2] == ELF_VER_CHR)
1931 alc[len - 2] = '\0';
1933 h->verinfo.vertree = t;
1937 if (t->globals.list != NULL)
1938 d = (*t->match) (&t->globals, NULL, alc);
1940 /* See if there is anything to force this symbol to
1942 if (d == NULL && t->locals.list != NULL)
1944 d = (*t->match) (&t->locals, NULL, alc);
1947 && ! info->export_dynamic)
1948 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1956 /* If we are building an application, we need to create a
1957 version node for this version. */
1958 if (t == NULL && info->executable)
1960 struct bfd_elf_version_tree **pp;
1963 /* If we aren't going to export this symbol, we don't need
1964 to worry about it. */
1965 if (h->dynindx == -1)
1969 t = bfd_zalloc (sinfo->output_bfd, amt);
1972 sinfo->failed = TRUE;
1977 t->name_indx = (unsigned int) -1;
1981 /* Don't count anonymous version tag. */
1982 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1984 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1986 t->vernum = version_index;
1990 h->verinfo.vertree = t;
1994 /* We could not find the version for a symbol when
1995 generating a shared archive. Return an error. */
1996 (*_bfd_error_handler)
1997 (_("%B: version node not found for symbol %s"),
1998 sinfo->output_bfd, h->root.root.string);
1999 bfd_set_error (bfd_error_bad_value);
2000 sinfo->failed = TRUE;
2008 /* If we don't have a version for this symbol, see if we can find
2010 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
2012 struct bfd_elf_version_tree *t;
2013 struct bfd_elf_version_tree *local_ver, *global_ver, *exist_ver;
2014 struct bfd_elf_version_expr *d;
2016 /* See if can find what version this symbol is in. If the
2017 symbol is supposed to be local, then don't actually register
2022 for (t = sinfo->verdefs; t != NULL; t = t->next)
2024 if (t->globals.list != NULL)
2027 while ((d = (*t->match) (&t->globals, d,
2028 h->root.root.string)) != NULL)
2035 /* If the match is a wildcard pattern, keep looking for
2036 a more explicit, perhaps even local, match. */
2045 if (t->locals.list != NULL)
2048 while ((d = (*t->match) (&t->locals, d,
2049 h->root.root.string)) != NULL)
2052 /* If the match is a wildcard pattern, keep looking for
2053 a more explicit, perhaps even global, match. */
2056 /* An exact match overrides a global wildcard. */
2067 if (global_ver != NULL)
2069 h->verinfo.vertree = global_ver;
2070 /* If we already have a versioned symbol that matches the
2071 node for this symbol, then we don't want to create a
2072 duplicate from the unversioned symbol. Instead hide the
2073 unversioned symbol. */
2074 if (exist_ver == global_ver)
2075 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2077 else if (local_ver != NULL)
2079 h->verinfo.vertree = local_ver;
2080 if (!info->export_dynamic
2081 || exist_ver == local_ver)
2082 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2089 /* Read and swap the relocs from the section indicated by SHDR. This
2090 may be either a REL or a RELA section. The relocations are
2091 translated into RELA relocations and stored in INTERNAL_RELOCS,
2092 which should have already been allocated to contain enough space.
2093 The EXTERNAL_RELOCS are a buffer where the external form of the
2094 relocations should be stored.
2096 Returns FALSE if something goes wrong. */
2099 elf_link_read_relocs_from_section (bfd *abfd,
2101 Elf_Internal_Shdr *shdr,
2102 void *external_relocs,
2103 Elf_Internal_Rela *internal_relocs)
2105 const struct elf_backend_data *bed;
2106 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2107 const bfd_byte *erela;
2108 const bfd_byte *erelaend;
2109 Elf_Internal_Rela *irela;
2110 Elf_Internal_Shdr *symtab_hdr;
2113 /* Position ourselves at the start of the section. */
2114 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2117 /* Read the relocations. */
2118 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2121 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2122 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
2124 bed = get_elf_backend_data (abfd);
2126 /* Convert the external relocations to the internal format. */
2127 if (shdr->sh_entsize == bed->s->sizeof_rel)
2128 swap_in = bed->s->swap_reloc_in;
2129 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2130 swap_in = bed->s->swap_reloca_in;
2133 bfd_set_error (bfd_error_wrong_format);
2137 erela = external_relocs;
2138 erelaend = erela + shdr->sh_size;
2139 irela = internal_relocs;
2140 while (erela < erelaend)
2144 (*swap_in) (abfd, erela, irela);
2145 r_symndx = ELF32_R_SYM (irela->r_info);
2146 if (bed->s->arch_size == 64)
2148 if ((size_t) r_symndx >= nsyms)
2150 (*_bfd_error_handler)
2151 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2152 " for offset 0x%lx in section `%A'"),
2154 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2155 bfd_set_error (bfd_error_bad_value);
2158 irela += bed->s->int_rels_per_ext_rel;
2159 erela += shdr->sh_entsize;
2165 /* Read and swap the relocs for a section O. They may have been
2166 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2167 not NULL, they are used as buffers to read into. They are known to
2168 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2169 the return value is allocated using either malloc or bfd_alloc,
2170 according to the KEEP_MEMORY argument. If O has two relocation
2171 sections (both REL and RELA relocations), then the REL_HDR
2172 relocations will appear first in INTERNAL_RELOCS, followed by the
2173 REL_HDR2 relocations. */
2176 _bfd_elf_link_read_relocs (bfd *abfd,
2178 void *external_relocs,
2179 Elf_Internal_Rela *internal_relocs,
2180 bfd_boolean keep_memory)
2182 Elf_Internal_Shdr *rel_hdr;
2183 void *alloc1 = NULL;
2184 Elf_Internal_Rela *alloc2 = NULL;
2185 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2187 if (elf_section_data (o)->relocs != NULL)
2188 return elf_section_data (o)->relocs;
2190 if (o->reloc_count == 0)
2193 rel_hdr = &elf_section_data (o)->rel_hdr;
2195 if (internal_relocs == NULL)
2199 size = o->reloc_count;
2200 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2202 internal_relocs = alloc2 = bfd_alloc (abfd, size);
2204 internal_relocs = alloc2 = bfd_malloc (size);
2205 if (internal_relocs == NULL)
2209 if (external_relocs == NULL)
2211 bfd_size_type size = rel_hdr->sh_size;
2213 if (elf_section_data (o)->rel_hdr2)
2214 size += elf_section_data (o)->rel_hdr2->sh_size;
2215 alloc1 = bfd_malloc (size);
2218 external_relocs = alloc1;
2221 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2225 if (elf_section_data (o)->rel_hdr2
2226 && (!elf_link_read_relocs_from_section
2228 elf_section_data (o)->rel_hdr2,
2229 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2230 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2231 * bed->s->int_rels_per_ext_rel))))
2234 /* Cache the results for next time, if we can. */
2236 elf_section_data (o)->relocs = internal_relocs;
2241 /* Don't free alloc2, since if it was allocated we are passing it
2242 back (under the name of internal_relocs). */
2244 return internal_relocs;
2252 bfd_release (abfd, alloc2);
2259 /* Compute the size of, and allocate space for, REL_HDR which is the
2260 section header for a section containing relocations for O. */
2263 _bfd_elf_link_size_reloc_section (bfd *abfd,
2264 Elf_Internal_Shdr *rel_hdr,
2267 bfd_size_type reloc_count;
2268 bfd_size_type num_rel_hashes;
2270 /* Figure out how many relocations there will be. */
2271 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2272 reloc_count = elf_section_data (o)->rel_count;
2274 reloc_count = elf_section_data (o)->rel_count2;
2276 num_rel_hashes = o->reloc_count;
2277 if (num_rel_hashes < reloc_count)
2278 num_rel_hashes = reloc_count;
2280 /* That allows us to calculate the size of the section. */
2281 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2283 /* The contents field must last into write_object_contents, so we
2284 allocate it with bfd_alloc rather than malloc. Also since we
2285 cannot be sure that the contents will actually be filled in,
2286 we zero the allocated space. */
2287 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2288 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2291 /* We only allocate one set of hash entries, so we only do it the
2292 first time we are called. */
2293 if (elf_section_data (o)->rel_hashes == NULL
2296 struct elf_link_hash_entry **p;
2298 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2302 elf_section_data (o)->rel_hashes = p;
2308 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2309 originated from the section given by INPUT_REL_HDR) to the
2313 _bfd_elf_link_output_relocs (bfd *output_bfd,
2314 asection *input_section,
2315 Elf_Internal_Shdr *input_rel_hdr,
2316 Elf_Internal_Rela *internal_relocs,
2317 struct elf_link_hash_entry **rel_hash
2320 Elf_Internal_Rela *irela;
2321 Elf_Internal_Rela *irelaend;
2323 Elf_Internal_Shdr *output_rel_hdr;
2324 asection *output_section;
2325 unsigned int *rel_countp = NULL;
2326 const struct elf_backend_data *bed;
2327 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2329 output_section = input_section->output_section;
2330 output_rel_hdr = NULL;
2332 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2333 == input_rel_hdr->sh_entsize)
2335 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2336 rel_countp = &elf_section_data (output_section)->rel_count;
2338 else if (elf_section_data (output_section)->rel_hdr2
2339 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2340 == input_rel_hdr->sh_entsize))
2342 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2343 rel_countp = &elf_section_data (output_section)->rel_count2;
2347 (*_bfd_error_handler)
2348 (_("%B: relocation size mismatch in %B section %A"),
2349 output_bfd, input_section->owner, input_section);
2350 bfd_set_error (bfd_error_wrong_format);
2354 bed = get_elf_backend_data (output_bfd);
2355 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2356 swap_out = bed->s->swap_reloc_out;
2357 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2358 swap_out = bed->s->swap_reloca_out;
2362 erel = output_rel_hdr->contents;
2363 erel += *rel_countp * input_rel_hdr->sh_entsize;
2364 irela = internal_relocs;
2365 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2366 * bed->s->int_rels_per_ext_rel);
2367 while (irela < irelaend)
2369 (*swap_out) (output_bfd, irela, erel);
2370 irela += bed->s->int_rels_per_ext_rel;
2371 erel += input_rel_hdr->sh_entsize;
2374 /* Bump the counter, so that we know where to add the next set of
2376 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2381 /* Make weak undefined symbols in PIE dynamic. */
2384 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2385 struct elf_link_hash_entry *h)
2389 && h->root.type == bfd_link_hash_undefweak)
2390 return bfd_elf_link_record_dynamic_symbol (info, h);
2395 /* Fix up the flags for a symbol. This handles various cases which
2396 can only be fixed after all the input files are seen. This is
2397 currently called by both adjust_dynamic_symbol and
2398 assign_sym_version, which is unnecessary but perhaps more robust in
2399 the face of future changes. */
2402 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2403 struct elf_info_failed *eif)
2405 const struct elf_backend_data *bed;
2407 /* If this symbol was mentioned in a non-ELF file, try to set
2408 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2409 permit a non-ELF file to correctly refer to a symbol defined in
2410 an ELF dynamic object. */
2413 while (h->root.type == bfd_link_hash_indirect)
2414 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2416 if (h->root.type != bfd_link_hash_defined
2417 && h->root.type != bfd_link_hash_defweak)
2420 h->ref_regular_nonweak = 1;
2424 if (h->root.u.def.section->owner != NULL
2425 && (bfd_get_flavour (h->root.u.def.section->owner)
2426 == bfd_target_elf_flavour))
2429 h->ref_regular_nonweak = 1;
2435 if (h->dynindx == -1
2439 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2448 /* Unfortunately, NON_ELF is only correct if the symbol
2449 was first seen in a non-ELF file. Fortunately, if the symbol
2450 was first seen in an ELF file, we're probably OK unless the
2451 symbol was defined in a non-ELF file. Catch that case here.
2452 FIXME: We're still in trouble if the symbol was first seen in
2453 a dynamic object, and then later in a non-ELF regular object. */
2454 if ((h->root.type == bfd_link_hash_defined
2455 || h->root.type == bfd_link_hash_defweak)
2457 && (h->root.u.def.section->owner != NULL
2458 ? (bfd_get_flavour (h->root.u.def.section->owner)
2459 != bfd_target_elf_flavour)
2460 : (bfd_is_abs_section (h->root.u.def.section)
2461 && !h->def_dynamic)))
2465 /* Backend specific symbol fixup. */
2466 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2467 if (bed->elf_backend_fixup_symbol
2468 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2471 /* If this is a final link, and the symbol was defined as a common
2472 symbol in a regular object file, and there was no definition in
2473 any dynamic object, then the linker will have allocated space for
2474 the symbol in a common section but the DEF_REGULAR
2475 flag will not have been set. */
2476 if (h->root.type == bfd_link_hash_defined
2480 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2483 /* If -Bsymbolic was used (which means to bind references to global
2484 symbols to the definition within the shared object), and this
2485 symbol was defined in a regular object, then it actually doesn't
2486 need a PLT entry. Likewise, if the symbol has non-default
2487 visibility. If the symbol has hidden or internal visibility, we
2488 will force it local. */
2490 && eif->info->shared
2491 && is_elf_hash_table (eif->info->hash)
2492 && (SYMBOLIC_BIND (eif->info, h)
2493 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2496 bfd_boolean force_local;
2498 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2499 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2500 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2503 /* If a weak undefined symbol has non-default visibility, we also
2504 hide it from the dynamic linker. */
2505 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2506 && h->root.type == bfd_link_hash_undefweak)
2507 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2509 /* If this is a weak defined symbol in a dynamic object, and we know
2510 the real definition in the dynamic object, copy interesting flags
2511 over to the real definition. */
2512 if (h->u.weakdef != NULL)
2514 struct elf_link_hash_entry *weakdef;
2516 weakdef = h->u.weakdef;
2517 if (h->root.type == bfd_link_hash_indirect)
2518 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2520 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2521 || h->root.type == bfd_link_hash_defweak);
2522 BFD_ASSERT (weakdef->def_dynamic);
2524 /* If the real definition is defined by a regular object file,
2525 don't do anything special. See the longer description in
2526 _bfd_elf_adjust_dynamic_symbol, below. */
2527 if (weakdef->def_regular)
2528 h->u.weakdef = NULL;
2531 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2532 || weakdef->root.type == bfd_link_hash_defweak);
2533 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2540 /* Make the backend pick a good value for a dynamic symbol. This is
2541 called via elf_link_hash_traverse, and also calls itself
2545 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2547 struct elf_info_failed *eif = data;
2549 const struct elf_backend_data *bed;
2551 if (! is_elf_hash_table (eif->info->hash))
2554 if (h->root.type == bfd_link_hash_warning)
2556 h->got = elf_hash_table (eif->info)->init_got_offset;
2557 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2559 /* When warning symbols are created, they **replace** the "real"
2560 entry in the hash table, thus we never get to see the real
2561 symbol in a hash traversal. So look at it now. */
2562 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2565 /* Ignore indirect symbols. These are added by the versioning code. */
2566 if (h->root.type == bfd_link_hash_indirect)
2569 /* Fix the symbol flags. */
2570 if (! _bfd_elf_fix_symbol_flags (h, eif))
2573 /* If this symbol does not require a PLT entry, and it is not
2574 defined by a dynamic object, or is not referenced by a regular
2575 object, ignore it. We do have to handle a weak defined symbol,
2576 even if no regular object refers to it, if we decided to add it
2577 to the dynamic symbol table. FIXME: Do we normally need to worry
2578 about symbols which are defined by one dynamic object and
2579 referenced by another one? */
2584 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2586 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2590 /* If we've already adjusted this symbol, don't do it again. This
2591 can happen via a recursive call. */
2592 if (h->dynamic_adjusted)
2595 /* Don't look at this symbol again. Note that we must set this
2596 after checking the above conditions, because we may look at a
2597 symbol once, decide not to do anything, and then get called
2598 recursively later after REF_REGULAR is set below. */
2599 h->dynamic_adjusted = 1;
2601 /* If this is a weak definition, and we know a real definition, and
2602 the real symbol is not itself defined by a regular object file,
2603 then get a good value for the real definition. We handle the
2604 real symbol first, for the convenience of the backend routine.
2606 Note that there is a confusing case here. If the real definition
2607 is defined by a regular object file, we don't get the real symbol
2608 from the dynamic object, but we do get the weak symbol. If the
2609 processor backend uses a COPY reloc, then if some routine in the
2610 dynamic object changes the real symbol, we will not see that
2611 change in the corresponding weak symbol. This is the way other
2612 ELF linkers work as well, and seems to be a result of the shared
2615 I will clarify this issue. Most SVR4 shared libraries define the
2616 variable _timezone and define timezone as a weak synonym. The
2617 tzset call changes _timezone. If you write
2618 extern int timezone;
2620 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2621 you might expect that, since timezone is a synonym for _timezone,
2622 the same number will print both times. However, if the processor
2623 backend uses a COPY reloc, then actually timezone will be copied
2624 into your process image, and, since you define _timezone
2625 yourself, _timezone will not. Thus timezone and _timezone will
2626 wind up at different memory locations. The tzset call will set
2627 _timezone, leaving timezone unchanged. */
2629 if (h->u.weakdef != NULL)
2631 /* If we get to this point, we know there is an implicit
2632 reference by a regular object file via the weak symbol H.
2633 FIXME: Is this really true? What if the traversal finds
2634 H->U.WEAKDEF before it finds H? */
2635 h->u.weakdef->ref_regular = 1;
2637 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2641 /* If a symbol has no type and no size and does not require a PLT
2642 entry, then we are probably about to do the wrong thing here: we
2643 are probably going to create a COPY reloc for an empty object.
2644 This case can arise when a shared object is built with assembly
2645 code, and the assembly code fails to set the symbol type. */
2647 && h->type == STT_NOTYPE
2649 (*_bfd_error_handler)
2650 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2651 h->root.root.string);
2653 dynobj = elf_hash_table (eif->info)->dynobj;
2654 bed = get_elf_backend_data (dynobj);
2656 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2665 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2669 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2672 unsigned int power_of_two;
2674 asection *sec = h->root.u.def.section;
2676 /* The section aligment of definition is the maximum alignment
2677 requirement of symbols defined in the section. Since we don't
2678 know the symbol alignment requirement, we start with the
2679 maximum alignment and check low bits of the symbol address
2680 for the minimum alignment. */
2681 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2682 mask = ((bfd_vma) 1 << power_of_two) - 1;
2683 while ((h->root.u.def.value & mask) != 0)
2689 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2692 /* Adjust the section alignment if needed. */
2693 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2698 /* We make sure that the symbol will be aligned properly. */
2699 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2701 /* Define the symbol as being at this point in DYNBSS. */
2702 h->root.u.def.section = dynbss;
2703 h->root.u.def.value = dynbss->size;
2705 /* Increment the size of DYNBSS to make room for the symbol. */
2706 dynbss->size += h->size;
2711 /* Adjust all external symbols pointing into SEC_MERGE sections
2712 to reflect the object merging within the sections. */
2715 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2719 if (h->root.type == bfd_link_hash_warning)
2720 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2722 if ((h->root.type == bfd_link_hash_defined
2723 || h->root.type == bfd_link_hash_defweak)
2724 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2725 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2727 bfd *output_bfd = data;
2729 h->root.u.def.value =
2730 _bfd_merged_section_offset (output_bfd,
2731 &h->root.u.def.section,
2732 elf_section_data (sec)->sec_info,
2733 h->root.u.def.value);
2739 /* Returns false if the symbol referred to by H should be considered
2740 to resolve local to the current module, and true if it should be
2741 considered to bind dynamically. */
2744 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2745 struct bfd_link_info *info,
2746 bfd_boolean ignore_protected)
2748 bfd_boolean binding_stays_local_p;
2749 const struct elf_backend_data *bed;
2750 struct elf_link_hash_table *hash_table;
2755 while (h->root.type == bfd_link_hash_indirect
2756 || h->root.type == bfd_link_hash_warning)
2757 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2759 /* If it was forced local, then clearly it's not dynamic. */
2760 if (h->dynindx == -1)
2762 if (h->forced_local)
2765 /* Identify the cases where name binding rules say that a
2766 visible symbol resolves locally. */
2767 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2769 switch (ELF_ST_VISIBILITY (h->other))
2776 hash_table = elf_hash_table (info);
2777 if (!is_elf_hash_table (hash_table))
2780 bed = get_elf_backend_data (hash_table->dynobj);
2782 /* Proper resolution for function pointer equality may require
2783 that these symbols perhaps be resolved dynamically, even though
2784 we should be resolving them to the current module. */
2785 if (!ignore_protected || !bed->is_function_type (h->type))
2786 binding_stays_local_p = TRUE;
2793 /* If it isn't defined locally, then clearly it's dynamic. */
2794 if (!h->def_regular)
2797 /* Otherwise, the symbol is dynamic if binding rules don't tell
2798 us that it remains local. */
2799 return !binding_stays_local_p;
2802 /* Return true if the symbol referred to by H should be considered
2803 to resolve local to the current module, and false otherwise. Differs
2804 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2805 undefined symbols and weak symbols. */
2808 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2809 struct bfd_link_info *info,
2810 bfd_boolean local_protected)
2812 const struct elf_backend_data *bed;
2813 struct elf_link_hash_table *hash_table;
2815 /* If it's a local sym, of course we resolve locally. */
2819 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2820 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2821 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2824 /* Common symbols that become definitions don't get the DEF_REGULAR
2825 flag set, so test it first, and don't bail out. */
2826 if (ELF_COMMON_DEF_P (h))
2828 /* If we don't have a definition in a regular file, then we can't
2829 resolve locally. The sym is either undefined or dynamic. */
2830 else if (!h->def_regular)
2833 /* Forced local symbols resolve locally. */
2834 if (h->forced_local)
2837 /* As do non-dynamic symbols. */
2838 if (h->dynindx == -1)
2841 /* At this point, we know the symbol is defined and dynamic. In an
2842 executable it must resolve locally, likewise when building symbolic
2843 shared libraries. */
2844 if (info->executable || SYMBOLIC_BIND (info, h))
2847 /* Now deal with defined dynamic symbols in shared libraries. Ones
2848 with default visibility might not resolve locally. */
2849 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2852 hash_table = elf_hash_table (info);
2853 if (!is_elf_hash_table (hash_table))
2856 bed = get_elf_backend_data (hash_table->dynobj);
2858 /* STV_PROTECTED non-function symbols are local. */
2859 if (!bed->is_function_type (h->type))
2862 /* Function pointer equality tests may require that STV_PROTECTED
2863 symbols be treated as dynamic symbols, even when we know that the
2864 dynamic linker will resolve them locally. */
2865 return local_protected;
2868 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2869 aligned. Returns the first TLS output section. */
2871 struct bfd_section *
2872 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2874 struct bfd_section *sec, *tls;
2875 unsigned int align = 0;
2877 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2878 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2882 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2883 if (sec->alignment_power > align)
2884 align = sec->alignment_power;
2886 elf_hash_table (info)->tls_sec = tls;
2888 /* Ensure the alignment of the first section is the largest alignment,
2889 so that the tls segment starts aligned. */
2891 tls->alignment_power = align;
2896 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2898 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2899 Elf_Internal_Sym *sym)
2901 const struct elf_backend_data *bed;
2903 /* Local symbols do not count, but target specific ones might. */
2904 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2905 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2908 bed = get_elf_backend_data (abfd);
2909 /* Function symbols do not count. */
2910 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2913 /* If the section is undefined, then so is the symbol. */
2914 if (sym->st_shndx == SHN_UNDEF)
2917 /* If the symbol is defined in the common section, then
2918 it is a common definition and so does not count. */
2919 if (bed->common_definition (sym))
2922 /* If the symbol is in a target specific section then we
2923 must rely upon the backend to tell us what it is. */
2924 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2925 /* FIXME - this function is not coded yet:
2927 return _bfd_is_global_symbol_definition (abfd, sym);
2929 Instead for now assume that the definition is not global,
2930 Even if this is wrong, at least the linker will behave
2931 in the same way that it used to do. */
2937 /* Search the symbol table of the archive element of the archive ABFD
2938 whose archive map contains a mention of SYMDEF, and determine if
2939 the symbol is defined in this element. */
2941 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2943 Elf_Internal_Shdr * hdr;
2944 bfd_size_type symcount;
2945 bfd_size_type extsymcount;
2946 bfd_size_type extsymoff;
2947 Elf_Internal_Sym *isymbuf;
2948 Elf_Internal_Sym *isym;
2949 Elf_Internal_Sym *isymend;
2952 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2956 if (! bfd_check_format (abfd, bfd_object))
2959 /* If we have already included the element containing this symbol in the
2960 link then we do not need to include it again. Just claim that any symbol
2961 it contains is not a definition, so that our caller will not decide to
2962 (re)include this element. */
2963 if (abfd->archive_pass)
2966 /* Select the appropriate symbol table. */
2967 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2968 hdr = &elf_tdata (abfd)->symtab_hdr;
2970 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2972 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2974 /* The sh_info field of the symtab header tells us where the
2975 external symbols start. We don't care about the local symbols. */
2976 if (elf_bad_symtab (abfd))
2978 extsymcount = symcount;
2983 extsymcount = symcount - hdr->sh_info;
2984 extsymoff = hdr->sh_info;
2987 if (extsymcount == 0)
2990 /* Read in the symbol table. */
2991 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2993 if (isymbuf == NULL)
2996 /* Scan the symbol table looking for SYMDEF. */
2998 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3002 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3007 if (strcmp (name, symdef->name) == 0)
3009 result = is_global_data_symbol_definition (abfd, isym);
3019 /* Add an entry to the .dynamic table. */
3022 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3026 struct elf_link_hash_table *hash_table;
3027 const struct elf_backend_data *bed;
3029 bfd_size_type newsize;
3030 bfd_byte *newcontents;
3031 Elf_Internal_Dyn dyn;
3033 hash_table = elf_hash_table (info);
3034 if (! is_elf_hash_table (hash_table))
3037 bed = get_elf_backend_data (hash_table->dynobj);
3038 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3039 BFD_ASSERT (s != NULL);
3041 newsize = s->size + bed->s->sizeof_dyn;
3042 newcontents = bfd_realloc (s->contents, newsize);
3043 if (newcontents == NULL)
3047 dyn.d_un.d_val = val;
3048 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3051 s->contents = newcontents;
3056 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3057 otherwise just check whether one already exists. Returns -1 on error,
3058 1 if a DT_NEEDED tag already exists, and 0 on success. */
3061 elf_add_dt_needed_tag (bfd *abfd,
3062 struct bfd_link_info *info,
3066 struct elf_link_hash_table *hash_table;
3067 bfd_size_type oldsize;
3068 bfd_size_type strindex;
3070 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3073 hash_table = elf_hash_table (info);
3074 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3075 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3076 if (strindex == (bfd_size_type) -1)
3079 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3082 const struct elf_backend_data *bed;
3085 bed = get_elf_backend_data (hash_table->dynobj);
3086 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3088 for (extdyn = sdyn->contents;
3089 extdyn < sdyn->contents + sdyn->size;
3090 extdyn += bed->s->sizeof_dyn)
3092 Elf_Internal_Dyn dyn;
3094 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3095 if (dyn.d_tag == DT_NEEDED
3096 && dyn.d_un.d_val == strindex)
3098 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3106 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3109 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3113 /* We were just checking for existence of the tag. */
3114 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3119 /* Sort symbol by value and section. */
3121 elf_sort_symbol (const void *arg1, const void *arg2)
3123 const struct elf_link_hash_entry *h1;
3124 const struct elf_link_hash_entry *h2;
3125 bfd_signed_vma vdiff;
3127 h1 = *(const struct elf_link_hash_entry **) arg1;
3128 h2 = *(const struct elf_link_hash_entry **) arg2;
3129 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3131 return vdiff > 0 ? 1 : -1;
3134 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3136 return sdiff > 0 ? 1 : -1;
3141 /* This function is used to adjust offsets into .dynstr for
3142 dynamic symbols. This is called via elf_link_hash_traverse. */
3145 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3147 struct elf_strtab_hash *dynstr = data;
3149 if (h->root.type == bfd_link_hash_warning)
3150 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3152 if (h->dynindx != -1)
3153 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3157 /* Assign string offsets in .dynstr, update all structures referencing
3161 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3163 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3164 struct elf_link_local_dynamic_entry *entry;
3165 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3166 bfd *dynobj = hash_table->dynobj;
3169 const struct elf_backend_data *bed;
3172 _bfd_elf_strtab_finalize (dynstr);
3173 size = _bfd_elf_strtab_size (dynstr);
3175 bed = get_elf_backend_data (dynobj);
3176 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3177 BFD_ASSERT (sdyn != NULL);
3179 /* Update all .dynamic entries referencing .dynstr strings. */
3180 for (extdyn = sdyn->contents;
3181 extdyn < sdyn->contents + sdyn->size;
3182 extdyn += bed->s->sizeof_dyn)
3184 Elf_Internal_Dyn dyn;
3186 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3190 dyn.d_un.d_val = size;
3198 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3203 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3206 /* Now update local dynamic symbols. */
3207 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3208 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3209 entry->isym.st_name);
3211 /* And the rest of dynamic symbols. */
3212 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3214 /* Adjust version definitions. */
3215 if (elf_tdata (output_bfd)->cverdefs)
3220 Elf_Internal_Verdef def;
3221 Elf_Internal_Verdaux defaux;
3223 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3227 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3229 p += sizeof (Elf_External_Verdef);
3230 if (def.vd_aux != sizeof (Elf_External_Verdef))
3232 for (i = 0; i < def.vd_cnt; ++i)
3234 _bfd_elf_swap_verdaux_in (output_bfd,
3235 (Elf_External_Verdaux *) p, &defaux);
3236 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3238 _bfd_elf_swap_verdaux_out (output_bfd,
3239 &defaux, (Elf_External_Verdaux *) p);
3240 p += sizeof (Elf_External_Verdaux);
3243 while (def.vd_next);
3246 /* Adjust version references. */
3247 if (elf_tdata (output_bfd)->verref)
3252 Elf_Internal_Verneed need;
3253 Elf_Internal_Vernaux needaux;
3255 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3259 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3261 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3262 _bfd_elf_swap_verneed_out (output_bfd, &need,
3263 (Elf_External_Verneed *) p);
3264 p += sizeof (Elf_External_Verneed);
3265 for (i = 0; i < need.vn_cnt; ++i)
3267 _bfd_elf_swap_vernaux_in (output_bfd,
3268 (Elf_External_Vernaux *) p, &needaux);
3269 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3271 _bfd_elf_swap_vernaux_out (output_bfd,
3273 (Elf_External_Vernaux *) p);
3274 p += sizeof (Elf_External_Vernaux);
3277 while (need.vn_next);
3283 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3284 The default is to only match when the INPUT and OUTPUT are exactly
3288 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3289 const bfd_target *output)
3291 return input == output;
3294 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3295 This version is used when different targets for the same architecture
3296 are virtually identical. */
3299 _bfd_elf_relocs_compatible (const bfd_target *input,
3300 const bfd_target *output)
3302 const struct elf_backend_data *obed, *ibed;
3304 if (input == output)
3307 ibed = xvec_get_elf_backend_data (input);
3308 obed = xvec_get_elf_backend_data (output);
3310 if (ibed->arch != obed->arch)
3313 /* If both backends are using this function, deem them compatible. */
3314 return ibed->relocs_compatible == obed->relocs_compatible;
3317 /* Add symbols from an ELF object file to the linker hash table. */
3320 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3322 Elf_Internal_Shdr *hdr;
3323 bfd_size_type symcount;
3324 bfd_size_type extsymcount;
3325 bfd_size_type extsymoff;
3326 struct elf_link_hash_entry **sym_hash;
3327 bfd_boolean dynamic;
3328 Elf_External_Versym *extversym = NULL;
3329 Elf_External_Versym *ever;
3330 struct elf_link_hash_entry *weaks;
3331 struct elf_link_hash_entry **nondeflt_vers = NULL;
3332 bfd_size_type nondeflt_vers_cnt = 0;
3333 Elf_Internal_Sym *isymbuf = NULL;
3334 Elf_Internal_Sym *isym;
3335 Elf_Internal_Sym *isymend;
3336 const struct elf_backend_data *bed;
3337 bfd_boolean add_needed;
3338 struct elf_link_hash_table *htab;
3340 void *alloc_mark = NULL;
3341 struct bfd_hash_entry **old_table = NULL;
3342 unsigned int old_size = 0;
3343 unsigned int old_count = 0;
3344 void *old_tab = NULL;
3347 struct bfd_link_hash_entry *old_undefs = NULL;
3348 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3349 long old_dynsymcount = 0;
3351 size_t hashsize = 0;
3353 htab = elf_hash_table (info);
3354 bed = get_elf_backend_data (abfd);
3356 if ((abfd->flags & DYNAMIC) == 0)
3362 /* You can't use -r against a dynamic object. Also, there's no
3363 hope of using a dynamic object which does not exactly match
3364 the format of the output file. */
3365 if (info->relocatable
3366 || !is_elf_hash_table (htab)
3367 || info->output_bfd->xvec != abfd->xvec)
3369 if (info->relocatable)
3370 bfd_set_error (bfd_error_invalid_operation);
3372 bfd_set_error (bfd_error_wrong_format);
3377 /* As a GNU extension, any input sections which are named
3378 .gnu.warning.SYMBOL are treated as warning symbols for the given
3379 symbol. This differs from .gnu.warning sections, which generate
3380 warnings when they are included in an output file. */
3381 if (info->executable)
3385 for (s = abfd->sections; s != NULL; s = s->next)
3389 name = bfd_get_section_name (abfd, s);
3390 if (CONST_STRNEQ (name, ".gnu.warning."))
3395 name += sizeof ".gnu.warning." - 1;
3397 /* If this is a shared object, then look up the symbol
3398 in the hash table. If it is there, and it is already
3399 been defined, then we will not be using the entry
3400 from this shared object, so we don't need to warn.
3401 FIXME: If we see the definition in a regular object
3402 later on, we will warn, but we shouldn't. The only
3403 fix is to keep track of what warnings we are supposed
3404 to emit, and then handle them all at the end of the
3408 struct elf_link_hash_entry *h;
3410 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3412 /* FIXME: What about bfd_link_hash_common? */
3414 && (h->root.type == bfd_link_hash_defined
3415 || h->root.type == bfd_link_hash_defweak))
3417 /* We don't want to issue this warning. Clobber
3418 the section size so that the warning does not
3419 get copied into the output file. */
3426 msg = bfd_alloc (abfd, sz + 1);
3430 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3435 if (! (_bfd_generic_link_add_one_symbol
3436 (info, abfd, name, BSF_WARNING, s, 0, msg,
3437 FALSE, bed->collect, NULL)))
3440 if (! info->relocatable)
3442 /* Clobber the section size so that the warning does
3443 not get copied into the output file. */
3446 /* Also set SEC_EXCLUDE, so that symbols defined in
3447 the warning section don't get copied to the output. */
3448 s->flags |= SEC_EXCLUDE;
3457 /* If we are creating a shared library, create all the dynamic
3458 sections immediately. We need to attach them to something,
3459 so we attach them to this BFD, provided it is the right
3460 format. FIXME: If there are no input BFD's of the same
3461 format as the output, we can't make a shared library. */
3463 && is_elf_hash_table (htab)
3464 && info->output_bfd->xvec == abfd->xvec
3465 && !htab->dynamic_sections_created)
3467 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3471 else if (!is_elf_hash_table (htab))
3476 const char *soname = NULL;
3477 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3480 /* ld --just-symbols and dynamic objects don't mix very well.
3481 ld shouldn't allow it. */
3482 if ((s = abfd->sections) != NULL
3483 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3486 /* If this dynamic lib was specified on the command line with
3487 --as-needed in effect, then we don't want to add a DT_NEEDED
3488 tag unless the lib is actually used. Similary for libs brought
3489 in by another lib's DT_NEEDED. When --no-add-needed is used
3490 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3491 any dynamic library in DT_NEEDED tags in the dynamic lib at
3493 add_needed = (elf_dyn_lib_class (abfd)
3494 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3495 | DYN_NO_NEEDED)) == 0;
3497 s = bfd_get_section_by_name (abfd, ".dynamic");
3502 unsigned int elfsec;
3503 unsigned long shlink;
3505 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3506 goto error_free_dyn;
3508 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3509 if (elfsec == SHN_BAD)
3510 goto error_free_dyn;
3511 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3513 for (extdyn = dynbuf;
3514 extdyn < dynbuf + s->size;
3515 extdyn += bed->s->sizeof_dyn)
3517 Elf_Internal_Dyn dyn;
3519 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3520 if (dyn.d_tag == DT_SONAME)
3522 unsigned int tagv = dyn.d_un.d_val;
3523 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3525 goto error_free_dyn;
3527 if (dyn.d_tag == DT_NEEDED)
3529 struct bfd_link_needed_list *n, **pn;
3531 unsigned int tagv = dyn.d_un.d_val;
3533 amt = sizeof (struct bfd_link_needed_list);
3534 n = bfd_alloc (abfd, amt);
3535 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3536 if (n == NULL || fnm == NULL)
3537 goto error_free_dyn;
3538 amt = strlen (fnm) + 1;
3539 anm = bfd_alloc (abfd, amt);
3541 goto error_free_dyn;
3542 memcpy (anm, fnm, amt);
3546 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3550 if (dyn.d_tag == DT_RUNPATH)
3552 struct bfd_link_needed_list *n, **pn;
3554 unsigned int tagv = dyn.d_un.d_val;
3556 amt = sizeof (struct bfd_link_needed_list);
3557 n = bfd_alloc (abfd, amt);
3558 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3559 if (n == NULL || fnm == NULL)
3560 goto error_free_dyn;
3561 amt = strlen (fnm) + 1;
3562 anm = bfd_alloc (abfd, amt);
3564 goto error_free_dyn;
3565 memcpy (anm, fnm, amt);
3569 for (pn = & runpath;
3575 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3576 if (!runpath && dyn.d_tag == DT_RPATH)
3578 struct bfd_link_needed_list *n, **pn;
3580 unsigned int tagv = dyn.d_un.d_val;
3582 amt = sizeof (struct bfd_link_needed_list);
3583 n = bfd_alloc (abfd, amt);
3584 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3585 if (n == NULL || fnm == NULL)
3586 goto error_free_dyn;
3587 amt = strlen (fnm) + 1;
3588 anm = bfd_alloc (abfd, amt);
3595 memcpy (anm, fnm, amt);
3610 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3611 frees all more recently bfd_alloc'd blocks as well. */
3617 struct bfd_link_needed_list **pn;
3618 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3623 /* We do not want to include any of the sections in a dynamic
3624 object in the output file. We hack by simply clobbering the
3625 list of sections in the BFD. This could be handled more
3626 cleanly by, say, a new section flag; the existing
3627 SEC_NEVER_LOAD flag is not the one we want, because that one
3628 still implies that the section takes up space in the output
3630 bfd_section_list_clear (abfd);
3632 /* Find the name to use in a DT_NEEDED entry that refers to this
3633 object. If the object has a DT_SONAME entry, we use it.
3634 Otherwise, if the generic linker stuck something in
3635 elf_dt_name, we use that. Otherwise, we just use the file
3637 if (soname == NULL || *soname == '\0')
3639 soname = elf_dt_name (abfd);
3640 if (soname == NULL || *soname == '\0')
3641 soname = bfd_get_filename (abfd);
3644 /* Save the SONAME because sometimes the linker emulation code
3645 will need to know it. */
3646 elf_dt_name (abfd) = soname;
3648 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3652 /* If we have already included this dynamic object in the
3653 link, just ignore it. There is no reason to include a
3654 particular dynamic object more than once. */
3659 /* If this is a dynamic object, we always link against the .dynsym
3660 symbol table, not the .symtab symbol table. The dynamic linker
3661 will only see the .dynsym symbol table, so there is no reason to
3662 look at .symtab for a dynamic object. */
3664 if (! dynamic || elf_dynsymtab (abfd) == 0)
3665 hdr = &elf_tdata (abfd)->symtab_hdr;
3667 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3669 symcount = hdr->sh_size / bed->s->sizeof_sym;
3671 /* The sh_info field of the symtab header tells us where the
3672 external symbols start. We don't care about the local symbols at
3674 if (elf_bad_symtab (abfd))
3676 extsymcount = symcount;
3681 extsymcount = symcount - hdr->sh_info;
3682 extsymoff = hdr->sh_info;
3686 if (extsymcount != 0)
3688 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3690 if (isymbuf == NULL)
3693 /* We store a pointer to the hash table entry for each external
3695 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3696 sym_hash = bfd_alloc (abfd, amt);
3697 if (sym_hash == NULL)
3698 goto error_free_sym;
3699 elf_sym_hashes (abfd) = sym_hash;
3704 /* Read in any version definitions. */
3705 if (!_bfd_elf_slurp_version_tables (abfd,
3706 info->default_imported_symver))
3707 goto error_free_sym;
3709 /* Read in the symbol versions, but don't bother to convert them
3710 to internal format. */
3711 if (elf_dynversym (abfd) != 0)
3713 Elf_Internal_Shdr *versymhdr;
3715 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3716 extversym = bfd_malloc (versymhdr->sh_size);
3717 if (extversym == NULL)
3718 goto error_free_sym;
3719 amt = versymhdr->sh_size;
3720 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3721 || bfd_bread (extversym, amt, abfd) != amt)
3722 goto error_free_vers;
3726 /* If we are loading an as-needed shared lib, save the symbol table
3727 state before we start adding symbols. If the lib turns out
3728 to be unneeded, restore the state. */
3729 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3734 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3736 struct bfd_hash_entry *p;
3737 struct elf_link_hash_entry *h;
3739 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3741 h = (struct elf_link_hash_entry *) p;
3742 entsize += htab->root.table.entsize;
3743 if (h->root.type == bfd_link_hash_warning)
3744 entsize += htab->root.table.entsize;
3748 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3749 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3750 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3751 if (old_tab == NULL)
3752 goto error_free_vers;
3754 /* Remember the current objalloc pointer, so that all mem for
3755 symbols added can later be reclaimed. */
3756 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3757 if (alloc_mark == NULL)
3758 goto error_free_vers;
3760 /* Make a special call to the linker "notice" function to
3761 tell it that we are about to handle an as-needed lib. */
3762 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3764 goto error_free_vers;
3766 /* Clone the symbol table and sym hashes. Remember some
3767 pointers into the symbol table, and dynamic symbol count. */
3768 old_hash = (char *) old_tab + tabsize;
3769 old_ent = (char *) old_hash + hashsize;
3770 memcpy (old_tab, htab->root.table.table, tabsize);
3771 memcpy (old_hash, sym_hash, hashsize);
3772 old_undefs = htab->root.undefs;
3773 old_undefs_tail = htab->root.undefs_tail;
3774 old_table = htab->root.table.table;
3775 old_size = htab->root.table.size;
3776 old_count = htab->root.table.count;
3777 old_dynsymcount = htab->dynsymcount;
3779 for (i = 0; i < htab->root.table.size; i++)
3781 struct bfd_hash_entry *p;
3782 struct elf_link_hash_entry *h;
3784 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3786 memcpy (old_ent, p, htab->root.table.entsize);
3787 old_ent = (char *) old_ent + htab->root.table.entsize;
3788 h = (struct elf_link_hash_entry *) p;
3789 if (h->root.type == bfd_link_hash_warning)
3791 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3792 old_ent = (char *) old_ent + htab->root.table.entsize;
3799 ever = extversym != NULL ? extversym + extsymoff : NULL;
3800 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3802 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3806 asection *sec, *new_sec;
3809 struct elf_link_hash_entry *h;
3810 bfd_boolean definition;
3811 bfd_boolean size_change_ok;
3812 bfd_boolean type_change_ok;
3813 bfd_boolean new_weakdef;
3814 bfd_boolean override;
3816 unsigned int old_alignment;
3821 flags = BSF_NO_FLAGS;
3823 value = isym->st_value;
3825 common = bed->common_definition (isym);
3827 bind = ELF_ST_BIND (isym->st_info);
3828 if (bind == STB_LOCAL)
3830 /* This should be impossible, since ELF requires that all
3831 global symbols follow all local symbols, and that sh_info
3832 point to the first global symbol. Unfortunately, Irix 5
3836 else if (bind == STB_GLOBAL)
3838 if (isym->st_shndx != SHN_UNDEF && !common)
3841 else if (bind == STB_WEAK)
3845 /* Leave it up to the processor backend. */
3848 if (isym->st_shndx == SHN_UNDEF)
3849 sec = bfd_und_section_ptr;
3850 else if (isym->st_shndx == SHN_ABS)
3851 sec = bfd_abs_section_ptr;
3852 else if (isym->st_shndx == SHN_COMMON)
3854 sec = bfd_com_section_ptr;
3855 /* What ELF calls the size we call the value. What ELF
3856 calls the value we call the alignment. */
3857 value = isym->st_size;
3861 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3863 sec = bfd_abs_section_ptr;
3864 else if (sec->kept_section)
3866 /* Symbols from discarded section are undefined. We keep
3868 sec = bfd_und_section_ptr;
3869 isym->st_shndx = SHN_UNDEF;
3871 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3875 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3878 goto error_free_vers;
3880 if (isym->st_shndx == SHN_COMMON
3881 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3882 && !info->relocatable)
3884 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3888 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3891 | SEC_LINKER_CREATED
3892 | SEC_THREAD_LOCAL));
3894 goto error_free_vers;
3898 else if (bed->elf_add_symbol_hook)
3900 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3902 goto error_free_vers;
3904 /* The hook function sets the name to NULL if this symbol
3905 should be skipped for some reason. */
3910 /* Sanity check that all possibilities were handled. */
3913 bfd_set_error (bfd_error_bad_value);
3914 goto error_free_vers;
3917 if (bfd_is_und_section (sec)
3918 || bfd_is_com_section (sec))
3923 size_change_ok = FALSE;
3924 type_change_ok = bed->type_change_ok;
3929 if (is_elf_hash_table (htab))
3931 Elf_Internal_Versym iver;
3932 unsigned int vernum = 0;
3937 if (info->default_imported_symver)
3938 /* Use the default symbol version created earlier. */
3939 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3944 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3946 vernum = iver.vs_vers & VERSYM_VERSION;
3948 /* If this is a hidden symbol, or if it is not version
3949 1, we append the version name to the symbol name.
3950 However, we do not modify a non-hidden absolute symbol
3951 if it is not a function, because it might be the version
3952 symbol itself. FIXME: What if it isn't? */
3953 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3955 && (!bfd_is_abs_section (sec)
3956 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
3959 size_t namelen, verlen, newlen;
3962 if (isym->st_shndx != SHN_UNDEF)
3964 if (vernum > elf_tdata (abfd)->cverdefs)
3966 else if (vernum > 1)
3968 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3974 (*_bfd_error_handler)
3975 (_("%B: %s: invalid version %u (max %d)"),
3977 elf_tdata (abfd)->cverdefs);
3978 bfd_set_error (bfd_error_bad_value);
3979 goto error_free_vers;
3984 /* We cannot simply test for the number of
3985 entries in the VERNEED section since the
3986 numbers for the needed versions do not start
3988 Elf_Internal_Verneed *t;
3991 for (t = elf_tdata (abfd)->verref;
3995 Elf_Internal_Vernaux *a;
3997 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3999 if (a->vna_other == vernum)
4001 verstr = a->vna_nodename;
4010 (*_bfd_error_handler)
4011 (_("%B: %s: invalid needed version %d"),
4012 abfd, name, vernum);
4013 bfd_set_error (bfd_error_bad_value);
4014 goto error_free_vers;
4018 namelen = strlen (name);
4019 verlen = strlen (verstr);
4020 newlen = namelen + verlen + 2;
4021 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4022 && isym->st_shndx != SHN_UNDEF)
4025 newname = bfd_hash_allocate (&htab->root.table, newlen);
4026 if (newname == NULL)
4027 goto error_free_vers;
4028 memcpy (newname, name, namelen);
4029 p = newname + namelen;
4031 /* If this is a defined non-hidden version symbol,
4032 we add another @ to the name. This indicates the
4033 default version of the symbol. */
4034 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4035 && isym->st_shndx != SHN_UNDEF)
4037 memcpy (p, verstr, verlen + 1);
4042 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4043 &value, &old_alignment,
4044 sym_hash, &skip, &override,
4045 &type_change_ok, &size_change_ok))
4046 goto error_free_vers;
4055 while (h->root.type == bfd_link_hash_indirect
4056 || h->root.type == bfd_link_hash_warning)
4057 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4059 /* Remember the old alignment if this is a common symbol, so
4060 that we don't reduce the alignment later on. We can't
4061 check later, because _bfd_generic_link_add_one_symbol
4062 will set a default for the alignment which we want to
4063 override. We also remember the old bfd where the existing
4064 definition comes from. */
4065 switch (h->root.type)
4070 case bfd_link_hash_defined:
4071 case bfd_link_hash_defweak:
4072 old_bfd = h->root.u.def.section->owner;
4075 case bfd_link_hash_common:
4076 old_bfd = h->root.u.c.p->section->owner;
4077 old_alignment = h->root.u.c.p->alignment_power;
4081 if (elf_tdata (abfd)->verdef != NULL
4085 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4088 if (! (_bfd_generic_link_add_one_symbol
4089 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4090 (struct bfd_link_hash_entry **) sym_hash)))
4091 goto error_free_vers;
4094 while (h->root.type == bfd_link_hash_indirect
4095 || h->root.type == bfd_link_hash_warning)
4096 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4099 new_weakdef = FALSE;
4102 && (flags & BSF_WEAK) != 0
4103 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4104 && is_elf_hash_table (htab)
4105 && h->u.weakdef == NULL)
4107 /* Keep a list of all weak defined non function symbols from
4108 a dynamic object, using the weakdef field. Later in this
4109 function we will set the weakdef field to the correct
4110 value. We only put non-function symbols from dynamic
4111 objects on this list, because that happens to be the only
4112 time we need to know the normal symbol corresponding to a
4113 weak symbol, and the information is time consuming to
4114 figure out. If the weakdef field is not already NULL,
4115 then this symbol was already defined by some previous
4116 dynamic object, and we will be using that previous
4117 definition anyhow. */
4119 h->u.weakdef = weaks;
4124 /* Set the alignment of a common symbol. */
4125 if ((common || bfd_is_com_section (sec))
4126 && h->root.type == bfd_link_hash_common)
4131 align = bfd_log2 (isym->st_value);
4134 /* The new symbol is a common symbol in a shared object.
4135 We need to get the alignment from the section. */
4136 align = new_sec->alignment_power;
4138 if (align > old_alignment
4139 /* Permit an alignment power of zero if an alignment of one
4140 is specified and no other alignments have been specified. */
4141 || (isym->st_value == 1 && old_alignment == 0))
4142 h->root.u.c.p->alignment_power = align;
4144 h->root.u.c.p->alignment_power = old_alignment;
4147 if (is_elf_hash_table (htab))
4151 /* Check the alignment when a common symbol is involved. This
4152 can change when a common symbol is overridden by a normal
4153 definition or a common symbol is ignored due to the old
4154 normal definition. We need to make sure the maximum
4155 alignment is maintained. */
4156 if ((old_alignment || common)
4157 && h->root.type != bfd_link_hash_common)
4159 unsigned int common_align;
4160 unsigned int normal_align;
4161 unsigned int symbol_align;
4165 symbol_align = ffs (h->root.u.def.value) - 1;
4166 if (h->root.u.def.section->owner != NULL
4167 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4169 normal_align = h->root.u.def.section->alignment_power;
4170 if (normal_align > symbol_align)
4171 normal_align = symbol_align;
4174 normal_align = symbol_align;
4178 common_align = old_alignment;
4179 common_bfd = old_bfd;
4184 common_align = bfd_log2 (isym->st_value);
4186 normal_bfd = old_bfd;
4189 if (normal_align < common_align)
4191 /* PR binutils/2735 */
4192 if (normal_bfd == NULL)
4193 (*_bfd_error_handler)
4194 (_("Warning: alignment %u of common symbol `%s' in %B"
4195 " is greater than the alignment (%u) of its section %A"),
4196 common_bfd, h->root.u.def.section,
4197 1 << common_align, name, 1 << normal_align);
4199 (*_bfd_error_handler)
4200 (_("Warning: alignment %u of symbol `%s' in %B"
4201 " is smaller than %u in %B"),
4202 normal_bfd, common_bfd,
4203 1 << normal_align, name, 1 << common_align);
4207 /* Remember the symbol size if it isn't undefined. */
4208 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4209 && (definition || h->size == 0))
4212 && h->size != isym->st_size
4213 && ! size_change_ok)
4214 (*_bfd_error_handler)
4215 (_("Warning: size of symbol `%s' changed"
4216 " from %lu in %B to %lu in %B"),
4218 name, (unsigned long) h->size,
4219 (unsigned long) isym->st_size);
4221 h->size = isym->st_size;
4224 /* If this is a common symbol, then we always want H->SIZE
4225 to be the size of the common symbol. The code just above
4226 won't fix the size if a common symbol becomes larger. We
4227 don't warn about a size change here, because that is
4228 covered by --warn-common. Allow changed between different
4230 if (h->root.type == bfd_link_hash_common)
4231 h->size = h->root.u.c.size;
4233 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4234 && (definition || h->type == STT_NOTYPE))
4236 if (h->type != STT_NOTYPE
4237 && h->type != ELF_ST_TYPE (isym->st_info)
4238 && ! type_change_ok)
4239 (*_bfd_error_handler)
4240 (_("Warning: type of symbol `%s' changed"
4241 " from %d to %d in %B"),
4242 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
4244 h->type = ELF_ST_TYPE (isym->st_info);
4247 /* If st_other has a processor-specific meaning, specific
4248 code might be needed here. We never merge the visibility
4249 attribute with the one from a dynamic object. */
4250 if (bed->elf_backend_merge_symbol_attribute)
4251 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
4254 /* If this symbol has default visibility and the user has requested
4255 we not re-export it, then mark it as hidden. */
4256 if (definition && !dynamic
4258 || (abfd->my_archive && abfd->my_archive->no_export))
4259 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4260 isym->st_other = (STV_HIDDEN
4261 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4263 if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic)
4265 unsigned char hvis, symvis, other, nvis;
4267 /* Only merge the visibility. Leave the remainder of the
4268 st_other field to elf_backend_merge_symbol_attribute. */
4269 other = h->other & ~ELF_ST_VISIBILITY (-1);
4271 /* Combine visibilities, using the most constraining one. */
4272 hvis = ELF_ST_VISIBILITY (h->other);
4273 symvis = ELF_ST_VISIBILITY (isym->st_other);
4279 nvis = hvis < symvis ? hvis : symvis;
4281 h->other = other | nvis;
4284 /* Set a flag in the hash table entry indicating the type of
4285 reference or definition we just found. Keep a count of
4286 the number of dynamic symbols we find. A dynamic symbol
4287 is one which is referenced or defined by both a regular
4288 object and a shared object. */
4295 if (bind != STB_WEAK)
4296 h->ref_regular_nonweak = 1;
4308 if (! info->executable
4321 || (h->u.weakdef != NULL
4323 && h->u.weakdef->dynindx != -1))
4327 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
4329 /* We don't want to make debug symbol dynamic. */
4330 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4334 /* Check to see if we need to add an indirect symbol for
4335 the default name. */
4336 if (definition || h->root.type == bfd_link_hash_common)
4337 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4338 &sec, &value, &dynsym,
4340 goto error_free_vers;
4342 if (definition && !dynamic)
4344 char *p = strchr (name, ELF_VER_CHR);
4345 if (p != NULL && p[1] != ELF_VER_CHR)
4347 /* Queue non-default versions so that .symver x, x@FOO
4348 aliases can be checked. */
4351 amt = ((isymend - isym + 1)
4352 * sizeof (struct elf_link_hash_entry *));
4353 nondeflt_vers = bfd_malloc (amt);
4355 goto error_free_vers;
4357 nondeflt_vers[nondeflt_vers_cnt++] = h;
4361 if (dynsym && h->dynindx == -1)
4363 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4364 goto error_free_vers;
4365 if (h->u.weakdef != NULL
4367 && h->u.weakdef->dynindx == -1)
4369 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4370 goto error_free_vers;
4373 else if (dynsym && h->dynindx != -1)
4374 /* If the symbol already has a dynamic index, but
4375 visibility says it should not be visible, turn it into
4377 switch (ELF_ST_VISIBILITY (h->other))
4381 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4392 const char *soname = elf_dt_name (abfd);
4394 /* A symbol from a library loaded via DT_NEEDED of some
4395 other library is referenced by a regular object.
4396 Add a DT_NEEDED entry for it. Issue an error if
4397 --no-add-needed is used. */
4398 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4400 (*_bfd_error_handler)
4401 (_("%s: invalid DSO for symbol `%s' definition"),
4403 bfd_set_error (bfd_error_bad_value);
4404 goto error_free_vers;
4407 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4410 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4412 goto error_free_vers;
4414 BFD_ASSERT (ret == 0);
4419 if (extversym != NULL)
4425 if (isymbuf != NULL)
4431 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4435 /* Restore the symbol table. */
4436 if (bed->as_needed_cleanup)
4437 (*bed->as_needed_cleanup) (abfd, info);
4438 old_hash = (char *) old_tab + tabsize;
4439 old_ent = (char *) old_hash + hashsize;
4440 sym_hash = elf_sym_hashes (abfd);
4441 htab->root.table.table = old_table;
4442 htab->root.table.size = old_size;
4443 htab->root.table.count = old_count;
4444 memcpy (htab->root.table.table, old_tab, tabsize);
4445 memcpy (sym_hash, old_hash, hashsize);
4446 htab->root.undefs = old_undefs;
4447 htab->root.undefs_tail = old_undefs_tail;
4448 for (i = 0; i < htab->root.table.size; i++)
4450 struct bfd_hash_entry *p;
4451 struct elf_link_hash_entry *h;
4453 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4455 h = (struct elf_link_hash_entry *) p;
4456 if (h->root.type == bfd_link_hash_warning)
4457 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4458 if (h->dynindx >= old_dynsymcount)
4459 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4461 memcpy (p, old_ent, htab->root.table.entsize);
4462 old_ent = (char *) old_ent + htab->root.table.entsize;
4463 h = (struct elf_link_hash_entry *) p;
4464 if (h->root.type == bfd_link_hash_warning)
4466 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4467 old_ent = (char *) old_ent + htab->root.table.entsize;
4472 /* Make a special call to the linker "notice" function to
4473 tell it that symbols added for crefs may need to be removed. */
4474 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4476 goto error_free_vers;
4479 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4481 if (nondeflt_vers != NULL)
4482 free (nondeflt_vers);
4486 if (old_tab != NULL)
4488 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4490 goto error_free_vers;
4495 /* Now that all the symbols from this input file are created, handle
4496 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4497 if (nondeflt_vers != NULL)
4499 bfd_size_type cnt, symidx;
4501 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4503 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4504 char *shortname, *p;
4506 p = strchr (h->root.root.string, ELF_VER_CHR);
4508 || (h->root.type != bfd_link_hash_defined
4509 && h->root.type != bfd_link_hash_defweak))
4512 amt = p - h->root.root.string;
4513 shortname = bfd_malloc (amt + 1);
4515 goto error_free_vers;
4516 memcpy (shortname, h->root.root.string, amt);
4517 shortname[amt] = '\0';
4519 hi = (struct elf_link_hash_entry *)
4520 bfd_link_hash_lookup (&htab->root, shortname,
4521 FALSE, FALSE, FALSE);
4523 && hi->root.type == h->root.type
4524 && hi->root.u.def.value == h->root.u.def.value
4525 && hi->root.u.def.section == h->root.u.def.section)
4527 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4528 hi->root.type = bfd_link_hash_indirect;
4529 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4530 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4531 sym_hash = elf_sym_hashes (abfd);
4533 for (symidx = 0; symidx < extsymcount; ++symidx)
4534 if (sym_hash[symidx] == hi)
4536 sym_hash[symidx] = h;
4542 free (nondeflt_vers);
4543 nondeflt_vers = NULL;
4546 /* Now set the weakdefs field correctly for all the weak defined
4547 symbols we found. The only way to do this is to search all the
4548 symbols. Since we only need the information for non functions in
4549 dynamic objects, that's the only time we actually put anything on
4550 the list WEAKS. We need this information so that if a regular
4551 object refers to a symbol defined weakly in a dynamic object, the
4552 real symbol in the dynamic object is also put in the dynamic
4553 symbols; we also must arrange for both symbols to point to the
4554 same memory location. We could handle the general case of symbol
4555 aliasing, but a general symbol alias can only be generated in
4556 assembler code, handling it correctly would be very time
4557 consuming, and other ELF linkers don't handle general aliasing
4561 struct elf_link_hash_entry **hpp;
4562 struct elf_link_hash_entry **hppend;
4563 struct elf_link_hash_entry **sorted_sym_hash;
4564 struct elf_link_hash_entry *h;
4567 /* Since we have to search the whole symbol list for each weak
4568 defined symbol, search time for N weak defined symbols will be
4569 O(N^2). Binary search will cut it down to O(NlogN). */
4570 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4571 sorted_sym_hash = bfd_malloc (amt);
4572 if (sorted_sym_hash == NULL)
4574 sym_hash = sorted_sym_hash;
4575 hpp = elf_sym_hashes (abfd);
4576 hppend = hpp + extsymcount;
4578 for (; hpp < hppend; hpp++)
4582 && h->root.type == bfd_link_hash_defined
4583 && !bed->is_function_type (h->type))
4591 qsort (sorted_sym_hash, sym_count,
4592 sizeof (struct elf_link_hash_entry *),
4595 while (weaks != NULL)
4597 struct elf_link_hash_entry *hlook;
4604 weaks = hlook->u.weakdef;
4605 hlook->u.weakdef = NULL;
4607 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4608 || hlook->root.type == bfd_link_hash_defweak
4609 || hlook->root.type == bfd_link_hash_common
4610 || hlook->root.type == bfd_link_hash_indirect);
4611 slook = hlook->root.u.def.section;
4612 vlook = hlook->root.u.def.value;
4619 bfd_signed_vma vdiff;
4621 h = sorted_sym_hash [idx];
4622 vdiff = vlook - h->root.u.def.value;
4629 long sdiff = slook->id - h->root.u.def.section->id;
4642 /* We didn't find a value/section match. */
4646 for (i = ilook; i < sym_count; i++)
4648 h = sorted_sym_hash [i];
4650 /* Stop if value or section doesn't match. */
4651 if (h->root.u.def.value != vlook
4652 || h->root.u.def.section != slook)
4654 else if (h != hlook)
4656 hlook->u.weakdef = h;
4658 /* If the weak definition is in the list of dynamic
4659 symbols, make sure the real definition is put
4661 if (hlook->dynindx != -1 && h->dynindx == -1)
4663 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4666 free (sorted_sym_hash);
4671 /* If the real definition is in the list of dynamic
4672 symbols, make sure the weak definition is put
4673 there as well. If we don't do this, then the
4674 dynamic loader might not merge the entries for the
4675 real definition and the weak definition. */
4676 if (h->dynindx != -1 && hlook->dynindx == -1)
4678 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4679 goto err_free_sym_hash;
4686 free (sorted_sym_hash);
4689 if (bed->check_directives
4690 && !(*bed->check_directives) (abfd, info))
4693 /* If this object is the same format as the output object, and it is
4694 not a shared library, then let the backend look through the
4697 This is required to build global offset table entries and to
4698 arrange for dynamic relocs. It is not required for the
4699 particular common case of linking non PIC code, even when linking
4700 against shared libraries, but unfortunately there is no way of
4701 knowing whether an object file has been compiled PIC or not.
4702 Looking through the relocs is not particularly time consuming.
4703 The problem is that we must either (1) keep the relocs in memory,
4704 which causes the linker to require additional runtime memory or
4705 (2) read the relocs twice from the input file, which wastes time.
4706 This would be a good case for using mmap.
4708 I have no idea how to handle linking PIC code into a file of a
4709 different format. It probably can't be done. */
4711 && is_elf_hash_table (htab)
4712 && bed->check_relocs != NULL
4713 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4717 for (o = abfd->sections; o != NULL; o = o->next)
4719 Elf_Internal_Rela *internal_relocs;
4722 if ((o->flags & SEC_RELOC) == 0
4723 || o->reloc_count == 0
4724 || ((info->strip == strip_all || info->strip == strip_debugger)
4725 && (o->flags & SEC_DEBUGGING) != 0)
4726 || bfd_is_abs_section (o->output_section))
4729 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4731 if (internal_relocs == NULL)
4734 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4736 if (elf_section_data (o)->relocs != internal_relocs)
4737 free (internal_relocs);
4744 /* If this is a non-traditional link, try to optimize the handling
4745 of the .stab/.stabstr sections. */
4747 && ! info->traditional_format
4748 && is_elf_hash_table (htab)
4749 && (info->strip != strip_all && info->strip != strip_debugger))
4753 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4754 if (stabstr != NULL)
4756 bfd_size_type string_offset = 0;
4759 for (stab = abfd->sections; stab; stab = stab->next)
4760 if (CONST_STRNEQ (stab->name, ".stab")
4761 && (!stab->name[5] ||
4762 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4763 && (stab->flags & SEC_MERGE) == 0
4764 && !bfd_is_abs_section (stab->output_section))
4766 struct bfd_elf_section_data *secdata;
4768 secdata = elf_section_data (stab);
4769 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4770 stabstr, &secdata->sec_info,
4773 if (secdata->sec_info)
4774 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4779 if (is_elf_hash_table (htab) && add_needed)
4781 /* Add this bfd to the loaded list. */
4782 struct elf_link_loaded_list *n;
4784 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4788 n->next = htab->loaded;
4795 if (old_tab != NULL)
4797 if (nondeflt_vers != NULL)
4798 free (nondeflt_vers);
4799 if (extversym != NULL)
4802 if (isymbuf != NULL)
4808 /* Return the linker hash table entry of a symbol that might be
4809 satisfied by an archive symbol. Return -1 on error. */
4811 struct elf_link_hash_entry *
4812 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4813 struct bfd_link_info *info,
4816 struct elf_link_hash_entry *h;
4820 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4824 /* If this is a default version (the name contains @@), look up the
4825 symbol again with only one `@' as well as without the version.
4826 The effect is that references to the symbol with and without the
4827 version will be matched by the default symbol in the archive. */
4829 p = strchr (name, ELF_VER_CHR);
4830 if (p == NULL || p[1] != ELF_VER_CHR)
4833 /* First check with only one `@'. */
4834 len = strlen (name);
4835 copy = bfd_alloc (abfd, len);
4837 return (struct elf_link_hash_entry *) 0 - 1;
4839 first = p - name + 1;
4840 memcpy (copy, name, first);
4841 memcpy (copy + first, name + first + 1, len - first);
4843 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4846 /* We also need to check references to the symbol without the
4848 copy[first - 1] = '\0';
4849 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4850 FALSE, FALSE, FALSE);
4853 bfd_release (abfd, copy);
4857 /* Add symbols from an ELF archive file to the linker hash table. We
4858 don't use _bfd_generic_link_add_archive_symbols because of a
4859 problem which arises on UnixWare. The UnixWare libc.so is an
4860 archive which includes an entry libc.so.1 which defines a bunch of
4861 symbols. The libc.so archive also includes a number of other
4862 object files, which also define symbols, some of which are the same
4863 as those defined in libc.so.1. Correct linking requires that we
4864 consider each object file in turn, and include it if it defines any
4865 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4866 this; it looks through the list of undefined symbols, and includes
4867 any object file which defines them. When this algorithm is used on
4868 UnixWare, it winds up pulling in libc.so.1 early and defining a
4869 bunch of symbols. This means that some of the other objects in the
4870 archive are not included in the link, which is incorrect since they
4871 precede libc.so.1 in the archive.
4873 Fortunately, ELF archive handling is simpler than that done by
4874 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4875 oddities. In ELF, if we find a symbol in the archive map, and the
4876 symbol is currently undefined, we know that we must pull in that
4879 Unfortunately, we do have to make multiple passes over the symbol
4880 table until nothing further is resolved. */
4883 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4886 bfd_boolean *defined = NULL;
4887 bfd_boolean *included = NULL;
4891 const struct elf_backend_data *bed;
4892 struct elf_link_hash_entry * (*archive_symbol_lookup)
4893 (bfd *, struct bfd_link_info *, const char *);
4895 if (! bfd_has_map (abfd))
4897 /* An empty archive is a special case. */
4898 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4900 bfd_set_error (bfd_error_no_armap);
4904 /* Keep track of all symbols we know to be already defined, and all
4905 files we know to be already included. This is to speed up the
4906 second and subsequent passes. */
4907 c = bfd_ardata (abfd)->symdef_count;
4911 amt *= sizeof (bfd_boolean);
4912 defined = bfd_zmalloc (amt);
4913 included = bfd_zmalloc (amt);
4914 if (defined == NULL || included == NULL)
4917 symdefs = bfd_ardata (abfd)->symdefs;
4918 bed = get_elf_backend_data (abfd);
4919 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4932 symdefend = symdef + c;
4933 for (i = 0; symdef < symdefend; symdef++, i++)
4935 struct elf_link_hash_entry *h;
4937 struct bfd_link_hash_entry *undefs_tail;
4940 if (defined[i] || included[i])
4942 if (symdef->file_offset == last)
4948 h = archive_symbol_lookup (abfd, info, symdef->name);
4949 if (h == (struct elf_link_hash_entry *) 0 - 1)
4955 if (h->root.type == bfd_link_hash_common)
4957 /* We currently have a common symbol. The archive map contains
4958 a reference to this symbol, so we may want to include it. We
4959 only want to include it however, if this archive element
4960 contains a definition of the symbol, not just another common
4963 Unfortunately some archivers (including GNU ar) will put
4964 declarations of common symbols into their archive maps, as
4965 well as real definitions, so we cannot just go by the archive
4966 map alone. Instead we must read in the element's symbol
4967 table and check that to see what kind of symbol definition
4969 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4972 else if (h->root.type != bfd_link_hash_undefined)
4974 if (h->root.type != bfd_link_hash_undefweak)
4979 /* We need to include this archive member. */
4980 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4981 if (element == NULL)
4984 if (! bfd_check_format (element, bfd_object))
4987 /* Doublecheck that we have not included this object
4988 already--it should be impossible, but there may be
4989 something wrong with the archive. */
4990 if (element->archive_pass != 0)
4992 bfd_set_error (bfd_error_bad_value);
4995 element->archive_pass = 1;
4997 undefs_tail = info->hash->undefs_tail;
4999 if (! (*info->callbacks->add_archive_element) (info, element,
5002 if (! bfd_link_add_symbols (element, info))
5005 /* If there are any new undefined symbols, we need to make
5006 another pass through the archive in order to see whether
5007 they can be defined. FIXME: This isn't perfect, because
5008 common symbols wind up on undefs_tail and because an
5009 undefined symbol which is defined later on in this pass
5010 does not require another pass. This isn't a bug, but it
5011 does make the code less efficient than it could be. */
5012 if (undefs_tail != info->hash->undefs_tail)
5015 /* Look backward to mark all symbols from this object file
5016 which we have already seen in this pass. */
5020 included[mark] = TRUE;
5025 while (symdefs[mark].file_offset == symdef->file_offset);
5027 /* We mark subsequent symbols from this object file as we go
5028 on through the loop. */
5029 last = symdef->file_offset;
5040 if (defined != NULL)
5042 if (included != NULL)
5047 /* Given an ELF BFD, add symbols to the global hash table as
5051 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5053 switch (bfd_get_format (abfd))
5056 return elf_link_add_object_symbols (abfd, info);
5058 return elf_link_add_archive_symbols (abfd, info);
5060 bfd_set_error (bfd_error_wrong_format);
5065 struct hash_codes_info
5067 unsigned long *hashcodes;
5071 /* This function will be called though elf_link_hash_traverse to store
5072 all hash value of the exported symbols in an array. */
5075 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5077 struct hash_codes_info *inf = data;
5083 if (h->root.type == bfd_link_hash_warning)
5084 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5086 /* Ignore indirect symbols. These are added by the versioning code. */
5087 if (h->dynindx == -1)
5090 name = h->root.root.string;
5091 p = strchr (name, ELF_VER_CHR);
5094 alc = bfd_malloc (p - name + 1);
5100 memcpy (alc, name, p - name);
5101 alc[p - name] = '\0';
5105 /* Compute the hash value. */
5106 ha = bfd_elf_hash (name);
5108 /* Store the found hash value in the array given as the argument. */
5109 *(inf->hashcodes)++ = ha;
5111 /* And store it in the struct so that we can put it in the hash table
5113 h->u.elf_hash_value = ha;
5121 struct collect_gnu_hash_codes
5124 const struct elf_backend_data *bed;
5125 unsigned long int nsyms;
5126 unsigned long int maskbits;
5127 unsigned long int *hashcodes;
5128 unsigned long int *hashval;
5129 unsigned long int *indx;
5130 unsigned long int *counts;
5133 long int min_dynindx;
5134 unsigned long int bucketcount;
5135 unsigned long int symindx;
5136 long int local_indx;
5137 long int shift1, shift2;
5138 unsigned long int mask;
5142 /* This function will be called though elf_link_hash_traverse to store
5143 all hash value of the exported symbols in an array. */
5146 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5148 struct collect_gnu_hash_codes *s = data;
5154 if (h->root.type == bfd_link_hash_warning)
5155 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5157 /* Ignore indirect symbols. These are added by the versioning code. */
5158 if (h->dynindx == -1)
5161 /* Ignore also local symbols and undefined symbols. */
5162 if (! (*s->bed->elf_hash_symbol) (h))
5165 name = h->root.root.string;
5166 p = strchr (name, ELF_VER_CHR);
5169 alc = bfd_malloc (p - name + 1);
5175 memcpy (alc, name, p - name);
5176 alc[p - name] = '\0';
5180 /* Compute the hash value. */
5181 ha = bfd_elf_gnu_hash (name);
5183 /* Store the found hash value in the array for compute_bucket_count,
5184 and also for .dynsym reordering purposes. */
5185 s->hashcodes[s->nsyms] = ha;
5186 s->hashval[h->dynindx] = ha;
5188 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5189 s->min_dynindx = h->dynindx;
5197 /* This function will be called though elf_link_hash_traverse to do
5198 final dynaminc symbol renumbering. */
5201 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5203 struct collect_gnu_hash_codes *s = data;
5204 unsigned long int bucket;
5205 unsigned long int val;
5207 if (h->root.type == bfd_link_hash_warning)
5208 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5210 /* Ignore indirect symbols. */
5211 if (h->dynindx == -1)
5214 /* Ignore also local symbols and undefined symbols. */
5215 if (! (*s->bed->elf_hash_symbol) (h))
5217 if (h->dynindx >= s->min_dynindx)
5218 h->dynindx = s->local_indx++;
5222 bucket = s->hashval[h->dynindx] % s->bucketcount;
5223 val = (s->hashval[h->dynindx] >> s->shift1)
5224 & ((s->maskbits >> s->shift1) - 1);
5225 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5227 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5228 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5229 if (s->counts[bucket] == 1)
5230 /* Last element terminates the chain. */
5232 bfd_put_32 (s->output_bfd, val,
5233 s->contents + (s->indx[bucket] - s->symindx) * 4);
5234 --s->counts[bucket];
5235 h->dynindx = s->indx[bucket]++;
5239 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5242 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5244 return !(h->forced_local
5245 || h->root.type == bfd_link_hash_undefined
5246 || h->root.type == bfd_link_hash_undefweak
5247 || ((h->root.type == bfd_link_hash_defined
5248 || h->root.type == bfd_link_hash_defweak)
5249 && h->root.u.def.section->output_section == NULL));
5252 /* Array used to determine the number of hash table buckets to use
5253 based on the number of symbols there are. If there are fewer than
5254 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5255 fewer than 37 we use 17 buckets, and so forth. We never use more
5256 than 32771 buckets. */
5258 static const size_t elf_buckets[] =
5260 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5264 /* Compute bucket count for hashing table. We do not use a static set
5265 of possible tables sizes anymore. Instead we determine for all
5266 possible reasonable sizes of the table the outcome (i.e., the
5267 number of collisions etc) and choose the best solution. The
5268 weighting functions are not too simple to allow the table to grow
5269 without bounds. Instead one of the weighting factors is the size.
5270 Therefore the result is always a good payoff between few collisions
5271 (= short chain lengths) and table size. */
5273 compute_bucket_count (struct bfd_link_info *info,
5274 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5275 unsigned long int nsyms,
5278 size_t best_size = 0;
5279 unsigned long int i;
5281 /* We have a problem here. The following code to optimize the table
5282 size requires an integer type with more the 32 bits. If
5283 BFD_HOST_U_64_BIT is set we know about such a type. */
5284 #ifdef BFD_HOST_U_64_BIT
5289 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5290 bfd *dynobj = elf_hash_table (info)->dynobj;
5291 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5292 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5293 unsigned long int *counts;
5296 /* Possible optimization parameters: if we have NSYMS symbols we say
5297 that the hashing table must at least have NSYMS/4 and at most
5299 minsize = nsyms / 4;
5302 best_size = maxsize = nsyms * 2;
5307 if ((best_size & 31) == 0)
5311 /* Create array where we count the collisions in. We must use bfd_malloc
5312 since the size could be large. */
5314 amt *= sizeof (unsigned long int);
5315 counts = bfd_malloc (amt);
5319 /* Compute the "optimal" size for the hash table. The criteria is a
5320 minimal chain length. The minor criteria is (of course) the size
5322 for (i = minsize; i < maxsize; ++i)
5324 /* Walk through the array of hashcodes and count the collisions. */
5325 BFD_HOST_U_64_BIT max;
5326 unsigned long int j;
5327 unsigned long int fact;
5329 if (gnu_hash && (i & 31) == 0)
5332 memset (counts, '\0', i * sizeof (unsigned long int));
5334 /* Determine how often each hash bucket is used. */
5335 for (j = 0; j < nsyms; ++j)
5336 ++counts[hashcodes[j] % i];
5338 /* For the weight function we need some information about the
5339 pagesize on the target. This is information need not be 100%
5340 accurate. Since this information is not available (so far) we
5341 define it here to a reasonable default value. If it is crucial
5342 to have a better value some day simply define this value. */
5343 # ifndef BFD_TARGET_PAGESIZE
5344 # define BFD_TARGET_PAGESIZE (4096)
5347 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5349 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5352 /* Variant 1: optimize for short chains. We add the squares
5353 of all the chain lengths (which favors many small chain
5354 over a few long chains). */
5355 for (j = 0; j < i; ++j)
5356 max += counts[j] * counts[j];
5358 /* This adds penalties for the overall size of the table. */
5359 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5362 /* Variant 2: Optimize a lot more for small table. Here we
5363 also add squares of the size but we also add penalties for
5364 empty slots (the +1 term). */
5365 for (j = 0; j < i; ++j)
5366 max += (1 + counts[j]) * (1 + counts[j]);
5368 /* The overall size of the table is considered, but not as
5369 strong as in variant 1, where it is squared. */
5370 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5374 /* Compare with current best results. */
5375 if (max < best_chlen)
5385 #endif /* defined (BFD_HOST_U_64_BIT) */
5387 /* This is the fallback solution if no 64bit type is available or if we
5388 are not supposed to spend much time on optimizations. We select the
5389 bucket count using a fixed set of numbers. */
5390 for (i = 0; elf_buckets[i] != 0; i++)
5392 best_size = elf_buckets[i];
5393 if (nsyms < elf_buckets[i + 1])
5396 if (gnu_hash && best_size < 2)
5403 /* Set up the sizes and contents of the ELF dynamic sections. This is
5404 called by the ELF linker emulation before_allocation routine. We
5405 must set the sizes of the sections before the linker sets the
5406 addresses of the various sections. */
5409 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5412 const char *filter_shlib,
5413 const char * const *auxiliary_filters,
5414 struct bfd_link_info *info,
5415 asection **sinterpptr,
5416 struct bfd_elf_version_tree *verdefs)
5418 bfd_size_type soname_indx;
5420 const struct elf_backend_data *bed;
5421 struct elf_assign_sym_version_info asvinfo;
5425 soname_indx = (bfd_size_type) -1;
5427 if (!is_elf_hash_table (info->hash))
5430 bed = get_elf_backend_data (output_bfd);
5431 if (info->execstack)
5432 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5433 else if (info->noexecstack)
5434 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5438 asection *notesec = NULL;
5441 for (inputobj = info->input_bfds;
5443 inputobj = inputobj->link_next)
5447 if (inputobj->flags & (DYNAMIC | EXEC_P | BFD_LINKER_CREATED))
5449 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5452 if (s->flags & SEC_CODE)
5456 else if (bed->default_execstack)
5461 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5462 if (exec && info->relocatable
5463 && notesec->output_section != bfd_abs_section_ptr)
5464 notesec->output_section->flags |= SEC_CODE;
5468 /* Any syms created from now on start with -1 in
5469 got.refcount/offset and plt.refcount/offset. */
5470 elf_hash_table (info)->init_got_refcount
5471 = elf_hash_table (info)->init_got_offset;
5472 elf_hash_table (info)->init_plt_refcount
5473 = elf_hash_table (info)->init_plt_offset;
5475 /* The backend may have to create some sections regardless of whether
5476 we're dynamic or not. */
5477 if (bed->elf_backend_always_size_sections
5478 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5481 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5484 dynobj = elf_hash_table (info)->dynobj;
5486 /* If there were no dynamic objects in the link, there is nothing to
5491 if (elf_hash_table (info)->dynamic_sections_created)
5493 struct elf_info_failed eif;
5494 struct elf_link_hash_entry *h;
5496 struct bfd_elf_version_tree *t;
5497 struct bfd_elf_version_expr *d;
5499 bfd_boolean all_defined;
5501 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5502 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5506 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5508 if (soname_indx == (bfd_size_type) -1
5509 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5515 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5517 info->flags |= DF_SYMBOLIC;
5524 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5526 if (indx == (bfd_size_type) -1
5527 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5530 if (info->new_dtags)
5532 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5533 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5538 if (filter_shlib != NULL)
5542 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5543 filter_shlib, TRUE);
5544 if (indx == (bfd_size_type) -1
5545 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5549 if (auxiliary_filters != NULL)
5551 const char * const *p;
5553 for (p = auxiliary_filters; *p != NULL; p++)
5557 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5559 if (indx == (bfd_size_type) -1
5560 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5566 eif.verdefs = verdefs;
5569 /* If we are supposed to export all symbols into the dynamic symbol
5570 table (this is not the normal case), then do so. */
5571 if (info->export_dynamic
5572 || (info->executable && info->dynamic))
5574 elf_link_hash_traverse (elf_hash_table (info),
5575 _bfd_elf_export_symbol,
5581 /* Make all global versions with definition. */
5582 for (t = verdefs; t != NULL; t = t->next)
5583 for (d = t->globals.list; d != NULL; d = d->next)
5584 if (!d->symver && d->literal)
5586 const char *verstr, *name;
5587 size_t namelen, verlen, newlen;
5589 struct elf_link_hash_entry *newh;
5592 namelen = strlen (name);
5594 verlen = strlen (verstr);
5595 newlen = namelen + verlen + 3;
5597 newname = bfd_malloc (newlen);
5598 if (newname == NULL)
5600 memcpy (newname, name, namelen);
5602 /* Check the hidden versioned definition. */
5603 p = newname + namelen;
5605 memcpy (p, verstr, verlen + 1);
5606 newh = elf_link_hash_lookup (elf_hash_table (info),
5607 newname, FALSE, FALSE,
5610 || (newh->root.type != bfd_link_hash_defined
5611 && newh->root.type != bfd_link_hash_defweak))
5613 /* Check the default versioned definition. */
5615 memcpy (p, verstr, verlen + 1);
5616 newh = elf_link_hash_lookup (elf_hash_table (info),
5617 newname, FALSE, FALSE,
5622 /* Mark this version if there is a definition and it is
5623 not defined in a shared object. */
5625 && !newh->def_dynamic
5626 && (newh->root.type == bfd_link_hash_defined
5627 || newh->root.type == bfd_link_hash_defweak))
5631 /* Attach all the symbols to their version information. */
5632 asvinfo.output_bfd = output_bfd;
5633 asvinfo.info = info;
5634 asvinfo.verdefs = verdefs;
5635 asvinfo.failed = FALSE;
5637 elf_link_hash_traverse (elf_hash_table (info),
5638 _bfd_elf_link_assign_sym_version,
5643 if (!info->allow_undefined_version)
5645 /* Check if all global versions have a definition. */
5647 for (t = verdefs; t != NULL; t = t->next)
5648 for (d = t->globals.list; d != NULL; d = d->next)
5649 if (d->literal && !d->symver && !d->script)
5651 (*_bfd_error_handler)
5652 (_("%s: undefined version: %s"),
5653 d->pattern, t->name);
5654 all_defined = FALSE;
5659 bfd_set_error (bfd_error_bad_value);
5664 /* Find all symbols which were defined in a dynamic object and make
5665 the backend pick a reasonable value for them. */
5666 elf_link_hash_traverse (elf_hash_table (info),
5667 _bfd_elf_adjust_dynamic_symbol,
5672 /* Add some entries to the .dynamic section. We fill in some of the
5673 values later, in bfd_elf_final_link, but we must add the entries
5674 now so that we know the final size of the .dynamic section. */
5676 /* If there are initialization and/or finalization functions to
5677 call then add the corresponding DT_INIT/DT_FINI entries. */
5678 h = (info->init_function
5679 ? elf_link_hash_lookup (elf_hash_table (info),
5680 info->init_function, FALSE,
5687 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5690 h = (info->fini_function
5691 ? elf_link_hash_lookup (elf_hash_table (info),
5692 info->fini_function, FALSE,
5699 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5703 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5704 if (s != NULL && s->linker_has_input)
5706 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5707 if (! info->executable)
5712 for (sub = info->input_bfds; sub != NULL;
5713 sub = sub->link_next)
5714 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5715 for (o = sub->sections; o != NULL; o = o->next)
5716 if (elf_section_data (o)->this_hdr.sh_type
5717 == SHT_PREINIT_ARRAY)
5719 (*_bfd_error_handler)
5720 (_("%B: .preinit_array section is not allowed in DSO"),
5725 bfd_set_error (bfd_error_nonrepresentable_section);
5729 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5730 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5733 s = bfd_get_section_by_name (output_bfd, ".init_array");
5734 if (s != NULL && s->linker_has_input)
5736 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5737 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5740 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5741 if (s != NULL && s->linker_has_input)
5743 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5744 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5748 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5749 /* If .dynstr is excluded from the link, we don't want any of
5750 these tags. Strictly, we should be checking each section
5751 individually; This quick check covers for the case where
5752 someone does a /DISCARD/ : { *(*) }. */
5753 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5755 bfd_size_type strsize;
5757 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5758 if ((info->emit_hash
5759 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5760 || (info->emit_gnu_hash
5761 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5762 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5763 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5764 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5765 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5766 bed->s->sizeof_sym))
5771 /* The backend must work out the sizes of all the other dynamic
5773 if (bed->elf_backend_size_dynamic_sections
5774 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5777 if (elf_hash_table (info)->dynamic_sections_created)
5779 unsigned long section_sym_count;
5782 /* Set up the version definition section. */
5783 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5784 BFD_ASSERT (s != NULL);
5786 /* We may have created additional version definitions if we are
5787 just linking a regular application. */
5788 verdefs = asvinfo.verdefs;
5790 /* Skip anonymous version tag. */
5791 if (verdefs != NULL && verdefs->vernum == 0)
5792 verdefs = verdefs->next;
5794 if (verdefs == NULL && !info->create_default_symver)
5795 s->flags |= SEC_EXCLUDE;
5800 struct bfd_elf_version_tree *t;
5802 Elf_Internal_Verdef def;
5803 Elf_Internal_Verdaux defaux;
5804 struct bfd_link_hash_entry *bh;
5805 struct elf_link_hash_entry *h;
5811 /* Make space for the base version. */
5812 size += sizeof (Elf_External_Verdef);
5813 size += sizeof (Elf_External_Verdaux);
5816 /* Make space for the default version. */
5817 if (info->create_default_symver)
5819 size += sizeof (Elf_External_Verdef);
5823 for (t = verdefs; t != NULL; t = t->next)
5825 struct bfd_elf_version_deps *n;
5827 size += sizeof (Elf_External_Verdef);
5828 size += sizeof (Elf_External_Verdaux);
5831 for (n = t->deps; n != NULL; n = n->next)
5832 size += sizeof (Elf_External_Verdaux);
5836 s->contents = bfd_alloc (output_bfd, s->size);
5837 if (s->contents == NULL && s->size != 0)
5840 /* Fill in the version definition section. */
5844 def.vd_version = VER_DEF_CURRENT;
5845 def.vd_flags = VER_FLG_BASE;
5848 if (info->create_default_symver)
5850 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5851 def.vd_next = sizeof (Elf_External_Verdef);
5855 def.vd_aux = sizeof (Elf_External_Verdef);
5856 def.vd_next = (sizeof (Elf_External_Verdef)
5857 + sizeof (Elf_External_Verdaux));
5860 if (soname_indx != (bfd_size_type) -1)
5862 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5864 def.vd_hash = bfd_elf_hash (soname);
5865 defaux.vda_name = soname_indx;
5872 name = lbasename (output_bfd->filename);
5873 def.vd_hash = bfd_elf_hash (name);
5874 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5876 if (indx == (bfd_size_type) -1)
5878 defaux.vda_name = indx;
5880 defaux.vda_next = 0;
5882 _bfd_elf_swap_verdef_out (output_bfd, &def,
5883 (Elf_External_Verdef *) p);
5884 p += sizeof (Elf_External_Verdef);
5885 if (info->create_default_symver)
5887 /* Add a symbol representing this version. */
5889 if (! (_bfd_generic_link_add_one_symbol
5890 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5892 get_elf_backend_data (dynobj)->collect, &bh)))
5894 h = (struct elf_link_hash_entry *) bh;
5897 h->type = STT_OBJECT;
5898 h->verinfo.vertree = NULL;
5900 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5903 /* Create a duplicate of the base version with the same
5904 aux block, but different flags. */
5907 def.vd_aux = sizeof (Elf_External_Verdef);
5909 def.vd_next = (sizeof (Elf_External_Verdef)
5910 + sizeof (Elf_External_Verdaux));
5913 _bfd_elf_swap_verdef_out (output_bfd, &def,
5914 (Elf_External_Verdef *) p);
5915 p += sizeof (Elf_External_Verdef);
5917 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5918 (Elf_External_Verdaux *) p);
5919 p += sizeof (Elf_External_Verdaux);
5921 for (t = verdefs; t != NULL; t = t->next)
5924 struct bfd_elf_version_deps *n;
5927 for (n = t->deps; n != NULL; n = n->next)
5930 /* Add a symbol representing this version. */
5932 if (! (_bfd_generic_link_add_one_symbol
5933 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5935 get_elf_backend_data (dynobj)->collect, &bh)))
5937 h = (struct elf_link_hash_entry *) bh;
5940 h->type = STT_OBJECT;
5941 h->verinfo.vertree = t;
5943 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5946 def.vd_version = VER_DEF_CURRENT;
5948 if (t->globals.list == NULL
5949 && t->locals.list == NULL
5951 def.vd_flags |= VER_FLG_WEAK;
5952 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5953 def.vd_cnt = cdeps + 1;
5954 def.vd_hash = bfd_elf_hash (t->name);
5955 def.vd_aux = sizeof (Elf_External_Verdef);
5957 if (t->next != NULL)
5958 def.vd_next = (sizeof (Elf_External_Verdef)
5959 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5961 _bfd_elf_swap_verdef_out (output_bfd, &def,
5962 (Elf_External_Verdef *) p);
5963 p += sizeof (Elf_External_Verdef);
5965 defaux.vda_name = h->dynstr_index;
5966 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5968 defaux.vda_next = 0;
5969 if (t->deps != NULL)
5970 defaux.vda_next = sizeof (Elf_External_Verdaux);
5971 t->name_indx = defaux.vda_name;
5973 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5974 (Elf_External_Verdaux *) p);
5975 p += sizeof (Elf_External_Verdaux);
5977 for (n = t->deps; n != NULL; n = n->next)
5979 if (n->version_needed == NULL)
5981 /* This can happen if there was an error in the
5983 defaux.vda_name = 0;
5987 defaux.vda_name = n->version_needed->name_indx;
5988 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5991 if (n->next == NULL)
5992 defaux.vda_next = 0;
5994 defaux.vda_next = sizeof (Elf_External_Verdaux);
5996 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5997 (Elf_External_Verdaux *) p);
5998 p += sizeof (Elf_External_Verdaux);
6002 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6003 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6006 elf_tdata (output_bfd)->cverdefs = cdefs;
6009 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6011 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6014 else if (info->flags & DF_BIND_NOW)
6016 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6022 if (info->executable)
6023 info->flags_1 &= ~ (DF_1_INITFIRST
6026 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6030 /* Work out the size of the version reference section. */
6032 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
6033 BFD_ASSERT (s != NULL);
6035 struct elf_find_verdep_info sinfo;
6037 sinfo.output_bfd = output_bfd;
6039 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6040 if (sinfo.vers == 0)
6042 sinfo.failed = FALSE;
6044 elf_link_hash_traverse (elf_hash_table (info),
6045 _bfd_elf_link_find_version_dependencies,
6050 if (elf_tdata (output_bfd)->verref == NULL)
6051 s->flags |= SEC_EXCLUDE;
6054 Elf_Internal_Verneed *t;
6059 /* Build the version definition section. */
6062 for (t = elf_tdata (output_bfd)->verref;
6066 Elf_Internal_Vernaux *a;
6068 size += sizeof (Elf_External_Verneed);
6070 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6071 size += sizeof (Elf_External_Vernaux);
6075 s->contents = bfd_alloc (output_bfd, s->size);
6076 if (s->contents == NULL)
6080 for (t = elf_tdata (output_bfd)->verref;
6085 Elf_Internal_Vernaux *a;
6089 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6092 t->vn_version = VER_NEED_CURRENT;
6094 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6095 elf_dt_name (t->vn_bfd) != NULL
6096 ? elf_dt_name (t->vn_bfd)
6097 : lbasename (t->vn_bfd->filename),
6099 if (indx == (bfd_size_type) -1)
6102 t->vn_aux = sizeof (Elf_External_Verneed);
6103 if (t->vn_nextref == NULL)
6106 t->vn_next = (sizeof (Elf_External_Verneed)
6107 + caux * sizeof (Elf_External_Vernaux));
6109 _bfd_elf_swap_verneed_out (output_bfd, t,
6110 (Elf_External_Verneed *) p);
6111 p += sizeof (Elf_External_Verneed);
6113 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6115 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6116 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6117 a->vna_nodename, FALSE);
6118 if (indx == (bfd_size_type) -1)
6121 if (a->vna_nextptr == NULL)
6124 a->vna_next = sizeof (Elf_External_Vernaux);
6126 _bfd_elf_swap_vernaux_out (output_bfd, a,
6127 (Elf_External_Vernaux *) p);
6128 p += sizeof (Elf_External_Vernaux);
6132 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6133 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6136 elf_tdata (output_bfd)->cverrefs = crefs;
6140 if ((elf_tdata (output_bfd)->cverrefs == 0
6141 && elf_tdata (output_bfd)->cverdefs == 0)
6142 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6143 §ion_sym_count) == 0)
6145 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6146 s->flags |= SEC_EXCLUDE;
6152 /* Find the first non-excluded output section. We'll use its
6153 section symbol for some emitted relocs. */
6155 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6159 for (s = output_bfd->sections; s != NULL; s = s->next)
6160 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6161 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6163 elf_hash_table (info)->text_index_section = s;
6168 /* Find two non-excluded output sections, one for code, one for data.
6169 We'll use their section symbols for some emitted relocs. */
6171 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6175 /* Data first, since setting text_index_section changes
6176 _bfd_elf_link_omit_section_dynsym. */
6177 for (s = output_bfd->sections; s != NULL; s = s->next)
6178 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6179 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6181 elf_hash_table (info)->data_index_section = s;
6185 for (s = output_bfd->sections; s != NULL; s = s->next)
6186 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6187 == (SEC_ALLOC | SEC_READONLY))
6188 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6190 elf_hash_table (info)->text_index_section = s;
6194 if (elf_hash_table (info)->text_index_section == NULL)
6195 elf_hash_table (info)->text_index_section
6196 = elf_hash_table (info)->data_index_section;
6200 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6202 const struct elf_backend_data *bed;
6204 if (!is_elf_hash_table (info->hash))
6207 bed = get_elf_backend_data (output_bfd);
6208 (*bed->elf_backend_init_index_section) (output_bfd, info);
6210 if (elf_hash_table (info)->dynamic_sections_created)
6214 bfd_size_type dynsymcount;
6215 unsigned long section_sym_count;
6216 unsigned int dtagcount;
6218 dynobj = elf_hash_table (info)->dynobj;
6220 /* Assign dynsym indicies. In a shared library we generate a
6221 section symbol for each output section, which come first.
6222 Next come all of the back-end allocated local dynamic syms,
6223 followed by the rest of the global symbols. */
6225 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6226 §ion_sym_count);
6228 /* Work out the size of the symbol version section. */
6229 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6230 BFD_ASSERT (s != NULL);
6231 if (dynsymcount != 0
6232 && (s->flags & SEC_EXCLUDE) == 0)
6234 s->size = dynsymcount * sizeof (Elf_External_Versym);
6235 s->contents = bfd_zalloc (output_bfd, s->size);
6236 if (s->contents == NULL)
6239 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6243 /* Set the size of the .dynsym and .hash sections. We counted
6244 the number of dynamic symbols in elf_link_add_object_symbols.
6245 We will build the contents of .dynsym and .hash when we build
6246 the final symbol table, because until then we do not know the
6247 correct value to give the symbols. We built the .dynstr
6248 section as we went along in elf_link_add_object_symbols. */
6249 s = bfd_get_section_by_name (dynobj, ".dynsym");
6250 BFD_ASSERT (s != NULL);
6251 s->size = dynsymcount * bed->s->sizeof_sym;
6253 if (dynsymcount != 0)
6255 s->contents = bfd_alloc (output_bfd, s->size);
6256 if (s->contents == NULL)
6259 /* The first entry in .dynsym is a dummy symbol.
6260 Clear all the section syms, in case we don't output them all. */
6261 ++section_sym_count;
6262 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6265 elf_hash_table (info)->bucketcount = 0;
6267 /* Compute the size of the hashing table. As a side effect this
6268 computes the hash values for all the names we export. */
6269 if (info->emit_hash)
6271 unsigned long int *hashcodes;
6272 struct hash_codes_info hashinf;
6274 unsigned long int nsyms;
6276 size_t hash_entry_size;
6278 /* Compute the hash values for all exported symbols. At the same
6279 time store the values in an array so that we could use them for
6281 amt = dynsymcount * sizeof (unsigned long int);
6282 hashcodes = bfd_malloc (amt);
6283 if (hashcodes == NULL)
6285 hashinf.hashcodes = hashcodes;
6286 hashinf.error = FALSE;
6288 /* Put all hash values in HASHCODES. */
6289 elf_link_hash_traverse (elf_hash_table (info),
6290 elf_collect_hash_codes, &hashinf);
6297 nsyms = hashinf.hashcodes - hashcodes;
6299 = compute_bucket_count (info, hashcodes, nsyms, 0);
6302 if (bucketcount == 0)
6305 elf_hash_table (info)->bucketcount = bucketcount;
6307 s = bfd_get_section_by_name (dynobj, ".hash");
6308 BFD_ASSERT (s != NULL);
6309 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6310 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6311 s->contents = bfd_zalloc (output_bfd, s->size);
6312 if (s->contents == NULL)
6315 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6316 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6317 s->contents + hash_entry_size);
6320 if (info->emit_gnu_hash)
6323 unsigned char *contents;
6324 struct collect_gnu_hash_codes cinfo;
6328 memset (&cinfo, 0, sizeof (cinfo));
6330 /* Compute the hash values for all exported symbols. At the same
6331 time store the values in an array so that we could use them for
6333 amt = dynsymcount * 2 * sizeof (unsigned long int);
6334 cinfo.hashcodes = bfd_malloc (amt);
6335 if (cinfo.hashcodes == NULL)
6338 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6339 cinfo.min_dynindx = -1;
6340 cinfo.output_bfd = output_bfd;
6343 /* Put all hash values in HASHCODES. */
6344 elf_link_hash_traverse (elf_hash_table (info),
6345 elf_collect_gnu_hash_codes, &cinfo);
6348 free (cinfo.hashcodes);
6353 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6355 if (bucketcount == 0)
6357 free (cinfo.hashcodes);
6361 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6362 BFD_ASSERT (s != NULL);
6364 if (cinfo.nsyms == 0)
6366 /* Empty .gnu.hash section is special. */
6367 BFD_ASSERT (cinfo.min_dynindx == -1);
6368 free (cinfo.hashcodes);
6369 s->size = 5 * 4 + bed->s->arch_size / 8;
6370 contents = bfd_zalloc (output_bfd, s->size);
6371 if (contents == NULL)
6373 s->contents = contents;
6374 /* 1 empty bucket. */
6375 bfd_put_32 (output_bfd, 1, contents);
6376 /* SYMIDX above the special symbol 0. */
6377 bfd_put_32 (output_bfd, 1, contents + 4);
6378 /* Just one word for bitmask. */
6379 bfd_put_32 (output_bfd, 1, contents + 8);
6380 /* Only hash fn bloom filter. */
6381 bfd_put_32 (output_bfd, 0, contents + 12);
6382 /* No hashes are valid - empty bitmask. */
6383 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6384 /* No hashes in the only bucket. */
6385 bfd_put_32 (output_bfd, 0,
6386 contents + 16 + bed->s->arch_size / 8);
6390 unsigned long int maskwords, maskbitslog2;
6391 BFD_ASSERT (cinfo.min_dynindx != -1);
6393 maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
6394 if (maskbitslog2 < 3)
6396 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6397 maskbitslog2 = maskbitslog2 + 3;
6399 maskbitslog2 = maskbitslog2 + 2;
6400 if (bed->s->arch_size == 64)
6402 if (maskbitslog2 == 5)
6408 cinfo.mask = (1 << cinfo.shift1) - 1;
6409 cinfo.shift2 = maskbitslog2;
6410 cinfo.maskbits = 1 << maskbitslog2;
6411 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6412 amt = bucketcount * sizeof (unsigned long int) * 2;
6413 amt += maskwords * sizeof (bfd_vma);
6414 cinfo.bitmask = bfd_malloc (amt);
6415 if (cinfo.bitmask == NULL)
6417 free (cinfo.hashcodes);
6421 cinfo.counts = (void *) (cinfo.bitmask + maskwords);
6422 cinfo.indx = cinfo.counts + bucketcount;
6423 cinfo.symindx = dynsymcount - cinfo.nsyms;
6424 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6426 /* Determine how often each hash bucket is used. */
6427 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6428 for (i = 0; i < cinfo.nsyms; ++i)
6429 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6431 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6432 if (cinfo.counts[i] != 0)
6434 cinfo.indx[i] = cnt;
6435 cnt += cinfo.counts[i];
6437 BFD_ASSERT (cnt == dynsymcount);
6438 cinfo.bucketcount = bucketcount;
6439 cinfo.local_indx = cinfo.min_dynindx;
6441 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6442 s->size += cinfo.maskbits / 8;
6443 contents = bfd_zalloc (output_bfd, s->size);
6444 if (contents == NULL)
6446 free (cinfo.bitmask);
6447 free (cinfo.hashcodes);
6451 s->contents = contents;
6452 bfd_put_32 (output_bfd, bucketcount, contents);
6453 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6454 bfd_put_32 (output_bfd, maskwords, contents + 8);
6455 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6456 contents += 16 + cinfo.maskbits / 8;
6458 for (i = 0; i < bucketcount; ++i)
6460 if (cinfo.counts[i] == 0)
6461 bfd_put_32 (output_bfd, 0, contents);
6463 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6467 cinfo.contents = contents;
6469 /* Renumber dynamic symbols, populate .gnu.hash section. */
6470 elf_link_hash_traverse (elf_hash_table (info),
6471 elf_renumber_gnu_hash_syms, &cinfo);
6473 contents = s->contents + 16;
6474 for (i = 0; i < maskwords; ++i)
6476 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6478 contents += bed->s->arch_size / 8;
6481 free (cinfo.bitmask);
6482 free (cinfo.hashcodes);
6486 s = bfd_get_section_by_name (dynobj, ".dynstr");
6487 BFD_ASSERT (s != NULL);
6489 elf_finalize_dynstr (output_bfd, info);
6491 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6493 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6494 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6501 /* Indicate that we are only retrieving symbol values from this
6505 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
6507 if (is_elf_hash_table (info->hash))
6508 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
6509 _bfd_generic_link_just_syms (sec, info);
6512 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6515 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6518 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
6519 sec->sec_info_type = ELF_INFO_TYPE_NONE;
6522 /* Finish SHF_MERGE section merging. */
6525 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6530 if (!is_elf_hash_table (info->hash))
6533 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6534 if ((ibfd->flags & DYNAMIC) == 0)
6535 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6536 if ((sec->flags & SEC_MERGE) != 0
6537 && !bfd_is_abs_section (sec->output_section))
6539 struct bfd_elf_section_data *secdata;
6541 secdata = elf_section_data (sec);
6542 if (! _bfd_add_merge_section (abfd,
6543 &elf_hash_table (info)->merge_info,
6544 sec, &secdata->sec_info))
6546 else if (secdata->sec_info)
6547 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
6550 if (elf_hash_table (info)->merge_info != NULL)
6551 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6552 merge_sections_remove_hook);
6556 /* Create an entry in an ELF linker hash table. */
6558 struct bfd_hash_entry *
6559 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6560 struct bfd_hash_table *table,
6563 /* Allocate the structure if it has not already been allocated by a
6567 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6572 /* Call the allocation method of the superclass. */
6573 entry = _bfd_link_hash_newfunc (entry, table, string);
6576 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6577 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6579 /* Set local fields. */
6582 ret->got = htab->init_got_refcount;
6583 ret->plt = htab->init_plt_refcount;
6584 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6585 - offsetof (struct elf_link_hash_entry, size)));
6586 /* Assume that we have been called by a non-ELF symbol reader.
6587 This flag is then reset by the code which reads an ELF input
6588 file. This ensures that a symbol created by a non-ELF symbol
6589 reader will have the flag set correctly. */
6596 /* Copy data from an indirect symbol to its direct symbol, hiding the
6597 old indirect symbol. Also used for copying flags to a weakdef. */
6600 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6601 struct elf_link_hash_entry *dir,
6602 struct elf_link_hash_entry *ind)
6604 struct elf_link_hash_table *htab;
6606 /* Copy down any references that we may have already seen to the
6607 symbol which just became indirect. */
6609 dir->ref_dynamic |= ind->ref_dynamic;
6610 dir->ref_regular |= ind->ref_regular;
6611 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6612 dir->non_got_ref |= ind->non_got_ref;
6613 dir->needs_plt |= ind->needs_plt;
6614 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6616 if (ind->root.type != bfd_link_hash_indirect)
6619 /* Copy over the global and procedure linkage table refcount entries.
6620 These may have been already set up by a check_relocs routine. */
6621 htab = elf_hash_table (info);
6622 if (ind->got.refcount > htab->init_got_refcount.refcount)
6624 if (dir->got.refcount < 0)
6625 dir->got.refcount = 0;
6626 dir->got.refcount += ind->got.refcount;
6627 ind->got.refcount = htab->init_got_refcount.refcount;
6630 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6632 if (dir->plt.refcount < 0)
6633 dir->plt.refcount = 0;
6634 dir->plt.refcount += ind->plt.refcount;
6635 ind->plt.refcount = htab->init_plt_refcount.refcount;
6638 if (ind->dynindx != -1)
6640 if (dir->dynindx != -1)
6641 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6642 dir->dynindx = ind->dynindx;
6643 dir->dynstr_index = ind->dynstr_index;
6645 ind->dynstr_index = 0;
6650 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6651 struct elf_link_hash_entry *h,
6652 bfd_boolean force_local)
6654 h->plt = elf_hash_table (info)->init_plt_offset;
6658 h->forced_local = 1;
6659 if (h->dynindx != -1)
6662 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6668 /* Initialize an ELF linker hash table. */
6671 _bfd_elf_link_hash_table_init
6672 (struct elf_link_hash_table *table,
6674 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6675 struct bfd_hash_table *,
6677 unsigned int entsize)
6680 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6682 memset (table, 0, sizeof * table);
6683 table->init_got_refcount.refcount = can_refcount - 1;
6684 table->init_plt_refcount.refcount = can_refcount - 1;
6685 table->init_got_offset.offset = -(bfd_vma) 1;
6686 table->init_plt_offset.offset = -(bfd_vma) 1;
6687 /* The first dynamic symbol is a dummy. */
6688 table->dynsymcount = 1;
6690 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6691 table->root.type = bfd_link_elf_hash_table;
6696 /* Create an ELF linker hash table. */
6698 struct bfd_link_hash_table *
6699 _bfd_elf_link_hash_table_create (bfd *abfd)
6701 struct elf_link_hash_table *ret;
6702 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6704 ret = bfd_malloc (amt);
6708 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
6709 sizeof (struct elf_link_hash_entry)))
6718 /* This is a hook for the ELF emulation code in the generic linker to
6719 tell the backend linker what file name to use for the DT_NEEDED
6720 entry for a dynamic object. */
6723 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6725 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6726 && bfd_get_format (abfd) == bfd_object)
6727 elf_dt_name (abfd) = name;
6731 bfd_elf_get_dyn_lib_class (bfd *abfd)
6734 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6735 && bfd_get_format (abfd) == bfd_object)
6736 lib_class = elf_dyn_lib_class (abfd);
6743 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6745 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6746 && bfd_get_format (abfd) == bfd_object)
6747 elf_dyn_lib_class (abfd) = lib_class;
6750 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6751 the linker ELF emulation code. */
6753 struct bfd_link_needed_list *
6754 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6755 struct bfd_link_info *info)
6757 if (! is_elf_hash_table (info->hash))
6759 return elf_hash_table (info)->needed;
6762 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6763 hook for the linker ELF emulation code. */
6765 struct bfd_link_needed_list *
6766 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6767 struct bfd_link_info *info)
6769 if (! is_elf_hash_table (info->hash))
6771 return elf_hash_table (info)->runpath;
6774 /* Get the name actually used for a dynamic object for a link. This
6775 is the SONAME entry if there is one. Otherwise, it is the string
6776 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6779 bfd_elf_get_dt_soname (bfd *abfd)
6781 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6782 && bfd_get_format (abfd) == bfd_object)
6783 return elf_dt_name (abfd);
6787 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6788 the ELF linker emulation code. */
6791 bfd_elf_get_bfd_needed_list (bfd *abfd,
6792 struct bfd_link_needed_list **pneeded)
6795 bfd_byte *dynbuf = NULL;
6796 unsigned int elfsec;
6797 unsigned long shlink;
6798 bfd_byte *extdyn, *extdynend;
6800 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6804 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6805 || bfd_get_format (abfd) != bfd_object)
6808 s = bfd_get_section_by_name (abfd, ".dynamic");
6809 if (s == NULL || s->size == 0)
6812 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6815 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
6816 if (elfsec == SHN_BAD)
6819 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
6821 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
6822 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
6825 extdynend = extdyn + s->size;
6826 for (; extdyn < extdynend; extdyn += extdynsize)
6828 Elf_Internal_Dyn dyn;
6830 (*swap_dyn_in) (abfd, extdyn, &dyn);
6832 if (dyn.d_tag == DT_NULL)
6835 if (dyn.d_tag == DT_NEEDED)
6838 struct bfd_link_needed_list *l;
6839 unsigned int tagv = dyn.d_un.d_val;
6842 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
6847 l = bfd_alloc (abfd, amt);
6868 struct elf_symbuf_symbol
6870 unsigned long st_name; /* Symbol name, index in string tbl */
6871 unsigned char st_info; /* Type and binding attributes */
6872 unsigned char st_other; /* Visibilty, and target specific */
6875 struct elf_symbuf_head
6877 struct elf_symbuf_symbol *ssym;
6878 bfd_size_type count;
6879 unsigned int st_shndx;
6886 Elf_Internal_Sym *isym;
6887 struct elf_symbuf_symbol *ssym;
6892 /* Sort references to symbols by ascending section number. */
6895 elf_sort_elf_symbol (const void *arg1, const void *arg2)
6897 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
6898 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
6900 return s1->st_shndx - s2->st_shndx;
6904 elf_sym_name_compare (const void *arg1, const void *arg2)
6906 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
6907 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
6908 return strcmp (s1->name, s2->name);
6911 static struct elf_symbuf_head *
6912 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
6914 Elf_Internal_Sym **ind, **indbufend, **indbuf;
6915 struct elf_symbuf_symbol *ssym;
6916 struct elf_symbuf_head *ssymbuf, *ssymhead;
6917 bfd_size_type i, shndx_count, total_size;
6919 indbuf = bfd_malloc2 (symcount, sizeof (*indbuf));
6923 for (ind = indbuf, i = 0; i < symcount; i++)
6924 if (isymbuf[i].st_shndx != SHN_UNDEF)
6925 *ind++ = &isymbuf[i];
6928 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
6929 elf_sort_elf_symbol);
6932 if (indbufend > indbuf)
6933 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
6934 if (ind[0]->st_shndx != ind[1]->st_shndx)
6937 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
6938 + (indbufend - indbuf) * sizeof (*ssym));
6939 ssymbuf = bfd_malloc (total_size);
6940 if (ssymbuf == NULL)
6946 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
6947 ssymbuf->ssym = NULL;
6948 ssymbuf->count = shndx_count;
6949 ssymbuf->st_shndx = 0;
6950 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
6952 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
6955 ssymhead->ssym = ssym;
6956 ssymhead->count = 0;
6957 ssymhead->st_shndx = (*ind)->st_shndx;
6959 ssym->st_name = (*ind)->st_name;
6960 ssym->st_info = (*ind)->st_info;
6961 ssym->st_other = (*ind)->st_other;
6964 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
6965 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
6972 /* Check if 2 sections define the same set of local and global
6976 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
6977 struct bfd_link_info *info)
6980 const struct elf_backend_data *bed1, *bed2;
6981 Elf_Internal_Shdr *hdr1, *hdr2;
6982 bfd_size_type symcount1, symcount2;
6983 Elf_Internal_Sym *isymbuf1, *isymbuf2;
6984 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
6985 Elf_Internal_Sym *isym, *isymend;
6986 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
6987 bfd_size_type count1, count2, i;
6988 unsigned int shndx1, shndx2;
6994 /* Both sections have to be in ELF. */
6995 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
6996 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
6999 if (elf_section_type (sec1) != elf_section_type (sec2))
7002 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7003 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7004 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7007 bed1 = get_elf_backend_data (bfd1);
7008 bed2 = get_elf_backend_data (bfd2);
7009 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7010 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7011 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7012 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7014 if (symcount1 == 0 || symcount2 == 0)
7020 ssymbuf1 = elf_tdata (bfd1)->symbuf;
7021 ssymbuf2 = elf_tdata (bfd2)->symbuf;
7023 if (ssymbuf1 == NULL)
7025 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7027 if (isymbuf1 == NULL)
7030 if (!info->reduce_memory_overheads)
7031 elf_tdata (bfd1)->symbuf = ssymbuf1
7032 = elf_create_symbuf (symcount1, isymbuf1);
7035 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7037 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7039 if (isymbuf2 == NULL)
7042 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7043 elf_tdata (bfd2)->symbuf = ssymbuf2
7044 = elf_create_symbuf (symcount2, isymbuf2);
7047 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7049 /* Optimized faster version. */
7050 bfd_size_type lo, hi, mid;
7051 struct elf_symbol *symp;
7052 struct elf_symbuf_symbol *ssym, *ssymend;
7055 hi = ssymbuf1->count;
7060 mid = (lo + hi) / 2;
7061 if (shndx1 < ssymbuf1[mid].st_shndx)
7063 else if (shndx1 > ssymbuf1[mid].st_shndx)
7067 count1 = ssymbuf1[mid].count;
7074 hi = ssymbuf2->count;
7079 mid = (lo + hi) / 2;
7080 if (shndx2 < ssymbuf2[mid].st_shndx)
7082 else if (shndx2 > ssymbuf2[mid].st_shndx)
7086 count2 = ssymbuf2[mid].count;
7092 if (count1 == 0 || count2 == 0 || count1 != count2)
7095 symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol));
7096 symtable2 = bfd_malloc (count2 * sizeof (struct elf_symbol));
7097 if (symtable1 == NULL || symtable2 == NULL)
7101 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7102 ssym < ssymend; ssym++, symp++)
7104 symp->u.ssym = ssym;
7105 symp->name = bfd_elf_string_from_elf_section (bfd1,
7111 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7112 ssym < ssymend; ssym++, symp++)
7114 symp->u.ssym = ssym;
7115 symp->name = bfd_elf_string_from_elf_section (bfd2,
7120 /* Sort symbol by name. */
7121 qsort (symtable1, count1, sizeof (struct elf_symbol),
7122 elf_sym_name_compare);
7123 qsort (symtable2, count1, sizeof (struct elf_symbol),
7124 elf_sym_name_compare);
7126 for (i = 0; i < count1; i++)
7127 /* Two symbols must have the same binding, type and name. */
7128 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7129 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7130 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7137 symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7138 symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7139 if (symtable1 == NULL || symtable2 == NULL)
7142 /* Count definitions in the section. */
7144 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7145 if (isym->st_shndx == shndx1)
7146 symtable1[count1++].u.isym = isym;
7149 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7150 if (isym->st_shndx == shndx2)
7151 symtable2[count2++].u.isym = isym;
7153 if (count1 == 0 || count2 == 0 || count1 != count2)
7156 for (i = 0; i < count1; i++)
7158 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7159 symtable1[i].u.isym->st_name);
7161 for (i = 0; i < count2; i++)
7163 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7164 symtable2[i].u.isym->st_name);
7166 /* Sort symbol by name. */
7167 qsort (symtable1, count1, sizeof (struct elf_symbol),
7168 elf_sym_name_compare);
7169 qsort (symtable2, count1, sizeof (struct elf_symbol),
7170 elf_sym_name_compare);
7172 for (i = 0; i < count1; i++)
7173 /* Two symbols must have the same binding, type and name. */
7174 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7175 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7176 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7194 /* Return TRUE if 2 section types are compatible. */
7197 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7198 bfd *bbfd, const asection *bsec)
7202 || abfd->xvec->flavour != bfd_target_elf_flavour
7203 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7206 return elf_section_type (asec) == elf_section_type (bsec);
7209 /* Final phase of ELF linker. */
7211 /* A structure we use to avoid passing large numbers of arguments. */
7213 struct elf_final_link_info
7215 /* General link information. */
7216 struct bfd_link_info *info;
7219 /* Symbol string table. */
7220 struct bfd_strtab_hash *symstrtab;
7221 /* .dynsym section. */
7222 asection *dynsym_sec;
7223 /* .hash section. */
7225 /* symbol version section (.gnu.version). */
7226 asection *symver_sec;
7227 /* Buffer large enough to hold contents of any section. */
7229 /* Buffer large enough to hold external relocs of any section. */
7230 void *external_relocs;
7231 /* Buffer large enough to hold internal relocs of any section. */
7232 Elf_Internal_Rela *internal_relocs;
7233 /* Buffer large enough to hold external local symbols of any input
7235 bfd_byte *external_syms;
7236 /* And a buffer for symbol section indices. */
7237 Elf_External_Sym_Shndx *locsym_shndx;
7238 /* Buffer large enough to hold internal local symbols of any input
7240 Elf_Internal_Sym *internal_syms;
7241 /* Array large enough to hold a symbol index for each local symbol
7242 of any input BFD. */
7244 /* Array large enough to hold a section pointer for each local
7245 symbol of any input BFD. */
7246 asection **sections;
7247 /* Buffer to hold swapped out symbols. */
7249 /* And one for symbol section indices. */
7250 Elf_External_Sym_Shndx *symshndxbuf;
7251 /* Number of swapped out symbols in buffer. */
7252 size_t symbuf_count;
7253 /* Number of symbols which fit in symbuf. */
7255 /* And same for symshndxbuf. */
7256 size_t shndxbuf_size;
7259 /* This struct is used to pass information to elf_link_output_extsym. */
7261 struct elf_outext_info
7264 bfd_boolean localsyms;
7265 struct elf_final_link_info *finfo;
7269 /* Support for evaluating a complex relocation.
7271 Complex relocations are generalized, self-describing relocations. The
7272 implementation of them consists of two parts: complex symbols, and the
7273 relocations themselves.
7275 The relocations are use a reserved elf-wide relocation type code (R_RELC
7276 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7277 information (start bit, end bit, word width, etc) into the addend. This
7278 information is extracted from CGEN-generated operand tables within gas.
7280 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7281 internal) representing prefix-notation expressions, including but not
7282 limited to those sorts of expressions normally encoded as addends in the
7283 addend field. The symbol mangling format is:
7286 | <unary-operator> ':' <node>
7287 | <binary-operator> ':' <node> ':' <node>
7290 <literal> := 's' <digits=N> ':' <N character symbol name>
7291 | 'S' <digits=N> ':' <N character section name>
7295 <binary-operator> := as in C
7296 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7299 set_symbol_value (bfd *bfd_with_globals,
7300 Elf_Internal_Sym *isymbuf,
7305 struct elf_link_hash_entry **sym_hashes;
7306 struct elf_link_hash_entry *h;
7307 size_t extsymoff = locsymcount;
7309 if (symidx < locsymcount)
7311 Elf_Internal_Sym *sym;
7313 sym = isymbuf + symidx;
7314 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7316 /* It is a local symbol: move it to the
7317 "absolute" section and give it a value. */
7318 sym->st_shndx = SHN_ABS;
7319 sym->st_value = val;
7322 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7326 /* It is a global symbol: set its link type
7327 to "defined" and give it a value. */
7329 sym_hashes = elf_sym_hashes (bfd_with_globals);
7330 h = sym_hashes [symidx - extsymoff];
7331 while (h->root.type == bfd_link_hash_indirect
7332 || h->root.type == bfd_link_hash_warning)
7333 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7334 h->root.type = bfd_link_hash_defined;
7335 h->root.u.def.value = val;
7336 h->root.u.def.section = bfd_abs_section_ptr;
7340 resolve_symbol (const char *name,
7342 struct elf_final_link_info *finfo,
7344 Elf_Internal_Sym *isymbuf,
7347 Elf_Internal_Sym *sym;
7348 struct bfd_link_hash_entry *global_entry;
7349 const char *candidate = NULL;
7350 Elf_Internal_Shdr *symtab_hdr;
7353 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7355 for (i = 0; i < locsymcount; ++ i)
7359 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7362 candidate = bfd_elf_string_from_elf_section (input_bfd,
7363 symtab_hdr->sh_link,
7366 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7367 name, candidate, (unsigned long) sym->st_value);
7369 if (candidate && strcmp (candidate, name) == 0)
7371 asection *sec = finfo->sections [i];
7373 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7374 *result += sec->output_offset + sec->output_section->vma;
7376 printf ("Found symbol with value %8.8lx\n",
7377 (unsigned long) *result);
7383 /* Hmm, haven't found it yet. perhaps it is a global. */
7384 global_entry = bfd_link_hash_lookup (finfo->info->hash, name,
7385 FALSE, FALSE, TRUE);
7389 if (global_entry->type == bfd_link_hash_defined
7390 || global_entry->type == bfd_link_hash_defweak)
7392 *result = (global_entry->u.def.value
7393 + global_entry->u.def.section->output_section->vma
7394 + global_entry->u.def.section->output_offset);
7396 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7397 global_entry->root.string, (unsigned long) *result);
7406 resolve_section (const char *name,
7413 for (curr = sections; curr; curr = curr->next)
7414 if (strcmp (curr->name, name) == 0)
7416 *result = curr->vma;
7420 /* Hmm. still haven't found it. try pseudo-section names. */
7421 for (curr = sections; curr; curr = curr->next)
7423 len = strlen (curr->name);
7424 if (len > strlen (name))
7427 if (strncmp (curr->name, name, len) == 0)
7429 if (strncmp (".end", name + len, 4) == 0)
7431 *result = curr->vma + curr->size;
7435 /* Insert more pseudo-section names here, if you like. */
7443 undefined_reference (const char *reftype, const char *name)
7445 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7450 eval_symbol (bfd_vma *result,
7453 struct elf_final_link_info *finfo,
7455 Elf_Internal_Sym *isymbuf,
7464 const char *sym = *symp;
7466 bfd_boolean symbol_is_section = FALSE;
7471 if (len < 1 || len > sizeof (symbuf))
7473 bfd_set_error (bfd_error_invalid_operation);
7486 *result = strtoul (sym, (char **) symp, 16);
7490 symbol_is_section = TRUE;
7493 symlen = strtol (sym, (char **) symp, 10);
7494 sym = *symp + 1; /* Skip the trailing ':'. */
7496 if (symend < sym || symlen + 1 > sizeof (symbuf))
7498 bfd_set_error (bfd_error_invalid_operation);
7502 memcpy (symbuf, sym, symlen);
7503 symbuf[symlen] = '\0';
7504 *symp = sym + symlen;
7506 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7507 the symbol as a section, or vice-versa. so we're pretty liberal in our
7508 interpretation here; section means "try section first", not "must be a
7509 section", and likewise with symbol. */
7511 if (symbol_is_section)
7513 if (!resolve_section (symbuf, finfo->output_bfd->sections, result)
7514 && !resolve_symbol (symbuf, input_bfd, finfo, result,
7515 isymbuf, locsymcount))
7517 undefined_reference ("section", symbuf);
7523 if (!resolve_symbol (symbuf, input_bfd, finfo, result,
7524 isymbuf, locsymcount)
7525 && !resolve_section (symbuf, finfo->output_bfd->sections,
7528 undefined_reference ("symbol", symbuf);
7535 /* All that remains are operators. */
7537 #define UNARY_OP(op) \
7538 if (strncmp (sym, #op, strlen (#op)) == 0) \
7540 sym += strlen (#op); \
7544 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7545 isymbuf, locsymcount, signed_p)) \
7548 *result = op ((bfd_signed_vma) a); \
7554 #define BINARY_OP(op) \
7555 if (strncmp (sym, #op, strlen (#op)) == 0) \
7557 sym += strlen (#op); \
7561 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7562 isymbuf, locsymcount, signed_p)) \
7565 if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \
7566 isymbuf, locsymcount, signed_p)) \
7569 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7599 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7600 bfd_set_error (bfd_error_invalid_operation);
7606 put_value (bfd_vma size,
7607 unsigned long chunksz,
7612 location += (size - chunksz);
7614 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
7622 bfd_put_8 (input_bfd, x, location);
7625 bfd_put_16 (input_bfd, x, location);
7628 bfd_put_32 (input_bfd, x, location);
7632 bfd_put_64 (input_bfd, x, location);
7642 get_value (bfd_vma size,
7643 unsigned long chunksz,
7649 for (; size; size -= chunksz, location += chunksz)
7657 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7660 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7663 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7667 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7678 decode_complex_addend (unsigned long *start, /* in bits */
7679 unsigned long *oplen, /* in bits */
7680 unsigned long *len, /* in bits */
7681 unsigned long *wordsz, /* in bytes */
7682 unsigned long *chunksz, /* in bytes */
7683 unsigned long *lsb0_p,
7684 unsigned long *signed_p,
7685 unsigned long *trunc_p,
7686 unsigned long encoded)
7688 * start = encoded & 0x3F;
7689 * len = (encoded >> 6) & 0x3F;
7690 * oplen = (encoded >> 12) & 0x3F;
7691 * wordsz = (encoded >> 18) & 0xF;
7692 * chunksz = (encoded >> 22) & 0xF;
7693 * lsb0_p = (encoded >> 27) & 1;
7694 * signed_p = (encoded >> 28) & 1;
7695 * trunc_p = (encoded >> 29) & 1;
7698 bfd_reloc_status_type
7699 bfd_elf_perform_complex_relocation (bfd *input_bfd,
7700 asection *input_section ATTRIBUTE_UNUSED,
7702 Elf_Internal_Rela *rel,
7705 bfd_vma shift, x, mask;
7706 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
7707 bfd_reloc_status_type r;
7709 /* Perform this reloc, since it is complex.
7710 (this is not to say that it necessarily refers to a complex
7711 symbol; merely that it is a self-describing CGEN based reloc.
7712 i.e. the addend has the complete reloc information (bit start, end,
7713 word size, etc) encoded within it.). */
7715 decode_complex_addend (&start, &oplen, &len, &wordsz,
7716 &chunksz, &lsb0_p, &signed_p,
7717 &trunc_p, rel->r_addend);
7719 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7722 shift = (start + 1) - len;
7724 shift = (8 * wordsz) - (start + len);
7726 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7729 printf ("Doing complex reloc: "
7730 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7731 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7732 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7733 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7734 oplen, x, mask, relocation);
7739 /* Now do an overflow check. */
7740 r = bfd_check_overflow ((signed_p
7741 ? complain_overflow_signed
7742 : complain_overflow_unsigned),
7743 len, 0, (8 * wordsz),
7747 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7750 printf (" relocation: %8.8lx\n"
7751 " shifted mask: %8.8lx\n"
7752 " shifted/masked reloc: %8.8lx\n"
7753 " result: %8.8lx\n",
7754 relocation, (mask << shift),
7755 ((relocation & mask) << shift), x);
7757 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
7761 /* When performing a relocatable link, the input relocations are
7762 preserved. But, if they reference global symbols, the indices
7763 referenced must be updated. Update all the relocations in
7764 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7767 elf_link_adjust_relocs (bfd *abfd,
7768 Elf_Internal_Shdr *rel_hdr,
7770 struct elf_link_hash_entry **rel_hash)
7773 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7775 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7776 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7777 bfd_vma r_type_mask;
7780 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
7782 swap_in = bed->s->swap_reloc_in;
7783 swap_out = bed->s->swap_reloc_out;
7785 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
7787 swap_in = bed->s->swap_reloca_in;
7788 swap_out = bed->s->swap_reloca_out;
7793 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7796 if (bed->s->arch_size == 32)
7803 r_type_mask = 0xffffffff;
7807 erela = rel_hdr->contents;
7808 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
7810 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7813 if (*rel_hash == NULL)
7816 BFD_ASSERT ((*rel_hash)->indx >= 0);
7818 (*swap_in) (abfd, erela, irela);
7819 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7820 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7821 | (irela[j].r_info & r_type_mask));
7822 (*swap_out) (abfd, irela, erela);
7826 struct elf_link_sort_rela
7832 enum elf_reloc_type_class type;
7833 /* We use this as an array of size int_rels_per_ext_rel. */
7834 Elf_Internal_Rela rela[1];
7838 elf_link_sort_cmp1 (const void *A, const void *B)
7840 const struct elf_link_sort_rela *a = A;
7841 const struct elf_link_sort_rela *b = B;
7842 int relativea, relativeb;
7844 relativea = a->type == reloc_class_relative;
7845 relativeb = b->type == reloc_class_relative;
7847 if (relativea < relativeb)
7849 if (relativea > relativeb)
7851 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
7853 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
7855 if (a->rela->r_offset < b->rela->r_offset)
7857 if (a->rela->r_offset > b->rela->r_offset)
7863 elf_link_sort_cmp2 (const void *A, const void *B)
7865 const struct elf_link_sort_rela *a = A;
7866 const struct elf_link_sort_rela *b = B;
7869 if (a->u.offset < b->u.offset)
7871 if (a->u.offset > b->u.offset)
7873 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
7874 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
7879 if (a->rela->r_offset < b->rela->r_offset)
7881 if (a->rela->r_offset > b->rela->r_offset)
7887 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
7889 asection *dynamic_relocs;
7892 bfd_size_type count, size;
7893 size_t i, ret, sort_elt, ext_size;
7894 bfd_byte *sort, *s_non_relative, *p;
7895 struct elf_link_sort_rela *sq;
7896 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7897 int i2e = bed->s->int_rels_per_ext_rel;
7898 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7899 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7900 struct bfd_link_order *lo;
7902 bfd_boolean use_rela;
7904 /* Find a dynamic reloc section. */
7905 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
7906 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
7907 if (rela_dyn != NULL && rela_dyn->size > 0
7908 && rel_dyn != NULL && rel_dyn->size > 0)
7910 bfd_boolean use_rela_initialised = FALSE;
7912 /* This is just here to stop gcc from complaining.
7913 It's initialization checking code is not perfect. */
7916 /* Both sections are present. Examine the sizes
7917 of the indirect sections to help us choose. */
7918 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7919 if (lo->type == bfd_indirect_link_order)
7921 asection *o = lo->u.indirect.section;
7923 if ((o->size % bed->s->sizeof_rela) == 0)
7925 if ((o->size % bed->s->sizeof_rel) == 0)
7926 /* Section size is divisible by both rel and rela sizes.
7927 It is of no help to us. */
7931 /* Section size is only divisible by rela. */
7932 if (use_rela_initialised && (use_rela == FALSE))
7935 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7936 bfd_set_error (bfd_error_invalid_operation);
7942 use_rela_initialised = TRUE;
7946 else if ((o->size % bed->s->sizeof_rel) == 0)
7948 /* Section size is only divisible by rel. */
7949 if (use_rela_initialised && (use_rela == TRUE))
7952 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7953 bfd_set_error (bfd_error_invalid_operation);
7959 use_rela_initialised = TRUE;
7964 /* The section size is not divisible by either - something is wrong. */
7966 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
7967 bfd_set_error (bfd_error_invalid_operation);
7972 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7973 if (lo->type == bfd_indirect_link_order)
7975 asection *o = lo->u.indirect.section;
7977 if ((o->size % bed->s->sizeof_rela) == 0)
7979 if ((o->size % bed->s->sizeof_rel) == 0)
7980 /* Section size is divisible by both rel and rela sizes.
7981 It is of no help to us. */
7985 /* Section size is only divisible by rela. */
7986 if (use_rela_initialised && (use_rela == FALSE))
7989 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7990 bfd_set_error (bfd_error_invalid_operation);
7996 use_rela_initialised = TRUE;
8000 else if ((o->size % bed->s->sizeof_rel) == 0)
8002 /* Section size is only divisible by rel. */
8003 if (use_rela_initialised && (use_rela == TRUE))
8006 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8007 bfd_set_error (bfd_error_invalid_operation);
8013 use_rela_initialised = TRUE;
8018 /* The section size is not divisible by either - something is wrong. */
8020 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8021 bfd_set_error (bfd_error_invalid_operation);
8026 if (! use_rela_initialised)
8030 else if (rela_dyn != NULL && rela_dyn->size > 0)
8032 else if (rel_dyn != NULL && rel_dyn->size > 0)
8039 dynamic_relocs = rela_dyn;
8040 ext_size = bed->s->sizeof_rela;
8041 swap_in = bed->s->swap_reloca_in;
8042 swap_out = bed->s->swap_reloca_out;
8046 dynamic_relocs = rel_dyn;
8047 ext_size = bed->s->sizeof_rel;
8048 swap_in = bed->s->swap_reloc_in;
8049 swap_out = bed->s->swap_reloc_out;
8053 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8054 if (lo->type == bfd_indirect_link_order)
8055 size += lo->u.indirect.section->size;
8057 if (size != dynamic_relocs->size)
8060 sort_elt = (sizeof (struct elf_link_sort_rela)
8061 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8063 count = dynamic_relocs->size / ext_size;
8064 sort = bfd_zmalloc (sort_elt * count);
8068 (*info->callbacks->warning)
8069 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8073 if (bed->s->arch_size == 32)
8074 r_sym_mask = ~(bfd_vma) 0xff;
8076 r_sym_mask = ~(bfd_vma) 0xffffffff;
8078 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8079 if (lo->type == bfd_indirect_link_order)
8081 bfd_byte *erel, *erelend;
8082 asection *o = lo->u.indirect.section;
8084 if (o->contents == NULL && o->size != 0)
8086 /* This is a reloc section that is being handled as a normal
8087 section. See bfd_section_from_shdr. We can't combine
8088 relocs in this case. */
8093 erelend = o->contents + o->size;
8094 p = sort + o->output_offset / ext_size * sort_elt;
8096 while (erel < erelend)
8098 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8100 (*swap_in) (abfd, erel, s->rela);
8101 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8102 s->u.sym_mask = r_sym_mask;
8108 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8110 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8112 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8113 if (s->type != reloc_class_relative)
8119 sq = (struct elf_link_sort_rela *) s_non_relative;
8120 for (; i < count; i++, p += sort_elt)
8122 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8123 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8125 sp->u.offset = sq->rela->r_offset;
8128 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8130 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8131 if (lo->type == bfd_indirect_link_order)
8133 bfd_byte *erel, *erelend;
8134 asection *o = lo->u.indirect.section;
8137 erelend = o->contents + o->size;
8138 p = sort + o->output_offset / ext_size * sort_elt;
8139 while (erel < erelend)
8141 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8142 (*swap_out) (abfd, s->rela, erel);
8149 *psec = dynamic_relocs;
8153 /* Flush the output symbols to the file. */
8156 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
8157 const struct elf_backend_data *bed)
8159 if (finfo->symbuf_count > 0)
8161 Elf_Internal_Shdr *hdr;
8165 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
8166 pos = hdr->sh_offset + hdr->sh_size;
8167 amt = finfo->symbuf_count * bed->s->sizeof_sym;
8168 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
8169 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
8172 hdr->sh_size += amt;
8173 finfo->symbuf_count = 0;
8179 /* Add a symbol to the output symbol table. */
8182 elf_link_output_sym (struct elf_final_link_info *finfo,
8184 Elf_Internal_Sym *elfsym,
8185 asection *input_sec,
8186 struct elf_link_hash_entry *h)
8189 Elf_External_Sym_Shndx *destshndx;
8190 bfd_boolean (*output_symbol_hook)
8191 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8192 struct elf_link_hash_entry *);
8193 const struct elf_backend_data *bed;
8195 bed = get_elf_backend_data (finfo->output_bfd);
8196 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8197 if (output_symbol_hook != NULL)
8199 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
8203 if (name == NULL || *name == '\0')
8204 elfsym->st_name = 0;
8205 else if (input_sec->flags & SEC_EXCLUDE)
8206 elfsym->st_name = 0;
8209 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
8211 if (elfsym->st_name == (unsigned long) -1)
8215 if (finfo->symbuf_count >= finfo->symbuf_size)
8217 if (! elf_link_flush_output_syms (finfo, bed))
8221 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
8222 destshndx = finfo->symshndxbuf;
8223 if (destshndx != NULL)
8225 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
8229 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
8230 destshndx = bfd_realloc (destshndx, amt * 2);
8231 if (destshndx == NULL)
8233 finfo->symshndxbuf = destshndx;
8234 memset ((char *) destshndx + amt, 0, amt);
8235 finfo->shndxbuf_size *= 2;
8237 destshndx += bfd_get_symcount (finfo->output_bfd);
8240 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
8241 finfo->symbuf_count += 1;
8242 bfd_get_symcount (finfo->output_bfd) += 1;
8247 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8250 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8252 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8253 && sym->st_shndx < SHN_LORESERVE)
8255 /* The gABI doesn't support dynamic symbols in output sections
8257 (*_bfd_error_handler)
8258 (_("%B: Too many sections: %d (>= %d)"),
8259 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8260 bfd_set_error (bfd_error_nonrepresentable_section);
8266 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8267 allowing an unsatisfied unversioned symbol in the DSO to match a
8268 versioned symbol that would normally require an explicit version.
8269 We also handle the case that a DSO references a hidden symbol
8270 which may be satisfied by a versioned symbol in another DSO. */
8273 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8274 const struct elf_backend_data *bed,
8275 struct elf_link_hash_entry *h)
8278 struct elf_link_loaded_list *loaded;
8280 if (!is_elf_hash_table (info->hash))
8283 switch (h->root.type)
8289 case bfd_link_hash_undefined:
8290 case bfd_link_hash_undefweak:
8291 abfd = h->root.u.undef.abfd;
8292 if ((abfd->flags & DYNAMIC) == 0
8293 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8297 case bfd_link_hash_defined:
8298 case bfd_link_hash_defweak:
8299 abfd = h->root.u.def.section->owner;
8302 case bfd_link_hash_common:
8303 abfd = h->root.u.c.p->section->owner;
8306 BFD_ASSERT (abfd != NULL);
8308 for (loaded = elf_hash_table (info)->loaded;
8310 loaded = loaded->next)
8313 Elf_Internal_Shdr *hdr;
8314 bfd_size_type symcount;
8315 bfd_size_type extsymcount;
8316 bfd_size_type extsymoff;
8317 Elf_Internal_Shdr *versymhdr;
8318 Elf_Internal_Sym *isym;
8319 Elf_Internal_Sym *isymend;
8320 Elf_Internal_Sym *isymbuf;
8321 Elf_External_Versym *ever;
8322 Elf_External_Versym *extversym;
8324 input = loaded->abfd;
8326 /* We check each DSO for a possible hidden versioned definition. */
8328 || (input->flags & DYNAMIC) == 0
8329 || elf_dynversym (input) == 0)
8332 hdr = &elf_tdata (input)->dynsymtab_hdr;
8334 symcount = hdr->sh_size / bed->s->sizeof_sym;
8335 if (elf_bad_symtab (input))
8337 extsymcount = symcount;
8342 extsymcount = symcount - hdr->sh_info;
8343 extsymoff = hdr->sh_info;
8346 if (extsymcount == 0)
8349 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8351 if (isymbuf == NULL)
8354 /* Read in any version definitions. */
8355 versymhdr = &elf_tdata (input)->dynversym_hdr;
8356 extversym = bfd_malloc (versymhdr->sh_size);
8357 if (extversym == NULL)
8360 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8361 || (bfd_bread (extversym, versymhdr->sh_size, input)
8362 != versymhdr->sh_size))
8370 ever = extversym + extsymoff;
8371 isymend = isymbuf + extsymcount;
8372 for (isym = isymbuf; isym < isymend; isym++, ever++)
8375 Elf_Internal_Versym iver;
8376 unsigned short version_index;
8378 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8379 || isym->st_shndx == SHN_UNDEF)
8382 name = bfd_elf_string_from_elf_section (input,
8385 if (strcmp (name, h->root.root.string) != 0)
8388 _bfd_elf_swap_versym_in (input, ever, &iver);
8390 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
8392 /* If we have a non-hidden versioned sym, then it should
8393 have provided a definition for the undefined sym. */
8397 version_index = iver.vs_vers & VERSYM_VERSION;
8398 if (version_index == 1 || version_index == 2)
8400 /* This is the base or first version. We can use it. */
8414 /* Add an external symbol to the symbol table. This is called from
8415 the hash table traversal routine. When generating a shared object,
8416 we go through the symbol table twice. The first time we output
8417 anything that might have been forced to local scope in a version
8418 script. The second time we output the symbols that are still
8422 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
8424 struct elf_outext_info *eoinfo = data;
8425 struct elf_final_link_info *finfo = eoinfo->finfo;
8427 Elf_Internal_Sym sym;
8428 asection *input_sec;
8429 const struct elf_backend_data *bed;
8431 if (h->root.type == bfd_link_hash_warning)
8433 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8434 if (h->root.type == bfd_link_hash_new)
8438 /* Decide whether to output this symbol in this pass. */
8439 if (eoinfo->localsyms)
8441 if (!h->forced_local)
8446 if (h->forced_local)
8450 bed = get_elf_backend_data (finfo->output_bfd);
8452 if (h->root.type == bfd_link_hash_undefined)
8454 /* If we have an undefined symbol reference here then it must have
8455 come from a shared library that is being linked in. (Undefined
8456 references in regular files have already been handled). */
8457 bfd_boolean ignore_undef = FALSE;
8459 /* Some symbols may be special in that the fact that they're
8460 undefined can be safely ignored - let backend determine that. */
8461 if (bed->elf_backend_ignore_undef_symbol)
8462 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8464 /* If we are reporting errors for this situation then do so now. */
8465 if (ignore_undef == FALSE
8468 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
8469 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
8471 if (! (finfo->info->callbacks->undefined_symbol
8472 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
8473 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
8475 eoinfo->failed = TRUE;
8481 /* We should also warn if a forced local symbol is referenced from
8482 shared libraries. */
8483 if (! finfo->info->relocatable
8484 && (! finfo->info->shared)
8489 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
8491 (*_bfd_error_handler)
8492 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
8494 h->root.u.def.section == bfd_abs_section_ptr
8495 ? finfo->output_bfd : h->root.u.def.section->owner,
8496 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
8498 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
8499 ? "hidden" : "local",
8500 h->root.root.string);
8501 eoinfo->failed = TRUE;
8505 /* We don't want to output symbols that have never been mentioned by
8506 a regular file, or that we have been told to strip. However, if
8507 h->indx is set to -2, the symbol is used by a reloc and we must
8511 else if ((h->def_dynamic
8513 || h->root.type == bfd_link_hash_new)
8517 else if (finfo->info->strip == strip_all)
8519 else if (finfo->info->strip == strip_some
8520 && bfd_hash_lookup (finfo->info->keep_hash,
8521 h->root.root.string, FALSE, FALSE) == NULL)
8523 else if (finfo->info->strip_discarded
8524 && (h->root.type == bfd_link_hash_defined
8525 || h->root.type == bfd_link_hash_defweak)
8526 && elf_discarded_section (h->root.u.def.section))
8531 /* If we're stripping it, and it's not a dynamic symbol, there's
8532 nothing else to do unless it is a forced local symbol. */
8535 && !h->forced_local)
8539 sym.st_size = h->size;
8540 sym.st_other = h->other;
8541 if (h->forced_local)
8542 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8543 else if (h->root.type == bfd_link_hash_undefweak
8544 || h->root.type == bfd_link_hash_defweak)
8545 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8547 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
8549 switch (h->root.type)
8552 case bfd_link_hash_new:
8553 case bfd_link_hash_warning:
8557 case bfd_link_hash_undefined:
8558 case bfd_link_hash_undefweak:
8559 input_sec = bfd_und_section_ptr;
8560 sym.st_shndx = SHN_UNDEF;
8563 case bfd_link_hash_defined:
8564 case bfd_link_hash_defweak:
8566 input_sec = h->root.u.def.section;
8567 if (input_sec->output_section != NULL)
8570 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
8571 input_sec->output_section);
8572 if (sym.st_shndx == SHN_BAD)
8574 (*_bfd_error_handler)
8575 (_("%B: could not find output section %A for input section %A"),
8576 finfo->output_bfd, input_sec->output_section, input_sec);
8577 eoinfo->failed = TRUE;
8581 /* ELF symbols in relocatable files are section relative,
8582 but in nonrelocatable files they are virtual
8584 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8585 if (! finfo->info->relocatable)
8587 sym.st_value += input_sec->output_section->vma;
8588 if (h->type == STT_TLS)
8590 asection *tls_sec = elf_hash_table (finfo->info)->tls_sec;
8591 if (tls_sec != NULL)
8592 sym.st_value -= tls_sec->vma;
8595 /* The TLS section may have been garbage collected. */
8596 BFD_ASSERT (finfo->info->gc_sections
8597 && !input_sec->gc_mark);
8604 BFD_ASSERT (input_sec->owner == NULL
8605 || (input_sec->owner->flags & DYNAMIC) != 0);
8606 sym.st_shndx = SHN_UNDEF;
8607 input_sec = bfd_und_section_ptr;
8612 case bfd_link_hash_common:
8613 input_sec = h->root.u.c.p->section;
8614 sym.st_shndx = bed->common_section_index (input_sec);
8615 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8618 case bfd_link_hash_indirect:
8619 /* These symbols are created by symbol versioning. They point
8620 to the decorated version of the name. For example, if the
8621 symbol foo@@GNU_1.2 is the default, which should be used when
8622 foo is used with no version, then we add an indirect symbol
8623 foo which points to foo@@GNU_1.2. We ignore these symbols,
8624 since the indirected symbol is already in the hash table. */
8628 /* Give the processor backend a chance to tweak the symbol value,
8629 and also to finish up anything that needs to be done for this
8630 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8631 forced local syms when non-shared is due to a historical quirk. */
8632 if ((h->dynindx != -1
8634 && ((finfo->info->shared
8635 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8636 || h->root.type != bfd_link_hash_undefweak))
8637 || !h->forced_local)
8638 && elf_hash_table (finfo->info)->dynamic_sections_created)
8640 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8641 (finfo->output_bfd, finfo->info, h, &sym)))
8643 eoinfo->failed = TRUE;
8648 /* If we are marking the symbol as undefined, and there are no
8649 non-weak references to this symbol from a regular object, then
8650 mark the symbol as weak undefined; if there are non-weak
8651 references, mark the symbol as strong. We can't do this earlier,
8652 because it might not be marked as undefined until the
8653 finish_dynamic_symbol routine gets through with it. */
8654 if (sym.st_shndx == SHN_UNDEF
8656 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8657 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8661 if (h->ref_regular_nonweak)
8662 bindtype = STB_GLOBAL;
8664 bindtype = STB_WEAK;
8665 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
8668 /* If this is a symbol defined in a dynamic library, don't use the
8669 symbol size from the dynamic library. Relinking an executable
8670 against a new library may introduce gratuitous changes in the
8671 executable's symbols if we keep the size. */
8672 if (sym.st_shndx == SHN_UNDEF
8677 /* If a non-weak symbol with non-default visibility is not defined
8678 locally, it is a fatal error. */
8679 if (! finfo->info->relocatable
8680 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8681 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8682 && h->root.type == bfd_link_hash_undefined
8685 (*_bfd_error_handler)
8686 (_("%B: %s symbol `%s' isn't defined"),
8688 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
8690 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
8691 ? "internal" : "hidden",
8692 h->root.root.string);
8693 eoinfo->failed = TRUE;
8697 /* If this symbol should be put in the .dynsym section, then put it
8698 there now. We already know the symbol index. We also fill in
8699 the entry in the .hash section. */
8700 if (h->dynindx != -1
8701 && elf_hash_table (finfo->info)->dynamic_sections_created)
8705 sym.st_name = h->dynstr_index;
8706 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
8707 if (! check_dynsym (finfo->output_bfd, &sym))
8709 eoinfo->failed = TRUE;
8712 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8714 if (finfo->hash_sec != NULL)
8716 size_t hash_entry_size;
8717 bfd_byte *bucketpos;
8722 bucketcount = elf_hash_table (finfo->info)->bucketcount;
8723 bucket = h->u.elf_hash_value % bucketcount;
8726 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8727 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8728 + (bucket + 2) * hash_entry_size);
8729 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8730 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8731 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8732 ((bfd_byte *) finfo->hash_sec->contents
8733 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8736 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8738 Elf_Internal_Versym iversym;
8739 Elf_External_Versym *eversym;
8741 if (!h->def_regular)
8743 if (h->verinfo.verdef == NULL)
8744 iversym.vs_vers = 0;
8746 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8750 if (h->verinfo.vertree == NULL)
8751 iversym.vs_vers = 1;
8753 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8754 if (finfo->info->create_default_symver)
8759 iversym.vs_vers |= VERSYM_HIDDEN;
8761 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8762 eversym += h->dynindx;
8763 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8767 /* If we're stripping it, then it was just a dynamic symbol, and
8768 there's nothing else to do. */
8769 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8772 h->indx = bfd_get_symcount (finfo->output_bfd);
8774 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
8776 eoinfo->failed = TRUE;
8783 /* Return TRUE if special handling is done for relocs in SEC against
8784 symbols defined in discarded sections. */
8787 elf_section_ignore_discarded_relocs (asection *sec)
8789 const struct elf_backend_data *bed;
8791 switch (sec->sec_info_type)
8793 case ELF_INFO_TYPE_STABS:
8794 case ELF_INFO_TYPE_EH_FRAME:
8800 bed = get_elf_backend_data (sec->owner);
8801 if (bed->elf_backend_ignore_discarded_relocs != NULL
8802 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8808 /* Return a mask saying how ld should treat relocations in SEC against
8809 symbols defined in discarded sections. If this function returns
8810 COMPLAIN set, ld will issue a warning message. If this function
8811 returns PRETEND set, and the discarded section was link-once and the
8812 same size as the kept link-once section, ld will pretend that the
8813 symbol was actually defined in the kept section. Otherwise ld will
8814 zero the reloc (at least that is the intent, but some cooperation by
8815 the target dependent code is needed, particularly for REL targets). */
8818 _bfd_elf_default_action_discarded (asection *sec)
8820 if (sec->flags & SEC_DEBUGGING)
8823 if (strcmp (".eh_frame", sec->name) == 0)
8826 if (strcmp (".gcc_except_table", sec->name) == 0)
8829 return COMPLAIN | PRETEND;
8832 /* Find a match between a section and a member of a section group. */
8835 match_group_member (asection *sec, asection *group,
8836 struct bfd_link_info *info)
8838 asection *first = elf_next_in_group (group);
8839 asection *s = first;
8843 if (bfd_elf_match_symbols_in_sections (s, sec, info))
8846 s = elf_next_in_group (s);
8854 /* Check if the kept section of a discarded section SEC can be used
8855 to replace it. Return the replacement if it is OK. Otherwise return
8859 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
8863 kept = sec->kept_section;
8866 if ((kept->flags & SEC_GROUP) != 0)
8867 kept = match_group_member (sec, kept, info);
8869 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
8870 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
8872 sec->kept_section = kept;
8877 /* Link an input file into the linker output file. This function
8878 handles all the sections and relocations of the input file at once.
8879 This is so that we only have to read the local symbols once, and
8880 don't have to keep them in memory. */
8883 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
8885 int (*relocate_section)
8886 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
8887 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
8889 Elf_Internal_Shdr *symtab_hdr;
8892 Elf_Internal_Sym *isymbuf;
8893 Elf_Internal_Sym *isym;
8894 Elf_Internal_Sym *isymend;
8896 asection **ppsection;
8898 const struct elf_backend_data *bed;
8899 struct elf_link_hash_entry **sym_hashes;
8901 output_bfd = finfo->output_bfd;
8902 bed = get_elf_backend_data (output_bfd);
8903 relocate_section = bed->elf_backend_relocate_section;
8905 /* If this is a dynamic object, we don't want to do anything here:
8906 we don't want the local symbols, and we don't want the section
8908 if ((input_bfd->flags & DYNAMIC) != 0)
8911 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8912 if (elf_bad_symtab (input_bfd))
8914 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8919 locsymcount = symtab_hdr->sh_info;
8920 extsymoff = symtab_hdr->sh_info;
8923 /* Read the local symbols. */
8924 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8925 if (isymbuf == NULL && locsymcount != 0)
8927 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8928 finfo->internal_syms,
8929 finfo->external_syms,
8930 finfo->locsym_shndx);
8931 if (isymbuf == NULL)
8935 /* Find local symbol sections and adjust values of symbols in
8936 SEC_MERGE sections. Write out those local symbols we know are
8937 going into the output file. */
8938 isymend = isymbuf + locsymcount;
8939 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
8941 isym++, pindex++, ppsection++)
8945 Elf_Internal_Sym osym;
8949 if (elf_bad_symtab (input_bfd))
8951 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
8958 if (isym->st_shndx == SHN_UNDEF)
8959 isec = bfd_und_section_ptr;
8960 else if (isym->st_shndx == SHN_ABS)
8961 isec = bfd_abs_section_ptr;
8962 else if (isym->st_shndx == SHN_COMMON)
8963 isec = bfd_com_section_ptr;
8966 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
8969 /* Don't attempt to output symbols with st_shnx in the
8970 reserved range other than SHN_ABS and SHN_COMMON. */
8974 else if (isec->sec_info_type == ELF_INFO_TYPE_MERGE
8975 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
8977 _bfd_merged_section_offset (output_bfd, &isec,
8978 elf_section_data (isec)->sec_info,
8984 /* Don't output the first, undefined, symbol. */
8985 if (ppsection == finfo->sections)
8988 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
8990 /* We never output section symbols. Instead, we use the
8991 section symbol of the corresponding section in the output
8996 /* If we are stripping all symbols, we don't want to output this
8998 if (finfo->info->strip == strip_all)
9001 /* If we are discarding all local symbols, we don't want to
9002 output this one. If we are generating a relocatable output
9003 file, then some of the local symbols may be required by
9004 relocs; we output them below as we discover that they are
9006 if (finfo->info->discard == discard_all)
9009 /* If this symbol is defined in a section which we are
9010 discarding, we don't need to keep it. */
9011 if (isym->st_shndx != SHN_UNDEF
9012 && isym->st_shndx < SHN_LORESERVE
9013 && bfd_section_removed_from_list (output_bfd,
9014 isec->output_section))
9017 /* Get the name of the symbol. */
9018 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9023 /* See if we are discarding symbols with this name. */
9024 if ((finfo->info->strip == strip_some
9025 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
9027 || (((finfo->info->discard == discard_sec_merge
9028 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
9029 || finfo->info->discard == discard_l)
9030 && bfd_is_local_label_name (input_bfd, name)))
9033 /* If we get here, we are going to output this symbol. */
9037 /* Adjust the section index for the output file. */
9038 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9039 isec->output_section);
9040 if (osym.st_shndx == SHN_BAD)
9043 *pindex = bfd_get_symcount (output_bfd);
9045 /* ELF symbols in relocatable files are section relative, but
9046 in executable files they are virtual addresses. Note that
9047 this code assumes that all ELF sections have an associated
9048 BFD section with a reasonable value for output_offset; below
9049 we assume that they also have a reasonable value for
9050 output_section. Any special sections must be set up to meet
9051 these requirements. */
9052 osym.st_value += isec->output_offset;
9053 if (! finfo->info->relocatable)
9055 osym.st_value += isec->output_section->vma;
9056 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9058 /* STT_TLS symbols are relative to PT_TLS segment base. */
9059 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
9060 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
9064 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
9068 /* Relocate the contents of each section. */
9069 sym_hashes = elf_sym_hashes (input_bfd);
9070 for (o = input_bfd->sections; o != NULL; o = o->next)
9074 if (! o->linker_mark)
9076 /* This section was omitted from the link. */
9080 if (finfo->info->relocatable
9081 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9083 /* Deal with the group signature symbol. */
9084 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9085 unsigned long symndx = sec_data->this_hdr.sh_info;
9086 asection *osec = o->output_section;
9088 if (symndx >= locsymcount
9089 || (elf_bad_symtab (input_bfd)
9090 && finfo->sections[symndx] == NULL))
9092 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9093 while (h->root.type == bfd_link_hash_indirect
9094 || h->root.type == bfd_link_hash_warning)
9095 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9096 /* Arrange for symbol to be output. */
9098 elf_section_data (osec)->this_hdr.sh_info = -2;
9100 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9102 /* We'll use the output section target_index. */
9103 asection *sec = finfo->sections[symndx]->output_section;
9104 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9108 if (finfo->indices[symndx] == -1)
9110 /* Otherwise output the local symbol now. */
9111 Elf_Internal_Sym sym = isymbuf[symndx];
9112 asection *sec = finfo->sections[symndx]->output_section;
9115 name = bfd_elf_string_from_elf_section (input_bfd,
9116 symtab_hdr->sh_link,
9121 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9123 if (sym.st_shndx == SHN_BAD)
9126 sym.st_value += o->output_offset;
9128 finfo->indices[symndx] = bfd_get_symcount (output_bfd);
9129 if (! elf_link_output_sym (finfo, name, &sym, o, NULL))
9132 elf_section_data (osec)->this_hdr.sh_info
9133 = finfo->indices[symndx];
9137 if ((o->flags & SEC_HAS_CONTENTS) == 0
9138 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9141 if ((o->flags & SEC_LINKER_CREATED) != 0)
9143 /* Section was created by _bfd_elf_link_create_dynamic_sections
9148 /* Get the contents of the section. They have been cached by a
9149 relaxation routine. Note that o is a section in an input
9150 file, so the contents field will not have been set by any of
9151 the routines which work on output files. */
9152 if (elf_section_data (o)->this_hdr.contents != NULL)
9153 contents = elf_section_data (o)->this_hdr.contents;
9156 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
9158 contents = finfo->contents;
9159 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
9163 if ((o->flags & SEC_RELOC) != 0)
9165 Elf_Internal_Rela *internal_relocs;
9166 Elf_Internal_Rela *rel, *relend;
9167 bfd_vma r_type_mask;
9169 int action_discarded;
9172 /* Get the swapped relocs. */
9174 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
9175 finfo->internal_relocs, FALSE);
9176 if (internal_relocs == NULL
9177 && o->reloc_count > 0)
9180 if (bed->s->arch_size == 32)
9187 r_type_mask = 0xffffffff;
9191 action_discarded = -1;
9192 if (!elf_section_ignore_discarded_relocs (o))
9193 action_discarded = (*bed->action_discarded) (o);
9195 /* Run through the relocs evaluating complex reloc symbols and
9196 looking for relocs against symbols from discarded sections
9197 or section symbols from removed link-once sections.
9198 Complain about relocs against discarded sections. Zero
9199 relocs against removed link-once sections. */
9201 rel = internal_relocs;
9202 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9203 for ( ; rel < relend; rel++)
9205 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9206 unsigned int s_type;
9207 asection **ps, *sec;
9208 struct elf_link_hash_entry *h = NULL;
9209 const char *sym_name;
9211 if (r_symndx == STN_UNDEF)
9214 if (r_symndx >= locsymcount
9215 || (elf_bad_symtab (input_bfd)
9216 && finfo->sections[r_symndx] == NULL))
9218 h = sym_hashes[r_symndx - extsymoff];
9220 /* Badly formatted input files can contain relocs that
9221 reference non-existant symbols. Check here so that
9222 we do not seg fault. */
9227 sprintf_vma (buffer, rel->r_info);
9228 (*_bfd_error_handler)
9229 (_("error: %B contains a reloc (0x%s) for section %A "
9230 "that references a non-existent global symbol"),
9231 input_bfd, o, buffer);
9232 bfd_set_error (bfd_error_bad_value);
9236 while (h->root.type == bfd_link_hash_indirect
9237 || h->root.type == bfd_link_hash_warning)
9238 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9243 if (h->root.type == bfd_link_hash_defined
9244 || h->root.type == bfd_link_hash_defweak)
9245 ps = &h->root.u.def.section;
9247 sym_name = h->root.root.string;
9251 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9253 s_type = ELF_ST_TYPE (sym->st_info);
9254 ps = &finfo->sections[r_symndx];
9255 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9259 if (s_type == STT_RELC || s_type == STT_SRELC)
9262 bfd_vma dot = (rel->r_offset
9263 + o->output_offset + o->output_section->vma);
9265 printf ("Encountered a complex symbol!");
9266 printf (" (input_bfd %s, section %s, reloc %ld\n",
9267 input_bfd->filename, o->name, rel - internal_relocs);
9268 printf (" symbol: idx %8.8lx, name %s\n",
9269 r_symndx, sym_name);
9270 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9271 (unsigned long) rel->r_info,
9272 (unsigned long) rel->r_offset);
9274 if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot,
9275 isymbuf, locsymcount, s_type == STT_SRELC))
9278 /* Symbol evaluated OK. Update to absolute value. */
9279 set_symbol_value (input_bfd, isymbuf, locsymcount,
9284 if (action_discarded != -1 && ps != NULL)
9286 /* Complain if the definition comes from a
9287 discarded section. */
9288 if ((sec = *ps) != NULL && elf_discarded_section (sec))
9290 BFD_ASSERT (r_symndx != 0);
9291 if (action_discarded & COMPLAIN)
9292 (*finfo->info->callbacks->einfo)
9293 (_("%X`%s' referenced in section `%A' of %B: "
9294 "defined in discarded section `%A' of %B\n"),
9295 sym_name, o, input_bfd, sec, sec->owner);
9297 /* Try to do the best we can to support buggy old
9298 versions of gcc. Pretend that the symbol is
9299 really defined in the kept linkonce section.
9300 FIXME: This is quite broken. Modifying the
9301 symbol here means we will be changing all later
9302 uses of the symbol, not just in this section. */
9303 if (action_discarded & PRETEND)
9307 kept = _bfd_elf_check_kept_section (sec,
9319 /* Relocate the section by invoking a back end routine.
9321 The back end routine is responsible for adjusting the
9322 section contents as necessary, and (if using Rela relocs
9323 and generating a relocatable output file) adjusting the
9324 reloc addend as necessary.
9326 The back end routine does not have to worry about setting
9327 the reloc address or the reloc symbol index.
9329 The back end routine is given a pointer to the swapped in
9330 internal symbols, and can access the hash table entries
9331 for the external symbols via elf_sym_hashes (input_bfd).
9333 When generating relocatable output, the back end routine
9334 must handle STB_LOCAL/STT_SECTION symbols specially. The
9335 output symbol is going to be a section symbol
9336 corresponding to the output section, which will require
9337 the addend to be adjusted. */
9339 ret = (*relocate_section) (output_bfd, finfo->info,
9340 input_bfd, o, contents,
9348 || finfo->info->relocatable
9349 || finfo->info->emitrelocations)
9351 Elf_Internal_Rela *irela;
9352 Elf_Internal_Rela *irelaend;
9353 bfd_vma last_offset;
9354 struct elf_link_hash_entry **rel_hash;
9355 struct elf_link_hash_entry **rel_hash_list;
9356 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
9357 unsigned int next_erel;
9358 bfd_boolean rela_normal;
9360 input_rel_hdr = &elf_section_data (o)->rel_hdr;
9361 rela_normal = (bed->rela_normal
9362 && (input_rel_hdr->sh_entsize
9363 == bed->s->sizeof_rela));
9365 /* Adjust the reloc addresses and symbol indices. */
9367 irela = internal_relocs;
9368 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
9369 rel_hash = (elf_section_data (o->output_section)->rel_hashes
9370 + elf_section_data (o->output_section)->rel_count
9371 + elf_section_data (o->output_section)->rel_count2);
9372 rel_hash_list = rel_hash;
9373 last_offset = o->output_offset;
9374 if (!finfo->info->relocatable)
9375 last_offset += o->output_section->vma;
9376 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9378 unsigned long r_symndx;
9380 Elf_Internal_Sym sym;
9382 if (next_erel == bed->s->int_rels_per_ext_rel)
9388 irela->r_offset = _bfd_elf_section_offset (output_bfd,
9391 if (irela->r_offset >= (bfd_vma) -2)
9393 /* This is a reloc for a deleted entry or somesuch.
9394 Turn it into an R_*_NONE reloc, at the same
9395 offset as the last reloc. elf_eh_frame.c and
9396 bfd_elf_discard_info rely on reloc offsets
9398 irela->r_offset = last_offset;
9400 irela->r_addend = 0;
9404 irela->r_offset += o->output_offset;
9406 /* Relocs in an executable have to be virtual addresses. */
9407 if (!finfo->info->relocatable)
9408 irela->r_offset += o->output_section->vma;
9410 last_offset = irela->r_offset;
9412 r_symndx = irela->r_info >> r_sym_shift;
9413 if (r_symndx == STN_UNDEF)
9416 if (r_symndx >= locsymcount
9417 || (elf_bad_symtab (input_bfd)
9418 && finfo->sections[r_symndx] == NULL))
9420 struct elf_link_hash_entry *rh;
9423 /* This is a reloc against a global symbol. We
9424 have not yet output all the local symbols, so
9425 we do not know the symbol index of any global
9426 symbol. We set the rel_hash entry for this
9427 reloc to point to the global hash table entry
9428 for this symbol. The symbol index is then
9429 set at the end of bfd_elf_final_link. */
9430 indx = r_symndx - extsymoff;
9431 rh = elf_sym_hashes (input_bfd)[indx];
9432 while (rh->root.type == bfd_link_hash_indirect
9433 || rh->root.type == bfd_link_hash_warning)
9434 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9436 /* Setting the index to -2 tells
9437 elf_link_output_extsym that this symbol is
9439 BFD_ASSERT (rh->indx < 0);
9447 /* This is a reloc against a local symbol. */
9450 sym = isymbuf[r_symndx];
9451 sec = finfo->sections[r_symndx];
9452 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9454 /* I suppose the backend ought to fill in the
9455 section of any STT_SECTION symbol against a
9456 processor specific section. */
9458 if (bfd_is_abs_section (sec))
9460 else if (sec == NULL || sec->owner == NULL)
9462 bfd_set_error (bfd_error_bad_value);
9467 asection *osec = sec->output_section;
9469 /* If we have discarded a section, the output
9470 section will be the absolute section. In
9471 case of discarded SEC_MERGE sections, use
9472 the kept section. relocate_section should
9473 have already handled discarded linkonce
9475 if (bfd_is_abs_section (osec)
9476 && sec->kept_section != NULL
9477 && sec->kept_section->output_section != NULL)
9479 osec = sec->kept_section->output_section;
9480 irela->r_addend -= osec->vma;
9483 if (!bfd_is_abs_section (osec))
9485 r_symndx = osec->target_index;
9488 struct elf_link_hash_table *htab;
9491 htab = elf_hash_table (finfo->info);
9492 oi = htab->text_index_section;
9493 if ((osec->flags & SEC_READONLY) == 0
9494 && htab->data_index_section != NULL)
9495 oi = htab->data_index_section;
9499 irela->r_addend += osec->vma - oi->vma;
9500 r_symndx = oi->target_index;
9504 BFD_ASSERT (r_symndx != 0);
9508 /* Adjust the addend according to where the
9509 section winds up in the output section. */
9511 irela->r_addend += sec->output_offset;
9515 if (finfo->indices[r_symndx] == -1)
9517 unsigned long shlink;
9521 if (finfo->info->strip == strip_all)
9523 /* You can't do ld -r -s. */
9524 bfd_set_error (bfd_error_invalid_operation);
9528 /* This symbol was skipped earlier, but
9529 since it is needed by a reloc, we
9530 must output it now. */
9531 shlink = symtab_hdr->sh_link;
9532 name = (bfd_elf_string_from_elf_section
9533 (input_bfd, shlink, sym.st_name));
9537 osec = sec->output_section;
9539 _bfd_elf_section_from_bfd_section (output_bfd,
9541 if (sym.st_shndx == SHN_BAD)
9544 sym.st_value += sec->output_offset;
9545 if (! finfo->info->relocatable)
9547 sym.st_value += osec->vma;
9548 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9550 /* STT_TLS symbols are relative to PT_TLS
9552 BFD_ASSERT (elf_hash_table (finfo->info)
9554 sym.st_value -= (elf_hash_table (finfo->info)
9559 finfo->indices[r_symndx]
9560 = bfd_get_symcount (output_bfd);
9562 if (! elf_link_output_sym (finfo, name, &sym, sec,
9567 r_symndx = finfo->indices[r_symndx];
9570 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9571 | (irela->r_info & r_type_mask));
9574 /* Swap out the relocs. */
9575 if (input_rel_hdr->sh_size != 0
9576 && !bed->elf_backend_emit_relocs (output_bfd, o,
9582 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
9583 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
9585 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9586 * bed->s->int_rels_per_ext_rel);
9587 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
9588 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9597 /* Write out the modified section contents. */
9598 if (bed->elf_backend_write_section
9599 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
9602 /* Section written out. */
9604 else switch (o->sec_info_type)
9606 case ELF_INFO_TYPE_STABS:
9607 if (! (_bfd_write_section_stabs
9609 &elf_hash_table (finfo->info)->stab_info,
9610 o, &elf_section_data (o)->sec_info, contents)))
9613 case ELF_INFO_TYPE_MERGE:
9614 if (! _bfd_write_merged_section (output_bfd, o,
9615 elf_section_data (o)->sec_info))
9618 case ELF_INFO_TYPE_EH_FRAME:
9620 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
9627 if (! (o->flags & SEC_EXCLUDE)
9628 && ! (o->output_section->flags & SEC_NEVER_LOAD)
9629 && ! bfd_set_section_contents (output_bfd, o->output_section,
9631 (file_ptr) o->output_offset,
9642 /* Generate a reloc when linking an ELF file. This is a reloc
9643 requested by the linker, and does not come from any input file. This
9644 is used to build constructor and destructor tables when linking
9648 elf_reloc_link_order (bfd *output_bfd,
9649 struct bfd_link_info *info,
9650 asection *output_section,
9651 struct bfd_link_order *link_order)
9653 reloc_howto_type *howto;
9657 struct elf_link_hash_entry **rel_hash_ptr;
9658 Elf_Internal_Shdr *rel_hdr;
9659 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9660 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9664 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9667 bfd_set_error (bfd_error_bad_value);
9671 addend = link_order->u.reloc.p->addend;
9673 /* Figure out the symbol index. */
9674 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
9675 + elf_section_data (output_section)->rel_count
9676 + elf_section_data (output_section)->rel_count2);
9677 if (link_order->type == bfd_section_reloc_link_order)
9679 indx = link_order->u.reloc.p->u.section->target_index;
9680 BFD_ASSERT (indx != 0);
9681 *rel_hash_ptr = NULL;
9685 struct elf_link_hash_entry *h;
9687 /* Treat a reloc against a defined symbol as though it were
9688 actually against the section. */
9689 h = ((struct elf_link_hash_entry *)
9690 bfd_wrapped_link_hash_lookup (output_bfd, info,
9691 link_order->u.reloc.p->u.name,
9692 FALSE, FALSE, TRUE));
9694 && (h->root.type == bfd_link_hash_defined
9695 || h->root.type == bfd_link_hash_defweak))
9699 section = h->root.u.def.section;
9700 indx = section->output_section->target_index;
9701 *rel_hash_ptr = NULL;
9702 /* It seems that we ought to add the symbol value to the
9703 addend here, but in practice it has already been added
9704 because it was passed to constructor_callback. */
9705 addend += section->output_section->vma + section->output_offset;
9709 /* Setting the index to -2 tells elf_link_output_extsym that
9710 this symbol is used by a reloc. */
9717 if (! ((*info->callbacks->unattached_reloc)
9718 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
9724 /* If this is an inplace reloc, we must write the addend into the
9726 if (howto->partial_inplace && addend != 0)
9729 bfd_reloc_status_type rstat;
9732 const char *sym_name;
9734 size = bfd_get_reloc_size (howto);
9735 buf = bfd_zmalloc (size);
9738 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
9745 case bfd_reloc_outofrange:
9748 case bfd_reloc_overflow:
9749 if (link_order->type == bfd_section_reloc_link_order)
9750 sym_name = bfd_section_name (output_bfd,
9751 link_order->u.reloc.p->u.section);
9753 sym_name = link_order->u.reloc.p->u.name;
9754 if (! ((*info->callbacks->reloc_overflow)
9755 (info, NULL, sym_name, howto->name, addend, NULL,
9756 NULL, (bfd_vma) 0)))
9763 ok = bfd_set_section_contents (output_bfd, output_section, buf,
9764 link_order->offset, size);
9770 /* The address of a reloc is relative to the section in a
9771 relocatable file, and is a virtual address in an executable
9773 offset = link_order->offset;
9774 if (! info->relocatable)
9775 offset += output_section->vma;
9777 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
9779 irel[i].r_offset = offset;
9781 irel[i].r_addend = 0;
9783 if (bed->s->arch_size == 32)
9784 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
9786 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
9788 rel_hdr = &elf_section_data (output_section)->rel_hdr;
9789 erel = rel_hdr->contents;
9790 if (rel_hdr->sh_type == SHT_REL)
9792 erel += (elf_section_data (output_section)->rel_count
9793 * bed->s->sizeof_rel);
9794 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
9798 irel[0].r_addend = addend;
9799 erel += (elf_section_data (output_section)->rel_count
9800 * bed->s->sizeof_rela);
9801 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
9804 ++elf_section_data (output_section)->rel_count;
9810 /* Get the output vma of the section pointed to by the sh_link field. */
9813 elf_get_linked_section_vma (struct bfd_link_order *p)
9815 Elf_Internal_Shdr **elf_shdrp;
9819 s = p->u.indirect.section;
9820 elf_shdrp = elf_elfsections (s->owner);
9821 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
9822 elfsec = elf_shdrp[elfsec]->sh_link;
9824 The Intel C compiler generates SHT_IA_64_UNWIND with
9825 SHF_LINK_ORDER. But it doesn't set the sh_link or
9826 sh_info fields. Hence we could get the situation
9827 where elfsec is 0. */
9830 const struct elf_backend_data *bed
9831 = get_elf_backend_data (s->owner);
9832 if (bed->link_order_error_handler)
9833 bed->link_order_error_handler
9834 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
9839 s = elf_shdrp[elfsec]->bfd_section;
9840 return s->output_section->vma + s->output_offset;
9845 /* Compare two sections based on the locations of the sections they are
9846 linked to. Used by elf_fixup_link_order. */
9849 compare_link_order (const void * a, const void * b)
9854 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
9855 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
9862 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9863 order as their linked sections. Returns false if this could not be done
9864 because an output section includes both ordered and unordered
9865 sections. Ideally we'd do this in the linker proper. */
9868 elf_fixup_link_order (bfd *abfd, asection *o)
9873 struct bfd_link_order *p;
9875 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9877 struct bfd_link_order **sections;
9878 asection *s, *other_sec, *linkorder_sec;
9882 linkorder_sec = NULL;
9885 for (p = o->map_head.link_order; p != NULL; p = p->next)
9887 if (p->type == bfd_indirect_link_order)
9889 s = p->u.indirect.section;
9891 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
9892 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
9893 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
9894 && elfsec < elf_numsections (sub)
9895 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
9896 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
9910 if (seen_other && seen_linkorder)
9912 if (other_sec && linkorder_sec)
9913 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9915 linkorder_sec->owner, other_sec,
9918 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
9920 bfd_set_error (bfd_error_bad_value);
9925 if (!seen_linkorder)
9928 sections = (struct bfd_link_order **)
9929 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
9930 if (sections == NULL)
9934 for (p = o->map_head.link_order; p != NULL; p = p->next)
9936 sections[seen_linkorder++] = p;
9938 /* Sort the input sections in the order of their linked section. */
9939 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
9940 compare_link_order);
9942 /* Change the offsets of the sections. */
9944 for (n = 0; n < seen_linkorder; n++)
9946 s = sections[n]->u.indirect.section;
9947 offset &= ~(bfd_vma) 0 << s->alignment_power;
9948 s->output_offset = offset;
9949 sections[n]->offset = offset;
9950 offset += sections[n]->size;
9958 /* Do the final step of an ELF link. */
9961 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
9963 bfd_boolean dynamic;
9964 bfd_boolean emit_relocs;
9966 struct elf_final_link_info finfo;
9967 register asection *o;
9968 register struct bfd_link_order *p;
9970 bfd_size_type max_contents_size;
9971 bfd_size_type max_external_reloc_size;
9972 bfd_size_type max_internal_reloc_count;
9973 bfd_size_type max_sym_count;
9974 bfd_size_type max_sym_shndx_count;
9976 Elf_Internal_Sym elfsym;
9978 Elf_Internal_Shdr *symtab_hdr;
9979 Elf_Internal_Shdr *symtab_shndx_hdr;
9980 Elf_Internal_Shdr *symstrtab_hdr;
9981 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9982 struct elf_outext_info eoinfo;
9984 size_t relativecount = 0;
9985 asection *reldyn = 0;
9987 asection *attr_section = NULL;
9988 bfd_vma attr_size = 0;
9989 const char *std_attrs_section;
9991 if (! is_elf_hash_table (info->hash))
9995 abfd->flags |= DYNAMIC;
9997 dynamic = elf_hash_table (info)->dynamic_sections_created;
9998 dynobj = elf_hash_table (info)->dynobj;
10000 emit_relocs = (info->relocatable
10001 || info->emitrelocations);
10004 finfo.output_bfd = abfd;
10005 finfo.symstrtab = _bfd_elf_stringtab_init ();
10006 if (finfo.symstrtab == NULL)
10011 finfo.dynsym_sec = NULL;
10012 finfo.hash_sec = NULL;
10013 finfo.symver_sec = NULL;
10017 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
10018 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
10019 BFD_ASSERT (finfo.dynsym_sec != NULL);
10020 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
10021 /* Note that it is OK if symver_sec is NULL. */
10024 finfo.contents = NULL;
10025 finfo.external_relocs = NULL;
10026 finfo.internal_relocs = NULL;
10027 finfo.external_syms = NULL;
10028 finfo.locsym_shndx = NULL;
10029 finfo.internal_syms = NULL;
10030 finfo.indices = NULL;
10031 finfo.sections = NULL;
10032 finfo.symbuf = NULL;
10033 finfo.symshndxbuf = NULL;
10034 finfo.symbuf_count = 0;
10035 finfo.shndxbuf_size = 0;
10037 /* The object attributes have been merged. Remove the input
10038 sections from the link, and set the contents of the output
10040 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10041 for (o = abfd->sections; o != NULL; o = o->next)
10043 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10044 || strcmp (o->name, ".gnu.attributes") == 0)
10046 for (p = o->map_head.link_order; p != NULL; p = p->next)
10048 asection *input_section;
10050 if (p->type != bfd_indirect_link_order)
10052 input_section = p->u.indirect.section;
10053 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10054 elf_link_input_bfd ignores this section. */
10055 input_section->flags &= ~SEC_HAS_CONTENTS;
10058 attr_size = bfd_elf_obj_attr_size (abfd);
10061 bfd_set_section_size (abfd, o, attr_size);
10063 /* Skip this section later on. */
10064 o->map_head.link_order = NULL;
10067 o->flags |= SEC_EXCLUDE;
10071 /* Count up the number of relocations we will output for each output
10072 section, so that we know the sizes of the reloc sections. We
10073 also figure out some maximum sizes. */
10074 max_contents_size = 0;
10075 max_external_reloc_size = 0;
10076 max_internal_reloc_count = 0;
10078 max_sym_shndx_count = 0;
10080 for (o = abfd->sections; o != NULL; o = o->next)
10082 struct bfd_elf_section_data *esdo = elf_section_data (o);
10083 o->reloc_count = 0;
10085 for (p = o->map_head.link_order; p != NULL; p = p->next)
10087 unsigned int reloc_count = 0;
10088 struct bfd_elf_section_data *esdi = NULL;
10089 unsigned int *rel_count1;
10091 if (p->type == bfd_section_reloc_link_order
10092 || p->type == bfd_symbol_reloc_link_order)
10094 else if (p->type == bfd_indirect_link_order)
10098 sec = p->u.indirect.section;
10099 esdi = elf_section_data (sec);
10101 /* Mark all sections which are to be included in the
10102 link. This will normally be every section. We need
10103 to do this so that we can identify any sections which
10104 the linker has decided to not include. */
10105 sec->linker_mark = TRUE;
10107 if (sec->flags & SEC_MERGE)
10110 if (info->relocatable || info->emitrelocations)
10111 reloc_count = sec->reloc_count;
10112 else if (bed->elf_backend_count_relocs)
10113 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
10115 if (sec->rawsize > max_contents_size)
10116 max_contents_size = sec->rawsize;
10117 if (sec->size > max_contents_size)
10118 max_contents_size = sec->size;
10120 /* We are interested in just local symbols, not all
10122 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10123 && (sec->owner->flags & DYNAMIC) == 0)
10127 if (elf_bad_symtab (sec->owner))
10128 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10129 / bed->s->sizeof_sym);
10131 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10133 if (sym_count > max_sym_count)
10134 max_sym_count = sym_count;
10136 if (sym_count > max_sym_shndx_count
10137 && elf_symtab_shndx (sec->owner) != 0)
10138 max_sym_shndx_count = sym_count;
10140 if ((sec->flags & SEC_RELOC) != 0)
10144 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
10145 if (ext_size > max_external_reloc_size)
10146 max_external_reloc_size = ext_size;
10147 if (sec->reloc_count > max_internal_reloc_count)
10148 max_internal_reloc_count = sec->reloc_count;
10153 if (reloc_count == 0)
10156 o->reloc_count += reloc_count;
10158 /* MIPS may have a mix of REL and RELA relocs on sections.
10159 To support this curious ABI we keep reloc counts in
10160 elf_section_data too. We must be careful to add the
10161 relocations from the input section to the right output
10162 count. FIXME: Get rid of one count. We have
10163 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
10164 rel_count1 = &esdo->rel_count;
10167 bfd_boolean same_size;
10168 bfd_size_type entsize1;
10170 entsize1 = esdi->rel_hdr.sh_entsize;
10171 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
10172 || entsize1 == bed->s->sizeof_rela);
10173 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
10176 rel_count1 = &esdo->rel_count2;
10178 if (esdi->rel_hdr2 != NULL)
10180 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
10181 unsigned int alt_count;
10182 unsigned int *rel_count2;
10184 BFD_ASSERT (entsize2 != entsize1
10185 && (entsize2 == bed->s->sizeof_rel
10186 || entsize2 == bed->s->sizeof_rela));
10188 rel_count2 = &esdo->rel_count2;
10190 rel_count2 = &esdo->rel_count;
10192 /* The following is probably too simplistic if the
10193 backend counts output relocs unusually. */
10194 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
10195 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
10196 *rel_count2 += alt_count;
10197 reloc_count -= alt_count;
10200 *rel_count1 += reloc_count;
10203 if (o->reloc_count > 0)
10204 o->flags |= SEC_RELOC;
10207 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10208 set it (this is probably a bug) and if it is set
10209 assign_section_numbers will create a reloc section. */
10210 o->flags &=~ SEC_RELOC;
10213 /* If the SEC_ALLOC flag is not set, force the section VMA to
10214 zero. This is done in elf_fake_sections as well, but forcing
10215 the VMA to 0 here will ensure that relocs against these
10216 sections are handled correctly. */
10217 if ((o->flags & SEC_ALLOC) == 0
10218 && ! o->user_set_vma)
10222 if (! info->relocatable && merged)
10223 elf_link_hash_traverse (elf_hash_table (info),
10224 _bfd_elf_link_sec_merge_syms, abfd);
10226 /* Figure out the file positions for everything but the symbol table
10227 and the relocs. We set symcount to force assign_section_numbers
10228 to create a symbol table. */
10229 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10230 BFD_ASSERT (! abfd->output_has_begun);
10231 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10234 /* Set sizes, and assign file positions for reloc sections. */
10235 for (o = abfd->sections; o != NULL; o = o->next)
10237 if ((o->flags & SEC_RELOC) != 0)
10239 if (!(_bfd_elf_link_size_reloc_section
10240 (abfd, &elf_section_data (o)->rel_hdr, o)))
10243 if (elf_section_data (o)->rel_hdr2
10244 && !(_bfd_elf_link_size_reloc_section
10245 (abfd, elf_section_data (o)->rel_hdr2, o)))
10249 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10250 to count upwards while actually outputting the relocations. */
10251 elf_section_data (o)->rel_count = 0;
10252 elf_section_data (o)->rel_count2 = 0;
10255 _bfd_elf_assign_file_positions_for_relocs (abfd);
10257 /* We have now assigned file positions for all the sections except
10258 .symtab and .strtab. We start the .symtab section at the current
10259 file position, and write directly to it. We build the .strtab
10260 section in memory. */
10261 bfd_get_symcount (abfd) = 0;
10262 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10263 /* sh_name is set in prep_headers. */
10264 symtab_hdr->sh_type = SHT_SYMTAB;
10265 /* sh_flags, sh_addr and sh_size all start off zero. */
10266 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10267 /* sh_link is set in assign_section_numbers. */
10268 /* sh_info is set below. */
10269 /* sh_offset is set just below. */
10270 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
10272 off = elf_tdata (abfd)->next_file_pos;
10273 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10275 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10276 incorrect. We do not yet know the size of the .symtab section.
10277 We correct next_file_pos below, after we do know the size. */
10279 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10280 continuously seeking to the right position in the file. */
10281 if (! info->keep_memory || max_sym_count < 20)
10282 finfo.symbuf_size = 20;
10284 finfo.symbuf_size = max_sym_count;
10285 amt = finfo.symbuf_size;
10286 amt *= bed->s->sizeof_sym;
10287 finfo.symbuf = bfd_malloc (amt);
10288 if (finfo.symbuf == NULL)
10290 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
10292 /* Wild guess at number of output symbols. realloc'd as needed. */
10293 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10294 finfo.shndxbuf_size = amt;
10295 amt *= sizeof (Elf_External_Sym_Shndx);
10296 finfo.symshndxbuf = bfd_zmalloc (amt);
10297 if (finfo.symshndxbuf == NULL)
10301 /* Start writing out the symbol table. The first symbol is always a
10303 if (info->strip != strip_all
10306 elfsym.st_value = 0;
10307 elfsym.st_size = 0;
10308 elfsym.st_info = 0;
10309 elfsym.st_other = 0;
10310 elfsym.st_shndx = SHN_UNDEF;
10311 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
10316 /* Output a symbol for each section. We output these even if we are
10317 discarding local symbols, since they are used for relocs. These
10318 symbols have no names. We store the index of each one in the
10319 index field of the section, so that we can find it again when
10320 outputting relocs. */
10321 if (info->strip != strip_all
10324 elfsym.st_size = 0;
10325 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10326 elfsym.st_other = 0;
10327 elfsym.st_value = 0;
10328 for (i = 1; i < elf_numsections (abfd); i++)
10330 o = bfd_section_from_elf_index (abfd, i);
10333 o->target_index = bfd_get_symcount (abfd);
10334 elfsym.st_shndx = i;
10335 if (!info->relocatable)
10336 elfsym.st_value = o->vma;
10337 if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
10343 /* Allocate some memory to hold information read in from the input
10345 if (max_contents_size != 0)
10347 finfo.contents = bfd_malloc (max_contents_size);
10348 if (finfo.contents == NULL)
10352 if (max_external_reloc_size != 0)
10354 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
10355 if (finfo.external_relocs == NULL)
10359 if (max_internal_reloc_count != 0)
10361 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10362 amt *= sizeof (Elf_Internal_Rela);
10363 finfo.internal_relocs = bfd_malloc (amt);
10364 if (finfo.internal_relocs == NULL)
10368 if (max_sym_count != 0)
10370 amt = max_sym_count * bed->s->sizeof_sym;
10371 finfo.external_syms = bfd_malloc (amt);
10372 if (finfo.external_syms == NULL)
10375 amt = max_sym_count * sizeof (Elf_Internal_Sym);
10376 finfo.internal_syms = bfd_malloc (amt);
10377 if (finfo.internal_syms == NULL)
10380 amt = max_sym_count * sizeof (long);
10381 finfo.indices = bfd_malloc (amt);
10382 if (finfo.indices == NULL)
10385 amt = max_sym_count * sizeof (asection *);
10386 finfo.sections = bfd_malloc (amt);
10387 if (finfo.sections == NULL)
10391 if (max_sym_shndx_count != 0)
10393 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
10394 finfo.locsym_shndx = bfd_malloc (amt);
10395 if (finfo.locsym_shndx == NULL)
10399 if (elf_hash_table (info)->tls_sec)
10401 bfd_vma base, end = 0;
10404 for (sec = elf_hash_table (info)->tls_sec;
10405 sec && (sec->flags & SEC_THREAD_LOCAL);
10408 bfd_size_type size = sec->size;
10411 && (sec->flags & SEC_HAS_CONTENTS) == 0)
10413 struct bfd_link_order *o = sec->map_tail.link_order;
10415 size = o->offset + o->size;
10417 end = sec->vma + size;
10419 base = elf_hash_table (info)->tls_sec->vma;
10420 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
10421 elf_hash_table (info)->tls_size = end - base;
10424 /* Reorder SHF_LINK_ORDER sections. */
10425 for (o = abfd->sections; o != NULL; o = o->next)
10427 if (!elf_fixup_link_order (abfd, o))
10431 /* Since ELF permits relocations to be against local symbols, we
10432 must have the local symbols available when we do the relocations.
10433 Since we would rather only read the local symbols once, and we
10434 would rather not keep them in memory, we handle all the
10435 relocations for a single input file at the same time.
10437 Unfortunately, there is no way to know the total number of local
10438 symbols until we have seen all of them, and the local symbol
10439 indices precede the global symbol indices. This means that when
10440 we are generating relocatable output, and we see a reloc against
10441 a global symbol, we can not know the symbol index until we have
10442 finished examining all the local symbols to see which ones we are
10443 going to output. To deal with this, we keep the relocations in
10444 memory, and don't output them until the end of the link. This is
10445 an unfortunate waste of memory, but I don't see a good way around
10446 it. Fortunately, it only happens when performing a relocatable
10447 link, which is not the common case. FIXME: If keep_memory is set
10448 we could write the relocs out and then read them again; I don't
10449 know how bad the memory loss will be. */
10451 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10452 sub->output_has_begun = FALSE;
10453 for (o = abfd->sections; o != NULL; o = o->next)
10455 for (p = o->map_head.link_order; p != NULL; p = p->next)
10457 if (p->type == bfd_indirect_link_order
10458 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10459 == bfd_target_elf_flavour)
10460 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10462 if (! sub->output_has_begun)
10464 if (! elf_link_input_bfd (&finfo, sub))
10466 sub->output_has_begun = TRUE;
10469 else if (p->type == bfd_section_reloc_link_order
10470 || p->type == bfd_symbol_reloc_link_order)
10472 if (! elf_reloc_link_order (abfd, info, o, p))
10477 if (! _bfd_default_link_order (abfd, info, o, p))
10483 /* Free symbol buffer if needed. */
10484 if (!info->reduce_memory_overheads)
10486 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10487 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10488 && elf_tdata (sub)->symbuf)
10490 free (elf_tdata (sub)->symbuf);
10491 elf_tdata (sub)->symbuf = NULL;
10495 /* Output any global symbols that got converted to local in a
10496 version script or due to symbol visibility. We do this in a
10497 separate step since ELF requires all local symbols to appear
10498 prior to any global symbols. FIXME: We should only do this if
10499 some global symbols were, in fact, converted to become local.
10500 FIXME: Will this work correctly with the Irix 5 linker? */
10501 eoinfo.failed = FALSE;
10502 eoinfo.finfo = &finfo;
10503 eoinfo.localsyms = TRUE;
10504 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10509 /* If backend needs to output some local symbols not present in the hash
10510 table, do it now. */
10511 if (bed->elf_backend_output_arch_local_syms)
10513 typedef bfd_boolean (*out_sym_func)
10514 (void *, const char *, Elf_Internal_Sym *, asection *,
10515 struct elf_link_hash_entry *);
10517 if (! ((*bed->elf_backend_output_arch_local_syms)
10518 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10522 /* That wrote out all the local symbols. Finish up the symbol table
10523 with the global symbols. Even if we want to strip everything we
10524 can, we still need to deal with those global symbols that got
10525 converted to local in a version script. */
10527 /* The sh_info field records the index of the first non local symbol. */
10528 symtab_hdr->sh_info = bfd_get_symcount (abfd);
10531 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10533 Elf_Internal_Sym sym;
10534 bfd_byte *dynsym = finfo.dynsym_sec->contents;
10535 long last_local = 0;
10537 /* Write out the section symbols for the output sections. */
10538 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
10544 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10547 for (s = abfd->sections; s != NULL; s = s->next)
10553 dynindx = elf_section_data (s)->dynindx;
10556 indx = elf_section_data (s)->this_idx;
10557 BFD_ASSERT (indx > 0);
10558 sym.st_shndx = indx;
10559 if (! check_dynsym (abfd, &sym))
10561 sym.st_value = s->vma;
10562 dest = dynsym + dynindx * bed->s->sizeof_sym;
10563 if (last_local < dynindx)
10564 last_local = dynindx;
10565 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10569 /* Write out the local dynsyms. */
10570 if (elf_hash_table (info)->dynlocal)
10572 struct elf_link_local_dynamic_entry *e;
10573 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
10578 sym.st_size = e->isym.st_size;
10579 sym.st_other = e->isym.st_other;
10581 /* Copy the internal symbol as is.
10582 Note that we saved a word of storage and overwrote
10583 the original st_name with the dynstr_index. */
10586 s = bfd_section_from_elf_index (e->input_bfd,
10591 elf_section_data (s->output_section)->this_idx;
10592 if (! check_dynsym (abfd, &sym))
10594 sym.st_value = (s->output_section->vma
10596 + e->isym.st_value);
10599 if (last_local < e->dynindx)
10600 last_local = e->dynindx;
10602 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
10603 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10607 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
10611 /* We get the global symbols from the hash table. */
10612 eoinfo.failed = FALSE;
10613 eoinfo.localsyms = FALSE;
10614 eoinfo.finfo = &finfo;
10615 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10620 /* If backend needs to output some symbols not present in the hash
10621 table, do it now. */
10622 if (bed->elf_backend_output_arch_syms)
10624 typedef bfd_boolean (*out_sym_func)
10625 (void *, const char *, Elf_Internal_Sym *, asection *,
10626 struct elf_link_hash_entry *);
10628 if (! ((*bed->elf_backend_output_arch_syms)
10629 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10633 /* Flush all symbols to the file. */
10634 if (! elf_link_flush_output_syms (&finfo, bed))
10637 /* Now we know the size of the symtab section. */
10638 off += symtab_hdr->sh_size;
10640 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
10641 if (symtab_shndx_hdr->sh_name != 0)
10643 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
10644 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
10645 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
10646 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
10647 symtab_shndx_hdr->sh_size = amt;
10649 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
10652 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
10653 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
10658 /* Finish up and write out the symbol string table (.strtab)
10660 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
10661 /* sh_name was set in prep_headers. */
10662 symstrtab_hdr->sh_type = SHT_STRTAB;
10663 symstrtab_hdr->sh_flags = 0;
10664 symstrtab_hdr->sh_addr = 0;
10665 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10666 symstrtab_hdr->sh_entsize = 0;
10667 symstrtab_hdr->sh_link = 0;
10668 symstrtab_hdr->sh_info = 0;
10669 /* sh_offset is set just below. */
10670 symstrtab_hdr->sh_addralign = 1;
10672 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10673 elf_tdata (abfd)->next_file_pos = off;
10675 if (bfd_get_symcount (abfd) > 0)
10677 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10678 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10682 /* Adjust the relocs to have the correct symbol indices. */
10683 for (o = abfd->sections; o != NULL; o = o->next)
10685 if ((o->flags & SEC_RELOC) == 0)
10688 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
10689 elf_section_data (o)->rel_count,
10690 elf_section_data (o)->rel_hashes);
10691 if (elf_section_data (o)->rel_hdr2 != NULL)
10692 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
10693 elf_section_data (o)->rel_count2,
10694 (elf_section_data (o)->rel_hashes
10695 + elf_section_data (o)->rel_count));
10697 /* Set the reloc_count field to 0 to prevent write_relocs from
10698 trying to swap the relocs out itself. */
10699 o->reloc_count = 0;
10702 if (dynamic && info->combreloc && dynobj != NULL)
10703 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
10705 /* If we are linking against a dynamic object, or generating a
10706 shared library, finish up the dynamic linking information. */
10709 bfd_byte *dyncon, *dynconend;
10711 /* Fix up .dynamic entries. */
10712 o = bfd_get_section_by_name (dynobj, ".dynamic");
10713 BFD_ASSERT (o != NULL);
10715 dyncon = o->contents;
10716 dynconend = o->contents + o->size;
10717 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10719 Elf_Internal_Dyn dyn;
10723 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10730 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
10732 switch (elf_section_data (reldyn)->this_hdr.sh_type)
10734 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
10735 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
10738 dyn.d_un.d_val = relativecount;
10745 name = info->init_function;
10748 name = info->fini_function;
10751 struct elf_link_hash_entry *h;
10753 h = elf_link_hash_lookup (elf_hash_table (info), name,
10754 FALSE, FALSE, TRUE);
10756 && (h->root.type == bfd_link_hash_defined
10757 || h->root.type == bfd_link_hash_defweak))
10759 dyn.d_un.d_ptr = h->root.u.def.value;
10760 o = h->root.u.def.section;
10761 if (o->output_section != NULL)
10762 dyn.d_un.d_ptr += (o->output_section->vma
10763 + o->output_offset);
10766 /* The symbol is imported from another shared
10767 library and does not apply to this one. */
10768 dyn.d_un.d_ptr = 0;
10775 case DT_PREINIT_ARRAYSZ:
10776 name = ".preinit_array";
10778 case DT_INIT_ARRAYSZ:
10779 name = ".init_array";
10781 case DT_FINI_ARRAYSZ:
10782 name = ".fini_array";
10784 o = bfd_get_section_by_name (abfd, name);
10787 (*_bfd_error_handler)
10788 (_("%B: could not find output section %s"), abfd, name);
10792 (*_bfd_error_handler)
10793 (_("warning: %s section has zero size"), name);
10794 dyn.d_un.d_val = o->size;
10797 case DT_PREINIT_ARRAY:
10798 name = ".preinit_array";
10800 case DT_INIT_ARRAY:
10801 name = ".init_array";
10803 case DT_FINI_ARRAY:
10804 name = ".fini_array";
10811 name = ".gnu.hash";
10820 name = ".gnu.version_d";
10823 name = ".gnu.version_r";
10826 name = ".gnu.version";
10828 o = bfd_get_section_by_name (abfd, name);
10831 (*_bfd_error_handler)
10832 (_("%B: could not find output section %s"), abfd, name);
10835 dyn.d_un.d_ptr = o->vma;
10842 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
10846 dyn.d_un.d_val = 0;
10847 dyn.d_un.d_ptr = 0;
10848 for (i = 1; i < elf_numsections (abfd); i++)
10850 Elf_Internal_Shdr *hdr;
10852 hdr = elf_elfsections (abfd)[i];
10853 if (hdr->sh_type == type
10854 && (hdr->sh_flags & SHF_ALLOC) != 0)
10856 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
10857 dyn.d_un.d_val += hdr->sh_size;
10860 if (dyn.d_un.d_ptr == 0
10861 || hdr->sh_addr < dyn.d_un.d_ptr)
10862 dyn.d_un.d_ptr = hdr->sh_addr;
10868 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
10872 /* If we have created any dynamic sections, then output them. */
10873 if (dynobj != NULL)
10875 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
10878 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10879 if (info->warn_shared_textrel && info->shared)
10881 bfd_byte *dyncon, *dynconend;
10883 /* Fix up .dynamic entries. */
10884 o = bfd_get_section_by_name (dynobj, ".dynamic");
10885 BFD_ASSERT (o != NULL);
10887 dyncon = o->contents;
10888 dynconend = o->contents + o->size;
10889 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10891 Elf_Internal_Dyn dyn;
10893 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10895 if (dyn.d_tag == DT_TEXTREL)
10897 info->callbacks->einfo
10898 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
10904 for (o = dynobj->sections; o != NULL; o = o->next)
10906 if ((o->flags & SEC_HAS_CONTENTS) == 0
10908 || o->output_section == bfd_abs_section_ptr)
10910 if ((o->flags & SEC_LINKER_CREATED) == 0)
10912 /* At this point, we are only interested in sections
10913 created by _bfd_elf_link_create_dynamic_sections. */
10916 if (elf_hash_table (info)->stab_info.stabstr == o)
10918 if (elf_hash_table (info)->eh_info.hdr_sec == o)
10920 if ((elf_section_data (o->output_section)->this_hdr.sh_type
10922 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
10924 if (! bfd_set_section_contents (abfd, o->output_section,
10926 (file_ptr) o->output_offset,
10932 /* The contents of the .dynstr section are actually in a
10934 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
10935 if (bfd_seek (abfd, off, SEEK_SET) != 0
10936 || ! _bfd_elf_strtab_emit (abfd,
10937 elf_hash_table (info)->dynstr))
10943 if (info->relocatable)
10945 bfd_boolean failed = FALSE;
10947 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
10952 /* If we have optimized stabs strings, output them. */
10953 if (elf_hash_table (info)->stab_info.stabstr != NULL)
10955 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
10959 if (info->eh_frame_hdr)
10961 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
10965 if (finfo.symstrtab != NULL)
10966 _bfd_stringtab_free (finfo.symstrtab);
10967 if (finfo.contents != NULL)
10968 free (finfo.contents);
10969 if (finfo.external_relocs != NULL)
10970 free (finfo.external_relocs);
10971 if (finfo.internal_relocs != NULL)
10972 free (finfo.internal_relocs);
10973 if (finfo.external_syms != NULL)
10974 free (finfo.external_syms);
10975 if (finfo.locsym_shndx != NULL)
10976 free (finfo.locsym_shndx);
10977 if (finfo.internal_syms != NULL)
10978 free (finfo.internal_syms);
10979 if (finfo.indices != NULL)
10980 free (finfo.indices);
10981 if (finfo.sections != NULL)
10982 free (finfo.sections);
10983 if (finfo.symbuf != NULL)
10984 free (finfo.symbuf);
10985 if (finfo.symshndxbuf != NULL)
10986 free (finfo.symshndxbuf);
10987 for (o = abfd->sections; o != NULL; o = o->next)
10989 if ((o->flags & SEC_RELOC) != 0
10990 && elf_section_data (o)->rel_hashes != NULL)
10991 free (elf_section_data (o)->rel_hashes);
10994 elf_tdata (abfd)->linker = TRUE;
10998 bfd_byte *contents = bfd_malloc (attr_size);
10999 if (contents == NULL)
11000 return FALSE; /* Bail out and fail. */
11001 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11002 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11009 if (finfo.symstrtab != NULL)
11010 _bfd_stringtab_free (finfo.symstrtab);
11011 if (finfo.contents != NULL)
11012 free (finfo.contents);
11013 if (finfo.external_relocs != NULL)
11014 free (finfo.external_relocs);
11015 if (finfo.internal_relocs != NULL)
11016 free (finfo.internal_relocs);
11017 if (finfo.external_syms != NULL)
11018 free (finfo.external_syms);
11019 if (finfo.locsym_shndx != NULL)
11020 free (finfo.locsym_shndx);
11021 if (finfo.internal_syms != NULL)
11022 free (finfo.internal_syms);
11023 if (finfo.indices != NULL)
11024 free (finfo.indices);
11025 if (finfo.sections != NULL)
11026 free (finfo.sections);
11027 if (finfo.symbuf != NULL)
11028 free (finfo.symbuf);
11029 if (finfo.symshndxbuf != NULL)
11030 free (finfo.symshndxbuf);
11031 for (o = abfd->sections; o != NULL; o = o->next)
11033 if ((o->flags & SEC_RELOC) != 0
11034 && elf_section_data (o)->rel_hashes != NULL)
11035 free (elf_section_data (o)->rel_hashes);
11041 /* Initialize COOKIE for input bfd ABFD. */
11044 init_reloc_cookie (struct elf_reloc_cookie *cookie,
11045 struct bfd_link_info *info, bfd *abfd)
11047 Elf_Internal_Shdr *symtab_hdr;
11048 const struct elf_backend_data *bed;
11050 bed = get_elf_backend_data (abfd);
11051 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11053 cookie->abfd = abfd;
11054 cookie->sym_hashes = elf_sym_hashes (abfd);
11055 cookie->bad_symtab = elf_bad_symtab (abfd);
11056 if (cookie->bad_symtab)
11058 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11059 cookie->extsymoff = 0;
11063 cookie->locsymcount = symtab_hdr->sh_info;
11064 cookie->extsymoff = symtab_hdr->sh_info;
11067 if (bed->s->arch_size == 32)
11068 cookie->r_sym_shift = 8;
11070 cookie->r_sym_shift = 32;
11072 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11073 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11075 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11076 cookie->locsymcount, 0,
11078 if (cookie->locsyms == NULL)
11080 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11083 if (info->keep_memory)
11084 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11089 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11092 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11094 Elf_Internal_Shdr *symtab_hdr;
11096 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11097 if (cookie->locsyms != NULL
11098 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11099 free (cookie->locsyms);
11102 /* Initialize the relocation information in COOKIE for input section SEC
11103 of input bfd ABFD. */
11106 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11107 struct bfd_link_info *info, bfd *abfd,
11110 const struct elf_backend_data *bed;
11112 if (sec->reloc_count == 0)
11114 cookie->rels = NULL;
11115 cookie->relend = NULL;
11119 bed = get_elf_backend_data (abfd);
11121 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11122 info->keep_memory);
11123 if (cookie->rels == NULL)
11125 cookie->rel = cookie->rels;
11126 cookie->relend = (cookie->rels
11127 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11129 cookie->rel = cookie->rels;
11133 /* Free the memory allocated by init_reloc_cookie_rels,
11137 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11140 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11141 free (cookie->rels);
11144 /* Initialize the whole of COOKIE for input section SEC. */
11147 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11148 struct bfd_link_info *info,
11151 if (!init_reloc_cookie (cookie, info, sec->owner))
11153 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11158 fini_reloc_cookie (cookie, sec->owner);
11163 /* Free the memory allocated by init_reloc_cookie_for_section,
11167 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11170 fini_reloc_cookie_rels (cookie, sec);
11171 fini_reloc_cookie (cookie, sec->owner);
11174 /* Garbage collect unused sections. */
11176 /* Default gc_mark_hook. */
11179 _bfd_elf_gc_mark_hook (asection *sec,
11180 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11181 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11182 struct elf_link_hash_entry *h,
11183 Elf_Internal_Sym *sym)
11187 switch (h->root.type)
11189 case bfd_link_hash_defined:
11190 case bfd_link_hash_defweak:
11191 return h->root.u.def.section;
11193 case bfd_link_hash_common:
11194 return h->root.u.c.p->section;
11201 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11206 /* COOKIE->rel describes a relocation against section SEC, which is
11207 a section we've decided to keep. Return the section that contains
11208 the relocation symbol, or NULL if no section contains it. */
11211 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11212 elf_gc_mark_hook_fn gc_mark_hook,
11213 struct elf_reloc_cookie *cookie)
11215 unsigned long r_symndx;
11216 struct elf_link_hash_entry *h;
11218 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
11222 if (r_symndx >= cookie->locsymcount
11223 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11225 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11226 while (h->root.type == bfd_link_hash_indirect
11227 || h->root.type == bfd_link_hash_warning)
11228 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11229 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11232 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11233 &cookie->locsyms[r_symndx]);
11236 /* COOKIE->rel describes a relocation against section SEC, which is
11237 a section we've decided to keep. Mark the section that contains
11238 the relocation symbol. */
11241 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11243 elf_gc_mark_hook_fn gc_mark_hook,
11244 struct elf_reloc_cookie *cookie)
11248 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11249 if (rsec && !rsec->gc_mark)
11251 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
11253 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11259 /* The mark phase of garbage collection. For a given section, mark
11260 it and any sections in this section's group, and all the sections
11261 which define symbols to which it refers. */
11264 _bfd_elf_gc_mark (struct bfd_link_info *info,
11266 elf_gc_mark_hook_fn gc_mark_hook)
11269 asection *group_sec, *eh_frame;
11273 /* Mark all the sections in the group. */
11274 group_sec = elf_section_data (sec)->next_in_group;
11275 if (group_sec && !group_sec->gc_mark)
11276 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
11279 /* Look through the section relocs. */
11281 eh_frame = elf_eh_frame_section (sec->owner);
11282 if ((sec->flags & SEC_RELOC) != 0
11283 && sec->reloc_count > 0
11284 && sec != eh_frame)
11286 struct elf_reloc_cookie cookie;
11288 if (!init_reloc_cookie_for_section (&cookie, info, sec))
11292 for (; cookie.rel < cookie.relend; cookie.rel++)
11293 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
11298 fini_reloc_cookie_for_section (&cookie, sec);
11302 if (ret && eh_frame && elf_fde_list (sec))
11304 struct elf_reloc_cookie cookie;
11306 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11310 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11311 gc_mark_hook, &cookie))
11313 fini_reloc_cookie_for_section (&cookie, eh_frame);
11320 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11322 struct elf_gc_sweep_symbol_info
11324 struct bfd_link_info *info;
11325 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11330 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
11332 if (h->root.type == bfd_link_hash_warning)
11333 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11335 if ((h->root.type == bfd_link_hash_defined
11336 || h->root.type == bfd_link_hash_defweak)
11337 && !h->root.u.def.section->gc_mark
11338 && !(h->root.u.def.section->owner->flags & DYNAMIC))
11340 struct elf_gc_sweep_symbol_info *inf = data;
11341 (*inf->hide_symbol) (inf->info, h, TRUE);
11347 /* The sweep phase of garbage collection. Remove all garbage sections. */
11349 typedef bfd_boolean (*gc_sweep_hook_fn)
11350 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11353 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
11356 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11357 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11358 unsigned long section_sym_count;
11359 struct elf_gc_sweep_symbol_info sweep_info;
11361 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11365 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11368 for (o = sub->sections; o != NULL; o = o->next)
11370 /* Keep debug and special sections. */
11371 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
11372 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
11378 /* Skip sweeping sections already excluded. */
11379 if (o->flags & SEC_EXCLUDE)
11382 /* Since this is early in the link process, it is simple
11383 to remove a section from the output. */
11384 o->flags |= SEC_EXCLUDE;
11386 if (info->print_gc_sections && o->size != 0)
11387 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11389 /* But we also have to update some of the relocation
11390 info we collected before. */
11392 && (o->flags & SEC_RELOC) != 0
11393 && o->reloc_count > 0
11394 && !bfd_is_abs_section (o->output_section))
11396 Elf_Internal_Rela *internal_relocs;
11400 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11401 info->keep_memory);
11402 if (internal_relocs == NULL)
11405 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11407 if (elf_section_data (o)->relocs != internal_relocs)
11408 free (internal_relocs);
11416 /* Remove the symbols that were in the swept sections from the dynamic
11417 symbol table. GCFIXME: Anyone know how to get them out of the
11418 static symbol table as well? */
11419 sweep_info.info = info;
11420 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11421 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11424 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
11428 /* Propagate collected vtable information. This is called through
11429 elf_link_hash_traverse. */
11432 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11434 if (h->root.type == bfd_link_hash_warning)
11435 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11437 /* Those that are not vtables. */
11438 if (h->vtable == NULL || h->vtable->parent == NULL)
11441 /* Those vtables that do not have parents, we cannot merge. */
11442 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
11445 /* If we've already been done, exit. */
11446 if (h->vtable->used && h->vtable->used[-1])
11449 /* Make sure the parent's table is up to date. */
11450 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
11452 if (h->vtable->used == NULL)
11454 /* None of this table's entries were referenced. Re-use the
11456 h->vtable->used = h->vtable->parent->vtable->used;
11457 h->vtable->size = h->vtable->parent->vtable->size;
11462 bfd_boolean *cu, *pu;
11464 /* Or the parent's entries into ours. */
11465 cu = h->vtable->used;
11467 pu = h->vtable->parent->vtable->used;
11470 const struct elf_backend_data *bed;
11471 unsigned int log_file_align;
11473 bed = get_elf_backend_data (h->root.u.def.section->owner);
11474 log_file_align = bed->s->log_file_align;
11475 n = h->vtable->parent->vtable->size >> log_file_align;
11490 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
11493 bfd_vma hstart, hend;
11494 Elf_Internal_Rela *relstart, *relend, *rel;
11495 const struct elf_backend_data *bed;
11496 unsigned int log_file_align;
11498 if (h->root.type == bfd_link_hash_warning)
11499 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11501 /* Take care of both those symbols that do not describe vtables as
11502 well as those that are not loaded. */
11503 if (h->vtable == NULL || h->vtable->parent == NULL)
11506 BFD_ASSERT (h->root.type == bfd_link_hash_defined
11507 || h->root.type == bfd_link_hash_defweak);
11509 sec = h->root.u.def.section;
11510 hstart = h->root.u.def.value;
11511 hend = hstart + h->size;
11513 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
11515 return *(bfd_boolean *) okp = FALSE;
11516 bed = get_elf_backend_data (sec->owner);
11517 log_file_align = bed->s->log_file_align;
11519 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11521 for (rel = relstart; rel < relend; ++rel)
11522 if (rel->r_offset >= hstart && rel->r_offset < hend)
11524 /* If the entry is in use, do nothing. */
11525 if (h->vtable->used
11526 && (rel->r_offset - hstart) < h->vtable->size)
11528 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
11529 if (h->vtable->used[entry])
11532 /* Otherwise, kill it. */
11533 rel->r_offset = rel->r_info = rel->r_addend = 0;
11539 /* Mark sections containing dynamically referenced symbols. When
11540 building shared libraries, we must assume that any visible symbol is
11544 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
11546 struct bfd_link_info *info = (struct bfd_link_info *) inf;
11548 if (h->root.type == bfd_link_hash_warning)
11549 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11551 if ((h->root.type == bfd_link_hash_defined
11552 || h->root.type == bfd_link_hash_defweak)
11554 || (!info->executable
11556 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
11557 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
11558 h->root.u.def.section->flags |= SEC_KEEP;
11563 /* Keep all sections containing symbols undefined on the command-line,
11564 and the section containing the entry symbol. */
11567 _bfd_elf_gc_keep (struct bfd_link_info *info)
11569 struct bfd_sym_chain *sym;
11571 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
11573 struct elf_link_hash_entry *h;
11575 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
11576 FALSE, FALSE, FALSE);
11579 && (h->root.type == bfd_link_hash_defined
11580 || h->root.type == bfd_link_hash_defweak)
11581 && !bfd_is_abs_section (h->root.u.def.section))
11582 h->root.u.def.section->flags |= SEC_KEEP;
11586 /* Do mark and sweep of unused sections. */
11589 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
11591 bfd_boolean ok = TRUE;
11593 elf_gc_mark_hook_fn gc_mark_hook;
11594 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11596 if (!bed->can_gc_sections
11597 || !is_elf_hash_table (info->hash))
11599 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
11603 bed->gc_keep (info);
11605 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
11606 at the .eh_frame section if we can mark the FDEs individually. */
11607 _bfd_elf_begin_eh_frame_parsing (info);
11608 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11611 struct elf_reloc_cookie cookie;
11613 sec = bfd_get_section_by_name (sub, ".eh_frame");
11614 if (sec && init_reloc_cookie_for_section (&cookie, info, sec))
11616 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
11617 if (elf_section_data (sec)->sec_info)
11618 elf_eh_frame_section (sub) = sec;
11619 fini_reloc_cookie_for_section (&cookie, sec);
11622 _bfd_elf_end_eh_frame_parsing (info);
11624 /* Apply transitive closure to the vtable entry usage info. */
11625 elf_link_hash_traverse (elf_hash_table (info),
11626 elf_gc_propagate_vtable_entries_used,
11631 /* Kill the vtable relocations that were not used. */
11632 elf_link_hash_traverse (elf_hash_table (info),
11633 elf_gc_smash_unused_vtentry_relocs,
11638 /* Mark dynamically referenced symbols. */
11639 if (elf_hash_table (info)->dynamic_sections_created)
11640 elf_link_hash_traverse (elf_hash_table (info),
11641 bed->gc_mark_dynamic_ref,
11644 /* Grovel through relocs to find out who stays ... */
11645 gc_mark_hook = bed->gc_mark_hook;
11646 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11650 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11653 for (o = sub->sections; o != NULL; o = o->next)
11654 if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark)
11655 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
11659 /* Allow the backend to mark additional target specific sections. */
11660 if (bed->gc_mark_extra_sections)
11661 bed->gc_mark_extra_sections (info, gc_mark_hook);
11663 /* ... and mark SEC_EXCLUDE for those that go. */
11664 return elf_gc_sweep (abfd, info);
11667 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
11670 bfd_elf_gc_record_vtinherit (bfd *abfd,
11672 struct elf_link_hash_entry *h,
11675 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
11676 struct elf_link_hash_entry **search, *child;
11677 bfd_size_type extsymcount;
11678 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11680 /* The sh_info field of the symtab header tells us where the
11681 external symbols start. We don't care about the local symbols at
11683 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
11684 if (!elf_bad_symtab (abfd))
11685 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
11687 sym_hashes = elf_sym_hashes (abfd);
11688 sym_hashes_end = sym_hashes + extsymcount;
11690 /* Hunt down the child symbol, which is in this section at the same
11691 offset as the relocation. */
11692 for (search = sym_hashes; search != sym_hashes_end; ++search)
11694 if ((child = *search) != NULL
11695 && (child->root.type == bfd_link_hash_defined
11696 || child->root.type == bfd_link_hash_defweak)
11697 && child->root.u.def.section == sec
11698 && child->root.u.def.value == offset)
11702 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
11703 abfd, sec, (unsigned long) offset);
11704 bfd_set_error (bfd_error_invalid_operation);
11708 if (!child->vtable)
11710 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
11711 if (!child->vtable)
11716 /* This *should* only be the absolute section. It could potentially
11717 be that someone has defined a non-global vtable though, which
11718 would be bad. It isn't worth paging in the local symbols to be
11719 sure though; that case should simply be handled by the assembler. */
11721 child->vtable->parent = (struct elf_link_hash_entry *) -1;
11724 child->vtable->parent = h;
11729 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
11732 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
11733 asection *sec ATTRIBUTE_UNUSED,
11734 struct elf_link_hash_entry *h,
11737 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11738 unsigned int log_file_align = bed->s->log_file_align;
11742 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
11747 if (addend >= h->vtable->size)
11749 size_t size, bytes, file_align;
11750 bfd_boolean *ptr = h->vtable->used;
11752 /* While the symbol is undefined, we have to be prepared to handle
11754 file_align = 1 << log_file_align;
11755 if (h->root.type == bfd_link_hash_undefined)
11756 size = addend + file_align;
11760 if (addend >= size)
11762 /* Oops! We've got a reference past the defined end of
11763 the table. This is probably a bug -- shall we warn? */
11764 size = addend + file_align;
11767 size = (size + file_align - 1) & -file_align;
11769 /* Allocate one extra entry for use as a "done" flag for the
11770 consolidation pass. */
11771 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
11775 ptr = bfd_realloc (ptr - 1, bytes);
11781 oldbytes = (((h->vtable->size >> log_file_align) + 1)
11782 * sizeof (bfd_boolean));
11783 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
11787 ptr = bfd_zmalloc (bytes);
11792 /* And arrange for that done flag to be at index -1. */
11793 h->vtable->used = ptr + 1;
11794 h->vtable->size = size;
11797 h->vtable->used[addend >> log_file_align] = TRUE;
11802 struct alloc_got_off_arg {
11804 struct bfd_link_info *info;
11807 /* We need a special top-level link routine to convert got reference counts
11808 to real got offsets. */
11811 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
11813 struct alloc_got_off_arg *gofarg = arg;
11814 bfd *obfd = gofarg->info->output_bfd;
11815 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
11817 if (h->root.type == bfd_link_hash_warning)
11818 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11820 if (h->got.refcount > 0)
11822 h->got.offset = gofarg->gotoff;
11823 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
11826 h->got.offset = (bfd_vma) -1;
11831 /* And an accompanying bit to work out final got entry offsets once
11832 we're done. Should be called from final_link. */
11835 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
11836 struct bfd_link_info *info)
11839 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11841 struct alloc_got_off_arg gofarg;
11843 BFD_ASSERT (abfd == info->output_bfd);
11845 if (! is_elf_hash_table (info->hash))
11848 /* The GOT offset is relative to the .got section, but the GOT header is
11849 put into the .got.plt section, if the backend uses it. */
11850 if (bed->want_got_plt)
11853 gotoff = bed->got_header_size;
11855 /* Do the local .got entries first. */
11856 for (i = info->input_bfds; i; i = i->link_next)
11858 bfd_signed_vma *local_got;
11859 bfd_size_type j, locsymcount;
11860 Elf_Internal_Shdr *symtab_hdr;
11862 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
11865 local_got = elf_local_got_refcounts (i);
11869 symtab_hdr = &elf_tdata (i)->symtab_hdr;
11870 if (elf_bad_symtab (i))
11871 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11873 locsymcount = symtab_hdr->sh_info;
11875 for (j = 0; j < locsymcount; ++j)
11877 if (local_got[j] > 0)
11879 local_got[j] = gotoff;
11880 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
11883 local_got[j] = (bfd_vma) -1;
11887 /* Then the global .got entries. .plt refcounts are handled by
11888 adjust_dynamic_symbol */
11889 gofarg.gotoff = gotoff;
11890 gofarg.info = info;
11891 elf_link_hash_traverse (elf_hash_table (info),
11892 elf_gc_allocate_got_offsets,
11897 /* Many folk need no more in the way of final link than this, once
11898 got entry reference counting is enabled. */
11901 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
11903 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
11906 /* Invoke the regular ELF backend linker to do all the work. */
11907 return bfd_elf_final_link (abfd, info);
11911 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
11913 struct elf_reloc_cookie *rcookie = cookie;
11915 if (rcookie->bad_symtab)
11916 rcookie->rel = rcookie->rels;
11918 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
11920 unsigned long r_symndx;
11922 if (! rcookie->bad_symtab)
11923 if (rcookie->rel->r_offset > offset)
11925 if (rcookie->rel->r_offset != offset)
11928 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
11929 if (r_symndx == SHN_UNDEF)
11932 if (r_symndx >= rcookie->locsymcount
11933 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11935 struct elf_link_hash_entry *h;
11937 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
11939 while (h->root.type == bfd_link_hash_indirect
11940 || h->root.type == bfd_link_hash_warning)
11941 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11943 if ((h->root.type == bfd_link_hash_defined
11944 || h->root.type == bfd_link_hash_defweak)
11945 && elf_discarded_section (h->root.u.def.section))
11952 /* It's not a relocation against a global symbol,
11953 but it could be a relocation against a local
11954 symbol for a discarded section. */
11956 Elf_Internal_Sym *isym;
11958 /* Need to: get the symbol; get the section. */
11959 isym = &rcookie->locsyms[r_symndx];
11960 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
11961 if (isec != NULL && elf_discarded_section (isec))
11969 /* Discard unneeded references to discarded sections.
11970 Returns TRUE if any section's size was changed. */
11971 /* This function assumes that the relocations are in sorted order,
11972 which is true for all known assemblers. */
11975 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
11977 struct elf_reloc_cookie cookie;
11978 asection *stab, *eh;
11979 const struct elf_backend_data *bed;
11981 bfd_boolean ret = FALSE;
11983 if (info->traditional_format
11984 || !is_elf_hash_table (info->hash))
11987 _bfd_elf_begin_eh_frame_parsing (info);
11988 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
11990 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
11993 bed = get_elf_backend_data (abfd);
11995 if ((abfd->flags & DYNAMIC) != 0)
11999 if (!info->relocatable)
12001 eh = bfd_get_section_by_name (abfd, ".eh_frame");
12004 || bfd_is_abs_section (eh->output_section)))
12008 stab = bfd_get_section_by_name (abfd, ".stab");
12010 && (stab->size == 0
12011 || bfd_is_abs_section (stab->output_section)
12012 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
12017 && bed->elf_backend_discard_info == NULL)
12020 if (!init_reloc_cookie (&cookie, info, abfd))
12024 && stab->reloc_count > 0
12025 && init_reloc_cookie_rels (&cookie, info, abfd, stab))
12027 if (_bfd_discard_section_stabs (abfd, stab,
12028 elf_section_data (stab)->sec_info,
12029 bfd_elf_reloc_symbol_deleted_p,
12032 fini_reloc_cookie_rels (&cookie, stab);
12036 && init_reloc_cookie_rels (&cookie, info, abfd, eh))
12038 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
12039 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
12040 bfd_elf_reloc_symbol_deleted_p,
12043 fini_reloc_cookie_rels (&cookie, eh);
12046 if (bed->elf_backend_discard_info != NULL
12047 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
12050 fini_reloc_cookie (&cookie, abfd);
12052 _bfd_elf_end_eh_frame_parsing (info);
12054 if (info->eh_frame_hdr
12055 && !info->relocatable
12056 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12062 /* For a SHT_GROUP section, return the group signature. For other
12063 sections, return the normal section name. */
12065 static const char *
12066 section_signature (asection *sec)
12068 if ((sec->flags & SEC_GROUP) != 0
12069 && elf_next_in_group (sec) != NULL
12070 && elf_group_name (elf_next_in_group (sec)) != NULL)
12071 return elf_group_name (elf_next_in_group (sec));
12076 _bfd_elf_section_already_linked (bfd *abfd, asection *sec,
12077 struct bfd_link_info *info)
12080 const char *name, *p;
12081 struct bfd_section_already_linked *l;
12082 struct bfd_section_already_linked_hash_entry *already_linked_list;
12084 if (sec->output_section == bfd_abs_section_ptr)
12087 flags = sec->flags;
12089 /* Return if it isn't a linkonce section. A comdat group section
12090 also has SEC_LINK_ONCE set. */
12091 if ((flags & SEC_LINK_ONCE) == 0)
12094 /* Don't put group member sections on our list of already linked
12095 sections. They are handled as a group via their group section. */
12096 if (elf_sec_group (sec) != NULL)
12099 /* FIXME: When doing a relocatable link, we may have trouble
12100 copying relocations in other sections that refer to local symbols
12101 in the section being discarded. Those relocations will have to
12102 be converted somehow; as of this writing I'm not sure that any of
12103 the backends handle that correctly.
12105 It is tempting to instead not discard link once sections when
12106 doing a relocatable link (technically, they should be discarded
12107 whenever we are building constructors). However, that fails,
12108 because the linker winds up combining all the link once sections
12109 into a single large link once section, which defeats the purpose
12110 of having link once sections in the first place.
12112 Also, not merging link once sections in a relocatable link
12113 causes trouble for MIPS ELF, which relies on link once semantics
12114 to handle the .reginfo section correctly. */
12116 name = section_signature (sec);
12118 if (CONST_STRNEQ (name, ".gnu.linkonce.")
12119 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12124 already_linked_list = bfd_section_already_linked_table_lookup (p);
12126 for (l = already_linked_list->entry; l != NULL; l = l->next)
12128 /* We may have 2 different types of sections on the list: group
12129 sections and linkonce sections. Match like sections. */
12130 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
12131 && strcmp (name, section_signature (l->sec)) == 0
12132 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
12134 /* The section has already been linked. See if we should
12135 issue a warning. */
12136 switch (flags & SEC_LINK_DUPLICATES)
12141 case SEC_LINK_DUPLICATES_DISCARD:
12144 case SEC_LINK_DUPLICATES_ONE_ONLY:
12145 (*_bfd_error_handler)
12146 (_("%B: ignoring duplicate section `%A'"),
12150 case SEC_LINK_DUPLICATES_SAME_SIZE:
12151 if (sec->size != l->sec->size)
12152 (*_bfd_error_handler)
12153 (_("%B: duplicate section `%A' has different size"),
12157 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
12158 if (sec->size != l->sec->size)
12159 (*_bfd_error_handler)
12160 (_("%B: duplicate section `%A' has different size"),
12162 else if (sec->size != 0)
12164 bfd_byte *sec_contents, *l_sec_contents;
12166 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
12167 (*_bfd_error_handler)
12168 (_("%B: warning: could not read contents of section `%A'"),
12170 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
12172 (*_bfd_error_handler)
12173 (_("%B: warning: could not read contents of section `%A'"),
12174 l->sec->owner, l->sec);
12175 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
12176 (*_bfd_error_handler)
12177 (_("%B: warning: duplicate section `%A' has different contents"),
12181 free (sec_contents);
12182 if (l_sec_contents)
12183 free (l_sec_contents);
12188 /* Set the output_section field so that lang_add_section
12189 does not create a lang_input_section structure for this
12190 section. Since there might be a symbol in the section
12191 being discarded, we must retain a pointer to the section
12192 which we are really going to use. */
12193 sec->output_section = bfd_abs_section_ptr;
12194 sec->kept_section = l->sec;
12196 if (flags & SEC_GROUP)
12198 asection *first = elf_next_in_group (sec);
12199 asection *s = first;
12203 s->output_section = bfd_abs_section_ptr;
12204 /* Record which group discards it. */
12205 s->kept_section = l->sec;
12206 s = elf_next_in_group (s);
12207 /* These lists are circular. */
12217 /* A single member comdat group section may be discarded by a
12218 linkonce section and vice versa. */
12220 if ((flags & SEC_GROUP) != 0)
12222 asection *first = elf_next_in_group (sec);
12224 if (first != NULL && elf_next_in_group (first) == first)
12225 /* Check this single member group against linkonce sections. */
12226 for (l = already_linked_list->entry; l != NULL; l = l->next)
12227 if ((l->sec->flags & SEC_GROUP) == 0
12228 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
12229 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12231 first->output_section = bfd_abs_section_ptr;
12232 first->kept_section = l->sec;
12233 sec->output_section = bfd_abs_section_ptr;
12238 /* Check this linkonce section against single member groups. */
12239 for (l = already_linked_list->entry; l != NULL; l = l->next)
12240 if (l->sec->flags & SEC_GROUP)
12242 asection *first = elf_next_in_group (l->sec);
12245 && elf_next_in_group (first) == first
12246 && bfd_elf_match_symbols_in_sections (first, sec, info))
12248 sec->output_section = bfd_abs_section_ptr;
12249 sec->kept_section = first;
12254 /* This is the first section with this name. Record it. */
12255 if (! bfd_section_already_linked_table_insert (already_linked_list, sec))
12256 info->callbacks->einfo (_("%F%P: already_linked_table: %E"));
12260 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
12262 return sym->st_shndx == SHN_COMMON;
12266 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12272 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12274 return bfd_com_section_ptr;
12278 _bfd_elf_default_got_elt_size (bfd *abfd,
12279 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12280 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
12281 bfd *ibfd ATTRIBUTE_UNUSED,
12282 unsigned long symndx ATTRIBUTE_UNUSED)
12284 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12285 return bed->s->arch_size / 8;
12288 /* Routines to support the creation of dynamic relocs. */
12290 /* Return true if NAME is a name of a relocation
12291 section associated with section S. */
12294 is_reloc_section (bfd_boolean rela, const char * name, asection * s)
12297 return CONST_STRNEQ (name, ".rela")
12298 && strcmp (bfd_get_section_name (NULL, s), name + 5) == 0;
12300 return CONST_STRNEQ (name, ".rel")
12301 && strcmp (bfd_get_section_name (NULL, s), name + 4) == 0;
12304 /* Returns the name of the dynamic reloc section associated with SEC. */
12306 static const char *
12307 get_dynamic_reloc_section_name (bfd * abfd,
12309 bfd_boolean is_rela)
12312 unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
12313 unsigned int shnam = elf_section_data (sec)->rel_hdr.sh_name;
12315 name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
12319 if (! is_reloc_section (is_rela, name, sec))
12321 static bfd_boolean complained = FALSE;
12325 (*_bfd_error_handler)
12326 (_("%B: bad relocation section name `%s\'"), abfd, name);
12335 /* Returns the dynamic reloc section associated with SEC.
12336 If necessary compute the name of the dynamic reloc section based
12337 on SEC's name (looked up in ABFD's string table) and the setting
12341 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
12343 bfd_boolean is_rela)
12345 asection * reloc_sec = elf_section_data (sec)->sreloc;
12347 if (reloc_sec == NULL)
12349 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12353 reloc_sec = bfd_get_section_by_name (abfd, name);
12355 if (reloc_sec != NULL)
12356 elf_section_data (sec)->sreloc = reloc_sec;
12363 /* Returns the dynamic reloc section associated with SEC. If the
12364 section does not exist it is created and attached to the DYNOBJ
12365 bfd and stored in the SRELOC field of SEC's elf_section_data
12368 ALIGNMENT is the alignment for the newly created section and
12369 IS_RELA defines whether the name should be .rela.<SEC's name>
12370 or .rel.<SEC's name>. The section name is looked up in the
12371 string table associated with ABFD. */
12374 _bfd_elf_make_dynamic_reloc_section (asection * sec,
12376 unsigned int alignment,
12378 bfd_boolean is_rela)
12380 asection * reloc_sec = elf_section_data (sec)->sreloc;
12382 if (reloc_sec == NULL)
12384 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12389 reloc_sec = bfd_get_section_by_name (dynobj, name);
12391 if (reloc_sec == NULL)
12395 flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_IN_MEMORY | SEC_LINKER_CREATED);
12396 if ((sec->flags & SEC_ALLOC) != 0)
12397 flags |= SEC_ALLOC | SEC_LOAD;
12399 reloc_sec = bfd_make_section_with_flags (dynobj, name, flags);
12400 if (reloc_sec != NULL)
12402 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
12407 elf_section_data (sec)->sreloc = reloc_sec;