1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
31 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
35 struct elf_link_hash_entry *h;
36 struct bfd_link_hash_entry *bh;
37 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
40 /* This function may be called more than once. */
41 s = bfd_get_section_by_name (abfd, ".got");
42 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
45 switch (bed->s->arch_size)
56 bfd_set_error (bfd_error_bad_value);
60 flags = bed->dynamic_sec_flags;
62 s = bfd_make_section (abfd, ".got");
64 || !bfd_set_section_flags (abfd, s, flags)
65 || !bfd_set_section_alignment (abfd, s, ptralign))
68 if (bed->want_got_plt)
70 s = bfd_make_section (abfd, ".got.plt");
72 || !bfd_set_section_flags (abfd, s, flags)
73 || !bfd_set_section_alignment (abfd, s, ptralign))
77 if (bed->want_got_sym)
79 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
80 (or .got.plt) section. We don't do this in the linker script
81 because we don't want to define the symbol if we are not creating
82 a global offset table. */
84 if (!(_bfd_generic_link_add_one_symbol
85 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
86 bed->got_symbol_offset, NULL, FALSE, bed->collect, &bh)))
88 h = (struct elf_link_hash_entry *) bh;
92 if (! info->executable
93 && ! bfd_elf_link_record_dynamic_symbol (info, h))
96 elf_hash_table (info)->hgot = h;
99 /* The first bit of the global offset table is the header. */
100 s->size += bed->got_header_size + bed->got_symbol_offset;
105 /* Create some sections which will be filled in with dynamic linking
106 information. ABFD is an input file which requires dynamic sections
107 to be created. The dynamic sections take up virtual memory space
108 when the final executable is run, so we need to create them before
109 addresses are assigned to the output sections. We work out the
110 actual contents and size of these sections later. */
113 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
116 register asection *s;
117 struct elf_link_hash_entry *h;
118 struct bfd_link_hash_entry *bh;
119 const struct elf_backend_data *bed;
121 if (! is_elf_hash_table (info->hash))
124 if (elf_hash_table (info)->dynamic_sections_created)
127 /* Make sure that all dynamic sections use the same input BFD. */
128 if (elf_hash_table (info)->dynobj == NULL)
129 elf_hash_table (info)->dynobj = abfd;
131 abfd = elf_hash_table (info)->dynobj;
133 bed = get_elf_backend_data (abfd);
135 flags = bed->dynamic_sec_flags;
137 /* A dynamically linked executable has a .interp section, but a
138 shared library does not. */
139 if (info->executable)
141 s = bfd_make_section (abfd, ".interp");
143 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
147 if (! info->traditional_format)
149 s = bfd_make_section (abfd, ".eh_frame_hdr");
151 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
152 || ! bfd_set_section_alignment (abfd, s, 2))
154 elf_hash_table (info)->eh_info.hdr_sec = s;
157 /* Create sections to hold version informations. These are removed
158 if they are not needed. */
159 s = bfd_make_section (abfd, ".gnu.version_d");
161 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
162 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
165 s = bfd_make_section (abfd, ".gnu.version");
167 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
168 || ! bfd_set_section_alignment (abfd, s, 1))
171 s = bfd_make_section (abfd, ".gnu.version_r");
173 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
174 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
177 s = bfd_make_section (abfd, ".dynsym");
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, ".dynstr");
185 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
188 /* Create a strtab to hold the dynamic symbol names. */
189 if (elf_hash_table (info)->dynstr == NULL)
191 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
192 if (elf_hash_table (info)->dynstr == NULL)
196 s = bfd_make_section (abfd, ".dynamic");
198 || ! bfd_set_section_flags (abfd, s, flags)
199 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
202 /* The special symbol _DYNAMIC is always set to the start of the
203 .dynamic section. This call occurs before we have processed the
204 symbols for any dynamic object, so we don't have to worry about
205 overriding a dynamic definition. We could set _DYNAMIC in a
206 linker script, but we only want to define it if we are, in fact,
207 creating a .dynamic section. We don't want to define it if there
208 is no .dynamic section, since on some ELF platforms the start up
209 code examines it to decide how to initialize the process. */
211 if (! (_bfd_generic_link_add_one_symbol
212 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE,
213 get_elf_backend_data (abfd)->collect, &bh)))
215 h = (struct elf_link_hash_entry *) bh;
217 h->type = STT_OBJECT;
219 if (! info->executable
220 && ! bfd_elf_link_record_dynamic_symbol (info, h))
223 s = bfd_make_section (abfd, ".hash");
225 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
226 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
228 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
230 /* Let the backend create the rest of the sections. This lets the
231 backend set the right flags. The backend will normally create
232 the .got and .plt sections. */
233 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
236 elf_hash_table (info)->dynamic_sections_created = TRUE;
241 /* Create dynamic sections when linking against a dynamic object. */
244 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
246 flagword flags, pltflags;
248 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
250 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
251 .rel[a].bss sections. */
252 flags = bed->dynamic_sec_flags;
255 pltflags |= SEC_CODE;
256 if (bed->plt_not_loaded)
257 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
258 if (bed->plt_readonly)
259 pltflags |= SEC_READONLY;
261 s = bfd_make_section (abfd, ".plt");
263 || ! bfd_set_section_flags (abfd, s, pltflags)
264 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
267 if (bed->want_plt_sym)
269 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
271 struct elf_link_hash_entry *h;
272 struct bfd_link_hash_entry *bh = NULL;
274 if (! (_bfd_generic_link_add_one_symbol
275 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
276 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
278 h = (struct elf_link_hash_entry *) bh;
280 h->type = STT_OBJECT;
282 if (! info->executable
283 && ! bfd_elf_link_record_dynamic_symbol (info, h))
287 s = bfd_make_section (abfd,
288 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
290 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
291 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
294 if (! _bfd_elf_create_got_section (abfd, info))
297 if (bed->want_dynbss)
299 /* The .dynbss section is a place to put symbols which are defined
300 by dynamic objects, are referenced by regular objects, and are
301 not functions. We must allocate space for them in the process
302 image and use a R_*_COPY reloc to tell the dynamic linker to
303 initialize them at run time. The linker script puts the .dynbss
304 section into the .bss section of the final image. */
305 s = bfd_make_section (abfd, ".dynbss");
307 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
310 /* The .rel[a].bss section holds copy relocs. This section is not
311 normally needed. We need to create it here, though, so that the
312 linker will map it to an output section. We can't just create it
313 only if we need it, because we will not know whether we need it
314 until we have seen all the input files, and the first time the
315 main linker code calls BFD after examining all the input files
316 (size_dynamic_sections) the input sections have already been
317 mapped to the output sections. If the section turns out not to
318 be needed, we can discard it later. We will never need this
319 section when generating a shared object, since they do not use
323 s = bfd_make_section (abfd,
324 (bed->default_use_rela_p
325 ? ".rela.bss" : ".rel.bss"));
327 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
328 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
336 /* Record a new dynamic symbol. We record the dynamic symbols as we
337 read the input files, since we need to have a list of all of them
338 before we can determine the final sizes of the output sections.
339 Note that we may actually call this function even though we are not
340 going to output any dynamic symbols; in some cases we know that a
341 symbol should be in the dynamic symbol table, but only if there is
345 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
346 struct elf_link_hash_entry *h)
348 if (h->dynindx == -1)
350 struct elf_strtab_hash *dynstr;
355 /* XXX: The ABI draft says the linker must turn hidden and
356 internal symbols into STB_LOCAL symbols when producing the
357 DSO. However, if ld.so honors st_other in the dynamic table,
358 this would not be necessary. */
359 switch (ELF_ST_VISIBILITY (h->other))
363 if (h->root.type != bfd_link_hash_undefined
364 && h->root.type != bfd_link_hash_undefweak)
374 h->dynindx = elf_hash_table (info)->dynsymcount;
375 ++elf_hash_table (info)->dynsymcount;
377 dynstr = elf_hash_table (info)->dynstr;
380 /* Create a strtab to hold the dynamic symbol names. */
381 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
386 /* We don't put any version information in the dynamic string
388 name = h->root.root.string;
389 p = strchr (name, ELF_VER_CHR);
391 /* We know that the p points into writable memory. In fact,
392 there are only a few symbols that have read-only names, being
393 those like _GLOBAL_OFFSET_TABLE_ that are created specially
394 by the backends. Most symbols will have names pointing into
395 an ELF string table read from a file, or to objalloc memory. */
398 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
403 if (indx == (bfd_size_type) -1)
405 h->dynstr_index = indx;
411 /* Record an assignment to a symbol made by a linker script. We need
412 this in case some dynamic object refers to this symbol. */
415 bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED,
416 struct bfd_link_info *info,
420 struct elf_link_hash_entry *h;
422 if (!is_elf_hash_table (info->hash))
425 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE);
429 /* Since we're defining the symbol, don't let it seem to have not
430 been defined. record_dynamic_symbol and size_dynamic_sections
432 ??? Changing bfd_link_hash_undefined to bfd_link_hash_new (or
433 to bfd_link_hash_undefweak, see linker.c:link_action) runs the risk
434 of some later symbol manipulation setting the symbol back to
435 bfd_link_hash_undefined, and the linker trying to add the symbol to
436 the undefs list twice. */
437 if (h->root.type == bfd_link_hash_undefweak
438 || h->root.type == bfd_link_hash_undefined)
439 h->root.type = bfd_link_hash_new;
441 if (h->root.type == bfd_link_hash_new)
444 /* If this symbol is being provided by the linker script, and it is
445 currently defined by a dynamic object, but not by a regular
446 object, then mark it as undefined so that the generic linker will
447 force the correct value. */
451 h->root.type = bfd_link_hash_undefined;
453 /* If this symbol is not being provided by the linker script, and it is
454 currently defined by a dynamic object, but not by a regular object,
455 then clear out any version information because the symbol will not be
456 associated with the dynamic object any more. */
460 h->verinfo.verdef = NULL;
469 if (! bfd_elf_link_record_dynamic_symbol (info, h))
472 /* If this is a weak defined symbol, and we know a corresponding
473 real symbol from the same dynamic object, make sure the real
474 symbol is also made into a dynamic symbol. */
475 if (h->u.weakdef != NULL
476 && h->u.weakdef->dynindx == -1)
478 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
486 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
487 success, and 2 on a failure caused by attempting to record a symbol
488 in a discarded section, eg. a discarded link-once section symbol. */
491 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
496 struct elf_link_local_dynamic_entry *entry;
497 struct elf_link_hash_table *eht;
498 struct elf_strtab_hash *dynstr;
499 unsigned long dynstr_index;
501 Elf_External_Sym_Shndx eshndx;
502 char esym[sizeof (Elf64_External_Sym)];
504 if (! is_elf_hash_table (info->hash))
507 /* See if the entry exists already. */
508 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
509 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
512 amt = sizeof (*entry);
513 entry = bfd_alloc (input_bfd, amt);
517 /* Go find the symbol, so that we can find it's name. */
518 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
519 1, input_indx, &entry->isym, esym, &eshndx))
521 bfd_release (input_bfd, entry);
525 if (entry->isym.st_shndx != SHN_UNDEF
526 && (entry->isym.st_shndx < SHN_LORESERVE
527 || entry->isym.st_shndx > SHN_HIRESERVE))
531 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
532 if (s == NULL || bfd_is_abs_section (s->output_section))
534 /* We can still bfd_release here as nothing has done another
535 bfd_alloc. We can't do this later in this function. */
536 bfd_release (input_bfd, entry);
541 name = (bfd_elf_string_from_elf_section
542 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
543 entry->isym.st_name));
545 dynstr = elf_hash_table (info)->dynstr;
548 /* Create a strtab to hold the dynamic symbol names. */
549 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
554 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
555 if (dynstr_index == (unsigned long) -1)
557 entry->isym.st_name = dynstr_index;
559 eht = elf_hash_table (info);
561 entry->next = eht->dynlocal;
562 eht->dynlocal = entry;
563 entry->input_bfd = input_bfd;
564 entry->input_indx = input_indx;
567 /* Whatever binding the symbol had before, it's now local. */
569 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
571 /* The dynindx will be set at the end of size_dynamic_sections. */
576 /* Return the dynindex of a local dynamic symbol. */
579 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
583 struct elf_link_local_dynamic_entry *e;
585 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
586 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
591 /* This function is used to renumber the dynamic symbols, if some of
592 them are removed because they are marked as local. This is called
593 via elf_link_hash_traverse. */
596 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
599 size_t *count = data;
601 if (h->root.type == bfd_link_hash_warning)
602 h = (struct elf_link_hash_entry *) h->root.u.i.link;
604 if (h->dynindx != -1)
605 h->dynindx = ++(*count);
610 /* Return true if the dynamic symbol for a given section should be
611 omitted when creating a shared library. */
613 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
614 struct bfd_link_info *info,
617 switch (elf_section_data (p)->this_hdr.sh_type)
621 /* If sh_type is yet undecided, assume it could be
622 SHT_PROGBITS/SHT_NOBITS. */
624 if (strcmp (p->name, ".got") == 0
625 || strcmp (p->name, ".got.plt") == 0
626 || strcmp (p->name, ".plt") == 0)
629 bfd *dynobj = elf_hash_table (info)->dynobj;
632 && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL
633 && (ip->flags & SEC_LINKER_CREATED)
634 && ip->output_section == p)
639 /* There shouldn't be section relative relocations
640 against any other section. */
646 /* Assign dynsym indices. In a shared library we generate a section
647 symbol for each output section, which come first. Next come all of
648 the back-end allocated local dynamic syms, followed by the rest of
649 the global symbols. */
652 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
654 unsigned long dynsymcount = 0;
658 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
660 for (p = output_bfd->sections; p ; p = p->next)
661 if ((p->flags & SEC_EXCLUDE) == 0
662 && (p->flags & SEC_ALLOC) != 0
663 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
664 elf_section_data (p)->dynindx = ++dynsymcount;
667 if (elf_hash_table (info)->dynlocal)
669 struct elf_link_local_dynamic_entry *p;
670 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
671 p->dynindx = ++dynsymcount;
674 elf_link_hash_traverse (elf_hash_table (info),
675 elf_link_renumber_hash_table_dynsyms,
678 /* There is an unused NULL entry at the head of the table which
679 we must account for in our count. Unless there weren't any
680 symbols, which means we'll have no table at all. */
681 if (dynsymcount != 0)
684 return elf_hash_table (info)->dynsymcount = dynsymcount;
687 /* This function is called when we want to define a new symbol. It
688 handles the various cases which arise when we find a definition in
689 a dynamic object, or when there is already a definition in a
690 dynamic object. The new symbol is described by NAME, SYM, PSEC,
691 and PVALUE. We set SYM_HASH to the hash table entry. We set
692 OVERRIDE if the old symbol is overriding a new definition. We set
693 TYPE_CHANGE_OK if it is OK for the type to change. We set
694 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
695 change, we mean that we shouldn't warn if the type or size does
699 _bfd_elf_merge_symbol (bfd *abfd,
700 struct bfd_link_info *info,
702 Elf_Internal_Sym *sym,
705 struct elf_link_hash_entry **sym_hash,
707 bfd_boolean *override,
708 bfd_boolean *type_change_ok,
709 bfd_boolean *size_change_ok)
711 asection *sec, *oldsec;
712 struct elf_link_hash_entry *h;
713 struct elf_link_hash_entry *flip;
716 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
717 bfd_boolean newweak, oldweak;
723 bind = ELF_ST_BIND (sym->st_info);
725 if (! bfd_is_und_section (sec))
726 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
728 h = ((struct elf_link_hash_entry *)
729 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
734 /* This code is for coping with dynamic objects, and is only useful
735 if we are doing an ELF link. */
736 if (info->hash->creator != abfd->xvec)
739 /* For merging, we only care about real symbols. */
741 while (h->root.type == bfd_link_hash_indirect
742 || h->root.type == bfd_link_hash_warning)
743 h = (struct elf_link_hash_entry *) h->root.u.i.link;
745 /* If we just created the symbol, mark it as being an ELF symbol.
746 Other than that, there is nothing to do--there is no merge issue
747 with a newly defined symbol--so we just return. */
749 if (h->root.type == bfd_link_hash_new)
755 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
758 switch (h->root.type)
765 case bfd_link_hash_undefined:
766 case bfd_link_hash_undefweak:
767 oldbfd = h->root.u.undef.abfd;
771 case bfd_link_hash_defined:
772 case bfd_link_hash_defweak:
773 oldbfd = h->root.u.def.section->owner;
774 oldsec = h->root.u.def.section;
777 case bfd_link_hash_common:
778 oldbfd = h->root.u.c.p->section->owner;
779 oldsec = h->root.u.c.p->section;
783 /* In cases involving weak versioned symbols, we may wind up trying
784 to merge a symbol with itself. Catch that here, to avoid the
785 confusion that results if we try to override a symbol with
786 itself. The additional tests catch cases like
787 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
788 dynamic object, which we do want to handle here. */
790 && ((abfd->flags & DYNAMIC) == 0
794 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
795 respectively, is from a dynamic object. */
797 if ((abfd->flags & DYNAMIC) != 0)
803 olddyn = (oldbfd->flags & DYNAMIC) != 0;
808 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
809 indices used by MIPS ELF. */
810 switch (h->root.type)
816 case bfd_link_hash_defined:
817 case bfd_link_hash_defweak:
818 hsec = h->root.u.def.section;
821 case bfd_link_hash_common:
822 hsec = h->root.u.c.p->section;
829 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
832 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
833 respectively, appear to be a definition rather than reference. */
835 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
840 if (h->root.type == bfd_link_hash_undefined
841 || h->root.type == bfd_link_hash_undefweak
842 || h->root.type == bfd_link_hash_common)
847 /* Check TLS symbol. */
848 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
849 && ELF_ST_TYPE (sym->st_info) != h->type)
852 bfd_boolean ntdef, tdef;
853 asection *ntsec, *tsec;
855 if (h->type == STT_TLS)
875 (*_bfd_error_handler)
876 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
877 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
878 else if (!tdef && !ntdef)
879 (*_bfd_error_handler)
880 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
881 tbfd, ntbfd, h->root.root.string);
883 (*_bfd_error_handler)
884 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
885 tbfd, tsec, ntbfd, h->root.root.string);
887 (*_bfd_error_handler)
888 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
889 tbfd, ntbfd, ntsec, h->root.root.string);
891 bfd_set_error (bfd_error_bad_value);
895 /* We need to remember if a symbol has a definition in a dynamic
896 object or is weak in all dynamic objects. Internal and hidden
897 visibility will make it unavailable to dynamic objects. */
898 if (newdyn && !h->dynamic_def)
900 if (!bfd_is_und_section (sec))
904 /* Check if this symbol is weak in all dynamic objects. If it
905 is the first time we see it in a dynamic object, we mark
906 if it is weak. Otherwise, we clear it. */
909 if (bind == STB_WEAK)
912 else if (bind != STB_WEAK)
917 /* If the old symbol has non-default visibility, we ignore the new
918 definition from a dynamic object. */
920 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
921 && !bfd_is_und_section (sec))
924 /* Make sure this symbol is dynamic. */
926 /* A protected symbol has external availability. Make sure it is
929 FIXME: Should we check type and size for protected symbol? */
930 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
931 return bfd_elf_link_record_dynamic_symbol (info, h);
936 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
939 /* If the new symbol with non-default visibility comes from a
940 relocatable file and the old definition comes from a dynamic
941 object, we remove the old definition. */
942 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
945 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
946 && bfd_is_und_section (sec))
948 /* If the new symbol is undefined and the old symbol was
949 also undefined before, we need to make sure
950 _bfd_generic_link_add_one_symbol doesn't mess
951 up the linker hash table undefs list. Since the old
952 definition came from a dynamic object, it is still on the
954 h->root.type = bfd_link_hash_undefined;
955 h->root.u.undef.abfd = abfd;
959 h->root.type = bfd_link_hash_new;
960 h->root.u.undef.abfd = NULL;
969 /* FIXME: Should we check type and size for protected symbol? */
975 /* Differentiate strong and weak symbols. */
976 newweak = bind == STB_WEAK;
977 oldweak = (h->root.type == bfd_link_hash_defweak
978 || h->root.type == bfd_link_hash_undefweak);
980 /* If a new weak symbol definition comes from a regular file and the
981 old symbol comes from a dynamic library, we treat the new one as
982 strong. Similarly, an old weak symbol definition from a regular
983 file is treated as strong when the new symbol comes from a dynamic
984 library. Further, an old weak symbol from a dynamic library is
985 treated as strong if the new symbol is from a dynamic library.
986 This reflects the way glibc's ld.so works.
988 Do this before setting *type_change_ok or *size_change_ok so that
989 we warn properly when dynamic library symbols are overridden. */
991 if (newdef && !newdyn && olddyn)
993 if (olddef && newdyn)
996 /* It's OK to change the type if either the existing symbol or the
997 new symbol is weak. A type change is also OK if the old symbol
998 is undefined and the new symbol is defined. */
1003 && h->root.type == bfd_link_hash_undefined))
1004 *type_change_ok = TRUE;
1006 /* It's OK to change the size if either the existing symbol or the
1007 new symbol is weak, or if the old symbol is undefined. */
1010 || h->root.type == bfd_link_hash_undefined)
1011 *size_change_ok = TRUE;
1013 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1014 symbol, respectively, appears to be a common symbol in a dynamic
1015 object. If a symbol appears in an uninitialized section, and is
1016 not weak, and is not a function, then it may be a common symbol
1017 which was resolved when the dynamic object was created. We want
1018 to treat such symbols specially, because they raise special
1019 considerations when setting the symbol size: if the symbol
1020 appears as a common symbol in a regular object, and the size in
1021 the regular object is larger, we must make sure that we use the
1022 larger size. This problematic case can always be avoided in C,
1023 but it must be handled correctly when using Fortran shared
1026 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1027 likewise for OLDDYNCOMMON and OLDDEF.
1029 Note that this test is just a heuristic, and that it is quite
1030 possible to have an uninitialized symbol in a shared object which
1031 is really a definition, rather than a common symbol. This could
1032 lead to some minor confusion when the symbol really is a common
1033 symbol in some regular object. However, I think it will be
1039 && (sec->flags & SEC_ALLOC) != 0
1040 && (sec->flags & SEC_LOAD) == 0
1042 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1043 newdyncommon = TRUE;
1045 newdyncommon = FALSE;
1049 && h->root.type == bfd_link_hash_defined
1051 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1052 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1054 && h->type != STT_FUNC)
1055 olddyncommon = TRUE;
1057 olddyncommon = FALSE;
1059 /* If both the old and the new symbols look like common symbols in a
1060 dynamic object, set the size of the symbol to the larger of the
1065 && sym->st_size != h->size)
1067 /* Since we think we have two common symbols, issue a multiple
1068 common warning if desired. Note that we only warn if the
1069 size is different. If the size is the same, we simply let
1070 the old symbol override the new one as normally happens with
1071 symbols defined in dynamic objects. */
1073 if (! ((*info->callbacks->multiple_common)
1074 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1075 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1078 if (sym->st_size > h->size)
1079 h->size = sym->st_size;
1081 *size_change_ok = TRUE;
1084 /* If we are looking at a dynamic object, and we have found a
1085 definition, we need to see if the symbol was already defined by
1086 some other object. If so, we want to use the existing
1087 definition, and we do not want to report a multiple symbol
1088 definition error; we do this by clobbering *PSEC to be
1089 bfd_und_section_ptr.
1091 We treat a common symbol as a definition if the symbol in the
1092 shared library is a function, since common symbols always
1093 represent variables; this can cause confusion in principle, but
1094 any such confusion would seem to indicate an erroneous program or
1095 shared library. We also permit a common symbol in a regular
1096 object to override a weak symbol in a shared object. */
1101 || (h->root.type == bfd_link_hash_common
1103 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1107 newdyncommon = FALSE;
1109 *psec = sec = bfd_und_section_ptr;
1110 *size_change_ok = TRUE;
1112 /* If we get here when the old symbol is a common symbol, then
1113 we are explicitly letting it override a weak symbol or
1114 function in a dynamic object, and we don't want to warn about
1115 a type change. If the old symbol is a defined symbol, a type
1116 change warning may still be appropriate. */
1118 if (h->root.type == bfd_link_hash_common)
1119 *type_change_ok = TRUE;
1122 /* Handle the special case of an old common symbol merging with a
1123 new symbol which looks like a common symbol in a shared object.
1124 We change *PSEC and *PVALUE to make the new symbol look like a
1125 common symbol, and let _bfd_generic_link_add_one_symbol will do
1129 && h->root.type == bfd_link_hash_common)
1133 newdyncommon = FALSE;
1134 *pvalue = sym->st_size;
1135 *psec = sec = bfd_com_section_ptr;
1136 *size_change_ok = TRUE;
1139 /* If the old symbol is from a dynamic object, and the new symbol is
1140 a definition which is not from a dynamic object, then the new
1141 symbol overrides the old symbol. Symbols from regular files
1142 always take precedence over symbols from dynamic objects, even if
1143 they are defined after the dynamic object in the link.
1145 As above, we again permit a common symbol in a regular object to
1146 override a definition in a shared object if the shared object
1147 symbol is a function or is weak. */
1152 || (bfd_is_com_section (sec)
1154 || h->type == STT_FUNC)))
1159 /* Change the hash table entry to undefined, and let
1160 _bfd_generic_link_add_one_symbol do the right thing with the
1163 h->root.type = bfd_link_hash_undefined;
1164 h->root.u.undef.abfd = h->root.u.def.section->owner;
1165 *size_change_ok = TRUE;
1168 olddyncommon = FALSE;
1170 /* We again permit a type change when a common symbol may be
1171 overriding a function. */
1173 if (bfd_is_com_section (sec))
1174 *type_change_ok = TRUE;
1176 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1179 /* This union may have been set to be non-NULL when this symbol
1180 was seen in a dynamic object. We must force the union to be
1181 NULL, so that it is correct for a regular symbol. */
1182 h->verinfo.vertree = NULL;
1185 /* Handle the special case of a new common symbol merging with an
1186 old symbol that looks like it might be a common symbol defined in
1187 a shared object. Note that we have already handled the case in
1188 which a new common symbol should simply override the definition
1189 in the shared library. */
1192 && bfd_is_com_section (sec)
1195 /* It would be best if we could set the hash table entry to a
1196 common symbol, but we don't know what to use for the section
1197 or the alignment. */
1198 if (! ((*info->callbacks->multiple_common)
1199 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1200 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1203 /* If the presumed common symbol in the dynamic object is
1204 larger, pretend that the new symbol has its size. */
1206 if (h->size > *pvalue)
1209 /* FIXME: We no longer know the alignment required by the symbol
1210 in the dynamic object, so we just wind up using the one from
1211 the regular object. */
1214 olddyncommon = FALSE;
1216 h->root.type = bfd_link_hash_undefined;
1217 h->root.u.undef.abfd = h->root.u.def.section->owner;
1219 *size_change_ok = TRUE;
1220 *type_change_ok = TRUE;
1222 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1225 h->verinfo.vertree = NULL;
1230 /* Handle the case where we had a versioned symbol in a dynamic
1231 library and now find a definition in a normal object. In this
1232 case, we make the versioned symbol point to the normal one. */
1233 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1234 flip->root.type = h->root.type;
1235 h->root.type = bfd_link_hash_indirect;
1236 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1237 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1238 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1242 flip->ref_dynamic = 1;
1249 /* This function is called to create an indirect symbol from the
1250 default for the symbol with the default version if needed. The
1251 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1252 set DYNSYM if the new indirect symbol is dynamic. */
1255 _bfd_elf_add_default_symbol (bfd *abfd,
1256 struct bfd_link_info *info,
1257 struct elf_link_hash_entry *h,
1259 Elf_Internal_Sym *sym,
1262 bfd_boolean *dynsym,
1263 bfd_boolean override)
1265 bfd_boolean type_change_ok;
1266 bfd_boolean size_change_ok;
1269 struct elf_link_hash_entry *hi;
1270 struct bfd_link_hash_entry *bh;
1271 const struct elf_backend_data *bed;
1272 bfd_boolean collect;
1273 bfd_boolean dynamic;
1275 size_t len, shortlen;
1278 /* If this symbol has a version, and it is the default version, we
1279 create an indirect symbol from the default name to the fully
1280 decorated name. This will cause external references which do not
1281 specify a version to be bound to this version of the symbol. */
1282 p = strchr (name, ELF_VER_CHR);
1283 if (p == NULL || p[1] != ELF_VER_CHR)
1288 /* We are overridden by an old definition. We need to check if we
1289 need to create the indirect symbol from the default name. */
1290 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1292 BFD_ASSERT (hi != NULL);
1295 while (hi->root.type == bfd_link_hash_indirect
1296 || hi->root.type == bfd_link_hash_warning)
1298 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1304 bed = get_elf_backend_data (abfd);
1305 collect = bed->collect;
1306 dynamic = (abfd->flags & DYNAMIC) != 0;
1308 shortlen = p - name;
1309 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1310 if (shortname == NULL)
1312 memcpy (shortname, name, shortlen);
1313 shortname[shortlen] = '\0';
1315 /* We are going to create a new symbol. Merge it with any existing
1316 symbol with this name. For the purposes of the merge, act as
1317 though we were defining the symbol we just defined, although we
1318 actually going to define an indirect symbol. */
1319 type_change_ok = FALSE;
1320 size_change_ok = FALSE;
1322 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1323 &hi, &skip, &override, &type_change_ok,
1333 if (! (_bfd_generic_link_add_one_symbol
1334 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1335 0, name, FALSE, collect, &bh)))
1337 hi = (struct elf_link_hash_entry *) bh;
1341 /* In this case the symbol named SHORTNAME is overriding the
1342 indirect symbol we want to add. We were planning on making
1343 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1344 is the name without a version. NAME is the fully versioned
1345 name, and it is the default version.
1347 Overriding means that we already saw a definition for the
1348 symbol SHORTNAME in a regular object, and it is overriding
1349 the symbol defined in the dynamic object.
1351 When this happens, we actually want to change NAME, the
1352 symbol we just added, to refer to SHORTNAME. This will cause
1353 references to NAME in the shared object to become references
1354 to SHORTNAME in the regular object. This is what we expect
1355 when we override a function in a shared object: that the
1356 references in the shared object will be mapped to the
1357 definition in the regular object. */
1359 while (hi->root.type == bfd_link_hash_indirect
1360 || hi->root.type == bfd_link_hash_warning)
1361 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1363 h->root.type = bfd_link_hash_indirect;
1364 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1368 hi->ref_dynamic = 1;
1372 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1377 /* Now set HI to H, so that the following code will set the
1378 other fields correctly. */
1382 /* If there is a duplicate definition somewhere, then HI may not
1383 point to an indirect symbol. We will have reported an error to
1384 the user in that case. */
1386 if (hi->root.type == bfd_link_hash_indirect)
1388 struct elf_link_hash_entry *ht;
1390 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1391 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1393 /* See if the new flags lead us to realize that the symbol must
1405 if (hi->ref_regular)
1411 /* We also need to define an indirection from the nondefault version
1415 len = strlen (name);
1416 shortname = bfd_hash_allocate (&info->hash->table, len);
1417 if (shortname == NULL)
1419 memcpy (shortname, name, shortlen);
1420 memcpy (shortname + shortlen, p + 1, len - shortlen);
1422 /* Once again, merge with any existing symbol. */
1423 type_change_ok = FALSE;
1424 size_change_ok = FALSE;
1426 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1427 &hi, &skip, &override, &type_change_ok,
1436 /* Here SHORTNAME is a versioned name, so we don't expect to see
1437 the type of override we do in the case above unless it is
1438 overridden by a versioned definition. */
1439 if (hi->root.type != bfd_link_hash_defined
1440 && hi->root.type != bfd_link_hash_defweak)
1441 (*_bfd_error_handler)
1442 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1448 if (! (_bfd_generic_link_add_one_symbol
1449 (info, abfd, shortname, BSF_INDIRECT,
1450 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1452 hi = (struct elf_link_hash_entry *) bh;
1454 /* If there is a duplicate definition somewhere, then HI may not
1455 point to an indirect symbol. We will have reported an error
1456 to the user in that case. */
1458 if (hi->root.type == bfd_link_hash_indirect)
1460 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1462 /* See if the new flags lead us to realize that the symbol
1474 if (hi->ref_regular)
1484 /* This routine is used to export all defined symbols into the dynamic
1485 symbol table. It is called via elf_link_hash_traverse. */
1488 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1490 struct elf_info_failed *eif = data;
1492 /* Ignore indirect symbols. These are added by the versioning code. */
1493 if (h->root.type == bfd_link_hash_indirect)
1496 if (h->root.type == bfd_link_hash_warning)
1497 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1499 if (h->dynindx == -1
1503 struct bfd_elf_version_tree *t;
1504 struct bfd_elf_version_expr *d;
1506 for (t = eif->verdefs; t != NULL; t = t->next)
1508 if (t->globals.list != NULL)
1510 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1515 if (t->locals.list != NULL)
1517 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1526 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1537 /* Look through the symbols which are defined in other shared
1538 libraries and referenced here. Update the list of version
1539 dependencies. This will be put into the .gnu.version_r section.
1540 This function is called via elf_link_hash_traverse. */
1543 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1546 struct elf_find_verdep_info *rinfo = data;
1547 Elf_Internal_Verneed *t;
1548 Elf_Internal_Vernaux *a;
1551 if (h->root.type == bfd_link_hash_warning)
1552 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1554 /* We only care about symbols defined in shared objects with version
1559 || h->verinfo.verdef == NULL)
1562 /* See if we already know about this version. */
1563 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1565 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1568 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1569 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1575 /* This is a new version. Add it to tree we are building. */
1580 t = bfd_zalloc (rinfo->output_bfd, amt);
1583 rinfo->failed = TRUE;
1587 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1588 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1589 elf_tdata (rinfo->output_bfd)->verref = t;
1593 a = bfd_zalloc (rinfo->output_bfd, amt);
1595 /* Note that we are copying a string pointer here, and testing it
1596 above. If bfd_elf_string_from_elf_section is ever changed to
1597 discard the string data when low in memory, this will have to be
1599 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1601 a->vna_flags = h->verinfo.verdef->vd_flags;
1602 a->vna_nextptr = t->vn_auxptr;
1604 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1607 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1614 /* Figure out appropriate versions for all the symbols. We may not
1615 have the version number script until we have read all of the input
1616 files, so until that point we don't know which symbols should be
1617 local. This function is called via elf_link_hash_traverse. */
1620 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1622 struct elf_assign_sym_version_info *sinfo;
1623 struct bfd_link_info *info;
1624 const struct elf_backend_data *bed;
1625 struct elf_info_failed eif;
1632 if (h->root.type == bfd_link_hash_warning)
1633 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1635 /* Fix the symbol flags. */
1638 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1641 sinfo->failed = TRUE;
1645 /* We only need version numbers for symbols defined in regular
1647 if (!h->def_regular)
1650 bed = get_elf_backend_data (sinfo->output_bfd);
1651 p = strchr (h->root.root.string, ELF_VER_CHR);
1652 if (p != NULL && h->verinfo.vertree == NULL)
1654 struct bfd_elf_version_tree *t;
1659 /* There are two consecutive ELF_VER_CHR characters if this is
1660 not a hidden symbol. */
1662 if (*p == ELF_VER_CHR)
1668 /* If there is no version string, we can just return out. */
1676 /* Look for the version. If we find it, it is no longer weak. */
1677 for (t = sinfo->verdefs; t != NULL; t = t->next)
1679 if (strcmp (t->name, p) == 0)
1683 struct bfd_elf_version_expr *d;
1685 len = p - h->root.root.string;
1686 alc = bfd_malloc (len);
1689 memcpy (alc, h->root.root.string, len - 1);
1690 alc[len - 1] = '\0';
1691 if (alc[len - 2] == ELF_VER_CHR)
1692 alc[len - 2] = '\0';
1694 h->verinfo.vertree = t;
1698 if (t->globals.list != NULL)
1699 d = (*t->match) (&t->globals, NULL, alc);
1701 /* See if there is anything to force this symbol to
1703 if (d == NULL && t->locals.list != NULL)
1705 d = (*t->match) (&t->locals, NULL, alc);
1709 && ! info->export_dynamic)
1710 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1718 /* If we are building an application, we need to create a
1719 version node for this version. */
1720 if (t == NULL && info->executable)
1722 struct bfd_elf_version_tree **pp;
1725 /* If we aren't going to export this symbol, we don't need
1726 to worry about it. */
1727 if (h->dynindx == -1)
1731 t = bfd_zalloc (sinfo->output_bfd, amt);
1734 sinfo->failed = TRUE;
1739 t->name_indx = (unsigned int) -1;
1743 /* Don't count anonymous version tag. */
1744 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1746 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1748 t->vernum = version_index;
1752 h->verinfo.vertree = t;
1756 /* We could not find the version for a symbol when
1757 generating a shared archive. Return an error. */
1758 (*_bfd_error_handler)
1759 (_("%B: undefined versioned symbol name %s"),
1760 sinfo->output_bfd, h->root.root.string);
1761 bfd_set_error (bfd_error_bad_value);
1762 sinfo->failed = TRUE;
1770 /* If we don't have a version for this symbol, see if we can find
1772 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1774 struct bfd_elf_version_tree *t;
1775 struct bfd_elf_version_tree *local_ver;
1776 struct bfd_elf_version_expr *d;
1778 /* See if can find what version this symbol is in. If the
1779 symbol is supposed to be local, then don't actually register
1782 for (t = sinfo->verdefs; t != NULL; t = t->next)
1784 if (t->globals.list != NULL)
1786 bfd_boolean matched;
1790 while ((d = (*t->match) (&t->globals, d,
1791 h->root.root.string)) != NULL)
1796 /* There is a version without definition. Make
1797 the symbol the default definition for this
1799 h->verinfo.vertree = t;
1807 /* There is no undefined version for this symbol. Hide the
1809 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1812 if (t->locals.list != NULL)
1815 while ((d = (*t->match) (&t->locals, d,
1816 h->root.root.string)) != NULL)
1819 /* If the match is "*", keep looking for a more
1820 explicit, perhaps even global, match.
1821 XXX: Shouldn't this be !d->wildcard instead? */
1822 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1831 if (local_ver != NULL)
1833 h->verinfo.vertree = local_ver;
1834 if (h->dynindx != -1
1836 && ! info->export_dynamic)
1838 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1846 /* Read and swap the relocs from the section indicated by SHDR. This
1847 may be either a REL or a RELA section. The relocations are
1848 translated into RELA relocations and stored in INTERNAL_RELOCS,
1849 which should have already been allocated to contain enough space.
1850 The EXTERNAL_RELOCS are a buffer where the external form of the
1851 relocations should be stored.
1853 Returns FALSE if something goes wrong. */
1856 elf_link_read_relocs_from_section (bfd *abfd,
1858 Elf_Internal_Shdr *shdr,
1859 void *external_relocs,
1860 Elf_Internal_Rela *internal_relocs)
1862 const struct elf_backend_data *bed;
1863 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1864 const bfd_byte *erela;
1865 const bfd_byte *erelaend;
1866 Elf_Internal_Rela *irela;
1867 Elf_Internal_Shdr *symtab_hdr;
1870 /* Position ourselves at the start of the section. */
1871 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1874 /* Read the relocations. */
1875 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1878 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1879 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1881 bed = get_elf_backend_data (abfd);
1883 /* Convert the external relocations to the internal format. */
1884 if (shdr->sh_entsize == bed->s->sizeof_rel)
1885 swap_in = bed->s->swap_reloc_in;
1886 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1887 swap_in = bed->s->swap_reloca_in;
1890 bfd_set_error (bfd_error_wrong_format);
1894 erela = external_relocs;
1895 erelaend = erela + shdr->sh_size;
1896 irela = internal_relocs;
1897 while (erela < erelaend)
1901 (*swap_in) (abfd, erela, irela);
1902 r_symndx = ELF32_R_SYM (irela->r_info);
1903 if (bed->s->arch_size == 64)
1905 if ((size_t) r_symndx >= nsyms)
1907 (*_bfd_error_handler)
1908 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1909 " for offset 0x%lx in section `%A'"),
1911 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1912 bfd_set_error (bfd_error_bad_value);
1915 irela += bed->s->int_rels_per_ext_rel;
1916 erela += shdr->sh_entsize;
1922 /* Read and swap the relocs for a section O. They may have been
1923 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1924 not NULL, they are used as buffers to read into. They are known to
1925 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1926 the return value is allocated using either malloc or bfd_alloc,
1927 according to the KEEP_MEMORY argument. If O has two relocation
1928 sections (both REL and RELA relocations), then the REL_HDR
1929 relocations will appear first in INTERNAL_RELOCS, followed by the
1930 REL_HDR2 relocations. */
1933 _bfd_elf_link_read_relocs (bfd *abfd,
1935 void *external_relocs,
1936 Elf_Internal_Rela *internal_relocs,
1937 bfd_boolean keep_memory)
1939 Elf_Internal_Shdr *rel_hdr;
1940 void *alloc1 = NULL;
1941 Elf_Internal_Rela *alloc2 = NULL;
1942 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1944 if (elf_section_data (o)->relocs != NULL)
1945 return elf_section_data (o)->relocs;
1947 if (o->reloc_count == 0)
1950 rel_hdr = &elf_section_data (o)->rel_hdr;
1952 if (internal_relocs == NULL)
1956 size = o->reloc_count;
1957 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1959 internal_relocs = bfd_alloc (abfd, size);
1961 internal_relocs = alloc2 = bfd_malloc (size);
1962 if (internal_relocs == NULL)
1966 if (external_relocs == NULL)
1968 bfd_size_type size = rel_hdr->sh_size;
1970 if (elf_section_data (o)->rel_hdr2)
1971 size += elf_section_data (o)->rel_hdr2->sh_size;
1972 alloc1 = bfd_malloc (size);
1975 external_relocs = alloc1;
1978 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
1982 if (elf_section_data (o)->rel_hdr2
1983 && (!elf_link_read_relocs_from_section
1985 elf_section_data (o)->rel_hdr2,
1986 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
1987 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
1988 * bed->s->int_rels_per_ext_rel))))
1991 /* Cache the results for next time, if we can. */
1993 elf_section_data (o)->relocs = internal_relocs;
1998 /* Don't free alloc2, since if it was allocated we are passing it
1999 back (under the name of internal_relocs). */
2001 return internal_relocs;
2011 /* Compute the size of, and allocate space for, REL_HDR which is the
2012 section header for a section containing relocations for O. */
2015 _bfd_elf_link_size_reloc_section (bfd *abfd,
2016 Elf_Internal_Shdr *rel_hdr,
2019 bfd_size_type reloc_count;
2020 bfd_size_type num_rel_hashes;
2022 /* Figure out how many relocations there will be. */
2023 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2024 reloc_count = elf_section_data (o)->rel_count;
2026 reloc_count = elf_section_data (o)->rel_count2;
2028 num_rel_hashes = o->reloc_count;
2029 if (num_rel_hashes < reloc_count)
2030 num_rel_hashes = reloc_count;
2032 /* That allows us to calculate the size of the section. */
2033 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2035 /* The contents field must last into write_object_contents, so we
2036 allocate it with bfd_alloc rather than malloc. Also since we
2037 cannot be sure that the contents will actually be filled in,
2038 we zero the allocated space. */
2039 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2040 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2043 /* We only allocate one set of hash entries, so we only do it the
2044 first time we are called. */
2045 if (elf_section_data (o)->rel_hashes == NULL
2048 struct elf_link_hash_entry **p;
2050 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2054 elf_section_data (o)->rel_hashes = p;
2060 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2061 originated from the section given by INPUT_REL_HDR) to the
2065 _bfd_elf_link_output_relocs (bfd *output_bfd,
2066 asection *input_section,
2067 Elf_Internal_Shdr *input_rel_hdr,
2068 Elf_Internal_Rela *internal_relocs)
2070 Elf_Internal_Rela *irela;
2071 Elf_Internal_Rela *irelaend;
2073 Elf_Internal_Shdr *output_rel_hdr;
2074 asection *output_section;
2075 unsigned int *rel_countp = NULL;
2076 const struct elf_backend_data *bed;
2077 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2079 output_section = input_section->output_section;
2080 output_rel_hdr = NULL;
2082 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2083 == input_rel_hdr->sh_entsize)
2085 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2086 rel_countp = &elf_section_data (output_section)->rel_count;
2088 else if (elf_section_data (output_section)->rel_hdr2
2089 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2090 == input_rel_hdr->sh_entsize))
2092 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2093 rel_countp = &elf_section_data (output_section)->rel_count2;
2097 (*_bfd_error_handler)
2098 (_("%B: relocation size mismatch in %B section %A"),
2099 output_bfd, input_section->owner, input_section);
2100 bfd_set_error (bfd_error_wrong_object_format);
2104 bed = get_elf_backend_data (output_bfd);
2105 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2106 swap_out = bed->s->swap_reloc_out;
2107 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2108 swap_out = bed->s->swap_reloca_out;
2112 erel = output_rel_hdr->contents;
2113 erel += *rel_countp * input_rel_hdr->sh_entsize;
2114 irela = internal_relocs;
2115 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2116 * bed->s->int_rels_per_ext_rel);
2117 while (irela < irelaend)
2119 (*swap_out) (output_bfd, irela, erel);
2120 irela += bed->s->int_rels_per_ext_rel;
2121 erel += input_rel_hdr->sh_entsize;
2124 /* Bump the counter, so that we know where to add the next set of
2126 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2131 /* Fix up the flags for a symbol. This handles various cases which
2132 can only be fixed after all the input files are seen. This is
2133 currently called by both adjust_dynamic_symbol and
2134 assign_sym_version, which is unnecessary but perhaps more robust in
2135 the face of future changes. */
2138 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2139 struct elf_info_failed *eif)
2141 /* If this symbol was mentioned in a non-ELF file, try to set
2142 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2143 permit a non-ELF file to correctly refer to a symbol defined in
2144 an ELF dynamic object. */
2147 while (h->root.type == bfd_link_hash_indirect)
2148 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2150 if (h->root.type != bfd_link_hash_defined
2151 && h->root.type != bfd_link_hash_defweak)
2154 h->ref_regular_nonweak = 1;
2158 if (h->root.u.def.section->owner != NULL
2159 && (bfd_get_flavour (h->root.u.def.section->owner)
2160 == bfd_target_elf_flavour))
2163 h->ref_regular_nonweak = 1;
2169 if (h->dynindx == -1
2173 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2182 /* Unfortunately, NON_ELF is only correct if the symbol
2183 was first seen in a non-ELF file. Fortunately, if the symbol
2184 was first seen in an ELF file, we're probably OK unless the
2185 symbol was defined in a non-ELF file. Catch that case here.
2186 FIXME: We're still in trouble if the symbol was first seen in
2187 a dynamic object, and then later in a non-ELF regular object. */
2188 if ((h->root.type == bfd_link_hash_defined
2189 || h->root.type == bfd_link_hash_defweak)
2191 && (h->root.u.def.section->owner != NULL
2192 ? (bfd_get_flavour (h->root.u.def.section->owner)
2193 != bfd_target_elf_flavour)
2194 : (bfd_is_abs_section (h->root.u.def.section)
2195 && !h->def_dynamic)))
2199 /* If this is a final link, and the symbol was defined as a common
2200 symbol in a regular object file, and there was no definition in
2201 any dynamic object, then the linker will have allocated space for
2202 the symbol in a common section but the DEF_REGULAR
2203 flag will not have been set. */
2204 if (h->root.type == bfd_link_hash_defined
2208 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2211 /* If -Bsymbolic was used (which means to bind references to global
2212 symbols to the definition within the shared object), and this
2213 symbol was defined in a regular object, then it actually doesn't
2214 need a PLT entry. Likewise, if the symbol has non-default
2215 visibility. If the symbol has hidden or internal visibility, we
2216 will force it local. */
2218 && eif->info->shared
2219 && is_elf_hash_table (eif->info->hash)
2220 && (eif->info->symbolic
2221 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2224 const struct elf_backend_data *bed;
2225 bfd_boolean force_local;
2227 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2229 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2230 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2231 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2234 /* If a weak undefined symbol has non-default visibility, we also
2235 hide it from the dynamic linker. */
2236 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2237 && h->root.type == bfd_link_hash_undefweak)
2239 const struct elf_backend_data *bed;
2240 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2241 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2244 /* If this is a weak defined symbol in a dynamic object, and we know
2245 the real definition in the dynamic object, copy interesting flags
2246 over to the real definition. */
2247 if (h->u.weakdef != NULL)
2249 struct elf_link_hash_entry *weakdef;
2251 weakdef = h->u.weakdef;
2252 if (h->root.type == bfd_link_hash_indirect)
2253 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2255 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2256 || h->root.type == bfd_link_hash_defweak);
2257 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2258 || weakdef->root.type == bfd_link_hash_defweak);
2259 BFD_ASSERT (weakdef->def_dynamic);
2261 /* If the real definition is defined by a regular object file,
2262 don't do anything special. See the longer description in
2263 _bfd_elf_adjust_dynamic_symbol, below. */
2264 if (weakdef->def_regular)
2265 h->u.weakdef = NULL;
2268 const struct elf_backend_data *bed;
2270 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2271 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2278 /* Make the backend pick a good value for a dynamic symbol. This is
2279 called via elf_link_hash_traverse, and also calls itself
2283 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2285 struct elf_info_failed *eif = data;
2287 const struct elf_backend_data *bed;
2289 if (! is_elf_hash_table (eif->info->hash))
2292 if (h->root.type == bfd_link_hash_warning)
2294 h->plt = elf_hash_table (eif->info)->init_offset;
2295 h->got = elf_hash_table (eif->info)->init_offset;
2297 /* When warning symbols are created, they **replace** the "real"
2298 entry in the hash table, thus we never get to see the real
2299 symbol in a hash traversal. So look at it now. */
2300 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2303 /* Ignore indirect symbols. These are added by the versioning code. */
2304 if (h->root.type == bfd_link_hash_indirect)
2307 /* Fix the symbol flags. */
2308 if (! _bfd_elf_fix_symbol_flags (h, eif))
2311 /* If this symbol does not require a PLT entry, and it is not
2312 defined by a dynamic object, or is not referenced by a regular
2313 object, ignore it. We do have to handle a weak defined symbol,
2314 even if no regular object refers to it, if we decided to add it
2315 to the dynamic symbol table. FIXME: Do we normally need to worry
2316 about symbols which are defined by one dynamic object and
2317 referenced by another one? */
2322 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2324 h->plt = elf_hash_table (eif->info)->init_offset;
2328 /* If we've already adjusted this symbol, don't do it again. This
2329 can happen via a recursive call. */
2330 if (h->dynamic_adjusted)
2333 /* Don't look at this symbol again. Note that we must set this
2334 after checking the above conditions, because we may look at a
2335 symbol once, decide not to do anything, and then get called
2336 recursively later after REF_REGULAR is set below. */
2337 h->dynamic_adjusted = 1;
2339 /* If this is a weak definition, and we know a real definition, and
2340 the real symbol is not itself defined by a regular object file,
2341 then get a good value for the real definition. We handle the
2342 real symbol first, for the convenience of the backend routine.
2344 Note that there is a confusing case here. If the real definition
2345 is defined by a regular object file, we don't get the real symbol
2346 from the dynamic object, but we do get the weak symbol. If the
2347 processor backend uses a COPY reloc, then if some routine in the
2348 dynamic object changes the real symbol, we will not see that
2349 change in the corresponding weak symbol. This is the way other
2350 ELF linkers work as well, and seems to be a result of the shared
2353 I will clarify this issue. Most SVR4 shared libraries define the
2354 variable _timezone and define timezone as a weak synonym. The
2355 tzset call changes _timezone. If you write
2356 extern int timezone;
2358 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2359 you might expect that, since timezone is a synonym for _timezone,
2360 the same number will print both times. However, if the processor
2361 backend uses a COPY reloc, then actually timezone will be copied
2362 into your process image, and, since you define _timezone
2363 yourself, _timezone will not. Thus timezone and _timezone will
2364 wind up at different memory locations. The tzset call will set
2365 _timezone, leaving timezone unchanged. */
2367 if (h->u.weakdef != NULL)
2369 /* If we get to this point, we know there is an implicit
2370 reference by a regular object file via the weak symbol H.
2371 FIXME: Is this really true? What if the traversal finds
2372 H->U.WEAKDEF before it finds H? */
2373 h->u.weakdef->ref_regular = 1;
2375 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2379 /* If a symbol has no type and no size and does not require a PLT
2380 entry, then we are probably about to do the wrong thing here: we
2381 are probably going to create a COPY reloc for an empty object.
2382 This case can arise when a shared object is built with assembly
2383 code, and the assembly code fails to set the symbol type. */
2385 && h->type == STT_NOTYPE
2387 (*_bfd_error_handler)
2388 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2389 h->root.root.string);
2391 dynobj = elf_hash_table (eif->info)->dynobj;
2392 bed = get_elf_backend_data (dynobj);
2393 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2402 /* Adjust all external symbols pointing into SEC_MERGE sections
2403 to reflect the object merging within the sections. */
2406 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2410 if (h->root.type == bfd_link_hash_warning)
2411 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2413 if ((h->root.type == bfd_link_hash_defined
2414 || h->root.type == bfd_link_hash_defweak)
2415 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2416 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2418 bfd *output_bfd = data;
2420 h->root.u.def.value =
2421 _bfd_merged_section_offset (output_bfd,
2422 &h->root.u.def.section,
2423 elf_section_data (sec)->sec_info,
2424 h->root.u.def.value);
2430 /* Returns false if the symbol referred to by H should be considered
2431 to resolve local to the current module, and true if it should be
2432 considered to bind dynamically. */
2435 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2436 struct bfd_link_info *info,
2437 bfd_boolean ignore_protected)
2439 bfd_boolean binding_stays_local_p;
2444 while (h->root.type == bfd_link_hash_indirect
2445 || h->root.type == bfd_link_hash_warning)
2446 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2448 /* If it was forced local, then clearly it's not dynamic. */
2449 if (h->dynindx == -1)
2451 if (h->forced_local)
2454 /* Identify the cases where name binding rules say that a
2455 visible symbol resolves locally. */
2456 binding_stays_local_p = info->executable || info->symbolic;
2458 switch (ELF_ST_VISIBILITY (h->other))
2465 /* Proper resolution for function pointer equality may require
2466 that these symbols perhaps be resolved dynamically, even though
2467 we should be resolving them to the current module. */
2468 if (!ignore_protected)
2469 binding_stays_local_p = TRUE;
2476 /* If it isn't defined locally, then clearly it's dynamic. */
2477 if (!h->def_regular)
2480 /* Otherwise, the symbol is dynamic if binding rules don't tell
2481 us that it remains local. */
2482 return !binding_stays_local_p;
2485 /* Return true if the symbol referred to by H should be considered
2486 to resolve local to the current module, and false otherwise. Differs
2487 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2488 undefined symbols and weak symbols. */
2491 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2492 struct bfd_link_info *info,
2493 bfd_boolean local_protected)
2495 /* If it's a local sym, of course we resolve locally. */
2499 /* Common symbols that become definitions don't get the DEF_REGULAR
2500 flag set, so test it first, and don't bail out. */
2501 if (ELF_COMMON_DEF_P (h))
2503 /* If we don't have a definition in a regular file, then we can't
2504 resolve locally. The sym is either undefined or dynamic. */
2505 else if (!h->def_regular)
2508 /* Forced local symbols resolve locally. */
2509 if (h->forced_local)
2512 /* As do non-dynamic symbols. */
2513 if (h->dynindx == -1)
2516 /* At this point, we know the symbol is defined and dynamic. In an
2517 executable it must resolve locally, likewise when building symbolic
2518 shared libraries. */
2519 if (info->executable || info->symbolic)
2522 /* Now deal with defined dynamic symbols in shared libraries. Ones
2523 with default visibility might not resolve locally. */
2524 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2527 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2528 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2531 /* Function pointer equality tests may require that STV_PROTECTED
2532 symbols be treated as dynamic symbols, even when we know that the
2533 dynamic linker will resolve them locally. */
2534 return local_protected;
2537 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2538 aligned. Returns the first TLS output section. */
2540 struct bfd_section *
2541 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2543 struct bfd_section *sec, *tls;
2544 unsigned int align = 0;
2546 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2547 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2551 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2552 if (sec->alignment_power > align)
2553 align = sec->alignment_power;
2555 elf_hash_table (info)->tls_sec = tls;
2557 /* Ensure the alignment of the first section is the largest alignment,
2558 so that the tls segment starts aligned. */
2560 tls->alignment_power = align;
2565 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2567 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2568 Elf_Internal_Sym *sym)
2570 /* Local symbols do not count, but target specific ones might. */
2571 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2572 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2575 /* Function symbols do not count. */
2576 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2579 /* If the section is undefined, then so is the symbol. */
2580 if (sym->st_shndx == SHN_UNDEF)
2583 /* If the symbol is defined in the common section, then
2584 it is a common definition and so does not count. */
2585 if (sym->st_shndx == SHN_COMMON)
2588 /* If the symbol is in a target specific section then we
2589 must rely upon the backend to tell us what it is. */
2590 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2591 /* FIXME - this function is not coded yet:
2593 return _bfd_is_global_symbol_definition (abfd, sym);
2595 Instead for now assume that the definition is not global,
2596 Even if this is wrong, at least the linker will behave
2597 in the same way that it used to do. */
2603 /* Search the symbol table of the archive element of the archive ABFD
2604 whose archive map contains a mention of SYMDEF, and determine if
2605 the symbol is defined in this element. */
2607 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2609 Elf_Internal_Shdr * hdr;
2610 bfd_size_type symcount;
2611 bfd_size_type extsymcount;
2612 bfd_size_type extsymoff;
2613 Elf_Internal_Sym *isymbuf;
2614 Elf_Internal_Sym *isym;
2615 Elf_Internal_Sym *isymend;
2618 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2622 if (! bfd_check_format (abfd, bfd_object))
2625 /* If we have already included the element containing this symbol in the
2626 link then we do not need to include it again. Just claim that any symbol
2627 it contains is not a definition, so that our caller will not decide to
2628 (re)include this element. */
2629 if (abfd->archive_pass)
2632 /* Select the appropriate symbol table. */
2633 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2634 hdr = &elf_tdata (abfd)->symtab_hdr;
2636 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2638 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2640 /* The sh_info field of the symtab header tells us where the
2641 external symbols start. We don't care about the local symbols. */
2642 if (elf_bad_symtab (abfd))
2644 extsymcount = symcount;
2649 extsymcount = symcount - hdr->sh_info;
2650 extsymoff = hdr->sh_info;
2653 if (extsymcount == 0)
2656 /* Read in the symbol table. */
2657 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2659 if (isymbuf == NULL)
2662 /* Scan the symbol table looking for SYMDEF. */
2664 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2668 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2673 if (strcmp (name, symdef->name) == 0)
2675 result = is_global_data_symbol_definition (abfd, isym);
2685 /* Add an entry to the .dynamic table. */
2688 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2692 struct elf_link_hash_table *hash_table;
2693 const struct elf_backend_data *bed;
2695 bfd_size_type newsize;
2696 bfd_byte *newcontents;
2697 Elf_Internal_Dyn dyn;
2699 hash_table = elf_hash_table (info);
2700 if (! is_elf_hash_table (hash_table))
2703 if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
2705 (_("warning: creating a DT_TEXTREL in a shared object."));
2707 bed = get_elf_backend_data (hash_table->dynobj);
2708 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2709 BFD_ASSERT (s != NULL);
2711 newsize = s->size + bed->s->sizeof_dyn;
2712 newcontents = bfd_realloc (s->contents, newsize);
2713 if (newcontents == NULL)
2717 dyn.d_un.d_val = val;
2718 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2721 s->contents = newcontents;
2726 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2727 otherwise just check whether one already exists. Returns -1 on error,
2728 1 if a DT_NEEDED tag already exists, and 0 on success. */
2731 elf_add_dt_needed_tag (struct bfd_link_info *info,
2735 struct elf_link_hash_table *hash_table;
2736 bfd_size_type oldsize;
2737 bfd_size_type strindex;
2739 hash_table = elf_hash_table (info);
2740 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2741 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2742 if (strindex == (bfd_size_type) -1)
2745 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2748 const struct elf_backend_data *bed;
2751 bed = get_elf_backend_data (hash_table->dynobj);
2752 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2753 BFD_ASSERT (sdyn != NULL);
2755 for (extdyn = sdyn->contents;
2756 extdyn < sdyn->contents + sdyn->size;
2757 extdyn += bed->s->sizeof_dyn)
2759 Elf_Internal_Dyn dyn;
2761 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2762 if (dyn.d_tag == DT_NEEDED
2763 && dyn.d_un.d_val == strindex)
2765 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2773 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2777 /* We were just checking for existence of the tag. */
2778 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2783 /* Sort symbol by value and section. */
2785 elf_sort_symbol (const void *arg1, const void *arg2)
2787 const struct elf_link_hash_entry *h1;
2788 const struct elf_link_hash_entry *h2;
2789 bfd_signed_vma vdiff;
2791 h1 = *(const struct elf_link_hash_entry **) arg1;
2792 h2 = *(const struct elf_link_hash_entry **) arg2;
2793 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2795 return vdiff > 0 ? 1 : -1;
2798 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2800 return sdiff > 0 ? 1 : -1;
2805 /* This function is used to adjust offsets into .dynstr for
2806 dynamic symbols. This is called via elf_link_hash_traverse. */
2809 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2811 struct elf_strtab_hash *dynstr = data;
2813 if (h->root.type == bfd_link_hash_warning)
2814 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2816 if (h->dynindx != -1)
2817 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2821 /* Assign string offsets in .dynstr, update all structures referencing
2825 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2827 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2828 struct elf_link_local_dynamic_entry *entry;
2829 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2830 bfd *dynobj = hash_table->dynobj;
2833 const struct elf_backend_data *bed;
2836 _bfd_elf_strtab_finalize (dynstr);
2837 size = _bfd_elf_strtab_size (dynstr);
2839 bed = get_elf_backend_data (dynobj);
2840 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2841 BFD_ASSERT (sdyn != NULL);
2843 /* Update all .dynamic entries referencing .dynstr strings. */
2844 for (extdyn = sdyn->contents;
2845 extdyn < sdyn->contents + sdyn->size;
2846 extdyn += bed->s->sizeof_dyn)
2848 Elf_Internal_Dyn dyn;
2850 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
2854 dyn.d_un.d_val = size;
2862 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2867 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
2870 /* Now update local dynamic symbols. */
2871 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
2872 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2873 entry->isym.st_name);
2875 /* And the rest of dynamic symbols. */
2876 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
2878 /* Adjust version definitions. */
2879 if (elf_tdata (output_bfd)->cverdefs)
2884 Elf_Internal_Verdef def;
2885 Elf_Internal_Verdaux defaux;
2887 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2891 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2893 p += sizeof (Elf_External_Verdef);
2894 for (i = 0; i < def.vd_cnt; ++i)
2896 _bfd_elf_swap_verdaux_in (output_bfd,
2897 (Elf_External_Verdaux *) p, &defaux);
2898 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2900 _bfd_elf_swap_verdaux_out (output_bfd,
2901 &defaux, (Elf_External_Verdaux *) p);
2902 p += sizeof (Elf_External_Verdaux);
2905 while (def.vd_next);
2908 /* Adjust version references. */
2909 if (elf_tdata (output_bfd)->verref)
2914 Elf_Internal_Verneed need;
2915 Elf_Internal_Vernaux needaux;
2917 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2921 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2923 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2924 _bfd_elf_swap_verneed_out (output_bfd, &need,
2925 (Elf_External_Verneed *) p);
2926 p += sizeof (Elf_External_Verneed);
2927 for (i = 0; i < need.vn_cnt; ++i)
2929 _bfd_elf_swap_vernaux_in (output_bfd,
2930 (Elf_External_Vernaux *) p, &needaux);
2931 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2933 _bfd_elf_swap_vernaux_out (output_bfd,
2935 (Elf_External_Vernaux *) p);
2936 p += sizeof (Elf_External_Vernaux);
2939 while (need.vn_next);
2945 /* Add symbols from an ELF object file to the linker hash table. */
2948 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2950 bfd_boolean (*add_symbol_hook)
2951 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
2952 const char **, flagword *, asection **, bfd_vma *);
2953 bfd_boolean (*check_relocs)
2954 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
2955 bfd_boolean (*check_directives)
2956 (bfd *, struct bfd_link_info *);
2957 bfd_boolean collect;
2958 Elf_Internal_Shdr *hdr;
2959 bfd_size_type symcount;
2960 bfd_size_type extsymcount;
2961 bfd_size_type extsymoff;
2962 struct elf_link_hash_entry **sym_hash;
2963 bfd_boolean dynamic;
2964 Elf_External_Versym *extversym = NULL;
2965 Elf_External_Versym *ever;
2966 struct elf_link_hash_entry *weaks;
2967 struct elf_link_hash_entry **nondeflt_vers = NULL;
2968 bfd_size_type nondeflt_vers_cnt = 0;
2969 Elf_Internal_Sym *isymbuf = NULL;
2970 Elf_Internal_Sym *isym;
2971 Elf_Internal_Sym *isymend;
2972 const struct elf_backend_data *bed;
2973 bfd_boolean add_needed;
2974 struct elf_link_hash_table * hash_table;
2977 hash_table = elf_hash_table (info);
2979 bed = get_elf_backend_data (abfd);
2980 add_symbol_hook = bed->elf_add_symbol_hook;
2981 collect = bed->collect;
2983 if ((abfd->flags & DYNAMIC) == 0)
2989 /* You can't use -r against a dynamic object. Also, there's no
2990 hope of using a dynamic object which does not exactly match
2991 the format of the output file. */
2992 if (info->relocatable
2993 || !is_elf_hash_table (hash_table)
2994 || hash_table->root.creator != abfd->xvec)
2996 if (info->relocatable)
2997 bfd_set_error (bfd_error_invalid_operation);
2999 bfd_set_error (bfd_error_wrong_format);
3004 /* As a GNU extension, any input sections which are named
3005 .gnu.warning.SYMBOL are treated as warning symbols for the given
3006 symbol. This differs from .gnu.warning sections, which generate
3007 warnings when they are included in an output file. */
3008 if (info->executable)
3012 for (s = abfd->sections; s != NULL; s = s->next)
3016 name = bfd_get_section_name (abfd, s);
3017 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3021 bfd_size_type prefix_len;
3022 const char * gnu_warning_prefix = _("warning: ");
3024 name += sizeof ".gnu.warning." - 1;
3026 /* If this is a shared object, then look up the symbol
3027 in the hash table. If it is there, and it is already
3028 been defined, then we will not be using the entry
3029 from this shared object, so we don't need to warn.
3030 FIXME: If we see the definition in a regular object
3031 later on, we will warn, but we shouldn't. The only
3032 fix is to keep track of what warnings we are supposed
3033 to emit, and then handle them all at the end of the
3037 struct elf_link_hash_entry *h;
3039 h = elf_link_hash_lookup (hash_table, name,
3040 FALSE, FALSE, TRUE);
3042 /* FIXME: What about bfd_link_hash_common? */
3044 && (h->root.type == bfd_link_hash_defined
3045 || h->root.type == bfd_link_hash_defweak))
3047 /* We don't want to issue this warning. Clobber
3048 the section size so that the warning does not
3049 get copied into the output file. */
3056 prefix_len = strlen (gnu_warning_prefix);
3057 msg = bfd_alloc (abfd, prefix_len + sz + 1);
3061 strcpy (msg, gnu_warning_prefix);
3062 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
3065 msg[prefix_len + sz] = '\0';
3067 if (! (_bfd_generic_link_add_one_symbol
3068 (info, abfd, name, BSF_WARNING, s, 0, msg,
3069 FALSE, collect, NULL)))
3072 if (! info->relocatable)
3074 /* Clobber the section size so that the warning does
3075 not get copied into the output file. */
3085 /* If we are creating a shared library, create all the dynamic
3086 sections immediately. We need to attach them to something,
3087 so we attach them to this BFD, provided it is the right
3088 format. FIXME: If there are no input BFD's of the same
3089 format as the output, we can't make a shared library. */
3091 && is_elf_hash_table (hash_table)
3092 && hash_table->root.creator == abfd->xvec
3093 && ! hash_table->dynamic_sections_created)
3095 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3099 else if (!is_elf_hash_table (hash_table))
3104 const char *soname = NULL;
3105 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3108 /* ld --just-symbols and dynamic objects don't mix very well.
3109 Test for --just-symbols by looking at info set up by
3110 _bfd_elf_link_just_syms. */
3111 if ((s = abfd->sections) != NULL
3112 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3115 /* If this dynamic lib was specified on the command line with
3116 --as-needed in effect, then we don't want to add a DT_NEEDED
3117 tag unless the lib is actually used. Similary for libs brought
3118 in by another lib's DT_NEEDED. When --no-add-needed is used
3119 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3120 any dynamic library in DT_NEEDED tags in the dynamic lib at
3122 add_needed = (elf_dyn_lib_class (abfd)
3123 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3124 | DYN_NO_NEEDED)) == 0;
3126 s = bfd_get_section_by_name (abfd, ".dynamic");
3132 unsigned long shlink;
3134 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3135 goto error_free_dyn;
3137 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3139 goto error_free_dyn;
3140 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3142 for (extdyn = dynbuf;
3143 extdyn < dynbuf + s->size;
3144 extdyn += bed->s->sizeof_dyn)
3146 Elf_Internal_Dyn dyn;
3148 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3149 if (dyn.d_tag == DT_SONAME)
3151 unsigned int tagv = dyn.d_un.d_val;
3152 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3154 goto error_free_dyn;
3156 if (dyn.d_tag == DT_NEEDED)
3158 struct bfd_link_needed_list *n, **pn;
3160 unsigned int tagv = dyn.d_un.d_val;
3162 amt = sizeof (struct bfd_link_needed_list);
3163 n = bfd_alloc (abfd, amt);
3164 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3165 if (n == NULL || fnm == NULL)
3166 goto error_free_dyn;
3167 amt = strlen (fnm) + 1;
3168 anm = bfd_alloc (abfd, amt);
3170 goto error_free_dyn;
3171 memcpy (anm, fnm, amt);
3175 for (pn = & hash_table->needed;
3181 if (dyn.d_tag == DT_RUNPATH)
3183 struct bfd_link_needed_list *n, **pn;
3185 unsigned int tagv = dyn.d_un.d_val;
3187 amt = sizeof (struct bfd_link_needed_list);
3188 n = bfd_alloc (abfd, amt);
3189 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3190 if (n == NULL || fnm == NULL)
3191 goto error_free_dyn;
3192 amt = strlen (fnm) + 1;
3193 anm = bfd_alloc (abfd, amt);
3195 goto error_free_dyn;
3196 memcpy (anm, fnm, amt);
3200 for (pn = & runpath;
3206 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3207 if (!runpath && dyn.d_tag == DT_RPATH)
3209 struct bfd_link_needed_list *n, **pn;
3211 unsigned int tagv = dyn.d_un.d_val;
3213 amt = sizeof (struct bfd_link_needed_list);
3214 n = bfd_alloc (abfd, amt);
3215 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3216 if (n == NULL || fnm == NULL)
3217 goto error_free_dyn;
3218 amt = strlen (fnm) + 1;
3219 anm = bfd_alloc (abfd, amt);
3226 memcpy (anm, fnm, amt);
3241 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3242 frees all more recently bfd_alloc'd blocks as well. */
3248 struct bfd_link_needed_list **pn;
3249 for (pn = & hash_table->runpath;
3256 /* We do not want to include any of the sections in a dynamic
3257 object in the output file. We hack by simply clobbering the
3258 list of sections in the BFD. This could be handled more
3259 cleanly by, say, a new section flag; the existing
3260 SEC_NEVER_LOAD flag is not the one we want, because that one
3261 still implies that the section takes up space in the output
3263 bfd_section_list_clear (abfd);
3265 /* If this is the first dynamic object found in the link, create
3266 the special sections required for dynamic linking. */
3267 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3270 /* Find the name to use in a DT_NEEDED entry that refers to this
3271 object. If the object has a DT_SONAME entry, we use it.
3272 Otherwise, if the generic linker stuck something in
3273 elf_dt_name, we use that. Otherwise, we just use the file
3275 if (soname == NULL || *soname == '\0')
3277 soname = elf_dt_name (abfd);
3278 if (soname == NULL || *soname == '\0')
3279 soname = bfd_get_filename (abfd);
3282 /* Save the SONAME because sometimes the linker emulation code
3283 will need to know it. */
3284 elf_dt_name (abfd) = soname;
3286 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3290 /* If we have already included this dynamic object in the
3291 link, just ignore it. There is no reason to include a
3292 particular dynamic object more than once. */
3297 /* If this is a dynamic object, we always link against the .dynsym
3298 symbol table, not the .symtab symbol table. The dynamic linker
3299 will only see the .dynsym symbol table, so there is no reason to
3300 look at .symtab for a dynamic object. */
3302 if (! dynamic || elf_dynsymtab (abfd) == 0)
3303 hdr = &elf_tdata (abfd)->symtab_hdr;
3305 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3307 symcount = hdr->sh_size / bed->s->sizeof_sym;
3309 /* The sh_info field of the symtab header tells us where the
3310 external symbols start. We don't care about the local symbols at
3312 if (elf_bad_symtab (abfd))
3314 extsymcount = symcount;
3319 extsymcount = symcount - hdr->sh_info;
3320 extsymoff = hdr->sh_info;
3324 if (extsymcount != 0)
3326 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3328 if (isymbuf == NULL)
3331 /* We store a pointer to the hash table entry for each external
3333 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3334 sym_hash = bfd_alloc (abfd, amt);
3335 if (sym_hash == NULL)
3336 goto error_free_sym;
3337 elf_sym_hashes (abfd) = sym_hash;
3342 /* Read in any version definitions. */
3343 if (! _bfd_elf_slurp_version_tables (abfd))
3344 goto error_free_sym;
3346 /* Read in the symbol versions, but don't bother to convert them
3347 to internal format. */
3348 if (elf_dynversym (abfd) != 0)
3350 Elf_Internal_Shdr *versymhdr;
3352 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3353 extversym = bfd_malloc (versymhdr->sh_size);
3354 if (extversym == NULL)
3355 goto error_free_sym;
3356 amt = versymhdr->sh_size;
3357 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3358 || bfd_bread (extversym, amt, abfd) != amt)
3359 goto error_free_vers;
3365 ever = extversym != NULL ? extversym + extsymoff : NULL;
3366 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3368 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3375 struct elf_link_hash_entry *h;
3376 bfd_boolean definition;
3377 bfd_boolean size_change_ok;
3378 bfd_boolean type_change_ok;
3379 bfd_boolean new_weakdef;
3380 bfd_boolean override;
3381 unsigned int old_alignment;
3386 flags = BSF_NO_FLAGS;
3388 value = isym->st_value;
3391 bind = ELF_ST_BIND (isym->st_info);
3392 if (bind == STB_LOCAL)
3394 /* This should be impossible, since ELF requires that all
3395 global symbols follow all local symbols, and that sh_info
3396 point to the first global symbol. Unfortunately, Irix 5
3400 else if (bind == STB_GLOBAL)
3402 if (isym->st_shndx != SHN_UNDEF
3403 && isym->st_shndx != SHN_COMMON)
3406 else if (bind == STB_WEAK)
3410 /* Leave it up to the processor backend. */
3413 if (isym->st_shndx == SHN_UNDEF)
3414 sec = bfd_und_section_ptr;
3415 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3417 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3419 sec = bfd_abs_section_ptr;
3420 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3423 else if (isym->st_shndx == SHN_ABS)
3424 sec = bfd_abs_section_ptr;
3425 else if (isym->st_shndx == SHN_COMMON)
3427 sec = bfd_com_section_ptr;
3428 /* What ELF calls the size we call the value. What ELF
3429 calls the value we call the alignment. */
3430 value = isym->st_size;
3434 /* Leave it up to the processor backend. */
3437 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3440 goto error_free_vers;
3442 if (isym->st_shndx == SHN_COMMON
3443 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3445 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3449 tcomm = bfd_make_section (abfd, ".tcommon");
3451 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
3453 | SEC_LINKER_CREATED
3454 | SEC_THREAD_LOCAL)))
3455 goto error_free_vers;
3459 else if (add_symbol_hook)
3461 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3463 goto error_free_vers;
3465 /* The hook function sets the name to NULL if this symbol
3466 should be skipped for some reason. */
3471 /* Sanity check that all possibilities were handled. */
3474 bfd_set_error (bfd_error_bad_value);
3475 goto error_free_vers;
3478 if (bfd_is_und_section (sec)
3479 || bfd_is_com_section (sec))
3484 size_change_ok = FALSE;
3485 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3489 if (is_elf_hash_table (hash_table))
3491 Elf_Internal_Versym iver;
3492 unsigned int vernum = 0;
3497 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3498 vernum = iver.vs_vers & VERSYM_VERSION;
3500 /* If this is a hidden symbol, or if it is not version
3501 1, we append the version name to the symbol name.
3502 However, we do not modify a non-hidden absolute
3503 symbol, because it might be the version symbol
3504 itself. FIXME: What if it isn't? */
3505 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3506 || (vernum > 1 && ! bfd_is_abs_section (sec)))
3509 size_t namelen, verlen, newlen;
3512 if (isym->st_shndx != SHN_UNDEF)
3514 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
3516 (*_bfd_error_handler)
3517 (_("%B: %s: invalid version %u (max %d)"),
3519 elf_tdata (abfd)->dynverdef_hdr.sh_info);
3520 bfd_set_error (bfd_error_bad_value);
3521 goto error_free_vers;
3523 else if (vernum > 1)
3525 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3531 /* We cannot simply test for the number of
3532 entries in the VERNEED section since the
3533 numbers for the needed versions do not start
3535 Elf_Internal_Verneed *t;
3538 for (t = elf_tdata (abfd)->verref;
3542 Elf_Internal_Vernaux *a;
3544 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3546 if (a->vna_other == vernum)
3548 verstr = a->vna_nodename;
3557 (*_bfd_error_handler)
3558 (_("%B: %s: invalid needed version %d"),
3559 abfd, name, vernum);
3560 bfd_set_error (bfd_error_bad_value);
3561 goto error_free_vers;
3565 namelen = strlen (name);
3566 verlen = strlen (verstr);
3567 newlen = namelen + verlen + 2;
3568 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3569 && isym->st_shndx != SHN_UNDEF)
3572 newname = bfd_alloc (abfd, newlen);
3573 if (newname == NULL)
3574 goto error_free_vers;
3575 memcpy (newname, name, namelen);
3576 p = newname + namelen;
3578 /* If this is a defined non-hidden version symbol,
3579 we add another @ to the name. This indicates the
3580 default version of the symbol. */
3581 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3582 && isym->st_shndx != SHN_UNDEF)
3584 memcpy (p, verstr, verlen + 1);
3590 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
3591 sym_hash, &skip, &override,
3592 &type_change_ok, &size_change_ok))
3593 goto error_free_vers;
3602 while (h->root.type == bfd_link_hash_indirect
3603 || h->root.type == bfd_link_hash_warning)
3604 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3606 /* Remember the old alignment if this is a common symbol, so
3607 that we don't reduce the alignment later on. We can't
3608 check later, because _bfd_generic_link_add_one_symbol
3609 will set a default for the alignment which we want to
3610 override. We also remember the old bfd where the existing
3611 definition comes from. */
3612 switch (h->root.type)
3617 case bfd_link_hash_defined:
3618 case bfd_link_hash_defweak:
3619 old_bfd = h->root.u.def.section->owner;
3622 case bfd_link_hash_common:
3623 old_bfd = h->root.u.c.p->section->owner;
3624 old_alignment = h->root.u.c.p->alignment_power;
3628 if (elf_tdata (abfd)->verdef != NULL
3632 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3635 if (! (_bfd_generic_link_add_one_symbol
3636 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3637 (struct bfd_link_hash_entry **) sym_hash)))
3638 goto error_free_vers;
3641 while (h->root.type == bfd_link_hash_indirect
3642 || h->root.type == bfd_link_hash_warning)
3643 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3646 new_weakdef = FALSE;
3649 && (flags & BSF_WEAK) != 0
3650 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3651 && is_elf_hash_table (hash_table)
3652 && h->u.weakdef == NULL)
3654 /* Keep a list of all weak defined non function symbols from
3655 a dynamic object, using the weakdef field. Later in this
3656 function we will set the weakdef field to the correct
3657 value. We only put non-function symbols from dynamic
3658 objects on this list, because that happens to be the only
3659 time we need to know the normal symbol corresponding to a
3660 weak symbol, and the information is time consuming to
3661 figure out. If the weakdef field is not already NULL,
3662 then this symbol was already defined by some previous
3663 dynamic object, and we will be using that previous
3664 definition anyhow. */
3666 h->u.weakdef = weaks;
3671 /* Set the alignment of a common symbol. */
3672 if (isym->st_shndx == SHN_COMMON
3673 && h->root.type == bfd_link_hash_common)
3677 align = bfd_log2 (isym->st_value);
3678 if (align > old_alignment
3679 /* Permit an alignment power of zero if an alignment of one
3680 is specified and no other alignments have been specified. */
3681 || (isym->st_value == 1 && old_alignment == 0))
3682 h->root.u.c.p->alignment_power = align;
3684 h->root.u.c.p->alignment_power = old_alignment;
3687 if (is_elf_hash_table (hash_table))
3691 /* Check the alignment when a common symbol is involved. This
3692 can change when a common symbol is overridden by a normal
3693 definition or a common symbol is ignored due to the old
3694 normal definition. We need to make sure the maximum
3695 alignment is maintained. */
3696 if ((old_alignment || isym->st_shndx == SHN_COMMON)
3697 && h->root.type != bfd_link_hash_common)
3699 unsigned int common_align;
3700 unsigned int normal_align;
3701 unsigned int symbol_align;
3705 symbol_align = ffs (h->root.u.def.value) - 1;
3706 if (h->root.u.def.section->owner != NULL
3707 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3709 normal_align = h->root.u.def.section->alignment_power;
3710 if (normal_align > symbol_align)
3711 normal_align = symbol_align;
3714 normal_align = symbol_align;
3718 common_align = old_alignment;
3719 common_bfd = old_bfd;
3724 common_align = bfd_log2 (isym->st_value);
3726 normal_bfd = old_bfd;
3729 if (normal_align < common_align)
3730 (*_bfd_error_handler)
3731 (_("Warning: alignment %u of symbol `%s' in %B"
3732 " is smaller than %u in %B"),
3733 normal_bfd, common_bfd,
3734 1 << normal_align, name, 1 << common_align);
3737 /* Remember the symbol size and type. */
3738 if (isym->st_size != 0
3739 && (definition || h->size == 0))
3741 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3742 (*_bfd_error_handler)
3743 (_("Warning: size of symbol `%s' changed"
3744 " from %lu in %B to %lu in %B"),
3746 name, (unsigned long) h->size,
3747 (unsigned long) isym->st_size);
3749 h->size = isym->st_size;
3752 /* If this is a common symbol, then we always want H->SIZE
3753 to be the size of the common symbol. The code just above
3754 won't fix the size if a common symbol becomes larger. We
3755 don't warn about a size change here, because that is
3756 covered by --warn-common. */
3757 if (h->root.type == bfd_link_hash_common)
3758 h->size = h->root.u.c.size;
3760 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3761 && (definition || h->type == STT_NOTYPE))
3763 if (h->type != STT_NOTYPE
3764 && h->type != ELF_ST_TYPE (isym->st_info)
3765 && ! type_change_ok)
3766 (*_bfd_error_handler)
3767 (_("Warning: type of symbol `%s' changed"
3768 " from %d to %d in %B"),
3769 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3771 h->type = ELF_ST_TYPE (isym->st_info);
3774 /* If st_other has a processor-specific meaning, specific
3775 code might be needed here. We never merge the visibility
3776 attribute with the one from a dynamic object. */
3777 if (bed->elf_backend_merge_symbol_attribute)
3778 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3781 if (isym->st_other != 0 && !dynamic)
3783 unsigned char hvis, symvis, other, nvis;
3785 /* Take the balance of OTHER from the definition. */
3786 other = (definition ? isym->st_other : h->other);
3787 other &= ~ ELF_ST_VISIBILITY (-1);
3789 /* Combine visibilities, using the most constraining one. */
3790 hvis = ELF_ST_VISIBILITY (h->other);
3791 symvis = ELF_ST_VISIBILITY (isym->st_other);
3797 nvis = hvis < symvis ? hvis : symvis;
3799 h->other = other | nvis;
3802 /* Set a flag in the hash table entry indicating the type of
3803 reference or definition we just found. Keep a count of
3804 the number of dynamic symbols we find. A dynamic symbol
3805 is one which is referenced or defined by both a regular
3806 object and a shared object. */
3813 if (bind != STB_WEAK)
3814 h->ref_regular_nonweak = 1;
3818 if (! info->executable
3831 || (h->u.weakdef != NULL
3833 && h->u.weakdef->dynindx != -1))
3837 /* Check to see if we need to add an indirect symbol for
3838 the default name. */
3839 if (definition || h->root.type == bfd_link_hash_common)
3840 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
3841 &sec, &value, &dynsym,
3843 goto error_free_vers;
3845 if (definition && !dynamic)
3847 char *p = strchr (name, ELF_VER_CHR);
3848 if (p != NULL && p[1] != ELF_VER_CHR)
3850 /* Queue non-default versions so that .symver x, x@FOO
3851 aliases can be checked. */
3852 if (! nondeflt_vers)
3854 amt = (isymend - isym + 1)
3855 * sizeof (struct elf_link_hash_entry *);
3856 nondeflt_vers = bfd_malloc (amt);
3858 nondeflt_vers [nondeflt_vers_cnt++] = h;
3862 if (dynsym && h->dynindx == -1)
3864 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3865 goto error_free_vers;
3866 if (h->u.weakdef != NULL
3868 && h->u.weakdef->dynindx == -1)
3870 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
3871 goto error_free_vers;
3874 else if (dynsym && h->dynindx != -1)
3875 /* If the symbol already has a dynamic index, but
3876 visibility says it should not be visible, turn it into
3878 switch (ELF_ST_VISIBILITY (h->other))
3882 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
3893 const char *soname = elf_dt_name (abfd);
3895 /* A symbol from a library loaded via DT_NEEDED of some
3896 other library is referenced by a regular object.
3897 Add a DT_NEEDED entry for it. Issue an error if
3898 --no-add-needed is used. */
3899 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
3901 (*_bfd_error_handler)
3902 (_("%s: invalid DSO for symbol `%s' definition"),
3904 bfd_set_error (bfd_error_bad_value);
3905 goto error_free_vers;
3909 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3911 goto error_free_vers;
3913 BFD_ASSERT (ret == 0);
3918 /* Now that all the symbols from this input file are created, handle
3919 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3920 if (nondeflt_vers != NULL)
3922 bfd_size_type cnt, symidx;
3924 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
3926 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
3927 char *shortname, *p;
3929 p = strchr (h->root.root.string, ELF_VER_CHR);
3931 || (h->root.type != bfd_link_hash_defined
3932 && h->root.type != bfd_link_hash_defweak))
3935 amt = p - h->root.root.string;
3936 shortname = bfd_malloc (amt + 1);
3937 memcpy (shortname, h->root.root.string, amt);
3938 shortname[amt] = '\0';
3940 hi = (struct elf_link_hash_entry *)
3941 bfd_link_hash_lookup (&hash_table->root, shortname,
3942 FALSE, FALSE, FALSE);
3944 && hi->root.type == h->root.type
3945 && hi->root.u.def.value == h->root.u.def.value
3946 && hi->root.u.def.section == h->root.u.def.section)
3948 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
3949 hi->root.type = bfd_link_hash_indirect;
3950 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
3951 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
3952 sym_hash = elf_sym_hashes (abfd);
3954 for (symidx = 0; symidx < extsymcount; ++symidx)
3955 if (sym_hash[symidx] == hi)
3957 sym_hash[symidx] = h;
3963 free (nondeflt_vers);
3964 nondeflt_vers = NULL;
3967 if (extversym != NULL)
3973 if (isymbuf != NULL)
3977 /* Now set the weakdefs field correctly for all the weak defined
3978 symbols we found. The only way to do this is to search all the
3979 symbols. Since we only need the information for non functions in
3980 dynamic objects, that's the only time we actually put anything on
3981 the list WEAKS. We need this information so that if a regular
3982 object refers to a symbol defined weakly in a dynamic object, the
3983 real symbol in the dynamic object is also put in the dynamic
3984 symbols; we also must arrange for both symbols to point to the
3985 same memory location. We could handle the general case of symbol
3986 aliasing, but a general symbol alias can only be generated in
3987 assembler code, handling it correctly would be very time
3988 consuming, and other ELF linkers don't handle general aliasing
3992 struct elf_link_hash_entry **hpp;
3993 struct elf_link_hash_entry **hppend;
3994 struct elf_link_hash_entry **sorted_sym_hash;
3995 struct elf_link_hash_entry *h;
3998 /* Since we have to search the whole symbol list for each weak
3999 defined symbol, search time for N weak defined symbols will be
4000 O(N^2). Binary search will cut it down to O(NlogN). */
4001 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4002 sorted_sym_hash = bfd_malloc (amt);
4003 if (sorted_sym_hash == NULL)
4005 sym_hash = sorted_sym_hash;
4006 hpp = elf_sym_hashes (abfd);
4007 hppend = hpp + extsymcount;
4009 for (; hpp < hppend; hpp++)
4013 && h->root.type == bfd_link_hash_defined
4014 && h->type != STT_FUNC)
4022 qsort (sorted_sym_hash, sym_count,
4023 sizeof (struct elf_link_hash_entry *),
4026 while (weaks != NULL)
4028 struct elf_link_hash_entry *hlook;
4035 weaks = hlook->u.weakdef;
4036 hlook->u.weakdef = NULL;
4038 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4039 || hlook->root.type == bfd_link_hash_defweak
4040 || hlook->root.type == bfd_link_hash_common
4041 || hlook->root.type == bfd_link_hash_indirect);
4042 slook = hlook->root.u.def.section;
4043 vlook = hlook->root.u.def.value;
4050 bfd_signed_vma vdiff;
4052 h = sorted_sym_hash [idx];
4053 vdiff = vlook - h->root.u.def.value;
4060 long sdiff = slook->id - h->root.u.def.section->id;
4073 /* We didn't find a value/section match. */
4077 for (i = ilook; i < sym_count; i++)
4079 h = sorted_sym_hash [i];
4081 /* Stop if value or section doesn't match. */
4082 if (h->root.u.def.value != vlook
4083 || h->root.u.def.section != slook)
4085 else if (h != hlook)
4087 hlook->u.weakdef = h;
4089 /* If the weak definition is in the list of dynamic
4090 symbols, make sure the real definition is put
4092 if (hlook->dynindx != -1 && h->dynindx == -1)
4094 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4098 /* If the real definition is in the list of dynamic
4099 symbols, make sure the weak definition is put
4100 there as well. If we don't do this, then the
4101 dynamic loader might not merge the entries for the
4102 real definition and the weak definition. */
4103 if (h->dynindx != -1 && hlook->dynindx == -1)
4105 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4113 free (sorted_sym_hash);
4116 check_directives = get_elf_backend_data (abfd)->check_directives;
4117 if (check_directives)
4118 check_directives (abfd, info);
4120 /* If this object is the same format as the output object, and it is
4121 not a shared library, then let the backend look through the
4124 This is required to build global offset table entries and to
4125 arrange for dynamic relocs. It is not required for the
4126 particular common case of linking non PIC code, even when linking
4127 against shared libraries, but unfortunately there is no way of
4128 knowing whether an object file has been compiled PIC or not.
4129 Looking through the relocs is not particularly time consuming.
4130 The problem is that we must either (1) keep the relocs in memory,
4131 which causes the linker to require additional runtime memory or
4132 (2) read the relocs twice from the input file, which wastes time.
4133 This would be a good case for using mmap.
4135 I have no idea how to handle linking PIC code into a file of a
4136 different format. It probably can't be done. */
4137 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4139 && is_elf_hash_table (hash_table)
4140 && hash_table->root.creator == abfd->xvec
4141 && check_relocs != NULL)
4145 for (o = abfd->sections; o != NULL; o = o->next)
4147 Elf_Internal_Rela *internal_relocs;
4150 if ((o->flags & SEC_RELOC) == 0
4151 || o->reloc_count == 0
4152 || ((info->strip == strip_all || info->strip == strip_debugger)
4153 && (o->flags & SEC_DEBUGGING) != 0)
4154 || bfd_is_abs_section (o->output_section))
4157 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4159 if (internal_relocs == NULL)
4162 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4164 if (elf_section_data (o)->relocs != internal_relocs)
4165 free (internal_relocs);
4172 /* If this is a non-traditional link, try to optimize the handling
4173 of the .stab/.stabstr sections. */
4175 && ! info->traditional_format
4176 && is_elf_hash_table (hash_table)
4177 && (info->strip != strip_all && info->strip != strip_debugger))
4181 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4182 if (stabstr != NULL)
4184 bfd_size_type string_offset = 0;
4187 for (stab = abfd->sections; stab; stab = stab->next)
4188 if (strncmp (".stab", stab->name, 5) == 0
4189 && (!stab->name[5] ||
4190 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4191 && (stab->flags & SEC_MERGE) == 0
4192 && !bfd_is_abs_section (stab->output_section))
4194 struct bfd_elf_section_data *secdata;
4196 secdata = elf_section_data (stab);
4197 if (! _bfd_link_section_stabs (abfd,
4198 &hash_table->stab_info,
4203 if (secdata->sec_info)
4204 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4209 if (is_elf_hash_table (hash_table))
4211 /* Add this bfd to the loaded list. */
4212 struct elf_link_loaded_list *n;
4214 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4218 n->next = hash_table->loaded;
4219 hash_table->loaded = n;
4225 if (nondeflt_vers != NULL)
4226 free (nondeflt_vers);
4227 if (extversym != NULL)
4230 if (isymbuf != NULL)
4236 /* Return the linker hash table entry of a symbol that might be
4237 satisfied by an archive symbol. Return -1 on error. */
4239 struct elf_link_hash_entry *
4240 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4241 struct bfd_link_info *info,
4244 struct elf_link_hash_entry *h;
4248 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4252 /* If this is a default version (the name contains @@), look up the
4253 symbol again with only one `@' as well as without the version.
4254 The effect is that references to the symbol with and without the
4255 version will be matched by the default symbol in the archive. */
4257 p = strchr (name, ELF_VER_CHR);
4258 if (p == NULL || p[1] != ELF_VER_CHR)
4261 /* First check with only one `@'. */
4262 len = strlen (name);
4263 copy = bfd_alloc (abfd, len);
4265 return (struct elf_link_hash_entry *) 0 - 1;
4267 first = p - name + 1;
4268 memcpy (copy, name, first);
4269 memcpy (copy + first, name + first + 1, len - first);
4271 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4274 /* We also need to check references to the symbol without the
4276 copy[first - 1] = '\0';
4277 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4278 FALSE, FALSE, FALSE);
4281 bfd_release (abfd, copy);
4285 /* Add symbols from an ELF archive file to the linker hash table. We
4286 don't use _bfd_generic_link_add_archive_symbols because of a
4287 problem which arises on UnixWare. The UnixWare libc.so is an
4288 archive which includes an entry libc.so.1 which defines a bunch of
4289 symbols. The libc.so archive also includes a number of other
4290 object files, which also define symbols, some of which are the same
4291 as those defined in libc.so.1. Correct linking requires that we
4292 consider each object file in turn, and include it if it defines any
4293 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4294 this; it looks through the list of undefined symbols, and includes
4295 any object file which defines them. When this algorithm is used on
4296 UnixWare, it winds up pulling in libc.so.1 early and defining a
4297 bunch of symbols. This means that some of the other objects in the
4298 archive are not included in the link, which is incorrect since they
4299 precede libc.so.1 in the archive.
4301 Fortunately, ELF archive handling is simpler than that done by
4302 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4303 oddities. In ELF, if we find a symbol in the archive map, and the
4304 symbol is currently undefined, we know that we must pull in that
4307 Unfortunately, we do have to make multiple passes over the symbol
4308 table until nothing further is resolved. */
4311 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4314 bfd_boolean *defined = NULL;
4315 bfd_boolean *included = NULL;
4319 const struct elf_backend_data *bed;
4320 struct elf_link_hash_entry * (*archive_symbol_lookup)
4321 (bfd *, struct bfd_link_info *, const char *);
4323 if (! bfd_has_map (abfd))
4325 /* An empty archive is a special case. */
4326 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4328 bfd_set_error (bfd_error_no_armap);
4332 /* Keep track of all symbols we know to be already defined, and all
4333 files we know to be already included. This is to speed up the
4334 second and subsequent passes. */
4335 c = bfd_ardata (abfd)->symdef_count;
4339 amt *= sizeof (bfd_boolean);
4340 defined = bfd_zmalloc (amt);
4341 included = bfd_zmalloc (amt);
4342 if (defined == NULL || included == NULL)
4345 symdefs = bfd_ardata (abfd)->symdefs;
4346 bed = get_elf_backend_data (abfd);
4347 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4360 symdefend = symdef + c;
4361 for (i = 0; symdef < symdefend; symdef++, i++)
4363 struct elf_link_hash_entry *h;
4365 struct bfd_link_hash_entry *undefs_tail;
4368 if (defined[i] || included[i])
4370 if (symdef->file_offset == last)
4376 h = archive_symbol_lookup (abfd, info, symdef->name);
4377 if (h == (struct elf_link_hash_entry *) 0 - 1)
4383 if (h->root.type == bfd_link_hash_common)
4385 /* We currently have a common symbol. The archive map contains
4386 a reference to this symbol, so we may want to include it. We
4387 only want to include it however, if this archive element
4388 contains a definition of the symbol, not just another common
4391 Unfortunately some archivers (including GNU ar) will put
4392 declarations of common symbols into their archive maps, as
4393 well as real definitions, so we cannot just go by the archive
4394 map alone. Instead we must read in the element's symbol
4395 table and check that to see what kind of symbol definition
4397 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4400 else if (h->root.type != bfd_link_hash_undefined)
4402 if (h->root.type != bfd_link_hash_undefweak)
4407 /* We need to include this archive member. */
4408 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4409 if (element == NULL)
4412 if (! bfd_check_format (element, bfd_object))
4415 /* Doublecheck that we have not included this object
4416 already--it should be impossible, but there may be
4417 something wrong with the archive. */
4418 if (element->archive_pass != 0)
4420 bfd_set_error (bfd_error_bad_value);
4423 element->archive_pass = 1;
4425 undefs_tail = info->hash->undefs_tail;
4427 if (! (*info->callbacks->add_archive_element) (info, element,
4430 if (! bfd_link_add_symbols (element, info))
4433 /* If there are any new undefined symbols, we need to make
4434 another pass through the archive in order to see whether
4435 they can be defined. FIXME: This isn't perfect, because
4436 common symbols wind up on undefs_tail and because an
4437 undefined symbol which is defined later on in this pass
4438 does not require another pass. This isn't a bug, but it
4439 does make the code less efficient than it could be. */
4440 if (undefs_tail != info->hash->undefs_tail)
4443 /* Look backward to mark all symbols from this object file
4444 which we have already seen in this pass. */
4448 included[mark] = TRUE;
4453 while (symdefs[mark].file_offset == symdef->file_offset);
4455 /* We mark subsequent symbols from this object file as we go
4456 on through the loop. */
4457 last = symdef->file_offset;
4468 if (defined != NULL)
4470 if (included != NULL)
4475 /* Given an ELF BFD, add symbols to the global hash table as
4479 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4481 switch (bfd_get_format (abfd))
4484 return elf_link_add_object_symbols (abfd, info);
4486 return elf_link_add_archive_symbols (abfd, info);
4488 bfd_set_error (bfd_error_wrong_format);
4493 /* This function will be called though elf_link_hash_traverse to store
4494 all hash value of the exported symbols in an array. */
4497 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4499 unsigned long **valuep = data;
4505 if (h->root.type == bfd_link_hash_warning)
4506 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4508 /* Ignore indirect symbols. These are added by the versioning code. */
4509 if (h->dynindx == -1)
4512 name = h->root.root.string;
4513 p = strchr (name, ELF_VER_CHR);
4516 alc = bfd_malloc (p - name + 1);
4517 memcpy (alc, name, p - name);
4518 alc[p - name] = '\0';
4522 /* Compute the hash value. */
4523 ha = bfd_elf_hash (name);
4525 /* Store the found hash value in the array given as the argument. */
4528 /* And store it in the struct so that we can put it in the hash table
4530 h->u.elf_hash_value = ha;
4538 /* Array used to determine the number of hash table buckets to use
4539 based on the number of symbols there are. If there are fewer than
4540 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4541 fewer than 37 we use 17 buckets, and so forth. We never use more
4542 than 32771 buckets. */
4544 static const size_t elf_buckets[] =
4546 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4550 /* Compute bucket count for hashing table. We do not use a static set
4551 of possible tables sizes anymore. Instead we determine for all
4552 possible reasonable sizes of the table the outcome (i.e., the
4553 number of collisions etc) and choose the best solution. The
4554 weighting functions are not too simple to allow the table to grow
4555 without bounds. Instead one of the weighting factors is the size.
4556 Therefore the result is always a good payoff between few collisions
4557 (= short chain lengths) and table size. */
4559 compute_bucket_count (struct bfd_link_info *info)
4561 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4562 size_t best_size = 0;
4563 unsigned long int *hashcodes;
4564 unsigned long int *hashcodesp;
4565 unsigned long int i;
4568 /* Compute the hash values for all exported symbols. At the same
4569 time store the values in an array so that we could use them for
4572 amt *= sizeof (unsigned long int);
4573 hashcodes = bfd_malloc (amt);
4574 if (hashcodes == NULL)
4576 hashcodesp = hashcodes;
4578 /* Put all hash values in HASHCODES. */
4579 elf_link_hash_traverse (elf_hash_table (info),
4580 elf_collect_hash_codes, &hashcodesp);
4582 /* We have a problem here. The following code to optimize the table
4583 size requires an integer type with more the 32 bits. If
4584 BFD_HOST_U_64_BIT is set we know about such a type. */
4585 #ifdef BFD_HOST_U_64_BIT
4588 unsigned long int nsyms = hashcodesp - hashcodes;
4591 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4592 unsigned long int *counts ;
4593 bfd *dynobj = elf_hash_table (info)->dynobj;
4594 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4596 /* Possible optimization parameters: if we have NSYMS symbols we say
4597 that the hashing table must at least have NSYMS/4 and at most
4599 minsize = nsyms / 4;
4602 best_size = maxsize = nsyms * 2;
4604 /* Create array where we count the collisions in. We must use bfd_malloc
4605 since the size could be large. */
4607 amt *= sizeof (unsigned long int);
4608 counts = bfd_malloc (amt);
4615 /* Compute the "optimal" size for the hash table. The criteria is a
4616 minimal chain length. The minor criteria is (of course) the size
4618 for (i = minsize; i < maxsize; ++i)
4620 /* Walk through the array of hashcodes and count the collisions. */
4621 BFD_HOST_U_64_BIT max;
4622 unsigned long int j;
4623 unsigned long int fact;
4625 memset (counts, '\0', i * sizeof (unsigned long int));
4627 /* Determine how often each hash bucket is used. */
4628 for (j = 0; j < nsyms; ++j)
4629 ++counts[hashcodes[j] % i];
4631 /* For the weight function we need some information about the
4632 pagesize on the target. This is information need not be 100%
4633 accurate. Since this information is not available (so far) we
4634 define it here to a reasonable default value. If it is crucial
4635 to have a better value some day simply define this value. */
4636 # ifndef BFD_TARGET_PAGESIZE
4637 # define BFD_TARGET_PAGESIZE (4096)
4640 /* We in any case need 2 + NSYMS entries for the size values and
4642 max = (2 + nsyms) * (bed->s->arch_size / 8);
4645 /* Variant 1: optimize for short chains. We add the squares
4646 of all the chain lengths (which favors many small chain
4647 over a few long chains). */
4648 for (j = 0; j < i; ++j)
4649 max += counts[j] * counts[j];
4651 /* This adds penalties for the overall size of the table. */
4652 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4655 /* Variant 2: Optimize a lot more for small table. Here we
4656 also add squares of the size but we also add penalties for
4657 empty slots (the +1 term). */
4658 for (j = 0; j < i; ++j)
4659 max += (1 + counts[j]) * (1 + counts[j]);
4661 /* The overall size of the table is considered, but not as
4662 strong as in variant 1, where it is squared. */
4663 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4667 /* Compare with current best results. */
4668 if (max < best_chlen)
4678 #endif /* defined (BFD_HOST_U_64_BIT) */
4680 /* This is the fallback solution if no 64bit type is available or if we
4681 are not supposed to spend much time on optimizations. We select the
4682 bucket count using a fixed set of numbers. */
4683 for (i = 0; elf_buckets[i] != 0; i++)
4685 best_size = elf_buckets[i];
4686 if (dynsymcount < elf_buckets[i + 1])
4691 /* Free the arrays we needed. */
4697 /* Set up the sizes and contents of the ELF dynamic sections. This is
4698 called by the ELF linker emulation before_allocation routine. We
4699 must set the sizes of the sections before the linker sets the
4700 addresses of the various sections. */
4703 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4706 const char *filter_shlib,
4707 const char * const *auxiliary_filters,
4708 struct bfd_link_info *info,
4709 asection **sinterpptr,
4710 struct bfd_elf_version_tree *verdefs)
4712 bfd_size_type soname_indx;
4714 const struct elf_backend_data *bed;
4715 struct elf_assign_sym_version_info asvinfo;
4719 soname_indx = (bfd_size_type) -1;
4721 if (!is_elf_hash_table (info->hash))
4724 elf_tdata (output_bfd)->relro = info->relro;
4725 if (info->execstack)
4726 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4727 else if (info->noexecstack)
4728 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4732 asection *notesec = NULL;
4735 for (inputobj = info->input_bfds;
4737 inputobj = inputobj->link_next)
4741 if (inputobj->flags & DYNAMIC)
4743 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4746 if (s->flags & SEC_CODE)
4755 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4756 if (exec && info->relocatable
4757 && notesec->output_section != bfd_abs_section_ptr)
4758 notesec->output_section->flags |= SEC_CODE;
4762 /* Any syms created from now on start with -1 in
4763 got.refcount/offset and plt.refcount/offset. */
4764 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
4766 /* The backend may have to create some sections regardless of whether
4767 we're dynamic or not. */
4768 bed = get_elf_backend_data (output_bfd);
4769 if (bed->elf_backend_always_size_sections
4770 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4773 dynobj = elf_hash_table (info)->dynobj;
4775 /* If there were no dynamic objects in the link, there is nothing to
4780 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
4783 if (elf_hash_table (info)->dynamic_sections_created)
4785 struct elf_info_failed eif;
4786 struct elf_link_hash_entry *h;
4788 struct bfd_elf_version_tree *t;
4789 struct bfd_elf_version_expr *d;
4790 bfd_boolean all_defined;
4792 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
4793 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
4797 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4799 if (soname_indx == (bfd_size_type) -1
4800 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
4806 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
4808 info->flags |= DF_SYMBOLIC;
4815 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
4817 if (indx == (bfd_size_type) -1
4818 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
4821 if (info->new_dtags)
4823 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
4824 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
4829 if (filter_shlib != NULL)
4833 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4834 filter_shlib, TRUE);
4835 if (indx == (bfd_size_type) -1
4836 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
4840 if (auxiliary_filters != NULL)
4842 const char * const *p;
4844 for (p = auxiliary_filters; *p != NULL; p++)
4848 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4850 if (indx == (bfd_size_type) -1
4851 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
4857 eif.verdefs = verdefs;
4860 /* If we are supposed to export all symbols into the dynamic symbol
4861 table (this is not the normal case), then do so. */
4862 if (info->export_dynamic)
4864 elf_link_hash_traverse (elf_hash_table (info),
4865 _bfd_elf_export_symbol,
4871 /* Make all global versions with definition. */
4872 for (t = verdefs; t != NULL; t = t->next)
4873 for (d = t->globals.list; d != NULL; d = d->next)
4874 if (!d->symver && d->symbol)
4876 const char *verstr, *name;
4877 size_t namelen, verlen, newlen;
4879 struct elf_link_hash_entry *newh;
4882 namelen = strlen (name);
4884 verlen = strlen (verstr);
4885 newlen = namelen + verlen + 3;
4887 newname = bfd_malloc (newlen);
4888 if (newname == NULL)
4890 memcpy (newname, name, namelen);
4892 /* Check the hidden versioned definition. */
4893 p = newname + namelen;
4895 memcpy (p, verstr, verlen + 1);
4896 newh = elf_link_hash_lookup (elf_hash_table (info),
4897 newname, FALSE, FALSE,
4900 || (newh->root.type != bfd_link_hash_defined
4901 && newh->root.type != bfd_link_hash_defweak))
4903 /* Check the default versioned definition. */
4905 memcpy (p, verstr, verlen + 1);
4906 newh = elf_link_hash_lookup (elf_hash_table (info),
4907 newname, FALSE, FALSE,
4912 /* Mark this version if there is a definition and it is
4913 not defined in a shared object. */
4915 && !newh->def_dynamic
4916 && (newh->root.type == bfd_link_hash_defined
4917 || newh->root.type == bfd_link_hash_defweak))
4921 /* Attach all the symbols to their version information. */
4922 asvinfo.output_bfd = output_bfd;
4923 asvinfo.info = info;
4924 asvinfo.verdefs = verdefs;
4925 asvinfo.failed = FALSE;
4927 elf_link_hash_traverse (elf_hash_table (info),
4928 _bfd_elf_link_assign_sym_version,
4933 if (!info->allow_undefined_version)
4935 /* Check if all global versions have a definition. */
4937 for (t = verdefs; t != NULL; t = t->next)
4938 for (d = t->globals.list; d != NULL; d = d->next)
4939 if (!d->symver && !d->script)
4941 (*_bfd_error_handler)
4942 (_("%s: undefined version: %s"),
4943 d->pattern, t->name);
4944 all_defined = FALSE;
4949 bfd_set_error (bfd_error_bad_value);
4954 /* Find all symbols which were defined in a dynamic object and make
4955 the backend pick a reasonable value for them. */
4956 elf_link_hash_traverse (elf_hash_table (info),
4957 _bfd_elf_adjust_dynamic_symbol,
4962 /* Add some entries to the .dynamic section. We fill in some of the
4963 values later, in bfd_elf_final_link, but we must add the entries
4964 now so that we know the final size of the .dynamic section. */
4966 /* If there are initialization and/or finalization functions to
4967 call then add the corresponding DT_INIT/DT_FINI entries. */
4968 h = (info->init_function
4969 ? elf_link_hash_lookup (elf_hash_table (info),
4970 info->init_function, FALSE,
4977 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
4980 h = (info->fini_function
4981 ? elf_link_hash_lookup (elf_hash_table (info),
4982 info->fini_function, FALSE,
4989 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
4993 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
4995 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4996 if (! info->executable)
5001 for (sub = info->input_bfds; sub != NULL;
5002 sub = sub->link_next)
5003 for (o = sub->sections; o != NULL; o = o->next)
5004 if (elf_section_data (o)->this_hdr.sh_type
5005 == SHT_PREINIT_ARRAY)
5007 (*_bfd_error_handler)
5008 (_("%B: .preinit_array section is not allowed in DSO"),
5013 bfd_set_error (bfd_error_nonrepresentable_section);
5017 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5018 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5021 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
5023 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5024 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5027 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
5029 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5030 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5034 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5035 /* If .dynstr is excluded from the link, we don't want any of
5036 these tags. Strictly, we should be checking each section
5037 individually; This quick check covers for the case where
5038 someone does a /DISCARD/ : { *(*) }. */
5039 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5041 bfd_size_type strsize;
5043 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5044 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5045 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5046 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5047 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5048 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5049 bed->s->sizeof_sym))
5054 /* The backend must work out the sizes of all the other dynamic
5056 if (bed->elf_backend_size_dynamic_sections
5057 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5060 if (elf_hash_table (info)->dynamic_sections_created)
5062 bfd_size_type dynsymcount;
5064 size_t bucketcount = 0;
5065 size_t hash_entry_size;
5066 unsigned int dtagcount;
5068 /* Set up the version definition section. */
5069 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5070 BFD_ASSERT (s != NULL);
5072 /* We may have created additional version definitions if we are
5073 just linking a regular application. */
5074 verdefs = asvinfo.verdefs;
5076 /* Skip anonymous version tag. */
5077 if (verdefs != NULL && verdefs->vernum == 0)
5078 verdefs = verdefs->next;
5080 if (verdefs == NULL)
5081 _bfd_strip_section_from_output (info, s);
5086 struct bfd_elf_version_tree *t;
5088 Elf_Internal_Verdef def;
5089 Elf_Internal_Verdaux defaux;
5094 /* Make space for the base version. */
5095 size += sizeof (Elf_External_Verdef);
5096 size += sizeof (Elf_External_Verdaux);
5099 for (t = verdefs; t != NULL; t = t->next)
5101 struct bfd_elf_version_deps *n;
5103 size += sizeof (Elf_External_Verdef);
5104 size += sizeof (Elf_External_Verdaux);
5107 for (n = t->deps; n != NULL; n = n->next)
5108 size += sizeof (Elf_External_Verdaux);
5112 s->contents = bfd_alloc (output_bfd, s->size);
5113 if (s->contents == NULL && s->size != 0)
5116 /* Fill in the version definition section. */
5120 def.vd_version = VER_DEF_CURRENT;
5121 def.vd_flags = VER_FLG_BASE;
5124 def.vd_aux = sizeof (Elf_External_Verdef);
5125 def.vd_next = (sizeof (Elf_External_Verdef)
5126 + sizeof (Elf_External_Verdaux));
5128 if (soname_indx != (bfd_size_type) -1)
5130 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5132 def.vd_hash = bfd_elf_hash (soname);
5133 defaux.vda_name = soname_indx;
5140 name = basename (output_bfd->filename);
5141 def.vd_hash = bfd_elf_hash (name);
5142 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5144 if (indx == (bfd_size_type) -1)
5146 defaux.vda_name = indx;
5148 defaux.vda_next = 0;
5150 _bfd_elf_swap_verdef_out (output_bfd, &def,
5151 (Elf_External_Verdef *) p);
5152 p += sizeof (Elf_External_Verdef);
5153 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5154 (Elf_External_Verdaux *) p);
5155 p += sizeof (Elf_External_Verdaux);
5157 for (t = verdefs; t != NULL; t = t->next)
5160 struct bfd_elf_version_deps *n;
5161 struct elf_link_hash_entry *h;
5162 struct bfd_link_hash_entry *bh;
5165 for (n = t->deps; n != NULL; n = n->next)
5168 /* Add a symbol representing this version. */
5170 if (! (_bfd_generic_link_add_one_symbol
5171 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5173 get_elf_backend_data (dynobj)->collect, &bh)))
5175 h = (struct elf_link_hash_entry *) bh;
5178 h->type = STT_OBJECT;
5179 h->verinfo.vertree = t;
5181 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5184 def.vd_version = VER_DEF_CURRENT;
5186 if (t->globals.list == NULL
5187 && t->locals.list == NULL
5189 def.vd_flags |= VER_FLG_WEAK;
5190 def.vd_ndx = t->vernum + 1;
5191 def.vd_cnt = cdeps + 1;
5192 def.vd_hash = bfd_elf_hash (t->name);
5193 def.vd_aux = sizeof (Elf_External_Verdef);
5195 if (t->next != NULL)
5196 def.vd_next = (sizeof (Elf_External_Verdef)
5197 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5199 _bfd_elf_swap_verdef_out (output_bfd, &def,
5200 (Elf_External_Verdef *) p);
5201 p += sizeof (Elf_External_Verdef);
5203 defaux.vda_name = h->dynstr_index;
5204 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5206 defaux.vda_next = 0;
5207 if (t->deps != NULL)
5208 defaux.vda_next = sizeof (Elf_External_Verdaux);
5209 t->name_indx = defaux.vda_name;
5211 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5212 (Elf_External_Verdaux *) p);
5213 p += sizeof (Elf_External_Verdaux);
5215 for (n = t->deps; n != NULL; n = n->next)
5217 if (n->version_needed == NULL)
5219 /* This can happen if there was an error in the
5221 defaux.vda_name = 0;
5225 defaux.vda_name = n->version_needed->name_indx;
5226 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5229 if (n->next == NULL)
5230 defaux.vda_next = 0;
5232 defaux.vda_next = sizeof (Elf_External_Verdaux);
5234 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5235 (Elf_External_Verdaux *) p);
5236 p += sizeof (Elf_External_Verdaux);
5240 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5241 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5244 elf_tdata (output_bfd)->cverdefs = cdefs;
5247 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5249 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5252 else if (info->flags & DF_BIND_NOW)
5254 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5260 if (info->executable)
5261 info->flags_1 &= ~ (DF_1_INITFIRST
5264 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5268 /* Work out the size of the version reference section. */
5270 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5271 BFD_ASSERT (s != NULL);
5273 struct elf_find_verdep_info sinfo;
5275 sinfo.output_bfd = output_bfd;
5277 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5278 if (sinfo.vers == 0)
5280 sinfo.failed = FALSE;
5282 elf_link_hash_traverse (elf_hash_table (info),
5283 _bfd_elf_link_find_version_dependencies,
5286 if (elf_tdata (output_bfd)->verref == NULL)
5287 _bfd_strip_section_from_output (info, s);
5290 Elf_Internal_Verneed *t;
5295 /* Build the version definition section. */
5298 for (t = elf_tdata (output_bfd)->verref;
5302 Elf_Internal_Vernaux *a;
5304 size += sizeof (Elf_External_Verneed);
5306 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5307 size += sizeof (Elf_External_Vernaux);
5311 s->contents = bfd_alloc (output_bfd, s->size);
5312 if (s->contents == NULL)
5316 for (t = elf_tdata (output_bfd)->verref;
5321 Elf_Internal_Vernaux *a;
5325 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5328 t->vn_version = VER_NEED_CURRENT;
5330 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5331 elf_dt_name (t->vn_bfd) != NULL
5332 ? elf_dt_name (t->vn_bfd)
5333 : basename (t->vn_bfd->filename),
5335 if (indx == (bfd_size_type) -1)
5338 t->vn_aux = sizeof (Elf_External_Verneed);
5339 if (t->vn_nextref == NULL)
5342 t->vn_next = (sizeof (Elf_External_Verneed)
5343 + caux * sizeof (Elf_External_Vernaux));
5345 _bfd_elf_swap_verneed_out (output_bfd, t,
5346 (Elf_External_Verneed *) p);
5347 p += sizeof (Elf_External_Verneed);
5349 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5351 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5352 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5353 a->vna_nodename, FALSE);
5354 if (indx == (bfd_size_type) -1)
5357 if (a->vna_nextptr == NULL)
5360 a->vna_next = sizeof (Elf_External_Vernaux);
5362 _bfd_elf_swap_vernaux_out (output_bfd, a,
5363 (Elf_External_Vernaux *) p);
5364 p += sizeof (Elf_External_Vernaux);
5368 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5369 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5372 elf_tdata (output_bfd)->cverrefs = crefs;
5376 /* Assign dynsym indicies. In a shared library we generate a
5377 section symbol for each output section, which come first.
5378 Next come all of the back-end allocated local dynamic syms,
5379 followed by the rest of the global symbols. */
5381 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5383 /* Work out the size of the symbol version section. */
5384 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5385 BFD_ASSERT (s != NULL);
5386 if (dynsymcount == 0
5387 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
5389 _bfd_strip_section_from_output (info, s);
5390 /* The DYNSYMCOUNT might have changed if we were going to
5391 output a dynamic symbol table entry for S. */
5392 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5396 s->size = dynsymcount * sizeof (Elf_External_Versym);
5397 s->contents = bfd_zalloc (output_bfd, s->size);
5398 if (s->contents == NULL)
5401 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5405 /* Set the size of the .dynsym and .hash sections. We counted
5406 the number of dynamic symbols in elf_link_add_object_symbols.
5407 We will build the contents of .dynsym and .hash when we build
5408 the final symbol table, because until then we do not know the
5409 correct value to give the symbols. We built the .dynstr
5410 section as we went along in elf_link_add_object_symbols. */
5411 s = bfd_get_section_by_name (dynobj, ".dynsym");
5412 BFD_ASSERT (s != NULL);
5413 s->size = dynsymcount * bed->s->sizeof_sym;
5414 s->contents = bfd_alloc (output_bfd, s->size);
5415 if (s->contents == NULL && s->size != 0)
5418 if (dynsymcount != 0)
5420 Elf_Internal_Sym isym;
5422 /* The first entry in .dynsym is a dummy symbol. */
5429 bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0);
5432 /* Compute the size of the hashing table. As a side effect this
5433 computes the hash values for all the names we export. */
5434 bucketcount = compute_bucket_count (info);
5436 s = bfd_get_section_by_name (dynobj, ".hash");
5437 BFD_ASSERT (s != NULL);
5438 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5439 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5440 s->contents = bfd_zalloc (output_bfd, s->size);
5441 if (s->contents == NULL)
5444 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5445 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5446 s->contents + hash_entry_size);
5448 elf_hash_table (info)->bucketcount = bucketcount;
5450 s = bfd_get_section_by_name (dynobj, ".dynstr");
5451 BFD_ASSERT (s != NULL);
5453 elf_finalize_dynstr (output_bfd, info);
5455 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5457 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5458 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5465 /* Final phase of ELF linker. */
5467 /* A structure we use to avoid passing large numbers of arguments. */
5469 struct elf_final_link_info
5471 /* General link information. */
5472 struct bfd_link_info *info;
5475 /* Symbol string table. */
5476 struct bfd_strtab_hash *symstrtab;
5477 /* .dynsym section. */
5478 asection *dynsym_sec;
5479 /* .hash section. */
5481 /* symbol version section (.gnu.version). */
5482 asection *symver_sec;
5483 /* Buffer large enough to hold contents of any section. */
5485 /* Buffer large enough to hold external relocs of any section. */
5486 void *external_relocs;
5487 /* Buffer large enough to hold internal relocs of any section. */
5488 Elf_Internal_Rela *internal_relocs;
5489 /* Buffer large enough to hold external local symbols of any input
5491 bfd_byte *external_syms;
5492 /* And a buffer for symbol section indices. */
5493 Elf_External_Sym_Shndx *locsym_shndx;
5494 /* Buffer large enough to hold internal local symbols of any input
5496 Elf_Internal_Sym *internal_syms;
5497 /* Array large enough to hold a symbol index for each local symbol
5498 of any input BFD. */
5500 /* Array large enough to hold a section pointer for each local
5501 symbol of any input BFD. */
5502 asection **sections;
5503 /* Buffer to hold swapped out symbols. */
5505 /* And one for symbol section indices. */
5506 Elf_External_Sym_Shndx *symshndxbuf;
5507 /* Number of swapped out symbols in buffer. */
5508 size_t symbuf_count;
5509 /* Number of symbols which fit in symbuf. */
5511 /* And same for symshndxbuf. */
5512 size_t shndxbuf_size;
5515 /* This struct is used to pass information to elf_link_output_extsym. */
5517 struct elf_outext_info
5520 bfd_boolean localsyms;
5521 struct elf_final_link_info *finfo;
5524 /* When performing a relocatable link, the input relocations are
5525 preserved. But, if they reference global symbols, the indices
5526 referenced must be updated. Update all the relocations in
5527 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5530 elf_link_adjust_relocs (bfd *abfd,
5531 Elf_Internal_Shdr *rel_hdr,
5533 struct elf_link_hash_entry **rel_hash)
5536 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5538 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5539 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5540 bfd_vma r_type_mask;
5543 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5545 swap_in = bed->s->swap_reloc_in;
5546 swap_out = bed->s->swap_reloc_out;
5548 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5550 swap_in = bed->s->swap_reloca_in;
5551 swap_out = bed->s->swap_reloca_out;
5556 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5559 if (bed->s->arch_size == 32)
5566 r_type_mask = 0xffffffff;
5570 erela = rel_hdr->contents;
5571 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5573 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5576 if (*rel_hash == NULL)
5579 BFD_ASSERT ((*rel_hash)->indx >= 0);
5581 (*swap_in) (abfd, erela, irela);
5582 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5583 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5584 | (irela[j].r_info & r_type_mask));
5585 (*swap_out) (abfd, irela, erela);
5589 struct elf_link_sort_rela
5595 enum elf_reloc_type_class type;
5596 /* We use this as an array of size int_rels_per_ext_rel. */
5597 Elf_Internal_Rela rela[1];
5601 elf_link_sort_cmp1 (const void *A, const void *B)
5603 const struct elf_link_sort_rela *a = A;
5604 const struct elf_link_sort_rela *b = B;
5605 int relativea, relativeb;
5607 relativea = a->type == reloc_class_relative;
5608 relativeb = b->type == reloc_class_relative;
5610 if (relativea < relativeb)
5612 if (relativea > relativeb)
5614 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5616 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5618 if (a->rela->r_offset < b->rela->r_offset)
5620 if (a->rela->r_offset > b->rela->r_offset)
5626 elf_link_sort_cmp2 (const void *A, const void *B)
5628 const struct elf_link_sort_rela *a = A;
5629 const struct elf_link_sort_rela *b = B;
5632 if (a->u.offset < b->u.offset)
5634 if (a->u.offset > b->u.offset)
5636 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5637 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5642 if (a->rela->r_offset < b->rela->r_offset)
5644 if (a->rela->r_offset > b->rela->r_offset)
5650 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5653 bfd_size_type count, size;
5654 size_t i, ret, sort_elt, ext_size;
5655 bfd_byte *sort, *s_non_relative, *p;
5656 struct elf_link_sort_rela *sq;
5657 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5658 int i2e = bed->s->int_rels_per_ext_rel;
5659 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5660 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5661 struct bfd_link_order *lo;
5664 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5665 if (reldyn == NULL || reldyn->size == 0)
5667 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5668 if (reldyn == NULL || reldyn->size == 0)
5670 ext_size = bed->s->sizeof_rel;
5671 swap_in = bed->s->swap_reloc_in;
5672 swap_out = bed->s->swap_reloc_out;
5676 ext_size = bed->s->sizeof_rela;
5677 swap_in = bed->s->swap_reloca_in;
5678 swap_out = bed->s->swap_reloca_out;
5680 count = reldyn->size / ext_size;
5683 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5684 if (lo->type == bfd_indirect_link_order)
5686 asection *o = lo->u.indirect.section;
5690 if (size != reldyn->size)
5693 sort_elt = (sizeof (struct elf_link_sort_rela)
5694 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5695 sort = bfd_zmalloc (sort_elt * count);
5698 (*info->callbacks->warning)
5699 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5703 if (bed->s->arch_size == 32)
5704 r_sym_mask = ~(bfd_vma) 0xff;
5706 r_sym_mask = ~(bfd_vma) 0xffffffff;
5708 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5709 if (lo->type == bfd_indirect_link_order)
5711 bfd_byte *erel, *erelend;
5712 asection *o = lo->u.indirect.section;
5714 if (o->contents == NULL && o->size != 0)
5716 /* This is a reloc section that is being handled as a normal
5717 section. See bfd_section_from_shdr. We can't combine
5718 relocs in this case. */
5723 erelend = o->contents + o->size;
5724 p = sort + o->output_offset / ext_size * sort_elt;
5725 while (erel < erelend)
5727 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5728 (*swap_in) (abfd, erel, s->rela);
5729 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
5730 s->u.sym_mask = r_sym_mask;
5736 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
5738 for (i = 0, p = sort; i < count; i++, p += sort_elt)
5740 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5741 if (s->type != reloc_class_relative)
5747 sq = (struct elf_link_sort_rela *) s_non_relative;
5748 for (; i < count; i++, p += sort_elt)
5750 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
5751 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
5753 sp->u.offset = sq->rela->r_offset;
5756 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
5758 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5759 if (lo->type == bfd_indirect_link_order)
5761 bfd_byte *erel, *erelend;
5762 asection *o = lo->u.indirect.section;
5765 erelend = o->contents + o->size;
5766 p = sort + o->output_offset / ext_size * sort_elt;
5767 while (erel < erelend)
5769 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5770 (*swap_out) (abfd, s->rela, erel);
5781 /* Flush the output symbols to the file. */
5784 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
5785 const struct elf_backend_data *bed)
5787 if (finfo->symbuf_count > 0)
5789 Elf_Internal_Shdr *hdr;
5793 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5794 pos = hdr->sh_offset + hdr->sh_size;
5795 amt = finfo->symbuf_count * bed->s->sizeof_sym;
5796 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5797 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
5800 hdr->sh_size += amt;
5801 finfo->symbuf_count = 0;
5807 /* Add a symbol to the output symbol table. */
5810 elf_link_output_sym (struct elf_final_link_info *finfo,
5812 Elf_Internal_Sym *elfsym,
5813 asection *input_sec,
5814 struct elf_link_hash_entry *h)
5817 Elf_External_Sym_Shndx *destshndx;
5818 bfd_boolean (*output_symbol_hook)
5819 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
5820 struct elf_link_hash_entry *);
5821 const struct elf_backend_data *bed;
5823 bed = get_elf_backend_data (finfo->output_bfd);
5824 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
5825 if (output_symbol_hook != NULL)
5827 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
5831 if (name == NULL || *name == '\0')
5832 elfsym->st_name = 0;
5833 else if (input_sec->flags & SEC_EXCLUDE)
5834 elfsym->st_name = 0;
5837 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5839 if (elfsym->st_name == (unsigned long) -1)
5843 if (finfo->symbuf_count >= finfo->symbuf_size)
5845 if (! elf_link_flush_output_syms (finfo, bed))
5849 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
5850 destshndx = finfo->symshndxbuf;
5851 if (destshndx != NULL)
5853 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
5857 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
5858 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
5859 if (destshndx == NULL)
5861 memset ((char *) destshndx + amt, 0, amt);
5862 finfo->shndxbuf_size *= 2;
5864 destshndx += bfd_get_symcount (finfo->output_bfd);
5867 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
5868 finfo->symbuf_count += 1;
5869 bfd_get_symcount (finfo->output_bfd) += 1;
5874 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5875 allowing an unsatisfied unversioned symbol in the DSO to match a
5876 versioned symbol that would normally require an explicit version.
5877 We also handle the case that a DSO references a hidden symbol
5878 which may be satisfied by a versioned symbol in another DSO. */
5881 elf_link_check_versioned_symbol (struct bfd_link_info *info,
5882 const struct elf_backend_data *bed,
5883 struct elf_link_hash_entry *h)
5886 struct elf_link_loaded_list *loaded;
5888 if (!is_elf_hash_table (info->hash))
5891 switch (h->root.type)
5897 case bfd_link_hash_undefined:
5898 case bfd_link_hash_undefweak:
5899 abfd = h->root.u.undef.abfd;
5900 if ((abfd->flags & DYNAMIC) == 0
5901 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
5905 case bfd_link_hash_defined:
5906 case bfd_link_hash_defweak:
5907 abfd = h->root.u.def.section->owner;
5910 case bfd_link_hash_common:
5911 abfd = h->root.u.c.p->section->owner;
5914 BFD_ASSERT (abfd != NULL);
5916 for (loaded = elf_hash_table (info)->loaded;
5918 loaded = loaded->next)
5921 Elf_Internal_Shdr *hdr;
5922 bfd_size_type symcount;
5923 bfd_size_type extsymcount;
5924 bfd_size_type extsymoff;
5925 Elf_Internal_Shdr *versymhdr;
5926 Elf_Internal_Sym *isym;
5927 Elf_Internal_Sym *isymend;
5928 Elf_Internal_Sym *isymbuf;
5929 Elf_External_Versym *ever;
5930 Elf_External_Versym *extversym;
5932 input = loaded->abfd;
5934 /* We check each DSO for a possible hidden versioned definition. */
5936 || (input->flags & DYNAMIC) == 0
5937 || elf_dynversym (input) == 0)
5940 hdr = &elf_tdata (input)->dynsymtab_hdr;
5942 symcount = hdr->sh_size / bed->s->sizeof_sym;
5943 if (elf_bad_symtab (input))
5945 extsymcount = symcount;
5950 extsymcount = symcount - hdr->sh_info;
5951 extsymoff = hdr->sh_info;
5954 if (extsymcount == 0)
5957 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
5959 if (isymbuf == NULL)
5962 /* Read in any version definitions. */
5963 versymhdr = &elf_tdata (input)->dynversym_hdr;
5964 extversym = bfd_malloc (versymhdr->sh_size);
5965 if (extversym == NULL)
5968 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
5969 || (bfd_bread (extversym, versymhdr->sh_size, input)
5970 != versymhdr->sh_size))
5978 ever = extversym + extsymoff;
5979 isymend = isymbuf + extsymcount;
5980 for (isym = isymbuf; isym < isymend; isym++, ever++)
5983 Elf_Internal_Versym iver;
5984 unsigned short version_index;
5986 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
5987 || isym->st_shndx == SHN_UNDEF)
5990 name = bfd_elf_string_from_elf_section (input,
5993 if (strcmp (name, h->root.root.string) != 0)
5996 _bfd_elf_swap_versym_in (input, ever, &iver);
5998 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6000 /* If we have a non-hidden versioned sym, then it should
6001 have provided a definition for the undefined sym. */
6005 version_index = iver.vs_vers & VERSYM_VERSION;
6006 if (version_index == 1 || version_index == 2)
6008 /* This is the base or first version. We can use it. */
6022 /* Add an external symbol to the symbol table. This is called from
6023 the hash table traversal routine. When generating a shared object,
6024 we go through the symbol table twice. The first time we output
6025 anything that might have been forced to local scope in a version
6026 script. The second time we output the symbols that are still
6030 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6032 struct elf_outext_info *eoinfo = data;
6033 struct elf_final_link_info *finfo = eoinfo->finfo;
6035 Elf_Internal_Sym sym;
6036 asection *input_sec;
6037 const struct elf_backend_data *bed;
6039 if (h->root.type == bfd_link_hash_warning)
6041 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6042 if (h->root.type == bfd_link_hash_new)
6046 /* Decide whether to output this symbol in this pass. */
6047 if (eoinfo->localsyms)
6049 if (!h->forced_local)
6054 if (h->forced_local)
6058 bed = get_elf_backend_data (finfo->output_bfd);
6060 /* If we have an undefined symbol reference here then it must have
6061 come from a shared library that is being linked in. (Undefined
6062 references in regular files have already been handled). If we
6063 are reporting errors for this situation then do so now. */
6064 if (h->root.type == bfd_link_hash_undefined
6067 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6068 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6070 if (! ((*finfo->info->callbacks->undefined_symbol)
6071 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6072 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6074 eoinfo->failed = TRUE;
6079 /* We should also warn if a forced local symbol is referenced from
6080 shared libraries. */
6081 if (! finfo->info->relocatable
6082 && (! finfo->info->shared)
6087 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6089 (*_bfd_error_handler)
6090 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6091 finfo->output_bfd, h->root.u.def.section->owner,
6092 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6094 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6095 ? "hidden" : "local",
6096 h->root.root.string);
6097 eoinfo->failed = TRUE;
6101 /* We don't want to output symbols that have never been mentioned by
6102 a regular file, or that we have been told to strip. However, if
6103 h->indx is set to -2, the symbol is used by a reloc and we must
6107 else if ((h->def_dynamic
6112 else if (finfo->info->strip == strip_all)
6114 else if (finfo->info->strip == strip_some
6115 && bfd_hash_lookup (finfo->info->keep_hash,
6116 h->root.root.string, FALSE, FALSE) == NULL)
6118 else if (finfo->info->strip_discarded
6119 && (h->root.type == bfd_link_hash_defined
6120 || h->root.type == bfd_link_hash_defweak)
6121 && elf_discarded_section (h->root.u.def.section))
6126 /* If we're stripping it, and it's not a dynamic symbol, there's
6127 nothing else to do unless it is a forced local symbol. */
6130 && !h->forced_local)
6134 sym.st_size = h->size;
6135 sym.st_other = h->other;
6136 if (h->forced_local)
6137 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6138 else if (h->root.type == bfd_link_hash_undefweak
6139 || h->root.type == bfd_link_hash_defweak)
6140 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6142 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6144 switch (h->root.type)
6147 case bfd_link_hash_new:
6148 case bfd_link_hash_warning:
6152 case bfd_link_hash_undefined:
6153 case bfd_link_hash_undefweak:
6154 input_sec = bfd_und_section_ptr;
6155 sym.st_shndx = SHN_UNDEF;
6158 case bfd_link_hash_defined:
6159 case bfd_link_hash_defweak:
6161 input_sec = h->root.u.def.section;
6162 if (input_sec->output_section != NULL)
6165 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6166 input_sec->output_section);
6167 if (sym.st_shndx == SHN_BAD)
6169 (*_bfd_error_handler)
6170 (_("%B: could not find output section %A for input section %A"),
6171 finfo->output_bfd, input_sec->output_section, input_sec);
6172 eoinfo->failed = TRUE;
6176 /* ELF symbols in relocatable files are section relative,
6177 but in nonrelocatable files they are virtual
6179 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6180 if (! finfo->info->relocatable)
6182 sym.st_value += input_sec->output_section->vma;
6183 if (h->type == STT_TLS)
6185 /* STT_TLS symbols are relative to PT_TLS segment
6187 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6188 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6194 BFD_ASSERT (input_sec->owner == NULL
6195 || (input_sec->owner->flags & DYNAMIC) != 0);
6196 sym.st_shndx = SHN_UNDEF;
6197 input_sec = bfd_und_section_ptr;
6202 case bfd_link_hash_common:
6203 input_sec = h->root.u.c.p->section;
6204 sym.st_shndx = SHN_COMMON;
6205 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6208 case bfd_link_hash_indirect:
6209 /* These symbols are created by symbol versioning. They point
6210 to the decorated version of the name. For example, if the
6211 symbol foo@@GNU_1.2 is the default, which should be used when
6212 foo is used with no version, then we add an indirect symbol
6213 foo which points to foo@@GNU_1.2. We ignore these symbols,
6214 since the indirected symbol is already in the hash table. */
6218 /* Give the processor backend a chance to tweak the symbol value,
6219 and also to finish up anything that needs to be done for this
6220 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6221 forced local syms when non-shared is due to a historical quirk. */
6222 if ((h->dynindx != -1
6224 && ((finfo->info->shared
6225 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6226 || h->root.type != bfd_link_hash_undefweak))
6227 || !h->forced_local)
6228 && elf_hash_table (finfo->info)->dynamic_sections_created)
6230 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6231 (finfo->output_bfd, finfo->info, h, &sym)))
6233 eoinfo->failed = TRUE;
6238 /* If we are marking the symbol as undefined, and there are no
6239 non-weak references to this symbol from a regular object, then
6240 mark the symbol as weak undefined; if there are non-weak
6241 references, mark the symbol as strong. We can't do this earlier,
6242 because it might not be marked as undefined until the
6243 finish_dynamic_symbol routine gets through with it. */
6244 if (sym.st_shndx == SHN_UNDEF
6246 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6247 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6251 if (h->ref_regular_nonweak)
6252 bindtype = STB_GLOBAL;
6254 bindtype = STB_WEAK;
6255 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6258 /* If a non-weak symbol with non-default visibility is not defined
6259 locally, it is a fatal error. */
6260 if (! finfo->info->relocatable
6261 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6262 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6263 && h->root.type == bfd_link_hash_undefined
6266 (*_bfd_error_handler)
6267 (_("%B: %s symbol `%s' isn't defined"),
6269 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6271 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6272 ? "internal" : "hidden",
6273 h->root.root.string);
6274 eoinfo->failed = TRUE;
6278 /* If this symbol should be put in the .dynsym section, then put it
6279 there now. We already know the symbol index. We also fill in
6280 the entry in the .hash section. */
6281 if (h->dynindx != -1
6282 && elf_hash_table (finfo->info)->dynamic_sections_created)
6286 size_t hash_entry_size;
6287 bfd_byte *bucketpos;
6291 sym.st_name = h->dynstr_index;
6292 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6293 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6295 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6296 bucket = h->u.elf_hash_value % bucketcount;
6298 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6299 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6300 + (bucket + 2) * hash_entry_size);
6301 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6302 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6303 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6304 ((bfd_byte *) finfo->hash_sec->contents
6305 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6307 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6309 Elf_Internal_Versym iversym;
6310 Elf_External_Versym *eversym;
6312 if (!h->def_regular)
6314 if (h->verinfo.verdef == NULL)
6315 iversym.vs_vers = 0;
6317 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6321 if (h->verinfo.vertree == NULL)
6322 iversym.vs_vers = 1;
6324 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6328 iversym.vs_vers |= VERSYM_HIDDEN;
6330 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6331 eversym += h->dynindx;
6332 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6336 /* If we're stripping it, then it was just a dynamic symbol, and
6337 there's nothing else to do. */
6338 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6341 h->indx = bfd_get_symcount (finfo->output_bfd);
6343 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6345 eoinfo->failed = TRUE;
6352 /* Return TRUE if special handling is done for relocs in SEC against
6353 symbols defined in discarded sections. */
6356 elf_section_ignore_discarded_relocs (asection *sec)
6358 const struct elf_backend_data *bed;
6360 switch (sec->sec_info_type)
6362 case ELF_INFO_TYPE_STABS:
6363 case ELF_INFO_TYPE_EH_FRAME:
6369 bed = get_elf_backend_data (sec->owner);
6370 if (bed->elf_backend_ignore_discarded_relocs != NULL
6371 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6377 /* Return TRUE if we should complain about a reloc in SEC against a
6378 symbol defined in a discarded section. */
6381 elf_section_complain_discarded (asection *sec)
6383 if (strncmp (".stab", sec->name, 5) == 0
6384 && (!sec->name[5] ||
6385 (sec->name[5] == '.' && ISDIGIT (sec->name[6]))))
6388 if (strcmp (".eh_frame", sec->name) == 0)
6391 if (strcmp (".gcc_except_table", sec->name) == 0)
6394 if (strcmp (".PARISC.unwind", sec->name) == 0)
6397 if (strcmp (".fixup", sec->name) == 0)
6403 /* Find a match between a section and a member of a section group. */
6406 match_group_member (asection *sec, asection *group)
6408 asection *first = elf_next_in_group (group);
6409 asection *s = first;
6413 if (bfd_elf_match_symbols_in_sections (s, sec))
6423 /* Link an input file into the linker output file. This function
6424 handles all the sections and relocations of the input file at once.
6425 This is so that we only have to read the local symbols once, and
6426 don't have to keep them in memory. */
6429 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6431 bfd_boolean (*relocate_section)
6432 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6433 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6435 Elf_Internal_Shdr *symtab_hdr;
6438 Elf_Internal_Sym *isymbuf;
6439 Elf_Internal_Sym *isym;
6440 Elf_Internal_Sym *isymend;
6442 asection **ppsection;
6444 const struct elf_backend_data *bed;
6445 bfd_boolean emit_relocs;
6446 struct elf_link_hash_entry **sym_hashes;
6448 output_bfd = finfo->output_bfd;
6449 bed = get_elf_backend_data (output_bfd);
6450 relocate_section = bed->elf_backend_relocate_section;
6452 /* If this is a dynamic object, we don't want to do anything here:
6453 we don't want the local symbols, and we don't want the section
6455 if ((input_bfd->flags & DYNAMIC) != 0)
6458 emit_relocs = (finfo->info->relocatable
6459 || finfo->info->emitrelocations
6460 || bed->elf_backend_emit_relocs);
6462 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6463 if (elf_bad_symtab (input_bfd))
6465 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6470 locsymcount = symtab_hdr->sh_info;
6471 extsymoff = symtab_hdr->sh_info;
6474 /* Read the local symbols. */
6475 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6476 if (isymbuf == NULL && locsymcount != 0)
6478 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6479 finfo->internal_syms,
6480 finfo->external_syms,
6481 finfo->locsym_shndx);
6482 if (isymbuf == NULL)
6486 /* Find local symbol sections and adjust values of symbols in
6487 SEC_MERGE sections. Write out those local symbols we know are
6488 going into the output file. */
6489 isymend = isymbuf + locsymcount;
6490 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6492 isym++, pindex++, ppsection++)
6496 Elf_Internal_Sym osym;
6500 if (elf_bad_symtab (input_bfd))
6502 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6509 if (isym->st_shndx == SHN_UNDEF)
6510 isec = bfd_und_section_ptr;
6511 else if (isym->st_shndx < SHN_LORESERVE
6512 || isym->st_shndx > SHN_HIRESERVE)
6514 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6516 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6517 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6519 _bfd_merged_section_offset (output_bfd, &isec,
6520 elf_section_data (isec)->sec_info,
6523 else if (isym->st_shndx == SHN_ABS)
6524 isec = bfd_abs_section_ptr;
6525 else if (isym->st_shndx == SHN_COMMON)
6526 isec = bfd_com_section_ptr;
6535 /* Don't output the first, undefined, symbol. */
6536 if (ppsection == finfo->sections)
6539 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6541 /* We never output section symbols. Instead, we use the
6542 section symbol of the corresponding section in the output
6547 /* If we are stripping all symbols, we don't want to output this
6549 if (finfo->info->strip == strip_all)
6552 /* If we are discarding all local symbols, we don't want to
6553 output this one. If we are generating a relocatable output
6554 file, then some of the local symbols may be required by
6555 relocs; we output them below as we discover that they are
6557 if (finfo->info->discard == discard_all)
6560 /* If this symbol is defined in a section which we are
6561 discarding, we don't need to keep it, but note that
6562 linker_mark is only reliable for sections that have contents.
6563 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6564 as well as linker_mark. */
6565 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6567 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6568 || (! finfo->info->relocatable
6569 && (isec->flags & SEC_EXCLUDE) != 0)))
6572 /* Get the name of the symbol. */
6573 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6578 /* See if we are discarding symbols with this name. */
6579 if ((finfo->info->strip == strip_some
6580 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6582 || (((finfo->info->discard == discard_sec_merge
6583 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6584 || finfo->info->discard == discard_l)
6585 && bfd_is_local_label_name (input_bfd, name)))
6588 /* If we get here, we are going to output this symbol. */
6592 /* Adjust the section index for the output file. */
6593 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6594 isec->output_section);
6595 if (osym.st_shndx == SHN_BAD)
6598 *pindex = bfd_get_symcount (output_bfd);
6600 /* ELF symbols in relocatable files are section relative, but
6601 in executable files they are virtual addresses. Note that
6602 this code assumes that all ELF sections have an associated
6603 BFD section with a reasonable value for output_offset; below
6604 we assume that they also have a reasonable value for
6605 output_section. Any special sections must be set up to meet
6606 these requirements. */
6607 osym.st_value += isec->output_offset;
6608 if (! finfo->info->relocatable)
6610 osym.st_value += isec->output_section->vma;
6611 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6613 /* STT_TLS symbols are relative to PT_TLS segment base. */
6614 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6615 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6619 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6623 /* Relocate the contents of each section. */
6624 sym_hashes = elf_sym_hashes (input_bfd);
6625 for (o = input_bfd->sections; o != NULL; o = o->next)
6629 if (! o->linker_mark)
6631 /* This section was omitted from the link. */
6635 if ((o->flags & SEC_HAS_CONTENTS) == 0
6636 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6639 if ((o->flags & SEC_LINKER_CREATED) != 0)
6641 /* Section was created by _bfd_elf_link_create_dynamic_sections
6646 /* Get the contents of the section. They have been cached by a
6647 relaxation routine. Note that o is a section in an input
6648 file, so the contents field will not have been set by any of
6649 the routines which work on output files. */
6650 if (elf_section_data (o)->this_hdr.contents != NULL)
6651 contents = elf_section_data (o)->this_hdr.contents;
6654 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6656 contents = finfo->contents;
6657 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6661 if ((o->flags & SEC_RELOC) != 0)
6663 Elf_Internal_Rela *internal_relocs;
6664 bfd_vma r_type_mask;
6667 /* Get the swapped relocs. */
6669 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6670 finfo->internal_relocs, FALSE);
6671 if (internal_relocs == NULL
6672 && o->reloc_count > 0)
6675 if (bed->s->arch_size == 32)
6682 r_type_mask = 0xffffffff;
6686 /* Run through the relocs looking for any against symbols
6687 from discarded sections and section symbols from
6688 removed link-once sections. Complain about relocs
6689 against discarded sections. Zero relocs against removed
6690 link-once sections. Preserve debug information as much
6692 if (!elf_section_ignore_discarded_relocs (o))
6694 Elf_Internal_Rela *rel, *relend;
6695 bfd_boolean complain = elf_section_complain_discarded (o);
6697 rel = internal_relocs;
6698 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6699 for ( ; rel < relend; rel++)
6701 unsigned long r_symndx = rel->r_info >> r_sym_shift;
6702 asection **ps, *sec;
6703 struct elf_link_hash_entry *h = NULL;
6704 const char *sym_name;
6706 if (r_symndx == STN_UNDEF)
6709 if (r_symndx >= locsymcount
6710 || (elf_bad_symtab (input_bfd)
6711 && finfo->sections[r_symndx] == NULL))
6713 h = sym_hashes[r_symndx - extsymoff];
6714 while (h->root.type == bfd_link_hash_indirect
6715 || h->root.type == bfd_link_hash_warning)
6716 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6718 if (h->root.type != bfd_link_hash_defined
6719 && h->root.type != bfd_link_hash_defweak)
6722 ps = &h->root.u.def.section;
6723 sym_name = h->root.root.string;
6727 Elf_Internal_Sym *sym = isymbuf + r_symndx;
6728 ps = &finfo->sections[r_symndx];
6729 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
6732 /* Complain if the definition comes from a
6733 discarded section. */
6734 if ((sec = *ps) != NULL && elf_discarded_section (sec))
6736 if ((o->flags & SEC_DEBUGGING) != 0)
6738 BFD_ASSERT (r_symndx != 0);
6740 /* Try to preserve debug information.
6741 FIXME: This is quite broken. Modifying
6742 the symbol here means we will be changing
6743 all uses of the symbol, not just those in
6744 debug sections. The only thing that makes
6745 this half reasonable is that debug sections
6746 tend to come after other sections. Of
6747 course, that doesn't help with globals.
6748 ??? All link-once sections of the same name
6749 ought to define the same set of symbols, so
6750 it would seem that globals ought to always
6751 be defined in the kept section. */
6752 if (sec->kept_section != NULL)
6756 /* Check if it is a linkonce section or
6757 member of a comdat group. */
6758 if (elf_sec_group (sec) == NULL
6759 && sec->size == sec->kept_section->size)
6761 *ps = sec->kept_section;
6764 else if (elf_sec_group (sec) != NULL
6765 && (member = match_group_member (sec, sec->kept_section))
6766 && sec->size == member->size)
6775 (*_bfd_error_handler)
6776 (_("`%s' referenced in section `%A' of %B: "
6777 "defined in discarded section `%A' of %B\n"),
6778 o, input_bfd, sec, sec->owner, sym_name);
6781 /* Remove the symbol reference from the reloc, but
6782 don't kill the reloc completely. This is so that
6783 a zero value will be written into the section,
6784 which may have non-zero contents put there by the
6785 assembler. Zero in things like an eh_frame fde
6786 pc_begin allows stack unwinders to recognize the
6788 rel->r_info &= r_type_mask;
6794 /* Relocate the section by invoking a back end routine.
6796 The back end routine is responsible for adjusting the
6797 section contents as necessary, and (if using Rela relocs
6798 and generating a relocatable output file) adjusting the
6799 reloc addend as necessary.
6801 The back end routine does not have to worry about setting
6802 the reloc address or the reloc symbol index.
6804 The back end routine is given a pointer to the swapped in
6805 internal symbols, and can access the hash table entries
6806 for the external symbols via elf_sym_hashes (input_bfd).
6808 When generating relocatable output, the back end routine
6809 must handle STB_LOCAL/STT_SECTION symbols specially. The
6810 output symbol is going to be a section symbol
6811 corresponding to the output section, which will require
6812 the addend to be adjusted. */
6814 if (! (*relocate_section) (output_bfd, finfo->info,
6815 input_bfd, o, contents,
6823 Elf_Internal_Rela *irela;
6824 Elf_Internal_Rela *irelaend;
6825 bfd_vma last_offset;
6826 struct elf_link_hash_entry **rel_hash;
6827 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
6828 unsigned int next_erel;
6829 bfd_boolean (*reloc_emitter)
6830 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
6831 bfd_boolean rela_normal;
6833 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6834 rela_normal = (bed->rela_normal
6835 && (input_rel_hdr->sh_entsize
6836 == bed->s->sizeof_rela));
6838 /* Adjust the reloc addresses and symbol indices. */
6840 irela = internal_relocs;
6841 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6842 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6843 + elf_section_data (o->output_section)->rel_count
6844 + elf_section_data (o->output_section)->rel_count2);
6845 last_offset = o->output_offset;
6846 if (!finfo->info->relocatable)
6847 last_offset += o->output_section->vma;
6848 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6850 unsigned long r_symndx;
6852 Elf_Internal_Sym sym;
6854 if (next_erel == bed->s->int_rels_per_ext_rel)
6860 irela->r_offset = _bfd_elf_section_offset (output_bfd,
6863 if (irela->r_offset >= (bfd_vma) -2)
6865 /* This is a reloc for a deleted entry or somesuch.
6866 Turn it into an R_*_NONE reloc, at the same
6867 offset as the last reloc. elf_eh_frame.c and
6868 elf_bfd_discard_info rely on reloc offsets
6870 irela->r_offset = last_offset;
6872 irela->r_addend = 0;
6876 irela->r_offset += o->output_offset;
6878 /* Relocs in an executable have to be virtual addresses. */
6879 if (!finfo->info->relocatable)
6880 irela->r_offset += o->output_section->vma;
6882 last_offset = irela->r_offset;
6884 r_symndx = irela->r_info >> r_sym_shift;
6885 if (r_symndx == STN_UNDEF)
6888 if (r_symndx >= locsymcount
6889 || (elf_bad_symtab (input_bfd)
6890 && finfo->sections[r_symndx] == NULL))
6892 struct elf_link_hash_entry *rh;
6895 /* This is a reloc against a global symbol. We
6896 have not yet output all the local symbols, so
6897 we do not know the symbol index of any global
6898 symbol. We set the rel_hash entry for this
6899 reloc to point to the global hash table entry
6900 for this symbol. The symbol index is then
6901 set at the end of bfd_elf_final_link. */
6902 indx = r_symndx - extsymoff;
6903 rh = elf_sym_hashes (input_bfd)[indx];
6904 while (rh->root.type == bfd_link_hash_indirect
6905 || rh->root.type == bfd_link_hash_warning)
6906 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6908 /* Setting the index to -2 tells
6909 elf_link_output_extsym that this symbol is
6911 BFD_ASSERT (rh->indx < 0);
6919 /* This is a reloc against a local symbol. */
6922 sym = isymbuf[r_symndx];
6923 sec = finfo->sections[r_symndx];
6924 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
6926 /* I suppose the backend ought to fill in the
6927 section of any STT_SECTION symbol against a
6928 processor specific section. */
6930 if (bfd_is_abs_section (sec))
6932 else if (sec == NULL || sec->owner == NULL)
6934 bfd_set_error (bfd_error_bad_value);
6939 asection *osec = sec->output_section;
6941 /* If we have discarded a section, the output
6942 section will be the absolute section. In
6943 case of discarded link-once and discarded
6944 SEC_MERGE sections, use the kept section. */
6945 if (bfd_is_abs_section (osec)
6946 && sec->kept_section != NULL
6947 && sec->kept_section->output_section != NULL)
6949 osec = sec->kept_section->output_section;
6950 irela->r_addend -= osec->vma;
6953 if (!bfd_is_abs_section (osec))
6955 r_symndx = osec->target_index;
6956 BFD_ASSERT (r_symndx != 0);
6960 /* Adjust the addend according to where the
6961 section winds up in the output section. */
6963 irela->r_addend += sec->output_offset;
6967 if (finfo->indices[r_symndx] == -1)
6969 unsigned long shlink;
6973 if (finfo->info->strip == strip_all)
6975 /* You can't do ld -r -s. */
6976 bfd_set_error (bfd_error_invalid_operation);
6980 /* This symbol was skipped earlier, but
6981 since it is needed by a reloc, we
6982 must output it now. */
6983 shlink = symtab_hdr->sh_link;
6984 name = (bfd_elf_string_from_elf_section
6985 (input_bfd, shlink, sym.st_name));
6989 osec = sec->output_section;
6991 _bfd_elf_section_from_bfd_section (output_bfd,
6993 if (sym.st_shndx == SHN_BAD)
6996 sym.st_value += sec->output_offset;
6997 if (! finfo->info->relocatable)
6999 sym.st_value += osec->vma;
7000 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7002 /* STT_TLS symbols are relative to PT_TLS
7004 BFD_ASSERT (elf_hash_table (finfo->info)
7006 sym.st_value -= (elf_hash_table (finfo->info)
7011 finfo->indices[r_symndx]
7012 = bfd_get_symcount (output_bfd);
7014 if (! elf_link_output_sym (finfo, name, &sym, sec,
7019 r_symndx = finfo->indices[r_symndx];
7022 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7023 | (irela->r_info & r_type_mask));
7026 /* Swap out the relocs. */
7027 if (bed->elf_backend_emit_relocs
7028 && !(finfo->info->relocatable
7029 || finfo->info->emitrelocations))
7030 reloc_emitter = bed->elf_backend_emit_relocs;
7032 reloc_emitter = _bfd_elf_link_output_relocs;
7034 if (input_rel_hdr->sh_size != 0
7035 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
7039 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7040 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7042 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7043 * bed->s->int_rels_per_ext_rel);
7044 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
7051 /* Write out the modified section contents. */
7052 if (bed->elf_backend_write_section
7053 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7055 /* Section written out. */
7057 else switch (o->sec_info_type)
7059 case ELF_INFO_TYPE_STABS:
7060 if (! (_bfd_write_section_stabs
7062 &elf_hash_table (finfo->info)->stab_info,
7063 o, &elf_section_data (o)->sec_info, contents)))
7066 case ELF_INFO_TYPE_MERGE:
7067 if (! _bfd_write_merged_section (output_bfd, o,
7068 elf_section_data (o)->sec_info))
7071 case ELF_INFO_TYPE_EH_FRAME:
7073 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7080 if (! (o->flags & SEC_EXCLUDE)
7081 && ! bfd_set_section_contents (output_bfd, o->output_section,
7083 (file_ptr) o->output_offset,
7094 /* Generate a reloc when linking an ELF file. This is a reloc
7095 requested by the linker, and does come from any input file. This
7096 is used to build constructor and destructor tables when linking
7100 elf_reloc_link_order (bfd *output_bfd,
7101 struct bfd_link_info *info,
7102 asection *output_section,
7103 struct bfd_link_order *link_order)
7105 reloc_howto_type *howto;
7109 struct elf_link_hash_entry **rel_hash_ptr;
7110 Elf_Internal_Shdr *rel_hdr;
7111 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7112 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7116 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7119 bfd_set_error (bfd_error_bad_value);
7123 addend = link_order->u.reloc.p->addend;
7125 /* Figure out the symbol index. */
7126 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7127 + elf_section_data (output_section)->rel_count
7128 + elf_section_data (output_section)->rel_count2);
7129 if (link_order->type == bfd_section_reloc_link_order)
7131 indx = link_order->u.reloc.p->u.section->target_index;
7132 BFD_ASSERT (indx != 0);
7133 *rel_hash_ptr = NULL;
7137 struct elf_link_hash_entry *h;
7139 /* Treat a reloc against a defined symbol as though it were
7140 actually against the section. */
7141 h = ((struct elf_link_hash_entry *)
7142 bfd_wrapped_link_hash_lookup (output_bfd, info,
7143 link_order->u.reloc.p->u.name,
7144 FALSE, FALSE, TRUE));
7146 && (h->root.type == bfd_link_hash_defined
7147 || h->root.type == bfd_link_hash_defweak))
7151 section = h->root.u.def.section;
7152 indx = section->output_section->target_index;
7153 *rel_hash_ptr = NULL;
7154 /* It seems that we ought to add the symbol value to the
7155 addend here, but in practice it has already been added
7156 because it was passed to constructor_callback. */
7157 addend += section->output_section->vma + section->output_offset;
7161 /* Setting the index to -2 tells elf_link_output_extsym that
7162 this symbol is used by a reloc. */
7169 if (! ((*info->callbacks->unattached_reloc)
7170 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7176 /* If this is an inplace reloc, we must write the addend into the
7178 if (howto->partial_inplace && addend != 0)
7181 bfd_reloc_status_type rstat;
7184 const char *sym_name;
7186 size = bfd_get_reloc_size (howto);
7187 buf = bfd_zmalloc (size);
7190 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7197 case bfd_reloc_outofrange:
7200 case bfd_reloc_overflow:
7201 if (link_order->type == bfd_section_reloc_link_order)
7202 sym_name = bfd_section_name (output_bfd,
7203 link_order->u.reloc.p->u.section);
7205 sym_name = link_order->u.reloc.p->u.name;
7206 if (! ((*info->callbacks->reloc_overflow)
7207 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
7214 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7215 link_order->offset, size);
7221 /* The address of a reloc is relative to the section in a
7222 relocatable file, and is a virtual address in an executable
7224 offset = link_order->offset;
7225 if (! info->relocatable)
7226 offset += output_section->vma;
7228 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7230 irel[i].r_offset = offset;
7232 irel[i].r_addend = 0;
7234 if (bed->s->arch_size == 32)
7235 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7237 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7239 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7240 erel = rel_hdr->contents;
7241 if (rel_hdr->sh_type == SHT_REL)
7243 erel += (elf_section_data (output_section)->rel_count
7244 * bed->s->sizeof_rel);
7245 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7249 irel[0].r_addend = addend;
7250 erel += (elf_section_data (output_section)->rel_count
7251 * bed->s->sizeof_rela);
7252 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7255 ++elf_section_data (output_section)->rel_count;
7261 /* Get the output vma of the section pointed to by the sh_link field. */
7264 elf_get_linked_section_vma (struct bfd_link_order *p)
7266 Elf_Internal_Shdr **elf_shdrp;
7270 s = p->u.indirect.section;
7271 elf_shdrp = elf_elfsections (s->owner);
7272 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7273 elfsec = elf_shdrp[elfsec]->sh_link;
7275 The Intel C compiler generates SHT_IA_64_UNWIND with
7276 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7277 sh_info fields. Hence we could get the situation
7278 where elfsec is 0. */
7281 const struct elf_backend_data *bed
7282 = get_elf_backend_data (s->owner);
7283 if (bed->link_order_error_handler)
7284 bed->link_order_error_handler
7285 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7290 s = elf_shdrp[elfsec]->bfd_section;
7291 return s->output_section->vma + s->output_offset;
7296 /* Compare two sections based on the locations of the sections they are
7297 linked to. Used by elf_fixup_link_order. */
7300 compare_link_order (const void * a, const void * b)
7305 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7306 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7313 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7314 order as their linked sections. Returns false if this could not be done
7315 because an output section includes both ordered and unordered
7316 sections. Ideally we'd do this in the linker proper. */
7319 elf_fixup_link_order (bfd *abfd, asection *o)
7324 struct bfd_link_order *p;
7326 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7328 struct bfd_link_order **sections;
7334 for (p = o->link_order_head; p != NULL; p = p->next)
7336 if (p->type == bfd_indirect_link_order
7337 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7338 == bfd_target_elf_flavour)
7339 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7341 s = p->u.indirect.section;
7342 elfsec = _bfd_elf_section_from_bfd_section (sub, s);
7344 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7353 if (!seen_linkorder)
7356 if (seen_other && seen_linkorder)
7358 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7360 bfd_set_error (bfd_error_bad_value);
7364 sections = (struct bfd_link_order **)
7365 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7368 for (p = o->link_order_head; p != NULL; p = p->next)
7370 sections[seen_linkorder++] = p;
7372 /* Sort the input sections in the order of their linked section. */
7373 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7374 compare_link_order);
7376 /* Change the offsets of the sections. */
7378 for (n = 0; n < seen_linkorder; n++)
7380 s = sections[n]->u.indirect.section;
7381 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7382 s->output_offset = offset;
7383 sections[n]->offset = offset;
7384 offset += sections[n]->size;
7391 /* Do the final step of an ELF link. */
7394 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7396 bfd_boolean dynamic;
7397 bfd_boolean emit_relocs;
7399 struct elf_final_link_info finfo;
7400 register asection *o;
7401 register struct bfd_link_order *p;
7403 bfd_size_type max_contents_size;
7404 bfd_size_type max_external_reloc_size;
7405 bfd_size_type max_internal_reloc_count;
7406 bfd_size_type max_sym_count;
7407 bfd_size_type max_sym_shndx_count;
7409 Elf_Internal_Sym elfsym;
7411 Elf_Internal_Shdr *symtab_hdr;
7412 Elf_Internal_Shdr *symtab_shndx_hdr;
7413 Elf_Internal_Shdr *symstrtab_hdr;
7414 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7415 struct elf_outext_info eoinfo;
7417 size_t relativecount = 0;
7418 asection *reldyn = 0;
7421 if (! is_elf_hash_table (info->hash))
7425 abfd->flags |= DYNAMIC;
7427 dynamic = elf_hash_table (info)->dynamic_sections_created;
7428 dynobj = elf_hash_table (info)->dynobj;
7430 emit_relocs = (info->relocatable
7431 || info->emitrelocations
7432 || bed->elf_backend_emit_relocs);
7435 finfo.output_bfd = abfd;
7436 finfo.symstrtab = _bfd_elf_stringtab_init ();
7437 if (finfo.symstrtab == NULL)
7442 finfo.dynsym_sec = NULL;
7443 finfo.hash_sec = NULL;
7444 finfo.symver_sec = NULL;
7448 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7449 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7450 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7451 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7452 /* Note that it is OK if symver_sec is NULL. */
7455 finfo.contents = NULL;
7456 finfo.external_relocs = NULL;
7457 finfo.internal_relocs = NULL;
7458 finfo.external_syms = NULL;
7459 finfo.locsym_shndx = NULL;
7460 finfo.internal_syms = NULL;
7461 finfo.indices = NULL;
7462 finfo.sections = NULL;
7463 finfo.symbuf = NULL;
7464 finfo.symshndxbuf = NULL;
7465 finfo.symbuf_count = 0;
7466 finfo.shndxbuf_size = 0;
7468 /* Count up the number of relocations we will output for each output
7469 section, so that we know the sizes of the reloc sections. We
7470 also figure out some maximum sizes. */
7471 max_contents_size = 0;
7472 max_external_reloc_size = 0;
7473 max_internal_reloc_count = 0;
7475 max_sym_shndx_count = 0;
7477 for (o = abfd->sections; o != NULL; o = o->next)
7479 struct bfd_elf_section_data *esdo = elf_section_data (o);
7482 for (p = o->link_order_head; p != NULL; p = p->next)
7484 unsigned int reloc_count = 0;
7485 struct bfd_elf_section_data *esdi = NULL;
7486 unsigned int *rel_count1;
7488 if (p->type == bfd_section_reloc_link_order
7489 || p->type == bfd_symbol_reloc_link_order)
7491 else if (p->type == bfd_indirect_link_order)
7495 sec = p->u.indirect.section;
7496 esdi = elf_section_data (sec);
7498 /* Mark all sections which are to be included in the
7499 link. This will normally be every section. We need
7500 to do this so that we can identify any sections which
7501 the linker has decided to not include. */
7502 sec->linker_mark = TRUE;
7504 if (sec->flags & SEC_MERGE)
7507 if (info->relocatable || info->emitrelocations)
7508 reloc_count = sec->reloc_count;
7509 else if (bed->elf_backend_count_relocs)
7511 Elf_Internal_Rela * relocs;
7513 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7516 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7518 if (elf_section_data (o)->relocs != relocs)
7522 if (sec->rawsize > max_contents_size)
7523 max_contents_size = sec->rawsize;
7524 if (sec->size > max_contents_size)
7525 max_contents_size = sec->size;
7527 /* We are interested in just local symbols, not all
7529 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7530 && (sec->owner->flags & DYNAMIC) == 0)
7534 if (elf_bad_symtab (sec->owner))
7535 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7536 / bed->s->sizeof_sym);
7538 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7540 if (sym_count > max_sym_count)
7541 max_sym_count = sym_count;
7543 if (sym_count > max_sym_shndx_count
7544 && elf_symtab_shndx (sec->owner) != 0)
7545 max_sym_shndx_count = sym_count;
7547 if ((sec->flags & SEC_RELOC) != 0)
7551 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7552 if (ext_size > max_external_reloc_size)
7553 max_external_reloc_size = ext_size;
7554 if (sec->reloc_count > max_internal_reloc_count)
7555 max_internal_reloc_count = sec->reloc_count;
7560 if (reloc_count == 0)
7563 o->reloc_count += reloc_count;
7565 /* MIPS may have a mix of REL and RELA relocs on sections.
7566 To support this curious ABI we keep reloc counts in
7567 elf_section_data too. We must be careful to add the
7568 relocations from the input section to the right output
7569 count. FIXME: Get rid of one count. We have
7570 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7571 rel_count1 = &esdo->rel_count;
7574 bfd_boolean same_size;
7575 bfd_size_type entsize1;
7577 entsize1 = esdi->rel_hdr.sh_entsize;
7578 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7579 || entsize1 == bed->s->sizeof_rela);
7580 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7583 rel_count1 = &esdo->rel_count2;
7585 if (esdi->rel_hdr2 != NULL)
7587 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7588 unsigned int alt_count;
7589 unsigned int *rel_count2;
7591 BFD_ASSERT (entsize2 != entsize1
7592 && (entsize2 == bed->s->sizeof_rel
7593 || entsize2 == bed->s->sizeof_rela));
7595 rel_count2 = &esdo->rel_count2;
7597 rel_count2 = &esdo->rel_count;
7599 /* The following is probably too simplistic if the
7600 backend counts output relocs unusually. */
7601 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7602 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7603 *rel_count2 += alt_count;
7604 reloc_count -= alt_count;
7607 *rel_count1 += reloc_count;
7610 if (o->reloc_count > 0)
7611 o->flags |= SEC_RELOC;
7614 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7615 set it (this is probably a bug) and if it is set
7616 assign_section_numbers will create a reloc section. */
7617 o->flags &=~ SEC_RELOC;
7620 /* If the SEC_ALLOC flag is not set, force the section VMA to
7621 zero. This is done in elf_fake_sections as well, but forcing
7622 the VMA to 0 here will ensure that relocs against these
7623 sections are handled correctly. */
7624 if ((o->flags & SEC_ALLOC) == 0
7625 && ! o->user_set_vma)
7629 if (! info->relocatable && merged)
7630 elf_link_hash_traverse (elf_hash_table (info),
7631 _bfd_elf_link_sec_merge_syms, abfd);
7633 /* Figure out the file positions for everything but the symbol table
7634 and the relocs. We set symcount to force assign_section_numbers
7635 to create a symbol table. */
7636 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7637 BFD_ASSERT (! abfd->output_has_begun);
7638 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7641 /* Set sizes, and assign file positions for reloc sections. */
7642 for (o = abfd->sections; o != NULL; o = o->next)
7644 if ((o->flags & SEC_RELOC) != 0)
7646 if (!(_bfd_elf_link_size_reloc_section
7647 (abfd, &elf_section_data (o)->rel_hdr, o)))
7650 if (elf_section_data (o)->rel_hdr2
7651 && !(_bfd_elf_link_size_reloc_section
7652 (abfd, elf_section_data (o)->rel_hdr2, o)))
7656 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7657 to count upwards while actually outputting the relocations. */
7658 elf_section_data (o)->rel_count = 0;
7659 elf_section_data (o)->rel_count2 = 0;
7662 _bfd_elf_assign_file_positions_for_relocs (abfd);
7664 /* We have now assigned file positions for all the sections except
7665 .symtab and .strtab. We start the .symtab section at the current
7666 file position, and write directly to it. We build the .strtab
7667 section in memory. */
7668 bfd_get_symcount (abfd) = 0;
7669 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7670 /* sh_name is set in prep_headers. */
7671 symtab_hdr->sh_type = SHT_SYMTAB;
7672 /* sh_flags, sh_addr and sh_size all start off zero. */
7673 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7674 /* sh_link is set in assign_section_numbers. */
7675 /* sh_info is set below. */
7676 /* sh_offset is set just below. */
7677 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
7679 off = elf_tdata (abfd)->next_file_pos;
7680 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
7682 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7683 incorrect. We do not yet know the size of the .symtab section.
7684 We correct next_file_pos below, after we do know the size. */
7686 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7687 continuously seeking to the right position in the file. */
7688 if (! info->keep_memory || max_sym_count < 20)
7689 finfo.symbuf_size = 20;
7691 finfo.symbuf_size = max_sym_count;
7692 amt = finfo.symbuf_size;
7693 amt *= bed->s->sizeof_sym;
7694 finfo.symbuf = bfd_malloc (amt);
7695 if (finfo.symbuf == NULL)
7697 if (elf_numsections (abfd) > SHN_LORESERVE)
7699 /* Wild guess at number of output symbols. realloc'd as needed. */
7700 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
7701 finfo.shndxbuf_size = amt;
7702 amt *= sizeof (Elf_External_Sym_Shndx);
7703 finfo.symshndxbuf = bfd_zmalloc (amt);
7704 if (finfo.symshndxbuf == NULL)
7708 /* Start writing out the symbol table. The first symbol is always a
7710 if (info->strip != strip_all
7713 elfsym.st_value = 0;
7716 elfsym.st_other = 0;
7717 elfsym.st_shndx = SHN_UNDEF;
7718 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
7724 /* Some standard ELF linkers do this, but we don't because it causes
7725 bootstrap comparison failures. */
7726 /* Output a file symbol for the output file as the second symbol.
7727 We output this even if we are discarding local symbols, although
7728 I'm not sure if this is correct. */
7729 elfsym.st_value = 0;
7731 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7732 elfsym.st_other = 0;
7733 elfsym.st_shndx = SHN_ABS;
7734 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
7735 &elfsym, bfd_abs_section_ptr, NULL))
7739 /* Output a symbol for each section. We output these even if we are
7740 discarding local symbols, since they are used for relocs. These
7741 symbols have no names. We store the index of each one in the
7742 index field of the section, so that we can find it again when
7743 outputting relocs. */
7744 if (info->strip != strip_all
7748 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7749 elfsym.st_other = 0;
7750 for (i = 1; i < elf_numsections (abfd); i++)
7752 o = bfd_section_from_elf_index (abfd, i);
7754 o->target_index = bfd_get_symcount (abfd);
7755 elfsym.st_shndx = i;
7756 if (info->relocatable || o == NULL)
7757 elfsym.st_value = 0;
7759 elfsym.st_value = o->vma;
7760 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
7762 if (i == SHN_LORESERVE - 1)
7763 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
7767 /* Allocate some memory to hold information read in from the input
7769 if (max_contents_size != 0)
7771 finfo.contents = bfd_malloc (max_contents_size);
7772 if (finfo.contents == NULL)
7776 if (max_external_reloc_size != 0)
7778 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
7779 if (finfo.external_relocs == NULL)
7783 if (max_internal_reloc_count != 0)
7785 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
7786 amt *= sizeof (Elf_Internal_Rela);
7787 finfo.internal_relocs = bfd_malloc (amt);
7788 if (finfo.internal_relocs == NULL)
7792 if (max_sym_count != 0)
7794 amt = max_sym_count * bed->s->sizeof_sym;
7795 finfo.external_syms = bfd_malloc (amt);
7796 if (finfo.external_syms == NULL)
7799 amt = max_sym_count * sizeof (Elf_Internal_Sym);
7800 finfo.internal_syms = bfd_malloc (amt);
7801 if (finfo.internal_syms == NULL)
7804 amt = max_sym_count * sizeof (long);
7805 finfo.indices = bfd_malloc (amt);
7806 if (finfo.indices == NULL)
7809 amt = max_sym_count * sizeof (asection *);
7810 finfo.sections = bfd_malloc (amt);
7811 if (finfo.sections == NULL)
7815 if (max_sym_shndx_count != 0)
7817 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
7818 finfo.locsym_shndx = bfd_malloc (amt);
7819 if (finfo.locsym_shndx == NULL)
7823 if (elf_hash_table (info)->tls_sec)
7825 bfd_vma base, end = 0;
7828 for (sec = elf_hash_table (info)->tls_sec;
7829 sec && (sec->flags & SEC_THREAD_LOCAL);
7832 bfd_vma size = sec->size;
7834 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
7836 struct bfd_link_order *o;
7838 for (o = sec->link_order_head; o != NULL; o = o->next)
7839 if (size < o->offset + o->size)
7840 size = o->offset + o->size;
7842 end = sec->vma + size;
7844 base = elf_hash_table (info)->tls_sec->vma;
7845 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
7846 elf_hash_table (info)->tls_size = end - base;
7849 /* Reorder SHF_LINK_ORDER sections. */
7850 for (o = abfd->sections; o != NULL; o = o->next)
7852 if (!elf_fixup_link_order (abfd, o))
7856 /* Since ELF permits relocations to be against local symbols, we
7857 must have the local symbols available when we do the relocations.
7858 Since we would rather only read the local symbols once, and we
7859 would rather not keep them in memory, we handle all the
7860 relocations for a single input file at the same time.
7862 Unfortunately, there is no way to know the total number of local
7863 symbols until we have seen all of them, and the local symbol
7864 indices precede the global symbol indices. This means that when
7865 we are generating relocatable output, and we see a reloc against
7866 a global symbol, we can not know the symbol index until we have
7867 finished examining all the local symbols to see which ones we are
7868 going to output. To deal with this, we keep the relocations in
7869 memory, and don't output them until the end of the link. This is
7870 an unfortunate waste of memory, but I don't see a good way around
7871 it. Fortunately, it only happens when performing a relocatable
7872 link, which is not the common case. FIXME: If keep_memory is set
7873 we could write the relocs out and then read them again; I don't
7874 know how bad the memory loss will be. */
7876 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7877 sub->output_has_begun = FALSE;
7878 for (o = abfd->sections; o != NULL; o = o->next)
7880 for (p = o->link_order_head; p != NULL; p = p->next)
7882 if (p->type == bfd_indirect_link_order
7883 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7884 == bfd_target_elf_flavour)
7885 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7887 if (! sub->output_has_begun)
7889 if (! elf_link_input_bfd (&finfo, sub))
7891 sub->output_has_begun = TRUE;
7894 else if (p->type == bfd_section_reloc_link_order
7895 || p->type == bfd_symbol_reloc_link_order)
7897 if (! elf_reloc_link_order (abfd, info, o, p))
7902 if (! _bfd_default_link_order (abfd, info, o, p))
7908 /* Output any global symbols that got converted to local in a
7909 version script or due to symbol visibility. We do this in a
7910 separate step since ELF requires all local symbols to appear
7911 prior to any global symbols. FIXME: We should only do this if
7912 some global symbols were, in fact, converted to become local.
7913 FIXME: Will this work correctly with the Irix 5 linker? */
7914 eoinfo.failed = FALSE;
7915 eoinfo.finfo = &finfo;
7916 eoinfo.localsyms = TRUE;
7917 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
7922 /* That wrote out all the local symbols. Finish up the symbol table
7923 with the global symbols. Even if we want to strip everything we
7924 can, we still need to deal with those global symbols that got
7925 converted to local in a version script. */
7927 /* The sh_info field records the index of the first non local symbol. */
7928 symtab_hdr->sh_info = bfd_get_symcount (abfd);
7931 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
7933 Elf_Internal_Sym sym;
7934 bfd_byte *dynsym = finfo.dynsym_sec->contents;
7935 long last_local = 0;
7937 /* Write out the section symbols for the output sections. */
7944 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7947 for (s = abfd->sections; s != NULL; s = s->next)
7953 dynindx = elf_section_data (s)->dynindx;
7956 indx = elf_section_data (s)->this_idx;
7957 BFD_ASSERT (indx > 0);
7958 sym.st_shndx = indx;
7959 sym.st_value = s->vma;
7960 dest = dynsym + dynindx * bed->s->sizeof_sym;
7961 if (last_local < dynindx)
7962 last_local = dynindx;
7963 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
7967 /* Write out the local dynsyms. */
7968 if (elf_hash_table (info)->dynlocal)
7970 struct elf_link_local_dynamic_entry *e;
7971 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
7976 sym.st_size = e->isym.st_size;
7977 sym.st_other = e->isym.st_other;
7979 /* Copy the internal symbol as is.
7980 Note that we saved a word of storage and overwrote
7981 the original st_name with the dynstr_index. */
7984 if (e->isym.st_shndx != SHN_UNDEF
7985 && (e->isym.st_shndx < SHN_LORESERVE
7986 || e->isym.st_shndx > SHN_HIRESERVE))
7988 s = bfd_section_from_elf_index (e->input_bfd,
7992 elf_section_data (s->output_section)->this_idx;
7993 sym.st_value = (s->output_section->vma
7995 + e->isym.st_value);
7998 if (last_local < e->dynindx)
7999 last_local = e->dynindx;
8001 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8002 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8006 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8010 /* We get the global symbols from the hash table. */
8011 eoinfo.failed = FALSE;
8012 eoinfo.localsyms = FALSE;
8013 eoinfo.finfo = &finfo;
8014 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8019 /* If backend needs to output some symbols not present in the hash
8020 table, do it now. */
8021 if (bed->elf_backend_output_arch_syms)
8023 typedef bfd_boolean (*out_sym_func)
8024 (void *, const char *, Elf_Internal_Sym *, asection *,
8025 struct elf_link_hash_entry *);
8027 if (! ((*bed->elf_backend_output_arch_syms)
8028 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8032 /* Flush all symbols to the file. */
8033 if (! elf_link_flush_output_syms (&finfo, bed))
8036 /* Now we know the size of the symtab section. */
8037 off += symtab_hdr->sh_size;
8039 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8040 if (symtab_shndx_hdr->sh_name != 0)
8042 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8043 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8044 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8045 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8046 symtab_shndx_hdr->sh_size = amt;
8048 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8051 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8052 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8057 /* Finish up and write out the symbol string table (.strtab)
8059 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8060 /* sh_name was set in prep_headers. */
8061 symstrtab_hdr->sh_type = SHT_STRTAB;
8062 symstrtab_hdr->sh_flags = 0;
8063 symstrtab_hdr->sh_addr = 0;
8064 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8065 symstrtab_hdr->sh_entsize = 0;
8066 symstrtab_hdr->sh_link = 0;
8067 symstrtab_hdr->sh_info = 0;
8068 /* sh_offset is set just below. */
8069 symstrtab_hdr->sh_addralign = 1;
8071 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8072 elf_tdata (abfd)->next_file_pos = off;
8074 if (bfd_get_symcount (abfd) > 0)
8076 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8077 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8081 /* Adjust the relocs to have the correct symbol indices. */
8082 for (o = abfd->sections; o != NULL; o = o->next)
8084 if ((o->flags & SEC_RELOC) == 0)
8087 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8088 elf_section_data (o)->rel_count,
8089 elf_section_data (o)->rel_hashes);
8090 if (elf_section_data (o)->rel_hdr2 != NULL)
8091 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8092 elf_section_data (o)->rel_count2,
8093 (elf_section_data (o)->rel_hashes
8094 + elf_section_data (o)->rel_count));
8096 /* Set the reloc_count field to 0 to prevent write_relocs from
8097 trying to swap the relocs out itself. */
8101 if (dynamic && info->combreloc && dynobj != NULL)
8102 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8104 /* If we are linking against a dynamic object, or generating a
8105 shared library, finish up the dynamic linking information. */
8108 bfd_byte *dyncon, *dynconend;
8110 /* Fix up .dynamic entries. */
8111 o = bfd_get_section_by_name (dynobj, ".dynamic");
8112 BFD_ASSERT (o != NULL);
8114 dyncon = o->contents;
8115 dynconend = o->contents + o->size;
8116 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8118 Elf_Internal_Dyn dyn;
8122 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8129 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8131 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8133 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8134 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8137 dyn.d_un.d_val = relativecount;
8144 name = info->init_function;
8147 name = info->fini_function;
8150 struct elf_link_hash_entry *h;
8152 h = elf_link_hash_lookup (elf_hash_table (info), name,
8153 FALSE, FALSE, TRUE);
8155 && (h->root.type == bfd_link_hash_defined
8156 || h->root.type == bfd_link_hash_defweak))
8158 dyn.d_un.d_val = h->root.u.def.value;
8159 o = h->root.u.def.section;
8160 if (o->output_section != NULL)
8161 dyn.d_un.d_val += (o->output_section->vma
8162 + o->output_offset);
8165 /* The symbol is imported from another shared
8166 library and does not apply to this one. */
8174 case DT_PREINIT_ARRAYSZ:
8175 name = ".preinit_array";
8177 case DT_INIT_ARRAYSZ:
8178 name = ".init_array";
8180 case DT_FINI_ARRAYSZ:
8181 name = ".fini_array";
8183 o = bfd_get_section_by_name (abfd, name);
8186 (*_bfd_error_handler)
8187 (_("%B: could not find output section %s"), abfd, name);
8191 (*_bfd_error_handler)
8192 (_("warning: %s section has zero size"), name);
8193 dyn.d_un.d_val = o->size;
8196 case DT_PREINIT_ARRAY:
8197 name = ".preinit_array";
8200 name = ".init_array";
8203 name = ".fini_array";
8216 name = ".gnu.version_d";
8219 name = ".gnu.version_r";
8222 name = ".gnu.version";
8224 o = bfd_get_section_by_name (abfd, name);
8227 (*_bfd_error_handler)
8228 (_("%B: could not find output section %s"), abfd, name);
8231 dyn.d_un.d_ptr = o->vma;
8238 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8243 for (i = 1; i < elf_numsections (abfd); i++)
8245 Elf_Internal_Shdr *hdr;
8247 hdr = elf_elfsections (abfd)[i];
8248 if (hdr->sh_type == type
8249 && (hdr->sh_flags & SHF_ALLOC) != 0)
8251 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8252 dyn.d_un.d_val += hdr->sh_size;
8255 if (dyn.d_un.d_val == 0
8256 || hdr->sh_addr < dyn.d_un.d_val)
8257 dyn.d_un.d_val = hdr->sh_addr;
8263 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8267 /* If we have created any dynamic sections, then output them. */
8270 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8273 for (o = dynobj->sections; o != NULL; o = o->next)
8275 if ((o->flags & SEC_HAS_CONTENTS) == 0
8277 || o->output_section == bfd_abs_section_ptr)
8279 if ((o->flags & SEC_LINKER_CREATED) == 0)
8281 /* At this point, we are only interested in sections
8282 created by _bfd_elf_link_create_dynamic_sections. */
8285 if (elf_hash_table (info)->stab_info.stabstr == o)
8287 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8289 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8291 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8293 if (! bfd_set_section_contents (abfd, o->output_section,
8295 (file_ptr) o->output_offset,
8301 /* The contents of the .dynstr section are actually in a
8303 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8304 if (bfd_seek (abfd, off, SEEK_SET) != 0
8305 || ! _bfd_elf_strtab_emit (abfd,
8306 elf_hash_table (info)->dynstr))
8312 if (info->relocatable)
8314 bfd_boolean failed = FALSE;
8316 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8321 /* If we have optimized stabs strings, output them. */
8322 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8324 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8328 if (info->eh_frame_hdr)
8330 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8334 if (finfo.symstrtab != NULL)
8335 _bfd_stringtab_free (finfo.symstrtab);
8336 if (finfo.contents != NULL)
8337 free (finfo.contents);
8338 if (finfo.external_relocs != NULL)
8339 free (finfo.external_relocs);
8340 if (finfo.internal_relocs != NULL)
8341 free (finfo.internal_relocs);
8342 if (finfo.external_syms != NULL)
8343 free (finfo.external_syms);
8344 if (finfo.locsym_shndx != NULL)
8345 free (finfo.locsym_shndx);
8346 if (finfo.internal_syms != NULL)
8347 free (finfo.internal_syms);
8348 if (finfo.indices != NULL)
8349 free (finfo.indices);
8350 if (finfo.sections != NULL)
8351 free (finfo.sections);
8352 if (finfo.symbuf != NULL)
8353 free (finfo.symbuf);
8354 if (finfo.symshndxbuf != NULL)
8355 free (finfo.symshndxbuf);
8356 for (o = abfd->sections; o != NULL; o = o->next)
8358 if ((o->flags & SEC_RELOC) != 0
8359 && elf_section_data (o)->rel_hashes != NULL)
8360 free (elf_section_data (o)->rel_hashes);
8363 elf_tdata (abfd)->linker = TRUE;
8368 if (finfo.symstrtab != NULL)
8369 _bfd_stringtab_free (finfo.symstrtab);
8370 if (finfo.contents != NULL)
8371 free (finfo.contents);
8372 if (finfo.external_relocs != NULL)
8373 free (finfo.external_relocs);
8374 if (finfo.internal_relocs != NULL)
8375 free (finfo.internal_relocs);
8376 if (finfo.external_syms != NULL)
8377 free (finfo.external_syms);
8378 if (finfo.locsym_shndx != NULL)
8379 free (finfo.locsym_shndx);
8380 if (finfo.internal_syms != NULL)
8381 free (finfo.internal_syms);
8382 if (finfo.indices != NULL)
8383 free (finfo.indices);
8384 if (finfo.sections != NULL)
8385 free (finfo.sections);
8386 if (finfo.symbuf != NULL)
8387 free (finfo.symbuf);
8388 if (finfo.symshndxbuf != NULL)
8389 free (finfo.symshndxbuf);
8390 for (o = abfd->sections; o != NULL; o = o->next)
8392 if ((o->flags & SEC_RELOC) != 0
8393 && elf_section_data (o)->rel_hashes != NULL)
8394 free (elf_section_data (o)->rel_hashes);
8400 /* Garbage collect unused sections. */
8402 /* The mark phase of garbage collection. For a given section, mark
8403 it and any sections in this section's group, and all the sections
8404 which define symbols to which it refers. */
8406 typedef asection * (*gc_mark_hook_fn)
8407 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8408 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8411 _bfd_elf_gc_mark (struct bfd_link_info *info,
8413 gc_mark_hook_fn gc_mark_hook)
8416 asection *group_sec;
8420 /* Mark all the sections in the group. */
8421 group_sec = elf_section_data (sec)->next_in_group;
8422 if (group_sec && !group_sec->gc_mark)
8423 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8426 /* Look through the section relocs. */
8428 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8430 Elf_Internal_Rela *relstart, *rel, *relend;
8431 Elf_Internal_Shdr *symtab_hdr;
8432 struct elf_link_hash_entry **sym_hashes;
8435 bfd *input_bfd = sec->owner;
8436 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8437 Elf_Internal_Sym *isym = NULL;
8440 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8441 sym_hashes = elf_sym_hashes (input_bfd);
8443 /* Read the local symbols. */
8444 if (elf_bad_symtab (input_bfd))
8446 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8450 extsymoff = nlocsyms = symtab_hdr->sh_info;
8452 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8453 if (isym == NULL && nlocsyms != 0)
8455 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8461 /* Read the relocations. */
8462 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8464 if (relstart == NULL)
8469 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8471 if (bed->s->arch_size == 32)
8476 for (rel = relstart; rel < relend; rel++)
8478 unsigned long r_symndx;
8480 struct elf_link_hash_entry *h;
8482 r_symndx = rel->r_info >> r_sym_shift;
8486 if (r_symndx >= nlocsyms
8487 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8489 h = sym_hashes[r_symndx - extsymoff];
8490 while (h->root.type == bfd_link_hash_indirect
8491 || h->root.type == bfd_link_hash_warning)
8492 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8493 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8497 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8500 if (rsec && !rsec->gc_mark)
8502 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8504 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8513 if (elf_section_data (sec)->relocs != relstart)
8516 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8518 if (! info->keep_memory)
8521 symtab_hdr->contents = (unsigned char *) isym;
8528 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8531 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8535 if (h->root.type == bfd_link_hash_warning)
8536 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8538 if (h->dynindx != -1
8539 && ((h->root.type != bfd_link_hash_defined
8540 && h->root.type != bfd_link_hash_defweak)
8541 || h->root.u.def.section->gc_mark))
8542 h->dynindx = (*idx)++;
8547 /* The sweep phase of garbage collection. Remove all garbage sections. */
8549 typedef bfd_boolean (*gc_sweep_hook_fn)
8550 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8553 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8557 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8561 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8564 for (o = sub->sections; o != NULL; o = o->next)
8566 /* Keep debug and special sections. */
8567 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8568 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8574 /* Skip sweeping sections already excluded. */
8575 if (o->flags & SEC_EXCLUDE)
8578 /* Since this is early in the link process, it is simple
8579 to remove a section from the output. */
8580 o->flags |= SEC_EXCLUDE;
8582 /* But we also have to update some of the relocation
8583 info we collected before. */
8585 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
8587 Elf_Internal_Rela *internal_relocs;
8591 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8593 if (internal_relocs == NULL)
8596 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8598 if (elf_section_data (o)->relocs != internal_relocs)
8599 free (internal_relocs);
8607 /* Remove the symbols that were in the swept sections from the dynamic
8608 symbol table. GCFIXME: Anyone know how to get them out of the
8609 static symbol table as well? */
8613 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8615 elf_hash_table (info)->dynsymcount = i;
8621 /* Propagate collected vtable information. This is called through
8622 elf_link_hash_traverse. */
8625 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8627 if (h->root.type == bfd_link_hash_warning)
8628 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8630 /* Those that are not vtables. */
8631 if (h->vtable == NULL || h->vtable->parent == NULL)
8634 /* Those vtables that do not have parents, we cannot merge. */
8635 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
8638 /* If we've already been done, exit. */
8639 if (h->vtable->used && h->vtable->used[-1])
8642 /* Make sure the parent's table is up to date. */
8643 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
8645 if (h->vtable->used == NULL)
8647 /* None of this table's entries were referenced. Re-use the
8649 h->vtable->used = h->vtable->parent->vtable->used;
8650 h->vtable->size = h->vtable->parent->vtable->size;
8655 bfd_boolean *cu, *pu;
8657 /* Or the parent's entries into ours. */
8658 cu = h->vtable->used;
8660 pu = h->vtable->parent->vtable->used;
8663 const struct elf_backend_data *bed;
8664 unsigned int log_file_align;
8666 bed = get_elf_backend_data (h->root.u.def.section->owner);
8667 log_file_align = bed->s->log_file_align;
8668 n = h->vtable->parent->vtable->size >> log_file_align;
8683 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
8686 bfd_vma hstart, hend;
8687 Elf_Internal_Rela *relstart, *relend, *rel;
8688 const struct elf_backend_data *bed;
8689 unsigned int log_file_align;
8691 if (h->root.type == bfd_link_hash_warning)
8692 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8694 /* Take care of both those symbols that do not describe vtables as
8695 well as those that are not loaded. */
8696 if (h->vtable == NULL || h->vtable->parent == NULL)
8699 BFD_ASSERT (h->root.type == bfd_link_hash_defined
8700 || h->root.type == bfd_link_hash_defweak);
8702 sec = h->root.u.def.section;
8703 hstart = h->root.u.def.value;
8704 hend = hstart + h->size;
8706 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
8708 return *(bfd_boolean *) okp = FALSE;
8709 bed = get_elf_backend_data (sec->owner);
8710 log_file_align = bed->s->log_file_align;
8712 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8714 for (rel = relstart; rel < relend; ++rel)
8715 if (rel->r_offset >= hstart && rel->r_offset < hend)
8717 /* If the entry is in use, do nothing. */
8719 && (rel->r_offset - hstart) < h->vtable->size)
8721 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
8722 if (h->vtable->used[entry])
8725 /* Otherwise, kill it. */
8726 rel->r_offset = rel->r_info = rel->r_addend = 0;
8732 /* Mark sections containing dynamically referenced symbols. This is called
8733 through elf_link_hash_traverse. */
8736 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h,
8737 void *okp ATTRIBUTE_UNUSED)
8739 if (h->root.type == bfd_link_hash_warning)
8740 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8742 if ((h->root.type == bfd_link_hash_defined
8743 || h->root.type == bfd_link_hash_defweak)
8745 h->root.u.def.section->flags |= SEC_KEEP;
8750 /* Do mark and sweep of unused sections. */
8753 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
8755 bfd_boolean ok = TRUE;
8757 asection * (*gc_mark_hook)
8758 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8759 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
8761 if (!get_elf_backend_data (abfd)->can_gc_sections
8762 || info->relocatable
8763 || info->emitrelocations
8765 || !is_elf_hash_table (info->hash))
8767 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
8771 /* Apply transitive closure to the vtable entry usage info. */
8772 elf_link_hash_traverse (elf_hash_table (info),
8773 elf_gc_propagate_vtable_entries_used,
8778 /* Kill the vtable relocations that were not used. */
8779 elf_link_hash_traverse (elf_hash_table (info),
8780 elf_gc_smash_unused_vtentry_relocs,
8785 /* Mark dynamically referenced symbols. */
8786 if (elf_hash_table (info)->dynamic_sections_created)
8787 elf_link_hash_traverse (elf_hash_table (info),
8788 elf_gc_mark_dynamic_ref_symbol,
8793 /* Grovel through relocs to find out who stays ... */
8794 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
8795 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8799 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8802 for (o = sub->sections; o != NULL; o = o->next)
8804 if (o->flags & SEC_KEEP)
8806 /* _bfd_elf_discard_section_eh_frame knows how to discard
8807 orphaned FDEs so don't mark sections referenced by the
8808 EH frame section. */
8809 if (strcmp (o->name, ".eh_frame") == 0)
8811 else if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
8817 /* ... and mark SEC_EXCLUDE for those that go. */
8818 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
8824 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8827 bfd_elf_gc_record_vtinherit (bfd *abfd,
8829 struct elf_link_hash_entry *h,
8832 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
8833 struct elf_link_hash_entry **search, *child;
8834 bfd_size_type extsymcount;
8835 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8837 /* The sh_info field of the symtab header tells us where the
8838 external symbols start. We don't care about the local symbols at
8840 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
8841 if (!elf_bad_symtab (abfd))
8842 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8844 sym_hashes = elf_sym_hashes (abfd);
8845 sym_hashes_end = sym_hashes + extsymcount;
8847 /* Hunt down the child symbol, which is in this section at the same
8848 offset as the relocation. */
8849 for (search = sym_hashes; search != sym_hashes_end; ++search)
8851 if ((child = *search) != NULL
8852 && (child->root.type == bfd_link_hash_defined
8853 || child->root.type == bfd_link_hash_defweak)
8854 && child->root.u.def.section == sec
8855 && child->root.u.def.value == offset)
8859 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
8860 abfd, sec, (unsigned long) offset);
8861 bfd_set_error (bfd_error_invalid_operation);
8867 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
8873 /* This *should* only be the absolute section. It could potentially
8874 be that someone has defined a non-global vtable though, which
8875 would be bad. It isn't worth paging in the local symbols to be
8876 sure though; that case should simply be handled by the assembler. */
8878 child->vtable->parent = (struct elf_link_hash_entry *) -1;
8881 child->vtable->parent = h;
8886 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8889 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
8890 asection *sec ATTRIBUTE_UNUSED,
8891 struct elf_link_hash_entry *h,
8894 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8895 unsigned int log_file_align = bed->s->log_file_align;
8899 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
8904 if (addend >= h->vtable->size)
8906 size_t size, bytes, file_align;
8907 bfd_boolean *ptr = h->vtable->used;
8909 /* While the symbol is undefined, we have to be prepared to handle
8911 file_align = 1 << log_file_align;
8912 if (h->root.type == bfd_link_hash_undefined)
8913 size = addend + file_align;
8919 /* Oops! We've got a reference past the defined end of
8920 the table. This is probably a bug -- shall we warn? */
8921 size = addend + file_align;
8924 size = (size + file_align - 1) & -file_align;
8926 /* Allocate one extra entry for use as a "done" flag for the
8927 consolidation pass. */
8928 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
8932 ptr = bfd_realloc (ptr - 1, bytes);
8938 oldbytes = (((h->vtable->size >> log_file_align) + 1)
8939 * sizeof (bfd_boolean));
8940 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
8944 ptr = bfd_zmalloc (bytes);
8949 /* And arrange for that done flag to be at index -1. */
8950 h->vtable->used = ptr + 1;
8951 h->vtable->size = size;
8954 h->vtable->used[addend >> log_file_align] = TRUE;
8959 struct alloc_got_off_arg {
8961 unsigned int got_elt_size;
8964 /* We need a special top-level link routine to convert got reference counts
8965 to real got offsets. */
8968 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
8970 struct alloc_got_off_arg *gofarg = arg;
8972 if (h->root.type == bfd_link_hash_warning)
8973 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8975 if (h->got.refcount > 0)
8977 h->got.offset = gofarg->gotoff;
8978 gofarg->gotoff += gofarg->got_elt_size;
8981 h->got.offset = (bfd_vma) -1;
8986 /* And an accompanying bit to work out final got entry offsets once
8987 we're done. Should be called from final_link. */
8990 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
8991 struct bfd_link_info *info)
8994 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8996 unsigned int got_elt_size = bed->s->arch_size / 8;
8997 struct alloc_got_off_arg gofarg;
8999 if (! is_elf_hash_table (info->hash))
9002 /* The GOT offset is relative to the .got section, but the GOT header is
9003 put into the .got.plt section, if the backend uses it. */
9004 if (bed->want_got_plt)
9007 gotoff = bed->got_header_size;
9009 /* Do the local .got entries first. */
9010 for (i = info->input_bfds; i; i = i->link_next)
9012 bfd_signed_vma *local_got;
9013 bfd_size_type j, locsymcount;
9014 Elf_Internal_Shdr *symtab_hdr;
9016 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9019 local_got = elf_local_got_refcounts (i);
9023 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9024 if (elf_bad_symtab (i))
9025 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9027 locsymcount = symtab_hdr->sh_info;
9029 for (j = 0; j < locsymcount; ++j)
9031 if (local_got[j] > 0)
9033 local_got[j] = gotoff;
9034 gotoff += got_elt_size;
9037 local_got[j] = (bfd_vma) -1;
9041 /* Then the global .got entries. .plt refcounts are handled by
9042 adjust_dynamic_symbol */
9043 gofarg.gotoff = gotoff;
9044 gofarg.got_elt_size = got_elt_size;
9045 elf_link_hash_traverse (elf_hash_table (info),
9046 elf_gc_allocate_got_offsets,
9051 /* Many folk need no more in the way of final link than this, once
9052 got entry reference counting is enabled. */
9055 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9057 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9060 /* Invoke the regular ELF backend linker to do all the work. */
9061 return bfd_elf_final_link (abfd, info);
9065 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9067 struct elf_reloc_cookie *rcookie = cookie;
9069 if (rcookie->bad_symtab)
9070 rcookie->rel = rcookie->rels;
9072 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9074 unsigned long r_symndx;
9076 if (! rcookie->bad_symtab)
9077 if (rcookie->rel->r_offset > offset)
9079 if (rcookie->rel->r_offset != offset)
9082 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9083 if (r_symndx == SHN_UNDEF)
9086 if (r_symndx >= rcookie->locsymcount
9087 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9089 struct elf_link_hash_entry *h;
9091 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9093 while (h->root.type == bfd_link_hash_indirect
9094 || h->root.type == bfd_link_hash_warning)
9095 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9097 if ((h->root.type == bfd_link_hash_defined
9098 || h->root.type == bfd_link_hash_defweak)
9099 && elf_discarded_section (h->root.u.def.section))
9106 /* It's not a relocation against a global symbol,
9107 but it could be a relocation against a local
9108 symbol for a discarded section. */
9110 Elf_Internal_Sym *isym;
9112 /* Need to: get the symbol; get the section. */
9113 isym = &rcookie->locsyms[r_symndx];
9114 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9116 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9117 if (isec != NULL && elf_discarded_section (isec))
9126 /* Discard unneeded references to discarded sections.
9127 Returns TRUE if any section's size was changed. */
9128 /* This function assumes that the relocations are in sorted order,
9129 which is true for all known assemblers. */
9132 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9134 struct elf_reloc_cookie cookie;
9135 asection *stab, *eh;
9136 Elf_Internal_Shdr *symtab_hdr;
9137 const struct elf_backend_data *bed;
9140 bfd_boolean ret = FALSE;
9142 if (info->traditional_format
9143 || !is_elf_hash_table (info->hash))
9146 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9148 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9151 bed = get_elf_backend_data (abfd);
9153 if ((abfd->flags & DYNAMIC) != 0)
9156 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9157 if (info->relocatable
9160 || bfd_is_abs_section (eh->output_section))))
9163 stab = bfd_get_section_by_name (abfd, ".stab");
9166 || bfd_is_abs_section (stab->output_section)
9167 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9172 && bed->elf_backend_discard_info == NULL)
9175 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9177 cookie.sym_hashes = elf_sym_hashes (abfd);
9178 cookie.bad_symtab = elf_bad_symtab (abfd);
9179 if (cookie.bad_symtab)
9181 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9182 cookie.extsymoff = 0;
9186 cookie.locsymcount = symtab_hdr->sh_info;
9187 cookie.extsymoff = symtab_hdr->sh_info;
9190 if (bed->s->arch_size == 32)
9191 cookie.r_sym_shift = 8;
9193 cookie.r_sym_shift = 32;
9195 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9196 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9198 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9199 cookie.locsymcount, 0,
9201 if (cookie.locsyms == NULL)
9208 count = stab->reloc_count;
9210 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9212 if (cookie.rels != NULL)
9214 cookie.rel = cookie.rels;
9215 cookie.relend = cookie.rels;
9216 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9217 if (_bfd_discard_section_stabs (abfd, stab,
9218 elf_section_data (stab)->sec_info,
9219 bfd_elf_reloc_symbol_deleted_p,
9222 if (elf_section_data (stab)->relocs != cookie.rels)
9230 count = eh->reloc_count;
9232 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9234 cookie.rel = cookie.rels;
9235 cookie.relend = cookie.rels;
9236 if (cookie.rels != NULL)
9237 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9239 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9240 bfd_elf_reloc_symbol_deleted_p,
9244 if (cookie.rels != NULL
9245 && elf_section_data (eh)->relocs != cookie.rels)
9249 if (bed->elf_backend_discard_info != NULL
9250 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9253 if (cookie.locsyms != NULL
9254 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9256 if (! info->keep_memory)
9257 free (cookie.locsyms);
9259 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9263 if (info->eh_frame_hdr
9264 && !info->relocatable
9265 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9271 struct already_linked_section
9277 /* Check if the member of a single member comdat group matches a
9278 linkonce section and vice versa. */
9280 try_match_symbols_in_sections
9281 (struct bfd_section_already_linked_hash_entry *h, void *info)
9283 struct bfd_section_already_linked *l;
9284 struct already_linked_section *s
9285 = (struct already_linked_section *) info;
9287 if (elf_sec_group (s->sec) == NULL)
9289 /* It is a linkonce section. Try to match it with the member of a
9290 single member comdat group. */
9291 for (l = h->entry; l != NULL; l = l->next)
9292 if ((l->sec->flags & SEC_GROUP))
9294 asection *first = elf_next_in_group (l->sec);
9297 && elf_next_in_group (first) == first
9298 && bfd_elf_match_symbols_in_sections (first, s->sec))
9307 /* It is the member of a single member comdat group. Try to match
9308 it with a linkonce section. */
9309 for (l = h->entry; l != NULL; l = l->next)
9310 if ((l->sec->flags & SEC_GROUP) == 0
9311 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9312 && bfd_elf_match_symbols_in_sections (l->sec, s->sec))
9323 already_linked (asection *sec, asection *group)
9325 struct already_linked_section result;
9328 result.linked = NULL;
9330 bfd_section_already_linked_table_traverse
9331 (try_match_symbols_in_sections, &result);
9335 sec->output_section = bfd_abs_section_ptr;
9336 sec->kept_section = result.linked;
9338 /* Also discard the group section. */
9340 group->output_section = bfd_abs_section_ptr;
9349 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9353 struct bfd_section_already_linked *l;
9354 struct bfd_section_already_linked_hash_entry *already_linked_list;
9357 /* A single member comdat group section may be discarded by a
9358 linkonce section. See below. */
9359 if (sec->output_section == bfd_abs_section_ptr)
9364 /* Check if it belongs to a section group. */
9365 group = elf_sec_group (sec);
9367 /* Return if it isn't a linkonce section nor a member of a group. A
9368 comdat group section also has SEC_LINK_ONCE set. */
9369 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9374 /* If this is the member of a single member comdat group, check if
9375 the group should be discarded. */
9376 if (elf_next_in_group (sec) == sec
9377 && (group->flags & SEC_LINK_ONCE) != 0)
9383 /* FIXME: When doing a relocatable link, we may have trouble
9384 copying relocations in other sections that refer to local symbols
9385 in the section being discarded. Those relocations will have to
9386 be converted somehow; as of this writing I'm not sure that any of
9387 the backends handle that correctly.
9389 It is tempting to instead not discard link once sections when
9390 doing a relocatable link (technically, they should be discarded
9391 whenever we are building constructors). However, that fails,
9392 because the linker winds up combining all the link once sections
9393 into a single large link once section, which defeats the purpose
9394 of having link once sections in the first place.
9396 Also, not merging link once sections in a relocatable link
9397 causes trouble for MIPS ELF, which relies on link once semantics
9398 to handle the .reginfo section correctly. */
9400 name = bfd_get_section_name (abfd, sec);
9402 already_linked_list = bfd_section_already_linked_table_lookup (name);
9404 for (l = already_linked_list->entry; l != NULL; l = l->next)
9406 /* We may have 3 different sections on the list: group section,
9407 comdat section and linkonce section. SEC may be a linkonce or
9408 group section. We match a group section with a group section,
9409 a linkonce section with a linkonce section, and ignore comdat
9411 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9412 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9414 /* The section has already been linked. See if we should
9416 switch (flags & SEC_LINK_DUPLICATES)
9421 case SEC_LINK_DUPLICATES_DISCARD:
9424 case SEC_LINK_DUPLICATES_ONE_ONLY:
9425 (*_bfd_error_handler)
9426 (_("%B: ignoring duplicate section `%A'\n"),
9430 case SEC_LINK_DUPLICATES_SAME_SIZE:
9431 if (sec->size != l->sec->size)
9432 (*_bfd_error_handler)
9433 (_("%B: duplicate section `%A' has different size\n"),
9437 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9438 if (sec->size != l->sec->size)
9439 (*_bfd_error_handler)
9440 (_("%B: duplicate section `%A' has different size\n"),
9442 else if (sec->size != 0)
9444 bfd_byte *sec_contents, *l_sec_contents;
9446 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9447 (*_bfd_error_handler)
9448 (_("%B: warning: could not read contents of section `%A'\n"),
9450 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9452 (*_bfd_error_handler)
9453 (_("%B: warning: could not read contents of section `%A'\n"),
9454 l->sec->owner, l->sec);
9455 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9456 (*_bfd_error_handler)
9457 (_("%B: warning: duplicate section `%A' has different contents\n"),
9461 free (sec_contents);
9463 free (l_sec_contents);
9468 /* Set the output_section field so that lang_add_section
9469 does not create a lang_input_section structure for this
9470 section. Since there might be a symbol in the section
9471 being discarded, we must retain a pointer to the section
9472 which we are really going to use. */
9473 sec->output_section = bfd_abs_section_ptr;
9474 sec->kept_section = l->sec;
9476 if (flags & SEC_GROUP)
9478 asection *first = elf_next_in_group (sec);
9479 asection *s = first;
9483 s->output_section = bfd_abs_section_ptr;
9484 /* Record which group discards it. */
9485 s->kept_section = l->sec;
9486 s = elf_next_in_group (s);
9487 /* These lists are circular. */
9499 /* If this is the member of a single member comdat group and the
9500 group hasn't be discarded, we check if it matches a linkonce
9501 section. We only record the discarded comdat group. Otherwise
9502 the undiscarded group will be discarded incorrectly later since
9503 itself has been recorded. */
9504 if (! already_linked (elf_next_in_group (sec), group))
9508 /* There is no direct match. But for linkonce section, we should
9509 check if there is a match with comdat group member. We always
9510 record the linkonce section, discarded or not. */
9511 already_linked (sec, group);
9513 /* This is the first section with this name. Record it. */
9514 bfd_section_already_linked_table_insert (already_linked_list, sec);