1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 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 a strtab to hold the dynamic symbol names. */
108 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
110 struct elf_link_hash_table *hash_table;
112 hash_table = elf_hash_table (info);
113 if (hash_table->dynobj == NULL)
114 hash_table->dynobj = abfd;
116 if (hash_table->dynstr == NULL)
118 hash_table->dynstr = _bfd_elf_strtab_init ();
119 if (hash_table->dynstr == NULL)
125 /* Create some sections which will be filled in with dynamic linking
126 information. ABFD is an input file which requires dynamic sections
127 to be created. The dynamic sections take up virtual memory space
128 when the final executable is run, so we need to create them before
129 addresses are assigned to the output sections. We work out the
130 actual contents and size of these sections later. */
133 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
136 register asection *s;
137 struct elf_link_hash_entry *h;
138 struct bfd_link_hash_entry *bh;
139 const struct elf_backend_data *bed;
141 if (! is_elf_hash_table (info->hash))
144 if (elf_hash_table (info)->dynamic_sections_created)
147 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
150 abfd = elf_hash_table (info)->dynobj;
151 bed = get_elf_backend_data (abfd);
153 flags = bed->dynamic_sec_flags;
155 /* A dynamically linked executable has a .interp section, but a
156 shared library does not. */
157 if (info->executable)
159 s = bfd_make_section (abfd, ".interp");
161 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
165 if (! info->traditional_format)
167 s = bfd_make_section (abfd, ".eh_frame_hdr");
169 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
170 || ! bfd_set_section_alignment (abfd, s, 2))
172 elf_hash_table (info)->eh_info.hdr_sec = s;
175 /* Create sections to hold version informations. These are removed
176 if they are not needed. */
177 s = bfd_make_section (abfd, ".gnu.version_d");
179 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
180 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
183 s = bfd_make_section (abfd, ".gnu.version");
185 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
186 || ! bfd_set_section_alignment (abfd, s, 1))
189 s = bfd_make_section (abfd, ".gnu.version_r");
191 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
192 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
195 s = bfd_make_section (abfd, ".dynsym");
197 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
198 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
201 s = bfd_make_section (abfd, ".dynstr");
203 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
206 s = bfd_make_section (abfd, ".dynamic");
208 || ! bfd_set_section_flags (abfd, s, flags)
209 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
212 /* The special symbol _DYNAMIC is always set to the start of the
213 .dynamic section. This call occurs before we have processed the
214 symbols for any dynamic object, so we don't have to worry about
215 overriding a dynamic definition. We could set _DYNAMIC in a
216 linker script, but we only want to define it if we are, in fact,
217 creating a .dynamic section. We don't want to define it if there
218 is no .dynamic section, since on some ELF platforms the start up
219 code examines it to decide how to initialize the process. */
221 if (! (_bfd_generic_link_add_one_symbol
222 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE,
223 get_elf_backend_data (abfd)->collect, &bh)))
225 h = (struct elf_link_hash_entry *) bh;
227 h->type = STT_OBJECT;
229 if (! info->executable
230 && ! bfd_elf_link_record_dynamic_symbol (info, h))
233 s = bfd_make_section (abfd, ".hash");
235 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
236 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
238 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
240 /* Let the backend create the rest of the sections. This lets the
241 backend set the right flags. The backend will normally create
242 the .got and .plt sections. */
243 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
246 elf_hash_table (info)->dynamic_sections_created = TRUE;
251 /* Create dynamic sections when linking against a dynamic object. */
254 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
256 flagword flags, pltflags;
258 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
260 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
261 .rel[a].bss sections. */
262 flags = bed->dynamic_sec_flags;
265 if (bed->plt_not_loaded)
266 /* We do not clear SEC_ALLOC here because we still want the OS to
267 allocate space for the section; it's just that there's nothing
268 to read in from the object file. */
269 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
271 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
272 if (bed->plt_readonly)
273 pltflags |= SEC_READONLY;
275 s = bfd_make_section (abfd, ".plt");
277 || ! bfd_set_section_flags (abfd, s, pltflags)
278 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
281 if (bed->want_plt_sym)
283 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
285 struct elf_link_hash_entry *h;
286 struct bfd_link_hash_entry *bh = NULL;
288 if (! (_bfd_generic_link_add_one_symbol
289 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
290 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
292 h = (struct elf_link_hash_entry *) bh;
294 h->type = STT_OBJECT;
296 if (! info->executable
297 && ! bfd_elf_link_record_dynamic_symbol (info, h))
301 s = bfd_make_section (abfd,
302 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
304 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
305 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
308 if (! _bfd_elf_create_got_section (abfd, info))
311 if (bed->want_dynbss)
313 /* The .dynbss section is a place to put symbols which are defined
314 by dynamic objects, are referenced by regular objects, and are
315 not functions. We must allocate space for them in the process
316 image and use a R_*_COPY reloc to tell the dynamic linker to
317 initialize them at run time. The linker script puts the .dynbss
318 section into the .bss section of the final image. */
319 s = bfd_make_section (abfd, ".dynbss");
321 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
324 /* The .rel[a].bss section holds copy relocs. This section is not
325 normally needed. We need to create it here, though, so that the
326 linker will map it to an output section. We can't just create it
327 only if we need it, because we will not know whether we need it
328 until we have seen all the input files, and the first time the
329 main linker code calls BFD after examining all the input files
330 (size_dynamic_sections) the input sections have already been
331 mapped to the output sections. If the section turns out not to
332 be needed, we can discard it later. We will never need this
333 section when generating a shared object, since they do not use
337 s = bfd_make_section (abfd,
338 (bed->default_use_rela_p
339 ? ".rela.bss" : ".rel.bss"));
341 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
342 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
350 /* Record a new dynamic symbol. We record the dynamic symbols as we
351 read the input files, since we need to have a list of all of them
352 before we can determine the final sizes of the output sections.
353 Note that we may actually call this function even though we are not
354 going to output any dynamic symbols; in some cases we know that a
355 symbol should be in the dynamic symbol table, but only if there is
359 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
360 struct elf_link_hash_entry *h)
362 if (h->dynindx == -1)
364 struct elf_strtab_hash *dynstr;
369 /* XXX: The ABI draft says the linker must turn hidden and
370 internal symbols into STB_LOCAL symbols when producing the
371 DSO. However, if ld.so honors st_other in the dynamic table,
372 this would not be necessary. */
373 switch (ELF_ST_VISIBILITY (h->other))
377 if (h->root.type != bfd_link_hash_undefined
378 && h->root.type != bfd_link_hash_undefweak)
388 h->dynindx = elf_hash_table (info)->dynsymcount;
389 ++elf_hash_table (info)->dynsymcount;
391 dynstr = elf_hash_table (info)->dynstr;
394 /* Create a strtab to hold the dynamic symbol names. */
395 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
400 /* We don't put any version information in the dynamic string
402 name = h->root.root.string;
403 p = strchr (name, ELF_VER_CHR);
405 /* We know that the p points into writable memory. In fact,
406 there are only a few symbols that have read-only names, being
407 those like _GLOBAL_OFFSET_TABLE_ that are created specially
408 by the backends. Most symbols will have names pointing into
409 an ELF string table read from a file, or to objalloc memory. */
412 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
417 if (indx == (bfd_size_type) -1)
419 h->dynstr_index = indx;
425 /* Record an assignment to a symbol made by a linker script. We need
426 this in case some dynamic object refers to this symbol. */
429 bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED,
430 struct bfd_link_info *info,
434 struct elf_link_hash_entry *h;
436 if (!is_elf_hash_table (info->hash))
439 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE);
443 /* Since we're defining the symbol, don't let it seem to have not
444 been defined. record_dynamic_symbol and size_dynamic_sections
446 ??? Changing bfd_link_hash_undefined to bfd_link_hash_new (or
447 to bfd_link_hash_undefweak, see linker.c:link_action) runs the risk
448 of some later symbol manipulation setting the symbol back to
449 bfd_link_hash_undefined, and the linker trying to add the symbol to
450 the undefs list twice. */
451 if (h->root.type == bfd_link_hash_undefweak
452 || h->root.type == bfd_link_hash_undefined)
453 h->root.type = bfd_link_hash_new;
455 if (h->root.type == bfd_link_hash_new)
458 /* If this symbol is being provided by the linker script, and it is
459 currently defined by a dynamic object, but not by a regular
460 object, then mark it as undefined so that the generic linker will
461 force the correct value. */
465 h->root.type = bfd_link_hash_undefined;
467 /* If this symbol is not being provided by the linker script, and it is
468 currently defined by a dynamic object, but not by a regular object,
469 then clear out any version information because the symbol will not be
470 associated with the dynamic object any more. */
474 h->verinfo.verdef = NULL;
483 if (! bfd_elf_link_record_dynamic_symbol (info, h))
486 /* If this is a weak defined symbol, and we know a corresponding
487 real symbol from the same dynamic object, make sure the real
488 symbol is also made into a dynamic symbol. */
489 if (h->u.weakdef != NULL
490 && h->u.weakdef->dynindx == -1)
492 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
500 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
501 success, and 2 on a failure caused by attempting to record a symbol
502 in a discarded section, eg. a discarded link-once section symbol. */
505 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
510 struct elf_link_local_dynamic_entry *entry;
511 struct elf_link_hash_table *eht;
512 struct elf_strtab_hash *dynstr;
513 unsigned long dynstr_index;
515 Elf_External_Sym_Shndx eshndx;
516 char esym[sizeof (Elf64_External_Sym)];
518 if (! is_elf_hash_table (info->hash))
521 /* See if the entry exists already. */
522 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
523 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
526 amt = sizeof (*entry);
527 entry = bfd_alloc (input_bfd, amt);
531 /* Go find the symbol, so that we can find it's name. */
532 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
533 1, input_indx, &entry->isym, esym, &eshndx))
535 bfd_release (input_bfd, entry);
539 if (entry->isym.st_shndx != SHN_UNDEF
540 && (entry->isym.st_shndx < SHN_LORESERVE
541 || entry->isym.st_shndx > SHN_HIRESERVE))
545 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
546 if (s == NULL || bfd_is_abs_section (s->output_section))
548 /* We can still bfd_release here as nothing has done another
549 bfd_alloc. We can't do this later in this function. */
550 bfd_release (input_bfd, entry);
555 name = (bfd_elf_string_from_elf_section
556 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
557 entry->isym.st_name));
559 dynstr = elf_hash_table (info)->dynstr;
562 /* Create a strtab to hold the dynamic symbol names. */
563 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
568 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
569 if (dynstr_index == (unsigned long) -1)
571 entry->isym.st_name = dynstr_index;
573 eht = elf_hash_table (info);
575 entry->next = eht->dynlocal;
576 eht->dynlocal = entry;
577 entry->input_bfd = input_bfd;
578 entry->input_indx = input_indx;
581 /* Whatever binding the symbol had before, it's now local. */
583 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
585 /* The dynindx will be set at the end of size_dynamic_sections. */
590 /* Return the dynindex of a local dynamic symbol. */
593 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
597 struct elf_link_local_dynamic_entry *e;
599 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
600 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
605 /* This function is used to renumber the dynamic symbols, if some of
606 them are removed because they are marked as local. This is called
607 via elf_link_hash_traverse. */
610 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
613 size_t *count = data;
615 if (h->root.type == bfd_link_hash_warning)
616 h = (struct elf_link_hash_entry *) h->root.u.i.link;
618 if (h->dynindx != -1)
619 h->dynindx = ++(*count);
624 /* Return true if the dynamic symbol for a given section should be
625 omitted when creating a shared library. */
627 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
628 struct bfd_link_info *info,
631 switch (elf_section_data (p)->this_hdr.sh_type)
635 /* If sh_type is yet undecided, assume it could be
636 SHT_PROGBITS/SHT_NOBITS. */
638 if (strcmp (p->name, ".got") == 0
639 || strcmp (p->name, ".got.plt") == 0
640 || strcmp (p->name, ".plt") == 0)
643 bfd *dynobj = elf_hash_table (info)->dynobj;
646 && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL
647 && (ip->flags & SEC_LINKER_CREATED)
648 && ip->output_section == p)
653 /* There shouldn't be section relative relocations
654 against any other section. */
660 /* Assign dynsym indices. In a shared library we generate a section
661 symbol for each output section, which come first. Next come all of
662 the back-end allocated local dynamic syms, followed by the rest of
663 the global symbols. */
666 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
668 unsigned long dynsymcount = 0;
672 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
674 for (p = output_bfd->sections; p ; p = p->next)
675 if ((p->flags & SEC_EXCLUDE) == 0
676 && (p->flags & SEC_ALLOC) != 0
677 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
678 elf_section_data (p)->dynindx = ++dynsymcount;
681 if (elf_hash_table (info)->dynlocal)
683 struct elf_link_local_dynamic_entry *p;
684 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
685 p->dynindx = ++dynsymcount;
688 elf_link_hash_traverse (elf_hash_table (info),
689 elf_link_renumber_hash_table_dynsyms,
692 /* There is an unused NULL entry at the head of the table which
693 we must account for in our count. Unless there weren't any
694 symbols, which means we'll have no table at all. */
695 if (dynsymcount != 0)
698 return elf_hash_table (info)->dynsymcount = dynsymcount;
701 /* This function is called when we want to define a new symbol. It
702 handles the various cases which arise when we find a definition in
703 a dynamic object, or when there is already a definition in a
704 dynamic object. The new symbol is described by NAME, SYM, PSEC,
705 and PVALUE. We set SYM_HASH to the hash table entry. We set
706 OVERRIDE if the old symbol is overriding a new definition. We set
707 TYPE_CHANGE_OK if it is OK for the type to change. We set
708 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
709 change, we mean that we shouldn't warn if the type or size does
713 _bfd_elf_merge_symbol (bfd *abfd,
714 struct bfd_link_info *info,
716 Elf_Internal_Sym *sym,
719 struct elf_link_hash_entry **sym_hash,
721 bfd_boolean *override,
722 bfd_boolean *type_change_ok,
723 bfd_boolean *size_change_ok)
725 asection *sec, *oldsec;
726 struct elf_link_hash_entry *h;
727 struct elf_link_hash_entry *flip;
730 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
731 bfd_boolean newweak, oldweak, old_asneeded;
737 bind = ELF_ST_BIND (sym->st_info);
739 if (! bfd_is_und_section (sec))
740 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
742 h = ((struct elf_link_hash_entry *)
743 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
748 /* This code is for coping with dynamic objects, and is only useful
749 if we are doing an ELF link. */
750 if (info->hash->creator != abfd->xvec)
753 /* For merging, we only care about real symbols. */
755 while (h->root.type == bfd_link_hash_indirect
756 || h->root.type == bfd_link_hash_warning)
757 h = (struct elf_link_hash_entry *) h->root.u.i.link;
759 /* If we just created the symbol, mark it as being an ELF symbol.
760 Other than that, there is nothing to do--there is no merge issue
761 with a newly defined symbol--so we just return. */
763 if (h->root.type == bfd_link_hash_new)
769 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
772 switch (h->root.type)
779 case bfd_link_hash_undefined:
780 case bfd_link_hash_undefweak:
781 oldbfd = h->root.u.undef.abfd;
785 case bfd_link_hash_defined:
786 case bfd_link_hash_defweak:
787 oldbfd = h->root.u.def.section->owner;
788 oldsec = h->root.u.def.section;
791 case bfd_link_hash_common:
792 oldbfd = h->root.u.c.p->section->owner;
793 oldsec = h->root.u.c.p->section;
797 /* In cases involving weak versioned symbols, we may wind up trying
798 to merge a symbol with itself. Catch that here, to avoid the
799 confusion that results if we try to override a symbol with
800 itself. The additional tests catch cases like
801 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
802 dynamic object, which we do want to handle here. */
804 && ((abfd->flags & DYNAMIC) == 0
808 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
809 respectively, is from a dynamic object. */
811 if ((abfd->flags & DYNAMIC) != 0)
817 olddyn = (oldbfd->flags & DYNAMIC) != 0;
822 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
823 indices used by MIPS ELF. */
824 switch (h->root.type)
830 case bfd_link_hash_defined:
831 case bfd_link_hash_defweak:
832 hsec = h->root.u.def.section;
835 case bfd_link_hash_common:
836 hsec = h->root.u.c.p->section;
843 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
846 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
847 respectively, appear to be a definition rather than reference. */
849 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
854 if (h->root.type == bfd_link_hash_undefined
855 || h->root.type == bfd_link_hash_undefweak
856 || h->root.type == bfd_link_hash_common)
861 /* If the old definition came from an as-needed dynamic library which
862 wasn't found to be needed, treat the sym as undefined. */
863 old_asneeded = FALSE;
866 && (elf_dyn_lib_class (oldbfd) & DYN_AS_NEEDED) != 0)
869 /* Check TLS symbol. */
870 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
871 && ELF_ST_TYPE (sym->st_info) != h->type)
874 bfd_boolean ntdef, tdef;
875 asection *ntsec, *tsec;
877 if (h->type == STT_TLS)
897 (*_bfd_error_handler)
898 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
899 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
900 else if (!tdef && !ntdef)
901 (*_bfd_error_handler)
902 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
903 tbfd, ntbfd, h->root.root.string);
905 (*_bfd_error_handler)
906 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
907 tbfd, tsec, ntbfd, h->root.root.string);
909 (*_bfd_error_handler)
910 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
911 tbfd, ntbfd, ntsec, h->root.root.string);
913 bfd_set_error (bfd_error_bad_value);
917 /* We need to remember if a symbol has a definition in a dynamic
918 object or is weak in all dynamic objects. Internal and hidden
919 visibility will make it unavailable to dynamic objects. */
920 if (newdyn && !h->dynamic_def)
922 if (!bfd_is_und_section (sec))
926 /* Check if this symbol is weak in all dynamic objects. If it
927 is the first time we see it in a dynamic object, we mark
928 if it is weak. Otherwise, we clear it. */
931 if (bind == STB_WEAK)
934 else if (bind != STB_WEAK)
939 /* If the old symbol has non-default visibility, we ignore the new
940 definition from a dynamic object. */
942 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
943 && !bfd_is_und_section (sec))
946 /* Make sure this symbol is dynamic. */
948 /* A protected symbol has external availability. Make sure it is
951 FIXME: Should we check type and size for protected symbol? */
952 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
953 return bfd_elf_link_record_dynamic_symbol (info, h);
958 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
961 /* If the new symbol with non-default visibility comes from a
962 relocatable file and the old definition comes from a dynamic
963 object, we remove the old definition. */
964 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
967 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
968 && bfd_is_und_section (sec))
970 /* If the new symbol is undefined and the old symbol was
971 also undefined before, we need to make sure
972 _bfd_generic_link_add_one_symbol doesn't mess
973 up the linker hash table undefs list. Since the old
974 definition came from a dynamic object, it is still on the
976 h->root.type = bfd_link_hash_undefined;
977 h->root.u.undef.abfd = abfd;
981 h->root.type = bfd_link_hash_new;
982 h->root.u.undef.abfd = NULL;
991 /* FIXME: Should we check type and size for protected symbol? */
997 /* Differentiate strong and weak symbols. */
998 newweak = bind == STB_WEAK;
999 oldweak = (h->root.type == bfd_link_hash_defweak
1000 || h->root.type == bfd_link_hash_undefweak);
1002 /* If a new weak symbol definition comes from a regular file and the
1003 old symbol comes from a dynamic library, we treat the new one as
1004 strong. Similarly, an old weak symbol definition from a regular
1005 file is treated as strong when the new symbol comes from a dynamic
1006 library. Further, an old weak symbol from a dynamic library is
1007 treated as strong if the new symbol is from a dynamic library.
1008 This reflects the way glibc's ld.so works.
1010 Do this before setting *type_change_ok or *size_change_ok so that
1011 we warn properly when dynamic library symbols are overridden. */
1013 if (newdef && !newdyn && olddyn)
1015 if (olddef && newdyn)
1018 /* It's OK to change the type if either the existing symbol or the
1019 new symbol is weak. A type change is also OK if the old symbol
1020 is undefined and the new symbol is defined. */
1025 && h->root.type == bfd_link_hash_undefined))
1026 *type_change_ok = TRUE;
1028 /* It's OK to change the size if either the existing symbol or the
1029 new symbol is weak, or if the old symbol is undefined. */
1032 || h->root.type == bfd_link_hash_undefined)
1033 *size_change_ok = TRUE;
1035 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1036 symbol, respectively, appears to be a common symbol in a dynamic
1037 object. If a symbol appears in an uninitialized section, and is
1038 not weak, and is not a function, then it may be a common symbol
1039 which was resolved when the dynamic object was created. We want
1040 to treat such symbols specially, because they raise special
1041 considerations when setting the symbol size: if the symbol
1042 appears as a common symbol in a regular object, and the size in
1043 the regular object is larger, we must make sure that we use the
1044 larger size. This problematic case can always be avoided in C,
1045 but it must be handled correctly when using Fortran shared
1048 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1049 likewise for OLDDYNCOMMON and OLDDEF.
1051 Note that this test is just a heuristic, and that it is quite
1052 possible to have an uninitialized symbol in a shared object which
1053 is really a definition, rather than a common symbol. This could
1054 lead to some minor confusion when the symbol really is a common
1055 symbol in some regular object. However, I think it will be
1061 && (sec->flags & SEC_ALLOC) != 0
1062 && (sec->flags & SEC_LOAD) == 0
1064 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1065 newdyncommon = TRUE;
1067 newdyncommon = FALSE;
1072 && h->root.type == bfd_link_hash_defined
1074 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1075 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1077 && h->type != STT_FUNC)
1078 olddyncommon = TRUE;
1080 olddyncommon = FALSE;
1082 /* If both the old and the new symbols look like common symbols in a
1083 dynamic object, set the size of the symbol to the larger of the
1088 && sym->st_size != h->size)
1090 /* Since we think we have two common symbols, issue a multiple
1091 common warning if desired. Note that we only warn if the
1092 size is different. If the size is the same, we simply let
1093 the old symbol override the new one as normally happens with
1094 symbols defined in dynamic objects. */
1096 if (! ((*info->callbacks->multiple_common)
1097 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1098 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1101 if (sym->st_size > h->size)
1102 h->size = sym->st_size;
1104 *size_change_ok = TRUE;
1107 /* If we are looking at a dynamic object, and we have found a
1108 definition, we need to see if the symbol was already defined by
1109 some other object. If so, we want to use the existing
1110 definition, and we do not want to report a multiple symbol
1111 definition error; we do this by clobbering *PSEC to be
1112 bfd_und_section_ptr.
1114 We treat a common symbol as a definition if the symbol in the
1115 shared library is a function, since common symbols always
1116 represent variables; this can cause confusion in principle, but
1117 any such confusion would seem to indicate an erroneous program or
1118 shared library. We also permit a common symbol in a regular
1119 object to override a weak symbol in a shared object. */
1123 && ((olddef && !old_asneeded)
1124 || (h->root.type == bfd_link_hash_common
1126 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1130 newdyncommon = FALSE;
1132 *psec = sec = bfd_und_section_ptr;
1133 *size_change_ok = TRUE;
1135 /* If we get here when the old symbol is a common symbol, then
1136 we are explicitly letting it override a weak symbol or
1137 function in a dynamic object, and we don't want to warn about
1138 a type change. If the old symbol is a defined symbol, a type
1139 change warning may still be appropriate. */
1141 if (h->root.type == bfd_link_hash_common)
1142 *type_change_ok = TRUE;
1145 /* Handle the special case of an old common symbol merging with a
1146 new symbol which looks like a common symbol in a shared object.
1147 We change *PSEC and *PVALUE to make the new symbol look like a
1148 common symbol, and let _bfd_generic_link_add_one_symbol will do
1152 && h->root.type == bfd_link_hash_common)
1156 newdyncommon = FALSE;
1157 *pvalue = sym->st_size;
1158 *psec = sec = bfd_com_section_ptr;
1159 *size_change_ok = TRUE;
1162 /* If the old symbol is from a dynamic object, and the new symbol is
1163 a definition which is not from a dynamic object, then the new
1164 symbol overrides the old symbol. Symbols from regular files
1165 always take precedence over symbols from dynamic objects, even if
1166 they are defined after the dynamic object in the link.
1168 As above, we again permit a common symbol in a regular object to
1169 override a definition in a shared object if the shared object
1170 symbol is a function or is weak. */
1173 if ((!newdyn || old_asneeded)
1175 || (bfd_is_com_section (sec)
1177 || h->type == STT_FUNC)))
1182 /* Change the hash table entry to undefined, and let
1183 _bfd_generic_link_add_one_symbol do the right thing with the
1186 h->root.type = bfd_link_hash_undefined;
1187 h->root.u.undef.abfd = h->root.u.def.section->owner;
1188 *size_change_ok = TRUE;
1191 olddyncommon = FALSE;
1193 /* We again permit a type change when a common symbol may be
1194 overriding a function. */
1196 if (bfd_is_com_section (sec))
1197 *type_change_ok = TRUE;
1199 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1202 /* This union may have been set to be non-NULL when this symbol
1203 was seen in a dynamic object. We must force the union to be
1204 NULL, so that it is correct for a regular symbol. */
1205 h->verinfo.vertree = NULL;
1208 /* Handle the special case of a new common symbol merging with an
1209 old symbol that looks like it might be a common symbol defined in
1210 a shared object. Note that we have already handled the case in
1211 which a new common symbol should simply override the definition
1212 in the shared library. */
1215 && bfd_is_com_section (sec)
1218 /* It would be best if we could set the hash table entry to a
1219 common symbol, but we don't know what to use for the section
1220 or the alignment. */
1221 if (! ((*info->callbacks->multiple_common)
1222 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1223 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1226 /* If the presumed common symbol in the dynamic object is
1227 larger, pretend that the new symbol has its size. */
1229 if (h->size > *pvalue)
1232 /* FIXME: We no longer know the alignment required by the symbol
1233 in the dynamic object, so we just wind up using the one from
1234 the regular object. */
1237 olddyncommon = FALSE;
1239 h->root.type = bfd_link_hash_undefined;
1240 h->root.u.undef.abfd = h->root.u.def.section->owner;
1242 *size_change_ok = TRUE;
1243 *type_change_ok = TRUE;
1245 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1248 h->verinfo.vertree = NULL;
1253 /* Handle the case where we had a versioned symbol in a dynamic
1254 library and now find a definition in a normal object. In this
1255 case, we make the versioned symbol point to the normal one. */
1256 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1257 flip->root.type = h->root.type;
1258 h->root.type = bfd_link_hash_indirect;
1259 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1260 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1261 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1265 flip->ref_dynamic = 1;
1272 /* This function is called to create an indirect symbol from the
1273 default for the symbol with the default version if needed. The
1274 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1275 set DYNSYM if the new indirect symbol is dynamic. */
1278 _bfd_elf_add_default_symbol (bfd *abfd,
1279 struct bfd_link_info *info,
1280 struct elf_link_hash_entry *h,
1282 Elf_Internal_Sym *sym,
1285 bfd_boolean *dynsym,
1286 bfd_boolean override)
1288 bfd_boolean type_change_ok;
1289 bfd_boolean size_change_ok;
1292 struct elf_link_hash_entry *hi;
1293 struct bfd_link_hash_entry *bh;
1294 const struct elf_backend_data *bed;
1295 bfd_boolean collect;
1296 bfd_boolean dynamic;
1298 size_t len, shortlen;
1301 /* If this symbol has a version, and it is the default version, we
1302 create an indirect symbol from the default name to the fully
1303 decorated name. This will cause external references which do not
1304 specify a version to be bound to this version of the symbol. */
1305 p = strchr (name, ELF_VER_CHR);
1306 if (p == NULL || p[1] != ELF_VER_CHR)
1311 /* We are overridden by an old definition. We need to check if we
1312 need to create the indirect symbol from the default name. */
1313 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1315 BFD_ASSERT (hi != NULL);
1318 while (hi->root.type == bfd_link_hash_indirect
1319 || hi->root.type == bfd_link_hash_warning)
1321 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1327 bed = get_elf_backend_data (abfd);
1328 collect = bed->collect;
1329 dynamic = (abfd->flags & DYNAMIC) != 0;
1331 shortlen = p - name;
1332 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1333 if (shortname == NULL)
1335 memcpy (shortname, name, shortlen);
1336 shortname[shortlen] = '\0';
1338 /* We are going to create a new symbol. Merge it with any existing
1339 symbol with this name. For the purposes of the merge, act as
1340 though we were defining the symbol we just defined, although we
1341 actually going to define an indirect symbol. */
1342 type_change_ok = FALSE;
1343 size_change_ok = FALSE;
1345 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1346 &hi, &skip, &override, &type_change_ok,
1356 if (! (_bfd_generic_link_add_one_symbol
1357 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1358 0, name, FALSE, collect, &bh)))
1360 hi = (struct elf_link_hash_entry *) bh;
1364 /* In this case the symbol named SHORTNAME is overriding the
1365 indirect symbol we want to add. We were planning on making
1366 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1367 is the name without a version. NAME is the fully versioned
1368 name, and it is the default version.
1370 Overriding means that we already saw a definition for the
1371 symbol SHORTNAME in a regular object, and it is overriding
1372 the symbol defined in the dynamic object.
1374 When this happens, we actually want to change NAME, the
1375 symbol we just added, to refer to SHORTNAME. This will cause
1376 references to NAME in the shared object to become references
1377 to SHORTNAME in the regular object. This is what we expect
1378 when we override a function in a shared object: that the
1379 references in the shared object will be mapped to the
1380 definition in the regular object. */
1382 while (hi->root.type == bfd_link_hash_indirect
1383 || hi->root.type == bfd_link_hash_warning)
1384 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1386 h->root.type = bfd_link_hash_indirect;
1387 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1391 hi->ref_dynamic = 1;
1395 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1400 /* Now set HI to H, so that the following code will set the
1401 other fields correctly. */
1405 /* If there is a duplicate definition somewhere, then HI may not
1406 point to an indirect symbol. We will have reported an error to
1407 the user in that case. */
1409 if (hi->root.type == bfd_link_hash_indirect)
1411 struct elf_link_hash_entry *ht;
1413 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1414 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1416 /* See if the new flags lead us to realize that the symbol must
1428 if (hi->ref_regular)
1434 /* We also need to define an indirection from the nondefault version
1438 len = strlen (name);
1439 shortname = bfd_hash_allocate (&info->hash->table, len);
1440 if (shortname == NULL)
1442 memcpy (shortname, name, shortlen);
1443 memcpy (shortname + shortlen, p + 1, len - shortlen);
1445 /* Once again, merge with any existing symbol. */
1446 type_change_ok = FALSE;
1447 size_change_ok = FALSE;
1449 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1450 &hi, &skip, &override, &type_change_ok,
1459 /* Here SHORTNAME is a versioned name, so we don't expect to see
1460 the type of override we do in the case above unless it is
1461 overridden by a versioned definition. */
1462 if (hi->root.type != bfd_link_hash_defined
1463 && hi->root.type != bfd_link_hash_defweak)
1464 (*_bfd_error_handler)
1465 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1471 if (! (_bfd_generic_link_add_one_symbol
1472 (info, abfd, shortname, BSF_INDIRECT,
1473 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1475 hi = (struct elf_link_hash_entry *) bh;
1477 /* If there is a duplicate definition somewhere, then HI may not
1478 point to an indirect symbol. We will have reported an error
1479 to the user in that case. */
1481 if (hi->root.type == bfd_link_hash_indirect)
1483 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1485 /* See if the new flags lead us to realize that the symbol
1497 if (hi->ref_regular)
1507 /* This routine is used to export all defined symbols into the dynamic
1508 symbol table. It is called via elf_link_hash_traverse. */
1511 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1513 struct elf_info_failed *eif = data;
1515 /* Ignore indirect symbols. These are added by the versioning code. */
1516 if (h->root.type == bfd_link_hash_indirect)
1519 if (h->root.type == bfd_link_hash_warning)
1520 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1522 if (h->dynindx == -1
1526 struct bfd_elf_version_tree *t;
1527 struct bfd_elf_version_expr *d;
1529 for (t = eif->verdefs; t != NULL; t = t->next)
1531 if (t->globals.list != NULL)
1533 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1538 if (t->locals.list != NULL)
1540 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1549 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1560 /* Look through the symbols which are defined in other shared
1561 libraries and referenced here. Update the list of version
1562 dependencies. This will be put into the .gnu.version_r section.
1563 This function is called via elf_link_hash_traverse. */
1566 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1569 struct elf_find_verdep_info *rinfo = data;
1570 Elf_Internal_Verneed *t;
1571 Elf_Internal_Vernaux *a;
1574 if (h->root.type == bfd_link_hash_warning)
1575 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1577 /* We only care about symbols defined in shared objects with version
1582 || h->verinfo.verdef == NULL)
1585 /* See if we already know about this version. */
1586 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1588 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1591 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1592 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1598 /* This is a new version. Add it to tree we are building. */
1603 t = bfd_zalloc (rinfo->output_bfd, amt);
1606 rinfo->failed = TRUE;
1610 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1611 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1612 elf_tdata (rinfo->output_bfd)->verref = t;
1616 a = bfd_zalloc (rinfo->output_bfd, amt);
1618 /* Note that we are copying a string pointer here, and testing it
1619 above. If bfd_elf_string_from_elf_section is ever changed to
1620 discard the string data when low in memory, this will have to be
1622 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1624 a->vna_flags = h->verinfo.verdef->vd_flags;
1625 a->vna_nextptr = t->vn_auxptr;
1627 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1630 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1637 /* Figure out appropriate versions for all the symbols. We may not
1638 have the version number script until we have read all of the input
1639 files, so until that point we don't know which symbols should be
1640 local. This function is called via elf_link_hash_traverse. */
1643 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1645 struct elf_assign_sym_version_info *sinfo;
1646 struct bfd_link_info *info;
1647 const struct elf_backend_data *bed;
1648 struct elf_info_failed eif;
1655 if (h->root.type == bfd_link_hash_warning)
1656 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1658 /* Fix the symbol flags. */
1661 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1664 sinfo->failed = TRUE;
1668 /* We only need version numbers for symbols defined in regular
1670 if (!h->def_regular)
1673 bed = get_elf_backend_data (sinfo->output_bfd);
1674 p = strchr (h->root.root.string, ELF_VER_CHR);
1675 if (p != NULL && h->verinfo.vertree == NULL)
1677 struct bfd_elf_version_tree *t;
1682 /* There are two consecutive ELF_VER_CHR characters if this is
1683 not a hidden symbol. */
1685 if (*p == ELF_VER_CHR)
1691 /* If there is no version string, we can just return out. */
1699 /* Look for the version. If we find it, it is no longer weak. */
1700 for (t = sinfo->verdefs; t != NULL; t = t->next)
1702 if (strcmp (t->name, p) == 0)
1706 struct bfd_elf_version_expr *d;
1708 len = p - h->root.root.string;
1709 alc = bfd_malloc (len);
1712 memcpy (alc, h->root.root.string, len - 1);
1713 alc[len - 1] = '\0';
1714 if (alc[len - 2] == ELF_VER_CHR)
1715 alc[len - 2] = '\0';
1717 h->verinfo.vertree = t;
1721 if (t->globals.list != NULL)
1722 d = (*t->match) (&t->globals, NULL, alc);
1724 /* See if there is anything to force this symbol to
1726 if (d == NULL && t->locals.list != NULL)
1728 d = (*t->match) (&t->locals, NULL, alc);
1732 && ! info->export_dynamic)
1733 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1741 /* If we are building an application, we need to create a
1742 version node for this version. */
1743 if (t == NULL && info->executable)
1745 struct bfd_elf_version_tree **pp;
1748 /* If we aren't going to export this symbol, we don't need
1749 to worry about it. */
1750 if (h->dynindx == -1)
1754 t = bfd_zalloc (sinfo->output_bfd, amt);
1757 sinfo->failed = TRUE;
1762 t->name_indx = (unsigned int) -1;
1766 /* Don't count anonymous version tag. */
1767 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1769 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1771 t->vernum = version_index;
1775 h->verinfo.vertree = t;
1779 /* We could not find the version for a symbol when
1780 generating a shared archive. Return an error. */
1781 (*_bfd_error_handler)
1782 (_("%B: undefined versioned symbol name %s"),
1783 sinfo->output_bfd, h->root.root.string);
1784 bfd_set_error (bfd_error_bad_value);
1785 sinfo->failed = TRUE;
1793 /* If we don't have a version for this symbol, see if we can find
1795 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1797 struct bfd_elf_version_tree *t;
1798 struct bfd_elf_version_tree *local_ver;
1799 struct bfd_elf_version_expr *d;
1801 /* See if can find what version this symbol is in. If the
1802 symbol is supposed to be local, then don't actually register
1805 for (t = sinfo->verdefs; t != NULL; t = t->next)
1807 if (t->globals.list != NULL)
1809 bfd_boolean matched;
1813 while ((d = (*t->match) (&t->globals, d,
1814 h->root.root.string)) != NULL)
1819 /* There is a version without definition. Make
1820 the symbol the default definition for this
1822 h->verinfo.vertree = t;
1830 /* There is no undefined version for this symbol. Hide the
1832 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1835 if (t->locals.list != NULL)
1838 while ((d = (*t->match) (&t->locals, d,
1839 h->root.root.string)) != NULL)
1842 /* If the match is "*", keep looking for a more
1843 explicit, perhaps even global, match.
1844 XXX: Shouldn't this be !d->wildcard instead? */
1845 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1854 if (local_ver != NULL)
1856 h->verinfo.vertree = local_ver;
1857 if (h->dynindx != -1
1859 && ! info->export_dynamic)
1861 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1869 /* Read and swap the relocs from the section indicated by SHDR. This
1870 may be either a REL or a RELA section. The relocations are
1871 translated into RELA relocations and stored in INTERNAL_RELOCS,
1872 which should have already been allocated to contain enough space.
1873 The EXTERNAL_RELOCS are a buffer where the external form of the
1874 relocations should be stored.
1876 Returns FALSE if something goes wrong. */
1879 elf_link_read_relocs_from_section (bfd *abfd,
1881 Elf_Internal_Shdr *shdr,
1882 void *external_relocs,
1883 Elf_Internal_Rela *internal_relocs)
1885 const struct elf_backend_data *bed;
1886 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1887 const bfd_byte *erela;
1888 const bfd_byte *erelaend;
1889 Elf_Internal_Rela *irela;
1890 Elf_Internal_Shdr *symtab_hdr;
1893 /* Position ourselves at the start of the section. */
1894 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1897 /* Read the relocations. */
1898 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1901 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1902 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1904 bed = get_elf_backend_data (abfd);
1906 /* Convert the external relocations to the internal format. */
1907 if (shdr->sh_entsize == bed->s->sizeof_rel)
1908 swap_in = bed->s->swap_reloc_in;
1909 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1910 swap_in = bed->s->swap_reloca_in;
1913 bfd_set_error (bfd_error_wrong_format);
1917 erela = external_relocs;
1918 erelaend = erela + shdr->sh_size;
1919 irela = internal_relocs;
1920 while (erela < erelaend)
1924 (*swap_in) (abfd, erela, irela);
1925 r_symndx = ELF32_R_SYM (irela->r_info);
1926 if (bed->s->arch_size == 64)
1928 if ((size_t) r_symndx >= nsyms)
1930 (*_bfd_error_handler)
1931 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1932 " for offset 0x%lx in section `%A'"),
1934 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1935 bfd_set_error (bfd_error_bad_value);
1938 irela += bed->s->int_rels_per_ext_rel;
1939 erela += shdr->sh_entsize;
1945 /* Read and swap the relocs for a section O. They may have been
1946 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1947 not NULL, they are used as buffers to read into. They are known to
1948 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1949 the return value is allocated using either malloc or bfd_alloc,
1950 according to the KEEP_MEMORY argument. If O has two relocation
1951 sections (both REL and RELA relocations), then the REL_HDR
1952 relocations will appear first in INTERNAL_RELOCS, followed by the
1953 REL_HDR2 relocations. */
1956 _bfd_elf_link_read_relocs (bfd *abfd,
1958 void *external_relocs,
1959 Elf_Internal_Rela *internal_relocs,
1960 bfd_boolean keep_memory)
1962 Elf_Internal_Shdr *rel_hdr;
1963 void *alloc1 = NULL;
1964 Elf_Internal_Rela *alloc2 = NULL;
1965 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1967 if (elf_section_data (o)->relocs != NULL)
1968 return elf_section_data (o)->relocs;
1970 if (o->reloc_count == 0)
1973 rel_hdr = &elf_section_data (o)->rel_hdr;
1975 if (internal_relocs == NULL)
1979 size = o->reloc_count;
1980 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1982 internal_relocs = bfd_alloc (abfd, size);
1984 internal_relocs = alloc2 = bfd_malloc (size);
1985 if (internal_relocs == NULL)
1989 if (external_relocs == NULL)
1991 bfd_size_type size = rel_hdr->sh_size;
1993 if (elf_section_data (o)->rel_hdr2)
1994 size += elf_section_data (o)->rel_hdr2->sh_size;
1995 alloc1 = bfd_malloc (size);
1998 external_relocs = alloc1;
2001 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2005 if (elf_section_data (o)->rel_hdr2
2006 && (!elf_link_read_relocs_from_section
2008 elf_section_data (o)->rel_hdr2,
2009 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2010 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2011 * bed->s->int_rels_per_ext_rel))))
2014 /* Cache the results for next time, if we can. */
2016 elf_section_data (o)->relocs = internal_relocs;
2021 /* Don't free alloc2, since if it was allocated we are passing it
2022 back (under the name of internal_relocs). */
2024 return internal_relocs;
2034 /* Compute the size of, and allocate space for, REL_HDR which is the
2035 section header for a section containing relocations for O. */
2038 _bfd_elf_link_size_reloc_section (bfd *abfd,
2039 Elf_Internal_Shdr *rel_hdr,
2042 bfd_size_type reloc_count;
2043 bfd_size_type num_rel_hashes;
2045 /* Figure out how many relocations there will be. */
2046 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2047 reloc_count = elf_section_data (o)->rel_count;
2049 reloc_count = elf_section_data (o)->rel_count2;
2051 num_rel_hashes = o->reloc_count;
2052 if (num_rel_hashes < reloc_count)
2053 num_rel_hashes = reloc_count;
2055 /* That allows us to calculate the size of the section. */
2056 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2058 /* The contents field must last into write_object_contents, so we
2059 allocate it with bfd_alloc rather than malloc. Also since we
2060 cannot be sure that the contents will actually be filled in,
2061 we zero the allocated space. */
2062 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2063 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2066 /* We only allocate one set of hash entries, so we only do it the
2067 first time we are called. */
2068 if (elf_section_data (o)->rel_hashes == NULL
2071 struct elf_link_hash_entry **p;
2073 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2077 elf_section_data (o)->rel_hashes = p;
2083 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2084 originated from the section given by INPUT_REL_HDR) to the
2088 _bfd_elf_link_output_relocs (bfd *output_bfd,
2089 asection *input_section,
2090 Elf_Internal_Shdr *input_rel_hdr,
2091 Elf_Internal_Rela *internal_relocs)
2093 Elf_Internal_Rela *irela;
2094 Elf_Internal_Rela *irelaend;
2096 Elf_Internal_Shdr *output_rel_hdr;
2097 asection *output_section;
2098 unsigned int *rel_countp = NULL;
2099 const struct elf_backend_data *bed;
2100 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2102 output_section = input_section->output_section;
2103 output_rel_hdr = NULL;
2105 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2106 == input_rel_hdr->sh_entsize)
2108 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2109 rel_countp = &elf_section_data (output_section)->rel_count;
2111 else if (elf_section_data (output_section)->rel_hdr2
2112 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2113 == input_rel_hdr->sh_entsize))
2115 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2116 rel_countp = &elf_section_data (output_section)->rel_count2;
2120 (*_bfd_error_handler)
2121 (_("%B: relocation size mismatch in %B section %A"),
2122 output_bfd, input_section->owner, input_section);
2123 bfd_set_error (bfd_error_wrong_object_format);
2127 bed = get_elf_backend_data (output_bfd);
2128 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2129 swap_out = bed->s->swap_reloc_out;
2130 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2131 swap_out = bed->s->swap_reloca_out;
2135 erel = output_rel_hdr->contents;
2136 erel += *rel_countp * input_rel_hdr->sh_entsize;
2137 irela = internal_relocs;
2138 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2139 * bed->s->int_rels_per_ext_rel);
2140 while (irela < irelaend)
2142 (*swap_out) (output_bfd, irela, erel);
2143 irela += bed->s->int_rels_per_ext_rel;
2144 erel += input_rel_hdr->sh_entsize;
2147 /* Bump the counter, so that we know where to add the next set of
2149 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2154 /* Fix up the flags for a symbol. This handles various cases which
2155 can only be fixed after all the input files are seen. This is
2156 currently called by both adjust_dynamic_symbol and
2157 assign_sym_version, which is unnecessary but perhaps more robust in
2158 the face of future changes. */
2161 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2162 struct elf_info_failed *eif)
2164 /* If this symbol was mentioned in a non-ELF file, try to set
2165 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2166 permit a non-ELF file to correctly refer to a symbol defined in
2167 an ELF dynamic object. */
2170 while (h->root.type == bfd_link_hash_indirect)
2171 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2173 if (h->root.type != bfd_link_hash_defined
2174 && h->root.type != bfd_link_hash_defweak)
2177 h->ref_regular_nonweak = 1;
2181 if (h->root.u.def.section->owner != NULL
2182 && (bfd_get_flavour (h->root.u.def.section->owner)
2183 == bfd_target_elf_flavour))
2186 h->ref_regular_nonweak = 1;
2192 if (h->dynindx == -1
2196 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2205 /* Unfortunately, NON_ELF is only correct if the symbol
2206 was first seen in a non-ELF file. Fortunately, if the symbol
2207 was first seen in an ELF file, we're probably OK unless the
2208 symbol was defined in a non-ELF file. Catch that case here.
2209 FIXME: We're still in trouble if the symbol was first seen in
2210 a dynamic object, and then later in a non-ELF regular object. */
2211 if ((h->root.type == bfd_link_hash_defined
2212 || h->root.type == bfd_link_hash_defweak)
2214 && (h->root.u.def.section->owner != NULL
2215 ? (bfd_get_flavour (h->root.u.def.section->owner)
2216 != bfd_target_elf_flavour)
2217 : (bfd_is_abs_section (h->root.u.def.section)
2218 && !h->def_dynamic)))
2222 /* If this is a final link, and the symbol was defined as a common
2223 symbol in a regular object file, and there was no definition in
2224 any dynamic object, then the linker will have allocated space for
2225 the symbol in a common section but the DEF_REGULAR
2226 flag will not have been set. */
2227 if (h->root.type == bfd_link_hash_defined
2231 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2234 /* If -Bsymbolic was used (which means to bind references to global
2235 symbols to the definition within the shared object), and this
2236 symbol was defined in a regular object, then it actually doesn't
2237 need a PLT entry. Likewise, if the symbol has non-default
2238 visibility. If the symbol has hidden or internal visibility, we
2239 will force it local. */
2241 && eif->info->shared
2242 && is_elf_hash_table (eif->info->hash)
2243 && (eif->info->symbolic
2244 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2247 const struct elf_backend_data *bed;
2248 bfd_boolean force_local;
2250 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2252 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2253 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2254 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2257 /* If a weak undefined symbol has non-default visibility, we also
2258 hide it from the dynamic linker. */
2259 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2260 && h->root.type == bfd_link_hash_undefweak)
2262 const struct elf_backend_data *bed;
2263 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2264 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2267 /* If this is a weak defined symbol in a dynamic object, and we know
2268 the real definition in the dynamic object, copy interesting flags
2269 over to the real definition. */
2270 if (h->u.weakdef != NULL)
2272 struct elf_link_hash_entry *weakdef;
2274 weakdef = h->u.weakdef;
2275 if (h->root.type == bfd_link_hash_indirect)
2276 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2278 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2279 || h->root.type == bfd_link_hash_defweak);
2280 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2281 || weakdef->root.type == bfd_link_hash_defweak);
2282 BFD_ASSERT (weakdef->def_dynamic);
2284 /* If the real definition is defined by a regular object file,
2285 don't do anything special. See the longer description in
2286 _bfd_elf_adjust_dynamic_symbol, below. */
2287 if (weakdef->def_regular)
2288 h->u.weakdef = NULL;
2291 const struct elf_backend_data *bed;
2293 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2294 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2301 /* Make the backend pick a good value for a dynamic symbol. This is
2302 called via elf_link_hash_traverse, and also calls itself
2306 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2308 struct elf_info_failed *eif = data;
2310 const struct elf_backend_data *bed;
2312 if (! is_elf_hash_table (eif->info->hash))
2315 if (h->root.type == bfd_link_hash_warning)
2317 h->plt = elf_hash_table (eif->info)->init_offset;
2318 h->got = elf_hash_table (eif->info)->init_offset;
2320 /* When warning symbols are created, they **replace** the "real"
2321 entry in the hash table, thus we never get to see the real
2322 symbol in a hash traversal. So look at it now. */
2323 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2326 /* Ignore indirect symbols. These are added by the versioning code. */
2327 if (h->root.type == bfd_link_hash_indirect)
2330 /* Fix the symbol flags. */
2331 if (! _bfd_elf_fix_symbol_flags (h, eif))
2334 /* If this symbol does not require a PLT entry, and it is not
2335 defined by a dynamic object, or is not referenced by a regular
2336 object, ignore it. We do have to handle a weak defined symbol,
2337 even if no regular object refers to it, if we decided to add it
2338 to the dynamic symbol table. FIXME: Do we normally need to worry
2339 about symbols which are defined by one dynamic object and
2340 referenced by another one? */
2345 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2347 h->plt = elf_hash_table (eif->info)->init_offset;
2351 /* If we've already adjusted this symbol, don't do it again. This
2352 can happen via a recursive call. */
2353 if (h->dynamic_adjusted)
2356 /* Don't look at this symbol again. Note that we must set this
2357 after checking the above conditions, because we may look at a
2358 symbol once, decide not to do anything, and then get called
2359 recursively later after REF_REGULAR is set below. */
2360 h->dynamic_adjusted = 1;
2362 /* If this is a weak definition, and we know a real definition, and
2363 the real symbol is not itself defined by a regular object file,
2364 then get a good value for the real definition. We handle the
2365 real symbol first, for the convenience of the backend routine.
2367 Note that there is a confusing case here. If the real definition
2368 is defined by a regular object file, we don't get the real symbol
2369 from the dynamic object, but we do get the weak symbol. If the
2370 processor backend uses a COPY reloc, then if some routine in the
2371 dynamic object changes the real symbol, we will not see that
2372 change in the corresponding weak symbol. This is the way other
2373 ELF linkers work as well, and seems to be a result of the shared
2376 I will clarify this issue. Most SVR4 shared libraries define the
2377 variable _timezone and define timezone as a weak synonym. The
2378 tzset call changes _timezone. If you write
2379 extern int timezone;
2381 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2382 you might expect that, since timezone is a synonym for _timezone,
2383 the same number will print both times. However, if the processor
2384 backend uses a COPY reloc, then actually timezone will be copied
2385 into your process image, and, since you define _timezone
2386 yourself, _timezone will not. Thus timezone and _timezone will
2387 wind up at different memory locations. The tzset call will set
2388 _timezone, leaving timezone unchanged. */
2390 if (h->u.weakdef != NULL)
2392 /* If we get to this point, we know there is an implicit
2393 reference by a regular object file via the weak symbol H.
2394 FIXME: Is this really true? What if the traversal finds
2395 H->U.WEAKDEF before it finds H? */
2396 h->u.weakdef->ref_regular = 1;
2398 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2402 /* If a symbol has no type and no size and does not require a PLT
2403 entry, then we are probably about to do the wrong thing here: we
2404 are probably going to create a COPY reloc for an empty object.
2405 This case can arise when a shared object is built with assembly
2406 code, and the assembly code fails to set the symbol type. */
2408 && h->type == STT_NOTYPE
2410 (*_bfd_error_handler)
2411 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2412 h->root.root.string);
2414 dynobj = elf_hash_table (eif->info)->dynobj;
2415 bed = get_elf_backend_data (dynobj);
2416 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2425 /* Adjust all external symbols pointing into SEC_MERGE sections
2426 to reflect the object merging within the sections. */
2429 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2433 if (h->root.type == bfd_link_hash_warning)
2434 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2436 if ((h->root.type == bfd_link_hash_defined
2437 || h->root.type == bfd_link_hash_defweak)
2438 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2439 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2441 bfd *output_bfd = data;
2443 h->root.u.def.value =
2444 _bfd_merged_section_offset (output_bfd,
2445 &h->root.u.def.section,
2446 elf_section_data (sec)->sec_info,
2447 h->root.u.def.value);
2453 /* Returns false if the symbol referred to by H should be considered
2454 to resolve local to the current module, and true if it should be
2455 considered to bind dynamically. */
2458 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2459 struct bfd_link_info *info,
2460 bfd_boolean ignore_protected)
2462 bfd_boolean binding_stays_local_p;
2467 while (h->root.type == bfd_link_hash_indirect
2468 || h->root.type == bfd_link_hash_warning)
2469 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2471 /* If it was forced local, then clearly it's not dynamic. */
2472 if (h->dynindx == -1)
2474 if (h->forced_local)
2477 /* Identify the cases where name binding rules say that a
2478 visible symbol resolves locally. */
2479 binding_stays_local_p = info->executable || info->symbolic;
2481 switch (ELF_ST_VISIBILITY (h->other))
2488 /* Proper resolution for function pointer equality may require
2489 that these symbols perhaps be resolved dynamically, even though
2490 we should be resolving them to the current module. */
2491 if (!ignore_protected || h->type != STT_FUNC)
2492 binding_stays_local_p = TRUE;
2499 /* If it isn't defined locally, then clearly it's dynamic. */
2500 if (!h->def_regular)
2503 /* Otherwise, the symbol is dynamic if binding rules don't tell
2504 us that it remains local. */
2505 return !binding_stays_local_p;
2508 /* Return true if the symbol referred to by H should be considered
2509 to resolve local to the current module, and false otherwise. Differs
2510 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2511 undefined symbols and weak symbols. */
2514 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2515 struct bfd_link_info *info,
2516 bfd_boolean local_protected)
2518 /* If it's a local sym, of course we resolve locally. */
2522 /* Common symbols that become definitions don't get the DEF_REGULAR
2523 flag set, so test it first, and don't bail out. */
2524 if (ELF_COMMON_DEF_P (h))
2526 /* If we don't have a definition in a regular file, then we can't
2527 resolve locally. The sym is either undefined or dynamic. */
2528 else if (!h->def_regular)
2531 /* Forced local symbols resolve locally. */
2532 if (h->forced_local)
2535 /* As do non-dynamic symbols. */
2536 if (h->dynindx == -1)
2539 /* At this point, we know the symbol is defined and dynamic. In an
2540 executable it must resolve locally, likewise when building symbolic
2541 shared libraries. */
2542 if (info->executable || info->symbolic)
2545 /* Now deal with defined dynamic symbols in shared libraries. Ones
2546 with default visibility might not resolve locally. */
2547 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2550 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2551 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2554 /* STV_PROTECTED non-function symbols are local. */
2555 if (h->type != STT_FUNC)
2558 /* Function pointer equality tests may require that STV_PROTECTED
2559 symbols be treated as dynamic symbols, even when we know that the
2560 dynamic linker will resolve them locally. */
2561 return local_protected;
2564 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2565 aligned. Returns the first TLS output section. */
2567 struct bfd_section *
2568 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2570 struct bfd_section *sec, *tls;
2571 unsigned int align = 0;
2573 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2574 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2578 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2579 if (sec->alignment_power > align)
2580 align = sec->alignment_power;
2582 elf_hash_table (info)->tls_sec = tls;
2584 /* Ensure the alignment of the first section is the largest alignment,
2585 so that the tls segment starts aligned. */
2587 tls->alignment_power = align;
2592 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2594 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2595 Elf_Internal_Sym *sym)
2597 /* Local symbols do not count, but target specific ones might. */
2598 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2599 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2602 /* Function symbols do not count. */
2603 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2606 /* If the section is undefined, then so is the symbol. */
2607 if (sym->st_shndx == SHN_UNDEF)
2610 /* If the symbol is defined in the common section, then
2611 it is a common definition and so does not count. */
2612 if (sym->st_shndx == SHN_COMMON)
2615 /* If the symbol is in a target specific section then we
2616 must rely upon the backend to tell us what it is. */
2617 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2618 /* FIXME - this function is not coded yet:
2620 return _bfd_is_global_symbol_definition (abfd, sym);
2622 Instead for now assume that the definition is not global,
2623 Even if this is wrong, at least the linker will behave
2624 in the same way that it used to do. */
2630 /* Search the symbol table of the archive element of the archive ABFD
2631 whose archive map contains a mention of SYMDEF, and determine if
2632 the symbol is defined in this element. */
2634 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2636 Elf_Internal_Shdr * hdr;
2637 bfd_size_type symcount;
2638 bfd_size_type extsymcount;
2639 bfd_size_type extsymoff;
2640 Elf_Internal_Sym *isymbuf;
2641 Elf_Internal_Sym *isym;
2642 Elf_Internal_Sym *isymend;
2645 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2649 if (! bfd_check_format (abfd, bfd_object))
2652 /* If we have already included the element containing this symbol in the
2653 link then we do not need to include it again. Just claim that any symbol
2654 it contains is not a definition, so that our caller will not decide to
2655 (re)include this element. */
2656 if (abfd->archive_pass)
2659 /* Select the appropriate symbol table. */
2660 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2661 hdr = &elf_tdata (abfd)->symtab_hdr;
2663 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2665 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2667 /* The sh_info field of the symtab header tells us where the
2668 external symbols start. We don't care about the local symbols. */
2669 if (elf_bad_symtab (abfd))
2671 extsymcount = symcount;
2676 extsymcount = symcount - hdr->sh_info;
2677 extsymoff = hdr->sh_info;
2680 if (extsymcount == 0)
2683 /* Read in the symbol table. */
2684 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2686 if (isymbuf == NULL)
2689 /* Scan the symbol table looking for SYMDEF. */
2691 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2695 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2700 if (strcmp (name, symdef->name) == 0)
2702 result = is_global_data_symbol_definition (abfd, isym);
2712 /* Add an entry to the .dynamic table. */
2715 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2719 struct elf_link_hash_table *hash_table;
2720 const struct elf_backend_data *bed;
2722 bfd_size_type newsize;
2723 bfd_byte *newcontents;
2724 Elf_Internal_Dyn dyn;
2726 hash_table = elf_hash_table (info);
2727 if (! is_elf_hash_table (hash_table))
2730 if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
2732 (_("warning: creating a DT_TEXTREL in a shared object."));
2734 bed = get_elf_backend_data (hash_table->dynobj);
2735 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2736 BFD_ASSERT (s != NULL);
2738 newsize = s->size + bed->s->sizeof_dyn;
2739 newcontents = bfd_realloc (s->contents, newsize);
2740 if (newcontents == NULL)
2744 dyn.d_un.d_val = val;
2745 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2748 s->contents = newcontents;
2753 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2754 otherwise just check whether one already exists. Returns -1 on error,
2755 1 if a DT_NEEDED tag already exists, and 0 on success. */
2758 elf_add_dt_needed_tag (bfd *abfd,
2759 struct bfd_link_info *info,
2763 struct elf_link_hash_table *hash_table;
2764 bfd_size_type oldsize;
2765 bfd_size_type strindex;
2767 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2770 hash_table = elf_hash_table (info);
2771 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2772 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2773 if (strindex == (bfd_size_type) -1)
2776 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2779 const struct elf_backend_data *bed;
2782 bed = get_elf_backend_data (hash_table->dynobj);
2783 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2785 for (extdyn = sdyn->contents;
2786 extdyn < sdyn->contents + sdyn->size;
2787 extdyn += bed->s->sizeof_dyn)
2789 Elf_Internal_Dyn dyn;
2791 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2792 if (dyn.d_tag == DT_NEEDED
2793 && dyn.d_un.d_val == strindex)
2795 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2803 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
2806 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2810 /* We were just checking for existence of the tag. */
2811 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2816 /* Sort symbol by value and section. */
2818 elf_sort_symbol (const void *arg1, const void *arg2)
2820 const struct elf_link_hash_entry *h1;
2821 const struct elf_link_hash_entry *h2;
2822 bfd_signed_vma vdiff;
2824 h1 = *(const struct elf_link_hash_entry **) arg1;
2825 h2 = *(const struct elf_link_hash_entry **) arg2;
2826 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2828 return vdiff > 0 ? 1 : -1;
2831 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2833 return sdiff > 0 ? 1 : -1;
2838 /* This function is used to adjust offsets into .dynstr for
2839 dynamic symbols. This is called via elf_link_hash_traverse. */
2842 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2844 struct elf_strtab_hash *dynstr = data;
2846 if (h->root.type == bfd_link_hash_warning)
2847 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2849 if (h->dynindx != -1)
2850 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2854 /* Assign string offsets in .dynstr, update all structures referencing
2858 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2860 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2861 struct elf_link_local_dynamic_entry *entry;
2862 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2863 bfd *dynobj = hash_table->dynobj;
2866 const struct elf_backend_data *bed;
2869 _bfd_elf_strtab_finalize (dynstr);
2870 size = _bfd_elf_strtab_size (dynstr);
2872 bed = get_elf_backend_data (dynobj);
2873 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2874 BFD_ASSERT (sdyn != NULL);
2876 /* Update all .dynamic entries referencing .dynstr strings. */
2877 for (extdyn = sdyn->contents;
2878 extdyn < sdyn->contents + sdyn->size;
2879 extdyn += bed->s->sizeof_dyn)
2881 Elf_Internal_Dyn dyn;
2883 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
2887 dyn.d_un.d_val = size;
2895 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2900 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
2903 /* Now update local dynamic symbols. */
2904 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
2905 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2906 entry->isym.st_name);
2908 /* And the rest of dynamic symbols. */
2909 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
2911 /* Adjust version definitions. */
2912 if (elf_tdata (output_bfd)->cverdefs)
2917 Elf_Internal_Verdef def;
2918 Elf_Internal_Verdaux defaux;
2920 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2924 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2926 p += sizeof (Elf_External_Verdef);
2927 if (def.vd_aux != sizeof (Elf_External_Verdef))
2929 for (i = 0; i < def.vd_cnt; ++i)
2931 _bfd_elf_swap_verdaux_in (output_bfd,
2932 (Elf_External_Verdaux *) p, &defaux);
2933 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2935 _bfd_elf_swap_verdaux_out (output_bfd,
2936 &defaux, (Elf_External_Verdaux *) p);
2937 p += sizeof (Elf_External_Verdaux);
2940 while (def.vd_next);
2943 /* Adjust version references. */
2944 if (elf_tdata (output_bfd)->verref)
2949 Elf_Internal_Verneed need;
2950 Elf_Internal_Vernaux needaux;
2952 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2956 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2958 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2959 _bfd_elf_swap_verneed_out (output_bfd, &need,
2960 (Elf_External_Verneed *) p);
2961 p += sizeof (Elf_External_Verneed);
2962 for (i = 0; i < need.vn_cnt; ++i)
2964 _bfd_elf_swap_vernaux_in (output_bfd,
2965 (Elf_External_Vernaux *) p, &needaux);
2966 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2968 _bfd_elf_swap_vernaux_out (output_bfd,
2970 (Elf_External_Vernaux *) p);
2971 p += sizeof (Elf_External_Vernaux);
2974 while (need.vn_next);
2980 /* Add symbols from an ELF object file to the linker hash table. */
2983 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2985 bfd_boolean (*add_symbol_hook)
2986 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
2987 const char **, flagword *, asection **, bfd_vma *);
2988 bfd_boolean (*check_relocs)
2989 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
2990 bfd_boolean (*check_directives)
2991 (bfd *, struct bfd_link_info *);
2992 bfd_boolean collect;
2993 Elf_Internal_Shdr *hdr;
2994 bfd_size_type symcount;
2995 bfd_size_type extsymcount;
2996 bfd_size_type extsymoff;
2997 struct elf_link_hash_entry **sym_hash;
2998 bfd_boolean dynamic;
2999 Elf_External_Versym *extversym = NULL;
3000 Elf_External_Versym *ever;
3001 struct elf_link_hash_entry *weaks;
3002 struct elf_link_hash_entry **nondeflt_vers = NULL;
3003 bfd_size_type nondeflt_vers_cnt = 0;
3004 Elf_Internal_Sym *isymbuf = NULL;
3005 Elf_Internal_Sym *isym;
3006 Elf_Internal_Sym *isymend;
3007 const struct elf_backend_data *bed;
3008 bfd_boolean add_needed;
3009 struct elf_link_hash_table * hash_table;
3012 hash_table = elf_hash_table (info);
3014 bed = get_elf_backend_data (abfd);
3015 add_symbol_hook = bed->elf_add_symbol_hook;
3016 collect = bed->collect;
3018 if ((abfd->flags & DYNAMIC) == 0)
3024 /* You can't use -r against a dynamic object. Also, there's no
3025 hope of using a dynamic object which does not exactly match
3026 the format of the output file. */
3027 if (info->relocatable
3028 || !is_elf_hash_table (hash_table)
3029 || hash_table->root.creator != abfd->xvec)
3031 if (info->relocatable)
3032 bfd_set_error (bfd_error_invalid_operation);
3034 bfd_set_error (bfd_error_wrong_format);
3039 /* As a GNU extension, any input sections which are named
3040 .gnu.warning.SYMBOL are treated as warning symbols for the given
3041 symbol. This differs from .gnu.warning sections, which generate
3042 warnings when they are included in an output file. */
3043 if (info->executable)
3047 for (s = abfd->sections; s != NULL; s = s->next)
3051 name = bfd_get_section_name (abfd, s);
3052 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3056 bfd_size_type prefix_len;
3057 const char * gnu_warning_prefix = _("warning: ");
3059 name += sizeof ".gnu.warning." - 1;
3061 /* If this is a shared object, then look up the symbol
3062 in the hash table. If it is there, and it is already
3063 been defined, then we will not be using the entry
3064 from this shared object, so we don't need to warn.
3065 FIXME: If we see the definition in a regular object
3066 later on, we will warn, but we shouldn't. The only
3067 fix is to keep track of what warnings we are supposed
3068 to emit, and then handle them all at the end of the
3072 struct elf_link_hash_entry *h;
3074 h = elf_link_hash_lookup (hash_table, name,
3075 FALSE, FALSE, TRUE);
3077 /* FIXME: What about bfd_link_hash_common? */
3079 && (h->root.type == bfd_link_hash_defined
3080 || h->root.type == bfd_link_hash_defweak))
3082 /* We don't want to issue this warning. Clobber
3083 the section size so that the warning does not
3084 get copied into the output file. */
3091 prefix_len = strlen (gnu_warning_prefix);
3092 msg = bfd_alloc (abfd, prefix_len + sz + 1);
3096 strcpy (msg, gnu_warning_prefix);
3097 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
3100 msg[prefix_len + sz] = '\0';
3102 if (! (_bfd_generic_link_add_one_symbol
3103 (info, abfd, name, BSF_WARNING, s, 0, msg,
3104 FALSE, collect, NULL)))
3107 if (! info->relocatable)
3109 /* Clobber the section size so that the warning does
3110 not get copied into the output file. */
3120 /* If we are creating a shared library, create all the dynamic
3121 sections immediately. We need to attach them to something,
3122 so we attach them to this BFD, provided it is the right
3123 format. FIXME: If there are no input BFD's of the same
3124 format as the output, we can't make a shared library. */
3126 && is_elf_hash_table (hash_table)
3127 && hash_table->root.creator == abfd->xvec
3128 && ! hash_table->dynamic_sections_created)
3130 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3134 else if (!is_elf_hash_table (hash_table))
3139 const char *soname = NULL;
3140 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3143 /* ld --just-symbols and dynamic objects don't mix very well.
3144 Test for --just-symbols by looking at info set up by
3145 _bfd_elf_link_just_syms. */
3146 if ((s = abfd->sections) != NULL
3147 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3150 /* If this dynamic lib was specified on the command line with
3151 --as-needed in effect, then we don't want to add a DT_NEEDED
3152 tag unless the lib is actually used. Similary for libs brought
3153 in by another lib's DT_NEEDED. When --no-add-needed is used
3154 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3155 any dynamic library in DT_NEEDED tags in the dynamic lib at
3157 add_needed = (elf_dyn_lib_class (abfd)
3158 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3159 | DYN_NO_NEEDED)) == 0;
3161 s = bfd_get_section_by_name (abfd, ".dynamic");
3167 unsigned long shlink;
3169 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3170 goto error_free_dyn;
3172 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3174 goto error_free_dyn;
3175 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3177 for (extdyn = dynbuf;
3178 extdyn < dynbuf + s->size;
3179 extdyn += bed->s->sizeof_dyn)
3181 Elf_Internal_Dyn dyn;
3183 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3184 if (dyn.d_tag == DT_SONAME)
3186 unsigned int tagv = dyn.d_un.d_val;
3187 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3189 goto error_free_dyn;
3191 if (dyn.d_tag == DT_NEEDED)
3193 struct bfd_link_needed_list *n, **pn;
3195 unsigned int tagv = dyn.d_un.d_val;
3197 amt = sizeof (struct bfd_link_needed_list);
3198 n = bfd_alloc (abfd, amt);
3199 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3200 if (n == NULL || fnm == NULL)
3201 goto error_free_dyn;
3202 amt = strlen (fnm) + 1;
3203 anm = bfd_alloc (abfd, amt);
3205 goto error_free_dyn;
3206 memcpy (anm, fnm, amt);
3210 for (pn = & hash_table->needed;
3216 if (dyn.d_tag == DT_RUNPATH)
3218 struct bfd_link_needed_list *n, **pn;
3220 unsigned int tagv = dyn.d_un.d_val;
3222 amt = sizeof (struct bfd_link_needed_list);
3223 n = bfd_alloc (abfd, amt);
3224 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3225 if (n == NULL || fnm == NULL)
3226 goto error_free_dyn;
3227 amt = strlen (fnm) + 1;
3228 anm = bfd_alloc (abfd, amt);
3230 goto error_free_dyn;
3231 memcpy (anm, fnm, amt);
3235 for (pn = & runpath;
3241 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3242 if (!runpath && dyn.d_tag == DT_RPATH)
3244 struct bfd_link_needed_list *n, **pn;
3246 unsigned int tagv = dyn.d_un.d_val;
3248 amt = sizeof (struct bfd_link_needed_list);
3249 n = bfd_alloc (abfd, amt);
3250 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3251 if (n == NULL || fnm == NULL)
3252 goto error_free_dyn;
3253 amt = strlen (fnm) + 1;
3254 anm = bfd_alloc (abfd, amt);
3261 memcpy (anm, fnm, amt);
3276 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3277 frees all more recently bfd_alloc'd blocks as well. */
3283 struct bfd_link_needed_list **pn;
3284 for (pn = & hash_table->runpath;
3291 /* We do not want to include any of the sections in a dynamic
3292 object in the output file. We hack by simply clobbering the
3293 list of sections in the BFD. This could be handled more
3294 cleanly by, say, a new section flag; the existing
3295 SEC_NEVER_LOAD flag is not the one we want, because that one
3296 still implies that the section takes up space in the output
3298 bfd_section_list_clear (abfd);
3300 /* Find the name to use in a DT_NEEDED entry that refers to this
3301 object. If the object has a DT_SONAME entry, we use it.
3302 Otherwise, if the generic linker stuck something in
3303 elf_dt_name, we use that. Otherwise, we just use the file
3305 if (soname == NULL || *soname == '\0')
3307 soname = elf_dt_name (abfd);
3308 if (soname == NULL || *soname == '\0')
3309 soname = bfd_get_filename (abfd);
3312 /* Save the SONAME because sometimes the linker emulation code
3313 will need to know it. */
3314 elf_dt_name (abfd) = soname;
3316 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3320 /* If we have already included this dynamic object in the
3321 link, just ignore it. There is no reason to include a
3322 particular dynamic object more than once. */
3327 /* If this is a dynamic object, we always link against the .dynsym
3328 symbol table, not the .symtab symbol table. The dynamic linker
3329 will only see the .dynsym symbol table, so there is no reason to
3330 look at .symtab for a dynamic object. */
3332 if (! dynamic || elf_dynsymtab (abfd) == 0)
3333 hdr = &elf_tdata (abfd)->symtab_hdr;
3335 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3337 symcount = hdr->sh_size / bed->s->sizeof_sym;
3339 /* The sh_info field of the symtab header tells us where the
3340 external symbols start. We don't care about the local symbols at
3342 if (elf_bad_symtab (abfd))
3344 extsymcount = symcount;
3349 extsymcount = symcount - hdr->sh_info;
3350 extsymoff = hdr->sh_info;
3354 if (extsymcount != 0)
3356 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3358 if (isymbuf == NULL)
3361 /* We store a pointer to the hash table entry for each external
3363 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3364 sym_hash = bfd_alloc (abfd, amt);
3365 if (sym_hash == NULL)
3366 goto error_free_sym;
3367 elf_sym_hashes (abfd) = sym_hash;
3372 /* Read in any version definitions. */
3373 if (!_bfd_elf_slurp_version_tables (abfd,
3374 info->default_imported_symver))
3375 goto error_free_sym;
3377 /* Read in the symbol versions, but don't bother to convert them
3378 to internal format. */
3379 if (elf_dynversym (abfd) != 0)
3381 Elf_Internal_Shdr *versymhdr;
3383 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3384 extversym = bfd_malloc (versymhdr->sh_size);
3385 if (extversym == NULL)
3386 goto error_free_sym;
3387 amt = versymhdr->sh_size;
3388 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3389 || bfd_bread (extversym, amt, abfd) != amt)
3390 goto error_free_vers;
3396 ever = extversym != NULL ? extversym + extsymoff : NULL;
3397 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3399 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3406 struct elf_link_hash_entry *h;
3407 bfd_boolean definition;
3408 bfd_boolean size_change_ok;
3409 bfd_boolean type_change_ok;
3410 bfd_boolean new_weakdef;
3411 bfd_boolean override;
3412 unsigned int old_alignment;
3417 flags = BSF_NO_FLAGS;
3419 value = isym->st_value;
3422 bind = ELF_ST_BIND (isym->st_info);
3423 if (bind == STB_LOCAL)
3425 /* This should be impossible, since ELF requires that all
3426 global symbols follow all local symbols, and that sh_info
3427 point to the first global symbol. Unfortunately, Irix 5
3431 else if (bind == STB_GLOBAL)
3433 if (isym->st_shndx != SHN_UNDEF
3434 && isym->st_shndx != SHN_COMMON)
3437 else if (bind == STB_WEAK)
3441 /* Leave it up to the processor backend. */
3444 if (isym->st_shndx == SHN_UNDEF)
3445 sec = bfd_und_section_ptr;
3446 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3448 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3450 sec = bfd_abs_section_ptr;
3451 else if (sec->kept_section)
3453 /* Symbols from discarded section are undefined. */
3454 sec = bfd_und_section_ptr;
3455 isym->st_shndx = SHN_UNDEF;
3457 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3460 else if (isym->st_shndx == SHN_ABS)
3461 sec = bfd_abs_section_ptr;
3462 else if (isym->st_shndx == SHN_COMMON)
3464 sec = bfd_com_section_ptr;
3465 /* What ELF calls the size we call the value. What ELF
3466 calls the value we call the alignment. */
3467 value = isym->st_size;
3471 /* Leave it up to the processor backend. */
3474 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3477 goto error_free_vers;
3479 if (isym->st_shndx == SHN_COMMON
3480 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3482 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3486 tcomm = bfd_make_section (abfd, ".tcommon");
3488 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
3490 | SEC_LINKER_CREATED
3491 | SEC_THREAD_LOCAL)))
3492 goto error_free_vers;
3496 else if (add_symbol_hook)
3498 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3500 goto error_free_vers;
3502 /* The hook function sets the name to NULL if this symbol
3503 should be skipped for some reason. */
3508 /* Sanity check that all possibilities were handled. */
3511 bfd_set_error (bfd_error_bad_value);
3512 goto error_free_vers;
3515 if (bfd_is_und_section (sec)
3516 || bfd_is_com_section (sec))
3521 size_change_ok = FALSE;
3522 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3526 if (is_elf_hash_table (hash_table))
3528 Elf_Internal_Versym iver;
3529 unsigned int vernum = 0;
3534 if (info->default_imported_symver)
3535 /* Use the default symbol version created earlier. */
3536 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3541 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3543 vernum = iver.vs_vers & VERSYM_VERSION;
3545 /* If this is a hidden symbol, or if it is not version
3546 1, we append the version name to the symbol name.
3547 However, we do not modify a non-hidden absolute
3548 symbol, because it might be the version symbol
3549 itself. FIXME: What if it isn't? */
3550 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3551 || (vernum > 1 && ! bfd_is_abs_section (sec)))
3554 size_t namelen, verlen, newlen;
3557 if (isym->st_shndx != SHN_UNDEF)
3559 if (vernum > elf_tdata (abfd)->cverdefs)
3561 else if (vernum > 1)
3563 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3569 (*_bfd_error_handler)
3570 (_("%B: %s: invalid version %u (max %d)"),
3572 elf_tdata (abfd)->cverdefs);
3573 bfd_set_error (bfd_error_bad_value);
3574 goto error_free_vers;
3579 /* We cannot simply test for the number of
3580 entries in the VERNEED section since the
3581 numbers for the needed versions do not start
3583 Elf_Internal_Verneed *t;
3586 for (t = elf_tdata (abfd)->verref;
3590 Elf_Internal_Vernaux *a;
3592 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3594 if (a->vna_other == vernum)
3596 verstr = a->vna_nodename;
3605 (*_bfd_error_handler)
3606 (_("%B: %s: invalid needed version %d"),
3607 abfd, name, vernum);
3608 bfd_set_error (bfd_error_bad_value);
3609 goto error_free_vers;
3613 namelen = strlen (name);
3614 verlen = strlen (verstr);
3615 newlen = namelen + verlen + 2;
3616 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3617 && isym->st_shndx != SHN_UNDEF)
3620 newname = bfd_alloc (abfd, newlen);
3621 if (newname == NULL)
3622 goto error_free_vers;
3623 memcpy (newname, name, namelen);
3624 p = newname + namelen;
3626 /* If this is a defined non-hidden version symbol,
3627 we add another @ to the name. This indicates the
3628 default version of the symbol. */
3629 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3630 && isym->st_shndx != SHN_UNDEF)
3632 memcpy (p, verstr, verlen + 1);
3637 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
3638 sym_hash, &skip, &override,
3639 &type_change_ok, &size_change_ok))
3640 goto error_free_vers;
3649 while (h->root.type == bfd_link_hash_indirect
3650 || h->root.type == bfd_link_hash_warning)
3651 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3653 /* Remember the old alignment if this is a common symbol, so
3654 that we don't reduce the alignment later on. We can't
3655 check later, because _bfd_generic_link_add_one_symbol
3656 will set a default for the alignment which we want to
3657 override. We also remember the old bfd where the existing
3658 definition comes from. */
3659 switch (h->root.type)
3664 case bfd_link_hash_defined:
3665 case bfd_link_hash_defweak:
3666 old_bfd = h->root.u.def.section->owner;
3669 case bfd_link_hash_common:
3670 old_bfd = h->root.u.c.p->section->owner;
3671 old_alignment = h->root.u.c.p->alignment_power;
3675 if (elf_tdata (abfd)->verdef != NULL
3679 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3682 if (! (_bfd_generic_link_add_one_symbol
3683 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3684 (struct bfd_link_hash_entry **) sym_hash)))
3685 goto error_free_vers;
3688 while (h->root.type == bfd_link_hash_indirect
3689 || h->root.type == bfd_link_hash_warning)
3690 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3693 new_weakdef = FALSE;
3696 && (flags & BSF_WEAK) != 0
3697 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3698 && is_elf_hash_table (hash_table)
3699 && h->u.weakdef == NULL)
3701 /* Keep a list of all weak defined non function symbols from
3702 a dynamic object, using the weakdef field. Later in this
3703 function we will set the weakdef field to the correct
3704 value. We only put non-function symbols from dynamic
3705 objects on this list, because that happens to be the only
3706 time we need to know the normal symbol corresponding to a
3707 weak symbol, and the information is time consuming to
3708 figure out. If the weakdef field is not already NULL,
3709 then this symbol was already defined by some previous
3710 dynamic object, and we will be using that previous
3711 definition anyhow. */
3713 h->u.weakdef = weaks;
3718 /* Set the alignment of a common symbol. */
3719 if (isym->st_shndx == SHN_COMMON
3720 && h->root.type == bfd_link_hash_common)
3724 align = bfd_log2 (isym->st_value);
3725 if (align > old_alignment
3726 /* Permit an alignment power of zero if an alignment of one
3727 is specified and no other alignments have been specified. */
3728 || (isym->st_value == 1 && old_alignment == 0))
3729 h->root.u.c.p->alignment_power = align;
3731 h->root.u.c.p->alignment_power = old_alignment;
3734 if (is_elf_hash_table (hash_table))
3738 /* Check the alignment when a common symbol is involved. This
3739 can change when a common symbol is overridden by a normal
3740 definition or a common symbol is ignored due to the old
3741 normal definition. We need to make sure the maximum
3742 alignment is maintained. */
3743 if ((old_alignment || isym->st_shndx == SHN_COMMON)
3744 && h->root.type != bfd_link_hash_common)
3746 unsigned int common_align;
3747 unsigned int normal_align;
3748 unsigned int symbol_align;
3752 symbol_align = ffs (h->root.u.def.value) - 1;
3753 if (h->root.u.def.section->owner != NULL
3754 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3756 normal_align = h->root.u.def.section->alignment_power;
3757 if (normal_align > symbol_align)
3758 normal_align = symbol_align;
3761 normal_align = symbol_align;
3765 common_align = old_alignment;
3766 common_bfd = old_bfd;
3771 common_align = bfd_log2 (isym->st_value);
3773 normal_bfd = old_bfd;
3776 if (normal_align < common_align)
3777 (*_bfd_error_handler)
3778 (_("Warning: alignment %u of symbol `%s' in %B"
3779 " is smaller than %u in %B"),
3780 normal_bfd, common_bfd,
3781 1 << normal_align, name, 1 << common_align);
3784 /* Remember the symbol size and type. */
3785 if (isym->st_size != 0
3786 && (definition || h->size == 0))
3788 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3789 (*_bfd_error_handler)
3790 (_("Warning: size of symbol `%s' changed"
3791 " from %lu in %B to %lu in %B"),
3793 name, (unsigned long) h->size,
3794 (unsigned long) isym->st_size);
3796 h->size = isym->st_size;
3799 /* If this is a common symbol, then we always want H->SIZE
3800 to be the size of the common symbol. The code just above
3801 won't fix the size if a common symbol becomes larger. We
3802 don't warn about a size change here, because that is
3803 covered by --warn-common. */
3804 if (h->root.type == bfd_link_hash_common)
3805 h->size = h->root.u.c.size;
3807 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3808 && (definition || h->type == STT_NOTYPE))
3810 if (h->type != STT_NOTYPE
3811 && h->type != ELF_ST_TYPE (isym->st_info)
3812 && ! type_change_ok)
3813 (*_bfd_error_handler)
3814 (_("Warning: type of symbol `%s' changed"
3815 " from %d to %d in %B"),
3816 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3818 h->type = ELF_ST_TYPE (isym->st_info);
3821 /* If st_other has a processor-specific meaning, specific
3822 code might be needed here. We never merge the visibility
3823 attribute with the one from a dynamic object. */
3824 if (bed->elf_backend_merge_symbol_attribute)
3825 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3828 /* If this symbol has default visibility and the user has requested
3829 we not re-export it, then mark it as hidden. */
3830 if (definition && !dynamic
3832 || (abfd->my_archive && abfd->my_archive->no_export))
3833 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
3834 isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1));
3836 if (isym->st_other != 0 && !dynamic)
3838 unsigned char hvis, symvis, other, nvis;
3840 /* Take the balance of OTHER from the definition. */
3841 other = (definition ? isym->st_other : h->other);
3842 other &= ~ ELF_ST_VISIBILITY (-1);
3844 /* Combine visibilities, using the most constraining one. */
3845 hvis = ELF_ST_VISIBILITY (h->other);
3846 symvis = ELF_ST_VISIBILITY (isym->st_other);
3852 nvis = hvis < symvis ? hvis : symvis;
3854 h->other = other | nvis;
3857 /* Set a flag in the hash table entry indicating the type of
3858 reference or definition we just found. Keep a count of
3859 the number of dynamic symbols we find. A dynamic symbol
3860 is one which is referenced or defined by both a regular
3861 object and a shared object. */
3868 if (bind != STB_WEAK)
3869 h->ref_regular_nonweak = 1;
3873 if (! info->executable
3886 || (h->u.weakdef != NULL
3888 && h->u.weakdef->dynindx != -1))
3892 /* Check to see if we need to add an indirect symbol for
3893 the default name. */
3894 if (definition || h->root.type == bfd_link_hash_common)
3895 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
3896 &sec, &value, &dynsym,
3898 goto error_free_vers;
3900 if (definition && !dynamic)
3902 char *p = strchr (name, ELF_VER_CHR);
3903 if (p != NULL && p[1] != ELF_VER_CHR)
3905 /* Queue non-default versions so that .symver x, x@FOO
3906 aliases can be checked. */
3907 if (! nondeflt_vers)
3909 amt = (isymend - isym + 1)
3910 * sizeof (struct elf_link_hash_entry *);
3911 nondeflt_vers = bfd_malloc (amt);
3913 nondeflt_vers [nondeflt_vers_cnt++] = h;
3917 if (dynsym && h->dynindx == -1)
3919 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3920 goto error_free_vers;
3921 if (h->u.weakdef != NULL
3923 && h->u.weakdef->dynindx == -1)
3925 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
3926 goto error_free_vers;
3929 else if (dynsym && h->dynindx != -1)
3930 /* If the symbol already has a dynamic index, but
3931 visibility says it should not be visible, turn it into
3933 switch (ELF_ST_VISIBILITY (h->other))
3937 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
3948 const char *soname = elf_dt_name (abfd);
3950 /* A symbol from a library loaded via DT_NEEDED of some
3951 other library is referenced by a regular object.
3952 Add a DT_NEEDED entry for it. Issue an error if
3953 --no-add-needed is used. */
3954 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
3956 (*_bfd_error_handler)
3957 (_("%s: invalid DSO for symbol `%s' definition"),
3959 bfd_set_error (bfd_error_bad_value);
3960 goto error_free_vers;
3963 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
3966 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3968 goto error_free_vers;
3970 BFD_ASSERT (ret == 0);
3975 /* Now that all the symbols from this input file are created, handle
3976 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3977 if (nondeflt_vers != NULL)
3979 bfd_size_type cnt, symidx;
3981 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
3983 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
3984 char *shortname, *p;
3986 p = strchr (h->root.root.string, ELF_VER_CHR);
3988 || (h->root.type != bfd_link_hash_defined
3989 && h->root.type != bfd_link_hash_defweak))
3992 amt = p - h->root.root.string;
3993 shortname = bfd_malloc (amt + 1);
3994 memcpy (shortname, h->root.root.string, amt);
3995 shortname[amt] = '\0';
3997 hi = (struct elf_link_hash_entry *)
3998 bfd_link_hash_lookup (&hash_table->root, shortname,
3999 FALSE, FALSE, FALSE);
4001 && hi->root.type == h->root.type
4002 && hi->root.u.def.value == h->root.u.def.value
4003 && hi->root.u.def.section == h->root.u.def.section)
4005 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4006 hi->root.type = bfd_link_hash_indirect;
4007 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4008 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
4009 sym_hash = elf_sym_hashes (abfd);
4011 for (symidx = 0; symidx < extsymcount; ++symidx)
4012 if (sym_hash[symidx] == hi)
4014 sym_hash[symidx] = h;
4020 free (nondeflt_vers);
4021 nondeflt_vers = NULL;
4024 if (extversym != NULL)
4030 if (isymbuf != NULL)
4034 /* Now set the weakdefs field correctly for all the weak defined
4035 symbols we found. The only way to do this is to search all the
4036 symbols. Since we only need the information for non functions in
4037 dynamic objects, that's the only time we actually put anything on
4038 the list WEAKS. We need this information so that if a regular
4039 object refers to a symbol defined weakly in a dynamic object, the
4040 real symbol in the dynamic object is also put in the dynamic
4041 symbols; we also must arrange for both symbols to point to the
4042 same memory location. We could handle the general case of symbol
4043 aliasing, but a general symbol alias can only be generated in
4044 assembler code, handling it correctly would be very time
4045 consuming, and other ELF linkers don't handle general aliasing
4049 struct elf_link_hash_entry **hpp;
4050 struct elf_link_hash_entry **hppend;
4051 struct elf_link_hash_entry **sorted_sym_hash;
4052 struct elf_link_hash_entry *h;
4055 /* Since we have to search the whole symbol list for each weak
4056 defined symbol, search time for N weak defined symbols will be
4057 O(N^2). Binary search will cut it down to O(NlogN). */
4058 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4059 sorted_sym_hash = bfd_malloc (amt);
4060 if (sorted_sym_hash == NULL)
4062 sym_hash = sorted_sym_hash;
4063 hpp = elf_sym_hashes (abfd);
4064 hppend = hpp + extsymcount;
4066 for (; hpp < hppend; hpp++)
4070 && h->root.type == bfd_link_hash_defined
4071 && h->type != STT_FUNC)
4079 qsort (sorted_sym_hash, sym_count,
4080 sizeof (struct elf_link_hash_entry *),
4083 while (weaks != NULL)
4085 struct elf_link_hash_entry *hlook;
4092 weaks = hlook->u.weakdef;
4093 hlook->u.weakdef = NULL;
4095 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4096 || hlook->root.type == bfd_link_hash_defweak
4097 || hlook->root.type == bfd_link_hash_common
4098 || hlook->root.type == bfd_link_hash_indirect);
4099 slook = hlook->root.u.def.section;
4100 vlook = hlook->root.u.def.value;
4107 bfd_signed_vma vdiff;
4109 h = sorted_sym_hash [idx];
4110 vdiff = vlook - h->root.u.def.value;
4117 long sdiff = slook->id - h->root.u.def.section->id;
4130 /* We didn't find a value/section match. */
4134 for (i = ilook; i < sym_count; i++)
4136 h = sorted_sym_hash [i];
4138 /* Stop if value or section doesn't match. */
4139 if (h->root.u.def.value != vlook
4140 || h->root.u.def.section != slook)
4142 else if (h != hlook)
4144 hlook->u.weakdef = h;
4146 /* If the weak definition is in the list of dynamic
4147 symbols, make sure the real definition is put
4149 if (hlook->dynindx != -1 && h->dynindx == -1)
4151 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4155 /* If the real definition is in the list of dynamic
4156 symbols, make sure the weak definition is put
4157 there as well. If we don't do this, then the
4158 dynamic loader might not merge the entries for the
4159 real definition and the weak definition. */
4160 if (h->dynindx != -1 && hlook->dynindx == -1)
4162 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4170 free (sorted_sym_hash);
4173 check_directives = get_elf_backend_data (abfd)->check_directives;
4174 if (check_directives)
4175 check_directives (abfd, info);
4177 /* If this object is the same format as the output object, and it is
4178 not a shared library, then let the backend look through the
4181 This is required to build global offset table entries and to
4182 arrange for dynamic relocs. It is not required for the
4183 particular common case of linking non PIC code, even when linking
4184 against shared libraries, but unfortunately there is no way of
4185 knowing whether an object file has been compiled PIC or not.
4186 Looking through the relocs is not particularly time consuming.
4187 The problem is that we must either (1) keep the relocs in memory,
4188 which causes the linker to require additional runtime memory or
4189 (2) read the relocs twice from the input file, which wastes time.
4190 This would be a good case for using mmap.
4192 I have no idea how to handle linking PIC code into a file of a
4193 different format. It probably can't be done. */
4194 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4196 && is_elf_hash_table (hash_table)
4197 && hash_table->root.creator == abfd->xvec
4198 && check_relocs != NULL)
4202 for (o = abfd->sections; o != NULL; o = o->next)
4204 Elf_Internal_Rela *internal_relocs;
4207 if ((o->flags & SEC_RELOC) == 0
4208 || o->reloc_count == 0
4209 || ((info->strip == strip_all || info->strip == strip_debugger)
4210 && (o->flags & SEC_DEBUGGING) != 0)
4211 || bfd_is_abs_section (o->output_section))
4214 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4216 if (internal_relocs == NULL)
4219 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4221 if (elf_section_data (o)->relocs != internal_relocs)
4222 free (internal_relocs);
4229 /* If this is a non-traditional link, try to optimize the handling
4230 of the .stab/.stabstr sections. */
4232 && ! info->traditional_format
4233 && is_elf_hash_table (hash_table)
4234 && (info->strip != strip_all && info->strip != strip_debugger))
4238 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4239 if (stabstr != NULL)
4241 bfd_size_type string_offset = 0;
4244 for (stab = abfd->sections; stab; stab = stab->next)
4245 if (strncmp (".stab", stab->name, 5) == 0
4246 && (!stab->name[5] ||
4247 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4248 && (stab->flags & SEC_MERGE) == 0
4249 && !bfd_is_abs_section (stab->output_section))
4251 struct bfd_elf_section_data *secdata;
4253 secdata = elf_section_data (stab);
4254 if (! _bfd_link_section_stabs (abfd,
4255 &hash_table->stab_info,
4260 if (secdata->sec_info)
4261 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4266 if (is_elf_hash_table (hash_table))
4268 /* Add this bfd to the loaded list. */
4269 struct elf_link_loaded_list *n;
4271 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4275 n->next = hash_table->loaded;
4276 hash_table->loaded = n;
4282 if (nondeflt_vers != NULL)
4283 free (nondeflt_vers);
4284 if (extversym != NULL)
4287 if (isymbuf != NULL)
4293 /* Return the linker hash table entry of a symbol that might be
4294 satisfied by an archive symbol. Return -1 on error. */
4296 struct elf_link_hash_entry *
4297 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4298 struct bfd_link_info *info,
4301 struct elf_link_hash_entry *h;
4305 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4309 /* If this is a default version (the name contains @@), look up the
4310 symbol again with only one `@' as well as without the version.
4311 The effect is that references to the symbol with and without the
4312 version will be matched by the default symbol in the archive. */
4314 p = strchr (name, ELF_VER_CHR);
4315 if (p == NULL || p[1] != ELF_VER_CHR)
4318 /* First check with only one `@'. */
4319 len = strlen (name);
4320 copy = bfd_alloc (abfd, len);
4322 return (struct elf_link_hash_entry *) 0 - 1;
4324 first = p - name + 1;
4325 memcpy (copy, name, first);
4326 memcpy (copy + first, name + first + 1, len - first);
4328 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4331 /* We also need to check references to the symbol without the
4333 copy[first - 1] = '\0';
4334 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4335 FALSE, FALSE, FALSE);
4338 bfd_release (abfd, copy);
4342 /* Add symbols from an ELF archive file to the linker hash table. We
4343 don't use _bfd_generic_link_add_archive_symbols because of a
4344 problem which arises on UnixWare. The UnixWare libc.so is an
4345 archive which includes an entry libc.so.1 which defines a bunch of
4346 symbols. The libc.so archive also includes a number of other
4347 object files, which also define symbols, some of which are the same
4348 as those defined in libc.so.1. Correct linking requires that we
4349 consider each object file in turn, and include it if it defines any
4350 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4351 this; it looks through the list of undefined symbols, and includes
4352 any object file which defines them. When this algorithm is used on
4353 UnixWare, it winds up pulling in libc.so.1 early and defining a
4354 bunch of symbols. This means that some of the other objects in the
4355 archive are not included in the link, which is incorrect since they
4356 precede libc.so.1 in the archive.
4358 Fortunately, ELF archive handling is simpler than that done by
4359 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4360 oddities. In ELF, if we find a symbol in the archive map, and the
4361 symbol is currently undefined, we know that we must pull in that
4364 Unfortunately, we do have to make multiple passes over the symbol
4365 table until nothing further is resolved. */
4368 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4371 bfd_boolean *defined = NULL;
4372 bfd_boolean *included = NULL;
4376 const struct elf_backend_data *bed;
4377 struct elf_link_hash_entry * (*archive_symbol_lookup)
4378 (bfd *, struct bfd_link_info *, const char *);
4380 if (! bfd_has_map (abfd))
4382 /* An empty archive is a special case. */
4383 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4385 bfd_set_error (bfd_error_no_armap);
4389 /* Keep track of all symbols we know to be already defined, and all
4390 files we know to be already included. This is to speed up the
4391 second and subsequent passes. */
4392 c = bfd_ardata (abfd)->symdef_count;
4396 amt *= sizeof (bfd_boolean);
4397 defined = bfd_zmalloc (amt);
4398 included = bfd_zmalloc (amt);
4399 if (defined == NULL || included == NULL)
4402 symdefs = bfd_ardata (abfd)->symdefs;
4403 bed = get_elf_backend_data (abfd);
4404 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4417 symdefend = symdef + c;
4418 for (i = 0; symdef < symdefend; symdef++, i++)
4420 struct elf_link_hash_entry *h;
4422 struct bfd_link_hash_entry *undefs_tail;
4425 if (defined[i] || included[i])
4427 if (symdef->file_offset == last)
4433 h = archive_symbol_lookup (abfd, info, symdef->name);
4434 if (h == (struct elf_link_hash_entry *) 0 - 1)
4440 if (h->root.type == bfd_link_hash_common)
4442 /* We currently have a common symbol. The archive map contains
4443 a reference to this symbol, so we may want to include it. We
4444 only want to include it however, if this archive element
4445 contains a definition of the symbol, not just another common
4448 Unfortunately some archivers (including GNU ar) will put
4449 declarations of common symbols into their archive maps, as
4450 well as real definitions, so we cannot just go by the archive
4451 map alone. Instead we must read in the element's symbol
4452 table and check that to see what kind of symbol definition
4454 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4457 else if (h->root.type != bfd_link_hash_undefined)
4459 if (h->root.type != bfd_link_hash_undefweak)
4464 /* We need to include this archive member. */
4465 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4466 if (element == NULL)
4469 if (! bfd_check_format (element, bfd_object))
4472 /* Doublecheck that we have not included this object
4473 already--it should be impossible, but there may be
4474 something wrong with the archive. */
4475 if (element->archive_pass != 0)
4477 bfd_set_error (bfd_error_bad_value);
4480 element->archive_pass = 1;
4482 undefs_tail = info->hash->undefs_tail;
4484 if (! (*info->callbacks->add_archive_element) (info, element,
4487 if (! bfd_link_add_symbols (element, info))
4490 /* If there are any new undefined symbols, we need to make
4491 another pass through the archive in order to see whether
4492 they can be defined. FIXME: This isn't perfect, because
4493 common symbols wind up on undefs_tail and because an
4494 undefined symbol which is defined later on in this pass
4495 does not require another pass. This isn't a bug, but it
4496 does make the code less efficient than it could be. */
4497 if (undefs_tail != info->hash->undefs_tail)
4500 /* Look backward to mark all symbols from this object file
4501 which we have already seen in this pass. */
4505 included[mark] = TRUE;
4510 while (symdefs[mark].file_offset == symdef->file_offset);
4512 /* We mark subsequent symbols from this object file as we go
4513 on through the loop. */
4514 last = symdef->file_offset;
4525 if (defined != NULL)
4527 if (included != NULL)
4532 /* Given an ELF BFD, add symbols to the global hash table as
4536 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4538 switch (bfd_get_format (abfd))
4541 return elf_link_add_object_symbols (abfd, info);
4543 return elf_link_add_archive_symbols (abfd, info);
4545 bfd_set_error (bfd_error_wrong_format);
4550 /* This function will be called though elf_link_hash_traverse to store
4551 all hash value of the exported symbols in an array. */
4554 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4556 unsigned long **valuep = data;
4562 if (h->root.type == bfd_link_hash_warning)
4563 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4565 /* Ignore indirect symbols. These are added by the versioning code. */
4566 if (h->dynindx == -1)
4569 name = h->root.root.string;
4570 p = strchr (name, ELF_VER_CHR);
4573 alc = bfd_malloc (p - name + 1);
4574 memcpy (alc, name, p - name);
4575 alc[p - name] = '\0';
4579 /* Compute the hash value. */
4580 ha = bfd_elf_hash (name);
4582 /* Store the found hash value in the array given as the argument. */
4585 /* And store it in the struct so that we can put it in the hash table
4587 h->u.elf_hash_value = ha;
4595 /* Array used to determine the number of hash table buckets to use
4596 based on the number of symbols there are. If there are fewer than
4597 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4598 fewer than 37 we use 17 buckets, and so forth. We never use more
4599 than 32771 buckets. */
4601 static const size_t elf_buckets[] =
4603 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4607 /* Compute bucket count for hashing table. We do not use a static set
4608 of possible tables sizes anymore. Instead we determine for all
4609 possible reasonable sizes of the table the outcome (i.e., the
4610 number of collisions etc) and choose the best solution. The
4611 weighting functions are not too simple to allow the table to grow
4612 without bounds. Instead one of the weighting factors is the size.
4613 Therefore the result is always a good payoff between few collisions
4614 (= short chain lengths) and table size. */
4616 compute_bucket_count (struct bfd_link_info *info)
4618 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4619 size_t best_size = 0;
4620 unsigned long int *hashcodes;
4621 unsigned long int *hashcodesp;
4622 unsigned long int i;
4625 /* Compute the hash values for all exported symbols. At the same
4626 time store the values in an array so that we could use them for
4629 amt *= sizeof (unsigned long int);
4630 hashcodes = bfd_malloc (amt);
4631 if (hashcodes == NULL)
4633 hashcodesp = hashcodes;
4635 /* Put all hash values in HASHCODES. */
4636 elf_link_hash_traverse (elf_hash_table (info),
4637 elf_collect_hash_codes, &hashcodesp);
4639 /* We have a problem here. The following code to optimize the table
4640 size requires an integer type with more the 32 bits. If
4641 BFD_HOST_U_64_BIT is set we know about such a type. */
4642 #ifdef BFD_HOST_U_64_BIT
4645 unsigned long int nsyms = hashcodesp - hashcodes;
4648 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4649 unsigned long int *counts ;
4650 bfd *dynobj = elf_hash_table (info)->dynobj;
4651 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4653 /* Possible optimization parameters: if we have NSYMS symbols we say
4654 that the hashing table must at least have NSYMS/4 and at most
4656 minsize = nsyms / 4;
4659 best_size = maxsize = nsyms * 2;
4661 /* Create array where we count the collisions in. We must use bfd_malloc
4662 since the size could be large. */
4664 amt *= sizeof (unsigned long int);
4665 counts = bfd_malloc (amt);
4672 /* Compute the "optimal" size for the hash table. The criteria is a
4673 minimal chain length. The minor criteria is (of course) the size
4675 for (i = minsize; i < maxsize; ++i)
4677 /* Walk through the array of hashcodes and count the collisions. */
4678 BFD_HOST_U_64_BIT max;
4679 unsigned long int j;
4680 unsigned long int fact;
4682 memset (counts, '\0', i * sizeof (unsigned long int));
4684 /* Determine how often each hash bucket is used. */
4685 for (j = 0; j < nsyms; ++j)
4686 ++counts[hashcodes[j] % i];
4688 /* For the weight function we need some information about the
4689 pagesize on the target. This is information need not be 100%
4690 accurate. Since this information is not available (so far) we
4691 define it here to a reasonable default value. If it is crucial
4692 to have a better value some day simply define this value. */
4693 # ifndef BFD_TARGET_PAGESIZE
4694 # define BFD_TARGET_PAGESIZE (4096)
4697 /* We in any case need 2 + NSYMS entries for the size values and
4699 max = (2 + nsyms) * (bed->s->arch_size / 8);
4702 /* Variant 1: optimize for short chains. We add the squares
4703 of all the chain lengths (which favors many small chain
4704 over a few long chains). */
4705 for (j = 0; j < i; ++j)
4706 max += counts[j] * counts[j];
4708 /* This adds penalties for the overall size of the table. */
4709 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4712 /* Variant 2: Optimize a lot more for small table. Here we
4713 also add squares of the size but we also add penalties for
4714 empty slots (the +1 term). */
4715 for (j = 0; j < i; ++j)
4716 max += (1 + counts[j]) * (1 + counts[j]);
4718 /* The overall size of the table is considered, but not as
4719 strong as in variant 1, where it is squared. */
4720 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4724 /* Compare with current best results. */
4725 if (max < best_chlen)
4735 #endif /* defined (BFD_HOST_U_64_BIT) */
4737 /* This is the fallback solution if no 64bit type is available or if we
4738 are not supposed to spend much time on optimizations. We select the
4739 bucket count using a fixed set of numbers. */
4740 for (i = 0; elf_buckets[i] != 0; i++)
4742 best_size = elf_buckets[i];
4743 if (dynsymcount < elf_buckets[i + 1])
4748 /* Free the arrays we needed. */
4754 /* Set up the sizes and contents of the ELF dynamic sections. This is
4755 called by the ELF linker emulation before_allocation routine. We
4756 must set the sizes of the sections before the linker sets the
4757 addresses of the various sections. */
4760 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4763 const char *filter_shlib,
4764 const char * const *auxiliary_filters,
4765 struct bfd_link_info *info,
4766 asection **sinterpptr,
4767 struct bfd_elf_version_tree *verdefs)
4769 bfd_size_type soname_indx;
4771 const struct elf_backend_data *bed;
4772 struct elf_assign_sym_version_info asvinfo;
4776 soname_indx = (bfd_size_type) -1;
4778 if (!is_elf_hash_table (info->hash))
4781 elf_tdata (output_bfd)->relro = info->relro;
4782 if (info->execstack)
4783 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4784 else if (info->noexecstack)
4785 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4789 asection *notesec = NULL;
4792 for (inputobj = info->input_bfds;
4794 inputobj = inputobj->link_next)
4798 if (inputobj->flags & DYNAMIC)
4800 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4803 if (s->flags & SEC_CODE)
4812 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4813 if (exec && info->relocatable
4814 && notesec->output_section != bfd_abs_section_ptr)
4815 notesec->output_section->flags |= SEC_CODE;
4819 /* Any syms created from now on start with -1 in
4820 got.refcount/offset and plt.refcount/offset. */
4821 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
4823 /* The backend may have to create some sections regardless of whether
4824 we're dynamic or not. */
4825 bed = get_elf_backend_data (output_bfd);
4826 if (bed->elf_backend_always_size_sections
4827 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4830 dynobj = elf_hash_table (info)->dynobj;
4832 /* If there were no dynamic objects in the link, there is nothing to
4837 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
4840 if (elf_hash_table (info)->dynamic_sections_created)
4842 struct elf_info_failed eif;
4843 struct elf_link_hash_entry *h;
4845 struct bfd_elf_version_tree *t;
4846 struct bfd_elf_version_expr *d;
4847 bfd_boolean all_defined;
4849 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
4850 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
4854 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4856 if (soname_indx == (bfd_size_type) -1
4857 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
4863 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
4865 info->flags |= DF_SYMBOLIC;
4872 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
4874 if (indx == (bfd_size_type) -1
4875 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
4878 if (info->new_dtags)
4880 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
4881 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
4886 if (filter_shlib != NULL)
4890 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4891 filter_shlib, TRUE);
4892 if (indx == (bfd_size_type) -1
4893 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
4897 if (auxiliary_filters != NULL)
4899 const char * const *p;
4901 for (p = auxiliary_filters; *p != NULL; p++)
4905 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4907 if (indx == (bfd_size_type) -1
4908 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
4914 eif.verdefs = verdefs;
4917 /* If we are supposed to export all symbols into the dynamic symbol
4918 table (this is not the normal case), then do so. */
4919 if (info->export_dynamic)
4921 elf_link_hash_traverse (elf_hash_table (info),
4922 _bfd_elf_export_symbol,
4928 /* Make all global versions with definition. */
4929 for (t = verdefs; t != NULL; t = t->next)
4930 for (d = t->globals.list; d != NULL; d = d->next)
4931 if (!d->symver && d->symbol)
4933 const char *verstr, *name;
4934 size_t namelen, verlen, newlen;
4936 struct elf_link_hash_entry *newh;
4939 namelen = strlen (name);
4941 verlen = strlen (verstr);
4942 newlen = namelen + verlen + 3;
4944 newname = bfd_malloc (newlen);
4945 if (newname == NULL)
4947 memcpy (newname, name, namelen);
4949 /* Check the hidden versioned definition. */
4950 p = newname + namelen;
4952 memcpy (p, verstr, verlen + 1);
4953 newh = elf_link_hash_lookup (elf_hash_table (info),
4954 newname, FALSE, FALSE,
4957 || (newh->root.type != bfd_link_hash_defined
4958 && newh->root.type != bfd_link_hash_defweak))
4960 /* Check the default versioned definition. */
4962 memcpy (p, verstr, verlen + 1);
4963 newh = elf_link_hash_lookup (elf_hash_table (info),
4964 newname, FALSE, FALSE,
4969 /* Mark this version if there is a definition and it is
4970 not defined in a shared object. */
4972 && !newh->def_dynamic
4973 && (newh->root.type == bfd_link_hash_defined
4974 || newh->root.type == bfd_link_hash_defweak))
4978 /* Attach all the symbols to their version information. */
4979 asvinfo.output_bfd = output_bfd;
4980 asvinfo.info = info;
4981 asvinfo.verdefs = verdefs;
4982 asvinfo.failed = FALSE;
4984 elf_link_hash_traverse (elf_hash_table (info),
4985 _bfd_elf_link_assign_sym_version,
4990 if (!info->allow_undefined_version)
4992 /* Check if all global versions have a definition. */
4994 for (t = verdefs; t != NULL; t = t->next)
4995 for (d = t->globals.list; d != NULL; d = d->next)
4996 if (!d->symver && !d->script)
4998 (*_bfd_error_handler)
4999 (_("%s: undefined version: %s"),
5000 d->pattern, t->name);
5001 all_defined = FALSE;
5006 bfd_set_error (bfd_error_bad_value);
5011 /* Find all symbols which were defined in a dynamic object and make
5012 the backend pick a reasonable value for them. */
5013 elf_link_hash_traverse (elf_hash_table (info),
5014 _bfd_elf_adjust_dynamic_symbol,
5019 /* Add some entries to the .dynamic section. We fill in some of the
5020 values later, in bfd_elf_final_link, but we must add the entries
5021 now so that we know the final size of the .dynamic section. */
5023 /* If there are initialization and/or finalization functions to
5024 call then add the corresponding DT_INIT/DT_FINI entries. */
5025 h = (info->init_function
5026 ? elf_link_hash_lookup (elf_hash_table (info),
5027 info->init_function, FALSE,
5034 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5037 h = (info->fini_function
5038 ? elf_link_hash_lookup (elf_hash_table (info),
5039 info->fini_function, FALSE,
5046 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5050 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
5052 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5053 if (! info->executable)
5058 for (sub = info->input_bfds; sub != NULL;
5059 sub = sub->link_next)
5060 for (o = sub->sections; o != NULL; o = o->next)
5061 if (elf_section_data (o)->this_hdr.sh_type
5062 == SHT_PREINIT_ARRAY)
5064 (*_bfd_error_handler)
5065 (_("%B: .preinit_array section is not allowed in DSO"),
5070 bfd_set_error (bfd_error_nonrepresentable_section);
5074 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5075 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5078 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
5080 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5081 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5084 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
5086 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5087 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5091 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5092 /* If .dynstr is excluded from the link, we don't want any of
5093 these tags. Strictly, we should be checking each section
5094 individually; This quick check covers for the case where
5095 someone does a /DISCARD/ : { *(*) }. */
5096 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5098 bfd_size_type strsize;
5100 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5101 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5102 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5103 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5104 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5105 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5106 bed->s->sizeof_sym))
5111 /* The backend must work out the sizes of all the other dynamic
5113 if (bed->elf_backend_size_dynamic_sections
5114 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5117 if (elf_hash_table (info)->dynamic_sections_created)
5119 bfd_size_type dynsymcount;
5121 size_t bucketcount = 0;
5122 size_t hash_entry_size;
5123 unsigned int dtagcount;
5125 /* Set up the version definition section. */
5126 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5127 BFD_ASSERT (s != NULL);
5129 /* We may have created additional version definitions if we are
5130 just linking a regular application. */
5131 verdefs = asvinfo.verdefs;
5133 /* Skip anonymous version tag. */
5134 if (verdefs != NULL && verdefs->vernum == 0)
5135 verdefs = verdefs->next;
5137 if (verdefs == NULL && !info->create_default_symver)
5138 _bfd_strip_section_from_output (info, s);
5143 struct bfd_elf_version_tree *t;
5145 Elf_Internal_Verdef def;
5146 Elf_Internal_Verdaux defaux;
5147 struct bfd_link_hash_entry *bh;
5148 struct elf_link_hash_entry *h;
5154 /* Make space for the base version. */
5155 size += sizeof (Elf_External_Verdef);
5156 size += sizeof (Elf_External_Verdaux);
5159 /* Make space for the default version. */
5160 if (info->create_default_symver)
5162 size += sizeof (Elf_External_Verdef);
5166 for (t = verdefs; t != NULL; t = t->next)
5168 struct bfd_elf_version_deps *n;
5170 size += sizeof (Elf_External_Verdef);
5171 size += sizeof (Elf_External_Verdaux);
5174 for (n = t->deps; n != NULL; n = n->next)
5175 size += sizeof (Elf_External_Verdaux);
5179 s->contents = bfd_alloc (output_bfd, s->size);
5180 if (s->contents == NULL && s->size != 0)
5183 /* Fill in the version definition section. */
5187 def.vd_version = VER_DEF_CURRENT;
5188 def.vd_flags = VER_FLG_BASE;
5191 if (info->create_default_symver)
5193 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5194 def.vd_next = sizeof (Elf_External_Verdef);
5198 def.vd_aux = sizeof (Elf_External_Verdef);
5199 def.vd_next = (sizeof (Elf_External_Verdef)
5200 + sizeof (Elf_External_Verdaux));
5203 if (soname_indx != (bfd_size_type) -1)
5205 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5207 def.vd_hash = bfd_elf_hash (soname);
5208 defaux.vda_name = soname_indx;
5215 name = basename (output_bfd->filename);
5216 def.vd_hash = bfd_elf_hash (name);
5217 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5219 if (indx == (bfd_size_type) -1)
5221 defaux.vda_name = indx;
5223 defaux.vda_next = 0;
5225 _bfd_elf_swap_verdef_out (output_bfd, &def,
5226 (Elf_External_Verdef *) p);
5227 p += sizeof (Elf_External_Verdef);
5228 if (info->create_default_symver)
5230 /* Add a symbol representing this version. */
5232 if (! (_bfd_generic_link_add_one_symbol
5233 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5235 get_elf_backend_data (dynobj)->collect, &bh)))
5237 h = (struct elf_link_hash_entry *) bh;
5240 h->type = STT_OBJECT;
5241 h->verinfo.vertree = NULL;
5243 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5246 /* Create a duplicate of the base version with the same
5247 aux block, but different flags. */
5250 def.vd_aux = sizeof (Elf_External_Verdef);
5252 def.vd_next = (sizeof (Elf_External_Verdef)
5253 + sizeof (Elf_External_Verdaux));
5256 _bfd_elf_swap_verdef_out (output_bfd, &def,
5257 (Elf_External_Verdef *) p);
5258 p += sizeof (Elf_External_Verdef);
5260 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5261 (Elf_External_Verdaux *) p);
5262 p += sizeof (Elf_External_Verdaux);
5264 for (t = verdefs; t != NULL; t = t->next)
5267 struct bfd_elf_version_deps *n;
5270 for (n = t->deps; n != NULL; n = n->next)
5273 /* Add a symbol representing this version. */
5275 if (! (_bfd_generic_link_add_one_symbol
5276 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5278 get_elf_backend_data (dynobj)->collect, &bh)))
5280 h = (struct elf_link_hash_entry *) bh;
5283 h->type = STT_OBJECT;
5284 h->verinfo.vertree = t;
5286 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5289 def.vd_version = VER_DEF_CURRENT;
5291 if (t->globals.list == NULL
5292 && t->locals.list == NULL
5294 def.vd_flags |= VER_FLG_WEAK;
5295 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5296 def.vd_cnt = cdeps + 1;
5297 def.vd_hash = bfd_elf_hash (t->name);
5298 def.vd_aux = sizeof (Elf_External_Verdef);
5300 if (t->next != NULL)
5301 def.vd_next = (sizeof (Elf_External_Verdef)
5302 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5304 _bfd_elf_swap_verdef_out (output_bfd, &def,
5305 (Elf_External_Verdef *) p);
5306 p += sizeof (Elf_External_Verdef);
5308 defaux.vda_name = h->dynstr_index;
5309 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5311 defaux.vda_next = 0;
5312 if (t->deps != NULL)
5313 defaux.vda_next = sizeof (Elf_External_Verdaux);
5314 t->name_indx = defaux.vda_name;
5316 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5317 (Elf_External_Verdaux *) p);
5318 p += sizeof (Elf_External_Verdaux);
5320 for (n = t->deps; n != NULL; n = n->next)
5322 if (n->version_needed == NULL)
5324 /* This can happen if there was an error in the
5326 defaux.vda_name = 0;
5330 defaux.vda_name = n->version_needed->name_indx;
5331 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5334 if (n->next == NULL)
5335 defaux.vda_next = 0;
5337 defaux.vda_next = sizeof (Elf_External_Verdaux);
5339 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5340 (Elf_External_Verdaux *) p);
5341 p += sizeof (Elf_External_Verdaux);
5345 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5346 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5349 elf_tdata (output_bfd)->cverdefs = cdefs;
5352 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5354 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5357 else if (info->flags & DF_BIND_NOW)
5359 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5365 if (info->executable)
5366 info->flags_1 &= ~ (DF_1_INITFIRST
5369 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5373 /* Work out the size of the version reference section. */
5375 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5376 BFD_ASSERT (s != NULL);
5378 struct elf_find_verdep_info sinfo;
5380 sinfo.output_bfd = output_bfd;
5382 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5383 if (sinfo.vers == 0)
5385 sinfo.failed = FALSE;
5387 elf_link_hash_traverse (elf_hash_table (info),
5388 _bfd_elf_link_find_version_dependencies,
5391 if (elf_tdata (output_bfd)->verref == NULL)
5392 _bfd_strip_section_from_output (info, s);
5395 Elf_Internal_Verneed *t;
5400 /* Build the version definition section. */
5403 for (t = elf_tdata (output_bfd)->verref;
5407 Elf_Internal_Vernaux *a;
5409 size += sizeof (Elf_External_Verneed);
5411 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5412 size += sizeof (Elf_External_Vernaux);
5416 s->contents = bfd_alloc (output_bfd, s->size);
5417 if (s->contents == NULL)
5421 for (t = elf_tdata (output_bfd)->verref;
5426 Elf_Internal_Vernaux *a;
5430 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5433 t->vn_version = VER_NEED_CURRENT;
5435 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5436 elf_dt_name (t->vn_bfd) != NULL
5437 ? elf_dt_name (t->vn_bfd)
5438 : basename (t->vn_bfd->filename),
5440 if (indx == (bfd_size_type) -1)
5443 t->vn_aux = sizeof (Elf_External_Verneed);
5444 if (t->vn_nextref == NULL)
5447 t->vn_next = (sizeof (Elf_External_Verneed)
5448 + caux * sizeof (Elf_External_Vernaux));
5450 _bfd_elf_swap_verneed_out (output_bfd, t,
5451 (Elf_External_Verneed *) p);
5452 p += sizeof (Elf_External_Verneed);
5454 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5456 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5457 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5458 a->vna_nodename, FALSE);
5459 if (indx == (bfd_size_type) -1)
5462 if (a->vna_nextptr == NULL)
5465 a->vna_next = sizeof (Elf_External_Vernaux);
5467 _bfd_elf_swap_vernaux_out (output_bfd, a,
5468 (Elf_External_Vernaux *) p);
5469 p += sizeof (Elf_External_Vernaux);
5473 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5474 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5477 elf_tdata (output_bfd)->cverrefs = crefs;
5481 /* Assign dynsym indicies. In a shared library we generate a
5482 section symbol for each output section, which come first.
5483 Next come all of the back-end allocated local dynamic syms,
5484 followed by the rest of the global symbols. */
5486 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5488 /* Work out the size of the symbol version section. */
5489 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5490 BFD_ASSERT (s != NULL);
5491 if (dynsymcount == 0
5492 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL
5493 && !info->create_default_symver))
5495 _bfd_strip_section_from_output (info, s);
5496 /* The DYNSYMCOUNT might have changed if we were going to
5497 output a dynamic symbol table entry for S. */
5498 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5502 s->size = dynsymcount * sizeof (Elf_External_Versym);
5503 s->contents = bfd_zalloc (output_bfd, s->size);
5504 if (s->contents == NULL)
5507 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5511 /* Set the size of the .dynsym and .hash sections. We counted
5512 the number of dynamic symbols in elf_link_add_object_symbols.
5513 We will build the contents of .dynsym and .hash when we build
5514 the final symbol table, because until then we do not know the
5515 correct value to give the symbols. We built the .dynstr
5516 section as we went along in elf_link_add_object_symbols. */
5517 s = bfd_get_section_by_name (dynobj, ".dynsym");
5518 BFD_ASSERT (s != NULL);
5519 s->size = dynsymcount * bed->s->sizeof_sym;
5520 s->contents = bfd_alloc (output_bfd, s->size);
5521 if (s->contents == NULL && s->size != 0)
5524 if (dynsymcount != 0)
5526 Elf_Internal_Sym isym;
5528 /* The first entry in .dynsym is a dummy symbol. */
5535 bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0);
5538 /* Compute the size of the hashing table. As a side effect this
5539 computes the hash values for all the names we export. */
5540 bucketcount = compute_bucket_count (info);
5542 s = bfd_get_section_by_name (dynobj, ".hash");
5543 BFD_ASSERT (s != NULL);
5544 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5545 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5546 s->contents = bfd_zalloc (output_bfd, s->size);
5547 if (s->contents == NULL)
5550 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5551 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5552 s->contents + hash_entry_size);
5554 elf_hash_table (info)->bucketcount = bucketcount;
5556 s = bfd_get_section_by_name (dynobj, ".dynstr");
5557 BFD_ASSERT (s != NULL);
5559 elf_finalize_dynstr (output_bfd, info);
5561 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5563 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5564 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5571 /* Final phase of ELF linker. */
5573 /* A structure we use to avoid passing large numbers of arguments. */
5575 struct elf_final_link_info
5577 /* General link information. */
5578 struct bfd_link_info *info;
5581 /* Symbol string table. */
5582 struct bfd_strtab_hash *symstrtab;
5583 /* .dynsym section. */
5584 asection *dynsym_sec;
5585 /* .hash section. */
5587 /* symbol version section (.gnu.version). */
5588 asection *symver_sec;
5589 /* Buffer large enough to hold contents of any section. */
5591 /* Buffer large enough to hold external relocs of any section. */
5592 void *external_relocs;
5593 /* Buffer large enough to hold internal relocs of any section. */
5594 Elf_Internal_Rela *internal_relocs;
5595 /* Buffer large enough to hold external local symbols of any input
5597 bfd_byte *external_syms;
5598 /* And a buffer for symbol section indices. */
5599 Elf_External_Sym_Shndx *locsym_shndx;
5600 /* Buffer large enough to hold internal local symbols of any input
5602 Elf_Internal_Sym *internal_syms;
5603 /* Array large enough to hold a symbol index for each local symbol
5604 of any input BFD. */
5606 /* Array large enough to hold a section pointer for each local
5607 symbol of any input BFD. */
5608 asection **sections;
5609 /* Buffer to hold swapped out symbols. */
5611 /* And one for symbol section indices. */
5612 Elf_External_Sym_Shndx *symshndxbuf;
5613 /* Number of swapped out symbols in buffer. */
5614 size_t symbuf_count;
5615 /* Number of symbols which fit in symbuf. */
5617 /* And same for symshndxbuf. */
5618 size_t shndxbuf_size;
5621 /* This struct is used to pass information to elf_link_output_extsym. */
5623 struct elf_outext_info
5626 bfd_boolean localsyms;
5627 struct elf_final_link_info *finfo;
5630 /* When performing a relocatable link, the input relocations are
5631 preserved. But, if they reference global symbols, the indices
5632 referenced must be updated. Update all the relocations in
5633 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5636 elf_link_adjust_relocs (bfd *abfd,
5637 Elf_Internal_Shdr *rel_hdr,
5639 struct elf_link_hash_entry **rel_hash)
5642 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5644 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5645 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5646 bfd_vma r_type_mask;
5649 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5651 swap_in = bed->s->swap_reloc_in;
5652 swap_out = bed->s->swap_reloc_out;
5654 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5656 swap_in = bed->s->swap_reloca_in;
5657 swap_out = bed->s->swap_reloca_out;
5662 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5665 if (bed->s->arch_size == 32)
5672 r_type_mask = 0xffffffff;
5676 erela = rel_hdr->contents;
5677 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5679 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5682 if (*rel_hash == NULL)
5685 BFD_ASSERT ((*rel_hash)->indx >= 0);
5687 (*swap_in) (abfd, erela, irela);
5688 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5689 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5690 | (irela[j].r_info & r_type_mask));
5691 (*swap_out) (abfd, irela, erela);
5695 struct elf_link_sort_rela
5701 enum elf_reloc_type_class type;
5702 /* We use this as an array of size int_rels_per_ext_rel. */
5703 Elf_Internal_Rela rela[1];
5707 elf_link_sort_cmp1 (const void *A, const void *B)
5709 const struct elf_link_sort_rela *a = A;
5710 const struct elf_link_sort_rela *b = B;
5711 int relativea, relativeb;
5713 relativea = a->type == reloc_class_relative;
5714 relativeb = b->type == reloc_class_relative;
5716 if (relativea < relativeb)
5718 if (relativea > relativeb)
5720 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5722 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5724 if (a->rela->r_offset < b->rela->r_offset)
5726 if (a->rela->r_offset > b->rela->r_offset)
5732 elf_link_sort_cmp2 (const void *A, const void *B)
5734 const struct elf_link_sort_rela *a = A;
5735 const struct elf_link_sort_rela *b = B;
5738 if (a->u.offset < b->u.offset)
5740 if (a->u.offset > b->u.offset)
5742 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5743 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5748 if (a->rela->r_offset < b->rela->r_offset)
5750 if (a->rela->r_offset > b->rela->r_offset)
5756 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5759 bfd_size_type count, size;
5760 size_t i, ret, sort_elt, ext_size;
5761 bfd_byte *sort, *s_non_relative, *p;
5762 struct elf_link_sort_rela *sq;
5763 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5764 int i2e = bed->s->int_rels_per_ext_rel;
5765 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5766 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5767 struct bfd_link_order *lo;
5770 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5771 if (reldyn == NULL || reldyn->size == 0)
5773 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5774 if (reldyn == NULL || reldyn->size == 0)
5776 ext_size = bed->s->sizeof_rel;
5777 swap_in = bed->s->swap_reloc_in;
5778 swap_out = bed->s->swap_reloc_out;
5782 ext_size = bed->s->sizeof_rela;
5783 swap_in = bed->s->swap_reloca_in;
5784 swap_out = bed->s->swap_reloca_out;
5786 count = reldyn->size / ext_size;
5789 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5790 if (lo->type == bfd_indirect_link_order)
5792 asection *o = lo->u.indirect.section;
5796 if (size != reldyn->size)
5799 sort_elt = (sizeof (struct elf_link_sort_rela)
5800 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5801 sort = bfd_zmalloc (sort_elt * count);
5804 (*info->callbacks->warning)
5805 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5809 if (bed->s->arch_size == 32)
5810 r_sym_mask = ~(bfd_vma) 0xff;
5812 r_sym_mask = ~(bfd_vma) 0xffffffff;
5814 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5815 if (lo->type == bfd_indirect_link_order)
5817 bfd_byte *erel, *erelend;
5818 asection *o = lo->u.indirect.section;
5820 if (o->contents == NULL && o->size != 0)
5822 /* This is a reloc section that is being handled as a normal
5823 section. See bfd_section_from_shdr. We can't combine
5824 relocs in this case. */
5829 erelend = o->contents + o->size;
5830 p = sort + o->output_offset / ext_size * sort_elt;
5831 while (erel < erelend)
5833 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5834 (*swap_in) (abfd, erel, s->rela);
5835 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
5836 s->u.sym_mask = r_sym_mask;
5842 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
5844 for (i = 0, p = sort; i < count; i++, p += sort_elt)
5846 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5847 if (s->type != reloc_class_relative)
5853 sq = (struct elf_link_sort_rela *) s_non_relative;
5854 for (; i < count; i++, p += sort_elt)
5856 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
5857 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
5859 sp->u.offset = sq->rela->r_offset;
5862 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
5864 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5865 if (lo->type == bfd_indirect_link_order)
5867 bfd_byte *erel, *erelend;
5868 asection *o = lo->u.indirect.section;
5871 erelend = o->contents + o->size;
5872 p = sort + o->output_offset / ext_size * sort_elt;
5873 while (erel < erelend)
5875 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5876 (*swap_out) (abfd, s->rela, erel);
5887 /* Flush the output symbols to the file. */
5890 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
5891 const struct elf_backend_data *bed)
5893 if (finfo->symbuf_count > 0)
5895 Elf_Internal_Shdr *hdr;
5899 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5900 pos = hdr->sh_offset + hdr->sh_size;
5901 amt = finfo->symbuf_count * bed->s->sizeof_sym;
5902 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5903 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
5906 hdr->sh_size += amt;
5907 finfo->symbuf_count = 0;
5913 /* Add a symbol to the output symbol table. */
5916 elf_link_output_sym (struct elf_final_link_info *finfo,
5918 Elf_Internal_Sym *elfsym,
5919 asection *input_sec,
5920 struct elf_link_hash_entry *h)
5923 Elf_External_Sym_Shndx *destshndx;
5924 bfd_boolean (*output_symbol_hook)
5925 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
5926 struct elf_link_hash_entry *);
5927 const struct elf_backend_data *bed;
5929 bed = get_elf_backend_data (finfo->output_bfd);
5930 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
5931 if (output_symbol_hook != NULL)
5933 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
5937 if (name == NULL || *name == '\0')
5938 elfsym->st_name = 0;
5939 else if (input_sec->flags & SEC_EXCLUDE)
5940 elfsym->st_name = 0;
5943 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5945 if (elfsym->st_name == (unsigned long) -1)
5949 if (finfo->symbuf_count >= finfo->symbuf_size)
5951 if (! elf_link_flush_output_syms (finfo, bed))
5955 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
5956 destshndx = finfo->symshndxbuf;
5957 if (destshndx != NULL)
5959 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
5963 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
5964 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
5965 if (destshndx == NULL)
5967 memset ((char *) destshndx + amt, 0, amt);
5968 finfo->shndxbuf_size *= 2;
5970 destshndx += bfd_get_symcount (finfo->output_bfd);
5973 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
5974 finfo->symbuf_count += 1;
5975 bfd_get_symcount (finfo->output_bfd) += 1;
5980 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5981 allowing an unsatisfied unversioned symbol in the DSO to match a
5982 versioned symbol that would normally require an explicit version.
5983 We also handle the case that a DSO references a hidden symbol
5984 which may be satisfied by a versioned symbol in another DSO. */
5987 elf_link_check_versioned_symbol (struct bfd_link_info *info,
5988 const struct elf_backend_data *bed,
5989 struct elf_link_hash_entry *h)
5992 struct elf_link_loaded_list *loaded;
5994 if (!is_elf_hash_table (info->hash))
5997 switch (h->root.type)
6003 case bfd_link_hash_undefined:
6004 case bfd_link_hash_undefweak:
6005 abfd = h->root.u.undef.abfd;
6006 if ((abfd->flags & DYNAMIC) == 0
6007 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
6011 case bfd_link_hash_defined:
6012 case bfd_link_hash_defweak:
6013 abfd = h->root.u.def.section->owner;
6016 case bfd_link_hash_common:
6017 abfd = h->root.u.c.p->section->owner;
6020 BFD_ASSERT (abfd != NULL);
6022 for (loaded = elf_hash_table (info)->loaded;
6024 loaded = loaded->next)
6027 Elf_Internal_Shdr *hdr;
6028 bfd_size_type symcount;
6029 bfd_size_type extsymcount;
6030 bfd_size_type extsymoff;
6031 Elf_Internal_Shdr *versymhdr;
6032 Elf_Internal_Sym *isym;
6033 Elf_Internal_Sym *isymend;
6034 Elf_Internal_Sym *isymbuf;
6035 Elf_External_Versym *ever;
6036 Elf_External_Versym *extversym;
6038 input = loaded->abfd;
6040 /* We check each DSO for a possible hidden versioned definition. */
6042 || (input->flags & DYNAMIC) == 0
6043 || elf_dynversym (input) == 0)
6046 hdr = &elf_tdata (input)->dynsymtab_hdr;
6048 symcount = hdr->sh_size / bed->s->sizeof_sym;
6049 if (elf_bad_symtab (input))
6051 extsymcount = symcount;
6056 extsymcount = symcount - hdr->sh_info;
6057 extsymoff = hdr->sh_info;
6060 if (extsymcount == 0)
6063 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6065 if (isymbuf == NULL)
6068 /* Read in any version definitions. */
6069 versymhdr = &elf_tdata (input)->dynversym_hdr;
6070 extversym = bfd_malloc (versymhdr->sh_size);
6071 if (extversym == NULL)
6074 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6075 || (bfd_bread (extversym, versymhdr->sh_size, input)
6076 != versymhdr->sh_size))
6084 ever = extversym + extsymoff;
6085 isymend = isymbuf + extsymcount;
6086 for (isym = isymbuf; isym < isymend; isym++, ever++)
6089 Elf_Internal_Versym iver;
6090 unsigned short version_index;
6092 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6093 || isym->st_shndx == SHN_UNDEF)
6096 name = bfd_elf_string_from_elf_section (input,
6099 if (strcmp (name, h->root.root.string) != 0)
6102 _bfd_elf_swap_versym_in (input, ever, &iver);
6104 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6106 /* If we have a non-hidden versioned sym, then it should
6107 have provided a definition for the undefined sym. */
6111 version_index = iver.vs_vers & VERSYM_VERSION;
6112 if (version_index == 1 || version_index == 2)
6114 /* This is the base or first version. We can use it. */
6128 /* Add an external symbol to the symbol table. This is called from
6129 the hash table traversal routine. When generating a shared object,
6130 we go through the symbol table twice. The first time we output
6131 anything that might have been forced to local scope in a version
6132 script. The second time we output the symbols that are still
6136 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6138 struct elf_outext_info *eoinfo = data;
6139 struct elf_final_link_info *finfo = eoinfo->finfo;
6141 Elf_Internal_Sym sym;
6142 asection *input_sec;
6143 const struct elf_backend_data *bed;
6145 if (h->root.type == bfd_link_hash_warning)
6147 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6148 if (h->root.type == bfd_link_hash_new)
6152 /* Decide whether to output this symbol in this pass. */
6153 if (eoinfo->localsyms)
6155 if (!h->forced_local)
6160 if (h->forced_local)
6164 bed = get_elf_backend_data (finfo->output_bfd);
6166 /* If we have an undefined symbol reference here then it must have
6167 come from a shared library that is being linked in. (Undefined
6168 references in regular files have already been handled). If we
6169 are reporting errors for this situation then do so now. */
6170 if (h->root.type == bfd_link_hash_undefined
6173 && (elf_dyn_lib_class (h->root.u.undef.abfd) & DYN_AS_NEEDED) == 0
6174 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6175 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6177 if (! ((*finfo->info->callbacks->undefined_symbol)
6178 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6179 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6181 eoinfo->failed = TRUE;
6186 /* We should also warn if a forced local symbol is referenced from
6187 shared libraries. */
6188 if (! finfo->info->relocatable
6189 && (! finfo->info->shared)
6194 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6196 (*_bfd_error_handler)
6197 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6198 finfo->output_bfd, h->root.u.def.section->owner,
6199 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6201 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6202 ? "hidden" : "local",
6203 h->root.root.string);
6204 eoinfo->failed = TRUE;
6208 /* We don't want to output symbols that have never been mentioned by
6209 a regular file, or that we have been told to strip. However, if
6210 h->indx is set to -2, the symbol is used by a reloc and we must
6214 else if ((h->def_dynamic
6219 else if (finfo->info->strip == strip_all)
6221 else if (finfo->info->strip == strip_some
6222 && bfd_hash_lookup (finfo->info->keep_hash,
6223 h->root.root.string, FALSE, FALSE) == NULL)
6225 else if (finfo->info->strip_discarded
6226 && (h->root.type == bfd_link_hash_defined
6227 || h->root.type == bfd_link_hash_defweak)
6228 && elf_discarded_section (h->root.u.def.section))
6233 /* If we're stripping it, and it's not a dynamic symbol, there's
6234 nothing else to do unless it is a forced local symbol. */
6237 && !h->forced_local)
6241 sym.st_size = h->size;
6242 sym.st_other = h->other;
6243 if (h->forced_local)
6244 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6245 else if (h->root.type == bfd_link_hash_undefweak
6246 || h->root.type == bfd_link_hash_defweak)
6247 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6249 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6251 switch (h->root.type)
6254 case bfd_link_hash_new:
6255 case bfd_link_hash_warning:
6259 case bfd_link_hash_undefined:
6260 case bfd_link_hash_undefweak:
6261 input_sec = bfd_und_section_ptr;
6262 sym.st_shndx = SHN_UNDEF;
6265 case bfd_link_hash_defined:
6266 case bfd_link_hash_defweak:
6268 input_sec = h->root.u.def.section;
6269 if (input_sec->output_section != NULL)
6272 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6273 input_sec->output_section);
6274 if (sym.st_shndx == SHN_BAD)
6276 (*_bfd_error_handler)
6277 (_("%B: could not find output section %A for input section %A"),
6278 finfo->output_bfd, input_sec->output_section, input_sec);
6279 eoinfo->failed = TRUE;
6283 /* ELF symbols in relocatable files are section relative,
6284 but in nonrelocatable files they are virtual
6286 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6287 if (! finfo->info->relocatable)
6289 sym.st_value += input_sec->output_section->vma;
6290 if (h->type == STT_TLS)
6292 /* STT_TLS symbols are relative to PT_TLS segment
6294 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6295 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6301 BFD_ASSERT (input_sec->owner == NULL
6302 || (input_sec->owner->flags & DYNAMIC) != 0);
6303 sym.st_shndx = SHN_UNDEF;
6304 input_sec = bfd_und_section_ptr;
6309 case bfd_link_hash_common:
6310 input_sec = h->root.u.c.p->section;
6311 sym.st_shndx = SHN_COMMON;
6312 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6315 case bfd_link_hash_indirect:
6316 /* These symbols are created by symbol versioning. They point
6317 to the decorated version of the name. For example, if the
6318 symbol foo@@GNU_1.2 is the default, which should be used when
6319 foo is used with no version, then we add an indirect symbol
6320 foo which points to foo@@GNU_1.2. We ignore these symbols,
6321 since the indirected symbol is already in the hash table. */
6325 /* Give the processor backend a chance to tweak the symbol value,
6326 and also to finish up anything that needs to be done for this
6327 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6328 forced local syms when non-shared is due to a historical quirk. */
6329 if ((h->dynindx != -1
6331 && ((finfo->info->shared
6332 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6333 || h->root.type != bfd_link_hash_undefweak))
6334 || !h->forced_local)
6335 && elf_hash_table (finfo->info)->dynamic_sections_created)
6337 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6338 (finfo->output_bfd, finfo->info, h, &sym)))
6340 eoinfo->failed = TRUE;
6345 /* If we are marking the symbol as undefined, and there are no
6346 non-weak references to this symbol from a regular object, then
6347 mark the symbol as weak undefined; if there are non-weak
6348 references, mark the symbol as strong. We can't do this earlier,
6349 because it might not be marked as undefined until the
6350 finish_dynamic_symbol routine gets through with it. */
6351 if (sym.st_shndx == SHN_UNDEF
6353 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6354 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6358 if (h->ref_regular_nonweak)
6359 bindtype = STB_GLOBAL;
6361 bindtype = STB_WEAK;
6362 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6365 /* If a non-weak symbol with non-default visibility is not defined
6366 locally, it is a fatal error. */
6367 if (! finfo->info->relocatable
6368 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6369 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6370 && h->root.type == bfd_link_hash_undefined
6373 (*_bfd_error_handler)
6374 (_("%B: %s symbol `%s' isn't defined"),
6376 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6378 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6379 ? "internal" : "hidden",
6380 h->root.root.string);
6381 eoinfo->failed = TRUE;
6385 /* If this symbol should be put in the .dynsym section, then put it
6386 there now. We already know the symbol index. We also fill in
6387 the entry in the .hash section. */
6388 if (h->dynindx != -1
6389 && elf_hash_table (finfo->info)->dynamic_sections_created)
6393 size_t hash_entry_size;
6394 bfd_byte *bucketpos;
6398 sym.st_name = h->dynstr_index;
6399 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6400 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6402 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6403 bucket = h->u.elf_hash_value % bucketcount;
6405 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6406 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6407 + (bucket + 2) * hash_entry_size);
6408 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6409 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6410 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6411 ((bfd_byte *) finfo->hash_sec->contents
6412 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6414 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6416 Elf_Internal_Versym iversym;
6417 Elf_External_Versym *eversym;
6419 if (!h->def_regular)
6421 if (h->verinfo.verdef == NULL)
6422 iversym.vs_vers = 0;
6424 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6428 if (h->verinfo.vertree == NULL)
6429 iversym.vs_vers = 1;
6431 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6432 if (finfo->info->create_default_symver)
6437 iversym.vs_vers |= VERSYM_HIDDEN;
6439 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6440 eversym += h->dynindx;
6441 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6445 /* If we're stripping it, then it was just a dynamic symbol, and
6446 there's nothing else to do. */
6447 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6450 h->indx = bfd_get_symcount (finfo->output_bfd);
6452 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6454 eoinfo->failed = TRUE;
6461 /* Return TRUE if special handling is done for relocs in SEC against
6462 symbols defined in discarded sections. */
6465 elf_section_ignore_discarded_relocs (asection *sec)
6467 const struct elf_backend_data *bed;
6469 switch (sec->sec_info_type)
6471 case ELF_INFO_TYPE_STABS:
6472 case ELF_INFO_TYPE_EH_FRAME:
6478 bed = get_elf_backend_data (sec->owner);
6479 if (bed->elf_backend_ignore_discarded_relocs != NULL
6480 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6486 enum action_discarded
6492 /* Return a mask saying how ld should treat relocations in SEC against
6493 symbols defined in discarded sections. If this function returns
6494 COMPLAIN set, ld will issue a warning message. If this function
6495 returns PRETEND set, and the discarded section was link-once and the
6496 same size as the kept link-once section, ld will pretend that the
6497 symbol was actually defined in the kept section. Otherwise ld will
6498 zero the reloc (at least that is the intent, but some cooperation by
6499 the target dependent code is needed, particularly for REL targets). */
6502 elf_action_discarded (asection *sec)
6504 if (sec->flags & SEC_DEBUGGING)
6507 if (strcmp (".eh_frame", sec->name) == 0)
6510 if (strcmp (".gcc_except_table", sec->name) == 0)
6513 if (strcmp (".PARISC.unwind", sec->name) == 0)
6516 if (strcmp (".fixup", sec->name) == 0)
6519 return COMPLAIN | PRETEND;
6522 /* Find a match between a section and a member of a section group. */
6525 match_group_member (asection *sec, asection *group)
6527 asection *first = elf_next_in_group (group);
6528 asection *s = first;
6532 if (bfd_elf_match_symbols_in_sections (s, sec))
6542 /* Link an input file into the linker output file. This function
6543 handles all the sections and relocations of the input file at once.
6544 This is so that we only have to read the local symbols once, and
6545 don't have to keep them in memory. */
6548 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6550 bfd_boolean (*relocate_section)
6551 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6552 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6554 Elf_Internal_Shdr *symtab_hdr;
6557 Elf_Internal_Sym *isymbuf;
6558 Elf_Internal_Sym *isym;
6559 Elf_Internal_Sym *isymend;
6561 asection **ppsection;
6563 const struct elf_backend_data *bed;
6564 bfd_boolean emit_relocs;
6565 struct elf_link_hash_entry **sym_hashes;
6567 output_bfd = finfo->output_bfd;
6568 bed = get_elf_backend_data (output_bfd);
6569 relocate_section = bed->elf_backend_relocate_section;
6571 /* If this is a dynamic object, we don't want to do anything here:
6572 we don't want the local symbols, and we don't want the section
6574 if ((input_bfd->flags & DYNAMIC) != 0)
6577 emit_relocs = (finfo->info->relocatable
6578 || finfo->info->emitrelocations
6579 || bed->elf_backend_emit_relocs);
6581 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6582 if (elf_bad_symtab (input_bfd))
6584 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6589 locsymcount = symtab_hdr->sh_info;
6590 extsymoff = symtab_hdr->sh_info;
6593 /* Read the local symbols. */
6594 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6595 if (isymbuf == NULL && locsymcount != 0)
6597 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6598 finfo->internal_syms,
6599 finfo->external_syms,
6600 finfo->locsym_shndx);
6601 if (isymbuf == NULL)
6605 /* Find local symbol sections and adjust values of symbols in
6606 SEC_MERGE sections. Write out those local symbols we know are
6607 going into the output file. */
6608 isymend = isymbuf + locsymcount;
6609 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6611 isym++, pindex++, ppsection++)
6615 Elf_Internal_Sym osym;
6619 if (elf_bad_symtab (input_bfd))
6621 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6628 if (isym->st_shndx == SHN_UNDEF)
6629 isec = bfd_und_section_ptr;
6630 else if (isym->st_shndx < SHN_LORESERVE
6631 || isym->st_shndx > SHN_HIRESERVE)
6633 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6635 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6636 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6638 _bfd_merged_section_offset (output_bfd, &isec,
6639 elf_section_data (isec)->sec_info,
6642 else if (isym->st_shndx == SHN_ABS)
6643 isec = bfd_abs_section_ptr;
6644 else if (isym->st_shndx == SHN_COMMON)
6645 isec = bfd_com_section_ptr;
6654 /* Don't output the first, undefined, symbol. */
6655 if (ppsection == finfo->sections)
6658 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6660 /* We never output section symbols. Instead, we use the
6661 section symbol of the corresponding section in the output
6666 /* If we are stripping all symbols, we don't want to output this
6668 if (finfo->info->strip == strip_all)
6671 /* If we are discarding all local symbols, we don't want to
6672 output this one. If we are generating a relocatable output
6673 file, then some of the local symbols may be required by
6674 relocs; we output them below as we discover that they are
6676 if (finfo->info->discard == discard_all)
6679 /* If this symbol is defined in a section which we are
6680 discarding, we don't need to keep it, but note that
6681 linker_mark is only reliable for sections that have contents.
6682 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6683 as well as linker_mark. */
6684 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6686 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6687 || (! finfo->info->relocatable
6688 && (isec->flags & SEC_EXCLUDE) != 0)))
6691 /* Get the name of the symbol. */
6692 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6697 /* See if we are discarding symbols with this name. */
6698 if ((finfo->info->strip == strip_some
6699 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6701 || (((finfo->info->discard == discard_sec_merge
6702 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6703 || finfo->info->discard == discard_l)
6704 && bfd_is_local_label_name (input_bfd, name)))
6707 /* If we get here, we are going to output this symbol. */
6711 /* Adjust the section index for the output file. */
6712 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6713 isec->output_section);
6714 if (osym.st_shndx == SHN_BAD)
6717 *pindex = bfd_get_symcount (output_bfd);
6719 /* ELF symbols in relocatable files are section relative, but
6720 in executable files they are virtual addresses. Note that
6721 this code assumes that all ELF sections have an associated
6722 BFD section with a reasonable value for output_offset; below
6723 we assume that they also have a reasonable value for
6724 output_section. Any special sections must be set up to meet
6725 these requirements. */
6726 osym.st_value += isec->output_offset;
6727 if (! finfo->info->relocatable)
6729 osym.st_value += isec->output_section->vma;
6730 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6732 /* STT_TLS symbols are relative to PT_TLS segment base. */
6733 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6734 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6738 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6742 /* Relocate the contents of each section. */
6743 sym_hashes = elf_sym_hashes (input_bfd);
6744 for (o = input_bfd->sections; o != NULL; o = o->next)
6748 if (! o->linker_mark)
6750 /* This section was omitted from the link. */
6754 if ((o->flags & SEC_HAS_CONTENTS) == 0
6755 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6758 if ((o->flags & SEC_LINKER_CREATED) != 0)
6760 /* Section was created by _bfd_elf_link_create_dynamic_sections
6765 /* Get the contents of the section. They have been cached by a
6766 relaxation routine. Note that o is a section in an input
6767 file, so the contents field will not have been set by any of
6768 the routines which work on output files. */
6769 if (elf_section_data (o)->this_hdr.contents != NULL)
6770 contents = elf_section_data (o)->this_hdr.contents;
6773 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6775 contents = finfo->contents;
6776 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6780 if ((o->flags & SEC_RELOC) != 0)
6782 Elf_Internal_Rela *internal_relocs;
6783 bfd_vma r_type_mask;
6786 /* Get the swapped relocs. */
6788 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6789 finfo->internal_relocs, FALSE);
6790 if (internal_relocs == NULL
6791 && o->reloc_count > 0)
6794 if (bed->s->arch_size == 32)
6801 r_type_mask = 0xffffffff;
6805 /* Run through the relocs looking for any against symbols
6806 from discarded sections and section symbols from
6807 removed link-once sections. Complain about relocs
6808 against discarded sections. Zero relocs against removed
6809 link-once sections. Preserve debug information as much
6811 if (!elf_section_ignore_discarded_relocs (o))
6813 Elf_Internal_Rela *rel, *relend;
6814 unsigned int action = elf_action_discarded (o);
6816 rel = internal_relocs;
6817 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6818 for ( ; rel < relend; rel++)
6820 unsigned long r_symndx = rel->r_info >> r_sym_shift;
6821 asection **ps, *sec;
6822 struct elf_link_hash_entry *h = NULL;
6823 const char *sym_name;
6825 if (r_symndx == STN_UNDEF)
6828 if (r_symndx >= locsymcount
6829 || (elf_bad_symtab (input_bfd)
6830 && finfo->sections[r_symndx] == NULL))
6832 h = sym_hashes[r_symndx - extsymoff];
6833 while (h->root.type == bfd_link_hash_indirect
6834 || h->root.type == bfd_link_hash_warning)
6835 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6837 if (h->root.type != bfd_link_hash_defined
6838 && h->root.type != bfd_link_hash_defweak)
6841 ps = &h->root.u.def.section;
6842 sym_name = h->root.root.string;
6846 Elf_Internal_Sym *sym = isymbuf + r_symndx;
6847 ps = &finfo->sections[r_symndx];
6848 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym);
6851 /* Complain if the definition comes from a
6852 discarded section. */
6853 if ((sec = *ps) != NULL && elf_discarded_section (sec))
6857 BFD_ASSERT (r_symndx != 0);
6858 if (action & COMPLAIN)
6860 (*_bfd_error_handler)
6861 (_("`%s' referenced in section `%A' of %B: "
6862 "defined in discarded section `%A' of %B\n"),
6863 o, input_bfd, sec, sec->owner, sym_name);
6866 /* Try to do the best we can to support buggy old
6867 versions of gcc. If we've warned, or this is
6868 debugging info, pretend that the symbol is
6869 really defined in the kept linkonce section.
6870 FIXME: This is quite broken. Modifying the
6871 symbol here means we will be changing all later
6872 uses of the symbol, not just in this section.
6873 The only thing that makes this half reasonable
6874 is that we warn in non-debug sections, and
6875 debug sections tend to come after other
6877 kept = sec->kept_section;
6878 if (kept != NULL && (action & PRETEND))
6880 if (elf_sec_group (sec) != NULL)
6881 kept = match_group_member (sec, kept);
6883 && sec->size == kept->size)
6890 /* Remove the symbol reference from the reloc, but
6891 don't kill the reloc completely. This is so that
6892 a zero value will be written into the section,
6893 which may have non-zero contents put there by the
6894 assembler. Zero in things like an eh_frame fde
6895 pc_begin allows stack unwinders to recognize the
6897 rel->r_info &= r_type_mask;
6903 /* Relocate the section by invoking a back end routine.
6905 The back end routine is responsible for adjusting the
6906 section contents as necessary, and (if using Rela relocs
6907 and generating a relocatable output file) adjusting the
6908 reloc addend as necessary.
6910 The back end routine does not have to worry about setting
6911 the reloc address or the reloc symbol index.
6913 The back end routine is given a pointer to the swapped in
6914 internal symbols, and can access the hash table entries
6915 for the external symbols via elf_sym_hashes (input_bfd).
6917 When generating relocatable output, the back end routine
6918 must handle STB_LOCAL/STT_SECTION symbols specially. The
6919 output symbol is going to be a section symbol
6920 corresponding to the output section, which will require
6921 the addend to be adjusted. */
6923 if (! (*relocate_section) (output_bfd, finfo->info,
6924 input_bfd, o, contents,
6932 Elf_Internal_Rela *irela;
6933 Elf_Internal_Rela *irelaend;
6934 bfd_vma last_offset;
6935 struct elf_link_hash_entry **rel_hash;
6936 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
6937 unsigned int next_erel;
6938 bfd_boolean (*reloc_emitter)
6939 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
6940 bfd_boolean rela_normal;
6942 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6943 rela_normal = (bed->rela_normal
6944 && (input_rel_hdr->sh_entsize
6945 == bed->s->sizeof_rela));
6947 /* Adjust the reloc addresses and symbol indices. */
6949 irela = internal_relocs;
6950 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6951 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6952 + elf_section_data (o->output_section)->rel_count
6953 + elf_section_data (o->output_section)->rel_count2);
6954 last_offset = o->output_offset;
6955 if (!finfo->info->relocatable)
6956 last_offset += o->output_section->vma;
6957 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6959 unsigned long r_symndx;
6961 Elf_Internal_Sym sym;
6963 if (next_erel == bed->s->int_rels_per_ext_rel)
6969 irela->r_offset = _bfd_elf_section_offset (output_bfd,
6972 if (irela->r_offset >= (bfd_vma) -2)
6974 /* This is a reloc for a deleted entry or somesuch.
6975 Turn it into an R_*_NONE reloc, at the same
6976 offset as the last reloc. elf_eh_frame.c and
6977 elf_bfd_discard_info rely on reloc offsets
6979 irela->r_offset = last_offset;
6981 irela->r_addend = 0;
6985 irela->r_offset += o->output_offset;
6987 /* Relocs in an executable have to be virtual addresses. */
6988 if (!finfo->info->relocatable)
6989 irela->r_offset += o->output_section->vma;
6991 last_offset = irela->r_offset;
6993 r_symndx = irela->r_info >> r_sym_shift;
6994 if (r_symndx == STN_UNDEF)
6997 if (r_symndx >= locsymcount
6998 || (elf_bad_symtab (input_bfd)
6999 && finfo->sections[r_symndx] == NULL))
7001 struct elf_link_hash_entry *rh;
7004 /* This is a reloc against a global symbol. We
7005 have not yet output all the local symbols, so
7006 we do not know the symbol index of any global
7007 symbol. We set the rel_hash entry for this
7008 reloc to point to the global hash table entry
7009 for this symbol. The symbol index is then
7010 set at the end of bfd_elf_final_link. */
7011 indx = r_symndx - extsymoff;
7012 rh = elf_sym_hashes (input_bfd)[indx];
7013 while (rh->root.type == bfd_link_hash_indirect
7014 || rh->root.type == bfd_link_hash_warning)
7015 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7017 /* Setting the index to -2 tells
7018 elf_link_output_extsym that this symbol is
7020 BFD_ASSERT (rh->indx < 0);
7028 /* This is a reloc against a local symbol. */
7031 sym = isymbuf[r_symndx];
7032 sec = finfo->sections[r_symndx];
7033 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7035 /* I suppose the backend ought to fill in the
7036 section of any STT_SECTION symbol against a
7037 processor specific section. */
7039 if (bfd_is_abs_section (sec))
7041 else if (sec == NULL || sec->owner == NULL)
7043 bfd_set_error (bfd_error_bad_value);
7048 asection *osec = sec->output_section;
7050 /* If we have discarded a section, the output
7051 section will be the absolute section. In
7052 case of discarded link-once and discarded
7053 SEC_MERGE sections, use the kept section. */
7054 if (bfd_is_abs_section (osec)
7055 && sec->kept_section != NULL
7056 && sec->kept_section->output_section != NULL)
7058 osec = sec->kept_section->output_section;
7059 irela->r_addend -= osec->vma;
7062 if (!bfd_is_abs_section (osec))
7064 r_symndx = osec->target_index;
7065 BFD_ASSERT (r_symndx != 0);
7069 /* Adjust the addend according to where the
7070 section winds up in the output section. */
7072 irela->r_addend += sec->output_offset;
7076 if (finfo->indices[r_symndx] == -1)
7078 unsigned long shlink;
7082 if (finfo->info->strip == strip_all)
7084 /* You can't do ld -r -s. */
7085 bfd_set_error (bfd_error_invalid_operation);
7089 /* This symbol was skipped earlier, but
7090 since it is needed by a reloc, we
7091 must output it now. */
7092 shlink = symtab_hdr->sh_link;
7093 name = (bfd_elf_string_from_elf_section
7094 (input_bfd, shlink, sym.st_name));
7098 osec = sec->output_section;
7100 _bfd_elf_section_from_bfd_section (output_bfd,
7102 if (sym.st_shndx == SHN_BAD)
7105 sym.st_value += sec->output_offset;
7106 if (! finfo->info->relocatable)
7108 sym.st_value += osec->vma;
7109 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7111 /* STT_TLS symbols are relative to PT_TLS
7113 BFD_ASSERT (elf_hash_table (finfo->info)
7115 sym.st_value -= (elf_hash_table (finfo->info)
7120 finfo->indices[r_symndx]
7121 = bfd_get_symcount (output_bfd);
7123 if (! elf_link_output_sym (finfo, name, &sym, sec,
7128 r_symndx = finfo->indices[r_symndx];
7131 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7132 | (irela->r_info & r_type_mask));
7135 /* Swap out the relocs. */
7136 if (bed->elf_backend_emit_relocs
7137 && !(finfo->info->relocatable
7138 || finfo->info->emitrelocations))
7139 reloc_emitter = bed->elf_backend_emit_relocs;
7141 reloc_emitter = _bfd_elf_link_output_relocs;
7143 if (input_rel_hdr->sh_size != 0
7144 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
7148 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7149 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7151 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7152 * bed->s->int_rels_per_ext_rel);
7153 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
7160 /* Write out the modified section contents. */
7161 if (bed->elf_backend_write_section
7162 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7164 /* Section written out. */
7166 else switch (o->sec_info_type)
7168 case ELF_INFO_TYPE_STABS:
7169 if (! (_bfd_write_section_stabs
7171 &elf_hash_table (finfo->info)->stab_info,
7172 o, &elf_section_data (o)->sec_info, contents)))
7175 case ELF_INFO_TYPE_MERGE:
7176 if (! _bfd_write_merged_section (output_bfd, o,
7177 elf_section_data (o)->sec_info))
7180 case ELF_INFO_TYPE_EH_FRAME:
7182 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7189 if (! (o->flags & SEC_EXCLUDE)
7190 && ! bfd_set_section_contents (output_bfd, o->output_section,
7192 (file_ptr) o->output_offset,
7203 /* Generate a reloc when linking an ELF file. This is a reloc
7204 requested by the linker, and does come from any input file. This
7205 is used to build constructor and destructor tables when linking
7209 elf_reloc_link_order (bfd *output_bfd,
7210 struct bfd_link_info *info,
7211 asection *output_section,
7212 struct bfd_link_order *link_order)
7214 reloc_howto_type *howto;
7218 struct elf_link_hash_entry **rel_hash_ptr;
7219 Elf_Internal_Shdr *rel_hdr;
7220 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7221 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7225 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7228 bfd_set_error (bfd_error_bad_value);
7232 addend = link_order->u.reloc.p->addend;
7234 /* Figure out the symbol index. */
7235 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7236 + elf_section_data (output_section)->rel_count
7237 + elf_section_data (output_section)->rel_count2);
7238 if (link_order->type == bfd_section_reloc_link_order)
7240 indx = link_order->u.reloc.p->u.section->target_index;
7241 BFD_ASSERT (indx != 0);
7242 *rel_hash_ptr = NULL;
7246 struct elf_link_hash_entry *h;
7248 /* Treat a reloc against a defined symbol as though it were
7249 actually against the section. */
7250 h = ((struct elf_link_hash_entry *)
7251 bfd_wrapped_link_hash_lookup (output_bfd, info,
7252 link_order->u.reloc.p->u.name,
7253 FALSE, FALSE, TRUE));
7255 && (h->root.type == bfd_link_hash_defined
7256 || h->root.type == bfd_link_hash_defweak))
7260 section = h->root.u.def.section;
7261 indx = section->output_section->target_index;
7262 *rel_hash_ptr = NULL;
7263 /* It seems that we ought to add the symbol value to the
7264 addend here, but in practice it has already been added
7265 because it was passed to constructor_callback. */
7266 addend += section->output_section->vma + section->output_offset;
7270 /* Setting the index to -2 tells elf_link_output_extsym that
7271 this symbol is used by a reloc. */
7278 if (! ((*info->callbacks->unattached_reloc)
7279 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7285 /* If this is an inplace reloc, we must write the addend into the
7287 if (howto->partial_inplace && addend != 0)
7290 bfd_reloc_status_type rstat;
7293 const char *sym_name;
7295 size = bfd_get_reloc_size (howto);
7296 buf = bfd_zmalloc (size);
7299 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7306 case bfd_reloc_outofrange:
7309 case bfd_reloc_overflow:
7310 if (link_order->type == bfd_section_reloc_link_order)
7311 sym_name = bfd_section_name (output_bfd,
7312 link_order->u.reloc.p->u.section);
7314 sym_name = link_order->u.reloc.p->u.name;
7315 if (! ((*info->callbacks->reloc_overflow)
7316 (info, NULL, sym_name, howto->name, addend, NULL,
7317 NULL, (bfd_vma) 0)))
7324 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7325 link_order->offset, size);
7331 /* The address of a reloc is relative to the section in a
7332 relocatable file, and is a virtual address in an executable
7334 offset = link_order->offset;
7335 if (! info->relocatable)
7336 offset += output_section->vma;
7338 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7340 irel[i].r_offset = offset;
7342 irel[i].r_addend = 0;
7344 if (bed->s->arch_size == 32)
7345 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7347 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7349 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7350 erel = rel_hdr->contents;
7351 if (rel_hdr->sh_type == SHT_REL)
7353 erel += (elf_section_data (output_section)->rel_count
7354 * bed->s->sizeof_rel);
7355 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7359 irel[0].r_addend = addend;
7360 erel += (elf_section_data (output_section)->rel_count
7361 * bed->s->sizeof_rela);
7362 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7365 ++elf_section_data (output_section)->rel_count;
7371 /* Get the output vma of the section pointed to by the sh_link field. */
7374 elf_get_linked_section_vma (struct bfd_link_order *p)
7376 Elf_Internal_Shdr **elf_shdrp;
7380 s = p->u.indirect.section;
7381 elf_shdrp = elf_elfsections (s->owner);
7382 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7383 elfsec = elf_shdrp[elfsec]->sh_link;
7385 The Intel C compiler generates SHT_IA_64_UNWIND with
7386 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7387 sh_info fields. Hence we could get the situation
7388 where elfsec is 0. */
7391 const struct elf_backend_data *bed
7392 = get_elf_backend_data (s->owner);
7393 if (bed->link_order_error_handler)
7394 bed->link_order_error_handler
7395 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7400 s = elf_shdrp[elfsec]->bfd_section;
7401 return s->output_section->vma + s->output_offset;
7406 /* Compare two sections based on the locations of the sections they are
7407 linked to. Used by elf_fixup_link_order. */
7410 compare_link_order (const void * a, const void * b)
7415 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7416 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7423 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7424 order as their linked sections. Returns false if this could not be done
7425 because an output section includes both ordered and unordered
7426 sections. Ideally we'd do this in the linker proper. */
7429 elf_fixup_link_order (bfd *abfd, asection *o)
7434 struct bfd_link_order *p;
7436 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7438 struct bfd_link_order **sections;
7444 for (p = o->link_order_head; p != NULL; p = p->next)
7446 if (p->type == bfd_indirect_link_order
7447 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7448 == bfd_target_elf_flavour)
7449 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7451 s = p->u.indirect.section;
7452 elfsec = _bfd_elf_section_from_bfd_section (sub, s);
7454 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7463 if (!seen_linkorder)
7466 if (seen_other && seen_linkorder)
7468 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7470 bfd_set_error (bfd_error_bad_value);
7474 sections = (struct bfd_link_order **)
7475 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7478 for (p = o->link_order_head; p != NULL; p = p->next)
7480 sections[seen_linkorder++] = p;
7482 /* Sort the input sections in the order of their linked section. */
7483 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7484 compare_link_order);
7486 /* Change the offsets of the sections. */
7488 for (n = 0; n < seen_linkorder; n++)
7490 s = sections[n]->u.indirect.section;
7491 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7492 s->output_offset = offset;
7493 sections[n]->offset = offset;
7494 offset += sections[n]->size;
7501 /* Do the final step of an ELF link. */
7504 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7506 bfd_boolean dynamic;
7507 bfd_boolean emit_relocs;
7509 struct elf_final_link_info finfo;
7510 register asection *o;
7511 register struct bfd_link_order *p;
7513 bfd_size_type max_contents_size;
7514 bfd_size_type max_external_reloc_size;
7515 bfd_size_type max_internal_reloc_count;
7516 bfd_size_type max_sym_count;
7517 bfd_size_type max_sym_shndx_count;
7519 Elf_Internal_Sym elfsym;
7521 Elf_Internal_Shdr *symtab_hdr;
7522 Elf_Internal_Shdr *symtab_shndx_hdr;
7523 Elf_Internal_Shdr *symstrtab_hdr;
7524 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7525 struct elf_outext_info eoinfo;
7527 size_t relativecount = 0;
7528 asection *reldyn = 0;
7531 if (! is_elf_hash_table (info->hash))
7535 abfd->flags |= DYNAMIC;
7537 dynamic = elf_hash_table (info)->dynamic_sections_created;
7538 dynobj = elf_hash_table (info)->dynobj;
7540 emit_relocs = (info->relocatable
7541 || info->emitrelocations
7542 || bed->elf_backend_emit_relocs);
7545 finfo.output_bfd = abfd;
7546 finfo.symstrtab = _bfd_elf_stringtab_init ();
7547 if (finfo.symstrtab == NULL)
7552 finfo.dynsym_sec = NULL;
7553 finfo.hash_sec = NULL;
7554 finfo.symver_sec = NULL;
7558 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7559 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7560 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7561 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7562 /* Note that it is OK if symver_sec is NULL. */
7565 finfo.contents = NULL;
7566 finfo.external_relocs = NULL;
7567 finfo.internal_relocs = NULL;
7568 finfo.external_syms = NULL;
7569 finfo.locsym_shndx = NULL;
7570 finfo.internal_syms = NULL;
7571 finfo.indices = NULL;
7572 finfo.sections = NULL;
7573 finfo.symbuf = NULL;
7574 finfo.symshndxbuf = NULL;
7575 finfo.symbuf_count = 0;
7576 finfo.shndxbuf_size = 0;
7578 /* Count up the number of relocations we will output for each output
7579 section, so that we know the sizes of the reloc sections. We
7580 also figure out some maximum sizes. */
7581 max_contents_size = 0;
7582 max_external_reloc_size = 0;
7583 max_internal_reloc_count = 0;
7585 max_sym_shndx_count = 0;
7587 for (o = abfd->sections; o != NULL; o = o->next)
7589 struct bfd_elf_section_data *esdo = elf_section_data (o);
7592 for (p = o->link_order_head; p != NULL; p = p->next)
7594 unsigned int reloc_count = 0;
7595 struct bfd_elf_section_data *esdi = NULL;
7596 unsigned int *rel_count1;
7598 if (p->type == bfd_section_reloc_link_order
7599 || p->type == bfd_symbol_reloc_link_order)
7601 else if (p->type == bfd_indirect_link_order)
7605 sec = p->u.indirect.section;
7606 esdi = elf_section_data (sec);
7608 /* Mark all sections which are to be included in the
7609 link. This will normally be every section. We need
7610 to do this so that we can identify any sections which
7611 the linker has decided to not include. */
7612 sec->linker_mark = TRUE;
7614 if (sec->flags & SEC_MERGE)
7617 if (info->relocatable || info->emitrelocations)
7618 reloc_count = sec->reloc_count;
7619 else if (bed->elf_backend_count_relocs)
7621 Elf_Internal_Rela * relocs;
7623 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7626 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7628 if (elf_section_data (o)->relocs != relocs)
7632 if (sec->rawsize > max_contents_size)
7633 max_contents_size = sec->rawsize;
7634 if (sec->size > max_contents_size)
7635 max_contents_size = sec->size;
7637 /* We are interested in just local symbols, not all
7639 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7640 && (sec->owner->flags & DYNAMIC) == 0)
7644 if (elf_bad_symtab (sec->owner))
7645 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7646 / bed->s->sizeof_sym);
7648 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7650 if (sym_count > max_sym_count)
7651 max_sym_count = sym_count;
7653 if (sym_count > max_sym_shndx_count
7654 && elf_symtab_shndx (sec->owner) != 0)
7655 max_sym_shndx_count = sym_count;
7657 if ((sec->flags & SEC_RELOC) != 0)
7661 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7662 if (ext_size > max_external_reloc_size)
7663 max_external_reloc_size = ext_size;
7664 if (sec->reloc_count > max_internal_reloc_count)
7665 max_internal_reloc_count = sec->reloc_count;
7670 if (reloc_count == 0)
7673 o->reloc_count += reloc_count;
7675 /* MIPS may have a mix of REL and RELA relocs on sections.
7676 To support this curious ABI we keep reloc counts in
7677 elf_section_data too. We must be careful to add the
7678 relocations from the input section to the right output
7679 count. FIXME: Get rid of one count. We have
7680 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7681 rel_count1 = &esdo->rel_count;
7684 bfd_boolean same_size;
7685 bfd_size_type entsize1;
7687 entsize1 = esdi->rel_hdr.sh_entsize;
7688 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7689 || entsize1 == bed->s->sizeof_rela);
7690 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7693 rel_count1 = &esdo->rel_count2;
7695 if (esdi->rel_hdr2 != NULL)
7697 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7698 unsigned int alt_count;
7699 unsigned int *rel_count2;
7701 BFD_ASSERT (entsize2 != entsize1
7702 && (entsize2 == bed->s->sizeof_rel
7703 || entsize2 == bed->s->sizeof_rela));
7705 rel_count2 = &esdo->rel_count2;
7707 rel_count2 = &esdo->rel_count;
7709 /* The following is probably too simplistic if the
7710 backend counts output relocs unusually. */
7711 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7712 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7713 *rel_count2 += alt_count;
7714 reloc_count -= alt_count;
7717 *rel_count1 += reloc_count;
7720 if (o->reloc_count > 0)
7721 o->flags |= SEC_RELOC;
7724 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7725 set it (this is probably a bug) and if it is set
7726 assign_section_numbers will create a reloc section. */
7727 o->flags &=~ SEC_RELOC;
7730 /* If the SEC_ALLOC flag is not set, force the section VMA to
7731 zero. This is done in elf_fake_sections as well, but forcing
7732 the VMA to 0 here will ensure that relocs against these
7733 sections are handled correctly. */
7734 if ((o->flags & SEC_ALLOC) == 0
7735 && ! o->user_set_vma)
7739 if (! info->relocatable && merged)
7740 elf_link_hash_traverse (elf_hash_table (info),
7741 _bfd_elf_link_sec_merge_syms, abfd);
7743 /* Figure out the file positions for everything but the symbol table
7744 and the relocs. We set symcount to force assign_section_numbers
7745 to create a symbol table. */
7746 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7747 BFD_ASSERT (! abfd->output_has_begun);
7748 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7751 /* Set sizes, and assign file positions for reloc sections. */
7752 for (o = abfd->sections; o != NULL; o = o->next)
7754 if ((o->flags & SEC_RELOC) != 0)
7756 if (!(_bfd_elf_link_size_reloc_section
7757 (abfd, &elf_section_data (o)->rel_hdr, o)))
7760 if (elf_section_data (o)->rel_hdr2
7761 && !(_bfd_elf_link_size_reloc_section
7762 (abfd, elf_section_data (o)->rel_hdr2, o)))
7766 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7767 to count upwards while actually outputting the relocations. */
7768 elf_section_data (o)->rel_count = 0;
7769 elf_section_data (o)->rel_count2 = 0;
7772 _bfd_elf_assign_file_positions_for_relocs (abfd);
7774 /* We have now assigned file positions for all the sections except
7775 .symtab and .strtab. We start the .symtab section at the current
7776 file position, and write directly to it. We build the .strtab
7777 section in memory. */
7778 bfd_get_symcount (abfd) = 0;
7779 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7780 /* sh_name is set in prep_headers. */
7781 symtab_hdr->sh_type = SHT_SYMTAB;
7782 /* sh_flags, sh_addr and sh_size all start off zero. */
7783 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7784 /* sh_link is set in assign_section_numbers. */
7785 /* sh_info is set below. */
7786 /* sh_offset is set just below. */
7787 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
7789 off = elf_tdata (abfd)->next_file_pos;
7790 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
7792 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7793 incorrect. We do not yet know the size of the .symtab section.
7794 We correct next_file_pos below, after we do know the size. */
7796 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7797 continuously seeking to the right position in the file. */
7798 if (! info->keep_memory || max_sym_count < 20)
7799 finfo.symbuf_size = 20;
7801 finfo.symbuf_size = max_sym_count;
7802 amt = finfo.symbuf_size;
7803 amt *= bed->s->sizeof_sym;
7804 finfo.symbuf = bfd_malloc (amt);
7805 if (finfo.symbuf == NULL)
7807 if (elf_numsections (abfd) > SHN_LORESERVE)
7809 /* Wild guess at number of output symbols. realloc'd as needed. */
7810 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
7811 finfo.shndxbuf_size = amt;
7812 amt *= sizeof (Elf_External_Sym_Shndx);
7813 finfo.symshndxbuf = bfd_zmalloc (amt);
7814 if (finfo.symshndxbuf == NULL)
7818 /* Start writing out the symbol table. The first symbol is always a
7820 if (info->strip != strip_all
7823 elfsym.st_value = 0;
7826 elfsym.st_other = 0;
7827 elfsym.st_shndx = SHN_UNDEF;
7828 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
7833 /* Output a symbol for each section. We output these even if we are
7834 discarding local symbols, since they are used for relocs. These
7835 symbols have no names. We store the index of each one in the
7836 index field of the section, so that we can find it again when
7837 outputting relocs. */
7838 if (info->strip != strip_all
7842 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7843 elfsym.st_other = 0;
7844 for (i = 1; i < elf_numsections (abfd); i++)
7846 o = bfd_section_from_elf_index (abfd, i);
7848 o->target_index = bfd_get_symcount (abfd);
7849 elfsym.st_shndx = i;
7850 if (info->relocatable || o == NULL)
7851 elfsym.st_value = 0;
7853 elfsym.st_value = o->vma;
7854 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
7856 if (i == SHN_LORESERVE - 1)
7857 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
7861 /* Allocate some memory to hold information read in from the input
7863 if (max_contents_size != 0)
7865 finfo.contents = bfd_malloc (max_contents_size);
7866 if (finfo.contents == NULL)
7870 if (max_external_reloc_size != 0)
7872 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
7873 if (finfo.external_relocs == NULL)
7877 if (max_internal_reloc_count != 0)
7879 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
7880 amt *= sizeof (Elf_Internal_Rela);
7881 finfo.internal_relocs = bfd_malloc (amt);
7882 if (finfo.internal_relocs == NULL)
7886 if (max_sym_count != 0)
7888 amt = max_sym_count * bed->s->sizeof_sym;
7889 finfo.external_syms = bfd_malloc (amt);
7890 if (finfo.external_syms == NULL)
7893 amt = max_sym_count * sizeof (Elf_Internal_Sym);
7894 finfo.internal_syms = bfd_malloc (amt);
7895 if (finfo.internal_syms == NULL)
7898 amt = max_sym_count * sizeof (long);
7899 finfo.indices = bfd_malloc (amt);
7900 if (finfo.indices == NULL)
7903 amt = max_sym_count * sizeof (asection *);
7904 finfo.sections = bfd_malloc (amt);
7905 if (finfo.sections == NULL)
7909 if (max_sym_shndx_count != 0)
7911 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
7912 finfo.locsym_shndx = bfd_malloc (amt);
7913 if (finfo.locsym_shndx == NULL)
7917 if (elf_hash_table (info)->tls_sec)
7919 bfd_vma base, end = 0;
7922 for (sec = elf_hash_table (info)->tls_sec;
7923 sec && (sec->flags & SEC_THREAD_LOCAL);
7926 bfd_vma size = sec->size;
7928 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
7930 struct bfd_link_order *o;
7932 for (o = sec->link_order_head; o != NULL; o = o->next)
7933 if (size < o->offset + o->size)
7934 size = o->offset + o->size;
7936 end = sec->vma + size;
7938 base = elf_hash_table (info)->tls_sec->vma;
7939 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
7940 elf_hash_table (info)->tls_size = end - base;
7943 /* Reorder SHF_LINK_ORDER sections. */
7944 for (o = abfd->sections; o != NULL; o = o->next)
7946 if (!elf_fixup_link_order (abfd, o))
7950 /* Since ELF permits relocations to be against local symbols, we
7951 must have the local symbols available when we do the relocations.
7952 Since we would rather only read the local symbols once, and we
7953 would rather not keep them in memory, we handle all the
7954 relocations for a single input file at the same time.
7956 Unfortunately, there is no way to know the total number of local
7957 symbols until we have seen all of them, and the local symbol
7958 indices precede the global symbol indices. This means that when
7959 we are generating relocatable output, and we see a reloc against
7960 a global symbol, we can not know the symbol index until we have
7961 finished examining all the local symbols to see which ones we are
7962 going to output. To deal with this, we keep the relocations in
7963 memory, and don't output them until the end of the link. This is
7964 an unfortunate waste of memory, but I don't see a good way around
7965 it. Fortunately, it only happens when performing a relocatable
7966 link, which is not the common case. FIXME: If keep_memory is set
7967 we could write the relocs out and then read them again; I don't
7968 know how bad the memory loss will be. */
7970 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7971 sub->output_has_begun = FALSE;
7972 for (o = abfd->sections; o != NULL; o = o->next)
7974 for (p = o->link_order_head; p != NULL; p = p->next)
7976 if (p->type == bfd_indirect_link_order
7977 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7978 == bfd_target_elf_flavour)
7979 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7981 if (! sub->output_has_begun)
7983 if (! elf_link_input_bfd (&finfo, sub))
7985 sub->output_has_begun = TRUE;
7988 else if (p->type == bfd_section_reloc_link_order
7989 || p->type == bfd_symbol_reloc_link_order)
7991 if (! elf_reloc_link_order (abfd, info, o, p))
7996 if (! _bfd_default_link_order (abfd, info, o, p))
8002 /* Output any global symbols that got converted to local in a
8003 version script or due to symbol visibility. We do this in a
8004 separate step since ELF requires all local symbols to appear
8005 prior to any global symbols. FIXME: We should only do this if
8006 some global symbols were, in fact, converted to become local.
8007 FIXME: Will this work correctly with the Irix 5 linker? */
8008 eoinfo.failed = FALSE;
8009 eoinfo.finfo = &finfo;
8010 eoinfo.localsyms = TRUE;
8011 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8016 /* That wrote out all the local symbols. Finish up the symbol table
8017 with the global symbols. Even if we want to strip everything we
8018 can, we still need to deal with those global symbols that got
8019 converted to local in a version script. */
8021 /* The sh_info field records the index of the first non local symbol. */
8022 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8025 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8027 Elf_Internal_Sym sym;
8028 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8029 long last_local = 0;
8031 /* Write out the section symbols for the output sections. */
8038 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8041 for (s = abfd->sections; s != NULL; s = s->next)
8047 dynindx = elf_section_data (s)->dynindx;
8050 indx = elf_section_data (s)->this_idx;
8051 BFD_ASSERT (indx > 0);
8052 sym.st_shndx = indx;
8053 sym.st_value = s->vma;
8054 dest = dynsym + dynindx * bed->s->sizeof_sym;
8055 if (last_local < dynindx)
8056 last_local = dynindx;
8057 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8061 /* Write out the local dynsyms. */
8062 if (elf_hash_table (info)->dynlocal)
8064 struct elf_link_local_dynamic_entry *e;
8065 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8070 sym.st_size = e->isym.st_size;
8071 sym.st_other = e->isym.st_other;
8073 /* Copy the internal symbol as is.
8074 Note that we saved a word of storage and overwrote
8075 the original st_name with the dynstr_index. */
8078 if (e->isym.st_shndx != SHN_UNDEF
8079 && (e->isym.st_shndx < SHN_LORESERVE
8080 || e->isym.st_shndx > SHN_HIRESERVE))
8082 s = bfd_section_from_elf_index (e->input_bfd,
8086 elf_section_data (s->output_section)->this_idx;
8087 sym.st_value = (s->output_section->vma
8089 + e->isym.st_value);
8092 if (last_local < e->dynindx)
8093 last_local = e->dynindx;
8095 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8096 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8100 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8104 /* We get the global symbols from the hash table. */
8105 eoinfo.failed = FALSE;
8106 eoinfo.localsyms = FALSE;
8107 eoinfo.finfo = &finfo;
8108 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8113 /* If backend needs to output some symbols not present in the hash
8114 table, do it now. */
8115 if (bed->elf_backend_output_arch_syms)
8117 typedef bfd_boolean (*out_sym_func)
8118 (void *, const char *, Elf_Internal_Sym *, asection *,
8119 struct elf_link_hash_entry *);
8121 if (! ((*bed->elf_backend_output_arch_syms)
8122 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8126 /* Flush all symbols to the file. */
8127 if (! elf_link_flush_output_syms (&finfo, bed))
8130 /* Now we know the size of the symtab section. */
8131 off += symtab_hdr->sh_size;
8133 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8134 if (symtab_shndx_hdr->sh_name != 0)
8136 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8137 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8138 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8139 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8140 symtab_shndx_hdr->sh_size = amt;
8142 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8145 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8146 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8151 /* Finish up and write out the symbol string table (.strtab)
8153 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8154 /* sh_name was set in prep_headers. */
8155 symstrtab_hdr->sh_type = SHT_STRTAB;
8156 symstrtab_hdr->sh_flags = 0;
8157 symstrtab_hdr->sh_addr = 0;
8158 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8159 symstrtab_hdr->sh_entsize = 0;
8160 symstrtab_hdr->sh_link = 0;
8161 symstrtab_hdr->sh_info = 0;
8162 /* sh_offset is set just below. */
8163 symstrtab_hdr->sh_addralign = 1;
8165 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8166 elf_tdata (abfd)->next_file_pos = off;
8168 if (bfd_get_symcount (abfd) > 0)
8170 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8171 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8175 /* Adjust the relocs to have the correct symbol indices. */
8176 for (o = abfd->sections; o != NULL; o = o->next)
8178 if ((o->flags & SEC_RELOC) == 0)
8181 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8182 elf_section_data (o)->rel_count,
8183 elf_section_data (o)->rel_hashes);
8184 if (elf_section_data (o)->rel_hdr2 != NULL)
8185 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8186 elf_section_data (o)->rel_count2,
8187 (elf_section_data (o)->rel_hashes
8188 + elf_section_data (o)->rel_count));
8190 /* Set the reloc_count field to 0 to prevent write_relocs from
8191 trying to swap the relocs out itself. */
8195 if (dynamic && info->combreloc && dynobj != NULL)
8196 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8198 /* If we are linking against a dynamic object, or generating a
8199 shared library, finish up the dynamic linking information. */
8202 bfd_byte *dyncon, *dynconend;
8204 /* Fix up .dynamic entries. */
8205 o = bfd_get_section_by_name (dynobj, ".dynamic");
8206 BFD_ASSERT (o != NULL);
8208 dyncon = o->contents;
8209 dynconend = o->contents + o->size;
8210 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8212 Elf_Internal_Dyn dyn;
8216 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8223 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8225 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8227 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8228 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8231 dyn.d_un.d_val = relativecount;
8238 name = info->init_function;
8241 name = info->fini_function;
8244 struct elf_link_hash_entry *h;
8246 h = elf_link_hash_lookup (elf_hash_table (info), name,
8247 FALSE, FALSE, TRUE);
8249 && (h->root.type == bfd_link_hash_defined
8250 || h->root.type == bfd_link_hash_defweak))
8252 dyn.d_un.d_val = h->root.u.def.value;
8253 o = h->root.u.def.section;
8254 if (o->output_section != NULL)
8255 dyn.d_un.d_val += (o->output_section->vma
8256 + o->output_offset);
8259 /* The symbol is imported from another shared
8260 library and does not apply to this one. */
8268 case DT_PREINIT_ARRAYSZ:
8269 name = ".preinit_array";
8271 case DT_INIT_ARRAYSZ:
8272 name = ".init_array";
8274 case DT_FINI_ARRAYSZ:
8275 name = ".fini_array";
8277 o = bfd_get_section_by_name (abfd, name);
8280 (*_bfd_error_handler)
8281 (_("%B: could not find output section %s"), abfd, name);
8285 (*_bfd_error_handler)
8286 (_("warning: %s section has zero size"), name);
8287 dyn.d_un.d_val = o->size;
8290 case DT_PREINIT_ARRAY:
8291 name = ".preinit_array";
8294 name = ".init_array";
8297 name = ".fini_array";
8310 name = ".gnu.version_d";
8313 name = ".gnu.version_r";
8316 name = ".gnu.version";
8318 o = bfd_get_section_by_name (abfd, name);
8321 (*_bfd_error_handler)
8322 (_("%B: could not find output section %s"), abfd, name);
8325 dyn.d_un.d_ptr = o->vma;
8332 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8337 for (i = 1; i < elf_numsections (abfd); i++)
8339 Elf_Internal_Shdr *hdr;
8341 hdr = elf_elfsections (abfd)[i];
8342 if (hdr->sh_type == type
8343 && (hdr->sh_flags & SHF_ALLOC) != 0)
8345 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8346 dyn.d_un.d_val += hdr->sh_size;
8349 if (dyn.d_un.d_val == 0
8350 || hdr->sh_addr < dyn.d_un.d_val)
8351 dyn.d_un.d_val = hdr->sh_addr;
8357 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8361 /* If we have created any dynamic sections, then output them. */
8364 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8367 for (o = dynobj->sections; o != NULL; o = o->next)
8369 if ((o->flags & SEC_HAS_CONTENTS) == 0
8371 || o->output_section == bfd_abs_section_ptr)
8373 if ((o->flags & SEC_LINKER_CREATED) == 0)
8375 /* At this point, we are only interested in sections
8376 created by _bfd_elf_link_create_dynamic_sections. */
8379 if (elf_hash_table (info)->stab_info.stabstr == o)
8381 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8383 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8385 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8387 if (! bfd_set_section_contents (abfd, o->output_section,
8389 (file_ptr) o->output_offset,
8395 /* The contents of the .dynstr section are actually in a
8397 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8398 if (bfd_seek (abfd, off, SEEK_SET) != 0
8399 || ! _bfd_elf_strtab_emit (abfd,
8400 elf_hash_table (info)->dynstr))
8406 if (info->relocatable)
8408 bfd_boolean failed = FALSE;
8410 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8415 /* If we have optimized stabs strings, output them. */
8416 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8418 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8422 if (info->eh_frame_hdr)
8424 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8428 if (finfo.symstrtab != NULL)
8429 _bfd_stringtab_free (finfo.symstrtab);
8430 if (finfo.contents != NULL)
8431 free (finfo.contents);
8432 if (finfo.external_relocs != NULL)
8433 free (finfo.external_relocs);
8434 if (finfo.internal_relocs != NULL)
8435 free (finfo.internal_relocs);
8436 if (finfo.external_syms != NULL)
8437 free (finfo.external_syms);
8438 if (finfo.locsym_shndx != NULL)
8439 free (finfo.locsym_shndx);
8440 if (finfo.internal_syms != NULL)
8441 free (finfo.internal_syms);
8442 if (finfo.indices != NULL)
8443 free (finfo.indices);
8444 if (finfo.sections != NULL)
8445 free (finfo.sections);
8446 if (finfo.symbuf != NULL)
8447 free (finfo.symbuf);
8448 if (finfo.symshndxbuf != NULL)
8449 free (finfo.symshndxbuf);
8450 for (o = abfd->sections; o != NULL; o = o->next)
8452 if ((o->flags & SEC_RELOC) != 0
8453 && elf_section_data (o)->rel_hashes != NULL)
8454 free (elf_section_data (o)->rel_hashes);
8457 elf_tdata (abfd)->linker = TRUE;
8462 if (finfo.symstrtab != NULL)
8463 _bfd_stringtab_free (finfo.symstrtab);
8464 if (finfo.contents != NULL)
8465 free (finfo.contents);
8466 if (finfo.external_relocs != NULL)
8467 free (finfo.external_relocs);
8468 if (finfo.internal_relocs != NULL)
8469 free (finfo.internal_relocs);
8470 if (finfo.external_syms != NULL)
8471 free (finfo.external_syms);
8472 if (finfo.locsym_shndx != NULL)
8473 free (finfo.locsym_shndx);
8474 if (finfo.internal_syms != NULL)
8475 free (finfo.internal_syms);
8476 if (finfo.indices != NULL)
8477 free (finfo.indices);
8478 if (finfo.sections != NULL)
8479 free (finfo.sections);
8480 if (finfo.symbuf != NULL)
8481 free (finfo.symbuf);
8482 if (finfo.symshndxbuf != NULL)
8483 free (finfo.symshndxbuf);
8484 for (o = abfd->sections; o != NULL; o = o->next)
8486 if ((o->flags & SEC_RELOC) != 0
8487 && elf_section_data (o)->rel_hashes != NULL)
8488 free (elf_section_data (o)->rel_hashes);
8494 /* Garbage collect unused sections. */
8496 /* The mark phase of garbage collection. For a given section, mark
8497 it and any sections in this section's group, and all the sections
8498 which define symbols to which it refers. */
8500 typedef asection * (*gc_mark_hook_fn)
8501 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8502 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8505 _bfd_elf_gc_mark (struct bfd_link_info *info,
8507 gc_mark_hook_fn gc_mark_hook)
8510 asection *group_sec;
8514 /* Mark all the sections in the group. */
8515 group_sec = elf_section_data (sec)->next_in_group;
8516 if (group_sec && !group_sec->gc_mark)
8517 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8520 /* Look through the section relocs. */
8522 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8524 Elf_Internal_Rela *relstart, *rel, *relend;
8525 Elf_Internal_Shdr *symtab_hdr;
8526 struct elf_link_hash_entry **sym_hashes;
8529 bfd *input_bfd = sec->owner;
8530 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8531 Elf_Internal_Sym *isym = NULL;
8534 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8535 sym_hashes = elf_sym_hashes (input_bfd);
8537 /* Read the local symbols. */
8538 if (elf_bad_symtab (input_bfd))
8540 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8544 extsymoff = nlocsyms = symtab_hdr->sh_info;
8546 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8547 if (isym == NULL && nlocsyms != 0)
8549 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8555 /* Read the relocations. */
8556 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8558 if (relstart == NULL)
8563 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8565 if (bed->s->arch_size == 32)
8570 for (rel = relstart; rel < relend; rel++)
8572 unsigned long r_symndx;
8574 struct elf_link_hash_entry *h;
8576 r_symndx = rel->r_info >> r_sym_shift;
8580 if (r_symndx >= nlocsyms
8581 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8583 h = sym_hashes[r_symndx - extsymoff];
8584 while (h->root.type == bfd_link_hash_indirect
8585 || h->root.type == bfd_link_hash_warning)
8586 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8587 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8591 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8594 if (rsec && !rsec->gc_mark)
8596 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8598 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8607 if (elf_section_data (sec)->relocs != relstart)
8610 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8612 if (! info->keep_memory)
8615 symtab_hdr->contents = (unsigned char *) isym;
8622 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8625 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8629 if (h->root.type == bfd_link_hash_warning)
8630 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8632 if (h->dynindx != -1
8633 && ((h->root.type != bfd_link_hash_defined
8634 && h->root.type != bfd_link_hash_defweak)
8635 || h->root.u.def.section->gc_mark))
8636 h->dynindx = (*idx)++;
8641 /* The sweep phase of garbage collection. Remove all garbage sections. */
8643 typedef bfd_boolean (*gc_sweep_hook_fn)
8644 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8647 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8651 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8655 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8658 for (o = sub->sections; o != NULL; o = o->next)
8660 /* Keep debug and special sections. */
8661 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8662 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8668 /* Skip sweeping sections already excluded. */
8669 if (o->flags & SEC_EXCLUDE)
8672 /* Since this is early in the link process, it is simple
8673 to remove a section from the output. */
8674 o->flags |= SEC_EXCLUDE;
8676 /* But we also have to update some of the relocation
8677 info we collected before. */
8679 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
8681 Elf_Internal_Rela *internal_relocs;
8685 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8687 if (internal_relocs == NULL)
8690 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8692 if (elf_section_data (o)->relocs != internal_relocs)
8693 free (internal_relocs);
8701 /* Remove the symbols that were in the swept sections from the dynamic
8702 symbol table. GCFIXME: Anyone know how to get them out of the
8703 static symbol table as well? */
8707 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8709 elf_hash_table (info)->dynsymcount = i;
8715 /* Propagate collected vtable information. This is called through
8716 elf_link_hash_traverse. */
8719 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8721 if (h->root.type == bfd_link_hash_warning)
8722 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8724 /* Those that are not vtables. */
8725 if (h->vtable == NULL || h->vtable->parent == NULL)
8728 /* Those vtables that do not have parents, we cannot merge. */
8729 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
8732 /* If we've already been done, exit. */
8733 if (h->vtable->used && h->vtable->used[-1])
8736 /* Make sure the parent's table is up to date. */
8737 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
8739 if (h->vtable->used == NULL)
8741 /* None of this table's entries were referenced. Re-use the
8743 h->vtable->used = h->vtable->parent->vtable->used;
8744 h->vtable->size = h->vtable->parent->vtable->size;
8749 bfd_boolean *cu, *pu;
8751 /* Or the parent's entries into ours. */
8752 cu = h->vtable->used;
8754 pu = h->vtable->parent->vtable->used;
8757 const struct elf_backend_data *bed;
8758 unsigned int log_file_align;
8760 bed = get_elf_backend_data (h->root.u.def.section->owner);
8761 log_file_align = bed->s->log_file_align;
8762 n = h->vtable->parent->vtable->size >> log_file_align;
8777 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
8780 bfd_vma hstart, hend;
8781 Elf_Internal_Rela *relstart, *relend, *rel;
8782 const struct elf_backend_data *bed;
8783 unsigned int log_file_align;
8785 if (h->root.type == bfd_link_hash_warning)
8786 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8788 /* Take care of both those symbols that do not describe vtables as
8789 well as those that are not loaded. */
8790 if (h->vtable == NULL || h->vtable->parent == NULL)
8793 BFD_ASSERT (h->root.type == bfd_link_hash_defined
8794 || h->root.type == bfd_link_hash_defweak);
8796 sec = h->root.u.def.section;
8797 hstart = h->root.u.def.value;
8798 hend = hstart + h->size;
8800 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
8802 return *(bfd_boolean *) okp = FALSE;
8803 bed = get_elf_backend_data (sec->owner);
8804 log_file_align = bed->s->log_file_align;
8806 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8808 for (rel = relstart; rel < relend; ++rel)
8809 if (rel->r_offset >= hstart && rel->r_offset < hend)
8811 /* If the entry is in use, do nothing. */
8813 && (rel->r_offset - hstart) < h->vtable->size)
8815 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
8816 if (h->vtable->used[entry])
8819 /* Otherwise, kill it. */
8820 rel->r_offset = rel->r_info = rel->r_addend = 0;
8826 /* Mark sections containing dynamically referenced symbols. This is called
8827 through elf_link_hash_traverse. */
8830 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h,
8831 void *okp ATTRIBUTE_UNUSED)
8833 if (h->root.type == bfd_link_hash_warning)
8834 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8836 if ((h->root.type == bfd_link_hash_defined
8837 || h->root.type == bfd_link_hash_defweak)
8839 h->root.u.def.section->flags |= SEC_KEEP;
8844 /* Do mark and sweep of unused sections. */
8847 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
8849 bfd_boolean ok = TRUE;
8851 asection * (*gc_mark_hook)
8852 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8853 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
8855 if (!get_elf_backend_data (abfd)->can_gc_sections
8856 || info->relocatable
8857 || info->emitrelocations
8859 || !is_elf_hash_table (info->hash))
8861 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
8865 /* Apply transitive closure to the vtable entry usage info. */
8866 elf_link_hash_traverse (elf_hash_table (info),
8867 elf_gc_propagate_vtable_entries_used,
8872 /* Kill the vtable relocations that were not used. */
8873 elf_link_hash_traverse (elf_hash_table (info),
8874 elf_gc_smash_unused_vtentry_relocs,
8879 /* Mark dynamically referenced symbols. */
8880 if (elf_hash_table (info)->dynamic_sections_created)
8881 elf_link_hash_traverse (elf_hash_table (info),
8882 elf_gc_mark_dynamic_ref_symbol,
8887 /* Grovel through relocs to find out who stays ... */
8888 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
8889 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8893 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8896 for (o = sub->sections; o != NULL; o = o->next)
8898 if (o->flags & SEC_KEEP)
8900 /* _bfd_elf_discard_section_eh_frame knows how to discard
8901 orphaned FDEs so don't mark sections referenced by the
8902 EH frame section. */
8903 if (strcmp (o->name, ".eh_frame") == 0)
8905 else if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
8911 /* ... and mark SEC_EXCLUDE for those that go. */
8912 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
8918 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8921 bfd_elf_gc_record_vtinherit (bfd *abfd,
8923 struct elf_link_hash_entry *h,
8926 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
8927 struct elf_link_hash_entry **search, *child;
8928 bfd_size_type extsymcount;
8929 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8931 /* The sh_info field of the symtab header tells us where the
8932 external symbols start. We don't care about the local symbols at
8934 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
8935 if (!elf_bad_symtab (abfd))
8936 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8938 sym_hashes = elf_sym_hashes (abfd);
8939 sym_hashes_end = sym_hashes + extsymcount;
8941 /* Hunt down the child symbol, which is in this section at the same
8942 offset as the relocation. */
8943 for (search = sym_hashes; search != sym_hashes_end; ++search)
8945 if ((child = *search) != NULL
8946 && (child->root.type == bfd_link_hash_defined
8947 || child->root.type == bfd_link_hash_defweak)
8948 && child->root.u.def.section == sec
8949 && child->root.u.def.value == offset)
8953 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
8954 abfd, sec, (unsigned long) offset);
8955 bfd_set_error (bfd_error_invalid_operation);
8961 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
8967 /* This *should* only be the absolute section. It could potentially
8968 be that someone has defined a non-global vtable though, which
8969 would be bad. It isn't worth paging in the local symbols to be
8970 sure though; that case should simply be handled by the assembler. */
8972 child->vtable->parent = (struct elf_link_hash_entry *) -1;
8975 child->vtable->parent = h;
8980 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8983 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
8984 asection *sec ATTRIBUTE_UNUSED,
8985 struct elf_link_hash_entry *h,
8988 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8989 unsigned int log_file_align = bed->s->log_file_align;
8993 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
8998 if (addend >= h->vtable->size)
9000 size_t size, bytes, file_align;
9001 bfd_boolean *ptr = h->vtable->used;
9003 /* While the symbol is undefined, we have to be prepared to handle
9005 file_align = 1 << log_file_align;
9006 if (h->root.type == bfd_link_hash_undefined)
9007 size = addend + file_align;
9013 /* Oops! We've got a reference past the defined end of
9014 the table. This is probably a bug -- shall we warn? */
9015 size = addend + file_align;
9018 size = (size + file_align - 1) & -file_align;
9020 /* Allocate one extra entry for use as a "done" flag for the
9021 consolidation pass. */
9022 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9026 ptr = bfd_realloc (ptr - 1, bytes);
9032 oldbytes = (((h->vtable->size >> log_file_align) + 1)
9033 * sizeof (bfd_boolean));
9034 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9038 ptr = bfd_zmalloc (bytes);
9043 /* And arrange for that done flag to be at index -1. */
9044 h->vtable->used = ptr + 1;
9045 h->vtable->size = size;
9048 h->vtable->used[addend >> log_file_align] = TRUE;
9053 struct alloc_got_off_arg {
9055 unsigned int got_elt_size;
9058 /* We need a special top-level link routine to convert got reference counts
9059 to real got offsets. */
9062 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9064 struct alloc_got_off_arg *gofarg = arg;
9066 if (h->root.type == bfd_link_hash_warning)
9067 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9069 if (h->got.refcount > 0)
9071 h->got.offset = gofarg->gotoff;
9072 gofarg->gotoff += gofarg->got_elt_size;
9075 h->got.offset = (bfd_vma) -1;
9080 /* And an accompanying bit to work out final got entry offsets once
9081 we're done. Should be called from final_link. */
9084 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9085 struct bfd_link_info *info)
9088 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9090 unsigned int got_elt_size = bed->s->arch_size / 8;
9091 struct alloc_got_off_arg gofarg;
9093 if (! is_elf_hash_table (info->hash))
9096 /* The GOT offset is relative to the .got section, but the GOT header is
9097 put into the .got.plt section, if the backend uses it. */
9098 if (bed->want_got_plt)
9101 gotoff = bed->got_header_size;
9103 /* Do the local .got entries first. */
9104 for (i = info->input_bfds; i; i = i->link_next)
9106 bfd_signed_vma *local_got;
9107 bfd_size_type j, locsymcount;
9108 Elf_Internal_Shdr *symtab_hdr;
9110 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9113 local_got = elf_local_got_refcounts (i);
9117 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9118 if (elf_bad_symtab (i))
9119 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9121 locsymcount = symtab_hdr->sh_info;
9123 for (j = 0; j < locsymcount; ++j)
9125 if (local_got[j] > 0)
9127 local_got[j] = gotoff;
9128 gotoff += got_elt_size;
9131 local_got[j] = (bfd_vma) -1;
9135 /* Then the global .got entries. .plt refcounts are handled by
9136 adjust_dynamic_symbol */
9137 gofarg.gotoff = gotoff;
9138 gofarg.got_elt_size = got_elt_size;
9139 elf_link_hash_traverse (elf_hash_table (info),
9140 elf_gc_allocate_got_offsets,
9145 /* Many folk need no more in the way of final link than this, once
9146 got entry reference counting is enabled. */
9149 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9151 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9154 /* Invoke the regular ELF backend linker to do all the work. */
9155 return bfd_elf_final_link (abfd, info);
9159 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9161 struct elf_reloc_cookie *rcookie = cookie;
9163 if (rcookie->bad_symtab)
9164 rcookie->rel = rcookie->rels;
9166 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9168 unsigned long r_symndx;
9170 if (! rcookie->bad_symtab)
9171 if (rcookie->rel->r_offset > offset)
9173 if (rcookie->rel->r_offset != offset)
9176 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9177 if (r_symndx == SHN_UNDEF)
9180 if (r_symndx >= rcookie->locsymcount
9181 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9183 struct elf_link_hash_entry *h;
9185 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9187 while (h->root.type == bfd_link_hash_indirect
9188 || h->root.type == bfd_link_hash_warning)
9189 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9191 if ((h->root.type == bfd_link_hash_defined
9192 || h->root.type == bfd_link_hash_defweak)
9193 && elf_discarded_section (h->root.u.def.section))
9200 /* It's not a relocation against a global symbol,
9201 but it could be a relocation against a local
9202 symbol for a discarded section. */
9204 Elf_Internal_Sym *isym;
9206 /* Need to: get the symbol; get the section. */
9207 isym = &rcookie->locsyms[r_symndx];
9208 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9210 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9211 if (isec != NULL && elf_discarded_section (isec))
9220 /* Discard unneeded references to discarded sections.
9221 Returns TRUE if any section's size was changed. */
9222 /* This function assumes that the relocations are in sorted order,
9223 which is true for all known assemblers. */
9226 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9228 struct elf_reloc_cookie cookie;
9229 asection *stab, *eh;
9230 Elf_Internal_Shdr *symtab_hdr;
9231 const struct elf_backend_data *bed;
9234 bfd_boolean ret = FALSE;
9236 if (info->traditional_format
9237 || !is_elf_hash_table (info->hash))
9240 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9242 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9245 bed = get_elf_backend_data (abfd);
9247 if ((abfd->flags & DYNAMIC) != 0)
9250 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9251 if (info->relocatable
9254 || bfd_is_abs_section (eh->output_section))))
9257 stab = bfd_get_section_by_name (abfd, ".stab");
9260 || bfd_is_abs_section (stab->output_section)
9261 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9266 && bed->elf_backend_discard_info == NULL)
9269 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9271 cookie.sym_hashes = elf_sym_hashes (abfd);
9272 cookie.bad_symtab = elf_bad_symtab (abfd);
9273 if (cookie.bad_symtab)
9275 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9276 cookie.extsymoff = 0;
9280 cookie.locsymcount = symtab_hdr->sh_info;
9281 cookie.extsymoff = symtab_hdr->sh_info;
9284 if (bed->s->arch_size == 32)
9285 cookie.r_sym_shift = 8;
9287 cookie.r_sym_shift = 32;
9289 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9290 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9292 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9293 cookie.locsymcount, 0,
9295 if (cookie.locsyms == NULL)
9302 count = stab->reloc_count;
9304 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9306 if (cookie.rels != NULL)
9308 cookie.rel = cookie.rels;
9309 cookie.relend = cookie.rels;
9310 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9311 if (_bfd_discard_section_stabs (abfd, stab,
9312 elf_section_data (stab)->sec_info,
9313 bfd_elf_reloc_symbol_deleted_p,
9316 if (elf_section_data (stab)->relocs != cookie.rels)
9324 count = eh->reloc_count;
9326 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9328 cookie.rel = cookie.rels;
9329 cookie.relend = cookie.rels;
9330 if (cookie.rels != NULL)
9331 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9333 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9334 bfd_elf_reloc_symbol_deleted_p,
9338 if (cookie.rels != NULL
9339 && elf_section_data (eh)->relocs != cookie.rels)
9343 if (bed->elf_backend_discard_info != NULL
9344 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9347 if (cookie.locsyms != NULL
9348 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9350 if (! info->keep_memory)
9351 free (cookie.locsyms);
9353 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9357 if (info->eh_frame_hdr
9358 && !info->relocatable
9359 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9366 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9369 const char *name, *p;
9370 struct bfd_section_already_linked *l;
9371 struct bfd_section_already_linked_hash_entry *already_linked_list;
9374 /* A single member comdat group section may be discarded by a
9375 linkonce section. See below. */
9376 if (sec->output_section == bfd_abs_section_ptr)
9381 /* Check if it belongs to a section group. */
9382 group = elf_sec_group (sec);
9384 /* Return if it isn't a linkonce section nor a member of a group. A
9385 comdat group section also has SEC_LINK_ONCE set. */
9386 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9391 /* If this is the member of a single member comdat group, check if
9392 the group should be discarded. */
9393 if (elf_next_in_group (sec) == sec
9394 && (group->flags & SEC_LINK_ONCE) != 0)
9400 /* FIXME: When doing a relocatable link, we may have trouble
9401 copying relocations in other sections that refer to local symbols
9402 in the section being discarded. Those relocations will have to
9403 be converted somehow; as of this writing I'm not sure that any of
9404 the backends handle that correctly.
9406 It is tempting to instead not discard link once sections when
9407 doing a relocatable link (technically, they should be discarded
9408 whenever we are building constructors). However, that fails,
9409 because the linker winds up combining all the link once sections
9410 into a single large link once section, which defeats the purpose
9411 of having link once sections in the first place.
9413 Also, not merging link once sections in a relocatable link
9414 causes trouble for MIPS ELF, which relies on link once semantics
9415 to handle the .reginfo section correctly. */
9417 name = bfd_get_section_name (abfd, sec);
9419 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9420 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
9425 already_linked_list = bfd_section_already_linked_table_lookup (p);
9427 for (l = already_linked_list->entry; l != NULL; l = l->next)
9429 /* We may have 3 different sections on the list: group section,
9430 comdat section and linkonce section. SEC may be a linkonce or
9431 group section. We match a group section with a group section,
9432 a linkonce section with a linkonce section, and ignore comdat
9434 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9435 && strcmp (name, l->sec->name) == 0
9436 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9438 /* The section has already been linked. See if we should
9440 switch (flags & SEC_LINK_DUPLICATES)
9445 case SEC_LINK_DUPLICATES_DISCARD:
9448 case SEC_LINK_DUPLICATES_ONE_ONLY:
9449 (*_bfd_error_handler)
9450 (_("%B: ignoring duplicate section `%A'\n"),
9454 case SEC_LINK_DUPLICATES_SAME_SIZE:
9455 if (sec->size != l->sec->size)
9456 (*_bfd_error_handler)
9457 (_("%B: duplicate section `%A' has different size\n"),
9461 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9462 if (sec->size != l->sec->size)
9463 (*_bfd_error_handler)
9464 (_("%B: duplicate section `%A' has different size\n"),
9466 else if (sec->size != 0)
9468 bfd_byte *sec_contents, *l_sec_contents;
9470 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9471 (*_bfd_error_handler)
9472 (_("%B: warning: could not read contents of section `%A'\n"),
9474 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9476 (*_bfd_error_handler)
9477 (_("%B: warning: could not read contents of section `%A'\n"),
9478 l->sec->owner, l->sec);
9479 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9480 (*_bfd_error_handler)
9481 (_("%B: warning: duplicate section `%A' has different contents\n"),
9485 free (sec_contents);
9487 free (l_sec_contents);
9492 /* Set the output_section field so that lang_add_section
9493 does not create a lang_input_section structure for this
9494 section. Since there might be a symbol in the section
9495 being discarded, we must retain a pointer to the section
9496 which we are really going to use. */
9497 sec->output_section = bfd_abs_section_ptr;
9498 sec->kept_section = l->sec;
9500 if (flags & SEC_GROUP)
9502 asection *first = elf_next_in_group (sec);
9503 asection *s = first;
9507 s->output_section = bfd_abs_section_ptr;
9508 /* Record which group discards it. */
9509 s->kept_section = l->sec;
9510 s = elf_next_in_group (s);
9511 /* These lists are circular. */
9523 /* If this is the member of a single member comdat group and the
9524 group hasn't be discarded, we check if it matches a linkonce
9525 section. We only record the discarded comdat group. Otherwise
9526 the undiscarded group will be discarded incorrectly later since
9527 itself has been recorded. */
9528 for (l = already_linked_list->entry; l != NULL; l = l->next)
9529 if ((l->sec->flags & SEC_GROUP) == 0
9530 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9531 && bfd_elf_match_symbols_in_sections (l->sec,
9532 elf_next_in_group (sec)))
9534 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
9535 elf_next_in_group (sec)->kept_section = l->sec;
9536 group->output_section = bfd_abs_section_ptr;
9543 /* There is no direct match. But for linkonce section, we should
9544 check if there is a match with comdat group member. We always
9545 record the linkonce section, discarded or not. */
9546 for (l = already_linked_list->entry; l != NULL; l = l->next)
9547 if (l->sec->flags & SEC_GROUP)
9549 asection *first = elf_next_in_group (l->sec);
9552 && elf_next_in_group (first) == first
9553 && bfd_elf_match_symbols_in_sections (first, sec))
9555 sec->output_section = bfd_abs_section_ptr;
9556 sec->kept_section = l->sec;
9561 /* This is the first section with this name. Record it. */
9562 bfd_section_already_linked_table_insert (already_linked_list, sec);