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;
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
2020 for (t = sinfo->verdefs; t != NULL; t = t->next)
2022 if (t->globals.list != NULL)
2024 bfd_boolean matched;
2028 while ((d = (*t->match) (&t->globals, d,
2029 h->root.root.string)) != NULL)
2034 /* There is a version without definition. Make
2035 the symbol the default definition for this
2037 h->verinfo.vertree = t;
2045 /* There is no undefined version for this symbol. Hide the
2047 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2050 if (t->locals.list != NULL)
2053 while ((d = (*t->match) (&t->locals, d,
2054 h->root.root.string)) != NULL)
2057 /* If the match is "*", keep looking for a more
2058 explicit, perhaps even global, match.
2059 XXX: Shouldn't this be !d->wildcard instead? */
2060 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
2069 if (local_ver != NULL)
2071 h->verinfo.vertree = local_ver;
2072 if (h->dynindx != -1
2073 && ! info->export_dynamic)
2075 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2083 /* Read and swap the relocs from the section indicated by SHDR. This
2084 may be either a REL or a RELA section. The relocations are
2085 translated into RELA relocations and stored in INTERNAL_RELOCS,
2086 which should have already been allocated to contain enough space.
2087 The EXTERNAL_RELOCS are a buffer where the external form of the
2088 relocations should be stored.
2090 Returns FALSE if something goes wrong. */
2093 elf_link_read_relocs_from_section (bfd *abfd,
2095 Elf_Internal_Shdr *shdr,
2096 void *external_relocs,
2097 Elf_Internal_Rela *internal_relocs)
2099 const struct elf_backend_data *bed;
2100 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2101 const bfd_byte *erela;
2102 const bfd_byte *erelaend;
2103 Elf_Internal_Rela *irela;
2104 Elf_Internal_Shdr *symtab_hdr;
2107 /* Position ourselves at the start of the section. */
2108 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2111 /* Read the relocations. */
2112 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2115 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2116 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
2118 bed = get_elf_backend_data (abfd);
2120 /* Convert the external relocations to the internal format. */
2121 if (shdr->sh_entsize == bed->s->sizeof_rel)
2122 swap_in = bed->s->swap_reloc_in;
2123 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2124 swap_in = bed->s->swap_reloca_in;
2127 bfd_set_error (bfd_error_wrong_format);
2131 erela = external_relocs;
2132 erelaend = erela + shdr->sh_size;
2133 irela = internal_relocs;
2134 while (erela < erelaend)
2138 (*swap_in) (abfd, erela, irela);
2139 r_symndx = ELF32_R_SYM (irela->r_info);
2140 if (bed->s->arch_size == 64)
2142 if ((size_t) r_symndx >= nsyms)
2144 (*_bfd_error_handler)
2145 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2146 " for offset 0x%lx in section `%A'"),
2148 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2149 bfd_set_error (bfd_error_bad_value);
2152 irela += bed->s->int_rels_per_ext_rel;
2153 erela += shdr->sh_entsize;
2159 /* Read and swap the relocs for a section O. They may have been
2160 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2161 not NULL, they are used as buffers to read into. They are known to
2162 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2163 the return value is allocated using either malloc or bfd_alloc,
2164 according to the KEEP_MEMORY argument. If O has two relocation
2165 sections (both REL and RELA relocations), then the REL_HDR
2166 relocations will appear first in INTERNAL_RELOCS, followed by the
2167 REL_HDR2 relocations. */
2170 _bfd_elf_link_read_relocs (bfd *abfd,
2172 void *external_relocs,
2173 Elf_Internal_Rela *internal_relocs,
2174 bfd_boolean keep_memory)
2176 Elf_Internal_Shdr *rel_hdr;
2177 void *alloc1 = NULL;
2178 Elf_Internal_Rela *alloc2 = NULL;
2179 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2181 if (elf_section_data (o)->relocs != NULL)
2182 return elf_section_data (o)->relocs;
2184 if (o->reloc_count == 0)
2187 rel_hdr = &elf_section_data (o)->rel_hdr;
2189 if (internal_relocs == NULL)
2193 size = o->reloc_count;
2194 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2196 internal_relocs = alloc2 = bfd_alloc (abfd, size);
2198 internal_relocs = alloc2 = bfd_malloc (size);
2199 if (internal_relocs == NULL)
2203 if (external_relocs == NULL)
2205 bfd_size_type size = rel_hdr->sh_size;
2207 if (elf_section_data (o)->rel_hdr2)
2208 size += elf_section_data (o)->rel_hdr2->sh_size;
2209 alloc1 = bfd_malloc (size);
2212 external_relocs = alloc1;
2215 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2219 if (elf_section_data (o)->rel_hdr2
2220 && (!elf_link_read_relocs_from_section
2222 elf_section_data (o)->rel_hdr2,
2223 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2224 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2225 * bed->s->int_rels_per_ext_rel))))
2228 /* Cache the results for next time, if we can. */
2230 elf_section_data (o)->relocs = internal_relocs;
2235 /* Don't free alloc2, since if it was allocated we are passing it
2236 back (under the name of internal_relocs). */
2238 return internal_relocs;
2246 bfd_release (abfd, alloc2);
2253 /* Compute the size of, and allocate space for, REL_HDR which is the
2254 section header for a section containing relocations for O. */
2257 _bfd_elf_link_size_reloc_section (bfd *abfd,
2258 Elf_Internal_Shdr *rel_hdr,
2261 bfd_size_type reloc_count;
2262 bfd_size_type num_rel_hashes;
2264 /* Figure out how many relocations there will be. */
2265 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2266 reloc_count = elf_section_data (o)->rel_count;
2268 reloc_count = elf_section_data (o)->rel_count2;
2270 num_rel_hashes = o->reloc_count;
2271 if (num_rel_hashes < reloc_count)
2272 num_rel_hashes = reloc_count;
2274 /* That allows us to calculate the size of the section. */
2275 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2277 /* The contents field must last into write_object_contents, so we
2278 allocate it with bfd_alloc rather than malloc. Also since we
2279 cannot be sure that the contents will actually be filled in,
2280 we zero the allocated space. */
2281 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2282 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2285 /* We only allocate one set of hash entries, so we only do it the
2286 first time we are called. */
2287 if (elf_section_data (o)->rel_hashes == NULL
2290 struct elf_link_hash_entry **p;
2292 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2296 elf_section_data (o)->rel_hashes = p;
2302 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2303 originated from the section given by INPUT_REL_HDR) to the
2307 _bfd_elf_link_output_relocs (bfd *output_bfd,
2308 asection *input_section,
2309 Elf_Internal_Shdr *input_rel_hdr,
2310 Elf_Internal_Rela *internal_relocs,
2311 struct elf_link_hash_entry **rel_hash
2314 Elf_Internal_Rela *irela;
2315 Elf_Internal_Rela *irelaend;
2317 Elf_Internal_Shdr *output_rel_hdr;
2318 asection *output_section;
2319 unsigned int *rel_countp = NULL;
2320 const struct elf_backend_data *bed;
2321 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2323 output_section = input_section->output_section;
2324 output_rel_hdr = NULL;
2326 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2327 == input_rel_hdr->sh_entsize)
2329 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2330 rel_countp = &elf_section_data (output_section)->rel_count;
2332 else if (elf_section_data (output_section)->rel_hdr2
2333 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2334 == input_rel_hdr->sh_entsize))
2336 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2337 rel_countp = &elf_section_data (output_section)->rel_count2;
2341 (*_bfd_error_handler)
2342 (_("%B: relocation size mismatch in %B section %A"),
2343 output_bfd, input_section->owner, input_section);
2344 bfd_set_error (bfd_error_wrong_format);
2348 bed = get_elf_backend_data (output_bfd);
2349 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2350 swap_out = bed->s->swap_reloc_out;
2351 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2352 swap_out = bed->s->swap_reloca_out;
2356 erel = output_rel_hdr->contents;
2357 erel += *rel_countp * input_rel_hdr->sh_entsize;
2358 irela = internal_relocs;
2359 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2360 * bed->s->int_rels_per_ext_rel);
2361 while (irela < irelaend)
2363 (*swap_out) (output_bfd, irela, erel);
2364 irela += bed->s->int_rels_per_ext_rel;
2365 erel += input_rel_hdr->sh_entsize;
2368 /* Bump the counter, so that we know where to add the next set of
2370 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2375 /* Make weak undefined symbols in PIE dynamic. */
2378 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2379 struct elf_link_hash_entry *h)
2383 && h->root.type == bfd_link_hash_undefweak)
2384 return bfd_elf_link_record_dynamic_symbol (info, h);
2389 /* Fix up the flags for a symbol. This handles various cases which
2390 can only be fixed after all the input files are seen. This is
2391 currently called by both adjust_dynamic_symbol and
2392 assign_sym_version, which is unnecessary but perhaps more robust in
2393 the face of future changes. */
2396 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2397 struct elf_info_failed *eif)
2399 const struct elf_backend_data *bed;
2401 /* If this symbol was mentioned in a non-ELF file, try to set
2402 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2403 permit a non-ELF file to correctly refer to a symbol defined in
2404 an ELF dynamic object. */
2407 while (h->root.type == bfd_link_hash_indirect)
2408 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2410 if (h->root.type != bfd_link_hash_defined
2411 && h->root.type != bfd_link_hash_defweak)
2414 h->ref_regular_nonweak = 1;
2418 if (h->root.u.def.section->owner != NULL
2419 && (bfd_get_flavour (h->root.u.def.section->owner)
2420 == bfd_target_elf_flavour))
2423 h->ref_regular_nonweak = 1;
2429 if (h->dynindx == -1
2433 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2442 /* Unfortunately, NON_ELF is only correct if the symbol
2443 was first seen in a non-ELF file. Fortunately, if the symbol
2444 was first seen in an ELF file, we're probably OK unless the
2445 symbol was defined in a non-ELF file. Catch that case here.
2446 FIXME: We're still in trouble if the symbol was first seen in
2447 a dynamic object, and then later in a non-ELF regular object. */
2448 if ((h->root.type == bfd_link_hash_defined
2449 || h->root.type == bfd_link_hash_defweak)
2451 && (h->root.u.def.section->owner != NULL
2452 ? (bfd_get_flavour (h->root.u.def.section->owner)
2453 != bfd_target_elf_flavour)
2454 : (bfd_is_abs_section (h->root.u.def.section)
2455 && !h->def_dynamic)))
2459 /* Backend specific symbol fixup. */
2460 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2461 if (bed->elf_backend_fixup_symbol
2462 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2465 /* If this is a final link, and the symbol was defined as a common
2466 symbol in a regular object file, and there was no definition in
2467 any dynamic object, then the linker will have allocated space for
2468 the symbol in a common section but the DEF_REGULAR
2469 flag will not have been set. */
2470 if (h->root.type == bfd_link_hash_defined
2474 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2477 /* If -Bsymbolic was used (which means to bind references to global
2478 symbols to the definition within the shared object), and this
2479 symbol was defined in a regular object, then it actually doesn't
2480 need a PLT entry. Likewise, if the symbol has non-default
2481 visibility. If the symbol has hidden or internal visibility, we
2482 will force it local. */
2484 && eif->info->shared
2485 && is_elf_hash_table (eif->info->hash)
2486 && (SYMBOLIC_BIND (eif->info, h)
2487 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2490 bfd_boolean force_local;
2492 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2493 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2494 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2497 /* If a weak undefined symbol has non-default visibility, we also
2498 hide it from the dynamic linker. */
2499 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2500 && h->root.type == bfd_link_hash_undefweak)
2501 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2503 /* If this is a weak defined symbol in a dynamic object, and we know
2504 the real definition in the dynamic object, copy interesting flags
2505 over to the real definition. */
2506 if (h->u.weakdef != NULL)
2508 struct elf_link_hash_entry *weakdef;
2510 weakdef = h->u.weakdef;
2511 if (h->root.type == bfd_link_hash_indirect)
2512 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2514 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2515 || h->root.type == bfd_link_hash_defweak);
2516 BFD_ASSERT (weakdef->def_dynamic);
2518 /* If the real definition is defined by a regular object file,
2519 don't do anything special. See the longer description in
2520 _bfd_elf_adjust_dynamic_symbol, below. */
2521 if (weakdef->def_regular)
2522 h->u.weakdef = NULL;
2525 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2526 || weakdef->root.type == bfd_link_hash_defweak);
2527 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2534 /* Make the backend pick a good value for a dynamic symbol. This is
2535 called via elf_link_hash_traverse, and also calls itself
2539 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2541 struct elf_info_failed *eif = data;
2543 const struct elf_backend_data *bed;
2545 if (! is_elf_hash_table (eif->info->hash))
2548 if (h->root.type == bfd_link_hash_warning)
2550 h->got = elf_hash_table (eif->info)->init_got_offset;
2551 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2553 /* When warning symbols are created, they **replace** the "real"
2554 entry in the hash table, thus we never get to see the real
2555 symbol in a hash traversal. So look at it now. */
2556 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2559 /* Ignore indirect symbols. These are added by the versioning code. */
2560 if (h->root.type == bfd_link_hash_indirect)
2563 /* Fix the symbol flags. */
2564 if (! _bfd_elf_fix_symbol_flags (h, eif))
2567 /* If this symbol does not require a PLT entry, and it is not
2568 defined by a dynamic object, or is not referenced by a regular
2569 object, ignore it. We do have to handle a weak defined symbol,
2570 even if no regular object refers to it, if we decided to add it
2571 to the dynamic symbol table. FIXME: Do we normally need to worry
2572 about symbols which are defined by one dynamic object and
2573 referenced by another one? */
2578 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2580 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2584 /* If we've already adjusted this symbol, don't do it again. This
2585 can happen via a recursive call. */
2586 if (h->dynamic_adjusted)
2589 /* Don't look at this symbol again. Note that we must set this
2590 after checking the above conditions, because we may look at a
2591 symbol once, decide not to do anything, and then get called
2592 recursively later after REF_REGULAR is set below. */
2593 h->dynamic_adjusted = 1;
2595 /* If this is a weak definition, and we know a real definition, and
2596 the real symbol is not itself defined by a regular object file,
2597 then get a good value for the real definition. We handle the
2598 real symbol first, for the convenience of the backend routine.
2600 Note that there is a confusing case here. If the real definition
2601 is defined by a regular object file, we don't get the real symbol
2602 from the dynamic object, but we do get the weak symbol. If the
2603 processor backend uses a COPY reloc, then if some routine in the
2604 dynamic object changes the real symbol, we will not see that
2605 change in the corresponding weak symbol. This is the way other
2606 ELF linkers work as well, and seems to be a result of the shared
2609 I will clarify this issue. Most SVR4 shared libraries define the
2610 variable _timezone and define timezone as a weak synonym. The
2611 tzset call changes _timezone. If you write
2612 extern int timezone;
2614 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2615 you might expect that, since timezone is a synonym for _timezone,
2616 the same number will print both times. However, if the processor
2617 backend uses a COPY reloc, then actually timezone will be copied
2618 into your process image, and, since you define _timezone
2619 yourself, _timezone will not. Thus timezone and _timezone will
2620 wind up at different memory locations. The tzset call will set
2621 _timezone, leaving timezone unchanged. */
2623 if (h->u.weakdef != NULL)
2625 /* If we get to this point, we know there is an implicit
2626 reference by a regular object file via the weak symbol H.
2627 FIXME: Is this really true? What if the traversal finds
2628 H->U.WEAKDEF before it finds H? */
2629 h->u.weakdef->ref_regular = 1;
2631 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2635 /* If a symbol has no type and no size and does not require a PLT
2636 entry, then we are probably about to do the wrong thing here: we
2637 are probably going to create a COPY reloc for an empty object.
2638 This case can arise when a shared object is built with assembly
2639 code, and the assembly code fails to set the symbol type. */
2641 && h->type == STT_NOTYPE
2643 (*_bfd_error_handler)
2644 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2645 h->root.root.string);
2647 dynobj = elf_hash_table (eif->info)->dynobj;
2648 bed = get_elf_backend_data (dynobj);
2649 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2658 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2662 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2665 unsigned int power_of_two;
2667 asection *sec = h->root.u.def.section;
2669 /* The section aligment of definition is the maximum alignment
2670 requirement of symbols defined in the section. Since we don't
2671 know the symbol alignment requirement, we start with the
2672 maximum alignment and check low bits of the symbol address
2673 for the minimum alignment. */
2674 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2675 mask = ((bfd_vma) 1 << power_of_two) - 1;
2676 while ((h->root.u.def.value & mask) != 0)
2682 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2685 /* Adjust the section alignment if needed. */
2686 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2691 /* We make sure that the symbol will be aligned properly. */
2692 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2694 /* Define the symbol as being at this point in DYNBSS. */
2695 h->root.u.def.section = dynbss;
2696 h->root.u.def.value = dynbss->size;
2698 /* Increment the size of DYNBSS to make room for the symbol. */
2699 dynbss->size += h->size;
2704 /* Adjust all external symbols pointing into SEC_MERGE sections
2705 to reflect the object merging within the sections. */
2708 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2712 if (h->root.type == bfd_link_hash_warning)
2713 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2715 if ((h->root.type == bfd_link_hash_defined
2716 || h->root.type == bfd_link_hash_defweak)
2717 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2718 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2720 bfd *output_bfd = data;
2722 h->root.u.def.value =
2723 _bfd_merged_section_offset (output_bfd,
2724 &h->root.u.def.section,
2725 elf_section_data (sec)->sec_info,
2726 h->root.u.def.value);
2732 /* Returns false if the symbol referred to by H should be considered
2733 to resolve local to the current module, and true if it should be
2734 considered to bind dynamically. */
2737 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2738 struct bfd_link_info *info,
2739 bfd_boolean ignore_protected)
2741 bfd_boolean binding_stays_local_p;
2742 const struct elf_backend_data *bed;
2743 struct elf_link_hash_table *hash_table;
2748 while (h->root.type == bfd_link_hash_indirect
2749 || h->root.type == bfd_link_hash_warning)
2750 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2752 /* If it was forced local, then clearly it's not dynamic. */
2753 if (h->dynindx == -1)
2755 if (h->forced_local)
2758 /* Identify the cases where name binding rules say that a
2759 visible symbol resolves locally. */
2760 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2762 switch (ELF_ST_VISIBILITY (h->other))
2769 hash_table = elf_hash_table (info);
2770 if (!is_elf_hash_table (hash_table))
2773 bed = get_elf_backend_data (hash_table->dynobj);
2775 /* Proper resolution for function pointer equality may require
2776 that these symbols perhaps be resolved dynamically, even though
2777 we should be resolving them to the current module. */
2778 if (!ignore_protected || !bed->is_function_type (h->type))
2779 binding_stays_local_p = TRUE;
2786 /* If it isn't defined locally, then clearly it's dynamic. */
2787 if (!h->def_regular)
2790 /* Otherwise, the symbol is dynamic if binding rules don't tell
2791 us that it remains local. */
2792 return !binding_stays_local_p;
2795 /* Return true if the symbol referred to by H should be considered
2796 to resolve local to the current module, and false otherwise. Differs
2797 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2798 undefined symbols and weak symbols. */
2801 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2802 struct bfd_link_info *info,
2803 bfd_boolean local_protected)
2805 const struct elf_backend_data *bed;
2806 struct elf_link_hash_table *hash_table;
2808 /* If it's a local sym, of course we resolve locally. */
2812 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2813 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2814 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2817 /* Common symbols that become definitions don't get the DEF_REGULAR
2818 flag set, so test it first, and don't bail out. */
2819 if (ELF_COMMON_DEF_P (h))
2821 /* If we don't have a definition in a regular file, then we can't
2822 resolve locally. The sym is either undefined or dynamic. */
2823 else if (!h->def_regular)
2826 /* Forced local symbols resolve locally. */
2827 if (h->forced_local)
2830 /* As do non-dynamic symbols. */
2831 if (h->dynindx == -1)
2834 /* At this point, we know the symbol is defined and dynamic. In an
2835 executable it must resolve locally, likewise when building symbolic
2836 shared libraries. */
2837 if (info->executable || SYMBOLIC_BIND (info, h))
2840 /* Now deal with defined dynamic symbols in shared libraries. Ones
2841 with default visibility might not resolve locally. */
2842 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2845 hash_table = elf_hash_table (info);
2846 if (!is_elf_hash_table (hash_table))
2849 bed = get_elf_backend_data (hash_table->dynobj);
2851 /* STV_PROTECTED non-function symbols are local. */
2852 if (!bed->is_function_type (h->type))
2855 /* Function pointer equality tests may require that STV_PROTECTED
2856 symbols be treated as dynamic symbols, even when we know that the
2857 dynamic linker will resolve them locally. */
2858 return local_protected;
2861 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2862 aligned. Returns the first TLS output section. */
2864 struct bfd_section *
2865 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2867 struct bfd_section *sec, *tls;
2868 unsigned int align = 0;
2870 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2871 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2875 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2876 if (sec->alignment_power > align)
2877 align = sec->alignment_power;
2879 elf_hash_table (info)->tls_sec = tls;
2881 /* Ensure the alignment of the first section is the largest alignment,
2882 so that the tls segment starts aligned. */
2884 tls->alignment_power = align;
2889 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2891 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2892 Elf_Internal_Sym *sym)
2894 const struct elf_backend_data *bed;
2896 /* Local symbols do not count, but target specific ones might. */
2897 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2898 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2901 bed = get_elf_backend_data (abfd);
2902 /* Function symbols do not count. */
2903 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2906 /* If the section is undefined, then so is the symbol. */
2907 if (sym->st_shndx == SHN_UNDEF)
2910 /* If the symbol is defined in the common section, then
2911 it is a common definition and so does not count. */
2912 if (bed->common_definition (sym))
2915 /* If the symbol is in a target specific section then we
2916 must rely upon the backend to tell us what it is. */
2917 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2918 /* FIXME - this function is not coded yet:
2920 return _bfd_is_global_symbol_definition (abfd, sym);
2922 Instead for now assume that the definition is not global,
2923 Even if this is wrong, at least the linker will behave
2924 in the same way that it used to do. */
2930 /* Search the symbol table of the archive element of the archive ABFD
2931 whose archive map contains a mention of SYMDEF, and determine if
2932 the symbol is defined in this element. */
2934 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2936 Elf_Internal_Shdr * hdr;
2937 bfd_size_type symcount;
2938 bfd_size_type extsymcount;
2939 bfd_size_type extsymoff;
2940 Elf_Internal_Sym *isymbuf;
2941 Elf_Internal_Sym *isym;
2942 Elf_Internal_Sym *isymend;
2945 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2949 if (! bfd_check_format (abfd, bfd_object))
2952 /* If we have already included the element containing this symbol in the
2953 link then we do not need to include it again. Just claim that any symbol
2954 it contains is not a definition, so that our caller will not decide to
2955 (re)include this element. */
2956 if (abfd->archive_pass)
2959 /* Select the appropriate symbol table. */
2960 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2961 hdr = &elf_tdata (abfd)->symtab_hdr;
2963 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2965 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2967 /* The sh_info field of the symtab header tells us where the
2968 external symbols start. We don't care about the local symbols. */
2969 if (elf_bad_symtab (abfd))
2971 extsymcount = symcount;
2976 extsymcount = symcount - hdr->sh_info;
2977 extsymoff = hdr->sh_info;
2980 if (extsymcount == 0)
2983 /* Read in the symbol table. */
2984 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2986 if (isymbuf == NULL)
2989 /* Scan the symbol table looking for SYMDEF. */
2991 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2995 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3000 if (strcmp (name, symdef->name) == 0)
3002 result = is_global_data_symbol_definition (abfd, isym);
3012 /* Add an entry to the .dynamic table. */
3015 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3019 struct elf_link_hash_table *hash_table;
3020 const struct elf_backend_data *bed;
3022 bfd_size_type newsize;
3023 bfd_byte *newcontents;
3024 Elf_Internal_Dyn dyn;
3026 hash_table = elf_hash_table (info);
3027 if (! is_elf_hash_table (hash_table))
3030 bed = get_elf_backend_data (hash_table->dynobj);
3031 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3032 BFD_ASSERT (s != NULL);
3034 newsize = s->size + bed->s->sizeof_dyn;
3035 newcontents = bfd_realloc (s->contents, newsize);
3036 if (newcontents == NULL)
3040 dyn.d_un.d_val = val;
3041 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3044 s->contents = newcontents;
3049 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3050 otherwise just check whether one already exists. Returns -1 on error,
3051 1 if a DT_NEEDED tag already exists, and 0 on success. */
3054 elf_add_dt_needed_tag (bfd *abfd,
3055 struct bfd_link_info *info,
3059 struct elf_link_hash_table *hash_table;
3060 bfd_size_type oldsize;
3061 bfd_size_type strindex;
3063 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3066 hash_table = elf_hash_table (info);
3067 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3068 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3069 if (strindex == (bfd_size_type) -1)
3072 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3075 const struct elf_backend_data *bed;
3078 bed = get_elf_backend_data (hash_table->dynobj);
3079 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3081 for (extdyn = sdyn->contents;
3082 extdyn < sdyn->contents + sdyn->size;
3083 extdyn += bed->s->sizeof_dyn)
3085 Elf_Internal_Dyn dyn;
3087 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3088 if (dyn.d_tag == DT_NEEDED
3089 && dyn.d_un.d_val == strindex)
3091 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3099 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3102 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3106 /* We were just checking for existence of the tag. */
3107 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3112 /* Sort symbol by value and section. */
3114 elf_sort_symbol (const void *arg1, const void *arg2)
3116 const struct elf_link_hash_entry *h1;
3117 const struct elf_link_hash_entry *h2;
3118 bfd_signed_vma vdiff;
3120 h1 = *(const struct elf_link_hash_entry **) arg1;
3121 h2 = *(const struct elf_link_hash_entry **) arg2;
3122 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3124 return vdiff > 0 ? 1 : -1;
3127 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3129 return sdiff > 0 ? 1 : -1;
3134 /* This function is used to adjust offsets into .dynstr for
3135 dynamic symbols. This is called via elf_link_hash_traverse. */
3138 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3140 struct elf_strtab_hash *dynstr = data;
3142 if (h->root.type == bfd_link_hash_warning)
3143 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3145 if (h->dynindx != -1)
3146 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3150 /* Assign string offsets in .dynstr, update all structures referencing
3154 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3156 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3157 struct elf_link_local_dynamic_entry *entry;
3158 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3159 bfd *dynobj = hash_table->dynobj;
3162 const struct elf_backend_data *bed;
3165 _bfd_elf_strtab_finalize (dynstr);
3166 size = _bfd_elf_strtab_size (dynstr);
3168 bed = get_elf_backend_data (dynobj);
3169 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3170 BFD_ASSERT (sdyn != NULL);
3172 /* Update all .dynamic entries referencing .dynstr strings. */
3173 for (extdyn = sdyn->contents;
3174 extdyn < sdyn->contents + sdyn->size;
3175 extdyn += bed->s->sizeof_dyn)
3177 Elf_Internal_Dyn dyn;
3179 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3183 dyn.d_un.d_val = size;
3191 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3196 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3199 /* Now update local dynamic symbols. */
3200 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3201 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3202 entry->isym.st_name);
3204 /* And the rest of dynamic symbols. */
3205 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3207 /* Adjust version definitions. */
3208 if (elf_tdata (output_bfd)->cverdefs)
3213 Elf_Internal_Verdef def;
3214 Elf_Internal_Verdaux defaux;
3216 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3220 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3222 p += sizeof (Elf_External_Verdef);
3223 if (def.vd_aux != sizeof (Elf_External_Verdef))
3225 for (i = 0; i < def.vd_cnt; ++i)
3227 _bfd_elf_swap_verdaux_in (output_bfd,
3228 (Elf_External_Verdaux *) p, &defaux);
3229 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3231 _bfd_elf_swap_verdaux_out (output_bfd,
3232 &defaux, (Elf_External_Verdaux *) p);
3233 p += sizeof (Elf_External_Verdaux);
3236 while (def.vd_next);
3239 /* Adjust version references. */
3240 if (elf_tdata (output_bfd)->verref)
3245 Elf_Internal_Verneed need;
3246 Elf_Internal_Vernaux needaux;
3248 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3252 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3254 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3255 _bfd_elf_swap_verneed_out (output_bfd, &need,
3256 (Elf_External_Verneed *) p);
3257 p += sizeof (Elf_External_Verneed);
3258 for (i = 0; i < need.vn_cnt; ++i)
3260 _bfd_elf_swap_vernaux_in (output_bfd,
3261 (Elf_External_Vernaux *) p, &needaux);
3262 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3264 _bfd_elf_swap_vernaux_out (output_bfd,
3266 (Elf_External_Vernaux *) p);
3267 p += sizeof (Elf_External_Vernaux);
3270 while (need.vn_next);
3276 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3277 The default is to only match when the INPUT and OUTPUT are exactly
3281 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3282 const bfd_target *output)
3284 return input == output;
3287 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3288 This version is used when different targets for the same architecture
3289 are virtually identical. */
3292 _bfd_elf_relocs_compatible (const bfd_target *input,
3293 const bfd_target *output)
3295 const struct elf_backend_data *obed, *ibed;
3297 if (input == output)
3300 ibed = xvec_get_elf_backend_data (input);
3301 obed = xvec_get_elf_backend_data (output);
3303 if (ibed->arch != obed->arch)
3306 /* If both backends are using this function, deem them compatible. */
3307 return ibed->relocs_compatible == obed->relocs_compatible;
3310 /* Add symbols from an ELF object file to the linker hash table. */
3313 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3315 Elf_Internal_Shdr *hdr;
3316 bfd_size_type symcount;
3317 bfd_size_type extsymcount;
3318 bfd_size_type extsymoff;
3319 struct elf_link_hash_entry **sym_hash;
3320 bfd_boolean dynamic;
3321 Elf_External_Versym *extversym = NULL;
3322 Elf_External_Versym *ever;
3323 struct elf_link_hash_entry *weaks;
3324 struct elf_link_hash_entry **nondeflt_vers = NULL;
3325 bfd_size_type nondeflt_vers_cnt = 0;
3326 Elf_Internal_Sym *isymbuf = NULL;
3327 Elf_Internal_Sym *isym;
3328 Elf_Internal_Sym *isymend;
3329 const struct elf_backend_data *bed;
3330 bfd_boolean add_needed;
3331 struct elf_link_hash_table *htab;
3333 void *alloc_mark = NULL;
3334 struct bfd_hash_entry **old_table = NULL;
3335 unsigned int old_size = 0;
3336 unsigned int old_count = 0;
3337 void *old_tab = NULL;
3340 struct bfd_link_hash_entry *old_undefs = NULL;
3341 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3342 long old_dynsymcount = 0;
3344 size_t hashsize = 0;
3346 htab = elf_hash_table (info);
3347 bed = get_elf_backend_data (abfd);
3349 if ((abfd->flags & DYNAMIC) == 0)
3355 /* You can't use -r against a dynamic object. Also, there's no
3356 hope of using a dynamic object which does not exactly match
3357 the format of the output file. */
3358 if (info->relocatable
3359 || !is_elf_hash_table (htab)
3360 || info->output_bfd->xvec != abfd->xvec)
3362 if (info->relocatable)
3363 bfd_set_error (bfd_error_invalid_operation);
3365 bfd_set_error (bfd_error_wrong_format);
3370 /* As a GNU extension, any input sections which are named
3371 .gnu.warning.SYMBOL are treated as warning symbols for the given
3372 symbol. This differs from .gnu.warning sections, which generate
3373 warnings when they are included in an output file. */
3374 if (info->executable)
3378 for (s = abfd->sections; s != NULL; s = s->next)
3382 name = bfd_get_section_name (abfd, s);
3383 if (CONST_STRNEQ (name, ".gnu.warning."))
3388 name += sizeof ".gnu.warning." - 1;
3390 /* If this is a shared object, then look up the symbol
3391 in the hash table. If it is there, and it is already
3392 been defined, then we will not be using the entry
3393 from this shared object, so we don't need to warn.
3394 FIXME: If we see the definition in a regular object
3395 later on, we will warn, but we shouldn't. The only
3396 fix is to keep track of what warnings we are supposed
3397 to emit, and then handle them all at the end of the
3401 struct elf_link_hash_entry *h;
3403 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3405 /* FIXME: What about bfd_link_hash_common? */
3407 && (h->root.type == bfd_link_hash_defined
3408 || h->root.type == bfd_link_hash_defweak))
3410 /* We don't want to issue this warning. Clobber
3411 the section size so that the warning does not
3412 get copied into the output file. */
3419 msg = bfd_alloc (abfd, sz + 1);
3423 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3428 if (! (_bfd_generic_link_add_one_symbol
3429 (info, abfd, name, BSF_WARNING, s, 0, msg,
3430 FALSE, bed->collect, NULL)))
3433 if (! info->relocatable)
3435 /* Clobber the section size so that the warning does
3436 not get copied into the output file. */
3439 /* Also set SEC_EXCLUDE, so that symbols defined in
3440 the warning section don't get copied to the output. */
3441 s->flags |= SEC_EXCLUDE;
3450 /* If we are creating a shared library, create all the dynamic
3451 sections immediately. We need to attach them to something,
3452 so we attach them to this BFD, provided it is the right
3453 format. FIXME: If there are no input BFD's of the same
3454 format as the output, we can't make a shared library. */
3456 && is_elf_hash_table (htab)
3457 && info->output_bfd->xvec == abfd->xvec
3458 && !htab->dynamic_sections_created)
3460 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3464 else if (!is_elf_hash_table (htab))
3469 const char *soname = NULL;
3470 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3473 /* ld --just-symbols and dynamic objects don't mix very well.
3474 ld shouldn't allow it. */
3475 if ((s = abfd->sections) != NULL
3476 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3479 /* If this dynamic lib was specified on the command line with
3480 --as-needed in effect, then we don't want to add a DT_NEEDED
3481 tag unless the lib is actually used. Similary for libs brought
3482 in by another lib's DT_NEEDED. When --no-add-needed is used
3483 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3484 any dynamic library in DT_NEEDED tags in the dynamic lib at
3486 add_needed = (elf_dyn_lib_class (abfd)
3487 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3488 | DYN_NO_NEEDED)) == 0;
3490 s = bfd_get_section_by_name (abfd, ".dynamic");
3495 unsigned int elfsec;
3496 unsigned long shlink;
3498 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3499 goto error_free_dyn;
3501 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3502 if (elfsec == SHN_BAD)
3503 goto error_free_dyn;
3504 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3506 for (extdyn = dynbuf;
3507 extdyn < dynbuf + s->size;
3508 extdyn += bed->s->sizeof_dyn)
3510 Elf_Internal_Dyn dyn;
3512 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3513 if (dyn.d_tag == DT_SONAME)
3515 unsigned int tagv = dyn.d_un.d_val;
3516 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3518 goto error_free_dyn;
3520 if (dyn.d_tag == DT_NEEDED)
3522 struct bfd_link_needed_list *n, **pn;
3524 unsigned int tagv = dyn.d_un.d_val;
3526 amt = sizeof (struct bfd_link_needed_list);
3527 n = bfd_alloc (abfd, amt);
3528 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3529 if (n == NULL || fnm == NULL)
3530 goto error_free_dyn;
3531 amt = strlen (fnm) + 1;
3532 anm = bfd_alloc (abfd, amt);
3534 goto error_free_dyn;
3535 memcpy (anm, fnm, amt);
3539 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3543 if (dyn.d_tag == DT_RUNPATH)
3545 struct bfd_link_needed_list *n, **pn;
3547 unsigned int tagv = dyn.d_un.d_val;
3549 amt = sizeof (struct bfd_link_needed_list);
3550 n = bfd_alloc (abfd, amt);
3551 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3552 if (n == NULL || fnm == NULL)
3553 goto error_free_dyn;
3554 amt = strlen (fnm) + 1;
3555 anm = bfd_alloc (abfd, amt);
3557 goto error_free_dyn;
3558 memcpy (anm, fnm, amt);
3562 for (pn = & runpath;
3568 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3569 if (!runpath && dyn.d_tag == DT_RPATH)
3571 struct bfd_link_needed_list *n, **pn;
3573 unsigned int tagv = dyn.d_un.d_val;
3575 amt = sizeof (struct bfd_link_needed_list);
3576 n = bfd_alloc (abfd, amt);
3577 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3578 if (n == NULL || fnm == NULL)
3579 goto error_free_dyn;
3580 amt = strlen (fnm) + 1;
3581 anm = bfd_alloc (abfd, amt);
3588 memcpy (anm, fnm, amt);
3603 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3604 frees all more recently bfd_alloc'd blocks as well. */
3610 struct bfd_link_needed_list **pn;
3611 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3616 /* We do not want to include any of the sections in a dynamic
3617 object in the output file. We hack by simply clobbering the
3618 list of sections in the BFD. This could be handled more
3619 cleanly by, say, a new section flag; the existing
3620 SEC_NEVER_LOAD flag is not the one we want, because that one
3621 still implies that the section takes up space in the output
3623 bfd_section_list_clear (abfd);
3625 /* Find the name to use in a DT_NEEDED entry that refers to this
3626 object. If the object has a DT_SONAME entry, we use it.
3627 Otherwise, if the generic linker stuck something in
3628 elf_dt_name, we use that. Otherwise, we just use the file
3630 if (soname == NULL || *soname == '\0')
3632 soname = elf_dt_name (abfd);
3633 if (soname == NULL || *soname == '\0')
3634 soname = bfd_get_filename (abfd);
3637 /* Save the SONAME because sometimes the linker emulation code
3638 will need to know it. */
3639 elf_dt_name (abfd) = soname;
3641 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3645 /* If we have already included this dynamic object in the
3646 link, just ignore it. There is no reason to include a
3647 particular dynamic object more than once. */
3652 /* If this is a dynamic object, we always link against the .dynsym
3653 symbol table, not the .symtab symbol table. The dynamic linker
3654 will only see the .dynsym symbol table, so there is no reason to
3655 look at .symtab for a dynamic object. */
3657 if (! dynamic || elf_dynsymtab (abfd) == 0)
3658 hdr = &elf_tdata (abfd)->symtab_hdr;
3660 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3662 symcount = hdr->sh_size / bed->s->sizeof_sym;
3664 /* The sh_info field of the symtab header tells us where the
3665 external symbols start. We don't care about the local symbols at
3667 if (elf_bad_symtab (abfd))
3669 extsymcount = symcount;
3674 extsymcount = symcount - hdr->sh_info;
3675 extsymoff = hdr->sh_info;
3679 if (extsymcount != 0)
3681 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3683 if (isymbuf == NULL)
3686 /* We store a pointer to the hash table entry for each external
3688 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3689 sym_hash = bfd_alloc (abfd, amt);
3690 if (sym_hash == NULL)
3691 goto error_free_sym;
3692 elf_sym_hashes (abfd) = sym_hash;
3697 /* Read in any version definitions. */
3698 if (!_bfd_elf_slurp_version_tables (abfd,
3699 info->default_imported_symver))
3700 goto error_free_sym;
3702 /* Read in the symbol versions, but don't bother to convert them
3703 to internal format. */
3704 if (elf_dynversym (abfd) != 0)
3706 Elf_Internal_Shdr *versymhdr;
3708 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3709 extversym = bfd_malloc (versymhdr->sh_size);
3710 if (extversym == NULL)
3711 goto error_free_sym;
3712 amt = versymhdr->sh_size;
3713 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3714 || bfd_bread (extversym, amt, abfd) != amt)
3715 goto error_free_vers;
3719 /* If we are loading an as-needed shared lib, save the symbol table
3720 state before we start adding symbols. If the lib turns out
3721 to be unneeded, restore the state. */
3722 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3727 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3729 struct bfd_hash_entry *p;
3730 struct elf_link_hash_entry *h;
3732 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3734 h = (struct elf_link_hash_entry *) p;
3735 entsize += htab->root.table.entsize;
3736 if (h->root.type == bfd_link_hash_warning)
3737 entsize += htab->root.table.entsize;
3741 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3742 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3743 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3744 if (old_tab == NULL)
3745 goto error_free_vers;
3747 /* Remember the current objalloc pointer, so that all mem for
3748 symbols added can later be reclaimed. */
3749 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3750 if (alloc_mark == NULL)
3751 goto error_free_vers;
3753 /* Make a special call to the linker "notice" function to
3754 tell it that we are about to handle an as-needed lib. */
3755 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3757 goto error_free_vers;
3759 /* Clone the symbol table and sym hashes. Remember some
3760 pointers into the symbol table, and dynamic symbol count. */
3761 old_hash = (char *) old_tab + tabsize;
3762 old_ent = (char *) old_hash + hashsize;
3763 memcpy (old_tab, htab->root.table.table, tabsize);
3764 memcpy (old_hash, sym_hash, hashsize);
3765 old_undefs = htab->root.undefs;
3766 old_undefs_tail = htab->root.undefs_tail;
3767 old_table = htab->root.table.table;
3768 old_size = htab->root.table.size;
3769 old_count = htab->root.table.count;
3770 old_dynsymcount = htab->dynsymcount;
3772 for (i = 0; i < htab->root.table.size; i++)
3774 struct bfd_hash_entry *p;
3775 struct elf_link_hash_entry *h;
3777 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3779 memcpy (old_ent, p, htab->root.table.entsize);
3780 old_ent = (char *) old_ent + htab->root.table.entsize;
3781 h = (struct elf_link_hash_entry *) p;
3782 if (h->root.type == bfd_link_hash_warning)
3784 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3785 old_ent = (char *) old_ent + htab->root.table.entsize;
3792 ever = extversym != NULL ? extversym + extsymoff : NULL;
3793 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3795 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3799 asection *sec, *new_sec;
3802 struct elf_link_hash_entry *h;
3803 bfd_boolean definition;
3804 bfd_boolean size_change_ok;
3805 bfd_boolean type_change_ok;
3806 bfd_boolean new_weakdef;
3807 bfd_boolean override;
3809 unsigned int old_alignment;
3814 flags = BSF_NO_FLAGS;
3816 value = isym->st_value;
3818 common = bed->common_definition (isym);
3820 bind = ELF_ST_BIND (isym->st_info);
3821 if (bind == STB_LOCAL)
3823 /* This should be impossible, since ELF requires that all
3824 global symbols follow all local symbols, and that sh_info
3825 point to the first global symbol. Unfortunately, Irix 5
3829 else if (bind == STB_GLOBAL)
3831 if (isym->st_shndx != SHN_UNDEF && !common)
3834 else if (bind == STB_WEAK)
3838 /* Leave it up to the processor backend. */
3841 if (isym->st_shndx == SHN_UNDEF)
3842 sec = bfd_und_section_ptr;
3843 else if (isym->st_shndx == SHN_ABS)
3844 sec = bfd_abs_section_ptr;
3845 else if (isym->st_shndx == SHN_COMMON)
3847 sec = bfd_com_section_ptr;
3848 /* What ELF calls the size we call the value. What ELF
3849 calls the value we call the alignment. */
3850 value = isym->st_size;
3854 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3856 sec = bfd_abs_section_ptr;
3857 else if (sec->kept_section)
3859 /* Symbols from discarded section are undefined. We keep
3861 sec = bfd_und_section_ptr;
3862 isym->st_shndx = SHN_UNDEF;
3864 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3868 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3871 goto error_free_vers;
3873 if (isym->st_shndx == SHN_COMMON
3874 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3875 && !info->relocatable)
3877 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3881 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3884 | SEC_LINKER_CREATED
3885 | SEC_THREAD_LOCAL));
3887 goto error_free_vers;
3891 else if (bed->elf_add_symbol_hook)
3893 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3895 goto error_free_vers;
3897 /* The hook function sets the name to NULL if this symbol
3898 should be skipped for some reason. */
3903 /* Sanity check that all possibilities were handled. */
3906 bfd_set_error (bfd_error_bad_value);
3907 goto error_free_vers;
3910 if (bfd_is_und_section (sec)
3911 || bfd_is_com_section (sec))
3916 size_change_ok = FALSE;
3917 type_change_ok = bed->type_change_ok;
3922 if (is_elf_hash_table (htab))
3924 Elf_Internal_Versym iver;
3925 unsigned int vernum = 0;
3930 if (info->default_imported_symver)
3931 /* Use the default symbol version created earlier. */
3932 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3937 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3939 vernum = iver.vs_vers & VERSYM_VERSION;
3941 /* If this is a hidden symbol, or if it is not version
3942 1, we append the version name to the symbol name.
3943 However, we do not modify a non-hidden absolute symbol
3944 if it is not a function, because it might be the version
3945 symbol itself. FIXME: What if it isn't? */
3946 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3948 && (!bfd_is_abs_section (sec)
3949 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
3952 size_t namelen, verlen, newlen;
3955 if (isym->st_shndx != SHN_UNDEF)
3957 if (vernum > elf_tdata (abfd)->cverdefs)
3959 else if (vernum > 1)
3961 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3967 (*_bfd_error_handler)
3968 (_("%B: %s: invalid version %u (max %d)"),
3970 elf_tdata (abfd)->cverdefs);
3971 bfd_set_error (bfd_error_bad_value);
3972 goto error_free_vers;
3977 /* We cannot simply test for the number of
3978 entries in the VERNEED section since the
3979 numbers for the needed versions do not start
3981 Elf_Internal_Verneed *t;
3984 for (t = elf_tdata (abfd)->verref;
3988 Elf_Internal_Vernaux *a;
3990 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3992 if (a->vna_other == vernum)
3994 verstr = a->vna_nodename;
4003 (*_bfd_error_handler)
4004 (_("%B: %s: invalid needed version %d"),
4005 abfd, name, vernum);
4006 bfd_set_error (bfd_error_bad_value);
4007 goto error_free_vers;
4011 namelen = strlen (name);
4012 verlen = strlen (verstr);
4013 newlen = namelen + verlen + 2;
4014 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4015 && isym->st_shndx != SHN_UNDEF)
4018 newname = bfd_hash_allocate (&htab->root.table, newlen);
4019 if (newname == NULL)
4020 goto error_free_vers;
4021 memcpy (newname, name, namelen);
4022 p = newname + namelen;
4024 /* If this is a defined non-hidden version symbol,
4025 we add another @ to the name. This indicates the
4026 default version of the symbol. */
4027 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4028 && isym->st_shndx != SHN_UNDEF)
4030 memcpy (p, verstr, verlen + 1);
4035 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4036 &value, &old_alignment,
4037 sym_hash, &skip, &override,
4038 &type_change_ok, &size_change_ok))
4039 goto error_free_vers;
4048 while (h->root.type == bfd_link_hash_indirect
4049 || h->root.type == bfd_link_hash_warning)
4050 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4052 /* Remember the old alignment if this is a common symbol, so
4053 that we don't reduce the alignment later on. We can't
4054 check later, because _bfd_generic_link_add_one_symbol
4055 will set a default for the alignment which we want to
4056 override. We also remember the old bfd where the existing
4057 definition comes from. */
4058 switch (h->root.type)
4063 case bfd_link_hash_defined:
4064 case bfd_link_hash_defweak:
4065 old_bfd = h->root.u.def.section->owner;
4068 case bfd_link_hash_common:
4069 old_bfd = h->root.u.c.p->section->owner;
4070 old_alignment = h->root.u.c.p->alignment_power;
4074 if (elf_tdata (abfd)->verdef != NULL
4078 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4081 if (! (_bfd_generic_link_add_one_symbol
4082 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4083 (struct bfd_link_hash_entry **) sym_hash)))
4084 goto error_free_vers;
4087 while (h->root.type == bfd_link_hash_indirect
4088 || h->root.type == bfd_link_hash_warning)
4089 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4092 new_weakdef = FALSE;
4095 && (flags & BSF_WEAK) != 0
4096 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4097 && is_elf_hash_table (htab)
4098 && h->u.weakdef == NULL)
4100 /* Keep a list of all weak defined non function symbols from
4101 a dynamic object, using the weakdef field. Later in this
4102 function we will set the weakdef field to the correct
4103 value. We only put non-function symbols from dynamic
4104 objects on this list, because that happens to be the only
4105 time we need to know the normal symbol corresponding to a
4106 weak symbol, and the information is time consuming to
4107 figure out. If the weakdef field is not already NULL,
4108 then this symbol was already defined by some previous
4109 dynamic object, and we will be using that previous
4110 definition anyhow. */
4112 h->u.weakdef = weaks;
4117 /* Set the alignment of a common symbol. */
4118 if ((common || bfd_is_com_section (sec))
4119 && h->root.type == bfd_link_hash_common)
4124 align = bfd_log2 (isym->st_value);
4127 /* The new symbol is a common symbol in a shared object.
4128 We need to get the alignment from the section. */
4129 align = new_sec->alignment_power;
4131 if (align > old_alignment
4132 /* Permit an alignment power of zero if an alignment of one
4133 is specified and no other alignments have been specified. */
4134 || (isym->st_value == 1 && old_alignment == 0))
4135 h->root.u.c.p->alignment_power = align;
4137 h->root.u.c.p->alignment_power = old_alignment;
4140 if (is_elf_hash_table (htab))
4144 /* Check the alignment when a common symbol is involved. This
4145 can change when a common symbol is overridden by a normal
4146 definition or a common symbol is ignored due to the old
4147 normal definition. We need to make sure the maximum
4148 alignment is maintained. */
4149 if ((old_alignment || common)
4150 && h->root.type != bfd_link_hash_common)
4152 unsigned int common_align;
4153 unsigned int normal_align;
4154 unsigned int symbol_align;
4158 symbol_align = ffs (h->root.u.def.value) - 1;
4159 if (h->root.u.def.section->owner != NULL
4160 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4162 normal_align = h->root.u.def.section->alignment_power;
4163 if (normal_align > symbol_align)
4164 normal_align = symbol_align;
4167 normal_align = symbol_align;
4171 common_align = old_alignment;
4172 common_bfd = old_bfd;
4177 common_align = bfd_log2 (isym->st_value);
4179 normal_bfd = old_bfd;
4182 if (normal_align < common_align)
4184 /* PR binutils/2735 */
4185 if (normal_bfd == NULL)
4186 (*_bfd_error_handler)
4187 (_("Warning: alignment %u of common symbol `%s' in %B"
4188 " is greater than the alignment (%u) of its section %A"),
4189 common_bfd, h->root.u.def.section,
4190 1 << common_align, name, 1 << normal_align);
4192 (*_bfd_error_handler)
4193 (_("Warning: alignment %u of symbol `%s' in %B"
4194 " is smaller than %u in %B"),
4195 normal_bfd, common_bfd,
4196 1 << normal_align, name, 1 << common_align);
4200 /* Remember the symbol size if it isn't undefined. */
4201 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4202 && (definition || h->size == 0))
4205 && h->size != isym->st_size
4206 && ! size_change_ok)
4207 (*_bfd_error_handler)
4208 (_("Warning: size of symbol `%s' changed"
4209 " from %lu in %B to %lu in %B"),
4211 name, (unsigned long) h->size,
4212 (unsigned long) isym->st_size);
4214 h->size = isym->st_size;
4217 /* If this is a common symbol, then we always want H->SIZE
4218 to be the size of the common symbol. The code just above
4219 won't fix the size if a common symbol becomes larger. We
4220 don't warn about a size change here, because that is
4221 covered by --warn-common. Allow changed between different
4223 if (h->root.type == bfd_link_hash_common)
4224 h->size = h->root.u.c.size;
4226 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4227 && (definition || h->type == STT_NOTYPE))
4229 if (h->type != STT_NOTYPE
4230 && h->type != ELF_ST_TYPE (isym->st_info)
4231 && ! type_change_ok)
4232 (*_bfd_error_handler)
4233 (_("Warning: type of symbol `%s' changed"
4234 " from %d to %d in %B"),
4235 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
4237 h->type = ELF_ST_TYPE (isym->st_info);
4240 /* If st_other has a processor-specific meaning, specific
4241 code might be needed here. We never merge the visibility
4242 attribute with the one from a dynamic object. */
4243 if (bed->elf_backend_merge_symbol_attribute)
4244 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
4247 /* If this symbol has default visibility and the user has requested
4248 we not re-export it, then mark it as hidden. */
4249 if (definition && !dynamic
4251 || (abfd->my_archive && abfd->my_archive->no_export))
4252 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4253 isym->st_other = (STV_HIDDEN
4254 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4256 if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic)
4258 unsigned char hvis, symvis, other, nvis;
4260 /* Only merge the visibility. Leave the remainder of the
4261 st_other field to elf_backend_merge_symbol_attribute. */
4262 other = h->other & ~ELF_ST_VISIBILITY (-1);
4264 /* Combine visibilities, using the most constraining one. */
4265 hvis = ELF_ST_VISIBILITY (h->other);
4266 symvis = ELF_ST_VISIBILITY (isym->st_other);
4272 nvis = hvis < symvis ? hvis : symvis;
4274 h->other = other | nvis;
4277 /* Set a flag in the hash table entry indicating the type of
4278 reference or definition we just found. Keep a count of
4279 the number of dynamic symbols we find. A dynamic symbol
4280 is one which is referenced or defined by both a regular
4281 object and a shared object. */
4288 if (bind != STB_WEAK)
4289 h->ref_regular_nonweak = 1;
4293 if (! info->executable
4306 || (h->u.weakdef != NULL
4308 && h->u.weakdef->dynindx != -1))
4312 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
4314 /* We don't want to make debug symbol dynamic. */
4315 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4319 /* Check to see if we need to add an indirect symbol for
4320 the default name. */
4321 if (definition || h->root.type == bfd_link_hash_common)
4322 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4323 &sec, &value, &dynsym,
4325 goto error_free_vers;
4327 if (definition && !dynamic)
4329 char *p = strchr (name, ELF_VER_CHR);
4330 if (p != NULL && p[1] != ELF_VER_CHR)
4332 /* Queue non-default versions so that .symver x, x@FOO
4333 aliases can be checked. */
4336 amt = ((isymend - isym + 1)
4337 * sizeof (struct elf_link_hash_entry *));
4338 nondeflt_vers = bfd_malloc (amt);
4340 goto error_free_vers;
4342 nondeflt_vers[nondeflt_vers_cnt++] = h;
4346 if (dynsym && h->dynindx == -1)
4348 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4349 goto error_free_vers;
4350 if (h->u.weakdef != NULL
4352 && h->u.weakdef->dynindx == -1)
4354 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4355 goto error_free_vers;
4358 else if (dynsym && h->dynindx != -1)
4359 /* If the symbol already has a dynamic index, but
4360 visibility says it should not be visible, turn it into
4362 switch (ELF_ST_VISIBILITY (h->other))
4366 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4377 const char *soname = elf_dt_name (abfd);
4379 /* A symbol from a library loaded via DT_NEEDED of some
4380 other library is referenced by a regular object.
4381 Add a DT_NEEDED entry for it. Issue an error if
4382 --no-add-needed is used. */
4383 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4385 (*_bfd_error_handler)
4386 (_("%s: invalid DSO for symbol `%s' definition"),
4388 bfd_set_error (bfd_error_bad_value);
4389 goto error_free_vers;
4392 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4395 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4397 goto error_free_vers;
4399 BFD_ASSERT (ret == 0);
4404 if (extversym != NULL)
4410 if (isymbuf != NULL)
4416 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4420 /* Restore the symbol table. */
4421 if (bed->as_needed_cleanup)
4422 (*bed->as_needed_cleanup) (abfd, info);
4423 old_hash = (char *) old_tab + tabsize;
4424 old_ent = (char *) old_hash + hashsize;
4425 sym_hash = elf_sym_hashes (abfd);
4426 htab->root.table.table = old_table;
4427 htab->root.table.size = old_size;
4428 htab->root.table.count = old_count;
4429 memcpy (htab->root.table.table, old_tab, tabsize);
4430 memcpy (sym_hash, old_hash, hashsize);
4431 htab->root.undefs = old_undefs;
4432 htab->root.undefs_tail = old_undefs_tail;
4433 for (i = 0; i < htab->root.table.size; i++)
4435 struct bfd_hash_entry *p;
4436 struct elf_link_hash_entry *h;
4438 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4440 h = (struct elf_link_hash_entry *) p;
4441 if (h->root.type == bfd_link_hash_warning)
4442 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4443 if (h->dynindx >= old_dynsymcount)
4444 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4446 memcpy (p, old_ent, htab->root.table.entsize);
4447 old_ent = (char *) old_ent + htab->root.table.entsize;
4448 h = (struct elf_link_hash_entry *) p;
4449 if (h->root.type == bfd_link_hash_warning)
4451 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4452 old_ent = (char *) old_ent + htab->root.table.entsize;
4457 /* Make a special call to the linker "notice" function to
4458 tell it that symbols added for crefs may need to be removed. */
4459 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4461 goto error_free_vers;
4464 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4466 if (nondeflt_vers != NULL)
4467 free (nondeflt_vers);
4471 if (old_tab != NULL)
4473 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4475 goto error_free_vers;
4480 /* Now that all the symbols from this input file are created, handle
4481 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4482 if (nondeflt_vers != NULL)
4484 bfd_size_type cnt, symidx;
4486 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4488 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4489 char *shortname, *p;
4491 p = strchr (h->root.root.string, ELF_VER_CHR);
4493 || (h->root.type != bfd_link_hash_defined
4494 && h->root.type != bfd_link_hash_defweak))
4497 amt = p - h->root.root.string;
4498 shortname = bfd_malloc (amt + 1);
4500 goto error_free_vers;
4501 memcpy (shortname, h->root.root.string, amt);
4502 shortname[amt] = '\0';
4504 hi = (struct elf_link_hash_entry *)
4505 bfd_link_hash_lookup (&htab->root, shortname,
4506 FALSE, FALSE, FALSE);
4508 && hi->root.type == h->root.type
4509 && hi->root.u.def.value == h->root.u.def.value
4510 && hi->root.u.def.section == h->root.u.def.section)
4512 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4513 hi->root.type = bfd_link_hash_indirect;
4514 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4515 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4516 sym_hash = elf_sym_hashes (abfd);
4518 for (symidx = 0; symidx < extsymcount; ++symidx)
4519 if (sym_hash[symidx] == hi)
4521 sym_hash[symidx] = h;
4527 free (nondeflt_vers);
4528 nondeflt_vers = NULL;
4531 /* Now set the weakdefs field correctly for all the weak defined
4532 symbols we found. The only way to do this is to search all the
4533 symbols. Since we only need the information for non functions in
4534 dynamic objects, that's the only time we actually put anything on
4535 the list WEAKS. We need this information so that if a regular
4536 object refers to a symbol defined weakly in a dynamic object, the
4537 real symbol in the dynamic object is also put in the dynamic
4538 symbols; we also must arrange for both symbols to point to the
4539 same memory location. We could handle the general case of symbol
4540 aliasing, but a general symbol alias can only be generated in
4541 assembler code, handling it correctly would be very time
4542 consuming, and other ELF linkers don't handle general aliasing
4546 struct elf_link_hash_entry **hpp;
4547 struct elf_link_hash_entry **hppend;
4548 struct elf_link_hash_entry **sorted_sym_hash;
4549 struct elf_link_hash_entry *h;
4552 /* Since we have to search the whole symbol list for each weak
4553 defined symbol, search time for N weak defined symbols will be
4554 O(N^2). Binary search will cut it down to O(NlogN). */
4555 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4556 sorted_sym_hash = bfd_malloc (amt);
4557 if (sorted_sym_hash == NULL)
4559 sym_hash = sorted_sym_hash;
4560 hpp = elf_sym_hashes (abfd);
4561 hppend = hpp + extsymcount;
4563 for (; hpp < hppend; hpp++)
4567 && h->root.type == bfd_link_hash_defined
4568 && !bed->is_function_type (h->type))
4576 qsort (sorted_sym_hash, sym_count,
4577 sizeof (struct elf_link_hash_entry *),
4580 while (weaks != NULL)
4582 struct elf_link_hash_entry *hlook;
4589 weaks = hlook->u.weakdef;
4590 hlook->u.weakdef = NULL;
4592 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4593 || hlook->root.type == bfd_link_hash_defweak
4594 || hlook->root.type == bfd_link_hash_common
4595 || hlook->root.type == bfd_link_hash_indirect);
4596 slook = hlook->root.u.def.section;
4597 vlook = hlook->root.u.def.value;
4604 bfd_signed_vma vdiff;
4606 h = sorted_sym_hash [idx];
4607 vdiff = vlook - h->root.u.def.value;
4614 long sdiff = slook->id - h->root.u.def.section->id;
4627 /* We didn't find a value/section match. */
4631 for (i = ilook; i < sym_count; i++)
4633 h = sorted_sym_hash [i];
4635 /* Stop if value or section doesn't match. */
4636 if (h->root.u.def.value != vlook
4637 || h->root.u.def.section != slook)
4639 else if (h != hlook)
4641 hlook->u.weakdef = h;
4643 /* If the weak definition is in the list of dynamic
4644 symbols, make sure the real definition is put
4646 if (hlook->dynindx != -1 && h->dynindx == -1)
4648 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4651 free (sorted_sym_hash);
4656 /* If the real definition is in the list of dynamic
4657 symbols, make sure the weak definition is put
4658 there as well. If we don't do this, then the
4659 dynamic loader might not merge the entries for the
4660 real definition and the weak definition. */
4661 if (h->dynindx != -1 && hlook->dynindx == -1)
4663 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4664 goto err_free_sym_hash;
4671 free (sorted_sym_hash);
4674 if (bed->check_directives)
4675 (*bed->check_directives) (abfd, info);
4677 /* If this object is the same format as the output object, and it is
4678 not a shared library, then let the backend look through the
4681 This is required to build global offset table entries and to
4682 arrange for dynamic relocs. It is not required for the
4683 particular common case of linking non PIC code, even when linking
4684 against shared libraries, but unfortunately there is no way of
4685 knowing whether an object file has been compiled PIC or not.
4686 Looking through the relocs is not particularly time consuming.
4687 The problem is that we must either (1) keep the relocs in memory,
4688 which causes the linker to require additional runtime memory or
4689 (2) read the relocs twice from the input file, which wastes time.
4690 This would be a good case for using mmap.
4692 I have no idea how to handle linking PIC code into a file of a
4693 different format. It probably can't be done. */
4695 && is_elf_hash_table (htab)
4696 && bed->check_relocs != NULL
4697 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4701 for (o = abfd->sections; o != NULL; o = o->next)
4703 Elf_Internal_Rela *internal_relocs;
4706 if ((o->flags & SEC_RELOC) == 0
4707 || o->reloc_count == 0
4708 || ((info->strip == strip_all || info->strip == strip_debugger)
4709 && (o->flags & SEC_DEBUGGING) != 0)
4710 || bfd_is_abs_section (o->output_section))
4713 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4715 if (internal_relocs == NULL)
4718 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4720 if (elf_section_data (o)->relocs != internal_relocs)
4721 free (internal_relocs);
4728 /* If this is a non-traditional link, try to optimize the handling
4729 of the .stab/.stabstr sections. */
4731 && ! info->traditional_format
4732 && is_elf_hash_table (htab)
4733 && (info->strip != strip_all && info->strip != strip_debugger))
4737 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4738 if (stabstr != NULL)
4740 bfd_size_type string_offset = 0;
4743 for (stab = abfd->sections; stab; stab = stab->next)
4744 if (CONST_STRNEQ (stab->name, ".stab")
4745 && (!stab->name[5] ||
4746 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4747 && (stab->flags & SEC_MERGE) == 0
4748 && !bfd_is_abs_section (stab->output_section))
4750 struct bfd_elf_section_data *secdata;
4752 secdata = elf_section_data (stab);
4753 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4754 stabstr, &secdata->sec_info,
4757 if (secdata->sec_info)
4758 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4763 if (is_elf_hash_table (htab) && add_needed)
4765 /* Add this bfd to the loaded list. */
4766 struct elf_link_loaded_list *n;
4768 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4772 n->next = htab->loaded;
4779 if (old_tab != NULL)
4781 if (nondeflt_vers != NULL)
4782 free (nondeflt_vers);
4783 if (extversym != NULL)
4786 if (isymbuf != NULL)
4792 /* Return the linker hash table entry of a symbol that might be
4793 satisfied by an archive symbol. Return -1 on error. */
4795 struct elf_link_hash_entry *
4796 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4797 struct bfd_link_info *info,
4800 struct elf_link_hash_entry *h;
4804 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4808 /* If this is a default version (the name contains @@), look up the
4809 symbol again with only one `@' as well as without the version.
4810 The effect is that references to the symbol with and without the
4811 version will be matched by the default symbol in the archive. */
4813 p = strchr (name, ELF_VER_CHR);
4814 if (p == NULL || p[1] != ELF_VER_CHR)
4817 /* First check with only one `@'. */
4818 len = strlen (name);
4819 copy = bfd_alloc (abfd, len);
4821 return (struct elf_link_hash_entry *) 0 - 1;
4823 first = p - name + 1;
4824 memcpy (copy, name, first);
4825 memcpy (copy + first, name + first + 1, len - first);
4827 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4830 /* We also need to check references to the symbol without the
4832 copy[first - 1] = '\0';
4833 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4834 FALSE, FALSE, FALSE);
4837 bfd_release (abfd, copy);
4841 /* Add symbols from an ELF archive file to the linker hash table. We
4842 don't use _bfd_generic_link_add_archive_symbols because of a
4843 problem which arises on UnixWare. The UnixWare libc.so is an
4844 archive which includes an entry libc.so.1 which defines a bunch of
4845 symbols. The libc.so archive also includes a number of other
4846 object files, which also define symbols, some of which are the same
4847 as those defined in libc.so.1. Correct linking requires that we
4848 consider each object file in turn, and include it if it defines any
4849 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4850 this; it looks through the list of undefined symbols, and includes
4851 any object file which defines them. When this algorithm is used on
4852 UnixWare, it winds up pulling in libc.so.1 early and defining a
4853 bunch of symbols. This means that some of the other objects in the
4854 archive are not included in the link, which is incorrect since they
4855 precede libc.so.1 in the archive.
4857 Fortunately, ELF archive handling is simpler than that done by
4858 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4859 oddities. In ELF, if we find a symbol in the archive map, and the
4860 symbol is currently undefined, we know that we must pull in that
4863 Unfortunately, we do have to make multiple passes over the symbol
4864 table until nothing further is resolved. */
4867 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4870 bfd_boolean *defined = NULL;
4871 bfd_boolean *included = NULL;
4875 const struct elf_backend_data *bed;
4876 struct elf_link_hash_entry * (*archive_symbol_lookup)
4877 (bfd *, struct bfd_link_info *, const char *);
4879 if (! bfd_has_map (abfd))
4881 /* An empty archive is a special case. */
4882 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4884 bfd_set_error (bfd_error_no_armap);
4888 /* Keep track of all symbols we know to be already defined, and all
4889 files we know to be already included. This is to speed up the
4890 second and subsequent passes. */
4891 c = bfd_ardata (abfd)->symdef_count;
4895 amt *= sizeof (bfd_boolean);
4896 defined = bfd_zmalloc (amt);
4897 included = bfd_zmalloc (amt);
4898 if (defined == NULL || included == NULL)
4901 symdefs = bfd_ardata (abfd)->symdefs;
4902 bed = get_elf_backend_data (abfd);
4903 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4916 symdefend = symdef + c;
4917 for (i = 0; symdef < symdefend; symdef++, i++)
4919 struct elf_link_hash_entry *h;
4921 struct bfd_link_hash_entry *undefs_tail;
4924 if (defined[i] || included[i])
4926 if (symdef->file_offset == last)
4932 h = archive_symbol_lookup (abfd, info, symdef->name);
4933 if (h == (struct elf_link_hash_entry *) 0 - 1)
4939 if (h->root.type == bfd_link_hash_common)
4941 /* We currently have a common symbol. The archive map contains
4942 a reference to this symbol, so we may want to include it. We
4943 only want to include it however, if this archive element
4944 contains a definition of the symbol, not just another common
4947 Unfortunately some archivers (including GNU ar) will put
4948 declarations of common symbols into their archive maps, as
4949 well as real definitions, so we cannot just go by the archive
4950 map alone. Instead we must read in the element's symbol
4951 table and check that to see what kind of symbol definition
4953 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4956 else if (h->root.type != bfd_link_hash_undefined)
4958 if (h->root.type != bfd_link_hash_undefweak)
4963 /* We need to include this archive member. */
4964 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4965 if (element == NULL)
4968 if (! bfd_check_format (element, bfd_object))
4971 /* Doublecheck that we have not included this object
4972 already--it should be impossible, but there may be
4973 something wrong with the archive. */
4974 if (element->archive_pass != 0)
4976 bfd_set_error (bfd_error_bad_value);
4979 element->archive_pass = 1;
4981 undefs_tail = info->hash->undefs_tail;
4983 if (! (*info->callbacks->add_archive_element) (info, element,
4986 if (! bfd_link_add_symbols (element, info))
4989 /* If there are any new undefined symbols, we need to make
4990 another pass through the archive in order to see whether
4991 they can be defined. FIXME: This isn't perfect, because
4992 common symbols wind up on undefs_tail and because an
4993 undefined symbol which is defined later on in this pass
4994 does not require another pass. This isn't a bug, but it
4995 does make the code less efficient than it could be. */
4996 if (undefs_tail != info->hash->undefs_tail)
4999 /* Look backward to mark all symbols from this object file
5000 which we have already seen in this pass. */
5004 included[mark] = TRUE;
5009 while (symdefs[mark].file_offset == symdef->file_offset);
5011 /* We mark subsequent symbols from this object file as we go
5012 on through the loop. */
5013 last = symdef->file_offset;
5024 if (defined != NULL)
5026 if (included != NULL)
5031 /* Given an ELF BFD, add symbols to the global hash table as
5035 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5037 switch (bfd_get_format (abfd))
5040 return elf_link_add_object_symbols (abfd, info);
5042 return elf_link_add_archive_symbols (abfd, info);
5044 bfd_set_error (bfd_error_wrong_format);
5049 struct hash_codes_info
5051 unsigned long *hashcodes;
5055 /* This function will be called though elf_link_hash_traverse to store
5056 all hash value of the exported symbols in an array. */
5059 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5061 struct hash_codes_info *inf = data;
5067 if (h->root.type == bfd_link_hash_warning)
5068 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5070 /* Ignore indirect symbols. These are added by the versioning code. */
5071 if (h->dynindx == -1)
5074 name = h->root.root.string;
5075 p = strchr (name, ELF_VER_CHR);
5078 alc = bfd_malloc (p - name + 1);
5084 memcpy (alc, name, p - name);
5085 alc[p - name] = '\0';
5089 /* Compute the hash value. */
5090 ha = bfd_elf_hash (name);
5092 /* Store the found hash value in the array given as the argument. */
5093 *(inf->hashcodes)++ = ha;
5095 /* And store it in the struct so that we can put it in the hash table
5097 h->u.elf_hash_value = ha;
5105 struct collect_gnu_hash_codes
5108 const struct elf_backend_data *bed;
5109 unsigned long int nsyms;
5110 unsigned long int maskbits;
5111 unsigned long int *hashcodes;
5112 unsigned long int *hashval;
5113 unsigned long int *indx;
5114 unsigned long int *counts;
5117 long int min_dynindx;
5118 unsigned long int bucketcount;
5119 unsigned long int symindx;
5120 long int local_indx;
5121 long int shift1, shift2;
5122 unsigned long int mask;
5126 /* This function will be called though elf_link_hash_traverse to store
5127 all hash value of the exported symbols in an array. */
5130 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5132 struct collect_gnu_hash_codes *s = data;
5138 if (h->root.type == bfd_link_hash_warning)
5139 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5141 /* Ignore indirect symbols. These are added by the versioning code. */
5142 if (h->dynindx == -1)
5145 /* Ignore also local symbols and undefined symbols. */
5146 if (! (*s->bed->elf_hash_symbol) (h))
5149 name = h->root.root.string;
5150 p = strchr (name, ELF_VER_CHR);
5153 alc = bfd_malloc (p - name + 1);
5159 memcpy (alc, name, p - name);
5160 alc[p - name] = '\0';
5164 /* Compute the hash value. */
5165 ha = bfd_elf_gnu_hash (name);
5167 /* Store the found hash value in the array for compute_bucket_count,
5168 and also for .dynsym reordering purposes. */
5169 s->hashcodes[s->nsyms] = ha;
5170 s->hashval[h->dynindx] = ha;
5172 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5173 s->min_dynindx = h->dynindx;
5181 /* This function will be called though elf_link_hash_traverse to do
5182 final dynaminc symbol renumbering. */
5185 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5187 struct collect_gnu_hash_codes *s = data;
5188 unsigned long int bucket;
5189 unsigned long int val;
5191 if (h->root.type == bfd_link_hash_warning)
5192 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5194 /* Ignore indirect symbols. */
5195 if (h->dynindx == -1)
5198 /* Ignore also local symbols and undefined symbols. */
5199 if (! (*s->bed->elf_hash_symbol) (h))
5201 if (h->dynindx >= s->min_dynindx)
5202 h->dynindx = s->local_indx++;
5206 bucket = s->hashval[h->dynindx] % s->bucketcount;
5207 val = (s->hashval[h->dynindx] >> s->shift1)
5208 & ((s->maskbits >> s->shift1) - 1);
5209 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5211 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5212 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5213 if (s->counts[bucket] == 1)
5214 /* Last element terminates the chain. */
5216 bfd_put_32 (s->output_bfd, val,
5217 s->contents + (s->indx[bucket] - s->symindx) * 4);
5218 --s->counts[bucket];
5219 h->dynindx = s->indx[bucket]++;
5223 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5226 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5228 return !(h->forced_local
5229 || h->root.type == bfd_link_hash_undefined
5230 || h->root.type == bfd_link_hash_undefweak
5231 || ((h->root.type == bfd_link_hash_defined
5232 || h->root.type == bfd_link_hash_defweak)
5233 && h->root.u.def.section->output_section == NULL));
5236 /* Array used to determine the number of hash table buckets to use
5237 based on the number of symbols there are. If there are fewer than
5238 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5239 fewer than 37 we use 17 buckets, and so forth. We never use more
5240 than 32771 buckets. */
5242 static const size_t elf_buckets[] =
5244 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5248 /* Compute bucket count for hashing table. We do not use a static set
5249 of possible tables sizes anymore. Instead we determine for all
5250 possible reasonable sizes of the table the outcome (i.e., the
5251 number of collisions etc) and choose the best solution. The
5252 weighting functions are not too simple to allow the table to grow
5253 without bounds. Instead one of the weighting factors is the size.
5254 Therefore the result is always a good payoff between few collisions
5255 (= short chain lengths) and table size. */
5257 compute_bucket_count (struct bfd_link_info *info,
5258 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5259 unsigned long int nsyms,
5262 size_t best_size = 0;
5263 unsigned long int i;
5265 /* We have a problem here. The following code to optimize the table
5266 size requires an integer type with more the 32 bits. If
5267 BFD_HOST_U_64_BIT is set we know about such a type. */
5268 #ifdef BFD_HOST_U_64_BIT
5273 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5274 bfd *dynobj = elf_hash_table (info)->dynobj;
5275 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5276 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5277 unsigned long int *counts;
5280 /* Possible optimization parameters: if we have NSYMS symbols we say
5281 that the hashing table must at least have NSYMS/4 and at most
5283 minsize = nsyms / 4;
5286 best_size = maxsize = nsyms * 2;
5291 if ((best_size & 31) == 0)
5295 /* Create array where we count the collisions in. We must use bfd_malloc
5296 since the size could be large. */
5298 amt *= sizeof (unsigned long int);
5299 counts = bfd_malloc (amt);
5303 /* Compute the "optimal" size for the hash table. The criteria is a
5304 minimal chain length. The minor criteria is (of course) the size
5306 for (i = minsize; i < maxsize; ++i)
5308 /* Walk through the array of hashcodes and count the collisions. */
5309 BFD_HOST_U_64_BIT max;
5310 unsigned long int j;
5311 unsigned long int fact;
5313 if (gnu_hash && (i & 31) == 0)
5316 memset (counts, '\0', i * sizeof (unsigned long int));
5318 /* Determine how often each hash bucket is used. */
5319 for (j = 0; j < nsyms; ++j)
5320 ++counts[hashcodes[j] % i];
5322 /* For the weight function we need some information about the
5323 pagesize on the target. This is information need not be 100%
5324 accurate. Since this information is not available (so far) we
5325 define it here to a reasonable default value. If it is crucial
5326 to have a better value some day simply define this value. */
5327 # ifndef BFD_TARGET_PAGESIZE
5328 # define BFD_TARGET_PAGESIZE (4096)
5331 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5333 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5336 /* Variant 1: optimize for short chains. We add the squares
5337 of all the chain lengths (which favors many small chain
5338 over a few long chains). */
5339 for (j = 0; j < i; ++j)
5340 max += counts[j] * counts[j];
5342 /* This adds penalties for the overall size of the table. */
5343 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5346 /* Variant 2: Optimize a lot more for small table. Here we
5347 also add squares of the size but we also add penalties for
5348 empty slots (the +1 term). */
5349 for (j = 0; j < i; ++j)
5350 max += (1 + counts[j]) * (1 + counts[j]);
5352 /* The overall size of the table is considered, but not as
5353 strong as in variant 1, where it is squared. */
5354 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5358 /* Compare with current best results. */
5359 if (max < best_chlen)
5369 #endif /* defined (BFD_HOST_U_64_BIT) */
5371 /* This is the fallback solution if no 64bit type is available or if we
5372 are not supposed to spend much time on optimizations. We select the
5373 bucket count using a fixed set of numbers. */
5374 for (i = 0; elf_buckets[i] != 0; i++)
5376 best_size = elf_buckets[i];
5377 if (nsyms < elf_buckets[i + 1])
5380 if (gnu_hash && best_size < 2)
5387 /* Set up the sizes and contents of the ELF dynamic sections. This is
5388 called by the ELF linker emulation before_allocation routine. We
5389 must set the sizes of the sections before the linker sets the
5390 addresses of the various sections. */
5393 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5396 const char *filter_shlib,
5397 const char * const *auxiliary_filters,
5398 struct bfd_link_info *info,
5399 asection **sinterpptr,
5400 struct bfd_elf_version_tree *verdefs)
5402 bfd_size_type soname_indx;
5404 const struct elf_backend_data *bed;
5405 struct elf_assign_sym_version_info asvinfo;
5409 soname_indx = (bfd_size_type) -1;
5411 if (!is_elf_hash_table (info->hash))
5414 bed = get_elf_backend_data (output_bfd);
5415 if (info->execstack)
5416 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5417 else if (info->noexecstack)
5418 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5422 asection *notesec = NULL;
5425 for (inputobj = info->input_bfds;
5427 inputobj = inputobj->link_next)
5431 if (inputobj->flags & (DYNAMIC | EXEC_P | BFD_LINKER_CREATED))
5433 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5436 if (s->flags & SEC_CODE)
5440 else if (bed->default_execstack)
5445 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5446 if (exec && info->relocatable
5447 && notesec->output_section != bfd_abs_section_ptr)
5448 notesec->output_section->flags |= SEC_CODE;
5452 /* Any syms created from now on start with -1 in
5453 got.refcount/offset and plt.refcount/offset. */
5454 elf_hash_table (info)->init_got_refcount
5455 = elf_hash_table (info)->init_got_offset;
5456 elf_hash_table (info)->init_plt_refcount
5457 = elf_hash_table (info)->init_plt_offset;
5459 /* The backend may have to create some sections regardless of whether
5460 we're dynamic or not. */
5461 if (bed->elf_backend_always_size_sections
5462 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5465 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5468 dynobj = elf_hash_table (info)->dynobj;
5470 /* If there were no dynamic objects in the link, there is nothing to
5475 if (elf_hash_table (info)->dynamic_sections_created)
5477 struct elf_info_failed eif;
5478 struct elf_link_hash_entry *h;
5480 struct bfd_elf_version_tree *t;
5481 struct bfd_elf_version_expr *d;
5483 bfd_boolean all_defined;
5485 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5486 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5490 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5492 if (soname_indx == (bfd_size_type) -1
5493 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5499 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5501 info->flags |= DF_SYMBOLIC;
5508 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5510 if (indx == (bfd_size_type) -1
5511 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5514 if (info->new_dtags)
5516 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5517 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5522 if (filter_shlib != NULL)
5526 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5527 filter_shlib, TRUE);
5528 if (indx == (bfd_size_type) -1
5529 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5533 if (auxiliary_filters != NULL)
5535 const char * const *p;
5537 for (p = auxiliary_filters; *p != NULL; p++)
5541 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5543 if (indx == (bfd_size_type) -1
5544 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5550 eif.verdefs = verdefs;
5553 /* If we are supposed to export all symbols into the dynamic symbol
5554 table (this is not the normal case), then do so. */
5555 if (info->export_dynamic
5556 || (info->executable && info->dynamic))
5558 elf_link_hash_traverse (elf_hash_table (info),
5559 _bfd_elf_export_symbol,
5565 /* Make all global versions with definition. */
5566 for (t = verdefs; t != NULL; t = t->next)
5567 for (d = t->globals.list; d != NULL; d = d->next)
5568 if (!d->symver && d->symbol)
5570 const char *verstr, *name;
5571 size_t namelen, verlen, newlen;
5573 struct elf_link_hash_entry *newh;
5576 namelen = strlen (name);
5578 verlen = strlen (verstr);
5579 newlen = namelen + verlen + 3;
5581 newname = bfd_malloc (newlen);
5582 if (newname == NULL)
5584 memcpy (newname, name, namelen);
5586 /* Check the hidden versioned definition. */
5587 p = newname + namelen;
5589 memcpy (p, verstr, verlen + 1);
5590 newh = elf_link_hash_lookup (elf_hash_table (info),
5591 newname, FALSE, FALSE,
5594 || (newh->root.type != bfd_link_hash_defined
5595 && newh->root.type != bfd_link_hash_defweak))
5597 /* Check the default versioned definition. */
5599 memcpy (p, verstr, verlen + 1);
5600 newh = elf_link_hash_lookup (elf_hash_table (info),
5601 newname, FALSE, FALSE,
5606 /* Mark this version if there is a definition and it is
5607 not defined in a shared object. */
5609 && !newh->def_dynamic
5610 && (newh->root.type == bfd_link_hash_defined
5611 || newh->root.type == bfd_link_hash_defweak))
5615 /* Attach all the symbols to their version information. */
5616 asvinfo.output_bfd = output_bfd;
5617 asvinfo.info = info;
5618 asvinfo.verdefs = verdefs;
5619 asvinfo.failed = FALSE;
5621 elf_link_hash_traverse (elf_hash_table (info),
5622 _bfd_elf_link_assign_sym_version,
5627 if (!info->allow_undefined_version)
5629 /* Check if all global versions have a definition. */
5631 for (t = verdefs; t != NULL; t = t->next)
5632 for (d = t->globals.list; d != NULL; d = d->next)
5633 if (!d->symver && !d->script)
5635 (*_bfd_error_handler)
5636 (_("%s: undefined version: %s"),
5637 d->pattern, t->name);
5638 all_defined = FALSE;
5643 bfd_set_error (bfd_error_bad_value);
5648 /* Find all symbols which were defined in a dynamic object and make
5649 the backend pick a reasonable value for them. */
5650 elf_link_hash_traverse (elf_hash_table (info),
5651 _bfd_elf_adjust_dynamic_symbol,
5656 /* Add some entries to the .dynamic section. We fill in some of the
5657 values later, in bfd_elf_final_link, but we must add the entries
5658 now so that we know the final size of the .dynamic section. */
5660 /* If there are initialization and/or finalization functions to
5661 call then add the corresponding DT_INIT/DT_FINI entries. */
5662 h = (info->init_function
5663 ? elf_link_hash_lookup (elf_hash_table (info),
5664 info->init_function, FALSE,
5671 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5674 h = (info->fini_function
5675 ? elf_link_hash_lookup (elf_hash_table (info),
5676 info->fini_function, FALSE,
5683 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5687 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5688 if (s != NULL && s->linker_has_input)
5690 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5691 if (! info->executable)
5696 for (sub = info->input_bfds; sub != NULL;
5697 sub = sub->link_next)
5698 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5699 for (o = sub->sections; o != NULL; o = o->next)
5700 if (elf_section_data (o)->this_hdr.sh_type
5701 == SHT_PREINIT_ARRAY)
5703 (*_bfd_error_handler)
5704 (_("%B: .preinit_array section is not allowed in DSO"),
5709 bfd_set_error (bfd_error_nonrepresentable_section);
5713 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5714 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5717 s = bfd_get_section_by_name (output_bfd, ".init_array");
5718 if (s != NULL && s->linker_has_input)
5720 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5721 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5724 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5725 if (s != NULL && s->linker_has_input)
5727 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5728 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5732 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5733 /* If .dynstr is excluded from the link, we don't want any of
5734 these tags. Strictly, we should be checking each section
5735 individually; This quick check covers for the case where
5736 someone does a /DISCARD/ : { *(*) }. */
5737 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5739 bfd_size_type strsize;
5741 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5742 if ((info->emit_hash
5743 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5744 || (info->emit_gnu_hash
5745 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5746 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5747 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5748 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5749 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5750 bed->s->sizeof_sym))
5755 /* The backend must work out the sizes of all the other dynamic
5757 if (bed->elf_backend_size_dynamic_sections
5758 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5761 if (elf_hash_table (info)->dynamic_sections_created)
5763 unsigned long section_sym_count;
5766 /* Set up the version definition section. */
5767 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5768 BFD_ASSERT (s != NULL);
5770 /* We may have created additional version definitions if we are
5771 just linking a regular application. */
5772 verdefs = asvinfo.verdefs;
5774 /* Skip anonymous version tag. */
5775 if (verdefs != NULL && verdefs->vernum == 0)
5776 verdefs = verdefs->next;
5778 if (verdefs == NULL && !info->create_default_symver)
5779 s->flags |= SEC_EXCLUDE;
5784 struct bfd_elf_version_tree *t;
5786 Elf_Internal_Verdef def;
5787 Elf_Internal_Verdaux defaux;
5788 struct bfd_link_hash_entry *bh;
5789 struct elf_link_hash_entry *h;
5795 /* Make space for the base version. */
5796 size += sizeof (Elf_External_Verdef);
5797 size += sizeof (Elf_External_Verdaux);
5800 /* Make space for the default version. */
5801 if (info->create_default_symver)
5803 size += sizeof (Elf_External_Verdef);
5807 for (t = verdefs; t != NULL; t = t->next)
5809 struct bfd_elf_version_deps *n;
5811 size += sizeof (Elf_External_Verdef);
5812 size += sizeof (Elf_External_Verdaux);
5815 for (n = t->deps; n != NULL; n = n->next)
5816 size += sizeof (Elf_External_Verdaux);
5820 s->contents = bfd_alloc (output_bfd, s->size);
5821 if (s->contents == NULL && s->size != 0)
5824 /* Fill in the version definition section. */
5828 def.vd_version = VER_DEF_CURRENT;
5829 def.vd_flags = VER_FLG_BASE;
5832 if (info->create_default_symver)
5834 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5835 def.vd_next = sizeof (Elf_External_Verdef);
5839 def.vd_aux = sizeof (Elf_External_Verdef);
5840 def.vd_next = (sizeof (Elf_External_Verdef)
5841 + sizeof (Elf_External_Verdaux));
5844 if (soname_indx != (bfd_size_type) -1)
5846 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5848 def.vd_hash = bfd_elf_hash (soname);
5849 defaux.vda_name = soname_indx;
5856 name = lbasename (output_bfd->filename);
5857 def.vd_hash = bfd_elf_hash (name);
5858 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5860 if (indx == (bfd_size_type) -1)
5862 defaux.vda_name = indx;
5864 defaux.vda_next = 0;
5866 _bfd_elf_swap_verdef_out (output_bfd, &def,
5867 (Elf_External_Verdef *) p);
5868 p += sizeof (Elf_External_Verdef);
5869 if (info->create_default_symver)
5871 /* Add a symbol representing this version. */
5873 if (! (_bfd_generic_link_add_one_symbol
5874 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5876 get_elf_backend_data (dynobj)->collect, &bh)))
5878 h = (struct elf_link_hash_entry *) bh;
5881 h->type = STT_OBJECT;
5882 h->verinfo.vertree = NULL;
5884 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5887 /* Create a duplicate of the base version with the same
5888 aux block, but different flags. */
5891 def.vd_aux = sizeof (Elf_External_Verdef);
5893 def.vd_next = (sizeof (Elf_External_Verdef)
5894 + sizeof (Elf_External_Verdaux));
5897 _bfd_elf_swap_verdef_out (output_bfd, &def,
5898 (Elf_External_Verdef *) p);
5899 p += sizeof (Elf_External_Verdef);
5901 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5902 (Elf_External_Verdaux *) p);
5903 p += sizeof (Elf_External_Verdaux);
5905 for (t = verdefs; t != NULL; t = t->next)
5908 struct bfd_elf_version_deps *n;
5911 for (n = t->deps; n != NULL; n = n->next)
5914 /* Add a symbol representing this version. */
5916 if (! (_bfd_generic_link_add_one_symbol
5917 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5919 get_elf_backend_data (dynobj)->collect, &bh)))
5921 h = (struct elf_link_hash_entry *) bh;
5924 h->type = STT_OBJECT;
5925 h->verinfo.vertree = t;
5927 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5930 def.vd_version = VER_DEF_CURRENT;
5932 if (t->globals.list == NULL
5933 && t->locals.list == NULL
5935 def.vd_flags |= VER_FLG_WEAK;
5936 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5937 def.vd_cnt = cdeps + 1;
5938 def.vd_hash = bfd_elf_hash (t->name);
5939 def.vd_aux = sizeof (Elf_External_Verdef);
5941 if (t->next != NULL)
5942 def.vd_next = (sizeof (Elf_External_Verdef)
5943 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5945 _bfd_elf_swap_verdef_out (output_bfd, &def,
5946 (Elf_External_Verdef *) p);
5947 p += sizeof (Elf_External_Verdef);
5949 defaux.vda_name = h->dynstr_index;
5950 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5952 defaux.vda_next = 0;
5953 if (t->deps != NULL)
5954 defaux.vda_next = sizeof (Elf_External_Verdaux);
5955 t->name_indx = defaux.vda_name;
5957 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5958 (Elf_External_Verdaux *) p);
5959 p += sizeof (Elf_External_Verdaux);
5961 for (n = t->deps; n != NULL; n = n->next)
5963 if (n->version_needed == NULL)
5965 /* This can happen if there was an error in the
5967 defaux.vda_name = 0;
5971 defaux.vda_name = n->version_needed->name_indx;
5972 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5975 if (n->next == NULL)
5976 defaux.vda_next = 0;
5978 defaux.vda_next = sizeof (Elf_External_Verdaux);
5980 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5981 (Elf_External_Verdaux *) p);
5982 p += sizeof (Elf_External_Verdaux);
5986 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5987 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5990 elf_tdata (output_bfd)->cverdefs = cdefs;
5993 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5995 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5998 else if (info->flags & DF_BIND_NOW)
6000 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6006 if (info->executable)
6007 info->flags_1 &= ~ (DF_1_INITFIRST
6010 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6014 /* Work out the size of the version reference section. */
6016 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
6017 BFD_ASSERT (s != NULL);
6019 struct elf_find_verdep_info sinfo;
6021 sinfo.output_bfd = output_bfd;
6023 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6024 if (sinfo.vers == 0)
6026 sinfo.failed = FALSE;
6028 elf_link_hash_traverse (elf_hash_table (info),
6029 _bfd_elf_link_find_version_dependencies,
6034 if (elf_tdata (output_bfd)->verref == NULL)
6035 s->flags |= SEC_EXCLUDE;
6038 Elf_Internal_Verneed *t;
6043 /* Build the version definition section. */
6046 for (t = elf_tdata (output_bfd)->verref;
6050 Elf_Internal_Vernaux *a;
6052 size += sizeof (Elf_External_Verneed);
6054 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6055 size += sizeof (Elf_External_Vernaux);
6059 s->contents = bfd_alloc (output_bfd, s->size);
6060 if (s->contents == NULL)
6064 for (t = elf_tdata (output_bfd)->verref;
6069 Elf_Internal_Vernaux *a;
6073 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6076 t->vn_version = VER_NEED_CURRENT;
6078 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6079 elf_dt_name (t->vn_bfd) != NULL
6080 ? elf_dt_name (t->vn_bfd)
6081 : lbasename (t->vn_bfd->filename),
6083 if (indx == (bfd_size_type) -1)
6086 t->vn_aux = sizeof (Elf_External_Verneed);
6087 if (t->vn_nextref == NULL)
6090 t->vn_next = (sizeof (Elf_External_Verneed)
6091 + caux * sizeof (Elf_External_Vernaux));
6093 _bfd_elf_swap_verneed_out (output_bfd, t,
6094 (Elf_External_Verneed *) p);
6095 p += sizeof (Elf_External_Verneed);
6097 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6099 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6100 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6101 a->vna_nodename, FALSE);
6102 if (indx == (bfd_size_type) -1)
6105 if (a->vna_nextptr == NULL)
6108 a->vna_next = sizeof (Elf_External_Vernaux);
6110 _bfd_elf_swap_vernaux_out (output_bfd, a,
6111 (Elf_External_Vernaux *) p);
6112 p += sizeof (Elf_External_Vernaux);
6116 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6117 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6120 elf_tdata (output_bfd)->cverrefs = crefs;
6124 if ((elf_tdata (output_bfd)->cverrefs == 0
6125 && elf_tdata (output_bfd)->cverdefs == 0)
6126 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6127 §ion_sym_count) == 0)
6129 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6130 s->flags |= SEC_EXCLUDE;
6136 /* Find the first non-excluded output section. We'll use its
6137 section symbol for some emitted relocs. */
6139 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6143 for (s = output_bfd->sections; s != NULL; s = s->next)
6144 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6145 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6147 elf_hash_table (info)->text_index_section = s;
6152 /* Find two non-excluded output sections, one for code, one for data.
6153 We'll use their section symbols for some emitted relocs. */
6155 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6159 /* Data first, since setting text_index_section changes
6160 _bfd_elf_link_omit_section_dynsym. */
6161 for (s = output_bfd->sections; s != NULL; s = s->next)
6162 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6163 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6165 elf_hash_table (info)->data_index_section = s;
6169 for (s = output_bfd->sections; s != NULL; s = s->next)
6170 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6171 == (SEC_ALLOC | SEC_READONLY))
6172 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6174 elf_hash_table (info)->text_index_section = s;
6178 if (elf_hash_table (info)->text_index_section == NULL)
6179 elf_hash_table (info)->text_index_section
6180 = elf_hash_table (info)->data_index_section;
6184 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6186 const struct elf_backend_data *bed;
6188 if (!is_elf_hash_table (info->hash))
6191 bed = get_elf_backend_data (output_bfd);
6192 (*bed->elf_backend_init_index_section) (output_bfd, info);
6194 if (elf_hash_table (info)->dynamic_sections_created)
6198 bfd_size_type dynsymcount;
6199 unsigned long section_sym_count;
6200 unsigned int dtagcount;
6202 dynobj = elf_hash_table (info)->dynobj;
6204 /* Assign dynsym indicies. In a shared library we generate a
6205 section symbol for each output section, which come first.
6206 Next come all of the back-end allocated local dynamic syms,
6207 followed by the rest of the global symbols. */
6209 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6210 §ion_sym_count);
6212 /* Work out the size of the symbol version section. */
6213 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6214 BFD_ASSERT (s != NULL);
6215 if (dynsymcount != 0
6216 && (s->flags & SEC_EXCLUDE) == 0)
6218 s->size = dynsymcount * sizeof (Elf_External_Versym);
6219 s->contents = bfd_zalloc (output_bfd, s->size);
6220 if (s->contents == NULL)
6223 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6227 /* Set the size of the .dynsym and .hash sections. We counted
6228 the number of dynamic symbols in elf_link_add_object_symbols.
6229 We will build the contents of .dynsym and .hash when we build
6230 the final symbol table, because until then we do not know the
6231 correct value to give the symbols. We built the .dynstr
6232 section as we went along in elf_link_add_object_symbols. */
6233 s = bfd_get_section_by_name (dynobj, ".dynsym");
6234 BFD_ASSERT (s != NULL);
6235 s->size = dynsymcount * bed->s->sizeof_sym;
6237 if (dynsymcount != 0)
6239 s->contents = bfd_alloc (output_bfd, s->size);
6240 if (s->contents == NULL)
6243 /* The first entry in .dynsym is a dummy symbol.
6244 Clear all the section syms, in case we don't output them all. */
6245 ++section_sym_count;
6246 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6249 elf_hash_table (info)->bucketcount = 0;
6251 /* Compute the size of the hashing table. As a side effect this
6252 computes the hash values for all the names we export. */
6253 if (info->emit_hash)
6255 unsigned long int *hashcodes;
6256 struct hash_codes_info hashinf;
6258 unsigned long int nsyms;
6260 size_t hash_entry_size;
6262 /* Compute the hash values for all exported symbols. At the same
6263 time store the values in an array so that we could use them for
6265 amt = dynsymcount * sizeof (unsigned long int);
6266 hashcodes = bfd_malloc (amt);
6267 if (hashcodes == NULL)
6269 hashinf.hashcodes = hashcodes;
6270 hashinf.error = FALSE;
6272 /* Put all hash values in HASHCODES. */
6273 elf_link_hash_traverse (elf_hash_table (info),
6274 elf_collect_hash_codes, &hashinf);
6281 nsyms = hashinf.hashcodes - hashcodes;
6283 = compute_bucket_count (info, hashcodes, nsyms, 0);
6286 if (bucketcount == 0)
6289 elf_hash_table (info)->bucketcount = bucketcount;
6291 s = bfd_get_section_by_name (dynobj, ".hash");
6292 BFD_ASSERT (s != NULL);
6293 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6294 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6295 s->contents = bfd_zalloc (output_bfd, s->size);
6296 if (s->contents == NULL)
6299 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6300 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6301 s->contents + hash_entry_size);
6304 if (info->emit_gnu_hash)
6307 unsigned char *contents;
6308 struct collect_gnu_hash_codes cinfo;
6312 memset (&cinfo, 0, sizeof (cinfo));
6314 /* Compute the hash values for all exported symbols. At the same
6315 time store the values in an array so that we could use them for
6317 amt = dynsymcount * 2 * sizeof (unsigned long int);
6318 cinfo.hashcodes = bfd_malloc (amt);
6319 if (cinfo.hashcodes == NULL)
6322 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6323 cinfo.min_dynindx = -1;
6324 cinfo.output_bfd = output_bfd;
6327 /* Put all hash values in HASHCODES. */
6328 elf_link_hash_traverse (elf_hash_table (info),
6329 elf_collect_gnu_hash_codes, &cinfo);
6332 free (cinfo.hashcodes);
6337 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6339 if (bucketcount == 0)
6341 free (cinfo.hashcodes);
6345 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6346 BFD_ASSERT (s != NULL);
6348 if (cinfo.nsyms == 0)
6350 /* Empty .gnu.hash section is special. */
6351 BFD_ASSERT (cinfo.min_dynindx == -1);
6352 free (cinfo.hashcodes);
6353 s->size = 5 * 4 + bed->s->arch_size / 8;
6354 contents = bfd_zalloc (output_bfd, s->size);
6355 if (contents == NULL)
6357 s->contents = contents;
6358 /* 1 empty bucket. */
6359 bfd_put_32 (output_bfd, 1, contents);
6360 /* SYMIDX above the special symbol 0. */
6361 bfd_put_32 (output_bfd, 1, contents + 4);
6362 /* Just one word for bitmask. */
6363 bfd_put_32 (output_bfd, 1, contents + 8);
6364 /* Only hash fn bloom filter. */
6365 bfd_put_32 (output_bfd, 0, contents + 12);
6366 /* No hashes are valid - empty bitmask. */
6367 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6368 /* No hashes in the only bucket. */
6369 bfd_put_32 (output_bfd, 0,
6370 contents + 16 + bed->s->arch_size / 8);
6374 unsigned long int maskwords, maskbitslog2;
6375 BFD_ASSERT (cinfo.min_dynindx != -1);
6377 maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
6378 if (maskbitslog2 < 3)
6380 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6381 maskbitslog2 = maskbitslog2 + 3;
6383 maskbitslog2 = maskbitslog2 + 2;
6384 if (bed->s->arch_size == 64)
6386 if (maskbitslog2 == 5)
6392 cinfo.mask = (1 << cinfo.shift1) - 1;
6393 cinfo.shift2 = maskbitslog2;
6394 cinfo.maskbits = 1 << maskbitslog2;
6395 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6396 amt = bucketcount * sizeof (unsigned long int) * 2;
6397 amt += maskwords * sizeof (bfd_vma);
6398 cinfo.bitmask = bfd_malloc (amt);
6399 if (cinfo.bitmask == NULL)
6401 free (cinfo.hashcodes);
6405 cinfo.counts = (void *) (cinfo.bitmask + maskwords);
6406 cinfo.indx = cinfo.counts + bucketcount;
6407 cinfo.symindx = dynsymcount - cinfo.nsyms;
6408 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6410 /* Determine how often each hash bucket is used. */
6411 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6412 for (i = 0; i < cinfo.nsyms; ++i)
6413 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6415 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6416 if (cinfo.counts[i] != 0)
6418 cinfo.indx[i] = cnt;
6419 cnt += cinfo.counts[i];
6421 BFD_ASSERT (cnt == dynsymcount);
6422 cinfo.bucketcount = bucketcount;
6423 cinfo.local_indx = cinfo.min_dynindx;
6425 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6426 s->size += cinfo.maskbits / 8;
6427 contents = bfd_zalloc (output_bfd, s->size);
6428 if (contents == NULL)
6430 free (cinfo.bitmask);
6431 free (cinfo.hashcodes);
6435 s->contents = contents;
6436 bfd_put_32 (output_bfd, bucketcount, contents);
6437 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6438 bfd_put_32 (output_bfd, maskwords, contents + 8);
6439 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6440 contents += 16 + cinfo.maskbits / 8;
6442 for (i = 0; i < bucketcount; ++i)
6444 if (cinfo.counts[i] == 0)
6445 bfd_put_32 (output_bfd, 0, contents);
6447 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6451 cinfo.contents = contents;
6453 /* Renumber dynamic symbols, populate .gnu.hash section. */
6454 elf_link_hash_traverse (elf_hash_table (info),
6455 elf_renumber_gnu_hash_syms, &cinfo);
6457 contents = s->contents + 16;
6458 for (i = 0; i < maskwords; ++i)
6460 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6462 contents += bed->s->arch_size / 8;
6465 free (cinfo.bitmask);
6466 free (cinfo.hashcodes);
6470 s = bfd_get_section_by_name (dynobj, ".dynstr");
6471 BFD_ASSERT (s != NULL);
6473 elf_finalize_dynstr (output_bfd, info);
6475 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6477 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6478 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6485 /* Indicate that we are only retrieving symbol values from this
6489 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
6491 if (is_elf_hash_table (info->hash))
6492 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
6493 _bfd_generic_link_just_syms (sec, info);
6496 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6499 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6502 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
6503 sec->sec_info_type = ELF_INFO_TYPE_NONE;
6506 /* Finish SHF_MERGE section merging. */
6509 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6514 if (!is_elf_hash_table (info->hash))
6517 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6518 if ((ibfd->flags & DYNAMIC) == 0)
6519 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6520 if ((sec->flags & SEC_MERGE) != 0
6521 && !bfd_is_abs_section (sec->output_section))
6523 struct bfd_elf_section_data *secdata;
6525 secdata = elf_section_data (sec);
6526 if (! _bfd_add_merge_section (abfd,
6527 &elf_hash_table (info)->merge_info,
6528 sec, &secdata->sec_info))
6530 else if (secdata->sec_info)
6531 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
6534 if (elf_hash_table (info)->merge_info != NULL)
6535 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6536 merge_sections_remove_hook);
6540 /* Create an entry in an ELF linker hash table. */
6542 struct bfd_hash_entry *
6543 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6544 struct bfd_hash_table *table,
6547 /* Allocate the structure if it has not already been allocated by a
6551 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6556 /* Call the allocation method of the superclass. */
6557 entry = _bfd_link_hash_newfunc (entry, table, string);
6560 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6561 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6563 /* Set local fields. */
6566 ret->got = htab->init_got_refcount;
6567 ret->plt = htab->init_plt_refcount;
6568 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6569 - offsetof (struct elf_link_hash_entry, size)));
6570 /* Assume that we have been called by a non-ELF symbol reader.
6571 This flag is then reset by the code which reads an ELF input
6572 file. This ensures that a symbol created by a non-ELF symbol
6573 reader will have the flag set correctly. */
6580 /* Copy data from an indirect symbol to its direct symbol, hiding the
6581 old indirect symbol. Also used for copying flags to a weakdef. */
6584 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6585 struct elf_link_hash_entry *dir,
6586 struct elf_link_hash_entry *ind)
6588 struct elf_link_hash_table *htab;
6590 /* Copy down any references that we may have already seen to the
6591 symbol which just became indirect. */
6593 dir->ref_dynamic |= ind->ref_dynamic;
6594 dir->ref_regular |= ind->ref_regular;
6595 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6596 dir->non_got_ref |= ind->non_got_ref;
6597 dir->needs_plt |= ind->needs_plt;
6598 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6600 if (ind->root.type != bfd_link_hash_indirect)
6603 /* Copy over the global and procedure linkage table refcount entries.
6604 These may have been already set up by a check_relocs routine. */
6605 htab = elf_hash_table (info);
6606 if (ind->got.refcount > htab->init_got_refcount.refcount)
6608 if (dir->got.refcount < 0)
6609 dir->got.refcount = 0;
6610 dir->got.refcount += ind->got.refcount;
6611 ind->got.refcount = htab->init_got_refcount.refcount;
6614 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6616 if (dir->plt.refcount < 0)
6617 dir->plt.refcount = 0;
6618 dir->plt.refcount += ind->plt.refcount;
6619 ind->plt.refcount = htab->init_plt_refcount.refcount;
6622 if (ind->dynindx != -1)
6624 if (dir->dynindx != -1)
6625 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6626 dir->dynindx = ind->dynindx;
6627 dir->dynstr_index = ind->dynstr_index;
6629 ind->dynstr_index = 0;
6634 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6635 struct elf_link_hash_entry *h,
6636 bfd_boolean force_local)
6638 h->plt = elf_hash_table (info)->init_plt_offset;
6642 h->forced_local = 1;
6643 if (h->dynindx != -1)
6646 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6652 /* Initialize an ELF linker hash table. */
6655 _bfd_elf_link_hash_table_init
6656 (struct elf_link_hash_table *table,
6658 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6659 struct bfd_hash_table *,
6661 unsigned int entsize)
6664 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6666 memset (table, 0, sizeof * table);
6667 table->init_got_refcount.refcount = can_refcount - 1;
6668 table->init_plt_refcount.refcount = can_refcount - 1;
6669 table->init_got_offset.offset = -(bfd_vma) 1;
6670 table->init_plt_offset.offset = -(bfd_vma) 1;
6671 /* The first dynamic symbol is a dummy. */
6672 table->dynsymcount = 1;
6674 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6675 table->root.type = bfd_link_elf_hash_table;
6680 /* Create an ELF linker hash table. */
6682 struct bfd_link_hash_table *
6683 _bfd_elf_link_hash_table_create (bfd *abfd)
6685 struct elf_link_hash_table *ret;
6686 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6688 ret = bfd_malloc (amt);
6692 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
6693 sizeof (struct elf_link_hash_entry)))
6702 /* This is a hook for the ELF emulation code in the generic linker to
6703 tell the backend linker what file name to use for the DT_NEEDED
6704 entry for a dynamic object. */
6707 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6709 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6710 && bfd_get_format (abfd) == bfd_object)
6711 elf_dt_name (abfd) = name;
6715 bfd_elf_get_dyn_lib_class (bfd *abfd)
6718 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6719 && bfd_get_format (abfd) == bfd_object)
6720 lib_class = elf_dyn_lib_class (abfd);
6727 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6729 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6730 && bfd_get_format (abfd) == bfd_object)
6731 elf_dyn_lib_class (abfd) = lib_class;
6734 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6735 the linker ELF emulation code. */
6737 struct bfd_link_needed_list *
6738 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6739 struct bfd_link_info *info)
6741 if (! is_elf_hash_table (info->hash))
6743 return elf_hash_table (info)->needed;
6746 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6747 hook for the linker ELF emulation code. */
6749 struct bfd_link_needed_list *
6750 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6751 struct bfd_link_info *info)
6753 if (! is_elf_hash_table (info->hash))
6755 return elf_hash_table (info)->runpath;
6758 /* Get the name actually used for a dynamic object for a link. This
6759 is the SONAME entry if there is one. Otherwise, it is the string
6760 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6763 bfd_elf_get_dt_soname (bfd *abfd)
6765 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6766 && bfd_get_format (abfd) == bfd_object)
6767 return elf_dt_name (abfd);
6771 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6772 the ELF linker emulation code. */
6775 bfd_elf_get_bfd_needed_list (bfd *abfd,
6776 struct bfd_link_needed_list **pneeded)
6779 bfd_byte *dynbuf = NULL;
6780 unsigned int elfsec;
6781 unsigned long shlink;
6782 bfd_byte *extdyn, *extdynend;
6784 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6788 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6789 || bfd_get_format (abfd) != bfd_object)
6792 s = bfd_get_section_by_name (abfd, ".dynamic");
6793 if (s == NULL || s->size == 0)
6796 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6799 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
6800 if (elfsec == SHN_BAD)
6803 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
6805 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
6806 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
6809 extdynend = extdyn + s->size;
6810 for (; extdyn < extdynend; extdyn += extdynsize)
6812 Elf_Internal_Dyn dyn;
6814 (*swap_dyn_in) (abfd, extdyn, &dyn);
6816 if (dyn.d_tag == DT_NULL)
6819 if (dyn.d_tag == DT_NEEDED)
6822 struct bfd_link_needed_list *l;
6823 unsigned int tagv = dyn.d_un.d_val;
6826 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
6831 l = bfd_alloc (abfd, amt);
6852 struct elf_symbuf_symbol
6854 unsigned long st_name; /* Symbol name, index in string tbl */
6855 unsigned char st_info; /* Type and binding attributes */
6856 unsigned char st_other; /* Visibilty, and target specific */
6859 struct elf_symbuf_head
6861 struct elf_symbuf_symbol *ssym;
6862 bfd_size_type count;
6863 unsigned int st_shndx;
6870 Elf_Internal_Sym *isym;
6871 struct elf_symbuf_symbol *ssym;
6876 /* Sort references to symbols by ascending section number. */
6879 elf_sort_elf_symbol (const void *arg1, const void *arg2)
6881 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
6882 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
6884 return s1->st_shndx - s2->st_shndx;
6888 elf_sym_name_compare (const void *arg1, const void *arg2)
6890 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
6891 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
6892 return strcmp (s1->name, s2->name);
6895 static struct elf_symbuf_head *
6896 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
6898 Elf_Internal_Sym **ind, **indbufend, **indbuf;
6899 struct elf_symbuf_symbol *ssym;
6900 struct elf_symbuf_head *ssymbuf, *ssymhead;
6901 bfd_size_type i, shndx_count, total_size;
6903 indbuf = bfd_malloc2 (symcount, sizeof (*indbuf));
6907 for (ind = indbuf, i = 0; i < symcount; i++)
6908 if (isymbuf[i].st_shndx != SHN_UNDEF)
6909 *ind++ = &isymbuf[i];
6912 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
6913 elf_sort_elf_symbol);
6916 if (indbufend > indbuf)
6917 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
6918 if (ind[0]->st_shndx != ind[1]->st_shndx)
6921 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
6922 + (indbufend - indbuf) * sizeof (*ssym));
6923 ssymbuf = bfd_malloc (total_size);
6924 if (ssymbuf == NULL)
6930 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
6931 ssymbuf->ssym = NULL;
6932 ssymbuf->count = shndx_count;
6933 ssymbuf->st_shndx = 0;
6934 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
6936 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
6939 ssymhead->ssym = ssym;
6940 ssymhead->count = 0;
6941 ssymhead->st_shndx = (*ind)->st_shndx;
6943 ssym->st_name = (*ind)->st_name;
6944 ssym->st_info = (*ind)->st_info;
6945 ssym->st_other = (*ind)->st_other;
6948 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
6949 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
6956 /* Check if 2 sections define the same set of local and global
6960 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
6961 struct bfd_link_info *info)
6964 const struct elf_backend_data *bed1, *bed2;
6965 Elf_Internal_Shdr *hdr1, *hdr2;
6966 bfd_size_type symcount1, symcount2;
6967 Elf_Internal_Sym *isymbuf1, *isymbuf2;
6968 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
6969 Elf_Internal_Sym *isym, *isymend;
6970 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
6971 bfd_size_type count1, count2, i;
6972 unsigned int shndx1, shndx2;
6978 /* Both sections have to be in ELF. */
6979 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
6980 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
6983 if (elf_section_type (sec1) != elf_section_type (sec2))
6986 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
6987 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
6988 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
6991 bed1 = get_elf_backend_data (bfd1);
6992 bed2 = get_elf_backend_data (bfd2);
6993 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
6994 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
6995 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
6996 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
6998 if (symcount1 == 0 || symcount2 == 0)
7004 ssymbuf1 = elf_tdata (bfd1)->symbuf;
7005 ssymbuf2 = elf_tdata (bfd2)->symbuf;
7007 if (ssymbuf1 == NULL)
7009 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7011 if (isymbuf1 == NULL)
7014 if (!info->reduce_memory_overheads)
7015 elf_tdata (bfd1)->symbuf = ssymbuf1
7016 = elf_create_symbuf (symcount1, isymbuf1);
7019 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7021 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7023 if (isymbuf2 == NULL)
7026 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7027 elf_tdata (bfd2)->symbuf = ssymbuf2
7028 = elf_create_symbuf (symcount2, isymbuf2);
7031 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7033 /* Optimized faster version. */
7034 bfd_size_type lo, hi, mid;
7035 struct elf_symbol *symp;
7036 struct elf_symbuf_symbol *ssym, *ssymend;
7039 hi = ssymbuf1->count;
7044 mid = (lo + hi) / 2;
7045 if (shndx1 < ssymbuf1[mid].st_shndx)
7047 else if (shndx1 > ssymbuf1[mid].st_shndx)
7051 count1 = ssymbuf1[mid].count;
7058 hi = ssymbuf2->count;
7063 mid = (lo + hi) / 2;
7064 if (shndx2 < ssymbuf2[mid].st_shndx)
7066 else if (shndx2 > ssymbuf2[mid].st_shndx)
7070 count2 = ssymbuf2[mid].count;
7076 if (count1 == 0 || count2 == 0 || count1 != count2)
7079 symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol));
7080 symtable2 = bfd_malloc (count2 * sizeof (struct elf_symbol));
7081 if (symtable1 == NULL || symtable2 == NULL)
7085 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7086 ssym < ssymend; ssym++, symp++)
7088 symp->u.ssym = ssym;
7089 symp->name = bfd_elf_string_from_elf_section (bfd1,
7095 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7096 ssym < ssymend; ssym++, symp++)
7098 symp->u.ssym = ssym;
7099 symp->name = bfd_elf_string_from_elf_section (bfd2,
7104 /* Sort symbol by name. */
7105 qsort (symtable1, count1, sizeof (struct elf_symbol),
7106 elf_sym_name_compare);
7107 qsort (symtable2, count1, sizeof (struct elf_symbol),
7108 elf_sym_name_compare);
7110 for (i = 0; i < count1; i++)
7111 /* Two symbols must have the same binding, type and name. */
7112 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7113 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7114 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7121 symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7122 symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7123 if (symtable1 == NULL || symtable2 == NULL)
7126 /* Count definitions in the section. */
7128 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7129 if (isym->st_shndx == shndx1)
7130 symtable1[count1++].u.isym = isym;
7133 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7134 if (isym->st_shndx == shndx2)
7135 symtable2[count2++].u.isym = isym;
7137 if (count1 == 0 || count2 == 0 || count1 != count2)
7140 for (i = 0; i < count1; i++)
7142 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7143 symtable1[i].u.isym->st_name);
7145 for (i = 0; i < count2; i++)
7147 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7148 symtable2[i].u.isym->st_name);
7150 /* Sort symbol by name. */
7151 qsort (symtable1, count1, sizeof (struct elf_symbol),
7152 elf_sym_name_compare);
7153 qsort (symtable2, count1, sizeof (struct elf_symbol),
7154 elf_sym_name_compare);
7156 for (i = 0; i < count1; i++)
7157 /* Two symbols must have the same binding, type and name. */
7158 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7159 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7160 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7178 /* Return TRUE if 2 section types are compatible. */
7181 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7182 bfd *bbfd, const asection *bsec)
7186 || abfd->xvec->flavour != bfd_target_elf_flavour
7187 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7190 return elf_section_type (asec) == elf_section_type (bsec);
7193 /* Final phase of ELF linker. */
7195 /* A structure we use to avoid passing large numbers of arguments. */
7197 struct elf_final_link_info
7199 /* General link information. */
7200 struct bfd_link_info *info;
7203 /* Symbol string table. */
7204 struct bfd_strtab_hash *symstrtab;
7205 /* .dynsym section. */
7206 asection *dynsym_sec;
7207 /* .hash section. */
7209 /* symbol version section (.gnu.version). */
7210 asection *symver_sec;
7211 /* Buffer large enough to hold contents of any section. */
7213 /* Buffer large enough to hold external relocs of any section. */
7214 void *external_relocs;
7215 /* Buffer large enough to hold internal relocs of any section. */
7216 Elf_Internal_Rela *internal_relocs;
7217 /* Buffer large enough to hold external local symbols of any input
7219 bfd_byte *external_syms;
7220 /* And a buffer for symbol section indices. */
7221 Elf_External_Sym_Shndx *locsym_shndx;
7222 /* Buffer large enough to hold internal local symbols of any input
7224 Elf_Internal_Sym *internal_syms;
7225 /* Array large enough to hold a symbol index for each local symbol
7226 of any input BFD. */
7228 /* Array large enough to hold a section pointer for each local
7229 symbol of any input BFD. */
7230 asection **sections;
7231 /* Buffer to hold swapped out symbols. */
7233 /* And one for symbol section indices. */
7234 Elf_External_Sym_Shndx *symshndxbuf;
7235 /* Number of swapped out symbols in buffer. */
7236 size_t symbuf_count;
7237 /* Number of symbols which fit in symbuf. */
7239 /* And same for symshndxbuf. */
7240 size_t shndxbuf_size;
7243 /* This struct is used to pass information to elf_link_output_extsym. */
7245 struct elf_outext_info
7248 bfd_boolean localsyms;
7249 struct elf_final_link_info *finfo;
7253 /* Support for evaluating a complex relocation.
7255 Complex relocations are generalized, self-describing relocations. The
7256 implementation of them consists of two parts: complex symbols, and the
7257 relocations themselves.
7259 The relocations are use a reserved elf-wide relocation type code (R_RELC
7260 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7261 information (start bit, end bit, word width, etc) into the addend. This
7262 information is extracted from CGEN-generated operand tables within gas.
7264 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7265 internal) representing prefix-notation expressions, including but not
7266 limited to those sorts of expressions normally encoded as addends in the
7267 addend field. The symbol mangling format is:
7270 | <unary-operator> ':' <node>
7271 | <binary-operator> ':' <node> ':' <node>
7274 <literal> := 's' <digits=N> ':' <N character symbol name>
7275 | 'S' <digits=N> ':' <N character section name>
7279 <binary-operator> := as in C
7280 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7283 set_symbol_value (bfd *bfd_with_globals,
7284 Elf_Internal_Sym *isymbuf,
7289 struct elf_link_hash_entry **sym_hashes;
7290 struct elf_link_hash_entry *h;
7291 size_t extsymoff = locsymcount;
7293 if (symidx < locsymcount)
7295 Elf_Internal_Sym *sym;
7297 sym = isymbuf + symidx;
7298 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7300 /* It is a local symbol: move it to the
7301 "absolute" section and give it a value. */
7302 sym->st_shndx = SHN_ABS;
7303 sym->st_value = val;
7306 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7310 /* It is a global symbol: set its link type
7311 to "defined" and give it a value. */
7313 sym_hashes = elf_sym_hashes (bfd_with_globals);
7314 h = sym_hashes [symidx - extsymoff];
7315 while (h->root.type == bfd_link_hash_indirect
7316 || h->root.type == bfd_link_hash_warning)
7317 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7318 h->root.type = bfd_link_hash_defined;
7319 h->root.u.def.value = val;
7320 h->root.u.def.section = bfd_abs_section_ptr;
7324 resolve_symbol (const char *name,
7326 struct elf_final_link_info *finfo,
7328 Elf_Internal_Sym *isymbuf,
7331 Elf_Internal_Sym *sym;
7332 struct bfd_link_hash_entry *global_entry;
7333 const char *candidate = NULL;
7334 Elf_Internal_Shdr *symtab_hdr;
7337 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7339 for (i = 0; i < locsymcount; ++ i)
7343 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7346 candidate = bfd_elf_string_from_elf_section (input_bfd,
7347 symtab_hdr->sh_link,
7350 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7351 name, candidate, (unsigned long) sym->st_value);
7353 if (candidate && strcmp (candidate, name) == 0)
7355 asection *sec = finfo->sections [i];
7357 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7358 *result += sec->output_offset + sec->output_section->vma;
7360 printf ("Found symbol with value %8.8lx\n",
7361 (unsigned long) *result);
7367 /* Hmm, haven't found it yet. perhaps it is a global. */
7368 global_entry = bfd_link_hash_lookup (finfo->info->hash, name,
7369 FALSE, FALSE, TRUE);
7373 if (global_entry->type == bfd_link_hash_defined
7374 || global_entry->type == bfd_link_hash_defweak)
7376 *result = (global_entry->u.def.value
7377 + global_entry->u.def.section->output_section->vma
7378 + global_entry->u.def.section->output_offset);
7380 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7381 global_entry->root.string, (unsigned long) *result);
7390 resolve_section (const char *name,
7397 for (curr = sections; curr; curr = curr->next)
7398 if (strcmp (curr->name, name) == 0)
7400 *result = curr->vma;
7404 /* Hmm. still haven't found it. try pseudo-section names. */
7405 for (curr = sections; curr; curr = curr->next)
7407 len = strlen (curr->name);
7408 if (len > strlen (name))
7411 if (strncmp (curr->name, name, len) == 0)
7413 if (strncmp (".end", name + len, 4) == 0)
7415 *result = curr->vma + curr->size;
7419 /* Insert more pseudo-section names here, if you like. */
7427 undefined_reference (const char *reftype, const char *name)
7429 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7434 eval_symbol (bfd_vma *result,
7437 struct elf_final_link_info *finfo,
7439 Elf_Internal_Sym *isymbuf,
7448 const char *sym = *symp;
7450 bfd_boolean symbol_is_section = FALSE;
7455 if (len < 1 || len > sizeof (symbuf))
7457 bfd_set_error (bfd_error_invalid_operation);
7470 *result = strtoul (sym, (char **) symp, 16);
7474 symbol_is_section = TRUE;
7477 symlen = strtol (sym, (char **) symp, 10);
7478 sym = *symp + 1; /* Skip the trailing ':'. */
7480 if (symend < sym || symlen + 1 > sizeof (symbuf))
7482 bfd_set_error (bfd_error_invalid_operation);
7486 memcpy (symbuf, sym, symlen);
7487 symbuf[symlen] = '\0';
7488 *symp = sym + symlen;
7490 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7491 the symbol as a section, or vice-versa. so we're pretty liberal in our
7492 interpretation here; section means "try section first", not "must be a
7493 section", and likewise with symbol. */
7495 if (symbol_is_section)
7497 if (!resolve_section (symbuf, finfo->output_bfd->sections, result)
7498 && !resolve_symbol (symbuf, input_bfd, finfo, result,
7499 isymbuf, locsymcount))
7501 undefined_reference ("section", symbuf);
7507 if (!resolve_symbol (symbuf, input_bfd, finfo, result,
7508 isymbuf, locsymcount)
7509 && !resolve_section (symbuf, finfo->output_bfd->sections,
7512 undefined_reference ("symbol", symbuf);
7519 /* All that remains are operators. */
7521 #define UNARY_OP(op) \
7522 if (strncmp (sym, #op, strlen (#op)) == 0) \
7524 sym += strlen (#op); \
7528 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7529 isymbuf, locsymcount, signed_p)) \
7532 *result = op ((bfd_signed_vma) a); \
7538 #define BINARY_OP(op) \
7539 if (strncmp (sym, #op, strlen (#op)) == 0) \
7541 sym += strlen (#op); \
7545 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7546 isymbuf, locsymcount, signed_p)) \
7549 if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \
7550 isymbuf, locsymcount, signed_p)) \
7553 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7583 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7584 bfd_set_error (bfd_error_invalid_operation);
7590 put_value (bfd_vma size,
7591 unsigned long chunksz,
7596 location += (size - chunksz);
7598 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
7606 bfd_put_8 (input_bfd, x, location);
7609 bfd_put_16 (input_bfd, x, location);
7612 bfd_put_32 (input_bfd, x, location);
7616 bfd_put_64 (input_bfd, x, location);
7626 get_value (bfd_vma size,
7627 unsigned long chunksz,
7633 for (; size; size -= chunksz, location += chunksz)
7641 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7644 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7647 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7651 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7662 decode_complex_addend (unsigned long *start, /* in bits */
7663 unsigned long *oplen, /* in bits */
7664 unsigned long *len, /* in bits */
7665 unsigned long *wordsz, /* in bytes */
7666 unsigned long *chunksz, /* in bytes */
7667 unsigned long *lsb0_p,
7668 unsigned long *signed_p,
7669 unsigned long *trunc_p,
7670 unsigned long encoded)
7672 * start = encoded & 0x3F;
7673 * len = (encoded >> 6) & 0x3F;
7674 * oplen = (encoded >> 12) & 0x3F;
7675 * wordsz = (encoded >> 18) & 0xF;
7676 * chunksz = (encoded >> 22) & 0xF;
7677 * lsb0_p = (encoded >> 27) & 1;
7678 * signed_p = (encoded >> 28) & 1;
7679 * trunc_p = (encoded >> 29) & 1;
7682 bfd_reloc_status_type
7683 bfd_elf_perform_complex_relocation (bfd *input_bfd,
7684 asection *input_section ATTRIBUTE_UNUSED,
7686 Elf_Internal_Rela *rel,
7689 bfd_vma shift, x, mask;
7690 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
7691 bfd_reloc_status_type r;
7693 /* Perform this reloc, since it is complex.
7694 (this is not to say that it necessarily refers to a complex
7695 symbol; merely that it is a self-describing CGEN based reloc.
7696 i.e. the addend has the complete reloc information (bit start, end,
7697 word size, etc) encoded within it.). */
7699 decode_complex_addend (&start, &oplen, &len, &wordsz,
7700 &chunksz, &lsb0_p, &signed_p,
7701 &trunc_p, rel->r_addend);
7703 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7706 shift = (start + 1) - len;
7708 shift = (8 * wordsz) - (start + len);
7710 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7713 printf ("Doing complex reloc: "
7714 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7715 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7716 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7717 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7718 oplen, x, mask, relocation);
7723 /* Now do an overflow check. */
7724 r = bfd_check_overflow ((signed_p
7725 ? complain_overflow_signed
7726 : complain_overflow_unsigned),
7727 len, 0, (8 * wordsz),
7731 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7734 printf (" relocation: %8.8lx\n"
7735 " shifted mask: %8.8lx\n"
7736 " shifted/masked reloc: %8.8lx\n"
7737 " result: %8.8lx\n",
7738 relocation, (mask << shift),
7739 ((relocation & mask) << shift), x);
7741 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
7745 /* When performing a relocatable link, the input relocations are
7746 preserved. But, if they reference global symbols, the indices
7747 referenced must be updated. Update all the relocations in
7748 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7751 elf_link_adjust_relocs (bfd *abfd,
7752 Elf_Internal_Shdr *rel_hdr,
7754 struct elf_link_hash_entry **rel_hash)
7757 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7759 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7760 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7761 bfd_vma r_type_mask;
7764 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
7766 swap_in = bed->s->swap_reloc_in;
7767 swap_out = bed->s->swap_reloc_out;
7769 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
7771 swap_in = bed->s->swap_reloca_in;
7772 swap_out = bed->s->swap_reloca_out;
7777 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7780 if (bed->s->arch_size == 32)
7787 r_type_mask = 0xffffffff;
7791 erela = rel_hdr->contents;
7792 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
7794 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7797 if (*rel_hash == NULL)
7800 BFD_ASSERT ((*rel_hash)->indx >= 0);
7802 (*swap_in) (abfd, erela, irela);
7803 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7804 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7805 | (irela[j].r_info & r_type_mask));
7806 (*swap_out) (abfd, irela, erela);
7810 struct elf_link_sort_rela
7816 enum elf_reloc_type_class type;
7817 /* We use this as an array of size int_rels_per_ext_rel. */
7818 Elf_Internal_Rela rela[1];
7822 elf_link_sort_cmp1 (const void *A, const void *B)
7824 const struct elf_link_sort_rela *a = A;
7825 const struct elf_link_sort_rela *b = B;
7826 int relativea, relativeb;
7828 relativea = a->type == reloc_class_relative;
7829 relativeb = b->type == reloc_class_relative;
7831 if (relativea < relativeb)
7833 if (relativea > relativeb)
7835 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
7837 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
7839 if (a->rela->r_offset < b->rela->r_offset)
7841 if (a->rela->r_offset > b->rela->r_offset)
7847 elf_link_sort_cmp2 (const void *A, const void *B)
7849 const struct elf_link_sort_rela *a = A;
7850 const struct elf_link_sort_rela *b = B;
7853 if (a->u.offset < b->u.offset)
7855 if (a->u.offset > b->u.offset)
7857 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
7858 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
7863 if (a->rela->r_offset < b->rela->r_offset)
7865 if (a->rela->r_offset > b->rela->r_offset)
7871 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
7873 asection *dynamic_relocs;
7876 bfd_size_type count, size;
7877 size_t i, ret, sort_elt, ext_size;
7878 bfd_byte *sort, *s_non_relative, *p;
7879 struct elf_link_sort_rela *sq;
7880 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7881 int i2e = bed->s->int_rels_per_ext_rel;
7882 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7883 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7884 struct bfd_link_order *lo;
7886 bfd_boolean use_rela;
7888 /* Find a dynamic reloc section. */
7889 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
7890 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
7891 if (rela_dyn != NULL && rela_dyn->size > 0
7892 && rel_dyn != NULL && rel_dyn->size > 0)
7894 bfd_boolean use_rela_initialised = FALSE;
7896 /* This is just here to stop gcc from complaining.
7897 It's initialization checking code is not perfect. */
7900 /* Both sections are present. Examine the sizes
7901 of the indirect sections to help us choose. */
7902 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7903 if (lo->type == bfd_indirect_link_order)
7905 asection *o = lo->u.indirect.section;
7907 if ((o->size % bed->s->sizeof_rela) == 0)
7909 if ((o->size % bed->s->sizeof_rel) == 0)
7910 /* Section size is divisible by both rel and rela sizes.
7911 It is of no help to us. */
7915 /* Section size is only divisible by rela. */
7916 if (use_rela_initialised && (use_rela == FALSE))
7919 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7920 bfd_set_error (bfd_error_invalid_operation);
7926 use_rela_initialised = TRUE;
7930 else if ((o->size % bed->s->sizeof_rel) == 0)
7932 /* Section size is only divisible by rel. */
7933 if (use_rela_initialised && (use_rela == TRUE))
7936 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7937 bfd_set_error (bfd_error_invalid_operation);
7943 use_rela_initialised = TRUE;
7948 /* The section size is not divisible by either - something is wrong. */
7950 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
7951 bfd_set_error (bfd_error_invalid_operation);
7956 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7957 if (lo->type == bfd_indirect_link_order)
7959 asection *o = lo->u.indirect.section;
7961 if ((o->size % bed->s->sizeof_rela) == 0)
7963 if ((o->size % bed->s->sizeof_rel) == 0)
7964 /* Section size is divisible by both rel and rela sizes.
7965 It is of no help to us. */
7969 /* Section size is only divisible by rela. */
7970 if (use_rela_initialised && (use_rela == FALSE))
7973 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7974 bfd_set_error (bfd_error_invalid_operation);
7980 use_rela_initialised = TRUE;
7984 else if ((o->size % bed->s->sizeof_rel) == 0)
7986 /* Section size is only divisible by rel. */
7987 if (use_rela_initialised && (use_rela == TRUE))
7990 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7991 bfd_set_error (bfd_error_invalid_operation);
7997 use_rela_initialised = TRUE;
8002 /* The section size is not divisible by either - something is wrong. */
8004 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8005 bfd_set_error (bfd_error_invalid_operation);
8010 if (! use_rela_initialised)
8014 else if (rela_dyn != NULL && rela_dyn->size > 0)
8016 else if (rel_dyn != NULL && rel_dyn->size > 0)
8023 dynamic_relocs = rela_dyn;
8024 ext_size = bed->s->sizeof_rela;
8025 swap_in = bed->s->swap_reloca_in;
8026 swap_out = bed->s->swap_reloca_out;
8030 dynamic_relocs = rel_dyn;
8031 ext_size = bed->s->sizeof_rel;
8032 swap_in = bed->s->swap_reloc_in;
8033 swap_out = bed->s->swap_reloc_out;
8037 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8038 if (lo->type == bfd_indirect_link_order)
8039 size += lo->u.indirect.section->size;
8041 if (size != dynamic_relocs->size)
8044 sort_elt = (sizeof (struct elf_link_sort_rela)
8045 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8047 count = dynamic_relocs->size / ext_size;
8048 sort = bfd_zmalloc (sort_elt * count);
8052 (*info->callbacks->warning)
8053 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8057 if (bed->s->arch_size == 32)
8058 r_sym_mask = ~(bfd_vma) 0xff;
8060 r_sym_mask = ~(bfd_vma) 0xffffffff;
8062 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8063 if (lo->type == bfd_indirect_link_order)
8065 bfd_byte *erel, *erelend;
8066 asection *o = lo->u.indirect.section;
8068 if (o->contents == NULL && o->size != 0)
8070 /* This is a reloc section that is being handled as a normal
8071 section. See bfd_section_from_shdr. We can't combine
8072 relocs in this case. */
8077 erelend = o->contents + o->size;
8078 p = sort + o->output_offset / ext_size * sort_elt;
8080 while (erel < erelend)
8082 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8084 (*swap_in) (abfd, erel, s->rela);
8085 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8086 s->u.sym_mask = r_sym_mask;
8092 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8094 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8096 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8097 if (s->type != reloc_class_relative)
8103 sq = (struct elf_link_sort_rela *) s_non_relative;
8104 for (; i < count; i++, p += sort_elt)
8106 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8107 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8109 sp->u.offset = sq->rela->r_offset;
8112 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8114 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8115 if (lo->type == bfd_indirect_link_order)
8117 bfd_byte *erel, *erelend;
8118 asection *o = lo->u.indirect.section;
8121 erelend = o->contents + o->size;
8122 p = sort + o->output_offset / ext_size * sort_elt;
8123 while (erel < erelend)
8125 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8126 (*swap_out) (abfd, s->rela, erel);
8133 *psec = dynamic_relocs;
8137 /* Flush the output symbols to the file. */
8140 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
8141 const struct elf_backend_data *bed)
8143 if (finfo->symbuf_count > 0)
8145 Elf_Internal_Shdr *hdr;
8149 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
8150 pos = hdr->sh_offset + hdr->sh_size;
8151 amt = finfo->symbuf_count * bed->s->sizeof_sym;
8152 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
8153 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
8156 hdr->sh_size += amt;
8157 finfo->symbuf_count = 0;
8163 /* Add a symbol to the output symbol table. */
8166 elf_link_output_sym (struct elf_final_link_info *finfo,
8168 Elf_Internal_Sym *elfsym,
8169 asection *input_sec,
8170 struct elf_link_hash_entry *h)
8173 Elf_External_Sym_Shndx *destshndx;
8174 bfd_boolean (*output_symbol_hook)
8175 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8176 struct elf_link_hash_entry *);
8177 const struct elf_backend_data *bed;
8179 bed = get_elf_backend_data (finfo->output_bfd);
8180 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8181 if (output_symbol_hook != NULL)
8183 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
8187 if (name == NULL || *name == '\0')
8188 elfsym->st_name = 0;
8189 else if (input_sec->flags & SEC_EXCLUDE)
8190 elfsym->st_name = 0;
8193 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
8195 if (elfsym->st_name == (unsigned long) -1)
8199 if (finfo->symbuf_count >= finfo->symbuf_size)
8201 if (! elf_link_flush_output_syms (finfo, bed))
8205 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
8206 destshndx = finfo->symshndxbuf;
8207 if (destshndx != NULL)
8209 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
8213 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
8214 destshndx = bfd_realloc (destshndx, amt * 2);
8215 if (destshndx == NULL)
8217 finfo->symshndxbuf = destshndx;
8218 memset ((char *) destshndx + amt, 0, amt);
8219 finfo->shndxbuf_size *= 2;
8221 destshndx += bfd_get_symcount (finfo->output_bfd);
8224 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
8225 finfo->symbuf_count += 1;
8226 bfd_get_symcount (finfo->output_bfd) += 1;
8231 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8234 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8236 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8237 && sym->st_shndx < SHN_LORESERVE)
8239 /* The gABI doesn't support dynamic symbols in output sections
8241 (*_bfd_error_handler)
8242 (_("%B: Too many sections: %d (>= %d)"),
8243 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8244 bfd_set_error (bfd_error_nonrepresentable_section);
8250 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8251 allowing an unsatisfied unversioned symbol in the DSO to match a
8252 versioned symbol that would normally require an explicit version.
8253 We also handle the case that a DSO references a hidden symbol
8254 which may be satisfied by a versioned symbol in another DSO. */
8257 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8258 const struct elf_backend_data *bed,
8259 struct elf_link_hash_entry *h)
8262 struct elf_link_loaded_list *loaded;
8264 if (!is_elf_hash_table (info->hash))
8267 switch (h->root.type)
8273 case bfd_link_hash_undefined:
8274 case bfd_link_hash_undefweak:
8275 abfd = h->root.u.undef.abfd;
8276 if ((abfd->flags & DYNAMIC) == 0
8277 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8281 case bfd_link_hash_defined:
8282 case bfd_link_hash_defweak:
8283 abfd = h->root.u.def.section->owner;
8286 case bfd_link_hash_common:
8287 abfd = h->root.u.c.p->section->owner;
8290 BFD_ASSERT (abfd != NULL);
8292 for (loaded = elf_hash_table (info)->loaded;
8294 loaded = loaded->next)
8297 Elf_Internal_Shdr *hdr;
8298 bfd_size_type symcount;
8299 bfd_size_type extsymcount;
8300 bfd_size_type extsymoff;
8301 Elf_Internal_Shdr *versymhdr;
8302 Elf_Internal_Sym *isym;
8303 Elf_Internal_Sym *isymend;
8304 Elf_Internal_Sym *isymbuf;
8305 Elf_External_Versym *ever;
8306 Elf_External_Versym *extversym;
8308 input = loaded->abfd;
8310 /* We check each DSO for a possible hidden versioned definition. */
8312 || (input->flags & DYNAMIC) == 0
8313 || elf_dynversym (input) == 0)
8316 hdr = &elf_tdata (input)->dynsymtab_hdr;
8318 symcount = hdr->sh_size / bed->s->sizeof_sym;
8319 if (elf_bad_symtab (input))
8321 extsymcount = symcount;
8326 extsymcount = symcount - hdr->sh_info;
8327 extsymoff = hdr->sh_info;
8330 if (extsymcount == 0)
8333 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8335 if (isymbuf == NULL)
8338 /* Read in any version definitions. */
8339 versymhdr = &elf_tdata (input)->dynversym_hdr;
8340 extversym = bfd_malloc (versymhdr->sh_size);
8341 if (extversym == NULL)
8344 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8345 || (bfd_bread (extversym, versymhdr->sh_size, input)
8346 != versymhdr->sh_size))
8354 ever = extversym + extsymoff;
8355 isymend = isymbuf + extsymcount;
8356 for (isym = isymbuf; isym < isymend; isym++, ever++)
8359 Elf_Internal_Versym iver;
8360 unsigned short version_index;
8362 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8363 || isym->st_shndx == SHN_UNDEF)
8366 name = bfd_elf_string_from_elf_section (input,
8369 if (strcmp (name, h->root.root.string) != 0)
8372 _bfd_elf_swap_versym_in (input, ever, &iver);
8374 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
8376 /* If we have a non-hidden versioned sym, then it should
8377 have provided a definition for the undefined sym. */
8381 version_index = iver.vs_vers & VERSYM_VERSION;
8382 if (version_index == 1 || version_index == 2)
8384 /* This is the base or first version. We can use it. */
8398 /* Add an external symbol to the symbol table. This is called from
8399 the hash table traversal routine. When generating a shared object,
8400 we go through the symbol table twice. The first time we output
8401 anything that might have been forced to local scope in a version
8402 script. The second time we output the symbols that are still
8406 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
8408 struct elf_outext_info *eoinfo = data;
8409 struct elf_final_link_info *finfo = eoinfo->finfo;
8411 Elf_Internal_Sym sym;
8412 asection *input_sec;
8413 const struct elf_backend_data *bed;
8415 if (h->root.type == bfd_link_hash_warning)
8417 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8418 if (h->root.type == bfd_link_hash_new)
8422 /* Decide whether to output this symbol in this pass. */
8423 if (eoinfo->localsyms)
8425 if (!h->forced_local)
8430 if (h->forced_local)
8434 bed = get_elf_backend_data (finfo->output_bfd);
8436 if (h->root.type == bfd_link_hash_undefined)
8438 /* If we have an undefined symbol reference here then it must have
8439 come from a shared library that is being linked in. (Undefined
8440 references in regular files have already been handled). */
8441 bfd_boolean ignore_undef = FALSE;
8443 /* Some symbols may be special in that the fact that they're
8444 undefined can be safely ignored - let backend determine that. */
8445 if (bed->elf_backend_ignore_undef_symbol)
8446 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8448 /* If we are reporting errors for this situation then do so now. */
8449 if (ignore_undef == FALSE
8452 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
8453 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
8455 if (! (finfo->info->callbacks->undefined_symbol
8456 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
8457 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
8459 eoinfo->failed = TRUE;
8465 /* We should also warn if a forced local symbol is referenced from
8466 shared libraries. */
8467 if (! finfo->info->relocatable
8468 && (! finfo->info->shared)
8473 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
8475 (*_bfd_error_handler)
8476 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
8478 h->root.u.def.section == bfd_abs_section_ptr
8479 ? finfo->output_bfd : h->root.u.def.section->owner,
8480 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
8482 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
8483 ? "hidden" : "local",
8484 h->root.root.string);
8485 eoinfo->failed = TRUE;
8489 /* We don't want to output symbols that have never been mentioned by
8490 a regular file, or that we have been told to strip. However, if
8491 h->indx is set to -2, the symbol is used by a reloc and we must
8495 else if ((h->def_dynamic
8497 || h->root.type == bfd_link_hash_new)
8501 else if (finfo->info->strip == strip_all)
8503 else if (finfo->info->strip == strip_some
8504 && bfd_hash_lookup (finfo->info->keep_hash,
8505 h->root.root.string, FALSE, FALSE) == NULL)
8507 else if (finfo->info->strip_discarded
8508 && (h->root.type == bfd_link_hash_defined
8509 || h->root.type == bfd_link_hash_defweak)
8510 && elf_discarded_section (h->root.u.def.section))
8515 /* If we're stripping it, and it's not a dynamic symbol, there's
8516 nothing else to do unless it is a forced local symbol. */
8519 && !h->forced_local)
8523 sym.st_size = h->size;
8524 sym.st_other = h->other;
8525 if (h->forced_local)
8526 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8527 else if (h->root.type == bfd_link_hash_undefweak
8528 || h->root.type == bfd_link_hash_defweak)
8529 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8531 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
8533 switch (h->root.type)
8536 case bfd_link_hash_new:
8537 case bfd_link_hash_warning:
8541 case bfd_link_hash_undefined:
8542 case bfd_link_hash_undefweak:
8543 input_sec = bfd_und_section_ptr;
8544 sym.st_shndx = SHN_UNDEF;
8547 case bfd_link_hash_defined:
8548 case bfd_link_hash_defweak:
8550 input_sec = h->root.u.def.section;
8551 if (input_sec->output_section != NULL)
8554 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
8555 input_sec->output_section);
8556 if (sym.st_shndx == SHN_BAD)
8558 (*_bfd_error_handler)
8559 (_("%B: could not find output section %A for input section %A"),
8560 finfo->output_bfd, input_sec->output_section, input_sec);
8561 eoinfo->failed = TRUE;
8565 /* ELF symbols in relocatable files are section relative,
8566 but in nonrelocatable files they are virtual
8568 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8569 if (! finfo->info->relocatable)
8571 sym.st_value += input_sec->output_section->vma;
8572 if (h->type == STT_TLS)
8574 asection *tls_sec = elf_hash_table (finfo->info)->tls_sec;
8575 if (tls_sec != NULL)
8576 sym.st_value -= tls_sec->vma;
8579 /* The TLS section may have been garbage collected. */
8580 BFD_ASSERT (finfo->info->gc_sections
8581 && !input_sec->gc_mark);
8588 BFD_ASSERT (input_sec->owner == NULL
8589 || (input_sec->owner->flags & DYNAMIC) != 0);
8590 sym.st_shndx = SHN_UNDEF;
8591 input_sec = bfd_und_section_ptr;
8596 case bfd_link_hash_common:
8597 input_sec = h->root.u.c.p->section;
8598 sym.st_shndx = bed->common_section_index (input_sec);
8599 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8602 case bfd_link_hash_indirect:
8603 /* These symbols are created by symbol versioning. They point
8604 to the decorated version of the name. For example, if the
8605 symbol foo@@GNU_1.2 is the default, which should be used when
8606 foo is used with no version, then we add an indirect symbol
8607 foo which points to foo@@GNU_1.2. We ignore these symbols,
8608 since the indirected symbol is already in the hash table. */
8612 /* Give the processor backend a chance to tweak the symbol value,
8613 and also to finish up anything that needs to be done for this
8614 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8615 forced local syms when non-shared is due to a historical quirk. */
8616 if ((h->dynindx != -1
8618 && ((finfo->info->shared
8619 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8620 || h->root.type != bfd_link_hash_undefweak))
8621 || !h->forced_local)
8622 && elf_hash_table (finfo->info)->dynamic_sections_created)
8624 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8625 (finfo->output_bfd, finfo->info, h, &sym)))
8627 eoinfo->failed = TRUE;
8632 /* If we are marking the symbol as undefined, and there are no
8633 non-weak references to this symbol from a regular object, then
8634 mark the symbol as weak undefined; if there are non-weak
8635 references, mark the symbol as strong. We can't do this earlier,
8636 because it might not be marked as undefined until the
8637 finish_dynamic_symbol routine gets through with it. */
8638 if (sym.st_shndx == SHN_UNDEF
8640 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8641 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8645 if (h->ref_regular_nonweak)
8646 bindtype = STB_GLOBAL;
8648 bindtype = STB_WEAK;
8649 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
8652 /* If this is a symbol defined in a dynamic library, don't use the
8653 symbol size from the dynamic library. Relinking an executable
8654 against a new library may introduce gratuitous changes in the
8655 executable's symbols if we keep the size. */
8656 if (sym.st_shndx == SHN_UNDEF
8661 /* If a non-weak symbol with non-default visibility is not defined
8662 locally, it is a fatal error. */
8663 if (! finfo->info->relocatable
8664 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8665 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8666 && h->root.type == bfd_link_hash_undefined
8669 (*_bfd_error_handler)
8670 (_("%B: %s symbol `%s' isn't defined"),
8672 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
8674 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
8675 ? "internal" : "hidden",
8676 h->root.root.string);
8677 eoinfo->failed = TRUE;
8681 /* If this symbol should be put in the .dynsym section, then put it
8682 there now. We already know the symbol index. We also fill in
8683 the entry in the .hash section. */
8684 if (h->dynindx != -1
8685 && elf_hash_table (finfo->info)->dynamic_sections_created)
8689 sym.st_name = h->dynstr_index;
8690 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
8691 if (! check_dynsym (finfo->output_bfd, &sym))
8693 eoinfo->failed = TRUE;
8696 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8698 if (finfo->hash_sec != NULL)
8700 size_t hash_entry_size;
8701 bfd_byte *bucketpos;
8706 bucketcount = elf_hash_table (finfo->info)->bucketcount;
8707 bucket = h->u.elf_hash_value % bucketcount;
8710 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8711 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8712 + (bucket + 2) * hash_entry_size);
8713 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8714 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8715 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8716 ((bfd_byte *) finfo->hash_sec->contents
8717 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8720 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8722 Elf_Internal_Versym iversym;
8723 Elf_External_Versym *eversym;
8725 if (!h->def_regular)
8727 if (h->verinfo.verdef == NULL)
8728 iversym.vs_vers = 0;
8730 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8734 if (h->verinfo.vertree == NULL)
8735 iversym.vs_vers = 1;
8737 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8738 if (finfo->info->create_default_symver)
8743 iversym.vs_vers |= VERSYM_HIDDEN;
8745 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8746 eversym += h->dynindx;
8747 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8751 /* If we're stripping it, then it was just a dynamic symbol, and
8752 there's nothing else to do. */
8753 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8756 h->indx = bfd_get_symcount (finfo->output_bfd);
8758 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
8760 eoinfo->failed = TRUE;
8767 /* Return TRUE if special handling is done for relocs in SEC against
8768 symbols defined in discarded sections. */
8771 elf_section_ignore_discarded_relocs (asection *sec)
8773 const struct elf_backend_data *bed;
8775 switch (sec->sec_info_type)
8777 case ELF_INFO_TYPE_STABS:
8778 case ELF_INFO_TYPE_EH_FRAME:
8784 bed = get_elf_backend_data (sec->owner);
8785 if (bed->elf_backend_ignore_discarded_relocs != NULL
8786 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8792 /* Return a mask saying how ld should treat relocations in SEC against
8793 symbols defined in discarded sections. If this function returns
8794 COMPLAIN set, ld will issue a warning message. If this function
8795 returns PRETEND set, and the discarded section was link-once and the
8796 same size as the kept link-once section, ld will pretend that the
8797 symbol was actually defined in the kept section. Otherwise ld will
8798 zero the reloc (at least that is the intent, but some cooperation by
8799 the target dependent code is needed, particularly for REL targets). */
8802 _bfd_elf_default_action_discarded (asection *sec)
8804 if (sec->flags & SEC_DEBUGGING)
8807 if (strcmp (".eh_frame", sec->name) == 0)
8810 if (strcmp (".gcc_except_table", sec->name) == 0)
8813 return COMPLAIN | PRETEND;
8816 /* Find a match between a section and a member of a section group. */
8819 match_group_member (asection *sec, asection *group,
8820 struct bfd_link_info *info)
8822 asection *first = elf_next_in_group (group);
8823 asection *s = first;
8827 if (bfd_elf_match_symbols_in_sections (s, sec, info))
8830 s = elf_next_in_group (s);
8838 /* Check if the kept section of a discarded section SEC can be used
8839 to replace it. Return the replacement if it is OK. Otherwise return
8843 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
8847 kept = sec->kept_section;
8850 if ((kept->flags & SEC_GROUP) != 0)
8851 kept = match_group_member (sec, kept, info);
8853 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
8854 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
8856 sec->kept_section = kept;
8861 /* Link an input file into the linker output file. This function
8862 handles all the sections and relocations of the input file at once.
8863 This is so that we only have to read the local symbols once, and
8864 don't have to keep them in memory. */
8867 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
8869 int (*relocate_section)
8870 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
8871 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
8873 Elf_Internal_Shdr *symtab_hdr;
8876 Elf_Internal_Sym *isymbuf;
8877 Elf_Internal_Sym *isym;
8878 Elf_Internal_Sym *isymend;
8880 asection **ppsection;
8882 const struct elf_backend_data *bed;
8883 struct elf_link_hash_entry **sym_hashes;
8885 output_bfd = finfo->output_bfd;
8886 bed = get_elf_backend_data (output_bfd);
8887 relocate_section = bed->elf_backend_relocate_section;
8889 /* If this is a dynamic object, we don't want to do anything here:
8890 we don't want the local symbols, and we don't want the section
8892 if ((input_bfd->flags & DYNAMIC) != 0)
8895 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8896 if (elf_bad_symtab (input_bfd))
8898 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8903 locsymcount = symtab_hdr->sh_info;
8904 extsymoff = symtab_hdr->sh_info;
8907 /* Read the local symbols. */
8908 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8909 if (isymbuf == NULL && locsymcount != 0)
8911 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8912 finfo->internal_syms,
8913 finfo->external_syms,
8914 finfo->locsym_shndx);
8915 if (isymbuf == NULL)
8919 /* Find local symbol sections and adjust values of symbols in
8920 SEC_MERGE sections. Write out those local symbols we know are
8921 going into the output file. */
8922 isymend = isymbuf + locsymcount;
8923 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
8925 isym++, pindex++, ppsection++)
8929 Elf_Internal_Sym osym;
8933 if (elf_bad_symtab (input_bfd))
8935 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
8942 if (isym->st_shndx == SHN_UNDEF)
8943 isec = bfd_und_section_ptr;
8944 else if (isym->st_shndx == SHN_ABS)
8945 isec = bfd_abs_section_ptr;
8946 else if (isym->st_shndx == SHN_COMMON)
8947 isec = bfd_com_section_ptr;
8950 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
8953 /* Don't attempt to output symbols with st_shnx in the
8954 reserved range other than SHN_ABS and SHN_COMMON. */
8958 else if (isec->sec_info_type == ELF_INFO_TYPE_MERGE
8959 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
8961 _bfd_merged_section_offset (output_bfd, &isec,
8962 elf_section_data (isec)->sec_info,
8968 /* Don't output the first, undefined, symbol. */
8969 if (ppsection == finfo->sections)
8972 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
8974 /* We never output section symbols. Instead, we use the
8975 section symbol of the corresponding section in the output
8980 /* If we are stripping all symbols, we don't want to output this
8982 if (finfo->info->strip == strip_all)
8985 /* If we are discarding all local symbols, we don't want to
8986 output this one. If we are generating a relocatable output
8987 file, then some of the local symbols may be required by
8988 relocs; we output them below as we discover that they are
8990 if (finfo->info->discard == discard_all)
8993 /* If this symbol is defined in a section which we are
8994 discarding, we don't need to keep it. */
8995 if (isym->st_shndx != SHN_UNDEF
8996 && isym->st_shndx < SHN_LORESERVE
8997 && bfd_section_removed_from_list (output_bfd,
8998 isec->output_section))
9001 /* Get the name of the symbol. */
9002 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9007 /* See if we are discarding symbols with this name. */
9008 if ((finfo->info->strip == strip_some
9009 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
9011 || (((finfo->info->discard == discard_sec_merge
9012 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
9013 || finfo->info->discard == discard_l)
9014 && bfd_is_local_label_name (input_bfd, name)))
9017 /* If we get here, we are going to output this symbol. */
9021 /* Adjust the section index for the output file. */
9022 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9023 isec->output_section);
9024 if (osym.st_shndx == SHN_BAD)
9027 *pindex = bfd_get_symcount (output_bfd);
9029 /* ELF symbols in relocatable files are section relative, but
9030 in executable files they are virtual addresses. Note that
9031 this code assumes that all ELF sections have an associated
9032 BFD section with a reasonable value for output_offset; below
9033 we assume that they also have a reasonable value for
9034 output_section. Any special sections must be set up to meet
9035 these requirements. */
9036 osym.st_value += isec->output_offset;
9037 if (! finfo->info->relocatable)
9039 osym.st_value += isec->output_section->vma;
9040 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9042 /* STT_TLS symbols are relative to PT_TLS segment base. */
9043 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
9044 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
9048 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
9052 /* Relocate the contents of each section. */
9053 sym_hashes = elf_sym_hashes (input_bfd);
9054 for (o = input_bfd->sections; o != NULL; o = o->next)
9058 if (! o->linker_mark)
9060 /* This section was omitted from the link. */
9064 if ((o->flags & SEC_HAS_CONTENTS) == 0
9065 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9068 if ((o->flags & SEC_LINKER_CREATED) != 0)
9070 /* Section was created by _bfd_elf_link_create_dynamic_sections
9075 /* Get the contents of the section. They have been cached by a
9076 relaxation routine. Note that o is a section in an input
9077 file, so the contents field will not have been set by any of
9078 the routines which work on output files. */
9079 if (elf_section_data (o)->this_hdr.contents != NULL)
9080 contents = elf_section_data (o)->this_hdr.contents;
9083 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
9085 contents = finfo->contents;
9086 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
9090 if ((o->flags & SEC_RELOC) != 0)
9092 Elf_Internal_Rela *internal_relocs;
9093 Elf_Internal_Rela *rel, *relend;
9094 bfd_vma r_type_mask;
9096 int action_discarded;
9099 /* Get the swapped relocs. */
9101 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
9102 finfo->internal_relocs, FALSE);
9103 if (internal_relocs == NULL
9104 && o->reloc_count > 0)
9107 if (bed->s->arch_size == 32)
9114 r_type_mask = 0xffffffff;
9118 action_discarded = -1;
9119 if (!elf_section_ignore_discarded_relocs (o))
9120 action_discarded = (*bed->action_discarded) (o);
9122 /* Run through the relocs evaluating complex reloc symbols and
9123 looking for relocs against symbols from discarded sections
9124 or section symbols from removed link-once sections.
9125 Complain about relocs against discarded sections. Zero
9126 relocs against removed link-once sections. */
9128 rel = internal_relocs;
9129 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9130 for ( ; rel < relend; rel++)
9132 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9133 unsigned int s_type;
9134 asection **ps, *sec;
9135 struct elf_link_hash_entry *h = NULL;
9136 const char *sym_name;
9138 if (r_symndx == STN_UNDEF)
9141 if (r_symndx >= locsymcount
9142 || (elf_bad_symtab (input_bfd)
9143 && finfo->sections[r_symndx] == NULL))
9145 h = sym_hashes[r_symndx - extsymoff];
9147 /* Badly formatted input files can contain relocs that
9148 reference non-existant symbols. Check here so that
9149 we do not seg fault. */
9154 sprintf_vma (buffer, rel->r_info);
9155 (*_bfd_error_handler)
9156 (_("error: %B contains a reloc (0x%s) for section %A "
9157 "that references a non-existent global symbol"),
9158 input_bfd, o, buffer);
9159 bfd_set_error (bfd_error_bad_value);
9163 while (h->root.type == bfd_link_hash_indirect
9164 || h->root.type == bfd_link_hash_warning)
9165 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9170 if (h->root.type == bfd_link_hash_defined
9171 || h->root.type == bfd_link_hash_defweak)
9172 ps = &h->root.u.def.section;
9174 sym_name = h->root.root.string;
9178 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9180 s_type = ELF_ST_TYPE (sym->st_info);
9181 ps = &finfo->sections[r_symndx];
9182 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9186 if (s_type == STT_RELC || s_type == STT_SRELC)
9189 bfd_vma dot = (rel->r_offset
9190 + o->output_offset + o->output_section->vma);
9192 printf ("Encountered a complex symbol!");
9193 printf (" (input_bfd %s, section %s, reloc %ld\n",
9194 input_bfd->filename, o->name, rel - internal_relocs);
9195 printf (" symbol: idx %8.8lx, name %s\n",
9196 r_symndx, sym_name);
9197 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9198 (unsigned long) rel->r_info,
9199 (unsigned long) rel->r_offset);
9201 if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot,
9202 isymbuf, locsymcount, s_type == STT_SRELC))
9205 /* Symbol evaluated OK. Update to absolute value. */
9206 set_symbol_value (input_bfd, isymbuf, locsymcount,
9211 if (action_discarded != -1 && ps != NULL)
9213 /* Complain if the definition comes from a
9214 discarded section. */
9215 if ((sec = *ps) != NULL && elf_discarded_section (sec))
9217 BFD_ASSERT (r_symndx != 0);
9218 if (action_discarded & COMPLAIN)
9219 (*finfo->info->callbacks->einfo)
9220 (_("%X`%s' referenced in section `%A' of %B: "
9221 "defined in discarded section `%A' of %B\n"),
9222 sym_name, o, input_bfd, sec, sec->owner);
9224 /* Try to do the best we can to support buggy old
9225 versions of gcc. Pretend that the symbol is
9226 really defined in the kept linkonce section.
9227 FIXME: This is quite broken. Modifying the
9228 symbol here means we will be changing all later
9229 uses of the symbol, not just in this section. */
9230 if (action_discarded & PRETEND)
9234 kept = _bfd_elf_check_kept_section (sec,
9246 /* Relocate the section by invoking a back end routine.
9248 The back end routine is responsible for adjusting the
9249 section contents as necessary, and (if using Rela relocs
9250 and generating a relocatable output file) adjusting the
9251 reloc addend as necessary.
9253 The back end routine does not have to worry about setting
9254 the reloc address or the reloc symbol index.
9256 The back end routine is given a pointer to the swapped in
9257 internal symbols, and can access the hash table entries
9258 for the external symbols via elf_sym_hashes (input_bfd).
9260 When generating relocatable output, the back end routine
9261 must handle STB_LOCAL/STT_SECTION symbols specially. The
9262 output symbol is going to be a section symbol
9263 corresponding to the output section, which will require
9264 the addend to be adjusted. */
9266 ret = (*relocate_section) (output_bfd, finfo->info,
9267 input_bfd, o, contents,
9275 || finfo->info->relocatable
9276 || finfo->info->emitrelocations)
9278 Elf_Internal_Rela *irela;
9279 Elf_Internal_Rela *irelaend;
9280 bfd_vma last_offset;
9281 struct elf_link_hash_entry **rel_hash;
9282 struct elf_link_hash_entry **rel_hash_list;
9283 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
9284 unsigned int next_erel;
9285 bfd_boolean rela_normal;
9287 input_rel_hdr = &elf_section_data (o)->rel_hdr;
9288 rela_normal = (bed->rela_normal
9289 && (input_rel_hdr->sh_entsize
9290 == bed->s->sizeof_rela));
9292 /* Adjust the reloc addresses and symbol indices. */
9294 irela = internal_relocs;
9295 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
9296 rel_hash = (elf_section_data (o->output_section)->rel_hashes
9297 + elf_section_data (o->output_section)->rel_count
9298 + elf_section_data (o->output_section)->rel_count2);
9299 rel_hash_list = rel_hash;
9300 last_offset = o->output_offset;
9301 if (!finfo->info->relocatable)
9302 last_offset += o->output_section->vma;
9303 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9305 unsigned long r_symndx;
9307 Elf_Internal_Sym sym;
9309 if (next_erel == bed->s->int_rels_per_ext_rel)
9315 irela->r_offset = _bfd_elf_section_offset (output_bfd,
9318 if (irela->r_offset >= (bfd_vma) -2)
9320 /* This is a reloc for a deleted entry or somesuch.
9321 Turn it into an R_*_NONE reloc, at the same
9322 offset as the last reloc. elf_eh_frame.c and
9323 bfd_elf_discard_info rely on reloc offsets
9325 irela->r_offset = last_offset;
9327 irela->r_addend = 0;
9331 irela->r_offset += o->output_offset;
9333 /* Relocs in an executable have to be virtual addresses. */
9334 if (!finfo->info->relocatable)
9335 irela->r_offset += o->output_section->vma;
9337 last_offset = irela->r_offset;
9339 r_symndx = irela->r_info >> r_sym_shift;
9340 if (r_symndx == STN_UNDEF)
9343 if (r_symndx >= locsymcount
9344 || (elf_bad_symtab (input_bfd)
9345 && finfo->sections[r_symndx] == NULL))
9347 struct elf_link_hash_entry *rh;
9350 /* This is a reloc against a global symbol. We
9351 have not yet output all the local symbols, so
9352 we do not know the symbol index of any global
9353 symbol. We set the rel_hash entry for this
9354 reloc to point to the global hash table entry
9355 for this symbol. The symbol index is then
9356 set at the end of bfd_elf_final_link. */
9357 indx = r_symndx - extsymoff;
9358 rh = elf_sym_hashes (input_bfd)[indx];
9359 while (rh->root.type == bfd_link_hash_indirect
9360 || rh->root.type == bfd_link_hash_warning)
9361 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9363 /* Setting the index to -2 tells
9364 elf_link_output_extsym that this symbol is
9366 BFD_ASSERT (rh->indx < 0);
9374 /* This is a reloc against a local symbol. */
9377 sym = isymbuf[r_symndx];
9378 sec = finfo->sections[r_symndx];
9379 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9381 /* I suppose the backend ought to fill in the
9382 section of any STT_SECTION symbol against a
9383 processor specific section. */
9385 if (bfd_is_abs_section (sec))
9387 else if (sec == NULL || sec->owner == NULL)
9389 bfd_set_error (bfd_error_bad_value);
9394 asection *osec = sec->output_section;
9396 /* If we have discarded a section, the output
9397 section will be the absolute section. In
9398 case of discarded SEC_MERGE sections, use
9399 the kept section. relocate_section should
9400 have already handled discarded linkonce
9402 if (bfd_is_abs_section (osec)
9403 && sec->kept_section != NULL
9404 && sec->kept_section->output_section != NULL)
9406 osec = sec->kept_section->output_section;
9407 irela->r_addend -= osec->vma;
9410 if (!bfd_is_abs_section (osec))
9412 r_symndx = osec->target_index;
9415 struct elf_link_hash_table *htab;
9418 htab = elf_hash_table (finfo->info);
9419 oi = htab->text_index_section;
9420 if ((osec->flags & SEC_READONLY) == 0
9421 && htab->data_index_section != NULL)
9422 oi = htab->data_index_section;
9426 irela->r_addend += osec->vma - oi->vma;
9427 r_symndx = oi->target_index;
9431 BFD_ASSERT (r_symndx != 0);
9435 /* Adjust the addend according to where the
9436 section winds up in the output section. */
9438 irela->r_addend += sec->output_offset;
9442 if (finfo->indices[r_symndx] == -1)
9444 unsigned long shlink;
9448 if (finfo->info->strip == strip_all)
9450 /* You can't do ld -r -s. */
9451 bfd_set_error (bfd_error_invalid_operation);
9455 /* This symbol was skipped earlier, but
9456 since it is needed by a reloc, we
9457 must output it now. */
9458 shlink = symtab_hdr->sh_link;
9459 name = (bfd_elf_string_from_elf_section
9460 (input_bfd, shlink, sym.st_name));
9464 osec = sec->output_section;
9466 _bfd_elf_section_from_bfd_section (output_bfd,
9468 if (sym.st_shndx == SHN_BAD)
9471 sym.st_value += sec->output_offset;
9472 if (! finfo->info->relocatable)
9474 sym.st_value += osec->vma;
9475 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9477 /* STT_TLS symbols are relative to PT_TLS
9479 BFD_ASSERT (elf_hash_table (finfo->info)
9481 sym.st_value -= (elf_hash_table (finfo->info)
9486 finfo->indices[r_symndx]
9487 = bfd_get_symcount (output_bfd);
9489 if (! elf_link_output_sym (finfo, name, &sym, sec,
9494 r_symndx = finfo->indices[r_symndx];
9497 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9498 | (irela->r_info & r_type_mask));
9501 /* Swap out the relocs. */
9502 if (input_rel_hdr->sh_size != 0
9503 && !bed->elf_backend_emit_relocs (output_bfd, o,
9509 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
9510 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
9512 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9513 * bed->s->int_rels_per_ext_rel);
9514 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
9515 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9524 /* Write out the modified section contents. */
9525 if (bed->elf_backend_write_section
9526 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
9529 /* Section written out. */
9531 else switch (o->sec_info_type)
9533 case ELF_INFO_TYPE_STABS:
9534 if (! (_bfd_write_section_stabs
9536 &elf_hash_table (finfo->info)->stab_info,
9537 o, &elf_section_data (o)->sec_info, contents)))
9540 case ELF_INFO_TYPE_MERGE:
9541 if (! _bfd_write_merged_section (output_bfd, o,
9542 elf_section_data (o)->sec_info))
9545 case ELF_INFO_TYPE_EH_FRAME:
9547 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
9554 if (! (o->flags & SEC_EXCLUDE)
9555 && ! (o->output_section->flags & SEC_NEVER_LOAD)
9556 && ! bfd_set_section_contents (output_bfd, o->output_section,
9558 (file_ptr) o->output_offset,
9569 /* Generate a reloc when linking an ELF file. This is a reloc
9570 requested by the linker, and does not come from any input file. This
9571 is used to build constructor and destructor tables when linking
9575 elf_reloc_link_order (bfd *output_bfd,
9576 struct bfd_link_info *info,
9577 asection *output_section,
9578 struct bfd_link_order *link_order)
9580 reloc_howto_type *howto;
9584 struct elf_link_hash_entry **rel_hash_ptr;
9585 Elf_Internal_Shdr *rel_hdr;
9586 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9587 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9591 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9594 bfd_set_error (bfd_error_bad_value);
9598 addend = link_order->u.reloc.p->addend;
9600 /* Figure out the symbol index. */
9601 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
9602 + elf_section_data (output_section)->rel_count
9603 + elf_section_data (output_section)->rel_count2);
9604 if (link_order->type == bfd_section_reloc_link_order)
9606 indx = link_order->u.reloc.p->u.section->target_index;
9607 BFD_ASSERT (indx != 0);
9608 *rel_hash_ptr = NULL;
9612 struct elf_link_hash_entry *h;
9614 /* Treat a reloc against a defined symbol as though it were
9615 actually against the section. */
9616 h = ((struct elf_link_hash_entry *)
9617 bfd_wrapped_link_hash_lookup (output_bfd, info,
9618 link_order->u.reloc.p->u.name,
9619 FALSE, FALSE, TRUE));
9621 && (h->root.type == bfd_link_hash_defined
9622 || h->root.type == bfd_link_hash_defweak))
9626 section = h->root.u.def.section;
9627 indx = section->output_section->target_index;
9628 *rel_hash_ptr = NULL;
9629 /* It seems that we ought to add the symbol value to the
9630 addend here, but in practice it has already been added
9631 because it was passed to constructor_callback. */
9632 addend += section->output_section->vma + section->output_offset;
9636 /* Setting the index to -2 tells elf_link_output_extsym that
9637 this symbol is used by a reloc. */
9644 if (! ((*info->callbacks->unattached_reloc)
9645 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
9651 /* If this is an inplace reloc, we must write the addend into the
9653 if (howto->partial_inplace && addend != 0)
9656 bfd_reloc_status_type rstat;
9659 const char *sym_name;
9661 size = bfd_get_reloc_size (howto);
9662 buf = bfd_zmalloc (size);
9665 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
9672 case bfd_reloc_outofrange:
9675 case bfd_reloc_overflow:
9676 if (link_order->type == bfd_section_reloc_link_order)
9677 sym_name = bfd_section_name (output_bfd,
9678 link_order->u.reloc.p->u.section);
9680 sym_name = link_order->u.reloc.p->u.name;
9681 if (! ((*info->callbacks->reloc_overflow)
9682 (info, NULL, sym_name, howto->name, addend, NULL,
9683 NULL, (bfd_vma) 0)))
9690 ok = bfd_set_section_contents (output_bfd, output_section, buf,
9691 link_order->offset, size);
9697 /* The address of a reloc is relative to the section in a
9698 relocatable file, and is a virtual address in an executable
9700 offset = link_order->offset;
9701 if (! info->relocatable)
9702 offset += output_section->vma;
9704 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
9706 irel[i].r_offset = offset;
9708 irel[i].r_addend = 0;
9710 if (bed->s->arch_size == 32)
9711 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
9713 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
9715 rel_hdr = &elf_section_data (output_section)->rel_hdr;
9716 erel = rel_hdr->contents;
9717 if (rel_hdr->sh_type == SHT_REL)
9719 erel += (elf_section_data (output_section)->rel_count
9720 * bed->s->sizeof_rel);
9721 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
9725 irel[0].r_addend = addend;
9726 erel += (elf_section_data (output_section)->rel_count
9727 * bed->s->sizeof_rela);
9728 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
9731 ++elf_section_data (output_section)->rel_count;
9737 /* Get the output vma of the section pointed to by the sh_link field. */
9740 elf_get_linked_section_vma (struct bfd_link_order *p)
9742 Elf_Internal_Shdr **elf_shdrp;
9746 s = p->u.indirect.section;
9747 elf_shdrp = elf_elfsections (s->owner);
9748 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
9749 elfsec = elf_shdrp[elfsec]->sh_link;
9751 The Intel C compiler generates SHT_IA_64_UNWIND with
9752 SHF_LINK_ORDER. But it doesn't set the sh_link or
9753 sh_info fields. Hence we could get the situation
9754 where elfsec is 0. */
9757 const struct elf_backend_data *bed
9758 = get_elf_backend_data (s->owner);
9759 if (bed->link_order_error_handler)
9760 bed->link_order_error_handler
9761 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
9766 s = elf_shdrp[elfsec]->bfd_section;
9767 return s->output_section->vma + s->output_offset;
9772 /* Compare two sections based on the locations of the sections they are
9773 linked to. Used by elf_fixup_link_order. */
9776 compare_link_order (const void * a, const void * b)
9781 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
9782 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
9789 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9790 order as their linked sections. Returns false if this could not be done
9791 because an output section includes both ordered and unordered
9792 sections. Ideally we'd do this in the linker proper. */
9795 elf_fixup_link_order (bfd *abfd, asection *o)
9800 struct bfd_link_order *p;
9802 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9804 struct bfd_link_order **sections;
9805 asection *s, *other_sec, *linkorder_sec;
9809 linkorder_sec = NULL;
9812 for (p = o->map_head.link_order; p != NULL; p = p->next)
9814 if (p->type == bfd_indirect_link_order)
9816 s = p->u.indirect.section;
9818 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
9819 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
9820 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
9821 && elfsec < elf_numsections (sub)
9822 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
9823 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
9837 if (seen_other && seen_linkorder)
9839 if (other_sec && linkorder_sec)
9840 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9842 linkorder_sec->owner, other_sec,
9845 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
9847 bfd_set_error (bfd_error_bad_value);
9852 if (!seen_linkorder)
9855 sections = (struct bfd_link_order **)
9856 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
9857 if (sections == NULL)
9861 for (p = o->map_head.link_order; p != NULL; p = p->next)
9863 sections[seen_linkorder++] = p;
9865 /* Sort the input sections in the order of their linked section. */
9866 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
9867 compare_link_order);
9869 /* Change the offsets of the sections. */
9871 for (n = 0; n < seen_linkorder; n++)
9873 s = sections[n]->u.indirect.section;
9874 offset &= ~(bfd_vma) 0 << s->alignment_power;
9875 s->output_offset = offset;
9876 sections[n]->offset = offset;
9877 offset += sections[n]->size;
9885 /* Do the final step of an ELF link. */
9888 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
9890 bfd_boolean dynamic;
9891 bfd_boolean emit_relocs;
9893 struct elf_final_link_info finfo;
9894 register asection *o;
9895 register struct bfd_link_order *p;
9897 bfd_size_type max_contents_size;
9898 bfd_size_type max_external_reloc_size;
9899 bfd_size_type max_internal_reloc_count;
9900 bfd_size_type max_sym_count;
9901 bfd_size_type max_sym_shndx_count;
9903 Elf_Internal_Sym elfsym;
9905 Elf_Internal_Shdr *symtab_hdr;
9906 Elf_Internal_Shdr *symtab_shndx_hdr;
9907 Elf_Internal_Shdr *symstrtab_hdr;
9908 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9909 struct elf_outext_info eoinfo;
9911 size_t relativecount = 0;
9912 asection *reldyn = 0;
9914 asection *attr_section = NULL;
9915 bfd_vma attr_size = 0;
9916 const char *std_attrs_section;
9918 if (! is_elf_hash_table (info->hash))
9922 abfd->flags |= DYNAMIC;
9924 dynamic = elf_hash_table (info)->dynamic_sections_created;
9925 dynobj = elf_hash_table (info)->dynobj;
9927 emit_relocs = (info->relocatable
9928 || info->emitrelocations);
9931 finfo.output_bfd = abfd;
9932 finfo.symstrtab = _bfd_elf_stringtab_init ();
9933 if (finfo.symstrtab == NULL)
9938 finfo.dynsym_sec = NULL;
9939 finfo.hash_sec = NULL;
9940 finfo.symver_sec = NULL;
9944 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
9945 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
9946 BFD_ASSERT (finfo.dynsym_sec != NULL);
9947 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
9948 /* Note that it is OK if symver_sec is NULL. */
9951 finfo.contents = NULL;
9952 finfo.external_relocs = NULL;
9953 finfo.internal_relocs = NULL;
9954 finfo.external_syms = NULL;
9955 finfo.locsym_shndx = NULL;
9956 finfo.internal_syms = NULL;
9957 finfo.indices = NULL;
9958 finfo.sections = NULL;
9959 finfo.symbuf = NULL;
9960 finfo.symshndxbuf = NULL;
9961 finfo.symbuf_count = 0;
9962 finfo.shndxbuf_size = 0;
9964 /* The object attributes have been merged. Remove the input
9965 sections from the link, and set the contents of the output
9967 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
9968 for (o = abfd->sections; o != NULL; o = o->next)
9970 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
9971 || strcmp (o->name, ".gnu.attributes") == 0)
9973 for (p = o->map_head.link_order; p != NULL; p = p->next)
9975 asection *input_section;
9977 if (p->type != bfd_indirect_link_order)
9979 input_section = p->u.indirect.section;
9980 /* Hack: reset the SEC_HAS_CONTENTS flag so that
9981 elf_link_input_bfd ignores this section. */
9982 input_section->flags &= ~SEC_HAS_CONTENTS;
9985 attr_size = bfd_elf_obj_attr_size (abfd);
9988 bfd_set_section_size (abfd, o, attr_size);
9990 /* Skip this section later on. */
9991 o->map_head.link_order = NULL;
9994 o->flags |= SEC_EXCLUDE;
9998 /* Count up the number of relocations we will output for each output
9999 section, so that we know the sizes of the reloc sections. We
10000 also figure out some maximum sizes. */
10001 max_contents_size = 0;
10002 max_external_reloc_size = 0;
10003 max_internal_reloc_count = 0;
10005 max_sym_shndx_count = 0;
10007 for (o = abfd->sections; o != NULL; o = o->next)
10009 struct bfd_elf_section_data *esdo = elf_section_data (o);
10010 o->reloc_count = 0;
10012 for (p = o->map_head.link_order; p != NULL; p = p->next)
10014 unsigned int reloc_count = 0;
10015 struct bfd_elf_section_data *esdi = NULL;
10016 unsigned int *rel_count1;
10018 if (p->type == bfd_section_reloc_link_order
10019 || p->type == bfd_symbol_reloc_link_order)
10021 else if (p->type == bfd_indirect_link_order)
10025 sec = p->u.indirect.section;
10026 esdi = elf_section_data (sec);
10028 /* Mark all sections which are to be included in the
10029 link. This will normally be every section. We need
10030 to do this so that we can identify any sections which
10031 the linker has decided to not include. */
10032 sec->linker_mark = TRUE;
10034 if (sec->flags & SEC_MERGE)
10037 if (info->relocatable || info->emitrelocations)
10038 reloc_count = sec->reloc_count;
10039 else if (bed->elf_backend_count_relocs)
10041 Elf_Internal_Rela * relocs;
10043 relocs = _bfd_elf_link_read_relocs (sec->owner, sec,
10045 info->keep_memory);
10047 if (relocs != NULL)
10050 = (*bed->elf_backend_count_relocs) (sec, relocs);
10052 if (elf_section_data (sec)->relocs != relocs)
10057 if (sec->rawsize > max_contents_size)
10058 max_contents_size = sec->rawsize;
10059 if (sec->size > max_contents_size)
10060 max_contents_size = sec->size;
10062 /* We are interested in just local symbols, not all
10064 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10065 && (sec->owner->flags & DYNAMIC) == 0)
10069 if (elf_bad_symtab (sec->owner))
10070 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10071 / bed->s->sizeof_sym);
10073 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10075 if (sym_count > max_sym_count)
10076 max_sym_count = sym_count;
10078 if (sym_count > max_sym_shndx_count
10079 && elf_symtab_shndx (sec->owner) != 0)
10080 max_sym_shndx_count = sym_count;
10082 if ((sec->flags & SEC_RELOC) != 0)
10086 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
10087 if (ext_size > max_external_reloc_size)
10088 max_external_reloc_size = ext_size;
10089 if (sec->reloc_count > max_internal_reloc_count)
10090 max_internal_reloc_count = sec->reloc_count;
10095 if (reloc_count == 0)
10098 o->reloc_count += reloc_count;
10100 /* MIPS may have a mix of REL and RELA relocs on sections.
10101 To support this curious ABI we keep reloc counts in
10102 elf_section_data too. We must be careful to add the
10103 relocations from the input section to the right output
10104 count. FIXME: Get rid of one count. We have
10105 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
10106 rel_count1 = &esdo->rel_count;
10109 bfd_boolean same_size;
10110 bfd_size_type entsize1;
10112 entsize1 = esdi->rel_hdr.sh_entsize;
10113 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
10114 || entsize1 == bed->s->sizeof_rela);
10115 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
10118 rel_count1 = &esdo->rel_count2;
10120 if (esdi->rel_hdr2 != NULL)
10122 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
10123 unsigned int alt_count;
10124 unsigned int *rel_count2;
10126 BFD_ASSERT (entsize2 != entsize1
10127 && (entsize2 == bed->s->sizeof_rel
10128 || entsize2 == bed->s->sizeof_rela));
10130 rel_count2 = &esdo->rel_count2;
10132 rel_count2 = &esdo->rel_count;
10134 /* The following is probably too simplistic if the
10135 backend counts output relocs unusually. */
10136 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
10137 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
10138 *rel_count2 += alt_count;
10139 reloc_count -= alt_count;
10142 *rel_count1 += reloc_count;
10145 if (o->reloc_count > 0)
10146 o->flags |= SEC_RELOC;
10149 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10150 set it (this is probably a bug) and if it is set
10151 assign_section_numbers will create a reloc section. */
10152 o->flags &=~ SEC_RELOC;
10155 /* If the SEC_ALLOC flag is not set, force the section VMA to
10156 zero. This is done in elf_fake_sections as well, but forcing
10157 the VMA to 0 here will ensure that relocs against these
10158 sections are handled correctly. */
10159 if ((o->flags & SEC_ALLOC) == 0
10160 && ! o->user_set_vma)
10164 if (! info->relocatable && merged)
10165 elf_link_hash_traverse (elf_hash_table (info),
10166 _bfd_elf_link_sec_merge_syms, abfd);
10168 /* Figure out the file positions for everything but the symbol table
10169 and the relocs. We set symcount to force assign_section_numbers
10170 to create a symbol table. */
10171 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10172 BFD_ASSERT (! abfd->output_has_begun);
10173 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10176 /* Set sizes, and assign file positions for reloc sections. */
10177 for (o = abfd->sections; o != NULL; o = o->next)
10179 if ((o->flags & SEC_RELOC) != 0)
10181 if (!(_bfd_elf_link_size_reloc_section
10182 (abfd, &elf_section_data (o)->rel_hdr, o)))
10185 if (elf_section_data (o)->rel_hdr2
10186 && !(_bfd_elf_link_size_reloc_section
10187 (abfd, elf_section_data (o)->rel_hdr2, o)))
10191 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10192 to count upwards while actually outputting the relocations. */
10193 elf_section_data (o)->rel_count = 0;
10194 elf_section_data (o)->rel_count2 = 0;
10197 _bfd_elf_assign_file_positions_for_relocs (abfd);
10199 /* We have now assigned file positions for all the sections except
10200 .symtab and .strtab. We start the .symtab section at the current
10201 file position, and write directly to it. We build the .strtab
10202 section in memory. */
10203 bfd_get_symcount (abfd) = 0;
10204 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10205 /* sh_name is set in prep_headers. */
10206 symtab_hdr->sh_type = SHT_SYMTAB;
10207 /* sh_flags, sh_addr and sh_size all start off zero. */
10208 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10209 /* sh_link is set in assign_section_numbers. */
10210 /* sh_info is set below. */
10211 /* sh_offset is set just below. */
10212 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
10214 off = elf_tdata (abfd)->next_file_pos;
10215 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10217 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10218 incorrect. We do not yet know the size of the .symtab section.
10219 We correct next_file_pos below, after we do know the size. */
10221 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10222 continuously seeking to the right position in the file. */
10223 if (! info->keep_memory || max_sym_count < 20)
10224 finfo.symbuf_size = 20;
10226 finfo.symbuf_size = max_sym_count;
10227 amt = finfo.symbuf_size;
10228 amt *= bed->s->sizeof_sym;
10229 finfo.symbuf = bfd_malloc (amt);
10230 if (finfo.symbuf == NULL)
10232 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
10234 /* Wild guess at number of output symbols. realloc'd as needed. */
10235 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10236 finfo.shndxbuf_size = amt;
10237 amt *= sizeof (Elf_External_Sym_Shndx);
10238 finfo.symshndxbuf = bfd_zmalloc (amt);
10239 if (finfo.symshndxbuf == NULL)
10243 /* Start writing out the symbol table. The first symbol is always a
10245 if (info->strip != strip_all
10248 elfsym.st_value = 0;
10249 elfsym.st_size = 0;
10250 elfsym.st_info = 0;
10251 elfsym.st_other = 0;
10252 elfsym.st_shndx = SHN_UNDEF;
10253 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
10258 /* Output a symbol for each section. We output these even if we are
10259 discarding local symbols, since they are used for relocs. These
10260 symbols have no names. We store the index of each one in the
10261 index field of the section, so that we can find it again when
10262 outputting relocs. */
10263 if (info->strip != strip_all
10266 elfsym.st_size = 0;
10267 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10268 elfsym.st_other = 0;
10269 elfsym.st_value = 0;
10270 for (i = 1; i < elf_numsections (abfd); i++)
10272 o = bfd_section_from_elf_index (abfd, i);
10275 o->target_index = bfd_get_symcount (abfd);
10276 elfsym.st_shndx = i;
10277 if (!info->relocatable)
10278 elfsym.st_value = o->vma;
10279 if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
10285 /* Allocate some memory to hold information read in from the input
10287 if (max_contents_size != 0)
10289 finfo.contents = bfd_malloc (max_contents_size);
10290 if (finfo.contents == NULL)
10294 if (max_external_reloc_size != 0)
10296 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
10297 if (finfo.external_relocs == NULL)
10301 if (max_internal_reloc_count != 0)
10303 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10304 amt *= sizeof (Elf_Internal_Rela);
10305 finfo.internal_relocs = bfd_malloc (amt);
10306 if (finfo.internal_relocs == NULL)
10310 if (max_sym_count != 0)
10312 amt = max_sym_count * bed->s->sizeof_sym;
10313 finfo.external_syms = bfd_malloc (amt);
10314 if (finfo.external_syms == NULL)
10317 amt = max_sym_count * sizeof (Elf_Internal_Sym);
10318 finfo.internal_syms = bfd_malloc (amt);
10319 if (finfo.internal_syms == NULL)
10322 amt = max_sym_count * sizeof (long);
10323 finfo.indices = bfd_malloc (amt);
10324 if (finfo.indices == NULL)
10327 amt = max_sym_count * sizeof (asection *);
10328 finfo.sections = bfd_malloc (amt);
10329 if (finfo.sections == NULL)
10333 if (max_sym_shndx_count != 0)
10335 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
10336 finfo.locsym_shndx = bfd_malloc (amt);
10337 if (finfo.locsym_shndx == NULL)
10341 if (elf_hash_table (info)->tls_sec)
10343 bfd_vma base, end = 0;
10346 for (sec = elf_hash_table (info)->tls_sec;
10347 sec && (sec->flags & SEC_THREAD_LOCAL);
10350 bfd_size_type size = sec->size;
10353 && (sec->flags & SEC_HAS_CONTENTS) == 0)
10355 struct bfd_link_order *o = sec->map_tail.link_order;
10357 size = o->offset + o->size;
10359 end = sec->vma + size;
10361 base = elf_hash_table (info)->tls_sec->vma;
10362 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
10363 elf_hash_table (info)->tls_size = end - base;
10366 /* Reorder SHF_LINK_ORDER sections. */
10367 for (o = abfd->sections; o != NULL; o = o->next)
10369 if (!elf_fixup_link_order (abfd, o))
10373 /* Since ELF permits relocations to be against local symbols, we
10374 must have the local symbols available when we do the relocations.
10375 Since we would rather only read the local symbols once, and we
10376 would rather not keep them in memory, we handle all the
10377 relocations for a single input file at the same time.
10379 Unfortunately, there is no way to know the total number of local
10380 symbols until we have seen all of them, and the local symbol
10381 indices precede the global symbol indices. This means that when
10382 we are generating relocatable output, and we see a reloc against
10383 a global symbol, we can not know the symbol index until we have
10384 finished examining all the local symbols to see which ones we are
10385 going to output. To deal with this, we keep the relocations in
10386 memory, and don't output them until the end of the link. This is
10387 an unfortunate waste of memory, but I don't see a good way around
10388 it. Fortunately, it only happens when performing a relocatable
10389 link, which is not the common case. FIXME: If keep_memory is set
10390 we could write the relocs out and then read them again; I don't
10391 know how bad the memory loss will be. */
10393 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10394 sub->output_has_begun = FALSE;
10395 for (o = abfd->sections; o != NULL; o = o->next)
10397 for (p = o->map_head.link_order; p != NULL; p = p->next)
10399 if (p->type == bfd_indirect_link_order
10400 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10401 == bfd_target_elf_flavour)
10402 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10404 if (! sub->output_has_begun)
10406 if (! elf_link_input_bfd (&finfo, sub))
10408 sub->output_has_begun = TRUE;
10411 else if (p->type == bfd_section_reloc_link_order
10412 || p->type == bfd_symbol_reloc_link_order)
10414 if (! elf_reloc_link_order (abfd, info, o, p))
10419 if (! _bfd_default_link_order (abfd, info, o, p))
10425 /* Free symbol buffer if needed. */
10426 if (!info->reduce_memory_overheads)
10428 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10429 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10430 && elf_tdata (sub)->symbuf)
10432 free (elf_tdata (sub)->symbuf);
10433 elf_tdata (sub)->symbuf = NULL;
10437 /* Output any global symbols that got converted to local in a
10438 version script or due to symbol visibility. We do this in a
10439 separate step since ELF requires all local symbols to appear
10440 prior to any global symbols. FIXME: We should only do this if
10441 some global symbols were, in fact, converted to become local.
10442 FIXME: Will this work correctly with the Irix 5 linker? */
10443 eoinfo.failed = FALSE;
10444 eoinfo.finfo = &finfo;
10445 eoinfo.localsyms = TRUE;
10446 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10451 /* If backend needs to output some local symbols not present in the hash
10452 table, do it now. */
10453 if (bed->elf_backend_output_arch_local_syms)
10455 typedef bfd_boolean (*out_sym_func)
10456 (void *, const char *, Elf_Internal_Sym *, asection *,
10457 struct elf_link_hash_entry *);
10459 if (! ((*bed->elf_backend_output_arch_local_syms)
10460 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10464 /* That wrote out all the local symbols. Finish up the symbol table
10465 with the global symbols. Even if we want to strip everything we
10466 can, we still need to deal with those global symbols that got
10467 converted to local in a version script. */
10469 /* The sh_info field records the index of the first non local symbol. */
10470 symtab_hdr->sh_info = bfd_get_symcount (abfd);
10473 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10475 Elf_Internal_Sym sym;
10476 bfd_byte *dynsym = finfo.dynsym_sec->contents;
10477 long last_local = 0;
10479 /* Write out the section symbols for the output sections. */
10480 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
10486 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10489 for (s = abfd->sections; s != NULL; s = s->next)
10495 dynindx = elf_section_data (s)->dynindx;
10498 indx = elf_section_data (s)->this_idx;
10499 BFD_ASSERT (indx > 0);
10500 sym.st_shndx = indx;
10501 if (! check_dynsym (abfd, &sym))
10503 sym.st_value = s->vma;
10504 dest = dynsym + dynindx * bed->s->sizeof_sym;
10505 if (last_local < dynindx)
10506 last_local = dynindx;
10507 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10511 /* Write out the local dynsyms. */
10512 if (elf_hash_table (info)->dynlocal)
10514 struct elf_link_local_dynamic_entry *e;
10515 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
10520 sym.st_size = e->isym.st_size;
10521 sym.st_other = e->isym.st_other;
10523 /* Copy the internal symbol as is.
10524 Note that we saved a word of storage and overwrote
10525 the original st_name with the dynstr_index. */
10528 s = bfd_section_from_elf_index (e->input_bfd,
10533 elf_section_data (s->output_section)->this_idx;
10534 if (! check_dynsym (abfd, &sym))
10536 sym.st_value = (s->output_section->vma
10538 + e->isym.st_value);
10541 if (last_local < e->dynindx)
10542 last_local = e->dynindx;
10544 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
10545 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10549 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
10553 /* We get the global symbols from the hash table. */
10554 eoinfo.failed = FALSE;
10555 eoinfo.localsyms = FALSE;
10556 eoinfo.finfo = &finfo;
10557 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10562 /* If backend needs to output some symbols not present in the hash
10563 table, do it now. */
10564 if (bed->elf_backend_output_arch_syms)
10566 typedef bfd_boolean (*out_sym_func)
10567 (void *, const char *, Elf_Internal_Sym *, asection *,
10568 struct elf_link_hash_entry *);
10570 if (! ((*bed->elf_backend_output_arch_syms)
10571 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10575 /* Flush all symbols to the file. */
10576 if (! elf_link_flush_output_syms (&finfo, bed))
10579 /* Now we know the size of the symtab section. */
10580 off += symtab_hdr->sh_size;
10582 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
10583 if (symtab_shndx_hdr->sh_name != 0)
10585 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
10586 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
10587 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
10588 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
10589 symtab_shndx_hdr->sh_size = amt;
10591 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
10594 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
10595 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
10600 /* Finish up and write out the symbol string table (.strtab)
10602 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
10603 /* sh_name was set in prep_headers. */
10604 symstrtab_hdr->sh_type = SHT_STRTAB;
10605 symstrtab_hdr->sh_flags = 0;
10606 symstrtab_hdr->sh_addr = 0;
10607 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10608 symstrtab_hdr->sh_entsize = 0;
10609 symstrtab_hdr->sh_link = 0;
10610 symstrtab_hdr->sh_info = 0;
10611 /* sh_offset is set just below. */
10612 symstrtab_hdr->sh_addralign = 1;
10614 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10615 elf_tdata (abfd)->next_file_pos = off;
10617 if (bfd_get_symcount (abfd) > 0)
10619 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10620 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10624 /* Adjust the relocs to have the correct symbol indices. */
10625 for (o = abfd->sections; o != NULL; o = o->next)
10627 if ((o->flags & SEC_RELOC) == 0)
10630 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
10631 elf_section_data (o)->rel_count,
10632 elf_section_data (o)->rel_hashes);
10633 if (elf_section_data (o)->rel_hdr2 != NULL)
10634 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
10635 elf_section_data (o)->rel_count2,
10636 (elf_section_data (o)->rel_hashes
10637 + elf_section_data (o)->rel_count));
10639 /* Set the reloc_count field to 0 to prevent write_relocs from
10640 trying to swap the relocs out itself. */
10641 o->reloc_count = 0;
10644 if (dynamic && info->combreloc && dynobj != NULL)
10645 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
10647 /* If we are linking against a dynamic object, or generating a
10648 shared library, finish up the dynamic linking information. */
10651 bfd_byte *dyncon, *dynconend;
10653 /* Fix up .dynamic entries. */
10654 o = bfd_get_section_by_name (dynobj, ".dynamic");
10655 BFD_ASSERT (o != NULL);
10657 dyncon = o->contents;
10658 dynconend = o->contents + o->size;
10659 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10661 Elf_Internal_Dyn dyn;
10665 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10672 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
10674 switch (elf_section_data (reldyn)->this_hdr.sh_type)
10676 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
10677 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
10680 dyn.d_un.d_val = relativecount;
10687 name = info->init_function;
10690 name = info->fini_function;
10693 struct elf_link_hash_entry *h;
10695 h = elf_link_hash_lookup (elf_hash_table (info), name,
10696 FALSE, FALSE, TRUE);
10698 && (h->root.type == bfd_link_hash_defined
10699 || h->root.type == bfd_link_hash_defweak))
10701 dyn.d_un.d_val = h->root.u.def.value;
10702 o = h->root.u.def.section;
10703 if (o->output_section != NULL)
10704 dyn.d_un.d_val += (o->output_section->vma
10705 + o->output_offset);
10708 /* The symbol is imported from another shared
10709 library and does not apply to this one. */
10710 dyn.d_un.d_val = 0;
10717 case DT_PREINIT_ARRAYSZ:
10718 name = ".preinit_array";
10720 case DT_INIT_ARRAYSZ:
10721 name = ".init_array";
10723 case DT_FINI_ARRAYSZ:
10724 name = ".fini_array";
10726 o = bfd_get_section_by_name (abfd, name);
10729 (*_bfd_error_handler)
10730 (_("%B: could not find output section %s"), abfd, name);
10734 (*_bfd_error_handler)
10735 (_("warning: %s section has zero size"), name);
10736 dyn.d_un.d_val = o->size;
10739 case DT_PREINIT_ARRAY:
10740 name = ".preinit_array";
10742 case DT_INIT_ARRAY:
10743 name = ".init_array";
10745 case DT_FINI_ARRAY:
10746 name = ".fini_array";
10753 name = ".gnu.hash";
10762 name = ".gnu.version_d";
10765 name = ".gnu.version_r";
10768 name = ".gnu.version";
10770 o = bfd_get_section_by_name (abfd, name);
10773 (*_bfd_error_handler)
10774 (_("%B: could not find output section %s"), abfd, name);
10777 dyn.d_un.d_ptr = o->vma;
10784 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
10788 dyn.d_un.d_val = 0;
10789 for (i = 1; i < elf_numsections (abfd); i++)
10791 Elf_Internal_Shdr *hdr;
10793 hdr = elf_elfsections (abfd)[i];
10794 if (hdr->sh_type == type
10795 && (hdr->sh_flags & SHF_ALLOC) != 0)
10797 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
10798 dyn.d_un.d_val += hdr->sh_size;
10801 if (dyn.d_un.d_val == 0
10802 || hdr->sh_addr < dyn.d_un.d_val)
10803 dyn.d_un.d_val = hdr->sh_addr;
10809 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
10813 /* If we have created any dynamic sections, then output them. */
10814 if (dynobj != NULL)
10816 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
10819 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10820 if (info->warn_shared_textrel && info->shared)
10822 bfd_byte *dyncon, *dynconend;
10824 /* Fix up .dynamic entries. */
10825 o = bfd_get_section_by_name (dynobj, ".dynamic");
10826 BFD_ASSERT (o != NULL);
10828 dyncon = o->contents;
10829 dynconend = o->contents + o->size;
10830 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10832 Elf_Internal_Dyn dyn;
10834 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10836 if (dyn.d_tag == DT_TEXTREL)
10838 info->callbacks->einfo
10839 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
10845 for (o = dynobj->sections; o != NULL; o = o->next)
10847 if ((o->flags & SEC_HAS_CONTENTS) == 0
10849 || o->output_section == bfd_abs_section_ptr)
10851 if ((o->flags & SEC_LINKER_CREATED) == 0)
10853 /* At this point, we are only interested in sections
10854 created by _bfd_elf_link_create_dynamic_sections. */
10857 if (elf_hash_table (info)->stab_info.stabstr == o)
10859 if (elf_hash_table (info)->eh_info.hdr_sec == o)
10861 if ((elf_section_data (o->output_section)->this_hdr.sh_type
10863 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
10865 if (! bfd_set_section_contents (abfd, o->output_section,
10867 (file_ptr) o->output_offset,
10873 /* The contents of the .dynstr section are actually in a
10875 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
10876 if (bfd_seek (abfd, off, SEEK_SET) != 0
10877 || ! _bfd_elf_strtab_emit (abfd,
10878 elf_hash_table (info)->dynstr))
10884 if (info->relocatable)
10886 bfd_boolean failed = FALSE;
10888 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
10893 /* If we have optimized stabs strings, output them. */
10894 if (elf_hash_table (info)->stab_info.stabstr != NULL)
10896 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
10900 if (info->eh_frame_hdr)
10902 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
10906 if (finfo.symstrtab != NULL)
10907 _bfd_stringtab_free (finfo.symstrtab);
10908 if (finfo.contents != NULL)
10909 free (finfo.contents);
10910 if (finfo.external_relocs != NULL)
10911 free (finfo.external_relocs);
10912 if (finfo.internal_relocs != NULL)
10913 free (finfo.internal_relocs);
10914 if (finfo.external_syms != NULL)
10915 free (finfo.external_syms);
10916 if (finfo.locsym_shndx != NULL)
10917 free (finfo.locsym_shndx);
10918 if (finfo.internal_syms != NULL)
10919 free (finfo.internal_syms);
10920 if (finfo.indices != NULL)
10921 free (finfo.indices);
10922 if (finfo.sections != NULL)
10923 free (finfo.sections);
10924 if (finfo.symbuf != NULL)
10925 free (finfo.symbuf);
10926 if (finfo.symshndxbuf != NULL)
10927 free (finfo.symshndxbuf);
10928 for (o = abfd->sections; o != NULL; o = o->next)
10930 if ((o->flags & SEC_RELOC) != 0
10931 && elf_section_data (o)->rel_hashes != NULL)
10932 free (elf_section_data (o)->rel_hashes);
10935 elf_tdata (abfd)->linker = TRUE;
10939 bfd_byte *contents = bfd_malloc (attr_size);
10940 if (contents == NULL)
10941 return FALSE; /* Bail out and fail. */
10942 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
10943 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
10950 if (finfo.symstrtab != NULL)
10951 _bfd_stringtab_free (finfo.symstrtab);
10952 if (finfo.contents != NULL)
10953 free (finfo.contents);
10954 if (finfo.external_relocs != NULL)
10955 free (finfo.external_relocs);
10956 if (finfo.internal_relocs != NULL)
10957 free (finfo.internal_relocs);
10958 if (finfo.external_syms != NULL)
10959 free (finfo.external_syms);
10960 if (finfo.locsym_shndx != NULL)
10961 free (finfo.locsym_shndx);
10962 if (finfo.internal_syms != NULL)
10963 free (finfo.internal_syms);
10964 if (finfo.indices != NULL)
10965 free (finfo.indices);
10966 if (finfo.sections != NULL)
10967 free (finfo.sections);
10968 if (finfo.symbuf != NULL)
10969 free (finfo.symbuf);
10970 if (finfo.symshndxbuf != NULL)
10971 free (finfo.symshndxbuf);
10972 for (o = abfd->sections; o != NULL; o = o->next)
10974 if ((o->flags & SEC_RELOC) != 0
10975 && elf_section_data (o)->rel_hashes != NULL)
10976 free (elf_section_data (o)->rel_hashes);
10982 /* Initialize COOKIE for input bfd ABFD. */
10985 init_reloc_cookie (struct elf_reloc_cookie *cookie,
10986 struct bfd_link_info *info, bfd *abfd)
10988 Elf_Internal_Shdr *symtab_hdr;
10989 const struct elf_backend_data *bed;
10991 bed = get_elf_backend_data (abfd);
10992 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10994 cookie->abfd = abfd;
10995 cookie->sym_hashes = elf_sym_hashes (abfd);
10996 cookie->bad_symtab = elf_bad_symtab (abfd);
10997 if (cookie->bad_symtab)
10999 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11000 cookie->extsymoff = 0;
11004 cookie->locsymcount = symtab_hdr->sh_info;
11005 cookie->extsymoff = symtab_hdr->sh_info;
11008 if (bed->s->arch_size == 32)
11009 cookie->r_sym_shift = 8;
11011 cookie->r_sym_shift = 32;
11013 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11014 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11016 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11017 cookie->locsymcount, 0,
11019 if (cookie->locsyms == NULL)
11021 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11024 if (info->keep_memory)
11025 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11030 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11033 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11035 Elf_Internal_Shdr *symtab_hdr;
11037 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11038 if (cookie->locsyms != NULL
11039 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11040 free (cookie->locsyms);
11043 /* Initialize the relocation information in COOKIE for input section SEC
11044 of input bfd ABFD. */
11047 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11048 struct bfd_link_info *info, bfd *abfd,
11051 const struct elf_backend_data *bed;
11053 if (sec->reloc_count == 0)
11055 cookie->rels = NULL;
11056 cookie->relend = NULL;
11060 bed = get_elf_backend_data (abfd);
11062 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11063 info->keep_memory);
11064 if (cookie->rels == NULL)
11066 cookie->rel = cookie->rels;
11067 cookie->relend = (cookie->rels
11068 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11070 cookie->rel = cookie->rels;
11074 /* Free the memory allocated by init_reloc_cookie_rels,
11078 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11081 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11082 free (cookie->rels);
11085 /* Initialize the whole of COOKIE for input section SEC. */
11088 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11089 struct bfd_link_info *info,
11092 if (!init_reloc_cookie (cookie, info, sec->owner))
11094 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11099 fini_reloc_cookie (cookie, sec->owner);
11104 /* Free the memory allocated by init_reloc_cookie_for_section,
11108 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11111 fini_reloc_cookie_rels (cookie, sec);
11112 fini_reloc_cookie (cookie, sec->owner);
11115 /* Garbage collect unused sections. */
11117 /* Default gc_mark_hook. */
11120 _bfd_elf_gc_mark_hook (asection *sec,
11121 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11122 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11123 struct elf_link_hash_entry *h,
11124 Elf_Internal_Sym *sym)
11128 switch (h->root.type)
11130 case bfd_link_hash_defined:
11131 case bfd_link_hash_defweak:
11132 return h->root.u.def.section;
11134 case bfd_link_hash_common:
11135 return h->root.u.c.p->section;
11142 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11147 /* COOKIE->rel describes a relocation against section SEC, which is
11148 a section we've decided to keep. Return the section that contains
11149 the relocation symbol, or NULL if no section contains it. */
11152 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11153 elf_gc_mark_hook_fn gc_mark_hook,
11154 struct elf_reloc_cookie *cookie)
11156 unsigned long r_symndx;
11157 struct elf_link_hash_entry *h;
11159 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
11163 if (r_symndx >= cookie->locsymcount
11164 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11166 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11167 while (h->root.type == bfd_link_hash_indirect
11168 || h->root.type == bfd_link_hash_warning)
11169 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11170 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11173 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11174 &cookie->locsyms[r_symndx]);
11177 /* COOKIE->rel describes a relocation against section SEC, which is
11178 a section we've decided to keep. Mark the section that contains
11179 the relocation symbol. */
11182 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11184 elf_gc_mark_hook_fn gc_mark_hook,
11185 struct elf_reloc_cookie *cookie)
11189 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11190 if (rsec && !rsec->gc_mark)
11192 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
11194 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11200 /* The mark phase of garbage collection. For a given section, mark
11201 it and any sections in this section's group, and all the sections
11202 which define symbols to which it refers. */
11205 _bfd_elf_gc_mark (struct bfd_link_info *info,
11207 elf_gc_mark_hook_fn gc_mark_hook)
11210 asection *group_sec, *eh_frame;
11214 /* Mark all the sections in the group. */
11215 group_sec = elf_section_data (sec)->next_in_group;
11216 if (group_sec && !group_sec->gc_mark)
11217 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
11220 /* Look through the section relocs. */
11222 eh_frame = elf_eh_frame_section (sec->owner);
11223 if ((sec->flags & SEC_RELOC) != 0
11224 && sec->reloc_count > 0
11225 && sec != eh_frame)
11227 struct elf_reloc_cookie cookie;
11229 if (!init_reloc_cookie_for_section (&cookie, info, sec))
11233 for (; cookie.rel < cookie.relend; cookie.rel++)
11234 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
11239 fini_reloc_cookie_for_section (&cookie, sec);
11243 if (ret && eh_frame && elf_fde_list (sec))
11245 struct elf_reloc_cookie cookie;
11247 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11251 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11252 gc_mark_hook, &cookie))
11254 fini_reloc_cookie_for_section (&cookie, eh_frame);
11261 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11263 struct elf_gc_sweep_symbol_info
11265 struct bfd_link_info *info;
11266 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11271 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
11273 if (h->root.type == bfd_link_hash_warning)
11274 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11276 if ((h->root.type == bfd_link_hash_defined
11277 || h->root.type == bfd_link_hash_defweak)
11278 && !h->root.u.def.section->gc_mark
11279 && !(h->root.u.def.section->owner->flags & DYNAMIC))
11281 struct elf_gc_sweep_symbol_info *inf = data;
11282 (*inf->hide_symbol) (inf->info, h, TRUE);
11288 /* The sweep phase of garbage collection. Remove all garbage sections. */
11290 typedef bfd_boolean (*gc_sweep_hook_fn)
11291 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11294 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
11297 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11298 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11299 unsigned long section_sym_count;
11300 struct elf_gc_sweep_symbol_info sweep_info;
11302 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11306 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11309 for (o = sub->sections; o != NULL; o = o->next)
11311 /* Keep debug and special sections. */
11312 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
11313 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
11319 /* Skip sweeping sections already excluded. */
11320 if (o->flags & SEC_EXCLUDE)
11323 /* Since this is early in the link process, it is simple
11324 to remove a section from the output. */
11325 o->flags |= SEC_EXCLUDE;
11327 if (info->print_gc_sections && o->size != 0)
11328 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11330 /* But we also have to update some of the relocation
11331 info we collected before. */
11333 && (o->flags & SEC_RELOC) != 0
11334 && o->reloc_count > 0
11335 && !bfd_is_abs_section (o->output_section))
11337 Elf_Internal_Rela *internal_relocs;
11341 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11342 info->keep_memory);
11343 if (internal_relocs == NULL)
11346 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11348 if (elf_section_data (o)->relocs != internal_relocs)
11349 free (internal_relocs);
11357 /* Remove the symbols that were in the swept sections from the dynamic
11358 symbol table. GCFIXME: Anyone know how to get them out of the
11359 static symbol table as well? */
11360 sweep_info.info = info;
11361 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11362 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11365 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
11369 /* Propagate collected vtable information. This is called through
11370 elf_link_hash_traverse. */
11373 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11375 if (h->root.type == bfd_link_hash_warning)
11376 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11378 /* Those that are not vtables. */
11379 if (h->vtable == NULL || h->vtable->parent == NULL)
11382 /* Those vtables that do not have parents, we cannot merge. */
11383 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
11386 /* If we've already been done, exit. */
11387 if (h->vtable->used && h->vtable->used[-1])
11390 /* Make sure the parent's table is up to date. */
11391 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
11393 if (h->vtable->used == NULL)
11395 /* None of this table's entries were referenced. Re-use the
11397 h->vtable->used = h->vtable->parent->vtable->used;
11398 h->vtable->size = h->vtable->parent->vtable->size;
11403 bfd_boolean *cu, *pu;
11405 /* Or the parent's entries into ours. */
11406 cu = h->vtable->used;
11408 pu = h->vtable->parent->vtable->used;
11411 const struct elf_backend_data *bed;
11412 unsigned int log_file_align;
11414 bed = get_elf_backend_data (h->root.u.def.section->owner);
11415 log_file_align = bed->s->log_file_align;
11416 n = h->vtable->parent->vtable->size >> log_file_align;
11431 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
11434 bfd_vma hstart, hend;
11435 Elf_Internal_Rela *relstart, *relend, *rel;
11436 const struct elf_backend_data *bed;
11437 unsigned int log_file_align;
11439 if (h->root.type == bfd_link_hash_warning)
11440 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11442 /* Take care of both those symbols that do not describe vtables as
11443 well as those that are not loaded. */
11444 if (h->vtable == NULL || h->vtable->parent == NULL)
11447 BFD_ASSERT (h->root.type == bfd_link_hash_defined
11448 || h->root.type == bfd_link_hash_defweak);
11450 sec = h->root.u.def.section;
11451 hstart = h->root.u.def.value;
11452 hend = hstart + h->size;
11454 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
11456 return *(bfd_boolean *) okp = FALSE;
11457 bed = get_elf_backend_data (sec->owner);
11458 log_file_align = bed->s->log_file_align;
11460 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11462 for (rel = relstart; rel < relend; ++rel)
11463 if (rel->r_offset >= hstart && rel->r_offset < hend)
11465 /* If the entry is in use, do nothing. */
11466 if (h->vtable->used
11467 && (rel->r_offset - hstart) < h->vtable->size)
11469 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
11470 if (h->vtable->used[entry])
11473 /* Otherwise, kill it. */
11474 rel->r_offset = rel->r_info = rel->r_addend = 0;
11480 /* Mark sections containing dynamically referenced symbols. When
11481 building shared libraries, we must assume that any visible symbol is
11485 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
11487 struct bfd_link_info *info = (struct bfd_link_info *) inf;
11489 if (h->root.type == bfd_link_hash_warning)
11490 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11492 if ((h->root.type == bfd_link_hash_defined
11493 || h->root.type == bfd_link_hash_defweak)
11495 || (!info->executable
11497 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
11498 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
11499 h->root.u.def.section->flags |= SEC_KEEP;
11504 /* Keep all sections containing symbols undefined on the command-line,
11505 and the section containing the entry symbol. */
11508 _bfd_elf_gc_keep (struct bfd_link_info *info)
11510 struct bfd_sym_chain *sym;
11512 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
11514 struct elf_link_hash_entry *h;
11516 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
11517 FALSE, FALSE, FALSE);
11520 && (h->root.type == bfd_link_hash_defined
11521 || h->root.type == bfd_link_hash_defweak)
11522 && !bfd_is_abs_section (h->root.u.def.section))
11523 h->root.u.def.section->flags |= SEC_KEEP;
11527 /* Do mark and sweep of unused sections. */
11530 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
11532 bfd_boolean ok = TRUE;
11534 elf_gc_mark_hook_fn gc_mark_hook;
11535 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11537 if (!bed->can_gc_sections
11538 || !is_elf_hash_table (info->hash))
11540 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
11544 bed->gc_keep (info);
11546 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
11547 at the .eh_frame section if we can mark the FDEs individually. */
11548 _bfd_elf_begin_eh_frame_parsing (info);
11549 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11552 struct elf_reloc_cookie cookie;
11554 sec = bfd_get_section_by_name (sub, ".eh_frame");
11555 if (sec && init_reloc_cookie_for_section (&cookie, info, sec))
11557 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
11558 if (elf_section_data (sec)->sec_info)
11559 elf_eh_frame_section (sub) = sec;
11560 fini_reloc_cookie_for_section (&cookie, sec);
11563 _bfd_elf_end_eh_frame_parsing (info);
11565 /* Apply transitive closure to the vtable entry usage info. */
11566 elf_link_hash_traverse (elf_hash_table (info),
11567 elf_gc_propagate_vtable_entries_used,
11572 /* Kill the vtable relocations that were not used. */
11573 elf_link_hash_traverse (elf_hash_table (info),
11574 elf_gc_smash_unused_vtentry_relocs,
11579 /* Mark dynamically referenced symbols. */
11580 if (elf_hash_table (info)->dynamic_sections_created)
11581 elf_link_hash_traverse (elf_hash_table (info),
11582 bed->gc_mark_dynamic_ref,
11585 /* Grovel through relocs to find out who stays ... */
11586 gc_mark_hook = bed->gc_mark_hook;
11587 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11591 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11594 for (o = sub->sections; o != NULL; o = o->next)
11595 if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark)
11596 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
11600 /* Allow the backend to mark additional target specific sections. */
11601 if (bed->gc_mark_extra_sections)
11602 bed->gc_mark_extra_sections (info, gc_mark_hook);
11604 /* ... and mark SEC_EXCLUDE for those that go. */
11605 return elf_gc_sweep (abfd, info);
11608 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
11611 bfd_elf_gc_record_vtinherit (bfd *abfd,
11613 struct elf_link_hash_entry *h,
11616 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
11617 struct elf_link_hash_entry **search, *child;
11618 bfd_size_type extsymcount;
11619 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11621 /* The sh_info field of the symtab header tells us where the
11622 external symbols start. We don't care about the local symbols at
11624 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
11625 if (!elf_bad_symtab (abfd))
11626 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
11628 sym_hashes = elf_sym_hashes (abfd);
11629 sym_hashes_end = sym_hashes + extsymcount;
11631 /* Hunt down the child symbol, which is in this section at the same
11632 offset as the relocation. */
11633 for (search = sym_hashes; search != sym_hashes_end; ++search)
11635 if ((child = *search) != NULL
11636 && (child->root.type == bfd_link_hash_defined
11637 || child->root.type == bfd_link_hash_defweak)
11638 && child->root.u.def.section == sec
11639 && child->root.u.def.value == offset)
11643 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
11644 abfd, sec, (unsigned long) offset);
11645 bfd_set_error (bfd_error_invalid_operation);
11649 if (!child->vtable)
11651 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
11652 if (!child->vtable)
11657 /* This *should* only be the absolute section. It could potentially
11658 be that someone has defined a non-global vtable though, which
11659 would be bad. It isn't worth paging in the local symbols to be
11660 sure though; that case should simply be handled by the assembler. */
11662 child->vtable->parent = (struct elf_link_hash_entry *) -1;
11665 child->vtable->parent = h;
11670 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
11673 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
11674 asection *sec ATTRIBUTE_UNUSED,
11675 struct elf_link_hash_entry *h,
11678 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11679 unsigned int log_file_align = bed->s->log_file_align;
11683 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
11688 if (addend >= h->vtable->size)
11690 size_t size, bytes, file_align;
11691 bfd_boolean *ptr = h->vtable->used;
11693 /* While the symbol is undefined, we have to be prepared to handle
11695 file_align = 1 << log_file_align;
11696 if (h->root.type == bfd_link_hash_undefined)
11697 size = addend + file_align;
11701 if (addend >= size)
11703 /* Oops! We've got a reference past the defined end of
11704 the table. This is probably a bug -- shall we warn? */
11705 size = addend + file_align;
11708 size = (size + file_align - 1) & -file_align;
11710 /* Allocate one extra entry for use as a "done" flag for the
11711 consolidation pass. */
11712 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
11716 ptr = bfd_realloc (ptr - 1, bytes);
11722 oldbytes = (((h->vtable->size >> log_file_align) + 1)
11723 * sizeof (bfd_boolean));
11724 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
11728 ptr = bfd_zmalloc (bytes);
11733 /* And arrange for that done flag to be at index -1. */
11734 h->vtable->used = ptr + 1;
11735 h->vtable->size = size;
11738 h->vtable->used[addend >> log_file_align] = TRUE;
11743 struct alloc_got_off_arg {
11745 unsigned int got_elt_size;
11748 /* We need a special top-level link routine to convert got reference counts
11749 to real got offsets. */
11752 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
11754 struct alloc_got_off_arg *gofarg = arg;
11756 if (h->root.type == bfd_link_hash_warning)
11757 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11759 if (h->got.refcount > 0)
11761 h->got.offset = gofarg->gotoff;
11762 gofarg->gotoff += gofarg->got_elt_size;
11765 h->got.offset = (bfd_vma) -1;
11770 /* And an accompanying bit to work out final got entry offsets once
11771 we're done. Should be called from final_link. */
11774 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
11775 struct bfd_link_info *info)
11778 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11780 unsigned int got_elt_size = bed->s->arch_size / 8;
11781 struct alloc_got_off_arg gofarg;
11783 if (! is_elf_hash_table (info->hash))
11786 /* The GOT offset is relative to the .got section, but the GOT header is
11787 put into the .got.plt section, if the backend uses it. */
11788 if (bed->want_got_plt)
11791 gotoff = bed->got_header_size;
11793 /* Do the local .got entries first. */
11794 for (i = info->input_bfds; i; i = i->link_next)
11796 bfd_signed_vma *local_got;
11797 bfd_size_type j, locsymcount;
11798 Elf_Internal_Shdr *symtab_hdr;
11800 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
11803 local_got = elf_local_got_refcounts (i);
11807 symtab_hdr = &elf_tdata (i)->symtab_hdr;
11808 if (elf_bad_symtab (i))
11809 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11811 locsymcount = symtab_hdr->sh_info;
11813 for (j = 0; j < locsymcount; ++j)
11815 if (local_got[j] > 0)
11817 local_got[j] = gotoff;
11818 gotoff += got_elt_size;
11821 local_got[j] = (bfd_vma) -1;
11825 /* Then the global .got entries. .plt refcounts are handled by
11826 adjust_dynamic_symbol */
11827 gofarg.gotoff = gotoff;
11828 gofarg.got_elt_size = got_elt_size;
11829 elf_link_hash_traverse (elf_hash_table (info),
11830 elf_gc_allocate_got_offsets,
11835 /* Many folk need no more in the way of final link than this, once
11836 got entry reference counting is enabled. */
11839 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
11841 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
11844 /* Invoke the regular ELF backend linker to do all the work. */
11845 return bfd_elf_final_link (abfd, info);
11849 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
11851 struct elf_reloc_cookie *rcookie = cookie;
11853 if (rcookie->bad_symtab)
11854 rcookie->rel = rcookie->rels;
11856 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
11858 unsigned long r_symndx;
11860 if (! rcookie->bad_symtab)
11861 if (rcookie->rel->r_offset > offset)
11863 if (rcookie->rel->r_offset != offset)
11866 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
11867 if (r_symndx == SHN_UNDEF)
11870 if (r_symndx >= rcookie->locsymcount
11871 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11873 struct elf_link_hash_entry *h;
11875 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
11877 while (h->root.type == bfd_link_hash_indirect
11878 || h->root.type == bfd_link_hash_warning)
11879 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11881 if ((h->root.type == bfd_link_hash_defined
11882 || h->root.type == bfd_link_hash_defweak)
11883 && elf_discarded_section (h->root.u.def.section))
11890 /* It's not a relocation against a global symbol,
11891 but it could be a relocation against a local
11892 symbol for a discarded section. */
11894 Elf_Internal_Sym *isym;
11896 /* Need to: get the symbol; get the section. */
11897 isym = &rcookie->locsyms[r_symndx];
11898 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
11899 if (isec != NULL && elf_discarded_section (isec))
11907 /* Discard unneeded references to discarded sections.
11908 Returns TRUE if any section's size was changed. */
11909 /* This function assumes that the relocations are in sorted order,
11910 which is true for all known assemblers. */
11913 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
11915 struct elf_reloc_cookie cookie;
11916 asection *stab, *eh;
11917 const struct elf_backend_data *bed;
11919 bfd_boolean ret = FALSE;
11921 if (info->traditional_format
11922 || !is_elf_hash_table (info->hash))
11925 _bfd_elf_begin_eh_frame_parsing (info);
11926 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
11928 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
11931 bed = get_elf_backend_data (abfd);
11933 if ((abfd->flags & DYNAMIC) != 0)
11937 if (!info->relocatable)
11939 eh = bfd_get_section_by_name (abfd, ".eh_frame");
11942 || bfd_is_abs_section (eh->output_section)))
11946 stab = bfd_get_section_by_name (abfd, ".stab");
11948 && (stab->size == 0
11949 || bfd_is_abs_section (stab->output_section)
11950 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
11955 && bed->elf_backend_discard_info == NULL)
11958 if (!init_reloc_cookie (&cookie, info, abfd))
11962 && stab->reloc_count > 0
11963 && init_reloc_cookie_rels (&cookie, info, abfd, stab))
11965 if (_bfd_discard_section_stabs (abfd, stab,
11966 elf_section_data (stab)->sec_info,
11967 bfd_elf_reloc_symbol_deleted_p,
11970 fini_reloc_cookie_rels (&cookie, stab);
11974 && init_reloc_cookie_rels (&cookie, info, abfd, eh))
11976 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
11977 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
11978 bfd_elf_reloc_symbol_deleted_p,
11981 fini_reloc_cookie_rels (&cookie, eh);
11984 if (bed->elf_backend_discard_info != NULL
11985 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
11988 fini_reloc_cookie (&cookie, abfd);
11990 _bfd_elf_end_eh_frame_parsing (info);
11992 if (info->eh_frame_hdr
11993 && !info->relocatable
11994 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12001 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section *sec,
12002 struct bfd_link_info *info)
12005 const char *name, *p;
12006 struct bfd_section_already_linked *l;
12007 struct bfd_section_already_linked_hash_entry *already_linked_list;
12009 if (sec->output_section == bfd_abs_section_ptr)
12012 flags = sec->flags;
12014 /* Return if it isn't a linkonce section. A comdat group section
12015 also has SEC_LINK_ONCE set. */
12016 if ((flags & SEC_LINK_ONCE) == 0)
12019 /* Don't put group member sections on our list of already linked
12020 sections. They are handled as a group via their group section. */
12021 if (elf_sec_group (sec) != NULL)
12024 /* FIXME: When doing a relocatable link, we may have trouble
12025 copying relocations in other sections that refer to local symbols
12026 in the section being discarded. Those relocations will have to
12027 be converted somehow; as of this writing I'm not sure that any of
12028 the backends handle that correctly.
12030 It is tempting to instead not discard link once sections when
12031 doing a relocatable link (technically, they should be discarded
12032 whenever we are building constructors). However, that fails,
12033 because the linker winds up combining all the link once sections
12034 into a single large link once section, which defeats the purpose
12035 of having link once sections in the first place.
12037 Also, not merging link once sections in a relocatable link
12038 causes trouble for MIPS ELF, which relies on link once semantics
12039 to handle the .reginfo section correctly. */
12041 name = bfd_get_section_name (abfd, sec);
12043 if (CONST_STRNEQ (name, ".gnu.linkonce.")
12044 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12049 already_linked_list = bfd_section_already_linked_table_lookup (p);
12051 for (l = already_linked_list->entry; l != NULL; l = l->next)
12053 /* We may have 2 different types of sections on the list: group
12054 sections and linkonce sections. Match like sections. */
12055 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
12056 && strcmp (name, l->sec->name) == 0
12057 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
12059 /* The section has already been linked. See if we should
12060 issue a warning. */
12061 switch (flags & SEC_LINK_DUPLICATES)
12066 case SEC_LINK_DUPLICATES_DISCARD:
12069 case SEC_LINK_DUPLICATES_ONE_ONLY:
12070 (*_bfd_error_handler)
12071 (_("%B: ignoring duplicate section `%A'"),
12075 case SEC_LINK_DUPLICATES_SAME_SIZE:
12076 if (sec->size != l->sec->size)
12077 (*_bfd_error_handler)
12078 (_("%B: duplicate section `%A' has different size"),
12082 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
12083 if (sec->size != l->sec->size)
12084 (*_bfd_error_handler)
12085 (_("%B: duplicate section `%A' has different size"),
12087 else if (sec->size != 0)
12089 bfd_byte *sec_contents, *l_sec_contents;
12091 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
12092 (*_bfd_error_handler)
12093 (_("%B: warning: could not read contents of section `%A'"),
12095 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
12097 (*_bfd_error_handler)
12098 (_("%B: warning: could not read contents of section `%A'"),
12099 l->sec->owner, l->sec);
12100 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
12101 (*_bfd_error_handler)
12102 (_("%B: warning: duplicate section `%A' has different contents"),
12106 free (sec_contents);
12107 if (l_sec_contents)
12108 free (l_sec_contents);
12113 /* Set the output_section field so that lang_add_section
12114 does not create a lang_input_section structure for this
12115 section. Since there might be a symbol in the section
12116 being discarded, we must retain a pointer to the section
12117 which we are really going to use. */
12118 sec->output_section = bfd_abs_section_ptr;
12119 sec->kept_section = l->sec;
12121 if (flags & SEC_GROUP)
12123 asection *first = elf_next_in_group (sec);
12124 asection *s = first;
12128 s->output_section = bfd_abs_section_ptr;
12129 /* Record which group discards it. */
12130 s->kept_section = l->sec;
12131 s = elf_next_in_group (s);
12132 /* These lists are circular. */
12142 /* A single member comdat group section may be discarded by a
12143 linkonce section and vice versa. */
12145 if ((flags & SEC_GROUP) != 0)
12147 asection *first = elf_next_in_group (sec);
12149 if (first != NULL && elf_next_in_group (first) == first)
12150 /* Check this single member group against linkonce sections. */
12151 for (l = already_linked_list->entry; l != NULL; l = l->next)
12152 if ((l->sec->flags & SEC_GROUP) == 0
12153 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
12154 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12156 first->output_section = bfd_abs_section_ptr;
12157 first->kept_section = l->sec;
12158 sec->output_section = bfd_abs_section_ptr;
12163 /* Check this linkonce section against single member groups. */
12164 for (l = already_linked_list->entry; l != NULL; l = l->next)
12165 if (l->sec->flags & SEC_GROUP)
12167 asection *first = elf_next_in_group (l->sec);
12170 && elf_next_in_group (first) == first
12171 && bfd_elf_match_symbols_in_sections (first, sec, info))
12173 sec->output_section = bfd_abs_section_ptr;
12174 sec->kept_section = first;
12179 /* This is the first section with this name. Record it. */
12180 if (! bfd_section_already_linked_table_insert (already_linked_list, sec))
12181 info->callbacks->einfo (_("%F%P: already_linked_table: %E"));
12185 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
12187 return sym->st_shndx == SHN_COMMON;
12191 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12197 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12199 return bfd_com_section_ptr;