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;
783 const struct elf_backend_data *bed;
789 bind = ELF_ST_BIND (sym->st_info);
791 if (! bfd_is_und_section (sec))
792 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
794 h = ((struct elf_link_hash_entry *)
795 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
800 /* This code is for coping with dynamic objects, and is only useful
801 if we are doing an ELF link. */
802 if (info->hash->creator != abfd->xvec)
805 /* For merging, we only care about real symbols. */
807 while (h->root.type == bfd_link_hash_indirect
808 || h->root.type == bfd_link_hash_warning)
809 h = (struct elf_link_hash_entry *) h->root.u.i.link;
811 /* If we just created the symbol, mark it as being an ELF symbol.
812 Other than that, there is nothing to do--there is no merge issue
813 with a newly defined symbol--so we just return. */
815 if (h->root.type == bfd_link_hash_new)
821 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
824 switch (h->root.type)
831 case bfd_link_hash_undefined:
832 case bfd_link_hash_undefweak:
833 oldbfd = h->root.u.undef.abfd;
837 case bfd_link_hash_defined:
838 case bfd_link_hash_defweak:
839 oldbfd = h->root.u.def.section->owner;
840 oldsec = h->root.u.def.section;
843 case bfd_link_hash_common:
844 oldbfd = h->root.u.c.p->section->owner;
845 oldsec = h->root.u.c.p->section;
849 /* In cases involving weak versioned symbols, we may wind up trying
850 to merge a symbol with itself. Catch that here, to avoid the
851 confusion that results if we try to override a symbol with
852 itself. The additional tests catch cases like
853 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
854 dynamic object, which we do want to handle here. */
856 && ((abfd->flags & DYNAMIC) == 0
860 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
861 respectively, is from a dynamic object. */
863 if ((abfd->flags & DYNAMIC) != 0)
869 olddyn = (oldbfd->flags & DYNAMIC) != 0;
874 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
875 indices used by MIPS ELF. */
876 switch (h->root.type)
882 case bfd_link_hash_defined:
883 case bfd_link_hash_defweak:
884 hsec = h->root.u.def.section;
887 case bfd_link_hash_common:
888 hsec = h->root.u.c.p->section;
895 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
898 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
899 respectively, appear to be a definition rather than reference. */
901 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
906 if (h->root.type == bfd_link_hash_undefined
907 || h->root.type == bfd_link_hash_undefweak
908 || h->root.type == bfd_link_hash_common)
913 /* Check TLS symbol. */
914 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
915 && ELF_ST_TYPE (sym->st_info) != h->type)
918 bfd_boolean ntdef, tdef;
919 asection *ntsec, *tsec;
921 if (h->type == STT_TLS)
941 (*_bfd_error_handler)
942 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
943 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
944 else if (!tdef && !ntdef)
945 (*_bfd_error_handler)
946 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
947 tbfd, ntbfd, h->root.root.string);
949 (*_bfd_error_handler)
950 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
951 tbfd, tsec, ntbfd, h->root.root.string);
953 (*_bfd_error_handler)
954 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
955 tbfd, ntbfd, ntsec, h->root.root.string);
957 bfd_set_error (bfd_error_bad_value);
961 /* We need to remember if a symbol has a definition in a dynamic
962 object or is weak in all dynamic objects. Internal and hidden
963 visibility will make it unavailable to dynamic objects. */
964 if (newdyn && !h->dynamic_def)
966 if (!bfd_is_und_section (sec))
970 /* Check if this symbol is weak in all dynamic objects. If it
971 is the first time we see it in a dynamic object, we mark
972 if it is weak. Otherwise, we clear it. */
975 if (bind == STB_WEAK)
978 else if (bind != STB_WEAK)
983 /* If the old symbol has non-default visibility, we ignore the new
984 definition from a dynamic object. */
986 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
987 && !bfd_is_und_section (sec))
990 /* Make sure this symbol is dynamic. */
992 /* A protected symbol has external availability. Make sure it is
995 FIXME: Should we check type and size for protected symbol? */
996 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
997 return bfd_elf_link_record_dynamic_symbol (info, h);
1002 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1005 /* If the new symbol with non-default visibility comes from a
1006 relocatable file and the old definition comes from a dynamic
1007 object, we remove the old definition. */
1008 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1011 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1012 && bfd_is_und_section (sec))
1014 /* If the new symbol is undefined and the old symbol was
1015 also undefined before, we need to make sure
1016 _bfd_generic_link_add_one_symbol doesn't mess
1017 up the linker hash table undefs list. Since the old
1018 definition came from a dynamic object, it is still on the
1020 h->root.type = bfd_link_hash_undefined;
1021 h->root.u.undef.abfd = abfd;
1025 h->root.type = bfd_link_hash_new;
1026 h->root.u.undef.abfd = NULL;
1035 /* FIXME: Should we check type and size for protected symbol? */
1041 /* Differentiate strong and weak symbols. */
1042 newweak = bind == STB_WEAK;
1043 oldweak = (h->root.type == bfd_link_hash_defweak
1044 || h->root.type == bfd_link_hash_undefweak);
1046 /* If a new weak symbol definition comes from a regular file and the
1047 old symbol comes from a dynamic library, we treat the new one as
1048 strong. Similarly, an old weak symbol definition from a regular
1049 file is treated as strong when the new symbol comes from a dynamic
1050 library. Further, an old weak symbol from a dynamic library is
1051 treated as strong if the new symbol is from a dynamic library.
1052 This reflects the way glibc's ld.so works.
1054 Do this before setting *type_change_ok or *size_change_ok so that
1055 we warn properly when dynamic library symbols are overridden. */
1057 if (newdef && !newdyn && olddyn)
1059 if (olddef && newdyn)
1062 /* It's OK to change the type if either the existing symbol or the
1063 new symbol is weak. A type change is also OK if the old symbol
1064 is undefined and the new symbol is defined. */
1069 && h->root.type == bfd_link_hash_undefined))
1070 *type_change_ok = TRUE;
1072 /* It's OK to change the size if either the existing symbol or the
1073 new symbol is weak, or if the old symbol is undefined. */
1076 || h->root.type == bfd_link_hash_undefined)
1077 *size_change_ok = TRUE;
1079 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1080 symbol, respectively, appears to be a common symbol in a dynamic
1081 object. If a symbol appears in an uninitialized section, and is
1082 not weak, and is not a function, then it may be a common symbol
1083 which was resolved when the dynamic object was created. We want
1084 to treat such symbols specially, because they raise special
1085 considerations when setting the symbol size: if the symbol
1086 appears as a common symbol in a regular object, and the size in
1087 the regular object is larger, we must make sure that we use the
1088 larger size. This problematic case can always be avoided in C,
1089 but it must be handled correctly when using Fortran shared
1092 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1093 likewise for OLDDYNCOMMON and OLDDEF.
1095 Note that this test is just a heuristic, and that it is quite
1096 possible to have an uninitialized symbol in a shared object which
1097 is really a definition, rather than a common symbol. This could
1098 lead to some minor confusion when the symbol really is a common
1099 symbol in some regular object. However, I think it will be
1105 && (sec->flags & SEC_ALLOC) != 0
1106 && (sec->flags & SEC_LOAD) == 0
1108 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1109 newdyncommon = TRUE;
1111 newdyncommon = FALSE;
1115 && h->root.type == bfd_link_hash_defined
1117 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1118 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1120 && h->type != STT_FUNC)
1121 olddyncommon = TRUE;
1123 olddyncommon = FALSE;
1125 /* We now know everything about the old and new symbols. We ask the
1126 backend to check if we can merge them. */
1127 bed = get_elf_backend_data (abfd);
1128 if (bed->merge_symbol
1129 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1130 pold_alignment, skip, override,
1131 type_change_ok, size_change_ok,
1132 &newdyn, &newdef, &newdyncommon, &newweak,
1134 &olddyn, &olddef, &olddyncommon, &oldweak,
1138 /* If both the old and the new symbols look like common symbols in a
1139 dynamic object, set the size of the symbol to the larger of the
1144 && sym->st_size != h->size)
1146 /* Since we think we have two common symbols, issue a multiple
1147 common warning if desired. Note that we only warn if the
1148 size is different. If the size is the same, we simply let
1149 the old symbol override the new one as normally happens with
1150 symbols defined in dynamic objects. */
1152 if (! ((*info->callbacks->multiple_common)
1153 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1154 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1157 if (sym->st_size > h->size)
1158 h->size = sym->st_size;
1160 *size_change_ok = TRUE;
1163 /* If we are looking at a dynamic object, and we have found a
1164 definition, we need to see if the symbol was already defined by
1165 some other object. If so, we want to use the existing
1166 definition, and we do not want to report a multiple symbol
1167 definition error; we do this by clobbering *PSEC to be
1168 bfd_und_section_ptr.
1170 We treat a common symbol as a definition if the symbol in the
1171 shared library is a function, since common symbols always
1172 represent variables; this can cause confusion in principle, but
1173 any such confusion would seem to indicate an erroneous program or
1174 shared library. We also permit a common symbol in a regular
1175 object to override a weak symbol in a shared object. */
1180 || (h->root.type == bfd_link_hash_common
1182 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1186 newdyncommon = FALSE;
1188 *psec = sec = bfd_und_section_ptr;
1189 *size_change_ok = TRUE;
1191 /* If we get here when the old symbol is a common symbol, then
1192 we are explicitly letting it override a weak symbol or
1193 function in a dynamic object, and we don't want to warn about
1194 a type change. If the old symbol is a defined symbol, a type
1195 change warning may still be appropriate. */
1197 if (h->root.type == bfd_link_hash_common)
1198 *type_change_ok = TRUE;
1201 /* Handle the special case of an old common symbol merging with a
1202 new symbol which looks like a common symbol in a shared object.
1203 We change *PSEC and *PVALUE to make the new symbol look like a
1204 common symbol, and let _bfd_generic_link_add_one_symbol do the
1208 && h->root.type == bfd_link_hash_common)
1212 newdyncommon = FALSE;
1213 *pvalue = sym->st_size;
1214 *psec = sec = bed->common_section (oldsec);
1215 *size_change_ok = TRUE;
1218 /* Skip weak definitions of symbols that are already defined. */
1219 if (newdef && olddef && newweak)
1222 /* If the old symbol is from a dynamic object, and the new symbol is
1223 a definition which is not from a dynamic object, then the new
1224 symbol overrides the old symbol. Symbols from regular files
1225 always take precedence over symbols from dynamic objects, even if
1226 they are defined after the dynamic object in the link.
1228 As above, we again permit a common symbol in a regular object to
1229 override a definition in a shared object if the shared object
1230 symbol is a function or is weak. */
1235 || (bfd_is_com_section (sec)
1237 || h->type == STT_FUNC)))
1242 /* Change the hash table entry to undefined, and let
1243 _bfd_generic_link_add_one_symbol do the right thing with the
1246 h->root.type = bfd_link_hash_undefined;
1247 h->root.u.undef.abfd = h->root.u.def.section->owner;
1248 *size_change_ok = TRUE;
1251 olddyncommon = FALSE;
1253 /* We again permit a type change when a common symbol may be
1254 overriding a function. */
1256 if (bfd_is_com_section (sec))
1257 *type_change_ok = TRUE;
1259 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1262 /* This union may have been set to be non-NULL when this symbol
1263 was seen in a dynamic object. We must force the union to be
1264 NULL, so that it is correct for a regular symbol. */
1265 h->verinfo.vertree = NULL;
1268 /* Handle the special case of a new common symbol merging with an
1269 old symbol that looks like it might be a common symbol defined in
1270 a shared object. Note that we have already handled the case in
1271 which a new common symbol should simply override the definition
1272 in the shared library. */
1275 && bfd_is_com_section (sec)
1278 /* It would be best if we could set the hash table entry to a
1279 common symbol, but we don't know what to use for the section
1280 or the alignment. */
1281 if (! ((*info->callbacks->multiple_common)
1282 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1283 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1286 /* If the presumed common symbol in the dynamic object is
1287 larger, pretend that the new symbol has its size. */
1289 if (h->size > *pvalue)
1292 /* We need to remember the alignment required by the symbol
1293 in the dynamic object. */
1294 BFD_ASSERT (pold_alignment);
1295 *pold_alignment = h->root.u.def.section->alignment_power;
1298 olddyncommon = FALSE;
1300 h->root.type = bfd_link_hash_undefined;
1301 h->root.u.undef.abfd = h->root.u.def.section->owner;
1303 *size_change_ok = TRUE;
1304 *type_change_ok = TRUE;
1306 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1309 h->verinfo.vertree = NULL;
1314 /* Handle the case where we had a versioned symbol in a dynamic
1315 library and now find a definition in a normal object. In this
1316 case, we make the versioned symbol point to the normal one. */
1317 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1318 flip->root.type = h->root.type;
1319 h->root.type = bfd_link_hash_indirect;
1320 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1321 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1322 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1326 flip->ref_dynamic = 1;
1333 /* This function is called to create an indirect symbol from the
1334 default for the symbol with the default version if needed. The
1335 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1336 set DYNSYM if the new indirect symbol is dynamic. */
1339 _bfd_elf_add_default_symbol (bfd *abfd,
1340 struct bfd_link_info *info,
1341 struct elf_link_hash_entry *h,
1343 Elf_Internal_Sym *sym,
1346 bfd_boolean *dynsym,
1347 bfd_boolean override)
1349 bfd_boolean type_change_ok;
1350 bfd_boolean size_change_ok;
1353 struct elf_link_hash_entry *hi;
1354 struct bfd_link_hash_entry *bh;
1355 const struct elf_backend_data *bed;
1356 bfd_boolean collect;
1357 bfd_boolean dynamic;
1359 size_t len, shortlen;
1362 /* If this symbol has a version, and it is the default version, we
1363 create an indirect symbol from the default name to the fully
1364 decorated name. This will cause external references which do not
1365 specify a version to be bound to this version of the symbol. */
1366 p = strchr (name, ELF_VER_CHR);
1367 if (p == NULL || p[1] != ELF_VER_CHR)
1372 /* We are overridden by an old definition. We need to check if we
1373 need to create the indirect symbol from the default name. */
1374 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1376 BFD_ASSERT (hi != NULL);
1379 while (hi->root.type == bfd_link_hash_indirect
1380 || hi->root.type == bfd_link_hash_warning)
1382 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1388 bed = get_elf_backend_data (abfd);
1389 collect = bed->collect;
1390 dynamic = (abfd->flags & DYNAMIC) != 0;
1392 shortlen = p - name;
1393 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1394 if (shortname == NULL)
1396 memcpy (shortname, name, shortlen);
1397 shortname[shortlen] = '\0';
1399 /* We are going to create a new symbol. Merge it with any existing
1400 symbol with this name. For the purposes of the merge, act as
1401 though we were defining the symbol we just defined, although we
1402 actually going to define an indirect symbol. */
1403 type_change_ok = FALSE;
1404 size_change_ok = FALSE;
1406 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1407 NULL, &hi, &skip, &override,
1408 &type_change_ok, &size_change_ok))
1417 if (! (_bfd_generic_link_add_one_symbol
1418 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1419 0, name, FALSE, collect, &bh)))
1421 hi = (struct elf_link_hash_entry *) bh;
1425 /* In this case the symbol named SHORTNAME is overriding the
1426 indirect symbol we want to add. We were planning on making
1427 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1428 is the name without a version. NAME is the fully versioned
1429 name, and it is the default version.
1431 Overriding means that we already saw a definition for the
1432 symbol SHORTNAME in a regular object, and it is overriding
1433 the symbol defined in the dynamic object.
1435 When this happens, we actually want to change NAME, the
1436 symbol we just added, to refer to SHORTNAME. This will cause
1437 references to NAME in the shared object to become references
1438 to SHORTNAME in the regular object. This is what we expect
1439 when we override a function in a shared object: that the
1440 references in the shared object will be mapped to the
1441 definition in the regular object. */
1443 while (hi->root.type == bfd_link_hash_indirect
1444 || hi->root.type == bfd_link_hash_warning)
1445 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1447 h->root.type = bfd_link_hash_indirect;
1448 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1452 hi->ref_dynamic = 1;
1456 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1461 /* Now set HI to H, so that the following code will set the
1462 other fields correctly. */
1466 /* If there is a duplicate definition somewhere, then HI may not
1467 point to an indirect symbol. We will have reported an error to
1468 the user in that case. */
1470 if (hi->root.type == bfd_link_hash_indirect)
1472 struct elf_link_hash_entry *ht;
1474 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1475 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1477 /* See if the new flags lead us to realize that the symbol must
1489 if (hi->ref_regular)
1495 /* We also need to define an indirection from the nondefault version
1499 len = strlen (name);
1500 shortname = bfd_hash_allocate (&info->hash->table, len);
1501 if (shortname == NULL)
1503 memcpy (shortname, name, shortlen);
1504 memcpy (shortname + shortlen, p + 1, len - shortlen);
1506 /* Once again, merge with any existing symbol. */
1507 type_change_ok = FALSE;
1508 size_change_ok = FALSE;
1510 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1511 NULL, &hi, &skip, &override,
1512 &type_change_ok, &size_change_ok))
1520 /* Here SHORTNAME is a versioned name, so we don't expect to see
1521 the type of override we do in the case above unless it is
1522 overridden by a versioned definition. */
1523 if (hi->root.type != bfd_link_hash_defined
1524 && hi->root.type != bfd_link_hash_defweak)
1525 (*_bfd_error_handler)
1526 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1532 if (! (_bfd_generic_link_add_one_symbol
1533 (info, abfd, shortname, BSF_INDIRECT,
1534 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1536 hi = (struct elf_link_hash_entry *) bh;
1538 /* If there is a duplicate definition somewhere, then HI may not
1539 point to an indirect symbol. We will have reported an error
1540 to the user in that case. */
1542 if (hi->root.type == bfd_link_hash_indirect)
1544 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1546 /* See if the new flags lead us to realize that the symbol
1558 if (hi->ref_regular)
1568 /* This routine is used to export all defined symbols into the dynamic
1569 symbol table. It is called via elf_link_hash_traverse. */
1572 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1574 struct elf_info_failed *eif = data;
1576 /* Ignore indirect symbols. These are added by the versioning code. */
1577 if (h->root.type == bfd_link_hash_indirect)
1580 if (h->root.type == bfd_link_hash_warning)
1581 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1583 if (h->dynindx == -1
1587 struct bfd_elf_version_tree *t;
1588 struct bfd_elf_version_expr *d;
1590 for (t = eif->verdefs; t != NULL; t = t->next)
1592 if (t->globals.list != NULL)
1594 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1599 if (t->locals.list != NULL)
1601 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1610 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1621 /* Look through the symbols which are defined in other shared
1622 libraries and referenced here. Update the list of version
1623 dependencies. This will be put into the .gnu.version_r section.
1624 This function is called via elf_link_hash_traverse. */
1627 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1630 struct elf_find_verdep_info *rinfo = data;
1631 Elf_Internal_Verneed *t;
1632 Elf_Internal_Vernaux *a;
1635 if (h->root.type == bfd_link_hash_warning)
1636 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1638 /* We only care about symbols defined in shared objects with version
1643 || h->verinfo.verdef == NULL)
1646 /* See if we already know about this version. */
1647 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1649 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1652 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1653 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1659 /* This is a new version. Add it to tree we are building. */
1664 t = bfd_zalloc (rinfo->output_bfd, amt);
1667 rinfo->failed = TRUE;
1671 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1672 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1673 elf_tdata (rinfo->output_bfd)->verref = t;
1677 a = bfd_zalloc (rinfo->output_bfd, amt);
1679 /* Note that we are copying a string pointer here, and testing it
1680 above. If bfd_elf_string_from_elf_section is ever changed to
1681 discard the string data when low in memory, this will have to be
1683 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1685 a->vna_flags = h->verinfo.verdef->vd_flags;
1686 a->vna_nextptr = t->vn_auxptr;
1688 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1691 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1698 /* Figure out appropriate versions for all the symbols. We may not
1699 have the version number script until we have read all of the input
1700 files, so until that point we don't know which symbols should be
1701 local. This function is called via elf_link_hash_traverse. */
1704 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1706 struct elf_assign_sym_version_info *sinfo;
1707 struct bfd_link_info *info;
1708 const struct elf_backend_data *bed;
1709 struct elf_info_failed eif;
1716 if (h->root.type == bfd_link_hash_warning)
1717 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1719 /* Fix the symbol flags. */
1722 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1725 sinfo->failed = TRUE;
1729 /* We only need version numbers for symbols defined in regular
1731 if (!h->def_regular)
1734 bed = get_elf_backend_data (sinfo->output_bfd);
1735 p = strchr (h->root.root.string, ELF_VER_CHR);
1736 if (p != NULL && h->verinfo.vertree == NULL)
1738 struct bfd_elf_version_tree *t;
1743 /* There are two consecutive ELF_VER_CHR characters if this is
1744 not a hidden symbol. */
1746 if (*p == ELF_VER_CHR)
1752 /* If there is no version string, we can just return out. */
1760 /* Look for the version. If we find it, it is no longer weak. */
1761 for (t = sinfo->verdefs; t != NULL; t = t->next)
1763 if (strcmp (t->name, p) == 0)
1767 struct bfd_elf_version_expr *d;
1769 len = p - h->root.root.string;
1770 alc = bfd_malloc (len);
1773 memcpy (alc, h->root.root.string, len - 1);
1774 alc[len - 1] = '\0';
1775 if (alc[len - 2] == ELF_VER_CHR)
1776 alc[len - 2] = '\0';
1778 h->verinfo.vertree = t;
1782 if (t->globals.list != NULL)
1783 d = (*t->match) (&t->globals, NULL, alc);
1785 /* See if there is anything to force this symbol to
1787 if (d == NULL && t->locals.list != NULL)
1789 d = (*t->match) (&t->locals, NULL, alc);
1792 && ! info->export_dynamic)
1793 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1801 /* If we are building an application, we need to create a
1802 version node for this version. */
1803 if (t == NULL && info->executable)
1805 struct bfd_elf_version_tree **pp;
1808 /* If we aren't going to export this symbol, we don't need
1809 to worry about it. */
1810 if (h->dynindx == -1)
1814 t = bfd_zalloc (sinfo->output_bfd, amt);
1817 sinfo->failed = TRUE;
1822 t->name_indx = (unsigned int) -1;
1826 /* Don't count anonymous version tag. */
1827 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1829 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1831 t->vernum = version_index;
1835 h->verinfo.vertree = t;
1839 /* We could not find the version for a symbol when
1840 generating a shared archive. Return an error. */
1841 (*_bfd_error_handler)
1842 (_("%B: undefined versioned symbol name %s"),
1843 sinfo->output_bfd, h->root.root.string);
1844 bfd_set_error (bfd_error_bad_value);
1845 sinfo->failed = TRUE;
1853 /* If we don't have a version for this symbol, see if we can find
1855 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1857 struct bfd_elf_version_tree *t;
1858 struct bfd_elf_version_tree *local_ver;
1859 struct bfd_elf_version_expr *d;
1861 /* See if can find what version this symbol is in. If the
1862 symbol is supposed to be local, then don't actually register
1865 for (t = sinfo->verdefs; t != NULL; t = t->next)
1867 if (t->globals.list != NULL)
1869 bfd_boolean matched;
1873 while ((d = (*t->match) (&t->globals, d,
1874 h->root.root.string)) != NULL)
1879 /* There is a version without definition. Make
1880 the symbol the default definition for this
1882 h->verinfo.vertree = t;
1890 /* There is no undefined version for this symbol. Hide the
1892 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1895 if (t->locals.list != NULL)
1898 while ((d = (*t->match) (&t->locals, d,
1899 h->root.root.string)) != NULL)
1902 /* If the match is "*", keep looking for a more
1903 explicit, perhaps even global, match.
1904 XXX: Shouldn't this be !d->wildcard instead? */
1905 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1914 if (local_ver != NULL)
1916 h->verinfo.vertree = local_ver;
1917 if (h->dynindx != -1
1918 && ! info->export_dynamic)
1920 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1928 /* Read and swap the relocs from the section indicated by SHDR. This
1929 may be either a REL or a RELA section. The relocations are
1930 translated into RELA relocations and stored in INTERNAL_RELOCS,
1931 which should have already been allocated to contain enough space.
1932 The EXTERNAL_RELOCS are a buffer where the external form of the
1933 relocations should be stored.
1935 Returns FALSE if something goes wrong. */
1938 elf_link_read_relocs_from_section (bfd *abfd,
1940 Elf_Internal_Shdr *shdr,
1941 void *external_relocs,
1942 Elf_Internal_Rela *internal_relocs)
1944 const struct elf_backend_data *bed;
1945 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1946 const bfd_byte *erela;
1947 const bfd_byte *erelaend;
1948 Elf_Internal_Rela *irela;
1949 Elf_Internal_Shdr *symtab_hdr;
1952 /* Position ourselves at the start of the section. */
1953 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1956 /* Read the relocations. */
1957 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1960 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1961 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1963 bed = get_elf_backend_data (abfd);
1965 /* Convert the external relocations to the internal format. */
1966 if (shdr->sh_entsize == bed->s->sizeof_rel)
1967 swap_in = bed->s->swap_reloc_in;
1968 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1969 swap_in = bed->s->swap_reloca_in;
1972 bfd_set_error (bfd_error_wrong_format);
1976 erela = external_relocs;
1977 erelaend = erela + shdr->sh_size;
1978 irela = internal_relocs;
1979 while (erela < erelaend)
1983 (*swap_in) (abfd, erela, irela);
1984 r_symndx = ELF32_R_SYM (irela->r_info);
1985 if (bed->s->arch_size == 64)
1987 if ((size_t) r_symndx >= nsyms)
1989 (*_bfd_error_handler)
1990 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1991 " for offset 0x%lx in section `%A'"),
1993 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1994 bfd_set_error (bfd_error_bad_value);
1997 irela += bed->s->int_rels_per_ext_rel;
1998 erela += shdr->sh_entsize;
2004 /* Read and swap the relocs for a section O. They may have been
2005 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2006 not NULL, they are used as buffers to read into. They are known to
2007 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2008 the return value is allocated using either malloc or bfd_alloc,
2009 according to the KEEP_MEMORY argument. If O has two relocation
2010 sections (both REL and RELA relocations), then the REL_HDR
2011 relocations will appear first in INTERNAL_RELOCS, followed by the
2012 REL_HDR2 relocations. */
2015 _bfd_elf_link_read_relocs (bfd *abfd,
2017 void *external_relocs,
2018 Elf_Internal_Rela *internal_relocs,
2019 bfd_boolean keep_memory)
2021 Elf_Internal_Shdr *rel_hdr;
2022 void *alloc1 = NULL;
2023 Elf_Internal_Rela *alloc2 = NULL;
2024 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2026 if (elf_section_data (o)->relocs != NULL)
2027 return elf_section_data (o)->relocs;
2029 if (o->reloc_count == 0)
2032 rel_hdr = &elf_section_data (o)->rel_hdr;
2034 if (internal_relocs == NULL)
2038 size = o->reloc_count;
2039 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2041 internal_relocs = bfd_alloc (abfd, size);
2043 internal_relocs = alloc2 = bfd_malloc (size);
2044 if (internal_relocs == NULL)
2048 if (external_relocs == NULL)
2050 bfd_size_type size = rel_hdr->sh_size;
2052 if (elf_section_data (o)->rel_hdr2)
2053 size += elf_section_data (o)->rel_hdr2->sh_size;
2054 alloc1 = bfd_malloc (size);
2057 external_relocs = alloc1;
2060 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2064 if (elf_section_data (o)->rel_hdr2
2065 && (!elf_link_read_relocs_from_section
2067 elf_section_data (o)->rel_hdr2,
2068 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2069 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2070 * bed->s->int_rels_per_ext_rel))))
2073 /* Cache the results for next time, if we can. */
2075 elf_section_data (o)->relocs = internal_relocs;
2080 /* Don't free alloc2, since if it was allocated we are passing it
2081 back (under the name of internal_relocs). */
2083 return internal_relocs;
2093 /* Compute the size of, and allocate space for, REL_HDR which is the
2094 section header for a section containing relocations for O. */
2097 _bfd_elf_link_size_reloc_section (bfd *abfd,
2098 Elf_Internal_Shdr *rel_hdr,
2101 bfd_size_type reloc_count;
2102 bfd_size_type num_rel_hashes;
2104 /* Figure out how many relocations there will be. */
2105 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2106 reloc_count = elf_section_data (o)->rel_count;
2108 reloc_count = elf_section_data (o)->rel_count2;
2110 num_rel_hashes = o->reloc_count;
2111 if (num_rel_hashes < reloc_count)
2112 num_rel_hashes = reloc_count;
2114 /* That allows us to calculate the size of the section. */
2115 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2117 /* The contents field must last into write_object_contents, so we
2118 allocate it with bfd_alloc rather than malloc. Also since we
2119 cannot be sure that the contents will actually be filled in,
2120 we zero the allocated space. */
2121 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2122 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2125 /* We only allocate one set of hash entries, so we only do it the
2126 first time we are called. */
2127 if (elf_section_data (o)->rel_hashes == NULL
2130 struct elf_link_hash_entry **p;
2132 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2136 elf_section_data (o)->rel_hashes = p;
2142 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2143 originated from the section given by INPUT_REL_HDR) to the
2147 _bfd_elf_link_output_relocs (bfd *output_bfd,
2148 asection *input_section,
2149 Elf_Internal_Shdr *input_rel_hdr,
2150 Elf_Internal_Rela *internal_relocs,
2151 struct elf_link_hash_entry **rel_hash
2154 Elf_Internal_Rela *irela;
2155 Elf_Internal_Rela *irelaend;
2157 Elf_Internal_Shdr *output_rel_hdr;
2158 asection *output_section;
2159 unsigned int *rel_countp = NULL;
2160 const struct elf_backend_data *bed;
2161 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2163 output_section = input_section->output_section;
2164 output_rel_hdr = NULL;
2166 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2167 == input_rel_hdr->sh_entsize)
2169 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2170 rel_countp = &elf_section_data (output_section)->rel_count;
2172 else if (elf_section_data (output_section)->rel_hdr2
2173 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2174 == input_rel_hdr->sh_entsize))
2176 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2177 rel_countp = &elf_section_data (output_section)->rel_count2;
2181 (*_bfd_error_handler)
2182 (_("%B: relocation size mismatch in %B section %A"),
2183 output_bfd, input_section->owner, input_section);
2184 bfd_set_error (bfd_error_wrong_object_format);
2188 bed = get_elf_backend_data (output_bfd);
2189 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2190 swap_out = bed->s->swap_reloc_out;
2191 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2192 swap_out = bed->s->swap_reloca_out;
2196 erel = output_rel_hdr->contents;
2197 erel += *rel_countp * input_rel_hdr->sh_entsize;
2198 irela = internal_relocs;
2199 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2200 * bed->s->int_rels_per_ext_rel);
2201 while (irela < irelaend)
2203 (*swap_out) (output_bfd, irela, erel);
2204 irela += bed->s->int_rels_per_ext_rel;
2205 erel += input_rel_hdr->sh_entsize;
2208 /* Bump the counter, so that we know where to add the next set of
2210 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2215 /* Fix up the flags for a symbol. This handles various cases which
2216 can only be fixed after all the input files are seen. This is
2217 currently called by both adjust_dynamic_symbol and
2218 assign_sym_version, which is unnecessary but perhaps more robust in
2219 the face of future changes. */
2222 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2223 struct elf_info_failed *eif)
2225 /* If this symbol was mentioned in a non-ELF file, try to set
2226 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2227 permit a non-ELF file to correctly refer to a symbol defined in
2228 an ELF dynamic object. */
2231 while (h->root.type == bfd_link_hash_indirect)
2232 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2234 if (h->root.type != bfd_link_hash_defined
2235 && h->root.type != bfd_link_hash_defweak)
2238 h->ref_regular_nonweak = 1;
2242 if (h->root.u.def.section->owner != NULL
2243 && (bfd_get_flavour (h->root.u.def.section->owner)
2244 == bfd_target_elf_flavour))
2247 h->ref_regular_nonweak = 1;
2253 if (h->dynindx == -1
2257 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2266 /* Unfortunately, NON_ELF is only correct if the symbol
2267 was first seen in a non-ELF file. Fortunately, if the symbol
2268 was first seen in an ELF file, we're probably OK unless the
2269 symbol was defined in a non-ELF file. Catch that case here.
2270 FIXME: We're still in trouble if the symbol was first seen in
2271 a dynamic object, and then later in a non-ELF regular object. */
2272 if ((h->root.type == bfd_link_hash_defined
2273 || h->root.type == bfd_link_hash_defweak)
2275 && (h->root.u.def.section->owner != NULL
2276 ? (bfd_get_flavour (h->root.u.def.section->owner)
2277 != bfd_target_elf_flavour)
2278 : (bfd_is_abs_section (h->root.u.def.section)
2279 && !h->def_dynamic)))
2283 /* If this is a final link, and the symbol was defined as a common
2284 symbol in a regular object file, and there was no definition in
2285 any dynamic object, then the linker will have allocated space for
2286 the symbol in a common section but the DEF_REGULAR
2287 flag will not have been set. */
2288 if (h->root.type == bfd_link_hash_defined
2292 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2295 /* If -Bsymbolic was used (which means to bind references to global
2296 symbols to the definition within the shared object), and this
2297 symbol was defined in a regular object, then it actually doesn't
2298 need a PLT entry. Likewise, if the symbol has non-default
2299 visibility. If the symbol has hidden or internal visibility, we
2300 will force it local. */
2302 && eif->info->shared
2303 && is_elf_hash_table (eif->info->hash)
2304 && (eif->info->symbolic
2305 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2308 const struct elf_backend_data *bed;
2309 bfd_boolean force_local;
2311 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2313 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2314 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2315 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2318 /* If a weak undefined symbol has non-default visibility, we also
2319 hide it from the dynamic linker. */
2320 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2321 && h->root.type == bfd_link_hash_undefweak)
2323 const struct elf_backend_data *bed;
2324 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2325 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2328 /* If this is a weak defined symbol in a dynamic object, and we know
2329 the real definition in the dynamic object, copy interesting flags
2330 over to the real definition. */
2331 if (h->u.weakdef != NULL)
2333 struct elf_link_hash_entry *weakdef;
2335 weakdef = h->u.weakdef;
2336 if (h->root.type == bfd_link_hash_indirect)
2337 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2339 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2340 || h->root.type == bfd_link_hash_defweak);
2341 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2342 || weakdef->root.type == bfd_link_hash_defweak);
2343 BFD_ASSERT (weakdef->def_dynamic);
2345 /* If the real definition is defined by a regular object file,
2346 don't do anything special. See the longer description in
2347 _bfd_elf_adjust_dynamic_symbol, below. */
2348 if (weakdef->def_regular)
2349 h->u.weakdef = NULL;
2352 const struct elf_backend_data *bed;
2354 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2355 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2362 /* Make the backend pick a good value for a dynamic symbol. This is
2363 called via elf_link_hash_traverse, and also calls itself
2367 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2369 struct elf_info_failed *eif = data;
2371 const struct elf_backend_data *bed;
2373 if (! is_elf_hash_table (eif->info->hash))
2376 if (h->root.type == bfd_link_hash_warning)
2378 h->got = elf_hash_table (eif->info)->init_got_offset;
2379 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2381 /* When warning symbols are created, they **replace** the "real"
2382 entry in the hash table, thus we never get to see the real
2383 symbol in a hash traversal. So look at it now. */
2384 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2387 /* Ignore indirect symbols. These are added by the versioning code. */
2388 if (h->root.type == bfd_link_hash_indirect)
2391 /* Fix the symbol flags. */
2392 if (! _bfd_elf_fix_symbol_flags (h, eif))
2395 /* If this symbol does not require a PLT entry, and it is not
2396 defined by a dynamic object, or is not referenced by a regular
2397 object, ignore it. We do have to handle a weak defined symbol,
2398 even if no regular object refers to it, if we decided to add it
2399 to the dynamic symbol table. FIXME: Do we normally need to worry
2400 about symbols which are defined by one dynamic object and
2401 referenced by another one? */
2406 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2408 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2412 /* If we've already adjusted this symbol, don't do it again. This
2413 can happen via a recursive call. */
2414 if (h->dynamic_adjusted)
2417 /* Don't look at this symbol again. Note that we must set this
2418 after checking the above conditions, because we may look at a
2419 symbol once, decide not to do anything, and then get called
2420 recursively later after REF_REGULAR is set below. */
2421 h->dynamic_adjusted = 1;
2423 /* If this is a weak definition, and we know a real definition, and
2424 the real symbol is not itself defined by a regular object file,
2425 then get a good value for the real definition. We handle the
2426 real symbol first, for the convenience of the backend routine.
2428 Note that there is a confusing case here. If the real definition
2429 is defined by a regular object file, we don't get the real symbol
2430 from the dynamic object, but we do get the weak symbol. If the
2431 processor backend uses a COPY reloc, then if some routine in the
2432 dynamic object changes the real symbol, we will not see that
2433 change in the corresponding weak symbol. This is the way other
2434 ELF linkers work as well, and seems to be a result of the shared
2437 I will clarify this issue. Most SVR4 shared libraries define the
2438 variable _timezone and define timezone as a weak synonym. The
2439 tzset call changes _timezone. If you write
2440 extern int timezone;
2442 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2443 you might expect that, since timezone is a synonym for _timezone,
2444 the same number will print both times. However, if the processor
2445 backend uses a COPY reloc, then actually timezone will be copied
2446 into your process image, and, since you define _timezone
2447 yourself, _timezone will not. Thus timezone and _timezone will
2448 wind up at different memory locations. The tzset call will set
2449 _timezone, leaving timezone unchanged. */
2451 if (h->u.weakdef != NULL)
2453 /* If we get to this point, we know there is an implicit
2454 reference by a regular object file via the weak symbol H.
2455 FIXME: Is this really true? What if the traversal finds
2456 H->U.WEAKDEF before it finds H? */
2457 h->u.weakdef->ref_regular = 1;
2459 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2463 /* If a symbol has no type and no size and does not require a PLT
2464 entry, then we are probably about to do the wrong thing here: we
2465 are probably going to create a COPY reloc for an empty object.
2466 This case can arise when a shared object is built with assembly
2467 code, and the assembly code fails to set the symbol type. */
2469 && h->type == STT_NOTYPE
2471 (*_bfd_error_handler)
2472 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2473 h->root.root.string);
2475 dynobj = elf_hash_table (eif->info)->dynobj;
2476 bed = get_elf_backend_data (dynobj);
2477 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2486 /* Adjust all external symbols pointing into SEC_MERGE sections
2487 to reflect the object merging within the sections. */
2490 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2494 if (h->root.type == bfd_link_hash_warning)
2495 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2497 if ((h->root.type == bfd_link_hash_defined
2498 || h->root.type == bfd_link_hash_defweak)
2499 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2500 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2502 bfd *output_bfd = data;
2504 h->root.u.def.value =
2505 _bfd_merged_section_offset (output_bfd,
2506 &h->root.u.def.section,
2507 elf_section_data (sec)->sec_info,
2508 h->root.u.def.value);
2514 /* Returns false if the symbol referred to by H should be considered
2515 to resolve local to the current module, and true if it should be
2516 considered to bind dynamically. */
2519 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2520 struct bfd_link_info *info,
2521 bfd_boolean ignore_protected)
2523 bfd_boolean binding_stays_local_p;
2528 while (h->root.type == bfd_link_hash_indirect
2529 || h->root.type == bfd_link_hash_warning)
2530 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2532 /* If it was forced local, then clearly it's not dynamic. */
2533 if (h->dynindx == -1)
2535 if (h->forced_local)
2538 /* Identify the cases where name binding rules say that a
2539 visible symbol resolves locally. */
2540 binding_stays_local_p = info->executable || info->symbolic;
2542 switch (ELF_ST_VISIBILITY (h->other))
2549 /* Proper resolution for function pointer equality may require
2550 that these symbols perhaps be resolved dynamically, even though
2551 we should be resolving them to the current module. */
2552 if (!ignore_protected || h->type != STT_FUNC)
2553 binding_stays_local_p = TRUE;
2560 /* If it isn't defined locally, then clearly it's dynamic. */
2561 if (!h->def_regular)
2564 /* Otherwise, the symbol is dynamic if binding rules don't tell
2565 us that it remains local. */
2566 return !binding_stays_local_p;
2569 /* Return true if the symbol referred to by H should be considered
2570 to resolve local to the current module, and false otherwise. Differs
2571 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2572 undefined symbols and weak symbols. */
2575 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2576 struct bfd_link_info *info,
2577 bfd_boolean local_protected)
2579 /* If it's a local sym, of course we resolve locally. */
2583 /* Common symbols that become definitions don't get the DEF_REGULAR
2584 flag set, so test it first, and don't bail out. */
2585 if (ELF_COMMON_DEF_P (h))
2587 /* If we don't have a definition in a regular file, then we can't
2588 resolve locally. The sym is either undefined or dynamic. */
2589 else if (!h->def_regular)
2592 /* Forced local symbols resolve locally. */
2593 if (h->forced_local)
2596 /* As do non-dynamic symbols. */
2597 if (h->dynindx == -1)
2600 /* At this point, we know the symbol is defined and dynamic. In an
2601 executable it must resolve locally, likewise when building symbolic
2602 shared libraries. */
2603 if (info->executable || info->symbolic)
2606 /* Now deal with defined dynamic symbols in shared libraries. Ones
2607 with default visibility might not resolve locally. */
2608 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2611 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2612 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2615 /* STV_PROTECTED non-function symbols are local. */
2616 if (h->type != STT_FUNC)
2619 /* Function pointer equality tests may require that STV_PROTECTED
2620 symbols be treated as dynamic symbols, even when we know that the
2621 dynamic linker will resolve them locally. */
2622 return local_protected;
2625 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2626 aligned. Returns the first TLS output section. */
2628 struct bfd_section *
2629 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2631 struct bfd_section *sec, *tls;
2632 unsigned int align = 0;
2634 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2635 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2639 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2640 if (sec->alignment_power > align)
2641 align = sec->alignment_power;
2643 elf_hash_table (info)->tls_sec = tls;
2645 /* Ensure the alignment of the first section is the largest alignment,
2646 so that the tls segment starts aligned. */
2648 tls->alignment_power = align;
2653 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2655 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2656 Elf_Internal_Sym *sym)
2658 const struct elf_backend_data *bed;
2660 /* Local symbols do not count, but target specific ones might. */
2661 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2662 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2665 /* Function symbols do not count. */
2666 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2669 /* If the section is undefined, then so is the symbol. */
2670 if (sym->st_shndx == SHN_UNDEF)
2673 /* If the symbol is defined in the common section, then
2674 it is a common definition and so does not count. */
2675 bed = get_elf_backend_data (abfd);
2676 if (bed->common_definition (sym))
2679 /* If the symbol is in a target specific section then we
2680 must rely upon the backend to tell us what it is. */
2681 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2682 /* FIXME - this function is not coded yet:
2684 return _bfd_is_global_symbol_definition (abfd, sym);
2686 Instead for now assume that the definition is not global,
2687 Even if this is wrong, at least the linker will behave
2688 in the same way that it used to do. */
2694 /* Search the symbol table of the archive element of the archive ABFD
2695 whose archive map contains a mention of SYMDEF, and determine if
2696 the symbol is defined in this element. */
2698 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2700 Elf_Internal_Shdr * hdr;
2701 bfd_size_type symcount;
2702 bfd_size_type extsymcount;
2703 bfd_size_type extsymoff;
2704 Elf_Internal_Sym *isymbuf;
2705 Elf_Internal_Sym *isym;
2706 Elf_Internal_Sym *isymend;
2709 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2713 if (! bfd_check_format (abfd, bfd_object))
2716 /* If we have already included the element containing this symbol in the
2717 link then we do not need to include it again. Just claim that any symbol
2718 it contains is not a definition, so that our caller will not decide to
2719 (re)include this element. */
2720 if (abfd->archive_pass)
2723 /* Select the appropriate symbol table. */
2724 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2725 hdr = &elf_tdata (abfd)->symtab_hdr;
2727 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2729 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2731 /* The sh_info field of the symtab header tells us where the
2732 external symbols start. We don't care about the local symbols. */
2733 if (elf_bad_symtab (abfd))
2735 extsymcount = symcount;
2740 extsymcount = symcount - hdr->sh_info;
2741 extsymoff = hdr->sh_info;
2744 if (extsymcount == 0)
2747 /* Read in the symbol table. */
2748 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2750 if (isymbuf == NULL)
2753 /* Scan the symbol table looking for SYMDEF. */
2755 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2759 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2764 if (strcmp (name, symdef->name) == 0)
2766 result = is_global_data_symbol_definition (abfd, isym);
2776 /* Add an entry to the .dynamic table. */
2779 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2783 struct elf_link_hash_table *hash_table;
2784 const struct elf_backend_data *bed;
2786 bfd_size_type newsize;
2787 bfd_byte *newcontents;
2788 Elf_Internal_Dyn dyn;
2790 hash_table = elf_hash_table (info);
2791 if (! is_elf_hash_table (hash_table))
2794 if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
2796 (_("warning: creating a DT_TEXTREL in a shared object."));
2798 bed = get_elf_backend_data (hash_table->dynobj);
2799 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2800 BFD_ASSERT (s != NULL);
2802 newsize = s->size + bed->s->sizeof_dyn;
2803 newcontents = bfd_realloc (s->contents, newsize);
2804 if (newcontents == NULL)
2808 dyn.d_un.d_val = val;
2809 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2812 s->contents = newcontents;
2817 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2818 otherwise just check whether one already exists. Returns -1 on error,
2819 1 if a DT_NEEDED tag already exists, and 0 on success. */
2822 elf_add_dt_needed_tag (bfd *abfd,
2823 struct bfd_link_info *info,
2827 struct elf_link_hash_table *hash_table;
2828 bfd_size_type oldsize;
2829 bfd_size_type strindex;
2831 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2834 hash_table = elf_hash_table (info);
2835 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2836 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2837 if (strindex == (bfd_size_type) -1)
2840 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2843 const struct elf_backend_data *bed;
2846 bed = get_elf_backend_data (hash_table->dynobj);
2847 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2849 for (extdyn = sdyn->contents;
2850 extdyn < sdyn->contents + sdyn->size;
2851 extdyn += bed->s->sizeof_dyn)
2853 Elf_Internal_Dyn dyn;
2855 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2856 if (dyn.d_tag == DT_NEEDED
2857 && dyn.d_un.d_val == strindex)
2859 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2867 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
2870 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2874 /* We were just checking for existence of the tag. */
2875 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2880 /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols
2881 belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no
2882 references from regular objects to these symbols.
2884 ??? Should we do something about references from other dynamic
2885 obects? If not, we potentially lose some warnings about undefined
2886 symbols. But how can we recover the initial undefined / undefweak
2889 struct elf_smash_syms_data
2892 struct elf_link_hash_table *htab;
2893 bfd_boolean twiddled;
2897 elf_smash_syms (struct elf_link_hash_entry *h, void *data)
2899 struct elf_smash_syms_data *inf = (struct elf_smash_syms_data *) data;
2900 struct bfd_link_hash_entry *bh;
2902 switch (h->root.type)
2905 case bfd_link_hash_new:
2908 case bfd_link_hash_undefined:
2909 if (h->root.u.undef.abfd != inf->not_needed)
2911 if (h->root.u.undef.weak != NULL
2912 && h->root.u.undef.weak != inf->not_needed)
2914 /* Symbol was undefweak in u.undef.weak bfd, and has become
2915 undefined in as-needed lib. Restore weak. */
2916 h->root.type = bfd_link_hash_undefweak;
2917 h->root.u.undef.abfd = h->root.u.undef.weak;
2918 if (h->root.u.undef.next != NULL
2919 || inf->htab->root.undefs_tail == &h->root)
2920 inf->twiddled = TRUE;
2925 case bfd_link_hash_undefweak:
2926 if (h->root.u.undef.abfd != inf->not_needed)
2930 case bfd_link_hash_defined:
2931 case bfd_link_hash_defweak:
2932 if (h->root.u.def.section->owner != inf->not_needed)
2936 case bfd_link_hash_common:
2937 if (h->root.u.c.p->section->owner != inf->not_needed)
2941 case bfd_link_hash_warning:
2942 case bfd_link_hash_indirect:
2943 elf_smash_syms ((struct elf_link_hash_entry *) h->root.u.i.link, data);
2944 if (h->root.u.i.link->type != bfd_link_hash_new)
2946 if (h->root.u.i.link->u.undef.abfd != inf->not_needed)
2951 /* There is no way we can undo symbol table state from defined or
2952 defweak back to undefined. */
2956 /* Set sym back to newly created state, but keep undef.next if it is
2957 being used as a list pointer. */
2958 bh = h->root.u.undef.next;
2961 if (bh != NULL || inf->htab->root.undefs_tail == &h->root)
2962 inf->twiddled = TRUE;
2963 (*inf->htab->root.table.newfunc) (&h->root.root,
2964 &inf->htab->root.table,
2965 h->root.root.string);
2966 h->root.u.undef.next = bh;
2967 h->root.u.undef.abfd = inf->not_needed;
2972 /* Sort symbol by value and section. */
2974 elf_sort_symbol (const void *arg1, const void *arg2)
2976 const struct elf_link_hash_entry *h1;
2977 const struct elf_link_hash_entry *h2;
2978 bfd_signed_vma vdiff;
2980 h1 = *(const struct elf_link_hash_entry **) arg1;
2981 h2 = *(const struct elf_link_hash_entry **) arg2;
2982 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2984 return vdiff > 0 ? 1 : -1;
2987 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2989 return sdiff > 0 ? 1 : -1;
2994 /* This function is used to adjust offsets into .dynstr for
2995 dynamic symbols. This is called via elf_link_hash_traverse. */
2998 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3000 struct elf_strtab_hash *dynstr = data;
3002 if (h->root.type == bfd_link_hash_warning)
3003 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3005 if (h->dynindx != -1)
3006 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3010 /* Assign string offsets in .dynstr, update all structures referencing
3014 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3016 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3017 struct elf_link_local_dynamic_entry *entry;
3018 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3019 bfd *dynobj = hash_table->dynobj;
3022 const struct elf_backend_data *bed;
3025 _bfd_elf_strtab_finalize (dynstr);
3026 size = _bfd_elf_strtab_size (dynstr);
3028 bed = get_elf_backend_data (dynobj);
3029 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3030 BFD_ASSERT (sdyn != NULL);
3032 /* Update all .dynamic entries referencing .dynstr strings. */
3033 for (extdyn = sdyn->contents;
3034 extdyn < sdyn->contents + sdyn->size;
3035 extdyn += bed->s->sizeof_dyn)
3037 Elf_Internal_Dyn dyn;
3039 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3043 dyn.d_un.d_val = size;
3051 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3056 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3059 /* Now update local dynamic symbols. */
3060 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3061 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3062 entry->isym.st_name);
3064 /* And the rest of dynamic symbols. */
3065 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3067 /* Adjust version definitions. */
3068 if (elf_tdata (output_bfd)->cverdefs)
3073 Elf_Internal_Verdef def;
3074 Elf_Internal_Verdaux defaux;
3076 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3080 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3082 p += sizeof (Elf_External_Verdef);
3083 if (def.vd_aux != sizeof (Elf_External_Verdef))
3085 for (i = 0; i < def.vd_cnt; ++i)
3087 _bfd_elf_swap_verdaux_in (output_bfd,
3088 (Elf_External_Verdaux *) p, &defaux);
3089 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3091 _bfd_elf_swap_verdaux_out (output_bfd,
3092 &defaux, (Elf_External_Verdaux *) p);
3093 p += sizeof (Elf_External_Verdaux);
3096 while (def.vd_next);
3099 /* Adjust version references. */
3100 if (elf_tdata (output_bfd)->verref)
3105 Elf_Internal_Verneed need;
3106 Elf_Internal_Vernaux needaux;
3108 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3112 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3114 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3115 _bfd_elf_swap_verneed_out (output_bfd, &need,
3116 (Elf_External_Verneed *) p);
3117 p += sizeof (Elf_External_Verneed);
3118 for (i = 0; i < need.vn_cnt; ++i)
3120 _bfd_elf_swap_vernaux_in (output_bfd,
3121 (Elf_External_Vernaux *) p, &needaux);
3122 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3124 _bfd_elf_swap_vernaux_out (output_bfd,
3126 (Elf_External_Vernaux *) p);
3127 p += sizeof (Elf_External_Vernaux);
3130 while (need.vn_next);
3136 /* Add symbols from an ELF object file to the linker hash table. */
3139 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3141 bfd_boolean (*add_symbol_hook)
3142 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
3143 const char **, flagword *, asection **, bfd_vma *);
3144 bfd_boolean (*check_relocs)
3145 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
3146 bfd_boolean (*check_directives)
3147 (bfd *, struct bfd_link_info *);
3148 bfd_boolean collect;
3149 Elf_Internal_Shdr *hdr;
3150 bfd_size_type symcount;
3151 bfd_size_type extsymcount;
3152 bfd_size_type extsymoff;
3153 struct elf_link_hash_entry **sym_hash;
3154 bfd_boolean dynamic;
3155 Elf_External_Versym *extversym = NULL;
3156 Elf_External_Versym *ever;
3157 struct elf_link_hash_entry *weaks;
3158 struct elf_link_hash_entry **nondeflt_vers = NULL;
3159 bfd_size_type nondeflt_vers_cnt = 0;
3160 Elf_Internal_Sym *isymbuf = NULL;
3161 Elf_Internal_Sym *isym;
3162 Elf_Internal_Sym *isymend;
3163 const struct elf_backend_data *bed;
3164 bfd_boolean add_needed;
3165 struct elf_link_hash_table * hash_table;
3168 hash_table = elf_hash_table (info);
3170 bed = get_elf_backend_data (abfd);
3171 add_symbol_hook = bed->elf_add_symbol_hook;
3172 collect = bed->collect;
3174 if ((abfd->flags & DYNAMIC) == 0)
3180 /* You can't use -r against a dynamic object. Also, there's no
3181 hope of using a dynamic object which does not exactly match
3182 the format of the output file. */
3183 if (info->relocatable
3184 || !is_elf_hash_table (hash_table)
3185 || hash_table->root.creator != abfd->xvec)
3187 if (info->relocatable)
3188 bfd_set_error (bfd_error_invalid_operation);
3190 bfd_set_error (bfd_error_wrong_format);
3195 /* As a GNU extension, any input sections which are named
3196 .gnu.warning.SYMBOL are treated as warning symbols for the given
3197 symbol. This differs from .gnu.warning sections, which generate
3198 warnings when they are included in an output file. */
3199 if (info->executable)
3203 for (s = abfd->sections; s != NULL; s = s->next)
3207 name = bfd_get_section_name (abfd, s);
3208 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3213 name += sizeof ".gnu.warning." - 1;
3215 /* If this is a shared object, then look up the symbol
3216 in the hash table. If it is there, and it is already
3217 been defined, then we will not be using the entry
3218 from this shared object, so we don't need to warn.
3219 FIXME: If we see the definition in a regular object
3220 later on, we will warn, but we shouldn't. The only
3221 fix is to keep track of what warnings we are supposed
3222 to emit, and then handle them all at the end of the
3226 struct elf_link_hash_entry *h;
3228 h = elf_link_hash_lookup (hash_table, name,
3229 FALSE, FALSE, TRUE);
3231 /* FIXME: What about bfd_link_hash_common? */
3233 && (h->root.type == bfd_link_hash_defined
3234 || h->root.type == bfd_link_hash_defweak))
3236 /* We don't want to issue this warning. Clobber
3237 the section size so that the warning does not
3238 get copied into the output file. */
3245 msg = bfd_alloc (abfd, sz + 1);
3249 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3254 if (! (_bfd_generic_link_add_one_symbol
3255 (info, abfd, name, BSF_WARNING, s, 0, msg,
3256 FALSE, collect, NULL)))
3259 if (! info->relocatable)
3261 /* Clobber the section size so that the warning does
3262 not get copied into the output file. */
3265 /* Also set SEC_EXCLUDE, so that symbols defined in
3266 the warning section don't get copied to the output. */
3267 s->flags |= SEC_EXCLUDE;
3276 /* If we are creating a shared library, create all the dynamic
3277 sections immediately. We need to attach them to something,
3278 so we attach them to this BFD, provided it is the right
3279 format. FIXME: If there are no input BFD's of the same
3280 format as the output, we can't make a shared library. */
3282 && is_elf_hash_table (hash_table)
3283 && hash_table->root.creator == abfd->xvec
3284 && ! hash_table->dynamic_sections_created)
3286 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3290 else if (!is_elf_hash_table (hash_table))
3295 const char *soname = NULL;
3296 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3299 /* ld --just-symbols and dynamic objects don't mix very well.
3300 Test for --just-symbols by looking at info set up by
3301 _bfd_elf_link_just_syms. */
3302 if ((s = abfd->sections) != NULL
3303 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3306 /* If this dynamic lib was specified on the command line with
3307 --as-needed in effect, then we don't want to add a DT_NEEDED
3308 tag unless the lib is actually used. Similary for libs brought
3309 in by another lib's DT_NEEDED. When --no-add-needed is used
3310 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3311 any dynamic library in DT_NEEDED tags in the dynamic lib at
3313 add_needed = (elf_dyn_lib_class (abfd)
3314 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3315 | DYN_NO_NEEDED)) == 0;
3317 s = bfd_get_section_by_name (abfd, ".dynamic");
3323 unsigned long shlink;
3325 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3326 goto error_free_dyn;
3328 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3330 goto error_free_dyn;
3331 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3333 for (extdyn = dynbuf;
3334 extdyn < dynbuf + s->size;
3335 extdyn += bed->s->sizeof_dyn)
3337 Elf_Internal_Dyn dyn;
3339 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3340 if (dyn.d_tag == DT_SONAME)
3342 unsigned int tagv = dyn.d_un.d_val;
3343 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3345 goto error_free_dyn;
3347 if (dyn.d_tag == DT_NEEDED)
3349 struct bfd_link_needed_list *n, **pn;
3351 unsigned int tagv = dyn.d_un.d_val;
3353 amt = sizeof (struct bfd_link_needed_list);
3354 n = bfd_alloc (abfd, amt);
3355 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3356 if (n == NULL || fnm == NULL)
3357 goto error_free_dyn;
3358 amt = strlen (fnm) + 1;
3359 anm = bfd_alloc (abfd, amt);
3361 goto error_free_dyn;
3362 memcpy (anm, fnm, amt);
3366 for (pn = & hash_table->needed;
3372 if (dyn.d_tag == DT_RUNPATH)
3374 struct bfd_link_needed_list *n, **pn;
3376 unsigned int tagv = dyn.d_un.d_val;
3378 amt = sizeof (struct bfd_link_needed_list);
3379 n = bfd_alloc (abfd, amt);
3380 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3381 if (n == NULL || fnm == NULL)
3382 goto error_free_dyn;
3383 amt = strlen (fnm) + 1;
3384 anm = bfd_alloc (abfd, amt);
3386 goto error_free_dyn;
3387 memcpy (anm, fnm, amt);
3391 for (pn = & runpath;
3397 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3398 if (!runpath && dyn.d_tag == DT_RPATH)
3400 struct bfd_link_needed_list *n, **pn;
3402 unsigned int tagv = dyn.d_un.d_val;
3404 amt = sizeof (struct bfd_link_needed_list);
3405 n = bfd_alloc (abfd, amt);
3406 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3407 if (n == NULL || fnm == NULL)
3408 goto error_free_dyn;
3409 amt = strlen (fnm) + 1;
3410 anm = bfd_alloc (abfd, amt);
3417 memcpy (anm, fnm, amt);
3432 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3433 frees all more recently bfd_alloc'd blocks as well. */
3439 struct bfd_link_needed_list **pn;
3440 for (pn = & hash_table->runpath;
3447 /* We do not want to include any of the sections in a dynamic
3448 object in the output file. We hack by simply clobbering the
3449 list of sections in the BFD. This could be handled more
3450 cleanly by, say, a new section flag; the existing
3451 SEC_NEVER_LOAD flag is not the one we want, because that one
3452 still implies that the section takes up space in the output
3454 bfd_section_list_clear (abfd);
3456 /* Find the name to use in a DT_NEEDED entry that refers to this
3457 object. If the object has a DT_SONAME entry, we use it.
3458 Otherwise, if the generic linker stuck something in
3459 elf_dt_name, we use that. Otherwise, we just use the file
3461 if (soname == NULL || *soname == '\0')
3463 soname = elf_dt_name (abfd);
3464 if (soname == NULL || *soname == '\0')
3465 soname = bfd_get_filename (abfd);
3468 /* Save the SONAME because sometimes the linker emulation code
3469 will need to know it. */
3470 elf_dt_name (abfd) = soname;
3472 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3476 /* If we have already included this dynamic object in the
3477 link, just ignore it. There is no reason to include a
3478 particular dynamic object more than once. */
3483 /* If this is a dynamic object, we always link against the .dynsym
3484 symbol table, not the .symtab symbol table. The dynamic linker
3485 will only see the .dynsym symbol table, so there is no reason to
3486 look at .symtab for a dynamic object. */
3488 if (! dynamic || elf_dynsymtab (abfd) == 0)
3489 hdr = &elf_tdata (abfd)->symtab_hdr;
3491 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3493 symcount = hdr->sh_size / bed->s->sizeof_sym;
3495 /* The sh_info field of the symtab header tells us where the
3496 external symbols start. We don't care about the local symbols at
3498 if (elf_bad_symtab (abfd))
3500 extsymcount = symcount;
3505 extsymcount = symcount - hdr->sh_info;
3506 extsymoff = hdr->sh_info;
3510 if (extsymcount != 0)
3512 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3514 if (isymbuf == NULL)
3517 /* We store a pointer to the hash table entry for each external
3519 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3520 sym_hash = bfd_alloc (abfd, amt);
3521 if (sym_hash == NULL)
3522 goto error_free_sym;
3523 elf_sym_hashes (abfd) = sym_hash;
3528 /* Read in any version definitions. */
3529 if (!_bfd_elf_slurp_version_tables (abfd,
3530 info->default_imported_symver))
3531 goto error_free_sym;
3533 /* Read in the symbol versions, but don't bother to convert them
3534 to internal format. */
3535 if (elf_dynversym (abfd) != 0)
3537 Elf_Internal_Shdr *versymhdr;
3539 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3540 extversym = bfd_malloc (versymhdr->sh_size);
3541 if (extversym == NULL)
3542 goto error_free_sym;
3543 amt = versymhdr->sh_size;
3544 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3545 || bfd_bread (extversym, amt, abfd) != amt)
3546 goto error_free_vers;
3552 ever = extversym != NULL ? extversym + extsymoff : NULL;
3553 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3555 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3559 asection *sec, *new_sec;
3562 struct elf_link_hash_entry *h;
3563 bfd_boolean definition;
3564 bfd_boolean size_change_ok;
3565 bfd_boolean type_change_ok;
3566 bfd_boolean new_weakdef;
3567 bfd_boolean override;
3569 unsigned int old_alignment;
3574 flags = BSF_NO_FLAGS;
3576 value = isym->st_value;
3578 common = bed->common_definition (isym);
3580 bind = ELF_ST_BIND (isym->st_info);
3581 if (bind == STB_LOCAL)
3583 /* This should be impossible, since ELF requires that all
3584 global symbols follow all local symbols, and that sh_info
3585 point to the first global symbol. Unfortunately, Irix 5
3589 else if (bind == STB_GLOBAL)
3591 if (isym->st_shndx != SHN_UNDEF && !common)
3594 else if (bind == STB_WEAK)
3598 /* Leave it up to the processor backend. */
3601 if (isym->st_shndx == SHN_UNDEF)
3602 sec = bfd_und_section_ptr;
3603 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3605 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3607 sec = bfd_abs_section_ptr;
3608 else if (sec->kept_section)
3610 /* Symbols from discarded section are undefined, and have
3611 default visibility. */
3612 sec = bfd_und_section_ptr;
3613 isym->st_shndx = SHN_UNDEF;
3614 isym->st_other = STV_DEFAULT
3615 | (isym->st_other & ~ ELF_ST_VISIBILITY(-1));
3617 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3620 else if (isym->st_shndx == SHN_ABS)
3621 sec = bfd_abs_section_ptr;
3622 else if (isym->st_shndx == SHN_COMMON)
3624 sec = bfd_com_section_ptr;
3625 /* What ELF calls the size we call the value. What ELF
3626 calls the value we call the alignment. */
3627 value = isym->st_size;
3631 /* Leave it up to the processor backend. */
3634 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3637 goto error_free_vers;
3639 if (isym->st_shndx == SHN_COMMON
3640 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3642 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3646 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3649 | SEC_LINKER_CREATED
3650 | SEC_THREAD_LOCAL));
3652 goto error_free_vers;
3656 else if (add_symbol_hook)
3658 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3660 goto error_free_vers;
3662 /* The hook function sets the name to NULL if this symbol
3663 should be skipped for some reason. */
3668 /* Sanity check that all possibilities were handled. */
3671 bfd_set_error (bfd_error_bad_value);
3672 goto error_free_vers;
3675 if (bfd_is_und_section (sec)
3676 || bfd_is_com_section (sec))
3681 size_change_ok = FALSE;
3682 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3687 if (is_elf_hash_table (hash_table))
3689 Elf_Internal_Versym iver;
3690 unsigned int vernum = 0;
3695 if (info->default_imported_symver)
3696 /* Use the default symbol version created earlier. */
3697 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3702 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3704 vernum = iver.vs_vers & VERSYM_VERSION;
3706 /* If this is a hidden symbol, or if it is not version
3707 1, we append the version name to the symbol name.
3708 However, we do not modify a non-hidden absolute symbol
3709 if it is not a function, because it might be the version
3710 symbol itself. FIXME: What if it isn't? */
3711 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3712 || (vernum > 1 && (! bfd_is_abs_section (sec)
3713 || ELF_ST_TYPE (isym->st_info) == STT_FUNC)))
3716 size_t namelen, verlen, newlen;
3719 if (isym->st_shndx != SHN_UNDEF)
3721 if (vernum > elf_tdata (abfd)->cverdefs)
3723 else if (vernum > 1)
3725 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3731 (*_bfd_error_handler)
3732 (_("%B: %s: invalid version %u (max %d)"),
3734 elf_tdata (abfd)->cverdefs);
3735 bfd_set_error (bfd_error_bad_value);
3736 goto error_free_vers;
3741 /* We cannot simply test for the number of
3742 entries in the VERNEED section since the
3743 numbers for the needed versions do not start
3745 Elf_Internal_Verneed *t;
3748 for (t = elf_tdata (abfd)->verref;
3752 Elf_Internal_Vernaux *a;
3754 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3756 if (a->vna_other == vernum)
3758 verstr = a->vna_nodename;
3767 (*_bfd_error_handler)
3768 (_("%B: %s: invalid needed version %d"),
3769 abfd, name, vernum);
3770 bfd_set_error (bfd_error_bad_value);
3771 goto error_free_vers;
3775 namelen = strlen (name);
3776 verlen = strlen (verstr);
3777 newlen = namelen + verlen + 2;
3778 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3779 && isym->st_shndx != SHN_UNDEF)
3782 newname = bfd_alloc (abfd, newlen);
3783 if (newname == NULL)
3784 goto error_free_vers;
3785 memcpy (newname, name, namelen);
3786 p = newname + namelen;
3788 /* If this is a defined non-hidden version symbol,
3789 we add another @ to the name. This indicates the
3790 default version of the symbol. */
3791 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3792 && isym->st_shndx != SHN_UNDEF)
3794 memcpy (p, verstr, verlen + 1);
3799 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3800 &value, &old_alignment,
3801 sym_hash, &skip, &override,
3802 &type_change_ok, &size_change_ok))
3803 goto error_free_vers;
3812 while (h->root.type == bfd_link_hash_indirect
3813 || h->root.type == bfd_link_hash_warning)
3814 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3816 /* Remember the old alignment if this is a common symbol, so
3817 that we don't reduce the alignment later on. We can't
3818 check later, because _bfd_generic_link_add_one_symbol
3819 will set a default for the alignment which we want to
3820 override. We also remember the old bfd where the existing
3821 definition comes from. */
3822 switch (h->root.type)
3827 case bfd_link_hash_defined:
3828 case bfd_link_hash_defweak:
3829 old_bfd = h->root.u.def.section->owner;
3832 case bfd_link_hash_common:
3833 old_bfd = h->root.u.c.p->section->owner;
3834 old_alignment = h->root.u.c.p->alignment_power;
3838 if (elf_tdata (abfd)->verdef != NULL
3842 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3845 if (! (_bfd_generic_link_add_one_symbol
3846 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3847 (struct bfd_link_hash_entry **) sym_hash)))
3848 goto error_free_vers;
3851 while (h->root.type == bfd_link_hash_indirect
3852 || h->root.type == bfd_link_hash_warning)
3853 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3856 new_weakdef = FALSE;
3859 && (flags & BSF_WEAK) != 0
3860 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3861 && is_elf_hash_table (hash_table)
3862 && h->u.weakdef == NULL)
3864 /* Keep a list of all weak defined non function symbols from
3865 a dynamic object, using the weakdef field. Later in this
3866 function we will set the weakdef field to the correct
3867 value. We only put non-function symbols from dynamic
3868 objects on this list, because that happens to be the only
3869 time we need to know the normal symbol corresponding to a
3870 weak symbol, and the information is time consuming to
3871 figure out. If the weakdef field is not already NULL,
3872 then this symbol was already defined by some previous
3873 dynamic object, and we will be using that previous
3874 definition anyhow. */
3876 h->u.weakdef = weaks;
3881 /* Set the alignment of a common symbol. */
3882 if ((common || bfd_is_com_section (sec))
3883 && h->root.type == bfd_link_hash_common)
3888 align = bfd_log2 (isym->st_value);
3891 /* The new symbol is a common symbol in a shared object.
3892 We need to get the alignment from the section. */
3893 align = new_sec->alignment_power;
3895 if (align > old_alignment
3896 /* Permit an alignment power of zero if an alignment of one
3897 is specified and no other alignments have been specified. */
3898 || (isym->st_value == 1 && old_alignment == 0))
3899 h->root.u.c.p->alignment_power = align;
3901 h->root.u.c.p->alignment_power = old_alignment;
3904 if (is_elf_hash_table (hash_table))
3908 /* Check the alignment when a common symbol is involved. This
3909 can change when a common symbol is overridden by a normal
3910 definition or a common symbol is ignored due to the old
3911 normal definition. We need to make sure the maximum
3912 alignment is maintained. */
3913 if ((old_alignment || common)
3914 && h->root.type != bfd_link_hash_common)
3916 unsigned int common_align;
3917 unsigned int normal_align;
3918 unsigned int symbol_align;
3922 symbol_align = ffs (h->root.u.def.value) - 1;
3923 if (h->root.u.def.section->owner != NULL
3924 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3926 normal_align = h->root.u.def.section->alignment_power;
3927 if (normal_align > symbol_align)
3928 normal_align = symbol_align;
3931 normal_align = symbol_align;
3935 common_align = old_alignment;
3936 common_bfd = old_bfd;
3941 common_align = bfd_log2 (isym->st_value);
3943 normal_bfd = old_bfd;
3946 if (normal_align < common_align)
3947 (*_bfd_error_handler)
3948 (_("Warning: alignment %u of symbol `%s' in %B"
3949 " is smaller than %u in %B"),
3950 normal_bfd, common_bfd,
3951 1 << normal_align, name, 1 << common_align);
3954 /* Remember the symbol size and type. */
3955 if (isym->st_size != 0
3956 && (definition || h->size == 0))
3958 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3959 (*_bfd_error_handler)
3960 (_("Warning: size of symbol `%s' changed"
3961 " from %lu in %B to %lu in %B"),
3963 name, (unsigned long) h->size,
3964 (unsigned long) isym->st_size);
3966 h->size = isym->st_size;
3969 /* If this is a common symbol, then we always want H->SIZE
3970 to be the size of the common symbol. The code just above
3971 won't fix the size if a common symbol becomes larger. We
3972 don't warn about a size change here, because that is
3973 covered by --warn-common. */
3974 if (h->root.type == bfd_link_hash_common)
3975 h->size = h->root.u.c.size;
3977 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3978 && (definition || h->type == STT_NOTYPE))
3980 if (h->type != STT_NOTYPE
3981 && h->type != ELF_ST_TYPE (isym->st_info)
3982 && ! type_change_ok)
3983 (*_bfd_error_handler)
3984 (_("Warning: type of symbol `%s' changed"
3985 " from %d to %d in %B"),
3986 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3988 h->type = ELF_ST_TYPE (isym->st_info);
3991 /* If st_other has a processor-specific meaning, specific
3992 code might be needed here. We never merge the visibility
3993 attribute with the one from a dynamic object. */
3994 if (bed->elf_backend_merge_symbol_attribute)
3995 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3998 /* If this symbol has default visibility and the user has requested
3999 we not re-export it, then mark it as hidden. */
4000 if (definition && !dynamic
4002 || (abfd->my_archive && abfd->my_archive->no_export))
4003 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4004 isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1));
4006 if (isym->st_other != 0 && !dynamic)
4008 unsigned char hvis, symvis, other, nvis;
4010 /* Take the balance of OTHER from the definition. */
4011 other = (definition ? isym->st_other : h->other);
4012 other &= ~ ELF_ST_VISIBILITY (-1);
4014 /* Combine visibilities, using the most constraining one. */
4015 hvis = ELF_ST_VISIBILITY (h->other);
4016 symvis = ELF_ST_VISIBILITY (isym->st_other);
4022 nvis = hvis < symvis ? hvis : symvis;
4024 h->other = other | nvis;
4027 /* Set a flag in the hash table entry indicating the type of
4028 reference or definition we just found. Keep a count of
4029 the number of dynamic symbols we find. A dynamic symbol
4030 is one which is referenced or defined by both a regular
4031 object and a shared object. */
4038 if (bind != STB_WEAK)
4039 h->ref_regular_nonweak = 1;
4043 if (! info->executable
4056 || (h->u.weakdef != NULL
4058 && h->u.weakdef->dynindx != -1))
4062 /* Check to see if we need to add an indirect symbol for
4063 the default name. */
4064 if (definition || h->root.type == bfd_link_hash_common)
4065 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4066 &sec, &value, &dynsym,
4068 goto error_free_vers;
4070 if (definition && !dynamic)
4072 char *p = strchr (name, ELF_VER_CHR);
4073 if (p != NULL && p[1] != ELF_VER_CHR)
4075 /* Queue non-default versions so that .symver x, x@FOO
4076 aliases can be checked. */
4077 if (! nondeflt_vers)
4079 amt = (isymend - isym + 1)
4080 * sizeof (struct elf_link_hash_entry *);
4081 nondeflt_vers = bfd_malloc (amt);
4083 nondeflt_vers [nondeflt_vers_cnt++] = h;
4087 if (dynsym && h->dynindx == -1)
4089 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4090 goto error_free_vers;
4091 if (h->u.weakdef != NULL
4093 && h->u.weakdef->dynindx == -1)
4095 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4096 goto error_free_vers;
4099 else if (dynsym && h->dynindx != -1)
4100 /* If the symbol already has a dynamic index, but
4101 visibility says it should not be visible, turn it into
4103 switch (ELF_ST_VISIBILITY (h->other))
4107 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4118 const char *soname = elf_dt_name (abfd);
4120 /* A symbol from a library loaded via DT_NEEDED of some
4121 other library is referenced by a regular object.
4122 Add a DT_NEEDED entry for it. Issue an error if
4123 --no-add-needed is used. */
4124 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4126 (*_bfd_error_handler)
4127 (_("%s: invalid DSO for symbol `%s' definition"),
4129 bfd_set_error (bfd_error_bad_value);
4130 goto error_free_vers;
4133 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4136 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4138 goto error_free_vers;
4140 BFD_ASSERT (ret == 0);
4145 /* Now that all the symbols from this input file are created, handle
4146 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4147 if (nondeflt_vers != NULL)
4149 bfd_size_type cnt, symidx;
4151 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4153 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4154 char *shortname, *p;
4156 p = strchr (h->root.root.string, ELF_VER_CHR);
4158 || (h->root.type != bfd_link_hash_defined
4159 && h->root.type != bfd_link_hash_defweak))
4162 amt = p - h->root.root.string;
4163 shortname = bfd_malloc (amt + 1);
4164 memcpy (shortname, h->root.root.string, amt);
4165 shortname[amt] = '\0';
4167 hi = (struct elf_link_hash_entry *)
4168 bfd_link_hash_lookup (&hash_table->root, shortname,
4169 FALSE, FALSE, FALSE);
4171 && hi->root.type == h->root.type
4172 && hi->root.u.def.value == h->root.u.def.value
4173 && hi->root.u.def.section == h->root.u.def.section)
4175 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4176 hi->root.type = bfd_link_hash_indirect;
4177 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4178 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
4179 sym_hash = elf_sym_hashes (abfd);
4181 for (symidx = 0; symidx < extsymcount; ++symidx)
4182 if (sym_hash[symidx] == hi)
4184 sym_hash[symidx] = h;
4190 free (nondeflt_vers);
4191 nondeflt_vers = NULL;
4194 if (extversym != NULL)
4200 if (isymbuf != NULL)
4205 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4207 /* Remove symbols defined in an as-needed shared lib that wasn't
4209 struct elf_smash_syms_data inf;
4210 inf.not_needed = abfd;
4211 inf.htab = hash_table;
4212 inf.twiddled = FALSE;
4213 elf_link_hash_traverse (hash_table, elf_smash_syms, &inf);
4215 bfd_link_repair_undef_list (&hash_table->root);
4219 /* Now set the weakdefs field correctly for all the weak defined
4220 symbols we found. The only way to do this is to search all the
4221 symbols. Since we only need the information for non functions in
4222 dynamic objects, that's the only time we actually put anything on
4223 the list WEAKS. We need this information so that if a regular
4224 object refers to a symbol defined weakly in a dynamic object, the
4225 real symbol in the dynamic object is also put in the dynamic
4226 symbols; we also must arrange for both symbols to point to the
4227 same memory location. We could handle the general case of symbol
4228 aliasing, but a general symbol alias can only be generated in
4229 assembler code, handling it correctly would be very time
4230 consuming, and other ELF linkers don't handle general aliasing
4234 struct elf_link_hash_entry **hpp;
4235 struct elf_link_hash_entry **hppend;
4236 struct elf_link_hash_entry **sorted_sym_hash;
4237 struct elf_link_hash_entry *h;
4240 /* Since we have to search the whole symbol list for each weak
4241 defined symbol, search time for N weak defined symbols will be
4242 O(N^2). Binary search will cut it down to O(NlogN). */
4243 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4244 sorted_sym_hash = bfd_malloc (amt);
4245 if (sorted_sym_hash == NULL)
4247 sym_hash = sorted_sym_hash;
4248 hpp = elf_sym_hashes (abfd);
4249 hppend = hpp + extsymcount;
4251 for (; hpp < hppend; hpp++)
4255 && h->root.type == bfd_link_hash_defined
4256 && h->type != STT_FUNC)
4264 qsort (sorted_sym_hash, sym_count,
4265 sizeof (struct elf_link_hash_entry *),
4268 while (weaks != NULL)
4270 struct elf_link_hash_entry *hlook;
4277 weaks = hlook->u.weakdef;
4278 hlook->u.weakdef = NULL;
4280 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4281 || hlook->root.type == bfd_link_hash_defweak
4282 || hlook->root.type == bfd_link_hash_common
4283 || hlook->root.type == bfd_link_hash_indirect);
4284 slook = hlook->root.u.def.section;
4285 vlook = hlook->root.u.def.value;
4292 bfd_signed_vma vdiff;
4294 h = sorted_sym_hash [idx];
4295 vdiff = vlook - h->root.u.def.value;
4302 long sdiff = slook->id - h->root.u.def.section->id;
4315 /* We didn't find a value/section match. */
4319 for (i = ilook; i < sym_count; i++)
4321 h = sorted_sym_hash [i];
4323 /* Stop if value or section doesn't match. */
4324 if (h->root.u.def.value != vlook
4325 || h->root.u.def.section != slook)
4327 else if (h != hlook)
4329 hlook->u.weakdef = h;
4331 /* If the weak definition is in the list of dynamic
4332 symbols, make sure the real definition is put
4334 if (hlook->dynindx != -1 && h->dynindx == -1)
4336 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4340 /* If the real definition is in the list of dynamic
4341 symbols, make sure the weak definition is put
4342 there as well. If we don't do this, then the
4343 dynamic loader might not merge the entries for the
4344 real definition and the weak definition. */
4345 if (h->dynindx != -1 && hlook->dynindx == -1)
4347 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4355 free (sorted_sym_hash);
4358 check_directives = get_elf_backend_data (abfd)->check_directives;
4359 if (check_directives)
4360 check_directives (abfd, info);
4362 /* If this object is the same format as the output object, and it is
4363 not a shared library, then let the backend look through the
4366 This is required to build global offset table entries and to
4367 arrange for dynamic relocs. It is not required for the
4368 particular common case of linking non PIC code, even when linking
4369 against shared libraries, but unfortunately there is no way of
4370 knowing whether an object file has been compiled PIC or not.
4371 Looking through the relocs is not particularly time consuming.
4372 The problem is that we must either (1) keep the relocs in memory,
4373 which causes the linker to require additional runtime memory or
4374 (2) read the relocs twice from the input file, which wastes time.
4375 This would be a good case for using mmap.
4377 I have no idea how to handle linking PIC code into a file of a
4378 different format. It probably can't be done. */
4379 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4381 && is_elf_hash_table (hash_table)
4382 && hash_table->root.creator == abfd->xvec
4383 && check_relocs != NULL)
4387 for (o = abfd->sections; o != NULL; o = o->next)
4389 Elf_Internal_Rela *internal_relocs;
4392 if ((o->flags & SEC_RELOC) == 0
4393 || o->reloc_count == 0
4394 || ((info->strip == strip_all || info->strip == strip_debugger)
4395 && (o->flags & SEC_DEBUGGING) != 0)
4396 || bfd_is_abs_section (o->output_section))
4399 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4401 if (internal_relocs == NULL)
4404 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4406 if (elf_section_data (o)->relocs != internal_relocs)
4407 free (internal_relocs);
4414 /* If this is a non-traditional link, try to optimize the handling
4415 of the .stab/.stabstr sections. */
4417 && ! info->traditional_format
4418 && is_elf_hash_table (hash_table)
4419 && (info->strip != strip_all && info->strip != strip_debugger))
4423 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4424 if (stabstr != NULL)
4426 bfd_size_type string_offset = 0;
4429 for (stab = abfd->sections; stab; stab = stab->next)
4430 if (strncmp (".stab", stab->name, 5) == 0
4431 && (!stab->name[5] ||
4432 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4433 && (stab->flags & SEC_MERGE) == 0
4434 && !bfd_is_abs_section (stab->output_section))
4436 struct bfd_elf_section_data *secdata;
4438 secdata = elf_section_data (stab);
4439 if (! _bfd_link_section_stabs (abfd,
4440 &hash_table->stab_info,
4445 if (secdata->sec_info)
4446 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4451 if (is_elf_hash_table (hash_table) && add_needed)
4453 /* Add this bfd to the loaded list. */
4454 struct elf_link_loaded_list *n;
4456 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4460 n->next = hash_table->loaded;
4461 hash_table->loaded = n;
4467 if (nondeflt_vers != NULL)
4468 free (nondeflt_vers);
4469 if (extversym != NULL)
4472 if (isymbuf != NULL)
4478 /* Return the linker hash table entry of a symbol that might be
4479 satisfied by an archive symbol. Return -1 on error. */
4481 struct elf_link_hash_entry *
4482 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4483 struct bfd_link_info *info,
4486 struct elf_link_hash_entry *h;
4490 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4494 /* If this is a default version (the name contains @@), look up the
4495 symbol again with only one `@' as well as without the version.
4496 The effect is that references to the symbol with and without the
4497 version will be matched by the default symbol in the archive. */
4499 p = strchr (name, ELF_VER_CHR);
4500 if (p == NULL || p[1] != ELF_VER_CHR)
4503 /* First check with only one `@'. */
4504 len = strlen (name);
4505 copy = bfd_alloc (abfd, len);
4507 return (struct elf_link_hash_entry *) 0 - 1;
4509 first = p - name + 1;
4510 memcpy (copy, name, first);
4511 memcpy (copy + first, name + first + 1, len - first);
4513 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4516 /* We also need to check references to the symbol without the
4518 copy[first - 1] = '\0';
4519 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4520 FALSE, FALSE, FALSE);
4523 bfd_release (abfd, copy);
4527 /* Add symbols from an ELF archive file to the linker hash table. We
4528 don't use _bfd_generic_link_add_archive_symbols because of a
4529 problem which arises on UnixWare. The UnixWare libc.so is an
4530 archive which includes an entry libc.so.1 which defines a bunch of
4531 symbols. The libc.so archive also includes a number of other
4532 object files, which also define symbols, some of which are the same
4533 as those defined in libc.so.1. Correct linking requires that we
4534 consider each object file in turn, and include it if it defines any
4535 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4536 this; it looks through the list of undefined symbols, and includes
4537 any object file which defines them. When this algorithm is used on
4538 UnixWare, it winds up pulling in libc.so.1 early and defining a
4539 bunch of symbols. This means that some of the other objects in the
4540 archive are not included in the link, which is incorrect since they
4541 precede libc.so.1 in the archive.
4543 Fortunately, ELF archive handling is simpler than that done by
4544 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4545 oddities. In ELF, if we find a symbol in the archive map, and the
4546 symbol is currently undefined, we know that we must pull in that
4549 Unfortunately, we do have to make multiple passes over the symbol
4550 table until nothing further is resolved. */
4553 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4556 bfd_boolean *defined = NULL;
4557 bfd_boolean *included = NULL;
4561 const struct elf_backend_data *bed;
4562 struct elf_link_hash_entry * (*archive_symbol_lookup)
4563 (bfd *, struct bfd_link_info *, const char *);
4565 if (! bfd_has_map (abfd))
4567 /* An empty archive is a special case. */
4568 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4570 bfd_set_error (bfd_error_no_armap);
4574 /* Keep track of all symbols we know to be already defined, and all
4575 files we know to be already included. This is to speed up the
4576 second and subsequent passes. */
4577 c = bfd_ardata (abfd)->symdef_count;
4581 amt *= sizeof (bfd_boolean);
4582 defined = bfd_zmalloc (amt);
4583 included = bfd_zmalloc (amt);
4584 if (defined == NULL || included == NULL)
4587 symdefs = bfd_ardata (abfd)->symdefs;
4588 bed = get_elf_backend_data (abfd);
4589 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4602 symdefend = symdef + c;
4603 for (i = 0; symdef < symdefend; symdef++, i++)
4605 struct elf_link_hash_entry *h;
4607 struct bfd_link_hash_entry *undefs_tail;
4610 if (defined[i] || included[i])
4612 if (symdef->file_offset == last)
4618 h = archive_symbol_lookup (abfd, info, symdef->name);
4619 if (h == (struct elf_link_hash_entry *) 0 - 1)
4625 if (h->root.type == bfd_link_hash_common)
4627 /* We currently have a common symbol. The archive map contains
4628 a reference to this symbol, so we may want to include it. We
4629 only want to include it however, if this archive element
4630 contains a definition of the symbol, not just another common
4633 Unfortunately some archivers (including GNU ar) will put
4634 declarations of common symbols into their archive maps, as
4635 well as real definitions, so we cannot just go by the archive
4636 map alone. Instead we must read in the element's symbol
4637 table and check that to see what kind of symbol definition
4639 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4642 else if (h->root.type != bfd_link_hash_undefined)
4644 if (h->root.type != bfd_link_hash_undefweak)
4649 /* We need to include this archive member. */
4650 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4651 if (element == NULL)
4654 if (! bfd_check_format (element, bfd_object))
4657 /* Doublecheck that we have not included this object
4658 already--it should be impossible, but there may be
4659 something wrong with the archive. */
4660 if (element->archive_pass != 0)
4662 bfd_set_error (bfd_error_bad_value);
4665 element->archive_pass = 1;
4667 undefs_tail = info->hash->undefs_tail;
4669 if (! (*info->callbacks->add_archive_element) (info, element,
4672 if (! bfd_link_add_symbols (element, info))
4675 /* If there are any new undefined symbols, we need to make
4676 another pass through the archive in order to see whether
4677 they can be defined. FIXME: This isn't perfect, because
4678 common symbols wind up on undefs_tail and because an
4679 undefined symbol which is defined later on in this pass
4680 does not require another pass. This isn't a bug, but it
4681 does make the code less efficient than it could be. */
4682 if (undefs_tail != info->hash->undefs_tail)
4685 /* Look backward to mark all symbols from this object file
4686 which we have already seen in this pass. */
4690 included[mark] = TRUE;
4695 while (symdefs[mark].file_offset == symdef->file_offset);
4697 /* We mark subsequent symbols from this object file as we go
4698 on through the loop. */
4699 last = symdef->file_offset;
4710 if (defined != NULL)
4712 if (included != NULL)
4717 /* Given an ELF BFD, add symbols to the global hash table as
4721 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4723 switch (bfd_get_format (abfd))
4726 return elf_link_add_object_symbols (abfd, info);
4728 return elf_link_add_archive_symbols (abfd, info);
4730 bfd_set_error (bfd_error_wrong_format);
4735 /* This function will be called though elf_link_hash_traverse to store
4736 all hash value of the exported symbols in an array. */
4739 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4741 unsigned long **valuep = data;
4747 if (h->root.type == bfd_link_hash_warning)
4748 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4750 /* Ignore indirect symbols. These are added by the versioning code. */
4751 if (h->dynindx == -1)
4754 name = h->root.root.string;
4755 p = strchr (name, ELF_VER_CHR);
4758 alc = bfd_malloc (p - name + 1);
4759 memcpy (alc, name, p - name);
4760 alc[p - name] = '\0';
4764 /* Compute the hash value. */
4765 ha = bfd_elf_hash (name);
4767 /* Store the found hash value in the array given as the argument. */
4770 /* And store it in the struct so that we can put it in the hash table
4772 h->u.elf_hash_value = ha;
4780 /* Array used to determine the number of hash table buckets to use
4781 based on the number of symbols there are. If there are fewer than
4782 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4783 fewer than 37 we use 17 buckets, and so forth. We never use more
4784 than 32771 buckets. */
4786 static const size_t elf_buckets[] =
4788 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4792 /* Compute bucket count for hashing table. We do not use a static set
4793 of possible tables sizes anymore. Instead we determine for all
4794 possible reasonable sizes of the table the outcome (i.e., the
4795 number of collisions etc) and choose the best solution. The
4796 weighting functions are not too simple to allow the table to grow
4797 without bounds. Instead one of the weighting factors is the size.
4798 Therefore the result is always a good payoff between few collisions
4799 (= short chain lengths) and table size. */
4801 compute_bucket_count (struct bfd_link_info *info)
4803 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4804 size_t best_size = 0;
4805 unsigned long int *hashcodes;
4806 unsigned long int *hashcodesp;
4807 unsigned long int i;
4810 /* Compute the hash values for all exported symbols. At the same
4811 time store the values in an array so that we could use them for
4814 amt *= sizeof (unsigned long int);
4815 hashcodes = bfd_malloc (amt);
4816 if (hashcodes == NULL)
4818 hashcodesp = hashcodes;
4820 /* Put all hash values in HASHCODES. */
4821 elf_link_hash_traverse (elf_hash_table (info),
4822 elf_collect_hash_codes, &hashcodesp);
4824 /* We have a problem here. The following code to optimize the table
4825 size requires an integer type with more the 32 bits. If
4826 BFD_HOST_U_64_BIT is set we know about such a type. */
4827 #ifdef BFD_HOST_U_64_BIT
4830 unsigned long int nsyms = hashcodesp - hashcodes;
4833 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4834 unsigned long int *counts ;
4835 bfd *dynobj = elf_hash_table (info)->dynobj;
4836 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4838 /* Possible optimization parameters: if we have NSYMS symbols we say
4839 that the hashing table must at least have NSYMS/4 and at most
4841 minsize = nsyms / 4;
4844 best_size = maxsize = nsyms * 2;
4846 /* Create array where we count the collisions in. We must use bfd_malloc
4847 since the size could be large. */
4849 amt *= sizeof (unsigned long int);
4850 counts = bfd_malloc (amt);
4857 /* Compute the "optimal" size for the hash table. The criteria is a
4858 minimal chain length. The minor criteria is (of course) the size
4860 for (i = minsize; i < maxsize; ++i)
4862 /* Walk through the array of hashcodes and count the collisions. */
4863 BFD_HOST_U_64_BIT max;
4864 unsigned long int j;
4865 unsigned long int fact;
4867 memset (counts, '\0', i * sizeof (unsigned long int));
4869 /* Determine how often each hash bucket is used. */
4870 for (j = 0; j < nsyms; ++j)
4871 ++counts[hashcodes[j] % i];
4873 /* For the weight function we need some information about the
4874 pagesize on the target. This is information need not be 100%
4875 accurate. Since this information is not available (so far) we
4876 define it here to a reasonable default value. If it is crucial
4877 to have a better value some day simply define this value. */
4878 # ifndef BFD_TARGET_PAGESIZE
4879 # define BFD_TARGET_PAGESIZE (4096)
4882 /* We in any case need 2 + NSYMS entries for the size values and
4884 max = (2 + nsyms) * (bed->s->arch_size / 8);
4887 /* Variant 1: optimize for short chains. We add the squares
4888 of all the chain lengths (which favors many small chain
4889 over a few long chains). */
4890 for (j = 0; j < i; ++j)
4891 max += counts[j] * counts[j];
4893 /* This adds penalties for the overall size of the table. */
4894 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4897 /* Variant 2: Optimize a lot more for small table. Here we
4898 also add squares of the size but we also add penalties for
4899 empty slots (the +1 term). */
4900 for (j = 0; j < i; ++j)
4901 max += (1 + counts[j]) * (1 + counts[j]);
4903 /* The overall size of the table is considered, but not as
4904 strong as in variant 1, where it is squared. */
4905 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4909 /* Compare with current best results. */
4910 if (max < best_chlen)
4920 #endif /* defined (BFD_HOST_U_64_BIT) */
4922 /* This is the fallback solution if no 64bit type is available or if we
4923 are not supposed to spend much time on optimizations. We select the
4924 bucket count using a fixed set of numbers. */
4925 for (i = 0; elf_buckets[i] != 0; i++)
4927 best_size = elf_buckets[i];
4928 if (dynsymcount < elf_buckets[i + 1])
4933 /* Free the arrays we needed. */
4939 /* Set up the sizes and contents of the ELF dynamic sections. This is
4940 called by the ELF linker emulation before_allocation routine. We
4941 must set the sizes of the sections before the linker sets the
4942 addresses of the various sections. */
4945 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4948 const char *filter_shlib,
4949 const char * const *auxiliary_filters,
4950 struct bfd_link_info *info,
4951 asection **sinterpptr,
4952 struct bfd_elf_version_tree *verdefs)
4954 bfd_size_type soname_indx;
4956 const struct elf_backend_data *bed;
4957 struct elf_assign_sym_version_info asvinfo;
4961 soname_indx = (bfd_size_type) -1;
4963 if (!is_elf_hash_table (info->hash))
4966 elf_tdata (output_bfd)->relro = info->relro;
4967 if (info->execstack)
4968 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4969 else if (info->noexecstack)
4970 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4974 asection *notesec = NULL;
4977 for (inputobj = info->input_bfds;
4979 inputobj = inputobj->link_next)
4983 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
4985 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4988 if (s->flags & SEC_CODE)
4997 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4998 if (exec && info->relocatable
4999 && notesec->output_section != bfd_abs_section_ptr)
5000 notesec->output_section->flags |= SEC_CODE;
5004 /* Any syms created from now on start with -1 in
5005 got.refcount/offset and plt.refcount/offset. */
5006 elf_hash_table (info)->init_got_refcount
5007 = elf_hash_table (info)->init_got_offset;
5008 elf_hash_table (info)->init_plt_refcount
5009 = elf_hash_table (info)->init_plt_offset;
5011 /* The backend may have to create some sections regardless of whether
5012 we're dynamic or not. */
5013 bed = get_elf_backend_data (output_bfd);
5014 if (bed->elf_backend_always_size_sections
5015 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5018 dynobj = elf_hash_table (info)->dynobj;
5020 /* If there were no dynamic objects in the link, there is nothing to
5025 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5028 if (elf_hash_table (info)->dynamic_sections_created)
5030 struct elf_info_failed eif;
5031 struct elf_link_hash_entry *h;
5033 struct bfd_elf_version_tree *t;
5034 struct bfd_elf_version_expr *d;
5036 bfd_boolean all_defined;
5038 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5039 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5043 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5045 if (soname_indx == (bfd_size_type) -1
5046 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5052 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5054 info->flags |= DF_SYMBOLIC;
5061 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5063 if (indx == (bfd_size_type) -1
5064 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5067 if (info->new_dtags)
5069 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5070 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5075 if (filter_shlib != NULL)
5079 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5080 filter_shlib, TRUE);
5081 if (indx == (bfd_size_type) -1
5082 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5086 if (auxiliary_filters != NULL)
5088 const char * const *p;
5090 for (p = auxiliary_filters; *p != NULL; p++)
5094 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5096 if (indx == (bfd_size_type) -1
5097 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5103 eif.verdefs = verdefs;
5106 /* If we are supposed to export all symbols into the dynamic symbol
5107 table (this is not the normal case), then do so. */
5108 if (info->export_dynamic)
5110 elf_link_hash_traverse (elf_hash_table (info),
5111 _bfd_elf_export_symbol,
5117 /* Make all global versions with definition. */
5118 for (t = verdefs; t != NULL; t = t->next)
5119 for (d = t->globals.list; d != NULL; d = d->next)
5120 if (!d->symver && d->symbol)
5122 const char *verstr, *name;
5123 size_t namelen, verlen, newlen;
5125 struct elf_link_hash_entry *newh;
5128 namelen = strlen (name);
5130 verlen = strlen (verstr);
5131 newlen = namelen + verlen + 3;
5133 newname = bfd_malloc (newlen);
5134 if (newname == NULL)
5136 memcpy (newname, name, namelen);
5138 /* Check the hidden versioned definition. */
5139 p = newname + namelen;
5141 memcpy (p, verstr, verlen + 1);
5142 newh = elf_link_hash_lookup (elf_hash_table (info),
5143 newname, FALSE, FALSE,
5146 || (newh->root.type != bfd_link_hash_defined
5147 && newh->root.type != bfd_link_hash_defweak))
5149 /* Check the default versioned definition. */
5151 memcpy (p, verstr, verlen + 1);
5152 newh = elf_link_hash_lookup (elf_hash_table (info),
5153 newname, FALSE, FALSE,
5158 /* Mark this version if there is a definition and it is
5159 not defined in a shared object. */
5161 && !newh->def_dynamic
5162 && (newh->root.type == bfd_link_hash_defined
5163 || newh->root.type == bfd_link_hash_defweak))
5167 /* Attach all the symbols to their version information. */
5168 asvinfo.output_bfd = output_bfd;
5169 asvinfo.info = info;
5170 asvinfo.verdefs = verdefs;
5171 asvinfo.failed = FALSE;
5173 elf_link_hash_traverse (elf_hash_table (info),
5174 _bfd_elf_link_assign_sym_version,
5179 if (!info->allow_undefined_version)
5181 /* Check if all global versions have a definition. */
5183 for (t = verdefs; t != NULL; t = t->next)
5184 for (d = t->globals.list; d != NULL; d = d->next)
5185 if (!d->symver && !d->script)
5187 (*_bfd_error_handler)
5188 (_("%s: undefined version: %s"),
5189 d->pattern, t->name);
5190 all_defined = FALSE;
5195 bfd_set_error (bfd_error_bad_value);
5200 /* Find all symbols which were defined in a dynamic object and make
5201 the backend pick a reasonable value for them. */
5202 elf_link_hash_traverse (elf_hash_table (info),
5203 _bfd_elf_adjust_dynamic_symbol,
5208 /* Add some entries to the .dynamic section. We fill in some of the
5209 values later, in bfd_elf_final_link, but we must add the entries
5210 now so that we know the final size of the .dynamic section. */
5212 /* If there are initialization and/or finalization functions to
5213 call then add the corresponding DT_INIT/DT_FINI entries. */
5214 h = (info->init_function
5215 ? elf_link_hash_lookup (elf_hash_table (info),
5216 info->init_function, FALSE,
5223 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5226 h = (info->fini_function
5227 ? elf_link_hash_lookup (elf_hash_table (info),
5228 info->fini_function, FALSE,
5235 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5239 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5240 if (s != NULL && s->linker_has_input)
5242 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5243 if (! info->executable)
5248 for (sub = info->input_bfds; sub != NULL;
5249 sub = sub->link_next)
5250 for (o = sub->sections; o != NULL; o = o->next)
5251 if (elf_section_data (o)->this_hdr.sh_type
5252 == SHT_PREINIT_ARRAY)
5254 (*_bfd_error_handler)
5255 (_("%B: .preinit_array section is not allowed in DSO"),
5260 bfd_set_error (bfd_error_nonrepresentable_section);
5264 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5265 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5268 s = bfd_get_section_by_name (output_bfd, ".init_array");
5269 if (s != NULL && s->linker_has_input)
5271 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5272 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5275 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5276 if (s != NULL && s->linker_has_input)
5278 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5279 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5283 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5284 /* If .dynstr is excluded from the link, we don't want any of
5285 these tags. Strictly, we should be checking each section
5286 individually; This quick check covers for the case where
5287 someone does a /DISCARD/ : { *(*) }. */
5288 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5290 bfd_size_type strsize;
5292 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5293 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5294 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5295 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5296 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5297 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5298 bed->s->sizeof_sym))
5303 /* The backend must work out the sizes of all the other dynamic
5305 if (bed->elf_backend_size_dynamic_sections
5306 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5309 if (elf_hash_table (info)->dynamic_sections_created)
5311 unsigned long section_sym_count;
5314 /* Set up the version definition section. */
5315 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5316 BFD_ASSERT (s != NULL);
5318 /* We may have created additional version definitions if we are
5319 just linking a regular application. */
5320 verdefs = asvinfo.verdefs;
5322 /* Skip anonymous version tag. */
5323 if (verdefs != NULL && verdefs->vernum == 0)
5324 verdefs = verdefs->next;
5326 if (verdefs == NULL && !info->create_default_symver)
5327 s->flags |= SEC_EXCLUDE;
5332 struct bfd_elf_version_tree *t;
5334 Elf_Internal_Verdef def;
5335 Elf_Internal_Verdaux defaux;
5336 struct bfd_link_hash_entry *bh;
5337 struct elf_link_hash_entry *h;
5343 /* Make space for the base version. */
5344 size += sizeof (Elf_External_Verdef);
5345 size += sizeof (Elf_External_Verdaux);
5348 /* Make space for the default version. */
5349 if (info->create_default_symver)
5351 size += sizeof (Elf_External_Verdef);
5355 for (t = verdefs; t != NULL; t = t->next)
5357 struct bfd_elf_version_deps *n;
5359 size += sizeof (Elf_External_Verdef);
5360 size += sizeof (Elf_External_Verdaux);
5363 for (n = t->deps; n != NULL; n = n->next)
5364 size += sizeof (Elf_External_Verdaux);
5368 s->contents = bfd_alloc (output_bfd, s->size);
5369 if (s->contents == NULL && s->size != 0)
5372 /* Fill in the version definition section. */
5376 def.vd_version = VER_DEF_CURRENT;
5377 def.vd_flags = VER_FLG_BASE;
5380 if (info->create_default_symver)
5382 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5383 def.vd_next = sizeof (Elf_External_Verdef);
5387 def.vd_aux = sizeof (Elf_External_Verdef);
5388 def.vd_next = (sizeof (Elf_External_Verdef)
5389 + sizeof (Elf_External_Verdaux));
5392 if (soname_indx != (bfd_size_type) -1)
5394 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5396 def.vd_hash = bfd_elf_hash (soname);
5397 defaux.vda_name = soname_indx;
5404 name = lbasename (output_bfd->filename);
5405 def.vd_hash = bfd_elf_hash (name);
5406 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5408 if (indx == (bfd_size_type) -1)
5410 defaux.vda_name = indx;
5412 defaux.vda_next = 0;
5414 _bfd_elf_swap_verdef_out (output_bfd, &def,
5415 (Elf_External_Verdef *) p);
5416 p += sizeof (Elf_External_Verdef);
5417 if (info->create_default_symver)
5419 /* Add a symbol representing this version. */
5421 if (! (_bfd_generic_link_add_one_symbol
5422 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5424 get_elf_backend_data (dynobj)->collect, &bh)))
5426 h = (struct elf_link_hash_entry *) bh;
5429 h->type = STT_OBJECT;
5430 h->verinfo.vertree = NULL;
5432 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5435 /* Create a duplicate of the base version with the same
5436 aux block, but different flags. */
5439 def.vd_aux = sizeof (Elf_External_Verdef);
5441 def.vd_next = (sizeof (Elf_External_Verdef)
5442 + sizeof (Elf_External_Verdaux));
5445 _bfd_elf_swap_verdef_out (output_bfd, &def,
5446 (Elf_External_Verdef *) p);
5447 p += sizeof (Elf_External_Verdef);
5449 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5450 (Elf_External_Verdaux *) p);
5451 p += sizeof (Elf_External_Verdaux);
5453 for (t = verdefs; t != NULL; t = t->next)
5456 struct bfd_elf_version_deps *n;
5459 for (n = t->deps; n != NULL; n = n->next)
5462 /* Add a symbol representing this version. */
5464 if (! (_bfd_generic_link_add_one_symbol
5465 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5467 get_elf_backend_data (dynobj)->collect, &bh)))
5469 h = (struct elf_link_hash_entry *) bh;
5472 h->type = STT_OBJECT;
5473 h->verinfo.vertree = t;
5475 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5478 def.vd_version = VER_DEF_CURRENT;
5480 if (t->globals.list == NULL
5481 && t->locals.list == NULL
5483 def.vd_flags |= VER_FLG_WEAK;
5484 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5485 def.vd_cnt = cdeps + 1;
5486 def.vd_hash = bfd_elf_hash (t->name);
5487 def.vd_aux = sizeof (Elf_External_Verdef);
5489 if (t->next != NULL)
5490 def.vd_next = (sizeof (Elf_External_Verdef)
5491 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5493 _bfd_elf_swap_verdef_out (output_bfd, &def,
5494 (Elf_External_Verdef *) p);
5495 p += sizeof (Elf_External_Verdef);
5497 defaux.vda_name = h->dynstr_index;
5498 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5500 defaux.vda_next = 0;
5501 if (t->deps != NULL)
5502 defaux.vda_next = sizeof (Elf_External_Verdaux);
5503 t->name_indx = defaux.vda_name;
5505 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5506 (Elf_External_Verdaux *) p);
5507 p += sizeof (Elf_External_Verdaux);
5509 for (n = t->deps; n != NULL; n = n->next)
5511 if (n->version_needed == NULL)
5513 /* This can happen if there was an error in the
5515 defaux.vda_name = 0;
5519 defaux.vda_name = n->version_needed->name_indx;
5520 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5523 if (n->next == NULL)
5524 defaux.vda_next = 0;
5526 defaux.vda_next = sizeof (Elf_External_Verdaux);
5528 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5529 (Elf_External_Verdaux *) p);
5530 p += sizeof (Elf_External_Verdaux);
5534 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5535 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5538 elf_tdata (output_bfd)->cverdefs = cdefs;
5541 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5543 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5546 else if (info->flags & DF_BIND_NOW)
5548 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5554 if (info->executable)
5555 info->flags_1 &= ~ (DF_1_INITFIRST
5558 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5562 /* Work out the size of the version reference section. */
5564 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5565 BFD_ASSERT (s != NULL);
5567 struct elf_find_verdep_info sinfo;
5569 sinfo.output_bfd = output_bfd;
5571 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5572 if (sinfo.vers == 0)
5574 sinfo.failed = FALSE;
5576 elf_link_hash_traverse (elf_hash_table (info),
5577 _bfd_elf_link_find_version_dependencies,
5580 if (elf_tdata (output_bfd)->verref == NULL)
5581 s->flags |= SEC_EXCLUDE;
5584 Elf_Internal_Verneed *t;
5589 /* Build the version definition section. */
5592 for (t = elf_tdata (output_bfd)->verref;
5596 Elf_Internal_Vernaux *a;
5598 size += sizeof (Elf_External_Verneed);
5600 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5601 size += sizeof (Elf_External_Vernaux);
5605 s->contents = bfd_alloc (output_bfd, s->size);
5606 if (s->contents == NULL)
5610 for (t = elf_tdata (output_bfd)->verref;
5615 Elf_Internal_Vernaux *a;
5619 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5622 t->vn_version = VER_NEED_CURRENT;
5624 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5625 elf_dt_name (t->vn_bfd) != NULL
5626 ? elf_dt_name (t->vn_bfd)
5627 : lbasename (t->vn_bfd->filename),
5629 if (indx == (bfd_size_type) -1)
5632 t->vn_aux = sizeof (Elf_External_Verneed);
5633 if (t->vn_nextref == NULL)
5636 t->vn_next = (sizeof (Elf_External_Verneed)
5637 + caux * sizeof (Elf_External_Vernaux));
5639 _bfd_elf_swap_verneed_out (output_bfd, t,
5640 (Elf_External_Verneed *) p);
5641 p += sizeof (Elf_External_Verneed);
5643 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5645 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5646 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5647 a->vna_nodename, FALSE);
5648 if (indx == (bfd_size_type) -1)
5651 if (a->vna_nextptr == NULL)
5654 a->vna_next = sizeof (Elf_External_Vernaux);
5656 _bfd_elf_swap_vernaux_out (output_bfd, a,
5657 (Elf_External_Vernaux *) p);
5658 p += sizeof (Elf_External_Vernaux);
5662 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5663 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5666 elf_tdata (output_bfd)->cverrefs = crefs;
5670 if ((elf_tdata (output_bfd)->cverrefs == 0
5671 && elf_tdata (output_bfd)->cverdefs == 0)
5672 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5673 §ion_sym_count) == 0)
5675 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5676 s->flags |= SEC_EXCLUDE;
5683 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5685 if (!is_elf_hash_table (info->hash))
5688 if (elf_hash_table (info)->dynamic_sections_created)
5691 const struct elf_backend_data *bed;
5693 bfd_size_type dynsymcount;
5694 unsigned long section_sym_count;
5695 size_t bucketcount = 0;
5696 size_t hash_entry_size;
5697 unsigned int dtagcount;
5699 dynobj = elf_hash_table (info)->dynobj;
5701 /* Assign dynsym indicies. In a shared library we generate a
5702 section symbol for each output section, which come first.
5703 Next come all of the back-end allocated local dynamic syms,
5704 followed by the rest of the global symbols. */
5706 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5707 §ion_sym_count);
5709 /* Work out the size of the symbol version section. */
5710 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5711 BFD_ASSERT (s != NULL);
5712 if (dynsymcount != 0
5713 && (s->flags & SEC_EXCLUDE) == 0)
5715 s->size = dynsymcount * sizeof (Elf_External_Versym);
5716 s->contents = bfd_zalloc (output_bfd, s->size);
5717 if (s->contents == NULL)
5720 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5724 /* Set the size of the .dynsym and .hash sections. We counted
5725 the number of dynamic symbols in elf_link_add_object_symbols.
5726 We will build the contents of .dynsym and .hash when we build
5727 the final symbol table, because until then we do not know the
5728 correct value to give the symbols. We built the .dynstr
5729 section as we went along in elf_link_add_object_symbols. */
5730 s = bfd_get_section_by_name (dynobj, ".dynsym");
5731 BFD_ASSERT (s != NULL);
5732 bed = get_elf_backend_data (output_bfd);
5733 s->size = dynsymcount * bed->s->sizeof_sym;
5735 if (dynsymcount != 0)
5737 s->contents = bfd_alloc (output_bfd, s->size);
5738 if (s->contents == NULL)
5741 /* The first entry in .dynsym is a dummy symbol.
5742 Clear all the section syms, in case we don't output them all. */
5743 ++section_sym_count;
5744 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5747 /* Compute the size of the hashing table. As a side effect this
5748 computes the hash values for all the names we export. */
5749 bucketcount = compute_bucket_count (info);
5751 s = bfd_get_section_by_name (dynobj, ".hash");
5752 BFD_ASSERT (s != NULL);
5753 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5754 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5755 s->contents = bfd_zalloc (output_bfd, s->size);
5756 if (s->contents == NULL)
5759 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5760 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5761 s->contents + hash_entry_size);
5763 elf_hash_table (info)->bucketcount = bucketcount;
5765 s = bfd_get_section_by_name (dynobj, ".dynstr");
5766 BFD_ASSERT (s != NULL);
5768 elf_finalize_dynstr (output_bfd, info);
5770 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5772 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5773 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5780 /* Final phase of ELF linker. */
5782 /* A structure we use to avoid passing large numbers of arguments. */
5784 struct elf_final_link_info
5786 /* General link information. */
5787 struct bfd_link_info *info;
5790 /* Symbol string table. */
5791 struct bfd_strtab_hash *symstrtab;
5792 /* .dynsym section. */
5793 asection *dynsym_sec;
5794 /* .hash section. */
5796 /* symbol version section (.gnu.version). */
5797 asection *symver_sec;
5798 /* Buffer large enough to hold contents of any section. */
5800 /* Buffer large enough to hold external relocs of any section. */
5801 void *external_relocs;
5802 /* Buffer large enough to hold internal relocs of any section. */
5803 Elf_Internal_Rela *internal_relocs;
5804 /* Buffer large enough to hold external local symbols of any input
5806 bfd_byte *external_syms;
5807 /* And a buffer for symbol section indices. */
5808 Elf_External_Sym_Shndx *locsym_shndx;
5809 /* Buffer large enough to hold internal local symbols of any input
5811 Elf_Internal_Sym *internal_syms;
5812 /* Array large enough to hold a symbol index for each local symbol
5813 of any input BFD. */
5815 /* Array large enough to hold a section pointer for each local
5816 symbol of any input BFD. */
5817 asection **sections;
5818 /* Buffer to hold swapped out symbols. */
5820 /* And one for symbol section indices. */
5821 Elf_External_Sym_Shndx *symshndxbuf;
5822 /* Number of swapped out symbols in buffer. */
5823 size_t symbuf_count;
5824 /* Number of symbols which fit in symbuf. */
5826 /* And same for symshndxbuf. */
5827 size_t shndxbuf_size;
5830 /* This struct is used to pass information to elf_link_output_extsym. */
5832 struct elf_outext_info
5835 bfd_boolean localsyms;
5836 struct elf_final_link_info *finfo;
5839 /* When performing a relocatable link, the input relocations are
5840 preserved. But, if they reference global symbols, the indices
5841 referenced must be updated. Update all the relocations in
5842 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5845 elf_link_adjust_relocs (bfd *abfd,
5846 Elf_Internal_Shdr *rel_hdr,
5848 struct elf_link_hash_entry **rel_hash)
5851 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5853 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5854 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5855 bfd_vma r_type_mask;
5858 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5860 swap_in = bed->s->swap_reloc_in;
5861 swap_out = bed->s->swap_reloc_out;
5863 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5865 swap_in = bed->s->swap_reloca_in;
5866 swap_out = bed->s->swap_reloca_out;
5871 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5874 if (bed->s->arch_size == 32)
5881 r_type_mask = 0xffffffff;
5885 erela = rel_hdr->contents;
5886 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5888 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5891 if (*rel_hash == NULL)
5894 BFD_ASSERT ((*rel_hash)->indx >= 0);
5896 (*swap_in) (abfd, erela, irela);
5897 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5898 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5899 | (irela[j].r_info & r_type_mask));
5900 (*swap_out) (abfd, irela, erela);
5904 struct elf_link_sort_rela
5910 enum elf_reloc_type_class type;
5911 /* We use this as an array of size int_rels_per_ext_rel. */
5912 Elf_Internal_Rela rela[1];
5916 elf_link_sort_cmp1 (const void *A, const void *B)
5918 const struct elf_link_sort_rela *a = A;
5919 const struct elf_link_sort_rela *b = B;
5920 int relativea, relativeb;
5922 relativea = a->type == reloc_class_relative;
5923 relativeb = b->type == reloc_class_relative;
5925 if (relativea < relativeb)
5927 if (relativea > relativeb)
5929 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5931 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5933 if (a->rela->r_offset < b->rela->r_offset)
5935 if (a->rela->r_offset > b->rela->r_offset)
5941 elf_link_sort_cmp2 (const void *A, const void *B)
5943 const struct elf_link_sort_rela *a = A;
5944 const struct elf_link_sort_rela *b = B;
5947 if (a->u.offset < b->u.offset)
5949 if (a->u.offset > b->u.offset)
5951 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5952 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5957 if (a->rela->r_offset < b->rela->r_offset)
5959 if (a->rela->r_offset > b->rela->r_offset)
5965 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5968 bfd_size_type count, size;
5969 size_t i, ret, sort_elt, ext_size;
5970 bfd_byte *sort, *s_non_relative, *p;
5971 struct elf_link_sort_rela *sq;
5972 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5973 int i2e = bed->s->int_rels_per_ext_rel;
5974 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5975 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5976 struct bfd_link_order *lo;
5979 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5980 if (reldyn == NULL || reldyn->size == 0)
5982 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5983 if (reldyn == NULL || reldyn->size == 0)
5985 ext_size = bed->s->sizeof_rel;
5986 swap_in = bed->s->swap_reloc_in;
5987 swap_out = bed->s->swap_reloc_out;
5991 ext_size = bed->s->sizeof_rela;
5992 swap_in = bed->s->swap_reloca_in;
5993 swap_out = bed->s->swap_reloca_out;
5995 count = reldyn->size / ext_size;
5998 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
5999 if (lo->type == bfd_indirect_link_order)
6001 asection *o = lo->u.indirect.section;
6005 if (size != reldyn->size)
6008 sort_elt = (sizeof (struct elf_link_sort_rela)
6009 + (i2e - 1) * sizeof (Elf_Internal_Rela));
6010 sort = bfd_zmalloc (sort_elt * count);
6013 (*info->callbacks->warning)
6014 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
6018 if (bed->s->arch_size == 32)
6019 r_sym_mask = ~(bfd_vma) 0xff;
6021 r_sym_mask = ~(bfd_vma) 0xffffffff;
6023 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6024 if (lo->type == bfd_indirect_link_order)
6026 bfd_byte *erel, *erelend;
6027 asection *o = lo->u.indirect.section;
6029 if (o->contents == NULL && o->size != 0)
6031 /* This is a reloc section that is being handled as a normal
6032 section. See bfd_section_from_shdr. We can't combine
6033 relocs in this case. */
6038 erelend = o->contents + o->size;
6039 p = sort + o->output_offset / ext_size * sort_elt;
6040 while (erel < erelend)
6042 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6043 (*swap_in) (abfd, erel, s->rela);
6044 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
6045 s->u.sym_mask = r_sym_mask;
6051 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
6053 for (i = 0, p = sort; i < count; i++, p += sort_elt)
6055 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6056 if (s->type != reloc_class_relative)
6062 sq = (struct elf_link_sort_rela *) s_non_relative;
6063 for (; i < count; i++, p += sort_elt)
6065 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
6066 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
6068 sp->u.offset = sq->rela->r_offset;
6071 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
6073 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6074 if (lo->type == bfd_indirect_link_order)
6076 bfd_byte *erel, *erelend;
6077 asection *o = lo->u.indirect.section;
6080 erelend = o->contents + o->size;
6081 p = sort + o->output_offset / ext_size * sort_elt;
6082 while (erel < erelend)
6084 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6085 (*swap_out) (abfd, s->rela, erel);
6096 /* Flush the output symbols to the file. */
6099 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
6100 const struct elf_backend_data *bed)
6102 if (finfo->symbuf_count > 0)
6104 Elf_Internal_Shdr *hdr;
6108 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6109 pos = hdr->sh_offset + hdr->sh_size;
6110 amt = finfo->symbuf_count * bed->s->sizeof_sym;
6111 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6112 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
6115 hdr->sh_size += amt;
6116 finfo->symbuf_count = 0;
6122 /* Add a symbol to the output symbol table. */
6125 elf_link_output_sym (struct elf_final_link_info *finfo,
6127 Elf_Internal_Sym *elfsym,
6128 asection *input_sec,
6129 struct elf_link_hash_entry *h)
6132 Elf_External_Sym_Shndx *destshndx;
6133 bfd_boolean (*output_symbol_hook)
6134 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
6135 struct elf_link_hash_entry *);
6136 const struct elf_backend_data *bed;
6138 bed = get_elf_backend_data (finfo->output_bfd);
6139 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
6140 if (output_symbol_hook != NULL)
6142 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
6146 if (name == NULL || *name == '\0')
6147 elfsym->st_name = 0;
6148 else if (input_sec->flags & SEC_EXCLUDE)
6149 elfsym->st_name = 0;
6152 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
6154 if (elfsym->st_name == (unsigned long) -1)
6158 if (finfo->symbuf_count >= finfo->symbuf_size)
6160 if (! elf_link_flush_output_syms (finfo, bed))
6164 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
6165 destshndx = finfo->symshndxbuf;
6166 if (destshndx != NULL)
6168 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6172 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6173 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6174 if (destshndx == NULL)
6176 memset ((char *) destshndx + amt, 0, amt);
6177 finfo->shndxbuf_size *= 2;
6179 destshndx += bfd_get_symcount (finfo->output_bfd);
6182 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
6183 finfo->symbuf_count += 1;
6184 bfd_get_symcount (finfo->output_bfd) += 1;
6189 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6190 allowing an unsatisfied unversioned symbol in the DSO to match a
6191 versioned symbol that would normally require an explicit version.
6192 We also handle the case that a DSO references a hidden symbol
6193 which may be satisfied by a versioned symbol in another DSO. */
6196 elf_link_check_versioned_symbol (struct bfd_link_info *info,
6197 const struct elf_backend_data *bed,
6198 struct elf_link_hash_entry *h)
6201 struct elf_link_loaded_list *loaded;
6203 if (!is_elf_hash_table (info->hash))
6206 switch (h->root.type)
6212 case bfd_link_hash_undefined:
6213 case bfd_link_hash_undefweak:
6214 abfd = h->root.u.undef.abfd;
6215 if ((abfd->flags & DYNAMIC) == 0
6216 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
6220 case bfd_link_hash_defined:
6221 case bfd_link_hash_defweak:
6222 abfd = h->root.u.def.section->owner;
6225 case bfd_link_hash_common:
6226 abfd = h->root.u.c.p->section->owner;
6229 BFD_ASSERT (abfd != NULL);
6231 for (loaded = elf_hash_table (info)->loaded;
6233 loaded = loaded->next)
6236 Elf_Internal_Shdr *hdr;
6237 bfd_size_type symcount;
6238 bfd_size_type extsymcount;
6239 bfd_size_type extsymoff;
6240 Elf_Internal_Shdr *versymhdr;
6241 Elf_Internal_Sym *isym;
6242 Elf_Internal_Sym *isymend;
6243 Elf_Internal_Sym *isymbuf;
6244 Elf_External_Versym *ever;
6245 Elf_External_Versym *extversym;
6247 input = loaded->abfd;
6249 /* We check each DSO for a possible hidden versioned definition. */
6251 || (input->flags & DYNAMIC) == 0
6252 || elf_dynversym (input) == 0)
6255 hdr = &elf_tdata (input)->dynsymtab_hdr;
6257 symcount = hdr->sh_size / bed->s->sizeof_sym;
6258 if (elf_bad_symtab (input))
6260 extsymcount = symcount;
6265 extsymcount = symcount - hdr->sh_info;
6266 extsymoff = hdr->sh_info;
6269 if (extsymcount == 0)
6272 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6274 if (isymbuf == NULL)
6277 /* Read in any version definitions. */
6278 versymhdr = &elf_tdata (input)->dynversym_hdr;
6279 extversym = bfd_malloc (versymhdr->sh_size);
6280 if (extversym == NULL)
6283 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6284 || (bfd_bread (extversym, versymhdr->sh_size, input)
6285 != versymhdr->sh_size))
6293 ever = extversym + extsymoff;
6294 isymend = isymbuf + extsymcount;
6295 for (isym = isymbuf; isym < isymend; isym++, ever++)
6298 Elf_Internal_Versym iver;
6299 unsigned short version_index;
6301 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6302 || isym->st_shndx == SHN_UNDEF)
6305 name = bfd_elf_string_from_elf_section (input,
6308 if (strcmp (name, h->root.root.string) != 0)
6311 _bfd_elf_swap_versym_in (input, ever, &iver);
6313 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6315 /* If we have a non-hidden versioned sym, then it should
6316 have provided a definition for the undefined sym. */
6320 version_index = iver.vs_vers & VERSYM_VERSION;
6321 if (version_index == 1 || version_index == 2)
6323 /* This is the base or first version. We can use it. */
6337 /* Add an external symbol to the symbol table. This is called from
6338 the hash table traversal routine. When generating a shared object,
6339 we go through the symbol table twice. The first time we output
6340 anything that might have been forced to local scope in a version
6341 script. The second time we output the symbols that are still
6345 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6347 struct elf_outext_info *eoinfo = data;
6348 struct elf_final_link_info *finfo = eoinfo->finfo;
6350 Elf_Internal_Sym sym;
6351 asection *input_sec;
6352 const struct elf_backend_data *bed;
6354 if (h->root.type == bfd_link_hash_warning)
6356 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6357 if (h->root.type == bfd_link_hash_new)
6361 /* Decide whether to output this symbol in this pass. */
6362 if (eoinfo->localsyms)
6364 if (!h->forced_local)
6369 if (h->forced_local)
6373 bed = get_elf_backend_data (finfo->output_bfd);
6375 /* If we have an undefined symbol reference here then it must have
6376 come from a shared library that is being linked in. (Undefined
6377 references in regular files have already been handled). If we
6378 are reporting errors for this situation then do so now. */
6379 if (h->root.type == bfd_link_hash_undefined
6382 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6383 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6385 if (! ((*finfo->info->callbacks->undefined_symbol)
6386 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6387 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6389 eoinfo->failed = TRUE;
6394 /* We should also warn if a forced local symbol is referenced from
6395 shared libraries. */
6396 if (! finfo->info->relocatable
6397 && (! finfo->info->shared)
6402 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6404 (*_bfd_error_handler)
6405 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6407 h->root.u.def.section == bfd_abs_section_ptr
6408 ? finfo->output_bfd : h->root.u.def.section->owner,
6409 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6411 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6412 ? "hidden" : "local",
6413 h->root.root.string);
6414 eoinfo->failed = TRUE;
6418 /* We don't want to output symbols that have never been mentioned by
6419 a regular file, or that we have been told to strip. However, if
6420 h->indx is set to -2, the symbol is used by a reloc and we must
6424 else if ((h->def_dynamic
6426 || h->root.type == bfd_link_hash_new)
6430 else if (finfo->info->strip == strip_all)
6432 else if (finfo->info->strip == strip_some
6433 && bfd_hash_lookup (finfo->info->keep_hash,
6434 h->root.root.string, FALSE, FALSE) == NULL)
6436 else if (finfo->info->strip_discarded
6437 && (h->root.type == bfd_link_hash_defined
6438 || h->root.type == bfd_link_hash_defweak)
6439 && elf_discarded_section (h->root.u.def.section))
6444 /* If we're stripping it, and it's not a dynamic symbol, there's
6445 nothing else to do unless it is a forced local symbol. */
6448 && !h->forced_local)
6452 sym.st_size = h->size;
6453 sym.st_other = h->other;
6454 if (h->forced_local)
6455 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6456 else if (h->root.type == bfd_link_hash_undefweak
6457 || h->root.type == bfd_link_hash_defweak)
6458 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6460 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6462 switch (h->root.type)
6465 case bfd_link_hash_new:
6466 case bfd_link_hash_warning:
6470 case bfd_link_hash_undefined:
6471 case bfd_link_hash_undefweak:
6472 input_sec = bfd_und_section_ptr;
6473 sym.st_shndx = SHN_UNDEF;
6476 case bfd_link_hash_defined:
6477 case bfd_link_hash_defweak:
6479 input_sec = h->root.u.def.section;
6480 if (input_sec->output_section != NULL)
6483 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6484 input_sec->output_section);
6485 if (sym.st_shndx == SHN_BAD)
6487 (*_bfd_error_handler)
6488 (_("%B: could not find output section %A for input section %A"),
6489 finfo->output_bfd, input_sec->output_section, input_sec);
6490 eoinfo->failed = TRUE;
6494 /* ELF symbols in relocatable files are section relative,
6495 but in nonrelocatable files they are virtual
6497 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6498 if (! finfo->info->relocatable)
6500 sym.st_value += input_sec->output_section->vma;
6501 if (h->type == STT_TLS)
6503 /* STT_TLS symbols are relative to PT_TLS segment
6505 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6506 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6512 BFD_ASSERT (input_sec->owner == NULL
6513 || (input_sec->owner->flags & DYNAMIC) != 0);
6514 sym.st_shndx = SHN_UNDEF;
6515 input_sec = bfd_und_section_ptr;
6520 case bfd_link_hash_common:
6521 input_sec = h->root.u.c.p->section;
6522 sym.st_shndx = bed->common_section_index (input_sec);
6523 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6526 case bfd_link_hash_indirect:
6527 /* These symbols are created by symbol versioning. They point
6528 to the decorated version of the name. For example, if the
6529 symbol foo@@GNU_1.2 is the default, which should be used when
6530 foo is used with no version, then we add an indirect symbol
6531 foo which points to foo@@GNU_1.2. We ignore these symbols,
6532 since the indirected symbol is already in the hash table. */
6536 /* Give the processor backend a chance to tweak the symbol value,
6537 and also to finish up anything that needs to be done for this
6538 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6539 forced local syms when non-shared is due to a historical quirk. */
6540 if ((h->dynindx != -1
6542 && ((finfo->info->shared
6543 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6544 || h->root.type != bfd_link_hash_undefweak))
6545 || !h->forced_local)
6546 && elf_hash_table (finfo->info)->dynamic_sections_created)
6548 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6549 (finfo->output_bfd, finfo->info, h, &sym)))
6551 eoinfo->failed = TRUE;
6556 /* If we are marking the symbol as undefined, and there are no
6557 non-weak references to this symbol from a regular object, then
6558 mark the symbol as weak undefined; if there are non-weak
6559 references, mark the symbol as strong. We can't do this earlier,
6560 because it might not be marked as undefined until the
6561 finish_dynamic_symbol routine gets through with it. */
6562 if (sym.st_shndx == SHN_UNDEF
6564 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6565 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6569 if (h->ref_regular_nonweak)
6570 bindtype = STB_GLOBAL;
6572 bindtype = STB_WEAK;
6573 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6576 /* If a non-weak symbol with non-default visibility is not defined
6577 locally, it is a fatal error. */
6578 if (! finfo->info->relocatable
6579 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6580 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6581 && h->root.type == bfd_link_hash_undefined
6584 (*_bfd_error_handler)
6585 (_("%B: %s symbol `%s' isn't defined"),
6587 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6589 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6590 ? "internal" : "hidden",
6591 h->root.root.string);
6592 eoinfo->failed = TRUE;
6596 /* If this symbol should be put in the .dynsym section, then put it
6597 there now. We already know the symbol index. We also fill in
6598 the entry in the .hash section. */
6599 if (h->dynindx != -1
6600 && elf_hash_table (finfo->info)->dynamic_sections_created)
6604 size_t hash_entry_size;
6605 bfd_byte *bucketpos;
6609 sym.st_name = h->dynstr_index;
6610 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6611 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6613 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6614 bucket = h->u.elf_hash_value % bucketcount;
6616 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6617 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6618 + (bucket + 2) * hash_entry_size);
6619 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6620 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6621 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6622 ((bfd_byte *) finfo->hash_sec->contents
6623 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6625 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6627 Elf_Internal_Versym iversym;
6628 Elf_External_Versym *eversym;
6630 if (!h->def_regular)
6632 if (h->verinfo.verdef == NULL)
6633 iversym.vs_vers = 0;
6635 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6639 if (h->verinfo.vertree == NULL)
6640 iversym.vs_vers = 1;
6642 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6643 if (finfo->info->create_default_symver)
6648 iversym.vs_vers |= VERSYM_HIDDEN;
6650 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6651 eversym += h->dynindx;
6652 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6656 /* If we're stripping it, then it was just a dynamic symbol, and
6657 there's nothing else to do. */
6658 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6661 h->indx = bfd_get_symcount (finfo->output_bfd);
6663 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6665 eoinfo->failed = TRUE;
6672 /* Return TRUE if special handling is done for relocs in SEC against
6673 symbols defined in discarded sections. */
6676 elf_section_ignore_discarded_relocs (asection *sec)
6678 const struct elf_backend_data *bed;
6680 switch (sec->sec_info_type)
6682 case ELF_INFO_TYPE_STABS:
6683 case ELF_INFO_TYPE_EH_FRAME:
6689 bed = get_elf_backend_data (sec->owner);
6690 if (bed->elf_backend_ignore_discarded_relocs != NULL
6691 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6697 /* Return a mask saying how ld should treat relocations in SEC against
6698 symbols defined in discarded sections. If this function returns
6699 COMPLAIN set, ld will issue a warning message. If this function
6700 returns PRETEND set, and the discarded section was link-once and the
6701 same size as the kept link-once section, ld will pretend that the
6702 symbol was actually defined in the kept section. Otherwise ld will
6703 zero the reloc (at least that is the intent, but some cooperation by
6704 the target dependent code is needed, particularly for REL targets). */
6707 _bfd_elf_default_action_discarded (asection *sec)
6709 if (sec->flags & SEC_DEBUGGING)
6712 if (strcmp (".eh_frame", sec->name) == 0)
6715 if (strcmp (".gcc_except_table", sec->name) == 0)
6718 return COMPLAIN | PRETEND;
6721 /* Find a match between a section and a member of a section group. */
6724 match_group_member (asection *sec, asection *group)
6726 asection *first = elf_next_in_group (group);
6727 asection *s = first;
6731 if (bfd_elf_match_symbols_in_sections (s, sec))
6741 /* Check if the kept section of a discarded section SEC can be used
6742 to replace it. Return the replacement if it is OK. Otherwise return
6746 _bfd_elf_check_kept_section (asection *sec)
6750 kept = sec->kept_section;
6753 if (elf_sec_group (sec) != NULL)
6754 kept = match_group_member (sec, kept);
6755 if (kept != NULL && sec->size != kept->size)
6761 /* Link an input file into the linker output file. This function
6762 handles all the sections and relocations of the input file at once.
6763 This is so that we only have to read the local symbols once, and
6764 don't have to keep them in memory. */
6767 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6769 bfd_boolean (*relocate_section)
6770 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6771 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6773 Elf_Internal_Shdr *symtab_hdr;
6776 Elf_Internal_Sym *isymbuf;
6777 Elf_Internal_Sym *isym;
6778 Elf_Internal_Sym *isymend;
6780 asection **ppsection;
6782 const struct elf_backend_data *bed;
6783 bfd_boolean emit_relocs;
6784 struct elf_link_hash_entry **sym_hashes;
6786 output_bfd = finfo->output_bfd;
6787 bed = get_elf_backend_data (output_bfd);
6788 relocate_section = bed->elf_backend_relocate_section;
6790 /* If this is a dynamic object, we don't want to do anything here:
6791 we don't want the local symbols, and we don't want the section
6793 if ((input_bfd->flags & DYNAMIC) != 0)
6796 emit_relocs = (finfo->info->relocatable
6797 || finfo->info->emitrelocations);
6799 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6800 if (elf_bad_symtab (input_bfd))
6802 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6807 locsymcount = symtab_hdr->sh_info;
6808 extsymoff = symtab_hdr->sh_info;
6811 /* Read the local symbols. */
6812 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6813 if (isymbuf == NULL && locsymcount != 0)
6815 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6816 finfo->internal_syms,
6817 finfo->external_syms,
6818 finfo->locsym_shndx);
6819 if (isymbuf == NULL)
6823 /* Find local symbol sections and adjust values of symbols in
6824 SEC_MERGE sections. Write out those local symbols we know are
6825 going into the output file. */
6826 isymend = isymbuf + locsymcount;
6827 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6829 isym++, pindex++, ppsection++)
6833 Elf_Internal_Sym osym;
6837 if (elf_bad_symtab (input_bfd))
6839 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6846 if (isym->st_shndx == SHN_UNDEF)
6847 isec = bfd_und_section_ptr;
6848 else if (isym->st_shndx < SHN_LORESERVE
6849 || isym->st_shndx > SHN_HIRESERVE)
6851 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6853 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6854 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6856 _bfd_merged_section_offset (output_bfd, &isec,
6857 elf_section_data (isec)->sec_info,
6860 else if (isym->st_shndx == SHN_ABS)
6861 isec = bfd_abs_section_ptr;
6862 else if (isym->st_shndx == SHN_COMMON)
6863 isec = bfd_com_section_ptr;
6872 /* Don't output the first, undefined, symbol. */
6873 if (ppsection == finfo->sections)
6876 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6878 /* We never output section symbols. Instead, we use the
6879 section symbol of the corresponding section in the output
6884 /* If we are stripping all symbols, we don't want to output this
6886 if (finfo->info->strip == strip_all)
6889 /* If we are discarding all local symbols, we don't want to
6890 output this one. If we are generating a relocatable output
6891 file, then some of the local symbols may be required by
6892 relocs; we output them below as we discover that they are
6894 if (finfo->info->discard == discard_all)
6897 /* If this symbol is defined in a section which we are
6898 discarding, we don't need to keep it, but note that
6899 linker_mark is only reliable for sections that have contents.
6900 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6901 as well as linker_mark. */
6902 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6904 || (! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6905 || (! finfo->info->relocatable
6906 && (isec->flags & SEC_EXCLUDE) != 0)))
6909 /* If the section is not in the output BFD's section list, it is not
6911 if (bfd_section_removed_from_list (output_bfd, isec->output_section))
6914 /* Get the name of the symbol. */
6915 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6920 /* See if we are discarding symbols with this name. */
6921 if ((finfo->info->strip == strip_some
6922 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6924 || (((finfo->info->discard == discard_sec_merge
6925 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6926 || finfo->info->discard == discard_l)
6927 && bfd_is_local_label_name (input_bfd, name)))
6930 /* If we get here, we are going to output this symbol. */
6934 /* Adjust the section index for the output file. */
6935 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6936 isec->output_section);
6937 if (osym.st_shndx == SHN_BAD)
6940 *pindex = bfd_get_symcount (output_bfd);
6942 /* ELF symbols in relocatable files are section relative, but
6943 in executable files they are virtual addresses. Note that
6944 this code assumes that all ELF sections have an associated
6945 BFD section with a reasonable value for output_offset; below
6946 we assume that they also have a reasonable value for
6947 output_section. Any special sections must be set up to meet
6948 these requirements. */
6949 osym.st_value += isec->output_offset;
6950 if (! finfo->info->relocatable)
6952 osym.st_value += isec->output_section->vma;
6953 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6955 /* STT_TLS symbols are relative to PT_TLS segment base. */
6956 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6957 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6961 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6965 /* Relocate the contents of each section. */
6966 sym_hashes = elf_sym_hashes (input_bfd);
6967 for (o = input_bfd->sections; o != NULL; o = o->next)
6971 if (! o->linker_mark)
6973 /* This section was omitted from the link. */
6977 if ((o->flags & SEC_HAS_CONTENTS) == 0
6978 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6981 if ((o->flags & SEC_LINKER_CREATED) != 0)
6983 /* Section was created by _bfd_elf_link_create_dynamic_sections
6988 /* Get the contents of the section. They have been cached by a
6989 relaxation routine. Note that o is a section in an input
6990 file, so the contents field will not have been set by any of
6991 the routines which work on output files. */
6992 if (elf_section_data (o)->this_hdr.contents != NULL)
6993 contents = elf_section_data (o)->this_hdr.contents;
6996 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6998 contents = finfo->contents;
6999 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
7003 if ((o->flags & SEC_RELOC) != 0)
7005 Elf_Internal_Rela *internal_relocs;
7006 bfd_vma r_type_mask;
7009 /* Get the swapped relocs. */
7011 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
7012 finfo->internal_relocs, FALSE);
7013 if (internal_relocs == NULL
7014 && o->reloc_count > 0)
7017 if (bed->s->arch_size == 32)
7024 r_type_mask = 0xffffffff;
7028 /* Run through the relocs looking for any against symbols
7029 from discarded sections and section symbols from
7030 removed link-once sections. Complain about relocs
7031 against discarded sections. Zero relocs against removed
7032 link-once sections. Preserve debug information as much
7034 if (!elf_section_ignore_discarded_relocs (o))
7036 Elf_Internal_Rela *rel, *relend;
7037 unsigned int action = (*bed->action_discarded) (o);
7039 rel = internal_relocs;
7040 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
7041 for ( ; rel < relend; rel++)
7043 unsigned long r_symndx = rel->r_info >> r_sym_shift;
7044 asection **ps, *sec;
7045 struct elf_link_hash_entry *h = NULL;
7046 const char *sym_name;
7048 if (r_symndx == STN_UNDEF)
7051 if (r_symndx >= locsymcount
7052 || (elf_bad_symtab (input_bfd)
7053 && finfo->sections[r_symndx] == NULL))
7055 h = sym_hashes[r_symndx - extsymoff];
7057 /* Badly formatted input files can contain relocs that
7058 reference non-existant symbols. Check here so that
7059 we do not seg fault. */
7064 sprintf_vma (buffer, rel->r_info);
7065 (*_bfd_error_handler)
7066 (_("error: %B contains a reloc (0x%s) for section %A "
7067 "that references a non-existent global symbol"),
7068 input_bfd, o, buffer);
7069 bfd_set_error (bfd_error_bad_value);
7073 while (h->root.type == bfd_link_hash_indirect
7074 || h->root.type == bfd_link_hash_warning)
7075 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7077 if (h->root.type != bfd_link_hash_defined
7078 && h->root.type != bfd_link_hash_defweak)
7081 ps = &h->root.u.def.section;
7082 sym_name = h->root.root.string;
7086 Elf_Internal_Sym *sym = isymbuf + r_symndx;
7087 ps = &finfo->sections[r_symndx];
7088 sym_name = bfd_elf_sym_name (input_bfd,
7093 /* Complain if the definition comes from a
7094 discarded section. */
7095 if ((sec = *ps) != NULL && elf_discarded_section (sec))
7097 BFD_ASSERT (r_symndx != 0);
7098 if (action & COMPLAIN)
7099 (*finfo->info->callbacks->einfo)
7100 (_("%X`%s' referenced in section `%A' of %B: "
7101 "defined in discarded section `%A' of %B\n"),
7102 sym_name, o, input_bfd, sec, sec->owner);
7104 /* Try to do the best we can to support buggy old
7105 versions of gcc. If we've warned, or this is
7106 debugging info, pretend that the symbol is
7107 really defined in the kept linkonce section.
7108 FIXME: This is quite broken. Modifying the
7109 symbol here means we will be changing all later
7110 uses of the symbol, not just in this section.
7111 The only thing that makes this half reasonable
7112 is that we warn in non-debug sections, and
7113 debug sections tend to come after other
7115 if (action & PRETEND)
7119 kept = _bfd_elf_check_kept_section (sec);
7127 /* Remove the symbol reference from the reloc, but
7128 don't kill the reloc completely. This is so that
7129 a zero value will be written into the section,
7130 which may have non-zero contents put there by the
7131 assembler. Zero in things like an eh_frame fde
7132 pc_begin allows stack unwinders to recognize the
7134 rel->r_info &= r_type_mask;
7140 /* Relocate the section by invoking a back end routine.
7142 The back end routine is responsible for adjusting the
7143 section contents as necessary, and (if using Rela relocs
7144 and generating a relocatable output file) adjusting the
7145 reloc addend as necessary.
7147 The back end routine does not have to worry about setting
7148 the reloc address or the reloc symbol index.
7150 The back end routine is given a pointer to the swapped in
7151 internal symbols, and can access the hash table entries
7152 for the external symbols via elf_sym_hashes (input_bfd).
7154 When generating relocatable output, the back end routine
7155 must handle STB_LOCAL/STT_SECTION symbols specially. The
7156 output symbol is going to be a section symbol
7157 corresponding to the output section, which will require
7158 the addend to be adjusted. */
7160 if (! (*relocate_section) (output_bfd, finfo->info,
7161 input_bfd, o, contents,
7169 Elf_Internal_Rela *irela;
7170 Elf_Internal_Rela *irelaend;
7171 bfd_vma last_offset;
7172 struct elf_link_hash_entry **rel_hash;
7173 struct elf_link_hash_entry **rel_hash_list;
7174 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
7175 unsigned int next_erel;
7176 bfd_boolean rela_normal;
7178 input_rel_hdr = &elf_section_data (o)->rel_hdr;
7179 rela_normal = (bed->rela_normal
7180 && (input_rel_hdr->sh_entsize
7181 == bed->s->sizeof_rela));
7183 /* Adjust the reloc addresses and symbol indices. */
7185 irela = internal_relocs;
7186 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
7187 rel_hash = (elf_section_data (o->output_section)->rel_hashes
7188 + elf_section_data (o->output_section)->rel_count
7189 + elf_section_data (o->output_section)->rel_count2);
7190 rel_hash_list = rel_hash;
7191 last_offset = o->output_offset;
7192 if (!finfo->info->relocatable)
7193 last_offset += o->output_section->vma;
7194 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
7196 unsigned long r_symndx;
7198 Elf_Internal_Sym sym;
7200 if (next_erel == bed->s->int_rels_per_ext_rel)
7206 irela->r_offset = _bfd_elf_section_offset (output_bfd,
7209 if (irela->r_offset >= (bfd_vma) -2)
7211 /* This is a reloc for a deleted entry or somesuch.
7212 Turn it into an R_*_NONE reloc, at the same
7213 offset as the last reloc. elf_eh_frame.c and
7214 elf_bfd_discard_info rely on reloc offsets
7216 irela->r_offset = last_offset;
7218 irela->r_addend = 0;
7222 irela->r_offset += o->output_offset;
7224 /* Relocs in an executable have to be virtual addresses. */
7225 if (!finfo->info->relocatable)
7226 irela->r_offset += o->output_section->vma;
7228 last_offset = irela->r_offset;
7230 r_symndx = irela->r_info >> r_sym_shift;
7231 if (r_symndx == STN_UNDEF)
7234 if (r_symndx >= locsymcount
7235 || (elf_bad_symtab (input_bfd)
7236 && finfo->sections[r_symndx] == NULL))
7238 struct elf_link_hash_entry *rh;
7241 /* This is a reloc against a global symbol. We
7242 have not yet output all the local symbols, so
7243 we do not know the symbol index of any global
7244 symbol. We set the rel_hash entry for this
7245 reloc to point to the global hash table entry
7246 for this symbol. The symbol index is then
7247 set at the end of bfd_elf_final_link. */
7248 indx = r_symndx - extsymoff;
7249 rh = elf_sym_hashes (input_bfd)[indx];
7250 while (rh->root.type == bfd_link_hash_indirect
7251 || rh->root.type == bfd_link_hash_warning)
7252 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7254 /* Setting the index to -2 tells
7255 elf_link_output_extsym that this symbol is
7257 BFD_ASSERT (rh->indx < 0);
7265 /* This is a reloc against a local symbol. */
7268 sym = isymbuf[r_symndx];
7269 sec = finfo->sections[r_symndx];
7270 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7272 /* I suppose the backend ought to fill in the
7273 section of any STT_SECTION symbol against a
7274 processor specific section. */
7276 if (bfd_is_abs_section (sec))
7278 else if (sec == NULL || sec->owner == NULL)
7280 bfd_set_error (bfd_error_bad_value);
7285 asection *osec = sec->output_section;
7287 /* If we have discarded a section, the output
7288 section will be the absolute section. In
7289 case of discarded link-once and discarded
7290 SEC_MERGE sections, use the kept section. */
7291 if (bfd_is_abs_section (osec)
7292 && sec->kept_section != NULL
7293 && sec->kept_section->output_section != NULL)
7295 osec = sec->kept_section->output_section;
7296 irela->r_addend -= osec->vma;
7299 if (!bfd_is_abs_section (osec))
7301 r_symndx = osec->target_index;
7302 BFD_ASSERT (r_symndx != 0);
7306 /* Adjust the addend according to where the
7307 section winds up in the output section. */
7309 irela->r_addend += sec->output_offset;
7313 if (finfo->indices[r_symndx] == -1)
7315 unsigned long shlink;
7319 if (finfo->info->strip == strip_all)
7321 /* You can't do ld -r -s. */
7322 bfd_set_error (bfd_error_invalid_operation);
7326 /* This symbol was skipped earlier, but
7327 since it is needed by a reloc, we
7328 must output it now. */
7329 shlink = symtab_hdr->sh_link;
7330 name = (bfd_elf_string_from_elf_section
7331 (input_bfd, shlink, sym.st_name));
7335 osec = sec->output_section;
7337 _bfd_elf_section_from_bfd_section (output_bfd,
7339 if (sym.st_shndx == SHN_BAD)
7342 sym.st_value += sec->output_offset;
7343 if (! finfo->info->relocatable)
7345 sym.st_value += osec->vma;
7346 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7348 /* STT_TLS symbols are relative to PT_TLS
7350 BFD_ASSERT (elf_hash_table (finfo->info)
7352 sym.st_value -= (elf_hash_table (finfo->info)
7357 finfo->indices[r_symndx]
7358 = bfd_get_symcount (output_bfd);
7360 if (! elf_link_output_sym (finfo, name, &sym, sec,
7365 r_symndx = finfo->indices[r_symndx];
7368 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7369 | (irela->r_info & r_type_mask));
7372 /* Swap out the relocs. */
7373 if (input_rel_hdr->sh_size != 0
7374 && !bed->elf_backend_emit_relocs (output_bfd, o,
7380 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7381 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7383 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7384 * bed->s->int_rels_per_ext_rel);
7385 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
7386 if (!bed->elf_backend_emit_relocs (output_bfd, o,
7395 /* Write out the modified section contents. */
7396 if (bed->elf_backend_write_section
7397 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7399 /* Section written out. */
7401 else switch (o->sec_info_type)
7403 case ELF_INFO_TYPE_STABS:
7404 if (! (_bfd_write_section_stabs
7406 &elf_hash_table (finfo->info)->stab_info,
7407 o, &elf_section_data (o)->sec_info, contents)))
7410 case ELF_INFO_TYPE_MERGE:
7411 if (! _bfd_write_merged_section (output_bfd, o,
7412 elf_section_data (o)->sec_info))
7415 case ELF_INFO_TYPE_EH_FRAME:
7417 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7424 if (! (o->flags & SEC_EXCLUDE)
7425 && ! bfd_set_section_contents (output_bfd, o->output_section,
7427 (file_ptr) o->output_offset,
7438 /* Generate a reloc when linking an ELF file. This is a reloc
7439 requested by the linker, and does come from any input file. This
7440 is used to build constructor and destructor tables when linking
7444 elf_reloc_link_order (bfd *output_bfd,
7445 struct bfd_link_info *info,
7446 asection *output_section,
7447 struct bfd_link_order *link_order)
7449 reloc_howto_type *howto;
7453 struct elf_link_hash_entry **rel_hash_ptr;
7454 Elf_Internal_Shdr *rel_hdr;
7455 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7456 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7460 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7463 bfd_set_error (bfd_error_bad_value);
7467 addend = link_order->u.reloc.p->addend;
7469 /* Figure out the symbol index. */
7470 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7471 + elf_section_data (output_section)->rel_count
7472 + elf_section_data (output_section)->rel_count2);
7473 if (link_order->type == bfd_section_reloc_link_order)
7475 indx = link_order->u.reloc.p->u.section->target_index;
7476 BFD_ASSERT (indx != 0);
7477 *rel_hash_ptr = NULL;
7481 struct elf_link_hash_entry *h;
7483 /* Treat a reloc against a defined symbol as though it were
7484 actually against the section. */
7485 h = ((struct elf_link_hash_entry *)
7486 bfd_wrapped_link_hash_lookup (output_bfd, info,
7487 link_order->u.reloc.p->u.name,
7488 FALSE, FALSE, TRUE));
7490 && (h->root.type == bfd_link_hash_defined
7491 || h->root.type == bfd_link_hash_defweak))
7495 section = h->root.u.def.section;
7496 indx = section->output_section->target_index;
7497 *rel_hash_ptr = NULL;
7498 /* It seems that we ought to add the symbol value to the
7499 addend here, but in practice it has already been added
7500 because it was passed to constructor_callback. */
7501 addend += section->output_section->vma + section->output_offset;
7505 /* Setting the index to -2 tells elf_link_output_extsym that
7506 this symbol is used by a reloc. */
7513 if (! ((*info->callbacks->unattached_reloc)
7514 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7520 /* If this is an inplace reloc, we must write the addend into the
7522 if (howto->partial_inplace && addend != 0)
7525 bfd_reloc_status_type rstat;
7528 const char *sym_name;
7530 size = bfd_get_reloc_size (howto);
7531 buf = bfd_zmalloc (size);
7534 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7541 case bfd_reloc_outofrange:
7544 case bfd_reloc_overflow:
7545 if (link_order->type == bfd_section_reloc_link_order)
7546 sym_name = bfd_section_name (output_bfd,
7547 link_order->u.reloc.p->u.section);
7549 sym_name = link_order->u.reloc.p->u.name;
7550 if (! ((*info->callbacks->reloc_overflow)
7551 (info, NULL, sym_name, howto->name, addend, NULL,
7552 NULL, (bfd_vma) 0)))
7559 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7560 link_order->offset, size);
7566 /* The address of a reloc is relative to the section in a
7567 relocatable file, and is a virtual address in an executable
7569 offset = link_order->offset;
7570 if (! info->relocatable)
7571 offset += output_section->vma;
7573 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7575 irel[i].r_offset = offset;
7577 irel[i].r_addend = 0;
7579 if (bed->s->arch_size == 32)
7580 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7582 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7584 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7585 erel = rel_hdr->contents;
7586 if (rel_hdr->sh_type == SHT_REL)
7588 erel += (elf_section_data (output_section)->rel_count
7589 * bed->s->sizeof_rel);
7590 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7594 irel[0].r_addend = addend;
7595 erel += (elf_section_data (output_section)->rel_count
7596 * bed->s->sizeof_rela);
7597 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7600 ++elf_section_data (output_section)->rel_count;
7606 /* Get the output vma of the section pointed to by the sh_link field. */
7609 elf_get_linked_section_vma (struct bfd_link_order *p)
7611 Elf_Internal_Shdr **elf_shdrp;
7615 s = p->u.indirect.section;
7616 elf_shdrp = elf_elfsections (s->owner);
7617 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7618 elfsec = elf_shdrp[elfsec]->sh_link;
7620 The Intel C compiler generates SHT_IA_64_UNWIND with
7621 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7622 sh_info fields. Hence we could get the situation
7623 where elfsec is 0. */
7626 const struct elf_backend_data *bed
7627 = get_elf_backend_data (s->owner);
7628 if (bed->link_order_error_handler)
7629 bed->link_order_error_handler
7630 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7635 s = elf_shdrp[elfsec]->bfd_section;
7636 return s->output_section->vma + s->output_offset;
7641 /* Compare two sections based on the locations of the sections they are
7642 linked to. Used by elf_fixup_link_order. */
7645 compare_link_order (const void * a, const void * b)
7650 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7651 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7658 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7659 order as their linked sections. Returns false if this could not be done
7660 because an output section includes both ordered and unordered
7661 sections. Ideally we'd do this in the linker proper. */
7664 elf_fixup_link_order (bfd *abfd, asection *o)
7669 struct bfd_link_order *p;
7671 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7673 struct bfd_link_order **sections;
7679 for (p = o->map_head.link_order; p != NULL; p = p->next)
7681 if (p->type == bfd_indirect_link_order
7682 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7683 == bfd_target_elf_flavour)
7684 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7686 s = p->u.indirect.section;
7687 elfsec = _bfd_elf_section_from_bfd_section (sub, s);
7689 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7698 if (!seen_linkorder)
7701 if (seen_other && seen_linkorder)
7703 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7705 bfd_set_error (bfd_error_bad_value);
7709 sections = (struct bfd_link_order **)
7710 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7713 for (p = o->map_head.link_order; p != NULL; p = p->next)
7715 sections[seen_linkorder++] = p;
7717 /* Sort the input sections in the order of their linked section. */
7718 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7719 compare_link_order);
7721 /* Change the offsets of the sections. */
7723 for (n = 0; n < seen_linkorder; n++)
7725 s = sections[n]->u.indirect.section;
7726 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7727 s->output_offset = offset;
7728 sections[n]->offset = offset;
7729 offset += sections[n]->size;
7736 /* Do the final step of an ELF link. */
7739 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7741 bfd_boolean dynamic;
7742 bfd_boolean emit_relocs;
7744 struct elf_final_link_info finfo;
7745 register asection *o;
7746 register struct bfd_link_order *p;
7748 bfd_size_type max_contents_size;
7749 bfd_size_type max_external_reloc_size;
7750 bfd_size_type max_internal_reloc_count;
7751 bfd_size_type max_sym_count;
7752 bfd_size_type max_sym_shndx_count;
7754 Elf_Internal_Sym elfsym;
7756 Elf_Internal_Shdr *symtab_hdr;
7757 Elf_Internal_Shdr *symtab_shndx_hdr;
7758 Elf_Internal_Shdr *symstrtab_hdr;
7759 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7760 struct elf_outext_info eoinfo;
7762 size_t relativecount = 0;
7763 asection *reldyn = 0;
7766 if (! is_elf_hash_table (info->hash))
7770 abfd->flags |= DYNAMIC;
7772 dynamic = elf_hash_table (info)->dynamic_sections_created;
7773 dynobj = elf_hash_table (info)->dynobj;
7775 emit_relocs = (info->relocatable
7776 || info->emitrelocations
7777 || bed->elf_backend_emit_relocs);
7780 finfo.output_bfd = abfd;
7781 finfo.symstrtab = _bfd_elf_stringtab_init ();
7782 if (finfo.symstrtab == NULL)
7787 finfo.dynsym_sec = NULL;
7788 finfo.hash_sec = NULL;
7789 finfo.symver_sec = NULL;
7793 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7794 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7795 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7796 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7797 /* Note that it is OK if symver_sec is NULL. */
7800 finfo.contents = NULL;
7801 finfo.external_relocs = NULL;
7802 finfo.internal_relocs = NULL;
7803 finfo.external_syms = NULL;
7804 finfo.locsym_shndx = NULL;
7805 finfo.internal_syms = NULL;
7806 finfo.indices = NULL;
7807 finfo.sections = NULL;
7808 finfo.symbuf = NULL;
7809 finfo.symshndxbuf = NULL;
7810 finfo.symbuf_count = 0;
7811 finfo.shndxbuf_size = 0;
7813 /* Count up the number of relocations we will output for each output
7814 section, so that we know the sizes of the reloc sections. We
7815 also figure out some maximum sizes. */
7816 max_contents_size = 0;
7817 max_external_reloc_size = 0;
7818 max_internal_reloc_count = 0;
7820 max_sym_shndx_count = 0;
7822 for (o = abfd->sections; o != NULL; o = o->next)
7824 struct bfd_elf_section_data *esdo = elf_section_data (o);
7827 for (p = o->map_head.link_order; p != NULL; p = p->next)
7829 unsigned int reloc_count = 0;
7830 struct bfd_elf_section_data *esdi = NULL;
7831 unsigned int *rel_count1;
7833 if (p->type == bfd_section_reloc_link_order
7834 || p->type == bfd_symbol_reloc_link_order)
7836 else if (p->type == bfd_indirect_link_order)
7840 sec = p->u.indirect.section;
7841 esdi = elf_section_data (sec);
7843 /* Mark all sections which are to be included in the
7844 link. This will normally be every section. We need
7845 to do this so that we can identify any sections which
7846 the linker has decided to not include. */
7847 sec->linker_mark = TRUE;
7849 if (sec->flags & SEC_MERGE)
7852 if (info->relocatable || info->emitrelocations)
7853 reloc_count = sec->reloc_count;
7854 else if (bed->elf_backend_count_relocs)
7856 Elf_Internal_Rela * relocs;
7858 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7861 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7863 if (elf_section_data (o)->relocs != relocs)
7867 if (sec->rawsize > max_contents_size)
7868 max_contents_size = sec->rawsize;
7869 if (sec->size > max_contents_size)
7870 max_contents_size = sec->size;
7872 /* We are interested in just local symbols, not all
7874 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7875 && (sec->owner->flags & DYNAMIC) == 0)
7879 if (elf_bad_symtab (sec->owner))
7880 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7881 / bed->s->sizeof_sym);
7883 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7885 if (sym_count > max_sym_count)
7886 max_sym_count = sym_count;
7888 if (sym_count > max_sym_shndx_count
7889 && elf_symtab_shndx (sec->owner) != 0)
7890 max_sym_shndx_count = sym_count;
7892 if ((sec->flags & SEC_RELOC) != 0)
7896 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7897 if (ext_size > max_external_reloc_size)
7898 max_external_reloc_size = ext_size;
7899 if (sec->reloc_count > max_internal_reloc_count)
7900 max_internal_reloc_count = sec->reloc_count;
7905 if (reloc_count == 0)
7908 o->reloc_count += reloc_count;
7910 /* MIPS may have a mix of REL and RELA relocs on sections.
7911 To support this curious ABI we keep reloc counts in
7912 elf_section_data too. We must be careful to add the
7913 relocations from the input section to the right output
7914 count. FIXME: Get rid of one count. We have
7915 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7916 rel_count1 = &esdo->rel_count;
7919 bfd_boolean same_size;
7920 bfd_size_type entsize1;
7922 entsize1 = esdi->rel_hdr.sh_entsize;
7923 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7924 || entsize1 == bed->s->sizeof_rela);
7925 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7928 rel_count1 = &esdo->rel_count2;
7930 if (esdi->rel_hdr2 != NULL)
7932 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7933 unsigned int alt_count;
7934 unsigned int *rel_count2;
7936 BFD_ASSERT (entsize2 != entsize1
7937 && (entsize2 == bed->s->sizeof_rel
7938 || entsize2 == bed->s->sizeof_rela));
7940 rel_count2 = &esdo->rel_count2;
7942 rel_count2 = &esdo->rel_count;
7944 /* The following is probably too simplistic if the
7945 backend counts output relocs unusually. */
7946 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7947 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7948 *rel_count2 += alt_count;
7949 reloc_count -= alt_count;
7952 *rel_count1 += reloc_count;
7955 if (o->reloc_count > 0)
7956 o->flags |= SEC_RELOC;
7959 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7960 set it (this is probably a bug) and if it is set
7961 assign_section_numbers will create a reloc section. */
7962 o->flags &=~ SEC_RELOC;
7965 /* If the SEC_ALLOC flag is not set, force the section VMA to
7966 zero. This is done in elf_fake_sections as well, but forcing
7967 the VMA to 0 here will ensure that relocs against these
7968 sections are handled correctly. */
7969 if ((o->flags & SEC_ALLOC) == 0
7970 && ! o->user_set_vma)
7974 if (! info->relocatable && merged)
7975 elf_link_hash_traverse (elf_hash_table (info),
7976 _bfd_elf_link_sec_merge_syms, abfd);
7978 /* Figure out the file positions for everything but the symbol table
7979 and the relocs. We set symcount to force assign_section_numbers
7980 to create a symbol table. */
7981 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7982 BFD_ASSERT (! abfd->output_has_begun);
7983 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7986 /* Set sizes, and assign file positions for reloc sections. */
7987 for (o = abfd->sections; o != NULL; o = o->next)
7989 if ((o->flags & SEC_RELOC) != 0)
7991 if (!(_bfd_elf_link_size_reloc_section
7992 (abfd, &elf_section_data (o)->rel_hdr, o)))
7995 if (elf_section_data (o)->rel_hdr2
7996 && !(_bfd_elf_link_size_reloc_section
7997 (abfd, elf_section_data (o)->rel_hdr2, o)))
8001 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
8002 to count upwards while actually outputting the relocations. */
8003 elf_section_data (o)->rel_count = 0;
8004 elf_section_data (o)->rel_count2 = 0;
8007 _bfd_elf_assign_file_positions_for_relocs (abfd);
8009 /* We have now assigned file positions for all the sections except
8010 .symtab and .strtab. We start the .symtab section at the current
8011 file position, and write directly to it. We build the .strtab
8012 section in memory. */
8013 bfd_get_symcount (abfd) = 0;
8014 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8015 /* sh_name is set in prep_headers. */
8016 symtab_hdr->sh_type = SHT_SYMTAB;
8017 /* sh_flags, sh_addr and sh_size all start off zero. */
8018 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8019 /* sh_link is set in assign_section_numbers. */
8020 /* sh_info is set below. */
8021 /* sh_offset is set just below. */
8022 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
8024 off = elf_tdata (abfd)->next_file_pos;
8025 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
8027 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8028 incorrect. We do not yet know the size of the .symtab section.
8029 We correct next_file_pos below, after we do know the size. */
8031 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8032 continuously seeking to the right position in the file. */
8033 if (! info->keep_memory || max_sym_count < 20)
8034 finfo.symbuf_size = 20;
8036 finfo.symbuf_size = max_sym_count;
8037 amt = finfo.symbuf_size;
8038 amt *= bed->s->sizeof_sym;
8039 finfo.symbuf = bfd_malloc (amt);
8040 if (finfo.symbuf == NULL)
8042 if (elf_numsections (abfd) > SHN_LORESERVE)
8044 /* Wild guess at number of output symbols. realloc'd as needed. */
8045 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
8046 finfo.shndxbuf_size = amt;
8047 amt *= sizeof (Elf_External_Sym_Shndx);
8048 finfo.symshndxbuf = bfd_zmalloc (amt);
8049 if (finfo.symshndxbuf == NULL)
8053 /* Start writing out the symbol table. The first symbol is always a
8055 if (info->strip != strip_all
8058 elfsym.st_value = 0;
8061 elfsym.st_other = 0;
8062 elfsym.st_shndx = SHN_UNDEF;
8063 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
8068 /* Output a symbol for each section. We output these even if we are
8069 discarding local symbols, since they are used for relocs. These
8070 symbols have no names. We store the index of each one in the
8071 index field of the section, so that we can find it again when
8072 outputting relocs. */
8073 if (info->strip != strip_all
8077 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8078 elfsym.st_other = 0;
8079 for (i = 1; i < elf_numsections (abfd); i++)
8081 o = bfd_section_from_elf_index (abfd, i);
8083 o->target_index = bfd_get_symcount (abfd);
8084 elfsym.st_shndx = i;
8085 if (info->relocatable || o == NULL)
8086 elfsym.st_value = 0;
8088 elfsym.st_value = o->vma;
8089 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
8091 if (i == SHN_LORESERVE - 1)
8092 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
8096 /* Allocate some memory to hold information read in from the input
8098 if (max_contents_size != 0)
8100 finfo.contents = bfd_malloc (max_contents_size);
8101 if (finfo.contents == NULL)
8105 if (max_external_reloc_size != 0)
8107 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
8108 if (finfo.external_relocs == NULL)
8112 if (max_internal_reloc_count != 0)
8114 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
8115 amt *= sizeof (Elf_Internal_Rela);
8116 finfo.internal_relocs = bfd_malloc (amt);
8117 if (finfo.internal_relocs == NULL)
8121 if (max_sym_count != 0)
8123 amt = max_sym_count * bed->s->sizeof_sym;
8124 finfo.external_syms = bfd_malloc (amt);
8125 if (finfo.external_syms == NULL)
8128 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8129 finfo.internal_syms = bfd_malloc (amt);
8130 if (finfo.internal_syms == NULL)
8133 amt = max_sym_count * sizeof (long);
8134 finfo.indices = bfd_malloc (amt);
8135 if (finfo.indices == NULL)
8138 amt = max_sym_count * sizeof (asection *);
8139 finfo.sections = bfd_malloc (amt);
8140 if (finfo.sections == NULL)
8144 if (max_sym_shndx_count != 0)
8146 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8147 finfo.locsym_shndx = bfd_malloc (amt);
8148 if (finfo.locsym_shndx == NULL)
8152 if (elf_hash_table (info)->tls_sec)
8154 bfd_vma base, end = 0;
8157 for (sec = elf_hash_table (info)->tls_sec;
8158 sec && (sec->flags & SEC_THREAD_LOCAL);
8161 bfd_vma size = sec->size;
8163 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
8165 struct bfd_link_order *o;
8167 for (o = sec->map_head.link_order; o != NULL; o = o->next)
8168 if (size < o->offset + o->size)
8169 size = o->offset + o->size;
8171 end = sec->vma + size;
8173 base = elf_hash_table (info)->tls_sec->vma;
8174 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
8175 elf_hash_table (info)->tls_size = end - base;
8178 /* Reorder SHF_LINK_ORDER sections. */
8179 for (o = abfd->sections; o != NULL; o = o->next)
8181 if (!elf_fixup_link_order (abfd, o))
8185 /* Since ELF permits relocations to be against local symbols, we
8186 must have the local symbols available when we do the relocations.
8187 Since we would rather only read the local symbols once, and we
8188 would rather not keep them in memory, we handle all the
8189 relocations for a single input file at the same time.
8191 Unfortunately, there is no way to know the total number of local
8192 symbols until we have seen all of them, and the local symbol
8193 indices precede the global symbol indices. This means that when
8194 we are generating relocatable output, and we see a reloc against
8195 a global symbol, we can not know the symbol index until we have
8196 finished examining all the local symbols to see which ones we are
8197 going to output. To deal with this, we keep the relocations in
8198 memory, and don't output them until the end of the link. This is
8199 an unfortunate waste of memory, but I don't see a good way around
8200 it. Fortunately, it only happens when performing a relocatable
8201 link, which is not the common case. FIXME: If keep_memory is set
8202 we could write the relocs out and then read them again; I don't
8203 know how bad the memory loss will be. */
8205 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8206 sub->output_has_begun = FALSE;
8207 for (o = abfd->sections; o != NULL; o = o->next)
8209 for (p = o->map_head.link_order; p != NULL; p = p->next)
8211 if (p->type == bfd_indirect_link_order
8212 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
8213 == bfd_target_elf_flavour)
8214 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
8216 if (! sub->output_has_begun)
8218 if (! elf_link_input_bfd (&finfo, sub))
8220 sub->output_has_begun = TRUE;
8223 else if (p->type == bfd_section_reloc_link_order
8224 || p->type == bfd_symbol_reloc_link_order)
8226 if (! elf_reloc_link_order (abfd, info, o, p))
8231 if (! _bfd_default_link_order (abfd, info, o, p))
8237 /* Output any global symbols that got converted to local in a
8238 version script or due to symbol visibility. We do this in a
8239 separate step since ELF requires all local symbols to appear
8240 prior to any global symbols. FIXME: We should only do this if
8241 some global symbols were, in fact, converted to become local.
8242 FIXME: Will this work correctly with the Irix 5 linker? */
8243 eoinfo.failed = FALSE;
8244 eoinfo.finfo = &finfo;
8245 eoinfo.localsyms = TRUE;
8246 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8251 /* That wrote out all the local symbols. Finish up the symbol table
8252 with the global symbols. Even if we want to strip everything we
8253 can, we still need to deal with those global symbols that got
8254 converted to local in a version script. */
8256 /* The sh_info field records the index of the first non local symbol. */
8257 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8260 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8262 Elf_Internal_Sym sym;
8263 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8264 long last_local = 0;
8266 /* Write out the section symbols for the output sections. */
8267 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
8273 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8276 for (s = abfd->sections; s != NULL; s = s->next)
8282 dynindx = elf_section_data (s)->dynindx;
8285 indx = elf_section_data (s)->this_idx;
8286 BFD_ASSERT (indx > 0);
8287 sym.st_shndx = indx;
8288 sym.st_value = s->vma;
8289 dest = dynsym + dynindx * bed->s->sizeof_sym;
8290 if (last_local < dynindx)
8291 last_local = dynindx;
8292 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8296 /* Write out the local dynsyms. */
8297 if (elf_hash_table (info)->dynlocal)
8299 struct elf_link_local_dynamic_entry *e;
8300 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8305 sym.st_size = e->isym.st_size;
8306 sym.st_other = e->isym.st_other;
8308 /* Copy the internal symbol as is.
8309 Note that we saved a word of storage and overwrote
8310 the original st_name with the dynstr_index. */
8313 if (e->isym.st_shndx != SHN_UNDEF
8314 && (e->isym.st_shndx < SHN_LORESERVE
8315 || e->isym.st_shndx > SHN_HIRESERVE))
8317 s = bfd_section_from_elf_index (e->input_bfd,
8321 elf_section_data (s->output_section)->this_idx;
8322 sym.st_value = (s->output_section->vma
8324 + e->isym.st_value);
8327 if (last_local < e->dynindx)
8328 last_local = e->dynindx;
8330 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8331 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8335 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8339 /* We get the global symbols from the hash table. */
8340 eoinfo.failed = FALSE;
8341 eoinfo.localsyms = FALSE;
8342 eoinfo.finfo = &finfo;
8343 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8348 /* If backend needs to output some symbols not present in the hash
8349 table, do it now. */
8350 if (bed->elf_backend_output_arch_syms)
8352 typedef bfd_boolean (*out_sym_func)
8353 (void *, const char *, Elf_Internal_Sym *, asection *,
8354 struct elf_link_hash_entry *);
8356 if (! ((*bed->elf_backend_output_arch_syms)
8357 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8361 /* Flush all symbols to the file. */
8362 if (! elf_link_flush_output_syms (&finfo, bed))
8365 /* Now we know the size of the symtab section. */
8366 off += symtab_hdr->sh_size;
8368 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8369 if (symtab_shndx_hdr->sh_name != 0)
8371 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8372 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8373 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8374 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8375 symtab_shndx_hdr->sh_size = amt;
8377 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8380 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8381 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8386 /* Finish up and write out the symbol string table (.strtab)
8388 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8389 /* sh_name was set in prep_headers. */
8390 symstrtab_hdr->sh_type = SHT_STRTAB;
8391 symstrtab_hdr->sh_flags = 0;
8392 symstrtab_hdr->sh_addr = 0;
8393 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8394 symstrtab_hdr->sh_entsize = 0;
8395 symstrtab_hdr->sh_link = 0;
8396 symstrtab_hdr->sh_info = 0;
8397 /* sh_offset is set just below. */
8398 symstrtab_hdr->sh_addralign = 1;
8400 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8401 elf_tdata (abfd)->next_file_pos = off;
8403 if (bfd_get_symcount (abfd) > 0)
8405 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8406 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8410 /* Adjust the relocs to have the correct symbol indices. */
8411 for (o = abfd->sections; o != NULL; o = o->next)
8413 if ((o->flags & SEC_RELOC) == 0)
8416 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8417 elf_section_data (o)->rel_count,
8418 elf_section_data (o)->rel_hashes);
8419 if (elf_section_data (o)->rel_hdr2 != NULL)
8420 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8421 elf_section_data (o)->rel_count2,
8422 (elf_section_data (o)->rel_hashes
8423 + elf_section_data (o)->rel_count));
8425 /* Set the reloc_count field to 0 to prevent write_relocs from
8426 trying to swap the relocs out itself. */
8430 if (dynamic && info->combreloc && dynobj != NULL)
8431 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8433 /* If we are linking against a dynamic object, or generating a
8434 shared library, finish up the dynamic linking information. */
8437 bfd_byte *dyncon, *dynconend;
8439 /* Fix up .dynamic entries. */
8440 o = bfd_get_section_by_name (dynobj, ".dynamic");
8441 BFD_ASSERT (o != NULL);
8443 dyncon = o->contents;
8444 dynconend = o->contents + o->size;
8445 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8447 Elf_Internal_Dyn dyn;
8451 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8458 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8460 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8462 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8463 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8466 dyn.d_un.d_val = relativecount;
8473 name = info->init_function;
8476 name = info->fini_function;
8479 struct elf_link_hash_entry *h;
8481 h = elf_link_hash_lookup (elf_hash_table (info), name,
8482 FALSE, FALSE, TRUE);
8484 && (h->root.type == bfd_link_hash_defined
8485 || h->root.type == bfd_link_hash_defweak))
8487 dyn.d_un.d_val = h->root.u.def.value;
8488 o = h->root.u.def.section;
8489 if (o->output_section != NULL)
8490 dyn.d_un.d_val += (o->output_section->vma
8491 + o->output_offset);
8494 /* The symbol is imported from another shared
8495 library and does not apply to this one. */
8503 case DT_PREINIT_ARRAYSZ:
8504 name = ".preinit_array";
8506 case DT_INIT_ARRAYSZ:
8507 name = ".init_array";
8509 case DT_FINI_ARRAYSZ:
8510 name = ".fini_array";
8512 o = bfd_get_section_by_name (abfd, name);
8515 (*_bfd_error_handler)
8516 (_("%B: could not find output section %s"), abfd, name);
8520 (*_bfd_error_handler)
8521 (_("warning: %s section has zero size"), name);
8522 dyn.d_un.d_val = o->size;
8525 case DT_PREINIT_ARRAY:
8526 name = ".preinit_array";
8529 name = ".init_array";
8532 name = ".fini_array";
8545 name = ".gnu.version_d";
8548 name = ".gnu.version_r";
8551 name = ".gnu.version";
8553 o = bfd_get_section_by_name (abfd, name);
8556 (*_bfd_error_handler)
8557 (_("%B: could not find output section %s"), abfd, name);
8560 dyn.d_un.d_ptr = o->vma;
8567 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8572 for (i = 1; i < elf_numsections (abfd); i++)
8574 Elf_Internal_Shdr *hdr;
8576 hdr = elf_elfsections (abfd)[i];
8577 if (hdr->sh_type == type
8578 && (hdr->sh_flags & SHF_ALLOC) != 0)
8580 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8581 dyn.d_un.d_val += hdr->sh_size;
8584 if (dyn.d_un.d_val == 0
8585 || hdr->sh_addr < dyn.d_un.d_val)
8586 dyn.d_un.d_val = hdr->sh_addr;
8592 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8596 /* If we have created any dynamic sections, then output them. */
8599 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8602 for (o = dynobj->sections; o != NULL; o = o->next)
8604 if ((o->flags & SEC_HAS_CONTENTS) == 0
8606 || o->output_section == bfd_abs_section_ptr)
8608 if ((o->flags & SEC_LINKER_CREATED) == 0)
8610 /* At this point, we are only interested in sections
8611 created by _bfd_elf_link_create_dynamic_sections. */
8614 if (elf_hash_table (info)->stab_info.stabstr == o)
8616 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8618 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8620 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8622 if (! bfd_set_section_contents (abfd, o->output_section,
8624 (file_ptr) o->output_offset,
8630 /* The contents of the .dynstr section are actually in a
8632 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8633 if (bfd_seek (abfd, off, SEEK_SET) != 0
8634 || ! _bfd_elf_strtab_emit (abfd,
8635 elf_hash_table (info)->dynstr))
8641 if (info->relocatable)
8643 bfd_boolean failed = FALSE;
8645 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8650 /* If we have optimized stabs strings, output them. */
8651 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8653 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8657 if (info->eh_frame_hdr)
8659 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8663 if (finfo.symstrtab != NULL)
8664 _bfd_stringtab_free (finfo.symstrtab);
8665 if (finfo.contents != NULL)
8666 free (finfo.contents);
8667 if (finfo.external_relocs != NULL)
8668 free (finfo.external_relocs);
8669 if (finfo.internal_relocs != NULL)
8670 free (finfo.internal_relocs);
8671 if (finfo.external_syms != NULL)
8672 free (finfo.external_syms);
8673 if (finfo.locsym_shndx != NULL)
8674 free (finfo.locsym_shndx);
8675 if (finfo.internal_syms != NULL)
8676 free (finfo.internal_syms);
8677 if (finfo.indices != NULL)
8678 free (finfo.indices);
8679 if (finfo.sections != NULL)
8680 free (finfo.sections);
8681 if (finfo.symbuf != NULL)
8682 free (finfo.symbuf);
8683 if (finfo.symshndxbuf != NULL)
8684 free (finfo.symshndxbuf);
8685 for (o = abfd->sections; o != NULL; o = o->next)
8687 if ((o->flags & SEC_RELOC) != 0
8688 && elf_section_data (o)->rel_hashes != NULL)
8689 free (elf_section_data (o)->rel_hashes);
8692 elf_tdata (abfd)->linker = TRUE;
8697 if (finfo.symstrtab != NULL)
8698 _bfd_stringtab_free (finfo.symstrtab);
8699 if (finfo.contents != NULL)
8700 free (finfo.contents);
8701 if (finfo.external_relocs != NULL)
8702 free (finfo.external_relocs);
8703 if (finfo.internal_relocs != NULL)
8704 free (finfo.internal_relocs);
8705 if (finfo.external_syms != NULL)
8706 free (finfo.external_syms);
8707 if (finfo.locsym_shndx != NULL)
8708 free (finfo.locsym_shndx);
8709 if (finfo.internal_syms != NULL)
8710 free (finfo.internal_syms);
8711 if (finfo.indices != NULL)
8712 free (finfo.indices);
8713 if (finfo.sections != NULL)
8714 free (finfo.sections);
8715 if (finfo.symbuf != NULL)
8716 free (finfo.symbuf);
8717 if (finfo.symshndxbuf != NULL)
8718 free (finfo.symshndxbuf);
8719 for (o = abfd->sections; o != NULL; o = o->next)
8721 if ((o->flags & SEC_RELOC) != 0
8722 && elf_section_data (o)->rel_hashes != NULL)
8723 free (elf_section_data (o)->rel_hashes);
8729 /* Garbage collect unused sections. */
8731 /* The mark phase of garbage collection. For a given section, mark
8732 it and any sections in this section's group, and all the sections
8733 which define symbols to which it refers. */
8735 typedef asection * (*gc_mark_hook_fn)
8736 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8737 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8740 _bfd_elf_gc_mark (struct bfd_link_info *info,
8742 gc_mark_hook_fn gc_mark_hook)
8746 asection *group_sec;
8750 /* Mark all the sections in the group. */
8751 group_sec = elf_section_data (sec)->next_in_group;
8752 if (group_sec && !group_sec->gc_mark)
8753 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8756 /* Look through the section relocs. */
8758 is_eh = strcmp (sec->name, ".eh_frame") == 0;
8759 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8761 Elf_Internal_Rela *relstart, *rel, *relend;
8762 Elf_Internal_Shdr *symtab_hdr;
8763 struct elf_link_hash_entry **sym_hashes;
8766 bfd *input_bfd = sec->owner;
8767 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8768 Elf_Internal_Sym *isym = NULL;
8771 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8772 sym_hashes = elf_sym_hashes (input_bfd);
8774 /* Read the local symbols. */
8775 if (elf_bad_symtab (input_bfd))
8777 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8781 extsymoff = nlocsyms = symtab_hdr->sh_info;
8783 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8784 if (isym == NULL && nlocsyms != 0)
8786 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8792 /* Read the relocations. */
8793 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8795 if (relstart == NULL)
8800 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8802 if (bed->s->arch_size == 32)
8807 for (rel = relstart; rel < relend; rel++)
8809 unsigned long r_symndx;
8811 struct elf_link_hash_entry *h;
8813 r_symndx = rel->r_info >> r_sym_shift;
8817 if (r_symndx >= nlocsyms
8818 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8820 h = sym_hashes[r_symndx - extsymoff];
8821 while (h->root.type == bfd_link_hash_indirect
8822 || h->root.type == bfd_link_hash_warning)
8823 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8824 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8828 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8831 if (rsec && !rsec->gc_mark)
8833 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8836 rsec->gc_mark_from_eh = 1;
8837 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8846 if (elf_section_data (sec)->relocs != relstart)
8849 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8851 if (! info->keep_memory)
8854 symtab_hdr->contents = (unsigned char *) isym;
8861 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8864 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8868 if (h->root.type == bfd_link_hash_warning)
8869 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8871 if (h->dynindx != -1
8872 && ((h->root.type != bfd_link_hash_defined
8873 && h->root.type != bfd_link_hash_defweak)
8874 || h->root.u.def.section->gc_mark))
8875 h->dynindx = (*idx)++;
8880 /* The sweep phase of garbage collection. Remove all garbage sections. */
8882 typedef bfd_boolean (*gc_sweep_hook_fn)
8883 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8886 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8890 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8894 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8897 for (o = sub->sections; o != NULL; o = o->next)
8899 /* Keep debug and special sections. */
8900 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8901 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8907 /* Skip sweeping sections already excluded. */
8908 if (o->flags & SEC_EXCLUDE)
8911 /* Since this is early in the link process, it is simple
8912 to remove a section from the output. */
8913 o->flags |= SEC_EXCLUDE;
8915 /* But we also have to update some of the relocation
8916 info we collected before. */
8918 && (o->flags & SEC_RELOC) != 0
8919 && o->reloc_count > 0
8920 && !bfd_is_abs_section (o->output_section))
8922 Elf_Internal_Rela *internal_relocs;
8926 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8928 if (internal_relocs == NULL)
8931 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8933 if (elf_section_data (o)->relocs != internal_relocs)
8934 free (internal_relocs);
8942 /* Remove the symbols that were in the swept sections from the dynamic
8943 symbol table. GCFIXME: Anyone know how to get them out of the
8944 static symbol table as well? */
8948 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8950 /* There is an unused NULL entry at the head of the table which
8951 we must account for in our count. Unless there weren't any
8952 symbols, which means we'll have no table at all. */
8956 elf_hash_table (info)->dynsymcount = i;
8962 /* Propagate collected vtable information. This is called through
8963 elf_link_hash_traverse. */
8966 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8968 if (h->root.type == bfd_link_hash_warning)
8969 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8971 /* Those that are not vtables. */
8972 if (h->vtable == NULL || h->vtable->parent == NULL)
8975 /* Those vtables that do not have parents, we cannot merge. */
8976 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
8979 /* If we've already been done, exit. */
8980 if (h->vtable->used && h->vtable->used[-1])
8983 /* Make sure the parent's table is up to date. */
8984 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
8986 if (h->vtable->used == NULL)
8988 /* None of this table's entries were referenced. Re-use the
8990 h->vtable->used = h->vtable->parent->vtable->used;
8991 h->vtable->size = h->vtable->parent->vtable->size;
8996 bfd_boolean *cu, *pu;
8998 /* Or the parent's entries into ours. */
8999 cu = h->vtable->used;
9001 pu = h->vtable->parent->vtable->used;
9004 const struct elf_backend_data *bed;
9005 unsigned int log_file_align;
9007 bed = get_elf_backend_data (h->root.u.def.section->owner);
9008 log_file_align = bed->s->log_file_align;
9009 n = h->vtable->parent->vtable->size >> log_file_align;
9024 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
9027 bfd_vma hstart, hend;
9028 Elf_Internal_Rela *relstart, *relend, *rel;
9029 const struct elf_backend_data *bed;
9030 unsigned int log_file_align;
9032 if (h->root.type == bfd_link_hash_warning)
9033 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9035 /* Take care of both those symbols that do not describe vtables as
9036 well as those that are not loaded. */
9037 if (h->vtable == NULL || h->vtable->parent == NULL)
9040 BFD_ASSERT (h->root.type == bfd_link_hash_defined
9041 || h->root.type == bfd_link_hash_defweak);
9043 sec = h->root.u.def.section;
9044 hstart = h->root.u.def.value;
9045 hend = hstart + h->size;
9047 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
9049 return *(bfd_boolean *) okp = FALSE;
9050 bed = get_elf_backend_data (sec->owner);
9051 log_file_align = bed->s->log_file_align;
9053 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
9055 for (rel = relstart; rel < relend; ++rel)
9056 if (rel->r_offset >= hstart && rel->r_offset < hend)
9058 /* If the entry is in use, do nothing. */
9060 && (rel->r_offset - hstart) < h->vtable->size)
9062 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
9063 if (h->vtable->used[entry])
9066 /* Otherwise, kill it. */
9067 rel->r_offset = rel->r_info = rel->r_addend = 0;
9073 /* Mark sections containing dynamically referenced symbols. When
9074 building shared libraries, we must assume that any visible symbol is
9078 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
9080 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9082 if (h->root.type == bfd_link_hash_warning)
9083 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9085 if ((h->root.type == bfd_link_hash_defined
9086 || h->root.type == bfd_link_hash_defweak)
9090 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
9091 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
9092 h->root.u.def.section->flags |= SEC_KEEP;
9097 /* Do mark and sweep of unused sections. */
9100 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
9102 bfd_boolean ok = TRUE;
9104 asection * (*gc_mark_hook)
9105 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
9106 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
9108 if (!get_elf_backend_data (abfd)->can_gc_sections
9109 || info->relocatable
9110 || info->emitrelocations
9111 || !is_elf_hash_table (info->hash))
9113 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
9117 /* Apply transitive closure to the vtable entry usage info. */
9118 elf_link_hash_traverse (elf_hash_table (info),
9119 elf_gc_propagate_vtable_entries_used,
9124 /* Kill the vtable relocations that were not used. */
9125 elf_link_hash_traverse (elf_hash_table (info),
9126 elf_gc_smash_unused_vtentry_relocs,
9131 /* Mark dynamically referenced symbols. */
9132 if (elf_hash_table (info)->dynamic_sections_created)
9133 elf_link_hash_traverse (elf_hash_table (info),
9134 elf_gc_mark_dynamic_ref_symbol,
9137 /* Grovel through relocs to find out who stays ... */
9138 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
9139 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9143 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9146 for (o = sub->sections; o != NULL; o = o->next)
9147 if ((o->flags & SEC_KEEP) != 0 && !o->gc_mark)
9148 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9152 /* ... again for sections marked from eh_frame. */
9153 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9157 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9160 /* Keep .gcc_except_table.* if the associated .text.* is
9161 marked. This isn't very nice, but the proper solution,
9162 splitting .eh_frame up and using comdat doesn't pan out
9163 easily due to needing special relocs to handle the
9164 difference of two symbols in separate sections.
9165 Don't keep code sections referenced by .eh_frame. */
9166 for (o = sub->sections; o != NULL; o = o->next)
9167 if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
9169 if (strncmp (o->name, ".gcc_except_table.", 18) == 0)
9175 len = strlen (o->name + 18) + 1;
9176 fn_name = bfd_malloc (len + 6);
9177 if (fn_name == NULL)
9179 memcpy (fn_name, ".text.", 6);
9180 memcpy (fn_name + 6, o->name + 18, len);
9181 fn_text = bfd_get_section_by_name (sub, fn_name);
9183 if (fn_text == NULL || !fn_text->gc_mark)
9187 /* If not using specially named exception table section,
9188 then keep whatever we are using. */
9189 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9194 /* ... and mark SEC_EXCLUDE for those that go. */
9195 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
9201 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9204 bfd_elf_gc_record_vtinherit (bfd *abfd,
9206 struct elf_link_hash_entry *h,
9209 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
9210 struct elf_link_hash_entry **search, *child;
9211 bfd_size_type extsymcount;
9212 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9214 /* The sh_info field of the symtab header tells us where the
9215 external symbols start. We don't care about the local symbols at
9217 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
9218 if (!elf_bad_symtab (abfd))
9219 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
9221 sym_hashes = elf_sym_hashes (abfd);
9222 sym_hashes_end = sym_hashes + extsymcount;
9224 /* Hunt down the child symbol, which is in this section at the same
9225 offset as the relocation. */
9226 for (search = sym_hashes; search != sym_hashes_end; ++search)
9228 if ((child = *search) != NULL
9229 && (child->root.type == bfd_link_hash_defined
9230 || child->root.type == bfd_link_hash_defweak)
9231 && child->root.u.def.section == sec
9232 && child->root.u.def.value == offset)
9236 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
9237 abfd, sec, (unsigned long) offset);
9238 bfd_set_error (bfd_error_invalid_operation);
9244 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
9250 /* This *should* only be the absolute section. It could potentially
9251 be that someone has defined a non-global vtable though, which
9252 would be bad. It isn't worth paging in the local symbols to be
9253 sure though; that case should simply be handled by the assembler. */
9255 child->vtable->parent = (struct elf_link_hash_entry *) -1;
9258 child->vtable->parent = h;
9263 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9266 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
9267 asection *sec ATTRIBUTE_UNUSED,
9268 struct elf_link_hash_entry *h,
9271 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9272 unsigned int log_file_align = bed->s->log_file_align;
9276 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
9281 if (addend >= h->vtable->size)
9283 size_t size, bytes, file_align;
9284 bfd_boolean *ptr = h->vtable->used;
9286 /* While the symbol is undefined, we have to be prepared to handle
9288 file_align = 1 << log_file_align;
9289 if (h->root.type == bfd_link_hash_undefined)
9290 size = addend + file_align;
9296 /* Oops! We've got a reference past the defined end of
9297 the table. This is probably a bug -- shall we warn? */
9298 size = addend + file_align;
9301 size = (size + file_align - 1) & -file_align;
9303 /* Allocate one extra entry for use as a "done" flag for the
9304 consolidation pass. */
9305 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9309 ptr = bfd_realloc (ptr - 1, bytes);
9315 oldbytes = (((h->vtable->size >> log_file_align) + 1)
9316 * sizeof (bfd_boolean));
9317 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9321 ptr = bfd_zmalloc (bytes);
9326 /* And arrange for that done flag to be at index -1. */
9327 h->vtable->used = ptr + 1;
9328 h->vtable->size = size;
9331 h->vtable->used[addend >> log_file_align] = TRUE;
9336 struct alloc_got_off_arg {
9338 unsigned int got_elt_size;
9341 /* We need a special top-level link routine to convert got reference counts
9342 to real got offsets. */
9345 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9347 struct alloc_got_off_arg *gofarg = arg;
9349 if (h->root.type == bfd_link_hash_warning)
9350 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9352 if (h->got.refcount > 0)
9354 h->got.offset = gofarg->gotoff;
9355 gofarg->gotoff += gofarg->got_elt_size;
9358 h->got.offset = (bfd_vma) -1;
9363 /* And an accompanying bit to work out final got entry offsets once
9364 we're done. Should be called from final_link. */
9367 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9368 struct bfd_link_info *info)
9371 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9373 unsigned int got_elt_size = bed->s->arch_size / 8;
9374 struct alloc_got_off_arg gofarg;
9376 if (! is_elf_hash_table (info->hash))
9379 /* The GOT offset is relative to the .got section, but the GOT header is
9380 put into the .got.plt section, if the backend uses it. */
9381 if (bed->want_got_plt)
9384 gotoff = bed->got_header_size;
9386 /* Do the local .got entries first. */
9387 for (i = info->input_bfds; i; i = i->link_next)
9389 bfd_signed_vma *local_got;
9390 bfd_size_type j, locsymcount;
9391 Elf_Internal_Shdr *symtab_hdr;
9393 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9396 local_got = elf_local_got_refcounts (i);
9400 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9401 if (elf_bad_symtab (i))
9402 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9404 locsymcount = symtab_hdr->sh_info;
9406 for (j = 0; j < locsymcount; ++j)
9408 if (local_got[j] > 0)
9410 local_got[j] = gotoff;
9411 gotoff += got_elt_size;
9414 local_got[j] = (bfd_vma) -1;
9418 /* Then the global .got entries. .plt refcounts are handled by
9419 adjust_dynamic_symbol */
9420 gofarg.gotoff = gotoff;
9421 gofarg.got_elt_size = got_elt_size;
9422 elf_link_hash_traverse (elf_hash_table (info),
9423 elf_gc_allocate_got_offsets,
9428 /* Many folk need no more in the way of final link than this, once
9429 got entry reference counting is enabled. */
9432 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9434 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9437 /* Invoke the regular ELF backend linker to do all the work. */
9438 return bfd_elf_final_link (abfd, info);
9442 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9444 struct elf_reloc_cookie *rcookie = cookie;
9446 if (rcookie->bad_symtab)
9447 rcookie->rel = rcookie->rels;
9449 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9451 unsigned long r_symndx;
9453 if (! rcookie->bad_symtab)
9454 if (rcookie->rel->r_offset > offset)
9456 if (rcookie->rel->r_offset != offset)
9459 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9460 if (r_symndx == SHN_UNDEF)
9463 if (r_symndx >= rcookie->locsymcount
9464 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9466 struct elf_link_hash_entry *h;
9468 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9470 while (h->root.type == bfd_link_hash_indirect
9471 || h->root.type == bfd_link_hash_warning)
9472 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9474 if ((h->root.type == bfd_link_hash_defined
9475 || h->root.type == bfd_link_hash_defweak)
9476 && elf_discarded_section (h->root.u.def.section))
9483 /* It's not a relocation against a global symbol,
9484 but it could be a relocation against a local
9485 symbol for a discarded section. */
9487 Elf_Internal_Sym *isym;
9489 /* Need to: get the symbol; get the section. */
9490 isym = &rcookie->locsyms[r_symndx];
9491 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9493 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9494 if (isec != NULL && elf_discarded_section (isec))
9503 /* Discard unneeded references to discarded sections.
9504 Returns TRUE if any section's size was changed. */
9505 /* This function assumes that the relocations are in sorted order,
9506 which is true for all known assemblers. */
9509 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9511 struct elf_reloc_cookie cookie;
9512 asection *stab, *eh;
9513 Elf_Internal_Shdr *symtab_hdr;
9514 const struct elf_backend_data *bed;
9517 bfd_boolean ret = FALSE;
9519 if (info->traditional_format
9520 || !is_elf_hash_table (info->hash))
9523 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9525 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9528 bed = get_elf_backend_data (abfd);
9530 if ((abfd->flags & DYNAMIC) != 0)
9533 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9534 if (info->relocatable
9537 || bfd_is_abs_section (eh->output_section))))
9540 stab = bfd_get_section_by_name (abfd, ".stab");
9543 || bfd_is_abs_section (stab->output_section)
9544 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9549 && bed->elf_backend_discard_info == NULL)
9552 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9554 cookie.sym_hashes = elf_sym_hashes (abfd);
9555 cookie.bad_symtab = elf_bad_symtab (abfd);
9556 if (cookie.bad_symtab)
9558 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9559 cookie.extsymoff = 0;
9563 cookie.locsymcount = symtab_hdr->sh_info;
9564 cookie.extsymoff = symtab_hdr->sh_info;
9567 if (bed->s->arch_size == 32)
9568 cookie.r_sym_shift = 8;
9570 cookie.r_sym_shift = 32;
9572 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9573 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9575 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9576 cookie.locsymcount, 0,
9578 if (cookie.locsyms == NULL)
9585 count = stab->reloc_count;
9587 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9589 if (cookie.rels != NULL)
9591 cookie.rel = cookie.rels;
9592 cookie.relend = cookie.rels;
9593 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9594 if (_bfd_discard_section_stabs (abfd, stab,
9595 elf_section_data (stab)->sec_info,
9596 bfd_elf_reloc_symbol_deleted_p,
9599 if (elf_section_data (stab)->relocs != cookie.rels)
9607 count = eh->reloc_count;
9609 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9611 cookie.rel = cookie.rels;
9612 cookie.relend = cookie.rels;
9613 if (cookie.rels != NULL)
9614 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9616 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9617 bfd_elf_reloc_symbol_deleted_p,
9621 if (cookie.rels != NULL
9622 && elf_section_data (eh)->relocs != cookie.rels)
9626 if (bed->elf_backend_discard_info != NULL
9627 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9630 if (cookie.locsyms != NULL
9631 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9633 if (! info->keep_memory)
9634 free (cookie.locsyms);
9636 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9640 if (info->eh_frame_hdr
9641 && !info->relocatable
9642 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9649 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9652 const char *name, *p;
9653 struct bfd_section_already_linked *l;
9654 struct bfd_section_already_linked_hash_entry *already_linked_list;
9657 /* A single member comdat group section may be discarded by a
9658 linkonce section. See below. */
9659 if (sec->output_section == bfd_abs_section_ptr)
9664 /* Check if it belongs to a section group. */
9665 group = elf_sec_group (sec);
9667 /* Return if it isn't a linkonce section nor a member of a group. A
9668 comdat group section also has SEC_LINK_ONCE set. */
9669 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9674 /* If this is the member of a single member comdat group, check if
9675 the group should be discarded. */
9676 if (elf_next_in_group (sec) == sec
9677 && (group->flags & SEC_LINK_ONCE) != 0)
9683 /* FIXME: When doing a relocatable link, we may have trouble
9684 copying relocations in other sections that refer to local symbols
9685 in the section being discarded. Those relocations will have to
9686 be converted somehow; as of this writing I'm not sure that any of
9687 the backends handle that correctly.
9689 It is tempting to instead not discard link once sections when
9690 doing a relocatable link (technically, they should be discarded
9691 whenever we are building constructors). However, that fails,
9692 because the linker winds up combining all the link once sections
9693 into a single large link once section, which defeats the purpose
9694 of having link once sections in the first place.
9696 Also, not merging link once sections in a relocatable link
9697 causes trouble for MIPS ELF, which relies on link once semantics
9698 to handle the .reginfo section correctly. */
9700 name = bfd_get_section_name (abfd, sec);
9702 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9703 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
9708 already_linked_list = bfd_section_already_linked_table_lookup (p);
9710 for (l = already_linked_list->entry; l != NULL; l = l->next)
9712 /* We may have 3 different sections on the list: group section,
9713 comdat section and linkonce section. SEC may be a linkonce or
9714 group section. We match a group section with a group section,
9715 a linkonce section with a linkonce section, and ignore comdat
9717 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9718 && strcmp (name, l->sec->name) == 0
9719 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9721 /* The section has already been linked. See if we should
9723 switch (flags & SEC_LINK_DUPLICATES)
9728 case SEC_LINK_DUPLICATES_DISCARD:
9731 case SEC_LINK_DUPLICATES_ONE_ONLY:
9732 (*_bfd_error_handler)
9733 (_("%B: ignoring duplicate section `%A'"),
9737 case SEC_LINK_DUPLICATES_SAME_SIZE:
9738 if (sec->size != l->sec->size)
9739 (*_bfd_error_handler)
9740 (_("%B: duplicate section `%A' has different size"),
9744 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9745 if (sec->size != l->sec->size)
9746 (*_bfd_error_handler)
9747 (_("%B: duplicate section `%A' has different size"),
9749 else if (sec->size != 0)
9751 bfd_byte *sec_contents, *l_sec_contents;
9753 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9754 (*_bfd_error_handler)
9755 (_("%B: warning: could not read contents of section `%A'"),
9757 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9759 (*_bfd_error_handler)
9760 (_("%B: warning: could not read contents of section `%A'"),
9761 l->sec->owner, l->sec);
9762 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9763 (*_bfd_error_handler)
9764 (_("%B: warning: duplicate section `%A' has different contents"),
9768 free (sec_contents);
9770 free (l_sec_contents);
9775 /* Set the output_section field so that lang_add_section
9776 does not create a lang_input_section structure for this
9777 section. Since there might be a symbol in the section
9778 being discarded, we must retain a pointer to the section
9779 which we are really going to use. */
9780 sec->output_section = bfd_abs_section_ptr;
9781 sec->kept_section = l->sec;
9783 if (flags & SEC_GROUP)
9785 asection *first = elf_next_in_group (sec);
9786 asection *s = first;
9790 s->output_section = bfd_abs_section_ptr;
9791 /* Record which group discards it. */
9792 s->kept_section = l->sec;
9793 s = elf_next_in_group (s);
9794 /* These lists are circular. */
9806 /* If this is the member of a single member comdat group and the
9807 group hasn't be discarded, we check if it matches a linkonce
9808 section. We only record the discarded comdat group. Otherwise
9809 the undiscarded group will be discarded incorrectly later since
9810 itself has been recorded. */
9811 for (l = already_linked_list->entry; l != NULL; l = l->next)
9812 if ((l->sec->flags & SEC_GROUP) == 0
9813 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9814 && bfd_elf_match_symbols_in_sections (l->sec,
9815 elf_next_in_group (sec)))
9817 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
9818 elf_next_in_group (sec)->kept_section = l->sec;
9819 group->output_section = bfd_abs_section_ptr;
9826 /* There is no direct match. But for linkonce section, we should
9827 check if there is a match with comdat group member. We always
9828 record the linkonce section, discarded or not. */
9829 for (l = already_linked_list->entry; l != NULL; l = l->next)
9830 if (l->sec->flags & SEC_GROUP)
9832 asection *first = elf_next_in_group (l->sec);
9835 && elf_next_in_group (first) == first
9836 && bfd_elf_match_symbols_in_sections (first, sec))
9838 sec->output_section = bfd_abs_section_ptr;
9839 sec->kept_section = l->sec;
9844 /* This is the first section with this name. Record it. */
9845 bfd_section_already_linked_table_insert (already_linked_list, sec);
9849 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
9851 return sym->st_shndx == SHN_COMMON;
9855 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
9861 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
9863 return bfd_com_section_ptr;