2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* ELF linker code. */
23 /* This struct is used to pass information to routines called via
24 elf_link_hash_traverse which must return failure. */
26 struct elf_info_failed
29 struct bfd_link_info *info;
30 struct bfd_elf_version_tree *verdefs;
33 static boolean is_global_data_symbol_definition
34 PARAMS ((bfd *, Elf_Internal_Sym *));
35 static boolean elf_link_is_defined_archive_symbol
36 PARAMS ((bfd *, carsym *));
37 static boolean elf_link_add_object_symbols
38 PARAMS ((bfd *, struct bfd_link_info *));
39 static boolean elf_link_add_archive_symbols
40 PARAMS ((bfd *, struct bfd_link_info *));
41 static boolean elf_merge_symbol
42 PARAMS ((bfd *, struct bfd_link_info *, const char *,
43 Elf_Internal_Sym *, asection **, bfd_vma *,
44 struct elf_link_hash_entry **, boolean *, boolean *,
46 static boolean elf_add_default_symbol
47 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
48 const char *, Elf_Internal_Sym *, asection **, bfd_vma *,
49 boolean *, boolean, boolean));
50 static boolean elf_export_symbol
51 PARAMS ((struct elf_link_hash_entry *, PTR));
52 static boolean elf_finalize_dynstr
53 PARAMS ((bfd *, struct bfd_link_info *));
54 static boolean elf_fix_symbol_flags
55 PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *));
56 static boolean elf_adjust_dynamic_symbol
57 PARAMS ((struct elf_link_hash_entry *, PTR));
58 static boolean elf_link_find_version_dependencies
59 PARAMS ((struct elf_link_hash_entry *, PTR));
60 static boolean elf_link_find_version_dependencies
61 PARAMS ((struct elf_link_hash_entry *, PTR));
62 static boolean elf_link_assign_sym_version
63 PARAMS ((struct elf_link_hash_entry *, PTR));
64 static boolean elf_collect_hash_codes
65 PARAMS ((struct elf_link_hash_entry *, PTR));
66 static boolean elf_link_read_relocs_from_section
67 PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *));
68 static size_t compute_bucket_count
69 PARAMS ((struct bfd_link_info *));
70 static void elf_link_output_relocs
71 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
72 static boolean elf_link_size_reloc_section
73 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
74 static void elf_link_adjust_relocs
75 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int,
76 struct elf_link_hash_entry **));
77 static int elf_link_sort_cmp1
78 PARAMS ((const void *, const void *));
79 static int elf_link_sort_cmp2
80 PARAMS ((const void *, const void *));
81 static size_t elf_link_sort_relocs
82 PARAMS ((bfd *, struct bfd_link_info *, asection **));
83 static boolean elf_section_ignore_discarded_relocs
84 PARAMS ((asection *));
86 /* Given an ELF BFD, add symbols to the global hash table as
90 elf_bfd_link_add_symbols (abfd, info)
92 struct bfd_link_info *info;
94 switch (bfd_get_format (abfd))
97 return elf_link_add_object_symbols (abfd, info);
99 return elf_link_add_archive_symbols (abfd, info);
101 bfd_set_error (bfd_error_wrong_format);
106 /* Return true iff this is a non-common, definition of a non-function symbol. */
108 is_global_data_symbol_definition (abfd, sym)
109 bfd * abfd ATTRIBUTE_UNUSED;
110 Elf_Internal_Sym * sym;
112 /* Local symbols do not count, but target specific ones might. */
113 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
114 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
117 /* Function symbols do not count. */
118 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
121 /* If the section is undefined, then so is the symbol. */
122 if (sym->st_shndx == SHN_UNDEF)
125 /* If the symbol is defined in the common section, then
126 it is a common definition and so does not count. */
127 if (sym->st_shndx == SHN_COMMON)
130 /* If the symbol is in a target specific section then we
131 must rely upon the backend to tell us what it is. */
132 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
133 /* FIXME - this function is not coded yet:
135 return _bfd_is_global_symbol_definition (abfd, sym);
137 Instead for now assume that the definition is not global,
138 Even if this is wrong, at least the linker will behave
139 in the same way that it used to do. */
145 /* Search the symbol table of the archive element of the archive ABFD
146 whose archive map contains a mention of SYMDEF, and determine if
147 the symbol is defined in this element. */
149 elf_link_is_defined_archive_symbol (abfd, symdef)
153 Elf_Internal_Shdr * hdr;
154 Elf_Internal_Shdr * shndx_hdr;
155 Elf_External_Sym * esym;
156 Elf_External_Sym * esymend;
157 Elf_External_Sym * buf = NULL;
158 Elf_External_Sym_Shndx * shndx_buf = NULL;
159 Elf_External_Sym_Shndx * shndx;
160 bfd_size_type symcount;
161 bfd_size_type extsymcount;
162 bfd_size_type extsymoff;
163 boolean result = false;
167 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
168 if (abfd == (bfd *) NULL)
171 if (! bfd_check_format (abfd, bfd_object))
174 /* If we have already included the element containing this symbol in the
175 link then we do not need to include it again. Just claim that any symbol
176 it contains is not a definition, so that our caller will not decide to
177 (re)include this element. */
178 if (abfd->archive_pass)
181 /* Select the appropriate symbol table. */
182 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
184 hdr = &elf_tdata (abfd)->symtab_hdr;
185 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
189 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
193 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
195 /* The sh_info field of the symtab header tells us where the
196 external symbols start. We don't care about the local symbols. */
197 if (elf_bad_symtab (abfd))
199 extsymcount = symcount;
204 extsymcount = symcount - hdr->sh_info;
205 extsymoff = hdr->sh_info;
208 amt = extsymcount * sizeof (Elf_External_Sym);
209 buf = (Elf_External_Sym *) bfd_malloc (amt);
210 if (buf == NULL && extsymcount != 0)
213 /* Read in the symbol table.
214 FIXME: This ought to be cached somewhere. */
215 pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
216 if (bfd_seek (abfd, pos, SEEK_SET) != 0
217 || bfd_bread ((PTR) buf, amt, abfd) != amt)
220 if (shndx_hdr != NULL && shndx_hdr->sh_size != 0)
222 amt = extsymcount * sizeof (Elf_External_Sym_Shndx);
223 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
224 if (shndx_buf == NULL && extsymcount != 0)
227 pos = shndx_hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym_Shndx);
228 if (bfd_seek (abfd, pos, SEEK_SET) != 0
229 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
233 /* Scan the symbol table looking for SYMDEF. */
234 esymend = buf + extsymcount;
235 for (esym = buf, shndx = shndx_buf;
237 esym++, shndx = (shndx != NULL ? shndx + 1 : NULL))
239 Elf_Internal_Sym sym;
242 elf_swap_symbol_in (abfd, esym, shndx, &sym);
244 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
245 if (name == (const char *) NULL)
248 if (strcmp (name, symdef->name) == 0)
250 result = is_global_data_symbol_definition (abfd, & sym);
256 if (shndx_buf != NULL)
264 /* Add symbols from an ELF archive file to the linker hash table. We
265 don't use _bfd_generic_link_add_archive_symbols because of a
266 problem which arises on UnixWare. The UnixWare libc.so is an
267 archive which includes an entry libc.so.1 which defines a bunch of
268 symbols. The libc.so archive also includes a number of other
269 object files, which also define symbols, some of which are the same
270 as those defined in libc.so.1. Correct linking requires that we
271 consider each object file in turn, and include it if it defines any
272 symbols we need. _bfd_generic_link_add_archive_symbols does not do
273 this; it looks through the list of undefined symbols, and includes
274 any object file which defines them. When this algorithm is used on
275 UnixWare, it winds up pulling in libc.so.1 early and defining a
276 bunch of symbols. This means that some of the other objects in the
277 archive are not included in the link, which is incorrect since they
278 precede libc.so.1 in the archive.
280 Fortunately, ELF archive handling is simpler than that done by
281 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
282 oddities. In ELF, if we find a symbol in the archive map, and the
283 symbol is currently undefined, we know that we must pull in that
286 Unfortunately, we do have to make multiple passes over the symbol
287 table until nothing further is resolved. */
290 elf_link_add_archive_symbols (abfd, info)
292 struct bfd_link_info *info;
295 boolean *defined = NULL;
296 boolean *included = NULL;
301 if (! bfd_has_map (abfd))
303 /* An empty archive is a special case. */
304 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
306 bfd_set_error (bfd_error_no_armap);
310 /* Keep track of all symbols we know to be already defined, and all
311 files we know to be already included. This is to speed up the
312 second and subsequent passes. */
313 c = bfd_ardata (abfd)->symdef_count;
317 amt *= sizeof (boolean);
318 defined = (boolean *) bfd_malloc (amt);
319 included = (boolean *) bfd_malloc (amt);
320 if (defined == (boolean *) NULL || included == (boolean *) NULL)
322 memset (defined, 0, (size_t) amt);
323 memset (included, 0, (size_t) amt);
325 symdefs = bfd_ardata (abfd)->symdefs;
338 symdefend = symdef + c;
339 for (i = 0; symdef < symdefend; symdef++, i++)
341 struct elf_link_hash_entry *h;
343 struct bfd_link_hash_entry *undefs_tail;
346 if (defined[i] || included[i])
348 if (symdef->file_offset == last)
354 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
355 false, false, false);
361 /* If this is a default version (the name contains @@),
362 look up the symbol again without the version. The
363 effect is that references to the symbol without the
364 version will be matched by the default symbol in the
367 p = strchr (symdef->name, ELF_VER_CHR);
368 if (p == NULL || p[1] != ELF_VER_CHR)
371 copy = bfd_alloc (abfd, (bfd_size_type) (p - symdef->name + 1));
374 memcpy (copy, symdef->name, (size_t) (p - symdef->name));
375 copy[p - symdef->name] = '\0';
377 h = elf_link_hash_lookup (elf_hash_table (info), copy,
378 false, false, false);
380 bfd_release (abfd, copy);
386 if (h->root.type == bfd_link_hash_common)
388 /* We currently have a common symbol. The archive map contains
389 a reference to this symbol, so we may want to include it. We
390 only want to include it however, if this archive element
391 contains a definition of the symbol, not just another common
394 Unfortunately some archivers (including GNU ar) will put
395 declarations of common symbols into their archive maps, as
396 well as real definitions, so we cannot just go by the archive
397 map alone. Instead we must read in the element's symbol
398 table and check that to see what kind of symbol definition
400 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
403 else if (h->root.type != bfd_link_hash_undefined)
405 if (h->root.type != bfd_link_hash_undefweak)
410 /* We need to include this archive member. */
411 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
412 if (element == (bfd *) NULL)
415 if (! bfd_check_format (element, bfd_object))
418 /* Doublecheck that we have not included this object
419 already--it should be impossible, but there may be
420 something wrong with the archive. */
421 if (element->archive_pass != 0)
423 bfd_set_error (bfd_error_bad_value);
426 element->archive_pass = 1;
428 undefs_tail = info->hash->undefs_tail;
430 if (! (*info->callbacks->add_archive_element) (info, element,
433 if (! elf_link_add_object_symbols (element, info))
436 /* If there are any new undefined symbols, we need to make
437 another pass through the archive in order to see whether
438 they can be defined. FIXME: This isn't perfect, because
439 common symbols wind up on undefs_tail and because an
440 undefined symbol which is defined later on in this pass
441 does not require another pass. This isn't a bug, but it
442 does make the code less efficient than it could be. */
443 if (undefs_tail != info->hash->undefs_tail)
446 /* Look backward to mark all symbols from this object file
447 which we have already seen in this pass. */
451 included[mark] = true;
456 while (symdefs[mark].file_offset == symdef->file_offset);
458 /* We mark subsequent symbols from this object file as we go
459 on through the loop. */
460 last = symdef->file_offset;
471 if (defined != (boolean *) NULL)
473 if (included != (boolean *) NULL)
478 /* This function is called when we want to define a new symbol. It
479 handles the various cases which arise when we find a definition in
480 a dynamic object, or when there is already a definition in a
481 dynamic object. The new symbol is described by NAME, SYM, PSEC,
482 and PVALUE. We set SYM_HASH to the hash table entry. We set
483 OVERRIDE if the old symbol is overriding a new definition. We set
484 TYPE_CHANGE_OK if it is OK for the type to change. We set
485 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
486 change, we mean that we shouldn't warn if the type or size does
487 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
491 elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
492 override, type_change_ok, size_change_ok, dt_needed)
494 struct bfd_link_info *info;
496 Elf_Internal_Sym *sym;
499 struct elf_link_hash_entry **sym_hash;
501 boolean *type_change_ok;
502 boolean *size_change_ok;
506 struct elf_link_hash_entry *h;
509 boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
514 bind = ELF_ST_BIND (sym->st_info);
516 if (! bfd_is_und_section (sec))
517 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
519 h = ((struct elf_link_hash_entry *)
520 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
525 /* This code is for coping with dynamic objects, and is only useful
526 if we are doing an ELF link. */
527 if (info->hash->creator != abfd->xvec)
530 /* For merging, we only care about real symbols. */
532 while (h->root.type == bfd_link_hash_indirect
533 || h->root.type == bfd_link_hash_warning)
534 h = (struct elf_link_hash_entry *) h->root.u.i.link;
536 /* If we just created the symbol, mark it as being an ELF symbol.
537 Other than that, there is nothing to do--there is no merge issue
538 with a newly defined symbol--so we just return. */
540 if (h->root.type == bfd_link_hash_new)
542 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
546 /* OLDBFD is a BFD associated with the existing symbol. */
548 switch (h->root.type)
554 case bfd_link_hash_undefined:
555 case bfd_link_hash_undefweak:
556 oldbfd = h->root.u.undef.abfd;
559 case bfd_link_hash_defined:
560 case bfd_link_hash_defweak:
561 oldbfd = h->root.u.def.section->owner;
564 case bfd_link_hash_common:
565 oldbfd = h->root.u.c.p->section->owner;
569 /* In cases involving weak versioned symbols, we may wind up trying
570 to merge a symbol with itself. Catch that here, to avoid the
571 confusion that results if we try to override a symbol with
572 itself. The additional tests catch cases like
573 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
574 dynamic object, which we do want to handle here. */
576 && ((abfd->flags & DYNAMIC) == 0
577 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
580 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
581 respectively, is from a dynamic object. */
583 if ((abfd->flags & DYNAMIC) != 0)
589 olddyn = (oldbfd->flags & DYNAMIC) != 0;
594 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
595 indices used by MIPS ELF. */
596 switch (h->root.type)
602 case bfd_link_hash_defined:
603 case bfd_link_hash_defweak:
604 hsec = h->root.u.def.section;
607 case bfd_link_hash_common:
608 hsec = h->root.u.c.p->section;
615 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
618 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
619 respectively, appear to be a definition rather than reference. */
621 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
626 if (h->root.type == bfd_link_hash_undefined
627 || h->root.type == bfd_link_hash_undefweak
628 || h->root.type == bfd_link_hash_common)
633 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
634 symbol, respectively, appears to be a common symbol in a dynamic
635 object. If a symbol appears in an uninitialized section, and is
636 not weak, and is not a function, then it may be a common symbol
637 which was resolved when the dynamic object was created. We want
638 to treat such symbols specially, because they raise special
639 considerations when setting the symbol size: if the symbol
640 appears as a common symbol in a regular object, and the size in
641 the regular object is larger, we must make sure that we use the
642 larger size. This problematic case can always be avoided in C,
643 but it must be handled correctly when using Fortran shared
646 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
647 likewise for OLDDYNCOMMON and OLDDEF.
649 Note that this test is just a heuristic, and that it is quite
650 possible to have an uninitialized symbol in a shared object which
651 is really a definition, rather than a common symbol. This could
652 lead to some minor confusion when the symbol really is a common
653 symbol in some regular object. However, I think it will be
658 && (sec->flags & SEC_ALLOC) != 0
659 && (sec->flags & SEC_LOAD) == 0
662 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
665 newdyncommon = false;
669 && h->root.type == bfd_link_hash_defined
670 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
671 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
672 && (h->root.u.def.section->flags & SEC_LOAD) == 0
674 && h->type != STT_FUNC)
677 olddyncommon = false;
679 /* It's OK to change the type if either the existing symbol or the
680 new symbol is weak unless it comes from a DT_NEEDED entry of
681 a shared object, in which case, the DT_NEEDED entry may not be
682 required at the run time. */
684 if ((! dt_needed && h->root.type == bfd_link_hash_defweak)
685 || h->root.type == bfd_link_hash_undefweak
687 *type_change_ok = true;
689 /* It's OK to change the size if either the existing symbol or the
690 new symbol is weak, or if the old symbol is undefined. */
693 || h->root.type == bfd_link_hash_undefined)
694 *size_change_ok = true;
696 /* If both the old and the new symbols look like common symbols in a
697 dynamic object, set the size of the symbol to the larger of the
702 && sym->st_size != h->size)
704 /* Since we think we have two common symbols, issue a multiple
705 common warning if desired. Note that we only warn if the
706 size is different. If the size is the same, we simply let
707 the old symbol override the new one as normally happens with
708 symbols defined in dynamic objects. */
710 if (! ((*info->callbacks->multiple_common)
711 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
712 h->size, abfd, bfd_link_hash_common, sym->st_size)))
715 if (sym->st_size > h->size)
716 h->size = sym->st_size;
718 *size_change_ok = true;
721 /* If we are looking at a dynamic object, and we have found a
722 definition, we need to see if the symbol was already defined by
723 some other object. If so, we want to use the existing
724 definition, and we do not want to report a multiple symbol
725 definition error; we do this by clobbering *PSEC to be
728 We treat a common symbol as a definition if the symbol in the
729 shared library is a function, since common symbols always
730 represent variables; this can cause confusion in principle, but
731 any such confusion would seem to indicate an erroneous program or
732 shared library. We also permit a common symbol in a regular
733 object to override a weak symbol in a shared object.
735 We prefer a non-weak definition in a shared library to a weak
736 definition in the executable unless it comes from a DT_NEEDED
737 entry of a shared object, in which case, the DT_NEEDED entry
738 may not be required at the run time. */
743 || (h->root.type == bfd_link_hash_common
745 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
746 && (h->root.type != bfd_link_hash_defweak
748 || bind == STB_WEAK))
752 newdyncommon = false;
754 *psec = sec = bfd_und_section_ptr;
755 *size_change_ok = true;
757 /* If we get here when the old symbol is a common symbol, then
758 we are explicitly letting it override a weak symbol or
759 function in a dynamic object, and we don't want to warn about
760 a type change. If the old symbol is a defined symbol, a type
761 change warning may still be appropriate. */
763 if (h->root.type == bfd_link_hash_common)
764 *type_change_ok = true;
767 /* Handle the special case of an old common symbol merging with a
768 new symbol which looks like a common symbol in a shared object.
769 We change *PSEC and *PVALUE to make the new symbol look like a
770 common symbol, and let _bfd_generic_link_add_one_symbol will do
774 && h->root.type == bfd_link_hash_common)
778 newdyncommon = false;
779 *pvalue = sym->st_size;
780 *psec = sec = bfd_com_section_ptr;
781 *size_change_ok = true;
784 /* If the old symbol is from a dynamic object, and the new symbol is
785 a definition which is not from a dynamic object, then the new
786 symbol overrides the old symbol. Symbols from regular files
787 always take precedence over symbols from dynamic objects, even if
788 they are defined after the dynamic object in the link.
790 As above, we again permit a common symbol in a regular object to
791 override a definition in a shared object if the shared object
792 symbol is a function or is weak.
794 As above, we permit a non-weak definition in a shared object to
795 override a weak definition in a regular object. */
799 || (bfd_is_com_section (sec)
800 && (h->root.type == bfd_link_hash_defweak
801 || h->type == STT_FUNC)))
804 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
806 || h->root.type == bfd_link_hash_defweak))
808 /* Change the hash table entry to undefined, and let
809 _bfd_generic_link_add_one_symbol do the right thing with the
812 h->root.type = bfd_link_hash_undefined;
813 h->root.u.undef.abfd = h->root.u.def.section->owner;
814 *size_change_ok = true;
817 olddyncommon = false;
819 /* We again permit a type change when a common symbol may be
820 overriding a function. */
822 if (bfd_is_com_section (sec))
823 *type_change_ok = true;
825 /* This union may have been set to be non-NULL when this symbol
826 was seen in a dynamic object. We must force the union to be
827 NULL, so that it is correct for a regular symbol. */
829 h->verinfo.vertree = NULL;
831 /* In this special case, if H is the target of an indirection,
832 we want the caller to frob with H rather than with the
833 indirect symbol. That will permit the caller to redefine the
834 target of the indirection, rather than the indirect symbol
835 itself. FIXME: This will break the -y option if we store a
836 symbol with a different name. */
840 /* Handle the special case of a new common symbol merging with an
841 old symbol that looks like it might be a common symbol defined in
842 a shared object. Note that we have already handled the case in
843 which a new common symbol should simply override the definition
844 in the shared library. */
847 && bfd_is_com_section (sec)
850 /* It would be best if we could set the hash table entry to a
851 common symbol, but we don't know what to use for the section
853 if (! ((*info->callbacks->multiple_common)
854 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
855 h->size, abfd, bfd_link_hash_common, sym->st_size)))
858 /* If the predumed common symbol in the dynamic object is
859 larger, pretend that the new symbol has its size. */
861 if (h->size > *pvalue)
864 /* FIXME: We no longer know the alignment required by the symbol
865 in the dynamic object, so we just wind up using the one from
866 the regular object. */
869 olddyncommon = false;
871 h->root.type = bfd_link_hash_undefined;
872 h->root.u.undef.abfd = h->root.u.def.section->owner;
874 *size_change_ok = true;
875 *type_change_ok = true;
877 h->verinfo.vertree = NULL;
880 /* Handle the special case of a weak definition in a regular object
881 followed by a non-weak definition in a shared object. In this
882 case, we prefer the definition in the shared object unless it
883 comes from a DT_NEEDED entry of a shared object, in which case,
884 the DT_NEEDED entry may not be required at the run time. */
887 && h->root.type == bfd_link_hash_defweak
892 /* To make this work we have to frob the flags so that the rest
893 of the code does not think we are using the regular
895 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
896 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
897 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
898 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
899 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
900 | ELF_LINK_HASH_DEF_DYNAMIC);
902 /* If H is the target of an indirection, we want the caller to
903 use H rather than the indirect symbol. Otherwise if we are
904 defining a new indirect symbol we will wind up attaching it
905 to the entry we are overriding. */
909 /* Handle the special case of a non-weak definition in a shared
910 object followed by a weak definition in a regular object. In
911 this case we prefer to definition in the shared object. To make
912 this work we have to tell the caller to not treat the new symbol
916 && h->root.type != bfd_link_hash_defweak
925 /* This function is called to create an indirect symbol from the
926 default for the symbol with the default version if needed. The
927 symbol is described by H, NAME, SYM, SEC, VALUE, and OVERRIDE. We
928 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
929 indicates if it comes from a DT_NEEDED entry of a shared object. */
932 elf_add_default_symbol (abfd, info, h, name, sym, sec, value,
933 dynsym, override, dt_needed)
935 struct bfd_link_info *info;
936 struct elf_link_hash_entry *h;
938 Elf_Internal_Sym *sym;
945 boolean type_change_ok;
946 boolean size_change_ok;
948 struct elf_link_hash_entry *hi;
949 struct elf_backend_data *bed;
954 /* If this symbol has a version, and it is the default version, we
955 create an indirect symbol from the default name to the fully
956 decorated name. This will cause external references which do not
957 specify a version to be bound to this version of the symbol. */
958 p = strchr (name, ELF_VER_CHR);
959 if (p == NULL || p[1] != ELF_VER_CHR)
964 /* We are overridden by an old defition. We need to check if we
965 need to crreate the indirect symbol from the default name. */
966 hi = elf_link_hash_lookup (elf_hash_table (info), name, true,
968 BFD_ASSERT (hi != NULL);
971 while (hi->root.type == bfd_link_hash_indirect
972 || hi->root.type == bfd_link_hash_warning)
974 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
980 bed = get_elf_backend_data (abfd);
981 collect = bed->collect;
982 dynamic = (abfd->flags & DYNAMIC) != 0;
984 shortname = bfd_hash_allocate (&info->hash->table,
985 (size_t) (p - name + 1));
986 if (shortname == NULL)
988 strncpy (shortname, name, (size_t) (p - name));
989 shortname [p - name] = '\0';
991 /* We are going to create a new symbol. Merge it with any existing
992 symbol with this name. For the purposes of the merge, act as
993 though we were defining the symbol we just defined, although we
994 actually going to define an indirect symbol. */
995 type_change_ok = false;
996 size_change_ok = false;
997 if (! elf_merge_symbol (abfd, info, shortname, sym, sec, value,
998 &hi, &override, &type_change_ok,
999 &size_change_ok, dt_needed))
1004 if (! (_bfd_generic_link_add_one_symbol
1005 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1006 (bfd_vma) 0, name, false, collect,
1007 (struct bfd_link_hash_entry **) &hi)))
1012 /* In this case the symbol named SHORTNAME is overriding the
1013 indirect symbol we want to add. We were planning on making
1014 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1015 is the name without a version. NAME is the fully versioned
1016 name, and it is the default version.
1018 Overriding means that we already saw a definition for the
1019 symbol SHORTNAME in a regular object, and it is overriding
1020 the symbol defined in the dynamic object.
1022 When this happens, we actually want to change NAME, the
1023 symbol we just added, to refer to SHORTNAME. This will cause
1024 references to NAME in the shared object to become references
1025 to SHORTNAME in the regular object. This is what we expect
1026 when we override a function in a shared object: that the
1027 references in the shared object will be mapped to the
1028 definition in the regular object. */
1030 while (hi->root.type == bfd_link_hash_indirect
1031 || hi->root.type == bfd_link_hash_warning)
1032 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1034 h->root.type = bfd_link_hash_indirect;
1035 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1036 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1038 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1039 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1040 if (hi->elf_link_hash_flags
1041 & (ELF_LINK_HASH_REF_REGULAR
1042 | ELF_LINK_HASH_DEF_REGULAR))
1044 if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1049 /* Now set HI to H, so that the following code will set the
1050 other fields correctly. */
1054 /* If there is a duplicate definition somewhere, then HI may not
1055 point to an indirect symbol. We will have reported an error to
1056 the user in that case. */
1058 if (hi->root.type == bfd_link_hash_indirect)
1060 struct elf_link_hash_entry *ht;
1062 /* If the symbol became indirect, then we assume that we have
1063 not seen a definition before. */
1064 BFD_ASSERT ((hi->elf_link_hash_flags
1065 & (ELF_LINK_HASH_DEF_DYNAMIC
1066 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1068 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1069 (*bed->elf_backend_copy_indirect_symbol) (ht, hi);
1071 /* See if the new flags lead us to realize that the symbol must
1078 || ((hi->elf_link_hash_flags
1079 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1084 if ((hi->elf_link_hash_flags
1085 & ELF_LINK_HASH_REF_REGULAR) != 0)
1091 /* We also need to define an indirection from the nondefault version
1094 shortname = bfd_hash_allocate (&info->hash->table, strlen (name));
1095 if (shortname == NULL)
1097 strncpy (shortname, name, (size_t) (p - name));
1098 strcpy (shortname + (p - name), p + 1);
1100 /* Once again, merge with any existing symbol. */
1101 type_change_ok = false;
1102 size_change_ok = false;
1103 if (! elf_merge_symbol (abfd, info, shortname, sym, sec, value,
1104 &hi, &override, &type_change_ok,
1105 &size_change_ok, dt_needed))
1110 /* Here SHORTNAME is a versioned name, so we don't expect to see
1111 the type of override we do in the case above. */
1112 (*_bfd_error_handler)
1113 (_("%s: warning: unexpected redefinition of `%s'"),
1114 bfd_archive_filename (abfd), shortname);
1118 if (! (_bfd_generic_link_add_one_symbol
1119 (info, abfd, shortname, BSF_INDIRECT,
1120 bfd_ind_section_ptr, (bfd_vma) 0, name, false,
1121 collect, (struct bfd_link_hash_entry **) &hi)))
1124 /* If there is a duplicate definition somewhere, then HI may not
1125 point to an indirect symbol. We will have reported an error
1126 to the user in that case. */
1128 if (hi->root.type == bfd_link_hash_indirect)
1130 /* If the symbol became indirect, then we assume that we have
1131 not seen a definition before. */
1132 BFD_ASSERT ((hi->elf_link_hash_flags
1133 & (ELF_LINK_HASH_DEF_DYNAMIC
1134 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1136 (*bed->elf_backend_copy_indirect_symbol) (h, hi);
1138 /* See if the new flags lead us to realize that the symbol
1145 || ((hi->elf_link_hash_flags
1146 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1151 if ((hi->elf_link_hash_flags
1152 & ELF_LINK_HASH_REF_REGULAR) != 0)
1162 /* Add symbols from an ELF object file to the linker hash table. */
1165 elf_link_add_object_symbols (abfd, info)
1167 struct bfd_link_info *info;
1169 boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
1170 const Elf_Internal_Sym *,
1171 const char **, flagword *,
1172 asection **, bfd_vma *));
1173 boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *,
1174 asection *, const Elf_Internal_Rela *));
1176 Elf_Internal_Shdr *hdr;
1177 Elf_Internal_Shdr *shndx_hdr;
1178 bfd_size_type symcount;
1179 bfd_size_type extsymcount;
1180 bfd_size_type extsymoff;
1181 Elf_External_Sym *buf = NULL;
1182 Elf_External_Sym_Shndx *shndx_buf = NULL;
1183 Elf_External_Sym_Shndx *shndx;
1184 struct elf_link_hash_entry **sym_hash;
1186 Elf_External_Versym *extversym = NULL;
1187 Elf_External_Versym *ever;
1188 Elf_External_Dyn *dynbuf = NULL;
1189 struct elf_link_hash_entry *weaks;
1190 Elf_External_Sym *esym;
1191 Elf_External_Sym *esymend;
1192 struct elf_backend_data *bed;
1194 struct elf_link_hash_table * hash_table;
1198 hash_table = elf_hash_table (info);
1200 bed = get_elf_backend_data (abfd);
1201 add_symbol_hook = bed->elf_add_symbol_hook;
1202 collect = bed->collect;
1204 if ((abfd->flags & DYNAMIC) == 0)
1210 /* You can't use -r against a dynamic object. Also, there's no
1211 hope of using a dynamic object which does not exactly match
1212 the format of the output file. */
1213 if (info->relocateable || info->hash->creator != abfd->xvec)
1215 bfd_set_error (bfd_error_invalid_operation);
1220 /* As a GNU extension, any input sections which are named
1221 .gnu.warning.SYMBOL are treated as warning symbols for the given
1222 symbol. This differs from .gnu.warning sections, which generate
1223 warnings when they are included in an output file. */
1228 for (s = abfd->sections; s != NULL; s = s->next)
1232 name = bfd_get_section_name (abfd, s);
1233 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1238 name += sizeof ".gnu.warning." - 1;
1240 /* If this is a shared object, then look up the symbol
1241 in the hash table. If it is there, and it is already
1242 been defined, then we will not be using the entry
1243 from this shared object, so we don't need to warn.
1244 FIXME: If we see the definition in a regular object
1245 later on, we will warn, but we shouldn't. The only
1246 fix is to keep track of what warnings we are supposed
1247 to emit, and then handle them all at the end of the
1249 if (dynamic && abfd->xvec == info->hash->creator)
1251 struct elf_link_hash_entry *h;
1253 h = elf_link_hash_lookup (hash_table, name,
1254 false, false, true);
1256 /* FIXME: What about bfd_link_hash_common? */
1258 && (h->root.type == bfd_link_hash_defined
1259 || h->root.type == bfd_link_hash_defweak))
1261 /* We don't want to issue this warning. Clobber
1262 the section size so that the warning does not
1263 get copied into the output file. */
1269 sz = bfd_section_size (abfd, s);
1270 msg = (char *) bfd_alloc (abfd, sz + 1);
1274 if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
1279 if (! (_bfd_generic_link_add_one_symbol
1280 (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
1281 false, collect, (struct bfd_link_hash_entry **) NULL)))
1284 if (! info->relocateable)
1286 /* Clobber the section size so that the warning does
1287 not get copied into the output file. */
1294 /* If this is a dynamic object, we always link against the .dynsym
1295 symbol table, not the .symtab symbol table. The dynamic linker
1296 will only see the .dynsym symbol table, so there is no reason to
1297 look at .symtab for a dynamic object. */
1299 if (! dynamic || elf_dynsymtab (abfd) == 0)
1301 hdr = &elf_tdata (abfd)->symtab_hdr;
1302 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1306 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1312 /* Read in any version definitions. */
1314 if (! _bfd_elf_slurp_version_tables (abfd))
1317 /* Read in the symbol versions, but don't bother to convert them
1318 to internal format. */
1319 if (elf_dynversym (abfd) != 0)
1321 Elf_Internal_Shdr *versymhdr;
1323 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
1324 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
1325 if (extversym == NULL)
1327 amt = versymhdr->sh_size;
1328 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
1329 || bfd_bread ((PTR) extversym, amt, abfd) != amt)
1334 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1336 /* The sh_info field of the symtab header tells us where the
1337 external symbols start. We don't care about the local symbols at
1339 if (elf_bad_symtab (abfd))
1341 extsymcount = symcount;
1346 extsymcount = symcount - hdr->sh_info;
1347 extsymoff = hdr->sh_info;
1350 amt = extsymcount * sizeof (Elf_External_Sym);
1351 buf = (Elf_External_Sym *) bfd_malloc (amt);
1352 if (buf == NULL && extsymcount != 0)
1355 if (shndx_hdr != NULL && shndx_hdr->sh_size != 0)
1357 amt = extsymcount * sizeof (Elf_External_Sym_Shndx);
1358 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1359 if (shndx_buf == NULL && extsymcount != 0)
1363 /* We store a pointer to the hash table entry for each external
1365 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
1366 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
1367 if (sym_hash == NULL)
1369 elf_sym_hashes (abfd) = sym_hash;
1375 /* If we are creating a shared library, create all the dynamic
1376 sections immediately. We need to attach them to something,
1377 so we attach them to this BFD, provided it is the right
1378 format. FIXME: If there are no input BFD's of the same
1379 format as the output, we can't make a shared library. */
1381 && is_elf_hash_table (info)
1382 && ! hash_table->dynamic_sections_created
1383 && abfd->xvec == info->hash->creator)
1385 if (! elf_link_create_dynamic_sections (abfd, info))
1389 else if (! is_elf_hash_table (info))
1396 bfd_size_type oldsize;
1397 bfd_size_type strindex;
1399 /* Find the name to use in a DT_NEEDED entry that refers to this
1400 object. If the object has a DT_SONAME entry, we use it.
1401 Otherwise, if the generic linker stuck something in
1402 elf_dt_name, we use that. Otherwise, we just use the file
1403 name. If the generic linker put a null string into
1404 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1405 there is a DT_SONAME entry. */
1407 name = bfd_get_filename (abfd);
1408 if (elf_dt_name (abfd) != NULL)
1410 name = elf_dt_name (abfd);
1413 if (elf_dt_soname (abfd) != NULL)
1419 s = bfd_get_section_by_name (abfd, ".dynamic");
1422 Elf_External_Dyn *extdyn;
1423 Elf_External_Dyn *extdynend;
1425 unsigned long shlink;
1429 dynbuf = (Elf_External_Dyn *) bfd_malloc (s->_raw_size);
1433 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
1434 (file_ptr) 0, s->_raw_size))
1437 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1440 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1443 /* The shared libraries distributed with hpux11 have a bogus
1444 sh_link field for the ".dynamic" section. This code detects
1445 when SHLINK refers to a section that is not a string table
1446 and tries to find the string table for the ".dynsym" section
1448 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[shlink];
1449 if (shdr->sh_type != SHT_STRTAB)
1451 asection *ds = bfd_get_section_by_name (abfd, ".dynsym");
1452 int elfdsec = _bfd_elf_section_from_bfd_section (abfd, ds);
1455 shlink = elf_elfsections (abfd)[elfdsec]->sh_link;
1460 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
1463 for (; extdyn < extdynend; extdyn++)
1465 Elf_Internal_Dyn dyn;
1467 elf_swap_dyn_in (abfd, extdyn, &dyn);
1468 if (dyn.d_tag == DT_SONAME)
1470 unsigned int tagv = dyn.d_un.d_val;
1471 name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1475 if (dyn.d_tag == DT_NEEDED)
1477 struct bfd_link_needed_list *n, **pn;
1479 unsigned int tagv = dyn.d_un.d_val;
1481 amt = sizeof (struct bfd_link_needed_list);
1482 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1483 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1484 if (n == NULL || fnm == NULL)
1486 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1493 for (pn = & hash_table->needed;
1499 if (dyn.d_tag == DT_RUNPATH)
1501 struct bfd_link_needed_list *n, **pn;
1503 unsigned int tagv = dyn.d_un.d_val;
1505 /* When we see DT_RPATH before DT_RUNPATH, we have
1506 to clear runpath. Do _NOT_ bfd_release, as that
1507 frees all more recently bfd_alloc'd blocks as
1509 if (rpath && hash_table->runpath)
1510 hash_table->runpath = NULL;
1512 amt = sizeof (struct bfd_link_needed_list);
1513 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1514 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1515 if (n == NULL || fnm == NULL)
1517 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1524 for (pn = & hash_table->runpath;
1532 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1533 if (!runpath && dyn.d_tag == DT_RPATH)
1535 struct bfd_link_needed_list *n, **pn;
1537 unsigned int tagv = dyn.d_un.d_val;
1539 amt = sizeof (struct bfd_link_needed_list);
1540 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1541 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1542 if (n == NULL || fnm == NULL)
1544 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1551 for (pn = & hash_table->runpath;
1564 /* We do not want to include any of the sections in a dynamic
1565 object in the output file. We hack by simply clobbering the
1566 list of sections in the BFD. This could be handled more
1567 cleanly by, say, a new section flag; the existing
1568 SEC_NEVER_LOAD flag is not the one we want, because that one
1569 still implies that the section takes up space in the output
1571 abfd->sections = NULL;
1572 abfd->section_count = 0;
1574 /* If this is the first dynamic object found in the link, create
1575 the special sections required for dynamic linking. */
1576 if (! hash_table->dynamic_sections_created)
1577 if (! elf_link_create_dynamic_sections (abfd, info))
1582 /* Add a DT_NEEDED entry for this dynamic object. */
1583 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
1584 strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, false);
1585 if (strindex == (bfd_size_type) -1)
1588 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
1591 Elf_External_Dyn *dyncon, *dynconend;
1593 /* The hash table size did not change, which means that
1594 the dynamic object name was already entered. If we
1595 have already included this dynamic object in the
1596 link, just ignore it. There is no reason to include
1597 a particular dynamic object more than once. */
1598 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
1599 BFD_ASSERT (sdyn != NULL);
1601 dyncon = (Elf_External_Dyn *) sdyn->contents;
1602 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1604 for (; dyncon < dynconend; dyncon++)
1606 Elf_Internal_Dyn dyn;
1608 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
1609 if (dyn.d_tag == DT_NEEDED
1610 && dyn.d_un.d_val == strindex)
1614 if (extversym != NULL)
1616 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
1622 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
1626 /* Save the SONAME, if there is one, because sometimes the
1627 linker emulation code will need to know it. */
1629 name = basename (bfd_get_filename (abfd));
1630 elf_dt_name (abfd) = name;
1633 pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
1634 amt = extsymcount * sizeof (Elf_External_Sym);
1635 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1636 || bfd_bread ((PTR) buf, amt, abfd) != amt)
1639 if (shndx_hdr != NULL && shndx_hdr->sh_size != 0)
1641 amt = extsymcount * sizeof (Elf_External_Sym_Shndx);
1642 pos = shndx_hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym_Shndx);
1643 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1644 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
1650 ever = extversym != NULL ? extversym + extsymoff : NULL;
1651 esymend = buf + extsymcount;
1652 for (esym = buf, shndx = shndx_buf;
1654 esym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL),
1655 shndx = (shndx != NULL ? shndx + 1 : NULL))
1657 Elf_Internal_Sym sym;
1663 struct elf_link_hash_entry *h;
1665 boolean size_change_ok, type_change_ok;
1666 boolean new_weakdef;
1667 unsigned int old_alignment;
1672 elf_swap_symbol_in (abfd, esym, shndx, &sym);
1674 flags = BSF_NO_FLAGS;
1676 value = sym.st_value;
1679 bind = ELF_ST_BIND (sym.st_info);
1680 if (bind == STB_LOCAL)
1682 /* This should be impossible, since ELF requires that all
1683 global symbols follow all local symbols, and that sh_info
1684 point to the first global symbol. Unfortunatealy, Irix 5
1688 else if (bind == STB_GLOBAL)
1690 if (sym.st_shndx != SHN_UNDEF
1691 && sym.st_shndx != SHN_COMMON)
1694 else if (bind == STB_WEAK)
1698 /* Leave it up to the processor backend. */
1701 if (sym.st_shndx == SHN_UNDEF)
1702 sec = bfd_und_section_ptr;
1703 else if (sym.st_shndx < SHN_LORESERVE || sym.st_shndx > SHN_HIRESERVE)
1705 sec = section_from_elf_index (abfd, sym.st_shndx);
1707 sec = bfd_abs_section_ptr;
1708 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1711 else if (sym.st_shndx == SHN_ABS)
1712 sec = bfd_abs_section_ptr;
1713 else if (sym.st_shndx == SHN_COMMON)
1715 sec = bfd_com_section_ptr;
1716 /* What ELF calls the size we call the value. What ELF
1717 calls the value we call the alignment. */
1718 value = sym.st_size;
1722 /* Leave it up to the processor backend. */
1725 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
1726 if (name == (const char *) NULL)
1729 if (add_symbol_hook)
1731 if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec,
1735 /* The hook function sets the name to NULL if this symbol
1736 should be skipped for some reason. */
1737 if (name == (const char *) NULL)
1741 /* Sanity check that all possibilities were handled. */
1742 if (sec == (asection *) NULL)
1744 bfd_set_error (bfd_error_bad_value);
1748 if (bfd_is_und_section (sec)
1749 || bfd_is_com_section (sec))
1754 size_change_ok = false;
1755 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
1757 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1759 Elf_Internal_Versym iver;
1760 unsigned int vernum = 0;
1764 _bfd_elf_swap_versym_in (abfd, ever, &iver);
1765 vernum = iver.vs_vers & VERSYM_VERSION;
1767 /* If this is a hidden symbol, or if it is not version
1768 1, we append the version name to the symbol name.
1769 However, we do not modify a non-hidden absolute
1770 symbol, because it might be the version symbol
1771 itself. FIXME: What if it isn't? */
1772 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
1773 || (vernum > 1 && ! bfd_is_abs_section (sec)))
1776 unsigned int namelen;
1777 bfd_size_type newlen;
1780 if (sym.st_shndx != SHN_UNDEF)
1782 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
1784 (*_bfd_error_handler)
1785 (_("%s: %s: invalid version %u (max %d)"),
1786 bfd_archive_filename (abfd), name, vernum,
1787 elf_tdata (abfd)->dynverdef_hdr.sh_info);
1788 bfd_set_error (bfd_error_bad_value);
1791 else if (vernum > 1)
1793 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1799 /* We cannot simply test for the number of
1800 entries in the VERNEED section since the
1801 numbers for the needed versions do not start
1803 Elf_Internal_Verneed *t;
1806 for (t = elf_tdata (abfd)->verref;
1810 Elf_Internal_Vernaux *a;
1812 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1814 if (a->vna_other == vernum)
1816 verstr = a->vna_nodename;
1825 (*_bfd_error_handler)
1826 (_("%s: %s: invalid needed version %d"),
1827 bfd_archive_filename (abfd), name, vernum);
1828 bfd_set_error (bfd_error_bad_value);
1833 namelen = strlen (name);
1834 newlen = namelen + strlen (verstr) + 2;
1835 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
1838 newname = (char *) bfd_alloc (abfd, newlen);
1839 if (newname == NULL)
1841 strcpy (newname, name);
1842 p = newname + namelen;
1844 /* If this is a defined non-hidden version symbol,
1845 we add another @ to the name. This indicates the
1846 default version of the symbol. */
1847 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1848 && sym.st_shndx != SHN_UNDEF)
1856 if (! elf_merge_symbol (abfd, info, name, &sym, &sec, &value,
1857 sym_hash, &override, &type_change_ok,
1858 &size_change_ok, dt_needed))
1865 while (h->root.type == bfd_link_hash_indirect
1866 || h->root.type == bfd_link_hash_warning)
1867 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1869 /* Remember the old alignment if this is a common symbol, so
1870 that we don't reduce the alignment later on. We can't
1871 check later, because _bfd_generic_link_add_one_symbol
1872 will set a default for the alignment which we want to
1874 if (h->root.type == bfd_link_hash_common)
1875 old_alignment = h->root.u.c.p->alignment_power;
1877 if (elf_tdata (abfd)->verdef != NULL
1881 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1884 if (! (_bfd_generic_link_add_one_symbol
1885 (info, abfd, name, flags, sec, value, (const char *) NULL,
1886 false, collect, (struct bfd_link_hash_entry **) sym_hash)))
1890 while (h->root.type == bfd_link_hash_indirect
1891 || h->root.type == bfd_link_hash_warning)
1892 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1895 new_weakdef = false;
1898 && (flags & BSF_WEAK) != 0
1899 && ELF_ST_TYPE (sym.st_info) != STT_FUNC
1900 && info->hash->creator->flavour == bfd_target_elf_flavour
1901 && h->weakdef == NULL)
1903 /* Keep a list of all weak defined non function symbols from
1904 a dynamic object, using the weakdef field. Later in this
1905 function we will set the weakdef field to the correct
1906 value. We only put non-function symbols from dynamic
1907 objects on this list, because that happens to be the only
1908 time we need to know the normal symbol corresponding to a
1909 weak symbol, and the information is time consuming to
1910 figure out. If the weakdef field is not already NULL,
1911 then this symbol was already defined by some previous
1912 dynamic object, and we will be using that previous
1913 definition anyhow. */
1920 /* Set the alignment of a common symbol. */
1921 if (sym.st_shndx == SHN_COMMON
1922 && h->root.type == bfd_link_hash_common)
1926 align = bfd_log2 (sym.st_value);
1927 if (align > old_alignment
1928 /* Permit an alignment power of zero if an alignment of one
1929 is specified and no other alignments have been specified. */
1930 || (sym.st_value == 1 && old_alignment == 0))
1931 h->root.u.c.p->alignment_power = align;
1934 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1940 /* Remember the symbol size and type. */
1941 if (sym.st_size != 0
1942 && (definition || h->size == 0))
1944 if (h->size != 0 && h->size != sym.st_size && ! size_change_ok)
1945 (*_bfd_error_handler)
1946 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1947 name, (unsigned long) h->size, (unsigned long) sym.st_size,
1948 bfd_archive_filename (abfd));
1950 h->size = sym.st_size;
1953 /* If this is a common symbol, then we always want H->SIZE
1954 to be the size of the common symbol. The code just above
1955 won't fix the size if a common symbol becomes larger. We
1956 don't warn about a size change here, because that is
1957 covered by --warn-common. */
1958 if (h->root.type == bfd_link_hash_common)
1959 h->size = h->root.u.c.size;
1961 if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE
1962 && (definition || h->type == STT_NOTYPE))
1964 if (h->type != STT_NOTYPE
1965 && h->type != ELF_ST_TYPE (sym.st_info)
1966 && ! type_change_ok)
1967 (*_bfd_error_handler)
1968 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1969 name, h->type, ELF_ST_TYPE (sym.st_info),
1970 bfd_archive_filename (abfd));
1972 h->type = ELF_ST_TYPE (sym.st_info);
1975 /* If st_other has a processor-specific meaning, specific code
1976 might be needed here. */
1977 if (sym.st_other != 0)
1979 /* Combine visibilities, using the most constraining one. */
1980 unsigned char hvis = ELF_ST_VISIBILITY (h->other);
1981 unsigned char symvis = ELF_ST_VISIBILITY (sym.st_other);
1983 if (symvis && (hvis > symvis || hvis == 0))
1984 h->other = sym.st_other;
1986 /* If neither has visibility, use the st_other of the
1987 definition. This is an arbitrary choice, since the
1988 other bits have no general meaning. */
1989 if (!symvis && !hvis
1990 && (definition || h->other == 0))
1991 h->other = sym.st_other;
1994 /* Set a flag in the hash table entry indicating the type of
1995 reference or definition we just found. Keep a count of
1996 the number of dynamic symbols we find. A dynamic symbol
1997 is one which is referenced or defined by both a regular
1998 object and a shared object. */
1999 old_flags = h->elf_link_hash_flags;
2005 new_flag = ELF_LINK_HASH_REF_REGULAR;
2006 if (bind != STB_WEAK)
2007 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
2010 new_flag = ELF_LINK_HASH_DEF_REGULAR;
2012 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2013 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
2019 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
2021 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
2022 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
2023 | ELF_LINK_HASH_REF_REGULAR)) != 0
2024 || (h->weakdef != NULL
2026 && h->weakdef->dynindx != -1))
2030 h->elf_link_hash_flags |= new_flag;
2032 /* Check to see if we need to add an indirect symbol for
2033 the default name. */
2034 if (definition || h->root.type == bfd_link_hash_common)
2035 if (! elf_add_default_symbol (abfd, info, h, name, &sym,
2036 &sec, &value, &dynsym,
2037 override, dt_needed))
2040 if (dynsym && h->dynindx == -1)
2042 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2044 if (h->weakdef != NULL
2046 && h->weakdef->dynindx == -1)
2048 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2052 else if (dynsym && h->dynindx != -1)
2053 /* If the symbol already has a dynamic index, but
2054 visibility says it should not be visible, turn it into
2056 switch (ELF_ST_VISIBILITY (h->other))
2060 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
2061 (*bed->elf_backend_hide_symbol) (info, h);
2062 _bfd_elf_strtab_delref (hash_table->dynstr,
2067 if (dt_needed && definition
2068 && (h->elf_link_hash_flags
2069 & ELF_LINK_HASH_REF_REGULAR) != 0)
2071 bfd_size_type oldsize;
2072 bfd_size_type strindex;
2074 if (! is_elf_hash_table (info))
2077 /* The symbol from a DT_NEEDED object is referenced from
2078 the regular object to create a dynamic executable. We
2079 have to make sure there is a DT_NEEDED entry for it. */
2082 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2083 strindex = _bfd_elf_strtab_add (hash_table->dynstr,
2084 elf_dt_soname (abfd), false);
2085 if (strindex == (bfd_size_type) -1)
2088 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2091 Elf_External_Dyn *dyncon, *dynconend;
2093 sdyn = bfd_get_section_by_name (hash_table->dynobj,
2095 BFD_ASSERT (sdyn != NULL);
2097 dyncon = (Elf_External_Dyn *) sdyn->contents;
2098 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2100 for (; dyncon < dynconend; dyncon++)
2102 Elf_Internal_Dyn dyn;
2104 elf_swap_dyn_in (hash_table->dynobj,
2106 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
2107 dyn.d_un.d_val != strindex);
2111 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
2117 /* Now set the weakdefs field correctly for all the weak defined
2118 symbols we found. The only way to do this is to search all the
2119 symbols. Since we only need the information for non functions in
2120 dynamic objects, that's the only time we actually put anything on
2121 the list WEAKS. We need this information so that if a regular
2122 object refers to a symbol defined weakly in a dynamic object, the
2123 real symbol in the dynamic object is also put in the dynamic
2124 symbols; we also must arrange for both symbols to point to the
2125 same memory location. We could handle the general case of symbol
2126 aliasing, but a general symbol alias can only be generated in
2127 assembler code, handling it correctly would be very time
2128 consuming, and other ELF linkers don't handle general aliasing
2130 while (weaks != NULL)
2132 struct elf_link_hash_entry *hlook;
2135 struct elf_link_hash_entry **hpp;
2136 struct elf_link_hash_entry **hppend;
2139 weaks = hlook->weakdef;
2140 hlook->weakdef = NULL;
2142 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
2143 || hlook->root.type == bfd_link_hash_defweak
2144 || hlook->root.type == bfd_link_hash_common
2145 || hlook->root.type == bfd_link_hash_indirect);
2146 slook = hlook->root.u.def.section;
2147 vlook = hlook->root.u.def.value;
2149 hpp = elf_sym_hashes (abfd);
2150 hppend = hpp + extsymcount;
2151 for (; hpp < hppend; hpp++)
2153 struct elf_link_hash_entry *h;
2156 if (h != NULL && h != hlook
2157 && h->root.type == bfd_link_hash_defined
2158 && h->root.u.def.section == slook
2159 && h->root.u.def.value == vlook)
2163 /* If the weak definition is in the list of dynamic
2164 symbols, make sure the real definition is put there
2166 if (hlook->dynindx != -1
2167 && h->dynindx == -1)
2169 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2173 /* If the real definition is in the list of dynamic
2174 symbols, make sure the weak definition is put there
2175 as well. If we don't do this, then the dynamic
2176 loader might not merge the entries for the real
2177 definition and the weak definition. */
2178 if (h->dynindx != -1
2179 && hlook->dynindx == -1)
2181 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
2196 if (extversym != NULL)
2202 /* If this object is the same format as the output object, and it is
2203 not a shared library, then let the backend look through the
2206 This is required to build global offset table entries and to
2207 arrange for dynamic relocs. It is not required for the
2208 particular common case of linking non PIC code, even when linking
2209 against shared libraries, but unfortunately there is no way of
2210 knowing whether an object file has been compiled PIC or not.
2211 Looking through the relocs is not particularly time consuming.
2212 The problem is that we must either (1) keep the relocs in memory,
2213 which causes the linker to require additional runtime memory or
2214 (2) read the relocs twice from the input file, which wastes time.
2215 This would be a good case for using mmap.
2217 I have no idea how to handle linking PIC code into a file of a
2218 different format. It probably can't be done. */
2219 check_relocs = get_elf_backend_data (abfd)->check_relocs;
2221 && abfd->xvec == info->hash->creator
2222 && check_relocs != NULL)
2226 for (o = abfd->sections; o != NULL; o = o->next)
2228 Elf_Internal_Rela *internal_relocs;
2231 if ((o->flags & SEC_RELOC) == 0
2232 || o->reloc_count == 0
2233 || ((info->strip == strip_all || info->strip == strip_debugger)
2234 && (o->flags & SEC_DEBUGGING) != 0)
2235 || bfd_is_abs_section (o->output_section))
2238 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
2239 (abfd, o, (PTR) NULL,
2240 (Elf_Internal_Rela *) NULL,
2241 info->keep_memory));
2242 if (internal_relocs == NULL)
2245 ok = (*check_relocs) (abfd, info, o, internal_relocs);
2247 if (! info->keep_memory)
2248 free (internal_relocs);
2255 /* If this is a non-traditional, non-relocateable link, try to
2256 optimize the handling of the .stab/.stabstr sections. */
2258 && ! info->relocateable
2259 && ! info->traditional_format
2260 && info->hash->creator->flavour == bfd_target_elf_flavour
2261 && is_elf_hash_table (info)
2262 && (info->strip != strip_all && info->strip != strip_debugger))
2264 asection *stab, *stabstr;
2266 stab = bfd_get_section_by_name (abfd, ".stab");
2267 if (stab != NULL && !(stab->flags & SEC_MERGE))
2269 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
2271 if (stabstr != NULL)
2273 struct bfd_elf_section_data *secdata;
2275 secdata = elf_section_data (stab);
2276 if (! _bfd_link_section_stabs (abfd,
2277 & hash_table->stab_info,
2279 &secdata->sec_info))
2281 if (secdata->sec_info)
2282 secdata->sec_info_type = ELF_INFO_TYPE_STABS;
2287 if (! info->relocateable && ! dynamic
2288 && is_elf_hash_table (info))
2292 for (s = abfd->sections; s != NULL; s = s->next)
2293 if (s->flags & SEC_MERGE)
2295 struct bfd_elf_section_data *secdata;
2297 secdata = elf_section_data (s);
2298 if (! _bfd_merge_section (abfd,
2299 & hash_table->merge_info,
2300 s, &secdata->sec_info))
2302 else if (secdata->sec_info)
2303 secdata->sec_info_type = ELF_INFO_TYPE_MERGE;
2314 if (extversym != NULL)
2319 /* Create some sections which will be filled in with dynamic linking
2320 information. ABFD is an input file which requires dynamic sections
2321 to be created. The dynamic sections take up virtual memory space
2322 when the final executable is run, so we need to create them before
2323 addresses are assigned to the output sections. We work out the
2324 actual contents and size of these sections later. */
2327 elf_link_create_dynamic_sections (abfd, info)
2329 struct bfd_link_info *info;
2332 register asection *s;
2333 struct elf_link_hash_entry *h;
2334 struct elf_backend_data *bed;
2336 if (! is_elf_hash_table (info))
2339 if (elf_hash_table (info)->dynamic_sections_created)
2342 /* Make sure that all dynamic sections use the same input BFD. */
2343 if (elf_hash_table (info)->dynobj == NULL)
2344 elf_hash_table (info)->dynobj = abfd;
2346 abfd = elf_hash_table (info)->dynobj;
2348 /* Note that we set the SEC_IN_MEMORY flag for all of these
2350 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2351 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2353 /* A dynamically linked executable has a .interp section, but a
2354 shared library does not. */
2357 s = bfd_make_section (abfd, ".interp");
2359 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2363 if (! info->traditional_format
2364 && info->hash->creator->flavour == bfd_target_elf_flavour)
2366 s = bfd_make_section (abfd, ".eh_frame_hdr");
2368 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2369 || ! bfd_set_section_alignment (abfd, s, 2))
2373 /* Create sections to hold version informations. These are removed
2374 if they are not needed. */
2375 s = bfd_make_section (abfd, ".gnu.version_d");
2377 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2378 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2381 s = bfd_make_section (abfd, ".gnu.version");
2383 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2384 || ! bfd_set_section_alignment (abfd, s, 1))
2387 s = bfd_make_section (abfd, ".gnu.version_r");
2389 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2390 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2393 s = bfd_make_section (abfd, ".dynsym");
2395 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2396 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2399 s = bfd_make_section (abfd, ".dynstr");
2401 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2404 /* Create a strtab to hold the dynamic symbol names. */
2405 if (elf_hash_table (info)->dynstr == NULL)
2407 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
2408 if (elf_hash_table (info)->dynstr == NULL)
2412 s = bfd_make_section (abfd, ".dynamic");
2414 || ! bfd_set_section_flags (abfd, s, flags)
2415 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2418 /* The special symbol _DYNAMIC is always set to the start of the
2419 .dynamic section. This call occurs before we have processed the
2420 symbols for any dynamic object, so we don't have to worry about
2421 overriding a dynamic definition. We could set _DYNAMIC in a
2422 linker script, but we only want to define it if we are, in fact,
2423 creating a .dynamic section. We don't want to define it if there
2424 is no .dynamic section, since on some ELF platforms the start up
2425 code examines it to decide how to initialize the process. */
2427 if (! (_bfd_generic_link_add_one_symbol
2428 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
2429 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
2430 (struct bfd_link_hash_entry **) &h)))
2432 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2433 h->type = STT_OBJECT;
2436 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2439 bed = get_elf_backend_data (abfd);
2441 s = bfd_make_section (abfd, ".hash");
2443 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2444 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2446 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
2448 /* Let the backend create the rest of the sections. This lets the
2449 backend set the right flags. The backend will normally create
2450 the .got and .plt sections. */
2451 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
2454 elf_hash_table (info)->dynamic_sections_created = true;
2459 /* Add an entry to the .dynamic table. */
2462 elf_add_dynamic_entry (info, tag, val)
2463 struct bfd_link_info *info;
2467 Elf_Internal_Dyn dyn;
2470 bfd_size_type newsize;
2471 bfd_byte *newcontents;
2473 if (! is_elf_hash_table (info))
2476 dynobj = elf_hash_table (info)->dynobj;
2478 s = bfd_get_section_by_name (dynobj, ".dynamic");
2479 BFD_ASSERT (s != NULL);
2481 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
2482 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
2483 if (newcontents == NULL)
2487 dyn.d_un.d_val = val;
2488 elf_swap_dyn_out (dynobj, &dyn,
2489 (Elf_External_Dyn *) (newcontents + s->_raw_size));
2491 s->_raw_size = newsize;
2492 s->contents = newcontents;
2497 /* Record a new local dynamic symbol. */
2500 elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
2501 struct bfd_link_info *info;
2505 struct elf_link_local_dynamic_entry *entry;
2506 struct elf_link_hash_table *eht;
2507 struct elf_strtab_hash *dynstr;
2508 Elf_External_Sym esym;
2509 Elf_External_Sym_Shndx eshndx;
2510 Elf_External_Sym_Shndx *shndx;
2511 unsigned long dynstr_index;
2516 if (! is_elf_hash_table (info))
2519 /* See if the entry exists already. */
2520 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2521 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
2524 entry = (struct elf_link_local_dynamic_entry *)
2525 bfd_alloc (input_bfd, (bfd_size_type) sizeof (*entry));
2529 /* Go find the symbol, so that we can find it's name. */
2530 amt = sizeof (Elf_External_Sym);
2531 pos = elf_tdata (input_bfd)->symtab_hdr.sh_offset + input_indx * amt;
2532 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
2533 || bfd_bread ((PTR) &esym, amt, input_bfd) != amt)
2536 if (elf_tdata (input_bfd)->symtab_shndx_hdr.sh_size != 0)
2538 amt = sizeof (Elf_External_Sym_Shndx);
2539 pos = elf_tdata (input_bfd)->symtab_shndx_hdr.sh_offset;
2540 pos += input_indx * amt;
2542 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
2543 || bfd_bread ((PTR) shndx, amt, input_bfd) != amt)
2546 elf_swap_symbol_in (input_bfd, &esym, shndx, &entry->isym);
2548 name = (bfd_elf_string_from_elf_section
2549 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
2550 entry->isym.st_name));
2552 dynstr = elf_hash_table (info)->dynstr;
2555 /* Create a strtab to hold the dynamic symbol names. */
2556 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
2561 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
2562 if (dynstr_index == (unsigned long) -1)
2564 entry->isym.st_name = dynstr_index;
2566 eht = elf_hash_table (info);
2568 entry->next = eht->dynlocal;
2569 eht->dynlocal = entry;
2570 entry->input_bfd = input_bfd;
2571 entry->input_indx = input_indx;
2574 /* Whatever binding the symbol had before, it's now local. */
2576 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
2578 /* The dynindx will be set at the end of size_dynamic_sections. */
2583 /* Read and swap the relocs from the section indicated by SHDR. This
2584 may be either a REL or a RELA section. The relocations are
2585 translated into RELA relocations and stored in INTERNAL_RELOCS,
2586 which should have already been allocated to contain enough space.
2587 The EXTERNAL_RELOCS are a buffer where the external form of the
2588 relocations should be stored.
2590 Returns false if something goes wrong. */
2593 elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
2596 Elf_Internal_Shdr *shdr;
2597 PTR external_relocs;
2598 Elf_Internal_Rela *internal_relocs;
2600 struct elf_backend_data *bed;
2603 /* If there aren't any relocations, that's OK. */
2607 /* Position ourselves at the start of the section. */
2608 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2611 /* Read the relocations. */
2612 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2615 bed = get_elf_backend_data (abfd);
2617 /* Convert the external relocations to the internal format. */
2618 if (shdr->sh_entsize == sizeof (Elf_External_Rel))
2620 Elf_External_Rel *erel;
2621 Elf_External_Rel *erelend;
2622 Elf_Internal_Rela *irela;
2623 Elf_Internal_Rel *irel;
2625 erel = (Elf_External_Rel *) external_relocs;
2626 erelend = erel + NUM_SHDR_ENTRIES (shdr);
2627 irela = internal_relocs;
2628 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
2629 irel = bfd_alloc (abfd, amt);
2630 for (; erel < erelend; erel++, irela += bed->s->int_rels_per_ext_rel)
2634 if (bed->s->swap_reloc_in)
2635 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
2637 elf_swap_reloc_in (abfd, erel, irel);
2639 for (i = 0; i < bed->s->int_rels_per_ext_rel; ++i)
2641 irela[i].r_offset = irel[i].r_offset;
2642 irela[i].r_info = irel[i].r_info;
2643 irela[i].r_addend = 0;
2649 Elf_External_Rela *erela;
2650 Elf_External_Rela *erelaend;
2651 Elf_Internal_Rela *irela;
2653 BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela));
2655 erela = (Elf_External_Rela *) external_relocs;
2656 erelaend = erela + NUM_SHDR_ENTRIES (shdr);
2657 irela = internal_relocs;
2658 for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel)
2660 if (bed->s->swap_reloca_in)
2661 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
2663 elf_swap_reloca_in (abfd, erela, irela);
2670 /* Read and swap the relocs for a section O. They may have been
2671 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2672 not NULL, they are used as buffers to read into. They are known to
2673 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2674 the return value is allocated using either malloc or bfd_alloc,
2675 according to the KEEP_MEMORY argument. If O has two relocation
2676 sections (both REL and RELA relocations), then the REL_HDR
2677 relocations will appear first in INTERNAL_RELOCS, followed by the
2678 REL_HDR2 relocations. */
2681 NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
2685 PTR external_relocs;
2686 Elf_Internal_Rela *internal_relocs;
2687 boolean keep_memory;
2689 Elf_Internal_Shdr *rel_hdr;
2691 Elf_Internal_Rela *alloc2 = NULL;
2692 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2694 if (elf_section_data (o)->relocs != NULL)
2695 return elf_section_data (o)->relocs;
2697 if (o->reloc_count == 0)
2700 rel_hdr = &elf_section_data (o)->rel_hdr;
2702 if (internal_relocs == NULL)
2706 size = o->reloc_count;
2707 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2709 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2711 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2712 if (internal_relocs == NULL)
2716 if (external_relocs == NULL)
2718 bfd_size_type size = rel_hdr->sh_size;
2720 if (elf_section_data (o)->rel_hdr2)
2721 size += elf_section_data (o)->rel_hdr2->sh_size;
2722 alloc1 = (PTR) bfd_malloc (size);
2725 external_relocs = alloc1;
2728 if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
2732 if (!elf_link_read_relocs_from_section
2734 elf_section_data (o)->rel_hdr2,
2735 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2736 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2737 * bed->s->int_rels_per_ext_rel)))
2740 /* Cache the results for next time, if we can. */
2742 elf_section_data (o)->relocs = internal_relocs;
2747 /* Don't free alloc2, since if it was allocated we are passing it
2748 back (under the name of internal_relocs). */
2750 return internal_relocs;
2760 /* Record an assignment to a symbol made by a linker script. We need
2761 this in case some dynamic object refers to this symbol. */
2764 NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
2765 bfd *output_bfd ATTRIBUTE_UNUSED;
2766 struct bfd_link_info *info;
2770 struct elf_link_hash_entry *h;
2772 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2775 h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
2779 if (h->root.type == bfd_link_hash_new)
2780 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
2782 /* If this symbol is being provided by the linker script, and it is
2783 currently defined by a dynamic object, but not by a regular
2784 object, then mark it as undefined so that the generic linker will
2785 force the correct value. */
2787 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2788 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2789 h->root.type = bfd_link_hash_undefined;
2791 /* If this symbol is not being provided by the linker script, and it is
2792 currently defined by a dynamic object, but not by a regular object,
2793 then clear out any version information because the symbol will not be
2794 associated with the dynamic object any more. */
2796 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2797 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2798 h->verinfo.verdef = NULL;
2800 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2802 /* When possible, keep the original type of the symbol. */
2803 if (h->type == STT_NOTYPE)
2804 h->type = STT_OBJECT;
2806 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2807 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
2809 && h->dynindx == -1)
2811 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2814 /* If this is a weak defined symbol, and we know a corresponding
2815 real symbol from the same dynamic object, make sure the real
2816 symbol is also made into a dynamic symbol. */
2817 if (h->weakdef != NULL
2818 && h->weakdef->dynindx == -1)
2820 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2828 /* This structure is used to pass information to
2829 elf_link_assign_sym_version. */
2831 struct elf_assign_sym_version_info
2835 /* General link information. */
2836 struct bfd_link_info *info;
2838 struct bfd_elf_version_tree *verdefs;
2839 /* Whether we had a failure. */
2843 /* This structure is used to pass information to
2844 elf_link_find_version_dependencies. */
2846 struct elf_find_verdep_info
2850 /* General link information. */
2851 struct bfd_link_info *info;
2852 /* The number of dependencies. */
2854 /* Whether we had a failure. */
2858 /* Array used to determine the number of hash table buckets to use
2859 based on the number of symbols there are. If there are fewer than
2860 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2861 fewer than 37 we use 17 buckets, and so forth. We never use more
2862 than 32771 buckets. */
2864 static const size_t elf_buckets[] =
2866 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2870 /* Compute bucket count for hashing table. We do not use a static set
2871 of possible tables sizes anymore. Instead we determine for all
2872 possible reasonable sizes of the table the outcome (i.e., the
2873 number of collisions etc) and choose the best solution. The
2874 weighting functions are not too simple to allow the table to grow
2875 without bounds. Instead one of the weighting factors is the size.
2876 Therefore the result is always a good payoff between few collisions
2877 (= short chain lengths) and table size. */
2879 compute_bucket_count (info)
2880 struct bfd_link_info *info;
2882 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
2883 size_t best_size = 0;
2884 unsigned long int *hashcodes;
2885 unsigned long int *hashcodesp;
2886 unsigned long int i;
2889 /* Compute the hash values for all exported symbols. At the same
2890 time store the values in an array so that we could use them for
2893 amt *= sizeof (unsigned long int);
2894 hashcodes = (unsigned long int *) bfd_malloc (amt);
2895 if (hashcodes == NULL)
2897 hashcodesp = hashcodes;
2899 /* Put all hash values in HASHCODES. */
2900 elf_link_hash_traverse (elf_hash_table (info),
2901 elf_collect_hash_codes, &hashcodesp);
2903 /* We have a problem here. The following code to optimize the table
2904 size requires an integer type with more the 32 bits. If
2905 BFD_HOST_U_64_BIT is set we know about such a type. */
2906 #ifdef BFD_HOST_U_64_BIT
2907 if (info->optimize == true)
2909 unsigned long int nsyms = hashcodesp - hashcodes;
2912 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
2913 unsigned long int *counts ;
2915 /* Possible optimization parameters: if we have NSYMS symbols we say
2916 that the hashing table must at least have NSYMS/4 and at most
2918 minsize = nsyms / 4;
2921 best_size = maxsize = nsyms * 2;
2923 /* Create array where we count the collisions in. We must use bfd_malloc
2924 since the size could be large. */
2926 amt *= sizeof (unsigned long int);
2927 counts = (unsigned long int *) bfd_malloc (amt);
2934 /* Compute the "optimal" size for the hash table. The criteria is a
2935 minimal chain length. The minor criteria is (of course) the size
2937 for (i = minsize; i < maxsize; ++i)
2939 /* Walk through the array of hashcodes and count the collisions. */
2940 BFD_HOST_U_64_BIT max;
2941 unsigned long int j;
2942 unsigned long int fact;
2944 memset (counts, '\0', i * sizeof (unsigned long int));
2946 /* Determine how often each hash bucket is used. */
2947 for (j = 0; j < nsyms; ++j)
2948 ++counts[hashcodes[j] % i];
2950 /* For the weight function we need some information about the
2951 pagesize on the target. This is information need not be 100%
2952 accurate. Since this information is not available (so far) we
2953 define it here to a reasonable default value. If it is crucial
2954 to have a better value some day simply define this value. */
2955 # ifndef BFD_TARGET_PAGESIZE
2956 # define BFD_TARGET_PAGESIZE (4096)
2959 /* We in any case need 2 + NSYMS entries for the size values and
2961 max = (2 + nsyms) * (ARCH_SIZE / 8);
2964 /* Variant 1: optimize for short chains. We add the squares
2965 of all the chain lengths (which favous many small chain
2966 over a few long chains). */
2967 for (j = 0; j < i; ++j)
2968 max += counts[j] * counts[j];
2970 /* This adds penalties for the overall size of the table. */
2971 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2974 /* Variant 2: Optimize a lot more for small table. Here we
2975 also add squares of the size but we also add penalties for
2976 empty slots (the +1 term). */
2977 for (j = 0; j < i; ++j)
2978 max += (1 + counts[j]) * (1 + counts[j]);
2980 /* The overall size of the table is considered, but not as
2981 strong as in variant 1, where it is squared. */
2982 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2986 /* Compare with current best results. */
2987 if (max < best_chlen)
2997 #endif /* defined (BFD_HOST_U_64_BIT) */
2999 /* This is the fallback solution if no 64bit type is available or if we
3000 are not supposed to spend much time on optimizations. We select the
3001 bucket count using a fixed set of numbers. */
3002 for (i = 0; elf_buckets[i] != 0; i++)
3004 best_size = elf_buckets[i];
3005 if (dynsymcount < elf_buckets[i + 1])
3010 /* Free the arrays we needed. */
3016 /* Set up the sizes and contents of the ELF dynamic sections. This is
3017 called by the ELF linker emulation before_allocation routine. We
3018 must set the sizes of the sections before the linker sets the
3019 addresses of the various sections. */
3022 NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
3024 auxiliary_filters, info, sinterpptr,
3029 const char *filter_shlib;
3030 const char * const *auxiliary_filters;
3031 struct bfd_link_info *info;
3032 asection **sinterpptr;
3033 struct bfd_elf_version_tree *verdefs;
3035 bfd_size_type soname_indx;
3037 struct elf_backend_data *bed;
3038 struct elf_assign_sym_version_info asvinfo;
3042 soname_indx = (bfd_size_type) -1;
3044 if (info->hash->creator->flavour != bfd_target_elf_flavour)
3047 if (! is_elf_hash_table (info))
3050 /* Any syms created from now on start with -1 in
3051 got.refcount/offset and plt.refcount/offset. */
3052 elf_hash_table (info)->init_refcount = -1;
3054 /* The backend may have to create some sections regardless of whether
3055 we're dynamic or not. */
3056 bed = get_elf_backend_data (output_bfd);
3057 if (bed->elf_backend_always_size_sections
3058 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
3061 dynobj = elf_hash_table (info)->dynobj;
3063 /* If there were no dynamic objects in the link, there is nothing to
3068 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
3071 if (elf_hash_table (info)->dynamic_sections_created)
3073 struct elf_info_failed eif;
3074 struct elf_link_hash_entry *h;
3077 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
3078 BFD_ASSERT (*sinterpptr != NULL || info->shared);
3082 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3084 if (soname_indx == (bfd_size_type) -1
3085 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SONAME,
3092 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMBOLIC,
3095 info->flags |= DF_SYMBOLIC;
3102 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
3104 if (info->new_dtags)
3105 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
3106 if (indx == (bfd_size_type) -1
3107 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_RPATH, indx)
3109 && ! elf_add_dynamic_entry (info, (bfd_vma) DT_RUNPATH,
3114 if (filter_shlib != NULL)
3118 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3119 filter_shlib, true);
3120 if (indx == (bfd_size_type) -1
3121 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_FILTER, indx))
3125 if (auxiliary_filters != NULL)
3127 const char * const *p;
3129 for (p = auxiliary_filters; *p != NULL; p++)
3133 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3135 if (indx == (bfd_size_type) -1
3136 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_AUXILIARY,
3143 eif.verdefs = verdefs;
3146 /* If we are supposed to export all symbols into the dynamic symbol
3147 table (this is not the normal case), then do so. */
3148 if (info->export_dynamic)
3150 elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
3156 /* Attach all the symbols to their version information. */
3157 asvinfo.output_bfd = output_bfd;
3158 asvinfo.info = info;
3159 asvinfo.verdefs = verdefs;
3160 asvinfo.failed = false;
3162 elf_link_hash_traverse (elf_hash_table (info),
3163 elf_link_assign_sym_version,
3168 /* Find all symbols which were defined in a dynamic object and make
3169 the backend pick a reasonable value for them. */
3170 elf_link_hash_traverse (elf_hash_table (info),
3171 elf_adjust_dynamic_symbol,
3176 /* Add some entries to the .dynamic section. We fill in some of the
3177 values later, in elf_bfd_final_link, but we must add the entries
3178 now so that we know the final size of the .dynamic section. */
3180 /* If there are initialization and/or finalization functions to
3181 call then add the corresponding DT_INIT/DT_FINI entries. */
3182 h = (info->init_function
3183 ? elf_link_hash_lookup (elf_hash_table (info),
3184 info->init_function, false,
3188 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3189 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3191 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_INIT, (bfd_vma) 0))
3194 h = (info->fini_function
3195 ? elf_link_hash_lookup (elf_hash_table (info),
3196 info->fini_function, false,
3200 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3201 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3203 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FINI, (bfd_vma) 0))
3207 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
3208 /* If .dynstr is excluded from the link, we don't want any of
3209 these tags. Strictly, we should be checking each section
3210 individually; This quick check covers for the case where
3211 someone does a /DISCARD/ : { *(*) }. */
3212 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
3214 bfd_size_type strsize;
3216 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3217 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_HASH, (bfd_vma) 0)
3218 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRTAB, (bfd_vma) 0)
3219 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMTAB, (bfd_vma) 0)
3220 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRSZ, strsize)
3221 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMENT,
3222 (bfd_vma) sizeof (Elf_External_Sym)))
3227 /* The backend must work out the sizes of all the other dynamic
3229 if (bed->elf_backend_size_dynamic_sections
3230 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
3233 if (elf_hash_table (info)->dynamic_sections_created)
3235 bfd_size_type dynsymcount;
3237 size_t bucketcount = 0;
3238 size_t hash_entry_size;
3239 unsigned int dtagcount;
3241 /* Set up the version definition section. */
3242 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3243 BFD_ASSERT (s != NULL);
3245 /* We may have created additional version definitions if we are
3246 just linking a regular application. */
3247 verdefs = asvinfo.verdefs;
3249 /* Skip anonymous version tag. */
3250 if (verdefs != NULL && verdefs->vernum == 0)
3251 verdefs = verdefs->next;
3253 if (verdefs == NULL)
3254 _bfd_strip_section_from_output (info, s);
3259 struct bfd_elf_version_tree *t;
3261 Elf_Internal_Verdef def;
3262 Elf_Internal_Verdaux defaux;
3267 /* Make space for the base version. */
3268 size += sizeof (Elf_External_Verdef);
3269 size += sizeof (Elf_External_Verdaux);
3272 for (t = verdefs; t != NULL; t = t->next)
3274 struct bfd_elf_version_deps *n;
3276 size += sizeof (Elf_External_Verdef);
3277 size += sizeof (Elf_External_Verdaux);
3280 for (n = t->deps; n != NULL; n = n->next)
3281 size += sizeof (Elf_External_Verdaux);
3284 s->_raw_size = size;
3285 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3286 if (s->contents == NULL && s->_raw_size != 0)
3289 /* Fill in the version definition section. */
3293 def.vd_version = VER_DEF_CURRENT;
3294 def.vd_flags = VER_FLG_BASE;
3297 def.vd_aux = sizeof (Elf_External_Verdef);
3298 def.vd_next = (sizeof (Elf_External_Verdef)
3299 + sizeof (Elf_External_Verdaux));
3301 if (soname_indx != (bfd_size_type) -1)
3303 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3305 def.vd_hash = bfd_elf_hash (soname);
3306 defaux.vda_name = soname_indx;
3313 name = basename (output_bfd->filename);
3314 def.vd_hash = bfd_elf_hash (name);
3315 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3317 if (indx == (bfd_size_type) -1)
3319 defaux.vda_name = indx;
3321 defaux.vda_next = 0;
3323 _bfd_elf_swap_verdef_out (output_bfd, &def,
3324 (Elf_External_Verdef *) p);
3325 p += sizeof (Elf_External_Verdef);
3326 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3327 (Elf_External_Verdaux *) p);
3328 p += sizeof (Elf_External_Verdaux);
3330 for (t = verdefs; t != NULL; t = t->next)
3333 struct bfd_elf_version_deps *n;
3334 struct elf_link_hash_entry *h;
3337 for (n = t->deps; n != NULL; n = n->next)
3340 /* Add a symbol representing this version. */
3342 if (! (_bfd_generic_link_add_one_symbol
3343 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
3344 (bfd_vma) 0, (const char *) NULL, false,
3345 get_elf_backend_data (dynobj)->collect,
3346 (struct bfd_link_hash_entry **) &h)))
3348 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
3349 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3350 h->type = STT_OBJECT;
3351 h->verinfo.vertree = t;
3353 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
3356 def.vd_version = VER_DEF_CURRENT;
3358 if (t->globals == NULL && t->locals == NULL && ! t->used)
3359 def.vd_flags |= VER_FLG_WEAK;
3360 def.vd_ndx = t->vernum + 1;
3361 def.vd_cnt = cdeps + 1;
3362 def.vd_hash = bfd_elf_hash (t->name);
3363 def.vd_aux = sizeof (Elf_External_Verdef);
3364 if (t->next != NULL)
3365 def.vd_next = (sizeof (Elf_External_Verdef)
3366 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
3370 _bfd_elf_swap_verdef_out (output_bfd, &def,
3371 (Elf_External_Verdef *) p);
3372 p += sizeof (Elf_External_Verdef);
3374 defaux.vda_name = h->dynstr_index;
3375 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3377 if (t->deps == NULL)
3378 defaux.vda_next = 0;
3380 defaux.vda_next = sizeof (Elf_External_Verdaux);
3381 t->name_indx = defaux.vda_name;
3383 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3384 (Elf_External_Verdaux *) p);
3385 p += sizeof (Elf_External_Verdaux);
3387 for (n = t->deps; n != NULL; n = n->next)
3389 if (n->version_needed == NULL)
3391 /* This can happen if there was an error in the
3393 defaux.vda_name = 0;
3397 defaux.vda_name = n->version_needed->name_indx;
3398 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3401 if (n->next == NULL)
3402 defaux.vda_next = 0;
3404 defaux.vda_next = sizeof (Elf_External_Verdaux);
3406 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3407 (Elf_External_Verdaux *) p);
3408 p += sizeof (Elf_External_Verdaux);
3412 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEF, (bfd_vma) 0)
3413 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEFNUM,
3417 elf_tdata (output_bfd)->cverdefs = cdefs;
3420 if (info->new_dtags && info->flags)
3422 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags))
3429 info->flags_1 &= ~ (DF_1_INITFIRST
3432 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS_1,
3437 /* Work out the size of the version reference section. */
3439 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3440 BFD_ASSERT (s != NULL);
3442 struct elf_find_verdep_info sinfo;
3444 sinfo.output_bfd = output_bfd;
3446 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
3447 if (sinfo.vers == 0)
3449 sinfo.failed = false;
3451 elf_link_hash_traverse (elf_hash_table (info),
3452 elf_link_find_version_dependencies,
3455 if (elf_tdata (output_bfd)->verref == NULL)
3456 _bfd_strip_section_from_output (info, s);
3459 Elf_Internal_Verneed *t;
3464 /* Build the version definition section. */
3467 for (t = elf_tdata (output_bfd)->verref;
3471 Elf_Internal_Vernaux *a;
3473 size += sizeof (Elf_External_Verneed);
3475 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3476 size += sizeof (Elf_External_Vernaux);
3479 s->_raw_size = size;
3480 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3481 if (s->contents == NULL)
3485 for (t = elf_tdata (output_bfd)->verref;
3490 Elf_Internal_Vernaux *a;
3494 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3497 t->vn_version = VER_NEED_CURRENT;
3499 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3500 elf_dt_name (t->vn_bfd) != NULL
3501 ? elf_dt_name (t->vn_bfd)
3502 : basename (t->vn_bfd->filename),
3504 if (indx == (bfd_size_type) -1)
3507 t->vn_aux = sizeof (Elf_External_Verneed);
3508 if (t->vn_nextref == NULL)
3511 t->vn_next = (sizeof (Elf_External_Verneed)
3512 + caux * sizeof (Elf_External_Vernaux));
3514 _bfd_elf_swap_verneed_out (output_bfd, t,
3515 (Elf_External_Verneed *) p);
3516 p += sizeof (Elf_External_Verneed);
3518 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3520 a->vna_hash = bfd_elf_hash (a->vna_nodename);
3521 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3522 a->vna_nodename, false);
3523 if (indx == (bfd_size_type) -1)
3526 if (a->vna_nextptr == NULL)
3529 a->vna_next = sizeof (Elf_External_Vernaux);
3531 _bfd_elf_swap_vernaux_out (output_bfd, a,
3532 (Elf_External_Vernaux *) p);
3533 p += sizeof (Elf_External_Vernaux);
3537 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEED,
3539 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEEDNUM,
3543 elf_tdata (output_bfd)->cverrefs = crefs;
3547 /* Assign dynsym indicies. In a shared library we generate a
3548 section symbol for each output section, which come first.
3549 Next come all of the back-end allocated local dynamic syms,
3550 followed by the rest of the global symbols. */
3552 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3554 /* Work out the size of the symbol version section. */
3555 s = bfd_get_section_by_name (dynobj, ".gnu.version");
3556 BFD_ASSERT (s != NULL);
3557 if (dynsymcount == 0
3558 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
3560 _bfd_strip_section_from_output (info, s);
3561 /* The DYNSYMCOUNT might have changed if we were going to
3562 output a dynamic symbol table entry for S. */
3563 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3567 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
3568 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3569 if (s->contents == NULL)
3572 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERSYM, (bfd_vma) 0))
3576 /* Set the size of the .dynsym and .hash sections. We counted
3577 the number of dynamic symbols in elf_link_add_object_symbols.
3578 We will build the contents of .dynsym and .hash when we build
3579 the final symbol table, because until then we do not know the
3580 correct value to give the symbols. We built the .dynstr
3581 section as we went along in elf_link_add_object_symbols. */
3582 s = bfd_get_section_by_name (dynobj, ".dynsym");
3583 BFD_ASSERT (s != NULL);
3584 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
3585 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3586 if (s->contents == NULL && s->_raw_size != 0)
3589 if (dynsymcount != 0)
3591 Elf_Internal_Sym isym;
3593 /* The first entry in .dynsym is a dummy symbol. */
3600 elf_swap_symbol_out (output_bfd, &isym, (PTR) s->contents, (PTR) 0);
3603 /* Compute the size of the hashing table. As a side effect this
3604 computes the hash values for all the names we export. */
3605 bucketcount = compute_bucket_count (info);
3607 s = bfd_get_section_by_name (dynobj, ".hash");
3608 BFD_ASSERT (s != NULL);
3609 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
3610 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
3611 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3612 if (s->contents == NULL)
3614 memset (s->contents, 0, (size_t) s->_raw_size);
3616 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount,
3618 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) dynsymcount,
3619 s->contents + hash_entry_size);
3621 elf_hash_table (info)->bucketcount = bucketcount;
3623 s = bfd_get_section_by_name (dynobj, ".dynstr");
3624 BFD_ASSERT (s != NULL);
3626 elf_finalize_dynstr (output_bfd, info);
3628 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3630 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
3631 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NULL, (bfd_vma) 0))
3638 /* This function is used to adjust offsets into .dynstr for
3639 dynamic symbols. This is called via elf_link_hash_traverse. */
3641 static boolean elf_adjust_dynstr_offsets
3642 PARAMS ((struct elf_link_hash_entry *, PTR));
3645 elf_adjust_dynstr_offsets (h, data)
3646 struct elf_link_hash_entry *h;
3649 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3651 if (h->dynindx != -1)
3652 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3656 /* Assign string offsets in .dynstr, update all structures referencing
3660 elf_finalize_dynstr (output_bfd, info)
3662 struct bfd_link_info *info;
3664 struct elf_link_local_dynamic_entry *entry;
3665 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
3666 bfd *dynobj = elf_hash_table (info)->dynobj;
3669 Elf_External_Dyn *dyncon, *dynconend;
3671 _bfd_elf_strtab_finalize (dynstr);
3672 size = _bfd_elf_strtab_size (dynstr);
3674 /* Update all .dynamic entries referencing .dynstr strings. */
3675 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3676 BFD_ASSERT (sdyn != NULL);
3678 dyncon = (Elf_External_Dyn *) sdyn->contents;
3679 dynconend = (Elf_External_Dyn *) (sdyn->contents +
3681 for (; dyncon < dynconend; dyncon++)
3683 Elf_Internal_Dyn dyn;
3685 elf_swap_dyn_in (dynobj, dyncon, & dyn);
3689 dyn.d_un.d_val = size;
3690 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3698 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3699 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3706 /* Now update local dynamic symbols. */
3707 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
3708 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3709 entry->isym.st_name);
3711 /* And the rest of dynamic symbols. */
3712 elf_link_hash_traverse (elf_hash_table (info),
3713 elf_adjust_dynstr_offsets, dynstr);
3715 /* Adjust version definitions. */
3716 if (elf_tdata (output_bfd)->cverdefs)
3721 Elf_Internal_Verdef def;
3722 Elf_Internal_Verdaux defaux;
3724 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3725 p = (bfd_byte *) s->contents;
3728 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3730 p += sizeof (Elf_External_Verdef);
3731 for (i = 0; i < def.vd_cnt; ++i)
3733 _bfd_elf_swap_verdaux_in (output_bfd,
3734 (Elf_External_Verdaux *) p, &defaux);
3735 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3737 _bfd_elf_swap_verdaux_out (output_bfd,
3738 &defaux, (Elf_External_Verdaux *) p);
3739 p += sizeof (Elf_External_Verdaux);
3742 while (def.vd_next);
3745 /* Adjust version references. */
3746 if (elf_tdata (output_bfd)->verref)
3751 Elf_Internal_Verneed need;
3752 Elf_Internal_Vernaux needaux;
3754 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3755 p = (bfd_byte *) s->contents;
3758 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3760 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3761 _bfd_elf_swap_verneed_out (output_bfd, &need,
3762 (Elf_External_Verneed *) p);
3763 p += sizeof (Elf_External_Verneed);
3764 for (i = 0; i < need.vn_cnt; ++i)
3766 _bfd_elf_swap_vernaux_in (output_bfd,
3767 (Elf_External_Vernaux *) p, &needaux);
3768 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3770 _bfd_elf_swap_vernaux_out (output_bfd,
3772 (Elf_External_Vernaux *) p);
3773 p += sizeof (Elf_External_Vernaux);
3776 while (need.vn_next);
3782 /* Fix up the flags for a symbol. This handles various cases which
3783 can only be fixed after all the input files are seen. This is
3784 currently called by both adjust_dynamic_symbol and
3785 assign_sym_version, which is unnecessary but perhaps more robust in
3786 the face of future changes. */
3789 elf_fix_symbol_flags (h, eif)
3790 struct elf_link_hash_entry *h;
3791 struct elf_info_failed *eif;
3793 /* If this symbol was mentioned in a non-ELF file, try to set
3794 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3795 permit a non-ELF file to correctly refer to a symbol defined in
3796 an ELF dynamic object. */
3797 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
3799 while (h->root.type == bfd_link_hash_indirect)
3800 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3802 if (h->root.type != bfd_link_hash_defined
3803 && h->root.type != bfd_link_hash_defweak)
3804 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3805 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3808 if (h->root.u.def.section->owner != NULL
3809 && (bfd_get_flavour (h->root.u.def.section->owner)
3810 == bfd_target_elf_flavour))
3811 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3812 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3814 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3817 if (h->dynindx == -1
3818 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3819 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3821 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3830 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3831 was first seen in a non-ELF file. Fortunately, if the symbol
3832 was first seen in an ELF file, we're probably OK unless the
3833 symbol was defined in a non-ELF file. Catch that case here.
3834 FIXME: We're still in trouble if the symbol was first seen in
3835 a dynamic object, and then later in a non-ELF regular object. */
3836 if ((h->root.type == bfd_link_hash_defined
3837 || h->root.type == bfd_link_hash_defweak)
3838 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3839 && (h->root.u.def.section->owner != NULL
3840 ? (bfd_get_flavour (h->root.u.def.section->owner)
3841 != bfd_target_elf_flavour)
3842 : (bfd_is_abs_section (h->root.u.def.section)
3843 && (h->elf_link_hash_flags
3844 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
3845 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3848 /* If this is a final link, and the symbol was defined as a common
3849 symbol in a regular object file, and there was no definition in
3850 any dynamic object, then the linker will have allocated space for
3851 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3852 flag will not have been set. */
3853 if (h->root.type == bfd_link_hash_defined
3854 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3855 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
3856 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3857 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3858 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3860 /* If -Bsymbolic was used (which means to bind references to global
3861 symbols to the definition within the shared object), and this
3862 symbol was defined in a regular object, then it actually doesn't
3863 need a PLT entry, and we can accomplish that by forcing it local.
3864 Likewise, if the symbol has hidden or internal visibility.
3865 FIXME: It might be that we also do not need a PLT for other
3866 non-hidden visibilities, but we would have to tell that to the
3867 backend specifically; we can't just clear PLT-related data here. */
3868 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
3869 && eif->info->shared
3870 && is_elf_hash_table (eif->info)
3871 && (eif->info->symbolic
3872 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3873 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3874 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3876 struct elf_backend_data *bed;
3878 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3879 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3880 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3882 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
3883 _bfd_elf_strtab_delref (elf_hash_table (eif->info)->dynstr,
3886 (*bed->elf_backend_hide_symbol) (eif->info, h);
3889 /* If this is a weak defined symbol in a dynamic object, and we know
3890 the real definition in the dynamic object, copy interesting flags
3891 over to the real definition. */
3892 if (h->weakdef != NULL)
3894 struct elf_link_hash_entry *weakdef;
3896 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3897 || h->root.type == bfd_link_hash_defweak);
3898 weakdef = h->weakdef;
3899 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
3900 || weakdef->root.type == bfd_link_hash_defweak);
3901 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
3903 /* If the real definition is defined by a regular object file,
3904 don't do anything special. See the longer description in
3905 elf_adjust_dynamic_symbol, below. */
3906 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3910 struct elf_backend_data *bed;
3912 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3913 (*bed->elf_backend_copy_indirect_symbol) (weakdef, h);
3920 /* Make the backend pick a good value for a dynamic symbol. This is
3921 called via elf_link_hash_traverse, and also calls itself
3925 elf_adjust_dynamic_symbol (h, data)
3926 struct elf_link_hash_entry *h;
3929 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3931 struct elf_backend_data *bed;
3933 /* Ignore indirect symbols. These are added by the versioning code. */
3934 if (h->root.type == bfd_link_hash_indirect)
3937 if (! is_elf_hash_table (eif->info))
3940 /* Fix the symbol flags. */
3941 if (! elf_fix_symbol_flags (h, eif))
3944 /* If this symbol does not require a PLT entry, and it is not
3945 defined by a dynamic object, or is not referenced by a regular
3946 object, ignore it. We do have to handle a weak defined symbol,
3947 even if no regular object refers to it, if we decided to add it
3948 to the dynamic symbol table. FIXME: Do we normally need to worry
3949 about symbols which are defined by one dynamic object and
3950 referenced by another one? */
3951 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
3952 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
3953 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3954 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
3955 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
3957 h->plt.offset = (bfd_vma) -1;
3961 /* If we've already adjusted this symbol, don't do it again. This
3962 can happen via a recursive call. */
3963 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
3966 /* Don't look at this symbol again. Note that we must set this
3967 after checking the above conditions, because we may look at a
3968 symbol once, decide not to do anything, and then get called
3969 recursively later after REF_REGULAR is set below. */
3970 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
3972 /* If this is a weak definition, and we know a real definition, and
3973 the real symbol is not itself defined by a regular object file,
3974 then get a good value for the real definition. We handle the
3975 real symbol first, for the convenience of the backend routine.
3977 Note that there is a confusing case here. If the real definition
3978 is defined by a regular object file, we don't get the real symbol
3979 from the dynamic object, but we do get the weak symbol. If the
3980 processor backend uses a COPY reloc, then if some routine in the
3981 dynamic object changes the real symbol, we will not see that
3982 change in the corresponding weak symbol. This is the way other
3983 ELF linkers work as well, and seems to be a result of the shared
3986 I will clarify this issue. Most SVR4 shared libraries define the
3987 variable _timezone and define timezone as a weak synonym. The
3988 tzset call changes _timezone. If you write
3989 extern int timezone;
3991 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3992 you might expect that, since timezone is a synonym for _timezone,
3993 the same number will print both times. However, if the processor
3994 backend uses a COPY reloc, then actually timezone will be copied
3995 into your process image, and, since you define _timezone
3996 yourself, _timezone will not. Thus timezone and _timezone will
3997 wind up at different memory locations. The tzset call will set
3998 _timezone, leaving timezone unchanged. */
4000 if (h->weakdef != NULL)
4002 /* If we get to this point, we know there is an implicit
4003 reference by a regular object file via the weak symbol H.
4004 FIXME: Is this really true? What if the traversal finds
4005 H->WEAKDEF before it finds H? */
4006 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
4008 if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif))
4012 /* If a symbol has no type and no size and does not require a PLT
4013 entry, then we are probably about to do the wrong thing here: we
4014 are probably going to create a COPY reloc for an empty object.
4015 This case can arise when a shared object is built with assembly
4016 code, and the assembly code fails to set the symbol type. */
4018 && h->type == STT_NOTYPE
4019 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
4020 (*_bfd_error_handler)
4021 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4022 h->root.root.string);
4024 dynobj = elf_hash_table (eif->info)->dynobj;
4025 bed = get_elf_backend_data (dynobj);
4026 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
4035 /* This routine is used to export all defined symbols into the dynamic
4036 symbol table. It is called via elf_link_hash_traverse. */
4039 elf_export_symbol (h, data)
4040 struct elf_link_hash_entry *h;
4043 struct elf_info_failed *eif = (struct elf_info_failed *) data;
4045 /* Ignore indirect symbols. These are added by the versioning code. */
4046 if (h->root.type == bfd_link_hash_indirect)
4049 if (h->dynindx == -1
4050 && (h->elf_link_hash_flags
4051 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
4053 struct bfd_elf_version_tree *t;
4054 struct bfd_elf_version_expr *d;
4056 for (t = eif->verdefs; t != NULL; t = t->next)
4058 if (t->globals != NULL)
4060 for (d = t->globals; d != NULL; d = d->next)
4062 if ((*d->match) (d, h->root.root.string))
4067 if (t->locals != NULL)
4069 for (d = t->locals ; d != NULL; d = d->next)
4071 if ((*d->match) (d, h->root.root.string))
4080 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
4091 /* Look through the symbols which are defined in other shared
4092 libraries and referenced here. Update the list of version
4093 dependencies. This will be put into the .gnu.version_r section.
4094 This function is called via elf_link_hash_traverse. */
4097 elf_link_find_version_dependencies (h, data)
4098 struct elf_link_hash_entry *h;
4101 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
4102 Elf_Internal_Verneed *t;
4103 Elf_Internal_Vernaux *a;
4106 /* We only care about symbols defined in shared objects with version
4108 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4109 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4111 || h->verinfo.verdef == NULL)
4114 /* See if we already know about this version. */
4115 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
4117 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
4120 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4121 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
4127 /* This is a new version. Add it to tree we are building. */
4132 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt);
4135 rinfo->failed = true;
4139 t->vn_bfd = h->verinfo.verdef->vd_bfd;
4140 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
4141 elf_tdata (rinfo->output_bfd)->verref = t;
4145 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt);
4147 /* Note that we are copying a string pointer here, and testing it
4148 above. If bfd_elf_string_from_elf_section is ever changed to
4149 discard the string data when low in memory, this will have to be
4151 a->vna_nodename = h->verinfo.verdef->vd_nodename;
4153 a->vna_flags = h->verinfo.verdef->vd_flags;
4154 a->vna_nextptr = t->vn_auxptr;
4156 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
4159 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
4166 /* Figure out appropriate versions for all the symbols. We may not
4167 have the version number script until we have read all of the input
4168 files, so until that point we don't know which symbols should be
4169 local. This function is called via elf_link_hash_traverse. */
4172 elf_link_assign_sym_version (h, data)
4173 struct elf_link_hash_entry *h;
4176 struct elf_assign_sym_version_info *sinfo;
4177 struct bfd_link_info *info;
4178 struct elf_backend_data *bed;
4179 struct elf_info_failed eif;
4183 sinfo = (struct elf_assign_sym_version_info *) data;
4186 /* Fix the symbol flags. */
4189 if (! elf_fix_symbol_flags (h, &eif))
4192 sinfo->failed = true;
4196 /* We only need version numbers for symbols defined in regular
4198 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4201 bed = get_elf_backend_data (sinfo->output_bfd);
4202 p = strchr (h->root.root.string, ELF_VER_CHR);
4203 if (p != NULL && h->verinfo.vertree == NULL)
4205 struct bfd_elf_version_tree *t;
4210 /* There are two consecutive ELF_VER_CHR characters if this is
4211 not a hidden symbol. */
4213 if (*p == ELF_VER_CHR)
4219 /* If there is no version string, we can just return out. */
4223 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4227 /* Look for the version. If we find it, it is no longer weak. */
4228 for (t = sinfo->verdefs; t != NULL; t = t->next)
4230 if (strcmp (t->name, p) == 0)
4234 struct bfd_elf_version_expr *d;
4236 len = p - h->root.root.string;
4237 alc = bfd_alloc (sinfo->output_bfd, (bfd_size_type) len);
4240 strncpy (alc, h->root.root.string, len - 1);
4241 alc[len - 1] = '\0';
4242 if (alc[len - 2] == ELF_VER_CHR)
4243 alc[len - 2] = '\0';
4245 h->verinfo.vertree = t;
4249 if (t->globals != NULL)
4251 for (d = t->globals; d != NULL; d = d->next)
4252 if ((*d->match) (d, alc))
4256 /* See if there is anything to force this symbol to
4258 if (d == NULL && t->locals != NULL)
4260 for (d = t->locals; d != NULL; d = d->next)
4262 if ((*d->match) (d, alc))
4264 if (h->dynindx != -1
4266 && ! info->export_dynamic)
4268 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
4269 (*bed->elf_backend_hide_symbol) (info, h);
4270 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4279 bfd_release (sinfo->output_bfd, alc);
4284 /* If we are building an application, we need to create a
4285 version node for this version. */
4286 if (t == NULL && ! info->shared)
4288 struct bfd_elf_version_tree **pp;
4291 /* If we aren't going to export this symbol, we don't need
4292 to worry about it. */
4293 if (h->dynindx == -1)
4297 t = ((struct bfd_elf_version_tree *)
4298 bfd_alloc (sinfo->output_bfd, amt));
4301 sinfo->failed = true;
4310 t->name_indx = (unsigned int) -1;
4314 /* Don't count anonymous version tag. */
4315 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
4317 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
4319 t->vernum = version_index;
4323 h->verinfo.vertree = t;
4327 /* We could not find the version for a symbol when
4328 generating a shared archive. Return an error. */
4329 (*_bfd_error_handler)
4330 (_("%s: undefined versioned symbol name %s"),
4331 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
4332 bfd_set_error (bfd_error_bad_value);
4333 sinfo->failed = true;
4338 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4341 /* If we don't have a version for this symbol, see if we can find
4343 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
4345 struct bfd_elf_version_tree *t;
4346 struct bfd_elf_version_tree *deflt;
4347 struct bfd_elf_version_expr *d;
4349 /* See if can find what version this symbol is in. If the
4350 symbol is supposed to be local, then don't actually register
4353 for (t = sinfo->verdefs; t != NULL; t = t->next)
4355 if (t->globals != NULL)
4357 for (d = t->globals; d != NULL; d = d->next)
4359 if ((*d->match) (d, h->root.root.string))
4361 h->verinfo.vertree = t;
4370 if (t->locals != NULL)
4372 for (d = t->locals; d != NULL; d = d->next)
4374 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
4376 else if ((*d->match) (d, h->root.root.string))
4378 h->verinfo.vertree = t;
4379 if (h->dynindx != -1
4381 && ! info->export_dynamic)
4383 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
4384 (*bed->elf_backend_hide_symbol) (info, h);
4385 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4397 if (deflt != NULL && h->verinfo.vertree == NULL)
4399 h->verinfo.vertree = deflt;
4400 if (h->dynindx != -1
4402 && ! info->export_dynamic)
4404 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
4405 (*bed->elf_backend_hide_symbol) (info, h);
4406 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4415 /* Final phase of ELF linker. */
4417 /* A structure we use to avoid passing large numbers of arguments. */
4419 struct elf_final_link_info
4421 /* General link information. */
4422 struct bfd_link_info *info;
4425 /* Symbol string table. */
4426 struct bfd_strtab_hash *symstrtab;
4427 /* .dynsym section. */
4428 asection *dynsym_sec;
4429 /* .hash section. */
4431 /* symbol version section (.gnu.version). */
4432 asection *symver_sec;
4433 /* Buffer large enough to hold contents of any section. */
4435 /* Buffer large enough to hold external relocs of any section. */
4436 PTR external_relocs;
4437 /* Buffer large enough to hold internal relocs of any section. */
4438 Elf_Internal_Rela *internal_relocs;
4439 /* Buffer large enough to hold external local symbols of any input
4441 Elf_External_Sym *external_syms;
4442 /* And a buffer for symbol section indices. */
4443 Elf_External_Sym_Shndx *locsym_shndx;
4444 /* Buffer large enough to hold internal local symbols of any input
4446 Elf_Internal_Sym *internal_syms;
4447 /* Array large enough to hold a symbol index for each local symbol
4448 of any input BFD. */
4450 /* Array large enough to hold a section pointer for each local
4451 symbol of any input BFD. */
4452 asection **sections;
4453 /* Buffer to hold swapped out symbols. */
4454 Elf_External_Sym *symbuf;
4455 /* And one for symbol section indices. */
4456 Elf_External_Sym_Shndx *symshndxbuf;
4457 /* Number of swapped out symbols in buffer. */
4458 size_t symbuf_count;
4459 /* Number of symbols which fit in symbuf. */
4463 static boolean elf_link_output_sym
4464 PARAMS ((struct elf_final_link_info *, const char *,
4465 Elf_Internal_Sym *, asection *));
4466 static boolean elf_link_flush_output_syms
4467 PARAMS ((struct elf_final_link_info *));
4468 static boolean elf_link_output_extsym
4469 PARAMS ((struct elf_link_hash_entry *, PTR));
4470 static boolean elf_link_sec_merge_syms
4471 PARAMS ((struct elf_link_hash_entry *, PTR));
4472 static boolean elf_link_input_bfd
4473 PARAMS ((struct elf_final_link_info *, bfd *));
4474 static boolean elf_reloc_link_order
4475 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4476 struct bfd_link_order *));
4478 /* This struct is used to pass information to elf_link_output_extsym. */
4480 struct elf_outext_info
4484 struct elf_final_link_info *finfo;
4487 /* Compute the size of, and allocate space for, REL_HDR which is the
4488 section header for a section containing relocations for O. */
4491 elf_link_size_reloc_section (abfd, rel_hdr, o)
4493 Elf_Internal_Shdr *rel_hdr;
4496 bfd_size_type reloc_count;
4497 bfd_size_type num_rel_hashes;
4499 /* Figure out how many relocations there will be. */
4500 if (rel_hdr == &elf_section_data (o)->rel_hdr)
4501 reloc_count = elf_section_data (o)->rel_count;
4503 reloc_count = elf_section_data (o)->rel_count2;
4505 num_rel_hashes = o->reloc_count;
4506 if (num_rel_hashes < reloc_count)
4507 num_rel_hashes = reloc_count;
4509 /* That allows us to calculate the size of the section. */
4510 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
4512 /* The contents field must last into write_object_contents, so we
4513 allocate it with bfd_alloc rather than malloc. Also since we
4514 cannot be sure that the contents will actually be filled in,
4515 we zero the allocated space. */
4516 rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size);
4517 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
4520 /* We only allocate one set of hash entries, so we only do it the
4521 first time we are called. */
4522 if (elf_section_data (o)->rel_hashes == NULL
4525 struct elf_link_hash_entry **p;
4527 p = ((struct elf_link_hash_entry **)
4528 bfd_zmalloc (num_rel_hashes
4529 * sizeof (struct elf_link_hash_entry *)));
4533 elf_section_data (o)->rel_hashes = p;
4539 /* When performing a relocateable link, the input relocations are
4540 preserved. But, if they reference global symbols, the indices
4541 referenced must be updated. Update all the relocations in
4542 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4545 elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
4547 Elf_Internal_Shdr *rel_hdr;
4549 struct elf_link_hash_entry **rel_hash;
4552 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4553 Elf_Internal_Rel *irel;
4554 Elf_Internal_Rela *irela;
4555 bfd_size_type amt = sizeof (Elf_Internal_Rel) * bed->s->int_rels_per_ext_rel;
4557 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
4560 (*_bfd_error_handler) (_("Error: out of memory"));
4564 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
4565 irela = (Elf_Internal_Rela *) bfd_zmalloc (amt);
4568 (*_bfd_error_handler) (_("Error: out of memory"));
4572 for (i = 0; i < count; i++, rel_hash++)
4574 if (*rel_hash == NULL)
4577 BFD_ASSERT ((*rel_hash)->indx >= 0);
4579 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
4581 Elf_External_Rel *erel;
4584 erel = (Elf_External_Rel *) rel_hdr->contents + i;
4585 if (bed->s->swap_reloc_in)
4586 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
4588 elf_swap_reloc_in (abfd, erel, irel);
4590 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4591 irel[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4592 ELF_R_TYPE (irel[j].r_info));
4594 if (bed->s->swap_reloc_out)
4595 (*bed->s->swap_reloc_out) (abfd, irel, (bfd_byte *) erel);
4597 elf_swap_reloc_out (abfd, irel, erel);
4601 Elf_External_Rela *erela;
4604 BFD_ASSERT (rel_hdr->sh_entsize
4605 == sizeof (Elf_External_Rela));
4607 erela = (Elf_External_Rela *) rel_hdr->contents + i;
4608 if (bed->s->swap_reloca_in)
4609 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
4611 elf_swap_reloca_in (abfd, erela, irela);
4613 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4614 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4615 ELF_R_TYPE (irela[j].r_info));
4617 if (bed->s->swap_reloca_out)
4618 (*bed->s->swap_reloca_out) (abfd, irela, (bfd_byte *) erela);
4620 elf_swap_reloca_out (abfd, irela, erela);
4628 struct elf_link_sort_rela {
4630 enum elf_reloc_type_class type;
4632 Elf_Internal_Rel rel;
4633 Elf_Internal_Rela rela;
4638 elf_link_sort_cmp1 (A, B)
4642 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4643 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4644 int relativea, relativeb;
4646 relativea = a->type == reloc_class_relative;
4647 relativeb = b->type == reloc_class_relative;
4649 if (relativea < relativeb)
4651 if (relativea > relativeb)
4653 if (ELF_R_SYM (a->u.rel.r_info) < ELF_R_SYM (b->u.rel.r_info))
4655 if (ELF_R_SYM (a->u.rel.r_info) > ELF_R_SYM (b->u.rel.r_info))
4657 if (a->u.rel.r_offset < b->u.rel.r_offset)
4659 if (a->u.rel.r_offset > b->u.rel.r_offset)
4665 elf_link_sort_cmp2 (A, B)
4669 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4670 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4673 if (a->offset < b->offset)
4675 if (a->offset > b->offset)
4677 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
4678 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
4683 if (a->u.rel.r_offset < b->u.rel.r_offset)
4685 if (a->u.rel.r_offset > b->u.rel.r_offset)
4691 elf_link_sort_relocs (abfd, info, psec)
4693 struct bfd_link_info *info;
4696 bfd *dynobj = elf_hash_table (info)->dynobj;
4697 asection *reldyn, *o;
4698 boolean rel = false;
4699 bfd_size_type count, size;
4701 struct elf_link_sort_rela *rela;
4702 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4704 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
4705 if (reldyn == NULL || reldyn->_raw_size == 0)
4707 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
4708 if (reldyn == NULL || reldyn->_raw_size == 0)
4711 count = reldyn->_raw_size / sizeof (Elf_External_Rel);
4714 count = reldyn->_raw_size / sizeof (Elf_External_Rela);
4717 for (o = dynobj->sections; o != NULL; o = o->next)
4718 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4719 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4720 && o->output_section == reldyn)
4721 size += o->_raw_size;
4723 if (size != reldyn->_raw_size)
4726 rela = (struct elf_link_sort_rela *) bfd_zmalloc (sizeof (*rela) * count);
4729 (*info->callbacks->warning)
4730 (info, _("Not enough memory to sort relocations"), 0, abfd, 0,
4735 for (o = dynobj->sections; o != NULL; o = o->next)
4736 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4737 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4738 && o->output_section == reldyn)
4742 Elf_External_Rel *erel, *erelend;
4743 struct elf_link_sort_rela *s;
4745 erel = (Elf_External_Rel *) o->contents;
4746 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4747 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4748 for (; erel < erelend; erel++, s++)
4750 if (bed->s->swap_reloc_in)
4751 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, &s->u.rel);
4753 elf_swap_reloc_in (abfd, erel, &s->u.rel);
4755 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
4760 Elf_External_Rela *erela, *erelaend;
4761 struct elf_link_sort_rela *s;
4763 erela = (Elf_External_Rela *) o->contents;
4764 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4765 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4766 for (; erela < erelaend; erela++, s++)
4768 if (bed->s->swap_reloca_in)
4769 (*bed->s->swap_reloca_in) (dynobj, (bfd_byte *) erela,
4772 elf_swap_reloca_in (dynobj, erela, &s->u.rela);
4774 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
4779 qsort (rela, (size_t) count, sizeof (*rela), elf_link_sort_cmp1);
4780 for (ret = 0; ret < count && rela[ret].type == reloc_class_relative; ret++)
4782 for (i = ret, j = ret; i < count; i++)
4784 if (ELF_R_SYM (rela[i].u.rel.r_info) != ELF_R_SYM (rela[j].u.rel.r_info))
4786 rela[i].offset = rela[j].u.rel.r_offset;
4788 qsort (rela + ret, (size_t) count - ret, sizeof (*rela), elf_link_sort_cmp2);
4790 for (o = dynobj->sections; o != NULL; o = o->next)
4791 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4792 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4793 && o->output_section == reldyn)
4797 Elf_External_Rel *erel, *erelend;
4798 struct elf_link_sort_rela *s;
4800 erel = (Elf_External_Rel *) o->contents;
4801 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4802 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4803 for (; erel < erelend; erel++, s++)
4805 if (bed->s->swap_reloc_out)
4806 (*bed->s->swap_reloc_out) (abfd, &s->u.rel,
4809 elf_swap_reloc_out (abfd, &s->u.rel, erel);
4814 Elf_External_Rela *erela, *erelaend;
4815 struct elf_link_sort_rela *s;
4817 erela = (Elf_External_Rela *) o->contents;
4818 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4819 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4820 for (; erela < erelaend; erela++, s++)
4822 if (bed->s->swap_reloca_out)
4823 (*bed->s->swap_reloca_out) (dynobj, &s->u.rela,
4824 (bfd_byte *) erela);
4826 elf_swap_reloca_out (dynobj, &s->u.rela, erela);
4836 /* Do the final step of an ELF link. */
4839 elf_bfd_final_link (abfd, info)
4841 struct bfd_link_info *info;
4844 boolean emit_relocs;
4846 struct elf_final_link_info finfo;
4847 register asection *o;
4848 register struct bfd_link_order *p;
4850 bfd_size_type max_contents_size;
4851 bfd_size_type max_external_reloc_size;
4852 bfd_size_type max_internal_reloc_count;
4853 bfd_size_type max_sym_count;
4854 bfd_size_type max_sym_shndx_count;
4856 Elf_Internal_Sym elfsym;
4858 Elf_Internal_Shdr *symtab_hdr;
4859 Elf_Internal_Shdr *symstrtab_hdr;
4860 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4861 struct elf_outext_info eoinfo;
4863 size_t relativecount = 0;
4864 asection *reldyn = 0;
4867 if (! is_elf_hash_table (info))
4871 abfd->flags |= DYNAMIC;
4873 dynamic = elf_hash_table (info)->dynamic_sections_created;
4874 dynobj = elf_hash_table (info)->dynobj;
4876 emit_relocs = (info->relocateable
4877 || info->emitrelocations
4878 || bed->elf_backend_emit_relocs);
4881 finfo.output_bfd = abfd;
4882 finfo.symstrtab = elf_stringtab_init ();
4883 if (finfo.symstrtab == NULL)
4888 finfo.dynsym_sec = NULL;
4889 finfo.hash_sec = NULL;
4890 finfo.symver_sec = NULL;
4894 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
4895 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
4896 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
4897 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
4898 /* Note that it is OK if symver_sec is NULL. */
4901 finfo.contents = NULL;
4902 finfo.external_relocs = NULL;
4903 finfo.internal_relocs = NULL;
4904 finfo.external_syms = NULL;
4905 finfo.locsym_shndx = NULL;
4906 finfo.internal_syms = NULL;
4907 finfo.indices = NULL;
4908 finfo.sections = NULL;
4909 finfo.symbuf = NULL;
4910 finfo.symshndxbuf = NULL;
4911 finfo.symbuf_count = 0;
4913 /* Count up the number of relocations we will output for each output
4914 section, so that we know the sizes of the reloc sections. We
4915 also figure out some maximum sizes. */
4916 max_contents_size = 0;
4917 max_external_reloc_size = 0;
4918 max_internal_reloc_count = 0;
4920 max_sym_shndx_count = 0;
4922 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4926 for (p = o->link_order_head; p != NULL; p = p->next)
4928 if (p->type == bfd_section_reloc_link_order
4929 || p->type == bfd_symbol_reloc_link_order)
4931 else if (p->type == bfd_indirect_link_order)
4935 sec = p->u.indirect.section;
4937 /* Mark all sections which are to be included in the
4938 link. This will normally be every section. We need
4939 to do this so that we can identify any sections which
4940 the linker has decided to not include. */
4941 sec->linker_mark = true;
4943 if (sec->flags & SEC_MERGE)
4946 if (info->relocateable || info->emitrelocations)
4947 o->reloc_count += sec->reloc_count;
4948 else if (bed->elf_backend_count_relocs)
4950 Elf_Internal_Rela * relocs;
4952 relocs = (NAME(_bfd_elf,link_read_relocs)
4953 (abfd, sec, (PTR) NULL,
4954 (Elf_Internal_Rela *) NULL, info->keep_memory));
4956 o->reloc_count += (*bed->elf_backend_count_relocs)
4959 if (!info->keep_memory)
4963 if (sec->_raw_size > max_contents_size)
4964 max_contents_size = sec->_raw_size;
4965 if (sec->_cooked_size > max_contents_size)
4966 max_contents_size = sec->_cooked_size;
4968 /* We are interested in just local symbols, not all
4970 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
4971 && (sec->owner->flags & DYNAMIC) == 0)
4975 if (elf_bad_symtab (sec->owner))
4976 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
4977 / sizeof (Elf_External_Sym));
4979 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
4981 if (sym_count > max_sym_count)
4982 max_sym_count = sym_count;
4984 if (sym_count > max_sym_shndx_count
4985 && elf_symtab_shndx (sec->owner) != 0)
4986 max_sym_shndx_count = sym_count;
4988 if ((sec->flags & SEC_RELOC) != 0)
4992 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
4993 if (ext_size > max_external_reloc_size)
4994 max_external_reloc_size = ext_size;
4995 if (sec->reloc_count > max_internal_reloc_count)
4996 max_internal_reloc_count = sec->reloc_count;
5002 if (o->reloc_count > 0)
5003 o->flags |= SEC_RELOC;
5006 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5007 set it (this is probably a bug) and if it is set
5008 assign_section_numbers will create a reloc section. */
5009 o->flags &=~ SEC_RELOC;
5012 /* If the SEC_ALLOC flag is not set, force the section VMA to
5013 zero. This is done in elf_fake_sections as well, but forcing
5014 the VMA to 0 here will ensure that relocs against these
5015 sections are handled correctly. */
5016 if ((o->flags & SEC_ALLOC) == 0
5017 && ! o->user_set_vma)
5021 if (! info->relocateable && merged)
5022 elf_link_hash_traverse (elf_hash_table (info),
5023 elf_link_sec_merge_syms, (PTR) abfd);
5025 /* Figure out the file positions for everything but the symbol table
5026 and the relocs. We set symcount to force assign_section_numbers
5027 to create a symbol table. */
5028 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
5029 BFD_ASSERT (! abfd->output_has_begun);
5030 if (! _bfd_elf_compute_section_file_positions (abfd, info))
5033 /* Figure out how many relocations we will have in each section.
5034 Just using RELOC_COUNT isn't good enough since that doesn't
5035 maintain a separate value for REL vs. RELA relocations. */
5037 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5038 for (o = sub->sections; o != NULL; o = o->next)
5040 asection *output_section;
5042 if (! o->linker_mark)
5044 /* This section was omitted from the link. */
5048 output_section = o->output_section;
5050 if (output_section != NULL
5051 && (o->flags & SEC_RELOC) != 0)
5053 struct bfd_elf_section_data *esdi
5054 = elf_section_data (o);
5055 struct bfd_elf_section_data *esdo
5056 = elf_section_data (output_section);
5057 unsigned int *rel_count;
5058 unsigned int *rel_count2;
5060 /* We must be careful to add the relocation froms the
5061 input section to the right output count. */
5062 if (esdi->rel_hdr.sh_entsize == esdo->rel_hdr.sh_entsize)
5064 rel_count = &esdo->rel_count;
5065 rel_count2 = &esdo->rel_count2;
5069 rel_count = &esdo->rel_count2;
5070 rel_count2 = &esdo->rel_count;
5073 *rel_count += NUM_SHDR_ENTRIES (& esdi->rel_hdr);
5075 *rel_count2 += NUM_SHDR_ENTRIES (esdi->rel_hdr2);
5076 output_section->flags |= SEC_RELOC;
5080 /* That created the reloc sections. Set their sizes, and assign
5081 them file positions, and allocate some buffers. */
5082 for (o = abfd->sections; o != NULL; o = o->next)
5084 if ((o->flags & SEC_RELOC) != 0)
5086 if (!elf_link_size_reloc_section (abfd,
5087 &elf_section_data (o)->rel_hdr,
5091 if (elf_section_data (o)->rel_hdr2
5092 && !elf_link_size_reloc_section (abfd,
5093 elf_section_data (o)->rel_hdr2,
5098 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5099 to count upwards while actually outputting the relocations. */
5100 elf_section_data (o)->rel_count = 0;
5101 elf_section_data (o)->rel_count2 = 0;
5104 _bfd_elf_assign_file_positions_for_relocs (abfd);
5106 /* We have now assigned file positions for all the sections except
5107 .symtab and .strtab. We start the .symtab section at the current
5108 file position, and write directly to it. We build the .strtab
5109 section in memory. */
5110 bfd_get_symcount (abfd) = 0;
5111 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5112 /* sh_name is set in prep_headers. */
5113 symtab_hdr->sh_type = SHT_SYMTAB;
5114 symtab_hdr->sh_flags = 0;
5115 symtab_hdr->sh_addr = 0;
5116 symtab_hdr->sh_size = 0;
5117 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
5118 /* sh_link is set in assign_section_numbers. */
5119 /* sh_info is set below. */
5120 /* sh_offset is set just below. */
5121 symtab_hdr->sh_addralign = bed->s->file_align;
5123 off = elf_tdata (abfd)->next_file_pos;
5124 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
5126 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5127 incorrect. We do not yet know the size of the .symtab section.
5128 We correct next_file_pos below, after we do know the size. */
5130 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5131 continuously seeking to the right position in the file. */
5132 if (! info->keep_memory || max_sym_count < 20)
5133 finfo.symbuf_size = 20;
5135 finfo.symbuf_size = max_sym_count;
5136 amt = finfo.symbuf_size;
5137 amt *= sizeof (Elf_External_Sym);
5138 finfo.symbuf = (Elf_External_Sym *) bfd_malloc (amt);
5139 if (finfo.symbuf == NULL)
5141 if (elf_numsections (abfd) > SHN_LORESERVE)
5143 amt = finfo.symbuf_size;
5144 amt *= sizeof (Elf_External_Sym_Shndx);
5145 finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5146 if (finfo.symshndxbuf == NULL)
5150 /* Start writing out the symbol table. The first symbol is always a
5152 if (info->strip != strip_all
5155 elfsym.st_value = 0;
5158 elfsym.st_other = 0;
5159 elfsym.st_shndx = SHN_UNDEF;
5160 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5161 &elfsym, bfd_und_section_ptr))
5166 /* Some standard ELF linkers do this, but we don't because it causes
5167 bootstrap comparison failures. */
5168 /* Output a file symbol for the output file as the second symbol.
5169 We output this even if we are discarding local symbols, although
5170 I'm not sure if this is correct. */
5171 elfsym.st_value = 0;
5173 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5174 elfsym.st_other = 0;
5175 elfsym.st_shndx = SHN_ABS;
5176 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
5177 &elfsym, bfd_abs_section_ptr))
5181 /* Output a symbol for each section. We output these even if we are
5182 discarding local symbols, since they are used for relocs. These
5183 symbols have no names. We store the index of each one in the
5184 index field of the section, so that we can find it again when
5185 outputting relocs. */
5186 if (info->strip != strip_all
5190 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5191 elfsym.st_other = 0;
5192 for (i = 1; i < elf_numsections (abfd); i++)
5194 o = section_from_elf_index (abfd, i);
5196 o->target_index = bfd_get_symcount (abfd);
5197 elfsym.st_shndx = i;
5198 if (info->relocateable || o == NULL)
5199 elfsym.st_value = 0;
5201 elfsym.st_value = o->vma;
5202 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5205 if (i == SHN_LORESERVE)
5206 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
5210 /* Allocate some memory to hold information read in from the input
5212 if (max_contents_size != 0)
5214 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
5215 if (finfo.contents == NULL)
5219 if (max_external_reloc_size != 0)
5221 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
5222 if (finfo.external_relocs == NULL)
5226 if (max_internal_reloc_count != 0)
5228 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
5229 amt *= sizeof (Elf_Internal_Rela);
5230 finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
5231 if (finfo.internal_relocs == NULL)
5235 if (max_sym_count != 0)
5237 amt = max_sym_count * sizeof (Elf_External_Sym);
5238 finfo.external_syms = (Elf_External_Sym *) bfd_malloc (amt);
5239 if (finfo.external_syms == NULL)
5242 amt = max_sym_count * sizeof (Elf_Internal_Sym);
5243 finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
5244 if (finfo.internal_syms == NULL)
5247 amt = max_sym_count * sizeof (long);
5248 finfo.indices = (long *) bfd_malloc (amt);
5249 if (finfo.indices == NULL)
5252 amt = max_sym_count * sizeof (asection *);
5253 finfo.sections = (asection **) bfd_malloc (amt);
5254 if (finfo.sections == NULL)
5258 if (max_sym_shndx_count != 0)
5260 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
5261 finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5262 if (finfo.locsym_shndx == NULL)
5266 /* Since ELF permits relocations to be against local symbols, we
5267 must have the local symbols available when we do the relocations.
5268 Since we would rather only read the local symbols once, and we
5269 would rather not keep them in memory, we handle all the
5270 relocations for a single input file at the same time.
5272 Unfortunately, there is no way to know the total number of local
5273 symbols until we have seen all of them, and the local symbol
5274 indices precede the global symbol indices. This means that when
5275 we are generating relocateable output, and we see a reloc against
5276 a global symbol, we can not know the symbol index until we have
5277 finished examining all the local symbols to see which ones we are
5278 going to output. To deal with this, we keep the relocations in
5279 memory, and don't output them until the end of the link. This is
5280 an unfortunate waste of memory, but I don't see a good way around
5281 it. Fortunately, it only happens when performing a relocateable
5282 link, which is not the common case. FIXME: If keep_memory is set
5283 we could write the relocs out and then read them again; I don't
5284 know how bad the memory loss will be. */
5286 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5287 sub->output_has_begun = false;
5288 for (o = abfd->sections; o != NULL; o = o->next)
5290 for (p = o->link_order_head; p != NULL; p = p->next)
5292 if (p->type == bfd_indirect_link_order
5293 && (bfd_get_flavour (p->u.indirect.section->owner)
5294 == bfd_target_elf_flavour))
5296 sub = p->u.indirect.section->owner;
5297 if (! sub->output_has_begun)
5299 if (! elf_link_input_bfd (&finfo, sub))
5301 sub->output_has_begun = true;
5304 else if (p->type == bfd_section_reloc_link_order
5305 || p->type == bfd_symbol_reloc_link_order)
5307 if (! elf_reloc_link_order (abfd, info, o, p))
5312 if (! _bfd_default_link_order (abfd, info, o, p))
5318 /* That wrote out all the local symbols. Finish up the symbol table
5319 with the global symbols. Even if we want to strip everything we
5320 can, we still need to deal with those global symbols that got
5321 converted to local in a version script. */
5325 /* Output any global symbols that got converted to local in a
5326 version script. We do this in a separate step since ELF
5327 requires all local symbols to appear prior to any global
5328 symbols. FIXME: We should only do this if some global
5329 symbols were, in fact, converted to become local. FIXME:
5330 Will this work correctly with the Irix 5 linker? */
5331 eoinfo.failed = false;
5332 eoinfo.finfo = &finfo;
5333 eoinfo.localsyms = true;
5334 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5340 /* The sh_info field records the index of the first non local symbol. */
5341 symtab_hdr->sh_info = bfd_get_symcount (abfd);
5344 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
5346 Elf_Internal_Sym sym;
5347 Elf_External_Sym *dynsym =
5348 (Elf_External_Sym *) finfo.dynsym_sec->contents;
5349 long last_local = 0;
5351 /* Write out the section symbols for the output sections. */
5358 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5361 for (s = abfd->sections; s != NULL; s = s->next)
5364 Elf_External_Sym *dest;
5366 indx = elf_section_data (s)->this_idx;
5367 BFD_ASSERT (indx > 0);
5368 sym.st_shndx = indx;
5369 sym.st_value = s->vma;
5370 dest = dynsym + elf_section_data (s)->dynindx;
5371 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
5374 last_local = bfd_count_sections (abfd);
5377 /* Write out the local dynsyms. */
5378 if (elf_hash_table (info)->dynlocal)
5380 struct elf_link_local_dynamic_entry *e;
5381 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
5384 Elf_External_Sym *dest;
5386 sym.st_size = e->isym.st_size;
5387 sym.st_other = e->isym.st_other;
5389 /* Copy the internal symbol as is.
5390 Note that we saved a word of storage and overwrote
5391 the original st_name with the dynstr_index. */
5394 if (e->isym.st_shndx < SHN_LORESERVE
5395 || e->isym.st_shndx > SHN_HIRESERVE)
5397 s = bfd_section_from_elf_index (e->input_bfd,
5401 elf_section_data (s->output_section)->this_idx;
5402 sym.st_value = (s->output_section->vma
5404 + e->isym.st_value);
5407 if (last_local < e->dynindx)
5408 last_local = e->dynindx;
5410 dest = dynsym + e->dynindx;
5411 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
5415 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
5419 /* We get the global symbols from the hash table. */
5420 eoinfo.failed = false;
5421 eoinfo.localsyms = false;
5422 eoinfo.finfo = &finfo;
5423 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5428 /* If backend needs to output some symbols not present in the hash
5429 table, do it now. */
5430 if (bed->elf_backend_output_arch_syms)
5432 typedef boolean (*out_sym_func) PARAMS ((PTR, const char *,
5436 if (! ((*bed->elf_backend_output_arch_syms)
5437 (abfd, info, (PTR) &finfo, (out_sym_func) elf_link_output_sym)))
5441 /* Flush all symbols to the file. */
5442 if (! elf_link_flush_output_syms (&finfo))
5445 /* Now we know the size of the symtab section. */
5446 off += symtab_hdr->sh_size;
5448 /* Finish up and write out the symbol string table (.strtab)
5450 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5451 /* sh_name was set in prep_headers. */
5452 symstrtab_hdr->sh_type = SHT_STRTAB;
5453 symstrtab_hdr->sh_flags = 0;
5454 symstrtab_hdr->sh_addr = 0;
5455 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
5456 symstrtab_hdr->sh_entsize = 0;
5457 symstrtab_hdr->sh_link = 0;
5458 symstrtab_hdr->sh_info = 0;
5459 /* sh_offset is set just below. */
5460 symstrtab_hdr->sh_addralign = 1;
5462 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true);
5463 elf_tdata (abfd)->next_file_pos = off;
5465 if (bfd_get_symcount (abfd) > 0)
5467 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
5468 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
5472 /* Adjust the relocs to have the correct symbol indices. */
5473 for (o = abfd->sections; o != NULL; o = o->next)
5475 if ((o->flags & SEC_RELOC) == 0)
5478 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
5479 elf_section_data (o)->rel_count,
5480 elf_section_data (o)->rel_hashes);
5481 if (elf_section_data (o)->rel_hdr2 != NULL)
5482 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
5483 elf_section_data (o)->rel_count2,
5484 (elf_section_data (o)->rel_hashes
5485 + elf_section_data (o)->rel_count));
5487 /* Set the reloc_count field to 0 to prevent write_relocs from
5488 trying to swap the relocs out itself. */
5492 if (dynamic && info->combreloc && dynobj != NULL)
5493 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
5495 /* If we are linking against a dynamic object, or generating a
5496 shared library, finish up the dynamic linking information. */
5499 Elf_External_Dyn *dyncon, *dynconend;
5501 /* Fix up .dynamic entries. */
5502 o = bfd_get_section_by_name (dynobj, ".dynamic");
5503 BFD_ASSERT (o != NULL);
5505 dyncon = (Elf_External_Dyn *) o->contents;
5506 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
5507 for (; dyncon < dynconend; dyncon++)
5509 Elf_Internal_Dyn dyn;
5513 elf_swap_dyn_in (dynobj, dyncon, &dyn);
5520 if (relativecount > 0 && dyncon + 1 < dynconend)
5522 switch (elf_section_data (reldyn)->this_hdr.sh_type)
5524 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
5525 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
5528 if (dyn.d_tag != DT_NULL)
5530 dyn.d_un.d_val = relativecount;
5531 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5537 name = info->init_function;
5540 name = info->fini_function;
5543 struct elf_link_hash_entry *h;
5545 h = elf_link_hash_lookup (elf_hash_table (info), name,
5546 false, false, true);
5548 && (h->root.type == bfd_link_hash_defined
5549 || h->root.type == bfd_link_hash_defweak))
5551 dyn.d_un.d_val = h->root.u.def.value;
5552 o = h->root.u.def.section;
5553 if (o->output_section != NULL)
5554 dyn.d_un.d_val += (o->output_section->vma
5555 + o->output_offset);
5558 /* The symbol is imported from another shared
5559 library and does not apply to this one. */
5563 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5578 name = ".gnu.version_d";
5581 name = ".gnu.version_r";
5584 name = ".gnu.version";
5586 o = bfd_get_section_by_name (abfd, name);
5587 BFD_ASSERT (o != NULL);
5588 dyn.d_un.d_ptr = o->vma;
5589 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5596 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5601 for (i = 1; i < elf_numsections (abfd); i++)
5603 Elf_Internal_Shdr *hdr;
5605 hdr = elf_elfsections (abfd)[i];
5606 if (hdr->sh_type == type
5607 && (hdr->sh_flags & SHF_ALLOC) != 0)
5609 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
5610 dyn.d_un.d_val += hdr->sh_size;
5613 if (dyn.d_un.d_val == 0
5614 || hdr->sh_addr < dyn.d_un.d_val)
5615 dyn.d_un.d_val = hdr->sh_addr;
5619 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5625 /* If we have created any dynamic sections, then output them. */
5628 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
5631 for (o = dynobj->sections; o != NULL; o = o->next)
5633 if ((o->flags & SEC_HAS_CONTENTS) == 0
5634 || o->_raw_size == 0
5635 || o->output_section == bfd_abs_section_ptr)
5637 if ((o->flags & SEC_LINKER_CREATED) == 0)
5639 /* At this point, we are only interested in sections
5640 created by elf_link_create_dynamic_sections. */
5643 if ((elf_section_data (o->output_section)->this_hdr.sh_type
5645 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
5647 if (! bfd_set_section_contents (abfd, o->output_section,
5649 (file_ptr) o->output_offset,
5655 /* The contents of the .dynstr section are actually in a
5657 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
5658 if (bfd_seek (abfd, off, SEEK_SET) != 0
5659 || ! _bfd_elf_strtab_emit (abfd,
5660 elf_hash_table (info)->dynstr))
5666 /* If we have optimized stabs strings, output them. */
5667 if (elf_hash_table (info)->stab_info != NULL)
5669 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
5673 if (info->eh_frame_hdr)
5675 o = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
5678 && (elf_section_data (o)->sec_info_type
5679 == ELF_INFO_TYPE_EH_FRAME_HDR))
5681 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, o))
5686 if (finfo.symstrtab != NULL)
5687 _bfd_stringtab_free (finfo.symstrtab);
5688 if (finfo.contents != NULL)
5689 free (finfo.contents);
5690 if (finfo.external_relocs != NULL)
5691 free (finfo.external_relocs);
5692 if (finfo.internal_relocs != NULL)
5693 free (finfo.internal_relocs);
5694 if (finfo.external_syms != NULL)
5695 free (finfo.external_syms);
5696 if (finfo.locsym_shndx != NULL)
5697 free (finfo.locsym_shndx);
5698 if (finfo.internal_syms != NULL)
5699 free (finfo.internal_syms);
5700 if (finfo.indices != NULL)
5701 free (finfo.indices);
5702 if (finfo.sections != NULL)
5703 free (finfo.sections);
5704 if (finfo.symbuf != NULL)
5705 free (finfo.symbuf);
5706 if (finfo.symshndxbuf != NULL)
5707 free (finfo.symbuf);
5708 for (o = abfd->sections; o != NULL; o = o->next)
5710 if ((o->flags & SEC_RELOC) != 0
5711 && elf_section_data (o)->rel_hashes != NULL)
5712 free (elf_section_data (o)->rel_hashes);
5715 elf_tdata (abfd)->linker = true;
5720 if (finfo.symstrtab != NULL)
5721 _bfd_stringtab_free (finfo.symstrtab);
5722 if (finfo.contents != NULL)
5723 free (finfo.contents);
5724 if (finfo.external_relocs != NULL)
5725 free (finfo.external_relocs);
5726 if (finfo.internal_relocs != NULL)
5727 free (finfo.internal_relocs);
5728 if (finfo.external_syms != NULL)
5729 free (finfo.external_syms);
5730 if (finfo.locsym_shndx != NULL)
5731 free (finfo.locsym_shndx);
5732 if (finfo.internal_syms != NULL)
5733 free (finfo.internal_syms);
5734 if (finfo.indices != NULL)
5735 free (finfo.indices);
5736 if (finfo.sections != NULL)
5737 free (finfo.sections);
5738 if (finfo.symbuf != NULL)
5739 free (finfo.symbuf);
5740 if (finfo.symshndxbuf != NULL)
5741 free (finfo.symbuf);
5742 for (o = abfd->sections; o != NULL; o = o->next)
5744 if ((o->flags & SEC_RELOC) != 0
5745 && elf_section_data (o)->rel_hashes != NULL)
5746 free (elf_section_data (o)->rel_hashes);
5752 /* Add a symbol to the output symbol table. */
5755 elf_link_output_sym (finfo, name, elfsym, input_sec)
5756 struct elf_final_link_info *finfo;
5758 Elf_Internal_Sym *elfsym;
5759 asection *input_sec;
5761 Elf_External_Sym *dest;
5762 Elf_External_Sym_Shndx *destshndx;
5764 boolean (*output_symbol_hook) PARAMS ((bfd *,
5765 struct bfd_link_info *info,
5770 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
5771 elf_backend_link_output_symbol_hook;
5772 if (output_symbol_hook != NULL)
5774 if (! ((*output_symbol_hook)
5775 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
5779 if (name == (const char *) NULL || *name == '\0')
5780 elfsym->st_name = 0;
5781 else if (input_sec->flags & SEC_EXCLUDE)
5782 elfsym->st_name = 0;
5785 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5787 if (elfsym->st_name == (unsigned long) -1)
5791 if (finfo->symbuf_count >= finfo->symbuf_size)
5793 if (! elf_link_flush_output_syms (finfo))
5797 dest = finfo->symbuf + finfo->symbuf_count;
5798 destshndx = finfo->symshndxbuf;
5799 if (destshndx != NULL)
5800 destshndx += finfo->symbuf_count;
5801 elf_swap_symbol_out (finfo->output_bfd, elfsym, (PTR) dest, (PTR) destshndx);
5802 ++finfo->symbuf_count;
5804 ++ bfd_get_symcount (finfo->output_bfd);
5809 /* Flush the output symbols to the file. */
5812 elf_link_flush_output_syms (finfo)
5813 struct elf_final_link_info *finfo;
5815 if (finfo->symbuf_count > 0)
5817 Elf_Internal_Shdr *hdr;
5821 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5822 pos = hdr->sh_offset + hdr->sh_size;
5823 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
5824 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5825 || bfd_bwrite ((PTR) finfo->symbuf, amt, finfo->output_bfd) != amt)
5828 hdr->sh_size += amt;
5830 if (finfo->symshndxbuf != NULL)
5832 hdr = &elf_tdata (finfo->output_bfd)->symtab_shndx_hdr;
5833 pos = hdr->sh_offset + hdr->sh_size;
5834 amt = finfo->symbuf_count * sizeof (Elf_External_Sym_Shndx);
5835 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5836 || (bfd_bwrite ((PTR) finfo->symshndxbuf, amt, finfo->output_bfd)
5840 hdr->sh_size += amt;
5843 finfo->symbuf_count = 0;
5849 /* Adjust all external symbols pointing into SEC_MERGE sections
5850 to reflect the object merging within the sections. */
5853 elf_link_sec_merge_syms (h, data)
5854 struct elf_link_hash_entry *h;
5859 if ((h->root.type == bfd_link_hash_defined
5860 || h->root.type == bfd_link_hash_defweak)
5861 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
5862 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
5864 bfd *output_bfd = (bfd *) data;
5866 h->root.u.def.value =
5867 _bfd_merged_section_offset (output_bfd,
5868 &h->root.u.def.section,
5869 elf_section_data (sec)->sec_info,
5870 h->root.u.def.value, (bfd_vma) 0);
5876 /* Add an external symbol to the symbol table. This is called from
5877 the hash table traversal routine. When generating a shared object,
5878 we go through the symbol table twice. The first time we output
5879 anything that might have been forced to local scope in a version
5880 script. The second time we output the symbols that are still
5884 elf_link_output_extsym (h, data)
5885 struct elf_link_hash_entry *h;
5888 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
5889 struct elf_final_link_info *finfo = eoinfo->finfo;
5891 Elf_Internal_Sym sym;
5892 asection *input_sec;
5894 /* Decide whether to output this symbol in this pass. */
5895 if (eoinfo->localsyms)
5897 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5902 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5906 /* If we are not creating a shared library, and this symbol is
5907 referenced by a shared library but is not defined anywhere, then
5908 warn that it is undefined. If we do not do this, the runtime
5909 linker will complain that the symbol is undefined when the
5910 program is run. We don't have to worry about symbols that are
5911 referenced by regular files, because we will already have issued
5912 warnings for them. */
5913 if (! finfo->info->relocateable
5914 && ! finfo->info->allow_shlib_undefined
5915 && ! finfo->info->shared
5916 && h->root.type == bfd_link_hash_undefined
5917 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
5918 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
5920 if (! ((*finfo->info->callbacks->undefined_symbol)
5921 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
5922 (asection *) NULL, (bfd_vma) 0, true)))
5924 eoinfo->failed = true;
5929 /* We don't want to output symbols that have never been mentioned by
5930 a regular file, or that we have been told to strip. However, if
5931 h->indx is set to -2, the symbol is used by a reloc and we must
5935 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5936 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
5937 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
5938 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
5940 else if (finfo->info->strip == strip_all
5941 || (finfo->info->strip == strip_some
5942 && bfd_hash_lookup (finfo->info->keep_hash,
5943 h->root.root.string,
5944 false, false) == NULL))
5949 /* If we're stripping it, and it's not a dynamic symbol, there's
5950 nothing else to do unless it is a forced local symbol. */
5953 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5957 sym.st_size = h->size;
5958 sym.st_other = h->other;
5959 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5960 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
5961 else if (h->root.type == bfd_link_hash_undefweak
5962 || h->root.type == bfd_link_hash_defweak)
5963 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
5965 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
5967 switch (h->root.type)
5970 case bfd_link_hash_new:
5974 case bfd_link_hash_undefined:
5975 input_sec = bfd_und_section_ptr;
5976 sym.st_shndx = SHN_UNDEF;
5979 case bfd_link_hash_undefweak:
5980 input_sec = bfd_und_section_ptr;
5981 sym.st_shndx = SHN_UNDEF;
5984 case bfd_link_hash_defined:
5985 case bfd_link_hash_defweak:
5987 input_sec = h->root.u.def.section;
5988 if (input_sec->output_section != NULL)
5991 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
5992 input_sec->output_section);
5993 if (sym.st_shndx == SHN_BAD)
5995 (*_bfd_error_handler)
5996 (_("%s: could not find output section %s for input section %s"),
5997 bfd_get_filename (finfo->output_bfd),
5998 input_sec->output_section->name,
6000 eoinfo->failed = true;
6004 /* ELF symbols in relocateable files are section relative,
6005 but in nonrelocateable files they are virtual
6007 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6008 if (! finfo->info->relocateable)
6009 sym.st_value += input_sec->output_section->vma;
6013 BFD_ASSERT (input_sec->owner == NULL
6014 || (input_sec->owner->flags & DYNAMIC) != 0);
6015 sym.st_shndx = SHN_UNDEF;
6016 input_sec = bfd_und_section_ptr;
6021 case bfd_link_hash_common:
6022 input_sec = h->root.u.c.p->section;
6023 sym.st_shndx = SHN_COMMON;
6024 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6027 case bfd_link_hash_indirect:
6028 /* These symbols are created by symbol versioning. They point
6029 to the decorated version of the name. For example, if the
6030 symbol foo@@GNU_1.2 is the default, which should be used when
6031 foo is used with no version, then we add an indirect symbol
6032 foo which points to foo@@GNU_1.2. We ignore these symbols,
6033 since the indirected symbol is already in the hash table. */
6036 case bfd_link_hash_warning:
6037 /* We can't represent these symbols in ELF, although a warning
6038 symbol may have come from a .gnu.warning.SYMBOL section. We
6039 just put the target symbol in the hash table. If the target
6040 symbol does not really exist, don't do anything. */
6041 if (h->root.u.i.link->type == bfd_link_hash_new)
6043 return (elf_link_output_extsym
6044 ((struct elf_link_hash_entry *) h->root.u.i.link, data));
6047 /* Give the processor backend a chance to tweak the symbol value,
6048 and also to finish up anything that needs to be done for this
6050 if ((h->dynindx != -1
6051 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6052 && elf_hash_table (finfo->info)->dynamic_sections_created)
6054 struct elf_backend_data *bed;
6056 bed = get_elf_backend_data (finfo->output_bfd);
6057 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6058 (finfo->output_bfd, finfo->info, h, &sym)))
6060 eoinfo->failed = true;
6065 /* If we are marking the symbol as undefined, and there are no
6066 non-weak references to this symbol from a regular object, then
6067 mark the symbol as weak undefined; if there are non-weak
6068 references, mark the symbol as strong. We can't do this earlier,
6069 because it might not be marked as undefined until the
6070 finish_dynamic_symbol routine gets through with it. */
6071 if (sym.st_shndx == SHN_UNDEF
6072 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
6073 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6074 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6078 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
6079 bindtype = STB_GLOBAL;
6081 bindtype = STB_WEAK;
6082 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6085 /* If a symbol is not defined locally, we clear the visibility
6087 if (! finfo->info->relocateable
6088 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6089 sym.st_other ^= ELF_ST_VISIBILITY (sym.st_other);
6091 /* If this symbol should be put in the .dynsym section, then put it
6092 there now. We have already know the symbol index. We also fill
6093 in the entry in the .hash section. */
6094 if (h->dynindx != -1
6095 && elf_hash_table (finfo->info)->dynamic_sections_created)
6099 size_t hash_entry_size;
6100 bfd_byte *bucketpos;
6102 Elf_External_Sym *esym;
6104 sym.st_name = h->dynstr_index;
6105 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
6106 elf_swap_symbol_out (finfo->output_bfd, &sym, (PTR) esym, (PTR) 0);
6108 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6109 bucket = h->elf_hash_value % bucketcount;
6111 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6112 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6113 + (bucket + 2) * hash_entry_size);
6114 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6115 bfd_put (8 * hash_entry_size, finfo->output_bfd, (bfd_vma) h->dynindx,
6117 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6118 ((bfd_byte *) finfo->hash_sec->contents
6119 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6121 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6123 Elf_Internal_Versym iversym;
6124 Elf_External_Versym *eversym;
6126 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6128 if (h->verinfo.verdef == NULL)
6129 iversym.vs_vers = 0;
6131 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6135 if (h->verinfo.vertree == NULL)
6136 iversym.vs_vers = 1;
6138 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6141 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
6142 iversym.vs_vers |= VERSYM_HIDDEN;
6144 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6145 eversym += h->dynindx;
6146 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6150 /* If we're stripping it, then it was just a dynamic symbol, and
6151 there's nothing else to do. */
6155 h->indx = bfd_get_symcount (finfo->output_bfd);
6157 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
6159 eoinfo->failed = true;
6166 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6167 originated from the section given by INPUT_REL_HDR) to the
6171 elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
6174 asection *input_section;
6175 Elf_Internal_Shdr *input_rel_hdr;
6176 Elf_Internal_Rela *internal_relocs;
6178 Elf_Internal_Rela *irela;
6179 Elf_Internal_Rela *irelaend;
6180 Elf_Internal_Shdr *output_rel_hdr;
6181 asection *output_section;
6182 unsigned int *rel_countp = NULL;
6183 struct elf_backend_data *bed;
6186 output_section = input_section->output_section;
6187 output_rel_hdr = NULL;
6189 if (elf_section_data (output_section)->rel_hdr.sh_entsize
6190 == input_rel_hdr->sh_entsize)
6192 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
6193 rel_countp = &elf_section_data (output_section)->rel_count;
6195 else if (elf_section_data (output_section)->rel_hdr2
6196 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
6197 == input_rel_hdr->sh_entsize))
6199 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
6200 rel_countp = &elf_section_data (output_section)->rel_count2;
6203 BFD_ASSERT (output_rel_hdr != NULL);
6205 bed = get_elf_backend_data (output_bfd);
6206 irela = internal_relocs;
6207 irelaend = irela + NUM_SHDR_ENTRIES (input_rel_hdr)
6208 * bed->s->int_rels_per_ext_rel;
6210 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
6212 Elf_External_Rel *erel;
6213 Elf_Internal_Rel *irel;
6215 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
6216 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
6219 (*_bfd_error_handler) (_("Error: out of memory"));
6223 erel = ((Elf_External_Rel *) output_rel_hdr->contents + *rel_countp);
6224 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erel++)
6228 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6230 irel[i].r_offset = irela[i].r_offset;
6231 irel[i].r_info = irela[i].r_info;
6232 BFD_ASSERT (irela[i].r_addend == 0);
6235 if (bed->s->swap_reloc_out)
6236 (*bed->s->swap_reloc_out) (output_bfd, irel, (PTR) erel);
6238 elf_swap_reloc_out (output_bfd, irel, erel);
6245 Elf_External_Rela *erela;
6247 BFD_ASSERT (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rela));
6249 erela = ((Elf_External_Rela *) output_rel_hdr->contents + *rel_countp);
6250 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erela++)
6251 if (bed->s->swap_reloca_out)
6252 (*bed->s->swap_reloca_out) (output_bfd, irela, (PTR) erela);
6254 elf_swap_reloca_out (output_bfd, irela, erela);
6257 /* Bump the counter, so that we know where to add the next set of
6259 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
6262 /* Link an input file into the linker output file. This function
6263 handles all the sections and relocations of the input file at once.
6264 This is so that we only have to read the local symbols once, and
6265 don't have to keep them in memory. */
6268 elf_link_input_bfd (finfo, input_bfd)
6269 struct elf_final_link_info *finfo;
6272 boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
6273 bfd *, asection *, bfd_byte *,
6274 Elf_Internal_Rela *,
6275 Elf_Internal_Sym *, asection **));
6277 Elf_Internal_Shdr *symtab_hdr;
6278 Elf_Internal_Shdr *shndx_hdr;
6281 Elf_External_Sym *external_syms;
6282 Elf_External_Sym *esym;
6283 Elf_External_Sym *esymend;
6284 Elf_External_Sym_Shndx *shndx_buf;
6285 Elf_External_Sym_Shndx *shndx;
6286 Elf_Internal_Sym *isym;
6288 asection **ppsection;
6290 struct elf_backend_data *bed;
6291 boolean emit_relocs;
6292 struct elf_link_hash_entry **sym_hashes;
6294 output_bfd = finfo->output_bfd;
6295 bed = get_elf_backend_data (output_bfd);
6296 relocate_section = bed->elf_backend_relocate_section;
6298 /* If this is a dynamic object, we don't want to do anything here:
6299 we don't want the local symbols, and we don't want the section
6301 if ((input_bfd->flags & DYNAMIC) != 0)
6304 emit_relocs = (finfo->info->relocateable
6305 || finfo->info->emitrelocations
6306 || bed->elf_backend_emit_relocs);
6308 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6309 if (elf_bad_symtab (input_bfd))
6311 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6316 locsymcount = symtab_hdr->sh_info;
6317 extsymoff = symtab_hdr->sh_info;
6320 /* Read the local symbols. */
6321 if (symtab_hdr->contents != NULL)
6322 external_syms = (Elf_External_Sym *) symtab_hdr->contents;
6323 else if (locsymcount == 0)
6324 external_syms = NULL;
6327 bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym);
6328 external_syms = finfo->external_syms;
6329 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
6330 || bfd_bread (external_syms, amt, input_bfd) != amt)
6334 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
6336 if (shndx_hdr->sh_size != 0 && locsymcount != 0)
6338 bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym_Shndx);
6339 shndx_buf = finfo->locsym_shndx;
6340 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
6341 || bfd_bread (shndx_buf, amt, input_bfd) != amt)
6345 /* Swap in the local symbols and write out the ones which we know
6346 are going into the output file. */
6347 for (esym = external_syms, esymend = esym + locsymcount,
6348 isym = finfo->internal_syms, pindex = finfo->indices,
6349 ppsection = finfo->sections, shndx = shndx_buf;
6351 esym++, isym++, pindex++, ppsection++,
6352 shndx = (shndx != NULL ? shndx + 1 : NULL))
6356 Elf_Internal_Sym osym;
6358 elf_swap_symbol_in (input_bfd, esym, shndx, isym);
6361 if (elf_bad_symtab (input_bfd))
6363 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6370 if (isym->st_shndx == SHN_UNDEF)
6371 isec = bfd_und_section_ptr;
6372 else if (isym->st_shndx < SHN_LORESERVE
6373 || isym->st_shndx > SHN_HIRESERVE)
6375 isec = section_from_elf_index (input_bfd, isym->st_shndx);
6377 && elf_section_data (isec)->sec_info_type == ELF_INFO_TYPE_MERGE
6378 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6380 _bfd_merged_section_offset (output_bfd, &isec,
6381 elf_section_data (isec)->sec_info,
6382 isym->st_value, (bfd_vma) 0);
6384 else if (isym->st_shndx == SHN_ABS)
6385 isec = bfd_abs_section_ptr;
6386 else if (isym->st_shndx == SHN_COMMON)
6387 isec = bfd_com_section_ptr;
6396 /* Don't output the first, undefined, symbol. */
6397 if (esym == external_syms)
6400 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6402 /* We never output section symbols. Instead, we use the
6403 section symbol of the corresponding section in the output
6408 /* If we are stripping all symbols, we don't want to output this
6410 if (finfo->info->strip == strip_all)
6413 /* If we are discarding all local symbols, we don't want to
6414 output this one. If we are generating a relocateable output
6415 file, then some of the local symbols may be required by
6416 relocs; we output them below as we discover that they are
6418 if (finfo->info->discard == discard_all)
6421 /* If this symbol is defined in a section which we are
6422 discarding, we don't need to keep it, but note that
6423 linker_mark is only reliable for sections that have contents.
6424 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6425 as well as linker_mark. */
6426 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6428 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6429 || (! finfo->info->relocateable
6430 && (isec->flags & SEC_EXCLUDE) != 0)))
6433 /* Get the name of the symbol. */
6434 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6439 /* See if we are discarding symbols with this name. */
6440 if ((finfo->info->strip == strip_some
6441 && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
6443 || (((finfo->info->discard == discard_sec_merge
6444 && (isec->flags & SEC_MERGE) && ! finfo->info->relocateable)
6445 || finfo->info->discard == discard_l)
6446 && bfd_is_local_label_name (input_bfd, name)))
6449 /* If we get here, we are going to output this symbol. */
6453 /* Adjust the section index for the output file. */
6454 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6455 isec->output_section);
6456 if (osym.st_shndx == SHN_BAD)
6459 *pindex = bfd_get_symcount (output_bfd);
6461 /* ELF symbols in relocateable files are section relative, but
6462 in executable files they are virtual addresses. Note that
6463 this code assumes that all ELF sections have an associated
6464 BFD section with a reasonable value for output_offset; below
6465 we assume that they also have a reasonable value for
6466 output_section. Any special sections must be set up to meet
6467 these requirements. */
6468 osym.st_value += isec->output_offset;
6469 if (! finfo->info->relocateable)
6470 osym.st_value += isec->output_section->vma;
6472 if (! elf_link_output_sym (finfo, name, &osym, isec))
6476 /* Relocate the contents of each section. */
6477 sym_hashes = elf_sym_hashes (input_bfd);
6478 for (o = input_bfd->sections; o != NULL; o = o->next)
6482 if (! o->linker_mark)
6484 /* This section was omitted from the link. */
6488 if ((o->flags & SEC_HAS_CONTENTS) == 0
6489 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
6492 if ((o->flags & SEC_LINKER_CREATED) != 0)
6494 /* Section was created by elf_link_create_dynamic_sections
6499 /* Get the contents of the section. They have been cached by a
6500 relaxation routine. Note that o is a section in an input
6501 file, so the contents field will not have been set by any of
6502 the routines which work on output files. */
6503 if (elf_section_data (o)->this_hdr.contents != NULL)
6504 contents = elf_section_data (o)->this_hdr.contents;
6507 contents = finfo->contents;
6508 if (! bfd_get_section_contents (input_bfd, o, contents,
6509 (file_ptr) 0, o->_raw_size))
6513 if ((o->flags & SEC_RELOC) != 0)
6515 Elf_Internal_Rela *internal_relocs;
6517 /* Get the swapped relocs. */
6518 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
6519 (input_bfd, o, finfo->external_relocs,
6520 finfo->internal_relocs, false));
6521 if (internal_relocs == NULL
6522 && o->reloc_count > 0)
6525 /* Run through the relocs looking for any against symbols
6526 from discarded sections and section symbols from
6527 removed link-once sections. Complain about relocs
6528 against discarded sections. Zero relocs against removed
6529 link-once sections. We should really complain if
6530 anything in the final link tries to use it, but
6531 DWARF-based exception handling might have an entry in
6532 .eh_frame to describe a routine in the linkonce section,
6533 and it turns out to be hard to remove the .eh_frame
6534 entry too. FIXME. */
6535 if (!finfo->info->relocateable
6536 && !elf_section_ignore_discarded_relocs (o))
6538 Elf_Internal_Rela *rel, *relend;
6540 rel = internal_relocs;
6541 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6542 for ( ; rel < relend; rel++)
6544 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
6546 if (r_symndx >= locsymcount
6547 || (elf_bad_symtab (input_bfd)
6548 && finfo->sections[r_symndx] == NULL))
6550 struct elf_link_hash_entry *h;
6552 h = sym_hashes[r_symndx - extsymoff];
6553 while (h->root.type == bfd_link_hash_indirect
6554 || h->root.type == bfd_link_hash_warning)
6555 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6557 /* Complain if the definition comes from a
6558 discarded section. */
6559 if ((h->root.type == bfd_link_hash_defined
6560 || h->root.type == bfd_link_hash_defweak)
6561 && elf_discarded_section (h->root.u.def.section))
6563 #if BFD_VERSION_DATE < 20031005
6564 if ((o->flags & SEC_DEBUGGING) != 0)
6566 #if BFD_VERSION_DATE > 20021005
6567 (*finfo->info->callbacks->warning)
6569 _("warning: relocation against removed section; zeroing"),
6570 NULL, input_bfd, o, rel->r_offset);
6572 BFD_ASSERT (r_symndx != 0);
6573 memset (rel, 0, sizeof (*rel));
6578 if (! ((*finfo->info->callbacks->undefined_symbol)
6579 (finfo->info, h->root.root.string,
6580 input_bfd, o, rel->r_offset,
6588 asection *sec = finfo->sections[r_symndx];
6590 if (sec != NULL && elf_discarded_section (sec))
6592 #if BFD_VERSION_DATE < 20031005
6593 if ((o->flags & SEC_DEBUGGING) != 0
6594 || (sec->flags & SEC_LINK_ONCE) != 0)
6596 #if BFD_VERSION_DATE > 20021005
6597 (*finfo->info->callbacks->warning)
6599 _("warning: relocation against removed section"),
6600 NULL, input_bfd, o, rel->r_offset);
6602 BFD_ASSERT (r_symndx != 0);
6604 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
6612 = _("local symbols in discarded section %s");
6614 = strlen (sec->name) + strlen (msg) - 1;
6615 char *buf = (char *) bfd_malloc (amt);
6618 sprintf (buf, msg, sec->name);
6620 buf = (char *) sec->name;
6621 ok = (*finfo->info->callbacks
6622 ->undefined_symbol) (finfo->info, buf,
6626 if (buf != sec->name)
6636 /* Relocate the section by invoking a back end routine.
6638 The back end routine is responsible for adjusting the
6639 section contents as necessary, and (if using Rela relocs
6640 and generating a relocateable output file) adjusting the
6641 reloc addend as necessary.
6643 The back end routine does not have to worry about setting
6644 the reloc address or the reloc symbol index.
6646 The back end routine is given a pointer to the swapped in
6647 internal symbols, and can access the hash table entries
6648 for the external symbols via elf_sym_hashes (input_bfd).
6650 When generating relocateable output, the back end routine
6651 must handle STB_LOCAL/STT_SECTION symbols specially. The
6652 output symbol is going to be a section symbol
6653 corresponding to the output section, which will require
6654 the addend to be adjusted. */
6656 if (! (*relocate_section) (output_bfd, finfo->info,
6657 input_bfd, o, contents,
6659 finfo->internal_syms,
6665 Elf_Internal_Rela *irela;
6666 Elf_Internal_Rela *irelaend;
6667 struct elf_link_hash_entry **rel_hash;
6668 Elf_Internal_Shdr *input_rel_hdr;
6669 unsigned int next_erel;
6670 void (*reloc_emitter) PARAMS ((bfd *, asection *,
6671 Elf_Internal_Shdr *,
6672 Elf_Internal_Rela *));
6674 /* Adjust the reloc addresses and symbol indices. */
6676 irela = internal_relocs;
6677 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6678 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6679 + elf_section_data (o->output_section)->rel_count
6680 + elf_section_data (o->output_section)->rel_count2);
6681 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6683 unsigned long r_symndx;
6686 if (next_erel == bed->s->int_rels_per_ext_rel)
6692 irela->r_offset += o->output_offset;
6694 /* Relocs in an executable have to be virtual addresses. */
6695 if (finfo->info->emitrelocations)
6696 irela->r_offset += o->output_section->vma;
6698 r_symndx = ELF_R_SYM (irela->r_info);
6703 if (r_symndx >= locsymcount
6704 || (elf_bad_symtab (input_bfd)
6705 && finfo->sections[r_symndx] == NULL))
6707 struct elf_link_hash_entry *rh;
6710 /* This is a reloc against a global symbol. We
6711 have not yet output all the local symbols, so
6712 we do not know the symbol index of any global
6713 symbol. We set the rel_hash entry for this
6714 reloc to point to the global hash table entry
6715 for this symbol. The symbol index is then
6716 set at the end of elf_bfd_final_link. */
6717 indx = r_symndx - extsymoff;
6718 rh = elf_sym_hashes (input_bfd)[indx];
6719 while (rh->root.type == bfd_link_hash_indirect
6720 || rh->root.type == bfd_link_hash_warning)
6721 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6723 /* Setting the index to -2 tells
6724 elf_link_output_extsym that this symbol is
6726 BFD_ASSERT (rh->indx < 0);
6734 /* This is a reloc against a local symbol. */
6737 isym = finfo->internal_syms + r_symndx;
6738 sec = finfo->sections[r_symndx];
6739 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6741 /* I suppose the backend ought to fill in the
6742 section of any STT_SECTION symbol against a
6743 processor specific section. If we have
6744 discarded a section, the output_section will
6745 be the absolute section. */
6747 && (bfd_is_abs_section (sec)
6748 || (sec->output_section != NULL
6749 && bfd_is_abs_section (sec->output_section))))
6751 else if (sec == NULL || sec->owner == NULL)
6753 bfd_set_error (bfd_error_bad_value);
6758 r_symndx = sec->output_section->target_index;
6759 BFD_ASSERT (r_symndx != 0);
6764 if (finfo->indices[r_symndx] == -1)
6766 unsigned long shlink;
6770 if (finfo->info->strip == strip_all)
6772 /* You can't do ld -r -s. */
6773 bfd_set_error (bfd_error_invalid_operation);
6777 /* This symbol was skipped earlier, but
6778 since it is needed by a reloc, we
6779 must output it now. */
6780 shlink = symtab_hdr->sh_link;
6781 name = (bfd_elf_string_from_elf_section
6782 (input_bfd, shlink, isym->st_name));
6786 osec = sec->output_section;
6788 _bfd_elf_section_from_bfd_section (output_bfd,
6790 if (isym->st_shndx == SHN_BAD)
6793 isym->st_value += sec->output_offset;
6794 if (! finfo->info->relocateable)
6795 isym->st_value += osec->vma;
6797 finfo->indices[r_symndx]
6798 = bfd_get_symcount (output_bfd);
6800 if (! elf_link_output_sym (finfo, name, isym, sec))
6804 r_symndx = finfo->indices[r_symndx];
6807 irela->r_info = ELF_R_INFO (r_symndx,
6808 ELF_R_TYPE (irela->r_info));
6811 /* Swap out the relocs. */
6812 if (bed->elf_backend_emit_relocs
6813 && !(finfo->info->relocateable
6814 || finfo->info->emitrelocations))
6815 reloc_emitter = bed->elf_backend_emit_relocs;
6817 reloc_emitter = elf_link_output_relocs;
6819 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6820 (*reloc_emitter) (output_bfd, o, input_rel_hdr, internal_relocs);
6822 input_rel_hdr = elf_section_data (o)->rel_hdr2;
6825 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
6826 * bed->s->int_rels_per_ext_rel);
6827 reloc_emitter (output_bfd, o, input_rel_hdr, internal_relocs);
6833 /* Write out the modified section contents. */
6834 if (bed->elf_backend_write_section
6835 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
6837 /* Section written out. */
6839 else switch (elf_section_data (o)->sec_info_type)
6841 case ELF_INFO_TYPE_STABS:
6842 if (! (_bfd_write_section_stabs
6844 &elf_hash_table (finfo->info)->stab_info,
6845 o, &elf_section_data (o)->sec_info, contents)))
6848 case ELF_INFO_TYPE_MERGE:
6849 if (! (_bfd_write_merged_section
6850 (output_bfd, o, elf_section_data (o)->sec_info)))
6853 case ELF_INFO_TYPE_EH_FRAME:
6858 = bfd_get_section_by_name (elf_hash_table (finfo->info)->dynobj,
6860 if (! (_bfd_elf_write_section_eh_frame (output_bfd, o, ehdrsec,
6867 bfd_size_type sec_size;
6869 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
6870 if (! (o->flags & SEC_EXCLUDE)
6871 && ! bfd_set_section_contents (output_bfd, o->output_section,
6873 (file_ptr) o->output_offset,
6884 /* Generate a reloc when linking an ELF file. This is a reloc
6885 requested by the linker, and does come from any input file. This
6886 is used to build constructor and destructor tables when linking
6890 elf_reloc_link_order (output_bfd, info, output_section, link_order)
6892 struct bfd_link_info *info;
6893 asection *output_section;
6894 struct bfd_link_order *link_order;
6896 reloc_howto_type *howto;
6900 struct elf_link_hash_entry **rel_hash_ptr;
6901 Elf_Internal_Shdr *rel_hdr;
6902 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
6904 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
6907 bfd_set_error (bfd_error_bad_value);
6911 addend = link_order->u.reloc.p->addend;
6913 /* Figure out the symbol index. */
6914 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
6915 + elf_section_data (output_section)->rel_count
6916 + elf_section_data (output_section)->rel_count2);
6917 if (link_order->type == bfd_section_reloc_link_order)
6919 indx = link_order->u.reloc.p->u.section->target_index;
6920 BFD_ASSERT (indx != 0);
6921 *rel_hash_ptr = NULL;
6925 struct elf_link_hash_entry *h;
6927 /* Treat a reloc against a defined symbol as though it were
6928 actually against the section. */
6929 h = ((struct elf_link_hash_entry *)
6930 bfd_wrapped_link_hash_lookup (output_bfd, info,
6931 link_order->u.reloc.p->u.name,
6932 false, false, true));
6934 && (h->root.type == bfd_link_hash_defined
6935 || h->root.type == bfd_link_hash_defweak))
6939 section = h->root.u.def.section;
6940 indx = section->output_section->target_index;
6941 *rel_hash_ptr = NULL;
6942 /* It seems that we ought to add the symbol value to the
6943 addend here, but in practice it has already been added
6944 because it was passed to constructor_callback. */
6945 addend += section->output_section->vma + section->output_offset;
6949 /* Setting the index to -2 tells elf_link_output_extsym that
6950 this symbol is used by a reloc. */
6957 if (! ((*info->callbacks->unattached_reloc)
6958 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
6959 (asection *) NULL, (bfd_vma) 0)))
6965 /* If this is an inplace reloc, we must write the addend into the
6967 if (howto->partial_inplace && addend != 0)
6970 bfd_reloc_status_type rstat;
6973 const char *sym_name;
6975 size = bfd_get_reloc_size (howto);
6976 buf = (bfd_byte *) bfd_zmalloc (size);
6977 if (buf == (bfd_byte *) NULL)
6979 rstat = _bfd_relocate_contents (howto, output_bfd, (bfd_vma) addend, buf);
6986 case bfd_reloc_outofrange:
6989 case bfd_reloc_overflow:
6990 if (link_order->type == bfd_section_reloc_link_order)
6991 sym_name = bfd_section_name (output_bfd,
6992 link_order->u.reloc.p->u.section);
6994 sym_name = link_order->u.reloc.p->u.name;
6995 if (! ((*info->callbacks->reloc_overflow)
6996 (info, sym_name, howto->name, addend,
6997 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
7004 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
7005 (file_ptr) link_order->offset, size);
7011 /* The address of a reloc is relative to the section in a
7012 relocateable file, and is a virtual address in an executable
7014 offset = link_order->offset;
7015 if (! info->relocateable)
7016 offset += output_section->vma;
7018 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7020 if (rel_hdr->sh_type == SHT_REL)
7023 Elf_Internal_Rel *irel;
7024 Elf_External_Rel *erel;
7027 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
7028 irel = (Elf_Internal_Rel *) bfd_zmalloc (size);
7032 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7033 irel[i].r_offset = offset;
7034 irel[0].r_info = ELF_R_INFO (indx, howto->type);
7036 erel = ((Elf_External_Rel *) rel_hdr->contents
7037 + elf_section_data (output_section)->rel_count);
7039 if (bed->s->swap_reloc_out)
7040 (*bed->s->swap_reloc_out) (output_bfd, irel, (bfd_byte *) erel);
7042 elf_swap_reloc_out (output_bfd, irel, erel);
7049 Elf_Internal_Rela *irela;
7050 Elf_External_Rela *erela;
7053 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
7054 irela = (Elf_Internal_Rela *) bfd_zmalloc (size);
7058 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7059 irela[i].r_offset = offset;
7060 irela[0].r_info = ELF_R_INFO (indx, howto->type);
7061 irela[0].r_addend = addend;
7063 erela = ((Elf_External_Rela *) rel_hdr->contents
7064 + elf_section_data (output_section)->rel_count);
7066 if (bed->s->swap_reloca_out)
7067 (*bed->s->swap_reloca_out) (output_bfd, irela, (bfd_byte *) erela);
7069 elf_swap_reloca_out (output_bfd, irela, erela);
7072 ++elf_section_data (output_section)->rel_count;
7077 /* Allocate a pointer to live in a linker created section. */
7080 elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
7082 struct bfd_link_info *info;
7083 elf_linker_section_t *lsect;
7084 struct elf_link_hash_entry *h;
7085 const Elf_Internal_Rela *rel;
7087 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
7088 elf_linker_section_pointers_t *linker_section_ptr;
7089 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7092 BFD_ASSERT (lsect != NULL);
7094 /* Is this a global symbol? */
7097 /* Has this symbol already been allocated? If so, our work is done. */
7098 if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
7103 ptr_linker_section_ptr = &h->linker_section_pointer;
7104 /* Make sure this symbol is output as a dynamic symbol. */
7105 if (h->dynindx == -1)
7107 if (! elf_link_record_dynamic_symbol (info, h))
7111 if (lsect->rel_section)
7112 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7116 /* Allocation of a pointer to a local symbol. */
7117 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
7119 /* Allocate a table to hold the local symbols if first time. */
7122 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
7123 register unsigned int i;
7126 amt *= sizeof (elf_linker_section_pointers_t *);
7127 ptr = (elf_linker_section_pointers_t **) bfd_alloc (abfd, amt);
7132 elf_local_ptr_offsets (abfd) = ptr;
7133 for (i = 0; i < num_symbols; i++)
7134 ptr[i] = (elf_linker_section_pointers_t *) 0;
7137 /* Has this symbol already been allocated? If so, our work is done. */
7138 if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
7143 ptr_linker_section_ptr = &ptr[r_symndx];
7147 /* If we are generating a shared object, we need to
7148 output a R_<xxx>_RELATIVE reloc so that the
7149 dynamic linker can adjust this GOT entry. */
7150 BFD_ASSERT (lsect->rel_section != NULL);
7151 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7155 /* Allocate space for a pointer in the linker section, and allocate
7156 a new pointer record from internal memory. */
7157 BFD_ASSERT (ptr_linker_section_ptr != NULL);
7158 amt = sizeof (elf_linker_section_pointers_t);
7159 linker_section_ptr = (elf_linker_section_pointers_t *) bfd_alloc (abfd, amt);
7161 if (!linker_section_ptr)
7164 linker_section_ptr->next = *ptr_linker_section_ptr;
7165 linker_section_ptr->addend = rel->r_addend;
7166 linker_section_ptr->which = lsect->which;
7167 linker_section_ptr->written_address_p = false;
7168 *ptr_linker_section_ptr = linker_section_ptr;
7171 if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
7173 linker_section_ptr->offset = (lsect->section->_raw_size
7174 - lsect->hole_size + (ARCH_SIZE / 8));
7175 lsect->hole_offset += ARCH_SIZE / 8;
7176 lsect->sym_offset += ARCH_SIZE / 8;
7177 if (lsect->sym_hash)
7179 /* Bump up symbol value if needed. */
7180 lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
7182 fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
7183 lsect->sym_hash->root.root.string,
7184 (long) ARCH_SIZE / 8,
7185 (long) lsect->sym_hash->root.u.def.value);
7191 linker_section_ptr->offset = lsect->section->_raw_size;
7193 lsect->section->_raw_size += ARCH_SIZE / 8;
7197 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7198 lsect->name, (long) linker_section_ptr->offset,
7199 (long) lsect->section->_raw_size);
7206 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7209 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7212 /* Fill in the address for a pointer generated in a linker section. */
7215 elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h,
7216 relocation, rel, relative_reloc)
7219 struct bfd_link_info *info;
7220 elf_linker_section_t *lsect;
7221 struct elf_link_hash_entry *h;
7223 const Elf_Internal_Rela *rel;
7226 elf_linker_section_pointers_t *linker_section_ptr;
7228 BFD_ASSERT (lsect != NULL);
7232 /* Handle global symbol. */
7233 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7234 (h->linker_section_pointer,
7238 BFD_ASSERT (linker_section_ptr != NULL);
7240 if (! elf_hash_table (info)->dynamic_sections_created
7243 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
7245 /* This is actually a static link, or it is a
7246 -Bsymbolic link and the symbol is defined
7247 locally. We must initialize this entry in the
7250 When doing a dynamic link, we create a .rela.<xxx>
7251 relocation entry to initialize the value. This
7252 is done in the finish_dynamic_symbol routine. */
7253 if (!linker_section_ptr->written_address_p)
7255 linker_section_ptr->written_address_p = true;
7256 bfd_put_ptr (output_bfd,
7257 relocation + linker_section_ptr->addend,
7258 (lsect->section->contents
7259 + linker_section_ptr->offset));
7265 /* Handle local symbol. */
7266 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7267 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
7268 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
7269 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7270 (elf_local_ptr_offsets (input_bfd)[r_symndx],
7274 BFD_ASSERT (linker_section_ptr != NULL);
7276 /* Write out pointer if it hasn't been rewritten out before. */
7277 if (!linker_section_ptr->written_address_p)
7279 linker_section_ptr->written_address_p = true;
7280 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
7281 lsect->section->contents + linker_section_ptr->offset);
7285 asection *srel = lsect->rel_section;
7286 Elf_Internal_Rela *outrel;
7287 Elf_External_Rela *erel;
7288 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7292 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
7293 outrel = (Elf_Internal_Rela *) bfd_zmalloc (amt);
7296 (*_bfd_error_handler) (_("Error: out of memory"));
7300 /* We need to generate a relative reloc for the dynamic
7304 srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
7306 lsect->rel_section = srel;
7309 BFD_ASSERT (srel != NULL);
7311 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7312 outrel[i].r_offset = (lsect->section->output_section->vma
7313 + lsect->section->output_offset
7314 + linker_section_ptr->offset);
7315 outrel[0].r_info = ELF_R_INFO (0, relative_reloc);
7316 outrel[0].r_addend = 0;
7317 erel = (Elf_External_Rela *) lsect->section->contents;
7318 erel += elf_section_data (lsect->section)->rel_count;
7319 elf_swap_reloca_out (output_bfd, outrel, erel);
7320 ++elf_section_data (lsect->section)->rel_count;
7327 relocation = (lsect->section->output_offset
7328 + linker_section_ptr->offset
7329 - lsect->hole_offset
7330 - lsect->sym_offset);
7334 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7335 lsect->name, (long) relocation, (long) relocation);
7338 /* Subtract out the addend, because it will get added back in by the normal
7340 return relocation - linker_section_ptr->addend;
7343 /* Garbage collect unused sections. */
7345 static boolean elf_gc_mark
7346 PARAMS ((struct bfd_link_info *info, asection *sec,
7347 asection * (*gc_mark_hook)
7348 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7349 struct elf_link_hash_entry *, Elf_Internal_Sym *))));
7351 static boolean elf_gc_sweep
7352 PARAMS ((struct bfd_link_info *info,
7353 boolean (*gc_sweep_hook)
7354 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7355 const Elf_Internal_Rela *relocs))));
7357 static boolean elf_gc_sweep_symbol
7358 PARAMS ((struct elf_link_hash_entry *h, PTR idxptr));
7360 static boolean elf_gc_allocate_got_offsets
7361 PARAMS ((struct elf_link_hash_entry *h, PTR offarg));
7363 static boolean elf_gc_propagate_vtable_entries_used
7364 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7366 static boolean elf_gc_smash_unused_vtentry_relocs
7367 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7369 /* The mark phase of garbage collection. For a given section, mark
7370 it and any sections in this section's group, and all the sections
7371 which define symbols to which it refers. */
7374 elf_gc_mark (info, sec, gc_mark_hook)
7375 struct bfd_link_info *info;
7377 asection * (*gc_mark_hook)
7378 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7379 struct elf_link_hash_entry *, Elf_Internal_Sym *));
7382 asection *group_sec;
7386 /* Mark all the sections in the group. */
7387 group_sec = elf_section_data (sec)->next_in_group;
7388 if (group_sec && !group_sec->gc_mark)
7389 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
7392 /* Look through the section relocs. */
7394 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
7396 Elf_Internal_Rela *relstart, *rel, *relend;
7397 Elf_Internal_Shdr *symtab_hdr;
7398 Elf_Internal_Shdr *shndx_hdr;
7399 struct elf_link_hash_entry **sym_hashes;
7402 Elf_External_Sym *locsyms, *freesyms = NULL;
7403 Elf_External_Sym_Shndx *locsym_shndx;
7404 bfd *input_bfd = sec->owner;
7405 struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
7407 /* GCFIXME: how to arrange so that relocs and symbols are not
7408 reread continually? */
7410 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7411 sym_hashes = elf_sym_hashes (input_bfd);
7413 /* Read the local symbols. */
7414 if (elf_bad_symtab (input_bfd))
7416 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
7420 extsymoff = nlocsyms = symtab_hdr->sh_info;
7422 if (symtab_hdr->contents)
7423 locsyms = (Elf_External_Sym *) symtab_hdr->contents;
7424 else if (nlocsyms == 0)
7428 bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym);
7429 locsyms = freesyms = bfd_malloc (amt);
7430 if (freesyms == NULL
7431 || bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
7432 || bfd_bread (locsyms, amt, input_bfd) != amt)
7439 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
7440 locsym_shndx = NULL;
7441 if (shndx_hdr->sh_size != 0 && nlocsyms != 0)
7443 bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym_Shndx);
7444 locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
7445 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
7446 || bfd_bread (locsym_shndx, amt, input_bfd) != amt)
7450 /* Read the relocations. */
7451 relstart = (NAME(_bfd_elf,link_read_relocs)
7452 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL,
7453 info->keep_memory));
7454 if (relstart == NULL)
7459 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7461 for (rel = relstart; rel < relend; rel++)
7463 unsigned long r_symndx;
7465 struct elf_link_hash_entry *h;
7468 r_symndx = ELF_R_SYM (rel->r_info);
7472 if (elf_bad_symtab (sec->owner))
7474 elf_swap_symbol_in (input_bfd,
7476 locsym_shndx + (locsym_shndx ? r_symndx : 0),
7478 if (ELF_ST_BIND (s.st_info) == STB_LOCAL)
7479 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
7482 h = sym_hashes[r_symndx - extsymoff];
7483 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
7486 else if (r_symndx >= nlocsyms)
7488 h = sym_hashes[r_symndx - extsymoff];
7489 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
7493 elf_swap_symbol_in (input_bfd,
7495 locsym_shndx + (locsym_shndx ? r_symndx : 0),
7497 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
7500 if (rsec && !rsec->gc_mark)
7501 if (!elf_gc_mark (info, rsec, gc_mark_hook))
7509 if (!info->keep_memory)
7519 /* The sweep phase of garbage collection. Remove all garbage sections. */
7522 elf_gc_sweep (info, gc_sweep_hook)
7523 struct bfd_link_info *info;
7524 boolean (*gc_sweep_hook)
7525 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7526 const Elf_Internal_Rela *relocs));
7530 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7534 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7537 for (o = sub->sections; o != NULL; o = o->next)
7539 /* Keep special sections. Keep .debug sections. */
7540 if ((o->flags & SEC_LINKER_CREATED)
7541 || (o->flags & SEC_DEBUGGING))
7547 /* Skip sweeping sections already excluded. */
7548 if (o->flags & SEC_EXCLUDE)
7551 /* Since this is early in the link process, it is simple
7552 to remove a section from the output. */
7553 o->flags |= SEC_EXCLUDE;
7555 /* But we also have to update some of the relocation
7556 info we collected before. */
7558 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
7560 Elf_Internal_Rela *internal_relocs;
7563 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
7564 (o->owner, o, NULL, NULL, info->keep_memory));
7565 if (internal_relocs == NULL)
7568 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
7570 if (!info->keep_memory)
7571 free (internal_relocs);
7579 /* Remove the symbols that were in the swept sections from the dynamic
7580 symbol table. GCFIXME: Anyone know how to get them out of the
7581 static symbol table as well? */
7585 elf_link_hash_traverse (elf_hash_table (info),
7586 elf_gc_sweep_symbol,
7589 elf_hash_table (info)->dynsymcount = i;
7595 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7598 elf_gc_sweep_symbol (h, idxptr)
7599 struct elf_link_hash_entry *h;
7602 int *idx = (int *) idxptr;
7604 if (h->dynindx != -1
7605 && ((h->root.type != bfd_link_hash_defined
7606 && h->root.type != bfd_link_hash_defweak)
7607 || h->root.u.def.section->gc_mark))
7608 h->dynindx = (*idx)++;
7613 /* Propogate collected vtable information. This is called through
7614 elf_link_hash_traverse. */
7617 elf_gc_propagate_vtable_entries_used (h, okp)
7618 struct elf_link_hash_entry *h;
7621 /* Those that are not vtables. */
7622 if (h->vtable_parent == NULL)
7625 /* Those vtables that do not have parents, we cannot merge. */
7626 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
7629 /* If we've already been done, exit. */
7630 if (h->vtable_entries_used && h->vtable_entries_used[-1])
7633 /* Make sure the parent's table is up to date. */
7634 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
7636 if (h->vtable_entries_used == NULL)
7638 /* None of this table's entries were referenced. Re-use the
7640 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
7641 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
7648 /* Or the parent's entries into ours. */
7649 cu = h->vtable_entries_used;
7651 pu = h->vtable_parent->vtable_entries_used;
7654 asection *sec = h->root.u.def.section;
7655 struct elf_backend_data *bed = get_elf_backend_data (sec->owner);
7656 int file_align = bed->s->file_align;
7658 n = h->vtable_parent->vtable_entries_size / file_align;
7673 elf_gc_smash_unused_vtentry_relocs (h, okp)
7674 struct elf_link_hash_entry *h;
7678 bfd_vma hstart, hend;
7679 Elf_Internal_Rela *relstart, *relend, *rel;
7680 struct elf_backend_data *bed;
7683 /* Take care of both those symbols that do not describe vtables as
7684 well as those that are not loaded. */
7685 if (h->vtable_parent == NULL)
7688 BFD_ASSERT (h->root.type == bfd_link_hash_defined
7689 || h->root.type == bfd_link_hash_defweak);
7691 sec = h->root.u.def.section;
7692 hstart = h->root.u.def.value;
7693 hend = hstart + h->size;
7695 relstart = (NAME(_bfd_elf,link_read_relocs)
7696 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, true));
7698 return *(boolean *) okp = false;
7699 bed = get_elf_backend_data (sec->owner);
7700 file_align = bed->s->file_align;
7702 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7704 for (rel = relstart; rel < relend; ++rel)
7705 if (rel->r_offset >= hstart && rel->r_offset < hend)
7707 /* If the entry is in use, do nothing. */
7708 if (h->vtable_entries_used
7709 && (rel->r_offset - hstart) < h->vtable_entries_size)
7711 bfd_vma entry = (rel->r_offset - hstart) / file_align;
7712 if (h->vtable_entries_used[entry])
7715 /* Otherwise, kill it. */
7716 rel->r_offset = rel->r_info = rel->r_addend = 0;
7722 /* Do mark and sweep of unused sections. */
7725 elf_gc_sections (abfd, info)
7727 struct bfd_link_info *info;
7731 asection * (*gc_mark_hook)
7732 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7733 struct elf_link_hash_entry *h, Elf_Internal_Sym *));
7735 if (!get_elf_backend_data (abfd)->can_gc_sections
7736 || info->relocateable || info->emitrelocations
7737 || elf_hash_table (info)->dynamic_sections_created)
7740 /* Apply transitive closure to the vtable entry usage info. */
7741 elf_link_hash_traverse (elf_hash_table (info),
7742 elf_gc_propagate_vtable_entries_used,
7747 /* Kill the vtable relocations that were not used. */
7748 elf_link_hash_traverse (elf_hash_table (info),
7749 elf_gc_smash_unused_vtentry_relocs,
7754 /* Grovel through relocs to find out who stays ... */
7756 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
7757 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7761 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7764 for (o = sub->sections; o != NULL; o = o->next)
7766 if (o->flags & SEC_KEEP)
7767 if (!elf_gc_mark (info, o, gc_mark_hook))
7772 /* ... and mark SEC_EXCLUDE for those that go. */
7773 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
7779 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
7782 elf_gc_record_vtinherit (abfd, sec, h, offset)
7785 struct elf_link_hash_entry *h;
7788 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7789 struct elf_link_hash_entry **search, *child;
7790 bfd_size_type extsymcount;
7792 /* The sh_info field of the symtab header tells us where the
7793 external symbols start. We don't care about the local symbols at
7795 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
7796 if (!elf_bad_symtab (abfd))
7797 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
7799 sym_hashes = elf_sym_hashes (abfd);
7800 sym_hashes_end = sym_hashes + extsymcount;
7802 /* Hunt down the child symbol, which is in this section at the same
7803 offset as the relocation. */
7804 for (search = sym_hashes; search != sym_hashes_end; ++search)
7806 if ((child = *search) != NULL
7807 && (child->root.type == bfd_link_hash_defined
7808 || child->root.type == bfd_link_hash_defweak)
7809 && child->root.u.def.section == sec
7810 && child->root.u.def.value == offset)
7814 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
7815 bfd_archive_filename (abfd), sec->name,
7816 (unsigned long) offset);
7817 bfd_set_error (bfd_error_invalid_operation);
7823 /* This *should* only be the absolute section. It could potentially
7824 be that someone has defined a non-global vtable though, which
7825 would be bad. It isn't worth paging in the local symbols to be
7826 sure though; that case should simply be handled by the assembler. */
7828 child->vtable_parent = (struct elf_link_hash_entry *) -1;
7831 child->vtable_parent = h;
7836 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
7839 elf_gc_record_vtentry (abfd, sec, h, addend)
7840 bfd *abfd ATTRIBUTE_UNUSED;
7841 asection *sec ATTRIBUTE_UNUSED;
7842 struct elf_link_hash_entry *h;
7845 struct elf_backend_data *bed = get_elf_backend_data (abfd);
7846 int file_align = bed->s->file_align;
7848 if (addend >= h->vtable_entries_size)
7851 boolean *ptr = h->vtable_entries_used;
7853 /* While the symbol is undefined, we have to be prepared to handle
7855 if (h->root.type == bfd_link_hash_undefined)
7862 /* Oops! We've got a reference past the defined end of
7863 the table. This is probably a bug -- shall we warn? */
7868 /* Allocate one extra entry for use as a "done" flag for the
7869 consolidation pass. */
7870 bytes = (size / file_align + 1) * sizeof (boolean);
7874 ptr = bfd_realloc (ptr - 1, (bfd_size_type) bytes);
7880 oldbytes = ((h->vtable_entries_size / file_align + 1)
7881 * sizeof (boolean));
7882 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
7886 ptr = bfd_zmalloc ((bfd_size_type) bytes);
7891 /* And arrange for that done flag to be at index -1. */
7892 h->vtable_entries_used = ptr + 1;
7893 h->vtable_entries_size = size;
7896 h->vtable_entries_used[addend / file_align] = true;
7901 /* And an accompanying bit to work out final got entry offsets once
7902 we're done. Should be called from final_link. */
7905 elf_gc_common_finalize_got_offsets (abfd, info)
7907 struct bfd_link_info *info;
7910 struct elf_backend_data *bed = get_elf_backend_data (abfd);
7913 /* The GOT offset is relative to the .got section, but the GOT header is
7914 put into the .got.plt section, if the backend uses it. */
7915 if (bed->want_got_plt)
7918 gotoff = bed->got_header_size;
7920 /* Do the local .got entries first. */
7921 for (i = info->input_bfds; i; i = i->link_next)
7923 bfd_signed_vma *local_got;
7924 bfd_size_type j, locsymcount;
7925 Elf_Internal_Shdr *symtab_hdr;
7927 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
7930 local_got = elf_local_got_refcounts (i);
7934 symtab_hdr = &elf_tdata (i)->symtab_hdr;
7935 if (elf_bad_symtab (i))
7936 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
7938 locsymcount = symtab_hdr->sh_info;
7940 for (j = 0; j < locsymcount; ++j)
7942 if (local_got[j] > 0)
7944 local_got[j] = gotoff;
7945 gotoff += ARCH_SIZE / 8;
7948 local_got[j] = (bfd_vma) -1;
7952 /* Then the global .got entries. .plt refcounts are handled by
7953 adjust_dynamic_symbol */
7954 elf_link_hash_traverse (elf_hash_table (info),
7955 elf_gc_allocate_got_offsets,
7960 /* We need a special top-level link routine to convert got reference counts
7961 to real got offsets. */
7964 elf_gc_allocate_got_offsets (h, offarg)
7965 struct elf_link_hash_entry *h;
7968 bfd_vma *off = (bfd_vma *) offarg;
7970 if (h->got.refcount > 0)
7972 h->got.offset = off[0];
7973 off[0] += ARCH_SIZE / 8;
7976 h->got.offset = (bfd_vma) -1;
7981 /* Many folk need no more in the way of final link than this, once
7982 got entry reference counting is enabled. */
7985 elf_gc_common_final_link (abfd, info)
7987 struct bfd_link_info *info;
7989 if (!elf_gc_common_finalize_got_offsets (abfd, info))
7992 /* Invoke the regular ELF backend linker to do all the work. */
7993 return elf_bfd_final_link (abfd, info);
7996 /* This function will be called though elf_link_hash_traverse to store
7997 all hash value of the exported symbols in an array. */
8000 elf_collect_hash_codes (h, data)
8001 struct elf_link_hash_entry *h;
8004 unsigned long **valuep = (unsigned long **) data;
8010 /* Ignore indirect symbols. These are added by the versioning code. */
8011 if (h->dynindx == -1)
8014 name = h->root.root.string;
8015 p = strchr (name, ELF_VER_CHR);
8018 alc = bfd_malloc ((bfd_size_type) (p - name + 1));
8019 memcpy (alc, name, (size_t) (p - name));
8020 alc[p - name] = '\0';
8024 /* Compute the hash value. */
8025 ha = bfd_elf_hash (name);
8027 /* Store the found hash value in the array given as the argument. */
8030 /* And store it in the struct so that we can put it in the hash table
8032 h->elf_hash_value = ha;
8041 elf_reloc_symbol_deleted_p (offset, cookie)
8045 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
8047 if (rcookie->bad_symtab)
8048 rcookie->rel = rcookie->rels;
8050 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
8052 unsigned long r_symndx = ELF_R_SYM (rcookie->rel->r_info);
8053 Elf_Internal_Sym isym;
8055 if (! rcookie->bad_symtab)
8056 if (rcookie->rel->r_offset > offset)
8058 if (rcookie->rel->r_offset != offset)
8061 if (rcookie->locsyms && r_symndx < rcookie->locsymcount)
8063 Elf_External_Sym *lsym;
8064 Elf_External_Sym_Shndx *lshndx;
8066 lsym = (Elf_External_Sym *) rcookie->locsyms + r_symndx;
8067 lshndx = (Elf_External_Sym_Shndx *) rcookie->locsym_shndx;
8070 elf_swap_symbol_in (rcookie->abfd, lsym, lshndx, &isym);
8073 if (r_symndx >= rcookie->locsymcount
8074 || (rcookie->locsyms
8075 && ELF_ST_BIND (isym.st_info) != STB_LOCAL))
8077 struct elf_link_hash_entry *h;
8079 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
8081 while (h->root.type == bfd_link_hash_indirect
8082 || h->root.type == bfd_link_hash_warning)
8083 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8085 if ((h->root.type == bfd_link_hash_defined
8086 || h->root.type == bfd_link_hash_defweak)
8087 && elf_discarded_section (h->root.u.def.section))
8092 else if (rcookie->locsyms)
8094 /* It's not a relocation against a global symbol,
8095 but it could be a relocation against a local
8096 symbol for a discarded section. */
8099 /* Need to: get the symbol; get the section. */
8100 if (isym.st_shndx < SHN_LORESERVE || isym.st_shndx > SHN_HIRESERVE)
8102 isec = section_from_elf_index (rcookie->abfd, isym.st_shndx);
8103 if (isec != NULL && elf_discarded_section (isec))
8112 /* Discard unneeded references to discarded sections.
8113 Returns true if any section's size was changed. */
8114 /* This function assumes that the relocations are in sorted order,
8115 which is true for all known assemblers. */
8118 elf_bfd_discard_info (output_bfd, info)
8120 struct bfd_link_info *info;
8122 struct elf_reloc_cookie cookie;
8123 asection *stab, *eh, *ehdr;
8124 Elf_Internal_Shdr *symtab_hdr;
8125 Elf_Internal_Shdr *shndx_hdr;
8126 Elf_External_Sym *freesyms;
8127 struct elf_backend_data *bed;
8129 boolean ret = false;
8130 boolean strip = info->strip == strip_all || info->strip == strip_debugger;
8132 if (info->relocateable
8133 || info->traditional_format
8134 || info->hash->creator->flavour != bfd_target_elf_flavour
8135 || ! is_elf_hash_table (info))
8139 if (elf_hash_table (info)->dynobj != NULL)
8140 ehdr = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
8143 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
8145 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
8148 bed = get_elf_backend_data (abfd);
8150 if ((abfd->flags & DYNAMIC) != 0)
8156 eh = bfd_get_section_by_name (abfd, ".eh_frame");
8157 if (eh && eh->_raw_size == 0)
8161 stab = strip ? NULL : bfd_get_section_by_name (abfd, ".stab");
8162 if ((! stab || elf_section_data(stab)->sec_info_type != ELF_INFO_TYPE_STABS)
8164 && (strip || ! bed->elf_backend_discard_info))
8167 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8168 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8171 cookie.sym_hashes = elf_sym_hashes (abfd);
8172 cookie.bad_symtab = elf_bad_symtab (abfd);
8173 if (cookie.bad_symtab)
8175 cookie.locsymcount =
8176 symtab_hdr->sh_size / sizeof (Elf_External_Sym);
8177 cookie.extsymoff = 0;
8181 cookie.locsymcount = symtab_hdr->sh_info;
8182 cookie.extsymoff = symtab_hdr->sh_info;
8186 if (symtab_hdr->contents)
8187 cookie.locsyms = (void *) symtab_hdr->contents;
8188 else if (cookie.locsymcount == 0)
8189 cookie.locsyms = NULL;
8192 bfd_size_type amt = cookie.locsymcount * sizeof (Elf_External_Sym);
8193 cookie.locsyms = bfd_malloc (amt);
8194 if (cookie.locsyms == NULL)
8196 freesyms = cookie.locsyms;
8197 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
8198 || bfd_bread (cookie.locsyms, amt, abfd) != amt)
8201 free (cookie.locsyms);
8206 cookie.locsym_shndx = NULL;
8207 if (shndx_hdr->sh_size != 0 && cookie.locsymcount != 0)
8210 amt = cookie.locsymcount * sizeof (Elf_External_Sym_Shndx);
8211 cookie.locsym_shndx = bfd_malloc (amt);
8212 if (cookie.locsym_shndx == NULL)
8213 goto error_ret_free_loc;
8214 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
8215 || bfd_bread (cookie.locsym_shndx, amt, abfd) != amt)
8217 free (cookie.locsym_shndx);
8218 goto error_ret_free_loc;
8224 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8225 (abfd, stab, (PTR) NULL,
8226 (Elf_Internal_Rela *) NULL,
8227 info->keep_memory));
8230 cookie.rel = cookie.rels;
8232 cookie.rels + stab->reloc_count * bed->s->int_rels_per_ext_rel;
8233 if (_bfd_discard_section_stabs (abfd, stab,
8234 elf_section_data (stab)->sec_info,
8235 elf_reloc_symbol_deleted_p,
8238 if (! info->keep_memory)
8247 cookie.relend = NULL;
8248 if (eh->reloc_count)
8249 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
8250 (abfd, eh, (PTR) NULL,
8251 (Elf_Internal_Rela *) NULL,
8252 info->keep_memory));
8255 cookie.rel = cookie.rels;
8257 cookie.rels + eh->reloc_count * bed->s->int_rels_per_ext_rel;
8259 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, ehdr,
8260 elf_reloc_symbol_deleted_p,
8263 if (! info->keep_memory)
8267 if (bed->elf_backend_discard_info)
8269 if (bed->elf_backend_discard_info (abfd, &cookie, info))
8273 if (cookie.locsym_shndx != NULL)
8274 free (cookie.locsym_shndx);
8276 if (freesyms != NULL)
8281 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd,
8288 elf_section_ignore_discarded_relocs (sec)
8291 switch (elf_section_data (sec)->sec_info_type)
8293 case ELF_INFO_TYPE_STABS:
8294 case ELF_INFO_TYPE_EH_FRAME:
8299 if ((get_elf_backend_data (sec->owner)->elf_backend_ignore_discarded_relocs
8301 && (*get_elf_backend_data (sec->owner)
8302 ->elf_backend_ignore_discarded_relocs) (sec))