2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 bfd_boolean is_global_data_symbol_definition
34 PARAMS ((bfd
*, Elf_Internal_Sym
*));
35 static bfd_boolean elf_link_is_defined_archive_symbol
36 PARAMS ((bfd
*, carsym
*));
37 static bfd_boolean elf_link_add_object_symbols
38 PARAMS ((bfd
*, struct bfd_link_info
*));
39 static bfd_boolean elf_link_add_archive_symbols
40 PARAMS ((bfd
*, struct bfd_link_info
*));
41 static bfd_boolean elf_merge_symbol
42 PARAMS ((bfd
*, struct bfd_link_info
*, const char *,
43 Elf_Internal_Sym
*, asection
**, bfd_vma
*,
44 struct elf_link_hash_entry
**, bfd_boolean
*, bfd_boolean
*,
45 bfd_boolean
*, bfd_boolean
*, bfd_boolean
));
46 static bfd_boolean elf_add_default_symbol
47 PARAMS ((bfd
*, struct bfd_link_info
*, struct elf_link_hash_entry
*,
48 const char *, Elf_Internal_Sym
*, asection
**, bfd_vma
*,
49 bfd_boolean
*, bfd_boolean
, bfd_boolean
));
50 static bfd_boolean elf_export_symbol
51 PARAMS ((struct elf_link_hash_entry
*, PTR
));
52 static bfd_boolean elf_finalize_dynstr
53 PARAMS ((bfd
*, struct bfd_link_info
*));
54 static bfd_boolean elf_fix_symbol_flags
55 PARAMS ((struct elf_link_hash_entry
*, struct elf_info_failed
*));
56 static bfd_boolean elf_adjust_dynamic_symbol
57 PARAMS ((struct elf_link_hash_entry
*, PTR
));
58 static bfd_boolean elf_link_find_version_dependencies
59 PARAMS ((struct elf_link_hash_entry
*, PTR
));
60 static bfd_boolean elf_link_assign_sym_version
61 PARAMS ((struct elf_link_hash_entry
*, PTR
));
62 static bfd_boolean elf_collect_hash_codes
63 PARAMS ((struct elf_link_hash_entry
*, PTR
));
64 static bfd_boolean elf_link_read_relocs_from_section
65 PARAMS ((bfd
*, Elf_Internal_Shdr
*, PTR
, Elf_Internal_Rela
*));
66 static size_t compute_bucket_count
67 PARAMS ((struct bfd_link_info
*));
68 static bfd_boolean elf_link_output_relocs
69 PARAMS ((bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*));
70 static bfd_boolean elf_link_size_reloc_section
71 PARAMS ((bfd
*, Elf_Internal_Shdr
*, asection
*));
72 static void elf_link_adjust_relocs
73 PARAMS ((bfd
*, Elf_Internal_Shdr
*, unsigned int,
74 struct elf_link_hash_entry
**));
75 static int elf_link_sort_cmp1
76 PARAMS ((const void *, const void *));
77 static int elf_link_sort_cmp2
78 PARAMS ((const void *, const void *));
79 static size_t elf_link_sort_relocs
80 PARAMS ((bfd
*, struct bfd_link_info
*, asection
**));
81 static bfd_boolean elf_section_ignore_discarded_relocs
82 PARAMS ((asection
*));
84 /* Given an ELF BFD, add symbols to the global hash table as
88 elf_bfd_link_add_symbols (abfd
, info
)
90 struct bfd_link_info
*info
;
92 switch (bfd_get_format (abfd
))
95 return elf_link_add_object_symbols (abfd
, info
);
97 return elf_link_add_archive_symbols (abfd
, info
);
99 bfd_set_error (bfd_error_wrong_format
);
104 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
106 is_global_data_symbol_definition (abfd
, sym
)
107 bfd
* abfd ATTRIBUTE_UNUSED
;
108 Elf_Internal_Sym
* sym
;
110 /* Local symbols do not count, but target specific ones might. */
111 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
112 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
115 /* Function symbols do not count. */
116 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
119 /* If the section is undefined, then so is the symbol. */
120 if (sym
->st_shndx
== SHN_UNDEF
)
123 /* If the symbol is defined in the common section, then
124 it is a common definition and so does not count. */
125 if (sym
->st_shndx
== SHN_COMMON
)
128 /* If the symbol is in a target specific section then we
129 must rely upon the backend to tell us what it is. */
130 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
131 /* FIXME - this function is not coded yet:
133 return _bfd_is_global_symbol_definition (abfd, sym);
135 Instead for now assume that the definition is not global,
136 Even if this is wrong, at least the linker will behave
137 in the same way that it used to do. */
143 /* Search the symbol table of the archive element of the archive ABFD
144 whose archive map contains a mention of SYMDEF, and determine if
145 the symbol is defined in this element. */
147 elf_link_is_defined_archive_symbol (abfd
, symdef
)
151 Elf_Internal_Shdr
* hdr
;
152 bfd_size_type symcount
;
153 bfd_size_type extsymcount
;
154 bfd_size_type extsymoff
;
155 Elf_Internal_Sym
*isymbuf
;
156 Elf_Internal_Sym
*isym
;
157 Elf_Internal_Sym
*isymend
;
160 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
161 if (abfd
== (bfd
*) NULL
)
164 if (! bfd_check_format (abfd
, bfd_object
))
167 /* If we have already included the element containing this symbol in the
168 link then we do not need to include it again. Just claim that any symbol
169 it contains is not a definition, so that our caller will not decide to
170 (re)include this element. */
171 if (abfd
->archive_pass
)
174 /* Select the appropriate symbol table. */
175 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
176 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
178 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
180 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
182 /* The sh_info field of the symtab header tells us where the
183 external symbols start. We don't care about the local symbols. */
184 if (elf_bad_symtab (abfd
))
186 extsymcount
= symcount
;
191 extsymcount
= symcount
- hdr
->sh_info
;
192 extsymoff
= hdr
->sh_info
;
195 if (extsymcount
== 0)
198 /* Read in the symbol table. */
199 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
204 /* Scan the symbol table looking for SYMDEF. */
206 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
210 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
212 if (name
== (const char *) NULL
)
215 if (strcmp (name
, symdef
->name
) == 0)
217 result
= is_global_data_symbol_definition (abfd
, isym
);
227 /* Add symbols from an ELF archive file to the linker hash table. We
228 don't use _bfd_generic_link_add_archive_symbols because of a
229 problem which arises on UnixWare. The UnixWare libc.so is an
230 archive which includes an entry libc.so.1 which defines a bunch of
231 symbols. The libc.so archive also includes a number of other
232 object files, which also define symbols, some of which are the same
233 as those defined in libc.so.1. Correct linking requires that we
234 consider each object file in turn, and include it if it defines any
235 symbols we need. _bfd_generic_link_add_archive_symbols does not do
236 this; it looks through the list of undefined symbols, and includes
237 any object file which defines them. When this algorithm is used on
238 UnixWare, it winds up pulling in libc.so.1 early and defining a
239 bunch of symbols. This means that some of the other objects in the
240 archive are not included in the link, which is incorrect since they
241 precede libc.so.1 in the archive.
243 Fortunately, ELF archive handling is simpler than that done by
244 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
245 oddities. In ELF, if we find a symbol in the archive map, and the
246 symbol is currently undefined, we know that we must pull in that
249 Unfortunately, we do have to make multiple passes over the symbol
250 table until nothing further is resolved. */
253 elf_link_add_archive_symbols (abfd
, info
)
255 struct bfd_link_info
*info
;
258 bfd_boolean
*defined
= NULL
;
259 bfd_boolean
*included
= NULL
;
264 if (! bfd_has_map (abfd
))
266 /* An empty archive is a special case. */
267 if (bfd_openr_next_archived_file (abfd
, (bfd
*) NULL
) == NULL
)
269 bfd_set_error (bfd_error_no_armap
);
273 /* Keep track of all symbols we know to be already defined, and all
274 files we know to be already included. This is to speed up the
275 second and subsequent passes. */
276 c
= bfd_ardata (abfd
)->symdef_count
;
280 amt
*= sizeof (bfd_boolean
);
281 defined
= (bfd_boolean
*) bfd_zmalloc (amt
);
282 included
= (bfd_boolean
*) bfd_zmalloc (amt
);
283 if (defined
== (bfd_boolean
*) NULL
|| included
== (bfd_boolean
*) NULL
)
286 symdefs
= bfd_ardata (abfd
)->symdefs
;
299 symdefend
= symdef
+ c
;
300 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
302 struct elf_link_hash_entry
*h
;
304 struct bfd_link_hash_entry
*undefs_tail
;
307 if (defined
[i
] || included
[i
])
309 if (symdef
->file_offset
== last
)
315 h
= elf_link_hash_lookup (elf_hash_table (info
), symdef
->name
,
316 FALSE
, FALSE
, FALSE
);
323 /* If this is a default version (the name contains @@),
324 look up the symbol again with only one `@' as well
325 as without the version. The effect is that references
326 to the symbol with and without the version will be
327 matched by the default symbol in the archive. */
329 p
= strchr (symdef
->name
, ELF_VER_CHR
);
330 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
333 /* First check with only one `@'. */
334 len
= strlen (symdef
->name
);
335 copy
= bfd_alloc (abfd
, (bfd_size_type
) len
);
338 first
= p
- symdef
->name
+ 1;
339 memcpy (copy
, symdef
->name
, first
);
340 memcpy (copy
+ first
, symdef
->name
+ first
+ 1, len
- first
);
342 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
343 FALSE
, FALSE
, FALSE
);
347 /* We also need to check references to the symbol
348 without the version. */
350 copy
[first
- 1] = '\0';
351 h
= elf_link_hash_lookup (elf_hash_table (info
),
352 copy
, FALSE
, FALSE
, FALSE
);
355 bfd_release (abfd
, copy
);
361 if (h
->root
.type
== bfd_link_hash_common
)
363 /* We currently have a common symbol. The archive map contains
364 a reference to this symbol, so we may want to include it. We
365 only want to include it however, if this archive element
366 contains a definition of the symbol, not just another common
369 Unfortunately some archivers (including GNU ar) will put
370 declarations of common symbols into their archive maps, as
371 well as real definitions, so we cannot just go by the archive
372 map alone. Instead we must read in the element's symbol
373 table and check that to see what kind of symbol definition
375 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
378 else if (h
->root
.type
!= bfd_link_hash_undefined
)
380 if (h
->root
.type
!= bfd_link_hash_undefweak
)
385 /* We need to include this archive member. */
386 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
387 if (element
== (bfd
*) NULL
)
390 if (! bfd_check_format (element
, bfd_object
))
393 /* Doublecheck that we have not included this object
394 already--it should be impossible, but there may be
395 something wrong with the archive. */
396 if (element
->archive_pass
!= 0)
398 bfd_set_error (bfd_error_bad_value
);
401 element
->archive_pass
= 1;
403 undefs_tail
= info
->hash
->undefs_tail
;
405 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
408 if (! elf_link_add_object_symbols (element
, info
))
411 /* If there are any new undefined symbols, we need to make
412 another pass through the archive in order to see whether
413 they can be defined. FIXME: This isn't perfect, because
414 common symbols wind up on undefs_tail and because an
415 undefined symbol which is defined later on in this pass
416 does not require another pass. This isn't a bug, but it
417 does make the code less efficient than it could be. */
418 if (undefs_tail
!= info
->hash
->undefs_tail
)
421 /* Look backward to mark all symbols from this object file
422 which we have already seen in this pass. */
426 included
[mark
] = TRUE
;
431 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
433 /* We mark subsequent symbols from this object file as we go
434 on through the loop. */
435 last
= symdef
->file_offset
;
446 if (defined
!= (bfd_boolean
*) NULL
)
448 if (included
!= (bfd_boolean
*) NULL
)
453 /* This function is called when we want to define a new symbol. It
454 handles the various cases which arise when we find a definition in
455 a dynamic object, or when there is already a definition in a
456 dynamic object. The new symbol is described by NAME, SYM, PSEC,
457 and PVALUE. We set SYM_HASH to the hash table entry. We set
458 OVERRIDE if the old symbol is overriding a new definition. We set
459 TYPE_CHANGE_OK if it is OK for the type to change. We set
460 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
461 change, we mean that we shouldn't warn if the type or size does
462 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
466 elf_merge_symbol (abfd
, info
, name
, sym
, psec
, pvalue
, sym_hash
, skip
,
467 override
, type_change_ok
, size_change_ok
, dt_needed
)
469 struct bfd_link_info
*info
;
471 Elf_Internal_Sym
*sym
;
474 struct elf_link_hash_entry
**sym_hash
;
476 bfd_boolean
*override
;
477 bfd_boolean
*type_change_ok
;
478 bfd_boolean
*size_change_ok
;
479 bfd_boolean dt_needed
;
482 struct elf_link_hash_entry
*h
;
483 struct elf_link_hash_entry
*flip
;
486 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
487 bfd_boolean newweakdef
, oldweakdef
, newweakundef
, oldweakundef
;
493 bind
= ELF_ST_BIND (sym
->st_info
);
495 if (! bfd_is_und_section (sec
))
496 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
498 h
= ((struct elf_link_hash_entry
*)
499 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
504 /* This code is for coping with dynamic objects, and is only useful
505 if we are doing an ELF link. */
506 if (info
->hash
->creator
!= abfd
->xvec
)
509 /* For merging, we only care about real symbols. */
511 while (h
->root
.type
== bfd_link_hash_indirect
512 || h
->root
.type
== bfd_link_hash_warning
)
513 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
515 /* If we just created the symbol, mark it as being an ELF symbol.
516 Other than that, there is nothing to do--there is no merge issue
517 with a newly defined symbol--so we just return. */
519 if (h
->root
.type
== bfd_link_hash_new
)
521 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
525 /* OLDBFD is a BFD associated with the existing symbol. */
527 switch (h
->root
.type
)
533 case bfd_link_hash_undefined
:
534 case bfd_link_hash_undefweak
:
535 oldbfd
= h
->root
.u
.undef
.abfd
;
538 case bfd_link_hash_defined
:
539 case bfd_link_hash_defweak
:
540 oldbfd
= h
->root
.u
.def
.section
->owner
;
543 case bfd_link_hash_common
:
544 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
548 /* In cases involving weak versioned symbols, we may wind up trying
549 to merge a symbol with itself. Catch that here, to avoid the
550 confusion that results if we try to override a symbol with
551 itself. The additional tests catch cases like
552 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
553 dynamic object, which we do want to handle here. */
555 && ((abfd
->flags
& DYNAMIC
) == 0
556 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0))
559 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
560 respectively, is from a dynamic object. */
562 if ((abfd
->flags
& DYNAMIC
) != 0)
568 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
573 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
574 indices used by MIPS ELF. */
575 switch (h
->root
.type
)
581 case bfd_link_hash_defined
:
582 case bfd_link_hash_defweak
:
583 hsec
= h
->root
.u
.def
.section
;
586 case bfd_link_hash_common
:
587 hsec
= h
->root
.u
.c
.p
->section
;
594 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
597 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
598 respectively, appear to be a definition rather than reference. */
600 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
605 if (h
->root
.type
== bfd_link_hash_undefined
606 || h
->root
.type
== bfd_link_hash_undefweak
607 || h
->root
.type
== bfd_link_hash_common
)
612 /* We need to rememeber if a symbol has a definition in a dynamic
613 object or is weak in all dynamic objects. Internal and hidden
614 visibility will make it unavailable to dynamic objects. */
615 if (newdyn
&& (h
->elf_link_hash_flags
& ELF_LINK_DYNAMIC_DEF
) == 0)
617 if (!bfd_is_und_section (sec
))
618 h
->elf_link_hash_flags
|= ELF_LINK_DYNAMIC_DEF
;
621 /* Check if this symbol is weak in all dynamic objects. If it
622 is the first time we see it in a dynamic object, we mark
623 if it is weak. Otherwise, we clear it. */
624 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) == 0)
626 if (bind
== STB_WEAK
)
627 h
->elf_link_hash_flags
|= ELF_LINK_DYNAMIC_WEAK
;
629 else if (bind
!= STB_WEAK
)
630 h
->elf_link_hash_flags
&= ~ELF_LINK_DYNAMIC_WEAK
;
634 /* If the old symbol has non-default visibility, we ignore the new
635 definition from a dynamic object. */
637 && ELF_ST_VISIBILITY (h
->other
)
638 && !bfd_is_und_section (sec
))
641 /* Make sure this symbol is dynamic. */
642 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
643 /* A protected symbol has external availability. Make sure it is
646 FIXME: Should we check type and size for protected symbol? */
647 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
648 return _bfd_elf_link_record_dynamic_symbol (info
, h
);
653 && ELF_ST_VISIBILITY (sym
->st_other
)
654 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
656 /* If the new symbol with non-default visibility comes from a
657 relocatable file and the old definition comes from a dynamic
658 object, we remove the old definition. */
659 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
661 h
->root
.type
= bfd_link_hash_new
;
662 h
->root
.u
.undef
.abfd
= NULL
;
663 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
665 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
666 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
668 /* FIXME: Should we check type and size for protected symbol? */
674 /* We need to treat weak definiton right, depending on if there is a
675 definition from a dynamic object. */
676 if (bind
== STB_WEAK
)
681 newweakundef
= FALSE
;
690 newweakdef
= newweakundef
= FALSE
;
692 /* If the new weak definition comes from a relocatable file and the
693 old symbol comes from a dynamic object, we treat the new one as
695 if (newweakdef
&& !newdyn
&& olddyn
)
698 if (h
->root
.type
== bfd_link_hash_defweak
)
701 oldweakundef
= FALSE
;
703 else if (h
->root
.type
== bfd_link_hash_undefweak
)
709 oldweakdef
= oldweakundef
= FALSE
;
711 /* If the old weak definition comes from a relocatable file and the
712 new symbol comes from a dynamic object, we treat the old one as
714 if (oldweakdef
&& !olddyn
&& newdyn
)
717 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
718 symbol, respectively, appears to be a common symbol in a dynamic
719 object. If a symbol appears in an uninitialized section, and is
720 not weak, and is not a function, then it may be a common symbol
721 which was resolved when the dynamic object was created. We want
722 to treat such symbols specially, because they raise special
723 considerations when setting the symbol size: if the symbol
724 appears as a common symbol in a regular object, and the size in
725 the regular object is larger, we must make sure that we use the
726 larger size. This problematic case can always be avoided in C,
727 but it must be handled correctly when using Fortran shared
730 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
731 likewise for OLDDYNCOMMON and OLDDEF.
733 Note that this test is just a heuristic, and that it is quite
734 possible to have an uninitialized symbol in a shared object which
735 is really a definition, rather than a common symbol. This could
736 lead to some minor confusion when the symbol really is a common
737 symbol in some regular object. However, I think it will be
742 && (sec
->flags
& SEC_ALLOC
) != 0
743 && (sec
->flags
& SEC_LOAD
) == 0
747 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
750 newdyncommon
= FALSE
;
754 && h
->root
.type
== bfd_link_hash_defined
755 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
756 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
757 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
759 && h
->type
!= STT_FUNC
)
762 olddyncommon
= FALSE
;
764 /* It's OK to change the type if either the existing symbol or the
765 new symbol is weak unless it comes from a DT_NEEDED entry of
766 a shared object, in which case, the DT_NEEDED entry may not be
767 required at the run time. */
769 if ((! dt_needed
&& oldweakdef
)
773 *type_change_ok
= TRUE
;
775 /* It's OK to change the size if either the existing symbol or the
776 new symbol is weak, or if the old symbol is undefined. */
779 || h
->root
.type
== bfd_link_hash_undefined
)
780 *size_change_ok
= TRUE
;
782 /* If both the old and the new symbols look like common symbols in a
783 dynamic object, set the size of the symbol to the larger of the
788 && sym
->st_size
!= h
->size
)
790 /* Since we think we have two common symbols, issue a multiple
791 common warning if desired. Note that we only warn if the
792 size is different. If the size is the same, we simply let
793 the old symbol override the new one as normally happens with
794 symbols defined in dynamic objects. */
796 if (! ((*info
->callbacks
->multiple_common
)
797 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
798 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
801 if (sym
->st_size
> h
->size
)
802 h
->size
= sym
->st_size
;
804 *size_change_ok
= TRUE
;
807 /* If we are looking at a dynamic object, and we have found a
808 definition, we need to see if the symbol was already defined by
809 some other object. If so, we want to use the existing
810 definition, and we do not want to report a multiple symbol
811 definition error; we do this by clobbering *PSEC to be
814 We treat a common symbol as a definition if the symbol in the
815 shared library is a function, since common symbols always
816 represent variables; this can cause confusion in principle, but
817 any such confusion would seem to indicate an erroneous program or
818 shared library. We also permit a common symbol in a regular
819 object to override a weak symbol in a shared object.
821 We prefer a non-weak definition in a shared library to a weak
822 definition in the executable unless it comes from a DT_NEEDED
823 entry of a shared object, in which case, the DT_NEEDED entry
824 may not be required at the run time. */
829 || (h
->root
.type
== bfd_link_hash_common
832 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)))
840 newdyncommon
= FALSE
;
842 *psec
= sec
= bfd_und_section_ptr
;
843 *size_change_ok
= TRUE
;
845 /* If we get here when the old symbol is a common symbol, then
846 we are explicitly letting it override a weak symbol or
847 function in a dynamic object, and we don't want to warn about
848 a type change. If the old symbol is a defined symbol, a type
849 change warning may still be appropriate. */
851 if (h
->root
.type
== bfd_link_hash_common
)
852 *type_change_ok
= TRUE
;
855 /* Handle the special case of an old common symbol merging with a
856 new symbol which looks like a common symbol in a shared object.
857 We change *PSEC and *PVALUE to make the new symbol look like a
858 common symbol, and let _bfd_generic_link_add_one_symbol will do
862 && h
->root
.type
== bfd_link_hash_common
)
866 newdyncommon
= FALSE
;
867 *pvalue
= sym
->st_size
;
868 *psec
= sec
= bfd_com_section_ptr
;
869 *size_change_ok
= TRUE
;
872 /* If the old symbol is from a dynamic object, and the new symbol is
873 a definition which is not from a dynamic object, then the new
874 symbol overrides the old symbol. Symbols from regular files
875 always take precedence over symbols from dynamic objects, even if
876 they are defined after the dynamic object in the link.
878 As above, we again permit a common symbol in a regular object to
879 override a definition in a shared object if the shared object
880 symbol is a function or is weak.
882 As above, we permit a non-weak definition in a shared object to
883 override a weak definition in a regular object. */
888 || (bfd_is_com_section (sec
)
889 && (oldweakdef
|| h
->type
== STT_FUNC
)))
892 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
893 && ((!newweakdef
&& !newweakundef
) || oldweakdef
))
895 /* Change the hash table entry to undefined, and let
896 _bfd_generic_link_add_one_symbol do the right thing with the
899 h
->root
.type
= bfd_link_hash_undefined
;
900 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
901 *size_change_ok
= TRUE
;
904 olddyncommon
= FALSE
;
906 /* We again permit a type change when a common symbol may be
907 overriding a function. */
909 if (bfd_is_com_section (sec
))
910 *type_change_ok
= TRUE
;
912 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
915 /* This union may have been set to be non-NULL when this symbol
916 was seen in a dynamic object. We must force the union to be
917 NULL, so that it is correct for a regular symbol. */
918 h
->verinfo
.vertree
= NULL
;
921 /* Handle the special case of a new common symbol merging with an
922 old symbol that looks like it might be a common symbol defined in
923 a shared object. Note that we have already handled the case in
924 which a new common symbol should simply override the definition
925 in the shared library. */
928 && bfd_is_com_section (sec
)
931 /* It would be best if we could set the hash table entry to a
932 common symbol, but we don't know what to use for the section
934 if (! ((*info
->callbacks
->multiple_common
)
935 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
936 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
939 /* If the predumed common symbol in the dynamic object is
940 larger, pretend that the new symbol has its size. */
942 if (h
->size
> *pvalue
)
945 /* FIXME: We no longer know the alignment required by the symbol
946 in the dynamic object, so we just wind up using the one from
947 the regular object. */
950 olddyncommon
= FALSE
;
952 h
->root
.type
= bfd_link_hash_undefined
;
953 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
955 *size_change_ok
= TRUE
;
956 *type_change_ok
= TRUE
;
958 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
961 h
->verinfo
.vertree
= NULL
;
966 /* Handle the case where we had a versioned symbol in a dynamic
967 library and now find a definition in a normal object. In this
968 case, we make the versioned symbol point to the normal one. */
969 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
970 flip
->root
.type
= h
->root
.type
;
971 h
->root
.type
= bfd_link_hash_indirect
;
972 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
973 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
974 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
975 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
977 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
978 flip
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
982 /* Handle the special case of a weak definition in a regular object
983 followed by a non-weak definition in a shared object. In this
984 case, we prefer the definition in the shared object unless it
985 comes from a DT_NEEDED entry of a shared object, in which case,
986 the DT_NEEDED entry may not be required at the run time. */
995 /* To make this work we have to frob the flags so that the rest
996 of the code does not think we are using the regular
998 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
999 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
1000 else if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
1001 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1002 h
->elf_link_hash_flags
&= ~ (ELF_LINK_HASH_DEF_REGULAR
1003 | ELF_LINK_HASH_DEF_DYNAMIC
);
1005 /* If H is the target of an indirection, we want the caller to
1006 use H rather than the indirect symbol. Otherwise if we are
1007 defining a new indirect symbol we will wind up attaching it
1008 to the entry we are overriding. */
1012 /* Handle the special case of a non-weak definition in a shared
1013 object followed by a weak definition in a regular object. In
1014 this case we prefer the definition in the shared object. To make
1015 this work we have to tell the caller to not treat the new symbol
1022 && (newweakdef
|| newweakundef
))
1028 /* This function is called to create an indirect symbol from the
1029 default for the symbol with the default version if needed. The
1030 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1031 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
1032 indicates if it comes from a DT_NEEDED entry of a shared object. */
1035 elf_add_default_symbol (abfd
, info
, h
, name
, sym
, psec
, value
,
1036 dynsym
, override
, dt_needed
)
1038 struct bfd_link_info
*info
;
1039 struct elf_link_hash_entry
*h
;
1041 Elf_Internal_Sym
*sym
;
1044 bfd_boolean
*dynsym
;
1045 bfd_boolean override
;
1046 bfd_boolean dt_needed
;
1048 bfd_boolean type_change_ok
;
1049 bfd_boolean size_change_ok
;
1052 struct elf_link_hash_entry
*hi
;
1053 struct bfd_link_hash_entry
*bh
;
1054 struct elf_backend_data
*bed
;
1055 bfd_boolean collect
;
1056 bfd_boolean dynamic
;
1058 size_t len
, shortlen
;
1061 /* If this symbol has a version, and it is the default version, we
1062 create an indirect symbol from the default name to the fully
1063 decorated name. This will cause external references which do not
1064 specify a version to be bound to this version of the symbol. */
1065 p
= strchr (name
, ELF_VER_CHR
);
1066 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1071 /* We are overridden by an old defition. We need to check if we
1072 need to create the indirect symbol from the default name. */
1073 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1075 BFD_ASSERT (hi
!= NULL
);
1078 while (hi
->root
.type
== bfd_link_hash_indirect
1079 || hi
->root
.type
== bfd_link_hash_warning
)
1081 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1087 bed
= get_elf_backend_data (abfd
);
1088 collect
= bed
->collect
;
1089 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1091 shortlen
= p
- name
;
1092 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1093 if (shortname
== NULL
)
1095 memcpy (shortname
, name
, shortlen
);
1096 shortname
[shortlen
] = '\0';
1098 /* We are going to create a new symbol. Merge it with any existing
1099 symbol with this name. For the purposes of the merge, act as
1100 though we were defining the symbol we just defined, although we
1101 actually going to define an indirect symbol. */
1102 type_change_ok
= FALSE
;
1103 size_change_ok
= FALSE
;
1105 if (! elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1106 &hi
, &skip
, &override
, &type_change_ok
,
1107 &size_change_ok
, dt_needed
))
1116 if (! (_bfd_generic_link_add_one_symbol
1117 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1118 (bfd_vma
) 0, name
, FALSE
, collect
, &bh
)))
1120 hi
= (struct elf_link_hash_entry
*) bh
;
1124 /* In this case the symbol named SHORTNAME is overriding the
1125 indirect symbol we want to add. We were planning on making
1126 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1127 is the name without a version. NAME is the fully versioned
1128 name, and it is the default version.
1130 Overriding means that we already saw a definition for the
1131 symbol SHORTNAME in a regular object, and it is overriding
1132 the symbol defined in the dynamic object.
1134 When this happens, we actually want to change NAME, the
1135 symbol we just added, to refer to SHORTNAME. This will cause
1136 references to NAME in the shared object to become references
1137 to SHORTNAME in the regular object. This is what we expect
1138 when we override a function in a shared object: that the
1139 references in the shared object will be mapped to the
1140 definition in the regular object. */
1142 while (hi
->root
.type
== bfd_link_hash_indirect
1143 || hi
->root
.type
== bfd_link_hash_warning
)
1144 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1146 h
->root
.type
= bfd_link_hash_indirect
;
1147 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1148 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1150 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1151 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1152 if (hi
->elf_link_hash_flags
1153 & (ELF_LINK_HASH_REF_REGULAR
1154 | ELF_LINK_HASH_DEF_REGULAR
))
1156 if (! _bfd_elf_link_record_dynamic_symbol (info
, hi
))
1161 /* Now set HI to H, so that the following code will set the
1162 other fields correctly. */
1166 /* If there is a duplicate definition somewhere, then HI may not
1167 point to an indirect symbol. We will have reported an error to
1168 the user in that case. */
1170 if (hi
->root
.type
== bfd_link_hash_indirect
)
1172 struct elf_link_hash_entry
*ht
;
1174 /* If the symbol became indirect, then we assume that we have
1175 not seen a definition before. */
1176 BFD_ASSERT ((hi
->elf_link_hash_flags
1177 & (ELF_LINK_HASH_DEF_DYNAMIC
1178 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1180 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1181 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1183 /* See if the new flags lead us to realize that the symbol must
1190 || ((hi
->elf_link_hash_flags
1191 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1196 if ((hi
->elf_link_hash_flags
1197 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1203 /* We also need to define an indirection from the nondefault version
1207 len
= strlen (name
);
1208 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1209 if (shortname
== NULL
)
1211 memcpy (shortname
, name
, shortlen
);
1212 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1214 /* Once again, merge with any existing symbol. */
1215 type_change_ok
= FALSE
;
1216 size_change_ok
= FALSE
;
1218 if (! elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1219 &hi
, &skip
, &override
, &type_change_ok
,
1220 &size_change_ok
, dt_needed
))
1228 /* Here SHORTNAME is a versioned name, so we don't expect to see
1229 the type of override we do in the case above unless it is
1230 overridden by a versioned definiton. */
1231 if (hi
->root
.type
!= bfd_link_hash_defined
1232 && hi
->root
.type
!= bfd_link_hash_defweak
)
1233 (*_bfd_error_handler
)
1234 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1235 bfd_archive_filename (abfd
), shortname
);
1240 if (! (_bfd_generic_link_add_one_symbol
1241 (info
, abfd
, shortname
, BSF_INDIRECT
,
1242 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, FALSE
, collect
, &bh
)))
1244 hi
= (struct elf_link_hash_entry
*) bh
;
1246 /* If there is a duplicate definition somewhere, then HI may not
1247 point to an indirect symbol. We will have reported an error
1248 to the user in that case. */
1250 if (hi
->root
.type
== bfd_link_hash_indirect
)
1252 /* If the symbol became indirect, then we assume that we have
1253 not seen a definition before. */
1254 BFD_ASSERT ((hi
->elf_link_hash_flags
1255 & (ELF_LINK_HASH_DEF_DYNAMIC
1256 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1258 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1260 /* See if the new flags lead us to realize that the symbol
1267 || ((hi
->elf_link_hash_flags
1268 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1273 if ((hi
->elf_link_hash_flags
1274 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1284 /* Add symbols from an ELF object file to the linker hash table. */
1287 elf_link_add_object_symbols (abfd
, info
)
1289 struct bfd_link_info
*info
;
1291 bfd_boolean (*add_symbol_hook
)
1292 PARAMS ((bfd
*, struct bfd_link_info
*, const Elf_Internal_Sym
*,
1293 const char **, flagword
*, asection
**, bfd_vma
*));
1294 bfd_boolean (*check_relocs
)
1295 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
1296 const Elf_Internal_Rela
*));
1297 bfd_boolean collect
;
1298 Elf_Internal_Shdr
*hdr
;
1299 bfd_size_type symcount
;
1300 bfd_size_type extsymcount
;
1301 bfd_size_type extsymoff
;
1302 struct elf_link_hash_entry
**sym_hash
;
1303 bfd_boolean dynamic
;
1304 Elf_External_Versym
*extversym
= NULL
;
1305 Elf_External_Versym
*ever
;
1306 struct elf_link_hash_entry
*weaks
;
1307 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
1308 bfd_size_type nondeflt_vers_cnt
= 0;
1309 Elf_Internal_Sym
*isymbuf
= NULL
;
1310 Elf_Internal_Sym
*isym
;
1311 Elf_Internal_Sym
*isymend
;
1312 struct elf_backend_data
*bed
;
1313 bfd_boolean dt_needed
;
1314 struct elf_link_hash_table
* hash_table
;
1317 hash_table
= elf_hash_table (info
);
1319 bed
= get_elf_backend_data (abfd
);
1320 add_symbol_hook
= bed
->elf_add_symbol_hook
;
1321 collect
= bed
->collect
;
1323 if ((abfd
->flags
& DYNAMIC
) == 0)
1329 /* You can't use -r against a dynamic object. Also, there's no
1330 hope of using a dynamic object which does not exactly match
1331 the format of the output file. */
1332 if (info
->relocateable
|| info
->hash
->creator
!= abfd
->xvec
)
1334 bfd_set_error (bfd_error_invalid_operation
);
1339 /* As a GNU extension, any input sections which are named
1340 .gnu.warning.SYMBOL are treated as warning symbols for the given
1341 symbol. This differs from .gnu.warning sections, which generate
1342 warnings when they are included in an output file. */
1347 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
1351 name
= bfd_get_section_name (abfd
, s
);
1352 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1357 name
+= sizeof ".gnu.warning." - 1;
1359 /* If this is a shared object, then look up the symbol
1360 in the hash table. If it is there, and it is already
1361 been defined, then we will not be using the entry
1362 from this shared object, so we don't need to warn.
1363 FIXME: If we see the definition in a regular object
1364 later on, we will warn, but we shouldn't. The only
1365 fix is to keep track of what warnings we are supposed
1366 to emit, and then handle them all at the end of the
1368 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
1370 struct elf_link_hash_entry
*h
;
1372 h
= elf_link_hash_lookup (hash_table
, name
,
1373 FALSE
, FALSE
, TRUE
);
1375 /* FIXME: What about bfd_link_hash_common? */
1377 && (h
->root
.type
== bfd_link_hash_defined
1378 || h
->root
.type
== bfd_link_hash_defweak
))
1380 /* We don't want to issue this warning. Clobber
1381 the section size so that the warning does not
1382 get copied into the output file. */
1388 sz
= bfd_section_size (abfd
, s
);
1389 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
1393 if (! bfd_get_section_contents (abfd
, s
, msg
, (file_ptr
) 0, sz
))
1398 if (! (_bfd_generic_link_add_one_symbol
1399 (info
, abfd
, name
, BSF_WARNING
, s
, (bfd_vma
) 0, msg
,
1400 FALSE
, collect
, (struct bfd_link_hash_entry
**) NULL
)))
1403 if (! info
->relocateable
)
1405 /* Clobber the section size so that the warning does
1406 not get copied into the output file. */
1416 /* If we are creating a shared library, create all the dynamic
1417 sections immediately. We need to attach them to something,
1418 so we attach them to this BFD, provided it is the right
1419 format. FIXME: If there are no input BFD's of the same
1420 format as the output, we can't make a shared library. */
1422 && is_elf_hash_table (info
)
1423 && ! hash_table
->dynamic_sections_created
1424 && abfd
->xvec
== info
->hash
->creator
)
1426 if (! elf_link_create_dynamic_sections (abfd
, info
))
1430 else if (! is_elf_hash_table (info
))
1435 bfd_boolean add_needed
;
1437 bfd_size_type oldsize
;
1438 bfd_size_type strindex
;
1439 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
1441 /* ld --just-symbols and dynamic objects don't mix very well.
1442 Test for --just-symbols by looking at info set up by
1443 _bfd_elf_link_just_syms. */
1444 if ((s
= abfd
->sections
) != NULL
1445 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
1448 /* Find the name to use in a DT_NEEDED entry that refers to this
1449 object. If the object has a DT_SONAME entry, we use it.
1450 Otherwise, if the generic linker stuck something in
1451 elf_dt_name, we use that. Otherwise, we just use the file
1452 name. If the generic linker put a null string into
1453 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1454 there is a DT_SONAME entry. */
1456 name
= bfd_get_filename (abfd
);
1457 if (elf_dt_name (abfd
) != NULL
)
1459 name
= elf_dt_name (abfd
);
1462 if (elf_dt_soname (abfd
) != NULL
)
1468 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1471 Elf_External_Dyn
*dynbuf
= NULL
;
1472 Elf_External_Dyn
*extdyn
;
1473 Elf_External_Dyn
*extdynend
;
1475 unsigned long shlink
;
1477 dynbuf
= (Elf_External_Dyn
*) bfd_malloc (s
->_raw_size
);
1481 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
1482 (file_ptr
) 0, s
->_raw_size
))
1483 goto error_free_dyn
;
1485 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1487 goto error_free_dyn
;
1488 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1491 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
1492 for (; extdyn
< extdynend
; extdyn
++)
1494 Elf_Internal_Dyn dyn
;
1496 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
1497 if (dyn
.d_tag
== DT_SONAME
)
1499 unsigned int tagv
= dyn
.d_un
.d_val
;
1500 name
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1502 goto error_free_dyn
;
1504 if (dyn
.d_tag
== DT_NEEDED
)
1506 struct bfd_link_needed_list
*n
, **pn
;
1508 unsigned int tagv
= dyn
.d_un
.d_val
;
1510 amt
= sizeof (struct bfd_link_needed_list
);
1511 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1512 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1513 if (n
== NULL
|| fnm
== NULL
)
1514 goto error_free_dyn
;
1515 amt
= strlen (fnm
) + 1;
1516 anm
= bfd_alloc (abfd
, amt
);
1518 goto error_free_dyn
;
1519 memcpy (anm
, fnm
, (size_t) amt
);
1523 for (pn
= & hash_table
->needed
;
1529 if (dyn
.d_tag
== DT_RUNPATH
)
1531 struct bfd_link_needed_list
*n
, **pn
;
1533 unsigned int tagv
= dyn
.d_un
.d_val
;
1535 amt
= sizeof (struct bfd_link_needed_list
);
1536 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1537 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1538 if (n
== NULL
|| fnm
== NULL
)
1539 goto error_free_dyn
;
1540 amt
= strlen (fnm
) + 1;
1541 anm
= bfd_alloc (abfd
, amt
);
1543 goto error_free_dyn
;
1544 memcpy (anm
, fnm
, (size_t) amt
);
1548 for (pn
= & runpath
;
1554 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1555 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
1557 struct bfd_link_needed_list
*n
, **pn
;
1559 unsigned int tagv
= dyn
.d_un
.d_val
;
1561 amt
= sizeof (struct bfd_link_needed_list
);
1562 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1563 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1564 if (n
== NULL
|| fnm
== NULL
)
1565 goto error_free_dyn
;
1566 amt
= strlen (fnm
) + 1;
1567 anm
= bfd_alloc (abfd
, amt
);
1574 memcpy (anm
, fnm
, (size_t) amt
);
1589 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
1590 frees all more recently bfd_alloc'd blocks as well. */
1596 struct bfd_link_needed_list
**pn
;
1597 for (pn
= & hash_table
->runpath
;
1604 /* We do not want to include any of the sections in a dynamic
1605 object in the output file. We hack by simply clobbering the
1606 list of sections in the BFD. This could be handled more
1607 cleanly by, say, a new section flag; the existing
1608 SEC_NEVER_LOAD flag is not the one we want, because that one
1609 still implies that the section takes up space in the output
1611 bfd_section_list_clear (abfd
);
1613 /* If this is the first dynamic object found in the link, create
1614 the special sections required for dynamic linking. */
1615 if (! hash_table
->dynamic_sections_created
)
1616 if (! elf_link_create_dynamic_sections (abfd
, info
))
1621 /* Add a DT_NEEDED entry for this dynamic object. */
1622 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
1623 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, name
, FALSE
);
1624 if (strindex
== (bfd_size_type
) -1)
1627 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
1630 Elf_External_Dyn
*dyncon
, *dynconend
;
1632 /* The hash table size did not change, which means that
1633 the dynamic object name was already entered. If we
1634 have already included this dynamic object in the
1635 link, just ignore it. There is no reason to include
1636 a particular dynamic object more than once. */
1637 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
1638 BFD_ASSERT (sdyn
!= NULL
);
1640 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1641 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1643 for (; dyncon
< dynconend
; dyncon
++)
1645 Elf_Internal_Dyn dyn
;
1647 elf_swap_dyn_in (hash_table
->dynobj
, dyncon
, & dyn
);
1648 if (dyn
.d_tag
== DT_NEEDED
1649 && dyn
.d_un
.d_val
== strindex
)
1651 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
1657 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
1661 /* Save the SONAME, if there is one, because sometimes the
1662 linker emulation code will need to know it. */
1664 name
= basename (bfd_get_filename (abfd
));
1665 elf_dt_name (abfd
) = name
;
1668 /* If this is a dynamic object, we always link against the .dynsym
1669 symbol table, not the .symtab symbol table. The dynamic linker
1670 will only see the .dynsym symbol table, so there is no reason to
1671 look at .symtab for a dynamic object. */
1673 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
1674 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1676 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1678 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
1680 /* The sh_info field of the symtab header tells us where the
1681 external symbols start. We don't care about the local symbols at
1683 if (elf_bad_symtab (abfd
))
1685 extsymcount
= symcount
;
1690 extsymcount
= symcount
- hdr
->sh_info
;
1691 extsymoff
= hdr
->sh_info
;
1695 if (extsymcount
!= 0)
1697 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
1699 if (isymbuf
== NULL
)
1702 /* We store a pointer to the hash table entry for each external
1704 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
1705 sym_hash
= (struct elf_link_hash_entry
**) bfd_alloc (abfd
, amt
);
1706 if (sym_hash
== NULL
)
1707 goto error_free_sym
;
1708 elf_sym_hashes (abfd
) = sym_hash
;
1713 /* Read in any version definitions. */
1714 if (! _bfd_elf_slurp_version_tables (abfd
))
1715 goto error_free_sym
;
1717 /* Read in the symbol versions, but don't bother to convert them
1718 to internal format. */
1719 if (elf_dynversym (abfd
) != 0)
1721 Elf_Internal_Shdr
*versymhdr
;
1723 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
1724 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
1725 if (extversym
== NULL
)
1726 goto error_free_sym
;
1727 amt
= versymhdr
->sh_size
;
1728 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
1729 || bfd_bread ((PTR
) extversym
, amt
, abfd
) != amt
)
1730 goto error_free_vers
;
1736 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
1737 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
1739 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
1746 struct elf_link_hash_entry
*h
;
1747 bfd_boolean definition
;
1748 bfd_boolean size_change_ok
;
1749 bfd_boolean type_change_ok
;
1750 bfd_boolean new_weakdef
;
1751 bfd_boolean override
;
1752 unsigned int old_alignment
;
1757 flags
= BSF_NO_FLAGS
;
1759 value
= isym
->st_value
;
1762 bind
= ELF_ST_BIND (isym
->st_info
);
1763 if (bind
== STB_LOCAL
)
1765 /* This should be impossible, since ELF requires that all
1766 global symbols follow all local symbols, and that sh_info
1767 point to the first global symbol. Unfortunatealy, Irix 5
1771 else if (bind
== STB_GLOBAL
)
1773 if (isym
->st_shndx
!= SHN_UNDEF
1774 && isym
->st_shndx
!= SHN_COMMON
)
1777 else if (bind
== STB_WEAK
)
1781 /* Leave it up to the processor backend. */
1784 if (isym
->st_shndx
== SHN_UNDEF
)
1785 sec
= bfd_und_section_ptr
;
1786 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
1788 sec
= section_from_elf_index (abfd
, isym
->st_shndx
);
1790 sec
= bfd_abs_section_ptr
;
1791 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
1794 else if (isym
->st_shndx
== SHN_ABS
)
1795 sec
= bfd_abs_section_ptr
;
1796 else if (isym
->st_shndx
== SHN_COMMON
)
1798 sec
= bfd_com_section_ptr
;
1799 /* What ELF calls the size we call the value. What ELF
1800 calls the value we call the alignment. */
1801 value
= isym
->st_size
;
1805 /* Leave it up to the processor backend. */
1808 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
1810 if (name
== (const char *) NULL
)
1811 goto error_free_vers
;
1813 if (isym
->st_shndx
== SHN_COMMON
1814 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
1816 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
1820 tcomm
= bfd_make_section (abfd
, ".tcommon");
1822 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
1824 | SEC_LINKER_CREATED
1825 | SEC_THREAD_LOCAL
)))
1826 goto error_free_vers
;
1830 else if (add_symbol_hook
)
1832 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
1834 goto error_free_vers
;
1836 /* The hook function sets the name to NULL if this symbol
1837 should be skipped for some reason. */
1838 if (name
== (const char *) NULL
)
1842 /* Sanity check that all possibilities were handled. */
1843 if (sec
== (asection
*) NULL
)
1845 bfd_set_error (bfd_error_bad_value
);
1846 goto error_free_vers
;
1849 if (bfd_is_und_section (sec
)
1850 || bfd_is_com_section (sec
))
1855 size_change_ok
= FALSE
;
1856 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
1860 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1862 Elf_Internal_Versym iver
;
1863 unsigned int vernum
= 0;
1868 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
1869 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
1871 /* If this is a hidden symbol, or if it is not version
1872 1, we append the version name to the symbol name.
1873 However, we do not modify a non-hidden absolute
1874 symbol, because it might be the version symbol
1875 itself. FIXME: What if it isn't? */
1876 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
1877 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
1880 size_t namelen
, verlen
, newlen
;
1883 if (isym
->st_shndx
!= SHN_UNDEF
)
1885 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
1887 (*_bfd_error_handler
)
1888 (_("%s: %s: invalid version %u (max %d)"),
1889 bfd_archive_filename (abfd
), name
, vernum
,
1890 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1891 bfd_set_error (bfd_error_bad_value
);
1892 goto error_free_vers
;
1894 else if (vernum
> 1)
1896 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1902 /* We cannot simply test for the number of
1903 entries in the VERNEED section since the
1904 numbers for the needed versions do not start
1906 Elf_Internal_Verneed
*t
;
1909 for (t
= elf_tdata (abfd
)->verref
;
1913 Elf_Internal_Vernaux
*a
;
1915 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1917 if (a
->vna_other
== vernum
)
1919 verstr
= a
->vna_nodename
;
1928 (*_bfd_error_handler
)
1929 (_("%s: %s: invalid needed version %d"),
1930 bfd_archive_filename (abfd
), name
, vernum
);
1931 bfd_set_error (bfd_error_bad_value
);
1932 goto error_free_vers
;
1936 namelen
= strlen (name
);
1937 verlen
= strlen (verstr
);
1938 newlen
= namelen
+ verlen
+ 2;
1939 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1940 && isym
->st_shndx
!= SHN_UNDEF
)
1943 newname
= (char *) bfd_alloc (abfd
, (bfd_size_type
) newlen
);
1944 if (newname
== NULL
)
1945 goto error_free_vers
;
1946 memcpy (newname
, name
, namelen
);
1947 p
= newname
+ namelen
;
1949 /* If this is a defined non-hidden version symbol,
1950 we add another @ to the name. This indicates the
1951 default version of the symbol. */
1952 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1953 && isym
->st_shndx
!= SHN_UNDEF
)
1955 memcpy (p
, verstr
, verlen
+ 1);
1961 if (! elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
1962 sym_hash
, &skip
, &override
,
1963 &type_change_ok
, &size_change_ok
,
1965 goto error_free_vers
;
1974 while (h
->root
.type
== bfd_link_hash_indirect
1975 || h
->root
.type
== bfd_link_hash_warning
)
1976 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1978 /* Remember the old alignment if this is a common symbol, so
1979 that we don't reduce the alignment later on. We can't
1980 check later, because _bfd_generic_link_add_one_symbol
1981 will set a default for the alignment which we want to
1982 override. We also remember the old bfd where the existing
1983 definition comes from. */
1984 switch (h
->root
.type
)
1989 case bfd_link_hash_defined
:
1990 case bfd_link_hash_defweak
:
1991 old_bfd
= h
->root
.u
.def
.section
->owner
;
1994 case bfd_link_hash_common
:
1995 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
1996 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
2000 if (elf_tdata (abfd
)->verdef
!= NULL
2004 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
2007 if (! (_bfd_generic_link_add_one_symbol
2008 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
2009 FALSE
, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
2010 goto error_free_vers
;
2013 while (h
->root
.type
== bfd_link_hash_indirect
2014 || h
->root
.type
== bfd_link_hash_warning
)
2015 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2018 new_weakdef
= FALSE
;
2021 && (flags
& BSF_WEAK
) != 0
2022 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
2023 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
2024 && h
->weakdef
== NULL
)
2026 /* Keep a list of all weak defined non function symbols from
2027 a dynamic object, using the weakdef field. Later in this
2028 function we will set the weakdef field to the correct
2029 value. We only put non-function symbols from dynamic
2030 objects on this list, because that happens to be the only
2031 time we need to know the normal symbol corresponding to a
2032 weak symbol, and the information is time consuming to
2033 figure out. If the weakdef field is not already NULL,
2034 then this symbol was already defined by some previous
2035 dynamic object, and we will be using that previous
2036 definition anyhow. */
2043 /* Set the alignment of a common symbol. */
2044 if (isym
->st_shndx
== SHN_COMMON
2045 && h
->root
.type
== bfd_link_hash_common
)
2049 align
= bfd_log2 (isym
->st_value
);
2050 if (align
> old_alignment
2051 /* Permit an alignment power of zero if an alignment of one
2052 is specified and no other alignments have been specified. */
2053 || (isym
->st_value
== 1 && old_alignment
== 0))
2054 h
->root
.u
.c
.p
->alignment_power
= align
;
2056 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
2059 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
2065 /* Check the alignment when a common symbol is involved. This
2066 can change when a common symbol is overriden by a normal
2067 definition or a common symbol is ignored due to the old
2068 normal definition. We need to make sure the maximum
2069 alignment is maintained. */
2070 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
2071 && h
->root
.type
!= bfd_link_hash_common
)
2073 unsigned int common_align
;
2074 unsigned int normal_align
;
2075 unsigned int symbol_align
;
2079 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
2080 if ((h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2082 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
2083 if (normal_align
> symbol_align
)
2084 normal_align
= symbol_align
;
2087 normal_align
= symbol_align
;
2091 common_align
= old_alignment
;
2092 common_bfd
= old_bfd
;
2097 common_align
= bfd_log2 (isym
->st_value
);
2099 normal_bfd
= old_bfd
;
2102 if (normal_align
< common_align
)
2103 (*_bfd_error_handler
)
2104 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
2107 bfd_archive_filename (normal_bfd
),
2109 bfd_archive_filename (common_bfd
));
2112 /* Remember the symbol size and type. */
2113 if (isym
->st_size
!= 0
2114 && (definition
|| h
->size
== 0))
2116 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
2117 (*_bfd_error_handler
)
2118 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
2119 name
, (unsigned long) h
->size
,
2120 bfd_archive_filename (old_bfd
),
2121 (unsigned long) isym
->st_size
,
2122 bfd_archive_filename (abfd
));
2124 h
->size
= isym
->st_size
;
2127 /* If this is a common symbol, then we always want H->SIZE
2128 to be the size of the common symbol. The code just above
2129 won't fix the size if a common symbol becomes larger. We
2130 don't warn about a size change here, because that is
2131 covered by --warn-common. */
2132 if (h
->root
.type
== bfd_link_hash_common
)
2133 h
->size
= h
->root
.u
.c
.size
;
2135 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
2136 && (definition
|| h
->type
== STT_NOTYPE
))
2138 if (h
->type
!= STT_NOTYPE
2139 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
2140 && ! type_change_ok
)
2141 (*_bfd_error_handler
)
2142 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
2143 name
, h
->type
, ELF_ST_TYPE (isym
->st_info
),
2144 bfd_archive_filename (abfd
));
2146 h
->type
= ELF_ST_TYPE (isym
->st_info
);
2149 /* If st_other has a processor-specific meaning, specific
2150 code might be needed here. We never merge the visibility
2151 attribute with the one from a dynamic object. */
2152 if (isym
->st_other
!= 0 && !dynamic
)
2154 unsigned char hvis
, symvis
, other
, nvis
;
2156 /* Take the balance of OTHER from the definition. */
2157 other
= (definition
? isym
->st_other
: h
->other
);
2158 other
&= ~ ELF_ST_VISIBILITY (-1);
2160 /* Combine visibilities, using the most constraining one. */
2161 hvis
= ELF_ST_VISIBILITY (h
->other
);
2162 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
2168 nvis
= hvis
< symvis
? hvis
: symvis
;
2170 h
->other
= other
| nvis
;
2173 /* Set a flag in the hash table entry indicating the type of
2174 reference or definition we just found. Keep a count of
2175 the number of dynamic symbols we find. A dynamic symbol
2176 is one which is referenced or defined by both a regular
2177 object and a shared object. */
2178 old_flags
= h
->elf_link_hash_flags
;
2184 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
2185 if (bind
!= STB_WEAK
)
2186 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
2189 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
2191 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2192 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
2198 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
2200 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
2201 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
2202 | ELF_LINK_HASH_REF_REGULAR
)) != 0
2203 || (h
->weakdef
!= NULL
2205 && h
->weakdef
->dynindx
!= -1))
2209 h
->elf_link_hash_flags
|= new_flag
;
2211 /* Check to see if we need to add an indirect symbol for
2212 the default name. */
2213 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
2214 if (! elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
2215 &sec
, &value
, &dynsym
,
2216 override
, dt_needed
))
2217 goto error_free_vers
;
2219 if (definition
&& !dynamic
)
2221 char *p
= strchr (name
, ELF_VER_CHR
);
2222 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
2224 /* Queue non-default versions so that .symver x, x@FOO
2225 aliases can be checked. */
2226 if (! nondeflt_vers
)
2228 amt
= (isymend
- isym
+ 1)
2229 * sizeof (struct elf_link_hash_entry
*);
2230 nondeflt_vers
= bfd_malloc (amt
);
2232 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
2236 if (dynsym
&& h
->dynindx
== -1)
2238 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2239 goto error_free_vers
;
2240 if (h
->weakdef
!= NULL
2242 && h
->weakdef
->dynindx
== -1)
2244 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2245 goto error_free_vers
;
2248 else if (dynsym
&& h
->dynindx
!= -1)
2249 /* If the symbol already has a dynamic index, but
2250 visibility says it should not be visible, turn it into
2252 switch (ELF_ST_VISIBILITY (h
->other
))
2256 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2260 if (dt_needed
&& definition
2261 && (h
->elf_link_hash_flags
2262 & ELF_LINK_HASH_REF_REGULAR
) != 0)
2264 bfd_size_type oldsize
;
2265 bfd_size_type strindex
;
2267 if (! is_elf_hash_table (info
))
2268 goto error_free_vers
;
2270 /* The symbol from a DT_NEEDED object is referenced from
2271 the regular object to create a dynamic executable. We
2272 have to make sure there is a DT_NEEDED entry for it. */
2275 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2276 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
,
2277 elf_dt_soname (abfd
), FALSE
);
2278 if (strindex
== (bfd_size_type
) -1)
2279 goto error_free_vers
;
2281 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2284 Elf_External_Dyn
*dyncon
, *dynconend
;
2286 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
,
2288 BFD_ASSERT (sdyn
!= NULL
);
2290 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
2291 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
2293 for (; dyncon
< dynconend
; dyncon
++)
2295 Elf_Internal_Dyn dyn
;
2297 elf_swap_dyn_in (hash_table
->dynobj
,
2299 BFD_ASSERT (dyn
.d_tag
!= DT_NEEDED
||
2300 dyn
.d_un
.d_val
!= strindex
);
2304 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
2305 goto error_free_vers
;
2310 /* Now that all the symbols from this input file are created, handle
2311 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
2312 if (nondeflt_vers
!= NULL
)
2314 bfd_size_type cnt
, symidx
;
2316 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
2318 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
2319 char *shortname
, *p
;
2321 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2323 || (h
->root
.type
!= bfd_link_hash_defined
2324 && h
->root
.type
!= bfd_link_hash_defweak
))
2327 amt
= p
- h
->root
.root
.string
;
2328 shortname
= bfd_malloc (amt
+ 1);
2329 memcpy (shortname
, h
->root
.root
.string
, amt
);
2330 shortname
[amt
] = '\0';
2332 hi
= (struct elf_link_hash_entry
*)
2333 bfd_link_hash_lookup (info
->hash
, shortname
,
2334 FALSE
, FALSE
, FALSE
);
2336 && hi
->root
.type
== h
->root
.type
2337 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
2338 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
2340 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
2341 hi
->root
.type
= bfd_link_hash_indirect
;
2342 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
2343 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
2344 sym_hash
= elf_sym_hashes (abfd
);
2346 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
2347 if (sym_hash
[symidx
] == hi
)
2349 sym_hash
[symidx
] = h
;
2355 free (nondeflt_vers
);
2356 nondeflt_vers
= NULL
;
2359 if (extversym
!= NULL
)
2365 if (isymbuf
!= NULL
)
2369 /* Now set the weakdefs field correctly for all the weak defined
2370 symbols we found. The only way to do this is to search all the
2371 symbols. Since we only need the information for non functions in
2372 dynamic objects, that's the only time we actually put anything on
2373 the list WEAKS. We need this information so that if a regular
2374 object refers to a symbol defined weakly in a dynamic object, the
2375 real symbol in the dynamic object is also put in the dynamic
2376 symbols; we also must arrange for both symbols to point to the
2377 same memory location. We could handle the general case of symbol
2378 aliasing, but a general symbol alias can only be generated in
2379 assembler code, handling it correctly would be very time
2380 consuming, and other ELF linkers don't handle general aliasing
2382 while (weaks
!= NULL
)
2384 struct elf_link_hash_entry
*hlook
;
2387 struct elf_link_hash_entry
**hpp
;
2388 struct elf_link_hash_entry
**hppend
;
2391 weaks
= hlook
->weakdef
;
2392 hlook
->weakdef
= NULL
;
2394 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
2395 || hlook
->root
.type
== bfd_link_hash_defweak
2396 || hlook
->root
.type
== bfd_link_hash_common
2397 || hlook
->root
.type
== bfd_link_hash_indirect
);
2398 slook
= hlook
->root
.u
.def
.section
;
2399 vlook
= hlook
->root
.u
.def
.value
;
2401 hpp
= elf_sym_hashes (abfd
);
2402 hppend
= hpp
+ extsymcount
;
2403 for (; hpp
< hppend
; hpp
++)
2405 struct elf_link_hash_entry
*h
;
2408 if (h
!= NULL
&& h
!= hlook
2409 && h
->root
.type
== bfd_link_hash_defined
2410 && h
->root
.u
.def
.section
== slook
2411 && h
->root
.u
.def
.value
== vlook
)
2415 /* If the weak definition is in the list of dynamic
2416 symbols, make sure the real definition is put there
2418 if (hlook
->dynindx
!= -1
2419 && h
->dynindx
== -1)
2421 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2425 /* If the real definition is in the list of dynamic
2426 symbols, make sure the weak definition is put there
2427 as well. If we don't do this, then the dynamic
2428 loader might not merge the entries for the real
2429 definition and the weak definition. */
2430 if (h
->dynindx
!= -1
2431 && hlook
->dynindx
== -1)
2433 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
2441 /* If this object is the same format as the output object, and it is
2442 not a shared library, then let the backend look through the
2445 This is required to build global offset table entries and to
2446 arrange for dynamic relocs. It is not required for the
2447 particular common case of linking non PIC code, even when linking
2448 against shared libraries, but unfortunately there is no way of
2449 knowing whether an object file has been compiled PIC or not.
2450 Looking through the relocs is not particularly time consuming.
2451 The problem is that we must either (1) keep the relocs in memory,
2452 which causes the linker to require additional runtime memory or
2453 (2) read the relocs twice from the input file, which wastes time.
2454 This would be a good case for using mmap.
2456 I have no idea how to handle linking PIC code into a file of a
2457 different format. It probably can't be done. */
2458 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
2460 && abfd
->xvec
== info
->hash
->creator
2461 && check_relocs
!= NULL
)
2465 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2467 Elf_Internal_Rela
*internal_relocs
;
2470 if ((o
->flags
& SEC_RELOC
) == 0
2471 || o
->reloc_count
== 0
2472 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
2473 && (o
->flags
& SEC_DEBUGGING
) != 0)
2474 || bfd_is_abs_section (o
->output_section
))
2477 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
2478 (abfd
, o
, (PTR
) NULL
,
2479 (Elf_Internal_Rela
*) NULL
,
2480 info
->keep_memory
));
2481 if (internal_relocs
== NULL
)
2484 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
2486 if (elf_section_data (o
)->relocs
!= internal_relocs
)
2487 free (internal_relocs
);
2494 /* If this is a non-traditional link, try to optimize the handling
2495 of the .stab/.stabstr sections. */
2497 && ! info
->traditional_format
2498 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
2499 && is_elf_hash_table (info
)
2500 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
2502 asection
*stab
, *stabstr
;
2504 stab
= bfd_get_section_by_name (abfd
, ".stab");
2506 && (stab
->flags
& SEC_MERGE
) == 0
2507 && !bfd_is_abs_section (stab
->output_section
))
2509 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
2511 if (stabstr
!= NULL
)
2513 struct bfd_elf_section_data
*secdata
;
2515 secdata
= elf_section_data (stab
);
2516 if (! _bfd_link_section_stabs (abfd
,
2517 & hash_table
->stab_info
,
2519 &secdata
->sec_info
))
2521 if (secdata
->sec_info
)
2522 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
2527 if (! info
->relocateable
&& ! dynamic
2528 && is_elf_hash_table (info
))
2532 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2533 if ((s
->flags
& SEC_MERGE
) != 0
2534 && !bfd_is_abs_section (s
->output_section
))
2536 struct bfd_elf_section_data
*secdata
;
2538 secdata
= elf_section_data (s
);
2539 if (! _bfd_merge_section (abfd
,
2540 & hash_table
->merge_info
,
2541 s
, &secdata
->sec_info
))
2543 else if (secdata
->sec_info
)
2544 s
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
2548 if (is_elf_hash_table (info
))
2550 /* Add this bfd to the loaded list. */
2551 struct elf_link_loaded_list
*n
;
2553 n
= ((struct elf_link_loaded_list
*)
2554 bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
)));
2558 n
->next
= hash_table
->loaded
;
2559 hash_table
->loaded
= n
;
2565 if (nondeflt_vers
!= NULL
)
2566 free (nondeflt_vers
);
2567 if (extversym
!= NULL
)
2570 if (isymbuf
!= NULL
)
2576 /* Create some sections which will be filled in with dynamic linking
2577 information. ABFD is an input file which requires dynamic sections
2578 to be created. The dynamic sections take up virtual memory space
2579 when the final executable is run, so we need to create them before
2580 addresses are assigned to the output sections. We work out the
2581 actual contents and size of these sections later. */
2584 elf_link_create_dynamic_sections (abfd
, info
)
2586 struct bfd_link_info
*info
;
2589 register asection
*s
;
2590 struct elf_link_hash_entry
*h
;
2591 struct bfd_link_hash_entry
*bh
;
2592 struct elf_backend_data
*bed
;
2594 if (! is_elf_hash_table (info
))
2597 if (elf_hash_table (info
)->dynamic_sections_created
)
2600 /* Make sure that all dynamic sections use the same input BFD. */
2601 if (elf_hash_table (info
)->dynobj
== NULL
)
2602 elf_hash_table (info
)->dynobj
= abfd
;
2604 abfd
= elf_hash_table (info
)->dynobj
;
2606 /* Note that we set the SEC_IN_MEMORY flag for all of these
2608 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
2609 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
2611 /* A dynamically linked executable has a .interp section, but a
2612 shared library does not. */
2615 s
= bfd_make_section (abfd
, ".interp");
2617 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2621 if (! info
->traditional_format
2622 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
2624 s
= bfd_make_section (abfd
, ".eh_frame_hdr");
2626 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2627 || ! bfd_set_section_alignment (abfd
, s
, 2))
2629 elf_hash_table (info
)->eh_info
.hdr_sec
= s
;
2632 /* Create sections to hold version informations. These are removed
2633 if they are not needed. */
2634 s
= bfd_make_section (abfd
, ".gnu.version_d");
2636 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2637 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2640 s
= bfd_make_section (abfd
, ".gnu.version");
2642 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2643 || ! bfd_set_section_alignment (abfd
, s
, 1))
2646 s
= bfd_make_section (abfd
, ".gnu.version_r");
2648 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2649 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2652 s
= bfd_make_section (abfd
, ".dynsym");
2654 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2655 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2658 s
= bfd_make_section (abfd
, ".dynstr");
2660 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2663 /* Create a strtab to hold the dynamic symbol names. */
2664 if (elf_hash_table (info
)->dynstr
== NULL
)
2666 elf_hash_table (info
)->dynstr
= _bfd_elf_strtab_init ();
2667 if (elf_hash_table (info
)->dynstr
== NULL
)
2671 s
= bfd_make_section (abfd
, ".dynamic");
2673 || ! bfd_set_section_flags (abfd
, s
, flags
)
2674 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2677 /* The special symbol _DYNAMIC is always set to the start of the
2678 .dynamic section. This call occurs before we have processed the
2679 symbols for any dynamic object, so we don't have to worry about
2680 overriding a dynamic definition. We could set _DYNAMIC in a
2681 linker script, but we only want to define it if we are, in fact,
2682 creating a .dynamic section. We don't want to define it if there
2683 is no .dynamic section, since on some ELF platforms the start up
2684 code examines it to decide how to initialize the process. */
2686 if (! (_bfd_generic_link_add_one_symbol
2687 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
2688 (const char *) 0, FALSE
, get_elf_backend_data (abfd
)->collect
, &bh
)))
2690 h
= (struct elf_link_hash_entry
*) bh
;
2691 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2692 h
->type
= STT_OBJECT
;
2695 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2698 bed
= get_elf_backend_data (abfd
);
2700 s
= bfd_make_section (abfd
, ".hash");
2702 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2703 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2705 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
2707 /* Let the backend create the rest of the sections. This lets the
2708 backend set the right flags. The backend will normally create
2709 the .got and .plt sections. */
2710 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
2713 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
2718 /* Add an entry to the .dynamic table. */
2721 elf_add_dynamic_entry (info
, tag
, val
)
2722 struct bfd_link_info
*info
;
2726 Elf_Internal_Dyn dyn
;
2729 bfd_size_type newsize
;
2730 bfd_byte
*newcontents
;
2732 if (! is_elf_hash_table (info
))
2735 dynobj
= elf_hash_table (info
)->dynobj
;
2737 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
2738 BFD_ASSERT (s
!= NULL
);
2740 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
2741 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
2742 if (newcontents
== NULL
)
2746 dyn
.d_un
.d_val
= val
;
2747 elf_swap_dyn_out (dynobj
, &dyn
,
2748 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
2750 s
->_raw_size
= newsize
;
2751 s
->contents
= newcontents
;
2756 /* Read and swap the relocs from the section indicated by SHDR. This
2757 may be either a REL or a RELA section. The relocations are
2758 translated into RELA relocations and stored in INTERNAL_RELOCS,
2759 which should have already been allocated to contain enough space.
2760 The EXTERNAL_RELOCS are a buffer where the external form of the
2761 relocations should be stored.
2763 Returns FALSE if something goes wrong. */
2766 elf_link_read_relocs_from_section (abfd
, shdr
, external_relocs
,
2769 Elf_Internal_Shdr
*shdr
;
2770 PTR external_relocs
;
2771 Elf_Internal_Rela
*internal_relocs
;
2773 struct elf_backend_data
*bed
;
2774 void (*swap_in
) PARAMS ((bfd
*, const bfd_byte
*, Elf_Internal_Rela
*));
2775 const bfd_byte
*erela
;
2776 const bfd_byte
*erelaend
;
2777 Elf_Internal_Rela
*irela
;
2779 /* If there aren't any relocations, that's OK. */
2783 /* Position ourselves at the start of the section. */
2784 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2787 /* Read the relocations. */
2788 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2791 bed
= get_elf_backend_data (abfd
);
2793 /* Convert the external relocations to the internal format. */
2794 if (shdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2795 swap_in
= bed
->s
->swap_reloc_in
;
2796 else if (shdr
->sh_entsize
== sizeof (Elf_External_Rela
))
2797 swap_in
= bed
->s
->swap_reloca_in
;
2800 bfd_set_error (bfd_error_wrong_format
);
2804 erela
= external_relocs
;
2805 erelaend
= erela
+ NUM_SHDR_ENTRIES (shdr
) * shdr
->sh_entsize
;
2806 irela
= internal_relocs
;
2807 while (erela
< erelaend
)
2809 (*swap_in
) (abfd
, erela
, irela
);
2810 irela
+= bed
->s
->int_rels_per_ext_rel
;
2811 erela
+= shdr
->sh_entsize
;
2817 /* Read and swap the relocs for a section O. They may have been
2818 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2819 not NULL, they are used as buffers to read into. They are known to
2820 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2821 the return value is allocated using either malloc or bfd_alloc,
2822 according to the KEEP_MEMORY argument. If O has two relocation
2823 sections (both REL and RELA relocations), then the REL_HDR
2824 relocations will appear first in INTERNAL_RELOCS, followed by the
2825 REL_HDR2 relocations. */
2828 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
2832 PTR external_relocs
;
2833 Elf_Internal_Rela
*internal_relocs
;
2834 bfd_boolean keep_memory
;
2836 Elf_Internal_Shdr
*rel_hdr
;
2838 Elf_Internal_Rela
*alloc2
= NULL
;
2839 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2841 if (elf_section_data (o
)->relocs
!= NULL
)
2842 return elf_section_data (o
)->relocs
;
2844 if (o
->reloc_count
== 0)
2847 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2849 if (internal_relocs
== NULL
)
2853 size
= o
->reloc_count
;
2854 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2856 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2858 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2859 if (internal_relocs
== NULL
)
2863 if (external_relocs
== NULL
)
2865 bfd_size_type size
= rel_hdr
->sh_size
;
2867 if (elf_section_data (o
)->rel_hdr2
)
2868 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2869 alloc1
= (PTR
) bfd_malloc (size
);
2872 external_relocs
= alloc1
;
2875 if (!elf_link_read_relocs_from_section (abfd
, rel_hdr
,
2879 if (!elf_link_read_relocs_from_section
2881 elf_section_data (o
)->rel_hdr2
,
2882 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2883 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2884 * bed
->s
->int_rels_per_ext_rel
)))
2887 /* Cache the results for next time, if we can. */
2889 elf_section_data (o
)->relocs
= internal_relocs
;
2894 /* Don't free alloc2, since if it was allocated we are passing it
2895 back (under the name of internal_relocs). */
2897 return internal_relocs
;
2907 /* Record an assignment to a symbol made by a linker script. We need
2908 this in case some dynamic object refers to this symbol. */
2911 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
2912 bfd
*output_bfd ATTRIBUTE_UNUSED
;
2913 struct bfd_link_info
*info
;
2915 bfd_boolean provide
;
2917 struct elf_link_hash_entry
*h
;
2919 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2922 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, TRUE
, FALSE
);
2926 if (h
->root
.type
== bfd_link_hash_new
)
2927 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_ELF
;
2929 /* If this symbol is being provided by the linker script, and it is
2930 currently defined by a dynamic object, but not by a regular
2931 object, then mark it as undefined so that the generic linker will
2932 force the correct value. */
2934 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2935 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2936 h
->root
.type
= bfd_link_hash_undefined
;
2938 /* If this symbol is not being provided by the linker script, and it is
2939 currently defined by a dynamic object, but not by a regular object,
2940 then clear out any version information because the symbol will not be
2941 associated with the dynamic object any more. */
2943 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2944 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2945 h
->verinfo
.verdef
= NULL
;
2947 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2949 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2950 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
2952 && h
->dynindx
== -1)
2954 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2957 /* If this is a weak defined symbol, and we know a corresponding
2958 real symbol from the same dynamic object, make sure the real
2959 symbol is also made into a dynamic symbol. */
2960 if (h
->weakdef
!= NULL
2961 && h
->weakdef
->dynindx
== -1)
2963 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2971 /* This structure is used to pass information to
2972 elf_link_assign_sym_version. */
2974 struct elf_assign_sym_version_info
2978 /* General link information. */
2979 struct bfd_link_info
*info
;
2981 struct bfd_elf_version_tree
*verdefs
;
2982 /* Whether we had a failure. */
2986 /* This structure is used to pass information to
2987 elf_link_find_version_dependencies. */
2989 struct elf_find_verdep_info
2993 /* General link information. */
2994 struct bfd_link_info
*info
;
2995 /* The number of dependencies. */
2997 /* Whether we had a failure. */
3001 /* Array used to determine the number of hash table buckets to use
3002 based on the number of symbols there are. If there are fewer than
3003 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
3004 fewer than 37 we use 17 buckets, and so forth. We never use more
3005 than 32771 buckets. */
3007 static const size_t elf_buckets
[] =
3009 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
3013 /* Compute bucket count for hashing table. We do not use a static set
3014 of possible tables sizes anymore. Instead we determine for all
3015 possible reasonable sizes of the table the outcome (i.e., the
3016 number of collisions etc) and choose the best solution. The
3017 weighting functions are not too simple to allow the table to grow
3018 without bounds. Instead one of the weighting factors is the size.
3019 Therefore the result is always a good payoff between few collisions
3020 (= short chain lengths) and table size. */
3022 compute_bucket_count (info
)
3023 struct bfd_link_info
*info
;
3025 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
3026 size_t best_size
= 0;
3027 unsigned long int *hashcodes
;
3028 unsigned long int *hashcodesp
;
3029 unsigned long int i
;
3032 /* Compute the hash values for all exported symbols. At the same
3033 time store the values in an array so that we could use them for
3036 amt
*= sizeof (unsigned long int);
3037 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
3038 if (hashcodes
== NULL
)
3040 hashcodesp
= hashcodes
;
3042 /* Put all hash values in HASHCODES. */
3043 elf_link_hash_traverse (elf_hash_table (info
),
3044 elf_collect_hash_codes
, &hashcodesp
);
3046 /* We have a problem here. The following code to optimize the table
3047 size requires an integer type with more the 32 bits. If
3048 BFD_HOST_U_64_BIT is set we know about such a type. */
3049 #ifdef BFD_HOST_U_64_BIT
3052 unsigned long int nsyms
= hashcodesp
- hashcodes
;
3055 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
3056 unsigned long int *counts
;
3058 /* Possible optimization parameters: if we have NSYMS symbols we say
3059 that the hashing table must at least have NSYMS/4 and at most
3061 minsize
= nsyms
/ 4;
3064 best_size
= maxsize
= nsyms
* 2;
3066 /* Create array where we count the collisions in. We must use bfd_malloc
3067 since the size could be large. */
3069 amt
*= sizeof (unsigned long int);
3070 counts
= (unsigned long int *) bfd_malloc (amt
);
3077 /* Compute the "optimal" size for the hash table. The criteria is a
3078 minimal chain length. The minor criteria is (of course) the size
3080 for (i
= minsize
; i
< maxsize
; ++i
)
3082 /* Walk through the array of hashcodes and count the collisions. */
3083 BFD_HOST_U_64_BIT max
;
3084 unsigned long int j
;
3085 unsigned long int fact
;
3087 memset (counts
, '\0', i
* sizeof (unsigned long int));
3089 /* Determine how often each hash bucket is used. */
3090 for (j
= 0; j
< nsyms
; ++j
)
3091 ++counts
[hashcodes
[j
] % i
];
3093 /* For the weight function we need some information about the
3094 pagesize on the target. This is information need not be 100%
3095 accurate. Since this information is not available (so far) we
3096 define it here to a reasonable default value. If it is crucial
3097 to have a better value some day simply define this value. */
3098 # ifndef BFD_TARGET_PAGESIZE
3099 # define BFD_TARGET_PAGESIZE (4096)
3102 /* We in any case need 2 + NSYMS entries for the size values and
3104 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
3107 /* Variant 1: optimize for short chains. We add the squares
3108 of all the chain lengths (which favous many small chain
3109 over a few long chains). */
3110 for (j
= 0; j
< i
; ++j
)
3111 max
+= counts
[j
] * counts
[j
];
3113 /* This adds penalties for the overall size of the table. */
3114 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
3117 /* Variant 2: Optimize a lot more for small table. Here we
3118 also add squares of the size but we also add penalties for
3119 empty slots (the +1 term). */
3120 for (j
= 0; j
< i
; ++j
)
3121 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
3123 /* The overall size of the table is considered, but not as
3124 strong as in variant 1, where it is squared. */
3125 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
3129 /* Compare with current best results. */
3130 if (max
< best_chlen
)
3140 #endif /* defined (BFD_HOST_U_64_BIT) */
3142 /* This is the fallback solution if no 64bit type is available or if we
3143 are not supposed to spend much time on optimizations. We select the
3144 bucket count using a fixed set of numbers. */
3145 for (i
= 0; elf_buckets
[i
] != 0; i
++)
3147 best_size
= elf_buckets
[i
];
3148 if (dynsymcount
< elf_buckets
[i
+ 1])
3153 /* Free the arrays we needed. */
3159 /* Set up the sizes and contents of the ELF dynamic sections. This is
3160 called by the ELF linker emulation before_allocation routine. We
3161 must set the sizes of the sections before the linker sets the
3162 addresses of the various sections. */
3165 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
3167 auxiliary_filters
, info
, sinterpptr
,
3172 const char *filter_shlib
;
3173 const char * const *auxiliary_filters
;
3174 struct bfd_link_info
*info
;
3175 asection
**sinterpptr
;
3176 struct bfd_elf_version_tree
*verdefs
;
3178 bfd_size_type soname_indx
;
3180 struct elf_backend_data
*bed
;
3181 struct elf_assign_sym_version_info asvinfo
;
3185 soname_indx
= (bfd_size_type
) -1;
3187 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
3190 if (! is_elf_hash_table (info
))
3193 /* Any syms created from now on start with -1 in
3194 got.refcount/offset and plt.refcount/offset. */
3195 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
3197 /* The backend may have to create some sections regardless of whether
3198 we're dynamic or not. */
3199 bed
= get_elf_backend_data (output_bfd
);
3200 if (bed
->elf_backend_always_size_sections
3201 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
3204 dynobj
= elf_hash_table (info
)->dynobj
;
3206 /* If there were no dynamic objects in the link, there is nothing to
3211 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
3214 if (elf_hash_table (info
)->dynamic_sections_created
)
3216 struct elf_info_failed eif
;
3217 struct elf_link_hash_entry
*h
;
3219 struct bfd_elf_version_tree
*t
;
3220 struct bfd_elf_version_expr
*d
;
3221 bfd_boolean all_defined
;
3223 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
3224 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
3228 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3230 if (soname_indx
== (bfd_size_type
) -1
3231 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SONAME
,
3238 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMBOLIC
,
3241 info
->flags
|= DF_SYMBOLIC
;
3248 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
3250 if (info
->new_dtags
)
3251 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
3252 if (indx
== (bfd_size_type
) -1
3253 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RPATH
, indx
)
3255 && ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RUNPATH
,
3260 if (filter_shlib
!= NULL
)
3264 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3265 filter_shlib
, TRUE
);
3266 if (indx
== (bfd_size_type
) -1
3267 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FILTER
, indx
))
3271 if (auxiliary_filters
!= NULL
)
3273 const char * const *p
;
3275 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
3279 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3281 if (indx
== (bfd_size_type
) -1
3282 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_AUXILIARY
,
3289 eif
.verdefs
= verdefs
;
3292 /* If we are supposed to export all symbols into the dynamic symbol
3293 table (this is not the normal case), then do so. */
3294 if (info
->export_dynamic
)
3296 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
3302 /* Make all global versions with definiton. */
3303 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3304 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3305 if (!d
->symver
&& strchr (d
->pattern
, '*') == NULL
)
3307 const char *verstr
, *name
;
3308 size_t namelen
, verlen
, newlen
;
3310 struct elf_link_hash_entry
*newh
;
3313 namelen
= strlen (name
);
3315 verlen
= strlen (verstr
);
3316 newlen
= namelen
+ verlen
+ 3;
3318 newname
= (char *) bfd_malloc ((bfd_size_type
) newlen
);
3319 if (newname
== NULL
)
3321 memcpy (newname
, name
, namelen
);
3323 /* Check the hidden versioned definition. */
3324 p
= newname
+ namelen
;
3326 memcpy (p
, verstr
, verlen
+ 1);
3327 newh
= elf_link_hash_lookup (elf_hash_table (info
),
3328 newname
, FALSE
, FALSE
,
3331 || (newh
->root
.type
!= bfd_link_hash_defined
3332 && newh
->root
.type
!= bfd_link_hash_defweak
))
3334 /* Check the default versioned definition. */
3336 memcpy (p
, verstr
, verlen
+ 1);
3337 newh
= elf_link_hash_lookup (elf_hash_table (info
),
3338 newname
, FALSE
, FALSE
,
3343 /* Mark this version if there is a definition and it is
3344 not defined in a shared object. */
3346 && ((newh
->elf_link_hash_flags
3347 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)
3348 && (newh
->root
.type
== bfd_link_hash_defined
3349 || newh
->root
.type
== bfd_link_hash_defweak
))
3353 /* Attach all the symbols to their version information. */
3354 asvinfo
.output_bfd
= output_bfd
;
3355 asvinfo
.info
= info
;
3356 asvinfo
.verdefs
= verdefs
;
3357 asvinfo
.failed
= FALSE
;
3359 elf_link_hash_traverse (elf_hash_table (info
),
3360 elf_link_assign_sym_version
,
3365 if (!info
->allow_undefined_version
)
3367 /* Check if all global versions have a definiton. */
3369 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3370 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3371 if (!d
->symver
&& !d
->script
3372 && strchr (d
->pattern
, '*') == NULL
)
3374 (*_bfd_error_handler
)
3375 (_("%s: undefined version: %s"),
3376 d
->pattern
, t
->name
);
3377 all_defined
= FALSE
;
3382 bfd_set_error (bfd_error_bad_value
);
3387 /* Find all symbols which were defined in a dynamic object and make
3388 the backend pick a reasonable value for them. */
3389 elf_link_hash_traverse (elf_hash_table (info
),
3390 elf_adjust_dynamic_symbol
,
3395 /* Add some entries to the .dynamic section. We fill in some of the
3396 values later, in elf_bfd_final_link, but we must add the entries
3397 now so that we know the final size of the .dynamic section. */
3399 /* If there are initialization and/or finalization functions to
3400 call then add the corresponding DT_INIT/DT_FINI entries. */
3401 h
= (info
->init_function
3402 ? elf_link_hash_lookup (elf_hash_table (info
),
3403 info
->init_function
, FALSE
,
3407 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3408 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3410 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT
, (bfd_vma
) 0))
3413 h
= (info
->fini_function
3414 ? elf_link_hash_lookup (elf_hash_table (info
),
3415 info
->fini_function
, FALSE
,
3419 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3420 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3422 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI
, (bfd_vma
) 0))
3426 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
3428 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3434 for (sub
= info
->input_bfds
; sub
!= NULL
;
3435 sub
= sub
->link_next
)
3436 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3437 if (elf_section_data (o
)->this_hdr
.sh_type
3438 == SHT_PREINIT_ARRAY
)
3440 (*_bfd_error_handler
)
3441 (_("%s: .preinit_array section is not allowed in DSO"),
3442 bfd_archive_filename (sub
));
3446 bfd_set_error (bfd_error_nonrepresentable_section
);
3450 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAY
,
3452 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAYSZ
,
3456 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
3458 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAY
,
3460 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAYSZ
,
3464 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
3466 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAY
,
3468 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAYSZ
,
3473 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
3474 /* If .dynstr is excluded from the link, we don't want any of
3475 these tags. Strictly, we should be checking each section
3476 individually; This quick check covers for the case where
3477 someone does a /DISCARD/ : { *(*) }. */
3478 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
3480 bfd_size_type strsize
;
3482 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3483 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_HASH
, (bfd_vma
) 0)
3484 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRTAB
, (bfd_vma
) 0)
3485 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMTAB
, (bfd_vma
) 0)
3486 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRSZ
, strsize
)
3487 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMENT
,
3488 (bfd_vma
) sizeof (Elf_External_Sym
)))
3493 /* The backend must work out the sizes of all the other dynamic
3495 if (bed
->elf_backend_size_dynamic_sections
3496 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
3499 if (elf_hash_table (info
)->dynamic_sections_created
)
3501 bfd_size_type dynsymcount
;
3503 size_t bucketcount
= 0;
3504 size_t hash_entry_size
;
3505 unsigned int dtagcount
;
3507 /* Set up the version definition section. */
3508 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3509 BFD_ASSERT (s
!= NULL
);
3511 /* We may have created additional version definitions if we are
3512 just linking a regular application. */
3513 verdefs
= asvinfo
.verdefs
;
3515 /* Skip anonymous version tag. */
3516 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
3517 verdefs
= verdefs
->next
;
3519 if (verdefs
== NULL
)
3520 _bfd_strip_section_from_output (info
, s
);
3525 struct bfd_elf_version_tree
*t
;
3527 Elf_Internal_Verdef def
;
3528 Elf_Internal_Verdaux defaux
;
3533 /* Make space for the base version. */
3534 size
+= sizeof (Elf_External_Verdef
);
3535 size
+= sizeof (Elf_External_Verdaux
);
3538 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3540 struct bfd_elf_version_deps
*n
;
3542 size
+= sizeof (Elf_External_Verdef
);
3543 size
+= sizeof (Elf_External_Verdaux
);
3546 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3547 size
+= sizeof (Elf_External_Verdaux
);
3550 s
->_raw_size
= size
;
3551 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3552 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3555 /* Fill in the version definition section. */
3559 def
.vd_version
= VER_DEF_CURRENT
;
3560 def
.vd_flags
= VER_FLG_BASE
;
3563 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3564 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3565 + sizeof (Elf_External_Verdaux
));
3567 if (soname_indx
!= (bfd_size_type
) -1)
3569 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3571 def
.vd_hash
= bfd_elf_hash (soname
);
3572 defaux
.vda_name
= soname_indx
;
3579 name
= basename (output_bfd
->filename
);
3580 def
.vd_hash
= bfd_elf_hash (name
);
3581 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3583 if (indx
== (bfd_size_type
) -1)
3585 defaux
.vda_name
= indx
;
3587 defaux
.vda_next
= 0;
3589 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3590 (Elf_External_Verdef
*) p
);
3591 p
+= sizeof (Elf_External_Verdef
);
3592 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3593 (Elf_External_Verdaux
*) p
);
3594 p
+= sizeof (Elf_External_Verdaux
);
3596 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3599 struct bfd_elf_version_deps
*n
;
3600 struct elf_link_hash_entry
*h
;
3601 struct bfd_link_hash_entry
*bh
;
3604 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3607 /* Add a symbol representing this version. */
3609 if (! (_bfd_generic_link_add_one_symbol
3610 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
3611 (bfd_vma
) 0, (const char *) NULL
, FALSE
,
3612 get_elf_backend_data (dynobj
)->collect
, &bh
)))
3614 h
= (struct elf_link_hash_entry
*) bh
;
3615 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
3616 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3617 h
->type
= STT_OBJECT
;
3618 h
->verinfo
.vertree
= t
;
3620 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
3623 def
.vd_version
= VER_DEF_CURRENT
;
3625 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
3626 def
.vd_flags
|= VER_FLG_WEAK
;
3627 def
.vd_ndx
= t
->vernum
+ 1;
3628 def
.vd_cnt
= cdeps
+ 1;
3629 def
.vd_hash
= bfd_elf_hash (t
->name
);
3630 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3631 if (t
->next
!= NULL
)
3632 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3633 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
3637 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3638 (Elf_External_Verdef
*) p
);
3639 p
+= sizeof (Elf_External_Verdef
);
3641 defaux
.vda_name
= h
->dynstr_index
;
3642 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3644 if (t
->deps
== NULL
)
3645 defaux
.vda_next
= 0;
3647 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3648 t
->name_indx
= defaux
.vda_name
;
3650 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3651 (Elf_External_Verdaux
*) p
);
3652 p
+= sizeof (Elf_External_Verdaux
);
3654 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3656 if (n
->version_needed
== NULL
)
3658 /* This can happen if there was an error in the
3660 defaux
.vda_name
= 0;
3664 defaux
.vda_name
= n
->version_needed
->name_indx
;
3665 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3668 if (n
->next
== NULL
)
3669 defaux
.vda_next
= 0;
3671 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3673 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3674 (Elf_External_Verdaux
*) p
);
3675 p
+= sizeof (Elf_External_Verdaux
);
3679 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEF
, (bfd_vma
) 0)
3680 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEFNUM
,
3684 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
3687 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
3689 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS
, info
->flags
))
3696 info
->flags_1
&= ~ (DF_1_INITFIRST
3699 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS_1
,
3704 /* Work out the size of the version reference section. */
3706 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3707 BFD_ASSERT (s
!= NULL
);
3709 struct elf_find_verdep_info sinfo
;
3711 sinfo
.output_bfd
= output_bfd
;
3713 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
3714 if (sinfo
.vers
== 0)
3716 sinfo
.failed
= FALSE
;
3718 elf_link_hash_traverse (elf_hash_table (info
),
3719 elf_link_find_version_dependencies
,
3722 if (elf_tdata (output_bfd
)->verref
== NULL
)
3723 _bfd_strip_section_from_output (info
, s
);
3726 Elf_Internal_Verneed
*t
;
3731 /* Build the version definition section. */
3734 for (t
= elf_tdata (output_bfd
)->verref
;
3738 Elf_Internal_Vernaux
*a
;
3740 size
+= sizeof (Elf_External_Verneed
);
3742 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3743 size
+= sizeof (Elf_External_Vernaux
);
3746 s
->_raw_size
= size
;
3747 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3748 if (s
->contents
== NULL
)
3752 for (t
= elf_tdata (output_bfd
)->verref
;
3757 Elf_Internal_Vernaux
*a
;
3761 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3764 t
->vn_version
= VER_NEED_CURRENT
;
3766 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3767 elf_dt_name (t
->vn_bfd
) != NULL
3768 ? elf_dt_name (t
->vn_bfd
)
3769 : basename (t
->vn_bfd
->filename
),
3771 if (indx
== (bfd_size_type
) -1)
3774 t
->vn_aux
= sizeof (Elf_External_Verneed
);
3775 if (t
->vn_nextref
== NULL
)
3778 t
->vn_next
= (sizeof (Elf_External_Verneed
)
3779 + caux
* sizeof (Elf_External_Vernaux
));
3781 _bfd_elf_swap_verneed_out (output_bfd
, t
,
3782 (Elf_External_Verneed
*) p
);
3783 p
+= sizeof (Elf_External_Verneed
);
3785 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3787 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
3788 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3789 a
->vna_nodename
, FALSE
);
3790 if (indx
== (bfd_size_type
) -1)
3793 if (a
->vna_nextptr
== NULL
)
3796 a
->vna_next
= sizeof (Elf_External_Vernaux
);
3798 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
3799 (Elf_External_Vernaux
*) p
);
3800 p
+= sizeof (Elf_External_Vernaux
);
3804 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEED
,
3806 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEEDNUM
,
3810 elf_tdata (output_bfd
)->cverrefs
= crefs
;
3814 /* Assign dynsym indicies. In a shared library we generate a
3815 section symbol for each output section, which come first.
3816 Next come all of the back-end allocated local dynamic syms,
3817 followed by the rest of the global symbols. */
3819 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3821 /* Work out the size of the symbol version section. */
3822 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3823 BFD_ASSERT (s
!= NULL
);
3824 if (dynsymcount
== 0
3825 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
3827 _bfd_strip_section_from_output (info
, s
);
3828 /* The DYNSYMCOUNT might have changed if we were going to
3829 output a dynamic symbol table entry for S. */
3830 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3834 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
3835 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3836 if (s
->contents
== NULL
)
3839 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERSYM
, (bfd_vma
) 0))
3843 /* Set the size of the .dynsym and .hash sections. We counted
3844 the number of dynamic symbols in elf_link_add_object_symbols.
3845 We will build the contents of .dynsym and .hash when we build
3846 the final symbol table, because until then we do not know the
3847 correct value to give the symbols. We built the .dynstr
3848 section as we went along in elf_link_add_object_symbols. */
3849 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
3850 BFD_ASSERT (s
!= NULL
);
3851 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
3852 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3853 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3856 if (dynsymcount
!= 0)
3858 Elf_Internal_Sym isym
;
3860 /* The first entry in .dynsym is a dummy symbol. */
3867 elf_swap_symbol_out (output_bfd
, &isym
, (PTR
) s
->contents
, (PTR
) 0);
3870 /* Compute the size of the hashing table. As a side effect this
3871 computes the hash values for all the names we export. */
3872 bucketcount
= compute_bucket_count (info
);
3874 s
= bfd_get_section_by_name (dynobj
, ".hash");
3875 BFD_ASSERT (s
!= NULL
);
3876 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
3877 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
3878 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3879 if (s
->contents
== NULL
)
3882 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) bucketcount
,
3884 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) dynsymcount
,
3885 s
->contents
+ hash_entry_size
);
3887 elf_hash_table (info
)->bucketcount
= bucketcount
;
3889 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
3890 BFD_ASSERT (s
!= NULL
);
3892 elf_finalize_dynstr (output_bfd
, info
);
3894 s
->_raw_size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3896 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
3897 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NULL
, (bfd_vma
) 0))
3904 /* This function is used to adjust offsets into .dynstr for
3905 dynamic symbols. This is called via elf_link_hash_traverse. */
3907 static bfd_boolean elf_adjust_dynstr_offsets
3908 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3911 elf_adjust_dynstr_offsets (h
, data
)
3912 struct elf_link_hash_entry
*h
;
3915 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3917 if (h
->root
.type
== bfd_link_hash_warning
)
3918 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3920 if (h
->dynindx
!= -1)
3921 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3925 /* Assign string offsets in .dynstr, update all structures referencing
3929 elf_finalize_dynstr (output_bfd
, info
)
3931 struct bfd_link_info
*info
;
3933 struct elf_link_local_dynamic_entry
*entry
;
3934 struct elf_strtab_hash
*dynstr
= elf_hash_table (info
)->dynstr
;
3935 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
3938 Elf_External_Dyn
*dyncon
, *dynconend
;
3940 _bfd_elf_strtab_finalize (dynstr
);
3941 size
= _bfd_elf_strtab_size (dynstr
);
3943 /* Update all .dynamic entries referencing .dynstr strings. */
3944 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3945 BFD_ASSERT (sdyn
!= NULL
);
3947 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
3948 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
3950 for (; dyncon
< dynconend
; dyncon
++)
3952 Elf_Internal_Dyn dyn
;
3954 elf_swap_dyn_in (dynobj
, dyncon
, & dyn
);
3958 dyn
.d_un
.d_val
= size
;
3959 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3967 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3968 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3975 /* Now update local dynamic symbols. */
3976 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
3977 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3978 entry
->isym
.st_name
);
3980 /* And the rest of dynamic symbols. */
3981 elf_link_hash_traverse (elf_hash_table (info
),
3982 elf_adjust_dynstr_offsets
, dynstr
);
3984 /* Adjust version definitions. */
3985 if (elf_tdata (output_bfd
)->cverdefs
)
3990 Elf_Internal_Verdef def
;
3991 Elf_Internal_Verdaux defaux
;
3993 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3994 p
= (bfd_byte
*) s
->contents
;
3997 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3999 p
+= sizeof (Elf_External_Verdef
);
4000 for (i
= 0; i
< def
.vd_cnt
; ++i
)
4002 _bfd_elf_swap_verdaux_in (output_bfd
,
4003 (Elf_External_Verdaux
*) p
, &defaux
);
4004 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
4006 _bfd_elf_swap_verdaux_out (output_bfd
,
4007 &defaux
, (Elf_External_Verdaux
*) p
);
4008 p
+= sizeof (Elf_External_Verdaux
);
4011 while (def
.vd_next
);
4014 /* Adjust version references. */
4015 if (elf_tdata (output_bfd
)->verref
)
4020 Elf_Internal_Verneed need
;
4021 Elf_Internal_Vernaux needaux
;
4023 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
4024 p
= (bfd_byte
*) s
->contents
;
4027 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
4029 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
4030 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
4031 (Elf_External_Verneed
*) p
);
4032 p
+= sizeof (Elf_External_Verneed
);
4033 for (i
= 0; i
< need
.vn_cnt
; ++i
)
4035 _bfd_elf_swap_vernaux_in (output_bfd
,
4036 (Elf_External_Vernaux
*) p
, &needaux
);
4037 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
4039 _bfd_elf_swap_vernaux_out (output_bfd
,
4041 (Elf_External_Vernaux
*) p
);
4042 p
+= sizeof (Elf_External_Vernaux
);
4045 while (need
.vn_next
);
4051 /* Fix up the flags for a symbol. This handles various cases which
4052 can only be fixed after all the input files are seen. This is
4053 currently called by both adjust_dynamic_symbol and
4054 assign_sym_version, which is unnecessary but perhaps more robust in
4055 the face of future changes. */
4058 elf_fix_symbol_flags (h
, eif
)
4059 struct elf_link_hash_entry
*h
;
4060 struct elf_info_failed
*eif
;
4062 /* If this symbol was mentioned in a non-ELF file, try to set
4063 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
4064 permit a non-ELF file to correctly refer to a symbol defined in
4065 an ELF dynamic object. */
4066 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
4068 while (h
->root
.type
== bfd_link_hash_indirect
)
4069 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4071 if (h
->root
.type
!= bfd_link_hash_defined
4072 && h
->root
.type
!= bfd_link_hash_defweak
)
4073 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
4074 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
4077 if (h
->root
.u
.def
.section
->owner
!= NULL
4078 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
4079 == bfd_target_elf_flavour
))
4080 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
4081 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
4083 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4086 if (h
->dynindx
== -1
4087 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
4088 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
4090 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
4099 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
4100 was first seen in a non-ELF file. Fortunately, if the symbol
4101 was first seen in an ELF file, we're probably OK unless the
4102 symbol was defined in a non-ELF file. Catch that case here.
4103 FIXME: We're still in trouble if the symbol was first seen in
4104 a dynamic object, and then later in a non-ELF regular object. */
4105 if ((h
->root
.type
== bfd_link_hash_defined
4106 || h
->root
.type
== bfd_link_hash_defweak
)
4107 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
4108 && (h
->root
.u
.def
.section
->owner
!= NULL
4109 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
4110 != bfd_target_elf_flavour
)
4111 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
4112 && (h
->elf_link_hash_flags
4113 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
4114 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4117 /* If this is a final link, and the symbol was defined as a common
4118 symbol in a regular object file, and there was no definition in
4119 any dynamic object, then the linker will have allocated space for
4120 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
4121 flag will not have been set. */
4122 if (h
->root
.type
== bfd_link_hash_defined
4123 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
4124 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
4125 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4126 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
4127 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4129 /* If -Bsymbolic was used (which means to bind references to global
4130 symbols to the definition within the shared object), and this
4131 symbol was defined in a regular object, then it actually doesn't
4132 need a PLT entry, and we can accomplish that by forcing it local.
4133 Likewise, if the symbol has hidden or internal visibility.
4134 FIXME: It might be that we also do not need a PLT for other
4135 non-hidden visibilities, but we would have to tell that to the
4136 backend specifically; we can't just clear PLT-related data here. */
4137 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
4138 && eif
->info
->shared
4139 && is_elf_hash_table (eif
->info
)
4140 && (eif
->info
->symbolic
4141 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
4142 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
4143 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
4145 struct elf_backend_data
*bed
;
4146 bfd_boolean force_local
;
4148 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
4150 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
4151 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
4152 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
4155 /* If a weak undefined symbol has non-default visibility, we also
4156 hide it from the dynamic linker. */
4157 if (ELF_ST_VISIBILITY (h
->other
)
4158 && h
->root
.type
== bfd_link_hash_undefweak
)
4160 struct elf_backend_data
*bed
;
4161 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
4162 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
4165 /* If this is a weak defined symbol in a dynamic object, and we know
4166 the real definition in the dynamic object, copy interesting flags
4167 over to the real definition. */
4168 if (h
->weakdef
!= NULL
)
4170 struct elf_link_hash_entry
*weakdef
;
4172 weakdef
= h
->weakdef
;
4173 if (h
->root
.type
== bfd_link_hash_indirect
)
4174 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4176 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4177 || h
->root
.type
== bfd_link_hash_defweak
);
4178 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
4179 || weakdef
->root
.type
== bfd_link_hash_defweak
);
4180 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
4182 /* If the real definition is defined by a regular object file,
4183 don't do anything special. See the longer description in
4184 elf_adjust_dynamic_symbol, below. */
4185 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
4189 struct elf_backend_data
*bed
;
4191 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
4192 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
4199 /* Make the backend pick a good value for a dynamic symbol. This is
4200 called via elf_link_hash_traverse, and also calls itself
4204 elf_adjust_dynamic_symbol (h
, data
)
4205 struct elf_link_hash_entry
*h
;
4208 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
4210 struct elf_backend_data
*bed
;
4212 if (! is_elf_hash_table (eif
->info
))
4215 if (h
->root
.type
== bfd_link_hash_warning
)
4217 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
4218 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
4220 /* When warning symbols are created, they **replace** the "real"
4221 entry in the hash table, thus we never get to see the real
4222 symbol in a hash traversal. So look at it now. */
4223 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4226 /* Ignore indirect symbols. These are added by the versioning code. */
4227 if (h
->root
.type
== bfd_link_hash_indirect
)
4230 /* Fix the symbol flags. */
4231 if (! elf_fix_symbol_flags (h
, eif
))
4234 /* If this symbol does not require a PLT entry, and it is not
4235 defined by a dynamic object, or is not referenced by a regular
4236 object, ignore it. We do have to handle a weak defined symbol,
4237 even if no regular object refers to it, if we decided to add it
4238 to the dynamic symbol table. FIXME: Do we normally need to worry
4239 about symbols which are defined by one dynamic object and
4240 referenced by another one? */
4241 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
4242 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4243 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4244 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
4245 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
4247 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
4251 /* If we've already adjusted this symbol, don't do it again. This
4252 can happen via a recursive call. */
4253 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
4256 /* Don't look at this symbol again. Note that we must set this
4257 after checking the above conditions, because we may look at a
4258 symbol once, decide not to do anything, and then get called
4259 recursively later after REF_REGULAR is set below. */
4260 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
4262 /* If this is a weak definition, and we know a real definition, and
4263 the real symbol is not itself defined by a regular object file,
4264 then get a good value for the real definition. We handle the
4265 real symbol first, for the convenience of the backend routine.
4267 Note that there is a confusing case here. If the real definition
4268 is defined by a regular object file, we don't get the real symbol
4269 from the dynamic object, but we do get the weak symbol. If the
4270 processor backend uses a COPY reloc, then if some routine in the
4271 dynamic object changes the real symbol, we will not see that
4272 change in the corresponding weak symbol. This is the way other
4273 ELF linkers work as well, and seems to be a result of the shared
4276 I will clarify this issue. Most SVR4 shared libraries define the
4277 variable _timezone and define timezone as a weak synonym. The
4278 tzset call changes _timezone. If you write
4279 extern int timezone;
4281 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4282 you might expect that, since timezone is a synonym for _timezone,
4283 the same number will print both times. However, if the processor
4284 backend uses a COPY reloc, then actually timezone will be copied
4285 into your process image, and, since you define _timezone
4286 yourself, _timezone will not. Thus timezone and _timezone will
4287 wind up at different memory locations. The tzset call will set
4288 _timezone, leaving timezone unchanged. */
4290 if (h
->weakdef
!= NULL
)
4292 /* If we get to this point, we know there is an implicit
4293 reference by a regular object file via the weak symbol H.
4294 FIXME: Is this really true? What if the traversal finds
4295 H->WEAKDEF before it finds H? */
4296 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
4298 if (! elf_adjust_dynamic_symbol (h
->weakdef
, (PTR
) eif
))
4302 /* If a symbol has no type and no size and does not require a PLT
4303 entry, then we are probably about to do the wrong thing here: we
4304 are probably going to create a COPY reloc for an empty object.
4305 This case can arise when a shared object is built with assembly
4306 code, and the assembly code fails to set the symbol type. */
4308 && h
->type
== STT_NOTYPE
4309 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
4310 (*_bfd_error_handler
)
4311 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4312 h
->root
.root
.string
);
4314 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
4315 bed
= get_elf_backend_data (dynobj
);
4316 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
4325 /* This routine is used to export all defined symbols into the dynamic
4326 symbol table. It is called via elf_link_hash_traverse. */
4329 elf_export_symbol (h
, data
)
4330 struct elf_link_hash_entry
*h
;
4333 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
4335 /* Ignore indirect symbols. These are added by the versioning code. */
4336 if (h
->root
.type
== bfd_link_hash_indirect
)
4339 if (h
->root
.type
== bfd_link_hash_warning
)
4340 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4342 if (h
->dynindx
== -1
4343 && (h
->elf_link_hash_flags
4344 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
4346 struct bfd_elf_version_tree
*t
;
4347 struct bfd_elf_version_expr
*d
;
4349 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
4351 if (t
->globals
!= NULL
)
4353 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4355 if ((*d
->match
) (d
, h
->root
.root
.string
))
4360 if (t
->locals
!= NULL
)
4362 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4364 if ((*d
->match
) (d
, h
->root
.root
.string
))
4373 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
4384 /* Look through the symbols which are defined in other shared
4385 libraries and referenced here. Update the list of version
4386 dependencies. This will be put into the .gnu.version_r section.
4387 This function is called via elf_link_hash_traverse. */
4390 elf_link_find_version_dependencies (h
, data
)
4391 struct elf_link_hash_entry
*h
;
4394 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
4395 Elf_Internal_Verneed
*t
;
4396 Elf_Internal_Vernaux
*a
;
4399 if (h
->root
.type
== bfd_link_hash_warning
)
4400 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4402 /* We only care about symbols defined in shared objects with version
4404 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4405 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4407 || h
->verinfo
.verdef
== NULL
)
4410 /* See if we already know about this version. */
4411 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
4413 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
4416 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4417 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
4423 /* This is a new version. Add it to tree we are building. */
4428 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4431 rinfo
->failed
= TRUE
;
4435 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
4436 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
4437 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
4441 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4443 /* Note that we are copying a string pointer here, and testing it
4444 above. If bfd_elf_string_from_elf_section is ever changed to
4445 discard the string data when low in memory, this will have to be
4447 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
4449 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
4450 a
->vna_nextptr
= t
->vn_auxptr
;
4452 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
4455 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4462 /* Figure out appropriate versions for all the symbols. We may not
4463 have the version number script until we have read all of the input
4464 files, so until that point we don't know which symbols should be
4465 local. This function is called via elf_link_hash_traverse. */
4468 elf_link_assign_sym_version (h
, data
)
4469 struct elf_link_hash_entry
*h
;
4472 struct elf_assign_sym_version_info
*sinfo
;
4473 struct bfd_link_info
*info
;
4474 struct elf_backend_data
*bed
;
4475 struct elf_info_failed eif
;
4479 sinfo
= (struct elf_assign_sym_version_info
*) data
;
4482 if (h
->root
.type
== bfd_link_hash_warning
)
4483 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4485 /* Fix the symbol flags. */
4488 if (! elf_fix_symbol_flags (h
, &eif
))
4491 sinfo
->failed
= TRUE
;
4495 /* We only need version numbers for symbols defined in regular
4497 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4500 bed
= get_elf_backend_data (sinfo
->output_bfd
);
4501 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4502 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
4504 struct bfd_elf_version_tree
*t
;
4509 /* There are two consecutive ELF_VER_CHR characters if this is
4510 not a hidden symbol. */
4512 if (*p
== ELF_VER_CHR
)
4518 /* If there is no version string, we can just return out. */
4522 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4526 /* Look for the version. If we find it, it is no longer weak. */
4527 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4529 if (strcmp (t
->name
, p
) == 0)
4533 struct bfd_elf_version_expr
*d
;
4535 len
= p
- h
->root
.root
.string
;
4536 alc
= bfd_malloc ((bfd_size_type
) len
);
4539 memcpy (alc
, h
->root
.root
.string
, len
- 1);
4540 alc
[len
- 1] = '\0';
4541 if (alc
[len
- 2] == ELF_VER_CHR
)
4542 alc
[len
- 2] = '\0';
4544 h
->verinfo
.vertree
= t
;
4548 if (t
->globals
!= NULL
)
4550 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4551 if ((*d
->match
) (d
, alc
))
4555 /* See if there is anything to force this symbol to
4557 if (d
== NULL
&& t
->locals
!= NULL
)
4559 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4561 if ((*d
->match
) (d
, alc
))
4563 if (h
->dynindx
!= -1
4565 && ! info
->export_dynamic
)
4567 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4580 /* If we are building an application, we need to create a
4581 version node for this version. */
4582 if (t
== NULL
&& ! info
->shared
)
4584 struct bfd_elf_version_tree
**pp
;
4587 /* If we aren't going to export this symbol, we don't need
4588 to worry about it. */
4589 if (h
->dynindx
== -1)
4593 t
= ((struct bfd_elf_version_tree
*)
4594 bfd_alloc (sinfo
->output_bfd
, amt
));
4597 sinfo
->failed
= TRUE
;
4606 t
->name_indx
= (unsigned int) -1;
4610 /* Don't count anonymous version tag. */
4611 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
4613 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4615 t
->vernum
= version_index
;
4619 h
->verinfo
.vertree
= t
;
4623 /* We could not find the version for a symbol when
4624 generating a shared archive. Return an error. */
4625 (*_bfd_error_handler
)
4626 (_("%s: undefined versioned symbol name %s"),
4627 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
4628 bfd_set_error (bfd_error_bad_value
);
4629 sinfo
->failed
= TRUE
;
4634 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4637 /* If we don't have a version for this symbol, see if we can find
4639 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
4641 struct bfd_elf_version_tree
*t
;
4642 struct bfd_elf_version_tree
*local_ver
;
4643 struct bfd_elf_version_expr
*d
;
4645 /* See if can find what version this symbol is in. If the
4646 symbol is supposed to be local, then don't actually register
4649 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4651 if (t
->globals
!= NULL
)
4653 bfd_boolean matched
;
4656 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4658 if ((*d
->match
) (d
, h
->root
.root
.string
))
4664 /* There is a version without definition. Make
4665 the symbol the default definition for this
4667 h
->verinfo
.vertree
= t
;
4678 /* There is no undefined version for this symbol. Hide the
4680 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4683 if (t
->locals
!= NULL
)
4685 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4687 /* If the match is "*", keep looking for a more
4688 explicit, perhaps even global, match. */
4689 if (d
->pattern
[0] == '*' && d
->pattern
[1] == '\0')
4691 else if ((*d
->match
) (d
, h
->root
.root
.string
))
4703 if (local_ver
!= NULL
)
4705 h
->verinfo
.vertree
= local_ver
;
4706 if (h
->dynindx
!= -1
4708 && ! info
->export_dynamic
)
4710 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4718 /* Final phase of ELF linker. */
4720 /* A structure we use to avoid passing large numbers of arguments. */
4722 struct elf_final_link_info
4724 /* General link information. */
4725 struct bfd_link_info
*info
;
4728 /* Symbol string table. */
4729 struct bfd_strtab_hash
*symstrtab
;
4730 /* .dynsym section. */
4731 asection
*dynsym_sec
;
4732 /* .hash section. */
4734 /* symbol version section (.gnu.version). */
4735 asection
*symver_sec
;
4736 /* first SHF_TLS section (if any). */
4737 asection
*first_tls_sec
;
4738 /* Buffer large enough to hold contents of any section. */
4740 /* Buffer large enough to hold external relocs of any section. */
4741 PTR external_relocs
;
4742 /* Buffer large enough to hold internal relocs of any section. */
4743 Elf_Internal_Rela
*internal_relocs
;
4744 /* Buffer large enough to hold external local symbols of any input
4746 Elf_External_Sym
*external_syms
;
4747 /* And a buffer for symbol section indices. */
4748 Elf_External_Sym_Shndx
*locsym_shndx
;
4749 /* Buffer large enough to hold internal local symbols of any input
4751 Elf_Internal_Sym
*internal_syms
;
4752 /* Array large enough to hold a symbol index for each local symbol
4753 of any input BFD. */
4755 /* Array large enough to hold a section pointer for each local
4756 symbol of any input BFD. */
4757 asection
**sections
;
4758 /* Buffer to hold swapped out symbols. */
4759 Elf_External_Sym
*symbuf
;
4760 /* And one for symbol section indices. */
4761 Elf_External_Sym_Shndx
*symshndxbuf
;
4762 /* Number of swapped out symbols in buffer. */
4763 size_t symbuf_count
;
4764 /* Number of symbols which fit in symbuf. */
4766 /* And same for symshndxbuf. */
4767 size_t shndxbuf_size
;
4770 static bfd_boolean elf_link_output_sym
4771 PARAMS ((struct elf_final_link_info
*, const char *,
4772 Elf_Internal_Sym
*, asection
*));
4773 static bfd_boolean elf_link_flush_output_syms
4774 PARAMS ((struct elf_final_link_info
*));
4775 static bfd_boolean elf_link_output_extsym
4776 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4777 static bfd_boolean elf_link_sec_merge_syms
4778 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4779 static bfd_boolean elf_link_check_versioned_symbol
4780 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
4781 static bfd_boolean elf_link_input_bfd
4782 PARAMS ((struct elf_final_link_info
*, bfd
*));
4783 static bfd_boolean elf_reloc_link_order
4784 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
4785 struct bfd_link_order
*));
4787 /* This struct is used to pass information to elf_link_output_extsym. */
4789 struct elf_outext_info
4792 bfd_boolean localsyms
;
4793 struct elf_final_link_info
*finfo
;
4796 /* Compute the size of, and allocate space for, REL_HDR which is the
4797 section header for a section containing relocations for O. */
4800 elf_link_size_reloc_section (abfd
, rel_hdr
, o
)
4802 Elf_Internal_Shdr
*rel_hdr
;
4805 bfd_size_type reloc_count
;
4806 bfd_size_type num_rel_hashes
;
4808 /* Figure out how many relocations there will be. */
4809 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
4810 reloc_count
= elf_section_data (o
)->rel_count
;
4812 reloc_count
= elf_section_data (o
)->rel_count2
;
4814 num_rel_hashes
= o
->reloc_count
;
4815 if (num_rel_hashes
< reloc_count
)
4816 num_rel_hashes
= reloc_count
;
4818 /* That allows us to calculate the size of the section. */
4819 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
4821 /* The contents field must last into write_object_contents, so we
4822 allocate it with bfd_alloc rather than malloc. Also since we
4823 cannot be sure that the contents will actually be filled in,
4824 we zero the allocated space. */
4825 rel_hdr
->contents
= (PTR
) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
4826 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
4829 /* We only allocate one set of hash entries, so we only do it the
4830 first time we are called. */
4831 if (elf_section_data (o
)->rel_hashes
== NULL
4834 struct elf_link_hash_entry
**p
;
4836 p
= ((struct elf_link_hash_entry
**)
4837 bfd_zmalloc (num_rel_hashes
4838 * sizeof (struct elf_link_hash_entry
*)));
4842 elf_section_data (o
)->rel_hashes
= p
;
4848 /* When performing a relocateable link, the input relocations are
4849 preserved. But, if they reference global symbols, the indices
4850 referenced must be updated. Update all the relocations in
4851 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4854 elf_link_adjust_relocs (abfd
, rel_hdr
, count
, rel_hash
)
4856 Elf_Internal_Shdr
*rel_hdr
;
4858 struct elf_link_hash_entry
**rel_hash
;
4861 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4863 void (*swap_in
) PARAMS ((bfd
*, const bfd_byte
*, Elf_Internal_Rela
*));
4864 void (*swap_out
) PARAMS ((bfd
*, const Elf_Internal_Rela
*, bfd_byte
*));
4866 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4868 swap_in
= bed
->s
->swap_reloc_in
;
4869 swap_out
= bed
->s
->swap_reloc_out
;
4871 else if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
))
4873 swap_in
= bed
->s
->swap_reloca_in
;
4874 swap_out
= bed
->s
->swap_reloca_out
;
4879 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
4882 erela
= rel_hdr
->contents
;
4883 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
4885 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
4888 if (*rel_hash
== NULL
)
4891 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
4893 (*swap_in
) (abfd
, erela
, irela
);
4894 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4895 irela
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4896 ELF_R_TYPE (irela
[j
].r_info
));
4897 (*swap_out
) (abfd
, irela
, erela
);
4901 struct elf_link_sort_rela
4904 enum elf_reloc_type_class type
;
4905 /* We use this as an array of size int_rels_per_ext_rel. */
4906 Elf_Internal_Rela rela
[1];
4910 elf_link_sort_cmp1 (A
, B
)
4914 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4915 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4916 int relativea
, relativeb
;
4918 relativea
= a
->type
== reloc_class_relative
;
4919 relativeb
= b
->type
== reloc_class_relative
;
4921 if (relativea
< relativeb
)
4923 if (relativea
> relativeb
)
4925 if (ELF_R_SYM (a
->rela
->r_info
) < ELF_R_SYM (b
->rela
->r_info
))
4927 if (ELF_R_SYM (a
->rela
->r_info
) > ELF_R_SYM (b
->rela
->r_info
))
4929 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
4931 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
4937 elf_link_sort_cmp2 (A
, B
)
4941 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4942 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4945 if (a
->offset
< b
->offset
)
4947 if (a
->offset
> b
->offset
)
4949 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
4950 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
4955 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
4957 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
4963 elf_link_sort_relocs (abfd
, info
, psec
)
4965 struct bfd_link_info
*info
;
4968 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4969 asection
*reldyn
, *o
;
4970 bfd_size_type count
, size
;
4971 size_t i
, ret
, sort_elt
, ext_size
;
4972 bfd_byte
*sort
, *s_non_relative
, *p
;
4973 struct elf_link_sort_rela
*sq
;
4974 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4975 int i2e
= bed
->s
->int_rels_per_ext_rel
;
4976 void (*swap_in
) PARAMS ((bfd
*, const bfd_byte
*, Elf_Internal_Rela
*));
4977 void (*swap_out
) PARAMS ((bfd
*, const Elf_Internal_Rela
*, bfd_byte
*));
4979 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
4980 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4982 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
4983 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4985 ext_size
= sizeof (Elf_External_Rel
);
4986 swap_in
= bed
->s
->swap_reloc_in
;
4987 swap_out
= bed
->s
->swap_reloc_out
;
4991 ext_size
= sizeof (Elf_External_Rela
);
4992 swap_in
= bed
->s
->swap_reloca_in
;
4993 swap_out
= bed
->s
->swap_reloca_out
;
4995 count
= reldyn
->_raw_size
/ ext_size
;
4998 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4999 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
5000 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
5001 && o
->output_section
== reldyn
)
5002 size
+= o
->_raw_size
;
5004 if (size
!= reldyn
->_raw_size
)
5007 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5008 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5009 sort
= bfd_zmalloc (sort_elt
* count
);
5012 (*info
->callbacks
->warning
)
5013 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0,
5018 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
5019 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
5020 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
5021 && o
->output_section
== reldyn
)
5023 bfd_byte
*erel
, *erelend
;
5026 erelend
= o
->contents
+ o
->_raw_size
;
5027 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5028 while (erel
< erelend
)
5030 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5031 (*swap_in
) (abfd
, erel
, s
->rela
);
5032 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5038 qsort (sort
, (size_t) count
, sort_elt
, elf_link_sort_cmp1
);
5040 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5042 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5043 if (s
->type
!= reloc_class_relative
)
5049 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5050 for (; i
< count
; i
++, p
+= sort_elt
)
5052 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5053 if (ELF_R_SYM (sp
->rela
->r_info
) != ELF_R_SYM (sq
->rela
->r_info
))
5055 sp
->offset
= sq
->rela
->r_offset
;
5058 qsort (s_non_relative
, (size_t) count
- ret
, sort_elt
, elf_link_sort_cmp2
);
5060 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
5061 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
5062 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
5063 && o
->output_section
== reldyn
)
5065 bfd_byte
*erel
, *erelend
;
5068 erelend
= o
->contents
+ o
->_raw_size
;
5069 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5070 while (erel
< erelend
)
5072 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5073 (*swap_out
) (abfd
, s
->rela
, erel
);
5084 /* Do the final step of an ELF link. */
5087 elf_bfd_final_link (abfd
, info
)
5089 struct bfd_link_info
*info
;
5091 bfd_boolean dynamic
;
5092 bfd_boolean emit_relocs
;
5094 struct elf_final_link_info finfo
;
5095 register asection
*o
;
5096 register struct bfd_link_order
*p
;
5098 bfd_size_type max_contents_size
;
5099 bfd_size_type max_external_reloc_size
;
5100 bfd_size_type max_internal_reloc_count
;
5101 bfd_size_type max_sym_count
;
5102 bfd_size_type max_sym_shndx_count
;
5104 Elf_Internal_Sym elfsym
;
5106 Elf_Internal_Shdr
*symtab_hdr
;
5107 Elf_Internal_Shdr
*symtab_shndx_hdr
;
5108 Elf_Internal_Shdr
*symstrtab_hdr
;
5109 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5110 struct elf_outext_info eoinfo
;
5112 size_t relativecount
= 0;
5113 asection
*reldyn
= 0;
5116 if (! is_elf_hash_table (info
))
5120 abfd
->flags
|= DYNAMIC
;
5122 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
5123 dynobj
= elf_hash_table (info
)->dynobj
;
5125 emit_relocs
= (info
->relocateable
5126 || info
->emitrelocations
5127 || bed
->elf_backend_emit_relocs
);
5130 finfo
.output_bfd
= abfd
;
5131 finfo
.symstrtab
= elf_stringtab_init ();
5132 if (finfo
.symstrtab
== NULL
)
5137 finfo
.dynsym_sec
= NULL
;
5138 finfo
.hash_sec
= NULL
;
5139 finfo
.symver_sec
= NULL
;
5143 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
5144 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
5145 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
5146 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5147 /* Note that it is OK if symver_sec is NULL. */
5150 finfo
.contents
= NULL
;
5151 finfo
.external_relocs
= NULL
;
5152 finfo
.internal_relocs
= NULL
;
5153 finfo
.external_syms
= NULL
;
5154 finfo
.locsym_shndx
= NULL
;
5155 finfo
.internal_syms
= NULL
;
5156 finfo
.indices
= NULL
;
5157 finfo
.sections
= NULL
;
5158 finfo
.symbuf
= NULL
;
5159 finfo
.symshndxbuf
= NULL
;
5160 finfo
.symbuf_count
= 0;
5161 finfo
.shndxbuf_size
= 0;
5162 finfo
.first_tls_sec
= NULL
;
5163 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
5164 if ((o
->flags
& SEC_THREAD_LOCAL
) != 0
5165 && (o
->flags
& SEC_LOAD
) != 0)
5167 finfo
.first_tls_sec
= o
;
5171 /* Count up the number of relocations we will output for each output
5172 section, so that we know the sizes of the reloc sections. We
5173 also figure out some maximum sizes. */
5174 max_contents_size
= 0;
5175 max_external_reloc_size
= 0;
5176 max_internal_reloc_count
= 0;
5178 max_sym_shndx_count
= 0;
5180 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
5182 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
5185 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5187 unsigned int reloc_count
= 0;
5188 struct bfd_elf_section_data
*esdi
= NULL
;
5189 unsigned int *rel_count1
;
5191 if (p
->type
== bfd_section_reloc_link_order
5192 || p
->type
== bfd_symbol_reloc_link_order
)
5194 else if (p
->type
== bfd_indirect_link_order
)
5198 sec
= p
->u
.indirect
.section
;
5199 esdi
= elf_section_data (sec
);
5201 /* Mark all sections which are to be included in the
5202 link. This will normally be every section. We need
5203 to do this so that we can identify any sections which
5204 the linker has decided to not include. */
5205 sec
->linker_mark
= TRUE
;
5207 if (sec
->flags
& SEC_MERGE
)
5210 if (info
->relocateable
|| info
->emitrelocations
)
5211 reloc_count
= sec
->reloc_count
;
5212 else if (bed
->elf_backend_count_relocs
)
5214 Elf_Internal_Rela
* relocs
;
5216 relocs
= (NAME(_bfd_elf
,link_read_relocs
)
5217 (abfd
, sec
, (PTR
) NULL
,
5218 (Elf_Internal_Rela
*) NULL
, info
->keep_memory
));
5220 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
5222 if (elf_section_data (o
)->relocs
!= relocs
)
5226 if (sec
->_raw_size
> max_contents_size
)
5227 max_contents_size
= sec
->_raw_size
;
5228 if (sec
->_cooked_size
> max_contents_size
)
5229 max_contents_size
= sec
->_cooked_size
;
5231 /* We are interested in just local symbols, not all
5233 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
5234 && (sec
->owner
->flags
& DYNAMIC
) == 0)
5238 if (elf_bad_symtab (sec
->owner
))
5239 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
5240 / sizeof (Elf_External_Sym
));
5242 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
5244 if (sym_count
> max_sym_count
)
5245 max_sym_count
= sym_count
;
5247 if (sym_count
> max_sym_shndx_count
5248 && elf_symtab_shndx (sec
->owner
) != 0)
5249 max_sym_shndx_count
= sym_count
;
5251 if ((sec
->flags
& SEC_RELOC
) != 0)
5255 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
5256 if (ext_size
> max_external_reloc_size
)
5257 max_external_reloc_size
= ext_size
;
5258 if (sec
->reloc_count
> max_internal_reloc_count
)
5259 max_internal_reloc_count
= sec
->reloc_count
;
5264 if (reloc_count
== 0)
5267 o
->reloc_count
+= reloc_count
;
5269 /* MIPS may have a mix of REL and RELA relocs on sections.
5270 To support this curious ABI we keep reloc counts in
5271 elf_section_data too. We must be careful to add the
5272 relocations from the input section to the right output
5273 count. FIXME: Get rid of one count. We have
5274 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
5275 rel_count1
= &esdo
->rel_count
;
5278 bfd_boolean same_size
;
5279 bfd_size_type entsize1
;
5281 entsize1
= esdi
->rel_hdr
.sh_entsize
;
5282 BFD_ASSERT (entsize1
== sizeof (Elf_External_Rel
)
5283 || entsize1
== sizeof (Elf_External_Rela
));
5284 same_size
= (!o
->use_rela_p
5285 == (entsize1
== sizeof (Elf_External_Rel
)));
5288 rel_count1
= &esdo
->rel_count2
;
5290 if (esdi
->rel_hdr2
!= NULL
)
5292 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
5293 unsigned int alt_count
;
5294 unsigned int *rel_count2
;
5296 BFD_ASSERT (entsize2
!= entsize1
5297 && (entsize2
== sizeof (Elf_External_Rel
)
5298 || entsize2
== sizeof (Elf_External_Rela
)));
5300 rel_count2
= &esdo
->rel_count2
;
5302 rel_count2
= &esdo
->rel_count
;
5304 /* The following is probably too simplistic if the
5305 backend counts output relocs unusually. */
5306 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
5307 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
5308 *rel_count2
+= alt_count
;
5309 reloc_count
-= alt_count
;
5312 *rel_count1
+= reloc_count
;
5315 if (o
->reloc_count
> 0)
5316 o
->flags
|= SEC_RELOC
;
5319 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5320 set it (this is probably a bug) and if it is set
5321 assign_section_numbers will create a reloc section. */
5322 o
->flags
&=~ SEC_RELOC
;
5325 /* If the SEC_ALLOC flag is not set, force the section VMA to
5326 zero. This is done in elf_fake_sections as well, but forcing
5327 the VMA to 0 here will ensure that relocs against these
5328 sections are handled correctly. */
5329 if ((o
->flags
& SEC_ALLOC
) == 0
5330 && ! o
->user_set_vma
)
5334 if (! info
->relocateable
&& merged
)
5335 elf_link_hash_traverse (elf_hash_table (info
),
5336 elf_link_sec_merge_syms
, (PTR
) abfd
);
5338 /* Figure out the file positions for everything but the symbol table
5339 and the relocs. We set symcount to force assign_section_numbers
5340 to create a symbol table. */
5341 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
5342 BFD_ASSERT (! abfd
->output_has_begun
);
5343 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
5346 /* That created the reloc sections. Set their sizes, and assign
5347 them file positions, and allocate some buffers. */
5348 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5350 if ((o
->flags
& SEC_RELOC
) != 0)
5352 if (!elf_link_size_reloc_section (abfd
,
5353 &elf_section_data (o
)->rel_hdr
,
5357 if (elf_section_data (o
)->rel_hdr2
5358 && !elf_link_size_reloc_section (abfd
,
5359 elf_section_data (o
)->rel_hdr2
,
5364 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5365 to count upwards while actually outputting the relocations. */
5366 elf_section_data (o
)->rel_count
= 0;
5367 elf_section_data (o
)->rel_count2
= 0;
5370 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5372 /* We have now assigned file positions for all the sections except
5373 .symtab and .strtab. We start the .symtab section at the current
5374 file position, and write directly to it. We build the .strtab
5375 section in memory. */
5376 bfd_get_symcount (abfd
) = 0;
5377 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5378 /* sh_name is set in prep_headers. */
5379 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5380 /* sh_flags, sh_addr and sh_size all start off zero. */
5381 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
5382 /* sh_link is set in assign_section_numbers. */
5383 /* sh_info is set below. */
5384 /* sh_offset is set just below. */
5385 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
5387 off
= elf_tdata (abfd
)->next_file_pos
;
5388 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
5390 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5391 incorrect. We do not yet know the size of the .symtab section.
5392 We correct next_file_pos below, after we do know the size. */
5394 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5395 continuously seeking to the right position in the file. */
5396 if (! info
->keep_memory
|| max_sym_count
< 20)
5397 finfo
.symbuf_size
= 20;
5399 finfo
.symbuf_size
= max_sym_count
;
5400 amt
= finfo
.symbuf_size
;
5401 amt
*= sizeof (Elf_External_Sym
);
5402 finfo
.symbuf
= (Elf_External_Sym
*) bfd_malloc (amt
);
5403 if (finfo
.symbuf
== NULL
)
5405 if (elf_numsections (abfd
) > SHN_LORESERVE
)
5407 /* Wild guess at number of output symbols. realloc'd as needed. */
5408 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
5409 finfo
.shndxbuf_size
= amt
;
5410 amt
*= sizeof (Elf_External_Sym_Shndx
);
5411 finfo
.symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
5412 if (finfo
.symshndxbuf
== NULL
)
5416 /* Start writing out the symbol table. The first symbol is always a
5418 if (info
->strip
!= strip_all
5421 elfsym
.st_value
= 0;
5424 elfsym
.st_other
= 0;
5425 elfsym
.st_shndx
= SHN_UNDEF
;
5426 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5427 &elfsym
, bfd_und_section_ptr
))
5432 /* Some standard ELF linkers do this, but we don't because it causes
5433 bootstrap comparison failures. */
5434 /* Output a file symbol for the output file as the second symbol.
5435 We output this even if we are discarding local symbols, although
5436 I'm not sure if this is correct. */
5437 elfsym
.st_value
= 0;
5439 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5440 elfsym
.st_other
= 0;
5441 elfsym
.st_shndx
= SHN_ABS
;
5442 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
5443 &elfsym
, bfd_abs_section_ptr
))
5447 /* Output a symbol for each section. We output these even if we are
5448 discarding local symbols, since they are used for relocs. These
5449 symbols have no names. We store the index of each one in the
5450 index field of the section, so that we can find it again when
5451 outputting relocs. */
5452 if (info
->strip
!= strip_all
5456 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5457 elfsym
.st_other
= 0;
5458 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5460 o
= section_from_elf_index (abfd
, i
);
5462 o
->target_index
= bfd_get_symcount (abfd
);
5463 elfsym
.st_shndx
= i
;
5464 if (info
->relocateable
|| o
== NULL
)
5465 elfsym
.st_value
= 0;
5467 elfsym
.st_value
= o
->vma
;
5468 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5471 if (i
== SHN_LORESERVE
- 1)
5472 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
5476 /* Allocate some memory to hold information read in from the input
5478 if (max_contents_size
!= 0)
5480 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
5481 if (finfo
.contents
== NULL
)
5485 if (max_external_reloc_size
!= 0)
5487 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
5488 if (finfo
.external_relocs
== NULL
)
5492 if (max_internal_reloc_count
!= 0)
5494 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5495 amt
*= sizeof (Elf_Internal_Rela
);
5496 finfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
5497 if (finfo
.internal_relocs
== NULL
)
5501 if (max_sym_count
!= 0)
5503 amt
= max_sym_count
* sizeof (Elf_External_Sym
);
5504 finfo
.external_syms
= (Elf_External_Sym
*) bfd_malloc (amt
);
5505 if (finfo
.external_syms
== NULL
)
5508 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
5509 finfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
5510 if (finfo
.internal_syms
== NULL
)
5513 amt
= max_sym_count
* sizeof (long);
5514 finfo
.indices
= (long *) bfd_malloc (amt
);
5515 if (finfo
.indices
== NULL
)
5518 amt
= max_sym_count
* sizeof (asection
*);
5519 finfo
.sections
= (asection
**) bfd_malloc (amt
);
5520 if (finfo
.sections
== NULL
)
5524 if (max_sym_shndx_count
!= 0)
5526 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
5527 finfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
5528 if (finfo
.locsym_shndx
== NULL
)
5532 if (finfo
.first_tls_sec
)
5534 unsigned int align
= 0;
5535 bfd_vma base
= finfo
.first_tls_sec
->vma
, end
= 0;
5538 for (sec
= finfo
.first_tls_sec
;
5539 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
5542 bfd_vma size
= sec
->_raw_size
;
5544 if (bfd_get_section_alignment (abfd
, sec
) > align
)
5545 align
= bfd_get_section_alignment (abfd
, sec
);
5546 if (sec
->_raw_size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
5548 struct bfd_link_order
*o
;
5551 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
5552 if (size
< o
->offset
+ o
->size
)
5553 size
= o
->offset
+ o
->size
;
5555 end
= sec
->vma
+ size
;
5557 elf_hash_table (info
)->tls_segment
5558 = bfd_zalloc (abfd
, sizeof (struct elf_link_tls_segment
));
5559 if (elf_hash_table (info
)->tls_segment
== NULL
)
5561 elf_hash_table (info
)->tls_segment
->start
= base
;
5562 elf_hash_table (info
)->tls_segment
->size
= end
- base
;
5563 elf_hash_table (info
)->tls_segment
->align
= align
;
5566 /* Since ELF permits relocations to be against local symbols, we
5567 must have the local symbols available when we do the relocations.
5568 Since we would rather only read the local symbols once, and we
5569 would rather not keep them in memory, we handle all the
5570 relocations for a single input file at the same time.
5572 Unfortunately, there is no way to know the total number of local
5573 symbols until we have seen all of them, and the local symbol
5574 indices precede the global symbol indices. This means that when
5575 we are generating relocateable output, and we see a reloc against
5576 a global symbol, we can not know the symbol index until we have
5577 finished examining all the local symbols to see which ones we are
5578 going to output. To deal with this, we keep the relocations in
5579 memory, and don't output them until the end of the link. This is
5580 an unfortunate waste of memory, but I don't see a good way around
5581 it. Fortunately, it only happens when performing a relocateable
5582 link, which is not the common case. FIXME: If keep_memory is set
5583 we could write the relocs out and then read them again; I don't
5584 know how bad the memory loss will be. */
5586 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5587 sub
->output_has_begun
= FALSE
;
5588 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5590 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5592 if (p
->type
== bfd_indirect_link_order
5593 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
5594 == bfd_target_elf_flavour
)
5595 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
5597 if (! sub
->output_has_begun
)
5599 if (! elf_link_input_bfd (&finfo
, sub
))
5601 sub
->output_has_begun
= TRUE
;
5604 else if (p
->type
== bfd_section_reloc_link_order
5605 || p
->type
== bfd_symbol_reloc_link_order
)
5607 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
5612 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
5618 /* Output any global symbols that got converted to local in a
5619 version script or due to symbol visibility. We do this in a
5620 separate step since ELF requires all local symbols to appear
5621 prior to any global symbols. FIXME: We should only do this if
5622 some global symbols were, in fact, converted to become local.
5623 FIXME: Will this work correctly with the Irix 5 linker? */
5624 eoinfo
.failed
= FALSE
;
5625 eoinfo
.finfo
= &finfo
;
5626 eoinfo
.localsyms
= TRUE
;
5627 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5632 /* That wrote out all the local symbols. Finish up the symbol table
5633 with the global symbols. Even if we want to strip everything we
5634 can, we still need to deal with those global symbols that got
5635 converted to local in a version script. */
5637 /* The sh_info field records the index of the first non local symbol. */
5638 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
5641 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
5643 Elf_Internal_Sym sym
;
5644 Elf_External_Sym
*dynsym
=
5645 (Elf_External_Sym
*) finfo
.dynsym_sec
->contents
;
5646 long last_local
= 0;
5648 /* Write out the section symbols for the output sections. */
5655 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5658 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5661 Elf_External_Sym
*dest
;
5663 indx
= elf_section_data (s
)->this_idx
;
5664 BFD_ASSERT (indx
> 0);
5665 sym
.st_shndx
= indx
;
5666 sym
.st_value
= s
->vma
;
5667 dest
= dynsym
+ elf_section_data (s
)->dynindx
;
5668 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5671 last_local
= bfd_count_sections (abfd
);
5674 /* Write out the local dynsyms. */
5675 if (elf_hash_table (info
)->dynlocal
)
5677 struct elf_link_local_dynamic_entry
*e
;
5678 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
5681 Elf_External_Sym
*dest
;
5683 sym
.st_size
= e
->isym
.st_size
;
5684 sym
.st_other
= e
->isym
.st_other
;
5686 /* Copy the internal symbol as is.
5687 Note that we saved a word of storage and overwrote
5688 the original st_name with the dynstr_index. */
5691 if (e
->isym
.st_shndx
!= SHN_UNDEF
5692 && (e
->isym
.st_shndx
< SHN_LORESERVE
5693 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
5695 s
= bfd_section_from_elf_index (e
->input_bfd
,
5699 elf_section_data (s
->output_section
)->this_idx
;
5700 sym
.st_value
= (s
->output_section
->vma
5702 + e
->isym
.st_value
);
5705 if (last_local
< e
->dynindx
)
5706 last_local
= e
->dynindx
;
5708 dest
= dynsym
+ e
->dynindx
;
5709 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5713 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
5717 /* We get the global symbols from the hash table. */
5718 eoinfo
.failed
= FALSE
;
5719 eoinfo
.localsyms
= FALSE
;
5720 eoinfo
.finfo
= &finfo
;
5721 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5726 /* If backend needs to output some symbols not present in the hash
5727 table, do it now. */
5728 if (bed
->elf_backend_output_arch_syms
)
5730 typedef bfd_boolean (*out_sym_func
)
5731 PARAMS ((PTR
, const char *, Elf_Internal_Sym
*, asection
*));
5733 if (! ((*bed
->elf_backend_output_arch_syms
)
5734 (abfd
, info
, (PTR
) &finfo
, (out_sym_func
) elf_link_output_sym
)))
5738 /* Flush all symbols to the file. */
5739 if (! elf_link_flush_output_syms (&finfo
))
5742 /* Now we know the size of the symtab section. */
5743 off
+= symtab_hdr
->sh_size
;
5745 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
5746 if (symtab_shndx_hdr
->sh_name
!= 0)
5748 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
5749 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
5750 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
5751 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
5752 symtab_shndx_hdr
->sh_size
= amt
;
5754 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
5757 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
5758 || (bfd_bwrite ((PTR
) finfo
.symshndxbuf
, amt
, abfd
) != amt
))
5763 /* Finish up and write out the symbol string table (.strtab)
5765 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5766 /* sh_name was set in prep_headers. */
5767 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5768 symstrtab_hdr
->sh_flags
= 0;
5769 symstrtab_hdr
->sh_addr
= 0;
5770 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
5771 symstrtab_hdr
->sh_entsize
= 0;
5772 symstrtab_hdr
->sh_link
= 0;
5773 symstrtab_hdr
->sh_info
= 0;
5774 /* sh_offset is set just below. */
5775 symstrtab_hdr
->sh_addralign
= 1;
5777 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
5778 elf_tdata (abfd
)->next_file_pos
= off
;
5780 if (bfd_get_symcount (abfd
) > 0)
5782 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
5783 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
5787 /* Adjust the relocs to have the correct symbol indices. */
5788 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5790 if ((o
->flags
& SEC_RELOC
) == 0)
5793 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
5794 elf_section_data (o
)->rel_count
,
5795 elf_section_data (o
)->rel_hashes
);
5796 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
5797 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
5798 elf_section_data (o
)->rel_count2
,
5799 (elf_section_data (o
)->rel_hashes
5800 + elf_section_data (o
)->rel_count
));
5802 /* Set the reloc_count field to 0 to prevent write_relocs from
5803 trying to swap the relocs out itself. */
5807 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
5808 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
5810 /* If we are linking against a dynamic object, or generating a
5811 shared library, finish up the dynamic linking information. */
5814 Elf_External_Dyn
*dyncon
, *dynconend
;
5816 /* Fix up .dynamic entries. */
5817 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
5818 BFD_ASSERT (o
!= NULL
);
5820 dyncon
= (Elf_External_Dyn
*) o
->contents
;
5821 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
5822 for (; dyncon
< dynconend
; dyncon
++)
5824 Elf_Internal_Dyn dyn
;
5828 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
5835 if (relativecount
> 0 && dyncon
+ 1 < dynconend
)
5837 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
5839 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
5840 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
5843 if (dyn
.d_tag
!= DT_NULL
)
5845 dyn
.d_un
.d_val
= relativecount
;
5846 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5852 name
= info
->init_function
;
5855 name
= info
->fini_function
;
5858 struct elf_link_hash_entry
*h
;
5860 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
5861 FALSE
, FALSE
, TRUE
);
5863 && (h
->root
.type
== bfd_link_hash_defined
5864 || h
->root
.type
== bfd_link_hash_defweak
))
5866 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
5867 o
= h
->root
.u
.def
.section
;
5868 if (o
->output_section
!= NULL
)
5869 dyn
.d_un
.d_val
+= (o
->output_section
->vma
5870 + o
->output_offset
);
5873 /* The symbol is imported from another shared
5874 library and does not apply to this one. */
5878 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5883 case DT_PREINIT_ARRAYSZ
:
5884 name
= ".preinit_array";
5886 case DT_INIT_ARRAYSZ
:
5887 name
= ".init_array";
5889 case DT_FINI_ARRAYSZ
:
5890 name
= ".fini_array";
5892 o
= bfd_get_section_by_name (abfd
, name
);
5895 (*_bfd_error_handler
)
5896 (_("%s: could not find output section %s"),
5897 bfd_get_filename (abfd
), name
);
5900 if (o
->_raw_size
== 0)
5901 (*_bfd_error_handler
)
5902 (_("warning: %s section has zero size"), name
);
5903 dyn
.d_un
.d_val
= o
->_raw_size
;
5904 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5907 case DT_PREINIT_ARRAY
:
5908 name
= ".preinit_array";
5911 name
= ".init_array";
5914 name
= ".fini_array";
5927 name
= ".gnu.version_d";
5930 name
= ".gnu.version_r";
5933 name
= ".gnu.version";
5935 o
= bfd_get_section_by_name (abfd
, name
);
5938 (*_bfd_error_handler
)
5939 (_("%s: could not find output section %s"),
5940 bfd_get_filename (abfd
), name
);
5943 dyn
.d_un
.d_ptr
= o
->vma
;
5944 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5951 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
5956 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5958 Elf_Internal_Shdr
*hdr
;
5960 hdr
= elf_elfsections (abfd
)[i
];
5961 if (hdr
->sh_type
== type
5962 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
5964 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
5965 dyn
.d_un
.d_val
+= hdr
->sh_size
;
5968 if (dyn
.d_un
.d_val
== 0
5969 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
5970 dyn
.d_un
.d_val
= hdr
->sh_addr
;
5974 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5980 /* If we have created any dynamic sections, then output them. */
5983 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
5986 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
5988 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5989 || o
->_raw_size
== 0
5990 || o
->output_section
== bfd_abs_section_ptr
)
5992 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
5994 /* At this point, we are only interested in sections
5995 created by elf_link_create_dynamic_sections. */
5998 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
6000 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
6002 if (! bfd_set_section_contents (abfd
, o
->output_section
,
6004 (file_ptr
) o
->output_offset
,
6010 /* The contents of the .dynstr section are actually in a
6012 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
6013 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
6014 || ! _bfd_elf_strtab_emit (abfd
,
6015 elf_hash_table (info
)->dynstr
))
6021 if (info
->relocateable
)
6023 bfd_boolean failed
= FALSE
;
6025 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
6030 /* If we have optimized stabs strings, output them. */
6031 if (elf_hash_table (info
)->stab_info
!= NULL
)
6033 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
6037 if (info
->eh_frame_hdr
)
6039 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
6043 if (finfo
.symstrtab
!= NULL
)
6044 _bfd_stringtab_free (finfo
.symstrtab
);
6045 if (finfo
.contents
!= NULL
)
6046 free (finfo
.contents
);
6047 if (finfo
.external_relocs
!= NULL
)
6048 free (finfo
.external_relocs
);
6049 if (finfo
.internal_relocs
!= NULL
)
6050 free (finfo
.internal_relocs
);
6051 if (finfo
.external_syms
!= NULL
)
6052 free (finfo
.external_syms
);
6053 if (finfo
.locsym_shndx
!= NULL
)
6054 free (finfo
.locsym_shndx
);
6055 if (finfo
.internal_syms
!= NULL
)
6056 free (finfo
.internal_syms
);
6057 if (finfo
.indices
!= NULL
)
6058 free (finfo
.indices
);
6059 if (finfo
.sections
!= NULL
)
6060 free (finfo
.sections
);
6061 if (finfo
.symbuf
!= NULL
)
6062 free (finfo
.symbuf
);
6063 if (finfo
.symshndxbuf
!= NULL
)
6064 free (finfo
.symshndxbuf
);
6065 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6067 if ((o
->flags
& SEC_RELOC
) != 0
6068 && elf_section_data (o
)->rel_hashes
!= NULL
)
6069 free (elf_section_data (o
)->rel_hashes
);
6072 elf_tdata (abfd
)->linker
= TRUE
;
6077 if (finfo
.symstrtab
!= NULL
)
6078 _bfd_stringtab_free (finfo
.symstrtab
);
6079 if (finfo
.contents
!= NULL
)
6080 free (finfo
.contents
);
6081 if (finfo
.external_relocs
!= NULL
)
6082 free (finfo
.external_relocs
);
6083 if (finfo
.internal_relocs
!= NULL
)
6084 free (finfo
.internal_relocs
);
6085 if (finfo
.external_syms
!= NULL
)
6086 free (finfo
.external_syms
);
6087 if (finfo
.locsym_shndx
!= NULL
)
6088 free (finfo
.locsym_shndx
);
6089 if (finfo
.internal_syms
!= NULL
)
6090 free (finfo
.internal_syms
);
6091 if (finfo
.indices
!= NULL
)
6092 free (finfo
.indices
);
6093 if (finfo
.sections
!= NULL
)
6094 free (finfo
.sections
);
6095 if (finfo
.symbuf
!= NULL
)
6096 free (finfo
.symbuf
);
6097 if (finfo
.symshndxbuf
!= NULL
)
6098 free (finfo
.symshndxbuf
);
6099 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6101 if ((o
->flags
& SEC_RELOC
) != 0
6102 && elf_section_data (o
)->rel_hashes
!= NULL
)
6103 free (elf_section_data (o
)->rel_hashes
);
6109 /* Add a symbol to the output symbol table. */
6112 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
6113 struct elf_final_link_info
*finfo
;
6115 Elf_Internal_Sym
*elfsym
;
6116 asection
*input_sec
;
6118 Elf_External_Sym
*dest
;
6119 Elf_External_Sym_Shndx
*destshndx
;
6120 bfd_boolean (*output_symbol_hook
)
6121 PARAMS ((bfd
*, struct bfd_link_info
*info
, const char *,
6122 Elf_Internal_Sym
*, asection
*));
6124 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
6125 elf_backend_link_output_symbol_hook
;
6126 if (output_symbol_hook
!= NULL
)
6128 if (! ((*output_symbol_hook
)
6129 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
6133 if (name
== (const char *) NULL
|| *name
== '\0')
6134 elfsym
->st_name
= 0;
6135 else if (input_sec
->flags
& SEC_EXCLUDE
)
6136 elfsym
->st_name
= 0;
6139 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
6141 if (elfsym
->st_name
== (unsigned long) -1)
6145 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
6147 if (! elf_link_flush_output_syms (finfo
))
6151 dest
= finfo
->symbuf
+ finfo
->symbuf_count
;
6152 destshndx
= finfo
->symshndxbuf
;
6153 if (destshndx
!= NULL
)
6155 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
6159 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
6160 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
6161 if (destshndx
== NULL
)
6163 memset ((char *) destshndx
+ amt
, 0, amt
);
6164 finfo
->shndxbuf_size
*= 2;
6166 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
6169 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
, (PTR
) dest
, (PTR
) destshndx
);
6170 finfo
->symbuf_count
+= 1;
6171 bfd_get_symcount (finfo
->output_bfd
) += 1;
6176 /* Flush the output symbols to the file. */
6179 elf_link_flush_output_syms (finfo
)
6180 struct elf_final_link_info
*finfo
;
6182 if (finfo
->symbuf_count
> 0)
6184 Elf_Internal_Shdr
*hdr
;
6188 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6189 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6190 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
6191 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6192 || bfd_bwrite ((PTR
) finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6195 hdr
->sh_size
+= amt
;
6196 finfo
->symbuf_count
= 0;
6202 /* Adjust all external symbols pointing into SEC_MERGE sections
6203 to reflect the object merging within the sections. */
6206 elf_link_sec_merge_syms (h
, data
)
6207 struct elf_link_hash_entry
*h
;
6212 if (h
->root
.type
== bfd_link_hash_warning
)
6213 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6215 if ((h
->root
.type
== bfd_link_hash_defined
6216 || h
->root
.type
== bfd_link_hash_defweak
)
6217 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
6218 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
6220 bfd
*output_bfd
= (bfd
*) data
;
6222 h
->root
.u
.def
.value
=
6223 _bfd_merged_section_offset (output_bfd
,
6224 &h
->root
.u
.def
.section
,
6225 elf_section_data (sec
)->sec_info
,
6226 h
->root
.u
.def
.value
, (bfd_vma
) 0);
6232 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6233 allowing an unsatisfied unversioned symbol in the DSO to match a
6234 versioned symbol that would normally require an explicit version.
6235 We also handle the case that a DSO references a hidden symbol
6236 which may be satisfied by a versioned symbol in another DSO. */
6239 elf_link_check_versioned_symbol (info
, h
)
6240 struct bfd_link_info
*info
;
6241 struct elf_link_hash_entry
*h
;
6244 struct elf_link_loaded_list
*loaded
;
6246 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
6249 switch (h
->root
.type
)
6255 case bfd_link_hash_undefined
:
6256 case bfd_link_hash_undefweak
:
6257 abfd
= h
->root
.u
.undef
.abfd
;
6258 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dt_soname (abfd
) == NULL
)
6262 case bfd_link_hash_defined
:
6263 case bfd_link_hash_defweak
:
6264 abfd
= h
->root
.u
.def
.section
->owner
;
6267 case bfd_link_hash_common
:
6268 abfd
= h
->root
.u
.c
.p
->section
->owner
;
6271 BFD_ASSERT (abfd
!= NULL
);
6273 for (loaded
= elf_hash_table (info
)->loaded
;
6275 loaded
= loaded
->next
)
6278 Elf_Internal_Shdr
*hdr
;
6279 bfd_size_type symcount
;
6280 bfd_size_type extsymcount
;
6281 bfd_size_type extsymoff
;
6282 Elf_Internal_Shdr
*versymhdr
;
6283 Elf_Internal_Sym
*isym
;
6284 Elf_Internal_Sym
*isymend
;
6285 Elf_Internal_Sym
*isymbuf
;
6286 Elf_External_Versym
*ever
;
6287 Elf_External_Versym
*extversym
;
6289 input
= loaded
->abfd
;
6291 /* We check each DSO for a possible hidden versioned definition. */
6293 || (input
->flags
& DYNAMIC
) == 0
6294 || elf_dynversym (input
) == 0)
6297 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6299 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6300 if (elf_bad_symtab (input
))
6302 extsymcount
= symcount
;
6307 extsymcount
= symcount
- hdr
->sh_info
;
6308 extsymoff
= hdr
->sh_info
;
6311 if (extsymcount
== 0)
6314 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6316 if (isymbuf
== NULL
)
6319 /* Read in any version definitions. */
6320 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6321 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
6322 if (extversym
== NULL
)
6325 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6326 || (bfd_bread ((PTR
) extversym
, versymhdr
->sh_size
, input
)
6327 != versymhdr
->sh_size
))
6335 ever
= extversym
+ extsymoff
;
6336 isymend
= isymbuf
+ extsymcount
;
6337 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6340 Elf_Internal_Versym iver
;
6342 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6343 || isym
->st_shndx
== SHN_UNDEF
)
6346 name
= bfd_elf_string_from_elf_section (input
,
6349 if (strcmp (name
, h
->root
.root
.string
) != 0)
6352 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6354 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6356 /* If we have a non-hidden versioned sym, then it should
6357 have provided a definition for the undefined sym. */
6361 if ((iver
.vs_vers
& VERSYM_VERSION
) == 2)
6363 /* This is the oldest (default) sym. We can use it. */
6377 /* Add an external symbol to the symbol table. This is called from
6378 the hash table traversal routine. When generating a shared object,
6379 we go through the symbol table twice. The first time we output
6380 anything that might have been forced to local scope in a version
6381 script. The second time we output the symbols that are still
6385 elf_link_output_extsym (h
, data
)
6386 struct elf_link_hash_entry
*h
;
6389 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
6390 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6392 Elf_Internal_Sym sym
;
6393 asection
*input_sec
;
6395 if (h
->root
.type
== bfd_link_hash_warning
)
6397 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6398 if (h
->root
.type
== bfd_link_hash_new
)
6402 /* Decide whether to output this symbol in this pass. */
6403 if (eoinfo
->localsyms
)
6405 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6410 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6414 /* If we are not creating a shared library, and this symbol is
6415 referenced by a shared library but is not defined anywhere, then
6416 warn that it is undefined. If we do not do this, the runtime
6417 linker will complain that the symbol is undefined when the
6418 program is run. We don't have to worry about symbols that are
6419 referenced by regular files, because we will already have issued
6420 warnings for them. */
6421 if (! finfo
->info
->relocateable
6422 && (! finfo
->info
->shared
|| ! finfo
->info
->allow_shlib_undefined
)
6423 && h
->root
.type
== bfd_link_hash_undefined
6424 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
6425 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
6426 && ! elf_link_check_versioned_symbol (finfo
->info
, h
))
6428 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6429 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6430 (asection
*) NULL
, (bfd_vma
) 0, TRUE
)))
6432 eoinfo
->failed
= TRUE
;
6437 /* We should also warn if a forced local symbol is referenced from
6438 shared libraries. */
6439 if (! finfo
->info
->relocateable
6440 && (! finfo
->info
->shared
|| ! finfo
->info
->allow_shlib_undefined
)
6441 && (h
->elf_link_hash_flags
6442 & (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
6443 | ELF_LINK_DYNAMIC_DEF
| ELF_LINK_DYNAMIC_WEAK
))
6444 == (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
)
6445 && ! elf_link_check_versioned_symbol (finfo
->info
, h
))
6447 (*_bfd_error_handler
)
6448 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
6449 bfd_get_filename (finfo
->output_bfd
),
6450 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6452 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6453 ? "hidden" : "local",
6454 h
->root
.root
.string
,
6455 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
6456 eoinfo
->failed
= TRUE
;
6460 /* We don't want to output symbols that have never been mentioned by
6461 a regular file, or that we have been told to strip. However, if
6462 h->indx is set to -2, the symbol is used by a reloc and we must
6466 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
6467 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
6468 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
6469 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
6471 else if (finfo
->info
->strip
== strip_all
)
6473 else if (finfo
->info
->strip
== strip_some
6474 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6475 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6477 else if (finfo
->info
->strip_discarded
6478 && (h
->root
.type
== bfd_link_hash_defined
6479 || h
->root
.type
== bfd_link_hash_defweak
)
6480 && elf_discarded_section (h
->root
.u
.def
.section
))
6485 /* If we're stripping it, and it's not a dynamic symbol, there's
6486 nothing else to do unless it is a forced local symbol. */
6489 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6493 sym
.st_size
= h
->size
;
6494 sym
.st_other
= h
->other
;
6495 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6496 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6497 else if (h
->root
.type
== bfd_link_hash_undefweak
6498 || h
->root
.type
== bfd_link_hash_defweak
)
6499 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6501 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6503 switch (h
->root
.type
)
6506 case bfd_link_hash_new
:
6507 case bfd_link_hash_warning
:
6511 case bfd_link_hash_undefined
:
6512 case bfd_link_hash_undefweak
:
6513 input_sec
= bfd_und_section_ptr
;
6514 sym
.st_shndx
= SHN_UNDEF
;
6517 case bfd_link_hash_defined
:
6518 case bfd_link_hash_defweak
:
6520 input_sec
= h
->root
.u
.def
.section
;
6521 if (input_sec
->output_section
!= NULL
)
6524 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6525 input_sec
->output_section
);
6526 if (sym
.st_shndx
== SHN_BAD
)
6528 (*_bfd_error_handler
)
6529 (_("%s: could not find output section %s for input section %s"),
6530 bfd_get_filename (finfo
->output_bfd
),
6531 input_sec
->output_section
->name
,
6533 eoinfo
->failed
= TRUE
;
6537 /* ELF symbols in relocateable files are section relative,
6538 but in nonrelocateable files they are virtual
6540 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6541 if (! finfo
->info
->relocateable
)
6543 sym
.st_value
+= input_sec
->output_section
->vma
;
6544 if (h
->type
== STT_TLS
)
6546 /* STT_TLS symbols are relative to PT_TLS segment
6548 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6549 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
6555 BFD_ASSERT (input_sec
->owner
== NULL
6556 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6557 sym
.st_shndx
= SHN_UNDEF
;
6558 input_sec
= bfd_und_section_ptr
;
6563 case bfd_link_hash_common
:
6564 input_sec
= h
->root
.u
.c
.p
->section
;
6565 sym
.st_shndx
= SHN_COMMON
;
6566 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6569 case bfd_link_hash_indirect
:
6570 /* These symbols are created by symbol versioning. They point
6571 to the decorated version of the name. For example, if the
6572 symbol foo@@GNU_1.2 is the default, which should be used when
6573 foo is used with no version, then we add an indirect symbol
6574 foo which points to foo@@GNU_1.2. We ignore these symbols,
6575 since the indirected symbol is already in the hash table. */
6579 /* Give the processor backend a chance to tweak the symbol value,
6580 and also to finish up anything that needs to be done for this
6581 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6582 forced local syms when non-shared is due to a historical quirk. */
6583 if ((h
->dynindx
!= -1
6584 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6585 && ((finfo
->info
->shared
6586 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6587 || h
->root
.type
!= bfd_link_hash_undefweak
))
6588 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6589 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6591 struct elf_backend_data
*bed
;
6593 bed
= get_elf_backend_data (finfo
->output_bfd
);
6594 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6595 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6597 eoinfo
->failed
= TRUE
;
6602 /* If we are marking the symbol as undefined, and there are no
6603 non-weak references to this symbol from a regular object, then
6604 mark the symbol as weak undefined; if there are non-weak
6605 references, mark the symbol as strong. We can't do this earlier,
6606 because it might not be marked as undefined until the
6607 finish_dynamic_symbol routine gets through with it. */
6608 if (sym
.st_shndx
== SHN_UNDEF
6609 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
6610 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6611 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6615 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
6616 bindtype
= STB_GLOBAL
;
6618 bindtype
= STB_WEAK
;
6619 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6622 /* If a non-weak symbol with non-default visibility is not defined
6623 locally, it is a fatal error. */
6624 if (! finfo
->info
->relocateable
6625 && ELF_ST_VISIBILITY (sym
.st_other
)
6626 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6627 && h
->root
.type
!= bfd_link_hash_undefweak
6628 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6630 (*_bfd_error_handler
)
6631 (_("%s: %s symbol `%s' isn't defined"),
6632 bfd_get_filename (finfo
->output_bfd
),
6633 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6635 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6636 ? "internal" : "hidden",
6637 h
->root
.root
.string
);
6638 eoinfo
->failed
= TRUE
;
6642 /* If this symbol should be put in the .dynsym section, then put it
6643 there now. We already know the symbol index. We also fill in
6644 the entry in the .hash section. */
6645 if (h
->dynindx
!= -1
6646 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6650 size_t hash_entry_size
;
6651 bfd_byte
*bucketpos
;
6653 Elf_External_Sym
*esym
;
6655 sym
.st_name
= h
->dynstr_index
;
6656 esym
= (Elf_External_Sym
*) finfo
->dynsym_sec
->contents
+ h
->dynindx
;
6657 elf_swap_symbol_out (finfo
->output_bfd
, &sym
, (PTR
) esym
, (PTR
) 0);
6659 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6660 bucket
= h
->elf_hash_value
% bucketcount
;
6662 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6663 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6664 + (bucket
+ 2) * hash_entry_size
);
6665 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6666 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, (bfd_vma
) h
->dynindx
,
6668 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6669 ((bfd_byte
*) finfo
->hash_sec
->contents
6670 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6672 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6674 Elf_Internal_Versym iversym
;
6675 Elf_External_Versym
*eversym
;
6677 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6679 if (h
->verinfo
.verdef
== NULL
)
6680 iversym
.vs_vers
= 0;
6682 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6686 if (h
->verinfo
.vertree
== NULL
)
6687 iversym
.vs_vers
= 1;
6689 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6692 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
6693 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6695 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6696 eversym
+= h
->dynindx
;
6697 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6701 /* If we're stripping it, then it was just a dynamic symbol, and
6702 there's nothing else to do. */
6703 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6706 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6708 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
6710 eoinfo
->failed
= TRUE
;
6717 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6718 originated from the section given by INPUT_REL_HDR) to the
6722 elf_link_output_relocs (output_bfd
, input_section
, input_rel_hdr
,
6725 asection
*input_section
;
6726 Elf_Internal_Shdr
*input_rel_hdr
;
6727 Elf_Internal_Rela
*internal_relocs
;
6729 Elf_Internal_Rela
*irela
;
6730 Elf_Internal_Rela
*irelaend
;
6732 Elf_Internal_Shdr
*output_rel_hdr
;
6733 asection
*output_section
;
6734 unsigned int *rel_countp
= NULL
;
6735 struct elf_backend_data
*bed
;
6736 void (*swap_out
) PARAMS ((bfd
*, const Elf_Internal_Rela
*, bfd_byte
*));
6738 output_section
= input_section
->output_section
;
6739 output_rel_hdr
= NULL
;
6741 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
6742 == input_rel_hdr
->sh_entsize
)
6744 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
6745 rel_countp
= &elf_section_data (output_section
)->rel_count
;
6747 else if (elf_section_data (output_section
)->rel_hdr2
6748 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
6749 == input_rel_hdr
->sh_entsize
))
6751 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
6752 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
6756 (*_bfd_error_handler
)
6757 (_("%s: relocation size mismatch in %s section %s"),
6758 bfd_get_filename (output_bfd
),
6759 bfd_archive_filename (input_section
->owner
),
6760 input_section
->name
);
6761 bfd_set_error (bfd_error_wrong_object_format
);
6765 bed
= get_elf_backend_data (output_bfd
);
6766 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
6767 swap_out
= bed
->s
->swap_reloc_out
;
6768 else if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
))
6769 swap_out
= bed
->s
->swap_reloca_out
;
6773 erel
= output_rel_hdr
->contents
;
6774 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
6775 irela
= internal_relocs
;
6776 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
6777 * bed
->s
->int_rels_per_ext_rel
);
6778 while (irela
< irelaend
)
6780 (*swap_out
) (output_bfd
, irela
, erel
);
6781 irela
+= bed
->s
->int_rels_per_ext_rel
;
6782 erel
+= input_rel_hdr
->sh_entsize
;
6785 /* Bump the counter, so that we know where to add the next set of
6787 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
6792 /* Link an input file into the linker output file. This function
6793 handles all the sections and relocations of the input file at once.
6794 This is so that we only have to read the local symbols once, and
6795 don't have to keep them in memory. */
6798 elf_link_input_bfd (finfo
, input_bfd
)
6799 struct elf_final_link_info
*finfo
;
6802 bfd_boolean (*relocate_section
)
6803 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6804 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
6806 Elf_Internal_Shdr
*symtab_hdr
;
6809 Elf_Internal_Sym
*isymbuf
;
6810 Elf_Internal_Sym
*isym
;
6811 Elf_Internal_Sym
*isymend
;
6813 asection
**ppsection
;
6815 struct elf_backend_data
*bed
;
6816 bfd_boolean emit_relocs
;
6817 struct elf_link_hash_entry
**sym_hashes
;
6819 output_bfd
= finfo
->output_bfd
;
6820 bed
= get_elf_backend_data (output_bfd
);
6821 relocate_section
= bed
->elf_backend_relocate_section
;
6823 /* If this is a dynamic object, we don't want to do anything here:
6824 we don't want the local symbols, and we don't want the section
6826 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6829 emit_relocs
= (finfo
->info
->relocateable
6830 || finfo
->info
->emitrelocations
6831 || bed
->elf_backend_emit_relocs
);
6833 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6834 if (elf_bad_symtab (input_bfd
))
6836 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6841 locsymcount
= symtab_hdr
->sh_info
;
6842 extsymoff
= symtab_hdr
->sh_info
;
6845 /* Read the local symbols. */
6846 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6847 if (isymbuf
== NULL
&& locsymcount
!= 0)
6849 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6850 finfo
->internal_syms
,
6851 finfo
->external_syms
,
6852 finfo
->locsym_shndx
);
6853 if (isymbuf
== NULL
)
6857 /* Find local symbol sections and adjust values of symbols in
6858 SEC_MERGE sections. Write out those local symbols we know are
6859 going into the output file. */
6860 isymend
= isymbuf
+ locsymcount
;
6861 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6863 isym
++, pindex
++, ppsection
++)
6867 Elf_Internal_Sym osym
;
6871 if (elf_bad_symtab (input_bfd
))
6873 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6880 if (isym
->st_shndx
== SHN_UNDEF
)
6881 isec
= bfd_und_section_ptr
;
6882 else if (isym
->st_shndx
< SHN_LORESERVE
6883 || isym
->st_shndx
> SHN_HIRESERVE
)
6885 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
6887 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6888 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6890 _bfd_merged_section_offset (output_bfd
, &isec
,
6891 elf_section_data (isec
)->sec_info
,
6892 isym
->st_value
, (bfd_vma
) 0);
6894 else if (isym
->st_shndx
== SHN_ABS
)
6895 isec
= bfd_abs_section_ptr
;
6896 else if (isym
->st_shndx
== SHN_COMMON
)
6897 isec
= bfd_com_section_ptr
;
6906 /* Don't output the first, undefined, symbol. */
6907 if (ppsection
== finfo
->sections
)
6910 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6912 /* We never output section symbols. Instead, we use the
6913 section symbol of the corresponding section in the output
6918 /* If we are stripping all symbols, we don't want to output this
6920 if (finfo
->info
->strip
== strip_all
)
6923 /* If we are discarding all local symbols, we don't want to
6924 output this one. If we are generating a relocateable output
6925 file, then some of the local symbols may be required by
6926 relocs; we output them below as we discover that they are
6928 if (finfo
->info
->discard
== discard_all
)
6931 /* If this symbol is defined in a section which we are
6932 discarding, we don't need to keep it, but note that
6933 linker_mark is only reliable for sections that have contents.
6934 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6935 as well as linker_mark. */
6936 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6938 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6939 || (! finfo
->info
->relocateable
6940 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6943 /* Get the name of the symbol. */
6944 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6949 /* See if we are discarding symbols with this name. */
6950 if ((finfo
->info
->strip
== strip_some
6951 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6953 || (((finfo
->info
->discard
== discard_sec_merge
6954 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocateable
)
6955 || finfo
->info
->discard
== discard_l
)
6956 && bfd_is_local_label_name (input_bfd
, name
)))
6959 /* If we get here, we are going to output this symbol. */
6963 /* Adjust the section index for the output file. */
6964 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6965 isec
->output_section
);
6966 if (osym
.st_shndx
== SHN_BAD
)
6969 *pindex
= bfd_get_symcount (output_bfd
);
6971 /* ELF symbols in relocateable files are section relative, but
6972 in executable files they are virtual addresses. Note that
6973 this code assumes that all ELF sections have an associated
6974 BFD section with a reasonable value for output_offset; below
6975 we assume that they also have a reasonable value for
6976 output_section. Any special sections must be set up to meet
6977 these requirements. */
6978 osym
.st_value
+= isec
->output_offset
;
6979 if (! finfo
->info
->relocateable
)
6981 osym
.st_value
+= isec
->output_section
->vma
;
6982 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6984 /* STT_TLS symbols are relative to PT_TLS segment base. */
6985 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6986 osym
.st_value
-= finfo
->first_tls_sec
->vma
;
6990 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
6994 /* Relocate the contents of each section. */
6995 sym_hashes
= elf_sym_hashes (input_bfd
);
6996 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
7000 if (! o
->linker_mark
)
7002 /* This section was omitted from the link. */
7006 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
7007 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
7010 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
7012 /* Section was created by elf_link_create_dynamic_sections
7017 /* Get the contents of the section. They have been cached by a
7018 relaxation routine. Note that o is a section in an input
7019 file, so the contents field will not have been set by any of
7020 the routines which work on output files. */
7021 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
7022 contents
= elf_section_data (o
)->this_hdr
.contents
;
7025 contents
= finfo
->contents
;
7026 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
7027 (file_ptr
) 0, o
->_raw_size
))
7031 if ((o
->flags
& SEC_RELOC
) != 0)
7033 Elf_Internal_Rela
*internal_relocs
;
7035 /* Get the swapped relocs. */
7036 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
7037 (input_bfd
, o
, finfo
->external_relocs
,
7038 finfo
->internal_relocs
, FALSE
));
7039 if (internal_relocs
== NULL
7040 && o
->reloc_count
> 0)
7043 /* Run through the relocs looking for any against symbols
7044 from discarded sections and section symbols from
7045 removed link-once sections. Complain about relocs
7046 against discarded sections. Zero relocs against removed
7047 link-once sections. */
7048 if (!finfo
->info
->relocateable
7049 && !elf_section_ignore_discarded_relocs (o
))
7051 Elf_Internal_Rela
*rel
, *relend
;
7053 rel
= internal_relocs
;
7054 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7055 for ( ; rel
< relend
; rel
++)
7057 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7059 if (r_symndx
>= locsymcount
7060 || (elf_bad_symtab (input_bfd
)
7061 && finfo
->sections
[r_symndx
] == NULL
))
7063 struct elf_link_hash_entry
*h
;
7065 h
= sym_hashes
[r_symndx
- extsymoff
];
7066 while (h
->root
.type
== bfd_link_hash_indirect
7067 || h
->root
.type
== bfd_link_hash_warning
)
7068 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7070 /* Complain if the definition comes from a
7071 discarded section. */
7072 if ((h
->root
.type
== bfd_link_hash_defined
7073 || h
->root
.type
== bfd_link_hash_defweak
)
7074 && elf_discarded_section (h
->root
.u
.def
.section
))
7076 if ((o
->flags
& SEC_DEBUGGING
) != 0)
7078 BFD_ASSERT (r_symndx
!= 0);
7079 memset (rel
, 0, sizeof (*rel
));
7083 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
7084 (finfo
->info
, h
->root
.root
.string
,
7085 input_bfd
, o
, rel
->r_offset
,
7093 asection
*sec
= finfo
->sections
[r_symndx
];
7095 if (sec
!= NULL
&& elf_discarded_section (sec
))
7097 if ((o
->flags
& SEC_DEBUGGING
) != 0
7098 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
7100 BFD_ASSERT (r_symndx
!= 0);
7102 = ELF_R_INFO (0, ELF_R_TYPE (rel
->r_info
));
7109 = _("local symbols in discarded section %s");
7111 = strlen (sec
->name
) + strlen (msg
) - 1;
7112 char *buf
= (char *) bfd_malloc (amt
);
7115 sprintf (buf
, msg
, sec
->name
);
7117 buf
= (char *) sec
->name
;
7118 ok
= (*finfo
->info
->callbacks
7119 ->undefined_symbol
) (finfo
->info
, buf
,
7123 if (buf
!= sec
->name
)
7133 /* Relocate the section by invoking a back end routine.
7135 The back end routine is responsible for adjusting the
7136 section contents as necessary, and (if using Rela relocs
7137 and generating a relocateable output file) adjusting the
7138 reloc addend as necessary.
7140 The back end routine does not have to worry about setting
7141 the reloc address or the reloc symbol index.
7143 The back end routine is given a pointer to the swapped in
7144 internal symbols, and can access the hash table entries
7145 for the external symbols via elf_sym_hashes (input_bfd).
7147 When generating relocateable output, the back end routine
7148 must handle STB_LOCAL/STT_SECTION symbols specially. The
7149 output symbol is going to be a section symbol
7150 corresponding to the output section, which will require
7151 the addend to be adjusted. */
7153 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
7154 input_bfd
, o
, contents
,
7162 Elf_Internal_Rela
*irela
;
7163 Elf_Internal_Rela
*irelaend
;
7164 bfd_vma last_offset
;
7165 struct elf_link_hash_entry
**rel_hash
;
7166 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
7167 unsigned int next_erel
;
7168 bfd_boolean (*reloc_emitter
)
7169 PARAMS ((bfd
*, asection
*, Elf_Internal_Shdr
*,
7170 Elf_Internal_Rela
*));
7171 bfd_boolean rela_normal
;
7173 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7174 rela_normal
= (bed
->rela_normal
7175 && (input_rel_hdr
->sh_entsize
7176 == sizeof (Elf_External_Rela
)));
7178 /* Adjust the reloc addresses and symbol indices. */
7180 irela
= internal_relocs
;
7181 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7182 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7183 + elf_section_data (o
->output_section
)->rel_count
7184 + elf_section_data (o
->output_section
)->rel_count2
);
7185 last_offset
= o
->output_offset
;
7186 if (!finfo
->info
->relocateable
)
7187 last_offset
+= o
->output_section
->vma
;
7188 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7190 unsigned long r_symndx
;
7192 Elf_Internal_Sym sym
;
7194 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7200 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
7203 if (irela
->r_offset
>= (bfd_vma
) -2)
7205 /* This is a reloc for a deleted entry or somesuch.
7206 Turn it into an R_*_NONE reloc, at the same
7207 offset as the last reloc. elf_eh_frame.c and
7208 elf_bfd_discard_info rely on reloc offsets
7210 irela
->r_offset
= last_offset
;
7212 irela
->r_addend
= 0;
7216 irela
->r_offset
+= o
->output_offset
;
7218 /* Relocs in an executable have to be virtual addresses. */
7219 if (!finfo
->info
->relocateable
)
7220 irela
->r_offset
+= o
->output_section
->vma
;
7222 last_offset
= irela
->r_offset
;
7224 r_symndx
= ELF_R_SYM (irela
->r_info
);
7225 if (r_symndx
== STN_UNDEF
)
7228 if (r_symndx
>= locsymcount
7229 || (elf_bad_symtab (input_bfd
)
7230 && finfo
->sections
[r_symndx
] == NULL
))
7232 struct elf_link_hash_entry
*rh
;
7235 /* This is a reloc against a global symbol. We
7236 have not yet output all the local symbols, so
7237 we do not know the symbol index of any global
7238 symbol. We set the rel_hash entry for this
7239 reloc to point to the global hash table entry
7240 for this symbol. The symbol index is then
7241 set at the end of elf_bfd_final_link. */
7242 indx
= r_symndx
- extsymoff
;
7243 rh
= elf_sym_hashes (input_bfd
)[indx
];
7244 while (rh
->root
.type
== bfd_link_hash_indirect
7245 || rh
->root
.type
== bfd_link_hash_warning
)
7246 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7248 /* Setting the index to -2 tells
7249 elf_link_output_extsym that this symbol is
7251 BFD_ASSERT (rh
->indx
< 0);
7259 /* This is a reloc against a local symbol. */
7262 sym
= isymbuf
[r_symndx
];
7263 sec
= finfo
->sections
[r_symndx
];
7264 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7266 /* I suppose the backend ought to fill in the
7267 section of any STT_SECTION symbol against a
7268 processor specific section. If we have
7269 discarded a section, the output_section will
7270 be the absolute section. */
7271 if (bfd_is_abs_section (sec
)
7273 && bfd_is_abs_section (sec
->output_section
)))
7275 else if (sec
== NULL
|| sec
->owner
== NULL
)
7277 bfd_set_error (bfd_error_bad_value
);
7282 r_symndx
= sec
->output_section
->target_index
;
7283 BFD_ASSERT (r_symndx
!= 0);
7286 /* Adjust the addend according to where the
7287 section winds up in the output section. */
7289 irela
->r_addend
+= sec
->output_offset
;
7293 if (finfo
->indices
[r_symndx
] == -1)
7295 unsigned long shlink
;
7299 if (finfo
->info
->strip
== strip_all
)
7301 /* You can't do ld -r -s. */
7302 bfd_set_error (bfd_error_invalid_operation
);
7306 /* This symbol was skipped earlier, but
7307 since it is needed by a reloc, we
7308 must output it now. */
7309 shlink
= symtab_hdr
->sh_link
;
7310 name
= (bfd_elf_string_from_elf_section
7311 (input_bfd
, shlink
, sym
.st_name
));
7315 osec
= sec
->output_section
;
7317 _bfd_elf_section_from_bfd_section (output_bfd
,
7319 if (sym
.st_shndx
== SHN_BAD
)
7322 sym
.st_value
+= sec
->output_offset
;
7323 if (! finfo
->info
->relocateable
)
7325 sym
.st_value
+= osec
->vma
;
7326 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7328 /* STT_TLS symbols are relative to PT_TLS
7330 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
7331 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
7335 finfo
->indices
[r_symndx
]
7336 = bfd_get_symcount (output_bfd
);
7338 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
))
7342 r_symndx
= finfo
->indices
[r_symndx
];
7345 irela
->r_info
= ELF_R_INFO (r_symndx
,
7346 ELF_R_TYPE (irela
->r_info
));
7349 /* Swap out the relocs. */
7350 if (bed
->elf_backend_emit_relocs
7351 && !(finfo
->info
->relocateable
7352 || finfo
->info
->emitrelocations
))
7353 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7355 reloc_emitter
= elf_link_output_relocs
;
7357 if (input_rel_hdr
->sh_size
!= 0
7358 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7362 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7363 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7365 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7366 * bed
->s
->int_rels_per_ext_rel
);
7367 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
7374 /* Write out the modified section contents. */
7375 if (bed
->elf_backend_write_section
7376 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7378 /* Section written out. */
7380 else switch (o
->sec_info_type
)
7382 case ELF_INFO_TYPE_STABS
:
7383 if (! (_bfd_write_section_stabs
7385 &elf_hash_table (finfo
->info
)->stab_info
,
7386 o
, &elf_section_data (o
)->sec_info
, contents
)))
7389 case ELF_INFO_TYPE_MERGE
:
7390 if (! _bfd_write_merged_section (output_bfd
, o
,
7391 elf_section_data (o
)->sec_info
))
7394 case ELF_INFO_TYPE_EH_FRAME
:
7396 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7403 bfd_size_type sec_size
;
7405 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
7406 if (! (o
->flags
& SEC_EXCLUDE
)
7407 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7409 (file_ptr
) o
->output_offset
,
7420 /* Generate a reloc when linking an ELF file. This is a reloc
7421 requested by the linker, and does come from any input file. This
7422 is used to build constructor and destructor tables when linking
7426 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
7428 struct bfd_link_info
*info
;
7429 asection
*output_section
;
7430 struct bfd_link_order
*link_order
;
7432 reloc_howto_type
*howto
;
7436 struct elf_link_hash_entry
**rel_hash_ptr
;
7437 Elf_Internal_Shdr
*rel_hdr
;
7438 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7439 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7443 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7446 bfd_set_error (bfd_error_bad_value
);
7450 addend
= link_order
->u
.reloc
.p
->addend
;
7452 /* Figure out the symbol index. */
7453 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7454 + elf_section_data (output_section
)->rel_count
7455 + elf_section_data (output_section
)->rel_count2
);
7456 if (link_order
->type
== bfd_section_reloc_link_order
)
7458 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7459 BFD_ASSERT (indx
!= 0);
7460 *rel_hash_ptr
= NULL
;
7464 struct elf_link_hash_entry
*h
;
7466 /* Treat a reloc against a defined symbol as though it were
7467 actually against the section. */
7468 h
= ((struct elf_link_hash_entry
*)
7469 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7470 link_order
->u
.reloc
.p
->u
.name
,
7471 FALSE
, FALSE
, TRUE
));
7473 && (h
->root
.type
== bfd_link_hash_defined
7474 || h
->root
.type
== bfd_link_hash_defweak
))
7478 section
= h
->root
.u
.def
.section
;
7479 indx
= section
->output_section
->target_index
;
7480 *rel_hash_ptr
= NULL
;
7481 /* It seems that we ought to add the symbol value to the
7482 addend here, but in practice it has already been added
7483 because it was passed to constructor_callback. */
7484 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7488 /* Setting the index to -2 tells elf_link_output_extsym that
7489 this symbol is used by a reloc. */
7496 if (! ((*info
->callbacks
->unattached_reloc
)
7497 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
7498 (asection
*) NULL
, (bfd_vma
) 0)))
7504 /* If this is an inplace reloc, we must write the addend into the
7506 if (howto
->partial_inplace
&& addend
!= 0)
7509 bfd_reloc_status_type rstat
;
7512 const char *sym_name
;
7514 size
= bfd_get_reloc_size (howto
);
7515 buf
= (bfd_byte
*) bfd_zmalloc (size
);
7516 if (buf
== (bfd_byte
*) NULL
)
7518 rstat
= _bfd_relocate_contents (howto
, output_bfd
, (bfd_vma
) addend
, buf
);
7525 case bfd_reloc_outofrange
:
7528 case bfd_reloc_overflow
:
7529 if (link_order
->type
== bfd_section_reloc_link_order
)
7530 sym_name
= bfd_section_name (output_bfd
,
7531 link_order
->u
.reloc
.p
->u
.section
);
7533 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7534 if (! ((*info
->callbacks
->reloc_overflow
)
7535 (info
, sym_name
, howto
->name
, addend
,
7536 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
7543 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
7544 (file_ptr
) link_order
->offset
, size
);
7550 /* The address of a reloc is relative to the section in a
7551 relocateable file, and is a virtual address in an executable
7553 offset
= link_order
->offset
;
7554 if (! info
->relocateable
)
7555 offset
+= output_section
->vma
;
7557 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7559 irel
[i
].r_offset
= offset
;
7561 irel
[i
].r_addend
= 0;
7563 irel
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
7565 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7566 erel
= rel_hdr
->contents
;
7567 if (rel_hdr
->sh_type
== SHT_REL
)
7569 erel
+= (elf_section_data (output_section
)->rel_count
7570 * sizeof (Elf_External_Rel
));
7571 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7575 irel
[0].r_addend
= addend
;
7576 erel
+= (elf_section_data (output_section
)->rel_count
7577 * sizeof (Elf_External_Rela
));
7578 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7581 ++elf_section_data (output_section
)->rel_count
;
7586 /* Allocate a pointer to live in a linker created section. */
7589 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
7591 struct bfd_link_info
*info
;
7592 elf_linker_section_t
*lsect
;
7593 struct elf_link_hash_entry
*h
;
7594 const Elf_Internal_Rela
*rel
;
7596 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
7597 elf_linker_section_pointers_t
*linker_section_ptr
;
7598 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7601 BFD_ASSERT (lsect
!= NULL
);
7603 /* Is this a global symbol? */
7606 /* Has this symbol already been allocated? If so, our work is done. */
7607 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
7612 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
7613 /* Make sure this symbol is output as a dynamic symbol. */
7614 if (h
->dynindx
== -1)
7616 if (! elf_link_record_dynamic_symbol (info
, h
))
7620 if (lsect
->rel_section
)
7621 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7625 /* Allocation of a pointer to a local symbol. */
7626 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
7628 /* Allocate a table to hold the local symbols if first time. */
7631 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
7632 register unsigned int i
;
7635 amt
*= sizeof (elf_linker_section_pointers_t
*);
7636 ptr
= (elf_linker_section_pointers_t
**) bfd_alloc (abfd
, amt
);
7641 elf_local_ptr_offsets (abfd
) = ptr
;
7642 for (i
= 0; i
< num_symbols
; i
++)
7643 ptr
[i
] = (elf_linker_section_pointers_t
*) 0;
7646 /* Has this symbol already been allocated? If so, our work is done. */
7647 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
7652 ptr_linker_section_ptr
= &ptr
[r_symndx
];
7656 /* If we are generating a shared object, we need to
7657 output a R_<xxx>_RELATIVE reloc so that the
7658 dynamic linker can adjust this GOT entry. */
7659 BFD_ASSERT (lsect
->rel_section
!= NULL
);
7660 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7664 /* Allocate space for a pointer in the linker section, and allocate
7665 a new pointer record from internal memory. */
7666 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
7667 amt
= sizeof (elf_linker_section_pointers_t
);
7668 linker_section_ptr
= (elf_linker_section_pointers_t
*) bfd_alloc (abfd
, amt
);
7670 if (!linker_section_ptr
)
7673 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
7674 linker_section_ptr
->addend
= rel
->r_addend
;
7675 linker_section_ptr
->which
= lsect
->which
;
7676 linker_section_ptr
->written_address_p
= FALSE
;
7677 *ptr_linker_section_ptr
= linker_section_ptr
;
7680 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
7682 linker_section_ptr
->offset
= (lsect
->section
->_raw_size
7683 - lsect
->hole_size
+ (ARCH_SIZE
/ 8));
7684 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
7685 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
7686 if (lsect
->sym_hash
)
7688 /* Bump up symbol value if needed. */
7689 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
7691 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
7692 lsect
->sym_hash
->root
.root
.string
,
7693 (long) ARCH_SIZE
/ 8,
7694 (long) lsect
->sym_hash
->root
.u
.def
.value
);
7700 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
7702 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
7706 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7707 lsect
->name
, (long) linker_section_ptr
->offset
,
7708 (long) lsect
->section
->_raw_size
);
7715 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7718 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7721 /* Fill in the address for a pointer generated in a linker section. */
7724 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
,
7725 relocation
, rel
, relative_reloc
)
7728 struct bfd_link_info
*info
;
7729 elf_linker_section_t
*lsect
;
7730 struct elf_link_hash_entry
*h
;
7732 const Elf_Internal_Rela
*rel
;
7735 elf_linker_section_pointers_t
*linker_section_ptr
;
7737 BFD_ASSERT (lsect
!= NULL
);
7741 /* Handle global symbol. */
7742 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7743 (h
->linker_section_pointer
,
7747 BFD_ASSERT (linker_section_ptr
!= NULL
);
7749 if (! elf_hash_table (info
)->dynamic_sections_created
7752 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
7754 /* This is actually a static link, or it is a
7755 -Bsymbolic link and the symbol is defined
7756 locally. We must initialize this entry in the
7759 When doing a dynamic link, we create a .rela.<xxx>
7760 relocation entry to initialize the value. This
7761 is done in the finish_dynamic_symbol routine. */
7762 if (!linker_section_ptr
->written_address_p
)
7764 linker_section_ptr
->written_address_p
= TRUE
;
7765 bfd_put_ptr (output_bfd
,
7766 relocation
+ linker_section_ptr
->addend
,
7767 (lsect
->section
->contents
7768 + linker_section_ptr
->offset
));
7774 /* Handle local symbol. */
7775 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7776 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
7777 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
7778 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7779 (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
7783 BFD_ASSERT (linker_section_ptr
!= NULL
);
7785 /* Write out pointer if it hasn't been rewritten out before. */
7786 if (!linker_section_ptr
->written_address_p
)
7788 linker_section_ptr
->written_address_p
= TRUE
;
7789 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
7790 lsect
->section
->contents
+ linker_section_ptr
->offset
);
7794 asection
*srel
= lsect
->rel_section
;
7795 Elf_Internal_Rela outrel
[MAX_INT_RELS_PER_EXT_REL
];
7797 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7800 /* We need to generate a relative reloc for the dynamic
7804 srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
7806 lsect
->rel_section
= srel
;
7809 BFD_ASSERT (srel
!= NULL
);
7811 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7813 outrel
[i
].r_offset
= (lsect
->section
->output_section
->vma
7814 + lsect
->section
->output_offset
7815 + linker_section_ptr
->offset
);
7816 outrel
[i
].r_info
= 0;
7817 outrel
[i
].r_addend
= 0;
7819 outrel
[0].r_info
= ELF_R_INFO (0, relative_reloc
);
7820 erel
= lsect
->section
->contents
;
7821 erel
+= (elf_section_data (lsect
->section
)->rel_count
++
7822 * sizeof (Elf_External_Rela
));
7823 elf_swap_reloca_out (output_bfd
, outrel
, erel
);
7828 relocation
= (lsect
->section
->output_offset
7829 + linker_section_ptr
->offset
7830 - lsect
->hole_offset
7831 - lsect
->sym_offset
);
7835 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7836 lsect
->name
, (long) relocation
, (long) relocation
);
7839 /* Subtract out the addend, because it will get added back in by the normal
7841 return relocation
- linker_section_ptr
->addend
;
7844 /* Garbage collect unused sections. */
7846 static bfd_boolean elf_gc_mark
7847 PARAMS ((struct bfd_link_info
*, asection
*,
7848 asection
* (*) (asection
*, struct bfd_link_info
*,
7849 Elf_Internal_Rela
*, struct elf_link_hash_entry
*,
7850 Elf_Internal_Sym
*)));
7852 static bfd_boolean elf_gc_sweep
7853 PARAMS ((struct bfd_link_info
*,
7854 bfd_boolean (*) (bfd
*, struct bfd_link_info
*, asection
*,
7855 const Elf_Internal_Rela
*)));
7857 static bfd_boolean elf_gc_sweep_symbol
7858 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7860 static bfd_boolean elf_gc_allocate_got_offsets
7861 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7863 static bfd_boolean elf_gc_propagate_vtable_entries_used
7864 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7866 static bfd_boolean elf_gc_smash_unused_vtentry_relocs
7867 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7869 /* The mark phase of garbage collection. For a given section, mark
7870 it and any sections in this section's group, and all the sections
7871 which define symbols to which it refers. */
7873 typedef asection
* (*gc_mark_hook_fn
)
7874 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7875 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
7878 elf_gc_mark (info
, sec
, gc_mark_hook
)
7879 struct bfd_link_info
*info
;
7881 gc_mark_hook_fn gc_mark_hook
;
7884 asection
*group_sec
;
7888 /* Mark all the sections in the group. */
7889 group_sec
= elf_section_data (sec
)->next_in_group
;
7890 if (group_sec
&& !group_sec
->gc_mark
)
7891 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
7894 /* Look through the section relocs. */
7896 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
7898 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
7899 Elf_Internal_Shdr
*symtab_hdr
;
7900 struct elf_link_hash_entry
**sym_hashes
;
7903 bfd
*input_bfd
= sec
->owner
;
7904 struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
7905 Elf_Internal_Sym
*isym
= NULL
;
7907 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
7908 sym_hashes
= elf_sym_hashes (input_bfd
);
7910 /* Read the local symbols. */
7911 if (elf_bad_symtab (input_bfd
))
7913 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
7917 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
7919 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
7920 if (isym
== NULL
&& nlocsyms
!= 0)
7922 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
7928 /* Read the relocations. */
7929 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
7930 (input_bfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
7931 info
->keep_memory
));
7932 if (relstart
== NULL
)
7937 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7939 for (rel
= relstart
; rel
< relend
; rel
++)
7941 unsigned long r_symndx
;
7943 struct elf_link_hash_entry
*h
;
7945 r_symndx
= ELF_R_SYM (rel
->r_info
);
7949 if (r_symndx
>= nlocsyms
7950 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
7952 h
= sym_hashes
[r_symndx
- extsymoff
];
7953 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
7957 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
7960 if (rsec
&& !rsec
->gc_mark
)
7962 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
7964 else if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
7973 if (elf_section_data (sec
)->relocs
!= relstart
)
7976 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
7978 if (! info
->keep_memory
)
7981 symtab_hdr
->contents
= (unsigned char *) isym
;
7988 /* The sweep phase of garbage collection. Remove all garbage sections. */
7990 typedef bfd_boolean (*gc_sweep_hook_fn
)
7991 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
7992 const Elf_Internal_Rela
*));
7995 elf_gc_sweep (info
, gc_sweep_hook
)
7996 struct bfd_link_info
*info
;
7997 gc_sweep_hook_fn gc_sweep_hook
;
8001 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8005 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8008 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8010 /* Keep special sections. Keep .debug sections. */
8011 if ((o
->flags
& SEC_LINKER_CREATED
)
8012 || (o
->flags
& SEC_DEBUGGING
))
8018 /* Skip sweeping sections already excluded. */
8019 if (o
->flags
& SEC_EXCLUDE
)
8022 /* Since this is early in the link process, it is simple
8023 to remove a section from the output. */
8024 o
->flags
|= SEC_EXCLUDE
;
8026 /* But we also have to update some of the relocation
8027 info we collected before. */
8029 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8031 Elf_Internal_Rela
*internal_relocs
;
8034 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
8035 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
8036 if (internal_relocs
== NULL
)
8039 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8041 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8042 free (internal_relocs
);
8050 /* Remove the symbols that were in the swept sections from the dynamic
8051 symbol table. GCFIXME: Anyone know how to get them out of the
8052 static symbol table as well? */
8056 elf_link_hash_traverse (elf_hash_table (info
),
8057 elf_gc_sweep_symbol
,
8060 elf_hash_table (info
)->dynsymcount
= i
;
8066 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8069 elf_gc_sweep_symbol (h
, idxptr
)
8070 struct elf_link_hash_entry
*h
;
8073 int *idx
= (int *) idxptr
;
8075 if (h
->root
.type
== bfd_link_hash_warning
)
8076 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8078 if (h
->dynindx
!= -1
8079 && ((h
->root
.type
!= bfd_link_hash_defined
8080 && h
->root
.type
!= bfd_link_hash_defweak
)
8081 || h
->root
.u
.def
.section
->gc_mark
))
8082 h
->dynindx
= (*idx
)++;
8087 /* Propogate collected vtable information. This is called through
8088 elf_link_hash_traverse. */
8091 elf_gc_propagate_vtable_entries_used (h
, okp
)
8092 struct elf_link_hash_entry
*h
;
8095 if (h
->root
.type
== bfd_link_hash_warning
)
8096 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8098 /* Those that are not vtables. */
8099 if (h
->vtable_parent
== NULL
)
8102 /* Those vtables that do not have parents, we cannot merge. */
8103 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
8106 /* If we've already been done, exit. */
8107 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
8110 /* Make sure the parent's table is up to date. */
8111 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
8113 if (h
->vtable_entries_used
== NULL
)
8115 /* None of this table's entries were referenced. Re-use the
8117 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
8118 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
8123 bfd_boolean
*cu
, *pu
;
8125 /* Or the parent's entries into ours. */
8126 cu
= h
->vtable_entries_used
;
8128 pu
= h
->vtable_parent
->vtable_entries_used
;
8131 asection
*sec
= h
->root
.u
.def
.section
;
8132 struct elf_backend_data
*bed
= get_elf_backend_data (sec
->owner
);
8133 int file_align
= bed
->s
->file_align
;
8135 n
= h
->vtable_parent
->vtable_entries_size
/ file_align
;
8150 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
8151 struct elf_link_hash_entry
*h
;
8155 bfd_vma hstart
, hend
;
8156 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8157 struct elf_backend_data
*bed
;
8160 if (h
->root
.type
== bfd_link_hash_warning
)
8161 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8163 /* Take care of both those symbols that do not describe vtables as
8164 well as those that are not loaded. */
8165 if (h
->vtable_parent
== NULL
)
8168 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8169 || h
->root
.type
== bfd_link_hash_defweak
);
8171 sec
= h
->root
.u
.def
.section
;
8172 hstart
= h
->root
.u
.def
.value
;
8173 hend
= hstart
+ h
->size
;
8175 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
8176 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, TRUE
));
8178 return *(bfd_boolean
*) okp
= FALSE
;
8179 bed
= get_elf_backend_data (sec
->owner
);
8180 file_align
= bed
->s
->file_align
;
8182 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8184 for (rel
= relstart
; rel
< relend
; ++rel
)
8185 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8187 /* If the entry is in use, do nothing. */
8188 if (h
->vtable_entries_used
8189 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
8191 bfd_vma entry
= (rel
->r_offset
- hstart
) / file_align
;
8192 if (h
->vtable_entries_used
[entry
])
8195 /* Otherwise, kill it. */
8196 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8202 /* Do mark and sweep of unused sections. */
8205 elf_gc_sections (abfd
, info
)
8207 struct bfd_link_info
*info
;
8209 bfd_boolean ok
= TRUE
;
8211 asection
* (*gc_mark_hook
)
8212 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8213 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
8215 if (!get_elf_backend_data (abfd
)->can_gc_sections
8216 || info
->relocateable
|| info
->emitrelocations
8217 || elf_hash_table (info
)->dynamic_sections_created
)
8220 /* Apply transitive closure to the vtable entry usage info. */
8221 elf_link_hash_traverse (elf_hash_table (info
),
8222 elf_gc_propagate_vtable_entries_used
,
8227 /* Kill the vtable relocations that were not used. */
8228 elf_link_hash_traverse (elf_hash_table (info
),
8229 elf_gc_smash_unused_vtentry_relocs
,
8234 /* Grovel through relocs to find out who stays ... */
8236 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8237 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8241 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8244 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8246 if (o
->flags
& SEC_KEEP
)
8247 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
8252 /* ... and mark SEC_EXCLUDE for those that go. */
8253 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8259 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
8262 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
8265 struct elf_link_hash_entry
*h
;
8268 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8269 struct elf_link_hash_entry
**search
, *child
;
8270 bfd_size_type extsymcount
;
8272 /* The sh_info field of the symtab header tells us where the
8273 external symbols start. We don't care about the local symbols at
8275 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
8276 if (!elf_bad_symtab (abfd
))
8277 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8279 sym_hashes
= elf_sym_hashes (abfd
);
8280 sym_hashes_end
= sym_hashes
+ extsymcount
;
8282 /* Hunt down the child symbol, which is in this section at the same
8283 offset as the relocation. */
8284 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8286 if ((child
= *search
) != NULL
8287 && (child
->root
.type
== bfd_link_hash_defined
8288 || child
->root
.type
== bfd_link_hash_defweak
)
8289 && child
->root
.u
.def
.section
== sec
8290 && child
->root
.u
.def
.value
== offset
)
8294 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
8295 bfd_archive_filename (abfd
), sec
->name
,
8296 (unsigned long) offset
);
8297 bfd_set_error (bfd_error_invalid_operation
);
8303 /* This *should* only be the absolute section. It could potentially
8304 be that someone has defined a non-global vtable though, which
8305 would be bad. It isn't worth paging in the local symbols to be
8306 sure though; that case should simply be handled by the assembler. */
8308 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
8311 child
->vtable_parent
= h
;
8316 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
8319 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
8320 bfd
*abfd ATTRIBUTE_UNUSED
;
8321 asection
*sec ATTRIBUTE_UNUSED
;
8322 struct elf_link_hash_entry
*h
;
8325 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8326 int file_align
= bed
->s
->file_align
;
8328 if (addend
>= h
->vtable_entries_size
)
8331 bfd_boolean
*ptr
= h
->vtable_entries_used
;
8333 /* While the symbol is undefined, we have to be prepared to handle
8335 if (h
->root
.type
== bfd_link_hash_undefined
)
8342 /* Oops! We've got a reference past the defined end of
8343 the table. This is probably a bug -- shall we warn? */
8348 /* Allocate one extra entry for use as a "done" flag for the
8349 consolidation pass. */
8350 bytes
= (size
/ file_align
+ 1) * sizeof (bfd_boolean
);
8354 ptr
= bfd_realloc (ptr
- 1, (bfd_size_type
) bytes
);
8360 oldbytes
= ((h
->vtable_entries_size
/ file_align
+ 1)
8361 * sizeof (bfd_boolean
));
8362 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8366 ptr
= bfd_zmalloc ((bfd_size_type
) bytes
);
8371 /* And arrange for that done flag to be at index -1. */
8372 h
->vtable_entries_used
= ptr
+ 1;
8373 h
->vtable_entries_size
= size
;
8376 h
->vtable_entries_used
[addend
/ file_align
] = TRUE
;
8381 /* And an accompanying bit to work out final got entry offsets once
8382 we're done. Should be called from final_link. */
8385 elf_gc_common_finalize_got_offsets (abfd
, info
)
8387 struct bfd_link_info
*info
;
8390 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8393 /* The GOT offset is relative to the .got section, but the GOT header is
8394 put into the .got.plt section, if the backend uses it. */
8395 if (bed
->want_got_plt
)
8398 gotoff
= bed
->got_header_size
;
8400 /* Do the local .got entries first. */
8401 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
8403 bfd_signed_vma
*local_got
;
8404 bfd_size_type j
, locsymcount
;
8405 Elf_Internal_Shdr
*symtab_hdr
;
8407 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
8410 local_got
= elf_local_got_refcounts (i
);
8414 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
8415 if (elf_bad_symtab (i
))
8416 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8418 locsymcount
= symtab_hdr
->sh_info
;
8420 for (j
= 0; j
< locsymcount
; ++j
)
8422 if (local_got
[j
] > 0)
8424 local_got
[j
] = gotoff
;
8425 gotoff
+= ARCH_SIZE
/ 8;
8428 local_got
[j
] = (bfd_vma
) -1;
8432 /* Then the global .got entries. .plt refcounts are handled by
8433 adjust_dynamic_symbol */
8434 elf_link_hash_traverse (elf_hash_table (info
),
8435 elf_gc_allocate_got_offsets
,
8440 /* We need a special top-level link routine to convert got reference counts
8441 to real got offsets. */
8444 elf_gc_allocate_got_offsets (h
, offarg
)
8445 struct elf_link_hash_entry
*h
;
8448 bfd_vma
*off
= (bfd_vma
*) offarg
;
8450 if (h
->root
.type
== bfd_link_hash_warning
)
8451 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8453 if (h
->got
.refcount
> 0)
8455 h
->got
.offset
= off
[0];
8456 off
[0] += ARCH_SIZE
/ 8;
8459 h
->got
.offset
= (bfd_vma
) -1;
8464 /* Many folk need no more in the way of final link than this, once
8465 got entry reference counting is enabled. */
8468 elf_gc_common_final_link (abfd
, info
)
8470 struct bfd_link_info
*info
;
8472 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
8475 /* Invoke the regular ELF backend linker to do all the work. */
8476 return elf_bfd_final_link (abfd
, info
);
8479 /* This function will be called though elf_link_hash_traverse to store
8480 all hash value of the exported symbols in an array. */
8483 elf_collect_hash_codes (h
, data
)
8484 struct elf_link_hash_entry
*h
;
8487 unsigned long **valuep
= (unsigned long **) data
;
8493 if (h
->root
.type
== bfd_link_hash_warning
)
8494 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8496 /* Ignore indirect symbols. These are added by the versioning code. */
8497 if (h
->dynindx
== -1)
8500 name
= h
->root
.root
.string
;
8501 p
= strchr (name
, ELF_VER_CHR
);
8504 alc
= bfd_malloc ((bfd_size_type
) (p
- name
+ 1));
8505 memcpy (alc
, name
, (size_t) (p
- name
));
8506 alc
[p
- name
] = '\0';
8510 /* Compute the hash value. */
8511 ha
= bfd_elf_hash (name
);
8513 /* Store the found hash value in the array given as the argument. */
8516 /* And store it in the struct so that we can put it in the hash table
8518 h
->elf_hash_value
= ha
;
8527 elf_reloc_symbol_deleted_p (offset
, cookie
)
8531 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
8533 if (rcookie
->bad_symtab
)
8534 rcookie
->rel
= rcookie
->rels
;
8536 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
8538 unsigned long r_symndx
;
8540 if (! rcookie
->bad_symtab
)
8541 if (rcookie
->rel
->r_offset
> offset
)
8543 if (rcookie
->rel
->r_offset
!= offset
)
8546 r_symndx
= ELF_R_SYM (rcookie
->rel
->r_info
);
8547 if (r_symndx
== SHN_UNDEF
)
8550 if (r_symndx
>= rcookie
->locsymcount
8551 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
8553 struct elf_link_hash_entry
*h
;
8555 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
8557 while (h
->root
.type
== bfd_link_hash_indirect
8558 || h
->root
.type
== bfd_link_hash_warning
)
8559 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8561 if ((h
->root
.type
== bfd_link_hash_defined
8562 || h
->root
.type
== bfd_link_hash_defweak
)
8563 && elf_discarded_section (h
->root
.u
.def
.section
))
8570 /* It's not a relocation against a global symbol,
8571 but it could be a relocation against a local
8572 symbol for a discarded section. */
8574 Elf_Internal_Sym
*isym
;
8576 /* Need to: get the symbol; get the section. */
8577 isym
= &rcookie
->locsyms
[r_symndx
];
8578 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
8580 isec
= section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
8581 if (isec
!= NULL
&& elf_discarded_section (isec
))
8590 /* Discard unneeded references to discarded sections.
8591 Returns TRUE if any section's size was changed. */
8592 /* This function assumes that the relocations are in sorted order,
8593 which is true for all known assemblers. */
8596 elf_bfd_discard_info (output_bfd
, info
)
8598 struct bfd_link_info
*info
;
8600 struct elf_reloc_cookie cookie
;
8601 asection
*stab
, *eh
;
8602 Elf_Internal_Shdr
*symtab_hdr
;
8603 struct elf_backend_data
*bed
;
8606 bfd_boolean ret
= FALSE
;
8608 if (info
->traditional_format
8609 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
8610 || ! is_elf_hash_table (info
))
8613 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
8615 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
8618 bed
= get_elf_backend_data (abfd
);
8620 if ((abfd
->flags
& DYNAMIC
) != 0)
8623 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
8624 if (info
->relocateable
8626 && (eh
->_raw_size
== 0
8627 || bfd_is_abs_section (eh
->output_section
))))
8630 stab
= bfd_get_section_by_name (abfd
, ".stab");
8632 && (stab
->_raw_size
== 0
8633 || bfd_is_abs_section (stab
->output_section
)
8634 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
8639 && bed
->elf_backend_discard_info
== NULL
)
8642 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8644 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
8645 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
8646 if (cookie
.bad_symtab
)
8648 cookie
.locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8649 cookie
.extsymoff
= 0;
8653 cookie
.locsymcount
= symtab_hdr
->sh_info
;
8654 cookie
.extsymoff
= symtab_hdr
->sh_info
;
8657 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8658 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
8660 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
8661 cookie
.locsymcount
, 0,
8663 if (cookie
.locsyms
== NULL
)
8670 count
= stab
->reloc_count
;
8672 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8673 (abfd
, stab
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
8674 info
->keep_memory
));
8675 if (cookie
.rels
!= NULL
)
8677 cookie
.rel
= cookie
.rels
;
8678 cookie
.relend
= cookie
.rels
;
8679 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
8680 if (_bfd_discard_section_stabs (abfd
, stab
,
8681 elf_section_data (stab
)->sec_info
,
8682 elf_reloc_symbol_deleted_p
,
8685 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
8693 count
= eh
->reloc_count
;
8695 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8696 (abfd
, eh
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
8697 info
->keep_memory
));
8698 cookie
.rel
= cookie
.rels
;
8699 cookie
.relend
= cookie
.rels
;
8700 if (cookie
.rels
!= NULL
)
8701 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
8703 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
8704 elf_reloc_symbol_deleted_p
,
8708 if (cookie
.rels
!= NULL
8709 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
8713 if (bed
->elf_backend_discard_info
!= NULL
8714 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
8717 if (cookie
.locsyms
!= NULL
8718 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
8720 if (! info
->keep_memory
)
8721 free (cookie
.locsyms
);
8723 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
8727 if (info
->eh_frame_hdr
8728 && !info
->relocateable
8729 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
8736 elf_section_ignore_discarded_relocs (sec
)
8739 struct elf_backend_data
*bed
;
8741 switch (sec
->sec_info_type
)
8743 case ELF_INFO_TYPE_STABS
:
8744 case ELF_INFO_TYPE_EH_FRAME
:
8750 bed
= get_elf_backend_data (sec
->owner
);
8751 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
8752 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))