2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
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_assign_sym_version
61 PARAMS ((struct elf_link_hash_entry *, PTR));
62 static boolean elf_collect_hash_codes
63 PARAMS ((struct elf_link_hash_entry *, PTR));
64 static boolean elf_link_read_relocs_from_section
65 PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *));
66 static size_t compute_bucket_count
67 PARAMS ((struct bfd_link_info *));
68 static boolean elf_link_output_relocs
69 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
70 static boolean elf_link_size_reloc_section
71 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
72 static void elf_link_adjust_relocs
73 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int,
74 struct elf_link_hash_entry **));
75 static int elf_link_sort_cmp1
76 PARAMS ((const void *, const void *));
77 static int elf_link_sort_cmp2
78 PARAMS ((const void *, const void *));
79 static size_t elf_link_sort_relocs
80 PARAMS ((bfd *, struct bfd_link_info *, asection **));
81 static boolean elf_section_ignore_discarded_relocs
82 PARAMS ((asection *));
84 /* Given an ELF BFD, add symbols to the global hash table as
88 elf_bfd_link_add_symbols (abfd, info)
90 struct bfd_link_info *info;
92 switch (bfd_get_format (abfd))
95 return elf_link_add_object_symbols (abfd, info);
97 return elf_link_add_archive_symbols (abfd, info);
99 bfd_set_error (bfd_error_wrong_format);
104 /* Return true iff this is a non-common, definition of a non-function symbol. */
106 is_global_data_symbol_definition (abfd, sym)
107 bfd * abfd ATTRIBUTE_UNUSED;
108 Elf_Internal_Sym * sym;
110 /* Local symbols do not count, but target specific ones might. */
111 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
112 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
115 /* Function symbols do not count. */
116 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
119 /* If the section is undefined, then so is the symbol. */
120 if (sym->st_shndx == SHN_UNDEF)
123 /* If the symbol is defined in the common section, then
124 it is a common definition and so does not count. */
125 if (sym->st_shndx == SHN_COMMON)
128 /* If the symbol is in a target specific section then we
129 must rely upon the backend to tell us what it is. */
130 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
131 /* FIXME - this function is not coded yet:
133 return _bfd_is_global_symbol_definition (abfd, sym);
135 Instead for now assume that the definition is not global,
136 Even if this is wrong, at least the linker will behave
137 in the same way that it used to do. */
143 /* Search the symbol table of the archive element of the archive ABFD
144 whose archive map contains a mention of SYMDEF, and determine if
145 the symbol is defined in this element. */
147 elf_link_is_defined_archive_symbol (abfd, symdef)
151 Elf_Internal_Shdr * hdr;
152 Elf_Internal_Shdr * shndx_hdr;
153 Elf_External_Sym * esym;
154 Elf_External_Sym * esymend;
155 Elf_External_Sym * buf = NULL;
156 Elf_External_Sym_Shndx * shndx_buf = NULL;
157 Elf_External_Sym_Shndx * shndx;
158 bfd_size_type symcount;
159 bfd_size_type extsymcount;
160 bfd_size_type extsymoff;
161 boolean result = false;
165 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
166 if (abfd == (bfd *) NULL)
169 if (! bfd_check_format (abfd, bfd_object))
172 /* If we have already included the element containing this symbol in the
173 link then we do not need to include it again. Just claim that any symbol
174 it contains is not a definition, so that our caller will not decide to
175 (re)include this element. */
176 if (abfd->archive_pass)
179 /* Select the appropriate symbol table. */
180 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
182 hdr = &elf_tdata (abfd)->symtab_hdr;
183 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
187 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
191 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
193 /* The sh_info field of the symtab header tells us where the
194 external symbols start. We don't care about the local symbols. */
195 if (elf_bad_symtab (abfd))
197 extsymcount = symcount;
202 extsymcount = symcount - hdr->sh_info;
203 extsymoff = hdr->sh_info;
206 amt = extsymcount * sizeof (Elf_External_Sym);
207 buf = (Elf_External_Sym *) bfd_malloc (amt);
208 if (buf == NULL && extsymcount != 0)
211 /* Read in the symbol table.
212 FIXME: This ought to be cached somewhere. */
213 pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
214 if (bfd_seek (abfd, pos, SEEK_SET) != 0
215 || bfd_bread ((PTR) buf, amt, abfd) != amt)
218 if (shndx_hdr != NULL && shndx_hdr->sh_size != 0)
220 amt = extsymcount * sizeof (Elf_External_Sym_Shndx);
221 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
222 if (shndx_buf == NULL && extsymcount != 0)
225 pos = shndx_hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym_Shndx);
226 if (bfd_seek (abfd, pos, SEEK_SET) != 0
227 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
231 /* Scan the symbol table looking for SYMDEF. */
232 esymend = buf + extsymcount;
233 for (esym = buf, shndx = shndx_buf;
235 esym++, shndx = (shndx != NULL ? shndx + 1 : NULL))
237 Elf_Internal_Sym sym;
240 elf_swap_symbol_in (abfd, (const PTR) esym, (const PTR) shndx, &sym);
242 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
243 if (name == (const char *) NULL)
246 if (strcmp (name, symdef->name) == 0)
248 result = is_global_data_symbol_definition (abfd, & sym);
254 if (shndx_buf != NULL)
262 /* Add symbols from an ELF archive file to the linker hash table. We
263 don't use _bfd_generic_link_add_archive_symbols because of a
264 problem which arises on UnixWare. The UnixWare libc.so is an
265 archive which includes an entry libc.so.1 which defines a bunch of
266 symbols. The libc.so archive also includes a number of other
267 object files, which also define symbols, some of which are the same
268 as those defined in libc.so.1. Correct linking requires that we
269 consider each object file in turn, and include it if it defines any
270 symbols we need. _bfd_generic_link_add_archive_symbols does not do
271 this; it looks through the list of undefined symbols, and includes
272 any object file which defines them. When this algorithm is used on
273 UnixWare, it winds up pulling in libc.so.1 early and defining a
274 bunch of symbols. This means that some of the other objects in the
275 archive are not included in the link, which is incorrect since they
276 precede libc.so.1 in the archive.
278 Fortunately, ELF archive handling is simpler than that done by
279 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
280 oddities. In ELF, if we find a symbol in the archive map, and the
281 symbol is currently undefined, we know that we must pull in that
284 Unfortunately, we do have to make multiple passes over the symbol
285 table until nothing further is resolved. */
288 elf_link_add_archive_symbols (abfd, info)
290 struct bfd_link_info *info;
293 boolean *defined = NULL;
294 boolean *included = NULL;
299 if (! bfd_has_map (abfd))
301 /* An empty archive is a special case. */
302 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
304 bfd_set_error (bfd_error_no_armap);
308 /* Keep track of all symbols we know to be already defined, and all
309 files we know to be already included. This is to speed up the
310 second and subsequent passes. */
311 c = bfd_ardata (abfd)->symdef_count;
315 amt *= sizeof (boolean);
316 defined = (boolean *) bfd_zmalloc (amt);
317 included = (boolean *) bfd_zmalloc (amt);
318 if (defined == (boolean *) NULL || included == (boolean *) NULL)
321 symdefs = bfd_ardata (abfd)->symdefs;
334 symdefend = symdef + c;
335 for (i = 0; symdef < symdefend; symdef++, i++)
337 struct elf_link_hash_entry *h;
339 struct bfd_link_hash_entry *undefs_tail;
342 if (defined[i] || included[i])
344 if (symdef->file_offset == last)
350 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
351 false, false, false);
357 /* If this is a default version (the name contains @@),
358 look up the symbol again without the version. The
359 effect is that references to the symbol without the
360 version will be matched by the default symbol in the
363 p = strchr (symdef->name, ELF_VER_CHR);
364 if (p == NULL || p[1] != ELF_VER_CHR)
367 copy = bfd_alloc (abfd, (bfd_size_type) (p - symdef->name + 1));
370 memcpy (copy, symdef->name, (size_t) (p - symdef->name));
371 copy[p - symdef->name] = '\0';
373 h = elf_link_hash_lookup (elf_hash_table (info), copy,
374 false, false, false);
376 bfd_release (abfd, copy);
382 if (h->root.type == bfd_link_hash_common)
384 /* We currently have a common symbol. The archive map contains
385 a reference to this symbol, so we may want to include it. We
386 only want to include it however, if this archive element
387 contains a definition of the symbol, not just another common
390 Unfortunately some archivers (including GNU ar) will put
391 declarations of common symbols into their archive maps, as
392 well as real definitions, so we cannot just go by the archive
393 map alone. Instead we must read in the element's symbol
394 table and check that to see what kind of symbol definition
396 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
399 else if (h->root.type != bfd_link_hash_undefined)
401 if (h->root.type != bfd_link_hash_undefweak)
406 /* We need to include this archive member. */
407 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
408 if (element == (bfd *) NULL)
411 if (! bfd_check_format (element, bfd_object))
414 /* Doublecheck that we have not included this object
415 already--it should be impossible, but there may be
416 something wrong with the archive. */
417 if (element->archive_pass != 0)
419 bfd_set_error (bfd_error_bad_value);
422 element->archive_pass = 1;
424 undefs_tail = info->hash->undefs_tail;
426 if (! (*info->callbacks->add_archive_element) (info, element,
429 if (! elf_link_add_object_symbols (element, info))
432 /* If there are any new undefined symbols, we need to make
433 another pass through the archive in order to see whether
434 they can be defined. FIXME: This isn't perfect, because
435 common symbols wind up on undefs_tail and because an
436 undefined symbol which is defined later on in this pass
437 does not require another pass. This isn't a bug, but it
438 does make the code less efficient than it could be. */
439 if (undefs_tail != info->hash->undefs_tail)
442 /* Look backward to mark all symbols from this object file
443 which we have already seen in this pass. */
447 included[mark] = true;
452 while (symdefs[mark].file_offset == symdef->file_offset);
454 /* We mark subsequent symbols from this object file as we go
455 on through the loop. */
456 last = symdef->file_offset;
467 if (defined != (boolean *) NULL)
469 if (included != (boolean *) NULL)
474 /* This function is called when we want to define a new symbol. It
475 handles the various cases which arise when we find a definition in
476 a dynamic object, or when there is already a definition in a
477 dynamic object. The new symbol is described by NAME, SYM, PSEC,
478 and PVALUE. We set SYM_HASH to the hash table entry. We set
479 OVERRIDE if the old symbol is overriding a new definition. We set
480 TYPE_CHANGE_OK if it is OK for the type to change. We set
481 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
482 change, we mean that we shouldn't warn if the type or size does
483 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
487 elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
488 override, type_change_ok, size_change_ok, dt_needed)
490 struct bfd_link_info *info;
492 Elf_Internal_Sym *sym;
495 struct elf_link_hash_entry **sym_hash;
497 boolean *type_change_ok;
498 boolean *size_change_ok;
502 struct elf_link_hash_entry *h;
505 boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
510 bind = ELF_ST_BIND (sym->st_info);
512 if (! bfd_is_und_section (sec))
513 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
515 h = ((struct elf_link_hash_entry *)
516 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
521 /* This code is for coping with dynamic objects, and is only useful
522 if we are doing an ELF link. */
523 if (info->hash->creator != abfd->xvec)
526 /* For merging, we only care about real symbols. */
528 while (h->root.type == bfd_link_hash_indirect
529 || h->root.type == bfd_link_hash_warning)
530 h = (struct elf_link_hash_entry *) h->root.u.i.link;
532 /* If we just created the symbol, mark it as being an ELF symbol.
533 Other than that, there is nothing to do--there is no merge issue
534 with a newly defined symbol--so we just return. */
536 if (h->root.type == bfd_link_hash_new)
538 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
542 /* OLDBFD is a BFD associated with the existing symbol. */
544 switch (h->root.type)
550 case bfd_link_hash_undefined:
551 case bfd_link_hash_undefweak:
552 oldbfd = h->root.u.undef.abfd;
555 case bfd_link_hash_defined:
556 case bfd_link_hash_defweak:
557 oldbfd = h->root.u.def.section->owner;
560 case bfd_link_hash_common:
561 oldbfd = h->root.u.c.p->section->owner;
565 /* In cases involving weak versioned symbols, we may wind up trying
566 to merge a symbol with itself. Catch that here, to avoid the
567 confusion that results if we try to override a symbol with
568 itself. The additional tests catch cases like
569 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
570 dynamic object, which we do want to handle here. */
572 && ((abfd->flags & DYNAMIC) == 0
573 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
576 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
577 respectively, is from a dynamic object. */
579 if ((abfd->flags & DYNAMIC) != 0)
585 olddyn = (oldbfd->flags & DYNAMIC) != 0;
590 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
591 indices used by MIPS ELF. */
592 switch (h->root.type)
598 case bfd_link_hash_defined:
599 case bfd_link_hash_defweak:
600 hsec = h->root.u.def.section;
603 case bfd_link_hash_common:
604 hsec = h->root.u.c.p->section;
611 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
614 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
615 respectively, appear to be a definition rather than reference. */
617 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
622 if (h->root.type == bfd_link_hash_undefined
623 || h->root.type == bfd_link_hash_undefweak
624 || h->root.type == bfd_link_hash_common)
629 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
630 symbol, respectively, appears to be a common symbol in a dynamic
631 object. If a symbol appears in an uninitialized section, and is
632 not weak, and is not a function, then it may be a common symbol
633 which was resolved when the dynamic object was created. We want
634 to treat such symbols specially, because they raise special
635 considerations when setting the symbol size: if the symbol
636 appears as a common symbol in a regular object, and the size in
637 the regular object is larger, we must make sure that we use the
638 larger size. This problematic case can always be avoided in C,
639 but it must be handled correctly when using Fortran shared
642 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
643 likewise for OLDDYNCOMMON and OLDDEF.
645 Note that this test is just a heuristic, and that it is quite
646 possible to have an uninitialized symbol in a shared object which
647 is really a definition, rather than a common symbol. This could
648 lead to some minor confusion when the symbol really is a common
649 symbol in some regular object. However, I think it will be
654 && (sec->flags & SEC_ALLOC) != 0
655 && (sec->flags & SEC_LOAD) == 0
658 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
661 newdyncommon = false;
665 && h->root.type == bfd_link_hash_defined
666 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
667 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
668 && (h->root.u.def.section->flags & SEC_LOAD) == 0
670 && h->type != STT_FUNC)
673 olddyncommon = false;
675 /* It's OK to change the type if either the existing symbol or the
676 new symbol is weak unless it comes from a DT_NEEDED entry of
677 a shared object, in which case, the DT_NEEDED entry may not be
678 required at the run time. */
680 if ((! dt_needed && h->root.type == bfd_link_hash_defweak)
681 || h->root.type == bfd_link_hash_undefweak
683 *type_change_ok = true;
685 /* It's OK to change the size if either the existing symbol or the
686 new symbol is weak, or if the old symbol is undefined. */
689 || h->root.type == bfd_link_hash_undefined)
690 *size_change_ok = true;
692 /* If both the old and the new symbols look like common symbols in a
693 dynamic object, set the size of the symbol to the larger of the
698 && sym->st_size != h->size)
700 /* Since we think we have two common symbols, issue a multiple
701 common warning if desired. Note that we only warn if the
702 size is different. If the size is the same, we simply let
703 the old symbol override the new one as normally happens with
704 symbols defined in dynamic objects. */
706 if (! ((*info->callbacks->multiple_common)
707 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
708 h->size, abfd, bfd_link_hash_common, sym->st_size)))
711 if (sym->st_size > h->size)
712 h->size = sym->st_size;
714 *size_change_ok = true;
717 /* If we are looking at a dynamic object, and we have found a
718 definition, we need to see if the symbol was already defined by
719 some other object. If so, we want to use the existing
720 definition, and we do not want to report a multiple symbol
721 definition error; we do this by clobbering *PSEC to be
724 We treat a common symbol as a definition if the symbol in the
725 shared library is a function, since common symbols always
726 represent variables; this can cause confusion in principle, but
727 any such confusion would seem to indicate an erroneous program or
728 shared library. We also permit a common symbol in a regular
729 object to override a weak symbol in a shared object.
731 We prefer a non-weak definition in a shared library to a weak
732 definition in the executable unless it comes from a DT_NEEDED
733 entry of a shared object, in which case, the DT_NEEDED entry
734 may not be required at the run time. */
739 || (h->root.type == bfd_link_hash_common
741 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
742 && (h->root.type != bfd_link_hash_defweak
744 || bind == STB_WEAK))
748 newdyncommon = false;
750 *psec = sec = bfd_und_section_ptr;
751 *size_change_ok = true;
753 /* If we get here when the old symbol is a common symbol, then
754 we are explicitly letting it override a weak symbol or
755 function in a dynamic object, and we don't want to warn about
756 a type change. If the old symbol is a defined symbol, a type
757 change warning may still be appropriate. */
759 if (h->root.type == bfd_link_hash_common)
760 *type_change_ok = true;
763 /* Handle the special case of an old common symbol merging with a
764 new symbol which looks like a common symbol in a shared object.
765 We change *PSEC and *PVALUE to make the new symbol look like a
766 common symbol, and let _bfd_generic_link_add_one_symbol will do
770 && h->root.type == bfd_link_hash_common)
774 newdyncommon = false;
775 *pvalue = sym->st_size;
776 *psec = sec = bfd_com_section_ptr;
777 *size_change_ok = true;
780 /* If the old symbol is from a dynamic object, and the new symbol is
781 a definition which is not from a dynamic object, then the new
782 symbol overrides the old symbol. Symbols from regular files
783 always take precedence over symbols from dynamic objects, even if
784 they are defined after the dynamic object in the link.
786 As above, we again permit a common symbol in a regular object to
787 override a definition in a shared object if the shared object
788 symbol is a function or is weak.
790 As above, we permit a non-weak definition in a shared object to
791 override a weak definition in a regular object. */
795 || (bfd_is_com_section (sec)
796 && (h->root.type == bfd_link_hash_defweak
797 || h->type == STT_FUNC)))
800 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
802 || h->root.type == bfd_link_hash_defweak))
804 /* Change the hash table entry to undefined, and let
805 _bfd_generic_link_add_one_symbol do the right thing with the
808 h->root.type = bfd_link_hash_undefined;
809 h->root.u.undef.abfd = h->root.u.def.section->owner;
810 *size_change_ok = true;
813 olddyncommon = false;
815 /* We again permit a type change when a common symbol may be
816 overriding a function. */
818 if (bfd_is_com_section (sec))
819 *type_change_ok = true;
821 /* This union may have been set to be non-NULL when this symbol
822 was seen in a dynamic object. We must force the union to be
823 NULL, so that it is correct for a regular symbol. */
825 h->verinfo.vertree = NULL;
827 /* In this special case, if H is the target of an indirection,
828 we want the caller to frob with H rather than with the
829 indirect symbol. That will permit the caller to redefine the
830 target of the indirection, rather than the indirect symbol
831 itself. FIXME: This will break the -y option if we store a
832 symbol with a different name. */
836 /* Handle the special case of a new common symbol merging with an
837 old symbol that looks like it might be a common symbol defined in
838 a shared object. Note that we have already handled the case in
839 which a new common symbol should simply override the definition
840 in the shared library. */
843 && bfd_is_com_section (sec)
846 /* It would be best if we could set the hash table entry to a
847 common symbol, but we don't know what to use for the section
849 if (! ((*info->callbacks->multiple_common)
850 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
851 h->size, abfd, bfd_link_hash_common, sym->st_size)))
854 /* If the predumed common symbol in the dynamic object is
855 larger, pretend that the new symbol has its size. */
857 if (h->size > *pvalue)
860 /* FIXME: We no longer know the alignment required by the symbol
861 in the dynamic object, so we just wind up using the one from
862 the regular object. */
865 olddyncommon = false;
867 h->root.type = bfd_link_hash_undefined;
868 h->root.u.undef.abfd = h->root.u.def.section->owner;
870 *size_change_ok = true;
871 *type_change_ok = true;
873 h->verinfo.vertree = NULL;
876 /* Handle the special case of a weak definition in a regular object
877 followed by a non-weak definition in a shared object. In this
878 case, we prefer the definition in the shared object unless it
879 comes from a DT_NEEDED entry of a shared object, in which case,
880 the DT_NEEDED entry may not be required at the run time. */
883 && h->root.type == bfd_link_hash_defweak
888 /* To make this work we have to frob the flags so that the rest
889 of the code does not think we are using the regular
891 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
892 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
893 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
894 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
895 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
896 | ELF_LINK_HASH_DEF_DYNAMIC);
898 /* If H is the target of an indirection, we want the caller to
899 use H rather than the indirect symbol. Otherwise if we are
900 defining a new indirect symbol we will wind up attaching it
901 to the entry we are overriding. */
905 /* Handle the special case of a non-weak definition in a shared
906 object followed by a weak definition in a regular object. In
907 this case we prefer to definition in the shared object. To make
908 this work we have to tell the caller to not treat the new symbol
912 && h->root.type != bfd_link_hash_defweak
921 /* This function is called to create an indirect symbol from the
922 default for the symbol with the default version if needed. The
923 symbol is described by H, NAME, SYM, SEC, VALUE, and OVERRIDE. We
924 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
925 indicates if it comes from a DT_NEEDED entry of a shared object. */
928 elf_add_default_symbol (abfd, info, h, name, sym, sec, value,
929 dynsym, override, dt_needed)
931 struct bfd_link_info *info;
932 struct elf_link_hash_entry *h;
934 Elf_Internal_Sym *sym;
941 boolean type_change_ok;
942 boolean size_change_ok;
944 struct elf_link_hash_entry *hi;
945 struct elf_backend_data *bed;
950 /* If this symbol has a version, and it is the default version, we
951 create an indirect symbol from the default name to the fully
952 decorated name. This will cause external references which do not
953 specify a version to be bound to this version of the symbol. */
954 p = strchr (name, ELF_VER_CHR);
955 if (p == NULL || p[1] != ELF_VER_CHR)
960 /* We are overridden by an old defition. We need to check if we
961 need to crreate the indirect symbol from the default name. */
962 hi = elf_link_hash_lookup (elf_hash_table (info), name, true,
964 BFD_ASSERT (hi != NULL);
967 while (hi->root.type == bfd_link_hash_indirect
968 || hi->root.type == bfd_link_hash_warning)
970 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
976 bed = get_elf_backend_data (abfd);
977 collect = bed->collect;
978 dynamic = (abfd->flags & DYNAMIC) != 0;
980 shortname = bfd_hash_allocate (&info->hash->table,
981 (size_t) (p - name + 1));
982 if (shortname == NULL)
984 strncpy (shortname, name, (size_t) (p - name));
985 shortname [p - name] = '\0';
987 /* We are going to create a new symbol. Merge it with any existing
988 symbol with this name. For the purposes of the merge, act as
989 though we were defining the symbol we just defined, although we
990 actually going to define an indirect symbol. */
991 type_change_ok = false;
992 size_change_ok = false;
993 if (! elf_merge_symbol (abfd, info, shortname, sym, sec, value,
994 &hi, &override, &type_change_ok,
995 &size_change_ok, dt_needed))
1000 if (! (_bfd_generic_link_add_one_symbol
1001 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1002 (bfd_vma) 0, name, false, collect,
1003 (struct bfd_link_hash_entry **) &hi)))
1008 /* In this case the symbol named SHORTNAME is overriding the
1009 indirect symbol we want to add. We were planning on making
1010 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1011 is the name without a version. NAME is the fully versioned
1012 name, and it is the default version.
1014 Overriding means that we already saw a definition for the
1015 symbol SHORTNAME in a regular object, and it is overriding
1016 the symbol defined in the dynamic object.
1018 When this happens, we actually want to change NAME, the
1019 symbol we just added, to refer to SHORTNAME. This will cause
1020 references to NAME in the shared object to become references
1021 to SHORTNAME in the regular object. This is what we expect
1022 when we override a function in a shared object: that the
1023 references in the shared object will be mapped to the
1024 definition in the regular object. */
1026 while (hi->root.type == bfd_link_hash_indirect
1027 || hi->root.type == bfd_link_hash_warning)
1028 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1030 h->root.type = bfd_link_hash_indirect;
1031 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1032 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1034 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1035 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1036 if (hi->elf_link_hash_flags
1037 & (ELF_LINK_HASH_REF_REGULAR
1038 | ELF_LINK_HASH_DEF_REGULAR))
1040 if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1045 /* Now set HI to H, so that the following code will set the
1046 other fields correctly. */
1050 /* If there is a duplicate definition somewhere, then HI may not
1051 point to an indirect symbol. We will have reported an error to
1052 the user in that case. */
1054 if (hi->root.type == bfd_link_hash_indirect)
1056 struct elf_link_hash_entry *ht;
1058 /* If the symbol became indirect, then we assume that we have
1059 not seen a definition before. */
1060 BFD_ASSERT ((hi->elf_link_hash_flags
1061 & (ELF_LINK_HASH_DEF_DYNAMIC
1062 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1064 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1065 (*bed->elf_backend_copy_indirect_symbol) (ht, hi);
1067 /* See if the new flags lead us to realize that the symbol must
1074 || ((hi->elf_link_hash_flags
1075 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1080 if ((hi->elf_link_hash_flags
1081 & ELF_LINK_HASH_REF_REGULAR) != 0)
1087 /* We also need to define an indirection from the nondefault version
1090 shortname = bfd_hash_allocate (&info->hash->table, strlen (name));
1091 if (shortname == NULL)
1093 strncpy (shortname, name, (size_t) (p - name));
1094 strcpy (shortname + (p - name), p + 1);
1096 /* Once again, merge with any existing symbol. */
1097 type_change_ok = false;
1098 size_change_ok = false;
1099 if (! elf_merge_symbol (abfd, info, shortname, sym, sec, value,
1100 &hi, &override, &type_change_ok,
1101 &size_change_ok, dt_needed))
1106 /* Here SHORTNAME is a versioned name, so we don't expect to see
1107 the type of override we do in the case above. */
1108 (*_bfd_error_handler)
1109 (_("%s: warning: unexpected redefinition of `%s'"),
1110 bfd_archive_filename (abfd), shortname);
1114 if (! (_bfd_generic_link_add_one_symbol
1115 (info, abfd, shortname, BSF_INDIRECT,
1116 bfd_ind_section_ptr, (bfd_vma) 0, name, false,
1117 collect, (struct bfd_link_hash_entry **) &hi)))
1120 /* If there is a duplicate definition somewhere, then HI may not
1121 point to an indirect symbol. We will have reported an error
1122 to the user in that case. */
1124 if (hi->root.type == bfd_link_hash_indirect)
1126 /* If the symbol became indirect, then we assume that we have
1127 not seen a definition before. */
1128 BFD_ASSERT ((hi->elf_link_hash_flags
1129 & (ELF_LINK_HASH_DEF_DYNAMIC
1130 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1132 (*bed->elf_backend_copy_indirect_symbol) (h, hi);
1134 /* See if the new flags lead us to realize that the symbol
1141 || ((hi->elf_link_hash_flags
1142 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1147 if ((hi->elf_link_hash_flags
1148 & ELF_LINK_HASH_REF_REGULAR) != 0)
1158 /* Add symbols from an ELF object file to the linker hash table. */
1161 elf_link_add_object_symbols (abfd, info)
1163 struct bfd_link_info *info;
1165 boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
1166 const Elf_Internal_Sym *,
1167 const char **, flagword *,
1168 asection **, bfd_vma *));
1169 boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *,
1170 asection *, const Elf_Internal_Rela *));
1172 Elf_Internal_Shdr *hdr;
1173 Elf_Internal_Shdr *shndx_hdr;
1174 bfd_size_type symcount;
1175 bfd_size_type extsymcount;
1176 bfd_size_type extsymoff;
1177 Elf_External_Sym *buf = NULL;
1178 Elf_External_Sym_Shndx *shndx_buf = NULL;
1179 Elf_External_Sym_Shndx *shndx;
1180 struct elf_link_hash_entry **sym_hash;
1182 Elf_External_Versym *extversym = NULL;
1183 Elf_External_Versym *ever;
1184 Elf_External_Dyn *dynbuf = NULL;
1185 struct elf_link_hash_entry *weaks;
1186 Elf_External_Sym *esym;
1187 Elf_External_Sym *esymend;
1188 struct elf_backend_data *bed;
1190 struct elf_link_hash_table * hash_table;
1194 hash_table = elf_hash_table (info);
1196 bed = get_elf_backend_data (abfd);
1197 add_symbol_hook = bed->elf_add_symbol_hook;
1198 collect = bed->collect;
1200 if ((abfd->flags & DYNAMIC) == 0)
1206 /* You can't use -r against a dynamic object. Also, there's no
1207 hope of using a dynamic object which does not exactly match
1208 the format of the output file. */
1209 if (info->relocateable || info->hash->creator != abfd->xvec)
1211 bfd_set_error (bfd_error_invalid_operation);
1216 /* As a GNU extension, any input sections which are named
1217 .gnu.warning.SYMBOL are treated as warning symbols for the given
1218 symbol. This differs from .gnu.warning sections, which generate
1219 warnings when they are included in an output file. */
1224 for (s = abfd->sections; s != NULL; s = s->next)
1228 name = bfd_get_section_name (abfd, s);
1229 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1234 name += sizeof ".gnu.warning." - 1;
1236 /* If this is a shared object, then look up the symbol
1237 in the hash table. If it is there, and it is already
1238 been defined, then we will not be using the entry
1239 from this shared object, so we don't need to warn.
1240 FIXME: If we see the definition in a regular object
1241 later on, we will warn, but we shouldn't. The only
1242 fix is to keep track of what warnings we are supposed
1243 to emit, and then handle them all at the end of the
1245 if (dynamic && abfd->xvec == info->hash->creator)
1247 struct elf_link_hash_entry *h;
1249 h = elf_link_hash_lookup (hash_table, name,
1250 false, false, true);
1252 /* FIXME: What about bfd_link_hash_common? */
1254 && (h->root.type == bfd_link_hash_defined
1255 || h->root.type == bfd_link_hash_defweak))
1257 /* We don't want to issue this warning. Clobber
1258 the section size so that the warning does not
1259 get copied into the output file. */
1265 sz = bfd_section_size (abfd, s);
1266 msg = (char *) bfd_alloc (abfd, sz + 1);
1270 if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
1275 if (! (_bfd_generic_link_add_one_symbol
1276 (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
1277 false, collect, (struct bfd_link_hash_entry **) NULL)))
1280 if (! info->relocateable)
1282 /* Clobber the section size so that the warning does
1283 not get copied into the output file. */
1290 /* If this is a dynamic object, we always link against the .dynsym
1291 symbol table, not the .symtab symbol table. The dynamic linker
1292 will only see the .dynsym symbol table, so there is no reason to
1293 look at .symtab for a dynamic object. */
1295 if (! dynamic || elf_dynsymtab (abfd) == 0)
1297 hdr = &elf_tdata (abfd)->symtab_hdr;
1298 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1302 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1308 /* Read in any version definitions. */
1310 if (! _bfd_elf_slurp_version_tables (abfd))
1313 /* Read in the symbol versions, but don't bother to convert them
1314 to internal format. */
1315 if (elf_dynversym (abfd) != 0)
1317 Elf_Internal_Shdr *versymhdr;
1319 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
1320 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
1321 if (extversym == NULL)
1323 amt = versymhdr->sh_size;
1324 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
1325 || bfd_bread ((PTR) extversym, amt, abfd) != amt)
1330 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1332 /* The sh_info field of the symtab header tells us where the
1333 external symbols start. We don't care about the local symbols at
1335 if (elf_bad_symtab (abfd))
1337 extsymcount = symcount;
1342 extsymcount = symcount - hdr->sh_info;
1343 extsymoff = hdr->sh_info;
1346 amt = extsymcount * sizeof (Elf_External_Sym);
1347 buf = (Elf_External_Sym *) bfd_malloc (amt);
1348 if (buf == NULL && extsymcount != 0)
1351 if (shndx_hdr != NULL && shndx_hdr->sh_size != 0)
1353 amt = extsymcount * sizeof (Elf_External_Sym_Shndx);
1354 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1355 if (shndx_buf == NULL && extsymcount != 0)
1359 /* We store a pointer to the hash table entry for each external
1361 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
1362 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
1363 if (sym_hash == NULL)
1365 elf_sym_hashes (abfd) = sym_hash;
1371 /* If we are creating a shared library, create all the dynamic
1372 sections immediately. We need to attach them to something,
1373 so we attach them to this BFD, provided it is the right
1374 format. FIXME: If there are no input BFD's of the same
1375 format as the output, we can't make a shared library. */
1377 && is_elf_hash_table (info)
1378 && ! hash_table->dynamic_sections_created
1379 && abfd->xvec == info->hash->creator)
1381 if (! elf_link_create_dynamic_sections (abfd, info))
1385 else if (! is_elf_hash_table (info))
1392 bfd_size_type oldsize;
1393 bfd_size_type strindex;
1395 /* Find the name to use in a DT_NEEDED entry that refers to this
1396 object. If the object has a DT_SONAME entry, we use it.
1397 Otherwise, if the generic linker stuck something in
1398 elf_dt_name, we use that. Otherwise, we just use the file
1399 name. If the generic linker put a null string into
1400 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1401 there is a DT_SONAME entry. */
1403 name = bfd_get_filename (abfd);
1404 if (elf_dt_name (abfd) != NULL)
1406 name = elf_dt_name (abfd);
1409 if (elf_dt_soname (abfd) != NULL)
1415 s = bfd_get_section_by_name (abfd, ".dynamic");
1418 Elf_External_Dyn *extdyn;
1419 Elf_External_Dyn *extdynend;
1421 unsigned long shlink;
1425 dynbuf = (Elf_External_Dyn *) bfd_malloc (s->_raw_size);
1429 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
1430 (file_ptr) 0, s->_raw_size))
1433 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1436 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1439 /* The shared libraries distributed with hpux11 have a bogus
1440 sh_link field for the ".dynamic" section. This code detects
1441 when SHLINK refers to a section that is not a string table
1442 and tries to find the string table for the ".dynsym" section
1444 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[shlink];
1445 if (shdr->sh_type != SHT_STRTAB)
1447 asection *ds = bfd_get_section_by_name (abfd, ".dynsym");
1448 int elfdsec = _bfd_elf_section_from_bfd_section (abfd, ds);
1451 shlink = elf_elfsections (abfd)[elfdsec]->sh_link;
1456 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
1459 for (; extdyn < extdynend; extdyn++)
1461 Elf_Internal_Dyn dyn;
1463 elf_swap_dyn_in (abfd, extdyn, &dyn);
1464 if (dyn.d_tag == DT_SONAME)
1466 unsigned int tagv = dyn.d_un.d_val;
1467 name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1471 if (dyn.d_tag == DT_NEEDED)
1473 struct bfd_link_needed_list *n, **pn;
1475 unsigned int tagv = dyn.d_un.d_val;
1477 amt = sizeof (struct bfd_link_needed_list);
1478 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1479 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1480 if (n == NULL || fnm == NULL)
1482 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1489 for (pn = & hash_table->needed;
1495 if (dyn.d_tag == DT_RUNPATH)
1497 struct bfd_link_needed_list *n, **pn;
1499 unsigned int tagv = dyn.d_un.d_val;
1501 /* When we see DT_RPATH before DT_RUNPATH, we have
1502 to clear runpath. Do _NOT_ bfd_release, as that
1503 frees all more recently bfd_alloc'd blocks as
1505 if (rpath && hash_table->runpath)
1506 hash_table->runpath = NULL;
1508 amt = sizeof (struct bfd_link_needed_list);
1509 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1510 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1511 if (n == NULL || fnm == NULL)
1513 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1520 for (pn = & hash_table->runpath;
1528 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1529 if (!runpath && dyn.d_tag == DT_RPATH)
1531 struct bfd_link_needed_list *n, **pn;
1533 unsigned int tagv = dyn.d_un.d_val;
1535 amt = sizeof (struct bfd_link_needed_list);
1536 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1537 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1538 if (n == NULL || fnm == NULL)
1540 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1547 for (pn = & hash_table->runpath;
1560 /* We do not want to include any of the sections in a dynamic
1561 object in the output file. We hack by simply clobbering the
1562 list of sections in the BFD. This could be handled more
1563 cleanly by, say, a new section flag; the existing
1564 SEC_NEVER_LOAD flag is not the one we want, because that one
1565 still implies that the section takes up space in the output
1567 bfd_section_list_clear (abfd);
1569 /* If this is the first dynamic object found in the link, create
1570 the special sections required for dynamic linking. */
1571 if (! hash_table->dynamic_sections_created)
1572 if (! elf_link_create_dynamic_sections (abfd, info))
1577 /* Add a DT_NEEDED entry for this dynamic object. */
1578 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
1579 strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, false);
1580 if (strindex == (bfd_size_type) -1)
1583 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
1586 Elf_External_Dyn *dyncon, *dynconend;
1588 /* The hash table size did not change, which means that
1589 the dynamic object name was already entered. If we
1590 have already included this dynamic object in the
1591 link, just ignore it. There is no reason to include
1592 a particular dynamic object more than once. */
1593 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
1594 BFD_ASSERT (sdyn != NULL);
1596 dyncon = (Elf_External_Dyn *) sdyn->contents;
1597 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1599 for (; dyncon < dynconend; dyncon++)
1601 Elf_Internal_Dyn dyn;
1603 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
1604 if (dyn.d_tag == DT_NEEDED
1605 && dyn.d_un.d_val == strindex)
1609 if (extversym != NULL)
1611 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
1617 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
1621 /* Save the SONAME, if there is one, because sometimes the
1622 linker emulation code will need to know it. */
1624 name = basename (bfd_get_filename (abfd));
1625 elf_dt_name (abfd) = name;
1628 pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
1629 amt = extsymcount * sizeof (Elf_External_Sym);
1630 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1631 || bfd_bread ((PTR) buf, amt, abfd) != amt)
1634 if (shndx_hdr != NULL && shndx_hdr->sh_size != 0)
1636 amt = extsymcount * sizeof (Elf_External_Sym_Shndx);
1637 pos = shndx_hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym_Shndx);
1638 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1639 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
1645 ever = extversym != NULL ? extversym + extsymoff : NULL;
1646 esymend = buf + extsymcount;
1647 for (esym = buf, shndx = shndx_buf;
1649 esym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL),
1650 shndx = (shndx != NULL ? shndx + 1 : NULL))
1652 Elf_Internal_Sym sym;
1658 struct elf_link_hash_entry *h;
1660 boolean size_change_ok, type_change_ok;
1661 boolean new_weakdef;
1662 unsigned int old_alignment;
1667 elf_swap_symbol_in (abfd, (const PTR) esym, (const PTR) shndx, &sym);
1669 flags = BSF_NO_FLAGS;
1671 value = sym.st_value;
1674 bind = ELF_ST_BIND (sym.st_info);
1675 if (bind == STB_LOCAL)
1677 /* This should be impossible, since ELF requires that all
1678 global symbols follow all local symbols, and that sh_info
1679 point to the first global symbol. Unfortunatealy, Irix 5
1683 else if (bind == STB_GLOBAL)
1685 if (sym.st_shndx != SHN_UNDEF
1686 && sym.st_shndx != SHN_COMMON)
1689 else if (bind == STB_WEAK)
1693 /* Leave it up to the processor backend. */
1696 if (sym.st_shndx == SHN_UNDEF)
1697 sec = bfd_und_section_ptr;
1698 else if (sym.st_shndx < SHN_LORESERVE || sym.st_shndx > SHN_HIRESERVE)
1700 sec = section_from_elf_index (abfd, sym.st_shndx);
1702 sec = bfd_abs_section_ptr;
1703 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1706 else if (sym.st_shndx == SHN_ABS)
1707 sec = bfd_abs_section_ptr;
1708 else if (sym.st_shndx == SHN_COMMON)
1710 sec = bfd_com_section_ptr;
1711 /* What ELF calls the size we call the value. What ELF
1712 calls the value we call the alignment. */
1713 value = sym.st_size;
1717 /* Leave it up to the processor backend. */
1720 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
1721 if (name == (const char *) NULL)
1724 if (sym.st_shndx == SHN_COMMON && ELF_ST_TYPE (sym.st_info) == STT_TLS)
1726 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
1730 tcomm = bfd_make_section (abfd, ".tcommon");
1732 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
1734 | SEC_LINKER_CREATED
1735 | SEC_THREAD_LOCAL)))
1740 else if (add_symbol_hook)
1742 if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec,
1746 /* The hook function sets the name to NULL if this symbol
1747 should be skipped for some reason. */
1748 if (name == (const char *) NULL)
1752 /* Sanity check that all possibilities were handled. */
1753 if (sec == (asection *) NULL)
1755 bfd_set_error (bfd_error_bad_value);
1759 if (bfd_is_und_section (sec)
1760 || bfd_is_com_section (sec))
1765 size_change_ok = false;
1766 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
1768 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1770 Elf_Internal_Versym iver;
1771 unsigned int vernum = 0;
1775 _bfd_elf_swap_versym_in (abfd, ever, &iver);
1776 vernum = iver.vs_vers & VERSYM_VERSION;
1778 /* If this is a hidden symbol, or if it is not version
1779 1, we append the version name to the symbol name.
1780 However, we do not modify a non-hidden absolute
1781 symbol, because it might be the version symbol
1782 itself. FIXME: What if it isn't? */
1783 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
1784 || (vernum > 1 && ! bfd_is_abs_section (sec)))
1787 unsigned int namelen;
1788 bfd_size_type newlen;
1791 if (sym.st_shndx != SHN_UNDEF)
1793 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
1795 (*_bfd_error_handler)
1796 (_("%s: %s: invalid version %u (max %d)"),
1797 bfd_archive_filename (abfd), name, vernum,
1798 elf_tdata (abfd)->dynverdef_hdr.sh_info);
1799 bfd_set_error (bfd_error_bad_value);
1802 else if (vernum > 1)
1804 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1810 /* We cannot simply test for the number of
1811 entries in the VERNEED section since the
1812 numbers for the needed versions do not start
1814 Elf_Internal_Verneed *t;
1817 for (t = elf_tdata (abfd)->verref;
1821 Elf_Internal_Vernaux *a;
1823 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1825 if (a->vna_other == vernum)
1827 verstr = a->vna_nodename;
1836 (*_bfd_error_handler)
1837 (_("%s: %s: invalid needed version %d"),
1838 bfd_archive_filename (abfd), name, vernum);
1839 bfd_set_error (bfd_error_bad_value);
1844 namelen = strlen (name);
1845 newlen = namelen + strlen (verstr) + 2;
1846 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
1849 newname = (char *) bfd_alloc (abfd, newlen);
1850 if (newname == NULL)
1852 strcpy (newname, name);
1853 p = newname + namelen;
1855 /* If this is a defined non-hidden version symbol,
1856 we add another @ to the name. This indicates the
1857 default version of the symbol. */
1858 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1859 && sym.st_shndx != SHN_UNDEF)
1867 if (! elf_merge_symbol (abfd, info, name, &sym, &sec, &value,
1868 sym_hash, &override, &type_change_ok,
1869 &size_change_ok, dt_needed))
1876 while (h->root.type == bfd_link_hash_indirect
1877 || h->root.type == bfd_link_hash_warning)
1878 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1880 /* Remember the old alignment if this is a common symbol, so
1881 that we don't reduce the alignment later on. We can't
1882 check later, because _bfd_generic_link_add_one_symbol
1883 will set a default for the alignment which we want to
1885 if (h->root.type == bfd_link_hash_common)
1886 old_alignment = h->root.u.c.p->alignment_power;
1888 if (elf_tdata (abfd)->verdef != NULL
1892 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1895 if (! (_bfd_generic_link_add_one_symbol
1896 (info, abfd, name, flags, sec, value, (const char *) NULL,
1897 false, collect, (struct bfd_link_hash_entry **) sym_hash)))
1901 while (h->root.type == bfd_link_hash_indirect
1902 || h->root.type == bfd_link_hash_warning)
1903 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1906 new_weakdef = false;
1909 && (flags & BSF_WEAK) != 0
1910 && ELF_ST_TYPE (sym.st_info) != STT_FUNC
1911 && info->hash->creator->flavour == bfd_target_elf_flavour
1912 && h->weakdef == NULL)
1914 /* Keep a list of all weak defined non function symbols from
1915 a dynamic object, using the weakdef field. Later in this
1916 function we will set the weakdef field to the correct
1917 value. We only put non-function symbols from dynamic
1918 objects on this list, because that happens to be the only
1919 time we need to know the normal symbol corresponding to a
1920 weak symbol, and the information is time consuming to
1921 figure out. If the weakdef field is not already NULL,
1922 then this symbol was already defined by some previous
1923 dynamic object, and we will be using that previous
1924 definition anyhow. */
1931 /* Set the alignment of a common symbol. */
1932 if (sym.st_shndx == SHN_COMMON
1933 && h->root.type == bfd_link_hash_common)
1937 align = bfd_log2 (sym.st_value);
1938 if (align > old_alignment
1939 /* Permit an alignment power of zero if an alignment of one
1940 is specified and no other alignments have been specified. */
1941 || (sym.st_value == 1 && old_alignment == 0))
1942 h->root.u.c.p->alignment_power = align;
1945 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1951 /* Remember the symbol size and type. */
1952 if (sym.st_size != 0
1953 && (definition || h->size == 0))
1955 if (h->size != 0 && h->size != sym.st_size && ! size_change_ok)
1956 (*_bfd_error_handler)
1957 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1958 name, (unsigned long) h->size, (unsigned long) sym.st_size,
1959 bfd_archive_filename (abfd));
1961 h->size = sym.st_size;
1964 /* If this is a common symbol, then we always want H->SIZE
1965 to be the size of the common symbol. The code just above
1966 won't fix the size if a common symbol becomes larger. We
1967 don't warn about a size change here, because that is
1968 covered by --warn-common. */
1969 if (h->root.type == bfd_link_hash_common)
1970 h->size = h->root.u.c.size;
1972 if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE
1973 && (definition || h->type == STT_NOTYPE))
1975 if (h->type != STT_NOTYPE
1976 && h->type != ELF_ST_TYPE (sym.st_info)
1977 && ! type_change_ok)
1978 (*_bfd_error_handler)
1979 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1980 name, h->type, ELF_ST_TYPE (sym.st_info),
1981 bfd_archive_filename (abfd));
1983 h->type = ELF_ST_TYPE (sym.st_info);
1986 /* If st_other has a processor-specific meaning, specific code
1987 might be needed here. */
1988 if (sym.st_other != 0)
1990 /* Combine visibilities, using the most constraining one. */
1991 unsigned char hvis = ELF_ST_VISIBILITY (h->other);
1992 unsigned char symvis = ELF_ST_VISIBILITY (sym.st_other);
1994 if (symvis && (hvis > symvis || hvis == 0))
1995 h->other = sym.st_other;
1997 /* If neither has visibility, use the st_other of the
1998 definition. This is an arbitrary choice, since the
1999 other bits have no general meaning. */
2000 if (!symvis && !hvis
2001 && (definition || h->other == 0))
2002 h->other = sym.st_other;
2005 /* Set a flag in the hash table entry indicating the type of
2006 reference or definition we just found. Keep a count of
2007 the number of dynamic symbols we find. A dynamic symbol
2008 is one which is referenced or defined by both a regular
2009 object and a shared object. */
2010 old_flags = h->elf_link_hash_flags;
2016 new_flag = ELF_LINK_HASH_REF_REGULAR;
2017 if (bind != STB_WEAK)
2018 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
2021 new_flag = ELF_LINK_HASH_DEF_REGULAR;
2023 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2024 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
2030 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
2032 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
2033 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
2034 | ELF_LINK_HASH_REF_REGULAR)) != 0
2035 || (h->weakdef != NULL
2037 && h->weakdef->dynindx != -1))
2041 h->elf_link_hash_flags |= new_flag;
2043 /* Check to see if we need to add an indirect symbol for
2044 the default name. */
2045 if (definition || h->root.type == bfd_link_hash_common)
2046 if (! elf_add_default_symbol (abfd, info, h, name, &sym,
2047 &sec, &value, &dynsym,
2048 override, dt_needed))
2051 if (dynsym && h->dynindx == -1)
2053 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2055 if (h->weakdef != NULL
2057 && h->weakdef->dynindx == -1)
2059 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2063 else if (dynsym && h->dynindx != -1)
2064 /* If the symbol already has a dynamic index, but
2065 visibility says it should not be visible, turn it into
2067 switch (ELF_ST_VISIBILITY (h->other))
2071 (*bed->elf_backend_hide_symbol) (info, h, true);
2075 if (dt_needed && definition
2076 && (h->elf_link_hash_flags
2077 & ELF_LINK_HASH_REF_REGULAR) != 0)
2079 bfd_size_type oldsize;
2080 bfd_size_type strindex;
2082 if (! is_elf_hash_table (info))
2085 /* The symbol from a DT_NEEDED object is referenced from
2086 the regular object to create a dynamic executable. We
2087 have to make sure there is a DT_NEEDED entry for it. */
2090 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2091 strindex = _bfd_elf_strtab_add (hash_table->dynstr,
2092 elf_dt_soname (abfd), false);
2093 if (strindex == (bfd_size_type) -1)
2096 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2099 Elf_External_Dyn *dyncon, *dynconend;
2101 sdyn = bfd_get_section_by_name (hash_table->dynobj,
2103 BFD_ASSERT (sdyn != NULL);
2105 dyncon = (Elf_External_Dyn *) sdyn->contents;
2106 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2108 for (; dyncon < dynconend; dyncon++)
2110 Elf_Internal_Dyn dyn;
2112 elf_swap_dyn_in (hash_table->dynobj,
2114 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
2115 dyn.d_un.d_val != strindex);
2119 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
2125 /* Now set the weakdefs field correctly for all the weak defined
2126 symbols we found. The only way to do this is to search all the
2127 symbols. Since we only need the information for non functions in
2128 dynamic objects, that's the only time we actually put anything on
2129 the list WEAKS. We need this information so that if a regular
2130 object refers to a symbol defined weakly in a dynamic object, the
2131 real symbol in the dynamic object is also put in the dynamic
2132 symbols; we also must arrange for both symbols to point to the
2133 same memory location. We could handle the general case of symbol
2134 aliasing, but a general symbol alias can only be generated in
2135 assembler code, handling it correctly would be very time
2136 consuming, and other ELF linkers don't handle general aliasing
2138 while (weaks != NULL)
2140 struct elf_link_hash_entry *hlook;
2143 struct elf_link_hash_entry **hpp;
2144 struct elf_link_hash_entry **hppend;
2147 weaks = hlook->weakdef;
2148 hlook->weakdef = NULL;
2150 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
2151 || hlook->root.type == bfd_link_hash_defweak
2152 || hlook->root.type == bfd_link_hash_common
2153 || hlook->root.type == bfd_link_hash_indirect);
2154 slook = hlook->root.u.def.section;
2155 vlook = hlook->root.u.def.value;
2157 hpp = elf_sym_hashes (abfd);
2158 hppend = hpp + extsymcount;
2159 for (; hpp < hppend; hpp++)
2161 struct elf_link_hash_entry *h;
2164 if (h != NULL && h != hlook
2165 && h->root.type == bfd_link_hash_defined
2166 && h->root.u.def.section == slook
2167 && h->root.u.def.value == vlook)
2171 /* If the weak definition is in the list of dynamic
2172 symbols, make sure the real definition is put there
2174 if (hlook->dynindx != -1
2175 && h->dynindx == -1)
2177 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2181 /* If the real definition is in the list of dynamic
2182 symbols, make sure the weak definition is put there
2183 as well. If we don't do this, then the dynamic
2184 loader might not merge the entries for the real
2185 definition and the weak definition. */
2186 if (h->dynindx != -1
2187 && hlook->dynindx == -1)
2189 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
2204 if (extversym != NULL)
2210 /* If this object is the same format as the output object, and it is
2211 not a shared library, then let the backend look through the
2214 This is required to build global offset table entries and to
2215 arrange for dynamic relocs. It is not required for the
2216 particular common case of linking non PIC code, even when linking
2217 against shared libraries, but unfortunately there is no way of
2218 knowing whether an object file has been compiled PIC or not.
2219 Looking through the relocs is not particularly time consuming.
2220 The problem is that we must either (1) keep the relocs in memory,
2221 which causes the linker to require additional runtime memory or
2222 (2) read the relocs twice from the input file, which wastes time.
2223 This would be a good case for using mmap.
2225 I have no idea how to handle linking PIC code into a file of a
2226 different format. It probably can't be done. */
2227 check_relocs = get_elf_backend_data (abfd)->check_relocs;
2229 && abfd->xvec == info->hash->creator
2230 && check_relocs != NULL)
2234 for (o = abfd->sections; o != NULL; o = o->next)
2236 Elf_Internal_Rela *internal_relocs;
2239 if ((o->flags & SEC_RELOC) == 0
2240 || o->reloc_count == 0
2241 || ((info->strip == strip_all || info->strip == strip_debugger)
2242 && (o->flags & SEC_DEBUGGING) != 0)
2243 || bfd_is_abs_section (o->output_section))
2246 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
2247 (abfd, o, (PTR) NULL,
2248 (Elf_Internal_Rela *) NULL,
2249 info->keep_memory));
2250 if (internal_relocs == NULL)
2253 ok = (*check_relocs) (abfd, info, o, internal_relocs);
2255 if (! info->keep_memory)
2256 free (internal_relocs);
2263 /* If this is a non-traditional, non-relocateable link, try to
2264 optimize the handling of the .stab/.stabstr sections. */
2266 && ! info->relocateable
2267 && ! info->traditional_format
2268 && info->hash->creator->flavour == bfd_target_elf_flavour
2269 && is_elf_hash_table (info)
2270 && (info->strip != strip_all && info->strip != strip_debugger))
2272 asection *stab, *stabstr;
2274 stab = bfd_get_section_by_name (abfd, ".stab");
2276 && (stab->flags & SEC_MERGE) == 0
2277 && !bfd_is_abs_section (stab->output_section))
2279 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
2281 if (stabstr != NULL)
2283 struct bfd_elf_section_data *secdata;
2285 secdata = elf_section_data (stab);
2286 if (! _bfd_link_section_stabs (abfd,
2287 & hash_table->stab_info,
2289 &secdata->sec_info))
2291 if (secdata->sec_info)
2292 secdata->sec_info_type = ELF_INFO_TYPE_STABS;
2297 if (! info->relocateable && ! dynamic
2298 && is_elf_hash_table (info))
2302 for (s = abfd->sections; s != NULL; s = s->next)
2303 if ((s->flags & SEC_MERGE) != 0
2304 && !bfd_is_abs_section (s->output_section))
2306 struct bfd_elf_section_data *secdata;
2308 secdata = elf_section_data (s);
2309 if (! _bfd_merge_section (abfd,
2310 & hash_table->merge_info,
2311 s, &secdata->sec_info))
2313 else if (secdata->sec_info)
2314 secdata->sec_info_type = ELF_INFO_TYPE_MERGE;
2325 if (extversym != NULL)
2330 /* Create some sections which will be filled in with dynamic linking
2331 information. ABFD is an input file which requires dynamic sections
2332 to be created. The dynamic sections take up virtual memory space
2333 when the final executable is run, so we need to create them before
2334 addresses are assigned to the output sections. We work out the
2335 actual contents and size of these sections later. */
2338 elf_link_create_dynamic_sections (abfd, info)
2340 struct bfd_link_info *info;
2343 register asection *s;
2344 struct elf_link_hash_entry *h;
2345 struct elf_backend_data *bed;
2347 if (! is_elf_hash_table (info))
2350 if (elf_hash_table (info)->dynamic_sections_created)
2353 /* Make sure that all dynamic sections use the same input BFD. */
2354 if (elf_hash_table (info)->dynobj == NULL)
2355 elf_hash_table (info)->dynobj = abfd;
2357 abfd = elf_hash_table (info)->dynobj;
2359 /* Note that we set the SEC_IN_MEMORY flag for all of these
2361 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2362 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2364 /* A dynamically linked executable has a .interp section, but a
2365 shared library does not. */
2368 s = bfd_make_section (abfd, ".interp");
2370 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2374 if (! info->traditional_format
2375 && info->hash->creator->flavour == bfd_target_elf_flavour)
2377 s = bfd_make_section (abfd, ".eh_frame_hdr");
2379 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2380 || ! bfd_set_section_alignment (abfd, s, 2))
2384 /* Create sections to hold version informations. These are removed
2385 if they are not needed. */
2386 s = bfd_make_section (abfd, ".gnu.version_d");
2388 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2389 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2392 s = bfd_make_section (abfd, ".gnu.version");
2394 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2395 || ! bfd_set_section_alignment (abfd, s, 1))
2398 s = bfd_make_section (abfd, ".gnu.version_r");
2400 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2401 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2404 s = bfd_make_section (abfd, ".dynsym");
2406 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2407 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2410 s = bfd_make_section (abfd, ".dynstr");
2412 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2415 /* Create a strtab to hold the dynamic symbol names. */
2416 if (elf_hash_table (info)->dynstr == NULL)
2418 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
2419 if (elf_hash_table (info)->dynstr == NULL)
2423 s = bfd_make_section (abfd, ".dynamic");
2425 || ! bfd_set_section_flags (abfd, s, flags)
2426 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2429 /* The special symbol _DYNAMIC is always set to the start of the
2430 .dynamic section. This call occurs before we have processed the
2431 symbols for any dynamic object, so we don't have to worry about
2432 overriding a dynamic definition. We could set _DYNAMIC in a
2433 linker script, but we only want to define it if we are, in fact,
2434 creating a .dynamic section. We don't want to define it if there
2435 is no .dynamic section, since on some ELF platforms the start up
2436 code examines it to decide how to initialize the process. */
2438 if (! (_bfd_generic_link_add_one_symbol
2439 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
2440 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
2441 (struct bfd_link_hash_entry **) &h)))
2443 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2444 h->type = STT_OBJECT;
2447 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2450 bed = get_elf_backend_data (abfd);
2452 s = bfd_make_section (abfd, ".hash");
2454 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2455 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2457 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
2459 /* Let the backend create the rest of the sections. This lets the
2460 backend set the right flags. The backend will normally create
2461 the .got and .plt sections. */
2462 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
2465 elf_hash_table (info)->dynamic_sections_created = true;
2470 /* Add an entry to the .dynamic table. */
2473 elf_add_dynamic_entry (info, tag, val)
2474 struct bfd_link_info *info;
2478 Elf_Internal_Dyn dyn;
2481 bfd_size_type newsize;
2482 bfd_byte *newcontents;
2484 if (! is_elf_hash_table (info))
2487 dynobj = elf_hash_table (info)->dynobj;
2489 s = bfd_get_section_by_name (dynobj, ".dynamic");
2490 BFD_ASSERT (s != NULL);
2492 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
2493 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
2494 if (newcontents == NULL)
2498 dyn.d_un.d_val = val;
2499 elf_swap_dyn_out (dynobj, &dyn,
2500 (Elf_External_Dyn *) (newcontents + s->_raw_size));
2502 s->_raw_size = newsize;
2503 s->contents = newcontents;
2508 /* Record a new local dynamic symbol. */
2511 elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
2512 struct bfd_link_info *info;
2516 struct elf_link_local_dynamic_entry *entry;
2517 struct elf_link_hash_table *eht;
2518 struct elf_strtab_hash *dynstr;
2519 Elf_External_Sym esym;
2520 Elf_External_Sym_Shndx eshndx;
2521 Elf_External_Sym_Shndx *shndx;
2522 unsigned long dynstr_index;
2527 if (! is_elf_hash_table (info))
2530 /* See if the entry exists already. */
2531 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2532 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
2535 entry = (struct elf_link_local_dynamic_entry *)
2536 bfd_alloc (input_bfd, (bfd_size_type) sizeof (*entry));
2540 /* Go find the symbol, so that we can find it's name. */
2541 amt = sizeof (Elf_External_Sym);
2542 pos = elf_tdata (input_bfd)->symtab_hdr.sh_offset + input_indx * amt;
2543 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
2544 || bfd_bread ((PTR) &esym, amt, input_bfd) != amt)
2547 if (elf_tdata (input_bfd)->symtab_shndx_hdr.sh_size != 0)
2549 amt = sizeof (Elf_External_Sym_Shndx);
2550 pos = elf_tdata (input_bfd)->symtab_shndx_hdr.sh_offset;
2551 pos += input_indx * amt;
2553 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
2554 || bfd_bread ((PTR) shndx, amt, input_bfd) != amt)
2557 elf_swap_symbol_in (input_bfd, (const PTR) &esym, (const PTR) shndx,
2560 name = (bfd_elf_string_from_elf_section
2561 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
2562 entry->isym.st_name));
2564 dynstr = elf_hash_table (info)->dynstr;
2567 /* Create a strtab to hold the dynamic symbol names. */
2568 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
2573 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
2574 if (dynstr_index == (unsigned long) -1)
2576 entry->isym.st_name = dynstr_index;
2578 eht = elf_hash_table (info);
2580 entry->next = eht->dynlocal;
2581 eht->dynlocal = entry;
2582 entry->input_bfd = input_bfd;
2583 entry->input_indx = input_indx;
2586 /* Whatever binding the symbol had before, it's now local. */
2588 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
2590 /* The dynindx will be set at the end of size_dynamic_sections. */
2595 /* Read and swap the relocs from the section indicated by SHDR. This
2596 may be either a REL or a RELA section. The relocations are
2597 translated into RELA relocations and stored in INTERNAL_RELOCS,
2598 which should have already been allocated to contain enough space.
2599 The EXTERNAL_RELOCS are a buffer where the external form of the
2600 relocations should be stored.
2602 Returns false if something goes wrong. */
2605 elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
2608 Elf_Internal_Shdr *shdr;
2609 PTR external_relocs;
2610 Elf_Internal_Rela *internal_relocs;
2612 struct elf_backend_data *bed;
2615 /* If there aren't any relocations, that's OK. */
2619 /* Position ourselves at the start of the section. */
2620 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2623 /* Read the relocations. */
2624 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2627 bed = get_elf_backend_data (abfd);
2629 /* Convert the external relocations to the internal format. */
2630 if (shdr->sh_entsize == sizeof (Elf_External_Rel))
2632 Elf_External_Rel *erel;
2633 Elf_External_Rel *erelend;
2634 Elf_Internal_Rela *irela;
2635 Elf_Internal_Rel *irel;
2637 erel = (Elf_External_Rel *) external_relocs;
2638 erelend = erel + NUM_SHDR_ENTRIES (shdr);
2639 irela = internal_relocs;
2640 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
2641 irel = bfd_alloc (abfd, amt);
2642 for (; erel < erelend; erel++, irela += bed->s->int_rels_per_ext_rel)
2646 if (bed->s->swap_reloc_in)
2647 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
2649 elf_swap_reloc_in (abfd, erel, irel);
2651 for (i = 0; i < bed->s->int_rels_per_ext_rel; ++i)
2653 irela[i].r_offset = irel[i].r_offset;
2654 irela[i].r_info = irel[i].r_info;
2655 irela[i].r_addend = 0;
2661 Elf_External_Rela *erela;
2662 Elf_External_Rela *erelaend;
2663 Elf_Internal_Rela *irela;
2665 BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela));
2667 erela = (Elf_External_Rela *) external_relocs;
2668 erelaend = erela + NUM_SHDR_ENTRIES (shdr);
2669 irela = internal_relocs;
2670 for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel)
2672 if (bed->s->swap_reloca_in)
2673 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
2675 elf_swap_reloca_in (abfd, erela, irela);
2682 /* Read and swap the relocs for a section O. They may have been
2683 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2684 not NULL, they are used as buffers to read into. They are known to
2685 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2686 the return value is allocated using either malloc or bfd_alloc,
2687 according to the KEEP_MEMORY argument. If O has two relocation
2688 sections (both REL and RELA relocations), then the REL_HDR
2689 relocations will appear first in INTERNAL_RELOCS, followed by the
2690 REL_HDR2 relocations. */
2693 NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
2697 PTR external_relocs;
2698 Elf_Internal_Rela *internal_relocs;
2699 boolean keep_memory;
2701 Elf_Internal_Shdr *rel_hdr;
2703 Elf_Internal_Rela *alloc2 = NULL;
2704 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2706 if (elf_section_data (o)->relocs != NULL)
2707 return elf_section_data (o)->relocs;
2709 if (o->reloc_count == 0)
2712 rel_hdr = &elf_section_data (o)->rel_hdr;
2714 if (internal_relocs == NULL)
2718 size = o->reloc_count;
2719 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2721 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2723 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2724 if (internal_relocs == NULL)
2728 if (external_relocs == NULL)
2730 bfd_size_type size = rel_hdr->sh_size;
2732 if (elf_section_data (o)->rel_hdr2)
2733 size += elf_section_data (o)->rel_hdr2->sh_size;
2734 alloc1 = (PTR) bfd_malloc (size);
2737 external_relocs = alloc1;
2740 if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
2744 if (!elf_link_read_relocs_from_section
2746 elf_section_data (o)->rel_hdr2,
2747 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2748 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2749 * bed->s->int_rels_per_ext_rel)))
2752 /* Cache the results for next time, if we can. */
2754 elf_section_data (o)->relocs = internal_relocs;
2759 /* Don't free alloc2, since if it was allocated we are passing it
2760 back (under the name of internal_relocs). */
2762 return internal_relocs;
2772 /* Record an assignment to a symbol made by a linker script. We need
2773 this in case some dynamic object refers to this symbol. */
2776 NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
2777 bfd *output_bfd ATTRIBUTE_UNUSED;
2778 struct bfd_link_info *info;
2782 struct elf_link_hash_entry *h;
2784 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2787 h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
2791 if (h->root.type == bfd_link_hash_new)
2792 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
2794 /* If this symbol is being provided by the linker script, and it is
2795 currently defined by a dynamic object, but not by a regular
2796 object, then mark it as undefined so that the generic linker will
2797 force the correct value. */
2799 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2800 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2801 h->root.type = bfd_link_hash_undefined;
2803 /* If this symbol is not being provided by the linker script, and it is
2804 currently defined by a dynamic object, but not by a regular object,
2805 then clear out any version information because the symbol will not be
2806 associated with the dynamic object any more. */
2808 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2809 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2810 h->verinfo.verdef = NULL;
2812 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2814 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2815 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
2817 && h->dynindx == -1)
2819 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2822 /* If this is a weak defined symbol, and we know a corresponding
2823 real symbol from the same dynamic object, make sure the real
2824 symbol is also made into a dynamic symbol. */
2825 if (h->weakdef != NULL
2826 && h->weakdef->dynindx == -1)
2828 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2836 /* This structure is used to pass information to
2837 elf_link_assign_sym_version. */
2839 struct elf_assign_sym_version_info
2843 /* General link information. */
2844 struct bfd_link_info *info;
2846 struct bfd_elf_version_tree *verdefs;
2847 /* Whether we had a failure. */
2851 /* This structure is used to pass information to
2852 elf_link_find_version_dependencies. */
2854 struct elf_find_verdep_info
2858 /* General link information. */
2859 struct bfd_link_info *info;
2860 /* The number of dependencies. */
2862 /* Whether we had a failure. */
2866 /* Array used to determine the number of hash table buckets to use
2867 based on the number of symbols there are. If there are fewer than
2868 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2869 fewer than 37 we use 17 buckets, and so forth. We never use more
2870 than 32771 buckets. */
2872 static const size_t elf_buckets[] =
2874 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2878 /* Compute bucket count for hashing table. We do not use a static set
2879 of possible tables sizes anymore. Instead we determine for all
2880 possible reasonable sizes of the table the outcome (i.e., the
2881 number of collisions etc) and choose the best solution. The
2882 weighting functions are not too simple to allow the table to grow
2883 without bounds. Instead one of the weighting factors is the size.
2884 Therefore the result is always a good payoff between few collisions
2885 (= short chain lengths) and table size. */
2887 compute_bucket_count (info)
2888 struct bfd_link_info *info;
2890 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
2891 size_t best_size = 0;
2892 unsigned long int *hashcodes;
2893 unsigned long int *hashcodesp;
2894 unsigned long int i;
2897 /* Compute the hash values for all exported symbols. At the same
2898 time store the values in an array so that we could use them for
2901 amt *= sizeof (unsigned long int);
2902 hashcodes = (unsigned long int *) bfd_malloc (amt);
2903 if (hashcodes == NULL)
2905 hashcodesp = hashcodes;
2907 /* Put all hash values in HASHCODES. */
2908 elf_link_hash_traverse (elf_hash_table (info),
2909 elf_collect_hash_codes, &hashcodesp);
2911 /* We have a problem here. The following code to optimize the table
2912 size requires an integer type with more the 32 bits. If
2913 BFD_HOST_U_64_BIT is set we know about such a type. */
2914 #ifdef BFD_HOST_U_64_BIT
2915 if (info->optimize == true)
2917 unsigned long int nsyms = hashcodesp - hashcodes;
2920 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
2921 unsigned long int *counts ;
2923 /* Possible optimization parameters: if we have NSYMS symbols we say
2924 that the hashing table must at least have NSYMS/4 and at most
2926 minsize = nsyms / 4;
2929 best_size = maxsize = nsyms * 2;
2931 /* Create array where we count the collisions in. We must use bfd_malloc
2932 since the size could be large. */
2934 amt *= sizeof (unsigned long int);
2935 counts = (unsigned long int *) bfd_malloc (amt);
2942 /* Compute the "optimal" size for the hash table. The criteria is a
2943 minimal chain length. The minor criteria is (of course) the size
2945 for (i = minsize; i < maxsize; ++i)
2947 /* Walk through the array of hashcodes and count the collisions. */
2948 BFD_HOST_U_64_BIT max;
2949 unsigned long int j;
2950 unsigned long int fact;
2952 memset (counts, '\0', i * sizeof (unsigned long int));
2954 /* Determine how often each hash bucket is used. */
2955 for (j = 0; j < nsyms; ++j)
2956 ++counts[hashcodes[j] % i];
2958 /* For the weight function we need some information about the
2959 pagesize on the target. This is information need not be 100%
2960 accurate. Since this information is not available (so far) we
2961 define it here to a reasonable default value. If it is crucial
2962 to have a better value some day simply define this value. */
2963 # ifndef BFD_TARGET_PAGESIZE
2964 # define BFD_TARGET_PAGESIZE (4096)
2967 /* We in any case need 2 + NSYMS entries for the size values and
2969 max = (2 + nsyms) * (ARCH_SIZE / 8);
2972 /* Variant 1: optimize for short chains. We add the squares
2973 of all the chain lengths (which favous many small chain
2974 over a few long chains). */
2975 for (j = 0; j < i; ++j)
2976 max += counts[j] * counts[j];
2978 /* This adds penalties for the overall size of the table. */
2979 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2982 /* Variant 2: Optimize a lot more for small table. Here we
2983 also add squares of the size but we also add penalties for
2984 empty slots (the +1 term). */
2985 for (j = 0; j < i; ++j)
2986 max += (1 + counts[j]) * (1 + counts[j]);
2988 /* The overall size of the table is considered, but not as
2989 strong as in variant 1, where it is squared. */
2990 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2994 /* Compare with current best results. */
2995 if (max < best_chlen)
3005 #endif /* defined (BFD_HOST_U_64_BIT) */
3007 /* This is the fallback solution if no 64bit type is available or if we
3008 are not supposed to spend much time on optimizations. We select the
3009 bucket count using a fixed set of numbers. */
3010 for (i = 0; elf_buckets[i] != 0; i++)
3012 best_size = elf_buckets[i];
3013 if (dynsymcount < elf_buckets[i + 1])
3018 /* Free the arrays we needed. */
3024 /* Set up the sizes and contents of the ELF dynamic sections. This is
3025 called by the ELF linker emulation before_allocation routine. We
3026 must set the sizes of the sections before the linker sets the
3027 addresses of the various sections. */
3030 NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
3032 auxiliary_filters, info, sinterpptr,
3037 const char *filter_shlib;
3038 const char * const *auxiliary_filters;
3039 struct bfd_link_info *info;
3040 asection **sinterpptr;
3041 struct bfd_elf_version_tree *verdefs;
3043 bfd_size_type soname_indx;
3045 struct elf_backend_data *bed;
3046 struct elf_assign_sym_version_info asvinfo;
3050 soname_indx = (bfd_size_type) -1;
3052 if (info->hash->creator->flavour != bfd_target_elf_flavour)
3055 if (! is_elf_hash_table (info))
3058 /* Any syms created from now on start with -1 in
3059 got.refcount/offset and plt.refcount/offset. */
3060 elf_hash_table (info)->init_refcount = -1;
3062 /* The backend may have to create some sections regardless of whether
3063 we're dynamic or not. */
3064 bed = get_elf_backend_data (output_bfd);
3065 if (bed->elf_backend_always_size_sections
3066 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
3069 dynobj = elf_hash_table (info)->dynobj;
3071 /* If there were no dynamic objects in the link, there is nothing to
3076 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
3079 if (elf_hash_table (info)->dynamic_sections_created)
3081 struct elf_info_failed eif;
3082 struct elf_link_hash_entry *h;
3085 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
3086 BFD_ASSERT (*sinterpptr != NULL || info->shared);
3090 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3092 if (soname_indx == (bfd_size_type) -1
3093 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SONAME,
3100 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMBOLIC,
3103 info->flags |= DF_SYMBOLIC;
3110 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
3112 if (info->new_dtags)
3113 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
3114 if (indx == (bfd_size_type) -1
3115 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_RPATH, indx)
3117 && ! elf_add_dynamic_entry (info, (bfd_vma) DT_RUNPATH,
3122 if (filter_shlib != NULL)
3126 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3127 filter_shlib, true);
3128 if (indx == (bfd_size_type) -1
3129 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_FILTER, indx))
3133 if (auxiliary_filters != NULL)
3135 const char * const *p;
3137 for (p = auxiliary_filters; *p != NULL; p++)
3141 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3143 if (indx == (bfd_size_type) -1
3144 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_AUXILIARY,
3151 eif.verdefs = verdefs;
3154 /* If we are supposed to export all symbols into the dynamic symbol
3155 table (this is not the normal case), then do so. */
3156 if (info->export_dynamic)
3158 elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
3164 /* Attach all the symbols to their version information. */
3165 asvinfo.output_bfd = output_bfd;
3166 asvinfo.info = info;
3167 asvinfo.verdefs = verdefs;
3168 asvinfo.failed = false;
3170 elf_link_hash_traverse (elf_hash_table (info),
3171 elf_link_assign_sym_version,
3176 /* Find all symbols which were defined in a dynamic object and make
3177 the backend pick a reasonable value for them. */
3178 elf_link_hash_traverse (elf_hash_table (info),
3179 elf_adjust_dynamic_symbol,
3184 /* Add some entries to the .dynamic section. We fill in some of the
3185 values later, in elf_bfd_final_link, but we must add the entries
3186 now so that we know the final size of the .dynamic section. */
3188 /* If there are initialization and/or finalization functions to
3189 call then add the corresponding DT_INIT/DT_FINI entries. */
3190 h = (info->init_function
3191 ? elf_link_hash_lookup (elf_hash_table (info),
3192 info->init_function, false,
3196 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3197 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3199 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_INIT, (bfd_vma) 0))
3202 h = (info->fini_function
3203 ? elf_link_hash_lookup (elf_hash_table (info),
3204 info->fini_function, false,
3208 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3209 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3211 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FINI, (bfd_vma) 0))
3215 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
3217 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3223 for (sub = info->input_bfds; sub != NULL;
3224 sub = sub->link_next)
3225 for (o = sub->sections; o != NULL; o = o->next)
3226 if (elf_section_data (o)->this_hdr.sh_type
3227 == SHT_PREINIT_ARRAY)
3229 (*_bfd_error_handler)
3230 (_("%s: .preinit_array section is not allowed in DSO"),
3231 bfd_archive_filename (sub));
3235 bfd_set_error (bfd_error_nonrepresentable_section);
3239 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAY,
3241 || !elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAYSZ,
3245 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
3247 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAY,
3249 || !elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAYSZ,
3253 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
3255 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAY,
3257 || !elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAYSZ,
3262 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
3263 /* If .dynstr is excluded from the link, we don't want any of
3264 these tags. Strictly, we should be checking each section
3265 individually; This quick check covers for the case where
3266 someone does a /DISCARD/ : { *(*) }. */
3267 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
3269 bfd_size_type strsize;
3271 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3272 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_HASH, (bfd_vma) 0)
3273 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRTAB, (bfd_vma) 0)
3274 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMTAB, (bfd_vma) 0)
3275 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRSZ, strsize)
3276 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMENT,
3277 (bfd_vma) sizeof (Elf_External_Sym)))
3282 /* The backend must work out the sizes of all the other dynamic
3284 if (bed->elf_backend_size_dynamic_sections
3285 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
3288 if (elf_hash_table (info)->dynamic_sections_created)
3290 bfd_size_type dynsymcount;
3292 size_t bucketcount = 0;
3293 size_t hash_entry_size;
3294 unsigned int dtagcount;
3296 /* Set up the version definition section. */
3297 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3298 BFD_ASSERT (s != NULL);
3300 /* We may have created additional version definitions if we are
3301 just linking a regular application. */
3302 verdefs = asvinfo.verdefs;
3304 /* Skip anonymous version tag. */
3305 if (verdefs != NULL && verdefs->vernum == 0)
3306 verdefs = verdefs->next;
3308 if (verdefs == NULL)
3309 _bfd_strip_section_from_output (info, s);
3314 struct bfd_elf_version_tree *t;
3316 Elf_Internal_Verdef def;
3317 Elf_Internal_Verdaux defaux;
3322 /* Make space for the base version. */
3323 size += sizeof (Elf_External_Verdef);
3324 size += sizeof (Elf_External_Verdaux);
3327 for (t = verdefs; t != NULL; t = t->next)
3329 struct bfd_elf_version_deps *n;
3331 size += sizeof (Elf_External_Verdef);
3332 size += sizeof (Elf_External_Verdaux);
3335 for (n = t->deps; n != NULL; n = n->next)
3336 size += sizeof (Elf_External_Verdaux);
3339 s->_raw_size = size;
3340 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3341 if (s->contents == NULL && s->_raw_size != 0)
3344 /* Fill in the version definition section. */
3348 def.vd_version = VER_DEF_CURRENT;
3349 def.vd_flags = VER_FLG_BASE;
3352 def.vd_aux = sizeof (Elf_External_Verdef);
3353 def.vd_next = (sizeof (Elf_External_Verdef)
3354 + sizeof (Elf_External_Verdaux));
3356 if (soname_indx != (bfd_size_type) -1)
3358 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3360 def.vd_hash = bfd_elf_hash (soname);
3361 defaux.vda_name = soname_indx;
3368 name = basename (output_bfd->filename);
3369 def.vd_hash = bfd_elf_hash (name);
3370 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3372 if (indx == (bfd_size_type) -1)
3374 defaux.vda_name = indx;
3376 defaux.vda_next = 0;
3378 _bfd_elf_swap_verdef_out (output_bfd, &def,
3379 (Elf_External_Verdef *) p);
3380 p += sizeof (Elf_External_Verdef);
3381 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3382 (Elf_External_Verdaux *) p);
3383 p += sizeof (Elf_External_Verdaux);
3385 for (t = verdefs; t != NULL; t = t->next)
3388 struct bfd_elf_version_deps *n;
3389 struct elf_link_hash_entry *h;
3392 for (n = t->deps; n != NULL; n = n->next)
3395 /* Add a symbol representing this version. */
3397 if (! (_bfd_generic_link_add_one_symbol
3398 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
3399 (bfd_vma) 0, (const char *) NULL, false,
3400 get_elf_backend_data (dynobj)->collect,
3401 (struct bfd_link_hash_entry **) &h)))
3403 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
3404 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3405 h->type = STT_OBJECT;
3406 h->verinfo.vertree = t;
3408 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
3411 def.vd_version = VER_DEF_CURRENT;
3413 if (t->globals == NULL && t->locals == NULL && ! t->used)
3414 def.vd_flags |= VER_FLG_WEAK;
3415 def.vd_ndx = t->vernum + 1;
3416 def.vd_cnt = cdeps + 1;
3417 def.vd_hash = bfd_elf_hash (t->name);
3418 def.vd_aux = sizeof (Elf_External_Verdef);
3419 if (t->next != NULL)
3420 def.vd_next = (sizeof (Elf_External_Verdef)
3421 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
3425 _bfd_elf_swap_verdef_out (output_bfd, &def,
3426 (Elf_External_Verdef *) p);
3427 p += sizeof (Elf_External_Verdef);
3429 defaux.vda_name = h->dynstr_index;
3430 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3432 if (t->deps == NULL)
3433 defaux.vda_next = 0;
3435 defaux.vda_next = sizeof (Elf_External_Verdaux);
3436 t->name_indx = defaux.vda_name;
3438 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3439 (Elf_External_Verdaux *) p);
3440 p += sizeof (Elf_External_Verdaux);
3442 for (n = t->deps; n != NULL; n = n->next)
3444 if (n->version_needed == NULL)
3446 /* This can happen if there was an error in the
3448 defaux.vda_name = 0;
3452 defaux.vda_name = n->version_needed->name_indx;
3453 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3456 if (n->next == NULL)
3457 defaux.vda_next = 0;
3459 defaux.vda_next = sizeof (Elf_External_Verdaux);
3461 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3462 (Elf_External_Verdaux *) p);
3463 p += sizeof (Elf_External_Verdaux);
3467 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEF, (bfd_vma) 0)
3468 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEFNUM,
3472 elf_tdata (output_bfd)->cverdefs = cdefs;
3475 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
3477 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags))
3484 info->flags_1 &= ~ (DF_1_INITFIRST
3487 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS_1,
3492 /* Work out the size of the version reference section. */
3494 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3495 BFD_ASSERT (s != NULL);
3497 struct elf_find_verdep_info sinfo;
3499 sinfo.output_bfd = output_bfd;
3501 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
3502 if (sinfo.vers == 0)
3504 sinfo.failed = false;
3506 elf_link_hash_traverse (elf_hash_table (info),
3507 elf_link_find_version_dependencies,
3510 if (elf_tdata (output_bfd)->verref == NULL)
3511 _bfd_strip_section_from_output (info, s);
3514 Elf_Internal_Verneed *t;
3519 /* Build the version definition section. */
3522 for (t = elf_tdata (output_bfd)->verref;
3526 Elf_Internal_Vernaux *a;
3528 size += sizeof (Elf_External_Verneed);
3530 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3531 size += sizeof (Elf_External_Vernaux);
3534 s->_raw_size = size;
3535 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3536 if (s->contents == NULL)
3540 for (t = elf_tdata (output_bfd)->verref;
3545 Elf_Internal_Vernaux *a;
3549 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3552 t->vn_version = VER_NEED_CURRENT;
3554 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3555 elf_dt_name (t->vn_bfd) != NULL
3556 ? elf_dt_name (t->vn_bfd)
3557 : basename (t->vn_bfd->filename),
3559 if (indx == (bfd_size_type) -1)
3562 t->vn_aux = sizeof (Elf_External_Verneed);
3563 if (t->vn_nextref == NULL)
3566 t->vn_next = (sizeof (Elf_External_Verneed)
3567 + caux * sizeof (Elf_External_Vernaux));
3569 _bfd_elf_swap_verneed_out (output_bfd, t,
3570 (Elf_External_Verneed *) p);
3571 p += sizeof (Elf_External_Verneed);
3573 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3575 a->vna_hash = bfd_elf_hash (a->vna_nodename);
3576 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3577 a->vna_nodename, false);
3578 if (indx == (bfd_size_type) -1)
3581 if (a->vna_nextptr == NULL)
3584 a->vna_next = sizeof (Elf_External_Vernaux);
3586 _bfd_elf_swap_vernaux_out (output_bfd, a,
3587 (Elf_External_Vernaux *) p);
3588 p += sizeof (Elf_External_Vernaux);
3592 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEED,
3594 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEEDNUM,
3598 elf_tdata (output_bfd)->cverrefs = crefs;
3602 /* Assign dynsym indicies. In a shared library we generate a
3603 section symbol for each output section, which come first.
3604 Next come all of the back-end allocated local dynamic syms,
3605 followed by the rest of the global symbols. */
3607 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3609 /* Work out the size of the symbol version section. */
3610 s = bfd_get_section_by_name (dynobj, ".gnu.version");
3611 BFD_ASSERT (s != NULL);
3612 if (dynsymcount == 0
3613 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
3615 _bfd_strip_section_from_output (info, s);
3616 /* The DYNSYMCOUNT might have changed if we were going to
3617 output a dynamic symbol table entry for S. */
3618 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3622 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
3623 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3624 if (s->contents == NULL)
3627 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERSYM, (bfd_vma) 0))
3631 /* Set the size of the .dynsym and .hash sections. We counted
3632 the number of dynamic symbols in elf_link_add_object_symbols.
3633 We will build the contents of .dynsym and .hash when we build
3634 the final symbol table, because until then we do not know the
3635 correct value to give the symbols. We built the .dynstr
3636 section as we went along in elf_link_add_object_symbols. */
3637 s = bfd_get_section_by_name (dynobj, ".dynsym");
3638 BFD_ASSERT (s != NULL);
3639 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
3640 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3641 if (s->contents == NULL && s->_raw_size != 0)
3644 if (dynsymcount != 0)
3646 Elf_Internal_Sym isym;
3648 /* The first entry in .dynsym is a dummy symbol. */
3655 elf_swap_symbol_out (output_bfd, &isym, (PTR) s->contents, (PTR) 0);
3658 /* Compute the size of the hashing table. As a side effect this
3659 computes the hash values for all the names we export. */
3660 bucketcount = compute_bucket_count (info);
3662 s = bfd_get_section_by_name (dynobj, ".hash");
3663 BFD_ASSERT (s != NULL);
3664 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
3665 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
3666 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3667 if (s->contents == NULL)
3670 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount,
3672 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) dynsymcount,
3673 s->contents + hash_entry_size);
3675 elf_hash_table (info)->bucketcount = bucketcount;
3677 s = bfd_get_section_by_name (dynobj, ".dynstr");
3678 BFD_ASSERT (s != NULL);
3680 elf_finalize_dynstr (output_bfd, info);
3682 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3684 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
3685 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NULL, (bfd_vma) 0))
3692 /* This function is used to adjust offsets into .dynstr for
3693 dynamic symbols. This is called via elf_link_hash_traverse. */
3695 static boolean elf_adjust_dynstr_offsets
3696 PARAMS ((struct elf_link_hash_entry *, PTR));
3699 elf_adjust_dynstr_offsets (h, data)
3700 struct elf_link_hash_entry *h;
3703 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3705 if (h->root.type == bfd_link_hash_warning)
3706 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3708 if (h->dynindx != -1)
3709 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3713 /* Assign string offsets in .dynstr, update all structures referencing
3717 elf_finalize_dynstr (output_bfd, info)
3719 struct bfd_link_info *info;
3721 struct elf_link_local_dynamic_entry *entry;
3722 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
3723 bfd *dynobj = elf_hash_table (info)->dynobj;
3726 Elf_External_Dyn *dyncon, *dynconend;
3728 _bfd_elf_strtab_finalize (dynstr);
3729 size = _bfd_elf_strtab_size (dynstr);
3731 /* Update all .dynamic entries referencing .dynstr strings. */
3732 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3733 BFD_ASSERT (sdyn != NULL);
3735 dyncon = (Elf_External_Dyn *) sdyn->contents;
3736 dynconend = (Elf_External_Dyn *) (sdyn->contents +
3738 for (; dyncon < dynconend; dyncon++)
3740 Elf_Internal_Dyn dyn;
3742 elf_swap_dyn_in (dynobj, dyncon, & dyn);
3746 dyn.d_un.d_val = size;
3747 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3755 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3756 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3763 /* Now update local dynamic symbols. */
3764 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
3765 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3766 entry->isym.st_name);
3768 /* And the rest of dynamic symbols. */
3769 elf_link_hash_traverse (elf_hash_table (info),
3770 elf_adjust_dynstr_offsets, dynstr);
3772 /* Adjust version definitions. */
3773 if (elf_tdata (output_bfd)->cverdefs)
3778 Elf_Internal_Verdef def;
3779 Elf_Internal_Verdaux defaux;
3781 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3782 p = (bfd_byte *) s->contents;
3785 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3787 p += sizeof (Elf_External_Verdef);
3788 for (i = 0; i < def.vd_cnt; ++i)
3790 _bfd_elf_swap_verdaux_in (output_bfd,
3791 (Elf_External_Verdaux *) p, &defaux);
3792 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3794 _bfd_elf_swap_verdaux_out (output_bfd,
3795 &defaux, (Elf_External_Verdaux *) p);
3796 p += sizeof (Elf_External_Verdaux);
3799 while (def.vd_next);
3802 /* Adjust version references. */
3803 if (elf_tdata (output_bfd)->verref)
3808 Elf_Internal_Verneed need;
3809 Elf_Internal_Vernaux needaux;
3811 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3812 p = (bfd_byte *) s->contents;
3815 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3817 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3818 _bfd_elf_swap_verneed_out (output_bfd, &need,
3819 (Elf_External_Verneed *) p);
3820 p += sizeof (Elf_External_Verneed);
3821 for (i = 0; i < need.vn_cnt; ++i)
3823 _bfd_elf_swap_vernaux_in (output_bfd,
3824 (Elf_External_Vernaux *) p, &needaux);
3825 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3827 _bfd_elf_swap_vernaux_out (output_bfd,
3829 (Elf_External_Vernaux *) p);
3830 p += sizeof (Elf_External_Vernaux);
3833 while (need.vn_next);
3839 /* Fix up the flags for a symbol. This handles various cases which
3840 can only be fixed after all the input files are seen. This is
3841 currently called by both adjust_dynamic_symbol and
3842 assign_sym_version, which is unnecessary but perhaps more robust in
3843 the face of future changes. */
3846 elf_fix_symbol_flags (h, eif)
3847 struct elf_link_hash_entry *h;
3848 struct elf_info_failed *eif;
3850 /* If this symbol was mentioned in a non-ELF file, try to set
3851 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3852 permit a non-ELF file to correctly refer to a symbol defined in
3853 an ELF dynamic object. */
3854 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
3856 while (h->root.type == bfd_link_hash_indirect)
3857 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3859 if (h->root.type != bfd_link_hash_defined
3860 && h->root.type != bfd_link_hash_defweak)
3861 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3862 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3865 if (h->root.u.def.section->owner != NULL
3866 && (bfd_get_flavour (h->root.u.def.section->owner)
3867 == bfd_target_elf_flavour))
3868 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3869 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3871 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3874 if (h->dynindx == -1
3875 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3876 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3878 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3887 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3888 was first seen in a non-ELF file. Fortunately, if the symbol
3889 was first seen in an ELF file, we're probably OK unless the
3890 symbol was defined in a non-ELF file. Catch that case here.
3891 FIXME: We're still in trouble if the symbol was first seen in
3892 a dynamic object, and then later in a non-ELF regular object. */
3893 if ((h->root.type == bfd_link_hash_defined
3894 || h->root.type == bfd_link_hash_defweak)
3895 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3896 && (h->root.u.def.section->owner != NULL
3897 ? (bfd_get_flavour (h->root.u.def.section->owner)
3898 != bfd_target_elf_flavour)
3899 : (bfd_is_abs_section (h->root.u.def.section)
3900 && (h->elf_link_hash_flags
3901 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
3902 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3905 /* If this is a final link, and the symbol was defined as a common
3906 symbol in a regular object file, and there was no definition in
3907 any dynamic object, then the linker will have allocated space for
3908 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3909 flag will not have been set. */
3910 if (h->root.type == bfd_link_hash_defined
3911 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3912 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
3913 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3914 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3915 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3917 /* If -Bsymbolic was used (which means to bind references to global
3918 symbols to the definition within the shared object), and this
3919 symbol was defined in a regular object, then it actually doesn't
3920 need a PLT entry, and we can accomplish that by forcing it local.
3921 Likewise, if the symbol has hidden or internal visibility.
3922 FIXME: It might be that we also do not need a PLT for other
3923 non-hidden visibilities, but we would have to tell that to the
3924 backend specifically; we can't just clear PLT-related data here. */
3925 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
3926 && eif->info->shared
3927 && is_elf_hash_table (eif->info)
3928 && (eif->info->symbolic
3929 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3930 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3931 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3933 struct elf_backend_data *bed;
3934 boolean force_local;
3936 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3938 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3939 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3940 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3943 /* If this is a weak defined symbol in a dynamic object, and we know
3944 the real definition in the dynamic object, copy interesting flags
3945 over to the real definition. */
3946 if (h->weakdef != NULL)
3948 struct elf_link_hash_entry *weakdef;
3950 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3951 || h->root.type == bfd_link_hash_defweak);
3952 weakdef = h->weakdef;
3953 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
3954 || weakdef->root.type == bfd_link_hash_defweak);
3955 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
3957 /* If the real definition is defined by a regular object file,
3958 don't do anything special. See the longer description in
3959 elf_adjust_dynamic_symbol, below. */
3960 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3964 struct elf_backend_data *bed;
3966 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3967 (*bed->elf_backend_copy_indirect_symbol) (weakdef, h);
3974 /* Make the backend pick a good value for a dynamic symbol. This is
3975 called via elf_link_hash_traverse, and also calls itself
3979 elf_adjust_dynamic_symbol (h, data)
3980 struct elf_link_hash_entry *h;
3983 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3985 struct elf_backend_data *bed;
3987 if (h->root.type == bfd_link_hash_warning)
3989 h->plt.offset = (bfd_vma) -1;
3990 h->got.offset = (bfd_vma) -1;
3992 /* When warning symbols are created, they **replace** the "real"
3993 entry in the hash table, thus we never get to see the real
3994 symbol in a hash traversal. So look at it now. */
3995 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3998 /* Ignore indirect symbols. These are added by the versioning code. */
3999 if (h->root.type == bfd_link_hash_indirect)
4002 if (! is_elf_hash_table (eif->info))
4005 /* Fix the symbol flags. */
4006 if (! elf_fix_symbol_flags (h, eif))
4009 /* If this symbol does not require a PLT entry, and it is not
4010 defined by a dynamic object, or is not referenced by a regular
4011 object, ignore it. We do have to handle a weak defined symbol,
4012 even if no regular object refers to it, if we decided to add it
4013 to the dynamic symbol table. FIXME: Do we normally need to worry
4014 about symbols which are defined by one dynamic object and
4015 referenced by another one? */
4016 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
4017 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4018 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4019 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
4020 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
4022 h->plt.offset = (bfd_vma) -1;
4026 /* If we've already adjusted this symbol, don't do it again. This
4027 can happen via a recursive call. */
4028 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
4031 /* Don't look at this symbol again. Note that we must set this
4032 after checking the above conditions, because we may look at a
4033 symbol once, decide not to do anything, and then get called
4034 recursively later after REF_REGULAR is set below. */
4035 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
4037 /* If this is a weak definition, and we know a real definition, and
4038 the real symbol is not itself defined by a regular object file,
4039 then get a good value for the real definition. We handle the
4040 real symbol first, for the convenience of the backend routine.
4042 Note that there is a confusing case here. If the real definition
4043 is defined by a regular object file, we don't get the real symbol
4044 from the dynamic object, but we do get the weak symbol. If the
4045 processor backend uses a COPY reloc, then if some routine in the
4046 dynamic object changes the real symbol, we will not see that
4047 change in the corresponding weak symbol. This is the way other
4048 ELF linkers work as well, and seems to be a result of the shared
4051 I will clarify this issue. Most SVR4 shared libraries define the
4052 variable _timezone and define timezone as a weak synonym. The
4053 tzset call changes _timezone. If you write
4054 extern int timezone;
4056 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4057 you might expect that, since timezone is a synonym for _timezone,
4058 the same number will print both times. However, if the processor
4059 backend uses a COPY reloc, then actually timezone will be copied
4060 into your process image, and, since you define _timezone
4061 yourself, _timezone will not. Thus timezone and _timezone will
4062 wind up at different memory locations. The tzset call will set
4063 _timezone, leaving timezone unchanged. */
4065 if (h->weakdef != NULL)
4067 /* If we get to this point, we know there is an implicit
4068 reference by a regular object file via the weak symbol H.
4069 FIXME: Is this really true? What if the traversal finds
4070 H->WEAKDEF before it finds H? */
4071 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
4073 if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif))
4077 /* If a symbol has no type and no size and does not require a PLT
4078 entry, then we are probably about to do the wrong thing here: we
4079 are probably going to create a COPY reloc for an empty object.
4080 This case can arise when a shared object is built with assembly
4081 code, and the assembly code fails to set the symbol type. */
4083 && h->type == STT_NOTYPE
4084 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
4085 (*_bfd_error_handler)
4086 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4087 h->root.root.string);
4089 dynobj = elf_hash_table (eif->info)->dynobj;
4090 bed = get_elf_backend_data (dynobj);
4091 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
4100 /* This routine is used to export all defined symbols into the dynamic
4101 symbol table. It is called via elf_link_hash_traverse. */
4104 elf_export_symbol (h, data)
4105 struct elf_link_hash_entry *h;
4108 struct elf_info_failed *eif = (struct elf_info_failed *) data;
4110 /* Ignore indirect symbols. These are added by the versioning code. */
4111 if (h->root.type == bfd_link_hash_indirect)
4114 if (h->root.type == bfd_link_hash_warning)
4115 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4117 if (h->dynindx == -1
4118 && (h->elf_link_hash_flags
4119 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
4121 struct bfd_elf_version_tree *t;
4122 struct bfd_elf_version_expr *d;
4124 for (t = eif->verdefs; t != NULL; t = t->next)
4126 if (t->globals != NULL)
4128 for (d = t->globals; d != NULL; d = d->next)
4130 if ((*d->match) (d, h->root.root.string))
4135 if (t->locals != NULL)
4137 for (d = t->locals ; d != NULL; d = d->next)
4139 if ((*d->match) (d, h->root.root.string))
4148 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
4159 /* Look through the symbols which are defined in other shared
4160 libraries and referenced here. Update the list of version
4161 dependencies. This will be put into the .gnu.version_r section.
4162 This function is called via elf_link_hash_traverse. */
4165 elf_link_find_version_dependencies (h, data)
4166 struct elf_link_hash_entry *h;
4169 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
4170 Elf_Internal_Verneed *t;
4171 Elf_Internal_Vernaux *a;
4174 if (h->root.type == bfd_link_hash_warning)
4175 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4177 /* We only care about symbols defined in shared objects with version
4179 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4180 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4182 || h->verinfo.verdef == NULL)
4185 /* See if we already know about this version. */
4186 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
4188 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
4191 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4192 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
4198 /* This is a new version. Add it to tree we are building. */
4203 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt);
4206 rinfo->failed = true;
4210 t->vn_bfd = h->verinfo.verdef->vd_bfd;
4211 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
4212 elf_tdata (rinfo->output_bfd)->verref = t;
4216 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt);
4218 /* Note that we are copying a string pointer here, and testing it
4219 above. If bfd_elf_string_from_elf_section is ever changed to
4220 discard the string data when low in memory, this will have to be
4222 a->vna_nodename = h->verinfo.verdef->vd_nodename;
4224 a->vna_flags = h->verinfo.verdef->vd_flags;
4225 a->vna_nextptr = t->vn_auxptr;
4227 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
4230 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
4237 /* Figure out appropriate versions for all the symbols. We may not
4238 have the version number script until we have read all of the input
4239 files, so until that point we don't know which symbols should be
4240 local. This function is called via elf_link_hash_traverse. */
4243 elf_link_assign_sym_version (h, data)
4244 struct elf_link_hash_entry *h;
4247 struct elf_assign_sym_version_info *sinfo;
4248 struct bfd_link_info *info;
4249 struct elf_backend_data *bed;
4250 struct elf_info_failed eif;
4254 sinfo = (struct elf_assign_sym_version_info *) data;
4257 if (h->root.type == bfd_link_hash_warning)
4258 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4260 /* Fix the symbol flags. */
4263 if (! elf_fix_symbol_flags (h, &eif))
4266 sinfo->failed = true;
4270 /* We only need version numbers for symbols defined in regular
4272 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4275 bed = get_elf_backend_data (sinfo->output_bfd);
4276 p = strchr (h->root.root.string, ELF_VER_CHR);
4277 if (p != NULL && h->verinfo.vertree == NULL)
4279 struct bfd_elf_version_tree *t;
4284 /* There are two consecutive ELF_VER_CHR characters if this is
4285 not a hidden symbol. */
4287 if (*p == ELF_VER_CHR)
4293 /* If there is no version string, we can just return out. */
4297 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4301 /* Look for the version. If we find it, it is no longer weak. */
4302 for (t = sinfo->verdefs; t != NULL; t = t->next)
4304 if (strcmp (t->name, p) == 0)
4308 struct bfd_elf_version_expr *d;
4310 len = p - h->root.root.string;
4311 alc = bfd_malloc ((bfd_size_type) len);
4314 strncpy (alc, h->root.root.string, len - 1);
4315 alc[len - 1] = '\0';
4316 if (alc[len - 2] == ELF_VER_CHR)
4317 alc[len - 2] = '\0';
4319 h->verinfo.vertree = t;
4323 if (t->globals != NULL)
4325 for (d = t->globals; d != NULL; d = d->next)
4326 if ((*d->match) (d, alc))
4330 /* See if there is anything to force this symbol to
4332 if (d == NULL && t->locals != NULL)
4334 for (d = t->locals; d != NULL; d = d->next)
4336 if ((*d->match) (d, alc))
4338 if (h->dynindx != -1
4340 && ! info->export_dynamic)
4342 (*bed->elf_backend_hide_symbol) (info, h, true);
4355 /* If we are building an application, we need to create a
4356 version node for this version. */
4357 if (t == NULL && ! info->shared)
4359 struct bfd_elf_version_tree **pp;
4362 /* If we aren't going to export this symbol, we don't need
4363 to worry about it. */
4364 if (h->dynindx == -1)
4368 t = ((struct bfd_elf_version_tree *)
4369 bfd_alloc (sinfo->output_bfd, amt));
4372 sinfo->failed = true;
4381 t->name_indx = (unsigned int) -1;
4385 /* Don't count anonymous version tag. */
4386 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
4388 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
4390 t->vernum = version_index;
4394 h->verinfo.vertree = t;
4398 /* We could not find the version for a symbol when
4399 generating a shared archive. Return an error. */
4400 (*_bfd_error_handler)
4401 (_("%s: undefined versioned symbol name %s"),
4402 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
4403 bfd_set_error (bfd_error_bad_value);
4404 sinfo->failed = true;
4409 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4412 /* If we don't have a version for this symbol, see if we can find
4414 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
4416 struct bfd_elf_version_tree *t;
4417 struct bfd_elf_version_tree *deflt;
4418 struct bfd_elf_version_expr *d;
4420 /* See if can find what version this symbol is in. If the
4421 symbol is supposed to be local, then don't actually register
4424 for (t = sinfo->verdefs; t != NULL; t = t->next)
4426 if (t->globals != NULL)
4428 for (d = t->globals; d != NULL; d = d->next)
4430 if ((*d->match) (d, h->root.root.string))
4432 h->verinfo.vertree = t;
4441 if (t->locals != NULL)
4443 for (d = t->locals; d != NULL; d = d->next)
4445 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
4447 else if ((*d->match) (d, h->root.root.string))
4449 h->verinfo.vertree = t;
4450 if (h->dynindx != -1
4452 && ! info->export_dynamic)
4454 (*bed->elf_backend_hide_symbol) (info, h, true);
4465 if (deflt != NULL && h->verinfo.vertree == NULL)
4467 h->verinfo.vertree = deflt;
4468 if (h->dynindx != -1
4470 && ! info->export_dynamic)
4472 (*bed->elf_backend_hide_symbol) (info, h, true);
4480 /* Final phase of ELF linker. */
4482 /* A structure we use to avoid passing large numbers of arguments. */
4484 struct elf_final_link_info
4486 /* General link information. */
4487 struct bfd_link_info *info;
4490 /* Symbol string table. */
4491 struct bfd_strtab_hash *symstrtab;
4492 /* .dynsym section. */
4493 asection *dynsym_sec;
4494 /* .hash section. */
4496 /* symbol version section (.gnu.version). */
4497 asection *symver_sec;
4498 /* first SHF_TLS section (if any). */
4499 asection *first_tls_sec;
4500 /* Buffer large enough to hold contents of any section. */
4502 /* Buffer large enough to hold external relocs of any section. */
4503 PTR external_relocs;
4504 /* Buffer large enough to hold internal relocs of any section. */
4505 Elf_Internal_Rela *internal_relocs;
4506 /* Buffer large enough to hold external local symbols of any input
4508 Elf_External_Sym *external_syms;
4509 /* And a buffer for symbol section indices. */
4510 Elf_External_Sym_Shndx *locsym_shndx;
4511 /* Buffer large enough to hold internal local symbols of any input
4513 Elf_Internal_Sym *internal_syms;
4514 /* Array large enough to hold a symbol index for each local symbol
4515 of any input BFD. */
4517 /* Array large enough to hold a section pointer for each local
4518 symbol of any input BFD. */
4519 asection **sections;
4520 /* Buffer to hold swapped out symbols. */
4521 Elf_External_Sym *symbuf;
4522 /* And one for symbol section indices. */
4523 Elf_External_Sym_Shndx *symshndxbuf;
4524 /* Number of swapped out symbols in buffer. */
4525 size_t symbuf_count;
4526 /* Number of symbols which fit in symbuf. */
4530 static boolean elf_link_output_sym
4531 PARAMS ((struct elf_final_link_info *, const char *,
4532 Elf_Internal_Sym *, asection *));
4533 static boolean elf_link_flush_output_syms
4534 PARAMS ((struct elf_final_link_info *));
4535 static boolean elf_link_output_extsym
4536 PARAMS ((struct elf_link_hash_entry *, PTR));
4537 static boolean elf_link_sec_merge_syms
4538 PARAMS ((struct elf_link_hash_entry *, PTR));
4539 static boolean elf_link_input_bfd
4540 PARAMS ((struct elf_final_link_info *, bfd *));
4541 static boolean elf_reloc_link_order
4542 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4543 struct bfd_link_order *));
4545 /* This struct is used to pass information to elf_link_output_extsym. */
4547 struct elf_outext_info
4551 struct elf_final_link_info *finfo;
4554 /* Compute the size of, and allocate space for, REL_HDR which is the
4555 section header for a section containing relocations for O. */
4558 elf_link_size_reloc_section (abfd, rel_hdr, o)
4560 Elf_Internal_Shdr *rel_hdr;
4563 bfd_size_type reloc_count;
4564 bfd_size_type num_rel_hashes;
4566 /* Figure out how many relocations there will be. */
4567 if (rel_hdr == &elf_section_data (o)->rel_hdr)
4568 reloc_count = elf_section_data (o)->rel_count;
4570 reloc_count = elf_section_data (o)->rel_count2;
4572 num_rel_hashes = o->reloc_count;
4573 if (num_rel_hashes < reloc_count)
4574 num_rel_hashes = reloc_count;
4576 /* That allows us to calculate the size of the section. */
4577 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
4579 /* The contents field must last into write_object_contents, so we
4580 allocate it with bfd_alloc rather than malloc. Also since we
4581 cannot be sure that the contents will actually be filled in,
4582 we zero the allocated space. */
4583 rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size);
4584 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
4587 /* We only allocate one set of hash entries, so we only do it the
4588 first time we are called. */
4589 if (elf_section_data (o)->rel_hashes == NULL
4592 struct elf_link_hash_entry **p;
4594 p = ((struct elf_link_hash_entry **)
4595 bfd_zmalloc (num_rel_hashes
4596 * sizeof (struct elf_link_hash_entry *)));
4600 elf_section_data (o)->rel_hashes = p;
4606 /* When performing a relocateable link, the input relocations are
4607 preserved. But, if they reference global symbols, the indices
4608 referenced must be updated. Update all the relocations in
4609 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4612 elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
4614 Elf_Internal_Shdr *rel_hdr;
4616 struct elf_link_hash_entry **rel_hash;
4619 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4620 Elf_Internal_Rel *irel;
4621 Elf_Internal_Rela *irela;
4622 bfd_size_type amt = sizeof (Elf_Internal_Rel) * bed->s->int_rels_per_ext_rel;
4624 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
4627 (*_bfd_error_handler) (_("Error: out of memory"));
4631 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
4632 irela = (Elf_Internal_Rela *) bfd_zmalloc (amt);
4635 (*_bfd_error_handler) (_("Error: out of memory"));
4639 for (i = 0; i < count; i++, rel_hash++)
4641 if (*rel_hash == NULL)
4644 BFD_ASSERT ((*rel_hash)->indx >= 0);
4646 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
4648 Elf_External_Rel *erel;
4651 erel = (Elf_External_Rel *) rel_hdr->contents + i;
4652 if (bed->s->swap_reloc_in)
4653 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
4655 elf_swap_reloc_in (abfd, erel, irel);
4657 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4658 irel[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4659 ELF_R_TYPE (irel[j].r_info));
4661 if (bed->s->swap_reloc_out)
4662 (*bed->s->swap_reloc_out) (abfd, irel, (bfd_byte *) erel);
4664 elf_swap_reloc_out (abfd, irel, erel);
4668 Elf_External_Rela *erela;
4671 BFD_ASSERT (rel_hdr->sh_entsize
4672 == sizeof (Elf_External_Rela));
4674 erela = (Elf_External_Rela *) rel_hdr->contents + i;
4675 if (bed->s->swap_reloca_in)
4676 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
4678 elf_swap_reloca_in (abfd, erela, irela);
4680 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4681 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4682 ELF_R_TYPE (irela[j].r_info));
4684 if (bed->s->swap_reloca_out)
4685 (*bed->s->swap_reloca_out) (abfd, irela, (bfd_byte *) erela);
4687 elf_swap_reloca_out (abfd, irela, erela);
4695 struct elf_link_sort_rela {
4697 enum elf_reloc_type_class type;
4699 Elf_Internal_Rel rel;
4700 Elf_Internal_Rela rela;
4705 elf_link_sort_cmp1 (A, B)
4709 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4710 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4711 int relativea, relativeb;
4713 relativea = a->type == reloc_class_relative;
4714 relativeb = b->type == reloc_class_relative;
4716 if (relativea < relativeb)
4718 if (relativea > relativeb)
4720 if (ELF_R_SYM (a->u.rel.r_info) < ELF_R_SYM (b->u.rel.r_info))
4722 if (ELF_R_SYM (a->u.rel.r_info) > ELF_R_SYM (b->u.rel.r_info))
4724 if (a->u.rel.r_offset < b->u.rel.r_offset)
4726 if (a->u.rel.r_offset > b->u.rel.r_offset)
4732 elf_link_sort_cmp2 (A, B)
4736 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4737 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4740 if (a->offset < b->offset)
4742 if (a->offset > b->offset)
4744 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
4745 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
4750 if (a->u.rel.r_offset < b->u.rel.r_offset)
4752 if (a->u.rel.r_offset > b->u.rel.r_offset)
4758 elf_link_sort_relocs (abfd, info, psec)
4760 struct bfd_link_info *info;
4763 bfd *dynobj = elf_hash_table (info)->dynobj;
4764 asection *reldyn, *o;
4765 boolean rel = false;
4766 bfd_size_type count, size;
4768 struct elf_link_sort_rela *rela;
4769 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4771 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
4772 if (reldyn == NULL || reldyn->_raw_size == 0)
4774 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
4775 if (reldyn == NULL || reldyn->_raw_size == 0)
4778 count = reldyn->_raw_size / sizeof (Elf_External_Rel);
4781 count = reldyn->_raw_size / sizeof (Elf_External_Rela);
4784 for (o = dynobj->sections; o != NULL; o = o->next)
4785 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4786 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4787 && o->output_section == reldyn)
4788 size += o->_raw_size;
4790 if (size != reldyn->_raw_size)
4793 rela = (struct elf_link_sort_rela *) bfd_zmalloc (sizeof (*rela) * count);
4796 (*info->callbacks->warning)
4797 (info, _("Not enough memory to sort relocations"), 0, abfd, 0,
4802 for (o = dynobj->sections; o != NULL; o = o->next)
4803 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4804 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4805 && o->output_section == reldyn)
4809 Elf_External_Rel *erel, *erelend;
4810 struct elf_link_sort_rela *s;
4812 erel = (Elf_External_Rel *) o->contents;
4813 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4814 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4815 for (; erel < erelend; erel++, s++)
4817 if (bed->s->swap_reloc_in)
4818 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, &s->u.rel);
4820 elf_swap_reloc_in (abfd, erel, &s->u.rel);
4822 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
4827 Elf_External_Rela *erela, *erelaend;
4828 struct elf_link_sort_rela *s;
4830 erela = (Elf_External_Rela *) o->contents;
4831 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4832 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4833 for (; erela < erelaend; erela++, s++)
4835 if (bed->s->swap_reloca_in)
4836 (*bed->s->swap_reloca_in) (dynobj, (bfd_byte *) erela,
4839 elf_swap_reloca_in (dynobj, erela, &s->u.rela);
4841 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
4846 qsort (rela, (size_t) count, sizeof (*rela), elf_link_sort_cmp1);
4847 for (ret = 0; ret < count && rela[ret].type == reloc_class_relative; ret++)
4849 for (i = ret, j = ret; i < count; i++)
4851 if (ELF_R_SYM (rela[i].u.rel.r_info) != ELF_R_SYM (rela[j].u.rel.r_info))
4853 rela[i].offset = rela[j].u.rel.r_offset;
4855 qsort (rela + ret, (size_t) count - ret, sizeof (*rela), elf_link_sort_cmp2);
4857 for (o = dynobj->sections; o != NULL; o = o->next)
4858 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4859 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4860 && o->output_section == reldyn)
4864 Elf_External_Rel *erel, *erelend;
4865 struct elf_link_sort_rela *s;
4867 erel = (Elf_External_Rel *) o->contents;
4868 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4869 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4870 for (; erel < erelend; erel++, s++)
4872 if (bed->s->swap_reloc_out)
4873 (*bed->s->swap_reloc_out) (abfd, &s->u.rel,
4876 elf_swap_reloc_out (abfd, &s->u.rel, erel);
4881 Elf_External_Rela *erela, *erelaend;
4882 struct elf_link_sort_rela *s;
4884 erela = (Elf_External_Rela *) o->contents;
4885 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4886 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4887 for (; erela < erelaend; erela++, s++)
4889 if (bed->s->swap_reloca_out)
4890 (*bed->s->swap_reloca_out) (dynobj, &s->u.rela,
4891 (bfd_byte *) erela);
4893 elf_swap_reloca_out (dynobj, &s->u.rela, erela);
4903 /* Do the final step of an ELF link. */
4906 elf_bfd_final_link (abfd, info)
4908 struct bfd_link_info *info;
4911 boolean emit_relocs;
4913 struct elf_final_link_info finfo;
4914 register asection *o;
4915 register struct bfd_link_order *p;
4917 bfd_size_type max_contents_size;
4918 bfd_size_type max_external_reloc_size;
4919 bfd_size_type max_internal_reloc_count;
4920 bfd_size_type max_sym_count;
4921 bfd_size_type max_sym_shndx_count;
4923 Elf_Internal_Sym elfsym;
4925 Elf_Internal_Shdr *symtab_hdr;
4926 Elf_Internal_Shdr *symstrtab_hdr;
4927 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4928 struct elf_outext_info eoinfo;
4930 size_t relativecount = 0;
4931 asection *reldyn = 0;
4934 if (! is_elf_hash_table (info))
4938 abfd->flags |= DYNAMIC;
4940 dynamic = elf_hash_table (info)->dynamic_sections_created;
4941 dynobj = elf_hash_table (info)->dynobj;
4943 emit_relocs = (info->relocateable
4944 || info->emitrelocations
4945 || bed->elf_backend_emit_relocs);
4948 finfo.output_bfd = abfd;
4949 finfo.symstrtab = elf_stringtab_init ();
4950 if (finfo.symstrtab == NULL)
4955 finfo.dynsym_sec = NULL;
4956 finfo.hash_sec = NULL;
4957 finfo.symver_sec = NULL;
4961 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
4962 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
4963 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
4964 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
4965 /* Note that it is OK if symver_sec is NULL. */
4968 finfo.contents = NULL;
4969 finfo.external_relocs = NULL;
4970 finfo.internal_relocs = NULL;
4971 finfo.external_syms = NULL;
4972 finfo.locsym_shndx = NULL;
4973 finfo.internal_syms = NULL;
4974 finfo.indices = NULL;
4975 finfo.sections = NULL;
4976 finfo.symbuf = NULL;
4977 finfo.symshndxbuf = NULL;
4978 finfo.symbuf_count = 0;
4979 finfo.first_tls_sec = NULL;
4980 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4981 if ((o->flags & SEC_THREAD_LOCAL) != 0
4982 && (o->flags & SEC_LOAD) != 0)
4984 finfo.first_tls_sec = o;
4988 /* Count up the number of relocations we will output for each output
4989 section, so that we know the sizes of the reloc sections. We
4990 also figure out some maximum sizes. */
4991 max_contents_size = 0;
4992 max_external_reloc_size = 0;
4993 max_internal_reloc_count = 0;
4995 max_sym_shndx_count = 0;
4997 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
5001 for (p = o->link_order_head; p != NULL; p = p->next)
5003 if (p->type == bfd_section_reloc_link_order
5004 || p->type == bfd_symbol_reloc_link_order)
5006 else if (p->type == bfd_indirect_link_order)
5010 sec = p->u.indirect.section;
5012 /* Mark all sections which are to be included in the
5013 link. This will normally be every section. We need
5014 to do this so that we can identify any sections which
5015 the linker has decided to not include. */
5016 sec->linker_mark = true;
5018 if (sec->flags & SEC_MERGE)
5021 if (info->relocateable || info->emitrelocations)
5022 o->reloc_count += sec->reloc_count;
5023 else if (bed->elf_backend_count_relocs)
5025 Elf_Internal_Rela * relocs;
5027 relocs = (NAME(_bfd_elf,link_read_relocs)
5028 (abfd, sec, (PTR) NULL,
5029 (Elf_Internal_Rela *) NULL, info->keep_memory));
5032 += (*bed->elf_backend_count_relocs) (sec, relocs);
5034 if (!info->keep_memory)
5038 if (sec->_raw_size > max_contents_size)
5039 max_contents_size = sec->_raw_size;
5040 if (sec->_cooked_size > max_contents_size)
5041 max_contents_size = sec->_cooked_size;
5043 /* We are interested in just local symbols, not all
5045 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
5046 && (sec->owner->flags & DYNAMIC) == 0)
5050 if (elf_bad_symtab (sec->owner))
5051 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
5052 / sizeof (Elf_External_Sym));
5054 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
5056 if (sym_count > max_sym_count)
5057 max_sym_count = sym_count;
5059 if (sym_count > max_sym_shndx_count
5060 && elf_symtab_shndx (sec->owner) != 0)
5061 max_sym_shndx_count = sym_count;
5063 if ((sec->flags & SEC_RELOC) != 0)
5067 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
5068 if (ext_size > max_external_reloc_size)
5069 max_external_reloc_size = ext_size;
5070 if (sec->reloc_count > max_internal_reloc_count)
5071 max_internal_reloc_count = sec->reloc_count;
5077 if (o->reloc_count > 0)
5078 o->flags |= SEC_RELOC;
5081 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5082 set it (this is probably a bug) and if it is set
5083 assign_section_numbers will create a reloc section. */
5084 o->flags &=~ SEC_RELOC;
5087 /* If the SEC_ALLOC flag is not set, force the section VMA to
5088 zero. This is done in elf_fake_sections as well, but forcing
5089 the VMA to 0 here will ensure that relocs against these
5090 sections are handled correctly. */
5091 if ((o->flags & SEC_ALLOC) == 0
5092 && ! o->user_set_vma)
5096 if (! info->relocateable && merged)
5097 elf_link_hash_traverse (elf_hash_table (info),
5098 elf_link_sec_merge_syms, (PTR) abfd);
5100 /* Figure out the file positions for everything but the symbol table
5101 and the relocs. We set symcount to force assign_section_numbers
5102 to create a symbol table. */
5103 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
5104 BFD_ASSERT (! abfd->output_has_begun);
5105 if (! _bfd_elf_compute_section_file_positions (abfd, info))
5108 /* Figure out how many relocations we will have in each section.
5109 Just using RELOC_COUNT isn't good enough since that doesn't
5110 maintain a separate value for REL vs. RELA relocations. */
5112 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5113 for (o = sub->sections; o != NULL; o = o->next)
5115 asection *output_section;
5117 if (! o->linker_mark)
5119 /* This section was omitted from the link. */
5123 output_section = o->output_section;
5125 if (output_section != NULL
5126 && (o->flags & SEC_RELOC) != 0)
5128 struct bfd_elf_section_data *esdi
5129 = elf_section_data (o);
5130 struct bfd_elf_section_data *esdo
5131 = elf_section_data (output_section);
5132 unsigned int *rel_count;
5133 unsigned int *rel_count2;
5134 bfd_size_type entsize;
5135 bfd_size_type entsize2;
5137 /* We must be careful to add the relocations from the
5138 input section to the right output count. */
5139 entsize = esdi->rel_hdr.sh_entsize;
5140 entsize2 = esdi->rel_hdr2 ? esdi->rel_hdr2->sh_entsize : 0;
5141 BFD_ASSERT ((entsize == sizeof (Elf_External_Rel)
5142 || entsize == sizeof (Elf_External_Rela))
5143 && entsize2 != entsize
5145 || entsize2 == sizeof (Elf_External_Rel)
5146 || entsize2 == sizeof (Elf_External_Rela)));
5147 if (entsize == esdo->rel_hdr.sh_entsize)
5149 rel_count = &esdo->rel_count;
5150 rel_count2 = &esdo->rel_count2;
5154 rel_count = &esdo->rel_count2;
5155 rel_count2 = &esdo->rel_count;
5158 *rel_count += NUM_SHDR_ENTRIES (& esdi->rel_hdr);
5160 *rel_count2 += NUM_SHDR_ENTRIES (esdi->rel_hdr2);
5161 output_section->flags |= SEC_RELOC;
5165 /* That created the reloc sections. Set their sizes, and assign
5166 them file positions, and allocate some buffers. */
5167 for (o = abfd->sections; o != NULL; o = o->next)
5169 if ((o->flags & SEC_RELOC) != 0)
5171 if (!elf_link_size_reloc_section (abfd,
5172 &elf_section_data (o)->rel_hdr,
5176 if (elf_section_data (o)->rel_hdr2
5177 && !elf_link_size_reloc_section (abfd,
5178 elf_section_data (o)->rel_hdr2,
5183 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5184 to count upwards while actually outputting the relocations. */
5185 elf_section_data (o)->rel_count = 0;
5186 elf_section_data (o)->rel_count2 = 0;
5189 _bfd_elf_assign_file_positions_for_relocs (abfd);
5191 /* We have now assigned file positions for all the sections except
5192 .symtab and .strtab. We start the .symtab section at the current
5193 file position, and write directly to it. We build the .strtab
5194 section in memory. */
5195 bfd_get_symcount (abfd) = 0;
5196 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5197 /* sh_name is set in prep_headers. */
5198 symtab_hdr->sh_type = SHT_SYMTAB;
5199 symtab_hdr->sh_flags = 0;
5200 symtab_hdr->sh_addr = 0;
5201 symtab_hdr->sh_size = 0;
5202 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
5203 /* sh_link is set in assign_section_numbers. */
5204 /* sh_info is set below. */
5205 /* sh_offset is set just below. */
5206 symtab_hdr->sh_addralign = bed->s->file_align;
5208 off = elf_tdata (abfd)->next_file_pos;
5209 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
5211 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5212 incorrect. We do not yet know the size of the .symtab section.
5213 We correct next_file_pos below, after we do know the size. */
5215 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5216 continuously seeking to the right position in the file. */
5217 if (! info->keep_memory || max_sym_count < 20)
5218 finfo.symbuf_size = 20;
5220 finfo.symbuf_size = max_sym_count;
5221 amt = finfo.symbuf_size;
5222 amt *= sizeof (Elf_External_Sym);
5223 finfo.symbuf = (Elf_External_Sym *) bfd_malloc (amt);
5224 if (finfo.symbuf == NULL)
5226 if (elf_numsections (abfd) > SHN_LORESERVE)
5228 amt = finfo.symbuf_size;
5229 amt *= sizeof (Elf_External_Sym_Shndx);
5230 finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5231 if (finfo.symshndxbuf == NULL)
5235 /* Start writing out the symbol table. The first symbol is always a
5237 if (info->strip != strip_all
5240 elfsym.st_value = 0;
5243 elfsym.st_other = 0;
5244 elfsym.st_shndx = SHN_UNDEF;
5245 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5246 &elfsym, bfd_und_section_ptr))
5251 /* Some standard ELF linkers do this, but we don't because it causes
5252 bootstrap comparison failures. */
5253 /* Output a file symbol for the output file as the second symbol.
5254 We output this even if we are discarding local symbols, although
5255 I'm not sure if this is correct. */
5256 elfsym.st_value = 0;
5258 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5259 elfsym.st_other = 0;
5260 elfsym.st_shndx = SHN_ABS;
5261 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
5262 &elfsym, bfd_abs_section_ptr))
5266 /* Output a symbol for each section. We output these even if we are
5267 discarding local symbols, since they are used for relocs. These
5268 symbols have no names. We store the index of each one in the
5269 index field of the section, so that we can find it again when
5270 outputting relocs. */
5271 if (info->strip != strip_all
5275 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5276 elfsym.st_other = 0;
5277 for (i = 1; i < elf_numsections (abfd); i++)
5279 o = section_from_elf_index (abfd, i);
5281 o->target_index = bfd_get_symcount (abfd);
5282 elfsym.st_shndx = i;
5283 if (info->relocateable || o == NULL)
5284 elfsym.st_value = 0;
5286 elfsym.st_value = o->vma;
5287 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5290 if (i == SHN_LORESERVE)
5291 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
5295 /* Allocate some memory to hold information read in from the input
5297 if (max_contents_size != 0)
5299 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
5300 if (finfo.contents == NULL)
5304 if (max_external_reloc_size != 0)
5306 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
5307 if (finfo.external_relocs == NULL)
5311 if (max_internal_reloc_count != 0)
5313 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
5314 amt *= sizeof (Elf_Internal_Rela);
5315 finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
5316 if (finfo.internal_relocs == NULL)
5320 if (max_sym_count != 0)
5322 amt = max_sym_count * sizeof (Elf_External_Sym);
5323 finfo.external_syms = (Elf_External_Sym *) bfd_malloc (amt);
5324 if (finfo.external_syms == NULL)
5327 amt = max_sym_count * sizeof (Elf_Internal_Sym);
5328 finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
5329 if (finfo.internal_syms == NULL)
5332 amt = max_sym_count * sizeof (long);
5333 finfo.indices = (long *) bfd_malloc (amt);
5334 if (finfo.indices == NULL)
5337 amt = max_sym_count * sizeof (asection *);
5338 finfo.sections = (asection **) bfd_malloc (amt);
5339 if (finfo.sections == NULL)
5343 if (max_sym_shndx_count != 0)
5345 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
5346 finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5347 if (finfo.locsym_shndx == NULL)
5351 if (finfo.first_tls_sec)
5353 unsigned int align = 0;
5354 bfd_vma base = finfo.first_tls_sec->vma, end = 0;
5357 for (sec = finfo.first_tls_sec;
5358 sec && (sec->flags & SEC_THREAD_LOCAL);
5361 bfd_vma size = sec->_raw_size;
5363 if (bfd_get_section_alignment (abfd, sec) > align)
5364 align = bfd_get_section_alignment (abfd, sec);
5365 if (sec->_raw_size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
5367 struct bfd_link_order *o;
5370 for (o = sec->link_order_head; o != NULL; o = o->next)
5371 if (size < o->offset + o->size)
5372 size = o->offset + o->size;
5374 end = sec->vma + size;
5376 elf_hash_table (info)->tls_segment
5377 = bfd_zalloc (abfd, sizeof (struct elf_link_tls_segment));
5378 if (elf_hash_table (info)->tls_segment == NULL)
5380 elf_hash_table (info)->tls_segment->start = base;
5381 elf_hash_table (info)->tls_segment->size = end - base;
5382 elf_hash_table (info)->tls_segment->align = align;
5385 /* Since ELF permits relocations to be against local symbols, we
5386 must have the local symbols available when we do the relocations.
5387 Since we would rather only read the local symbols once, and we
5388 would rather not keep them in memory, we handle all the
5389 relocations for a single input file at the same time.
5391 Unfortunately, there is no way to know the total number of local
5392 symbols until we have seen all of them, and the local symbol
5393 indices precede the global symbol indices. This means that when
5394 we are generating relocateable output, and we see a reloc against
5395 a global symbol, we can not know the symbol index until we have
5396 finished examining all the local symbols to see which ones we are
5397 going to output. To deal with this, we keep the relocations in
5398 memory, and don't output them until the end of the link. This is
5399 an unfortunate waste of memory, but I don't see a good way around
5400 it. Fortunately, it only happens when performing a relocateable
5401 link, which is not the common case. FIXME: If keep_memory is set
5402 we could write the relocs out and then read them again; I don't
5403 know how bad the memory loss will be. */
5405 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5406 sub->output_has_begun = false;
5407 for (o = abfd->sections; o != NULL; o = o->next)
5409 for (p = o->link_order_head; p != NULL; p = p->next)
5411 if (p->type == bfd_indirect_link_order
5412 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
5413 == bfd_target_elf_flavour)
5414 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
5416 if (! sub->output_has_begun)
5418 if (! elf_link_input_bfd (&finfo, sub))
5420 sub->output_has_begun = true;
5423 else if (p->type == bfd_section_reloc_link_order
5424 || p->type == bfd_symbol_reloc_link_order)
5426 if (! elf_reloc_link_order (abfd, info, o, p))
5431 if (! _bfd_default_link_order (abfd, info, o, p))
5437 /* Output any global symbols that got converted to local in a
5438 version script or due to symbol visibility. We do this in a
5439 separate step since ELF requires all local symbols to appear
5440 prior to any global symbols. FIXME: We should only do this if
5441 some global symbols were, in fact, converted to become local.
5442 FIXME: Will this work correctly with the Irix 5 linker? */
5443 eoinfo.failed = false;
5444 eoinfo.finfo = &finfo;
5445 eoinfo.localsyms = true;
5446 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5451 /* That wrote out all the local symbols. Finish up the symbol table
5452 with the global symbols. Even if we want to strip everything we
5453 can, we still need to deal with those global symbols that got
5454 converted to local in a version script. */
5456 /* The sh_info field records the index of the first non local symbol. */
5457 symtab_hdr->sh_info = bfd_get_symcount (abfd);
5460 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
5462 Elf_Internal_Sym sym;
5463 Elf_External_Sym *dynsym =
5464 (Elf_External_Sym *) finfo.dynsym_sec->contents;
5465 long last_local = 0;
5467 /* Write out the section symbols for the output sections. */
5474 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5477 for (s = abfd->sections; s != NULL; s = s->next)
5480 Elf_External_Sym *dest;
5482 indx = elf_section_data (s)->this_idx;
5483 BFD_ASSERT (indx > 0);
5484 sym.st_shndx = indx;
5485 sym.st_value = s->vma;
5486 dest = dynsym + elf_section_data (s)->dynindx;
5487 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
5490 last_local = bfd_count_sections (abfd);
5493 /* Write out the local dynsyms. */
5494 if (elf_hash_table (info)->dynlocal)
5496 struct elf_link_local_dynamic_entry *e;
5497 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
5500 Elf_External_Sym *dest;
5502 sym.st_size = e->isym.st_size;
5503 sym.st_other = e->isym.st_other;
5505 /* Copy the internal symbol as is.
5506 Note that we saved a word of storage and overwrote
5507 the original st_name with the dynstr_index. */
5510 if (e->isym.st_shndx != SHN_UNDEF
5511 && (e->isym.st_shndx < SHN_LORESERVE
5512 || e->isym.st_shndx > SHN_HIRESERVE))
5514 s = bfd_section_from_elf_index (e->input_bfd,
5518 elf_section_data (s->output_section)->this_idx;
5519 sym.st_value = (s->output_section->vma
5521 + e->isym.st_value);
5524 if (last_local < e->dynindx)
5525 last_local = e->dynindx;
5527 dest = dynsym + e->dynindx;
5528 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
5532 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
5536 /* We get the global symbols from the hash table. */
5537 eoinfo.failed = false;
5538 eoinfo.localsyms = false;
5539 eoinfo.finfo = &finfo;
5540 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5545 /* If backend needs to output some symbols not present in the hash
5546 table, do it now. */
5547 if (bed->elf_backend_output_arch_syms)
5549 typedef boolean (*out_sym_func) PARAMS ((PTR, const char *,
5553 if (! ((*bed->elf_backend_output_arch_syms)
5554 (abfd, info, (PTR) &finfo, (out_sym_func) elf_link_output_sym)))
5558 /* Flush all symbols to the file. */
5559 if (! elf_link_flush_output_syms (&finfo))
5562 /* Now we know the size of the symtab section. */
5563 off += symtab_hdr->sh_size;
5565 /* Finish up and write out the symbol string table (.strtab)
5567 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5568 /* sh_name was set in prep_headers. */
5569 symstrtab_hdr->sh_type = SHT_STRTAB;
5570 symstrtab_hdr->sh_flags = 0;
5571 symstrtab_hdr->sh_addr = 0;
5572 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
5573 symstrtab_hdr->sh_entsize = 0;
5574 symstrtab_hdr->sh_link = 0;
5575 symstrtab_hdr->sh_info = 0;
5576 /* sh_offset is set just below. */
5577 symstrtab_hdr->sh_addralign = 1;
5579 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true);
5580 elf_tdata (abfd)->next_file_pos = off;
5582 if (bfd_get_symcount (abfd) > 0)
5584 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
5585 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
5589 /* Adjust the relocs to have the correct symbol indices. */
5590 for (o = abfd->sections; o != NULL; o = o->next)
5592 if ((o->flags & SEC_RELOC) == 0)
5595 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
5596 elf_section_data (o)->rel_count,
5597 elf_section_data (o)->rel_hashes);
5598 if (elf_section_data (o)->rel_hdr2 != NULL)
5599 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
5600 elf_section_data (o)->rel_count2,
5601 (elf_section_data (o)->rel_hashes
5602 + elf_section_data (o)->rel_count));
5604 /* Set the reloc_count field to 0 to prevent write_relocs from
5605 trying to swap the relocs out itself. */
5609 if (dynamic && info->combreloc && dynobj != NULL)
5610 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
5612 /* If we are linking against a dynamic object, or generating a
5613 shared library, finish up the dynamic linking information. */
5616 Elf_External_Dyn *dyncon, *dynconend;
5618 /* Fix up .dynamic entries. */
5619 o = bfd_get_section_by_name (dynobj, ".dynamic");
5620 BFD_ASSERT (o != NULL);
5622 dyncon = (Elf_External_Dyn *) o->contents;
5623 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
5624 for (; dyncon < dynconend; dyncon++)
5626 Elf_Internal_Dyn dyn;
5630 elf_swap_dyn_in (dynobj, dyncon, &dyn);
5637 if (relativecount > 0 && dyncon + 1 < dynconend)
5639 switch (elf_section_data (reldyn)->this_hdr.sh_type)
5641 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
5642 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
5645 if (dyn.d_tag != DT_NULL)
5647 dyn.d_un.d_val = relativecount;
5648 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5654 name = info->init_function;
5657 name = info->fini_function;
5660 struct elf_link_hash_entry *h;
5662 h = elf_link_hash_lookup (elf_hash_table (info), name,
5663 false, false, true);
5665 && (h->root.type == bfd_link_hash_defined
5666 || h->root.type == bfd_link_hash_defweak))
5668 dyn.d_un.d_val = h->root.u.def.value;
5669 o = h->root.u.def.section;
5670 if (o->output_section != NULL)
5671 dyn.d_un.d_val += (o->output_section->vma
5672 + o->output_offset);
5675 /* The symbol is imported from another shared
5676 library and does not apply to this one. */
5680 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5685 case DT_PREINIT_ARRAYSZ:
5686 name = ".preinit_array";
5688 case DT_INIT_ARRAYSZ:
5689 name = ".init_array";
5691 case DT_FINI_ARRAYSZ:
5692 name = ".fini_array";
5694 o = bfd_get_section_by_name (abfd, name);
5697 (*_bfd_error_handler)
5698 (_("%s: could not find output section %s"),
5699 bfd_get_filename (abfd), name);
5702 if (o->_raw_size == 0)
5703 (*_bfd_error_handler)
5704 (_("warning: %s section has zero size"), name);
5705 dyn.d_un.d_val = o->_raw_size;
5706 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5709 case DT_PREINIT_ARRAY:
5710 name = ".preinit_array";
5713 name = ".init_array";
5716 name = ".fini_array";
5729 name = ".gnu.version_d";
5732 name = ".gnu.version_r";
5735 name = ".gnu.version";
5737 o = bfd_get_section_by_name (abfd, name);
5740 (*_bfd_error_handler)
5741 (_("%s: could not find output section %s"),
5742 bfd_get_filename (abfd), name);
5745 dyn.d_un.d_ptr = o->vma;
5746 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5753 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5758 for (i = 1; i < elf_numsections (abfd); i++)
5760 Elf_Internal_Shdr *hdr;
5762 hdr = elf_elfsections (abfd)[i];
5763 if (hdr->sh_type == type
5764 && (hdr->sh_flags & SHF_ALLOC) != 0)
5766 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
5767 dyn.d_un.d_val += hdr->sh_size;
5770 if (dyn.d_un.d_val == 0
5771 || hdr->sh_addr < dyn.d_un.d_val)
5772 dyn.d_un.d_val = hdr->sh_addr;
5776 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5782 /* If we have created any dynamic sections, then output them. */
5785 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
5788 for (o = dynobj->sections; o != NULL; o = o->next)
5790 if ((o->flags & SEC_HAS_CONTENTS) == 0
5791 || o->_raw_size == 0
5792 || o->output_section == bfd_abs_section_ptr)
5794 if ((o->flags & SEC_LINKER_CREATED) == 0)
5796 /* At this point, we are only interested in sections
5797 created by elf_link_create_dynamic_sections. */
5800 if ((elf_section_data (o->output_section)->this_hdr.sh_type
5802 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
5804 if (! bfd_set_section_contents (abfd, o->output_section,
5806 (file_ptr) o->output_offset,
5812 /* The contents of the .dynstr section are actually in a
5814 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
5815 if (bfd_seek (abfd, off, SEEK_SET) != 0
5816 || ! _bfd_elf_strtab_emit (abfd,
5817 elf_hash_table (info)->dynstr))
5823 if (info->relocateable)
5825 boolean failed = false;
5827 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
5832 /* If we have optimized stabs strings, output them. */
5833 if (elf_hash_table (info)->stab_info != NULL)
5835 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
5839 if (info->eh_frame_hdr && elf_hash_table (info)->dynobj)
5841 o = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
5844 && (elf_section_data (o)->sec_info_type
5845 == ELF_INFO_TYPE_EH_FRAME_HDR))
5847 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, o))
5852 if (finfo.symstrtab != NULL)
5853 _bfd_stringtab_free (finfo.symstrtab);
5854 if (finfo.contents != NULL)
5855 free (finfo.contents);
5856 if (finfo.external_relocs != NULL)
5857 free (finfo.external_relocs);
5858 if (finfo.internal_relocs != NULL)
5859 free (finfo.internal_relocs);
5860 if (finfo.external_syms != NULL)
5861 free (finfo.external_syms);
5862 if (finfo.locsym_shndx != NULL)
5863 free (finfo.locsym_shndx);
5864 if (finfo.internal_syms != NULL)
5865 free (finfo.internal_syms);
5866 if (finfo.indices != NULL)
5867 free (finfo.indices);
5868 if (finfo.sections != NULL)
5869 free (finfo.sections);
5870 if (finfo.symbuf != NULL)
5871 free (finfo.symbuf);
5872 if (finfo.symshndxbuf != NULL)
5873 free (finfo.symbuf);
5874 for (o = abfd->sections; o != NULL; o = o->next)
5876 if ((o->flags & SEC_RELOC) != 0
5877 && elf_section_data (o)->rel_hashes != NULL)
5878 free (elf_section_data (o)->rel_hashes);
5881 elf_tdata (abfd)->linker = true;
5886 if (finfo.symstrtab != NULL)
5887 _bfd_stringtab_free (finfo.symstrtab);
5888 if (finfo.contents != NULL)
5889 free (finfo.contents);
5890 if (finfo.external_relocs != NULL)
5891 free (finfo.external_relocs);
5892 if (finfo.internal_relocs != NULL)
5893 free (finfo.internal_relocs);
5894 if (finfo.external_syms != NULL)
5895 free (finfo.external_syms);
5896 if (finfo.locsym_shndx != NULL)
5897 free (finfo.locsym_shndx);
5898 if (finfo.internal_syms != NULL)
5899 free (finfo.internal_syms);
5900 if (finfo.indices != NULL)
5901 free (finfo.indices);
5902 if (finfo.sections != NULL)
5903 free (finfo.sections);
5904 if (finfo.symbuf != NULL)
5905 free (finfo.symbuf);
5906 if (finfo.symshndxbuf != NULL)
5907 free (finfo.symbuf);
5908 for (o = abfd->sections; o != NULL; o = o->next)
5910 if ((o->flags & SEC_RELOC) != 0
5911 && elf_section_data (o)->rel_hashes != NULL)
5912 free (elf_section_data (o)->rel_hashes);
5918 /* Add a symbol to the output symbol table. */
5921 elf_link_output_sym (finfo, name, elfsym, input_sec)
5922 struct elf_final_link_info *finfo;
5924 Elf_Internal_Sym *elfsym;
5925 asection *input_sec;
5927 Elf_External_Sym *dest;
5928 Elf_External_Sym_Shndx *destshndx;
5930 boolean (*output_symbol_hook) PARAMS ((bfd *,
5931 struct bfd_link_info *info,
5936 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
5937 elf_backend_link_output_symbol_hook;
5938 if (output_symbol_hook != NULL)
5940 if (! ((*output_symbol_hook)
5941 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
5945 if (name == (const char *) NULL || *name == '\0')
5946 elfsym->st_name = 0;
5947 else if (input_sec->flags & SEC_EXCLUDE)
5948 elfsym->st_name = 0;
5951 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5953 if (elfsym->st_name == (unsigned long) -1)
5957 if (finfo->symbuf_count >= finfo->symbuf_size)
5959 if (! elf_link_flush_output_syms (finfo))
5963 dest = finfo->symbuf + finfo->symbuf_count;
5964 destshndx = finfo->symshndxbuf;
5965 if (destshndx != NULL)
5966 destshndx += finfo->symbuf_count;
5967 elf_swap_symbol_out (finfo->output_bfd, elfsym, (PTR) dest, (PTR) destshndx);
5968 ++finfo->symbuf_count;
5970 ++ bfd_get_symcount (finfo->output_bfd);
5975 /* Flush the output symbols to the file. */
5978 elf_link_flush_output_syms (finfo)
5979 struct elf_final_link_info *finfo;
5981 if (finfo->symbuf_count > 0)
5983 Elf_Internal_Shdr *hdr;
5987 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5988 pos = hdr->sh_offset + hdr->sh_size;
5989 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
5990 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5991 || bfd_bwrite ((PTR) finfo->symbuf, amt, finfo->output_bfd) != amt)
5994 hdr->sh_size += amt;
5996 if (finfo->symshndxbuf != NULL)
5998 hdr = &elf_tdata (finfo->output_bfd)->symtab_shndx_hdr;
5999 pos = hdr->sh_offset + hdr->sh_size;
6000 amt = finfo->symbuf_count * sizeof (Elf_External_Sym_Shndx);
6001 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6002 || (bfd_bwrite ((PTR) finfo->symshndxbuf, amt, finfo->output_bfd)
6006 hdr->sh_size += amt;
6009 finfo->symbuf_count = 0;
6015 /* Adjust all external symbols pointing into SEC_MERGE sections
6016 to reflect the object merging within the sections. */
6019 elf_link_sec_merge_syms (h, data)
6020 struct elf_link_hash_entry *h;
6025 if (h->root.type == bfd_link_hash_warning)
6026 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6028 if ((h->root.type == bfd_link_hash_defined
6029 || h->root.type == bfd_link_hash_defweak)
6030 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
6031 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
6033 bfd *output_bfd = (bfd *) data;
6035 h->root.u.def.value =
6036 _bfd_merged_section_offset (output_bfd,
6037 &h->root.u.def.section,
6038 elf_section_data (sec)->sec_info,
6039 h->root.u.def.value, (bfd_vma) 0);
6045 /* Add an external symbol to the symbol table. This is called from
6046 the hash table traversal routine. When generating a shared object,
6047 we go through the symbol table twice. The first time we output
6048 anything that might have been forced to local scope in a version
6049 script. The second time we output the symbols that are still
6053 elf_link_output_extsym (h, data)
6054 struct elf_link_hash_entry *h;
6057 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
6058 struct elf_final_link_info *finfo = eoinfo->finfo;
6060 Elf_Internal_Sym sym;
6061 asection *input_sec;
6063 if (h->root.type == bfd_link_hash_warning)
6065 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6066 if (h->root.type == bfd_link_hash_new)
6070 /* Decide whether to output this symbol in this pass. */
6071 if (eoinfo->localsyms)
6073 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6078 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6082 /* If we are not creating a shared library, and this symbol is
6083 referenced by a shared library but is not defined anywhere, then
6084 warn that it is undefined. If we do not do this, the runtime
6085 linker will complain that the symbol is undefined when the
6086 program is run. We don't have to worry about symbols that are
6087 referenced by regular files, because we will already have issued
6088 warnings for them. */
6089 if (! finfo->info->relocateable
6090 && ! finfo->info->allow_shlib_undefined
6091 && ! finfo->info->shared
6092 && h->root.type == bfd_link_hash_undefined
6093 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
6094 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
6096 if (! ((*finfo->info->callbacks->undefined_symbol)
6097 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6098 (asection *) NULL, (bfd_vma) 0, true)))
6100 eoinfo->failed = true;
6105 /* We don't want to output symbols that have never been mentioned by
6106 a regular file, or that we have been told to strip. However, if
6107 h->indx is set to -2, the symbol is used by a reloc and we must
6111 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6112 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
6113 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
6114 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
6116 else if (finfo->info->strip == strip_all
6117 || (finfo->info->strip == strip_some
6118 && bfd_hash_lookup (finfo->info->keep_hash,
6119 h->root.root.string,
6120 false, false) == NULL))
6125 /* If we're stripping it, and it's not a dynamic symbol, there's
6126 nothing else to do unless it is a forced local symbol. */
6129 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6133 sym.st_size = h->size;
6134 sym.st_other = h->other;
6135 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6136 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6137 else if (h->root.type == bfd_link_hash_undefweak
6138 || h->root.type == bfd_link_hash_defweak)
6139 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6141 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6143 switch (h->root.type)
6146 case bfd_link_hash_new:
6147 case bfd_link_hash_warning:
6151 case bfd_link_hash_undefined:
6152 case bfd_link_hash_undefweak:
6153 input_sec = bfd_und_section_ptr;
6154 sym.st_shndx = SHN_UNDEF;
6157 case bfd_link_hash_defined:
6158 case bfd_link_hash_defweak:
6160 input_sec = h->root.u.def.section;
6161 if (input_sec->output_section != NULL)
6164 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6165 input_sec->output_section);
6166 if (sym.st_shndx == SHN_BAD)
6168 (*_bfd_error_handler)
6169 (_("%s: could not find output section %s for input section %s"),
6170 bfd_get_filename (finfo->output_bfd),
6171 input_sec->output_section->name,
6173 eoinfo->failed = true;
6177 /* ELF symbols in relocateable files are section relative,
6178 but in nonrelocateable files they are virtual
6180 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6181 if (! finfo->info->relocateable)
6183 sym.st_value += input_sec->output_section->vma;
6184 if (h->type == STT_TLS)
6186 /* STT_TLS symbols are relative to PT_TLS segment
6188 BFD_ASSERT (finfo->first_tls_sec != NULL);
6189 sym.st_value -= finfo->first_tls_sec->vma;
6195 BFD_ASSERT (input_sec->owner == NULL
6196 || (input_sec->owner->flags & DYNAMIC) != 0);
6197 sym.st_shndx = SHN_UNDEF;
6198 input_sec = bfd_und_section_ptr;
6203 case bfd_link_hash_common:
6204 input_sec = h->root.u.c.p->section;
6205 sym.st_shndx = SHN_COMMON;
6206 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6209 case bfd_link_hash_indirect:
6210 /* These symbols are created by symbol versioning. They point
6211 to the decorated version of the name. For example, if the
6212 symbol foo@@GNU_1.2 is the default, which should be used when
6213 foo is used with no version, then we add an indirect symbol
6214 foo which points to foo@@GNU_1.2. We ignore these symbols,
6215 since the indirected symbol is already in the hash table. */
6219 /* Give the processor backend a chance to tweak the symbol value,
6220 and also to finish up anything that needs to be done for this
6221 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6222 forced local syms when non-shared is due to a historical quirk. */
6223 if ((h->dynindx != -1
6224 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6225 && (finfo->info->shared
6226 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6227 && elf_hash_table (finfo->info)->dynamic_sections_created)
6229 struct elf_backend_data *bed;
6231 bed = get_elf_backend_data (finfo->output_bfd);
6232 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6233 (finfo->output_bfd, finfo->info, h, &sym)))
6235 eoinfo->failed = true;
6240 /* If we are marking the symbol as undefined, and there are no
6241 non-weak references to this symbol from a regular object, then
6242 mark the symbol as weak undefined; if there are non-weak
6243 references, mark the symbol as strong. We can't do this earlier,
6244 because it might not be marked as undefined until the
6245 finish_dynamic_symbol routine gets through with it. */
6246 if (sym.st_shndx == SHN_UNDEF
6247 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
6248 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6249 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6253 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
6254 bindtype = STB_GLOBAL;
6256 bindtype = STB_WEAK;
6257 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6260 /* If a symbol is not defined locally, we clear the visibility
6262 if (! finfo->info->relocateable
6263 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6264 sym.st_other ^= ELF_ST_VISIBILITY (sym.st_other);
6266 /* If this symbol should be put in the .dynsym section, then put it
6267 there now. We have already know the symbol index. We also fill
6268 in the entry in the .hash section. */
6269 if (h->dynindx != -1
6270 && elf_hash_table (finfo->info)->dynamic_sections_created)
6274 size_t hash_entry_size;
6275 bfd_byte *bucketpos;
6277 Elf_External_Sym *esym;
6279 sym.st_name = h->dynstr_index;
6280 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
6281 elf_swap_symbol_out (finfo->output_bfd, &sym, (PTR) esym, (PTR) 0);
6283 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6284 bucket = h->elf_hash_value % bucketcount;
6286 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6287 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6288 + (bucket + 2) * hash_entry_size);
6289 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6290 bfd_put (8 * hash_entry_size, finfo->output_bfd, (bfd_vma) h->dynindx,
6292 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6293 ((bfd_byte *) finfo->hash_sec->contents
6294 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6296 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6298 Elf_Internal_Versym iversym;
6299 Elf_External_Versym *eversym;
6301 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6303 if (h->verinfo.verdef == NULL)
6304 iversym.vs_vers = 0;
6306 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6310 if (h->verinfo.vertree == NULL)
6311 iversym.vs_vers = 1;
6313 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6316 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
6317 iversym.vs_vers |= VERSYM_HIDDEN;
6319 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6320 eversym += h->dynindx;
6321 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6325 /* If we're stripping it, then it was just a dynamic symbol, and
6326 there's nothing else to do. */
6330 h->indx = bfd_get_symcount (finfo->output_bfd);
6332 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
6334 eoinfo->failed = true;
6341 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6342 originated from the section given by INPUT_REL_HDR) to the
6346 elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
6349 asection *input_section;
6350 Elf_Internal_Shdr *input_rel_hdr;
6351 Elf_Internal_Rela *internal_relocs;
6353 Elf_Internal_Rela *irela;
6354 Elf_Internal_Rela *irelaend;
6355 Elf_Internal_Shdr *output_rel_hdr;
6356 asection *output_section;
6357 unsigned int *rel_countp = NULL;
6358 struct elf_backend_data *bed;
6361 output_section = input_section->output_section;
6362 output_rel_hdr = NULL;
6364 if (elf_section_data (output_section)->rel_hdr.sh_entsize
6365 == input_rel_hdr->sh_entsize)
6367 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
6368 rel_countp = &elf_section_data (output_section)->rel_count;
6370 else if (elf_section_data (output_section)->rel_hdr2
6371 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
6372 == input_rel_hdr->sh_entsize))
6374 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
6375 rel_countp = &elf_section_data (output_section)->rel_count2;
6379 (*_bfd_error_handler) (
6380 _("%s: relocation size mismatch in %s section %s"),
6381 bfd_get_filename (output_bfd),
6382 bfd_archive_filename (input_section->owner),
6383 input_section->name);
6384 bfd_set_error (bfd_error_wrong_object_format);
6388 bed = get_elf_backend_data (output_bfd);
6389 irela = internal_relocs;
6390 irelaend = irela + NUM_SHDR_ENTRIES (input_rel_hdr)
6391 * bed->s->int_rels_per_ext_rel;
6393 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
6395 Elf_External_Rel *erel;
6396 Elf_Internal_Rel *irel;
6398 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
6399 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
6402 (*_bfd_error_handler) (_("Error: out of memory"));
6406 erel = ((Elf_External_Rel *) output_rel_hdr->contents + *rel_countp);
6407 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erel++)
6411 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6413 irel[i].r_offset = irela[i].r_offset;
6414 irel[i].r_info = irela[i].r_info;
6415 BFD_ASSERT (irela[i].r_addend == 0);
6418 if (bed->s->swap_reloc_out)
6419 (*bed->s->swap_reloc_out) (output_bfd, irel, (PTR) erel);
6421 elf_swap_reloc_out (output_bfd, irel, erel);
6428 Elf_External_Rela *erela;
6430 BFD_ASSERT (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rela));
6432 erela = ((Elf_External_Rela *) output_rel_hdr->contents + *rel_countp);
6433 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erela++)
6434 if (bed->s->swap_reloca_out)
6435 (*bed->s->swap_reloca_out) (output_bfd, irela, (PTR) erela);
6437 elf_swap_reloca_out (output_bfd, irela, erela);
6440 /* Bump the counter, so that we know where to add the next set of
6442 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
6447 /* Link an input file into the linker output file. This function
6448 handles all the sections and relocations of the input file at once.
6449 This is so that we only have to read the local symbols once, and
6450 don't have to keep them in memory. */
6453 elf_link_input_bfd (finfo, input_bfd)
6454 struct elf_final_link_info *finfo;
6457 boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
6458 bfd *, asection *, bfd_byte *,
6459 Elf_Internal_Rela *,
6460 Elf_Internal_Sym *, asection **));
6462 Elf_Internal_Shdr *symtab_hdr;
6463 Elf_Internal_Shdr *shndx_hdr;
6466 Elf_External_Sym *external_syms;
6467 Elf_External_Sym *esym;
6468 Elf_External_Sym *esymend;
6469 Elf_External_Sym_Shndx *shndx_buf;
6470 Elf_External_Sym_Shndx *shndx;
6471 Elf_Internal_Sym *isym;
6473 asection **ppsection;
6475 struct elf_backend_data *bed;
6476 boolean emit_relocs;
6477 struct elf_link_hash_entry **sym_hashes;
6479 output_bfd = finfo->output_bfd;
6480 bed = get_elf_backend_data (output_bfd);
6481 relocate_section = bed->elf_backend_relocate_section;
6483 /* If this is a dynamic object, we don't want to do anything here:
6484 we don't want the local symbols, and we don't want the section
6486 if ((input_bfd->flags & DYNAMIC) != 0)
6489 emit_relocs = (finfo->info->relocateable
6490 || finfo->info->emitrelocations
6491 || bed->elf_backend_emit_relocs);
6493 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6494 if (elf_bad_symtab (input_bfd))
6496 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6501 locsymcount = symtab_hdr->sh_info;
6502 extsymoff = symtab_hdr->sh_info;
6505 /* Read the local symbols. */
6506 if (symtab_hdr->contents != NULL)
6507 external_syms = (Elf_External_Sym *) symtab_hdr->contents;
6508 else if (locsymcount == 0)
6509 external_syms = NULL;
6512 bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym);
6513 external_syms = finfo->external_syms;
6514 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
6515 || bfd_bread (external_syms, amt, input_bfd) != amt)
6519 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
6521 if (shndx_hdr->sh_size != 0 && locsymcount != 0)
6523 bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym_Shndx);
6524 shndx_buf = finfo->locsym_shndx;
6525 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
6526 || bfd_bread (shndx_buf, amt, input_bfd) != amt)
6530 /* Swap in the local symbols and write out the ones which we know
6531 are going into the output file. */
6532 for (esym = external_syms, esymend = esym + locsymcount,
6533 isym = finfo->internal_syms, pindex = finfo->indices,
6534 ppsection = finfo->sections, shndx = shndx_buf;
6536 esym++, isym++, pindex++, ppsection++,
6537 shndx = (shndx != NULL ? shndx + 1 : NULL))
6541 Elf_Internal_Sym osym;
6543 elf_swap_symbol_in (input_bfd, (const PTR) esym, (const PTR) shndx,
6547 if (elf_bad_symtab (input_bfd))
6549 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6556 if (isym->st_shndx == SHN_UNDEF)
6557 isec = bfd_und_section_ptr;
6558 else if (isym->st_shndx < SHN_LORESERVE
6559 || isym->st_shndx > SHN_HIRESERVE)
6561 isec = section_from_elf_index (input_bfd, isym->st_shndx);
6563 && elf_section_data (isec)->sec_info_type == ELF_INFO_TYPE_MERGE
6564 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6566 _bfd_merged_section_offset (output_bfd, &isec,
6567 elf_section_data (isec)->sec_info,
6568 isym->st_value, (bfd_vma) 0);
6570 else if (isym->st_shndx == SHN_ABS)
6571 isec = bfd_abs_section_ptr;
6572 else if (isym->st_shndx == SHN_COMMON)
6573 isec = bfd_com_section_ptr;
6582 /* Don't output the first, undefined, symbol. */
6583 if (esym == external_syms)
6586 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6588 /* We never output section symbols. Instead, we use the
6589 section symbol of the corresponding section in the output
6594 /* If we are stripping all symbols, we don't want to output this
6596 if (finfo->info->strip == strip_all)
6599 /* If we are discarding all local symbols, we don't want to
6600 output this one. If we are generating a relocateable output
6601 file, then some of the local symbols may be required by
6602 relocs; we output them below as we discover that they are
6604 if (finfo->info->discard == discard_all)
6607 /* If this symbol is defined in a section which we are
6608 discarding, we don't need to keep it, but note that
6609 linker_mark is only reliable for sections that have contents.
6610 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6611 as well as linker_mark. */
6612 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6614 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6615 || (! finfo->info->relocateable
6616 && (isec->flags & SEC_EXCLUDE) != 0)))
6619 /* Get the name of the symbol. */
6620 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6625 /* See if we are discarding symbols with this name. */
6626 if ((finfo->info->strip == strip_some
6627 && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
6629 || (((finfo->info->discard == discard_sec_merge
6630 && (isec->flags & SEC_MERGE) && ! finfo->info->relocateable)
6631 || finfo->info->discard == discard_l)
6632 && bfd_is_local_label_name (input_bfd, name)))
6635 /* If we get here, we are going to output this symbol. */
6639 /* Adjust the section index for the output file. */
6640 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6641 isec->output_section);
6642 if (osym.st_shndx == SHN_BAD)
6645 *pindex = bfd_get_symcount (output_bfd);
6647 /* ELF symbols in relocateable files are section relative, but
6648 in executable files they are virtual addresses. Note that
6649 this code assumes that all ELF sections have an associated
6650 BFD section with a reasonable value for output_offset; below
6651 we assume that they also have a reasonable value for
6652 output_section. Any special sections must be set up to meet
6653 these requirements. */
6654 osym.st_value += isec->output_offset;
6655 if (! finfo->info->relocateable)
6657 osym.st_value += isec->output_section->vma;
6658 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6660 /* STT_TLS symbols are relative to PT_TLS segment base. */
6661 BFD_ASSERT (finfo->first_tls_sec != NULL);
6662 osym.st_value -= finfo->first_tls_sec->vma;
6666 if (! elf_link_output_sym (finfo, name, &osym, isec))
6670 /* Relocate the contents of each section. */
6671 sym_hashes = elf_sym_hashes (input_bfd);
6672 for (o = input_bfd->sections; o != NULL; o = o->next)
6676 if (! o->linker_mark)
6678 /* This section was omitted from the link. */
6682 if ((o->flags & SEC_HAS_CONTENTS) == 0
6683 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
6686 if ((o->flags & SEC_LINKER_CREATED) != 0)
6688 /* Section was created by elf_link_create_dynamic_sections
6693 /* Get the contents of the section. They have been cached by a
6694 relaxation routine. Note that o is a section in an input
6695 file, so the contents field will not have been set by any of
6696 the routines which work on output files. */
6697 if (elf_section_data (o)->this_hdr.contents != NULL)
6698 contents = elf_section_data (o)->this_hdr.contents;
6701 contents = finfo->contents;
6702 if (! bfd_get_section_contents (input_bfd, o, contents,
6703 (file_ptr) 0, o->_raw_size))
6707 if ((o->flags & SEC_RELOC) != 0)
6709 Elf_Internal_Rela *internal_relocs;
6711 /* Get the swapped relocs. */
6712 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
6713 (input_bfd, o, finfo->external_relocs,
6714 finfo->internal_relocs, false));
6715 if (internal_relocs == NULL
6716 && o->reloc_count > 0)
6719 /* Run through the relocs looking for any against symbols
6720 from discarded sections and section symbols from
6721 removed link-once sections. Complain about relocs
6722 against discarded sections. Zero relocs against removed
6723 link-once sections. We should really complain if
6724 anything in the final link tries to use it, but
6725 DWARF-based exception handling might have an entry in
6726 .eh_frame to describe a routine in the linkonce section,
6727 and it turns out to be hard to remove the .eh_frame
6728 entry too. FIXME. */
6729 if (!finfo->info->relocateable
6730 && !elf_section_ignore_discarded_relocs (o))
6732 Elf_Internal_Rela *rel, *relend;
6734 rel = internal_relocs;
6735 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6736 for ( ; rel < relend; rel++)
6738 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
6740 if (r_symndx >= locsymcount
6741 || (elf_bad_symtab (input_bfd)
6742 && finfo->sections[r_symndx] == NULL))
6744 struct elf_link_hash_entry *h;
6746 h = sym_hashes[r_symndx - extsymoff];
6747 while (h->root.type == bfd_link_hash_indirect
6748 || h->root.type == bfd_link_hash_warning)
6749 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6751 /* Complain if the definition comes from a
6752 discarded section. */
6753 if ((h->root.type == bfd_link_hash_defined
6754 || h->root.type == bfd_link_hash_defweak)
6755 && elf_discarded_section (h->root.u.def.section))
6757 #if BFD_VERSION_DATE < 20031005
6758 if ((o->flags & SEC_DEBUGGING) != 0)
6760 #if BFD_VERSION_DATE > 20021005
6761 (*finfo->info->callbacks->warning)
6763 _("warning: relocation against removed section; zeroing"),
6764 NULL, input_bfd, o, rel->r_offset);
6766 BFD_ASSERT (r_symndx != 0);
6767 memset (rel, 0, sizeof (*rel));
6772 if (! ((*finfo->info->callbacks->undefined_symbol)
6773 (finfo->info, h->root.root.string,
6774 input_bfd, o, rel->r_offset,
6782 asection *sec = finfo->sections[r_symndx];
6784 if (sec != NULL && elf_discarded_section (sec))
6786 #if BFD_VERSION_DATE < 20031005
6787 if ((o->flags & SEC_DEBUGGING) != 0
6788 || (sec->flags & SEC_LINK_ONCE) != 0)
6790 #if BFD_VERSION_DATE > 20021005
6791 (*finfo->info->callbacks->warning)
6793 _("warning: relocation against removed section"),
6794 NULL, input_bfd, o, rel->r_offset);
6796 BFD_ASSERT (r_symndx != 0);
6798 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
6806 = _("local symbols in discarded section %s");
6808 = strlen (sec->name) + strlen (msg) - 1;
6809 char *buf = (char *) bfd_malloc (amt);
6812 sprintf (buf, msg, sec->name);
6814 buf = (char *) sec->name;
6815 ok = (*finfo->info->callbacks
6816 ->undefined_symbol) (finfo->info, buf,
6820 if (buf != sec->name)
6830 /* Relocate the section by invoking a back end routine.
6832 The back end routine is responsible for adjusting the
6833 section contents as necessary, and (if using Rela relocs
6834 and generating a relocateable output file) adjusting the
6835 reloc addend as necessary.
6837 The back end routine does not have to worry about setting
6838 the reloc address or the reloc symbol index.
6840 The back end routine is given a pointer to the swapped in
6841 internal symbols, and can access the hash table entries
6842 for the external symbols via elf_sym_hashes (input_bfd).
6844 When generating relocateable output, the back end routine
6845 must handle STB_LOCAL/STT_SECTION symbols specially. The
6846 output symbol is going to be a section symbol
6847 corresponding to the output section, which will require
6848 the addend to be adjusted. */
6850 if (! (*relocate_section) (output_bfd, finfo->info,
6851 input_bfd, o, contents,
6853 finfo->internal_syms,
6859 Elf_Internal_Rela *irela;
6860 Elf_Internal_Rela *irelaend;
6861 struct elf_link_hash_entry **rel_hash;
6862 Elf_Internal_Shdr *input_rel_hdr;
6863 unsigned int next_erel;
6864 boolean (*reloc_emitter) PARAMS ((bfd *, asection *,
6865 Elf_Internal_Shdr *,
6866 Elf_Internal_Rela *));
6867 boolean rela_normal;
6869 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6870 rela_normal = (bed->rela_normal
6871 && (input_rel_hdr->sh_entsize
6872 == sizeof (Elf_External_Rela)));
6874 /* Adjust the reloc addresses and symbol indices. */
6876 irela = internal_relocs;
6877 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6878 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6879 + elf_section_data (o->output_section)->rel_count
6880 + elf_section_data (o->output_section)->rel_count2);
6881 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6883 unsigned long r_symndx;
6886 if (next_erel == bed->s->int_rels_per_ext_rel)
6892 irela->r_offset += o->output_offset;
6894 /* Relocs in an executable have to be virtual addresses. */
6895 if (!finfo->info->relocateable)
6896 irela->r_offset += o->output_section->vma;
6898 r_symndx = ELF_R_SYM (irela->r_info);
6903 if (r_symndx >= locsymcount
6904 || (elf_bad_symtab (input_bfd)
6905 && finfo->sections[r_symndx] == NULL))
6907 struct elf_link_hash_entry *rh;
6910 /* This is a reloc against a global symbol. We
6911 have not yet output all the local symbols, so
6912 we do not know the symbol index of any global
6913 symbol. We set the rel_hash entry for this
6914 reloc to point to the global hash table entry
6915 for this symbol. The symbol index is then
6916 set at the end of elf_bfd_final_link. */
6917 indx = r_symndx - extsymoff;
6918 rh = elf_sym_hashes (input_bfd)[indx];
6919 while (rh->root.type == bfd_link_hash_indirect
6920 || rh->root.type == bfd_link_hash_warning)
6921 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6923 /* Setting the index to -2 tells
6924 elf_link_output_extsym that this symbol is
6926 BFD_ASSERT (rh->indx < 0);
6934 /* This is a reloc against a local symbol. */
6937 isym = finfo->internal_syms + r_symndx;
6938 sec = finfo->sections[r_symndx];
6939 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6941 /* I suppose the backend ought to fill in the
6942 section of any STT_SECTION symbol against a
6943 processor specific section. If we have
6944 discarded a section, the output_section will
6945 be the absolute section. */
6946 if (bfd_is_abs_section (sec)
6948 && bfd_is_abs_section (sec->output_section)))
6950 else if (sec == NULL || sec->owner == NULL)
6952 bfd_set_error (bfd_error_bad_value);
6957 r_symndx = sec->output_section->target_index;
6958 BFD_ASSERT (r_symndx != 0);
6961 /* Adjust the addend according to where the
6962 section winds up in the output section. */
6964 irela->r_addend += sec->output_offset;
6968 if (finfo->indices[r_symndx] == -1)
6970 unsigned long shlink;
6974 if (finfo->info->strip == strip_all)
6976 /* You can't do ld -r -s. */
6977 bfd_set_error (bfd_error_invalid_operation);
6981 /* This symbol was skipped earlier, but
6982 since it is needed by a reloc, we
6983 must output it now. */
6984 shlink = symtab_hdr->sh_link;
6985 name = (bfd_elf_string_from_elf_section
6986 (input_bfd, shlink, isym->st_name));
6990 osec = sec->output_section;
6992 _bfd_elf_section_from_bfd_section (output_bfd,
6994 if (isym->st_shndx == SHN_BAD)
6997 isym->st_value += sec->output_offset;
6998 if (! finfo->info->relocateable)
7000 isym->st_value += osec->vma;
7001 if (ELF_ST_TYPE (isym->st_info) == STT_TLS)
7003 /* STT_TLS symbols are relative to PT_TLS
7005 BFD_ASSERT (finfo->first_tls_sec != NULL);
7006 isym->st_value -= finfo->first_tls_sec->vma;
7010 finfo->indices[r_symndx]
7011 = bfd_get_symcount (output_bfd);
7013 if (! elf_link_output_sym (finfo, name, isym, sec))
7017 r_symndx = finfo->indices[r_symndx];
7020 irela->r_info = ELF_R_INFO (r_symndx,
7021 ELF_R_TYPE (irela->r_info));
7024 /* Swap out the relocs. */
7025 if (bed->elf_backend_emit_relocs
7026 && !(finfo->info->relocateable
7027 || finfo->info->emitrelocations))
7028 reloc_emitter = bed->elf_backend_emit_relocs;
7030 reloc_emitter = elf_link_output_relocs;
7032 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
7036 input_rel_hdr = elf_section_data (o)->rel_hdr2;
7039 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7040 * bed->s->int_rels_per_ext_rel);
7041 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
7049 /* Write out the modified section contents. */
7050 if (bed->elf_backend_write_section
7051 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7053 /* Section written out. */
7055 else switch (elf_section_data (o)->sec_info_type)
7057 case ELF_INFO_TYPE_STABS:
7058 if (! (_bfd_write_section_stabs
7060 &elf_hash_table (finfo->info)->stab_info,
7061 o, &elf_section_data (o)->sec_info, contents)))
7064 case ELF_INFO_TYPE_MERGE:
7065 if (! (_bfd_write_merged_section
7066 (output_bfd, o, elf_section_data (o)->sec_info)))
7069 case ELF_INFO_TYPE_EH_FRAME:
7074 = bfd_get_section_by_name (elf_hash_table (finfo->info)->dynobj,
7076 if (! (_bfd_elf_write_section_eh_frame (output_bfd, o, ehdrsec,
7083 bfd_size_type sec_size;
7085 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
7086 if (! (o->flags & SEC_EXCLUDE)
7087 && ! bfd_set_section_contents (output_bfd, o->output_section,
7089 (file_ptr) o->output_offset,
7100 /* Generate a reloc when linking an ELF file. This is a reloc
7101 requested by the linker, and does come from any input file. This
7102 is used to build constructor and destructor tables when linking
7106 elf_reloc_link_order (output_bfd, info, output_section, link_order)
7108 struct bfd_link_info *info;
7109 asection *output_section;
7110 struct bfd_link_order *link_order;
7112 reloc_howto_type *howto;
7116 struct elf_link_hash_entry **rel_hash_ptr;
7117 Elf_Internal_Shdr *rel_hdr;
7118 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7120 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7123 bfd_set_error (bfd_error_bad_value);
7127 addend = link_order->u.reloc.p->addend;
7129 /* Figure out the symbol index. */
7130 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7131 + elf_section_data (output_section)->rel_count
7132 + elf_section_data (output_section)->rel_count2);
7133 if (link_order->type == bfd_section_reloc_link_order)
7135 indx = link_order->u.reloc.p->u.section->target_index;
7136 BFD_ASSERT (indx != 0);
7137 *rel_hash_ptr = NULL;
7141 struct elf_link_hash_entry *h;
7143 /* Treat a reloc against a defined symbol as though it were
7144 actually against the section. */
7145 h = ((struct elf_link_hash_entry *)
7146 bfd_wrapped_link_hash_lookup (output_bfd, info,
7147 link_order->u.reloc.p->u.name,
7148 false, false, true));
7150 && (h->root.type == bfd_link_hash_defined
7151 || h->root.type == bfd_link_hash_defweak))
7155 section = h->root.u.def.section;
7156 indx = section->output_section->target_index;
7157 *rel_hash_ptr = NULL;
7158 /* It seems that we ought to add the symbol value to the
7159 addend here, but in practice it has already been added
7160 because it was passed to constructor_callback. */
7161 addend += section->output_section->vma + section->output_offset;
7165 /* Setting the index to -2 tells elf_link_output_extsym that
7166 this symbol is used by a reloc. */
7173 if (! ((*info->callbacks->unattached_reloc)
7174 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
7175 (asection *) NULL, (bfd_vma) 0)))
7181 /* If this is an inplace reloc, we must write the addend into the
7183 if (howto->partial_inplace && addend != 0)
7186 bfd_reloc_status_type rstat;
7189 const char *sym_name;
7191 size = bfd_get_reloc_size (howto);
7192 buf = (bfd_byte *) bfd_zmalloc (size);
7193 if (buf == (bfd_byte *) NULL)
7195 rstat = _bfd_relocate_contents (howto, output_bfd, (bfd_vma) addend, buf);
7202 case bfd_reloc_outofrange:
7205 case bfd_reloc_overflow:
7206 if (link_order->type == bfd_section_reloc_link_order)
7207 sym_name = bfd_section_name (output_bfd,
7208 link_order->u.reloc.p->u.section);
7210 sym_name = link_order->u.reloc.p->u.name;
7211 if (! ((*info->callbacks->reloc_overflow)
7212 (info, sym_name, howto->name, addend,
7213 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
7220 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
7221 (file_ptr) link_order->offset, size);
7227 /* The address of a reloc is relative to the section in a
7228 relocateable file, and is a virtual address in an executable
7230 offset = link_order->offset;
7231 if (! info->relocateable)
7232 offset += output_section->vma;
7234 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7236 if (rel_hdr->sh_type == SHT_REL)
7239 Elf_Internal_Rel *irel;
7240 Elf_External_Rel *erel;
7243 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
7244 irel = (Elf_Internal_Rel *) bfd_zmalloc (size);
7248 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7249 irel[i].r_offset = offset;
7250 irel[0].r_info = ELF_R_INFO (indx, howto->type);
7252 erel = ((Elf_External_Rel *) rel_hdr->contents
7253 + elf_section_data (output_section)->rel_count);
7255 if (bed->s->swap_reloc_out)
7256 (*bed->s->swap_reloc_out) (output_bfd, irel, (bfd_byte *) erel);
7258 elf_swap_reloc_out (output_bfd, irel, erel);
7265 Elf_Internal_Rela *irela;
7266 Elf_External_Rela *erela;
7269 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
7270 irela = (Elf_Internal_Rela *) bfd_zmalloc (size);
7274 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7275 irela[i].r_offset = offset;
7276 irela[0].r_info = ELF_R_INFO (indx, howto->type);
7277 irela[0].r_addend = addend;
7279 erela = ((Elf_External_Rela *) rel_hdr->contents
7280 + elf_section_data (output_section)->rel_count);
7282 if (bed->s->swap_reloca_out)
7283 (*bed->s->swap_reloca_out) (output_bfd, irela, (bfd_byte *) erela);
7285 elf_swap_reloca_out (output_bfd, irela, erela);
7288 ++elf_section_data (output_section)->rel_count;
7293 /* Allocate a pointer to live in a linker created section. */
7296 elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
7298 struct bfd_link_info *info;
7299 elf_linker_section_t *lsect;
7300 struct elf_link_hash_entry *h;
7301 const Elf_Internal_Rela *rel;
7303 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
7304 elf_linker_section_pointers_t *linker_section_ptr;
7305 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7308 BFD_ASSERT (lsect != NULL);
7310 /* Is this a global symbol? */
7313 /* Has this symbol already been allocated? If so, our work is done. */
7314 if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
7319 ptr_linker_section_ptr = &h->linker_section_pointer;
7320 /* Make sure this symbol is output as a dynamic symbol. */
7321 if (h->dynindx == -1)
7323 if (! elf_link_record_dynamic_symbol (info, h))
7327 if (lsect->rel_section)
7328 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7332 /* Allocation of a pointer to a local symbol. */
7333 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
7335 /* Allocate a table to hold the local symbols if first time. */
7338 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
7339 register unsigned int i;
7342 amt *= sizeof (elf_linker_section_pointers_t *);
7343 ptr = (elf_linker_section_pointers_t **) bfd_alloc (abfd, amt);
7348 elf_local_ptr_offsets (abfd) = ptr;
7349 for (i = 0; i < num_symbols; i++)
7350 ptr[i] = (elf_linker_section_pointers_t *) 0;
7353 /* Has this symbol already been allocated? If so, our work is done. */
7354 if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
7359 ptr_linker_section_ptr = &ptr[r_symndx];
7363 /* If we are generating a shared object, we need to
7364 output a R_<xxx>_RELATIVE reloc so that the
7365 dynamic linker can adjust this GOT entry. */
7366 BFD_ASSERT (lsect->rel_section != NULL);
7367 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7371 /* Allocate space for a pointer in the linker section, and allocate
7372 a new pointer record from internal memory. */
7373 BFD_ASSERT (ptr_linker_section_ptr != NULL);
7374 amt = sizeof (elf_linker_section_pointers_t);
7375 linker_section_ptr = (elf_linker_section_pointers_t *) bfd_alloc (abfd, amt);
7377 if (!linker_section_ptr)
7380 linker_section_ptr->next = *ptr_linker_section_ptr;
7381 linker_section_ptr->addend = rel->r_addend;
7382 linker_section_ptr->which = lsect->which;
7383 linker_section_ptr->written_address_p = false;
7384 *ptr_linker_section_ptr = linker_section_ptr;
7387 if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
7389 linker_section_ptr->offset = (lsect->section->_raw_size
7390 - lsect->hole_size + (ARCH_SIZE / 8));
7391 lsect->hole_offset += ARCH_SIZE / 8;
7392 lsect->sym_offset += ARCH_SIZE / 8;
7393 if (lsect->sym_hash)
7395 /* Bump up symbol value if needed. */
7396 lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
7398 fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
7399 lsect->sym_hash->root.root.string,
7400 (long) ARCH_SIZE / 8,
7401 (long) lsect->sym_hash->root.u.def.value);
7407 linker_section_ptr->offset = lsect->section->_raw_size;
7409 lsect->section->_raw_size += ARCH_SIZE / 8;
7413 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7414 lsect->name, (long) linker_section_ptr->offset,
7415 (long) lsect->section->_raw_size);
7422 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7425 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7428 /* Fill in the address for a pointer generated in a linker section. */
7431 elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h,
7432 relocation, rel, relative_reloc)
7435 struct bfd_link_info *info;
7436 elf_linker_section_t *lsect;
7437 struct elf_link_hash_entry *h;
7439 const Elf_Internal_Rela *rel;
7442 elf_linker_section_pointers_t *linker_section_ptr;
7444 BFD_ASSERT (lsect != NULL);
7448 /* Handle global symbol. */
7449 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7450 (h->linker_section_pointer,
7454 BFD_ASSERT (linker_section_ptr != NULL);
7456 if (! elf_hash_table (info)->dynamic_sections_created
7459 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
7461 /* This is actually a static link, or it is a
7462 -Bsymbolic link and the symbol is defined
7463 locally. We must initialize this entry in the
7466 When doing a dynamic link, we create a .rela.<xxx>
7467 relocation entry to initialize the value. This
7468 is done in the finish_dynamic_symbol routine. */
7469 if (!linker_section_ptr->written_address_p)
7471 linker_section_ptr->written_address_p = true;
7472 bfd_put_ptr (output_bfd,
7473 relocation + linker_section_ptr->addend,
7474 (lsect->section->contents
7475 + linker_section_ptr->offset));
7481 /* Handle local symbol. */
7482 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7483 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
7484 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
7485 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7486 (elf_local_ptr_offsets (input_bfd)[r_symndx],
7490 BFD_ASSERT (linker_section_ptr != NULL);
7492 /* Write out pointer if it hasn't been rewritten out before. */
7493 if (!linker_section_ptr->written_address_p)
7495 linker_section_ptr->written_address_p = true;
7496 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
7497 lsect->section->contents + linker_section_ptr->offset);
7501 asection *srel = lsect->rel_section;
7502 Elf_Internal_Rela *outrel;
7503 Elf_External_Rela *erel;
7504 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7508 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
7509 outrel = (Elf_Internal_Rela *) bfd_zmalloc (amt);
7512 (*_bfd_error_handler) (_("Error: out of memory"));
7516 /* We need to generate a relative reloc for the dynamic
7520 srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
7522 lsect->rel_section = srel;
7525 BFD_ASSERT (srel != NULL);
7527 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7528 outrel[i].r_offset = (lsect->section->output_section->vma
7529 + lsect->section->output_offset
7530 + linker_section_ptr->offset);
7531 outrel[0].r_info = ELF_R_INFO (0, relative_reloc);
7532 outrel[0].r_addend = 0;
7533 erel = (Elf_External_Rela *) lsect->section->contents;
7534 erel += elf_section_data (lsect->section)->rel_count;
7535 elf_swap_reloca_out (output_bfd, outrel, erel);
7536 ++elf_section_data (lsect->section)->rel_count;
7543 relocation = (lsect->section->output_offset
7544 + linker_section_ptr->offset
7545 - lsect->hole_offset
7546 - lsect->sym_offset);
7550 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7551 lsect->name, (long) relocation, (long) relocation);
7554 /* Subtract out the addend, because it will get added back in by the normal
7556 return relocation - linker_section_ptr->addend;
7559 /* Garbage collect unused sections. */
7561 static boolean elf_gc_mark
7562 PARAMS ((struct bfd_link_info *info, asection *sec,
7563 asection * (*gc_mark_hook)
7564 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7565 struct elf_link_hash_entry *, Elf_Internal_Sym *))));
7567 static boolean elf_gc_sweep
7568 PARAMS ((struct bfd_link_info *info,
7569 boolean (*gc_sweep_hook)
7570 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7571 const Elf_Internal_Rela *relocs))));
7573 static boolean elf_gc_sweep_symbol
7574 PARAMS ((struct elf_link_hash_entry *h, PTR idxptr));
7576 static boolean elf_gc_allocate_got_offsets
7577 PARAMS ((struct elf_link_hash_entry *h, PTR offarg));
7579 static boolean elf_gc_propagate_vtable_entries_used
7580 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7582 static boolean elf_gc_smash_unused_vtentry_relocs
7583 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7585 /* The mark phase of garbage collection. For a given section, mark
7586 it and any sections in this section's group, and all the sections
7587 which define symbols to which it refers. */
7590 elf_gc_mark (info, sec, gc_mark_hook)
7591 struct bfd_link_info *info;
7593 asection * (*gc_mark_hook)
7594 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7595 struct elf_link_hash_entry *, Elf_Internal_Sym *));
7598 asection *group_sec;
7602 /* Mark all the sections in the group. */
7603 group_sec = elf_section_data (sec)->next_in_group;
7604 if (group_sec && !group_sec->gc_mark)
7605 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
7608 /* Look through the section relocs. */
7610 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
7612 Elf_Internal_Rela *relstart, *rel, *relend;
7613 Elf_Internal_Shdr *symtab_hdr;
7614 Elf_Internal_Shdr *shndx_hdr;
7615 struct elf_link_hash_entry **sym_hashes;
7618 Elf_External_Sym *locsyms, *freesyms = NULL;
7619 Elf_External_Sym_Shndx *locsym_shndx;
7620 bfd *input_bfd = sec->owner;
7621 struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
7623 /* GCFIXME: how to arrange so that relocs and symbols are not
7624 reread continually? */
7626 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7627 sym_hashes = elf_sym_hashes (input_bfd);
7629 /* Read the local symbols. */
7630 if (elf_bad_symtab (input_bfd))
7632 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
7636 extsymoff = nlocsyms = symtab_hdr->sh_info;
7638 if (symtab_hdr->contents)
7639 locsyms = (Elf_External_Sym *) symtab_hdr->contents;
7640 else if (nlocsyms == 0)
7644 bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym);
7645 locsyms = freesyms = bfd_malloc (amt);
7646 if (freesyms == NULL
7647 || bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
7648 || bfd_bread (locsyms, amt, input_bfd) != amt)
7655 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
7656 locsym_shndx = NULL;
7657 if (shndx_hdr->sh_size != 0 && nlocsyms != 0)
7659 bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym_Shndx);
7660 locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
7661 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
7662 || bfd_bread (locsym_shndx, amt, input_bfd) != amt)
7666 /* Read the relocations. */
7667 relstart = (NAME(_bfd_elf,link_read_relocs)
7668 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL,
7669 info->keep_memory));
7670 if (relstart == NULL)
7675 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7677 for (rel = relstart; rel < relend; rel++)
7679 unsigned long r_symndx;
7681 struct elf_link_hash_entry *h;
7683 Elf_External_Sym_Shndx *locshndx;
7685 r_symndx = ELF_R_SYM (rel->r_info);
7689 if (elf_bad_symtab (sec->owner))
7691 locshndx = locsym_shndx + (locsym_shndx ? r_symndx : 0);
7692 elf_swap_symbol_in (input_bfd,
7693 (const PTR) (locsyms + r_symndx),
7694 (const PTR) locshndx,
7696 if (ELF_ST_BIND (s.st_info) == STB_LOCAL)
7697 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
7700 h = sym_hashes[r_symndx - extsymoff];
7701 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
7704 else if (r_symndx >= nlocsyms)
7706 h = sym_hashes[r_symndx - extsymoff];
7707 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
7711 locshndx = locsym_shndx + (locsym_shndx ? r_symndx : 0);
7712 elf_swap_symbol_in (input_bfd,
7713 (const PTR) (locsyms + r_symndx),
7714 (const PTR) locshndx,
7716 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
7719 if (rsec && !rsec->gc_mark)
7721 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
7723 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
7732 if (!info->keep_memory)
7742 /* The sweep phase of garbage collection. Remove all garbage sections. */
7745 elf_gc_sweep (info, gc_sweep_hook)
7746 struct bfd_link_info *info;
7747 boolean (*gc_sweep_hook)
7748 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7749 const Elf_Internal_Rela *relocs));
7753 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7757 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7760 for (o = sub->sections; o != NULL; o = o->next)
7762 /* Keep special sections. Keep .debug sections. */
7763 if ((o->flags & SEC_LINKER_CREATED)
7764 || (o->flags & SEC_DEBUGGING))
7770 /* Skip sweeping sections already excluded. */
7771 if (o->flags & SEC_EXCLUDE)
7774 /* Since this is early in the link process, it is simple
7775 to remove a section from the output. */
7776 o->flags |= SEC_EXCLUDE;
7778 /* But we also have to update some of the relocation
7779 info we collected before. */
7781 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
7783 Elf_Internal_Rela *internal_relocs;
7786 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
7787 (o->owner, o, NULL, NULL, info->keep_memory));
7788 if (internal_relocs == NULL)
7791 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
7793 if (!info->keep_memory)
7794 free (internal_relocs);
7802 /* Remove the symbols that were in the swept sections from the dynamic
7803 symbol table. GCFIXME: Anyone know how to get them out of the
7804 static symbol table as well? */
7808 elf_link_hash_traverse (elf_hash_table (info),
7809 elf_gc_sweep_symbol,
7812 elf_hash_table (info)->dynsymcount = i;
7818 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7821 elf_gc_sweep_symbol (h, idxptr)
7822 struct elf_link_hash_entry *h;
7825 int *idx = (int *) idxptr;
7827 if (h->root.type == bfd_link_hash_warning)
7828 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7830 if (h->dynindx != -1
7831 && ((h->root.type != bfd_link_hash_defined
7832 && h->root.type != bfd_link_hash_defweak)
7833 || h->root.u.def.section->gc_mark))
7834 h->dynindx = (*idx)++;
7839 /* Propogate collected vtable information. This is called through
7840 elf_link_hash_traverse. */
7843 elf_gc_propagate_vtable_entries_used (h, okp)
7844 struct elf_link_hash_entry *h;
7847 if (h->root.type == bfd_link_hash_warning)
7848 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7850 /* Those that are not vtables. */
7851 if (h->vtable_parent == NULL)
7854 /* Those vtables that do not have parents, we cannot merge. */
7855 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
7858 /* If we've already been done, exit. */
7859 if (h->vtable_entries_used && h->vtable_entries_used[-1])
7862 /* Make sure the parent's table is up to date. */
7863 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
7865 if (h->vtable_entries_used == NULL)
7867 /* None of this table's entries were referenced. Re-use the
7869 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
7870 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
7877 /* Or the parent's entries into ours. */
7878 cu = h->vtable_entries_used;
7880 pu = h->vtable_parent->vtable_entries_used;
7883 asection *sec = h->root.u.def.section;
7884 struct elf_backend_data *bed = get_elf_backend_data (sec->owner);
7885 int file_align = bed->s->file_align;
7887 n = h->vtable_parent->vtable_entries_size / file_align;
7902 elf_gc_smash_unused_vtentry_relocs (h, okp)
7903 struct elf_link_hash_entry *h;
7907 bfd_vma hstart, hend;
7908 Elf_Internal_Rela *relstart, *relend, *rel;
7909 struct elf_backend_data *bed;
7912 if (h->root.type == bfd_link_hash_warning)
7913 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7915 /* Take care of both those symbols that do not describe vtables as
7916 well as those that are not loaded. */
7917 if (h->vtable_parent == NULL)
7920 BFD_ASSERT (h->root.type == bfd_link_hash_defined
7921 || h->root.type == bfd_link_hash_defweak);
7923 sec = h->root.u.def.section;
7924 hstart = h->root.u.def.value;
7925 hend = hstart + h->size;
7927 relstart = (NAME(_bfd_elf,link_read_relocs)
7928 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, true));
7930 return *(boolean *) okp = false;
7931 bed = get_elf_backend_data (sec->owner);
7932 file_align = bed->s->file_align;
7934 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7936 for (rel = relstart; rel < relend; ++rel)
7937 if (rel->r_offset >= hstart && rel->r_offset < hend)
7939 /* If the entry is in use, do nothing. */
7940 if (h->vtable_entries_used
7941 && (rel->r_offset - hstart) < h->vtable_entries_size)
7943 bfd_vma entry = (rel->r_offset - hstart) / file_align;
7944 if (h->vtable_entries_used[entry])
7947 /* Otherwise, kill it. */
7948 rel->r_offset = rel->r_info = rel->r_addend = 0;
7954 /* Do mark and sweep of unused sections. */
7957 elf_gc_sections (abfd, info)
7959 struct bfd_link_info *info;
7963 asection * (*gc_mark_hook)
7964 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7965 struct elf_link_hash_entry *h, Elf_Internal_Sym *));
7967 if (!get_elf_backend_data (abfd)->can_gc_sections
7968 || info->relocateable || info->emitrelocations
7969 || elf_hash_table (info)->dynamic_sections_created)
7972 /* Apply transitive closure to the vtable entry usage info. */
7973 elf_link_hash_traverse (elf_hash_table (info),
7974 elf_gc_propagate_vtable_entries_used,
7979 /* Kill the vtable relocations that were not used. */
7980 elf_link_hash_traverse (elf_hash_table (info),
7981 elf_gc_smash_unused_vtentry_relocs,
7986 /* Grovel through relocs to find out who stays ... */
7988 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
7989 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7993 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7996 for (o = sub->sections; o != NULL; o = o->next)
7998 if (o->flags & SEC_KEEP)
7999 if (!elf_gc_mark (info, o, gc_mark_hook))
8004 /* ... and mark SEC_EXCLUDE for those that go. */
8005 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
8011 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
8014 elf_gc_record_vtinherit (abfd, sec, h, offset)
8017 struct elf_link_hash_entry *h;
8020 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
8021 struct elf_link_hash_entry **search, *child;
8022 bfd_size_type extsymcount;
8024 /* The sh_info field of the symtab header tells us where the
8025 external symbols start. We don't care about the local symbols at
8027 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
8028 if (!elf_bad_symtab (abfd))
8029 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8031 sym_hashes = elf_sym_hashes (abfd);
8032 sym_hashes_end = sym_hashes + extsymcount;
8034 /* Hunt down the child symbol, which is in this section at the same
8035 offset as the relocation. */
8036 for (search = sym_hashes; search != sym_hashes_end; ++search)
8038 if ((child = *search) != NULL
8039 && (child->root.type == bfd_link_hash_defined
8040 || child->root.type == bfd_link_hash_defweak)
8041 && child->root.u.def.section == sec
8042 && child->root.u.def.value == offset)
8046 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
8047 bfd_archive_filename (abfd), sec->name,
8048 (unsigned long) offset);
8049 bfd_set_error (bfd_error_invalid_operation);
8055 /* This *should* only be the absolute section. It could potentially
8056 be that someone has defined a non-global vtable though, which
8057 would be bad. It isn't worth paging in the local symbols to be
8058 sure though; that case should simply be handled by the assembler. */
8060 child->vtable_parent = (struct elf_link_hash_entry *) -1;
8063 child->vtable_parent = h;
8068 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
8071 elf_gc_record_vtentry (abfd, sec, h, addend)
8072 bfd *abfd ATTRIBUTE_UNUSED;
8073 asection *sec ATTRIBUTE_UNUSED;
8074 struct elf_link_hash_entry *h;
8077 struct elf_backend_data *bed = get_elf_backend_data (abfd);
8078 int file_align = bed->s->file_align;
8080 if (addend >= h->vtable_entries_size)
8083 boolean *ptr = h->vtable_entries_used;
8085 /* While the symbol is undefined, we have to be prepared to handle
8087 if (h->root.type == bfd_link_hash_undefined)
8094 /* Oops! We've got a reference past the defined end of
8095 the table. This is probably a bug -- shall we warn? */
8100 /* Allocate one extra entry for use as a "done" flag for the
8101 consolidation pass. */
8102 bytes = (size / file_align + 1) * sizeof (boolean);
8106 ptr = bfd_realloc (ptr - 1, (bfd_size_type) bytes);
8112 oldbytes = ((h->vtable_entries_size / file_align + 1)
8113 * sizeof (boolean));
8114 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
8118 ptr = bfd_zmalloc ((bfd_size_type) bytes);
8123 /* And arrange for that done flag to be at index -1. */
8124 h->vtable_entries_used = ptr + 1;
8125 h->vtable_entries_size = size;
8128 h->vtable_entries_used[addend / file_align] = true;
8133 /* And an accompanying bit to work out final got entry offsets once
8134 we're done. Should be called from final_link. */
8137 elf_gc_common_finalize_got_offsets (abfd, info)
8139 struct bfd_link_info *info;
8142 struct elf_backend_data *bed = get_elf_backend_data (abfd);
8145 /* The GOT offset is relative to the .got section, but the GOT header is
8146 put into the .got.plt section, if the backend uses it. */
8147 if (bed->want_got_plt)
8150 gotoff = bed->got_header_size;
8152 /* Do the local .got entries first. */
8153 for (i = info->input_bfds; i; i = i->link_next)
8155 bfd_signed_vma *local_got;
8156 bfd_size_type j, locsymcount;
8157 Elf_Internal_Shdr *symtab_hdr;
8159 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
8162 local_got = elf_local_got_refcounts (i);
8166 symtab_hdr = &elf_tdata (i)->symtab_hdr;
8167 if (elf_bad_symtab (i))
8168 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
8170 locsymcount = symtab_hdr->sh_info;
8172 for (j = 0; j < locsymcount; ++j)
8174 if (local_got[j] > 0)
8176 local_got[j] = gotoff;
8177 gotoff += ARCH_SIZE / 8;
8180 local_got[j] = (bfd_vma) -1;
8184 /* Then the global .got entries. .plt refcounts are handled by
8185 adjust_dynamic_symbol */
8186 elf_link_hash_traverse (elf_hash_table (info),
8187 elf_gc_allocate_got_offsets,
8192 /* We need a special top-level link routine to convert got reference counts
8193 to real got offsets. */
8196 elf_gc_allocate_got_offsets (h, offarg)
8197 struct elf_link_hash_entry *h;
8200 bfd_vma *off = (bfd_vma *) offarg;
8202 if (h->root.type == bfd_link_hash_warning)
8203 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8205 if (h->got.refcount > 0)
8207 h->got.offset = off[0];
8208 off[0] += ARCH_SIZE / 8;
8211 h->got.offset = (bfd_vma) -1;
8216 /* Many folk need no more in the way of final link than this, once
8217 got entry reference counting is enabled. */
8220 elf_gc_common_final_link (abfd, info)
8222 struct bfd_link_info *info;
8224 if (!elf_gc_common_finalize_got_offsets (abfd, info))
8227 /* Invoke the regular ELF backend linker to do all the work. */
8228 return elf_bfd_final_link (abfd, info);
8231 /* This function will be called though elf_link_hash_traverse to store
8232 all hash value of the exported symbols in an array. */
8235 elf_collect_hash_codes (h, data)
8236 struct elf_link_hash_entry *h;
8239 unsigned long **valuep = (unsigned long **) data;
8245 if (h->root.type == bfd_link_hash_warning)
8246 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8248 /* Ignore indirect symbols. These are added by the versioning code. */
8249 if (h->dynindx == -1)
8252 name = h->root.root.string;
8253 p = strchr (name, ELF_VER_CHR);
8256 alc = bfd_malloc ((bfd_size_type) (p - name + 1));
8257 memcpy (alc, name, (size_t) (p - name));
8258 alc[p - name] = '\0';
8262 /* Compute the hash value. */
8263 ha = bfd_elf_hash (name);
8265 /* Store the found hash value in the array given as the argument. */
8268 /* And store it in the struct so that we can put it in the hash table
8270 h->elf_hash_value = ha;
8279 elf_reloc_symbol_deleted_p (offset, cookie)
8283 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
8285 if (rcookie->bad_symtab)
8286 rcookie->rel = rcookie->rels;
8288 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
8290 unsigned long r_symndx = ELF_R_SYM (rcookie->rel->r_info);
8291 Elf_Internal_Sym isym;
8293 if (! rcookie->bad_symtab)
8294 if (rcookie->rel->r_offset > offset)
8296 if (rcookie->rel->r_offset != offset)
8299 if (rcookie->locsyms && r_symndx < rcookie->locsymcount)
8301 Elf_External_Sym *lsym;
8302 Elf_External_Sym_Shndx *lshndx;
8304 lsym = (Elf_External_Sym *) rcookie->locsyms + r_symndx;
8305 lshndx = (Elf_External_Sym_Shndx *) rcookie->locsym_shndx;
8308 elf_swap_symbol_in (rcookie->abfd, (const PTR) lsym,
8309 (const PTR) lshndx, &isym);
8312 if (r_symndx >= rcookie->locsymcount
8313 || (rcookie->locsyms
8314 && ELF_ST_BIND (isym.st_info) != STB_LOCAL))
8316 struct elf_link_hash_entry *h;
8318 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
8320 while (h->root.type == bfd_link_hash_indirect
8321 || h->root.type == bfd_link_hash_warning)
8322 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8324 if ((h->root.type == bfd_link_hash_defined
8325 || h->root.type == bfd_link_hash_defweak)
8326 && elf_discarded_section (h->root.u.def.section))
8331 else if (rcookie->locsyms)
8333 /* It's not a relocation against a global symbol,
8334 but it could be a relocation against a local
8335 symbol for a discarded section. */
8338 /* Need to: get the symbol; get the section. */
8339 if (isym.st_shndx < SHN_LORESERVE || isym.st_shndx > SHN_HIRESERVE)
8341 isec = section_from_elf_index (rcookie->abfd, isym.st_shndx);
8342 if (isec != NULL && elf_discarded_section (isec))
8351 /* Discard unneeded references to discarded sections.
8352 Returns true if any section's size was changed. */
8353 /* This function assumes that the relocations are in sorted order,
8354 which is true for all known assemblers. */
8357 elf_bfd_discard_info (output_bfd, info)
8359 struct bfd_link_info *info;
8361 struct elf_reloc_cookie cookie;
8362 asection *stab, *eh, *ehdr;
8363 Elf_Internal_Shdr *symtab_hdr;
8364 Elf_Internal_Shdr *shndx_hdr;
8365 Elf_External_Sym *freesyms;
8366 struct elf_backend_data *bed;
8368 boolean ret = false;
8369 boolean strip = info->strip == strip_all || info->strip == strip_debugger;
8371 if (info->relocateable
8372 || info->traditional_format
8373 || info->hash->creator->flavour != bfd_target_elf_flavour
8374 || ! is_elf_hash_table (info))
8378 if (elf_hash_table (info)->dynobj != NULL)
8379 ehdr = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
8382 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
8384 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
8387 bed = get_elf_backend_data (abfd);
8389 if ((abfd->flags & DYNAMIC) != 0)
8395 eh = bfd_get_section_by_name (abfd, ".eh_frame");
8396 if (eh && (eh->_raw_size == 0
8397 || bfd_is_abs_section (eh->output_section)))
8404 stab = bfd_get_section_by_name (abfd, ".stab");
8405 if (stab && (stab->_raw_size == 0
8406 || bfd_is_abs_section (stab->output_section)))
8410 || elf_section_data(stab)->sec_info_type != ELF_INFO_TYPE_STABS)
8412 && (strip || ! bed->elf_backend_discard_info))
8415 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8416 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8419 cookie.sym_hashes = elf_sym_hashes (abfd);
8420 cookie.bad_symtab = elf_bad_symtab (abfd);
8421 if (cookie.bad_symtab)
8423 cookie.locsymcount =
8424 symtab_hdr->sh_size / sizeof (Elf_External_Sym);
8425 cookie.extsymoff = 0;
8429 cookie.locsymcount = symtab_hdr->sh_info;
8430 cookie.extsymoff = symtab_hdr->sh_info;
8434 if (symtab_hdr->contents)
8435 cookie.locsyms = (void *) symtab_hdr->contents;
8436 else if (cookie.locsymcount == 0)
8437 cookie.locsyms = NULL;
8440 bfd_size_type amt = cookie.locsymcount * sizeof (Elf_External_Sym);
8441 cookie.locsyms = bfd_malloc (amt);
8442 if (cookie.locsyms == NULL)
8444 freesyms = cookie.locsyms;
8445 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
8446 || bfd_bread (cookie.locsyms, amt, abfd) != amt)
8449 free (cookie.locsyms);
8454 cookie.locsym_shndx = NULL;
8455 if (shndx_hdr->sh_size != 0 && cookie.locsymcount != 0)
8458 amt = cookie.locsymcount * sizeof (Elf_External_Sym_Shndx);
8459 cookie.locsym_shndx = bfd_malloc (amt);
8460 if (cookie.locsym_shndx == NULL)
8461 goto error_ret_free_loc;
8462 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
8463 || bfd_bread (cookie.locsym_shndx, amt, abfd) != amt)
8465 free (cookie.locsym_shndx);
8466 goto error_ret_free_loc;
8472 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8473 (abfd, stab, (PTR) NULL,
8474 (Elf_Internal_Rela *) NULL,
8475 info->keep_memory));
8478 cookie.rel = cookie.rels;
8480 cookie.rels + stab->reloc_count * bed->s->int_rels_per_ext_rel;
8481 if (_bfd_discard_section_stabs (abfd, stab,
8482 elf_section_data (stab)->sec_info,
8483 elf_reloc_symbol_deleted_p,
8486 if (! info->keep_memory)
8495 cookie.relend = NULL;
8496 if (eh->reloc_count)
8497 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8498 (abfd, eh, (PTR) NULL, (Elf_Internal_Rela *) NULL,
8499 info->keep_memory));
8502 cookie.rel = cookie.rels;
8504 cookie.rels + eh->reloc_count * bed->s->int_rels_per_ext_rel;
8506 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, ehdr,
8507 elf_reloc_symbol_deleted_p,
8510 if (! info->keep_memory)
8514 if (bed->elf_backend_discard_info)
8516 if (bed->elf_backend_discard_info (abfd, &cookie, info))
8520 if (cookie.locsym_shndx != NULL)
8521 free (cookie.locsym_shndx);
8523 if (freesyms != NULL)
8527 if (ehdr && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info, ehdr))
8533 elf_section_ignore_discarded_relocs (sec)
8536 struct elf_backend_data *bed;
8538 switch (elf_section_data (sec)->sec_info_type)
8540 case ELF_INFO_TYPE_STABS:
8541 case ELF_INFO_TYPE_EH_FRAME:
8547 bed = get_elf_backend_data (sec->owner);
8548 if (bed->elf_backend_ignore_discarded_relocs != NULL
8549 && (*bed->elf_backend_ignore_discarded_relocs) (sec))