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))
634 && (ip->flags & SEC_LINKER_CREATED)
635 && ip->output_section == p)
640 /* There shouldn't be section relative relocations
641 against any other section. */
647 /* Assign dynsym indices. In a shared library we generate a section
648 symbol for each output section, which come first. Next come all of
649 the back-end allocated local dynamic syms, followed by the rest of
650 the global symbols. */
653 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
655 unsigned long dynsymcount = 0;
659 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
661 for (p = output_bfd->sections; p ; p = p->next)
662 if ((p->flags & SEC_EXCLUDE) == 0
663 && (p->flags & SEC_ALLOC) != 0
664 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
665 elf_section_data (p)->dynindx = ++dynsymcount;
668 if (elf_hash_table (info)->dynlocal)
670 struct elf_link_local_dynamic_entry *p;
671 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
672 p->dynindx = ++dynsymcount;
675 elf_link_hash_traverse (elf_hash_table (info),
676 elf_link_renumber_hash_table_dynsyms,
679 /* There is an unused NULL entry at the head of the table which
680 we must account for in our count. Unless there weren't any
681 symbols, which means we'll have no table at all. */
682 if (dynsymcount != 0)
685 return elf_hash_table (info)->dynsymcount = dynsymcount;
688 /* This function is called when we want to define a new symbol. It
689 handles the various cases which arise when we find a definition in
690 a dynamic object, or when there is already a definition in a
691 dynamic object. The new symbol is described by NAME, SYM, PSEC,
692 and PVALUE. We set SYM_HASH to the hash table entry. We set
693 OVERRIDE if the old symbol is overriding a new definition. We set
694 TYPE_CHANGE_OK if it is OK for the type to change. We set
695 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
696 change, we mean that we shouldn't warn if the type or size does
700 _bfd_elf_merge_symbol (bfd *abfd,
701 struct bfd_link_info *info,
703 Elf_Internal_Sym *sym,
706 struct elf_link_hash_entry **sym_hash,
708 bfd_boolean *override,
709 bfd_boolean *type_change_ok,
710 bfd_boolean *size_change_ok)
712 asection *sec, *oldsec;
713 struct elf_link_hash_entry *h;
714 struct elf_link_hash_entry *flip;
717 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
718 bfd_boolean newweak, oldweak;
724 bind = ELF_ST_BIND (sym->st_info);
726 if (! bfd_is_und_section (sec))
727 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
729 h = ((struct elf_link_hash_entry *)
730 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
735 /* This code is for coping with dynamic objects, and is only useful
736 if we are doing an ELF link. */
737 if (info->hash->creator != abfd->xvec)
740 /* For merging, we only care about real symbols. */
742 while (h->root.type == bfd_link_hash_indirect
743 || h->root.type == bfd_link_hash_warning)
744 h = (struct elf_link_hash_entry *) h->root.u.i.link;
746 /* If we just created the symbol, mark it as being an ELF symbol.
747 Other than that, there is nothing to do--there is no merge issue
748 with a newly defined symbol--so we just return. */
750 if (h->root.type == bfd_link_hash_new)
756 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
759 switch (h->root.type)
766 case bfd_link_hash_undefined:
767 case bfd_link_hash_undefweak:
768 oldbfd = h->root.u.undef.abfd;
772 case bfd_link_hash_defined:
773 case bfd_link_hash_defweak:
774 oldbfd = h->root.u.def.section->owner;
775 oldsec = h->root.u.def.section;
778 case bfd_link_hash_common:
779 oldbfd = h->root.u.c.p->section->owner;
780 oldsec = h->root.u.c.p->section;
784 /* In cases involving weak versioned symbols, we may wind up trying
785 to merge a symbol with itself. Catch that here, to avoid the
786 confusion that results if we try to override a symbol with
787 itself. The additional tests catch cases like
788 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
789 dynamic object, which we do want to handle here. */
791 && ((abfd->flags & DYNAMIC) == 0
795 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
796 respectively, is from a dynamic object. */
798 if ((abfd->flags & DYNAMIC) != 0)
804 olddyn = (oldbfd->flags & DYNAMIC) != 0;
809 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
810 indices used by MIPS ELF. */
811 switch (h->root.type)
817 case bfd_link_hash_defined:
818 case bfd_link_hash_defweak:
819 hsec = h->root.u.def.section;
822 case bfd_link_hash_common:
823 hsec = h->root.u.c.p->section;
830 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
833 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
834 respectively, appear to be a definition rather than reference. */
836 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
841 if (h->root.type == bfd_link_hash_undefined
842 || h->root.type == bfd_link_hash_undefweak
843 || h->root.type == bfd_link_hash_common)
848 /* Check TLS symbol. */
849 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
850 && ELF_ST_TYPE (sym->st_info) != h->type)
853 bfd_boolean ntdef, tdef;
854 asection *ntsec, *tsec;
856 if (h->type == STT_TLS)
876 (*_bfd_error_handler)
877 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
878 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
879 else if (!tdef && !ntdef)
880 (*_bfd_error_handler)
881 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
882 tbfd, ntbfd, h->root.root.string);
884 (*_bfd_error_handler)
885 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
886 tbfd, tsec, ntbfd, h->root.root.string);
888 (*_bfd_error_handler)
889 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
890 tbfd, ntbfd, ntsec, h->root.root.string);
892 bfd_set_error (bfd_error_bad_value);
896 /* We need to remember if a symbol has a definition in a dynamic
897 object or is weak in all dynamic objects. Internal and hidden
898 visibility will make it unavailable to dynamic objects. */
899 if (newdyn && !h->dynamic_def)
901 if (!bfd_is_und_section (sec))
905 /* Check if this symbol is weak in all dynamic objects. If it
906 is the first time we see it in a dynamic object, we mark
907 if it is weak. Otherwise, we clear it. */
910 if (bind == STB_WEAK)
913 else if (bind != STB_WEAK)
918 /* If the old symbol has non-default visibility, we ignore the new
919 definition from a dynamic object. */
921 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
922 && !bfd_is_und_section (sec))
925 /* Make sure this symbol is dynamic. */
927 /* A protected symbol has external availability. Make sure it is
930 FIXME: Should we check type and size for protected symbol? */
931 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
932 return bfd_elf_link_record_dynamic_symbol (info, h);
937 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
940 /* If the new symbol with non-default visibility comes from a
941 relocatable file and the old definition comes from a dynamic
942 object, we remove the old definition. */
943 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
946 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
947 && bfd_is_und_section (sec))
949 /* If the new symbol is undefined and the old symbol was
950 also undefined before, we need to make sure
951 _bfd_generic_link_add_one_symbol doesn't mess
952 up the linker hash table undefs list. Since the old
953 definition came from a dynamic object, it is still on the
955 h->root.type = bfd_link_hash_undefined;
956 h->root.u.undef.abfd = abfd;
960 h->root.type = bfd_link_hash_new;
961 h->root.u.undef.abfd = NULL;
970 /* FIXME: Should we check type and size for protected symbol? */
976 /* Differentiate strong and weak symbols. */
977 newweak = bind == STB_WEAK;
978 oldweak = (h->root.type == bfd_link_hash_defweak
979 || h->root.type == bfd_link_hash_undefweak);
981 /* If a new weak symbol definition comes from a regular file and the
982 old symbol comes from a dynamic library, we treat the new one as
983 strong. Similarly, an old weak symbol definition from a regular
984 file is treated as strong when the new symbol comes from a dynamic
985 library. Further, an old weak symbol from a dynamic library is
986 treated as strong if the new symbol is from a dynamic library.
987 This reflects the way glibc's ld.so works.
989 Do this before setting *type_change_ok or *size_change_ok so that
990 we warn properly when dynamic library symbols are overridden. */
992 if (newdef && !newdyn && olddyn)
994 if (olddef && newdyn)
997 /* It's OK to change the type if either the existing symbol or the
998 new symbol is weak. A type change is also OK if the old symbol
999 is undefined and the new symbol is defined. */
1004 && h->root.type == bfd_link_hash_undefined))
1005 *type_change_ok = TRUE;
1007 /* It's OK to change the size if either the existing symbol or the
1008 new symbol is weak, or if the old symbol is undefined. */
1011 || h->root.type == bfd_link_hash_undefined)
1012 *size_change_ok = TRUE;
1014 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1015 symbol, respectively, appears to be a common symbol in a dynamic
1016 object. If a symbol appears in an uninitialized section, and is
1017 not weak, and is not a function, then it may be a common symbol
1018 which was resolved when the dynamic object was created. We want
1019 to treat such symbols specially, because they raise special
1020 considerations when setting the symbol size: if the symbol
1021 appears as a common symbol in a regular object, and the size in
1022 the regular object is larger, we must make sure that we use the
1023 larger size. This problematic case can always be avoided in C,
1024 but it must be handled correctly when using Fortran shared
1027 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1028 likewise for OLDDYNCOMMON and OLDDEF.
1030 Note that this test is just a heuristic, and that it is quite
1031 possible to have an uninitialized symbol in a shared object which
1032 is really a definition, rather than a common symbol. This could
1033 lead to some minor confusion when the symbol really is a common
1034 symbol in some regular object. However, I think it will be
1040 && (sec->flags & SEC_ALLOC) != 0
1041 && (sec->flags & SEC_LOAD) == 0
1043 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1044 newdyncommon = TRUE;
1046 newdyncommon = FALSE;
1050 && h->root.type == bfd_link_hash_defined
1052 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1053 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1055 && h->type != STT_FUNC)
1056 olddyncommon = TRUE;
1058 olddyncommon = FALSE;
1060 /* If both the old and the new symbols look like common symbols in a
1061 dynamic object, set the size of the symbol to the larger of the
1066 && sym->st_size != h->size)
1068 /* Since we think we have two common symbols, issue a multiple
1069 common warning if desired. Note that we only warn if the
1070 size is different. If the size is the same, we simply let
1071 the old symbol override the new one as normally happens with
1072 symbols defined in dynamic objects. */
1074 if (! ((*info->callbacks->multiple_common)
1075 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1076 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1079 if (sym->st_size > h->size)
1080 h->size = sym->st_size;
1082 *size_change_ok = TRUE;
1085 /* If we are looking at a dynamic object, and we have found a
1086 definition, we need to see if the symbol was already defined by
1087 some other object. If so, we want to use the existing
1088 definition, and we do not want to report a multiple symbol
1089 definition error; we do this by clobbering *PSEC to be
1090 bfd_und_section_ptr.
1092 We treat a common symbol as a definition if the symbol in the
1093 shared library is a function, since common symbols always
1094 represent variables; this can cause confusion in principle, but
1095 any such confusion would seem to indicate an erroneous program or
1096 shared library. We also permit a common symbol in a regular
1097 object to override a weak symbol in a shared object. */
1102 || (h->root.type == bfd_link_hash_common
1104 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1108 newdyncommon = FALSE;
1110 *psec = sec = bfd_und_section_ptr;
1111 *size_change_ok = TRUE;
1113 /* If we get here when the old symbol is a common symbol, then
1114 we are explicitly letting it override a weak symbol or
1115 function in a dynamic object, and we don't want to warn about
1116 a type change. If the old symbol is a defined symbol, a type
1117 change warning may still be appropriate. */
1119 if (h->root.type == bfd_link_hash_common)
1120 *type_change_ok = TRUE;
1123 /* Handle the special case of an old common symbol merging with a
1124 new symbol which looks like a common symbol in a shared object.
1125 We change *PSEC and *PVALUE to make the new symbol look like a
1126 common symbol, and let _bfd_generic_link_add_one_symbol will do
1130 && h->root.type == bfd_link_hash_common)
1134 newdyncommon = FALSE;
1135 *pvalue = sym->st_size;
1136 *psec = sec = bfd_com_section_ptr;
1137 *size_change_ok = TRUE;
1140 /* If the old symbol is from a dynamic object, and the new symbol is
1141 a definition which is not from a dynamic object, then the new
1142 symbol overrides the old symbol. Symbols from regular files
1143 always take precedence over symbols from dynamic objects, even if
1144 they are defined after the dynamic object in the link.
1146 As above, we again permit a common symbol in a regular object to
1147 override a definition in a shared object if the shared object
1148 symbol is a function or is weak. */
1153 || (bfd_is_com_section (sec)
1155 || h->type == STT_FUNC)))
1160 /* Change the hash table entry to undefined, and let
1161 _bfd_generic_link_add_one_symbol do the right thing with the
1164 h->root.type = bfd_link_hash_undefined;
1165 h->root.u.undef.abfd = h->root.u.def.section->owner;
1166 *size_change_ok = TRUE;
1169 olddyncommon = FALSE;
1171 /* We again permit a type change when a common symbol may be
1172 overriding a function. */
1174 if (bfd_is_com_section (sec))
1175 *type_change_ok = TRUE;
1177 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1180 /* This union may have been set to be non-NULL when this symbol
1181 was seen in a dynamic object. We must force the union to be
1182 NULL, so that it is correct for a regular symbol. */
1183 h->verinfo.vertree = NULL;
1186 /* Handle the special case of a new common symbol merging with an
1187 old symbol that looks like it might be a common symbol defined in
1188 a shared object. Note that we have already handled the case in
1189 which a new common symbol should simply override the definition
1190 in the shared library. */
1193 && bfd_is_com_section (sec)
1196 /* It would be best if we could set the hash table entry to a
1197 common symbol, but we don't know what to use for the section
1198 or the alignment. */
1199 if (! ((*info->callbacks->multiple_common)
1200 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1201 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1204 /* If the presumed common symbol in the dynamic object is
1205 larger, pretend that the new symbol has its size. */
1207 if (h->size > *pvalue)
1210 /* FIXME: We no longer know the alignment required by the symbol
1211 in the dynamic object, so we just wind up using the one from
1212 the regular object. */
1215 olddyncommon = FALSE;
1217 h->root.type = bfd_link_hash_undefined;
1218 h->root.u.undef.abfd = h->root.u.def.section->owner;
1220 *size_change_ok = TRUE;
1221 *type_change_ok = TRUE;
1223 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1226 h->verinfo.vertree = NULL;
1231 /* Handle the case where we had a versioned symbol in a dynamic
1232 library and now find a definition in a normal object. In this
1233 case, we make the versioned symbol point to the normal one. */
1234 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1235 flip->root.type = h->root.type;
1236 h->root.type = bfd_link_hash_indirect;
1237 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1238 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1239 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1243 flip->ref_dynamic = 1;
1250 /* This function is called to create an indirect symbol from the
1251 default for the symbol with the default version if needed. The
1252 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1253 set DYNSYM if the new indirect symbol is dynamic. */
1256 _bfd_elf_add_default_symbol (bfd *abfd,
1257 struct bfd_link_info *info,
1258 struct elf_link_hash_entry *h,
1260 Elf_Internal_Sym *sym,
1263 bfd_boolean *dynsym,
1264 bfd_boolean override)
1266 bfd_boolean type_change_ok;
1267 bfd_boolean size_change_ok;
1270 struct elf_link_hash_entry *hi;
1271 struct bfd_link_hash_entry *bh;
1272 const struct elf_backend_data *bed;
1273 bfd_boolean collect;
1274 bfd_boolean dynamic;
1276 size_t len, shortlen;
1279 /* If this symbol has a version, and it is the default version, we
1280 create an indirect symbol from the default name to the fully
1281 decorated name. This will cause external references which do not
1282 specify a version to be bound to this version of the symbol. */
1283 p = strchr (name, ELF_VER_CHR);
1284 if (p == NULL || p[1] != ELF_VER_CHR)
1289 /* We are overridden by an old definition. We need to check if we
1290 need to create the indirect symbol from the default name. */
1291 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1293 BFD_ASSERT (hi != NULL);
1296 while (hi->root.type == bfd_link_hash_indirect
1297 || hi->root.type == bfd_link_hash_warning)
1299 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1305 bed = get_elf_backend_data (abfd);
1306 collect = bed->collect;
1307 dynamic = (abfd->flags & DYNAMIC) != 0;
1309 shortlen = p - name;
1310 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1311 if (shortname == NULL)
1313 memcpy (shortname, name, shortlen);
1314 shortname[shortlen] = '\0';
1316 /* We are going to create a new symbol. Merge it with any existing
1317 symbol with this name. For the purposes of the merge, act as
1318 though we were defining the symbol we just defined, although we
1319 actually going to define an indirect symbol. */
1320 type_change_ok = FALSE;
1321 size_change_ok = FALSE;
1323 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1324 &hi, &skip, &override, &type_change_ok,
1334 if (! (_bfd_generic_link_add_one_symbol
1335 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1336 0, name, FALSE, collect, &bh)))
1338 hi = (struct elf_link_hash_entry *) bh;
1342 /* In this case the symbol named SHORTNAME is overriding the
1343 indirect symbol we want to add. We were planning on making
1344 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1345 is the name without a version. NAME is the fully versioned
1346 name, and it is the default version.
1348 Overriding means that we already saw a definition for the
1349 symbol SHORTNAME in a regular object, and it is overriding
1350 the symbol defined in the dynamic object.
1352 When this happens, we actually want to change NAME, the
1353 symbol we just added, to refer to SHORTNAME. This will cause
1354 references to NAME in the shared object to become references
1355 to SHORTNAME in the regular object. This is what we expect
1356 when we override a function in a shared object: that the
1357 references in the shared object will be mapped to the
1358 definition in the regular object. */
1360 while (hi->root.type == bfd_link_hash_indirect
1361 || hi->root.type == bfd_link_hash_warning)
1362 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1364 h->root.type = bfd_link_hash_indirect;
1365 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1369 hi->ref_dynamic = 1;
1373 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1378 /* Now set HI to H, so that the following code will set the
1379 other fields correctly. */
1383 /* If there is a duplicate definition somewhere, then HI may not
1384 point to an indirect symbol. We will have reported an error to
1385 the user in that case. */
1387 if (hi->root.type == bfd_link_hash_indirect)
1389 struct elf_link_hash_entry *ht;
1391 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1392 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1394 /* See if the new flags lead us to realize that the symbol must
1406 if (hi->ref_regular)
1412 /* We also need to define an indirection from the nondefault version
1416 len = strlen (name);
1417 shortname = bfd_hash_allocate (&info->hash->table, len);
1418 if (shortname == NULL)
1420 memcpy (shortname, name, shortlen);
1421 memcpy (shortname + shortlen, p + 1, len - shortlen);
1423 /* Once again, merge with any existing symbol. */
1424 type_change_ok = FALSE;
1425 size_change_ok = FALSE;
1427 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1428 &hi, &skip, &override, &type_change_ok,
1437 /* Here SHORTNAME is a versioned name, so we don't expect to see
1438 the type of override we do in the case above unless it is
1439 overridden by a versioned definition. */
1440 if (hi->root.type != bfd_link_hash_defined
1441 && hi->root.type != bfd_link_hash_defweak)
1442 (*_bfd_error_handler)
1443 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1449 if (! (_bfd_generic_link_add_one_symbol
1450 (info, abfd, shortname, BSF_INDIRECT,
1451 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1453 hi = (struct elf_link_hash_entry *) bh;
1455 /* If there is a duplicate definition somewhere, then HI may not
1456 point to an indirect symbol. We will have reported an error
1457 to the user in that case. */
1459 if (hi->root.type == bfd_link_hash_indirect)
1461 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1463 /* See if the new flags lead us to realize that the symbol
1475 if (hi->ref_regular)
1485 /* This routine is used to export all defined symbols into the dynamic
1486 symbol table. It is called via elf_link_hash_traverse. */
1489 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1491 struct elf_info_failed *eif = data;
1493 /* Ignore indirect symbols. These are added by the versioning code. */
1494 if (h->root.type == bfd_link_hash_indirect)
1497 if (h->root.type == bfd_link_hash_warning)
1498 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1500 if (h->dynindx == -1
1504 struct bfd_elf_version_tree *t;
1505 struct bfd_elf_version_expr *d;
1507 for (t = eif->verdefs; t != NULL; t = t->next)
1509 if (t->globals.list != NULL)
1511 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1516 if (t->locals.list != NULL)
1518 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1527 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1538 /* Look through the symbols which are defined in other shared
1539 libraries and referenced here. Update the list of version
1540 dependencies. This will be put into the .gnu.version_r section.
1541 This function is called via elf_link_hash_traverse. */
1544 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1547 struct elf_find_verdep_info *rinfo = data;
1548 Elf_Internal_Verneed *t;
1549 Elf_Internal_Vernaux *a;
1552 if (h->root.type == bfd_link_hash_warning)
1553 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1555 /* We only care about symbols defined in shared objects with version
1560 || h->verinfo.verdef == NULL)
1563 /* See if we already know about this version. */
1564 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1566 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1569 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1570 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1576 /* This is a new version. Add it to tree we are building. */
1581 t = bfd_zalloc (rinfo->output_bfd, amt);
1584 rinfo->failed = TRUE;
1588 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1589 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1590 elf_tdata (rinfo->output_bfd)->verref = t;
1594 a = bfd_zalloc (rinfo->output_bfd, amt);
1596 /* Note that we are copying a string pointer here, and testing it
1597 above. If bfd_elf_string_from_elf_section is ever changed to
1598 discard the string data when low in memory, this will have to be
1600 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1602 a->vna_flags = h->verinfo.verdef->vd_flags;
1603 a->vna_nextptr = t->vn_auxptr;
1605 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1608 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1615 /* Figure out appropriate versions for all the symbols. We may not
1616 have the version number script until we have read all of the input
1617 files, so until that point we don't know which symbols should be
1618 local. This function is called via elf_link_hash_traverse. */
1621 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1623 struct elf_assign_sym_version_info *sinfo;
1624 struct bfd_link_info *info;
1625 const struct elf_backend_data *bed;
1626 struct elf_info_failed eif;
1633 if (h->root.type == bfd_link_hash_warning)
1634 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1636 /* Fix the symbol flags. */
1639 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1642 sinfo->failed = TRUE;
1646 /* We only need version numbers for symbols defined in regular
1648 if (!h->def_regular)
1651 bed = get_elf_backend_data (sinfo->output_bfd);
1652 p = strchr (h->root.root.string, ELF_VER_CHR);
1653 if (p != NULL && h->verinfo.vertree == NULL)
1655 struct bfd_elf_version_tree *t;
1660 /* There are two consecutive ELF_VER_CHR characters if this is
1661 not a hidden symbol. */
1663 if (*p == ELF_VER_CHR)
1669 /* If there is no version string, we can just return out. */
1677 /* Look for the version. If we find it, it is no longer weak. */
1678 for (t = sinfo->verdefs; t != NULL; t = t->next)
1680 if (strcmp (t->name, p) == 0)
1684 struct bfd_elf_version_expr *d;
1686 len = p - h->root.root.string;
1687 alc = bfd_malloc (len);
1690 memcpy (alc, h->root.root.string, len - 1);
1691 alc[len - 1] = '\0';
1692 if (alc[len - 2] == ELF_VER_CHR)
1693 alc[len - 2] = '\0';
1695 h->verinfo.vertree = t;
1699 if (t->globals.list != NULL)
1700 d = (*t->match) (&t->globals, NULL, alc);
1702 /* See if there is anything to force this symbol to
1704 if (d == NULL && t->locals.list != NULL)
1706 d = (*t->match) (&t->locals, NULL, alc);
1710 && ! info->export_dynamic)
1711 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1719 /* If we are building an application, we need to create a
1720 version node for this version. */
1721 if (t == NULL && info->executable)
1723 struct bfd_elf_version_tree **pp;
1726 /* If we aren't going to export this symbol, we don't need
1727 to worry about it. */
1728 if (h->dynindx == -1)
1732 t = bfd_zalloc (sinfo->output_bfd, amt);
1735 sinfo->failed = TRUE;
1740 t->name_indx = (unsigned int) -1;
1744 /* Don't count anonymous version tag. */
1745 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1747 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1749 t->vernum = version_index;
1753 h->verinfo.vertree = t;
1757 /* We could not find the version for a symbol when
1758 generating a shared archive. Return an error. */
1759 (*_bfd_error_handler)
1760 (_("%B: undefined versioned symbol name %s"),
1761 sinfo->output_bfd, h->root.root.string);
1762 bfd_set_error (bfd_error_bad_value);
1763 sinfo->failed = TRUE;
1771 /* If we don't have a version for this symbol, see if we can find
1773 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1775 struct bfd_elf_version_tree *t;
1776 struct bfd_elf_version_tree *local_ver;
1777 struct bfd_elf_version_expr *d;
1779 /* See if can find what version this symbol is in. If the
1780 symbol is supposed to be local, then don't actually register
1783 for (t = sinfo->verdefs; t != NULL; t = t->next)
1785 if (t->globals.list != NULL)
1787 bfd_boolean matched;
1791 while ((d = (*t->match) (&t->globals, d,
1792 h->root.root.string)) != NULL)
1797 /* There is a version without definition. Make
1798 the symbol the default definition for this
1800 h->verinfo.vertree = t;
1808 /* There is no undefined version for this symbol. Hide the
1810 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1813 if (t->locals.list != NULL)
1816 while ((d = (*t->match) (&t->locals, d,
1817 h->root.root.string)) != NULL)
1820 /* If the match is "*", keep looking for a more
1821 explicit, perhaps even global, match.
1822 XXX: Shouldn't this be !d->wildcard instead? */
1823 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1832 if (local_ver != NULL)
1834 h->verinfo.vertree = local_ver;
1835 if (h->dynindx != -1
1837 && ! info->export_dynamic)
1839 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1847 /* Read and swap the relocs from the section indicated by SHDR. This
1848 may be either a REL or a RELA section. The relocations are
1849 translated into RELA relocations and stored in INTERNAL_RELOCS,
1850 which should have already been allocated to contain enough space.
1851 The EXTERNAL_RELOCS are a buffer where the external form of the
1852 relocations should be stored.
1854 Returns FALSE if something goes wrong. */
1857 elf_link_read_relocs_from_section (bfd *abfd,
1859 Elf_Internal_Shdr *shdr,
1860 void *external_relocs,
1861 Elf_Internal_Rela *internal_relocs)
1863 const struct elf_backend_data *bed;
1864 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1865 const bfd_byte *erela;
1866 const bfd_byte *erelaend;
1867 Elf_Internal_Rela *irela;
1868 Elf_Internal_Shdr *symtab_hdr;
1871 /* Position ourselves at the start of the section. */
1872 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1875 /* Read the relocations. */
1876 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1879 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1880 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1882 bed = get_elf_backend_data (abfd);
1884 /* Convert the external relocations to the internal format. */
1885 if (shdr->sh_entsize == bed->s->sizeof_rel)
1886 swap_in = bed->s->swap_reloc_in;
1887 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1888 swap_in = bed->s->swap_reloca_in;
1891 bfd_set_error (bfd_error_wrong_format);
1895 erela = external_relocs;
1896 erelaend = erela + shdr->sh_size;
1897 irela = internal_relocs;
1898 while (erela < erelaend)
1902 (*swap_in) (abfd, erela, irela);
1903 r_symndx = ELF32_R_SYM (irela->r_info);
1904 if (bed->s->arch_size == 64)
1906 if ((size_t) r_symndx >= nsyms)
1908 (*_bfd_error_handler)
1909 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1910 " for offset 0x%lx in section `%A'"),
1912 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1913 bfd_set_error (bfd_error_bad_value);
1916 irela += bed->s->int_rels_per_ext_rel;
1917 erela += shdr->sh_entsize;
1923 /* Read and swap the relocs for a section O. They may have been
1924 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1925 not NULL, they are used as buffers to read into. They are known to
1926 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1927 the return value is allocated using either malloc or bfd_alloc,
1928 according to the KEEP_MEMORY argument. If O has two relocation
1929 sections (both REL and RELA relocations), then the REL_HDR
1930 relocations will appear first in INTERNAL_RELOCS, followed by the
1931 REL_HDR2 relocations. */
1934 _bfd_elf_link_read_relocs (bfd *abfd,
1936 void *external_relocs,
1937 Elf_Internal_Rela *internal_relocs,
1938 bfd_boolean keep_memory)
1940 Elf_Internal_Shdr *rel_hdr;
1941 void *alloc1 = NULL;
1942 Elf_Internal_Rela *alloc2 = NULL;
1943 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1945 if (elf_section_data (o)->relocs != NULL)
1946 return elf_section_data (o)->relocs;
1948 if (o->reloc_count == 0)
1951 rel_hdr = &elf_section_data (o)->rel_hdr;
1953 if (internal_relocs == NULL)
1957 size = o->reloc_count;
1958 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1960 internal_relocs = bfd_alloc (abfd, size);
1962 internal_relocs = alloc2 = bfd_malloc (size);
1963 if (internal_relocs == NULL)
1967 if (external_relocs == NULL)
1969 bfd_size_type size = rel_hdr->sh_size;
1971 if (elf_section_data (o)->rel_hdr2)
1972 size += elf_section_data (o)->rel_hdr2->sh_size;
1973 alloc1 = bfd_malloc (size);
1976 external_relocs = alloc1;
1979 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
1983 if (elf_section_data (o)->rel_hdr2
1984 && (!elf_link_read_relocs_from_section
1986 elf_section_data (o)->rel_hdr2,
1987 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
1988 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
1989 * bed->s->int_rels_per_ext_rel))))
1992 /* Cache the results for next time, if we can. */
1994 elf_section_data (o)->relocs = internal_relocs;
1999 /* Don't free alloc2, since if it was allocated we are passing it
2000 back (under the name of internal_relocs). */
2002 return internal_relocs;
2012 /* Compute the size of, and allocate space for, REL_HDR which is the
2013 section header for a section containing relocations for O. */
2016 _bfd_elf_link_size_reloc_section (bfd *abfd,
2017 Elf_Internal_Shdr *rel_hdr,
2020 bfd_size_type reloc_count;
2021 bfd_size_type num_rel_hashes;
2023 /* Figure out how many relocations there will be. */
2024 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2025 reloc_count = elf_section_data (o)->rel_count;
2027 reloc_count = elf_section_data (o)->rel_count2;
2029 num_rel_hashes = o->reloc_count;
2030 if (num_rel_hashes < reloc_count)
2031 num_rel_hashes = reloc_count;
2033 /* That allows us to calculate the size of the section. */
2034 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2036 /* The contents field must last into write_object_contents, so we
2037 allocate it with bfd_alloc rather than malloc. Also since we
2038 cannot be sure that the contents will actually be filled in,
2039 we zero the allocated space. */
2040 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2041 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2044 /* We only allocate one set of hash entries, so we only do it the
2045 first time we are called. */
2046 if (elf_section_data (o)->rel_hashes == NULL
2049 struct elf_link_hash_entry **p;
2051 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2055 elf_section_data (o)->rel_hashes = p;
2061 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2062 originated from the section given by INPUT_REL_HDR) to the
2066 _bfd_elf_link_output_relocs (bfd *output_bfd,
2067 asection *input_section,
2068 Elf_Internal_Shdr *input_rel_hdr,
2069 Elf_Internal_Rela *internal_relocs)
2071 Elf_Internal_Rela *irela;
2072 Elf_Internal_Rela *irelaend;
2074 Elf_Internal_Shdr *output_rel_hdr;
2075 asection *output_section;
2076 unsigned int *rel_countp = NULL;
2077 const struct elf_backend_data *bed;
2078 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2080 output_section = input_section->output_section;
2081 output_rel_hdr = NULL;
2083 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2084 == input_rel_hdr->sh_entsize)
2086 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2087 rel_countp = &elf_section_data (output_section)->rel_count;
2089 else if (elf_section_data (output_section)->rel_hdr2
2090 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2091 == input_rel_hdr->sh_entsize))
2093 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2094 rel_countp = &elf_section_data (output_section)->rel_count2;
2098 (*_bfd_error_handler)
2099 (_("%B: relocation size mismatch in %B section %A"),
2100 output_bfd, input_section->owner, input_section);
2101 bfd_set_error (bfd_error_wrong_object_format);
2105 bed = get_elf_backend_data (output_bfd);
2106 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2107 swap_out = bed->s->swap_reloc_out;
2108 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2109 swap_out = bed->s->swap_reloca_out;
2113 erel = output_rel_hdr->contents;
2114 erel += *rel_countp * input_rel_hdr->sh_entsize;
2115 irela = internal_relocs;
2116 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2117 * bed->s->int_rels_per_ext_rel);
2118 while (irela < irelaend)
2120 (*swap_out) (output_bfd, irela, erel);
2121 irela += bed->s->int_rels_per_ext_rel;
2122 erel += input_rel_hdr->sh_entsize;
2125 /* Bump the counter, so that we know where to add the next set of
2127 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2132 /* Fix up the flags for a symbol. This handles various cases which
2133 can only be fixed after all the input files are seen. This is
2134 currently called by both adjust_dynamic_symbol and
2135 assign_sym_version, which is unnecessary but perhaps more robust in
2136 the face of future changes. */
2139 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2140 struct elf_info_failed *eif)
2142 /* If this symbol was mentioned in a non-ELF file, try to set
2143 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2144 permit a non-ELF file to correctly refer to a symbol defined in
2145 an ELF dynamic object. */
2148 while (h->root.type == bfd_link_hash_indirect)
2149 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2151 if (h->root.type != bfd_link_hash_defined
2152 && h->root.type != bfd_link_hash_defweak)
2155 h->ref_regular_nonweak = 1;
2159 if (h->root.u.def.section->owner != NULL
2160 && (bfd_get_flavour (h->root.u.def.section->owner)
2161 == bfd_target_elf_flavour))
2164 h->ref_regular_nonweak = 1;
2170 if (h->dynindx == -1
2174 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2183 /* Unfortunately, NON_ELF is only correct if the symbol
2184 was first seen in a non-ELF file. Fortunately, if the symbol
2185 was first seen in an ELF file, we're probably OK unless the
2186 symbol was defined in a non-ELF file. Catch that case here.
2187 FIXME: We're still in trouble if the symbol was first seen in
2188 a dynamic object, and then later in a non-ELF regular object. */
2189 if ((h->root.type == bfd_link_hash_defined
2190 || h->root.type == bfd_link_hash_defweak)
2192 && (h->root.u.def.section->owner != NULL
2193 ? (bfd_get_flavour (h->root.u.def.section->owner)
2194 != bfd_target_elf_flavour)
2195 : (bfd_is_abs_section (h->root.u.def.section)
2196 && !h->def_dynamic)))
2200 /* If this is a final link, and the symbol was defined as a common
2201 symbol in a regular object file, and there was no definition in
2202 any dynamic object, then the linker will have allocated space for
2203 the symbol in a common section but the DEF_REGULAR
2204 flag will not have been set. */
2205 if (h->root.type == bfd_link_hash_defined
2209 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2212 /* If -Bsymbolic was used (which means to bind references to global
2213 symbols to the definition within the shared object), and this
2214 symbol was defined in a regular object, then it actually doesn't
2215 need a PLT entry. Likewise, if the symbol has non-default
2216 visibility. If the symbol has hidden or internal visibility, we
2217 will force it local. */
2219 && eif->info->shared
2220 && is_elf_hash_table (eif->info->hash)
2221 && (eif->info->symbolic
2222 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2225 const struct elf_backend_data *bed;
2226 bfd_boolean force_local;
2228 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2230 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2231 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2232 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2235 /* If a weak undefined symbol has non-default visibility, we also
2236 hide it from the dynamic linker. */
2237 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2238 && h->root.type == bfd_link_hash_undefweak)
2240 const struct elf_backend_data *bed;
2241 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2242 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2245 /* If this is a weak defined symbol in a dynamic object, and we know
2246 the real definition in the dynamic object, copy interesting flags
2247 over to the real definition. */
2248 if (h->u.weakdef != NULL)
2250 struct elf_link_hash_entry *weakdef;
2252 weakdef = h->u.weakdef;
2253 if (h->root.type == bfd_link_hash_indirect)
2254 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2256 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2257 || h->root.type == bfd_link_hash_defweak);
2258 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2259 || weakdef->root.type == bfd_link_hash_defweak);
2260 BFD_ASSERT (weakdef->def_dynamic);
2262 /* If the real definition is defined by a regular object file,
2263 don't do anything special. See the longer description in
2264 _bfd_elf_adjust_dynamic_symbol, below. */
2265 if (weakdef->def_regular)
2266 h->u.weakdef = NULL;
2269 const struct elf_backend_data *bed;
2271 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2272 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2279 /* Make the backend pick a good value for a dynamic symbol. This is
2280 called via elf_link_hash_traverse, and also calls itself
2284 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2286 struct elf_info_failed *eif = data;
2288 const struct elf_backend_data *bed;
2290 if (! is_elf_hash_table (eif->info->hash))
2293 if (h->root.type == bfd_link_hash_warning)
2295 h->plt = elf_hash_table (eif->info)->init_offset;
2296 h->got = elf_hash_table (eif->info)->init_offset;
2298 /* When warning symbols are created, they **replace** the "real"
2299 entry in the hash table, thus we never get to see the real
2300 symbol in a hash traversal. So look at it now. */
2301 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2304 /* Ignore indirect symbols. These are added by the versioning code. */
2305 if (h->root.type == bfd_link_hash_indirect)
2308 /* Fix the symbol flags. */
2309 if (! _bfd_elf_fix_symbol_flags (h, eif))
2312 /* If this symbol does not require a PLT entry, and it is not
2313 defined by a dynamic object, or is not referenced by a regular
2314 object, ignore it. We do have to handle a weak defined symbol,
2315 even if no regular object refers to it, if we decided to add it
2316 to the dynamic symbol table. FIXME: Do we normally need to worry
2317 about symbols which are defined by one dynamic object and
2318 referenced by another one? */
2323 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2325 h->plt = elf_hash_table (eif->info)->init_offset;
2329 /* If we've already adjusted this symbol, don't do it again. This
2330 can happen via a recursive call. */
2331 if (h->dynamic_adjusted)
2334 /* Don't look at this symbol again. Note that we must set this
2335 after checking the above conditions, because we may look at a
2336 symbol once, decide not to do anything, and then get called
2337 recursively later after REF_REGULAR is set below. */
2338 h->dynamic_adjusted = 1;
2340 /* If this is a weak definition, and we know a real definition, and
2341 the real symbol is not itself defined by a regular object file,
2342 then get a good value for the real definition. We handle the
2343 real symbol first, for the convenience of the backend routine.
2345 Note that there is a confusing case here. If the real definition
2346 is defined by a regular object file, we don't get the real symbol
2347 from the dynamic object, but we do get the weak symbol. If the
2348 processor backend uses a COPY reloc, then if some routine in the
2349 dynamic object changes the real symbol, we will not see that
2350 change in the corresponding weak symbol. This is the way other
2351 ELF linkers work as well, and seems to be a result of the shared
2354 I will clarify this issue. Most SVR4 shared libraries define the
2355 variable _timezone and define timezone as a weak synonym. The
2356 tzset call changes _timezone. If you write
2357 extern int timezone;
2359 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2360 you might expect that, since timezone is a synonym for _timezone,
2361 the same number will print both times. However, if the processor
2362 backend uses a COPY reloc, then actually timezone will be copied
2363 into your process image, and, since you define _timezone
2364 yourself, _timezone will not. Thus timezone and _timezone will
2365 wind up at different memory locations. The tzset call will set
2366 _timezone, leaving timezone unchanged. */
2368 if (h->u.weakdef != NULL)
2370 /* If we get to this point, we know there is an implicit
2371 reference by a regular object file via the weak symbol H.
2372 FIXME: Is this really true? What if the traversal finds
2373 H->U.WEAKDEF before it finds H? */
2374 h->u.weakdef->ref_regular = 1;
2376 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2380 /* If a symbol has no type and no size and does not require a PLT
2381 entry, then we are probably about to do the wrong thing here: we
2382 are probably going to create a COPY reloc for an empty object.
2383 This case can arise when a shared object is built with assembly
2384 code, and the assembly code fails to set the symbol type. */
2386 && h->type == STT_NOTYPE
2388 (*_bfd_error_handler)
2389 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2390 h->root.root.string);
2392 dynobj = elf_hash_table (eif->info)->dynobj;
2393 bed = get_elf_backend_data (dynobj);
2394 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2403 /* Adjust all external symbols pointing into SEC_MERGE sections
2404 to reflect the object merging within the sections. */
2407 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2411 if (h->root.type == bfd_link_hash_warning)
2412 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2414 if ((h->root.type == bfd_link_hash_defined
2415 || h->root.type == bfd_link_hash_defweak)
2416 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2417 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2419 bfd *output_bfd = data;
2421 h->root.u.def.value =
2422 _bfd_merged_section_offset (output_bfd,
2423 &h->root.u.def.section,
2424 elf_section_data (sec)->sec_info,
2425 h->root.u.def.value);
2431 /* Returns false if the symbol referred to by H should be considered
2432 to resolve local to the current module, and true if it should be
2433 considered to bind dynamically. */
2436 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2437 struct bfd_link_info *info,
2438 bfd_boolean ignore_protected)
2440 bfd_boolean binding_stays_local_p;
2445 while (h->root.type == bfd_link_hash_indirect
2446 || h->root.type == bfd_link_hash_warning)
2447 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2449 /* If it was forced local, then clearly it's not dynamic. */
2450 if (h->dynindx == -1)
2452 if (h->forced_local)
2455 /* Identify the cases where name binding rules say that a
2456 visible symbol resolves locally. */
2457 binding_stays_local_p = info->executable || info->symbolic;
2459 switch (ELF_ST_VISIBILITY (h->other))
2466 /* Proper resolution for function pointer equality may require
2467 that these symbols perhaps be resolved dynamically, even though
2468 we should be resolving them to the current module. */
2469 if (!ignore_protected)
2470 binding_stays_local_p = TRUE;
2477 /* If it isn't defined locally, then clearly it's dynamic. */
2478 if (!h->def_regular)
2481 /* Otherwise, the symbol is dynamic if binding rules don't tell
2482 us that it remains local. */
2483 return !binding_stays_local_p;
2486 /* Return true if the symbol referred to by H should be considered
2487 to resolve local to the current module, and false otherwise. Differs
2488 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2489 undefined symbols and weak symbols. */
2492 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2493 struct bfd_link_info *info,
2494 bfd_boolean local_protected)
2496 /* If it's a local sym, of course we resolve locally. */
2500 /* Common symbols that become definitions don't get the DEF_REGULAR
2501 flag set, so test it first, and don't bail out. */
2502 if (ELF_COMMON_DEF_P (h))
2504 /* If we don't have a definition in a regular file, then we can't
2505 resolve locally. The sym is either undefined or dynamic. */
2506 else if (!h->def_regular)
2509 /* Forced local symbols resolve locally. */
2510 if (h->forced_local)
2513 /* As do non-dynamic symbols. */
2514 if (h->dynindx == -1)
2517 /* At this point, we know the symbol is defined and dynamic. In an
2518 executable it must resolve locally, likewise when building symbolic
2519 shared libraries. */
2520 if (info->executable || info->symbolic)
2523 /* Now deal with defined dynamic symbols in shared libraries. Ones
2524 with default visibility might not resolve locally. */
2525 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2528 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2529 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2532 /* Function pointer equality tests may require that STV_PROTECTED
2533 symbols be treated as dynamic symbols, even when we know that the
2534 dynamic linker will resolve them locally. */
2535 return local_protected;
2538 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2539 aligned. Returns the first TLS output section. */
2541 struct bfd_section *
2542 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2544 struct bfd_section *sec, *tls;
2545 unsigned int align = 0;
2547 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2548 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2552 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2553 if (sec->alignment_power > align)
2554 align = sec->alignment_power;
2556 elf_hash_table (info)->tls_sec = tls;
2558 /* Ensure the alignment of the first section is the largest alignment,
2559 so that the tls segment starts aligned. */
2561 tls->alignment_power = align;
2566 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2568 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2569 Elf_Internal_Sym *sym)
2571 /* Local symbols do not count, but target specific ones might. */
2572 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2573 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2576 /* Function symbols do not count. */
2577 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2580 /* If the section is undefined, then so is the symbol. */
2581 if (sym->st_shndx == SHN_UNDEF)
2584 /* If the symbol is defined in the common section, then
2585 it is a common definition and so does not count. */
2586 if (sym->st_shndx == SHN_COMMON)
2589 /* If the symbol is in a target specific section then we
2590 must rely upon the backend to tell us what it is. */
2591 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2592 /* FIXME - this function is not coded yet:
2594 return _bfd_is_global_symbol_definition (abfd, sym);
2596 Instead for now assume that the definition is not global,
2597 Even if this is wrong, at least the linker will behave
2598 in the same way that it used to do. */
2604 /* Search the symbol table of the archive element of the archive ABFD
2605 whose archive map contains a mention of SYMDEF, and determine if
2606 the symbol is defined in this element. */
2608 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2610 Elf_Internal_Shdr * hdr;
2611 bfd_size_type symcount;
2612 bfd_size_type extsymcount;
2613 bfd_size_type extsymoff;
2614 Elf_Internal_Sym *isymbuf;
2615 Elf_Internal_Sym *isym;
2616 Elf_Internal_Sym *isymend;
2619 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2623 if (! bfd_check_format (abfd, bfd_object))
2626 /* If we have already included the element containing this symbol in the
2627 link then we do not need to include it again. Just claim that any symbol
2628 it contains is not a definition, so that our caller will not decide to
2629 (re)include this element. */
2630 if (abfd->archive_pass)
2633 /* Select the appropriate symbol table. */
2634 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2635 hdr = &elf_tdata (abfd)->symtab_hdr;
2637 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2639 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2641 /* The sh_info field of the symtab header tells us where the
2642 external symbols start. We don't care about the local symbols. */
2643 if (elf_bad_symtab (abfd))
2645 extsymcount = symcount;
2650 extsymcount = symcount - hdr->sh_info;
2651 extsymoff = hdr->sh_info;
2654 if (extsymcount == 0)
2657 /* Read in the symbol table. */
2658 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2660 if (isymbuf == NULL)
2663 /* Scan the symbol table looking for SYMDEF. */
2665 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2669 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2674 if (strcmp (name, symdef->name) == 0)
2676 result = is_global_data_symbol_definition (abfd, isym);
2686 /* Add an entry to the .dynamic table. */
2689 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2693 struct elf_link_hash_table *hash_table;
2694 const struct elf_backend_data *bed;
2696 bfd_size_type newsize;
2697 bfd_byte *newcontents;
2698 Elf_Internal_Dyn dyn;
2700 hash_table = elf_hash_table (info);
2701 if (! is_elf_hash_table (hash_table))
2704 if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
2706 (_("warning: creating a DT_TEXTREL in a shared object."));
2708 bed = get_elf_backend_data (hash_table->dynobj);
2709 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2710 BFD_ASSERT (s != NULL);
2712 newsize = s->size + bed->s->sizeof_dyn;
2713 newcontents = bfd_realloc (s->contents, newsize);
2714 if (newcontents == NULL)
2718 dyn.d_un.d_val = val;
2719 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2722 s->contents = newcontents;
2727 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2728 otherwise just check whether one already exists. Returns -1 on error,
2729 1 if a DT_NEEDED tag already exists, and 0 on success. */
2732 elf_add_dt_needed_tag (struct bfd_link_info *info,
2736 struct elf_link_hash_table *hash_table;
2737 bfd_size_type oldsize;
2738 bfd_size_type strindex;
2740 hash_table = elf_hash_table (info);
2741 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2742 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2743 if (strindex == (bfd_size_type) -1)
2746 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2749 const struct elf_backend_data *bed;
2752 bed = get_elf_backend_data (hash_table->dynobj);
2753 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2754 BFD_ASSERT (sdyn != NULL);
2756 for (extdyn = sdyn->contents;
2757 extdyn < sdyn->contents + sdyn->size;
2758 extdyn += bed->s->sizeof_dyn)
2760 Elf_Internal_Dyn dyn;
2762 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2763 if (dyn.d_tag == DT_NEEDED
2764 && dyn.d_un.d_val == strindex)
2766 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2774 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2778 /* We were just checking for existence of the tag. */
2779 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2784 /* Sort symbol by value and section. */
2786 elf_sort_symbol (const void *arg1, const void *arg2)
2788 const struct elf_link_hash_entry *h1;
2789 const struct elf_link_hash_entry *h2;
2790 bfd_signed_vma vdiff;
2792 h1 = *(const struct elf_link_hash_entry **) arg1;
2793 h2 = *(const struct elf_link_hash_entry **) arg2;
2794 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2796 return vdiff > 0 ? 1 : -1;
2799 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2801 return sdiff > 0 ? 1 : -1;
2806 /* This function is used to adjust offsets into .dynstr for
2807 dynamic symbols. This is called via elf_link_hash_traverse. */
2810 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2812 struct elf_strtab_hash *dynstr = data;
2814 if (h->root.type == bfd_link_hash_warning)
2815 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2817 if (h->dynindx != -1)
2818 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2822 /* Assign string offsets in .dynstr, update all structures referencing
2826 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2828 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2829 struct elf_link_local_dynamic_entry *entry;
2830 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2831 bfd *dynobj = hash_table->dynobj;
2834 const struct elf_backend_data *bed;
2837 _bfd_elf_strtab_finalize (dynstr);
2838 size = _bfd_elf_strtab_size (dynstr);
2840 bed = get_elf_backend_data (dynobj);
2841 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2842 BFD_ASSERT (sdyn != NULL);
2844 /* Update all .dynamic entries referencing .dynstr strings. */
2845 for (extdyn = sdyn->contents;
2846 extdyn < sdyn->contents + sdyn->size;
2847 extdyn += bed->s->sizeof_dyn)
2849 Elf_Internal_Dyn dyn;
2851 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
2855 dyn.d_un.d_val = size;
2863 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2868 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
2871 /* Now update local dynamic symbols. */
2872 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
2873 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2874 entry->isym.st_name);
2876 /* And the rest of dynamic symbols. */
2877 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
2879 /* Adjust version definitions. */
2880 if (elf_tdata (output_bfd)->cverdefs)
2885 Elf_Internal_Verdef def;
2886 Elf_Internal_Verdaux defaux;
2888 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2892 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2894 p += sizeof (Elf_External_Verdef);
2895 for (i = 0; i < def.vd_cnt; ++i)
2897 _bfd_elf_swap_verdaux_in (output_bfd,
2898 (Elf_External_Verdaux *) p, &defaux);
2899 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2901 _bfd_elf_swap_verdaux_out (output_bfd,
2902 &defaux, (Elf_External_Verdaux *) p);
2903 p += sizeof (Elf_External_Verdaux);
2906 while (def.vd_next);
2909 /* Adjust version references. */
2910 if (elf_tdata (output_bfd)->verref)
2915 Elf_Internal_Verneed need;
2916 Elf_Internal_Vernaux needaux;
2918 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2922 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2924 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2925 _bfd_elf_swap_verneed_out (output_bfd, &need,
2926 (Elf_External_Verneed *) p);
2927 p += sizeof (Elf_External_Verneed);
2928 for (i = 0; i < need.vn_cnt; ++i)
2930 _bfd_elf_swap_vernaux_in (output_bfd,
2931 (Elf_External_Vernaux *) p, &needaux);
2932 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2934 _bfd_elf_swap_vernaux_out (output_bfd,
2936 (Elf_External_Vernaux *) p);
2937 p += sizeof (Elf_External_Vernaux);
2940 while (need.vn_next);
2946 /* Add symbols from an ELF object file to the linker hash table. */
2949 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2951 bfd_boolean (*add_symbol_hook)
2952 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
2953 const char **, flagword *, asection **, bfd_vma *);
2954 bfd_boolean (*check_relocs)
2955 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
2956 bfd_boolean (*check_directives)
2957 (bfd *, struct bfd_link_info *);
2958 bfd_boolean collect;
2959 Elf_Internal_Shdr *hdr;
2960 bfd_size_type symcount;
2961 bfd_size_type extsymcount;
2962 bfd_size_type extsymoff;
2963 struct elf_link_hash_entry **sym_hash;
2964 bfd_boolean dynamic;
2965 Elf_External_Versym *extversym = NULL;
2966 Elf_External_Versym *ever;
2967 struct elf_link_hash_entry *weaks;
2968 struct elf_link_hash_entry **nondeflt_vers = NULL;
2969 bfd_size_type nondeflt_vers_cnt = 0;
2970 Elf_Internal_Sym *isymbuf = NULL;
2971 Elf_Internal_Sym *isym;
2972 Elf_Internal_Sym *isymend;
2973 const struct elf_backend_data *bed;
2974 bfd_boolean add_needed;
2975 struct elf_link_hash_table * hash_table;
2978 hash_table = elf_hash_table (info);
2980 bed = get_elf_backend_data (abfd);
2981 add_symbol_hook = bed->elf_add_symbol_hook;
2982 collect = bed->collect;
2984 if ((abfd->flags & DYNAMIC) == 0)
2990 /* You can't use -r against a dynamic object. Also, there's no
2991 hope of using a dynamic object which does not exactly match
2992 the format of the output file. */
2993 if (info->relocatable
2994 || !is_elf_hash_table (hash_table)
2995 || hash_table->root.creator != abfd->xvec)
2997 if (info->relocatable)
2998 bfd_set_error (bfd_error_invalid_operation);
3000 bfd_set_error (bfd_error_wrong_format);
3005 /* As a GNU extension, any input sections which are named
3006 .gnu.warning.SYMBOL are treated as warning symbols for the given
3007 symbol. This differs from .gnu.warning sections, which generate
3008 warnings when they are included in an output file. */
3009 if (info->executable)
3013 for (s = abfd->sections; s != NULL; s = s->next)
3017 name = bfd_get_section_name (abfd, s);
3018 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3022 bfd_size_type prefix_len;
3023 const char * gnu_warning_prefix = _("warning: ");
3025 name += sizeof ".gnu.warning." - 1;
3027 /* If this is a shared object, then look up the symbol
3028 in the hash table. If it is there, and it is already
3029 been defined, then we will not be using the entry
3030 from this shared object, so we don't need to warn.
3031 FIXME: If we see the definition in a regular object
3032 later on, we will warn, but we shouldn't. The only
3033 fix is to keep track of what warnings we are supposed
3034 to emit, and then handle them all at the end of the
3038 struct elf_link_hash_entry *h;
3040 h = elf_link_hash_lookup (hash_table, name,
3041 FALSE, FALSE, TRUE);
3043 /* FIXME: What about bfd_link_hash_common? */
3045 && (h->root.type == bfd_link_hash_defined
3046 || h->root.type == bfd_link_hash_defweak))
3048 /* We don't want to issue this warning. Clobber
3049 the section size so that the warning does not
3050 get copied into the output file. */
3057 prefix_len = strlen (gnu_warning_prefix);
3058 msg = bfd_alloc (abfd, prefix_len + sz + 1);
3062 strcpy (msg, gnu_warning_prefix);
3063 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
3066 msg[prefix_len + sz] = '\0';
3068 if (! (_bfd_generic_link_add_one_symbol
3069 (info, abfd, name, BSF_WARNING, s, 0, msg,
3070 FALSE, collect, NULL)))
3073 if (! info->relocatable)
3075 /* Clobber the section size so that the warning does
3076 not get copied into the output file. */
3086 /* If we are creating a shared library, create all the dynamic
3087 sections immediately. We need to attach them to something,
3088 so we attach them to this BFD, provided it is the right
3089 format. FIXME: If there are no input BFD's of the same
3090 format as the output, we can't make a shared library. */
3092 && is_elf_hash_table (hash_table)
3093 && hash_table->root.creator == abfd->xvec
3094 && ! hash_table->dynamic_sections_created)
3096 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3100 else if (!is_elf_hash_table (hash_table))
3105 const char *soname = NULL;
3106 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3109 /* ld --just-symbols and dynamic objects don't mix very well.
3110 Test for --just-symbols by looking at info set up by
3111 _bfd_elf_link_just_syms. */
3112 if ((s = abfd->sections) != NULL
3113 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3116 /* If this dynamic lib was specified on the command line with
3117 --as-needed in effect, then we don't want to add a DT_NEEDED
3118 tag unless the lib is actually used. Similary for libs brought
3119 in by another lib's DT_NEEDED. When --no-add-needed is used
3120 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3121 any dynamic library in DT_NEEDED tags in the dynamic lib at
3123 add_needed = (elf_dyn_lib_class (abfd)
3124 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3125 | DYN_NO_NEEDED)) == 0;
3127 s = bfd_get_section_by_name (abfd, ".dynamic");
3133 unsigned long shlink;
3135 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3136 goto error_free_dyn;
3138 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3140 goto error_free_dyn;
3141 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3143 for (extdyn = dynbuf;
3144 extdyn < dynbuf + s->size;
3145 extdyn += bed->s->sizeof_dyn)
3147 Elf_Internal_Dyn dyn;
3149 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3150 if (dyn.d_tag == DT_SONAME)
3152 unsigned int tagv = dyn.d_un.d_val;
3153 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3155 goto error_free_dyn;
3157 if (dyn.d_tag == DT_NEEDED)
3159 struct bfd_link_needed_list *n, **pn;
3161 unsigned int tagv = dyn.d_un.d_val;
3163 amt = sizeof (struct bfd_link_needed_list);
3164 n = bfd_alloc (abfd, amt);
3165 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3166 if (n == NULL || fnm == NULL)
3167 goto error_free_dyn;
3168 amt = strlen (fnm) + 1;
3169 anm = bfd_alloc (abfd, amt);
3171 goto error_free_dyn;
3172 memcpy (anm, fnm, amt);
3176 for (pn = & hash_table->needed;
3182 if (dyn.d_tag == DT_RUNPATH)
3184 struct bfd_link_needed_list *n, **pn;
3186 unsigned int tagv = dyn.d_un.d_val;
3188 amt = sizeof (struct bfd_link_needed_list);
3189 n = bfd_alloc (abfd, amt);
3190 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3191 if (n == NULL || fnm == NULL)
3192 goto error_free_dyn;
3193 amt = strlen (fnm) + 1;
3194 anm = bfd_alloc (abfd, amt);
3196 goto error_free_dyn;
3197 memcpy (anm, fnm, amt);
3201 for (pn = & runpath;
3207 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3208 if (!runpath && dyn.d_tag == DT_RPATH)
3210 struct bfd_link_needed_list *n, **pn;
3212 unsigned int tagv = dyn.d_un.d_val;
3214 amt = sizeof (struct bfd_link_needed_list);
3215 n = bfd_alloc (abfd, amt);
3216 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3217 if (n == NULL || fnm == NULL)
3218 goto error_free_dyn;
3219 amt = strlen (fnm) + 1;
3220 anm = bfd_alloc (abfd, amt);
3227 memcpy (anm, fnm, amt);
3242 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3243 frees all more recently bfd_alloc'd blocks as well. */
3249 struct bfd_link_needed_list **pn;
3250 for (pn = & hash_table->runpath;
3257 /* We do not want to include any of the sections in a dynamic
3258 object in the output file. We hack by simply clobbering the
3259 list of sections in the BFD. This could be handled more
3260 cleanly by, say, a new section flag; the existing
3261 SEC_NEVER_LOAD flag is not the one we want, because that one
3262 still implies that the section takes up space in the output
3264 bfd_section_list_clear (abfd);
3266 /* If this is the first dynamic object found in the link, create
3267 the special sections required for dynamic linking. */
3268 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3271 /* Find the name to use in a DT_NEEDED entry that refers to this
3272 object. If the object has a DT_SONAME entry, we use it.
3273 Otherwise, if the generic linker stuck something in
3274 elf_dt_name, we use that. Otherwise, we just use the file
3276 if (soname == NULL || *soname == '\0')
3278 soname = elf_dt_name (abfd);
3279 if (soname == NULL || *soname == '\0')
3280 soname = bfd_get_filename (abfd);
3283 /* Save the SONAME because sometimes the linker emulation code
3284 will need to know it. */
3285 elf_dt_name (abfd) = soname;
3287 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3291 /* If we have already included this dynamic object in the
3292 link, just ignore it. There is no reason to include a
3293 particular dynamic object more than once. */
3298 /* If this is a dynamic object, we always link against the .dynsym
3299 symbol table, not the .symtab symbol table. The dynamic linker
3300 will only see the .dynsym symbol table, so there is no reason to
3301 look at .symtab for a dynamic object. */
3303 if (! dynamic || elf_dynsymtab (abfd) == 0)
3304 hdr = &elf_tdata (abfd)->symtab_hdr;
3306 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3308 symcount = hdr->sh_size / bed->s->sizeof_sym;
3310 /* The sh_info field of the symtab header tells us where the
3311 external symbols start. We don't care about the local symbols at
3313 if (elf_bad_symtab (abfd))
3315 extsymcount = symcount;
3320 extsymcount = symcount - hdr->sh_info;
3321 extsymoff = hdr->sh_info;
3325 if (extsymcount != 0)
3327 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3329 if (isymbuf == NULL)
3332 /* We store a pointer to the hash table entry for each external
3334 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3335 sym_hash = bfd_alloc (abfd, amt);
3336 if (sym_hash == NULL)
3337 goto error_free_sym;
3338 elf_sym_hashes (abfd) = sym_hash;
3343 /* Read in any version definitions. */
3344 if (! _bfd_elf_slurp_version_tables (abfd))
3345 goto error_free_sym;
3347 /* Read in the symbol versions, but don't bother to convert them
3348 to internal format. */
3349 if (elf_dynversym (abfd) != 0)
3351 Elf_Internal_Shdr *versymhdr;
3353 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3354 extversym = bfd_malloc (versymhdr->sh_size);
3355 if (extversym == NULL)
3356 goto error_free_sym;
3357 amt = versymhdr->sh_size;
3358 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3359 || bfd_bread (extversym, amt, abfd) != amt)
3360 goto error_free_vers;
3366 ever = extversym != NULL ? extversym + extsymoff : NULL;
3367 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3369 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3376 struct elf_link_hash_entry *h;
3377 bfd_boolean definition;
3378 bfd_boolean size_change_ok;
3379 bfd_boolean type_change_ok;
3380 bfd_boolean new_weakdef;
3381 bfd_boolean override;
3382 unsigned int old_alignment;
3387 flags = BSF_NO_FLAGS;
3389 value = isym->st_value;
3392 bind = ELF_ST_BIND (isym->st_info);
3393 if (bind == STB_LOCAL)
3395 /* This should be impossible, since ELF requires that all
3396 global symbols follow all local symbols, and that sh_info
3397 point to the first global symbol. Unfortunately, Irix 5
3401 else if (bind == STB_GLOBAL)
3403 if (isym->st_shndx != SHN_UNDEF
3404 && isym->st_shndx != SHN_COMMON)
3407 else if (bind == STB_WEAK)
3411 /* Leave it up to the processor backend. */
3414 if (isym->st_shndx == SHN_UNDEF)
3415 sec = bfd_und_section_ptr;
3416 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3418 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3420 sec = bfd_abs_section_ptr;
3421 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3424 else if (isym->st_shndx == SHN_ABS)
3425 sec = bfd_abs_section_ptr;
3426 else if (isym->st_shndx == SHN_COMMON)
3428 sec = bfd_com_section_ptr;
3429 /* What ELF calls the size we call the value. What ELF
3430 calls the value we call the alignment. */
3431 value = isym->st_size;
3435 /* Leave it up to the processor backend. */
3438 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3441 goto error_free_vers;
3443 if (isym->st_shndx == SHN_COMMON
3444 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3446 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3450 tcomm = bfd_make_section (abfd, ".tcommon");
3452 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
3454 | SEC_LINKER_CREATED
3455 | SEC_THREAD_LOCAL)))
3456 goto error_free_vers;
3460 else if (add_symbol_hook)
3462 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3464 goto error_free_vers;
3466 /* The hook function sets the name to NULL if this symbol
3467 should be skipped for some reason. */
3472 /* Sanity check that all possibilities were handled. */
3475 bfd_set_error (bfd_error_bad_value);
3476 goto error_free_vers;
3479 if (bfd_is_und_section (sec)
3480 || bfd_is_com_section (sec))
3485 size_change_ok = FALSE;
3486 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3490 if (is_elf_hash_table (hash_table))
3492 Elf_Internal_Versym iver;
3493 unsigned int vernum = 0;
3498 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3499 vernum = iver.vs_vers & VERSYM_VERSION;
3501 /* If this is a hidden symbol, or if it is not version
3502 1, we append the version name to the symbol name.
3503 However, we do not modify a non-hidden absolute
3504 symbol, because it might be the version symbol
3505 itself. FIXME: What if it isn't? */
3506 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3507 || (vernum > 1 && ! bfd_is_abs_section (sec)))
3510 size_t namelen, verlen, newlen;
3513 if (isym->st_shndx != SHN_UNDEF)
3515 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
3517 (*_bfd_error_handler)
3518 (_("%B: %s: invalid version %u (max %d)"),
3520 elf_tdata (abfd)->dynverdef_hdr.sh_info);
3521 bfd_set_error (bfd_error_bad_value);
3522 goto error_free_vers;
3524 else if (vernum > 1)
3526 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3532 /* We cannot simply test for the number of
3533 entries in the VERNEED section since the
3534 numbers for the needed versions do not start
3536 Elf_Internal_Verneed *t;
3539 for (t = elf_tdata (abfd)->verref;
3543 Elf_Internal_Vernaux *a;
3545 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3547 if (a->vna_other == vernum)
3549 verstr = a->vna_nodename;
3558 (*_bfd_error_handler)
3559 (_("%B: %s: invalid needed version %d"),
3560 abfd, name, vernum);
3561 bfd_set_error (bfd_error_bad_value);
3562 goto error_free_vers;
3566 namelen = strlen (name);
3567 verlen = strlen (verstr);
3568 newlen = namelen + verlen + 2;
3569 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3570 && isym->st_shndx != SHN_UNDEF)
3573 newname = bfd_alloc (abfd, newlen);
3574 if (newname == NULL)
3575 goto error_free_vers;
3576 memcpy (newname, name, namelen);
3577 p = newname + namelen;
3579 /* If this is a defined non-hidden version symbol,
3580 we add another @ to the name. This indicates the
3581 default version of the symbol. */
3582 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3583 && isym->st_shndx != SHN_UNDEF)
3585 memcpy (p, verstr, verlen + 1);
3591 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
3592 sym_hash, &skip, &override,
3593 &type_change_ok, &size_change_ok))
3594 goto error_free_vers;
3603 while (h->root.type == bfd_link_hash_indirect
3604 || h->root.type == bfd_link_hash_warning)
3605 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3607 /* Remember the old alignment if this is a common symbol, so
3608 that we don't reduce the alignment later on. We can't
3609 check later, because _bfd_generic_link_add_one_symbol
3610 will set a default for the alignment which we want to
3611 override. We also remember the old bfd where the existing
3612 definition comes from. */
3613 switch (h->root.type)
3618 case bfd_link_hash_defined:
3619 case bfd_link_hash_defweak:
3620 old_bfd = h->root.u.def.section->owner;
3623 case bfd_link_hash_common:
3624 old_bfd = h->root.u.c.p->section->owner;
3625 old_alignment = h->root.u.c.p->alignment_power;
3629 if (elf_tdata (abfd)->verdef != NULL
3633 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3636 if (! (_bfd_generic_link_add_one_symbol
3637 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3638 (struct bfd_link_hash_entry **) sym_hash)))
3639 goto error_free_vers;
3642 while (h->root.type == bfd_link_hash_indirect
3643 || h->root.type == bfd_link_hash_warning)
3644 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3647 new_weakdef = FALSE;
3650 && (flags & BSF_WEAK) != 0
3651 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3652 && is_elf_hash_table (hash_table)
3653 && h->u.weakdef == NULL)
3655 /* Keep a list of all weak defined non function symbols from
3656 a dynamic object, using the weakdef field. Later in this
3657 function we will set the weakdef field to the correct
3658 value. We only put non-function symbols from dynamic
3659 objects on this list, because that happens to be the only
3660 time we need to know the normal symbol corresponding to a
3661 weak symbol, and the information is time consuming to
3662 figure out. If the weakdef field is not already NULL,
3663 then this symbol was already defined by some previous
3664 dynamic object, and we will be using that previous
3665 definition anyhow. */
3667 h->u.weakdef = weaks;
3672 /* Set the alignment of a common symbol. */
3673 if (isym->st_shndx == SHN_COMMON
3674 && h->root.type == bfd_link_hash_common)
3678 align = bfd_log2 (isym->st_value);
3679 if (align > old_alignment
3680 /* Permit an alignment power of zero if an alignment of one
3681 is specified and no other alignments have been specified. */
3682 || (isym->st_value == 1 && old_alignment == 0))
3683 h->root.u.c.p->alignment_power = align;
3685 h->root.u.c.p->alignment_power = old_alignment;
3688 if (is_elf_hash_table (hash_table))
3692 /* Check the alignment when a common symbol is involved. This
3693 can change when a common symbol is overridden by a normal
3694 definition or a common symbol is ignored due to the old
3695 normal definition. We need to make sure the maximum
3696 alignment is maintained. */
3697 if ((old_alignment || isym->st_shndx == SHN_COMMON)
3698 && h->root.type != bfd_link_hash_common)
3700 unsigned int common_align;
3701 unsigned int normal_align;
3702 unsigned int symbol_align;
3706 symbol_align = ffs (h->root.u.def.value) - 1;
3707 if (h->root.u.def.section->owner != NULL
3708 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3710 normal_align = h->root.u.def.section->alignment_power;
3711 if (normal_align > symbol_align)
3712 normal_align = symbol_align;
3715 normal_align = symbol_align;
3719 common_align = old_alignment;
3720 common_bfd = old_bfd;
3725 common_align = bfd_log2 (isym->st_value);
3727 normal_bfd = old_bfd;
3730 if (normal_align < common_align)
3731 (*_bfd_error_handler)
3732 (_("Warning: alignment %u of symbol `%s' in %B"
3733 " is smaller than %u in %B"),
3734 normal_bfd, common_bfd,
3735 1 << normal_align, name, 1 << common_align);
3738 /* Remember the symbol size and type. */
3739 if (isym->st_size != 0
3740 && (definition || h->size == 0))
3742 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3743 (*_bfd_error_handler)
3744 (_("Warning: size of symbol `%s' changed"
3745 " from %lu in %B to %lu in %B"),
3747 name, (unsigned long) h->size,
3748 (unsigned long) isym->st_size);
3750 h->size = isym->st_size;
3753 /* If this is a common symbol, then we always want H->SIZE
3754 to be the size of the common symbol. The code just above
3755 won't fix the size if a common symbol becomes larger. We
3756 don't warn about a size change here, because that is
3757 covered by --warn-common. */
3758 if (h->root.type == bfd_link_hash_common)
3759 h->size = h->root.u.c.size;
3761 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3762 && (definition || h->type == STT_NOTYPE))
3764 if (h->type != STT_NOTYPE
3765 && h->type != ELF_ST_TYPE (isym->st_info)
3766 && ! type_change_ok)
3767 (*_bfd_error_handler)
3768 (_("Warning: type of symbol `%s' changed"
3769 " from %d to %d in %B"),
3770 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3772 h->type = ELF_ST_TYPE (isym->st_info);
3775 /* If st_other has a processor-specific meaning, specific
3776 code might be needed here. We never merge the visibility
3777 attribute with the one from a dynamic object. */
3778 if (bed->elf_backend_merge_symbol_attribute)
3779 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3782 if (isym->st_other != 0 && !dynamic)
3784 unsigned char hvis, symvis, other, nvis;
3786 /* Take the balance of OTHER from the definition. */
3787 other = (definition ? isym->st_other : h->other);
3788 other &= ~ ELF_ST_VISIBILITY (-1);
3790 /* Combine visibilities, using the most constraining one. */
3791 hvis = ELF_ST_VISIBILITY (h->other);
3792 symvis = ELF_ST_VISIBILITY (isym->st_other);
3798 nvis = hvis < symvis ? hvis : symvis;
3800 h->other = other | nvis;
3803 /* Set a flag in the hash table entry indicating the type of
3804 reference or definition we just found. Keep a count of
3805 the number of dynamic symbols we find. A dynamic symbol
3806 is one which is referenced or defined by both a regular
3807 object and a shared object. */
3814 if (bind != STB_WEAK)
3815 h->ref_regular_nonweak = 1;
3819 if (! info->executable
3832 || (h->u.weakdef != NULL
3834 && h->u.weakdef->dynindx != -1))
3838 /* Check to see if we need to add an indirect symbol for
3839 the default name. */
3840 if (definition || h->root.type == bfd_link_hash_common)
3841 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
3842 &sec, &value, &dynsym,
3844 goto error_free_vers;
3846 if (definition && !dynamic)
3848 char *p = strchr (name, ELF_VER_CHR);
3849 if (p != NULL && p[1] != ELF_VER_CHR)
3851 /* Queue non-default versions so that .symver x, x@FOO
3852 aliases can be checked. */
3853 if (! nondeflt_vers)
3855 amt = (isymend - isym + 1)
3856 * sizeof (struct elf_link_hash_entry *);
3857 nondeflt_vers = bfd_malloc (amt);
3859 nondeflt_vers [nondeflt_vers_cnt++] = h;
3863 if (dynsym && h->dynindx == -1)
3865 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3866 goto error_free_vers;
3867 if (h->u.weakdef != NULL
3869 && h->u.weakdef->dynindx == -1)
3871 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
3872 goto error_free_vers;
3875 else if (dynsym && h->dynindx != -1)
3876 /* If the symbol already has a dynamic index, but
3877 visibility says it should not be visible, turn it into
3879 switch (ELF_ST_VISIBILITY (h->other))
3883 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
3894 const char *soname = elf_dt_name (abfd);
3896 /* A symbol from a library loaded via DT_NEEDED of some
3897 other library is referenced by a regular object.
3898 Add a DT_NEEDED entry for it. Issue an error if
3899 --no-add-needed is used. */
3900 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
3902 (*_bfd_error_handler)
3903 (_("%s: invalid DSO for symbol `%s' definition"),
3905 bfd_set_error (bfd_error_bad_value);
3906 goto error_free_vers;
3910 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3912 goto error_free_vers;
3914 BFD_ASSERT (ret == 0);
3919 /* Now that all the symbols from this input file are created, handle
3920 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3921 if (nondeflt_vers != NULL)
3923 bfd_size_type cnt, symidx;
3925 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
3927 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
3928 char *shortname, *p;
3930 p = strchr (h->root.root.string, ELF_VER_CHR);
3932 || (h->root.type != bfd_link_hash_defined
3933 && h->root.type != bfd_link_hash_defweak))
3936 amt = p - h->root.root.string;
3937 shortname = bfd_malloc (amt + 1);
3938 memcpy (shortname, h->root.root.string, amt);
3939 shortname[amt] = '\0';
3941 hi = (struct elf_link_hash_entry *)
3942 bfd_link_hash_lookup (&hash_table->root, shortname,
3943 FALSE, FALSE, FALSE);
3945 && hi->root.type == h->root.type
3946 && hi->root.u.def.value == h->root.u.def.value
3947 && hi->root.u.def.section == h->root.u.def.section)
3949 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
3950 hi->root.type = bfd_link_hash_indirect;
3951 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
3952 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
3953 sym_hash = elf_sym_hashes (abfd);
3955 for (symidx = 0; symidx < extsymcount; ++symidx)
3956 if (sym_hash[symidx] == hi)
3958 sym_hash[symidx] = h;
3964 free (nondeflt_vers);
3965 nondeflt_vers = NULL;
3968 if (extversym != NULL)
3974 if (isymbuf != NULL)
3978 /* Now set the weakdefs field correctly for all the weak defined
3979 symbols we found. The only way to do this is to search all the
3980 symbols. Since we only need the information for non functions in
3981 dynamic objects, that's the only time we actually put anything on
3982 the list WEAKS. We need this information so that if a regular
3983 object refers to a symbol defined weakly in a dynamic object, the
3984 real symbol in the dynamic object is also put in the dynamic
3985 symbols; we also must arrange for both symbols to point to the
3986 same memory location. We could handle the general case of symbol
3987 aliasing, but a general symbol alias can only be generated in
3988 assembler code, handling it correctly would be very time
3989 consuming, and other ELF linkers don't handle general aliasing
3993 struct elf_link_hash_entry **hpp;
3994 struct elf_link_hash_entry **hppend;
3995 struct elf_link_hash_entry **sorted_sym_hash;
3996 struct elf_link_hash_entry *h;
3999 /* Since we have to search the whole symbol list for each weak
4000 defined symbol, search time for N weak defined symbols will be
4001 O(N^2). Binary search will cut it down to O(NlogN). */
4002 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4003 sorted_sym_hash = bfd_malloc (amt);
4004 if (sorted_sym_hash == NULL)
4006 sym_hash = sorted_sym_hash;
4007 hpp = elf_sym_hashes (abfd);
4008 hppend = hpp + extsymcount;
4010 for (; hpp < hppend; hpp++)
4014 && h->root.type == bfd_link_hash_defined
4015 && h->type != STT_FUNC)
4023 qsort (sorted_sym_hash, sym_count,
4024 sizeof (struct elf_link_hash_entry *),
4027 while (weaks != NULL)
4029 struct elf_link_hash_entry *hlook;
4036 weaks = hlook->u.weakdef;
4037 hlook->u.weakdef = NULL;
4039 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4040 || hlook->root.type == bfd_link_hash_defweak
4041 || hlook->root.type == bfd_link_hash_common
4042 || hlook->root.type == bfd_link_hash_indirect);
4043 slook = hlook->root.u.def.section;
4044 vlook = hlook->root.u.def.value;
4051 bfd_signed_vma vdiff;
4053 h = sorted_sym_hash [idx];
4054 vdiff = vlook - h->root.u.def.value;
4061 long sdiff = slook->id - h->root.u.def.section->id;
4074 /* We didn't find a value/section match. */
4078 for (i = ilook; i < sym_count; i++)
4080 h = sorted_sym_hash [i];
4082 /* Stop if value or section doesn't match. */
4083 if (h->root.u.def.value != vlook
4084 || h->root.u.def.section != slook)
4086 else if (h != hlook)
4088 hlook->u.weakdef = h;
4090 /* If the weak definition is in the list of dynamic
4091 symbols, make sure the real definition is put
4093 if (hlook->dynindx != -1 && h->dynindx == -1)
4095 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4099 /* If the real definition is in the list of dynamic
4100 symbols, make sure the weak definition is put
4101 there as well. If we don't do this, then the
4102 dynamic loader might not merge the entries for the
4103 real definition and the weak definition. */
4104 if (h->dynindx != -1 && hlook->dynindx == -1)
4106 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4114 free (sorted_sym_hash);
4117 check_directives = get_elf_backend_data (abfd)->check_directives;
4118 if (check_directives)
4119 check_directives (abfd, info);
4121 /* If this object is the same format as the output object, and it is
4122 not a shared library, then let the backend look through the
4125 This is required to build global offset table entries and to
4126 arrange for dynamic relocs. It is not required for the
4127 particular common case of linking non PIC code, even when linking
4128 against shared libraries, but unfortunately there is no way of
4129 knowing whether an object file has been compiled PIC or not.
4130 Looking through the relocs is not particularly time consuming.
4131 The problem is that we must either (1) keep the relocs in memory,
4132 which causes the linker to require additional runtime memory or
4133 (2) read the relocs twice from the input file, which wastes time.
4134 This would be a good case for using mmap.
4136 I have no idea how to handle linking PIC code into a file of a
4137 different format. It probably can't be done. */
4138 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4140 && is_elf_hash_table (hash_table)
4141 && hash_table->root.creator == abfd->xvec
4142 && check_relocs != NULL)
4146 for (o = abfd->sections; o != NULL; o = o->next)
4148 Elf_Internal_Rela *internal_relocs;
4151 if ((o->flags & SEC_RELOC) == 0
4152 || o->reloc_count == 0
4153 || ((info->strip == strip_all || info->strip == strip_debugger)
4154 && (o->flags & SEC_DEBUGGING) != 0)
4155 || bfd_is_abs_section (o->output_section))
4158 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4160 if (internal_relocs == NULL)
4163 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4165 if (elf_section_data (o)->relocs != internal_relocs)
4166 free (internal_relocs);
4173 /* If this is a non-traditional link, try to optimize the handling
4174 of the .stab/.stabstr sections. */
4176 && ! info->traditional_format
4177 && is_elf_hash_table (hash_table)
4178 && (info->strip != strip_all && info->strip != strip_debugger))
4182 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4183 if (stabstr != NULL)
4185 bfd_size_type string_offset = 0;
4188 for (stab = abfd->sections; stab; stab = stab->next)
4189 if (strncmp (".stab", stab->name, 5) == 0
4190 && (!stab->name[5] ||
4191 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4192 && (stab->flags & SEC_MERGE) == 0
4193 && !bfd_is_abs_section (stab->output_section))
4195 struct bfd_elf_section_data *secdata;
4197 secdata = elf_section_data (stab);
4198 if (! _bfd_link_section_stabs (abfd,
4199 &hash_table->stab_info,
4204 if (secdata->sec_info)
4205 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4210 if (is_elf_hash_table (hash_table))
4212 /* Add this bfd to the loaded list. */
4213 struct elf_link_loaded_list *n;
4215 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4219 n->next = hash_table->loaded;
4220 hash_table->loaded = n;
4226 if (nondeflt_vers != NULL)
4227 free (nondeflt_vers);
4228 if (extversym != NULL)
4231 if (isymbuf != NULL)
4237 /* Return the linker hash table entry of a symbol that might be
4238 satisfied by an archive symbol. Return -1 on error. */
4240 struct elf_link_hash_entry *
4241 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4242 struct bfd_link_info *info,
4245 struct elf_link_hash_entry *h;
4249 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4253 /* If this is a default version (the name contains @@), look up the
4254 symbol again with only one `@' as well as without the version.
4255 The effect is that references to the symbol with and without the
4256 version will be matched by the default symbol in the archive. */
4258 p = strchr (name, ELF_VER_CHR);
4259 if (p == NULL || p[1] != ELF_VER_CHR)
4262 /* First check with only one `@'. */
4263 len = strlen (name);
4264 copy = bfd_alloc (abfd, len);
4266 return (struct elf_link_hash_entry *) 0 - 1;
4268 first = p - name + 1;
4269 memcpy (copy, name, first);
4270 memcpy (copy + first, name + first + 1, len - first);
4272 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4275 /* We also need to check references to the symbol without the
4277 copy[first - 1] = '\0';
4278 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4279 FALSE, FALSE, FALSE);
4282 bfd_release (abfd, copy);
4286 /* Add symbols from an ELF archive file to the linker hash table. We
4287 don't use _bfd_generic_link_add_archive_symbols because of a
4288 problem which arises on UnixWare. The UnixWare libc.so is an
4289 archive which includes an entry libc.so.1 which defines a bunch of
4290 symbols. The libc.so archive also includes a number of other
4291 object files, which also define symbols, some of which are the same
4292 as those defined in libc.so.1. Correct linking requires that we
4293 consider each object file in turn, and include it if it defines any
4294 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4295 this; it looks through the list of undefined symbols, and includes
4296 any object file which defines them. When this algorithm is used on
4297 UnixWare, it winds up pulling in libc.so.1 early and defining a
4298 bunch of symbols. This means that some of the other objects in the
4299 archive are not included in the link, which is incorrect since they
4300 precede libc.so.1 in the archive.
4302 Fortunately, ELF archive handling is simpler than that done by
4303 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4304 oddities. In ELF, if we find a symbol in the archive map, and the
4305 symbol is currently undefined, we know that we must pull in that
4308 Unfortunately, we do have to make multiple passes over the symbol
4309 table until nothing further is resolved. */
4312 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4315 bfd_boolean *defined = NULL;
4316 bfd_boolean *included = NULL;
4320 const struct elf_backend_data *bed;
4321 struct elf_link_hash_entry * (*archive_symbol_lookup)
4322 (bfd *, struct bfd_link_info *, const char *);
4324 if (! bfd_has_map (abfd))
4326 /* An empty archive is a special case. */
4327 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4329 bfd_set_error (bfd_error_no_armap);
4333 /* Keep track of all symbols we know to be already defined, and all
4334 files we know to be already included. This is to speed up the
4335 second and subsequent passes. */
4336 c = bfd_ardata (abfd)->symdef_count;
4340 amt *= sizeof (bfd_boolean);
4341 defined = bfd_zmalloc (amt);
4342 included = bfd_zmalloc (amt);
4343 if (defined == NULL || included == NULL)
4346 symdefs = bfd_ardata (abfd)->symdefs;
4347 bed = get_elf_backend_data (abfd);
4348 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4361 symdefend = symdef + c;
4362 for (i = 0; symdef < symdefend; symdef++, i++)
4364 struct elf_link_hash_entry *h;
4366 struct bfd_link_hash_entry *undefs_tail;
4369 if (defined[i] || included[i])
4371 if (symdef->file_offset == last)
4377 h = archive_symbol_lookup (abfd, info, symdef->name);
4378 if (h == (struct elf_link_hash_entry *) 0 - 1)
4384 if (h->root.type == bfd_link_hash_common)
4386 /* We currently have a common symbol. The archive map contains
4387 a reference to this symbol, so we may want to include it. We
4388 only want to include it however, if this archive element
4389 contains a definition of the symbol, not just another common
4392 Unfortunately some archivers (including GNU ar) will put
4393 declarations of common symbols into their archive maps, as
4394 well as real definitions, so we cannot just go by the archive
4395 map alone. Instead we must read in the element's symbol
4396 table and check that to see what kind of symbol definition
4398 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4401 else if (h->root.type != bfd_link_hash_undefined)
4403 if (h->root.type != bfd_link_hash_undefweak)
4408 /* We need to include this archive member. */
4409 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4410 if (element == NULL)
4413 if (! bfd_check_format (element, bfd_object))
4416 /* Doublecheck that we have not included this object
4417 already--it should be impossible, but there may be
4418 something wrong with the archive. */
4419 if (element->archive_pass != 0)
4421 bfd_set_error (bfd_error_bad_value);
4424 element->archive_pass = 1;
4426 undefs_tail = info->hash->undefs_tail;
4428 if (! (*info->callbacks->add_archive_element) (info, element,
4431 if (! bfd_link_add_symbols (element, info))
4434 /* If there are any new undefined symbols, we need to make
4435 another pass through the archive in order to see whether
4436 they can be defined. FIXME: This isn't perfect, because
4437 common symbols wind up on undefs_tail and because an
4438 undefined symbol which is defined later on in this pass
4439 does not require another pass. This isn't a bug, but it
4440 does make the code less efficient than it could be. */
4441 if (undefs_tail != info->hash->undefs_tail)
4444 /* Look backward to mark all symbols from this object file
4445 which we have already seen in this pass. */
4449 included[mark] = TRUE;
4454 while (symdefs[mark].file_offset == symdef->file_offset);
4456 /* We mark subsequent symbols from this object file as we go
4457 on through the loop. */
4458 last = symdef->file_offset;
4469 if (defined != NULL)
4471 if (included != NULL)
4476 /* Given an ELF BFD, add symbols to the global hash table as
4480 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4482 switch (bfd_get_format (abfd))
4485 return elf_link_add_object_symbols (abfd, info);
4487 return elf_link_add_archive_symbols (abfd, info);
4489 bfd_set_error (bfd_error_wrong_format);
4494 /* This function will be called though elf_link_hash_traverse to store
4495 all hash value of the exported symbols in an array. */
4498 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4500 unsigned long **valuep = data;
4506 if (h->root.type == bfd_link_hash_warning)
4507 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4509 /* Ignore indirect symbols. These are added by the versioning code. */
4510 if (h->dynindx == -1)
4513 name = h->root.root.string;
4514 p = strchr (name, ELF_VER_CHR);
4517 alc = bfd_malloc (p - name + 1);
4518 memcpy (alc, name, p - name);
4519 alc[p - name] = '\0';
4523 /* Compute the hash value. */
4524 ha = bfd_elf_hash (name);
4526 /* Store the found hash value in the array given as the argument. */
4529 /* And store it in the struct so that we can put it in the hash table
4531 h->u.elf_hash_value = ha;
4539 /* Array used to determine the number of hash table buckets to use
4540 based on the number of symbols there are. If there are fewer than
4541 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4542 fewer than 37 we use 17 buckets, and so forth. We never use more
4543 than 32771 buckets. */
4545 static const size_t elf_buckets[] =
4547 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4551 /* Compute bucket count for hashing table. We do not use a static set
4552 of possible tables sizes anymore. Instead we determine for all
4553 possible reasonable sizes of the table the outcome (i.e., the
4554 number of collisions etc) and choose the best solution. The
4555 weighting functions are not too simple to allow the table to grow
4556 without bounds. Instead one of the weighting factors is the size.
4557 Therefore the result is always a good payoff between few collisions
4558 (= short chain lengths) and table size. */
4560 compute_bucket_count (struct bfd_link_info *info)
4562 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4563 size_t best_size = 0;
4564 unsigned long int *hashcodes;
4565 unsigned long int *hashcodesp;
4566 unsigned long int i;
4569 /* Compute the hash values for all exported symbols. At the same
4570 time store the values in an array so that we could use them for
4573 amt *= sizeof (unsigned long int);
4574 hashcodes = bfd_malloc (amt);
4575 if (hashcodes == NULL)
4577 hashcodesp = hashcodes;
4579 /* Put all hash values in HASHCODES. */
4580 elf_link_hash_traverse (elf_hash_table (info),
4581 elf_collect_hash_codes, &hashcodesp);
4583 /* We have a problem here. The following code to optimize the table
4584 size requires an integer type with more the 32 bits. If
4585 BFD_HOST_U_64_BIT is set we know about such a type. */
4586 #ifdef BFD_HOST_U_64_BIT
4589 unsigned long int nsyms = hashcodesp - hashcodes;
4592 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4593 unsigned long int *counts ;
4594 bfd *dynobj = elf_hash_table (info)->dynobj;
4595 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4597 /* Possible optimization parameters: if we have NSYMS symbols we say
4598 that the hashing table must at least have NSYMS/4 and at most
4600 minsize = nsyms / 4;
4603 best_size = maxsize = nsyms * 2;
4605 /* Create array where we count the collisions in. We must use bfd_malloc
4606 since the size could be large. */
4608 amt *= sizeof (unsigned long int);
4609 counts = bfd_malloc (amt);
4616 /* Compute the "optimal" size for the hash table. The criteria is a
4617 minimal chain length. The minor criteria is (of course) the size
4619 for (i = minsize; i < maxsize; ++i)
4621 /* Walk through the array of hashcodes and count the collisions. */
4622 BFD_HOST_U_64_BIT max;
4623 unsigned long int j;
4624 unsigned long int fact;
4626 memset (counts, '\0', i * sizeof (unsigned long int));
4628 /* Determine how often each hash bucket is used. */
4629 for (j = 0; j < nsyms; ++j)
4630 ++counts[hashcodes[j] % i];
4632 /* For the weight function we need some information about the
4633 pagesize on the target. This is information need not be 100%
4634 accurate. Since this information is not available (so far) we
4635 define it here to a reasonable default value. If it is crucial
4636 to have a better value some day simply define this value. */
4637 # ifndef BFD_TARGET_PAGESIZE
4638 # define BFD_TARGET_PAGESIZE (4096)
4641 /* We in any case need 2 + NSYMS entries for the size values and
4643 max = (2 + nsyms) * (bed->s->arch_size / 8);
4646 /* Variant 1: optimize for short chains. We add the squares
4647 of all the chain lengths (which favors many small chain
4648 over a few long chains). */
4649 for (j = 0; j < i; ++j)
4650 max += counts[j] * counts[j];
4652 /* This adds penalties for the overall size of the table. */
4653 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4656 /* Variant 2: Optimize a lot more for small table. Here we
4657 also add squares of the size but we also add penalties for
4658 empty slots (the +1 term). */
4659 for (j = 0; j < i; ++j)
4660 max += (1 + counts[j]) * (1 + counts[j]);
4662 /* The overall size of the table is considered, but not as
4663 strong as in variant 1, where it is squared. */
4664 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4668 /* Compare with current best results. */
4669 if (max < best_chlen)
4679 #endif /* defined (BFD_HOST_U_64_BIT) */
4681 /* This is the fallback solution if no 64bit type is available or if we
4682 are not supposed to spend much time on optimizations. We select the
4683 bucket count using a fixed set of numbers. */
4684 for (i = 0; elf_buckets[i] != 0; i++)
4686 best_size = elf_buckets[i];
4687 if (dynsymcount < elf_buckets[i + 1])
4692 /* Free the arrays we needed. */
4698 /* Set up the sizes and contents of the ELF dynamic sections. This is
4699 called by the ELF linker emulation before_allocation routine. We
4700 must set the sizes of the sections before the linker sets the
4701 addresses of the various sections. */
4704 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4707 const char *filter_shlib,
4708 const char * const *auxiliary_filters,
4709 struct bfd_link_info *info,
4710 asection **sinterpptr,
4711 struct bfd_elf_version_tree *verdefs)
4713 bfd_size_type soname_indx;
4715 const struct elf_backend_data *bed;
4716 struct elf_assign_sym_version_info asvinfo;
4720 soname_indx = (bfd_size_type) -1;
4722 if (!is_elf_hash_table (info->hash))
4725 elf_tdata (output_bfd)->relro = info->relro;
4726 if (info->execstack)
4727 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4728 else if (info->noexecstack)
4729 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4733 asection *notesec = NULL;
4736 for (inputobj = info->input_bfds;
4738 inputobj = inputobj->link_next)
4742 if (inputobj->flags & DYNAMIC)
4744 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4747 if (s->flags & SEC_CODE)
4756 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4757 if (exec && info->relocatable
4758 && notesec->output_section != bfd_abs_section_ptr)
4759 notesec->output_section->flags |= SEC_CODE;
4763 /* Any syms created from now on start with -1 in
4764 got.refcount/offset and plt.refcount/offset. */
4765 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
4767 /* The backend may have to create some sections regardless of whether
4768 we're dynamic or not. */
4769 bed = get_elf_backend_data (output_bfd);
4770 if (bed->elf_backend_always_size_sections
4771 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4774 dynobj = elf_hash_table (info)->dynobj;
4776 /* If there were no dynamic objects in the link, there is nothing to
4781 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
4784 if (elf_hash_table (info)->dynamic_sections_created)
4786 struct elf_info_failed eif;
4787 struct elf_link_hash_entry *h;
4789 struct bfd_elf_version_tree *t;
4790 struct bfd_elf_version_expr *d;
4791 bfd_boolean all_defined;
4793 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
4794 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
4798 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4800 if (soname_indx == (bfd_size_type) -1
4801 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
4807 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
4809 info->flags |= DF_SYMBOLIC;
4816 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
4818 if (indx == (bfd_size_type) -1
4819 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
4822 if (info->new_dtags)
4824 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
4825 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
4830 if (filter_shlib != NULL)
4834 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4835 filter_shlib, TRUE);
4836 if (indx == (bfd_size_type) -1
4837 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
4841 if (auxiliary_filters != NULL)
4843 const char * const *p;
4845 for (p = auxiliary_filters; *p != NULL; p++)
4849 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4851 if (indx == (bfd_size_type) -1
4852 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
4858 eif.verdefs = verdefs;
4861 /* If we are supposed to export all symbols into the dynamic symbol
4862 table (this is not the normal case), then do so. */
4863 if (info->export_dynamic)
4865 elf_link_hash_traverse (elf_hash_table (info),
4866 _bfd_elf_export_symbol,
4872 /* Make all global versions with definition. */
4873 for (t = verdefs; t != NULL; t = t->next)
4874 for (d = t->globals.list; d != NULL; d = d->next)
4875 if (!d->symver && d->symbol)
4877 const char *verstr, *name;
4878 size_t namelen, verlen, newlen;
4880 struct elf_link_hash_entry *newh;
4883 namelen = strlen (name);
4885 verlen = strlen (verstr);
4886 newlen = namelen + verlen + 3;
4888 newname = bfd_malloc (newlen);
4889 if (newname == NULL)
4891 memcpy (newname, name, namelen);
4893 /* Check the hidden versioned definition. */
4894 p = newname + namelen;
4896 memcpy (p, verstr, verlen + 1);
4897 newh = elf_link_hash_lookup (elf_hash_table (info),
4898 newname, FALSE, FALSE,
4901 || (newh->root.type != bfd_link_hash_defined
4902 && newh->root.type != bfd_link_hash_defweak))
4904 /* Check the default versioned definition. */
4906 memcpy (p, verstr, verlen + 1);
4907 newh = elf_link_hash_lookup (elf_hash_table (info),
4908 newname, FALSE, FALSE,
4913 /* Mark this version if there is a definition and it is
4914 not defined in a shared object. */
4916 && !newh->def_dynamic
4917 && (newh->root.type == bfd_link_hash_defined
4918 || newh->root.type == bfd_link_hash_defweak))
4922 /* Attach all the symbols to their version information. */
4923 asvinfo.output_bfd = output_bfd;
4924 asvinfo.info = info;
4925 asvinfo.verdefs = verdefs;
4926 asvinfo.failed = FALSE;
4928 elf_link_hash_traverse (elf_hash_table (info),
4929 _bfd_elf_link_assign_sym_version,
4934 if (!info->allow_undefined_version)
4936 /* Check if all global versions have a definition. */
4938 for (t = verdefs; t != NULL; t = t->next)
4939 for (d = t->globals.list; d != NULL; d = d->next)
4940 if (!d->symver && !d->script)
4942 (*_bfd_error_handler)
4943 (_("%s: undefined version: %s"),
4944 d->pattern, t->name);
4945 all_defined = FALSE;
4950 bfd_set_error (bfd_error_bad_value);
4955 /* Find all symbols which were defined in a dynamic object and make
4956 the backend pick a reasonable value for them. */
4957 elf_link_hash_traverse (elf_hash_table (info),
4958 _bfd_elf_adjust_dynamic_symbol,
4963 /* Add some entries to the .dynamic section. We fill in some of the
4964 values later, in bfd_elf_final_link, but we must add the entries
4965 now so that we know the final size of the .dynamic section. */
4967 /* If there are initialization and/or finalization functions to
4968 call then add the corresponding DT_INIT/DT_FINI entries. */
4969 h = (info->init_function
4970 ? elf_link_hash_lookup (elf_hash_table (info),
4971 info->init_function, FALSE,
4978 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
4981 h = (info->fini_function
4982 ? elf_link_hash_lookup (elf_hash_table (info),
4983 info->fini_function, FALSE,
4990 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
4994 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
4996 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4997 if (! info->executable)
5002 for (sub = info->input_bfds; sub != NULL;
5003 sub = sub->link_next)
5004 for (o = sub->sections; o != NULL; o = o->next)
5005 if (elf_section_data (o)->this_hdr.sh_type
5006 == SHT_PREINIT_ARRAY)
5008 (*_bfd_error_handler)
5009 (_("%B: .preinit_array section is not allowed in DSO"),
5014 bfd_set_error (bfd_error_nonrepresentable_section);
5018 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5019 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5022 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
5024 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5025 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5028 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
5030 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5031 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5035 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5036 /* If .dynstr is excluded from the link, we don't want any of
5037 these tags. Strictly, we should be checking each section
5038 individually; This quick check covers for the case where
5039 someone does a /DISCARD/ : { *(*) }. */
5040 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5042 bfd_size_type strsize;
5044 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5045 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5046 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5047 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5048 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5049 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5050 bed->s->sizeof_sym))
5055 /* The backend must work out the sizes of all the other dynamic
5057 if (bed->elf_backend_size_dynamic_sections
5058 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5061 if (elf_hash_table (info)->dynamic_sections_created)
5063 bfd_size_type dynsymcount;
5065 size_t bucketcount = 0;
5066 size_t hash_entry_size;
5067 unsigned int dtagcount;
5069 /* Set up the version definition section. */
5070 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5071 BFD_ASSERT (s != NULL);
5073 /* We may have created additional version definitions if we are
5074 just linking a regular application. */
5075 verdefs = asvinfo.verdefs;
5077 /* Skip anonymous version tag. */
5078 if (verdefs != NULL && verdefs->vernum == 0)
5079 verdefs = verdefs->next;
5081 if (verdefs == NULL)
5082 _bfd_strip_section_from_output (info, s);
5087 struct bfd_elf_version_tree *t;
5089 Elf_Internal_Verdef def;
5090 Elf_Internal_Verdaux defaux;
5095 /* Make space for the base version. */
5096 size += sizeof (Elf_External_Verdef);
5097 size += sizeof (Elf_External_Verdaux);
5100 for (t = verdefs; t != NULL; t = t->next)
5102 struct bfd_elf_version_deps *n;
5104 size += sizeof (Elf_External_Verdef);
5105 size += sizeof (Elf_External_Verdaux);
5108 for (n = t->deps; n != NULL; n = n->next)
5109 size += sizeof (Elf_External_Verdaux);
5113 s->contents = bfd_alloc (output_bfd, s->size);
5114 if (s->contents == NULL && s->size != 0)
5117 /* Fill in the version definition section. */
5121 def.vd_version = VER_DEF_CURRENT;
5122 def.vd_flags = VER_FLG_BASE;
5125 def.vd_aux = sizeof (Elf_External_Verdef);
5126 def.vd_next = (sizeof (Elf_External_Verdef)
5127 + sizeof (Elf_External_Verdaux));
5129 if (soname_indx != (bfd_size_type) -1)
5131 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5133 def.vd_hash = bfd_elf_hash (soname);
5134 defaux.vda_name = soname_indx;
5141 name = basename (output_bfd->filename);
5142 def.vd_hash = bfd_elf_hash (name);
5143 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5145 if (indx == (bfd_size_type) -1)
5147 defaux.vda_name = indx;
5149 defaux.vda_next = 0;
5151 _bfd_elf_swap_verdef_out (output_bfd, &def,
5152 (Elf_External_Verdef *) p);
5153 p += sizeof (Elf_External_Verdef);
5154 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5155 (Elf_External_Verdaux *) p);
5156 p += sizeof (Elf_External_Verdaux);
5158 for (t = verdefs; t != NULL; t = t->next)
5161 struct bfd_elf_version_deps *n;
5162 struct elf_link_hash_entry *h;
5163 struct bfd_link_hash_entry *bh;
5166 for (n = t->deps; n != NULL; n = n->next)
5169 /* Add a symbol representing this version. */
5171 if (! (_bfd_generic_link_add_one_symbol
5172 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5174 get_elf_backend_data (dynobj)->collect, &bh)))
5176 h = (struct elf_link_hash_entry *) bh;
5179 h->type = STT_OBJECT;
5180 h->verinfo.vertree = t;
5182 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5185 def.vd_version = VER_DEF_CURRENT;
5187 if (t->globals.list == NULL
5188 && t->locals.list == NULL
5190 def.vd_flags |= VER_FLG_WEAK;
5191 def.vd_ndx = t->vernum + 1;
5192 def.vd_cnt = cdeps + 1;
5193 def.vd_hash = bfd_elf_hash (t->name);
5194 def.vd_aux = sizeof (Elf_External_Verdef);
5196 if (t->next != NULL)
5197 def.vd_next = (sizeof (Elf_External_Verdef)
5198 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5200 _bfd_elf_swap_verdef_out (output_bfd, &def,
5201 (Elf_External_Verdef *) p);
5202 p += sizeof (Elf_External_Verdef);
5204 defaux.vda_name = h->dynstr_index;
5205 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5207 defaux.vda_next = 0;
5208 if (t->deps != NULL)
5209 defaux.vda_next = sizeof (Elf_External_Verdaux);
5210 t->name_indx = defaux.vda_name;
5212 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5213 (Elf_External_Verdaux *) p);
5214 p += sizeof (Elf_External_Verdaux);
5216 for (n = t->deps; n != NULL; n = n->next)
5218 if (n->version_needed == NULL)
5220 /* This can happen if there was an error in the
5222 defaux.vda_name = 0;
5226 defaux.vda_name = n->version_needed->name_indx;
5227 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5230 if (n->next == NULL)
5231 defaux.vda_next = 0;
5233 defaux.vda_next = sizeof (Elf_External_Verdaux);
5235 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5236 (Elf_External_Verdaux *) p);
5237 p += sizeof (Elf_External_Verdaux);
5241 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5242 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5245 elf_tdata (output_bfd)->cverdefs = cdefs;
5248 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5250 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5253 else if (info->flags & DF_BIND_NOW)
5255 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5261 if (info->executable)
5262 info->flags_1 &= ~ (DF_1_INITFIRST
5265 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5269 /* Work out the size of the version reference section. */
5271 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5272 BFD_ASSERT (s != NULL);
5274 struct elf_find_verdep_info sinfo;
5276 sinfo.output_bfd = output_bfd;
5278 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5279 if (sinfo.vers == 0)
5281 sinfo.failed = FALSE;
5283 elf_link_hash_traverse (elf_hash_table (info),
5284 _bfd_elf_link_find_version_dependencies,
5287 if (elf_tdata (output_bfd)->verref == NULL)
5288 _bfd_strip_section_from_output (info, s);
5291 Elf_Internal_Verneed *t;
5296 /* Build the version definition section. */
5299 for (t = elf_tdata (output_bfd)->verref;
5303 Elf_Internal_Vernaux *a;
5305 size += sizeof (Elf_External_Verneed);
5307 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5308 size += sizeof (Elf_External_Vernaux);
5312 s->contents = bfd_alloc (output_bfd, s->size);
5313 if (s->contents == NULL)
5317 for (t = elf_tdata (output_bfd)->verref;
5322 Elf_Internal_Vernaux *a;
5326 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5329 t->vn_version = VER_NEED_CURRENT;
5331 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5332 elf_dt_name (t->vn_bfd) != NULL
5333 ? elf_dt_name (t->vn_bfd)
5334 : basename (t->vn_bfd->filename),
5336 if (indx == (bfd_size_type) -1)
5339 t->vn_aux = sizeof (Elf_External_Verneed);
5340 if (t->vn_nextref == NULL)
5343 t->vn_next = (sizeof (Elf_External_Verneed)
5344 + caux * sizeof (Elf_External_Vernaux));
5346 _bfd_elf_swap_verneed_out (output_bfd, t,
5347 (Elf_External_Verneed *) p);
5348 p += sizeof (Elf_External_Verneed);
5350 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5352 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5353 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5354 a->vna_nodename, FALSE);
5355 if (indx == (bfd_size_type) -1)
5358 if (a->vna_nextptr == NULL)
5361 a->vna_next = sizeof (Elf_External_Vernaux);
5363 _bfd_elf_swap_vernaux_out (output_bfd, a,
5364 (Elf_External_Vernaux *) p);
5365 p += sizeof (Elf_External_Vernaux);
5369 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5370 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5373 elf_tdata (output_bfd)->cverrefs = crefs;
5377 /* Assign dynsym indicies. In a shared library we generate a
5378 section symbol for each output section, which come first.
5379 Next come all of the back-end allocated local dynamic syms,
5380 followed by the rest of the global symbols. */
5382 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5384 /* Work out the size of the symbol version section. */
5385 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5386 BFD_ASSERT (s != NULL);
5387 if (dynsymcount == 0
5388 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
5390 _bfd_strip_section_from_output (info, s);
5391 /* The DYNSYMCOUNT might have changed if we were going to
5392 output a dynamic symbol table entry for S. */
5393 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5397 s->size = dynsymcount * sizeof (Elf_External_Versym);
5398 s->contents = bfd_zalloc (output_bfd, s->size);
5399 if (s->contents == NULL)
5402 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5406 /* Set the size of the .dynsym and .hash sections. We counted
5407 the number of dynamic symbols in elf_link_add_object_symbols.
5408 We will build the contents of .dynsym and .hash when we build
5409 the final symbol table, because until then we do not know the
5410 correct value to give the symbols. We built the .dynstr
5411 section as we went along in elf_link_add_object_symbols. */
5412 s = bfd_get_section_by_name (dynobj, ".dynsym");
5413 BFD_ASSERT (s != NULL);
5414 s->size = dynsymcount * bed->s->sizeof_sym;
5415 s->contents = bfd_alloc (output_bfd, s->size);
5416 if (s->contents == NULL && s->size != 0)
5419 if (dynsymcount != 0)
5421 Elf_Internal_Sym isym;
5423 /* The first entry in .dynsym is a dummy symbol. */
5430 bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0);
5433 /* Compute the size of the hashing table. As a side effect this
5434 computes the hash values for all the names we export. */
5435 bucketcount = compute_bucket_count (info);
5437 s = bfd_get_section_by_name (dynobj, ".hash");
5438 BFD_ASSERT (s != NULL);
5439 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5440 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5441 s->contents = bfd_zalloc (output_bfd, s->size);
5442 if (s->contents == NULL)
5445 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5446 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5447 s->contents + hash_entry_size);
5449 elf_hash_table (info)->bucketcount = bucketcount;
5451 s = bfd_get_section_by_name (dynobj, ".dynstr");
5452 BFD_ASSERT (s != NULL);
5454 elf_finalize_dynstr (output_bfd, info);
5456 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5458 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5459 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5466 /* Final phase of ELF linker. */
5468 /* A structure we use to avoid passing large numbers of arguments. */
5470 struct elf_final_link_info
5472 /* General link information. */
5473 struct bfd_link_info *info;
5476 /* Symbol string table. */
5477 struct bfd_strtab_hash *symstrtab;
5478 /* .dynsym section. */
5479 asection *dynsym_sec;
5480 /* .hash section. */
5482 /* symbol version section (.gnu.version). */
5483 asection *symver_sec;
5484 /* Buffer large enough to hold contents of any section. */
5486 /* Buffer large enough to hold external relocs of any section. */
5487 void *external_relocs;
5488 /* Buffer large enough to hold internal relocs of any section. */
5489 Elf_Internal_Rela *internal_relocs;
5490 /* Buffer large enough to hold external local symbols of any input
5492 bfd_byte *external_syms;
5493 /* And a buffer for symbol section indices. */
5494 Elf_External_Sym_Shndx *locsym_shndx;
5495 /* Buffer large enough to hold internal local symbols of any input
5497 Elf_Internal_Sym *internal_syms;
5498 /* Array large enough to hold a symbol index for each local symbol
5499 of any input BFD. */
5501 /* Array large enough to hold a section pointer for each local
5502 symbol of any input BFD. */
5503 asection **sections;
5504 /* Buffer to hold swapped out symbols. */
5506 /* And one for symbol section indices. */
5507 Elf_External_Sym_Shndx *symshndxbuf;
5508 /* Number of swapped out symbols in buffer. */
5509 size_t symbuf_count;
5510 /* Number of symbols which fit in symbuf. */
5512 /* And same for symshndxbuf. */
5513 size_t shndxbuf_size;
5516 /* This struct is used to pass information to elf_link_output_extsym. */
5518 struct elf_outext_info
5521 bfd_boolean localsyms;
5522 struct elf_final_link_info *finfo;
5525 /* When performing a relocatable link, the input relocations are
5526 preserved. But, if they reference global symbols, the indices
5527 referenced must be updated. Update all the relocations in
5528 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5531 elf_link_adjust_relocs (bfd *abfd,
5532 Elf_Internal_Shdr *rel_hdr,
5534 struct elf_link_hash_entry **rel_hash)
5537 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5539 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5540 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5541 bfd_vma r_type_mask;
5544 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5546 swap_in = bed->s->swap_reloc_in;
5547 swap_out = bed->s->swap_reloc_out;
5549 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5551 swap_in = bed->s->swap_reloca_in;
5552 swap_out = bed->s->swap_reloca_out;
5557 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5560 if (bed->s->arch_size == 32)
5567 r_type_mask = 0xffffffff;
5571 erela = rel_hdr->contents;
5572 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5574 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5577 if (*rel_hash == NULL)
5580 BFD_ASSERT ((*rel_hash)->indx >= 0);
5582 (*swap_in) (abfd, erela, irela);
5583 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5584 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5585 | (irela[j].r_info & r_type_mask));
5586 (*swap_out) (abfd, irela, erela);
5590 struct elf_link_sort_rela
5596 enum elf_reloc_type_class type;
5597 /* We use this as an array of size int_rels_per_ext_rel. */
5598 Elf_Internal_Rela rela[1];
5602 elf_link_sort_cmp1 (const void *A, const void *B)
5604 const struct elf_link_sort_rela *a = A;
5605 const struct elf_link_sort_rela *b = B;
5606 int relativea, relativeb;
5608 relativea = a->type == reloc_class_relative;
5609 relativeb = b->type == reloc_class_relative;
5611 if (relativea < relativeb)
5613 if (relativea > relativeb)
5615 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5617 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5619 if (a->rela->r_offset < b->rela->r_offset)
5621 if (a->rela->r_offset > b->rela->r_offset)
5627 elf_link_sort_cmp2 (const void *A, const void *B)
5629 const struct elf_link_sort_rela *a = A;
5630 const struct elf_link_sort_rela *b = B;
5633 if (a->u.offset < b->u.offset)
5635 if (a->u.offset > b->u.offset)
5637 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5638 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5643 if (a->rela->r_offset < b->rela->r_offset)
5645 if (a->rela->r_offset > b->rela->r_offset)
5651 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5654 bfd_size_type count, size;
5655 size_t i, ret, sort_elt, ext_size;
5656 bfd_byte *sort, *s_non_relative, *p;
5657 struct elf_link_sort_rela *sq;
5658 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5659 int i2e = bed->s->int_rels_per_ext_rel;
5660 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5661 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5662 struct bfd_link_order *lo;
5665 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5666 if (reldyn == NULL || reldyn->size == 0)
5668 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5669 if (reldyn == NULL || reldyn->size == 0)
5671 ext_size = bed->s->sizeof_rel;
5672 swap_in = bed->s->swap_reloc_in;
5673 swap_out = bed->s->swap_reloc_out;
5677 ext_size = bed->s->sizeof_rela;
5678 swap_in = bed->s->swap_reloca_in;
5679 swap_out = bed->s->swap_reloca_out;
5681 count = reldyn->size / ext_size;
5684 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5685 if (lo->type == bfd_indirect_link_order)
5687 asection *o = lo->u.indirect.section;
5691 if (size != reldyn->size)
5694 sort_elt = (sizeof (struct elf_link_sort_rela)
5695 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5696 sort = bfd_zmalloc (sort_elt * count);
5699 (*info->callbacks->warning)
5700 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5704 if (bed->s->arch_size == 32)
5705 r_sym_mask = ~(bfd_vma) 0xff;
5707 r_sym_mask = ~(bfd_vma) 0xffffffff;
5709 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5710 if (lo->type == bfd_indirect_link_order)
5712 bfd_byte *erel, *erelend;
5713 asection *o = lo->u.indirect.section;
5716 erelend = o->contents + o->size;
5717 p = sort + o->output_offset / ext_size * sort_elt;
5718 while (erel < erelend)
5720 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5721 (*swap_in) (abfd, erel, s->rela);
5722 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
5723 s->u.sym_mask = r_sym_mask;
5729 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
5731 for (i = 0, p = sort; i < count; i++, p += sort_elt)
5733 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5734 if (s->type != reloc_class_relative)
5740 sq = (struct elf_link_sort_rela *) s_non_relative;
5741 for (; i < count; i++, p += sort_elt)
5743 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
5744 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
5746 sp->u.offset = sq->rela->r_offset;
5749 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
5751 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5752 if (lo->type == bfd_indirect_link_order)
5754 bfd_byte *erel, *erelend;
5755 asection *o = lo->u.indirect.section;
5758 erelend = o->contents + o->size;
5759 p = sort + o->output_offset / ext_size * sort_elt;
5760 while (erel < erelend)
5762 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5763 (*swap_out) (abfd, s->rela, erel);
5774 /* Flush the output symbols to the file. */
5777 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
5778 const struct elf_backend_data *bed)
5780 if (finfo->symbuf_count > 0)
5782 Elf_Internal_Shdr *hdr;
5786 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5787 pos = hdr->sh_offset + hdr->sh_size;
5788 amt = finfo->symbuf_count * bed->s->sizeof_sym;
5789 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5790 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
5793 hdr->sh_size += amt;
5794 finfo->symbuf_count = 0;
5800 /* Add a symbol to the output symbol table. */
5803 elf_link_output_sym (struct elf_final_link_info *finfo,
5805 Elf_Internal_Sym *elfsym,
5806 asection *input_sec,
5807 struct elf_link_hash_entry *h)
5810 Elf_External_Sym_Shndx *destshndx;
5811 bfd_boolean (*output_symbol_hook)
5812 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
5813 struct elf_link_hash_entry *);
5814 const struct elf_backend_data *bed;
5816 bed = get_elf_backend_data (finfo->output_bfd);
5817 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
5818 if (output_symbol_hook != NULL)
5820 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
5824 if (name == NULL || *name == '\0')
5825 elfsym->st_name = 0;
5826 else if (input_sec->flags & SEC_EXCLUDE)
5827 elfsym->st_name = 0;
5830 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5832 if (elfsym->st_name == (unsigned long) -1)
5836 if (finfo->symbuf_count >= finfo->symbuf_size)
5838 if (! elf_link_flush_output_syms (finfo, bed))
5842 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
5843 destshndx = finfo->symshndxbuf;
5844 if (destshndx != NULL)
5846 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
5850 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
5851 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
5852 if (destshndx == NULL)
5854 memset ((char *) destshndx + amt, 0, amt);
5855 finfo->shndxbuf_size *= 2;
5857 destshndx += bfd_get_symcount (finfo->output_bfd);
5860 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
5861 finfo->symbuf_count += 1;
5862 bfd_get_symcount (finfo->output_bfd) += 1;
5867 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5868 allowing an unsatisfied unversioned symbol in the DSO to match a
5869 versioned symbol that would normally require an explicit version.
5870 We also handle the case that a DSO references a hidden symbol
5871 which may be satisfied by a versioned symbol in another DSO. */
5874 elf_link_check_versioned_symbol (struct bfd_link_info *info,
5875 const struct elf_backend_data *bed,
5876 struct elf_link_hash_entry *h)
5879 struct elf_link_loaded_list *loaded;
5881 if (!is_elf_hash_table (info->hash))
5884 switch (h->root.type)
5890 case bfd_link_hash_undefined:
5891 case bfd_link_hash_undefweak:
5892 abfd = h->root.u.undef.abfd;
5893 if ((abfd->flags & DYNAMIC) == 0
5894 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
5898 case bfd_link_hash_defined:
5899 case bfd_link_hash_defweak:
5900 abfd = h->root.u.def.section->owner;
5903 case bfd_link_hash_common:
5904 abfd = h->root.u.c.p->section->owner;
5907 BFD_ASSERT (abfd != NULL);
5909 for (loaded = elf_hash_table (info)->loaded;
5911 loaded = loaded->next)
5914 Elf_Internal_Shdr *hdr;
5915 bfd_size_type symcount;
5916 bfd_size_type extsymcount;
5917 bfd_size_type extsymoff;
5918 Elf_Internal_Shdr *versymhdr;
5919 Elf_Internal_Sym *isym;
5920 Elf_Internal_Sym *isymend;
5921 Elf_Internal_Sym *isymbuf;
5922 Elf_External_Versym *ever;
5923 Elf_External_Versym *extversym;
5925 input = loaded->abfd;
5927 /* We check each DSO for a possible hidden versioned definition. */
5929 || (input->flags & DYNAMIC) == 0
5930 || elf_dynversym (input) == 0)
5933 hdr = &elf_tdata (input)->dynsymtab_hdr;
5935 symcount = hdr->sh_size / bed->s->sizeof_sym;
5936 if (elf_bad_symtab (input))
5938 extsymcount = symcount;
5943 extsymcount = symcount - hdr->sh_info;
5944 extsymoff = hdr->sh_info;
5947 if (extsymcount == 0)
5950 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
5952 if (isymbuf == NULL)
5955 /* Read in any version definitions. */
5956 versymhdr = &elf_tdata (input)->dynversym_hdr;
5957 extversym = bfd_malloc (versymhdr->sh_size);
5958 if (extversym == NULL)
5961 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
5962 || (bfd_bread (extversym, versymhdr->sh_size, input)
5963 != versymhdr->sh_size))
5971 ever = extversym + extsymoff;
5972 isymend = isymbuf + extsymcount;
5973 for (isym = isymbuf; isym < isymend; isym++, ever++)
5976 Elf_Internal_Versym iver;
5977 unsigned short version_index;
5979 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
5980 || isym->st_shndx == SHN_UNDEF)
5983 name = bfd_elf_string_from_elf_section (input,
5986 if (strcmp (name, h->root.root.string) != 0)
5989 _bfd_elf_swap_versym_in (input, ever, &iver);
5991 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
5993 /* If we have a non-hidden versioned sym, then it should
5994 have provided a definition for the undefined sym. */
5998 version_index = iver.vs_vers & VERSYM_VERSION;
5999 if (version_index == 1 || version_index == 2)
6001 /* This is the base or first version. We can use it. */
6015 /* Add an external symbol to the symbol table. This is called from
6016 the hash table traversal routine. When generating a shared object,
6017 we go through the symbol table twice. The first time we output
6018 anything that might have been forced to local scope in a version
6019 script. The second time we output the symbols that are still
6023 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6025 struct elf_outext_info *eoinfo = data;
6026 struct elf_final_link_info *finfo = eoinfo->finfo;
6028 Elf_Internal_Sym sym;
6029 asection *input_sec;
6030 const struct elf_backend_data *bed;
6032 if (h->root.type == bfd_link_hash_warning)
6034 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6035 if (h->root.type == bfd_link_hash_new)
6039 /* Decide whether to output this symbol in this pass. */
6040 if (eoinfo->localsyms)
6042 if (!h->forced_local)
6047 if (h->forced_local)
6051 bed = get_elf_backend_data (finfo->output_bfd);
6053 /* If we have an undefined symbol reference here then it must have
6054 come from a shared library that is being linked in. (Undefined
6055 references in regular files have already been handled). If we
6056 are reporting errors for this situation then do so now. */
6057 if (h->root.type == bfd_link_hash_undefined
6060 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6061 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6063 if (! ((*finfo->info->callbacks->undefined_symbol)
6064 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6065 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6067 eoinfo->failed = TRUE;
6072 /* We should also warn if a forced local symbol is referenced from
6073 shared libraries. */
6074 if (! finfo->info->relocatable
6075 && (! finfo->info->shared)
6080 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6082 (*_bfd_error_handler)
6083 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6084 finfo->output_bfd, h->root.u.def.section->owner,
6085 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6087 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6088 ? "hidden" : "local",
6089 h->root.root.string);
6090 eoinfo->failed = TRUE;
6094 /* We don't want to output symbols that have never been mentioned by
6095 a regular file, or that we have been told to strip. However, if
6096 h->indx is set to -2, the symbol is used by a reloc and we must
6100 else if ((h->def_dynamic
6105 else if (finfo->info->strip == strip_all)
6107 else if (finfo->info->strip == strip_some
6108 && bfd_hash_lookup (finfo->info->keep_hash,
6109 h->root.root.string, FALSE, FALSE) == NULL)
6111 else if (finfo->info->strip_discarded
6112 && (h->root.type == bfd_link_hash_defined
6113 || h->root.type == bfd_link_hash_defweak)
6114 && elf_discarded_section (h->root.u.def.section))
6119 /* If we're stripping it, and it's not a dynamic symbol, there's
6120 nothing else to do unless it is a forced local symbol. */
6123 && !h->forced_local)
6127 sym.st_size = h->size;
6128 sym.st_other = h->other;
6129 if (h->forced_local)
6130 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6131 else if (h->root.type == bfd_link_hash_undefweak
6132 || h->root.type == bfd_link_hash_defweak)
6133 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6135 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6137 switch (h->root.type)
6140 case bfd_link_hash_new:
6141 case bfd_link_hash_warning:
6145 case bfd_link_hash_undefined:
6146 case bfd_link_hash_undefweak:
6147 input_sec = bfd_und_section_ptr;
6148 sym.st_shndx = SHN_UNDEF;
6151 case bfd_link_hash_defined:
6152 case bfd_link_hash_defweak:
6154 input_sec = h->root.u.def.section;
6155 if (input_sec->output_section != NULL)
6158 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6159 input_sec->output_section);
6160 if (sym.st_shndx == SHN_BAD)
6162 (*_bfd_error_handler)
6163 (_("%B: could not find output section %A for input section %A"),
6164 finfo->output_bfd, input_sec->output_section, input_sec);
6165 eoinfo->failed = TRUE;
6169 /* ELF symbols in relocatable files are section relative,
6170 but in nonrelocatable files they are virtual
6172 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6173 if (! finfo->info->relocatable)
6175 sym.st_value += input_sec->output_section->vma;
6176 if (h->type == STT_TLS)
6178 /* STT_TLS symbols are relative to PT_TLS segment
6180 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6181 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6187 BFD_ASSERT (input_sec->owner == NULL
6188 || (input_sec->owner->flags & DYNAMIC) != 0);
6189 sym.st_shndx = SHN_UNDEF;
6190 input_sec = bfd_und_section_ptr;
6195 case bfd_link_hash_common:
6196 input_sec = h->root.u.c.p->section;
6197 sym.st_shndx = SHN_COMMON;
6198 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6201 case bfd_link_hash_indirect:
6202 /* These symbols are created by symbol versioning. They point
6203 to the decorated version of the name. For example, if the
6204 symbol foo@@GNU_1.2 is the default, which should be used when
6205 foo is used with no version, then we add an indirect symbol
6206 foo which points to foo@@GNU_1.2. We ignore these symbols,
6207 since the indirected symbol is already in the hash table. */
6211 /* Give the processor backend a chance to tweak the symbol value,
6212 and also to finish up anything that needs to be done for this
6213 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6214 forced local syms when non-shared is due to a historical quirk. */
6215 if ((h->dynindx != -1
6217 && ((finfo->info->shared
6218 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6219 || h->root.type != bfd_link_hash_undefweak))
6220 || !h->forced_local)
6221 && elf_hash_table (finfo->info)->dynamic_sections_created)
6223 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6224 (finfo->output_bfd, finfo->info, h, &sym)))
6226 eoinfo->failed = TRUE;
6231 /* If we are marking the symbol as undefined, and there are no
6232 non-weak references to this symbol from a regular object, then
6233 mark the symbol as weak undefined; if there are non-weak
6234 references, mark the symbol as strong. We can't do this earlier,
6235 because it might not be marked as undefined until the
6236 finish_dynamic_symbol routine gets through with it. */
6237 if (sym.st_shndx == SHN_UNDEF
6239 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6240 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6244 if (h->ref_regular_nonweak)
6245 bindtype = STB_GLOBAL;
6247 bindtype = STB_WEAK;
6248 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6251 /* If a non-weak symbol with non-default visibility is not defined
6252 locally, it is a fatal error. */
6253 if (! finfo->info->relocatable
6254 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6255 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6256 && h->root.type == bfd_link_hash_undefined
6259 (*_bfd_error_handler)
6260 (_("%B: %s symbol `%s' isn't defined"),
6262 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6264 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6265 ? "internal" : "hidden",
6266 h->root.root.string);
6267 eoinfo->failed = TRUE;
6271 /* If this symbol should be put in the .dynsym section, then put it
6272 there now. We already know the symbol index. We also fill in
6273 the entry in the .hash section. */
6274 if (h->dynindx != -1
6275 && elf_hash_table (finfo->info)->dynamic_sections_created)
6279 size_t hash_entry_size;
6280 bfd_byte *bucketpos;
6284 sym.st_name = h->dynstr_index;
6285 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6286 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6288 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6289 bucket = h->u.elf_hash_value % bucketcount;
6291 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6292 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6293 + (bucket + 2) * hash_entry_size);
6294 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6295 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6296 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6297 ((bfd_byte *) finfo->hash_sec->contents
6298 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6300 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6302 Elf_Internal_Versym iversym;
6303 Elf_External_Versym *eversym;
6305 if (!h->def_regular)
6307 if (h->verinfo.verdef == NULL)
6308 iversym.vs_vers = 0;
6310 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6314 if (h->verinfo.vertree == NULL)
6315 iversym.vs_vers = 1;
6317 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6321 iversym.vs_vers |= VERSYM_HIDDEN;
6323 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6324 eversym += h->dynindx;
6325 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6329 /* If we're stripping it, then it was just a dynamic symbol, and
6330 there's nothing else to do. */
6331 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6334 h->indx = bfd_get_symcount (finfo->output_bfd);
6336 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6338 eoinfo->failed = TRUE;
6345 /* Return TRUE if special handling is done for relocs in SEC against
6346 symbols defined in discarded sections. */
6349 elf_section_ignore_discarded_relocs (asection *sec)
6351 const struct elf_backend_data *bed;
6353 switch (sec->sec_info_type)
6355 case ELF_INFO_TYPE_STABS:
6356 case ELF_INFO_TYPE_EH_FRAME:
6362 bed = get_elf_backend_data (sec->owner);
6363 if (bed->elf_backend_ignore_discarded_relocs != NULL
6364 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6370 /* Return TRUE if we should complain about a reloc in SEC against a
6371 symbol defined in a discarded section. */
6374 elf_section_complain_discarded (asection *sec)
6376 if (strncmp (".stab", sec->name, 5) == 0
6377 && (!sec->name[5] ||
6378 (sec->name[5] == '.' && ISDIGIT (sec->name[6]))))
6381 if (strcmp (".eh_frame", sec->name) == 0)
6384 if (strcmp (".gcc_except_table", sec->name) == 0)
6387 if (strcmp (".PARISC.unwind", sec->name) == 0)
6390 if (strcmp (".fixup", sec->name) == 0)
6396 /* Find a match between a section and a member of a section group. */
6399 match_group_member (asection *sec, asection *group)
6401 asection *first = elf_next_in_group (group);
6402 asection *s = first;
6406 if (bfd_elf_match_symbols_in_sections (s, sec))
6416 /* Link an input file into the linker output file. This function
6417 handles all the sections and relocations of the input file at once.
6418 This is so that we only have to read the local symbols once, and
6419 don't have to keep them in memory. */
6422 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6424 bfd_boolean (*relocate_section)
6425 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6426 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6428 Elf_Internal_Shdr *symtab_hdr;
6431 Elf_Internal_Sym *isymbuf;
6432 Elf_Internal_Sym *isym;
6433 Elf_Internal_Sym *isymend;
6435 asection **ppsection;
6437 const struct elf_backend_data *bed;
6438 bfd_boolean emit_relocs;
6439 struct elf_link_hash_entry **sym_hashes;
6441 output_bfd = finfo->output_bfd;
6442 bed = get_elf_backend_data (output_bfd);
6443 relocate_section = bed->elf_backend_relocate_section;
6445 /* If this is a dynamic object, we don't want to do anything here:
6446 we don't want the local symbols, and we don't want the section
6448 if ((input_bfd->flags & DYNAMIC) != 0)
6451 emit_relocs = (finfo->info->relocatable
6452 || finfo->info->emitrelocations
6453 || bed->elf_backend_emit_relocs);
6455 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6456 if (elf_bad_symtab (input_bfd))
6458 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6463 locsymcount = symtab_hdr->sh_info;
6464 extsymoff = symtab_hdr->sh_info;
6467 /* Read the local symbols. */
6468 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6469 if (isymbuf == NULL && locsymcount != 0)
6471 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6472 finfo->internal_syms,
6473 finfo->external_syms,
6474 finfo->locsym_shndx);
6475 if (isymbuf == NULL)
6479 /* Find local symbol sections and adjust values of symbols in
6480 SEC_MERGE sections. Write out those local symbols we know are
6481 going into the output file. */
6482 isymend = isymbuf + locsymcount;
6483 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6485 isym++, pindex++, ppsection++)
6489 Elf_Internal_Sym osym;
6493 if (elf_bad_symtab (input_bfd))
6495 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6502 if (isym->st_shndx == SHN_UNDEF)
6503 isec = bfd_und_section_ptr;
6504 else if (isym->st_shndx < SHN_LORESERVE
6505 || isym->st_shndx > SHN_HIRESERVE)
6507 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6509 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6510 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6512 _bfd_merged_section_offset (output_bfd, &isec,
6513 elf_section_data (isec)->sec_info,
6516 else if (isym->st_shndx == SHN_ABS)
6517 isec = bfd_abs_section_ptr;
6518 else if (isym->st_shndx == SHN_COMMON)
6519 isec = bfd_com_section_ptr;
6528 /* Don't output the first, undefined, symbol. */
6529 if (ppsection == finfo->sections)
6532 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6534 /* We never output section symbols. Instead, we use the
6535 section symbol of the corresponding section in the output
6540 /* If we are stripping all symbols, we don't want to output this
6542 if (finfo->info->strip == strip_all)
6545 /* If we are discarding all local symbols, we don't want to
6546 output this one. If we are generating a relocatable output
6547 file, then some of the local symbols may be required by
6548 relocs; we output them below as we discover that they are
6550 if (finfo->info->discard == discard_all)
6553 /* If this symbol is defined in a section which we are
6554 discarding, we don't need to keep it, but note that
6555 linker_mark is only reliable for sections that have contents.
6556 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6557 as well as linker_mark. */
6558 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6560 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6561 || (! finfo->info->relocatable
6562 && (isec->flags & SEC_EXCLUDE) != 0)))
6565 /* Get the name of the symbol. */
6566 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6571 /* See if we are discarding symbols with this name. */
6572 if ((finfo->info->strip == strip_some
6573 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6575 || (((finfo->info->discard == discard_sec_merge
6576 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6577 || finfo->info->discard == discard_l)
6578 && bfd_is_local_label_name (input_bfd, name)))
6581 /* If we get here, we are going to output this symbol. */
6585 /* Adjust the section index for the output file. */
6586 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6587 isec->output_section);
6588 if (osym.st_shndx == SHN_BAD)
6591 *pindex = bfd_get_symcount (output_bfd);
6593 /* ELF symbols in relocatable files are section relative, but
6594 in executable files they are virtual addresses. Note that
6595 this code assumes that all ELF sections have an associated
6596 BFD section with a reasonable value for output_offset; below
6597 we assume that they also have a reasonable value for
6598 output_section. Any special sections must be set up to meet
6599 these requirements. */
6600 osym.st_value += isec->output_offset;
6601 if (! finfo->info->relocatable)
6603 osym.st_value += isec->output_section->vma;
6604 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6606 /* STT_TLS symbols are relative to PT_TLS segment base. */
6607 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6608 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6612 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6616 /* Relocate the contents of each section. */
6617 sym_hashes = elf_sym_hashes (input_bfd);
6618 for (o = input_bfd->sections; o != NULL; o = o->next)
6622 if (! o->linker_mark)
6624 /* This section was omitted from the link. */
6628 if ((o->flags & SEC_HAS_CONTENTS) == 0
6629 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6632 if ((o->flags & SEC_LINKER_CREATED) != 0)
6634 /* Section was created by _bfd_elf_link_create_dynamic_sections
6639 /* Get the contents of the section. They have been cached by a
6640 relaxation routine. Note that o is a section in an input
6641 file, so the contents field will not have been set by any of
6642 the routines which work on output files. */
6643 if (elf_section_data (o)->this_hdr.contents != NULL)
6644 contents = elf_section_data (o)->this_hdr.contents;
6647 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6649 contents = finfo->contents;
6650 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6654 if ((o->flags & SEC_RELOC) != 0)
6656 Elf_Internal_Rela *internal_relocs;
6657 bfd_vma r_type_mask;
6660 /* Get the swapped relocs. */
6662 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6663 finfo->internal_relocs, FALSE);
6664 if (internal_relocs == NULL
6665 && o->reloc_count > 0)
6668 if (bed->s->arch_size == 32)
6675 r_type_mask = 0xffffffff;
6679 /* Run through the relocs looking for any against symbols
6680 from discarded sections and section symbols from
6681 removed link-once sections. Complain about relocs
6682 against discarded sections. Zero relocs against removed
6683 link-once sections. Preserve debug information as much
6685 if (!elf_section_ignore_discarded_relocs (o))
6687 Elf_Internal_Rela *rel, *relend;
6688 bfd_boolean complain = elf_section_complain_discarded (o);
6690 rel = internal_relocs;
6691 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6692 for ( ; rel < relend; rel++)
6694 unsigned long r_symndx = rel->r_info >> r_sym_shift;
6695 asection **ps, *sec;
6696 struct elf_link_hash_entry *h = NULL;
6697 const char *sym_name;
6699 if (r_symndx == STN_UNDEF)
6702 if (r_symndx >= locsymcount
6703 || (elf_bad_symtab (input_bfd)
6704 && finfo->sections[r_symndx] == NULL))
6706 h = sym_hashes[r_symndx - extsymoff];
6707 while (h->root.type == bfd_link_hash_indirect
6708 || h->root.type == bfd_link_hash_warning)
6709 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6711 if (h->root.type != bfd_link_hash_defined
6712 && h->root.type != bfd_link_hash_defweak)
6715 ps = &h->root.u.def.section;
6716 sym_name = h->root.root.string;
6720 Elf_Internal_Sym *sym = isymbuf + r_symndx;
6721 ps = &finfo->sections[r_symndx];
6722 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
6725 /* Complain if the definition comes from a
6726 discarded section. */
6727 if ((sec = *ps) != NULL && elf_discarded_section (sec))
6729 if ((o->flags & SEC_DEBUGGING) != 0)
6731 BFD_ASSERT (r_symndx != 0);
6733 /* Try to preserve debug information.
6734 FIXME: This is quite broken. Modifying
6735 the symbol here means we will be changing
6736 all uses of the symbol, not just those in
6737 debug sections. The only thing that makes
6738 this half reasonable is that debug sections
6739 tend to come after other sections. Of
6740 course, that doesn't help with globals.
6741 ??? All link-once sections of the same name
6742 ought to define the same set of symbols, so
6743 it would seem that globals ought to always
6744 be defined in the kept section. */
6745 if (sec->kept_section != NULL)
6749 /* Check if it is a linkonce section or
6750 member of a comdat group. */
6751 if (elf_sec_group (sec) == NULL
6752 && sec->size == sec->kept_section->size)
6754 *ps = sec->kept_section;
6757 else if (elf_sec_group (sec) != NULL
6758 && (member = match_group_member (sec, sec->kept_section))
6759 && sec->size == member->size)
6768 (*_bfd_error_handler)
6769 (_("`%s' referenced in section `%A' of %B: "
6770 "defined in discarded section `%A' of %B\n"),
6771 o, input_bfd, sec, sec->owner, sym_name);
6774 /* Remove the symbol reference from the reloc, but
6775 don't kill the reloc completely. This is so that
6776 a zero value will be written into the section,
6777 which may have non-zero contents put there by the
6778 assembler. Zero in things like an eh_frame fde
6779 pc_begin allows stack unwinders to recognize the
6781 rel->r_info &= r_type_mask;
6787 /* Relocate the section by invoking a back end routine.
6789 The back end routine is responsible for adjusting the
6790 section contents as necessary, and (if using Rela relocs
6791 and generating a relocatable output file) adjusting the
6792 reloc addend as necessary.
6794 The back end routine does not have to worry about setting
6795 the reloc address or the reloc symbol index.
6797 The back end routine is given a pointer to the swapped in
6798 internal symbols, and can access the hash table entries
6799 for the external symbols via elf_sym_hashes (input_bfd).
6801 When generating relocatable output, the back end routine
6802 must handle STB_LOCAL/STT_SECTION symbols specially. The
6803 output symbol is going to be a section symbol
6804 corresponding to the output section, which will require
6805 the addend to be adjusted. */
6807 if (! (*relocate_section) (output_bfd, finfo->info,
6808 input_bfd, o, contents,
6816 Elf_Internal_Rela *irela;
6817 Elf_Internal_Rela *irelaend;
6818 bfd_vma last_offset;
6819 struct elf_link_hash_entry **rel_hash;
6820 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
6821 unsigned int next_erel;
6822 bfd_boolean (*reloc_emitter)
6823 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
6824 bfd_boolean rela_normal;
6826 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6827 rela_normal = (bed->rela_normal
6828 && (input_rel_hdr->sh_entsize
6829 == bed->s->sizeof_rela));
6831 /* Adjust the reloc addresses and symbol indices. */
6833 irela = internal_relocs;
6834 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6835 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6836 + elf_section_data (o->output_section)->rel_count
6837 + elf_section_data (o->output_section)->rel_count2);
6838 last_offset = o->output_offset;
6839 if (!finfo->info->relocatable)
6840 last_offset += o->output_section->vma;
6841 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6843 unsigned long r_symndx;
6845 Elf_Internal_Sym sym;
6847 if (next_erel == bed->s->int_rels_per_ext_rel)
6853 irela->r_offset = _bfd_elf_section_offset (output_bfd,
6856 if (irela->r_offset >= (bfd_vma) -2)
6858 /* This is a reloc for a deleted entry or somesuch.
6859 Turn it into an R_*_NONE reloc, at the same
6860 offset as the last reloc. elf_eh_frame.c and
6861 elf_bfd_discard_info rely on reloc offsets
6863 irela->r_offset = last_offset;
6865 irela->r_addend = 0;
6869 irela->r_offset += o->output_offset;
6871 /* Relocs in an executable have to be virtual addresses. */
6872 if (!finfo->info->relocatable)
6873 irela->r_offset += o->output_section->vma;
6875 last_offset = irela->r_offset;
6877 r_symndx = irela->r_info >> r_sym_shift;
6878 if (r_symndx == STN_UNDEF)
6881 if (r_symndx >= locsymcount
6882 || (elf_bad_symtab (input_bfd)
6883 && finfo->sections[r_symndx] == NULL))
6885 struct elf_link_hash_entry *rh;
6888 /* This is a reloc against a global symbol. We
6889 have not yet output all the local symbols, so
6890 we do not know the symbol index of any global
6891 symbol. We set the rel_hash entry for this
6892 reloc to point to the global hash table entry
6893 for this symbol. The symbol index is then
6894 set at the end of bfd_elf_final_link. */
6895 indx = r_symndx - extsymoff;
6896 rh = elf_sym_hashes (input_bfd)[indx];
6897 while (rh->root.type == bfd_link_hash_indirect
6898 || rh->root.type == bfd_link_hash_warning)
6899 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6901 /* Setting the index to -2 tells
6902 elf_link_output_extsym that this symbol is
6904 BFD_ASSERT (rh->indx < 0);
6912 /* This is a reloc against a local symbol. */
6915 sym = isymbuf[r_symndx];
6916 sec = finfo->sections[r_symndx];
6917 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
6919 /* I suppose the backend ought to fill in the
6920 section of any STT_SECTION symbol against a
6921 processor specific section. */
6923 if (bfd_is_abs_section (sec))
6925 else if (sec == NULL || sec->owner == NULL)
6927 bfd_set_error (bfd_error_bad_value);
6932 asection *osec = sec->output_section;
6934 /* If we have discarded a section, the output
6935 section will be the absolute section. In
6936 case of discarded link-once and discarded
6937 SEC_MERGE sections, use the kept section. */
6938 if (bfd_is_abs_section (osec)
6939 && sec->kept_section != NULL
6940 && sec->kept_section->output_section != NULL)
6942 osec = sec->kept_section->output_section;
6943 irela->r_addend -= osec->vma;
6946 if (!bfd_is_abs_section (osec))
6948 r_symndx = osec->target_index;
6949 BFD_ASSERT (r_symndx != 0);
6953 /* Adjust the addend according to where the
6954 section winds up in the output section. */
6956 irela->r_addend += sec->output_offset;
6960 if (finfo->indices[r_symndx] == -1)
6962 unsigned long shlink;
6966 if (finfo->info->strip == strip_all)
6968 /* You can't do ld -r -s. */
6969 bfd_set_error (bfd_error_invalid_operation);
6973 /* This symbol was skipped earlier, but
6974 since it is needed by a reloc, we
6975 must output it now. */
6976 shlink = symtab_hdr->sh_link;
6977 name = (bfd_elf_string_from_elf_section
6978 (input_bfd, shlink, sym.st_name));
6982 osec = sec->output_section;
6984 _bfd_elf_section_from_bfd_section (output_bfd,
6986 if (sym.st_shndx == SHN_BAD)
6989 sym.st_value += sec->output_offset;
6990 if (! finfo->info->relocatable)
6992 sym.st_value += osec->vma;
6993 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
6995 /* STT_TLS symbols are relative to PT_TLS
6997 BFD_ASSERT (elf_hash_table (finfo->info)
6999 sym.st_value -= (elf_hash_table (finfo->info)
7004 finfo->indices[r_symndx]
7005 = bfd_get_symcount (output_bfd);
7007 if (! elf_link_output_sym (finfo, name, &sym, sec,
7012 r_symndx = finfo->indices[r_symndx];
7015 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7016 | (irela->r_info & r_type_mask));
7019 /* Swap out the relocs. */
7020 if (bed->elf_backend_emit_relocs
7021 && !(finfo->info->relocatable
7022 || finfo->info->emitrelocations))
7023 reloc_emitter = bed->elf_backend_emit_relocs;
7025 reloc_emitter = _bfd_elf_link_output_relocs;
7027 if (input_rel_hdr->sh_size != 0
7028 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
7032 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7033 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7035 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7036 * bed->s->int_rels_per_ext_rel);
7037 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
7044 /* Write out the modified section contents. */
7045 if (bed->elf_backend_write_section
7046 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7048 /* Section written out. */
7050 else switch (o->sec_info_type)
7052 case ELF_INFO_TYPE_STABS:
7053 if (! (_bfd_write_section_stabs
7055 &elf_hash_table (finfo->info)->stab_info,
7056 o, &elf_section_data (o)->sec_info, contents)))
7059 case ELF_INFO_TYPE_MERGE:
7060 if (! _bfd_write_merged_section (output_bfd, o,
7061 elf_section_data (o)->sec_info))
7064 case ELF_INFO_TYPE_EH_FRAME:
7066 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7073 if (! (o->flags & SEC_EXCLUDE)
7074 && ! bfd_set_section_contents (output_bfd, o->output_section,
7076 (file_ptr) o->output_offset,
7087 /* Generate a reloc when linking an ELF file. This is a reloc
7088 requested by the linker, and does come from any input file. This
7089 is used to build constructor and destructor tables when linking
7093 elf_reloc_link_order (bfd *output_bfd,
7094 struct bfd_link_info *info,
7095 asection *output_section,
7096 struct bfd_link_order *link_order)
7098 reloc_howto_type *howto;
7102 struct elf_link_hash_entry **rel_hash_ptr;
7103 Elf_Internal_Shdr *rel_hdr;
7104 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7105 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7109 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7112 bfd_set_error (bfd_error_bad_value);
7116 addend = link_order->u.reloc.p->addend;
7118 /* Figure out the symbol index. */
7119 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7120 + elf_section_data (output_section)->rel_count
7121 + elf_section_data (output_section)->rel_count2);
7122 if (link_order->type == bfd_section_reloc_link_order)
7124 indx = link_order->u.reloc.p->u.section->target_index;
7125 BFD_ASSERT (indx != 0);
7126 *rel_hash_ptr = NULL;
7130 struct elf_link_hash_entry *h;
7132 /* Treat a reloc against a defined symbol as though it were
7133 actually against the section. */
7134 h = ((struct elf_link_hash_entry *)
7135 bfd_wrapped_link_hash_lookup (output_bfd, info,
7136 link_order->u.reloc.p->u.name,
7137 FALSE, FALSE, TRUE));
7139 && (h->root.type == bfd_link_hash_defined
7140 || h->root.type == bfd_link_hash_defweak))
7144 section = h->root.u.def.section;
7145 indx = section->output_section->target_index;
7146 *rel_hash_ptr = NULL;
7147 /* It seems that we ought to add the symbol value to the
7148 addend here, but in practice it has already been added
7149 because it was passed to constructor_callback. */
7150 addend += section->output_section->vma + section->output_offset;
7154 /* Setting the index to -2 tells elf_link_output_extsym that
7155 this symbol is used by a reloc. */
7162 if (! ((*info->callbacks->unattached_reloc)
7163 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7169 /* If this is an inplace reloc, we must write the addend into the
7171 if (howto->partial_inplace && addend != 0)
7174 bfd_reloc_status_type rstat;
7177 const char *sym_name;
7179 size = bfd_get_reloc_size (howto);
7180 buf = bfd_zmalloc (size);
7183 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7190 case bfd_reloc_outofrange:
7193 case bfd_reloc_overflow:
7194 if (link_order->type == bfd_section_reloc_link_order)
7195 sym_name = bfd_section_name (output_bfd,
7196 link_order->u.reloc.p->u.section);
7198 sym_name = link_order->u.reloc.p->u.name;
7199 if (! ((*info->callbacks->reloc_overflow)
7200 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
7207 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7208 link_order->offset, size);
7214 /* The address of a reloc is relative to the section in a
7215 relocatable file, and is a virtual address in an executable
7217 offset = link_order->offset;
7218 if (! info->relocatable)
7219 offset += output_section->vma;
7221 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7223 irel[i].r_offset = offset;
7225 irel[i].r_addend = 0;
7227 if (bed->s->arch_size == 32)
7228 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7230 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7232 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7233 erel = rel_hdr->contents;
7234 if (rel_hdr->sh_type == SHT_REL)
7236 erel += (elf_section_data (output_section)->rel_count
7237 * bed->s->sizeof_rel);
7238 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7242 irel[0].r_addend = addend;
7243 erel += (elf_section_data (output_section)->rel_count
7244 * bed->s->sizeof_rela);
7245 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7248 ++elf_section_data (output_section)->rel_count;
7254 /* Get the output vma of the section pointed to by the sh_link field. */
7257 elf_get_linked_section_vma (struct bfd_link_order *p)
7259 Elf_Internal_Shdr **elf_shdrp;
7263 s = p->u.indirect.section;
7264 elf_shdrp = elf_elfsections (s->owner);
7265 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7266 elfsec = elf_shdrp[elfsec]->sh_link;
7268 The Intel C compiler generates SHT_IA_64_UNWIND with
7269 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7270 sh_info fields. Hence we could get the situation
7271 where elfsec is 0. */
7274 const struct elf_backend_data *bed
7275 = get_elf_backend_data (s->owner);
7276 if (bed->link_order_error_handler)
7277 bed->link_order_error_handler
7278 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7283 s = elf_shdrp[elfsec]->bfd_section;
7284 return s->output_section->vma + s->output_offset;
7289 /* Compare two sections based on the locations of the sections they are
7290 linked to. Used by elf_fixup_link_order. */
7293 compare_link_order (const void * a, const void * b)
7298 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7299 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7306 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7307 order as their linked sections. Returns false if this could not be done
7308 because an output section includes both ordered and unordered
7309 sections. Ideally we'd do this in the linker proper. */
7312 elf_fixup_link_order (bfd *abfd, asection *o)
7317 struct bfd_link_order *p;
7319 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7321 struct bfd_link_order **sections;
7327 for (p = o->link_order_head; p != NULL; p = p->next)
7329 if (p->type == bfd_indirect_link_order
7330 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7331 == bfd_target_elf_flavour)
7332 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7334 s = p->u.indirect.section;
7335 elfsec = _bfd_elf_section_from_bfd_section (sub, s);
7337 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7346 if (!seen_linkorder)
7349 if (seen_other && seen_linkorder)
7351 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7353 bfd_set_error (bfd_error_bad_value);
7357 sections = (struct bfd_link_order **)
7358 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7361 for (p = o->link_order_head; p != NULL; p = p->next)
7363 sections[seen_linkorder++] = p;
7365 /* Sort the input sections in the order of their linked section. */
7366 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7367 compare_link_order);
7369 /* Change the offsets of the sections. */
7371 for (n = 0; n < seen_linkorder; n++)
7373 s = sections[n]->u.indirect.section;
7374 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7375 s->output_offset = offset;
7376 sections[n]->offset = offset;
7377 offset += sections[n]->size;
7384 /* Do the final step of an ELF link. */
7387 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7389 bfd_boolean dynamic;
7390 bfd_boolean emit_relocs;
7392 struct elf_final_link_info finfo;
7393 register asection *o;
7394 register struct bfd_link_order *p;
7396 bfd_size_type max_contents_size;
7397 bfd_size_type max_external_reloc_size;
7398 bfd_size_type max_internal_reloc_count;
7399 bfd_size_type max_sym_count;
7400 bfd_size_type max_sym_shndx_count;
7402 Elf_Internal_Sym elfsym;
7404 Elf_Internal_Shdr *symtab_hdr;
7405 Elf_Internal_Shdr *symtab_shndx_hdr;
7406 Elf_Internal_Shdr *symstrtab_hdr;
7407 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7408 struct elf_outext_info eoinfo;
7410 size_t relativecount = 0;
7411 asection *reldyn = 0;
7414 if (! is_elf_hash_table (info->hash))
7418 abfd->flags |= DYNAMIC;
7420 dynamic = elf_hash_table (info)->dynamic_sections_created;
7421 dynobj = elf_hash_table (info)->dynobj;
7423 emit_relocs = (info->relocatable
7424 || info->emitrelocations
7425 || bed->elf_backend_emit_relocs);
7428 finfo.output_bfd = abfd;
7429 finfo.symstrtab = _bfd_elf_stringtab_init ();
7430 if (finfo.symstrtab == NULL)
7435 finfo.dynsym_sec = NULL;
7436 finfo.hash_sec = NULL;
7437 finfo.symver_sec = NULL;
7441 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7442 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7443 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7444 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7445 /* Note that it is OK if symver_sec is NULL. */
7448 finfo.contents = NULL;
7449 finfo.external_relocs = NULL;
7450 finfo.internal_relocs = NULL;
7451 finfo.external_syms = NULL;
7452 finfo.locsym_shndx = NULL;
7453 finfo.internal_syms = NULL;
7454 finfo.indices = NULL;
7455 finfo.sections = NULL;
7456 finfo.symbuf = NULL;
7457 finfo.symshndxbuf = NULL;
7458 finfo.symbuf_count = 0;
7459 finfo.shndxbuf_size = 0;
7461 /* Count up the number of relocations we will output for each output
7462 section, so that we know the sizes of the reloc sections. We
7463 also figure out some maximum sizes. */
7464 max_contents_size = 0;
7465 max_external_reloc_size = 0;
7466 max_internal_reloc_count = 0;
7468 max_sym_shndx_count = 0;
7470 for (o = abfd->sections; o != NULL; o = o->next)
7472 struct bfd_elf_section_data *esdo = elf_section_data (o);
7475 for (p = o->link_order_head; p != NULL; p = p->next)
7477 unsigned int reloc_count = 0;
7478 struct bfd_elf_section_data *esdi = NULL;
7479 unsigned int *rel_count1;
7481 if (p->type == bfd_section_reloc_link_order
7482 || p->type == bfd_symbol_reloc_link_order)
7484 else if (p->type == bfd_indirect_link_order)
7488 sec = p->u.indirect.section;
7489 esdi = elf_section_data (sec);
7491 /* Mark all sections which are to be included in the
7492 link. This will normally be every section. We need
7493 to do this so that we can identify any sections which
7494 the linker has decided to not include. */
7495 sec->linker_mark = TRUE;
7497 if (sec->flags & SEC_MERGE)
7500 if (info->relocatable || info->emitrelocations)
7501 reloc_count = sec->reloc_count;
7502 else if (bed->elf_backend_count_relocs)
7504 Elf_Internal_Rela * relocs;
7506 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7509 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7511 if (elf_section_data (o)->relocs != relocs)
7515 if (sec->rawsize > max_contents_size)
7516 max_contents_size = sec->rawsize;
7517 if (sec->size > max_contents_size)
7518 max_contents_size = sec->size;
7520 /* We are interested in just local symbols, not all
7522 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7523 && (sec->owner->flags & DYNAMIC) == 0)
7527 if (elf_bad_symtab (sec->owner))
7528 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7529 / bed->s->sizeof_sym);
7531 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7533 if (sym_count > max_sym_count)
7534 max_sym_count = sym_count;
7536 if (sym_count > max_sym_shndx_count
7537 && elf_symtab_shndx (sec->owner) != 0)
7538 max_sym_shndx_count = sym_count;
7540 if ((sec->flags & SEC_RELOC) != 0)
7544 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7545 if (ext_size > max_external_reloc_size)
7546 max_external_reloc_size = ext_size;
7547 if (sec->reloc_count > max_internal_reloc_count)
7548 max_internal_reloc_count = sec->reloc_count;
7553 if (reloc_count == 0)
7556 o->reloc_count += reloc_count;
7558 /* MIPS may have a mix of REL and RELA relocs on sections.
7559 To support this curious ABI we keep reloc counts in
7560 elf_section_data too. We must be careful to add the
7561 relocations from the input section to the right output
7562 count. FIXME: Get rid of one count. We have
7563 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7564 rel_count1 = &esdo->rel_count;
7567 bfd_boolean same_size;
7568 bfd_size_type entsize1;
7570 entsize1 = esdi->rel_hdr.sh_entsize;
7571 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7572 || entsize1 == bed->s->sizeof_rela);
7573 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7576 rel_count1 = &esdo->rel_count2;
7578 if (esdi->rel_hdr2 != NULL)
7580 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7581 unsigned int alt_count;
7582 unsigned int *rel_count2;
7584 BFD_ASSERT (entsize2 != entsize1
7585 && (entsize2 == bed->s->sizeof_rel
7586 || entsize2 == bed->s->sizeof_rela));
7588 rel_count2 = &esdo->rel_count2;
7590 rel_count2 = &esdo->rel_count;
7592 /* The following is probably too simplistic if the
7593 backend counts output relocs unusually. */
7594 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7595 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7596 *rel_count2 += alt_count;
7597 reloc_count -= alt_count;
7600 *rel_count1 += reloc_count;
7603 if (o->reloc_count > 0)
7604 o->flags |= SEC_RELOC;
7607 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7608 set it (this is probably a bug) and if it is set
7609 assign_section_numbers will create a reloc section. */
7610 o->flags &=~ SEC_RELOC;
7613 /* If the SEC_ALLOC flag is not set, force the section VMA to
7614 zero. This is done in elf_fake_sections as well, but forcing
7615 the VMA to 0 here will ensure that relocs against these
7616 sections are handled correctly. */
7617 if ((o->flags & SEC_ALLOC) == 0
7618 && ! o->user_set_vma)
7622 if (! info->relocatable && merged)
7623 elf_link_hash_traverse (elf_hash_table (info),
7624 _bfd_elf_link_sec_merge_syms, abfd);
7626 /* Figure out the file positions for everything but the symbol table
7627 and the relocs. We set symcount to force assign_section_numbers
7628 to create a symbol table. */
7629 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7630 BFD_ASSERT (! abfd->output_has_begun);
7631 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7634 /* Set sizes, and assign file positions for reloc sections. */
7635 for (o = abfd->sections; o != NULL; o = o->next)
7637 if ((o->flags & SEC_RELOC) != 0)
7639 if (!(_bfd_elf_link_size_reloc_section
7640 (abfd, &elf_section_data (o)->rel_hdr, o)))
7643 if (elf_section_data (o)->rel_hdr2
7644 && !(_bfd_elf_link_size_reloc_section
7645 (abfd, elf_section_data (o)->rel_hdr2, o)))
7649 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7650 to count upwards while actually outputting the relocations. */
7651 elf_section_data (o)->rel_count = 0;
7652 elf_section_data (o)->rel_count2 = 0;
7655 _bfd_elf_assign_file_positions_for_relocs (abfd);
7657 /* We have now assigned file positions for all the sections except
7658 .symtab and .strtab. We start the .symtab section at the current
7659 file position, and write directly to it. We build the .strtab
7660 section in memory. */
7661 bfd_get_symcount (abfd) = 0;
7662 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7663 /* sh_name is set in prep_headers. */
7664 symtab_hdr->sh_type = SHT_SYMTAB;
7665 /* sh_flags, sh_addr and sh_size all start off zero. */
7666 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7667 /* sh_link is set in assign_section_numbers. */
7668 /* sh_info is set below. */
7669 /* sh_offset is set just below. */
7670 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
7672 off = elf_tdata (abfd)->next_file_pos;
7673 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
7675 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7676 incorrect. We do not yet know the size of the .symtab section.
7677 We correct next_file_pos below, after we do know the size. */
7679 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7680 continuously seeking to the right position in the file. */
7681 if (! info->keep_memory || max_sym_count < 20)
7682 finfo.symbuf_size = 20;
7684 finfo.symbuf_size = max_sym_count;
7685 amt = finfo.symbuf_size;
7686 amt *= bed->s->sizeof_sym;
7687 finfo.symbuf = bfd_malloc (amt);
7688 if (finfo.symbuf == NULL)
7690 if (elf_numsections (abfd) > SHN_LORESERVE)
7692 /* Wild guess at number of output symbols. realloc'd as needed. */
7693 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
7694 finfo.shndxbuf_size = amt;
7695 amt *= sizeof (Elf_External_Sym_Shndx);
7696 finfo.symshndxbuf = bfd_zmalloc (amt);
7697 if (finfo.symshndxbuf == NULL)
7701 /* Start writing out the symbol table. The first symbol is always a
7703 if (info->strip != strip_all
7706 elfsym.st_value = 0;
7709 elfsym.st_other = 0;
7710 elfsym.st_shndx = SHN_UNDEF;
7711 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
7717 /* Some standard ELF linkers do this, but we don't because it causes
7718 bootstrap comparison failures. */
7719 /* Output a file symbol for the output file as the second symbol.
7720 We output this even if we are discarding local symbols, although
7721 I'm not sure if this is correct. */
7722 elfsym.st_value = 0;
7724 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7725 elfsym.st_other = 0;
7726 elfsym.st_shndx = SHN_ABS;
7727 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
7728 &elfsym, bfd_abs_section_ptr, NULL))
7732 /* Output a symbol for each section. We output these even if we are
7733 discarding local symbols, since they are used for relocs. These
7734 symbols have no names. We store the index of each one in the
7735 index field of the section, so that we can find it again when
7736 outputting relocs. */
7737 if (info->strip != strip_all
7741 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7742 elfsym.st_other = 0;
7743 for (i = 1; i < elf_numsections (abfd); i++)
7745 o = bfd_section_from_elf_index (abfd, i);
7747 o->target_index = bfd_get_symcount (abfd);
7748 elfsym.st_shndx = i;
7749 if (info->relocatable || o == NULL)
7750 elfsym.st_value = 0;
7752 elfsym.st_value = o->vma;
7753 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
7755 if (i == SHN_LORESERVE - 1)
7756 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
7760 /* Allocate some memory to hold information read in from the input
7762 if (max_contents_size != 0)
7764 finfo.contents = bfd_malloc (max_contents_size);
7765 if (finfo.contents == NULL)
7769 if (max_external_reloc_size != 0)
7771 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
7772 if (finfo.external_relocs == NULL)
7776 if (max_internal_reloc_count != 0)
7778 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
7779 amt *= sizeof (Elf_Internal_Rela);
7780 finfo.internal_relocs = bfd_malloc (amt);
7781 if (finfo.internal_relocs == NULL)
7785 if (max_sym_count != 0)
7787 amt = max_sym_count * bed->s->sizeof_sym;
7788 finfo.external_syms = bfd_malloc (amt);
7789 if (finfo.external_syms == NULL)
7792 amt = max_sym_count * sizeof (Elf_Internal_Sym);
7793 finfo.internal_syms = bfd_malloc (amt);
7794 if (finfo.internal_syms == NULL)
7797 amt = max_sym_count * sizeof (long);
7798 finfo.indices = bfd_malloc (amt);
7799 if (finfo.indices == NULL)
7802 amt = max_sym_count * sizeof (asection *);
7803 finfo.sections = bfd_malloc (amt);
7804 if (finfo.sections == NULL)
7808 if (max_sym_shndx_count != 0)
7810 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
7811 finfo.locsym_shndx = bfd_malloc (amt);
7812 if (finfo.locsym_shndx == NULL)
7816 if (elf_hash_table (info)->tls_sec)
7818 bfd_vma base, end = 0;
7821 for (sec = elf_hash_table (info)->tls_sec;
7822 sec && (sec->flags & SEC_THREAD_LOCAL);
7825 bfd_vma size = sec->size;
7827 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
7829 struct bfd_link_order *o;
7831 for (o = sec->link_order_head; o != NULL; o = o->next)
7832 if (size < o->offset + o->size)
7833 size = o->offset + o->size;
7835 end = sec->vma + size;
7837 base = elf_hash_table (info)->tls_sec->vma;
7838 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
7839 elf_hash_table (info)->tls_size = end - base;
7842 /* Reorder SHF_LINK_ORDER sections. */
7843 for (o = abfd->sections; o != NULL; o = o->next)
7845 if (!elf_fixup_link_order (abfd, o))
7849 /* Since ELF permits relocations to be against local symbols, we
7850 must have the local symbols available when we do the relocations.
7851 Since we would rather only read the local symbols once, and we
7852 would rather not keep them in memory, we handle all the
7853 relocations for a single input file at the same time.
7855 Unfortunately, there is no way to know the total number of local
7856 symbols until we have seen all of them, and the local symbol
7857 indices precede the global symbol indices. This means that when
7858 we are generating relocatable output, and we see a reloc against
7859 a global symbol, we can not know the symbol index until we have
7860 finished examining all the local symbols to see which ones we are
7861 going to output. To deal with this, we keep the relocations in
7862 memory, and don't output them until the end of the link. This is
7863 an unfortunate waste of memory, but I don't see a good way around
7864 it. Fortunately, it only happens when performing a relocatable
7865 link, which is not the common case. FIXME: If keep_memory is set
7866 we could write the relocs out and then read them again; I don't
7867 know how bad the memory loss will be. */
7869 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7870 sub->output_has_begun = FALSE;
7871 for (o = abfd->sections; o != NULL; o = o->next)
7873 for (p = o->link_order_head; p != NULL; p = p->next)
7875 if (p->type == bfd_indirect_link_order
7876 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7877 == bfd_target_elf_flavour)
7878 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7880 if (! sub->output_has_begun)
7882 if (! elf_link_input_bfd (&finfo, sub))
7884 sub->output_has_begun = TRUE;
7887 else if (p->type == bfd_section_reloc_link_order
7888 || p->type == bfd_symbol_reloc_link_order)
7890 if (! elf_reloc_link_order (abfd, info, o, p))
7895 if (! _bfd_default_link_order (abfd, info, o, p))
7901 /* Output any global symbols that got converted to local in a
7902 version script or due to symbol visibility. We do this in a
7903 separate step since ELF requires all local symbols to appear
7904 prior to any global symbols. FIXME: We should only do this if
7905 some global symbols were, in fact, converted to become local.
7906 FIXME: Will this work correctly with the Irix 5 linker? */
7907 eoinfo.failed = FALSE;
7908 eoinfo.finfo = &finfo;
7909 eoinfo.localsyms = TRUE;
7910 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
7915 /* That wrote out all the local symbols. Finish up the symbol table
7916 with the global symbols. Even if we want to strip everything we
7917 can, we still need to deal with those global symbols that got
7918 converted to local in a version script. */
7920 /* The sh_info field records the index of the first non local symbol. */
7921 symtab_hdr->sh_info = bfd_get_symcount (abfd);
7924 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
7926 Elf_Internal_Sym sym;
7927 bfd_byte *dynsym = finfo.dynsym_sec->contents;
7928 long last_local = 0;
7930 /* Write out the section symbols for the output sections. */
7937 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7940 for (s = abfd->sections; s != NULL; s = s->next)
7946 dynindx = elf_section_data (s)->dynindx;
7949 indx = elf_section_data (s)->this_idx;
7950 BFD_ASSERT (indx > 0);
7951 sym.st_shndx = indx;
7952 sym.st_value = s->vma;
7953 dest = dynsym + dynindx * bed->s->sizeof_sym;
7954 if (last_local < dynindx)
7955 last_local = dynindx;
7956 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
7960 /* Write out the local dynsyms. */
7961 if (elf_hash_table (info)->dynlocal)
7963 struct elf_link_local_dynamic_entry *e;
7964 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
7969 sym.st_size = e->isym.st_size;
7970 sym.st_other = e->isym.st_other;
7972 /* Copy the internal symbol as is.
7973 Note that we saved a word of storage and overwrote
7974 the original st_name with the dynstr_index. */
7977 if (e->isym.st_shndx != SHN_UNDEF
7978 && (e->isym.st_shndx < SHN_LORESERVE
7979 || e->isym.st_shndx > SHN_HIRESERVE))
7981 s = bfd_section_from_elf_index (e->input_bfd,
7985 elf_section_data (s->output_section)->this_idx;
7986 sym.st_value = (s->output_section->vma
7988 + e->isym.st_value);
7991 if (last_local < e->dynindx)
7992 last_local = e->dynindx;
7994 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
7995 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
7999 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8003 /* We get the global symbols from the hash table. */
8004 eoinfo.failed = FALSE;
8005 eoinfo.localsyms = FALSE;
8006 eoinfo.finfo = &finfo;
8007 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8012 /* If backend needs to output some symbols not present in the hash
8013 table, do it now. */
8014 if (bed->elf_backend_output_arch_syms)
8016 typedef bfd_boolean (*out_sym_func)
8017 (void *, const char *, Elf_Internal_Sym *, asection *,
8018 struct elf_link_hash_entry *);
8020 if (! ((*bed->elf_backend_output_arch_syms)
8021 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8025 /* Flush all symbols to the file. */
8026 if (! elf_link_flush_output_syms (&finfo, bed))
8029 /* Now we know the size of the symtab section. */
8030 off += symtab_hdr->sh_size;
8032 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8033 if (symtab_shndx_hdr->sh_name != 0)
8035 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8036 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8037 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8038 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8039 symtab_shndx_hdr->sh_size = amt;
8041 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8044 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8045 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8050 /* Finish up and write out the symbol string table (.strtab)
8052 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8053 /* sh_name was set in prep_headers. */
8054 symstrtab_hdr->sh_type = SHT_STRTAB;
8055 symstrtab_hdr->sh_flags = 0;
8056 symstrtab_hdr->sh_addr = 0;
8057 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8058 symstrtab_hdr->sh_entsize = 0;
8059 symstrtab_hdr->sh_link = 0;
8060 symstrtab_hdr->sh_info = 0;
8061 /* sh_offset is set just below. */
8062 symstrtab_hdr->sh_addralign = 1;
8064 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8065 elf_tdata (abfd)->next_file_pos = off;
8067 if (bfd_get_symcount (abfd) > 0)
8069 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8070 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8074 /* Adjust the relocs to have the correct symbol indices. */
8075 for (o = abfd->sections; o != NULL; o = o->next)
8077 if ((o->flags & SEC_RELOC) == 0)
8080 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8081 elf_section_data (o)->rel_count,
8082 elf_section_data (o)->rel_hashes);
8083 if (elf_section_data (o)->rel_hdr2 != NULL)
8084 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8085 elf_section_data (o)->rel_count2,
8086 (elf_section_data (o)->rel_hashes
8087 + elf_section_data (o)->rel_count));
8089 /* Set the reloc_count field to 0 to prevent write_relocs from
8090 trying to swap the relocs out itself. */
8094 if (dynamic && info->combreloc && dynobj != NULL)
8095 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8097 /* If we are linking against a dynamic object, or generating a
8098 shared library, finish up the dynamic linking information. */
8101 bfd_byte *dyncon, *dynconend;
8103 /* Fix up .dynamic entries. */
8104 o = bfd_get_section_by_name (dynobj, ".dynamic");
8105 BFD_ASSERT (o != NULL);
8107 dyncon = o->contents;
8108 dynconend = o->contents + o->size;
8109 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8111 Elf_Internal_Dyn dyn;
8115 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8122 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8124 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8126 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8127 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8130 dyn.d_un.d_val = relativecount;
8137 name = info->init_function;
8140 name = info->fini_function;
8143 struct elf_link_hash_entry *h;
8145 h = elf_link_hash_lookup (elf_hash_table (info), name,
8146 FALSE, FALSE, TRUE);
8148 && (h->root.type == bfd_link_hash_defined
8149 || h->root.type == bfd_link_hash_defweak))
8151 dyn.d_un.d_val = h->root.u.def.value;
8152 o = h->root.u.def.section;
8153 if (o->output_section != NULL)
8154 dyn.d_un.d_val += (o->output_section->vma
8155 + o->output_offset);
8158 /* The symbol is imported from another shared
8159 library and does not apply to this one. */
8167 case DT_PREINIT_ARRAYSZ:
8168 name = ".preinit_array";
8170 case DT_INIT_ARRAYSZ:
8171 name = ".init_array";
8173 case DT_FINI_ARRAYSZ:
8174 name = ".fini_array";
8176 o = bfd_get_section_by_name (abfd, name);
8179 (*_bfd_error_handler)
8180 (_("%B: could not find output section %s"), abfd, name);
8184 (*_bfd_error_handler)
8185 (_("warning: %s section has zero size"), name);
8186 dyn.d_un.d_val = o->size;
8189 case DT_PREINIT_ARRAY:
8190 name = ".preinit_array";
8193 name = ".init_array";
8196 name = ".fini_array";
8209 name = ".gnu.version_d";
8212 name = ".gnu.version_r";
8215 name = ".gnu.version";
8217 o = bfd_get_section_by_name (abfd, name);
8220 (*_bfd_error_handler)
8221 (_("%B: could not find output section %s"), abfd, name);
8224 dyn.d_un.d_ptr = o->vma;
8231 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8236 for (i = 1; i < elf_numsections (abfd); i++)
8238 Elf_Internal_Shdr *hdr;
8240 hdr = elf_elfsections (abfd)[i];
8241 if (hdr->sh_type == type
8242 && (hdr->sh_flags & SHF_ALLOC) != 0)
8244 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8245 dyn.d_un.d_val += hdr->sh_size;
8248 if (dyn.d_un.d_val == 0
8249 || hdr->sh_addr < dyn.d_un.d_val)
8250 dyn.d_un.d_val = hdr->sh_addr;
8256 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8260 /* If we have created any dynamic sections, then output them. */
8263 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8266 for (o = dynobj->sections; o != NULL; o = o->next)
8268 if ((o->flags & SEC_HAS_CONTENTS) == 0
8270 || o->output_section == bfd_abs_section_ptr)
8272 if ((o->flags & SEC_LINKER_CREATED) == 0)
8274 /* At this point, we are only interested in sections
8275 created by _bfd_elf_link_create_dynamic_sections. */
8278 if (elf_hash_table (info)->stab_info.stabstr == o)
8280 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8282 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8284 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8286 if (! bfd_set_section_contents (abfd, o->output_section,
8288 (file_ptr) o->output_offset,
8294 /* The contents of the .dynstr section are actually in a
8296 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8297 if (bfd_seek (abfd, off, SEEK_SET) != 0
8298 || ! _bfd_elf_strtab_emit (abfd,
8299 elf_hash_table (info)->dynstr))
8305 if (info->relocatable)
8307 bfd_boolean failed = FALSE;
8309 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8314 /* If we have optimized stabs strings, output them. */
8315 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8317 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8321 if (info->eh_frame_hdr)
8323 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8327 if (finfo.symstrtab != NULL)
8328 _bfd_stringtab_free (finfo.symstrtab);
8329 if (finfo.contents != NULL)
8330 free (finfo.contents);
8331 if (finfo.external_relocs != NULL)
8332 free (finfo.external_relocs);
8333 if (finfo.internal_relocs != NULL)
8334 free (finfo.internal_relocs);
8335 if (finfo.external_syms != NULL)
8336 free (finfo.external_syms);
8337 if (finfo.locsym_shndx != NULL)
8338 free (finfo.locsym_shndx);
8339 if (finfo.internal_syms != NULL)
8340 free (finfo.internal_syms);
8341 if (finfo.indices != NULL)
8342 free (finfo.indices);
8343 if (finfo.sections != NULL)
8344 free (finfo.sections);
8345 if (finfo.symbuf != NULL)
8346 free (finfo.symbuf);
8347 if (finfo.symshndxbuf != NULL)
8348 free (finfo.symshndxbuf);
8349 for (o = abfd->sections; o != NULL; o = o->next)
8351 if ((o->flags & SEC_RELOC) != 0
8352 && elf_section_data (o)->rel_hashes != NULL)
8353 free (elf_section_data (o)->rel_hashes);
8356 elf_tdata (abfd)->linker = TRUE;
8361 if (finfo.symstrtab != NULL)
8362 _bfd_stringtab_free (finfo.symstrtab);
8363 if (finfo.contents != NULL)
8364 free (finfo.contents);
8365 if (finfo.external_relocs != NULL)
8366 free (finfo.external_relocs);
8367 if (finfo.internal_relocs != NULL)
8368 free (finfo.internal_relocs);
8369 if (finfo.external_syms != NULL)
8370 free (finfo.external_syms);
8371 if (finfo.locsym_shndx != NULL)
8372 free (finfo.locsym_shndx);
8373 if (finfo.internal_syms != NULL)
8374 free (finfo.internal_syms);
8375 if (finfo.indices != NULL)
8376 free (finfo.indices);
8377 if (finfo.sections != NULL)
8378 free (finfo.sections);
8379 if (finfo.symbuf != NULL)
8380 free (finfo.symbuf);
8381 if (finfo.symshndxbuf != NULL)
8382 free (finfo.symshndxbuf);
8383 for (o = abfd->sections; o != NULL; o = o->next)
8385 if ((o->flags & SEC_RELOC) != 0
8386 && elf_section_data (o)->rel_hashes != NULL)
8387 free (elf_section_data (o)->rel_hashes);
8393 /* Garbage collect unused sections. */
8395 /* The mark phase of garbage collection. For a given section, mark
8396 it and any sections in this section's group, and all the sections
8397 which define symbols to which it refers. */
8399 typedef asection * (*gc_mark_hook_fn)
8400 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8401 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8404 _bfd_elf_gc_mark (struct bfd_link_info *info,
8406 gc_mark_hook_fn gc_mark_hook)
8409 asection *group_sec;
8413 /* Mark all the sections in the group. */
8414 group_sec = elf_section_data (sec)->next_in_group;
8415 if (group_sec && !group_sec->gc_mark)
8416 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8419 /* Look through the section relocs. */
8421 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8423 Elf_Internal_Rela *relstart, *rel, *relend;
8424 Elf_Internal_Shdr *symtab_hdr;
8425 struct elf_link_hash_entry **sym_hashes;
8428 bfd *input_bfd = sec->owner;
8429 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8430 Elf_Internal_Sym *isym = NULL;
8433 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8434 sym_hashes = elf_sym_hashes (input_bfd);
8436 /* Read the local symbols. */
8437 if (elf_bad_symtab (input_bfd))
8439 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8443 extsymoff = nlocsyms = symtab_hdr->sh_info;
8445 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8446 if (isym == NULL && nlocsyms != 0)
8448 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8454 /* Read the relocations. */
8455 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8457 if (relstart == NULL)
8462 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8464 if (bed->s->arch_size == 32)
8469 for (rel = relstart; rel < relend; rel++)
8471 unsigned long r_symndx;
8473 struct elf_link_hash_entry *h;
8475 r_symndx = rel->r_info >> r_sym_shift;
8479 if (r_symndx >= nlocsyms
8480 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8482 h = sym_hashes[r_symndx - extsymoff];
8483 while (h->root.type == bfd_link_hash_indirect
8484 || h->root.type == bfd_link_hash_warning)
8485 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8486 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8490 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8493 if (rsec && !rsec->gc_mark)
8495 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8497 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8506 if (elf_section_data (sec)->relocs != relstart)
8509 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8511 if (! info->keep_memory)
8514 symtab_hdr->contents = (unsigned char *) isym;
8521 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8524 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8528 if (h->root.type == bfd_link_hash_warning)
8529 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8531 if (h->dynindx != -1
8532 && ((h->root.type != bfd_link_hash_defined
8533 && h->root.type != bfd_link_hash_defweak)
8534 || h->root.u.def.section->gc_mark))
8535 h->dynindx = (*idx)++;
8540 /* The sweep phase of garbage collection. Remove all garbage sections. */
8542 typedef bfd_boolean (*gc_sweep_hook_fn)
8543 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8546 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8550 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8554 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8557 for (o = sub->sections; o != NULL; o = o->next)
8559 /* Keep debug and special sections. */
8560 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8561 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8567 /* Skip sweeping sections already excluded. */
8568 if (o->flags & SEC_EXCLUDE)
8571 /* Since this is early in the link process, it is simple
8572 to remove a section from the output. */
8573 o->flags |= SEC_EXCLUDE;
8575 /* But we also have to update some of the relocation
8576 info we collected before. */
8578 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
8580 Elf_Internal_Rela *internal_relocs;
8584 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8586 if (internal_relocs == NULL)
8589 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8591 if (elf_section_data (o)->relocs != internal_relocs)
8592 free (internal_relocs);
8600 /* Remove the symbols that were in the swept sections from the dynamic
8601 symbol table. GCFIXME: Anyone know how to get them out of the
8602 static symbol table as well? */
8606 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8608 elf_hash_table (info)->dynsymcount = i;
8614 /* Propagate collected vtable information. This is called through
8615 elf_link_hash_traverse. */
8618 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8620 if (h->root.type == bfd_link_hash_warning)
8621 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8623 /* Those that are not vtables. */
8624 if (h->vtable == NULL || h->vtable->parent == NULL)
8627 /* Those vtables that do not have parents, we cannot merge. */
8628 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
8631 /* If we've already been done, exit. */
8632 if (h->vtable->used && h->vtable->used[-1])
8635 /* Make sure the parent's table is up to date. */
8636 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
8638 if (h->vtable->used == NULL)
8640 /* None of this table's entries were referenced. Re-use the
8642 h->vtable->used = h->vtable->parent->vtable->used;
8643 h->vtable->size = h->vtable->parent->vtable->size;
8648 bfd_boolean *cu, *pu;
8650 /* Or the parent's entries into ours. */
8651 cu = h->vtable->used;
8653 pu = h->vtable->parent->vtable->used;
8656 const struct elf_backend_data *bed;
8657 unsigned int log_file_align;
8659 bed = get_elf_backend_data (h->root.u.def.section->owner);
8660 log_file_align = bed->s->log_file_align;
8661 n = h->vtable->parent->vtable->size >> log_file_align;
8676 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
8679 bfd_vma hstart, hend;
8680 Elf_Internal_Rela *relstart, *relend, *rel;
8681 const struct elf_backend_data *bed;
8682 unsigned int log_file_align;
8684 if (h->root.type == bfd_link_hash_warning)
8685 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8687 /* Take care of both those symbols that do not describe vtables as
8688 well as those that are not loaded. */
8689 if (h->vtable == NULL || h->vtable->parent == NULL)
8692 BFD_ASSERT (h->root.type == bfd_link_hash_defined
8693 || h->root.type == bfd_link_hash_defweak);
8695 sec = h->root.u.def.section;
8696 hstart = h->root.u.def.value;
8697 hend = hstart + h->size;
8699 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
8701 return *(bfd_boolean *) okp = FALSE;
8702 bed = get_elf_backend_data (sec->owner);
8703 log_file_align = bed->s->log_file_align;
8705 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8707 for (rel = relstart; rel < relend; ++rel)
8708 if (rel->r_offset >= hstart && rel->r_offset < hend)
8710 /* If the entry is in use, do nothing. */
8712 && (rel->r_offset - hstart) < h->vtable->size)
8714 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
8715 if (h->vtable->used[entry])
8718 /* Otherwise, kill it. */
8719 rel->r_offset = rel->r_info = rel->r_addend = 0;
8725 /* Mark sections containing dynamically referenced symbols. This is called
8726 through elf_link_hash_traverse. */
8729 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h,
8730 void *okp ATTRIBUTE_UNUSED)
8732 if (h->root.type == bfd_link_hash_warning)
8733 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8735 if ((h->root.type == bfd_link_hash_defined
8736 || h->root.type == bfd_link_hash_defweak)
8738 h->root.u.def.section->flags |= SEC_KEEP;
8743 /* Do mark and sweep of unused sections. */
8746 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
8748 bfd_boolean ok = TRUE;
8750 asection * (*gc_mark_hook)
8751 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8752 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
8754 if (!get_elf_backend_data (abfd)->can_gc_sections
8755 || info->relocatable
8756 || info->emitrelocations
8758 || !is_elf_hash_table (info->hash))
8760 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
8764 /* Apply transitive closure to the vtable entry usage info. */
8765 elf_link_hash_traverse (elf_hash_table (info),
8766 elf_gc_propagate_vtable_entries_used,
8771 /* Kill the vtable relocations that were not used. */
8772 elf_link_hash_traverse (elf_hash_table (info),
8773 elf_gc_smash_unused_vtentry_relocs,
8778 /* Mark dynamically referenced symbols. */
8779 if (elf_hash_table (info)->dynamic_sections_created)
8780 elf_link_hash_traverse (elf_hash_table (info),
8781 elf_gc_mark_dynamic_ref_symbol,
8786 /* Grovel through relocs to find out who stays ... */
8787 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
8788 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8792 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8795 for (o = sub->sections; o != NULL; o = o->next)
8797 if (o->flags & SEC_KEEP)
8799 /* _bfd_elf_discard_section_eh_frame knows how to discard
8800 orphaned FDEs so don't mark sections referenced by the
8801 EH frame section. */
8802 if (strcmp (o->name, ".eh_frame") == 0)
8804 else if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
8810 /* ... and mark SEC_EXCLUDE for those that go. */
8811 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
8817 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8820 bfd_elf_gc_record_vtinherit (bfd *abfd,
8822 struct elf_link_hash_entry *h,
8825 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
8826 struct elf_link_hash_entry **search, *child;
8827 bfd_size_type extsymcount;
8828 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8830 /* The sh_info field of the symtab header tells us where the
8831 external symbols start. We don't care about the local symbols at
8833 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
8834 if (!elf_bad_symtab (abfd))
8835 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8837 sym_hashes = elf_sym_hashes (abfd);
8838 sym_hashes_end = sym_hashes + extsymcount;
8840 /* Hunt down the child symbol, which is in this section at the same
8841 offset as the relocation. */
8842 for (search = sym_hashes; search != sym_hashes_end; ++search)
8844 if ((child = *search) != NULL
8845 && (child->root.type == bfd_link_hash_defined
8846 || child->root.type == bfd_link_hash_defweak)
8847 && child->root.u.def.section == sec
8848 && child->root.u.def.value == offset)
8852 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
8853 abfd, sec, (unsigned long) offset);
8854 bfd_set_error (bfd_error_invalid_operation);
8860 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
8866 /* This *should* only be the absolute section. It could potentially
8867 be that someone has defined a non-global vtable though, which
8868 would be bad. It isn't worth paging in the local symbols to be
8869 sure though; that case should simply be handled by the assembler. */
8871 child->vtable->parent = (struct elf_link_hash_entry *) -1;
8874 child->vtable->parent = h;
8879 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8882 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
8883 asection *sec ATTRIBUTE_UNUSED,
8884 struct elf_link_hash_entry *h,
8887 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8888 unsigned int log_file_align = bed->s->log_file_align;
8892 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
8897 if (addend >= h->vtable->size)
8899 size_t size, bytes, file_align;
8900 bfd_boolean *ptr = h->vtable->used;
8902 /* While the symbol is undefined, we have to be prepared to handle
8904 file_align = 1 << log_file_align;
8905 if (h->root.type == bfd_link_hash_undefined)
8906 size = addend + file_align;
8912 /* Oops! We've got a reference past the defined end of
8913 the table. This is probably a bug -- shall we warn? */
8914 size = addend + file_align;
8917 size = (size + file_align - 1) & -file_align;
8919 /* Allocate one extra entry for use as a "done" flag for the
8920 consolidation pass. */
8921 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
8925 ptr = bfd_realloc (ptr - 1, bytes);
8931 oldbytes = (((h->vtable->size >> log_file_align) + 1)
8932 * sizeof (bfd_boolean));
8933 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
8937 ptr = bfd_zmalloc (bytes);
8942 /* And arrange for that done flag to be at index -1. */
8943 h->vtable->used = ptr + 1;
8944 h->vtable->size = size;
8947 h->vtable->used[addend >> log_file_align] = TRUE;
8952 struct alloc_got_off_arg {
8954 unsigned int got_elt_size;
8957 /* We need a special top-level link routine to convert got reference counts
8958 to real got offsets. */
8961 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
8963 struct alloc_got_off_arg *gofarg = arg;
8965 if (h->root.type == bfd_link_hash_warning)
8966 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8968 if (h->got.refcount > 0)
8970 h->got.offset = gofarg->gotoff;
8971 gofarg->gotoff += gofarg->got_elt_size;
8974 h->got.offset = (bfd_vma) -1;
8979 /* And an accompanying bit to work out final got entry offsets once
8980 we're done. Should be called from final_link. */
8983 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
8984 struct bfd_link_info *info)
8987 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8989 unsigned int got_elt_size = bed->s->arch_size / 8;
8990 struct alloc_got_off_arg gofarg;
8992 if (! is_elf_hash_table (info->hash))
8995 /* The GOT offset is relative to the .got section, but the GOT header is
8996 put into the .got.plt section, if the backend uses it. */
8997 if (bed->want_got_plt)
9000 gotoff = bed->got_header_size;
9002 /* Do the local .got entries first. */
9003 for (i = info->input_bfds; i; i = i->link_next)
9005 bfd_signed_vma *local_got;
9006 bfd_size_type j, locsymcount;
9007 Elf_Internal_Shdr *symtab_hdr;
9009 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9012 local_got = elf_local_got_refcounts (i);
9016 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9017 if (elf_bad_symtab (i))
9018 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9020 locsymcount = symtab_hdr->sh_info;
9022 for (j = 0; j < locsymcount; ++j)
9024 if (local_got[j] > 0)
9026 local_got[j] = gotoff;
9027 gotoff += got_elt_size;
9030 local_got[j] = (bfd_vma) -1;
9034 /* Then the global .got entries. .plt refcounts are handled by
9035 adjust_dynamic_symbol */
9036 gofarg.gotoff = gotoff;
9037 gofarg.got_elt_size = got_elt_size;
9038 elf_link_hash_traverse (elf_hash_table (info),
9039 elf_gc_allocate_got_offsets,
9044 /* Many folk need no more in the way of final link than this, once
9045 got entry reference counting is enabled. */
9048 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9050 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9053 /* Invoke the regular ELF backend linker to do all the work. */
9054 return bfd_elf_final_link (abfd, info);
9058 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9060 struct elf_reloc_cookie *rcookie = cookie;
9062 if (rcookie->bad_symtab)
9063 rcookie->rel = rcookie->rels;
9065 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9067 unsigned long r_symndx;
9069 if (! rcookie->bad_symtab)
9070 if (rcookie->rel->r_offset > offset)
9072 if (rcookie->rel->r_offset != offset)
9075 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9076 if (r_symndx == SHN_UNDEF)
9079 if (r_symndx >= rcookie->locsymcount
9080 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9082 struct elf_link_hash_entry *h;
9084 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9086 while (h->root.type == bfd_link_hash_indirect
9087 || h->root.type == bfd_link_hash_warning)
9088 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9090 if ((h->root.type == bfd_link_hash_defined
9091 || h->root.type == bfd_link_hash_defweak)
9092 && elf_discarded_section (h->root.u.def.section))
9099 /* It's not a relocation against a global symbol,
9100 but it could be a relocation against a local
9101 symbol for a discarded section. */
9103 Elf_Internal_Sym *isym;
9105 /* Need to: get the symbol; get the section. */
9106 isym = &rcookie->locsyms[r_symndx];
9107 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9109 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9110 if (isec != NULL && elf_discarded_section (isec))
9119 /* Discard unneeded references to discarded sections.
9120 Returns TRUE if any section's size was changed. */
9121 /* This function assumes that the relocations are in sorted order,
9122 which is true for all known assemblers. */
9125 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9127 struct elf_reloc_cookie cookie;
9128 asection *stab, *eh;
9129 Elf_Internal_Shdr *symtab_hdr;
9130 const struct elf_backend_data *bed;
9133 bfd_boolean ret = FALSE;
9135 if (info->traditional_format
9136 || !is_elf_hash_table (info->hash))
9139 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9141 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9144 bed = get_elf_backend_data (abfd);
9146 if ((abfd->flags & DYNAMIC) != 0)
9149 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9150 if (info->relocatable
9153 || bfd_is_abs_section (eh->output_section))))
9156 stab = bfd_get_section_by_name (abfd, ".stab");
9159 || bfd_is_abs_section (stab->output_section)
9160 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9165 && bed->elf_backend_discard_info == NULL)
9168 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9170 cookie.sym_hashes = elf_sym_hashes (abfd);
9171 cookie.bad_symtab = elf_bad_symtab (abfd);
9172 if (cookie.bad_symtab)
9174 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9175 cookie.extsymoff = 0;
9179 cookie.locsymcount = symtab_hdr->sh_info;
9180 cookie.extsymoff = symtab_hdr->sh_info;
9183 if (bed->s->arch_size == 32)
9184 cookie.r_sym_shift = 8;
9186 cookie.r_sym_shift = 32;
9188 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9189 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9191 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9192 cookie.locsymcount, 0,
9194 if (cookie.locsyms == NULL)
9201 count = stab->reloc_count;
9203 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9205 if (cookie.rels != NULL)
9207 cookie.rel = cookie.rels;
9208 cookie.relend = cookie.rels;
9209 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9210 if (_bfd_discard_section_stabs (abfd, stab,
9211 elf_section_data (stab)->sec_info,
9212 bfd_elf_reloc_symbol_deleted_p,
9215 if (elf_section_data (stab)->relocs != cookie.rels)
9223 count = eh->reloc_count;
9225 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9227 cookie.rel = cookie.rels;
9228 cookie.relend = cookie.rels;
9229 if (cookie.rels != NULL)
9230 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9232 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9233 bfd_elf_reloc_symbol_deleted_p,
9237 if (cookie.rels != NULL
9238 && elf_section_data (eh)->relocs != cookie.rels)
9242 if (bed->elf_backend_discard_info != NULL
9243 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9246 if (cookie.locsyms != NULL
9247 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9249 if (! info->keep_memory)
9250 free (cookie.locsyms);
9252 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9256 if (info->eh_frame_hdr
9257 && !info->relocatable
9258 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9264 struct already_linked_section
9270 /* Check if the member of a single member comdat group matches a
9271 linkonce section and vice versa. */
9273 try_match_symbols_in_sections
9274 (struct bfd_section_already_linked_hash_entry *h, void *info)
9276 struct bfd_section_already_linked *l;
9277 struct already_linked_section *s
9278 = (struct already_linked_section *) info;
9280 if (elf_sec_group (s->sec) == NULL)
9282 /* It is a linkonce section. Try to match it with the member of a
9283 single member comdat group. */
9284 for (l = h->entry; l != NULL; l = l->next)
9285 if ((l->sec->flags & SEC_GROUP))
9287 asection *first = elf_next_in_group (l->sec);
9290 && elf_next_in_group (first) == first
9291 && bfd_elf_match_symbols_in_sections (first, s->sec))
9300 /* It is the member of a single member comdat group. Try to match
9301 it with a linkonce section. */
9302 for (l = h->entry; l != NULL; l = l->next)
9303 if ((l->sec->flags & SEC_GROUP) == 0
9304 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9305 && bfd_elf_match_symbols_in_sections (l->sec, s->sec))
9316 already_linked (asection *sec, asection *group)
9318 struct already_linked_section result;
9321 result.linked = NULL;
9323 bfd_section_already_linked_table_traverse
9324 (try_match_symbols_in_sections, &result);
9328 sec->output_section = bfd_abs_section_ptr;
9329 sec->kept_section = result.linked;
9331 /* Also discard the group section. */
9333 group->output_section = bfd_abs_section_ptr;
9342 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9346 struct bfd_section_already_linked *l;
9347 struct bfd_section_already_linked_hash_entry *already_linked_list;
9350 /* A single member comdat group section may be discarded by a
9351 linkonce section. See below. */
9352 if (sec->output_section == bfd_abs_section_ptr)
9357 /* Check if it belongs to a section group. */
9358 group = elf_sec_group (sec);
9360 /* Return if it isn't a linkonce section nor a member of a group. A
9361 comdat group section also has SEC_LINK_ONCE set. */
9362 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9367 /* If this is the member of a single member comdat group, check if
9368 the group should be discarded. */
9369 if (elf_next_in_group (sec) == sec
9370 && (group->flags & SEC_LINK_ONCE) != 0)
9376 /* FIXME: When doing a relocatable link, we may have trouble
9377 copying relocations in other sections that refer to local symbols
9378 in the section being discarded. Those relocations will have to
9379 be converted somehow; as of this writing I'm not sure that any of
9380 the backends handle that correctly.
9382 It is tempting to instead not discard link once sections when
9383 doing a relocatable link (technically, they should be discarded
9384 whenever we are building constructors). However, that fails,
9385 because the linker winds up combining all the link once sections
9386 into a single large link once section, which defeats the purpose
9387 of having link once sections in the first place.
9389 Also, not merging link once sections in a relocatable link
9390 causes trouble for MIPS ELF, which relies on link once semantics
9391 to handle the .reginfo section correctly. */
9393 name = bfd_get_section_name (abfd, sec);
9395 already_linked_list = bfd_section_already_linked_table_lookup (name);
9397 for (l = already_linked_list->entry; l != NULL; l = l->next)
9399 /* We may have 3 different sections on the list: group section,
9400 comdat section and linkonce section. SEC may be a linkonce or
9401 group section. We match a group section with a group section,
9402 a linkonce section with a linkonce section, and ignore comdat
9404 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9405 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9407 /* The section has already been linked. See if we should
9409 switch (flags & SEC_LINK_DUPLICATES)
9414 case SEC_LINK_DUPLICATES_DISCARD:
9417 case SEC_LINK_DUPLICATES_ONE_ONLY:
9418 (*_bfd_error_handler)
9419 (_("%B: ignoring duplicate section `%A'\n"),
9423 case SEC_LINK_DUPLICATES_SAME_SIZE:
9424 if (sec->size != l->sec->size)
9425 (*_bfd_error_handler)
9426 (_("%B: duplicate section `%A' has different size\n"),
9430 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9431 if (sec->size != l->sec->size)
9432 (*_bfd_error_handler)
9433 (_("%B: duplicate section `%A' has different size\n"),
9435 else if (sec->size != 0)
9437 bfd_byte *sec_contents, *l_sec_contents;
9439 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9440 (*_bfd_error_handler)
9441 (_("%B: warning: could not read contents of section `%A'\n"),
9443 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9445 (*_bfd_error_handler)
9446 (_("%B: warning: could not read contents of section `%A'\n"),
9447 l->sec->owner, l->sec);
9448 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9449 (*_bfd_error_handler)
9450 (_("%B: warning: duplicate section `%A' has different contents\n"),
9454 free (sec_contents);
9456 free (l_sec_contents);
9461 /* Set the output_section field so that lang_add_section
9462 does not create a lang_input_section structure for this
9463 section. Since there might be a symbol in the section
9464 being discarded, we must retain a pointer to the section
9465 which we are really going to use. */
9466 sec->output_section = bfd_abs_section_ptr;
9467 sec->kept_section = l->sec;
9469 if (flags & SEC_GROUP)
9471 asection *first = elf_next_in_group (sec);
9472 asection *s = first;
9476 s->output_section = bfd_abs_section_ptr;
9477 /* Record which group discards it. */
9478 s->kept_section = l->sec;
9479 s = elf_next_in_group (s);
9480 /* These lists are circular. */
9492 /* If this is the member of a single member comdat group and the
9493 group hasn't be discarded, we check if it matches a linkonce
9494 section. We only record the discarded comdat group. Otherwise
9495 the undiscarded group will be discarded incorrectly later since
9496 itself has been recorded. */
9497 if (! already_linked (elf_next_in_group (sec), group))
9501 /* There is no direct match. But for linkonce section, we should
9502 check if there is a match with comdat group member. We always
9503 record the linkonce section, discarded or not. */
9504 already_linked (sec, group);
9506 /* This is the first section with this name. Record it. */
9507 bfd_section_already_linked_table_insert (already_linked_list, sec);