2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001
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. */
21 /* ELF linker code. */
23 /* This struct is used to pass information to routines called via
24 elf_link_hash_traverse which must return failure. */
26 struct elf_info_failed
29 struct bfd_link_info *info;
30 struct bfd_elf_version_tree *verdefs;
33 static boolean is_global_data_symbol_definition
34 PARAMS ((bfd *, Elf_Internal_Sym *));
35 static boolean elf_link_is_defined_archive_symbol
36 PARAMS ((bfd *, carsym *));
37 static boolean elf_link_add_object_symbols
38 PARAMS ((bfd *, struct bfd_link_info *));
39 static boolean elf_link_add_archive_symbols
40 PARAMS ((bfd *, struct bfd_link_info *));
41 static boolean elf_merge_symbol
42 PARAMS ((bfd *, struct bfd_link_info *, const char *,
43 Elf_Internal_Sym *, asection **, bfd_vma *,
44 struct elf_link_hash_entry **, boolean *, boolean *,
46 static boolean elf_add_default_symbol
47 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
48 const char *, Elf_Internal_Sym *, asection **, bfd_vma *,
49 boolean *, boolean, boolean));
50 static boolean elf_export_symbol
51 PARAMS ((struct elf_link_hash_entry *, PTR));
52 static boolean elf_finalize_dynstr
53 PARAMS ((bfd *, struct bfd_link_info *));
54 static boolean elf_fix_symbol_flags
55 PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *));
56 static boolean elf_adjust_dynamic_symbol
57 PARAMS ((struct elf_link_hash_entry *, PTR));
58 static boolean elf_link_find_version_dependencies
59 PARAMS ((struct elf_link_hash_entry *, PTR));
60 static boolean elf_link_find_version_dependencies
61 PARAMS ((struct elf_link_hash_entry *, PTR));
62 static boolean elf_link_assign_sym_version
63 PARAMS ((struct elf_link_hash_entry *, PTR));
64 static boolean elf_collect_hash_codes
65 PARAMS ((struct elf_link_hash_entry *, PTR));
66 static boolean elf_link_read_relocs_from_section
67 PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *));
68 static size_t compute_bucket_count
69 PARAMS ((struct bfd_link_info *));
70 static void elf_link_output_relocs
71 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
72 static boolean elf_link_size_reloc_section
73 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
74 static void elf_link_adjust_relocs
75 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int,
76 struct elf_link_hash_entry **));
77 static int elf_link_sort_cmp1
78 PARAMS ((const void *, const void *));
79 static int elf_link_sort_cmp2
80 PARAMS ((const void *, const void *));
81 static size_t elf_link_sort_relocs
82 PARAMS ((bfd *, struct bfd_link_info *, asection **));
83 static boolean elf_section_ignore_discarded_relocs
84 PARAMS ((asection *));
86 /* Given an ELF BFD, add symbols to the global hash table as
90 elf_bfd_link_add_symbols (abfd, info)
92 struct bfd_link_info *info;
94 switch (bfd_get_format (abfd))
97 return elf_link_add_object_symbols (abfd, info);
99 return elf_link_add_archive_symbols (abfd, info);
101 bfd_set_error (bfd_error_wrong_format);
106 /* Return true iff this is a non-common, definition of a non-function symbol. */
108 is_global_data_symbol_definition (abfd, sym)
109 bfd * abfd ATTRIBUTE_UNUSED;
110 Elf_Internal_Sym * sym;
112 /* Local symbols do not count, but target specific ones might. */
113 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
114 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
117 /* Function symbols do not count. */
118 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
121 /* If the section is undefined, then so is the symbol. */
122 if (sym->st_shndx == SHN_UNDEF)
125 /* If the symbol is defined in the common section, then
126 it is a common definition and so does not count. */
127 if (sym->st_shndx == SHN_COMMON)
130 /* If the symbol is in a target specific section then we
131 must rely upon the backend to tell us what it is. */
132 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
133 /* FIXME - this function is not coded yet:
135 return _bfd_is_global_symbol_definition (abfd, sym);
137 Instead for now assume that the definition is not global,
138 Even if this is wrong, at least the linker will behave
139 in the same way that it used to do. */
145 /* Search the symbol table of the archive element of the archive ABFD
146 whose archive map contains a mention of SYMDEF, and determine if
147 the symbol is defined in this element. */
149 elf_link_is_defined_archive_symbol (abfd, symdef)
153 Elf_Internal_Shdr * hdr;
154 Elf_External_Sym * esym;
155 Elf_External_Sym * esymend;
156 Elf_External_Sym * buf = NULL;
157 bfd_size_type symcount;
158 bfd_size_type extsymcount;
159 bfd_size_type extsymoff;
160 boolean result = false;
164 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
165 if (abfd == (bfd *) NULL)
168 if (! bfd_check_format (abfd, bfd_object))
171 /* If we have already included the element containing this symbol in the
172 link then we do not need to include it again. Just claim that any symbol
173 it contains is not a definition, so that our caller will not decide to
174 (re)include this element. */
175 if (abfd->archive_pass)
178 /* Select the appropriate symbol table. */
179 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
180 hdr = &elf_tdata (abfd)->symtab_hdr;
182 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
184 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
186 /* The sh_info field of the symtab header tells us where the
187 external symbols start. We don't care about the local symbols. */
188 if (elf_bad_symtab (abfd))
190 extsymcount = symcount;
195 extsymcount = symcount - hdr->sh_info;
196 extsymoff = hdr->sh_info;
199 amt = extsymcount * sizeof (Elf_External_Sym);
200 buf = (Elf_External_Sym *) bfd_malloc (amt);
201 if (buf == NULL && extsymcount != 0)
204 /* Read in the symbol table.
205 FIXME: This ought to be cached somewhere. */
206 pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
207 if (bfd_seek (abfd, pos, SEEK_SET) != 0
208 || bfd_bread ((PTR) buf, amt, abfd) != amt)
214 /* Scan the symbol table looking for SYMDEF. */
215 esymend = buf + extsymcount;
220 Elf_Internal_Sym sym;
223 elf_swap_symbol_in (abfd, esym, & sym);
225 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
226 if (name == (const char *) NULL)
229 if (strcmp (name, symdef->name) == 0)
231 result = is_global_data_symbol_definition (abfd, & sym);
241 /* Add symbols from an ELF archive file to the linker hash table. We
242 don't use _bfd_generic_link_add_archive_symbols because of a
243 problem which arises on UnixWare. The UnixWare libc.so is an
244 archive which includes an entry libc.so.1 which defines a bunch of
245 symbols. The libc.so archive also includes a number of other
246 object files, which also define symbols, some of which are the same
247 as those defined in libc.so.1. Correct linking requires that we
248 consider each object file in turn, and include it if it defines any
249 symbols we need. _bfd_generic_link_add_archive_symbols does not do
250 this; it looks through the list of undefined symbols, and includes
251 any object file which defines them. When this algorithm is used on
252 UnixWare, it winds up pulling in libc.so.1 early and defining a
253 bunch of symbols. This means that some of the other objects in the
254 archive are not included in the link, which is incorrect since they
255 precede libc.so.1 in the archive.
257 Fortunately, ELF archive handling is simpler than that done by
258 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
259 oddities. In ELF, if we find a symbol in the archive map, and the
260 symbol is currently undefined, we know that we must pull in that
263 Unfortunately, we do have to make multiple passes over the symbol
264 table until nothing further is resolved. */
267 elf_link_add_archive_symbols (abfd, info)
269 struct bfd_link_info *info;
272 boolean *defined = NULL;
273 boolean *included = NULL;
278 if (! bfd_has_map (abfd))
280 /* An empty archive is a special case. */
281 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
283 bfd_set_error (bfd_error_no_armap);
287 /* Keep track of all symbols we know to be already defined, and all
288 files we know to be already included. This is to speed up the
289 second and subsequent passes. */
290 c = bfd_ardata (abfd)->symdef_count;
294 amt *= sizeof (boolean);
295 defined = (boolean *) bfd_malloc (amt);
296 included = (boolean *) bfd_malloc (amt);
297 if (defined == (boolean *) NULL || included == (boolean *) NULL)
299 memset (defined, 0, (size_t) amt);
300 memset (included, 0, (size_t) amt);
302 symdefs = bfd_ardata (abfd)->symdefs;
315 symdefend = symdef + c;
316 for (i = 0; symdef < symdefend; symdef++, i++)
318 struct elf_link_hash_entry *h;
320 struct bfd_link_hash_entry *undefs_tail;
323 if (defined[i] || included[i])
325 if (symdef->file_offset == last)
331 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
332 false, false, false);
338 /* If this is a default version (the name contains @@),
339 look up the symbol again without the version. The
340 effect is that references to the symbol without the
341 version will be matched by the default symbol in the
344 p = strchr (symdef->name, ELF_VER_CHR);
345 if (p == NULL || p[1] != ELF_VER_CHR)
348 copy = bfd_alloc (abfd, (bfd_size_type) (p - symdef->name + 1));
351 memcpy (copy, symdef->name, (size_t) (p - symdef->name));
352 copy[p - symdef->name] = '\0';
354 h = elf_link_hash_lookup (elf_hash_table (info), copy,
355 false, false, false);
357 bfd_release (abfd, copy);
363 if (h->root.type == bfd_link_hash_common)
365 /* We currently have a common symbol. The archive map contains
366 a reference to this symbol, so we may want to include it. We
367 only want to include it however, if this archive element
368 contains a definition of the symbol, not just another common
371 Unfortunately some archivers (including GNU ar) will put
372 declarations of common symbols into their archive maps, as
373 well as real definitions, so we cannot just go by the archive
374 map alone. Instead we must read in the element's symbol
375 table and check that to see what kind of symbol definition
377 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
380 else if (h->root.type != bfd_link_hash_undefined)
382 if (h->root.type != bfd_link_hash_undefweak)
387 /* We need to include this archive member. */
388 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
389 if (element == (bfd *) NULL)
392 if (! bfd_check_format (element, bfd_object))
395 /* Doublecheck that we have not included this object
396 already--it should be impossible, but there may be
397 something wrong with the archive. */
398 if (element->archive_pass != 0)
400 bfd_set_error (bfd_error_bad_value);
403 element->archive_pass = 1;
405 undefs_tail = info->hash->undefs_tail;
407 if (! (*info->callbacks->add_archive_element) (info, element,
410 if (! elf_link_add_object_symbols (element, info))
413 /* If there are any new undefined symbols, we need to make
414 another pass through the archive in order to see whether
415 they can be defined. FIXME: This isn't perfect, because
416 common symbols wind up on undefs_tail and because an
417 undefined symbol which is defined later on in this pass
418 does not require another pass. This isn't a bug, but it
419 does make the code less efficient than it could be. */
420 if (undefs_tail != info->hash->undefs_tail)
423 /* Look backward to mark all symbols from this object file
424 which we have already seen in this pass. */
428 included[mark] = true;
433 while (symdefs[mark].file_offset == symdef->file_offset);
435 /* We mark subsequent symbols from this object file as we go
436 on through the loop. */
437 last = symdef->file_offset;
448 if (defined != (boolean *) NULL)
450 if (included != (boolean *) NULL)
455 /* This function is called when we want to define a new symbol. It
456 handles the various cases which arise when we find a definition in
457 a dynamic object, or when there is already a definition in a
458 dynamic object. The new symbol is described by NAME, SYM, PSEC,
459 and PVALUE. We set SYM_HASH to the hash table entry. We set
460 OVERRIDE if the old symbol is overriding a new definition. We set
461 TYPE_CHANGE_OK if it is OK for the type to change. We set
462 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
463 change, we mean that we shouldn't warn if the type or size does
464 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
468 elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
469 override, type_change_ok, size_change_ok, dt_needed)
471 struct bfd_link_info *info;
473 Elf_Internal_Sym *sym;
476 struct elf_link_hash_entry **sym_hash;
478 boolean *type_change_ok;
479 boolean *size_change_ok;
483 struct elf_link_hash_entry *h;
486 boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
491 bind = ELF_ST_BIND (sym->st_info);
493 if (! bfd_is_und_section (sec))
494 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
496 h = ((struct elf_link_hash_entry *)
497 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
502 /* This code is for coping with dynamic objects, and is only useful
503 if we are doing an ELF link. */
504 if (info->hash->creator != abfd->xvec)
507 /* For merging, we only care about real symbols. */
509 while (h->root.type == bfd_link_hash_indirect
510 || h->root.type == bfd_link_hash_warning)
511 h = (struct elf_link_hash_entry *) h->root.u.i.link;
513 /* If we just created the symbol, mark it as being an ELF symbol.
514 Other than that, there is nothing to do--there is no merge issue
515 with a newly defined symbol--so we just return. */
517 if (h->root.type == bfd_link_hash_new)
519 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
523 /* OLDBFD is a BFD associated with the existing symbol. */
525 switch (h->root.type)
531 case bfd_link_hash_undefined:
532 case bfd_link_hash_undefweak:
533 oldbfd = h->root.u.undef.abfd;
536 case bfd_link_hash_defined:
537 case bfd_link_hash_defweak:
538 oldbfd = h->root.u.def.section->owner;
541 case bfd_link_hash_common:
542 oldbfd = h->root.u.c.p->section->owner;
546 /* In cases involving weak versioned symbols, we may wind up trying
547 to merge a symbol with itself. Catch that here, to avoid the
548 confusion that results if we try to override a symbol with
549 itself. The additional tests catch cases like
550 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
551 dynamic object, which we do want to handle here. */
553 && ((abfd->flags & DYNAMIC) == 0
554 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
557 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
558 respectively, is from a dynamic object. */
560 if ((abfd->flags & DYNAMIC) != 0)
566 olddyn = (oldbfd->flags & DYNAMIC) != 0;
571 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
572 indices used by MIPS ELF. */
573 switch (h->root.type)
579 case bfd_link_hash_defined:
580 case bfd_link_hash_defweak:
581 hsec = h->root.u.def.section;
584 case bfd_link_hash_common:
585 hsec = h->root.u.c.p->section;
592 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
595 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
596 respectively, appear to be a definition rather than reference. */
598 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
603 if (h->root.type == bfd_link_hash_undefined
604 || h->root.type == bfd_link_hash_undefweak
605 || h->root.type == bfd_link_hash_common)
610 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
611 symbol, respectively, appears to be a common symbol in a dynamic
612 object. If a symbol appears in an uninitialized section, and is
613 not weak, and is not a function, then it may be a common symbol
614 which was resolved when the dynamic object was created. We want
615 to treat such symbols specially, because they raise special
616 considerations when setting the symbol size: if the symbol
617 appears as a common symbol in a regular object, and the size in
618 the regular object is larger, we must make sure that we use the
619 larger size. This problematic case can always be avoided in C,
620 but it must be handled correctly when using Fortran shared
623 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
624 likewise for OLDDYNCOMMON and OLDDEF.
626 Note that this test is just a heuristic, and that it is quite
627 possible to have an uninitialized symbol in a shared object which
628 is really a definition, rather than a common symbol. This could
629 lead to some minor confusion when the symbol really is a common
630 symbol in some regular object. However, I think it will be
635 && (sec->flags & SEC_ALLOC) != 0
636 && (sec->flags & SEC_LOAD) == 0
639 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
642 newdyncommon = false;
646 && h->root.type == bfd_link_hash_defined
647 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
648 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
649 && (h->root.u.def.section->flags & SEC_LOAD) == 0
651 && h->type != STT_FUNC)
654 olddyncommon = false;
656 /* It's OK to change the type if either the existing symbol or the
657 new symbol is weak unless it comes from a DT_NEEDED entry of
658 a shared object, in which case, the DT_NEEDED entry may not be
659 required at the run time. */
661 if ((! dt_needed && h->root.type == bfd_link_hash_defweak)
662 || h->root.type == bfd_link_hash_undefweak
664 *type_change_ok = true;
666 /* It's OK to change the size if either the existing symbol or the
667 new symbol is weak, or if the old symbol is undefined. */
670 || h->root.type == bfd_link_hash_undefined)
671 *size_change_ok = true;
673 /* If both the old and the new symbols look like common symbols in a
674 dynamic object, set the size of the symbol to the larger of the
679 && sym->st_size != h->size)
681 /* Since we think we have two common symbols, issue a multiple
682 common warning if desired. Note that we only warn if the
683 size is different. If the size is the same, we simply let
684 the old symbol override the new one as normally happens with
685 symbols defined in dynamic objects. */
687 if (! ((*info->callbacks->multiple_common)
688 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
689 h->size, abfd, bfd_link_hash_common, sym->st_size)))
692 if (sym->st_size > h->size)
693 h->size = sym->st_size;
695 *size_change_ok = true;
698 /* If we are looking at a dynamic object, and we have found a
699 definition, we need to see if the symbol was already defined by
700 some other object. If so, we want to use the existing
701 definition, and we do not want to report a multiple symbol
702 definition error; we do this by clobbering *PSEC to be
705 We treat a common symbol as a definition if the symbol in the
706 shared library is a function, since common symbols always
707 represent variables; this can cause confusion in principle, but
708 any such confusion would seem to indicate an erroneous program or
709 shared library. We also permit a common symbol in a regular
710 object to override a weak symbol in a shared object.
712 We prefer a non-weak definition in a shared library to a weak
713 definition in the executable unless it comes from a DT_NEEDED
714 entry of a shared object, in which case, the DT_NEEDED entry
715 may not be required at the run time. */
720 || (h->root.type == bfd_link_hash_common
722 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
723 && (h->root.type != bfd_link_hash_defweak
725 || bind == STB_WEAK))
729 newdyncommon = false;
731 *psec = sec = bfd_und_section_ptr;
732 *size_change_ok = true;
734 /* If we get here when the old symbol is a common symbol, then
735 we are explicitly letting it override a weak symbol or
736 function in a dynamic object, and we don't want to warn about
737 a type change. If the old symbol is a defined symbol, a type
738 change warning may still be appropriate. */
740 if (h->root.type == bfd_link_hash_common)
741 *type_change_ok = true;
744 /* Handle the special case of an old common symbol merging with a
745 new symbol which looks like a common symbol in a shared object.
746 We change *PSEC and *PVALUE to make the new symbol look like a
747 common symbol, and let _bfd_generic_link_add_one_symbol will do
751 && h->root.type == bfd_link_hash_common)
755 newdyncommon = false;
756 *pvalue = sym->st_size;
757 *psec = sec = bfd_com_section_ptr;
758 *size_change_ok = true;
761 /* If the old symbol is from a dynamic object, and the new symbol is
762 a definition which is not from a dynamic object, then the new
763 symbol overrides the old symbol. Symbols from regular files
764 always take precedence over symbols from dynamic objects, even if
765 they are defined after the dynamic object in the link.
767 As above, we again permit a common symbol in a regular object to
768 override a definition in a shared object if the shared object
769 symbol is a function or is weak.
771 As above, we permit a non-weak definition in a shared object to
772 override a weak definition in a regular object. */
776 || (bfd_is_com_section (sec)
777 && (h->root.type == bfd_link_hash_defweak
778 || h->type == STT_FUNC)))
781 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
783 || h->root.type == bfd_link_hash_defweak))
785 /* Change the hash table entry to undefined, and let
786 _bfd_generic_link_add_one_symbol do the right thing with the
789 h->root.type = bfd_link_hash_undefined;
790 h->root.u.undef.abfd = h->root.u.def.section->owner;
791 *size_change_ok = true;
794 olddyncommon = false;
796 /* We again permit a type change when a common symbol may be
797 overriding a function. */
799 if (bfd_is_com_section (sec))
800 *type_change_ok = true;
802 /* This union may have been set to be non-NULL when this symbol
803 was seen in a dynamic object. We must force the union to be
804 NULL, so that it is correct for a regular symbol. */
806 h->verinfo.vertree = NULL;
808 /* In this special case, if H is the target of an indirection,
809 we want the caller to frob with H rather than with the
810 indirect symbol. That will permit the caller to redefine the
811 target of the indirection, rather than the indirect symbol
812 itself. FIXME: This will break the -y option if we store a
813 symbol with a different name. */
817 /* Handle the special case of a new common symbol merging with an
818 old symbol that looks like it might be a common symbol defined in
819 a shared object. Note that we have already handled the case in
820 which a new common symbol should simply override the definition
821 in the shared library. */
824 && bfd_is_com_section (sec)
827 /* It would be best if we could set the hash table entry to a
828 common symbol, but we don't know what to use for the section
830 if (! ((*info->callbacks->multiple_common)
831 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
832 h->size, abfd, bfd_link_hash_common, sym->st_size)))
835 /* If the predumed common symbol in the dynamic object is
836 larger, pretend that the new symbol has its size. */
838 if (h->size > *pvalue)
841 /* FIXME: We no longer know the alignment required by the symbol
842 in the dynamic object, so we just wind up using the one from
843 the regular object. */
846 olddyncommon = false;
848 h->root.type = bfd_link_hash_undefined;
849 h->root.u.undef.abfd = h->root.u.def.section->owner;
851 *size_change_ok = true;
852 *type_change_ok = true;
854 h->verinfo.vertree = NULL;
857 /* Handle the special case of a weak definition in a regular object
858 followed by a non-weak definition in a shared object. In this
859 case, we prefer the definition in the shared object unless it
860 comes from a DT_NEEDED entry of a shared object, in which case,
861 the DT_NEEDED entry may not be required at the run time. */
864 && h->root.type == bfd_link_hash_defweak
869 /* To make this work we have to frob the flags so that the rest
870 of the code does not think we are using the regular
872 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
873 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
874 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
875 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
876 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
877 | ELF_LINK_HASH_DEF_DYNAMIC);
879 /* If H is the target of an indirection, we want the caller to
880 use H rather than the indirect symbol. Otherwise if we are
881 defining a new indirect symbol we will wind up attaching it
882 to the entry we are overriding. */
886 /* Handle the special case of a non-weak definition in a shared
887 object followed by a weak definition in a regular object. In
888 this case we prefer to definition in the shared object. To make
889 this work we have to tell the caller to not treat the new symbol
893 && h->root.type != bfd_link_hash_defweak
902 /* This function is called to create an indirect symbol from the
903 default for the symbol with the default version if needed. The
904 symbol is described by H, NAME, SYM, SEC, VALUE, and OVERRIDE. We
905 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
906 indicates if it comes from a DT_NEEDED entry of a shared object. */
909 elf_add_default_symbol (abfd, info, h, name, sym, sec, value,
910 dynsym, override, dt_needed)
912 struct bfd_link_info *info;
913 struct elf_link_hash_entry *h;
915 Elf_Internal_Sym *sym;
922 boolean type_change_ok;
923 boolean size_change_ok;
925 struct elf_link_hash_entry *hi;
926 struct elf_backend_data *bed;
931 /* If this symbol has a version, and it is the default version, we
932 create an indirect symbol from the default name to the fully
933 decorated name. This will cause external references which do not
934 specify a version to be bound to this version of the symbol. */
935 p = strchr (name, ELF_VER_CHR);
936 if (p == NULL || p[1] != ELF_VER_CHR)
941 /* We are overridden by an old defition. We need to check if we
942 need to crreate the indirect symbol from the default name. */
943 hi = elf_link_hash_lookup (elf_hash_table (info), name, true,
945 BFD_ASSERT (hi != NULL);
948 while (hi->root.type == bfd_link_hash_indirect
949 || hi->root.type == bfd_link_hash_warning)
951 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
957 bed = get_elf_backend_data (abfd);
958 collect = bed->collect;
959 dynamic = (abfd->flags & DYNAMIC) != 0;
961 shortname = bfd_hash_allocate (&info->hash->table,
962 (size_t) (p - name + 1));
963 if (shortname == NULL)
965 strncpy (shortname, name, (size_t) (p - name));
966 shortname [p - name] = '\0';
968 /* We are going to create a new symbol. Merge it with any existing
969 symbol with this name. For the purposes of the merge, act as
970 though we were defining the symbol we just defined, although we
971 actually going to define an indirect symbol. */
972 type_change_ok = false;
973 size_change_ok = false;
974 if (! elf_merge_symbol (abfd, info, shortname, sym, sec, value,
975 &hi, &override, &type_change_ok,
976 &size_change_ok, dt_needed))
981 if (! (_bfd_generic_link_add_one_symbol
982 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
983 (bfd_vma) 0, name, false, collect,
984 (struct bfd_link_hash_entry **) &hi)))
989 /* In this case the symbol named SHORTNAME is overriding the
990 indirect symbol we want to add. We were planning on making
991 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
992 is the name without a version. NAME is the fully versioned
993 name, and it is the default version.
995 Overriding means that we already saw a definition for the
996 symbol SHORTNAME in a regular object, and it is overriding
997 the symbol defined in the dynamic object.
999 When this happens, we actually want to change NAME, the
1000 symbol we just added, to refer to SHORTNAME. This will cause
1001 references to NAME in the shared object to become references
1002 to SHORTNAME in the regular object. This is what we expect
1003 when we override a function in a shared object: that the
1004 references in the shared object will be mapped to the
1005 definition in the regular object. */
1007 while (hi->root.type == bfd_link_hash_indirect
1008 || hi->root.type == bfd_link_hash_warning)
1009 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1011 h->root.type = bfd_link_hash_indirect;
1012 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1013 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1015 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1016 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1017 if (hi->elf_link_hash_flags
1018 & (ELF_LINK_HASH_REF_REGULAR
1019 | ELF_LINK_HASH_DEF_REGULAR))
1021 if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1026 /* Now set HI to H, so that the following code will set the
1027 other fields correctly. */
1031 /* If there is a duplicate definition somewhere, then HI may not
1032 point to an indirect symbol. We will have reported an error to
1033 the user in that case. */
1035 if (hi->root.type == bfd_link_hash_indirect)
1037 struct elf_link_hash_entry *ht;
1039 /* If the symbol became indirect, then we assume that we have
1040 not seen a definition before. */
1041 BFD_ASSERT ((hi->elf_link_hash_flags
1042 & (ELF_LINK_HASH_DEF_DYNAMIC
1043 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1045 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1046 (*bed->elf_backend_copy_indirect_symbol) (ht, hi);
1048 /* See if the new flags lead us to realize that the symbol must
1055 || ((hi->elf_link_hash_flags
1056 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1061 if ((hi->elf_link_hash_flags
1062 & ELF_LINK_HASH_REF_REGULAR) != 0)
1068 /* We also need to define an indirection from the nondefault version
1071 shortname = bfd_hash_allocate (&info->hash->table, strlen (name));
1072 if (shortname == NULL)
1074 strncpy (shortname, name, (size_t) (p - name));
1075 strcpy (shortname + (p - name), p + 1);
1077 /* Once again, merge with any existing symbol. */
1078 type_change_ok = false;
1079 size_change_ok = false;
1080 if (! elf_merge_symbol (abfd, info, shortname, sym, sec, value,
1081 &hi, &override, &type_change_ok,
1082 &size_change_ok, dt_needed))
1087 /* Here SHORTNAME is a versioned name, so we don't expect to see
1088 the type of override we do in the case above. */
1089 (*_bfd_error_handler)
1090 (_("%s: warning: unexpected redefinition of `%s'"),
1091 bfd_archive_filename (abfd), shortname);
1095 if (! (_bfd_generic_link_add_one_symbol
1096 (info, abfd, shortname, BSF_INDIRECT,
1097 bfd_ind_section_ptr, (bfd_vma) 0, name, false,
1098 collect, (struct bfd_link_hash_entry **) &hi)))
1101 /* If there is a duplicate definition somewhere, then HI may not
1102 point to an indirect symbol. We will have reported an error
1103 to the user in that case. */
1105 if (hi->root.type == bfd_link_hash_indirect)
1107 /* If the symbol became indirect, then we assume that we have
1108 not seen a definition before. */
1109 BFD_ASSERT ((hi->elf_link_hash_flags
1110 & (ELF_LINK_HASH_DEF_DYNAMIC
1111 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1113 (*bed->elf_backend_copy_indirect_symbol) (h, hi);
1115 /* See if the new flags lead us to realize that the symbol
1122 || ((hi->elf_link_hash_flags
1123 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1128 if ((hi->elf_link_hash_flags
1129 & ELF_LINK_HASH_REF_REGULAR) != 0)
1139 /* Add symbols from an ELF object file to the linker hash table. */
1142 elf_link_add_object_symbols (abfd, info)
1144 struct bfd_link_info *info;
1146 boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
1147 const Elf_Internal_Sym *,
1148 const char **, flagword *,
1149 asection **, bfd_vma *));
1150 boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *,
1151 asection *, const Elf_Internal_Rela *));
1153 Elf_Internal_Shdr *hdr;
1154 bfd_size_type symcount;
1155 bfd_size_type extsymcount;
1156 bfd_size_type extsymoff;
1157 Elf_External_Sym *buf = NULL;
1158 struct elf_link_hash_entry **sym_hash;
1160 Elf_External_Versym *extversym = NULL;
1161 Elf_External_Versym *ever;
1162 Elf_External_Dyn *dynbuf = NULL;
1163 struct elf_link_hash_entry *weaks;
1164 Elf_External_Sym *esym;
1165 Elf_External_Sym *esymend;
1166 struct elf_backend_data *bed;
1168 struct elf_link_hash_table * hash_table;
1172 hash_table = elf_hash_table (info);
1174 bed = get_elf_backend_data (abfd);
1175 add_symbol_hook = bed->elf_add_symbol_hook;
1176 collect = bed->collect;
1178 if ((abfd->flags & DYNAMIC) == 0)
1184 /* You can't use -r against a dynamic object. Also, there's no
1185 hope of using a dynamic object which does not exactly match
1186 the format of the output file. */
1187 if (info->relocateable || info->hash->creator != abfd->xvec)
1189 bfd_set_error (bfd_error_invalid_operation);
1194 /* As a GNU extension, any input sections which are named
1195 .gnu.warning.SYMBOL are treated as warning symbols for the given
1196 symbol. This differs from .gnu.warning sections, which generate
1197 warnings when they are included in an output file. */
1202 for (s = abfd->sections; s != NULL; s = s->next)
1206 name = bfd_get_section_name (abfd, s);
1207 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1212 name += sizeof ".gnu.warning." - 1;
1214 /* If this is a shared object, then look up the symbol
1215 in the hash table. If it is there, and it is already
1216 been defined, then we will not be using the entry
1217 from this shared object, so we don't need to warn.
1218 FIXME: If we see the definition in a regular object
1219 later on, we will warn, but we shouldn't. The only
1220 fix is to keep track of what warnings we are supposed
1221 to emit, and then handle them all at the end of the
1223 if (dynamic && abfd->xvec == info->hash->creator)
1225 struct elf_link_hash_entry *h;
1227 h = elf_link_hash_lookup (hash_table, name,
1228 false, false, true);
1230 /* FIXME: What about bfd_link_hash_common? */
1232 && (h->root.type == bfd_link_hash_defined
1233 || h->root.type == bfd_link_hash_defweak))
1235 /* We don't want to issue this warning. Clobber
1236 the section size so that the warning does not
1237 get copied into the output file. */
1243 sz = bfd_section_size (abfd, s);
1244 msg = (char *) bfd_alloc (abfd, sz + 1);
1248 if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
1253 if (! (_bfd_generic_link_add_one_symbol
1254 (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
1255 false, collect, (struct bfd_link_hash_entry **) NULL)))
1258 if (! info->relocateable)
1260 /* Clobber the section size so that the warning does
1261 not get copied into the output file. */
1268 /* If this is a dynamic object, we always link against the .dynsym
1269 symbol table, not the .symtab symbol table. The dynamic linker
1270 will only see the .dynsym symbol table, so there is no reason to
1271 look at .symtab for a dynamic object. */
1273 if (! dynamic || elf_dynsymtab (abfd) == 0)
1274 hdr = &elf_tdata (abfd)->symtab_hdr;
1276 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1280 /* Read in any version definitions. */
1282 if (! _bfd_elf_slurp_version_tables (abfd))
1285 /* Read in the symbol versions, but don't bother to convert them
1286 to internal format. */
1287 if (elf_dynversym (abfd) != 0)
1289 Elf_Internal_Shdr *versymhdr;
1291 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
1292 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
1293 if (extversym == NULL)
1295 amt = versymhdr->sh_size;
1296 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
1297 || bfd_bread ((PTR) extversym, amt, abfd) != amt)
1302 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1304 /* The sh_info field of the symtab header tells us where the
1305 external symbols start. We don't care about the local symbols at
1307 if (elf_bad_symtab (abfd))
1309 extsymcount = symcount;
1314 extsymcount = symcount - hdr->sh_info;
1315 extsymoff = hdr->sh_info;
1318 amt = extsymcount * sizeof (Elf_External_Sym);
1319 buf = (Elf_External_Sym *) bfd_malloc (amt);
1320 if (buf == NULL && extsymcount != 0)
1323 /* We store a pointer to the hash table entry for each external
1325 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
1326 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
1327 if (sym_hash == NULL)
1329 elf_sym_hashes (abfd) = sym_hash;
1335 /* If we are creating a shared library, create all the dynamic
1336 sections immediately. We need to attach them to something,
1337 so we attach them to this BFD, provided it is the right
1338 format. FIXME: If there are no input BFD's of the same
1339 format as the output, we can't make a shared library. */
1341 && is_elf_hash_table (info)
1342 && ! hash_table->dynamic_sections_created
1343 && abfd->xvec == info->hash->creator)
1345 if (! elf_link_create_dynamic_sections (abfd, info))
1349 else if (! is_elf_hash_table (info))
1356 bfd_size_type oldsize;
1357 bfd_size_type strindex;
1359 /* Find the name to use in a DT_NEEDED entry that refers to this
1360 object. If the object has a DT_SONAME entry, we use it.
1361 Otherwise, if the generic linker stuck something in
1362 elf_dt_name, we use that. Otherwise, we just use the file
1363 name. If the generic linker put a null string into
1364 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1365 there is a DT_SONAME entry. */
1367 name = bfd_get_filename (abfd);
1368 if (elf_dt_name (abfd) != NULL)
1370 name = elf_dt_name (abfd);
1373 if (elf_dt_soname (abfd) != NULL)
1379 s = bfd_get_section_by_name (abfd, ".dynamic");
1382 Elf_External_Dyn *extdyn;
1383 Elf_External_Dyn *extdynend;
1385 unsigned long shlink;
1389 dynbuf = (Elf_External_Dyn *) bfd_malloc (s->_raw_size);
1393 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
1394 (file_ptr) 0, s->_raw_size))
1397 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1400 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1403 /* The shared libraries distributed with hpux11 have a bogus
1404 sh_link field for the ".dynamic" section. This code detects
1405 when SHLINK refers to a section that is not a string table
1406 and tries to find the string table for the ".dynsym" section
1408 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[shlink];
1409 if (shdr->sh_type != SHT_STRTAB)
1411 asection *ds = bfd_get_section_by_name (abfd, ".dynsym");
1412 int elfdsec = _bfd_elf_section_from_bfd_section (abfd, ds);
1415 shlink = elf_elfsections (abfd)[elfdsec]->sh_link;
1420 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
1423 for (; extdyn < extdynend; extdyn++)
1425 Elf_Internal_Dyn dyn;
1427 elf_swap_dyn_in (abfd, extdyn, &dyn);
1428 if (dyn.d_tag == DT_SONAME)
1430 unsigned int tagv = dyn.d_un.d_val;
1431 name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1435 if (dyn.d_tag == DT_NEEDED)
1437 struct bfd_link_needed_list *n, **pn;
1439 unsigned int tagv = dyn.d_un.d_val;
1441 amt = sizeof (struct bfd_link_needed_list);
1442 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1443 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1444 if (n == NULL || fnm == NULL)
1446 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1453 for (pn = & hash_table->needed;
1459 if (dyn.d_tag == DT_RUNPATH)
1461 struct bfd_link_needed_list *n, **pn;
1463 unsigned int tagv = dyn.d_un.d_val;
1465 /* When we see DT_RPATH before DT_RUNPATH, we have
1466 to clear runpath. Do _NOT_ bfd_release, as that
1467 frees all more recently bfd_alloc'd blocks as
1469 if (rpath && hash_table->runpath)
1470 hash_table->runpath = NULL;
1472 amt = sizeof (struct bfd_link_needed_list);
1473 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1474 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1475 if (n == NULL || fnm == NULL)
1477 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1484 for (pn = & hash_table->runpath;
1492 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1493 if (!runpath && dyn.d_tag == DT_RPATH)
1495 struct bfd_link_needed_list *n, **pn;
1497 unsigned int tagv = dyn.d_un.d_val;
1499 amt = sizeof (struct bfd_link_needed_list);
1500 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1501 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1502 if (n == NULL || fnm == NULL)
1504 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1511 for (pn = & hash_table->runpath;
1524 /* We do not want to include any of the sections in a dynamic
1525 object in the output file. We hack by simply clobbering the
1526 list of sections in the BFD. This could be handled more
1527 cleanly by, say, a new section flag; the existing
1528 SEC_NEVER_LOAD flag is not the one we want, because that one
1529 still implies that the section takes up space in the output
1531 abfd->sections = NULL;
1532 abfd->section_count = 0;
1534 /* If this is the first dynamic object found in the link, create
1535 the special sections required for dynamic linking. */
1536 if (! hash_table->dynamic_sections_created)
1537 if (! elf_link_create_dynamic_sections (abfd, info))
1542 /* Add a DT_NEEDED entry for this dynamic object. */
1543 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
1544 strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, false);
1545 if (strindex == (bfd_size_type) -1)
1548 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
1551 Elf_External_Dyn *dyncon, *dynconend;
1553 /* The hash table size did not change, which means that
1554 the dynamic object name was already entered. If we
1555 have already included this dynamic object in the
1556 link, just ignore it. There is no reason to include
1557 a particular dynamic object more than once. */
1558 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
1559 BFD_ASSERT (sdyn != NULL);
1561 dyncon = (Elf_External_Dyn *) sdyn->contents;
1562 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1564 for (; dyncon < dynconend; dyncon++)
1566 Elf_Internal_Dyn dyn;
1568 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
1569 if (dyn.d_tag == DT_NEEDED
1570 && dyn.d_un.d_val == strindex)
1574 if (extversym != NULL)
1576 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
1582 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
1586 /* Save the SONAME, if there is one, because sometimes the
1587 linker emulation code will need to know it. */
1589 name = basename (bfd_get_filename (abfd));
1590 elf_dt_name (abfd) = name;
1593 pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
1594 amt = extsymcount * sizeof (Elf_External_Sym);
1595 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1596 || bfd_bread ((PTR) buf, amt, abfd) != amt)
1601 ever = extversym != NULL ? extversym + extsymoff : NULL;
1602 esymend = buf + extsymcount;
1605 esym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
1607 Elf_Internal_Sym sym;
1613 struct elf_link_hash_entry *h;
1615 boolean size_change_ok, type_change_ok;
1616 boolean new_weakdef;
1617 unsigned int old_alignment;
1622 elf_swap_symbol_in (abfd, esym, &sym);
1624 flags = BSF_NO_FLAGS;
1626 value = sym.st_value;
1629 bind = ELF_ST_BIND (sym.st_info);
1630 if (bind == STB_LOCAL)
1632 /* This should be impossible, since ELF requires that all
1633 global symbols follow all local symbols, and that sh_info
1634 point to the first global symbol. Unfortunatealy, Irix 5
1638 else if (bind == STB_GLOBAL)
1640 if (sym.st_shndx != SHN_UNDEF
1641 && sym.st_shndx != SHN_COMMON)
1644 else if (bind == STB_WEAK)
1648 /* Leave it up to the processor backend. */
1651 if (sym.st_shndx == SHN_UNDEF)
1652 sec = bfd_und_section_ptr;
1653 else if (sym.st_shndx > 0 && sym.st_shndx < SHN_LORESERVE)
1655 sec = section_from_elf_index (abfd, sym.st_shndx);
1657 sec = bfd_abs_section_ptr;
1658 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1661 else if (sym.st_shndx == SHN_ABS)
1662 sec = bfd_abs_section_ptr;
1663 else if (sym.st_shndx == SHN_COMMON)
1665 sec = bfd_com_section_ptr;
1666 /* What ELF calls the size we call the value. What ELF
1667 calls the value we call the alignment. */
1668 value = sym.st_size;
1672 /* Leave it up to the processor backend. */
1675 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
1676 if (name == (const char *) NULL)
1679 if (add_symbol_hook)
1681 if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec,
1685 /* The hook function sets the name to NULL if this symbol
1686 should be skipped for some reason. */
1687 if (name == (const char *) NULL)
1691 /* Sanity check that all possibilities were handled. */
1692 if (sec == (asection *) NULL)
1694 bfd_set_error (bfd_error_bad_value);
1698 if (bfd_is_und_section (sec)
1699 || bfd_is_com_section (sec))
1704 size_change_ok = false;
1705 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
1707 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1709 Elf_Internal_Versym iver;
1710 unsigned int vernum = 0;
1714 _bfd_elf_swap_versym_in (abfd, ever, &iver);
1715 vernum = iver.vs_vers & VERSYM_VERSION;
1717 /* If this is a hidden symbol, or if it is not version
1718 1, we append the version name to the symbol name.
1719 However, we do not modify a non-hidden absolute
1720 symbol, because it might be the version symbol
1721 itself. FIXME: What if it isn't? */
1722 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
1723 || (vernum > 1 && ! bfd_is_abs_section (sec)))
1726 unsigned int namelen;
1727 bfd_size_type newlen;
1730 if (sym.st_shndx != SHN_UNDEF)
1732 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
1734 (*_bfd_error_handler)
1735 (_("%s: %s: invalid version %u (max %d)"),
1736 bfd_archive_filename (abfd), name, vernum,
1737 elf_tdata (abfd)->dynverdef_hdr.sh_info);
1738 bfd_set_error (bfd_error_bad_value);
1741 else if (vernum > 1)
1743 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1749 /* We cannot simply test for the number of
1750 entries in the VERNEED section since the
1751 numbers for the needed versions do not start
1753 Elf_Internal_Verneed *t;
1756 for (t = elf_tdata (abfd)->verref;
1760 Elf_Internal_Vernaux *a;
1762 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1764 if (a->vna_other == vernum)
1766 verstr = a->vna_nodename;
1775 (*_bfd_error_handler)
1776 (_("%s: %s: invalid needed version %d"),
1777 bfd_archive_filename (abfd), name, vernum);
1778 bfd_set_error (bfd_error_bad_value);
1783 namelen = strlen (name);
1784 newlen = namelen + strlen (verstr) + 2;
1785 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
1788 newname = (char *) bfd_alloc (abfd, newlen);
1789 if (newname == NULL)
1791 strcpy (newname, name);
1792 p = newname + namelen;
1794 /* If this is a defined non-hidden version symbol,
1795 we add another @ to the name. This indicates the
1796 default version of the symbol. */
1797 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1798 && sym.st_shndx != SHN_UNDEF)
1806 if (! elf_merge_symbol (abfd, info, name, &sym, &sec, &value,
1807 sym_hash, &override, &type_change_ok,
1808 &size_change_ok, dt_needed))
1815 while (h->root.type == bfd_link_hash_indirect
1816 || h->root.type == bfd_link_hash_warning)
1817 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1819 /* Remember the old alignment if this is a common symbol, so
1820 that we don't reduce the alignment later on. We can't
1821 check later, because _bfd_generic_link_add_one_symbol
1822 will set a default for the alignment which we want to
1824 if (h->root.type == bfd_link_hash_common)
1825 old_alignment = h->root.u.c.p->alignment_power;
1827 if (elf_tdata (abfd)->verdef != NULL
1831 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1834 if (! (_bfd_generic_link_add_one_symbol
1835 (info, abfd, name, flags, sec, value, (const char *) NULL,
1836 false, collect, (struct bfd_link_hash_entry **) sym_hash)))
1840 while (h->root.type == bfd_link_hash_indirect
1841 || h->root.type == bfd_link_hash_warning)
1842 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1845 new_weakdef = false;
1848 && (flags & BSF_WEAK) != 0
1849 && ELF_ST_TYPE (sym.st_info) != STT_FUNC
1850 && info->hash->creator->flavour == bfd_target_elf_flavour
1851 && h->weakdef == NULL)
1853 /* Keep a list of all weak defined non function symbols from
1854 a dynamic object, using the weakdef field. Later in this
1855 function we will set the weakdef field to the correct
1856 value. We only put non-function symbols from dynamic
1857 objects on this list, because that happens to be the only
1858 time we need to know the normal symbol corresponding to a
1859 weak symbol, and the information is time consuming to
1860 figure out. If the weakdef field is not already NULL,
1861 then this symbol was already defined by some previous
1862 dynamic object, and we will be using that previous
1863 definition anyhow. */
1870 /* Set the alignment of a common symbol. */
1871 if (sym.st_shndx == SHN_COMMON
1872 && h->root.type == bfd_link_hash_common)
1876 align = bfd_log2 (sym.st_value);
1877 if (align > old_alignment
1878 /* Permit an alignment power of zero if an alignment of one
1879 is specified and no other alignments have been specified. */
1880 || (sym.st_value == 1 && old_alignment == 0))
1881 h->root.u.c.p->alignment_power = align;
1884 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1890 /* Remember the symbol size and type. */
1891 if (sym.st_size != 0
1892 && (definition || h->size == 0))
1894 if (h->size != 0 && h->size != sym.st_size && ! size_change_ok)
1895 (*_bfd_error_handler)
1896 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1897 name, (unsigned long) h->size, (unsigned long) sym.st_size,
1898 bfd_archive_filename (abfd));
1900 h->size = sym.st_size;
1903 /* If this is a common symbol, then we always want H->SIZE
1904 to be the size of the common symbol. The code just above
1905 won't fix the size if a common symbol becomes larger. We
1906 don't warn about a size change here, because that is
1907 covered by --warn-common. */
1908 if (h->root.type == bfd_link_hash_common)
1909 h->size = h->root.u.c.size;
1911 if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE
1912 && (definition || h->type == STT_NOTYPE))
1914 if (h->type != STT_NOTYPE
1915 && h->type != ELF_ST_TYPE (sym.st_info)
1916 && ! type_change_ok)
1917 (*_bfd_error_handler)
1918 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1919 name, h->type, ELF_ST_TYPE (sym.st_info),
1920 bfd_archive_filename (abfd));
1922 h->type = ELF_ST_TYPE (sym.st_info);
1925 /* If st_other has a processor-specific meaning, specific code
1926 might be needed here. */
1927 if (sym.st_other != 0)
1929 /* Combine visibilities, using the most constraining one. */
1930 unsigned char hvis = ELF_ST_VISIBILITY (h->other);
1931 unsigned char symvis = ELF_ST_VISIBILITY (sym.st_other);
1933 if (symvis && (hvis > symvis || hvis == 0))
1934 h->other = sym.st_other;
1936 /* If neither has visibility, use the st_other of the
1937 definition. This is an arbitrary choice, since the
1938 other bits have no general meaning. */
1939 if (!symvis && !hvis
1940 && (definition || h->other == 0))
1941 h->other = sym.st_other;
1944 /* Set a flag in the hash table entry indicating the type of
1945 reference or definition we just found. Keep a count of
1946 the number of dynamic symbols we find. A dynamic symbol
1947 is one which is referenced or defined by both a regular
1948 object and a shared object. */
1949 old_flags = h->elf_link_hash_flags;
1955 new_flag = ELF_LINK_HASH_REF_REGULAR;
1956 if (bind != STB_WEAK)
1957 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
1960 new_flag = ELF_LINK_HASH_DEF_REGULAR;
1962 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
1963 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
1969 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
1971 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
1972 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
1973 | ELF_LINK_HASH_REF_REGULAR)) != 0
1974 || (h->weakdef != NULL
1976 && h->weakdef->dynindx != -1))
1980 h->elf_link_hash_flags |= new_flag;
1982 /* Check to see if we need to add an indirect symbol for
1983 the default name. */
1984 if (definition || h->root.type == bfd_link_hash_common)
1985 if (! elf_add_default_symbol (abfd, info, h, name, &sym,
1986 &sec, &value, &dynsym,
1987 override, dt_needed))
1990 if (dynsym && h->dynindx == -1)
1992 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1994 if (h->weakdef != NULL
1996 && h->weakdef->dynindx == -1)
1998 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2002 else if (dynsym && h->dynindx != -1)
2003 /* If the symbol already has a dynamic index, but
2004 visibility says it should not be visible, turn it into
2006 switch (ELF_ST_VISIBILITY (h->other))
2010 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
2011 (*bed->elf_backend_hide_symbol) (info, h);
2012 _bfd_elf_strtab_delref (hash_table->dynstr,
2017 if (dt_needed && definition
2018 && (h->elf_link_hash_flags
2019 & ELF_LINK_HASH_REF_REGULAR) != 0)
2021 bfd_size_type oldsize;
2022 bfd_size_type strindex;
2024 if (! is_elf_hash_table (info))
2027 /* The symbol from a DT_NEEDED object is referenced from
2028 the regular object to create a dynamic executable. We
2029 have to make sure there is a DT_NEEDED entry for it. */
2032 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2033 strindex = _bfd_elf_strtab_add (hash_table->dynstr,
2034 elf_dt_soname (abfd), false);
2035 if (strindex == (bfd_size_type) -1)
2038 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2041 Elf_External_Dyn *dyncon, *dynconend;
2043 sdyn = bfd_get_section_by_name (hash_table->dynobj,
2045 BFD_ASSERT (sdyn != NULL);
2047 dyncon = (Elf_External_Dyn *) sdyn->contents;
2048 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2050 for (; dyncon < dynconend; dyncon++)
2052 Elf_Internal_Dyn dyn;
2054 elf_swap_dyn_in (hash_table->dynobj,
2056 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
2057 dyn.d_un.d_val != strindex);
2061 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
2067 /* Now set the weakdefs field correctly for all the weak defined
2068 symbols we found. The only way to do this is to search all the
2069 symbols. Since we only need the information for non functions in
2070 dynamic objects, that's the only time we actually put anything on
2071 the list WEAKS. We need this information so that if a regular
2072 object refers to a symbol defined weakly in a dynamic object, the
2073 real symbol in the dynamic object is also put in the dynamic
2074 symbols; we also must arrange for both symbols to point to the
2075 same memory location. We could handle the general case of symbol
2076 aliasing, but a general symbol alias can only be generated in
2077 assembler code, handling it correctly would be very time
2078 consuming, and other ELF linkers don't handle general aliasing
2080 while (weaks != NULL)
2082 struct elf_link_hash_entry *hlook;
2085 struct elf_link_hash_entry **hpp;
2086 struct elf_link_hash_entry **hppend;
2089 weaks = hlook->weakdef;
2090 hlook->weakdef = NULL;
2092 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
2093 || hlook->root.type == bfd_link_hash_defweak
2094 || hlook->root.type == bfd_link_hash_common
2095 || hlook->root.type == bfd_link_hash_indirect);
2096 slook = hlook->root.u.def.section;
2097 vlook = hlook->root.u.def.value;
2099 hpp = elf_sym_hashes (abfd);
2100 hppend = hpp + extsymcount;
2101 for (; hpp < hppend; hpp++)
2103 struct elf_link_hash_entry *h;
2106 if (h != NULL && h != hlook
2107 && h->root.type == bfd_link_hash_defined
2108 && h->root.u.def.section == slook
2109 && h->root.u.def.value == vlook)
2113 /* If the weak definition is in the list of dynamic
2114 symbols, make sure the real definition is put there
2116 if (hlook->dynindx != -1
2117 && h->dynindx == -1)
2119 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2123 /* If the real definition is in the list of dynamic
2124 symbols, make sure the weak definition is put there
2125 as well. If we don't do this, then the dynamic
2126 loader might not merge the entries for the real
2127 definition and the weak definition. */
2128 if (h->dynindx != -1
2129 && hlook->dynindx == -1)
2131 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
2146 if (extversym != NULL)
2152 /* If this object is the same format as the output object, and it is
2153 not a shared library, then let the backend look through the
2156 This is required to build global offset table entries and to
2157 arrange for dynamic relocs. It is not required for the
2158 particular common case of linking non PIC code, even when linking
2159 against shared libraries, but unfortunately there is no way of
2160 knowing whether an object file has been compiled PIC or not.
2161 Looking through the relocs is not particularly time consuming.
2162 The problem is that we must either (1) keep the relocs in memory,
2163 which causes the linker to require additional runtime memory or
2164 (2) read the relocs twice from the input file, which wastes time.
2165 This would be a good case for using mmap.
2167 I have no idea how to handle linking PIC code into a file of a
2168 different format. It probably can't be done. */
2169 check_relocs = get_elf_backend_data (abfd)->check_relocs;
2171 && abfd->xvec == info->hash->creator
2172 && check_relocs != NULL)
2176 for (o = abfd->sections; o != NULL; o = o->next)
2178 Elf_Internal_Rela *internal_relocs;
2181 if ((o->flags & SEC_RELOC) == 0
2182 || o->reloc_count == 0
2183 || ((info->strip == strip_all || info->strip == strip_debugger)
2184 && (o->flags & SEC_DEBUGGING) != 0)
2185 || bfd_is_abs_section (o->output_section))
2188 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
2189 (abfd, o, (PTR) NULL,
2190 (Elf_Internal_Rela *) NULL,
2191 info->keep_memory));
2192 if (internal_relocs == NULL)
2195 ok = (*check_relocs) (abfd, info, o, internal_relocs);
2197 if (! info->keep_memory)
2198 free (internal_relocs);
2205 /* If this is a non-traditional, non-relocateable link, try to
2206 optimize the handling of the .stab/.stabstr sections. */
2208 && ! info->relocateable
2209 && ! info->traditional_format
2210 && info->hash->creator->flavour == bfd_target_elf_flavour
2211 && is_elf_hash_table (info)
2212 && (info->strip != strip_all && info->strip != strip_debugger))
2214 asection *stab, *stabstr;
2216 stab = bfd_get_section_by_name (abfd, ".stab");
2217 if (stab != NULL && !(stab->flags & SEC_MERGE))
2219 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
2221 if (stabstr != NULL)
2223 struct bfd_elf_section_data *secdata;
2225 secdata = elf_section_data (stab);
2226 if (! _bfd_link_section_stabs (abfd,
2227 & hash_table->stab_info,
2229 &secdata->sec_info))
2231 if (secdata->sec_info)
2232 secdata->sec_info_type = ELF_INFO_TYPE_STABS;
2237 if (! info->relocateable && ! dynamic
2238 && is_elf_hash_table (info))
2242 for (s = abfd->sections; s != NULL; s = s->next)
2243 if (s->flags & SEC_MERGE)
2245 struct bfd_elf_section_data *secdata;
2247 secdata = elf_section_data (s);
2248 if (! _bfd_merge_section (abfd,
2249 & hash_table->merge_info,
2250 s, &secdata->sec_info))
2252 else if (secdata->sec_info)
2253 secdata->sec_info_type = ELF_INFO_TYPE_MERGE;
2264 if (extversym != NULL)
2269 /* Create some sections which will be filled in with dynamic linking
2270 information. ABFD is an input file which requires dynamic sections
2271 to be created. The dynamic sections take up virtual memory space
2272 when the final executable is run, so we need to create them before
2273 addresses are assigned to the output sections. We work out the
2274 actual contents and size of these sections later. */
2277 elf_link_create_dynamic_sections (abfd, info)
2279 struct bfd_link_info *info;
2282 register asection *s;
2283 struct elf_link_hash_entry *h;
2284 struct elf_backend_data *bed;
2286 if (! is_elf_hash_table (info))
2289 if (elf_hash_table (info)->dynamic_sections_created)
2292 /* Make sure that all dynamic sections use the same input BFD. */
2293 if (elf_hash_table (info)->dynobj == NULL)
2294 elf_hash_table (info)->dynobj = abfd;
2296 abfd = elf_hash_table (info)->dynobj;
2298 /* Note that we set the SEC_IN_MEMORY flag for all of these
2300 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2301 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2303 /* A dynamically linked executable has a .interp section, but a
2304 shared library does not. */
2307 s = bfd_make_section (abfd, ".interp");
2309 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2313 if (! info->traditional_format
2314 && info->hash->creator->flavour == bfd_target_elf_flavour)
2316 s = bfd_make_section (abfd, ".eh_frame_hdr");
2318 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2319 || ! bfd_set_section_alignment (abfd, s, 2))
2323 /* Create sections to hold version informations. These are removed
2324 if they are not needed. */
2325 s = bfd_make_section (abfd, ".gnu.version_d");
2327 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2328 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2331 s = bfd_make_section (abfd, ".gnu.version");
2333 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2334 || ! bfd_set_section_alignment (abfd, s, 1))
2337 s = bfd_make_section (abfd, ".gnu.version_r");
2339 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2340 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2343 s = bfd_make_section (abfd, ".dynsym");
2345 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2346 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2349 s = bfd_make_section (abfd, ".dynstr");
2351 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2354 /* Create a strtab to hold the dynamic symbol names. */
2355 if (elf_hash_table (info)->dynstr == NULL)
2357 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
2358 if (elf_hash_table (info)->dynstr == NULL)
2362 s = bfd_make_section (abfd, ".dynamic");
2364 || ! bfd_set_section_flags (abfd, s, flags)
2365 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2368 /* The special symbol _DYNAMIC is always set to the start of the
2369 .dynamic section. This call occurs before we have processed the
2370 symbols for any dynamic object, so we don't have to worry about
2371 overriding a dynamic definition. We could set _DYNAMIC in a
2372 linker script, but we only want to define it if we are, in fact,
2373 creating a .dynamic section. We don't want to define it if there
2374 is no .dynamic section, since on some ELF platforms the start up
2375 code examines it to decide how to initialize the process. */
2377 if (! (_bfd_generic_link_add_one_symbol
2378 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
2379 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
2380 (struct bfd_link_hash_entry **) &h)))
2382 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2383 h->type = STT_OBJECT;
2386 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2389 bed = get_elf_backend_data (abfd);
2391 s = bfd_make_section (abfd, ".hash");
2393 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2394 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2396 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
2398 /* Let the backend create the rest of the sections. This lets the
2399 backend set the right flags. The backend will normally create
2400 the .got and .plt sections. */
2401 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
2404 elf_hash_table (info)->dynamic_sections_created = true;
2409 /* Add an entry to the .dynamic table. */
2412 elf_add_dynamic_entry (info, tag, val)
2413 struct bfd_link_info *info;
2417 Elf_Internal_Dyn dyn;
2420 bfd_size_type newsize;
2421 bfd_byte *newcontents;
2423 if (! is_elf_hash_table (info))
2426 dynobj = elf_hash_table (info)->dynobj;
2428 s = bfd_get_section_by_name (dynobj, ".dynamic");
2429 BFD_ASSERT (s != NULL);
2431 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
2432 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
2433 if (newcontents == NULL)
2437 dyn.d_un.d_val = val;
2438 elf_swap_dyn_out (dynobj, &dyn,
2439 (Elf_External_Dyn *) (newcontents + s->_raw_size));
2441 s->_raw_size = newsize;
2442 s->contents = newcontents;
2447 /* Record a new local dynamic symbol. */
2450 elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
2451 struct bfd_link_info *info;
2455 struct elf_link_local_dynamic_entry *entry;
2456 struct elf_link_hash_table *eht;
2457 struct elf_strtab_hash *dynstr;
2458 Elf_External_Sym esym;
2459 unsigned long dynstr_index;
2464 if (! is_elf_hash_table (info))
2467 /* See if the entry exists already. */
2468 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2469 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
2472 entry = (struct elf_link_local_dynamic_entry *)
2473 bfd_alloc (input_bfd, (bfd_size_type) sizeof (*entry));
2477 /* Go find the symbol, so that we can find it's name. */
2478 amt = sizeof (Elf_External_Sym);
2479 pos = elf_tdata (input_bfd)->symtab_hdr.sh_offset + input_indx * amt;
2480 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
2481 || bfd_bread (&esym, amt, input_bfd) != amt)
2483 elf_swap_symbol_in (input_bfd, &esym, &entry->isym);
2485 name = (bfd_elf_string_from_elf_section
2486 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
2487 entry->isym.st_name));
2489 dynstr = elf_hash_table (info)->dynstr;
2492 /* Create a strtab to hold the dynamic symbol names. */
2493 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
2498 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
2499 if (dynstr_index == (unsigned long) -1)
2501 entry->isym.st_name = dynstr_index;
2503 eht = elf_hash_table (info);
2505 entry->next = eht->dynlocal;
2506 eht->dynlocal = entry;
2507 entry->input_bfd = input_bfd;
2508 entry->input_indx = input_indx;
2511 /* Whatever binding the symbol had before, it's now local. */
2513 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
2515 /* The dynindx will be set at the end of size_dynamic_sections. */
2520 /* Read and swap the relocs from the section indicated by SHDR. This
2521 may be either a REL or a RELA section. The relocations are
2522 translated into RELA relocations and stored in INTERNAL_RELOCS,
2523 which should have already been allocated to contain enough space.
2524 The EXTERNAL_RELOCS are a buffer where the external form of the
2525 relocations should be stored.
2527 Returns false if something goes wrong. */
2530 elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
2533 Elf_Internal_Shdr *shdr;
2534 PTR external_relocs;
2535 Elf_Internal_Rela *internal_relocs;
2537 struct elf_backend_data *bed;
2540 /* If there aren't any relocations, that's OK. */
2544 /* Position ourselves at the start of the section. */
2545 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2548 /* Read the relocations. */
2549 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2552 bed = get_elf_backend_data (abfd);
2554 /* Convert the external relocations to the internal format. */
2555 if (shdr->sh_entsize == sizeof (Elf_External_Rel))
2557 Elf_External_Rel *erel;
2558 Elf_External_Rel *erelend;
2559 Elf_Internal_Rela *irela;
2560 Elf_Internal_Rel *irel;
2562 erel = (Elf_External_Rel *) external_relocs;
2563 erelend = erel + NUM_SHDR_ENTRIES (shdr);
2564 irela = internal_relocs;
2565 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
2566 irel = bfd_alloc (abfd, amt);
2567 for (; erel < erelend; erel++, irela += bed->s->int_rels_per_ext_rel)
2571 if (bed->s->swap_reloc_in)
2572 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
2574 elf_swap_reloc_in (abfd, erel, irel);
2576 for (i = 0; i < bed->s->int_rels_per_ext_rel; ++i)
2578 irela[i].r_offset = irel[i].r_offset;
2579 irela[i].r_info = irel[i].r_info;
2580 irela[i].r_addend = 0;
2586 Elf_External_Rela *erela;
2587 Elf_External_Rela *erelaend;
2588 Elf_Internal_Rela *irela;
2590 BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela));
2592 erela = (Elf_External_Rela *) external_relocs;
2593 erelaend = erela + NUM_SHDR_ENTRIES (shdr);
2594 irela = internal_relocs;
2595 for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel)
2597 if (bed->s->swap_reloca_in)
2598 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
2600 elf_swap_reloca_in (abfd, erela, irela);
2607 /* Read and swap the relocs for a section O. They may have been
2608 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2609 not NULL, they are used as buffers to read into. They are known to
2610 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2611 the return value is allocated using either malloc or bfd_alloc,
2612 according to the KEEP_MEMORY argument. If O has two relocation
2613 sections (both REL and RELA relocations), then the REL_HDR
2614 relocations will appear first in INTERNAL_RELOCS, followed by the
2615 REL_HDR2 relocations. */
2618 NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
2622 PTR external_relocs;
2623 Elf_Internal_Rela *internal_relocs;
2624 boolean keep_memory;
2626 Elf_Internal_Shdr *rel_hdr;
2628 Elf_Internal_Rela *alloc2 = NULL;
2629 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2631 if (elf_section_data (o)->relocs != NULL)
2632 return elf_section_data (o)->relocs;
2634 if (o->reloc_count == 0)
2637 rel_hdr = &elf_section_data (o)->rel_hdr;
2639 if (internal_relocs == NULL)
2643 size = o->reloc_count;
2644 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2646 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2648 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2649 if (internal_relocs == NULL)
2653 if (external_relocs == NULL)
2655 bfd_size_type size = rel_hdr->sh_size;
2657 if (elf_section_data (o)->rel_hdr2)
2658 size += elf_section_data (o)->rel_hdr2->sh_size;
2659 alloc1 = (PTR) bfd_malloc (size);
2662 external_relocs = alloc1;
2665 if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
2669 if (!elf_link_read_relocs_from_section
2671 elf_section_data (o)->rel_hdr2,
2672 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2673 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2674 * bed->s->int_rels_per_ext_rel)))
2677 /* Cache the results for next time, if we can. */
2679 elf_section_data (o)->relocs = internal_relocs;
2684 /* Don't free alloc2, since if it was allocated we are passing it
2685 back (under the name of internal_relocs). */
2687 return internal_relocs;
2697 /* Record an assignment to a symbol made by a linker script. We need
2698 this in case some dynamic object refers to this symbol. */
2701 NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
2702 bfd *output_bfd ATTRIBUTE_UNUSED;
2703 struct bfd_link_info *info;
2707 struct elf_link_hash_entry *h;
2709 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2712 h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
2716 if (h->root.type == bfd_link_hash_new)
2717 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
2719 /* If this symbol is being provided by the linker script, and it is
2720 currently defined by a dynamic object, but not by a regular
2721 object, then mark it as undefined so that the generic linker will
2722 force the correct value. */
2724 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2725 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2726 h->root.type = bfd_link_hash_undefined;
2728 /* If this symbol is not being provided by the linker script, and it is
2729 currently defined by a dynamic object, but not by a regular object,
2730 then clear out any version information because the symbol will not be
2731 associated with the dynamic object any more. */
2733 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2734 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2735 h->verinfo.verdef = NULL;
2737 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2739 /* When possible, keep the original type of the symbol. */
2740 if (h->type == STT_NOTYPE)
2741 h->type = STT_OBJECT;
2743 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2744 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
2746 && h->dynindx == -1)
2748 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2751 /* If this is a weak defined symbol, and we know a corresponding
2752 real symbol from the same dynamic object, make sure the real
2753 symbol is also made into a dynamic symbol. */
2754 if (h->weakdef != NULL
2755 && h->weakdef->dynindx == -1)
2757 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2765 /* This structure is used to pass information to
2766 elf_link_assign_sym_version. */
2768 struct elf_assign_sym_version_info
2772 /* General link information. */
2773 struct bfd_link_info *info;
2775 struct bfd_elf_version_tree *verdefs;
2776 /* Whether we had a failure. */
2780 /* This structure is used to pass information to
2781 elf_link_find_version_dependencies. */
2783 struct elf_find_verdep_info
2787 /* General link information. */
2788 struct bfd_link_info *info;
2789 /* The number of dependencies. */
2791 /* Whether we had a failure. */
2795 /* Array used to determine the number of hash table buckets to use
2796 based on the number of symbols there are. If there are fewer than
2797 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2798 fewer than 37 we use 17 buckets, and so forth. We never use more
2799 than 32771 buckets. */
2801 static const size_t elf_buckets[] =
2803 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2807 /* Compute bucket count for hashing table. We do not use a static set
2808 of possible tables sizes anymore. Instead we determine for all
2809 possible reasonable sizes of the table the outcome (i.e., the
2810 number of collisions etc) and choose the best solution. The
2811 weighting functions are not too simple to allow the table to grow
2812 without bounds. Instead one of the weighting factors is the size.
2813 Therefore the result is always a good payoff between few collisions
2814 (= short chain lengths) and table size. */
2816 compute_bucket_count (info)
2817 struct bfd_link_info *info;
2819 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
2820 size_t best_size = 0;
2821 unsigned long int *hashcodes;
2822 unsigned long int *hashcodesp;
2823 unsigned long int i;
2826 /* Compute the hash values for all exported symbols. At the same
2827 time store the values in an array so that we could use them for
2830 amt *= sizeof (unsigned long int);
2831 hashcodes = (unsigned long int *) bfd_malloc (amt);
2832 if (hashcodes == NULL)
2834 hashcodesp = hashcodes;
2836 /* Put all hash values in HASHCODES. */
2837 elf_link_hash_traverse (elf_hash_table (info),
2838 elf_collect_hash_codes, &hashcodesp);
2840 /* We have a problem here. The following code to optimize the table
2841 size requires an integer type with more the 32 bits. If
2842 BFD_HOST_U_64_BIT is set we know about such a type. */
2843 #ifdef BFD_HOST_U_64_BIT
2844 if (info->optimize == true)
2846 unsigned long int nsyms = hashcodesp - hashcodes;
2849 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
2850 unsigned long int *counts ;
2852 /* Possible optimization parameters: if we have NSYMS symbols we say
2853 that the hashing table must at least have NSYMS/4 and at most
2855 minsize = nsyms / 4;
2858 best_size = maxsize = nsyms * 2;
2860 /* Create array where we count the collisions in. We must use bfd_malloc
2861 since the size could be large. */
2863 amt *= sizeof (unsigned long int);
2864 counts = (unsigned long int *) bfd_malloc (amt);
2871 /* Compute the "optimal" size for the hash table. The criteria is a
2872 minimal chain length. The minor criteria is (of course) the size
2874 for (i = minsize; i < maxsize; ++i)
2876 /* Walk through the array of hashcodes and count the collisions. */
2877 BFD_HOST_U_64_BIT max;
2878 unsigned long int j;
2879 unsigned long int fact;
2881 memset (counts, '\0', i * sizeof (unsigned long int));
2883 /* Determine how often each hash bucket is used. */
2884 for (j = 0; j < nsyms; ++j)
2885 ++counts[hashcodes[j] % i];
2887 /* For the weight function we need some information about the
2888 pagesize on the target. This is information need not be 100%
2889 accurate. Since this information is not available (so far) we
2890 define it here to a reasonable default value. If it is crucial
2891 to have a better value some day simply define this value. */
2892 # ifndef BFD_TARGET_PAGESIZE
2893 # define BFD_TARGET_PAGESIZE (4096)
2896 /* We in any case need 2 + NSYMS entries for the size values and
2898 max = (2 + nsyms) * (ARCH_SIZE / 8);
2901 /* Variant 1: optimize for short chains. We add the squares
2902 of all the chain lengths (which favous many small chain
2903 over a few long chains). */
2904 for (j = 0; j < i; ++j)
2905 max += counts[j] * counts[j];
2907 /* This adds penalties for the overall size of the table. */
2908 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2911 /* Variant 2: Optimize a lot more for small table. Here we
2912 also add squares of the size but we also add penalties for
2913 empty slots (the +1 term). */
2914 for (j = 0; j < i; ++j)
2915 max += (1 + counts[j]) * (1 + counts[j]);
2917 /* The overall size of the table is considered, but not as
2918 strong as in variant 1, where it is squared. */
2919 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2923 /* Compare with current best results. */
2924 if (max < best_chlen)
2934 #endif /* defined (BFD_HOST_U_64_BIT) */
2936 /* This is the fallback solution if no 64bit type is available or if we
2937 are not supposed to spend much time on optimizations. We select the
2938 bucket count using a fixed set of numbers. */
2939 for (i = 0; elf_buckets[i] != 0; i++)
2941 best_size = elf_buckets[i];
2942 if (dynsymcount < elf_buckets[i + 1])
2947 /* Free the arrays we needed. */
2953 /* Set up the sizes and contents of the ELF dynamic sections. This is
2954 called by the ELF linker emulation before_allocation routine. We
2955 must set the sizes of the sections before the linker sets the
2956 addresses of the various sections. */
2959 NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
2961 auxiliary_filters, info, sinterpptr,
2966 const char *filter_shlib;
2967 const char * const *auxiliary_filters;
2968 struct bfd_link_info *info;
2969 asection **sinterpptr;
2970 struct bfd_elf_version_tree *verdefs;
2972 bfd_size_type soname_indx;
2974 struct elf_backend_data *bed;
2975 struct elf_assign_sym_version_info asvinfo;
2979 soname_indx = (bfd_size_type) -1;
2981 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2984 if (! is_elf_hash_table (info))
2987 /* Any syms created from now on start with -1 in
2988 got.refcount/offset and plt.refcount/offset. */
2989 elf_hash_table (info)->init_refcount = -1;
2991 /* The backend may have to create some sections regardless of whether
2992 we're dynamic or not. */
2993 bed = get_elf_backend_data (output_bfd);
2994 if (bed->elf_backend_always_size_sections
2995 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
2998 dynobj = elf_hash_table (info)->dynobj;
3000 /* If there were no dynamic objects in the link, there is nothing to
3005 if (elf_hash_table (info)->dynamic_sections_created)
3007 struct elf_info_failed eif;
3008 struct elf_link_hash_entry *h;
3011 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
3012 BFD_ASSERT (*sinterpptr != NULL || info->shared);
3016 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3018 if (soname_indx == (bfd_size_type) -1
3019 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SONAME,
3026 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMBOLIC,
3029 info->flags |= DF_SYMBOLIC;
3036 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
3038 if (info->new_dtags)
3039 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
3040 if (indx == (bfd_size_type) -1
3041 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_RPATH, indx)
3043 && ! elf_add_dynamic_entry (info, (bfd_vma) DT_RUNPATH,
3048 if (filter_shlib != NULL)
3052 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3053 filter_shlib, true);
3054 if (indx == (bfd_size_type) -1
3055 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_FILTER, indx))
3059 if (auxiliary_filters != NULL)
3061 const char * const *p;
3063 for (p = auxiliary_filters; *p != NULL; p++)
3067 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3069 if (indx == (bfd_size_type) -1
3070 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_AUXILIARY,
3077 eif.verdefs = verdefs;
3080 /* If we are supposed to export all symbols into the dynamic symbol
3081 table (this is not the normal case), then do so. */
3082 if (info->export_dynamic)
3084 elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
3090 /* Attach all the symbols to their version information. */
3091 asvinfo.output_bfd = output_bfd;
3092 asvinfo.info = info;
3093 asvinfo.verdefs = verdefs;
3094 asvinfo.failed = false;
3096 elf_link_hash_traverse (elf_hash_table (info),
3097 elf_link_assign_sym_version,
3102 /* Find all symbols which were defined in a dynamic object and make
3103 the backend pick a reasonable value for them. */
3104 elf_link_hash_traverse (elf_hash_table (info),
3105 elf_adjust_dynamic_symbol,
3110 /* Add some entries to the .dynamic section. We fill in some of the
3111 values later, in elf_bfd_final_link, but we must add the entries
3112 now so that we know the final size of the .dynamic section. */
3114 /* If there are initialization and/or finalization functions to
3115 call then add the corresponding DT_INIT/DT_FINI entries. */
3116 h = (info->init_function
3117 ? elf_link_hash_lookup (elf_hash_table (info),
3118 info->init_function, false,
3122 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3123 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3125 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_INIT, (bfd_vma) 0))
3128 h = (info->fini_function
3129 ? elf_link_hash_lookup (elf_hash_table (info),
3130 info->fini_function, false,
3134 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3135 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3137 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FINI, (bfd_vma) 0))
3141 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
3142 /* If .dynstr is excluded from the link, we don't want any of
3143 these tags. Strictly, we should be checking each section
3144 individually; This quick check covers for the case where
3145 someone does a /DISCARD/ : { *(*) }. */
3146 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
3148 bfd_size_type strsize;
3150 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3151 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_HASH, (bfd_vma) 0)
3152 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRTAB, (bfd_vma) 0)
3153 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMTAB, (bfd_vma) 0)
3154 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRSZ, strsize)
3155 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMENT,
3156 (bfd_vma) sizeof (Elf_External_Sym)))
3161 /* The backend must work out the sizes of all the other dynamic
3163 if (bed->elf_backend_size_dynamic_sections
3164 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
3167 if (elf_hash_table (info)->dynamic_sections_created)
3169 bfd_size_type dynsymcount;
3171 size_t bucketcount = 0;
3172 size_t hash_entry_size;
3173 unsigned int dtagcount;
3175 /* Set up the version definition section. */
3176 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3177 BFD_ASSERT (s != NULL);
3179 /* We may have created additional version definitions if we are
3180 just linking a regular application. */
3181 verdefs = asvinfo.verdefs;
3183 if (verdefs == NULL)
3184 _bfd_strip_section_from_output (info, s);
3189 struct bfd_elf_version_tree *t;
3191 Elf_Internal_Verdef def;
3192 Elf_Internal_Verdaux defaux;
3197 /* Make space for the base version. */
3198 size += sizeof (Elf_External_Verdef);
3199 size += sizeof (Elf_External_Verdaux);
3202 for (t = verdefs; t != NULL; t = t->next)
3204 struct bfd_elf_version_deps *n;
3206 size += sizeof (Elf_External_Verdef);
3207 size += sizeof (Elf_External_Verdaux);
3210 for (n = t->deps; n != NULL; n = n->next)
3211 size += sizeof (Elf_External_Verdaux);
3214 s->_raw_size = size;
3215 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3216 if (s->contents == NULL && s->_raw_size != 0)
3219 /* Fill in the version definition section. */
3223 def.vd_version = VER_DEF_CURRENT;
3224 def.vd_flags = VER_FLG_BASE;
3227 def.vd_aux = sizeof (Elf_External_Verdef);
3228 def.vd_next = (sizeof (Elf_External_Verdef)
3229 + sizeof (Elf_External_Verdaux));
3231 if (soname_indx != (bfd_size_type) -1)
3233 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3235 def.vd_hash = bfd_elf_hash (soname);
3236 defaux.vda_name = soname_indx;
3243 name = basename (output_bfd->filename);
3244 def.vd_hash = bfd_elf_hash (name);
3245 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3247 if (indx == (bfd_size_type) -1)
3249 defaux.vda_name = indx;
3251 defaux.vda_next = 0;
3253 _bfd_elf_swap_verdef_out (output_bfd, &def,
3254 (Elf_External_Verdef *) p);
3255 p += sizeof (Elf_External_Verdef);
3256 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3257 (Elf_External_Verdaux *) p);
3258 p += sizeof (Elf_External_Verdaux);
3260 for (t = verdefs; t != NULL; t = t->next)
3263 struct bfd_elf_version_deps *n;
3264 struct elf_link_hash_entry *h;
3267 for (n = t->deps; n != NULL; n = n->next)
3270 /* Add a symbol representing this version. */
3272 if (! (_bfd_generic_link_add_one_symbol
3273 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
3274 (bfd_vma) 0, (const char *) NULL, false,
3275 get_elf_backend_data (dynobj)->collect,
3276 (struct bfd_link_hash_entry **) &h)))
3278 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
3279 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3280 h->type = STT_OBJECT;
3281 h->verinfo.vertree = t;
3283 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
3286 def.vd_version = VER_DEF_CURRENT;
3288 if (t->globals == NULL && t->locals == NULL && ! t->used)
3289 def.vd_flags |= VER_FLG_WEAK;
3290 def.vd_ndx = t->vernum + 1;
3291 def.vd_cnt = cdeps + 1;
3292 def.vd_hash = bfd_elf_hash (t->name);
3293 def.vd_aux = sizeof (Elf_External_Verdef);
3294 if (t->next != NULL)
3295 def.vd_next = (sizeof (Elf_External_Verdef)
3296 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
3300 _bfd_elf_swap_verdef_out (output_bfd, &def,
3301 (Elf_External_Verdef *) p);
3302 p += sizeof (Elf_External_Verdef);
3304 defaux.vda_name = h->dynstr_index;
3305 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3307 if (t->deps == NULL)
3308 defaux.vda_next = 0;
3310 defaux.vda_next = sizeof (Elf_External_Verdaux);
3311 t->name_indx = defaux.vda_name;
3313 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3314 (Elf_External_Verdaux *) p);
3315 p += sizeof (Elf_External_Verdaux);
3317 for (n = t->deps; n != NULL; n = n->next)
3319 if (n->version_needed == NULL)
3321 /* This can happen if there was an error in the
3323 defaux.vda_name = 0;
3327 defaux.vda_name = n->version_needed->name_indx;
3328 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3331 if (n->next == NULL)
3332 defaux.vda_next = 0;
3334 defaux.vda_next = sizeof (Elf_External_Verdaux);
3336 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3337 (Elf_External_Verdaux *) p);
3338 p += sizeof (Elf_External_Verdaux);
3342 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEF, (bfd_vma) 0)
3343 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEFNUM,
3347 elf_tdata (output_bfd)->cverdefs = cdefs;
3350 if (info->new_dtags && info->flags)
3352 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags))
3359 info->flags_1 &= ~ (DF_1_INITFIRST
3362 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS_1,
3367 /* Work out the size of the version reference section. */
3369 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3370 BFD_ASSERT (s != NULL);
3372 struct elf_find_verdep_info sinfo;
3374 sinfo.output_bfd = output_bfd;
3376 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
3377 if (sinfo.vers == 0)
3379 sinfo.failed = false;
3381 elf_link_hash_traverse (elf_hash_table (info),
3382 elf_link_find_version_dependencies,
3385 if (elf_tdata (output_bfd)->verref == NULL)
3386 _bfd_strip_section_from_output (info, s);
3389 Elf_Internal_Verneed *t;
3394 /* Build the version definition section. */
3397 for (t = elf_tdata (output_bfd)->verref;
3401 Elf_Internal_Vernaux *a;
3403 size += sizeof (Elf_External_Verneed);
3405 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3406 size += sizeof (Elf_External_Vernaux);
3409 s->_raw_size = size;
3410 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3411 if (s->contents == NULL)
3415 for (t = elf_tdata (output_bfd)->verref;
3420 Elf_Internal_Vernaux *a;
3424 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3427 t->vn_version = VER_NEED_CURRENT;
3429 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3430 elf_dt_name (t->vn_bfd) != NULL
3431 ? elf_dt_name (t->vn_bfd)
3432 : basename (t->vn_bfd->filename),
3434 if (indx == (bfd_size_type) -1)
3437 t->vn_aux = sizeof (Elf_External_Verneed);
3438 if (t->vn_nextref == NULL)
3441 t->vn_next = (sizeof (Elf_External_Verneed)
3442 + caux * sizeof (Elf_External_Vernaux));
3444 _bfd_elf_swap_verneed_out (output_bfd, t,
3445 (Elf_External_Verneed *) p);
3446 p += sizeof (Elf_External_Verneed);
3448 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3450 a->vna_hash = bfd_elf_hash (a->vna_nodename);
3451 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3452 a->vna_nodename, false);
3453 if (indx == (bfd_size_type) -1)
3456 if (a->vna_nextptr == NULL)
3459 a->vna_next = sizeof (Elf_External_Vernaux);
3461 _bfd_elf_swap_vernaux_out (output_bfd, a,
3462 (Elf_External_Vernaux *) p);
3463 p += sizeof (Elf_External_Vernaux);
3467 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEED,
3469 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEEDNUM,
3473 elf_tdata (output_bfd)->cverrefs = crefs;
3477 /* Assign dynsym indicies. In a shared library we generate a
3478 section symbol for each output section, which come first.
3479 Next come all of the back-end allocated local dynamic syms,
3480 followed by the rest of the global symbols. */
3482 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3484 /* Work out the size of the symbol version section. */
3485 s = bfd_get_section_by_name (dynobj, ".gnu.version");
3486 BFD_ASSERT (s != NULL);
3487 if (dynsymcount == 0
3488 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
3490 _bfd_strip_section_from_output (info, s);
3491 /* The DYNSYMCOUNT might have changed if we were going to
3492 output a dynamic symbol table entry for S. */
3493 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3497 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
3498 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3499 if (s->contents == NULL)
3502 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERSYM, (bfd_vma) 0))
3506 /* Set the size of the .dynsym and .hash sections. We counted
3507 the number of dynamic symbols in elf_link_add_object_symbols.
3508 We will build the contents of .dynsym and .hash when we build
3509 the final symbol table, because until then we do not know the
3510 correct value to give the symbols. We built the .dynstr
3511 section as we went along in elf_link_add_object_symbols. */
3512 s = bfd_get_section_by_name (dynobj, ".dynsym");
3513 BFD_ASSERT (s != NULL);
3514 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
3515 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3516 if (s->contents == NULL && s->_raw_size != 0)
3519 if (dynsymcount != 0)
3521 Elf_Internal_Sym isym;
3523 /* The first entry in .dynsym is a dummy symbol. */
3530 elf_swap_symbol_out (output_bfd, &isym,
3531 (PTR) (Elf_External_Sym *) s->contents);
3534 /* Compute the size of the hashing table. As a side effect this
3535 computes the hash values for all the names we export. */
3536 bucketcount = compute_bucket_count (info);
3538 s = bfd_get_section_by_name (dynobj, ".hash");
3539 BFD_ASSERT (s != NULL);
3540 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
3541 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
3542 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3543 if (s->contents == NULL)
3545 memset (s->contents, 0, (size_t) s->_raw_size);
3547 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount,
3549 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) dynsymcount,
3550 s->contents + hash_entry_size);
3552 elf_hash_table (info)->bucketcount = bucketcount;
3554 s = bfd_get_section_by_name (dynobj, ".dynstr");
3555 BFD_ASSERT (s != NULL);
3557 elf_finalize_dynstr (output_bfd, info);
3559 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3561 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
3562 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NULL, (bfd_vma) 0))
3569 /* This function is used to adjust offsets into .dynstr for
3570 dynamic symbols. This is called via elf_link_hash_traverse. */
3572 static boolean elf_adjust_dynstr_offsets
3573 PARAMS ((struct elf_link_hash_entry *, PTR));
3576 elf_adjust_dynstr_offsets (h, data)
3577 struct elf_link_hash_entry *h;
3580 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3582 if (h->dynindx != -1)
3583 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3587 /* Assign string offsets in .dynstr, update all structures referencing
3591 elf_finalize_dynstr (output_bfd, info)
3593 struct bfd_link_info *info;
3595 struct elf_link_local_dynamic_entry *entry;
3596 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
3597 bfd *dynobj = elf_hash_table (info)->dynobj;
3600 Elf_External_Dyn *dyncon, *dynconend;
3602 _bfd_elf_strtab_finalize (dynstr);
3603 size = _bfd_elf_strtab_size (dynstr);
3605 /* Update all .dynamic entries referencing .dynstr strings. */
3606 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3607 BFD_ASSERT (sdyn != NULL);
3609 dyncon = (Elf_External_Dyn *) sdyn->contents;
3610 dynconend = (Elf_External_Dyn *) (sdyn->contents +
3612 for (; dyncon < dynconend; dyncon++)
3614 Elf_Internal_Dyn dyn;
3616 elf_swap_dyn_in (dynobj, dyncon, & dyn);
3620 dyn.d_un.d_val = size;
3621 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3629 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3630 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3637 /* Now update local dynamic symbols. */
3638 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
3639 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3640 entry->isym.st_name);
3642 /* And the rest of dynamic symbols. */
3643 elf_link_hash_traverse (elf_hash_table (info),
3644 elf_adjust_dynstr_offsets, dynstr);
3646 /* Adjust version definitions. */
3647 if (elf_tdata (output_bfd)->cverdefs)
3652 Elf_Internal_Verdef def;
3653 Elf_Internal_Verdaux defaux;
3655 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3656 p = (bfd_byte *) s->contents;
3659 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3661 p += sizeof (Elf_External_Verdef);
3662 for (i = 0; i < def.vd_cnt; ++i)
3664 _bfd_elf_swap_verdaux_in (output_bfd,
3665 (Elf_External_Verdaux *) p, &defaux);
3666 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3668 _bfd_elf_swap_verdaux_out (output_bfd,
3669 &defaux, (Elf_External_Verdaux *) p);
3670 p += sizeof (Elf_External_Verdaux);
3673 while (def.vd_next);
3676 /* Adjust version references. */
3677 if (elf_tdata (output_bfd)->verref)
3682 Elf_Internal_Verneed need;
3683 Elf_Internal_Vernaux needaux;
3685 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3686 p = (bfd_byte *) s->contents;
3689 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3691 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3692 _bfd_elf_swap_verneed_out (output_bfd, &need,
3693 (Elf_External_Verneed *) p);
3694 p += sizeof (Elf_External_Verneed);
3695 for (i = 0; i < need.vn_cnt; ++i)
3697 _bfd_elf_swap_vernaux_in (output_bfd,
3698 (Elf_External_Vernaux *) p, &needaux);
3699 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3701 _bfd_elf_swap_vernaux_out (output_bfd,
3703 (Elf_External_Vernaux *) p);
3704 p += sizeof (Elf_External_Vernaux);
3707 while (need.vn_next);
3713 /* Fix up the flags for a symbol. This handles various cases which
3714 can only be fixed after all the input files are seen. This is
3715 currently called by both adjust_dynamic_symbol and
3716 assign_sym_version, which is unnecessary but perhaps more robust in
3717 the face of future changes. */
3720 elf_fix_symbol_flags (h, eif)
3721 struct elf_link_hash_entry *h;
3722 struct elf_info_failed *eif;
3724 /* If this symbol was mentioned in a non-ELF file, try to set
3725 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3726 permit a non-ELF file to correctly refer to a symbol defined in
3727 an ELF dynamic object. */
3728 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
3730 while (h->root.type == bfd_link_hash_indirect)
3731 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3733 if (h->root.type != bfd_link_hash_defined
3734 && h->root.type != bfd_link_hash_defweak)
3735 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3736 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3739 if (h->root.u.def.section->owner != NULL
3740 && (bfd_get_flavour (h->root.u.def.section->owner)
3741 == bfd_target_elf_flavour))
3742 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3743 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3745 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3748 if (h->dynindx == -1
3749 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3750 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3752 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3761 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3762 was first seen in a non-ELF file. Fortunately, if the symbol
3763 was first seen in an ELF file, we're probably OK unless the
3764 symbol was defined in a non-ELF file. Catch that case here.
3765 FIXME: We're still in trouble if the symbol was first seen in
3766 a dynamic object, and then later in a non-ELF regular object. */
3767 if ((h->root.type == bfd_link_hash_defined
3768 || h->root.type == bfd_link_hash_defweak)
3769 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3770 && (h->root.u.def.section->owner != NULL
3771 ? (bfd_get_flavour (h->root.u.def.section->owner)
3772 != bfd_target_elf_flavour)
3773 : (bfd_is_abs_section (h->root.u.def.section)
3774 && (h->elf_link_hash_flags
3775 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
3776 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3779 /* If this is a final link, and the symbol was defined as a common
3780 symbol in a regular object file, and there was no definition in
3781 any dynamic object, then the linker will have allocated space for
3782 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3783 flag will not have been set. */
3784 if (h->root.type == bfd_link_hash_defined
3785 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3786 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
3787 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3788 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3789 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3791 /* If -Bsymbolic was used (which means to bind references to global
3792 symbols to the definition within the shared object), and this
3793 symbol was defined in a regular object, then it actually doesn't
3794 need a PLT entry, and we can accomplish that by forcing it local.
3795 Likewise, if the symbol has hidden or internal visibility.
3796 FIXME: It might be that we also do not need a PLT for other
3797 non-hidden visibilities, but we would have to tell that to the
3798 backend specifically; we can't just clear PLT-related data here. */
3799 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
3800 && eif->info->shared
3801 && is_elf_hash_table (eif->info)
3802 && (eif->info->symbolic
3803 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3804 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3805 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3807 struct elf_backend_data *bed;
3809 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3810 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3811 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3813 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
3814 _bfd_elf_strtab_delref (elf_hash_table (eif->info)->dynstr,
3817 (*bed->elf_backend_hide_symbol) (eif->info, h);
3820 /* If this is a weak defined symbol in a dynamic object, and we know
3821 the real definition in the dynamic object, copy interesting flags
3822 over to the real definition. */
3823 if (h->weakdef != NULL)
3825 struct elf_link_hash_entry *weakdef;
3827 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3828 || h->root.type == bfd_link_hash_defweak);
3829 weakdef = h->weakdef;
3830 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
3831 || weakdef->root.type == bfd_link_hash_defweak);
3832 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
3834 /* If the real definition is defined by a regular object file,
3835 don't do anything special. See the longer description in
3836 elf_adjust_dynamic_symbol, below. */
3837 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3841 struct elf_backend_data *bed;
3843 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3844 (*bed->elf_backend_copy_indirect_symbol) (weakdef, h);
3851 /* Make the backend pick a good value for a dynamic symbol. This is
3852 called via elf_link_hash_traverse, and also calls itself
3856 elf_adjust_dynamic_symbol (h, data)
3857 struct elf_link_hash_entry *h;
3860 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3862 struct elf_backend_data *bed;
3864 /* Ignore indirect symbols. These are added by the versioning code. */
3865 if (h->root.type == bfd_link_hash_indirect)
3868 if (! is_elf_hash_table (eif->info))
3871 /* Fix the symbol flags. */
3872 if (! elf_fix_symbol_flags (h, eif))
3875 /* If this symbol does not require a PLT entry, and it is not
3876 defined by a dynamic object, or is not referenced by a regular
3877 object, ignore it. We do have to handle a weak defined symbol,
3878 even if no regular object refers to it, if we decided to add it
3879 to the dynamic symbol table. FIXME: Do we normally need to worry
3880 about symbols which are defined by one dynamic object and
3881 referenced by another one? */
3882 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
3883 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
3884 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3885 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
3886 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
3888 h->plt.offset = (bfd_vma) -1;
3892 /* If we've already adjusted this symbol, don't do it again. This
3893 can happen via a recursive call. */
3894 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
3897 /* Don't look at this symbol again. Note that we must set this
3898 after checking the above conditions, because we may look at a
3899 symbol once, decide not to do anything, and then get called
3900 recursively later after REF_REGULAR is set below. */
3901 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
3903 /* If this is a weak definition, and we know a real definition, and
3904 the real symbol is not itself defined by a regular object file,
3905 then get a good value for the real definition. We handle the
3906 real symbol first, for the convenience of the backend routine.
3908 Note that there is a confusing case here. If the real definition
3909 is defined by a regular object file, we don't get the real symbol
3910 from the dynamic object, but we do get the weak symbol. If the
3911 processor backend uses a COPY reloc, then if some routine in the
3912 dynamic object changes the real symbol, we will not see that
3913 change in the corresponding weak symbol. This is the way other
3914 ELF linkers work as well, and seems to be a result of the shared
3917 I will clarify this issue. Most SVR4 shared libraries define the
3918 variable _timezone and define timezone as a weak synonym. The
3919 tzset call changes _timezone. If you write
3920 extern int timezone;
3922 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3923 you might expect that, since timezone is a synonym for _timezone,
3924 the same number will print both times. However, if the processor
3925 backend uses a COPY reloc, then actually timezone will be copied
3926 into your process image, and, since you define _timezone
3927 yourself, _timezone will not. Thus timezone and _timezone will
3928 wind up at different memory locations. The tzset call will set
3929 _timezone, leaving timezone unchanged. */
3931 if (h->weakdef != NULL)
3933 /* If we get to this point, we know there is an implicit
3934 reference by a regular object file via the weak symbol H.
3935 FIXME: Is this really true? What if the traversal finds
3936 H->WEAKDEF before it finds H? */
3937 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
3939 if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif))
3943 /* If a symbol has no type and no size and does not require a PLT
3944 entry, then we are probably about to do the wrong thing here: we
3945 are probably going to create a COPY reloc for an empty object.
3946 This case can arise when a shared object is built with assembly
3947 code, and the assembly code fails to set the symbol type. */
3949 && h->type == STT_NOTYPE
3950 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
3951 (*_bfd_error_handler)
3952 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3953 h->root.root.string);
3955 dynobj = elf_hash_table (eif->info)->dynobj;
3956 bed = get_elf_backend_data (dynobj);
3957 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3966 /* This routine is used to export all defined symbols into the dynamic
3967 symbol table. It is called via elf_link_hash_traverse. */
3970 elf_export_symbol (h, data)
3971 struct elf_link_hash_entry *h;
3974 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3976 /* Ignore indirect symbols. These are added by the versioning code. */
3977 if (h->root.type == bfd_link_hash_indirect)
3980 if (h->dynindx == -1
3981 && (h->elf_link_hash_flags
3982 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
3984 struct bfd_elf_version_tree *t;
3985 struct bfd_elf_version_expr *d;
3987 for (t = eif->verdefs; t != NULL; t = t->next)
3989 if (t->globals != NULL)
3991 for (d = t->globals; d != NULL; d = d->next)
3993 if ((*d->match) (d, h->root.root.string))
3998 if (t->locals != NULL)
4000 for (d = t->locals ; d != NULL; d = d->next)
4002 if ((*d->match) (d, h->root.root.string))
4011 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
4022 /* Look through the symbols which are defined in other shared
4023 libraries and referenced here. Update the list of version
4024 dependencies. This will be put into the .gnu.version_r section.
4025 This function is called via elf_link_hash_traverse. */
4028 elf_link_find_version_dependencies (h, data)
4029 struct elf_link_hash_entry *h;
4032 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
4033 Elf_Internal_Verneed *t;
4034 Elf_Internal_Vernaux *a;
4037 /* We only care about symbols defined in shared objects with version
4039 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4040 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4042 || h->verinfo.verdef == NULL)
4045 /* See if we already know about this version. */
4046 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
4048 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
4051 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4052 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
4058 /* This is a new version. Add it to tree we are building. */
4063 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt);
4066 rinfo->failed = true;
4070 t->vn_bfd = h->verinfo.verdef->vd_bfd;
4071 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
4072 elf_tdata (rinfo->output_bfd)->verref = t;
4076 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt);
4078 /* Note that we are copying a string pointer here, and testing it
4079 above. If bfd_elf_string_from_elf_section is ever changed to
4080 discard the string data when low in memory, this will have to be
4082 a->vna_nodename = h->verinfo.verdef->vd_nodename;
4084 a->vna_flags = h->verinfo.verdef->vd_flags;
4085 a->vna_nextptr = t->vn_auxptr;
4087 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
4090 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
4097 /* Figure out appropriate versions for all the symbols. We may not
4098 have the version number script until we have read all of the input
4099 files, so until that point we don't know which symbols should be
4100 local. This function is called via elf_link_hash_traverse. */
4103 elf_link_assign_sym_version (h, data)
4104 struct elf_link_hash_entry *h;
4107 struct elf_assign_sym_version_info *sinfo;
4108 struct bfd_link_info *info;
4109 struct elf_backend_data *bed;
4110 struct elf_info_failed eif;
4114 sinfo = (struct elf_assign_sym_version_info *) data;
4117 /* Fix the symbol flags. */
4120 if (! elf_fix_symbol_flags (h, &eif))
4123 sinfo->failed = true;
4127 /* We only need version numbers for symbols defined in regular
4129 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4132 bed = get_elf_backend_data (sinfo->output_bfd);
4133 p = strchr (h->root.root.string, ELF_VER_CHR);
4134 if (p != NULL && h->verinfo.vertree == NULL)
4136 struct bfd_elf_version_tree *t;
4141 /* There are two consecutive ELF_VER_CHR characters if this is
4142 not a hidden symbol. */
4144 if (*p == ELF_VER_CHR)
4150 /* If there is no version string, we can just return out. */
4154 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4158 /* Look for the version. If we find it, it is no longer weak. */
4159 for (t = sinfo->verdefs; t != NULL; t = t->next)
4161 if (strcmp (t->name, p) == 0)
4165 struct bfd_elf_version_expr *d;
4167 len = p - h->root.root.string;
4168 alc = bfd_alloc (sinfo->output_bfd, (bfd_size_type) len);
4171 strncpy (alc, h->root.root.string, len - 1);
4172 alc[len - 1] = '\0';
4173 if (alc[len - 2] == ELF_VER_CHR)
4174 alc[len - 2] = '\0';
4176 h->verinfo.vertree = t;
4180 if (t->globals != NULL)
4182 for (d = t->globals; d != NULL; d = d->next)
4183 if ((*d->match) (d, alc))
4187 /* See if there is anything to force this symbol to
4189 if (d == NULL && t->locals != NULL)
4191 for (d = t->locals; d != NULL; d = d->next)
4193 if ((*d->match) (d, alc))
4195 if (h->dynindx != -1
4197 && ! info->export_dynamic)
4199 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
4200 (*bed->elf_backend_hide_symbol) (info, h);
4201 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4210 bfd_release (sinfo->output_bfd, alc);
4215 /* If we are building an application, we need to create a
4216 version node for this version. */
4217 if (t == NULL && ! info->shared)
4219 struct bfd_elf_version_tree **pp;
4222 /* If we aren't going to export this symbol, we don't need
4223 to worry about it. */
4224 if (h->dynindx == -1)
4228 t = ((struct bfd_elf_version_tree *)
4229 bfd_alloc (sinfo->output_bfd, amt));
4232 sinfo->failed = true;
4241 t->name_indx = (unsigned int) -1;
4245 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
4247 t->vernum = version_index;
4251 h->verinfo.vertree = t;
4255 /* We could not find the version for a symbol when
4256 generating a shared archive. Return an error. */
4257 (*_bfd_error_handler)
4258 (_("%s: undefined versioned symbol name %s"),
4259 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
4260 bfd_set_error (bfd_error_bad_value);
4261 sinfo->failed = true;
4266 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4269 /* If we don't have a version for this symbol, see if we can find
4271 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
4273 struct bfd_elf_version_tree *t;
4274 struct bfd_elf_version_tree *deflt;
4275 struct bfd_elf_version_expr *d;
4277 /* See if can find what version this symbol is in. If the
4278 symbol is supposed to be local, then don't actually register
4281 for (t = sinfo->verdefs; t != NULL; t = t->next)
4283 if (t->globals != NULL)
4285 for (d = t->globals; d != NULL; d = d->next)
4287 if ((*d->match) (d, h->root.root.string))
4289 h->verinfo.vertree = t;
4298 if (t->locals != NULL)
4300 for (d = t->locals; d != NULL; d = d->next)
4302 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
4304 else if ((*d->match) (d, h->root.root.string))
4306 h->verinfo.vertree = t;
4307 if (h->dynindx != -1
4309 && ! info->export_dynamic)
4311 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
4312 (*bed->elf_backend_hide_symbol) (info, h);
4313 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4325 if (deflt != NULL && h->verinfo.vertree == NULL)
4327 h->verinfo.vertree = deflt;
4328 if (h->dynindx != -1
4330 && ! info->export_dynamic)
4332 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
4333 (*bed->elf_backend_hide_symbol) (info, h);
4334 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4343 /* Final phase of ELF linker. */
4345 /* A structure we use to avoid passing large numbers of arguments. */
4347 struct elf_final_link_info
4349 /* General link information. */
4350 struct bfd_link_info *info;
4353 /* Symbol string table. */
4354 struct bfd_strtab_hash *symstrtab;
4355 /* .dynsym section. */
4356 asection *dynsym_sec;
4357 /* .hash section. */
4359 /* symbol version section (.gnu.version). */
4360 asection *symver_sec;
4361 /* Buffer large enough to hold contents of any section. */
4363 /* Buffer large enough to hold external relocs of any section. */
4364 PTR external_relocs;
4365 /* Buffer large enough to hold internal relocs of any section. */
4366 Elf_Internal_Rela *internal_relocs;
4367 /* Buffer large enough to hold external local symbols of any input
4369 Elf_External_Sym *external_syms;
4370 /* Buffer large enough to hold internal local symbols of any input
4372 Elf_Internal_Sym *internal_syms;
4373 /* Array large enough to hold a symbol index for each local symbol
4374 of any input BFD. */
4376 /* Array large enough to hold a section pointer for each local
4377 symbol of any input BFD. */
4378 asection **sections;
4379 /* Buffer to hold swapped out symbols. */
4380 Elf_External_Sym *symbuf;
4381 /* Number of swapped out symbols in buffer. */
4382 size_t symbuf_count;
4383 /* Number of symbols which fit in symbuf. */
4387 static boolean elf_link_output_sym
4388 PARAMS ((struct elf_final_link_info *, const char *,
4389 Elf_Internal_Sym *, asection *));
4390 static boolean elf_link_flush_output_syms
4391 PARAMS ((struct elf_final_link_info *));
4392 static boolean elf_link_output_extsym
4393 PARAMS ((struct elf_link_hash_entry *, PTR));
4394 static boolean elf_link_sec_merge_syms
4395 PARAMS ((struct elf_link_hash_entry *, PTR));
4396 static boolean elf_link_input_bfd
4397 PARAMS ((struct elf_final_link_info *, bfd *));
4398 static boolean elf_reloc_link_order
4399 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4400 struct bfd_link_order *));
4402 /* This struct is used to pass information to elf_link_output_extsym. */
4404 struct elf_outext_info
4408 struct elf_final_link_info *finfo;
4411 /* Compute the size of, and allocate space for, REL_HDR which is the
4412 section header for a section containing relocations for O. */
4415 elf_link_size_reloc_section (abfd, rel_hdr, o)
4417 Elf_Internal_Shdr *rel_hdr;
4420 bfd_size_type reloc_count;
4421 bfd_size_type num_rel_hashes;
4423 /* Figure out how many relocations there will be. */
4424 if (rel_hdr == &elf_section_data (o)->rel_hdr)
4425 reloc_count = elf_section_data (o)->rel_count;
4427 reloc_count = elf_section_data (o)->rel_count2;
4429 num_rel_hashes = o->reloc_count;
4430 if (num_rel_hashes < reloc_count)
4431 num_rel_hashes = reloc_count;
4433 /* That allows us to calculate the size of the section. */
4434 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
4436 /* The contents field must last into write_object_contents, so we
4437 allocate it with bfd_alloc rather than malloc. Also since we
4438 cannot be sure that the contents will actually be filled in,
4439 we zero the allocated space. */
4440 rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size);
4441 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
4444 /* We only allocate one set of hash entries, so we only do it the
4445 first time we are called. */
4446 if (elf_section_data (o)->rel_hashes == NULL
4449 struct elf_link_hash_entry **p;
4451 p = ((struct elf_link_hash_entry **)
4452 bfd_zmalloc (num_rel_hashes
4453 * sizeof (struct elf_link_hash_entry *)));
4457 elf_section_data (o)->rel_hashes = p;
4463 /* When performing a relocateable link, the input relocations are
4464 preserved. But, if they reference global symbols, the indices
4465 referenced must be updated. Update all the relocations in
4466 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4469 elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
4471 Elf_Internal_Shdr *rel_hdr;
4473 struct elf_link_hash_entry **rel_hash;
4476 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4477 Elf_Internal_Rel *irel;
4478 Elf_Internal_Rela *irela;
4479 bfd_size_type amt = sizeof (Elf_Internal_Rel) * bed->s->int_rels_per_ext_rel;
4481 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
4484 (*_bfd_error_handler) (_("Error: out of memory"));
4488 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
4489 irela = (Elf_Internal_Rela *) bfd_zmalloc (amt);
4492 (*_bfd_error_handler) (_("Error: out of memory"));
4496 for (i = 0; i < count; i++, rel_hash++)
4498 if (*rel_hash == NULL)
4501 BFD_ASSERT ((*rel_hash)->indx >= 0);
4503 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
4505 Elf_External_Rel *erel;
4508 erel = (Elf_External_Rel *) rel_hdr->contents + i;
4509 if (bed->s->swap_reloc_in)
4510 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
4512 elf_swap_reloc_in (abfd, erel, irel);
4514 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4515 irel[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4516 ELF_R_TYPE (irel[j].r_info));
4518 if (bed->s->swap_reloc_out)
4519 (*bed->s->swap_reloc_out) (abfd, irel, (bfd_byte *) erel);
4521 elf_swap_reloc_out (abfd, irel, erel);
4525 Elf_External_Rela *erela;
4528 BFD_ASSERT (rel_hdr->sh_entsize
4529 == sizeof (Elf_External_Rela));
4531 erela = (Elf_External_Rela *) rel_hdr->contents + i;
4532 if (bed->s->swap_reloca_in)
4533 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
4535 elf_swap_reloca_in (abfd, erela, irela);
4537 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4538 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4539 ELF_R_TYPE (irela[j].r_info));
4541 if (bed->s->swap_reloca_out)
4542 (*bed->s->swap_reloca_out) (abfd, irela, (bfd_byte *) erela);
4544 elf_swap_reloca_out (abfd, irela, erela);
4552 struct elf_link_sort_rela {
4554 enum elf_reloc_type_class type;
4556 Elf_Internal_Rel rel;
4557 Elf_Internal_Rela rela;
4562 elf_link_sort_cmp1 (A, B)
4566 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4567 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4568 int relativea, relativeb;
4570 relativea = a->type == reloc_class_relative;
4571 relativeb = b->type == reloc_class_relative;
4573 if (relativea < relativeb)
4575 if (relativea > relativeb)
4577 if (ELF_R_SYM (a->u.rel.r_info) < ELF_R_SYM (b->u.rel.r_info))
4579 if (ELF_R_SYM (a->u.rel.r_info) > ELF_R_SYM (b->u.rel.r_info))
4581 if (a->u.rel.r_offset < b->u.rel.r_offset)
4583 if (a->u.rel.r_offset > b->u.rel.r_offset)
4589 elf_link_sort_cmp2 (A, B)
4593 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4594 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4597 if (a->offset < b->offset)
4599 if (a->offset > b->offset)
4601 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
4602 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
4607 if (a->u.rel.r_offset < b->u.rel.r_offset)
4609 if (a->u.rel.r_offset > b->u.rel.r_offset)
4615 elf_link_sort_relocs (abfd, info, psec)
4617 struct bfd_link_info *info;
4620 bfd *dynobj = elf_hash_table (info)->dynobj;
4621 asection *reldyn, *o;
4622 boolean rel = false;
4623 bfd_size_type count, size;
4625 struct elf_link_sort_rela *rela;
4626 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4628 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
4629 if (reldyn == NULL || reldyn->_raw_size == 0)
4631 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
4632 if (reldyn == NULL || reldyn->_raw_size == 0)
4635 count = reldyn->_raw_size / sizeof (Elf_External_Rel);
4638 count = reldyn->_raw_size / sizeof (Elf_External_Rela);
4641 for (o = dynobj->sections; o != NULL; o = o->next)
4642 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4643 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4644 && o->output_section == reldyn)
4645 size += o->_raw_size;
4647 if (size != reldyn->_raw_size)
4650 rela = (struct elf_link_sort_rela *) bfd_zmalloc (sizeof (*rela) * count);
4653 (*info->callbacks->warning)
4654 (info, _("Not enough memory to sort relocations"), 0, abfd, 0,
4659 for (o = dynobj->sections; o != NULL; o = o->next)
4660 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4661 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4662 && o->output_section == reldyn)
4666 Elf_External_Rel *erel, *erelend;
4667 struct elf_link_sort_rela *s;
4669 erel = (Elf_External_Rel *) o->contents;
4670 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4671 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4672 for (; erel < erelend; erel++, s++)
4674 if (bed->s->swap_reloc_in)
4675 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, &s->u.rel);
4677 elf_swap_reloc_in (abfd, erel, &s->u.rel);
4679 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
4684 Elf_External_Rela *erela, *erelaend;
4685 struct elf_link_sort_rela *s;
4687 erela = (Elf_External_Rela *) o->contents;
4688 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4689 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4690 for (; erela < erelaend; erela++, s++)
4692 if (bed->s->swap_reloca_in)
4693 (*bed->s->swap_reloca_in) (dynobj, (bfd_byte *) erela,
4696 elf_swap_reloca_in (dynobj, erela, &s->u.rela);
4698 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
4703 qsort (rela, (size_t) count, sizeof (*rela), elf_link_sort_cmp1);
4704 for (ret = 0; ret < count && rela[ret].type == reloc_class_relative; ret++)
4706 for (i = ret, j = ret; i < count; i++)
4708 if (ELF_R_SYM (rela[i].u.rel.r_info) != ELF_R_SYM (rela[j].u.rel.r_info))
4710 rela[i].offset = rela[j].u.rel.r_offset;
4712 qsort (rela + ret, (size_t) count - ret, sizeof (*rela), elf_link_sort_cmp2);
4714 for (o = dynobj->sections; o != NULL; o = o->next)
4715 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4716 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4717 && o->output_section == reldyn)
4721 Elf_External_Rel *erel, *erelend;
4722 struct elf_link_sort_rela *s;
4724 erel = (Elf_External_Rel *) o->contents;
4725 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4726 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4727 for (; erel < erelend; erel++, s++)
4729 if (bed->s->swap_reloc_out)
4730 (*bed->s->swap_reloc_out) (abfd, &s->u.rel,
4733 elf_swap_reloc_out (abfd, &s->u.rel, erel);
4738 Elf_External_Rela *erela, *erelaend;
4739 struct elf_link_sort_rela *s;
4741 erela = (Elf_External_Rela *) o->contents;
4742 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4743 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4744 for (; erela < erelaend; erela++, s++)
4746 if (bed->s->swap_reloca_out)
4747 (*bed->s->swap_reloca_out) (dynobj, &s->u.rela,
4748 (bfd_byte *) erela);
4750 elf_swap_reloca_out (dynobj, &s->u.rela, erela);
4760 /* Do the final step of an ELF link. */
4763 elf_bfd_final_link (abfd, info)
4765 struct bfd_link_info *info;
4768 boolean emit_relocs;
4770 struct elf_final_link_info finfo;
4771 register asection *o;
4772 register struct bfd_link_order *p;
4774 bfd_size_type max_contents_size;
4775 bfd_size_type max_external_reloc_size;
4776 bfd_size_type max_internal_reloc_count;
4777 bfd_size_type max_sym_count;
4779 Elf_Internal_Sym elfsym;
4781 Elf_Internal_Shdr *symtab_hdr;
4782 Elf_Internal_Shdr *symstrtab_hdr;
4783 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4784 struct elf_outext_info eoinfo;
4786 size_t relativecount = 0;
4787 asection *reldyn = 0;
4790 if (! is_elf_hash_table (info))
4794 abfd->flags |= DYNAMIC;
4796 dynamic = elf_hash_table (info)->dynamic_sections_created;
4797 dynobj = elf_hash_table (info)->dynobj;
4799 emit_relocs = (info->relocateable
4800 || info->emitrelocations
4801 || bed->elf_backend_emit_relocs);
4804 finfo.output_bfd = abfd;
4805 finfo.symstrtab = elf_stringtab_init ();
4806 if (finfo.symstrtab == NULL)
4811 finfo.dynsym_sec = NULL;
4812 finfo.hash_sec = NULL;
4813 finfo.symver_sec = NULL;
4817 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
4818 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
4819 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
4820 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
4821 /* Note that it is OK if symver_sec is NULL. */
4824 finfo.contents = NULL;
4825 finfo.external_relocs = NULL;
4826 finfo.internal_relocs = NULL;
4827 finfo.external_syms = NULL;
4828 finfo.internal_syms = NULL;
4829 finfo.indices = NULL;
4830 finfo.sections = NULL;
4831 finfo.symbuf = NULL;
4832 finfo.symbuf_count = 0;
4834 /* Count up the number of relocations we will output for each output
4835 section, so that we know the sizes of the reloc sections. We
4836 also figure out some maximum sizes. */
4837 max_contents_size = 0;
4838 max_external_reloc_size = 0;
4839 max_internal_reloc_count = 0;
4842 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4846 for (p = o->link_order_head; p != NULL; p = p->next)
4848 if (p->type == bfd_section_reloc_link_order
4849 || p->type == bfd_symbol_reloc_link_order)
4851 else if (p->type == bfd_indirect_link_order)
4855 sec = p->u.indirect.section;
4857 /* Mark all sections which are to be included in the
4858 link. This will normally be every section. We need
4859 to do this so that we can identify any sections which
4860 the linker has decided to not include. */
4861 sec->linker_mark = true;
4863 if (sec->flags & SEC_MERGE)
4866 if (info->relocateable || info->emitrelocations)
4867 o->reloc_count += sec->reloc_count;
4868 else if (bed->elf_backend_count_relocs)
4870 Elf_Internal_Rela * relocs;
4872 relocs = (NAME(_bfd_elf,link_read_relocs)
4873 (abfd, sec, (PTR) NULL,
4874 (Elf_Internal_Rela *) NULL, info->keep_memory));
4876 o->reloc_count += (*bed->elf_backend_count_relocs)
4879 if (!info->keep_memory)
4883 if (sec->_raw_size > max_contents_size)
4884 max_contents_size = sec->_raw_size;
4885 if (sec->_cooked_size > max_contents_size)
4886 max_contents_size = sec->_cooked_size;
4888 /* We are interested in just local symbols, not all
4890 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
4891 && (sec->owner->flags & DYNAMIC) == 0)
4895 if (elf_bad_symtab (sec->owner))
4896 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
4897 / sizeof (Elf_External_Sym));
4899 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
4901 if (sym_count > max_sym_count)
4902 max_sym_count = sym_count;
4904 if ((sec->flags & SEC_RELOC) != 0)
4908 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
4909 if (ext_size > max_external_reloc_size)
4910 max_external_reloc_size = ext_size;
4911 if (sec->reloc_count > max_internal_reloc_count)
4912 max_internal_reloc_count = sec->reloc_count;
4918 if (o->reloc_count > 0)
4919 o->flags |= SEC_RELOC;
4922 /* Explicitly clear the SEC_RELOC flag. The linker tends to
4923 set it (this is probably a bug) and if it is set
4924 assign_section_numbers will create a reloc section. */
4925 o->flags &=~ SEC_RELOC;
4928 /* If the SEC_ALLOC flag is not set, force the section VMA to
4929 zero. This is done in elf_fake_sections as well, but forcing
4930 the VMA to 0 here will ensure that relocs against these
4931 sections are handled correctly. */
4932 if ((o->flags & SEC_ALLOC) == 0
4933 && ! o->user_set_vma)
4937 if (! info->relocateable && merged)
4938 elf_link_hash_traverse (elf_hash_table (info),
4939 elf_link_sec_merge_syms, (PTR) abfd);
4941 /* Figure out the file positions for everything but the symbol table
4942 and the relocs. We set symcount to force assign_section_numbers
4943 to create a symbol table. */
4944 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
4945 BFD_ASSERT (! abfd->output_has_begun);
4946 if (! _bfd_elf_compute_section_file_positions (abfd, info))
4949 /* Figure out how many relocations we will have in each section.
4950 Just using RELOC_COUNT isn't good enough since that doesn't
4951 maintain a separate value for REL vs. RELA relocations. */
4953 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
4954 for (o = sub->sections; o != NULL; o = o->next)
4956 asection *output_section;
4958 if (! o->linker_mark)
4960 /* This section was omitted from the link. */
4964 output_section = o->output_section;
4966 if (output_section != NULL
4967 && (o->flags & SEC_RELOC) != 0)
4969 struct bfd_elf_section_data *esdi
4970 = elf_section_data (o);
4971 struct bfd_elf_section_data *esdo
4972 = elf_section_data (output_section);
4973 unsigned int *rel_count;
4974 unsigned int *rel_count2;
4976 /* We must be careful to add the relocation froms the
4977 input section to the right output count. */
4978 if (esdi->rel_hdr.sh_entsize == esdo->rel_hdr.sh_entsize)
4980 rel_count = &esdo->rel_count;
4981 rel_count2 = &esdo->rel_count2;
4985 rel_count = &esdo->rel_count2;
4986 rel_count2 = &esdo->rel_count;
4989 *rel_count += NUM_SHDR_ENTRIES (& esdi->rel_hdr);
4991 *rel_count2 += NUM_SHDR_ENTRIES (esdi->rel_hdr2);
4992 output_section->flags |= SEC_RELOC;
4996 /* That created the reloc sections. Set their sizes, and assign
4997 them file positions, and allocate some buffers. */
4998 for (o = abfd->sections; o != NULL; o = o->next)
5000 if ((o->flags & SEC_RELOC) != 0)
5002 if (!elf_link_size_reloc_section (abfd,
5003 &elf_section_data (o)->rel_hdr,
5007 if (elf_section_data (o)->rel_hdr2
5008 && !elf_link_size_reloc_section (abfd,
5009 elf_section_data (o)->rel_hdr2,
5014 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5015 to count upwards while actually outputting the relocations. */
5016 elf_section_data (o)->rel_count = 0;
5017 elf_section_data (o)->rel_count2 = 0;
5020 _bfd_elf_assign_file_positions_for_relocs (abfd);
5022 /* We have now assigned file positions for all the sections except
5023 .symtab and .strtab. We start the .symtab section at the current
5024 file position, and write directly to it. We build the .strtab
5025 section in memory. */
5026 bfd_get_symcount (abfd) = 0;
5027 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5028 /* sh_name is set in prep_headers. */
5029 symtab_hdr->sh_type = SHT_SYMTAB;
5030 symtab_hdr->sh_flags = 0;
5031 symtab_hdr->sh_addr = 0;
5032 symtab_hdr->sh_size = 0;
5033 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
5034 /* sh_link is set in assign_section_numbers. */
5035 /* sh_info is set below. */
5036 /* sh_offset is set just below. */
5037 symtab_hdr->sh_addralign = bed->s->file_align;
5039 off = elf_tdata (abfd)->next_file_pos;
5040 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
5042 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5043 incorrect. We do not yet know the size of the .symtab section.
5044 We correct next_file_pos below, after we do know the size. */
5046 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5047 continuously seeking to the right position in the file. */
5048 if (! info->keep_memory || max_sym_count < 20)
5049 finfo.symbuf_size = 20;
5051 finfo.symbuf_size = max_sym_count;
5052 amt = finfo.symbuf_size;
5053 amt *= sizeof (Elf_External_Sym);
5054 finfo.symbuf = (Elf_External_Sym *) bfd_malloc (amt);
5055 if (finfo.symbuf == NULL)
5058 /* Start writing out the symbol table. The first symbol is always a
5060 if (info->strip != strip_all
5063 elfsym.st_value = 0;
5066 elfsym.st_other = 0;
5067 elfsym.st_shndx = SHN_UNDEF;
5068 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5069 &elfsym, bfd_und_section_ptr))
5074 /* Some standard ELF linkers do this, but we don't because it causes
5075 bootstrap comparison failures. */
5076 /* Output a file symbol for the output file as the second symbol.
5077 We output this even if we are discarding local symbols, although
5078 I'm not sure if this is correct. */
5079 elfsym.st_value = 0;
5081 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5082 elfsym.st_other = 0;
5083 elfsym.st_shndx = SHN_ABS;
5084 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
5085 &elfsym, bfd_abs_section_ptr))
5089 /* Output a symbol for each section. We output these even if we are
5090 discarding local symbols, since they are used for relocs. These
5091 symbols have no names. We store the index of each one in the
5092 index field of the section, so that we can find it again when
5093 outputting relocs. */
5094 if (info->strip != strip_all
5098 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5099 elfsym.st_other = 0;
5100 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
5102 o = section_from_elf_index (abfd, i);
5104 o->target_index = bfd_get_symcount (abfd);
5105 elfsym.st_shndx = i;
5106 if (info->relocateable || o == NULL)
5107 elfsym.st_value = 0;
5109 elfsym.st_value = o->vma;
5110 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5116 /* Allocate some memory to hold information read in from the input
5118 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
5119 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
5120 finfo.internal_relocs = ((Elf_Internal_Rela *)
5121 bfd_malloc (max_internal_reloc_count
5122 * sizeof (Elf_Internal_Rela)
5123 * bed->s->int_rels_per_ext_rel));
5124 finfo.external_syms = ((Elf_External_Sym *)
5125 bfd_malloc (max_sym_count
5126 * sizeof (Elf_External_Sym)));
5127 finfo.internal_syms = ((Elf_Internal_Sym *)
5128 bfd_malloc (max_sym_count
5129 * sizeof (Elf_Internal_Sym)));
5130 finfo.indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
5131 finfo.sections = ((asection **)
5132 bfd_malloc (max_sym_count * sizeof (asection *)));
5133 if ((finfo.contents == NULL && max_contents_size != 0)
5134 || (finfo.external_relocs == NULL && max_external_reloc_size != 0)
5135 || (finfo.internal_relocs == NULL && max_internal_reloc_count != 0)
5136 || (finfo.external_syms == NULL && max_sym_count != 0)
5137 || (finfo.internal_syms == NULL && max_sym_count != 0)
5138 || (finfo.indices == NULL && max_sym_count != 0)
5139 || (finfo.sections == NULL && max_sym_count != 0))
5142 /* Since ELF permits relocations to be against local symbols, we
5143 must have the local symbols available when we do the relocations.
5144 Since we would rather only read the local symbols once, and we
5145 would rather not keep them in memory, we handle all the
5146 relocations for a single input file at the same time.
5148 Unfortunately, there is no way to know the total number of local
5149 symbols until we have seen all of them, and the local symbol
5150 indices precede the global symbol indices. This means that when
5151 we are generating relocateable output, and we see a reloc against
5152 a global symbol, we can not know the symbol index until we have
5153 finished examining all the local symbols to see which ones we are
5154 going to output. To deal with this, we keep the relocations in
5155 memory, and don't output them until the end of the link. This is
5156 an unfortunate waste of memory, but I don't see a good way around
5157 it. Fortunately, it only happens when performing a relocateable
5158 link, which is not the common case. FIXME: If keep_memory is set
5159 we could write the relocs out and then read them again; I don't
5160 know how bad the memory loss will be. */
5162 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5163 sub->output_has_begun = false;
5164 for (o = abfd->sections; o != NULL; o = o->next)
5166 for (p = o->link_order_head; p != NULL; p = p->next)
5168 if (p->type == bfd_indirect_link_order
5169 && (bfd_get_flavour (p->u.indirect.section->owner)
5170 == bfd_target_elf_flavour))
5172 sub = p->u.indirect.section->owner;
5173 if (! sub->output_has_begun)
5175 if (! elf_link_input_bfd (&finfo, sub))
5177 sub->output_has_begun = true;
5180 else if (p->type == bfd_section_reloc_link_order
5181 || p->type == bfd_symbol_reloc_link_order)
5183 if (! elf_reloc_link_order (abfd, info, o, p))
5188 if (! _bfd_default_link_order (abfd, info, o, p))
5194 /* That wrote out all the local symbols. Finish up the symbol table
5195 with the global symbols. Even if we want to strip everything we
5196 can, we still need to deal with those global symbols that got
5197 converted to local in a version script. */
5201 /* Output any global symbols that got converted to local in a
5202 version script. We do this in a separate step since ELF
5203 requires all local symbols to appear prior to any global
5204 symbols. FIXME: We should only do this if some global
5205 symbols were, in fact, converted to become local. FIXME:
5206 Will this work correctly with the Irix 5 linker? */
5207 eoinfo.failed = false;
5208 eoinfo.finfo = &finfo;
5209 eoinfo.localsyms = true;
5210 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5216 /* The sh_info field records the index of the first non local symbol. */
5217 symtab_hdr->sh_info = bfd_get_symcount (abfd);
5220 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
5222 Elf_Internal_Sym sym;
5223 Elf_External_Sym *dynsym =
5224 (Elf_External_Sym *) finfo.dynsym_sec->contents;
5225 long last_local = 0;
5227 /* Write out the section symbols for the output sections. */
5234 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5237 for (s = abfd->sections; s != NULL; s = s->next)
5240 indx = elf_section_data (s)->this_idx;
5241 BFD_ASSERT (indx > 0);
5242 sym.st_shndx = indx;
5243 sym.st_value = s->vma;
5245 elf_swap_symbol_out (abfd, &sym,
5246 dynsym + elf_section_data (s)->dynindx);
5249 last_local = bfd_count_sections (abfd);
5252 /* Write out the local dynsyms. */
5253 if (elf_hash_table (info)->dynlocal)
5255 struct elf_link_local_dynamic_entry *e;
5256 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
5260 sym.st_size = e->isym.st_size;
5261 sym.st_other = e->isym.st_other;
5263 /* Copy the internal symbol as is.
5264 Note that we saved a word of storage and overwrote
5265 the original st_name with the dynstr_index. */
5268 if (e->isym.st_shndx > 0 && e->isym.st_shndx < SHN_LORESERVE)
5270 s = bfd_section_from_elf_index (e->input_bfd,
5274 elf_section_data (s->output_section)->this_idx;
5275 sym.st_value = (s->output_section->vma
5277 + e->isym.st_value);
5280 if (last_local < e->dynindx)
5281 last_local = e->dynindx;
5283 elf_swap_symbol_out (abfd, &sym, dynsym + e->dynindx);
5287 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
5291 /* We get the global symbols from the hash table. */
5292 eoinfo.failed = false;
5293 eoinfo.localsyms = false;
5294 eoinfo.finfo = &finfo;
5295 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5300 /* If backend needs to output some symbols not present in the hash
5301 table, do it now. */
5302 if (bed->elf_backend_output_arch_syms)
5304 typedef boolean (*out_sym_func) PARAMS ((PTR, const char *,
5308 if (! ((*bed->elf_backend_output_arch_syms)
5309 (abfd, info, (PTR) &finfo, (out_sym_func) elf_link_output_sym)))
5313 /* Flush all symbols to the file. */
5314 if (! elf_link_flush_output_syms (&finfo))
5317 /* Now we know the size of the symtab section. */
5318 off += symtab_hdr->sh_size;
5320 /* Finish up and write out the symbol string table (.strtab)
5322 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5323 /* sh_name was set in prep_headers. */
5324 symstrtab_hdr->sh_type = SHT_STRTAB;
5325 symstrtab_hdr->sh_flags = 0;
5326 symstrtab_hdr->sh_addr = 0;
5327 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
5328 symstrtab_hdr->sh_entsize = 0;
5329 symstrtab_hdr->sh_link = 0;
5330 symstrtab_hdr->sh_info = 0;
5331 /* sh_offset is set just below. */
5332 symstrtab_hdr->sh_addralign = 1;
5334 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true);
5335 elf_tdata (abfd)->next_file_pos = off;
5337 if (bfd_get_symcount (abfd) > 0)
5339 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
5340 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
5344 /* Adjust the relocs to have the correct symbol indices. */
5345 for (o = abfd->sections; o != NULL; o = o->next)
5347 if ((o->flags & SEC_RELOC) == 0)
5350 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
5351 elf_section_data (o)->rel_count,
5352 elf_section_data (o)->rel_hashes);
5353 if (elf_section_data (o)->rel_hdr2 != NULL)
5354 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
5355 elf_section_data (o)->rel_count2,
5356 (elf_section_data (o)->rel_hashes
5357 + elf_section_data (o)->rel_count));
5359 /* Set the reloc_count field to 0 to prevent write_relocs from
5360 trying to swap the relocs out itself. */
5364 if (dynamic && info->combreloc && dynobj != NULL)
5365 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
5367 /* If we are linking against a dynamic object, or generating a
5368 shared library, finish up the dynamic linking information. */
5371 Elf_External_Dyn *dyncon, *dynconend;
5373 /* Fix up .dynamic entries. */
5374 o = bfd_get_section_by_name (dynobj, ".dynamic");
5375 BFD_ASSERT (o != NULL);
5377 dyncon = (Elf_External_Dyn *) o->contents;
5378 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
5379 for (; dyncon < dynconend; dyncon++)
5381 Elf_Internal_Dyn dyn;
5385 elf_swap_dyn_in (dynobj, dyncon, &dyn);
5392 if (relativecount > 0 && dyncon + 1 < dynconend)
5394 switch (elf_section_data (reldyn)->this_hdr.sh_type)
5396 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
5397 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
5400 if (dyn.d_tag != DT_NULL)
5402 dyn.d_un.d_val = relativecount;
5403 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5409 name = info->init_function;
5412 name = info->fini_function;
5415 struct elf_link_hash_entry *h;
5417 h = elf_link_hash_lookup (elf_hash_table (info), name,
5418 false, false, true);
5420 && (h->root.type == bfd_link_hash_defined
5421 || h->root.type == bfd_link_hash_defweak))
5423 dyn.d_un.d_val = h->root.u.def.value;
5424 o = h->root.u.def.section;
5425 if (o->output_section != NULL)
5426 dyn.d_un.d_val += (o->output_section->vma
5427 + o->output_offset);
5430 /* The symbol is imported from another shared
5431 library and does not apply to this one. */
5435 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5450 name = ".gnu.version_d";
5453 name = ".gnu.version_r";
5456 name = ".gnu.version";
5458 o = bfd_get_section_by_name (abfd, name);
5459 BFD_ASSERT (o != NULL);
5460 dyn.d_un.d_ptr = o->vma;
5461 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5468 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5473 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
5475 Elf_Internal_Shdr *hdr;
5477 hdr = elf_elfsections (abfd)[i];
5478 if (hdr->sh_type == type
5479 && (hdr->sh_flags & SHF_ALLOC) != 0)
5481 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
5482 dyn.d_un.d_val += hdr->sh_size;
5485 if (dyn.d_un.d_val == 0
5486 || hdr->sh_addr < dyn.d_un.d_val)
5487 dyn.d_un.d_val = hdr->sh_addr;
5491 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5497 /* If we have created any dynamic sections, then output them. */
5500 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
5503 for (o = dynobj->sections; o != NULL; o = o->next)
5505 if ((o->flags & SEC_HAS_CONTENTS) == 0
5506 || o->_raw_size == 0
5507 || o->output_section == bfd_abs_section_ptr)
5509 if ((o->flags & SEC_LINKER_CREATED) == 0)
5511 /* At this point, we are only interested in sections
5512 created by elf_link_create_dynamic_sections. */
5515 if ((elf_section_data (o->output_section)->this_hdr.sh_type
5517 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
5519 if (! bfd_set_section_contents (abfd, o->output_section,
5521 (file_ptr) o->output_offset,
5527 /* The contents of the .dynstr section are actually in a
5529 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
5530 if (bfd_seek (abfd, off, SEEK_SET) != 0
5531 || ! _bfd_elf_strtab_emit (abfd,
5532 elf_hash_table (info)->dynstr))
5538 /* If we have optimized stabs strings, output them. */
5539 if (elf_hash_table (info)->stab_info != NULL)
5541 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
5545 if (info->eh_frame_hdr)
5547 o = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
5550 && (elf_section_data (o)->sec_info_type
5551 == ELF_INFO_TYPE_EH_FRAME_HDR))
5553 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, o))
5558 if (finfo.symstrtab != NULL)
5559 _bfd_stringtab_free (finfo.symstrtab);
5560 if (finfo.contents != NULL)
5561 free (finfo.contents);
5562 if (finfo.external_relocs != NULL)
5563 free (finfo.external_relocs);
5564 if (finfo.internal_relocs != NULL)
5565 free (finfo.internal_relocs);
5566 if (finfo.external_syms != NULL)
5567 free (finfo.external_syms);
5568 if (finfo.internal_syms != NULL)
5569 free (finfo.internal_syms);
5570 if (finfo.indices != NULL)
5571 free (finfo.indices);
5572 if (finfo.sections != NULL)
5573 free (finfo.sections);
5574 if (finfo.symbuf != NULL)
5575 free (finfo.symbuf);
5576 for (o = abfd->sections; o != NULL; o = o->next)
5578 if ((o->flags & SEC_RELOC) != 0
5579 && elf_section_data (o)->rel_hashes != NULL)
5580 free (elf_section_data (o)->rel_hashes);
5583 elf_tdata (abfd)->linker = true;
5588 if (finfo.symstrtab != NULL)
5589 _bfd_stringtab_free (finfo.symstrtab);
5590 if (finfo.contents != NULL)
5591 free (finfo.contents);
5592 if (finfo.external_relocs != NULL)
5593 free (finfo.external_relocs);
5594 if (finfo.internal_relocs != NULL)
5595 free (finfo.internal_relocs);
5596 if (finfo.external_syms != NULL)
5597 free (finfo.external_syms);
5598 if (finfo.internal_syms != NULL)
5599 free (finfo.internal_syms);
5600 if (finfo.indices != NULL)
5601 free (finfo.indices);
5602 if (finfo.sections != NULL)
5603 free (finfo.sections);
5604 if (finfo.symbuf != NULL)
5605 free (finfo.symbuf);
5606 for (o = abfd->sections; o != NULL; o = o->next)
5608 if ((o->flags & SEC_RELOC) != 0
5609 && elf_section_data (o)->rel_hashes != NULL)
5610 free (elf_section_data (o)->rel_hashes);
5616 /* Add a symbol to the output symbol table. */
5619 elf_link_output_sym (finfo, name, elfsym, input_sec)
5620 struct elf_final_link_info *finfo;
5622 Elf_Internal_Sym *elfsym;
5623 asection *input_sec;
5625 boolean (*output_symbol_hook) PARAMS ((bfd *,
5626 struct bfd_link_info *info,
5631 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
5632 elf_backend_link_output_symbol_hook;
5633 if (output_symbol_hook != NULL)
5635 if (! ((*output_symbol_hook)
5636 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
5640 if (name == (const char *) NULL || *name == '\0')
5641 elfsym->st_name = 0;
5642 else if (input_sec->flags & SEC_EXCLUDE)
5643 elfsym->st_name = 0;
5646 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5648 if (elfsym->st_name == (unsigned long) -1)
5652 if (finfo->symbuf_count >= finfo->symbuf_size)
5654 if (! elf_link_flush_output_syms (finfo))
5658 elf_swap_symbol_out (finfo->output_bfd, elfsym,
5659 (PTR) (finfo->symbuf + finfo->symbuf_count));
5660 ++finfo->symbuf_count;
5662 ++ bfd_get_symcount (finfo->output_bfd);
5667 /* Flush the output symbols to the file. */
5670 elf_link_flush_output_syms (finfo)
5671 struct elf_final_link_info *finfo;
5673 if (finfo->symbuf_count > 0)
5675 Elf_Internal_Shdr *symtab;
5679 symtab = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5680 pos = symtab->sh_offset + symtab->sh_size;
5681 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
5682 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5683 || bfd_bwrite ((PTR) finfo->symbuf, amt, finfo->output_bfd) != amt)
5686 symtab->sh_size += finfo->symbuf_count * sizeof (Elf_External_Sym);
5688 finfo->symbuf_count = 0;
5694 /* Adjust all external symbols pointing into SEC_MERGE sections
5695 to reflect the object merging within the sections. */
5698 elf_link_sec_merge_syms (h, data)
5699 struct elf_link_hash_entry *h;
5704 if ((h->root.type == bfd_link_hash_defined
5705 || h->root.type == bfd_link_hash_defweak)
5706 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
5707 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
5709 bfd *output_bfd = (bfd *) data;
5711 h->root.u.def.value =
5712 _bfd_merged_section_offset (output_bfd,
5713 &h->root.u.def.section,
5714 elf_section_data (sec)->sec_info,
5715 h->root.u.def.value, (bfd_vma) 0);
5721 /* Add an external symbol to the symbol table. This is called from
5722 the hash table traversal routine. When generating a shared object,
5723 we go through the symbol table twice. The first time we output
5724 anything that might have been forced to local scope in a version
5725 script. The second time we output the symbols that are still
5729 elf_link_output_extsym (h, data)
5730 struct elf_link_hash_entry *h;
5733 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
5734 struct elf_final_link_info *finfo = eoinfo->finfo;
5736 Elf_Internal_Sym sym;
5737 asection *input_sec;
5739 /* Decide whether to output this symbol in this pass. */
5740 if (eoinfo->localsyms)
5742 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5747 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5751 /* If we are not creating a shared library, and this symbol is
5752 referenced by a shared library but is not defined anywhere, then
5753 warn that it is undefined. If we do not do this, the runtime
5754 linker will complain that the symbol is undefined when the
5755 program is run. We don't have to worry about symbols that are
5756 referenced by regular files, because we will already have issued
5757 warnings for them. */
5758 if (! finfo->info->relocateable
5759 && ! finfo->info->allow_shlib_undefined
5760 && ! finfo->info->shared
5761 && h->root.type == bfd_link_hash_undefined
5762 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
5763 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
5765 if (! ((*finfo->info->callbacks->undefined_symbol)
5766 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
5767 (asection *) NULL, (bfd_vma) 0, true)))
5769 eoinfo->failed = true;
5774 /* We don't want to output symbols that have never been mentioned by
5775 a regular file, or that we have been told to strip. However, if
5776 h->indx is set to -2, the symbol is used by a reloc and we must
5780 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5781 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
5782 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
5783 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
5785 else if (finfo->info->strip == strip_all
5786 || (finfo->info->strip == strip_some
5787 && bfd_hash_lookup (finfo->info->keep_hash,
5788 h->root.root.string,
5789 false, false) == NULL))
5794 /* If we're stripping it, and it's not a dynamic symbol, there's
5795 nothing else to do unless it is a forced local symbol. */
5798 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5802 sym.st_size = h->size;
5803 sym.st_other = h->other;
5804 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5805 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
5806 else if (h->root.type == bfd_link_hash_undefweak
5807 || h->root.type == bfd_link_hash_defweak)
5808 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
5810 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
5812 switch (h->root.type)
5815 case bfd_link_hash_new:
5819 case bfd_link_hash_undefined:
5820 input_sec = bfd_und_section_ptr;
5821 sym.st_shndx = SHN_UNDEF;
5824 case bfd_link_hash_undefweak:
5825 input_sec = bfd_und_section_ptr;
5826 sym.st_shndx = SHN_UNDEF;
5829 case bfd_link_hash_defined:
5830 case bfd_link_hash_defweak:
5832 input_sec = h->root.u.def.section;
5833 if (input_sec->output_section != NULL)
5836 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
5837 input_sec->output_section);
5838 if (sym.st_shndx == (unsigned short) -1)
5840 (*_bfd_error_handler)
5841 (_("%s: could not find output section %s for input section %s"),
5842 bfd_get_filename (finfo->output_bfd),
5843 input_sec->output_section->name,
5845 eoinfo->failed = true;
5849 /* ELF symbols in relocateable files are section relative,
5850 but in nonrelocateable files they are virtual
5852 sym.st_value = h->root.u.def.value + input_sec->output_offset;
5853 if (! finfo->info->relocateable)
5854 sym.st_value += input_sec->output_section->vma;
5858 BFD_ASSERT (input_sec->owner == NULL
5859 || (input_sec->owner->flags & DYNAMIC) != 0);
5860 sym.st_shndx = SHN_UNDEF;
5861 input_sec = bfd_und_section_ptr;
5866 case bfd_link_hash_common:
5867 input_sec = h->root.u.c.p->section;
5868 sym.st_shndx = SHN_COMMON;
5869 sym.st_value = 1 << h->root.u.c.p->alignment_power;
5872 case bfd_link_hash_indirect:
5873 /* These symbols are created by symbol versioning. They point
5874 to the decorated version of the name. For example, if the
5875 symbol foo@@GNU_1.2 is the default, which should be used when
5876 foo is used with no version, then we add an indirect symbol
5877 foo which points to foo@@GNU_1.2. We ignore these symbols,
5878 since the indirected symbol is already in the hash table. */
5881 case bfd_link_hash_warning:
5882 /* We can't represent these symbols in ELF, although a warning
5883 symbol may have come from a .gnu.warning.SYMBOL section. We
5884 just put the target symbol in the hash table. If the target
5885 symbol does not really exist, don't do anything. */
5886 if (h->root.u.i.link->type == bfd_link_hash_new)
5888 return (elf_link_output_extsym
5889 ((struct elf_link_hash_entry *) h->root.u.i.link, data));
5892 /* Give the processor backend a chance to tweak the symbol value,
5893 and also to finish up anything that needs to be done for this
5895 if ((h->dynindx != -1
5896 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5897 && elf_hash_table (finfo->info)->dynamic_sections_created)
5899 struct elf_backend_data *bed;
5901 bed = get_elf_backend_data (finfo->output_bfd);
5902 if (! ((*bed->elf_backend_finish_dynamic_symbol)
5903 (finfo->output_bfd, finfo->info, h, &sym)))
5905 eoinfo->failed = true;
5910 /* If we are marking the symbol as undefined, and there are no
5911 non-weak references to this symbol from a regular object, then
5912 mark the symbol as weak undefined; if there are non-weak
5913 references, mark the symbol as strong. We can't do this earlier,
5914 because it might not be marked as undefined until the
5915 finish_dynamic_symbol routine gets through with it. */
5916 if (sym.st_shndx == SHN_UNDEF
5917 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
5918 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
5919 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
5923 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
5924 bindtype = STB_GLOBAL;
5926 bindtype = STB_WEAK;
5927 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
5930 /* If a symbol is not defined locally, we clear the visibility
5932 if (! finfo->info->relocateable
5933 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5934 sym.st_other ^= ELF_ST_VISIBILITY (sym.st_other);
5936 /* If this symbol should be put in the .dynsym section, then put it
5937 there now. We have already know the symbol index. We also fill
5938 in the entry in the .hash section. */
5939 if (h->dynindx != -1
5940 && elf_hash_table (finfo->info)->dynamic_sections_created)
5944 size_t hash_entry_size;
5945 bfd_byte *bucketpos;
5947 Elf_External_Sym *esym;
5949 sym.st_name = h->dynstr_index;
5950 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
5951 elf_swap_symbol_out (finfo->output_bfd, &sym, (PTR) esym);
5953 bucketcount = elf_hash_table (finfo->info)->bucketcount;
5954 bucket = h->elf_hash_value % bucketcount;
5956 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
5957 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
5958 + (bucket + 2) * hash_entry_size);
5959 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
5960 bfd_put (8 * hash_entry_size, finfo->output_bfd, (bfd_vma) h->dynindx,
5962 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
5963 ((bfd_byte *) finfo->hash_sec->contents
5964 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
5966 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
5968 Elf_Internal_Versym iversym;
5969 Elf_External_Versym *eversym;
5971 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5973 if (h->verinfo.verdef == NULL)
5974 iversym.vs_vers = 0;
5976 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
5980 if (h->verinfo.vertree == NULL)
5981 iversym.vs_vers = 1;
5983 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
5986 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
5987 iversym.vs_vers |= VERSYM_HIDDEN;
5989 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
5990 eversym += h->dynindx;
5991 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
5995 /* If we're stripping it, then it was just a dynamic symbol, and
5996 there's nothing else to do. */
6000 h->indx = bfd_get_symcount (finfo->output_bfd);
6002 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
6004 eoinfo->failed = true;
6011 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6012 originated from the section given by INPUT_REL_HDR) to the
6016 elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
6019 asection *input_section;
6020 Elf_Internal_Shdr *input_rel_hdr;
6021 Elf_Internal_Rela *internal_relocs;
6023 Elf_Internal_Rela *irela;
6024 Elf_Internal_Rela *irelaend;
6025 Elf_Internal_Shdr *output_rel_hdr;
6026 asection *output_section;
6027 unsigned int *rel_countp = NULL;
6028 struct elf_backend_data *bed;
6031 output_section = input_section->output_section;
6032 output_rel_hdr = NULL;
6034 if (elf_section_data (output_section)->rel_hdr.sh_entsize
6035 == input_rel_hdr->sh_entsize)
6037 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
6038 rel_countp = &elf_section_data (output_section)->rel_count;
6040 else if (elf_section_data (output_section)->rel_hdr2
6041 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
6042 == input_rel_hdr->sh_entsize))
6044 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
6045 rel_countp = &elf_section_data (output_section)->rel_count2;
6048 BFD_ASSERT (output_rel_hdr != NULL);
6050 bed = get_elf_backend_data (output_bfd);
6051 irela = internal_relocs;
6052 irelaend = irela + NUM_SHDR_ENTRIES (input_rel_hdr)
6053 * bed->s->int_rels_per_ext_rel;
6055 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
6057 Elf_External_Rel *erel;
6058 Elf_Internal_Rel *irel;
6060 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
6061 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
6064 (*_bfd_error_handler) (_("Error: out of memory"));
6068 erel = ((Elf_External_Rel *) output_rel_hdr->contents + *rel_countp);
6069 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erel++)
6073 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6075 irel[i].r_offset = irela[i].r_offset;
6076 irel[i].r_info = irela[i].r_info;
6077 BFD_ASSERT (irela[i].r_addend == 0);
6080 if (bed->s->swap_reloc_out)
6081 (*bed->s->swap_reloc_out) (output_bfd, irel, (PTR) erel);
6083 elf_swap_reloc_out (output_bfd, irel, erel);
6090 Elf_External_Rela *erela;
6092 BFD_ASSERT (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rela));
6094 erela = ((Elf_External_Rela *) output_rel_hdr->contents + *rel_countp);
6095 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erela++)
6096 if (bed->s->swap_reloca_out)
6097 (*bed->s->swap_reloca_out) (output_bfd, irela, (PTR) erela);
6099 elf_swap_reloca_out (output_bfd, irela, erela);
6102 /* Bump the counter, so that we know where to add the next set of
6104 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
6107 /* Link an input file into the linker output file. This function
6108 handles all the sections and relocations of the input file at once.
6109 This is so that we only have to read the local symbols once, and
6110 don't have to keep them in memory. */
6113 elf_link_input_bfd (finfo, input_bfd)
6114 struct elf_final_link_info *finfo;
6117 boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
6118 bfd *, asection *, bfd_byte *,
6119 Elf_Internal_Rela *,
6120 Elf_Internal_Sym *, asection **));
6122 Elf_Internal_Shdr *symtab_hdr;
6125 Elf_External_Sym *external_syms;
6126 Elf_External_Sym *esym;
6127 Elf_External_Sym *esymend;
6128 Elf_Internal_Sym *isym;
6130 asection **ppsection;
6132 struct elf_backend_data *bed;
6133 boolean emit_relocs;
6134 struct elf_link_hash_entry **sym_hashes;
6136 output_bfd = finfo->output_bfd;
6137 bed = get_elf_backend_data (output_bfd);
6138 relocate_section = bed->elf_backend_relocate_section;
6140 /* If this is a dynamic object, we don't want to do anything here:
6141 we don't want the local symbols, and we don't want the section
6143 if ((input_bfd->flags & DYNAMIC) != 0)
6146 emit_relocs = (finfo->info->relocateable
6147 || finfo->info->emitrelocations
6148 || bed->elf_backend_emit_relocs);
6150 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6151 if (elf_bad_symtab (input_bfd))
6153 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6158 locsymcount = symtab_hdr->sh_info;
6159 extsymoff = symtab_hdr->sh_info;
6162 /* Read the local symbols. */
6163 if (symtab_hdr->contents != NULL)
6164 external_syms = (Elf_External_Sym *) symtab_hdr->contents;
6165 else if (locsymcount == 0)
6166 external_syms = NULL;
6169 bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym);
6170 external_syms = finfo->external_syms;
6171 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
6172 || bfd_bread (external_syms, amt, input_bfd) != amt)
6176 /* Swap in the local symbols and write out the ones which we know
6177 are going into the output file. */
6178 esym = external_syms;
6179 esymend = esym + locsymcount;
6180 isym = finfo->internal_syms;
6181 pindex = finfo->indices;
6182 ppsection = finfo->sections;
6183 for (; esym < esymend; esym++, isym++, pindex++, ppsection++)
6187 Elf_Internal_Sym osym;
6189 elf_swap_symbol_in (input_bfd, esym, isym);
6192 if (elf_bad_symtab (input_bfd))
6194 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6201 if (isym->st_shndx == SHN_UNDEF)
6202 isec = bfd_und_section_ptr;
6203 else if (isym->st_shndx > 0 && isym->st_shndx < SHN_LORESERVE)
6205 isec = section_from_elf_index (input_bfd, isym->st_shndx);
6207 && elf_section_data (isec)->sec_info_type == ELF_INFO_TYPE_MERGE
6208 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6210 _bfd_merged_section_offset (output_bfd, &isec,
6211 elf_section_data (isec)->sec_info,
6212 isym->st_value, (bfd_vma) 0);
6214 else if (isym->st_shndx == SHN_ABS)
6215 isec = bfd_abs_section_ptr;
6216 else if (isym->st_shndx == SHN_COMMON)
6217 isec = bfd_com_section_ptr;
6226 /* Don't output the first, undefined, symbol. */
6227 if (esym == external_syms)
6230 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6232 /* We never output section symbols. Instead, we use the
6233 section symbol of the corresponding section in the output
6238 /* If we are stripping all symbols, we don't want to output this
6240 if (finfo->info->strip == strip_all)
6243 /* If we are discarding all local symbols, we don't want to
6244 output this one. If we are generating a relocateable output
6245 file, then some of the local symbols may be required by
6246 relocs; we output them below as we discover that they are
6248 if (finfo->info->discard == discard_all)
6251 /* If this symbol is defined in a section which we are
6252 discarding, we don't need to keep it, but note that
6253 linker_mark is only reliable for sections that have contents.
6254 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6255 as well as linker_mark. */
6256 if (isym->st_shndx > 0
6257 && isym->st_shndx < SHN_LORESERVE
6259 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6260 || (! finfo->info->relocateable
6261 && (isec->flags & SEC_EXCLUDE) != 0)))
6264 /* Get the name of the symbol. */
6265 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6270 /* See if we are discarding symbols with this name. */
6271 if ((finfo->info->strip == strip_some
6272 && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
6274 || (((finfo->info->discard == discard_sec_merge
6275 && (isec->flags & SEC_MERGE) && ! finfo->info->relocateable)
6276 || finfo->info->discard == discard_l)
6277 && bfd_is_local_label_name (input_bfd, name)))
6280 /* If we get here, we are going to output this symbol. */
6284 /* Adjust the section index for the output file. */
6285 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6286 isec->output_section);
6287 if (osym.st_shndx == (unsigned short) -1)
6290 *pindex = bfd_get_symcount (output_bfd);
6292 /* ELF symbols in relocateable files are section relative, but
6293 in executable files they are virtual addresses. Note that
6294 this code assumes that all ELF sections have an associated
6295 BFD section with a reasonable value for output_offset; below
6296 we assume that they also have a reasonable value for
6297 output_section. Any special sections must be set up to meet
6298 these requirements. */
6299 osym.st_value += isec->output_offset;
6300 if (! finfo->info->relocateable)
6301 osym.st_value += isec->output_section->vma;
6303 if (! elf_link_output_sym (finfo, name, &osym, isec))
6307 /* Relocate the contents of each section. */
6308 sym_hashes = elf_sym_hashes (input_bfd);
6309 for (o = input_bfd->sections; o != NULL; o = o->next)
6313 if (! o->linker_mark)
6315 /* This section was omitted from the link. */
6319 if ((o->flags & SEC_HAS_CONTENTS) == 0
6320 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
6323 if ((o->flags & SEC_LINKER_CREATED) != 0)
6325 /* Section was created by elf_link_create_dynamic_sections
6330 /* Get the contents of the section. They have been cached by a
6331 relaxation routine. Note that o is a section in an input
6332 file, so the contents field will not have been set by any of
6333 the routines which work on output files. */
6334 if (elf_section_data (o)->this_hdr.contents != NULL)
6335 contents = elf_section_data (o)->this_hdr.contents;
6338 contents = finfo->contents;
6339 if (! bfd_get_section_contents (input_bfd, o, contents,
6340 (file_ptr) 0, o->_raw_size))
6344 if ((o->flags & SEC_RELOC) != 0)
6346 Elf_Internal_Rela *internal_relocs;
6348 /* Get the swapped relocs. */
6349 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
6350 (input_bfd, o, finfo->external_relocs,
6351 finfo->internal_relocs, false));
6352 if (internal_relocs == NULL
6353 && o->reloc_count > 0)
6356 /* Run through the relocs looking for any against symbols
6357 from discarded sections and section symbols from
6358 removed link-once sections. Complain about relocs
6359 against discarded sections. Zero relocs against removed
6360 link-once sections. We should really complain if
6361 anything in the final link tries to use it, but
6362 DWARF-based exception handling might have an entry in
6363 .eh_frame to describe a routine in the linkonce section,
6364 and it turns out to be hard to remove the .eh_frame
6365 entry too. FIXME. */
6366 if (!finfo->info->relocateable
6367 && !elf_section_ignore_discarded_relocs (o))
6369 Elf_Internal_Rela *rel, *relend;
6371 rel = internal_relocs;
6372 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6373 for ( ; rel < relend; rel++)
6375 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
6377 if (r_symndx >= locsymcount
6378 || (elf_bad_symtab (input_bfd)
6379 && finfo->sections[r_symndx] == NULL))
6381 struct elf_link_hash_entry *h;
6383 h = sym_hashes[r_symndx - extsymoff];
6384 while (h->root.type == bfd_link_hash_indirect
6385 || h->root.type == bfd_link_hash_warning)
6386 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6388 /* Complain if the definition comes from a
6389 discarded section. */
6390 if ((h->root.type == bfd_link_hash_defined
6391 || h->root.type == bfd_link_hash_defweak)
6392 && ! bfd_is_abs_section (h->root.u.def.section)
6393 && bfd_is_abs_section (h->root.u.def.section
6395 && (elf_section_data (h->root.u.def.section)
6396 ->sec_info_type != ELF_INFO_TYPE_MERGE))
6398 #if BFD_VERSION_DATE < 20031005
6399 if ((o->flags & SEC_DEBUGGING) != 0)
6401 #if BFD_VERSION_DATE > 20021005
6402 (*finfo->info->callbacks->warning)
6404 _("warning: relocation against removed section; zeroing"),
6405 NULL, input_bfd, o, rel->r_offset);
6407 BFD_ASSERT (r_symndx != 0);
6408 memset (rel, 0, sizeof (*rel));
6413 if (! ((*finfo->info->callbacks->undefined_symbol)
6414 (finfo->info, h->root.root.string,
6415 input_bfd, o, rel->r_offset,
6423 asection *sec = finfo->sections[r_symndx];
6426 && ! bfd_is_abs_section (sec)
6427 && bfd_is_abs_section (sec->output_section)
6428 && (elf_section_data (sec)->sec_info_type
6429 != ELF_INFO_TYPE_MERGE))
6431 #if BFD_VERSION_DATE < 20031005
6432 if ((o->flags & SEC_DEBUGGING) != 0
6433 || (sec->flags & SEC_LINK_ONCE) != 0)
6435 #if BFD_VERSION_DATE > 20021005
6436 (*finfo->info->callbacks->warning)
6438 _("warning: relocation against removed section"),
6439 NULL, input_bfd, o, rel->r_offset);
6441 BFD_ASSERT (r_symndx != 0);
6443 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
6451 = _("local symbols in discarded section %s");
6453 = strlen (sec->name) + strlen (msg) - 1;
6454 char *buf = (char *) bfd_malloc (amt);
6457 sprintf (buf, msg, sec->name);
6459 buf = (char *) sec->name;
6460 ok = (*finfo->info->callbacks
6461 ->undefined_symbol) (finfo->info, buf,
6465 if (buf != sec->name)
6475 /* Relocate the section by invoking a back end routine.
6477 The back end routine is responsible for adjusting the
6478 section contents as necessary, and (if using Rela relocs
6479 and generating a relocateable output file) adjusting the
6480 reloc addend as necessary.
6482 The back end routine does not have to worry about setting
6483 the reloc address or the reloc symbol index.
6485 The back end routine is given a pointer to the swapped in
6486 internal symbols, and can access the hash table entries
6487 for the external symbols via elf_sym_hashes (input_bfd).
6489 When generating relocateable output, the back end routine
6490 must handle STB_LOCAL/STT_SECTION symbols specially. The
6491 output symbol is going to be a section symbol
6492 corresponding to the output section, which will require
6493 the addend to be adjusted. */
6495 if (! (*relocate_section) (output_bfd, finfo->info,
6496 input_bfd, o, contents,
6498 finfo->internal_syms,
6504 Elf_Internal_Rela *irela;
6505 Elf_Internal_Rela *irelaend;
6506 struct elf_link_hash_entry **rel_hash;
6507 Elf_Internal_Shdr *input_rel_hdr;
6508 unsigned int next_erel;
6509 void (*reloc_emitter) PARAMS ((bfd *, asection *,
6510 Elf_Internal_Shdr *,
6511 Elf_Internal_Rela *));
6513 /* Adjust the reloc addresses and symbol indices. */
6515 irela = internal_relocs;
6516 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6517 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6518 + elf_section_data (o->output_section)->rel_count
6519 + elf_section_data (o->output_section)->rel_count2);
6520 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6522 unsigned long r_symndx;
6525 if (next_erel == bed->s->int_rels_per_ext_rel)
6531 irela->r_offset += o->output_offset;
6533 /* Relocs in an executable have to be virtual addresses. */
6534 if (finfo->info->emitrelocations)
6535 irela->r_offset += o->output_section->vma;
6537 r_symndx = ELF_R_SYM (irela->r_info);
6542 if (r_symndx >= locsymcount
6543 || (elf_bad_symtab (input_bfd)
6544 && finfo->sections[r_symndx] == NULL))
6546 struct elf_link_hash_entry *rh;
6549 /* This is a reloc against a global symbol. We
6550 have not yet output all the local symbols, so
6551 we do not know the symbol index of any global
6552 symbol. We set the rel_hash entry for this
6553 reloc to point to the global hash table entry
6554 for this symbol. The symbol index is then
6555 set at the end of elf_bfd_final_link. */
6556 indx = r_symndx - extsymoff;
6557 rh = elf_sym_hashes (input_bfd)[indx];
6558 while (rh->root.type == bfd_link_hash_indirect
6559 || rh->root.type == bfd_link_hash_warning)
6560 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6562 /* Setting the index to -2 tells
6563 elf_link_output_extsym that this symbol is
6565 BFD_ASSERT (rh->indx < 0);
6573 /* This is a reloc against a local symbol. */
6576 isym = finfo->internal_syms + r_symndx;
6577 sec = finfo->sections[r_symndx];
6578 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6580 /* I suppose the backend ought to fill in the
6581 section of any STT_SECTION symbol against a
6582 processor specific section. If we have
6583 discarded a section, the output_section will
6584 be the absolute section. */
6586 && (bfd_is_abs_section (sec)
6587 || (sec->output_section != NULL
6588 && bfd_is_abs_section (sec->output_section))))
6590 else if (sec == NULL || sec->owner == NULL)
6592 bfd_set_error (bfd_error_bad_value);
6597 r_symndx = sec->output_section->target_index;
6598 BFD_ASSERT (r_symndx != 0);
6603 if (finfo->indices[r_symndx] == -1)
6605 unsigned long shlink;
6609 if (finfo->info->strip == strip_all)
6611 /* You can't do ld -r -s. */
6612 bfd_set_error (bfd_error_invalid_operation);
6616 /* This symbol was skipped earlier, but
6617 since it is needed by a reloc, we
6618 must output it now. */
6619 shlink = symtab_hdr->sh_link;
6620 name = (bfd_elf_string_from_elf_section
6621 (input_bfd, shlink, isym->st_name));
6625 osec = sec->output_section;
6627 _bfd_elf_section_from_bfd_section (output_bfd,
6629 if (isym->st_shndx == (unsigned short) -1)
6632 isym->st_value += sec->output_offset;
6633 if (! finfo->info->relocateable)
6634 isym->st_value += osec->vma;
6636 finfo->indices[r_symndx]
6637 = bfd_get_symcount (output_bfd);
6639 if (! elf_link_output_sym (finfo, name, isym, sec))
6643 r_symndx = finfo->indices[r_symndx];
6646 irela->r_info = ELF_R_INFO (r_symndx,
6647 ELF_R_TYPE (irela->r_info));
6650 /* Swap out the relocs. */
6651 if (bed->elf_backend_emit_relocs
6652 && !(finfo->info->relocateable
6653 || finfo->info->emitrelocations))
6654 reloc_emitter = bed->elf_backend_emit_relocs;
6656 reloc_emitter = elf_link_output_relocs;
6658 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6659 (*reloc_emitter) (output_bfd, o, input_rel_hdr, internal_relocs);
6661 input_rel_hdr = elf_section_data (o)->rel_hdr2;
6664 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
6665 * bed->s->int_rels_per_ext_rel);
6666 reloc_emitter (output_bfd, o, input_rel_hdr, internal_relocs);
6672 /* Write out the modified section contents. */
6673 if (bed->elf_backend_write_section
6674 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
6676 /* Section written out. */
6678 else switch (elf_section_data (o)->sec_info_type)
6680 case ELF_INFO_TYPE_STABS:
6681 if (! (_bfd_write_section_stabs
6683 &elf_hash_table (finfo->info)->stab_info,
6684 o, &elf_section_data (o)->sec_info, contents)))
6687 case ELF_INFO_TYPE_MERGE:
6688 if (! (_bfd_write_merged_section
6689 (output_bfd, o, elf_section_data (o)->sec_info)))
6692 case ELF_INFO_TYPE_EH_FRAME:
6697 = bfd_get_section_by_name (elf_hash_table (finfo->info)->dynobj,
6699 if (! (_bfd_elf_write_section_eh_frame (output_bfd, o, ehdrsec,
6706 bfd_size_type sec_size;
6708 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
6709 if (! (o->flags & SEC_EXCLUDE)
6710 && ! bfd_set_section_contents (output_bfd, o->output_section,
6712 (file_ptr) o->output_offset,
6723 /* Generate a reloc when linking an ELF file. This is a reloc
6724 requested by the linker, and does come from any input file. This
6725 is used to build constructor and destructor tables when linking
6729 elf_reloc_link_order (output_bfd, info, output_section, link_order)
6731 struct bfd_link_info *info;
6732 asection *output_section;
6733 struct bfd_link_order *link_order;
6735 reloc_howto_type *howto;
6739 struct elf_link_hash_entry **rel_hash_ptr;
6740 Elf_Internal_Shdr *rel_hdr;
6741 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
6743 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
6746 bfd_set_error (bfd_error_bad_value);
6750 addend = link_order->u.reloc.p->addend;
6752 /* Figure out the symbol index. */
6753 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
6754 + elf_section_data (output_section)->rel_count
6755 + elf_section_data (output_section)->rel_count2);
6756 if (link_order->type == bfd_section_reloc_link_order)
6758 indx = link_order->u.reloc.p->u.section->target_index;
6759 BFD_ASSERT (indx != 0);
6760 *rel_hash_ptr = NULL;
6764 struct elf_link_hash_entry *h;
6766 /* Treat a reloc against a defined symbol as though it were
6767 actually against the section. */
6768 h = ((struct elf_link_hash_entry *)
6769 bfd_wrapped_link_hash_lookup (output_bfd, info,
6770 link_order->u.reloc.p->u.name,
6771 false, false, true));
6773 && (h->root.type == bfd_link_hash_defined
6774 || h->root.type == bfd_link_hash_defweak))
6778 section = h->root.u.def.section;
6779 indx = section->output_section->target_index;
6780 *rel_hash_ptr = NULL;
6781 /* It seems that we ought to add the symbol value to the
6782 addend here, but in practice it has already been added
6783 because it was passed to constructor_callback. */
6784 addend += section->output_section->vma + section->output_offset;
6788 /* Setting the index to -2 tells elf_link_output_extsym that
6789 this symbol is used by a reloc. */
6796 if (! ((*info->callbacks->unattached_reloc)
6797 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
6798 (asection *) NULL, (bfd_vma) 0)))
6804 /* If this is an inplace reloc, we must write the addend into the
6806 if (howto->partial_inplace && addend != 0)
6809 bfd_reloc_status_type rstat;
6812 const char *sym_name;
6814 size = bfd_get_reloc_size (howto);
6815 buf = (bfd_byte *) bfd_zmalloc (size);
6816 if (buf == (bfd_byte *) NULL)
6818 rstat = _bfd_relocate_contents (howto, output_bfd, (bfd_vma) addend, buf);
6825 case bfd_reloc_outofrange:
6828 case bfd_reloc_overflow:
6829 if (link_order->type == bfd_section_reloc_link_order)
6830 sym_name = bfd_section_name (output_bfd,
6831 link_order->u.reloc.p->u.section);
6833 sym_name = link_order->u.reloc.p->u.name;
6834 if (! ((*info->callbacks->reloc_overflow)
6835 (info, sym_name, howto->name, addend,
6836 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
6843 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
6844 (file_ptr) link_order->offset, size);
6850 /* The address of a reloc is relative to the section in a
6851 relocateable file, and is a virtual address in an executable
6853 offset = link_order->offset;
6854 if (! info->relocateable)
6855 offset += output_section->vma;
6857 rel_hdr = &elf_section_data (output_section)->rel_hdr;
6859 if (rel_hdr->sh_type == SHT_REL)
6862 Elf_Internal_Rel *irel;
6863 Elf_External_Rel *erel;
6866 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
6867 irel = (Elf_Internal_Rel *) bfd_zmalloc (size);
6871 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6872 irel[i].r_offset = offset;
6873 irel[0].r_info = ELF_R_INFO (indx, howto->type);
6875 erel = ((Elf_External_Rel *) rel_hdr->contents
6876 + elf_section_data (output_section)->rel_count);
6878 if (bed->s->swap_reloc_out)
6879 (*bed->s->swap_reloc_out) (output_bfd, irel, (bfd_byte *) erel);
6881 elf_swap_reloc_out (output_bfd, irel, erel);
6888 Elf_Internal_Rela *irela;
6889 Elf_External_Rela *erela;
6892 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
6893 irela = (Elf_Internal_Rela *) bfd_zmalloc (size);
6897 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6898 irela[i].r_offset = offset;
6899 irela[0].r_info = ELF_R_INFO (indx, howto->type);
6900 irela[0].r_addend = addend;
6902 erela = ((Elf_External_Rela *) rel_hdr->contents
6903 + elf_section_data (output_section)->rel_count);
6905 if (bed->s->swap_reloca_out)
6906 (*bed->s->swap_reloca_out) (output_bfd, irela, (bfd_byte *) erela);
6908 elf_swap_reloca_out (output_bfd, irela, erela);
6911 ++elf_section_data (output_section)->rel_count;
6916 /* Allocate a pointer to live in a linker created section. */
6919 elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
6921 struct bfd_link_info *info;
6922 elf_linker_section_t *lsect;
6923 struct elf_link_hash_entry *h;
6924 const Elf_Internal_Rela *rel;
6926 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
6927 elf_linker_section_pointers_t *linker_section_ptr;
6928 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
6931 BFD_ASSERT (lsect != NULL);
6933 /* Is this a global symbol? */
6936 /* Has this symbol already been allocated? If so, our work is done. */
6937 if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
6942 ptr_linker_section_ptr = &h->linker_section_pointer;
6943 /* Make sure this symbol is output as a dynamic symbol. */
6944 if (h->dynindx == -1)
6946 if (! elf_link_record_dynamic_symbol (info, h))
6950 if (lsect->rel_section)
6951 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
6955 /* Allocation of a pointer to a local symbol. */
6956 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
6958 /* Allocate a table to hold the local symbols if first time. */
6961 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
6962 register unsigned int i;
6965 amt *= sizeof (elf_linker_section_pointers_t *);
6966 ptr = (elf_linker_section_pointers_t **) bfd_alloc (abfd, amt);
6971 elf_local_ptr_offsets (abfd) = ptr;
6972 for (i = 0; i < num_symbols; i++)
6973 ptr[i] = (elf_linker_section_pointers_t *) 0;
6976 /* Has this symbol already been allocated? If so, our work is done. */
6977 if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
6982 ptr_linker_section_ptr = &ptr[r_symndx];
6986 /* If we are generating a shared object, we need to
6987 output a R_<xxx>_RELATIVE reloc so that the
6988 dynamic linker can adjust this GOT entry. */
6989 BFD_ASSERT (lsect->rel_section != NULL);
6990 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
6994 /* Allocate space for a pointer in the linker section, and allocate
6995 a new pointer record from internal memory. */
6996 BFD_ASSERT (ptr_linker_section_ptr != NULL);
6997 amt = sizeof (elf_linker_section_pointers_t);
6998 linker_section_ptr = (elf_linker_section_pointers_t *) bfd_alloc (abfd, amt);
7000 if (!linker_section_ptr)
7003 linker_section_ptr->next = *ptr_linker_section_ptr;
7004 linker_section_ptr->addend = rel->r_addend;
7005 linker_section_ptr->which = lsect->which;
7006 linker_section_ptr->written_address_p = false;
7007 *ptr_linker_section_ptr = linker_section_ptr;
7010 if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
7012 linker_section_ptr->offset = (lsect->section->_raw_size
7013 - lsect->hole_size + (ARCH_SIZE / 8));
7014 lsect->hole_offset += ARCH_SIZE / 8;
7015 lsect->sym_offset += ARCH_SIZE / 8;
7016 if (lsect->sym_hash)
7018 /* Bump up symbol value if needed. */
7019 lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
7021 fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
7022 lsect->sym_hash->root.root.string,
7023 (long) ARCH_SIZE / 8,
7024 (long) lsect->sym_hash->root.u.def.value);
7030 linker_section_ptr->offset = lsect->section->_raw_size;
7032 lsect->section->_raw_size += ARCH_SIZE / 8;
7036 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7037 lsect->name, (long) linker_section_ptr->offset,
7038 (long) lsect->section->_raw_size);
7045 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7048 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7051 /* Fill in the address for a pointer generated in a linker section. */
7054 elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h,
7055 relocation, rel, relative_reloc)
7058 struct bfd_link_info *info;
7059 elf_linker_section_t *lsect;
7060 struct elf_link_hash_entry *h;
7062 const Elf_Internal_Rela *rel;
7065 elf_linker_section_pointers_t *linker_section_ptr;
7067 BFD_ASSERT (lsect != NULL);
7071 /* Handle global symbol. */
7072 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7073 (h->linker_section_pointer,
7077 BFD_ASSERT (linker_section_ptr != NULL);
7079 if (! elf_hash_table (info)->dynamic_sections_created
7082 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
7084 /* This is actually a static link, or it is a
7085 -Bsymbolic link and the symbol is defined
7086 locally. We must initialize this entry in the
7089 When doing a dynamic link, we create a .rela.<xxx>
7090 relocation entry to initialize the value. This
7091 is done in the finish_dynamic_symbol routine. */
7092 if (!linker_section_ptr->written_address_p)
7094 linker_section_ptr->written_address_p = true;
7095 bfd_put_ptr (output_bfd,
7096 relocation + linker_section_ptr->addend,
7097 (lsect->section->contents
7098 + linker_section_ptr->offset));
7104 /* Handle local symbol. */
7105 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7106 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
7107 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
7108 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7109 (elf_local_ptr_offsets (input_bfd)[r_symndx],
7113 BFD_ASSERT (linker_section_ptr != NULL);
7115 /* Write out pointer if it hasn't been rewritten out before. */
7116 if (!linker_section_ptr->written_address_p)
7118 linker_section_ptr->written_address_p = true;
7119 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
7120 lsect->section->contents + linker_section_ptr->offset);
7124 asection *srel = lsect->rel_section;
7125 Elf_Internal_Rela *outrel;
7126 Elf_External_Rela *erel;
7127 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7131 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
7132 outrel = (Elf_Internal_Rela *) bfd_zmalloc (amt);
7135 (*_bfd_error_handler) (_("Error: out of memory"));
7139 /* We need to generate a relative reloc for the dynamic
7143 srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
7145 lsect->rel_section = srel;
7148 BFD_ASSERT (srel != NULL);
7150 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7151 outrel[i].r_offset = (lsect->section->output_section->vma
7152 + lsect->section->output_offset
7153 + linker_section_ptr->offset);
7154 outrel[0].r_info = ELF_R_INFO (0, relative_reloc);
7155 outrel[0].r_addend = 0;
7156 erel = (Elf_External_Rela *) lsect->section->contents;
7157 erel += elf_section_data (lsect->section)->rel_count;
7158 elf_swap_reloca_out (output_bfd, outrel, erel);
7159 ++elf_section_data (lsect->section)->rel_count;
7166 relocation = (lsect->section->output_offset
7167 + linker_section_ptr->offset
7168 - lsect->hole_offset
7169 - lsect->sym_offset);
7173 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7174 lsect->name, (long) relocation, (long) relocation);
7177 /* Subtract out the addend, because it will get added back in by the normal
7179 return relocation - linker_section_ptr->addend;
7182 /* Garbage collect unused sections. */
7184 static boolean elf_gc_mark
7185 PARAMS ((struct bfd_link_info *info, asection *sec,
7186 asection * (*gc_mark_hook)
7187 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7188 struct elf_link_hash_entry *, Elf_Internal_Sym *))));
7190 static boolean elf_gc_sweep
7191 PARAMS ((struct bfd_link_info *info,
7192 boolean (*gc_sweep_hook)
7193 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7194 const Elf_Internal_Rela *relocs))));
7196 static boolean elf_gc_sweep_symbol
7197 PARAMS ((struct elf_link_hash_entry *h, PTR idxptr));
7199 static boolean elf_gc_allocate_got_offsets
7200 PARAMS ((struct elf_link_hash_entry *h, PTR offarg));
7202 static boolean elf_gc_propagate_vtable_entries_used
7203 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7205 static boolean elf_gc_smash_unused_vtentry_relocs
7206 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7208 /* The mark phase of garbage collection. For a given section, mark
7209 it and any sections in this section's group, and all the sections
7210 which define symbols to which it refers. */
7213 elf_gc_mark (info, sec, gc_mark_hook)
7214 struct bfd_link_info *info;
7216 asection * (*gc_mark_hook)
7217 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7218 struct elf_link_hash_entry *, Elf_Internal_Sym *));
7221 asection *group_sec;
7225 /* Mark all the sections in the group. */
7226 group_sec = elf_section_data (sec)->next_in_group;
7227 if (group_sec && !group_sec->gc_mark)
7228 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
7231 /* Look through the section relocs. */
7233 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
7235 Elf_Internal_Rela *relstart, *rel, *relend;
7236 Elf_Internal_Shdr *symtab_hdr;
7237 struct elf_link_hash_entry **sym_hashes;
7240 Elf_External_Sym *locsyms, *freesyms = NULL;
7241 bfd *input_bfd = sec->owner;
7242 struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
7244 /* GCFIXME: how to arrange so that relocs and symbols are not
7245 reread continually? */
7247 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7248 sym_hashes = elf_sym_hashes (input_bfd);
7250 /* Read the local symbols. */
7251 if (elf_bad_symtab (input_bfd))
7253 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
7257 extsymoff = nlocsyms = symtab_hdr->sh_info;
7258 if (symtab_hdr->contents)
7259 locsyms = (Elf_External_Sym *) symtab_hdr->contents;
7260 else if (nlocsyms == 0)
7264 bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym);
7265 locsyms = freesyms = bfd_malloc (amt);
7266 if (freesyms == NULL
7267 || bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
7268 || bfd_bread (locsyms, amt, input_bfd) != amt)
7275 /* Read the relocations. */
7276 relstart = (NAME(_bfd_elf,link_read_relocs)
7277 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL,
7278 info->keep_memory));
7279 if (relstart == NULL)
7284 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7286 for (rel = relstart; rel < relend; rel++)
7288 unsigned long r_symndx;
7290 struct elf_link_hash_entry *h;
7293 r_symndx = ELF_R_SYM (rel->r_info);
7297 if (elf_bad_symtab (sec->owner))
7299 elf_swap_symbol_in (input_bfd, &locsyms[r_symndx], &s);
7300 if (ELF_ST_BIND (s.st_info) == STB_LOCAL)
7301 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
7304 h = sym_hashes[r_symndx - extsymoff];
7305 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
7308 else if (r_symndx >= nlocsyms)
7310 h = sym_hashes[r_symndx - extsymoff];
7311 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
7315 elf_swap_symbol_in (input_bfd, &locsyms[r_symndx], &s);
7316 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
7319 if (rsec && !rsec->gc_mark)
7320 if (!elf_gc_mark (info, rsec, gc_mark_hook))
7328 if (!info->keep_memory)
7338 /* The sweep phase of garbage collection. Remove all garbage sections. */
7341 elf_gc_sweep (info, gc_sweep_hook)
7342 struct bfd_link_info *info;
7343 boolean (*gc_sweep_hook)
7344 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7345 const Elf_Internal_Rela *relocs));
7349 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7353 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7356 for (o = sub->sections; o != NULL; o = o->next)
7358 /* Keep special sections. Keep .debug sections. */
7359 if ((o->flags & SEC_LINKER_CREATED)
7360 || (o->flags & SEC_DEBUGGING))
7366 /* Skip sweeping sections already excluded. */
7367 if (o->flags & SEC_EXCLUDE)
7370 /* Since this is early in the link process, it is simple
7371 to remove a section from the output. */
7372 o->flags |= SEC_EXCLUDE;
7374 /* But we also have to update some of the relocation
7375 info we collected before. */
7377 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
7379 Elf_Internal_Rela *internal_relocs;
7382 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
7383 (o->owner, o, NULL, NULL, info->keep_memory));
7384 if (internal_relocs == NULL)
7387 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
7389 if (!info->keep_memory)
7390 free (internal_relocs);
7398 /* Remove the symbols that were in the swept sections from the dynamic
7399 symbol table. GCFIXME: Anyone know how to get them out of the
7400 static symbol table as well? */
7404 elf_link_hash_traverse (elf_hash_table (info),
7405 elf_gc_sweep_symbol,
7408 elf_hash_table (info)->dynsymcount = i;
7414 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7417 elf_gc_sweep_symbol (h, idxptr)
7418 struct elf_link_hash_entry *h;
7421 int *idx = (int *) idxptr;
7423 if (h->dynindx != -1
7424 && ((h->root.type != bfd_link_hash_defined
7425 && h->root.type != bfd_link_hash_defweak)
7426 || h->root.u.def.section->gc_mark))
7427 h->dynindx = (*idx)++;
7432 /* Propogate collected vtable information. This is called through
7433 elf_link_hash_traverse. */
7436 elf_gc_propagate_vtable_entries_used (h, okp)
7437 struct elf_link_hash_entry *h;
7440 /* Those that are not vtables. */
7441 if (h->vtable_parent == NULL)
7444 /* Those vtables that do not have parents, we cannot merge. */
7445 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
7448 /* If we've already been done, exit. */
7449 if (h->vtable_entries_used && h->vtable_entries_used[-1])
7452 /* Make sure the parent's table is up to date. */
7453 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
7455 if (h->vtable_entries_used == NULL)
7457 /* None of this table's entries were referenced. Re-use the
7459 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
7460 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
7467 /* Or the parent's entries into ours. */
7468 cu = h->vtable_entries_used;
7470 pu = h->vtable_parent->vtable_entries_used;
7473 asection *sec = h->root.u.def.section;
7474 struct elf_backend_data *bed = get_elf_backend_data (sec->owner);
7475 int file_align = bed->s->file_align;
7477 n = h->vtable_parent->vtable_entries_size / file_align;
7492 elf_gc_smash_unused_vtentry_relocs (h, okp)
7493 struct elf_link_hash_entry *h;
7497 bfd_vma hstart, hend;
7498 Elf_Internal_Rela *relstart, *relend, *rel;
7499 struct elf_backend_data *bed;
7502 /* Take care of both those symbols that do not describe vtables as
7503 well as those that are not loaded. */
7504 if (h->vtable_parent == NULL)
7507 BFD_ASSERT (h->root.type == bfd_link_hash_defined
7508 || h->root.type == bfd_link_hash_defweak);
7510 sec = h->root.u.def.section;
7511 hstart = h->root.u.def.value;
7512 hend = hstart + h->size;
7514 relstart = (NAME(_bfd_elf,link_read_relocs)
7515 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, true));
7517 return *(boolean *) okp = false;
7518 bed = get_elf_backend_data (sec->owner);
7519 file_align = bed->s->file_align;
7521 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7523 for (rel = relstart; rel < relend; ++rel)
7524 if (rel->r_offset >= hstart && rel->r_offset < hend)
7526 /* If the entry is in use, do nothing. */
7527 if (h->vtable_entries_used
7528 && (rel->r_offset - hstart) < h->vtable_entries_size)
7530 bfd_vma entry = (rel->r_offset - hstart) / file_align;
7531 if (h->vtable_entries_used[entry])
7534 /* Otherwise, kill it. */
7535 rel->r_offset = rel->r_info = rel->r_addend = 0;
7541 /* Do mark and sweep of unused sections. */
7544 elf_gc_sections (abfd, info)
7546 struct bfd_link_info *info;
7550 asection * (*gc_mark_hook)
7551 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7552 struct elf_link_hash_entry *h, Elf_Internal_Sym *));
7554 if (!get_elf_backend_data (abfd)->can_gc_sections
7555 || info->relocateable || info->emitrelocations
7556 || elf_hash_table (info)->dynamic_sections_created)
7559 /* Apply transitive closure to the vtable entry usage info. */
7560 elf_link_hash_traverse (elf_hash_table (info),
7561 elf_gc_propagate_vtable_entries_used,
7566 /* Kill the vtable relocations that were not used. */
7567 elf_link_hash_traverse (elf_hash_table (info),
7568 elf_gc_smash_unused_vtentry_relocs,
7573 /* Grovel through relocs to find out who stays ... */
7575 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
7576 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7580 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7583 for (o = sub->sections; o != NULL; o = o->next)
7585 if (o->flags & SEC_KEEP)
7586 if (!elf_gc_mark (info, o, gc_mark_hook))
7591 /* ... and mark SEC_EXCLUDE for those that go. */
7592 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
7598 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
7601 elf_gc_record_vtinherit (abfd, sec, h, offset)
7604 struct elf_link_hash_entry *h;
7607 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7608 struct elf_link_hash_entry **search, *child;
7609 bfd_size_type extsymcount;
7611 /* The sh_info field of the symtab header tells us where the
7612 external symbols start. We don't care about the local symbols at
7614 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
7615 if (!elf_bad_symtab (abfd))
7616 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
7618 sym_hashes = elf_sym_hashes (abfd);
7619 sym_hashes_end = sym_hashes + extsymcount;
7621 /* Hunt down the child symbol, which is in this section at the same
7622 offset as the relocation. */
7623 for (search = sym_hashes; search != sym_hashes_end; ++search)
7625 if ((child = *search) != NULL
7626 && (child->root.type == bfd_link_hash_defined
7627 || child->root.type == bfd_link_hash_defweak)
7628 && child->root.u.def.section == sec
7629 && child->root.u.def.value == offset)
7633 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
7634 bfd_archive_filename (abfd), sec->name,
7635 (unsigned long) offset);
7636 bfd_set_error (bfd_error_invalid_operation);
7642 /* This *should* only be the absolute section. It could potentially
7643 be that someone has defined a non-global vtable though, which
7644 would be bad. It isn't worth paging in the local symbols to be
7645 sure though; that case should simply be handled by the assembler. */
7647 child->vtable_parent = (struct elf_link_hash_entry *) -1;
7650 child->vtable_parent = h;
7655 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
7658 elf_gc_record_vtentry (abfd, sec, h, addend)
7659 bfd *abfd ATTRIBUTE_UNUSED;
7660 asection *sec ATTRIBUTE_UNUSED;
7661 struct elf_link_hash_entry *h;
7664 struct elf_backend_data *bed = get_elf_backend_data (abfd);
7665 int file_align = bed->s->file_align;
7667 if (addend >= h->vtable_entries_size)
7670 boolean *ptr = h->vtable_entries_used;
7672 /* While the symbol is undefined, we have to be prepared to handle
7674 if (h->root.type == bfd_link_hash_undefined)
7681 /* Oops! We've got a reference past the defined end of
7682 the table. This is probably a bug -- shall we warn? */
7687 /* Allocate one extra entry for use as a "done" flag for the
7688 consolidation pass. */
7689 bytes = (size / file_align + 1) * sizeof (boolean);
7693 ptr = bfd_realloc (ptr - 1, (bfd_size_type) bytes);
7699 oldbytes = ((h->vtable_entries_size / file_align + 1)
7700 * sizeof (boolean));
7701 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
7705 ptr = bfd_zmalloc ((bfd_size_type) bytes);
7710 /* And arrange for that done flag to be at index -1. */
7711 h->vtable_entries_used = ptr + 1;
7712 h->vtable_entries_size = size;
7715 h->vtable_entries_used[addend / file_align] = true;
7720 /* And an accompanying bit to work out final got entry offsets once
7721 we're done. Should be called from final_link. */
7724 elf_gc_common_finalize_got_offsets (abfd, info)
7726 struct bfd_link_info *info;
7729 struct elf_backend_data *bed = get_elf_backend_data (abfd);
7732 /* The GOT offset is relative to the .got section, but the GOT header is
7733 put into the .got.plt section, if the backend uses it. */
7734 if (bed->want_got_plt)
7737 gotoff = bed->got_header_size;
7739 /* Do the local .got entries first. */
7740 for (i = info->input_bfds; i; i = i->link_next)
7742 bfd_signed_vma *local_got;
7743 bfd_size_type j, locsymcount;
7744 Elf_Internal_Shdr *symtab_hdr;
7746 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
7749 local_got = elf_local_got_refcounts (i);
7753 symtab_hdr = &elf_tdata (i)->symtab_hdr;
7754 if (elf_bad_symtab (i))
7755 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
7757 locsymcount = symtab_hdr->sh_info;
7759 for (j = 0; j < locsymcount; ++j)
7761 if (local_got[j] > 0)
7763 local_got[j] = gotoff;
7764 gotoff += ARCH_SIZE / 8;
7767 local_got[j] = (bfd_vma) -1;
7771 /* Then the global .got entries. .plt refcounts are handled by
7772 adjust_dynamic_symbol */
7773 elf_link_hash_traverse (elf_hash_table (info),
7774 elf_gc_allocate_got_offsets,
7779 /* We need a special top-level link routine to convert got reference counts
7780 to real got offsets. */
7783 elf_gc_allocate_got_offsets (h, offarg)
7784 struct elf_link_hash_entry *h;
7787 bfd_vma *off = (bfd_vma *) offarg;
7789 if (h->got.refcount > 0)
7791 h->got.offset = off[0];
7792 off[0] += ARCH_SIZE / 8;
7795 h->got.offset = (bfd_vma) -1;
7800 /* Many folk need no more in the way of final link than this, once
7801 got entry reference counting is enabled. */
7804 elf_gc_common_final_link (abfd, info)
7806 struct bfd_link_info *info;
7808 if (!elf_gc_common_finalize_got_offsets (abfd, info))
7811 /* Invoke the regular ELF backend linker to do all the work. */
7812 return elf_bfd_final_link (abfd, info);
7815 /* This function will be called though elf_link_hash_traverse to store
7816 all hash value of the exported symbols in an array. */
7819 elf_collect_hash_codes (h, data)
7820 struct elf_link_hash_entry *h;
7823 unsigned long **valuep = (unsigned long **) data;
7829 /* Ignore indirect symbols. These are added by the versioning code. */
7830 if (h->dynindx == -1)
7833 name = h->root.root.string;
7834 p = strchr (name, ELF_VER_CHR);
7837 alc = bfd_malloc ((bfd_size_type) (p - name + 1));
7838 memcpy (alc, name, (size_t) (p - name));
7839 alc[p - name] = '\0';
7843 /* Compute the hash value. */
7844 ha = bfd_elf_hash (name);
7846 /* Store the found hash value in the array given as the argument. */
7849 /* And store it in the struct so that we can put it in the hash table
7851 h->elf_hash_value = ha;
7860 elf_reloc_symbol_deleted_p (offset, cookie)
7864 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *)cookie;
7866 if (rcookie->bad_symtab)
7867 rcookie->rel = rcookie->rels;
7869 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
7871 unsigned long r_symndx = ELF_R_SYM (rcookie->rel->r_info);
7872 Elf_Internal_Sym isym;
7874 if (! rcookie->bad_symtab)
7875 if (rcookie->rel->r_offset > offset)
7877 if (rcookie->rel->r_offset != offset)
7880 if (rcookie->locsyms && r_symndx < rcookie->locsymcount)
7881 elf_swap_symbol_in (rcookie->abfd,
7882 (Elf_External_Sym *) rcookie->locsyms + r_symndx,
7885 if (r_symndx >= rcookie->locsymcount
7886 || (rcookie->locsyms
7887 && ELF_ST_BIND (isym.st_info) != STB_LOCAL))
7889 struct elf_link_hash_entry *h;
7891 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
7893 while (h->root.type == bfd_link_hash_indirect
7894 || h->root.type == bfd_link_hash_warning)
7895 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7897 if ((h->root.type == bfd_link_hash_defined
7898 || h->root.type == bfd_link_hash_defweak)
7899 && ! bfd_is_abs_section (h->root.u.def.section)
7900 && bfd_is_abs_section (h->root.u.def.section
7906 else if (rcookie->locsyms)
7908 /* It's not a relocation against a global symbol,
7909 but it could be a relocation against a local
7910 symbol for a discarded section. */
7913 /* Need to: get the symbol; get the section. */
7914 if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
7916 isec = section_from_elf_index (rcookie->abfd, isym.st_shndx);
7918 && ! bfd_is_abs_section (isec)
7919 && bfd_is_abs_section (isec->output_section))
7928 /* Discard unneeded references to discarded sections.
7929 Returns true if any section's size was changed. */
7930 /* This function assumes that the relocations are in sorted order,
7931 which is true for all known assemblers. */
7934 elf_bfd_discard_info (output_bfd, info)
7936 struct bfd_link_info *info;
7938 struct elf_reloc_cookie cookie;
7939 asection *stab, *eh, *ehdr;
7940 Elf_Internal_Shdr *symtab_hdr;
7941 Elf_External_Sym *freesyms;
7942 struct elf_backend_data *bed;
7944 boolean ret = false;
7945 boolean strip = info->strip == strip_all || info->strip == strip_debugger;
7947 if (info->relocateable
7948 || info->traditional_format
7949 || info->hash->creator->flavour != bfd_target_elf_flavour
7950 || ! is_elf_hash_table (info))
7953 ehdr = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
7956 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
7958 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
7961 bed = get_elf_backend_data (abfd);
7963 if ((abfd->flags & DYNAMIC) != 0)
7969 eh = bfd_get_section_by_name (abfd, ".eh_frame");
7970 if (eh && eh->_raw_size == 0)
7974 stab = strip ? NULL : bfd_get_section_by_name (abfd, ".stab");
7975 if ((! stab || elf_section_data(stab)->sec_info_type != ELF_INFO_TYPE_STABS)
7977 && (strip || ! bed->elf_backend_discard_info))
7980 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7983 cookie.sym_hashes = elf_sym_hashes (abfd);
7984 cookie.bad_symtab = elf_bad_symtab (abfd);
7985 if (cookie.bad_symtab)
7987 cookie.locsymcount =
7988 symtab_hdr->sh_size / sizeof (Elf_External_Sym);
7989 cookie.extsymoff = 0;
7993 cookie.locsymcount = symtab_hdr->sh_info;
7994 cookie.extsymoff = symtab_hdr->sh_info;
7998 if (symtab_hdr->contents)
7999 cookie.locsyms = (void *) symtab_hdr->contents;
8000 else if (cookie.locsymcount == 0)
8001 cookie.locsyms = NULL;
8004 bfd_size_type amt = cookie.locsymcount * sizeof (Elf_External_Sym);
8005 cookie.locsyms = bfd_malloc (amt);
8006 if (cookie.locsyms == NULL
8007 || bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
8008 || bfd_bread (cookie.locsyms, amt, abfd) != amt)
8010 /* Something is very wrong - but we can still do our job for
8011 global symbols, so don't give up. */
8013 free (cookie.locsyms);
8014 cookie.locsyms = NULL;
8018 freesyms = cookie.locsyms;
8024 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8025 (abfd, stab, (PTR) NULL,
8026 (Elf_Internal_Rela *) NULL,
8027 info->keep_memory));
8030 cookie.rel = cookie.rels;
8032 cookie.rels + stab->reloc_count * bed->s->int_rels_per_ext_rel;
8033 if (_bfd_discard_section_stabs (abfd, stab,
8034 elf_section_data (stab)->sec_info,
8035 elf_reloc_symbol_deleted_p,
8038 if (! info->keep_memory)
8047 cookie.relend = NULL;
8048 if (eh->reloc_count)
8049 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8050 (abfd, eh, (PTR) NULL,
8051 (Elf_Internal_Rela *) NULL,
8052 info->keep_memory));
8055 cookie.rel = cookie.rels;
8057 cookie.rels + eh->reloc_count * bed->s->int_rels_per_ext_rel;
8059 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, ehdr,
8060 elf_reloc_symbol_deleted_p,
8063 if (! info->keep_memory)
8067 if (bed->elf_backend_discard_info)
8069 if (bed->elf_backend_discard_info (abfd, &cookie, info))
8078 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd,
8085 elf_section_ignore_discarded_relocs (sec)
8088 switch (elf_section_data (sec)->sec_info_type)
8090 case ELF_INFO_TYPE_STABS:
8091 case ELF_INFO_TYPE_EH_FRAME:
8096 if ((get_elf_backend_data (sec->owner)->elf_backend_ignore_discarded_relocs
8098 && (*get_elf_backend_data (sec->owner)
8099 ->elf_backend_ignore_discarded_relocs) (sec))