1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
31 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
35 struct elf_link_hash_entry *h;
36 struct bfd_link_hash_entry *bh;
37 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
40 /* This function may be called more than once. */
41 s = bfd_get_section_by_name (abfd, ".got");
42 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
45 switch (bed->s->arch_size)
56 bfd_set_error (bfd_error_bad_value);
60 flags = bed->dynamic_sec_flags;
62 s = bfd_make_section_with_flags (abfd, ".got", flags);
64 || !bfd_set_section_alignment (abfd, s, ptralign))
67 if (bed->want_got_plt)
69 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
71 || !bfd_set_section_alignment (abfd, s, ptralign))
75 if (bed->want_got_sym)
77 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
78 (or .got.plt) section. We don't do this in the linker script
79 because we don't want to define the symbol if we are not creating
80 a global offset table. */
82 if (!(_bfd_generic_link_add_one_symbol
83 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
84 0, NULL, FALSE, bed->collect, &bh)))
86 h = (struct elf_link_hash_entry *) bh;
89 h->other = STV_HIDDEN;
91 if (! info->executable
92 && ! bfd_elf_link_record_dynamic_symbol (info, h))
95 elf_hash_table (info)->hgot = h;
98 /* The first bit of the global offset table is the header. */
99 s->size += bed->got_header_size;
104 /* Create a strtab to hold the dynamic symbol names. */
106 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
108 struct elf_link_hash_table *hash_table;
110 hash_table = elf_hash_table (info);
111 if (hash_table->dynobj == NULL)
112 hash_table->dynobj = abfd;
114 if (hash_table->dynstr == NULL)
116 hash_table->dynstr = _bfd_elf_strtab_init ();
117 if (hash_table->dynstr == NULL)
123 /* Create some sections which will be filled in with dynamic linking
124 information. ABFD is an input file which requires dynamic sections
125 to be created. The dynamic sections take up virtual memory space
126 when the final executable is run, so we need to create them before
127 addresses are assigned to the output sections. We work out the
128 actual contents and size of these sections later. */
131 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
134 register asection *s;
135 struct elf_link_hash_entry *h;
136 struct bfd_link_hash_entry *bh;
137 const struct elf_backend_data *bed;
139 if (! is_elf_hash_table (info->hash))
142 if (elf_hash_table (info)->dynamic_sections_created)
145 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
148 abfd = elf_hash_table (info)->dynobj;
149 bed = get_elf_backend_data (abfd);
151 flags = bed->dynamic_sec_flags;
153 /* A dynamically linked executable has a .interp section, but a
154 shared library does not. */
155 if (info->executable)
157 s = bfd_make_section_with_flags (abfd, ".interp",
158 flags | SEC_READONLY);
163 if (! info->traditional_format)
165 s = bfd_make_section_with_flags (abfd, ".eh_frame_hdr",
166 flags | SEC_READONLY);
168 || ! bfd_set_section_alignment (abfd, s, 2))
170 elf_hash_table (info)->eh_info.hdr_sec = s;
173 /* Create sections to hold version informations. These are removed
174 if they are not needed. */
175 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
176 flags | SEC_READONLY);
178 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
181 s = bfd_make_section_with_flags (abfd, ".gnu.version",
182 flags | SEC_READONLY);
184 || ! bfd_set_section_alignment (abfd, s, 1))
187 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
188 flags | SEC_READONLY);
190 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
193 s = bfd_make_section_with_flags (abfd, ".dynsym",
194 flags | SEC_READONLY);
196 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
199 s = bfd_make_section_with_flags (abfd, ".dynstr",
200 flags | SEC_READONLY);
204 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
206 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
209 /* The special symbol _DYNAMIC is always set to the start of the
210 .dynamic section. We could set _DYNAMIC in a linker script, but we
211 only want to define it if we are, in fact, creating a .dynamic
212 section. We don't want to define it if there is no .dynamic
213 section, since on some ELF platforms the start up code examines it
214 to decide how to initialize the process. */
215 h = elf_link_hash_lookup (elf_hash_table (info), "_DYNAMIC",
216 FALSE, FALSE, FALSE);
219 /* Zap symbol defined in an as-needed lib that wasn't linked.
220 This is a symptom of a larger problem: Absolute symbols
221 defined in shared libraries can't be overridden, because we
222 lose the link to the bfd which is via the symbol section. */
223 h->root.type = bfd_link_hash_new;
226 if (! (_bfd_generic_link_add_one_symbol
227 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE,
228 get_elf_backend_data (abfd)->collect, &bh)))
230 h = (struct elf_link_hash_entry *) bh;
232 h->type = STT_OBJECT;
234 if (! info->executable
235 && ! bfd_elf_link_record_dynamic_symbol (info, h))
238 s = bfd_make_section_with_flags (abfd, ".hash",
239 flags | SEC_READONLY);
241 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
243 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
245 /* Let the backend create the rest of the sections. This lets the
246 backend set the right flags. The backend will normally create
247 the .got and .plt sections. */
248 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
251 elf_hash_table (info)->dynamic_sections_created = TRUE;
256 /* Create dynamic sections when linking against a dynamic object. */
259 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
261 flagword flags, pltflags;
263 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
265 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
266 .rel[a].bss sections. */
267 flags = bed->dynamic_sec_flags;
270 if (bed->plt_not_loaded)
271 /* We do not clear SEC_ALLOC here because we still want the OS to
272 allocate space for the section; it's just that there's nothing
273 to read in from the object file. */
274 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
276 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
277 if (bed->plt_readonly)
278 pltflags |= SEC_READONLY;
280 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
282 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
285 if (bed->want_plt_sym)
287 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
289 struct elf_link_hash_entry *h;
290 struct bfd_link_hash_entry *bh = NULL;
292 if (! (_bfd_generic_link_add_one_symbol
293 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
294 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
296 h = (struct elf_link_hash_entry *) bh;
298 h->type = STT_OBJECT;
300 if (! info->executable
301 && ! bfd_elf_link_record_dynamic_symbol (info, h))
305 s = bfd_make_section_with_flags (abfd,
306 (bed->default_use_rela_p
307 ? ".rela.plt" : ".rel.plt"),
308 flags | SEC_READONLY);
310 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
313 if (! _bfd_elf_create_got_section (abfd, info))
316 if (bed->want_dynbss)
318 /* The .dynbss section is a place to put symbols which are defined
319 by dynamic objects, are referenced by regular objects, and are
320 not functions. We must allocate space for them in the process
321 image and use a R_*_COPY reloc to tell the dynamic linker to
322 initialize them at run time. The linker script puts the .dynbss
323 section into the .bss section of the final image. */
324 s = bfd_make_section_with_flags (abfd, ".dynbss",
326 | SEC_LINKER_CREATED));
330 /* The .rel[a].bss section holds copy relocs. This section is not
331 normally needed. We need to create it here, though, so that the
332 linker will map it to an output section. We can't just create it
333 only if we need it, because we will not know whether we need it
334 until we have seen all the input files, and the first time the
335 main linker code calls BFD after examining all the input files
336 (size_dynamic_sections) the input sections have already been
337 mapped to the output sections. If the section turns out not to
338 be needed, we can discard it later. We will never need this
339 section when generating a shared object, since they do not use
343 s = bfd_make_section_with_flags (abfd,
344 (bed->default_use_rela_p
345 ? ".rela.bss" : ".rel.bss"),
346 flags | SEC_READONLY);
348 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
356 /* Record a new dynamic symbol. We record the dynamic symbols as we
357 read the input files, since we need to have a list of all of them
358 before we can determine the final sizes of the output sections.
359 Note that we may actually call this function even though we are not
360 going to output any dynamic symbols; in some cases we know that a
361 symbol should be in the dynamic symbol table, but only if there is
365 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
366 struct elf_link_hash_entry *h)
368 if (h->dynindx == -1)
370 struct elf_strtab_hash *dynstr;
375 /* XXX: The ABI draft says the linker must turn hidden and
376 internal symbols into STB_LOCAL symbols when producing the
377 DSO. However, if ld.so honors st_other in the dynamic table,
378 this would not be necessary. */
379 switch (ELF_ST_VISIBILITY (h->other))
383 if (h->root.type != bfd_link_hash_undefined
384 && h->root.type != bfd_link_hash_undefweak)
387 if (!elf_hash_table (info)->is_relocatable_executable)
395 h->dynindx = elf_hash_table (info)->dynsymcount;
396 ++elf_hash_table (info)->dynsymcount;
398 dynstr = elf_hash_table (info)->dynstr;
401 /* Create a strtab to hold the dynamic symbol names. */
402 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
407 /* We don't put any version information in the dynamic string
409 name = h->root.root.string;
410 p = strchr (name, ELF_VER_CHR);
412 /* We know that the p points into writable memory. In fact,
413 there are only a few symbols that have read-only names, being
414 those like _GLOBAL_OFFSET_TABLE_ that are created specially
415 by the backends. Most symbols will have names pointing into
416 an ELF string table read from a file, or to objalloc memory. */
419 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
424 if (indx == (bfd_size_type) -1)
426 h->dynstr_index = indx;
432 /* Record an assignment to a symbol made by a linker script. We need
433 this in case some dynamic object refers to this symbol. */
436 bfd_elf_record_link_assignment (struct bfd_link_info *info,
440 struct elf_link_hash_entry *h;
441 struct elf_link_hash_table *htab;
443 if (!is_elf_hash_table (info->hash))
446 htab = elf_hash_table (info);
447 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
451 /* Since we're defining the symbol, don't let it seem to have not
452 been defined. record_dynamic_symbol and size_dynamic_sections
453 may depend on this. */
454 if (h->root.type == bfd_link_hash_undefweak
455 || h->root.type == bfd_link_hash_undefined)
457 h->root.type = bfd_link_hash_new;
458 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
459 bfd_link_repair_undef_list (&htab->root);
462 if (h->root.type == bfd_link_hash_new)
465 /* If this symbol is being provided by the linker script, and it is
466 currently defined by a dynamic object, but not by a regular
467 object, then mark it as undefined so that the generic linker will
468 force the correct value. */
472 h->root.type = bfd_link_hash_undefined;
474 /* If this symbol is not being provided by the linker script, and it is
475 currently defined by a dynamic object, but not by a regular object,
476 then clear out any version information because the symbol will not be
477 associated with the dynamic object any more. */
481 h->verinfo.verdef = NULL;
485 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
487 if (!info->relocatable
489 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
490 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
496 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
499 if (! bfd_elf_link_record_dynamic_symbol (info, h))
502 /* If this is a weak defined symbol, and we know a corresponding
503 real symbol from the same dynamic object, make sure the real
504 symbol is also made into a dynamic symbol. */
505 if (h->u.weakdef != NULL
506 && h->u.weakdef->dynindx == -1)
508 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
516 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
517 success, and 2 on a failure caused by attempting to record a symbol
518 in a discarded section, eg. a discarded link-once section symbol. */
521 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
526 struct elf_link_local_dynamic_entry *entry;
527 struct elf_link_hash_table *eht;
528 struct elf_strtab_hash *dynstr;
529 unsigned long dynstr_index;
531 Elf_External_Sym_Shndx eshndx;
532 char esym[sizeof (Elf64_External_Sym)];
534 if (! is_elf_hash_table (info->hash))
537 /* See if the entry exists already. */
538 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
539 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
542 amt = sizeof (*entry);
543 entry = bfd_alloc (input_bfd, amt);
547 /* Go find the symbol, so that we can find it's name. */
548 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
549 1, input_indx, &entry->isym, esym, &eshndx))
551 bfd_release (input_bfd, entry);
555 if (entry->isym.st_shndx != SHN_UNDEF
556 && (entry->isym.st_shndx < SHN_LORESERVE
557 || entry->isym.st_shndx > SHN_HIRESERVE))
561 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
562 if (s == NULL || bfd_is_abs_section (s->output_section))
564 /* We can still bfd_release here as nothing has done another
565 bfd_alloc. We can't do this later in this function. */
566 bfd_release (input_bfd, entry);
571 name = (bfd_elf_string_from_elf_section
572 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
573 entry->isym.st_name));
575 dynstr = elf_hash_table (info)->dynstr;
578 /* Create a strtab to hold the dynamic symbol names. */
579 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
584 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
585 if (dynstr_index == (unsigned long) -1)
587 entry->isym.st_name = dynstr_index;
589 eht = elf_hash_table (info);
591 entry->next = eht->dynlocal;
592 eht->dynlocal = entry;
593 entry->input_bfd = input_bfd;
594 entry->input_indx = input_indx;
597 /* Whatever binding the symbol had before, it's now local. */
599 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
601 /* The dynindx will be set at the end of size_dynamic_sections. */
606 /* Return the dynindex of a local dynamic symbol. */
609 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
613 struct elf_link_local_dynamic_entry *e;
615 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
616 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
621 /* This function is used to renumber the dynamic symbols, if some of
622 them are removed because they are marked as local. This is called
623 via elf_link_hash_traverse. */
626 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
629 size_t *count = data;
631 if (h->root.type == bfd_link_hash_warning)
632 h = (struct elf_link_hash_entry *) h->root.u.i.link;
637 if (h->dynindx != -1)
638 h->dynindx = ++(*count);
644 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
645 STB_LOCAL binding. */
648 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
651 size_t *count = data;
653 if (h->root.type == bfd_link_hash_warning)
654 h = (struct elf_link_hash_entry *) h->root.u.i.link;
656 if (!h->forced_local)
659 if (h->dynindx != -1)
660 h->dynindx = ++(*count);
665 /* Return true if the dynamic symbol for a given section should be
666 omitted when creating a shared library. */
668 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
669 struct bfd_link_info *info,
672 switch (elf_section_data (p)->this_hdr.sh_type)
676 /* If sh_type is yet undecided, assume it could be
677 SHT_PROGBITS/SHT_NOBITS. */
679 if (strcmp (p->name, ".got") == 0
680 || strcmp (p->name, ".got.plt") == 0
681 || strcmp (p->name, ".plt") == 0)
684 bfd *dynobj = elf_hash_table (info)->dynobj;
687 && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL
688 && (ip->flags & SEC_LINKER_CREATED)
689 && ip->output_section == p)
694 /* There shouldn't be section relative relocations
695 against any other section. */
701 /* Assign dynsym indices. In a shared library we generate a section
702 symbol for each output section, which come first. Next come symbols
703 which have been forced to local binding. Then all of the back-end
704 allocated local dynamic syms, followed by the rest of the global
708 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
709 struct bfd_link_info *info,
710 unsigned long *section_sym_count)
712 unsigned long dynsymcount = 0;
714 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
716 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
718 for (p = output_bfd->sections; p ; p = p->next)
719 if ((p->flags & SEC_EXCLUDE) == 0
720 && (p->flags & SEC_ALLOC) != 0
721 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
722 elf_section_data (p)->dynindx = ++dynsymcount;
724 *section_sym_count = dynsymcount;
726 elf_link_hash_traverse (elf_hash_table (info),
727 elf_link_renumber_local_hash_table_dynsyms,
730 if (elf_hash_table (info)->dynlocal)
732 struct elf_link_local_dynamic_entry *p;
733 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
734 p->dynindx = ++dynsymcount;
737 elf_link_hash_traverse (elf_hash_table (info),
738 elf_link_renumber_hash_table_dynsyms,
741 /* There is an unused NULL entry at the head of the table which
742 we must account for in our count. Unless there weren't any
743 symbols, which means we'll have no table at all. */
744 if (dynsymcount != 0)
747 return elf_hash_table (info)->dynsymcount = dynsymcount;
750 /* This function is called when we want to define a new symbol. It
751 handles the various cases which arise when we find a definition in
752 a dynamic object, or when there is already a definition in a
753 dynamic object. The new symbol is described by NAME, SYM, PSEC,
754 and PVALUE. We set SYM_HASH to the hash table entry. We set
755 OVERRIDE if the old symbol is overriding a new definition. We set
756 TYPE_CHANGE_OK if it is OK for the type to change. We set
757 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
758 change, we mean that we shouldn't warn if the type or size does
759 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
760 object is overridden by a regular object. */
763 _bfd_elf_merge_symbol (bfd *abfd,
764 struct bfd_link_info *info,
766 Elf_Internal_Sym *sym,
769 unsigned int *pold_alignment,
770 struct elf_link_hash_entry **sym_hash,
772 bfd_boolean *override,
773 bfd_boolean *type_change_ok,
774 bfd_boolean *size_change_ok)
776 asection *sec, *oldsec;
777 struct elf_link_hash_entry *h;
778 struct elf_link_hash_entry *flip;
781 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
782 bfd_boolean newweak, oldweak;
788 bind = ELF_ST_BIND (sym->st_info);
790 if (! bfd_is_und_section (sec))
791 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
793 h = ((struct elf_link_hash_entry *)
794 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
799 /* This code is for coping with dynamic objects, and is only useful
800 if we are doing an ELF link. */
801 if (info->hash->creator != abfd->xvec)
804 /* For merging, we only care about real symbols. */
806 while (h->root.type == bfd_link_hash_indirect
807 || h->root.type == bfd_link_hash_warning)
808 h = (struct elf_link_hash_entry *) h->root.u.i.link;
810 /* If we just created the symbol, mark it as being an ELF symbol.
811 Other than that, there is nothing to do--there is no merge issue
812 with a newly defined symbol--so we just return. */
814 if (h->root.type == bfd_link_hash_new)
820 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
823 switch (h->root.type)
830 case bfd_link_hash_undefined:
831 case bfd_link_hash_undefweak:
832 oldbfd = h->root.u.undef.abfd;
836 case bfd_link_hash_defined:
837 case bfd_link_hash_defweak:
838 oldbfd = h->root.u.def.section->owner;
839 oldsec = h->root.u.def.section;
842 case bfd_link_hash_common:
843 oldbfd = h->root.u.c.p->section->owner;
844 oldsec = h->root.u.c.p->section;
848 /* In cases involving weak versioned symbols, we may wind up trying
849 to merge a symbol with itself. Catch that here, to avoid the
850 confusion that results if we try to override a symbol with
851 itself. The additional tests catch cases like
852 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
853 dynamic object, which we do want to handle here. */
855 && ((abfd->flags & DYNAMIC) == 0
859 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
860 respectively, is from a dynamic object. */
862 if ((abfd->flags & DYNAMIC) != 0)
868 olddyn = (oldbfd->flags & DYNAMIC) != 0;
873 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
874 indices used by MIPS ELF. */
875 switch (h->root.type)
881 case bfd_link_hash_defined:
882 case bfd_link_hash_defweak:
883 hsec = h->root.u.def.section;
886 case bfd_link_hash_common:
887 hsec = h->root.u.c.p->section;
894 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
897 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
898 respectively, appear to be a definition rather than reference. */
900 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
905 if (h->root.type == bfd_link_hash_undefined
906 || h->root.type == bfd_link_hash_undefweak
907 || h->root.type == bfd_link_hash_common)
912 /* Check TLS symbol. */
913 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
914 && ELF_ST_TYPE (sym->st_info) != h->type)
917 bfd_boolean ntdef, tdef;
918 asection *ntsec, *tsec;
920 if (h->type == STT_TLS)
940 (*_bfd_error_handler)
941 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
942 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
943 else if (!tdef && !ntdef)
944 (*_bfd_error_handler)
945 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
946 tbfd, ntbfd, h->root.root.string);
948 (*_bfd_error_handler)
949 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
950 tbfd, tsec, ntbfd, h->root.root.string);
952 (*_bfd_error_handler)
953 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
954 tbfd, ntbfd, ntsec, h->root.root.string);
956 bfd_set_error (bfd_error_bad_value);
960 /* We need to remember if a symbol has a definition in a dynamic
961 object or is weak in all dynamic objects. Internal and hidden
962 visibility will make it unavailable to dynamic objects. */
963 if (newdyn && !h->dynamic_def)
965 if (!bfd_is_und_section (sec))
969 /* Check if this symbol is weak in all dynamic objects. If it
970 is the first time we see it in a dynamic object, we mark
971 if it is weak. Otherwise, we clear it. */
974 if (bind == STB_WEAK)
977 else if (bind != STB_WEAK)
982 /* If the old symbol has non-default visibility, we ignore the new
983 definition from a dynamic object. */
985 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
986 && !bfd_is_und_section (sec))
989 /* Make sure this symbol is dynamic. */
991 /* A protected symbol has external availability. Make sure it is
994 FIXME: Should we check type and size for protected symbol? */
995 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
996 return bfd_elf_link_record_dynamic_symbol (info, h);
1001 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1004 /* If the new symbol with non-default visibility comes from a
1005 relocatable file and the old definition comes from a dynamic
1006 object, we remove the old definition. */
1007 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1010 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1011 && bfd_is_und_section (sec))
1013 /* If the new symbol is undefined and the old symbol was
1014 also undefined before, we need to make sure
1015 _bfd_generic_link_add_one_symbol doesn't mess
1016 up the linker hash table undefs list. Since the old
1017 definition came from a dynamic object, it is still on the
1019 h->root.type = bfd_link_hash_undefined;
1020 h->root.u.undef.abfd = abfd;
1024 h->root.type = bfd_link_hash_new;
1025 h->root.u.undef.abfd = NULL;
1034 /* FIXME: Should we check type and size for protected symbol? */
1040 /* Differentiate strong and weak symbols. */
1041 newweak = bind == STB_WEAK;
1042 oldweak = (h->root.type == bfd_link_hash_defweak
1043 || h->root.type == bfd_link_hash_undefweak);
1045 /* If a new weak symbol definition comes from a regular file and the
1046 old symbol comes from a dynamic library, we treat the new one as
1047 strong. Similarly, an old weak symbol definition from a regular
1048 file is treated as strong when the new symbol comes from a dynamic
1049 library. Further, an old weak symbol from a dynamic library is
1050 treated as strong if the new symbol is from a dynamic library.
1051 This reflects the way glibc's ld.so works.
1053 Do this before setting *type_change_ok or *size_change_ok so that
1054 we warn properly when dynamic library symbols are overridden. */
1056 if (newdef && !newdyn && olddyn)
1058 if (olddef && newdyn)
1061 /* It's OK to change the type if either the existing symbol or the
1062 new symbol is weak. A type change is also OK if the old symbol
1063 is undefined and the new symbol is defined. */
1068 && h->root.type == bfd_link_hash_undefined))
1069 *type_change_ok = TRUE;
1071 /* It's OK to change the size if either the existing symbol or the
1072 new symbol is weak, or if the old symbol is undefined. */
1075 || h->root.type == bfd_link_hash_undefined)
1076 *size_change_ok = TRUE;
1078 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1079 symbol, respectively, appears to be a common symbol in a dynamic
1080 object. If a symbol appears in an uninitialized section, and is
1081 not weak, and is not a function, then it may be a common symbol
1082 which was resolved when the dynamic object was created. We want
1083 to treat such symbols specially, because they raise special
1084 considerations when setting the symbol size: if the symbol
1085 appears as a common symbol in a regular object, and the size in
1086 the regular object is larger, we must make sure that we use the
1087 larger size. This problematic case can always be avoided in C,
1088 but it must be handled correctly when using Fortran shared
1091 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1092 likewise for OLDDYNCOMMON and OLDDEF.
1094 Note that this test is just a heuristic, and that it is quite
1095 possible to have an uninitialized symbol in a shared object which
1096 is really a definition, rather than a common symbol. This could
1097 lead to some minor confusion when the symbol really is a common
1098 symbol in some regular object. However, I think it will be
1104 && (sec->flags & SEC_ALLOC) != 0
1105 && (sec->flags & SEC_LOAD) == 0
1107 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1108 newdyncommon = TRUE;
1110 newdyncommon = FALSE;
1114 && h->root.type == bfd_link_hash_defined
1116 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1117 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1119 && h->type != STT_FUNC)
1120 olddyncommon = TRUE;
1122 olddyncommon = FALSE;
1124 /* If both the old and the new symbols look like common symbols in a
1125 dynamic object, set the size of the symbol to the larger of the
1130 && sym->st_size != h->size)
1132 /* Since we think we have two common symbols, issue a multiple
1133 common warning if desired. Note that we only warn if the
1134 size is different. If the size is the same, we simply let
1135 the old symbol override the new one as normally happens with
1136 symbols defined in dynamic objects. */
1138 if (! ((*info->callbacks->multiple_common)
1139 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1140 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1143 if (sym->st_size > h->size)
1144 h->size = sym->st_size;
1146 *size_change_ok = TRUE;
1149 /* If we are looking at a dynamic object, and we have found a
1150 definition, we need to see if the symbol was already defined by
1151 some other object. If so, we want to use the existing
1152 definition, and we do not want to report a multiple symbol
1153 definition error; we do this by clobbering *PSEC to be
1154 bfd_und_section_ptr.
1156 We treat a common symbol as a definition if the symbol in the
1157 shared library is a function, since common symbols always
1158 represent variables; this can cause confusion in principle, but
1159 any such confusion would seem to indicate an erroneous program or
1160 shared library. We also permit a common symbol in a regular
1161 object to override a weak symbol in a shared object. */
1166 || (h->root.type == bfd_link_hash_common
1168 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1172 newdyncommon = FALSE;
1174 *psec = sec = bfd_und_section_ptr;
1175 *size_change_ok = TRUE;
1177 /* If we get here when the old symbol is a common symbol, then
1178 we are explicitly letting it override a weak symbol or
1179 function in a dynamic object, and we don't want to warn about
1180 a type change. If the old symbol is a defined symbol, a type
1181 change warning may still be appropriate. */
1183 if (h->root.type == bfd_link_hash_common)
1184 *type_change_ok = TRUE;
1187 /* Handle the special case of an old common symbol merging with a
1188 new symbol which looks like a common symbol in a shared object.
1189 We change *PSEC and *PVALUE to make the new symbol look like a
1190 common symbol, and let _bfd_generic_link_add_one_symbol do the
1194 && h->root.type == bfd_link_hash_common)
1198 newdyncommon = FALSE;
1199 *pvalue = sym->st_size;
1200 *psec = sec = bfd_com_section_ptr;
1201 *size_change_ok = TRUE;
1204 /* Skip weak definitions of symbols that are already defined. */
1205 if (newdef && olddef && newweak && !oldweak)
1208 /* If the old symbol is from a dynamic object, and the new symbol is
1209 a definition which is not from a dynamic object, then the new
1210 symbol overrides the old symbol. Symbols from regular files
1211 always take precedence over symbols from dynamic objects, even if
1212 they are defined after the dynamic object in the link.
1214 As above, we again permit a common symbol in a regular object to
1215 override a definition in a shared object if the shared object
1216 symbol is a function or is weak. */
1221 || (bfd_is_com_section (sec)
1223 || h->type == STT_FUNC)))
1228 /* Change the hash table entry to undefined, and let
1229 _bfd_generic_link_add_one_symbol do the right thing with the
1232 h->root.type = bfd_link_hash_undefined;
1233 h->root.u.undef.abfd = h->root.u.def.section->owner;
1234 *size_change_ok = TRUE;
1237 olddyncommon = FALSE;
1239 /* We again permit a type change when a common symbol may be
1240 overriding a function. */
1242 if (bfd_is_com_section (sec))
1243 *type_change_ok = TRUE;
1245 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1248 /* This union may have been set to be non-NULL when this symbol
1249 was seen in a dynamic object. We must force the union to be
1250 NULL, so that it is correct for a regular symbol. */
1251 h->verinfo.vertree = NULL;
1254 /* Handle the special case of a new common symbol merging with an
1255 old symbol that looks like it might be a common symbol defined in
1256 a shared object. Note that we have already handled the case in
1257 which a new common symbol should simply override the definition
1258 in the shared library. */
1261 && bfd_is_com_section (sec)
1264 /* It would be best if we could set the hash table entry to a
1265 common symbol, but we don't know what to use for the section
1266 or the alignment. */
1267 if (! ((*info->callbacks->multiple_common)
1268 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1269 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1272 /* If the presumed common symbol in the dynamic object is
1273 larger, pretend that the new symbol has its size. */
1275 if (h->size > *pvalue)
1278 /* We need to remember the alignment required by the symbol
1279 in the dynamic object. */
1280 BFD_ASSERT (pold_alignment);
1281 *pold_alignment = h->root.u.def.section->alignment_power;
1284 olddyncommon = FALSE;
1286 h->root.type = bfd_link_hash_undefined;
1287 h->root.u.undef.abfd = h->root.u.def.section->owner;
1289 *size_change_ok = TRUE;
1290 *type_change_ok = TRUE;
1292 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1295 h->verinfo.vertree = NULL;
1300 /* Handle the case where we had a versioned symbol in a dynamic
1301 library and now find a definition in a normal object. In this
1302 case, we make the versioned symbol point to the normal one. */
1303 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1304 flip->root.type = h->root.type;
1305 h->root.type = bfd_link_hash_indirect;
1306 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1307 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1308 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1312 flip->ref_dynamic = 1;
1319 /* This function is called to create an indirect symbol from the
1320 default for the symbol with the default version if needed. The
1321 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1322 set DYNSYM if the new indirect symbol is dynamic. */
1325 _bfd_elf_add_default_symbol (bfd *abfd,
1326 struct bfd_link_info *info,
1327 struct elf_link_hash_entry *h,
1329 Elf_Internal_Sym *sym,
1332 bfd_boolean *dynsym,
1333 bfd_boolean override)
1335 bfd_boolean type_change_ok;
1336 bfd_boolean size_change_ok;
1339 struct elf_link_hash_entry *hi;
1340 struct bfd_link_hash_entry *bh;
1341 const struct elf_backend_data *bed;
1342 bfd_boolean collect;
1343 bfd_boolean dynamic;
1345 size_t len, shortlen;
1348 /* If this symbol has a version, and it is the default version, we
1349 create an indirect symbol from the default name to the fully
1350 decorated name. This will cause external references which do not
1351 specify a version to be bound to this version of the symbol. */
1352 p = strchr (name, ELF_VER_CHR);
1353 if (p == NULL || p[1] != ELF_VER_CHR)
1358 /* We are overridden by an old definition. We need to check if we
1359 need to create the indirect symbol from the default name. */
1360 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1362 BFD_ASSERT (hi != NULL);
1365 while (hi->root.type == bfd_link_hash_indirect
1366 || hi->root.type == bfd_link_hash_warning)
1368 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1374 bed = get_elf_backend_data (abfd);
1375 collect = bed->collect;
1376 dynamic = (abfd->flags & DYNAMIC) != 0;
1378 shortlen = p - name;
1379 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1380 if (shortname == NULL)
1382 memcpy (shortname, name, shortlen);
1383 shortname[shortlen] = '\0';
1385 /* We are going to create a new symbol. Merge it with any existing
1386 symbol with this name. For the purposes of the merge, act as
1387 though we were defining the symbol we just defined, although we
1388 actually going to define an indirect symbol. */
1389 type_change_ok = FALSE;
1390 size_change_ok = FALSE;
1392 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1393 NULL, &hi, &skip, &override,
1394 &type_change_ok, &size_change_ok))
1403 if (! (_bfd_generic_link_add_one_symbol
1404 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1405 0, name, FALSE, collect, &bh)))
1407 hi = (struct elf_link_hash_entry *) bh;
1411 /* In this case the symbol named SHORTNAME is overriding the
1412 indirect symbol we want to add. We were planning on making
1413 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1414 is the name without a version. NAME is the fully versioned
1415 name, and it is the default version.
1417 Overriding means that we already saw a definition for the
1418 symbol SHORTNAME in a regular object, and it is overriding
1419 the symbol defined in the dynamic object.
1421 When this happens, we actually want to change NAME, the
1422 symbol we just added, to refer to SHORTNAME. This will cause
1423 references to NAME in the shared object to become references
1424 to SHORTNAME in the regular object. This is what we expect
1425 when we override a function in a shared object: that the
1426 references in the shared object will be mapped to the
1427 definition in the regular object. */
1429 while (hi->root.type == bfd_link_hash_indirect
1430 || hi->root.type == bfd_link_hash_warning)
1431 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1433 h->root.type = bfd_link_hash_indirect;
1434 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1438 hi->ref_dynamic = 1;
1442 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1447 /* Now set HI to H, so that the following code will set the
1448 other fields correctly. */
1452 /* If there is a duplicate definition somewhere, then HI may not
1453 point to an indirect symbol. We will have reported an error to
1454 the user in that case. */
1456 if (hi->root.type == bfd_link_hash_indirect)
1458 struct elf_link_hash_entry *ht;
1460 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1461 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1463 /* See if the new flags lead us to realize that the symbol must
1475 if (hi->ref_regular)
1481 /* We also need to define an indirection from the nondefault version
1485 len = strlen (name);
1486 shortname = bfd_hash_allocate (&info->hash->table, len);
1487 if (shortname == NULL)
1489 memcpy (shortname, name, shortlen);
1490 memcpy (shortname + shortlen, p + 1, len - shortlen);
1492 /* Once again, merge with any existing symbol. */
1493 type_change_ok = FALSE;
1494 size_change_ok = FALSE;
1496 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1497 NULL, &hi, &skip, &override,
1498 &type_change_ok, &size_change_ok))
1506 /* Here SHORTNAME is a versioned name, so we don't expect to see
1507 the type of override we do in the case above unless it is
1508 overridden by a versioned definition. */
1509 if (hi->root.type != bfd_link_hash_defined
1510 && hi->root.type != bfd_link_hash_defweak)
1511 (*_bfd_error_handler)
1512 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1518 if (! (_bfd_generic_link_add_one_symbol
1519 (info, abfd, shortname, BSF_INDIRECT,
1520 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1522 hi = (struct elf_link_hash_entry *) bh;
1524 /* If there is a duplicate definition somewhere, then HI may not
1525 point to an indirect symbol. We will have reported an error
1526 to the user in that case. */
1528 if (hi->root.type == bfd_link_hash_indirect)
1530 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1532 /* See if the new flags lead us to realize that the symbol
1544 if (hi->ref_regular)
1554 /* This routine is used to export all defined symbols into the dynamic
1555 symbol table. It is called via elf_link_hash_traverse. */
1558 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1560 struct elf_info_failed *eif = data;
1562 /* Ignore indirect symbols. These are added by the versioning code. */
1563 if (h->root.type == bfd_link_hash_indirect)
1566 if (h->root.type == bfd_link_hash_warning)
1567 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1569 if (h->dynindx == -1
1573 struct bfd_elf_version_tree *t;
1574 struct bfd_elf_version_expr *d;
1576 for (t = eif->verdefs; t != NULL; t = t->next)
1578 if (t->globals.list != NULL)
1580 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1585 if (t->locals.list != NULL)
1587 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1596 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1607 /* Look through the symbols which are defined in other shared
1608 libraries and referenced here. Update the list of version
1609 dependencies. This will be put into the .gnu.version_r section.
1610 This function is called via elf_link_hash_traverse. */
1613 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1616 struct elf_find_verdep_info *rinfo = data;
1617 Elf_Internal_Verneed *t;
1618 Elf_Internal_Vernaux *a;
1621 if (h->root.type == bfd_link_hash_warning)
1622 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1624 /* We only care about symbols defined in shared objects with version
1629 || h->verinfo.verdef == NULL)
1632 /* See if we already know about this version. */
1633 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1635 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1638 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1639 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1645 /* This is a new version. Add it to tree we are building. */
1650 t = bfd_zalloc (rinfo->output_bfd, amt);
1653 rinfo->failed = TRUE;
1657 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1658 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1659 elf_tdata (rinfo->output_bfd)->verref = t;
1663 a = bfd_zalloc (rinfo->output_bfd, amt);
1665 /* Note that we are copying a string pointer here, and testing it
1666 above. If bfd_elf_string_from_elf_section is ever changed to
1667 discard the string data when low in memory, this will have to be
1669 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1671 a->vna_flags = h->verinfo.verdef->vd_flags;
1672 a->vna_nextptr = t->vn_auxptr;
1674 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1677 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1684 /* Figure out appropriate versions for all the symbols. We may not
1685 have the version number script until we have read all of the input
1686 files, so until that point we don't know which symbols should be
1687 local. This function is called via elf_link_hash_traverse. */
1690 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1692 struct elf_assign_sym_version_info *sinfo;
1693 struct bfd_link_info *info;
1694 const struct elf_backend_data *bed;
1695 struct elf_info_failed eif;
1702 if (h->root.type == bfd_link_hash_warning)
1703 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1705 /* Fix the symbol flags. */
1708 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1711 sinfo->failed = TRUE;
1715 /* We only need version numbers for symbols defined in regular
1717 if (!h->def_regular)
1720 bed = get_elf_backend_data (sinfo->output_bfd);
1721 p = strchr (h->root.root.string, ELF_VER_CHR);
1722 if (p != NULL && h->verinfo.vertree == NULL)
1724 struct bfd_elf_version_tree *t;
1729 /* There are two consecutive ELF_VER_CHR characters if this is
1730 not a hidden symbol. */
1732 if (*p == ELF_VER_CHR)
1738 /* If there is no version string, we can just return out. */
1746 /* Look for the version. If we find it, it is no longer weak. */
1747 for (t = sinfo->verdefs; t != NULL; t = t->next)
1749 if (strcmp (t->name, p) == 0)
1753 struct bfd_elf_version_expr *d;
1755 len = p - h->root.root.string;
1756 alc = bfd_malloc (len);
1759 memcpy (alc, h->root.root.string, len - 1);
1760 alc[len - 1] = '\0';
1761 if (alc[len - 2] == ELF_VER_CHR)
1762 alc[len - 2] = '\0';
1764 h->verinfo.vertree = t;
1768 if (t->globals.list != NULL)
1769 d = (*t->match) (&t->globals, NULL, alc);
1771 /* See if there is anything to force this symbol to
1773 if (d == NULL && t->locals.list != NULL)
1775 d = (*t->match) (&t->locals, NULL, alc);
1778 && ! info->export_dynamic)
1779 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1787 /* If we are building an application, we need to create a
1788 version node for this version. */
1789 if (t == NULL && info->executable)
1791 struct bfd_elf_version_tree **pp;
1794 /* If we aren't going to export this symbol, we don't need
1795 to worry about it. */
1796 if (h->dynindx == -1)
1800 t = bfd_zalloc (sinfo->output_bfd, amt);
1803 sinfo->failed = TRUE;
1808 t->name_indx = (unsigned int) -1;
1812 /* Don't count anonymous version tag. */
1813 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1815 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1817 t->vernum = version_index;
1821 h->verinfo.vertree = t;
1825 /* We could not find the version for a symbol when
1826 generating a shared archive. Return an error. */
1827 (*_bfd_error_handler)
1828 (_("%B: undefined versioned symbol name %s"),
1829 sinfo->output_bfd, h->root.root.string);
1830 bfd_set_error (bfd_error_bad_value);
1831 sinfo->failed = TRUE;
1839 /* If we don't have a version for this symbol, see if we can find
1841 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1843 struct bfd_elf_version_tree *t;
1844 struct bfd_elf_version_tree *local_ver;
1845 struct bfd_elf_version_expr *d;
1847 /* See if can find what version this symbol is in. If the
1848 symbol is supposed to be local, then don't actually register
1851 for (t = sinfo->verdefs; t != NULL; t = t->next)
1853 if (t->globals.list != NULL)
1855 bfd_boolean matched;
1859 while ((d = (*t->match) (&t->globals, d,
1860 h->root.root.string)) != NULL)
1865 /* There is a version without definition. Make
1866 the symbol the default definition for this
1868 h->verinfo.vertree = t;
1876 /* There is no undefined version for this symbol. Hide the
1878 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1881 if (t->locals.list != NULL)
1884 while ((d = (*t->match) (&t->locals, d,
1885 h->root.root.string)) != NULL)
1888 /* If the match is "*", keep looking for a more
1889 explicit, perhaps even global, match.
1890 XXX: Shouldn't this be !d->wildcard instead? */
1891 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1900 if (local_ver != NULL)
1902 h->verinfo.vertree = local_ver;
1903 if (h->dynindx != -1
1904 && ! info->export_dynamic)
1906 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1914 /* Read and swap the relocs from the section indicated by SHDR. This
1915 may be either a REL or a RELA section. The relocations are
1916 translated into RELA relocations and stored in INTERNAL_RELOCS,
1917 which should have already been allocated to contain enough space.
1918 The EXTERNAL_RELOCS are a buffer where the external form of the
1919 relocations should be stored.
1921 Returns FALSE if something goes wrong. */
1924 elf_link_read_relocs_from_section (bfd *abfd,
1926 Elf_Internal_Shdr *shdr,
1927 void *external_relocs,
1928 Elf_Internal_Rela *internal_relocs)
1930 const struct elf_backend_data *bed;
1931 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1932 const bfd_byte *erela;
1933 const bfd_byte *erelaend;
1934 Elf_Internal_Rela *irela;
1935 Elf_Internal_Shdr *symtab_hdr;
1938 /* Position ourselves at the start of the section. */
1939 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1942 /* Read the relocations. */
1943 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1946 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1947 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1949 bed = get_elf_backend_data (abfd);
1951 /* Convert the external relocations to the internal format. */
1952 if (shdr->sh_entsize == bed->s->sizeof_rel)
1953 swap_in = bed->s->swap_reloc_in;
1954 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1955 swap_in = bed->s->swap_reloca_in;
1958 bfd_set_error (bfd_error_wrong_format);
1962 erela = external_relocs;
1963 erelaend = erela + shdr->sh_size;
1964 irela = internal_relocs;
1965 while (erela < erelaend)
1969 (*swap_in) (abfd, erela, irela);
1970 r_symndx = ELF32_R_SYM (irela->r_info);
1971 if (bed->s->arch_size == 64)
1973 if ((size_t) r_symndx >= nsyms)
1975 (*_bfd_error_handler)
1976 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1977 " for offset 0x%lx in section `%A'"),
1979 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1980 bfd_set_error (bfd_error_bad_value);
1983 irela += bed->s->int_rels_per_ext_rel;
1984 erela += shdr->sh_entsize;
1990 /* Read and swap the relocs for a section O. They may have been
1991 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1992 not NULL, they are used as buffers to read into. They are known to
1993 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1994 the return value is allocated using either malloc or bfd_alloc,
1995 according to the KEEP_MEMORY argument. If O has two relocation
1996 sections (both REL and RELA relocations), then the REL_HDR
1997 relocations will appear first in INTERNAL_RELOCS, followed by the
1998 REL_HDR2 relocations. */
2001 _bfd_elf_link_read_relocs (bfd *abfd,
2003 void *external_relocs,
2004 Elf_Internal_Rela *internal_relocs,
2005 bfd_boolean keep_memory)
2007 Elf_Internal_Shdr *rel_hdr;
2008 void *alloc1 = NULL;
2009 Elf_Internal_Rela *alloc2 = NULL;
2010 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2012 if (elf_section_data (o)->relocs != NULL)
2013 return elf_section_data (o)->relocs;
2015 if (o->reloc_count == 0)
2018 rel_hdr = &elf_section_data (o)->rel_hdr;
2020 if (internal_relocs == NULL)
2024 size = o->reloc_count;
2025 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2027 internal_relocs = bfd_alloc (abfd, size);
2029 internal_relocs = alloc2 = bfd_malloc (size);
2030 if (internal_relocs == NULL)
2034 if (external_relocs == NULL)
2036 bfd_size_type size = rel_hdr->sh_size;
2038 if (elf_section_data (o)->rel_hdr2)
2039 size += elf_section_data (o)->rel_hdr2->sh_size;
2040 alloc1 = bfd_malloc (size);
2043 external_relocs = alloc1;
2046 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2050 if (elf_section_data (o)->rel_hdr2
2051 && (!elf_link_read_relocs_from_section
2053 elf_section_data (o)->rel_hdr2,
2054 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2055 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2056 * bed->s->int_rels_per_ext_rel))))
2059 /* Cache the results for next time, if we can. */
2061 elf_section_data (o)->relocs = internal_relocs;
2066 /* Don't free alloc2, since if it was allocated we are passing it
2067 back (under the name of internal_relocs). */
2069 return internal_relocs;
2079 /* Compute the size of, and allocate space for, REL_HDR which is the
2080 section header for a section containing relocations for O. */
2083 _bfd_elf_link_size_reloc_section (bfd *abfd,
2084 Elf_Internal_Shdr *rel_hdr,
2087 bfd_size_type reloc_count;
2088 bfd_size_type num_rel_hashes;
2090 /* Figure out how many relocations there will be. */
2091 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2092 reloc_count = elf_section_data (o)->rel_count;
2094 reloc_count = elf_section_data (o)->rel_count2;
2096 num_rel_hashes = o->reloc_count;
2097 if (num_rel_hashes < reloc_count)
2098 num_rel_hashes = reloc_count;
2100 /* That allows us to calculate the size of the section. */
2101 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2103 /* The contents field must last into write_object_contents, so we
2104 allocate it with bfd_alloc rather than malloc. Also since we
2105 cannot be sure that the contents will actually be filled in,
2106 we zero the allocated space. */
2107 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2108 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2111 /* We only allocate one set of hash entries, so we only do it the
2112 first time we are called. */
2113 if (elf_section_data (o)->rel_hashes == NULL
2116 struct elf_link_hash_entry **p;
2118 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2122 elf_section_data (o)->rel_hashes = p;
2128 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2129 originated from the section given by INPUT_REL_HDR) to the
2133 _bfd_elf_link_output_relocs (bfd *output_bfd,
2134 asection *input_section,
2135 Elf_Internal_Shdr *input_rel_hdr,
2136 Elf_Internal_Rela *internal_relocs,
2137 struct elf_link_hash_entry **rel_hash
2140 Elf_Internal_Rela *irela;
2141 Elf_Internal_Rela *irelaend;
2143 Elf_Internal_Shdr *output_rel_hdr;
2144 asection *output_section;
2145 unsigned int *rel_countp = NULL;
2146 const struct elf_backend_data *bed;
2147 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2149 output_section = input_section->output_section;
2150 output_rel_hdr = NULL;
2152 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2153 == input_rel_hdr->sh_entsize)
2155 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2156 rel_countp = &elf_section_data (output_section)->rel_count;
2158 else if (elf_section_data (output_section)->rel_hdr2
2159 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2160 == input_rel_hdr->sh_entsize))
2162 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2163 rel_countp = &elf_section_data (output_section)->rel_count2;
2167 (*_bfd_error_handler)
2168 (_("%B: relocation size mismatch in %B section %A"),
2169 output_bfd, input_section->owner, input_section);
2170 bfd_set_error (bfd_error_wrong_object_format);
2174 bed = get_elf_backend_data (output_bfd);
2175 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2176 swap_out = bed->s->swap_reloc_out;
2177 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2178 swap_out = bed->s->swap_reloca_out;
2182 erel = output_rel_hdr->contents;
2183 erel += *rel_countp * input_rel_hdr->sh_entsize;
2184 irela = internal_relocs;
2185 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2186 * bed->s->int_rels_per_ext_rel);
2187 while (irela < irelaend)
2189 (*swap_out) (output_bfd, irela, erel);
2190 irela += bed->s->int_rels_per_ext_rel;
2191 erel += input_rel_hdr->sh_entsize;
2194 /* Bump the counter, so that we know where to add the next set of
2196 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2201 /* Fix up the flags for a symbol. This handles various cases which
2202 can only be fixed after all the input files are seen. This is
2203 currently called by both adjust_dynamic_symbol and
2204 assign_sym_version, which is unnecessary but perhaps more robust in
2205 the face of future changes. */
2208 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2209 struct elf_info_failed *eif)
2211 /* If this symbol was mentioned in a non-ELF file, try to set
2212 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2213 permit a non-ELF file to correctly refer to a symbol defined in
2214 an ELF dynamic object. */
2217 while (h->root.type == bfd_link_hash_indirect)
2218 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2220 if (h->root.type != bfd_link_hash_defined
2221 && h->root.type != bfd_link_hash_defweak)
2224 h->ref_regular_nonweak = 1;
2228 if (h->root.u.def.section->owner != NULL
2229 && (bfd_get_flavour (h->root.u.def.section->owner)
2230 == bfd_target_elf_flavour))
2233 h->ref_regular_nonweak = 1;
2239 if (h->dynindx == -1
2243 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2252 /* Unfortunately, NON_ELF is only correct if the symbol
2253 was first seen in a non-ELF file. Fortunately, if the symbol
2254 was first seen in an ELF file, we're probably OK unless the
2255 symbol was defined in a non-ELF file. Catch that case here.
2256 FIXME: We're still in trouble if the symbol was first seen in
2257 a dynamic object, and then later in a non-ELF regular object. */
2258 if ((h->root.type == bfd_link_hash_defined
2259 || h->root.type == bfd_link_hash_defweak)
2261 && (h->root.u.def.section->owner != NULL
2262 ? (bfd_get_flavour (h->root.u.def.section->owner)
2263 != bfd_target_elf_flavour)
2264 : (bfd_is_abs_section (h->root.u.def.section)
2265 && !h->def_dynamic)))
2269 /* If this is a final link, and the symbol was defined as a common
2270 symbol in a regular object file, and there was no definition in
2271 any dynamic object, then the linker will have allocated space for
2272 the symbol in a common section but the DEF_REGULAR
2273 flag will not have been set. */
2274 if (h->root.type == bfd_link_hash_defined
2278 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2281 /* If -Bsymbolic was used (which means to bind references to global
2282 symbols to the definition within the shared object), and this
2283 symbol was defined in a regular object, then it actually doesn't
2284 need a PLT entry. Likewise, if the symbol has non-default
2285 visibility. If the symbol has hidden or internal visibility, we
2286 will force it local. */
2288 && eif->info->shared
2289 && is_elf_hash_table (eif->info->hash)
2290 && (eif->info->symbolic
2291 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2294 const struct elf_backend_data *bed;
2295 bfd_boolean force_local;
2297 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2299 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2300 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2301 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2304 /* If a weak undefined symbol has non-default visibility, we also
2305 hide it from the dynamic linker. */
2306 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2307 && h->root.type == bfd_link_hash_undefweak)
2309 const struct elf_backend_data *bed;
2310 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2311 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2314 /* If this is a weak defined symbol in a dynamic object, and we know
2315 the real definition in the dynamic object, copy interesting flags
2316 over to the real definition. */
2317 if (h->u.weakdef != NULL)
2319 struct elf_link_hash_entry *weakdef;
2321 weakdef = h->u.weakdef;
2322 if (h->root.type == bfd_link_hash_indirect)
2323 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2325 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2326 || h->root.type == bfd_link_hash_defweak);
2327 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2328 || weakdef->root.type == bfd_link_hash_defweak);
2329 BFD_ASSERT (weakdef->def_dynamic);
2331 /* If the real definition is defined by a regular object file,
2332 don't do anything special. See the longer description in
2333 _bfd_elf_adjust_dynamic_symbol, below. */
2334 if (weakdef->def_regular)
2335 h->u.weakdef = NULL;
2338 const struct elf_backend_data *bed;
2340 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2341 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2348 /* Make the backend pick a good value for a dynamic symbol. This is
2349 called via elf_link_hash_traverse, and also calls itself
2353 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2355 struct elf_info_failed *eif = data;
2357 const struct elf_backend_data *bed;
2359 if (! is_elf_hash_table (eif->info->hash))
2362 if (h->root.type == bfd_link_hash_warning)
2364 h->got = elf_hash_table (eif->info)->init_got_offset;
2365 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2367 /* When warning symbols are created, they **replace** the "real"
2368 entry in the hash table, thus we never get to see the real
2369 symbol in a hash traversal. So look at it now. */
2370 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2373 /* Ignore indirect symbols. These are added by the versioning code. */
2374 if (h->root.type == bfd_link_hash_indirect)
2377 /* Fix the symbol flags. */
2378 if (! _bfd_elf_fix_symbol_flags (h, eif))
2381 /* If this symbol does not require a PLT entry, and it is not
2382 defined by a dynamic object, or is not referenced by a regular
2383 object, ignore it. We do have to handle a weak defined symbol,
2384 even if no regular object refers to it, if we decided to add it
2385 to the dynamic symbol table. FIXME: Do we normally need to worry
2386 about symbols which are defined by one dynamic object and
2387 referenced by another one? */
2392 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2394 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2398 /* If we've already adjusted this symbol, don't do it again. This
2399 can happen via a recursive call. */
2400 if (h->dynamic_adjusted)
2403 /* Don't look at this symbol again. Note that we must set this
2404 after checking the above conditions, because we may look at a
2405 symbol once, decide not to do anything, and then get called
2406 recursively later after REF_REGULAR is set below. */
2407 h->dynamic_adjusted = 1;
2409 /* If this is a weak definition, and we know a real definition, and
2410 the real symbol is not itself defined by a regular object file,
2411 then get a good value for the real definition. We handle the
2412 real symbol first, for the convenience of the backend routine.
2414 Note that there is a confusing case here. If the real definition
2415 is defined by a regular object file, we don't get the real symbol
2416 from the dynamic object, but we do get the weak symbol. If the
2417 processor backend uses a COPY reloc, then if some routine in the
2418 dynamic object changes the real symbol, we will not see that
2419 change in the corresponding weak symbol. This is the way other
2420 ELF linkers work as well, and seems to be a result of the shared
2423 I will clarify this issue. Most SVR4 shared libraries define the
2424 variable _timezone and define timezone as a weak synonym. The
2425 tzset call changes _timezone. If you write
2426 extern int timezone;
2428 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2429 you might expect that, since timezone is a synonym for _timezone,
2430 the same number will print both times. However, if the processor
2431 backend uses a COPY reloc, then actually timezone will be copied
2432 into your process image, and, since you define _timezone
2433 yourself, _timezone will not. Thus timezone and _timezone will
2434 wind up at different memory locations. The tzset call will set
2435 _timezone, leaving timezone unchanged. */
2437 if (h->u.weakdef != NULL)
2439 /* If we get to this point, we know there is an implicit
2440 reference by a regular object file via the weak symbol H.
2441 FIXME: Is this really true? What if the traversal finds
2442 H->U.WEAKDEF before it finds H? */
2443 h->u.weakdef->ref_regular = 1;
2445 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2449 /* If a symbol has no type and no size and does not require a PLT
2450 entry, then we are probably about to do the wrong thing here: we
2451 are probably going to create a COPY reloc for an empty object.
2452 This case can arise when a shared object is built with assembly
2453 code, and the assembly code fails to set the symbol type. */
2455 && h->type == STT_NOTYPE
2457 (*_bfd_error_handler)
2458 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2459 h->root.root.string);
2461 dynobj = elf_hash_table (eif->info)->dynobj;
2462 bed = get_elf_backend_data (dynobj);
2463 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2472 /* Adjust all external symbols pointing into SEC_MERGE sections
2473 to reflect the object merging within the sections. */
2476 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2480 if (h->root.type == bfd_link_hash_warning)
2481 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2483 if ((h->root.type == bfd_link_hash_defined
2484 || h->root.type == bfd_link_hash_defweak)
2485 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2486 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2488 bfd *output_bfd = data;
2490 h->root.u.def.value =
2491 _bfd_merged_section_offset (output_bfd,
2492 &h->root.u.def.section,
2493 elf_section_data (sec)->sec_info,
2494 h->root.u.def.value);
2500 /* Returns false if the symbol referred to by H should be considered
2501 to resolve local to the current module, and true if it should be
2502 considered to bind dynamically. */
2505 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2506 struct bfd_link_info *info,
2507 bfd_boolean ignore_protected)
2509 bfd_boolean binding_stays_local_p;
2514 while (h->root.type == bfd_link_hash_indirect
2515 || h->root.type == bfd_link_hash_warning)
2516 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2518 /* If it was forced local, then clearly it's not dynamic. */
2519 if (h->dynindx == -1)
2521 if (h->forced_local)
2524 /* Identify the cases where name binding rules say that a
2525 visible symbol resolves locally. */
2526 binding_stays_local_p = info->executable || info->symbolic;
2528 switch (ELF_ST_VISIBILITY (h->other))
2535 /* Proper resolution for function pointer equality may require
2536 that these symbols perhaps be resolved dynamically, even though
2537 we should be resolving them to the current module. */
2538 if (!ignore_protected || h->type != STT_FUNC)
2539 binding_stays_local_p = TRUE;
2546 /* If it isn't defined locally, then clearly it's dynamic. */
2547 if (!h->def_regular)
2550 /* Otherwise, the symbol is dynamic if binding rules don't tell
2551 us that it remains local. */
2552 return !binding_stays_local_p;
2555 /* Return true if the symbol referred to by H should be considered
2556 to resolve local to the current module, and false otherwise. Differs
2557 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2558 undefined symbols and weak symbols. */
2561 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2562 struct bfd_link_info *info,
2563 bfd_boolean local_protected)
2565 /* If it's a local sym, of course we resolve locally. */
2569 /* Common symbols that become definitions don't get the DEF_REGULAR
2570 flag set, so test it first, and don't bail out. */
2571 if (ELF_COMMON_DEF_P (h))
2573 /* If we don't have a definition in a regular file, then we can't
2574 resolve locally. The sym is either undefined or dynamic. */
2575 else if (!h->def_regular)
2578 /* Forced local symbols resolve locally. */
2579 if (h->forced_local)
2582 /* As do non-dynamic symbols. */
2583 if (h->dynindx == -1)
2586 /* At this point, we know the symbol is defined and dynamic. In an
2587 executable it must resolve locally, likewise when building symbolic
2588 shared libraries. */
2589 if (info->executable || info->symbolic)
2592 /* Now deal with defined dynamic symbols in shared libraries. Ones
2593 with default visibility might not resolve locally. */
2594 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2597 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2598 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2601 /* STV_PROTECTED non-function symbols are local. */
2602 if (h->type != STT_FUNC)
2605 /* Function pointer equality tests may require that STV_PROTECTED
2606 symbols be treated as dynamic symbols, even when we know that the
2607 dynamic linker will resolve them locally. */
2608 return local_protected;
2611 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2612 aligned. Returns the first TLS output section. */
2614 struct bfd_section *
2615 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2617 struct bfd_section *sec, *tls;
2618 unsigned int align = 0;
2620 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2621 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2625 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2626 if (sec->alignment_power > align)
2627 align = sec->alignment_power;
2629 elf_hash_table (info)->tls_sec = tls;
2631 /* Ensure the alignment of the first section is the largest alignment,
2632 so that the tls segment starts aligned. */
2634 tls->alignment_power = align;
2639 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2641 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2642 Elf_Internal_Sym *sym)
2644 /* Local symbols do not count, but target specific ones might. */
2645 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2646 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2649 /* Function symbols do not count. */
2650 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2653 /* If the section is undefined, then so is the symbol. */
2654 if (sym->st_shndx == SHN_UNDEF)
2657 /* If the symbol is defined in the common section, then
2658 it is a common definition and so does not count. */
2659 if (sym->st_shndx == SHN_COMMON)
2662 /* If the symbol is in a target specific section then we
2663 must rely upon the backend to tell us what it is. */
2664 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2665 /* FIXME - this function is not coded yet:
2667 return _bfd_is_global_symbol_definition (abfd, sym);
2669 Instead for now assume that the definition is not global,
2670 Even if this is wrong, at least the linker will behave
2671 in the same way that it used to do. */
2677 /* Search the symbol table of the archive element of the archive ABFD
2678 whose archive map contains a mention of SYMDEF, and determine if
2679 the symbol is defined in this element. */
2681 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2683 Elf_Internal_Shdr * hdr;
2684 bfd_size_type symcount;
2685 bfd_size_type extsymcount;
2686 bfd_size_type extsymoff;
2687 Elf_Internal_Sym *isymbuf;
2688 Elf_Internal_Sym *isym;
2689 Elf_Internal_Sym *isymend;
2692 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2696 if (! bfd_check_format (abfd, bfd_object))
2699 /* If we have already included the element containing this symbol in the
2700 link then we do not need to include it again. Just claim that any symbol
2701 it contains is not a definition, so that our caller will not decide to
2702 (re)include this element. */
2703 if (abfd->archive_pass)
2706 /* Select the appropriate symbol table. */
2707 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2708 hdr = &elf_tdata (abfd)->symtab_hdr;
2710 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2712 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2714 /* The sh_info field of the symtab header tells us where the
2715 external symbols start. We don't care about the local symbols. */
2716 if (elf_bad_symtab (abfd))
2718 extsymcount = symcount;
2723 extsymcount = symcount - hdr->sh_info;
2724 extsymoff = hdr->sh_info;
2727 if (extsymcount == 0)
2730 /* Read in the symbol table. */
2731 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2733 if (isymbuf == NULL)
2736 /* Scan the symbol table looking for SYMDEF. */
2738 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2742 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2747 if (strcmp (name, symdef->name) == 0)
2749 result = is_global_data_symbol_definition (abfd, isym);
2759 /* Add an entry to the .dynamic table. */
2762 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2766 struct elf_link_hash_table *hash_table;
2767 const struct elf_backend_data *bed;
2769 bfd_size_type newsize;
2770 bfd_byte *newcontents;
2771 Elf_Internal_Dyn dyn;
2773 hash_table = elf_hash_table (info);
2774 if (! is_elf_hash_table (hash_table))
2777 if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
2779 (_("warning: creating a DT_TEXTREL in a shared object."));
2781 bed = get_elf_backend_data (hash_table->dynobj);
2782 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2783 BFD_ASSERT (s != NULL);
2785 newsize = s->size + bed->s->sizeof_dyn;
2786 newcontents = bfd_realloc (s->contents, newsize);
2787 if (newcontents == NULL)
2791 dyn.d_un.d_val = val;
2792 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2795 s->contents = newcontents;
2800 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2801 otherwise just check whether one already exists. Returns -1 on error,
2802 1 if a DT_NEEDED tag already exists, and 0 on success. */
2805 elf_add_dt_needed_tag (bfd *abfd,
2806 struct bfd_link_info *info,
2810 struct elf_link_hash_table *hash_table;
2811 bfd_size_type oldsize;
2812 bfd_size_type strindex;
2814 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2817 hash_table = elf_hash_table (info);
2818 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2819 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2820 if (strindex == (bfd_size_type) -1)
2823 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2826 const struct elf_backend_data *bed;
2829 bed = get_elf_backend_data (hash_table->dynobj);
2830 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2832 for (extdyn = sdyn->contents;
2833 extdyn < sdyn->contents + sdyn->size;
2834 extdyn += bed->s->sizeof_dyn)
2836 Elf_Internal_Dyn dyn;
2838 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2839 if (dyn.d_tag == DT_NEEDED
2840 && dyn.d_un.d_val == strindex)
2842 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2850 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
2853 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2857 /* We were just checking for existence of the tag. */
2858 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2863 /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols
2864 belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no
2865 references from regular objects to these symbols.
2867 ??? Should we do something about references from other dynamic
2868 obects? If not, we potentially lose some warnings about undefined
2869 symbols. But how can we recover the initial undefined / undefweak
2872 struct elf_smash_syms_data
2875 struct elf_link_hash_table *htab;
2876 bfd_boolean twiddled;
2880 elf_smash_syms (struct elf_link_hash_entry *h, void *data)
2882 struct elf_smash_syms_data *inf = (struct elf_smash_syms_data *) data;
2883 struct bfd_link_hash_entry *bh;
2885 switch (h->root.type)
2888 case bfd_link_hash_new:
2891 case bfd_link_hash_undefined:
2892 if (h->root.u.undef.abfd != inf->not_needed)
2894 if (h->root.u.undef.weak != NULL
2895 && h->root.u.undef.weak != inf->not_needed)
2897 /* Symbol was undefweak in u.undef.weak bfd, and has become
2898 undefined in as-needed lib. Restore weak. */
2899 h->root.type = bfd_link_hash_undefweak;
2900 h->root.u.undef.abfd = h->root.u.undef.weak;
2901 if (h->root.u.undef.next != NULL
2902 || inf->htab->root.undefs_tail == &h->root)
2903 inf->twiddled = TRUE;
2908 case bfd_link_hash_undefweak:
2909 if (h->root.u.undef.abfd != inf->not_needed)
2913 case bfd_link_hash_defined:
2914 case bfd_link_hash_defweak:
2915 if (h->root.u.def.section->owner != inf->not_needed)
2919 case bfd_link_hash_common:
2920 if (h->root.u.c.p->section->owner != inf->not_needed)
2924 case bfd_link_hash_warning:
2925 case bfd_link_hash_indirect:
2926 elf_smash_syms ((struct elf_link_hash_entry *) h->root.u.i.link, data);
2927 if (h->root.u.i.link->type != bfd_link_hash_new)
2929 if (h->root.u.i.link->u.undef.abfd != inf->not_needed)
2934 /* There is no way we can undo symbol table state from defined or
2935 defweak back to undefined. */
2939 /* Set sym back to newly created state, but keep undef.next if it is
2940 being used as a list pointer. */
2941 bh = h->root.u.undef.next;
2944 if (bh != NULL || inf->htab->root.undefs_tail == &h->root)
2945 inf->twiddled = TRUE;
2946 (*inf->htab->root.table.newfunc) (&h->root.root,
2947 &inf->htab->root.table,
2948 h->root.root.string);
2949 h->root.u.undef.next = bh;
2950 h->root.u.undef.abfd = inf->not_needed;
2955 /* Sort symbol by value and section. */
2957 elf_sort_symbol (const void *arg1, const void *arg2)
2959 const struct elf_link_hash_entry *h1;
2960 const struct elf_link_hash_entry *h2;
2961 bfd_signed_vma vdiff;
2963 h1 = *(const struct elf_link_hash_entry **) arg1;
2964 h2 = *(const struct elf_link_hash_entry **) arg2;
2965 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2967 return vdiff > 0 ? 1 : -1;
2970 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2972 return sdiff > 0 ? 1 : -1;
2977 /* This function is used to adjust offsets into .dynstr for
2978 dynamic symbols. This is called via elf_link_hash_traverse. */
2981 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2983 struct elf_strtab_hash *dynstr = data;
2985 if (h->root.type == bfd_link_hash_warning)
2986 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2988 if (h->dynindx != -1)
2989 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2993 /* Assign string offsets in .dynstr, update all structures referencing
2997 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2999 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3000 struct elf_link_local_dynamic_entry *entry;
3001 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3002 bfd *dynobj = hash_table->dynobj;
3005 const struct elf_backend_data *bed;
3008 _bfd_elf_strtab_finalize (dynstr);
3009 size = _bfd_elf_strtab_size (dynstr);
3011 bed = get_elf_backend_data (dynobj);
3012 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3013 BFD_ASSERT (sdyn != NULL);
3015 /* Update all .dynamic entries referencing .dynstr strings. */
3016 for (extdyn = sdyn->contents;
3017 extdyn < sdyn->contents + sdyn->size;
3018 extdyn += bed->s->sizeof_dyn)
3020 Elf_Internal_Dyn dyn;
3022 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3026 dyn.d_un.d_val = size;
3034 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3039 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3042 /* Now update local dynamic symbols. */
3043 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3044 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3045 entry->isym.st_name);
3047 /* And the rest of dynamic symbols. */
3048 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3050 /* Adjust version definitions. */
3051 if (elf_tdata (output_bfd)->cverdefs)
3056 Elf_Internal_Verdef def;
3057 Elf_Internal_Verdaux defaux;
3059 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3063 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3065 p += sizeof (Elf_External_Verdef);
3066 if (def.vd_aux != sizeof (Elf_External_Verdef))
3068 for (i = 0; i < def.vd_cnt; ++i)
3070 _bfd_elf_swap_verdaux_in (output_bfd,
3071 (Elf_External_Verdaux *) p, &defaux);
3072 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3074 _bfd_elf_swap_verdaux_out (output_bfd,
3075 &defaux, (Elf_External_Verdaux *) p);
3076 p += sizeof (Elf_External_Verdaux);
3079 while (def.vd_next);
3082 /* Adjust version references. */
3083 if (elf_tdata (output_bfd)->verref)
3088 Elf_Internal_Verneed need;
3089 Elf_Internal_Vernaux needaux;
3091 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3095 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3097 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3098 _bfd_elf_swap_verneed_out (output_bfd, &need,
3099 (Elf_External_Verneed *) p);
3100 p += sizeof (Elf_External_Verneed);
3101 for (i = 0; i < need.vn_cnt; ++i)
3103 _bfd_elf_swap_vernaux_in (output_bfd,
3104 (Elf_External_Vernaux *) p, &needaux);
3105 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3107 _bfd_elf_swap_vernaux_out (output_bfd,
3109 (Elf_External_Vernaux *) p);
3110 p += sizeof (Elf_External_Vernaux);
3113 while (need.vn_next);
3119 /* Add symbols from an ELF object file to the linker hash table. */
3122 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3124 bfd_boolean (*add_symbol_hook)
3125 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
3126 const char **, flagword *, asection **, bfd_vma *);
3127 bfd_boolean (*check_relocs)
3128 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
3129 bfd_boolean (*check_directives)
3130 (bfd *, struct bfd_link_info *);
3131 bfd_boolean collect;
3132 Elf_Internal_Shdr *hdr;
3133 bfd_size_type symcount;
3134 bfd_size_type extsymcount;
3135 bfd_size_type extsymoff;
3136 struct elf_link_hash_entry **sym_hash;
3137 bfd_boolean dynamic;
3138 Elf_External_Versym *extversym = NULL;
3139 Elf_External_Versym *ever;
3140 struct elf_link_hash_entry *weaks;
3141 struct elf_link_hash_entry **nondeflt_vers = NULL;
3142 bfd_size_type nondeflt_vers_cnt = 0;
3143 Elf_Internal_Sym *isymbuf = NULL;
3144 Elf_Internal_Sym *isym;
3145 Elf_Internal_Sym *isymend;
3146 const struct elf_backend_data *bed;
3147 bfd_boolean add_needed;
3148 struct elf_link_hash_table * hash_table;
3151 hash_table = elf_hash_table (info);
3153 bed = get_elf_backend_data (abfd);
3154 add_symbol_hook = bed->elf_add_symbol_hook;
3155 collect = bed->collect;
3157 if ((abfd->flags & DYNAMIC) == 0)
3163 /* You can't use -r against a dynamic object. Also, there's no
3164 hope of using a dynamic object which does not exactly match
3165 the format of the output file. */
3166 if (info->relocatable
3167 || !is_elf_hash_table (hash_table)
3168 || hash_table->root.creator != abfd->xvec)
3170 if (info->relocatable)
3171 bfd_set_error (bfd_error_invalid_operation);
3173 bfd_set_error (bfd_error_wrong_format);
3178 /* As a GNU extension, any input sections which are named
3179 .gnu.warning.SYMBOL are treated as warning symbols for the given
3180 symbol. This differs from .gnu.warning sections, which generate
3181 warnings when they are included in an output file. */
3182 if (info->executable)
3186 for (s = abfd->sections; s != NULL; s = s->next)
3190 name = bfd_get_section_name (abfd, s);
3191 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3196 name += sizeof ".gnu.warning." - 1;
3198 /* If this is a shared object, then look up the symbol
3199 in the hash table. If it is there, and it is already
3200 been defined, then we will not be using the entry
3201 from this shared object, so we don't need to warn.
3202 FIXME: If we see the definition in a regular object
3203 later on, we will warn, but we shouldn't. The only
3204 fix is to keep track of what warnings we are supposed
3205 to emit, and then handle them all at the end of the
3209 struct elf_link_hash_entry *h;
3211 h = elf_link_hash_lookup (hash_table, name,
3212 FALSE, FALSE, TRUE);
3214 /* FIXME: What about bfd_link_hash_common? */
3216 && (h->root.type == bfd_link_hash_defined
3217 || h->root.type == bfd_link_hash_defweak))
3219 /* We don't want to issue this warning. Clobber
3220 the section size so that the warning does not
3221 get copied into the output file. */
3228 msg = bfd_alloc (abfd, sz + 1);
3232 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3237 if (! (_bfd_generic_link_add_one_symbol
3238 (info, abfd, name, BSF_WARNING, s, 0, msg,
3239 FALSE, collect, NULL)))
3242 if (! info->relocatable)
3244 /* Clobber the section size so that the warning does
3245 not get copied into the output file. */
3248 /* Also set SEC_EXCLUDE, so that symbols defined in
3249 the warning section don't get copied to the output. */
3250 s->flags |= SEC_EXCLUDE;
3259 /* If we are creating a shared library, create all the dynamic
3260 sections immediately. We need to attach them to something,
3261 so we attach them to this BFD, provided it is the right
3262 format. FIXME: If there are no input BFD's of the same
3263 format as the output, we can't make a shared library. */
3265 && is_elf_hash_table (hash_table)
3266 && hash_table->root.creator == abfd->xvec
3267 && ! hash_table->dynamic_sections_created)
3269 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3273 else if (!is_elf_hash_table (hash_table))
3278 const char *soname = NULL;
3279 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3282 /* ld --just-symbols and dynamic objects don't mix very well.
3283 Test for --just-symbols by looking at info set up by
3284 _bfd_elf_link_just_syms. */
3285 if ((s = abfd->sections) != NULL
3286 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3289 /* If this dynamic lib was specified on the command line with
3290 --as-needed in effect, then we don't want to add a DT_NEEDED
3291 tag unless the lib is actually used. Similary for libs brought
3292 in by another lib's DT_NEEDED. When --no-add-needed is used
3293 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3294 any dynamic library in DT_NEEDED tags in the dynamic lib at
3296 add_needed = (elf_dyn_lib_class (abfd)
3297 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3298 | DYN_NO_NEEDED)) == 0;
3300 s = bfd_get_section_by_name (abfd, ".dynamic");
3306 unsigned long shlink;
3308 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3309 goto error_free_dyn;
3311 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3313 goto error_free_dyn;
3314 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3316 for (extdyn = dynbuf;
3317 extdyn < dynbuf + s->size;
3318 extdyn += bed->s->sizeof_dyn)
3320 Elf_Internal_Dyn dyn;
3322 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3323 if (dyn.d_tag == DT_SONAME)
3325 unsigned int tagv = dyn.d_un.d_val;
3326 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3328 goto error_free_dyn;
3330 if (dyn.d_tag == DT_NEEDED)
3332 struct bfd_link_needed_list *n, **pn;
3334 unsigned int tagv = dyn.d_un.d_val;
3336 amt = sizeof (struct bfd_link_needed_list);
3337 n = bfd_alloc (abfd, amt);
3338 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3339 if (n == NULL || fnm == NULL)
3340 goto error_free_dyn;
3341 amt = strlen (fnm) + 1;
3342 anm = bfd_alloc (abfd, amt);
3344 goto error_free_dyn;
3345 memcpy (anm, fnm, amt);
3349 for (pn = & hash_table->needed;
3355 if (dyn.d_tag == DT_RUNPATH)
3357 struct bfd_link_needed_list *n, **pn;
3359 unsigned int tagv = dyn.d_un.d_val;
3361 amt = sizeof (struct bfd_link_needed_list);
3362 n = bfd_alloc (abfd, amt);
3363 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3364 if (n == NULL || fnm == NULL)
3365 goto error_free_dyn;
3366 amt = strlen (fnm) + 1;
3367 anm = bfd_alloc (abfd, amt);
3369 goto error_free_dyn;
3370 memcpy (anm, fnm, amt);
3374 for (pn = & runpath;
3380 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3381 if (!runpath && dyn.d_tag == DT_RPATH)
3383 struct bfd_link_needed_list *n, **pn;
3385 unsigned int tagv = dyn.d_un.d_val;
3387 amt = sizeof (struct bfd_link_needed_list);
3388 n = bfd_alloc (abfd, amt);
3389 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3390 if (n == NULL || fnm == NULL)
3391 goto error_free_dyn;
3392 amt = strlen (fnm) + 1;
3393 anm = bfd_alloc (abfd, amt);
3400 memcpy (anm, fnm, amt);
3415 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3416 frees all more recently bfd_alloc'd blocks as well. */
3422 struct bfd_link_needed_list **pn;
3423 for (pn = & hash_table->runpath;
3430 /* We do not want to include any of the sections in a dynamic
3431 object in the output file. We hack by simply clobbering the
3432 list of sections in the BFD. This could be handled more
3433 cleanly by, say, a new section flag; the existing
3434 SEC_NEVER_LOAD flag is not the one we want, because that one
3435 still implies that the section takes up space in the output
3437 bfd_section_list_clear (abfd);
3439 /* Find the name to use in a DT_NEEDED entry that refers to this
3440 object. If the object has a DT_SONAME entry, we use it.
3441 Otherwise, if the generic linker stuck something in
3442 elf_dt_name, we use that. Otherwise, we just use the file
3444 if (soname == NULL || *soname == '\0')
3446 soname = elf_dt_name (abfd);
3447 if (soname == NULL || *soname == '\0')
3448 soname = bfd_get_filename (abfd);
3451 /* Save the SONAME because sometimes the linker emulation code
3452 will need to know it. */
3453 elf_dt_name (abfd) = soname;
3455 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3459 /* If we have already included this dynamic object in the
3460 link, just ignore it. There is no reason to include a
3461 particular dynamic object more than once. */
3466 /* If this is a dynamic object, we always link against the .dynsym
3467 symbol table, not the .symtab symbol table. The dynamic linker
3468 will only see the .dynsym symbol table, so there is no reason to
3469 look at .symtab for a dynamic object. */
3471 if (! dynamic || elf_dynsymtab (abfd) == 0)
3472 hdr = &elf_tdata (abfd)->symtab_hdr;
3474 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3476 symcount = hdr->sh_size / bed->s->sizeof_sym;
3478 /* The sh_info field of the symtab header tells us where the
3479 external symbols start. We don't care about the local symbols at
3481 if (elf_bad_symtab (abfd))
3483 extsymcount = symcount;
3488 extsymcount = symcount - hdr->sh_info;
3489 extsymoff = hdr->sh_info;
3493 if (extsymcount != 0)
3495 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3497 if (isymbuf == NULL)
3500 /* We store a pointer to the hash table entry for each external
3502 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3503 sym_hash = bfd_alloc (abfd, amt);
3504 if (sym_hash == NULL)
3505 goto error_free_sym;
3506 elf_sym_hashes (abfd) = sym_hash;
3511 /* Read in any version definitions. */
3512 if (!_bfd_elf_slurp_version_tables (abfd,
3513 info->default_imported_symver))
3514 goto error_free_sym;
3516 /* Read in the symbol versions, but don't bother to convert them
3517 to internal format. */
3518 if (elf_dynversym (abfd) != 0)
3520 Elf_Internal_Shdr *versymhdr;
3522 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3523 extversym = bfd_malloc (versymhdr->sh_size);
3524 if (extversym == NULL)
3525 goto error_free_sym;
3526 amt = versymhdr->sh_size;
3527 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3528 || bfd_bread (extversym, amt, abfd) != amt)
3529 goto error_free_vers;
3535 ever = extversym != NULL ? extversym + extsymoff : NULL;
3536 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3538 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3542 asection *sec, *new_sec;
3545 struct elf_link_hash_entry *h;
3546 bfd_boolean definition;
3547 bfd_boolean size_change_ok;
3548 bfd_boolean type_change_ok;
3549 bfd_boolean new_weakdef;
3550 bfd_boolean override;
3551 unsigned int old_alignment;
3556 flags = BSF_NO_FLAGS;
3558 value = isym->st_value;
3561 bind = ELF_ST_BIND (isym->st_info);
3562 if (bind == STB_LOCAL)
3564 /* This should be impossible, since ELF requires that all
3565 global symbols follow all local symbols, and that sh_info
3566 point to the first global symbol. Unfortunately, Irix 5
3570 else if (bind == STB_GLOBAL)
3572 if (isym->st_shndx != SHN_UNDEF
3573 && isym->st_shndx != SHN_COMMON)
3576 else if (bind == STB_WEAK)
3580 /* Leave it up to the processor backend. */
3583 if (isym->st_shndx == SHN_UNDEF)
3584 sec = bfd_und_section_ptr;
3585 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3587 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3589 sec = bfd_abs_section_ptr;
3590 else if (sec->kept_section)
3592 /* Symbols from discarded section are undefined, and have
3593 default visibility. */
3594 sec = bfd_und_section_ptr;
3595 isym->st_shndx = SHN_UNDEF;
3596 isym->st_other = STV_DEFAULT
3597 | (isym->st_other & ~ ELF_ST_VISIBILITY(-1));
3599 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3602 else if (isym->st_shndx == SHN_ABS)
3603 sec = bfd_abs_section_ptr;
3604 else if (isym->st_shndx == SHN_COMMON)
3606 sec = bfd_com_section_ptr;
3607 /* What ELF calls the size we call the value. What ELF
3608 calls the value we call the alignment. */
3609 value = isym->st_size;
3613 /* Leave it up to the processor backend. */
3616 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3619 goto error_free_vers;
3621 if (isym->st_shndx == SHN_COMMON
3622 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3624 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3628 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3631 | SEC_LINKER_CREATED
3632 | SEC_THREAD_LOCAL));
3634 goto error_free_vers;
3638 else if (add_symbol_hook)
3640 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3642 goto error_free_vers;
3644 /* The hook function sets the name to NULL if this symbol
3645 should be skipped for some reason. */
3650 /* Sanity check that all possibilities were handled. */
3653 bfd_set_error (bfd_error_bad_value);
3654 goto error_free_vers;
3657 if (bfd_is_und_section (sec)
3658 || bfd_is_com_section (sec))
3663 size_change_ok = FALSE;
3664 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3669 if (is_elf_hash_table (hash_table))
3671 Elf_Internal_Versym iver;
3672 unsigned int vernum = 0;
3677 if (info->default_imported_symver)
3678 /* Use the default symbol version created earlier. */
3679 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3684 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3686 vernum = iver.vs_vers & VERSYM_VERSION;
3688 /* If this is a hidden symbol, or if it is not version
3689 1, we append the version name to the symbol name.
3690 However, we do not modify a non-hidden absolute symbol
3691 if it is not a function, because it might be the version
3692 symbol itself. FIXME: What if it isn't? */
3693 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3694 || (vernum > 1 && (! bfd_is_abs_section (sec)
3695 || ELF_ST_TYPE (isym->st_info) == STT_FUNC)))
3698 size_t namelen, verlen, newlen;
3701 if (isym->st_shndx != SHN_UNDEF)
3703 if (vernum > elf_tdata (abfd)->cverdefs)
3705 else if (vernum > 1)
3707 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3713 (*_bfd_error_handler)
3714 (_("%B: %s: invalid version %u (max %d)"),
3716 elf_tdata (abfd)->cverdefs);
3717 bfd_set_error (bfd_error_bad_value);
3718 goto error_free_vers;
3723 /* We cannot simply test for the number of
3724 entries in the VERNEED section since the
3725 numbers for the needed versions do not start
3727 Elf_Internal_Verneed *t;
3730 for (t = elf_tdata (abfd)->verref;
3734 Elf_Internal_Vernaux *a;
3736 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3738 if (a->vna_other == vernum)
3740 verstr = a->vna_nodename;
3749 (*_bfd_error_handler)
3750 (_("%B: %s: invalid needed version %d"),
3751 abfd, name, vernum);
3752 bfd_set_error (bfd_error_bad_value);
3753 goto error_free_vers;
3757 namelen = strlen (name);
3758 verlen = strlen (verstr);
3759 newlen = namelen + verlen + 2;
3760 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3761 && isym->st_shndx != SHN_UNDEF)
3764 newname = bfd_alloc (abfd, newlen);
3765 if (newname == NULL)
3766 goto error_free_vers;
3767 memcpy (newname, name, namelen);
3768 p = newname + namelen;
3770 /* If this is a defined non-hidden version symbol,
3771 we add another @ to the name. This indicates the
3772 default version of the symbol. */
3773 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3774 && isym->st_shndx != SHN_UNDEF)
3776 memcpy (p, verstr, verlen + 1);
3781 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3782 &value, &old_alignment,
3783 sym_hash, &skip, &override,
3784 &type_change_ok, &size_change_ok))
3785 goto error_free_vers;
3794 while (h->root.type == bfd_link_hash_indirect
3795 || h->root.type == bfd_link_hash_warning)
3796 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3798 /* Remember the old alignment if this is a common symbol, so
3799 that we don't reduce the alignment later on. We can't
3800 check later, because _bfd_generic_link_add_one_symbol
3801 will set a default for the alignment which we want to
3802 override. We also remember the old bfd where the existing
3803 definition comes from. */
3804 switch (h->root.type)
3809 case bfd_link_hash_defined:
3810 case bfd_link_hash_defweak:
3811 old_bfd = h->root.u.def.section->owner;
3814 case bfd_link_hash_common:
3815 old_bfd = h->root.u.c.p->section->owner;
3816 old_alignment = h->root.u.c.p->alignment_power;
3820 if (elf_tdata (abfd)->verdef != NULL
3824 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3827 if (! (_bfd_generic_link_add_one_symbol
3828 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3829 (struct bfd_link_hash_entry **) sym_hash)))
3830 goto error_free_vers;
3833 while (h->root.type == bfd_link_hash_indirect
3834 || h->root.type == bfd_link_hash_warning)
3835 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3838 new_weakdef = FALSE;
3841 && (flags & BSF_WEAK) != 0
3842 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3843 && is_elf_hash_table (hash_table)
3844 && h->u.weakdef == NULL)
3846 /* Keep a list of all weak defined non function symbols from
3847 a dynamic object, using the weakdef field. Later in this
3848 function we will set the weakdef field to the correct
3849 value. We only put non-function symbols from dynamic
3850 objects on this list, because that happens to be the only
3851 time we need to know the normal symbol corresponding to a
3852 weak symbol, and the information is time consuming to
3853 figure out. If the weakdef field is not already NULL,
3854 then this symbol was already defined by some previous
3855 dynamic object, and we will be using that previous
3856 definition anyhow. */
3858 h->u.weakdef = weaks;
3863 /* Set the alignment of a common symbol. */
3864 if ((isym->st_shndx == SHN_COMMON
3865 || bfd_is_com_section (sec))
3866 && h->root.type == bfd_link_hash_common)
3870 if (isym->st_shndx == SHN_COMMON)
3871 align = bfd_log2 (isym->st_value);
3874 /* The new symbol is a common symbol in a shared object.
3875 We need to get the alignment from the section. */
3876 align = new_sec->alignment_power;
3878 if (align > old_alignment
3879 /* Permit an alignment power of zero if an alignment of one
3880 is specified and no other alignments have been specified. */
3881 || (isym->st_value == 1 && old_alignment == 0))
3882 h->root.u.c.p->alignment_power = align;
3884 h->root.u.c.p->alignment_power = old_alignment;
3887 if (is_elf_hash_table (hash_table))
3891 /* Check the alignment when a common symbol is involved. This
3892 can change when a common symbol is overridden by a normal
3893 definition or a common symbol is ignored due to the old
3894 normal definition. We need to make sure the maximum
3895 alignment is maintained. */
3896 if ((old_alignment || isym->st_shndx == SHN_COMMON)
3897 && h->root.type != bfd_link_hash_common)
3899 unsigned int common_align;
3900 unsigned int normal_align;
3901 unsigned int symbol_align;
3905 symbol_align = ffs (h->root.u.def.value) - 1;
3906 if (h->root.u.def.section->owner != NULL
3907 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3909 normal_align = h->root.u.def.section->alignment_power;
3910 if (normal_align > symbol_align)
3911 normal_align = symbol_align;
3914 normal_align = symbol_align;
3918 common_align = old_alignment;
3919 common_bfd = old_bfd;
3924 common_align = bfd_log2 (isym->st_value);
3926 normal_bfd = old_bfd;
3929 if (normal_align < common_align)
3930 (*_bfd_error_handler)
3931 (_("Warning: alignment %u of symbol `%s' in %B"
3932 " is smaller than %u in %B"),
3933 normal_bfd, common_bfd,
3934 1 << normal_align, name, 1 << common_align);
3937 /* Remember the symbol size and type. */
3938 if (isym->st_size != 0
3939 && (definition || h->size == 0))
3941 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3942 (*_bfd_error_handler)
3943 (_("Warning: size of symbol `%s' changed"
3944 " from %lu in %B to %lu in %B"),
3946 name, (unsigned long) h->size,
3947 (unsigned long) isym->st_size);
3949 h->size = isym->st_size;
3952 /* If this is a common symbol, then we always want H->SIZE
3953 to be the size of the common symbol. The code just above
3954 won't fix the size if a common symbol becomes larger. We
3955 don't warn about a size change here, because that is
3956 covered by --warn-common. */
3957 if (h->root.type == bfd_link_hash_common)
3958 h->size = h->root.u.c.size;
3960 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3961 && (definition || h->type == STT_NOTYPE))
3963 if (h->type != STT_NOTYPE
3964 && h->type != ELF_ST_TYPE (isym->st_info)
3965 && ! type_change_ok)
3966 (*_bfd_error_handler)
3967 (_("Warning: type of symbol `%s' changed"
3968 " from %d to %d in %B"),
3969 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3971 h->type = ELF_ST_TYPE (isym->st_info);
3974 /* If st_other has a processor-specific meaning, specific
3975 code might be needed here. We never merge the visibility
3976 attribute with the one from a dynamic object. */
3977 if (bed->elf_backend_merge_symbol_attribute)
3978 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3981 /* If this symbol has default visibility and the user has requested
3982 we not re-export it, then mark it as hidden. */
3983 if (definition && !dynamic
3985 || (abfd->my_archive && abfd->my_archive->no_export))
3986 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
3987 isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1));
3989 if (isym->st_other != 0 && !dynamic)
3991 unsigned char hvis, symvis, other, nvis;
3993 /* Take the balance of OTHER from the definition. */
3994 other = (definition ? isym->st_other : h->other);
3995 other &= ~ ELF_ST_VISIBILITY (-1);
3997 /* Combine visibilities, using the most constraining one. */
3998 hvis = ELF_ST_VISIBILITY (h->other);
3999 symvis = ELF_ST_VISIBILITY (isym->st_other);
4005 nvis = hvis < symvis ? hvis : symvis;
4007 h->other = other | nvis;
4010 /* Set a flag in the hash table entry indicating the type of
4011 reference or definition we just found. Keep a count of
4012 the number of dynamic symbols we find. A dynamic symbol
4013 is one which is referenced or defined by both a regular
4014 object and a shared object. */
4021 if (bind != STB_WEAK)
4022 h->ref_regular_nonweak = 1;
4026 if (! info->executable
4039 || (h->u.weakdef != NULL
4041 && h->u.weakdef->dynindx != -1))
4045 /* Check to see if we need to add an indirect symbol for
4046 the default name. */
4047 if (definition || h->root.type == bfd_link_hash_common)
4048 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4049 &sec, &value, &dynsym,
4051 goto error_free_vers;
4053 if (definition && !dynamic)
4055 char *p = strchr (name, ELF_VER_CHR);
4056 if (p != NULL && p[1] != ELF_VER_CHR)
4058 /* Queue non-default versions so that .symver x, x@FOO
4059 aliases can be checked. */
4060 if (! nondeflt_vers)
4062 amt = (isymend - isym + 1)
4063 * sizeof (struct elf_link_hash_entry *);
4064 nondeflt_vers = bfd_malloc (amt);
4066 nondeflt_vers [nondeflt_vers_cnt++] = h;
4070 if (dynsym && h->dynindx == -1)
4072 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4073 goto error_free_vers;
4074 if (h->u.weakdef != NULL
4076 && h->u.weakdef->dynindx == -1)
4078 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4079 goto error_free_vers;
4082 else if (dynsym && h->dynindx != -1)
4083 /* If the symbol already has a dynamic index, but
4084 visibility says it should not be visible, turn it into
4086 switch (ELF_ST_VISIBILITY (h->other))
4090 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4101 const char *soname = elf_dt_name (abfd);
4103 /* A symbol from a library loaded via DT_NEEDED of some
4104 other library is referenced by a regular object.
4105 Add a DT_NEEDED entry for it. Issue an error if
4106 --no-add-needed is used. */
4107 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4109 (*_bfd_error_handler)
4110 (_("%s: invalid DSO for symbol `%s' definition"),
4112 bfd_set_error (bfd_error_bad_value);
4113 goto error_free_vers;
4116 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4119 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4121 goto error_free_vers;
4123 BFD_ASSERT (ret == 0);
4128 /* Now that all the symbols from this input file are created, handle
4129 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4130 if (nondeflt_vers != NULL)
4132 bfd_size_type cnt, symidx;
4134 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4136 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4137 char *shortname, *p;
4139 p = strchr (h->root.root.string, ELF_VER_CHR);
4141 || (h->root.type != bfd_link_hash_defined
4142 && h->root.type != bfd_link_hash_defweak))
4145 amt = p - h->root.root.string;
4146 shortname = bfd_malloc (amt + 1);
4147 memcpy (shortname, h->root.root.string, amt);
4148 shortname[amt] = '\0';
4150 hi = (struct elf_link_hash_entry *)
4151 bfd_link_hash_lookup (&hash_table->root, shortname,
4152 FALSE, FALSE, FALSE);
4154 && hi->root.type == h->root.type
4155 && hi->root.u.def.value == h->root.u.def.value
4156 && hi->root.u.def.section == h->root.u.def.section)
4158 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4159 hi->root.type = bfd_link_hash_indirect;
4160 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4161 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
4162 sym_hash = elf_sym_hashes (abfd);
4164 for (symidx = 0; symidx < extsymcount; ++symidx)
4165 if (sym_hash[symidx] == hi)
4167 sym_hash[symidx] = h;
4173 free (nondeflt_vers);
4174 nondeflt_vers = NULL;
4177 if (extversym != NULL)
4183 if (isymbuf != NULL)
4188 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4190 /* Remove symbols defined in an as-needed shared lib that wasn't
4192 struct elf_smash_syms_data inf;
4193 inf.not_needed = abfd;
4194 inf.htab = hash_table;
4195 inf.twiddled = FALSE;
4196 elf_link_hash_traverse (hash_table, elf_smash_syms, &inf);
4198 bfd_link_repair_undef_list (&hash_table->root);
4202 /* Now set the weakdefs field correctly for all the weak defined
4203 symbols we found. The only way to do this is to search all the
4204 symbols. Since we only need the information for non functions in
4205 dynamic objects, that's the only time we actually put anything on
4206 the list WEAKS. We need this information so that if a regular
4207 object refers to a symbol defined weakly in a dynamic object, the
4208 real symbol in the dynamic object is also put in the dynamic
4209 symbols; we also must arrange for both symbols to point to the
4210 same memory location. We could handle the general case of symbol
4211 aliasing, but a general symbol alias can only be generated in
4212 assembler code, handling it correctly would be very time
4213 consuming, and other ELF linkers don't handle general aliasing
4217 struct elf_link_hash_entry **hpp;
4218 struct elf_link_hash_entry **hppend;
4219 struct elf_link_hash_entry **sorted_sym_hash;
4220 struct elf_link_hash_entry *h;
4223 /* Since we have to search the whole symbol list for each weak
4224 defined symbol, search time for N weak defined symbols will be
4225 O(N^2). Binary search will cut it down to O(NlogN). */
4226 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4227 sorted_sym_hash = bfd_malloc (amt);
4228 if (sorted_sym_hash == NULL)
4230 sym_hash = sorted_sym_hash;
4231 hpp = elf_sym_hashes (abfd);
4232 hppend = hpp + extsymcount;
4234 for (; hpp < hppend; hpp++)
4238 && h->root.type == bfd_link_hash_defined
4239 && h->type != STT_FUNC)
4247 qsort (sorted_sym_hash, sym_count,
4248 sizeof (struct elf_link_hash_entry *),
4251 while (weaks != NULL)
4253 struct elf_link_hash_entry *hlook;
4260 weaks = hlook->u.weakdef;
4261 hlook->u.weakdef = NULL;
4263 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4264 || hlook->root.type == bfd_link_hash_defweak
4265 || hlook->root.type == bfd_link_hash_common
4266 || hlook->root.type == bfd_link_hash_indirect);
4267 slook = hlook->root.u.def.section;
4268 vlook = hlook->root.u.def.value;
4275 bfd_signed_vma vdiff;
4277 h = sorted_sym_hash [idx];
4278 vdiff = vlook - h->root.u.def.value;
4285 long sdiff = slook->id - h->root.u.def.section->id;
4298 /* We didn't find a value/section match. */
4302 for (i = ilook; i < sym_count; i++)
4304 h = sorted_sym_hash [i];
4306 /* Stop if value or section doesn't match. */
4307 if (h->root.u.def.value != vlook
4308 || h->root.u.def.section != slook)
4310 else if (h != hlook)
4312 hlook->u.weakdef = h;
4314 /* If the weak definition is in the list of dynamic
4315 symbols, make sure the real definition is put
4317 if (hlook->dynindx != -1 && h->dynindx == -1)
4319 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4323 /* If the real definition is in the list of dynamic
4324 symbols, make sure the weak definition is put
4325 there as well. If we don't do this, then the
4326 dynamic loader might not merge the entries for the
4327 real definition and the weak definition. */
4328 if (h->dynindx != -1 && hlook->dynindx == -1)
4330 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4338 free (sorted_sym_hash);
4341 check_directives = get_elf_backend_data (abfd)->check_directives;
4342 if (check_directives)
4343 check_directives (abfd, info);
4345 /* If this object is the same format as the output object, and it is
4346 not a shared library, then let the backend look through the
4349 This is required to build global offset table entries and to
4350 arrange for dynamic relocs. It is not required for the
4351 particular common case of linking non PIC code, even when linking
4352 against shared libraries, but unfortunately there is no way of
4353 knowing whether an object file has been compiled PIC or not.
4354 Looking through the relocs is not particularly time consuming.
4355 The problem is that we must either (1) keep the relocs in memory,
4356 which causes the linker to require additional runtime memory or
4357 (2) read the relocs twice from the input file, which wastes time.
4358 This would be a good case for using mmap.
4360 I have no idea how to handle linking PIC code into a file of a
4361 different format. It probably can't be done. */
4362 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4364 && is_elf_hash_table (hash_table)
4365 && hash_table->root.creator == abfd->xvec
4366 && check_relocs != NULL)
4370 for (o = abfd->sections; o != NULL; o = o->next)
4372 Elf_Internal_Rela *internal_relocs;
4375 if ((o->flags & SEC_RELOC) == 0
4376 || o->reloc_count == 0
4377 || ((info->strip == strip_all || info->strip == strip_debugger)
4378 && (o->flags & SEC_DEBUGGING) != 0)
4379 || bfd_is_abs_section (o->output_section))
4382 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4384 if (internal_relocs == NULL)
4387 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4389 if (elf_section_data (o)->relocs != internal_relocs)
4390 free (internal_relocs);
4397 /* If this is a non-traditional link, try to optimize the handling
4398 of the .stab/.stabstr sections. */
4400 && ! info->traditional_format
4401 && is_elf_hash_table (hash_table)
4402 && (info->strip != strip_all && info->strip != strip_debugger))
4406 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4407 if (stabstr != NULL)
4409 bfd_size_type string_offset = 0;
4412 for (stab = abfd->sections; stab; stab = stab->next)
4413 if (strncmp (".stab", stab->name, 5) == 0
4414 && (!stab->name[5] ||
4415 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4416 && (stab->flags & SEC_MERGE) == 0
4417 && !bfd_is_abs_section (stab->output_section))
4419 struct bfd_elf_section_data *secdata;
4421 secdata = elf_section_data (stab);
4422 if (! _bfd_link_section_stabs (abfd,
4423 &hash_table->stab_info,
4428 if (secdata->sec_info)
4429 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4434 if (is_elf_hash_table (hash_table) && add_needed)
4436 /* Add this bfd to the loaded list. */
4437 struct elf_link_loaded_list *n;
4439 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4443 n->next = hash_table->loaded;
4444 hash_table->loaded = n;
4450 if (nondeflt_vers != NULL)
4451 free (nondeflt_vers);
4452 if (extversym != NULL)
4455 if (isymbuf != NULL)
4461 /* Return the linker hash table entry of a symbol that might be
4462 satisfied by an archive symbol. Return -1 on error. */
4464 struct elf_link_hash_entry *
4465 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4466 struct bfd_link_info *info,
4469 struct elf_link_hash_entry *h;
4473 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4477 /* If this is a default version (the name contains @@), look up the
4478 symbol again with only one `@' as well as without the version.
4479 The effect is that references to the symbol with and without the
4480 version will be matched by the default symbol in the archive. */
4482 p = strchr (name, ELF_VER_CHR);
4483 if (p == NULL || p[1] != ELF_VER_CHR)
4486 /* First check with only one `@'. */
4487 len = strlen (name);
4488 copy = bfd_alloc (abfd, len);
4490 return (struct elf_link_hash_entry *) 0 - 1;
4492 first = p - name + 1;
4493 memcpy (copy, name, first);
4494 memcpy (copy + first, name + first + 1, len - first);
4496 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4499 /* We also need to check references to the symbol without the
4501 copy[first - 1] = '\0';
4502 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4503 FALSE, FALSE, FALSE);
4506 bfd_release (abfd, copy);
4510 /* Add symbols from an ELF archive file to the linker hash table. We
4511 don't use _bfd_generic_link_add_archive_symbols because of a
4512 problem which arises on UnixWare. The UnixWare libc.so is an
4513 archive which includes an entry libc.so.1 which defines a bunch of
4514 symbols. The libc.so archive also includes a number of other
4515 object files, which also define symbols, some of which are the same
4516 as those defined in libc.so.1. Correct linking requires that we
4517 consider each object file in turn, and include it if it defines any
4518 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4519 this; it looks through the list of undefined symbols, and includes
4520 any object file which defines them. When this algorithm is used on
4521 UnixWare, it winds up pulling in libc.so.1 early and defining a
4522 bunch of symbols. This means that some of the other objects in the
4523 archive are not included in the link, which is incorrect since they
4524 precede libc.so.1 in the archive.
4526 Fortunately, ELF archive handling is simpler than that done by
4527 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4528 oddities. In ELF, if we find a symbol in the archive map, and the
4529 symbol is currently undefined, we know that we must pull in that
4532 Unfortunately, we do have to make multiple passes over the symbol
4533 table until nothing further is resolved. */
4536 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4539 bfd_boolean *defined = NULL;
4540 bfd_boolean *included = NULL;
4544 const struct elf_backend_data *bed;
4545 struct elf_link_hash_entry * (*archive_symbol_lookup)
4546 (bfd *, struct bfd_link_info *, const char *);
4548 if (! bfd_has_map (abfd))
4550 /* An empty archive is a special case. */
4551 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4553 bfd_set_error (bfd_error_no_armap);
4557 /* Keep track of all symbols we know to be already defined, and all
4558 files we know to be already included. This is to speed up the
4559 second and subsequent passes. */
4560 c = bfd_ardata (abfd)->symdef_count;
4564 amt *= sizeof (bfd_boolean);
4565 defined = bfd_zmalloc (amt);
4566 included = bfd_zmalloc (amt);
4567 if (defined == NULL || included == NULL)
4570 symdefs = bfd_ardata (abfd)->symdefs;
4571 bed = get_elf_backend_data (abfd);
4572 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4585 symdefend = symdef + c;
4586 for (i = 0; symdef < symdefend; symdef++, i++)
4588 struct elf_link_hash_entry *h;
4590 struct bfd_link_hash_entry *undefs_tail;
4593 if (defined[i] || included[i])
4595 if (symdef->file_offset == last)
4601 h = archive_symbol_lookup (abfd, info, symdef->name);
4602 if (h == (struct elf_link_hash_entry *) 0 - 1)
4608 if (h->root.type == bfd_link_hash_common)
4610 /* We currently have a common symbol. The archive map contains
4611 a reference to this symbol, so we may want to include it. We
4612 only want to include it however, if this archive element
4613 contains a definition of the symbol, not just another common
4616 Unfortunately some archivers (including GNU ar) will put
4617 declarations of common symbols into their archive maps, as
4618 well as real definitions, so we cannot just go by the archive
4619 map alone. Instead we must read in the element's symbol
4620 table and check that to see what kind of symbol definition
4622 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4625 else if (h->root.type != bfd_link_hash_undefined)
4627 if (h->root.type != bfd_link_hash_undefweak)
4632 /* We need to include this archive member. */
4633 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4634 if (element == NULL)
4637 if (! bfd_check_format (element, bfd_object))
4640 /* Doublecheck that we have not included this object
4641 already--it should be impossible, but there may be
4642 something wrong with the archive. */
4643 if (element->archive_pass != 0)
4645 bfd_set_error (bfd_error_bad_value);
4648 element->archive_pass = 1;
4650 undefs_tail = info->hash->undefs_tail;
4652 if (! (*info->callbacks->add_archive_element) (info, element,
4655 if (! bfd_link_add_symbols (element, info))
4658 /* If there are any new undefined symbols, we need to make
4659 another pass through the archive in order to see whether
4660 they can be defined. FIXME: This isn't perfect, because
4661 common symbols wind up on undefs_tail and because an
4662 undefined symbol which is defined later on in this pass
4663 does not require another pass. This isn't a bug, but it
4664 does make the code less efficient than it could be. */
4665 if (undefs_tail != info->hash->undefs_tail)
4668 /* Look backward to mark all symbols from this object file
4669 which we have already seen in this pass. */
4673 included[mark] = TRUE;
4678 while (symdefs[mark].file_offset == symdef->file_offset);
4680 /* We mark subsequent symbols from this object file as we go
4681 on through the loop. */
4682 last = symdef->file_offset;
4693 if (defined != NULL)
4695 if (included != NULL)
4700 /* Given an ELF BFD, add symbols to the global hash table as
4704 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4706 switch (bfd_get_format (abfd))
4709 return elf_link_add_object_symbols (abfd, info);
4711 return elf_link_add_archive_symbols (abfd, info);
4713 bfd_set_error (bfd_error_wrong_format);
4718 /* This function will be called though elf_link_hash_traverse to store
4719 all hash value of the exported symbols in an array. */
4722 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4724 unsigned long **valuep = data;
4730 if (h->root.type == bfd_link_hash_warning)
4731 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4733 /* Ignore indirect symbols. These are added by the versioning code. */
4734 if (h->dynindx == -1)
4737 name = h->root.root.string;
4738 p = strchr (name, ELF_VER_CHR);
4741 alc = bfd_malloc (p - name + 1);
4742 memcpy (alc, name, p - name);
4743 alc[p - name] = '\0';
4747 /* Compute the hash value. */
4748 ha = bfd_elf_hash (name);
4750 /* Store the found hash value in the array given as the argument. */
4753 /* And store it in the struct so that we can put it in the hash table
4755 h->u.elf_hash_value = ha;
4763 /* Array used to determine the number of hash table buckets to use
4764 based on the number of symbols there are. If there are fewer than
4765 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4766 fewer than 37 we use 17 buckets, and so forth. We never use more
4767 than 32771 buckets. */
4769 static const size_t elf_buckets[] =
4771 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4775 /* Compute bucket count for hashing table. We do not use a static set
4776 of possible tables sizes anymore. Instead we determine for all
4777 possible reasonable sizes of the table the outcome (i.e., the
4778 number of collisions etc) and choose the best solution. The
4779 weighting functions are not too simple to allow the table to grow
4780 without bounds. Instead one of the weighting factors is the size.
4781 Therefore the result is always a good payoff between few collisions
4782 (= short chain lengths) and table size. */
4784 compute_bucket_count (struct bfd_link_info *info)
4786 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4787 size_t best_size = 0;
4788 unsigned long int *hashcodes;
4789 unsigned long int *hashcodesp;
4790 unsigned long int i;
4793 /* Compute the hash values for all exported symbols. At the same
4794 time store the values in an array so that we could use them for
4797 amt *= sizeof (unsigned long int);
4798 hashcodes = bfd_malloc (amt);
4799 if (hashcodes == NULL)
4801 hashcodesp = hashcodes;
4803 /* Put all hash values in HASHCODES. */
4804 elf_link_hash_traverse (elf_hash_table (info),
4805 elf_collect_hash_codes, &hashcodesp);
4807 /* We have a problem here. The following code to optimize the table
4808 size requires an integer type with more the 32 bits. If
4809 BFD_HOST_U_64_BIT is set we know about such a type. */
4810 #ifdef BFD_HOST_U_64_BIT
4813 unsigned long int nsyms = hashcodesp - hashcodes;
4816 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4817 unsigned long int *counts ;
4818 bfd *dynobj = elf_hash_table (info)->dynobj;
4819 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4821 /* Possible optimization parameters: if we have NSYMS symbols we say
4822 that the hashing table must at least have NSYMS/4 and at most
4824 minsize = nsyms / 4;
4827 best_size = maxsize = nsyms * 2;
4829 /* Create array where we count the collisions in. We must use bfd_malloc
4830 since the size could be large. */
4832 amt *= sizeof (unsigned long int);
4833 counts = bfd_malloc (amt);
4840 /* Compute the "optimal" size for the hash table. The criteria is a
4841 minimal chain length. The minor criteria is (of course) the size
4843 for (i = minsize; i < maxsize; ++i)
4845 /* Walk through the array of hashcodes and count the collisions. */
4846 BFD_HOST_U_64_BIT max;
4847 unsigned long int j;
4848 unsigned long int fact;
4850 memset (counts, '\0', i * sizeof (unsigned long int));
4852 /* Determine how often each hash bucket is used. */
4853 for (j = 0; j < nsyms; ++j)
4854 ++counts[hashcodes[j] % i];
4856 /* For the weight function we need some information about the
4857 pagesize on the target. This is information need not be 100%
4858 accurate. Since this information is not available (so far) we
4859 define it here to a reasonable default value. If it is crucial
4860 to have a better value some day simply define this value. */
4861 # ifndef BFD_TARGET_PAGESIZE
4862 # define BFD_TARGET_PAGESIZE (4096)
4865 /* We in any case need 2 + NSYMS entries for the size values and
4867 max = (2 + nsyms) * (bed->s->arch_size / 8);
4870 /* Variant 1: optimize for short chains. We add the squares
4871 of all the chain lengths (which favors many small chain
4872 over a few long chains). */
4873 for (j = 0; j < i; ++j)
4874 max += counts[j] * counts[j];
4876 /* This adds penalties for the overall size of the table. */
4877 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4880 /* Variant 2: Optimize a lot more for small table. Here we
4881 also add squares of the size but we also add penalties for
4882 empty slots (the +1 term). */
4883 for (j = 0; j < i; ++j)
4884 max += (1 + counts[j]) * (1 + counts[j]);
4886 /* The overall size of the table is considered, but not as
4887 strong as in variant 1, where it is squared. */
4888 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4892 /* Compare with current best results. */
4893 if (max < best_chlen)
4903 #endif /* defined (BFD_HOST_U_64_BIT) */
4905 /* This is the fallback solution if no 64bit type is available or if we
4906 are not supposed to spend much time on optimizations. We select the
4907 bucket count using a fixed set of numbers. */
4908 for (i = 0; elf_buckets[i] != 0; i++)
4910 best_size = elf_buckets[i];
4911 if (dynsymcount < elf_buckets[i + 1])
4916 /* Free the arrays we needed. */
4922 /* Set up the sizes and contents of the ELF dynamic sections. This is
4923 called by the ELF linker emulation before_allocation routine. We
4924 must set the sizes of the sections before the linker sets the
4925 addresses of the various sections. */
4928 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4931 const char *filter_shlib,
4932 const char * const *auxiliary_filters,
4933 struct bfd_link_info *info,
4934 asection **sinterpptr,
4935 struct bfd_elf_version_tree *verdefs)
4937 bfd_size_type soname_indx;
4939 const struct elf_backend_data *bed;
4940 struct elf_assign_sym_version_info asvinfo;
4944 soname_indx = (bfd_size_type) -1;
4946 if (!is_elf_hash_table (info->hash))
4949 elf_tdata (output_bfd)->relro = info->relro;
4950 if (info->execstack)
4951 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4952 else if (info->noexecstack)
4953 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4957 asection *notesec = NULL;
4960 for (inputobj = info->input_bfds;
4962 inputobj = inputobj->link_next)
4966 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
4968 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4971 if (s->flags & SEC_CODE)
4980 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4981 if (exec && info->relocatable
4982 && notesec->output_section != bfd_abs_section_ptr)
4983 notesec->output_section->flags |= SEC_CODE;
4987 /* Any syms created from now on start with -1 in
4988 got.refcount/offset and plt.refcount/offset. */
4989 elf_hash_table (info)->init_got_refcount
4990 = elf_hash_table (info)->init_got_offset;
4991 elf_hash_table (info)->init_plt_refcount
4992 = elf_hash_table (info)->init_plt_offset;
4994 /* The backend may have to create some sections regardless of whether
4995 we're dynamic or not. */
4996 bed = get_elf_backend_data (output_bfd);
4997 if (bed->elf_backend_always_size_sections
4998 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5001 dynobj = elf_hash_table (info)->dynobj;
5003 /* If there were no dynamic objects in the link, there is nothing to
5008 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5011 if (elf_hash_table (info)->dynamic_sections_created)
5013 struct elf_info_failed eif;
5014 struct elf_link_hash_entry *h;
5016 struct bfd_elf_version_tree *t;
5017 struct bfd_elf_version_expr *d;
5018 bfd_boolean all_defined;
5020 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5021 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5025 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5027 if (soname_indx == (bfd_size_type) -1
5028 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5034 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5036 info->flags |= DF_SYMBOLIC;
5043 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5045 if (indx == (bfd_size_type) -1
5046 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5049 if (info->new_dtags)
5051 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5052 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5057 if (filter_shlib != NULL)
5061 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5062 filter_shlib, TRUE);
5063 if (indx == (bfd_size_type) -1
5064 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5068 if (auxiliary_filters != NULL)
5070 const char * const *p;
5072 for (p = auxiliary_filters; *p != NULL; p++)
5076 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5078 if (indx == (bfd_size_type) -1
5079 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5085 eif.verdefs = verdefs;
5088 /* If we are supposed to export all symbols into the dynamic symbol
5089 table (this is not the normal case), then do so. */
5090 if (info->export_dynamic)
5092 elf_link_hash_traverse (elf_hash_table (info),
5093 _bfd_elf_export_symbol,
5099 /* Make all global versions with definition. */
5100 for (t = verdefs; t != NULL; t = t->next)
5101 for (d = t->globals.list; d != NULL; d = d->next)
5102 if (!d->symver && d->symbol)
5104 const char *verstr, *name;
5105 size_t namelen, verlen, newlen;
5107 struct elf_link_hash_entry *newh;
5110 namelen = strlen (name);
5112 verlen = strlen (verstr);
5113 newlen = namelen + verlen + 3;
5115 newname = bfd_malloc (newlen);
5116 if (newname == NULL)
5118 memcpy (newname, name, namelen);
5120 /* Check the hidden versioned definition. */
5121 p = newname + namelen;
5123 memcpy (p, verstr, verlen + 1);
5124 newh = elf_link_hash_lookup (elf_hash_table (info),
5125 newname, FALSE, FALSE,
5128 || (newh->root.type != bfd_link_hash_defined
5129 && newh->root.type != bfd_link_hash_defweak))
5131 /* Check the default versioned definition. */
5133 memcpy (p, verstr, verlen + 1);
5134 newh = elf_link_hash_lookup (elf_hash_table (info),
5135 newname, FALSE, FALSE,
5140 /* Mark this version if there is a definition and it is
5141 not defined in a shared object. */
5143 && !newh->def_dynamic
5144 && (newh->root.type == bfd_link_hash_defined
5145 || newh->root.type == bfd_link_hash_defweak))
5149 /* Attach all the symbols to their version information. */
5150 asvinfo.output_bfd = output_bfd;
5151 asvinfo.info = info;
5152 asvinfo.verdefs = verdefs;
5153 asvinfo.failed = FALSE;
5155 elf_link_hash_traverse (elf_hash_table (info),
5156 _bfd_elf_link_assign_sym_version,
5161 if (!info->allow_undefined_version)
5163 /* Check if all global versions have a definition. */
5165 for (t = verdefs; t != NULL; t = t->next)
5166 for (d = t->globals.list; d != NULL; d = d->next)
5167 if (!d->symver && !d->script)
5169 (*_bfd_error_handler)
5170 (_("%s: undefined version: %s"),
5171 d->pattern, t->name);
5172 all_defined = FALSE;
5177 bfd_set_error (bfd_error_bad_value);
5182 /* Find all symbols which were defined in a dynamic object and make
5183 the backend pick a reasonable value for them. */
5184 elf_link_hash_traverse (elf_hash_table (info),
5185 _bfd_elf_adjust_dynamic_symbol,
5190 /* Add some entries to the .dynamic section. We fill in some of the
5191 values later, in bfd_elf_final_link, but we must add the entries
5192 now so that we know the final size of the .dynamic section. */
5194 /* If there are initialization and/or finalization functions to
5195 call then add the corresponding DT_INIT/DT_FINI entries. */
5196 h = (info->init_function
5197 ? elf_link_hash_lookup (elf_hash_table (info),
5198 info->init_function, FALSE,
5205 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5208 h = (info->fini_function
5209 ? elf_link_hash_lookup (elf_hash_table (info),
5210 info->fini_function, FALSE,
5217 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5221 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
5223 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5224 if (! info->executable)
5229 for (sub = info->input_bfds; sub != NULL;
5230 sub = sub->link_next)
5231 for (o = sub->sections; o != NULL; o = o->next)
5232 if (elf_section_data (o)->this_hdr.sh_type
5233 == SHT_PREINIT_ARRAY)
5235 (*_bfd_error_handler)
5236 (_("%B: .preinit_array section is not allowed in DSO"),
5241 bfd_set_error (bfd_error_nonrepresentable_section);
5245 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5246 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5249 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
5251 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5252 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5255 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
5257 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5258 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5262 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5263 /* If .dynstr is excluded from the link, we don't want any of
5264 these tags. Strictly, we should be checking each section
5265 individually; This quick check covers for the case where
5266 someone does a /DISCARD/ : { *(*) }. */
5267 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5269 bfd_size_type strsize;
5271 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5272 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5273 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5274 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5275 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5276 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5277 bed->s->sizeof_sym))
5282 /* The backend must work out the sizes of all the other dynamic
5284 if (bed->elf_backend_size_dynamic_sections
5285 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5288 if (elf_hash_table (info)->dynamic_sections_created)
5290 unsigned long section_sym_count;
5293 /* Set up the version definition section. */
5294 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5295 BFD_ASSERT (s != NULL);
5297 /* We may have created additional version definitions if we are
5298 just linking a regular application. */
5299 verdefs = asvinfo.verdefs;
5301 /* Skip anonymous version tag. */
5302 if (verdefs != NULL && verdefs->vernum == 0)
5303 verdefs = verdefs->next;
5305 if (verdefs == NULL && !info->create_default_symver)
5306 s->flags |= SEC_EXCLUDE;
5311 struct bfd_elf_version_tree *t;
5313 Elf_Internal_Verdef def;
5314 Elf_Internal_Verdaux defaux;
5315 struct bfd_link_hash_entry *bh;
5316 struct elf_link_hash_entry *h;
5322 /* Make space for the base version. */
5323 size += sizeof (Elf_External_Verdef);
5324 size += sizeof (Elf_External_Verdaux);
5327 /* Make space for the default version. */
5328 if (info->create_default_symver)
5330 size += sizeof (Elf_External_Verdef);
5334 for (t = verdefs; t != NULL; t = t->next)
5336 struct bfd_elf_version_deps *n;
5338 size += sizeof (Elf_External_Verdef);
5339 size += sizeof (Elf_External_Verdaux);
5342 for (n = t->deps; n != NULL; n = n->next)
5343 size += sizeof (Elf_External_Verdaux);
5347 s->contents = bfd_alloc (output_bfd, s->size);
5348 if (s->contents == NULL && s->size != 0)
5351 /* Fill in the version definition section. */
5355 def.vd_version = VER_DEF_CURRENT;
5356 def.vd_flags = VER_FLG_BASE;
5359 if (info->create_default_symver)
5361 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5362 def.vd_next = sizeof (Elf_External_Verdef);
5366 def.vd_aux = sizeof (Elf_External_Verdef);
5367 def.vd_next = (sizeof (Elf_External_Verdef)
5368 + sizeof (Elf_External_Verdaux));
5371 if (soname_indx != (bfd_size_type) -1)
5373 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5375 def.vd_hash = bfd_elf_hash (soname);
5376 defaux.vda_name = soname_indx;
5383 name = lbasename (output_bfd->filename);
5384 def.vd_hash = bfd_elf_hash (name);
5385 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5387 if (indx == (bfd_size_type) -1)
5389 defaux.vda_name = indx;
5391 defaux.vda_next = 0;
5393 _bfd_elf_swap_verdef_out (output_bfd, &def,
5394 (Elf_External_Verdef *) p);
5395 p += sizeof (Elf_External_Verdef);
5396 if (info->create_default_symver)
5398 /* Add a symbol representing this version. */
5400 if (! (_bfd_generic_link_add_one_symbol
5401 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5403 get_elf_backend_data (dynobj)->collect, &bh)))
5405 h = (struct elf_link_hash_entry *) bh;
5408 h->type = STT_OBJECT;
5409 h->verinfo.vertree = NULL;
5411 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5414 /* Create a duplicate of the base version with the same
5415 aux block, but different flags. */
5418 def.vd_aux = sizeof (Elf_External_Verdef);
5420 def.vd_next = (sizeof (Elf_External_Verdef)
5421 + sizeof (Elf_External_Verdaux));
5424 _bfd_elf_swap_verdef_out (output_bfd, &def,
5425 (Elf_External_Verdef *) p);
5426 p += sizeof (Elf_External_Verdef);
5428 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5429 (Elf_External_Verdaux *) p);
5430 p += sizeof (Elf_External_Verdaux);
5432 for (t = verdefs; t != NULL; t = t->next)
5435 struct bfd_elf_version_deps *n;
5438 for (n = t->deps; n != NULL; n = n->next)
5441 /* Add a symbol representing this version. */
5443 if (! (_bfd_generic_link_add_one_symbol
5444 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5446 get_elf_backend_data (dynobj)->collect, &bh)))
5448 h = (struct elf_link_hash_entry *) bh;
5451 h->type = STT_OBJECT;
5452 h->verinfo.vertree = t;
5454 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5457 def.vd_version = VER_DEF_CURRENT;
5459 if (t->globals.list == NULL
5460 && t->locals.list == NULL
5462 def.vd_flags |= VER_FLG_WEAK;
5463 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5464 def.vd_cnt = cdeps + 1;
5465 def.vd_hash = bfd_elf_hash (t->name);
5466 def.vd_aux = sizeof (Elf_External_Verdef);
5468 if (t->next != NULL)
5469 def.vd_next = (sizeof (Elf_External_Verdef)
5470 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5472 _bfd_elf_swap_verdef_out (output_bfd, &def,
5473 (Elf_External_Verdef *) p);
5474 p += sizeof (Elf_External_Verdef);
5476 defaux.vda_name = h->dynstr_index;
5477 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5479 defaux.vda_next = 0;
5480 if (t->deps != NULL)
5481 defaux.vda_next = sizeof (Elf_External_Verdaux);
5482 t->name_indx = defaux.vda_name;
5484 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5485 (Elf_External_Verdaux *) p);
5486 p += sizeof (Elf_External_Verdaux);
5488 for (n = t->deps; n != NULL; n = n->next)
5490 if (n->version_needed == NULL)
5492 /* This can happen if there was an error in the
5494 defaux.vda_name = 0;
5498 defaux.vda_name = n->version_needed->name_indx;
5499 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5502 if (n->next == NULL)
5503 defaux.vda_next = 0;
5505 defaux.vda_next = sizeof (Elf_External_Verdaux);
5507 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5508 (Elf_External_Verdaux *) p);
5509 p += sizeof (Elf_External_Verdaux);
5513 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5514 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5517 elf_tdata (output_bfd)->cverdefs = cdefs;
5520 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5522 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5525 else if (info->flags & DF_BIND_NOW)
5527 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5533 if (info->executable)
5534 info->flags_1 &= ~ (DF_1_INITFIRST
5537 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5541 /* Work out the size of the version reference section. */
5543 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5544 BFD_ASSERT (s != NULL);
5546 struct elf_find_verdep_info sinfo;
5548 sinfo.output_bfd = output_bfd;
5550 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5551 if (sinfo.vers == 0)
5553 sinfo.failed = FALSE;
5555 elf_link_hash_traverse (elf_hash_table (info),
5556 _bfd_elf_link_find_version_dependencies,
5559 if (elf_tdata (output_bfd)->verref == NULL)
5560 s->flags |= SEC_EXCLUDE;
5563 Elf_Internal_Verneed *t;
5568 /* Build the version definition section. */
5571 for (t = elf_tdata (output_bfd)->verref;
5575 Elf_Internal_Vernaux *a;
5577 size += sizeof (Elf_External_Verneed);
5579 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5580 size += sizeof (Elf_External_Vernaux);
5584 s->contents = bfd_alloc (output_bfd, s->size);
5585 if (s->contents == NULL)
5589 for (t = elf_tdata (output_bfd)->verref;
5594 Elf_Internal_Vernaux *a;
5598 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5601 t->vn_version = VER_NEED_CURRENT;
5603 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5604 elf_dt_name (t->vn_bfd) != NULL
5605 ? elf_dt_name (t->vn_bfd)
5606 : lbasename (t->vn_bfd->filename),
5608 if (indx == (bfd_size_type) -1)
5611 t->vn_aux = sizeof (Elf_External_Verneed);
5612 if (t->vn_nextref == NULL)
5615 t->vn_next = (sizeof (Elf_External_Verneed)
5616 + caux * sizeof (Elf_External_Vernaux));
5618 _bfd_elf_swap_verneed_out (output_bfd, t,
5619 (Elf_External_Verneed *) p);
5620 p += sizeof (Elf_External_Verneed);
5622 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5624 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5625 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5626 a->vna_nodename, FALSE);
5627 if (indx == (bfd_size_type) -1)
5630 if (a->vna_nextptr == NULL)
5633 a->vna_next = sizeof (Elf_External_Vernaux);
5635 _bfd_elf_swap_vernaux_out (output_bfd, a,
5636 (Elf_External_Vernaux *) p);
5637 p += sizeof (Elf_External_Vernaux);
5641 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5642 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5645 elf_tdata (output_bfd)->cverrefs = crefs;
5649 if ((elf_tdata (output_bfd)->cverrefs == 0
5650 && elf_tdata (output_bfd)->cverdefs == 0)
5651 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5652 §ion_sym_count) == 0)
5654 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5655 s->flags |= SEC_EXCLUDE;
5662 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5664 if (!is_elf_hash_table (info->hash))
5667 if (elf_hash_table (info)->dynamic_sections_created)
5670 const struct elf_backend_data *bed;
5672 bfd_size_type dynsymcount;
5673 unsigned long section_sym_count;
5674 size_t bucketcount = 0;
5675 size_t hash_entry_size;
5676 unsigned int dtagcount;
5678 dynobj = elf_hash_table (info)->dynobj;
5680 /* Assign dynsym indicies. In a shared library we generate a
5681 section symbol for each output section, which come first.
5682 Next come all of the back-end allocated local dynamic syms,
5683 followed by the rest of the global symbols. */
5685 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5686 §ion_sym_count);
5688 /* Work out the size of the symbol version section. */
5689 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5690 BFD_ASSERT (s != NULL);
5691 if (dynsymcount != 0
5692 && (s->flags & SEC_EXCLUDE) == 0)
5694 s->size = dynsymcount * sizeof (Elf_External_Versym);
5695 s->contents = bfd_zalloc (output_bfd, s->size);
5696 if (s->contents == NULL)
5699 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5703 /* Set the size of the .dynsym and .hash sections. We counted
5704 the number of dynamic symbols in elf_link_add_object_symbols.
5705 We will build the contents of .dynsym and .hash when we build
5706 the final symbol table, because until then we do not know the
5707 correct value to give the symbols. We built the .dynstr
5708 section as we went along in elf_link_add_object_symbols. */
5709 s = bfd_get_section_by_name (dynobj, ".dynsym");
5710 BFD_ASSERT (s != NULL);
5711 bed = get_elf_backend_data (output_bfd);
5712 s->size = dynsymcount * bed->s->sizeof_sym;
5714 if (dynsymcount != 0)
5716 s->contents = bfd_alloc (output_bfd, s->size);
5717 if (s->contents == NULL)
5720 /* The first entry in .dynsym is a dummy symbol.
5721 Clear all the section syms, in case we don't output them all. */
5722 ++section_sym_count;
5723 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5726 /* Compute the size of the hashing table. As a side effect this
5727 computes the hash values for all the names we export. */
5728 bucketcount = compute_bucket_count (info);
5730 s = bfd_get_section_by_name (dynobj, ".hash");
5731 BFD_ASSERT (s != NULL);
5732 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5733 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5734 s->contents = bfd_zalloc (output_bfd, s->size);
5735 if (s->contents == NULL)
5738 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5739 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5740 s->contents + hash_entry_size);
5742 elf_hash_table (info)->bucketcount = bucketcount;
5744 s = bfd_get_section_by_name (dynobj, ".dynstr");
5745 BFD_ASSERT (s != NULL);
5747 elf_finalize_dynstr (output_bfd, info);
5749 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5751 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5752 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5759 /* Final phase of ELF linker. */
5761 /* A structure we use to avoid passing large numbers of arguments. */
5763 struct elf_final_link_info
5765 /* General link information. */
5766 struct bfd_link_info *info;
5769 /* Symbol string table. */
5770 struct bfd_strtab_hash *symstrtab;
5771 /* .dynsym section. */
5772 asection *dynsym_sec;
5773 /* .hash section. */
5775 /* symbol version section (.gnu.version). */
5776 asection *symver_sec;
5777 /* Buffer large enough to hold contents of any section. */
5779 /* Buffer large enough to hold external relocs of any section. */
5780 void *external_relocs;
5781 /* Buffer large enough to hold internal relocs of any section. */
5782 Elf_Internal_Rela *internal_relocs;
5783 /* Buffer large enough to hold external local symbols of any input
5785 bfd_byte *external_syms;
5786 /* And a buffer for symbol section indices. */
5787 Elf_External_Sym_Shndx *locsym_shndx;
5788 /* Buffer large enough to hold internal local symbols of any input
5790 Elf_Internal_Sym *internal_syms;
5791 /* Array large enough to hold a symbol index for each local symbol
5792 of any input BFD. */
5794 /* Array large enough to hold a section pointer for each local
5795 symbol of any input BFD. */
5796 asection **sections;
5797 /* Buffer to hold swapped out symbols. */
5799 /* And one for symbol section indices. */
5800 Elf_External_Sym_Shndx *symshndxbuf;
5801 /* Number of swapped out symbols in buffer. */
5802 size_t symbuf_count;
5803 /* Number of symbols which fit in symbuf. */
5805 /* And same for symshndxbuf. */
5806 size_t shndxbuf_size;
5809 /* This struct is used to pass information to elf_link_output_extsym. */
5811 struct elf_outext_info
5814 bfd_boolean localsyms;
5815 struct elf_final_link_info *finfo;
5818 /* When performing a relocatable link, the input relocations are
5819 preserved. But, if they reference global symbols, the indices
5820 referenced must be updated. Update all the relocations in
5821 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5824 elf_link_adjust_relocs (bfd *abfd,
5825 Elf_Internal_Shdr *rel_hdr,
5827 struct elf_link_hash_entry **rel_hash)
5830 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5832 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5833 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5834 bfd_vma r_type_mask;
5837 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5839 swap_in = bed->s->swap_reloc_in;
5840 swap_out = bed->s->swap_reloc_out;
5842 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5844 swap_in = bed->s->swap_reloca_in;
5845 swap_out = bed->s->swap_reloca_out;
5850 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5853 if (bed->s->arch_size == 32)
5860 r_type_mask = 0xffffffff;
5864 erela = rel_hdr->contents;
5865 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5867 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5870 if (*rel_hash == NULL)
5873 BFD_ASSERT ((*rel_hash)->indx >= 0);
5875 (*swap_in) (abfd, erela, irela);
5876 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5877 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5878 | (irela[j].r_info & r_type_mask));
5879 (*swap_out) (abfd, irela, erela);
5883 struct elf_link_sort_rela
5889 enum elf_reloc_type_class type;
5890 /* We use this as an array of size int_rels_per_ext_rel. */
5891 Elf_Internal_Rela rela[1];
5895 elf_link_sort_cmp1 (const void *A, const void *B)
5897 const struct elf_link_sort_rela *a = A;
5898 const struct elf_link_sort_rela *b = B;
5899 int relativea, relativeb;
5901 relativea = a->type == reloc_class_relative;
5902 relativeb = b->type == reloc_class_relative;
5904 if (relativea < relativeb)
5906 if (relativea > relativeb)
5908 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5910 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5912 if (a->rela->r_offset < b->rela->r_offset)
5914 if (a->rela->r_offset > b->rela->r_offset)
5920 elf_link_sort_cmp2 (const void *A, const void *B)
5922 const struct elf_link_sort_rela *a = A;
5923 const struct elf_link_sort_rela *b = B;
5926 if (a->u.offset < b->u.offset)
5928 if (a->u.offset > b->u.offset)
5930 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5931 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5936 if (a->rela->r_offset < b->rela->r_offset)
5938 if (a->rela->r_offset > b->rela->r_offset)
5944 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5947 bfd_size_type count, size;
5948 size_t i, ret, sort_elt, ext_size;
5949 bfd_byte *sort, *s_non_relative, *p;
5950 struct elf_link_sort_rela *sq;
5951 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5952 int i2e = bed->s->int_rels_per_ext_rel;
5953 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5954 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5955 struct bfd_link_order *lo;
5958 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5959 if (reldyn == NULL || reldyn->size == 0)
5961 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5962 if (reldyn == NULL || reldyn->size == 0)
5964 ext_size = bed->s->sizeof_rel;
5965 swap_in = bed->s->swap_reloc_in;
5966 swap_out = bed->s->swap_reloc_out;
5970 ext_size = bed->s->sizeof_rela;
5971 swap_in = bed->s->swap_reloca_in;
5972 swap_out = bed->s->swap_reloca_out;
5974 count = reldyn->size / ext_size;
5977 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
5978 if (lo->type == bfd_indirect_link_order)
5980 asection *o = lo->u.indirect.section;
5984 if (size != reldyn->size)
5987 sort_elt = (sizeof (struct elf_link_sort_rela)
5988 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5989 sort = bfd_zmalloc (sort_elt * count);
5992 (*info->callbacks->warning)
5993 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5997 if (bed->s->arch_size == 32)
5998 r_sym_mask = ~(bfd_vma) 0xff;
6000 r_sym_mask = ~(bfd_vma) 0xffffffff;
6002 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6003 if (lo->type == bfd_indirect_link_order)
6005 bfd_byte *erel, *erelend;
6006 asection *o = lo->u.indirect.section;
6008 if (o->contents == NULL && o->size != 0)
6010 /* This is a reloc section that is being handled as a normal
6011 section. See bfd_section_from_shdr. We can't combine
6012 relocs in this case. */
6017 erelend = o->contents + o->size;
6018 p = sort + o->output_offset / ext_size * sort_elt;
6019 while (erel < erelend)
6021 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6022 (*swap_in) (abfd, erel, s->rela);
6023 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
6024 s->u.sym_mask = r_sym_mask;
6030 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
6032 for (i = 0, p = sort; i < count; i++, p += sort_elt)
6034 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6035 if (s->type != reloc_class_relative)
6041 sq = (struct elf_link_sort_rela *) s_non_relative;
6042 for (; i < count; i++, p += sort_elt)
6044 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
6045 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
6047 sp->u.offset = sq->rela->r_offset;
6050 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
6052 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6053 if (lo->type == bfd_indirect_link_order)
6055 bfd_byte *erel, *erelend;
6056 asection *o = lo->u.indirect.section;
6059 erelend = o->contents + o->size;
6060 p = sort + o->output_offset / ext_size * sort_elt;
6061 while (erel < erelend)
6063 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6064 (*swap_out) (abfd, s->rela, erel);
6075 /* Flush the output symbols to the file. */
6078 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
6079 const struct elf_backend_data *bed)
6081 if (finfo->symbuf_count > 0)
6083 Elf_Internal_Shdr *hdr;
6087 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6088 pos = hdr->sh_offset + hdr->sh_size;
6089 amt = finfo->symbuf_count * bed->s->sizeof_sym;
6090 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6091 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
6094 hdr->sh_size += amt;
6095 finfo->symbuf_count = 0;
6101 /* Add a symbol to the output symbol table. */
6104 elf_link_output_sym (struct elf_final_link_info *finfo,
6106 Elf_Internal_Sym *elfsym,
6107 asection *input_sec,
6108 struct elf_link_hash_entry *h)
6111 Elf_External_Sym_Shndx *destshndx;
6112 bfd_boolean (*output_symbol_hook)
6113 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
6114 struct elf_link_hash_entry *);
6115 const struct elf_backend_data *bed;
6117 bed = get_elf_backend_data (finfo->output_bfd);
6118 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
6119 if (output_symbol_hook != NULL)
6121 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
6125 if (name == NULL || *name == '\0')
6126 elfsym->st_name = 0;
6127 else if (input_sec->flags & SEC_EXCLUDE)
6128 elfsym->st_name = 0;
6131 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
6133 if (elfsym->st_name == (unsigned long) -1)
6137 if (finfo->symbuf_count >= finfo->symbuf_size)
6139 if (! elf_link_flush_output_syms (finfo, bed))
6143 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
6144 destshndx = finfo->symshndxbuf;
6145 if (destshndx != NULL)
6147 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6151 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6152 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6153 if (destshndx == NULL)
6155 memset ((char *) destshndx + amt, 0, amt);
6156 finfo->shndxbuf_size *= 2;
6158 destshndx += bfd_get_symcount (finfo->output_bfd);
6161 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
6162 finfo->symbuf_count += 1;
6163 bfd_get_symcount (finfo->output_bfd) += 1;
6168 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6169 allowing an unsatisfied unversioned symbol in the DSO to match a
6170 versioned symbol that would normally require an explicit version.
6171 We also handle the case that a DSO references a hidden symbol
6172 which may be satisfied by a versioned symbol in another DSO. */
6175 elf_link_check_versioned_symbol (struct bfd_link_info *info,
6176 const struct elf_backend_data *bed,
6177 struct elf_link_hash_entry *h)
6180 struct elf_link_loaded_list *loaded;
6182 if (!is_elf_hash_table (info->hash))
6185 switch (h->root.type)
6191 case bfd_link_hash_undefined:
6192 case bfd_link_hash_undefweak:
6193 abfd = h->root.u.undef.abfd;
6194 if ((abfd->flags & DYNAMIC) == 0
6195 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
6199 case bfd_link_hash_defined:
6200 case bfd_link_hash_defweak:
6201 abfd = h->root.u.def.section->owner;
6204 case bfd_link_hash_common:
6205 abfd = h->root.u.c.p->section->owner;
6208 BFD_ASSERT (abfd != NULL);
6210 for (loaded = elf_hash_table (info)->loaded;
6212 loaded = loaded->next)
6215 Elf_Internal_Shdr *hdr;
6216 bfd_size_type symcount;
6217 bfd_size_type extsymcount;
6218 bfd_size_type extsymoff;
6219 Elf_Internal_Shdr *versymhdr;
6220 Elf_Internal_Sym *isym;
6221 Elf_Internal_Sym *isymend;
6222 Elf_Internal_Sym *isymbuf;
6223 Elf_External_Versym *ever;
6224 Elf_External_Versym *extversym;
6226 input = loaded->abfd;
6228 /* We check each DSO for a possible hidden versioned definition. */
6230 || (input->flags & DYNAMIC) == 0
6231 || elf_dynversym (input) == 0)
6234 hdr = &elf_tdata (input)->dynsymtab_hdr;
6236 symcount = hdr->sh_size / bed->s->sizeof_sym;
6237 if (elf_bad_symtab (input))
6239 extsymcount = symcount;
6244 extsymcount = symcount - hdr->sh_info;
6245 extsymoff = hdr->sh_info;
6248 if (extsymcount == 0)
6251 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6253 if (isymbuf == NULL)
6256 /* Read in any version definitions. */
6257 versymhdr = &elf_tdata (input)->dynversym_hdr;
6258 extversym = bfd_malloc (versymhdr->sh_size);
6259 if (extversym == NULL)
6262 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6263 || (bfd_bread (extversym, versymhdr->sh_size, input)
6264 != versymhdr->sh_size))
6272 ever = extversym + extsymoff;
6273 isymend = isymbuf + extsymcount;
6274 for (isym = isymbuf; isym < isymend; isym++, ever++)
6277 Elf_Internal_Versym iver;
6278 unsigned short version_index;
6280 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6281 || isym->st_shndx == SHN_UNDEF)
6284 name = bfd_elf_string_from_elf_section (input,
6287 if (strcmp (name, h->root.root.string) != 0)
6290 _bfd_elf_swap_versym_in (input, ever, &iver);
6292 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6294 /* If we have a non-hidden versioned sym, then it should
6295 have provided a definition for the undefined sym. */
6299 version_index = iver.vs_vers & VERSYM_VERSION;
6300 if (version_index == 1 || version_index == 2)
6302 /* This is the base or first version. We can use it. */
6316 /* Add an external symbol to the symbol table. This is called from
6317 the hash table traversal routine. When generating a shared object,
6318 we go through the symbol table twice. The first time we output
6319 anything that might have been forced to local scope in a version
6320 script. The second time we output the symbols that are still
6324 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6326 struct elf_outext_info *eoinfo = data;
6327 struct elf_final_link_info *finfo = eoinfo->finfo;
6329 Elf_Internal_Sym sym;
6330 asection *input_sec;
6331 const struct elf_backend_data *bed;
6333 if (h->root.type == bfd_link_hash_warning)
6335 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6336 if (h->root.type == bfd_link_hash_new)
6340 /* Decide whether to output this symbol in this pass. */
6341 if (eoinfo->localsyms)
6343 if (!h->forced_local)
6348 if (h->forced_local)
6352 bed = get_elf_backend_data (finfo->output_bfd);
6354 /* If we have an undefined symbol reference here then it must have
6355 come from a shared library that is being linked in. (Undefined
6356 references in regular files have already been handled). If we
6357 are reporting errors for this situation then do so now. */
6358 if (h->root.type == bfd_link_hash_undefined
6361 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6362 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6364 if (! ((*finfo->info->callbacks->undefined_symbol)
6365 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6366 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6368 eoinfo->failed = TRUE;
6373 /* We should also warn if a forced local symbol is referenced from
6374 shared libraries. */
6375 if (! finfo->info->relocatable
6376 && (! finfo->info->shared)
6381 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6383 (*_bfd_error_handler)
6384 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6386 h->root.u.def.section == bfd_abs_section_ptr
6387 ? finfo->output_bfd : h->root.u.def.section->owner,
6388 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6390 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6391 ? "hidden" : "local",
6392 h->root.root.string);
6393 eoinfo->failed = TRUE;
6397 /* We don't want to output symbols that have never been mentioned by
6398 a regular file, or that we have been told to strip. However, if
6399 h->indx is set to -2, the symbol is used by a reloc and we must
6403 else if ((h->def_dynamic
6405 || h->root.type == bfd_link_hash_new)
6409 else if (finfo->info->strip == strip_all)
6411 else if (finfo->info->strip == strip_some
6412 && bfd_hash_lookup (finfo->info->keep_hash,
6413 h->root.root.string, FALSE, FALSE) == NULL)
6415 else if (finfo->info->strip_discarded
6416 && (h->root.type == bfd_link_hash_defined
6417 || h->root.type == bfd_link_hash_defweak)
6418 && elf_discarded_section (h->root.u.def.section))
6423 /* If we're stripping it, and it's not a dynamic symbol, there's
6424 nothing else to do unless it is a forced local symbol. */
6427 && !h->forced_local)
6431 sym.st_size = h->size;
6432 sym.st_other = h->other;
6433 if (h->forced_local)
6434 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6435 else if (h->root.type == bfd_link_hash_undefweak
6436 || h->root.type == bfd_link_hash_defweak)
6437 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6439 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6441 switch (h->root.type)
6444 case bfd_link_hash_new:
6445 case bfd_link_hash_warning:
6449 case bfd_link_hash_undefined:
6450 case bfd_link_hash_undefweak:
6451 input_sec = bfd_und_section_ptr;
6452 sym.st_shndx = SHN_UNDEF;
6455 case bfd_link_hash_defined:
6456 case bfd_link_hash_defweak:
6458 input_sec = h->root.u.def.section;
6459 if (input_sec->output_section != NULL)
6462 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6463 input_sec->output_section);
6464 if (sym.st_shndx == SHN_BAD)
6466 (*_bfd_error_handler)
6467 (_("%B: could not find output section %A for input section %A"),
6468 finfo->output_bfd, input_sec->output_section, input_sec);
6469 eoinfo->failed = TRUE;
6473 /* ELF symbols in relocatable files are section relative,
6474 but in nonrelocatable files they are virtual
6476 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6477 if (! finfo->info->relocatable)
6479 sym.st_value += input_sec->output_section->vma;
6480 if (h->type == STT_TLS)
6482 /* STT_TLS symbols are relative to PT_TLS segment
6484 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6485 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6491 BFD_ASSERT (input_sec->owner == NULL
6492 || (input_sec->owner->flags & DYNAMIC) != 0);
6493 sym.st_shndx = SHN_UNDEF;
6494 input_sec = bfd_und_section_ptr;
6499 case bfd_link_hash_common:
6500 input_sec = h->root.u.c.p->section;
6501 sym.st_shndx = SHN_COMMON;
6502 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6505 case bfd_link_hash_indirect:
6506 /* These symbols are created by symbol versioning. They point
6507 to the decorated version of the name. For example, if the
6508 symbol foo@@GNU_1.2 is the default, which should be used when
6509 foo is used with no version, then we add an indirect symbol
6510 foo which points to foo@@GNU_1.2. We ignore these symbols,
6511 since the indirected symbol is already in the hash table. */
6515 /* Give the processor backend a chance to tweak the symbol value,
6516 and also to finish up anything that needs to be done for this
6517 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6518 forced local syms when non-shared is due to a historical quirk. */
6519 if ((h->dynindx != -1
6521 && ((finfo->info->shared
6522 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6523 || h->root.type != bfd_link_hash_undefweak))
6524 || !h->forced_local)
6525 && elf_hash_table (finfo->info)->dynamic_sections_created)
6527 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6528 (finfo->output_bfd, finfo->info, h, &sym)))
6530 eoinfo->failed = TRUE;
6535 /* If we are marking the symbol as undefined, and there are no
6536 non-weak references to this symbol from a regular object, then
6537 mark the symbol as weak undefined; if there are non-weak
6538 references, mark the symbol as strong. We can't do this earlier,
6539 because it might not be marked as undefined until the
6540 finish_dynamic_symbol routine gets through with it. */
6541 if (sym.st_shndx == SHN_UNDEF
6543 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6544 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6548 if (h->ref_regular_nonweak)
6549 bindtype = STB_GLOBAL;
6551 bindtype = STB_WEAK;
6552 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6555 /* If a non-weak symbol with non-default visibility is not defined
6556 locally, it is a fatal error. */
6557 if (! finfo->info->relocatable
6558 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6559 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6560 && h->root.type == bfd_link_hash_undefined
6563 (*_bfd_error_handler)
6564 (_("%B: %s symbol `%s' isn't defined"),
6566 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6568 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6569 ? "internal" : "hidden",
6570 h->root.root.string);
6571 eoinfo->failed = TRUE;
6575 /* If this symbol should be put in the .dynsym section, then put it
6576 there now. We already know the symbol index. We also fill in
6577 the entry in the .hash section. */
6578 if (h->dynindx != -1
6579 && elf_hash_table (finfo->info)->dynamic_sections_created)
6583 size_t hash_entry_size;
6584 bfd_byte *bucketpos;
6588 sym.st_name = h->dynstr_index;
6589 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6590 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6592 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6593 bucket = h->u.elf_hash_value % bucketcount;
6595 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6596 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6597 + (bucket + 2) * hash_entry_size);
6598 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6599 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6600 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6601 ((bfd_byte *) finfo->hash_sec->contents
6602 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6604 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6606 Elf_Internal_Versym iversym;
6607 Elf_External_Versym *eversym;
6609 if (!h->def_regular)
6611 if (h->verinfo.verdef == NULL)
6612 iversym.vs_vers = 0;
6614 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6618 if (h->verinfo.vertree == NULL)
6619 iversym.vs_vers = 1;
6621 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6622 if (finfo->info->create_default_symver)
6627 iversym.vs_vers |= VERSYM_HIDDEN;
6629 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6630 eversym += h->dynindx;
6631 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6635 /* If we're stripping it, then it was just a dynamic symbol, and
6636 there's nothing else to do. */
6637 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6640 h->indx = bfd_get_symcount (finfo->output_bfd);
6642 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6644 eoinfo->failed = TRUE;
6651 /* Return TRUE if special handling is done for relocs in SEC against
6652 symbols defined in discarded sections. */
6655 elf_section_ignore_discarded_relocs (asection *sec)
6657 const struct elf_backend_data *bed;
6659 switch (sec->sec_info_type)
6661 case ELF_INFO_TYPE_STABS:
6662 case ELF_INFO_TYPE_EH_FRAME:
6668 bed = get_elf_backend_data (sec->owner);
6669 if (bed->elf_backend_ignore_discarded_relocs != NULL
6670 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6676 enum action_discarded
6682 /* Return a mask saying how ld should treat relocations in SEC against
6683 symbols defined in discarded sections. If this function returns
6684 COMPLAIN set, ld will issue a warning message. If this function
6685 returns PRETEND set, and the discarded section was link-once and the
6686 same size as the kept link-once section, ld will pretend that the
6687 symbol was actually defined in the kept section. Otherwise ld will
6688 zero the reloc (at least that is the intent, but some cooperation by
6689 the target dependent code is needed, particularly for REL targets). */
6692 elf_action_discarded (asection *sec)
6694 if (sec->flags & SEC_DEBUGGING)
6697 if (strcmp (".eh_frame", sec->name) == 0)
6700 if (strcmp (".gcc_except_table", sec->name) == 0)
6703 if (strcmp (".PARISC.unwind", sec->name) == 0)
6706 if (strcmp (".fixup", sec->name) == 0)
6709 return COMPLAIN | PRETEND;
6712 /* Find a match between a section and a member of a section group. */
6715 match_group_member (asection *sec, asection *group)
6717 asection *first = elf_next_in_group (group);
6718 asection *s = first;
6722 if (bfd_elf_match_symbols_in_sections (s, sec))
6732 /* Check if the kept section of a discarded section SEC can be used
6733 to replace it. Return the replacement if it is OK. Otherwise return
6737 _bfd_elf_check_kept_section (asection *sec)
6741 kept = sec->kept_section;
6744 if (elf_sec_group (sec) != NULL)
6745 kept = match_group_member (sec, kept);
6746 if (kept != NULL && sec->size != kept->size)
6752 /* Link an input file into the linker output file. This function
6753 handles all the sections and relocations of the input file at once.
6754 This is so that we only have to read the local symbols once, and
6755 don't have to keep them in memory. */
6758 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6760 bfd_boolean (*relocate_section)
6761 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6762 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6764 Elf_Internal_Shdr *symtab_hdr;
6767 Elf_Internal_Sym *isymbuf;
6768 Elf_Internal_Sym *isym;
6769 Elf_Internal_Sym *isymend;
6771 asection **ppsection;
6773 const struct elf_backend_data *bed;
6774 bfd_boolean emit_relocs;
6775 struct elf_link_hash_entry **sym_hashes;
6777 output_bfd = finfo->output_bfd;
6778 bed = get_elf_backend_data (output_bfd);
6779 relocate_section = bed->elf_backend_relocate_section;
6781 /* If this is a dynamic object, we don't want to do anything here:
6782 we don't want the local symbols, and we don't want the section
6784 if ((input_bfd->flags & DYNAMIC) != 0)
6787 emit_relocs = (finfo->info->relocatable
6788 || finfo->info->emitrelocations);
6790 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6791 if (elf_bad_symtab (input_bfd))
6793 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6798 locsymcount = symtab_hdr->sh_info;
6799 extsymoff = symtab_hdr->sh_info;
6802 /* Read the local symbols. */
6803 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6804 if (isymbuf == NULL && locsymcount != 0)
6806 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6807 finfo->internal_syms,
6808 finfo->external_syms,
6809 finfo->locsym_shndx);
6810 if (isymbuf == NULL)
6814 /* Find local symbol sections and adjust values of symbols in
6815 SEC_MERGE sections. Write out those local symbols we know are
6816 going into the output file. */
6817 isymend = isymbuf + locsymcount;
6818 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6820 isym++, pindex++, ppsection++)
6824 Elf_Internal_Sym osym;
6828 if (elf_bad_symtab (input_bfd))
6830 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6837 if (isym->st_shndx == SHN_UNDEF)
6838 isec = bfd_und_section_ptr;
6839 else if (isym->st_shndx < SHN_LORESERVE
6840 || isym->st_shndx > SHN_HIRESERVE)
6842 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6844 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6845 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6847 _bfd_merged_section_offset (output_bfd, &isec,
6848 elf_section_data (isec)->sec_info,
6851 else if (isym->st_shndx == SHN_ABS)
6852 isec = bfd_abs_section_ptr;
6853 else if (isym->st_shndx == SHN_COMMON)
6854 isec = bfd_com_section_ptr;
6863 /* Don't output the first, undefined, symbol. */
6864 if (ppsection == finfo->sections)
6867 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6869 /* We never output section symbols. Instead, we use the
6870 section symbol of the corresponding section in the output
6875 /* If we are stripping all symbols, we don't want to output this
6877 if (finfo->info->strip == strip_all)
6880 /* If we are discarding all local symbols, we don't want to
6881 output this one. If we are generating a relocatable output
6882 file, then some of the local symbols may be required by
6883 relocs; we output them below as we discover that they are
6885 if (finfo->info->discard == discard_all)
6888 /* If this symbol is defined in a section which we are
6889 discarding, we don't need to keep it, but note that
6890 linker_mark is only reliable for sections that have contents.
6891 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6892 as well as linker_mark. */
6893 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6895 || (! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6896 || (! finfo->info->relocatable
6897 && (isec->flags & SEC_EXCLUDE) != 0)))
6900 /* If the section is not in the output BFD's section list, it is not
6902 if (bfd_section_removed_from_list (output_bfd, isec->output_section))
6905 /* Get the name of the symbol. */
6906 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6911 /* See if we are discarding symbols with this name. */
6912 if ((finfo->info->strip == strip_some
6913 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6915 || (((finfo->info->discard == discard_sec_merge
6916 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6917 || finfo->info->discard == discard_l)
6918 && bfd_is_local_label_name (input_bfd, name)))
6921 /* If we get here, we are going to output this symbol. */
6925 /* Adjust the section index for the output file. */
6926 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6927 isec->output_section);
6928 if (osym.st_shndx == SHN_BAD)
6931 *pindex = bfd_get_symcount (output_bfd);
6933 /* ELF symbols in relocatable files are section relative, but
6934 in executable files they are virtual addresses. Note that
6935 this code assumes that all ELF sections have an associated
6936 BFD section with a reasonable value for output_offset; below
6937 we assume that they also have a reasonable value for
6938 output_section. Any special sections must be set up to meet
6939 these requirements. */
6940 osym.st_value += isec->output_offset;
6941 if (! finfo->info->relocatable)
6943 osym.st_value += isec->output_section->vma;
6944 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6946 /* STT_TLS symbols are relative to PT_TLS segment base. */
6947 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6948 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6952 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6956 /* Relocate the contents of each section. */
6957 sym_hashes = elf_sym_hashes (input_bfd);
6958 for (o = input_bfd->sections; o != NULL; o = o->next)
6962 if (! o->linker_mark)
6964 /* This section was omitted from the link. */
6968 if ((o->flags & SEC_HAS_CONTENTS) == 0
6969 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6972 if ((o->flags & SEC_LINKER_CREATED) != 0)
6974 /* Section was created by _bfd_elf_link_create_dynamic_sections
6979 /* Get the contents of the section. They have been cached by a
6980 relaxation routine. Note that o is a section in an input
6981 file, so the contents field will not have been set by any of
6982 the routines which work on output files. */
6983 if (elf_section_data (o)->this_hdr.contents != NULL)
6984 contents = elf_section_data (o)->this_hdr.contents;
6987 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6989 contents = finfo->contents;
6990 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6994 if ((o->flags & SEC_RELOC) != 0)
6996 Elf_Internal_Rela *internal_relocs;
6997 bfd_vma r_type_mask;
7000 /* Get the swapped relocs. */
7002 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
7003 finfo->internal_relocs, FALSE);
7004 if (internal_relocs == NULL
7005 && o->reloc_count > 0)
7008 if (bed->s->arch_size == 32)
7015 r_type_mask = 0xffffffff;
7019 /* Run through the relocs looking for any against symbols
7020 from discarded sections and section symbols from
7021 removed link-once sections. Complain about relocs
7022 against discarded sections. Zero relocs against removed
7023 link-once sections. Preserve debug information as much
7025 if (!elf_section_ignore_discarded_relocs (o))
7027 Elf_Internal_Rela *rel, *relend;
7028 unsigned int action = elf_action_discarded (o);
7030 rel = internal_relocs;
7031 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
7032 for ( ; rel < relend; rel++)
7034 unsigned long r_symndx = rel->r_info >> r_sym_shift;
7035 asection **ps, *sec;
7036 struct elf_link_hash_entry *h = NULL;
7037 const char *sym_name;
7039 if (r_symndx == STN_UNDEF)
7042 if (r_symndx >= locsymcount
7043 || (elf_bad_symtab (input_bfd)
7044 && finfo->sections[r_symndx] == NULL))
7046 h = sym_hashes[r_symndx - extsymoff];
7048 /* Badly formatted input files can contain relocs that
7049 reference non-existant symbols. Check here so that
7050 we do not seg fault. */
7055 sprintf_vma (buffer, rel->r_info);
7056 (*_bfd_error_handler)
7057 (_("error: %B contains a reloc (0x%s) for section %A "
7058 "that references a non-existent global symbol"),
7059 input_bfd, o, buffer);
7060 bfd_set_error (bfd_error_bad_value);
7064 while (h->root.type == bfd_link_hash_indirect
7065 || h->root.type == bfd_link_hash_warning)
7066 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7068 if (h->root.type != bfd_link_hash_defined
7069 && h->root.type != bfd_link_hash_defweak)
7072 ps = &h->root.u.def.section;
7073 sym_name = h->root.root.string;
7077 Elf_Internal_Sym *sym = isymbuf + r_symndx;
7078 ps = &finfo->sections[r_symndx];
7079 sym_name = bfd_elf_sym_name (input_bfd,
7084 /* Complain if the definition comes from a
7085 discarded section. */
7086 if ((sec = *ps) != NULL && elf_discarded_section (sec))
7088 BFD_ASSERT (r_symndx != 0);
7089 if (action & COMPLAIN)
7090 (*finfo->info->callbacks->einfo)
7091 (_("%X`%s' referenced in section `%A' of %B: "
7092 "defined in discarded section `%A' of %B"),
7093 sym_name, o, input_bfd, sec, sec->owner);
7095 /* Try to do the best we can to support buggy old
7096 versions of gcc. If we've warned, or this is
7097 debugging info, pretend that the symbol is
7098 really defined in the kept linkonce section.
7099 FIXME: This is quite broken. Modifying the
7100 symbol here means we will be changing all later
7101 uses of the symbol, not just in this section.
7102 The only thing that makes this half reasonable
7103 is that we warn in non-debug sections, and
7104 debug sections tend to come after other
7106 if (action & PRETEND)
7110 kept = _bfd_elf_check_kept_section (sec);
7118 /* Remove the symbol reference from the reloc, but
7119 don't kill the reloc completely. This is so that
7120 a zero value will be written into the section,
7121 which may have non-zero contents put there by the
7122 assembler. Zero in things like an eh_frame fde
7123 pc_begin allows stack unwinders to recognize the
7125 rel->r_info &= r_type_mask;
7131 /* Relocate the section by invoking a back end routine.
7133 The back end routine is responsible for adjusting the
7134 section contents as necessary, and (if using Rela relocs
7135 and generating a relocatable output file) adjusting the
7136 reloc addend as necessary.
7138 The back end routine does not have to worry about setting
7139 the reloc address or the reloc symbol index.
7141 The back end routine is given a pointer to the swapped in
7142 internal symbols, and can access the hash table entries
7143 for the external symbols via elf_sym_hashes (input_bfd).
7145 When generating relocatable output, the back end routine
7146 must handle STB_LOCAL/STT_SECTION symbols specially. The
7147 output symbol is going to be a section symbol
7148 corresponding to the output section, which will require
7149 the addend to be adjusted. */
7151 if (! (*relocate_section) (output_bfd, finfo->info,
7152 input_bfd, o, contents,
7160 Elf_Internal_Rela *irela;
7161 Elf_Internal_Rela *irelaend;
7162 bfd_vma last_offset;
7163 struct elf_link_hash_entry **rel_hash;
7164 struct elf_link_hash_entry **rel_hash_list;
7165 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
7166 unsigned int next_erel;
7167 bfd_boolean rela_normal;
7169 input_rel_hdr = &elf_section_data (o)->rel_hdr;
7170 rela_normal = (bed->rela_normal
7171 && (input_rel_hdr->sh_entsize
7172 == bed->s->sizeof_rela));
7174 /* Adjust the reloc addresses and symbol indices. */
7176 irela = internal_relocs;
7177 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
7178 rel_hash = (elf_section_data (o->output_section)->rel_hashes
7179 + elf_section_data (o->output_section)->rel_count
7180 + elf_section_data (o->output_section)->rel_count2);
7181 rel_hash_list = rel_hash;
7182 last_offset = o->output_offset;
7183 if (!finfo->info->relocatable)
7184 last_offset += o->output_section->vma;
7185 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
7187 unsigned long r_symndx;
7189 Elf_Internal_Sym sym;
7191 if (next_erel == bed->s->int_rels_per_ext_rel)
7197 irela->r_offset = _bfd_elf_section_offset (output_bfd,
7200 if (irela->r_offset >= (bfd_vma) -2)
7202 /* This is a reloc for a deleted entry or somesuch.
7203 Turn it into an R_*_NONE reloc, at the same
7204 offset as the last reloc. elf_eh_frame.c and
7205 elf_bfd_discard_info rely on reloc offsets
7207 irela->r_offset = last_offset;
7209 irela->r_addend = 0;
7213 irela->r_offset += o->output_offset;
7215 /* Relocs in an executable have to be virtual addresses. */
7216 if (!finfo->info->relocatable)
7217 irela->r_offset += o->output_section->vma;
7219 last_offset = irela->r_offset;
7221 r_symndx = irela->r_info >> r_sym_shift;
7222 if (r_symndx == STN_UNDEF)
7225 if (r_symndx >= locsymcount
7226 || (elf_bad_symtab (input_bfd)
7227 && finfo->sections[r_symndx] == NULL))
7229 struct elf_link_hash_entry *rh;
7232 /* This is a reloc against a global symbol. We
7233 have not yet output all the local symbols, so
7234 we do not know the symbol index of any global
7235 symbol. We set the rel_hash entry for this
7236 reloc to point to the global hash table entry
7237 for this symbol. The symbol index is then
7238 set at the end of bfd_elf_final_link. */
7239 indx = r_symndx - extsymoff;
7240 rh = elf_sym_hashes (input_bfd)[indx];
7241 while (rh->root.type == bfd_link_hash_indirect
7242 || rh->root.type == bfd_link_hash_warning)
7243 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7245 /* Setting the index to -2 tells
7246 elf_link_output_extsym that this symbol is
7248 BFD_ASSERT (rh->indx < 0);
7256 /* This is a reloc against a local symbol. */
7259 sym = isymbuf[r_symndx];
7260 sec = finfo->sections[r_symndx];
7261 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7263 /* I suppose the backend ought to fill in the
7264 section of any STT_SECTION symbol against a
7265 processor specific section. */
7267 if (bfd_is_abs_section (sec))
7269 else if (sec == NULL || sec->owner == NULL)
7271 bfd_set_error (bfd_error_bad_value);
7276 asection *osec = sec->output_section;
7278 /* If we have discarded a section, the output
7279 section will be the absolute section. In
7280 case of discarded link-once and discarded
7281 SEC_MERGE sections, use the kept section. */
7282 if (bfd_is_abs_section (osec)
7283 && sec->kept_section != NULL
7284 && sec->kept_section->output_section != NULL)
7286 osec = sec->kept_section->output_section;
7287 irela->r_addend -= osec->vma;
7290 if (!bfd_is_abs_section (osec))
7292 r_symndx = osec->target_index;
7293 BFD_ASSERT (r_symndx != 0);
7297 /* Adjust the addend according to where the
7298 section winds up in the output section. */
7300 irela->r_addend += sec->output_offset;
7304 if (finfo->indices[r_symndx] == -1)
7306 unsigned long shlink;
7310 if (finfo->info->strip == strip_all)
7312 /* You can't do ld -r -s. */
7313 bfd_set_error (bfd_error_invalid_operation);
7317 /* This symbol was skipped earlier, but
7318 since it is needed by a reloc, we
7319 must output it now. */
7320 shlink = symtab_hdr->sh_link;
7321 name = (bfd_elf_string_from_elf_section
7322 (input_bfd, shlink, sym.st_name));
7326 osec = sec->output_section;
7328 _bfd_elf_section_from_bfd_section (output_bfd,
7330 if (sym.st_shndx == SHN_BAD)
7333 sym.st_value += sec->output_offset;
7334 if (! finfo->info->relocatable)
7336 sym.st_value += osec->vma;
7337 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7339 /* STT_TLS symbols are relative to PT_TLS
7341 BFD_ASSERT (elf_hash_table (finfo->info)
7343 sym.st_value -= (elf_hash_table (finfo->info)
7348 finfo->indices[r_symndx]
7349 = bfd_get_symcount (output_bfd);
7351 if (! elf_link_output_sym (finfo, name, &sym, sec,
7356 r_symndx = finfo->indices[r_symndx];
7359 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7360 | (irela->r_info & r_type_mask));
7363 /* Swap out the relocs. */
7364 if (input_rel_hdr->sh_size != 0
7365 && !bed->elf_backend_emit_relocs (output_bfd, o,
7371 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7372 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7374 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7375 * bed->s->int_rels_per_ext_rel);
7376 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
7377 if (!bed->elf_backend_emit_relocs (output_bfd, o,
7386 /* Write out the modified section contents. */
7387 if (bed->elf_backend_write_section
7388 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7390 /* Section written out. */
7392 else switch (o->sec_info_type)
7394 case ELF_INFO_TYPE_STABS:
7395 if (! (_bfd_write_section_stabs
7397 &elf_hash_table (finfo->info)->stab_info,
7398 o, &elf_section_data (o)->sec_info, contents)))
7401 case ELF_INFO_TYPE_MERGE:
7402 if (! _bfd_write_merged_section (output_bfd, o,
7403 elf_section_data (o)->sec_info))
7406 case ELF_INFO_TYPE_EH_FRAME:
7408 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7415 if (! (o->flags & SEC_EXCLUDE)
7416 && ! bfd_set_section_contents (output_bfd, o->output_section,
7418 (file_ptr) o->output_offset,
7429 /* Generate a reloc when linking an ELF file. This is a reloc
7430 requested by the linker, and does come from any input file. This
7431 is used to build constructor and destructor tables when linking
7435 elf_reloc_link_order (bfd *output_bfd,
7436 struct bfd_link_info *info,
7437 asection *output_section,
7438 struct bfd_link_order *link_order)
7440 reloc_howto_type *howto;
7444 struct elf_link_hash_entry **rel_hash_ptr;
7445 Elf_Internal_Shdr *rel_hdr;
7446 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7447 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7451 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7454 bfd_set_error (bfd_error_bad_value);
7458 addend = link_order->u.reloc.p->addend;
7460 /* Figure out the symbol index. */
7461 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7462 + elf_section_data (output_section)->rel_count
7463 + elf_section_data (output_section)->rel_count2);
7464 if (link_order->type == bfd_section_reloc_link_order)
7466 indx = link_order->u.reloc.p->u.section->target_index;
7467 BFD_ASSERT (indx != 0);
7468 *rel_hash_ptr = NULL;
7472 struct elf_link_hash_entry *h;
7474 /* Treat a reloc against a defined symbol as though it were
7475 actually against the section. */
7476 h = ((struct elf_link_hash_entry *)
7477 bfd_wrapped_link_hash_lookup (output_bfd, info,
7478 link_order->u.reloc.p->u.name,
7479 FALSE, FALSE, TRUE));
7481 && (h->root.type == bfd_link_hash_defined
7482 || h->root.type == bfd_link_hash_defweak))
7486 section = h->root.u.def.section;
7487 indx = section->output_section->target_index;
7488 *rel_hash_ptr = NULL;
7489 /* It seems that we ought to add the symbol value to the
7490 addend here, but in practice it has already been added
7491 because it was passed to constructor_callback. */
7492 addend += section->output_section->vma + section->output_offset;
7496 /* Setting the index to -2 tells elf_link_output_extsym that
7497 this symbol is used by a reloc. */
7504 if (! ((*info->callbacks->unattached_reloc)
7505 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7511 /* If this is an inplace reloc, we must write the addend into the
7513 if (howto->partial_inplace && addend != 0)
7516 bfd_reloc_status_type rstat;
7519 const char *sym_name;
7521 size = bfd_get_reloc_size (howto);
7522 buf = bfd_zmalloc (size);
7525 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7532 case bfd_reloc_outofrange:
7535 case bfd_reloc_overflow:
7536 if (link_order->type == bfd_section_reloc_link_order)
7537 sym_name = bfd_section_name (output_bfd,
7538 link_order->u.reloc.p->u.section);
7540 sym_name = link_order->u.reloc.p->u.name;
7541 if (! ((*info->callbacks->reloc_overflow)
7542 (info, NULL, sym_name, howto->name, addend, NULL,
7543 NULL, (bfd_vma) 0)))
7550 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7551 link_order->offset, size);
7557 /* The address of a reloc is relative to the section in a
7558 relocatable file, and is a virtual address in an executable
7560 offset = link_order->offset;
7561 if (! info->relocatable)
7562 offset += output_section->vma;
7564 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7566 irel[i].r_offset = offset;
7568 irel[i].r_addend = 0;
7570 if (bed->s->arch_size == 32)
7571 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7573 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7575 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7576 erel = rel_hdr->contents;
7577 if (rel_hdr->sh_type == SHT_REL)
7579 erel += (elf_section_data (output_section)->rel_count
7580 * bed->s->sizeof_rel);
7581 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7585 irel[0].r_addend = addend;
7586 erel += (elf_section_data (output_section)->rel_count
7587 * bed->s->sizeof_rela);
7588 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7591 ++elf_section_data (output_section)->rel_count;
7597 /* Get the output vma of the section pointed to by the sh_link field. */
7600 elf_get_linked_section_vma (struct bfd_link_order *p)
7602 Elf_Internal_Shdr **elf_shdrp;
7606 s = p->u.indirect.section;
7607 elf_shdrp = elf_elfsections (s->owner);
7608 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7609 elfsec = elf_shdrp[elfsec]->sh_link;
7611 The Intel C compiler generates SHT_IA_64_UNWIND with
7612 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7613 sh_info fields. Hence we could get the situation
7614 where elfsec is 0. */
7617 const struct elf_backend_data *bed
7618 = get_elf_backend_data (s->owner);
7619 if (bed->link_order_error_handler)
7620 bed->link_order_error_handler
7621 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7626 s = elf_shdrp[elfsec]->bfd_section;
7627 return s->output_section->vma + s->output_offset;
7632 /* Compare two sections based on the locations of the sections they are
7633 linked to. Used by elf_fixup_link_order. */
7636 compare_link_order (const void * a, const void * b)
7641 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7642 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7649 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7650 order as their linked sections. Returns false if this could not be done
7651 because an output section includes both ordered and unordered
7652 sections. Ideally we'd do this in the linker proper. */
7655 elf_fixup_link_order (bfd *abfd, asection *o)
7660 struct bfd_link_order *p;
7662 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7664 struct bfd_link_order **sections;
7670 for (p = o->map_head.link_order; p != NULL; p = p->next)
7672 if (p->type == bfd_indirect_link_order
7673 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7674 == bfd_target_elf_flavour)
7675 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7677 s = p->u.indirect.section;
7678 elfsec = _bfd_elf_section_from_bfd_section (sub, s);
7680 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7689 if (!seen_linkorder)
7692 if (seen_other && seen_linkorder)
7694 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7696 bfd_set_error (bfd_error_bad_value);
7700 sections = (struct bfd_link_order **)
7701 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7704 for (p = o->map_head.link_order; p != NULL; p = p->next)
7706 sections[seen_linkorder++] = p;
7708 /* Sort the input sections in the order of their linked section. */
7709 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7710 compare_link_order);
7712 /* Change the offsets of the sections. */
7714 for (n = 0; n < seen_linkorder; n++)
7716 s = sections[n]->u.indirect.section;
7717 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7718 s->output_offset = offset;
7719 sections[n]->offset = offset;
7720 offset += sections[n]->size;
7727 /* Do the final step of an ELF link. */
7730 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7732 bfd_boolean dynamic;
7733 bfd_boolean emit_relocs;
7735 struct elf_final_link_info finfo;
7736 register asection *o;
7737 register struct bfd_link_order *p;
7739 bfd_size_type max_contents_size;
7740 bfd_size_type max_external_reloc_size;
7741 bfd_size_type max_internal_reloc_count;
7742 bfd_size_type max_sym_count;
7743 bfd_size_type max_sym_shndx_count;
7745 Elf_Internal_Sym elfsym;
7747 Elf_Internal_Shdr *symtab_hdr;
7748 Elf_Internal_Shdr *symtab_shndx_hdr;
7749 Elf_Internal_Shdr *symstrtab_hdr;
7750 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7751 struct elf_outext_info eoinfo;
7753 size_t relativecount = 0;
7754 asection *reldyn = 0;
7757 if (! is_elf_hash_table (info->hash))
7761 abfd->flags |= DYNAMIC;
7763 dynamic = elf_hash_table (info)->dynamic_sections_created;
7764 dynobj = elf_hash_table (info)->dynobj;
7766 emit_relocs = (info->relocatable
7767 || info->emitrelocations
7768 || bed->elf_backend_emit_relocs);
7771 finfo.output_bfd = abfd;
7772 finfo.symstrtab = _bfd_elf_stringtab_init ();
7773 if (finfo.symstrtab == NULL)
7778 finfo.dynsym_sec = NULL;
7779 finfo.hash_sec = NULL;
7780 finfo.symver_sec = NULL;
7784 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7785 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7786 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7787 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7788 /* Note that it is OK if symver_sec is NULL. */
7791 finfo.contents = NULL;
7792 finfo.external_relocs = NULL;
7793 finfo.internal_relocs = NULL;
7794 finfo.external_syms = NULL;
7795 finfo.locsym_shndx = NULL;
7796 finfo.internal_syms = NULL;
7797 finfo.indices = NULL;
7798 finfo.sections = NULL;
7799 finfo.symbuf = NULL;
7800 finfo.symshndxbuf = NULL;
7801 finfo.symbuf_count = 0;
7802 finfo.shndxbuf_size = 0;
7804 /* Count up the number of relocations we will output for each output
7805 section, so that we know the sizes of the reloc sections. We
7806 also figure out some maximum sizes. */
7807 max_contents_size = 0;
7808 max_external_reloc_size = 0;
7809 max_internal_reloc_count = 0;
7811 max_sym_shndx_count = 0;
7813 for (o = abfd->sections; o != NULL; o = o->next)
7815 struct bfd_elf_section_data *esdo = elf_section_data (o);
7818 for (p = o->map_head.link_order; p != NULL; p = p->next)
7820 unsigned int reloc_count = 0;
7821 struct bfd_elf_section_data *esdi = NULL;
7822 unsigned int *rel_count1;
7824 if (p->type == bfd_section_reloc_link_order
7825 || p->type == bfd_symbol_reloc_link_order)
7827 else if (p->type == bfd_indirect_link_order)
7831 sec = p->u.indirect.section;
7832 esdi = elf_section_data (sec);
7834 /* Mark all sections which are to be included in the
7835 link. This will normally be every section. We need
7836 to do this so that we can identify any sections which
7837 the linker has decided to not include. */
7838 sec->linker_mark = TRUE;
7840 if (sec->flags & SEC_MERGE)
7843 if (info->relocatable || info->emitrelocations)
7844 reloc_count = sec->reloc_count;
7845 else if (bed->elf_backend_count_relocs)
7847 Elf_Internal_Rela * relocs;
7849 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7852 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7854 if (elf_section_data (o)->relocs != relocs)
7858 if (sec->rawsize > max_contents_size)
7859 max_contents_size = sec->rawsize;
7860 if (sec->size > max_contents_size)
7861 max_contents_size = sec->size;
7863 /* We are interested in just local symbols, not all
7865 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7866 && (sec->owner->flags & DYNAMIC) == 0)
7870 if (elf_bad_symtab (sec->owner))
7871 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7872 / bed->s->sizeof_sym);
7874 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7876 if (sym_count > max_sym_count)
7877 max_sym_count = sym_count;
7879 if (sym_count > max_sym_shndx_count
7880 && elf_symtab_shndx (sec->owner) != 0)
7881 max_sym_shndx_count = sym_count;
7883 if ((sec->flags & SEC_RELOC) != 0)
7887 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7888 if (ext_size > max_external_reloc_size)
7889 max_external_reloc_size = ext_size;
7890 if (sec->reloc_count > max_internal_reloc_count)
7891 max_internal_reloc_count = sec->reloc_count;
7896 if (reloc_count == 0)
7899 o->reloc_count += reloc_count;
7901 /* MIPS may have a mix of REL and RELA relocs on sections.
7902 To support this curious ABI we keep reloc counts in
7903 elf_section_data too. We must be careful to add the
7904 relocations from the input section to the right output
7905 count. FIXME: Get rid of one count. We have
7906 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7907 rel_count1 = &esdo->rel_count;
7910 bfd_boolean same_size;
7911 bfd_size_type entsize1;
7913 entsize1 = esdi->rel_hdr.sh_entsize;
7914 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7915 || entsize1 == bed->s->sizeof_rela);
7916 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7919 rel_count1 = &esdo->rel_count2;
7921 if (esdi->rel_hdr2 != NULL)
7923 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7924 unsigned int alt_count;
7925 unsigned int *rel_count2;
7927 BFD_ASSERT (entsize2 != entsize1
7928 && (entsize2 == bed->s->sizeof_rel
7929 || entsize2 == bed->s->sizeof_rela));
7931 rel_count2 = &esdo->rel_count2;
7933 rel_count2 = &esdo->rel_count;
7935 /* The following is probably too simplistic if the
7936 backend counts output relocs unusually. */
7937 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7938 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7939 *rel_count2 += alt_count;
7940 reloc_count -= alt_count;
7943 *rel_count1 += reloc_count;
7946 if (o->reloc_count > 0)
7947 o->flags |= SEC_RELOC;
7950 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7951 set it (this is probably a bug) and if it is set
7952 assign_section_numbers will create a reloc section. */
7953 o->flags &=~ SEC_RELOC;
7956 /* If the SEC_ALLOC flag is not set, force the section VMA to
7957 zero. This is done in elf_fake_sections as well, but forcing
7958 the VMA to 0 here will ensure that relocs against these
7959 sections are handled correctly. */
7960 if ((o->flags & SEC_ALLOC) == 0
7961 && ! o->user_set_vma)
7965 if (! info->relocatable && merged)
7966 elf_link_hash_traverse (elf_hash_table (info),
7967 _bfd_elf_link_sec_merge_syms, abfd);
7969 /* Figure out the file positions for everything but the symbol table
7970 and the relocs. We set symcount to force assign_section_numbers
7971 to create a symbol table. */
7972 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7973 BFD_ASSERT (! abfd->output_has_begun);
7974 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7977 /* Set sizes, and assign file positions for reloc sections. */
7978 for (o = abfd->sections; o != NULL; o = o->next)
7980 if ((o->flags & SEC_RELOC) != 0)
7982 if (!(_bfd_elf_link_size_reloc_section
7983 (abfd, &elf_section_data (o)->rel_hdr, o)))
7986 if (elf_section_data (o)->rel_hdr2
7987 && !(_bfd_elf_link_size_reloc_section
7988 (abfd, elf_section_data (o)->rel_hdr2, o)))
7992 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7993 to count upwards while actually outputting the relocations. */
7994 elf_section_data (o)->rel_count = 0;
7995 elf_section_data (o)->rel_count2 = 0;
7998 _bfd_elf_assign_file_positions_for_relocs (abfd);
8000 /* We have now assigned file positions for all the sections except
8001 .symtab and .strtab. We start the .symtab section at the current
8002 file position, and write directly to it. We build the .strtab
8003 section in memory. */
8004 bfd_get_symcount (abfd) = 0;
8005 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8006 /* sh_name is set in prep_headers. */
8007 symtab_hdr->sh_type = SHT_SYMTAB;
8008 /* sh_flags, sh_addr and sh_size all start off zero. */
8009 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8010 /* sh_link is set in assign_section_numbers. */
8011 /* sh_info is set below. */
8012 /* sh_offset is set just below. */
8013 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
8015 off = elf_tdata (abfd)->next_file_pos;
8016 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
8018 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8019 incorrect. We do not yet know the size of the .symtab section.
8020 We correct next_file_pos below, after we do know the size. */
8022 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8023 continuously seeking to the right position in the file. */
8024 if (! info->keep_memory || max_sym_count < 20)
8025 finfo.symbuf_size = 20;
8027 finfo.symbuf_size = max_sym_count;
8028 amt = finfo.symbuf_size;
8029 amt *= bed->s->sizeof_sym;
8030 finfo.symbuf = bfd_malloc (amt);
8031 if (finfo.symbuf == NULL)
8033 if (elf_numsections (abfd) > SHN_LORESERVE)
8035 /* Wild guess at number of output symbols. realloc'd as needed. */
8036 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
8037 finfo.shndxbuf_size = amt;
8038 amt *= sizeof (Elf_External_Sym_Shndx);
8039 finfo.symshndxbuf = bfd_zmalloc (amt);
8040 if (finfo.symshndxbuf == NULL)
8044 /* Start writing out the symbol table. The first symbol is always a
8046 if (info->strip != strip_all
8049 elfsym.st_value = 0;
8052 elfsym.st_other = 0;
8053 elfsym.st_shndx = SHN_UNDEF;
8054 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
8059 /* Output a symbol for each section. We output these even if we are
8060 discarding local symbols, since they are used for relocs. These
8061 symbols have no names. We store the index of each one in the
8062 index field of the section, so that we can find it again when
8063 outputting relocs. */
8064 if (info->strip != strip_all
8068 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8069 elfsym.st_other = 0;
8070 for (i = 1; i < elf_numsections (abfd); i++)
8072 o = bfd_section_from_elf_index (abfd, i);
8074 o->target_index = bfd_get_symcount (abfd);
8075 elfsym.st_shndx = i;
8076 if (info->relocatable || o == NULL)
8077 elfsym.st_value = 0;
8079 elfsym.st_value = o->vma;
8080 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
8082 if (i == SHN_LORESERVE - 1)
8083 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
8087 /* Allocate some memory to hold information read in from the input
8089 if (max_contents_size != 0)
8091 finfo.contents = bfd_malloc (max_contents_size);
8092 if (finfo.contents == NULL)
8096 if (max_external_reloc_size != 0)
8098 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
8099 if (finfo.external_relocs == NULL)
8103 if (max_internal_reloc_count != 0)
8105 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
8106 amt *= sizeof (Elf_Internal_Rela);
8107 finfo.internal_relocs = bfd_malloc (amt);
8108 if (finfo.internal_relocs == NULL)
8112 if (max_sym_count != 0)
8114 amt = max_sym_count * bed->s->sizeof_sym;
8115 finfo.external_syms = bfd_malloc (amt);
8116 if (finfo.external_syms == NULL)
8119 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8120 finfo.internal_syms = bfd_malloc (amt);
8121 if (finfo.internal_syms == NULL)
8124 amt = max_sym_count * sizeof (long);
8125 finfo.indices = bfd_malloc (amt);
8126 if (finfo.indices == NULL)
8129 amt = max_sym_count * sizeof (asection *);
8130 finfo.sections = bfd_malloc (amt);
8131 if (finfo.sections == NULL)
8135 if (max_sym_shndx_count != 0)
8137 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8138 finfo.locsym_shndx = bfd_malloc (amt);
8139 if (finfo.locsym_shndx == NULL)
8143 if (elf_hash_table (info)->tls_sec)
8145 bfd_vma base, end = 0;
8148 for (sec = elf_hash_table (info)->tls_sec;
8149 sec && (sec->flags & SEC_THREAD_LOCAL);
8152 bfd_vma size = sec->size;
8154 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
8156 struct bfd_link_order *o;
8158 for (o = sec->map_head.link_order; o != NULL; o = o->next)
8159 if (size < o->offset + o->size)
8160 size = o->offset + o->size;
8162 end = sec->vma + size;
8164 base = elf_hash_table (info)->tls_sec->vma;
8165 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
8166 elf_hash_table (info)->tls_size = end - base;
8169 /* Reorder SHF_LINK_ORDER sections. */
8170 for (o = abfd->sections; o != NULL; o = o->next)
8172 if (!elf_fixup_link_order (abfd, o))
8176 /* Since ELF permits relocations to be against local symbols, we
8177 must have the local symbols available when we do the relocations.
8178 Since we would rather only read the local symbols once, and we
8179 would rather not keep them in memory, we handle all the
8180 relocations for a single input file at the same time.
8182 Unfortunately, there is no way to know the total number of local
8183 symbols until we have seen all of them, and the local symbol
8184 indices precede the global symbol indices. This means that when
8185 we are generating relocatable output, and we see a reloc against
8186 a global symbol, we can not know the symbol index until we have
8187 finished examining all the local symbols to see which ones we are
8188 going to output. To deal with this, we keep the relocations in
8189 memory, and don't output them until the end of the link. This is
8190 an unfortunate waste of memory, but I don't see a good way around
8191 it. Fortunately, it only happens when performing a relocatable
8192 link, which is not the common case. FIXME: If keep_memory is set
8193 we could write the relocs out and then read them again; I don't
8194 know how bad the memory loss will be. */
8196 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8197 sub->output_has_begun = FALSE;
8198 for (o = abfd->sections; o != NULL; o = o->next)
8200 for (p = o->map_head.link_order; p != NULL; p = p->next)
8202 if (p->type == bfd_indirect_link_order
8203 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
8204 == bfd_target_elf_flavour)
8205 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
8207 if (! sub->output_has_begun)
8209 if (! elf_link_input_bfd (&finfo, sub))
8211 sub->output_has_begun = TRUE;
8214 else if (p->type == bfd_section_reloc_link_order
8215 || p->type == bfd_symbol_reloc_link_order)
8217 if (! elf_reloc_link_order (abfd, info, o, p))
8222 if (! _bfd_default_link_order (abfd, info, o, p))
8228 /* Output any global symbols that got converted to local in a
8229 version script or due to symbol visibility. We do this in a
8230 separate step since ELF requires all local symbols to appear
8231 prior to any global symbols. FIXME: We should only do this if
8232 some global symbols were, in fact, converted to become local.
8233 FIXME: Will this work correctly with the Irix 5 linker? */
8234 eoinfo.failed = FALSE;
8235 eoinfo.finfo = &finfo;
8236 eoinfo.localsyms = TRUE;
8237 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8242 /* That wrote out all the local symbols. Finish up the symbol table
8243 with the global symbols. Even if we want to strip everything we
8244 can, we still need to deal with those global symbols that got
8245 converted to local in a version script. */
8247 /* The sh_info field records the index of the first non local symbol. */
8248 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8251 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8253 Elf_Internal_Sym sym;
8254 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8255 long last_local = 0;
8257 /* Write out the section symbols for the output sections. */
8258 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
8264 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8267 for (s = abfd->sections; s != NULL; s = s->next)
8273 dynindx = elf_section_data (s)->dynindx;
8276 indx = elf_section_data (s)->this_idx;
8277 BFD_ASSERT (indx > 0);
8278 sym.st_shndx = indx;
8279 sym.st_value = s->vma;
8280 dest = dynsym + dynindx * bed->s->sizeof_sym;
8281 if (last_local < dynindx)
8282 last_local = dynindx;
8283 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8287 /* Write out the local dynsyms. */
8288 if (elf_hash_table (info)->dynlocal)
8290 struct elf_link_local_dynamic_entry *e;
8291 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8296 sym.st_size = e->isym.st_size;
8297 sym.st_other = e->isym.st_other;
8299 /* Copy the internal symbol as is.
8300 Note that we saved a word of storage and overwrote
8301 the original st_name with the dynstr_index. */
8304 if (e->isym.st_shndx != SHN_UNDEF
8305 && (e->isym.st_shndx < SHN_LORESERVE
8306 || e->isym.st_shndx > SHN_HIRESERVE))
8308 s = bfd_section_from_elf_index (e->input_bfd,
8312 elf_section_data (s->output_section)->this_idx;
8313 sym.st_value = (s->output_section->vma
8315 + e->isym.st_value);
8318 if (last_local < e->dynindx)
8319 last_local = e->dynindx;
8321 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8322 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8326 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8330 /* We get the global symbols from the hash table. */
8331 eoinfo.failed = FALSE;
8332 eoinfo.localsyms = FALSE;
8333 eoinfo.finfo = &finfo;
8334 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8339 /* If backend needs to output some symbols not present in the hash
8340 table, do it now. */
8341 if (bed->elf_backend_output_arch_syms)
8343 typedef bfd_boolean (*out_sym_func)
8344 (void *, const char *, Elf_Internal_Sym *, asection *,
8345 struct elf_link_hash_entry *);
8347 if (! ((*bed->elf_backend_output_arch_syms)
8348 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8352 /* Flush all symbols to the file. */
8353 if (! elf_link_flush_output_syms (&finfo, bed))
8356 /* Now we know the size of the symtab section. */
8357 off += symtab_hdr->sh_size;
8359 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8360 if (symtab_shndx_hdr->sh_name != 0)
8362 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8363 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8364 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8365 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8366 symtab_shndx_hdr->sh_size = amt;
8368 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8371 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8372 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8377 /* Finish up and write out the symbol string table (.strtab)
8379 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8380 /* sh_name was set in prep_headers. */
8381 symstrtab_hdr->sh_type = SHT_STRTAB;
8382 symstrtab_hdr->sh_flags = 0;
8383 symstrtab_hdr->sh_addr = 0;
8384 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8385 symstrtab_hdr->sh_entsize = 0;
8386 symstrtab_hdr->sh_link = 0;
8387 symstrtab_hdr->sh_info = 0;
8388 /* sh_offset is set just below. */
8389 symstrtab_hdr->sh_addralign = 1;
8391 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8392 elf_tdata (abfd)->next_file_pos = off;
8394 if (bfd_get_symcount (abfd) > 0)
8396 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8397 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8401 /* Adjust the relocs to have the correct symbol indices. */
8402 for (o = abfd->sections; o != NULL; o = o->next)
8404 if ((o->flags & SEC_RELOC) == 0)
8407 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8408 elf_section_data (o)->rel_count,
8409 elf_section_data (o)->rel_hashes);
8410 if (elf_section_data (o)->rel_hdr2 != NULL)
8411 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8412 elf_section_data (o)->rel_count2,
8413 (elf_section_data (o)->rel_hashes
8414 + elf_section_data (o)->rel_count));
8416 /* Set the reloc_count field to 0 to prevent write_relocs from
8417 trying to swap the relocs out itself. */
8421 if (dynamic && info->combreloc && dynobj != NULL)
8422 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8424 /* If we are linking against a dynamic object, or generating a
8425 shared library, finish up the dynamic linking information. */
8428 bfd_byte *dyncon, *dynconend;
8430 /* Fix up .dynamic entries. */
8431 o = bfd_get_section_by_name (dynobj, ".dynamic");
8432 BFD_ASSERT (o != NULL);
8434 dyncon = o->contents;
8435 dynconend = o->contents + o->size;
8436 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8438 Elf_Internal_Dyn dyn;
8442 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8449 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8451 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8453 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8454 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8457 dyn.d_un.d_val = relativecount;
8464 name = info->init_function;
8467 name = info->fini_function;
8470 struct elf_link_hash_entry *h;
8472 h = elf_link_hash_lookup (elf_hash_table (info), name,
8473 FALSE, FALSE, TRUE);
8475 && (h->root.type == bfd_link_hash_defined
8476 || h->root.type == bfd_link_hash_defweak))
8478 dyn.d_un.d_val = h->root.u.def.value;
8479 o = h->root.u.def.section;
8480 if (o->output_section != NULL)
8481 dyn.d_un.d_val += (o->output_section->vma
8482 + o->output_offset);
8485 /* The symbol is imported from another shared
8486 library and does not apply to this one. */
8494 case DT_PREINIT_ARRAYSZ:
8495 name = ".preinit_array";
8497 case DT_INIT_ARRAYSZ:
8498 name = ".init_array";
8500 case DT_FINI_ARRAYSZ:
8501 name = ".fini_array";
8503 o = bfd_get_section_by_name (abfd, name);
8506 (*_bfd_error_handler)
8507 (_("%B: could not find output section %s"), abfd, name);
8511 (*_bfd_error_handler)
8512 (_("warning: %s section has zero size"), name);
8513 dyn.d_un.d_val = o->size;
8516 case DT_PREINIT_ARRAY:
8517 name = ".preinit_array";
8520 name = ".init_array";
8523 name = ".fini_array";
8536 name = ".gnu.version_d";
8539 name = ".gnu.version_r";
8542 name = ".gnu.version";
8544 o = bfd_get_section_by_name (abfd, name);
8547 (*_bfd_error_handler)
8548 (_("%B: could not find output section %s"), abfd, name);
8551 dyn.d_un.d_ptr = o->vma;
8558 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8563 for (i = 1; i < elf_numsections (abfd); i++)
8565 Elf_Internal_Shdr *hdr;
8567 hdr = elf_elfsections (abfd)[i];
8568 if (hdr->sh_type == type
8569 && (hdr->sh_flags & SHF_ALLOC) != 0)
8571 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8572 dyn.d_un.d_val += hdr->sh_size;
8575 if (dyn.d_un.d_val == 0
8576 || hdr->sh_addr < dyn.d_un.d_val)
8577 dyn.d_un.d_val = hdr->sh_addr;
8583 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8587 /* If we have created any dynamic sections, then output them. */
8590 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8593 for (o = dynobj->sections; o != NULL; o = o->next)
8595 if ((o->flags & SEC_HAS_CONTENTS) == 0
8597 || o->output_section == bfd_abs_section_ptr)
8599 if ((o->flags & SEC_LINKER_CREATED) == 0)
8601 /* At this point, we are only interested in sections
8602 created by _bfd_elf_link_create_dynamic_sections. */
8605 if (elf_hash_table (info)->stab_info.stabstr == o)
8607 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8609 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8611 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8613 if (! bfd_set_section_contents (abfd, o->output_section,
8615 (file_ptr) o->output_offset,
8621 /* The contents of the .dynstr section are actually in a
8623 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8624 if (bfd_seek (abfd, off, SEEK_SET) != 0
8625 || ! _bfd_elf_strtab_emit (abfd,
8626 elf_hash_table (info)->dynstr))
8632 if (info->relocatable)
8634 bfd_boolean failed = FALSE;
8636 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8641 /* If we have optimized stabs strings, output them. */
8642 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8644 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8648 if (info->eh_frame_hdr)
8650 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8654 if (finfo.symstrtab != NULL)
8655 _bfd_stringtab_free (finfo.symstrtab);
8656 if (finfo.contents != NULL)
8657 free (finfo.contents);
8658 if (finfo.external_relocs != NULL)
8659 free (finfo.external_relocs);
8660 if (finfo.internal_relocs != NULL)
8661 free (finfo.internal_relocs);
8662 if (finfo.external_syms != NULL)
8663 free (finfo.external_syms);
8664 if (finfo.locsym_shndx != NULL)
8665 free (finfo.locsym_shndx);
8666 if (finfo.internal_syms != NULL)
8667 free (finfo.internal_syms);
8668 if (finfo.indices != NULL)
8669 free (finfo.indices);
8670 if (finfo.sections != NULL)
8671 free (finfo.sections);
8672 if (finfo.symbuf != NULL)
8673 free (finfo.symbuf);
8674 if (finfo.symshndxbuf != NULL)
8675 free (finfo.symshndxbuf);
8676 for (o = abfd->sections; o != NULL; o = o->next)
8678 if ((o->flags & SEC_RELOC) != 0
8679 && elf_section_data (o)->rel_hashes != NULL)
8680 free (elf_section_data (o)->rel_hashes);
8683 elf_tdata (abfd)->linker = TRUE;
8688 if (finfo.symstrtab != NULL)
8689 _bfd_stringtab_free (finfo.symstrtab);
8690 if (finfo.contents != NULL)
8691 free (finfo.contents);
8692 if (finfo.external_relocs != NULL)
8693 free (finfo.external_relocs);
8694 if (finfo.internal_relocs != NULL)
8695 free (finfo.internal_relocs);
8696 if (finfo.external_syms != NULL)
8697 free (finfo.external_syms);
8698 if (finfo.locsym_shndx != NULL)
8699 free (finfo.locsym_shndx);
8700 if (finfo.internal_syms != NULL)
8701 free (finfo.internal_syms);
8702 if (finfo.indices != NULL)
8703 free (finfo.indices);
8704 if (finfo.sections != NULL)
8705 free (finfo.sections);
8706 if (finfo.symbuf != NULL)
8707 free (finfo.symbuf);
8708 if (finfo.symshndxbuf != NULL)
8709 free (finfo.symshndxbuf);
8710 for (o = abfd->sections; o != NULL; o = o->next)
8712 if ((o->flags & SEC_RELOC) != 0
8713 && elf_section_data (o)->rel_hashes != NULL)
8714 free (elf_section_data (o)->rel_hashes);
8720 /* Garbage collect unused sections. */
8722 /* The mark phase of garbage collection. For a given section, mark
8723 it and any sections in this section's group, and all the sections
8724 which define symbols to which it refers. */
8726 typedef asection * (*gc_mark_hook_fn)
8727 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8728 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8731 _bfd_elf_gc_mark (struct bfd_link_info *info,
8733 gc_mark_hook_fn gc_mark_hook)
8737 asection *group_sec;
8741 /* Mark all the sections in the group. */
8742 group_sec = elf_section_data (sec)->next_in_group;
8743 if (group_sec && !group_sec->gc_mark)
8744 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8747 /* Look through the section relocs. */
8749 is_eh = strcmp (sec->name, ".eh_frame") == 0;
8750 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8752 Elf_Internal_Rela *relstart, *rel, *relend;
8753 Elf_Internal_Shdr *symtab_hdr;
8754 struct elf_link_hash_entry **sym_hashes;
8757 bfd *input_bfd = sec->owner;
8758 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8759 Elf_Internal_Sym *isym = NULL;
8762 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8763 sym_hashes = elf_sym_hashes (input_bfd);
8765 /* Read the local symbols. */
8766 if (elf_bad_symtab (input_bfd))
8768 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8772 extsymoff = nlocsyms = symtab_hdr->sh_info;
8774 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8775 if (isym == NULL && nlocsyms != 0)
8777 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8783 /* Read the relocations. */
8784 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8786 if (relstart == NULL)
8791 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8793 if (bed->s->arch_size == 32)
8798 for (rel = relstart; rel < relend; rel++)
8800 unsigned long r_symndx;
8802 struct elf_link_hash_entry *h;
8804 r_symndx = rel->r_info >> r_sym_shift;
8808 if (r_symndx >= nlocsyms
8809 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8811 h = sym_hashes[r_symndx - extsymoff];
8812 while (h->root.type == bfd_link_hash_indirect
8813 || h->root.type == bfd_link_hash_warning)
8814 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8815 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8819 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8822 if (rsec && !rsec->gc_mark)
8824 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8827 rsec->gc_mark_from_eh = 1;
8828 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8837 if (elf_section_data (sec)->relocs != relstart)
8840 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8842 if (! info->keep_memory)
8845 symtab_hdr->contents = (unsigned char *) isym;
8852 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8855 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8859 if (h->root.type == bfd_link_hash_warning)
8860 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8862 if (h->dynindx != -1
8863 && ((h->root.type != bfd_link_hash_defined
8864 && h->root.type != bfd_link_hash_defweak)
8865 || h->root.u.def.section->gc_mark))
8866 h->dynindx = (*idx)++;
8871 /* The sweep phase of garbage collection. Remove all garbage sections. */
8873 typedef bfd_boolean (*gc_sweep_hook_fn)
8874 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8877 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8881 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8885 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8888 for (o = sub->sections; o != NULL; o = o->next)
8890 /* Keep debug and special sections. */
8891 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8892 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8898 /* Keep .gcc_except_table.* if the associated .text.* is
8899 marked. This isn't very nice, but the proper solution,
8900 splitting .eh_frame up and using comdat doesn't pan out
8901 easily due to needing special relocs to handle the
8902 difference of two symbols in separate sections.
8903 Don't keep code sections referenced by .eh_frame. */
8904 if (o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
8906 if (strncmp (o->name, ".gcc_except_table.", 18) == 0)
8912 len = strlen (o->name + 18) + 1;
8913 fn_name = bfd_malloc (len + 6);
8914 if (fn_name == NULL)
8916 memcpy (fn_name, ".text.", 6);
8917 memcpy (fn_name + 6, o->name + 18, len);
8918 fn_text = bfd_get_section_by_name (sub, fn_name);
8920 if (fn_text != NULL && fn_text->gc_mark)
8924 /* If not using specially named exception table section,
8925 then keep whatever we are using. */
8933 /* Skip sweeping sections already excluded. */
8934 if (o->flags & SEC_EXCLUDE)
8937 /* Since this is early in the link process, it is simple
8938 to remove a section from the output. */
8939 o->flags |= SEC_EXCLUDE;
8941 /* But we also have to update some of the relocation
8942 info we collected before. */
8944 && (o->flags & SEC_RELOC) != 0
8945 && o->reloc_count > 0
8946 && !bfd_is_abs_section (o->output_section))
8948 Elf_Internal_Rela *internal_relocs;
8952 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8954 if (internal_relocs == NULL)
8957 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8959 if (elf_section_data (o)->relocs != internal_relocs)
8960 free (internal_relocs);
8968 /* Remove the symbols that were in the swept sections from the dynamic
8969 symbol table. GCFIXME: Anyone know how to get them out of the
8970 static symbol table as well? */
8974 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8976 elf_hash_table (info)->dynsymcount = i;
8982 /* Propagate collected vtable information. This is called through
8983 elf_link_hash_traverse. */
8986 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8988 if (h->root.type == bfd_link_hash_warning)
8989 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8991 /* Those that are not vtables. */
8992 if (h->vtable == NULL || h->vtable->parent == NULL)
8995 /* Those vtables that do not have parents, we cannot merge. */
8996 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
8999 /* If we've already been done, exit. */
9000 if (h->vtable->used && h->vtable->used[-1])
9003 /* Make sure the parent's table is up to date. */
9004 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
9006 if (h->vtable->used == NULL)
9008 /* None of this table's entries were referenced. Re-use the
9010 h->vtable->used = h->vtable->parent->vtable->used;
9011 h->vtable->size = h->vtable->parent->vtable->size;
9016 bfd_boolean *cu, *pu;
9018 /* Or the parent's entries into ours. */
9019 cu = h->vtable->used;
9021 pu = h->vtable->parent->vtable->used;
9024 const struct elf_backend_data *bed;
9025 unsigned int log_file_align;
9027 bed = get_elf_backend_data (h->root.u.def.section->owner);
9028 log_file_align = bed->s->log_file_align;
9029 n = h->vtable->parent->vtable->size >> log_file_align;
9044 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
9047 bfd_vma hstart, hend;
9048 Elf_Internal_Rela *relstart, *relend, *rel;
9049 const struct elf_backend_data *bed;
9050 unsigned int log_file_align;
9052 if (h->root.type == bfd_link_hash_warning)
9053 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9055 /* Take care of both those symbols that do not describe vtables as
9056 well as those that are not loaded. */
9057 if (h->vtable == NULL || h->vtable->parent == NULL)
9060 BFD_ASSERT (h->root.type == bfd_link_hash_defined
9061 || h->root.type == bfd_link_hash_defweak);
9063 sec = h->root.u.def.section;
9064 hstart = h->root.u.def.value;
9065 hend = hstart + h->size;
9067 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
9069 return *(bfd_boolean *) okp = FALSE;
9070 bed = get_elf_backend_data (sec->owner);
9071 log_file_align = bed->s->log_file_align;
9073 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
9075 for (rel = relstart; rel < relend; ++rel)
9076 if (rel->r_offset >= hstart && rel->r_offset < hend)
9078 /* If the entry is in use, do nothing. */
9080 && (rel->r_offset - hstart) < h->vtable->size)
9082 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
9083 if (h->vtable->used[entry])
9086 /* Otherwise, kill it. */
9087 rel->r_offset = rel->r_info = rel->r_addend = 0;
9093 /* Mark sections containing dynamically referenced symbols. This is called
9094 through elf_link_hash_traverse. */
9097 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h,
9098 void *okp ATTRIBUTE_UNUSED)
9100 if (h->root.type == bfd_link_hash_warning)
9101 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9103 if ((h->root.type == bfd_link_hash_defined
9104 || h->root.type == bfd_link_hash_defweak)
9106 h->root.u.def.section->flags |= SEC_KEEP;
9111 /* Do mark and sweep of unused sections. */
9114 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
9116 bfd_boolean ok = TRUE;
9118 asection * (*gc_mark_hook)
9119 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
9120 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
9122 if (!get_elf_backend_data (abfd)->can_gc_sections
9123 || info->relocatable
9124 || info->emitrelocations
9126 || !is_elf_hash_table (info->hash))
9128 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
9132 /* Apply transitive closure to the vtable entry usage info. */
9133 elf_link_hash_traverse (elf_hash_table (info),
9134 elf_gc_propagate_vtable_entries_used,
9139 /* Kill the vtable relocations that were not used. */
9140 elf_link_hash_traverse (elf_hash_table (info),
9141 elf_gc_smash_unused_vtentry_relocs,
9146 /* Mark dynamically referenced symbols. */
9147 if (elf_hash_table (info)->dynamic_sections_created)
9148 elf_link_hash_traverse (elf_hash_table (info),
9149 elf_gc_mark_dynamic_ref_symbol,
9154 /* Grovel through relocs to find out who stays ... */
9155 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
9156 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9160 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9163 for (o = sub->sections; o != NULL; o = o->next)
9164 if ((o->flags & SEC_KEEP) != 0 && !o->gc_mark)
9165 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9169 /* ... and mark SEC_EXCLUDE for those that go. */
9170 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
9176 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9179 bfd_elf_gc_record_vtinherit (bfd *abfd,
9181 struct elf_link_hash_entry *h,
9184 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
9185 struct elf_link_hash_entry **search, *child;
9186 bfd_size_type extsymcount;
9187 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9189 /* The sh_info field of the symtab header tells us where the
9190 external symbols start. We don't care about the local symbols at
9192 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
9193 if (!elf_bad_symtab (abfd))
9194 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
9196 sym_hashes = elf_sym_hashes (abfd);
9197 sym_hashes_end = sym_hashes + extsymcount;
9199 /* Hunt down the child symbol, which is in this section at the same
9200 offset as the relocation. */
9201 for (search = sym_hashes; search != sym_hashes_end; ++search)
9203 if ((child = *search) != NULL
9204 && (child->root.type == bfd_link_hash_defined
9205 || child->root.type == bfd_link_hash_defweak)
9206 && child->root.u.def.section == sec
9207 && child->root.u.def.value == offset)
9211 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
9212 abfd, sec, (unsigned long) offset);
9213 bfd_set_error (bfd_error_invalid_operation);
9219 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
9225 /* This *should* only be the absolute section. It could potentially
9226 be that someone has defined a non-global vtable though, which
9227 would be bad. It isn't worth paging in the local symbols to be
9228 sure though; that case should simply be handled by the assembler. */
9230 child->vtable->parent = (struct elf_link_hash_entry *) -1;
9233 child->vtable->parent = h;
9238 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9241 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
9242 asection *sec ATTRIBUTE_UNUSED,
9243 struct elf_link_hash_entry *h,
9246 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9247 unsigned int log_file_align = bed->s->log_file_align;
9251 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
9256 if (addend >= h->vtable->size)
9258 size_t size, bytes, file_align;
9259 bfd_boolean *ptr = h->vtable->used;
9261 /* While the symbol is undefined, we have to be prepared to handle
9263 file_align = 1 << log_file_align;
9264 if (h->root.type == bfd_link_hash_undefined)
9265 size = addend + file_align;
9271 /* Oops! We've got a reference past the defined end of
9272 the table. This is probably a bug -- shall we warn? */
9273 size = addend + file_align;
9276 size = (size + file_align - 1) & -file_align;
9278 /* Allocate one extra entry for use as a "done" flag for the
9279 consolidation pass. */
9280 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9284 ptr = bfd_realloc (ptr - 1, bytes);
9290 oldbytes = (((h->vtable->size >> log_file_align) + 1)
9291 * sizeof (bfd_boolean));
9292 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9296 ptr = bfd_zmalloc (bytes);
9301 /* And arrange for that done flag to be at index -1. */
9302 h->vtable->used = ptr + 1;
9303 h->vtable->size = size;
9306 h->vtable->used[addend >> log_file_align] = TRUE;
9311 struct alloc_got_off_arg {
9313 unsigned int got_elt_size;
9316 /* We need a special top-level link routine to convert got reference counts
9317 to real got offsets. */
9320 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9322 struct alloc_got_off_arg *gofarg = arg;
9324 if (h->root.type == bfd_link_hash_warning)
9325 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9327 if (h->got.refcount > 0)
9329 h->got.offset = gofarg->gotoff;
9330 gofarg->gotoff += gofarg->got_elt_size;
9333 h->got.offset = (bfd_vma) -1;
9338 /* And an accompanying bit to work out final got entry offsets once
9339 we're done. Should be called from final_link. */
9342 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9343 struct bfd_link_info *info)
9346 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9348 unsigned int got_elt_size = bed->s->arch_size / 8;
9349 struct alloc_got_off_arg gofarg;
9351 if (! is_elf_hash_table (info->hash))
9354 /* The GOT offset is relative to the .got section, but the GOT header is
9355 put into the .got.plt section, if the backend uses it. */
9356 if (bed->want_got_plt)
9359 gotoff = bed->got_header_size;
9361 /* Do the local .got entries first. */
9362 for (i = info->input_bfds; i; i = i->link_next)
9364 bfd_signed_vma *local_got;
9365 bfd_size_type j, locsymcount;
9366 Elf_Internal_Shdr *symtab_hdr;
9368 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9371 local_got = elf_local_got_refcounts (i);
9375 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9376 if (elf_bad_symtab (i))
9377 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9379 locsymcount = symtab_hdr->sh_info;
9381 for (j = 0; j < locsymcount; ++j)
9383 if (local_got[j] > 0)
9385 local_got[j] = gotoff;
9386 gotoff += got_elt_size;
9389 local_got[j] = (bfd_vma) -1;
9393 /* Then the global .got entries. .plt refcounts are handled by
9394 adjust_dynamic_symbol */
9395 gofarg.gotoff = gotoff;
9396 gofarg.got_elt_size = got_elt_size;
9397 elf_link_hash_traverse (elf_hash_table (info),
9398 elf_gc_allocate_got_offsets,
9403 /* Many folk need no more in the way of final link than this, once
9404 got entry reference counting is enabled. */
9407 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9409 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9412 /* Invoke the regular ELF backend linker to do all the work. */
9413 return bfd_elf_final_link (abfd, info);
9417 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9419 struct elf_reloc_cookie *rcookie = cookie;
9421 if (rcookie->bad_symtab)
9422 rcookie->rel = rcookie->rels;
9424 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9426 unsigned long r_symndx;
9428 if (! rcookie->bad_symtab)
9429 if (rcookie->rel->r_offset > offset)
9431 if (rcookie->rel->r_offset != offset)
9434 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9435 if (r_symndx == SHN_UNDEF)
9438 if (r_symndx >= rcookie->locsymcount
9439 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9441 struct elf_link_hash_entry *h;
9443 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9445 while (h->root.type == bfd_link_hash_indirect
9446 || h->root.type == bfd_link_hash_warning)
9447 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9449 if ((h->root.type == bfd_link_hash_defined
9450 || h->root.type == bfd_link_hash_defweak)
9451 && elf_discarded_section (h->root.u.def.section))
9458 /* It's not a relocation against a global symbol,
9459 but it could be a relocation against a local
9460 symbol for a discarded section. */
9462 Elf_Internal_Sym *isym;
9464 /* Need to: get the symbol; get the section. */
9465 isym = &rcookie->locsyms[r_symndx];
9466 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9468 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9469 if (isec != NULL && elf_discarded_section (isec))
9478 /* Discard unneeded references to discarded sections.
9479 Returns TRUE if any section's size was changed. */
9480 /* This function assumes that the relocations are in sorted order,
9481 which is true for all known assemblers. */
9484 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9486 struct elf_reloc_cookie cookie;
9487 asection *stab, *eh;
9488 Elf_Internal_Shdr *symtab_hdr;
9489 const struct elf_backend_data *bed;
9492 bfd_boolean ret = FALSE;
9494 if (info->traditional_format
9495 || !is_elf_hash_table (info->hash))
9498 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9500 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9503 bed = get_elf_backend_data (abfd);
9505 if ((abfd->flags & DYNAMIC) != 0)
9508 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9509 if (info->relocatable
9512 || bfd_is_abs_section (eh->output_section))))
9515 stab = bfd_get_section_by_name (abfd, ".stab");
9518 || bfd_is_abs_section (stab->output_section)
9519 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9524 && bed->elf_backend_discard_info == NULL)
9527 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9529 cookie.sym_hashes = elf_sym_hashes (abfd);
9530 cookie.bad_symtab = elf_bad_symtab (abfd);
9531 if (cookie.bad_symtab)
9533 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9534 cookie.extsymoff = 0;
9538 cookie.locsymcount = symtab_hdr->sh_info;
9539 cookie.extsymoff = symtab_hdr->sh_info;
9542 if (bed->s->arch_size == 32)
9543 cookie.r_sym_shift = 8;
9545 cookie.r_sym_shift = 32;
9547 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9548 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9550 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9551 cookie.locsymcount, 0,
9553 if (cookie.locsyms == NULL)
9560 count = stab->reloc_count;
9562 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9564 if (cookie.rels != NULL)
9566 cookie.rel = cookie.rels;
9567 cookie.relend = cookie.rels;
9568 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9569 if (_bfd_discard_section_stabs (abfd, stab,
9570 elf_section_data (stab)->sec_info,
9571 bfd_elf_reloc_symbol_deleted_p,
9574 if (elf_section_data (stab)->relocs != cookie.rels)
9582 count = eh->reloc_count;
9584 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9586 cookie.rel = cookie.rels;
9587 cookie.relend = cookie.rels;
9588 if (cookie.rels != NULL)
9589 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9591 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9592 bfd_elf_reloc_symbol_deleted_p,
9596 if (cookie.rels != NULL
9597 && elf_section_data (eh)->relocs != cookie.rels)
9601 if (bed->elf_backend_discard_info != NULL
9602 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9605 if (cookie.locsyms != NULL
9606 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9608 if (! info->keep_memory)
9609 free (cookie.locsyms);
9611 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9615 if (info->eh_frame_hdr
9616 && !info->relocatable
9617 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9624 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9627 const char *name, *p;
9628 struct bfd_section_already_linked *l;
9629 struct bfd_section_already_linked_hash_entry *already_linked_list;
9632 /* A single member comdat group section may be discarded by a
9633 linkonce section. See below. */
9634 if (sec->output_section == bfd_abs_section_ptr)
9639 /* Check if it belongs to a section group. */
9640 group = elf_sec_group (sec);
9642 /* Return if it isn't a linkonce section nor a member of a group. A
9643 comdat group section also has SEC_LINK_ONCE set. */
9644 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9649 /* If this is the member of a single member comdat group, check if
9650 the group should be discarded. */
9651 if (elf_next_in_group (sec) == sec
9652 && (group->flags & SEC_LINK_ONCE) != 0)
9658 /* FIXME: When doing a relocatable link, we may have trouble
9659 copying relocations in other sections that refer to local symbols
9660 in the section being discarded. Those relocations will have to
9661 be converted somehow; as of this writing I'm not sure that any of
9662 the backends handle that correctly.
9664 It is tempting to instead not discard link once sections when
9665 doing a relocatable link (technically, they should be discarded
9666 whenever we are building constructors). However, that fails,
9667 because the linker winds up combining all the link once sections
9668 into a single large link once section, which defeats the purpose
9669 of having link once sections in the first place.
9671 Also, not merging link once sections in a relocatable link
9672 causes trouble for MIPS ELF, which relies on link once semantics
9673 to handle the .reginfo section correctly. */
9675 name = bfd_get_section_name (abfd, sec);
9677 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9678 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
9683 already_linked_list = bfd_section_already_linked_table_lookup (p);
9685 for (l = already_linked_list->entry; l != NULL; l = l->next)
9687 /* We may have 3 different sections on the list: group section,
9688 comdat section and linkonce section. SEC may be a linkonce or
9689 group section. We match a group section with a group section,
9690 a linkonce section with a linkonce section, and ignore comdat
9692 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9693 && strcmp (name, l->sec->name) == 0
9694 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9696 /* The section has already been linked. See if we should
9698 switch (flags & SEC_LINK_DUPLICATES)
9703 case SEC_LINK_DUPLICATES_DISCARD:
9706 case SEC_LINK_DUPLICATES_ONE_ONLY:
9707 (*_bfd_error_handler)
9708 (_("%B: ignoring duplicate section `%A'"),
9712 case SEC_LINK_DUPLICATES_SAME_SIZE:
9713 if (sec->size != l->sec->size)
9714 (*_bfd_error_handler)
9715 (_("%B: duplicate section `%A' has different size"),
9719 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9720 if (sec->size != l->sec->size)
9721 (*_bfd_error_handler)
9722 (_("%B: duplicate section `%A' has different size"),
9724 else if (sec->size != 0)
9726 bfd_byte *sec_contents, *l_sec_contents;
9728 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9729 (*_bfd_error_handler)
9730 (_("%B: warning: could not read contents of section `%A'"),
9732 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9734 (*_bfd_error_handler)
9735 (_("%B: warning: could not read contents of section `%A'"),
9736 l->sec->owner, l->sec);
9737 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9738 (*_bfd_error_handler)
9739 (_("%B: warning: duplicate section `%A' has different contents"),
9743 free (sec_contents);
9745 free (l_sec_contents);
9750 /* Set the output_section field so that lang_add_section
9751 does not create a lang_input_section structure for this
9752 section. Since there might be a symbol in the section
9753 being discarded, we must retain a pointer to the section
9754 which we are really going to use. */
9755 sec->output_section = bfd_abs_section_ptr;
9756 sec->kept_section = l->sec;
9758 if (flags & SEC_GROUP)
9760 asection *first = elf_next_in_group (sec);
9761 asection *s = first;
9765 s->output_section = bfd_abs_section_ptr;
9766 /* Record which group discards it. */
9767 s->kept_section = l->sec;
9768 s = elf_next_in_group (s);
9769 /* These lists are circular. */
9781 /* If this is the member of a single member comdat group and the
9782 group hasn't be discarded, we check if it matches a linkonce
9783 section. We only record the discarded comdat group. Otherwise
9784 the undiscarded group will be discarded incorrectly later since
9785 itself has been recorded. */
9786 for (l = already_linked_list->entry; l != NULL; l = l->next)
9787 if ((l->sec->flags & SEC_GROUP) == 0
9788 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9789 && bfd_elf_match_symbols_in_sections (l->sec,
9790 elf_next_in_group (sec)))
9792 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
9793 elf_next_in_group (sec)->kept_section = l->sec;
9794 group->output_section = bfd_abs_section_ptr;
9801 /* There is no direct match. But for linkonce section, we should
9802 check if there is a match with comdat group member. We always
9803 record the linkonce section, discarded or not. */
9804 for (l = already_linked_list->entry; l != NULL; l = l->next)
9805 if (l->sec->flags & SEC_GROUP)
9807 asection *first = elf_next_in_group (l->sec);
9810 && elf_next_in_group (first) == first
9811 && bfd_elf_match_symbols_in_sections (first, sec))
9813 sec->output_section = bfd_abs_section_ptr;
9814 sec->kept_section = l->sec;
9819 /* This is the first section with this name. Record it. */
9820 bfd_section_already_linked_table_insert (already_linked_list, sec);
9824 bfd_elf_set_symbol (struct elf_link_hash_entry *h, bfd_vma val,
9825 struct bfd_section *s)
9827 h->root.type = bfd_link_hash_defined;
9828 h->root.u.def.section = s ? s : bfd_abs_section_ptr;
9829 h->root.u.def.value = val;
9831 h->type = STT_OBJECT;
9832 h->other = STV_HIDDEN | (h->other & ~ ELF_ST_VISIBILITY (-1));
9833 h->forced_local = 1;
9836 /* Set NAME to VAL if the symbol exists and is not defined in a regular
9837 object file. If S is NULL it is an absolute symbol, otherwise it is
9838 relative to that section. */
9841 _bfd_elf_provide_symbol (struct bfd_link_info *info, const char *name,
9842 bfd_vma val, struct bfd_section *s)
9844 struct elf_link_hash_entry *h;
9846 bfd_elf_record_link_assignment (info, name, TRUE);
9848 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
9850 && !(h->root.type == bfd_link_hash_defined
9851 && h->root.u.def.section != NULL
9852 && h->root.u.def.section != h->root.u.def.section->output_section))
9853 bfd_elf_set_symbol (h, val, s);
9856 /* Set START and END to boundaries of SEC if they exist and are not
9857 defined in regular object files. */
9860 _bfd_elf_provide_section_bound_symbols (struct bfd_link_info *info,
9866 _bfd_elf_provide_symbol (info, start, val, sec);
9869 _bfd_elf_provide_symbol (info, end, val, sec);
9872 /* Convert symbols in excluded output sections to absolute. */
9875 fix_syms (struct bfd_link_hash_entry *h, void *data)
9877 bfd *obfd = (bfd *) data;
9879 if (h->type == bfd_link_hash_warning)
9882 if (h->type == bfd_link_hash_defined
9883 || h->type == bfd_link_hash_defweak)
9885 asection *s = h->u.def.section;
9887 && s == s->output_section
9888 && bfd_section_removed_from_list (obfd, s))
9890 h->u.def.value += s->vma;
9891 h->u.def.section = bfd_abs_section_ptr;
9899 _bfd_elf_fix_excluded_sec_syms (bfd *obfd, struct bfd_link_info *info)
9901 bfd_link_hash_traverse (info->hash, fix_syms, obfd);