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 *, Elf_Internal_Sym *,
43 asection **, bfd_vma *, struct elf_link_hash_entry **,
44 boolean *, boolean *, boolean *, boolean));
45 static boolean elf_export_symbol
46 PARAMS ((struct elf_link_hash_entry *, PTR));
47 static boolean elf_finalize_dynstr
48 PARAMS ((bfd *, struct bfd_link_info *));
49 static boolean elf_fix_symbol_flags
50 PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *));
51 static boolean elf_adjust_dynamic_symbol
52 PARAMS ((struct elf_link_hash_entry *, PTR));
53 static boolean elf_link_find_version_dependencies
54 PARAMS ((struct elf_link_hash_entry *, PTR));
55 static boolean elf_link_find_version_dependencies
56 PARAMS ((struct elf_link_hash_entry *, PTR));
57 static boolean elf_link_assign_sym_version
58 PARAMS ((struct elf_link_hash_entry *, PTR));
59 static boolean elf_collect_hash_codes
60 PARAMS ((struct elf_link_hash_entry *, PTR));
61 static boolean elf_link_read_relocs_from_section
62 PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *));
63 static size_t compute_bucket_count
64 PARAMS ((struct bfd_link_info *));
65 static void elf_link_output_relocs
66 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
67 static boolean elf_link_size_reloc_section
68 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
69 static void elf_link_adjust_relocs
70 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int,
71 struct elf_link_hash_entry **));
72 static int elf_link_sort_cmp1
73 PARAMS ((const void *, const void *));
74 static int elf_link_sort_cmp2
75 PARAMS ((const void *, const void *));
76 static size_t elf_link_sort_relocs
77 PARAMS ((bfd *, struct bfd_link_info *, asection **));
78 static boolean elf_section_ignore_discarded_relocs
79 PARAMS ((asection *));
81 /* Given an ELF BFD, add symbols to the global hash table as
85 elf_bfd_link_add_symbols (abfd, info)
87 struct bfd_link_info *info;
89 switch (bfd_get_format (abfd))
92 return elf_link_add_object_symbols (abfd, info);
94 return elf_link_add_archive_symbols (abfd, info);
96 bfd_set_error (bfd_error_wrong_format);
101 /* Return true iff this is a non-common, definition of a non-function symbol. */
103 is_global_data_symbol_definition (abfd, sym)
104 bfd * abfd ATTRIBUTE_UNUSED;
105 Elf_Internal_Sym * sym;
107 /* Local symbols do not count, but target specific ones might. */
108 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
109 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
112 /* Function symbols do not count. */
113 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
116 /* If the section is undefined, then so is the symbol. */
117 if (sym->st_shndx == SHN_UNDEF)
120 /* If the symbol is defined in the common section, then
121 it is a common definition and so does not count. */
122 if (sym->st_shndx == SHN_COMMON)
125 /* If the symbol is in a target specific section then we
126 must rely upon the backend to tell us what it is. */
127 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
128 /* FIXME - this function is not coded yet:
130 return _bfd_is_global_symbol_definition (abfd, sym);
132 Instead for now assume that the definition is not global,
133 Even if this is wrong, at least the linker will behave
134 in the same way that it used to do. */
140 /* Search the symbol table of the archive element of the archive ABFD
141 whose archive map contains a mention of SYMDEF, and determine if
142 the symbol is defined in this element. */
144 elf_link_is_defined_archive_symbol (abfd, symdef)
148 Elf_Internal_Shdr * hdr;
149 Elf_External_Sym * esym;
150 Elf_External_Sym * esymend;
151 Elf_External_Sym * buf = NULL;
152 bfd_size_type symcount;
153 bfd_size_type extsymcount;
154 bfd_size_type extsymoff;
155 boolean result = false;
159 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
160 if (abfd == (bfd *) NULL)
163 if (! bfd_check_format (abfd, bfd_object))
166 /* If we have already included the element containing this symbol in the
167 link then we do not need to include it again. Just claim that any symbol
168 it contains is not a definition, so that our caller will not decide to
169 (re)include this element. */
170 if (abfd->archive_pass)
173 /* Select the appropriate symbol table. */
174 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
175 hdr = &elf_tdata (abfd)->symtab_hdr;
177 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
179 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
181 /* The sh_info field of the symtab header tells us where the
182 external symbols start. We don't care about the local symbols. */
183 if (elf_bad_symtab (abfd))
185 extsymcount = symcount;
190 extsymcount = symcount - hdr->sh_info;
191 extsymoff = hdr->sh_info;
194 amt = extsymcount * sizeof (Elf_External_Sym);
195 buf = (Elf_External_Sym *) bfd_malloc (amt);
196 if (buf == NULL && extsymcount != 0)
199 /* Read in the symbol table.
200 FIXME: This ought to be cached somewhere. */
201 pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
202 if (bfd_seek (abfd, pos, SEEK_SET) != 0
203 || bfd_bread ((PTR) buf, amt, abfd) != amt)
209 /* Scan the symbol table looking for SYMDEF. */
210 esymend = buf + extsymcount;
215 Elf_Internal_Sym sym;
218 elf_swap_symbol_in (abfd, esym, & sym);
220 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
221 if (name == (const char *) NULL)
224 if (strcmp (name, symdef->name) == 0)
226 result = is_global_data_symbol_definition (abfd, & sym);
236 /* Add symbols from an ELF archive file to the linker hash table. We
237 don't use _bfd_generic_link_add_archive_symbols because of a
238 problem which arises on UnixWare. The UnixWare libc.so is an
239 archive which includes an entry libc.so.1 which defines a bunch of
240 symbols. The libc.so archive also includes a number of other
241 object files, which also define symbols, some of which are the same
242 as those defined in libc.so.1. Correct linking requires that we
243 consider each object file in turn, and include it if it defines any
244 symbols we need. _bfd_generic_link_add_archive_symbols does not do
245 this; it looks through the list of undefined symbols, and includes
246 any object file which defines them. When this algorithm is used on
247 UnixWare, it winds up pulling in libc.so.1 early and defining a
248 bunch of symbols. This means that some of the other objects in the
249 archive are not included in the link, which is incorrect since they
250 precede libc.so.1 in the archive.
252 Fortunately, ELF archive handling is simpler than that done by
253 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
254 oddities. In ELF, if we find a symbol in the archive map, and the
255 symbol is currently undefined, we know that we must pull in that
258 Unfortunately, we do have to make multiple passes over the symbol
259 table until nothing further is resolved. */
262 elf_link_add_archive_symbols (abfd, info)
264 struct bfd_link_info *info;
267 boolean *defined = NULL;
268 boolean *included = NULL;
273 if (! bfd_has_map (abfd))
275 /* An empty archive is a special case. */
276 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
278 bfd_set_error (bfd_error_no_armap);
282 /* Keep track of all symbols we know to be already defined, and all
283 files we know to be already included. This is to speed up the
284 second and subsequent passes. */
285 c = bfd_ardata (abfd)->symdef_count;
289 amt *= sizeof (boolean);
290 defined = (boolean *) bfd_malloc (amt);
291 included = (boolean *) bfd_malloc (amt);
292 if (defined == (boolean *) NULL || included == (boolean *) NULL)
294 memset (defined, 0, (size_t) amt);
295 memset (included, 0, (size_t) amt);
297 symdefs = bfd_ardata (abfd)->symdefs;
310 symdefend = symdef + c;
311 for (i = 0; symdef < symdefend; symdef++, i++)
313 struct elf_link_hash_entry *h;
315 struct bfd_link_hash_entry *undefs_tail;
318 if (defined[i] || included[i])
320 if (symdef->file_offset == last)
326 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
327 false, false, false);
333 /* If this is a default version (the name contains @@),
334 look up the symbol again without the version. The
335 effect is that references to the symbol without the
336 version will be matched by the default symbol in the
339 p = strchr (symdef->name, ELF_VER_CHR);
340 if (p == NULL || p[1] != ELF_VER_CHR)
343 copy = bfd_alloc (abfd, (bfd_size_type) (p - symdef->name + 1));
346 memcpy (copy, symdef->name, (size_t) (p - symdef->name));
347 copy[p - symdef->name] = '\0';
349 h = elf_link_hash_lookup (elf_hash_table (info), copy,
350 false, false, false);
352 bfd_release (abfd, copy);
358 if (h->root.type == bfd_link_hash_common)
360 /* We currently have a common symbol. The archive map contains
361 a reference to this symbol, so we may want to include it. We
362 only want to include it however, if this archive element
363 contains a definition of the symbol, not just another common
366 Unfortunately some archivers (including GNU ar) will put
367 declarations of common symbols into their archive maps, as
368 well as real definitions, so we cannot just go by the archive
369 map alone. Instead we must read in the element's symbol
370 table and check that to see what kind of symbol definition
372 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
375 else if (h->root.type != bfd_link_hash_undefined)
377 if (h->root.type != bfd_link_hash_undefweak)
382 /* We need to include this archive member. */
383 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
384 if (element == (bfd *) NULL)
387 if (! bfd_check_format (element, bfd_object))
390 /* Doublecheck that we have not included this object
391 already--it should be impossible, but there may be
392 something wrong with the archive. */
393 if (element->archive_pass != 0)
395 bfd_set_error (bfd_error_bad_value);
398 element->archive_pass = 1;
400 undefs_tail = info->hash->undefs_tail;
402 if (! (*info->callbacks->add_archive_element) (info, element,
405 if (! elf_link_add_object_symbols (element, info))
408 /* If there are any new undefined symbols, we need to make
409 another pass through the archive in order to see whether
410 they can be defined. FIXME: This isn't perfect, because
411 common symbols wind up on undefs_tail and because an
412 undefined symbol which is defined later on in this pass
413 does not require another pass. This isn't a bug, but it
414 does make the code less efficient than it could be. */
415 if (undefs_tail != info->hash->undefs_tail)
418 /* Look backward to mark all symbols from this object file
419 which we have already seen in this pass. */
423 included[mark] = true;
428 while (symdefs[mark].file_offset == symdef->file_offset);
430 /* We mark subsequent symbols from this object file as we go
431 on through the loop. */
432 last = symdef->file_offset;
443 if (defined != (boolean *) NULL)
445 if (included != (boolean *) NULL)
450 /* This function is called when we want to define a new symbol. It
451 handles the various cases which arise when we find a definition in
452 a dynamic object, or when there is already a definition in a
453 dynamic object. The new symbol is described by NAME, SYM, PSEC,
454 and PVALUE. We set SYM_HASH to the hash table entry. We set
455 OVERRIDE if the old symbol is overriding a new definition. We set
456 TYPE_CHANGE_OK if it is OK for the type to change. We set
457 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
458 change, we mean that we shouldn't warn if the type or size does
459 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
463 elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
464 override, type_change_ok, size_change_ok, dt_needed)
466 struct bfd_link_info *info;
468 Elf_Internal_Sym *sym;
471 struct elf_link_hash_entry **sym_hash;
473 boolean *type_change_ok;
474 boolean *size_change_ok;
478 struct elf_link_hash_entry *h;
481 boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
486 bind = ELF_ST_BIND (sym->st_info);
488 if (! bfd_is_und_section (sec))
489 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
491 h = ((struct elf_link_hash_entry *)
492 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
497 /* This code is for coping with dynamic objects, and is only useful
498 if we are doing an ELF link. */
499 if (info->hash->creator != abfd->xvec)
502 /* For merging, we only care about real symbols. */
504 while (h->root.type == bfd_link_hash_indirect
505 || h->root.type == bfd_link_hash_warning)
506 h = (struct elf_link_hash_entry *) h->root.u.i.link;
508 /* If we just created the symbol, mark it as being an ELF symbol.
509 Other than that, there is nothing to do--there is no merge issue
510 with a newly defined symbol--so we just return. */
512 if (h->root.type == bfd_link_hash_new)
514 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
518 /* OLDBFD is a BFD associated with the existing symbol. */
520 switch (h->root.type)
526 case bfd_link_hash_undefined:
527 case bfd_link_hash_undefweak:
528 oldbfd = h->root.u.undef.abfd;
531 case bfd_link_hash_defined:
532 case bfd_link_hash_defweak:
533 oldbfd = h->root.u.def.section->owner;
536 case bfd_link_hash_common:
537 oldbfd = h->root.u.c.p->section->owner;
541 /* In cases involving weak versioned symbols, we may wind up trying
542 to merge a symbol with itself. Catch that here, to avoid the
543 confusion that results if we try to override a symbol with
544 itself. The additional tests catch cases like
545 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
546 dynamic object, which we do want to handle here. */
548 && ((abfd->flags & DYNAMIC) == 0
549 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
552 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
553 respectively, is from a dynamic object. */
555 if ((abfd->flags & DYNAMIC) != 0)
561 olddyn = (oldbfd->flags & DYNAMIC) != 0;
566 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
567 indices used by MIPS ELF. */
568 switch (h->root.type)
574 case bfd_link_hash_defined:
575 case bfd_link_hash_defweak:
576 hsec = h->root.u.def.section;
579 case bfd_link_hash_common:
580 hsec = h->root.u.c.p->section;
587 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
590 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
591 respectively, appear to be a definition rather than reference. */
593 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
598 if (h->root.type == bfd_link_hash_undefined
599 || h->root.type == bfd_link_hash_undefweak
600 || h->root.type == bfd_link_hash_common)
605 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
606 symbol, respectively, appears to be a common symbol in a dynamic
607 object. If a symbol appears in an uninitialized section, and is
608 not weak, and is not a function, then it may be a common symbol
609 which was resolved when the dynamic object was created. We want
610 to treat such symbols specially, because they raise special
611 considerations when setting the symbol size: if the symbol
612 appears as a common symbol in a regular object, and the size in
613 the regular object is larger, we must make sure that we use the
614 larger size. This problematic case can always be avoided in C,
615 but it must be handled correctly when using Fortran shared
618 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
619 likewise for OLDDYNCOMMON and OLDDEF.
621 Note that this test is just a heuristic, and that it is quite
622 possible to have an uninitialized symbol in a shared object which
623 is really a definition, rather than a common symbol. This could
624 lead to some minor confusion when the symbol really is a common
625 symbol in some regular object. However, I think it will be
630 && (sec->flags & SEC_ALLOC) != 0
631 && (sec->flags & SEC_LOAD) == 0
634 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
637 newdyncommon = false;
641 && h->root.type == bfd_link_hash_defined
642 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
643 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
644 && (h->root.u.def.section->flags & SEC_LOAD) == 0
646 && h->type != STT_FUNC)
649 olddyncommon = false;
651 /* It's OK to change the type if either the existing symbol or the
652 new symbol is weak unless it comes from a DT_NEEDED entry of
653 a shared object, in which case, the DT_NEEDED entry may not be
654 required at the run time. */
656 if ((! dt_needed && h->root.type == bfd_link_hash_defweak)
657 || h->root.type == bfd_link_hash_undefweak
659 *type_change_ok = true;
661 /* It's OK to change the size if either the existing symbol or the
662 new symbol is weak, or if the old symbol is undefined. */
665 || h->root.type == bfd_link_hash_undefined)
666 *size_change_ok = true;
668 /* If both the old and the new symbols look like common symbols in a
669 dynamic object, set the size of the symbol to the larger of the
674 && sym->st_size != h->size)
676 /* Since we think we have two common symbols, issue a multiple
677 common warning if desired. Note that we only warn if the
678 size is different. If the size is the same, we simply let
679 the old symbol override the new one as normally happens with
680 symbols defined in dynamic objects. */
682 if (! ((*info->callbacks->multiple_common)
683 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
684 h->size, abfd, bfd_link_hash_common, sym->st_size)))
687 if (sym->st_size > h->size)
688 h->size = sym->st_size;
690 *size_change_ok = true;
693 /* If we are looking at a dynamic object, and we have found a
694 definition, we need to see if the symbol was already defined by
695 some other object. If so, we want to use the existing
696 definition, and we do not want to report a multiple symbol
697 definition error; we do this by clobbering *PSEC to be
700 We treat a common symbol as a definition if the symbol in the
701 shared library is a function, since common symbols always
702 represent variables; this can cause confusion in principle, but
703 any such confusion would seem to indicate an erroneous program or
704 shared library. We also permit a common symbol in a regular
705 object to override a weak symbol in a shared object.
707 We prefer a non-weak definition in a shared library to a weak
708 definition in the executable unless it comes from a DT_NEEDED
709 entry of a shared object, in which case, the DT_NEEDED entry
710 may not be required at the run time. */
715 || (h->root.type == bfd_link_hash_common
717 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
718 && (h->root.type != bfd_link_hash_defweak
720 || bind == STB_WEAK))
724 newdyncommon = false;
726 *psec = sec = bfd_und_section_ptr;
727 *size_change_ok = true;
729 /* If we get here when the old symbol is a common symbol, then
730 we are explicitly letting it override a weak symbol or
731 function in a dynamic object, and we don't want to warn about
732 a type change. If the old symbol is a defined symbol, a type
733 change warning may still be appropriate. */
735 if (h->root.type == bfd_link_hash_common)
736 *type_change_ok = true;
739 /* Handle the special case of an old common symbol merging with a
740 new symbol which looks like a common symbol in a shared object.
741 We change *PSEC and *PVALUE to make the new symbol look like a
742 common symbol, and let _bfd_generic_link_add_one_symbol will do
746 && h->root.type == bfd_link_hash_common)
750 newdyncommon = false;
751 *pvalue = sym->st_size;
752 *psec = sec = bfd_com_section_ptr;
753 *size_change_ok = true;
756 /* If the old symbol is from a dynamic object, and the new symbol is
757 a definition which is not from a dynamic object, then the new
758 symbol overrides the old symbol. Symbols from regular files
759 always take precedence over symbols from dynamic objects, even if
760 they are defined after the dynamic object in the link.
762 As above, we again permit a common symbol in a regular object to
763 override a definition in a shared object if the shared object
764 symbol is a function or is weak.
766 As above, we permit a non-weak definition in a shared object to
767 override a weak definition in a regular object. */
771 || (bfd_is_com_section (sec)
772 && (h->root.type == bfd_link_hash_defweak
773 || h->type == STT_FUNC)))
776 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
778 || h->root.type == bfd_link_hash_defweak))
780 /* Change the hash table entry to undefined, and let
781 _bfd_generic_link_add_one_symbol do the right thing with the
784 h->root.type = bfd_link_hash_undefined;
785 h->root.u.undef.abfd = h->root.u.def.section->owner;
786 *size_change_ok = true;
789 olddyncommon = false;
791 /* We again permit a type change when a common symbol may be
792 overriding a function. */
794 if (bfd_is_com_section (sec))
795 *type_change_ok = true;
797 /* This union may have been set to be non-NULL when this symbol
798 was seen in a dynamic object. We must force the union to be
799 NULL, so that it is correct for a regular symbol. */
801 h->verinfo.vertree = NULL;
803 /* In this special case, if H is the target of an indirection,
804 we want the caller to frob with H rather than with the
805 indirect symbol. That will permit the caller to redefine the
806 target of the indirection, rather than the indirect symbol
807 itself. FIXME: This will break the -y option if we store a
808 symbol with a different name. */
812 /* Handle the special case of a new common symbol merging with an
813 old symbol that looks like it might be a common symbol defined in
814 a shared object. Note that we have already handled the case in
815 which a new common symbol should simply override the definition
816 in the shared library. */
819 && bfd_is_com_section (sec)
822 /* It would be best if we could set the hash table entry to a
823 common symbol, but we don't know what to use for the section
825 if (! ((*info->callbacks->multiple_common)
826 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
827 h->size, abfd, bfd_link_hash_common, sym->st_size)))
830 /* If the predumed common symbol in the dynamic object is
831 larger, pretend that the new symbol has its size. */
833 if (h->size > *pvalue)
836 /* FIXME: We no longer know the alignment required by the symbol
837 in the dynamic object, so we just wind up using the one from
838 the regular object. */
841 olddyncommon = false;
843 h->root.type = bfd_link_hash_undefined;
844 h->root.u.undef.abfd = h->root.u.def.section->owner;
846 *size_change_ok = true;
847 *type_change_ok = true;
849 h->verinfo.vertree = NULL;
852 /* Handle the special case of a weak definition in a regular object
853 followed by a non-weak definition in a shared object. In this
854 case, we prefer the definition in the shared object unless it
855 comes from a DT_NEEDED entry of a shared object, in which case,
856 the DT_NEEDED entry may not be required at the run time. */
859 && h->root.type == bfd_link_hash_defweak
864 /* To make this work we have to frob the flags so that the rest
865 of the code does not think we are using the regular
867 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
868 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
869 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
870 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
871 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
872 | ELF_LINK_HASH_DEF_DYNAMIC);
874 /* If H is the target of an indirection, we want the caller to
875 use H rather than the indirect symbol. Otherwise if we are
876 defining a new indirect symbol we will wind up attaching it
877 to the entry we are overriding. */
881 /* Handle the special case of a non-weak definition in a shared
882 object followed by a weak definition in a regular object. In
883 this case we prefer to definition in the shared object. To make
884 this work we have to tell the caller to not treat the new symbol
888 && h->root.type != bfd_link_hash_defweak
897 /* Add symbols from an ELF object file to the linker hash table. */
900 elf_link_add_object_symbols (abfd, info)
902 struct bfd_link_info *info;
904 boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
905 const Elf_Internal_Sym *,
906 const char **, flagword *,
907 asection **, bfd_vma *));
908 boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *,
909 asection *, const Elf_Internal_Rela *));
911 Elf_Internal_Shdr *hdr;
912 bfd_size_type symcount;
913 bfd_size_type extsymcount;
914 bfd_size_type extsymoff;
915 Elf_External_Sym *buf = NULL;
916 struct elf_link_hash_entry **sym_hash;
918 Elf_External_Versym *extversym = NULL;
919 Elf_External_Versym *ever;
920 Elf_External_Dyn *dynbuf = NULL;
921 struct elf_link_hash_entry *weaks;
922 Elf_External_Sym *esym;
923 Elf_External_Sym *esymend;
924 struct elf_backend_data *bed;
926 struct elf_link_hash_table * hash_table;
930 hash_table = elf_hash_table (info);
932 bed = get_elf_backend_data (abfd);
933 add_symbol_hook = bed->elf_add_symbol_hook;
934 collect = bed->collect;
936 if ((abfd->flags & DYNAMIC) == 0)
942 /* You can't use -r against a dynamic object. Also, there's no
943 hope of using a dynamic object which does not exactly match
944 the format of the output file. */
945 if (info->relocateable || info->hash->creator != abfd->xvec)
947 bfd_set_error (bfd_error_invalid_operation);
952 /* As a GNU extension, any input sections which are named
953 .gnu.warning.SYMBOL are treated as warning symbols for the given
954 symbol. This differs from .gnu.warning sections, which generate
955 warnings when they are included in an output file. */
960 for (s = abfd->sections; s != NULL; s = s->next)
964 name = bfd_get_section_name (abfd, s);
965 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
970 name += sizeof ".gnu.warning." - 1;
972 /* If this is a shared object, then look up the symbol
973 in the hash table. If it is there, and it is already
974 been defined, then we will not be using the entry
975 from this shared object, so we don't need to warn.
976 FIXME: If we see the definition in a regular object
977 later on, we will warn, but we shouldn't. The only
978 fix is to keep track of what warnings we are supposed
979 to emit, and then handle them all at the end of the
981 if (dynamic && abfd->xvec == info->hash->creator)
983 struct elf_link_hash_entry *h;
985 h = elf_link_hash_lookup (hash_table, name,
988 /* FIXME: What about bfd_link_hash_common? */
990 && (h->root.type == bfd_link_hash_defined
991 || h->root.type == bfd_link_hash_defweak))
993 /* We don't want to issue this warning. Clobber
994 the section size so that the warning does not
995 get copied into the output file. */
1001 sz = bfd_section_size (abfd, s);
1002 msg = (char *) bfd_alloc (abfd, sz + 1);
1006 if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
1011 if (! (_bfd_generic_link_add_one_symbol
1012 (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
1013 false, collect, (struct bfd_link_hash_entry **) NULL)))
1016 if (! info->relocateable)
1018 /* Clobber the section size so that the warning does
1019 not get copied into the output file. */
1026 /* If this is a dynamic object, we always link against the .dynsym
1027 symbol table, not the .symtab symbol table. The dynamic linker
1028 will only see the .dynsym symbol table, so there is no reason to
1029 look at .symtab for a dynamic object. */
1031 if (! dynamic || elf_dynsymtab (abfd) == 0)
1032 hdr = &elf_tdata (abfd)->symtab_hdr;
1034 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1038 /* Read in any version definitions. */
1040 if (! _bfd_elf_slurp_version_tables (abfd))
1043 /* Read in the symbol versions, but don't bother to convert them
1044 to internal format. */
1045 if (elf_dynversym (abfd) != 0)
1047 Elf_Internal_Shdr *versymhdr;
1049 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
1050 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
1051 if (extversym == NULL)
1053 amt = versymhdr->sh_size;
1054 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
1055 || bfd_bread ((PTR) extversym, amt, abfd) != amt)
1060 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1062 /* The sh_info field of the symtab header tells us where the
1063 external symbols start. We don't care about the local symbols at
1065 if (elf_bad_symtab (abfd))
1067 extsymcount = symcount;
1072 extsymcount = symcount - hdr->sh_info;
1073 extsymoff = hdr->sh_info;
1076 amt = extsymcount * sizeof (Elf_External_Sym);
1077 buf = (Elf_External_Sym *) bfd_malloc (amt);
1078 if (buf == NULL && extsymcount != 0)
1081 /* We store a pointer to the hash table entry for each external
1083 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
1084 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
1085 if (sym_hash == NULL)
1087 elf_sym_hashes (abfd) = sym_hash;
1093 /* If we are creating a shared library, create all the dynamic
1094 sections immediately. We need to attach them to something,
1095 so we attach them to this BFD, provided it is the right
1096 format. FIXME: If there are no input BFD's of the same
1097 format as the output, we can't make a shared library. */
1099 && is_elf_hash_table (info)
1100 && ! hash_table->dynamic_sections_created
1101 && abfd->xvec == info->hash->creator)
1103 if (! elf_link_create_dynamic_sections (abfd, info))
1107 else if (! is_elf_hash_table (info))
1114 bfd_size_type oldsize;
1115 bfd_size_type strindex;
1117 /* Find the name to use in a DT_NEEDED entry that refers to this
1118 object. If the object has a DT_SONAME entry, we use it.
1119 Otherwise, if the generic linker stuck something in
1120 elf_dt_name, we use that. Otherwise, we just use the file
1121 name. If the generic linker put a null string into
1122 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1123 there is a DT_SONAME entry. */
1125 name = bfd_get_filename (abfd);
1126 if (elf_dt_name (abfd) != NULL)
1128 name = elf_dt_name (abfd);
1131 if (elf_dt_soname (abfd) != NULL)
1137 s = bfd_get_section_by_name (abfd, ".dynamic");
1140 Elf_External_Dyn *extdyn;
1141 Elf_External_Dyn *extdynend;
1143 unsigned long shlink;
1147 dynbuf = (Elf_External_Dyn *) bfd_malloc (s->_raw_size);
1151 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
1152 (file_ptr) 0, s->_raw_size))
1155 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1158 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1161 /* The shared libraries distributed with hpux11 have a bogus
1162 sh_link field for the ".dynamic" section. This code detects
1163 when SHLINK refers to a section that is not a string table
1164 and tries to find the string table for the ".dynsym" section
1166 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[shlink];
1167 if (shdr->sh_type != SHT_STRTAB)
1169 asection *ds = bfd_get_section_by_name (abfd, ".dynsym");
1170 int elfdsec = _bfd_elf_section_from_bfd_section (abfd, ds);
1173 shlink = elf_elfsections (abfd)[elfdsec]->sh_link;
1178 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
1181 for (; extdyn < extdynend; extdyn++)
1183 Elf_Internal_Dyn dyn;
1185 elf_swap_dyn_in (abfd, extdyn, &dyn);
1186 if (dyn.d_tag == DT_SONAME)
1188 unsigned int tagv = dyn.d_un.d_val;
1189 name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1193 if (dyn.d_tag == DT_NEEDED)
1195 struct bfd_link_needed_list *n, **pn;
1197 unsigned int tagv = dyn.d_un.d_val;
1199 amt = sizeof (struct bfd_link_needed_list);
1200 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1201 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1202 if (n == NULL || fnm == NULL)
1204 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1211 for (pn = & hash_table->needed;
1217 if (dyn.d_tag == DT_RUNPATH)
1219 struct bfd_link_needed_list *n, **pn;
1221 unsigned int tagv = dyn.d_un.d_val;
1223 /* When we see DT_RPATH before DT_RUNPATH, we have
1224 to clear runpath. Do _NOT_ bfd_release, as that
1225 frees all more recently bfd_alloc'd blocks as
1227 if (rpath && hash_table->runpath)
1228 hash_table->runpath = NULL;
1230 amt = sizeof (struct bfd_link_needed_list);
1231 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1232 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1233 if (n == NULL || fnm == NULL)
1235 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1242 for (pn = & hash_table->runpath;
1250 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1251 if (!runpath && dyn.d_tag == DT_RPATH)
1253 struct bfd_link_needed_list *n, **pn;
1255 unsigned int tagv = dyn.d_un.d_val;
1257 amt = sizeof (struct bfd_link_needed_list);
1258 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1259 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1260 if (n == NULL || fnm == NULL)
1262 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
1269 for (pn = & hash_table->runpath;
1282 /* We do not want to include any of the sections in a dynamic
1283 object in the output file. We hack by simply clobbering the
1284 list of sections in the BFD. This could be handled more
1285 cleanly by, say, a new section flag; the existing
1286 SEC_NEVER_LOAD flag is not the one we want, because that one
1287 still implies that the section takes up space in the output
1289 abfd->sections = NULL;
1290 abfd->section_count = 0;
1292 /* If this is the first dynamic object found in the link, create
1293 the special sections required for dynamic linking. */
1294 if (! hash_table->dynamic_sections_created)
1295 if (! elf_link_create_dynamic_sections (abfd, info))
1300 /* Add a DT_NEEDED entry for this dynamic object. */
1301 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
1302 strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, false);
1303 if (strindex == (bfd_size_type) -1)
1306 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
1309 Elf_External_Dyn *dyncon, *dynconend;
1311 /* The hash table size did not change, which means that
1312 the dynamic object name was already entered. If we
1313 have already included this dynamic object in the
1314 link, just ignore it. There is no reason to include
1315 a particular dynamic object more than once. */
1316 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
1317 BFD_ASSERT (sdyn != NULL);
1319 dyncon = (Elf_External_Dyn *) sdyn->contents;
1320 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1322 for (; dyncon < dynconend; dyncon++)
1324 Elf_Internal_Dyn dyn;
1326 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
1327 if (dyn.d_tag == DT_NEEDED
1328 && dyn.d_un.d_val == strindex)
1332 if (extversym != NULL)
1334 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
1340 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
1344 /* Save the SONAME, if there is one, because sometimes the
1345 linker emulation code will need to know it. */
1347 name = basename (bfd_get_filename (abfd));
1348 elf_dt_name (abfd) = name;
1351 pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
1352 amt = extsymcount * sizeof (Elf_External_Sym);
1353 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1354 || bfd_bread ((PTR) buf, amt, abfd) != amt)
1359 ever = extversym != NULL ? extversym + extsymoff : NULL;
1360 esymend = buf + extsymcount;
1363 esym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
1365 Elf_Internal_Sym sym;
1371 struct elf_link_hash_entry *h;
1373 boolean size_change_ok, type_change_ok;
1374 boolean new_weakdef;
1375 unsigned int old_alignment;
1377 elf_swap_symbol_in (abfd, esym, &sym);
1379 flags = BSF_NO_FLAGS;
1381 value = sym.st_value;
1384 bind = ELF_ST_BIND (sym.st_info);
1385 if (bind == STB_LOCAL)
1387 /* This should be impossible, since ELF requires that all
1388 global symbols follow all local symbols, and that sh_info
1389 point to the first global symbol. Unfortunatealy, Irix 5
1393 else if (bind == STB_GLOBAL)
1395 if (sym.st_shndx != SHN_UNDEF
1396 && sym.st_shndx != SHN_COMMON)
1399 else if (bind == STB_WEAK)
1403 /* Leave it up to the processor backend. */
1406 if (sym.st_shndx == SHN_UNDEF)
1407 sec = bfd_und_section_ptr;
1408 else if (sym.st_shndx > 0 && sym.st_shndx < SHN_LORESERVE)
1410 sec = section_from_elf_index (abfd, sym.st_shndx);
1412 sec = bfd_abs_section_ptr;
1413 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1416 else if (sym.st_shndx == SHN_ABS)
1417 sec = bfd_abs_section_ptr;
1418 else if (sym.st_shndx == SHN_COMMON)
1420 sec = bfd_com_section_ptr;
1421 /* What ELF calls the size we call the value. What ELF
1422 calls the value we call the alignment. */
1423 value = sym.st_size;
1427 /* Leave it up to the processor backend. */
1430 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
1431 if (name == (const char *) NULL)
1434 if (add_symbol_hook)
1436 if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec,
1440 /* The hook function sets the name to NULL if this symbol
1441 should be skipped for some reason. */
1442 if (name == (const char *) NULL)
1446 /* Sanity check that all possibilities were handled. */
1447 if (sec == (asection *) NULL)
1449 bfd_set_error (bfd_error_bad_value);
1453 if (bfd_is_und_section (sec)
1454 || bfd_is_com_section (sec))
1459 size_change_ok = false;
1460 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
1462 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1464 Elf_Internal_Versym iver;
1465 unsigned int vernum = 0;
1470 _bfd_elf_swap_versym_in (abfd, ever, &iver);
1471 vernum = iver.vs_vers & VERSYM_VERSION;
1473 /* If this is a hidden symbol, or if it is not version
1474 1, we append the version name to the symbol name.
1475 However, we do not modify a non-hidden absolute
1476 symbol, because it might be the version symbol
1477 itself. FIXME: What if it isn't? */
1478 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
1479 || (vernum > 1 && ! bfd_is_abs_section (sec)))
1482 unsigned int namelen;
1483 bfd_size_type newlen;
1486 if (sym.st_shndx != SHN_UNDEF)
1488 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
1490 (*_bfd_error_handler)
1491 (_("%s: %s: invalid version %u (max %d)"),
1492 bfd_archive_filename (abfd), name, vernum,
1493 elf_tdata (abfd)->dynverdef_hdr.sh_info);
1494 bfd_set_error (bfd_error_bad_value);
1497 else if (vernum > 1)
1499 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1505 /* We cannot simply test for the number of
1506 entries in the VERNEED section since the
1507 numbers for the needed versions do not start
1509 Elf_Internal_Verneed *t;
1512 for (t = elf_tdata (abfd)->verref;
1516 Elf_Internal_Vernaux *a;
1518 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1520 if (a->vna_other == vernum)
1522 verstr = a->vna_nodename;
1531 (*_bfd_error_handler)
1532 (_("%s: %s: invalid needed version %d"),
1533 bfd_archive_filename (abfd), name, vernum);
1534 bfd_set_error (bfd_error_bad_value);
1539 namelen = strlen (name);
1540 newlen = namelen + strlen (verstr) + 2;
1541 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
1544 newname = (char *) bfd_alloc (abfd, newlen);
1545 if (newname == NULL)
1547 strcpy (newname, name);
1548 p = newname + namelen;
1550 /* If this is a defined non-hidden version symbol,
1551 we add another @ to the name. This indicates the
1552 default version of the symbol. */
1553 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1554 && sym.st_shndx != SHN_UNDEF)
1562 if (! elf_merge_symbol (abfd, info, name, &sym, &sec, &value,
1563 sym_hash, &override, &type_change_ok,
1564 &size_change_ok, dt_needed))
1571 while (h->root.type == bfd_link_hash_indirect
1572 || h->root.type == bfd_link_hash_warning)
1573 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1575 /* Remember the old alignment if this is a common symbol, so
1576 that we don't reduce the alignment later on. We can't
1577 check later, because _bfd_generic_link_add_one_symbol
1578 will set a default for the alignment which we want to
1580 if (h->root.type == bfd_link_hash_common)
1581 old_alignment = h->root.u.c.p->alignment_power;
1583 if (elf_tdata (abfd)->verdef != NULL
1587 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1590 if (! (_bfd_generic_link_add_one_symbol
1591 (info, abfd, name, flags, sec, value, (const char *) NULL,
1592 false, collect, (struct bfd_link_hash_entry **) sym_hash)))
1596 while (h->root.type == bfd_link_hash_indirect
1597 || h->root.type == bfd_link_hash_warning)
1598 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1601 new_weakdef = false;
1604 && (flags & BSF_WEAK) != 0
1605 && ELF_ST_TYPE (sym.st_info) != STT_FUNC
1606 && info->hash->creator->flavour == bfd_target_elf_flavour
1607 && h->weakdef == NULL)
1609 /* Keep a list of all weak defined non function symbols from
1610 a dynamic object, using the weakdef field. Later in this
1611 function we will set the weakdef field to the correct
1612 value. We only put non-function symbols from dynamic
1613 objects on this list, because that happens to be the only
1614 time we need to know the normal symbol corresponding to a
1615 weak symbol, and the information is time consuming to
1616 figure out. If the weakdef field is not already NULL,
1617 then this symbol was already defined by some previous
1618 dynamic object, and we will be using that previous
1619 definition anyhow. */
1626 /* Set the alignment of a common symbol. */
1627 if (sym.st_shndx == SHN_COMMON
1628 && h->root.type == bfd_link_hash_common)
1632 align = bfd_log2 (sym.st_value);
1633 if (align > old_alignment
1634 /* Permit an alignment power of zero if an alignment of one
1635 is specified and no other alignments have been specified. */
1636 || (sym.st_value == 1 && old_alignment == 0))
1637 h->root.u.c.p->alignment_power = align;
1640 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1646 /* Remember the symbol size and type. */
1647 if (sym.st_size != 0
1648 && (definition || h->size == 0))
1650 if (h->size != 0 && h->size != sym.st_size && ! size_change_ok)
1651 (*_bfd_error_handler)
1652 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1653 name, (unsigned long) h->size, (unsigned long) sym.st_size,
1654 bfd_archive_filename (abfd));
1656 h->size = sym.st_size;
1659 /* If this is a common symbol, then we always want H->SIZE
1660 to be the size of the common symbol. The code just above
1661 won't fix the size if a common symbol becomes larger. We
1662 don't warn about a size change here, because that is
1663 covered by --warn-common. */
1664 if (h->root.type == bfd_link_hash_common)
1665 h->size = h->root.u.c.size;
1667 if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE
1668 && (definition || h->type == STT_NOTYPE))
1670 if (h->type != STT_NOTYPE
1671 && h->type != ELF_ST_TYPE (sym.st_info)
1672 && ! type_change_ok)
1673 (*_bfd_error_handler)
1674 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1675 name, h->type, ELF_ST_TYPE (sym.st_info),
1676 bfd_archive_filename (abfd));
1678 h->type = ELF_ST_TYPE (sym.st_info);
1681 /* If st_other has a processor-specific meaning, specific code
1682 might be needed here. */
1683 if (sym.st_other != 0)
1685 /* Combine visibilities, using the most constraining one. */
1686 unsigned char hvis = ELF_ST_VISIBILITY (h->other);
1687 unsigned char symvis = ELF_ST_VISIBILITY (sym.st_other);
1689 if (symvis && (hvis > symvis || hvis == 0))
1690 h->other = sym.st_other;
1692 /* If neither has visibility, use the st_other of the
1693 definition. This is an arbitrary choice, since the
1694 other bits have no general meaning. */
1695 if (!symvis && !hvis
1696 && (definition || h->other == 0))
1697 h->other = sym.st_other;
1700 /* Set a flag in the hash table entry indicating the type of
1701 reference or definition we just found. Keep a count of
1702 the number of dynamic symbols we find. A dynamic symbol
1703 is one which is referenced or defined by both a regular
1704 object and a shared object. */
1705 old_flags = h->elf_link_hash_flags;
1711 new_flag = ELF_LINK_HASH_REF_REGULAR;
1712 if (bind != STB_WEAK)
1713 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
1716 new_flag = ELF_LINK_HASH_DEF_REGULAR;
1718 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
1719 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
1725 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
1727 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
1728 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
1729 | ELF_LINK_HASH_REF_REGULAR)) != 0
1730 || (h->weakdef != NULL
1732 && h->weakdef->dynindx != -1))
1736 h->elf_link_hash_flags |= new_flag;
1738 /* If this symbol has a version, and it is the default
1739 version, we create an indirect symbol from the default
1740 name to the fully decorated name. This will cause
1741 external references which do not specify a version to be
1742 bound to this version of the symbol. */
1743 if (definition || h->root.type == bfd_link_hash_common)
1747 p = strchr (name, ELF_VER_CHR);
1748 if (p != NULL && p[1] == ELF_VER_CHR)
1751 struct elf_link_hash_entry *hi;
1754 shortname = bfd_hash_allocate (&info->hash->table,
1755 (size_t) (p - name + 1));
1756 if (shortname == NULL)
1758 strncpy (shortname, name, (size_t) (p - name));
1759 shortname[p - name] = '\0';
1761 /* We are going to create a new symbol. Merge it
1762 with any existing symbol with this name. For the
1763 purposes of the merge, act as though we were
1764 defining the symbol we just defined, although we
1765 actually going to define an indirect symbol. */
1766 type_change_ok = false;
1767 size_change_ok = false;
1768 if (! elf_merge_symbol (abfd, info, shortname, &sym, &sec,
1769 &value, &hi, &override,
1771 &size_change_ok, dt_needed))
1776 if (! (_bfd_generic_link_add_one_symbol
1777 (info, abfd, shortname, BSF_INDIRECT,
1778 bfd_ind_section_ptr, (bfd_vma) 0, name, false,
1779 collect, (struct bfd_link_hash_entry **) &hi)))
1784 /* In this case the symbol named SHORTNAME is
1785 overriding the indirect symbol we want to
1786 add. We were planning on making SHORTNAME an
1787 indirect symbol referring to NAME. SHORTNAME
1788 is the name without a version. NAME is the
1789 fully versioned name, and it is the default
1792 Overriding means that we already saw a
1793 definition for the symbol SHORTNAME in a
1794 regular object, and it is overriding the
1795 symbol defined in the dynamic object.
1797 When this happens, we actually want to change
1798 NAME, the symbol we just added, to refer to
1799 SHORTNAME. This will cause references to
1800 NAME in the shared object to become
1801 references to SHORTNAME in the regular
1802 object. This is what we expect when we
1803 override a function in a shared object: that
1804 the references in the shared object will be
1805 mapped to the definition in the regular
1808 while (hi->root.type == bfd_link_hash_indirect
1809 || hi->root.type == bfd_link_hash_warning)
1810 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1812 h->root.type = bfd_link_hash_indirect;
1813 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1814 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1816 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1817 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1818 if (hi->elf_link_hash_flags
1819 & (ELF_LINK_HASH_REF_REGULAR
1820 | ELF_LINK_HASH_DEF_REGULAR))
1822 if (! _bfd_elf_link_record_dynamic_symbol (info,
1828 /* Now set HI to H, so that the following code
1829 will set the other fields correctly. */
1833 /* If there is a duplicate definition somewhere,
1834 then HI may not point to an indirect symbol. We
1835 will have reported an error to the user in that
1838 if (hi->root.type == bfd_link_hash_indirect)
1840 struct elf_link_hash_entry *ht;
1842 /* If the symbol became indirect, then we assume
1843 that we have not seen a definition before. */
1844 BFD_ASSERT ((hi->elf_link_hash_flags
1845 & (ELF_LINK_HASH_DEF_DYNAMIC
1846 | ELF_LINK_HASH_DEF_REGULAR))
1849 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1850 (*bed->elf_backend_copy_indirect_symbol) (ht, hi);
1852 /* See if the new flags lead us to realize that
1853 the symbol must be dynamic. */
1859 || ((hi->elf_link_hash_flags
1860 & ELF_LINK_HASH_REF_DYNAMIC)
1866 if ((hi->elf_link_hash_flags
1867 & ELF_LINK_HASH_REF_REGULAR) != 0)
1873 /* We also need to define an indirection from the
1874 nondefault version of the symbol. */
1876 shortname = bfd_hash_allocate (&info->hash->table,
1878 if (shortname == NULL)
1880 strncpy (shortname, name, (size_t) (p - name));
1881 strcpy (shortname + (p - name), p + 1);
1883 /* Once again, merge with any existing symbol. */
1884 type_change_ok = false;
1885 size_change_ok = false;
1886 if (! elf_merge_symbol (abfd, info, shortname, &sym, &sec,
1887 &value, &hi, &override,
1889 &size_change_ok, dt_needed))
1894 /* Here SHORTNAME is a versioned name, so we
1895 don't expect to see the type of override we
1896 do in the case above. */
1897 (*_bfd_error_handler)
1898 (_("%s: warning: unexpected redefinition of `%s'"),
1899 bfd_archive_filename (abfd), shortname);
1903 if (! (_bfd_generic_link_add_one_symbol
1904 (info, abfd, shortname, BSF_INDIRECT,
1905 bfd_ind_section_ptr, (bfd_vma) 0, name, false,
1906 collect, (struct bfd_link_hash_entry **) &hi)))
1909 /* If there is a duplicate definition somewhere,
1910 then HI may not point to an indirect symbol.
1911 We will have reported an error to the user in
1914 if (hi->root.type == bfd_link_hash_indirect)
1916 /* If the symbol became indirect, then we
1917 assume that we have not seen a definition
1919 BFD_ASSERT ((hi->elf_link_hash_flags
1920 & (ELF_LINK_HASH_DEF_DYNAMIC
1921 | ELF_LINK_HASH_DEF_REGULAR))
1924 (*bed->elf_backend_copy_indirect_symbol) (h, hi);
1926 /* See if the new flags lead us to realize
1927 that the symbol must be dynamic. */
1933 || ((hi->elf_link_hash_flags
1934 & ELF_LINK_HASH_REF_DYNAMIC)
1940 if ((hi->elf_link_hash_flags
1941 & ELF_LINK_HASH_REF_REGULAR) != 0)
1950 if (dynsym && h->dynindx == -1)
1952 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1954 if (h->weakdef != NULL
1956 && h->weakdef->dynindx == -1)
1958 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
1962 else if (dynsym && h->dynindx != -1)
1963 /* If the symbol already has a dynamic index, but
1964 visibility says it should not be visible, turn it into
1966 switch (ELF_ST_VISIBILITY (h->other))
1970 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
1971 (*bed->elf_backend_hide_symbol) (info, h);
1972 _bfd_elf_strtab_delref (hash_table->dynstr,
1977 if (dt_needed && definition
1978 && (h->elf_link_hash_flags
1979 & ELF_LINK_HASH_REF_REGULAR) != 0)
1981 bfd_size_type oldsize;
1982 bfd_size_type strindex;
1984 if (! is_elf_hash_table (info))
1987 /* The symbol from a DT_NEEDED object is referenced from
1988 the regular object to create a dynamic executable. We
1989 have to make sure there is a DT_NEEDED entry for it. */
1992 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
1993 strindex = _bfd_elf_strtab_add (hash_table->dynstr,
1994 elf_dt_soname (abfd), false);
1995 if (strindex == (bfd_size_type) -1)
1998 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2001 Elf_External_Dyn *dyncon, *dynconend;
2003 sdyn = bfd_get_section_by_name (hash_table->dynobj,
2005 BFD_ASSERT (sdyn != NULL);
2007 dyncon = (Elf_External_Dyn *) sdyn->contents;
2008 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2010 for (; dyncon < dynconend; dyncon++)
2012 Elf_Internal_Dyn dyn;
2014 elf_swap_dyn_in (hash_table->dynobj,
2016 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
2017 dyn.d_un.d_val != strindex);
2021 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
2027 /* Now set the weakdefs field correctly for all the weak defined
2028 symbols we found. The only way to do this is to search all the
2029 symbols. Since we only need the information for non functions in
2030 dynamic objects, that's the only time we actually put anything on
2031 the list WEAKS. We need this information so that if a regular
2032 object refers to a symbol defined weakly in a dynamic object, the
2033 real symbol in the dynamic object is also put in the dynamic
2034 symbols; we also must arrange for both symbols to point to the
2035 same memory location. We could handle the general case of symbol
2036 aliasing, but a general symbol alias can only be generated in
2037 assembler code, handling it correctly would be very time
2038 consuming, and other ELF linkers don't handle general aliasing
2040 while (weaks != NULL)
2042 struct elf_link_hash_entry *hlook;
2045 struct elf_link_hash_entry **hpp;
2046 struct elf_link_hash_entry **hppend;
2049 weaks = hlook->weakdef;
2050 hlook->weakdef = NULL;
2052 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
2053 || hlook->root.type == bfd_link_hash_defweak
2054 || hlook->root.type == bfd_link_hash_common
2055 || hlook->root.type == bfd_link_hash_indirect);
2056 slook = hlook->root.u.def.section;
2057 vlook = hlook->root.u.def.value;
2059 hpp = elf_sym_hashes (abfd);
2060 hppend = hpp + extsymcount;
2061 for (; hpp < hppend; hpp++)
2063 struct elf_link_hash_entry *h;
2066 if (h != NULL && h != hlook
2067 && h->root.type == bfd_link_hash_defined
2068 && h->root.u.def.section == slook
2069 && h->root.u.def.value == vlook)
2073 /* If the weak definition is in the list of dynamic
2074 symbols, make sure the real definition is put there
2076 if (hlook->dynindx != -1
2077 && h->dynindx == -1)
2079 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2083 /* If the real definition is in the list of dynamic
2084 symbols, make sure the weak definition is put there
2085 as well. If we don't do this, then the dynamic
2086 loader might not merge the entries for the real
2087 definition and the weak definition. */
2088 if (h->dynindx != -1
2089 && hlook->dynindx == -1)
2091 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
2106 if (extversym != NULL)
2112 /* If this object is the same format as the output object, and it is
2113 not a shared library, then let the backend look through the
2116 This is required to build global offset table entries and to
2117 arrange for dynamic relocs. It is not required for the
2118 particular common case of linking non PIC code, even when linking
2119 against shared libraries, but unfortunately there is no way of
2120 knowing whether an object file has been compiled PIC or not.
2121 Looking through the relocs is not particularly time consuming.
2122 The problem is that we must either (1) keep the relocs in memory,
2123 which causes the linker to require additional runtime memory or
2124 (2) read the relocs twice from the input file, which wastes time.
2125 This would be a good case for using mmap.
2127 I have no idea how to handle linking PIC code into a file of a
2128 different format. It probably can't be done. */
2129 check_relocs = get_elf_backend_data (abfd)->check_relocs;
2131 && abfd->xvec == info->hash->creator
2132 && check_relocs != NULL)
2136 for (o = abfd->sections; o != NULL; o = o->next)
2138 Elf_Internal_Rela *internal_relocs;
2141 if ((o->flags & SEC_RELOC) == 0
2142 || o->reloc_count == 0
2143 || ((info->strip == strip_all || info->strip == strip_debugger)
2144 && (o->flags & SEC_DEBUGGING) != 0)
2145 || bfd_is_abs_section (o->output_section))
2148 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
2149 (abfd, o, (PTR) NULL,
2150 (Elf_Internal_Rela *) NULL,
2151 info->keep_memory));
2152 if (internal_relocs == NULL)
2155 ok = (*check_relocs) (abfd, info, o, internal_relocs);
2157 if (! info->keep_memory)
2158 free (internal_relocs);
2165 /* If this is a non-traditional, non-relocateable link, try to
2166 optimize the handling of the .stab/.stabstr sections. */
2168 && ! info->relocateable
2169 && ! info->traditional_format
2170 && info->hash->creator->flavour == bfd_target_elf_flavour
2171 && is_elf_hash_table (info)
2172 && (info->strip != strip_all && info->strip != strip_debugger))
2174 asection *stab, *stabstr;
2176 stab = bfd_get_section_by_name (abfd, ".stab");
2179 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
2181 if (stabstr != NULL)
2183 struct bfd_elf_section_data *secdata;
2185 secdata = elf_section_data (stab);
2186 if (! _bfd_link_section_stabs (abfd,
2187 & hash_table->stab_info,
2189 &secdata->stab_info))
2195 if (! info->relocateable && ! dynamic
2196 && is_elf_hash_table (info))
2200 for (s = abfd->sections; s != NULL; s = s->next)
2201 if ((s->flags & SEC_MERGE)
2202 && ! _bfd_merge_section (abfd, & hash_table->merge_info, s,
2203 & elf_section_data (s)->merge_info))
2214 if (extversym != NULL)
2219 /* Create some sections which will be filled in with dynamic linking
2220 information. ABFD is an input file which requires dynamic sections
2221 to be created. The dynamic sections take up virtual memory space
2222 when the final executable is run, so we need to create them before
2223 addresses are assigned to the output sections. We work out the
2224 actual contents and size of these sections later. */
2227 elf_link_create_dynamic_sections (abfd, info)
2229 struct bfd_link_info *info;
2232 register asection *s;
2233 struct elf_link_hash_entry *h;
2234 struct elf_backend_data *bed;
2236 if (! is_elf_hash_table (info))
2239 if (elf_hash_table (info)->dynamic_sections_created)
2242 /* Make sure that all dynamic sections use the same input BFD. */
2243 if (elf_hash_table (info)->dynobj == NULL)
2244 elf_hash_table (info)->dynobj = abfd;
2246 abfd = elf_hash_table (info)->dynobj;
2248 /* Note that we set the SEC_IN_MEMORY flag for all of these
2250 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2251 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2253 /* A dynamically linked executable has a .interp section, but a
2254 shared library does not. */
2257 s = bfd_make_section (abfd, ".interp");
2259 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2263 /* Create sections to hold version informations. These are removed
2264 if they are not needed. */
2265 s = bfd_make_section (abfd, ".gnu.version_d");
2267 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2268 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2271 s = bfd_make_section (abfd, ".gnu.version");
2273 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2274 || ! bfd_set_section_alignment (abfd, s, 1))
2277 s = bfd_make_section (abfd, ".gnu.version_r");
2279 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2280 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2283 s = bfd_make_section (abfd, ".dynsym");
2285 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2286 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2289 s = bfd_make_section (abfd, ".dynstr");
2291 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2294 /* Create a strtab to hold the dynamic symbol names. */
2295 if (elf_hash_table (info)->dynstr == NULL)
2297 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
2298 if (elf_hash_table (info)->dynstr == NULL)
2302 s = bfd_make_section (abfd, ".dynamic");
2304 || ! bfd_set_section_flags (abfd, s, flags)
2305 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2308 /* The special symbol _DYNAMIC is always set to the start of the
2309 .dynamic section. This call occurs before we have processed the
2310 symbols for any dynamic object, so we don't have to worry about
2311 overriding a dynamic definition. We could set _DYNAMIC in a
2312 linker script, but we only want to define it if we are, in fact,
2313 creating a .dynamic section. We don't want to define it if there
2314 is no .dynamic section, since on some ELF platforms the start up
2315 code examines it to decide how to initialize the process. */
2317 if (! (_bfd_generic_link_add_one_symbol
2318 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
2319 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
2320 (struct bfd_link_hash_entry **) &h)))
2322 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2323 h->type = STT_OBJECT;
2326 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2329 bed = get_elf_backend_data (abfd);
2331 s = bfd_make_section (abfd, ".hash");
2333 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2334 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2336 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
2338 /* Let the backend create the rest of the sections. This lets the
2339 backend set the right flags. The backend will normally create
2340 the .got and .plt sections. */
2341 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
2344 elf_hash_table (info)->dynamic_sections_created = true;
2349 /* Add an entry to the .dynamic table. */
2352 elf_add_dynamic_entry (info, tag, val)
2353 struct bfd_link_info *info;
2357 Elf_Internal_Dyn dyn;
2360 bfd_size_type newsize;
2361 bfd_byte *newcontents;
2363 if (! is_elf_hash_table (info))
2366 dynobj = elf_hash_table (info)->dynobj;
2368 s = bfd_get_section_by_name (dynobj, ".dynamic");
2369 BFD_ASSERT (s != NULL);
2371 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
2372 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
2373 if (newcontents == NULL)
2377 dyn.d_un.d_val = val;
2378 elf_swap_dyn_out (dynobj, &dyn,
2379 (Elf_External_Dyn *) (newcontents + s->_raw_size));
2381 s->_raw_size = newsize;
2382 s->contents = newcontents;
2387 /* Record a new local dynamic symbol. */
2390 elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
2391 struct bfd_link_info *info;
2395 struct elf_link_local_dynamic_entry *entry;
2396 struct elf_link_hash_table *eht;
2397 struct elf_strtab_hash *dynstr;
2398 Elf_External_Sym esym;
2399 unsigned long dynstr_index;
2404 if (! is_elf_hash_table (info))
2407 /* See if the entry exists already. */
2408 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2409 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
2412 entry = (struct elf_link_local_dynamic_entry *)
2413 bfd_alloc (input_bfd, (bfd_size_type) sizeof (*entry));
2417 /* Go find the symbol, so that we can find it's name. */
2418 amt = sizeof (Elf_External_Sym);
2419 pos = elf_tdata (input_bfd)->symtab_hdr.sh_offset + input_indx * amt;
2420 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
2421 || bfd_bread (&esym, amt, input_bfd) != amt)
2423 elf_swap_symbol_in (input_bfd, &esym, &entry->isym);
2425 name = (bfd_elf_string_from_elf_section
2426 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
2427 entry->isym.st_name));
2429 dynstr = elf_hash_table (info)->dynstr;
2432 /* Create a strtab to hold the dynamic symbol names. */
2433 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
2438 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
2439 if (dynstr_index == (unsigned long) -1)
2441 entry->isym.st_name = dynstr_index;
2443 eht = elf_hash_table (info);
2445 entry->next = eht->dynlocal;
2446 eht->dynlocal = entry;
2447 entry->input_bfd = input_bfd;
2448 entry->input_indx = input_indx;
2451 /* Whatever binding the symbol had before, it's now local. */
2453 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
2455 /* The dynindx will be set at the end of size_dynamic_sections. */
2460 /* Read and swap the relocs from the section indicated by SHDR. This
2461 may be either a REL or a RELA section. The relocations are
2462 translated into RELA relocations and stored in INTERNAL_RELOCS,
2463 which should have already been allocated to contain enough space.
2464 The EXTERNAL_RELOCS are a buffer where the external form of the
2465 relocations should be stored.
2467 Returns false if something goes wrong. */
2470 elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
2473 Elf_Internal_Shdr *shdr;
2474 PTR external_relocs;
2475 Elf_Internal_Rela *internal_relocs;
2477 struct elf_backend_data *bed;
2480 /* If there aren't any relocations, that's OK. */
2484 /* Position ourselves at the start of the section. */
2485 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2488 /* Read the relocations. */
2489 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2492 bed = get_elf_backend_data (abfd);
2494 /* Convert the external relocations to the internal format. */
2495 if (shdr->sh_entsize == sizeof (Elf_External_Rel))
2497 Elf_External_Rel *erel;
2498 Elf_External_Rel *erelend;
2499 Elf_Internal_Rela *irela;
2500 Elf_Internal_Rel *irel;
2502 erel = (Elf_External_Rel *) external_relocs;
2503 erelend = erel + NUM_SHDR_ENTRIES (shdr);
2504 irela = internal_relocs;
2505 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
2506 irel = bfd_alloc (abfd, amt);
2507 for (; erel < erelend; erel++, irela += bed->s->int_rels_per_ext_rel)
2511 if (bed->s->swap_reloc_in)
2512 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
2514 elf_swap_reloc_in (abfd, erel, irel);
2516 for (i = 0; i < bed->s->int_rels_per_ext_rel; ++i)
2518 irela[i].r_offset = irel[i].r_offset;
2519 irela[i].r_info = irel[i].r_info;
2520 irela[i].r_addend = 0;
2526 Elf_External_Rela *erela;
2527 Elf_External_Rela *erelaend;
2528 Elf_Internal_Rela *irela;
2530 BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela));
2532 erela = (Elf_External_Rela *) external_relocs;
2533 erelaend = erela + NUM_SHDR_ENTRIES (shdr);
2534 irela = internal_relocs;
2535 for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel)
2537 if (bed->s->swap_reloca_in)
2538 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
2540 elf_swap_reloca_in (abfd, erela, irela);
2547 /* Read and swap the relocs for a section O. They may have been
2548 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2549 not NULL, they are used as buffers to read into. They are known to
2550 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2551 the return value is allocated using either malloc or bfd_alloc,
2552 according to the KEEP_MEMORY argument. If O has two relocation
2553 sections (both REL and RELA relocations), then the REL_HDR
2554 relocations will appear first in INTERNAL_RELOCS, followed by the
2555 REL_HDR2 relocations. */
2558 NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
2562 PTR external_relocs;
2563 Elf_Internal_Rela *internal_relocs;
2564 boolean keep_memory;
2566 Elf_Internal_Shdr *rel_hdr;
2568 Elf_Internal_Rela *alloc2 = NULL;
2569 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2571 if (elf_section_data (o)->relocs != NULL)
2572 return elf_section_data (o)->relocs;
2574 if (o->reloc_count == 0)
2577 rel_hdr = &elf_section_data (o)->rel_hdr;
2579 if (internal_relocs == NULL)
2583 size = o->reloc_count;
2584 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2586 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2588 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2589 if (internal_relocs == NULL)
2593 if (external_relocs == NULL)
2595 bfd_size_type size = rel_hdr->sh_size;
2597 if (elf_section_data (o)->rel_hdr2)
2598 size += elf_section_data (o)->rel_hdr2->sh_size;
2599 alloc1 = (PTR) bfd_malloc (size);
2602 external_relocs = alloc1;
2605 if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
2609 if (!elf_link_read_relocs_from_section
2611 elf_section_data (o)->rel_hdr2,
2612 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2613 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2614 * bed->s->int_rels_per_ext_rel)))
2617 /* Cache the results for next time, if we can. */
2619 elf_section_data (o)->relocs = internal_relocs;
2624 /* Don't free alloc2, since if it was allocated we are passing it
2625 back (under the name of internal_relocs). */
2627 return internal_relocs;
2637 /* Record an assignment to a symbol made by a linker script. We need
2638 this in case some dynamic object refers to this symbol. */
2641 NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
2642 bfd *output_bfd ATTRIBUTE_UNUSED;
2643 struct bfd_link_info *info;
2647 struct elf_link_hash_entry *h;
2649 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2652 h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
2656 if (h->root.type == bfd_link_hash_new)
2657 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
2659 /* If this symbol is being provided by the linker script, and it is
2660 currently defined by a dynamic object, but not by a regular
2661 object, then mark it as undefined so that the generic linker will
2662 force the correct value. */
2664 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2665 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2666 h->root.type = bfd_link_hash_undefined;
2668 /* If this symbol is not being provided by the linker script, and it is
2669 currently defined by a dynamic object, but not by a regular object,
2670 then clear out any version information because the symbol will not be
2671 associated with the dynamic object any more. */
2673 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2674 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2675 h->verinfo.verdef = NULL;
2677 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2679 /* When possible, keep the original type of the symbol. */
2680 if (h->type == STT_NOTYPE)
2681 h->type = STT_OBJECT;
2683 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2684 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
2686 && h->dynindx == -1)
2688 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2691 /* If this is a weak defined symbol, and we know a corresponding
2692 real symbol from the same dynamic object, make sure the real
2693 symbol is also made into a dynamic symbol. */
2694 if (h->weakdef != NULL
2695 && h->weakdef->dynindx == -1)
2697 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2705 /* This structure is used to pass information to
2706 elf_link_assign_sym_version. */
2708 struct elf_assign_sym_version_info
2712 /* General link information. */
2713 struct bfd_link_info *info;
2715 struct bfd_elf_version_tree *verdefs;
2716 /* Whether we had a failure. */
2720 /* This structure is used to pass information to
2721 elf_link_find_version_dependencies. */
2723 struct elf_find_verdep_info
2727 /* General link information. */
2728 struct bfd_link_info *info;
2729 /* The number of dependencies. */
2731 /* Whether we had a failure. */
2735 /* Array used to determine the number of hash table buckets to use
2736 based on the number of symbols there are. If there are fewer than
2737 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2738 fewer than 37 we use 17 buckets, and so forth. We never use more
2739 than 32771 buckets. */
2741 static const size_t elf_buckets[] =
2743 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2747 /* Compute bucket count for hashing table. We do not use a static set
2748 of possible tables sizes anymore. Instead we determine for all
2749 possible reasonable sizes of the table the outcome (i.e., the
2750 number of collisions etc) and choose the best solution. The
2751 weighting functions are not too simple to allow the table to grow
2752 without bounds. Instead one of the weighting factors is the size.
2753 Therefore the result is always a good payoff between few collisions
2754 (= short chain lengths) and table size. */
2756 compute_bucket_count (info)
2757 struct bfd_link_info *info;
2759 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
2760 size_t best_size = 0;
2761 unsigned long int *hashcodes;
2762 unsigned long int *hashcodesp;
2763 unsigned long int i;
2766 /* Compute the hash values for all exported symbols. At the same
2767 time store the values in an array so that we could use them for
2770 amt *= sizeof (unsigned long int);
2771 hashcodes = (unsigned long int *) bfd_malloc (amt);
2772 if (hashcodes == NULL)
2774 hashcodesp = hashcodes;
2776 /* Put all hash values in HASHCODES. */
2777 elf_link_hash_traverse (elf_hash_table (info),
2778 elf_collect_hash_codes, &hashcodesp);
2780 /* We have a problem here. The following code to optimize the table
2781 size requires an integer type with more the 32 bits. If
2782 BFD_HOST_U_64_BIT is set we know about such a type. */
2783 #ifdef BFD_HOST_U_64_BIT
2784 if (info->optimize == true)
2786 unsigned long int nsyms = hashcodesp - hashcodes;
2789 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
2790 unsigned long int *counts ;
2792 /* Possible optimization parameters: if we have NSYMS symbols we say
2793 that the hashing table must at least have NSYMS/4 and at most
2795 minsize = nsyms / 4;
2798 best_size = maxsize = nsyms * 2;
2800 /* Create array where we count the collisions in. We must use bfd_malloc
2801 since the size could be large. */
2803 amt *= sizeof (unsigned long int);
2804 counts = (unsigned long int *) bfd_malloc (amt);
2811 /* Compute the "optimal" size for the hash table. The criteria is a
2812 minimal chain length. The minor criteria is (of course) the size
2814 for (i = minsize; i < maxsize; ++i)
2816 /* Walk through the array of hashcodes and count the collisions. */
2817 BFD_HOST_U_64_BIT max;
2818 unsigned long int j;
2819 unsigned long int fact;
2821 memset (counts, '\0', i * sizeof (unsigned long int));
2823 /* Determine how often each hash bucket is used. */
2824 for (j = 0; j < nsyms; ++j)
2825 ++counts[hashcodes[j] % i];
2827 /* For the weight function we need some information about the
2828 pagesize on the target. This is information need not be 100%
2829 accurate. Since this information is not available (so far) we
2830 define it here to a reasonable default value. If it is crucial
2831 to have a better value some day simply define this value. */
2832 # ifndef BFD_TARGET_PAGESIZE
2833 # define BFD_TARGET_PAGESIZE (4096)
2836 /* We in any case need 2 + NSYMS entries for the size values and
2838 max = (2 + nsyms) * (ARCH_SIZE / 8);
2841 /* Variant 1: optimize for short chains. We add the squares
2842 of all the chain lengths (which favous many small chain
2843 over a few long chains). */
2844 for (j = 0; j < i; ++j)
2845 max += counts[j] * counts[j];
2847 /* This adds penalties for the overall size of the table. */
2848 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2851 /* Variant 2: Optimize a lot more for small table. Here we
2852 also add squares of the size but we also add penalties for
2853 empty slots (the +1 term). */
2854 for (j = 0; j < i; ++j)
2855 max += (1 + counts[j]) * (1 + counts[j]);
2857 /* The overall size of the table is considered, but not as
2858 strong as in variant 1, where it is squared. */
2859 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2863 /* Compare with current best results. */
2864 if (max < best_chlen)
2874 #endif /* defined (BFD_HOST_U_64_BIT) */
2876 /* This is the fallback solution if no 64bit type is available or if we
2877 are not supposed to spend much time on optimizations. We select the
2878 bucket count using a fixed set of numbers. */
2879 for (i = 0; elf_buckets[i] != 0; i++)
2881 best_size = elf_buckets[i];
2882 if (dynsymcount < elf_buckets[i + 1])
2887 /* Free the arrays we needed. */
2893 /* Set up the sizes and contents of the ELF dynamic sections. This is
2894 called by the ELF linker emulation before_allocation routine. We
2895 must set the sizes of the sections before the linker sets the
2896 addresses of the various sections. */
2899 NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
2901 auxiliary_filters, info, sinterpptr,
2906 const char *filter_shlib;
2907 const char * const *auxiliary_filters;
2908 struct bfd_link_info *info;
2909 asection **sinterpptr;
2910 struct bfd_elf_version_tree *verdefs;
2912 bfd_size_type soname_indx;
2914 struct elf_backend_data *bed;
2915 struct elf_assign_sym_version_info asvinfo;
2919 soname_indx = (bfd_size_type) -1;
2921 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2924 if (! is_elf_hash_table (info))
2927 /* Any syms created from now on start with -1 in
2928 got.refcount/offset and plt.refcount/offset. */
2929 elf_hash_table (info)->init_refcount = -1;
2931 /* The backend may have to create some sections regardless of whether
2932 we're dynamic or not. */
2933 bed = get_elf_backend_data (output_bfd);
2934 if (bed->elf_backend_always_size_sections
2935 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
2938 dynobj = elf_hash_table (info)->dynobj;
2940 /* If there were no dynamic objects in the link, there is nothing to
2945 if (elf_hash_table (info)->dynamic_sections_created)
2947 struct elf_info_failed eif;
2948 struct elf_link_hash_entry *h;
2951 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
2952 BFD_ASSERT (*sinterpptr != NULL || info->shared);
2956 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2958 if (soname_indx == (bfd_size_type) -1
2959 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SONAME,
2966 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMBOLIC,
2969 info->flags |= DF_SYMBOLIC;
2976 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
2978 if (info->new_dtags)
2979 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
2980 if (indx == (bfd_size_type) -1
2981 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_RPATH, indx)
2983 && ! elf_add_dynamic_entry (info, (bfd_vma) DT_RUNPATH,
2988 if (filter_shlib != NULL)
2992 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2993 filter_shlib, true);
2994 if (indx == (bfd_size_type) -1
2995 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_FILTER, indx))
2999 if (auxiliary_filters != NULL)
3001 const char * const *p;
3003 for (p = auxiliary_filters; *p != NULL; p++)
3007 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3009 if (indx == (bfd_size_type) -1
3010 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_AUXILIARY,
3017 eif.verdefs = verdefs;
3020 /* If we are supposed to export all symbols into the dynamic symbol
3021 table (this is not the normal case), then do so. */
3022 if (info->export_dynamic)
3024 elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
3030 /* Attach all the symbols to their version information. */
3031 asvinfo.output_bfd = output_bfd;
3032 asvinfo.info = info;
3033 asvinfo.verdefs = verdefs;
3034 asvinfo.failed = false;
3036 elf_link_hash_traverse (elf_hash_table (info),
3037 elf_link_assign_sym_version,
3042 /* Find all symbols which were defined in a dynamic object and make
3043 the backend pick a reasonable value for them. */
3044 elf_link_hash_traverse (elf_hash_table (info),
3045 elf_adjust_dynamic_symbol,
3050 /* Add some entries to the .dynamic section. We fill in some of the
3051 values later, in elf_bfd_final_link, but we must add the entries
3052 now so that we know the final size of the .dynamic section. */
3054 /* If there are initialization and/or finalization functions to
3055 call then add the corresponding DT_INIT/DT_FINI entries. */
3056 h = (info->init_function
3057 ? elf_link_hash_lookup (elf_hash_table (info),
3058 info->init_function, false,
3062 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3063 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3065 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_INIT, (bfd_vma) 0))
3068 h = (info->fini_function
3069 ? elf_link_hash_lookup (elf_hash_table (info),
3070 info->fini_function, false,
3074 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3075 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3077 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FINI, (bfd_vma) 0))
3081 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
3082 /* If .dynstr is excluded from the link, we don't want any of
3083 these tags. Strictly, we should be checking each section
3084 individually; This quick check covers for the case where
3085 someone does a /DISCARD/ : { *(*) }. */
3086 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
3088 bfd_size_type strsize;
3090 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3091 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_HASH, (bfd_vma) 0)
3092 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRTAB, (bfd_vma) 0)
3093 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMTAB, (bfd_vma) 0)
3094 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRSZ, strsize)
3095 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMENT,
3096 (bfd_vma) sizeof (Elf_External_Sym)))
3101 /* The backend must work out the sizes of all the other dynamic
3103 if (bed->elf_backend_size_dynamic_sections
3104 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
3107 if (elf_hash_table (info)->dynamic_sections_created)
3109 bfd_size_type dynsymcount;
3111 size_t bucketcount = 0;
3112 size_t hash_entry_size;
3113 unsigned int dtagcount;
3115 /* Set up the version definition section. */
3116 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3117 BFD_ASSERT (s != NULL);
3119 /* We may have created additional version definitions if we are
3120 just linking a regular application. */
3121 verdefs = asvinfo.verdefs;
3123 if (verdefs == NULL)
3124 _bfd_strip_section_from_output (info, s);
3129 struct bfd_elf_version_tree *t;
3131 Elf_Internal_Verdef def;
3132 Elf_Internal_Verdaux defaux;
3137 /* Make space for the base version. */
3138 size += sizeof (Elf_External_Verdef);
3139 size += sizeof (Elf_External_Verdaux);
3142 for (t = verdefs; t != NULL; t = t->next)
3144 struct bfd_elf_version_deps *n;
3146 size += sizeof (Elf_External_Verdef);
3147 size += sizeof (Elf_External_Verdaux);
3150 for (n = t->deps; n != NULL; n = n->next)
3151 size += sizeof (Elf_External_Verdaux);
3154 s->_raw_size = size;
3155 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3156 if (s->contents == NULL && s->_raw_size != 0)
3159 /* Fill in the version definition section. */
3163 def.vd_version = VER_DEF_CURRENT;
3164 def.vd_flags = VER_FLG_BASE;
3167 def.vd_aux = sizeof (Elf_External_Verdef);
3168 def.vd_next = (sizeof (Elf_External_Verdef)
3169 + sizeof (Elf_External_Verdaux));
3171 if (soname_indx != (bfd_size_type) -1)
3173 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3175 def.vd_hash = bfd_elf_hash (soname);
3176 defaux.vda_name = soname_indx;
3183 name = basename (output_bfd->filename);
3184 def.vd_hash = bfd_elf_hash (name);
3185 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3187 if (indx == (bfd_size_type) -1)
3189 defaux.vda_name = indx;
3191 defaux.vda_next = 0;
3193 _bfd_elf_swap_verdef_out (output_bfd, &def,
3194 (Elf_External_Verdef *) p);
3195 p += sizeof (Elf_External_Verdef);
3196 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3197 (Elf_External_Verdaux *) p);
3198 p += sizeof (Elf_External_Verdaux);
3200 for (t = verdefs; t != NULL; t = t->next)
3203 struct bfd_elf_version_deps *n;
3204 struct elf_link_hash_entry *h;
3207 for (n = t->deps; n != NULL; n = n->next)
3210 /* Add a symbol representing this version. */
3212 if (! (_bfd_generic_link_add_one_symbol
3213 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
3214 (bfd_vma) 0, (const char *) NULL, false,
3215 get_elf_backend_data (dynobj)->collect,
3216 (struct bfd_link_hash_entry **) &h)))
3218 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
3219 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3220 h->type = STT_OBJECT;
3221 h->verinfo.vertree = t;
3223 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
3226 def.vd_version = VER_DEF_CURRENT;
3228 if (t->globals == NULL && t->locals == NULL && ! t->used)
3229 def.vd_flags |= VER_FLG_WEAK;
3230 def.vd_ndx = t->vernum + 1;
3231 def.vd_cnt = cdeps + 1;
3232 def.vd_hash = bfd_elf_hash (t->name);
3233 def.vd_aux = sizeof (Elf_External_Verdef);
3234 if (t->next != NULL)
3235 def.vd_next = (sizeof (Elf_External_Verdef)
3236 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
3240 _bfd_elf_swap_verdef_out (output_bfd, &def,
3241 (Elf_External_Verdef *) p);
3242 p += sizeof (Elf_External_Verdef);
3244 defaux.vda_name = h->dynstr_index;
3245 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3247 if (t->deps == NULL)
3248 defaux.vda_next = 0;
3250 defaux.vda_next = sizeof (Elf_External_Verdaux);
3251 t->name_indx = defaux.vda_name;
3253 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3254 (Elf_External_Verdaux *) p);
3255 p += sizeof (Elf_External_Verdaux);
3257 for (n = t->deps; n != NULL; n = n->next)
3259 if (n->version_needed == NULL)
3261 /* This can happen if there was an error in the
3263 defaux.vda_name = 0;
3267 defaux.vda_name = n->version_needed->name_indx;
3268 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3271 if (n->next == NULL)
3272 defaux.vda_next = 0;
3274 defaux.vda_next = sizeof (Elf_External_Verdaux);
3276 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3277 (Elf_External_Verdaux *) p);
3278 p += sizeof (Elf_External_Verdaux);
3282 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEF, (bfd_vma) 0)
3283 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEFNUM,
3287 elf_tdata (output_bfd)->cverdefs = cdefs;
3290 if (info->new_dtags && info->flags)
3292 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags))
3299 info->flags_1 &= ~ (DF_1_INITFIRST
3302 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS_1,
3307 /* Work out the size of the version reference section. */
3309 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3310 BFD_ASSERT (s != NULL);
3312 struct elf_find_verdep_info sinfo;
3314 sinfo.output_bfd = output_bfd;
3316 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
3317 if (sinfo.vers == 0)
3319 sinfo.failed = false;
3321 elf_link_hash_traverse (elf_hash_table (info),
3322 elf_link_find_version_dependencies,
3325 if (elf_tdata (output_bfd)->verref == NULL)
3326 _bfd_strip_section_from_output (info, s);
3329 Elf_Internal_Verneed *t;
3334 /* Build the version definition section. */
3337 for (t = elf_tdata (output_bfd)->verref;
3341 Elf_Internal_Vernaux *a;
3343 size += sizeof (Elf_External_Verneed);
3345 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3346 size += sizeof (Elf_External_Vernaux);
3349 s->_raw_size = size;
3350 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3351 if (s->contents == NULL)
3355 for (t = elf_tdata (output_bfd)->verref;
3360 Elf_Internal_Vernaux *a;
3364 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3367 t->vn_version = VER_NEED_CURRENT;
3369 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3370 elf_dt_name (t->vn_bfd) != NULL
3371 ? elf_dt_name (t->vn_bfd)
3372 : basename (t->vn_bfd->filename),
3374 if (indx == (bfd_size_type) -1)
3377 t->vn_aux = sizeof (Elf_External_Verneed);
3378 if (t->vn_nextref == NULL)
3381 t->vn_next = (sizeof (Elf_External_Verneed)
3382 + caux * sizeof (Elf_External_Vernaux));
3384 _bfd_elf_swap_verneed_out (output_bfd, t,
3385 (Elf_External_Verneed *) p);
3386 p += sizeof (Elf_External_Verneed);
3388 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3390 a->vna_hash = bfd_elf_hash (a->vna_nodename);
3391 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3392 a->vna_nodename, false);
3393 if (indx == (bfd_size_type) -1)
3396 if (a->vna_nextptr == NULL)
3399 a->vna_next = sizeof (Elf_External_Vernaux);
3401 _bfd_elf_swap_vernaux_out (output_bfd, a,
3402 (Elf_External_Vernaux *) p);
3403 p += sizeof (Elf_External_Vernaux);
3407 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEED,
3409 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEEDNUM,
3413 elf_tdata (output_bfd)->cverrefs = crefs;
3417 /* Assign dynsym indicies. In a shared library we generate a
3418 section symbol for each output section, which come first.
3419 Next come all of the back-end allocated local dynamic syms,
3420 followed by the rest of the global symbols. */
3422 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3424 /* Work out the size of the symbol version section. */
3425 s = bfd_get_section_by_name (dynobj, ".gnu.version");
3426 BFD_ASSERT (s != NULL);
3427 if (dynsymcount == 0
3428 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
3430 _bfd_strip_section_from_output (info, s);
3431 /* The DYNSYMCOUNT might have changed if we were going to
3432 output a dynamic symbol table entry for S. */
3433 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
3437 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
3438 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3439 if (s->contents == NULL)
3442 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERSYM, (bfd_vma) 0))
3446 /* Set the size of the .dynsym and .hash sections. We counted
3447 the number of dynamic symbols in elf_link_add_object_symbols.
3448 We will build the contents of .dynsym and .hash when we build
3449 the final symbol table, because until then we do not know the
3450 correct value to give the symbols. We built the .dynstr
3451 section as we went along in elf_link_add_object_symbols. */
3452 s = bfd_get_section_by_name (dynobj, ".dynsym");
3453 BFD_ASSERT (s != NULL);
3454 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
3455 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3456 if (s->contents == NULL && s->_raw_size != 0)
3459 if (dynsymcount != 0)
3461 Elf_Internal_Sym isym;
3463 /* The first entry in .dynsym is a dummy symbol. */
3470 elf_swap_symbol_out (output_bfd, &isym,
3471 (PTR) (Elf_External_Sym *) s->contents);
3474 /* Compute the size of the hashing table. As a side effect this
3475 computes the hash values for all the names we export. */
3476 bucketcount = compute_bucket_count (info);
3478 s = bfd_get_section_by_name (dynobj, ".hash");
3479 BFD_ASSERT (s != NULL);
3480 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
3481 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
3482 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3483 if (s->contents == NULL)
3485 memset (s->contents, 0, (size_t) s->_raw_size);
3487 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount,
3489 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) dynsymcount,
3490 s->contents + hash_entry_size);
3492 elf_hash_table (info)->bucketcount = bucketcount;
3494 s = bfd_get_section_by_name (dynobj, ".dynstr");
3495 BFD_ASSERT (s != NULL);
3497 elf_finalize_dynstr (output_bfd, info);
3499 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
3501 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
3502 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NULL, (bfd_vma) 0))
3509 /* This function is used to adjust offsets into .dynstr for
3510 dynamic symbols. This is called via elf_link_hash_traverse. */
3512 static boolean elf_adjust_dynstr_offsets
3513 PARAMS ((struct elf_link_hash_entry *, PTR));
3516 elf_adjust_dynstr_offsets (h, data)
3517 struct elf_link_hash_entry *h;
3520 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3522 if (h->dynindx != -1)
3523 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3527 /* Assign string offsets in .dynstr, update all structures referencing
3531 elf_finalize_dynstr (output_bfd, info)
3533 struct bfd_link_info *info;
3535 struct elf_link_local_dynamic_entry *entry;
3536 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
3537 bfd *dynobj = elf_hash_table (info)->dynobj;
3540 Elf_External_Dyn *dyncon, *dynconend;
3542 _bfd_elf_strtab_finalize (dynstr);
3543 size = _bfd_elf_strtab_size (dynstr);
3545 /* Update all .dynamic entries referencing .dynstr strings. */
3546 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3547 BFD_ASSERT (sdyn != NULL);
3549 dyncon = (Elf_External_Dyn *) sdyn->contents;
3550 dynconend = (Elf_External_Dyn *) (sdyn->contents +
3552 for (; dyncon < dynconend; dyncon++)
3554 Elf_Internal_Dyn dyn;
3556 elf_swap_dyn_in (dynobj, dyncon, & dyn);
3560 dyn.d_un.d_val = size;
3561 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3569 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3570 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3577 /* Now update local dynamic symbols. */
3578 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
3579 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3580 entry->isym.st_name);
3582 /* And the rest of dynamic symbols. */
3583 elf_link_hash_traverse (elf_hash_table (info),
3584 elf_adjust_dynstr_offsets, dynstr);
3586 /* Adjust version definitions. */
3587 if (elf_tdata (output_bfd)->cverdefs)
3592 Elf_Internal_Verdef def;
3593 Elf_Internal_Verdaux defaux;
3595 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3596 p = (bfd_byte *) s->contents;
3599 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3601 p += sizeof (Elf_External_Verdef);
3602 for (i = 0; i < def.vd_cnt; ++i)
3604 _bfd_elf_swap_verdaux_in (output_bfd,
3605 (Elf_External_Verdaux *) p, &defaux);
3606 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3608 _bfd_elf_swap_verdaux_out (output_bfd,
3609 &defaux, (Elf_External_Verdaux *) p);
3610 p += sizeof (Elf_External_Verdaux);
3613 while (def.vd_next);
3616 /* Adjust version references. */
3617 if (elf_tdata (output_bfd)->verref)
3622 Elf_Internal_Verneed need;
3623 Elf_Internal_Vernaux needaux;
3625 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3626 p = (bfd_byte *) s->contents;
3629 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3631 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3632 _bfd_elf_swap_verneed_out (output_bfd, &need,
3633 (Elf_External_Verneed *) p);
3634 p += sizeof (Elf_External_Verneed);
3635 for (i = 0; i < need.vn_cnt; ++i)
3637 _bfd_elf_swap_vernaux_in (output_bfd,
3638 (Elf_External_Vernaux *) p, &needaux);
3639 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3641 _bfd_elf_swap_vernaux_out (output_bfd,
3643 (Elf_External_Vernaux *) p);
3644 p += sizeof (Elf_External_Vernaux);
3647 while (need.vn_next);
3653 /* Fix up the flags for a symbol. This handles various cases which
3654 can only be fixed after all the input files are seen. This is
3655 currently called by both adjust_dynamic_symbol and
3656 assign_sym_version, which is unnecessary but perhaps more robust in
3657 the face of future changes. */
3660 elf_fix_symbol_flags (h, eif)
3661 struct elf_link_hash_entry *h;
3662 struct elf_info_failed *eif;
3664 /* If this symbol was mentioned in a non-ELF file, try to set
3665 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3666 permit a non-ELF file to correctly refer to a symbol defined in
3667 an ELF dynamic object. */
3668 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
3670 while (h->root.type == bfd_link_hash_indirect)
3671 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3673 if (h->root.type != bfd_link_hash_defined
3674 && h->root.type != bfd_link_hash_defweak)
3675 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3676 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3679 if (h->root.u.def.section->owner != NULL
3680 && (bfd_get_flavour (h->root.u.def.section->owner)
3681 == bfd_target_elf_flavour))
3682 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3683 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3685 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3688 if (h->dynindx == -1
3689 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3690 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3692 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3701 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3702 was first seen in a non-ELF file. Fortunately, if the symbol
3703 was first seen in an ELF file, we're probably OK unless the
3704 symbol was defined in a non-ELF file. Catch that case here.
3705 FIXME: We're still in trouble if the symbol was first seen in
3706 a dynamic object, and then later in a non-ELF regular object. */
3707 if ((h->root.type == bfd_link_hash_defined
3708 || h->root.type == bfd_link_hash_defweak)
3709 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3710 && (h->root.u.def.section->owner != NULL
3711 ? (bfd_get_flavour (h->root.u.def.section->owner)
3712 != bfd_target_elf_flavour)
3713 : (bfd_is_abs_section (h->root.u.def.section)
3714 && (h->elf_link_hash_flags
3715 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
3716 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3719 /* If this is a final link, and the symbol was defined as a common
3720 symbol in a regular object file, and there was no definition in
3721 any dynamic object, then the linker will have allocated space for
3722 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3723 flag will not have been set. */
3724 if (h->root.type == bfd_link_hash_defined
3725 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3726 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
3727 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3728 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3729 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3731 /* If -Bsymbolic was used (which means to bind references to global
3732 symbols to the definition within the shared object), and this
3733 symbol was defined in a regular object, then it actually doesn't
3734 need a PLT entry, and we can accomplish that by forcing it local.
3735 Likewise, if the symbol has hidden or internal visibility.
3736 FIXME: It might be that we also do not need a PLT for other
3737 non-hidden visibilities, but we would have to tell that to the
3738 backend specifically; we can't just clear PLT-related data here. */
3739 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
3740 && eif->info->shared
3741 && is_elf_hash_table (eif->info)
3742 && (eif->info->symbolic
3743 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3744 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3745 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3747 struct elf_backend_data *bed;
3749 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3750 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3751 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3753 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
3754 _bfd_elf_strtab_delref (elf_hash_table (eif->info)->dynstr,
3757 (*bed->elf_backend_hide_symbol) (eif->info, h);
3760 /* If this is a weak defined symbol in a dynamic object, and we know
3761 the real definition in the dynamic object, copy interesting flags
3762 over to the real definition. */
3763 if (h->weakdef != NULL)
3765 struct elf_link_hash_entry *weakdef;
3767 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3768 || h->root.type == bfd_link_hash_defweak);
3769 weakdef = h->weakdef;
3770 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
3771 || weakdef->root.type == bfd_link_hash_defweak);
3772 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
3774 /* If the real definition is defined by a regular object file,
3775 don't do anything special. See the longer description in
3776 elf_adjust_dynamic_symbol, below. */
3777 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3781 struct elf_backend_data *bed;
3783 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3784 (*bed->elf_backend_copy_indirect_symbol) (weakdef, h);
3791 /* Make the backend pick a good value for a dynamic symbol. This is
3792 called via elf_link_hash_traverse, and also calls itself
3796 elf_adjust_dynamic_symbol (h, data)
3797 struct elf_link_hash_entry *h;
3800 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3802 struct elf_backend_data *bed;
3804 /* Ignore indirect symbols. These are added by the versioning code. */
3805 if (h->root.type == bfd_link_hash_indirect)
3808 if (! is_elf_hash_table (eif->info))
3811 /* Fix the symbol flags. */
3812 if (! elf_fix_symbol_flags (h, eif))
3815 /* If this symbol does not require a PLT entry, and it is not
3816 defined by a dynamic object, or is not referenced by a regular
3817 object, ignore it. We do have to handle a weak defined symbol,
3818 even if no regular object refers to it, if we decided to add it
3819 to the dynamic symbol table. FIXME: Do we normally need to worry
3820 about symbols which are defined by one dynamic object and
3821 referenced by another one? */
3822 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
3823 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
3824 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3825 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
3826 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
3828 h->plt.offset = (bfd_vma) -1;
3832 /* If we've already adjusted this symbol, don't do it again. This
3833 can happen via a recursive call. */
3834 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
3837 /* Don't look at this symbol again. Note that we must set this
3838 after checking the above conditions, because we may look at a
3839 symbol once, decide not to do anything, and then get called
3840 recursively later after REF_REGULAR is set below. */
3841 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
3843 /* If this is a weak definition, and we know a real definition, and
3844 the real symbol is not itself defined by a regular object file,
3845 then get a good value for the real definition. We handle the
3846 real symbol first, for the convenience of the backend routine.
3848 Note that there is a confusing case here. If the real definition
3849 is defined by a regular object file, we don't get the real symbol
3850 from the dynamic object, but we do get the weak symbol. If the
3851 processor backend uses a COPY reloc, then if some routine in the
3852 dynamic object changes the real symbol, we will not see that
3853 change in the corresponding weak symbol. This is the way other
3854 ELF linkers work as well, and seems to be a result of the shared
3857 I will clarify this issue. Most SVR4 shared libraries define the
3858 variable _timezone and define timezone as a weak synonym. The
3859 tzset call changes _timezone. If you write
3860 extern int timezone;
3862 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3863 you might expect that, since timezone is a synonym for _timezone,
3864 the same number will print both times. However, if the processor
3865 backend uses a COPY reloc, then actually timezone will be copied
3866 into your process image, and, since you define _timezone
3867 yourself, _timezone will not. Thus timezone and _timezone will
3868 wind up at different memory locations. The tzset call will set
3869 _timezone, leaving timezone unchanged. */
3871 if (h->weakdef != NULL)
3873 /* If we get to this point, we know there is an implicit
3874 reference by a regular object file via the weak symbol H.
3875 FIXME: Is this really true? What if the traversal finds
3876 H->WEAKDEF before it finds H? */
3877 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
3879 if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif))
3883 /* If a symbol has no type and no size and does not require a PLT
3884 entry, then we are probably about to do the wrong thing here: we
3885 are probably going to create a COPY reloc for an empty object.
3886 This case can arise when a shared object is built with assembly
3887 code, and the assembly code fails to set the symbol type. */
3889 && h->type == STT_NOTYPE
3890 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
3891 (*_bfd_error_handler)
3892 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3893 h->root.root.string);
3895 dynobj = elf_hash_table (eif->info)->dynobj;
3896 bed = get_elf_backend_data (dynobj);
3897 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3906 /* This routine is used to export all defined symbols into the dynamic
3907 symbol table. It is called via elf_link_hash_traverse. */
3910 elf_export_symbol (h, data)
3911 struct elf_link_hash_entry *h;
3914 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3916 /* Ignore indirect symbols. These are added by the versioning code. */
3917 if (h->root.type == bfd_link_hash_indirect)
3920 if (h->dynindx == -1
3921 && (h->elf_link_hash_flags
3922 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
3924 struct bfd_elf_version_tree *t;
3925 struct bfd_elf_version_expr *d;
3927 for (t = eif->verdefs; t != NULL; t = t->next)
3929 if (t->globals != NULL)
3931 for (d = t->globals; d != NULL; d = d->next)
3933 if ((*d->match) (d, h->root.root.string))
3938 if (t->locals != NULL)
3940 for (d = t->locals ; d != NULL; d = d->next)
3942 if ((*d->match) (d, h->root.root.string))
3951 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3962 /* Look through the symbols which are defined in other shared
3963 libraries and referenced here. Update the list of version
3964 dependencies. This will be put into the .gnu.version_r section.
3965 This function is called via elf_link_hash_traverse. */
3968 elf_link_find_version_dependencies (h, data)
3969 struct elf_link_hash_entry *h;
3972 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
3973 Elf_Internal_Verneed *t;
3974 Elf_Internal_Vernaux *a;
3977 /* We only care about symbols defined in shared objects with version
3979 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3980 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
3982 || h->verinfo.verdef == NULL)
3985 /* See if we already know about this version. */
3986 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
3988 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
3991 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3992 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
3998 /* This is a new version. Add it to tree we are building. */
4003 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt);
4006 rinfo->failed = true;
4010 t->vn_bfd = h->verinfo.verdef->vd_bfd;
4011 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
4012 elf_tdata (rinfo->output_bfd)->verref = t;
4016 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt);
4018 /* Note that we are copying a string pointer here, and testing it
4019 above. If bfd_elf_string_from_elf_section is ever changed to
4020 discard the string data when low in memory, this will have to be
4022 a->vna_nodename = h->verinfo.verdef->vd_nodename;
4024 a->vna_flags = h->verinfo.verdef->vd_flags;
4025 a->vna_nextptr = t->vn_auxptr;
4027 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
4030 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
4037 /* Figure out appropriate versions for all the symbols. We may not
4038 have the version number script until we have read all of the input
4039 files, so until that point we don't know which symbols should be
4040 local. This function is called via elf_link_hash_traverse. */
4043 elf_link_assign_sym_version (h, data)
4044 struct elf_link_hash_entry *h;
4047 struct elf_assign_sym_version_info *sinfo;
4048 struct bfd_link_info *info;
4049 struct elf_backend_data *bed;
4050 struct elf_info_failed eif;
4054 sinfo = (struct elf_assign_sym_version_info *) data;
4057 /* Fix the symbol flags. */
4060 if (! elf_fix_symbol_flags (h, &eif))
4063 sinfo->failed = true;
4067 /* We only need version numbers for symbols defined in regular
4069 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4072 bed = get_elf_backend_data (sinfo->output_bfd);
4073 p = strchr (h->root.root.string, ELF_VER_CHR);
4074 if (p != NULL && h->verinfo.vertree == NULL)
4076 struct bfd_elf_version_tree *t;
4081 /* There are two consecutive ELF_VER_CHR characters if this is
4082 not a hidden symbol. */
4084 if (*p == ELF_VER_CHR)
4090 /* If there is no version string, we can just return out. */
4094 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4098 /* Look for the version. If we find it, it is no longer weak. */
4099 for (t = sinfo->verdefs; t != NULL; t = t->next)
4101 if (strcmp (t->name, p) == 0)
4105 struct bfd_elf_version_expr *d;
4107 len = p - h->root.root.string;
4108 alc = bfd_alloc (sinfo->output_bfd, (bfd_size_type) len);
4111 strncpy (alc, h->root.root.string, len - 1);
4112 alc[len - 1] = '\0';
4113 if (alc[len - 2] == ELF_VER_CHR)
4114 alc[len - 2] = '\0';
4116 h->verinfo.vertree = t;
4120 if (t->globals != NULL)
4122 for (d = t->globals; d != NULL; d = d->next)
4123 if ((*d->match) (d, alc))
4127 /* See if there is anything to force this symbol to
4129 if (d == NULL && t->locals != NULL)
4131 for (d = t->locals; d != NULL; d = d->next)
4133 if ((*d->match) (d, alc))
4135 if (h->dynindx != -1
4137 && ! info->export_dynamic)
4139 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
4140 (*bed->elf_backend_hide_symbol) (info, h);
4141 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4150 bfd_release (sinfo->output_bfd, alc);
4155 /* If we are building an application, we need to create a
4156 version node for this version. */
4157 if (t == NULL && ! info->shared)
4159 struct bfd_elf_version_tree **pp;
4162 /* If we aren't going to export this symbol, we don't need
4163 to worry about it. */
4164 if (h->dynindx == -1)
4168 t = ((struct bfd_elf_version_tree *)
4169 bfd_alloc (sinfo->output_bfd, amt));
4172 sinfo->failed = true;
4181 t->name_indx = (unsigned int) -1;
4185 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
4187 t->vernum = version_index;
4191 h->verinfo.vertree = t;
4195 /* We could not find the version for a symbol when
4196 generating a shared archive. Return an error. */
4197 (*_bfd_error_handler)
4198 (_("%s: undefined versioned symbol name %s"),
4199 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
4200 bfd_set_error (bfd_error_bad_value);
4201 sinfo->failed = true;
4206 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4209 /* If we don't have a version for this symbol, see if we can find
4211 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
4213 struct bfd_elf_version_tree *t;
4214 struct bfd_elf_version_tree *deflt;
4215 struct bfd_elf_version_expr *d;
4217 /* See if can find what version this symbol is in. If the
4218 symbol is supposed to be local, then don't actually register
4221 for (t = sinfo->verdefs; t != NULL; t = t->next)
4223 if (t->globals != NULL)
4225 for (d = t->globals; d != NULL; d = d->next)
4227 if ((*d->match) (d, h->root.root.string))
4229 h->verinfo.vertree = t;
4238 if (t->locals != NULL)
4240 for (d = t->locals; d != NULL; d = d->next)
4242 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
4244 else if ((*d->match) (d, h->root.root.string))
4246 h->verinfo.vertree = t;
4247 if (h->dynindx != -1
4249 && ! info->export_dynamic)
4251 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
4252 (*bed->elf_backend_hide_symbol) (info, h);
4253 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4265 if (deflt != NULL && h->verinfo.vertree == NULL)
4267 h->verinfo.vertree = deflt;
4268 if (h->dynindx != -1
4270 && ! info->export_dynamic)
4272 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
4273 (*bed->elf_backend_hide_symbol) (info, h);
4274 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4283 /* Final phase of ELF linker. */
4285 /* A structure we use to avoid passing large numbers of arguments. */
4287 struct elf_final_link_info
4289 /* General link information. */
4290 struct bfd_link_info *info;
4293 /* Symbol string table. */
4294 struct bfd_strtab_hash *symstrtab;
4295 /* .dynsym section. */
4296 asection *dynsym_sec;
4297 /* .hash section. */
4299 /* symbol version section (.gnu.version). */
4300 asection *symver_sec;
4301 /* Buffer large enough to hold contents of any section. */
4303 /* Buffer large enough to hold external relocs of any section. */
4304 PTR external_relocs;
4305 /* Buffer large enough to hold internal relocs of any section. */
4306 Elf_Internal_Rela *internal_relocs;
4307 /* Buffer large enough to hold external local symbols of any input
4309 Elf_External_Sym *external_syms;
4310 /* Buffer large enough to hold internal local symbols of any input
4312 Elf_Internal_Sym *internal_syms;
4313 /* Array large enough to hold a symbol index for each local symbol
4314 of any input BFD. */
4316 /* Array large enough to hold a section pointer for each local
4317 symbol of any input BFD. */
4318 asection **sections;
4319 /* Buffer to hold swapped out symbols. */
4320 Elf_External_Sym *symbuf;
4321 /* Number of swapped out symbols in buffer. */
4322 size_t symbuf_count;
4323 /* Number of symbols which fit in symbuf. */
4327 static boolean elf_link_output_sym
4328 PARAMS ((struct elf_final_link_info *, const char *,
4329 Elf_Internal_Sym *, asection *));
4330 static boolean elf_link_flush_output_syms
4331 PARAMS ((struct elf_final_link_info *));
4332 static boolean elf_link_output_extsym
4333 PARAMS ((struct elf_link_hash_entry *, PTR));
4334 static boolean elf_link_sec_merge_syms
4335 PARAMS ((struct elf_link_hash_entry *, PTR));
4336 static boolean elf_link_input_bfd
4337 PARAMS ((struct elf_final_link_info *, bfd *));
4338 static boolean elf_reloc_link_order
4339 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4340 struct bfd_link_order *));
4342 /* This struct is used to pass information to elf_link_output_extsym. */
4344 struct elf_outext_info
4348 struct elf_final_link_info *finfo;
4351 /* Compute the size of, and allocate space for, REL_HDR which is the
4352 section header for a section containing relocations for O. */
4355 elf_link_size_reloc_section (abfd, rel_hdr, o)
4357 Elf_Internal_Shdr *rel_hdr;
4360 bfd_size_type reloc_count;
4361 bfd_size_type num_rel_hashes;
4363 /* Figure out how many relocations there will be. */
4364 if (rel_hdr == &elf_section_data (o)->rel_hdr)
4365 reloc_count = elf_section_data (o)->rel_count;
4367 reloc_count = elf_section_data (o)->rel_count2;
4369 num_rel_hashes = o->reloc_count;
4370 if (num_rel_hashes < reloc_count)
4371 num_rel_hashes = reloc_count;
4373 /* That allows us to calculate the size of the section. */
4374 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
4376 /* The contents field must last into write_object_contents, so we
4377 allocate it with bfd_alloc rather than malloc. Also since we
4378 cannot be sure that the contents will actually be filled in,
4379 we zero the allocated space. */
4380 rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size);
4381 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
4384 /* We only allocate one set of hash entries, so we only do it the
4385 first time we are called. */
4386 if (elf_section_data (o)->rel_hashes == NULL
4389 struct elf_link_hash_entry **p;
4391 p = ((struct elf_link_hash_entry **)
4392 bfd_zmalloc (num_rel_hashes
4393 * sizeof (struct elf_link_hash_entry *)));
4397 elf_section_data (o)->rel_hashes = p;
4403 /* When performing a relocateable link, the input relocations are
4404 preserved. But, if they reference global symbols, the indices
4405 referenced must be updated. Update all the relocations in
4406 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4409 elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
4411 Elf_Internal_Shdr *rel_hdr;
4413 struct elf_link_hash_entry **rel_hash;
4416 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4417 Elf_Internal_Rel *irel;
4418 Elf_Internal_Rela *irela;
4419 bfd_size_type amt = sizeof (Elf_Internal_Rel) * bed->s->int_rels_per_ext_rel;
4421 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
4424 (*_bfd_error_handler) (_("Error: out of memory"));
4428 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
4429 irela = (Elf_Internal_Rela *) bfd_zmalloc (amt);
4432 (*_bfd_error_handler) (_("Error: out of memory"));
4436 for (i = 0; i < count; i++, rel_hash++)
4438 if (*rel_hash == NULL)
4441 BFD_ASSERT ((*rel_hash)->indx >= 0);
4443 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
4445 Elf_External_Rel *erel;
4448 erel = (Elf_External_Rel *) rel_hdr->contents + i;
4449 if (bed->s->swap_reloc_in)
4450 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
4452 elf_swap_reloc_in (abfd, erel, irel);
4454 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4455 irel[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4456 ELF_R_TYPE (irel[j].r_info));
4458 if (bed->s->swap_reloc_out)
4459 (*bed->s->swap_reloc_out) (abfd, irel, (bfd_byte *) erel);
4461 elf_swap_reloc_out (abfd, irel, erel);
4465 Elf_External_Rela *erela;
4468 BFD_ASSERT (rel_hdr->sh_entsize
4469 == sizeof (Elf_External_Rela));
4471 erela = (Elf_External_Rela *) rel_hdr->contents + i;
4472 if (bed->s->swap_reloca_in)
4473 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
4475 elf_swap_reloca_in (abfd, erela, irela);
4477 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4478 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4479 ELF_R_TYPE (irela[j].r_info));
4481 if (bed->s->swap_reloca_out)
4482 (*bed->s->swap_reloca_out) (abfd, irela, (bfd_byte *) erela);
4484 elf_swap_reloca_out (abfd, irela, erela);
4492 struct elf_link_sort_rela {
4494 enum elf_reloc_type_class type;
4496 Elf_Internal_Rel rel;
4497 Elf_Internal_Rela rela;
4502 elf_link_sort_cmp1 (A, B)
4506 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4507 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4508 int relativea, relativeb;
4510 relativea = a->type == reloc_class_relative;
4511 relativeb = b->type == reloc_class_relative;
4513 if (relativea < relativeb)
4515 if (relativea > relativeb)
4517 if (ELF_R_SYM (a->u.rel.r_info) < ELF_R_SYM (b->u.rel.r_info))
4519 if (ELF_R_SYM (a->u.rel.r_info) > ELF_R_SYM (b->u.rel.r_info))
4521 if (a->u.rel.r_offset < b->u.rel.r_offset)
4523 if (a->u.rel.r_offset > b->u.rel.r_offset)
4529 elf_link_sort_cmp2 (A, B)
4533 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4534 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
4537 if (a->offset < b->offset)
4539 if (a->offset > b->offset)
4541 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
4542 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
4547 if (a->u.rel.r_offset < b->u.rel.r_offset)
4549 if (a->u.rel.r_offset > b->u.rel.r_offset)
4555 elf_link_sort_relocs (abfd, info, psec)
4557 struct bfd_link_info *info;
4560 bfd *dynobj = elf_hash_table (info)->dynobj;
4561 asection *reldyn, *o;
4562 boolean rel = false;
4563 bfd_size_type count, size;
4565 struct elf_link_sort_rela *rela;
4566 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4568 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
4569 if (reldyn == NULL || reldyn->_raw_size == 0)
4571 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
4572 if (reldyn == NULL || reldyn->_raw_size == 0)
4575 count = reldyn->_raw_size / sizeof (Elf_External_Rel);
4578 count = reldyn->_raw_size / sizeof (Elf_External_Rela);
4581 for (o = dynobj->sections; o != NULL; o = o->next)
4582 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4583 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4584 && o->output_section == reldyn)
4585 size += o->_raw_size;
4587 if (size != reldyn->_raw_size)
4590 rela = (struct elf_link_sort_rela *) bfd_zmalloc (sizeof (*rela) * count);
4593 (*info->callbacks->warning)
4594 (info, _("Not enough memory to sort relocations"), 0, abfd, 0,
4599 for (o = dynobj->sections; o != NULL; o = o->next)
4600 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4601 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4602 && o->output_section == reldyn)
4606 Elf_External_Rel *erel, *erelend;
4607 struct elf_link_sort_rela *s;
4609 erel = (Elf_External_Rel *) o->contents;
4610 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4611 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4612 for (; erel < erelend; erel++, s++)
4614 if (bed->s->swap_reloc_in)
4615 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, &s->u.rel);
4617 elf_swap_reloc_in (abfd, erel, &s->u.rel);
4619 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
4624 Elf_External_Rela *erela, *erelaend;
4625 struct elf_link_sort_rela *s;
4627 erela = (Elf_External_Rela *) o->contents;
4628 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4629 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4630 for (; erela < erelaend; erela++, s++)
4632 if (bed->s->swap_reloca_in)
4633 (*bed->s->swap_reloca_in) (dynobj, (bfd_byte *) erela,
4636 elf_swap_reloca_in (dynobj, erela, &s->u.rela);
4638 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
4643 qsort (rela, (size_t) count, sizeof (*rela), elf_link_sort_cmp1);
4644 for (ret = 0; ret < count && rela[ret].type == reloc_class_relative; ret++)
4646 for (i = ret, j = ret; i < count; i++)
4648 if (ELF_R_SYM (rela[i].u.rel.r_info) != ELF_R_SYM (rela[j].u.rel.r_info))
4650 rela[i].offset = rela[j].u.rel.r_offset;
4652 qsort (rela + ret, (size_t) count - ret, sizeof (*rela), elf_link_sort_cmp2);
4654 for (o = dynobj->sections; o != NULL; o = o->next)
4655 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4656 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4657 && o->output_section == reldyn)
4661 Elf_External_Rel *erel, *erelend;
4662 struct elf_link_sort_rela *s;
4664 erel = (Elf_External_Rel *) o->contents;
4665 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
4666 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4667 for (; erel < erelend; erel++, s++)
4669 if (bed->s->swap_reloc_out)
4670 (*bed->s->swap_reloc_out) (abfd, &s->u.rel,
4673 elf_swap_reloc_out (abfd, &s->u.rel, erel);
4678 Elf_External_Rela *erela, *erelaend;
4679 struct elf_link_sort_rela *s;
4681 erela = (Elf_External_Rela *) o->contents;
4682 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
4683 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4684 for (; erela < erelaend; erela++, s++)
4686 if (bed->s->swap_reloca_out)
4687 (*bed->s->swap_reloca_out) (dynobj, &s->u.rela,
4688 (bfd_byte *) erela);
4690 elf_swap_reloca_out (dynobj, &s->u.rela, erela);
4700 /* Do the final step of an ELF link. */
4703 elf_bfd_final_link (abfd, info)
4705 struct bfd_link_info *info;
4708 boolean emit_relocs;
4710 struct elf_final_link_info finfo;
4711 register asection *o;
4712 register struct bfd_link_order *p;
4714 bfd_size_type max_contents_size;
4715 bfd_size_type max_external_reloc_size;
4716 bfd_size_type max_internal_reloc_count;
4717 bfd_size_type max_sym_count;
4719 Elf_Internal_Sym elfsym;
4721 Elf_Internal_Shdr *symtab_hdr;
4722 Elf_Internal_Shdr *symstrtab_hdr;
4723 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4724 struct elf_outext_info eoinfo;
4726 size_t relativecount = 0;
4727 asection *reldyn = 0;
4730 if (! is_elf_hash_table (info))
4734 abfd->flags |= DYNAMIC;
4736 dynamic = elf_hash_table (info)->dynamic_sections_created;
4737 dynobj = elf_hash_table (info)->dynobj;
4739 emit_relocs = (info->relocateable
4740 || info->emitrelocations
4741 || bed->elf_backend_emit_relocs);
4744 finfo.output_bfd = abfd;
4745 finfo.symstrtab = elf_stringtab_init ();
4746 if (finfo.symstrtab == NULL)
4751 finfo.dynsym_sec = NULL;
4752 finfo.hash_sec = NULL;
4753 finfo.symver_sec = NULL;
4757 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
4758 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
4759 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
4760 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
4761 /* Note that it is OK if symver_sec is NULL. */
4764 finfo.contents = NULL;
4765 finfo.external_relocs = NULL;
4766 finfo.internal_relocs = NULL;
4767 finfo.external_syms = NULL;
4768 finfo.internal_syms = NULL;
4769 finfo.indices = NULL;
4770 finfo.sections = NULL;
4771 finfo.symbuf = NULL;
4772 finfo.symbuf_count = 0;
4774 /* Count up the number of relocations we will output for each output
4775 section, so that we know the sizes of the reloc sections. We
4776 also figure out some maximum sizes. */
4777 max_contents_size = 0;
4778 max_external_reloc_size = 0;
4779 max_internal_reloc_count = 0;
4782 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4786 for (p = o->link_order_head; p != NULL; p = p->next)
4788 if (p->type == bfd_section_reloc_link_order
4789 || p->type == bfd_symbol_reloc_link_order)
4791 else if (p->type == bfd_indirect_link_order)
4795 sec = p->u.indirect.section;
4797 /* Mark all sections which are to be included in the
4798 link. This will normally be every section. We need
4799 to do this so that we can identify any sections which
4800 the linker has decided to not include. */
4801 sec->linker_mark = true;
4803 if (sec->flags & SEC_MERGE)
4806 if (info->relocateable || info->emitrelocations)
4807 o->reloc_count += sec->reloc_count;
4808 else if (bed->elf_backend_count_relocs)
4810 Elf_Internal_Rela * relocs;
4812 relocs = (NAME(_bfd_elf,link_read_relocs)
4813 (abfd, sec, (PTR) NULL,
4814 (Elf_Internal_Rela *) NULL, info->keep_memory));
4816 o->reloc_count += (*bed->elf_backend_count_relocs)
4819 if (!info->keep_memory)
4823 if (sec->_raw_size > max_contents_size)
4824 max_contents_size = sec->_raw_size;
4825 if (sec->_cooked_size > max_contents_size)
4826 max_contents_size = sec->_cooked_size;
4828 /* We are interested in just local symbols, not all
4830 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
4831 && (sec->owner->flags & DYNAMIC) == 0)
4835 if (elf_bad_symtab (sec->owner))
4836 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
4837 / sizeof (Elf_External_Sym));
4839 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
4841 if (sym_count > max_sym_count)
4842 max_sym_count = sym_count;
4844 if ((sec->flags & SEC_RELOC) != 0)
4848 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
4849 if (ext_size > max_external_reloc_size)
4850 max_external_reloc_size = ext_size;
4851 if (sec->reloc_count > max_internal_reloc_count)
4852 max_internal_reloc_count = sec->reloc_count;
4858 if (o->reloc_count > 0)
4859 o->flags |= SEC_RELOC;
4862 /* Explicitly clear the SEC_RELOC flag. The linker tends to
4863 set it (this is probably a bug) and if it is set
4864 assign_section_numbers will create a reloc section. */
4865 o->flags &=~ SEC_RELOC;
4868 /* If the SEC_ALLOC flag is not set, force the section VMA to
4869 zero. This is done in elf_fake_sections as well, but forcing
4870 the VMA to 0 here will ensure that relocs against these
4871 sections are handled correctly. */
4872 if ((o->flags & SEC_ALLOC) == 0
4873 && ! o->user_set_vma)
4877 if (! info->relocateable && merged)
4878 elf_link_hash_traverse (elf_hash_table (info),
4879 elf_link_sec_merge_syms, (PTR) abfd);
4881 /* Figure out the file positions for everything but the symbol table
4882 and the relocs. We set symcount to force assign_section_numbers
4883 to create a symbol table. */
4884 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
4885 BFD_ASSERT (! abfd->output_has_begun);
4886 if (! _bfd_elf_compute_section_file_positions (abfd, info))
4889 /* Figure out how many relocations we will have in each section.
4890 Just using RELOC_COUNT isn't good enough since that doesn't
4891 maintain a separate value for REL vs. RELA relocations. */
4893 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
4894 for (o = sub->sections; o != NULL; o = o->next)
4896 asection *output_section;
4898 if (! o->linker_mark)
4900 /* This section was omitted from the link. */
4904 output_section = o->output_section;
4906 if (output_section != NULL
4907 && (o->flags & SEC_RELOC) != 0)
4909 struct bfd_elf_section_data *esdi
4910 = elf_section_data (o);
4911 struct bfd_elf_section_data *esdo
4912 = elf_section_data (output_section);
4913 unsigned int *rel_count;
4914 unsigned int *rel_count2;
4916 /* We must be careful to add the relocation froms the
4917 input section to the right output count. */
4918 if (esdi->rel_hdr.sh_entsize == esdo->rel_hdr.sh_entsize)
4920 rel_count = &esdo->rel_count;
4921 rel_count2 = &esdo->rel_count2;
4925 rel_count = &esdo->rel_count2;
4926 rel_count2 = &esdo->rel_count;
4929 *rel_count += NUM_SHDR_ENTRIES (& esdi->rel_hdr);
4931 *rel_count2 += NUM_SHDR_ENTRIES (esdi->rel_hdr2);
4932 output_section->flags |= SEC_RELOC;
4936 /* That created the reloc sections. Set their sizes, and assign
4937 them file positions, and allocate some buffers. */
4938 for (o = abfd->sections; o != NULL; o = o->next)
4940 if ((o->flags & SEC_RELOC) != 0)
4942 if (!elf_link_size_reloc_section (abfd,
4943 &elf_section_data (o)->rel_hdr,
4947 if (elf_section_data (o)->rel_hdr2
4948 && !elf_link_size_reloc_section (abfd,
4949 elf_section_data (o)->rel_hdr2,
4954 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
4955 to count upwards while actually outputting the relocations. */
4956 elf_section_data (o)->rel_count = 0;
4957 elf_section_data (o)->rel_count2 = 0;
4960 _bfd_elf_assign_file_positions_for_relocs (abfd);
4962 /* We have now assigned file positions for all the sections except
4963 .symtab and .strtab. We start the .symtab section at the current
4964 file position, and write directly to it. We build the .strtab
4965 section in memory. */
4966 bfd_get_symcount (abfd) = 0;
4967 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4968 /* sh_name is set in prep_headers. */
4969 symtab_hdr->sh_type = SHT_SYMTAB;
4970 symtab_hdr->sh_flags = 0;
4971 symtab_hdr->sh_addr = 0;
4972 symtab_hdr->sh_size = 0;
4973 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
4974 /* sh_link is set in assign_section_numbers. */
4975 /* sh_info is set below. */
4976 /* sh_offset is set just below. */
4977 symtab_hdr->sh_addralign = bed->s->file_align;
4979 off = elf_tdata (abfd)->next_file_pos;
4980 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
4982 /* Note that at this point elf_tdata (abfd)->next_file_pos is
4983 incorrect. We do not yet know the size of the .symtab section.
4984 We correct next_file_pos below, after we do know the size. */
4986 /* Allocate a buffer to hold swapped out symbols. This is to avoid
4987 continuously seeking to the right position in the file. */
4988 if (! info->keep_memory || max_sym_count < 20)
4989 finfo.symbuf_size = 20;
4991 finfo.symbuf_size = max_sym_count;
4992 amt = finfo.symbuf_size;
4993 amt *= sizeof (Elf_External_Sym);
4994 finfo.symbuf = (Elf_External_Sym *) bfd_malloc (amt);
4995 if (finfo.symbuf == NULL)
4998 /* Start writing out the symbol table. The first symbol is always a
5000 if (info->strip != strip_all
5003 elfsym.st_value = 0;
5006 elfsym.st_other = 0;
5007 elfsym.st_shndx = SHN_UNDEF;
5008 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5009 &elfsym, bfd_und_section_ptr))
5014 /* Some standard ELF linkers do this, but we don't because it causes
5015 bootstrap comparison failures. */
5016 /* Output a file symbol for the output file as the second symbol.
5017 We output this even if we are discarding local symbols, although
5018 I'm not sure if this is correct. */
5019 elfsym.st_value = 0;
5021 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5022 elfsym.st_other = 0;
5023 elfsym.st_shndx = SHN_ABS;
5024 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
5025 &elfsym, bfd_abs_section_ptr))
5029 /* Output a symbol for each section. We output these even if we are
5030 discarding local symbols, since they are used for relocs. These
5031 symbols have no names. We store the index of each one in the
5032 index field of the section, so that we can find it again when
5033 outputting relocs. */
5034 if (info->strip != strip_all
5038 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5039 elfsym.st_other = 0;
5040 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
5042 o = section_from_elf_index (abfd, i);
5044 o->target_index = bfd_get_symcount (abfd);
5045 elfsym.st_shndx = i;
5046 if (info->relocateable || o == NULL)
5047 elfsym.st_value = 0;
5049 elfsym.st_value = o->vma;
5050 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5056 /* Allocate some memory to hold information read in from the input
5058 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
5059 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
5060 finfo.internal_relocs = ((Elf_Internal_Rela *)
5061 bfd_malloc (max_internal_reloc_count
5062 * sizeof (Elf_Internal_Rela)
5063 * bed->s->int_rels_per_ext_rel));
5064 finfo.external_syms = ((Elf_External_Sym *)
5065 bfd_malloc (max_sym_count
5066 * sizeof (Elf_External_Sym)));
5067 finfo.internal_syms = ((Elf_Internal_Sym *)
5068 bfd_malloc (max_sym_count
5069 * sizeof (Elf_Internal_Sym)));
5070 finfo.indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
5071 finfo.sections = ((asection **)
5072 bfd_malloc (max_sym_count * sizeof (asection *)));
5073 if ((finfo.contents == NULL && max_contents_size != 0)
5074 || (finfo.external_relocs == NULL && max_external_reloc_size != 0)
5075 || (finfo.internal_relocs == NULL && max_internal_reloc_count != 0)
5076 || (finfo.external_syms == NULL && max_sym_count != 0)
5077 || (finfo.internal_syms == NULL && max_sym_count != 0)
5078 || (finfo.indices == NULL && max_sym_count != 0)
5079 || (finfo.sections == NULL && max_sym_count != 0))
5082 /* Since ELF permits relocations to be against local symbols, we
5083 must have the local symbols available when we do the relocations.
5084 Since we would rather only read the local symbols once, and we
5085 would rather not keep them in memory, we handle all the
5086 relocations for a single input file at the same time.
5088 Unfortunately, there is no way to know the total number of local
5089 symbols until we have seen all of them, and the local symbol
5090 indices precede the global symbol indices. This means that when
5091 we are generating relocateable output, and we see a reloc against
5092 a global symbol, we can not know the symbol index until we have
5093 finished examining all the local symbols to see which ones we are
5094 going to output. To deal with this, we keep the relocations in
5095 memory, and don't output them until the end of the link. This is
5096 an unfortunate waste of memory, but I don't see a good way around
5097 it. Fortunately, it only happens when performing a relocateable
5098 link, which is not the common case. FIXME: If keep_memory is set
5099 we could write the relocs out and then read them again; I don't
5100 know how bad the memory loss will be. */
5102 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5103 sub->output_has_begun = false;
5104 for (o = abfd->sections; o != NULL; o = o->next)
5106 for (p = o->link_order_head; p != NULL; p = p->next)
5108 if (p->type == bfd_indirect_link_order
5109 && (bfd_get_flavour (p->u.indirect.section->owner)
5110 == bfd_target_elf_flavour))
5112 sub = p->u.indirect.section->owner;
5113 if (! sub->output_has_begun)
5115 if (! elf_link_input_bfd (&finfo, sub))
5117 sub->output_has_begun = true;
5120 else if (p->type == bfd_section_reloc_link_order
5121 || p->type == bfd_symbol_reloc_link_order)
5123 if (! elf_reloc_link_order (abfd, info, o, p))
5128 if (! _bfd_default_link_order (abfd, info, o, p))
5134 /* That wrote out all the local symbols. Finish up the symbol table
5135 with the global symbols. Even if we want to strip everything we
5136 can, we still need to deal with those global symbols that got
5137 converted to local in a version script. */
5141 /* Output any global symbols that got converted to local in a
5142 version script. We do this in a separate step since ELF
5143 requires all local symbols to appear prior to any global
5144 symbols. FIXME: We should only do this if some global
5145 symbols were, in fact, converted to become local. FIXME:
5146 Will this work correctly with the Irix 5 linker? */
5147 eoinfo.failed = false;
5148 eoinfo.finfo = &finfo;
5149 eoinfo.localsyms = true;
5150 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5156 /* The sh_info field records the index of the first non local symbol. */
5157 symtab_hdr->sh_info = bfd_get_symcount (abfd);
5160 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
5162 Elf_Internal_Sym sym;
5163 Elf_External_Sym *dynsym =
5164 (Elf_External_Sym *) finfo.dynsym_sec->contents;
5165 long last_local = 0;
5167 /* Write out the section symbols for the output sections. */
5174 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5177 for (s = abfd->sections; s != NULL; s = s->next)
5180 indx = elf_section_data (s)->this_idx;
5181 BFD_ASSERT (indx > 0);
5182 sym.st_shndx = indx;
5183 sym.st_value = s->vma;
5185 elf_swap_symbol_out (abfd, &sym,
5186 dynsym + elf_section_data (s)->dynindx);
5189 last_local = bfd_count_sections (abfd);
5192 /* Write out the local dynsyms. */
5193 if (elf_hash_table (info)->dynlocal)
5195 struct elf_link_local_dynamic_entry *e;
5196 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
5200 sym.st_size = e->isym.st_size;
5201 sym.st_other = e->isym.st_other;
5203 /* Copy the internal symbol as is.
5204 Note that we saved a word of storage and overwrote
5205 the original st_name with the dynstr_index. */
5208 if (e->isym.st_shndx > 0 && e->isym.st_shndx < SHN_LORESERVE)
5210 s = bfd_section_from_elf_index (e->input_bfd,
5214 elf_section_data (s->output_section)->this_idx;
5215 sym.st_value = (s->output_section->vma
5217 + e->isym.st_value);
5220 if (last_local < e->dynindx)
5221 last_local = e->dynindx;
5223 elf_swap_symbol_out (abfd, &sym, dynsym + e->dynindx);
5227 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
5231 /* We get the global symbols from the hash table. */
5232 eoinfo.failed = false;
5233 eoinfo.localsyms = false;
5234 eoinfo.finfo = &finfo;
5235 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5240 /* If backend needs to output some symbols not present in the hash
5241 table, do it now. */
5242 if (bed->elf_backend_output_arch_syms)
5244 typedef boolean (*out_sym_func) PARAMS ((PTR, const char *,
5248 if (! ((*bed->elf_backend_output_arch_syms)
5249 (abfd, info, (PTR) &finfo, (out_sym_func) elf_link_output_sym)))
5253 /* Flush all symbols to the file. */
5254 if (! elf_link_flush_output_syms (&finfo))
5257 /* Now we know the size of the symtab section. */
5258 off += symtab_hdr->sh_size;
5260 /* Finish up and write out the symbol string table (.strtab)
5262 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5263 /* sh_name was set in prep_headers. */
5264 symstrtab_hdr->sh_type = SHT_STRTAB;
5265 symstrtab_hdr->sh_flags = 0;
5266 symstrtab_hdr->sh_addr = 0;
5267 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
5268 symstrtab_hdr->sh_entsize = 0;
5269 symstrtab_hdr->sh_link = 0;
5270 symstrtab_hdr->sh_info = 0;
5271 /* sh_offset is set just below. */
5272 symstrtab_hdr->sh_addralign = 1;
5274 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true);
5275 elf_tdata (abfd)->next_file_pos = off;
5277 if (bfd_get_symcount (abfd) > 0)
5279 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
5280 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
5284 /* Adjust the relocs to have the correct symbol indices. */
5285 for (o = abfd->sections; o != NULL; o = o->next)
5287 if ((o->flags & SEC_RELOC) == 0)
5290 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
5291 elf_section_data (o)->rel_count,
5292 elf_section_data (o)->rel_hashes);
5293 if (elf_section_data (o)->rel_hdr2 != NULL)
5294 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
5295 elf_section_data (o)->rel_count2,
5296 (elf_section_data (o)->rel_hashes
5297 + elf_section_data (o)->rel_count));
5299 /* Set the reloc_count field to 0 to prevent write_relocs from
5300 trying to swap the relocs out itself. */
5304 if (dynamic && info->combreloc && dynobj != NULL)
5305 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
5307 /* If we are linking against a dynamic object, or generating a
5308 shared library, finish up the dynamic linking information. */
5311 Elf_External_Dyn *dyncon, *dynconend;
5313 /* Fix up .dynamic entries. */
5314 o = bfd_get_section_by_name (dynobj, ".dynamic");
5315 BFD_ASSERT (o != NULL);
5317 dyncon = (Elf_External_Dyn *) o->contents;
5318 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
5319 for (; dyncon < dynconend; dyncon++)
5321 Elf_Internal_Dyn dyn;
5325 elf_swap_dyn_in (dynobj, dyncon, &dyn);
5332 if (relativecount > 0 && dyncon + 1 < dynconend)
5334 switch (elf_section_data (reldyn)->this_hdr.sh_type)
5336 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
5337 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
5340 if (dyn.d_tag != DT_NULL)
5342 dyn.d_un.d_val = relativecount;
5343 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5349 name = info->init_function;
5352 name = info->fini_function;
5355 struct elf_link_hash_entry *h;
5357 h = elf_link_hash_lookup (elf_hash_table (info), name,
5358 false, false, true);
5360 && (h->root.type == bfd_link_hash_defined
5361 || h->root.type == bfd_link_hash_defweak))
5363 dyn.d_un.d_val = h->root.u.def.value;
5364 o = h->root.u.def.section;
5365 if (o->output_section != NULL)
5366 dyn.d_un.d_val += (o->output_section->vma
5367 + o->output_offset);
5370 /* The symbol is imported from another shared
5371 library and does not apply to this one. */
5375 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5390 name = ".gnu.version_d";
5393 name = ".gnu.version_r";
5396 name = ".gnu.version";
5398 o = bfd_get_section_by_name (abfd, name);
5399 BFD_ASSERT (o != NULL);
5400 dyn.d_un.d_ptr = o->vma;
5401 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5408 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5413 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
5415 Elf_Internal_Shdr *hdr;
5417 hdr = elf_elfsections (abfd)[i];
5418 if (hdr->sh_type == type
5419 && (hdr->sh_flags & SHF_ALLOC) != 0)
5421 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
5422 dyn.d_un.d_val += hdr->sh_size;
5425 if (dyn.d_un.d_val == 0
5426 || hdr->sh_addr < dyn.d_un.d_val)
5427 dyn.d_un.d_val = hdr->sh_addr;
5431 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5437 /* If we have created any dynamic sections, then output them. */
5440 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
5443 for (o = dynobj->sections; o != NULL; o = o->next)
5445 if ((o->flags & SEC_HAS_CONTENTS) == 0
5446 || o->_raw_size == 0
5447 || o->output_section == bfd_abs_section_ptr)
5449 if ((o->flags & SEC_LINKER_CREATED) == 0)
5451 /* At this point, we are only interested in sections
5452 created by elf_link_create_dynamic_sections. */
5455 if ((elf_section_data (o->output_section)->this_hdr.sh_type
5457 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
5459 if (! bfd_set_section_contents (abfd, o->output_section,
5461 (file_ptr) o->output_offset,
5467 /* The contents of the .dynstr section are actually in a
5469 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
5470 if (bfd_seek (abfd, off, SEEK_SET) != 0
5471 || ! _bfd_elf_strtab_emit (abfd,
5472 elf_hash_table (info)->dynstr))
5478 /* If we have optimized stabs strings, output them. */
5479 if (elf_hash_table (info)->stab_info != NULL)
5481 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
5485 if (finfo.symstrtab != NULL)
5486 _bfd_stringtab_free (finfo.symstrtab);
5487 if (finfo.contents != NULL)
5488 free (finfo.contents);
5489 if (finfo.external_relocs != NULL)
5490 free (finfo.external_relocs);
5491 if (finfo.internal_relocs != NULL)
5492 free (finfo.internal_relocs);
5493 if (finfo.external_syms != NULL)
5494 free (finfo.external_syms);
5495 if (finfo.internal_syms != NULL)
5496 free (finfo.internal_syms);
5497 if (finfo.indices != NULL)
5498 free (finfo.indices);
5499 if (finfo.sections != NULL)
5500 free (finfo.sections);
5501 if (finfo.symbuf != NULL)
5502 free (finfo.symbuf);
5503 for (o = abfd->sections; o != NULL; o = o->next)
5505 if ((o->flags & SEC_RELOC) != 0
5506 && elf_section_data (o)->rel_hashes != NULL)
5507 free (elf_section_data (o)->rel_hashes);
5510 elf_tdata (abfd)->linker = true;
5515 if (finfo.symstrtab != NULL)
5516 _bfd_stringtab_free (finfo.symstrtab);
5517 if (finfo.contents != NULL)
5518 free (finfo.contents);
5519 if (finfo.external_relocs != NULL)
5520 free (finfo.external_relocs);
5521 if (finfo.internal_relocs != NULL)
5522 free (finfo.internal_relocs);
5523 if (finfo.external_syms != NULL)
5524 free (finfo.external_syms);
5525 if (finfo.internal_syms != NULL)
5526 free (finfo.internal_syms);
5527 if (finfo.indices != NULL)
5528 free (finfo.indices);
5529 if (finfo.sections != NULL)
5530 free (finfo.sections);
5531 if (finfo.symbuf != NULL)
5532 free (finfo.symbuf);
5533 for (o = abfd->sections; o != NULL; o = o->next)
5535 if ((o->flags & SEC_RELOC) != 0
5536 && elf_section_data (o)->rel_hashes != NULL)
5537 free (elf_section_data (o)->rel_hashes);
5543 /* Add a symbol to the output symbol table. */
5546 elf_link_output_sym (finfo, name, elfsym, input_sec)
5547 struct elf_final_link_info *finfo;
5549 Elf_Internal_Sym *elfsym;
5550 asection *input_sec;
5552 boolean (*output_symbol_hook) PARAMS ((bfd *,
5553 struct bfd_link_info *info,
5558 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
5559 elf_backend_link_output_symbol_hook;
5560 if (output_symbol_hook != NULL)
5562 if (! ((*output_symbol_hook)
5563 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
5567 if (name == (const char *) NULL || *name == '\0')
5568 elfsym->st_name = 0;
5569 else if (input_sec->flags & SEC_EXCLUDE)
5570 elfsym->st_name = 0;
5573 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5575 if (elfsym->st_name == (unsigned long) -1)
5579 if (finfo->symbuf_count >= finfo->symbuf_size)
5581 if (! elf_link_flush_output_syms (finfo))
5585 elf_swap_symbol_out (finfo->output_bfd, elfsym,
5586 (PTR) (finfo->symbuf + finfo->symbuf_count));
5587 ++finfo->symbuf_count;
5589 ++ bfd_get_symcount (finfo->output_bfd);
5594 /* Flush the output symbols to the file. */
5597 elf_link_flush_output_syms (finfo)
5598 struct elf_final_link_info *finfo;
5600 if (finfo->symbuf_count > 0)
5602 Elf_Internal_Shdr *symtab;
5606 symtab = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5607 pos = symtab->sh_offset + symtab->sh_size;
5608 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
5609 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5610 || bfd_bwrite ((PTR) finfo->symbuf, amt, finfo->output_bfd) != amt)
5613 symtab->sh_size += finfo->symbuf_count * sizeof (Elf_External_Sym);
5615 finfo->symbuf_count = 0;
5621 /* Adjust all external symbols pointing into SEC_MERGE sections
5622 to reflect the object merging within the sections. */
5625 elf_link_sec_merge_syms (h, data)
5626 struct elf_link_hash_entry *h;
5631 if ((h->root.type == bfd_link_hash_defined
5632 || h->root.type == bfd_link_hash_defweak)
5633 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
5634 && elf_section_data (sec)->merge_info)
5636 bfd *output_bfd = (bfd *) data;
5638 h->root.u.def.value =
5639 _bfd_merged_section_offset (output_bfd,
5640 &h->root.u.def.section,
5641 elf_section_data (sec)->merge_info,
5642 h->root.u.def.value, (bfd_vma) 0);
5648 /* Add an external symbol to the symbol table. This is called from
5649 the hash table traversal routine. When generating a shared object,
5650 we go through the symbol table twice. The first time we output
5651 anything that might have been forced to local scope in a version
5652 script. The second time we output the symbols that are still
5656 elf_link_output_extsym (h, data)
5657 struct elf_link_hash_entry *h;
5660 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
5661 struct elf_final_link_info *finfo = eoinfo->finfo;
5663 Elf_Internal_Sym sym;
5664 asection *input_sec;
5666 /* Decide whether to output this symbol in this pass. */
5667 if (eoinfo->localsyms)
5669 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5674 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5678 /* If we are not creating a shared library, and this symbol is
5679 referenced by a shared library but is not defined anywhere, then
5680 warn that it is undefined. If we do not do this, the runtime
5681 linker will complain that the symbol is undefined when the
5682 program is run. We don't have to worry about symbols that are
5683 referenced by regular files, because we will already have issued
5684 warnings for them. */
5685 if (! finfo->info->relocateable
5686 && ! finfo->info->allow_shlib_undefined
5687 && ! finfo->info->shared
5688 && h->root.type == bfd_link_hash_undefined
5689 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
5690 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
5692 if (! ((*finfo->info->callbacks->undefined_symbol)
5693 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
5694 (asection *) NULL, (bfd_vma) 0, true)))
5696 eoinfo->failed = true;
5701 /* We don't want to output symbols that have never been mentioned by
5702 a regular file, or that we have been told to strip. However, if
5703 h->indx is set to -2, the symbol is used by a reloc and we must
5707 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5708 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
5709 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
5710 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
5712 else if (finfo->info->strip == strip_all
5713 || (finfo->info->strip == strip_some
5714 && bfd_hash_lookup (finfo->info->keep_hash,
5715 h->root.root.string,
5716 false, false) == NULL))
5721 /* If we're stripping it, and it's not a dynamic symbol, there's
5722 nothing else to do unless it is a forced local symbol. */
5725 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5729 sym.st_size = h->size;
5730 sym.st_other = h->other;
5731 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5732 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
5733 else if (h->root.type == bfd_link_hash_undefweak
5734 || h->root.type == bfd_link_hash_defweak)
5735 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
5737 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
5739 switch (h->root.type)
5742 case bfd_link_hash_new:
5746 case bfd_link_hash_undefined:
5747 input_sec = bfd_und_section_ptr;
5748 sym.st_shndx = SHN_UNDEF;
5751 case bfd_link_hash_undefweak:
5752 input_sec = bfd_und_section_ptr;
5753 sym.st_shndx = SHN_UNDEF;
5756 case bfd_link_hash_defined:
5757 case bfd_link_hash_defweak:
5759 input_sec = h->root.u.def.section;
5760 if (input_sec->output_section != NULL)
5763 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
5764 input_sec->output_section);
5765 if (sym.st_shndx == (unsigned short) -1)
5767 (*_bfd_error_handler)
5768 (_("%s: could not find output section %s for input section %s"),
5769 bfd_get_filename (finfo->output_bfd),
5770 input_sec->output_section->name,
5772 eoinfo->failed = true;
5776 /* ELF symbols in relocateable files are section relative,
5777 but in nonrelocateable files they are virtual
5779 sym.st_value = h->root.u.def.value + input_sec->output_offset;
5780 if (! finfo->info->relocateable)
5781 sym.st_value += input_sec->output_section->vma;
5785 BFD_ASSERT (input_sec->owner == NULL
5786 || (input_sec->owner->flags & DYNAMIC) != 0);
5787 sym.st_shndx = SHN_UNDEF;
5788 input_sec = bfd_und_section_ptr;
5793 case bfd_link_hash_common:
5794 input_sec = h->root.u.c.p->section;
5795 sym.st_shndx = SHN_COMMON;
5796 sym.st_value = 1 << h->root.u.c.p->alignment_power;
5799 case bfd_link_hash_indirect:
5800 /* These symbols are created by symbol versioning. They point
5801 to the decorated version of the name. For example, if the
5802 symbol foo@@GNU_1.2 is the default, which should be used when
5803 foo is used with no version, then we add an indirect symbol
5804 foo which points to foo@@GNU_1.2. We ignore these symbols,
5805 since the indirected symbol is already in the hash table. */
5808 case bfd_link_hash_warning:
5809 /* We can't represent these symbols in ELF, although a warning
5810 symbol may have come from a .gnu.warning.SYMBOL section. We
5811 just put the target symbol in the hash table. If the target
5812 symbol does not really exist, don't do anything. */
5813 if (h->root.u.i.link->type == bfd_link_hash_new)
5815 return (elf_link_output_extsym
5816 ((struct elf_link_hash_entry *) h->root.u.i.link, data));
5819 /* Give the processor backend a chance to tweak the symbol value,
5820 and also to finish up anything that needs to be done for this
5822 if ((h->dynindx != -1
5823 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5824 && elf_hash_table (finfo->info)->dynamic_sections_created)
5826 struct elf_backend_data *bed;
5828 bed = get_elf_backend_data (finfo->output_bfd);
5829 if (! ((*bed->elf_backend_finish_dynamic_symbol)
5830 (finfo->output_bfd, finfo->info, h, &sym)))
5832 eoinfo->failed = true;
5837 /* If we are marking the symbol as undefined, and there are no
5838 non-weak references to this symbol from a regular object, then
5839 mark the symbol as weak undefined; if there are non-weak
5840 references, mark the symbol as strong. We can't do this earlier,
5841 because it might not be marked as undefined until the
5842 finish_dynamic_symbol routine gets through with it. */
5843 if (sym.st_shndx == SHN_UNDEF
5844 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
5845 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
5846 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
5850 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
5851 bindtype = STB_GLOBAL;
5853 bindtype = STB_WEAK;
5854 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
5857 /* If a symbol is not defined locally, we clear the visibility
5859 if (! finfo->info->relocateable
5860 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5861 sym.st_other ^= ELF_ST_VISIBILITY (sym.st_other);
5863 /* If this symbol should be put in the .dynsym section, then put it
5864 there now. We have already know the symbol index. We also fill
5865 in the entry in the .hash section. */
5866 if (h->dynindx != -1
5867 && elf_hash_table (finfo->info)->dynamic_sections_created)
5871 size_t hash_entry_size;
5872 bfd_byte *bucketpos;
5874 Elf_External_Sym *esym;
5876 sym.st_name = h->dynstr_index;
5877 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
5878 elf_swap_symbol_out (finfo->output_bfd, &sym, (PTR) esym);
5880 bucketcount = elf_hash_table (finfo->info)->bucketcount;
5881 bucket = h->elf_hash_value % bucketcount;
5883 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
5884 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
5885 + (bucket + 2) * hash_entry_size);
5886 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
5887 bfd_put (8 * hash_entry_size, finfo->output_bfd, (bfd_vma) h->dynindx,
5889 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
5890 ((bfd_byte *) finfo->hash_sec->contents
5891 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
5893 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
5895 Elf_Internal_Versym iversym;
5896 Elf_External_Versym *eversym;
5898 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5900 if (h->verinfo.verdef == NULL)
5901 iversym.vs_vers = 0;
5903 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
5907 if (h->verinfo.vertree == NULL)
5908 iversym.vs_vers = 1;
5910 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
5913 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
5914 iversym.vs_vers |= VERSYM_HIDDEN;
5916 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
5917 eversym += h->dynindx;
5918 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
5922 /* If we're stripping it, then it was just a dynamic symbol, and
5923 there's nothing else to do. */
5927 h->indx = bfd_get_symcount (finfo->output_bfd);
5929 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
5931 eoinfo->failed = true;
5938 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
5939 originated from the section given by INPUT_REL_HDR) to the
5943 elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
5946 asection *input_section;
5947 Elf_Internal_Shdr *input_rel_hdr;
5948 Elf_Internal_Rela *internal_relocs;
5950 Elf_Internal_Rela *irela;
5951 Elf_Internal_Rela *irelaend;
5952 Elf_Internal_Shdr *output_rel_hdr;
5953 asection *output_section;
5954 unsigned int *rel_countp = NULL;
5955 struct elf_backend_data *bed;
5958 output_section = input_section->output_section;
5959 output_rel_hdr = NULL;
5961 if (elf_section_data (output_section)->rel_hdr.sh_entsize
5962 == input_rel_hdr->sh_entsize)
5964 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
5965 rel_countp = &elf_section_data (output_section)->rel_count;
5967 else if (elf_section_data (output_section)->rel_hdr2
5968 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
5969 == input_rel_hdr->sh_entsize))
5971 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
5972 rel_countp = &elf_section_data (output_section)->rel_count2;
5975 BFD_ASSERT (output_rel_hdr != NULL);
5977 bed = get_elf_backend_data (output_bfd);
5978 irela = internal_relocs;
5979 irelaend = irela + NUM_SHDR_ENTRIES (input_rel_hdr)
5980 * bed->s->int_rels_per_ext_rel;
5982 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
5984 Elf_External_Rel *erel;
5985 Elf_Internal_Rel *irel;
5987 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
5988 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
5991 (*_bfd_error_handler) (_("Error: out of memory"));
5995 erel = ((Elf_External_Rel *) output_rel_hdr->contents + *rel_countp);
5996 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erel++)
6000 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6002 irel[i].r_offset = irela[i].r_offset;
6003 irel[i].r_info = irela[i].r_info;
6004 BFD_ASSERT (irela[i].r_addend == 0);
6007 if (bed->s->swap_reloc_out)
6008 (*bed->s->swap_reloc_out) (output_bfd, irel, (PTR) erel);
6010 elf_swap_reloc_out (output_bfd, irel, erel);
6017 Elf_External_Rela *erela;
6019 BFD_ASSERT (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rela));
6021 erela = ((Elf_External_Rela *) output_rel_hdr->contents + *rel_countp);
6022 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erela++)
6023 if (bed->s->swap_reloca_out)
6024 (*bed->s->swap_reloca_out) (output_bfd, irela, (PTR) erela);
6026 elf_swap_reloca_out (output_bfd, irela, erela);
6029 /* Bump the counter, so that we know where to add the next set of
6031 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
6034 /* Link an input file into the linker output file. This function
6035 handles all the sections and relocations of the input file at once.
6036 This is so that we only have to read the local symbols once, and
6037 don't have to keep them in memory. */
6040 elf_link_input_bfd (finfo, input_bfd)
6041 struct elf_final_link_info *finfo;
6044 boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
6045 bfd *, asection *, bfd_byte *,
6046 Elf_Internal_Rela *,
6047 Elf_Internal_Sym *, asection **));
6049 Elf_Internal_Shdr *symtab_hdr;
6052 Elf_External_Sym *external_syms;
6053 Elf_External_Sym *esym;
6054 Elf_External_Sym *esymend;
6055 Elf_Internal_Sym *isym;
6057 asection **ppsection;
6059 struct elf_backend_data *bed;
6060 boolean emit_relocs;
6061 struct elf_link_hash_entry **sym_hashes;
6063 output_bfd = finfo->output_bfd;
6064 bed = get_elf_backend_data (output_bfd);
6065 relocate_section = bed->elf_backend_relocate_section;
6067 /* If this is a dynamic object, we don't want to do anything here:
6068 we don't want the local symbols, and we don't want the section
6070 if ((input_bfd->flags & DYNAMIC) != 0)
6073 emit_relocs = (finfo->info->relocateable
6074 || finfo->info->emitrelocations
6075 || bed->elf_backend_emit_relocs);
6077 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6078 if (elf_bad_symtab (input_bfd))
6080 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6085 locsymcount = symtab_hdr->sh_info;
6086 extsymoff = symtab_hdr->sh_info;
6089 /* Read the local symbols. */
6090 if (symtab_hdr->contents != NULL)
6091 external_syms = (Elf_External_Sym *) symtab_hdr->contents;
6092 else if (locsymcount == 0)
6093 external_syms = NULL;
6096 bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym);
6097 external_syms = finfo->external_syms;
6098 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
6099 || bfd_bread (external_syms, amt, input_bfd) != amt)
6103 /* Swap in the local symbols and write out the ones which we know
6104 are going into the output file. */
6105 esym = external_syms;
6106 esymend = esym + locsymcount;
6107 isym = finfo->internal_syms;
6108 pindex = finfo->indices;
6109 ppsection = finfo->sections;
6110 for (; esym < esymend; esym++, isym++, pindex++, ppsection++)
6114 Elf_Internal_Sym osym;
6116 elf_swap_symbol_in (input_bfd, esym, isym);
6119 if (elf_bad_symtab (input_bfd))
6121 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6128 if (isym->st_shndx == SHN_UNDEF)
6129 isec = bfd_und_section_ptr;
6130 else if (isym->st_shndx > 0 && isym->st_shndx < SHN_LORESERVE)
6132 isec = section_from_elf_index (input_bfd, isym->st_shndx);
6133 if (isec && elf_section_data (isec)->merge_info
6134 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6136 _bfd_merged_section_offset (output_bfd, &isec,
6137 elf_section_data (isec)->merge_info,
6138 isym->st_value, (bfd_vma) 0);
6140 else if (isym->st_shndx == SHN_ABS)
6141 isec = bfd_abs_section_ptr;
6142 else if (isym->st_shndx == SHN_COMMON)
6143 isec = bfd_com_section_ptr;
6152 /* Don't output the first, undefined, symbol. */
6153 if (esym == external_syms)
6156 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6158 /* We never output section symbols. Instead, we use the
6159 section symbol of the corresponding section in the output
6164 /* If we are stripping all symbols, we don't want to output this
6166 if (finfo->info->strip == strip_all)
6169 /* If we are discarding all local symbols, we don't want to
6170 output this one. If we are generating a relocateable output
6171 file, then some of the local symbols may be required by
6172 relocs; we output them below as we discover that they are
6174 if (finfo->info->discard == discard_all)
6177 /* If this symbol is defined in a section which we are
6178 discarding, we don't need to keep it, but note that
6179 linker_mark is only reliable for sections that have contents.
6180 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6181 as well as linker_mark. */
6182 if (isym->st_shndx > 0
6183 && isym->st_shndx < SHN_LORESERVE
6185 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6186 || (! finfo->info->relocateable
6187 && (isec->flags & SEC_EXCLUDE) != 0)))
6190 /* Get the name of the symbol. */
6191 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6196 /* See if we are discarding symbols with this name. */
6197 if ((finfo->info->strip == strip_some
6198 && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
6200 || (((finfo->info->discard == discard_sec_merge
6201 && (isec->flags & SEC_MERGE) && ! finfo->info->relocateable)
6202 || finfo->info->discard == discard_l)
6203 && bfd_is_local_label_name (input_bfd, name)))
6206 /* If we get here, we are going to output this symbol. */
6210 /* Adjust the section index for the output file. */
6211 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6212 isec->output_section);
6213 if (osym.st_shndx == (unsigned short) -1)
6216 *pindex = bfd_get_symcount (output_bfd);
6218 /* ELF symbols in relocateable files are section relative, but
6219 in executable files they are virtual addresses. Note that
6220 this code assumes that all ELF sections have an associated
6221 BFD section with a reasonable value for output_offset; below
6222 we assume that they also have a reasonable value for
6223 output_section. Any special sections must be set up to meet
6224 these requirements. */
6225 osym.st_value += isec->output_offset;
6226 if (! finfo->info->relocateable)
6227 osym.st_value += isec->output_section->vma;
6229 if (! elf_link_output_sym (finfo, name, &osym, isec))
6233 /* Relocate the contents of each section. */
6234 sym_hashes = elf_sym_hashes (input_bfd);
6235 for (o = input_bfd->sections; o != NULL; o = o->next)
6239 if (! o->linker_mark)
6241 /* This section was omitted from the link. */
6245 if ((o->flags & SEC_HAS_CONTENTS) == 0
6246 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
6249 if ((o->flags & SEC_LINKER_CREATED) != 0)
6251 /* Section was created by elf_link_create_dynamic_sections
6256 /* Get the contents of the section. They have been cached by a
6257 relaxation routine. Note that o is a section in an input
6258 file, so the contents field will not have been set by any of
6259 the routines which work on output files. */
6260 if (elf_section_data (o)->this_hdr.contents != NULL)
6261 contents = elf_section_data (o)->this_hdr.contents;
6264 contents = finfo->contents;
6265 if (! bfd_get_section_contents (input_bfd, o, contents,
6266 (file_ptr) 0, o->_raw_size))
6270 if ((o->flags & SEC_RELOC) != 0)
6272 Elf_Internal_Rela *internal_relocs;
6274 /* Get the swapped relocs. */
6275 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
6276 (input_bfd, o, finfo->external_relocs,
6277 finfo->internal_relocs, false));
6278 if (internal_relocs == NULL
6279 && o->reloc_count > 0)
6282 /* Run through the relocs looking for any against symbols
6283 from discarded sections and section symbols from
6284 removed link-once sections. Complain about relocs
6285 against discarded sections. Zero relocs against removed
6286 link-once sections. We should really complain if
6287 anything in the final link tries to use it, but
6288 DWARF-based exception handling might have an entry in
6289 .eh_frame to describe a routine in the linkonce section,
6290 and it turns out to be hard to remove the .eh_frame
6291 entry too. FIXME. */
6292 if (!finfo->info->relocateable
6293 && !elf_section_ignore_discarded_relocs (o))
6295 Elf_Internal_Rela *rel, *relend;
6297 rel = internal_relocs;
6298 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6299 for ( ; rel < relend; rel++)
6301 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
6303 if (r_symndx >= locsymcount
6304 || (elf_bad_symtab (input_bfd)
6305 && finfo->sections[r_symndx] == NULL))
6307 struct elf_link_hash_entry *h;
6309 h = sym_hashes[r_symndx - extsymoff];
6310 while (h->root.type == bfd_link_hash_indirect
6311 || h->root.type == bfd_link_hash_warning)
6312 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6314 /* Complain if the definition comes from a
6315 discarded section. */
6316 if ((h->root.type == bfd_link_hash_defined
6317 || h->root.type == bfd_link_hash_defweak)
6318 && ! bfd_is_abs_section (h->root.u.def.section)
6319 && bfd_is_abs_section (h->root.u.def.section
6322 #if BFD_VERSION_DATE < 20031005
6323 if ((o->flags & SEC_DEBUGGING) != 0)
6325 #if BFD_VERSION_DATE > 20021005
6326 (*finfo->info->callbacks->warning)
6328 _("warning: relocation against removed section; zeroing"),
6329 NULL, input_bfd, o, rel->r_offset);
6331 memset (rel, 0, sizeof (*rel));
6336 if (! ((*finfo->info->callbacks->undefined_symbol)
6337 (finfo->info, h->root.root.string,
6338 input_bfd, o, rel->r_offset,
6346 asection *sec = finfo->sections[r_symndx];
6349 && ! bfd_is_abs_section (sec)
6350 && bfd_is_abs_section (sec->output_section))
6352 #if BFD_VERSION_DATE < 20031005
6353 if ((o->flags & SEC_DEBUGGING) != 0
6354 || (sec->flags & SEC_LINK_ONCE) != 0)
6356 #if BFD_VERSION_DATE > 20021005
6357 (*finfo->info->callbacks->warning)
6359 _("warning: relocation against removed section"),
6360 NULL, input_bfd, o, rel->r_offset);
6363 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
6371 = _("local symbols in discarded section %s");
6373 = strlen (sec->name) + strlen (msg) - 1;
6374 char *buf = (char *) bfd_malloc (amt);
6377 sprintf (buf, msg, sec->name);
6379 buf = (char *) sec->name;
6380 ok = (*finfo->info->callbacks
6381 ->undefined_symbol) (finfo->info, buf,
6385 if (buf != sec->name)
6395 /* Relocate the section by invoking a back end routine.
6397 The back end routine is responsible for adjusting the
6398 section contents as necessary, and (if using Rela relocs
6399 and generating a relocateable output file) adjusting the
6400 reloc addend as necessary.
6402 The back end routine does not have to worry about setting
6403 the reloc address or the reloc symbol index.
6405 The back end routine is given a pointer to the swapped in
6406 internal symbols, and can access the hash table entries
6407 for the external symbols via elf_sym_hashes (input_bfd).
6409 When generating relocateable output, the back end routine
6410 must handle STB_LOCAL/STT_SECTION symbols specially. The
6411 output symbol is going to be a section symbol
6412 corresponding to the output section, which will require
6413 the addend to be adjusted. */
6415 if (! (*relocate_section) (output_bfd, finfo->info,
6416 input_bfd, o, contents,
6418 finfo->internal_syms,
6424 Elf_Internal_Rela *irela;
6425 Elf_Internal_Rela *irelaend;
6426 struct elf_link_hash_entry **rel_hash;
6427 Elf_Internal_Shdr *input_rel_hdr;
6428 unsigned int next_erel;
6429 void (*reloc_emitter) PARAMS ((bfd *, asection *,
6430 Elf_Internal_Shdr *,
6431 Elf_Internal_Rela *));
6433 /* Adjust the reloc addresses and symbol indices. */
6435 irela = internal_relocs;
6436 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6437 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6438 + elf_section_data (o->output_section)->rel_count
6439 + elf_section_data (o->output_section)->rel_count2);
6440 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6442 unsigned long r_symndx;
6445 if (next_erel == bed->s->int_rels_per_ext_rel)
6451 irela->r_offset += o->output_offset;
6453 /* Relocs in an executable have to be virtual addresses. */
6454 if (finfo->info->emitrelocations)
6455 irela->r_offset += o->output_section->vma;
6457 r_symndx = ELF_R_SYM (irela->r_info);
6462 if (r_symndx >= locsymcount
6463 || (elf_bad_symtab (input_bfd)
6464 && finfo->sections[r_symndx] == NULL))
6466 struct elf_link_hash_entry *rh;
6469 /* This is a reloc against a global symbol. We
6470 have not yet output all the local symbols, so
6471 we do not know the symbol index of any global
6472 symbol. We set the rel_hash entry for this
6473 reloc to point to the global hash table entry
6474 for this symbol. The symbol index is then
6475 set at the end of elf_bfd_final_link. */
6476 indx = r_symndx - extsymoff;
6477 rh = elf_sym_hashes (input_bfd)[indx];
6478 while (rh->root.type == bfd_link_hash_indirect
6479 || rh->root.type == bfd_link_hash_warning)
6480 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6482 /* Setting the index to -2 tells
6483 elf_link_output_extsym that this symbol is
6485 BFD_ASSERT (rh->indx < 0);
6493 /* This is a reloc against a local symbol. */
6496 isym = finfo->internal_syms + r_symndx;
6497 sec = finfo->sections[r_symndx];
6498 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6500 /* I suppose the backend ought to fill in the
6501 section of any STT_SECTION symbol against a
6502 processor specific section. If we have
6503 discarded a section, the output_section will
6504 be the absolute section. */
6506 && (bfd_is_abs_section (sec)
6507 || (sec->output_section != NULL
6508 && bfd_is_abs_section (sec->output_section))))
6510 else if (sec == NULL || sec->owner == NULL)
6512 bfd_set_error (bfd_error_bad_value);
6517 r_symndx = sec->output_section->target_index;
6518 BFD_ASSERT (r_symndx != 0);
6523 if (finfo->indices[r_symndx] == -1)
6525 unsigned long shlink;
6529 if (finfo->info->strip == strip_all)
6531 /* You can't do ld -r -s. */
6532 bfd_set_error (bfd_error_invalid_operation);
6536 /* This symbol was skipped earlier, but
6537 since it is needed by a reloc, we
6538 must output it now. */
6539 shlink = symtab_hdr->sh_link;
6540 name = (bfd_elf_string_from_elf_section
6541 (input_bfd, shlink, isym->st_name));
6545 osec = sec->output_section;
6547 _bfd_elf_section_from_bfd_section (output_bfd,
6549 if (isym->st_shndx == (unsigned short) -1)
6552 isym->st_value += sec->output_offset;
6553 if (! finfo->info->relocateable)
6554 isym->st_value += osec->vma;
6556 finfo->indices[r_symndx]
6557 = bfd_get_symcount (output_bfd);
6559 if (! elf_link_output_sym (finfo, name, isym, sec))
6563 r_symndx = finfo->indices[r_symndx];
6566 irela->r_info = ELF_R_INFO (r_symndx,
6567 ELF_R_TYPE (irela->r_info));
6570 /* Swap out the relocs. */
6571 if (bed->elf_backend_emit_relocs
6572 && !(finfo->info->relocateable
6573 || finfo->info->emitrelocations))
6574 reloc_emitter = bed->elf_backend_emit_relocs;
6576 reloc_emitter = elf_link_output_relocs;
6578 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6579 (*reloc_emitter) (output_bfd, o, input_rel_hdr, internal_relocs);
6581 input_rel_hdr = elf_section_data (o)->rel_hdr2;
6584 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
6585 * bed->s->int_rels_per_ext_rel);
6586 reloc_emitter (output_bfd, o, input_rel_hdr, internal_relocs);
6592 /* Write out the modified section contents. */
6593 if (bed->elf_backend_write_section
6594 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
6596 /* Section written out. */
6598 else if (elf_section_data (o)->stab_info)
6600 if (! (_bfd_write_section_stabs
6601 (output_bfd, &elf_hash_table (finfo->info)->stab_info,
6602 o, &elf_section_data (o)->stab_info, contents)))
6605 else if (elf_section_data (o)->merge_info)
6607 if (! (_bfd_write_merged_section
6608 (output_bfd, o, elf_section_data (o)->merge_info)))
6613 bfd_size_type sec_size;
6615 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
6616 if (! (o->flags & SEC_EXCLUDE)
6617 && ! bfd_set_section_contents (output_bfd, o->output_section,
6619 (file_ptr) o->output_offset,
6628 /* Generate a reloc when linking an ELF file. This is a reloc
6629 requested by the linker, and does come from any input file. This
6630 is used to build constructor and destructor tables when linking
6634 elf_reloc_link_order (output_bfd, info, output_section, link_order)
6636 struct bfd_link_info *info;
6637 asection *output_section;
6638 struct bfd_link_order *link_order;
6640 reloc_howto_type *howto;
6644 struct elf_link_hash_entry **rel_hash_ptr;
6645 Elf_Internal_Shdr *rel_hdr;
6646 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
6648 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
6651 bfd_set_error (bfd_error_bad_value);
6655 addend = link_order->u.reloc.p->addend;
6657 /* Figure out the symbol index. */
6658 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
6659 + elf_section_data (output_section)->rel_count
6660 + elf_section_data (output_section)->rel_count2);
6661 if (link_order->type == bfd_section_reloc_link_order)
6663 indx = link_order->u.reloc.p->u.section->target_index;
6664 BFD_ASSERT (indx != 0);
6665 *rel_hash_ptr = NULL;
6669 struct elf_link_hash_entry *h;
6671 /* Treat a reloc against a defined symbol as though it were
6672 actually against the section. */
6673 h = ((struct elf_link_hash_entry *)
6674 bfd_wrapped_link_hash_lookup (output_bfd, info,
6675 link_order->u.reloc.p->u.name,
6676 false, false, true));
6678 && (h->root.type == bfd_link_hash_defined
6679 || h->root.type == bfd_link_hash_defweak))
6683 section = h->root.u.def.section;
6684 indx = section->output_section->target_index;
6685 *rel_hash_ptr = NULL;
6686 /* It seems that we ought to add the symbol value to the
6687 addend here, but in practice it has already been added
6688 because it was passed to constructor_callback. */
6689 addend += section->output_section->vma + section->output_offset;
6693 /* Setting the index to -2 tells elf_link_output_extsym that
6694 this symbol is used by a reloc. */
6701 if (! ((*info->callbacks->unattached_reloc)
6702 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
6703 (asection *) NULL, (bfd_vma) 0)))
6709 /* If this is an inplace reloc, we must write the addend into the
6711 if (howto->partial_inplace && addend != 0)
6714 bfd_reloc_status_type rstat;
6717 const char *sym_name;
6719 size = bfd_get_reloc_size (howto);
6720 buf = (bfd_byte *) bfd_zmalloc (size);
6721 if (buf == (bfd_byte *) NULL)
6723 rstat = _bfd_relocate_contents (howto, output_bfd, (bfd_vma) addend, buf);
6730 case bfd_reloc_outofrange:
6733 case bfd_reloc_overflow:
6734 if (link_order->type == bfd_section_reloc_link_order)
6735 sym_name = bfd_section_name (output_bfd,
6736 link_order->u.reloc.p->u.section);
6738 sym_name = link_order->u.reloc.p->u.name;
6739 if (! ((*info->callbacks->reloc_overflow)
6740 (info, sym_name, howto->name, addend,
6741 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
6748 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
6749 (file_ptr) link_order->offset, size);
6755 /* The address of a reloc is relative to the section in a
6756 relocateable file, and is a virtual address in an executable
6758 offset = link_order->offset;
6759 if (! info->relocateable)
6760 offset += output_section->vma;
6762 rel_hdr = &elf_section_data (output_section)->rel_hdr;
6764 if (rel_hdr->sh_type == SHT_REL)
6767 Elf_Internal_Rel *irel;
6768 Elf_External_Rel *erel;
6771 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
6772 irel = (Elf_Internal_Rel *) bfd_zmalloc (size);
6776 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6777 irel[i].r_offset = offset;
6778 irel[0].r_info = ELF_R_INFO (indx, howto->type);
6780 erel = ((Elf_External_Rel *) rel_hdr->contents
6781 + elf_section_data (output_section)->rel_count);
6783 if (bed->s->swap_reloc_out)
6784 (*bed->s->swap_reloc_out) (output_bfd, irel, (bfd_byte *) erel);
6786 elf_swap_reloc_out (output_bfd, irel, erel);
6793 Elf_Internal_Rela *irela;
6794 Elf_External_Rela *erela;
6797 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
6798 irela = (Elf_Internal_Rela *) bfd_zmalloc (size);
6802 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6803 irela[i].r_offset = offset;
6804 irela[0].r_info = ELF_R_INFO (indx, howto->type);
6805 irela[0].r_addend = addend;
6807 erela = ((Elf_External_Rela *) rel_hdr->contents
6808 + elf_section_data (output_section)->rel_count);
6810 if (bed->s->swap_reloca_out)
6811 (*bed->s->swap_reloca_out) (output_bfd, irela, (bfd_byte *) erela);
6813 elf_swap_reloca_out (output_bfd, irela, erela);
6816 ++elf_section_data (output_section)->rel_count;
6821 /* Allocate a pointer to live in a linker created section. */
6824 elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
6826 struct bfd_link_info *info;
6827 elf_linker_section_t *lsect;
6828 struct elf_link_hash_entry *h;
6829 const Elf_Internal_Rela *rel;
6831 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
6832 elf_linker_section_pointers_t *linker_section_ptr;
6833 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
6836 BFD_ASSERT (lsect != NULL);
6838 /* Is this a global symbol? */
6841 /* Has this symbol already been allocated? If so, our work is done. */
6842 if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
6847 ptr_linker_section_ptr = &h->linker_section_pointer;
6848 /* Make sure this symbol is output as a dynamic symbol. */
6849 if (h->dynindx == -1)
6851 if (! elf_link_record_dynamic_symbol (info, h))
6855 if (lsect->rel_section)
6856 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
6860 /* Allocation of a pointer to a local symbol. */
6861 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
6863 /* Allocate a table to hold the local symbols if first time. */
6866 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
6867 register unsigned int i;
6870 amt *= sizeof (elf_linker_section_pointers_t *);
6871 ptr = (elf_linker_section_pointers_t **) bfd_alloc (abfd, amt);
6876 elf_local_ptr_offsets (abfd) = ptr;
6877 for (i = 0; i < num_symbols; i++)
6878 ptr[i] = (elf_linker_section_pointers_t *) 0;
6881 /* Has this symbol already been allocated? If so, our work is done. */
6882 if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
6887 ptr_linker_section_ptr = &ptr[r_symndx];
6891 /* If we are generating a shared object, we need to
6892 output a R_<xxx>_RELATIVE reloc so that the
6893 dynamic linker can adjust this GOT entry. */
6894 BFD_ASSERT (lsect->rel_section != NULL);
6895 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
6899 /* Allocate space for a pointer in the linker section, and allocate
6900 a new pointer record from internal memory. */
6901 BFD_ASSERT (ptr_linker_section_ptr != NULL);
6902 amt = sizeof (elf_linker_section_pointers_t);
6903 linker_section_ptr = (elf_linker_section_pointers_t *) bfd_alloc (abfd, amt);
6905 if (!linker_section_ptr)
6908 linker_section_ptr->next = *ptr_linker_section_ptr;
6909 linker_section_ptr->addend = rel->r_addend;
6910 linker_section_ptr->which = lsect->which;
6911 linker_section_ptr->written_address_p = false;
6912 *ptr_linker_section_ptr = linker_section_ptr;
6915 if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
6917 linker_section_ptr->offset = (lsect->section->_raw_size
6918 - lsect->hole_size + (ARCH_SIZE / 8));
6919 lsect->hole_offset += ARCH_SIZE / 8;
6920 lsect->sym_offset += ARCH_SIZE / 8;
6921 if (lsect->sym_hash)
6923 /* Bump up symbol value if needed. */
6924 lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
6926 fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
6927 lsect->sym_hash->root.root.string,
6928 (long) ARCH_SIZE / 8,
6929 (long) lsect->sym_hash->root.u.def.value);
6935 linker_section_ptr->offset = lsect->section->_raw_size;
6937 lsect->section->_raw_size += ARCH_SIZE / 8;
6941 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
6942 lsect->name, (long) linker_section_ptr->offset,
6943 (long) lsect->section->_raw_size);
6950 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
6953 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
6956 /* Fill in the address for a pointer generated in a linker section. */
6959 elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h,
6960 relocation, rel, relative_reloc)
6963 struct bfd_link_info *info;
6964 elf_linker_section_t *lsect;
6965 struct elf_link_hash_entry *h;
6967 const Elf_Internal_Rela *rel;
6970 elf_linker_section_pointers_t *linker_section_ptr;
6972 BFD_ASSERT (lsect != NULL);
6976 /* Handle global symbol. */
6977 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
6978 (h->linker_section_pointer,
6982 BFD_ASSERT (linker_section_ptr != NULL);
6984 if (! elf_hash_table (info)->dynamic_sections_created
6987 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
6989 /* This is actually a static link, or it is a
6990 -Bsymbolic link and the symbol is defined
6991 locally. We must initialize this entry in the
6994 When doing a dynamic link, we create a .rela.<xxx>
6995 relocation entry to initialize the value. This
6996 is done in the finish_dynamic_symbol routine. */
6997 if (!linker_section_ptr->written_address_p)
6999 linker_section_ptr->written_address_p = true;
7000 bfd_put_ptr (output_bfd,
7001 relocation + linker_section_ptr->addend,
7002 (lsect->section->contents
7003 + linker_section_ptr->offset));
7009 /* Handle local symbol. */
7010 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7011 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
7012 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
7013 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7014 (elf_local_ptr_offsets (input_bfd)[r_symndx],
7018 BFD_ASSERT (linker_section_ptr != NULL);
7020 /* Write out pointer if it hasn't been rewritten out before. */
7021 if (!linker_section_ptr->written_address_p)
7023 linker_section_ptr->written_address_p = true;
7024 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
7025 lsect->section->contents + linker_section_ptr->offset);
7029 asection *srel = lsect->rel_section;
7030 Elf_Internal_Rela *outrel;
7031 Elf_External_Rela *erel;
7032 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7036 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
7037 outrel = (Elf_Internal_Rela *) bfd_zmalloc (amt);
7040 (*_bfd_error_handler) (_("Error: out of memory"));
7044 /* We need to generate a relative reloc for the dynamic
7048 srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
7050 lsect->rel_section = srel;
7053 BFD_ASSERT (srel != NULL);
7055 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7056 outrel[i].r_offset = (lsect->section->output_section->vma
7057 + lsect->section->output_offset
7058 + linker_section_ptr->offset);
7059 outrel[0].r_info = ELF_R_INFO (0, relative_reloc);
7060 outrel[0].r_addend = 0;
7061 erel = (Elf_External_Rela *) lsect->section->contents;
7062 erel += elf_section_data (lsect->section)->rel_count;
7063 elf_swap_reloca_out (output_bfd, outrel, erel);
7064 ++elf_section_data (lsect->section)->rel_count;
7071 relocation = (lsect->section->output_offset
7072 + linker_section_ptr->offset
7073 - lsect->hole_offset
7074 - lsect->sym_offset);
7078 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7079 lsect->name, (long) relocation, (long) relocation);
7082 /* Subtract out the addend, because it will get added back in by the normal
7084 return relocation - linker_section_ptr->addend;
7087 /* Garbage collect unused sections. */
7089 static boolean elf_gc_mark
7090 PARAMS ((struct bfd_link_info *info, asection *sec,
7091 asection * (*gc_mark_hook)
7092 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7093 struct elf_link_hash_entry *, Elf_Internal_Sym *))));
7095 static boolean elf_gc_sweep
7096 PARAMS ((struct bfd_link_info *info,
7097 boolean (*gc_sweep_hook)
7098 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7099 const Elf_Internal_Rela *relocs))));
7101 static boolean elf_gc_sweep_symbol
7102 PARAMS ((struct elf_link_hash_entry *h, PTR idxptr));
7104 static boolean elf_gc_allocate_got_offsets
7105 PARAMS ((struct elf_link_hash_entry *h, PTR offarg));
7107 static boolean elf_gc_propagate_vtable_entries_used
7108 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7110 static boolean elf_gc_smash_unused_vtentry_relocs
7111 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7113 /* The mark phase of garbage collection. For a given section, mark
7114 it and any sections in this section's group, and all the sections
7115 which define symbols to which it refers. */
7118 elf_gc_mark (info, sec, gc_mark_hook)
7119 struct bfd_link_info *info;
7121 asection * (*gc_mark_hook)
7122 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7123 struct elf_link_hash_entry *, Elf_Internal_Sym *));
7126 asection *group_sec;
7130 /* Mark all the sections in the group. */
7131 group_sec = elf_section_data (sec)->next_in_group;
7132 if (group_sec && !group_sec->gc_mark)
7133 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
7136 /* Look through the section relocs. */
7138 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
7140 Elf_Internal_Rela *relstart, *rel, *relend;
7141 Elf_Internal_Shdr *symtab_hdr;
7142 struct elf_link_hash_entry **sym_hashes;
7145 Elf_External_Sym *locsyms, *freesyms = NULL;
7146 bfd *input_bfd = sec->owner;
7147 struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
7149 /* GCFIXME: how to arrange so that relocs and symbols are not
7150 reread continually? */
7152 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7153 sym_hashes = elf_sym_hashes (input_bfd);
7155 /* Read the local symbols. */
7156 if (elf_bad_symtab (input_bfd))
7158 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
7162 extsymoff = nlocsyms = symtab_hdr->sh_info;
7163 if (symtab_hdr->contents)
7164 locsyms = (Elf_External_Sym *) symtab_hdr->contents;
7165 else if (nlocsyms == 0)
7169 bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym);
7170 locsyms = freesyms = bfd_malloc (amt);
7171 if (freesyms == NULL
7172 || bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
7173 || bfd_bread (locsyms, amt, input_bfd) != amt)
7180 /* Read the relocations. */
7181 relstart = (NAME(_bfd_elf,link_read_relocs)
7182 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL,
7183 info->keep_memory));
7184 if (relstart == NULL)
7189 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7191 for (rel = relstart; rel < relend; rel++)
7193 unsigned long r_symndx;
7195 struct elf_link_hash_entry *h;
7198 r_symndx = ELF_R_SYM (rel->r_info);
7202 if (elf_bad_symtab (sec->owner))
7204 elf_swap_symbol_in (input_bfd, &locsyms[r_symndx], &s);
7205 if (ELF_ST_BIND (s.st_info) == STB_LOCAL)
7206 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
7209 h = sym_hashes[r_symndx - extsymoff];
7210 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
7213 else if (r_symndx >= nlocsyms)
7215 h = sym_hashes[r_symndx - extsymoff];
7216 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
7220 elf_swap_symbol_in (input_bfd, &locsyms[r_symndx], &s);
7221 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
7224 if (rsec && !rsec->gc_mark)
7225 if (!elf_gc_mark (info, rsec, gc_mark_hook))
7233 if (!info->keep_memory)
7243 /* The sweep phase of garbage collection. Remove all garbage sections. */
7246 elf_gc_sweep (info, gc_sweep_hook)
7247 struct bfd_link_info *info;
7248 boolean (*gc_sweep_hook)
7249 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7250 const Elf_Internal_Rela *relocs));
7254 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7258 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7261 for (o = sub->sections; o != NULL; o = o->next)
7263 /* Keep special sections. Keep .debug sections. */
7264 if ((o->flags & SEC_LINKER_CREATED)
7265 || (o->flags & SEC_DEBUGGING))
7271 /* Skip sweeping sections already excluded. */
7272 if (o->flags & SEC_EXCLUDE)
7275 /* Since this is early in the link process, it is simple
7276 to remove a section from the output. */
7277 o->flags |= SEC_EXCLUDE;
7279 /* But we also have to update some of the relocation
7280 info we collected before. */
7282 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
7284 Elf_Internal_Rela *internal_relocs;
7287 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
7288 (o->owner, o, NULL, NULL, info->keep_memory));
7289 if (internal_relocs == NULL)
7292 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
7294 if (!info->keep_memory)
7295 free (internal_relocs);
7303 /* Remove the symbols that were in the swept sections from the dynamic
7304 symbol table. GCFIXME: Anyone know how to get them out of the
7305 static symbol table as well? */
7309 elf_link_hash_traverse (elf_hash_table (info),
7310 elf_gc_sweep_symbol,
7313 elf_hash_table (info)->dynsymcount = i;
7319 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7322 elf_gc_sweep_symbol (h, idxptr)
7323 struct elf_link_hash_entry *h;
7326 int *idx = (int *) idxptr;
7328 if (h->dynindx != -1
7329 && ((h->root.type != bfd_link_hash_defined
7330 && h->root.type != bfd_link_hash_defweak)
7331 || h->root.u.def.section->gc_mark))
7332 h->dynindx = (*idx)++;
7337 /* Propogate collected vtable information. This is called through
7338 elf_link_hash_traverse. */
7341 elf_gc_propagate_vtable_entries_used (h, okp)
7342 struct elf_link_hash_entry *h;
7345 /* Those that are not vtables. */
7346 if (h->vtable_parent == NULL)
7349 /* Those vtables that do not have parents, we cannot merge. */
7350 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
7353 /* If we've already been done, exit. */
7354 if (h->vtable_entries_used && h->vtable_entries_used[-1])
7357 /* Make sure the parent's table is up to date. */
7358 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
7360 if (h->vtable_entries_used == NULL)
7362 /* None of this table's entries were referenced. Re-use the
7364 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
7365 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
7372 /* Or the parent's entries into ours. */
7373 cu = h->vtable_entries_used;
7375 pu = h->vtable_parent->vtable_entries_used;
7378 asection *sec = h->root.u.def.section;
7379 struct elf_backend_data *bed = get_elf_backend_data (sec->owner);
7380 int file_align = bed->s->file_align;
7382 n = h->vtable_parent->vtable_entries_size / file_align;
7397 elf_gc_smash_unused_vtentry_relocs (h, okp)
7398 struct elf_link_hash_entry *h;
7402 bfd_vma hstart, hend;
7403 Elf_Internal_Rela *relstart, *relend, *rel;
7404 struct elf_backend_data *bed;
7407 /* Take care of both those symbols that do not describe vtables as
7408 well as those that are not loaded. */
7409 if (h->vtable_parent == NULL)
7412 BFD_ASSERT (h->root.type == bfd_link_hash_defined
7413 || h->root.type == bfd_link_hash_defweak);
7415 sec = h->root.u.def.section;
7416 hstart = h->root.u.def.value;
7417 hend = hstart + h->size;
7419 relstart = (NAME(_bfd_elf,link_read_relocs)
7420 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, true));
7422 return *(boolean *) okp = false;
7423 bed = get_elf_backend_data (sec->owner);
7424 file_align = bed->s->file_align;
7426 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7428 for (rel = relstart; rel < relend; ++rel)
7429 if (rel->r_offset >= hstart && rel->r_offset < hend)
7431 /* If the entry is in use, do nothing. */
7432 if (h->vtable_entries_used
7433 && (rel->r_offset - hstart) < h->vtable_entries_size)
7435 bfd_vma entry = (rel->r_offset - hstart) / file_align;
7436 if (h->vtable_entries_used[entry])
7439 /* Otherwise, kill it. */
7440 rel->r_offset = rel->r_info = rel->r_addend = 0;
7446 /* Do mark and sweep of unused sections. */
7449 elf_gc_sections (abfd, info)
7451 struct bfd_link_info *info;
7455 asection * (*gc_mark_hook)
7456 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7457 struct elf_link_hash_entry *h, Elf_Internal_Sym *));
7459 if (!get_elf_backend_data (abfd)->can_gc_sections
7460 || info->relocateable || info->emitrelocations
7461 || elf_hash_table (info)->dynamic_sections_created)
7464 /* Apply transitive closure to the vtable entry usage info. */
7465 elf_link_hash_traverse (elf_hash_table (info),
7466 elf_gc_propagate_vtable_entries_used,
7471 /* Kill the vtable relocations that were not used. */
7472 elf_link_hash_traverse (elf_hash_table (info),
7473 elf_gc_smash_unused_vtentry_relocs,
7478 /* Grovel through relocs to find out who stays ... */
7480 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
7481 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7485 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7488 for (o = sub->sections; o != NULL; o = o->next)
7490 if (o->flags & SEC_KEEP)
7491 if (!elf_gc_mark (info, o, gc_mark_hook))
7496 /* ... and mark SEC_EXCLUDE for those that go. */
7497 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
7503 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
7506 elf_gc_record_vtinherit (abfd, sec, h, offset)
7509 struct elf_link_hash_entry *h;
7512 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7513 struct elf_link_hash_entry **search, *child;
7514 bfd_size_type extsymcount;
7516 /* The sh_info field of the symtab header tells us where the
7517 external symbols start. We don't care about the local symbols at
7519 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
7520 if (!elf_bad_symtab (abfd))
7521 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
7523 sym_hashes = elf_sym_hashes (abfd);
7524 sym_hashes_end = sym_hashes + extsymcount;
7526 /* Hunt down the child symbol, which is in this section at the same
7527 offset as the relocation. */
7528 for (search = sym_hashes; search != sym_hashes_end; ++search)
7530 if ((child = *search) != NULL
7531 && (child->root.type == bfd_link_hash_defined
7532 || child->root.type == bfd_link_hash_defweak)
7533 && child->root.u.def.section == sec
7534 && child->root.u.def.value == offset)
7538 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
7539 bfd_archive_filename (abfd), sec->name,
7540 (unsigned long) offset);
7541 bfd_set_error (bfd_error_invalid_operation);
7547 /* This *should* only be the absolute section. It could potentially
7548 be that someone has defined a non-global vtable though, which
7549 would be bad. It isn't worth paging in the local symbols to be
7550 sure though; that case should simply be handled by the assembler. */
7552 child->vtable_parent = (struct elf_link_hash_entry *) -1;
7555 child->vtable_parent = h;
7560 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
7563 elf_gc_record_vtentry (abfd, sec, h, addend)
7564 bfd *abfd ATTRIBUTE_UNUSED;
7565 asection *sec ATTRIBUTE_UNUSED;
7566 struct elf_link_hash_entry *h;
7569 struct elf_backend_data *bed = get_elf_backend_data (abfd);
7570 int file_align = bed->s->file_align;
7572 if (addend >= h->vtable_entries_size)
7575 boolean *ptr = h->vtable_entries_used;
7577 /* While the symbol is undefined, we have to be prepared to handle
7579 if (h->root.type == bfd_link_hash_undefined)
7586 /* Oops! We've got a reference past the defined end of
7587 the table. This is probably a bug -- shall we warn? */
7592 /* Allocate one extra entry for use as a "done" flag for the
7593 consolidation pass. */
7594 bytes = (size / file_align + 1) * sizeof (boolean);
7598 ptr = bfd_realloc (ptr - 1, (bfd_size_type) bytes);
7604 oldbytes = ((h->vtable_entries_size / file_align + 1)
7605 * sizeof (boolean));
7606 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
7610 ptr = bfd_zmalloc ((bfd_size_type) bytes);
7615 /* And arrange for that done flag to be at index -1. */
7616 h->vtable_entries_used = ptr + 1;
7617 h->vtable_entries_size = size;
7620 h->vtable_entries_used[addend / file_align] = true;
7625 /* And an accompanying bit to work out final got entry offsets once
7626 we're done. Should be called from final_link. */
7629 elf_gc_common_finalize_got_offsets (abfd, info)
7631 struct bfd_link_info *info;
7634 struct elf_backend_data *bed = get_elf_backend_data (abfd);
7637 /* The GOT offset is relative to the .got section, but the GOT header is
7638 put into the .got.plt section, if the backend uses it. */
7639 if (bed->want_got_plt)
7642 gotoff = bed->got_header_size;
7644 /* Do the local .got entries first. */
7645 for (i = info->input_bfds; i; i = i->link_next)
7647 bfd_signed_vma *local_got;
7648 bfd_size_type j, locsymcount;
7649 Elf_Internal_Shdr *symtab_hdr;
7651 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
7654 local_got = elf_local_got_refcounts (i);
7658 symtab_hdr = &elf_tdata (i)->symtab_hdr;
7659 if (elf_bad_symtab (i))
7660 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
7662 locsymcount = symtab_hdr->sh_info;
7664 for (j = 0; j < locsymcount; ++j)
7666 if (local_got[j] > 0)
7668 local_got[j] = gotoff;
7669 gotoff += ARCH_SIZE / 8;
7672 local_got[j] = (bfd_vma) -1;
7676 /* Then the global .got entries. .plt refcounts are handled by
7677 adjust_dynamic_symbol */
7678 elf_link_hash_traverse (elf_hash_table (info),
7679 elf_gc_allocate_got_offsets,
7684 /* We need a special top-level link routine to convert got reference counts
7685 to real got offsets. */
7688 elf_gc_allocate_got_offsets (h, offarg)
7689 struct elf_link_hash_entry *h;
7692 bfd_vma *off = (bfd_vma *) offarg;
7694 if (h->got.refcount > 0)
7696 h->got.offset = off[0];
7697 off[0] += ARCH_SIZE / 8;
7700 h->got.offset = (bfd_vma) -1;
7705 /* Many folk need no more in the way of final link than this, once
7706 got entry reference counting is enabled. */
7709 elf_gc_common_final_link (abfd, info)
7711 struct bfd_link_info *info;
7713 if (!elf_gc_common_finalize_got_offsets (abfd, info))
7716 /* Invoke the regular ELF backend linker to do all the work. */
7717 return elf_bfd_final_link (abfd, info);
7720 /* This function will be called though elf_link_hash_traverse to store
7721 all hash value of the exported symbols in an array. */
7724 elf_collect_hash_codes (h, data)
7725 struct elf_link_hash_entry *h;
7728 unsigned long **valuep = (unsigned long **) data;
7734 /* Ignore indirect symbols. These are added by the versioning code. */
7735 if (h->dynindx == -1)
7738 name = h->root.root.string;
7739 p = strchr (name, ELF_VER_CHR);
7742 alc = bfd_malloc ((bfd_size_type) (p - name + 1));
7743 memcpy (alc, name, (size_t) (p - name));
7744 alc[p - name] = '\0';
7748 /* Compute the hash value. */
7749 ha = bfd_elf_hash (name);
7751 /* Store the found hash value in the array given as the argument. */
7754 /* And store it in the struct so that we can put it in the hash table
7756 h->elf_hash_value = ha;
7765 elf_reloc_symbol_deleted_p (offset, cookie)
7769 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *)cookie;
7771 if (rcookie->bad_symtab)
7772 rcookie->rel = rcookie->rels;
7774 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
7776 unsigned long r_symndx = ELF_R_SYM (rcookie->rel->r_info);
7777 Elf_Internal_Sym isym;
7779 if (! rcookie->bad_symtab)
7780 if (rcookie->rel->r_offset > offset)
7782 if (rcookie->rel->r_offset != offset)
7785 if (rcookie->locsyms && r_symndx < rcookie->locsymcount)
7786 elf_swap_symbol_in (rcookie->abfd,
7787 (Elf_External_Sym *) rcookie->locsyms + r_symndx,
7790 if (r_symndx >= rcookie->locsymcount
7791 || (rcookie->locsyms
7792 && ELF_ST_BIND (isym.st_info) != STB_LOCAL))
7794 struct elf_link_hash_entry *h;
7796 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
7798 while (h->root.type == bfd_link_hash_indirect
7799 || h->root.type == bfd_link_hash_warning)
7800 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7802 if ((h->root.type == bfd_link_hash_defined
7803 || h->root.type == bfd_link_hash_defweak)
7804 && ! bfd_is_abs_section (h->root.u.def.section)
7805 && bfd_is_abs_section (h->root.u.def.section
7811 else if (rcookie->locsyms)
7813 /* It's not a relocation against a global symbol,
7814 but it could be a relocation against a local
7815 symbol for a discarded section. */
7818 /* Need to: get the symbol; get the section. */
7819 if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
7821 isec = section_from_elf_index (rcookie->abfd, isym.st_shndx);
7823 && ! bfd_is_abs_section (isec)
7824 && bfd_is_abs_section (isec->output_section))
7833 /* Discard unneeded references to discarded sections.
7834 Returns true if any section's size was changed. */
7835 /* This function assumes that the relocations are in sorted order,
7836 which is true for all known assemblers. */
7839 elf_bfd_discard_info (info)
7840 struct bfd_link_info *info;
7842 struct elf_reloc_cookie cookie;
7844 Elf_Internal_Shdr *symtab_hdr;
7845 Elf_External_Sym *freesyms;
7846 struct elf_backend_data *bed;
7848 boolean ret = false;
7850 if (info->relocateable
7851 || info->traditional_format
7852 || info->hash->creator->flavour != bfd_target_elf_flavour
7853 || ! is_elf_hash_table (info)
7854 || info->strip == strip_all
7855 || info->strip == strip_debugger)
7857 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
7859 bed = get_elf_backend_data (abfd);
7861 if ((abfd->flags & DYNAMIC) != 0)
7864 o = bfd_get_section_by_name (abfd, ".stab");
7865 if (! o && ! bed->elf_backend_discard_info)
7868 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7871 cookie.sym_hashes = elf_sym_hashes (abfd);
7872 cookie.bad_symtab = elf_bad_symtab (abfd);
7873 if (cookie.bad_symtab)
7875 cookie.locsymcount =
7876 symtab_hdr->sh_size / sizeof (Elf_External_Sym);
7877 cookie.extsymoff = 0;
7881 cookie.locsymcount = symtab_hdr->sh_info;
7882 cookie.extsymoff = symtab_hdr->sh_info;
7886 if (symtab_hdr->contents)
7887 cookie.locsyms = (void *) symtab_hdr->contents;
7888 else if (cookie.locsymcount == 0)
7889 cookie.locsyms = NULL;
7892 bfd_size_type amt = cookie.locsymcount * sizeof (Elf_External_Sym);
7893 cookie.locsyms = bfd_malloc (amt);
7894 if (cookie.locsyms == NULL
7895 || bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
7896 || bfd_bread (cookie.locsyms, amt, abfd) != amt)
7898 /* Something is very wrong - but we can still do our job for
7899 global symbols, so don't give up. */
7901 free (cookie.locsyms);
7902 cookie.locsyms = NULL;
7906 freesyms = cookie.locsyms;
7912 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
7913 (abfd, o, (PTR) NULL,
7914 (Elf_Internal_Rela *) NULL,
7915 info->keep_memory));
7918 cookie.rel = cookie.rels;
7920 cookie.rels + o->reloc_count * bed->s->int_rels_per_ext_rel;
7921 if (_bfd_discard_section_stabs (abfd, o,
7922 elf_section_data (o)->stab_info,
7923 elf_reloc_symbol_deleted_p,
7926 if (! info->keep_memory)
7931 if (bed->elf_backend_discard_info)
7933 if (bed->elf_backend_discard_info (abfd, &cookie, info))
7944 elf_section_ignore_discarded_relocs (sec)
7947 if (strcmp (sec->name, ".stab") == 0)
7949 else if ((get_elf_backend_data (sec->owner)
7950 ->elf_backend_ignore_discarded_relocs != NULL)
7951 && (*get_elf_backend_data (sec->owner)
7952 ->elf_backend_ignore_discarded_relocs) (sec))