1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2016 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "bfd_stdint.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
31 #ifdef BFD_SUPPORTS_PLUGINS
35 /* This struct is used to pass information to routines called via
36 elf_link_hash_traverse which must return failure. */
38 struct elf_info_failed
40 struct bfd_link_info *info;
44 /* This structure is used to pass information to
45 _bfd_elf_link_find_version_dependencies. */
47 struct elf_find_verdep_info
49 /* General link information. */
50 struct bfd_link_info *info;
51 /* The number of dependencies. */
53 /* Whether we had a failure. */
57 static bfd_boolean _bfd_elf_fix_symbol_flags
58 (struct elf_link_hash_entry *, struct elf_info_failed *);
61 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
62 unsigned long r_symndx,
65 if (r_symndx >= cookie->locsymcount
66 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 struct elf_link_hash_entry *h;
70 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72 while (h->root.type == bfd_link_hash_indirect
73 || h->root.type == bfd_link_hash_warning)
74 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76 if ((h->root.type == bfd_link_hash_defined
77 || h->root.type == bfd_link_hash_defweak)
78 && discarded_section (h->root.u.def.section))
79 return h->root.u.def.section;
85 /* It's not a relocation against a global symbol,
86 but it could be a relocation against a local
87 symbol for a discarded section. */
89 Elf_Internal_Sym *isym;
91 /* Need to: get the symbol; get the section. */
92 isym = &cookie->locsyms[r_symndx];
93 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 && discard ? discarded_section (isec) : 1)
101 /* Define a symbol in a dynamic linkage section. */
103 struct elf_link_hash_entry *
104 _bfd_elf_define_linkage_sym (bfd *abfd,
105 struct bfd_link_info *info,
109 struct elf_link_hash_entry *h;
110 struct bfd_link_hash_entry *bh;
111 const struct elf_backend_data *bed;
113 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
116 /* Zap symbol defined in an as-needed lib that wasn't linked.
117 This is a symptom of a larger problem: Absolute symbols
118 defined in shared libraries can't be overridden, because we
119 lose the link to the bfd which is via the symbol section. */
120 h->root.type = bfd_link_hash_new;
124 bed = get_elf_backend_data (abfd);
125 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
126 sec, 0, NULL, FALSE, bed->collect,
129 h = (struct elf_link_hash_entry *) bh;
132 h->root.linker_def = 1;
133 h->type = STT_OBJECT;
134 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
135 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
137 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
142 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
146 struct elf_link_hash_entry *h;
147 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
148 struct elf_link_hash_table *htab = elf_hash_table (info);
150 /* This function may be called more than once. */
151 s = bfd_get_linker_section (abfd, ".got");
155 flags = bed->dynamic_sec_flags;
157 s = bfd_make_section_anyway_with_flags (abfd,
158 (bed->rela_plts_and_copies_p
159 ? ".rela.got" : ".rel.got"),
160 (bed->dynamic_sec_flags
163 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
169 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
173 if (bed->want_got_plt)
175 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
177 || !bfd_set_section_alignment (abfd, s,
178 bed->s->log_file_align))
183 /* The first bit of the global offset table is the header. */
184 s->size += bed->got_header_size;
186 if (bed->want_got_sym)
188 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
189 (or .got.plt) section. We don't do this in the linker script
190 because we don't want to define the symbol if we are not creating
191 a global offset table. */
192 h = _bfd_elf_define_linkage_sym (abfd, info, s,
193 "_GLOBAL_OFFSET_TABLE_");
194 elf_hash_table (info)->hgot = h;
202 /* Create a strtab to hold the dynamic symbol names. */
204 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
206 struct elf_link_hash_table *hash_table;
208 hash_table = elf_hash_table (info);
209 if (hash_table->dynobj == NULL)
211 /* We may not set dynobj, an input file holding linker created
212 dynamic sections to abfd, which may be a dynamic object with
213 its own dynamic sections. We need to find a normal input file
214 to hold linker created sections if possible. */
215 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
218 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
220 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
226 hash_table->dynobj = abfd;
229 if (hash_table->dynstr == NULL)
231 hash_table->dynstr = _bfd_elf_strtab_init ();
232 if (hash_table->dynstr == NULL)
238 /* Create some sections which will be filled in with dynamic linking
239 information. ABFD is an input file which requires dynamic sections
240 to be created. The dynamic sections take up virtual memory space
241 when the final executable is run, so we need to create them before
242 addresses are assigned to the output sections. We work out the
243 actual contents and size of these sections later. */
246 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
250 const struct elf_backend_data *bed;
251 struct elf_link_hash_entry *h;
253 if (! is_elf_hash_table (info->hash))
256 if (elf_hash_table (info)->dynamic_sections_created)
259 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
262 abfd = elf_hash_table (info)->dynobj;
263 bed = get_elf_backend_data (abfd);
265 flags = bed->dynamic_sec_flags;
267 /* A dynamically linked executable has a .interp section, but a
268 shared library does not. */
269 if (bfd_link_executable (info) && !info->nointerp)
271 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
272 flags | SEC_READONLY);
277 /* Create sections to hold version informations. These are removed
278 if they are not needed. */
279 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
280 flags | SEC_READONLY);
282 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
286 flags | SEC_READONLY);
288 || ! bfd_set_section_alignment (abfd, s, 1))
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
292 flags | SEC_READONLY);
294 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
297 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
298 flags | SEC_READONLY);
300 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 elf_hash_table (info)->dynsym = s;
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
305 flags | SEC_READONLY);
309 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
311 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
314 /* The special symbol _DYNAMIC is always set to the start of the
315 .dynamic section. We could set _DYNAMIC in a linker script, but we
316 only want to define it if we are, in fact, creating a .dynamic
317 section. We don't want to define it if there is no .dynamic
318 section, since on some ELF platforms the start up code examines it
319 to decide how to initialize the process. */
320 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
321 elf_hash_table (info)->hdynamic = h;
327 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
328 flags | SEC_READONLY);
330 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
332 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
335 if (info->emit_gnu_hash)
337 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
338 flags | SEC_READONLY);
340 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
342 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
343 4 32-bit words followed by variable count of 64-bit words, then
344 variable count of 32-bit words. */
345 if (bed->s->arch_size == 64)
346 elf_section_data (s)->this_hdr.sh_entsize = 0;
348 elf_section_data (s)->this_hdr.sh_entsize = 4;
351 /* Let the backend create the rest of the sections. This lets the
352 backend set the right flags. The backend will normally create
353 the .got and .plt sections. */
354 if (bed->elf_backend_create_dynamic_sections == NULL
355 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
358 elf_hash_table (info)->dynamic_sections_created = TRUE;
363 /* Create dynamic sections when linking against a dynamic object. */
366 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
368 flagword flags, pltflags;
369 struct elf_link_hash_entry *h;
371 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
372 struct elf_link_hash_table *htab = elf_hash_table (info);
374 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
375 .rel[a].bss sections. */
376 flags = bed->dynamic_sec_flags;
379 if (bed->plt_not_loaded)
380 /* We do not clear SEC_ALLOC here because we still want the OS to
381 allocate space for the section; it's just that there's nothing
382 to read in from the object file. */
383 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
385 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
386 if (bed->plt_readonly)
387 pltflags |= SEC_READONLY;
389 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
391 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
395 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
397 if (bed->want_plt_sym)
399 h = _bfd_elf_define_linkage_sym (abfd, info, s,
400 "_PROCEDURE_LINKAGE_TABLE_");
401 elf_hash_table (info)->hplt = h;
406 s = bfd_make_section_anyway_with_flags (abfd,
407 (bed->rela_plts_and_copies_p
408 ? ".rela.plt" : ".rel.plt"),
409 flags | SEC_READONLY);
411 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
415 if (! _bfd_elf_create_got_section (abfd, info))
418 if (bed->want_dynbss)
420 /* The .dynbss section is a place to put symbols which are defined
421 by dynamic objects, are referenced by regular objects, and are
422 not functions. We must allocate space for them in the process
423 image and use a R_*_COPY reloc to tell the dynamic linker to
424 initialize them at run time. The linker script puts the .dynbss
425 section into the .bss section of the final image. */
426 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
427 (SEC_ALLOC | SEC_LINKER_CREATED));
431 /* The .rel[a].bss section holds copy relocs. This section is not
432 normally needed. We need to create it here, though, so that the
433 linker will map it to an output section. We can't just create it
434 only if we need it, because we will not know whether we need it
435 until we have seen all the input files, and the first time the
436 main linker code calls BFD after examining all the input files
437 (size_dynamic_sections) the input sections have already been
438 mapped to the output sections. If the section turns out not to
439 be needed, we can discard it later. We will never need this
440 section when generating a shared object, since they do not use
442 if (! bfd_link_pic (info))
444 s = bfd_make_section_anyway_with_flags (abfd,
445 (bed->rela_plts_and_copies_p
446 ? ".rela.bss" : ".rel.bss"),
447 flags | SEC_READONLY);
449 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
457 /* Record a new dynamic symbol. We record the dynamic symbols as we
458 read the input files, since we need to have a list of all of them
459 before we can determine the final sizes of the output sections.
460 Note that we may actually call this function even though we are not
461 going to output any dynamic symbols; in some cases we know that a
462 symbol should be in the dynamic symbol table, but only if there is
466 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
467 struct elf_link_hash_entry *h)
469 if (h->dynindx == -1)
471 struct elf_strtab_hash *dynstr;
476 /* XXX: The ABI draft says the linker must turn hidden and
477 internal symbols into STB_LOCAL symbols when producing the
478 DSO. However, if ld.so honors st_other in the dynamic table,
479 this would not be necessary. */
480 switch (ELF_ST_VISIBILITY (h->other))
484 if (h->root.type != bfd_link_hash_undefined
485 && h->root.type != bfd_link_hash_undefweak)
488 if (!elf_hash_table (info)->is_relocatable_executable)
496 h->dynindx = elf_hash_table (info)->dynsymcount;
497 ++elf_hash_table (info)->dynsymcount;
499 dynstr = elf_hash_table (info)->dynstr;
502 /* Create a strtab to hold the dynamic symbol names. */
503 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
508 /* We don't put any version information in the dynamic string
510 name = h->root.root.string;
511 p = strchr (name, ELF_VER_CHR);
513 /* We know that the p points into writable memory. In fact,
514 there are only a few symbols that have read-only names, being
515 those like _GLOBAL_OFFSET_TABLE_ that are created specially
516 by the backends. Most symbols will have names pointing into
517 an ELF string table read from a file, or to objalloc memory. */
520 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
525 if (indx == (size_t) -1)
527 h->dynstr_index = indx;
533 /* Mark a symbol dynamic. */
536 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
537 struct elf_link_hash_entry *h,
538 Elf_Internal_Sym *sym)
540 struct bfd_elf_dynamic_list *d = info->dynamic_list;
542 /* It may be called more than once on the same H. */
543 if(h->dynamic || bfd_link_relocatable (info))
546 if ((info->dynamic_data
547 && (h->type == STT_OBJECT
548 || h->type == STT_COMMON
550 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
551 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
553 && h->root.type == bfd_link_hash_new
554 && (*d->match) (&d->head, NULL, h->root.root.string)))
558 /* Record an assignment to a symbol made by a linker script. We need
559 this in case some dynamic object refers to this symbol. */
562 bfd_elf_record_link_assignment (bfd *output_bfd,
563 struct bfd_link_info *info,
568 struct elf_link_hash_entry *h, *hv;
569 struct elf_link_hash_table *htab;
570 const struct elf_backend_data *bed;
572 if (!is_elf_hash_table (info->hash))
575 htab = elf_hash_table (info);
576 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
580 if (h->versioned == unknown)
582 /* Set versioned if symbol version is unknown. */
583 char *version = strrchr (name, ELF_VER_CHR);
586 if (version > name && version[-1] != ELF_VER_CHR)
587 h->versioned = versioned_hidden;
589 h->versioned = versioned;
593 switch (h->root.type)
595 case bfd_link_hash_defined:
596 case bfd_link_hash_defweak:
597 case bfd_link_hash_common:
599 case bfd_link_hash_undefweak:
600 case bfd_link_hash_undefined:
601 /* Since we're defining the symbol, don't let it seem to have not
602 been defined. record_dynamic_symbol and size_dynamic_sections
603 may depend on this. */
604 h->root.type = bfd_link_hash_new;
605 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
606 bfd_link_repair_undef_list (&htab->root);
608 case bfd_link_hash_new:
609 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
612 case bfd_link_hash_indirect:
613 /* We had a versioned symbol in a dynamic library. We make the
614 the versioned symbol point to this one. */
615 bed = get_elf_backend_data (output_bfd);
617 while (hv->root.type == bfd_link_hash_indirect
618 || hv->root.type == bfd_link_hash_warning)
619 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
620 /* We don't need to update h->root.u since linker will set them
622 h->root.type = bfd_link_hash_undefined;
623 hv->root.type = bfd_link_hash_indirect;
624 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
625 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
627 case bfd_link_hash_warning:
632 /* If this symbol is being provided by the linker script, and it is
633 currently defined by a dynamic object, but not by a regular
634 object, then mark it as undefined so that the generic linker will
635 force the correct value. */
639 h->root.type = bfd_link_hash_undefined;
641 /* If this symbol is not being provided by the linker script, and it is
642 currently defined by a dynamic object, but not by a regular object,
643 then clear out any version information because the symbol will not be
644 associated with the dynamic object any more. */
648 h->verinfo.verdef = NULL;
654 bed = get_elf_backend_data (output_bfd);
655 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
656 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
657 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
660 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
662 if (!bfd_link_relocatable (info)
664 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
665 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
670 || bfd_link_dll (info)
671 || elf_hash_table (info)->is_relocatable_executable)
674 if (! bfd_elf_link_record_dynamic_symbol (info, h))
677 /* If this is a weak defined symbol, and we know a corresponding
678 real symbol from the same dynamic object, make sure the real
679 symbol is also made into a dynamic symbol. */
680 if (h->u.weakdef != NULL
681 && h->u.weakdef->dynindx == -1)
683 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
691 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
692 success, and 2 on a failure caused by attempting to record a symbol
693 in a discarded section, eg. a discarded link-once section symbol. */
696 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
701 struct elf_link_local_dynamic_entry *entry;
702 struct elf_link_hash_table *eht;
703 struct elf_strtab_hash *dynstr;
706 Elf_External_Sym_Shndx eshndx;
707 char esym[sizeof (Elf64_External_Sym)];
709 if (! is_elf_hash_table (info->hash))
712 /* See if the entry exists already. */
713 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
714 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
717 amt = sizeof (*entry);
718 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
722 /* Go find the symbol, so that we can find it's name. */
723 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
724 1, input_indx, &entry->isym, esym, &eshndx))
726 bfd_release (input_bfd, entry);
730 if (entry->isym.st_shndx != SHN_UNDEF
731 && entry->isym.st_shndx < SHN_LORESERVE)
735 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
736 if (s == NULL || bfd_is_abs_section (s->output_section))
738 /* We can still bfd_release here as nothing has done another
739 bfd_alloc. We can't do this later in this function. */
740 bfd_release (input_bfd, entry);
745 name = (bfd_elf_string_from_elf_section
746 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
747 entry->isym.st_name));
749 dynstr = elf_hash_table (info)->dynstr;
752 /* Create a strtab to hold the dynamic symbol names. */
753 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
758 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
759 if (dynstr_index == (size_t) -1)
761 entry->isym.st_name = dynstr_index;
763 eht = elf_hash_table (info);
765 entry->next = eht->dynlocal;
766 eht->dynlocal = entry;
767 entry->input_bfd = input_bfd;
768 entry->input_indx = input_indx;
771 /* Whatever binding the symbol had before, it's now local. */
773 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
775 /* The dynindx will be set at the end of size_dynamic_sections. */
780 /* Return the dynindex of a local dynamic symbol. */
783 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
787 struct elf_link_local_dynamic_entry *e;
789 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
790 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
795 /* This function is used to renumber the dynamic symbols, if some of
796 them are removed because they are marked as local. This is called
797 via elf_link_hash_traverse. */
800 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
803 size_t *count = (size_t *) data;
808 if (h->dynindx != -1)
809 h->dynindx = ++(*count);
815 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
816 STB_LOCAL binding. */
819 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
822 size_t *count = (size_t *) data;
824 if (!h->forced_local)
827 if (h->dynindx != -1)
828 h->dynindx = ++(*count);
833 /* Return true if the dynamic symbol for a given section should be
834 omitted when creating a shared library. */
836 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
837 struct bfd_link_info *info,
840 struct elf_link_hash_table *htab;
843 switch (elf_section_data (p)->this_hdr.sh_type)
847 /* If sh_type is yet undecided, assume it could be
848 SHT_PROGBITS/SHT_NOBITS. */
850 htab = elf_hash_table (info);
851 if (p == htab->tls_sec)
854 if (htab->text_index_section != NULL)
855 return p != htab->text_index_section && p != htab->data_index_section;
857 return (htab->dynobj != NULL
858 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
859 && ip->output_section == p);
861 /* There shouldn't be section relative relocations
862 against any other section. */
868 /* Assign dynsym indices. In a shared library we generate a section
869 symbol for each output section, which come first. Next come symbols
870 which have been forced to local binding. Then all of the back-end
871 allocated local dynamic syms, followed by the rest of the global
875 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
876 struct bfd_link_info *info,
877 unsigned long *section_sym_count)
879 unsigned long dynsymcount = 0;
881 if (bfd_link_pic (info)
882 || elf_hash_table (info)->is_relocatable_executable)
884 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
886 for (p = output_bfd->sections; p ; p = p->next)
887 if ((p->flags & SEC_EXCLUDE) == 0
888 && (p->flags & SEC_ALLOC) != 0
889 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
890 elf_section_data (p)->dynindx = ++dynsymcount;
892 elf_section_data (p)->dynindx = 0;
894 *section_sym_count = dynsymcount;
896 elf_link_hash_traverse (elf_hash_table (info),
897 elf_link_renumber_local_hash_table_dynsyms,
900 if (elf_hash_table (info)->dynlocal)
902 struct elf_link_local_dynamic_entry *p;
903 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
904 p->dynindx = ++dynsymcount;
907 elf_link_hash_traverse (elf_hash_table (info),
908 elf_link_renumber_hash_table_dynsyms,
911 /* There is an unused NULL entry at the head of the table which we
912 must account for in our count even if the table is empty since it
913 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
917 elf_hash_table (info)->dynsymcount = dynsymcount;
921 /* Merge st_other field. */
924 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
925 const Elf_Internal_Sym *isym, asection *sec,
926 bfd_boolean definition, bfd_boolean dynamic)
928 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
930 /* If st_other has a processor-specific meaning, specific
931 code might be needed here. */
932 if (bed->elf_backend_merge_symbol_attribute)
933 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
938 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
939 unsigned hvis = ELF_ST_VISIBILITY (h->other);
941 /* Keep the most constraining visibility. Leave the remainder
942 of the st_other field to elf_backend_merge_symbol_attribute. */
943 if (symvis - 1 < hvis - 1)
944 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
947 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
948 && (sec->flags & SEC_READONLY) == 0)
949 h->protected_def = 1;
952 /* This function is called when we want to merge a new symbol with an
953 existing symbol. It handles the various cases which arise when we
954 find a definition in a dynamic object, or when there is already a
955 definition in a dynamic object. The new symbol is described by
956 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
957 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
958 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
959 of an old common symbol. We set OVERRIDE if the old symbol is
960 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
961 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
962 to change. By OK to change, we mean that we shouldn't warn if the
963 type or size does change. */
966 _bfd_elf_merge_symbol (bfd *abfd,
967 struct bfd_link_info *info,
969 Elf_Internal_Sym *sym,
972 struct elf_link_hash_entry **sym_hash,
974 bfd_boolean *pold_weak,
975 unsigned int *pold_alignment,
977 bfd_boolean *override,
978 bfd_boolean *type_change_ok,
979 bfd_boolean *size_change_ok,
980 bfd_boolean *matched)
982 asection *sec, *oldsec;
983 struct elf_link_hash_entry *h;
984 struct elf_link_hash_entry *hi;
985 struct elf_link_hash_entry *flip;
988 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
989 bfd_boolean newweak, oldweak, newfunc, oldfunc;
990 const struct elf_backend_data *bed;
997 bind = ELF_ST_BIND (sym->st_info);
999 if (! bfd_is_und_section (sec))
1000 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1002 h = ((struct elf_link_hash_entry *)
1003 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1008 bed = get_elf_backend_data (abfd);
1010 /* NEW_VERSION is the symbol version of the new symbol. */
1011 if (h->versioned != unversioned)
1013 /* Symbol version is unknown or versioned. */
1014 new_version = strrchr (name, ELF_VER_CHR);
1017 if (h->versioned == unknown)
1019 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1020 h->versioned = versioned_hidden;
1022 h->versioned = versioned;
1025 if (new_version[0] == '\0')
1029 h->versioned = unversioned;
1034 /* For merging, we only care about real symbols. But we need to make
1035 sure that indirect symbol dynamic flags are updated. */
1037 while (h->root.type == bfd_link_hash_indirect
1038 || h->root.type == bfd_link_hash_warning)
1039 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1043 if (hi == h || h->root.type == bfd_link_hash_new)
1047 /* OLD_HIDDEN is true if the existing symbol is only visible
1048 to the symbol with the same symbol version. NEW_HIDDEN is
1049 true if the new symbol is only visible to the symbol with
1050 the same symbol version. */
1051 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1052 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1053 if (!old_hidden && !new_hidden)
1054 /* The new symbol matches the existing symbol if both
1059 /* OLD_VERSION is the symbol version of the existing
1063 if (h->versioned >= versioned)
1064 old_version = strrchr (h->root.root.string,
1069 /* The new symbol matches the existing symbol if they
1070 have the same symbol version. */
1071 *matched = (old_version == new_version
1072 || (old_version != NULL
1073 && new_version != NULL
1074 && strcmp (old_version, new_version) == 0));
1079 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1084 switch (h->root.type)
1089 case bfd_link_hash_undefined:
1090 case bfd_link_hash_undefweak:
1091 oldbfd = h->root.u.undef.abfd;
1094 case bfd_link_hash_defined:
1095 case bfd_link_hash_defweak:
1096 oldbfd = h->root.u.def.section->owner;
1097 oldsec = h->root.u.def.section;
1100 case bfd_link_hash_common:
1101 oldbfd = h->root.u.c.p->section->owner;
1102 oldsec = h->root.u.c.p->section;
1104 *pold_alignment = h->root.u.c.p->alignment_power;
1107 if (poldbfd && *poldbfd == NULL)
1110 /* Differentiate strong and weak symbols. */
1111 newweak = bind == STB_WEAK;
1112 oldweak = (h->root.type == bfd_link_hash_defweak
1113 || h->root.type == bfd_link_hash_undefweak);
1115 *pold_weak = oldweak;
1117 /* This code is for coping with dynamic objects, and is only useful
1118 if we are doing an ELF link. */
1119 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1122 /* We have to check it for every instance since the first few may be
1123 references and not all compilers emit symbol type for undefined
1125 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1127 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1128 respectively, is from a dynamic object. */
1130 newdyn = (abfd->flags & DYNAMIC) != 0;
1132 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1133 syms and defined syms in dynamic libraries respectively.
1134 ref_dynamic on the other hand can be set for a symbol defined in
1135 a dynamic library, and def_dynamic may not be set; When the
1136 definition in a dynamic lib is overridden by a definition in the
1137 executable use of the symbol in the dynamic lib becomes a
1138 reference to the executable symbol. */
1141 if (bfd_is_und_section (sec))
1143 if (bind != STB_WEAK)
1145 h->ref_dynamic_nonweak = 1;
1146 hi->ref_dynamic_nonweak = 1;
1151 /* Update the existing symbol only if they match. */
1154 hi->dynamic_def = 1;
1158 /* If we just created the symbol, mark it as being an ELF symbol.
1159 Other than that, there is nothing to do--there is no merge issue
1160 with a newly defined symbol--so we just return. */
1162 if (h->root.type == bfd_link_hash_new)
1168 /* In cases involving weak versioned symbols, we may wind up trying
1169 to merge a symbol with itself. Catch that here, to avoid the
1170 confusion that results if we try to override a symbol with
1171 itself. The additional tests catch cases like
1172 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1173 dynamic object, which we do want to handle here. */
1175 && (newweak || oldweak)
1176 && ((abfd->flags & DYNAMIC) == 0
1177 || !h->def_regular))
1182 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1183 else if (oldsec != NULL)
1185 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1186 indices used by MIPS ELF. */
1187 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1190 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1191 respectively, appear to be a definition rather than reference. */
1193 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1195 olddef = (h->root.type != bfd_link_hash_undefined
1196 && h->root.type != bfd_link_hash_undefweak
1197 && h->root.type != bfd_link_hash_common);
1199 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1200 respectively, appear to be a function. */
1202 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1203 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1205 oldfunc = (h->type != STT_NOTYPE
1206 && bed->is_function_type (h->type));
1208 /* If creating a default indirect symbol ("foo" or "foo@") from a
1209 dynamic versioned definition ("foo@@") skip doing so if there is
1210 an existing regular definition with a different type. We don't
1211 want, for example, a "time" variable in the executable overriding
1212 a "time" function in a shared library. */
1213 if (pold_alignment == NULL
1217 && (olddef || h->root.type == bfd_link_hash_common)
1218 && ELF_ST_TYPE (sym->st_info) != h->type
1219 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1220 && h->type != STT_NOTYPE
1221 && !(newfunc && oldfunc))
1227 /* Check TLS symbols. We don't check undefined symbols introduced
1228 by "ld -u" which have no type (and oldbfd NULL), and we don't
1229 check symbols from plugins because they also have no type. */
1231 && (oldbfd->flags & BFD_PLUGIN) == 0
1232 && (abfd->flags & BFD_PLUGIN) == 0
1233 && ELF_ST_TYPE (sym->st_info) != h->type
1234 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1237 bfd_boolean ntdef, tdef;
1238 asection *ntsec, *tsec;
1240 if (h->type == STT_TLS)
1260 (*_bfd_error_handler)
1261 (_("%s: TLS definition in %B section %A "
1262 "mismatches non-TLS definition in %B section %A"),
1263 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1264 else if (!tdef && !ntdef)
1265 (*_bfd_error_handler)
1266 (_("%s: TLS reference in %B "
1267 "mismatches non-TLS reference in %B"),
1268 tbfd, ntbfd, h->root.root.string);
1270 (*_bfd_error_handler)
1271 (_("%s: TLS definition in %B section %A "
1272 "mismatches non-TLS reference in %B"),
1273 tbfd, tsec, ntbfd, h->root.root.string);
1275 (*_bfd_error_handler)
1276 (_("%s: TLS reference in %B "
1277 "mismatches non-TLS definition in %B section %A"),
1278 tbfd, ntbfd, ntsec, h->root.root.string);
1280 bfd_set_error (bfd_error_bad_value);
1284 /* If the old symbol has non-default visibility, we ignore the new
1285 definition from a dynamic object. */
1287 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1288 && !bfd_is_und_section (sec))
1291 /* Make sure this symbol is dynamic. */
1293 hi->ref_dynamic = 1;
1294 /* A protected symbol has external availability. Make sure it is
1295 recorded as dynamic.
1297 FIXME: Should we check type and size for protected symbol? */
1298 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1299 return bfd_elf_link_record_dynamic_symbol (info, h);
1304 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1307 /* If the new symbol with non-default visibility comes from a
1308 relocatable file and the old definition comes from a dynamic
1309 object, we remove the old definition. */
1310 if (hi->root.type == bfd_link_hash_indirect)
1312 /* Handle the case where the old dynamic definition is
1313 default versioned. We need to copy the symbol info from
1314 the symbol with default version to the normal one if it
1315 was referenced before. */
1318 hi->root.type = h->root.type;
1319 h->root.type = bfd_link_hash_indirect;
1320 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1322 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1323 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1325 /* If the new symbol is hidden or internal, completely undo
1326 any dynamic link state. */
1327 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1328 h->forced_local = 0;
1335 /* FIXME: Should we check type and size for protected symbol? */
1345 /* If the old symbol was undefined before, then it will still be
1346 on the undefs list. If the new symbol is undefined or
1347 common, we can't make it bfd_link_hash_new here, because new
1348 undefined or common symbols will be added to the undefs list
1349 by _bfd_generic_link_add_one_symbol. Symbols may not be
1350 added twice to the undefs list. Also, if the new symbol is
1351 undefweak then we don't want to lose the strong undef. */
1352 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1354 h->root.type = bfd_link_hash_undefined;
1355 h->root.u.undef.abfd = abfd;
1359 h->root.type = bfd_link_hash_new;
1360 h->root.u.undef.abfd = NULL;
1363 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1365 /* If the new symbol is hidden or internal, completely undo
1366 any dynamic link state. */
1367 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1368 h->forced_local = 0;
1374 /* FIXME: Should we check type and size for protected symbol? */
1380 /* If a new weak symbol definition comes from a regular file and the
1381 old symbol comes from a dynamic library, we treat the new one as
1382 strong. Similarly, an old weak symbol definition from a regular
1383 file is treated as strong when the new symbol comes from a dynamic
1384 library. Further, an old weak symbol from a dynamic library is
1385 treated as strong if the new symbol is from a dynamic library.
1386 This reflects the way glibc's ld.so works.
1388 Do this before setting *type_change_ok or *size_change_ok so that
1389 we warn properly when dynamic library symbols are overridden. */
1391 if (newdef && !newdyn && olddyn)
1393 if (olddef && newdyn)
1396 /* Allow changes between different types of function symbol. */
1397 if (newfunc && oldfunc)
1398 *type_change_ok = TRUE;
1400 /* It's OK to change the type if either the existing symbol or the
1401 new symbol is weak. A type change is also OK if the old symbol
1402 is undefined and the new symbol is defined. */
1407 && h->root.type == bfd_link_hash_undefined))
1408 *type_change_ok = TRUE;
1410 /* It's OK to change the size if either the existing symbol or the
1411 new symbol is weak, or if the old symbol is undefined. */
1414 || h->root.type == bfd_link_hash_undefined)
1415 *size_change_ok = TRUE;
1417 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1418 symbol, respectively, appears to be a common symbol in a dynamic
1419 object. If a symbol appears in an uninitialized section, and is
1420 not weak, and is not a function, then it may be a common symbol
1421 which was resolved when the dynamic object was created. We want
1422 to treat such symbols specially, because they raise special
1423 considerations when setting the symbol size: if the symbol
1424 appears as a common symbol in a regular object, and the size in
1425 the regular object is larger, we must make sure that we use the
1426 larger size. This problematic case can always be avoided in C,
1427 but it must be handled correctly when using Fortran shared
1430 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1431 likewise for OLDDYNCOMMON and OLDDEF.
1433 Note that this test is just a heuristic, and that it is quite
1434 possible to have an uninitialized symbol in a shared object which
1435 is really a definition, rather than a common symbol. This could
1436 lead to some minor confusion when the symbol really is a common
1437 symbol in some regular object. However, I think it will be
1443 && (sec->flags & SEC_ALLOC) != 0
1444 && (sec->flags & SEC_LOAD) == 0
1447 newdyncommon = TRUE;
1449 newdyncommon = FALSE;
1453 && h->root.type == bfd_link_hash_defined
1455 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1456 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1459 olddyncommon = TRUE;
1461 olddyncommon = FALSE;
1463 /* We now know everything about the old and new symbols. We ask the
1464 backend to check if we can merge them. */
1465 if (bed->merge_symbol != NULL)
1467 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1472 /* If both the old and the new symbols look like common symbols in a
1473 dynamic object, set the size of the symbol to the larger of the
1478 && sym->st_size != h->size)
1480 /* Since we think we have two common symbols, issue a multiple
1481 common warning if desired. Note that we only warn if the
1482 size is different. If the size is the same, we simply let
1483 the old symbol override the new one as normally happens with
1484 symbols defined in dynamic objects. */
1486 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1487 bfd_link_hash_common, sym->st_size);
1488 if (sym->st_size > h->size)
1489 h->size = sym->st_size;
1491 *size_change_ok = TRUE;
1494 /* If we are looking at a dynamic object, and we have found a
1495 definition, we need to see if the symbol was already defined by
1496 some other object. If so, we want to use the existing
1497 definition, and we do not want to report a multiple symbol
1498 definition error; we do this by clobbering *PSEC to be
1499 bfd_und_section_ptr.
1501 We treat a common symbol as a definition if the symbol in the
1502 shared library is a function, since common symbols always
1503 represent variables; this can cause confusion in principle, but
1504 any such confusion would seem to indicate an erroneous program or
1505 shared library. We also permit a common symbol in a regular
1506 object to override a weak symbol in a shared object. A common
1507 symbol in executable also overrides a symbol in a shared object. */
1512 || (h->root.type == bfd_link_hash_common
1515 || (!olddyn && bfd_link_executable (info))))))
1519 newdyncommon = FALSE;
1521 *psec = sec = bfd_und_section_ptr;
1522 *size_change_ok = TRUE;
1524 /* If we get here when the old symbol is a common symbol, then
1525 we are explicitly letting it override a weak symbol or
1526 function in a dynamic object, and we don't want to warn about
1527 a type change. If the old symbol is a defined symbol, a type
1528 change warning may still be appropriate. */
1530 if (h->root.type == bfd_link_hash_common)
1531 *type_change_ok = TRUE;
1534 /* Handle the special case of an old common symbol merging with a
1535 new symbol which looks like a common symbol in a shared object.
1536 We change *PSEC and *PVALUE to make the new symbol look like a
1537 common symbol, and let _bfd_generic_link_add_one_symbol do the
1541 && h->root.type == bfd_link_hash_common)
1545 newdyncommon = FALSE;
1546 *pvalue = sym->st_size;
1547 *psec = sec = bed->common_section (oldsec);
1548 *size_change_ok = TRUE;
1551 /* Skip weak definitions of symbols that are already defined. */
1552 if (newdef && olddef && newweak)
1554 /* Don't skip new non-IR weak syms. */
1555 if (!(oldbfd != NULL
1556 && (oldbfd->flags & BFD_PLUGIN) != 0
1557 && (abfd->flags & BFD_PLUGIN) == 0))
1563 /* Merge st_other. If the symbol already has a dynamic index,
1564 but visibility says it should not be visible, turn it into a
1566 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1567 if (h->dynindx != -1)
1568 switch (ELF_ST_VISIBILITY (h->other))
1572 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1577 /* If the old symbol is from a dynamic object, and the new symbol is
1578 a definition which is not from a dynamic object, then the new
1579 symbol overrides the old symbol. Symbols from regular files
1580 always take precedence over symbols from dynamic objects, even if
1581 they are defined after the dynamic object in the link.
1583 As above, we again permit a common symbol in a regular object to
1584 override a definition in a shared object if the shared object
1585 symbol is a function or is weak. */
1590 || (bfd_is_com_section (sec)
1591 && (oldweak || oldfunc)))
1596 /* Change the hash table entry to undefined, and let
1597 _bfd_generic_link_add_one_symbol do the right thing with the
1600 h->root.type = bfd_link_hash_undefined;
1601 h->root.u.undef.abfd = h->root.u.def.section->owner;
1602 *size_change_ok = TRUE;
1605 olddyncommon = FALSE;
1607 /* We again permit a type change when a common symbol may be
1608 overriding a function. */
1610 if (bfd_is_com_section (sec))
1614 /* If a common symbol overrides a function, make sure
1615 that it isn't defined dynamically nor has type
1618 h->type = STT_NOTYPE;
1620 *type_change_ok = TRUE;
1623 if (hi->root.type == bfd_link_hash_indirect)
1626 /* This union may have been set to be non-NULL when this symbol
1627 was seen in a dynamic object. We must force the union to be
1628 NULL, so that it is correct for a regular symbol. */
1629 h->verinfo.vertree = NULL;
1632 /* Handle the special case of a new common symbol merging with an
1633 old symbol that looks like it might be a common symbol defined in
1634 a shared object. Note that we have already handled the case in
1635 which a new common symbol should simply override the definition
1636 in the shared library. */
1639 && bfd_is_com_section (sec)
1642 /* It would be best if we could set the hash table entry to a
1643 common symbol, but we don't know what to use for the section
1644 or the alignment. */
1645 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1646 bfd_link_hash_common, sym->st_size);
1648 /* If the presumed common symbol in the dynamic object is
1649 larger, pretend that the new symbol has its size. */
1651 if (h->size > *pvalue)
1654 /* We need to remember the alignment required by the symbol
1655 in the dynamic object. */
1656 BFD_ASSERT (pold_alignment);
1657 *pold_alignment = h->root.u.def.section->alignment_power;
1660 olddyncommon = FALSE;
1662 h->root.type = bfd_link_hash_undefined;
1663 h->root.u.undef.abfd = h->root.u.def.section->owner;
1665 *size_change_ok = TRUE;
1666 *type_change_ok = TRUE;
1668 if (hi->root.type == bfd_link_hash_indirect)
1671 h->verinfo.vertree = NULL;
1676 /* Handle the case where we had a versioned symbol in a dynamic
1677 library and now find a definition in a normal object. In this
1678 case, we make the versioned symbol point to the normal one. */
1679 flip->root.type = h->root.type;
1680 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1681 h->root.type = bfd_link_hash_indirect;
1682 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1683 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1687 flip->ref_dynamic = 1;
1694 /* This function is called to create an indirect symbol from the
1695 default for the symbol with the default version if needed. The
1696 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1697 set DYNSYM if the new indirect symbol is dynamic. */
1700 _bfd_elf_add_default_symbol (bfd *abfd,
1701 struct bfd_link_info *info,
1702 struct elf_link_hash_entry *h,
1704 Elf_Internal_Sym *sym,
1708 bfd_boolean *dynsym)
1710 bfd_boolean type_change_ok;
1711 bfd_boolean size_change_ok;
1714 struct elf_link_hash_entry *hi;
1715 struct bfd_link_hash_entry *bh;
1716 const struct elf_backend_data *bed;
1717 bfd_boolean collect;
1718 bfd_boolean dynamic;
1719 bfd_boolean override;
1721 size_t len, shortlen;
1723 bfd_boolean matched;
1725 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1728 /* If this symbol has a version, and it is the default version, we
1729 create an indirect symbol from the default name to the fully
1730 decorated name. This will cause external references which do not
1731 specify a version to be bound to this version of the symbol. */
1732 p = strchr (name, ELF_VER_CHR);
1733 if (h->versioned == unknown)
1737 h->versioned = unversioned;
1742 if (p[1] != ELF_VER_CHR)
1744 h->versioned = versioned_hidden;
1748 h->versioned = versioned;
1753 /* PR ld/19073: We may see an unversioned definition after the
1759 bed = get_elf_backend_data (abfd);
1760 collect = bed->collect;
1761 dynamic = (abfd->flags & DYNAMIC) != 0;
1763 shortlen = p - name;
1764 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1765 if (shortname == NULL)
1767 memcpy (shortname, name, shortlen);
1768 shortname[shortlen] = '\0';
1770 /* We are going to create a new symbol. Merge it with any existing
1771 symbol with this name. For the purposes of the merge, act as
1772 though we were defining the symbol we just defined, although we
1773 actually going to define an indirect symbol. */
1774 type_change_ok = FALSE;
1775 size_change_ok = FALSE;
1778 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1779 &hi, poldbfd, NULL, NULL, &skip, &override,
1780 &type_change_ok, &size_change_ok, &matched))
1786 if (hi->def_regular)
1788 /* If the undecorated symbol will have a version added by a
1789 script different to H, then don't indirect to/from the
1790 undecorated symbol. This isn't ideal because we may not yet
1791 have seen symbol versions, if given by a script on the
1792 command line rather than via --version-script. */
1793 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1798 = bfd_find_version_for_sym (info->version_info,
1799 hi->root.root.string, &hide);
1800 if (hi->verinfo.vertree != NULL && hide)
1802 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1806 if (hi->verinfo.vertree != NULL
1807 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1813 /* Add the default symbol if not performing a relocatable link. */
1814 if (! bfd_link_relocatable (info))
1817 if (! (_bfd_generic_link_add_one_symbol
1818 (info, abfd, shortname, BSF_INDIRECT,
1819 bfd_ind_section_ptr,
1820 0, name, FALSE, collect, &bh)))
1822 hi = (struct elf_link_hash_entry *) bh;
1827 /* In this case the symbol named SHORTNAME is overriding the
1828 indirect symbol we want to add. We were planning on making
1829 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1830 is the name without a version. NAME is the fully versioned
1831 name, and it is the default version.
1833 Overriding means that we already saw a definition for the
1834 symbol SHORTNAME in a regular object, and it is overriding
1835 the symbol defined in the dynamic object.
1837 When this happens, we actually want to change NAME, the
1838 symbol we just added, to refer to SHORTNAME. This will cause
1839 references to NAME in the shared object to become references
1840 to SHORTNAME in the regular object. This is what we expect
1841 when we override a function in a shared object: that the
1842 references in the shared object will be mapped to the
1843 definition in the regular object. */
1845 while (hi->root.type == bfd_link_hash_indirect
1846 || hi->root.type == bfd_link_hash_warning)
1847 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1849 h->root.type = bfd_link_hash_indirect;
1850 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1854 hi->ref_dynamic = 1;
1858 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1863 /* Now set HI to H, so that the following code will set the
1864 other fields correctly. */
1868 /* Check if HI is a warning symbol. */
1869 if (hi->root.type == bfd_link_hash_warning)
1870 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1872 /* If there is a duplicate definition somewhere, then HI may not
1873 point to an indirect symbol. We will have reported an error to
1874 the user in that case. */
1876 if (hi->root.type == bfd_link_hash_indirect)
1878 struct elf_link_hash_entry *ht;
1880 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1881 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1883 /* A reference to the SHORTNAME symbol from a dynamic library
1884 will be satisfied by the versioned symbol at runtime. In
1885 effect, we have a reference to the versioned symbol. */
1886 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1887 hi->dynamic_def |= ht->dynamic_def;
1889 /* See if the new flags lead us to realize that the symbol must
1895 if (! bfd_link_executable (info)
1902 if (hi->ref_regular)
1908 /* We also need to define an indirection from the nondefault version
1912 len = strlen (name);
1913 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1914 if (shortname == NULL)
1916 memcpy (shortname, name, shortlen);
1917 memcpy (shortname + shortlen, p + 1, len - shortlen);
1919 /* Once again, merge with any existing symbol. */
1920 type_change_ok = FALSE;
1921 size_change_ok = FALSE;
1923 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1924 &hi, poldbfd, NULL, NULL, &skip, &override,
1925 &type_change_ok, &size_change_ok, &matched))
1933 /* Here SHORTNAME is a versioned name, so we don't expect to see
1934 the type of override we do in the case above unless it is
1935 overridden by a versioned definition. */
1936 if (hi->root.type != bfd_link_hash_defined
1937 && hi->root.type != bfd_link_hash_defweak)
1938 (*_bfd_error_handler)
1939 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1945 if (! (_bfd_generic_link_add_one_symbol
1946 (info, abfd, shortname, BSF_INDIRECT,
1947 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1949 hi = (struct elf_link_hash_entry *) bh;
1951 /* If there is a duplicate definition somewhere, then HI may not
1952 point to an indirect symbol. We will have reported an error
1953 to the user in that case. */
1955 if (hi->root.type == bfd_link_hash_indirect)
1957 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1958 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1959 hi->dynamic_def |= h->dynamic_def;
1961 /* See if the new flags lead us to realize that the symbol
1967 if (! bfd_link_executable (info)
1973 if (hi->ref_regular)
1983 /* This routine is used to export all defined symbols into the dynamic
1984 symbol table. It is called via elf_link_hash_traverse. */
1987 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1989 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1991 /* Ignore indirect symbols. These are added by the versioning code. */
1992 if (h->root.type == bfd_link_hash_indirect)
1995 /* Ignore this if we won't export it. */
1996 if (!eif->info->export_dynamic && !h->dynamic)
1999 if (h->dynindx == -1
2000 && (h->def_regular || h->ref_regular)
2001 && ! bfd_hide_sym_by_version (eif->info->version_info,
2002 h->root.root.string))
2004 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2014 /* Look through the symbols which are defined in other shared
2015 libraries and referenced here. Update the list of version
2016 dependencies. This will be put into the .gnu.version_r section.
2017 This function is called via elf_link_hash_traverse. */
2020 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2023 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2024 Elf_Internal_Verneed *t;
2025 Elf_Internal_Vernaux *a;
2028 /* We only care about symbols defined in shared objects with version
2033 || h->verinfo.verdef == NULL
2034 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2035 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2038 /* See if we already know about this version. */
2039 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2043 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2046 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2047 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2053 /* This is a new version. Add it to tree we are building. */
2058 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2061 rinfo->failed = TRUE;
2065 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2066 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2067 elf_tdata (rinfo->info->output_bfd)->verref = t;
2071 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2074 rinfo->failed = TRUE;
2078 /* Note that we are copying a string pointer here, and testing it
2079 above. If bfd_elf_string_from_elf_section is ever changed to
2080 discard the string data when low in memory, this will have to be
2082 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2084 a->vna_flags = h->verinfo.verdef->vd_flags;
2085 a->vna_nextptr = t->vn_auxptr;
2087 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2090 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2097 /* Figure out appropriate versions for all the symbols. We may not
2098 have the version number script until we have read all of the input
2099 files, so until that point we don't know which symbols should be
2100 local. This function is called via elf_link_hash_traverse. */
2103 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2105 struct elf_info_failed *sinfo;
2106 struct bfd_link_info *info;
2107 const struct elf_backend_data *bed;
2108 struct elf_info_failed eif;
2111 sinfo = (struct elf_info_failed *) data;
2114 /* Fix the symbol flags. */
2117 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2120 sinfo->failed = TRUE;
2124 /* We only need version numbers for symbols defined in regular
2126 if (!h->def_regular)
2129 bed = get_elf_backend_data (info->output_bfd);
2130 p = strchr (h->root.root.string, ELF_VER_CHR);
2131 if (p != NULL && h->verinfo.vertree == NULL)
2133 struct bfd_elf_version_tree *t;
2136 if (*p == ELF_VER_CHR)
2139 /* If there is no version string, we can just return out. */
2143 /* Look for the version. If we find it, it is no longer weak. */
2144 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2146 if (strcmp (t->name, p) == 0)
2150 struct bfd_elf_version_expr *d;
2152 len = p - h->root.root.string;
2153 alc = (char *) bfd_malloc (len);
2156 sinfo->failed = TRUE;
2159 memcpy (alc, h->root.root.string, len - 1);
2160 alc[len - 1] = '\0';
2161 if (alc[len - 2] == ELF_VER_CHR)
2162 alc[len - 2] = '\0';
2164 h->verinfo.vertree = t;
2168 if (t->globals.list != NULL)
2169 d = (*t->match) (&t->globals, NULL, alc);
2171 /* See if there is anything to force this symbol to
2173 if (d == NULL && t->locals.list != NULL)
2175 d = (*t->match) (&t->locals, NULL, alc);
2178 && ! info->export_dynamic)
2179 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2187 /* If we are building an application, we need to create a
2188 version node for this version. */
2189 if (t == NULL && bfd_link_executable (info))
2191 struct bfd_elf_version_tree **pp;
2194 /* If we aren't going to export this symbol, we don't need
2195 to worry about it. */
2196 if (h->dynindx == -1)
2199 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2203 sinfo->failed = TRUE;
2208 t->name_indx = (unsigned int) -1;
2212 /* Don't count anonymous version tag. */
2213 if (sinfo->info->version_info != NULL
2214 && sinfo->info->version_info->vernum == 0)
2216 for (pp = &sinfo->info->version_info;
2220 t->vernum = version_index;
2224 h->verinfo.vertree = t;
2228 /* We could not find the version for a symbol when
2229 generating a shared archive. Return an error. */
2230 (*_bfd_error_handler)
2231 (_("%B: version node not found for symbol %s"),
2232 info->output_bfd, h->root.root.string);
2233 bfd_set_error (bfd_error_bad_value);
2234 sinfo->failed = TRUE;
2239 /* If we don't have a version for this symbol, see if we can find
2241 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2246 = bfd_find_version_for_sym (sinfo->info->version_info,
2247 h->root.root.string, &hide);
2248 if (h->verinfo.vertree != NULL && hide)
2249 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2255 /* Read and swap the relocs from the section indicated by SHDR. This
2256 may be either a REL or a RELA section. The relocations are
2257 translated into RELA relocations and stored in INTERNAL_RELOCS,
2258 which should have already been allocated to contain enough space.
2259 The EXTERNAL_RELOCS are a buffer where the external form of the
2260 relocations should be stored.
2262 Returns FALSE if something goes wrong. */
2265 elf_link_read_relocs_from_section (bfd *abfd,
2267 Elf_Internal_Shdr *shdr,
2268 void *external_relocs,
2269 Elf_Internal_Rela *internal_relocs)
2271 const struct elf_backend_data *bed;
2272 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2273 const bfd_byte *erela;
2274 const bfd_byte *erelaend;
2275 Elf_Internal_Rela *irela;
2276 Elf_Internal_Shdr *symtab_hdr;
2279 /* Position ourselves at the start of the section. */
2280 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2283 /* Read the relocations. */
2284 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2287 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2288 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2290 bed = get_elf_backend_data (abfd);
2292 /* Convert the external relocations to the internal format. */
2293 if (shdr->sh_entsize == bed->s->sizeof_rel)
2294 swap_in = bed->s->swap_reloc_in;
2295 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2296 swap_in = bed->s->swap_reloca_in;
2299 bfd_set_error (bfd_error_wrong_format);
2303 erela = (const bfd_byte *) external_relocs;
2304 erelaend = erela + shdr->sh_size;
2305 irela = internal_relocs;
2306 while (erela < erelaend)
2310 (*swap_in) (abfd, erela, irela);
2311 r_symndx = ELF32_R_SYM (irela->r_info);
2312 if (bed->s->arch_size == 64)
2316 if ((size_t) r_symndx >= nsyms)
2318 (*_bfd_error_handler)
2319 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2320 " for offset 0x%lx in section `%A'"),
2322 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2323 bfd_set_error (bfd_error_bad_value);
2327 else if (r_symndx != STN_UNDEF)
2329 (*_bfd_error_handler)
2330 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2331 " when the object file has no symbol table"),
2333 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2334 bfd_set_error (bfd_error_bad_value);
2337 irela += bed->s->int_rels_per_ext_rel;
2338 erela += shdr->sh_entsize;
2344 /* Read and swap the relocs for a section O. They may have been
2345 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2346 not NULL, they are used as buffers to read into. They are known to
2347 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2348 the return value is allocated using either malloc or bfd_alloc,
2349 according to the KEEP_MEMORY argument. If O has two relocation
2350 sections (both REL and RELA relocations), then the REL_HDR
2351 relocations will appear first in INTERNAL_RELOCS, followed by the
2352 RELA_HDR relocations. */
2355 _bfd_elf_link_read_relocs (bfd *abfd,
2357 void *external_relocs,
2358 Elf_Internal_Rela *internal_relocs,
2359 bfd_boolean keep_memory)
2361 void *alloc1 = NULL;
2362 Elf_Internal_Rela *alloc2 = NULL;
2363 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2364 struct bfd_elf_section_data *esdo = elf_section_data (o);
2365 Elf_Internal_Rela *internal_rela_relocs;
2367 if (esdo->relocs != NULL)
2368 return esdo->relocs;
2370 if (o->reloc_count == 0)
2373 if (internal_relocs == NULL)
2377 size = o->reloc_count;
2378 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2380 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2382 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2383 if (internal_relocs == NULL)
2387 if (external_relocs == NULL)
2389 bfd_size_type size = 0;
2392 size += esdo->rel.hdr->sh_size;
2394 size += esdo->rela.hdr->sh_size;
2396 alloc1 = bfd_malloc (size);
2399 external_relocs = alloc1;
2402 internal_rela_relocs = internal_relocs;
2405 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2409 external_relocs = (((bfd_byte *) external_relocs)
2410 + esdo->rel.hdr->sh_size);
2411 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2412 * bed->s->int_rels_per_ext_rel);
2416 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2418 internal_rela_relocs)))
2421 /* Cache the results for next time, if we can. */
2423 esdo->relocs = internal_relocs;
2428 /* Don't free alloc2, since if it was allocated we are passing it
2429 back (under the name of internal_relocs). */
2431 return internal_relocs;
2439 bfd_release (abfd, alloc2);
2446 /* Compute the size of, and allocate space for, REL_HDR which is the
2447 section header for a section containing relocations for O. */
2450 _bfd_elf_link_size_reloc_section (bfd *abfd,
2451 struct bfd_elf_section_reloc_data *reldata)
2453 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2455 /* That allows us to calculate the size of the section. */
2456 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2458 /* The contents field must last into write_object_contents, so we
2459 allocate it with bfd_alloc rather than malloc. Also since we
2460 cannot be sure that the contents will actually be filled in,
2461 we zero the allocated space. */
2462 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2463 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2466 if (reldata->hashes == NULL && reldata->count)
2468 struct elf_link_hash_entry **p;
2470 p = ((struct elf_link_hash_entry **)
2471 bfd_zmalloc (reldata->count * sizeof (*p)));
2475 reldata->hashes = p;
2481 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2482 originated from the section given by INPUT_REL_HDR) to the
2486 _bfd_elf_link_output_relocs (bfd *output_bfd,
2487 asection *input_section,
2488 Elf_Internal_Shdr *input_rel_hdr,
2489 Elf_Internal_Rela *internal_relocs,
2490 struct elf_link_hash_entry **rel_hash
2493 Elf_Internal_Rela *irela;
2494 Elf_Internal_Rela *irelaend;
2496 struct bfd_elf_section_reloc_data *output_reldata;
2497 asection *output_section;
2498 const struct elf_backend_data *bed;
2499 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2500 struct bfd_elf_section_data *esdo;
2502 output_section = input_section->output_section;
2504 bed = get_elf_backend_data (output_bfd);
2505 esdo = elf_section_data (output_section);
2506 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2508 output_reldata = &esdo->rel;
2509 swap_out = bed->s->swap_reloc_out;
2511 else if (esdo->rela.hdr
2512 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2514 output_reldata = &esdo->rela;
2515 swap_out = bed->s->swap_reloca_out;
2519 (*_bfd_error_handler)
2520 (_("%B: relocation size mismatch in %B section %A"),
2521 output_bfd, input_section->owner, input_section);
2522 bfd_set_error (bfd_error_wrong_format);
2526 erel = output_reldata->hdr->contents;
2527 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2528 irela = internal_relocs;
2529 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2530 * bed->s->int_rels_per_ext_rel);
2531 while (irela < irelaend)
2533 (*swap_out) (output_bfd, irela, erel);
2534 irela += bed->s->int_rels_per_ext_rel;
2535 erel += input_rel_hdr->sh_entsize;
2538 /* Bump the counter, so that we know where to add the next set of
2540 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2545 /* Make weak undefined symbols in PIE dynamic. */
2548 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2549 struct elf_link_hash_entry *h)
2551 if (bfd_link_pie (info)
2553 && h->root.type == bfd_link_hash_undefweak)
2554 return bfd_elf_link_record_dynamic_symbol (info, h);
2559 /* Fix up the flags for a symbol. This handles various cases which
2560 can only be fixed after all the input files are seen. This is
2561 currently called by both adjust_dynamic_symbol and
2562 assign_sym_version, which is unnecessary but perhaps more robust in
2563 the face of future changes. */
2566 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2567 struct elf_info_failed *eif)
2569 const struct elf_backend_data *bed;
2571 /* If this symbol was mentioned in a non-ELF file, try to set
2572 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2573 permit a non-ELF file to correctly refer to a symbol defined in
2574 an ELF dynamic object. */
2577 while (h->root.type == bfd_link_hash_indirect)
2578 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2580 if (h->root.type != bfd_link_hash_defined
2581 && h->root.type != bfd_link_hash_defweak)
2584 h->ref_regular_nonweak = 1;
2588 if (h->root.u.def.section->owner != NULL
2589 && (bfd_get_flavour (h->root.u.def.section->owner)
2590 == bfd_target_elf_flavour))
2593 h->ref_regular_nonweak = 1;
2599 if (h->dynindx == -1
2603 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2612 /* Unfortunately, NON_ELF is only correct if the symbol
2613 was first seen in a non-ELF file. Fortunately, if the symbol
2614 was first seen in an ELF file, we're probably OK unless the
2615 symbol was defined in a non-ELF file. Catch that case here.
2616 FIXME: We're still in trouble if the symbol was first seen in
2617 a dynamic object, and then later in a non-ELF regular object. */
2618 if ((h->root.type == bfd_link_hash_defined
2619 || h->root.type == bfd_link_hash_defweak)
2621 && (h->root.u.def.section->owner != NULL
2622 ? (bfd_get_flavour (h->root.u.def.section->owner)
2623 != bfd_target_elf_flavour)
2624 : (bfd_is_abs_section (h->root.u.def.section)
2625 && !h->def_dynamic)))
2629 /* Backend specific symbol fixup. */
2630 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2631 if (bed->elf_backend_fixup_symbol
2632 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2635 /* If this is a final link, and the symbol was defined as a common
2636 symbol in a regular object file, and there was no definition in
2637 any dynamic object, then the linker will have allocated space for
2638 the symbol in a common section but the DEF_REGULAR
2639 flag will not have been set. */
2640 if (h->root.type == bfd_link_hash_defined
2644 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2647 /* If -Bsymbolic was used (which means to bind references to global
2648 symbols to the definition within the shared object), and this
2649 symbol was defined in a regular object, then it actually doesn't
2650 need a PLT entry. Likewise, if the symbol has non-default
2651 visibility. If the symbol has hidden or internal visibility, we
2652 will force it local. */
2654 && bfd_link_pic (eif->info)
2655 && is_elf_hash_table (eif->info->hash)
2656 && (SYMBOLIC_BIND (eif->info, h)
2657 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2660 bfd_boolean force_local;
2662 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2663 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2664 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2667 /* If a weak undefined symbol has non-default visibility, we also
2668 hide it from the dynamic linker. */
2669 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2670 && h->root.type == bfd_link_hash_undefweak)
2671 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2673 /* If this is a weak defined symbol in a dynamic object, and we know
2674 the real definition in the dynamic object, copy interesting flags
2675 over to the real definition. */
2676 if (h->u.weakdef != NULL)
2678 /* If the real definition is defined by a regular object file,
2679 don't do anything special. See the longer description in
2680 _bfd_elf_adjust_dynamic_symbol, below. */
2681 if (h->u.weakdef->def_regular)
2682 h->u.weakdef = NULL;
2685 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2687 while (h->root.type == bfd_link_hash_indirect)
2688 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2690 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2691 || h->root.type == bfd_link_hash_defweak);
2692 BFD_ASSERT (weakdef->def_dynamic);
2693 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2694 || weakdef->root.type == bfd_link_hash_defweak);
2695 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2702 /* Make the backend pick a good value for a dynamic symbol. This is
2703 called via elf_link_hash_traverse, and also calls itself
2707 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2709 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2711 const struct elf_backend_data *bed;
2713 if (! is_elf_hash_table (eif->info->hash))
2716 /* Ignore indirect symbols. These are added by the versioning code. */
2717 if (h->root.type == bfd_link_hash_indirect)
2720 /* Fix the symbol flags. */
2721 if (! _bfd_elf_fix_symbol_flags (h, eif))
2724 /* If this symbol does not require a PLT entry, and it is not
2725 defined by a dynamic object, or is not referenced by a regular
2726 object, ignore it. We do have to handle a weak defined symbol,
2727 even if no regular object refers to it, if we decided to add it
2728 to the dynamic symbol table. FIXME: Do we normally need to worry
2729 about symbols which are defined by one dynamic object and
2730 referenced by another one? */
2732 && h->type != STT_GNU_IFUNC
2736 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2738 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2742 /* If we've already adjusted this symbol, don't do it again. This
2743 can happen via a recursive call. */
2744 if (h->dynamic_adjusted)
2747 /* Don't look at this symbol again. Note that we must set this
2748 after checking the above conditions, because we may look at a
2749 symbol once, decide not to do anything, and then get called
2750 recursively later after REF_REGULAR is set below. */
2751 h->dynamic_adjusted = 1;
2753 /* If this is a weak definition, and we know a real definition, and
2754 the real symbol is not itself defined by a regular object file,
2755 then get a good value for the real definition. We handle the
2756 real symbol first, for the convenience of the backend routine.
2758 Note that there is a confusing case here. If the real definition
2759 is defined by a regular object file, we don't get the real symbol
2760 from the dynamic object, but we do get the weak symbol. If the
2761 processor backend uses a COPY reloc, then if some routine in the
2762 dynamic object changes the real symbol, we will not see that
2763 change in the corresponding weak symbol. This is the way other
2764 ELF linkers work as well, and seems to be a result of the shared
2767 I will clarify this issue. Most SVR4 shared libraries define the
2768 variable _timezone and define timezone as a weak synonym. The
2769 tzset call changes _timezone. If you write
2770 extern int timezone;
2772 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2773 you might expect that, since timezone is a synonym for _timezone,
2774 the same number will print both times. However, if the processor
2775 backend uses a COPY reloc, then actually timezone will be copied
2776 into your process image, and, since you define _timezone
2777 yourself, _timezone will not. Thus timezone and _timezone will
2778 wind up at different memory locations. The tzset call will set
2779 _timezone, leaving timezone unchanged. */
2781 if (h->u.weakdef != NULL)
2783 /* If we get to this point, there is an implicit reference to
2784 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2785 h->u.weakdef->ref_regular = 1;
2787 /* Ensure that the backend adjust_dynamic_symbol function sees
2788 H->U.WEAKDEF before H by recursively calling ourselves. */
2789 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2793 /* If a symbol has no type and no size and does not require a PLT
2794 entry, then we are probably about to do the wrong thing here: we
2795 are probably going to create a COPY reloc for an empty object.
2796 This case can arise when a shared object is built with assembly
2797 code, and the assembly code fails to set the symbol type. */
2799 && h->type == STT_NOTYPE
2801 (*_bfd_error_handler)
2802 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2803 h->root.root.string);
2805 dynobj = elf_hash_table (eif->info)->dynobj;
2806 bed = get_elf_backend_data (dynobj);
2808 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2817 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2821 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2822 struct elf_link_hash_entry *h,
2825 unsigned int power_of_two;
2827 asection *sec = h->root.u.def.section;
2829 /* The section aligment of definition is the maximum alignment
2830 requirement of symbols defined in the section. Since we don't
2831 know the symbol alignment requirement, we start with the
2832 maximum alignment and check low bits of the symbol address
2833 for the minimum alignment. */
2834 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2835 mask = ((bfd_vma) 1 << power_of_two) - 1;
2836 while ((h->root.u.def.value & mask) != 0)
2842 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2845 /* Adjust the section alignment if needed. */
2846 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2851 /* We make sure that the symbol will be aligned properly. */
2852 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2854 /* Define the symbol as being at this point in DYNBSS. */
2855 h->root.u.def.section = dynbss;
2856 h->root.u.def.value = dynbss->size;
2858 /* Increment the size of DYNBSS to make room for the symbol. */
2859 dynbss->size += h->size;
2861 /* No error if extern_protected_data is true. */
2862 if (h->protected_def
2863 && (!info->extern_protected_data
2864 || (info->extern_protected_data < 0
2865 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2866 info->callbacks->einfo
2867 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2868 h->root.root.string);
2873 /* Adjust all external symbols pointing into SEC_MERGE sections
2874 to reflect the object merging within the sections. */
2877 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2881 if ((h->root.type == bfd_link_hash_defined
2882 || h->root.type == bfd_link_hash_defweak)
2883 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2884 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2886 bfd *output_bfd = (bfd *) data;
2888 h->root.u.def.value =
2889 _bfd_merged_section_offset (output_bfd,
2890 &h->root.u.def.section,
2891 elf_section_data (sec)->sec_info,
2892 h->root.u.def.value);
2898 /* Returns false if the symbol referred to by H should be considered
2899 to resolve local to the current module, and true if it should be
2900 considered to bind dynamically. */
2903 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2904 struct bfd_link_info *info,
2905 bfd_boolean not_local_protected)
2907 bfd_boolean binding_stays_local_p;
2908 const struct elf_backend_data *bed;
2909 struct elf_link_hash_table *hash_table;
2914 while (h->root.type == bfd_link_hash_indirect
2915 || h->root.type == bfd_link_hash_warning)
2916 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2918 /* If it was forced local, then clearly it's not dynamic. */
2919 if (h->dynindx == -1)
2921 if (h->forced_local)
2924 /* Identify the cases where name binding rules say that a
2925 visible symbol resolves locally. */
2926 binding_stays_local_p = (bfd_link_executable (info)
2927 || SYMBOLIC_BIND (info, h));
2929 switch (ELF_ST_VISIBILITY (h->other))
2936 hash_table = elf_hash_table (info);
2937 if (!is_elf_hash_table (hash_table))
2940 bed = get_elf_backend_data (hash_table->dynobj);
2942 /* Proper resolution for function pointer equality may require
2943 that these symbols perhaps be resolved dynamically, even though
2944 we should be resolving them to the current module. */
2945 if (!not_local_protected || !bed->is_function_type (h->type))
2946 binding_stays_local_p = TRUE;
2953 /* If it isn't defined locally, then clearly it's dynamic. */
2954 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2957 /* Otherwise, the symbol is dynamic if binding rules don't tell
2958 us that it remains local. */
2959 return !binding_stays_local_p;
2962 /* Return true if the symbol referred to by H should be considered
2963 to resolve local to the current module, and false otherwise. Differs
2964 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2965 undefined symbols. The two functions are virtually identical except
2966 for the place where forced_local and dynindx == -1 are tested. If
2967 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2968 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2969 the symbol is local only for defined symbols.
2970 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2971 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2972 treatment of undefined weak symbols. For those that do not make
2973 undefined weak symbols dynamic, both functions may return false. */
2976 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2977 struct bfd_link_info *info,
2978 bfd_boolean local_protected)
2980 const struct elf_backend_data *bed;
2981 struct elf_link_hash_table *hash_table;
2983 /* If it's a local sym, of course we resolve locally. */
2987 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2988 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2989 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2992 /* Common symbols that become definitions don't get the DEF_REGULAR
2993 flag set, so test it first, and don't bail out. */
2994 if (ELF_COMMON_DEF_P (h))
2996 /* If we don't have a definition in a regular file, then we can't
2997 resolve locally. The sym is either undefined or dynamic. */
2998 else if (!h->def_regular)
3001 /* Forced local symbols resolve locally. */
3002 if (h->forced_local)
3005 /* As do non-dynamic symbols. */
3006 if (h->dynindx == -1)
3009 /* At this point, we know the symbol is defined and dynamic. In an
3010 executable it must resolve locally, likewise when building symbolic
3011 shared libraries. */
3012 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3015 /* Now deal with defined dynamic symbols in shared libraries. Ones
3016 with default visibility might not resolve locally. */
3017 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3020 hash_table = elf_hash_table (info);
3021 if (!is_elf_hash_table (hash_table))
3024 bed = get_elf_backend_data (hash_table->dynobj);
3026 /* If extern_protected_data is false, STV_PROTECTED non-function
3027 symbols are local. */
3028 if ((!info->extern_protected_data
3029 || (info->extern_protected_data < 0
3030 && !bed->extern_protected_data))
3031 && !bed->is_function_type (h->type))
3034 /* Function pointer equality tests may require that STV_PROTECTED
3035 symbols be treated as dynamic symbols. If the address of a
3036 function not defined in an executable is set to that function's
3037 plt entry in the executable, then the address of the function in
3038 a shared library must also be the plt entry in the executable. */
3039 return local_protected;
3042 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3043 aligned. Returns the first TLS output section. */
3045 struct bfd_section *
3046 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3048 struct bfd_section *sec, *tls;
3049 unsigned int align = 0;
3051 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3052 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3056 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3057 if (sec->alignment_power > align)
3058 align = sec->alignment_power;
3060 elf_hash_table (info)->tls_sec = tls;
3062 /* Ensure the alignment of the first section is the largest alignment,
3063 so that the tls segment starts aligned. */
3065 tls->alignment_power = align;
3070 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3072 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3073 Elf_Internal_Sym *sym)
3075 const struct elf_backend_data *bed;
3077 /* Local symbols do not count, but target specific ones might. */
3078 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3079 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3082 bed = get_elf_backend_data (abfd);
3083 /* Function symbols do not count. */
3084 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3087 /* If the section is undefined, then so is the symbol. */
3088 if (sym->st_shndx == SHN_UNDEF)
3091 /* If the symbol is defined in the common section, then
3092 it is a common definition and so does not count. */
3093 if (bed->common_definition (sym))
3096 /* If the symbol is in a target specific section then we
3097 must rely upon the backend to tell us what it is. */
3098 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3099 /* FIXME - this function is not coded yet:
3101 return _bfd_is_global_symbol_definition (abfd, sym);
3103 Instead for now assume that the definition is not global,
3104 Even if this is wrong, at least the linker will behave
3105 in the same way that it used to do. */
3111 /* Search the symbol table of the archive element of the archive ABFD
3112 whose archive map contains a mention of SYMDEF, and determine if
3113 the symbol is defined in this element. */
3115 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3117 Elf_Internal_Shdr * hdr;
3121 Elf_Internal_Sym *isymbuf;
3122 Elf_Internal_Sym *isym;
3123 Elf_Internal_Sym *isymend;
3126 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3130 if (! bfd_check_format (abfd, bfd_object))
3133 /* Select the appropriate symbol table. If we don't know if the
3134 object file is an IR object, give linker LTO plugin a chance to
3135 get the correct symbol table. */
3136 if (abfd->plugin_format == bfd_plugin_yes
3137 #ifdef BFD_SUPPORTS_PLUGINS
3138 || (abfd->plugin_format == bfd_plugin_unknown
3139 && bfd_link_plugin_object_p (abfd))
3143 /* Use the IR symbol table if the object has been claimed by
3145 abfd = abfd->plugin_dummy_bfd;
3146 hdr = &elf_tdata (abfd)->symtab_hdr;
3148 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3149 hdr = &elf_tdata (abfd)->symtab_hdr;
3151 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3153 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3155 /* The sh_info field of the symtab header tells us where the
3156 external symbols start. We don't care about the local symbols. */
3157 if (elf_bad_symtab (abfd))
3159 extsymcount = symcount;
3164 extsymcount = symcount - hdr->sh_info;
3165 extsymoff = hdr->sh_info;
3168 if (extsymcount == 0)
3171 /* Read in the symbol table. */
3172 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3174 if (isymbuf == NULL)
3177 /* Scan the symbol table looking for SYMDEF. */
3179 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3183 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3188 if (strcmp (name, symdef->name) == 0)
3190 result = is_global_data_symbol_definition (abfd, isym);
3200 /* Add an entry to the .dynamic table. */
3203 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3207 struct elf_link_hash_table *hash_table;
3208 const struct elf_backend_data *bed;
3210 bfd_size_type newsize;
3211 bfd_byte *newcontents;
3212 Elf_Internal_Dyn dyn;
3214 hash_table = elf_hash_table (info);
3215 if (! is_elf_hash_table (hash_table))
3218 bed = get_elf_backend_data (hash_table->dynobj);
3219 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3220 BFD_ASSERT (s != NULL);
3222 newsize = s->size + bed->s->sizeof_dyn;
3223 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3224 if (newcontents == NULL)
3228 dyn.d_un.d_val = val;
3229 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3232 s->contents = newcontents;
3237 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3238 otherwise just check whether one already exists. Returns -1 on error,
3239 1 if a DT_NEEDED tag already exists, and 0 on success. */
3242 elf_add_dt_needed_tag (bfd *abfd,
3243 struct bfd_link_info *info,
3247 struct elf_link_hash_table *hash_table;
3250 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3253 hash_table = elf_hash_table (info);
3254 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3255 if (strindex == (size_t) -1)
3258 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3261 const struct elf_backend_data *bed;
3264 bed = get_elf_backend_data (hash_table->dynobj);
3265 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3267 for (extdyn = sdyn->contents;
3268 extdyn < sdyn->contents + sdyn->size;
3269 extdyn += bed->s->sizeof_dyn)
3271 Elf_Internal_Dyn dyn;
3273 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3274 if (dyn.d_tag == DT_NEEDED
3275 && dyn.d_un.d_val == strindex)
3277 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3285 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3288 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3292 /* We were just checking for existence of the tag. */
3293 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3298 /* Return true if SONAME is on the needed list between NEEDED and STOP
3299 (or the end of list if STOP is NULL), and needed by a library that
3303 on_needed_list (const char *soname,
3304 struct bfd_link_needed_list *needed,
3305 struct bfd_link_needed_list *stop)
3307 struct bfd_link_needed_list *look;
3308 for (look = needed; look != stop; look = look->next)
3309 if (strcmp (soname, look->name) == 0
3310 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3311 /* If needed by a library that itself is not directly
3312 needed, recursively check whether that library is
3313 indirectly needed. Since we add DT_NEEDED entries to
3314 the end of the list, library dependencies appear after
3315 the library. Therefore search prior to the current
3316 LOOK, preventing possible infinite recursion. */
3317 || on_needed_list (elf_dt_name (look->by), needed, look)))
3323 /* Sort symbol by value, section, and size. */
3325 elf_sort_symbol (const void *arg1, const void *arg2)
3327 const struct elf_link_hash_entry *h1;
3328 const struct elf_link_hash_entry *h2;
3329 bfd_signed_vma vdiff;
3331 h1 = *(const struct elf_link_hash_entry **) arg1;
3332 h2 = *(const struct elf_link_hash_entry **) arg2;
3333 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3335 return vdiff > 0 ? 1 : -1;
3338 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3340 return sdiff > 0 ? 1 : -1;
3342 vdiff = h1->size - h2->size;
3343 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3346 /* This function is used to adjust offsets into .dynstr for
3347 dynamic symbols. This is called via elf_link_hash_traverse. */
3350 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3352 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3354 if (h->dynindx != -1)
3355 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3359 /* Assign string offsets in .dynstr, update all structures referencing
3363 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3365 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3366 struct elf_link_local_dynamic_entry *entry;
3367 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3368 bfd *dynobj = hash_table->dynobj;
3371 const struct elf_backend_data *bed;
3374 _bfd_elf_strtab_finalize (dynstr);
3375 size = _bfd_elf_strtab_size (dynstr);
3377 bed = get_elf_backend_data (dynobj);
3378 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3379 BFD_ASSERT (sdyn != NULL);
3381 /* Update all .dynamic entries referencing .dynstr strings. */
3382 for (extdyn = sdyn->contents;
3383 extdyn < sdyn->contents + sdyn->size;
3384 extdyn += bed->s->sizeof_dyn)
3386 Elf_Internal_Dyn dyn;
3388 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3392 dyn.d_un.d_val = size;
3402 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3407 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3410 /* Now update local dynamic symbols. */
3411 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3412 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3413 entry->isym.st_name);
3415 /* And the rest of dynamic symbols. */
3416 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3418 /* Adjust version definitions. */
3419 if (elf_tdata (output_bfd)->cverdefs)
3424 Elf_Internal_Verdef def;
3425 Elf_Internal_Verdaux defaux;
3427 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3431 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3433 p += sizeof (Elf_External_Verdef);
3434 if (def.vd_aux != sizeof (Elf_External_Verdef))
3436 for (i = 0; i < def.vd_cnt; ++i)
3438 _bfd_elf_swap_verdaux_in (output_bfd,
3439 (Elf_External_Verdaux *) p, &defaux);
3440 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3442 _bfd_elf_swap_verdaux_out (output_bfd,
3443 &defaux, (Elf_External_Verdaux *) p);
3444 p += sizeof (Elf_External_Verdaux);
3447 while (def.vd_next);
3450 /* Adjust version references. */
3451 if (elf_tdata (output_bfd)->verref)
3456 Elf_Internal_Verneed need;
3457 Elf_Internal_Vernaux needaux;
3459 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3463 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3465 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3466 _bfd_elf_swap_verneed_out (output_bfd, &need,
3467 (Elf_External_Verneed *) p);
3468 p += sizeof (Elf_External_Verneed);
3469 for (i = 0; i < need.vn_cnt; ++i)
3471 _bfd_elf_swap_vernaux_in (output_bfd,
3472 (Elf_External_Vernaux *) p, &needaux);
3473 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3475 _bfd_elf_swap_vernaux_out (output_bfd,
3477 (Elf_External_Vernaux *) p);
3478 p += sizeof (Elf_External_Vernaux);
3481 while (need.vn_next);
3487 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3488 The default is to only match when the INPUT and OUTPUT are exactly
3492 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3493 const bfd_target *output)
3495 return input == output;
3498 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3499 This version is used when different targets for the same architecture
3500 are virtually identical. */
3503 _bfd_elf_relocs_compatible (const bfd_target *input,
3504 const bfd_target *output)
3506 const struct elf_backend_data *obed, *ibed;
3508 if (input == output)
3511 ibed = xvec_get_elf_backend_data (input);
3512 obed = xvec_get_elf_backend_data (output);
3514 if (ibed->arch != obed->arch)
3517 /* If both backends are using this function, deem them compatible. */
3518 return ibed->relocs_compatible == obed->relocs_compatible;
3521 /* Make a special call to the linker "notice" function to tell it that
3522 we are about to handle an as-needed lib, or have finished
3523 processing the lib. */
3526 _bfd_elf_notice_as_needed (bfd *ibfd,
3527 struct bfd_link_info *info,
3528 enum notice_asneeded_action act)
3530 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3533 /* Check relocations an ELF object file. */
3536 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3538 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3539 struct elf_link_hash_table *htab = elf_hash_table (info);
3541 /* If this object is the same format as the output object, and it is
3542 not a shared library, then let the backend look through the
3545 This is required to build global offset table entries and to
3546 arrange for dynamic relocs. It is not required for the
3547 particular common case of linking non PIC code, even when linking
3548 against shared libraries, but unfortunately there is no way of
3549 knowing whether an object file has been compiled PIC or not.
3550 Looking through the relocs is not particularly time consuming.
3551 The problem is that we must either (1) keep the relocs in memory,
3552 which causes the linker to require additional runtime memory or
3553 (2) read the relocs twice from the input file, which wastes time.
3554 This would be a good case for using mmap.
3556 I have no idea how to handle linking PIC code into a file of a
3557 different format. It probably can't be done. */
3558 if ((abfd->flags & DYNAMIC) == 0
3559 && is_elf_hash_table (htab)
3560 && bed->check_relocs != NULL
3561 && elf_object_id (abfd) == elf_hash_table_id (htab)
3562 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3566 for (o = abfd->sections; o != NULL; o = o->next)
3568 Elf_Internal_Rela *internal_relocs;
3571 /* Don't check relocations in excluded sections. */
3572 if ((o->flags & SEC_RELOC) == 0
3573 || (o->flags & SEC_EXCLUDE) != 0
3574 || o->reloc_count == 0
3575 || ((info->strip == strip_all || info->strip == strip_debugger)
3576 && (o->flags & SEC_DEBUGGING) != 0)
3577 || bfd_is_abs_section (o->output_section))
3580 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3582 if (internal_relocs == NULL)
3585 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3587 if (elf_section_data (o)->relocs != internal_relocs)
3588 free (internal_relocs);
3598 /* Add symbols from an ELF object file to the linker hash table. */
3601 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3603 Elf_Internal_Ehdr *ehdr;
3604 Elf_Internal_Shdr *hdr;
3608 struct elf_link_hash_entry **sym_hash;
3609 bfd_boolean dynamic;
3610 Elf_External_Versym *extversym = NULL;
3611 Elf_External_Versym *ever;
3612 struct elf_link_hash_entry *weaks;
3613 struct elf_link_hash_entry **nondeflt_vers = NULL;
3614 size_t nondeflt_vers_cnt = 0;
3615 Elf_Internal_Sym *isymbuf = NULL;
3616 Elf_Internal_Sym *isym;
3617 Elf_Internal_Sym *isymend;
3618 const struct elf_backend_data *bed;
3619 bfd_boolean add_needed;
3620 struct elf_link_hash_table *htab;
3622 void *alloc_mark = NULL;
3623 struct bfd_hash_entry **old_table = NULL;
3624 unsigned int old_size = 0;
3625 unsigned int old_count = 0;
3626 void *old_tab = NULL;
3628 struct bfd_link_hash_entry *old_undefs = NULL;
3629 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3630 void *old_strtab = NULL;
3633 bfd_boolean just_syms;
3635 htab = elf_hash_table (info);
3636 bed = get_elf_backend_data (abfd);
3638 if ((abfd->flags & DYNAMIC) == 0)
3644 /* You can't use -r against a dynamic object. Also, there's no
3645 hope of using a dynamic object which does not exactly match
3646 the format of the output file. */
3647 if (bfd_link_relocatable (info)
3648 || !is_elf_hash_table (htab)
3649 || info->output_bfd->xvec != abfd->xvec)
3651 if (bfd_link_relocatable (info))
3652 bfd_set_error (bfd_error_invalid_operation);
3654 bfd_set_error (bfd_error_wrong_format);
3659 ehdr = elf_elfheader (abfd);
3660 if (info->warn_alternate_em
3661 && bed->elf_machine_code != ehdr->e_machine
3662 && ((bed->elf_machine_alt1 != 0
3663 && ehdr->e_machine == bed->elf_machine_alt1)
3664 || (bed->elf_machine_alt2 != 0
3665 && ehdr->e_machine == bed->elf_machine_alt2)))
3666 info->callbacks->einfo
3667 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3668 ehdr->e_machine, abfd, bed->elf_machine_code);
3670 /* As a GNU extension, any input sections which are named
3671 .gnu.warning.SYMBOL are treated as warning symbols for the given
3672 symbol. This differs from .gnu.warning sections, which generate
3673 warnings when they are included in an output file. */
3674 /* PR 12761: Also generate this warning when building shared libraries. */
3675 for (s = abfd->sections; s != NULL; s = s->next)
3679 name = bfd_get_section_name (abfd, s);
3680 if (CONST_STRNEQ (name, ".gnu.warning."))
3685 name += sizeof ".gnu.warning." - 1;
3687 /* If this is a shared object, then look up the symbol
3688 in the hash table. If it is there, and it is already
3689 been defined, then we will not be using the entry
3690 from this shared object, so we don't need to warn.
3691 FIXME: If we see the definition in a regular object
3692 later on, we will warn, but we shouldn't. The only
3693 fix is to keep track of what warnings we are supposed
3694 to emit, and then handle them all at the end of the
3698 struct elf_link_hash_entry *h;
3700 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3702 /* FIXME: What about bfd_link_hash_common? */
3704 && (h->root.type == bfd_link_hash_defined
3705 || h->root.type == bfd_link_hash_defweak))
3710 msg = (char *) bfd_alloc (abfd, sz + 1);
3714 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3719 if (! (_bfd_generic_link_add_one_symbol
3720 (info, abfd, name, BSF_WARNING, s, 0, msg,
3721 FALSE, bed->collect, NULL)))
3724 if (bfd_link_executable (info))
3726 /* Clobber the section size so that the warning does
3727 not get copied into the output file. */
3730 /* Also set SEC_EXCLUDE, so that symbols defined in
3731 the warning section don't get copied to the output. */
3732 s->flags |= SEC_EXCLUDE;
3737 just_syms = ((s = abfd->sections) != NULL
3738 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3743 /* If we are creating a shared library, create all the dynamic
3744 sections immediately. We need to attach them to something,
3745 so we attach them to this BFD, provided it is the right
3746 format and is not from ld --just-symbols. Always create the
3747 dynamic sections for -E/--dynamic-list. FIXME: If there
3748 are no input BFD's of the same format as the output, we can't
3749 make a shared library. */
3751 && (bfd_link_pic (info)
3752 || (!bfd_link_relocatable (info)
3753 && (info->export_dynamic || info->dynamic)))
3754 && is_elf_hash_table (htab)
3755 && info->output_bfd->xvec == abfd->xvec
3756 && !htab->dynamic_sections_created)
3758 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3762 else if (!is_elf_hash_table (htab))
3766 const char *soname = NULL;
3768 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3771 /* ld --just-symbols and dynamic objects don't mix very well.
3772 ld shouldn't allow it. */
3776 /* If this dynamic lib was specified on the command line with
3777 --as-needed in effect, then we don't want to add a DT_NEEDED
3778 tag unless the lib is actually used. Similary for libs brought
3779 in by another lib's DT_NEEDED. When --no-add-needed is used
3780 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3781 any dynamic library in DT_NEEDED tags in the dynamic lib at
3783 add_needed = (elf_dyn_lib_class (abfd)
3784 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3785 | DYN_NO_NEEDED)) == 0;
3787 s = bfd_get_section_by_name (abfd, ".dynamic");
3792 unsigned int elfsec;
3793 unsigned long shlink;
3795 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3802 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3803 if (elfsec == SHN_BAD)
3804 goto error_free_dyn;
3805 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3807 for (extdyn = dynbuf;
3808 extdyn < dynbuf + s->size;
3809 extdyn += bed->s->sizeof_dyn)
3811 Elf_Internal_Dyn dyn;
3813 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3814 if (dyn.d_tag == DT_SONAME)
3816 unsigned int tagv = dyn.d_un.d_val;
3817 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3819 goto error_free_dyn;
3821 if (dyn.d_tag == DT_NEEDED)
3823 struct bfd_link_needed_list *n, **pn;
3825 unsigned int tagv = dyn.d_un.d_val;
3827 amt = sizeof (struct bfd_link_needed_list);
3828 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3829 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3830 if (n == NULL || fnm == NULL)
3831 goto error_free_dyn;
3832 amt = strlen (fnm) + 1;
3833 anm = (char *) bfd_alloc (abfd, amt);
3835 goto error_free_dyn;
3836 memcpy (anm, fnm, amt);
3840 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3844 if (dyn.d_tag == DT_RUNPATH)
3846 struct bfd_link_needed_list *n, **pn;
3848 unsigned int tagv = dyn.d_un.d_val;
3850 amt = sizeof (struct bfd_link_needed_list);
3851 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3852 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3853 if (n == NULL || fnm == NULL)
3854 goto error_free_dyn;
3855 amt = strlen (fnm) + 1;
3856 anm = (char *) bfd_alloc (abfd, amt);
3858 goto error_free_dyn;
3859 memcpy (anm, fnm, amt);
3863 for (pn = & runpath;
3869 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3870 if (!runpath && dyn.d_tag == DT_RPATH)
3872 struct bfd_link_needed_list *n, **pn;
3874 unsigned int tagv = dyn.d_un.d_val;
3876 amt = sizeof (struct bfd_link_needed_list);
3877 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3878 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3879 if (n == NULL || fnm == NULL)
3880 goto error_free_dyn;
3881 amt = strlen (fnm) + 1;
3882 anm = (char *) bfd_alloc (abfd, amt);
3884 goto error_free_dyn;
3885 memcpy (anm, fnm, amt);
3895 if (dyn.d_tag == DT_AUDIT)
3897 unsigned int tagv = dyn.d_un.d_val;
3898 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3905 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3906 frees all more recently bfd_alloc'd blocks as well. */
3912 struct bfd_link_needed_list **pn;
3913 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3918 /* We do not want to include any of the sections in a dynamic
3919 object in the output file. We hack by simply clobbering the
3920 list of sections in the BFD. This could be handled more
3921 cleanly by, say, a new section flag; the existing
3922 SEC_NEVER_LOAD flag is not the one we want, because that one
3923 still implies that the section takes up space in the output
3925 bfd_section_list_clear (abfd);
3927 /* Find the name to use in a DT_NEEDED entry that refers to this
3928 object. If the object has a DT_SONAME entry, we use it.
3929 Otherwise, if the generic linker stuck something in
3930 elf_dt_name, we use that. Otherwise, we just use the file
3932 if (soname == NULL || *soname == '\0')
3934 soname = elf_dt_name (abfd);
3935 if (soname == NULL || *soname == '\0')
3936 soname = bfd_get_filename (abfd);
3939 /* Save the SONAME because sometimes the linker emulation code
3940 will need to know it. */
3941 elf_dt_name (abfd) = soname;
3943 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3947 /* If we have already included this dynamic object in the
3948 link, just ignore it. There is no reason to include a
3949 particular dynamic object more than once. */
3953 /* Save the DT_AUDIT entry for the linker emulation code. */
3954 elf_dt_audit (abfd) = audit;
3957 /* If this is a dynamic object, we always link against the .dynsym
3958 symbol table, not the .symtab symbol table. The dynamic linker
3959 will only see the .dynsym symbol table, so there is no reason to
3960 look at .symtab for a dynamic object. */
3962 if (! dynamic || elf_dynsymtab (abfd) == 0)
3963 hdr = &elf_tdata (abfd)->symtab_hdr;
3965 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3967 symcount = hdr->sh_size / bed->s->sizeof_sym;
3969 /* The sh_info field of the symtab header tells us where the
3970 external symbols start. We don't care about the local symbols at
3972 if (elf_bad_symtab (abfd))
3974 extsymcount = symcount;
3979 extsymcount = symcount - hdr->sh_info;
3980 extsymoff = hdr->sh_info;
3983 sym_hash = elf_sym_hashes (abfd);
3984 if (extsymcount != 0)
3986 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3988 if (isymbuf == NULL)
3991 if (sym_hash == NULL)
3993 /* We store a pointer to the hash table entry for each
3996 amt *= sizeof (struct elf_link_hash_entry *);
3997 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3998 if (sym_hash == NULL)
3999 goto error_free_sym;
4000 elf_sym_hashes (abfd) = sym_hash;
4006 /* Read in any version definitions. */
4007 if (!_bfd_elf_slurp_version_tables (abfd,
4008 info->default_imported_symver))
4009 goto error_free_sym;
4011 /* Read in the symbol versions, but don't bother to convert them
4012 to internal format. */
4013 if (elf_dynversym (abfd) != 0)
4015 Elf_Internal_Shdr *versymhdr;
4017 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4018 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4019 if (extversym == NULL)
4020 goto error_free_sym;
4021 amt = versymhdr->sh_size;
4022 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4023 || bfd_bread (extversym, amt, abfd) != amt)
4024 goto error_free_vers;
4028 /* If we are loading an as-needed shared lib, save the symbol table
4029 state before we start adding symbols. If the lib turns out
4030 to be unneeded, restore the state. */
4031 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4036 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4038 struct bfd_hash_entry *p;
4039 struct elf_link_hash_entry *h;
4041 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4043 h = (struct elf_link_hash_entry *) p;
4044 entsize += htab->root.table.entsize;
4045 if (h->root.type == bfd_link_hash_warning)
4046 entsize += htab->root.table.entsize;
4050 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4051 old_tab = bfd_malloc (tabsize + entsize);
4052 if (old_tab == NULL)
4053 goto error_free_vers;
4055 /* Remember the current objalloc pointer, so that all mem for
4056 symbols added can later be reclaimed. */
4057 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4058 if (alloc_mark == NULL)
4059 goto error_free_vers;
4061 /* Make a special call to the linker "notice" function to
4062 tell it that we are about to handle an as-needed lib. */
4063 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4064 goto error_free_vers;
4066 /* Clone the symbol table. Remember some pointers into the
4067 symbol table, and dynamic symbol count. */
4068 old_ent = (char *) old_tab + tabsize;
4069 memcpy (old_tab, htab->root.table.table, tabsize);
4070 old_undefs = htab->root.undefs;
4071 old_undefs_tail = htab->root.undefs_tail;
4072 old_table = htab->root.table.table;
4073 old_size = htab->root.table.size;
4074 old_count = htab->root.table.count;
4075 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4076 if (old_strtab == NULL)
4077 goto error_free_vers;
4079 for (i = 0; i < htab->root.table.size; i++)
4081 struct bfd_hash_entry *p;
4082 struct elf_link_hash_entry *h;
4084 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4086 memcpy (old_ent, p, htab->root.table.entsize);
4087 old_ent = (char *) old_ent + htab->root.table.entsize;
4088 h = (struct elf_link_hash_entry *) p;
4089 if (h->root.type == bfd_link_hash_warning)
4091 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4092 old_ent = (char *) old_ent + htab->root.table.entsize;
4099 ever = extversym != NULL ? extversym + extsymoff : NULL;
4100 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4102 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4106 asection *sec, *new_sec;
4109 struct elf_link_hash_entry *h;
4110 struct elf_link_hash_entry *hi;
4111 bfd_boolean definition;
4112 bfd_boolean size_change_ok;
4113 bfd_boolean type_change_ok;
4114 bfd_boolean new_weakdef;
4115 bfd_boolean new_weak;
4116 bfd_boolean old_weak;
4117 bfd_boolean override;
4119 bfd_boolean discarded;
4120 unsigned int old_alignment;
4122 bfd_boolean matched;
4126 flags = BSF_NO_FLAGS;
4128 value = isym->st_value;
4129 common = bed->common_definition (isym);
4132 bind = ELF_ST_BIND (isym->st_info);
4136 /* This should be impossible, since ELF requires that all
4137 global symbols follow all local symbols, and that sh_info
4138 point to the first global symbol. Unfortunately, Irix 5
4143 if (isym->st_shndx != SHN_UNDEF && !common)
4151 case STB_GNU_UNIQUE:
4152 flags = BSF_GNU_UNIQUE;
4156 /* Leave it up to the processor backend. */
4160 if (isym->st_shndx == SHN_UNDEF)
4161 sec = bfd_und_section_ptr;
4162 else if (isym->st_shndx == SHN_ABS)
4163 sec = bfd_abs_section_ptr;
4164 else if (isym->st_shndx == SHN_COMMON)
4166 sec = bfd_com_section_ptr;
4167 /* What ELF calls the size we call the value. What ELF
4168 calls the value we call the alignment. */
4169 value = isym->st_size;
4173 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4175 sec = bfd_abs_section_ptr;
4176 else if (discarded_section (sec))
4178 /* Symbols from discarded section are undefined. We keep
4180 sec = bfd_und_section_ptr;
4182 isym->st_shndx = SHN_UNDEF;
4184 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4188 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4191 goto error_free_vers;
4193 if (isym->st_shndx == SHN_COMMON
4194 && (abfd->flags & BFD_PLUGIN) != 0)
4196 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4200 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4202 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4204 goto error_free_vers;
4208 else if (isym->st_shndx == SHN_COMMON
4209 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4210 && !bfd_link_relocatable (info))
4212 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4216 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4217 | SEC_LINKER_CREATED);
4218 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4220 goto error_free_vers;
4224 else if (bed->elf_add_symbol_hook)
4226 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4228 goto error_free_vers;
4230 /* The hook function sets the name to NULL if this symbol
4231 should be skipped for some reason. */
4236 /* Sanity check that all possibilities were handled. */
4239 bfd_set_error (bfd_error_bad_value);
4240 goto error_free_vers;
4243 /* Silently discard TLS symbols from --just-syms. There's
4244 no way to combine a static TLS block with a new TLS block
4245 for this executable. */
4246 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4247 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4250 if (bfd_is_und_section (sec)
4251 || bfd_is_com_section (sec))
4256 size_change_ok = FALSE;
4257 type_change_ok = bed->type_change_ok;
4264 if (is_elf_hash_table (htab))
4266 Elf_Internal_Versym iver;
4267 unsigned int vernum = 0;
4272 if (info->default_imported_symver)
4273 /* Use the default symbol version created earlier. */
4274 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4279 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4281 vernum = iver.vs_vers & VERSYM_VERSION;
4283 /* If this is a hidden symbol, or if it is not version
4284 1, we append the version name to the symbol name.
4285 However, we do not modify a non-hidden absolute symbol
4286 if it is not a function, because it might be the version
4287 symbol itself. FIXME: What if it isn't? */
4288 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4290 && (!bfd_is_abs_section (sec)
4291 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4294 size_t namelen, verlen, newlen;
4297 if (isym->st_shndx != SHN_UNDEF)
4299 if (vernum > elf_tdata (abfd)->cverdefs)
4301 else if (vernum > 1)
4303 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4309 (*_bfd_error_handler)
4310 (_("%B: %s: invalid version %u (max %d)"),
4312 elf_tdata (abfd)->cverdefs);
4313 bfd_set_error (bfd_error_bad_value);
4314 goto error_free_vers;
4319 /* We cannot simply test for the number of
4320 entries in the VERNEED section since the
4321 numbers for the needed versions do not start
4323 Elf_Internal_Verneed *t;
4326 for (t = elf_tdata (abfd)->verref;
4330 Elf_Internal_Vernaux *a;
4332 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4334 if (a->vna_other == vernum)
4336 verstr = a->vna_nodename;
4345 (*_bfd_error_handler)
4346 (_("%B: %s: invalid needed version %d"),
4347 abfd, name, vernum);
4348 bfd_set_error (bfd_error_bad_value);
4349 goto error_free_vers;
4353 namelen = strlen (name);
4354 verlen = strlen (verstr);
4355 newlen = namelen + verlen + 2;
4356 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4357 && isym->st_shndx != SHN_UNDEF)
4360 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4361 if (newname == NULL)
4362 goto error_free_vers;
4363 memcpy (newname, name, namelen);
4364 p = newname + namelen;
4366 /* If this is a defined non-hidden version symbol,
4367 we add another @ to the name. This indicates the
4368 default version of the symbol. */
4369 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4370 && isym->st_shndx != SHN_UNDEF)
4372 memcpy (p, verstr, verlen + 1);
4377 /* If this symbol has default visibility and the user has
4378 requested we not re-export it, then mark it as hidden. */
4379 if (!bfd_is_und_section (sec)
4382 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4383 isym->st_other = (STV_HIDDEN
4384 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4386 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4387 sym_hash, &old_bfd, &old_weak,
4388 &old_alignment, &skip, &override,
4389 &type_change_ok, &size_change_ok,
4391 goto error_free_vers;
4396 /* Override a definition only if the new symbol matches the
4398 if (override && matched)
4402 while (h->root.type == bfd_link_hash_indirect
4403 || h->root.type == bfd_link_hash_warning)
4404 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4406 if (elf_tdata (abfd)->verdef != NULL
4409 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4412 if (! (_bfd_generic_link_add_one_symbol
4413 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4414 (struct bfd_link_hash_entry **) sym_hash)))
4415 goto error_free_vers;
4417 if ((flags & BSF_GNU_UNIQUE)
4418 && (abfd->flags & DYNAMIC) == 0
4419 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4420 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4423 /* We need to make sure that indirect symbol dynamic flags are
4426 while (h->root.type == bfd_link_hash_indirect
4427 || h->root.type == bfd_link_hash_warning)
4428 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4430 /* Setting the index to -3 tells elf_link_output_extsym that
4431 this symbol is defined in a discarded section. */
4437 new_weak = (flags & BSF_WEAK) != 0;
4438 new_weakdef = FALSE;
4442 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4443 && is_elf_hash_table (htab)
4444 && h->u.weakdef == NULL)
4446 /* Keep a list of all weak defined non function symbols from
4447 a dynamic object, using the weakdef field. Later in this
4448 function we will set the weakdef field to the correct
4449 value. We only put non-function symbols from dynamic
4450 objects on this list, because that happens to be the only
4451 time we need to know the normal symbol corresponding to a
4452 weak symbol, and the information is time consuming to
4453 figure out. If the weakdef field is not already NULL,
4454 then this symbol was already defined by some previous
4455 dynamic object, and we will be using that previous
4456 definition anyhow. */
4458 h->u.weakdef = weaks;
4463 /* Set the alignment of a common symbol. */
4464 if ((common || bfd_is_com_section (sec))
4465 && h->root.type == bfd_link_hash_common)
4470 align = bfd_log2 (isym->st_value);
4473 /* The new symbol is a common symbol in a shared object.
4474 We need to get the alignment from the section. */
4475 align = new_sec->alignment_power;
4477 if (align > old_alignment)
4478 h->root.u.c.p->alignment_power = align;
4480 h->root.u.c.p->alignment_power = old_alignment;
4483 if (is_elf_hash_table (htab))
4485 /* Set a flag in the hash table entry indicating the type of
4486 reference or definition we just found. A dynamic symbol
4487 is one which is referenced or defined by both a regular
4488 object and a shared object. */
4489 bfd_boolean dynsym = FALSE;
4491 /* Plugin symbols aren't normal. Don't set def_regular or
4492 ref_regular for them, or make them dynamic. */
4493 if ((abfd->flags & BFD_PLUGIN) != 0)
4500 if (bind != STB_WEAK)
4501 h->ref_regular_nonweak = 1;
4513 /* If the indirect symbol has been forced local, don't
4514 make the real symbol dynamic. */
4515 if ((h == hi || !hi->forced_local)
4516 && (bfd_link_dll (info)
4526 hi->ref_dynamic = 1;
4531 hi->def_dynamic = 1;
4534 /* If the indirect symbol has been forced local, don't
4535 make the real symbol dynamic. */
4536 if ((h == hi || !hi->forced_local)
4539 || (h->u.weakdef != NULL
4541 && h->u.weakdef->dynindx != -1)))
4545 /* Check to see if we need to add an indirect symbol for
4546 the default name. */
4548 || (!override && h->root.type == bfd_link_hash_common))
4549 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4550 sec, value, &old_bfd, &dynsym))
4551 goto error_free_vers;
4553 /* Check the alignment when a common symbol is involved. This
4554 can change when a common symbol is overridden by a normal
4555 definition or a common symbol is ignored due to the old
4556 normal definition. We need to make sure the maximum
4557 alignment is maintained. */
4558 if ((old_alignment || common)
4559 && h->root.type != bfd_link_hash_common)
4561 unsigned int common_align;
4562 unsigned int normal_align;
4563 unsigned int symbol_align;
4567 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4568 || h->root.type == bfd_link_hash_defweak);
4570 symbol_align = ffs (h->root.u.def.value) - 1;
4571 if (h->root.u.def.section->owner != NULL
4572 && (h->root.u.def.section->owner->flags
4573 & (DYNAMIC | BFD_PLUGIN)) == 0)
4575 normal_align = h->root.u.def.section->alignment_power;
4576 if (normal_align > symbol_align)
4577 normal_align = symbol_align;
4580 normal_align = symbol_align;
4584 common_align = old_alignment;
4585 common_bfd = old_bfd;
4590 common_align = bfd_log2 (isym->st_value);
4592 normal_bfd = old_bfd;
4595 if (normal_align < common_align)
4597 /* PR binutils/2735 */
4598 if (normal_bfd == NULL)
4599 (*_bfd_error_handler)
4600 (_("Warning: alignment %u of common symbol `%s' in %B is"
4601 " greater than the alignment (%u) of its section %A"),
4602 common_bfd, h->root.u.def.section,
4603 1 << common_align, name, 1 << normal_align);
4605 (*_bfd_error_handler)
4606 (_("Warning: alignment %u of symbol `%s' in %B"
4607 " is smaller than %u in %B"),
4608 normal_bfd, common_bfd,
4609 1 << normal_align, name, 1 << common_align);
4613 /* Remember the symbol size if it isn't undefined. */
4614 if (isym->st_size != 0
4615 && isym->st_shndx != SHN_UNDEF
4616 && (definition || h->size == 0))
4619 && h->size != isym->st_size
4620 && ! size_change_ok)
4621 (*_bfd_error_handler)
4622 (_("Warning: size of symbol `%s' changed"
4623 " from %lu in %B to %lu in %B"),
4625 name, (unsigned long) h->size,
4626 (unsigned long) isym->st_size);
4628 h->size = isym->st_size;
4631 /* If this is a common symbol, then we always want H->SIZE
4632 to be the size of the common symbol. The code just above
4633 won't fix the size if a common symbol becomes larger. We
4634 don't warn about a size change here, because that is
4635 covered by --warn-common. Allow changes between different
4637 if (h->root.type == bfd_link_hash_common)
4638 h->size = h->root.u.c.size;
4640 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4641 && ((definition && !new_weak)
4642 || (old_weak && h->root.type == bfd_link_hash_common)
4643 || h->type == STT_NOTYPE))
4645 unsigned int type = ELF_ST_TYPE (isym->st_info);
4647 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4649 if (type == STT_GNU_IFUNC
4650 && (abfd->flags & DYNAMIC) != 0)
4653 if (h->type != type)
4655 if (h->type != STT_NOTYPE && ! type_change_ok)
4656 (*_bfd_error_handler)
4657 (_("Warning: type of symbol `%s' changed"
4658 " from %d to %d in %B"),
4659 abfd, name, h->type, type);
4665 /* Merge st_other field. */
4666 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4668 /* We don't want to make debug symbol dynamic. */
4670 && (sec->flags & SEC_DEBUGGING)
4671 && !bfd_link_relocatable (info))
4674 /* Nor should we make plugin symbols dynamic. */
4675 if ((abfd->flags & BFD_PLUGIN) != 0)
4680 h->target_internal = isym->st_target_internal;
4681 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4684 if (definition && !dynamic)
4686 char *p = strchr (name, ELF_VER_CHR);
4687 if (p != NULL && p[1] != ELF_VER_CHR)
4689 /* Queue non-default versions so that .symver x, x@FOO
4690 aliases can be checked. */
4693 amt = ((isymend - isym + 1)
4694 * sizeof (struct elf_link_hash_entry *));
4696 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4698 goto error_free_vers;
4700 nondeflt_vers[nondeflt_vers_cnt++] = h;
4704 if (dynsym && h->dynindx == -1)
4706 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4707 goto error_free_vers;
4708 if (h->u.weakdef != NULL
4710 && h->u.weakdef->dynindx == -1)
4712 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4713 goto error_free_vers;
4716 else if (h->dynindx != -1)
4717 /* If the symbol already has a dynamic index, but
4718 visibility says it should not be visible, turn it into
4720 switch (ELF_ST_VISIBILITY (h->other))
4724 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4729 /* Don't add DT_NEEDED for references from the dummy bfd nor
4730 for unmatched symbol. */
4735 && h->ref_regular_nonweak
4737 || (old_bfd->flags & BFD_PLUGIN) == 0))
4738 || (h->ref_dynamic_nonweak
4739 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4740 && !on_needed_list (elf_dt_name (abfd),
4741 htab->needed, NULL))))
4744 const char *soname = elf_dt_name (abfd);
4746 info->callbacks->minfo ("%!", soname, old_bfd,
4747 h->root.root.string);
4749 /* A symbol from a library loaded via DT_NEEDED of some
4750 other library is referenced by a regular object.
4751 Add a DT_NEEDED entry for it. Issue an error if
4752 --no-add-needed is used and the reference was not
4755 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4757 (*_bfd_error_handler)
4758 (_("%B: undefined reference to symbol '%s'"),
4760 bfd_set_error (bfd_error_missing_dso);
4761 goto error_free_vers;
4764 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4765 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4768 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4770 goto error_free_vers;
4772 BFD_ASSERT (ret == 0);
4777 if (extversym != NULL)
4783 if (isymbuf != NULL)
4789 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4793 /* Restore the symbol table. */
4794 old_ent = (char *) old_tab + tabsize;
4795 memset (elf_sym_hashes (abfd), 0,
4796 extsymcount * sizeof (struct elf_link_hash_entry *));
4797 htab->root.table.table = old_table;
4798 htab->root.table.size = old_size;
4799 htab->root.table.count = old_count;
4800 memcpy (htab->root.table.table, old_tab, tabsize);
4801 htab->root.undefs = old_undefs;
4802 htab->root.undefs_tail = old_undefs_tail;
4803 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4806 for (i = 0; i < htab->root.table.size; i++)
4808 struct bfd_hash_entry *p;
4809 struct elf_link_hash_entry *h;
4811 unsigned int alignment_power;
4813 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4815 h = (struct elf_link_hash_entry *) p;
4816 if (h->root.type == bfd_link_hash_warning)
4817 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4819 /* Preserve the maximum alignment and size for common
4820 symbols even if this dynamic lib isn't on DT_NEEDED
4821 since it can still be loaded at run time by another
4823 if (h->root.type == bfd_link_hash_common)
4825 size = h->root.u.c.size;
4826 alignment_power = h->root.u.c.p->alignment_power;
4831 alignment_power = 0;
4833 memcpy (p, old_ent, htab->root.table.entsize);
4834 old_ent = (char *) old_ent + htab->root.table.entsize;
4835 h = (struct elf_link_hash_entry *) p;
4836 if (h->root.type == bfd_link_hash_warning)
4838 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4839 old_ent = (char *) old_ent + htab->root.table.entsize;
4840 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4842 if (h->root.type == bfd_link_hash_common)
4844 if (size > h->root.u.c.size)
4845 h->root.u.c.size = size;
4846 if (alignment_power > h->root.u.c.p->alignment_power)
4847 h->root.u.c.p->alignment_power = alignment_power;
4852 /* Make a special call to the linker "notice" function to
4853 tell it that symbols added for crefs may need to be removed. */
4854 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4855 goto error_free_vers;
4858 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4860 if (nondeflt_vers != NULL)
4861 free (nondeflt_vers);
4865 if (old_tab != NULL)
4867 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4868 goto error_free_vers;
4873 /* Now that all the symbols from this input file are created, if
4874 not performing a relocatable link, handle .symver foo, foo@BAR
4875 such that any relocs against foo become foo@BAR. */
4876 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4880 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4882 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4883 char *shortname, *p;
4885 p = strchr (h->root.root.string, ELF_VER_CHR);
4887 || (h->root.type != bfd_link_hash_defined
4888 && h->root.type != bfd_link_hash_defweak))
4891 amt = p - h->root.root.string;
4892 shortname = (char *) bfd_malloc (amt + 1);
4894 goto error_free_vers;
4895 memcpy (shortname, h->root.root.string, amt);
4896 shortname[amt] = '\0';
4898 hi = (struct elf_link_hash_entry *)
4899 bfd_link_hash_lookup (&htab->root, shortname,
4900 FALSE, FALSE, FALSE);
4902 && hi->root.type == h->root.type
4903 && hi->root.u.def.value == h->root.u.def.value
4904 && hi->root.u.def.section == h->root.u.def.section)
4906 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4907 hi->root.type = bfd_link_hash_indirect;
4908 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4909 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4910 sym_hash = elf_sym_hashes (abfd);
4912 for (symidx = 0; symidx < extsymcount; ++symidx)
4913 if (sym_hash[symidx] == hi)
4915 sym_hash[symidx] = h;
4921 free (nondeflt_vers);
4922 nondeflt_vers = NULL;
4925 /* Now set the weakdefs field correctly for all the weak defined
4926 symbols we found. The only way to do this is to search all the
4927 symbols. Since we only need the information for non functions in
4928 dynamic objects, that's the only time we actually put anything on
4929 the list WEAKS. We need this information so that if a regular
4930 object refers to a symbol defined weakly in a dynamic object, the
4931 real symbol in the dynamic object is also put in the dynamic
4932 symbols; we also must arrange for both symbols to point to the
4933 same memory location. We could handle the general case of symbol
4934 aliasing, but a general symbol alias can only be generated in
4935 assembler code, handling it correctly would be very time
4936 consuming, and other ELF linkers don't handle general aliasing
4940 struct elf_link_hash_entry **hpp;
4941 struct elf_link_hash_entry **hppend;
4942 struct elf_link_hash_entry **sorted_sym_hash;
4943 struct elf_link_hash_entry *h;
4946 /* Since we have to search the whole symbol list for each weak
4947 defined symbol, search time for N weak defined symbols will be
4948 O(N^2). Binary search will cut it down to O(NlogN). */
4950 amt *= sizeof (struct elf_link_hash_entry *);
4951 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4952 if (sorted_sym_hash == NULL)
4954 sym_hash = sorted_sym_hash;
4955 hpp = elf_sym_hashes (abfd);
4956 hppend = hpp + extsymcount;
4958 for (; hpp < hppend; hpp++)
4962 && h->root.type == bfd_link_hash_defined
4963 && !bed->is_function_type (h->type))
4971 qsort (sorted_sym_hash, sym_count,
4972 sizeof (struct elf_link_hash_entry *),
4975 while (weaks != NULL)
4977 struct elf_link_hash_entry *hlook;
4980 size_t i, j, idx = 0;
4983 weaks = hlook->u.weakdef;
4984 hlook->u.weakdef = NULL;
4986 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4987 || hlook->root.type == bfd_link_hash_defweak
4988 || hlook->root.type == bfd_link_hash_common
4989 || hlook->root.type == bfd_link_hash_indirect);
4990 slook = hlook->root.u.def.section;
4991 vlook = hlook->root.u.def.value;
4997 bfd_signed_vma vdiff;
4999 h = sorted_sym_hash[idx];
5000 vdiff = vlook - h->root.u.def.value;
5007 int sdiff = slook->id - h->root.u.def.section->id;
5017 /* We didn't find a value/section match. */
5021 /* With multiple aliases, or when the weak symbol is already
5022 strongly defined, we have multiple matching symbols and
5023 the binary search above may land on any of them. Step
5024 one past the matching symbol(s). */
5027 h = sorted_sym_hash[idx];
5028 if (h->root.u.def.section != slook
5029 || h->root.u.def.value != vlook)
5033 /* Now look back over the aliases. Since we sorted by size
5034 as well as value and section, we'll choose the one with
5035 the largest size. */
5038 h = sorted_sym_hash[idx];
5040 /* Stop if value or section doesn't match. */
5041 if (h->root.u.def.section != slook
5042 || h->root.u.def.value != vlook)
5044 else if (h != hlook)
5046 hlook->u.weakdef = h;
5048 /* If the weak definition is in the list of dynamic
5049 symbols, make sure the real definition is put
5051 if (hlook->dynindx != -1 && h->dynindx == -1)
5053 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5056 free (sorted_sym_hash);
5061 /* If the real definition is in the list of dynamic
5062 symbols, make sure the weak definition is put
5063 there as well. If we don't do this, then the
5064 dynamic loader might not merge the entries for the
5065 real definition and the weak definition. */
5066 if (h->dynindx != -1 && hlook->dynindx == -1)
5068 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5069 goto err_free_sym_hash;
5076 free (sorted_sym_hash);
5079 if (bed->check_directives
5080 && !(*bed->check_directives) (abfd, info))
5083 if (!info->check_relocs_after_open_input
5084 && !_bfd_elf_link_check_relocs (abfd, info))
5087 /* If this is a non-traditional link, try to optimize the handling
5088 of the .stab/.stabstr sections. */
5090 && ! info->traditional_format
5091 && is_elf_hash_table (htab)
5092 && (info->strip != strip_all && info->strip != strip_debugger))
5096 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5097 if (stabstr != NULL)
5099 bfd_size_type string_offset = 0;
5102 for (stab = abfd->sections; stab; stab = stab->next)
5103 if (CONST_STRNEQ (stab->name, ".stab")
5104 && (!stab->name[5] ||
5105 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5106 && (stab->flags & SEC_MERGE) == 0
5107 && !bfd_is_abs_section (stab->output_section))
5109 struct bfd_elf_section_data *secdata;
5111 secdata = elf_section_data (stab);
5112 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5113 stabstr, &secdata->sec_info,
5116 if (secdata->sec_info)
5117 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5122 if (is_elf_hash_table (htab) && add_needed)
5124 /* Add this bfd to the loaded list. */
5125 struct elf_link_loaded_list *n;
5127 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5131 n->next = htab->loaded;
5138 if (old_tab != NULL)
5140 if (old_strtab != NULL)
5142 if (nondeflt_vers != NULL)
5143 free (nondeflt_vers);
5144 if (extversym != NULL)
5147 if (isymbuf != NULL)
5153 /* Return the linker hash table entry of a symbol that might be
5154 satisfied by an archive symbol. Return -1 on error. */
5156 struct elf_link_hash_entry *
5157 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5158 struct bfd_link_info *info,
5161 struct elf_link_hash_entry *h;
5165 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5169 /* If this is a default version (the name contains @@), look up the
5170 symbol again with only one `@' as well as without the version.
5171 The effect is that references to the symbol with and without the
5172 version will be matched by the default symbol in the archive. */
5174 p = strchr (name, ELF_VER_CHR);
5175 if (p == NULL || p[1] != ELF_VER_CHR)
5178 /* First check with only one `@'. */
5179 len = strlen (name);
5180 copy = (char *) bfd_alloc (abfd, len);
5182 return (struct elf_link_hash_entry *) 0 - 1;
5184 first = p - name + 1;
5185 memcpy (copy, name, first);
5186 memcpy (copy + first, name + first + 1, len - first);
5188 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5191 /* We also need to check references to the symbol without the
5193 copy[first - 1] = '\0';
5194 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5195 FALSE, FALSE, TRUE);
5198 bfd_release (abfd, copy);
5202 /* Add symbols from an ELF archive file to the linker hash table. We
5203 don't use _bfd_generic_link_add_archive_symbols because we need to
5204 handle versioned symbols.
5206 Fortunately, ELF archive handling is simpler than that done by
5207 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5208 oddities. In ELF, if we find a symbol in the archive map, and the
5209 symbol is currently undefined, we know that we must pull in that
5212 Unfortunately, we do have to make multiple passes over the symbol
5213 table until nothing further is resolved. */
5216 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5219 unsigned char *included = NULL;
5223 const struct elf_backend_data *bed;
5224 struct elf_link_hash_entry * (*archive_symbol_lookup)
5225 (bfd *, struct bfd_link_info *, const char *);
5227 if (! bfd_has_map (abfd))
5229 /* An empty archive is a special case. */
5230 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5232 bfd_set_error (bfd_error_no_armap);
5236 /* Keep track of all symbols we know to be already defined, and all
5237 files we know to be already included. This is to speed up the
5238 second and subsequent passes. */
5239 c = bfd_ardata (abfd)->symdef_count;
5243 amt *= sizeof (*included);
5244 included = (unsigned char *) bfd_zmalloc (amt);
5245 if (included == NULL)
5248 symdefs = bfd_ardata (abfd)->symdefs;
5249 bed = get_elf_backend_data (abfd);
5250 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5263 symdefend = symdef + c;
5264 for (i = 0; symdef < symdefend; symdef++, i++)
5266 struct elf_link_hash_entry *h;
5268 struct bfd_link_hash_entry *undefs_tail;
5273 if (symdef->file_offset == last)
5279 h = archive_symbol_lookup (abfd, info, symdef->name);
5280 if (h == (struct elf_link_hash_entry *) 0 - 1)
5286 if (h->root.type == bfd_link_hash_common)
5288 /* We currently have a common symbol. The archive map contains
5289 a reference to this symbol, so we may want to include it. We
5290 only want to include it however, if this archive element
5291 contains a definition of the symbol, not just another common
5294 Unfortunately some archivers (including GNU ar) will put
5295 declarations of common symbols into their archive maps, as
5296 well as real definitions, so we cannot just go by the archive
5297 map alone. Instead we must read in the element's symbol
5298 table and check that to see what kind of symbol definition
5300 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5303 else if (h->root.type != bfd_link_hash_undefined)
5305 if (h->root.type != bfd_link_hash_undefweak)
5306 /* Symbol must be defined. Don't check it again. */
5311 /* We need to include this archive member. */
5312 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5313 if (element == NULL)
5316 if (! bfd_check_format (element, bfd_object))
5319 undefs_tail = info->hash->undefs_tail;
5321 if (!(*info->callbacks
5322 ->add_archive_element) (info, element, symdef->name, &element))
5324 if (!bfd_link_add_symbols (element, info))
5327 /* If there are any new undefined symbols, we need to make
5328 another pass through the archive in order to see whether
5329 they can be defined. FIXME: This isn't perfect, because
5330 common symbols wind up on undefs_tail and because an
5331 undefined symbol which is defined later on in this pass
5332 does not require another pass. This isn't a bug, but it
5333 does make the code less efficient than it could be. */
5334 if (undefs_tail != info->hash->undefs_tail)
5337 /* Look backward to mark all symbols from this object file
5338 which we have already seen in this pass. */
5342 included[mark] = TRUE;
5347 while (symdefs[mark].file_offset == symdef->file_offset);
5349 /* We mark subsequent symbols from this object file as we go
5350 on through the loop. */
5351 last = symdef->file_offset;
5361 if (included != NULL)
5366 /* Given an ELF BFD, add symbols to the global hash table as
5370 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5372 switch (bfd_get_format (abfd))
5375 return elf_link_add_object_symbols (abfd, info);
5377 return elf_link_add_archive_symbols (abfd, info);
5379 bfd_set_error (bfd_error_wrong_format);
5384 struct hash_codes_info
5386 unsigned long *hashcodes;
5390 /* This function will be called though elf_link_hash_traverse to store
5391 all hash value of the exported symbols in an array. */
5394 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5396 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5401 /* Ignore indirect symbols. These are added by the versioning code. */
5402 if (h->dynindx == -1)
5405 name = h->root.root.string;
5406 if (h->versioned >= versioned)
5408 char *p = strchr (name, ELF_VER_CHR);
5411 alc = (char *) bfd_malloc (p - name + 1);
5417 memcpy (alc, name, p - name);
5418 alc[p - name] = '\0';
5423 /* Compute the hash value. */
5424 ha = bfd_elf_hash (name);
5426 /* Store the found hash value in the array given as the argument. */
5427 *(inf->hashcodes)++ = ha;
5429 /* And store it in the struct so that we can put it in the hash table
5431 h->u.elf_hash_value = ha;
5439 struct collect_gnu_hash_codes
5442 const struct elf_backend_data *bed;
5443 unsigned long int nsyms;
5444 unsigned long int maskbits;
5445 unsigned long int *hashcodes;
5446 unsigned long int *hashval;
5447 unsigned long int *indx;
5448 unsigned long int *counts;
5451 long int min_dynindx;
5452 unsigned long int bucketcount;
5453 unsigned long int symindx;
5454 long int local_indx;
5455 long int shift1, shift2;
5456 unsigned long int mask;
5460 /* This function will be called though elf_link_hash_traverse to store
5461 all hash value of the exported symbols in an array. */
5464 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5466 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5471 /* Ignore indirect symbols. These are added by the versioning code. */
5472 if (h->dynindx == -1)
5475 /* Ignore also local symbols and undefined symbols. */
5476 if (! (*s->bed->elf_hash_symbol) (h))
5479 name = h->root.root.string;
5480 if (h->versioned >= versioned)
5482 char *p = strchr (name, ELF_VER_CHR);
5485 alc = (char *) bfd_malloc (p - name + 1);
5491 memcpy (alc, name, p - name);
5492 alc[p - name] = '\0';
5497 /* Compute the hash value. */
5498 ha = bfd_elf_gnu_hash (name);
5500 /* Store the found hash value in the array for compute_bucket_count,
5501 and also for .dynsym reordering purposes. */
5502 s->hashcodes[s->nsyms] = ha;
5503 s->hashval[h->dynindx] = ha;
5505 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5506 s->min_dynindx = h->dynindx;
5514 /* This function will be called though elf_link_hash_traverse to do
5515 final dynaminc symbol renumbering. */
5518 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5520 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5521 unsigned long int bucket;
5522 unsigned long int val;
5524 /* Ignore indirect symbols. */
5525 if (h->dynindx == -1)
5528 /* Ignore also local symbols and undefined symbols. */
5529 if (! (*s->bed->elf_hash_symbol) (h))
5531 if (h->dynindx >= s->min_dynindx)
5532 h->dynindx = s->local_indx++;
5536 bucket = s->hashval[h->dynindx] % s->bucketcount;
5537 val = (s->hashval[h->dynindx] >> s->shift1)
5538 & ((s->maskbits >> s->shift1) - 1);
5539 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5541 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5542 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5543 if (s->counts[bucket] == 1)
5544 /* Last element terminates the chain. */
5546 bfd_put_32 (s->output_bfd, val,
5547 s->contents + (s->indx[bucket] - s->symindx) * 4);
5548 --s->counts[bucket];
5549 h->dynindx = s->indx[bucket]++;
5553 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5556 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5558 return !(h->forced_local
5559 || h->root.type == bfd_link_hash_undefined
5560 || h->root.type == bfd_link_hash_undefweak
5561 || ((h->root.type == bfd_link_hash_defined
5562 || h->root.type == bfd_link_hash_defweak)
5563 && h->root.u.def.section->output_section == NULL));
5566 /* Array used to determine the number of hash table buckets to use
5567 based on the number of symbols there are. If there are fewer than
5568 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5569 fewer than 37 we use 17 buckets, and so forth. We never use more
5570 than 32771 buckets. */
5572 static const size_t elf_buckets[] =
5574 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5578 /* Compute bucket count for hashing table. We do not use a static set
5579 of possible tables sizes anymore. Instead we determine for all
5580 possible reasonable sizes of the table the outcome (i.e., the
5581 number of collisions etc) and choose the best solution. The
5582 weighting functions are not too simple to allow the table to grow
5583 without bounds. Instead one of the weighting factors is the size.
5584 Therefore the result is always a good payoff between few collisions
5585 (= short chain lengths) and table size. */
5587 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5588 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5589 unsigned long int nsyms,
5592 size_t best_size = 0;
5593 unsigned long int i;
5595 /* We have a problem here. The following code to optimize the table
5596 size requires an integer type with more the 32 bits. If
5597 BFD_HOST_U_64_BIT is set we know about such a type. */
5598 #ifdef BFD_HOST_U_64_BIT
5603 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5604 bfd *dynobj = elf_hash_table (info)->dynobj;
5605 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5606 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5607 unsigned long int *counts;
5609 unsigned int no_improvement_count = 0;
5611 /* Possible optimization parameters: if we have NSYMS symbols we say
5612 that the hashing table must at least have NSYMS/4 and at most
5614 minsize = nsyms / 4;
5617 best_size = maxsize = nsyms * 2;
5622 if ((best_size & 31) == 0)
5626 /* Create array where we count the collisions in. We must use bfd_malloc
5627 since the size could be large. */
5629 amt *= sizeof (unsigned long int);
5630 counts = (unsigned long int *) bfd_malloc (amt);
5634 /* Compute the "optimal" size for the hash table. The criteria is a
5635 minimal chain length. The minor criteria is (of course) the size
5637 for (i = minsize; i < maxsize; ++i)
5639 /* Walk through the array of hashcodes and count the collisions. */
5640 BFD_HOST_U_64_BIT max;
5641 unsigned long int j;
5642 unsigned long int fact;
5644 if (gnu_hash && (i & 31) == 0)
5647 memset (counts, '\0', i * sizeof (unsigned long int));
5649 /* Determine how often each hash bucket is used. */
5650 for (j = 0; j < nsyms; ++j)
5651 ++counts[hashcodes[j] % i];
5653 /* For the weight function we need some information about the
5654 pagesize on the target. This is information need not be 100%
5655 accurate. Since this information is not available (so far) we
5656 define it here to a reasonable default value. If it is crucial
5657 to have a better value some day simply define this value. */
5658 # ifndef BFD_TARGET_PAGESIZE
5659 # define BFD_TARGET_PAGESIZE (4096)
5662 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5664 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5667 /* Variant 1: optimize for short chains. We add the squares
5668 of all the chain lengths (which favors many small chain
5669 over a few long chains). */
5670 for (j = 0; j < i; ++j)
5671 max += counts[j] * counts[j];
5673 /* This adds penalties for the overall size of the table. */
5674 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5677 /* Variant 2: Optimize a lot more for small table. Here we
5678 also add squares of the size but we also add penalties for
5679 empty slots (the +1 term). */
5680 for (j = 0; j < i; ++j)
5681 max += (1 + counts[j]) * (1 + counts[j]);
5683 /* The overall size of the table is considered, but not as
5684 strong as in variant 1, where it is squared. */
5685 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5689 /* Compare with current best results. */
5690 if (max < best_chlen)
5694 no_improvement_count = 0;
5696 /* PR 11843: Avoid futile long searches for the best bucket size
5697 when there are a large number of symbols. */
5698 else if (++no_improvement_count == 100)
5705 #endif /* defined (BFD_HOST_U_64_BIT) */
5707 /* This is the fallback solution if no 64bit type is available or if we
5708 are not supposed to spend much time on optimizations. We select the
5709 bucket count using a fixed set of numbers. */
5710 for (i = 0; elf_buckets[i] != 0; i++)
5712 best_size = elf_buckets[i];
5713 if (nsyms < elf_buckets[i + 1])
5716 if (gnu_hash && best_size < 2)
5723 /* Size any SHT_GROUP section for ld -r. */
5726 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5730 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5731 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5732 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5737 /* Set a default stack segment size. The value in INFO wins. If it
5738 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5739 undefined it is initialized. */
5742 bfd_elf_stack_segment_size (bfd *output_bfd,
5743 struct bfd_link_info *info,
5744 const char *legacy_symbol,
5745 bfd_vma default_size)
5747 struct elf_link_hash_entry *h = NULL;
5749 /* Look for legacy symbol. */
5751 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5752 FALSE, FALSE, FALSE);
5753 if (h && (h->root.type == bfd_link_hash_defined
5754 || h->root.type == bfd_link_hash_defweak)
5756 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5758 /* The symbol has no type if specified on the command line. */
5759 h->type = STT_OBJECT;
5760 if (info->stacksize)
5761 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5762 output_bfd, legacy_symbol);
5763 else if (h->root.u.def.section != bfd_abs_section_ptr)
5764 (*_bfd_error_handler) (_("%B: %s not absolute"),
5765 output_bfd, legacy_symbol);
5767 info->stacksize = h->root.u.def.value;
5770 if (!info->stacksize)
5771 /* If the user didn't set a size, or explicitly inhibit the
5772 size, set it now. */
5773 info->stacksize = default_size;
5775 /* Provide the legacy symbol, if it is referenced. */
5776 if (h && (h->root.type == bfd_link_hash_undefined
5777 || h->root.type == bfd_link_hash_undefweak))
5779 struct bfd_link_hash_entry *bh = NULL;
5781 if (!(_bfd_generic_link_add_one_symbol
5782 (info, output_bfd, legacy_symbol,
5783 BSF_GLOBAL, bfd_abs_section_ptr,
5784 info->stacksize >= 0 ? info->stacksize : 0,
5785 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5788 h = (struct elf_link_hash_entry *) bh;
5790 h->type = STT_OBJECT;
5796 /* Set up the sizes and contents of the ELF dynamic sections. This is
5797 called by the ELF linker emulation before_allocation routine. We
5798 must set the sizes of the sections before the linker sets the
5799 addresses of the various sections. */
5802 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5805 const char *filter_shlib,
5807 const char *depaudit,
5808 const char * const *auxiliary_filters,
5809 struct bfd_link_info *info,
5810 asection **sinterpptr)
5814 const struct elf_backend_data *bed;
5815 struct elf_info_failed asvinfo;
5819 soname_indx = (size_t) -1;
5821 if (!is_elf_hash_table (info->hash))
5824 bed = get_elf_backend_data (output_bfd);
5826 /* Any syms created from now on start with -1 in
5827 got.refcount/offset and plt.refcount/offset. */
5828 elf_hash_table (info)->init_got_refcount
5829 = elf_hash_table (info)->init_got_offset;
5830 elf_hash_table (info)->init_plt_refcount
5831 = elf_hash_table (info)->init_plt_offset;
5833 if (bfd_link_relocatable (info)
5834 && !_bfd_elf_size_group_sections (info))
5837 /* The backend may have to create some sections regardless of whether
5838 we're dynamic or not. */
5839 if (bed->elf_backend_always_size_sections
5840 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5843 /* Determine any GNU_STACK segment requirements, after the backend
5844 has had a chance to set a default segment size. */
5845 if (info->execstack)
5846 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5847 else if (info->noexecstack)
5848 elf_stack_flags (output_bfd) = PF_R | PF_W;
5852 asection *notesec = NULL;
5855 for (inputobj = info->input_bfds;
5857 inputobj = inputobj->link.next)
5862 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5864 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5867 if (s->flags & SEC_CODE)
5871 else if (bed->default_execstack)
5874 if (notesec || info->stacksize > 0)
5875 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5876 if (notesec && exec && bfd_link_relocatable (info)
5877 && notesec->output_section != bfd_abs_section_ptr)
5878 notesec->output_section->flags |= SEC_CODE;
5881 dynobj = elf_hash_table (info)->dynobj;
5883 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5885 struct elf_info_failed eif;
5886 struct elf_link_hash_entry *h;
5888 struct bfd_elf_version_tree *t;
5889 struct bfd_elf_version_expr *d;
5891 bfd_boolean all_defined;
5893 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5894 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5898 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5900 if (soname_indx == (size_t) -1
5901 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5907 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5909 info->flags |= DF_SYMBOLIC;
5917 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5919 if (indx == (size_t) -1)
5922 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5923 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5927 if (filter_shlib != NULL)
5931 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5932 filter_shlib, TRUE);
5933 if (indx == (size_t) -1
5934 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5938 if (auxiliary_filters != NULL)
5940 const char * const *p;
5942 for (p = auxiliary_filters; *p != NULL; p++)
5946 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5948 if (indx == (size_t) -1
5949 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5958 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5960 if (indx == (size_t) -1
5961 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5965 if (depaudit != NULL)
5969 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5971 if (indx == (size_t) -1
5972 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5979 /* If we are supposed to export all symbols into the dynamic symbol
5980 table (this is not the normal case), then do so. */
5981 if (info->export_dynamic
5982 || (bfd_link_executable (info) && info->dynamic))
5984 elf_link_hash_traverse (elf_hash_table (info),
5985 _bfd_elf_export_symbol,
5991 /* Make all global versions with definition. */
5992 for (t = info->version_info; t != NULL; t = t->next)
5993 for (d = t->globals.list; d != NULL; d = d->next)
5994 if (!d->symver && d->literal)
5996 const char *verstr, *name;
5997 size_t namelen, verlen, newlen;
5998 char *newname, *p, leading_char;
5999 struct elf_link_hash_entry *newh;
6001 leading_char = bfd_get_symbol_leading_char (output_bfd);
6003 namelen = strlen (name) + (leading_char != '\0');
6005 verlen = strlen (verstr);
6006 newlen = namelen + verlen + 3;
6008 newname = (char *) bfd_malloc (newlen);
6009 if (newname == NULL)
6011 newname[0] = leading_char;
6012 memcpy (newname + (leading_char != '\0'), name, namelen);
6014 /* Check the hidden versioned definition. */
6015 p = newname + namelen;
6017 memcpy (p, verstr, verlen + 1);
6018 newh = elf_link_hash_lookup (elf_hash_table (info),
6019 newname, FALSE, FALSE,
6022 || (newh->root.type != bfd_link_hash_defined
6023 && newh->root.type != bfd_link_hash_defweak))
6025 /* Check the default versioned definition. */
6027 memcpy (p, verstr, verlen + 1);
6028 newh = elf_link_hash_lookup (elf_hash_table (info),
6029 newname, FALSE, FALSE,
6034 /* Mark this version if there is a definition and it is
6035 not defined in a shared object. */
6037 && !newh->def_dynamic
6038 && (newh->root.type == bfd_link_hash_defined
6039 || newh->root.type == bfd_link_hash_defweak))
6043 /* Attach all the symbols to their version information. */
6044 asvinfo.info = info;
6045 asvinfo.failed = FALSE;
6047 elf_link_hash_traverse (elf_hash_table (info),
6048 _bfd_elf_link_assign_sym_version,
6053 if (!info->allow_undefined_version)
6055 /* Check if all global versions have a definition. */
6057 for (t = info->version_info; t != NULL; t = t->next)
6058 for (d = t->globals.list; d != NULL; d = d->next)
6059 if (d->literal && !d->symver && !d->script)
6061 (*_bfd_error_handler)
6062 (_("%s: undefined version: %s"),
6063 d->pattern, t->name);
6064 all_defined = FALSE;
6069 bfd_set_error (bfd_error_bad_value);
6074 /* Find all symbols which were defined in a dynamic object and make
6075 the backend pick a reasonable value for them. */
6076 elf_link_hash_traverse (elf_hash_table (info),
6077 _bfd_elf_adjust_dynamic_symbol,
6082 /* Add some entries to the .dynamic section. We fill in some of the
6083 values later, in bfd_elf_final_link, but we must add the entries
6084 now so that we know the final size of the .dynamic section. */
6086 /* If there are initialization and/or finalization functions to
6087 call then add the corresponding DT_INIT/DT_FINI entries. */
6088 h = (info->init_function
6089 ? elf_link_hash_lookup (elf_hash_table (info),
6090 info->init_function, FALSE,
6097 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6100 h = (info->fini_function
6101 ? elf_link_hash_lookup (elf_hash_table (info),
6102 info->fini_function, FALSE,
6109 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6113 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6114 if (s != NULL && s->linker_has_input)
6116 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6117 if (! bfd_link_executable (info))
6122 for (sub = info->input_bfds; sub != NULL;
6123 sub = sub->link.next)
6124 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6125 for (o = sub->sections; o != NULL; o = o->next)
6126 if (elf_section_data (o)->this_hdr.sh_type
6127 == SHT_PREINIT_ARRAY)
6129 (*_bfd_error_handler)
6130 (_("%B: .preinit_array section is not allowed in DSO"),
6135 bfd_set_error (bfd_error_nonrepresentable_section);
6139 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6140 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6143 s = bfd_get_section_by_name (output_bfd, ".init_array");
6144 if (s != NULL && s->linker_has_input)
6146 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6147 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6150 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6151 if (s != NULL && s->linker_has_input)
6153 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6154 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6158 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6159 /* If .dynstr is excluded from the link, we don't want any of
6160 these tags. Strictly, we should be checking each section
6161 individually; This quick check covers for the case where
6162 someone does a /DISCARD/ : { *(*) }. */
6163 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6165 bfd_size_type strsize;
6167 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6168 if ((info->emit_hash
6169 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6170 || (info->emit_gnu_hash
6171 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6172 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6173 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6174 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6175 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6176 bed->s->sizeof_sym))
6181 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6184 /* The backend must work out the sizes of all the other dynamic
6187 && bed->elf_backend_size_dynamic_sections != NULL
6188 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6191 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6193 unsigned long section_sym_count;
6194 struct bfd_elf_version_tree *verdefs;
6197 /* Set up the version definition section. */
6198 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6199 BFD_ASSERT (s != NULL);
6201 /* We may have created additional version definitions if we are
6202 just linking a regular application. */
6203 verdefs = info->version_info;
6205 /* Skip anonymous version tag. */
6206 if (verdefs != NULL && verdefs->vernum == 0)
6207 verdefs = verdefs->next;
6209 if (verdefs == NULL && !info->create_default_symver)
6210 s->flags |= SEC_EXCLUDE;
6215 struct bfd_elf_version_tree *t;
6217 Elf_Internal_Verdef def;
6218 Elf_Internal_Verdaux defaux;
6219 struct bfd_link_hash_entry *bh;
6220 struct elf_link_hash_entry *h;
6226 /* Make space for the base version. */
6227 size += sizeof (Elf_External_Verdef);
6228 size += sizeof (Elf_External_Verdaux);
6231 /* Make space for the default version. */
6232 if (info->create_default_symver)
6234 size += sizeof (Elf_External_Verdef);
6238 for (t = verdefs; t != NULL; t = t->next)
6240 struct bfd_elf_version_deps *n;
6242 /* Don't emit base version twice. */
6246 size += sizeof (Elf_External_Verdef);
6247 size += sizeof (Elf_External_Verdaux);
6250 for (n = t->deps; n != NULL; n = n->next)
6251 size += sizeof (Elf_External_Verdaux);
6255 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6256 if (s->contents == NULL && s->size != 0)
6259 /* Fill in the version definition section. */
6263 def.vd_version = VER_DEF_CURRENT;
6264 def.vd_flags = VER_FLG_BASE;
6267 if (info->create_default_symver)
6269 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6270 def.vd_next = sizeof (Elf_External_Verdef);
6274 def.vd_aux = sizeof (Elf_External_Verdef);
6275 def.vd_next = (sizeof (Elf_External_Verdef)
6276 + sizeof (Elf_External_Verdaux));
6279 if (soname_indx != (size_t) -1)
6281 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6283 def.vd_hash = bfd_elf_hash (soname);
6284 defaux.vda_name = soname_indx;
6291 name = lbasename (output_bfd->filename);
6292 def.vd_hash = bfd_elf_hash (name);
6293 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6295 if (indx == (size_t) -1)
6297 defaux.vda_name = indx;
6299 defaux.vda_next = 0;
6301 _bfd_elf_swap_verdef_out (output_bfd, &def,
6302 (Elf_External_Verdef *) p);
6303 p += sizeof (Elf_External_Verdef);
6304 if (info->create_default_symver)
6306 /* Add a symbol representing this version. */
6308 if (! (_bfd_generic_link_add_one_symbol
6309 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6311 get_elf_backend_data (dynobj)->collect, &bh)))
6313 h = (struct elf_link_hash_entry *) bh;
6316 h->type = STT_OBJECT;
6317 h->verinfo.vertree = NULL;
6319 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6322 /* Create a duplicate of the base version with the same
6323 aux block, but different flags. */
6326 def.vd_aux = sizeof (Elf_External_Verdef);
6328 def.vd_next = (sizeof (Elf_External_Verdef)
6329 + sizeof (Elf_External_Verdaux));
6332 _bfd_elf_swap_verdef_out (output_bfd, &def,
6333 (Elf_External_Verdef *) p);
6334 p += sizeof (Elf_External_Verdef);
6336 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6337 (Elf_External_Verdaux *) p);
6338 p += sizeof (Elf_External_Verdaux);
6340 for (t = verdefs; t != NULL; t = t->next)
6343 struct bfd_elf_version_deps *n;
6345 /* Don't emit the base version twice. */
6350 for (n = t->deps; n != NULL; n = n->next)
6353 /* Add a symbol representing this version. */
6355 if (! (_bfd_generic_link_add_one_symbol
6356 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6358 get_elf_backend_data (dynobj)->collect, &bh)))
6360 h = (struct elf_link_hash_entry *) bh;
6363 h->type = STT_OBJECT;
6364 h->verinfo.vertree = t;
6366 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6369 def.vd_version = VER_DEF_CURRENT;
6371 if (t->globals.list == NULL
6372 && t->locals.list == NULL
6374 def.vd_flags |= VER_FLG_WEAK;
6375 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6376 def.vd_cnt = cdeps + 1;
6377 def.vd_hash = bfd_elf_hash (t->name);
6378 def.vd_aux = sizeof (Elf_External_Verdef);
6381 /* If a basever node is next, it *must* be the last node in
6382 the chain, otherwise Verdef construction breaks. */
6383 if (t->next != NULL && t->next->vernum == 0)
6384 BFD_ASSERT (t->next->next == NULL);
6386 if (t->next != NULL && t->next->vernum != 0)
6387 def.vd_next = (sizeof (Elf_External_Verdef)
6388 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6390 _bfd_elf_swap_verdef_out (output_bfd, &def,
6391 (Elf_External_Verdef *) p);
6392 p += sizeof (Elf_External_Verdef);
6394 defaux.vda_name = h->dynstr_index;
6395 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6397 defaux.vda_next = 0;
6398 if (t->deps != NULL)
6399 defaux.vda_next = sizeof (Elf_External_Verdaux);
6400 t->name_indx = defaux.vda_name;
6402 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6403 (Elf_External_Verdaux *) p);
6404 p += sizeof (Elf_External_Verdaux);
6406 for (n = t->deps; n != NULL; n = n->next)
6408 if (n->version_needed == NULL)
6410 /* This can happen if there was an error in the
6412 defaux.vda_name = 0;
6416 defaux.vda_name = n->version_needed->name_indx;
6417 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6420 if (n->next == NULL)
6421 defaux.vda_next = 0;
6423 defaux.vda_next = sizeof (Elf_External_Verdaux);
6425 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6426 (Elf_External_Verdaux *) p);
6427 p += sizeof (Elf_External_Verdaux);
6431 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6432 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6435 elf_tdata (output_bfd)->cverdefs = cdefs;
6438 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6440 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6443 else if (info->flags & DF_BIND_NOW)
6445 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6451 if (bfd_link_executable (info))
6452 info->flags_1 &= ~ (DF_1_INITFIRST
6455 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6459 /* Work out the size of the version reference section. */
6461 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6462 BFD_ASSERT (s != NULL);
6464 struct elf_find_verdep_info sinfo;
6467 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6468 if (sinfo.vers == 0)
6470 sinfo.failed = FALSE;
6472 elf_link_hash_traverse (elf_hash_table (info),
6473 _bfd_elf_link_find_version_dependencies,
6478 if (elf_tdata (output_bfd)->verref == NULL)
6479 s->flags |= SEC_EXCLUDE;
6482 Elf_Internal_Verneed *t;
6487 /* Build the version dependency section. */
6490 for (t = elf_tdata (output_bfd)->verref;
6494 Elf_Internal_Vernaux *a;
6496 size += sizeof (Elf_External_Verneed);
6498 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6499 size += sizeof (Elf_External_Vernaux);
6503 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6504 if (s->contents == NULL)
6508 for (t = elf_tdata (output_bfd)->verref;
6513 Elf_Internal_Vernaux *a;
6517 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6520 t->vn_version = VER_NEED_CURRENT;
6522 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6523 elf_dt_name (t->vn_bfd) != NULL
6524 ? elf_dt_name (t->vn_bfd)
6525 : lbasename (t->vn_bfd->filename),
6527 if (indx == (size_t) -1)
6530 t->vn_aux = sizeof (Elf_External_Verneed);
6531 if (t->vn_nextref == NULL)
6534 t->vn_next = (sizeof (Elf_External_Verneed)
6535 + caux * sizeof (Elf_External_Vernaux));
6537 _bfd_elf_swap_verneed_out (output_bfd, t,
6538 (Elf_External_Verneed *) p);
6539 p += sizeof (Elf_External_Verneed);
6541 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6543 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6544 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6545 a->vna_nodename, FALSE);
6546 if (indx == (size_t) -1)
6549 if (a->vna_nextptr == NULL)
6552 a->vna_next = sizeof (Elf_External_Vernaux);
6554 _bfd_elf_swap_vernaux_out (output_bfd, a,
6555 (Elf_External_Vernaux *) p);
6556 p += sizeof (Elf_External_Vernaux);
6560 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6561 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6564 elf_tdata (output_bfd)->cverrefs = crefs;
6568 if ((elf_tdata (output_bfd)->cverrefs == 0
6569 && elf_tdata (output_bfd)->cverdefs == 0)
6570 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6571 §ion_sym_count) == 0)
6573 s = bfd_get_linker_section (dynobj, ".gnu.version");
6574 s->flags |= SEC_EXCLUDE;
6580 /* Find the first non-excluded output section. We'll use its
6581 section symbol for some emitted relocs. */
6583 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6587 for (s = output_bfd->sections; s != NULL; s = s->next)
6588 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6589 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6591 elf_hash_table (info)->text_index_section = s;
6596 /* Find two non-excluded output sections, one for code, one for data.
6597 We'll use their section symbols for some emitted relocs. */
6599 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6603 /* Data first, since setting text_index_section changes
6604 _bfd_elf_link_omit_section_dynsym. */
6605 for (s = output_bfd->sections; s != NULL; s = s->next)
6606 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6607 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6609 elf_hash_table (info)->data_index_section = s;
6613 for (s = output_bfd->sections; s != NULL; s = s->next)
6614 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6615 == (SEC_ALLOC | SEC_READONLY))
6616 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6618 elf_hash_table (info)->text_index_section = s;
6622 if (elf_hash_table (info)->text_index_section == NULL)
6623 elf_hash_table (info)->text_index_section
6624 = elf_hash_table (info)->data_index_section;
6628 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6630 const struct elf_backend_data *bed;
6632 if (!is_elf_hash_table (info->hash))
6635 bed = get_elf_backend_data (output_bfd);
6636 (*bed->elf_backend_init_index_section) (output_bfd, info);
6638 if (elf_hash_table (info)->dynamic_sections_created)
6642 bfd_size_type dynsymcount;
6643 unsigned long section_sym_count;
6644 unsigned int dtagcount;
6646 dynobj = elf_hash_table (info)->dynobj;
6648 /* Assign dynsym indicies. In a shared library we generate a
6649 section symbol for each output section, which come first.
6650 Next come all of the back-end allocated local dynamic syms,
6651 followed by the rest of the global symbols. */
6653 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6654 §ion_sym_count);
6656 /* Work out the size of the symbol version section. */
6657 s = bfd_get_linker_section (dynobj, ".gnu.version");
6658 BFD_ASSERT (s != NULL);
6659 if ((s->flags & SEC_EXCLUDE) == 0)
6661 s->size = dynsymcount * sizeof (Elf_External_Versym);
6662 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6663 if (s->contents == NULL)
6666 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6670 /* Set the size of the .dynsym and .hash sections. We counted
6671 the number of dynamic symbols in elf_link_add_object_symbols.
6672 We will build the contents of .dynsym and .hash when we build
6673 the final symbol table, because until then we do not know the
6674 correct value to give the symbols. We built the .dynstr
6675 section as we went along in elf_link_add_object_symbols. */
6676 s = elf_hash_table (info)->dynsym;
6677 BFD_ASSERT (s != NULL);
6678 s->size = dynsymcount * bed->s->sizeof_sym;
6680 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6681 if (s->contents == NULL)
6684 /* The first entry in .dynsym is a dummy symbol. Clear all the
6685 section syms, in case we don't output them all. */
6686 ++section_sym_count;
6687 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6689 elf_hash_table (info)->bucketcount = 0;
6691 /* Compute the size of the hashing table. As a side effect this
6692 computes the hash values for all the names we export. */
6693 if (info->emit_hash)
6695 unsigned long int *hashcodes;
6696 struct hash_codes_info hashinf;
6698 unsigned long int nsyms;
6700 size_t hash_entry_size;
6702 /* Compute the hash values for all exported symbols. At the same
6703 time store the values in an array so that we could use them for
6705 amt = dynsymcount * sizeof (unsigned long int);
6706 hashcodes = (unsigned long int *) bfd_malloc (amt);
6707 if (hashcodes == NULL)
6709 hashinf.hashcodes = hashcodes;
6710 hashinf.error = FALSE;
6712 /* Put all hash values in HASHCODES. */
6713 elf_link_hash_traverse (elf_hash_table (info),
6714 elf_collect_hash_codes, &hashinf);
6721 nsyms = hashinf.hashcodes - hashcodes;
6723 = compute_bucket_count (info, hashcodes, nsyms, 0);
6726 if (bucketcount == 0)
6729 elf_hash_table (info)->bucketcount = bucketcount;
6731 s = bfd_get_linker_section (dynobj, ".hash");
6732 BFD_ASSERT (s != NULL);
6733 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6734 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6735 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6736 if (s->contents == NULL)
6739 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6740 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6741 s->contents + hash_entry_size);
6744 if (info->emit_gnu_hash)
6747 unsigned char *contents;
6748 struct collect_gnu_hash_codes cinfo;
6752 memset (&cinfo, 0, sizeof (cinfo));
6754 /* Compute the hash values for all exported symbols. At the same
6755 time store the values in an array so that we could use them for
6757 amt = dynsymcount * 2 * sizeof (unsigned long int);
6758 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6759 if (cinfo.hashcodes == NULL)
6762 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6763 cinfo.min_dynindx = -1;
6764 cinfo.output_bfd = output_bfd;
6767 /* Put all hash values in HASHCODES. */
6768 elf_link_hash_traverse (elf_hash_table (info),
6769 elf_collect_gnu_hash_codes, &cinfo);
6772 free (cinfo.hashcodes);
6777 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6779 if (bucketcount == 0)
6781 free (cinfo.hashcodes);
6785 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6786 BFD_ASSERT (s != NULL);
6788 if (cinfo.nsyms == 0)
6790 /* Empty .gnu.hash section is special. */
6791 BFD_ASSERT (cinfo.min_dynindx == -1);
6792 free (cinfo.hashcodes);
6793 s->size = 5 * 4 + bed->s->arch_size / 8;
6794 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6795 if (contents == NULL)
6797 s->contents = contents;
6798 /* 1 empty bucket. */
6799 bfd_put_32 (output_bfd, 1, contents);
6800 /* SYMIDX above the special symbol 0. */
6801 bfd_put_32 (output_bfd, 1, contents + 4);
6802 /* Just one word for bitmask. */
6803 bfd_put_32 (output_bfd, 1, contents + 8);
6804 /* Only hash fn bloom filter. */
6805 bfd_put_32 (output_bfd, 0, contents + 12);
6806 /* No hashes are valid - empty bitmask. */
6807 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6808 /* No hashes in the only bucket. */
6809 bfd_put_32 (output_bfd, 0,
6810 contents + 16 + bed->s->arch_size / 8);
6814 unsigned long int maskwords, maskbitslog2, x;
6815 BFD_ASSERT (cinfo.min_dynindx != -1);
6819 while ((x >>= 1) != 0)
6821 if (maskbitslog2 < 3)
6823 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6824 maskbitslog2 = maskbitslog2 + 3;
6826 maskbitslog2 = maskbitslog2 + 2;
6827 if (bed->s->arch_size == 64)
6829 if (maskbitslog2 == 5)
6835 cinfo.mask = (1 << cinfo.shift1) - 1;
6836 cinfo.shift2 = maskbitslog2;
6837 cinfo.maskbits = 1 << maskbitslog2;
6838 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6839 amt = bucketcount * sizeof (unsigned long int) * 2;
6840 amt += maskwords * sizeof (bfd_vma);
6841 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6842 if (cinfo.bitmask == NULL)
6844 free (cinfo.hashcodes);
6848 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6849 cinfo.indx = cinfo.counts + bucketcount;
6850 cinfo.symindx = dynsymcount - cinfo.nsyms;
6851 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6853 /* Determine how often each hash bucket is used. */
6854 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6855 for (i = 0; i < cinfo.nsyms; ++i)
6856 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6858 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6859 if (cinfo.counts[i] != 0)
6861 cinfo.indx[i] = cnt;
6862 cnt += cinfo.counts[i];
6864 BFD_ASSERT (cnt == dynsymcount);
6865 cinfo.bucketcount = bucketcount;
6866 cinfo.local_indx = cinfo.min_dynindx;
6868 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6869 s->size += cinfo.maskbits / 8;
6870 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6871 if (contents == NULL)
6873 free (cinfo.bitmask);
6874 free (cinfo.hashcodes);
6878 s->contents = contents;
6879 bfd_put_32 (output_bfd, bucketcount, contents);
6880 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6881 bfd_put_32 (output_bfd, maskwords, contents + 8);
6882 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6883 contents += 16 + cinfo.maskbits / 8;
6885 for (i = 0; i < bucketcount; ++i)
6887 if (cinfo.counts[i] == 0)
6888 bfd_put_32 (output_bfd, 0, contents);
6890 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6894 cinfo.contents = contents;
6896 /* Renumber dynamic symbols, populate .gnu.hash section. */
6897 elf_link_hash_traverse (elf_hash_table (info),
6898 elf_renumber_gnu_hash_syms, &cinfo);
6900 contents = s->contents + 16;
6901 for (i = 0; i < maskwords; ++i)
6903 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6905 contents += bed->s->arch_size / 8;
6908 free (cinfo.bitmask);
6909 free (cinfo.hashcodes);
6913 s = bfd_get_linker_section (dynobj, ".dynstr");
6914 BFD_ASSERT (s != NULL);
6916 elf_finalize_dynstr (output_bfd, info);
6918 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6920 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6921 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6928 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6931 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6934 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6935 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6938 /* Finish SHF_MERGE section merging. */
6941 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
6946 if (!is_elf_hash_table (info->hash))
6949 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6950 if ((ibfd->flags & DYNAMIC) == 0
6951 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6952 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6953 == get_elf_backend_data (obfd)->s->elfclass))
6954 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6955 if ((sec->flags & SEC_MERGE) != 0
6956 && !bfd_is_abs_section (sec->output_section))
6958 struct bfd_elf_section_data *secdata;
6960 secdata = elf_section_data (sec);
6961 if (! _bfd_add_merge_section (obfd,
6962 &elf_hash_table (info)->merge_info,
6963 sec, &secdata->sec_info))
6965 else if (secdata->sec_info)
6966 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6969 if (elf_hash_table (info)->merge_info != NULL)
6970 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
6971 merge_sections_remove_hook);
6975 /* Create an entry in an ELF linker hash table. */
6977 struct bfd_hash_entry *
6978 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6979 struct bfd_hash_table *table,
6982 /* Allocate the structure if it has not already been allocated by a
6986 entry = (struct bfd_hash_entry *)
6987 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6992 /* Call the allocation method of the superclass. */
6993 entry = _bfd_link_hash_newfunc (entry, table, string);
6996 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6997 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6999 /* Set local fields. */
7002 ret->got = htab->init_got_refcount;
7003 ret->plt = htab->init_plt_refcount;
7004 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7005 - offsetof (struct elf_link_hash_entry, size)));
7006 /* Assume that we have been called by a non-ELF symbol reader.
7007 This flag is then reset by the code which reads an ELF input
7008 file. This ensures that a symbol created by a non-ELF symbol
7009 reader will have the flag set correctly. */
7016 /* Copy data from an indirect symbol to its direct symbol, hiding the
7017 old indirect symbol. Also used for copying flags to a weakdef. */
7020 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7021 struct elf_link_hash_entry *dir,
7022 struct elf_link_hash_entry *ind)
7024 struct elf_link_hash_table *htab;
7026 /* Copy down any references that we may have already seen to the
7027 symbol which just became indirect if DIR isn't a hidden versioned
7030 if (dir->versioned != versioned_hidden)
7032 dir->ref_dynamic |= ind->ref_dynamic;
7033 dir->ref_regular |= ind->ref_regular;
7034 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7035 dir->non_got_ref |= ind->non_got_ref;
7036 dir->needs_plt |= ind->needs_plt;
7037 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7040 if (ind->root.type != bfd_link_hash_indirect)
7043 /* Copy over the global and procedure linkage table refcount entries.
7044 These may have been already set up by a check_relocs routine. */
7045 htab = elf_hash_table (info);
7046 if (ind->got.refcount > htab->init_got_refcount.refcount)
7048 if (dir->got.refcount < 0)
7049 dir->got.refcount = 0;
7050 dir->got.refcount += ind->got.refcount;
7051 ind->got.refcount = htab->init_got_refcount.refcount;
7054 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7056 if (dir->plt.refcount < 0)
7057 dir->plt.refcount = 0;
7058 dir->plt.refcount += ind->plt.refcount;
7059 ind->plt.refcount = htab->init_plt_refcount.refcount;
7062 if (ind->dynindx != -1)
7064 if (dir->dynindx != -1)
7065 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7066 dir->dynindx = ind->dynindx;
7067 dir->dynstr_index = ind->dynstr_index;
7069 ind->dynstr_index = 0;
7074 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7075 struct elf_link_hash_entry *h,
7076 bfd_boolean force_local)
7078 /* STT_GNU_IFUNC symbol must go through PLT. */
7079 if (h->type != STT_GNU_IFUNC)
7081 h->plt = elf_hash_table (info)->init_plt_offset;
7086 h->forced_local = 1;
7087 if (h->dynindx != -1)
7090 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7096 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7100 _bfd_elf_link_hash_table_init
7101 (struct elf_link_hash_table *table,
7103 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7104 struct bfd_hash_table *,
7106 unsigned int entsize,
7107 enum elf_target_id target_id)
7110 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7112 table->init_got_refcount.refcount = can_refcount - 1;
7113 table->init_plt_refcount.refcount = can_refcount - 1;
7114 table->init_got_offset.offset = -(bfd_vma) 1;
7115 table->init_plt_offset.offset = -(bfd_vma) 1;
7116 /* The first dynamic symbol is a dummy. */
7117 table->dynsymcount = 1;
7119 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7121 table->root.type = bfd_link_elf_hash_table;
7122 table->hash_table_id = target_id;
7127 /* Create an ELF linker hash table. */
7129 struct bfd_link_hash_table *
7130 _bfd_elf_link_hash_table_create (bfd *abfd)
7132 struct elf_link_hash_table *ret;
7133 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7135 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7139 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7140 sizeof (struct elf_link_hash_entry),
7146 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7151 /* Destroy an ELF linker hash table. */
7154 _bfd_elf_link_hash_table_free (bfd *obfd)
7156 struct elf_link_hash_table *htab;
7158 htab = (struct elf_link_hash_table *) obfd->link.hash;
7159 if (htab->dynstr != NULL)
7160 _bfd_elf_strtab_free (htab->dynstr);
7161 _bfd_merge_sections_free (htab->merge_info);
7162 _bfd_generic_link_hash_table_free (obfd);
7165 /* This is a hook for the ELF emulation code in the generic linker to
7166 tell the backend linker what file name to use for the DT_NEEDED
7167 entry for a dynamic object. */
7170 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7172 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7173 && bfd_get_format (abfd) == bfd_object)
7174 elf_dt_name (abfd) = name;
7178 bfd_elf_get_dyn_lib_class (bfd *abfd)
7181 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7182 && bfd_get_format (abfd) == bfd_object)
7183 lib_class = elf_dyn_lib_class (abfd);
7190 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7192 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7193 && bfd_get_format (abfd) == bfd_object)
7194 elf_dyn_lib_class (abfd) = lib_class;
7197 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7198 the linker ELF emulation code. */
7200 struct bfd_link_needed_list *
7201 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7202 struct bfd_link_info *info)
7204 if (! is_elf_hash_table (info->hash))
7206 return elf_hash_table (info)->needed;
7209 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7210 hook for the linker ELF emulation code. */
7212 struct bfd_link_needed_list *
7213 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7214 struct bfd_link_info *info)
7216 if (! is_elf_hash_table (info->hash))
7218 return elf_hash_table (info)->runpath;
7221 /* Get the name actually used for a dynamic object for a link. This
7222 is the SONAME entry if there is one. Otherwise, it is the string
7223 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7226 bfd_elf_get_dt_soname (bfd *abfd)
7228 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7229 && bfd_get_format (abfd) == bfd_object)
7230 return elf_dt_name (abfd);
7234 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7235 the ELF linker emulation code. */
7238 bfd_elf_get_bfd_needed_list (bfd *abfd,
7239 struct bfd_link_needed_list **pneeded)
7242 bfd_byte *dynbuf = NULL;
7243 unsigned int elfsec;
7244 unsigned long shlink;
7245 bfd_byte *extdyn, *extdynend;
7247 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7251 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7252 || bfd_get_format (abfd) != bfd_object)
7255 s = bfd_get_section_by_name (abfd, ".dynamic");
7256 if (s == NULL || s->size == 0)
7259 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7262 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7263 if (elfsec == SHN_BAD)
7266 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7268 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7269 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7272 extdynend = extdyn + s->size;
7273 for (; extdyn < extdynend; extdyn += extdynsize)
7275 Elf_Internal_Dyn dyn;
7277 (*swap_dyn_in) (abfd, extdyn, &dyn);
7279 if (dyn.d_tag == DT_NULL)
7282 if (dyn.d_tag == DT_NEEDED)
7285 struct bfd_link_needed_list *l;
7286 unsigned int tagv = dyn.d_un.d_val;
7289 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7294 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7315 struct elf_symbuf_symbol
7317 unsigned long st_name; /* Symbol name, index in string tbl */
7318 unsigned char st_info; /* Type and binding attributes */
7319 unsigned char st_other; /* Visibilty, and target specific */
7322 struct elf_symbuf_head
7324 struct elf_symbuf_symbol *ssym;
7326 unsigned int st_shndx;
7333 Elf_Internal_Sym *isym;
7334 struct elf_symbuf_symbol *ssym;
7339 /* Sort references to symbols by ascending section number. */
7342 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7344 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7345 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7347 return s1->st_shndx - s2->st_shndx;
7351 elf_sym_name_compare (const void *arg1, const void *arg2)
7353 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7354 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7355 return strcmp (s1->name, s2->name);
7358 static struct elf_symbuf_head *
7359 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7361 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7362 struct elf_symbuf_symbol *ssym;
7363 struct elf_symbuf_head *ssymbuf, *ssymhead;
7364 size_t i, shndx_count, total_size;
7366 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7370 for (ind = indbuf, i = 0; i < symcount; i++)
7371 if (isymbuf[i].st_shndx != SHN_UNDEF)
7372 *ind++ = &isymbuf[i];
7375 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7376 elf_sort_elf_symbol);
7379 if (indbufend > indbuf)
7380 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7381 if (ind[0]->st_shndx != ind[1]->st_shndx)
7384 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7385 + (indbufend - indbuf) * sizeof (*ssym));
7386 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7387 if (ssymbuf == NULL)
7393 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7394 ssymbuf->ssym = NULL;
7395 ssymbuf->count = shndx_count;
7396 ssymbuf->st_shndx = 0;
7397 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7399 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7402 ssymhead->ssym = ssym;
7403 ssymhead->count = 0;
7404 ssymhead->st_shndx = (*ind)->st_shndx;
7406 ssym->st_name = (*ind)->st_name;
7407 ssym->st_info = (*ind)->st_info;
7408 ssym->st_other = (*ind)->st_other;
7411 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7412 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7419 /* Check if 2 sections define the same set of local and global
7423 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7424 struct bfd_link_info *info)
7427 const struct elf_backend_data *bed1, *bed2;
7428 Elf_Internal_Shdr *hdr1, *hdr2;
7429 size_t symcount1, symcount2;
7430 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7431 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7432 Elf_Internal_Sym *isym, *isymend;
7433 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7434 size_t count1, count2, i;
7435 unsigned int shndx1, shndx2;
7441 /* Both sections have to be in ELF. */
7442 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7443 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7446 if (elf_section_type (sec1) != elf_section_type (sec2))
7449 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7450 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7451 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7454 bed1 = get_elf_backend_data (bfd1);
7455 bed2 = get_elf_backend_data (bfd2);
7456 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7457 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7458 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7459 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7461 if (symcount1 == 0 || symcount2 == 0)
7467 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7468 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7470 if (ssymbuf1 == NULL)
7472 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7474 if (isymbuf1 == NULL)
7477 if (!info->reduce_memory_overheads)
7478 elf_tdata (bfd1)->symbuf = ssymbuf1
7479 = elf_create_symbuf (symcount1, isymbuf1);
7482 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7484 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7486 if (isymbuf2 == NULL)
7489 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7490 elf_tdata (bfd2)->symbuf = ssymbuf2
7491 = elf_create_symbuf (symcount2, isymbuf2);
7494 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7496 /* Optimized faster version. */
7498 struct elf_symbol *symp;
7499 struct elf_symbuf_symbol *ssym, *ssymend;
7502 hi = ssymbuf1->count;
7507 mid = (lo + hi) / 2;
7508 if (shndx1 < ssymbuf1[mid].st_shndx)
7510 else if (shndx1 > ssymbuf1[mid].st_shndx)
7514 count1 = ssymbuf1[mid].count;
7521 hi = ssymbuf2->count;
7526 mid = (lo + hi) / 2;
7527 if (shndx2 < ssymbuf2[mid].st_shndx)
7529 else if (shndx2 > ssymbuf2[mid].st_shndx)
7533 count2 = ssymbuf2[mid].count;
7539 if (count1 == 0 || count2 == 0 || count1 != count2)
7543 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7545 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7546 if (symtable1 == NULL || symtable2 == NULL)
7550 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7551 ssym < ssymend; ssym++, symp++)
7553 symp->u.ssym = ssym;
7554 symp->name = bfd_elf_string_from_elf_section (bfd1,
7560 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7561 ssym < ssymend; ssym++, symp++)
7563 symp->u.ssym = ssym;
7564 symp->name = bfd_elf_string_from_elf_section (bfd2,
7569 /* Sort symbol by name. */
7570 qsort (symtable1, count1, sizeof (struct elf_symbol),
7571 elf_sym_name_compare);
7572 qsort (symtable2, count1, sizeof (struct elf_symbol),
7573 elf_sym_name_compare);
7575 for (i = 0; i < count1; i++)
7576 /* Two symbols must have the same binding, type and name. */
7577 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7578 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7579 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7586 symtable1 = (struct elf_symbol *)
7587 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7588 symtable2 = (struct elf_symbol *)
7589 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7590 if (symtable1 == NULL || symtable2 == NULL)
7593 /* Count definitions in the section. */
7595 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7596 if (isym->st_shndx == shndx1)
7597 symtable1[count1++].u.isym = isym;
7600 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7601 if (isym->st_shndx == shndx2)
7602 symtable2[count2++].u.isym = isym;
7604 if (count1 == 0 || count2 == 0 || count1 != count2)
7607 for (i = 0; i < count1; i++)
7609 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7610 symtable1[i].u.isym->st_name);
7612 for (i = 0; i < count2; i++)
7614 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7615 symtable2[i].u.isym->st_name);
7617 /* Sort symbol by name. */
7618 qsort (symtable1, count1, sizeof (struct elf_symbol),
7619 elf_sym_name_compare);
7620 qsort (symtable2, count1, sizeof (struct elf_symbol),
7621 elf_sym_name_compare);
7623 for (i = 0; i < count1; i++)
7624 /* Two symbols must have the same binding, type and name. */
7625 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7626 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7627 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7645 /* Return TRUE if 2 section types are compatible. */
7648 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7649 bfd *bbfd, const asection *bsec)
7653 || abfd->xvec->flavour != bfd_target_elf_flavour
7654 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7657 return elf_section_type (asec) == elf_section_type (bsec);
7660 /* Final phase of ELF linker. */
7662 /* A structure we use to avoid passing large numbers of arguments. */
7664 struct elf_final_link_info
7666 /* General link information. */
7667 struct bfd_link_info *info;
7670 /* Symbol string table. */
7671 struct elf_strtab_hash *symstrtab;
7672 /* .hash section. */
7674 /* symbol version section (.gnu.version). */
7675 asection *symver_sec;
7676 /* Buffer large enough to hold contents of any section. */
7678 /* Buffer large enough to hold external relocs of any section. */
7679 void *external_relocs;
7680 /* Buffer large enough to hold internal relocs of any section. */
7681 Elf_Internal_Rela *internal_relocs;
7682 /* Buffer large enough to hold external local symbols of any input
7684 bfd_byte *external_syms;
7685 /* And a buffer for symbol section indices. */
7686 Elf_External_Sym_Shndx *locsym_shndx;
7687 /* Buffer large enough to hold internal local symbols of any input
7689 Elf_Internal_Sym *internal_syms;
7690 /* Array large enough to hold a symbol index for each local symbol
7691 of any input BFD. */
7693 /* Array large enough to hold a section pointer for each local
7694 symbol of any input BFD. */
7695 asection **sections;
7696 /* Buffer for SHT_SYMTAB_SHNDX section. */
7697 Elf_External_Sym_Shndx *symshndxbuf;
7698 /* Number of STT_FILE syms seen. */
7699 size_t filesym_count;
7702 /* This struct is used to pass information to elf_link_output_extsym. */
7704 struct elf_outext_info
7707 bfd_boolean localsyms;
7708 bfd_boolean file_sym_done;
7709 struct elf_final_link_info *flinfo;
7713 /* Support for evaluating a complex relocation.
7715 Complex relocations are generalized, self-describing relocations. The
7716 implementation of them consists of two parts: complex symbols, and the
7717 relocations themselves.
7719 The relocations are use a reserved elf-wide relocation type code (R_RELC
7720 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7721 information (start bit, end bit, word width, etc) into the addend. This
7722 information is extracted from CGEN-generated operand tables within gas.
7724 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7725 internal) representing prefix-notation expressions, including but not
7726 limited to those sorts of expressions normally encoded as addends in the
7727 addend field. The symbol mangling format is:
7730 | <unary-operator> ':' <node>
7731 | <binary-operator> ':' <node> ':' <node>
7734 <literal> := 's' <digits=N> ':' <N character symbol name>
7735 | 'S' <digits=N> ':' <N character section name>
7739 <binary-operator> := as in C
7740 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7743 set_symbol_value (bfd *bfd_with_globals,
7744 Elf_Internal_Sym *isymbuf,
7749 struct elf_link_hash_entry **sym_hashes;
7750 struct elf_link_hash_entry *h;
7751 size_t extsymoff = locsymcount;
7753 if (symidx < locsymcount)
7755 Elf_Internal_Sym *sym;
7757 sym = isymbuf + symidx;
7758 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7760 /* It is a local symbol: move it to the
7761 "absolute" section and give it a value. */
7762 sym->st_shndx = SHN_ABS;
7763 sym->st_value = val;
7766 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7770 /* It is a global symbol: set its link type
7771 to "defined" and give it a value. */
7773 sym_hashes = elf_sym_hashes (bfd_with_globals);
7774 h = sym_hashes [symidx - extsymoff];
7775 while (h->root.type == bfd_link_hash_indirect
7776 || h->root.type == bfd_link_hash_warning)
7777 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7778 h->root.type = bfd_link_hash_defined;
7779 h->root.u.def.value = val;
7780 h->root.u.def.section = bfd_abs_section_ptr;
7784 resolve_symbol (const char *name,
7786 struct elf_final_link_info *flinfo,
7788 Elf_Internal_Sym *isymbuf,
7791 Elf_Internal_Sym *sym;
7792 struct bfd_link_hash_entry *global_entry;
7793 const char *candidate = NULL;
7794 Elf_Internal_Shdr *symtab_hdr;
7797 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7799 for (i = 0; i < locsymcount; ++ i)
7803 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7806 candidate = bfd_elf_string_from_elf_section (input_bfd,
7807 symtab_hdr->sh_link,
7810 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7811 name, candidate, (unsigned long) sym->st_value);
7813 if (candidate && strcmp (candidate, name) == 0)
7815 asection *sec = flinfo->sections [i];
7817 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7818 *result += sec->output_offset + sec->output_section->vma;
7820 printf ("Found symbol with value %8.8lx\n",
7821 (unsigned long) *result);
7827 /* Hmm, haven't found it yet. perhaps it is a global. */
7828 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7829 FALSE, FALSE, TRUE);
7833 if (global_entry->type == bfd_link_hash_defined
7834 || global_entry->type == bfd_link_hash_defweak)
7836 *result = (global_entry->u.def.value
7837 + global_entry->u.def.section->output_section->vma
7838 + global_entry->u.def.section->output_offset);
7840 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7841 global_entry->root.string, (unsigned long) *result);
7849 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7850 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7851 names like "foo.end" which is the end address of section "foo". */
7854 resolve_section (const char *name,
7862 for (curr = sections; curr; curr = curr->next)
7863 if (strcmp (curr->name, name) == 0)
7865 *result = curr->vma;
7869 /* Hmm. still haven't found it. try pseudo-section names. */
7870 /* FIXME: This could be coded more efficiently... */
7871 for (curr = sections; curr; curr = curr->next)
7873 len = strlen (curr->name);
7874 if (len > strlen (name))
7877 if (strncmp (curr->name, name, len) == 0)
7879 if (strncmp (".end", name + len, 4) == 0)
7881 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
7885 /* Insert more pseudo-section names here, if you like. */
7893 undefined_reference (const char *reftype, const char *name)
7895 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7900 eval_symbol (bfd_vma *result,
7903 struct elf_final_link_info *flinfo,
7905 Elf_Internal_Sym *isymbuf,
7914 const char *sym = *symp;
7916 bfd_boolean symbol_is_section = FALSE;
7921 if (len < 1 || len > sizeof (symbuf))
7923 bfd_set_error (bfd_error_invalid_operation);
7936 *result = strtoul (sym, (char **) symp, 16);
7940 symbol_is_section = TRUE;
7943 symlen = strtol (sym, (char **) symp, 10);
7944 sym = *symp + 1; /* Skip the trailing ':'. */
7946 if (symend < sym || symlen + 1 > sizeof (symbuf))
7948 bfd_set_error (bfd_error_invalid_operation);
7952 memcpy (symbuf, sym, symlen);
7953 symbuf[symlen] = '\0';
7954 *symp = sym + symlen;
7956 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7957 the symbol as a section, or vice-versa. so we're pretty liberal in our
7958 interpretation here; section means "try section first", not "must be a
7959 section", and likewise with symbol. */
7961 if (symbol_is_section)
7963 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
7964 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7965 isymbuf, locsymcount))
7967 undefined_reference ("section", symbuf);
7973 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7974 isymbuf, locsymcount)
7975 && !resolve_section (symbuf, flinfo->output_bfd->sections,
7978 undefined_reference ("symbol", symbuf);
7985 /* All that remains are operators. */
7987 #define UNARY_OP(op) \
7988 if (strncmp (sym, #op, strlen (#op)) == 0) \
7990 sym += strlen (#op); \
7994 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7995 isymbuf, locsymcount, signed_p)) \
7998 *result = op ((bfd_signed_vma) a); \
8004 #define BINARY_OP(op) \
8005 if (strncmp (sym, #op, strlen (#op)) == 0) \
8007 sym += strlen (#op); \
8011 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8012 isymbuf, locsymcount, signed_p)) \
8015 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8016 isymbuf, locsymcount, signed_p)) \
8019 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8049 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8050 bfd_set_error (bfd_error_invalid_operation);
8056 put_value (bfd_vma size,
8057 unsigned long chunksz,
8062 location += (size - chunksz);
8064 for (; size; size -= chunksz, location -= chunksz)
8069 bfd_put_8 (input_bfd, x, location);
8073 bfd_put_16 (input_bfd, x, location);
8077 bfd_put_32 (input_bfd, x, location);
8078 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8084 bfd_put_64 (input_bfd, x, location);
8085 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8098 get_value (bfd_vma size,
8099 unsigned long chunksz,
8106 /* Sanity checks. */
8107 BFD_ASSERT (chunksz <= sizeof (x)
8110 && (size % chunksz) == 0
8111 && input_bfd != NULL
8112 && location != NULL);
8114 if (chunksz == sizeof (x))
8116 BFD_ASSERT (size == chunksz);
8118 /* Make sure that we do not perform an undefined shift operation.
8119 We know that size == chunksz so there will only be one iteration
8120 of the loop below. */
8124 shift = 8 * chunksz;
8126 for (; size; size -= chunksz, location += chunksz)
8131 x = (x << shift) | bfd_get_8 (input_bfd, location);
8134 x = (x << shift) | bfd_get_16 (input_bfd, location);
8137 x = (x << shift) | bfd_get_32 (input_bfd, location);
8141 x = (x << shift) | bfd_get_64 (input_bfd, location);
8152 decode_complex_addend (unsigned long *start, /* in bits */
8153 unsigned long *oplen, /* in bits */
8154 unsigned long *len, /* in bits */
8155 unsigned long *wordsz, /* in bytes */
8156 unsigned long *chunksz, /* in bytes */
8157 unsigned long *lsb0_p,
8158 unsigned long *signed_p,
8159 unsigned long *trunc_p,
8160 unsigned long encoded)
8162 * start = encoded & 0x3F;
8163 * len = (encoded >> 6) & 0x3F;
8164 * oplen = (encoded >> 12) & 0x3F;
8165 * wordsz = (encoded >> 18) & 0xF;
8166 * chunksz = (encoded >> 22) & 0xF;
8167 * lsb0_p = (encoded >> 27) & 1;
8168 * signed_p = (encoded >> 28) & 1;
8169 * trunc_p = (encoded >> 29) & 1;
8172 bfd_reloc_status_type
8173 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8174 asection *input_section ATTRIBUTE_UNUSED,
8176 Elf_Internal_Rela *rel,
8179 bfd_vma shift, x, mask;
8180 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8181 bfd_reloc_status_type r;
8183 /* Perform this reloc, since it is complex.
8184 (this is not to say that it necessarily refers to a complex
8185 symbol; merely that it is a self-describing CGEN based reloc.
8186 i.e. the addend has the complete reloc information (bit start, end,
8187 word size, etc) encoded within it.). */
8189 decode_complex_addend (&start, &oplen, &len, &wordsz,
8190 &chunksz, &lsb0_p, &signed_p,
8191 &trunc_p, rel->r_addend);
8193 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8196 shift = (start + 1) - len;
8198 shift = (8 * wordsz) - (start + len);
8200 x = get_value (wordsz, chunksz, input_bfd,
8201 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8204 printf ("Doing complex reloc: "
8205 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8206 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8207 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8208 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8209 oplen, (unsigned long) x, (unsigned long) mask,
8210 (unsigned long) relocation);
8215 /* Now do an overflow check. */
8216 r = bfd_check_overflow ((signed_p
8217 ? complain_overflow_signed
8218 : complain_overflow_unsigned),
8219 len, 0, (8 * wordsz),
8223 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8226 printf (" relocation: %8.8lx\n"
8227 " shifted mask: %8.8lx\n"
8228 " shifted/masked reloc: %8.8lx\n"
8229 " result: %8.8lx\n",
8230 (unsigned long) relocation, (unsigned long) (mask << shift),
8231 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8233 put_value (wordsz, chunksz, input_bfd, x,
8234 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8238 /* Functions to read r_offset from external (target order) reloc
8239 entry. Faster than bfd_getl32 et al, because we let the compiler
8240 know the value is aligned. */
8243 ext32l_r_offset (const void *p)
8250 const union aligned32 *a
8251 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8253 uint32_t aval = ( (uint32_t) a->c[0]
8254 | (uint32_t) a->c[1] << 8
8255 | (uint32_t) a->c[2] << 16
8256 | (uint32_t) a->c[3] << 24);
8261 ext32b_r_offset (const void *p)
8268 const union aligned32 *a
8269 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8271 uint32_t aval = ( (uint32_t) a->c[0] << 24
8272 | (uint32_t) a->c[1] << 16
8273 | (uint32_t) a->c[2] << 8
8274 | (uint32_t) a->c[3]);
8278 #ifdef BFD_HOST_64_BIT
8280 ext64l_r_offset (const void *p)
8287 const union aligned64 *a
8288 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8290 uint64_t aval = ( (uint64_t) a->c[0]
8291 | (uint64_t) a->c[1] << 8
8292 | (uint64_t) a->c[2] << 16
8293 | (uint64_t) a->c[3] << 24
8294 | (uint64_t) a->c[4] << 32
8295 | (uint64_t) a->c[5] << 40
8296 | (uint64_t) a->c[6] << 48
8297 | (uint64_t) a->c[7] << 56);
8302 ext64b_r_offset (const void *p)
8309 const union aligned64 *a
8310 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8312 uint64_t aval = ( (uint64_t) a->c[0] << 56
8313 | (uint64_t) a->c[1] << 48
8314 | (uint64_t) a->c[2] << 40
8315 | (uint64_t) a->c[3] << 32
8316 | (uint64_t) a->c[4] << 24
8317 | (uint64_t) a->c[5] << 16
8318 | (uint64_t) a->c[6] << 8
8319 | (uint64_t) a->c[7]);
8324 /* When performing a relocatable link, the input relocations are
8325 preserved. But, if they reference global symbols, the indices
8326 referenced must be updated. Update all the relocations found in
8330 elf_link_adjust_relocs (bfd *abfd,
8331 struct bfd_elf_section_reloc_data *reldata,
8335 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8337 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8338 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8339 bfd_vma r_type_mask;
8341 unsigned int count = reldata->count;
8342 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8344 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8346 swap_in = bed->s->swap_reloc_in;
8347 swap_out = bed->s->swap_reloc_out;
8349 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8351 swap_in = bed->s->swap_reloca_in;
8352 swap_out = bed->s->swap_reloca_out;
8357 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8360 if (bed->s->arch_size == 32)
8367 r_type_mask = 0xffffffff;
8371 erela = reldata->hdr->contents;
8372 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8374 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8377 if (*rel_hash == NULL)
8380 BFD_ASSERT ((*rel_hash)->indx >= 0);
8382 (*swap_in) (abfd, erela, irela);
8383 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8384 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8385 | (irela[j].r_info & r_type_mask));
8386 (*swap_out) (abfd, irela, erela);
8389 if (sort && count != 0)
8391 bfd_vma (*ext_r_off) (const void *);
8394 bfd_byte *base, *end, *p, *loc;
8395 bfd_byte *buf = NULL;
8397 if (bed->s->arch_size == 32)
8399 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8400 ext_r_off = ext32l_r_offset;
8401 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8402 ext_r_off = ext32b_r_offset;
8408 #ifdef BFD_HOST_64_BIT
8409 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8410 ext_r_off = ext64l_r_offset;
8411 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8412 ext_r_off = ext64b_r_offset;
8418 /* Must use a stable sort here. A modified insertion sort,
8419 since the relocs are mostly sorted already. */
8420 elt_size = reldata->hdr->sh_entsize;
8421 base = reldata->hdr->contents;
8422 end = base + count * elt_size;
8423 if (elt_size > sizeof (Elf64_External_Rela))
8426 /* Ensure the first element is lowest. This acts as a sentinel,
8427 speeding the main loop below. */
8428 r_off = (*ext_r_off) (base);
8429 for (p = loc = base; (p += elt_size) < end; )
8431 bfd_vma r_off2 = (*ext_r_off) (p);
8440 /* Don't just swap *base and *loc as that changes the order
8441 of the original base[0] and base[1] if they happen to
8442 have the same r_offset. */
8443 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8444 memcpy (onebuf, loc, elt_size);
8445 memmove (base + elt_size, base, loc - base);
8446 memcpy (base, onebuf, elt_size);
8449 for (p = base + elt_size; (p += elt_size) < end; )
8451 /* base to p is sorted, *p is next to insert. */
8452 r_off = (*ext_r_off) (p);
8453 /* Search the sorted region for location to insert. */
8455 while (r_off < (*ext_r_off) (loc))
8460 /* Chances are there is a run of relocs to insert here,
8461 from one of more input files. Files are not always
8462 linked in order due to the way elf_link_input_bfd is
8463 called. See pr17666. */
8464 size_t sortlen = p - loc;
8465 bfd_vma r_off2 = (*ext_r_off) (loc);
8466 size_t runlen = elt_size;
8467 size_t buf_size = 96 * 1024;
8468 while (p + runlen < end
8469 && (sortlen <= buf_size
8470 || runlen + elt_size <= buf_size)
8471 && r_off2 > (*ext_r_off) (p + runlen))
8475 buf = bfd_malloc (buf_size);
8479 if (runlen < sortlen)
8481 memcpy (buf, p, runlen);
8482 memmove (loc + runlen, loc, sortlen);
8483 memcpy (loc, buf, runlen);
8487 memcpy (buf, loc, sortlen);
8488 memmove (loc, p, runlen);
8489 memcpy (loc + runlen, buf, sortlen);
8491 p += runlen - elt_size;
8494 /* Hashes are no longer valid. */
8495 free (reldata->hashes);
8496 reldata->hashes = NULL;
8502 struct elf_link_sort_rela
8508 enum elf_reloc_type_class type;
8509 /* We use this as an array of size int_rels_per_ext_rel. */
8510 Elf_Internal_Rela rela[1];
8514 elf_link_sort_cmp1 (const void *A, const void *B)
8516 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8517 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8518 int relativea, relativeb;
8520 relativea = a->type == reloc_class_relative;
8521 relativeb = b->type == reloc_class_relative;
8523 if (relativea < relativeb)
8525 if (relativea > relativeb)
8527 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8529 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8531 if (a->rela->r_offset < b->rela->r_offset)
8533 if (a->rela->r_offset > b->rela->r_offset)
8539 elf_link_sort_cmp2 (const void *A, const void *B)
8541 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8542 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8544 if (a->type < b->type)
8546 if (a->type > b->type)
8548 if (a->u.offset < b->u.offset)
8550 if (a->u.offset > b->u.offset)
8552 if (a->rela->r_offset < b->rela->r_offset)
8554 if (a->rela->r_offset > b->rela->r_offset)
8560 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8562 asection *dynamic_relocs;
8565 bfd_size_type count, size;
8566 size_t i, ret, sort_elt, ext_size;
8567 bfd_byte *sort, *s_non_relative, *p;
8568 struct elf_link_sort_rela *sq;
8569 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8570 int i2e = bed->s->int_rels_per_ext_rel;
8571 unsigned int opb = bfd_octets_per_byte (abfd);
8572 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8573 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8574 struct bfd_link_order *lo;
8576 bfd_boolean use_rela;
8578 /* Find a dynamic reloc section. */
8579 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8580 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8581 if (rela_dyn != NULL && rela_dyn->size > 0
8582 && rel_dyn != NULL && rel_dyn->size > 0)
8584 bfd_boolean use_rela_initialised = FALSE;
8586 /* This is just here to stop gcc from complaining.
8587 Its initialization checking code is not perfect. */
8590 /* Both sections are present. Examine the sizes
8591 of the indirect sections to help us choose. */
8592 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8593 if (lo->type == bfd_indirect_link_order)
8595 asection *o = lo->u.indirect.section;
8597 if ((o->size % bed->s->sizeof_rela) == 0)
8599 if ((o->size % bed->s->sizeof_rel) == 0)
8600 /* Section size is divisible by both rel and rela sizes.
8601 It is of no help to us. */
8605 /* Section size is only divisible by rela. */
8606 if (use_rela_initialised && (use_rela == FALSE))
8608 _bfd_error_handler (_("%B: Unable to sort relocs - "
8609 "they are in more than one size"),
8611 bfd_set_error (bfd_error_invalid_operation);
8617 use_rela_initialised = TRUE;
8621 else if ((o->size % bed->s->sizeof_rel) == 0)
8623 /* Section size is only divisible by rel. */
8624 if (use_rela_initialised && (use_rela == TRUE))
8626 _bfd_error_handler (_("%B: Unable to sort relocs - "
8627 "they are in more than one size"),
8629 bfd_set_error (bfd_error_invalid_operation);
8635 use_rela_initialised = TRUE;
8640 /* The section size is not divisible by either -
8641 something is wrong. */
8642 _bfd_error_handler (_("%B: Unable to sort relocs - "
8643 "they are of an unknown size"), abfd);
8644 bfd_set_error (bfd_error_invalid_operation);
8649 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8650 if (lo->type == bfd_indirect_link_order)
8652 asection *o = lo->u.indirect.section;
8654 if ((o->size % bed->s->sizeof_rela) == 0)
8656 if ((o->size % bed->s->sizeof_rel) == 0)
8657 /* Section size is divisible by both rel and rela sizes.
8658 It is of no help to us. */
8662 /* Section size is only divisible by rela. */
8663 if (use_rela_initialised && (use_rela == FALSE))
8665 _bfd_error_handler (_("%B: Unable to sort relocs - "
8666 "they are in more than one size"),
8668 bfd_set_error (bfd_error_invalid_operation);
8674 use_rela_initialised = TRUE;
8678 else if ((o->size % bed->s->sizeof_rel) == 0)
8680 /* Section size is only divisible by rel. */
8681 if (use_rela_initialised && (use_rela == TRUE))
8683 _bfd_error_handler (_("%B: Unable to sort relocs - "
8684 "they are in more than one size"),
8686 bfd_set_error (bfd_error_invalid_operation);
8692 use_rela_initialised = TRUE;
8697 /* The section size is not divisible by either -
8698 something is wrong. */
8699 _bfd_error_handler (_("%B: Unable to sort relocs - "
8700 "they are of an unknown size"), abfd);
8701 bfd_set_error (bfd_error_invalid_operation);
8706 if (! use_rela_initialised)
8710 else if (rela_dyn != NULL && rela_dyn->size > 0)
8712 else if (rel_dyn != NULL && rel_dyn->size > 0)
8719 dynamic_relocs = rela_dyn;
8720 ext_size = bed->s->sizeof_rela;
8721 swap_in = bed->s->swap_reloca_in;
8722 swap_out = bed->s->swap_reloca_out;
8726 dynamic_relocs = rel_dyn;
8727 ext_size = bed->s->sizeof_rel;
8728 swap_in = bed->s->swap_reloc_in;
8729 swap_out = bed->s->swap_reloc_out;
8733 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8734 if (lo->type == bfd_indirect_link_order)
8735 size += lo->u.indirect.section->size;
8737 if (size != dynamic_relocs->size)
8740 sort_elt = (sizeof (struct elf_link_sort_rela)
8741 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8743 count = dynamic_relocs->size / ext_size;
8746 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8750 (*info->callbacks->warning)
8751 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8755 if (bed->s->arch_size == 32)
8756 r_sym_mask = ~(bfd_vma) 0xff;
8758 r_sym_mask = ~(bfd_vma) 0xffffffff;
8760 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8761 if (lo->type == bfd_indirect_link_order)
8763 bfd_byte *erel, *erelend;
8764 asection *o = lo->u.indirect.section;
8766 if (o->contents == NULL && o->size != 0)
8768 /* This is a reloc section that is being handled as a normal
8769 section. See bfd_section_from_shdr. We can't combine
8770 relocs in this case. */
8775 erelend = o->contents + o->size;
8776 p = sort + o->output_offset * opb / ext_size * sort_elt;
8778 while (erel < erelend)
8780 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8782 (*swap_in) (abfd, erel, s->rela);
8783 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8784 s->u.sym_mask = r_sym_mask;
8790 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8792 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8794 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8795 if (s->type != reloc_class_relative)
8801 sq = (struct elf_link_sort_rela *) s_non_relative;
8802 for (; i < count; i++, p += sort_elt)
8804 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8805 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8807 sp->u.offset = sq->rela->r_offset;
8810 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8812 struct elf_link_hash_table *htab = elf_hash_table (info);
8813 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8815 /* We have plt relocs in .rela.dyn. */
8816 sq = (struct elf_link_sort_rela *) sort;
8817 for (i = 0; i < count; i++)
8818 if (sq[count - i - 1].type != reloc_class_plt)
8820 if (i != 0 && htab->srelplt->size == i * ext_size)
8822 struct bfd_link_order **plo;
8823 /* Put srelplt link_order last. This is so the output_offset
8824 set in the next loop is correct for DT_JMPREL. */
8825 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8826 if ((*plo)->type == bfd_indirect_link_order
8827 && (*plo)->u.indirect.section == htab->srelplt)
8833 plo = &(*plo)->next;
8836 dynamic_relocs->map_tail.link_order = lo;
8841 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8842 if (lo->type == bfd_indirect_link_order)
8844 bfd_byte *erel, *erelend;
8845 asection *o = lo->u.indirect.section;
8848 erelend = o->contents + o->size;
8849 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
8850 while (erel < erelend)
8852 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8853 (*swap_out) (abfd, s->rela, erel);
8860 *psec = dynamic_relocs;
8864 /* Add a symbol to the output symbol string table. */
8867 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8869 Elf_Internal_Sym *elfsym,
8870 asection *input_sec,
8871 struct elf_link_hash_entry *h)
8873 int (*output_symbol_hook)
8874 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8875 struct elf_link_hash_entry *);
8876 struct elf_link_hash_table *hash_table;
8877 const struct elf_backend_data *bed;
8878 bfd_size_type strtabsize;
8880 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8882 bed = get_elf_backend_data (flinfo->output_bfd);
8883 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8884 if (output_symbol_hook != NULL)
8886 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8893 || (input_sec->flags & SEC_EXCLUDE))
8894 elfsym->st_name = (unsigned long) -1;
8897 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8898 to get the final offset for st_name. */
8900 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8902 if (elfsym->st_name == (unsigned long) -1)
8906 hash_table = elf_hash_table (flinfo->info);
8907 strtabsize = hash_table->strtabsize;
8908 if (strtabsize <= hash_table->strtabcount)
8910 strtabsize += strtabsize;
8911 hash_table->strtabsize = strtabsize;
8912 strtabsize *= sizeof (*hash_table->strtab);
8914 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8916 if (hash_table->strtab == NULL)
8919 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8920 hash_table->strtab[hash_table->strtabcount].dest_index
8921 = hash_table->strtabcount;
8922 hash_table->strtab[hash_table->strtabcount].destshndx_index
8923 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8925 bfd_get_symcount (flinfo->output_bfd) += 1;
8926 hash_table->strtabcount += 1;
8931 /* Swap symbols out to the symbol table and flush the output symbols to
8935 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8937 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8940 const struct elf_backend_data *bed;
8942 Elf_Internal_Shdr *hdr;
8946 if (!hash_table->strtabcount)
8949 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8951 bed = get_elf_backend_data (flinfo->output_bfd);
8953 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8954 symbuf = (bfd_byte *) bfd_malloc (amt);
8958 if (flinfo->symshndxbuf)
8960 amt = sizeof (Elf_External_Sym_Shndx);
8961 amt *= bfd_get_symcount (flinfo->output_bfd);
8962 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8963 if (flinfo->symshndxbuf == NULL)
8970 for (i = 0; i < hash_table->strtabcount; i++)
8972 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8973 if (elfsym->sym.st_name == (unsigned long) -1)
8974 elfsym->sym.st_name = 0;
8977 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8978 elfsym->sym.st_name);
8979 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8980 ((bfd_byte *) symbuf
8981 + (elfsym->dest_index
8982 * bed->s->sizeof_sym)),
8983 (flinfo->symshndxbuf
8984 + elfsym->destshndx_index));
8987 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8988 pos = hdr->sh_offset + hdr->sh_size;
8989 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8990 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8991 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8993 hdr->sh_size += amt;
9001 free (hash_table->strtab);
9002 hash_table->strtab = NULL;
9007 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9010 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9012 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9013 && sym->st_shndx < SHN_LORESERVE)
9015 /* The gABI doesn't support dynamic symbols in output sections
9017 (*_bfd_error_handler)
9018 (_("%B: Too many sections: %d (>= %d)"),
9019 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9020 bfd_set_error (bfd_error_nonrepresentable_section);
9026 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9027 allowing an unsatisfied unversioned symbol in the DSO to match a
9028 versioned symbol that would normally require an explicit version.
9029 We also handle the case that a DSO references a hidden symbol
9030 which may be satisfied by a versioned symbol in another DSO. */
9033 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9034 const struct elf_backend_data *bed,
9035 struct elf_link_hash_entry *h)
9038 struct elf_link_loaded_list *loaded;
9040 if (!is_elf_hash_table (info->hash))
9043 /* Check indirect symbol. */
9044 while (h->root.type == bfd_link_hash_indirect)
9045 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9047 switch (h->root.type)
9053 case bfd_link_hash_undefined:
9054 case bfd_link_hash_undefweak:
9055 abfd = h->root.u.undef.abfd;
9056 if ((abfd->flags & DYNAMIC) == 0
9057 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9061 case bfd_link_hash_defined:
9062 case bfd_link_hash_defweak:
9063 abfd = h->root.u.def.section->owner;
9066 case bfd_link_hash_common:
9067 abfd = h->root.u.c.p->section->owner;
9070 BFD_ASSERT (abfd != NULL);
9072 for (loaded = elf_hash_table (info)->loaded;
9074 loaded = loaded->next)
9077 Elf_Internal_Shdr *hdr;
9081 Elf_Internal_Shdr *versymhdr;
9082 Elf_Internal_Sym *isym;
9083 Elf_Internal_Sym *isymend;
9084 Elf_Internal_Sym *isymbuf;
9085 Elf_External_Versym *ever;
9086 Elf_External_Versym *extversym;
9088 input = loaded->abfd;
9090 /* We check each DSO for a possible hidden versioned definition. */
9092 || (input->flags & DYNAMIC) == 0
9093 || elf_dynversym (input) == 0)
9096 hdr = &elf_tdata (input)->dynsymtab_hdr;
9098 symcount = hdr->sh_size / bed->s->sizeof_sym;
9099 if (elf_bad_symtab (input))
9101 extsymcount = symcount;
9106 extsymcount = symcount - hdr->sh_info;
9107 extsymoff = hdr->sh_info;
9110 if (extsymcount == 0)
9113 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9115 if (isymbuf == NULL)
9118 /* Read in any version definitions. */
9119 versymhdr = &elf_tdata (input)->dynversym_hdr;
9120 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9121 if (extversym == NULL)
9124 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9125 || (bfd_bread (extversym, versymhdr->sh_size, input)
9126 != versymhdr->sh_size))
9134 ever = extversym + extsymoff;
9135 isymend = isymbuf + extsymcount;
9136 for (isym = isymbuf; isym < isymend; isym++, ever++)
9139 Elf_Internal_Versym iver;
9140 unsigned short version_index;
9142 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9143 || isym->st_shndx == SHN_UNDEF)
9146 name = bfd_elf_string_from_elf_section (input,
9149 if (strcmp (name, h->root.root.string) != 0)
9152 _bfd_elf_swap_versym_in (input, ever, &iver);
9154 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9156 && h->forced_local))
9158 /* If we have a non-hidden versioned sym, then it should
9159 have provided a definition for the undefined sym unless
9160 it is defined in a non-shared object and forced local.
9165 version_index = iver.vs_vers & VERSYM_VERSION;
9166 if (version_index == 1 || version_index == 2)
9168 /* This is the base or first version. We can use it. */
9182 /* Convert ELF common symbol TYPE. */
9185 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9187 /* Commom symbol can only appear in relocatable link. */
9188 if (!bfd_link_relocatable (info))
9190 switch (info->elf_stt_common)
9194 case elf_stt_common:
9197 case no_elf_stt_common:
9204 /* Add an external symbol to the symbol table. This is called from
9205 the hash table traversal routine. When generating a shared object,
9206 we go through the symbol table twice. The first time we output
9207 anything that might have been forced to local scope in a version
9208 script. The second time we output the symbols that are still
9212 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9214 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9215 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9216 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9218 Elf_Internal_Sym sym;
9219 asection *input_sec;
9220 const struct elf_backend_data *bed;
9224 /* A symbol is bound locally if it is forced local or it is locally
9225 defined, hidden versioned, not referenced by shared library and
9226 not exported when linking executable. */
9227 bfd_boolean local_bind = (h->forced_local
9228 || (bfd_link_executable (flinfo->info)
9229 && !flinfo->info->export_dynamic
9233 && h->versioned == versioned_hidden));
9235 if (h->root.type == bfd_link_hash_warning)
9237 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9238 if (h->root.type == bfd_link_hash_new)
9242 /* Decide whether to output this symbol in this pass. */
9243 if (eoinfo->localsyms)
9254 bed = get_elf_backend_data (flinfo->output_bfd);
9256 if (h->root.type == bfd_link_hash_undefined)
9258 /* If we have an undefined symbol reference here then it must have
9259 come from a shared library that is being linked in. (Undefined
9260 references in regular files have already been handled unless
9261 they are in unreferenced sections which are removed by garbage
9263 bfd_boolean ignore_undef = FALSE;
9265 /* Some symbols may be special in that the fact that they're
9266 undefined can be safely ignored - let backend determine that. */
9267 if (bed->elf_backend_ignore_undef_symbol)
9268 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9270 /* If we are reporting errors for this situation then do so now. */
9273 && (!h->ref_regular || flinfo->info->gc_sections)
9274 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9275 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9276 (*flinfo->info->callbacks->undefined_symbol)
9277 (flinfo->info, h->root.root.string,
9278 h->ref_regular ? NULL : h->root.u.undef.abfd,
9280 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9282 /* Strip a global symbol defined in a discarded section. */
9287 /* We should also warn if a forced local symbol is referenced from
9288 shared libraries. */
9289 if (bfd_link_executable (flinfo->info)
9294 && h->ref_dynamic_nonweak
9295 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9299 struct elf_link_hash_entry *hi = h;
9301 /* Check indirect symbol. */
9302 while (hi->root.type == bfd_link_hash_indirect)
9303 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9305 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9306 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9307 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9308 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9310 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9311 def_bfd = flinfo->output_bfd;
9312 if (hi->root.u.def.section != bfd_abs_section_ptr)
9313 def_bfd = hi->root.u.def.section->owner;
9314 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
9315 h->root.root.string);
9316 bfd_set_error (bfd_error_bad_value);
9317 eoinfo->failed = TRUE;
9321 /* We don't want to output symbols that have never been mentioned by
9322 a regular file, or that we have been told to strip. However, if
9323 h->indx is set to -2, the symbol is used by a reloc and we must
9328 else if ((h->def_dynamic
9330 || h->root.type == bfd_link_hash_new)
9334 else if (flinfo->info->strip == strip_all)
9336 else if (flinfo->info->strip == strip_some
9337 && bfd_hash_lookup (flinfo->info->keep_hash,
9338 h->root.root.string, FALSE, FALSE) == NULL)
9340 else if ((h->root.type == bfd_link_hash_defined
9341 || h->root.type == bfd_link_hash_defweak)
9342 && ((flinfo->info->strip_discarded
9343 && discarded_section (h->root.u.def.section))
9344 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9345 && h->root.u.def.section->owner != NULL
9346 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9348 else if ((h->root.type == bfd_link_hash_undefined
9349 || h->root.type == bfd_link_hash_undefweak)
9350 && h->root.u.undef.abfd != NULL
9351 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9356 /* If we're stripping it, and it's not a dynamic symbol, there's
9357 nothing else to do. However, if it is a forced local symbol or
9358 an ifunc symbol we need to give the backend finish_dynamic_symbol
9359 function a chance to make it dynamic. */
9362 && type != STT_GNU_IFUNC
9363 && !h->forced_local)
9367 sym.st_size = h->size;
9368 sym.st_other = h->other;
9369 switch (h->root.type)
9372 case bfd_link_hash_new:
9373 case bfd_link_hash_warning:
9377 case bfd_link_hash_undefined:
9378 case bfd_link_hash_undefweak:
9379 input_sec = bfd_und_section_ptr;
9380 sym.st_shndx = SHN_UNDEF;
9383 case bfd_link_hash_defined:
9384 case bfd_link_hash_defweak:
9386 input_sec = h->root.u.def.section;
9387 if (input_sec->output_section != NULL)
9390 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9391 input_sec->output_section);
9392 if (sym.st_shndx == SHN_BAD)
9394 (*_bfd_error_handler)
9395 (_("%B: could not find output section %A for input section %A"),
9396 flinfo->output_bfd, input_sec->output_section, input_sec);
9397 bfd_set_error (bfd_error_nonrepresentable_section);
9398 eoinfo->failed = TRUE;
9402 /* ELF symbols in relocatable files are section relative,
9403 but in nonrelocatable files they are virtual
9405 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9406 if (!bfd_link_relocatable (flinfo->info))
9408 sym.st_value += input_sec->output_section->vma;
9409 if (h->type == STT_TLS)
9411 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9412 if (tls_sec != NULL)
9413 sym.st_value -= tls_sec->vma;
9419 BFD_ASSERT (input_sec->owner == NULL
9420 || (input_sec->owner->flags & DYNAMIC) != 0);
9421 sym.st_shndx = SHN_UNDEF;
9422 input_sec = bfd_und_section_ptr;
9427 case bfd_link_hash_common:
9428 input_sec = h->root.u.c.p->section;
9429 sym.st_shndx = bed->common_section_index (input_sec);
9430 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9433 case bfd_link_hash_indirect:
9434 /* These symbols are created by symbol versioning. They point
9435 to the decorated version of the name. For example, if the
9436 symbol foo@@GNU_1.2 is the default, which should be used when
9437 foo is used with no version, then we add an indirect symbol
9438 foo which points to foo@@GNU_1.2. We ignore these symbols,
9439 since the indirected symbol is already in the hash table. */
9443 if (type == STT_COMMON || type == STT_OBJECT)
9444 switch (h->root.type)
9446 case bfd_link_hash_common:
9447 type = elf_link_convert_common_type (flinfo->info, type);
9449 case bfd_link_hash_defined:
9450 case bfd_link_hash_defweak:
9451 if (bed->common_definition (&sym))
9452 type = elf_link_convert_common_type (flinfo->info, type);
9456 case bfd_link_hash_undefined:
9457 case bfd_link_hash_undefweak:
9465 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9466 /* Turn off visibility on local symbol. */
9467 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9469 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9470 else if (h->unique_global && h->def_regular)
9471 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9472 else if (h->root.type == bfd_link_hash_undefweak
9473 || h->root.type == bfd_link_hash_defweak)
9474 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9476 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9477 sym.st_target_internal = h->target_internal;
9479 /* Give the processor backend a chance to tweak the symbol value,
9480 and also to finish up anything that needs to be done for this
9481 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9482 forced local syms when non-shared is due to a historical quirk.
9483 STT_GNU_IFUNC symbol must go through PLT. */
9484 if ((h->type == STT_GNU_IFUNC
9486 && !bfd_link_relocatable (flinfo->info))
9487 || ((h->dynindx != -1
9489 && ((bfd_link_pic (flinfo->info)
9490 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9491 || h->root.type != bfd_link_hash_undefweak))
9492 || !h->forced_local)
9493 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9495 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9496 (flinfo->output_bfd, flinfo->info, h, &sym)))
9498 eoinfo->failed = TRUE;
9503 /* If we are marking the symbol as undefined, and there are no
9504 non-weak references to this symbol from a regular object, then
9505 mark the symbol as weak undefined; if there are non-weak
9506 references, mark the symbol as strong. We can't do this earlier,
9507 because it might not be marked as undefined until the
9508 finish_dynamic_symbol routine gets through with it. */
9509 if (sym.st_shndx == SHN_UNDEF
9511 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9512 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9515 type = ELF_ST_TYPE (sym.st_info);
9517 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9518 if (type == STT_GNU_IFUNC)
9521 if (h->ref_regular_nonweak)
9522 bindtype = STB_GLOBAL;
9524 bindtype = STB_WEAK;
9525 sym.st_info = ELF_ST_INFO (bindtype, type);
9528 /* If this is a symbol defined in a dynamic library, don't use the
9529 symbol size from the dynamic library. Relinking an executable
9530 against a new library may introduce gratuitous changes in the
9531 executable's symbols if we keep the size. */
9532 if (sym.st_shndx == SHN_UNDEF
9537 /* If a non-weak symbol with non-default visibility is not defined
9538 locally, it is a fatal error. */
9539 if (!bfd_link_relocatable (flinfo->info)
9540 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9541 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9542 && h->root.type == bfd_link_hash_undefined
9547 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9548 msg = _("%B: protected symbol `%s' isn't defined");
9549 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9550 msg = _("%B: internal symbol `%s' isn't defined");
9552 msg = _("%B: hidden symbol `%s' isn't defined");
9553 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9554 bfd_set_error (bfd_error_bad_value);
9555 eoinfo->failed = TRUE;
9559 /* If this symbol should be put in the .dynsym section, then put it
9560 there now. We already know the symbol index. We also fill in
9561 the entry in the .hash section. */
9562 if (elf_hash_table (flinfo->info)->dynsym != NULL
9564 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9568 /* Since there is no version information in the dynamic string,
9569 if there is no version info in symbol version section, we will
9570 have a run-time problem if not linking executable, referenced
9571 by shared library, not locally defined, or not bound locally.
9573 if (h->verinfo.verdef == NULL
9575 && (!bfd_link_executable (flinfo->info)
9577 || !h->def_regular))
9579 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9581 if (p && p [1] != '\0')
9583 (*_bfd_error_handler)
9584 (_("%B: No symbol version section for versioned symbol `%s'"),
9585 flinfo->output_bfd, h->root.root.string);
9586 eoinfo->failed = TRUE;
9591 sym.st_name = h->dynstr_index;
9592 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9593 + h->dynindx * bed->s->sizeof_sym);
9594 if (!check_dynsym (flinfo->output_bfd, &sym))
9596 eoinfo->failed = TRUE;
9599 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9601 if (flinfo->hash_sec != NULL)
9603 size_t hash_entry_size;
9604 bfd_byte *bucketpos;
9609 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9610 bucket = h->u.elf_hash_value % bucketcount;
9613 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9614 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9615 + (bucket + 2) * hash_entry_size);
9616 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9617 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9619 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9620 ((bfd_byte *) flinfo->hash_sec->contents
9621 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9624 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9626 Elf_Internal_Versym iversym;
9627 Elf_External_Versym *eversym;
9629 if (!h->def_regular)
9631 if (h->verinfo.verdef == NULL
9632 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9633 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9634 iversym.vs_vers = 0;
9636 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9640 if (h->verinfo.vertree == NULL)
9641 iversym.vs_vers = 1;
9643 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9644 if (flinfo->info->create_default_symver)
9648 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9650 if (h->versioned == versioned_hidden && h->def_regular)
9651 iversym.vs_vers |= VERSYM_HIDDEN;
9653 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9654 eversym += h->dynindx;
9655 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9659 /* If the symbol is undefined, and we didn't output it to .dynsym,
9660 strip it from .symtab too. Obviously we can't do this for
9661 relocatable output or when needed for --emit-relocs. */
9662 else if (input_sec == bfd_und_section_ptr
9664 && !bfd_link_relocatable (flinfo->info))
9666 /* Also strip others that we couldn't earlier due to dynamic symbol
9670 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9673 /* Output a FILE symbol so that following locals are not associated
9674 with the wrong input file. We need one for forced local symbols
9675 if we've seen more than one FILE symbol or when we have exactly
9676 one FILE symbol but global symbols are present in a file other
9677 than the one with the FILE symbol. We also need one if linker
9678 defined symbols are present. In practice these conditions are
9679 always met, so just emit the FILE symbol unconditionally. */
9680 if (eoinfo->localsyms
9681 && !eoinfo->file_sym_done
9682 && eoinfo->flinfo->filesym_count != 0)
9684 Elf_Internal_Sym fsym;
9686 memset (&fsym, 0, sizeof (fsym));
9687 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9688 fsym.st_shndx = SHN_ABS;
9689 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9690 bfd_und_section_ptr, NULL))
9693 eoinfo->file_sym_done = TRUE;
9696 indx = bfd_get_symcount (flinfo->output_bfd);
9697 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9701 eoinfo->failed = TRUE;
9706 else if (h->indx == -2)
9712 /* Return TRUE if special handling is done for relocs in SEC against
9713 symbols defined in discarded sections. */
9716 elf_section_ignore_discarded_relocs (asection *sec)
9718 const struct elf_backend_data *bed;
9720 switch (sec->sec_info_type)
9722 case SEC_INFO_TYPE_STABS:
9723 case SEC_INFO_TYPE_EH_FRAME:
9724 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9730 bed = get_elf_backend_data (sec->owner);
9731 if (bed->elf_backend_ignore_discarded_relocs != NULL
9732 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9738 /* Return a mask saying how ld should treat relocations in SEC against
9739 symbols defined in discarded sections. If this function returns
9740 COMPLAIN set, ld will issue a warning message. If this function
9741 returns PRETEND set, and the discarded section was link-once and the
9742 same size as the kept link-once section, ld will pretend that the
9743 symbol was actually defined in the kept section. Otherwise ld will
9744 zero the reloc (at least that is the intent, but some cooperation by
9745 the target dependent code is needed, particularly for REL targets). */
9748 _bfd_elf_default_action_discarded (asection *sec)
9750 if (sec->flags & SEC_DEBUGGING)
9753 if (strcmp (".eh_frame", sec->name) == 0)
9756 if (strcmp (".gcc_except_table", sec->name) == 0)
9759 return COMPLAIN | PRETEND;
9762 /* Find a match between a section and a member of a section group. */
9765 match_group_member (asection *sec, asection *group,
9766 struct bfd_link_info *info)
9768 asection *first = elf_next_in_group (group);
9769 asection *s = first;
9773 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9776 s = elf_next_in_group (s);
9784 /* Check if the kept section of a discarded section SEC can be used
9785 to replace it. Return the replacement if it is OK. Otherwise return
9789 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9793 kept = sec->kept_section;
9796 if ((kept->flags & SEC_GROUP) != 0)
9797 kept = match_group_member (sec, kept, info);
9799 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9800 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9802 sec->kept_section = kept;
9807 /* Link an input file into the linker output file. This function
9808 handles all the sections and relocations of the input file at once.
9809 This is so that we only have to read the local symbols once, and
9810 don't have to keep them in memory. */
9813 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9815 int (*relocate_section)
9816 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9817 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9819 Elf_Internal_Shdr *symtab_hdr;
9822 Elf_Internal_Sym *isymbuf;
9823 Elf_Internal_Sym *isym;
9824 Elf_Internal_Sym *isymend;
9826 asection **ppsection;
9828 const struct elf_backend_data *bed;
9829 struct elf_link_hash_entry **sym_hashes;
9830 bfd_size_type address_size;
9831 bfd_vma r_type_mask;
9833 bfd_boolean have_file_sym = FALSE;
9835 output_bfd = flinfo->output_bfd;
9836 bed = get_elf_backend_data (output_bfd);
9837 relocate_section = bed->elf_backend_relocate_section;
9839 /* If this is a dynamic object, we don't want to do anything here:
9840 we don't want the local symbols, and we don't want the section
9842 if ((input_bfd->flags & DYNAMIC) != 0)
9845 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9846 if (elf_bad_symtab (input_bfd))
9848 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9853 locsymcount = symtab_hdr->sh_info;
9854 extsymoff = symtab_hdr->sh_info;
9857 /* Read the local symbols. */
9858 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9859 if (isymbuf == NULL && locsymcount != 0)
9861 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9862 flinfo->internal_syms,
9863 flinfo->external_syms,
9864 flinfo->locsym_shndx);
9865 if (isymbuf == NULL)
9869 /* Find local symbol sections and adjust values of symbols in
9870 SEC_MERGE sections. Write out those local symbols we know are
9871 going into the output file. */
9872 isymend = isymbuf + locsymcount;
9873 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9875 isym++, pindex++, ppsection++)
9879 Elf_Internal_Sym osym;
9885 if (elf_bad_symtab (input_bfd))
9887 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9894 if (isym->st_shndx == SHN_UNDEF)
9895 isec = bfd_und_section_ptr;
9896 else if (isym->st_shndx == SHN_ABS)
9897 isec = bfd_abs_section_ptr;
9898 else if (isym->st_shndx == SHN_COMMON)
9899 isec = bfd_com_section_ptr;
9902 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9905 /* Don't attempt to output symbols with st_shnx in the
9906 reserved range other than SHN_ABS and SHN_COMMON. */
9910 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9911 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9913 _bfd_merged_section_offset (output_bfd, &isec,
9914 elf_section_data (isec)->sec_info,
9920 /* Don't output the first, undefined, symbol. In fact, don't
9921 output any undefined local symbol. */
9922 if (isec == bfd_und_section_ptr)
9925 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9927 /* We never output section symbols. Instead, we use the
9928 section symbol of the corresponding section in the output
9933 /* If we are stripping all symbols, we don't want to output this
9935 if (flinfo->info->strip == strip_all)
9938 /* If we are discarding all local symbols, we don't want to
9939 output this one. If we are generating a relocatable output
9940 file, then some of the local symbols may be required by
9941 relocs; we output them below as we discover that they are
9943 if (flinfo->info->discard == discard_all)
9946 /* If this symbol is defined in a section which we are
9947 discarding, we don't need to keep it. */
9948 if (isym->st_shndx != SHN_UNDEF
9949 && isym->st_shndx < SHN_LORESERVE
9950 && bfd_section_removed_from_list (output_bfd,
9951 isec->output_section))
9954 /* Get the name of the symbol. */
9955 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9960 /* See if we are discarding symbols with this name. */
9961 if ((flinfo->info->strip == strip_some
9962 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9964 || (((flinfo->info->discard == discard_sec_merge
9965 && (isec->flags & SEC_MERGE)
9966 && !bfd_link_relocatable (flinfo->info))
9967 || flinfo->info->discard == discard_l)
9968 && bfd_is_local_label_name (input_bfd, name)))
9971 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9973 if (input_bfd->lto_output)
9974 /* -flto puts a temp file name here. This means builds
9975 are not reproducible. Discard the symbol. */
9977 have_file_sym = TRUE;
9978 flinfo->filesym_count += 1;
9982 /* In the absence of debug info, bfd_find_nearest_line uses
9983 FILE symbols to determine the source file for local
9984 function symbols. Provide a FILE symbol here if input
9985 files lack such, so that their symbols won't be
9986 associated with a previous input file. It's not the
9987 source file, but the best we can do. */
9988 have_file_sym = TRUE;
9989 flinfo->filesym_count += 1;
9990 memset (&osym, 0, sizeof (osym));
9991 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9992 osym.st_shndx = SHN_ABS;
9993 if (!elf_link_output_symstrtab (flinfo,
9994 (input_bfd->lto_output ? NULL
9995 : input_bfd->filename),
9996 &osym, bfd_abs_section_ptr,
10003 /* Adjust the section index for the output file. */
10004 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10005 isec->output_section);
10006 if (osym.st_shndx == SHN_BAD)
10009 /* ELF symbols in relocatable files are section relative, but
10010 in executable files they are virtual addresses. Note that
10011 this code assumes that all ELF sections have an associated
10012 BFD section with a reasonable value for output_offset; below
10013 we assume that they also have a reasonable value for
10014 output_section. Any special sections must be set up to meet
10015 these requirements. */
10016 osym.st_value += isec->output_offset;
10017 if (!bfd_link_relocatable (flinfo->info))
10019 osym.st_value += isec->output_section->vma;
10020 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10022 /* STT_TLS symbols are relative to PT_TLS segment base. */
10023 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10024 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10028 indx = bfd_get_symcount (output_bfd);
10029 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10036 if (bed->s->arch_size == 32)
10038 r_type_mask = 0xff;
10044 r_type_mask = 0xffffffff;
10049 /* Relocate the contents of each section. */
10050 sym_hashes = elf_sym_hashes (input_bfd);
10051 for (o = input_bfd->sections; o != NULL; o = o->next)
10053 bfd_byte *contents;
10055 if (! o->linker_mark)
10057 /* This section was omitted from the link. */
10061 if (bfd_link_relocatable (flinfo->info)
10062 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10064 /* Deal with the group signature symbol. */
10065 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10066 unsigned long symndx = sec_data->this_hdr.sh_info;
10067 asection *osec = o->output_section;
10069 if (symndx >= locsymcount
10070 || (elf_bad_symtab (input_bfd)
10071 && flinfo->sections[symndx] == NULL))
10073 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10074 while (h->root.type == bfd_link_hash_indirect
10075 || h->root.type == bfd_link_hash_warning)
10076 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10077 /* Arrange for symbol to be output. */
10079 elf_section_data (osec)->this_hdr.sh_info = -2;
10081 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10083 /* We'll use the output section target_index. */
10084 asection *sec = flinfo->sections[symndx]->output_section;
10085 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10089 if (flinfo->indices[symndx] == -1)
10091 /* Otherwise output the local symbol now. */
10092 Elf_Internal_Sym sym = isymbuf[symndx];
10093 asection *sec = flinfo->sections[symndx]->output_section;
10098 name = bfd_elf_string_from_elf_section (input_bfd,
10099 symtab_hdr->sh_link,
10104 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10106 if (sym.st_shndx == SHN_BAD)
10109 sym.st_value += o->output_offset;
10111 indx = bfd_get_symcount (output_bfd);
10112 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10117 flinfo->indices[symndx] = indx;
10121 elf_section_data (osec)->this_hdr.sh_info
10122 = flinfo->indices[symndx];
10126 if ((o->flags & SEC_HAS_CONTENTS) == 0
10127 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10130 if ((o->flags & SEC_LINKER_CREATED) != 0)
10132 /* Section was created by _bfd_elf_link_create_dynamic_sections
10137 /* Get the contents of the section. They have been cached by a
10138 relaxation routine. Note that o is a section in an input
10139 file, so the contents field will not have been set by any of
10140 the routines which work on output files. */
10141 if (elf_section_data (o)->this_hdr.contents != NULL)
10143 contents = elf_section_data (o)->this_hdr.contents;
10144 if (bed->caches_rawsize
10146 && o->rawsize < o->size)
10148 memcpy (flinfo->contents, contents, o->rawsize);
10149 contents = flinfo->contents;
10154 contents = flinfo->contents;
10155 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10159 if ((o->flags & SEC_RELOC) != 0)
10161 Elf_Internal_Rela *internal_relocs;
10162 Elf_Internal_Rela *rel, *relend;
10163 int action_discarded;
10166 /* Get the swapped relocs. */
10168 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10169 flinfo->internal_relocs, FALSE);
10170 if (internal_relocs == NULL
10171 && o->reloc_count > 0)
10174 /* We need to reverse-copy input .ctors/.dtors sections if
10175 they are placed in .init_array/.finit_array for output. */
10176 if (o->size > address_size
10177 && ((strncmp (o->name, ".ctors", 6) == 0
10178 && strcmp (o->output_section->name,
10179 ".init_array") == 0)
10180 || (strncmp (o->name, ".dtors", 6) == 0
10181 && strcmp (o->output_section->name,
10182 ".fini_array") == 0))
10183 && (o->name[6] == 0 || o->name[6] == '.'))
10185 if (o->size != o->reloc_count * address_size)
10187 (*_bfd_error_handler)
10188 (_("error: %B: size of section %A is not "
10189 "multiple of address size"),
10191 bfd_set_error (bfd_error_on_input);
10194 o->flags |= SEC_ELF_REVERSE_COPY;
10197 action_discarded = -1;
10198 if (!elf_section_ignore_discarded_relocs (o))
10199 action_discarded = (*bed->action_discarded) (o);
10201 /* Run through the relocs evaluating complex reloc symbols and
10202 looking for relocs against symbols from discarded sections
10203 or section symbols from removed link-once sections.
10204 Complain about relocs against discarded sections. Zero
10205 relocs against removed link-once sections. */
10207 rel = internal_relocs;
10208 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10209 for ( ; rel < relend; rel++)
10211 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10212 unsigned int s_type;
10213 asection **ps, *sec;
10214 struct elf_link_hash_entry *h = NULL;
10215 const char *sym_name;
10217 if (r_symndx == STN_UNDEF)
10220 if (r_symndx >= locsymcount
10221 || (elf_bad_symtab (input_bfd)
10222 && flinfo->sections[r_symndx] == NULL))
10224 h = sym_hashes[r_symndx - extsymoff];
10226 /* Badly formatted input files can contain relocs that
10227 reference non-existant symbols. Check here so that
10228 we do not seg fault. */
10233 sprintf_vma (buffer, rel->r_info);
10234 (*_bfd_error_handler)
10235 (_("error: %B contains a reloc (0x%s) for section %A "
10236 "that references a non-existent global symbol"),
10237 input_bfd, o, buffer);
10238 bfd_set_error (bfd_error_bad_value);
10242 while (h->root.type == bfd_link_hash_indirect
10243 || h->root.type == bfd_link_hash_warning)
10244 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10248 /* If a plugin symbol is referenced from a non-IR file,
10249 mark the symbol as undefined. Note that the
10250 linker may attach linker created dynamic sections
10251 to the plugin bfd. Symbols defined in linker
10252 created sections are not plugin symbols. */
10253 if (h->root.non_ir_ref
10254 && (h->root.type == bfd_link_hash_defined
10255 || h->root.type == bfd_link_hash_defweak)
10256 && (h->root.u.def.section->flags
10257 & SEC_LINKER_CREATED) == 0
10258 && h->root.u.def.section->owner != NULL
10259 && (h->root.u.def.section->owner->flags
10260 & BFD_PLUGIN) != 0)
10262 h->root.type = bfd_link_hash_undefined;
10263 h->root.u.undef.abfd = h->root.u.def.section->owner;
10267 if (h->root.type == bfd_link_hash_defined
10268 || h->root.type == bfd_link_hash_defweak)
10269 ps = &h->root.u.def.section;
10271 sym_name = h->root.root.string;
10275 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10277 s_type = ELF_ST_TYPE (sym->st_info);
10278 ps = &flinfo->sections[r_symndx];
10279 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10283 if ((s_type == STT_RELC || s_type == STT_SRELC)
10284 && !bfd_link_relocatable (flinfo->info))
10287 bfd_vma dot = (rel->r_offset
10288 + o->output_offset + o->output_section->vma);
10290 printf ("Encountered a complex symbol!");
10291 printf (" (input_bfd %s, section %s, reloc %ld\n",
10292 input_bfd->filename, o->name,
10293 (long) (rel - internal_relocs));
10294 printf (" symbol: idx %8.8lx, name %s\n",
10295 r_symndx, sym_name);
10296 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10297 (unsigned long) rel->r_info,
10298 (unsigned long) rel->r_offset);
10300 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10301 isymbuf, locsymcount, s_type == STT_SRELC))
10304 /* Symbol evaluated OK. Update to absolute value. */
10305 set_symbol_value (input_bfd, isymbuf, locsymcount,
10310 if (action_discarded != -1 && ps != NULL)
10312 /* Complain if the definition comes from a
10313 discarded section. */
10314 if ((sec = *ps) != NULL && discarded_section (sec))
10316 BFD_ASSERT (r_symndx != STN_UNDEF);
10317 if (action_discarded & COMPLAIN)
10318 (*flinfo->info->callbacks->einfo)
10319 (_("%X`%s' referenced in section `%A' of %B: "
10320 "defined in discarded section `%A' of %B\n"),
10321 sym_name, o, input_bfd, sec, sec->owner);
10323 /* Try to do the best we can to support buggy old
10324 versions of gcc. Pretend that the symbol is
10325 really defined in the kept linkonce section.
10326 FIXME: This is quite broken. Modifying the
10327 symbol here means we will be changing all later
10328 uses of the symbol, not just in this section. */
10329 if (action_discarded & PRETEND)
10333 kept = _bfd_elf_check_kept_section (sec,
10345 /* Relocate the section by invoking a back end routine.
10347 The back end routine is responsible for adjusting the
10348 section contents as necessary, and (if using Rela relocs
10349 and generating a relocatable output file) adjusting the
10350 reloc addend as necessary.
10352 The back end routine does not have to worry about setting
10353 the reloc address or the reloc symbol index.
10355 The back end routine is given a pointer to the swapped in
10356 internal symbols, and can access the hash table entries
10357 for the external symbols via elf_sym_hashes (input_bfd).
10359 When generating relocatable output, the back end routine
10360 must handle STB_LOCAL/STT_SECTION symbols specially. The
10361 output symbol is going to be a section symbol
10362 corresponding to the output section, which will require
10363 the addend to be adjusted. */
10365 ret = (*relocate_section) (output_bfd, flinfo->info,
10366 input_bfd, o, contents,
10374 || bfd_link_relocatable (flinfo->info)
10375 || flinfo->info->emitrelocations)
10377 Elf_Internal_Rela *irela;
10378 Elf_Internal_Rela *irelaend, *irelamid;
10379 bfd_vma last_offset;
10380 struct elf_link_hash_entry **rel_hash;
10381 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10382 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10383 unsigned int next_erel;
10384 bfd_boolean rela_normal;
10385 struct bfd_elf_section_data *esdi, *esdo;
10387 esdi = elf_section_data (o);
10388 esdo = elf_section_data (o->output_section);
10389 rela_normal = FALSE;
10391 /* Adjust the reloc addresses and symbol indices. */
10393 irela = internal_relocs;
10394 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10395 rel_hash = esdo->rel.hashes + esdo->rel.count;
10396 /* We start processing the REL relocs, if any. When we reach
10397 IRELAMID in the loop, we switch to the RELA relocs. */
10399 if (esdi->rel.hdr != NULL)
10400 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10401 * bed->s->int_rels_per_ext_rel);
10402 rel_hash_list = rel_hash;
10403 rela_hash_list = NULL;
10404 last_offset = o->output_offset;
10405 if (!bfd_link_relocatable (flinfo->info))
10406 last_offset += o->output_section->vma;
10407 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10409 unsigned long r_symndx;
10411 Elf_Internal_Sym sym;
10413 if (next_erel == bed->s->int_rels_per_ext_rel)
10419 if (irela == irelamid)
10421 rel_hash = esdo->rela.hashes + esdo->rela.count;
10422 rela_hash_list = rel_hash;
10423 rela_normal = bed->rela_normal;
10426 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10429 if (irela->r_offset >= (bfd_vma) -2)
10431 /* This is a reloc for a deleted entry or somesuch.
10432 Turn it into an R_*_NONE reloc, at the same
10433 offset as the last reloc. elf_eh_frame.c and
10434 bfd_elf_discard_info rely on reloc offsets
10436 irela->r_offset = last_offset;
10438 irela->r_addend = 0;
10442 irela->r_offset += o->output_offset;
10444 /* Relocs in an executable have to be virtual addresses. */
10445 if (!bfd_link_relocatable (flinfo->info))
10446 irela->r_offset += o->output_section->vma;
10448 last_offset = irela->r_offset;
10450 r_symndx = irela->r_info >> r_sym_shift;
10451 if (r_symndx == STN_UNDEF)
10454 if (r_symndx >= locsymcount
10455 || (elf_bad_symtab (input_bfd)
10456 && flinfo->sections[r_symndx] == NULL))
10458 struct elf_link_hash_entry *rh;
10459 unsigned long indx;
10461 /* This is a reloc against a global symbol. We
10462 have not yet output all the local symbols, so
10463 we do not know the symbol index of any global
10464 symbol. We set the rel_hash entry for this
10465 reloc to point to the global hash table entry
10466 for this symbol. The symbol index is then
10467 set at the end of bfd_elf_final_link. */
10468 indx = r_symndx - extsymoff;
10469 rh = elf_sym_hashes (input_bfd)[indx];
10470 while (rh->root.type == bfd_link_hash_indirect
10471 || rh->root.type == bfd_link_hash_warning)
10472 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10474 /* Setting the index to -2 tells
10475 elf_link_output_extsym that this symbol is
10476 used by a reloc. */
10477 BFD_ASSERT (rh->indx < 0);
10485 /* This is a reloc against a local symbol. */
10488 sym = isymbuf[r_symndx];
10489 sec = flinfo->sections[r_symndx];
10490 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10492 /* I suppose the backend ought to fill in the
10493 section of any STT_SECTION symbol against a
10494 processor specific section. */
10495 r_symndx = STN_UNDEF;
10496 if (bfd_is_abs_section (sec))
10498 else if (sec == NULL || sec->owner == NULL)
10500 bfd_set_error (bfd_error_bad_value);
10505 asection *osec = sec->output_section;
10507 /* If we have discarded a section, the output
10508 section will be the absolute section. In
10509 case of discarded SEC_MERGE sections, use
10510 the kept section. relocate_section should
10511 have already handled discarded linkonce
10513 if (bfd_is_abs_section (osec)
10514 && sec->kept_section != NULL
10515 && sec->kept_section->output_section != NULL)
10517 osec = sec->kept_section->output_section;
10518 irela->r_addend -= osec->vma;
10521 if (!bfd_is_abs_section (osec))
10523 r_symndx = osec->target_index;
10524 if (r_symndx == STN_UNDEF)
10526 irela->r_addend += osec->vma;
10527 osec = _bfd_nearby_section (output_bfd, osec,
10529 irela->r_addend -= osec->vma;
10530 r_symndx = osec->target_index;
10535 /* Adjust the addend according to where the
10536 section winds up in the output section. */
10538 irela->r_addend += sec->output_offset;
10542 if (flinfo->indices[r_symndx] == -1)
10544 unsigned long shlink;
10549 if (flinfo->info->strip == strip_all)
10551 /* You can't do ld -r -s. */
10552 bfd_set_error (bfd_error_invalid_operation);
10556 /* This symbol was skipped earlier, but
10557 since it is needed by a reloc, we
10558 must output it now. */
10559 shlink = symtab_hdr->sh_link;
10560 name = (bfd_elf_string_from_elf_section
10561 (input_bfd, shlink, sym.st_name));
10565 osec = sec->output_section;
10567 _bfd_elf_section_from_bfd_section (output_bfd,
10569 if (sym.st_shndx == SHN_BAD)
10572 sym.st_value += sec->output_offset;
10573 if (!bfd_link_relocatable (flinfo->info))
10575 sym.st_value += osec->vma;
10576 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10578 /* STT_TLS symbols are relative to PT_TLS
10580 BFD_ASSERT (elf_hash_table (flinfo->info)
10581 ->tls_sec != NULL);
10582 sym.st_value -= (elf_hash_table (flinfo->info)
10587 indx = bfd_get_symcount (output_bfd);
10588 ret = elf_link_output_symstrtab (flinfo, name,
10594 flinfo->indices[r_symndx] = indx;
10599 r_symndx = flinfo->indices[r_symndx];
10602 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10603 | (irela->r_info & r_type_mask));
10606 /* Swap out the relocs. */
10607 input_rel_hdr = esdi->rel.hdr;
10608 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10610 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10615 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10616 * bed->s->int_rels_per_ext_rel);
10617 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10620 input_rela_hdr = esdi->rela.hdr;
10621 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10623 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10632 /* Write out the modified section contents. */
10633 if (bed->elf_backend_write_section
10634 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10637 /* Section written out. */
10639 else switch (o->sec_info_type)
10641 case SEC_INFO_TYPE_STABS:
10642 if (! (_bfd_write_section_stabs
10644 &elf_hash_table (flinfo->info)->stab_info,
10645 o, &elf_section_data (o)->sec_info, contents)))
10648 case SEC_INFO_TYPE_MERGE:
10649 if (! _bfd_write_merged_section (output_bfd, o,
10650 elf_section_data (o)->sec_info))
10653 case SEC_INFO_TYPE_EH_FRAME:
10655 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10660 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10662 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10670 if (! (o->flags & SEC_EXCLUDE))
10672 file_ptr offset = (file_ptr) o->output_offset;
10673 bfd_size_type todo = o->size;
10675 offset *= bfd_octets_per_byte (output_bfd);
10677 if ((o->flags & SEC_ELF_REVERSE_COPY))
10679 /* Reverse-copy input section to output. */
10682 todo -= address_size;
10683 if (! bfd_set_section_contents (output_bfd,
10691 offset += address_size;
10695 else if (! bfd_set_section_contents (output_bfd,
10709 /* Generate a reloc when linking an ELF file. This is a reloc
10710 requested by the linker, and does not come from any input file. This
10711 is used to build constructor and destructor tables when linking
10715 elf_reloc_link_order (bfd *output_bfd,
10716 struct bfd_link_info *info,
10717 asection *output_section,
10718 struct bfd_link_order *link_order)
10720 reloc_howto_type *howto;
10724 struct bfd_elf_section_reloc_data *reldata;
10725 struct elf_link_hash_entry **rel_hash_ptr;
10726 Elf_Internal_Shdr *rel_hdr;
10727 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10728 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10731 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10733 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10736 bfd_set_error (bfd_error_bad_value);
10740 addend = link_order->u.reloc.p->addend;
10743 reldata = &esdo->rel;
10744 else if (esdo->rela.hdr)
10745 reldata = &esdo->rela;
10752 /* Figure out the symbol index. */
10753 rel_hash_ptr = reldata->hashes + reldata->count;
10754 if (link_order->type == bfd_section_reloc_link_order)
10756 indx = link_order->u.reloc.p->u.section->target_index;
10757 BFD_ASSERT (indx != 0);
10758 *rel_hash_ptr = NULL;
10762 struct elf_link_hash_entry *h;
10764 /* Treat a reloc against a defined symbol as though it were
10765 actually against the section. */
10766 h = ((struct elf_link_hash_entry *)
10767 bfd_wrapped_link_hash_lookup (output_bfd, info,
10768 link_order->u.reloc.p->u.name,
10769 FALSE, FALSE, TRUE));
10771 && (h->root.type == bfd_link_hash_defined
10772 || h->root.type == bfd_link_hash_defweak))
10776 section = h->root.u.def.section;
10777 indx = section->output_section->target_index;
10778 *rel_hash_ptr = NULL;
10779 /* It seems that we ought to add the symbol value to the
10780 addend here, but in practice it has already been added
10781 because it was passed to constructor_callback. */
10782 addend += section->output_section->vma + section->output_offset;
10784 else if (h != NULL)
10786 /* Setting the index to -2 tells elf_link_output_extsym that
10787 this symbol is used by a reloc. */
10794 (*info->callbacks->unattached_reloc)
10795 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
10800 /* If this is an inplace reloc, we must write the addend into the
10802 if (howto->partial_inplace && addend != 0)
10804 bfd_size_type size;
10805 bfd_reloc_status_type rstat;
10808 const char *sym_name;
10810 size = (bfd_size_type) bfd_get_reloc_size (howto);
10811 buf = (bfd_byte *) bfd_zmalloc (size);
10812 if (buf == NULL && size != 0)
10814 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10821 case bfd_reloc_outofrange:
10824 case bfd_reloc_overflow:
10825 if (link_order->type == bfd_section_reloc_link_order)
10826 sym_name = bfd_section_name (output_bfd,
10827 link_order->u.reloc.p->u.section);
10829 sym_name = link_order->u.reloc.p->u.name;
10830 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10831 howto->name, addend, NULL, NULL,
10836 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10838 * bfd_octets_per_byte (output_bfd),
10845 /* The address of a reloc is relative to the section in a
10846 relocatable file, and is a virtual address in an executable
10848 offset = link_order->offset;
10849 if (! bfd_link_relocatable (info))
10850 offset += output_section->vma;
10852 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10854 irel[i].r_offset = offset;
10855 irel[i].r_info = 0;
10856 irel[i].r_addend = 0;
10858 if (bed->s->arch_size == 32)
10859 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10861 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10863 rel_hdr = reldata->hdr;
10864 erel = rel_hdr->contents;
10865 if (rel_hdr->sh_type == SHT_REL)
10867 erel += reldata->count * bed->s->sizeof_rel;
10868 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10872 irel[0].r_addend = addend;
10873 erel += reldata->count * bed->s->sizeof_rela;
10874 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10883 /* Get the output vma of the section pointed to by the sh_link field. */
10886 elf_get_linked_section_vma (struct bfd_link_order *p)
10888 Elf_Internal_Shdr **elf_shdrp;
10892 s = p->u.indirect.section;
10893 elf_shdrp = elf_elfsections (s->owner);
10894 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10895 elfsec = elf_shdrp[elfsec]->sh_link;
10897 The Intel C compiler generates SHT_IA_64_UNWIND with
10898 SHF_LINK_ORDER. But it doesn't set the sh_link or
10899 sh_info fields. Hence we could get the situation
10900 where elfsec is 0. */
10903 const struct elf_backend_data *bed
10904 = get_elf_backend_data (s->owner);
10905 if (bed->link_order_error_handler)
10906 bed->link_order_error_handler
10907 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10912 s = elf_shdrp[elfsec]->bfd_section;
10913 return s->output_section->vma + s->output_offset;
10918 /* Compare two sections based on the locations of the sections they are
10919 linked to. Used by elf_fixup_link_order. */
10922 compare_link_order (const void * a, const void * b)
10927 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10928 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10931 return apos > bpos;
10935 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10936 order as their linked sections. Returns false if this could not be done
10937 because an output section includes both ordered and unordered
10938 sections. Ideally we'd do this in the linker proper. */
10941 elf_fixup_link_order (bfd *abfd, asection *o)
10943 int seen_linkorder;
10946 struct bfd_link_order *p;
10948 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10950 struct bfd_link_order **sections;
10951 asection *s, *other_sec, *linkorder_sec;
10955 linkorder_sec = NULL;
10957 seen_linkorder = 0;
10958 for (p = o->map_head.link_order; p != NULL; p = p->next)
10960 if (p->type == bfd_indirect_link_order)
10962 s = p->u.indirect.section;
10964 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10965 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10966 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10967 && elfsec < elf_numsections (sub)
10968 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10969 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10983 if (seen_other && seen_linkorder)
10985 if (other_sec && linkorder_sec)
10986 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10988 linkorder_sec->owner, other_sec,
10991 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10993 bfd_set_error (bfd_error_bad_value);
10998 if (!seen_linkorder)
11001 sections = (struct bfd_link_order **)
11002 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11003 if (sections == NULL)
11005 seen_linkorder = 0;
11007 for (p = o->map_head.link_order; p != NULL; p = p->next)
11009 sections[seen_linkorder++] = p;
11011 /* Sort the input sections in the order of their linked section. */
11012 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11013 compare_link_order);
11015 /* Change the offsets of the sections. */
11017 for (n = 0; n < seen_linkorder; n++)
11019 s = sections[n]->u.indirect.section;
11020 offset &= ~(bfd_vma) 0 << s->alignment_power;
11021 s->output_offset = offset / bfd_octets_per_byte (abfd);
11022 sections[n]->offset = offset;
11023 offset += sections[n]->size;
11031 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11035 if (flinfo->symstrtab != NULL)
11036 _bfd_elf_strtab_free (flinfo->symstrtab);
11037 if (flinfo->contents != NULL)
11038 free (flinfo->contents);
11039 if (flinfo->external_relocs != NULL)
11040 free (flinfo->external_relocs);
11041 if (flinfo->internal_relocs != NULL)
11042 free (flinfo->internal_relocs);
11043 if (flinfo->external_syms != NULL)
11044 free (flinfo->external_syms);
11045 if (flinfo->locsym_shndx != NULL)
11046 free (flinfo->locsym_shndx);
11047 if (flinfo->internal_syms != NULL)
11048 free (flinfo->internal_syms);
11049 if (flinfo->indices != NULL)
11050 free (flinfo->indices);
11051 if (flinfo->sections != NULL)
11052 free (flinfo->sections);
11053 if (flinfo->symshndxbuf != NULL)
11054 free (flinfo->symshndxbuf);
11055 for (o = obfd->sections; o != NULL; o = o->next)
11057 struct bfd_elf_section_data *esdo = elf_section_data (o);
11058 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11059 free (esdo->rel.hashes);
11060 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11061 free (esdo->rela.hashes);
11065 /* Do the final step of an ELF link. */
11068 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11070 bfd_boolean dynamic;
11071 bfd_boolean emit_relocs;
11073 struct elf_final_link_info flinfo;
11075 struct bfd_link_order *p;
11077 bfd_size_type max_contents_size;
11078 bfd_size_type max_external_reloc_size;
11079 bfd_size_type max_internal_reloc_count;
11080 bfd_size_type max_sym_count;
11081 bfd_size_type max_sym_shndx_count;
11082 Elf_Internal_Sym elfsym;
11084 Elf_Internal_Shdr *symtab_hdr;
11085 Elf_Internal_Shdr *symtab_shndx_hdr;
11086 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11087 struct elf_outext_info eoinfo;
11088 bfd_boolean merged;
11089 size_t relativecount = 0;
11090 asection *reldyn = 0;
11092 asection *attr_section = NULL;
11093 bfd_vma attr_size = 0;
11094 const char *std_attrs_section;
11096 if (! is_elf_hash_table (info->hash))
11099 if (bfd_link_pic (info))
11100 abfd->flags |= DYNAMIC;
11102 dynamic = elf_hash_table (info)->dynamic_sections_created;
11103 dynobj = elf_hash_table (info)->dynobj;
11105 emit_relocs = (bfd_link_relocatable (info)
11106 || info->emitrelocations);
11108 flinfo.info = info;
11109 flinfo.output_bfd = abfd;
11110 flinfo.symstrtab = _bfd_elf_strtab_init ();
11111 if (flinfo.symstrtab == NULL)
11116 flinfo.hash_sec = NULL;
11117 flinfo.symver_sec = NULL;
11121 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11122 /* Note that dynsym_sec can be NULL (on VMS). */
11123 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11124 /* Note that it is OK if symver_sec is NULL. */
11127 flinfo.contents = NULL;
11128 flinfo.external_relocs = NULL;
11129 flinfo.internal_relocs = NULL;
11130 flinfo.external_syms = NULL;
11131 flinfo.locsym_shndx = NULL;
11132 flinfo.internal_syms = NULL;
11133 flinfo.indices = NULL;
11134 flinfo.sections = NULL;
11135 flinfo.symshndxbuf = NULL;
11136 flinfo.filesym_count = 0;
11138 /* The object attributes have been merged. Remove the input
11139 sections from the link, and set the contents of the output
11141 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11142 for (o = abfd->sections; o != NULL; o = o->next)
11144 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11145 || strcmp (o->name, ".gnu.attributes") == 0)
11147 for (p = o->map_head.link_order; p != NULL; p = p->next)
11149 asection *input_section;
11151 if (p->type != bfd_indirect_link_order)
11153 input_section = p->u.indirect.section;
11154 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11155 elf_link_input_bfd ignores this section. */
11156 input_section->flags &= ~SEC_HAS_CONTENTS;
11159 attr_size = bfd_elf_obj_attr_size (abfd);
11162 bfd_set_section_size (abfd, o, attr_size);
11164 /* Skip this section later on. */
11165 o->map_head.link_order = NULL;
11168 o->flags |= SEC_EXCLUDE;
11172 /* Count up the number of relocations we will output for each output
11173 section, so that we know the sizes of the reloc sections. We
11174 also figure out some maximum sizes. */
11175 max_contents_size = 0;
11176 max_external_reloc_size = 0;
11177 max_internal_reloc_count = 0;
11179 max_sym_shndx_count = 0;
11181 for (o = abfd->sections; o != NULL; o = o->next)
11183 struct bfd_elf_section_data *esdo = elf_section_data (o);
11184 o->reloc_count = 0;
11186 for (p = o->map_head.link_order; p != NULL; p = p->next)
11188 unsigned int reloc_count = 0;
11189 unsigned int additional_reloc_count = 0;
11190 struct bfd_elf_section_data *esdi = NULL;
11192 if (p->type == bfd_section_reloc_link_order
11193 || p->type == bfd_symbol_reloc_link_order)
11195 else if (p->type == bfd_indirect_link_order)
11199 sec = p->u.indirect.section;
11200 esdi = elf_section_data (sec);
11202 /* Mark all sections which are to be included in the
11203 link. This will normally be every section. We need
11204 to do this so that we can identify any sections which
11205 the linker has decided to not include. */
11206 sec->linker_mark = TRUE;
11208 if (sec->flags & SEC_MERGE)
11211 if (esdo->this_hdr.sh_type == SHT_REL
11212 || esdo->this_hdr.sh_type == SHT_RELA)
11213 /* Some backends use reloc_count in relocation sections
11214 to count particular types of relocs. Of course,
11215 reloc sections themselves can't have relocations. */
11217 else if (emit_relocs)
11219 reloc_count = sec->reloc_count;
11220 if (bed->elf_backend_count_additional_relocs)
11223 c = (*bed->elf_backend_count_additional_relocs) (sec);
11224 additional_reloc_count += c;
11227 else if (bed->elf_backend_count_relocs)
11228 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11230 if (sec->rawsize > max_contents_size)
11231 max_contents_size = sec->rawsize;
11232 if (sec->size > max_contents_size)
11233 max_contents_size = sec->size;
11235 /* We are interested in just local symbols, not all
11237 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11238 && (sec->owner->flags & DYNAMIC) == 0)
11242 if (elf_bad_symtab (sec->owner))
11243 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11244 / bed->s->sizeof_sym);
11246 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11248 if (sym_count > max_sym_count)
11249 max_sym_count = sym_count;
11251 if (sym_count > max_sym_shndx_count
11252 && elf_symtab_shndx_list (sec->owner) != NULL)
11253 max_sym_shndx_count = sym_count;
11255 if ((sec->flags & SEC_RELOC) != 0)
11257 size_t ext_size = 0;
11259 if (esdi->rel.hdr != NULL)
11260 ext_size = esdi->rel.hdr->sh_size;
11261 if (esdi->rela.hdr != NULL)
11262 ext_size += esdi->rela.hdr->sh_size;
11264 if (ext_size > max_external_reloc_size)
11265 max_external_reloc_size = ext_size;
11266 if (sec->reloc_count > max_internal_reloc_count)
11267 max_internal_reloc_count = sec->reloc_count;
11272 if (reloc_count == 0)
11275 reloc_count += additional_reloc_count;
11276 o->reloc_count += reloc_count;
11278 if (p->type == bfd_indirect_link_order && emit_relocs)
11282 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11283 esdo->rel.count += additional_reloc_count;
11285 if (esdi->rela.hdr)
11287 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11288 esdo->rela.count += additional_reloc_count;
11294 esdo->rela.count += reloc_count;
11296 esdo->rel.count += reloc_count;
11300 if (o->reloc_count > 0)
11301 o->flags |= SEC_RELOC;
11304 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11305 set it (this is probably a bug) and if it is set
11306 assign_section_numbers will create a reloc section. */
11307 o->flags &=~ SEC_RELOC;
11310 /* If the SEC_ALLOC flag is not set, force the section VMA to
11311 zero. This is done in elf_fake_sections as well, but forcing
11312 the VMA to 0 here will ensure that relocs against these
11313 sections are handled correctly. */
11314 if ((o->flags & SEC_ALLOC) == 0
11315 && ! o->user_set_vma)
11319 if (! bfd_link_relocatable (info) && merged)
11320 elf_link_hash_traverse (elf_hash_table (info),
11321 _bfd_elf_link_sec_merge_syms, abfd);
11323 /* Figure out the file positions for everything but the symbol table
11324 and the relocs. We set symcount to force assign_section_numbers
11325 to create a symbol table. */
11326 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11327 BFD_ASSERT (! abfd->output_has_begun);
11328 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11331 /* Set sizes, and assign file positions for reloc sections. */
11332 for (o = abfd->sections; o != NULL; o = o->next)
11334 struct bfd_elf_section_data *esdo = elf_section_data (o);
11335 if ((o->flags & SEC_RELOC) != 0)
11338 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11342 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11346 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11347 to count upwards while actually outputting the relocations. */
11348 esdo->rel.count = 0;
11349 esdo->rela.count = 0;
11351 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11353 /* Cache the section contents so that they can be compressed
11354 later. Use bfd_malloc since it will be freed by
11355 bfd_compress_section_contents. */
11356 unsigned char *contents = esdo->this_hdr.contents;
11357 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11360 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11361 if (contents == NULL)
11363 esdo->this_hdr.contents = contents;
11367 /* We have now assigned file positions for all the sections except
11368 .symtab, .strtab, and non-loaded reloc sections. We start the
11369 .symtab section at the current file position, and write directly
11370 to it. We build the .strtab section in memory. */
11371 bfd_get_symcount (abfd) = 0;
11372 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11373 /* sh_name is set in prep_headers. */
11374 symtab_hdr->sh_type = SHT_SYMTAB;
11375 /* sh_flags, sh_addr and sh_size all start off zero. */
11376 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11377 /* sh_link is set in assign_section_numbers. */
11378 /* sh_info is set below. */
11379 /* sh_offset is set just below. */
11380 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11382 if (max_sym_count < 20)
11383 max_sym_count = 20;
11384 elf_hash_table (info)->strtabsize = max_sym_count;
11385 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11386 elf_hash_table (info)->strtab
11387 = (struct elf_sym_strtab *) bfd_malloc (amt);
11388 if (elf_hash_table (info)->strtab == NULL)
11390 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11392 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11393 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11395 if (info->strip != strip_all || emit_relocs)
11397 file_ptr off = elf_next_file_pos (abfd);
11399 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11401 /* Note that at this point elf_next_file_pos (abfd) is
11402 incorrect. We do not yet know the size of the .symtab section.
11403 We correct next_file_pos below, after we do know the size. */
11405 /* Start writing out the symbol table. The first symbol is always a
11407 elfsym.st_value = 0;
11408 elfsym.st_size = 0;
11409 elfsym.st_info = 0;
11410 elfsym.st_other = 0;
11411 elfsym.st_shndx = SHN_UNDEF;
11412 elfsym.st_target_internal = 0;
11413 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11414 bfd_und_section_ptr, NULL) != 1)
11417 /* Output a symbol for each section. We output these even if we are
11418 discarding local symbols, since they are used for relocs. These
11419 symbols have no names. We store the index of each one in the
11420 index field of the section, so that we can find it again when
11421 outputting relocs. */
11423 elfsym.st_size = 0;
11424 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11425 elfsym.st_other = 0;
11426 elfsym.st_value = 0;
11427 elfsym.st_target_internal = 0;
11428 for (i = 1; i < elf_numsections (abfd); i++)
11430 o = bfd_section_from_elf_index (abfd, i);
11433 o->target_index = bfd_get_symcount (abfd);
11434 elfsym.st_shndx = i;
11435 if (!bfd_link_relocatable (info))
11436 elfsym.st_value = o->vma;
11437 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11444 /* Allocate some memory to hold information read in from the input
11446 if (max_contents_size != 0)
11448 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11449 if (flinfo.contents == NULL)
11453 if (max_external_reloc_size != 0)
11455 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11456 if (flinfo.external_relocs == NULL)
11460 if (max_internal_reloc_count != 0)
11462 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11463 amt *= sizeof (Elf_Internal_Rela);
11464 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11465 if (flinfo.internal_relocs == NULL)
11469 if (max_sym_count != 0)
11471 amt = max_sym_count * bed->s->sizeof_sym;
11472 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11473 if (flinfo.external_syms == NULL)
11476 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11477 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11478 if (flinfo.internal_syms == NULL)
11481 amt = max_sym_count * sizeof (long);
11482 flinfo.indices = (long int *) bfd_malloc (amt);
11483 if (flinfo.indices == NULL)
11486 amt = max_sym_count * sizeof (asection *);
11487 flinfo.sections = (asection **) bfd_malloc (amt);
11488 if (flinfo.sections == NULL)
11492 if (max_sym_shndx_count != 0)
11494 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11495 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11496 if (flinfo.locsym_shndx == NULL)
11500 if (elf_hash_table (info)->tls_sec)
11502 bfd_vma base, end = 0;
11505 for (sec = elf_hash_table (info)->tls_sec;
11506 sec && (sec->flags & SEC_THREAD_LOCAL);
11509 bfd_size_type size = sec->size;
11512 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11514 struct bfd_link_order *ord = sec->map_tail.link_order;
11517 size = ord->offset + ord->size;
11519 end = sec->vma + size;
11521 base = elf_hash_table (info)->tls_sec->vma;
11522 /* Only align end of TLS section if static TLS doesn't have special
11523 alignment requirements. */
11524 if (bed->static_tls_alignment == 1)
11525 end = align_power (end,
11526 elf_hash_table (info)->tls_sec->alignment_power);
11527 elf_hash_table (info)->tls_size = end - base;
11530 /* Reorder SHF_LINK_ORDER sections. */
11531 for (o = abfd->sections; o != NULL; o = o->next)
11533 if (!elf_fixup_link_order (abfd, o))
11537 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11540 /* Since ELF permits relocations to be against local symbols, we
11541 must have the local symbols available when we do the relocations.
11542 Since we would rather only read the local symbols once, and we
11543 would rather not keep them in memory, we handle all the
11544 relocations for a single input file at the same time.
11546 Unfortunately, there is no way to know the total number of local
11547 symbols until we have seen all of them, and the local symbol
11548 indices precede the global symbol indices. This means that when
11549 we are generating relocatable output, and we see a reloc against
11550 a global symbol, we can not know the symbol index until we have
11551 finished examining all the local symbols to see which ones we are
11552 going to output. To deal with this, we keep the relocations in
11553 memory, and don't output them until the end of the link. This is
11554 an unfortunate waste of memory, but I don't see a good way around
11555 it. Fortunately, it only happens when performing a relocatable
11556 link, which is not the common case. FIXME: If keep_memory is set
11557 we could write the relocs out and then read them again; I don't
11558 know how bad the memory loss will be. */
11560 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11561 sub->output_has_begun = FALSE;
11562 for (o = abfd->sections; o != NULL; o = o->next)
11564 for (p = o->map_head.link_order; p != NULL; p = p->next)
11566 if (p->type == bfd_indirect_link_order
11567 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11568 == bfd_target_elf_flavour)
11569 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11571 if (! sub->output_has_begun)
11573 if (! elf_link_input_bfd (&flinfo, sub))
11575 sub->output_has_begun = TRUE;
11578 else if (p->type == bfd_section_reloc_link_order
11579 || p->type == bfd_symbol_reloc_link_order)
11581 if (! elf_reloc_link_order (abfd, info, o, p))
11586 if (! _bfd_default_link_order (abfd, info, o, p))
11588 if (p->type == bfd_indirect_link_order
11589 && (bfd_get_flavour (sub)
11590 == bfd_target_elf_flavour)
11591 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11592 != bed->s->elfclass))
11594 const char *iclass, *oclass;
11596 switch (bed->s->elfclass)
11598 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11599 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11600 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11604 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11606 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11607 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11608 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11612 bfd_set_error (bfd_error_wrong_format);
11613 (*_bfd_error_handler)
11614 (_("%B: file class %s incompatible with %s"),
11615 sub, iclass, oclass);
11624 /* Free symbol buffer if needed. */
11625 if (!info->reduce_memory_overheads)
11627 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11628 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11629 && elf_tdata (sub)->symbuf)
11631 free (elf_tdata (sub)->symbuf);
11632 elf_tdata (sub)->symbuf = NULL;
11636 /* Output any global symbols that got converted to local in a
11637 version script or due to symbol visibility. We do this in a
11638 separate step since ELF requires all local symbols to appear
11639 prior to any global symbols. FIXME: We should only do this if
11640 some global symbols were, in fact, converted to become local.
11641 FIXME: Will this work correctly with the Irix 5 linker? */
11642 eoinfo.failed = FALSE;
11643 eoinfo.flinfo = &flinfo;
11644 eoinfo.localsyms = TRUE;
11645 eoinfo.file_sym_done = FALSE;
11646 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11650 /* If backend needs to output some local symbols not present in the hash
11651 table, do it now. */
11652 if (bed->elf_backend_output_arch_local_syms
11653 && (info->strip != strip_all || emit_relocs))
11655 typedef int (*out_sym_func)
11656 (void *, const char *, Elf_Internal_Sym *, asection *,
11657 struct elf_link_hash_entry *);
11659 if (! ((*bed->elf_backend_output_arch_local_syms)
11660 (abfd, info, &flinfo,
11661 (out_sym_func) elf_link_output_symstrtab)))
11665 /* That wrote out all the local symbols. Finish up the symbol table
11666 with the global symbols. Even if we want to strip everything we
11667 can, we still need to deal with those global symbols that got
11668 converted to local in a version script. */
11670 /* The sh_info field records the index of the first non local symbol. */
11671 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11674 && elf_hash_table (info)->dynsym != NULL
11675 && (elf_hash_table (info)->dynsym->output_section
11676 != bfd_abs_section_ptr))
11678 Elf_Internal_Sym sym;
11679 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
11680 long last_local = 0;
11682 /* Write out the section symbols for the output sections. */
11683 if (bfd_link_pic (info)
11684 || elf_hash_table (info)->is_relocatable_executable)
11690 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11692 sym.st_target_internal = 0;
11694 for (s = abfd->sections; s != NULL; s = s->next)
11700 dynindx = elf_section_data (s)->dynindx;
11703 indx = elf_section_data (s)->this_idx;
11704 BFD_ASSERT (indx > 0);
11705 sym.st_shndx = indx;
11706 if (! check_dynsym (abfd, &sym))
11708 sym.st_value = s->vma;
11709 dest = dynsym + dynindx * bed->s->sizeof_sym;
11710 if (last_local < dynindx)
11711 last_local = dynindx;
11712 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11716 /* Write out the local dynsyms. */
11717 if (elf_hash_table (info)->dynlocal)
11719 struct elf_link_local_dynamic_entry *e;
11720 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11725 /* Copy the internal symbol and turn off visibility.
11726 Note that we saved a word of storage and overwrote
11727 the original st_name with the dynstr_index. */
11729 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11731 s = bfd_section_from_elf_index (e->input_bfd,
11736 elf_section_data (s->output_section)->this_idx;
11737 if (! check_dynsym (abfd, &sym))
11739 sym.st_value = (s->output_section->vma
11741 + e->isym.st_value);
11744 if (last_local < e->dynindx)
11745 last_local = e->dynindx;
11747 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11748 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11752 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
11756 /* We get the global symbols from the hash table. */
11757 eoinfo.failed = FALSE;
11758 eoinfo.localsyms = FALSE;
11759 eoinfo.flinfo = &flinfo;
11760 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11764 /* If backend needs to output some symbols not present in the hash
11765 table, do it now. */
11766 if (bed->elf_backend_output_arch_syms
11767 && (info->strip != strip_all || emit_relocs))
11769 typedef int (*out_sym_func)
11770 (void *, const char *, Elf_Internal_Sym *, asection *,
11771 struct elf_link_hash_entry *);
11773 if (! ((*bed->elf_backend_output_arch_syms)
11774 (abfd, info, &flinfo,
11775 (out_sym_func) elf_link_output_symstrtab)))
11779 /* Finalize the .strtab section. */
11780 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11782 /* Swap out the .strtab section. */
11783 if (!elf_link_swap_symbols_out (&flinfo))
11786 /* Now we know the size of the symtab section. */
11787 if (bfd_get_symcount (abfd) > 0)
11789 /* Finish up and write out the symbol string table (.strtab)
11791 Elf_Internal_Shdr *symstrtab_hdr;
11792 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11794 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11795 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11797 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11798 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11799 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11800 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11801 symtab_shndx_hdr->sh_size = amt;
11803 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11806 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11807 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11811 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11812 /* sh_name was set in prep_headers. */
11813 symstrtab_hdr->sh_type = SHT_STRTAB;
11814 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
11815 symstrtab_hdr->sh_addr = 0;
11816 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11817 symstrtab_hdr->sh_entsize = 0;
11818 symstrtab_hdr->sh_link = 0;
11819 symstrtab_hdr->sh_info = 0;
11820 /* sh_offset is set just below. */
11821 symstrtab_hdr->sh_addralign = 1;
11823 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11825 elf_next_file_pos (abfd) = off;
11827 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11828 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11832 /* Adjust the relocs to have the correct symbol indices. */
11833 for (o = abfd->sections; o != NULL; o = o->next)
11835 struct bfd_elf_section_data *esdo = elf_section_data (o);
11837 if ((o->flags & SEC_RELOC) == 0)
11840 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11841 if (esdo->rel.hdr != NULL
11842 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11844 if (esdo->rela.hdr != NULL
11845 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11848 /* Set the reloc_count field to 0 to prevent write_relocs from
11849 trying to swap the relocs out itself. */
11850 o->reloc_count = 0;
11853 if (dynamic && info->combreloc && dynobj != NULL)
11854 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11856 /* If we are linking against a dynamic object, or generating a
11857 shared library, finish up the dynamic linking information. */
11860 bfd_byte *dyncon, *dynconend;
11862 /* Fix up .dynamic entries. */
11863 o = bfd_get_linker_section (dynobj, ".dynamic");
11864 BFD_ASSERT (o != NULL);
11866 dyncon = o->contents;
11867 dynconend = o->contents + o->size;
11868 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11870 Elf_Internal_Dyn dyn;
11874 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11881 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11883 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11885 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11886 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11889 dyn.d_un.d_val = relativecount;
11896 name = info->init_function;
11899 name = info->fini_function;
11902 struct elf_link_hash_entry *h;
11904 h = elf_link_hash_lookup (elf_hash_table (info), name,
11905 FALSE, FALSE, TRUE);
11907 && (h->root.type == bfd_link_hash_defined
11908 || h->root.type == bfd_link_hash_defweak))
11910 dyn.d_un.d_ptr = h->root.u.def.value;
11911 o = h->root.u.def.section;
11912 if (o->output_section != NULL)
11913 dyn.d_un.d_ptr += (o->output_section->vma
11914 + o->output_offset);
11917 /* The symbol is imported from another shared
11918 library and does not apply to this one. */
11919 dyn.d_un.d_ptr = 0;
11926 case DT_PREINIT_ARRAYSZ:
11927 name = ".preinit_array";
11929 case DT_INIT_ARRAYSZ:
11930 name = ".init_array";
11932 case DT_FINI_ARRAYSZ:
11933 name = ".fini_array";
11935 o = bfd_get_section_by_name (abfd, name);
11938 (*_bfd_error_handler)
11939 (_("could not find section %s"), name);
11943 (*_bfd_error_handler)
11944 (_("warning: %s section has zero size"), name);
11945 dyn.d_un.d_val = o->size;
11948 case DT_PREINIT_ARRAY:
11949 name = ".preinit_array";
11951 case DT_INIT_ARRAY:
11952 name = ".init_array";
11954 case DT_FINI_ARRAY:
11955 name = ".fini_array";
11957 o = bfd_get_section_by_name (abfd, name);
11964 name = ".gnu.hash";
11973 name = ".gnu.version_d";
11976 name = ".gnu.version_r";
11979 name = ".gnu.version";
11981 o = bfd_get_linker_section (dynobj, name);
11985 (*_bfd_error_handler)
11986 (_("could not find section %s"), name);
11989 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11991 (*_bfd_error_handler)
11992 (_("warning: section '%s' is being made into a note"), name);
11993 bfd_set_error (bfd_error_nonrepresentable_section);
11996 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12003 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12007 dyn.d_un.d_val = 0;
12008 dyn.d_un.d_ptr = 0;
12009 for (i = 1; i < elf_numsections (abfd); i++)
12011 Elf_Internal_Shdr *hdr;
12013 hdr = elf_elfsections (abfd)[i];
12014 if (hdr->sh_type == type
12015 && (hdr->sh_flags & SHF_ALLOC) != 0)
12017 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12018 dyn.d_un.d_val += hdr->sh_size;
12021 if (dyn.d_un.d_ptr == 0
12022 || hdr->sh_addr < dyn.d_un.d_ptr)
12023 dyn.d_un.d_ptr = hdr->sh_addr;
12029 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12033 /* If we have created any dynamic sections, then output them. */
12034 if (dynobj != NULL)
12036 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12039 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12040 if (((info->warn_shared_textrel && bfd_link_pic (info))
12041 || info->error_textrel)
12042 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12044 bfd_byte *dyncon, *dynconend;
12046 dyncon = o->contents;
12047 dynconend = o->contents + o->size;
12048 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12050 Elf_Internal_Dyn dyn;
12052 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12054 if (dyn.d_tag == DT_TEXTREL)
12056 if (info->error_textrel)
12057 info->callbacks->einfo
12058 (_("%P%X: read-only segment has dynamic relocations.\n"));
12060 info->callbacks->einfo
12061 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12067 for (o = dynobj->sections; o != NULL; o = o->next)
12069 if ((o->flags & SEC_HAS_CONTENTS) == 0
12071 || o->output_section == bfd_abs_section_ptr)
12073 if ((o->flags & SEC_LINKER_CREATED) == 0)
12075 /* At this point, we are only interested in sections
12076 created by _bfd_elf_link_create_dynamic_sections. */
12079 if (elf_hash_table (info)->stab_info.stabstr == o)
12081 if (elf_hash_table (info)->eh_info.hdr_sec == o)
12083 if (strcmp (o->name, ".dynstr") != 0)
12085 if (! bfd_set_section_contents (abfd, o->output_section,
12087 (file_ptr) o->output_offset
12088 * bfd_octets_per_byte (abfd),
12094 /* The contents of the .dynstr section are actually in a
12098 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12099 if (bfd_seek (abfd, off, SEEK_SET) != 0
12100 || ! _bfd_elf_strtab_emit (abfd,
12101 elf_hash_table (info)->dynstr))
12107 if (bfd_link_relocatable (info))
12109 bfd_boolean failed = FALSE;
12111 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12116 /* If we have optimized stabs strings, output them. */
12117 if (elf_hash_table (info)->stab_info.stabstr != NULL)
12119 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
12123 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12126 elf_final_link_free (abfd, &flinfo);
12128 elf_linker (abfd) = TRUE;
12132 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12133 if (contents == NULL)
12134 return FALSE; /* Bail out and fail. */
12135 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12136 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12143 elf_final_link_free (abfd, &flinfo);
12147 /* Initialize COOKIE for input bfd ABFD. */
12150 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12151 struct bfd_link_info *info, bfd *abfd)
12153 Elf_Internal_Shdr *symtab_hdr;
12154 const struct elf_backend_data *bed;
12156 bed = get_elf_backend_data (abfd);
12157 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12159 cookie->abfd = abfd;
12160 cookie->sym_hashes = elf_sym_hashes (abfd);
12161 cookie->bad_symtab = elf_bad_symtab (abfd);
12162 if (cookie->bad_symtab)
12164 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12165 cookie->extsymoff = 0;
12169 cookie->locsymcount = symtab_hdr->sh_info;
12170 cookie->extsymoff = symtab_hdr->sh_info;
12173 if (bed->s->arch_size == 32)
12174 cookie->r_sym_shift = 8;
12176 cookie->r_sym_shift = 32;
12178 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12179 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12181 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12182 cookie->locsymcount, 0,
12184 if (cookie->locsyms == NULL)
12186 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12189 if (info->keep_memory)
12190 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12195 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12198 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12200 Elf_Internal_Shdr *symtab_hdr;
12202 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12203 if (cookie->locsyms != NULL
12204 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12205 free (cookie->locsyms);
12208 /* Initialize the relocation information in COOKIE for input section SEC
12209 of input bfd ABFD. */
12212 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12213 struct bfd_link_info *info, bfd *abfd,
12216 const struct elf_backend_data *bed;
12218 if (sec->reloc_count == 0)
12220 cookie->rels = NULL;
12221 cookie->relend = NULL;
12225 bed = get_elf_backend_data (abfd);
12227 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12228 info->keep_memory);
12229 if (cookie->rels == NULL)
12231 cookie->rel = cookie->rels;
12232 cookie->relend = (cookie->rels
12233 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12235 cookie->rel = cookie->rels;
12239 /* Free the memory allocated by init_reloc_cookie_rels,
12243 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12246 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12247 free (cookie->rels);
12250 /* Initialize the whole of COOKIE for input section SEC. */
12253 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12254 struct bfd_link_info *info,
12257 if (!init_reloc_cookie (cookie, info, sec->owner))
12259 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12264 fini_reloc_cookie (cookie, sec->owner);
12269 /* Free the memory allocated by init_reloc_cookie_for_section,
12273 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12276 fini_reloc_cookie_rels (cookie, sec);
12277 fini_reloc_cookie (cookie, sec->owner);
12280 /* Garbage collect unused sections. */
12282 /* Default gc_mark_hook. */
12285 _bfd_elf_gc_mark_hook (asection *sec,
12286 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12287 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12288 struct elf_link_hash_entry *h,
12289 Elf_Internal_Sym *sym)
12293 switch (h->root.type)
12295 case bfd_link_hash_defined:
12296 case bfd_link_hash_defweak:
12297 return h->root.u.def.section;
12299 case bfd_link_hash_common:
12300 return h->root.u.c.p->section;
12307 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12312 /* For undefined __start_<name> and __stop_<name> symbols, return the
12313 first input section matching <name>. Return NULL otherwise. */
12316 _bfd_elf_is_start_stop (const struct bfd_link_info *info,
12317 struct elf_link_hash_entry *h)
12320 const char *sec_name;
12322 if (h->root.type != bfd_link_hash_undefined
12323 && h->root.type != bfd_link_hash_undefweak)
12326 s = h->root.u.undef.section;
12329 if (s == (asection *) 0 - 1)
12335 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12336 sec_name = h->root.root.string + 8;
12337 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12338 sec_name = h->root.root.string + 7;
12340 if (sec_name != NULL && *sec_name != '\0')
12344 for (i = info->input_bfds; i != NULL; i = i->link.next)
12346 s = bfd_get_section_by_name (i, sec_name);
12349 h->root.u.undef.section = s;
12356 h->root.u.undef.section = (asection *) 0 - 1;
12361 /* COOKIE->rel describes a relocation against section SEC, which is
12362 a section we've decided to keep. Return the section that contains
12363 the relocation symbol, or NULL if no section contains it. */
12366 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12367 elf_gc_mark_hook_fn gc_mark_hook,
12368 struct elf_reloc_cookie *cookie,
12369 bfd_boolean *start_stop)
12371 unsigned long r_symndx;
12372 struct elf_link_hash_entry *h;
12374 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12375 if (r_symndx == STN_UNDEF)
12378 if (r_symndx >= cookie->locsymcount
12379 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12381 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12384 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12388 while (h->root.type == bfd_link_hash_indirect
12389 || h->root.type == bfd_link_hash_warning)
12390 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12392 /* If this symbol is weak and there is a non-weak definition, we
12393 keep the non-weak definition because many backends put
12394 dynamic reloc info on the non-weak definition for code
12395 handling copy relocs. */
12396 if (h->u.weakdef != NULL)
12397 h->u.weakdef->mark = 1;
12399 if (start_stop != NULL)
12401 /* To work around a glibc bug, mark all XXX input sections
12402 when there is an as yet undefined reference to __start_XXX
12403 or __stop_XXX symbols. The linker will later define such
12404 symbols for orphan input sections that have a name
12405 representable as a C identifier. */
12406 asection *s = _bfd_elf_is_start_stop (info, h);
12410 *start_stop = !s->gc_mark;
12415 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12418 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12419 &cookie->locsyms[r_symndx]);
12422 /* COOKIE->rel describes a relocation against section SEC, which is
12423 a section we've decided to keep. Mark the section that contains
12424 the relocation symbol. */
12427 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12429 elf_gc_mark_hook_fn gc_mark_hook,
12430 struct elf_reloc_cookie *cookie)
12433 bfd_boolean start_stop = FALSE;
12435 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12436 while (rsec != NULL)
12438 if (!rsec->gc_mark)
12440 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12441 || (rsec->owner->flags & DYNAMIC) != 0)
12443 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12448 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12453 /* The mark phase of garbage collection. For a given section, mark
12454 it and any sections in this section's group, and all the sections
12455 which define symbols to which it refers. */
12458 _bfd_elf_gc_mark (struct bfd_link_info *info,
12460 elf_gc_mark_hook_fn gc_mark_hook)
12463 asection *group_sec, *eh_frame;
12467 /* Mark all the sections in the group. */
12468 group_sec = elf_section_data (sec)->next_in_group;
12469 if (group_sec && !group_sec->gc_mark)
12470 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12473 /* Look through the section relocs. */
12475 eh_frame = elf_eh_frame_section (sec->owner);
12476 if ((sec->flags & SEC_RELOC) != 0
12477 && sec->reloc_count > 0
12478 && sec != eh_frame)
12480 struct elf_reloc_cookie cookie;
12482 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12486 for (; cookie.rel < cookie.relend; cookie.rel++)
12487 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12492 fini_reloc_cookie_for_section (&cookie, sec);
12496 if (ret && eh_frame && elf_fde_list (sec))
12498 struct elf_reloc_cookie cookie;
12500 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12504 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12505 gc_mark_hook, &cookie))
12507 fini_reloc_cookie_for_section (&cookie, eh_frame);
12511 eh_frame = elf_section_eh_frame_entry (sec);
12512 if (ret && eh_frame && !eh_frame->gc_mark)
12513 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12519 /* Scan and mark sections in a special or debug section group. */
12522 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12524 /* Point to first section of section group. */
12526 /* Used to iterate the section group. */
12529 bfd_boolean is_special_grp = TRUE;
12530 bfd_boolean is_debug_grp = TRUE;
12532 /* First scan to see if group contains any section other than debug
12533 and special section. */
12534 ssec = msec = elf_next_in_group (grp);
12537 if ((msec->flags & SEC_DEBUGGING) == 0)
12538 is_debug_grp = FALSE;
12540 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12541 is_special_grp = FALSE;
12543 msec = elf_next_in_group (msec);
12545 while (msec != ssec);
12547 /* If this is a pure debug section group or pure special section group,
12548 keep all sections in this group. */
12549 if (is_debug_grp || is_special_grp)
12554 msec = elf_next_in_group (msec);
12556 while (msec != ssec);
12560 /* Keep debug and special sections. */
12563 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12564 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12568 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12571 bfd_boolean some_kept;
12572 bfd_boolean debug_frag_seen;
12574 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12577 /* Ensure all linker created sections are kept,
12578 see if any other section is already marked,
12579 and note if we have any fragmented debug sections. */
12580 debug_frag_seen = some_kept = FALSE;
12581 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12583 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12585 else if (isec->gc_mark)
12588 if (debug_frag_seen == FALSE
12589 && (isec->flags & SEC_DEBUGGING)
12590 && CONST_STRNEQ (isec->name, ".debug_line."))
12591 debug_frag_seen = TRUE;
12594 /* If no section in this file will be kept, then we can
12595 toss out the debug and special sections. */
12599 /* Keep debug and special sections like .comment when they are
12600 not part of a group. Also keep section groups that contain
12601 just debug sections or special sections. */
12602 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12604 if ((isec->flags & SEC_GROUP) != 0)
12605 _bfd_elf_gc_mark_debug_special_section_group (isec);
12606 else if (((isec->flags & SEC_DEBUGGING) != 0
12607 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12608 && elf_next_in_group (isec) == NULL)
12612 if (! debug_frag_seen)
12615 /* Look for CODE sections which are going to be discarded,
12616 and find and discard any fragmented debug sections which
12617 are associated with that code section. */
12618 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12619 if ((isec->flags & SEC_CODE) != 0
12620 && isec->gc_mark == 0)
12625 ilen = strlen (isec->name);
12627 /* Association is determined by the name of the debug section
12628 containing the name of the code section as a suffix. For
12629 example .debug_line.text.foo is a debug section associated
12631 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12635 if (dsec->gc_mark == 0
12636 || (dsec->flags & SEC_DEBUGGING) == 0)
12639 dlen = strlen (dsec->name);
12642 && strncmp (dsec->name + (dlen - ilen),
12643 isec->name, ilen) == 0)
12653 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12655 struct elf_gc_sweep_symbol_info
12657 struct bfd_link_info *info;
12658 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12663 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12666 && (((h->root.type == bfd_link_hash_defined
12667 || h->root.type == bfd_link_hash_defweak)
12668 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12669 && h->root.u.def.section->gc_mark))
12670 || h->root.type == bfd_link_hash_undefined
12671 || h->root.type == bfd_link_hash_undefweak))
12673 struct elf_gc_sweep_symbol_info *inf;
12675 inf = (struct elf_gc_sweep_symbol_info *) data;
12676 (*inf->hide_symbol) (inf->info, h, TRUE);
12677 h->def_regular = 0;
12678 h->ref_regular = 0;
12679 h->ref_regular_nonweak = 0;
12685 /* The sweep phase of garbage collection. Remove all garbage sections. */
12687 typedef bfd_boolean (*gc_sweep_hook_fn)
12688 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12691 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12694 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12695 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12696 unsigned long section_sym_count;
12697 struct elf_gc_sweep_symbol_info sweep_info;
12699 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12703 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12704 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12707 for (o = sub->sections; o != NULL; o = o->next)
12709 /* When any section in a section group is kept, we keep all
12710 sections in the section group. If the first member of
12711 the section group is excluded, we will also exclude the
12713 if (o->flags & SEC_GROUP)
12715 asection *first = elf_next_in_group (o);
12716 o->gc_mark = first->gc_mark;
12722 /* Skip sweeping sections already excluded. */
12723 if (o->flags & SEC_EXCLUDE)
12726 /* Since this is early in the link process, it is simple
12727 to remove a section from the output. */
12728 o->flags |= SEC_EXCLUDE;
12730 if (info->print_gc_sections && o->size != 0)
12731 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12733 /* But we also have to update some of the relocation
12734 info we collected before. */
12736 && (o->flags & SEC_RELOC) != 0
12737 && o->reloc_count != 0
12738 && !((info->strip == strip_all || info->strip == strip_debugger)
12739 && (o->flags & SEC_DEBUGGING) != 0)
12740 && !bfd_is_abs_section (o->output_section))
12742 Elf_Internal_Rela *internal_relocs;
12746 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12747 info->keep_memory);
12748 if (internal_relocs == NULL)
12751 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12753 if (elf_section_data (o)->relocs != internal_relocs)
12754 free (internal_relocs);
12762 /* Remove the symbols that were in the swept sections from the dynamic
12763 symbol table. GCFIXME: Anyone know how to get them out of the
12764 static symbol table as well? */
12765 sweep_info.info = info;
12766 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12767 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12770 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
12774 /* Propagate collected vtable information. This is called through
12775 elf_link_hash_traverse. */
12778 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12780 /* Those that are not vtables. */
12781 if (h->vtable == NULL || h->vtable->parent == NULL)
12784 /* Those vtables that do not have parents, we cannot merge. */
12785 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12788 /* If we've already been done, exit. */
12789 if (h->vtable->used && h->vtable->used[-1])
12792 /* Make sure the parent's table is up to date. */
12793 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12795 if (h->vtable->used == NULL)
12797 /* None of this table's entries were referenced. Re-use the
12799 h->vtable->used = h->vtable->parent->vtable->used;
12800 h->vtable->size = h->vtable->parent->vtable->size;
12805 bfd_boolean *cu, *pu;
12807 /* Or the parent's entries into ours. */
12808 cu = h->vtable->used;
12810 pu = h->vtable->parent->vtable->used;
12813 const struct elf_backend_data *bed;
12814 unsigned int log_file_align;
12816 bed = get_elf_backend_data (h->root.u.def.section->owner);
12817 log_file_align = bed->s->log_file_align;
12818 n = h->vtable->parent->vtable->size >> log_file_align;
12833 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12836 bfd_vma hstart, hend;
12837 Elf_Internal_Rela *relstart, *relend, *rel;
12838 const struct elf_backend_data *bed;
12839 unsigned int log_file_align;
12841 /* Take care of both those symbols that do not describe vtables as
12842 well as those that are not loaded. */
12843 if (h->vtable == NULL || h->vtable->parent == NULL)
12846 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12847 || h->root.type == bfd_link_hash_defweak);
12849 sec = h->root.u.def.section;
12850 hstart = h->root.u.def.value;
12851 hend = hstart + h->size;
12853 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12855 return *(bfd_boolean *) okp = FALSE;
12856 bed = get_elf_backend_data (sec->owner);
12857 log_file_align = bed->s->log_file_align;
12859 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12861 for (rel = relstart; rel < relend; ++rel)
12862 if (rel->r_offset >= hstart && rel->r_offset < hend)
12864 /* If the entry is in use, do nothing. */
12865 if (h->vtable->used
12866 && (rel->r_offset - hstart) < h->vtable->size)
12868 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12869 if (h->vtable->used[entry])
12872 /* Otherwise, kill it. */
12873 rel->r_offset = rel->r_info = rel->r_addend = 0;
12879 /* Mark sections containing dynamically referenced symbols. When
12880 building shared libraries, we must assume that any visible symbol is
12884 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12886 struct bfd_link_info *info = (struct bfd_link_info *) inf;
12887 struct bfd_elf_dynamic_list *d = info->dynamic_list;
12889 if ((h->root.type == bfd_link_hash_defined
12890 || h->root.type == bfd_link_hash_defweak)
12892 || ((h->def_regular || ELF_COMMON_DEF_P (h))
12893 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12894 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12895 && (!bfd_link_executable (info)
12896 || info->export_dynamic
12899 && (*d->match) (&d->head, NULL, h->root.root.string)))
12900 && (h->versioned >= versioned
12901 || !bfd_hide_sym_by_version (info->version_info,
12902 h->root.root.string)))))
12903 h->root.u.def.section->flags |= SEC_KEEP;
12908 /* Keep all sections containing symbols undefined on the command-line,
12909 and the section containing the entry symbol. */
12912 _bfd_elf_gc_keep (struct bfd_link_info *info)
12914 struct bfd_sym_chain *sym;
12916 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12918 struct elf_link_hash_entry *h;
12920 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12921 FALSE, FALSE, FALSE);
12924 && (h->root.type == bfd_link_hash_defined
12925 || h->root.type == bfd_link_hash_defweak)
12926 && !bfd_is_abs_section (h->root.u.def.section))
12927 h->root.u.def.section->flags |= SEC_KEEP;
12932 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12933 struct bfd_link_info *info)
12935 bfd *ibfd = info->input_bfds;
12937 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12940 struct elf_reloc_cookie cookie;
12942 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12945 if (!init_reloc_cookie (&cookie, info, ibfd))
12948 for (sec = ibfd->sections; sec; sec = sec->next)
12950 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12951 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12953 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12954 fini_reloc_cookie_rels (&cookie, sec);
12961 /* Do mark and sweep of unused sections. */
12964 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12966 bfd_boolean ok = TRUE;
12968 elf_gc_mark_hook_fn gc_mark_hook;
12969 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12970 struct elf_link_hash_table *htab;
12972 if (!bed->can_gc_sections
12973 || !is_elf_hash_table (info->hash))
12975 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12979 bed->gc_keep (info);
12980 htab = elf_hash_table (info);
12982 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12983 at the .eh_frame section if we can mark the FDEs individually. */
12984 for (sub = info->input_bfds;
12985 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12986 sub = sub->link.next)
12989 struct elf_reloc_cookie cookie;
12991 sec = bfd_get_section_by_name (sub, ".eh_frame");
12992 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12994 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12995 if (elf_section_data (sec)->sec_info
12996 && (sec->flags & SEC_LINKER_CREATED) == 0)
12997 elf_eh_frame_section (sub) = sec;
12998 fini_reloc_cookie_for_section (&cookie, sec);
12999 sec = bfd_get_next_section_by_name (NULL, sec);
13003 /* Apply transitive closure to the vtable entry usage info. */
13004 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13008 /* Kill the vtable relocations that were not used. */
13009 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13013 /* Mark dynamically referenced symbols. */
13014 if (htab->dynamic_sections_created)
13015 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13017 /* Grovel through relocs to find out who stays ... */
13018 gc_mark_hook = bed->gc_mark_hook;
13019 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13023 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13024 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13027 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13028 Also treat note sections as a root, if the section is not part
13030 for (o = sub->sections; o != NULL; o = o->next)
13032 && (o->flags & SEC_EXCLUDE) == 0
13033 && ((o->flags & SEC_KEEP) != 0
13034 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13035 && elf_next_in_group (o) == NULL )))
13037 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13042 /* Allow the backend to mark additional target specific sections. */
13043 bed->gc_mark_extra_sections (info, gc_mark_hook);
13045 /* ... and mark SEC_EXCLUDE for those that go. */
13046 return elf_gc_sweep (abfd, info);
13049 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13052 bfd_elf_gc_record_vtinherit (bfd *abfd,
13054 struct elf_link_hash_entry *h,
13057 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13058 struct elf_link_hash_entry **search, *child;
13059 size_t extsymcount;
13060 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13062 /* The sh_info field of the symtab header tells us where the
13063 external symbols start. We don't care about the local symbols at
13065 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13066 if (!elf_bad_symtab (abfd))
13067 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13069 sym_hashes = elf_sym_hashes (abfd);
13070 sym_hashes_end = sym_hashes + extsymcount;
13072 /* Hunt down the child symbol, which is in this section at the same
13073 offset as the relocation. */
13074 for (search = sym_hashes; search != sym_hashes_end; ++search)
13076 if ((child = *search) != NULL
13077 && (child->root.type == bfd_link_hash_defined
13078 || child->root.type == bfd_link_hash_defweak)
13079 && child->root.u.def.section == sec
13080 && child->root.u.def.value == offset)
13084 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
13085 abfd, sec, (unsigned long) offset);
13086 bfd_set_error (bfd_error_invalid_operation);
13090 if (!child->vtable)
13092 child->vtable = ((struct elf_link_virtual_table_entry *)
13093 bfd_zalloc (abfd, sizeof (*child->vtable)));
13094 if (!child->vtable)
13099 /* This *should* only be the absolute section. It could potentially
13100 be that someone has defined a non-global vtable though, which
13101 would be bad. It isn't worth paging in the local symbols to be
13102 sure though; that case should simply be handled by the assembler. */
13104 child->vtable->parent = (struct elf_link_hash_entry *) -1;
13107 child->vtable->parent = h;
13112 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13115 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13116 asection *sec ATTRIBUTE_UNUSED,
13117 struct elf_link_hash_entry *h,
13120 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13121 unsigned int log_file_align = bed->s->log_file_align;
13125 h->vtable = ((struct elf_link_virtual_table_entry *)
13126 bfd_zalloc (abfd, sizeof (*h->vtable)));
13131 if (addend >= h->vtable->size)
13133 size_t size, bytes, file_align;
13134 bfd_boolean *ptr = h->vtable->used;
13136 /* While the symbol is undefined, we have to be prepared to handle
13138 file_align = 1 << log_file_align;
13139 if (h->root.type == bfd_link_hash_undefined)
13140 size = addend + file_align;
13144 if (addend >= size)
13146 /* Oops! We've got a reference past the defined end of
13147 the table. This is probably a bug -- shall we warn? */
13148 size = addend + file_align;
13151 size = (size + file_align - 1) & -file_align;
13153 /* Allocate one extra entry for use as a "done" flag for the
13154 consolidation pass. */
13155 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13159 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13165 oldbytes = (((h->vtable->size >> log_file_align) + 1)
13166 * sizeof (bfd_boolean));
13167 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13171 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13176 /* And arrange for that done flag to be at index -1. */
13177 h->vtable->used = ptr + 1;
13178 h->vtable->size = size;
13181 h->vtable->used[addend >> log_file_align] = TRUE;
13186 /* Map an ELF section header flag to its corresponding string. */
13190 flagword flag_value;
13191 } elf_flags_to_name_table;
13193 static elf_flags_to_name_table elf_flags_to_names [] =
13195 { "SHF_WRITE", SHF_WRITE },
13196 { "SHF_ALLOC", SHF_ALLOC },
13197 { "SHF_EXECINSTR", SHF_EXECINSTR },
13198 { "SHF_MERGE", SHF_MERGE },
13199 { "SHF_STRINGS", SHF_STRINGS },
13200 { "SHF_INFO_LINK", SHF_INFO_LINK},
13201 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13202 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13203 { "SHF_GROUP", SHF_GROUP },
13204 { "SHF_TLS", SHF_TLS },
13205 { "SHF_MASKOS", SHF_MASKOS },
13206 { "SHF_EXCLUDE", SHF_EXCLUDE },
13209 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13211 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13212 struct flag_info *flaginfo,
13215 const bfd_vma sh_flags = elf_section_flags (section);
13217 if (!flaginfo->flags_initialized)
13219 bfd *obfd = info->output_bfd;
13220 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13221 struct flag_info_list *tf = flaginfo->flag_list;
13223 int without_hex = 0;
13225 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13228 flagword (*lookup) (char *);
13230 lookup = bed->elf_backend_lookup_section_flags_hook;
13231 if (lookup != NULL)
13233 flagword hexval = (*lookup) ((char *) tf->name);
13237 if (tf->with == with_flags)
13238 with_hex |= hexval;
13239 else if (tf->with == without_flags)
13240 without_hex |= hexval;
13245 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13247 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13249 if (tf->with == with_flags)
13250 with_hex |= elf_flags_to_names[i].flag_value;
13251 else if (tf->with == without_flags)
13252 without_hex |= elf_flags_to_names[i].flag_value;
13259 info->callbacks->einfo
13260 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13264 flaginfo->flags_initialized = TRUE;
13265 flaginfo->only_with_flags |= with_hex;
13266 flaginfo->not_with_flags |= without_hex;
13269 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13272 if ((flaginfo->not_with_flags & sh_flags) != 0)
13278 struct alloc_got_off_arg {
13280 struct bfd_link_info *info;
13283 /* We need a special top-level link routine to convert got reference counts
13284 to real got offsets. */
13287 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13289 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13290 bfd *obfd = gofarg->info->output_bfd;
13291 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13293 if (h->got.refcount > 0)
13295 h->got.offset = gofarg->gotoff;
13296 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13299 h->got.offset = (bfd_vma) -1;
13304 /* And an accompanying bit to work out final got entry offsets once
13305 we're done. Should be called from final_link. */
13308 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13309 struct bfd_link_info *info)
13312 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13314 struct alloc_got_off_arg gofarg;
13316 BFD_ASSERT (abfd == info->output_bfd);
13318 if (! is_elf_hash_table (info->hash))
13321 /* The GOT offset is relative to the .got section, but the GOT header is
13322 put into the .got.plt section, if the backend uses it. */
13323 if (bed->want_got_plt)
13326 gotoff = bed->got_header_size;
13328 /* Do the local .got entries first. */
13329 for (i = info->input_bfds; i; i = i->link.next)
13331 bfd_signed_vma *local_got;
13332 size_t j, locsymcount;
13333 Elf_Internal_Shdr *symtab_hdr;
13335 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13338 local_got = elf_local_got_refcounts (i);
13342 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13343 if (elf_bad_symtab (i))
13344 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13346 locsymcount = symtab_hdr->sh_info;
13348 for (j = 0; j < locsymcount; ++j)
13350 if (local_got[j] > 0)
13352 local_got[j] = gotoff;
13353 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13356 local_got[j] = (bfd_vma) -1;
13360 /* Then the global .got entries. .plt refcounts are handled by
13361 adjust_dynamic_symbol */
13362 gofarg.gotoff = gotoff;
13363 gofarg.info = info;
13364 elf_link_hash_traverse (elf_hash_table (info),
13365 elf_gc_allocate_got_offsets,
13370 /* Many folk need no more in the way of final link than this, once
13371 got entry reference counting is enabled. */
13374 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13376 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13379 /* Invoke the regular ELF backend linker to do all the work. */
13380 return bfd_elf_final_link (abfd, info);
13384 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13386 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13388 if (rcookie->bad_symtab)
13389 rcookie->rel = rcookie->rels;
13391 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13393 unsigned long r_symndx;
13395 if (! rcookie->bad_symtab)
13396 if (rcookie->rel->r_offset > offset)
13398 if (rcookie->rel->r_offset != offset)
13401 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13402 if (r_symndx == STN_UNDEF)
13405 if (r_symndx >= rcookie->locsymcount
13406 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13408 struct elf_link_hash_entry *h;
13410 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13412 while (h->root.type == bfd_link_hash_indirect
13413 || h->root.type == bfd_link_hash_warning)
13414 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13416 if ((h->root.type == bfd_link_hash_defined
13417 || h->root.type == bfd_link_hash_defweak)
13418 && (h->root.u.def.section->owner != rcookie->abfd
13419 || h->root.u.def.section->kept_section != NULL
13420 || discarded_section (h->root.u.def.section)))
13425 /* It's not a relocation against a global symbol,
13426 but it could be a relocation against a local
13427 symbol for a discarded section. */
13429 Elf_Internal_Sym *isym;
13431 /* Need to: get the symbol; get the section. */
13432 isym = &rcookie->locsyms[r_symndx];
13433 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13435 && (isec->kept_section != NULL
13436 || discarded_section (isec)))
13444 /* Discard unneeded references to discarded sections.
13445 Returns -1 on error, 1 if any section's size was changed, 0 if
13446 nothing changed. This function assumes that the relocations are in
13447 sorted order, which is true for all known assemblers. */
13450 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13452 struct elf_reloc_cookie cookie;
13457 if (info->traditional_format
13458 || !is_elf_hash_table (info->hash))
13461 o = bfd_get_section_by_name (output_bfd, ".stab");
13466 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13469 || i->reloc_count == 0
13470 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13474 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13477 if (!init_reloc_cookie_for_section (&cookie, info, i))
13480 if (_bfd_discard_section_stabs (abfd, i,
13481 elf_section_data (i)->sec_info,
13482 bfd_elf_reloc_symbol_deleted_p,
13486 fini_reloc_cookie_for_section (&cookie, i);
13491 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13492 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13497 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13503 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13506 if (!init_reloc_cookie_for_section (&cookie, info, i))
13509 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13510 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13511 bfd_elf_reloc_symbol_deleted_p,
13515 fini_reloc_cookie_for_section (&cookie, i);
13519 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13521 const struct elf_backend_data *bed;
13523 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13526 bed = get_elf_backend_data (abfd);
13528 if (bed->elf_backend_discard_info != NULL)
13530 if (!init_reloc_cookie (&cookie, info, abfd))
13533 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13536 fini_reloc_cookie (&cookie, abfd);
13540 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13541 _bfd_elf_end_eh_frame_parsing (info);
13543 if (info->eh_frame_hdr_type
13544 && !bfd_link_relocatable (info)
13545 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13552 _bfd_elf_section_already_linked (bfd *abfd,
13554 struct bfd_link_info *info)
13557 const char *name, *key;
13558 struct bfd_section_already_linked *l;
13559 struct bfd_section_already_linked_hash_entry *already_linked_list;
13561 if (sec->output_section == bfd_abs_section_ptr)
13564 flags = sec->flags;
13566 /* Return if it isn't a linkonce section. A comdat group section
13567 also has SEC_LINK_ONCE set. */
13568 if ((flags & SEC_LINK_ONCE) == 0)
13571 /* Don't put group member sections on our list of already linked
13572 sections. They are handled as a group via their group section. */
13573 if (elf_sec_group (sec) != NULL)
13576 /* For a SHT_GROUP section, use the group signature as the key. */
13578 if ((flags & SEC_GROUP) != 0
13579 && elf_next_in_group (sec) != NULL
13580 && elf_group_name (elf_next_in_group (sec)) != NULL)
13581 key = elf_group_name (elf_next_in_group (sec));
13584 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13585 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13586 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13589 /* Must be a user linkonce section that doesn't follow gcc's
13590 naming convention. In this case we won't be matching
13591 single member groups. */
13595 already_linked_list = bfd_section_already_linked_table_lookup (key);
13597 for (l = already_linked_list->entry; l != NULL; l = l->next)
13599 /* We may have 2 different types of sections on the list: group
13600 sections with a signature of <key> (<key> is some string),
13601 and linkonce sections named .gnu.linkonce.<type>.<key>.
13602 Match like sections. LTO plugin sections are an exception.
13603 They are always named .gnu.linkonce.t.<key> and match either
13604 type of section. */
13605 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13606 && ((flags & SEC_GROUP) != 0
13607 || strcmp (name, l->sec->name) == 0))
13608 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13610 /* The section has already been linked. See if we should
13611 issue a warning. */
13612 if (!_bfd_handle_already_linked (sec, l, info))
13615 if (flags & SEC_GROUP)
13617 asection *first = elf_next_in_group (sec);
13618 asection *s = first;
13622 s->output_section = bfd_abs_section_ptr;
13623 /* Record which group discards it. */
13624 s->kept_section = l->sec;
13625 s = elf_next_in_group (s);
13626 /* These lists are circular. */
13636 /* A single member comdat group section may be discarded by a
13637 linkonce section and vice versa. */
13638 if ((flags & SEC_GROUP) != 0)
13640 asection *first = elf_next_in_group (sec);
13642 if (first != NULL && elf_next_in_group (first) == first)
13643 /* Check this single member group against linkonce sections. */
13644 for (l = already_linked_list->entry; l != NULL; l = l->next)
13645 if ((l->sec->flags & SEC_GROUP) == 0
13646 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13648 first->output_section = bfd_abs_section_ptr;
13649 first->kept_section = l->sec;
13650 sec->output_section = bfd_abs_section_ptr;
13655 /* Check this linkonce section against single member groups. */
13656 for (l = already_linked_list->entry; l != NULL; l = l->next)
13657 if (l->sec->flags & SEC_GROUP)
13659 asection *first = elf_next_in_group (l->sec);
13662 && elf_next_in_group (first) == first
13663 && bfd_elf_match_symbols_in_sections (first, sec, info))
13665 sec->output_section = bfd_abs_section_ptr;
13666 sec->kept_section = first;
13671 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13672 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13673 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13674 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13675 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13676 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13677 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13678 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13679 The reverse order cannot happen as there is never a bfd with only the
13680 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13681 matter as here were are looking only for cross-bfd sections. */
13683 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13684 for (l = already_linked_list->entry; l != NULL; l = l->next)
13685 if ((l->sec->flags & SEC_GROUP) == 0
13686 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13688 if (abfd != l->sec->owner)
13689 sec->output_section = bfd_abs_section_ptr;
13693 /* This is the first section with this name. Record it. */
13694 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13695 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13696 return sec->output_section == bfd_abs_section_ptr;
13700 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13702 return sym->st_shndx == SHN_COMMON;
13706 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13712 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13714 return bfd_com_section_ptr;
13718 _bfd_elf_default_got_elt_size (bfd *abfd,
13719 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13720 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13721 bfd *ibfd ATTRIBUTE_UNUSED,
13722 unsigned long symndx ATTRIBUTE_UNUSED)
13724 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13725 return bed->s->arch_size / 8;
13728 /* Routines to support the creation of dynamic relocs. */
13730 /* Returns the name of the dynamic reloc section associated with SEC. */
13732 static const char *
13733 get_dynamic_reloc_section_name (bfd * abfd,
13735 bfd_boolean is_rela)
13738 const char *old_name = bfd_get_section_name (NULL, sec);
13739 const char *prefix = is_rela ? ".rela" : ".rel";
13741 if (old_name == NULL)
13744 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13745 sprintf (name, "%s%s", prefix, old_name);
13750 /* Returns the dynamic reloc section associated with SEC.
13751 If necessary compute the name of the dynamic reloc section based
13752 on SEC's name (looked up in ABFD's string table) and the setting
13756 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13758 bfd_boolean is_rela)
13760 asection * reloc_sec = elf_section_data (sec)->sreloc;
13762 if (reloc_sec == NULL)
13764 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13768 reloc_sec = bfd_get_linker_section (abfd, name);
13770 if (reloc_sec != NULL)
13771 elf_section_data (sec)->sreloc = reloc_sec;
13778 /* Returns the dynamic reloc section associated with SEC. If the
13779 section does not exist it is created and attached to the DYNOBJ
13780 bfd and stored in the SRELOC field of SEC's elf_section_data
13783 ALIGNMENT is the alignment for the newly created section and
13784 IS_RELA defines whether the name should be .rela.<SEC's name>
13785 or .rel.<SEC's name>. The section name is looked up in the
13786 string table associated with ABFD. */
13789 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13791 unsigned int alignment,
13793 bfd_boolean is_rela)
13795 asection * reloc_sec = elf_section_data (sec)->sreloc;
13797 if (reloc_sec == NULL)
13799 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13804 reloc_sec = bfd_get_linker_section (dynobj, name);
13806 if (reloc_sec == NULL)
13808 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13809 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13810 if ((sec->flags & SEC_ALLOC) != 0)
13811 flags |= SEC_ALLOC | SEC_LOAD;
13813 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13814 if (reloc_sec != NULL)
13816 /* _bfd_elf_get_sec_type_attr chooses a section type by
13817 name. Override as it may be wrong, eg. for a user
13818 section named "auto" we'll get ".relauto" which is
13819 seen to be a .rela section. */
13820 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13821 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13826 elf_section_data (sec)->sreloc = reloc_sec;
13832 /* Copy the ELF symbol type and other attributes for a linker script
13833 assignment from HSRC to HDEST. Generally this should be treated as
13834 if we found a strong non-dynamic definition for HDEST (except that
13835 ld ignores multiple definition errors). */
13837 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13838 struct bfd_link_hash_entry *hdest,
13839 struct bfd_link_hash_entry *hsrc)
13841 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13842 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13843 Elf_Internal_Sym isym;
13845 ehdest->type = ehsrc->type;
13846 ehdest->target_internal = ehsrc->target_internal;
13848 isym.st_other = ehsrc->other;
13849 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
13852 /* Append a RELA relocation REL to section S in BFD. */
13855 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13857 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13858 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13859 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13860 bed->s->swap_reloca_out (abfd, rel, loc);
13863 /* Append a REL relocation REL to section S in BFD. */
13866 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13868 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13869 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13870 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13871 bed->s->swap_reloc_out (abfd, rel, loc);