1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 (abfd, ".got");
64 || !bfd_set_section_flags (abfd, s, flags)
65 || !bfd_set_section_alignment (abfd, s, ptralign))
68 if (bed->want_got_plt)
70 s = bfd_make_section (abfd, ".got.plt");
72 || !bfd_set_section_flags (abfd, s, flags)
73 || !bfd_set_section_alignment (abfd, s, ptralign))
77 if (bed->want_got_sym)
79 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
80 (or .got.plt) section. We don't do this in the linker script
81 because we don't want to define the symbol if we are not creating
82 a global offset table. */
84 if (!(_bfd_generic_link_add_one_symbol
85 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
86 bed->got_symbol_offset, NULL, FALSE, bed->collect, &bh)))
88 h = (struct elf_link_hash_entry *) bh;
91 h->other = STV_HIDDEN;
93 if (! info->executable
94 && ! bfd_elf_link_record_dynamic_symbol (info, h))
97 elf_hash_table (info)->hgot = h;
100 /* The first bit of the global offset table is the header. */
101 s->size += bed->got_header_size + bed->got_symbol_offset;
106 /* Create some sections which will be filled in with dynamic linking
107 information. ABFD is an input file which requires dynamic sections
108 to be created. The dynamic sections take up virtual memory space
109 when the final executable is run, so we need to create them before
110 addresses are assigned to the output sections. We work out the
111 actual contents and size of these sections later. */
114 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
117 register asection *s;
118 struct elf_link_hash_entry *h;
119 struct bfd_link_hash_entry *bh;
120 const struct elf_backend_data *bed;
122 if (! is_elf_hash_table (info->hash))
125 if (elf_hash_table (info)->dynamic_sections_created)
128 /* Make sure that all dynamic sections use the same input BFD. */
129 if (elf_hash_table (info)->dynobj == NULL)
130 elf_hash_table (info)->dynobj = abfd;
132 abfd = elf_hash_table (info)->dynobj;
134 bed = get_elf_backend_data (abfd);
136 flags = bed->dynamic_sec_flags;
138 /* A dynamically linked executable has a .interp section, but a
139 shared library does not. */
140 if (info->executable)
142 s = bfd_make_section (abfd, ".interp");
144 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
148 if (! info->traditional_format)
150 s = bfd_make_section (abfd, ".eh_frame_hdr");
152 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
153 || ! bfd_set_section_alignment (abfd, s, 2))
155 elf_hash_table (info)->eh_info.hdr_sec = s;
158 /* Create sections to hold version informations. These are removed
159 if they are not needed. */
160 s = bfd_make_section (abfd, ".gnu.version_d");
162 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
163 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
166 s = bfd_make_section (abfd, ".gnu.version");
168 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
169 || ! bfd_set_section_alignment (abfd, s, 1))
172 s = bfd_make_section (abfd, ".gnu.version_r");
174 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
175 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
178 s = bfd_make_section (abfd, ".dynsym");
180 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
181 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
184 s = bfd_make_section (abfd, ".dynstr");
186 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
189 /* Create a strtab to hold the dynamic symbol names. */
190 if (elf_hash_table (info)->dynstr == NULL)
192 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
193 if (elf_hash_table (info)->dynstr == NULL)
197 s = bfd_make_section (abfd, ".dynamic");
199 || ! bfd_set_section_flags (abfd, s, flags)
200 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
203 /* The special symbol _DYNAMIC is always set to the start of the
204 .dynamic section. This call occurs before we have processed the
205 symbols for any dynamic object, so we don't have to worry about
206 overriding a dynamic definition. We could set _DYNAMIC in a
207 linker script, but we only want to define it if we are, in fact,
208 creating a .dynamic section. We don't want to define it if there
209 is no .dynamic section, since on some ELF platforms the start up
210 code examines it to decide how to initialize the process. */
212 if (! (_bfd_generic_link_add_one_symbol
213 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE,
214 get_elf_backend_data (abfd)->collect, &bh)))
216 h = (struct elf_link_hash_entry *) bh;
218 h->type = STT_OBJECT;
220 if (! info->executable
221 && ! bfd_elf_link_record_dynamic_symbol (info, h))
224 s = bfd_make_section (abfd, ".hash");
226 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
227 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
229 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
231 /* Let the backend create the rest of the sections. This lets the
232 backend set the right flags. The backend will normally create
233 the .got and .plt sections. */
234 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
237 elf_hash_table (info)->dynamic_sections_created = TRUE;
242 /* Create dynamic sections when linking against a dynamic object. */
245 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
247 flagword flags, pltflags;
249 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
251 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
252 .rel[a].bss sections. */
253 flags = bed->dynamic_sec_flags;
256 if (bed->plt_not_loaded)
257 /* We do not clear SEC_ALLOC here because we still want the OS to
258 allocate space for the section; it's just that there's nothing
259 to read in from the object file. */
260 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
262 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
263 if (bed->plt_readonly)
264 pltflags |= SEC_READONLY;
266 s = bfd_make_section (abfd, ".plt");
268 || ! bfd_set_section_flags (abfd, s, pltflags)
269 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
272 if (bed->want_plt_sym)
274 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
276 struct elf_link_hash_entry *h;
277 struct bfd_link_hash_entry *bh = NULL;
279 if (! (_bfd_generic_link_add_one_symbol
280 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
281 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
283 h = (struct elf_link_hash_entry *) bh;
285 h->type = STT_OBJECT;
287 if (! info->executable
288 && ! bfd_elf_link_record_dynamic_symbol (info, h))
292 s = bfd_make_section (abfd,
293 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
295 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
296 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
299 if (! _bfd_elf_create_got_section (abfd, info))
302 if (bed->want_dynbss)
304 /* The .dynbss section is a place to put symbols which are defined
305 by dynamic objects, are referenced by regular objects, and are
306 not functions. We must allocate space for them in the process
307 image and use a R_*_COPY reloc to tell the dynamic linker to
308 initialize them at run time. The linker script puts the .dynbss
309 section into the .bss section of the final image. */
310 s = bfd_make_section (abfd, ".dynbss");
312 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
315 /* The .rel[a].bss section holds copy relocs. This section is not
316 normally needed. We need to create it here, though, so that the
317 linker will map it to an output section. We can't just create it
318 only if we need it, because we will not know whether we need it
319 until we have seen all the input files, and the first time the
320 main linker code calls BFD after examining all the input files
321 (size_dynamic_sections) the input sections have already been
322 mapped to the output sections. If the section turns out not to
323 be needed, we can discard it later. We will never need this
324 section when generating a shared object, since they do not use
328 s = bfd_make_section (abfd,
329 (bed->default_use_rela_p
330 ? ".rela.bss" : ".rel.bss"));
332 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
333 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
341 /* Record a new dynamic symbol. We record the dynamic symbols as we
342 read the input files, since we need to have a list of all of them
343 before we can determine the final sizes of the output sections.
344 Note that we may actually call this function even though we are not
345 going to output any dynamic symbols; in some cases we know that a
346 symbol should be in the dynamic symbol table, but only if there is
350 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
351 struct elf_link_hash_entry *h)
353 if (h->dynindx == -1)
355 struct elf_strtab_hash *dynstr;
360 /* XXX: The ABI draft says the linker must turn hidden and
361 internal symbols into STB_LOCAL symbols when producing the
362 DSO. However, if ld.so honors st_other in the dynamic table,
363 this would not be necessary. */
364 switch (ELF_ST_VISIBILITY (h->other))
368 if (h->root.type != bfd_link_hash_undefined
369 && h->root.type != bfd_link_hash_undefweak)
379 h->dynindx = elf_hash_table (info)->dynsymcount;
380 ++elf_hash_table (info)->dynsymcount;
382 dynstr = elf_hash_table (info)->dynstr;
385 /* Create a strtab to hold the dynamic symbol names. */
386 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
391 /* We don't put any version information in the dynamic string
393 name = h->root.root.string;
394 p = strchr (name, ELF_VER_CHR);
396 /* We know that the p points into writable memory. In fact,
397 there are only a few symbols that have read-only names, being
398 those like _GLOBAL_OFFSET_TABLE_ that are created specially
399 by the backends. Most symbols will have names pointing into
400 an ELF string table read from a file, or to objalloc memory. */
403 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
408 if (indx == (bfd_size_type) -1)
410 h->dynstr_index = indx;
416 /* Record an assignment to a symbol made by a linker script. We need
417 this in case some dynamic object refers to this symbol. */
420 bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED,
421 struct bfd_link_info *info,
425 struct elf_link_hash_entry *h;
427 if (!is_elf_hash_table (info->hash))
430 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE);
434 /* Since we're defining the symbol, don't let it seem to have not
435 been defined. record_dynamic_symbol and size_dynamic_sections
437 ??? Changing bfd_link_hash_undefined to bfd_link_hash_new (or
438 to bfd_link_hash_undefweak, see linker.c:link_action) runs the risk
439 of some later symbol manipulation setting the symbol back to
440 bfd_link_hash_undefined, and the linker trying to add the symbol to
441 the undefs list twice. */
442 if (h->root.type == bfd_link_hash_undefweak
443 || h->root.type == bfd_link_hash_undefined)
444 h->root.type = bfd_link_hash_new;
446 if (h->root.type == bfd_link_hash_new)
449 /* If this symbol is being provided by the linker script, and it is
450 currently defined by a dynamic object, but not by a regular
451 object, then mark it as undefined so that the generic linker will
452 force the correct value. */
456 h->root.type = bfd_link_hash_undefined;
458 /* If this symbol is not being provided by the linker script, and it is
459 currently defined by a dynamic object, but not by a regular object,
460 then clear out any version information because the symbol will not be
461 associated with the dynamic object any more. */
465 h->verinfo.verdef = NULL;
474 if (! bfd_elf_link_record_dynamic_symbol (info, h))
477 /* If this is a weak defined symbol, and we know a corresponding
478 real symbol from the same dynamic object, make sure the real
479 symbol is also made into a dynamic symbol. */
480 if (h->u.weakdef != NULL
481 && h->u.weakdef->dynindx == -1)
483 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
491 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
492 success, and 2 on a failure caused by attempting to record a symbol
493 in a discarded section, eg. a discarded link-once section symbol. */
496 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_local_dynamic_entry *entry;
502 struct elf_link_hash_table *eht;
503 struct elf_strtab_hash *dynstr;
504 unsigned long dynstr_index;
506 Elf_External_Sym_Shndx eshndx;
507 char esym[sizeof (Elf64_External_Sym)];
509 if (! is_elf_hash_table (info->hash))
512 /* See if the entry exists already. */
513 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
514 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
517 amt = sizeof (*entry);
518 entry = bfd_alloc (input_bfd, amt);
522 /* Go find the symbol, so that we can find it's name. */
523 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
524 1, input_indx, &entry->isym, esym, &eshndx))
526 bfd_release (input_bfd, entry);
530 if (entry->isym.st_shndx != SHN_UNDEF
531 && (entry->isym.st_shndx < SHN_LORESERVE
532 || entry->isym.st_shndx > SHN_HIRESERVE))
536 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
537 if (s == NULL || bfd_is_abs_section (s->output_section))
539 /* We can still bfd_release here as nothing has done another
540 bfd_alloc. We can't do this later in this function. */
541 bfd_release (input_bfd, entry);
546 name = (bfd_elf_string_from_elf_section
547 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
548 entry->isym.st_name));
550 dynstr = elf_hash_table (info)->dynstr;
553 /* Create a strtab to hold the dynamic symbol names. */
554 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
559 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
560 if (dynstr_index == (unsigned long) -1)
562 entry->isym.st_name = dynstr_index;
564 eht = elf_hash_table (info);
566 entry->next = eht->dynlocal;
567 eht->dynlocal = entry;
568 entry->input_bfd = input_bfd;
569 entry->input_indx = input_indx;
572 /* Whatever binding the symbol had before, it's now local. */
574 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
576 /* The dynindx will be set at the end of size_dynamic_sections. */
581 /* Return the dynindex of a local dynamic symbol. */
584 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
588 struct elf_link_local_dynamic_entry *e;
590 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
591 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
596 /* This function is used to renumber the dynamic symbols, if some of
597 them are removed because they are marked as local. This is called
598 via elf_link_hash_traverse. */
601 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
604 size_t *count = data;
606 if (h->root.type == bfd_link_hash_warning)
607 h = (struct elf_link_hash_entry *) h->root.u.i.link;
609 if (h->dynindx != -1)
610 h->dynindx = ++(*count);
615 /* Return true if the dynamic symbol for a given section should be
616 omitted when creating a shared library. */
618 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
619 struct bfd_link_info *info,
622 switch (elf_section_data (p)->this_hdr.sh_type)
626 /* If sh_type is yet undecided, assume it could be
627 SHT_PROGBITS/SHT_NOBITS. */
629 if (strcmp (p->name, ".got") == 0
630 || strcmp (p->name, ".got.plt") == 0
631 || strcmp (p->name, ".plt") == 0)
634 bfd *dynobj = elf_hash_table (info)->dynobj;
637 && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL
638 && (ip->flags & SEC_LINKER_CREATED)
639 && ip->output_section == p)
644 /* There shouldn't be section relative relocations
645 against any other section. */
651 /* Assign dynsym indices. In a shared library we generate a section
652 symbol for each output section, which come first. Next come all of
653 the back-end allocated local dynamic syms, followed by the rest of
654 the global symbols. */
657 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
659 unsigned long dynsymcount = 0;
663 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
665 for (p = output_bfd->sections; p ; p = p->next)
666 if ((p->flags & SEC_EXCLUDE) == 0
667 && (p->flags & SEC_ALLOC) != 0
668 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
669 elf_section_data (p)->dynindx = ++dynsymcount;
672 if (elf_hash_table (info)->dynlocal)
674 struct elf_link_local_dynamic_entry *p;
675 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
676 p->dynindx = ++dynsymcount;
679 elf_link_hash_traverse (elf_hash_table (info),
680 elf_link_renumber_hash_table_dynsyms,
683 /* There is an unused NULL entry at the head of the table which
684 we must account for in our count. Unless there weren't any
685 symbols, which means we'll have no table at all. */
686 if (dynsymcount != 0)
689 return elf_hash_table (info)->dynsymcount = dynsymcount;
692 /* This function is called when we want to define a new symbol. It
693 handles the various cases which arise when we find a definition in
694 a dynamic object, or when there is already a definition in a
695 dynamic object. The new symbol is described by NAME, SYM, PSEC,
696 and PVALUE. We set SYM_HASH to the hash table entry. We set
697 OVERRIDE if the old symbol is overriding a new definition. We set
698 TYPE_CHANGE_OK if it is OK for the type to change. We set
699 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
700 change, we mean that we shouldn't warn if the type or size does
704 _bfd_elf_merge_symbol (bfd *abfd,
705 struct bfd_link_info *info,
707 Elf_Internal_Sym *sym,
710 struct elf_link_hash_entry **sym_hash,
712 bfd_boolean *override,
713 bfd_boolean *type_change_ok,
714 bfd_boolean *size_change_ok)
716 asection *sec, *oldsec;
717 struct elf_link_hash_entry *h;
718 struct elf_link_hash_entry *flip;
721 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
722 bfd_boolean newweak, oldweak, old_asneeded;
728 bind = ELF_ST_BIND (sym->st_info);
730 if (! bfd_is_und_section (sec))
731 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
733 h = ((struct elf_link_hash_entry *)
734 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
739 /* This code is for coping with dynamic objects, and is only useful
740 if we are doing an ELF link. */
741 if (info->hash->creator != abfd->xvec)
744 /* For merging, we only care about real symbols. */
746 while (h->root.type == bfd_link_hash_indirect
747 || h->root.type == bfd_link_hash_warning)
748 h = (struct elf_link_hash_entry *) h->root.u.i.link;
750 /* If we just created the symbol, mark it as being an ELF symbol.
751 Other than that, there is nothing to do--there is no merge issue
752 with a newly defined symbol--so we just return. */
754 if (h->root.type == bfd_link_hash_new)
760 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
763 switch (h->root.type)
770 case bfd_link_hash_undefined:
771 case bfd_link_hash_undefweak:
772 oldbfd = h->root.u.undef.abfd;
776 case bfd_link_hash_defined:
777 case bfd_link_hash_defweak:
778 oldbfd = h->root.u.def.section->owner;
779 oldsec = h->root.u.def.section;
782 case bfd_link_hash_common:
783 oldbfd = h->root.u.c.p->section->owner;
784 oldsec = h->root.u.c.p->section;
788 /* In cases involving weak versioned symbols, we may wind up trying
789 to merge a symbol with itself. Catch that here, to avoid the
790 confusion that results if we try to override a symbol with
791 itself. The additional tests catch cases like
792 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
793 dynamic object, which we do want to handle here. */
795 && ((abfd->flags & DYNAMIC) == 0
799 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
800 respectively, is from a dynamic object. */
802 if ((abfd->flags & DYNAMIC) != 0)
808 olddyn = (oldbfd->flags & DYNAMIC) != 0;
813 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
814 indices used by MIPS ELF. */
815 switch (h->root.type)
821 case bfd_link_hash_defined:
822 case bfd_link_hash_defweak:
823 hsec = h->root.u.def.section;
826 case bfd_link_hash_common:
827 hsec = h->root.u.c.p->section;
834 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
837 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
838 respectively, appear to be a definition rather than reference. */
840 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
845 if (h->root.type == bfd_link_hash_undefined
846 || h->root.type == bfd_link_hash_undefweak
847 || h->root.type == bfd_link_hash_common)
852 /* If the old definition came from an as-needed dynamic library which
853 wasn't found to be needed, treat the sym as undefined. */
854 old_asneeded = FALSE;
857 && (elf_dyn_lib_class (oldbfd) & DYN_AS_NEEDED) != 0)
860 /* Check TLS symbol. */
861 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
862 && ELF_ST_TYPE (sym->st_info) != h->type)
865 bfd_boolean ntdef, tdef;
866 asection *ntsec, *tsec;
868 if (h->type == STT_TLS)
888 (*_bfd_error_handler)
889 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
890 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
891 else if (!tdef && !ntdef)
892 (*_bfd_error_handler)
893 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
894 tbfd, ntbfd, h->root.root.string);
896 (*_bfd_error_handler)
897 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
898 tbfd, tsec, ntbfd, h->root.root.string);
900 (*_bfd_error_handler)
901 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
902 tbfd, ntbfd, ntsec, h->root.root.string);
904 bfd_set_error (bfd_error_bad_value);
908 /* We need to remember if a symbol has a definition in a dynamic
909 object or is weak in all dynamic objects. Internal and hidden
910 visibility will make it unavailable to dynamic objects. */
911 if (newdyn && !h->dynamic_def)
913 if (!bfd_is_und_section (sec))
917 /* Check if this symbol is weak in all dynamic objects. If it
918 is the first time we see it in a dynamic object, we mark
919 if it is weak. Otherwise, we clear it. */
922 if (bind == STB_WEAK)
925 else if (bind != STB_WEAK)
930 /* If the old symbol has non-default visibility, we ignore the new
931 definition from a dynamic object. */
933 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
934 && !bfd_is_und_section (sec))
937 /* Make sure this symbol is dynamic. */
939 /* A protected symbol has external availability. Make sure it is
942 FIXME: Should we check type and size for protected symbol? */
943 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
944 return bfd_elf_link_record_dynamic_symbol (info, h);
949 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
952 /* If the new symbol with non-default visibility comes from a
953 relocatable file and the old definition comes from a dynamic
954 object, we remove the old definition. */
955 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
958 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
959 && bfd_is_und_section (sec))
961 /* If the new symbol is undefined and the old symbol was
962 also undefined before, we need to make sure
963 _bfd_generic_link_add_one_symbol doesn't mess
964 up the linker hash table undefs list. Since the old
965 definition came from a dynamic object, it is still on the
967 h->root.type = bfd_link_hash_undefined;
968 h->root.u.undef.abfd = abfd;
972 h->root.type = bfd_link_hash_new;
973 h->root.u.undef.abfd = NULL;
982 /* FIXME: Should we check type and size for protected symbol? */
988 /* Differentiate strong and weak symbols. */
989 newweak = bind == STB_WEAK;
990 oldweak = (h->root.type == bfd_link_hash_defweak
991 || h->root.type == bfd_link_hash_undefweak);
993 /* If a new weak symbol definition comes from a regular file and the
994 old symbol comes from a dynamic library, we treat the new one as
995 strong. Similarly, an old weak symbol definition from a regular
996 file is treated as strong when the new symbol comes from a dynamic
997 library. Further, an old weak symbol from a dynamic library is
998 treated as strong if the new symbol is from a dynamic library.
999 This reflects the way glibc's ld.so works.
1001 Do this before setting *type_change_ok or *size_change_ok so that
1002 we warn properly when dynamic library symbols are overridden. */
1004 if (newdef && !newdyn && olddyn)
1006 if (olddef && newdyn)
1009 /* It's OK to change the type if either the existing symbol or the
1010 new symbol is weak. A type change is also OK if the old symbol
1011 is undefined and the new symbol is defined. */
1016 && h->root.type == bfd_link_hash_undefined))
1017 *type_change_ok = TRUE;
1019 /* It's OK to change the size if either the existing symbol or the
1020 new symbol is weak, or if the old symbol is undefined. */
1023 || h->root.type == bfd_link_hash_undefined)
1024 *size_change_ok = TRUE;
1026 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1027 symbol, respectively, appears to be a common symbol in a dynamic
1028 object. If a symbol appears in an uninitialized section, and is
1029 not weak, and is not a function, then it may be a common symbol
1030 which was resolved when the dynamic object was created. We want
1031 to treat such symbols specially, because they raise special
1032 considerations when setting the symbol size: if the symbol
1033 appears as a common symbol in a regular object, and the size in
1034 the regular object is larger, we must make sure that we use the
1035 larger size. This problematic case can always be avoided in C,
1036 but it must be handled correctly when using Fortran shared
1039 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1040 likewise for OLDDYNCOMMON and OLDDEF.
1042 Note that this test is just a heuristic, and that it is quite
1043 possible to have an uninitialized symbol in a shared object which
1044 is really a definition, rather than a common symbol. This could
1045 lead to some minor confusion when the symbol really is a common
1046 symbol in some regular object. However, I think it will be
1052 && (sec->flags & SEC_ALLOC) != 0
1053 && (sec->flags & SEC_LOAD) == 0
1055 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1056 newdyncommon = TRUE;
1058 newdyncommon = FALSE;
1063 && h->root.type == bfd_link_hash_defined
1065 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1066 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1068 && h->type != STT_FUNC)
1069 olddyncommon = TRUE;
1071 olddyncommon = FALSE;
1073 /* If both the old and the new symbols look like common symbols in a
1074 dynamic object, set the size of the symbol to the larger of the
1079 && sym->st_size != h->size)
1081 /* Since we think we have two common symbols, issue a multiple
1082 common warning if desired. Note that we only warn if the
1083 size is different. If the size is the same, we simply let
1084 the old symbol override the new one as normally happens with
1085 symbols defined in dynamic objects. */
1087 if (! ((*info->callbacks->multiple_common)
1088 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1089 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1092 if (sym->st_size > h->size)
1093 h->size = sym->st_size;
1095 *size_change_ok = TRUE;
1098 /* If we are looking at a dynamic object, and we have found a
1099 definition, we need to see if the symbol was already defined by
1100 some other object. If so, we want to use the existing
1101 definition, and we do not want to report a multiple symbol
1102 definition error; we do this by clobbering *PSEC to be
1103 bfd_und_section_ptr.
1105 We treat a common symbol as a definition if the symbol in the
1106 shared library is a function, since common symbols always
1107 represent variables; this can cause confusion in principle, but
1108 any such confusion would seem to indicate an erroneous program or
1109 shared library. We also permit a common symbol in a regular
1110 object to override a weak symbol in a shared object. */
1114 && ((olddef && !old_asneeded)
1115 || (h->root.type == bfd_link_hash_common
1117 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1121 newdyncommon = FALSE;
1123 *psec = sec = bfd_und_section_ptr;
1124 *size_change_ok = TRUE;
1126 /* If we get here when the old symbol is a common symbol, then
1127 we are explicitly letting it override a weak symbol or
1128 function in a dynamic object, and we don't want to warn about
1129 a type change. If the old symbol is a defined symbol, a type
1130 change warning may still be appropriate. */
1132 if (h->root.type == bfd_link_hash_common)
1133 *type_change_ok = TRUE;
1136 /* Handle the special case of an old common symbol merging with a
1137 new symbol which looks like a common symbol in a shared object.
1138 We change *PSEC and *PVALUE to make the new symbol look like a
1139 common symbol, and let _bfd_generic_link_add_one_symbol will do
1143 && h->root.type == bfd_link_hash_common)
1147 newdyncommon = FALSE;
1148 *pvalue = sym->st_size;
1149 *psec = sec = bfd_com_section_ptr;
1150 *size_change_ok = TRUE;
1153 /* If the old symbol is from a dynamic object, and the new symbol is
1154 a definition which is not from a dynamic object, then the new
1155 symbol overrides the old symbol. Symbols from regular files
1156 always take precedence over symbols from dynamic objects, even if
1157 they are defined after the dynamic object in the link.
1159 As above, we again permit a common symbol in a regular object to
1160 override a definition in a shared object if the shared object
1161 symbol is a function or is weak. */
1164 if ((!newdyn || old_asneeded)
1166 || (bfd_is_com_section (sec)
1168 || h->type == STT_FUNC)))
1173 /* Change the hash table entry to undefined, and let
1174 _bfd_generic_link_add_one_symbol do the right thing with the
1177 h->root.type = bfd_link_hash_undefined;
1178 h->root.u.undef.abfd = h->root.u.def.section->owner;
1179 *size_change_ok = TRUE;
1182 olddyncommon = FALSE;
1184 /* We again permit a type change when a common symbol may be
1185 overriding a function. */
1187 if (bfd_is_com_section (sec))
1188 *type_change_ok = TRUE;
1190 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1193 /* This union may have been set to be non-NULL when this symbol
1194 was seen in a dynamic object. We must force the union to be
1195 NULL, so that it is correct for a regular symbol. */
1196 h->verinfo.vertree = NULL;
1199 /* Handle the special case of a new common symbol merging with an
1200 old symbol that looks like it might be a common symbol defined in
1201 a shared object. Note that we have already handled the case in
1202 which a new common symbol should simply override the definition
1203 in the shared library. */
1206 && bfd_is_com_section (sec)
1209 /* It would be best if we could set the hash table entry to a
1210 common symbol, but we don't know what to use for the section
1211 or the alignment. */
1212 if (! ((*info->callbacks->multiple_common)
1213 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1214 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1217 /* If the presumed common symbol in the dynamic object is
1218 larger, pretend that the new symbol has its size. */
1220 if (h->size > *pvalue)
1223 /* FIXME: We no longer know the alignment required by the symbol
1224 in the dynamic object, so we just wind up using the one from
1225 the regular object. */
1228 olddyncommon = FALSE;
1230 h->root.type = bfd_link_hash_undefined;
1231 h->root.u.undef.abfd = h->root.u.def.section->owner;
1233 *size_change_ok = TRUE;
1234 *type_change_ok = TRUE;
1236 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1239 h->verinfo.vertree = NULL;
1244 /* Handle the case where we had a versioned symbol in a dynamic
1245 library and now find a definition in a normal object. In this
1246 case, we make the versioned symbol point to the normal one. */
1247 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1248 flip->root.type = h->root.type;
1249 h->root.type = bfd_link_hash_indirect;
1250 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1251 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1252 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1256 flip->ref_dynamic = 1;
1263 /* This function is called to create an indirect symbol from the
1264 default for the symbol with the default version if needed. The
1265 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1266 set DYNSYM if the new indirect symbol is dynamic. */
1269 _bfd_elf_add_default_symbol (bfd *abfd,
1270 struct bfd_link_info *info,
1271 struct elf_link_hash_entry *h,
1273 Elf_Internal_Sym *sym,
1276 bfd_boolean *dynsym,
1277 bfd_boolean override)
1279 bfd_boolean type_change_ok;
1280 bfd_boolean size_change_ok;
1283 struct elf_link_hash_entry *hi;
1284 struct bfd_link_hash_entry *bh;
1285 const struct elf_backend_data *bed;
1286 bfd_boolean collect;
1287 bfd_boolean dynamic;
1289 size_t len, shortlen;
1292 /* If this symbol has a version, and it is the default version, we
1293 create an indirect symbol from the default name to the fully
1294 decorated name. This will cause external references which do not
1295 specify a version to be bound to this version of the symbol. */
1296 p = strchr (name, ELF_VER_CHR);
1297 if (p == NULL || p[1] != ELF_VER_CHR)
1302 /* We are overridden by an old definition. We need to check if we
1303 need to create the indirect symbol from the default name. */
1304 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1306 BFD_ASSERT (hi != NULL);
1309 while (hi->root.type == bfd_link_hash_indirect
1310 || hi->root.type == bfd_link_hash_warning)
1312 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1318 bed = get_elf_backend_data (abfd);
1319 collect = bed->collect;
1320 dynamic = (abfd->flags & DYNAMIC) != 0;
1322 shortlen = p - name;
1323 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1324 if (shortname == NULL)
1326 memcpy (shortname, name, shortlen);
1327 shortname[shortlen] = '\0';
1329 /* We are going to create a new symbol. Merge it with any existing
1330 symbol with this name. For the purposes of the merge, act as
1331 though we were defining the symbol we just defined, although we
1332 actually going to define an indirect symbol. */
1333 type_change_ok = FALSE;
1334 size_change_ok = FALSE;
1336 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1337 &hi, &skip, &override, &type_change_ok,
1347 if (! (_bfd_generic_link_add_one_symbol
1348 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1349 0, name, FALSE, collect, &bh)))
1351 hi = (struct elf_link_hash_entry *) bh;
1355 /* In this case the symbol named SHORTNAME is overriding the
1356 indirect symbol we want to add. We were planning on making
1357 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1358 is the name without a version. NAME is the fully versioned
1359 name, and it is the default version.
1361 Overriding means that we already saw a definition for the
1362 symbol SHORTNAME in a regular object, and it is overriding
1363 the symbol defined in the dynamic object.
1365 When this happens, we actually want to change NAME, the
1366 symbol we just added, to refer to SHORTNAME. This will cause
1367 references to NAME in the shared object to become references
1368 to SHORTNAME in the regular object. This is what we expect
1369 when we override a function in a shared object: that the
1370 references in the shared object will be mapped to the
1371 definition in the regular object. */
1373 while (hi->root.type == bfd_link_hash_indirect
1374 || hi->root.type == bfd_link_hash_warning)
1375 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1377 h->root.type = bfd_link_hash_indirect;
1378 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1382 hi->ref_dynamic = 1;
1386 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1391 /* Now set HI to H, so that the following code will set the
1392 other fields correctly. */
1396 /* If there is a duplicate definition somewhere, then HI may not
1397 point to an indirect symbol. We will have reported an error to
1398 the user in that case. */
1400 if (hi->root.type == bfd_link_hash_indirect)
1402 struct elf_link_hash_entry *ht;
1404 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1405 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1407 /* See if the new flags lead us to realize that the symbol must
1419 if (hi->ref_regular)
1425 /* We also need to define an indirection from the nondefault version
1429 len = strlen (name);
1430 shortname = bfd_hash_allocate (&info->hash->table, len);
1431 if (shortname == NULL)
1433 memcpy (shortname, name, shortlen);
1434 memcpy (shortname + shortlen, p + 1, len - shortlen);
1436 /* Once again, merge with any existing symbol. */
1437 type_change_ok = FALSE;
1438 size_change_ok = FALSE;
1440 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1441 &hi, &skip, &override, &type_change_ok,
1450 /* Here SHORTNAME is a versioned name, so we don't expect to see
1451 the type of override we do in the case above unless it is
1452 overridden by a versioned definition. */
1453 if (hi->root.type != bfd_link_hash_defined
1454 && hi->root.type != bfd_link_hash_defweak)
1455 (*_bfd_error_handler)
1456 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1462 if (! (_bfd_generic_link_add_one_symbol
1463 (info, abfd, shortname, BSF_INDIRECT,
1464 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1466 hi = (struct elf_link_hash_entry *) bh;
1468 /* If there is a duplicate definition somewhere, then HI may not
1469 point to an indirect symbol. We will have reported an error
1470 to the user in that case. */
1472 if (hi->root.type == bfd_link_hash_indirect)
1474 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1476 /* See if the new flags lead us to realize that the symbol
1488 if (hi->ref_regular)
1498 /* This routine is used to export all defined symbols into the dynamic
1499 symbol table. It is called via elf_link_hash_traverse. */
1502 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1504 struct elf_info_failed *eif = data;
1506 /* Ignore indirect symbols. These are added by the versioning code. */
1507 if (h->root.type == bfd_link_hash_indirect)
1510 if (h->root.type == bfd_link_hash_warning)
1511 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1513 if (h->dynindx == -1
1517 struct bfd_elf_version_tree *t;
1518 struct bfd_elf_version_expr *d;
1520 for (t = eif->verdefs; t != NULL; t = t->next)
1522 if (t->globals.list != NULL)
1524 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1529 if (t->locals.list != NULL)
1531 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1540 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1551 /* Look through the symbols which are defined in other shared
1552 libraries and referenced here. Update the list of version
1553 dependencies. This will be put into the .gnu.version_r section.
1554 This function is called via elf_link_hash_traverse. */
1557 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1560 struct elf_find_verdep_info *rinfo = data;
1561 Elf_Internal_Verneed *t;
1562 Elf_Internal_Vernaux *a;
1565 if (h->root.type == bfd_link_hash_warning)
1566 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1568 /* We only care about symbols defined in shared objects with version
1573 || h->verinfo.verdef == NULL)
1576 /* See if we already know about this version. */
1577 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1579 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1582 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1583 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1589 /* This is a new version. Add it to tree we are building. */
1594 t = bfd_zalloc (rinfo->output_bfd, amt);
1597 rinfo->failed = TRUE;
1601 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1602 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1603 elf_tdata (rinfo->output_bfd)->verref = t;
1607 a = bfd_zalloc (rinfo->output_bfd, amt);
1609 /* Note that we are copying a string pointer here, and testing it
1610 above. If bfd_elf_string_from_elf_section is ever changed to
1611 discard the string data when low in memory, this will have to be
1613 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1615 a->vna_flags = h->verinfo.verdef->vd_flags;
1616 a->vna_nextptr = t->vn_auxptr;
1618 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1621 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1628 /* Figure out appropriate versions for all the symbols. We may not
1629 have the version number script until we have read all of the input
1630 files, so until that point we don't know which symbols should be
1631 local. This function is called via elf_link_hash_traverse. */
1634 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1636 struct elf_assign_sym_version_info *sinfo;
1637 struct bfd_link_info *info;
1638 const struct elf_backend_data *bed;
1639 struct elf_info_failed eif;
1646 if (h->root.type == bfd_link_hash_warning)
1647 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1649 /* Fix the symbol flags. */
1652 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1655 sinfo->failed = TRUE;
1659 /* We only need version numbers for symbols defined in regular
1661 if (!h->def_regular)
1664 bed = get_elf_backend_data (sinfo->output_bfd);
1665 p = strchr (h->root.root.string, ELF_VER_CHR);
1666 if (p != NULL && h->verinfo.vertree == NULL)
1668 struct bfd_elf_version_tree *t;
1673 /* There are two consecutive ELF_VER_CHR characters if this is
1674 not a hidden symbol. */
1676 if (*p == ELF_VER_CHR)
1682 /* If there is no version string, we can just return out. */
1690 /* Look for the version. If we find it, it is no longer weak. */
1691 for (t = sinfo->verdefs; t != NULL; t = t->next)
1693 if (strcmp (t->name, p) == 0)
1697 struct bfd_elf_version_expr *d;
1699 len = p - h->root.root.string;
1700 alc = bfd_malloc (len);
1703 memcpy (alc, h->root.root.string, len - 1);
1704 alc[len - 1] = '\0';
1705 if (alc[len - 2] == ELF_VER_CHR)
1706 alc[len - 2] = '\0';
1708 h->verinfo.vertree = t;
1712 if (t->globals.list != NULL)
1713 d = (*t->match) (&t->globals, NULL, alc);
1715 /* See if there is anything to force this symbol to
1717 if (d == NULL && t->locals.list != NULL)
1719 d = (*t->match) (&t->locals, NULL, alc);
1723 && ! info->export_dynamic)
1724 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1732 /* If we are building an application, we need to create a
1733 version node for this version. */
1734 if (t == NULL && info->executable)
1736 struct bfd_elf_version_tree **pp;
1739 /* If we aren't going to export this symbol, we don't need
1740 to worry about it. */
1741 if (h->dynindx == -1)
1745 t = bfd_zalloc (sinfo->output_bfd, amt);
1748 sinfo->failed = TRUE;
1753 t->name_indx = (unsigned int) -1;
1757 /* Don't count anonymous version tag. */
1758 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1760 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1762 t->vernum = version_index;
1766 h->verinfo.vertree = t;
1770 /* We could not find the version for a symbol when
1771 generating a shared archive. Return an error. */
1772 (*_bfd_error_handler)
1773 (_("%B: undefined versioned symbol name %s"),
1774 sinfo->output_bfd, h->root.root.string);
1775 bfd_set_error (bfd_error_bad_value);
1776 sinfo->failed = TRUE;
1784 /* If we don't have a version for this symbol, see if we can find
1786 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1788 struct bfd_elf_version_tree *t;
1789 struct bfd_elf_version_tree *local_ver;
1790 struct bfd_elf_version_expr *d;
1792 /* See if can find what version this symbol is in. If the
1793 symbol is supposed to be local, then don't actually register
1796 for (t = sinfo->verdefs; t != NULL; t = t->next)
1798 if (t->globals.list != NULL)
1800 bfd_boolean matched;
1804 while ((d = (*t->match) (&t->globals, d,
1805 h->root.root.string)) != NULL)
1810 /* There is a version without definition. Make
1811 the symbol the default definition for this
1813 h->verinfo.vertree = t;
1821 /* There is no undefined version for this symbol. Hide the
1823 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1826 if (t->locals.list != NULL)
1829 while ((d = (*t->match) (&t->locals, d,
1830 h->root.root.string)) != NULL)
1833 /* If the match is "*", keep looking for a more
1834 explicit, perhaps even global, match.
1835 XXX: Shouldn't this be !d->wildcard instead? */
1836 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1845 if (local_ver != NULL)
1847 h->verinfo.vertree = local_ver;
1848 if (h->dynindx != -1
1850 && ! info->export_dynamic)
1852 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1860 /* Read and swap the relocs from the section indicated by SHDR. This
1861 may be either a REL or a RELA section. The relocations are
1862 translated into RELA relocations and stored in INTERNAL_RELOCS,
1863 which should have already been allocated to contain enough space.
1864 The EXTERNAL_RELOCS are a buffer where the external form of the
1865 relocations should be stored.
1867 Returns FALSE if something goes wrong. */
1870 elf_link_read_relocs_from_section (bfd *abfd,
1872 Elf_Internal_Shdr *shdr,
1873 void *external_relocs,
1874 Elf_Internal_Rela *internal_relocs)
1876 const struct elf_backend_data *bed;
1877 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1878 const bfd_byte *erela;
1879 const bfd_byte *erelaend;
1880 Elf_Internal_Rela *irela;
1881 Elf_Internal_Shdr *symtab_hdr;
1884 /* Position ourselves at the start of the section. */
1885 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1888 /* Read the relocations. */
1889 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1892 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1893 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1895 bed = get_elf_backend_data (abfd);
1897 /* Convert the external relocations to the internal format. */
1898 if (shdr->sh_entsize == bed->s->sizeof_rel)
1899 swap_in = bed->s->swap_reloc_in;
1900 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1901 swap_in = bed->s->swap_reloca_in;
1904 bfd_set_error (bfd_error_wrong_format);
1908 erela = external_relocs;
1909 erelaend = erela + shdr->sh_size;
1910 irela = internal_relocs;
1911 while (erela < erelaend)
1915 (*swap_in) (abfd, erela, irela);
1916 r_symndx = ELF32_R_SYM (irela->r_info);
1917 if (bed->s->arch_size == 64)
1919 if ((size_t) r_symndx >= nsyms)
1921 (*_bfd_error_handler)
1922 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1923 " for offset 0x%lx in section `%A'"),
1925 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1926 bfd_set_error (bfd_error_bad_value);
1929 irela += bed->s->int_rels_per_ext_rel;
1930 erela += shdr->sh_entsize;
1936 /* Read and swap the relocs for a section O. They may have been
1937 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1938 not NULL, they are used as buffers to read into. They are known to
1939 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1940 the return value is allocated using either malloc or bfd_alloc,
1941 according to the KEEP_MEMORY argument. If O has two relocation
1942 sections (both REL and RELA relocations), then the REL_HDR
1943 relocations will appear first in INTERNAL_RELOCS, followed by the
1944 REL_HDR2 relocations. */
1947 _bfd_elf_link_read_relocs (bfd *abfd,
1949 void *external_relocs,
1950 Elf_Internal_Rela *internal_relocs,
1951 bfd_boolean keep_memory)
1953 Elf_Internal_Shdr *rel_hdr;
1954 void *alloc1 = NULL;
1955 Elf_Internal_Rela *alloc2 = NULL;
1956 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1958 if (elf_section_data (o)->relocs != NULL)
1959 return elf_section_data (o)->relocs;
1961 if (o->reloc_count == 0)
1964 rel_hdr = &elf_section_data (o)->rel_hdr;
1966 if (internal_relocs == NULL)
1970 size = o->reloc_count;
1971 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1973 internal_relocs = bfd_alloc (abfd, size);
1975 internal_relocs = alloc2 = bfd_malloc (size);
1976 if (internal_relocs == NULL)
1980 if (external_relocs == NULL)
1982 bfd_size_type size = rel_hdr->sh_size;
1984 if (elf_section_data (o)->rel_hdr2)
1985 size += elf_section_data (o)->rel_hdr2->sh_size;
1986 alloc1 = bfd_malloc (size);
1989 external_relocs = alloc1;
1992 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
1996 if (elf_section_data (o)->rel_hdr2
1997 && (!elf_link_read_relocs_from_section
1999 elf_section_data (o)->rel_hdr2,
2000 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2001 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2002 * bed->s->int_rels_per_ext_rel))))
2005 /* Cache the results for next time, if we can. */
2007 elf_section_data (o)->relocs = internal_relocs;
2012 /* Don't free alloc2, since if it was allocated we are passing it
2013 back (under the name of internal_relocs). */
2015 return internal_relocs;
2025 /* Compute the size of, and allocate space for, REL_HDR which is the
2026 section header for a section containing relocations for O. */
2029 _bfd_elf_link_size_reloc_section (bfd *abfd,
2030 Elf_Internal_Shdr *rel_hdr,
2033 bfd_size_type reloc_count;
2034 bfd_size_type num_rel_hashes;
2036 /* Figure out how many relocations there will be. */
2037 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2038 reloc_count = elf_section_data (o)->rel_count;
2040 reloc_count = elf_section_data (o)->rel_count2;
2042 num_rel_hashes = o->reloc_count;
2043 if (num_rel_hashes < reloc_count)
2044 num_rel_hashes = reloc_count;
2046 /* That allows us to calculate the size of the section. */
2047 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2049 /* The contents field must last into write_object_contents, so we
2050 allocate it with bfd_alloc rather than malloc. Also since we
2051 cannot be sure that the contents will actually be filled in,
2052 we zero the allocated space. */
2053 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2054 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2057 /* We only allocate one set of hash entries, so we only do it the
2058 first time we are called. */
2059 if (elf_section_data (o)->rel_hashes == NULL
2062 struct elf_link_hash_entry **p;
2064 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2068 elf_section_data (o)->rel_hashes = p;
2074 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2075 originated from the section given by INPUT_REL_HDR) to the
2079 _bfd_elf_link_output_relocs (bfd *output_bfd,
2080 asection *input_section,
2081 Elf_Internal_Shdr *input_rel_hdr,
2082 Elf_Internal_Rela *internal_relocs)
2084 Elf_Internal_Rela *irela;
2085 Elf_Internal_Rela *irelaend;
2087 Elf_Internal_Shdr *output_rel_hdr;
2088 asection *output_section;
2089 unsigned int *rel_countp = NULL;
2090 const struct elf_backend_data *bed;
2091 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2093 output_section = input_section->output_section;
2094 output_rel_hdr = NULL;
2096 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2097 == input_rel_hdr->sh_entsize)
2099 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2100 rel_countp = &elf_section_data (output_section)->rel_count;
2102 else if (elf_section_data (output_section)->rel_hdr2
2103 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2104 == input_rel_hdr->sh_entsize))
2106 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2107 rel_countp = &elf_section_data (output_section)->rel_count2;
2111 (*_bfd_error_handler)
2112 (_("%B: relocation size mismatch in %B section %A"),
2113 output_bfd, input_section->owner, input_section);
2114 bfd_set_error (bfd_error_wrong_object_format);
2118 bed = get_elf_backend_data (output_bfd);
2119 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2120 swap_out = bed->s->swap_reloc_out;
2121 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2122 swap_out = bed->s->swap_reloca_out;
2126 erel = output_rel_hdr->contents;
2127 erel += *rel_countp * input_rel_hdr->sh_entsize;
2128 irela = internal_relocs;
2129 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2130 * bed->s->int_rels_per_ext_rel);
2131 while (irela < irelaend)
2133 (*swap_out) (output_bfd, irela, erel);
2134 irela += bed->s->int_rels_per_ext_rel;
2135 erel += input_rel_hdr->sh_entsize;
2138 /* Bump the counter, so that we know where to add the next set of
2140 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2145 /* Fix up the flags for a symbol. This handles various cases which
2146 can only be fixed after all the input files are seen. This is
2147 currently called by both adjust_dynamic_symbol and
2148 assign_sym_version, which is unnecessary but perhaps more robust in
2149 the face of future changes. */
2152 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2153 struct elf_info_failed *eif)
2155 /* If this symbol was mentioned in a non-ELF file, try to set
2156 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2157 permit a non-ELF file to correctly refer to a symbol defined in
2158 an ELF dynamic object. */
2161 while (h->root.type == bfd_link_hash_indirect)
2162 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2164 if (h->root.type != bfd_link_hash_defined
2165 && h->root.type != bfd_link_hash_defweak)
2168 h->ref_regular_nonweak = 1;
2172 if (h->root.u.def.section->owner != NULL
2173 && (bfd_get_flavour (h->root.u.def.section->owner)
2174 == bfd_target_elf_flavour))
2177 h->ref_regular_nonweak = 1;
2183 if (h->dynindx == -1
2187 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2196 /* Unfortunately, NON_ELF is only correct if the symbol
2197 was first seen in a non-ELF file. Fortunately, if the symbol
2198 was first seen in an ELF file, we're probably OK unless the
2199 symbol was defined in a non-ELF file. Catch that case here.
2200 FIXME: We're still in trouble if the symbol was first seen in
2201 a dynamic object, and then later in a non-ELF regular object. */
2202 if ((h->root.type == bfd_link_hash_defined
2203 || h->root.type == bfd_link_hash_defweak)
2205 && (h->root.u.def.section->owner != NULL
2206 ? (bfd_get_flavour (h->root.u.def.section->owner)
2207 != bfd_target_elf_flavour)
2208 : (bfd_is_abs_section (h->root.u.def.section)
2209 && !h->def_dynamic)))
2213 /* If this is a final link, and the symbol was defined as a common
2214 symbol in a regular object file, and there was no definition in
2215 any dynamic object, then the linker will have allocated space for
2216 the symbol in a common section but the DEF_REGULAR
2217 flag will not have been set. */
2218 if (h->root.type == bfd_link_hash_defined
2222 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2225 /* If -Bsymbolic was used (which means to bind references to global
2226 symbols to the definition within the shared object), and this
2227 symbol was defined in a regular object, then it actually doesn't
2228 need a PLT entry. Likewise, if the symbol has non-default
2229 visibility. If the symbol has hidden or internal visibility, we
2230 will force it local. */
2232 && eif->info->shared
2233 && is_elf_hash_table (eif->info->hash)
2234 && (eif->info->symbolic
2235 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2238 const struct elf_backend_data *bed;
2239 bfd_boolean force_local;
2241 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2243 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2244 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2245 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2248 /* If a weak undefined symbol has non-default visibility, we also
2249 hide it from the dynamic linker. */
2250 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2251 && h->root.type == bfd_link_hash_undefweak)
2253 const struct elf_backend_data *bed;
2254 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2255 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2258 /* If this is a weak defined symbol in a dynamic object, and we know
2259 the real definition in the dynamic object, copy interesting flags
2260 over to the real definition. */
2261 if (h->u.weakdef != NULL)
2263 struct elf_link_hash_entry *weakdef;
2265 weakdef = h->u.weakdef;
2266 if (h->root.type == bfd_link_hash_indirect)
2267 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2269 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2270 || h->root.type == bfd_link_hash_defweak);
2271 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2272 || weakdef->root.type == bfd_link_hash_defweak);
2273 BFD_ASSERT (weakdef->def_dynamic);
2275 /* If the real definition is defined by a regular object file,
2276 don't do anything special. See the longer description in
2277 _bfd_elf_adjust_dynamic_symbol, below. */
2278 if (weakdef->def_regular)
2279 h->u.weakdef = NULL;
2282 const struct elf_backend_data *bed;
2284 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2285 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2292 /* Make the backend pick a good value for a dynamic symbol. This is
2293 called via elf_link_hash_traverse, and also calls itself
2297 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2299 struct elf_info_failed *eif = data;
2301 const struct elf_backend_data *bed;
2303 if (! is_elf_hash_table (eif->info->hash))
2306 if (h->root.type == bfd_link_hash_warning)
2308 h->plt = elf_hash_table (eif->info)->init_offset;
2309 h->got = elf_hash_table (eif->info)->init_offset;
2311 /* When warning symbols are created, they **replace** the "real"
2312 entry in the hash table, thus we never get to see the real
2313 symbol in a hash traversal. So look at it now. */
2314 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2317 /* Ignore indirect symbols. These are added by the versioning code. */
2318 if (h->root.type == bfd_link_hash_indirect)
2321 /* Fix the symbol flags. */
2322 if (! _bfd_elf_fix_symbol_flags (h, eif))
2325 /* If this symbol does not require a PLT entry, and it is not
2326 defined by a dynamic object, or is not referenced by a regular
2327 object, ignore it. We do have to handle a weak defined symbol,
2328 even if no regular object refers to it, if we decided to add it
2329 to the dynamic symbol table. FIXME: Do we normally need to worry
2330 about symbols which are defined by one dynamic object and
2331 referenced by another one? */
2336 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2338 h->plt = elf_hash_table (eif->info)->init_offset;
2342 /* If we've already adjusted this symbol, don't do it again. This
2343 can happen via a recursive call. */
2344 if (h->dynamic_adjusted)
2347 /* Don't look at this symbol again. Note that we must set this
2348 after checking the above conditions, because we may look at a
2349 symbol once, decide not to do anything, and then get called
2350 recursively later after REF_REGULAR is set below. */
2351 h->dynamic_adjusted = 1;
2353 /* If this is a weak definition, and we know a real definition, and
2354 the real symbol is not itself defined by a regular object file,
2355 then get a good value for the real definition. We handle the
2356 real symbol first, for the convenience of the backend routine.
2358 Note that there is a confusing case here. If the real definition
2359 is defined by a regular object file, we don't get the real symbol
2360 from the dynamic object, but we do get the weak symbol. If the
2361 processor backend uses a COPY reloc, then if some routine in the
2362 dynamic object changes the real symbol, we will not see that
2363 change in the corresponding weak symbol. This is the way other
2364 ELF linkers work as well, and seems to be a result of the shared
2367 I will clarify this issue. Most SVR4 shared libraries define the
2368 variable _timezone and define timezone as a weak synonym. The
2369 tzset call changes _timezone. If you write
2370 extern int timezone;
2372 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2373 you might expect that, since timezone is a synonym for _timezone,
2374 the same number will print both times. However, if the processor
2375 backend uses a COPY reloc, then actually timezone will be copied
2376 into your process image, and, since you define _timezone
2377 yourself, _timezone will not. Thus timezone and _timezone will
2378 wind up at different memory locations. The tzset call will set
2379 _timezone, leaving timezone unchanged. */
2381 if (h->u.weakdef != NULL)
2383 /* If we get to this point, we know there is an implicit
2384 reference by a regular object file via the weak symbol H.
2385 FIXME: Is this really true? What if the traversal finds
2386 H->U.WEAKDEF before it finds H? */
2387 h->u.weakdef->ref_regular = 1;
2389 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2393 /* If a symbol has no type and no size and does not require a PLT
2394 entry, then we are probably about to do the wrong thing here: we
2395 are probably going to create a COPY reloc for an empty object.
2396 This case can arise when a shared object is built with assembly
2397 code, and the assembly code fails to set the symbol type. */
2399 && h->type == STT_NOTYPE
2401 (*_bfd_error_handler)
2402 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2403 h->root.root.string);
2405 dynobj = elf_hash_table (eif->info)->dynobj;
2406 bed = get_elf_backend_data (dynobj);
2407 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2416 /* Adjust all external symbols pointing into SEC_MERGE sections
2417 to reflect the object merging within the sections. */
2420 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2424 if (h->root.type == bfd_link_hash_warning)
2425 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2427 if ((h->root.type == bfd_link_hash_defined
2428 || h->root.type == bfd_link_hash_defweak)
2429 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2430 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2432 bfd *output_bfd = data;
2434 h->root.u.def.value =
2435 _bfd_merged_section_offset (output_bfd,
2436 &h->root.u.def.section,
2437 elf_section_data (sec)->sec_info,
2438 h->root.u.def.value);
2444 /* Returns false if the symbol referred to by H should be considered
2445 to resolve local to the current module, and true if it should be
2446 considered to bind dynamically. */
2449 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2450 struct bfd_link_info *info,
2451 bfd_boolean ignore_protected)
2453 bfd_boolean binding_stays_local_p;
2458 while (h->root.type == bfd_link_hash_indirect
2459 || h->root.type == bfd_link_hash_warning)
2460 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2462 /* If it was forced local, then clearly it's not dynamic. */
2463 if (h->dynindx == -1)
2465 if (h->forced_local)
2468 /* Identify the cases where name binding rules say that a
2469 visible symbol resolves locally. */
2470 binding_stays_local_p = info->executable || info->symbolic;
2472 switch (ELF_ST_VISIBILITY (h->other))
2479 /* Proper resolution for function pointer equality may require
2480 that these symbols perhaps be resolved dynamically, even though
2481 we should be resolving them to the current module. */
2482 if (!ignore_protected || h->type != STT_FUNC)
2483 binding_stays_local_p = TRUE;
2490 /* If it isn't defined locally, then clearly it's dynamic. */
2491 if (!h->def_regular)
2494 /* Otherwise, the symbol is dynamic if binding rules don't tell
2495 us that it remains local. */
2496 return !binding_stays_local_p;
2499 /* Return true if the symbol referred to by H should be considered
2500 to resolve local to the current module, and false otherwise. Differs
2501 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2502 undefined symbols and weak symbols. */
2505 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2506 struct bfd_link_info *info,
2507 bfd_boolean local_protected)
2509 /* If it's a local sym, of course we resolve locally. */
2513 /* Common symbols that become definitions don't get the DEF_REGULAR
2514 flag set, so test it first, and don't bail out. */
2515 if (ELF_COMMON_DEF_P (h))
2517 /* If we don't have a definition in a regular file, then we can't
2518 resolve locally. The sym is either undefined or dynamic. */
2519 else if (!h->def_regular)
2522 /* Forced local symbols resolve locally. */
2523 if (h->forced_local)
2526 /* As do non-dynamic symbols. */
2527 if (h->dynindx == -1)
2530 /* At this point, we know the symbol is defined and dynamic. In an
2531 executable it must resolve locally, likewise when building symbolic
2532 shared libraries. */
2533 if (info->executable || info->symbolic)
2536 /* Now deal with defined dynamic symbols in shared libraries. Ones
2537 with default visibility might not resolve locally. */
2538 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2541 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2542 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2545 /* STV_PROTECTED non-function symbols are local. */
2546 if (h->type != STT_FUNC)
2549 /* Function pointer equality tests may require that STV_PROTECTED
2550 symbols be treated as dynamic symbols, even when we know that the
2551 dynamic linker will resolve them locally. */
2552 return local_protected;
2555 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2556 aligned. Returns the first TLS output section. */
2558 struct bfd_section *
2559 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2561 struct bfd_section *sec, *tls;
2562 unsigned int align = 0;
2564 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2565 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2569 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2570 if (sec->alignment_power > align)
2571 align = sec->alignment_power;
2573 elf_hash_table (info)->tls_sec = tls;
2575 /* Ensure the alignment of the first section is the largest alignment,
2576 so that the tls segment starts aligned. */
2578 tls->alignment_power = align;
2583 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2585 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2586 Elf_Internal_Sym *sym)
2588 /* Local symbols do not count, but target specific ones might. */
2589 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2590 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2593 /* Function symbols do not count. */
2594 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2597 /* If the section is undefined, then so is the symbol. */
2598 if (sym->st_shndx == SHN_UNDEF)
2601 /* If the symbol is defined in the common section, then
2602 it is a common definition and so does not count. */
2603 if (sym->st_shndx == SHN_COMMON)
2606 /* If the symbol is in a target specific section then we
2607 must rely upon the backend to tell us what it is. */
2608 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2609 /* FIXME - this function is not coded yet:
2611 return _bfd_is_global_symbol_definition (abfd, sym);
2613 Instead for now assume that the definition is not global,
2614 Even if this is wrong, at least the linker will behave
2615 in the same way that it used to do. */
2621 /* Search the symbol table of the archive element of the archive ABFD
2622 whose archive map contains a mention of SYMDEF, and determine if
2623 the symbol is defined in this element. */
2625 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2627 Elf_Internal_Shdr * hdr;
2628 bfd_size_type symcount;
2629 bfd_size_type extsymcount;
2630 bfd_size_type extsymoff;
2631 Elf_Internal_Sym *isymbuf;
2632 Elf_Internal_Sym *isym;
2633 Elf_Internal_Sym *isymend;
2636 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2640 if (! bfd_check_format (abfd, bfd_object))
2643 /* If we have already included the element containing this symbol in the
2644 link then we do not need to include it again. Just claim that any symbol
2645 it contains is not a definition, so that our caller will not decide to
2646 (re)include this element. */
2647 if (abfd->archive_pass)
2650 /* Select the appropriate symbol table. */
2651 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2652 hdr = &elf_tdata (abfd)->symtab_hdr;
2654 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2656 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2658 /* The sh_info field of the symtab header tells us where the
2659 external symbols start. We don't care about the local symbols. */
2660 if (elf_bad_symtab (abfd))
2662 extsymcount = symcount;
2667 extsymcount = symcount - hdr->sh_info;
2668 extsymoff = hdr->sh_info;
2671 if (extsymcount == 0)
2674 /* Read in the symbol table. */
2675 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2677 if (isymbuf == NULL)
2680 /* Scan the symbol table looking for SYMDEF. */
2682 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2686 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2691 if (strcmp (name, symdef->name) == 0)
2693 result = is_global_data_symbol_definition (abfd, isym);
2703 /* Add an entry to the .dynamic table. */
2706 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2710 struct elf_link_hash_table *hash_table;
2711 const struct elf_backend_data *bed;
2713 bfd_size_type newsize;
2714 bfd_byte *newcontents;
2715 Elf_Internal_Dyn dyn;
2717 hash_table = elf_hash_table (info);
2718 if (! is_elf_hash_table (hash_table))
2721 if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
2723 (_("warning: creating a DT_TEXTREL in a shared object."));
2725 bed = get_elf_backend_data (hash_table->dynobj);
2726 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2727 BFD_ASSERT (s != NULL);
2729 newsize = s->size + bed->s->sizeof_dyn;
2730 newcontents = bfd_realloc (s->contents, newsize);
2731 if (newcontents == NULL)
2735 dyn.d_un.d_val = val;
2736 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2739 s->contents = newcontents;
2744 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2745 otherwise just check whether one already exists. Returns -1 on error,
2746 1 if a DT_NEEDED tag already exists, and 0 on success. */
2749 elf_add_dt_needed_tag (struct bfd_link_info *info,
2753 struct elf_link_hash_table *hash_table;
2754 bfd_size_type oldsize;
2755 bfd_size_type strindex;
2757 hash_table = elf_hash_table (info);
2758 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2759 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2760 if (strindex == (bfd_size_type) -1)
2763 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2766 const struct elf_backend_data *bed;
2769 bed = get_elf_backend_data (hash_table->dynobj);
2770 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2771 BFD_ASSERT (sdyn != NULL);
2773 for (extdyn = sdyn->contents;
2774 extdyn < sdyn->contents + sdyn->size;
2775 extdyn += bed->s->sizeof_dyn)
2777 Elf_Internal_Dyn dyn;
2779 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2780 if (dyn.d_tag == DT_NEEDED
2781 && dyn.d_un.d_val == strindex)
2783 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2791 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2795 /* We were just checking for existence of the tag. */
2796 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2801 /* Sort symbol by value and section. */
2803 elf_sort_symbol (const void *arg1, const void *arg2)
2805 const struct elf_link_hash_entry *h1;
2806 const struct elf_link_hash_entry *h2;
2807 bfd_signed_vma vdiff;
2809 h1 = *(const struct elf_link_hash_entry **) arg1;
2810 h2 = *(const struct elf_link_hash_entry **) arg2;
2811 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2813 return vdiff > 0 ? 1 : -1;
2816 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2818 return sdiff > 0 ? 1 : -1;
2823 /* This function is used to adjust offsets into .dynstr for
2824 dynamic symbols. This is called via elf_link_hash_traverse. */
2827 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2829 struct elf_strtab_hash *dynstr = data;
2831 if (h->root.type == bfd_link_hash_warning)
2832 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2834 if (h->dynindx != -1)
2835 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2839 /* Assign string offsets in .dynstr, update all structures referencing
2843 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2845 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2846 struct elf_link_local_dynamic_entry *entry;
2847 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2848 bfd *dynobj = hash_table->dynobj;
2851 const struct elf_backend_data *bed;
2854 _bfd_elf_strtab_finalize (dynstr);
2855 size = _bfd_elf_strtab_size (dynstr);
2857 bed = get_elf_backend_data (dynobj);
2858 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2859 BFD_ASSERT (sdyn != NULL);
2861 /* Update all .dynamic entries referencing .dynstr strings. */
2862 for (extdyn = sdyn->contents;
2863 extdyn < sdyn->contents + sdyn->size;
2864 extdyn += bed->s->sizeof_dyn)
2866 Elf_Internal_Dyn dyn;
2868 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
2872 dyn.d_un.d_val = size;
2880 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2885 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
2888 /* Now update local dynamic symbols. */
2889 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
2890 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2891 entry->isym.st_name);
2893 /* And the rest of dynamic symbols. */
2894 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
2896 /* Adjust version definitions. */
2897 if (elf_tdata (output_bfd)->cverdefs)
2902 Elf_Internal_Verdef def;
2903 Elf_Internal_Verdaux defaux;
2905 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2909 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2911 p += sizeof (Elf_External_Verdef);
2912 if (def.vd_aux != sizeof (Elf_External_Verdef))
2914 for (i = 0; i < def.vd_cnt; ++i)
2916 _bfd_elf_swap_verdaux_in (output_bfd,
2917 (Elf_External_Verdaux *) p, &defaux);
2918 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2920 _bfd_elf_swap_verdaux_out (output_bfd,
2921 &defaux, (Elf_External_Verdaux *) p);
2922 p += sizeof (Elf_External_Verdaux);
2925 while (def.vd_next);
2928 /* Adjust version references. */
2929 if (elf_tdata (output_bfd)->verref)
2934 Elf_Internal_Verneed need;
2935 Elf_Internal_Vernaux needaux;
2937 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2941 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2943 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2944 _bfd_elf_swap_verneed_out (output_bfd, &need,
2945 (Elf_External_Verneed *) p);
2946 p += sizeof (Elf_External_Verneed);
2947 for (i = 0; i < need.vn_cnt; ++i)
2949 _bfd_elf_swap_vernaux_in (output_bfd,
2950 (Elf_External_Vernaux *) p, &needaux);
2951 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2953 _bfd_elf_swap_vernaux_out (output_bfd,
2955 (Elf_External_Vernaux *) p);
2956 p += sizeof (Elf_External_Vernaux);
2959 while (need.vn_next);
2965 /* Add symbols from an ELF object file to the linker hash table. */
2968 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2970 bfd_boolean (*add_symbol_hook)
2971 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
2972 const char **, flagword *, asection **, bfd_vma *);
2973 bfd_boolean (*check_relocs)
2974 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
2975 bfd_boolean (*check_directives)
2976 (bfd *, struct bfd_link_info *);
2977 bfd_boolean collect;
2978 Elf_Internal_Shdr *hdr;
2979 bfd_size_type symcount;
2980 bfd_size_type extsymcount;
2981 bfd_size_type extsymoff;
2982 struct elf_link_hash_entry **sym_hash;
2983 bfd_boolean dynamic;
2984 Elf_External_Versym *extversym = NULL;
2985 Elf_External_Versym *ever;
2986 struct elf_link_hash_entry *weaks;
2987 struct elf_link_hash_entry **nondeflt_vers = NULL;
2988 bfd_size_type nondeflt_vers_cnt = 0;
2989 Elf_Internal_Sym *isymbuf = NULL;
2990 Elf_Internal_Sym *isym;
2991 Elf_Internal_Sym *isymend;
2992 const struct elf_backend_data *bed;
2993 bfd_boolean add_needed;
2994 struct elf_link_hash_table * hash_table;
2997 hash_table = elf_hash_table (info);
2999 bed = get_elf_backend_data (abfd);
3000 add_symbol_hook = bed->elf_add_symbol_hook;
3001 collect = bed->collect;
3003 if ((abfd->flags & DYNAMIC) == 0)
3009 /* You can't use -r against a dynamic object. Also, there's no
3010 hope of using a dynamic object which does not exactly match
3011 the format of the output file. */
3012 if (info->relocatable
3013 || !is_elf_hash_table (hash_table)
3014 || hash_table->root.creator != abfd->xvec)
3016 if (info->relocatable)
3017 bfd_set_error (bfd_error_invalid_operation);
3019 bfd_set_error (bfd_error_wrong_format);
3024 /* As a GNU extension, any input sections which are named
3025 .gnu.warning.SYMBOL are treated as warning symbols for the given
3026 symbol. This differs from .gnu.warning sections, which generate
3027 warnings when they are included in an output file. */
3028 if (info->executable)
3032 for (s = abfd->sections; s != NULL; s = s->next)
3036 name = bfd_get_section_name (abfd, s);
3037 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3041 bfd_size_type prefix_len;
3042 const char * gnu_warning_prefix = _("warning: ");
3044 name += sizeof ".gnu.warning." - 1;
3046 /* If this is a shared object, then look up the symbol
3047 in the hash table. If it is there, and it is already
3048 been defined, then we will not be using the entry
3049 from this shared object, so we don't need to warn.
3050 FIXME: If we see the definition in a regular object
3051 later on, we will warn, but we shouldn't. The only
3052 fix is to keep track of what warnings we are supposed
3053 to emit, and then handle them all at the end of the
3057 struct elf_link_hash_entry *h;
3059 h = elf_link_hash_lookup (hash_table, name,
3060 FALSE, FALSE, TRUE);
3062 /* FIXME: What about bfd_link_hash_common? */
3064 && (h->root.type == bfd_link_hash_defined
3065 || h->root.type == bfd_link_hash_defweak))
3067 /* We don't want to issue this warning. Clobber
3068 the section size so that the warning does not
3069 get copied into the output file. */
3076 prefix_len = strlen (gnu_warning_prefix);
3077 msg = bfd_alloc (abfd, prefix_len + sz + 1);
3081 strcpy (msg, gnu_warning_prefix);
3082 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
3085 msg[prefix_len + sz] = '\0';
3087 if (! (_bfd_generic_link_add_one_symbol
3088 (info, abfd, name, BSF_WARNING, s, 0, msg,
3089 FALSE, collect, NULL)))
3092 if (! info->relocatable)
3094 /* Clobber the section size so that the warning does
3095 not get copied into the output file. */
3105 /* If we are creating a shared library, create all the dynamic
3106 sections immediately. We need to attach them to something,
3107 so we attach them to this BFD, provided it is the right
3108 format. FIXME: If there are no input BFD's of the same
3109 format as the output, we can't make a shared library. */
3111 && is_elf_hash_table (hash_table)
3112 && hash_table->root.creator == abfd->xvec
3113 && ! hash_table->dynamic_sections_created)
3115 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3119 else if (!is_elf_hash_table (hash_table))
3124 const char *soname = NULL;
3125 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3128 /* ld --just-symbols and dynamic objects don't mix very well.
3129 Test for --just-symbols by looking at info set up by
3130 _bfd_elf_link_just_syms. */
3131 if ((s = abfd->sections) != NULL
3132 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3135 /* If this dynamic lib was specified on the command line with
3136 --as-needed in effect, then we don't want to add a DT_NEEDED
3137 tag unless the lib is actually used. Similary for libs brought
3138 in by another lib's DT_NEEDED. When --no-add-needed is used
3139 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3140 any dynamic library in DT_NEEDED tags in the dynamic lib at
3142 add_needed = (elf_dyn_lib_class (abfd)
3143 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3144 | DYN_NO_NEEDED)) == 0;
3146 s = bfd_get_section_by_name (abfd, ".dynamic");
3152 unsigned long shlink;
3154 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3155 goto error_free_dyn;
3157 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3159 goto error_free_dyn;
3160 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3162 for (extdyn = dynbuf;
3163 extdyn < dynbuf + s->size;
3164 extdyn += bed->s->sizeof_dyn)
3166 Elf_Internal_Dyn dyn;
3168 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3169 if (dyn.d_tag == DT_SONAME)
3171 unsigned int tagv = dyn.d_un.d_val;
3172 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3174 goto error_free_dyn;
3176 if (dyn.d_tag == DT_NEEDED)
3178 struct bfd_link_needed_list *n, **pn;
3180 unsigned int tagv = dyn.d_un.d_val;
3182 amt = sizeof (struct bfd_link_needed_list);
3183 n = bfd_alloc (abfd, amt);
3184 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3185 if (n == NULL || fnm == NULL)
3186 goto error_free_dyn;
3187 amt = strlen (fnm) + 1;
3188 anm = bfd_alloc (abfd, amt);
3190 goto error_free_dyn;
3191 memcpy (anm, fnm, amt);
3195 for (pn = & hash_table->needed;
3201 if (dyn.d_tag == DT_RUNPATH)
3203 struct bfd_link_needed_list *n, **pn;
3205 unsigned int tagv = dyn.d_un.d_val;
3207 amt = sizeof (struct bfd_link_needed_list);
3208 n = bfd_alloc (abfd, amt);
3209 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3210 if (n == NULL || fnm == NULL)
3211 goto error_free_dyn;
3212 amt = strlen (fnm) + 1;
3213 anm = bfd_alloc (abfd, amt);
3215 goto error_free_dyn;
3216 memcpy (anm, fnm, amt);
3220 for (pn = & runpath;
3226 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3227 if (!runpath && dyn.d_tag == DT_RPATH)
3229 struct bfd_link_needed_list *n, **pn;
3231 unsigned int tagv = dyn.d_un.d_val;
3233 amt = sizeof (struct bfd_link_needed_list);
3234 n = bfd_alloc (abfd, amt);
3235 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3236 if (n == NULL || fnm == NULL)
3237 goto error_free_dyn;
3238 amt = strlen (fnm) + 1;
3239 anm = bfd_alloc (abfd, amt);
3246 memcpy (anm, fnm, amt);
3261 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3262 frees all more recently bfd_alloc'd blocks as well. */
3268 struct bfd_link_needed_list **pn;
3269 for (pn = & hash_table->runpath;
3276 /* We do not want to include any of the sections in a dynamic
3277 object in the output file. We hack by simply clobbering the
3278 list of sections in the BFD. This could be handled more
3279 cleanly by, say, a new section flag; the existing
3280 SEC_NEVER_LOAD flag is not the one we want, because that one
3281 still implies that the section takes up space in the output
3283 bfd_section_list_clear (abfd);
3285 /* If this is the first dynamic object found in the link, create
3286 the special sections required for dynamic linking. */
3287 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3290 /* Find the name to use in a DT_NEEDED entry that refers to this
3291 object. If the object has a DT_SONAME entry, we use it.
3292 Otherwise, if the generic linker stuck something in
3293 elf_dt_name, we use that. Otherwise, we just use the file
3295 if (soname == NULL || *soname == '\0')
3297 soname = elf_dt_name (abfd);
3298 if (soname == NULL || *soname == '\0')
3299 soname = bfd_get_filename (abfd);
3302 /* Save the SONAME because sometimes the linker emulation code
3303 will need to know it. */
3304 elf_dt_name (abfd) = soname;
3306 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3310 /* If we have already included this dynamic object in the
3311 link, just ignore it. There is no reason to include a
3312 particular dynamic object more than once. */
3317 /* If this is a dynamic object, we always link against the .dynsym
3318 symbol table, not the .symtab symbol table. The dynamic linker
3319 will only see the .dynsym symbol table, so there is no reason to
3320 look at .symtab for a dynamic object. */
3322 if (! dynamic || elf_dynsymtab (abfd) == 0)
3323 hdr = &elf_tdata (abfd)->symtab_hdr;
3325 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3327 symcount = hdr->sh_size / bed->s->sizeof_sym;
3329 /* The sh_info field of the symtab header tells us where the
3330 external symbols start. We don't care about the local symbols at
3332 if (elf_bad_symtab (abfd))
3334 extsymcount = symcount;
3339 extsymcount = symcount - hdr->sh_info;
3340 extsymoff = hdr->sh_info;
3344 if (extsymcount != 0)
3346 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3348 if (isymbuf == NULL)
3351 /* We store a pointer to the hash table entry for each external
3353 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3354 sym_hash = bfd_alloc (abfd, amt);
3355 if (sym_hash == NULL)
3356 goto error_free_sym;
3357 elf_sym_hashes (abfd) = sym_hash;
3362 /* Read in any version definitions. */
3363 if (!_bfd_elf_slurp_version_tables (abfd,
3364 info->default_imported_symver))
3365 goto error_free_sym;
3367 /* Read in the symbol versions, but don't bother to convert them
3368 to internal format. */
3369 if (elf_dynversym (abfd) != 0)
3371 Elf_Internal_Shdr *versymhdr;
3373 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3374 extversym = bfd_malloc (versymhdr->sh_size);
3375 if (extversym == NULL)
3376 goto error_free_sym;
3377 amt = versymhdr->sh_size;
3378 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3379 || bfd_bread (extversym, amt, abfd) != amt)
3380 goto error_free_vers;
3386 ever = extversym != NULL ? extversym + extsymoff : NULL;
3387 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3389 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3396 struct elf_link_hash_entry *h;
3397 bfd_boolean definition;
3398 bfd_boolean size_change_ok;
3399 bfd_boolean type_change_ok;
3400 bfd_boolean new_weakdef;
3401 bfd_boolean override;
3402 unsigned int old_alignment;
3407 flags = BSF_NO_FLAGS;
3409 value = isym->st_value;
3412 bind = ELF_ST_BIND (isym->st_info);
3413 if (bind == STB_LOCAL)
3415 /* This should be impossible, since ELF requires that all
3416 global symbols follow all local symbols, and that sh_info
3417 point to the first global symbol. Unfortunately, Irix 5
3421 else if (bind == STB_GLOBAL)
3423 if (isym->st_shndx != SHN_UNDEF
3424 && isym->st_shndx != SHN_COMMON)
3427 else if (bind == STB_WEAK)
3431 /* Leave it up to the processor backend. */
3434 if (isym->st_shndx == SHN_UNDEF)
3435 sec = bfd_und_section_ptr;
3436 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3438 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3440 sec = bfd_abs_section_ptr;
3441 else if (sec->kept_section)
3443 /* Symbols from discarded section are undefined. */
3444 sec = bfd_und_section_ptr;
3445 isym->st_shndx = SHN_UNDEF;
3447 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3450 else if (isym->st_shndx == SHN_ABS)
3451 sec = bfd_abs_section_ptr;
3452 else if (isym->st_shndx == SHN_COMMON)
3454 sec = bfd_com_section_ptr;
3455 /* What ELF calls the size we call the value. What ELF
3456 calls the value we call the alignment. */
3457 value = isym->st_size;
3461 /* Leave it up to the processor backend. */
3464 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3467 goto error_free_vers;
3469 if (isym->st_shndx == SHN_COMMON
3470 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3472 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3476 tcomm = bfd_make_section (abfd, ".tcommon");
3478 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
3480 | SEC_LINKER_CREATED
3481 | SEC_THREAD_LOCAL)))
3482 goto error_free_vers;
3486 else if (add_symbol_hook)
3488 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3490 goto error_free_vers;
3492 /* The hook function sets the name to NULL if this symbol
3493 should be skipped for some reason. */
3498 /* Sanity check that all possibilities were handled. */
3501 bfd_set_error (bfd_error_bad_value);
3502 goto error_free_vers;
3505 if (bfd_is_und_section (sec)
3506 || bfd_is_com_section (sec))
3511 size_change_ok = FALSE;
3512 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3516 if (is_elf_hash_table (hash_table))
3518 Elf_Internal_Versym iver;
3519 unsigned int vernum = 0;
3524 if (info->default_imported_symver)
3525 /* Use the default symbol version created earlier. */
3526 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3531 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3533 vernum = iver.vs_vers & VERSYM_VERSION;
3535 /* If this is a hidden symbol, or if it is not version
3536 1, we append the version name to the symbol name.
3537 However, we do not modify a non-hidden absolute
3538 symbol, because it might be the version symbol
3539 itself. FIXME: What if it isn't? */
3540 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3541 || (vernum > 1 && ! bfd_is_abs_section (sec)))
3544 size_t namelen, verlen, newlen;
3547 if (isym->st_shndx != SHN_UNDEF)
3549 if (vernum > elf_tdata (abfd)->cverdefs)
3551 else if (vernum > 1)
3553 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3559 (*_bfd_error_handler)
3560 (_("%B: %s: invalid version %u (max %d)"),
3562 elf_tdata (abfd)->cverdefs);
3563 bfd_set_error (bfd_error_bad_value);
3564 goto error_free_vers;
3569 /* We cannot simply test for the number of
3570 entries in the VERNEED section since the
3571 numbers for the needed versions do not start
3573 Elf_Internal_Verneed *t;
3576 for (t = elf_tdata (abfd)->verref;
3580 Elf_Internal_Vernaux *a;
3582 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3584 if (a->vna_other == vernum)
3586 verstr = a->vna_nodename;
3595 (*_bfd_error_handler)
3596 (_("%B: %s: invalid needed version %d"),
3597 abfd, name, vernum);
3598 bfd_set_error (bfd_error_bad_value);
3599 goto error_free_vers;
3603 namelen = strlen (name);
3604 verlen = strlen (verstr);
3605 newlen = namelen + verlen + 2;
3606 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3607 && isym->st_shndx != SHN_UNDEF)
3610 newname = bfd_alloc (abfd, newlen);
3611 if (newname == NULL)
3612 goto error_free_vers;
3613 memcpy (newname, name, namelen);
3614 p = newname + namelen;
3616 /* If this is a defined non-hidden version symbol,
3617 we add another @ to the name. This indicates the
3618 default version of the symbol. */
3619 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3620 && isym->st_shndx != SHN_UNDEF)
3622 memcpy (p, verstr, verlen + 1);
3627 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
3628 sym_hash, &skip, &override,
3629 &type_change_ok, &size_change_ok))
3630 goto error_free_vers;
3639 while (h->root.type == bfd_link_hash_indirect
3640 || h->root.type == bfd_link_hash_warning)
3641 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3643 /* Remember the old alignment if this is a common symbol, so
3644 that we don't reduce the alignment later on. We can't
3645 check later, because _bfd_generic_link_add_one_symbol
3646 will set a default for the alignment which we want to
3647 override. We also remember the old bfd where the existing
3648 definition comes from. */
3649 switch (h->root.type)
3654 case bfd_link_hash_defined:
3655 case bfd_link_hash_defweak:
3656 old_bfd = h->root.u.def.section->owner;
3659 case bfd_link_hash_common:
3660 old_bfd = h->root.u.c.p->section->owner;
3661 old_alignment = h->root.u.c.p->alignment_power;
3665 if (elf_tdata (abfd)->verdef != NULL
3669 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3672 if (! (_bfd_generic_link_add_one_symbol
3673 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3674 (struct bfd_link_hash_entry **) sym_hash)))
3675 goto error_free_vers;
3678 while (h->root.type == bfd_link_hash_indirect
3679 || h->root.type == bfd_link_hash_warning)
3680 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3683 new_weakdef = FALSE;
3686 && (flags & BSF_WEAK) != 0
3687 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3688 && is_elf_hash_table (hash_table)
3689 && h->u.weakdef == NULL)
3691 /* Keep a list of all weak defined non function symbols from
3692 a dynamic object, using the weakdef field. Later in this
3693 function we will set the weakdef field to the correct
3694 value. We only put non-function symbols from dynamic
3695 objects on this list, because that happens to be the only
3696 time we need to know the normal symbol corresponding to a
3697 weak symbol, and the information is time consuming to
3698 figure out. If the weakdef field is not already NULL,
3699 then this symbol was already defined by some previous
3700 dynamic object, and we will be using that previous
3701 definition anyhow. */
3703 h->u.weakdef = weaks;
3708 /* Set the alignment of a common symbol. */
3709 if (isym->st_shndx == SHN_COMMON
3710 && h->root.type == bfd_link_hash_common)
3714 align = bfd_log2 (isym->st_value);
3715 if (align > old_alignment
3716 /* Permit an alignment power of zero if an alignment of one
3717 is specified and no other alignments have been specified. */
3718 || (isym->st_value == 1 && old_alignment == 0))
3719 h->root.u.c.p->alignment_power = align;
3721 h->root.u.c.p->alignment_power = old_alignment;
3724 if (is_elf_hash_table (hash_table))
3728 /* Check the alignment when a common symbol is involved. This
3729 can change when a common symbol is overridden by a normal
3730 definition or a common symbol is ignored due to the old
3731 normal definition. We need to make sure the maximum
3732 alignment is maintained. */
3733 if ((old_alignment || isym->st_shndx == SHN_COMMON)
3734 && h->root.type != bfd_link_hash_common)
3736 unsigned int common_align;
3737 unsigned int normal_align;
3738 unsigned int symbol_align;
3742 symbol_align = ffs (h->root.u.def.value) - 1;
3743 if (h->root.u.def.section->owner != NULL
3744 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3746 normal_align = h->root.u.def.section->alignment_power;
3747 if (normal_align > symbol_align)
3748 normal_align = symbol_align;
3751 normal_align = symbol_align;
3755 common_align = old_alignment;
3756 common_bfd = old_bfd;
3761 common_align = bfd_log2 (isym->st_value);
3763 normal_bfd = old_bfd;
3766 if (normal_align < common_align)
3767 (*_bfd_error_handler)
3768 (_("Warning: alignment %u of symbol `%s' in %B"
3769 " is smaller than %u in %B"),
3770 normal_bfd, common_bfd,
3771 1 << normal_align, name, 1 << common_align);
3774 /* Remember the symbol size and type. */
3775 if (isym->st_size != 0
3776 && (definition || h->size == 0))
3778 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3779 (*_bfd_error_handler)
3780 (_("Warning: size of symbol `%s' changed"
3781 " from %lu in %B to %lu in %B"),
3783 name, (unsigned long) h->size,
3784 (unsigned long) isym->st_size);
3786 h->size = isym->st_size;
3789 /* If this is a common symbol, then we always want H->SIZE
3790 to be the size of the common symbol. The code just above
3791 won't fix the size if a common symbol becomes larger. We
3792 don't warn about a size change here, because that is
3793 covered by --warn-common. */
3794 if (h->root.type == bfd_link_hash_common)
3795 h->size = h->root.u.c.size;
3797 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3798 && (definition || h->type == STT_NOTYPE))
3800 if (h->type != STT_NOTYPE
3801 && h->type != ELF_ST_TYPE (isym->st_info)
3802 && ! type_change_ok)
3803 (*_bfd_error_handler)
3804 (_("Warning: type of symbol `%s' changed"
3805 " from %d to %d in %B"),
3806 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3808 h->type = ELF_ST_TYPE (isym->st_info);
3811 /* If st_other has a processor-specific meaning, specific
3812 code might be needed here. We never merge the visibility
3813 attribute with the one from a dynamic object. */
3814 if (bed->elf_backend_merge_symbol_attribute)
3815 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3818 /* If this symbol has default visibility and the user has requested
3819 we not re-export it, then mark it as hidden. */
3820 if (definition && !dynamic
3822 || (abfd->my_archive && abfd->my_archive->no_export))
3823 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
3824 isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1));
3826 if (isym->st_other != 0 && !dynamic)
3828 unsigned char hvis, symvis, other, nvis;
3830 /* Take the balance of OTHER from the definition. */
3831 other = (definition ? isym->st_other : h->other);
3832 other &= ~ ELF_ST_VISIBILITY (-1);
3834 /* Combine visibilities, using the most constraining one. */
3835 hvis = ELF_ST_VISIBILITY (h->other);
3836 symvis = ELF_ST_VISIBILITY (isym->st_other);
3842 nvis = hvis < symvis ? hvis : symvis;
3844 h->other = other | nvis;
3847 /* Set a flag in the hash table entry indicating the type of
3848 reference or definition we just found. Keep a count of
3849 the number of dynamic symbols we find. A dynamic symbol
3850 is one which is referenced or defined by both a regular
3851 object and a shared object. */
3858 if (bind != STB_WEAK)
3859 h->ref_regular_nonweak = 1;
3863 if (! info->executable
3876 || (h->u.weakdef != NULL
3878 && h->u.weakdef->dynindx != -1))
3882 /* Check to see if we need to add an indirect symbol for
3883 the default name. */
3884 if (definition || h->root.type == bfd_link_hash_common)
3885 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
3886 &sec, &value, &dynsym,
3888 goto error_free_vers;
3890 if (definition && !dynamic)
3892 char *p = strchr (name, ELF_VER_CHR);
3893 if (p != NULL && p[1] != ELF_VER_CHR)
3895 /* Queue non-default versions so that .symver x, x@FOO
3896 aliases can be checked. */
3897 if (! nondeflt_vers)
3899 amt = (isymend - isym + 1)
3900 * sizeof (struct elf_link_hash_entry *);
3901 nondeflt_vers = bfd_malloc (amt);
3903 nondeflt_vers [nondeflt_vers_cnt++] = h;
3907 if (dynsym && h->dynindx == -1)
3909 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3910 goto error_free_vers;
3911 if (h->u.weakdef != NULL
3913 && h->u.weakdef->dynindx == -1)
3915 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
3916 goto error_free_vers;
3919 else if (dynsym && h->dynindx != -1)
3920 /* If the symbol already has a dynamic index, but
3921 visibility says it should not be visible, turn it into
3923 switch (ELF_ST_VISIBILITY (h->other))
3927 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
3938 const char *soname = elf_dt_name (abfd);
3940 /* A symbol from a library loaded via DT_NEEDED of some
3941 other library is referenced by a regular object.
3942 Add a DT_NEEDED entry for it. Issue an error if
3943 --no-add-needed is used. */
3944 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
3946 (*_bfd_error_handler)
3947 (_("%s: invalid DSO for symbol `%s' definition"),
3949 bfd_set_error (bfd_error_bad_value);
3950 goto error_free_vers;
3953 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
3956 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3958 goto error_free_vers;
3960 BFD_ASSERT (ret == 0);
3965 /* Now that all the symbols from this input file are created, handle
3966 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3967 if (nondeflt_vers != NULL)
3969 bfd_size_type cnt, symidx;
3971 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
3973 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
3974 char *shortname, *p;
3976 p = strchr (h->root.root.string, ELF_VER_CHR);
3978 || (h->root.type != bfd_link_hash_defined
3979 && h->root.type != bfd_link_hash_defweak))
3982 amt = p - h->root.root.string;
3983 shortname = bfd_malloc (amt + 1);
3984 memcpy (shortname, h->root.root.string, amt);
3985 shortname[amt] = '\0';
3987 hi = (struct elf_link_hash_entry *)
3988 bfd_link_hash_lookup (&hash_table->root, shortname,
3989 FALSE, FALSE, FALSE);
3991 && hi->root.type == h->root.type
3992 && hi->root.u.def.value == h->root.u.def.value
3993 && hi->root.u.def.section == h->root.u.def.section)
3995 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
3996 hi->root.type = bfd_link_hash_indirect;
3997 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
3998 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
3999 sym_hash = elf_sym_hashes (abfd);
4001 for (symidx = 0; symidx < extsymcount; ++symidx)
4002 if (sym_hash[symidx] == hi)
4004 sym_hash[symidx] = h;
4010 free (nondeflt_vers);
4011 nondeflt_vers = NULL;
4014 if (extversym != NULL)
4020 if (isymbuf != NULL)
4024 /* Now set the weakdefs field correctly for all the weak defined
4025 symbols we found. The only way to do this is to search all the
4026 symbols. Since we only need the information for non functions in
4027 dynamic objects, that's the only time we actually put anything on
4028 the list WEAKS. We need this information so that if a regular
4029 object refers to a symbol defined weakly in a dynamic object, the
4030 real symbol in the dynamic object is also put in the dynamic
4031 symbols; we also must arrange for both symbols to point to the
4032 same memory location. We could handle the general case of symbol
4033 aliasing, but a general symbol alias can only be generated in
4034 assembler code, handling it correctly would be very time
4035 consuming, and other ELF linkers don't handle general aliasing
4039 struct elf_link_hash_entry **hpp;
4040 struct elf_link_hash_entry **hppend;
4041 struct elf_link_hash_entry **sorted_sym_hash;
4042 struct elf_link_hash_entry *h;
4045 /* Since we have to search the whole symbol list for each weak
4046 defined symbol, search time for N weak defined symbols will be
4047 O(N^2). Binary search will cut it down to O(NlogN). */
4048 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4049 sorted_sym_hash = bfd_malloc (amt);
4050 if (sorted_sym_hash == NULL)
4052 sym_hash = sorted_sym_hash;
4053 hpp = elf_sym_hashes (abfd);
4054 hppend = hpp + extsymcount;
4056 for (; hpp < hppend; hpp++)
4060 && h->root.type == bfd_link_hash_defined
4061 && h->type != STT_FUNC)
4069 qsort (sorted_sym_hash, sym_count,
4070 sizeof (struct elf_link_hash_entry *),
4073 while (weaks != NULL)
4075 struct elf_link_hash_entry *hlook;
4082 weaks = hlook->u.weakdef;
4083 hlook->u.weakdef = NULL;
4085 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4086 || hlook->root.type == bfd_link_hash_defweak
4087 || hlook->root.type == bfd_link_hash_common
4088 || hlook->root.type == bfd_link_hash_indirect);
4089 slook = hlook->root.u.def.section;
4090 vlook = hlook->root.u.def.value;
4097 bfd_signed_vma vdiff;
4099 h = sorted_sym_hash [idx];
4100 vdiff = vlook - h->root.u.def.value;
4107 long sdiff = slook->id - h->root.u.def.section->id;
4120 /* We didn't find a value/section match. */
4124 for (i = ilook; i < sym_count; i++)
4126 h = sorted_sym_hash [i];
4128 /* Stop if value or section doesn't match. */
4129 if (h->root.u.def.value != vlook
4130 || h->root.u.def.section != slook)
4132 else if (h != hlook)
4134 hlook->u.weakdef = h;
4136 /* If the weak definition is in the list of dynamic
4137 symbols, make sure the real definition is put
4139 if (hlook->dynindx != -1 && h->dynindx == -1)
4141 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4145 /* If the real definition is in the list of dynamic
4146 symbols, make sure the weak definition is put
4147 there as well. If we don't do this, then the
4148 dynamic loader might not merge the entries for the
4149 real definition and the weak definition. */
4150 if (h->dynindx != -1 && hlook->dynindx == -1)
4152 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4160 free (sorted_sym_hash);
4163 check_directives = get_elf_backend_data (abfd)->check_directives;
4164 if (check_directives)
4165 check_directives (abfd, info);
4167 /* If this object is the same format as the output object, and it is
4168 not a shared library, then let the backend look through the
4171 This is required to build global offset table entries and to
4172 arrange for dynamic relocs. It is not required for the
4173 particular common case of linking non PIC code, even when linking
4174 against shared libraries, but unfortunately there is no way of
4175 knowing whether an object file has been compiled PIC or not.
4176 Looking through the relocs is not particularly time consuming.
4177 The problem is that we must either (1) keep the relocs in memory,
4178 which causes the linker to require additional runtime memory or
4179 (2) read the relocs twice from the input file, which wastes time.
4180 This would be a good case for using mmap.
4182 I have no idea how to handle linking PIC code into a file of a
4183 different format. It probably can't be done. */
4184 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4186 && is_elf_hash_table (hash_table)
4187 && hash_table->root.creator == abfd->xvec
4188 && check_relocs != NULL)
4192 for (o = abfd->sections; o != NULL; o = o->next)
4194 Elf_Internal_Rela *internal_relocs;
4197 if ((o->flags & SEC_RELOC) == 0
4198 || o->reloc_count == 0
4199 || ((info->strip == strip_all || info->strip == strip_debugger)
4200 && (o->flags & SEC_DEBUGGING) != 0)
4201 || bfd_is_abs_section (o->output_section))
4204 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4206 if (internal_relocs == NULL)
4209 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4211 if (elf_section_data (o)->relocs != internal_relocs)
4212 free (internal_relocs);
4219 /* If this is a non-traditional link, try to optimize the handling
4220 of the .stab/.stabstr sections. */
4222 && ! info->traditional_format
4223 && is_elf_hash_table (hash_table)
4224 && (info->strip != strip_all && info->strip != strip_debugger))
4228 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4229 if (stabstr != NULL)
4231 bfd_size_type string_offset = 0;
4234 for (stab = abfd->sections; stab; stab = stab->next)
4235 if (strncmp (".stab", stab->name, 5) == 0
4236 && (!stab->name[5] ||
4237 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4238 && (stab->flags & SEC_MERGE) == 0
4239 && !bfd_is_abs_section (stab->output_section))
4241 struct bfd_elf_section_data *secdata;
4243 secdata = elf_section_data (stab);
4244 if (! _bfd_link_section_stabs (abfd,
4245 &hash_table->stab_info,
4250 if (secdata->sec_info)
4251 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4256 if (is_elf_hash_table (hash_table))
4258 /* Add this bfd to the loaded list. */
4259 struct elf_link_loaded_list *n;
4261 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4265 n->next = hash_table->loaded;
4266 hash_table->loaded = n;
4272 if (nondeflt_vers != NULL)
4273 free (nondeflt_vers);
4274 if (extversym != NULL)
4277 if (isymbuf != NULL)
4283 /* Return the linker hash table entry of a symbol that might be
4284 satisfied by an archive symbol. Return -1 on error. */
4286 struct elf_link_hash_entry *
4287 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4288 struct bfd_link_info *info,
4291 struct elf_link_hash_entry *h;
4295 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4299 /* If this is a default version (the name contains @@), look up the
4300 symbol again with only one `@' as well as without the version.
4301 The effect is that references to the symbol with and without the
4302 version will be matched by the default symbol in the archive. */
4304 p = strchr (name, ELF_VER_CHR);
4305 if (p == NULL || p[1] != ELF_VER_CHR)
4308 /* First check with only one `@'. */
4309 len = strlen (name);
4310 copy = bfd_alloc (abfd, len);
4312 return (struct elf_link_hash_entry *) 0 - 1;
4314 first = p - name + 1;
4315 memcpy (copy, name, first);
4316 memcpy (copy + first, name + first + 1, len - first);
4318 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4321 /* We also need to check references to the symbol without the
4323 copy[first - 1] = '\0';
4324 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4325 FALSE, FALSE, FALSE);
4328 bfd_release (abfd, copy);
4332 /* Add symbols from an ELF archive file to the linker hash table. We
4333 don't use _bfd_generic_link_add_archive_symbols because of a
4334 problem which arises on UnixWare. The UnixWare libc.so is an
4335 archive which includes an entry libc.so.1 which defines a bunch of
4336 symbols. The libc.so archive also includes a number of other
4337 object files, which also define symbols, some of which are the same
4338 as those defined in libc.so.1. Correct linking requires that we
4339 consider each object file in turn, and include it if it defines any
4340 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4341 this; it looks through the list of undefined symbols, and includes
4342 any object file which defines them. When this algorithm is used on
4343 UnixWare, it winds up pulling in libc.so.1 early and defining a
4344 bunch of symbols. This means that some of the other objects in the
4345 archive are not included in the link, which is incorrect since they
4346 precede libc.so.1 in the archive.
4348 Fortunately, ELF archive handling is simpler than that done by
4349 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4350 oddities. In ELF, if we find a symbol in the archive map, and the
4351 symbol is currently undefined, we know that we must pull in that
4354 Unfortunately, we do have to make multiple passes over the symbol
4355 table until nothing further is resolved. */
4358 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4361 bfd_boolean *defined = NULL;
4362 bfd_boolean *included = NULL;
4366 const struct elf_backend_data *bed;
4367 struct elf_link_hash_entry * (*archive_symbol_lookup)
4368 (bfd *, struct bfd_link_info *, const char *);
4370 if (! bfd_has_map (abfd))
4372 /* An empty archive is a special case. */
4373 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4375 bfd_set_error (bfd_error_no_armap);
4379 /* Keep track of all symbols we know to be already defined, and all
4380 files we know to be already included. This is to speed up the
4381 second and subsequent passes. */
4382 c = bfd_ardata (abfd)->symdef_count;
4386 amt *= sizeof (bfd_boolean);
4387 defined = bfd_zmalloc (amt);
4388 included = bfd_zmalloc (amt);
4389 if (defined == NULL || included == NULL)
4392 symdefs = bfd_ardata (abfd)->symdefs;
4393 bed = get_elf_backend_data (abfd);
4394 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4407 symdefend = symdef + c;
4408 for (i = 0; symdef < symdefend; symdef++, i++)
4410 struct elf_link_hash_entry *h;
4412 struct bfd_link_hash_entry *undefs_tail;
4415 if (defined[i] || included[i])
4417 if (symdef->file_offset == last)
4423 h = archive_symbol_lookup (abfd, info, symdef->name);
4424 if (h == (struct elf_link_hash_entry *) 0 - 1)
4430 if (h->root.type == bfd_link_hash_common)
4432 /* We currently have a common symbol. The archive map contains
4433 a reference to this symbol, so we may want to include it. We
4434 only want to include it however, if this archive element
4435 contains a definition of the symbol, not just another common
4438 Unfortunately some archivers (including GNU ar) will put
4439 declarations of common symbols into their archive maps, as
4440 well as real definitions, so we cannot just go by the archive
4441 map alone. Instead we must read in the element's symbol
4442 table and check that to see what kind of symbol definition
4444 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4447 else if (h->root.type != bfd_link_hash_undefined)
4449 if (h->root.type != bfd_link_hash_undefweak)
4454 /* We need to include this archive member. */
4455 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4456 if (element == NULL)
4459 if (! bfd_check_format (element, bfd_object))
4462 /* Doublecheck that we have not included this object
4463 already--it should be impossible, but there may be
4464 something wrong with the archive. */
4465 if (element->archive_pass != 0)
4467 bfd_set_error (bfd_error_bad_value);
4470 element->archive_pass = 1;
4472 undefs_tail = info->hash->undefs_tail;
4474 if (! (*info->callbacks->add_archive_element) (info, element,
4477 if (! bfd_link_add_symbols (element, info))
4480 /* If there are any new undefined symbols, we need to make
4481 another pass through the archive in order to see whether
4482 they can be defined. FIXME: This isn't perfect, because
4483 common symbols wind up on undefs_tail and because an
4484 undefined symbol which is defined later on in this pass
4485 does not require another pass. This isn't a bug, but it
4486 does make the code less efficient than it could be. */
4487 if (undefs_tail != info->hash->undefs_tail)
4490 /* Look backward to mark all symbols from this object file
4491 which we have already seen in this pass. */
4495 included[mark] = TRUE;
4500 while (symdefs[mark].file_offset == symdef->file_offset);
4502 /* We mark subsequent symbols from this object file as we go
4503 on through the loop. */
4504 last = symdef->file_offset;
4515 if (defined != NULL)
4517 if (included != NULL)
4522 /* Given an ELF BFD, add symbols to the global hash table as
4526 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4528 switch (bfd_get_format (abfd))
4531 return elf_link_add_object_symbols (abfd, info);
4533 return elf_link_add_archive_symbols (abfd, info);
4535 bfd_set_error (bfd_error_wrong_format);
4540 /* This function will be called though elf_link_hash_traverse to store
4541 all hash value of the exported symbols in an array. */
4544 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4546 unsigned long **valuep = data;
4552 if (h->root.type == bfd_link_hash_warning)
4553 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4555 /* Ignore indirect symbols. These are added by the versioning code. */
4556 if (h->dynindx == -1)
4559 name = h->root.root.string;
4560 p = strchr (name, ELF_VER_CHR);
4563 alc = bfd_malloc (p - name + 1);
4564 memcpy (alc, name, p - name);
4565 alc[p - name] = '\0';
4569 /* Compute the hash value. */
4570 ha = bfd_elf_hash (name);
4572 /* Store the found hash value in the array given as the argument. */
4575 /* And store it in the struct so that we can put it in the hash table
4577 h->u.elf_hash_value = ha;
4585 /* Array used to determine the number of hash table buckets to use
4586 based on the number of symbols there are. If there are fewer than
4587 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4588 fewer than 37 we use 17 buckets, and so forth. We never use more
4589 than 32771 buckets. */
4591 static const size_t elf_buckets[] =
4593 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4597 /* Compute bucket count for hashing table. We do not use a static set
4598 of possible tables sizes anymore. Instead we determine for all
4599 possible reasonable sizes of the table the outcome (i.e., the
4600 number of collisions etc) and choose the best solution. The
4601 weighting functions are not too simple to allow the table to grow
4602 without bounds. Instead one of the weighting factors is the size.
4603 Therefore the result is always a good payoff between few collisions
4604 (= short chain lengths) and table size. */
4606 compute_bucket_count (struct bfd_link_info *info)
4608 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4609 size_t best_size = 0;
4610 unsigned long int *hashcodes;
4611 unsigned long int *hashcodesp;
4612 unsigned long int i;
4615 /* Compute the hash values for all exported symbols. At the same
4616 time store the values in an array so that we could use them for
4619 amt *= sizeof (unsigned long int);
4620 hashcodes = bfd_malloc (amt);
4621 if (hashcodes == NULL)
4623 hashcodesp = hashcodes;
4625 /* Put all hash values in HASHCODES. */
4626 elf_link_hash_traverse (elf_hash_table (info),
4627 elf_collect_hash_codes, &hashcodesp);
4629 /* We have a problem here. The following code to optimize the table
4630 size requires an integer type with more the 32 bits. If
4631 BFD_HOST_U_64_BIT is set we know about such a type. */
4632 #ifdef BFD_HOST_U_64_BIT
4635 unsigned long int nsyms = hashcodesp - hashcodes;
4638 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4639 unsigned long int *counts ;
4640 bfd *dynobj = elf_hash_table (info)->dynobj;
4641 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4643 /* Possible optimization parameters: if we have NSYMS symbols we say
4644 that the hashing table must at least have NSYMS/4 and at most
4646 minsize = nsyms / 4;
4649 best_size = maxsize = nsyms * 2;
4651 /* Create array where we count the collisions in. We must use bfd_malloc
4652 since the size could be large. */
4654 amt *= sizeof (unsigned long int);
4655 counts = bfd_malloc (amt);
4662 /* Compute the "optimal" size for the hash table. The criteria is a
4663 minimal chain length. The minor criteria is (of course) the size
4665 for (i = minsize; i < maxsize; ++i)
4667 /* Walk through the array of hashcodes and count the collisions. */
4668 BFD_HOST_U_64_BIT max;
4669 unsigned long int j;
4670 unsigned long int fact;
4672 memset (counts, '\0', i * sizeof (unsigned long int));
4674 /* Determine how often each hash bucket is used. */
4675 for (j = 0; j < nsyms; ++j)
4676 ++counts[hashcodes[j] % i];
4678 /* For the weight function we need some information about the
4679 pagesize on the target. This is information need not be 100%
4680 accurate. Since this information is not available (so far) we
4681 define it here to a reasonable default value. If it is crucial
4682 to have a better value some day simply define this value. */
4683 # ifndef BFD_TARGET_PAGESIZE
4684 # define BFD_TARGET_PAGESIZE (4096)
4687 /* We in any case need 2 + NSYMS entries for the size values and
4689 max = (2 + nsyms) * (bed->s->arch_size / 8);
4692 /* Variant 1: optimize for short chains. We add the squares
4693 of all the chain lengths (which favors many small chain
4694 over a few long chains). */
4695 for (j = 0; j < i; ++j)
4696 max += counts[j] * counts[j];
4698 /* This adds penalties for the overall size of the table. */
4699 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4702 /* Variant 2: Optimize a lot more for small table. Here we
4703 also add squares of the size but we also add penalties for
4704 empty slots (the +1 term). */
4705 for (j = 0; j < i; ++j)
4706 max += (1 + counts[j]) * (1 + counts[j]);
4708 /* The overall size of the table is considered, but not as
4709 strong as in variant 1, where it is squared. */
4710 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4714 /* Compare with current best results. */
4715 if (max < best_chlen)
4725 #endif /* defined (BFD_HOST_U_64_BIT) */
4727 /* This is the fallback solution if no 64bit type is available or if we
4728 are not supposed to spend much time on optimizations. We select the
4729 bucket count using a fixed set of numbers. */
4730 for (i = 0; elf_buckets[i] != 0; i++)
4732 best_size = elf_buckets[i];
4733 if (dynsymcount < elf_buckets[i + 1])
4738 /* Free the arrays we needed. */
4744 /* Set up the sizes and contents of the ELF dynamic sections. This is
4745 called by the ELF linker emulation before_allocation routine. We
4746 must set the sizes of the sections before the linker sets the
4747 addresses of the various sections. */
4750 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4753 const char *filter_shlib,
4754 const char * const *auxiliary_filters,
4755 struct bfd_link_info *info,
4756 asection **sinterpptr,
4757 struct bfd_elf_version_tree *verdefs)
4759 bfd_size_type soname_indx;
4761 const struct elf_backend_data *bed;
4762 struct elf_assign_sym_version_info asvinfo;
4766 soname_indx = (bfd_size_type) -1;
4768 if (!is_elf_hash_table (info->hash))
4771 elf_tdata (output_bfd)->relro = info->relro;
4772 if (info->execstack)
4773 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4774 else if (info->noexecstack)
4775 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4779 asection *notesec = NULL;
4782 for (inputobj = info->input_bfds;
4784 inputobj = inputobj->link_next)
4788 if (inputobj->flags & DYNAMIC)
4790 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4793 if (s->flags & SEC_CODE)
4802 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4803 if (exec && info->relocatable
4804 && notesec->output_section != bfd_abs_section_ptr)
4805 notesec->output_section->flags |= SEC_CODE;
4809 /* Any syms created from now on start with -1 in
4810 got.refcount/offset and plt.refcount/offset. */
4811 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
4813 /* The backend may have to create some sections regardless of whether
4814 we're dynamic or not. */
4815 bed = get_elf_backend_data (output_bfd);
4816 if (bed->elf_backend_always_size_sections
4817 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4820 dynobj = elf_hash_table (info)->dynobj;
4822 /* If there were no dynamic objects in the link, there is nothing to
4827 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
4830 if (elf_hash_table (info)->dynamic_sections_created)
4832 struct elf_info_failed eif;
4833 struct elf_link_hash_entry *h;
4835 struct bfd_elf_version_tree *t;
4836 struct bfd_elf_version_expr *d;
4837 bfd_boolean all_defined;
4839 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
4840 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
4844 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4846 if (soname_indx == (bfd_size_type) -1
4847 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
4853 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
4855 info->flags |= DF_SYMBOLIC;
4862 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
4864 if (indx == (bfd_size_type) -1
4865 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
4868 if (info->new_dtags)
4870 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
4871 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
4876 if (filter_shlib != NULL)
4880 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4881 filter_shlib, TRUE);
4882 if (indx == (bfd_size_type) -1
4883 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
4887 if (auxiliary_filters != NULL)
4889 const char * const *p;
4891 for (p = auxiliary_filters; *p != NULL; p++)
4895 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4897 if (indx == (bfd_size_type) -1
4898 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
4904 eif.verdefs = verdefs;
4907 /* If we are supposed to export all symbols into the dynamic symbol
4908 table (this is not the normal case), then do so. */
4909 if (info->export_dynamic)
4911 elf_link_hash_traverse (elf_hash_table (info),
4912 _bfd_elf_export_symbol,
4918 /* Make all global versions with definition. */
4919 for (t = verdefs; t != NULL; t = t->next)
4920 for (d = t->globals.list; d != NULL; d = d->next)
4921 if (!d->symver && d->symbol)
4923 const char *verstr, *name;
4924 size_t namelen, verlen, newlen;
4926 struct elf_link_hash_entry *newh;
4929 namelen = strlen (name);
4931 verlen = strlen (verstr);
4932 newlen = namelen + verlen + 3;
4934 newname = bfd_malloc (newlen);
4935 if (newname == NULL)
4937 memcpy (newname, name, namelen);
4939 /* Check the hidden versioned definition. */
4940 p = newname + namelen;
4942 memcpy (p, verstr, verlen + 1);
4943 newh = elf_link_hash_lookup (elf_hash_table (info),
4944 newname, FALSE, FALSE,
4947 || (newh->root.type != bfd_link_hash_defined
4948 && newh->root.type != bfd_link_hash_defweak))
4950 /* Check the default versioned definition. */
4952 memcpy (p, verstr, verlen + 1);
4953 newh = elf_link_hash_lookup (elf_hash_table (info),
4954 newname, FALSE, FALSE,
4959 /* Mark this version if there is a definition and it is
4960 not defined in a shared object. */
4962 && !newh->def_dynamic
4963 && (newh->root.type == bfd_link_hash_defined
4964 || newh->root.type == bfd_link_hash_defweak))
4968 /* Attach all the symbols to their version information. */
4969 asvinfo.output_bfd = output_bfd;
4970 asvinfo.info = info;
4971 asvinfo.verdefs = verdefs;
4972 asvinfo.failed = FALSE;
4974 elf_link_hash_traverse (elf_hash_table (info),
4975 _bfd_elf_link_assign_sym_version,
4980 if (!info->allow_undefined_version)
4982 /* Check if all global versions have a definition. */
4984 for (t = verdefs; t != NULL; t = t->next)
4985 for (d = t->globals.list; d != NULL; d = d->next)
4986 if (!d->symver && !d->script)
4988 (*_bfd_error_handler)
4989 (_("%s: undefined version: %s"),
4990 d->pattern, t->name);
4991 all_defined = FALSE;
4996 bfd_set_error (bfd_error_bad_value);
5001 /* Find all symbols which were defined in a dynamic object and make
5002 the backend pick a reasonable value for them. */
5003 elf_link_hash_traverse (elf_hash_table (info),
5004 _bfd_elf_adjust_dynamic_symbol,
5009 /* Add some entries to the .dynamic section. We fill in some of the
5010 values later, in bfd_elf_final_link, but we must add the entries
5011 now so that we know the final size of the .dynamic section. */
5013 /* If there are initialization and/or finalization functions to
5014 call then add the corresponding DT_INIT/DT_FINI entries. */
5015 h = (info->init_function
5016 ? elf_link_hash_lookup (elf_hash_table (info),
5017 info->init_function, FALSE,
5024 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5027 h = (info->fini_function
5028 ? elf_link_hash_lookup (elf_hash_table (info),
5029 info->fini_function, FALSE,
5036 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5040 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
5042 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5043 if (! info->executable)
5048 for (sub = info->input_bfds; sub != NULL;
5049 sub = sub->link_next)
5050 for (o = sub->sections; o != NULL; o = o->next)
5051 if (elf_section_data (o)->this_hdr.sh_type
5052 == SHT_PREINIT_ARRAY)
5054 (*_bfd_error_handler)
5055 (_("%B: .preinit_array section is not allowed in DSO"),
5060 bfd_set_error (bfd_error_nonrepresentable_section);
5064 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5065 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5068 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
5070 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5071 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5074 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
5076 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5077 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5081 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5082 /* If .dynstr is excluded from the link, we don't want any of
5083 these tags. Strictly, we should be checking each section
5084 individually; This quick check covers for the case where
5085 someone does a /DISCARD/ : { *(*) }. */
5086 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5088 bfd_size_type strsize;
5090 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5091 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5092 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5093 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5094 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5095 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5096 bed->s->sizeof_sym))
5101 /* The backend must work out the sizes of all the other dynamic
5103 if (bed->elf_backend_size_dynamic_sections
5104 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5107 if (elf_hash_table (info)->dynamic_sections_created)
5109 bfd_size_type dynsymcount;
5111 size_t bucketcount = 0;
5112 size_t hash_entry_size;
5113 unsigned int dtagcount;
5115 /* Set up the version definition section. */
5116 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5117 BFD_ASSERT (s != NULL);
5119 /* We may have created additional version definitions if we are
5120 just linking a regular application. */
5121 verdefs = asvinfo.verdefs;
5123 /* Skip anonymous version tag. */
5124 if (verdefs != NULL && verdefs->vernum == 0)
5125 verdefs = verdefs->next;
5127 if (verdefs == NULL && !info->create_default_symver)
5128 _bfd_strip_section_from_output (info, s);
5133 struct bfd_elf_version_tree *t;
5135 Elf_Internal_Verdef def;
5136 Elf_Internal_Verdaux defaux;
5137 struct bfd_link_hash_entry *bh;
5138 struct elf_link_hash_entry *h;
5144 /* Make space for the base version. */
5145 size += sizeof (Elf_External_Verdef);
5146 size += sizeof (Elf_External_Verdaux);
5149 /* Make space for the default version. */
5150 if (info->create_default_symver)
5152 size += sizeof (Elf_External_Verdef);
5156 for (t = verdefs; t != NULL; t = t->next)
5158 struct bfd_elf_version_deps *n;
5160 size += sizeof (Elf_External_Verdef);
5161 size += sizeof (Elf_External_Verdaux);
5164 for (n = t->deps; n != NULL; n = n->next)
5165 size += sizeof (Elf_External_Verdaux);
5169 s->contents = bfd_alloc (output_bfd, s->size);
5170 if (s->contents == NULL && s->size != 0)
5173 /* Fill in the version definition section. */
5177 def.vd_version = VER_DEF_CURRENT;
5178 def.vd_flags = VER_FLG_BASE;
5181 if (info->create_default_symver)
5183 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5184 def.vd_next = sizeof (Elf_External_Verdef);
5188 def.vd_aux = sizeof (Elf_External_Verdef);
5189 def.vd_next = (sizeof (Elf_External_Verdef)
5190 + sizeof (Elf_External_Verdaux));
5193 if (soname_indx != (bfd_size_type) -1)
5195 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5197 def.vd_hash = bfd_elf_hash (soname);
5198 defaux.vda_name = soname_indx;
5205 name = basename (output_bfd->filename);
5206 def.vd_hash = bfd_elf_hash (name);
5207 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5209 if (indx == (bfd_size_type) -1)
5211 defaux.vda_name = indx;
5213 defaux.vda_next = 0;
5215 _bfd_elf_swap_verdef_out (output_bfd, &def,
5216 (Elf_External_Verdef *) p);
5217 p += sizeof (Elf_External_Verdef);
5218 if (info->create_default_symver)
5220 /* Add a symbol representing this version. */
5222 if (! (_bfd_generic_link_add_one_symbol
5223 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5225 get_elf_backend_data (dynobj)->collect, &bh)))
5227 h = (struct elf_link_hash_entry *) bh;
5230 h->type = STT_OBJECT;
5231 h->verinfo.vertree = NULL;
5233 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5236 /* Create a duplicate of the base version with the same
5237 aux block, but different flags. */
5240 def.vd_aux = sizeof (Elf_External_Verdef);
5242 def.vd_next = (sizeof (Elf_External_Verdef)
5243 + sizeof (Elf_External_Verdaux));
5246 _bfd_elf_swap_verdef_out (output_bfd, &def,
5247 (Elf_External_Verdef *) p);
5248 p += sizeof (Elf_External_Verdef);
5250 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5251 (Elf_External_Verdaux *) p);
5252 p += sizeof (Elf_External_Verdaux);
5254 for (t = verdefs; t != NULL; t = t->next)
5257 struct bfd_elf_version_deps *n;
5260 for (n = t->deps; n != NULL; n = n->next)
5263 /* Add a symbol representing this version. */
5265 if (! (_bfd_generic_link_add_one_symbol
5266 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5268 get_elf_backend_data (dynobj)->collect, &bh)))
5270 h = (struct elf_link_hash_entry *) bh;
5273 h->type = STT_OBJECT;
5274 h->verinfo.vertree = t;
5276 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5279 def.vd_version = VER_DEF_CURRENT;
5281 if (t->globals.list == NULL
5282 && t->locals.list == NULL
5284 def.vd_flags |= VER_FLG_WEAK;
5285 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5286 def.vd_cnt = cdeps + 1;
5287 def.vd_hash = bfd_elf_hash (t->name);
5288 def.vd_aux = sizeof (Elf_External_Verdef);
5290 if (t->next != NULL)
5291 def.vd_next = (sizeof (Elf_External_Verdef)
5292 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5294 _bfd_elf_swap_verdef_out (output_bfd, &def,
5295 (Elf_External_Verdef *) p);
5296 p += sizeof (Elf_External_Verdef);
5298 defaux.vda_name = h->dynstr_index;
5299 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5301 defaux.vda_next = 0;
5302 if (t->deps != NULL)
5303 defaux.vda_next = sizeof (Elf_External_Verdaux);
5304 t->name_indx = defaux.vda_name;
5306 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5307 (Elf_External_Verdaux *) p);
5308 p += sizeof (Elf_External_Verdaux);
5310 for (n = t->deps; n != NULL; n = n->next)
5312 if (n->version_needed == NULL)
5314 /* This can happen if there was an error in the
5316 defaux.vda_name = 0;
5320 defaux.vda_name = n->version_needed->name_indx;
5321 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5324 if (n->next == NULL)
5325 defaux.vda_next = 0;
5327 defaux.vda_next = sizeof (Elf_External_Verdaux);
5329 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5330 (Elf_External_Verdaux *) p);
5331 p += sizeof (Elf_External_Verdaux);
5335 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5336 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5339 elf_tdata (output_bfd)->cverdefs = cdefs;
5342 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5344 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5347 else if (info->flags & DF_BIND_NOW)
5349 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5355 if (info->executable)
5356 info->flags_1 &= ~ (DF_1_INITFIRST
5359 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5363 /* Work out the size of the version reference section. */
5365 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5366 BFD_ASSERT (s != NULL);
5368 struct elf_find_verdep_info sinfo;
5370 sinfo.output_bfd = output_bfd;
5372 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5373 if (sinfo.vers == 0)
5375 sinfo.failed = FALSE;
5377 elf_link_hash_traverse (elf_hash_table (info),
5378 _bfd_elf_link_find_version_dependencies,
5381 if (elf_tdata (output_bfd)->verref == NULL)
5382 _bfd_strip_section_from_output (info, s);
5385 Elf_Internal_Verneed *t;
5390 /* Build the version definition section. */
5393 for (t = elf_tdata (output_bfd)->verref;
5397 Elf_Internal_Vernaux *a;
5399 size += sizeof (Elf_External_Verneed);
5401 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5402 size += sizeof (Elf_External_Vernaux);
5406 s->contents = bfd_alloc (output_bfd, s->size);
5407 if (s->contents == NULL)
5411 for (t = elf_tdata (output_bfd)->verref;
5416 Elf_Internal_Vernaux *a;
5420 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5423 t->vn_version = VER_NEED_CURRENT;
5425 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5426 elf_dt_name (t->vn_bfd) != NULL
5427 ? elf_dt_name (t->vn_bfd)
5428 : basename (t->vn_bfd->filename),
5430 if (indx == (bfd_size_type) -1)
5433 t->vn_aux = sizeof (Elf_External_Verneed);
5434 if (t->vn_nextref == NULL)
5437 t->vn_next = (sizeof (Elf_External_Verneed)
5438 + caux * sizeof (Elf_External_Vernaux));
5440 _bfd_elf_swap_verneed_out (output_bfd, t,
5441 (Elf_External_Verneed *) p);
5442 p += sizeof (Elf_External_Verneed);
5444 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5446 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5447 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5448 a->vna_nodename, FALSE);
5449 if (indx == (bfd_size_type) -1)
5452 if (a->vna_nextptr == NULL)
5455 a->vna_next = sizeof (Elf_External_Vernaux);
5457 _bfd_elf_swap_vernaux_out (output_bfd, a,
5458 (Elf_External_Vernaux *) p);
5459 p += sizeof (Elf_External_Vernaux);
5463 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5464 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5467 elf_tdata (output_bfd)->cverrefs = crefs;
5471 /* Assign dynsym indicies. In a shared library we generate a
5472 section symbol for each output section, which come first.
5473 Next come all of the back-end allocated local dynamic syms,
5474 followed by the rest of the global symbols. */
5476 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5478 /* Work out the size of the symbol version section. */
5479 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5480 BFD_ASSERT (s != NULL);
5481 if (dynsymcount == 0
5482 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL
5483 && !info->create_default_symver))
5485 _bfd_strip_section_from_output (info, s);
5486 /* The DYNSYMCOUNT might have changed if we were going to
5487 output a dynamic symbol table entry for S. */
5488 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5492 s->size = dynsymcount * sizeof (Elf_External_Versym);
5493 s->contents = bfd_zalloc (output_bfd, s->size);
5494 if (s->contents == NULL)
5497 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5501 /* Set the size of the .dynsym and .hash sections. We counted
5502 the number of dynamic symbols in elf_link_add_object_symbols.
5503 We will build the contents of .dynsym and .hash when we build
5504 the final symbol table, because until then we do not know the
5505 correct value to give the symbols. We built the .dynstr
5506 section as we went along in elf_link_add_object_symbols. */
5507 s = bfd_get_section_by_name (dynobj, ".dynsym");
5508 BFD_ASSERT (s != NULL);
5509 s->size = dynsymcount * bed->s->sizeof_sym;
5510 s->contents = bfd_alloc (output_bfd, s->size);
5511 if (s->contents == NULL && s->size != 0)
5514 if (dynsymcount != 0)
5516 Elf_Internal_Sym isym;
5518 /* The first entry in .dynsym is a dummy symbol. */
5525 bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0);
5528 /* Compute the size of the hashing table. As a side effect this
5529 computes the hash values for all the names we export. */
5530 bucketcount = compute_bucket_count (info);
5532 s = bfd_get_section_by_name (dynobj, ".hash");
5533 BFD_ASSERT (s != NULL);
5534 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5535 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5536 s->contents = bfd_zalloc (output_bfd, s->size);
5537 if (s->contents == NULL)
5540 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5541 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5542 s->contents + hash_entry_size);
5544 elf_hash_table (info)->bucketcount = bucketcount;
5546 s = bfd_get_section_by_name (dynobj, ".dynstr");
5547 BFD_ASSERT (s != NULL);
5549 elf_finalize_dynstr (output_bfd, info);
5551 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5553 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5554 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5561 /* Final phase of ELF linker. */
5563 /* A structure we use to avoid passing large numbers of arguments. */
5565 struct elf_final_link_info
5567 /* General link information. */
5568 struct bfd_link_info *info;
5571 /* Symbol string table. */
5572 struct bfd_strtab_hash *symstrtab;
5573 /* .dynsym section. */
5574 asection *dynsym_sec;
5575 /* .hash section. */
5577 /* symbol version section (.gnu.version). */
5578 asection *symver_sec;
5579 /* Buffer large enough to hold contents of any section. */
5581 /* Buffer large enough to hold external relocs of any section. */
5582 void *external_relocs;
5583 /* Buffer large enough to hold internal relocs of any section. */
5584 Elf_Internal_Rela *internal_relocs;
5585 /* Buffer large enough to hold external local symbols of any input
5587 bfd_byte *external_syms;
5588 /* And a buffer for symbol section indices. */
5589 Elf_External_Sym_Shndx *locsym_shndx;
5590 /* Buffer large enough to hold internal local symbols of any input
5592 Elf_Internal_Sym *internal_syms;
5593 /* Array large enough to hold a symbol index for each local symbol
5594 of any input BFD. */
5596 /* Array large enough to hold a section pointer for each local
5597 symbol of any input BFD. */
5598 asection **sections;
5599 /* Buffer to hold swapped out symbols. */
5601 /* And one for symbol section indices. */
5602 Elf_External_Sym_Shndx *symshndxbuf;
5603 /* Number of swapped out symbols in buffer. */
5604 size_t symbuf_count;
5605 /* Number of symbols which fit in symbuf. */
5607 /* And same for symshndxbuf. */
5608 size_t shndxbuf_size;
5611 /* This struct is used to pass information to elf_link_output_extsym. */
5613 struct elf_outext_info
5616 bfd_boolean localsyms;
5617 struct elf_final_link_info *finfo;
5620 /* When performing a relocatable link, the input relocations are
5621 preserved. But, if they reference global symbols, the indices
5622 referenced must be updated. Update all the relocations in
5623 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5626 elf_link_adjust_relocs (bfd *abfd,
5627 Elf_Internal_Shdr *rel_hdr,
5629 struct elf_link_hash_entry **rel_hash)
5632 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5634 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5635 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5636 bfd_vma r_type_mask;
5639 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5641 swap_in = bed->s->swap_reloc_in;
5642 swap_out = bed->s->swap_reloc_out;
5644 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5646 swap_in = bed->s->swap_reloca_in;
5647 swap_out = bed->s->swap_reloca_out;
5652 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5655 if (bed->s->arch_size == 32)
5662 r_type_mask = 0xffffffff;
5666 erela = rel_hdr->contents;
5667 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5669 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5672 if (*rel_hash == NULL)
5675 BFD_ASSERT ((*rel_hash)->indx >= 0);
5677 (*swap_in) (abfd, erela, irela);
5678 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5679 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5680 | (irela[j].r_info & r_type_mask));
5681 (*swap_out) (abfd, irela, erela);
5685 struct elf_link_sort_rela
5691 enum elf_reloc_type_class type;
5692 /* We use this as an array of size int_rels_per_ext_rel. */
5693 Elf_Internal_Rela rela[1];
5697 elf_link_sort_cmp1 (const void *A, const void *B)
5699 const struct elf_link_sort_rela *a = A;
5700 const struct elf_link_sort_rela *b = B;
5701 int relativea, relativeb;
5703 relativea = a->type == reloc_class_relative;
5704 relativeb = b->type == reloc_class_relative;
5706 if (relativea < relativeb)
5708 if (relativea > relativeb)
5710 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5712 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5714 if (a->rela->r_offset < b->rela->r_offset)
5716 if (a->rela->r_offset > b->rela->r_offset)
5722 elf_link_sort_cmp2 (const void *A, const void *B)
5724 const struct elf_link_sort_rela *a = A;
5725 const struct elf_link_sort_rela *b = B;
5728 if (a->u.offset < b->u.offset)
5730 if (a->u.offset > b->u.offset)
5732 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5733 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5738 if (a->rela->r_offset < b->rela->r_offset)
5740 if (a->rela->r_offset > b->rela->r_offset)
5746 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5749 bfd_size_type count, size;
5750 size_t i, ret, sort_elt, ext_size;
5751 bfd_byte *sort, *s_non_relative, *p;
5752 struct elf_link_sort_rela *sq;
5753 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5754 int i2e = bed->s->int_rels_per_ext_rel;
5755 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5756 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5757 struct bfd_link_order *lo;
5760 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5761 if (reldyn == NULL || reldyn->size == 0)
5763 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5764 if (reldyn == NULL || reldyn->size == 0)
5766 ext_size = bed->s->sizeof_rel;
5767 swap_in = bed->s->swap_reloc_in;
5768 swap_out = bed->s->swap_reloc_out;
5772 ext_size = bed->s->sizeof_rela;
5773 swap_in = bed->s->swap_reloca_in;
5774 swap_out = bed->s->swap_reloca_out;
5776 count = reldyn->size / ext_size;
5779 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5780 if (lo->type == bfd_indirect_link_order)
5782 asection *o = lo->u.indirect.section;
5786 if (size != reldyn->size)
5789 sort_elt = (sizeof (struct elf_link_sort_rela)
5790 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5791 sort = bfd_zmalloc (sort_elt * count);
5794 (*info->callbacks->warning)
5795 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5799 if (bed->s->arch_size == 32)
5800 r_sym_mask = ~(bfd_vma) 0xff;
5802 r_sym_mask = ~(bfd_vma) 0xffffffff;
5804 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5805 if (lo->type == bfd_indirect_link_order)
5807 bfd_byte *erel, *erelend;
5808 asection *o = lo->u.indirect.section;
5810 if (o->contents == NULL && o->size != 0)
5812 /* This is a reloc section that is being handled as a normal
5813 section. See bfd_section_from_shdr. We can't combine
5814 relocs in this case. */
5819 erelend = o->contents + o->size;
5820 p = sort + o->output_offset / ext_size * sort_elt;
5821 while (erel < erelend)
5823 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5824 (*swap_in) (abfd, erel, s->rela);
5825 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
5826 s->u.sym_mask = r_sym_mask;
5832 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
5834 for (i = 0, p = sort; i < count; i++, p += sort_elt)
5836 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5837 if (s->type != reloc_class_relative)
5843 sq = (struct elf_link_sort_rela *) s_non_relative;
5844 for (; i < count; i++, p += sort_elt)
5846 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
5847 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
5849 sp->u.offset = sq->rela->r_offset;
5852 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
5854 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5855 if (lo->type == bfd_indirect_link_order)
5857 bfd_byte *erel, *erelend;
5858 asection *o = lo->u.indirect.section;
5861 erelend = o->contents + o->size;
5862 p = sort + o->output_offset / ext_size * sort_elt;
5863 while (erel < erelend)
5865 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5866 (*swap_out) (abfd, s->rela, erel);
5877 /* Flush the output symbols to the file. */
5880 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
5881 const struct elf_backend_data *bed)
5883 if (finfo->symbuf_count > 0)
5885 Elf_Internal_Shdr *hdr;
5889 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5890 pos = hdr->sh_offset + hdr->sh_size;
5891 amt = finfo->symbuf_count * bed->s->sizeof_sym;
5892 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5893 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
5896 hdr->sh_size += amt;
5897 finfo->symbuf_count = 0;
5903 /* Add a symbol to the output symbol table. */
5906 elf_link_output_sym (struct elf_final_link_info *finfo,
5908 Elf_Internal_Sym *elfsym,
5909 asection *input_sec,
5910 struct elf_link_hash_entry *h)
5913 Elf_External_Sym_Shndx *destshndx;
5914 bfd_boolean (*output_symbol_hook)
5915 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
5916 struct elf_link_hash_entry *);
5917 const struct elf_backend_data *bed;
5919 bed = get_elf_backend_data (finfo->output_bfd);
5920 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
5921 if (output_symbol_hook != NULL)
5923 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
5927 if (name == NULL || *name == '\0')
5928 elfsym->st_name = 0;
5929 else if (input_sec->flags & SEC_EXCLUDE)
5930 elfsym->st_name = 0;
5933 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5935 if (elfsym->st_name == (unsigned long) -1)
5939 if (finfo->symbuf_count >= finfo->symbuf_size)
5941 if (! elf_link_flush_output_syms (finfo, bed))
5945 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
5946 destshndx = finfo->symshndxbuf;
5947 if (destshndx != NULL)
5949 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
5953 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
5954 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
5955 if (destshndx == NULL)
5957 memset ((char *) destshndx + amt, 0, amt);
5958 finfo->shndxbuf_size *= 2;
5960 destshndx += bfd_get_symcount (finfo->output_bfd);
5963 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
5964 finfo->symbuf_count += 1;
5965 bfd_get_symcount (finfo->output_bfd) += 1;
5970 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5971 allowing an unsatisfied unversioned symbol in the DSO to match a
5972 versioned symbol that would normally require an explicit version.
5973 We also handle the case that a DSO references a hidden symbol
5974 which may be satisfied by a versioned symbol in another DSO. */
5977 elf_link_check_versioned_symbol (struct bfd_link_info *info,
5978 const struct elf_backend_data *bed,
5979 struct elf_link_hash_entry *h)
5982 struct elf_link_loaded_list *loaded;
5984 if (!is_elf_hash_table (info->hash))
5987 switch (h->root.type)
5993 case bfd_link_hash_undefined:
5994 case bfd_link_hash_undefweak:
5995 abfd = h->root.u.undef.abfd;
5996 if ((abfd->flags & DYNAMIC) == 0
5997 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
6001 case bfd_link_hash_defined:
6002 case bfd_link_hash_defweak:
6003 abfd = h->root.u.def.section->owner;
6006 case bfd_link_hash_common:
6007 abfd = h->root.u.c.p->section->owner;
6010 BFD_ASSERT (abfd != NULL);
6012 for (loaded = elf_hash_table (info)->loaded;
6014 loaded = loaded->next)
6017 Elf_Internal_Shdr *hdr;
6018 bfd_size_type symcount;
6019 bfd_size_type extsymcount;
6020 bfd_size_type extsymoff;
6021 Elf_Internal_Shdr *versymhdr;
6022 Elf_Internal_Sym *isym;
6023 Elf_Internal_Sym *isymend;
6024 Elf_Internal_Sym *isymbuf;
6025 Elf_External_Versym *ever;
6026 Elf_External_Versym *extversym;
6028 input = loaded->abfd;
6030 /* We check each DSO for a possible hidden versioned definition. */
6032 || (input->flags & DYNAMIC) == 0
6033 || elf_dynversym (input) == 0)
6036 hdr = &elf_tdata (input)->dynsymtab_hdr;
6038 symcount = hdr->sh_size / bed->s->sizeof_sym;
6039 if (elf_bad_symtab (input))
6041 extsymcount = symcount;
6046 extsymcount = symcount - hdr->sh_info;
6047 extsymoff = hdr->sh_info;
6050 if (extsymcount == 0)
6053 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6055 if (isymbuf == NULL)
6058 /* Read in any version definitions. */
6059 versymhdr = &elf_tdata (input)->dynversym_hdr;
6060 extversym = bfd_malloc (versymhdr->sh_size);
6061 if (extversym == NULL)
6064 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6065 || (bfd_bread (extversym, versymhdr->sh_size, input)
6066 != versymhdr->sh_size))
6074 ever = extversym + extsymoff;
6075 isymend = isymbuf + extsymcount;
6076 for (isym = isymbuf; isym < isymend; isym++, ever++)
6079 Elf_Internal_Versym iver;
6080 unsigned short version_index;
6082 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6083 || isym->st_shndx == SHN_UNDEF)
6086 name = bfd_elf_string_from_elf_section (input,
6089 if (strcmp (name, h->root.root.string) != 0)
6092 _bfd_elf_swap_versym_in (input, ever, &iver);
6094 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6096 /* If we have a non-hidden versioned sym, then it should
6097 have provided a definition for the undefined sym. */
6101 version_index = iver.vs_vers & VERSYM_VERSION;
6102 if (version_index == 1 || version_index == 2)
6104 /* This is the base or first version. We can use it. */
6118 /* Add an external symbol to the symbol table. This is called from
6119 the hash table traversal routine. When generating a shared object,
6120 we go through the symbol table twice. The first time we output
6121 anything that might have been forced to local scope in a version
6122 script. The second time we output the symbols that are still
6126 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6128 struct elf_outext_info *eoinfo = data;
6129 struct elf_final_link_info *finfo = eoinfo->finfo;
6131 Elf_Internal_Sym sym;
6132 asection *input_sec;
6133 const struct elf_backend_data *bed;
6135 if (h->root.type == bfd_link_hash_warning)
6137 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6138 if (h->root.type == bfd_link_hash_new)
6142 /* Decide whether to output this symbol in this pass. */
6143 if (eoinfo->localsyms)
6145 if (!h->forced_local)
6150 if (h->forced_local)
6154 bed = get_elf_backend_data (finfo->output_bfd);
6156 /* If we have an undefined symbol reference here then it must have
6157 come from a shared library that is being linked in. (Undefined
6158 references in regular files have already been handled). If we
6159 are reporting errors for this situation then do so now. */
6160 if (h->root.type == bfd_link_hash_undefined
6163 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6164 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6166 if (! ((*finfo->info->callbacks->undefined_symbol)
6167 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6168 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6170 eoinfo->failed = TRUE;
6175 /* We should also warn if a forced local symbol is referenced from
6176 shared libraries. */
6177 if (! finfo->info->relocatable
6178 && (! finfo->info->shared)
6183 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6185 (*_bfd_error_handler)
6186 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6187 finfo->output_bfd, h->root.u.def.section->owner,
6188 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6190 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6191 ? "hidden" : "local",
6192 h->root.root.string);
6193 eoinfo->failed = TRUE;
6197 /* We don't want to output symbols that have never been mentioned by
6198 a regular file, or that we have been told to strip. However, if
6199 h->indx is set to -2, the symbol is used by a reloc and we must
6203 else if ((h->def_dynamic
6208 else if (finfo->info->strip == strip_all)
6210 else if (finfo->info->strip == strip_some
6211 && bfd_hash_lookup (finfo->info->keep_hash,
6212 h->root.root.string, FALSE, FALSE) == NULL)
6214 else if (finfo->info->strip_discarded
6215 && (h->root.type == bfd_link_hash_defined
6216 || h->root.type == bfd_link_hash_defweak)
6217 && elf_discarded_section (h->root.u.def.section))
6222 /* If we're stripping it, and it's not a dynamic symbol, there's
6223 nothing else to do unless it is a forced local symbol. */
6226 && !h->forced_local)
6230 sym.st_size = h->size;
6231 sym.st_other = h->other;
6232 if (h->forced_local)
6233 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6234 else if (h->root.type == bfd_link_hash_undefweak
6235 || h->root.type == bfd_link_hash_defweak)
6236 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6238 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6240 switch (h->root.type)
6243 case bfd_link_hash_new:
6244 case bfd_link_hash_warning:
6248 case bfd_link_hash_undefined:
6249 case bfd_link_hash_undefweak:
6250 input_sec = bfd_und_section_ptr;
6251 sym.st_shndx = SHN_UNDEF;
6254 case bfd_link_hash_defined:
6255 case bfd_link_hash_defweak:
6257 input_sec = h->root.u.def.section;
6258 if (input_sec->output_section != NULL)
6261 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6262 input_sec->output_section);
6263 if (sym.st_shndx == SHN_BAD)
6265 (*_bfd_error_handler)
6266 (_("%B: could not find output section %A for input section %A"),
6267 finfo->output_bfd, input_sec->output_section, input_sec);
6268 eoinfo->failed = TRUE;
6272 /* ELF symbols in relocatable files are section relative,
6273 but in nonrelocatable files they are virtual
6275 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6276 if (! finfo->info->relocatable)
6278 sym.st_value += input_sec->output_section->vma;
6279 if (h->type == STT_TLS)
6281 /* STT_TLS symbols are relative to PT_TLS segment
6283 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6284 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6290 BFD_ASSERT (input_sec->owner == NULL
6291 || (input_sec->owner->flags & DYNAMIC) != 0);
6292 sym.st_shndx = SHN_UNDEF;
6293 input_sec = bfd_und_section_ptr;
6298 case bfd_link_hash_common:
6299 input_sec = h->root.u.c.p->section;
6300 sym.st_shndx = SHN_COMMON;
6301 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6304 case bfd_link_hash_indirect:
6305 /* These symbols are created by symbol versioning. They point
6306 to the decorated version of the name. For example, if the
6307 symbol foo@@GNU_1.2 is the default, which should be used when
6308 foo is used with no version, then we add an indirect symbol
6309 foo which points to foo@@GNU_1.2. We ignore these symbols,
6310 since the indirected symbol is already in the hash table. */
6314 /* Give the processor backend a chance to tweak the symbol value,
6315 and also to finish up anything that needs to be done for this
6316 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6317 forced local syms when non-shared is due to a historical quirk. */
6318 if ((h->dynindx != -1
6320 && ((finfo->info->shared
6321 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6322 || h->root.type != bfd_link_hash_undefweak))
6323 || !h->forced_local)
6324 && elf_hash_table (finfo->info)->dynamic_sections_created)
6326 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6327 (finfo->output_bfd, finfo->info, h, &sym)))
6329 eoinfo->failed = TRUE;
6334 /* If we are marking the symbol as undefined, and there are no
6335 non-weak references to this symbol from a regular object, then
6336 mark the symbol as weak undefined; if there are non-weak
6337 references, mark the symbol as strong. We can't do this earlier,
6338 because it might not be marked as undefined until the
6339 finish_dynamic_symbol routine gets through with it. */
6340 if (sym.st_shndx == SHN_UNDEF
6342 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6343 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6347 if (h->ref_regular_nonweak)
6348 bindtype = STB_GLOBAL;
6350 bindtype = STB_WEAK;
6351 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6354 /* If a non-weak symbol with non-default visibility is not defined
6355 locally, it is a fatal error. */
6356 if (! finfo->info->relocatable
6357 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6358 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6359 && h->root.type == bfd_link_hash_undefined
6362 (*_bfd_error_handler)
6363 (_("%B: %s symbol `%s' isn't defined"),
6365 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6367 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6368 ? "internal" : "hidden",
6369 h->root.root.string);
6370 eoinfo->failed = TRUE;
6374 /* If this symbol should be put in the .dynsym section, then put it
6375 there now. We already know the symbol index. We also fill in
6376 the entry in the .hash section. */
6377 if (h->dynindx != -1
6378 && elf_hash_table (finfo->info)->dynamic_sections_created)
6382 size_t hash_entry_size;
6383 bfd_byte *bucketpos;
6387 sym.st_name = h->dynstr_index;
6388 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6389 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6391 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6392 bucket = h->u.elf_hash_value % bucketcount;
6394 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6395 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6396 + (bucket + 2) * hash_entry_size);
6397 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6398 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6399 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6400 ((bfd_byte *) finfo->hash_sec->contents
6401 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6403 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6405 Elf_Internal_Versym iversym;
6406 Elf_External_Versym *eversym;
6408 if (!h->def_regular)
6410 if (h->verinfo.verdef == NULL)
6411 iversym.vs_vers = 0;
6413 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6417 if (h->verinfo.vertree == NULL)
6418 iversym.vs_vers = 1;
6420 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6421 if (finfo->info->create_default_symver)
6426 iversym.vs_vers |= VERSYM_HIDDEN;
6428 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6429 eversym += h->dynindx;
6430 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6434 /* If we're stripping it, then it was just a dynamic symbol, and
6435 there's nothing else to do. */
6436 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6439 h->indx = bfd_get_symcount (finfo->output_bfd);
6441 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6443 eoinfo->failed = TRUE;
6450 /* Return TRUE if special handling is done for relocs in SEC against
6451 symbols defined in discarded sections. */
6454 elf_section_ignore_discarded_relocs (asection *sec)
6456 const struct elf_backend_data *bed;
6458 switch (sec->sec_info_type)
6460 case ELF_INFO_TYPE_STABS:
6461 case ELF_INFO_TYPE_EH_FRAME:
6467 bed = get_elf_backend_data (sec->owner);
6468 if (bed->elf_backend_ignore_discarded_relocs != NULL
6469 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6475 enum action_discarded
6481 /* Return a mask saying how ld should treat relocations in SEC against
6482 symbols defined in discarded sections. If this function returns
6483 COMPLAIN set, ld will issue a warning message. If this function
6484 returns PRETEND set, and the discarded section was link-once and the
6485 same size as the kept link-once section, ld will pretend that the
6486 symbol was actually defined in the kept section. Otherwise ld will
6487 zero the reloc (at least that is the intent, but some cooperation by
6488 the target dependent code is needed, particularly for REL targets). */
6491 elf_action_discarded (asection *sec)
6493 if (sec->flags & SEC_DEBUGGING)
6496 if (strcmp (".eh_frame", sec->name) == 0)
6499 if (strcmp (".gcc_except_table", sec->name) == 0)
6502 if (strcmp (".PARISC.unwind", sec->name) == 0)
6505 if (strcmp (".fixup", sec->name) == 0)
6508 return COMPLAIN | PRETEND;
6511 /* Find a match between a section and a member of a section group. */
6514 match_group_member (asection *sec, asection *group)
6516 asection *first = elf_next_in_group (group);
6517 asection *s = first;
6521 if (bfd_elf_match_symbols_in_sections (s, sec))
6531 /* Link an input file into the linker output file. This function
6532 handles all the sections and relocations of the input file at once.
6533 This is so that we only have to read the local symbols once, and
6534 don't have to keep them in memory. */
6537 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6539 bfd_boolean (*relocate_section)
6540 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6541 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6543 Elf_Internal_Shdr *symtab_hdr;
6546 Elf_Internal_Sym *isymbuf;
6547 Elf_Internal_Sym *isym;
6548 Elf_Internal_Sym *isymend;
6550 asection **ppsection;
6552 const struct elf_backend_data *bed;
6553 bfd_boolean emit_relocs;
6554 struct elf_link_hash_entry **sym_hashes;
6556 output_bfd = finfo->output_bfd;
6557 bed = get_elf_backend_data (output_bfd);
6558 relocate_section = bed->elf_backend_relocate_section;
6560 /* If this is a dynamic object, we don't want to do anything here:
6561 we don't want the local symbols, and we don't want the section
6563 if ((input_bfd->flags & DYNAMIC) != 0)
6566 emit_relocs = (finfo->info->relocatable
6567 || finfo->info->emitrelocations
6568 || bed->elf_backend_emit_relocs);
6570 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6571 if (elf_bad_symtab (input_bfd))
6573 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6578 locsymcount = symtab_hdr->sh_info;
6579 extsymoff = symtab_hdr->sh_info;
6582 /* Read the local symbols. */
6583 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6584 if (isymbuf == NULL && locsymcount != 0)
6586 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6587 finfo->internal_syms,
6588 finfo->external_syms,
6589 finfo->locsym_shndx);
6590 if (isymbuf == NULL)
6594 /* Find local symbol sections and adjust values of symbols in
6595 SEC_MERGE sections. Write out those local symbols we know are
6596 going into the output file. */
6597 isymend = isymbuf + locsymcount;
6598 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6600 isym++, pindex++, ppsection++)
6604 Elf_Internal_Sym osym;
6608 if (elf_bad_symtab (input_bfd))
6610 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6617 if (isym->st_shndx == SHN_UNDEF)
6618 isec = bfd_und_section_ptr;
6619 else if (isym->st_shndx < SHN_LORESERVE
6620 || isym->st_shndx > SHN_HIRESERVE)
6622 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6624 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6625 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6627 _bfd_merged_section_offset (output_bfd, &isec,
6628 elf_section_data (isec)->sec_info,
6631 else if (isym->st_shndx == SHN_ABS)
6632 isec = bfd_abs_section_ptr;
6633 else if (isym->st_shndx == SHN_COMMON)
6634 isec = bfd_com_section_ptr;
6643 /* Don't output the first, undefined, symbol. */
6644 if (ppsection == finfo->sections)
6647 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6649 /* We never output section symbols. Instead, we use the
6650 section symbol of the corresponding section in the output
6655 /* If we are stripping all symbols, we don't want to output this
6657 if (finfo->info->strip == strip_all)
6660 /* If we are discarding all local symbols, we don't want to
6661 output this one. If we are generating a relocatable output
6662 file, then some of the local symbols may be required by
6663 relocs; we output them below as we discover that they are
6665 if (finfo->info->discard == discard_all)
6668 /* If this symbol is defined in a section which we are
6669 discarding, we don't need to keep it, but note that
6670 linker_mark is only reliable for sections that have contents.
6671 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6672 as well as linker_mark. */
6673 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6675 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6676 || (! finfo->info->relocatable
6677 && (isec->flags & SEC_EXCLUDE) != 0)))
6680 /* Get the name of the symbol. */
6681 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6686 /* See if we are discarding symbols with this name. */
6687 if ((finfo->info->strip == strip_some
6688 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6690 || (((finfo->info->discard == discard_sec_merge
6691 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6692 || finfo->info->discard == discard_l)
6693 && bfd_is_local_label_name (input_bfd, name)))
6696 /* If we get here, we are going to output this symbol. */
6700 /* Adjust the section index for the output file. */
6701 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6702 isec->output_section);
6703 if (osym.st_shndx == SHN_BAD)
6706 *pindex = bfd_get_symcount (output_bfd);
6708 /* ELF symbols in relocatable files are section relative, but
6709 in executable files they are virtual addresses. Note that
6710 this code assumes that all ELF sections have an associated
6711 BFD section with a reasonable value for output_offset; below
6712 we assume that they also have a reasonable value for
6713 output_section. Any special sections must be set up to meet
6714 these requirements. */
6715 osym.st_value += isec->output_offset;
6716 if (! finfo->info->relocatable)
6718 osym.st_value += isec->output_section->vma;
6719 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6721 /* STT_TLS symbols are relative to PT_TLS segment base. */
6722 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6723 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6727 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6731 /* Relocate the contents of each section. */
6732 sym_hashes = elf_sym_hashes (input_bfd);
6733 for (o = input_bfd->sections; o != NULL; o = o->next)
6737 if (! o->linker_mark)
6739 /* This section was omitted from the link. */
6743 if ((o->flags & SEC_HAS_CONTENTS) == 0
6744 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6747 if ((o->flags & SEC_LINKER_CREATED) != 0)
6749 /* Section was created by _bfd_elf_link_create_dynamic_sections
6754 /* Get the contents of the section. They have been cached by a
6755 relaxation routine. Note that o is a section in an input
6756 file, so the contents field will not have been set by any of
6757 the routines which work on output files. */
6758 if (elf_section_data (o)->this_hdr.contents != NULL)
6759 contents = elf_section_data (o)->this_hdr.contents;
6762 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6764 contents = finfo->contents;
6765 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6769 if ((o->flags & SEC_RELOC) != 0)
6771 Elf_Internal_Rela *internal_relocs;
6772 bfd_vma r_type_mask;
6775 /* Get the swapped relocs. */
6777 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6778 finfo->internal_relocs, FALSE);
6779 if (internal_relocs == NULL
6780 && o->reloc_count > 0)
6783 if (bed->s->arch_size == 32)
6790 r_type_mask = 0xffffffff;
6794 /* Run through the relocs looking for any against symbols
6795 from discarded sections and section symbols from
6796 removed link-once sections. Complain about relocs
6797 against discarded sections. Zero relocs against removed
6798 link-once sections. Preserve debug information as much
6800 if (!elf_section_ignore_discarded_relocs (o))
6802 Elf_Internal_Rela *rel, *relend;
6803 unsigned int action = elf_action_discarded (o);
6805 rel = internal_relocs;
6806 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6807 for ( ; rel < relend; rel++)
6809 unsigned long r_symndx = rel->r_info >> r_sym_shift;
6810 asection **ps, *sec;
6811 struct elf_link_hash_entry *h = NULL;
6812 const char *sym_name;
6814 if (r_symndx == STN_UNDEF)
6817 if (r_symndx >= locsymcount
6818 || (elf_bad_symtab (input_bfd)
6819 && finfo->sections[r_symndx] == NULL))
6821 h = sym_hashes[r_symndx - extsymoff];
6822 while (h->root.type == bfd_link_hash_indirect
6823 || h->root.type == bfd_link_hash_warning)
6824 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6826 if (h->root.type != bfd_link_hash_defined
6827 && h->root.type != bfd_link_hash_defweak)
6830 ps = &h->root.u.def.section;
6831 sym_name = h->root.root.string;
6835 Elf_Internal_Sym *sym = isymbuf + r_symndx;
6836 ps = &finfo->sections[r_symndx];
6837 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym);
6840 /* Complain if the definition comes from a
6841 discarded section. */
6842 if ((sec = *ps) != NULL && elf_discarded_section (sec))
6846 BFD_ASSERT (r_symndx != 0);
6847 if (action & COMPLAIN)
6849 (*_bfd_error_handler)
6850 (_("`%s' referenced in section `%A' of %B: "
6851 "defined in discarded section `%A' of %B\n"),
6852 o, input_bfd, sec, sec->owner, sym_name);
6855 /* Try to do the best we can to support buggy old
6856 versions of gcc. If we've warned, or this is
6857 debugging info, pretend that the symbol is
6858 really defined in the kept linkonce section.
6859 FIXME: This is quite broken. Modifying the
6860 symbol here means we will be changing all later
6861 uses of the symbol, not just in this section.
6862 The only thing that makes this half reasonable
6863 is that we warn in non-debug sections, and
6864 debug sections tend to come after other
6866 kept = sec->kept_section;
6867 if (kept != NULL && (action & PRETEND))
6869 if (elf_sec_group (sec) != NULL)
6870 kept = match_group_member (sec, kept);
6872 && sec->size == kept->size)
6879 /* Remove the symbol reference from the reloc, but
6880 don't kill the reloc completely. This is so that
6881 a zero value will be written into the section,
6882 which may have non-zero contents put there by the
6883 assembler. Zero in things like an eh_frame fde
6884 pc_begin allows stack unwinders to recognize the
6886 rel->r_info &= r_type_mask;
6892 /* Relocate the section by invoking a back end routine.
6894 The back end routine is responsible for adjusting the
6895 section contents as necessary, and (if using Rela relocs
6896 and generating a relocatable output file) adjusting the
6897 reloc addend as necessary.
6899 The back end routine does not have to worry about setting
6900 the reloc address or the reloc symbol index.
6902 The back end routine is given a pointer to the swapped in
6903 internal symbols, and can access the hash table entries
6904 for the external symbols via elf_sym_hashes (input_bfd).
6906 When generating relocatable output, the back end routine
6907 must handle STB_LOCAL/STT_SECTION symbols specially. The
6908 output symbol is going to be a section symbol
6909 corresponding to the output section, which will require
6910 the addend to be adjusted. */
6912 if (! (*relocate_section) (output_bfd, finfo->info,
6913 input_bfd, o, contents,
6921 Elf_Internal_Rela *irela;
6922 Elf_Internal_Rela *irelaend;
6923 bfd_vma last_offset;
6924 struct elf_link_hash_entry **rel_hash;
6925 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
6926 unsigned int next_erel;
6927 bfd_boolean (*reloc_emitter)
6928 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
6929 bfd_boolean rela_normal;
6931 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6932 rela_normal = (bed->rela_normal
6933 && (input_rel_hdr->sh_entsize
6934 == bed->s->sizeof_rela));
6936 /* Adjust the reloc addresses and symbol indices. */
6938 irela = internal_relocs;
6939 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6940 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6941 + elf_section_data (o->output_section)->rel_count
6942 + elf_section_data (o->output_section)->rel_count2);
6943 last_offset = o->output_offset;
6944 if (!finfo->info->relocatable)
6945 last_offset += o->output_section->vma;
6946 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6948 unsigned long r_symndx;
6950 Elf_Internal_Sym sym;
6952 if (next_erel == bed->s->int_rels_per_ext_rel)
6958 irela->r_offset = _bfd_elf_section_offset (output_bfd,
6961 if (irela->r_offset >= (bfd_vma) -2)
6963 /* This is a reloc for a deleted entry or somesuch.
6964 Turn it into an R_*_NONE reloc, at the same
6965 offset as the last reloc. elf_eh_frame.c and
6966 elf_bfd_discard_info rely on reloc offsets
6968 irela->r_offset = last_offset;
6970 irela->r_addend = 0;
6974 irela->r_offset += o->output_offset;
6976 /* Relocs in an executable have to be virtual addresses. */
6977 if (!finfo->info->relocatable)
6978 irela->r_offset += o->output_section->vma;
6980 last_offset = irela->r_offset;
6982 r_symndx = irela->r_info >> r_sym_shift;
6983 if (r_symndx == STN_UNDEF)
6986 if (r_symndx >= locsymcount
6987 || (elf_bad_symtab (input_bfd)
6988 && finfo->sections[r_symndx] == NULL))
6990 struct elf_link_hash_entry *rh;
6993 /* This is a reloc against a global symbol. We
6994 have not yet output all the local symbols, so
6995 we do not know the symbol index of any global
6996 symbol. We set the rel_hash entry for this
6997 reloc to point to the global hash table entry
6998 for this symbol. The symbol index is then
6999 set at the end of bfd_elf_final_link. */
7000 indx = r_symndx - extsymoff;
7001 rh = elf_sym_hashes (input_bfd)[indx];
7002 while (rh->root.type == bfd_link_hash_indirect
7003 || rh->root.type == bfd_link_hash_warning)
7004 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7006 /* Setting the index to -2 tells
7007 elf_link_output_extsym that this symbol is
7009 BFD_ASSERT (rh->indx < 0);
7017 /* This is a reloc against a local symbol. */
7020 sym = isymbuf[r_symndx];
7021 sec = finfo->sections[r_symndx];
7022 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7024 /* I suppose the backend ought to fill in the
7025 section of any STT_SECTION symbol against a
7026 processor specific section. */
7028 if (bfd_is_abs_section (sec))
7030 else if (sec == NULL || sec->owner == NULL)
7032 bfd_set_error (bfd_error_bad_value);
7037 asection *osec = sec->output_section;
7039 /* If we have discarded a section, the output
7040 section will be the absolute section. In
7041 case of discarded link-once and discarded
7042 SEC_MERGE sections, use the kept section. */
7043 if (bfd_is_abs_section (osec)
7044 && sec->kept_section != NULL
7045 && sec->kept_section->output_section != NULL)
7047 osec = sec->kept_section->output_section;
7048 irela->r_addend -= osec->vma;
7051 if (!bfd_is_abs_section (osec))
7053 r_symndx = osec->target_index;
7054 BFD_ASSERT (r_symndx != 0);
7058 /* Adjust the addend according to where the
7059 section winds up in the output section. */
7061 irela->r_addend += sec->output_offset;
7065 if (finfo->indices[r_symndx] == -1)
7067 unsigned long shlink;
7071 if (finfo->info->strip == strip_all)
7073 /* You can't do ld -r -s. */
7074 bfd_set_error (bfd_error_invalid_operation);
7078 /* This symbol was skipped earlier, but
7079 since it is needed by a reloc, we
7080 must output it now. */
7081 shlink = symtab_hdr->sh_link;
7082 name = (bfd_elf_string_from_elf_section
7083 (input_bfd, shlink, sym.st_name));
7087 osec = sec->output_section;
7089 _bfd_elf_section_from_bfd_section (output_bfd,
7091 if (sym.st_shndx == SHN_BAD)
7094 sym.st_value += sec->output_offset;
7095 if (! finfo->info->relocatable)
7097 sym.st_value += osec->vma;
7098 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7100 /* STT_TLS symbols are relative to PT_TLS
7102 BFD_ASSERT (elf_hash_table (finfo->info)
7104 sym.st_value -= (elf_hash_table (finfo->info)
7109 finfo->indices[r_symndx]
7110 = bfd_get_symcount (output_bfd);
7112 if (! elf_link_output_sym (finfo, name, &sym, sec,
7117 r_symndx = finfo->indices[r_symndx];
7120 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7121 | (irela->r_info & r_type_mask));
7124 /* Swap out the relocs. */
7125 if (bed->elf_backend_emit_relocs
7126 && !(finfo->info->relocatable
7127 || finfo->info->emitrelocations))
7128 reloc_emitter = bed->elf_backend_emit_relocs;
7130 reloc_emitter = _bfd_elf_link_output_relocs;
7132 if (input_rel_hdr->sh_size != 0
7133 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
7137 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7138 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7140 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7141 * bed->s->int_rels_per_ext_rel);
7142 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
7149 /* Write out the modified section contents. */
7150 if (bed->elf_backend_write_section
7151 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7153 /* Section written out. */
7155 else switch (o->sec_info_type)
7157 case ELF_INFO_TYPE_STABS:
7158 if (! (_bfd_write_section_stabs
7160 &elf_hash_table (finfo->info)->stab_info,
7161 o, &elf_section_data (o)->sec_info, contents)))
7164 case ELF_INFO_TYPE_MERGE:
7165 if (! _bfd_write_merged_section (output_bfd, o,
7166 elf_section_data (o)->sec_info))
7169 case ELF_INFO_TYPE_EH_FRAME:
7171 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7178 if (! (o->flags & SEC_EXCLUDE)
7179 && ! bfd_set_section_contents (output_bfd, o->output_section,
7181 (file_ptr) o->output_offset,
7192 /* Generate a reloc when linking an ELF file. This is a reloc
7193 requested by the linker, and does come from any input file. This
7194 is used to build constructor and destructor tables when linking
7198 elf_reloc_link_order (bfd *output_bfd,
7199 struct bfd_link_info *info,
7200 asection *output_section,
7201 struct bfd_link_order *link_order)
7203 reloc_howto_type *howto;
7207 struct elf_link_hash_entry **rel_hash_ptr;
7208 Elf_Internal_Shdr *rel_hdr;
7209 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7210 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7214 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7217 bfd_set_error (bfd_error_bad_value);
7221 addend = link_order->u.reloc.p->addend;
7223 /* Figure out the symbol index. */
7224 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7225 + elf_section_data (output_section)->rel_count
7226 + elf_section_data (output_section)->rel_count2);
7227 if (link_order->type == bfd_section_reloc_link_order)
7229 indx = link_order->u.reloc.p->u.section->target_index;
7230 BFD_ASSERT (indx != 0);
7231 *rel_hash_ptr = NULL;
7235 struct elf_link_hash_entry *h;
7237 /* Treat a reloc against a defined symbol as though it were
7238 actually against the section. */
7239 h = ((struct elf_link_hash_entry *)
7240 bfd_wrapped_link_hash_lookup (output_bfd, info,
7241 link_order->u.reloc.p->u.name,
7242 FALSE, FALSE, TRUE));
7244 && (h->root.type == bfd_link_hash_defined
7245 || h->root.type == bfd_link_hash_defweak))
7249 section = h->root.u.def.section;
7250 indx = section->output_section->target_index;
7251 *rel_hash_ptr = NULL;
7252 /* It seems that we ought to add the symbol value to the
7253 addend here, but in practice it has already been added
7254 because it was passed to constructor_callback. */
7255 addend += section->output_section->vma + section->output_offset;
7259 /* Setting the index to -2 tells elf_link_output_extsym that
7260 this symbol is used by a reloc. */
7267 if (! ((*info->callbacks->unattached_reloc)
7268 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7274 /* If this is an inplace reloc, we must write the addend into the
7276 if (howto->partial_inplace && addend != 0)
7279 bfd_reloc_status_type rstat;
7282 const char *sym_name;
7284 size = bfd_get_reloc_size (howto);
7285 buf = bfd_zmalloc (size);
7288 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7295 case bfd_reloc_outofrange:
7298 case bfd_reloc_overflow:
7299 if (link_order->type == bfd_section_reloc_link_order)
7300 sym_name = bfd_section_name (output_bfd,
7301 link_order->u.reloc.p->u.section);
7303 sym_name = link_order->u.reloc.p->u.name;
7304 if (! ((*info->callbacks->reloc_overflow)
7305 (info, NULL, sym_name, howto->name, addend, NULL,
7306 NULL, (bfd_vma) 0)))
7313 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7314 link_order->offset, size);
7320 /* The address of a reloc is relative to the section in a
7321 relocatable file, and is a virtual address in an executable
7323 offset = link_order->offset;
7324 if (! info->relocatable)
7325 offset += output_section->vma;
7327 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7329 irel[i].r_offset = offset;
7331 irel[i].r_addend = 0;
7333 if (bed->s->arch_size == 32)
7334 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7336 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7338 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7339 erel = rel_hdr->contents;
7340 if (rel_hdr->sh_type == SHT_REL)
7342 erel += (elf_section_data (output_section)->rel_count
7343 * bed->s->sizeof_rel);
7344 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7348 irel[0].r_addend = addend;
7349 erel += (elf_section_data (output_section)->rel_count
7350 * bed->s->sizeof_rela);
7351 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7354 ++elf_section_data (output_section)->rel_count;
7360 /* Get the output vma of the section pointed to by the sh_link field. */
7363 elf_get_linked_section_vma (struct bfd_link_order *p)
7365 Elf_Internal_Shdr **elf_shdrp;
7369 s = p->u.indirect.section;
7370 elf_shdrp = elf_elfsections (s->owner);
7371 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7372 elfsec = elf_shdrp[elfsec]->sh_link;
7374 The Intel C compiler generates SHT_IA_64_UNWIND with
7375 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7376 sh_info fields. Hence we could get the situation
7377 where elfsec is 0. */
7380 const struct elf_backend_data *bed
7381 = get_elf_backend_data (s->owner);
7382 if (bed->link_order_error_handler)
7383 bed->link_order_error_handler
7384 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7389 s = elf_shdrp[elfsec]->bfd_section;
7390 return s->output_section->vma + s->output_offset;
7395 /* Compare two sections based on the locations of the sections they are
7396 linked to. Used by elf_fixup_link_order. */
7399 compare_link_order (const void * a, const void * b)
7404 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7405 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7412 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7413 order as their linked sections. Returns false if this could not be done
7414 because an output section includes both ordered and unordered
7415 sections. Ideally we'd do this in the linker proper. */
7418 elf_fixup_link_order (bfd *abfd, asection *o)
7423 struct bfd_link_order *p;
7425 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7427 struct bfd_link_order **sections;
7433 for (p = o->link_order_head; p != NULL; p = p->next)
7435 if (p->type == bfd_indirect_link_order
7436 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7437 == bfd_target_elf_flavour)
7438 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7440 s = p->u.indirect.section;
7441 elfsec = _bfd_elf_section_from_bfd_section (sub, s);
7443 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7452 if (!seen_linkorder)
7455 if (seen_other && seen_linkorder)
7457 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7459 bfd_set_error (bfd_error_bad_value);
7463 sections = (struct bfd_link_order **)
7464 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7467 for (p = o->link_order_head; p != NULL; p = p->next)
7469 sections[seen_linkorder++] = p;
7471 /* Sort the input sections in the order of their linked section. */
7472 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7473 compare_link_order);
7475 /* Change the offsets of the sections. */
7477 for (n = 0; n < seen_linkorder; n++)
7479 s = sections[n]->u.indirect.section;
7480 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7481 s->output_offset = offset;
7482 sections[n]->offset = offset;
7483 offset += sections[n]->size;
7490 /* Do the final step of an ELF link. */
7493 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7495 bfd_boolean dynamic;
7496 bfd_boolean emit_relocs;
7498 struct elf_final_link_info finfo;
7499 register asection *o;
7500 register struct bfd_link_order *p;
7502 bfd_size_type max_contents_size;
7503 bfd_size_type max_external_reloc_size;
7504 bfd_size_type max_internal_reloc_count;
7505 bfd_size_type max_sym_count;
7506 bfd_size_type max_sym_shndx_count;
7508 Elf_Internal_Sym elfsym;
7510 Elf_Internal_Shdr *symtab_hdr;
7511 Elf_Internal_Shdr *symtab_shndx_hdr;
7512 Elf_Internal_Shdr *symstrtab_hdr;
7513 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7514 struct elf_outext_info eoinfo;
7516 size_t relativecount = 0;
7517 asection *reldyn = 0;
7520 if (! is_elf_hash_table (info->hash))
7524 abfd->flags |= DYNAMIC;
7526 dynamic = elf_hash_table (info)->dynamic_sections_created;
7527 dynobj = elf_hash_table (info)->dynobj;
7529 emit_relocs = (info->relocatable
7530 || info->emitrelocations
7531 || bed->elf_backend_emit_relocs);
7534 finfo.output_bfd = abfd;
7535 finfo.symstrtab = _bfd_elf_stringtab_init ();
7536 if (finfo.symstrtab == NULL)
7541 finfo.dynsym_sec = NULL;
7542 finfo.hash_sec = NULL;
7543 finfo.symver_sec = NULL;
7547 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7548 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7549 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7550 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7551 /* Note that it is OK if symver_sec is NULL. */
7554 finfo.contents = NULL;
7555 finfo.external_relocs = NULL;
7556 finfo.internal_relocs = NULL;
7557 finfo.external_syms = NULL;
7558 finfo.locsym_shndx = NULL;
7559 finfo.internal_syms = NULL;
7560 finfo.indices = NULL;
7561 finfo.sections = NULL;
7562 finfo.symbuf = NULL;
7563 finfo.symshndxbuf = NULL;
7564 finfo.symbuf_count = 0;
7565 finfo.shndxbuf_size = 0;
7567 /* Count up the number of relocations we will output for each output
7568 section, so that we know the sizes of the reloc sections. We
7569 also figure out some maximum sizes. */
7570 max_contents_size = 0;
7571 max_external_reloc_size = 0;
7572 max_internal_reloc_count = 0;
7574 max_sym_shndx_count = 0;
7576 for (o = abfd->sections; o != NULL; o = o->next)
7578 struct bfd_elf_section_data *esdo = elf_section_data (o);
7581 for (p = o->link_order_head; p != NULL; p = p->next)
7583 unsigned int reloc_count = 0;
7584 struct bfd_elf_section_data *esdi = NULL;
7585 unsigned int *rel_count1;
7587 if (p->type == bfd_section_reloc_link_order
7588 || p->type == bfd_symbol_reloc_link_order)
7590 else if (p->type == bfd_indirect_link_order)
7594 sec = p->u.indirect.section;
7595 esdi = elf_section_data (sec);
7597 /* Mark all sections which are to be included in the
7598 link. This will normally be every section. We need
7599 to do this so that we can identify any sections which
7600 the linker has decided to not include. */
7601 sec->linker_mark = TRUE;
7603 if (sec->flags & SEC_MERGE)
7606 if (info->relocatable || info->emitrelocations)
7607 reloc_count = sec->reloc_count;
7608 else if (bed->elf_backend_count_relocs)
7610 Elf_Internal_Rela * relocs;
7612 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7615 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7617 if (elf_section_data (o)->relocs != relocs)
7621 if (sec->rawsize > max_contents_size)
7622 max_contents_size = sec->rawsize;
7623 if (sec->size > max_contents_size)
7624 max_contents_size = sec->size;
7626 /* We are interested in just local symbols, not all
7628 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7629 && (sec->owner->flags & DYNAMIC) == 0)
7633 if (elf_bad_symtab (sec->owner))
7634 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7635 / bed->s->sizeof_sym);
7637 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7639 if (sym_count > max_sym_count)
7640 max_sym_count = sym_count;
7642 if (sym_count > max_sym_shndx_count
7643 && elf_symtab_shndx (sec->owner) != 0)
7644 max_sym_shndx_count = sym_count;
7646 if ((sec->flags & SEC_RELOC) != 0)
7650 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7651 if (ext_size > max_external_reloc_size)
7652 max_external_reloc_size = ext_size;
7653 if (sec->reloc_count > max_internal_reloc_count)
7654 max_internal_reloc_count = sec->reloc_count;
7659 if (reloc_count == 0)
7662 o->reloc_count += reloc_count;
7664 /* MIPS may have a mix of REL and RELA relocs on sections.
7665 To support this curious ABI we keep reloc counts in
7666 elf_section_data too. We must be careful to add the
7667 relocations from the input section to the right output
7668 count. FIXME: Get rid of one count. We have
7669 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7670 rel_count1 = &esdo->rel_count;
7673 bfd_boolean same_size;
7674 bfd_size_type entsize1;
7676 entsize1 = esdi->rel_hdr.sh_entsize;
7677 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7678 || entsize1 == bed->s->sizeof_rela);
7679 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7682 rel_count1 = &esdo->rel_count2;
7684 if (esdi->rel_hdr2 != NULL)
7686 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7687 unsigned int alt_count;
7688 unsigned int *rel_count2;
7690 BFD_ASSERT (entsize2 != entsize1
7691 && (entsize2 == bed->s->sizeof_rel
7692 || entsize2 == bed->s->sizeof_rela));
7694 rel_count2 = &esdo->rel_count2;
7696 rel_count2 = &esdo->rel_count;
7698 /* The following is probably too simplistic if the
7699 backend counts output relocs unusually. */
7700 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7701 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7702 *rel_count2 += alt_count;
7703 reloc_count -= alt_count;
7706 *rel_count1 += reloc_count;
7709 if (o->reloc_count > 0)
7710 o->flags |= SEC_RELOC;
7713 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7714 set it (this is probably a bug) and if it is set
7715 assign_section_numbers will create a reloc section. */
7716 o->flags &=~ SEC_RELOC;
7719 /* If the SEC_ALLOC flag is not set, force the section VMA to
7720 zero. This is done in elf_fake_sections as well, but forcing
7721 the VMA to 0 here will ensure that relocs against these
7722 sections are handled correctly. */
7723 if ((o->flags & SEC_ALLOC) == 0
7724 && ! o->user_set_vma)
7728 if (! info->relocatable && merged)
7729 elf_link_hash_traverse (elf_hash_table (info),
7730 _bfd_elf_link_sec_merge_syms, abfd);
7732 /* Figure out the file positions for everything but the symbol table
7733 and the relocs. We set symcount to force assign_section_numbers
7734 to create a symbol table. */
7735 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7736 BFD_ASSERT (! abfd->output_has_begun);
7737 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7740 /* Set sizes, and assign file positions for reloc sections. */
7741 for (o = abfd->sections; o != NULL; o = o->next)
7743 if ((o->flags & SEC_RELOC) != 0)
7745 if (!(_bfd_elf_link_size_reloc_section
7746 (abfd, &elf_section_data (o)->rel_hdr, o)))
7749 if (elf_section_data (o)->rel_hdr2
7750 && !(_bfd_elf_link_size_reloc_section
7751 (abfd, elf_section_data (o)->rel_hdr2, o)))
7755 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7756 to count upwards while actually outputting the relocations. */
7757 elf_section_data (o)->rel_count = 0;
7758 elf_section_data (o)->rel_count2 = 0;
7761 _bfd_elf_assign_file_positions_for_relocs (abfd);
7763 /* We have now assigned file positions for all the sections except
7764 .symtab and .strtab. We start the .symtab section at the current
7765 file position, and write directly to it. We build the .strtab
7766 section in memory. */
7767 bfd_get_symcount (abfd) = 0;
7768 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7769 /* sh_name is set in prep_headers. */
7770 symtab_hdr->sh_type = SHT_SYMTAB;
7771 /* sh_flags, sh_addr and sh_size all start off zero. */
7772 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7773 /* sh_link is set in assign_section_numbers. */
7774 /* sh_info is set below. */
7775 /* sh_offset is set just below. */
7776 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
7778 off = elf_tdata (abfd)->next_file_pos;
7779 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
7781 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7782 incorrect. We do not yet know the size of the .symtab section.
7783 We correct next_file_pos below, after we do know the size. */
7785 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7786 continuously seeking to the right position in the file. */
7787 if (! info->keep_memory || max_sym_count < 20)
7788 finfo.symbuf_size = 20;
7790 finfo.symbuf_size = max_sym_count;
7791 amt = finfo.symbuf_size;
7792 amt *= bed->s->sizeof_sym;
7793 finfo.symbuf = bfd_malloc (amt);
7794 if (finfo.symbuf == NULL)
7796 if (elf_numsections (abfd) > SHN_LORESERVE)
7798 /* Wild guess at number of output symbols. realloc'd as needed. */
7799 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
7800 finfo.shndxbuf_size = amt;
7801 amt *= sizeof (Elf_External_Sym_Shndx);
7802 finfo.symshndxbuf = bfd_zmalloc (amt);
7803 if (finfo.symshndxbuf == NULL)
7807 /* Start writing out the symbol table. The first symbol is always a
7809 if (info->strip != strip_all
7812 elfsym.st_value = 0;
7815 elfsym.st_other = 0;
7816 elfsym.st_shndx = SHN_UNDEF;
7817 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
7823 /* Some standard ELF linkers do this, but we don't because it causes
7824 bootstrap comparison failures. */
7825 /* Output a file symbol for the output file as the second symbol.
7826 We output this even if we are discarding local symbols, although
7827 I'm not sure if this is correct. */
7828 elfsym.st_value = 0;
7830 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7831 elfsym.st_other = 0;
7832 elfsym.st_shndx = SHN_ABS;
7833 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
7834 &elfsym, bfd_abs_section_ptr, NULL))
7838 /* Output a symbol for each section. We output these even if we are
7839 discarding local symbols, since they are used for relocs. These
7840 symbols have no names. We store the index of each one in the
7841 index field of the section, so that we can find it again when
7842 outputting relocs. */
7843 if (info->strip != strip_all
7847 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7848 elfsym.st_other = 0;
7849 for (i = 1; i < elf_numsections (abfd); i++)
7851 o = bfd_section_from_elf_index (abfd, i);
7853 o->target_index = bfd_get_symcount (abfd);
7854 elfsym.st_shndx = i;
7855 if (info->relocatable || o == NULL)
7856 elfsym.st_value = 0;
7858 elfsym.st_value = o->vma;
7859 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
7861 if (i == SHN_LORESERVE - 1)
7862 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
7866 /* Allocate some memory to hold information read in from the input
7868 if (max_contents_size != 0)
7870 finfo.contents = bfd_malloc (max_contents_size);
7871 if (finfo.contents == NULL)
7875 if (max_external_reloc_size != 0)
7877 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
7878 if (finfo.external_relocs == NULL)
7882 if (max_internal_reloc_count != 0)
7884 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
7885 amt *= sizeof (Elf_Internal_Rela);
7886 finfo.internal_relocs = bfd_malloc (amt);
7887 if (finfo.internal_relocs == NULL)
7891 if (max_sym_count != 0)
7893 amt = max_sym_count * bed->s->sizeof_sym;
7894 finfo.external_syms = bfd_malloc (amt);
7895 if (finfo.external_syms == NULL)
7898 amt = max_sym_count * sizeof (Elf_Internal_Sym);
7899 finfo.internal_syms = bfd_malloc (amt);
7900 if (finfo.internal_syms == NULL)
7903 amt = max_sym_count * sizeof (long);
7904 finfo.indices = bfd_malloc (amt);
7905 if (finfo.indices == NULL)
7908 amt = max_sym_count * sizeof (asection *);
7909 finfo.sections = bfd_malloc (amt);
7910 if (finfo.sections == NULL)
7914 if (max_sym_shndx_count != 0)
7916 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
7917 finfo.locsym_shndx = bfd_malloc (amt);
7918 if (finfo.locsym_shndx == NULL)
7922 if (elf_hash_table (info)->tls_sec)
7924 bfd_vma base, end = 0;
7927 for (sec = elf_hash_table (info)->tls_sec;
7928 sec && (sec->flags & SEC_THREAD_LOCAL);
7931 bfd_vma size = sec->size;
7933 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
7935 struct bfd_link_order *o;
7937 for (o = sec->link_order_head; o != NULL; o = o->next)
7938 if (size < o->offset + o->size)
7939 size = o->offset + o->size;
7941 end = sec->vma + size;
7943 base = elf_hash_table (info)->tls_sec->vma;
7944 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
7945 elf_hash_table (info)->tls_size = end - base;
7948 /* Reorder SHF_LINK_ORDER sections. */
7949 for (o = abfd->sections; o != NULL; o = o->next)
7951 if (!elf_fixup_link_order (abfd, o))
7955 /* Since ELF permits relocations to be against local symbols, we
7956 must have the local symbols available when we do the relocations.
7957 Since we would rather only read the local symbols once, and we
7958 would rather not keep them in memory, we handle all the
7959 relocations for a single input file at the same time.
7961 Unfortunately, there is no way to know the total number of local
7962 symbols until we have seen all of them, and the local symbol
7963 indices precede the global symbol indices. This means that when
7964 we are generating relocatable output, and we see a reloc against
7965 a global symbol, we can not know the symbol index until we have
7966 finished examining all the local symbols to see which ones we are
7967 going to output. To deal with this, we keep the relocations in
7968 memory, and don't output them until the end of the link. This is
7969 an unfortunate waste of memory, but I don't see a good way around
7970 it. Fortunately, it only happens when performing a relocatable
7971 link, which is not the common case. FIXME: If keep_memory is set
7972 we could write the relocs out and then read them again; I don't
7973 know how bad the memory loss will be. */
7975 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7976 sub->output_has_begun = FALSE;
7977 for (o = abfd->sections; o != NULL; o = o->next)
7979 for (p = o->link_order_head; p != NULL; p = p->next)
7981 if (p->type == bfd_indirect_link_order
7982 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7983 == bfd_target_elf_flavour)
7984 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7986 if (! sub->output_has_begun)
7988 if (! elf_link_input_bfd (&finfo, sub))
7990 sub->output_has_begun = TRUE;
7993 else if (p->type == bfd_section_reloc_link_order
7994 || p->type == bfd_symbol_reloc_link_order)
7996 if (! elf_reloc_link_order (abfd, info, o, p))
8001 if (! _bfd_default_link_order (abfd, info, o, p))
8007 /* Output any global symbols that got converted to local in a
8008 version script or due to symbol visibility. We do this in a
8009 separate step since ELF requires all local symbols to appear
8010 prior to any global symbols. FIXME: We should only do this if
8011 some global symbols were, in fact, converted to become local.
8012 FIXME: Will this work correctly with the Irix 5 linker? */
8013 eoinfo.failed = FALSE;
8014 eoinfo.finfo = &finfo;
8015 eoinfo.localsyms = TRUE;
8016 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8021 /* That wrote out all the local symbols. Finish up the symbol table
8022 with the global symbols. Even if we want to strip everything we
8023 can, we still need to deal with those global symbols that got
8024 converted to local in a version script. */
8026 /* The sh_info field records the index of the first non local symbol. */
8027 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8030 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8032 Elf_Internal_Sym sym;
8033 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8034 long last_local = 0;
8036 /* Write out the section symbols for the output sections. */
8043 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8046 for (s = abfd->sections; s != NULL; s = s->next)
8052 dynindx = elf_section_data (s)->dynindx;
8055 indx = elf_section_data (s)->this_idx;
8056 BFD_ASSERT (indx > 0);
8057 sym.st_shndx = indx;
8058 sym.st_value = s->vma;
8059 dest = dynsym + dynindx * bed->s->sizeof_sym;
8060 if (last_local < dynindx)
8061 last_local = dynindx;
8062 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8066 /* Write out the local dynsyms. */
8067 if (elf_hash_table (info)->dynlocal)
8069 struct elf_link_local_dynamic_entry *e;
8070 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8075 sym.st_size = e->isym.st_size;
8076 sym.st_other = e->isym.st_other;
8078 /* Copy the internal symbol as is.
8079 Note that we saved a word of storage and overwrote
8080 the original st_name with the dynstr_index. */
8083 if (e->isym.st_shndx != SHN_UNDEF
8084 && (e->isym.st_shndx < SHN_LORESERVE
8085 || e->isym.st_shndx > SHN_HIRESERVE))
8087 s = bfd_section_from_elf_index (e->input_bfd,
8091 elf_section_data (s->output_section)->this_idx;
8092 sym.st_value = (s->output_section->vma
8094 + e->isym.st_value);
8097 if (last_local < e->dynindx)
8098 last_local = e->dynindx;
8100 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8101 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8105 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8109 /* We get the global symbols from the hash table. */
8110 eoinfo.failed = FALSE;
8111 eoinfo.localsyms = FALSE;
8112 eoinfo.finfo = &finfo;
8113 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8118 /* If backend needs to output some symbols not present in the hash
8119 table, do it now. */
8120 if (bed->elf_backend_output_arch_syms)
8122 typedef bfd_boolean (*out_sym_func)
8123 (void *, const char *, Elf_Internal_Sym *, asection *,
8124 struct elf_link_hash_entry *);
8126 if (! ((*bed->elf_backend_output_arch_syms)
8127 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8131 /* Flush all symbols to the file. */
8132 if (! elf_link_flush_output_syms (&finfo, bed))
8135 /* Now we know the size of the symtab section. */
8136 off += symtab_hdr->sh_size;
8138 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8139 if (symtab_shndx_hdr->sh_name != 0)
8141 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8142 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8143 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8144 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8145 symtab_shndx_hdr->sh_size = amt;
8147 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8150 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8151 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8156 /* Finish up and write out the symbol string table (.strtab)
8158 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8159 /* sh_name was set in prep_headers. */
8160 symstrtab_hdr->sh_type = SHT_STRTAB;
8161 symstrtab_hdr->sh_flags = 0;
8162 symstrtab_hdr->sh_addr = 0;
8163 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8164 symstrtab_hdr->sh_entsize = 0;
8165 symstrtab_hdr->sh_link = 0;
8166 symstrtab_hdr->sh_info = 0;
8167 /* sh_offset is set just below. */
8168 symstrtab_hdr->sh_addralign = 1;
8170 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8171 elf_tdata (abfd)->next_file_pos = off;
8173 if (bfd_get_symcount (abfd) > 0)
8175 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8176 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8180 /* Adjust the relocs to have the correct symbol indices. */
8181 for (o = abfd->sections; o != NULL; o = o->next)
8183 if ((o->flags & SEC_RELOC) == 0)
8186 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8187 elf_section_data (o)->rel_count,
8188 elf_section_data (o)->rel_hashes);
8189 if (elf_section_data (o)->rel_hdr2 != NULL)
8190 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8191 elf_section_data (o)->rel_count2,
8192 (elf_section_data (o)->rel_hashes
8193 + elf_section_data (o)->rel_count));
8195 /* Set the reloc_count field to 0 to prevent write_relocs from
8196 trying to swap the relocs out itself. */
8200 if (dynamic && info->combreloc && dynobj != NULL)
8201 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8203 /* If we are linking against a dynamic object, or generating a
8204 shared library, finish up the dynamic linking information. */
8207 bfd_byte *dyncon, *dynconend;
8209 /* Fix up .dynamic entries. */
8210 o = bfd_get_section_by_name (dynobj, ".dynamic");
8211 BFD_ASSERT (o != NULL);
8213 dyncon = o->contents;
8214 dynconend = o->contents + o->size;
8215 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8217 Elf_Internal_Dyn dyn;
8221 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8228 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8230 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8232 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8233 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8236 dyn.d_un.d_val = relativecount;
8243 name = info->init_function;
8246 name = info->fini_function;
8249 struct elf_link_hash_entry *h;
8251 h = elf_link_hash_lookup (elf_hash_table (info), name,
8252 FALSE, FALSE, TRUE);
8254 && (h->root.type == bfd_link_hash_defined
8255 || h->root.type == bfd_link_hash_defweak))
8257 dyn.d_un.d_val = h->root.u.def.value;
8258 o = h->root.u.def.section;
8259 if (o->output_section != NULL)
8260 dyn.d_un.d_val += (o->output_section->vma
8261 + o->output_offset);
8264 /* The symbol is imported from another shared
8265 library and does not apply to this one. */
8273 case DT_PREINIT_ARRAYSZ:
8274 name = ".preinit_array";
8276 case DT_INIT_ARRAYSZ:
8277 name = ".init_array";
8279 case DT_FINI_ARRAYSZ:
8280 name = ".fini_array";
8282 o = bfd_get_section_by_name (abfd, name);
8285 (*_bfd_error_handler)
8286 (_("%B: could not find output section %s"), abfd, name);
8290 (*_bfd_error_handler)
8291 (_("warning: %s section has zero size"), name);
8292 dyn.d_un.d_val = o->size;
8295 case DT_PREINIT_ARRAY:
8296 name = ".preinit_array";
8299 name = ".init_array";
8302 name = ".fini_array";
8315 name = ".gnu.version_d";
8318 name = ".gnu.version_r";
8321 name = ".gnu.version";
8323 o = bfd_get_section_by_name (abfd, name);
8326 (*_bfd_error_handler)
8327 (_("%B: could not find output section %s"), abfd, name);
8330 dyn.d_un.d_ptr = o->vma;
8337 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8342 for (i = 1; i < elf_numsections (abfd); i++)
8344 Elf_Internal_Shdr *hdr;
8346 hdr = elf_elfsections (abfd)[i];
8347 if (hdr->sh_type == type
8348 && (hdr->sh_flags & SHF_ALLOC) != 0)
8350 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8351 dyn.d_un.d_val += hdr->sh_size;
8354 if (dyn.d_un.d_val == 0
8355 || hdr->sh_addr < dyn.d_un.d_val)
8356 dyn.d_un.d_val = hdr->sh_addr;
8362 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8366 /* If we have created any dynamic sections, then output them. */
8369 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8372 for (o = dynobj->sections; o != NULL; o = o->next)
8374 if ((o->flags & SEC_HAS_CONTENTS) == 0
8376 || o->output_section == bfd_abs_section_ptr)
8378 if ((o->flags & SEC_LINKER_CREATED) == 0)
8380 /* At this point, we are only interested in sections
8381 created by _bfd_elf_link_create_dynamic_sections. */
8384 if (elf_hash_table (info)->stab_info.stabstr == o)
8386 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8388 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8390 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8392 if (! bfd_set_section_contents (abfd, o->output_section,
8394 (file_ptr) o->output_offset,
8400 /* The contents of the .dynstr section are actually in a
8402 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8403 if (bfd_seek (abfd, off, SEEK_SET) != 0
8404 || ! _bfd_elf_strtab_emit (abfd,
8405 elf_hash_table (info)->dynstr))
8411 if (info->relocatable)
8413 bfd_boolean failed = FALSE;
8415 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8420 /* If we have optimized stabs strings, output them. */
8421 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8423 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8427 if (info->eh_frame_hdr)
8429 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8433 if (finfo.symstrtab != NULL)
8434 _bfd_stringtab_free (finfo.symstrtab);
8435 if (finfo.contents != NULL)
8436 free (finfo.contents);
8437 if (finfo.external_relocs != NULL)
8438 free (finfo.external_relocs);
8439 if (finfo.internal_relocs != NULL)
8440 free (finfo.internal_relocs);
8441 if (finfo.external_syms != NULL)
8442 free (finfo.external_syms);
8443 if (finfo.locsym_shndx != NULL)
8444 free (finfo.locsym_shndx);
8445 if (finfo.internal_syms != NULL)
8446 free (finfo.internal_syms);
8447 if (finfo.indices != NULL)
8448 free (finfo.indices);
8449 if (finfo.sections != NULL)
8450 free (finfo.sections);
8451 if (finfo.symbuf != NULL)
8452 free (finfo.symbuf);
8453 if (finfo.symshndxbuf != NULL)
8454 free (finfo.symshndxbuf);
8455 for (o = abfd->sections; o != NULL; o = o->next)
8457 if ((o->flags & SEC_RELOC) != 0
8458 && elf_section_data (o)->rel_hashes != NULL)
8459 free (elf_section_data (o)->rel_hashes);
8462 elf_tdata (abfd)->linker = TRUE;
8467 if (finfo.symstrtab != NULL)
8468 _bfd_stringtab_free (finfo.symstrtab);
8469 if (finfo.contents != NULL)
8470 free (finfo.contents);
8471 if (finfo.external_relocs != NULL)
8472 free (finfo.external_relocs);
8473 if (finfo.internal_relocs != NULL)
8474 free (finfo.internal_relocs);
8475 if (finfo.external_syms != NULL)
8476 free (finfo.external_syms);
8477 if (finfo.locsym_shndx != NULL)
8478 free (finfo.locsym_shndx);
8479 if (finfo.internal_syms != NULL)
8480 free (finfo.internal_syms);
8481 if (finfo.indices != NULL)
8482 free (finfo.indices);
8483 if (finfo.sections != NULL)
8484 free (finfo.sections);
8485 if (finfo.symbuf != NULL)
8486 free (finfo.symbuf);
8487 if (finfo.symshndxbuf != NULL)
8488 free (finfo.symshndxbuf);
8489 for (o = abfd->sections; o != NULL; o = o->next)
8491 if ((o->flags & SEC_RELOC) != 0
8492 && elf_section_data (o)->rel_hashes != NULL)
8493 free (elf_section_data (o)->rel_hashes);
8499 /* Garbage collect unused sections. */
8501 /* The mark phase of garbage collection. For a given section, mark
8502 it and any sections in this section's group, and all the sections
8503 which define symbols to which it refers. */
8505 typedef asection * (*gc_mark_hook_fn)
8506 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8507 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8510 _bfd_elf_gc_mark (struct bfd_link_info *info,
8512 gc_mark_hook_fn gc_mark_hook)
8515 asection *group_sec;
8519 /* Mark all the sections in the group. */
8520 group_sec = elf_section_data (sec)->next_in_group;
8521 if (group_sec && !group_sec->gc_mark)
8522 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8525 /* Look through the section relocs. */
8527 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8529 Elf_Internal_Rela *relstart, *rel, *relend;
8530 Elf_Internal_Shdr *symtab_hdr;
8531 struct elf_link_hash_entry **sym_hashes;
8534 bfd *input_bfd = sec->owner;
8535 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8536 Elf_Internal_Sym *isym = NULL;
8539 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8540 sym_hashes = elf_sym_hashes (input_bfd);
8542 /* Read the local symbols. */
8543 if (elf_bad_symtab (input_bfd))
8545 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8549 extsymoff = nlocsyms = symtab_hdr->sh_info;
8551 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8552 if (isym == NULL && nlocsyms != 0)
8554 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8560 /* Read the relocations. */
8561 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8563 if (relstart == NULL)
8568 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8570 if (bed->s->arch_size == 32)
8575 for (rel = relstart; rel < relend; rel++)
8577 unsigned long r_symndx;
8579 struct elf_link_hash_entry *h;
8581 r_symndx = rel->r_info >> r_sym_shift;
8585 if (r_symndx >= nlocsyms
8586 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8588 h = sym_hashes[r_symndx - extsymoff];
8589 while (h->root.type == bfd_link_hash_indirect
8590 || h->root.type == bfd_link_hash_warning)
8591 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8592 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8596 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8599 if (rsec && !rsec->gc_mark)
8601 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8603 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8612 if (elf_section_data (sec)->relocs != relstart)
8615 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8617 if (! info->keep_memory)
8620 symtab_hdr->contents = (unsigned char *) isym;
8627 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8630 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8634 if (h->root.type == bfd_link_hash_warning)
8635 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8637 if (h->dynindx != -1
8638 && ((h->root.type != bfd_link_hash_defined
8639 && h->root.type != bfd_link_hash_defweak)
8640 || h->root.u.def.section->gc_mark))
8641 h->dynindx = (*idx)++;
8646 /* The sweep phase of garbage collection. Remove all garbage sections. */
8648 typedef bfd_boolean (*gc_sweep_hook_fn)
8649 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8652 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8656 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8660 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8663 for (o = sub->sections; o != NULL; o = o->next)
8665 /* Keep debug and special sections. */
8666 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8667 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8673 /* Skip sweeping sections already excluded. */
8674 if (o->flags & SEC_EXCLUDE)
8677 /* Since this is early in the link process, it is simple
8678 to remove a section from the output. */
8679 o->flags |= SEC_EXCLUDE;
8681 /* But we also have to update some of the relocation
8682 info we collected before. */
8684 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
8686 Elf_Internal_Rela *internal_relocs;
8690 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8692 if (internal_relocs == NULL)
8695 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8697 if (elf_section_data (o)->relocs != internal_relocs)
8698 free (internal_relocs);
8706 /* Remove the symbols that were in the swept sections from the dynamic
8707 symbol table. GCFIXME: Anyone know how to get them out of the
8708 static symbol table as well? */
8712 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8714 elf_hash_table (info)->dynsymcount = i;
8720 /* Propagate collected vtable information. This is called through
8721 elf_link_hash_traverse. */
8724 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8726 if (h->root.type == bfd_link_hash_warning)
8727 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8729 /* Those that are not vtables. */
8730 if (h->vtable == NULL || h->vtable->parent == NULL)
8733 /* Those vtables that do not have parents, we cannot merge. */
8734 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
8737 /* If we've already been done, exit. */
8738 if (h->vtable->used && h->vtable->used[-1])
8741 /* Make sure the parent's table is up to date. */
8742 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
8744 if (h->vtable->used == NULL)
8746 /* None of this table's entries were referenced. Re-use the
8748 h->vtable->used = h->vtable->parent->vtable->used;
8749 h->vtable->size = h->vtable->parent->vtable->size;
8754 bfd_boolean *cu, *pu;
8756 /* Or the parent's entries into ours. */
8757 cu = h->vtable->used;
8759 pu = h->vtable->parent->vtable->used;
8762 const struct elf_backend_data *bed;
8763 unsigned int log_file_align;
8765 bed = get_elf_backend_data (h->root.u.def.section->owner);
8766 log_file_align = bed->s->log_file_align;
8767 n = h->vtable->parent->vtable->size >> log_file_align;
8782 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
8785 bfd_vma hstart, hend;
8786 Elf_Internal_Rela *relstart, *relend, *rel;
8787 const struct elf_backend_data *bed;
8788 unsigned int log_file_align;
8790 if (h->root.type == bfd_link_hash_warning)
8791 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8793 /* Take care of both those symbols that do not describe vtables as
8794 well as those that are not loaded. */
8795 if (h->vtable == NULL || h->vtable->parent == NULL)
8798 BFD_ASSERT (h->root.type == bfd_link_hash_defined
8799 || h->root.type == bfd_link_hash_defweak);
8801 sec = h->root.u.def.section;
8802 hstart = h->root.u.def.value;
8803 hend = hstart + h->size;
8805 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
8807 return *(bfd_boolean *) okp = FALSE;
8808 bed = get_elf_backend_data (sec->owner);
8809 log_file_align = bed->s->log_file_align;
8811 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8813 for (rel = relstart; rel < relend; ++rel)
8814 if (rel->r_offset >= hstart && rel->r_offset < hend)
8816 /* If the entry is in use, do nothing. */
8818 && (rel->r_offset - hstart) < h->vtable->size)
8820 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
8821 if (h->vtable->used[entry])
8824 /* Otherwise, kill it. */
8825 rel->r_offset = rel->r_info = rel->r_addend = 0;
8831 /* Mark sections containing dynamically referenced symbols. This is called
8832 through elf_link_hash_traverse. */
8835 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h,
8836 void *okp ATTRIBUTE_UNUSED)
8838 if (h->root.type == bfd_link_hash_warning)
8839 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8841 if ((h->root.type == bfd_link_hash_defined
8842 || h->root.type == bfd_link_hash_defweak)
8844 h->root.u.def.section->flags |= SEC_KEEP;
8849 /* Do mark and sweep of unused sections. */
8852 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
8854 bfd_boolean ok = TRUE;
8856 asection * (*gc_mark_hook)
8857 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8858 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
8860 if (!get_elf_backend_data (abfd)->can_gc_sections
8861 || info->relocatable
8862 || info->emitrelocations
8864 || !is_elf_hash_table (info->hash))
8866 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
8870 /* Apply transitive closure to the vtable entry usage info. */
8871 elf_link_hash_traverse (elf_hash_table (info),
8872 elf_gc_propagate_vtable_entries_used,
8877 /* Kill the vtable relocations that were not used. */
8878 elf_link_hash_traverse (elf_hash_table (info),
8879 elf_gc_smash_unused_vtentry_relocs,
8884 /* Mark dynamically referenced symbols. */
8885 if (elf_hash_table (info)->dynamic_sections_created)
8886 elf_link_hash_traverse (elf_hash_table (info),
8887 elf_gc_mark_dynamic_ref_symbol,
8892 /* Grovel through relocs to find out who stays ... */
8893 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
8894 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8898 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8901 for (o = sub->sections; o != NULL; o = o->next)
8903 if (o->flags & SEC_KEEP)
8905 /* _bfd_elf_discard_section_eh_frame knows how to discard
8906 orphaned FDEs so don't mark sections referenced by the
8907 EH frame section. */
8908 if (strcmp (o->name, ".eh_frame") == 0)
8910 else if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
8916 /* ... and mark SEC_EXCLUDE for those that go. */
8917 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
8923 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8926 bfd_elf_gc_record_vtinherit (bfd *abfd,
8928 struct elf_link_hash_entry *h,
8931 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
8932 struct elf_link_hash_entry **search, *child;
8933 bfd_size_type extsymcount;
8934 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8936 /* The sh_info field of the symtab header tells us where the
8937 external symbols start. We don't care about the local symbols at
8939 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
8940 if (!elf_bad_symtab (abfd))
8941 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8943 sym_hashes = elf_sym_hashes (abfd);
8944 sym_hashes_end = sym_hashes + extsymcount;
8946 /* Hunt down the child symbol, which is in this section at the same
8947 offset as the relocation. */
8948 for (search = sym_hashes; search != sym_hashes_end; ++search)
8950 if ((child = *search) != NULL
8951 && (child->root.type == bfd_link_hash_defined
8952 || child->root.type == bfd_link_hash_defweak)
8953 && child->root.u.def.section == sec
8954 && child->root.u.def.value == offset)
8958 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
8959 abfd, sec, (unsigned long) offset);
8960 bfd_set_error (bfd_error_invalid_operation);
8966 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
8972 /* This *should* only be the absolute section. It could potentially
8973 be that someone has defined a non-global vtable though, which
8974 would be bad. It isn't worth paging in the local symbols to be
8975 sure though; that case should simply be handled by the assembler. */
8977 child->vtable->parent = (struct elf_link_hash_entry *) -1;
8980 child->vtable->parent = h;
8985 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8988 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
8989 asection *sec ATTRIBUTE_UNUSED,
8990 struct elf_link_hash_entry *h,
8993 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8994 unsigned int log_file_align = bed->s->log_file_align;
8998 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
9003 if (addend >= h->vtable->size)
9005 size_t size, bytes, file_align;
9006 bfd_boolean *ptr = h->vtable->used;
9008 /* While the symbol is undefined, we have to be prepared to handle
9010 file_align = 1 << log_file_align;
9011 if (h->root.type == bfd_link_hash_undefined)
9012 size = addend + file_align;
9018 /* Oops! We've got a reference past the defined end of
9019 the table. This is probably a bug -- shall we warn? */
9020 size = addend + file_align;
9023 size = (size + file_align - 1) & -file_align;
9025 /* Allocate one extra entry for use as a "done" flag for the
9026 consolidation pass. */
9027 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9031 ptr = bfd_realloc (ptr - 1, bytes);
9037 oldbytes = (((h->vtable->size >> log_file_align) + 1)
9038 * sizeof (bfd_boolean));
9039 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9043 ptr = bfd_zmalloc (bytes);
9048 /* And arrange for that done flag to be at index -1. */
9049 h->vtable->used = ptr + 1;
9050 h->vtable->size = size;
9053 h->vtable->used[addend >> log_file_align] = TRUE;
9058 struct alloc_got_off_arg {
9060 unsigned int got_elt_size;
9063 /* We need a special top-level link routine to convert got reference counts
9064 to real got offsets. */
9067 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9069 struct alloc_got_off_arg *gofarg = arg;
9071 if (h->root.type == bfd_link_hash_warning)
9072 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9074 if (h->got.refcount > 0)
9076 h->got.offset = gofarg->gotoff;
9077 gofarg->gotoff += gofarg->got_elt_size;
9080 h->got.offset = (bfd_vma) -1;
9085 /* And an accompanying bit to work out final got entry offsets once
9086 we're done. Should be called from final_link. */
9089 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9090 struct bfd_link_info *info)
9093 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9095 unsigned int got_elt_size = bed->s->arch_size / 8;
9096 struct alloc_got_off_arg gofarg;
9098 if (! is_elf_hash_table (info->hash))
9101 /* The GOT offset is relative to the .got section, but the GOT header is
9102 put into the .got.plt section, if the backend uses it. */
9103 if (bed->want_got_plt)
9106 gotoff = bed->got_header_size;
9108 /* Do the local .got entries first. */
9109 for (i = info->input_bfds; i; i = i->link_next)
9111 bfd_signed_vma *local_got;
9112 bfd_size_type j, locsymcount;
9113 Elf_Internal_Shdr *symtab_hdr;
9115 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9118 local_got = elf_local_got_refcounts (i);
9122 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9123 if (elf_bad_symtab (i))
9124 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9126 locsymcount = symtab_hdr->sh_info;
9128 for (j = 0; j < locsymcount; ++j)
9130 if (local_got[j] > 0)
9132 local_got[j] = gotoff;
9133 gotoff += got_elt_size;
9136 local_got[j] = (bfd_vma) -1;
9140 /* Then the global .got entries. .plt refcounts are handled by
9141 adjust_dynamic_symbol */
9142 gofarg.gotoff = gotoff;
9143 gofarg.got_elt_size = got_elt_size;
9144 elf_link_hash_traverse (elf_hash_table (info),
9145 elf_gc_allocate_got_offsets,
9150 /* Many folk need no more in the way of final link than this, once
9151 got entry reference counting is enabled. */
9154 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9156 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9159 /* Invoke the regular ELF backend linker to do all the work. */
9160 return bfd_elf_final_link (abfd, info);
9164 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9166 struct elf_reloc_cookie *rcookie = cookie;
9168 if (rcookie->bad_symtab)
9169 rcookie->rel = rcookie->rels;
9171 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9173 unsigned long r_symndx;
9175 if (! rcookie->bad_symtab)
9176 if (rcookie->rel->r_offset > offset)
9178 if (rcookie->rel->r_offset != offset)
9181 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9182 if (r_symndx == SHN_UNDEF)
9185 if (r_symndx >= rcookie->locsymcount
9186 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9188 struct elf_link_hash_entry *h;
9190 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9192 while (h->root.type == bfd_link_hash_indirect
9193 || h->root.type == bfd_link_hash_warning)
9194 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9196 if ((h->root.type == bfd_link_hash_defined
9197 || h->root.type == bfd_link_hash_defweak)
9198 && elf_discarded_section (h->root.u.def.section))
9205 /* It's not a relocation against a global symbol,
9206 but it could be a relocation against a local
9207 symbol for a discarded section. */
9209 Elf_Internal_Sym *isym;
9211 /* Need to: get the symbol; get the section. */
9212 isym = &rcookie->locsyms[r_symndx];
9213 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9215 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9216 if (isec != NULL && elf_discarded_section (isec))
9225 /* Discard unneeded references to discarded sections.
9226 Returns TRUE if any section's size was changed. */
9227 /* This function assumes that the relocations are in sorted order,
9228 which is true for all known assemblers. */
9231 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9233 struct elf_reloc_cookie cookie;
9234 asection *stab, *eh;
9235 Elf_Internal_Shdr *symtab_hdr;
9236 const struct elf_backend_data *bed;
9239 bfd_boolean ret = FALSE;
9241 if (info->traditional_format
9242 || !is_elf_hash_table (info->hash))
9245 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9247 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9250 bed = get_elf_backend_data (abfd);
9252 if ((abfd->flags & DYNAMIC) != 0)
9255 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9256 if (info->relocatable
9259 || bfd_is_abs_section (eh->output_section))))
9262 stab = bfd_get_section_by_name (abfd, ".stab");
9265 || bfd_is_abs_section (stab->output_section)
9266 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9271 && bed->elf_backend_discard_info == NULL)
9274 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9276 cookie.sym_hashes = elf_sym_hashes (abfd);
9277 cookie.bad_symtab = elf_bad_symtab (abfd);
9278 if (cookie.bad_symtab)
9280 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9281 cookie.extsymoff = 0;
9285 cookie.locsymcount = symtab_hdr->sh_info;
9286 cookie.extsymoff = symtab_hdr->sh_info;
9289 if (bed->s->arch_size == 32)
9290 cookie.r_sym_shift = 8;
9292 cookie.r_sym_shift = 32;
9294 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9295 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9297 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9298 cookie.locsymcount, 0,
9300 if (cookie.locsyms == NULL)
9307 count = stab->reloc_count;
9309 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9311 if (cookie.rels != NULL)
9313 cookie.rel = cookie.rels;
9314 cookie.relend = cookie.rels;
9315 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9316 if (_bfd_discard_section_stabs (abfd, stab,
9317 elf_section_data (stab)->sec_info,
9318 bfd_elf_reloc_symbol_deleted_p,
9321 if (elf_section_data (stab)->relocs != cookie.rels)
9329 count = eh->reloc_count;
9331 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9333 cookie.rel = cookie.rels;
9334 cookie.relend = cookie.rels;
9335 if (cookie.rels != NULL)
9336 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9338 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9339 bfd_elf_reloc_symbol_deleted_p,
9343 if (cookie.rels != NULL
9344 && elf_section_data (eh)->relocs != cookie.rels)
9348 if (bed->elf_backend_discard_info != NULL
9349 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9352 if (cookie.locsyms != NULL
9353 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9355 if (! info->keep_memory)
9356 free (cookie.locsyms);
9358 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9362 if (info->eh_frame_hdr
9363 && !info->relocatable
9364 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9371 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9374 const char *name, *p;
9375 struct bfd_section_already_linked *l;
9376 struct bfd_section_already_linked_hash_entry *already_linked_list;
9379 /* A single member comdat group section may be discarded by a
9380 linkonce section. See below. */
9381 if (sec->output_section == bfd_abs_section_ptr)
9386 /* Check if it belongs to a section group. */
9387 group = elf_sec_group (sec);
9389 /* Return if it isn't a linkonce section nor a member of a group. A
9390 comdat group section also has SEC_LINK_ONCE set. */
9391 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9396 /* If this is the member of a single member comdat group, check if
9397 the group should be discarded. */
9398 if (elf_next_in_group (sec) == sec
9399 && (group->flags & SEC_LINK_ONCE) != 0)
9405 /* FIXME: When doing a relocatable link, we may have trouble
9406 copying relocations in other sections that refer to local symbols
9407 in the section being discarded. Those relocations will have to
9408 be converted somehow; as of this writing I'm not sure that any of
9409 the backends handle that correctly.
9411 It is tempting to instead not discard link once sections when
9412 doing a relocatable link (technically, they should be discarded
9413 whenever we are building constructors). However, that fails,
9414 because the linker winds up combining all the link once sections
9415 into a single large link once section, which defeats the purpose
9416 of having link once sections in the first place.
9418 Also, not merging link once sections in a relocatable link
9419 causes trouble for MIPS ELF, which relies on link once semantics
9420 to handle the .reginfo section correctly. */
9422 name = bfd_get_section_name (abfd, sec);
9424 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9425 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
9430 already_linked_list = bfd_section_already_linked_table_lookup (p);
9432 for (l = already_linked_list->entry; l != NULL; l = l->next)
9434 /* We may have 3 different sections on the list: group section,
9435 comdat section and linkonce section. SEC may be a linkonce or
9436 group section. We match a group section with a group section,
9437 a linkonce section with a linkonce section, and ignore comdat
9439 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9440 && strcmp (name, l->sec->name) == 0
9441 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9443 /* The section has already been linked. See if we should
9445 switch (flags & SEC_LINK_DUPLICATES)
9450 case SEC_LINK_DUPLICATES_DISCARD:
9453 case SEC_LINK_DUPLICATES_ONE_ONLY:
9454 (*_bfd_error_handler)
9455 (_("%B: ignoring duplicate section `%A'\n"),
9459 case SEC_LINK_DUPLICATES_SAME_SIZE:
9460 if (sec->size != l->sec->size)
9461 (*_bfd_error_handler)
9462 (_("%B: duplicate section `%A' has different size\n"),
9466 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9467 if (sec->size != l->sec->size)
9468 (*_bfd_error_handler)
9469 (_("%B: duplicate section `%A' has different size\n"),
9471 else if (sec->size != 0)
9473 bfd_byte *sec_contents, *l_sec_contents;
9475 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9476 (*_bfd_error_handler)
9477 (_("%B: warning: could not read contents of section `%A'\n"),
9479 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9481 (*_bfd_error_handler)
9482 (_("%B: warning: could not read contents of section `%A'\n"),
9483 l->sec->owner, l->sec);
9484 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9485 (*_bfd_error_handler)
9486 (_("%B: warning: duplicate section `%A' has different contents\n"),
9490 free (sec_contents);
9492 free (l_sec_contents);
9497 /* Set the output_section field so that lang_add_section
9498 does not create a lang_input_section structure for this
9499 section. Since there might be a symbol in the section
9500 being discarded, we must retain a pointer to the section
9501 which we are really going to use. */
9502 sec->output_section = bfd_abs_section_ptr;
9503 sec->kept_section = l->sec;
9505 if (flags & SEC_GROUP)
9507 asection *first = elf_next_in_group (sec);
9508 asection *s = first;
9512 s->output_section = bfd_abs_section_ptr;
9513 /* Record which group discards it. */
9514 s->kept_section = l->sec;
9515 s = elf_next_in_group (s);
9516 /* These lists are circular. */
9528 /* If this is the member of a single member comdat group and the
9529 group hasn't be discarded, we check if it matches a linkonce
9530 section. We only record the discarded comdat group. Otherwise
9531 the undiscarded group will be discarded incorrectly later since
9532 itself has been recorded. */
9533 for (l = already_linked_list->entry; l != NULL; l = l->next)
9534 if ((l->sec->flags & SEC_GROUP) == 0
9535 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9536 && bfd_elf_match_symbols_in_sections (l->sec,
9537 elf_next_in_group (sec)))
9539 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
9540 elf_next_in_group (sec)->kept_section = l->sec;
9541 group->output_section = bfd_abs_section_ptr;
9548 /* There is no direct match. But for linkonce section, we should
9549 check if there is a match with comdat group member. We always
9550 record the linkonce section, discarded or not. */
9551 for (l = already_linked_list->entry; l != NULL; l = l->next)
9552 if (l->sec->flags & SEC_GROUP)
9554 asection *first = elf_next_in_group (l->sec);
9557 && elf_next_in_group (first) == first
9558 && bfd_elf_match_symbols_in_sections (first, sec))
9560 sec->output_section = bfd_abs_section_ptr;
9561 sec->kept_section = l->sec;
9566 /* This is the first section with this name. Record it. */
9567 bfd_section_already_linked_table_insert (already_linked_list, sec);