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 = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
61 | SEC_LINKER_CREATED);
63 s = bfd_make_section (abfd, ".got");
65 || !bfd_set_section_flags (abfd, s, flags)
66 || !bfd_set_section_alignment (abfd, s, ptralign))
69 if (bed->want_got_plt)
71 s = bfd_make_section (abfd, ".got.plt");
73 || !bfd_set_section_flags (abfd, s, flags)
74 || !bfd_set_section_alignment (abfd, s, ptralign))
78 if (bed->want_got_sym)
80 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
81 (or .got.plt) section. We don't do this in the linker script
82 because we don't want to define the symbol if we are not creating
83 a global offset table. */
85 if (!(_bfd_generic_link_add_one_symbol
86 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
87 bed->got_symbol_offset, NULL, FALSE, bed->collect, &bh)))
89 h = (struct elf_link_hash_entry *) bh;
90 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
93 if (! info->executable
94 && ! bfd_elf_link_record_dynamic_symbol (info, h))
97 elf_hash_table (info)->hgot = h;
100 /* The first bit of the global offset table is the header. */
101 s->size += bed->got_header_size + bed->got_symbol_offset;
106 /* Create some sections which will be filled in with dynamic linking
107 information. ABFD is an input file which requires dynamic sections
108 to be created. The dynamic sections take up virtual memory space
109 when the final executable is run, so we need to create them before
110 addresses are assigned to the output sections. We work out the
111 actual contents and size of these sections later. */
114 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
117 register asection *s;
118 struct elf_link_hash_entry *h;
119 struct bfd_link_hash_entry *bh;
120 const struct elf_backend_data *bed;
122 if (! is_elf_hash_table (info->hash))
125 if (elf_hash_table (info)->dynamic_sections_created)
128 /* Make sure that all dynamic sections use the same input BFD. */
129 if (elf_hash_table (info)->dynobj == NULL)
130 elf_hash_table (info)->dynobj = abfd;
132 abfd = elf_hash_table (info)->dynobj;
134 /* Note that we set the SEC_IN_MEMORY flag for all of these
136 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
137 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
139 /* A dynamically linked executable has a .interp section, but a
140 shared library does not. */
141 if (info->executable)
143 s = bfd_make_section (abfd, ".interp");
145 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
149 if (! info->traditional_format)
151 s = bfd_make_section (abfd, ".eh_frame_hdr");
153 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
154 || ! bfd_set_section_alignment (abfd, s, 2))
156 elf_hash_table (info)->eh_info.hdr_sec = s;
159 bed = get_elf_backend_data (abfd);
161 /* Create sections to hold version informations. These are removed
162 if they are not needed. */
163 s = bfd_make_section (abfd, ".gnu.version_d");
165 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
169 s = bfd_make_section (abfd, ".gnu.version");
171 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
172 || ! bfd_set_section_alignment (abfd, s, 1))
175 s = bfd_make_section (abfd, ".gnu.version_r");
177 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
178 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
181 s = bfd_make_section (abfd, ".dynsym");
183 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
184 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
187 s = bfd_make_section (abfd, ".dynstr");
189 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
192 /* Create a strtab to hold the dynamic symbol names. */
193 if (elf_hash_table (info)->dynstr == NULL)
195 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
196 if (elf_hash_table (info)->dynstr == NULL)
200 s = bfd_make_section (abfd, ".dynamic");
202 || ! bfd_set_section_flags (abfd, s, flags)
203 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
206 /* The special symbol _DYNAMIC is always set to the start of the
207 .dynamic section. This call occurs before we have processed the
208 symbols for any dynamic object, so we don't have to worry about
209 overriding a dynamic definition. We could set _DYNAMIC in a
210 linker script, but we only want to define it if we are, in fact,
211 creating a .dynamic section. We don't want to define it if there
212 is no .dynamic section, since on some ELF platforms the start up
213 code examines it to decide how to initialize the process. */
215 if (! (_bfd_generic_link_add_one_symbol
216 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE,
217 get_elf_backend_data (abfd)->collect, &bh)))
219 h = (struct elf_link_hash_entry *) bh;
220 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
221 h->type = STT_OBJECT;
223 if (! info->executable
224 && ! bfd_elf_link_record_dynamic_symbol (info, h))
227 s = bfd_make_section (abfd, ".hash");
229 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
230 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
232 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
234 /* Let the backend create the rest of the sections. This lets the
235 backend set the right flags. The backend will normally create
236 the .got and .plt sections. */
237 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
240 elf_hash_table (info)->dynamic_sections_created = TRUE;
245 /* Create dynamic sections when linking against a dynamic object. */
248 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
250 flagword flags, pltflags;
252 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
254 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
255 .rel[a].bss sections. */
257 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
258 | SEC_LINKER_CREATED);
261 pltflags |= SEC_CODE;
262 if (bed->plt_not_loaded)
263 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
264 if (bed->plt_readonly)
265 pltflags |= SEC_READONLY;
267 s = bfd_make_section (abfd, ".plt");
269 || ! bfd_set_section_flags (abfd, s, pltflags)
270 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
273 if (bed->want_plt_sym)
275 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
277 struct elf_link_hash_entry *h;
278 struct bfd_link_hash_entry *bh = NULL;
280 if (! (_bfd_generic_link_add_one_symbol
281 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
282 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
284 h = (struct elf_link_hash_entry *) bh;
285 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
286 h->type = STT_OBJECT;
288 if (! info->executable
289 && ! bfd_elf_link_record_dynamic_symbol (info, h))
293 s = bfd_make_section (abfd,
294 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
296 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
297 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
300 if (! _bfd_elf_create_got_section (abfd, info))
303 if (bed->want_dynbss)
305 /* The .dynbss section is a place to put symbols which are defined
306 by dynamic objects, are referenced by regular objects, and are
307 not functions. We must allocate space for them in the process
308 image and use a R_*_COPY reloc to tell the dynamic linker to
309 initialize them at run time. The linker script puts the .dynbss
310 section into the .bss section of the final image. */
311 s = bfd_make_section (abfd, ".dynbss");
313 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
316 /* The .rel[a].bss section holds copy relocs. This section is not
317 normally needed. We need to create it here, though, so that the
318 linker will map it to an output section. We can't just create it
319 only if we need it, because we will not know whether we need it
320 until we have seen all the input files, and the first time the
321 main linker code calls BFD after examining all the input files
322 (size_dynamic_sections) the input sections have already been
323 mapped to the output sections. If the section turns out not to
324 be needed, we can discard it later. We will never need this
325 section when generating a shared object, since they do not use
329 s = bfd_make_section (abfd,
330 (bed->default_use_rela_p
331 ? ".rela.bss" : ".rel.bss"));
333 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
334 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
342 /* Record a new dynamic symbol. We record the dynamic symbols as we
343 read the input files, since we need to have a list of all of them
344 before we can determine the final sizes of the output sections.
345 Note that we may actually call this function even though we are not
346 going to output any dynamic symbols; in some cases we know that a
347 symbol should be in the dynamic symbol table, but only if there is
351 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
352 struct elf_link_hash_entry *h)
354 if (h->dynindx == -1)
356 struct elf_strtab_hash *dynstr;
361 /* XXX: The ABI draft says the linker must turn hidden and
362 internal symbols into STB_LOCAL symbols when producing the
363 DSO. However, if ld.so honors st_other in the dynamic table,
364 this would not be necessary. */
365 switch (ELF_ST_VISIBILITY (h->other))
369 if (h->root.type != bfd_link_hash_undefined
370 && h->root.type != bfd_link_hash_undefweak)
372 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
380 h->dynindx = elf_hash_table (info)->dynsymcount;
381 ++elf_hash_table (info)->dynsymcount;
383 dynstr = elf_hash_table (info)->dynstr;
386 /* Create a strtab to hold the dynamic symbol names. */
387 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
392 /* We don't put any version information in the dynamic string
394 name = h->root.root.string;
395 p = strchr (name, ELF_VER_CHR);
397 /* We know that the p points into writable memory. In fact,
398 there are only a few symbols that have read-only names, being
399 those like _GLOBAL_OFFSET_TABLE_ that are created specially
400 by the backends. Most symbols will have names pointing into
401 an ELF string table read from a file, or to objalloc memory. */
404 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
409 if (indx == (bfd_size_type) -1)
411 h->dynstr_index = indx;
417 /* Record an assignment to a symbol made by a linker script. We need
418 this in case some dynamic object refers to this symbol. */
421 bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED,
422 struct bfd_link_info *info,
426 struct elf_link_hash_entry *h;
428 if (!is_elf_hash_table (info->hash))
431 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE);
435 /* Since we're defining the symbol, don't let it seem to have not
436 been defined. record_dynamic_symbol and size_dynamic_sections
437 may depend on this. */
438 if (h->root.type == bfd_link_hash_undefweak
439 || h->root.type == bfd_link_hash_undefined)
440 h->root.type = bfd_link_hash_new;
442 if (h->root.type == bfd_link_hash_new)
443 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
445 /* If this symbol is being provided by the linker script, and it is
446 currently defined by a dynamic object, but not by a regular
447 object, then mark it as undefined so that the generic linker will
448 force the correct value. */
450 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
451 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
452 h->root.type = bfd_link_hash_undefined;
454 /* If this symbol is not being provided by the linker script, and it is
455 currently defined by a dynamic object, but not by a regular object,
456 then clear out any version information because the symbol will not be
457 associated with the dynamic object any more. */
459 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
460 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
461 h->verinfo.verdef = NULL;
463 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
465 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
466 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
470 if (! bfd_elf_link_record_dynamic_symbol (info, h))
473 /* If this is a weak defined symbol, and we know a corresponding
474 real symbol from the same dynamic object, make sure the real
475 symbol is also made into a dynamic symbol. */
476 if (h->weakdef != NULL
477 && h->weakdef->dynindx == -1)
479 if (! bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
487 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
488 success, and 2 on a failure caused by attempting to record a symbol
489 in a discarded section, eg. a discarded link-once section symbol. */
492 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
497 struct elf_link_local_dynamic_entry *entry;
498 struct elf_link_hash_table *eht;
499 struct elf_strtab_hash *dynstr;
500 unsigned long dynstr_index;
502 Elf_External_Sym_Shndx eshndx;
503 char esym[sizeof (Elf64_External_Sym)];
505 if (! is_elf_hash_table (info->hash))
508 /* See if the entry exists already. */
509 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
510 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
513 amt = sizeof (*entry);
514 entry = bfd_alloc (input_bfd, amt);
518 /* Go find the symbol, so that we can find it's name. */
519 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
520 1, input_indx, &entry->isym, esym, &eshndx))
522 bfd_release (input_bfd, entry);
526 if (entry->isym.st_shndx != SHN_UNDEF
527 && (entry->isym.st_shndx < SHN_LORESERVE
528 || entry->isym.st_shndx > SHN_HIRESERVE))
532 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
533 if (s == NULL || bfd_is_abs_section (s->output_section))
535 /* We can still bfd_release here as nothing has done another
536 bfd_alloc. We can't do this later in this function. */
537 bfd_release (input_bfd, entry);
542 name = (bfd_elf_string_from_elf_section
543 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
544 entry->isym.st_name));
546 dynstr = elf_hash_table (info)->dynstr;
549 /* Create a strtab to hold the dynamic symbol names. */
550 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
555 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
556 if (dynstr_index == (unsigned long) -1)
558 entry->isym.st_name = dynstr_index;
560 eht = elf_hash_table (info);
562 entry->next = eht->dynlocal;
563 eht->dynlocal = entry;
564 entry->input_bfd = input_bfd;
565 entry->input_indx = input_indx;
568 /* Whatever binding the symbol had before, it's now local. */
570 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
572 /* The dynindx will be set at the end of size_dynamic_sections. */
577 /* Return the dynindex of a local dynamic symbol. */
580 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
584 struct elf_link_local_dynamic_entry *e;
586 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
587 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
592 /* This function is used to renumber the dynamic symbols, if some of
593 them are removed because they are marked as local. This is called
594 via elf_link_hash_traverse. */
597 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
600 size_t *count = data;
602 if (h->root.type == bfd_link_hash_warning)
603 h = (struct elf_link_hash_entry *) h->root.u.i.link;
605 if (h->dynindx != -1)
606 h->dynindx = ++(*count);
611 /* Return true if the dynamic symbol for a given section should be
612 omitted when creating a shared library. */
614 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
615 struct bfd_link_info *info,
618 switch (elf_section_data (p)->this_hdr.sh_type)
622 /* If sh_type is yet undecided, assume it could be
623 SHT_PROGBITS/SHT_NOBITS. */
625 if (strcmp (p->name, ".got") == 0
626 || strcmp (p->name, ".got.plt") == 0
627 || strcmp (p->name, ".plt") == 0)
630 bfd *dynobj = elf_hash_table (info)->dynobj;
633 && (ip = bfd_get_section_by_name (dynobj, p->name))
635 && (ip->flags & SEC_LINKER_CREATED)
636 && ip->output_section == p)
641 /* There shouldn't be section relative relocations
642 against any other section. */
648 /* Assign dynsym indices. In a shared library we generate a section
649 symbol for each output section, which come first. Next come all of
650 the back-end allocated local dynamic syms, followed by the rest of
651 the global symbols. */
654 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
656 unsigned long dynsymcount = 0;
660 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
662 for (p = output_bfd->sections; p ; p = p->next)
663 if ((p->flags & SEC_EXCLUDE) == 0
664 && (p->flags & SEC_ALLOC) != 0
665 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
666 elf_section_data (p)->dynindx = ++dynsymcount;
669 if (elf_hash_table (info)->dynlocal)
671 struct elf_link_local_dynamic_entry *p;
672 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
673 p->dynindx = ++dynsymcount;
676 elf_link_hash_traverse (elf_hash_table (info),
677 elf_link_renumber_hash_table_dynsyms,
680 /* There is an unused NULL entry at the head of the table which
681 we must account for in our count. Unless there weren't any
682 symbols, which means we'll have no table at all. */
683 if (dynsymcount != 0)
686 return elf_hash_table (info)->dynsymcount = dynsymcount;
689 /* This function is called when we want to define a new symbol. It
690 handles the various cases which arise when we find a definition in
691 a dynamic object, or when there is already a definition in a
692 dynamic object. The new symbol is described by NAME, SYM, PSEC,
693 and PVALUE. We set SYM_HASH to the hash table entry. We set
694 OVERRIDE if the old symbol is overriding a new definition. We set
695 TYPE_CHANGE_OK if it is OK for the type to change. We set
696 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
697 change, we mean that we shouldn't warn if the type or size does
701 _bfd_elf_merge_symbol (bfd *abfd,
702 struct bfd_link_info *info,
704 Elf_Internal_Sym *sym,
707 struct elf_link_hash_entry **sym_hash,
709 bfd_boolean *override,
710 bfd_boolean *type_change_ok,
711 bfd_boolean *size_change_ok)
714 struct elf_link_hash_entry *h;
715 struct elf_link_hash_entry *flip;
718 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
719 bfd_boolean newweak, oldweak;
725 bind = ELF_ST_BIND (sym->st_info);
727 if (! bfd_is_und_section (sec))
728 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
730 h = ((struct elf_link_hash_entry *)
731 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
736 /* This code is for coping with dynamic objects, and is only useful
737 if we are doing an ELF link. */
738 if (info->hash->creator != abfd->xvec)
741 /* For merging, we only care about real symbols. */
743 while (h->root.type == bfd_link_hash_indirect
744 || h->root.type == bfd_link_hash_warning)
745 h = (struct elf_link_hash_entry *) h->root.u.i.link;
747 /* If we just created the symbol, mark it as being an ELF symbol.
748 Other than that, there is nothing to do--there is no merge issue
749 with a newly defined symbol--so we just return. */
751 if (h->root.type == bfd_link_hash_new)
753 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
757 /* OLDBFD is a BFD associated with the existing symbol. */
759 switch (h->root.type)
765 case bfd_link_hash_undefined:
766 case bfd_link_hash_undefweak:
767 oldbfd = h->root.u.undef.abfd;
770 case bfd_link_hash_defined:
771 case bfd_link_hash_defweak:
772 oldbfd = h->root.u.def.section->owner;
775 case bfd_link_hash_common:
776 oldbfd = h->root.u.c.p->section->owner;
780 /* In cases involving weak versioned symbols, we may wind up trying
781 to merge a symbol with itself. Catch that here, to avoid the
782 confusion that results if we try to override a symbol with
783 itself. The additional tests catch cases like
784 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
785 dynamic object, which we do want to handle here. */
787 && ((abfd->flags & DYNAMIC) == 0
788 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
791 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
792 respectively, is from a dynamic object. */
794 if ((abfd->flags & DYNAMIC) != 0)
800 olddyn = (oldbfd->flags & DYNAMIC) != 0;
805 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
806 indices used by MIPS ELF. */
807 switch (h->root.type)
813 case bfd_link_hash_defined:
814 case bfd_link_hash_defweak:
815 hsec = h->root.u.def.section;
818 case bfd_link_hash_common:
819 hsec = h->root.u.c.p->section;
826 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
829 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
830 respectively, appear to be a definition rather than reference. */
832 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
837 if (h->root.type == bfd_link_hash_undefined
838 || h->root.type == bfd_link_hash_undefweak
839 || h->root.type == bfd_link_hash_common)
844 /* We need to remember if a symbol has a definition in a dynamic
845 object or is weak in all dynamic objects. Internal and hidden
846 visibility will make it unavailable to dynamic objects. */
847 if (newdyn && (h->elf_link_hash_flags & ELF_LINK_DYNAMIC_DEF) == 0)
849 if (!bfd_is_und_section (sec))
850 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_DEF;
853 /* Check if this symbol is weak in all dynamic objects. If it
854 is the first time we see it in a dynamic object, we mark
855 if it is weak. Otherwise, we clear it. */
856 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
858 if (bind == STB_WEAK)
859 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_WEAK;
861 else if (bind != STB_WEAK)
862 h->elf_link_hash_flags &= ~ELF_LINK_DYNAMIC_WEAK;
866 /* If the old symbol has non-default visibility, we ignore the new
867 definition from a dynamic object. */
869 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
870 && !bfd_is_und_section (sec))
873 /* Make sure this symbol is dynamic. */
874 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
875 /* A protected symbol has external availability. Make sure it is
878 FIXME: Should we check type and size for protected symbol? */
879 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
880 return bfd_elf_link_record_dynamic_symbol (info, h);
885 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
886 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
888 /* If the new symbol with non-default visibility comes from a
889 relocatable file and the old definition comes from a dynamic
890 object, we remove the old definition. */
891 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
894 if ((h->root.und_next || info->hash->undefs_tail == &h->root)
895 && bfd_is_und_section (sec))
897 /* If the new symbol is undefined and the old symbol was
898 also undefined before, we need to make sure
899 _bfd_generic_link_add_one_symbol doesn't mess
900 up the linker hash table undefs list. Since the old
901 definition came from a dynamic object, it is still on the
903 h->root.type = bfd_link_hash_undefined;
904 /* FIXME: What if the new symbol is weak undefined? */
905 h->root.u.undef.abfd = abfd;
909 h->root.type = bfd_link_hash_new;
910 h->root.u.undef.abfd = NULL;
913 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
915 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
916 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_DYNAMIC
917 | ELF_LINK_DYNAMIC_DEF);
919 /* FIXME: Should we check type and size for protected symbol? */
925 /* Differentiate strong and weak symbols. */
926 newweak = bind == STB_WEAK;
927 oldweak = (h->root.type == bfd_link_hash_defweak
928 || h->root.type == bfd_link_hash_undefweak);
930 /* If a new weak symbol definition comes from a regular file and the
931 old symbol comes from a dynamic library, we treat the new one as
932 strong. Similarly, an old weak symbol definition from a regular
933 file is treated as strong when the new symbol comes from a dynamic
934 library. Further, an old weak symbol from a dynamic library is
935 treated as strong if the new symbol is from a dynamic library.
936 This reflects the way glibc's ld.so works.
938 Do this before setting *type_change_ok or *size_change_ok so that
939 we warn properly when dynamic library symbols are overridden. */
941 if (newdef && !newdyn && olddyn)
943 if (olddef && newdyn)
946 /* It's OK to change the type if either the existing symbol or the
947 new symbol is weak. A type change is also OK if the old symbol
948 is undefined and the new symbol is defined. */
953 && h->root.type == bfd_link_hash_undefined))
954 *type_change_ok = TRUE;
956 /* It's OK to change the size if either the existing symbol or the
957 new symbol is weak, or if the old symbol is undefined. */
960 || h->root.type == bfd_link_hash_undefined)
961 *size_change_ok = TRUE;
963 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
964 symbol, respectively, appears to be a common symbol in a dynamic
965 object. If a symbol appears in an uninitialized section, and is
966 not weak, and is not a function, then it may be a common symbol
967 which was resolved when the dynamic object was created. We want
968 to treat such symbols specially, because they raise special
969 considerations when setting the symbol size: if the symbol
970 appears as a common symbol in a regular object, and the size in
971 the regular object is larger, we must make sure that we use the
972 larger size. This problematic case can always be avoided in C,
973 but it must be handled correctly when using Fortran shared
976 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
977 likewise for OLDDYNCOMMON and OLDDEF.
979 Note that this test is just a heuristic, and that it is quite
980 possible to have an uninitialized symbol in a shared object which
981 is really a definition, rather than a common symbol. This could
982 lead to some minor confusion when the symbol really is a common
983 symbol in some regular object. However, I think it will be
989 && (sec->flags & SEC_ALLOC) != 0
990 && (sec->flags & SEC_LOAD) == 0
992 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
995 newdyncommon = FALSE;
999 && h->root.type == bfd_link_hash_defined
1000 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1001 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1002 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1004 && h->type != STT_FUNC)
1005 olddyncommon = TRUE;
1007 olddyncommon = FALSE;
1009 /* If both the old and the new symbols look like common symbols in a
1010 dynamic object, set the size of the symbol to the larger of the
1015 && sym->st_size != h->size)
1017 /* Since we think we have two common symbols, issue a multiple
1018 common warning if desired. Note that we only warn if the
1019 size is different. If the size is the same, we simply let
1020 the old symbol override the new one as normally happens with
1021 symbols defined in dynamic objects. */
1023 if (! ((*info->callbacks->multiple_common)
1024 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1025 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1028 if (sym->st_size > h->size)
1029 h->size = sym->st_size;
1031 *size_change_ok = TRUE;
1034 /* If we are looking at a dynamic object, and we have found a
1035 definition, we need to see if the symbol was already defined by
1036 some other object. If so, we want to use the existing
1037 definition, and we do not want to report a multiple symbol
1038 definition error; we do this by clobbering *PSEC to be
1039 bfd_und_section_ptr.
1041 We treat a common symbol as a definition if the symbol in the
1042 shared library is a function, since common symbols always
1043 represent variables; this can cause confusion in principle, but
1044 any such confusion would seem to indicate an erroneous program or
1045 shared library. We also permit a common symbol in a regular
1046 object to override a weak symbol in a shared object. */
1051 || (h->root.type == bfd_link_hash_common
1053 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1057 newdyncommon = FALSE;
1059 *psec = sec = bfd_und_section_ptr;
1060 *size_change_ok = TRUE;
1062 /* If we get here when the old symbol is a common symbol, then
1063 we are explicitly letting it override a weak symbol or
1064 function in a dynamic object, and we don't want to warn about
1065 a type change. If the old symbol is a defined symbol, a type
1066 change warning may still be appropriate. */
1068 if (h->root.type == bfd_link_hash_common)
1069 *type_change_ok = TRUE;
1072 /* Handle the special case of an old common symbol merging with a
1073 new symbol which looks like a common symbol in a shared object.
1074 We change *PSEC and *PVALUE to make the new symbol look like a
1075 common symbol, and let _bfd_generic_link_add_one_symbol will do
1079 && h->root.type == bfd_link_hash_common)
1083 newdyncommon = FALSE;
1084 *pvalue = sym->st_size;
1085 *psec = sec = bfd_com_section_ptr;
1086 *size_change_ok = TRUE;
1089 /* If the old symbol is from a dynamic object, and the new symbol is
1090 a definition which is not from a dynamic object, then the new
1091 symbol overrides the old symbol. Symbols from regular files
1092 always take precedence over symbols from dynamic objects, even if
1093 they are defined after the dynamic object in the link.
1095 As above, we again permit a common symbol in a regular object to
1096 override a definition in a shared object if the shared object
1097 symbol is a function or is weak. */
1102 || (bfd_is_com_section (sec)
1104 || h->type == STT_FUNC)))
1107 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
1109 /* Change the hash table entry to undefined, and let
1110 _bfd_generic_link_add_one_symbol do the right thing with the
1113 h->root.type = bfd_link_hash_undefined;
1114 h->root.u.undef.abfd = h->root.u.def.section->owner;
1115 *size_change_ok = TRUE;
1118 olddyncommon = FALSE;
1120 /* We again permit a type change when a common symbol may be
1121 overriding a function. */
1123 if (bfd_is_com_section (sec))
1124 *type_change_ok = TRUE;
1126 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1129 /* This union may have been set to be non-NULL when this symbol
1130 was seen in a dynamic object. We must force the union to be
1131 NULL, so that it is correct for a regular symbol. */
1132 h->verinfo.vertree = NULL;
1135 /* Handle the special case of a new common symbol merging with an
1136 old symbol that looks like it might be a common symbol defined in
1137 a shared object. Note that we have already handled the case in
1138 which a new common symbol should simply override the definition
1139 in the shared library. */
1142 && bfd_is_com_section (sec)
1145 /* It would be best if we could set the hash table entry to a
1146 common symbol, but we don't know what to use for the section
1147 or the alignment. */
1148 if (! ((*info->callbacks->multiple_common)
1149 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1150 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1153 /* If the presumed common symbol in the dynamic object is
1154 larger, pretend that the new symbol has its size. */
1156 if (h->size > *pvalue)
1159 /* FIXME: We no longer know the alignment required by the symbol
1160 in the dynamic object, so we just wind up using the one from
1161 the regular object. */
1164 olddyncommon = FALSE;
1166 h->root.type = bfd_link_hash_undefined;
1167 h->root.u.undef.abfd = h->root.u.def.section->owner;
1169 *size_change_ok = TRUE;
1170 *type_change_ok = TRUE;
1172 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1175 h->verinfo.vertree = NULL;
1180 /* Handle the case where we had a versioned symbol in a dynamic
1181 library and now find a definition in a normal object. In this
1182 case, we make the versioned symbol point to the normal one. */
1183 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1184 flip->root.type = h->root.type;
1185 h->root.type = bfd_link_hash_indirect;
1186 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1187 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1188 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1189 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1191 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
1192 flip->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1199 /* This function is called to create an indirect symbol from the
1200 default for the symbol with the default version if needed. The
1201 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1202 set DYNSYM if the new indirect symbol is dynamic. */
1205 _bfd_elf_add_default_symbol (bfd *abfd,
1206 struct bfd_link_info *info,
1207 struct elf_link_hash_entry *h,
1209 Elf_Internal_Sym *sym,
1212 bfd_boolean *dynsym,
1213 bfd_boolean override)
1215 bfd_boolean type_change_ok;
1216 bfd_boolean size_change_ok;
1219 struct elf_link_hash_entry *hi;
1220 struct bfd_link_hash_entry *bh;
1221 const struct elf_backend_data *bed;
1222 bfd_boolean collect;
1223 bfd_boolean dynamic;
1225 size_t len, shortlen;
1228 /* If this symbol has a version, and it is the default version, we
1229 create an indirect symbol from the default name to the fully
1230 decorated name. This will cause external references which do not
1231 specify a version to be bound to this version of the symbol. */
1232 p = strchr (name, ELF_VER_CHR);
1233 if (p == NULL || p[1] != ELF_VER_CHR)
1238 /* We are overridden by an old definition. We need to check if we
1239 need to create the indirect symbol from the default name. */
1240 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1242 BFD_ASSERT (hi != NULL);
1245 while (hi->root.type == bfd_link_hash_indirect
1246 || hi->root.type == bfd_link_hash_warning)
1248 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1254 bed = get_elf_backend_data (abfd);
1255 collect = bed->collect;
1256 dynamic = (abfd->flags & DYNAMIC) != 0;
1258 shortlen = p - name;
1259 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1260 if (shortname == NULL)
1262 memcpy (shortname, name, shortlen);
1263 shortname[shortlen] = '\0';
1265 /* We are going to create a new symbol. Merge it with any existing
1266 symbol with this name. For the purposes of the merge, act as
1267 though we were defining the symbol we just defined, although we
1268 actually going to define an indirect symbol. */
1269 type_change_ok = FALSE;
1270 size_change_ok = FALSE;
1272 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1273 &hi, &skip, &override, &type_change_ok,
1283 if (! (_bfd_generic_link_add_one_symbol
1284 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1285 0, name, FALSE, collect, &bh)))
1287 hi = (struct elf_link_hash_entry *) bh;
1291 /* In this case the symbol named SHORTNAME is overriding the
1292 indirect symbol we want to add. We were planning on making
1293 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1294 is the name without a version. NAME is the fully versioned
1295 name, and it is the default version.
1297 Overriding means that we already saw a definition for the
1298 symbol SHORTNAME in a regular object, and it is overriding
1299 the symbol defined in the dynamic object.
1301 When this happens, we actually want to change NAME, the
1302 symbol we just added, to refer to SHORTNAME. This will cause
1303 references to NAME in the shared object to become references
1304 to SHORTNAME in the regular object. This is what we expect
1305 when we override a function in a shared object: that the
1306 references in the shared object will be mapped to the
1307 definition in the regular object. */
1309 while (hi->root.type == bfd_link_hash_indirect
1310 || hi->root.type == bfd_link_hash_warning)
1311 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1313 h->root.type = bfd_link_hash_indirect;
1314 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1315 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1317 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1318 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1319 if (hi->elf_link_hash_flags
1320 & (ELF_LINK_HASH_REF_REGULAR
1321 | ELF_LINK_HASH_DEF_REGULAR))
1323 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1328 /* Now set HI to H, so that the following code will set the
1329 other fields correctly. */
1333 /* If there is a duplicate definition somewhere, then HI may not
1334 point to an indirect symbol. We will have reported an error to
1335 the user in that case. */
1337 if (hi->root.type == bfd_link_hash_indirect)
1339 struct elf_link_hash_entry *ht;
1341 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1342 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1344 /* See if the new flags lead us to realize that the symbol must
1351 || ((hi->elf_link_hash_flags
1352 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1357 if ((hi->elf_link_hash_flags
1358 & ELF_LINK_HASH_REF_REGULAR) != 0)
1364 /* We also need to define an indirection from the nondefault version
1368 len = strlen (name);
1369 shortname = bfd_hash_allocate (&info->hash->table, len);
1370 if (shortname == NULL)
1372 memcpy (shortname, name, shortlen);
1373 memcpy (shortname + shortlen, p + 1, len - shortlen);
1375 /* Once again, merge with any existing symbol. */
1376 type_change_ok = FALSE;
1377 size_change_ok = FALSE;
1379 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1380 &hi, &skip, &override, &type_change_ok,
1389 /* Here SHORTNAME is a versioned name, so we don't expect to see
1390 the type of override we do in the case above unless it is
1391 overridden by a versioned definition. */
1392 if (hi->root.type != bfd_link_hash_defined
1393 && hi->root.type != bfd_link_hash_defweak)
1394 (*_bfd_error_handler)
1395 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1396 bfd_archive_filename (abfd), shortname);
1401 if (! (_bfd_generic_link_add_one_symbol
1402 (info, abfd, shortname, BSF_INDIRECT,
1403 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1405 hi = (struct elf_link_hash_entry *) bh;
1407 /* If there is a duplicate definition somewhere, then HI may not
1408 point to an indirect symbol. We will have reported an error
1409 to the user in that case. */
1411 if (hi->root.type == bfd_link_hash_indirect)
1413 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1415 /* See if the new flags lead us to realize that the symbol
1422 || ((hi->elf_link_hash_flags
1423 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1428 if ((hi->elf_link_hash_flags
1429 & ELF_LINK_HASH_REF_REGULAR) != 0)
1439 /* This routine is used to export all defined symbols into the dynamic
1440 symbol table. It is called via elf_link_hash_traverse. */
1443 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1445 struct elf_info_failed *eif = data;
1447 /* Ignore indirect symbols. These are added by the versioning code. */
1448 if (h->root.type == bfd_link_hash_indirect)
1451 if (h->root.type == bfd_link_hash_warning)
1452 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1454 if (h->dynindx == -1
1455 && (h->elf_link_hash_flags
1456 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
1458 struct bfd_elf_version_tree *t;
1459 struct bfd_elf_version_expr *d;
1461 for (t = eif->verdefs; t != NULL; t = t->next)
1463 if (t->globals.list != NULL)
1465 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1470 if (t->locals.list != NULL)
1472 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1481 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1492 /* Look through the symbols which are defined in other shared
1493 libraries and referenced here. Update the list of version
1494 dependencies. This will be put into the .gnu.version_r section.
1495 This function is called via elf_link_hash_traverse. */
1498 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1501 struct elf_find_verdep_info *rinfo = data;
1502 Elf_Internal_Verneed *t;
1503 Elf_Internal_Vernaux *a;
1506 if (h->root.type == bfd_link_hash_warning)
1507 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1509 /* We only care about symbols defined in shared objects with version
1511 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1512 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1514 || h->verinfo.verdef == NULL)
1517 /* See if we already know about this version. */
1518 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1520 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1523 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1524 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1530 /* This is a new version. Add it to tree we are building. */
1535 t = bfd_zalloc (rinfo->output_bfd, amt);
1538 rinfo->failed = TRUE;
1542 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1543 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1544 elf_tdata (rinfo->output_bfd)->verref = t;
1548 a = bfd_zalloc (rinfo->output_bfd, amt);
1550 /* Note that we are copying a string pointer here, and testing it
1551 above. If bfd_elf_string_from_elf_section is ever changed to
1552 discard the string data when low in memory, this will have to be
1554 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1556 a->vna_flags = h->verinfo.verdef->vd_flags;
1557 a->vna_nextptr = t->vn_auxptr;
1559 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1562 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1569 /* Figure out appropriate versions for all the symbols. We may not
1570 have the version number script until we have read all of the input
1571 files, so until that point we don't know which symbols should be
1572 local. This function is called via elf_link_hash_traverse. */
1575 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1577 struct elf_assign_sym_version_info *sinfo;
1578 struct bfd_link_info *info;
1579 const struct elf_backend_data *bed;
1580 struct elf_info_failed eif;
1587 if (h->root.type == bfd_link_hash_warning)
1588 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1590 /* Fix the symbol flags. */
1593 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1596 sinfo->failed = TRUE;
1600 /* We only need version numbers for symbols defined in regular
1602 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1605 bed = get_elf_backend_data (sinfo->output_bfd);
1606 p = strchr (h->root.root.string, ELF_VER_CHR);
1607 if (p != NULL && h->verinfo.vertree == NULL)
1609 struct bfd_elf_version_tree *t;
1614 /* There are two consecutive ELF_VER_CHR characters if this is
1615 not a hidden symbol. */
1617 if (*p == ELF_VER_CHR)
1623 /* If there is no version string, we can just return out. */
1627 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1631 /* Look for the version. If we find it, it is no longer weak. */
1632 for (t = sinfo->verdefs; t != NULL; t = t->next)
1634 if (strcmp (t->name, p) == 0)
1638 struct bfd_elf_version_expr *d;
1640 len = p - h->root.root.string;
1641 alc = bfd_malloc (len);
1644 memcpy (alc, h->root.root.string, len - 1);
1645 alc[len - 1] = '\0';
1646 if (alc[len - 2] == ELF_VER_CHR)
1647 alc[len - 2] = '\0';
1649 h->verinfo.vertree = t;
1653 if (t->globals.list != NULL)
1654 d = (*t->match) (&t->globals, NULL, alc);
1656 /* See if there is anything to force this symbol to
1658 if (d == NULL && t->locals.list != NULL)
1660 d = (*t->match) (&t->locals, NULL, alc);
1664 && ! info->export_dynamic)
1665 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1673 /* If we are building an application, we need to create a
1674 version node for this version. */
1675 if (t == NULL && info->executable)
1677 struct bfd_elf_version_tree **pp;
1680 /* If we aren't going to export this symbol, we don't need
1681 to worry about it. */
1682 if (h->dynindx == -1)
1686 t = bfd_zalloc (sinfo->output_bfd, amt);
1689 sinfo->failed = TRUE;
1694 t->name_indx = (unsigned int) -1;
1698 /* Don't count anonymous version tag. */
1699 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1701 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1703 t->vernum = version_index;
1707 h->verinfo.vertree = t;
1711 /* We could not find the version for a symbol when
1712 generating a shared archive. Return an error. */
1713 (*_bfd_error_handler)
1714 (_("%s: undefined versioned symbol name %s"),
1715 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
1716 bfd_set_error (bfd_error_bad_value);
1717 sinfo->failed = TRUE;
1722 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1725 /* If we don't have a version for this symbol, see if we can find
1727 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1729 struct bfd_elf_version_tree *t;
1730 struct bfd_elf_version_tree *local_ver;
1731 struct bfd_elf_version_expr *d;
1733 /* See if can find what version this symbol is in. If the
1734 symbol is supposed to be local, then don't actually register
1737 for (t = sinfo->verdefs; t != NULL; t = t->next)
1739 if (t->globals.list != NULL)
1741 bfd_boolean matched;
1745 while ((d = (*t->match) (&t->globals, d,
1746 h->root.root.string)) != NULL)
1751 /* There is a version without definition. Make
1752 the symbol the default definition for this
1754 h->verinfo.vertree = t;
1762 /* There is no undefined version for this symbol. Hide the
1764 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1767 if (t->locals.list != NULL)
1770 while ((d = (*t->match) (&t->locals, d,
1771 h->root.root.string)) != NULL)
1774 /* If the match is "*", keep looking for a more
1775 explicit, perhaps even global, match.
1776 XXX: Shouldn't this be !d->wildcard instead? */
1777 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1786 if (local_ver != NULL)
1788 h->verinfo.vertree = local_ver;
1789 if (h->dynindx != -1
1791 && ! info->export_dynamic)
1793 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1801 /* Read and swap the relocs from the section indicated by SHDR. This
1802 may be either a REL or a RELA section. The relocations are
1803 translated into RELA relocations and stored in INTERNAL_RELOCS,
1804 which should have already been allocated to contain enough space.
1805 The EXTERNAL_RELOCS are a buffer where the external form of the
1806 relocations should be stored.
1808 Returns FALSE if something goes wrong. */
1811 elf_link_read_relocs_from_section (bfd *abfd,
1813 Elf_Internal_Shdr *shdr,
1814 void *external_relocs,
1815 Elf_Internal_Rela *internal_relocs)
1817 const struct elf_backend_data *bed;
1818 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1819 const bfd_byte *erela;
1820 const bfd_byte *erelaend;
1821 Elf_Internal_Rela *irela;
1822 Elf_Internal_Shdr *symtab_hdr;
1825 /* Position ourselves at the start of the section. */
1826 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1829 /* Read the relocations. */
1830 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1833 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1834 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1836 bed = get_elf_backend_data (abfd);
1838 /* Convert the external relocations to the internal format. */
1839 if (shdr->sh_entsize == bed->s->sizeof_rel)
1840 swap_in = bed->s->swap_reloc_in;
1841 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1842 swap_in = bed->s->swap_reloca_in;
1845 bfd_set_error (bfd_error_wrong_format);
1849 erela = external_relocs;
1850 erelaend = erela + shdr->sh_size;
1851 irela = internal_relocs;
1852 while (erela < erelaend)
1856 (*swap_in) (abfd, erela, irela);
1857 r_symndx = ELF32_R_SYM (irela->r_info);
1858 if (bed->s->arch_size == 64)
1860 if ((size_t) r_symndx >= nsyms)
1862 char *sec_name = bfd_get_section_ident (sec);
1863 (*_bfd_error_handler)
1864 (_("%s: bad reloc symbol index (0x%lx >= 0x%lx) for offset 0x%lx in section `%s'"),
1865 bfd_archive_filename (abfd), (unsigned long) r_symndx,
1866 (unsigned long) nsyms, irela->r_offset,
1867 sec_name ? sec_name : sec->name);
1870 bfd_set_error (bfd_error_bad_value);
1873 irela += bed->s->int_rels_per_ext_rel;
1874 erela += shdr->sh_entsize;
1880 /* Read and swap the relocs for a section O. They may have been
1881 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1882 not NULL, they are used as buffers to read into. They are known to
1883 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1884 the return value is allocated using either malloc or bfd_alloc,
1885 according to the KEEP_MEMORY argument. If O has two relocation
1886 sections (both REL and RELA relocations), then the REL_HDR
1887 relocations will appear first in INTERNAL_RELOCS, followed by the
1888 REL_HDR2 relocations. */
1891 _bfd_elf_link_read_relocs (bfd *abfd,
1893 void *external_relocs,
1894 Elf_Internal_Rela *internal_relocs,
1895 bfd_boolean keep_memory)
1897 Elf_Internal_Shdr *rel_hdr;
1898 void *alloc1 = NULL;
1899 Elf_Internal_Rela *alloc2 = NULL;
1900 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1902 if (elf_section_data (o)->relocs != NULL)
1903 return elf_section_data (o)->relocs;
1905 if (o->reloc_count == 0)
1908 rel_hdr = &elf_section_data (o)->rel_hdr;
1910 if (internal_relocs == NULL)
1914 size = o->reloc_count;
1915 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1917 internal_relocs = bfd_alloc (abfd, size);
1919 internal_relocs = alloc2 = bfd_malloc (size);
1920 if (internal_relocs == NULL)
1924 if (external_relocs == NULL)
1926 bfd_size_type size = rel_hdr->sh_size;
1928 if (elf_section_data (o)->rel_hdr2)
1929 size += elf_section_data (o)->rel_hdr2->sh_size;
1930 alloc1 = bfd_malloc (size);
1933 external_relocs = alloc1;
1936 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
1940 if (elf_section_data (o)->rel_hdr2
1941 && (!elf_link_read_relocs_from_section
1943 elf_section_data (o)->rel_hdr2,
1944 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
1945 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
1946 * bed->s->int_rels_per_ext_rel))))
1949 /* Cache the results for next time, if we can. */
1951 elf_section_data (o)->relocs = internal_relocs;
1956 /* Don't free alloc2, since if it was allocated we are passing it
1957 back (under the name of internal_relocs). */
1959 return internal_relocs;
1969 /* Compute the size of, and allocate space for, REL_HDR which is the
1970 section header for a section containing relocations for O. */
1973 _bfd_elf_link_size_reloc_section (bfd *abfd,
1974 Elf_Internal_Shdr *rel_hdr,
1977 bfd_size_type reloc_count;
1978 bfd_size_type num_rel_hashes;
1980 /* Figure out how many relocations there will be. */
1981 if (rel_hdr == &elf_section_data (o)->rel_hdr)
1982 reloc_count = elf_section_data (o)->rel_count;
1984 reloc_count = elf_section_data (o)->rel_count2;
1986 num_rel_hashes = o->reloc_count;
1987 if (num_rel_hashes < reloc_count)
1988 num_rel_hashes = reloc_count;
1990 /* That allows us to calculate the size of the section. */
1991 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
1993 /* The contents field must last into write_object_contents, so we
1994 allocate it with bfd_alloc rather than malloc. Also since we
1995 cannot be sure that the contents will actually be filled in,
1996 we zero the allocated space. */
1997 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
1998 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2001 /* We only allocate one set of hash entries, so we only do it the
2002 first time we are called. */
2003 if (elf_section_data (o)->rel_hashes == NULL
2006 struct elf_link_hash_entry **p;
2008 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2012 elf_section_data (o)->rel_hashes = p;
2018 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2019 originated from the section given by INPUT_REL_HDR) to the
2023 _bfd_elf_link_output_relocs (bfd *output_bfd,
2024 asection *input_section,
2025 Elf_Internal_Shdr *input_rel_hdr,
2026 Elf_Internal_Rela *internal_relocs)
2028 Elf_Internal_Rela *irela;
2029 Elf_Internal_Rela *irelaend;
2031 Elf_Internal_Shdr *output_rel_hdr;
2032 asection *output_section;
2033 unsigned int *rel_countp = NULL;
2034 const struct elf_backend_data *bed;
2035 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2037 output_section = input_section->output_section;
2038 output_rel_hdr = NULL;
2040 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2041 == input_rel_hdr->sh_entsize)
2043 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2044 rel_countp = &elf_section_data (output_section)->rel_count;
2046 else if (elf_section_data (output_section)->rel_hdr2
2047 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2048 == input_rel_hdr->sh_entsize))
2050 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2051 rel_countp = &elf_section_data (output_section)->rel_count2;
2055 char *sec_name = bfd_get_section_ident (input_section);
2056 (*_bfd_error_handler)
2057 (_("%s: relocation size mismatch in %s section %s"),
2058 bfd_get_filename (output_bfd),
2059 bfd_archive_filename (input_section->owner),
2060 sec_name ? sec_name : input_section->name);
2063 bfd_set_error (bfd_error_wrong_object_format);
2067 bed = get_elf_backend_data (output_bfd);
2068 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2069 swap_out = bed->s->swap_reloc_out;
2070 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2071 swap_out = bed->s->swap_reloca_out;
2075 erel = output_rel_hdr->contents;
2076 erel += *rel_countp * input_rel_hdr->sh_entsize;
2077 irela = internal_relocs;
2078 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2079 * bed->s->int_rels_per_ext_rel);
2080 while (irela < irelaend)
2082 (*swap_out) (output_bfd, irela, erel);
2083 irela += bed->s->int_rels_per_ext_rel;
2084 erel += input_rel_hdr->sh_entsize;
2087 /* Bump the counter, so that we know where to add the next set of
2089 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2094 /* Fix up the flags for a symbol. This handles various cases which
2095 can only be fixed after all the input files are seen. This is
2096 currently called by both adjust_dynamic_symbol and
2097 assign_sym_version, which is unnecessary but perhaps more robust in
2098 the face of future changes. */
2101 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2102 struct elf_info_failed *eif)
2104 /* If this symbol was mentioned in a non-ELF file, try to set
2105 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2106 permit a non-ELF file to correctly refer to a symbol defined in
2107 an ELF dynamic object. */
2108 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
2110 while (h->root.type == bfd_link_hash_indirect)
2111 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2113 if (h->root.type != bfd_link_hash_defined
2114 && h->root.type != bfd_link_hash_defweak)
2115 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2116 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2119 if (h->root.u.def.section->owner != NULL
2120 && (bfd_get_flavour (h->root.u.def.section->owner)
2121 == bfd_target_elf_flavour))
2122 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2123 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2125 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2128 if (h->dynindx == -1
2129 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2130 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
2132 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2141 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2142 was first seen in a non-ELF file. Fortunately, if the symbol
2143 was first seen in an ELF file, we're probably OK unless the
2144 symbol was defined in a non-ELF file. Catch that case here.
2145 FIXME: We're still in trouble if the symbol was first seen in
2146 a dynamic object, and then later in a non-ELF regular object. */
2147 if ((h->root.type == bfd_link_hash_defined
2148 || h->root.type == bfd_link_hash_defweak)
2149 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2150 && (h->root.u.def.section->owner != NULL
2151 ? (bfd_get_flavour (h->root.u.def.section->owner)
2152 != bfd_target_elf_flavour)
2153 : (bfd_is_abs_section (h->root.u.def.section)
2154 && (h->elf_link_hash_flags
2155 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
2156 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2159 /* If this is a final link, and the symbol was defined as a common
2160 symbol in a regular object file, and there was no definition in
2161 any dynamic object, then the linker will have allocated space for
2162 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2163 flag will not have been set. */
2164 if (h->root.type == bfd_link_hash_defined
2165 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2166 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
2167 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2168 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2169 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2171 /* If -Bsymbolic was used (which means to bind references to global
2172 symbols to the definition within the shared object), and this
2173 symbol was defined in a regular object, then it actually doesn't
2174 need a PLT entry. Likewise, if the symbol has non-default
2175 visibility. If the symbol has hidden or internal visibility, we
2176 will force it local. */
2177 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
2178 && eif->info->shared
2179 && is_elf_hash_table (eif->info->hash)
2180 && (eif->info->symbolic
2181 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2182 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2184 const struct elf_backend_data *bed;
2185 bfd_boolean force_local;
2187 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2189 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2190 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2191 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2194 /* If a weak undefined symbol has non-default visibility, we also
2195 hide it from the dynamic linker. */
2196 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2197 && h->root.type == bfd_link_hash_undefweak)
2199 const struct elf_backend_data *bed;
2200 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2201 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2204 /* If this is a weak defined symbol in a dynamic object, and we know
2205 the real definition in the dynamic object, copy interesting flags
2206 over to the real definition. */
2207 if (h->weakdef != NULL)
2209 struct elf_link_hash_entry *weakdef;
2211 weakdef = h->weakdef;
2212 if (h->root.type == bfd_link_hash_indirect)
2213 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2215 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2216 || h->root.type == bfd_link_hash_defweak);
2217 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2218 || weakdef->root.type == bfd_link_hash_defweak);
2219 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
2221 /* If the real definition is defined by a regular object file,
2222 don't do anything special. See the longer description in
2223 _bfd_elf_adjust_dynamic_symbol, below. */
2224 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2228 const struct elf_backend_data *bed;
2230 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2231 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2238 /* Make the backend pick a good value for a dynamic symbol. This is
2239 called via elf_link_hash_traverse, and also calls itself
2243 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2245 struct elf_info_failed *eif = data;
2247 const struct elf_backend_data *bed;
2249 if (! is_elf_hash_table (eif->info->hash))
2252 if (h->root.type == bfd_link_hash_warning)
2254 h->plt = elf_hash_table (eif->info)->init_offset;
2255 h->got = elf_hash_table (eif->info)->init_offset;
2257 /* When warning symbols are created, they **replace** the "real"
2258 entry in the hash table, thus we never get to see the real
2259 symbol in a hash traversal. So look at it now. */
2260 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2263 /* Ignore indirect symbols. These are added by the versioning code. */
2264 if (h->root.type == bfd_link_hash_indirect)
2267 /* Fix the symbol flags. */
2268 if (! _bfd_elf_fix_symbol_flags (h, eif))
2271 /* If this symbol does not require a PLT entry, and it is not
2272 defined by a dynamic object, or is not referenced by a regular
2273 object, ignore it. We do have to handle a weak defined symbol,
2274 even if no regular object refers to it, if we decided to add it
2275 to the dynamic symbol table. FIXME: Do we normally need to worry
2276 about symbols which are defined by one dynamic object and
2277 referenced by another one? */
2278 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
2279 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2280 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2281 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
2282 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
2284 h->plt = elf_hash_table (eif->info)->init_offset;
2288 /* If we've already adjusted this symbol, don't do it again. This
2289 can happen via a recursive call. */
2290 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
2293 /* Don't look at this symbol again. Note that we must set this
2294 after checking the above conditions, because we may look at a
2295 symbol once, decide not to do anything, and then get called
2296 recursively later after REF_REGULAR is set below. */
2297 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
2299 /* If this is a weak definition, and we know a real definition, and
2300 the real symbol is not itself defined by a regular object file,
2301 then get a good value for the real definition. We handle the
2302 real symbol first, for the convenience of the backend routine.
2304 Note that there is a confusing case here. If the real definition
2305 is defined by a regular object file, we don't get the real symbol
2306 from the dynamic object, but we do get the weak symbol. If the
2307 processor backend uses a COPY reloc, then if some routine in the
2308 dynamic object changes the real symbol, we will not see that
2309 change in the corresponding weak symbol. This is the way other
2310 ELF linkers work as well, and seems to be a result of the shared
2313 I will clarify this issue. Most SVR4 shared libraries define the
2314 variable _timezone and define timezone as a weak synonym. The
2315 tzset call changes _timezone. If you write
2316 extern int timezone;
2318 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2319 you might expect that, since timezone is a synonym for _timezone,
2320 the same number will print both times. However, if the processor
2321 backend uses a COPY reloc, then actually timezone will be copied
2322 into your process image, and, since you define _timezone
2323 yourself, _timezone will not. Thus timezone and _timezone will
2324 wind up at different memory locations. The tzset call will set
2325 _timezone, leaving timezone unchanged. */
2327 if (h->weakdef != NULL)
2329 /* If we get to this point, we know there is an implicit
2330 reference by a regular object file via the weak symbol H.
2331 FIXME: Is this really true? What if the traversal finds
2332 H->WEAKDEF before it finds H? */
2333 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2335 if (! _bfd_elf_adjust_dynamic_symbol (h->weakdef, eif))
2339 /* If a symbol has no type and no size and does not require a PLT
2340 entry, then we are probably about to do the wrong thing here: we
2341 are probably going to create a COPY reloc for an empty object.
2342 This case can arise when a shared object is built with assembly
2343 code, and the assembly code fails to set the symbol type. */
2345 && h->type == STT_NOTYPE
2346 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
2347 (*_bfd_error_handler)
2348 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2349 h->root.root.string);
2351 dynobj = elf_hash_table (eif->info)->dynobj;
2352 bed = get_elf_backend_data (dynobj);
2353 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2362 /* Adjust all external symbols pointing into SEC_MERGE sections
2363 to reflect the object merging within the sections. */
2366 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2370 if (h->root.type == bfd_link_hash_warning)
2371 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2373 if ((h->root.type == bfd_link_hash_defined
2374 || h->root.type == bfd_link_hash_defweak)
2375 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2376 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2378 bfd *output_bfd = data;
2380 h->root.u.def.value =
2381 _bfd_merged_section_offset (output_bfd,
2382 &h->root.u.def.section,
2383 elf_section_data (sec)->sec_info,
2384 h->root.u.def.value);
2390 /* Returns false if the symbol referred to by H should be considered
2391 to resolve local to the current module, and true if it should be
2392 considered to bind dynamically. */
2395 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2396 struct bfd_link_info *info,
2397 bfd_boolean ignore_protected)
2399 bfd_boolean binding_stays_local_p;
2404 while (h->root.type == bfd_link_hash_indirect
2405 || h->root.type == bfd_link_hash_warning)
2406 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2408 /* If it was forced local, then clearly it's not dynamic. */
2409 if (h->dynindx == -1)
2411 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2414 /* Identify the cases where name binding rules say that a
2415 visible symbol resolves locally. */
2416 binding_stays_local_p = info->executable || info->symbolic;
2418 switch (ELF_ST_VISIBILITY (h->other))
2425 /* Proper resolution for function pointer equality may require
2426 that these symbols perhaps be resolved dynamically, even though
2427 we should be resolving them to the current module. */
2428 if (!ignore_protected)
2429 binding_stays_local_p = TRUE;
2436 /* If it isn't defined locally, then clearly it's dynamic. */
2437 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2440 /* Otherwise, the symbol is dynamic if binding rules don't tell
2441 us that it remains local. */
2442 return !binding_stays_local_p;
2445 /* Return true if the symbol referred to by H should be considered
2446 to resolve local to the current module, and false otherwise. Differs
2447 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2448 undefined symbols and weak symbols. */
2451 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2452 struct bfd_link_info *info,
2453 bfd_boolean local_protected)
2455 /* If it's a local sym, of course we resolve locally. */
2459 /* If we don't have a definition in a regular file, then we can't
2460 resolve locally. The sym is either undefined or dynamic. */
2461 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2464 /* Forced local symbols resolve locally. */
2465 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
2468 /* As do non-dynamic symbols. */
2469 if (h->dynindx == -1)
2472 /* At this point, we know the symbol is defined and dynamic. In an
2473 executable it must resolve locally, likewise when building symbolic
2474 shared libraries. */
2475 if (info->executable || info->symbolic)
2478 /* Now deal with defined dynamic symbols in shared libraries. Ones
2479 with default visibility might not resolve locally. */
2480 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2483 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2484 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2487 /* Function pointer equality tests may require that STV_PROTECTED
2488 symbols be treated as dynamic symbols, even when we know that the
2489 dynamic linker will resolve them locally. */
2490 return local_protected;
2493 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2494 aligned. Returns the first TLS output section. */
2496 struct bfd_section *
2497 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2499 struct bfd_section *sec, *tls;
2500 unsigned int align = 0;
2502 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2503 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2507 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2508 if (sec->alignment_power > align)
2509 align = sec->alignment_power;
2511 elf_hash_table (info)->tls_sec = tls;
2513 /* Ensure the alignment of the first section is the largest alignment,
2514 so that the tls segment starts aligned. */
2516 tls->alignment_power = align;
2521 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2523 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2524 Elf_Internal_Sym *sym)
2526 /* Local symbols do not count, but target specific ones might. */
2527 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2528 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2531 /* Function symbols do not count. */
2532 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2535 /* If the section is undefined, then so is the symbol. */
2536 if (sym->st_shndx == SHN_UNDEF)
2539 /* If the symbol is defined in the common section, then
2540 it is a common definition and so does not count. */
2541 if (sym->st_shndx == SHN_COMMON)
2544 /* If the symbol is in a target specific section then we
2545 must rely upon the backend to tell us what it is. */
2546 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2547 /* FIXME - this function is not coded yet:
2549 return _bfd_is_global_symbol_definition (abfd, sym);
2551 Instead for now assume that the definition is not global,
2552 Even if this is wrong, at least the linker will behave
2553 in the same way that it used to do. */
2559 /* Search the symbol table of the archive element of the archive ABFD
2560 whose archive map contains a mention of SYMDEF, and determine if
2561 the symbol is defined in this element. */
2563 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2565 Elf_Internal_Shdr * hdr;
2566 bfd_size_type symcount;
2567 bfd_size_type extsymcount;
2568 bfd_size_type extsymoff;
2569 Elf_Internal_Sym *isymbuf;
2570 Elf_Internal_Sym *isym;
2571 Elf_Internal_Sym *isymend;
2574 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2578 if (! bfd_check_format (abfd, bfd_object))
2581 /* If we have already included the element containing this symbol in the
2582 link then we do not need to include it again. Just claim that any symbol
2583 it contains is not a definition, so that our caller will not decide to
2584 (re)include this element. */
2585 if (abfd->archive_pass)
2588 /* Select the appropriate symbol table. */
2589 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2590 hdr = &elf_tdata (abfd)->symtab_hdr;
2592 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2594 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2596 /* The sh_info field of the symtab header tells us where the
2597 external symbols start. We don't care about the local symbols. */
2598 if (elf_bad_symtab (abfd))
2600 extsymcount = symcount;
2605 extsymcount = symcount - hdr->sh_info;
2606 extsymoff = hdr->sh_info;
2609 if (extsymcount == 0)
2612 /* Read in the symbol table. */
2613 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2615 if (isymbuf == NULL)
2618 /* Scan the symbol table looking for SYMDEF. */
2620 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2624 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2629 if (strcmp (name, symdef->name) == 0)
2631 result = is_global_data_symbol_definition (abfd, isym);
2641 /* Add an entry to the .dynamic table. */
2644 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2648 struct elf_link_hash_table *hash_table;
2649 const struct elf_backend_data *bed;
2651 bfd_size_type newsize;
2652 bfd_byte *newcontents;
2653 Elf_Internal_Dyn dyn;
2655 hash_table = elf_hash_table (info);
2656 if (! is_elf_hash_table (hash_table))
2659 bed = get_elf_backend_data (hash_table->dynobj);
2660 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2661 BFD_ASSERT (s != NULL);
2663 newsize = s->size + bed->s->sizeof_dyn;
2664 newcontents = bfd_realloc (s->contents, newsize);
2665 if (newcontents == NULL)
2669 dyn.d_un.d_val = val;
2670 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2673 s->contents = newcontents;
2678 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2679 otherwise just check whether one already exists. Returns -1 on error,
2680 1 if a DT_NEEDED tag already exists, and 0 on success. */
2683 elf_add_dt_needed_tag (struct bfd_link_info *info,
2687 struct elf_link_hash_table *hash_table;
2688 bfd_size_type oldsize;
2689 bfd_size_type strindex;
2691 hash_table = elf_hash_table (info);
2692 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2693 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2694 if (strindex == (bfd_size_type) -1)
2697 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2700 const struct elf_backend_data *bed;
2703 bed = get_elf_backend_data (hash_table->dynobj);
2704 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2705 BFD_ASSERT (sdyn != NULL);
2707 for (extdyn = sdyn->contents;
2708 extdyn < sdyn->contents + sdyn->size;
2709 extdyn += bed->s->sizeof_dyn)
2711 Elf_Internal_Dyn dyn;
2713 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2714 if (dyn.d_tag == DT_NEEDED
2715 && dyn.d_un.d_val == strindex)
2717 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2725 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2729 /* We were just checking for existence of the tag. */
2730 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2735 /* Sort symbol by value and section. */
2737 elf_sort_symbol (const void *arg1, const void *arg2)
2739 const struct elf_link_hash_entry *h1;
2740 const struct elf_link_hash_entry *h2;
2741 bfd_signed_vma vdiff;
2743 h1 = *(const struct elf_link_hash_entry **) arg1;
2744 h2 = *(const struct elf_link_hash_entry **) arg2;
2745 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2747 return vdiff > 0 ? 1 : -1;
2750 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2752 return sdiff > 0 ? 1 : -1;
2757 /* This function is used to adjust offsets into .dynstr for
2758 dynamic symbols. This is called via elf_link_hash_traverse. */
2761 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2763 struct elf_strtab_hash *dynstr = data;
2765 if (h->root.type == bfd_link_hash_warning)
2766 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2768 if (h->dynindx != -1)
2769 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2773 /* Assign string offsets in .dynstr, update all structures referencing
2777 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2779 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2780 struct elf_link_local_dynamic_entry *entry;
2781 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2782 bfd *dynobj = hash_table->dynobj;
2785 const struct elf_backend_data *bed;
2788 _bfd_elf_strtab_finalize (dynstr);
2789 size = _bfd_elf_strtab_size (dynstr);
2791 bed = get_elf_backend_data (dynobj);
2792 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2793 BFD_ASSERT (sdyn != NULL);
2795 /* Update all .dynamic entries referencing .dynstr strings. */
2796 for (extdyn = sdyn->contents;
2797 extdyn < sdyn->contents + sdyn->size;
2798 extdyn += bed->s->sizeof_dyn)
2800 Elf_Internal_Dyn dyn;
2802 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
2806 dyn.d_un.d_val = size;
2814 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2819 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
2822 /* Now update local dynamic symbols. */
2823 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
2824 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2825 entry->isym.st_name);
2827 /* And the rest of dynamic symbols. */
2828 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
2830 /* Adjust version definitions. */
2831 if (elf_tdata (output_bfd)->cverdefs)
2836 Elf_Internal_Verdef def;
2837 Elf_Internal_Verdaux defaux;
2839 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2843 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2845 p += sizeof (Elf_External_Verdef);
2846 for (i = 0; i < def.vd_cnt; ++i)
2848 _bfd_elf_swap_verdaux_in (output_bfd,
2849 (Elf_External_Verdaux *) p, &defaux);
2850 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2852 _bfd_elf_swap_verdaux_out (output_bfd,
2853 &defaux, (Elf_External_Verdaux *) p);
2854 p += sizeof (Elf_External_Verdaux);
2857 while (def.vd_next);
2860 /* Adjust version references. */
2861 if (elf_tdata (output_bfd)->verref)
2866 Elf_Internal_Verneed need;
2867 Elf_Internal_Vernaux needaux;
2869 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2873 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2875 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2876 _bfd_elf_swap_verneed_out (output_bfd, &need,
2877 (Elf_External_Verneed *) p);
2878 p += sizeof (Elf_External_Verneed);
2879 for (i = 0; i < need.vn_cnt; ++i)
2881 _bfd_elf_swap_vernaux_in (output_bfd,
2882 (Elf_External_Vernaux *) p, &needaux);
2883 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2885 _bfd_elf_swap_vernaux_out (output_bfd,
2887 (Elf_External_Vernaux *) p);
2888 p += sizeof (Elf_External_Vernaux);
2891 while (need.vn_next);
2897 /* Add symbols from an ELF object file to the linker hash table. */
2900 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2902 bfd_boolean (*add_symbol_hook)
2903 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
2904 const char **, flagword *, asection **, bfd_vma *);
2905 bfd_boolean (*check_relocs)
2906 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
2907 bfd_boolean (*check_directives)
2908 (bfd *, struct bfd_link_info *);
2909 bfd_boolean collect;
2910 Elf_Internal_Shdr *hdr;
2911 bfd_size_type symcount;
2912 bfd_size_type extsymcount;
2913 bfd_size_type extsymoff;
2914 struct elf_link_hash_entry **sym_hash;
2915 bfd_boolean dynamic;
2916 Elf_External_Versym *extversym = NULL;
2917 Elf_External_Versym *ever;
2918 struct elf_link_hash_entry *weaks;
2919 struct elf_link_hash_entry **nondeflt_vers = NULL;
2920 bfd_size_type nondeflt_vers_cnt = 0;
2921 Elf_Internal_Sym *isymbuf = NULL;
2922 Elf_Internal_Sym *isym;
2923 Elf_Internal_Sym *isymend;
2924 const struct elf_backend_data *bed;
2925 bfd_boolean add_needed;
2926 struct elf_link_hash_table * hash_table;
2929 hash_table = elf_hash_table (info);
2931 bed = get_elf_backend_data (abfd);
2932 add_symbol_hook = bed->elf_add_symbol_hook;
2933 collect = bed->collect;
2935 if ((abfd->flags & DYNAMIC) == 0)
2941 /* You can't use -r against a dynamic object. Also, there's no
2942 hope of using a dynamic object which does not exactly match
2943 the format of the output file. */
2944 if (info->relocatable
2945 || !is_elf_hash_table (hash_table)
2946 || hash_table->root.creator != abfd->xvec)
2948 bfd_set_error (bfd_error_invalid_operation);
2953 /* As a GNU extension, any input sections which are named
2954 .gnu.warning.SYMBOL are treated as warning symbols for the given
2955 symbol. This differs from .gnu.warning sections, which generate
2956 warnings when they are included in an output file. */
2957 if (info->executable)
2961 for (s = abfd->sections; s != NULL; s = s->next)
2965 name = bfd_get_section_name (abfd, s);
2966 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
2970 bfd_size_type prefix_len;
2971 const char * gnu_warning_prefix = _("warning: ");
2973 name += sizeof ".gnu.warning." - 1;
2975 /* If this is a shared object, then look up the symbol
2976 in the hash table. If it is there, and it is already
2977 been defined, then we will not be using the entry
2978 from this shared object, so we don't need to warn.
2979 FIXME: If we see the definition in a regular object
2980 later on, we will warn, but we shouldn't. The only
2981 fix is to keep track of what warnings we are supposed
2982 to emit, and then handle them all at the end of the
2986 struct elf_link_hash_entry *h;
2988 h = elf_link_hash_lookup (hash_table, name,
2989 FALSE, FALSE, TRUE);
2991 /* FIXME: What about bfd_link_hash_common? */
2993 && (h->root.type == bfd_link_hash_defined
2994 || h->root.type == bfd_link_hash_defweak))
2996 /* We don't want to issue this warning. Clobber
2997 the section size so that the warning does not
2998 get copied into the output file. */
3005 prefix_len = strlen (gnu_warning_prefix);
3006 msg = bfd_alloc (abfd, prefix_len + sz + 1);
3010 strcpy (msg, gnu_warning_prefix);
3011 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
3014 msg[prefix_len + sz] = '\0';
3016 if (! (_bfd_generic_link_add_one_symbol
3017 (info, abfd, name, BSF_WARNING, s, 0, msg,
3018 FALSE, collect, NULL)))
3021 if (! info->relocatable)
3023 /* Clobber the section size so that the warning does
3024 not get copied into the output file. */
3034 /* If we are creating a shared library, create all the dynamic
3035 sections immediately. We need to attach them to something,
3036 so we attach them to this BFD, provided it is the right
3037 format. FIXME: If there are no input BFD's of the same
3038 format as the output, we can't make a shared library. */
3040 && is_elf_hash_table (hash_table)
3041 && hash_table->root.creator == abfd->xvec
3042 && ! hash_table->dynamic_sections_created)
3044 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3048 else if (!is_elf_hash_table (hash_table))
3053 const char *soname = NULL;
3054 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3057 /* ld --just-symbols and dynamic objects don't mix very well.
3058 Test for --just-symbols by looking at info set up by
3059 _bfd_elf_link_just_syms. */
3060 if ((s = abfd->sections) != NULL
3061 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3064 /* If this dynamic lib was specified on the command line with
3065 --as-needed in effect, then we don't want to add a DT_NEEDED
3066 tag unless the lib is actually used. Similary for libs brought
3067 in by another lib's DT_NEEDED. When --no-add-needed is used
3068 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3069 any dynamic library in DT_NEEDED tags in the dynamic lib at
3071 add_needed = (elf_dyn_lib_class (abfd)
3072 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3073 | DYN_NO_NEEDED)) == 0;
3075 s = bfd_get_section_by_name (abfd, ".dynamic");
3081 unsigned long shlink;
3083 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3084 goto error_free_dyn;
3086 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3088 goto error_free_dyn;
3089 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3091 for (extdyn = dynbuf;
3092 extdyn < dynbuf + s->size;
3093 extdyn += bed->s->sizeof_dyn)
3095 Elf_Internal_Dyn dyn;
3097 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3098 if (dyn.d_tag == DT_SONAME)
3100 unsigned int tagv = dyn.d_un.d_val;
3101 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3103 goto error_free_dyn;
3105 if (dyn.d_tag == DT_NEEDED)
3107 struct bfd_link_needed_list *n, **pn;
3109 unsigned int tagv = dyn.d_un.d_val;
3111 amt = sizeof (struct bfd_link_needed_list);
3112 n = bfd_alloc (abfd, amt);
3113 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3114 if (n == NULL || fnm == NULL)
3115 goto error_free_dyn;
3116 amt = strlen (fnm) + 1;
3117 anm = bfd_alloc (abfd, amt);
3119 goto error_free_dyn;
3120 memcpy (anm, fnm, amt);
3124 for (pn = & hash_table->needed;
3130 if (dyn.d_tag == DT_RUNPATH)
3132 struct bfd_link_needed_list *n, **pn;
3134 unsigned int tagv = dyn.d_un.d_val;
3136 amt = sizeof (struct bfd_link_needed_list);
3137 n = bfd_alloc (abfd, amt);
3138 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3139 if (n == NULL || fnm == NULL)
3140 goto error_free_dyn;
3141 amt = strlen (fnm) + 1;
3142 anm = bfd_alloc (abfd, amt);
3144 goto error_free_dyn;
3145 memcpy (anm, fnm, amt);
3149 for (pn = & runpath;
3155 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3156 if (!runpath && dyn.d_tag == DT_RPATH)
3158 struct bfd_link_needed_list *n, **pn;
3160 unsigned int tagv = dyn.d_un.d_val;
3162 amt = sizeof (struct bfd_link_needed_list);
3163 n = bfd_alloc (abfd, amt);
3164 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3165 if (n == NULL || fnm == NULL)
3166 goto error_free_dyn;
3167 amt = strlen (fnm) + 1;
3168 anm = bfd_alloc (abfd, amt);
3175 memcpy (anm, fnm, amt);
3190 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3191 frees all more recently bfd_alloc'd blocks as well. */
3197 struct bfd_link_needed_list **pn;
3198 for (pn = & hash_table->runpath;
3205 /* We do not want to include any of the sections in a dynamic
3206 object in the output file. We hack by simply clobbering the
3207 list of sections in the BFD. This could be handled more
3208 cleanly by, say, a new section flag; the existing
3209 SEC_NEVER_LOAD flag is not the one we want, because that one
3210 still implies that the section takes up space in the output
3212 bfd_section_list_clear (abfd);
3214 /* If this is the first dynamic object found in the link, create
3215 the special sections required for dynamic linking. */
3216 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3219 /* Find the name to use in a DT_NEEDED entry that refers to this
3220 object. If the object has a DT_SONAME entry, we use it.
3221 Otherwise, if the generic linker stuck something in
3222 elf_dt_name, we use that. Otherwise, we just use the file
3224 if (soname == NULL || *soname == '\0')
3226 soname = elf_dt_name (abfd);
3227 if (soname == NULL || *soname == '\0')
3228 soname = bfd_get_filename (abfd);
3231 /* Save the SONAME because sometimes the linker emulation code
3232 will need to know it. */
3233 elf_dt_name (abfd) = soname;
3235 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3239 /* If we have already included this dynamic object in the
3240 link, just ignore it. There is no reason to include a
3241 particular dynamic object more than once. */
3246 /* If this is a dynamic object, we always link against the .dynsym
3247 symbol table, not the .symtab symbol table. The dynamic linker
3248 will only see the .dynsym symbol table, so there is no reason to
3249 look at .symtab for a dynamic object. */
3251 if (! dynamic || elf_dynsymtab (abfd) == 0)
3252 hdr = &elf_tdata (abfd)->symtab_hdr;
3254 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3256 symcount = hdr->sh_size / bed->s->sizeof_sym;
3258 /* The sh_info field of the symtab header tells us where the
3259 external symbols start. We don't care about the local symbols at
3261 if (elf_bad_symtab (abfd))
3263 extsymcount = symcount;
3268 extsymcount = symcount - hdr->sh_info;
3269 extsymoff = hdr->sh_info;
3273 if (extsymcount != 0)
3275 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3277 if (isymbuf == NULL)
3280 /* We store a pointer to the hash table entry for each external
3282 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3283 sym_hash = bfd_alloc (abfd, amt);
3284 if (sym_hash == NULL)
3285 goto error_free_sym;
3286 elf_sym_hashes (abfd) = sym_hash;
3291 /* Read in any version definitions. */
3292 if (! _bfd_elf_slurp_version_tables (abfd))
3293 goto error_free_sym;
3295 /* Read in the symbol versions, but don't bother to convert them
3296 to internal format. */
3297 if (elf_dynversym (abfd) != 0)
3299 Elf_Internal_Shdr *versymhdr;
3301 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3302 extversym = bfd_malloc (versymhdr->sh_size);
3303 if (extversym == NULL)
3304 goto error_free_sym;
3305 amt = versymhdr->sh_size;
3306 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3307 || bfd_bread (extversym, amt, abfd) != amt)
3308 goto error_free_vers;
3314 ever = extversym != NULL ? extversym + extsymoff : NULL;
3315 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3317 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3324 struct elf_link_hash_entry *h;
3325 bfd_boolean definition;
3326 bfd_boolean size_change_ok;
3327 bfd_boolean type_change_ok;
3328 bfd_boolean new_weakdef;
3329 bfd_boolean override;
3330 unsigned int old_alignment;
3335 flags = BSF_NO_FLAGS;
3337 value = isym->st_value;
3340 bind = ELF_ST_BIND (isym->st_info);
3341 if (bind == STB_LOCAL)
3343 /* This should be impossible, since ELF requires that all
3344 global symbols follow all local symbols, and that sh_info
3345 point to the first global symbol. Unfortunately, Irix 5
3349 else if (bind == STB_GLOBAL)
3351 if (isym->st_shndx != SHN_UNDEF
3352 && isym->st_shndx != SHN_COMMON)
3355 else if (bind == STB_WEAK)
3359 /* Leave it up to the processor backend. */
3362 if (isym->st_shndx == SHN_UNDEF)
3363 sec = bfd_und_section_ptr;
3364 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3366 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3368 sec = bfd_abs_section_ptr;
3369 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3372 else if (isym->st_shndx == SHN_ABS)
3373 sec = bfd_abs_section_ptr;
3374 else if (isym->st_shndx == SHN_COMMON)
3376 sec = bfd_com_section_ptr;
3377 /* What ELF calls the size we call the value. What ELF
3378 calls the value we call the alignment. */
3379 value = isym->st_size;
3383 /* Leave it up to the processor backend. */
3386 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3389 goto error_free_vers;
3391 if (isym->st_shndx == SHN_COMMON
3392 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3394 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3398 tcomm = bfd_make_section (abfd, ".tcommon");
3400 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
3402 | SEC_LINKER_CREATED
3403 | SEC_THREAD_LOCAL)))
3404 goto error_free_vers;
3408 else if (add_symbol_hook)
3410 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3412 goto error_free_vers;
3414 /* The hook function sets the name to NULL if this symbol
3415 should be skipped for some reason. */
3420 /* Sanity check that all possibilities were handled. */
3423 bfd_set_error (bfd_error_bad_value);
3424 goto error_free_vers;
3427 if (bfd_is_und_section (sec)
3428 || bfd_is_com_section (sec))
3433 size_change_ok = FALSE;
3434 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3438 if (is_elf_hash_table (hash_table))
3440 Elf_Internal_Versym iver;
3441 unsigned int vernum = 0;
3446 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3447 vernum = iver.vs_vers & VERSYM_VERSION;
3449 /* If this is a hidden symbol, or if it is not version
3450 1, we append the version name to the symbol name.
3451 However, we do not modify a non-hidden absolute
3452 symbol, because it might be the version symbol
3453 itself. FIXME: What if it isn't? */
3454 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3455 || (vernum > 1 && ! bfd_is_abs_section (sec)))
3458 size_t namelen, verlen, newlen;
3461 if (isym->st_shndx != SHN_UNDEF)
3463 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
3465 (*_bfd_error_handler)
3466 (_("%s: %s: invalid version %u (max %d)"),
3467 bfd_archive_filename (abfd), name, vernum,
3468 elf_tdata (abfd)->dynverdef_hdr.sh_info);
3469 bfd_set_error (bfd_error_bad_value);
3470 goto error_free_vers;
3472 else if (vernum > 1)
3474 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3480 /* We cannot simply test for the number of
3481 entries in the VERNEED section since the
3482 numbers for the needed versions do not start
3484 Elf_Internal_Verneed *t;
3487 for (t = elf_tdata (abfd)->verref;
3491 Elf_Internal_Vernaux *a;
3493 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3495 if (a->vna_other == vernum)
3497 verstr = a->vna_nodename;
3506 (*_bfd_error_handler)
3507 (_("%s: %s: invalid needed version %d"),
3508 bfd_archive_filename (abfd), name, vernum);
3509 bfd_set_error (bfd_error_bad_value);
3510 goto error_free_vers;
3514 namelen = strlen (name);
3515 verlen = strlen (verstr);
3516 newlen = namelen + verlen + 2;
3517 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3518 && isym->st_shndx != SHN_UNDEF)
3521 newname = bfd_alloc (abfd, newlen);
3522 if (newname == NULL)
3523 goto error_free_vers;
3524 memcpy (newname, name, namelen);
3525 p = newname + namelen;
3527 /* If this is a defined non-hidden version symbol,
3528 we add another @ to the name. This indicates the
3529 default version of the symbol. */
3530 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3531 && isym->st_shndx != SHN_UNDEF)
3533 memcpy (p, verstr, verlen + 1);
3539 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
3540 sym_hash, &skip, &override,
3541 &type_change_ok, &size_change_ok))
3542 goto error_free_vers;
3551 while (h->root.type == bfd_link_hash_indirect
3552 || h->root.type == bfd_link_hash_warning)
3553 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3555 /* Remember the old alignment if this is a common symbol, so
3556 that we don't reduce the alignment later on. We can't
3557 check later, because _bfd_generic_link_add_one_symbol
3558 will set a default for the alignment which we want to
3559 override. We also remember the old bfd where the existing
3560 definition comes from. */
3561 switch (h->root.type)
3566 case bfd_link_hash_defined:
3567 case bfd_link_hash_defweak:
3568 old_bfd = h->root.u.def.section->owner;
3571 case bfd_link_hash_common:
3572 old_bfd = h->root.u.c.p->section->owner;
3573 old_alignment = h->root.u.c.p->alignment_power;
3577 if (elf_tdata (abfd)->verdef != NULL
3581 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3584 if (! (_bfd_generic_link_add_one_symbol
3585 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3586 (struct bfd_link_hash_entry **) sym_hash)))
3587 goto error_free_vers;
3590 while (h->root.type == bfd_link_hash_indirect
3591 || h->root.type == bfd_link_hash_warning)
3592 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3595 new_weakdef = FALSE;
3598 && (flags & BSF_WEAK) != 0
3599 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3600 && is_elf_hash_table (hash_table)
3601 && h->weakdef == NULL)
3603 /* Keep a list of all weak defined non function symbols from
3604 a dynamic object, using the weakdef field. Later in this
3605 function we will set the weakdef field to the correct
3606 value. We only put non-function symbols from dynamic
3607 objects on this list, because that happens to be the only
3608 time we need to know the normal symbol corresponding to a
3609 weak symbol, and the information is time consuming to
3610 figure out. If the weakdef field is not already NULL,
3611 then this symbol was already defined by some previous
3612 dynamic object, and we will be using that previous
3613 definition anyhow. */
3620 /* Set the alignment of a common symbol. */
3621 if (isym->st_shndx == SHN_COMMON
3622 && h->root.type == bfd_link_hash_common)
3626 align = bfd_log2 (isym->st_value);
3627 if (align > old_alignment
3628 /* Permit an alignment power of zero if an alignment of one
3629 is specified and no other alignments have been specified. */
3630 || (isym->st_value == 1 && old_alignment == 0))
3631 h->root.u.c.p->alignment_power = align;
3633 h->root.u.c.p->alignment_power = old_alignment;
3636 if (is_elf_hash_table (hash_table))
3642 /* Check the alignment when a common symbol is involved. This
3643 can change when a common symbol is overridden by a normal
3644 definition or a common symbol is ignored due to the old
3645 normal definition. We need to make sure the maximum
3646 alignment is maintained. */
3647 if ((old_alignment || isym->st_shndx == SHN_COMMON)
3648 && h->root.type != bfd_link_hash_common)
3650 unsigned int common_align;
3651 unsigned int normal_align;
3652 unsigned int symbol_align;
3656 symbol_align = ffs (h->root.u.def.value) - 1;
3657 if (h->root.u.def.section->owner != NULL
3658 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3660 normal_align = h->root.u.def.section->alignment_power;
3661 if (normal_align > symbol_align)
3662 normal_align = symbol_align;
3665 normal_align = symbol_align;
3669 common_align = old_alignment;
3670 common_bfd = old_bfd;
3675 common_align = bfd_log2 (isym->st_value);
3677 normal_bfd = old_bfd;
3680 if (normal_align < common_align)
3681 (*_bfd_error_handler)
3682 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
3685 bfd_archive_filename (normal_bfd),
3687 bfd_archive_filename (common_bfd));
3690 /* Remember the symbol size and type. */
3691 if (isym->st_size != 0
3692 && (definition || h->size == 0))
3694 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3695 (*_bfd_error_handler)
3696 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
3697 name, (unsigned long) h->size,
3698 bfd_archive_filename (old_bfd),
3699 (unsigned long) isym->st_size,
3700 bfd_archive_filename (abfd));
3702 h->size = isym->st_size;
3705 /* If this is a common symbol, then we always want H->SIZE
3706 to be the size of the common symbol. The code just above
3707 won't fix the size if a common symbol becomes larger. We
3708 don't warn about a size change here, because that is
3709 covered by --warn-common. */
3710 if (h->root.type == bfd_link_hash_common)
3711 h->size = h->root.u.c.size;
3713 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3714 && (definition || h->type == STT_NOTYPE))
3716 if (h->type != STT_NOTYPE
3717 && h->type != ELF_ST_TYPE (isym->st_info)
3718 && ! type_change_ok)
3719 (*_bfd_error_handler)
3720 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
3721 name, h->type, ELF_ST_TYPE (isym->st_info),
3722 bfd_archive_filename (abfd));
3724 h->type = ELF_ST_TYPE (isym->st_info);
3727 /* If st_other has a processor-specific meaning, specific
3728 code might be needed here. We never merge the visibility
3729 attribute with the one from a dynamic object. */
3730 if (bed->elf_backend_merge_symbol_attribute)
3731 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3734 if (isym->st_other != 0 && !dynamic)
3736 unsigned char hvis, symvis, other, nvis;
3738 /* Take the balance of OTHER from the definition. */
3739 other = (definition ? isym->st_other : h->other);
3740 other &= ~ ELF_ST_VISIBILITY (-1);
3742 /* Combine visibilities, using the most constraining one. */
3743 hvis = ELF_ST_VISIBILITY (h->other);
3744 symvis = ELF_ST_VISIBILITY (isym->st_other);
3750 nvis = hvis < symvis ? hvis : symvis;
3752 h->other = other | nvis;
3755 /* Set a flag in the hash table entry indicating the type of
3756 reference or definition we just found. Keep a count of
3757 the number of dynamic symbols we find. A dynamic symbol
3758 is one which is referenced or defined by both a regular
3759 object and a shared object. */
3760 old_flags = h->elf_link_hash_flags;
3766 new_flag = ELF_LINK_HASH_REF_REGULAR;
3767 if (bind != STB_WEAK)
3768 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
3771 new_flag = ELF_LINK_HASH_DEF_REGULAR;
3772 if (! info->executable
3773 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
3774 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
3780 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
3782 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
3783 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
3784 | ELF_LINK_HASH_REF_REGULAR)) != 0
3785 || (h->weakdef != NULL
3787 && h->weakdef->dynindx != -1))
3791 h->elf_link_hash_flags |= new_flag;
3793 /* Check to see if we need to add an indirect symbol for
3794 the default name. */
3795 if (definition || h->root.type == bfd_link_hash_common)
3796 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
3797 &sec, &value, &dynsym,
3799 goto error_free_vers;
3801 if (definition && !dynamic)
3803 char *p = strchr (name, ELF_VER_CHR);
3804 if (p != NULL && p[1] != ELF_VER_CHR)
3806 /* Queue non-default versions so that .symver x, x@FOO
3807 aliases can be checked. */
3808 if (! nondeflt_vers)
3810 amt = (isymend - isym + 1)
3811 * sizeof (struct elf_link_hash_entry *);
3812 nondeflt_vers = bfd_malloc (amt);
3814 nondeflt_vers [nondeflt_vers_cnt++] = h;
3818 if (dynsym && h->dynindx == -1)
3820 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3821 goto error_free_vers;
3822 if (h->weakdef != NULL
3824 && h->weakdef->dynindx == -1)
3826 if (! bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
3827 goto error_free_vers;
3830 else if (dynsym && h->dynindx != -1)
3831 /* If the symbol already has a dynamic index, but
3832 visibility says it should not be visible, turn it into
3834 switch (ELF_ST_VISIBILITY (h->other))
3838 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
3846 && (h->elf_link_hash_flags
3847 & ELF_LINK_HASH_REF_REGULAR) != 0)
3850 const char *soname = elf_dt_name (abfd);
3852 /* A symbol from a library loaded via DT_NEEDED of some
3853 other library is referenced by a regular object.
3854 Add a DT_NEEDED entry for it. Issue an error if
3855 --no-add-needed is used. */
3856 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
3858 (*_bfd_error_handler)
3859 (_("%s: invalid DSO for symbol `%s' definition"),
3860 bfd_archive_filename (abfd), name);
3861 bfd_set_error (bfd_error_bad_value);
3862 goto error_free_vers;
3866 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3868 goto error_free_vers;
3870 BFD_ASSERT (ret == 0);
3875 /* Now that all the symbols from this input file are created, handle
3876 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3877 if (nondeflt_vers != NULL)
3879 bfd_size_type cnt, symidx;
3881 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
3883 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
3884 char *shortname, *p;
3886 p = strchr (h->root.root.string, ELF_VER_CHR);
3888 || (h->root.type != bfd_link_hash_defined
3889 && h->root.type != bfd_link_hash_defweak))
3892 amt = p - h->root.root.string;
3893 shortname = bfd_malloc (amt + 1);
3894 memcpy (shortname, h->root.root.string, amt);
3895 shortname[amt] = '\0';
3897 hi = (struct elf_link_hash_entry *)
3898 bfd_link_hash_lookup (&hash_table->root, shortname,
3899 FALSE, FALSE, FALSE);
3901 && hi->root.type == h->root.type
3902 && hi->root.u.def.value == h->root.u.def.value
3903 && hi->root.u.def.section == h->root.u.def.section)
3905 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
3906 hi->root.type = bfd_link_hash_indirect;
3907 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
3908 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
3909 sym_hash = elf_sym_hashes (abfd);
3911 for (symidx = 0; symidx < extsymcount; ++symidx)
3912 if (sym_hash[symidx] == hi)
3914 sym_hash[symidx] = h;
3920 free (nondeflt_vers);
3921 nondeflt_vers = NULL;
3924 if (extversym != NULL)
3930 if (isymbuf != NULL)
3934 /* Now set the weakdefs field correctly for all the weak defined
3935 symbols we found. The only way to do this is to search all the
3936 symbols. Since we only need the information for non functions in
3937 dynamic objects, that's the only time we actually put anything on
3938 the list WEAKS. We need this information so that if a regular
3939 object refers to a symbol defined weakly in a dynamic object, the
3940 real symbol in the dynamic object is also put in the dynamic
3941 symbols; we also must arrange for both symbols to point to the
3942 same memory location. We could handle the general case of symbol
3943 aliasing, but a general symbol alias can only be generated in
3944 assembler code, handling it correctly would be very time
3945 consuming, and other ELF linkers don't handle general aliasing
3949 struct elf_link_hash_entry **hpp;
3950 struct elf_link_hash_entry **hppend;
3951 struct elf_link_hash_entry **sorted_sym_hash;
3952 struct elf_link_hash_entry *h;
3955 /* Since we have to search the whole symbol list for each weak
3956 defined symbol, search time for N weak defined symbols will be
3957 O(N^2). Binary search will cut it down to O(NlogN). */
3958 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3959 sorted_sym_hash = bfd_malloc (amt);
3960 if (sorted_sym_hash == NULL)
3962 sym_hash = sorted_sym_hash;
3963 hpp = elf_sym_hashes (abfd);
3964 hppend = hpp + extsymcount;
3966 for (; hpp < hppend; hpp++)
3970 && h->root.type == bfd_link_hash_defined
3971 && h->type != STT_FUNC)
3979 qsort (sorted_sym_hash, sym_count,
3980 sizeof (struct elf_link_hash_entry *),
3983 while (weaks != NULL)
3985 struct elf_link_hash_entry *hlook;
3992 weaks = hlook->weakdef;
3993 hlook->weakdef = NULL;
3995 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
3996 || hlook->root.type == bfd_link_hash_defweak
3997 || hlook->root.type == bfd_link_hash_common
3998 || hlook->root.type == bfd_link_hash_indirect);
3999 slook = hlook->root.u.def.section;
4000 vlook = hlook->root.u.def.value;
4007 bfd_signed_vma vdiff;
4009 h = sorted_sym_hash [idx];
4010 vdiff = vlook - h->root.u.def.value;
4017 long sdiff = slook->id - h->root.u.def.section->id;
4030 /* We didn't find a value/section match. */
4034 for (i = ilook; i < sym_count; i++)
4036 h = sorted_sym_hash [i];
4038 /* Stop if value or section doesn't match. */
4039 if (h->root.u.def.value != vlook
4040 || h->root.u.def.section != slook)
4042 else if (h != hlook)
4046 /* If the weak definition is in the list of dynamic
4047 symbols, make sure the real definition is put
4049 if (hlook->dynindx != -1 && h->dynindx == -1)
4051 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4055 /* If the real definition is in the list of dynamic
4056 symbols, make sure the weak definition is put
4057 there as well. If we don't do this, then the
4058 dynamic loader might not merge the entries for the
4059 real definition and the weak definition. */
4060 if (h->dynindx != -1 && hlook->dynindx == -1)
4062 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4070 free (sorted_sym_hash);
4073 check_directives = get_elf_backend_data (abfd)->check_directives;
4074 if (check_directives)
4075 check_directives (abfd, info);
4077 /* If this object is the same format as the output object, and it is
4078 not a shared library, then let the backend look through the
4081 This is required to build global offset table entries and to
4082 arrange for dynamic relocs. It is not required for the
4083 particular common case of linking non PIC code, even when linking
4084 against shared libraries, but unfortunately there is no way of
4085 knowing whether an object file has been compiled PIC or not.
4086 Looking through the relocs is not particularly time consuming.
4087 The problem is that we must either (1) keep the relocs in memory,
4088 which causes the linker to require additional runtime memory or
4089 (2) read the relocs twice from the input file, which wastes time.
4090 This would be a good case for using mmap.
4092 I have no idea how to handle linking PIC code into a file of a
4093 different format. It probably can't be done. */
4094 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4096 && is_elf_hash_table (hash_table)
4097 && hash_table->root.creator == abfd->xvec
4098 && check_relocs != NULL)
4102 for (o = abfd->sections; o != NULL; o = o->next)
4104 Elf_Internal_Rela *internal_relocs;
4107 if ((o->flags & SEC_RELOC) == 0
4108 || o->reloc_count == 0
4109 || ((info->strip == strip_all || info->strip == strip_debugger)
4110 && (o->flags & SEC_DEBUGGING) != 0)
4111 || bfd_is_abs_section (o->output_section))
4114 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4116 if (internal_relocs == NULL)
4119 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4121 if (elf_section_data (o)->relocs != internal_relocs)
4122 free (internal_relocs);
4129 /* If this is a non-traditional link, try to optimize the handling
4130 of the .stab/.stabstr sections. */
4132 && ! info->traditional_format
4133 && is_elf_hash_table (hash_table)
4134 && (info->strip != strip_all && info->strip != strip_debugger))
4138 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4139 if (stabstr != NULL)
4141 bfd_size_type string_offset = 0;
4144 for (stab = abfd->sections; stab; stab = stab->next)
4145 if (strncmp (".stab", stab->name, 5) == 0
4146 && (!stab->name[5] ||
4147 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4148 && (stab->flags & SEC_MERGE) == 0
4149 && !bfd_is_abs_section (stab->output_section))
4151 struct bfd_elf_section_data *secdata;
4153 secdata = elf_section_data (stab);
4154 if (! _bfd_link_section_stabs (abfd,
4155 &hash_table->stab_info,
4160 if (secdata->sec_info)
4161 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4166 if (is_elf_hash_table (hash_table))
4168 /* Add this bfd to the loaded list. */
4169 struct elf_link_loaded_list *n;
4171 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4175 n->next = hash_table->loaded;
4176 hash_table->loaded = n;
4182 if (nondeflt_vers != NULL)
4183 free (nondeflt_vers);
4184 if (extversym != NULL)
4187 if (isymbuf != NULL)
4193 /* Add symbols from an ELF archive file to the linker hash table. We
4194 don't use _bfd_generic_link_add_archive_symbols because of a
4195 problem which arises on UnixWare. The UnixWare libc.so is an
4196 archive which includes an entry libc.so.1 which defines a bunch of
4197 symbols. The libc.so archive also includes a number of other
4198 object files, which also define symbols, some of which are the same
4199 as those defined in libc.so.1. Correct linking requires that we
4200 consider each object file in turn, and include it if it defines any
4201 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4202 this; it looks through the list of undefined symbols, and includes
4203 any object file which defines them. When this algorithm is used on
4204 UnixWare, it winds up pulling in libc.so.1 early and defining a
4205 bunch of symbols. This means that some of the other objects in the
4206 archive are not included in the link, which is incorrect since they
4207 precede libc.so.1 in the archive.
4209 Fortunately, ELF archive handling is simpler than that done by
4210 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4211 oddities. In ELF, if we find a symbol in the archive map, and the
4212 symbol is currently undefined, we know that we must pull in that
4215 Unfortunately, we do have to make multiple passes over the symbol
4216 table until nothing further is resolved. */
4219 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4222 bfd_boolean *defined = NULL;
4223 bfd_boolean *included = NULL;
4228 if (! bfd_has_map (abfd))
4230 /* An empty archive is a special case. */
4231 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4233 bfd_set_error (bfd_error_no_armap);
4237 /* Keep track of all symbols we know to be already defined, and all
4238 files we know to be already included. This is to speed up the
4239 second and subsequent passes. */
4240 c = bfd_ardata (abfd)->symdef_count;
4244 amt *= sizeof (bfd_boolean);
4245 defined = bfd_zmalloc (amt);
4246 included = bfd_zmalloc (amt);
4247 if (defined == NULL || included == NULL)
4250 symdefs = bfd_ardata (abfd)->symdefs;
4263 symdefend = symdef + c;
4264 for (i = 0; symdef < symdefend; symdef++, i++)
4266 struct elf_link_hash_entry *h;
4268 struct bfd_link_hash_entry *undefs_tail;
4271 if (defined[i] || included[i])
4273 if (symdef->file_offset == last)
4279 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
4280 FALSE, FALSE, FALSE);
4287 /* If this is a default version (the name contains @@),
4288 look up the symbol again with only one `@' as well
4289 as without the version. The effect is that references
4290 to the symbol with and without the version will be
4291 matched by the default symbol in the archive. */
4293 p = strchr (symdef->name, ELF_VER_CHR);
4294 if (p == NULL || p[1] != ELF_VER_CHR)
4297 /* First check with only one `@'. */
4298 len = strlen (symdef->name);
4299 copy = bfd_alloc (abfd, len);
4302 first = p - symdef->name + 1;
4303 memcpy (copy, symdef->name, first);
4304 memcpy (copy + first, symdef->name + first + 1, len - first);
4306 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4307 FALSE, FALSE, FALSE);
4311 /* We also need to check references to the symbol
4312 without the version. */
4314 copy[first - 1] = '\0';
4315 h = elf_link_hash_lookup (elf_hash_table (info),
4316 copy, FALSE, FALSE, FALSE);
4319 bfd_release (abfd, copy);
4325 if (h->root.type == bfd_link_hash_common)
4327 /* We currently have a common symbol. The archive map contains
4328 a reference to this symbol, so we may want to include it. We
4329 only want to include it however, if this archive element
4330 contains a definition of the symbol, not just another common
4333 Unfortunately some archivers (including GNU ar) will put
4334 declarations of common symbols into their archive maps, as
4335 well as real definitions, so we cannot just go by the archive
4336 map alone. Instead we must read in the element's symbol
4337 table and check that to see what kind of symbol definition
4339 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4342 else if (h->root.type != bfd_link_hash_undefined)
4344 if (h->root.type != bfd_link_hash_undefweak)
4349 /* We need to include this archive member. */
4350 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4351 if (element == NULL)
4354 if (! bfd_check_format (element, bfd_object))
4357 /* Doublecheck that we have not included this object
4358 already--it should be impossible, but there may be
4359 something wrong with the archive. */
4360 if (element->archive_pass != 0)
4362 bfd_set_error (bfd_error_bad_value);
4365 element->archive_pass = 1;
4367 undefs_tail = info->hash->undefs_tail;
4369 if (! (*info->callbacks->add_archive_element) (info, element,
4372 if (! bfd_link_add_symbols (element, info))
4375 /* If there are any new undefined symbols, we need to make
4376 another pass through the archive in order to see whether
4377 they can be defined. FIXME: This isn't perfect, because
4378 common symbols wind up on undefs_tail and because an
4379 undefined symbol which is defined later on in this pass
4380 does not require another pass. This isn't a bug, but it
4381 does make the code less efficient than it could be. */
4382 if (undefs_tail != info->hash->undefs_tail)
4385 /* Look backward to mark all symbols from this object file
4386 which we have already seen in this pass. */
4390 included[mark] = TRUE;
4395 while (symdefs[mark].file_offset == symdef->file_offset);
4397 /* We mark subsequent symbols from this object file as we go
4398 on through the loop. */
4399 last = symdef->file_offset;
4410 if (defined != NULL)
4412 if (included != NULL)
4417 /* Given an ELF BFD, add symbols to the global hash table as
4421 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4423 switch (bfd_get_format (abfd))
4426 return elf_link_add_object_symbols (abfd, info);
4428 return elf_link_add_archive_symbols (abfd, info);
4430 bfd_set_error (bfd_error_wrong_format);
4435 /* This function will be called though elf_link_hash_traverse to store
4436 all hash value of the exported symbols in an array. */
4439 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4441 unsigned long **valuep = data;
4447 if (h->root.type == bfd_link_hash_warning)
4448 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4450 /* Ignore indirect symbols. These are added by the versioning code. */
4451 if (h->dynindx == -1)
4454 name = h->root.root.string;
4455 p = strchr (name, ELF_VER_CHR);
4458 alc = bfd_malloc (p - name + 1);
4459 memcpy (alc, name, p - name);
4460 alc[p - name] = '\0';
4464 /* Compute the hash value. */
4465 ha = bfd_elf_hash (name);
4467 /* Store the found hash value in the array given as the argument. */
4470 /* And store it in the struct so that we can put it in the hash table
4472 h->elf_hash_value = ha;
4480 /* Array used to determine the number of hash table buckets to use
4481 based on the number of symbols there are. If there are fewer than
4482 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4483 fewer than 37 we use 17 buckets, and so forth. We never use more
4484 than 32771 buckets. */
4486 static const size_t elf_buckets[] =
4488 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4492 /* Compute bucket count for hashing table. We do not use a static set
4493 of possible tables sizes anymore. Instead we determine for all
4494 possible reasonable sizes of the table the outcome (i.e., the
4495 number of collisions etc) and choose the best solution. The
4496 weighting functions are not too simple to allow the table to grow
4497 without bounds. Instead one of the weighting factors is the size.
4498 Therefore the result is always a good payoff between few collisions
4499 (= short chain lengths) and table size. */
4501 compute_bucket_count (struct bfd_link_info *info)
4503 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4504 size_t best_size = 0;
4505 unsigned long int *hashcodes;
4506 unsigned long int *hashcodesp;
4507 unsigned long int i;
4510 /* Compute the hash values for all exported symbols. At the same
4511 time store the values in an array so that we could use them for
4514 amt *= sizeof (unsigned long int);
4515 hashcodes = bfd_malloc (amt);
4516 if (hashcodes == NULL)
4518 hashcodesp = hashcodes;
4520 /* Put all hash values in HASHCODES. */
4521 elf_link_hash_traverse (elf_hash_table (info),
4522 elf_collect_hash_codes, &hashcodesp);
4524 /* We have a problem here. The following code to optimize the table
4525 size requires an integer type with more the 32 bits. If
4526 BFD_HOST_U_64_BIT is set we know about such a type. */
4527 #ifdef BFD_HOST_U_64_BIT
4530 unsigned long int nsyms = hashcodesp - hashcodes;
4533 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4534 unsigned long int *counts ;
4535 bfd *dynobj = elf_hash_table (info)->dynobj;
4536 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4538 /* Possible optimization parameters: if we have NSYMS symbols we say
4539 that the hashing table must at least have NSYMS/4 and at most
4541 minsize = nsyms / 4;
4544 best_size = maxsize = nsyms * 2;
4546 /* Create array where we count the collisions in. We must use bfd_malloc
4547 since the size could be large. */
4549 amt *= sizeof (unsigned long int);
4550 counts = bfd_malloc (amt);
4557 /* Compute the "optimal" size for the hash table. The criteria is a
4558 minimal chain length. The minor criteria is (of course) the size
4560 for (i = minsize; i < maxsize; ++i)
4562 /* Walk through the array of hashcodes and count the collisions. */
4563 BFD_HOST_U_64_BIT max;
4564 unsigned long int j;
4565 unsigned long int fact;
4567 memset (counts, '\0', i * sizeof (unsigned long int));
4569 /* Determine how often each hash bucket is used. */
4570 for (j = 0; j < nsyms; ++j)
4571 ++counts[hashcodes[j] % i];
4573 /* For the weight function we need some information about the
4574 pagesize on the target. This is information need not be 100%
4575 accurate. Since this information is not available (so far) we
4576 define it here to a reasonable default value. If it is crucial
4577 to have a better value some day simply define this value. */
4578 # ifndef BFD_TARGET_PAGESIZE
4579 # define BFD_TARGET_PAGESIZE (4096)
4582 /* We in any case need 2 + NSYMS entries for the size values and
4584 max = (2 + nsyms) * (bed->s->arch_size / 8);
4587 /* Variant 1: optimize for short chains. We add the squares
4588 of all the chain lengths (which favors many small chain
4589 over a few long chains). */
4590 for (j = 0; j < i; ++j)
4591 max += counts[j] * counts[j];
4593 /* This adds penalties for the overall size of the table. */
4594 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4597 /* Variant 2: Optimize a lot more for small table. Here we
4598 also add squares of the size but we also add penalties for
4599 empty slots (the +1 term). */
4600 for (j = 0; j < i; ++j)
4601 max += (1 + counts[j]) * (1 + counts[j]);
4603 /* The overall size of the table is considered, but not as
4604 strong as in variant 1, where it is squared. */
4605 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4609 /* Compare with current best results. */
4610 if (max < best_chlen)
4620 #endif /* defined (BFD_HOST_U_64_BIT) */
4622 /* This is the fallback solution if no 64bit type is available or if we
4623 are not supposed to spend much time on optimizations. We select the
4624 bucket count using a fixed set of numbers. */
4625 for (i = 0; elf_buckets[i] != 0; i++)
4627 best_size = elf_buckets[i];
4628 if (dynsymcount < elf_buckets[i + 1])
4633 /* Free the arrays we needed. */
4639 /* Set up the sizes and contents of the ELF dynamic sections. This is
4640 called by the ELF linker emulation before_allocation routine. We
4641 must set the sizes of the sections before the linker sets the
4642 addresses of the various sections. */
4645 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4648 const char *filter_shlib,
4649 const char * const *auxiliary_filters,
4650 struct bfd_link_info *info,
4651 asection **sinterpptr,
4652 struct bfd_elf_version_tree *verdefs)
4654 bfd_size_type soname_indx;
4656 const struct elf_backend_data *bed;
4657 struct elf_assign_sym_version_info asvinfo;
4661 soname_indx = (bfd_size_type) -1;
4663 if (!is_elf_hash_table (info->hash))
4666 elf_tdata (output_bfd)->relro = info->relro;
4667 if (info->execstack)
4668 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4669 else if (info->noexecstack)
4670 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4674 asection *notesec = NULL;
4677 for (inputobj = info->input_bfds;
4679 inputobj = inputobj->link_next)
4683 if (inputobj->flags & DYNAMIC)
4685 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4688 if (s->flags & SEC_CODE)
4697 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4698 if (exec && info->relocatable
4699 && notesec->output_section != bfd_abs_section_ptr)
4700 notesec->output_section->flags |= SEC_CODE;
4704 /* Any syms created from now on start with -1 in
4705 got.refcount/offset and plt.refcount/offset. */
4706 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
4708 /* The backend may have to create some sections regardless of whether
4709 we're dynamic or not. */
4710 bed = get_elf_backend_data (output_bfd);
4711 if (bed->elf_backend_always_size_sections
4712 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4715 dynobj = elf_hash_table (info)->dynobj;
4717 /* If there were no dynamic objects in the link, there is nothing to
4722 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
4725 if (elf_hash_table (info)->dynamic_sections_created)
4727 struct elf_info_failed eif;
4728 struct elf_link_hash_entry *h;
4730 struct bfd_elf_version_tree *t;
4731 struct bfd_elf_version_expr *d;
4732 bfd_boolean all_defined;
4734 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
4735 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
4739 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4741 if (soname_indx == (bfd_size_type) -1
4742 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
4748 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
4750 info->flags |= DF_SYMBOLIC;
4757 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
4759 if (indx == (bfd_size_type) -1
4760 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
4763 if (info->new_dtags)
4765 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
4766 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
4771 if (filter_shlib != NULL)
4775 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4776 filter_shlib, TRUE);
4777 if (indx == (bfd_size_type) -1
4778 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
4782 if (auxiliary_filters != NULL)
4784 const char * const *p;
4786 for (p = auxiliary_filters; *p != NULL; p++)
4790 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4792 if (indx == (bfd_size_type) -1
4793 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
4799 eif.verdefs = verdefs;
4802 /* If we are supposed to export all symbols into the dynamic symbol
4803 table (this is not the normal case), then do so. */
4804 if (info->export_dynamic)
4806 elf_link_hash_traverse (elf_hash_table (info),
4807 _bfd_elf_export_symbol,
4813 /* Make all global versions with definition. */
4814 for (t = verdefs; t != NULL; t = t->next)
4815 for (d = t->globals.list; d != NULL; d = d->next)
4816 if (!d->symver && d->symbol)
4818 const char *verstr, *name;
4819 size_t namelen, verlen, newlen;
4821 struct elf_link_hash_entry *newh;
4824 namelen = strlen (name);
4826 verlen = strlen (verstr);
4827 newlen = namelen + verlen + 3;
4829 newname = bfd_malloc (newlen);
4830 if (newname == NULL)
4832 memcpy (newname, name, namelen);
4834 /* Check the hidden versioned definition. */
4835 p = newname + namelen;
4837 memcpy (p, verstr, verlen + 1);
4838 newh = elf_link_hash_lookup (elf_hash_table (info),
4839 newname, FALSE, FALSE,
4842 || (newh->root.type != bfd_link_hash_defined
4843 && newh->root.type != bfd_link_hash_defweak))
4845 /* Check the default versioned definition. */
4847 memcpy (p, verstr, verlen + 1);
4848 newh = elf_link_hash_lookup (elf_hash_table (info),
4849 newname, FALSE, FALSE,
4854 /* Mark this version if there is a definition and it is
4855 not defined in a shared object. */
4857 && ((newh->elf_link_hash_flags
4858 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
4859 && (newh->root.type == bfd_link_hash_defined
4860 || newh->root.type == bfd_link_hash_defweak))
4864 /* Attach all the symbols to their version information. */
4865 asvinfo.output_bfd = output_bfd;
4866 asvinfo.info = info;
4867 asvinfo.verdefs = verdefs;
4868 asvinfo.failed = FALSE;
4870 elf_link_hash_traverse (elf_hash_table (info),
4871 _bfd_elf_link_assign_sym_version,
4876 if (!info->allow_undefined_version)
4878 /* Check if all global versions have a definition. */
4880 for (t = verdefs; t != NULL; t = t->next)
4881 for (d = t->globals.list; d != NULL; d = d->next)
4882 if (!d->symver && !d->script)
4884 (*_bfd_error_handler)
4885 (_("%s: undefined version: %s"),
4886 d->pattern, t->name);
4887 all_defined = FALSE;
4892 bfd_set_error (bfd_error_bad_value);
4897 /* Find all symbols which were defined in a dynamic object and make
4898 the backend pick a reasonable value for them. */
4899 elf_link_hash_traverse (elf_hash_table (info),
4900 _bfd_elf_adjust_dynamic_symbol,
4905 /* Add some entries to the .dynamic section. We fill in some of the
4906 values later, in elf_bfd_final_link, but we must add the entries
4907 now so that we know the final size of the .dynamic section. */
4909 /* If there are initialization and/or finalization functions to
4910 call then add the corresponding DT_INIT/DT_FINI entries. */
4911 h = (info->init_function
4912 ? elf_link_hash_lookup (elf_hash_table (info),
4913 info->init_function, FALSE,
4917 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
4918 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
4920 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
4923 h = (info->fini_function
4924 ? elf_link_hash_lookup (elf_hash_table (info),
4925 info->fini_function, FALSE,
4929 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
4930 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
4932 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
4936 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
4938 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4939 if (! info->executable)
4944 for (sub = info->input_bfds; sub != NULL;
4945 sub = sub->link_next)
4946 for (o = sub->sections; o != NULL; o = o->next)
4947 if (elf_section_data (o)->this_hdr.sh_type
4948 == SHT_PREINIT_ARRAY)
4950 (*_bfd_error_handler)
4951 (_("%s: .preinit_array section is not allowed in DSO"),
4952 bfd_archive_filename (sub));
4956 bfd_set_error (bfd_error_nonrepresentable_section);
4960 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
4961 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
4964 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
4966 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
4967 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
4970 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
4972 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
4973 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
4977 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
4978 /* If .dynstr is excluded from the link, we don't want any of
4979 these tags. Strictly, we should be checking each section
4980 individually; This quick check covers for the case where
4981 someone does a /DISCARD/ : { *(*) }. */
4982 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
4984 bfd_size_type strsize;
4986 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
4987 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
4988 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
4989 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
4990 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
4991 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
4992 bed->s->sizeof_sym))
4997 /* The backend must work out the sizes of all the other dynamic
4999 if (bed->elf_backend_size_dynamic_sections
5000 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5003 if (elf_hash_table (info)->dynamic_sections_created)
5005 bfd_size_type dynsymcount;
5007 size_t bucketcount = 0;
5008 size_t hash_entry_size;
5009 unsigned int dtagcount;
5011 /* Set up the version definition section. */
5012 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5013 BFD_ASSERT (s != NULL);
5015 /* We may have created additional version definitions if we are
5016 just linking a regular application. */
5017 verdefs = asvinfo.verdefs;
5019 /* Skip anonymous version tag. */
5020 if (verdefs != NULL && verdefs->vernum == 0)
5021 verdefs = verdefs->next;
5023 if (verdefs == NULL)
5024 _bfd_strip_section_from_output (info, s);
5029 struct bfd_elf_version_tree *t;
5031 Elf_Internal_Verdef def;
5032 Elf_Internal_Verdaux defaux;
5037 /* Make space for the base version. */
5038 size += sizeof (Elf_External_Verdef);
5039 size += sizeof (Elf_External_Verdaux);
5042 for (t = verdefs; t != NULL; t = t->next)
5044 struct bfd_elf_version_deps *n;
5046 size += sizeof (Elf_External_Verdef);
5047 size += sizeof (Elf_External_Verdaux);
5050 for (n = t->deps; n != NULL; n = n->next)
5051 size += sizeof (Elf_External_Verdaux);
5055 s->contents = bfd_alloc (output_bfd, s->size);
5056 if (s->contents == NULL && s->size != 0)
5059 /* Fill in the version definition section. */
5063 def.vd_version = VER_DEF_CURRENT;
5064 def.vd_flags = VER_FLG_BASE;
5067 def.vd_aux = sizeof (Elf_External_Verdef);
5068 def.vd_next = (sizeof (Elf_External_Verdef)
5069 + sizeof (Elf_External_Verdaux));
5071 if (soname_indx != (bfd_size_type) -1)
5073 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5075 def.vd_hash = bfd_elf_hash (soname);
5076 defaux.vda_name = soname_indx;
5083 name = basename (output_bfd->filename);
5084 def.vd_hash = bfd_elf_hash (name);
5085 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5087 if (indx == (bfd_size_type) -1)
5089 defaux.vda_name = indx;
5091 defaux.vda_next = 0;
5093 _bfd_elf_swap_verdef_out (output_bfd, &def,
5094 (Elf_External_Verdef *) p);
5095 p += sizeof (Elf_External_Verdef);
5096 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5097 (Elf_External_Verdaux *) p);
5098 p += sizeof (Elf_External_Verdaux);
5100 for (t = verdefs; t != NULL; t = t->next)
5103 struct bfd_elf_version_deps *n;
5104 struct elf_link_hash_entry *h;
5105 struct bfd_link_hash_entry *bh;
5108 for (n = t->deps; n != NULL; n = n->next)
5111 /* Add a symbol representing this version. */
5113 if (! (_bfd_generic_link_add_one_symbol
5114 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5116 get_elf_backend_data (dynobj)->collect, &bh)))
5118 h = (struct elf_link_hash_entry *) bh;
5119 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
5120 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5121 h->type = STT_OBJECT;
5122 h->verinfo.vertree = t;
5124 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5127 def.vd_version = VER_DEF_CURRENT;
5129 if (t->globals.list == NULL
5130 && t->locals.list == NULL
5132 def.vd_flags |= VER_FLG_WEAK;
5133 def.vd_ndx = t->vernum + 1;
5134 def.vd_cnt = cdeps + 1;
5135 def.vd_hash = bfd_elf_hash (t->name);
5136 def.vd_aux = sizeof (Elf_External_Verdef);
5138 if (t->next != NULL)
5139 def.vd_next = (sizeof (Elf_External_Verdef)
5140 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5142 _bfd_elf_swap_verdef_out (output_bfd, &def,
5143 (Elf_External_Verdef *) p);
5144 p += sizeof (Elf_External_Verdef);
5146 defaux.vda_name = h->dynstr_index;
5147 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5149 defaux.vda_next = 0;
5150 if (t->deps != NULL)
5151 defaux.vda_next = sizeof (Elf_External_Verdaux);
5152 t->name_indx = defaux.vda_name;
5154 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5155 (Elf_External_Verdaux *) p);
5156 p += sizeof (Elf_External_Verdaux);
5158 for (n = t->deps; n != NULL; n = n->next)
5160 if (n->version_needed == NULL)
5162 /* This can happen if there was an error in the
5164 defaux.vda_name = 0;
5168 defaux.vda_name = n->version_needed->name_indx;
5169 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5172 if (n->next == NULL)
5173 defaux.vda_next = 0;
5175 defaux.vda_next = sizeof (Elf_External_Verdaux);
5177 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5178 (Elf_External_Verdaux *) p);
5179 p += sizeof (Elf_External_Verdaux);
5183 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5184 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5187 elf_tdata (output_bfd)->cverdefs = cdefs;
5190 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5192 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5195 else if (info->flags & DF_BIND_NOW)
5197 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5203 if (info->executable)
5204 info->flags_1 &= ~ (DF_1_INITFIRST
5207 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5211 /* Work out the size of the version reference section. */
5213 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5214 BFD_ASSERT (s != NULL);
5216 struct elf_find_verdep_info sinfo;
5218 sinfo.output_bfd = output_bfd;
5220 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5221 if (sinfo.vers == 0)
5223 sinfo.failed = FALSE;
5225 elf_link_hash_traverse (elf_hash_table (info),
5226 _bfd_elf_link_find_version_dependencies,
5229 if (elf_tdata (output_bfd)->verref == NULL)
5230 _bfd_strip_section_from_output (info, s);
5233 Elf_Internal_Verneed *t;
5238 /* Build the version definition section. */
5241 for (t = elf_tdata (output_bfd)->verref;
5245 Elf_Internal_Vernaux *a;
5247 size += sizeof (Elf_External_Verneed);
5249 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5250 size += sizeof (Elf_External_Vernaux);
5254 s->contents = bfd_alloc (output_bfd, s->size);
5255 if (s->contents == NULL)
5259 for (t = elf_tdata (output_bfd)->verref;
5264 Elf_Internal_Vernaux *a;
5268 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5271 t->vn_version = VER_NEED_CURRENT;
5273 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5274 elf_dt_name (t->vn_bfd) != NULL
5275 ? elf_dt_name (t->vn_bfd)
5276 : basename (t->vn_bfd->filename),
5278 if (indx == (bfd_size_type) -1)
5281 t->vn_aux = sizeof (Elf_External_Verneed);
5282 if (t->vn_nextref == NULL)
5285 t->vn_next = (sizeof (Elf_External_Verneed)
5286 + caux * sizeof (Elf_External_Vernaux));
5288 _bfd_elf_swap_verneed_out (output_bfd, t,
5289 (Elf_External_Verneed *) p);
5290 p += sizeof (Elf_External_Verneed);
5292 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5294 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5295 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5296 a->vna_nodename, FALSE);
5297 if (indx == (bfd_size_type) -1)
5300 if (a->vna_nextptr == NULL)
5303 a->vna_next = sizeof (Elf_External_Vernaux);
5305 _bfd_elf_swap_vernaux_out (output_bfd, a,
5306 (Elf_External_Vernaux *) p);
5307 p += sizeof (Elf_External_Vernaux);
5311 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5312 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5315 elf_tdata (output_bfd)->cverrefs = crefs;
5319 /* Assign dynsym indicies. In a shared library we generate a
5320 section symbol for each output section, which come first.
5321 Next come all of the back-end allocated local dynamic syms,
5322 followed by the rest of the global symbols. */
5324 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5326 /* Work out the size of the symbol version section. */
5327 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5328 BFD_ASSERT (s != NULL);
5329 if (dynsymcount == 0
5330 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
5332 _bfd_strip_section_from_output (info, s);
5333 /* The DYNSYMCOUNT might have changed if we were going to
5334 output a dynamic symbol table entry for S. */
5335 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5339 s->size = dynsymcount * sizeof (Elf_External_Versym);
5340 s->contents = bfd_zalloc (output_bfd, s->size);
5341 if (s->contents == NULL)
5344 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5348 /* Set the size of the .dynsym and .hash sections. We counted
5349 the number of dynamic symbols in elf_link_add_object_symbols.
5350 We will build the contents of .dynsym and .hash when we build
5351 the final symbol table, because until then we do not know the
5352 correct value to give the symbols. We built the .dynstr
5353 section as we went along in elf_link_add_object_symbols. */
5354 s = bfd_get_section_by_name (dynobj, ".dynsym");
5355 BFD_ASSERT (s != NULL);
5356 s->size = dynsymcount * bed->s->sizeof_sym;
5357 s->contents = bfd_alloc (output_bfd, s->size);
5358 if (s->contents == NULL && s->size != 0)
5361 if (dynsymcount != 0)
5363 Elf_Internal_Sym isym;
5365 /* The first entry in .dynsym is a dummy symbol. */
5372 bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0);
5375 /* Compute the size of the hashing table. As a side effect this
5376 computes the hash values for all the names we export. */
5377 bucketcount = compute_bucket_count (info);
5379 s = bfd_get_section_by_name (dynobj, ".hash");
5380 BFD_ASSERT (s != NULL);
5381 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5382 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5383 s->contents = bfd_zalloc (output_bfd, s->size);
5384 if (s->contents == NULL)
5387 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5388 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5389 s->contents + hash_entry_size);
5391 elf_hash_table (info)->bucketcount = bucketcount;
5393 s = bfd_get_section_by_name (dynobj, ".dynstr");
5394 BFD_ASSERT (s != NULL);
5396 elf_finalize_dynstr (output_bfd, info);
5398 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5400 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5401 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5408 /* Final phase of ELF linker. */
5410 /* A structure we use to avoid passing large numbers of arguments. */
5412 struct elf_final_link_info
5414 /* General link information. */
5415 struct bfd_link_info *info;
5418 /* Symbol string table. */
5419 struct bfd_strtab_hash *symstrtab;
5420 /* .dynsym section. */
5421 asection *dynsym_sec;
5422 /* .hash section. */
5424 /* symbol version section (.gnu.version). */
5425 asection *symver_sec;
5426 /* Buffer large enough to hold contents of any section. */
5428 /* Buffer large enough to hold external relocs of any section. */
5429 void *external_relocs;
5430 /* Buffer large enough to hold internal relocs of any section. */
5431 Elf_Internal_Rela *internal_relocs;
5432 /* Buffer large enough to hold external local symbols of any input
5434 bfd_byte *external_syms;
5435 /* And a buffer for symbol section indices. */
5436 Elf_External_Sym_Shndx *locsym_shndx;
5437 /* Buffer large enough to hold internal local symbols of any input
5439 Elf_Internal_Sym *internal_syms;
5440 /* Array large enough to hold a symbol index for each local symbol
5441 of any input BFD. */
5443 /* Array large enough to hold a section pointer for each local
5444 symbol of any input BFD. */
5445 asection **sections;
5446 /* Buffer to hold swapped out symbols. */
5448 /* And one for symbol section indices. */
5449 Elf_External_Sym_Shndx *symshndxbuf;
5450 /* Number of swapped out symbols in buffer. */
5451 size_t symbuf_count;
5452 /* Number of symbols which fit in symbuf. */
5454 /* And same for symshndxbuf. */
5455 size_t shndxbuf_size;
5458 /* This struct is used to pass information to elf_link_output_extsym. */
5460 struct elf_outext_info
5463 bfd_boolean localsyms;
5464 struct elf_final_link_info *finfo;
5467 /* When performing a relocatable link, the input relocations are
5468 preserved. But, if they reference global symbols, the indices
5469 referenced must be updated. Update all the relocations in
5470 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5473 elf_link_adjust_relocs (bfd *abfd,
5474 Elf_Internal_Shdr *rel_hdr,
5476 struct elf_link_hash_entry **rel_hash)
5479 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5481 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5482 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5483 bfd_vma r_type_mask;
5486 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5488 swap_in = bed->s->swap_reloc_in;
5489 swap_out = bed->s->swap_reloc_out;
5491 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5493 swap_in = bed->s->swap_reloca_in;
5494 swap_out = bed->s->swap_reloca_out;
5499 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5502 if (bed->s->arch_size == 32)
5509 r_type_mask = 0xffffffff;
5513 erela = rel_hdr->contents;
5514 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5516 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5519 if (*rel_hash == NULL)
5522 BFD_ASSERT ((*rel_hash)->indx >= 0);
5524 (*swap_in) (abfd, erela, irela);
5525 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5526 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5527 | (irela[j].r_info & r_type_mask));
5528 (*swap_out) (abfd, irela, erela);
5532 struct elf_link_sort_rela
5538 enum elf_reloc_type_class type;
5539 /* We use this as an array of size int_rels_per_ext_rel. */
5540 Elf_Internal_Rela rela[1];
5544 elf_link_sort_cmp1 (const void *A, const void *B)
5546 const struct elf_link_sort_rela *a = A;
5547 const struct elf_link_sort_rela *b = B;
5548 int relativea, relativeb;
5550 relativea = a->type == reloc_class_relative;
5551 relativeb = b->type == reloc_class_relative;
5553 if (relativea < relativeb)
5555 if (relativea > relativeb)
5557 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5559 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5561 if (a->rela->r_offset < b->rela->r_offset)
5563 if (a->rela->r_offset > b->rela->r_offset)
5569 elf_link_sort_cmp2 (const void *A, const void *B)
5571 const struct elf_link_sort_rela *a = A;
5572 const struct elf_link_sort_rela *b = B;
5575 if (a->u.offset < b->u.offset)
5577 if (a->u.offset > b->u.offset)
5579 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5580 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5585 if (a->rela->r_offset < b->rela->r_offset)
5587 if (a->rela->r_offset > b->rela->r_offset)
5593 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5596 bfd_size_type count, size;
5597 size_t i, ret, sort_elt, ext_size;
5598 bfd_byte *sort, *s_non_relative, *p;
5599 struct elf_link_sort_rela *sq;
5600 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5601 int i2e = bed->s->int_rels_per_ext_rel;
5602 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5603 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5604 struct bfd_link_order *lo;
5607 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5608 if (reldyn == NULL || reldyn->size == 0)
5610 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5611 if (reldyn == NULL || reldyn->size == 0)
5613 ext_size = bed->s->sizeof_rel;
5614 swap_in = bed->s->swap_reloc_in;
5615 swap_out = bed->s->swap_reloc_out;
5619 ext_size = bed->s->sizeof_rela;
5620 swap_in = bed->s->swap_reloca_in;
5621 swap_out = bed->s->swap_reloca_out;
5623 count = reldyn->size / ext_size;
5626 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5627 if (lo->type == bfd_indirect_link_order)
5629 asection *o = lo->u.indirect.section;
5633 if (size != reldyn->size)
5636 sort_elt = (sizeof (struct elf_link_sort_rela)
5637 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5638 sort = bfd_zmalloc (sort_elt * count);
5641 (*info->callbacks->warning)
5642 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5646 if (bed->s->arch_size == 32)
5647 r_sym_mask = ~(bfd_vma) 0xff;
5649 r_sym_mask = ~(bfd_vma) 0xffffffff;
5651 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5652 if (lo->type == bfd_indirect_link_order)
5654 bfd_byte *erel, *erelend;
5655 asection *o = lo->u.indirect.section;
5658 erelend = o->contents + o->size;
5659 p = sort + o->output_offset / ext_size * sort_elt;
5660 while (erel < erelend)
5662 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5663 (*swap_in) (abfd, erel, s->rela);
5664 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
5665 s->u.sym_mask = r_sym_mask;
5671 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
5673 for (i = 0, p = sort; i < count; i++, p += sort_elt)
5675 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5676 if (s->type != reloc_class_relative)
5682 sq = (struct elf_link_sort_rela *) s_non_relative;
5683 for (; i < count; i++, p += sort_elt)
5685 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
5686 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
5688 sp->u.offset = sq->rela->r_offset;
5691 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
5693 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5694 if (lo->type == bfd_indirect_link_order)
5696 bfd_byte *erel, *erelend;
5697 asection *o = lo->u.indirect.section;
5700 erelend = o->contents + o->size;
5701 p = sort + o->output_offset / ext_size * sort_elt;
5702 while (erel < erelend)
5704 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5705 (*swap_out) (abfd, s->rela, erel);
5716 /* Flush the output symbols to the file. */
5719 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
5720 const struct elf_backend_data *bed)
5722 if (finfo->symbuf_count > 0)
5724 Elf_Internal_Shdr *hdr;
5728 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5729 pos = hdr->sh_offset + hdr->sh_size;
5730 amt = finfo->symbuf_count * bed->s->sizeof_sym;
5731 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5732 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
5735 hdr->sh_size += amt;
5736 finfo->symbuf_count = 0;
5742 /* Add a symbol to the output symbol table. */
5745 elf_link_output_sym (struct elf_final_link_info *finfo,
5747 Elf_Internal_Sym *elfsym,
5748 asection *input_sec,
5749 struct elf_link_hash_entry *h)
5752 Elf_External_Sym_Shndx *destshndx;
5753 bfd_boolean (*output_symbol_hook)
5754 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
5755 struct elf_link_hash_entry *);
5756 const struct elf_backend_data *bed;
5758 bed = get_elf_backend_data (finfo->output_bfd);
5759 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
5760 if (output_symbol_hook != NULL)
5762 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
5766 if (name == NULL || *name == '\0')
5767 elfsym->st_name = 0;
5768 else if (input_sec->flags & SEC_EXCLUDE)
5769 elfsym->st_name = 0;
5772 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5774 if (elfsym->st_name == (unsigned long) -1)
5778 if (finfo->symbuf_count >= finfo->symbuf_size)
5780 if (! elf_link_flush_output_syms (finfo, bed))
5784 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
5785 destshndx = finfo->symshndxbuf;
5786 if (destshndx != NULL)
5788 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
5792 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
5793 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
5794 if (destshndx == NULL)
5796 memset ((char *) destshndx + amt, 0, amt);
5797 finfo->shndxbuf_size *= 2;
5799 destshndx += bfd_get_symcount (finfo->output_bfd);
5802 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
5803 finfo->symbuf_count += 1;
5804 bfd_get_symcount (finfo->output_bfd) += 1;
5809 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5810 allowing an unsatisfied unversioned symbol in the DSO to match a
5811 versioned symbol that would normally require an explicit version.
5812 We also handle the case that a DSO references a hidden symbol
5813 which may be satisfied by a versioned symbol in another DSO. */
5816 elf_link_check_versioned_symbol (struct bfd_link_info *info,
5817 const struct elf_backend_data *bed,
5818 struct elf_link_hash_entry *h)
5821 struct elf_link_loaded_list *loaded;
5823 if (!is_elf_hash_table (info->hash))
5826 switch (h->root.type)
5832 case bfd_link_hash_undefined:
5833 case bfd_link_hash_undefweak:
5834 abfd = h->root.u.undef.abfd;
5835 if ((abfd->flags & DYNAMIC) == 0
5836 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
5840 case bfd_link_hash_defined:
5841 case bfd_link_hash_defweak:
5842 abfd = h->root.u.def.section->owner;
5845 case bfd_link_hash_common:
5846 abfd = h->root.u.c.p->section->owner;
5849 BFD_ASSERT (abfd != NULL);
5851 for (loaded = elf_hash_table (info)->loaded;
5853 loaded = loaded->next)
5856 Elf_Internal_Shdr *hdr;
5857 bfd_size_type symcount;
5858 bfd_size_type extsymcount;
5859 bfd_size_type extsymoff;
5860 Elf_Internal_Shdr *versymhdr;
5861 Elf_Internal_Sym *isym;
5862 Elf_Internal_Sym *isymend;
5863 Elf_Internal_Sym *isymbuf;
5864 Elf_External_Versym *ever;
5865 Elf_External_Versym *extversym;
5867 input = loaded->abfd;
5869 /* We check each DSO for a possible hidden versioned definition. */
5871 || (input->flags & DYNAMIC) == 0
5872 || elf_dynversym (input) == 0)
5875 hdr = &elf_tdata (input)->dynsymtab_hdr;
5877 symcount = hdr->sh_size / bed->s->sizeof_sym;
5878 if (elf_bad_symtab (input))
5880 extsymcount = symcount;
5885 extsymcount = symcount - hdr->sh_info;
5886 extsymoff = hdr->sh_info;
5889 if (extsymcount == 0)
5892 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
5894 if (isymbuf == NULL)
5897 /* Read in any version definitions. */
5898 versymhdr = &elf_tdata (input)->dynversym_hdr;
5899 extversym = bfd_malloc (versymhdr->sh_size);
5900 if (extversym == NULL)
5903 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
5904 || (bfd_bread (extversym, versymhdr->sh_size, input)
5905 != versymhdr->sh_size))
5913 ever = extversym + extsymoff;
5914 isymend = isymbuf + extsymcount;
5915 for (isym = isymbuf; isym < isymend; isym++, ever++)
5918 Elf_Internal_Versym iver;
5919 unsigned short version_index;
5921 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
5922 || isym->st_shndx == SHN_UNDEF)
5925 name = bfd_elf_string_from_elf_section (input,
5928 if (strcmp (name, h->root.root.string) != 0)
5931 _bfd_elf_swap_versym_in (input, ever, &iver);
5933 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
5935 /* If we have a non-hidden versioned sym, then it should
5936 have provided a definition for the undefined sym. */
5940 version_index = iver.vs_vers & VERSYM_VERSION;
5941 if (version_index == 1 || version_index == 2)
5943 /* This is the base or first version. We can use it. */
5957 /* Add an external symbol to the symbol table. This is called from
5958 the hash table traversal routine. When generating a shared object,
5959 we go through the symbol table twice. The first time we output
5960 anything that might have been forced to local scope in a version
5961 script. The second time we output the symbols that are still
5965 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
5967 struct elf_outext_info *eoinfo = data;
5968 struct elf_final_link_info *finfo = eoinfo->finfo;
5970 Elf_Internal_Sym sym;
5971 asection *input_sec;
5972 const struct elf_backend_data *bed;
5974 if (h->root.type == bfd_link_hash_warning)
5976 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5977 if (h->root.type == bfd_link_hash_new)
5981 /* Decide whether to output this symbol in this pass. */
5982 if (eoinfo->localsyms)
5984 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5989 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5993 bed = get_elf_backend_data (finfo->output_bfd);
5995 /* If we have an undefined symbol reference here then it must have
5996 come from a shared library that is being linked in. (Undefined
5997 references in regular files have already been handled). If we
5998 are reporting errors for this situation then do so now. */
5999 if (h->root.type == bfd_link_hash_undefined
6000 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
6001 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
6002 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6003 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6005 if (! ((*finfo->info->callbacks->undefined_symbol)
6006 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6007 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6009 eoinfo->failed = TRUE;
6014 /* We should also warn if a forced local symbol is referenced from
6015 shared libraries. */
6016 if (! finfo->info->relocatable
6017 && (! finfo->info->shared)
6018 && (h->elf_link_hash_flags
6019 & (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_DYNAMIC_DEF | ELF_LINK_DYNAMIC_WEAK))
6020 == (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC)
6021 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6023 (*_bfd_error_handler)
6024 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
6025 bfd_get_filename (finfo->output_bfd),
6026 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6028 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6029 ? "hidden" : "local",
6030 h->root.root.string,
6031 bfd_archive_filename (h->root.u.def.section->owner));
6032 eoinfo->failed = TRUE;
6036 /* We don't want to output symbols that have never been mentioned by
6037 a regular file, or that we have been told to strip. However, if
6038 h->indx is set to -2, the symbol is used by a reloc and we must
6042 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6043 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
6044 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
6045 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
6047 else if (finfo->info->strip == strip_all)
6049 else if (finfo->info->strip == strip_some
6050 && bfd_hash_lookup (finfo->info->keep_hash,
6051 h->root.root.string, FALSE, FALSE) == NULL)
6053 else if (finfo->info->strip_discarded
6054 && (h->root.type == bfd_link_hash_defined
6055 || h->root.type == bfd_link_hash_defweak)
6056 && elf_discarded_section (h->root.u.def.section))
6061 /* If we're stripping it, and it's not a dynamic symbol, there's
6062 nothing else to do unless it is a forced local symbol. */
6065 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6069 sym.st_size = h->size;
6070 sym.st_other = h->other;
6071 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6072 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6073 else if (h->root.type == bfd_link_hash_undefweak
6074 || h->root.type == bfd_link_hash_defweak)
6075 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6077 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6079 switch (h->root.type)
6082 case bfd_link_hash_new:
6083 case bfd_link_hash_warning:
6087 case bfd_link_hash_undefined:
6088 case bfd_link_hash_undefweak:
6089 input_sec = bfd_und_section_ptr;
6090 sym.st_shndx = SHN_UNDEF;
6093 case bfd_link_hash_defined:
6094 case bfd_link_hash_defweak:
6096 input_sec = h->root.u.def.section;
6097 if (input_sec->output_section != NULL)
6100 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6101 input_sec->output_section);
6102 if (sym.st_shndx == SHN_BAD)
6104 char *sec_name = bfd_get_section_ident (input_sec);
6105 (*_bfd_error_handler)
6106 (_("%s: could not find output section %s for input section %s"),
6107 bfd_get_filename (finfo->output_bfd),
6108 input_sec->output_section->name,
6109 sec_name ? sec_name : input_sec->name);
6112 eoinfo->failed = TRUE;
6116 /* ELF symbols in relocatable files are section relative,
6117 but in nonrelocatable files they are virtual
6119 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6120 if (! finfo->info->relocatable)
6122 sym.st_value += input_sec->output_section->vma;
6123 if (h->type == STT_TLS)
6125 /* STT_TLS symbols are relative to PT_TLS segment
6127 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6128 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6134 BFD_ASSERT (input_sec->owner == NULL
6135 || (input_sec->owner->flags & DYNAMIC) != 0);
6136 sym.st_shndx = SHN_UNDEF;
6137 input_sec = bfd_und_section_ptr;
6142 case bfd_link_hash_common:
6143 input_sec = h->root.u.c.p->section;
6144 sym.st_shndx = SHN_COMMON;
6145 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6148 case bfd_link_hash_indirect:
6149 /* These symbols are created by symbol versioning. They point
6150 to the decorated version of the name. For example, if the
6151 symbol foo@@GNU_1.2 is the default, which should be used when
6152 foo is used with no version, then we add an indirect symbol
6153 foo which points to foo@@GNU_1.2. We ignore these symbols,
6154 since the indirected symbol is already in the hash table. */
6158 /* Give the processor backend a chance to tweak the symbol value,
6159 and also to finish up anything that needs to be done for this
6160 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6161 forced local syms when non-shared is due to a historical quirk. */
6162 if ((h->dynindx != -1
6163 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6164 && ((finfo->info->shared
6165 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6166 || h->root.type != bfd_link_hash_undefweak))
6167 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6168 && elf_hash_table (finfo->info)->dynamic_sections_created)
6170 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6171 (finfo->output_bfd, finfo->info, h, &sym)))
6173 eoinfo->failed = TRUE;
6178 /* If we are marking the symbol as undefined, and there are no
6179 non-weak references to this symbol from a regular object, then
6180 mark the symbol as weak undefined; if there are non-weak
6181 references, mark the symbol as strong. We can't do this earlier,
6182 because it might not be marked as undefined until the
6183 finish_dynamic_symbol routine gets through with it. */
6184 if (sym.st_shndx == SHN_UNDEF
6185 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
6186 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6187 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6191 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
6192 bindtype = STB_GLOBAL;
6194 bindtype = STB_WEAK;
6195 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6198 /* If a non-weak symbol with non-default visibility is not defined
6199 locally, it is a fatal error. */
6200 if (! finfo->info->relocatable
6201 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6202 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6203 && h->root.type == bfd_link_hash_undefined
6204 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6206 (*_bfd_error_handler)
6207 (_("%s: %s symbol `%s' isn't defined"),
6208 bfd_get_filename (finfo->output_bfd),
6209 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6211 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6212 ? "internal" : "hidden",
6213 h->root.root.string);
6214 eoinfo->failed = TRUE;
6218 /* If this symbol should be put in the .dynsym section, then put it
6219 there now. We already know the symbol index. We also fill in
6220 the entry in the .hash section. */
6221 if (h->dynindx != -1
6222 && elf_hash_table (finfo->info)->dynamic_sections_created)
6226 size_t hash_entry_size;
6227 bfd_byte *bucketpos;
6231 sym.st_name = h->dynstr_index;
6232 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6233 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6235 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6236 bucket = h->elf_hash_value % bucketcount;
6238 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6239 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6240 + (bucket + 2) * hash_entry_size);
6241 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6242 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6243 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6244 ((bfd_byte *) finfo->hash_sec->contents
6245 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6247 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6249 Elf_Internal_Versym iversym;
6250 Elf_External_Versym *eversym;
6252 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6254 if (h->verinfo.verdef == NULL)
6255 iversym.vs_vers = 0;
6257 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6261 if (h->verinfo.vertree == NULL)
6262 iversym.vs_vers = 1;
6264 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6267 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
6268 iversym.vs_vers |= VERSYM_HIDDEN;
6270 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6271 eversym += h->dynindx;
6272 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6276 /* If we're stripping it, then it was just a dynamic symbol, and
6277 there's nothing else to do. */
6278 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6281 h->indx = bfd_get_symcount (finfo->output_bfd);
6283 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6285 eoinfo->failed = TRUE;
6292 /* Return TRUE if special handling is done for relocs in SEC against
6293 symbols defined in discarded sections. */
6296 elf_section_ignore_discarded_relocs (asection *sec)
6298 const struct elf_backend_data *bed;
6300 switch (sec->sec_info_type)
6302 case ELF_INFO_TYPE_STABS:
6303 case ELF_INFO_TYPE_EH_FRAME:
6309 bed = get_elf_backend_data (sec->owner);
6310 if (bed->elf_backend_ignore_discarded_relocs != NULL
6311 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6317 /* Return TRUE if we should complain about a reloc in SEC against a
6318 symbol defined in a discarded section. */
6321 elf_section_complain_discarded (asection *sec)
6323 if (strncmp (".stab", sec->name, 5) == 0
6324 && (!sec->name[5] ||
6325 (sec->name[5] == '.' && ISDIGIT (sec->name[6]))))
6328 if (strcmp (".eh_frame", sec->name) == 0)
6331 if (strcmp (".gcc_except_table", sec->name) == 0)
6334 if (strcmp (".PARISC.unwind", sec->name) == 0)
6340 /* Link an input file into the linker output file. This function
6341 handles all the sections and relocations of the input file at once.
6342 This is so that we only have to read the local symbols once, and
6343 don't have to keep them in memory. */
6346 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6348 bfd_boolean (*relocate_section)
6349 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6350 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6352 Elf_Internal_Shdr *symtab_hdr;
6355 Elf_Internal_Sym *isymbuf;
6356 Elf_Internal_Sym *isym;
6357 Elf_Internal_Sym *isymend;
6359 asection **ppsection;
6361 const struct elf_backend_data *bed;
6362 bfd_boolean emit_relocs;
6363 struct elf_link_hash_entry **sym_hashes;
6365 output_bfd = finfo->output_bfd;
6366 bed = get_elf_backend_data (output_bfd);
6367 relocate_section = bed->elf_backend_relocate_section;
6369 /* If this is a dynamic object, we don't want to do anything here:
6370 we don't want the local symbols, and we don't want the section
6372 if ((input_bfd->flags & DYNAMIC) != 0)
6375 emit_relocs = (finfo->info->relocatable
6376 || finfo->info->emitrelocations
6377 || bed->elf_backend_emit_relocs);
6379 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6380 if (elf_bad_symtab (input_bfd))
6382 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6387 locsymcount = symtab_hdr->sh_info;
6388 extsymoff = symtab_hdr->sh_info;
6391 /* Read the local symbols. */
6392 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6393 if (isymbuf == NULL && locsymcount != 0)
6395 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6396 finfo->internal_syms,
6397 finfo->external_syms,
6398 finfo->locsym_shndx);
6399 if (isymbuf == NULL)
6403 /* Find local symbol sections and adjust values of symbols in
6404 SEC_MERGE sections. Write out those local symbols we know are
6405 going into the output file. */
6406 isymend = isymbuf + locsymcount;
6407 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6409 isym++, pindex++, ppsection++)
6413 Elf_Internal_Sym osym;
6417 if (elf_bad_symtab (input_bfd))
6419 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6426 if (isym->st_shndx == SHN_UNDEF)
6427 isec = bfd_und_section_ptr;
6428 else if (isym->st_shndx < SHN_LORESERVE
6429 || isym->st_shndx > SHN_HIRESERVE)
6431 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6433 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6434 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6436 _bfd_merged_section_offset (output_bfd, &isec,
6437 elf_section_data (isec)->sec_info,
6440 else if (isym->st_shndx == SHN_ABS)
6441 isec = bfd_abs_section_ptr;
6442 else if (isym->st_shndx == SHN_COMMON)
6443 isec = bfd_com_section_ptr;
6452 /* Don't output the first, undefined, symbol. */
6453 if (ppsection == finfo->sections)
6456 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6458 /* We never output section symbols. Instead, we use the
6459 section symbol of the corresponding section in the output
6464 /* If we are stripping all symbols, we don't want to output this
6466 if (finfo->info->strip == strip_all)
6469 /* If we are discarding all local symbols, we don't want to
6470 output this one. If we are generating a relocatable output
6471 file, then some of the local symbols may be required by
6472 relocs; we output them below as we discover that they are
6474 if (finfo->info->discard == discard_all)
6477 /* If this symbol is defined in a section which we are
6478 discarding, we don't need to keep it, but note that
6479 linker_mark is only reliable for sections that have contents.
6480 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6481 as well as linker_mark. */
6482 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6484 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6485 || (! finfo->info->relocatable
6486 && (isec->flags & SEC_EXCLUDE) != 0)))
6489 /* Get the name of the symbol. */
6490 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6495 /* See if we are discarding symbols with this name. */
6496 if ((finfo->info->strip == strip_some
6497 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6499 || (((finfo->info->discard == discard_sec_merge
6500 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6501 || finfo->info->discard == discard_l)
6502 && bfd_is_local_label_name (input_bfd, name)))
6505 /* If we get here, we are going to output this symbol. */
6509 /* Adjust the section index for the output file. */
6510 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6511 isec->output_section);
6512 if (osym.st_shndx == SHN_BAD)
6515 *pindex = bfd_get_symcount (output_bfd);
6517 /* ELF symbols in relocatable files are section relative, but
6518 in executable files they are virtual addresses. Note that
6519 this code assumes that all ELF sections have an associated
6520 BFD section with a reasonable value for output_offset; below
6521 we assume that they also have a reasonable value for
6522 output_section. Any special sections must be set up to meet
6523 these requirements. */
6524 osym.st_value += isec->output_offset;
6525 if (! finfo->info->relocatable)
6527 osym.st_value += isec->output_section->vma;
6528 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6530 /* STT_TLS symbols are relative to PT_TLS segment base. */
6531 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6532 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6536 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6540 /* Relocate the contents of each section. */
6541 sym_hashes = elf_sym_hashes (input_bfd);
6542 for (o = input_bfd->sections; o != NULL; o = o->next)
6546 if (! o->linker_mark)
6548 /* This section was omitted from the link. */
6552 if ((o->flags & SEC_HAS_CONTENTS) == 0
6553 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6556 if ((o->flags & SEC_LINKER_CREATED) != 0)
6558 /* Section was created by _bfd_elf_link_create_dynamic_sections
6563 /* Get the contents of the section. They have been cached by a
6564 relaxation routine. Note that o is a section in an input
6565 file, so the contents field will not have been set by any of
6566 the routines which work on output files. */
6567 if (elf_section_data (o)->this_hdr.contents != NULL)
6568 contents = elf_section_data (o)->this_hdr.contents;
6571 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6573 contents = finfo->contents;
6574 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6578 if ((o->flags & SEC_RELOC) != 0)
6580 Elf_Internal_Rela *internal_relocs;
6581 bfd_vma r_type_mask;
6584 /* Get the swapped relocs. */
6586 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6587 finfo->internal_relocs, FALSE);
6588 if (internal_relocs == NULL
6589 && o->reloc_count > 0)
6592 if (bed->s->arch_size == 32)
6599 r_type_mask = 0xffffffff;
6603 /* Run through the relocs looking for any against symbols
6604 from discarded sections and section symbols from
6605 removed link-once sections. Complain about relocs
6606 against discarded sections. Zero relocs against removed
6607 link-once sections. Preserve debug information as much
6609 if (!elf_section_ignore_discarded_relocs (o))
6611 Elf_Internal_Rela *rel, *relend;
6612 bfd_boolean complain = elf_section_complain_discarded (o);
6614 rel = internal_relocs;
6615 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6616 for ( ; rel < relend; rel++)
6618 unsigned long r_symndx = rel->r_info >> r_sym_shift;
6619 asection **ps, *sec;
6620 struct elf_link_hash_entry *h = NULL;
6621 const char *sym_name;
6623 if (r_symndx >= locsymcount
6624 || (elf_bad_symtab (input_bfd)
6625 && finfo->sections[r_symndx] == NULL))
6627 h = sym_hashes[r_symndx - extsymoff];
6628 while (h->root.type == bfd_link_hash_indirect
6629 || h->root.type == bfd_link_hash_warning)
6630 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6632 if (h->root.type != bfd_link_hash_defined
6633 && h->root.type != bfd_link_hash_defweak)
6636 ps = &h->root.u.def.section;
6637 sym_name = h->root.root.string;
6641 Elf_Internal_Sym *sym = isymbuf + r_symndx;
6642 ps = &finfo->sections[r_symndx];
6643 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
6646 /* Complain if the definition comes from a
6647 discarded section. */
6648 if ((sec = *ps) != NULL && elf_discarded_section (sec))
6650 if ((o->flags & SEC_DEBUGGING) != 0)
6652 BFD_ASSERT (r_symndx != 0);
6654 /* Try to preserve debug information.
6655 FIXME: This is quite broken. Modifying
6656 the symbol here means we will be changing
6657 all uses of the symbol, not just those in
6658 debug sections. The only thing that makes
6659 this half reasonable is that debug sections
6660 tend to come after other sections. Of
6661 course, that doesn't help with globals.
6662 ??? All link-once sections of the same name
6663 ought to define the same set of symbols, so
6664 it would seem that globals ought to always
6665 be defined in the kept section. */
6666 if (sec->kept_section != NULL
6667 && sec->size == sec->kept_section->size)
6669 *ps = sec->kept_section;
6676 = bfd_get_section_ident (o);
6678 = bfd_get_section_ident (sec);
6679 finfo->info->callbacks->error_handler
6680 (LD_DEFINITION_IN_DISCARDED_SECTION,
6681 _("`%T' referenced in section `%s' of %B: "
6682 "defined in discarded section `%s' of %B\n"),
6684 r_sec ? r_sec : o->name, input_bfd,
6685 d_sec ? d_sec : sec->name, sec->owner);
6692 /* Remove the symbol reference from the reloc, but
6693 don't kill the reloc completely. This is so that
6694 a zero value will be written into the section,
6695 which may have non-zero contents put there by the
6696 assembler. Zero in things like an eh_frame fde
6697 pc_begin allows stack unwinders to recognize the
6699 rel->r_info &= r_type_mask;
6705 /* Relocate the section by invoking a back end routine.
6707 The back end routine is responsible for adjusting the
6708 section contents as necessary, and (if using Rela relocs
6709 and generating a relocatable output file) adjusting the
6710 reloc addend as necessary.
6712 The back end routine does not have to worry about setting
6713 the reloc address or the reloc symbol index.
6715 The back end routine is given a pointer to the swapped in
6716 internal symbols, and can access the hash table entries
6717 for the external symbols via elf_sym_hashes (input_bfd).
6719 When generating relocatable output, the back end routine
6720 must handle STB_LOCAL/STT_SECTION symbols specially. The
6721 output symbol is going to be a section symbol
6722 corresponding to the output section, which will require
6723 the addend to be adjusted. */
6725 if (! (*relocate_section) (output_bfd, finfo->info,
6726 input_bfd, o, contents,
6734 Elf_Internal_Rela *irela;
6735 Elf_Internal_Rela *irelaend;
6736 bfd_vma last_offset;
6737 struct elf_link_hash_entry **rel_hash;
6738 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
6739 unsigned int next_erel;
6740 bfd_boolean (*reloc_emitter)
6741 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
6742 bfd_boolean rela_normal;
6744 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6745 rela_normal = (bed->rela_normal
6746 && (input_rel_hdr->sh_entsize
6747 == bed->s->sizeof_rela));
6749 /* Adjust the reloc addresses and symbol indices. */
6751 irela = internal_relocs;
6752 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6753 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6754 + elf_section_data (o->output_section)->rel_count
6755 + elf_section_data (o->output_section)->rel_count2);
6756 last_offset = o->output_offset;
6757 if (!finfo->info->relocatable)
6758 last_offset += o->output_section->vma;
6759 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6761 unsigned long r_symndx;
6763 Elf_Internal_Sym sym;
6765 if (next_erel == bed->s->int_rels_per_ext_rel)
6771 irela->r_offset = _bfd_elf_section_offset (output_bfd,
6774 if (irela->r_offset >= (bfd_vma) -2)
6776 /* This is a reloc for a deleted entry or somesuch.
6777 Turn it into an R_*_NONE reloc, at the same
6778 offset as the last reloc. elf_eh_frame.c and
6779 elf_bfd_discard_info rely on reloc offsets
6781 irela->r_offset = last_offset;
6783 irela->r_addend = 0;
6787 irela->r_offset += o->output_offset;
6789 /* Relocs in an executable have to be virtual addresses. */
6790 if (!finfo->info->relocatable)
6791 irela->r_offset += o->output_section->vma;
6793 last_offset = irela->r_offset;
6795 r_symndx = irela->r_info >> r_sym_shift;
6796 if (r_symndx == STN_UNDEF)
6799 if (r_symndx >= locsymcount
6800 || (elf_bad_symtab (input_bfd)
6801 && finfo->sections[r_symndx] == NULL))
6803 struct elf_link_hash_entry *rh;
6806 /* This is a reloc against a global symbol. We
6807 have not yet output all the local symbols, so
6808 we do not know the symbol index of any global
6809 symbol. We set the rel_hash entry for this
6810 reloc to point to the global hash table entry
6811 for this symbol. The symbol index is then
6812 set at the end of elf_bfd_final_link. */
6813 indx = r_symndx - extsymoff;
6814 rh = elf_sym_hashes (input_bfd)[indx];
6815 while (rh->root.type == bfd_link_hash_indirect
6816 || rh->root.type == bfd_link_hash_warning)
6817 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6819 /* Setting the index to -2 tells
6820 elf_link_output_extsym that this symbol is
6822 BFD_ASSERT (rh->indx < 0);
6830 /* This is a reloc against a local symbol. */
6833 sym = isymbuf[r_symndx];
6834 sec = finfo->sections[r_symndx];
6835 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
6837 /* I suppose the backend ought to fill in the
6838 section of any STT_SECTION symbol against a
6839 processor specific section. */
6841 if (bfd_is_abs_section (sec))
6843 else if (sec == NULL || sec->owner == NULL)
6845 bfd_set_error (bfd_error_bad_value);
6850 asection *osec = sec->output_section;
6852 /* If we have discarded a section, the output
6853 section will be the absolute section. In
6854 case of discarded link-once and discarded
6855 SEC_MERGE sections, use the kept section. */
6856 if (bfd_is_abs_section (osec)
6857 && sec->kept_section != NULL
6858 && sec->kept_section->output_section != NULL)
6860 osec = sec->kept_section->output_section;
6861 irela->r_addend -= osec->vma;
6864 if (!bfd_is_abs_section (osec))
6866 r_symndx = osec->target_index;
6867 BFD_ASSERT (r_symndx != 0);
6871 /* Adjust the addend according to where the
6872 section winds up in the output section. */
6874 irela->r_addend += sec->output_offset;
6878 if (finfo->indices[r_symndx] == -1)
6880 unsigned long shlink;
6884 if (finfo->info->strip == strip_all)
6886 /* You can't do ld -r -s. */
6887 bfd_set_error (bfd_error_invalid_operation);
6891 /* This symbol was skipped earlier, but
6892 since it is needed by a reloc, we
6893 must output it now. */
6894 shlink = symtab_hdr->sh_link;
6895 name = (bfd_elf_string_from_elf_section
6896 (input_bfd, shlink, sym.st_name));
6900 osec = sec->output_section;
6902 _bfd_elf_section_from_bfd_section (output_bfd,
6904 if (sym.st_shndx == SHN_BAD)
6907 sym.st_value += sec->output_offset;
6908 if (! finfo->info->relocatable)
6910 sym.st_value += osec->vma;
6911 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
6913 /* STT_TLS symbols are relative to PT_TLS
6915 BFD_ASSERT (elf_hash_table (finfo->info)
6917 sym.st_value -= (elf_hash_table (finfo->info)
6922 finfo->indices[r_symndx]
6923 = bfd_get_symcount (output_bfd);
6925 if (! elf_link_output_sym (finfo, name, &sym, sec,
6930 r_symndx = finfo->indices[r_symndx];
6933 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
6934 | (irela->r_info & r_type_mask));
6937 /* Swap out the relocs. */
6938 if (bed->elf_backend_emit_relocs
6939 && !(finfo->info->relocatable
6940 || finfo->info->emitrelocations))
6941 reloc_emitter = bed->elf_backend_emit_relocs;
6943 reloc_emitter = _bfd_elf_link_output_relocs;
6945 if (input_rel_hdr->sh_size != 0
6946 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
6950 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
6951 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
6953 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
6954 * bed->s->int_rels_per_ext_rel);
6955 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
6962 /* Write out the modified section contents. */
6963 if (bed->elf_backend_write_section
6964 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
6966 /* Section written out. */
6968 else switch (o->sec_info_type)
6970 case ELF_INFO_TYPE_STABS:
6971 if (! (_bfd_write_section_stabs
6973 &elf_hash_table (finfo->info)->stab_info,
6974 o, &elf_section_data (o)->sec_info, contents)))
6977 case ELF_INFO_TYPE_MERGE:
6978 if (! _bfd_write_merged_section (output_bfd, o,
6979 elf_section_data (o)->sec_info))
6982 case ELF_INFO_TYPE_EH_FRAME:
6984 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
6991 if (! (o->flags & SEC_EXCLUDE)
6992 && ! bfd_set_section_contents (output_bfd, o->output_section,
6994 (file_ptr) o->output_offset,
7005 /* Generate a reloc when linking an ELF file. This is a reloc
7006 requested by the linker, and does come from any input file. This
7007 is used to build constructor and destructor tables when linking
7011 elf_reloc_link_order (bfd *output_bfd,
7012 struct bfd_link_info *info,
7013 asection *output_section,
7014 struct bfd_link_order *link_order)
7016 reloc_howto_type *howto;
7020 struct elf_link_hash_entry **rel_hash_ptr;
7021 Elf_Internal_Shdr *rel_hdr;
7022 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7023 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7027 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7030 bfd_set_error (bfd_error_bad_value);
7034 addend = link_order->u.reloc.p->addend;
7036 /* Figure out the symbol index. */
7037 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7038 + elf_section_data (output_section)->rel_count
7039 + elf_section_data (output_section)->rel_count2);
7040 if (link_order->type == bfd_section_reloc_link_order)
7042 indx = link_order->u.reloc.p->u.section->target_index;
7043 BFD_ASSERT (indx != 0);
7044 *rel_hash_ptr = NULL;
7048 struct elf_link_hash_entry *h;
7050 /* Treat a reloc against a defined symbol as though it were
7051 actually against the section. */
7052 h = ((struct elf_link_hash_entry *)
7053 bfd_wrapped_link_hash_lookup (output_bfd, info,
7054 link_order->u.reloc.p->u.name,
7055 FALSE, FALSE, TRUE));
7057 && (h->root.type == bfd_link_hash_defined
7058 || h->root.type == bfd_link_hash_defweak))
7062 section = h->root.u.def.section;
7063 indx = section->output_section->target_index;
7064 *rel_hash_ptr = NULL;
7065 /* It seems that we ought to add the symbol value to the
7066 addend here, but in practice it has already been added
7067 because it was passed to constructor_callback. */
7068 addend += section->output_section->vma + section->output_offset;
7072 /* Setting the index to -2 tells elf_link_output_extsym that
7073 this symbol is used by a reloc. */
7080 if (! ((*info->callbacks->unattached_reloc)
7081 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7087 /* If this is an inplace reloc, we must write the addend into the
7089 if (howto->partial_inplace && addend != 0)
7092 bfd_reloc_status_type rstat;
7095 const char *sym_name;
7097 size = bfd_get_reloc_size (howto);
7098 buf = bfd_zmalloc (size);
7101 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7108 case bfd_reloc_outofrange:
7111 case bfd_reloc_overflow:
7112 if (link_order->type == bfd_section_reloc_link_order)
7113 sym_name = bfd_section_name (output_bfd,
7114 link_order->u.reloc.p->u.section);
7116 sym_name = link_order->u.reloc.p->u.name;
7117 if (! ((*info->callbacks->reloc_overflow)
7118 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
7125 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7126 link_order->offset, size);
7132 /* The address of a reloc is relative to the section in a
7133 relocatable file, and is a virtual address in an executable
7135 offset = link_order->offset;
7136 if (! info->relocatable)
7137 offset += output_section->vma;
7139 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7141 irel[i].r_offset = offset;
7143 irel[i].r_addend = 0;
7145 if (bed->s->arch_size == 32)
7146 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7148 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7150 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7151 erel = rel_hdr->contents;
7152 if (rel_hdr->sh_type == SHT_REL)
7154 erel += (elf_section_data (output_section)->rel_count
7155 * bed->s->sizeof_rel);
7156 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7160 irel[0].r_addend = addend;
7161 erel += (elf_section_data (output_section)->rel_count
7162 * bed->s->sizeof_rela);
7163 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7166 ++elf_section_data (output_section)->rel_count;
7171 /* Do the final step of an ELF link. */
7174 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7176 bfd_boolean dynamic;
7177 bfd_boolean emit_relocs;
7179 struct elf_final_link_info finfo;
7180 register asection *o;
7181 register struct bfd_link_order *p;
7183 bfd_size_type max_contents_size;
7184 bfd_size_type max_external_reloc_size;
7185 bfd_size_type max_internal_reloc_count;
7186 bfd_size_type max_sym_count;
7187 bfd_size_type max_sym_shndx_count;
7189 Elf_Internal_Sym elfsym;
7191 Elf_Internal_Shdr *symtab_hdr;
7192 Elf_Internal_Shdr *symtab_shndx_hdr;
7193 Elf_Internal_Shdr *symstrtab_hdr;
7194 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7195 struct elf_outext_info eoinfo;
7197 size_t relativecount = 0;
7198 asection *reldyn = 0;
7201 if (! is_elf_hash_table (info->hash))
7205 abfd->flags |= DYNAMIC;
7207 dynamic = elf_hash_table (info)->dynamic_sections_created;
7208 dynobj = elf_hash_table (info)->dynobj;
7210 emit_relocs = (info->relocatable
7211 || info->emitrelocations
7212 || bed->elf_backend_emit_relocs);
7215 finfo.output_bfd = abfd;
7216 finfo.symstrtab = _bfd_elf_stringtab_init ();
7217 if (finfo.symstrtab == NULL)
7222 finfo.dynsym_sec = NULL;
7223 finfo.hash_sec = NULL;
7224 finfo.symver_sec = NULL;
7228 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7229 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7230 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7231 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7232 /* Note that it is OK if symver_sec is NULL. */
7235 finfo.contents = NULL;
7236 finfo.external_relocs = NULL;
7237 finfo.internal_relocs = NULL;
7238 finfo.external_syms = NULL;
7239 finfo.locsym_shndx = NULL;
7240 finfo.internal_syms = NULL;
7241 finfo.indices = NULL;
7242 finfo.sections = NULL;
7243 finfo.symbuf = NULL;
7244 finfo.symshndxbuf = NULL;
7245 finfo.symbuf_count = 0;
7246 finfo.shndxbuf_size = 0;
7248 /* Count up the number of relocations we will output for each output
7249 section, so that we know the sizes of the reloc sections. We
7250 also figure out some maximum sizes. */
7251 max_contents_size = 0;
7252 max_external_reloc_size = 0;
7253 max_internal_reloc_count = 0;
7255 max_sym_shndx_count = 0;
7257 for (o = abfd->sections; o != NULL; o = o->next)
7259 struct bfd_elf_section_data *esdo = elf_section_data (o);
7262 for (p = o->link_order_head; p != NULL; p = p->next)
7264 unsigned int reloc_count = 0;
7265 struct bfd_elf_section_data *esdi = NULL;
7266 unsigned int *rel_count1;
7268 if (p->type == bfd_section_reloc_link_order
7269 || p->type == bfd_symbol_reloc_link_order)
7271 else if (p->type == bfd_indirect_link_order)
7275 sec = p->u.indirect.section;
7276 esdi = elf_section_data (sec);
7278 /* Mark all sections which are to be included in the
7279 link. This will normally be every section. We need
7280 to do this so that we can identify any sections which
7281 the linker has decided to not include. */
7282 sec->linker_mark = TRUE;
7284 if (sec->flags & SEC_MERGE)
7287 if (info->relocatable || info->emitrelocations)
7288 reloc_count = sec->reloc_count;
7289 else if (bed->elf_backend_count_relocs)
7291 Elf_Internal_Rela * relocs;
7293 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7296 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7298 if (elf_section_data (o)->relocs != relocs)
7302 if (sec->rawsize > max_contents_size)
7303 max_contents_size = sec->rawsize;
7304 if (sec->size > max_contents_size)
7305 max_contents_size = sec->size;
7307 /* We are interested in just local symbols, not all
7309 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7310 && (sec->owner->flags & DYNAMIC) == 0)
7314 if (elf_bad_symtab (sec->owner))
7315 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7316 / bed->s->sizeof_sym);
7318 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7320 if (sym_count > max_sym_count)
7321 max_sym_count = sym_count;
7323 if (sym_count > max_sym_shndx_count
7324 && elf_symtab_shndx (sec->owner) != 0)
7325 max_sym_shndx_count = sym_count;
7327 if ((sec->flags & SEC_RELOC) != 0)
7331 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7332 if (ext_size > max_external_reloc_size)
7333 max_external_reloc_size = ext_size;
7334 if (sec->reloc_count > max_internal_reloc_count)
7335 max_internal_reloc_count = sec->reloc_count;
7340 if (reloc_count == 0)
7343 o->reloc_count += reloc_count;
7345 /* MIPS may have a mix of REL and RELA relocs on sections.
7346 To support this curious ABI we keep reloc counts in
7347 elf_section_data too. We must be careful to add the
7348 relocations from the input section to the right output
7349 count. FIXME: Get rid of one count. We have
7350 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7351 rel_count1 = &esdo->rel_count;
7354 bfd_boolean same_size;
7355 bfd_size_type entsize1;
7357 entsize1 = esdi->rel_hdr.sh_entsize;
7358 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7359 || entsize1 == bed->s->sizeof_rela);
7360 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7363 rel_count1 = &esdo->rel_count2;
7365 if (esdi->rel_hdr2 != NULL)
7367 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7368 unsigned int alt_count;
7369 unsigned int *rel_count2;
7371 BFD_ASSERT (entsize2 != entsize1
7372 && (entsize2 == bed->s->sizeof_rel
7373 || entsize2 == bed->s->sizeof_rela));
7375 rel_count2 = &esdo->rel_count2;
7377 rel_count2 = &esdo->rel_count;
7379 /* The following is probably too simplistic if the
7380 backend counts output relocs unusually. */
7381 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7382 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7383 *rel_count2 += alt_count;
7384 reloc_count -= alt_count;
7387 *rel_count1 += reloc_count;
7390 if (o->reloc_count > 0)
7391 o->flags |= SEC_RELOC;
7394 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7395 set it (this is probably a bug) and if it is set
7396 assign_section_numbers will create a reloc section. */
7397 o->flags &=~ SEC_RELOC;
7400 /* If the SEC_ALLOC flag is not set, force the section VMA to
7401 zero. This is done in elf_fake_sections as well, but forcing
7402 the VMA to 0 here will ensure that relocs against these
7403 sections are handled correctly. */
7404 if ((o->flags & SEC_ALLOC) == 0
7405 && ! o->user_set_vma)
7409 if (! info->relocatable && merged)
7410 elf_link_hash_traverse (elf_hash_table (info),
7411 _bfd_elf_link_sec_merge_syms, abfd);
7413 /* Figure out the file positions for everything but the symbol table
7414 and the relocs. We set symcount to force assign_section_numbers
7415 to create a symbol table. */
7416 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7417 BFD_ASSERT (! abfd->output_has_begun);
7418 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7421 /* That created the reloc sections. Set their sizes, and assign
7422 them file positions, and allocate some buffers. */
7423 for (o = abfd->sections; o != NULL; o = o->next)
7425 if ((o->flags & SEC_RELOC) != 0)
7427 if (!(_bfd_elf_link_size_reloc_section
7428 (abfd, &elf_section_data (o)->rel_hdr, o)))
7431 if (elf_section_data (o)->rel_hdr2
7432 && !(_bfd_elf_link_size_reloc_section
7433 (abfd, elf_section_data (o)->rel_hdr2, o)))
7437 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7438 to count upwards while actually outputting the relocations. */
7439 elf_section_data (o)->rel_count = 0;
7440 elf_section_data (o)->rel_count2 = 0;
7443 _bfd_elf_assign_file_positions_for_relocs (abfd);
7445 /* We have now assigned file positions for all the sections except
7446 .symtab and .strtab. We start the .symtab section at the current
7447 file position, and write directly to it. We build the .strtab
7448 section in memory. */
7449 bfd_get_symcount (abfd) = 0;
7450 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7451 /* sh_name is set in prep_headers. */
7452 symtab_hdr->sh_type = SHT_SYMTAB;
7453 /* sh_flags, sh_addr and sh_size all start off zero. */
7454 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7455 /* sh_link is set in assign_section_numbers. */
7456 /* sh_info is set below. */
7457 /* sh_offset is set just below. */
7458 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
7460 off = elf_tdata (abfd)->next_file_pos;
7461 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
7463 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7464 incorrect. We do not yet know the size of the .symtab section.
7465 We correct next_file_pos below, after we do know the size. */
7467 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7468 continuously seeking to the right position in the file. */
7469 if (! info->keep_memory || max_sym_count < 20)
7470 finfo.symbuf_size = 20;
7472 finfo.symbuf_size = max_sym_count;
7473 amt = finfo.symbuf_size;
7474 amt *= bed->s->sizeof_sym;
7475 finfo.symbuf = bfd_malloc (amt);
7476 if (finfo.symbuf == NULL)
7478 if (elf_numsections (abfd) > SHN_LORESERVE)
7480 /* Wild guess at number of output symbols. realloc'd as needed. */
7481 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
7482 finfo.shndxbuf_size = amt;
7483 amt *= sizeof (Elf_External_Sym_Shndx);
7484 finfo.symshndxbuf = bfd_zmalloc (amt);
7485 if (finfo.symshndxbuf == NULL)
7489 /* Start writing out the symbol table. The first symbol is always a
7491 if (info->strip != strip_all
7494 elfsym.st_value = 0;
7497 elfsym.st_other = 0;
7498 elfsym.st_shndx = SHN_UNDEF;
7499 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
7505 /* Some standard ELF linkers do this, but we don't because it causes
7506 bootstrap comparison failures. */
7507 /* Output a file symbol for the output file as the second symbol.
7508 We output this even if we are discarding local symbols, although
7509 I'm not sure if this is correct. */
7510 elfsym.st_value = 0;
7512 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7513 elfsym.st_other = 0;
7514 elfsym.st_shndx = SHN_ABS;
7515 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
7516 &elfsym, bfd_abs_section_ptr, NULL))
7520 /* Output a symbol for each section. We output these even if we are
7521 discarding local symbols, since they are used for relocs. These
7522 symbols have no names. We store the index of each one in the
7523 index field of the section, so that we can find it again when
7524 outputting relocs. */
7525 if (info->strip != strip_all
7529 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7530 elfsym.st_other = 0;
7531 for (i = 1; i < elf_numsections (abfd); i++)
7533 o = bfd_section_from_elf_index (abfd, i);
7535 o->target_index = bfd_get_symcount (abfd);
7536 elfsym.st_shndx = i;
7537 if (info->relocatable || o == NULL)
7538 elfsym.st_value = 0;
7540 elfsym.st_value = o->vma;
7541 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
7543 if (i == SHN_LORESERVE - 1)
7544 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
7548 /* Allocate some memory to hold information read in from the input
7550 if (max_contents_size != 0)
7552 finfo.contents = bfd_malloc (max_contents_size);
7553 if (finfo.contents == NULL)
7557 if (max_external_reloc_size != 0)
7559 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
7560 if (finfo.external_relocs == NULL)
7564 if (max_internal_reloc_count != 0)
7566 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
7567 amt *= sizeof (Elf_Internal_Rela);
7568 finfo.internal_relocs = bfd_malloc (amt);
7569 if (finfo.internal_relocs == NULL)
7573 if (max_sym_count != 0)
7575 amt = max_sym_count * bed->s->sizeof_sym;
7576 finfo.external_syms = bfd_malloc (amt);
7577 if (finfo.external_syms == NULL)
7580 amt = max_sym_count * sizeof (Elf_Internal_Sym);
7581 finfo.internal_syms = bfd_malloc (amt);
7582 if (finfo.internal_syms == NULL)
7585 amt = max_sym_count * sizeof (long);
7586 finfo.indices = bfd_malloc (amt);
7587 if (finfo.indices == NULL)
7590 amt = max_sym_count * sizeof (asection *);
7591 finfo.sections = bfd_malloc (amt);
7592 if (finfo.sections == NULL)
7596 if (max_sym_shndx_count != 0)
7598 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
7599 finfo.locsym_shndx = bfd_malloc (amt);
7600 if (finfo.locsym_shndx == NULL)
7604 if (elf_hash_table (info)->tls_sec)
7606 bfd_vma base, end = 0;
7609 for (sec = elf_hash_table (info)->tls_sec;
7610 sec && (sec->flags & SEC_THREAD_LOCAL);
7613 bfd_vma size = sec->size;
7615 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
7617 struct bfd_link_order *o;
7619 for (o = sec->link_order_head; o != NULL; o = o->next)
7620 if (size < o->offset + o->size)
7621 size = o->offset + o->size;
7623 end = sec->vma + size;
7625 base = elf_hash_table (info)->tls_sec->vma;
7626 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
7627 elf_hash_table (info)->tls_size = end - base;
7630 /* Since ELF permits relocations to be against local symbols, we
7631 must have the local symbols available when we do the relocations.
7632 Since we would rather only read the local symbols once, and we
7633 would rather not keep them in memory, we handle all the
7634 relocations for a single input file at the same time.
7636 Unfortunately, there is no way to know the total number of local
7637 symbols until we have seen all of them, and the local symbol
7638 indices precede the global symbol indices. This means that when
7639 we are generating relocatable output, and we see a reloc against
7640 a global symbol, we can not know the symbol index until we have
7641 finished examining all the local symbols to see which ones we are
7642 going to output. To deal with this, we keep the relocations in
7643 memory, and don't output them until the end of the link. This is
7644 an unfortunate waste of memory, but I don't see a good way around
7645 it. Fortunately, it only happens when performing a relocatable
7646 link, which is not the common case. FIXME: If keep_memory is set
7647 we could write the relocs out and then read them again; I don't
7648 know how bad the memory loss will be. */
7650 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7651 sub->output_has_begun = FALSE;
7652 for (o = abfd->sections; o != NULL; o = o->next)
7654 for (p = o->link_order_head; p != NULL; p = p->next)
7656 if (p->type == bfd_indirect_link_order
7657 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7658 == bfd_target_elf_flavour)
7659 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7661 if (! sub->output_has_begun)
7663 if (! elf_link_input_bfd (&finfo, sub))
7665 sub->output_has_begun = TRUE;
7668 else if (p->type == bfd_section_reloc_link_order
7669 || p->type == bfd_symbol_reloc_link_order)
7671 if (! elf_reloc_link_order (abfd, info, o, p))
7676 if (! _bfd_default_link_order (abfd, info, o, p))
7682 /* Output any global symbols that got converted to local in a
7683 version script or due to symbol visibility. We do this in a
7684 separate step since ELF requires all local symbols to appear
7685 prior to any global symbols. FIXME: We should only do this if
7686 some global symbols were, in fact, converted to become local.
7687 FIXME: Will this work correctly with the Irix 5 linker? */
7688 eoinfo.failed = FALSE;
7689 eoinfo.finfo = &finfo;
7690 eoinfo.localsyms = TRUE;
7691 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
7696 /* That wrote out all the local symbols. Finish up the symbol table
7697 with the global symbols. Even if we want to strip everything we
7698 can, we still need to deal with those global symbols that got
7699 converted to local in a version script. */
7701 /* The sh_info field records the index of the first non local symbol. */
7702 symtab_hdr->sh_info = bfd_get_symcount (abfd);
7705 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
7707 Elf_Internal_Sym sym;
7708 bfd_byte *dynsym = finfo.dynsym_sec->contents;
7709 long last_local = 0;
7711 /* Write out the section symbols for the output sections. */
7718 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7721 for (s = abfd->sections; s != NULL; s = s->next)
7727 dynindx = elf_section_data (s)->dynindx;
7730 indx = elf_section_data (s)->this_idx;
7731 BFD_ASSERT (indx > 0);
7732 sym.st_shndx = indx;
7733 sym.st_value = s->vma;
7734 dest = dynsym + dynindx * bed->s->sizeof_sym;
7735 if (last_local < dynindx)
7736 last_local = dynindx;
7737 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
7741 /* Write out the local dynsyms. */
7742 if (elf_hash_table (info)->dynlocal)
7744 struct elf_link_local_dynamic_entry *e;
7745 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
7750 sym.st_size = e->isym.st_size;
7751 sym.st_other = e->isym.st_other;
7753 /* Copy the internal symbol as is.
7754 Note that we saved a word of storage and overwrote
7755 the original st_name with the dynstr_index. */
7758 if (e->isym.st_shndx != SHN_UNDEF
7759 && (e->isym.st_shndx < SHN_LORESERVE
7760 || e->isym.st_shndx > SHN_HIRESERVE))
7762 s = bfd_section_from_elf_index (e->input_bfd,
7766 elf_section_data (s->output_section)->this_idx;
7767 sym.st_value = (s->output_section->vma
7769 + e->isym.st_value);
7772 if (last_local < e->dynindx)
7773 last_local = e->dynindx;
7775 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
7776 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
7780 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
7784 /* We get the global symbols from the hash table. */
7785 eoinfo.failed = FALSE;
7786 eoinfo.localsyms = FALSE;
7787 eoinfo.finfo = &finfo;
7788 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
7793 /* If backend needs to output some symbols not present in the hash
7794 table, do it now. */
7795 if (bed->elf_backend_output_arch_syms)
7797 typedef bfd_boolean (*out_sym_func)
7798 (void *, const char *, Elf_Internal_Sym *, asection *,
7799 struct elf_link_hash_entry *);
7801 if (! ((*bed->elf_backend_output_arch_syms)
7802 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
7806 /* Flush all symbols to the file. */
7807 if (! elf_link_flush_output_syms (&finfo, bed))
7810 /* Now we know the size of the symtab section. */
7811 off += symtab_hdr->sh_size;
7813 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
7814 if (symtab_shndx_hdr->sh_name != 0)
7816 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
7817 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
7818 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
7819 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
7820 symtab_shndx_hdr->sh_size = amt;
7822 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
7825 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
7826 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
7831 /* Finish up and write out the symbol string table (.strtab)
7833 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
7834 /* sh_name was set in prep_headers. */
7835 symstrtab_hdr->sh_type = SHT_STRTAB;
7836 symstrtab_hdr->sh_flags = 0;
7837 symstrtab_hdr->sh_addr = 0;
7838 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
7839 symstrtab_hdr->sh_entsize = 0;
7840 symstrtab_hdr->sh_link = 0;
7841 symstrtab_hdr->sh_info = 0;
7842 /* sh_offset is set just below. */
7843 symstrtab_hdr->sh_addralign = 1;
7845 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
7846 elf_tdata (abfd)->next_file_pos = off;
7848 if (bfd_get_symcount (abfd) > 0)
7850 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
7851 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
7855 /* Adjust the relocs to have the correct symbol indices. */
7856 for (o = abfd->sections; o != NULL; o = o->next)
7858 if ((o->flags & SEC_RELOC) == 0)
7861 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
7862 elf_section_data (o)->rel_count,
7863 elf_section_data (o)->rel_hashes);
7864 if (elf_section_data (o)->rel_hdr2 != NULL)
7865 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
7866 elf_section_data (o)->rel_count2,
7867 (elf_section_data (o)->rel_hashes
7868 + elf_section_data (o)->rel_count));
7870 /* Set the reloc_count field to 0 to prevent write_relocs from
7871 trying to swap the relocs out itself. */
7875 if (dynamic && info->combreloc && dynobj != NULL)
7876 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
7878 /* If we are linking against a dynamic object, or generating a
7879 shared library, finish up the dynamic linking information. */
7882 bfd_byte *dyncon, *dynconend;
7884 /* Fix up .dynamic entries. */
7885 o = bfd_get_section_by_name (dynobj, ".dynamic");
7886 BFD_ASSERT (o != NULL);
7888 dyncon = o->contents;
7889 dynconend = o->contents + o->size;
7890 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
7892 Elf_Internal_Dyn dyn;
7896 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
7903 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
7905 switch (elf_section_data (reldyn)->this_hdr.sh_type)
7907 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
7908 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
7911 dyn.d_un.d_val = relativecount;
7918 name = info->init_function;
7921 name = info->fini_function;
7924 struct elf_link_hash_entry *h;
7926 h = elf_link_hash_lookup (elf_hash_table (info), name,
7927 FALSE, FALSE, TRUE);
7929 && (h->root.type == bfd_link_hash_defined
7930 || h->root.type == bfd_link_hash_defweak))
7932 dyn.d_un.d_val = h->root.u.def.value;
7933 o = h->root.u.def.section;
7934 if (o->output_section != NULL)
7935 dyn.d_un.d_val += (o->output_section->vma
7936 + o->output_offset);
7939 /* The symbol is imported from another shared
7940 library and does not apply to this one. */
7948 case DT_PREINIT_ARRAYSZ:
7949 name = ".preinit_array";
7951 case DT_INIT_ARRAYSZ:
7952 name = ".init_array";
7954 case DT_FINI_ARRAYSZ:
7955 name = ".fini_array";
7957 o = bfd_get_section_by_name (abfd, name);
7960 (*_bfd_error_handler)
7961 (_("%s: could not find output section %s"),
7962 bfd_get_filename (abfd), name);
7966 (*_bfd_error_handler)
7967 (_("warning: %s section has zero size"), name);
7968 dyn.d_un.d_val = o->size;
7971 case DT_PREINIT_ARRAY:
7972 name = ".preinit_array";
7975 name = ".init_array";
7978 name = ".fini_array";
7991 name = ".gnu.version_d";
7994 name = ".gnu.version_r";
7997 name = ".gnu.version";
7999 o = bfd_get_section_by_name (abfd, name);
8002 (*_bfd_error_handler)
8003 (_("%s: could not find output section %s"),
8004 bfd_get_filename (abfd), name);
8007 dyn.d_un.d_ptr = o->vma;
8014 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8019 for (i = 1; i < elf_numsections (abfd); i++)
8021 Elf_Internal_Shdr *hdr;
8023 hdr = elf_elfsections (abfd)[i];
8024 if (hdr->sh_type == type
8025 && (hdr->sh_flags & SHF_ALLOC) != 0)
8027 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8028 dyn.d_un.d_val += hdr->sh_size;
8031 if (dyn.d_un.d_val == 0
8032 || hdr->sh_addr < dyn.d_un.d_val)
8033 dyn.d_un.d_val = hdr->sh_addr;
8039 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8043 /* If we have created any dynamic sections, then output them. */
8046 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8049 for (o = dynobj->sections; o != NULL; o = o->next)
8051 if ((o->flags & SEC_HAS_CONTENTS) == 0
8053 || o->output_section == bfd_abs_section_ptr)
8055 if ((o->flags & SEC_LINKER_CREATED) == 0)
8057 /* At this point, we are only interested in sections
8058 created by _bfd_elf_link_create_dynamic_sections. */
8061 if (elf_hash_table (info)->stab_info.stabstr == o)
8063 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8065 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8067 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8069 if (! bfd_set_section_contents (abfd, o->output_section,
8071 (file_ptr) o->output_offset,
8077 /* The contents of the .dynstr section are actually in a
8079 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8080 if (bfd_seek (abfd, off, SEEK_SET) != 0
8081 || ! _bfd_elf_strtab_emit (abfd,
8082 elf_hash_table (info)->dynstr))
8088 if (info->relocatable)
8090 bfd_boolean failed = FALSE;
8092 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8097 /* If we have optimized stabs strings, output them. */
8098 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8100 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8104 if (info->eh_frame_hdr)
8106 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8110 if (finfo.symstrtab != NULL)
8111 _bfd_stringtab_free (finfo.symstrtab);
8112 if (finfo.contents != NULL)
8113 free (finfo.contents);
8114 if (finfo.external_relocs != NULL)
8115 free (finfo.external_relocs);
8116 if (finfo.internal_relocs != NULL)
8117 free (finfo.internal_relocs);
8118 if (finfo.external_syms != NULL)
8119 free (finfo.external_syms);
8120 if (finfo.locsym_shndx != NULL)
8121 free (finfo.locsym_shndx);
8122 if (finfo.internal_syms != NULL)
8123 free (finfo.internal_syms);
8124 if (finfo.indices != NULL)
8125 free (finfo.indices);
8126 if (finfo.sections != NULL)
8127 free (finfo.sections);
8128 if (finfo.symbuf != NULL)
8129 free (finfo.symbuf);
8130 if (finfo.symshndxbuf != NULL)
8131 free (finfo.symshndxbuf);
8132 for (o = abfd->sections; o != NULL; o = o->next)
8134 if ((o->flags & SEC_RELOC) != 0
8135 && elf_section_data (o)->rel_hashes != NULL)
8136 free (elf_section_data (o)->rel_hashes);
8139 elf_tdata (abfd)->linker = TRUE;
8144 if (finfo.symstrtab != NULL)
8145 _bfd_stringtab_free (finfo.symstrtab);
8146 if (finfo.contents != NULL)
8147 free (finfo.contents);
8148 if (finfo.external_relocs != NULL)
8149 free (finfo.external_relocs);
8150 if (finfo.internal_relocs != NULL)
8151 free (finfo.internal_relocs);
8152 if (finfo.external_syms != NULL)
8153 free (finfo.external_syms);
8154 if (finfo.locsym_shndx != NULL)
8155 free (finfo.locsym_shndx);
8156 if (finfo.internal_syms != NULL)
8157 free (finfo.internal_syms);
8158 if (finfo.indices != NULL)
8159 free (finfo.indices);
8160 if (finfo.sections != NULL)
8161 free (finfo.sections);
8162 if (finfo.symbuf != NULL)
8163 free (finfo.symbuf);
8164 if (finfo.symshndxbuf != NULL)
8165 free (finfo.symshndxbuf);
8166 for (o = abfd->sections; o != NULL; o = o->next)
8168 if ((o->flags & SEC_RELOC) != 0
8169 && elf_section_data (o)->rel_hashes != NULL)
8170 free (elf_section_data (o)->rel_hashes);
8176 /* Garbage collect unused sections. */
8178 /* The mark phase of garbage collection. For a given section, mark
8179 it and any sections in this section's group, and all the sections
8180 which define symbols to which it refers. */
8182 typedef asection * (*gc_mark_hook_fn)
8183 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8184 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8187 elf_gc_mark (struct bfd_link_info *info,
8189 gc_mark_hook_fn gc_mark_hook)
8192 asection *group_sec;
8196 /* Mark all the sections in the group. */
8197 group_sec = elf_section_data (sec)->next_in_group;
8198 if (group_sec && !group_sec->gc_mark)
8199 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
8202 /* Look through the section relocs. */
8204 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8206 Elf_Internal_Rela *relstart, *rel, *relend;
8207 Elf_Internal_Shdr *symtab_hdr;
8208 struct elf_link_hash_entry **sym_hashes;
8211 bfd *input_bfd = sec->owner;
8212 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8213 Elf_Internal_Sym *isym = NULL;
8216 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8217 sym_hashes = elf_sym_hashes (input_bfd);
8219 /* Read the local symbols. */
8220 if (elf_bad_symtab (input_bfd))
8222 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8226 extsymoff = nlocsyms = symtab_hdr->sh_info;
8228 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8229 if (isym == NULL && nlocsyms != 0)
8231 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8237 /* Read the relocations. */
8238 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8240 if (relstart == NULL)
8245 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8247 if (bed->s->arch_size == 32)
8252 for (rel = relstart; rel < relend; rel++)
8254 unsigned long r_symndx;
8256 struct elf_link_hash_entry *h;
8258 r_symndx = rel->r_info >> r_sym_shift;
8262 if (r_symndx >= nlocsyms
8263 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8265 h = sym_hashes[r_symndx - extsymoff];
8266 while (h->root.type == bfd_link_hash_indirect
8267 || h->root.type == bfd_link_hash_warning)
8268 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8269 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8273 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8276 if (rsec && !rsec->gc_mark)
8278 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8280 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
8289 if (elf_section_data (sec)->relocs != relstart)
8292 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8294 if (! info->keep_memory)
8297 symtab_hdr->contents = (unsigned char *) isym;
8304 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8307 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8311 if (h->root.type == bfd_link_hash_warning)
8312 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8314 if (h->dynindx != -1
8315 && ((h->root.type != bfd_link_hash_defined
8316 && h->root.type != bfd_link_hash_defweak)
8317 || h->root.u.def.section->gc_mark))
8318 h->dynindx = (*idx)++;
8323 /* The sweep phase of garbage collection. Remove all garbage sections. */
8325 typedef bfd_boolean (*gc_sweep_hook_fn)
8326 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8329 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8333 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8337 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8340 for (o = sub->sections; o != NULL; o = o->next)
8342 /* Keep special sections. Keep .debug sections. */
8343 if ((o->flags & SEC_LINKER_CREATED)
8344 || (o->flags & SEC_DEBUGGING))
8350 /* Skip sweeping sections already excluded. */
8351 if (o->flags & SEC_EXCLUDE)
8354 /* Since this is early in the link process, it is simple
8355 to remove a section from the output. */
8356 o->flags |= SEC_EXCLUDE;
8358 /* But we also have to update some of the relocation
8359 info we collected before. */
8361 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
8363 Elf_Internal_Rela *internal_relocs;
8367 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8369 if (internal_relocs == NULL)
8372 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8374 if (elf_section_data (o)->relocs != internal_relocs)
8375 free (internal_relocs);
8383 /* Remove the symbols that were in the swept sections from the dynamic
8384 symbol table. GCFIXME: Anyone know how to get them out of the
8385 static symbol table as well? */
8389 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8391 elf_hash_table (info)->dynsymcount = i;
8397 /* Propagate collected vtable information. This is called through
8398 elf_link_hash_traverse. */
8401 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8403 if (h->root.type == bfd_link_hash_warning)
8404 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8406 /* Those that are not vtables. */
8407 if (h->vtable_parent == NULL)
8410 /* Those vtables that do not have parents, we cannot merge. */
8411 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
8414 /* If we've already been done, exit. */
8415 if (h->vtable_entries_used && h->vtable_entries_used[-1])
8418 /* Make sure the parent's table is up to date. */
8419 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
8421 if (h->vtable_entries_used == NULL)
8423 /* None of this table's entries were referenced. Re-use the
8425 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
8426 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
8431 bfd_boolean *cu, *pu;
8433 /* Or the parent's entries into ours. */
8434 cu = h->vtable_entries_used;
8436 pu = h->vtable_parent->vtable_entries_used;
8439 const struct elf_backend_data *bed;
8440 unsigned int log_file_align;
8442 bed = get_elf_backend_data (h->root.u.def.section->owner);
8443 log_file_align = bed->s->log_file_align;
8444 n = h->vtable_parent->vtable_entries_size >> log_file_align;
8459 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
8462 bfd_vma hstart, hend;
8463 Elf_Internal_Rela *relstart, *relend, *rel;
8464 const struct elf_backend_data *bed;
8465 unsigned int log_file_align;
8467 if (h->root.type == bfd_link_hash_warning)
8468 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8470 /* Take care of both those symbols that do not describe vtables as
8471 well as those that are not loaded. */
8472 if (h->vtable_parent == NULL)
8475 BFD_ASSERT (h->root.type == bfd_link_hash_defined
8476 || h->root.type == bfd_link_hash_defweak);
8478 sec = h->root.u.def.section;
8479 hstart = h->root.u.def.value;
8480 hend = hstart + h->size;
8482 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
8484 return *(bfd_boolean *) okp = FALSE;
8485 bed = get_elf_backend_data (sec->owner);
8486 log_file_align = bed->s->log_file_align;
8488 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8490 for (rel = relstart; rel < relend; ++rel)
8491 if (rel->r_offset >= hstart && rel->r_offset < hend)
8493 /* If the entry is in use, do nothing. */
8494 if (h->vtable_entries_used
8495 && (rel->r_offset - hstart) < h->vtable_entries_size)
8497 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
8498 if (h->vtable_entries_used[entry])
8501 /* Otherwise, kill it. */
8502 rel->r_offset = rel->r_info = rel->r_addend = 0;
8508 /* Mark sections containing dynamically referenced symbols. This is called
8509 through elf_link_hash_traverse. */
8512 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h,
8513 void *okp ATTRIBUTE_UNUSED)
8515 if (h->root.type == bfd_link_hash_warning)
8516 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8518 if ((h->root.type == bfd_link_hash_defined
8519 || h->root.type == bfd_link_hash_defweak)
8520 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC))
8521 h->root.u.def.section->flags |= SEC_KEEP;
8526 /* Do mark and sweep of unused sections. */
8529 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
8531 bfd_boolean ok = TRUE;
8533 asection * (*gc_mark_hook)
8534 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8535 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
8537 if (!get_elf_backend_data (abfd)->can_gc_sections
8538 || info->relocatable
8539 || info->emitrelocations
8541 || !is_elf_hash_table (info->hash))
8543 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
8547 /* Apply transitive closure to the vtable entry usage info. */
8548 elf_link_hash_traverse (elf_hash_table (info),
8549 elf_gc_propagate_vtable_entries_used,
8554 /* Kill the vtable relocations that were not used. */
8555 elf_link_hash_traverse (elf_hash_table (info),
8556 elf_gc_smash_unused_vtentry_relocs,
8561 /* Mark dynamically referenced symbols. */
8562 if (elf_hash_table (info)->dynamic_sections_created)
8563 elf_link_hash_traverse (elf_hash_table (info),
8564 elf_gc_mark_dynamic_ref_symbol,
8569 /* Grovel through relocs to find out who stays ... */
8570 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
8571 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8575 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8578 for (o = sub->sections; o != NULL; o = o->next)
8580 if (o->flags & SEC_KEEP)
8582 /* _bfd_elf_discard_section_eh_frame knows how to discard
8583 orphaned FDEs so don't mark sections referenced by the
8584 EH frame section. */
8585 if (strcmp (o->name, ".eh_frame") == 0)
8587 else if (!elf_gc_mark (info, o, gc_mark_hook))
8593 /* ... and mark SEC_EXCLUDE for those that go. */
8594 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
8600 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8603 bfd_elf_gc_record_vtinherit (bfd *abfd,
8605 struct elf_link_hash_entry *h,
8608 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
8609 struct elf_link_hash_entry **search, *child;
8610 bfd_size_type extsymcount;
8611 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8614 /* The sh_info field of the symtab header tells us where the
8615 external symbols start. We don't care about the local symbols at
8617 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
8618 if (!elf_bad_symtab (abfd))
8619 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8621 sym_hashes = elf_sym_hashes (abfd);
8622 sym_hashes_end = sym_hashes + extsymcount;
8624 /* Hunt down the child symbol, which is in this section at the same
8625 offset as the relocation. */
8626 for (search = sym_hashes; search != sym_hashes_end; ++search)
8628 if ((child = *search) != NULL
8629 && (child->root.type == bfd_link_hash_defined
8630 || child->root.type == bfd_link_hash_defweak)
8631 && child->root.u.def.section == sec
8632 && child->root.u.def.value == offset)
8636 sec_name = bfd_get_section_ident (sec);
8637 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
8638 bfd_archive_filename (abfd),
8639 sec_name ? sec_name : sec->name,
8640 (unsigned long) offset);
8641 bfd_set_error (bfd_error_invalid_operation);
8647 /* This *should* only be the absolute section. It could potentially
8648 be that someone has defined a non-global vtable though, which
8649 would be bad. It isn't worth paging in the local symbols to be
8650 sure though; that case should simply be handled by the assembler. */
8652 child->vtable_parent = (struct elf_link_hash_entry *) -1;
8655 child->vtable_parent = h;
8660 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8663 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
8664 asection *sec ATTRIBUTE_UNUSED,
8665 struct elf_link_hash_entry *h,
8668 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8669 unsigned int log_file_align = bed->s->log_file_align;
8671 if (addend >= h->vtable_entries_size)
8673 size_t size, bytes, file_align;
8674 bfd_boolean *ptr = h->vtable_entries_used;
8676 /* While the symbol is undefined, we have to be prepared to handle
8678 file_align = 1 << log_file_align;
8679 if (h->root.type == bfd_link_hash_undefined)
8680 size = addend + file_align;
8686 /* Oops! We've got a reference past the defined end of
8687 the table. This is probably a bug -- shall we warn? */
8688 size = addend + file_align;
8691 size = (size + file_align - 1) & -file_align;
8693 /* Allocate one extra entry for use as a "done" flag for the
8694 consolidation pass. */
8695 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
8699 ptr = bfd_realloc (ptr - 1, bytes);
8705 oldbytes = (((h->vtable_entries_size >> log_file_align) + 1)
8706 * sizeof (bfd_boolean));
8707 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
8711 ptr = bfd_zmalloc (bytes);
8716 /* And arrange for that done flag to be at index -1. */
8717 h->vtable_entries_used = ptr + 1;
8718 h->vtable_entries_size = size;
8721 h->vtable_entries_used[addend >> log_file_align] = TRUE;
8726 struct alloc_got_off_arg {
8728 unsigned int got_elt_size;
8731 /* We need a special top-level link routine to convert got reference counts
8732 to real got offsets. */
8735 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
8737 struct alloc_got_off_arg *gofarg = arg;
8739 if (h->root.type == bfd_link_hash_warning)
8740 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8742 if (h->got.refcount > 0)
8744 h->got.offset = gofarg->gotoff;
8745 gofarg->gotoff += gofarg->got_elt_size;
8748 h->got.offset = (bfd_vma) -1;
8753 /* And an accompanying bit to work out final got entry offsets once
8754 we're done. Should be called from final_link. */
8757 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
8758 struct bfd_link_info *info)
8761 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8763 unsigned int got_elt_size = bed->s->arch_size / 8;
8764 struct alloc_got_off_arg gofarg;
8766 if (! is_elf_hash_table (info->hash))
8769 /* The GOT offset is relative to the .got section, but the GOT header is
8770 put into the .got.plt section, if the backend uses it. */
8771 if (bed->want_got_plt)
8774 gotoff = bed->got_header_size;
8776 /* Do the local .got entries first. */
8777 for (i = info->input_bfds; i; i = i->link_next)
8779 bfd_signed_vma *local_got;
8780 bfd_size_type j, locsymcount;
8781 Elf_Internal_Shdr *symtab_hdr;
8783 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
8786 local_got = elf_local_got_refcounts (i);
8790 symtab_hdr = &elf_tdata (i)->symtab_hdr;
8791 if (elf_bad_symtab (i))
8792 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8794 locsymcount = symtab_hdr->sh_info;
8796 for (j = 0; j < locsymcount; ++j)
8798 if (local_got[j] > 0)
8800 local_got[j] = gotoff;
8801 gotoff += got_elt_size;
8804 local_got[j] = (bfd_vma) -1;
8808 /* Then the global .got entries. .plt refcounts are handled by
8809 adjust_dynamic_symbol */
8810 gofarg.gotoff = gotoff;
8811 gofarg.got_elt_size = got_elt_size;
8812 elf_link_hash_traverse (elf_hash_table (info),
8813 elf_gc_allocate_got_offsets,
8818 /* Many folk need no more in the way of final link than this, once
8819 got entry reference counting is enabled. */
8822 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
8824 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
8827 /* Invoke the regular ELF backend linker to do all the work. */
8828 return bfd_elf_final_link (abfd, info);
8832 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
8834 struct elf_reloc_cookie *rcookie = cookie;
8836 if (rcookie->bad_symtab)
8837 rcookie->rel = rcookie->rels;
8839 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
8841 unsigned long r_symndx;
8843 if (! rcookie->bad_symtab)
8844 if (rcookie->rel->r_offset > offset)
8846 if (rcookie->rel->r_offset != offset)
8849 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
8850 if (r_symndx == SHN_UNDEF)
8853 if (r_symndx >= rcookie->locsymcount
8854 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
8856 struct elf_link_hash_entry *h;
8858 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
8860 while (h->root.type == bfd_link_hash_indirect
8861 || h->root.type == bfd_link_hash_warning)
8862 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8864 if ((h->root.type == bfd_link_hash_defined
8865 || h->root.type == bfd_link_hash_defweak)
8866 && elf_discarded_section (h->root.u.def.section))
8873 /* It's not a relocation against a global symbol,
8874 but it could be a relocation against a local
8875 symbol for a discarded section. */
8877 Elf_Internal_Sym *isym;
8879 /* Need to: get the symbol; get the section. */
8880 isym = &rcookie->locsyms[r_symndx];
8881 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
8883 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
8884 if (isec != NULL && elf_discarded_section (isec))
8893 /* Discard unneeded references to discarded sections.
8894 Returns TRUE if any section's size was changed. */
8895 /* This function assumes that the relocations are in sorted order,
8896 which is true for all known assemblers. */
8899 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
8901 struct elf_reloc_cookie cookie;
8902 asection *stab, *eh;
8903 Elf_Internal_Shdr *symtab_hdr;
8904 const struct elf_backend_data *bed;
8907 bfd_boolean ret = FALSE;
8909 if (info->traditional_format
8910 || !is_elf_hash_table (info->hash))
8913 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
8915 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
8918 bed = get_elf_backend_data (abfd);
8920 if ((abfd->flags & DYNAMIC) != 0)
8923 eh = bfd_get_section_by_name (abfd, ".eh_frame");
8924 if (info->relocatable
8927 || bfd_is_abs_section (eh->output_section))))
8930 stab = bfd_get_section_by_name (abfd, ".stab");
8933 || bfd_is_abs_section (stab->output_section)
8934 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
8939 && bed->elf_backend_discard_info == NULL)
8942 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8944 cookie.sym_hashes = elf_sym_hashes (abfd);
8945 cookie.bad_symtab = elf_bad_symtab (abfd);
8946 if (cookie.bad_symtab)
8948 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8949 cookie.extsymoff = 0;
8953 cookie.locsymcount = symtab_hdr->sh_info;
8954 cookie.extsymoff = symtab_hdr->sh_info;
8957 if (bed->s->arch_size == 32)
8958 cookie.r_sym_shift = 8;
8960 cookie.r_sym_shift = 32;
8962 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
8963 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
8965 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8966 cookie.locsymcount, 0,
8968 if (cookie.locsyms == NULL)
8975 count = stab->reloc_count;
8977 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
8979 if (cookie.rels != NULL)
8981 cookie.rel = cookie.rels;
8982 cookie.relend = cookie.rels;
8983 cookie.relend += count * bed->s->int_rels_per_ext_rel;
8984 if (_bfd_discard_section_stabs (abfd, stab,
8985 elf_section_data (stab)->sec_info,
8986 bfd_elf_reloc_symbol_deleted_p,
8989 if (elf_section_data (stab)->relocs != cookie.rels)
8997 count = eh->reloc_count;
8999 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9001 cookie.rel = cookie.rels;
9002 cookie.relend = cookie.rels;
9003 if (cookie.rels != NULL)
9004 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9006 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9007 bfd_elf_reloc_symbol_deleted_p,
9011 if (cookie.rels != NULL
9012 && elf_section_data (eh)->relocs != cookie.rels)
9016 if (bed->elf_backend_discard_info != NULL
9017 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9020 if (cookie.locsyms != NULL
9021 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9023 if (! info->keep_memory)
9024 free (cookie.locsyms);
9026 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9030 if (info->eh_frame_hdr
9031 && !info->relocatable
9032 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))