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
1206 len
= strlen (name
);
1207 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1208 if (shortname
== NULL
)
1210 memcpy (shortname
, name
, shortlen
);
1211 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1213 /* Once again, merge with any existing symbol. */
1214 type_change_ok
= FALSE
;
1215 size_change_ok
= FALSE
;
1217 if (! elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1218 &hi
, &skip
, &override
, &type_change_ok
,
1219 &size_change_ok
, dt_needed
))
1227 /* Here SHORTNAME is a versioned name, so we don't expect to see
1228 the type of override we do in the case above unless it is
1229 overridden by a versioned definiton. */
1230 if (hi
->root
.type
!= bfd_link_hash_defined
1231 && hi
->root
.type
!= bfd_link_hash_defweak
)
1232 (*_bfd_error_handler
)
1233 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1234 bfd_archive_filename (abfd
), shortname
);
1239 if (! (_bfd_generic_link_add_one_symbol
1240 (info
, abfd
, shortname
, BSF_INDIRECT
,
1241 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, FALSE
, collect
, &bh
)))
1243 hi
= (struct elf_link_hash_entry
*) bh
;
1245 /* If there is a duplicate definition somewhere, then HI may not
1246 point to an indirect symbol. We will have reported an error
1247 to the user in that case. */
1249 if (hi
->root
.type
== bfd_link_hash_indirect
)
1251 /* If the symbol became indirect, then we assume that we have
1252 not seen a definition before. */
1253 BFD_ASSERT ((hi
->elf_link_hash_flags
1254 & (ELF_LINK_HASH_DEF_DYNAMIC
1255 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1257 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1259 /* See if the new flags lead us to realize that the symbol
1266 || ((hi
->elf_link_hash_flags
1267 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1272 if ((hi
->elf_link_hash_flags
1273 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1283 /* Add symbols from an ELF object file to the linker hash table. */
1286 elf_link_add_object_symbols (abfd
, info
)
1288 struct bfd_link_info
*info
;
1290 bfd_boolean (*add_symbol_hook
)
1291 PARAMS ((bfd
*, struct bfd_link_info
*, const Elf_Internal_Sym
*,
1292 const char **, flagword
*, asection
**, bfd_vma
*));
1293 bfd_boolean (*check_relocs
)
1294 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
1295 const Elf_Internal_Rela
*));
1296 bfd_boolean collect
;
1297 Elf_Internal_Shdr
*hdr
;
1298 bfd_size_type symcount
;
1299 bfd_size_type extsymcount
;
1300 bfd_size_type extsymoff
;
1301 struct elf_link_hash_entry
**sym_hash
;
1302 bfd_boolean dynamic
;
1303 Elf_External_Versym
*extversym
= NULL
;
1304 Elf_External_Versym
*ever
;
1305 struct elf_link_hash_entry
*weaks
;
1306 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
1307 bfd_size_type nondeflt_vers_cnt
= 0;
1308 Elf_Internal_Sym
*isymbuf
= NULL
;
1309 Elf_Internal_Sym
*isym
;
1310 Elf_Internal_Sym
*isymend
;
1311 struct elf_backend_data
*bed
;
1312 bfd_boolean dt_needed
;
1313 struct elf_link_hash_table
* hash_table
;
1316 hash_table
= elf_hash_table (info
);
1318 bed
= get_elf_backend_data (abfd
);
1319 add_symbol_hook
= bed
->elf_add_symbol_hook
;
1320 collect
= bed
->collect
;
1322 if ((abfd
->flags
& DYNAMIC
) == 0)
1328 /* You can't use -r against a dynamic object. Also, there's no
1329 hope of using a dynamic object which does not exactly match
1330 the format of the output file. */
1331 if (info
->relocateable
|| info
->hash
->creator
!= abfd
->xvec
)
1333 bfd_set_error (bfd_error_invalid_operation
);
1338 /* As a GNU extension, any input sections which are named
1339 .gnu.warning.SYMBOL are treated as warning symbols for the given
1340 symbol. This differs from .gnu.warning sections, which generate
1341 warnings when they are included in an output file. */
1346 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
1350 name
= bfd_get_section_name (abfd
, s
);
1351 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1356 name
+= sizeof ".gnu.warning." - 1;
1358 /* If this is a shared object, then look up the symbol
1359 in the hash table. If it is there, and it is already
1360 been defined, then we will not be using the entry
1361 from this shared object, so we don't need to warn.
1362 FIXME: If we see the definition in a regular object
1363 later on, we will warn, but we shouldn't. The only
1364 fix is to keep track of what warnings we are supposed
1365 to emit, and then handle them all at the end of the
1367 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
1369 struct elf_link_hash_entry
*h
;
1371 h
= elf_link_hash_lookup (hash_table
, name
,
1372 FALSE
, FALSE
, TRUE
);
1374 /* FIXME: What about bfd_link_hash_common? */
1376 && (h
->root
.type
== bfd_link_hash_defined
1377 || h
->root
.type
== bfd_link_hash_defweak
))
1379 /* We don't want to issue this warning. Clobber
1380 the section size so that the warning does not
1381 get copied into the output file. */
1387 sz
= bfd_section_size (abfd
, s
);
1388 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
1392 if (! bfd_get_section_contents (abfd
, s
, msg
, (file_ptr
) 0, sz
))
1397 if (! (_bfd_generic_link_add_one_symbol
1398 (info
, abfd
, name
, BSF_WARNING
, s
, (bfd_vma
) 0, msg
,
1399 FALSE
, collect
, (struct bfd_link_hash_entry
**) NULL
)))
1402 if (! info
->relocateable
)
1404 /* Clobber the section size so that the warning does
1405 not get copied into the output file. */
1415 /* If we are creating a shared library, create all the dynamic
1416 sections immediately. We need to attach them to something,
1417 so we attach them to this BFD, provided it is the right
1418 format. FIXME: If there are no input BFD's of the same
1419 format as the output, we can't make a shared library. */
1421 && is_elf_hash_table (info
)
1422 && ! hash_table
->dynamic_sections_created
1423 && abfd
->xvec
== info
->hash
->creator
)
1425 if (! elf_link_create_dynamic_sections (abfd
, info
))
1429 else if (! is_elf_hash_table (info
))
1434 bfd_boolean add_needed
;
1436 bfd_size_type oldsize
;
1437 bfd_size_type strindex
;
1438 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
1440 /* ld --just-symbols and dynamic objects don't mix very well.
1441 Test for --just-symbols by looking at info set up by
1442 _bfd_elf_link_just_syms. */
1443 if ((s
= abfd
->sections
) != NULL
1444 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
1447 /* Find the name to use in a DT_NEEDED entry that refers to this
1448 object. If the object has a DT_SONAME entry, we use it.
1449 Otherwise, if the generic linker stuck something in
1450 elf_dt_name, we use that. Otherwise, we just use the file
1451 name. If the generic linker put a null string into
1452 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1453 there is a DT_SONAME entry. */
1455 name
= bfd_get_filename (abfd
);
1456 if (elf_dt_name (abfd
) != NULL
)
1458 name
= elf_dt_name (abfd
);
1461 if (elf_dt_soname (abfd
) != NULL
)
1467 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1470 Elf_External_Dyn
*dynbuf
= NULL
;
1471 Elf_External_Dyn
*extdyn
;
1472 Elf_External_Dyn
*extdynend
;
1474 unsigned long shlink
;
1476 dynbuf
= (Elf_External_Dyn
*) bfd_malloc (s
->_raw_size
);
1480 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
1481 (file_ptr
) 0, s
->_raw_size
))
1482 goto error_free_dyn
;
1484 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1486 goto error_free_dyn
;
1487 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1490 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
1491 for (; extdyn
< extdynend
; extdyn
++)
1493 Elf_Internal_Dyn dyn
;
1495 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
1496 if (dyn
.d_tag
== DT_SONAME
)
1498 unsigned int tagv
= dyn
.d_un
.d_val
;
1499 name
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1501 goto error_free_dyn
;
1503 if (dyn
.d_tag
== DT_NEEDED
)
1505 struct bfd_link_needed_list
*n
, **pn
;
1507 unsigned int tagv
= dyn
.d_un
.d_val
;
1509 amt
= sizeof (struct bfd_link_needed_list
);
1510 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1511 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1512 if (n
== NULL
|| fnm
== NULL
)
1513 goto error_free_dyn
;
1514 amt
= strlen (fnm
) + 1;
1515 anm
= bfd_alloc (abfd
, amt
);
1517 goto error_free_dyn
;
1518 memcpy (anm
, fnm
, (size_t) amt
);
1522 for (pn
= & hash_table
->needed
;
1528 if (dyn
.d_tag
== DT_RUNPATH
)
1530 struct bfd_link_needed_list
*n
, **pn
;
1532 unsigned int tagv
= dyn
.d_un
.d_val
;
1534 amt
= sizeof (struct bfd_link_needed_list
);
1535 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1536 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1537 if (n
== NULL
|| fnm
== NULL
)
1538 goto error_free_dyn
;
1539 amt
= strlen (fnm
) + 1;
1540 anm
= bfd_alloc (abfd
, amt
);
1542 goto error_free_dyn
;
1543 memcpy (anm
, fnm
, (size_t) amt
);
1547 for (pn
= & runpath
;
1553 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1554 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
1556 struct bfd_link_needed_list
*n
, **pn
;
1558 unsigned int tagv
= dyn
.d_un
.d_val
;
1560 amt
= sizeof (struct bfd_link_needed_list
);
1561 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1562 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1563 if (n
== NULL
|| fnm
== NULL
)
1564 goto error_free_dyn
;
1565 amt
= strlen (fnm
) + 1;
1566 anm
= bfd_alloc (abfd
, amt
);
1573 memcpy (anm
, fnm
, (size_t) amt
);
1588 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
1589 frees all more recently bfd_alloc'd blocks as well. */
1595 struct bfd_link_needed_list
**pn
;
1596 for (pn
= & hash_table
->runpath
;
1603 /* We do not want to include any of the sections in a dynamic
1604 object in the output file. We hack by simply clobbering the
1605 list of sections in the BFD. This could be handled more
1606 cleanly by, say, a new section flag; the existing
1607 SEC_NEVER_LOAD flag is not the one we want, because that one
1608 still implies that the section takes up space in the output
1610 bfd_section_list_clear (abfd
);
1612 /* If this is the first dynamic object found in the link, create
1613 the special sections required for dynamic linking. */
1614 if (! hash_table
->dynamic_sections_created
)
1615 if (! elf_link_create_dynamic_sections (abfd
, info
))
1620 /* Add a DT_NEEDED entry for this dynamic object. */
1621 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
1622 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, name
, FALSE
);
1623 if (strindex
== (bfd_size_type
) -1)
1626 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
1629 Elf_External_Dyn
*dyncon
, *dynconend
;
1631 /* The hash table size did not change, which means that
1632 the dynamic object name was already entered. If we
1633 have already included this dynamic object in the
1634 link, just ignore it. There is no reason to include
1635 a particular dynamic object more than once. */
1636 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
1637 BFD_ASSERT (sdyn
!= NULL
);
1639 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1640 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1642 for (; dyncon
< dynconend
; dyncon
++)
1644 Elf_Internal_Dyn dyn
;
1646 elf_swap_dyn_in (hash_table
->dynobj
, dyncon
, & dyn
);
1647 if (dyn
.d_tag
== DT_NEEDED
1648 && dyn
.d_un
.d_val
== strindex
)
1650 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
1656 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
1660 /* Save the SONAME, if there is one, because sometimes the
1661 linker emulation code will need to know it. */
1663 name
= basename (bfd_get_filename (abfd
));
1664 elf_dt_name (abfd
) = name
;
1667 /* If this is a dynamic object, we always link against the .dynsym
1668 symbol table, not the .symtab symbol table. The dynamic linker
1669 will only see the .dynsym symbol table, so there is no reason to
1670 look at .symtab for a dynamic object. */
1672 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
1673 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1675 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1677 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
1679 /* The sh_info field of the symtab header tells us where the
1680 external symbols start. We don't care about the local symbols at
1682 if (elf_bad_symtab (abfd
))
1684 extsymcount
= symcount
;
1689 extsymcount
= symcount
- hdr
->sh_info
;
1690 extsymoff
= hdr
->sh_info
;
1694 if (extsymcount
!= 0)
1696 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
1698 if (isymbuf
== NULL
)
1701 /* We store a pointer to the hash table entry for each external
1703 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
1704 sym_hash
= (struct elf_link_hash_entry
**) bfd_alloc (abfd
, amt
);
1705 if (sym_hash
== NULL
)
1706 goto error_free_sym
;
1707 elf_sym_hashes (abfd
) = sym_hash
;
1712 /* Read in any version definitions. */
1713 if (! _bfd_elf_slurp_version_tables (abfd
))
1714 goto error_free_sym
;
1716 /* Read in the symbol versions, but don't bother to convert them
1717 to internal format. */
1718 if (elf_dynversym (abfd
) != 0)
1720 Elf_Internal_Shdr
*versymhdr
;
1722 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
1723 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
1724 if (extversym
== NULL
)
1725 goto error_free_sym
;
1726 amt
= versymhdr
->sh_size
;
1727 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
1728 || bfd_bread ((PTR
) extversym
, amt
, abfd
) != amt
)
1729 goto error_free_vers
;
1735 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
1736 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
1738 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
1745 struct elf_link_hash_entry
*h
;
1746 bfd_boolean definition
;
1747 bfd_boolean size_change_ok
;
1748 bfd_boolean type_change_ok
;
1749 bfd_boolean new_weakdef
;
1750 bfd_boolean override
;
1751 unsigned int old_alignment
;
1756 flags
= BSF_NO_FLAGS
;
1758 value
= isym
->st_value
;
1761 bind
= ELF_ST_BIND (isym
->st_info
);
1762 if (bind
== STB_LOCAL
)
1764 /* This should be impossible, since ELF requires that all
1765 global symbols follow all local symbols, and that sh_info
1766 point to the first global symbol. Unfortunatealy, Irix 5
1770 else if (bind
== STB_GLOBAL
)
1772 if (isym
->st_shndx
!= SHN_UNDEF
1773 && isym
->st_shndx
!= SHN_COMMON
)
1776 else if (bind
== STB_WEAK
)
1780 /* Leave it up to the processor backend. */
1783 if (isym
->st_shndx
== SHN_UNDEF
)
1784 sec
= bfd_und_section_ptr
;
1785 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
1787 sec
= section_from_elf_index (abfd
, isym
->st_shndx
);
1789 sec
= bfd_abs_section_ptr
;
1790 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
1793 else if (isym
->st_shndx
== SHN_ABS
)
1794 sec
= bfd_abs_section_ptr
;
1795 else if (isym
->st_shndx
== SHN_COMMON
)
1797 sec
= bfd_com_section_ptr
;
1798 /* What ELF calls the size we call the value. What ELF
1799 calls the value we call the alignment. */
1800 value
= isym
->st_size
;
1804 /* Leave it up to the processor backend. */
1807 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
1809 if (name
== (const char *) NULL
)
1810 goto error_free_vers
;
1812 if (isym
->st_shndx
== SHN_COMMON
1813 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
1815 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
1819 tcomm
= bfd_make_section (abfd
, ".tcommon");
1821 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
1823 | SEC_LINKER_CREATED
1824 | SEC_THREAD_LOCAL
)))
1825 goto error_free_vers
;
1829 else if (add_symbol_hook
)
1831 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
1833 goto error_free_vers
;
1835 /* The hook function sets the name to NULL if this symbol
1836 should be skipped for some reason. */
1837 if (name
== (const char *) NULL
)
1841 /* Sanity check that all possibilities were handled. */
1842 if (sec
== (asection
*) NULL
)
1844 bfd_set_error (bfd_error_bad_value
);
1845 goto error_free_vers
;
1848 if (bfd_is_und_section (sec
)
1849 || bfd_is_com_section (sec
))
1854 size_change_ok
= FALSE
;
1855 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
1859 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1861 Elf_Internal_Versym iver
;
1862 unsigned int vernum
= 0;
1867 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
1868 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
1870 /* If this is a hidden symbol, or if it is not version
1871 1, we append the version name to the symbol name.
1872 However, we do not modify a non-hidden absolute
1873 symbol, because it might be the version symbol
1874 itself. FIXME: What if it isn't? */
1875 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
1876 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
1879 size_t namelen
, verlen
, newlen
;
1882 if (isym
->st_shndx
!= SHN_UNDEF
)
1884 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
1886 (*_bfd_error_handler
)
1887 (_("%s: %s: invalid version %u (max %d)"),
1888 bfd_archive_filename (abfd
), name
, vernum
,
1889 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1890 bfd_set_error (bfd_error_bad_value
);
1891 goto error_free_vers
;
1893 else if (vernum
> 1)
1895 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1901 /* We cannot simply test for the number of
1902 entries in the VERNEED section since the
1903 numbers for the needed versions do not start
1905 Elf_Internal_Verneed
*t
;
1908 for (t
= elf_tdata (abfd
)->verref
;
1912 Elf_Internal_Vernaux
*a
;
1914 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1916 if (a
->vna_other
== vernum
)
1918 verstr
= a
->vna_nodename
;
1927 (*_bfd_error_handler
)
1928 (_("%s: %s: invalid needed version %d"),
1929 bfd_archive_filename (abfd
), name
, vernum
);
1930 bfd_set_error (bfd_error_bad_value
);
1931 goto error_free_vers
;
1935 namelen
= strlen (name
);
1936 verlen
= strlen (verstr
);
1937 newlen
= namelen
+ verlen
+ 2;
1938 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1939 && isym
->st_shndx
!= SHN_UNDEF
)
1942 newname
= (char *) bfd_alloc (abfd
, (bfd_size_type
) newlen
);
1943 if (newname
== NULL
)
1944 goto error_free_vers
;
1945 memcpy (newname
, name
, namelen
);
1946 p
= newname
+ namelen
;
1948 /* If this is a defined non-hidden version symbol,
1949 we add another @ to the name. This indicates the
1950 default version of the symbol. */
1951 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1952 && isym
->st_shndx
!= SHN_UNDEF
)
1954 memcpy (p
, verstr
, verlen
+ 1);
1960 if (! elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
1961 sym_hash
, &skip
, &override
,
1962 &type_change_ok
, &size_change_ok
,
1964 goto error_free_vers
;
1973 while (h
->root
.type
== bfd_link_hash_indirect
1974 || h
->root
.type
== bfd_link_hash_warning
)
1975 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1977 /* Remember the old alignment if this is a common symbol, so
1978 that we don't reduce the alignment later on. We can't
1979 check later, because _bfd_generic_link_add_one_symbol
1980 will set a default for the alignment which we want to
1981 override. We also remember the old bfd where the existing
1982 definition comes from. */
1983 switch (h
->root
.type
)
1988 case bfd_link_hash_defined
:
1989 case bfd_link_hash_defweak
:
1990 old_bfd
= h
->root
.u
.def
.section
->owner
;
1993 case bfd_link_hash_common
:
1994 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
1995 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1999 if (elf_tdata (abfd
)->verdef
!= NULL
2003 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
2006 if (! (_bfd_generic_link_add_one_symbol
2007 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
2008 FALSE
, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
2009 goto error_free_vers
;
2012 while (h
->root
.type
== bfd_link_hash_indirect
2013 || h
->root
.type
== bfd_link_hash_warning
)
2014 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2017 new_weakdef
= FALSE
;
2020 && (flags
& BSF_WEAK
) != 0
2021 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
2022 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
2023 && h
->weakdef
== NULL
)
2025 /* Keep a list of all weak defined non function symbols from
2026 a dynamic object, using the weakdef field. Later in this
2027 function we will set the weakdef field to the correct
2028 value. We only put non-function symbols from dynamic
2029 objects on this list, because that happens to be the only
2030 time we need to know the normal symbol corresponding to a
2031 weak symbol, and the information is time consuming to
2032 figure out. If the weakdef field is not already NULL,
2033 then this symbol was already defined by some previous
2034 dynamic object, and we will be using that previous
2035 definition anyhow. */
2042 /* Set the alignment of a common symbol. */
2043 if (isym
->st_shndx
== SHN_COMMON
2044 && h
->root
.type
== bfd_link_hash_common
)
2048 align
= bfd_log2 (isym
->st_value
);
2049 if (align
> old_alignment
2050 /* Permit an alignment power of zero if an alignment of one
2051 is specified and no other alignments have been specified. */
2052 || (isym
->st_value
== 1 && old_alignment
== 0))
2053 h
->root
.u
.c
.p
->alignment_power
= align
;
2055 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
2058 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
2064 /* Check the alignment when a common symbol is involved. This
2065 can change when a common symbol is overriden by a normal
2066 definition or a common symbol is ignored due to the old
2067 normal definition. We need to make sure the maximum
2068 alignment is maintained. */
2069 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
2070 && h
->root
.type
!= bfd_link_hash_common
)
2072 unsigned int common_align
;
2073 unsigned int normal_align
;
2074 unsigned int symbol_align
;
2078 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
2079 if ((h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2081 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
2082 if (normal_align
> symbol_align
)
2083 normal_align
= symbol_align
;
2086 normal_align
= symbol_align
;
2090 common_align
= old_alignment
;
2091 common_bfd
= old_bfd
;
2096 common_align
= bfd_log2 (isym
->st_value
);
2098 normal_bfd
= old_bfd
;
2101 if (normal_align
< common_align
)
2102 (*_bfd_error_handler
)
2103 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
2106 bfd_archive_filename (normal_bfd
),
2108 bfd_archive_filename (common_bfd
));
2111 /* Remember the symbol size and type. */
2112 if (isym
->st_size
!= 0
2113 && (definition
|| h
->size
== 0))
2115 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
2116 (*_bfd_error_handler
)
2117 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
2118 name
, (unsigned long) h
->size
,
2119 bfd_archive_filename (old_bfd
),
2120 (unsigned long) isym
->st_size
,
2121 bfd_archive_filename (abfd
));
2123 h
->size
= isym
->st_size
;
2126 /* If this is a common symbol, then we always want H->SIZE
2127 to be the size of the common symbol. The code just above
2128 won't fix the size if a common symbol becomes larger. We
2129 don't warn about a size change here, because that is
2130 covered by --warn-common. */
2131 if (h
->root
.type
== bfd_link_hash_common
)
2132 h
->size
= h
->root
.u
.c
.size
;
2134 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
2135 && (definition
|| h
->type
== STT_NOTYPE
))
2137 if (h
->type
!= STT_NOTYPE
2138 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
2139 && ! type_change_ok
)
2140 (*_bfd_error_handler
)
2141 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
2142 name
, h
->type
, ELF_ST_TYPE (isym
->st_info
),
2143 bfd_archive_filename (abfd
));
2145 h
->type
= ELF_ST_TYPE (isym
->st_info
);
2148 /* If st_other has a processor-specific meaning, specific
2149 code might be needed here. We never merge the visibility
2150 attribute with the one from a dynamic object. */
2151 if (isym
->st_other
!= 0 && !dynamic
)
2153 unsigned char hvis
, symvis
, other
, nvis
;
2155 /* Take the balance of OTHER from the definition. */
2156 other
= (definition
? isym
->st_other
: h
->other
);
2157 other
&= ~ ELF_ST_VISIBILITY (-1);
2159 /* Combine visibilities, using the most constraining one. */
2160 hvis
= ELF_ST_VISIBILITY (h
->other
);
2161 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
2167 nvis
= hvis
< symvis
? hvis
: symvis
;
2169 h
->other
= other
| nvis
;
2172 /* Set a flag in the hash table entry indicating the type of
2173 reference or definition we just found. Keep a count of
2174 the number of dynamic symbols we find. A dynamic symbol
2175 is one which is referenced or defined by both a regular
2176 object and a shared object. */
2177 old_flags
= h
->elf_link_hash_flags
;
2183 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
2184 if (bind
!= STB_WEAK
)
2185 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
2188 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
2190 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2191 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
2197 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
2199 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
2200 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
2201 | ELF_LINK_HASH_REF_REGULAR
)) != 0
2202 || (h
->weakdef
!= NULL
2204 && h
->weakdef
->dynindx
!= -1))
2208 h
->elf_link_hash_flags
|= new_flag
;
2210 /* Check to see if we need to add an indirect symbol for
2211 the default name. */
2212 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
2213 if (! elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
2214 &sec
, &value
, &dynsym
,
2215 override
, dt_needed
))
2216 goto error_free_vers
;
2218 if (definition
&& !dynamic
)
2220 char *p
= strchr (name
, ELF_VER_CHR
);
2221 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
2223 /* Queue non-default versions so that .symver x, x@FOO
2224 aliases can be checked. */
2225 if (! nondeflt_vers
)
2227 amt
= (isymend
- isym
+ 1)
2228 * sizeof (struct elf_link_hash_entry
*);
2229 nondeflt_vers
= bfd_malloc (amt
);
2231 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
2235 if (dynsym
&& h
->dynindx
== -1)
2237 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2238 goto error_free_vers
;
2239 if (h
->weakdef
!= NULL
2241 && h
->weakdef
->dynindx
== -1)
2243 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2244 goto error_free_vers
;
2247 else if (dynsym
&& h
->dynindx
!= -1)
2248 /* If the symbol already has a dynamic index, but
2249 visibility says it should not be visible, turn it into
2251 switch (ELF_ST_VISIBILITY (h
->other
))
2255 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2259 if (dt_needed
&& definition
2260 && (h
->elf_link_hash_flags
2261 & ELF_LINK_HASH_REF_REGULAR
) != 0)
2263 bfd_size_type oldsize
;
2264 bfd_size_type strindex
;
2266 if (! is_elf_hash_table (info
))
2267 goto error_free_vers
;
2269 /* The symbol from a DT_NEEDED object is referenced from
2270 the regular object to create a dynamic executable. We
2271 have to make sure there is a DT_NEEDED entry for it. */
2274 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2275 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
,
2276 elf_dt_soname (abfd
), FALSE
);
2277 if (strindex
== (bfd_size_type
) -1)
2278 goto error_free_vers
;
2280 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2283 Elf_External_Dyn
*dyncon
, *dynconend
;
2285 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
,
2287 BFD_ASSERT (sdyn
!= NULL
);
2289 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
2290 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
2292 for (; dyncon
< dynconend
; dyncon
++)
2294 Elf_Internal_Dyn dyn
;
2296 elf_swap_dyn_in (hash_table
->dynobj
,
2298 BFD_ASSERT (dyn
.d_tag
!= DT_NEEDED
||
2299 dyn
.d_un
.d_val
!= strindex
);
2303 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
2304 goto error_free_vers
;
2309 /* Now that all the symbols from this input file are created, handle
2310 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
2311 if (nondeflt_vers
!= NULL
)
2313 bfd_size_type cnt
, symidx
;
2315 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
2317 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
2318 char *shortname
, *p
;
2320 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2322 || (h
->root
.type
!= bfd_link_hash_defined
2323 && h
->root
.type
!= bfd_link_hash_defweak
))
2326 amt
= p
- h
->root
.root
.string
;
2327 shortname
= bfd_malloc (amt
+ 1);
2328 memcpy (shortname
, h
->root
.root
.string
, amt
);
2329 shortname
[amt
] = '\0';
2331 hi
= (struct elf_link_hash_entry
*)
2332 bfd_link_hash_lookup (info
->hash
, shortname
,
2333 FALSE
, FALSE
, FALSE
);
2335 && hi
->root
.type
== h
->root
.type
2336 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
2337 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
2339 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
2340 hi
->root
.type
= bfd_link_hash_indirect
;
2341 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
2342 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
2343 sym_hash
= elf_sym_hashes (abfd
);
2345 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
2346 if (sym_hash
[symidx
] == hi
)
2348 sym_hash
[symidx
] = h
;
2354 free (nondeflt_vers
);
2355 nondeflt_vers
= NULL
;
2358 if (extversym
!= NULL
)
2364 if (isymbuf
!= NULL
)
2368 /* Now set the weakdefs field correctly for all the weak defined
2369 symbols we found. The only way to do this is to search all the
2370 symbols. Since we only need the information for non functions in
2371 dynamic objects, that's the only time we actually put anything on
2372 the list WEAKS. We need this information so that if a regular
2373 object refers to a symbol defined weakly in a dynamic object, the
2374 real symbol in the dynamic object is also put in the dynamic
2375 symbols; we also must arrange for both symbols to point to the
2376 same memory location. We could handle the general case of symbol
2377 aliasing, but a general symbol alias can only be generated in
2378 assembler code, handling it correctly would be very time
2379 consuming, and other ELF linkers don't handle general aliasing
2381 while (weaks
!= NULL
)
2383 struct elf_link_hash_entry
*hlook
;
2386 struct elf_link_hash_entry
**hpp
;
2387 struct elf_link_hash_entry
**hppend
;
2390 weaks
= hlook
->weakdef
;
2391 hlook
->weakdef
= NULL
;
2393 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
2394 || hlook
->root
.type
== bfd_link_hash_defweak
2395 || hlook
->root
.type
== bfd_link_hash_common
2396 || hlook
->root
.type
== bfd_link_hash_indirect
);
2397 slook
= hlook
->root
.u
.def
.section
;
2398 vlook
= hlook
->root
.u
.def
.value
;
2400 hpp
= elf_sym_hashes (abfd
);
2401 hppend
= hpp
+ extsymcount
;
2402 for (; hpp
< hppend
; hpp
++)
2404 struct elf_link_hash_entry
*h
;
2407 if (h
!= NULL
&& h
!= hlook
2408 && h
->root
.type
== bfd_link_hash_defined
2409 && h
->root
.u
.def
.section
== slook
2410 && h
->root
.u
.def
.value
== vlook
)
2414 /* If the weak definition is in the list of dynamic
2415 symbols, make sure the real definition is put there
2417 if (hlook
->dynindx
!= -1
2418 && h
->dynindx
== -1)
2420 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2424 /* If the real definition is in the list of dynamic
2425 symbols, make sure the weak definition is put there
2426 as well. If we don't do this, then the dynamic
2427 loader might not merge the entries for the real
2428 definition and the weak definition. */
2429 if (h
->dynindx
!= -1
2430 && hlook
->dynindx
== -1)
2432 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
2440 /* If this object is the same format as the output object, and it is
2441 not a shared library, then let the backend look through the
2444 This is required to build global offset table entries and to
2445 arrange for dynamic relocs. It is not required for the
2446 particular common case of linking non PIC code, even when linking
2447 against shared libraries, but unfortunately there is no way of
2448 knowing whether an object file has been compiled PIC or not.
2449 Looking through the relocs is not particularly time consuming.
2450 The problem is that we must either (1) keep the relocs in memory,
2451 which causes the linker to require additional runtime memory or
2452 (2) read the relocs twice from the input file, which wastes time.
2453 This would be a good case for using mmap.
2455 I have no idea how to handle linking PIC code into a file of a
2456 different format. It probably can't be done. */
2457 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
2459 && abfd
->xvec
== info
->hash
->creator
2460 && check_relocs
!= NULL
)
2464 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2466 Elf_Internal_Rela
*internal_relocs
;
2469 if ((o
->flags
& SEC_RELOC
) == 0
2470 || o
->reloc_count
== 0
2471 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
2472 && (o
->flags
& SEC_DEBUGGING
) != 0)
2473 || bfd_is_abs_section (o
->output_section
))
2476 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
2477 (abfd
, o
, (PTR
) NULL
,
2478 (Elf_Internal_Rela
*) NULL
,
2479 info
->keep_memory
));
2480 if (internal_relocs
== NULL
)
2483 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
2485 if (elf_section_data (o
)->relocs
!= internal_relocs
)
2486 free (internal_relocs
);
2493 /* If this is a non-traditional link, try to optimize the handling
2494 of the .stab/.stabstr sections. */
2496 && ! info
->traditional_format
2497 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
2498 && is_elf_hash_table (info
)
2499 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
2501 asection
*stab
, *stabstr
;
2503 stab
= bfd_get_section_by_name (abfd
, ".stab");
2505 && (stab
->flags
& SEC_MERGE
) == 0
2506 && !bfd_is_abs_section (stab
->output_section
))
2508 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
2510 if (stabstr
!= NULL
)
2512 struct bfd_elf_section_data
*secdata
;
2514 secdata
= elf_section_data (stab
);
2515 if (! _bfd_link_section_stabs (abfd
,
2516 & hash_table
->stab_info
,
2518 &secdata
->sec_info
))
2520 if (secdata
->sec_info
)
2521 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
2526 if (! info
->relocateable
&& ! dynamic
2527 && is_elf_hash_table (info
))
2531 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2532 if ((s
->flags
& SEC_MERGE
) != 0
2533 && !bfd_is_abs_section (s
->output_section
))
2535 struct bfd_elf_section_data
*secdata
;
2537 secdata
= elf_section_data (s
);
2538 if (! _bfd_merge_section (abfd
,
2539 & hash_table
->merge_info
,
2540 s
, &secdata
->sec_info
))
2542 else if (secdata
->sec_info
)
2543 s
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
2547 if (is_elf_hash_table (info
))
2549 /* Add this bfd to the loaded list. */
2550 struct elf_link_loaded_list
*n
;
2552 n
= ((struct elf_link_loaded_list
*)
2553 bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
)));
2557 n
->next
= hash_table
->loaded
;
2558 hash_table
->loaded
= n
;
2564 if (nondeflt_vers
!= NULL
)
2565 free (nondeflt_vers
);
2566 if (extversym
!= NULL
)
2569 if (isymbuf
!= NULL
)
2575 /* Create some sections which will be filled in with dynamic linking
2576 information. ABFD is an input file which requires dynamic sections
2577 to be created. The dynamic sections take up virtual memory space
2578 when the final executable is run, so we need to create them before
2579 addresses are assigned to the output sections. We work out the
2580 actual contents and size of these sections later. */
2583 elf_link_create_dynamic_sections (abfd
, info
)
2585 struct bfd_link_info
*info
;
2588 register asection
*s
;
2589 struct elf_link_hash_entry
*h
;
2590 struct bfd_link_hash_entry
*bh
;
2591 struct elf_backend_data
*bed
;
2593 if (! is_elf_hash_table (info
))
2596 if (elf_hash_table (info
)->dynamic_sections_created
)
2599 /* Make sure that all dynamic sections use the same input BFD. */
2600 if (elf_hash_table (info
)->dynobj
== NULL
)
2601 elf_hash_table (info
)->dynobj
= abfd
;
2603 abfd
= elf_hash_table (info
)->dynobj
;
2605 /* Note that we set the SEC_IN_MEMORY flag for all of these
2607 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
2608 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
2610 /* A dynamically linked executable has a .interp section, but a
2611 shared library does not. */
2614 s
= bfd_make_section (abfd
, ".interp");
2616 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2620 if (! info
->traditional_format
2621 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
2623 s
= bfd_make_section (abfd
, ".eh_frame_hdr");
2625 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2626 || ! bfd_set_section_alignment (abfd
, s
, 2))
2628 elf_hash_table (info
)->eh_info
.hdr_sec
= s
;
2631 /* Create sections to hold version informations. These are removed
2632 if they are not needed. */
2633 s
= bfd_make_section (abfd
, ".gnu.version_d");
2635 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2636 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2639 s
= bfd_make_section (abfd
, ".gnu.version");
2641 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2642 || ! bfd_set_section_alignment (abfd
, s
, 1))
2645 s
= bfd_make_section (abfd
, ".gnu.version_r");
2647 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2648 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2651 s
= bfd_make_section (abfd
, ".dynsym");
2653 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2654 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2657 s
= bfd_make_section (abfd
, ".dynstr");
2659 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2662 /* Create a strtab to hold the dynamic symbol names. */
2663 if (elf_hash_table (info
)->dynstr
== NULL
)
2665 elf_hash_table (info
)->dynstr
= _bfd_elf_strtab_init ();
2666 if (elf_hash_table (info
)->dynstr
== NULL
)
2670 s
= bfd_make_section (abfd
, ".dynamic");
2672 || ! bfd_set_section_flags (abfd
, s
, flags
)
2673 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2676 /* The special symbol _DYNAMIC is always set to the start of the
2677 .dynamic section. This call occurs before we have processed the
2678 symbols for any dynamic object, so we don't have to worry about
2679 overriding a dynamic definition. We could set _DYNAMIC in a
2680 linker script, but we only want to define it if we are, in fact,
2681 creating a .dynamic section. We don't want to define it if there
2682 is no .dynamic section, since on some ELF platforms the start up
2683 code examines it to decide how to initialize the process. */
2685 if (! (_bfd_generic_link_add_one_symbol
2686 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
2687 (const char *) 0, FALSE
, get_elf_backend_data (abfd
)->collect
, &bh
)))
2689 h
= (struct elf_link_hash_entry
*) bh
;
2690 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2691 h
->type
= STT_OBJECT
;
2694 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2697 bed
= get_elf_backend_data (abfd
);
2699 s
= bfd_make_section (abfd
, ".hash");
2701 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2702 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2704 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
2706 /* Let the backend create the rest of the sections. This lets the
2707 backend set the right flags. The backend will normally create
2708 the .got and .plt sections. */
2709 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
2712 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
2717 /* Add an entry to the .dynamic table. */
2720 elf_add_dynamic_entry (info
, tag
, val
)
2721 struct bfd_link_info
*info
;
2725 Elf_Internal_Dyn dyn
;
2728 bfd_size_type newsize
;
2729 bfd_byte
*newcontents
;
2731 if (! is_elf_hash_table (info
))
2734 dynobj
= elf_hash_table (info
)->dynobj
;
2736 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
2737 BFD_ASSERT (s
!= NULL
);
2739 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
2740 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
2741 if (newcontents
== NULL
)
2745 dyn
.d_un
.d_val
= val
;
2746 elf_swap_dyn_out (dynobj
, &dyn
,
2747 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
2749 s
->_raw_size
= newsize
;
2750 s
->contents
= newcontents
;
2755 /* Read and swap the relocs from the section indicated by SHDR. This
2756 may be either a REL or a RELA section. The relocations are
2757 translated into RELA relocations and stored in INTERNAL_RELOCS,
2758 which should have already been allocated to contain enough space.
2759 The EXTERNAL_RELOCS are a buffer where the external form of the
2760 relocations should be stored.
2762 Returns FALSE if something goes wrong. */
2765 elf_link_read_relocs_from_section (abfd
, shdr
, external_relocs
,
2768 Elf_Internal_Shdr
*shdr
;
2769 PTR external_relocs
;
2770 Elf_Internal_Rela
*internal_relocs
;
2772 struct elf_backend_data
*bed
;
2773 void (*swap_in
) PARAMS ((bfd
*, const bfd_byte
*, Elf_Internal_Rela
*));
2774 const bfd_byte
*erela
;
2775 const bfd_byte
*erelaend
;
2776 Elf_Internal_Rela
*irela
;
2778 /* If there aren't any relocations, that's OK. */
2782 /* Position ourselves at the start of the section. */
2783 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2786 /* Read the relocations. */
2787 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2790 bed
= get_elf_backend_data (abfd
);
2792 /* Convert the external relocations to the internal format. */
2793 if (shdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2794 swap_in
= bed
->s
->swap_reloc_in
;
2795 else if (shdr
->sh_entsize
== sizeof (Elf_External_Rela
))
2796 swap_in
= bed
->s
->swap_reloca_in
;
2799 bfd_set_error (bfd_error_wrong_format
);
2803 erela
= external_relocs
;
2804 erelaend
= erela
+ NUM_SHDR_ENTRIES (shdr
) * shdr
->sh_entsize
;
2805 irela
= internal_relocs
;
2806 while (erela
< erelaend
)
2808 (*swap_in
) (abfd
, erela
, irela
);
2809 irela
+= bed
->s
->int_rels_per_ext_rel
;
2810 erela
+= shdr
->sh_entsize
;
2816 /* Read and swap the relocs for a section O. They may have been
2817 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2818 not NULL, they are used as buffers to read into. They are known to
2819 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2820 the return value is allocated using either malloc or bfd_alloc,
2821 according to the KEEP_MEMORY argument. If O has two relocation
2822 sections (both REL and RELA relocations), then the REL_HDR
2823 relocations will appear first in INTERNAL_RELOCS, followed by the
2824 REL_HDR2 relocations. */
2827 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
2831 PTR external_relocs
;
2832 Elf_Internal_Rela
*internal_relocs
;
2833 bfd_boolean keep_memory
;
2835 Elf_Internal_Shdr
*rel_hdr
;
2837 Elf_Internal_Rela
*alloc2
= NULL
;
2838 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2840 if (elf_section_data (o
)->relocs
!= NULL
)
2841 return elf_section_data (o
)->relocs
;
2843 if (o
->reloc_count
== 0)
2846 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2848 if (internal_relocs
== NULL
)
2852 size
= o
->reloc_count
;
2853 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2855 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2857 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2858 if (internal_relocs
== NULL
)
2862 if (external_relocs
== NULL
)
2864 bfd_size_type size
= rel_hdr
->sh_size
;
2866 if (elf_section_data (o
)->rel_hdr2
)
2867 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2868 alloc1
= (PTR
) bfd_malloc (size
);
2871 external_relocs
= alloc1
;
2874 if (!elf_link_read_relocs_from_section (abfd
, rel_hdr
,
2878 if (!elf_link_read_relocs_from_section
2880 elf_section_data (o
)->rel_hdr2
,
2881 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2882 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2883 * bed
->s
->int_rels_per_ext_rel
)))
2886 /* Cache the results for next time, if we can. */
2888 elf_section_data (o
)->relocs
= internal_relocs
;
2893 /* Don't free alloc2, since if it was allocated we are passing it
2894 back (under the name of internal_relocs). */
2896 return internal_relocs
;
2906 /* Record an assignment to a symbol made by a linker script. We need
2907 this in case some dynamic object refers to this symbol. */
2910 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
2911 bfd
*output_bfd ATTRIBUTE_UNUSED
;
2912 struct bfd_link_info
*info
;
2914 bfd_boolean provide
;
2916 struct elf_link_hash_entry
*h
;
2918 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2921 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, TRUE
, FALSE
);
2925 if (h
->root
.type
== bfd_link_hash_new
)
2926 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_ELF
;
2928 /* If this symbol is being provided by the linker script, and it is
2929 currently defined by a dynamic object, but not by a regular
2930 object, then mark it as undefined so that the generic linker will
2931 force the correct value. */
2933 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2934 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2935 h
->root
.type
= bfd_link_hash_undefined
;
2937 /* If this symbol is not being provided by the linker script, and it is
2938 currently defined by a dynamic object, but not by a regular object,
2939 then clear out any version information because the symbol will not be
2940 associated with the dynamic object any more. */
2942 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2943 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2944 h
->verinfo
.verdef
= NULL
;
2946 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2948 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2949 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
2951 && h
->dynindx
== -1)
2953 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2956 /* If this is a weak defined symbol, and we know a corresponding
2957 real symbol from the same dynamic object, make sure the real
2958 symbol is also made into a dynamic symbol. */
2959 if (h
->weakdef
!= NULL
2960 && h
->weakdef
->dynindx
== -1)
2962 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2970 /* This structure is used to pass information to
2971 elf_link_assign_sym_version. */
2973 struct elf_assign_sym_version_info
2977 /* General link information. */
2978 struct bfd_link_info
*info
;
2980 struct bfd_elf_version_tree
*verdefs
;
2981 /* Whether we had a failure. */
2985 /* This structure is used to pass information to
2986 elf_link_find_version_dependencies. */
2988 struct elf_find_verdep_info
2992 /* General link information. */
2993 struct bfd_link_info
*info
;
2994 /* The number of dependencies. */
2996 /* Whether we had a failure. */
3000 /* Array used to determine the number of hash table buckets to use
3001 based on the number of symbols there are. If there are fewer than
3002 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
3003 fewer than 37 we use 17 buckets, and so forth. We never use more
3004 than 32771 buckets. */
3006 static const size_t elf_buckets
[] =
3008 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
3012 /* Compute bucket count for hashing table. We do not use a static set
3013 of possible tables sizes anymore. Instead we determine for all
3014 possible reasonable sizes of the table the outcome (i.e., the
3015 number of collisions etc) and choose the best solution. The
3016 weighting functions are not too simple to allow the table to grow
3017 without bounds. Instead one of the weighting factors is the size.
3018 Therefore the result is always a good payoff between few collisions
3019 (= short chain lengths) and table size. */
3021 compute_bucket_count (info
)
3022 struct bfd_link_info
*info
;
3024 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
3025 size_t best_size
= 0;
3026 unsigned long int *hashcodes
;
3027 unsigned long int *hashcodesp
;
3028 unsigned long int i
;
3031 /* Compute the hash values for all exported symbols. At the same
3032 time store the values in an array so that we could use them for
3035 amt
*= sizeof (unsigned long int);
3036 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
3037 if (hashcodes
== NULL
)
3039 hashcodesp
= hashcodes
;
3041 /* Put all hash values in HASHCODES. */
3042 elf_link_hash_traverse (elf_hash_table (info
),
3043 elf_collect_hash_codes
, &hashcodesp
);
3045 /* We have a problem here. The following code to optimize the table
3046 size requires an integer type with more the 32 bits. If
3047 BFD_HOST_U_64_BIT is set we know about such a type. */
3048 #ifdef BFD_HOST_U_64_BIT
3051 unsigned long int nsyms
= hashcodesp
- hashcodes
;
3054 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
3055 unsigned long int *counts
;
3057 /* Possible optimization parameters: if we have NSYMS symbols we say
3058 that the hashing table must at least have NSYMS/4 and at most
3060 minsize
= nsyms
/ 4;
3063 best_size
= maxsize
= nsyms
* 2;
3065 /* Create array where we count the collisions in. We must use bfd_malloc
3066 since the size could be large. */
3068 amt
*= sizeof (unsigned long int);
3069 counts
= (unsigned long int *) bfd_malloc (amt
);
3076 /* Compute the "optimal" size for the hash table. The criteria is a
3077 minimal chain length. The minor criteria is (of course) the size
3079 for (i
= minsize
; i
< maxsize
; ++i
)
3081 /* Walk through the array of hashcodes and count the collisions. */
3082 BFD_HOST_U_64_BIT max
;
3083 unsigned long int j
;
3084 unsigned long int fact
;
3086 memset (counts
, '\0', i
* sizeof (unsigned long int));
3088 /* Determine how often each hash bucket is used. */
3089 for (j
= 0; j
< nsyms
; ++j
)
3090 ++counts
[hashcodes
[j
] % i
];
3092 /* For the weight function we need some information about the
3093 pagesize on the target. This is information need not be 100%
3094 accurate. Since this information is not available (so far) we
3095 define it here to a reasonable default value. If it is crucial
3096 to have a better value some day simply define this value. */
3097 # ifndef BFD_TARGET_PAGESIZE
3098 # define BFD_TARGET_PAGESIZE (4096)
3101 /* We in any case need 2 + NSYMS entries for the size values and
3103 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
3106 /* Variant 1: optimize for short chains. We add the squares
3107 of all the chain lengths (which favous many small chain
3108 over a few long chains). */
3109 for (j
= 0; j
< i
; ++j
)
3110 max
+= counts
[j
] * counts
[j
];
3112 /* This adds penalties for the overall size of the table. */
3113 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
3116 /* Variant 2: Optimize a lot more for small table. Here we
3117 also add squares of the size but we also add penalties for
3118 empty slots (the +1 term). */
3119 for (j
= 0; j
< i
; ++j
)
3120 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
3122 /* The overall size of the table is considered, but not as
3123 strong as in variant 1, where it is squared. */
3124 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
3128 /* Compare with current best results. */
3129 if (max
< best_chlen
)
3139 #endif /* defined (BFD_HOST_U_64_BIT) */
3141 /* This is the fallback solution if no 64bit type is available or if we
3142 are not supposed to spend much time on optimizations. We select the
3143 bucket count using a fixed set of numbers. */
3144 for (i
= 0; elf_buckets
[i
] != 0; i
++)
3146 best_size
= elf_buckets
[i
];
3147 if (dynsymcount
< elf_buckets
[i
+ 1])
3152 /* Free the arrays we needed. */
3158 /* Set up the sizes and contents of the ELF dynamic sections. This is
3159 called by the ELF linker emulation before_allocation routine. We
3160 must set the sizes of the sections before the linker sets the
3161 addresses of the various sections. */
3164 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
3166 auxiliary_filters
, info
, sinterpptr
,
3171 const char *filter_shlib
;
3172 const char * const *auxiliary_filters
;
3173 struct bfd_link_info
*info
;
3174 asection
**sinterpptr
;
3175 struct bfd_elf_version_tree
*verdefs
;
3177 bfd_size_type soname_indx
;
3179 struct elf_backend_data
*bed
;
3180 struct elf_assign_sym_version_info asvinfo
;
3184 soname_indx
= (bfd_size_type
) -1;
3186 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
3189 if (! is_elf_hash_table (info
))
3192 /* Any syms created from now on start with -1 in
3193 got.refcount/offset and plt.refcount/offset. */
3194 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
3196 /* The backend may have to create some sections regardless of whether
3197 we're dynamic or not. */
3198 bed
= get_elf_backend_data (output_bfd
);
3199 if (bed
->elf_backend_always_size_sections
3200 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
3203 dynobj
= elf_hash_table (info
)->dynobj
;
3205 /* If there were no dynamic objects in the link, there is nothing to
3210 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
3213 if (elf_hash_table (info
)->dynamic_sections_created
)
3215 struct elf_info_failed eif
;
3216 struct elf_link_hash_entry
*h
;
3218 struct bfd_elf_version_tree
*t
;
3219 struct bfd_elf_version_expr
*d
;
3220 bfd_boolean all_defined
;
3222 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
3223 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
3227 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3229 if (soname_indx
== (bfd_size_type
) -1
3230 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SONAME
,
3237 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMBOLIC
,
3240 info
->flags
|= DF_SYMBOLIC
;
3247 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
3249 if (info
->new_dtags
)
3250 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
3251 if (indx
== (bfd_size_type
) -1
3252 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RPATH
, indx
)
3254 && ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RUNPATH
,
3259 if (filter_shlib
!= NULL
)
3263 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3264 filter_shlib
, TRUE
);
3265 if (indx
== (bfd_size_type
) -1
3266 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FILTER
, indx
))
3270 if (auxiliary_filters
!= NULL
)
3272 const char * const *p
;
3274 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
3278 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3280 if (indx
== (bfd_size_type
) -1
3281 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_AUXILIARY
,
3288 eif
.verdefs
= verdefs
;
3291 /* If we are supposed to export all symbols into the dynamic symbol
3292 table (this is not the normal case), then do so. */
3293 if (info
->export_dynamic
)
3295 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
3301 /* Make all global versions with definiton. */
3302 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3303 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3304 if (!d
->symver
&& strchr (d
->pattern
, '*') == NULL
)
3306 const char *verstr
, *name
;
3307 size_t namelen
, verlen
, newlen
;
3309 struct elf_link_hash_entry
*newh
;
3312 namelen
= strlen (name
);
3314 verlen
= strlen (verstr
);
3315 newlen
= namelen
+ verlen
+ 3;
3317 newname
= (char *) bfd_malloc ((bfd_size_type
) newlen
);
3318 if (newname
== NULL
)
3320 memcpy (newname
, name
, namelen
);
3322 /* Check the hidden versioned definition. */
3323 p
= newname
+ namelen
;
3325 memcpy (p
, verstr
, verlen
+ 1);
3326 newh
= elf_link_hash_lookup (elf_hash_table (info
),
3327 newname
, FALSE
, FALSE
,
3330 || (newh
->root
.type
!= bfd_link_hash_defined
3331 && newh
->root
.type
!= bfd_link_hash_defweak
))
3333 /* Check the default versioned definition. */
3335 memcpy (p
, verstr
, verlen
+ 1);
3336 newh
= elf_link_hash_lookup (elf_hash_table (info
),
3337 newname
, FALSE
, FALSE
,
3342 /* Mark this version if there is a definition and it is
3343 not defined in a shared object. */
3345 && ((newh
->elf_link_hash_flags
3346 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)
3347 && (newh
->root
.type
== bfd_link_hash_defined
3348 || newh
->root
.type
== bfd_link_hash_defweak
))
3352 /* Attach all the symbols to their version information. */
3353 asvinfo
.output_bfd
= output_bfd
;
3354 asvinfo
.info
= info
;
3355 asvinfo
.verdefs
= verdefs
;
3356 asvinfo
.failed
= FALSE
;
3358 elf_link_hash_traverse (elf_hash_table (info
),
3359 elf_link_assign_sym_version
,
3364 if (!info
->allow_undefined_version
)
3366 /* Check if all global versions have a definiton. */
3368 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3369 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3370 if (!d
->symver
&& !d
->script
3371 && strchr (d
->pattern
, '*') == NULL
)
3373 (*_bfd_error_handler
)
3374 (_("%s: undefined version: %s"),
3375 d
->pattern
, t
->name
);
3376 all_defined
= FALSE
;
3381 bfd_set_error (bfd_error_bad_value
);
3386 /* Find all symbols which were defined in a dynamic object and make
3387 the backend pick a reasonable value for them. */
3388 elf_link_hash_traverse (elf_hash_table (info
),
3389 elf_adjust_dynamic_symbol
,
3394 /* Add some entries to the .dynamic section. We fill in some of the
3395 values later, in elf_bfd_final_link, but we must add the entries
3396 now so that we know the final size of the .dynamic section. */
3398 /* If there are initialization and/or finalization functions to
3399 call then add the corresponding DT_INIT/DT_FINI entries. */
3400 h
= (info
->init_function
3401 ? elf_link_hash_lookup (elf_hash_table (info
),
3402 info
->init_function
, FALSE
,
3406 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3407 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3409 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT
, (bfd_vma
) 0))
3412 h
= (info
->fini_function
3413 ? elf_link_hash_lookup (elf_hash_table (info
),
3414 info
->fini_function
, FALSE
,
3418 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3419 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3421 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI
, (bfd_vma
) 0))
3425 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
3427 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3433 for (sub
= info
->input_bfds
; sub
!= NULL
;
3434 sub
= sub
->link_next
)
3435 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3436 if (elf_section_data (o
)->this_hdr
.sh_type
3437 == SHT_PREINIT_ARRAY
)
3439 (*_bfd_error_handler
)
3440 (_("%s: .preinit_array section is not allowed in DSO"),
3441 bfd_archive_filename (sub
));
3445 bfd_set_error (bfd_error_nonrepresentable_section
);
3449 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAY
,
3451 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAYSZ
,
3455 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
3457 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAY
,
3459 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAYSZ
,
3463 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
3465 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAY
,
3467 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAYSZ
,
3472 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
3473 /* If .dynstr is excluded from the link, we don't want any of
3474 these tags. Strictly, we should be checking each section
3475 individually; This quick check covers for the case where
3476 someone does a /DISCARD/ : { *(*) }. */
3477 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
3479 bfd_size_type strsize
;
3481 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3482 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_HASH
, (bfd_vma
) 0)
3483 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRTAB
, (bfd_vma
) 0)
3484 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMTAB
, (bfd_vma
) 0)
3485 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRSZ
, strsize
)
3486 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMENT
,
3487 (bfd_vma
) sizeof (Elf_External_Sym
)))
3492 /* The backend must work out the sizes of all the other dynamic
3494 if (bed
->elf_backend_size_dynamic_sections
3495 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
3498 if (elf_hash_table (info
)->dynamic_sections_created
)
3500 bfd_size_type dynsymcount
;
3502 size_t bucketcount
= 0;
3503 size_t hash_entry_size
;
3504 unsigned int dtagcount
;
3506 /* Set up the version definition section. */
3507 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3508 BFD_ASSERT (s
!= NULL
);
3510 /* We may have created additional version definitions if we are
3511 just linking a regular application. */
3512 verdefs
= asvinfo
.verdefs
;
3514 /* Skip anonymous version tag. */
3515 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
3516 verdefs
= verdefs
->next
;
3518 if (verdefs
== NULL
)
3519 _bfd_strip_section_from_output (info
, s
);
3524 struct bfd_elf_version_tree
*t
;
3526 Elf_Internal_Verdef def
;
3527 Elf_Internal_Verdaux defaux
;
3532 /* Make space for the base version. */
3533 size
+= sizeof (Elf_External_Verdef
);
3534 size
+= sizeof (Elf_External_Verdaux
);
3537 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3539 struct bfd_elf_version_deps
*n
;
3541 size
+= sizeof (Elf_External_Verdef
);
3542 size
+= sizeof (Elf_External_Verdaux
);
3545 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3546 size
+= sizeof (Elf_External_Verdaux
);
3549 s
->_raw_size
= size
;
3550 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3551 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3554 /* Fill in the version definition section. */
3558 def
.vd_version
= VER_DEF_CURRENT
;
3559 def
.vd_flags
= VER_FLG_BASE
;
3562 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3563 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3564 + sizeof (Elf_External_Verdaux
));
3566 if (soname_indx
!= (bfd_size_type
) -1)
3568 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3570 def
.vd_hash
= bfd_elf_hash (soname
);
3571 defaux
.vda_name
= soname_indx
;
3578 name
= basename (output_bfd
->filename
);
3579 def
.vd_hash
= bfd_elf_hash (name
);
3580 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3582 if (indx
== (bfd_size_type
) -1)
3584 defaux
.vda_name
= indx
;
3586 defaux
.vda_next
= 0;
3588 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3589 (Elf_External_Verdef
*) p
);
3590 p
+= sizeof (Elf_External_Verdef
);
3591 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3592 (Elf_External_Verdaux
*) p
);
3593 p
+= sizeof (Elf_External_Verdaux
);
3595 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3598 struct bfd_elf_version_deps
*n
;
3599 struct elf_link_hash_entry
*h
;
3600 struct bfd_link_hash_entry
*bh
;
3603 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3606 /* Add a symbol representing this version. */
3608 if (! (_bfd_generic_link_add_one_symbol
3609 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
3610 (bfd_vma
) 0, (const char *) NULL
, FALSE
,
3611 get_elf_backend_data (dynobj
)->collect
, &bh
)))
3613 h
= (struct elf_link_hash_entry
*) bh
;
3614 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
3615 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3616 h
->type
= STT_OBJECT
;
3617 h
->verinfo
.vertree
= t
;
3619 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
3622 def
.vd_version
= VER_DEF_CURRENT
;
3624 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
3625 def
.vd_flags
|= VER_FLG_WEAK
;
3626 def
.vd_ndx
= t
->vernum
+ 1;
3627 def
.vd_cnt
= cdeps
+ 1;
3628 def
.vd_hash
= bfd_elf_hash (t
->name
);
3629 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3630 if (t
->next
!= NULL
)
3631 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3632 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
3636 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3637 (Elf_External_Verdef
*) p
);
3638 p
+= sizeof (Elf_External_Verdef
);
3640 defaux
.vda_name
= h
->dynstr_index
;
3641 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3643 if (t
->deps
== NULL
)
3644 defaux
.vda_next
= 0;
3646 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3647 t
->name_indx
= defaux
.vda_name
;
3649 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3650 (Elf_External_Verdaux
*) p
);
3651 p
+= sizeof (Elf_External_Verdaux
);
3653 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3655 if (n
->version_needed
== NULL
)
3657 /* This can happen if there was an error in the
3659 defaux
.vda_name
= 0;
3663 defaux
.vda_name
= n
->version_needed
->name_indx
;
3664 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3667 if (n
->next
== NULL
)
3668 defaux
.vda_next
= 0;
3670 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3672 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3673 (Elf_External_Verdaux
*) p
);
3674 p
+= sizeof (Elf_External_Verdaux
);
3678 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEF
, (bfd_vma
) 0)
3679 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEFNUM
,
3683 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
3686 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
3688 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS
, info
->flags
))
3695 info
->flags_1
&= ~ (DF_1_INITFIRST
3698 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS_1
,
3703 /* Work out the size of the version reference section. */
3705 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3706 BFD_ASSERT (s
!= NULL
);
3708 struct elf_find_verdep_info sinfo
;
3710 sinfo
.output_bfd
= output_bfd
;
3712 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
3713 if (sinfo
.vers
== 0)
3715 sinfo
.failed
= FALSE
;
3717 elf_link_hash_traverse (elf_hash_table (info
),
3718 elf_link_find_version_dependencies
,
3721 if (elf_tdata (output_bfd
)->verref
== NULL
)
3722 _bfd_strip_section_from_output (info
, s
);
3725 Elf_Internal_Verneed
*t
;
3730 /* Build the version definition section. */
3733 for (t
= elf_tdata (output_bfd
)->verref
;
3737 Elf_Internal_Vernaux
*a
;
3739 size
+= sizeof (Elf_External_Verneed
);
3741 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3742 size
+= sizeof (Elf_External_Vernaux
);
3745 s
->_raw_size
= size
;
3746 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3747 if (s
->contents
== NULL
)
3751 for (t
= elf_tdata (output_bfd
)->verref
;
3756 Elf_Internal_Vernaux
*a
;
3760 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3763 t
->vn_version
= VER_NEED_CURRENT
;
3765 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3766 elf_dt_name (t
->vn_bfd
) != NULL
3767 ? elf_dt_name (t
->vn_bfd
)
3768 : basename (t
->vn_bfd
->filename
),
3770 if (indx
== (bfd_size_type
) -1)
3773 t
->vn_aux
= sizeof (Elf_External_Verneed
);
3774 if (t
->vn_nextref
== NULL
)
3777 t
->vn_next
= (sizeof (Elf_External_Verneed
)
3778 + caux
* sizeof (Elf_External_Vernaux
));
3780 _bfd_elf_swap_verneed_out (output_bfd
, t
,
3781 (Elf_External_Verneed
*) p
);
3782 p
+= sizeof (Elf_External_Verneed
);
3784 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3786 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
3787 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3788 a
->vna_nodename
, FALSE
);
3789 if (indx
== (bfd_size_type
) -1)
3792 if (a
->vna_nextptr
== NULL
)
3795 a
->vna_next
= sizeof (Elf_External_Vernaux
);
3797 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
3798 (Elf_External_Vernaux
*) p
);
3799 p
+= sizeof (Elf_External_Vernaux
);
3803 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEED
,
3805 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEEDNUM
,
3809 elf_tdata (output_bfd
)->cverrefs
= crefs
;
3813 /* Assign dynsym indicies. In a shared library we generate a
3814 section symbol for each output section, which come first.
3815 Next come all of the back-end allocated local dynamic syms,
3816 followed by the rest of the global symbols. */
3818 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3820 /* Work out the size of the symbol version section. */
3821 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3822 BFD_ASSERT (s
!= NULL
);
3823 if (dynsymcount
== 0
3824 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
3826 _bfd_strip_section_from_output (info
, s
);
3827 /* The DYNSYMCOUNT might have changed if we were going to
3828 output a dynamic symbol table entry for S. */
3829 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3833 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
3834 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3835 if (s
->contents
== NULL
)
3838 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERSYM
, (bfd_vma
) 0))
3842 /* Set the size of the .dynsym and .hash sections. We counted
3843 the number of dynamic symbols in elf_link_add_object_symbols.
3844 We will build the contents of .dynsym and .hash when we build
3845 the final symbol table, because until then we do not know the
3846 correct value to give the symbols. We built the .dynstr
3847 section as we went along in elf_link_add_object_symbols. */
3848 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
3849 BFD_ASSERT (s
!= NULL
);
3850 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
3851 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3852 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3855 if (dynsymcount
!= 0)
3857 Elf_Internal_Sym isym
;
3859 /* The first entry in .dynsym is a dummy symbol. */
3866 elf_swap_symbol_out (output_bfd
, &isym
, (PTR
) s
->contents
, (PTR
) 0);
3869 /* Compute the size of the hashing table. As a side effect this
3870 computes the hash values for all the names we export. */
3871 bucketcount
= compute_bucket_count (info
);
3873 s
= bfd_get_section_by_name (dynobj
, ".hash");
3874 BFD_ASSERT (s
!= NULL
);
3875 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
3876 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
3877 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3878 if (s
->contents
== NULL
)
3881 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) bucketcount
,
3883 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) dynsymcount
,
3884 s
->contents
+ hash_entry_size
);
3886 elf_hash_table (info
)->bucketcount
= bucketcount
;
3888 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
3889 BFD_ASSERT (s
!= NULL
);
3891 elf_finalize_dynstr (output_bfd
, info
);
3893 s
->_raw_size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3895 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
3896 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NULL
, (bfd_vma
) 0))
3903 /* This function is used to adjust offsets into .dynstr for
3904 dynamic symbols. This is called via elf_link_hash_traverse. */
3906 static bfd_boolean elf_adjust_dynstr_offsets
3907 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3910 elf_adjust_dynstr_offsets (h
, data
)
3911 struct elf_link_hash_entry
*h
;
3914 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3916 if (h
->root
.type
== bfd_link_hash_warning
)
3917 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3919 if (h
->dynindx
!= -1)
3920 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3924 /* Assign string offsets in .dynstr, update all structures referencing
3928 elf_finalize_dynstr (output_bfd
, info
)
3930 struct bfd_link_info
*info
;
3932 struct elf_link_local_dynamic_entry
*entry
;
3933 struct elf_strtab_hash
*dynstr
= elf_hash_table (info
)->dynstr
;
3934 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
3937 Elf_External_Dyn
*dyncon
, *dynconend
;
3939 _bfd_elf_strtab_finalize (dynstr
);
3940 size
= _bfd_elf_strtab_size (dynstr
);
3942 /* Update all .dynamic entries referencing .dynstr strings. */
3943 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3944 BFD_ASSERT (sdyn
!= NULL
);
3946 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
3947 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
3949 for (; dyncon
< dynconend
; dyncon
++)
3951 Elf_Internal_Dyn dyn
;
3953 elf_swap_dyn_in (dynobj
, dyncon
, & dyn
);
3957 dyn
.d_un
.d_val
= size
;
3958 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3966 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3967 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3974 /* Now update local dynamic symbols. */
3975 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
3976 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3977 entry
->isym
.st_name
);
3979 /* And the rest of dynamic symbols. */
3980 elf_link_hash_traverse (elf_hash_table (info
),
3981 elf_adjust_dynstr_offsets
, dynstr
);
3983 /* Adjust version definitions. */
3984 if (elf_tdata (output_bfd
)->cverdefs
)
3989 Elf_Internal_Verdef def
;
3990 Elf_Internal_Verdaux defaux
;
3992 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3993 p
= (bfd_byte
*) s
->contents
;
3996 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3998 p
+= sizeof (Elf_External_Verdef
);
3999 for (i
= 0; i
< def
.vd_cnt
; ++i
)
4001 _bfd_elf_swap_verdaux_in (output_bfd
,
4002 (Elf_External_Verdaux
*) p
, &defaux
);
4003 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
4005 _bfd_elf_swap_verdaux_out (output_bfd
,
4006 &defaux
, (Elf_External_Verdaux
*) p
);
4007 p
+= sizeof (Elf_External_Verdaux
);
4010 while (def
.vd_next
);
4013 /* Adjust version references. */
4014 if (elf_tdata (output_bfd
)->verref
)
4019 Elf_Internal_Verneed need
;
4020 Elf_Internal_Vernaux needaux
;
4022 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
4023 p
= (bfd_byte
*) s
->contents
;
4026 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
4028 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
4029 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
4030 (Elf_External_Verneed
*) p
);
4031 p
+= sizeof (Elf_External_Verneed
);
4032 for (i
= 0; i
< need
.vn_cnt
; ++i
)
4034 _bfd_elf_swap_vernaux_in (output_bfd
,
4035 (Elf_External_Vernaux
*) p
, &needaux
);
4036 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
4038 _bfd_elf_swap_vernaux_out (output_bfd
,
4040 (Elf_External_Vernaux
*) p
);
4041 p
+= sizeof (Elf_External_Vernaux
);
4044 while (need
.vn_next
);
4050 /* Fix up the flags for a symbol. This handles various cases which
4051 can only be fixed after all the input files are seen. This is
4052 currently called by both adjust_dynamic_symbol and
4053 assign_sym_version, which is unnecessary but perhaps more robust in
4054 the face of future changes. */
4057 elf_fix_symbol_flags (h
, eif
)
4058 struct elf_link_hash_entry
*h
;
4059 struct elf_info_failed
*eif
;
4061 /* If this symbol was mentioned in a non-ELF file, try to set
4062 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
4063 permit a non-ELF file to correctly refer to a symbol defined in
4064 an ELF dynamic object. */
4065 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
4067 while (h
->root
.type
== bfd_link_hash_indirect
)
4068 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4070 if (h
->root
.type
!= bfd_link_hash_defined
4071 && h
->root
.type
!= bfd_link_hash_defweak
)
4072 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
4073 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
4076 if (h
->root
.u
.def
.section
->owner
!= NULL
4077 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
4078 == bfd_target_elf_flavour
))
4079 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
4080 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
4082 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4085 if (h
->dynindx
== -1
4086 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
4087 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
4089 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
4098 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
4099 was first seen in a non-ELF file. Fortunately, if the symbol
4100 was first seen in an ELF file, we're probably OK unless the
4101 symbol was defined in a non-ELF file. Catch that case here.
4102 FIXME: We're still in trouble if the symbol was first seen in
4103 a dynamic object, and then later in a non-ELF regular object. */
4104 if ((h
->root
.type
== bfd_link_hash_defined
4105 || h
->root
.type
== bfd_link_hash_defweak
)
4106 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
4107 && (h
->root
.u
.def
.section
->owner
!= NULL
4108 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
4109 != bfd_target_elf_flavour
)
4110 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
4111 && (h
->elf_link_hash_flags
4112 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
4113 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4116 /* If this is a final link, and the symbol was defined as a common
4117 symbol in a regular object file, and there was no definition in
4118 any dynamic object, then the linker will have allocated space for
4119 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
4120 flag will not have been set. */
4121 if (h
->root
.type
== bfd_link_hash_defined
4122 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
4123 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
4124 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4125 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
4126 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4128 /* If -Bsymbolic was used (which means to bind references to global
4129 symbols to the definition within the shared object), and this
4130 symbol was defined in a regular object, then it actually doesn't
4131 need a PLT entry, and we can accomplish that by forcing it local.
4132 Likewise, if the symbol has hidden or internal visibility.
4133 FIXME: It might be that we also do not need a PLT for other
4134 non-hidden visibilities, but we would have to tell that to the
4135 backend specifically; we can't just clear PLT-related data here. */
4136 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
4137 && eif
->info
->shared
4138 && is_elf_hash_table (eif
->info
)
4139 && (eif
->info
->symbolic
4140 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
4141 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
4142 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
4144 struct elf_backend_data
*bed
;
4145 bfd_boolean force_local
;
4147 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
4149 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
4150 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
4151 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
4154 /* If this is a weak defined symbol in a dynamic object, and we know
4155 the real definition in the dynamic object, copy interesting flags
4156 over to the real definition. */
4157 if (h
->weakdef
!= NULL
)
4159 struct elf_link_hash_entry
*weakdef
;
4161 weakdef
= h
->weakdef
;
4162 if (h
->root
.type
== bfd_link_hash_indirect
)
4163 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4165 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4166 || h
->root
.type
== bfd_link_hash_defweak
);
4167 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
4168 || weakdef
->root
.type
== bfd_link_hash_defweak
);
4169 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
4171 /* If the real definition is defined by a regular object file,
4172 don't do anything special. See the longer description in
4173 elf_adjust_dynamic_symbol, below. */
4174 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
4178 struct elf_backend_data
*bed
;
4180 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
4181 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
4188 /* Make the backend pick a good value for a dynamic symbol. This is
4189 called via elf_link_hash_traverse, and also calls itself
4193 elf_adjust_dynamic_symbol (h
, data
)
4194 struct elf_link_hash_entry
*h
;
4197 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
4199 struct elf_backend_data
*bed
;
4201 if (! is_elf_hash_table (eif
->info
))
4204 if (h
->root
.type
== bfd_link_hash_warning
)
4206 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
4207 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
4209 /* When warning symbols are created, they **replace** the "real"
4210 entry in the hash table, thus we never get to see the real
4211 symbol in a hash traversal. So look at it now. */
4212 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4215 /* Ignore indirect symbols. These are added by the versioning code. */
4216 if (h
->root
.type
== bfd_link_hash_indirect
)
4219 /* Fix the symbol flags. */
4220 if (! elf_fix_symbol_flags (h
, eif
))
4223 /* If this symbol does not require a PLT entry, and it is not
4224 defined by a dynamic object, or is not referenced by a regular
4225 object, ignore it. We do have to handle a weak defined symbol,
4226 even if no regular object refers to it, if we decided to add it
4227 to the dynamic symbol table. FIXME: Do we normally need to worry
4228 about symbols which are defined by one dynamic object and
4229 referenced by another one? */
4230 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
4231 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4232 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4233 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
4234 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
4236 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
4240 /* If we've already adjusted this symbol, don't do it again. This
4241 can happen via a recursive call. */
4242 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
4245 /* Don't look at this symbol again. Note that we must set this
4246 after checking the above conditions, because we may look at a
4247 symbol once, decide not to do anything, and then get called
4248 recursively later after REF_REGULAR is set below. */
4249 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
4251 /* If this is a weak definition, and we know a real definition, and
4252 the real symbol is not itself defined by a regular object file,
4253 then get a good value for the real definition. We handle the
4254 real symbol first, for the convenience of the backend routine.
4256 Note that there is a confusing case here. If the real definition
4257 is defined by a regular object file, we don't get the real symbol
4258 from the dynamic object, but we do get the weak symbol. If the
4259 processor backend uses a COPY reloc, then if some routine in the
4260 dynamic object changes the real symbol, we will not see that
4261 change in the corresponding weak symbol. This is the way other
4262 ELF linkers work as well, and seems to be a result of the shared
4265 I will clarify this issue. Most SVR4 shared libraries define the
4266 variable _timezone and define timezone as a weak synonym. The
4267 tzset call changes _timezone. If you write
4268 extern int timezone;
4270 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4271 you might expect that, since timezone is a synonym for _timezone,
4272 the same number will print both times. However, if the processor
4273 backend uses a COPY reloc, then actually timezone will be copied
4274 into your process image, and, since you define _timezone
4275 yourself, _timezone will not. Thus timezone and _timezone will
4276 wind up at different memory locations. The tzset call will set
4277 _timezone, leaving timezone unchanged. */
4279 if (h
->weakdef
!= NULL
)
4281 /* If we get to this point, we know there is an implicit
4282 reference by a regular object file via the weak symbol H.
4283 FIXME: Is this really true? What if the traversal finds
4284 H->WEAKDEF before it finds H? */
4285 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
4287 if (! elf_adjust_dynamic_symbol (h
->weakdef
, (PTR
) eif
))
4291 /* If a symbol has no type and no size and does not require a PLT
4292 entry, then we are probably about to do the wrong thing here: we
4293 are probably going to create a COPY reloc for an empty object.
4294 This case can arise when a shared object is built with assembly
4295 code, and the assembly code fails to set the symbol type. */
4297 && h
->type
== STT_NOTYPE
4298 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
4299 (*_bfd_error_handler
)
4300 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4301 h
->root
.root
.string
);
4303 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
4304 bed
= get_elf_backend_data (dynobj
);
4305 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
4314 /* This routine is used to export all defined symbols into the dynamic
4315 symbol table. It is called via elf_link_hash_traverse. */
4318 elf_export_symbol (h
, data
)
4319 struct elf_link_hash_entry
*h
;
4322 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
4324 /* Ignore indirect symbols. These are added by the versioning code. */
4325 if (h
->root
.type
== bfd_link_hash_indirect
)
4328 if (h
->root
.type
== bfd_link_hash_warning
)
4329 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4331 if (h
->dynindx
== -1
4332 && (h
->elf_link_hash_flags
4333 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
4335 struct bfd_elf_version_tree
*t
;
4336 struct bfd_elf_version_expr
*d
;
4338 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
4340 if (t
->globals
!= NULL
)
4342 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4344 if ((*d
->match
) (d
, h
->root
.root
.string
))
4349 if (t
->locals
!= NULL
)
4351 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4353 if ((*d
->match
) (d
, h
->root
.root
.string
))
4362 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
4373 /* Look through the symbols which are defined in other shared
4374 libraries and referenced here. Update the list of version
4375 dependencies. This will be put into the .gnu.version_r section.
4376 This function is called via elf_link_hash_traverse. */
4379 elf_link_find_version_dependencies (h
, data
)
4380 struct elf_link_hash_entry
*h
;
4383 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
4384 Elf_Internal_Verneed
*t
;
4385 Elf_Internal_Vernaux
*a
;
4388 if (h
->root
.type
== bfd_link_hash_warning
)
4389 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4391 /* We only care about symbols defined in shared objects with version
4393 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4394 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4396 || h
->verinfo
.verdef
== NULL
)
4399 /* See if we already know about this version. */
4400 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
4402 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
4405 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4406 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
4412 /* This is a new version. Add it to tree we are building. */
4417 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4420 rinfo
->failed
= TRUE
;
4424 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
4425 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
4426 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
4430 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4432 /* Note that we are copying a string pointer here, and testing it
4433 above. If bfd_elf_string_from_elf_section is ever changed to
4434 discard the string data when low in memory, this will have to be
4436 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
4438 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
4439 a
->vna_nextptr
= t
->vn_auxptr
;
4441 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
4444 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4451 /* Figure out appropriate versions for all the symbols. We may not
4452 have the version number script until we have read all of the input
4453 files, so until that point we don't know which symbols should be
4454 local. This function is called via elf_link_hash_traverse. */
4457 elf_link_assign_sym_version (h
, data
)
4458 struct elf_link_hash_entry
*h
;
4461 struct elf_assign_sym_version_info
*sinfo
;
4462 struct bfd_link_info
*info
;
4463 struct elf_backend_data
*bed
;
4464 struct elf_info_failed eif
;
4468 sinfo
= (struct elf_assign_sym_version_info
*) data
;
4471 if (h
->root
.type
== bfd_link_hash_warning
)
4472 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4474 /* Fix the symbol flags. */
4477 if (! elf_fix_symbol_flags (h
, &eif
))
4480 sinfo
->failed
= TRUE
;
4484 /* We only need version numbers for symbols defined in regular
4486 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4489 bed
= get_elf_backend_data (sinfo
->output_bfd
);
4490 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4491 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
4493 struct bfd_elf_version_tree
*t
;
4498 /* There are two consecutive ELF_VER_CHR characters if this is
4499 not a hidden symbol. */
4501 if (*p
== ELF_VER_CHR
)
4507 /* If there is no version string, we can just return out. */
4511 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4515 /* Look for the version. If we find it, it is no longer weak. */
4516 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4518 if (strcmp (t
->name
, p
) == 0)
4522 struct bfd_elf_version_expr
*d
;
4524 len
= p
- h
->root
.root
.string
;
4525 alc
= bfd_malloc ((bfd_size_type
) len
);
4528 memcpy (alc
, h
->root
.root
.string
, len
- 1);
4529 alc
[len
- 1] = '\0';
4530 if (alc
[len
- 2] == ELF_VER_CHR
)
4531 alc
[len
- 2] = '\0';
4533 h
->verinfo
.vertree
= t
;
4537 if (t
->globals
!= NULL
)
4539 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4540 if ((*d
->match
) (d
, alc
))
4544 /* See if there is anything to force this symbol to
4546 if (d
== NULL
&& t
->locals
!= NULL
)
4548 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4550 if ((*d
->match
) (d
, alc
))
4552 if (h
->dynindx
!= -1
4554 && ! info
->export_dynamic
)
4556 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4569 /* If we are building an application, we need to create a
4570 version node for this version. */
4571 if (t
== NULL
&& ! info
->shared
)
4573 struct bfd_elf_version_tree
**pp
;
4576 /* If we aren't going to export this symbol, we don't need
4577 to worry about it. */
4578 if (h
->dynindx
== -1)
4582 t
= ((struct bfd_elf_version_tree
*)
4583 bfd_alloc (sinfo
->output_bfd
, amt
));
4586 sinfo
->failed
= TRUE
;
4595 t
->name_indx
= (unsigned int) -1;
4599 /* Don't count anonymous version tag. */
4600 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
4602 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4604 t
->vernum
= version_index
;
4608 h
->verinfo
.vertree
= t
;
4612 /* We could not find the version for a symbol when
4613 generating a shared archive. Return an error. */
4614 (*_bfd_error_handler
)
4615 (_("%s: undefined versioned symbol name %s"),
4616 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
4617 bfd_set_error (bfd_error_bad_value
);
4618 sinfo
->failed
= TRUE
;
4623 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4626 /* If we don't have a version for this symbol, see if we can find
4628 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
4630 struct bfd_elf_version_tree
*t
;
4631 struct bfd_elf_version_tree
*local_ver
;
4632 struct bfd_elf_version_expr
*d
;
4634 /* See if can find what version this symbol is in. If the
4635 symbol is supposed to be local, then don't actually register
4638 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4640 if (t
->globals
!= NULL
)
4642 bfd_boolean matched
;
4645 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4647 if ((*d
->match
) (d
, h
->root
.root
.string
))
4653 /* There is a version without definition. Make
4654 the symbol the default definition for this
4656 h
->verinfo
.vertree
= t
;
4667 /* There is no undefined version for this symbol. Hide the
4669 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4672 if (t
->locals
!= NULL
)
4674 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4676 /* If the match is "*", keep looking for a more
4677 explicit, perhaps even global, match. */
4678 if (d
->pattern
[0] == '*' && d
->pattern
[1] == '\0')
4680 else if ((*d
->match
) (d
, h
->root
.root
.string
))
4692 if (local_ver
!= NULL
)
4694 h
->verinfo
.vertree
= local_ver
;
4695 if (h
->dynindx
!= -1
4697 && ! info
->export_dynamic
)
4699 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4707 /* Final phase of ELF linker. */
4709 /* A structure we use to avoid passing large numbers of arguments. */
4711 struct elf_final_link_info
4713 /* General link information. */
4714 struct bfd_link_info
*info
;
4717 /* Symbol string table. */
4718 struct bfd_strtab_hash
*symstrtab
;
4719 /* .dynsym section. */
4720 asection
*dynsym_sec
;
4721 /* .hash section. */
4723 /* symbol version section (.gnu.version). */
4724 asection
*symver_sec
;
4725 /* first SHF_TLS section (if any). */
4726 asection
*first_tls_sec
;
4727 /* Buffer large enough to hold contents of any section. */
4729 /* Buffer large enough to hold external relocs of any section. */
4730 PTR external_relocs
;
4731 /* Buffer large enough to hold internal relocs of any section. */
4732 Elf_Internal_Rela
*internal_relocs
;
4733 /* Buffer large enough to hold external local symbols of any input
4735 Elf_External_Sym
*external_syms
;
4736 /* And a buffer for symbol section indices. */
4737 Elf_External_Sym_Shndx
*locsym_shndx
;
4738 /* Buffer large enough to hold internal local symbols of any input
4740 Elf_Internal_Sym
*internal_syms
;
4741 /* Array large enough to hold a symbol index for each local symbol
4742 of any input BFD. */
4744 /* Array large enough to hold a section pointer for each local
4745 symbol of any input BFD. */
4746 asection
**sections
;
4747 /* Buffer to hold swapped out symbols. */
4748 Elf_External_Sym
*symbuf
;
4749 /* And one for symbol section indices. */
4750 Elf_External_Sym_Shndx
*symshndxbuf
;
4751 /* Number of swapped out symbols in buffer. */
4752 size_t symbuf_count
;
4753 /* Number of symbols which fit in symbuf. */
4755 /* And same for symshndxbuf. */
4756 size_t shndxbuf_size
;
4759 static bfd_boolean elf_link_output_sym
4760 PARAMS ((struct elf_final_link_info
*, const char *,
4761 Elf_Internal_Sym
*, asection
*));
4762 static bfd_boolean elf_link_flush_output_syms
4763 PARAMS ((struct elf_final_link_info
*));
4764 static bfd_boolean elf_link_output_extsym
4765 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4766 static bfd_boolean elf_link_sec_merge_syms
4767 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4768 static bfd_boolean elf_link_check_versioned_symbol
4769 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
4770 static bfd_boolean elf_link_input_bfd
4771 PARAMS ((struct elf_final_link_info
*, bfd
*));
4772 static bfd_boolean elf_reloc_link_order
4773 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
4774 struct bfd_link_order
*));
4776 /* This struct is used to pass information to elf_link_output_extsym. */
4778 struct elf_outext_info
4781 bfd_boolean localsyms
;
4782 struct elf_final_link_info
*finfo
;
4785 /* Compute the size of, and allocate space for, REL_HDR which is the
4786 section header for a section containing relocations for O. */
4789 elf_link_size_reloc_section (abfd
, rel_hdr
, o
)
4791 Elf_Internal_Shdr
*rel_hdr
;
4794 bfd_size_type reloc_count
;
4795 bfd_size_type num_rel_hashes
;
4797 /* Figure out how many relocations there will be. */
4798 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
4799 reloc_count
= elf_section_data (o
)->rel_count
;
4801 reloc_count
= elf_section_data (o
)->rel_count2
;
4803 num_rel_hashes
= o
->reloc_count
;
4804 if (num_rel_hashes
< reloc_count
)
4805 num_rel_hashes
= reloc_count
;
4807 /* That allows us to calculate the size of the section. */
4808 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
4810 /* The contents field must last into write_object_contents, so we
4811 allocate it with bfd_alloc rather than malloc. Also since we
4812 cannot be sure that the contents will actually be filled in,
4813 we zero the allocated space. */
4814 rel_hdr
->contents
= (PTR
) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
4815 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
4818 /* We only allocate one set of hash entries, so we only do it the
4819 first time we are called. */
4820 if (elf_section_data (o
)->rel_hashes
== NULL
4823 struct elf_link_hash_entry
**p
;
4825 p
= ((struct elf_link_hash_entry
**)
4826 bfd_zmalloc (num_rel_hashes
4827 * sizeof (struct elf_link_hash_entry
*)));
4831 elf_section_data (o
)->rel_hashes
= p
;
4837 /* When performing a relocateable link, the input relocations are
4838 preserved. But, if they reference global symbols, the indices
4839 referenced must be updated. Update all the relocations in
4840 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4843 elf_link_adjust_relocs (abfd
, rel_hdr
, count
, rel_hash
)
4845 Elf_Internal_Shdr
*rel_hdr
;
4847 struct elf_link_hash_entry
**rel_hash
;
4850 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4852 void (*swap_in
) PARAMS ((bfd
*, const bfd_byte
*, Elf_Internal_Rela
*));
4853 void (*swap_out
) PARAMS ((bfd
*, const Elf_Internal_Rela
*, bfd_byte
*));
4855 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4857 swap_in
= bed
->s
->swap_reloc_in
;
4858 swap_out
= bed
->s
->swap_reloc_out
;
4860 else if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
))
4862 swap_in
= bed
->s
->swap_reloca_in
;
4863 swap_out
= bed
->s
->swap_reloca_out
;
4868 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
4871 erela
= rel_hdr
->contents
;
4872 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
4874 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
4877 if (*rel_hash
== NULL
)
4880 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
4882 (*swap_in
) (abfd
, erela
, irela
);
4883 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4884 irela
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4885 ELF_R_TYPE (irela
[j
].r_info
));
4886 (*swap_out
) (abfd
, irela
, erela
);
4890 struct elf_link_sort_rela
4893 enum elf_reloc_type_class type
;
4894 /* We use this as an array of size int_rels_per_ext_rel. */
4895 Elf_Internal_Rela rela
[1];
4899 elf_link_sort_cmp1 (A
, B
)
4903 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4904 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4905 int relativea
, relativeb
;
4907 relativea
= a
->type
== reloc_class_relative
;
4908 relativeb
= b
->type
== reloc_class_relative
;
4910 if (relativea
< relativeb
)
4912 if (relativea
> relativeb
)
4914 if (ELF_R_SYM (a
->rela
->r_info
) < ELF_R_SYM (b
->rela
->r_info
))
4916 if (ELF_R_SYM (a
->rela
->r_info
) > ELF_R_SYM (b
->rela
->r_info
))
4918 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
4920 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
4926 elf_link_sort_cmp2 (A
, B
)
4930 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4931 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4934 if (a
->offset
< b
->offset
)
4936 if (a
->offset
> b
->offset
)
4938 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
4939 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
4944 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
4946 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
4952 elf_link_sort_relocs (abfd
, info
, psec
)
4954 struct bfd_link_info
*info
;
4957 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4958 asection
*reldyn
, *o
;
4959 bfd_size_type count
, size
;
4960 size_t i
, ret
, sort_elt
, ext_size
;
4961 bfd_byte
*sort
, *s_non_relative
, *p
;
4962 struct elf_link_sort_rela
*sq
;
4963 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4964 int i2e
= bed
->s
->int_rels_per_ext_rel
;
4965 void (*swap_in
) PARAMS ((bfd
*, const bfd_byte
*, Elf_Internal_Rela
*));
4966 void (*swap_out
) PARAMS ((bfd
*, const Elf_Internal_Rela
*, bfd_byte
*));
4968 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
4969 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4971 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
4972 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4974 ext_size
= sizeof (Elf_External_Rel
);
4975 swap_in
= bed
->s
->swap_reloc_in
;
4976 swap_out
= bed
->s
->swap_reloc_out
;
4980 ext_size
= sizeof (Elf_External_Rela
);
4981 swap_in
= bed
->s
->swap_reloca_in
;
4982 swap_out
= bed
->s
->swap_reloca_out
;
4984 count
= reldyn
->_raw_size
/ ext_size
;
4987 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4988 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4989 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4990 && o
->output_section
== reldyn
)
4991 size
+= o
->_raw_size
;
4993 if (size
!= reldyn
->_raw_size
)
4996 sort_elt
= (sizeof (struct elf_link_sort_rela
)
4997 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
4998 sort
= bfd_zmalloc (sort_elt
* count
);
5001 (*info
->callbacks
->warning
)
5002 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0,
5007 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
5008 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
5009 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
5010 && o
->output_section
== reldyn
)
5012 bfd_byte
*erel
, *erelend
;
5015 erelend
= o
->contents
+ o
->_raw_size
;
5016 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5017 while (erel
< erelend
)
5019 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5020 (*swap_in
) (abfd
, erel
, s
->rela
);
5021 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5027 qsort (sort
, (size_t) count
, sort_elt
, elf_link_sort_cmp1
);
5029 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5031 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5032 if (s
->type
!= reloc_class_relative
)
5038 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5039 for (; i
< count
; i
++, p
+= sort_elt
)
5041 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5042 if (ELF_R_SYM (sp
->rela
->r_info
) != ELF_R_SYM (sq
->rela
->r_info
))
5044 sp
->offset
= sq
->rela
->r_offset
;
5047 qsort (s_non_relative
, (size_t) count
- ret
, sort_elt
, elf_link_sort_cmp2
);
5049 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
5050 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
5051 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
5052 && o
->output_section
== reldyn
)
5054 bfd_byte
*erel
, *erelend
;
5057 erelend
= o
->contents
+ o
->_raw_size
;
5058 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5059 while (erel
< erelend
)
5061 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5062 (*swap_out
) (abfd
, s
->rela
, erel
);
5073 /* Do the final step of an ELF link. */
5076 elf_bfd_final_link (abfd
, info
)
5078 struct bfd_link_info
*info
;
5080 bfd_boolean dynamic
;
5081 bfd_boolean emit_relocs
;
5083 struct elf_final_link_info finfo
;
5084 register asection
*o
;
5085 register struct bfd_link_order
*p
;
5087 bfd_size_type max_contents_size
;
5088 bfd_size_type max_external_reloc_size
;
5089 bfd_size_type max_internal_reloc_count
;
5090 bfd_size_type max_sym_count
;
5091 bfd_size_type max_sym_shndx_count
;
5093 Elf_Internal_Sym elfsym
;
5095 Elf_Internal_Shdr
*symtab_hdr
;
5096 Elf_Internal_Shdr
*symtab_shndx_hdr
;
5097 Elf_Internal_Shdr
*symstrtab_hdr
;
5098 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5099 struct elf_outext_info eoinfo
;
5101 size_t relativecount
= 0;
5102 asection
*reldyn
= 0;
5105 if (! is_elf_hash_table (info
))
5109 abfd
->flags
|= DYNAMIC
;
5111 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
5112 dynobj
= elf_hash_table (info
)->dynobj
;
5114 emit_relocs
= (info
->relocateable
5115 || info
->emitrelocations
5116 || bed
->elf_backend_emit_relocs
);
5119 finfo
.output_bfd
= abfd
;
5120 finfo
.symstrtab
= elf_stringtab_init ();
5121 if (finfo
.symstrtab
== NULL
)
5126 finfo
.dynsym_sec
= NULL
;
5127 finfo
.hash_sec
= NULL
;
5128 finfo
.symver_sec
= NULL
;
5132 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
5133 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
5134 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
5135 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5136 /* Note that it is OK if symver_sec is NULL. */
5139 finfo
.contents
= NULL
;
5140 finfo
.external_relocs
= NULL
;
5141 finfo
.internal_relocs
= NULL
;
5142 finfo
.external_syms
= NULL
;
5143 finfo
.locsym_shndx
= NULL
;
5144 finfo
.internal_syms
= NULL
;
5145 finfo
.indices
= NULL
;
5146 finfo
.sections
= NULL
;
5147 finfo
.symbuf
= NULL
;
5148 finfo
.symshndxbuf
= NULL
;
5149 finfo
.symbuf_count
= 0;
5150 finfo
.shndxbuf_size
= 0;
5151 finfo
.first_tls_sec
= NULL
;
5152 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
5153 if ((o
->flags
& SEC_THREAD_LOCAL
) != 0
5154 && (o
->flags
& SEC_LOAD
) != 0)
5156 finfo
.first_tls_sec
= o
;
5160 /* Count up the number of relocations we will output for each output
5161 section, so that we know the sizes of the reloc sections. We
5162 also figure out some maximum sizes. */
5163 max_contents_size
= 0;
5164 max_external_reloc_size
= 0;
5165 max_internal_reloc_count
= 0;
5167 max_sym_shndx_count
= 0;
5169 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
5171 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
5174 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5176 unsigned int reloc_count
= 0;
5177 struct bfd_elf_section_data
*esdi
= NULL
;
5178 unsigned int *rel_count1
;
5180 if (p
->type
== bfd_section_reloc_link_order
5181 || p
->type
== bfd_symbol_reloc_link_order
)
5183 else if (p
->type
== bfd_indirect_link_order
)
5187 sec
= p
->u
.indirect
.section
;
5188 esdi
= elf_section_data (sec
);
5190 /* Mark all sections which are to be included in the
5191 link. This will normally be every section. We need
5192 to do this so that we can identify any sections which
5193 the linker has decided to not include. */
5194 sec
->linker_mark
= TRUE
;
5196 if (sec
->flags
& SEC_MERGE
)
5199 if (info
->relocateable
|| info
->emitrelocations
)
5200 reloc_count
= sec
->reloc_count
;
5201 else if (bed
->elf_backend_count_relocs
)
5203 Elf_Internal_Rela
* relocs
;
5205 relocs
= (NAME(_bfd_elf
,link_read_relocs
)
5206 (abfd
, sec
, (PTR
) NULL
,
5207 (Elf_Internal_Rela
*) NULL
, info
->keep_memory
));
5209 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
5211 if (elf_section_data (o
)->relocs
!= relocs
)
5215 if (sec
->_raw_size
> max_contents_size
)
5216 max_contents_size
= sec
->_raw_size
;
5217 if (sec
->_cooked_size
> max_contents_size
)
5218 max_contents_size
= sec
->_cooked_size
;
5220 /* We are interested in just local symbols, not all
5222 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
5223 && (sec
->owner
->flags
& DYNAMIC
) == 0)
5227 if (elf_bad_symtab (sec
->owner
))
5228 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
5229 / sizeof (Elf_External_Sym
));
5231 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
5233 if (sym_count
> max_sym_count
)
5234 max_sym_count
= sym_count
;
5236 if (sym_count
> max_sym_shndx_count
5237 && elf_symtab_shndx (sec
->owner
) != 0)
5238 max_sym_shndx_count
= sym_count
;
5240 if ((sec
->flags
& SEC_RELOC
) != 0)
5244 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
5245 if (ext_size
> max_external_reloc_size
)
5246 max_external_reloc_size
= ext_size
;
5247 if (sec
->reloc_count
> max_internal_reloc_count
)
5248 max_internal_reloc_count
= sec
->reloc_count
;
5253 if (reloc_count
== 0)
5256 o
->reloc_count
+= reloc_count
;
5258 /* MIPS may have a mix of REL and RELA relocs on sections.
5259 To support this curious ABI we keep reloc counts in
5260 elf_section_data too. We must be careful to add the
5261 relocations from the input section to the right output
5262 count. FIXME: Get rid of one count. We have
5263 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
5264 rel_count1
= &esdo
->rel_count
;
5267 bfd_boolean same_size
;
5268 bfd_size_type entsize1
;
5270 entsize1
= esdi
->rel_hdr
.sh_entsize
;
5271 BFD_ASSERT (entsize1
== sizeof (Elf_External_Rel
)
5272 || entsize1
== sizeof (Elf_External_Rela
));
5273 same_size
= (!o
->use_rela_p
5274 == (entsize1
== sizeof (Elf_External_Rel
)));
5277 rel_count1
= &esdo
->rel_count2
;
5279 if (esdi
->rel_hdr2
!= NULL
)
5281 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
5282 unsigned int alt_count
;
5283 unsigned int *rel_count2
;
5285 BFD_ASSERT (entsize2
!= entsize1
5286 && (entsize2
== sizeof (Elf_External_Rel
)
5287 || entsize2
== sizeof (Elf_External_Rela
)));
5289 rel_count2
= &esdo
->rel_count2
;
5291 rel_count2
= &esdo
->rel_count
;
5293 /* The following is probably too simplistic if the
5294 backend counts output relocs unusually. */
5295 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
5296 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
5297 *rel_count2
+= alt_count
;
5298 reloc_count
-= alt_count
;
5301 *rel_count1
+= reloc_count
;
5304 if (o
->reloc_count
> 0)
5305 o
->flags
|= SEC_RELOC
;
5308 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5309 set it (this is probably a bug) and if it is set
5310 assign_section_numbers will create a reloc section. */
5311 o
->flags
&=~ SEC_RELOC
;
5314 /* If the SEC_ALLOC flag is not set, force the section VMA to
5315 zero. This is done in elf_fake_sections as well, but forcing
5316 the VMA to 0 here will ensure that relocs against these
5317 sections are handled correctly. */
5318 if ((o
->flags
& SEC_ALLOC
) == 0
5319 && ! o
->user_set_vma
)
5323 if (! info
->relocateable
&& merged
)
5324 elf_link_hash_traverse (elf_hash_table (info
),
5325 elf_link_sec_merge_syms
, (PTR
) abfd
);
5327 /* Figure out the file positions for everything but the symbol table
5328 and the relocs. We set symcount to force assign_section_numbers
5329 to create a symbol table. */
5330 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
5331 BFD_ASSERT (! abfd
->output_has_begun
);
5332 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
5335 /* That created the reloc sections. Set their sizes, and assign
5336 them file positions, and allocate some buffers. */
5337 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5339 if ((o
->flags
& SEC_RELOC
) != 0)
5341 if (!elf_link_size_reloc_section (abfd
,
5342 &elf_section_data (o
)->rel_hdr
,
5346 if (elf_section_data (o
)->rel_hdr2
5347 && !elf_link_size_reloc_section (abfd
,
5348 elf_section_data (o
)->rel_hdr2
,
5353 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5354 to count upwards while actually outputting the relocations. */
5355 elf_section_data (o
)->rel_count
= 0;
5356 elf_section_data (o
)->rel_count2
= 0;
5359 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5361 /* We have now assigned file positions for all the sections except
5362 .symtab and .strtab. We start the .symtab section at the current
5363 file position, and write directly to it. We build the .strtab
5364 section in memory. */
5365 bfd_get_symcount (abfd
) = 0;
5366 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5367 /* sh_name is set in prep_headers. */
5368 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5369 /* sh_flags, sh_addr and sh_size all start off zero. */
5370 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
5371 /* sh_link is set in assign_section_numbers. */
5372 /* sh_info is set below. */
5373 /* sh_offset is set just below. */
5374 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
5376 off
= elf_tdata (abfd
)->next_file_pos
;
5377 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
5379 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5380 incorrect. We do not yet know the size of the .symtab section.
5381 We correct next_file_pos below, after we do know the size. */
5383 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5384 continuously seeking to the right position in the file. */
5385 if (! info
->keep_memory
|| max_sym_count
< 20)
5386 finfo
.symbuf_size
= 20;
5388 finfo
.symbuf_size
= max_sym_count
;
5389 amt
= finfo
.symbuf_size
;
5390 amt
*= sizeof (Elf_External_Sym
);
5391 finfo
.symbuf
= (Elf_External_Sym
*) bfd_malloc (amt
);
5392 if (finfo
.symbuf
== NULL
)
5394 if (elf_numsections (abfd
) > SHN_LORESERVE
)
5396 /* Wild guess at number of output symbols. realloc'd as needed. */
5397 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
5398 finfo
.shndxbuf_size
= amt
;
5399 amt
*= sizeof (Elf_External_Sym_Shndx
);
5400 finfo
.symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
5401 if (finfo
.symshndxbuf
== NULL
)
5405 /* Start writing out the symbol table. The first symbol is always a
5407 if (info
->strip
!= strip_all
5410 elfsym
.st_value
= 0;
5413 elfsym
.st_other
= 0;
5414 elfsym
.st_shndx
= SHN_UNDEF
;
5415 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5416 &elfsym
, bfd_und_section_ptr
))
5421 /* Some standard ELF linkers do this, but we don't because it causes
5422 bootstrap comparison failures. */
5423 /* Output a file symbol for the output file as the second symbol.
5424 We output this even if we are discarding local symbols, although
5425 I'm not sure if this is correct. */
5426 elfsym
.st_value
= 0;
5428 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5429 elfsym
.st_other
= 0;
5430 elfsym
.st_shndx
= SHN_ABS
;
5431 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
5432 &elfsym
, bfd_abs_section_ptr
))
5436 /* Output a symbol for each section. We output these even if we are
5437 discarding local symbols, since they are used for relocs. These
5438 symbols have no names. We store the index of each one in the
5439 index field of the section, so that we can find it again when
5440 outputting relocs. */
5441 if (info
->strip
!= strip_all
5445 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5446 elfsym
.st_other
= 0;
5447 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5449 o
= section_from_elf_index (abfd
, i
);
5451 o
->target_index
= bfd_get_symcount (abfd
);
5452 elfsym
.st_shndx
= i
;
5453 if (info
->relocateable
|| o
== NULL
)
5454 elfsym
.st_value
= 0;
5456 elfsym
.st_value
= o
->vma
;
5457 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5460 if (i
== SHN_LORESERVE
- 1)
5461 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
5465 /* Allocate some memory to hold information read in from the input
5467 if (max_contents_size
!= 0)
5469 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
5470 if (finfo
.contents
== NULL
)
5474 if (max_external_reloc_size
!= 0)
5476 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
5477 if (finfo
.external_relocs
== NULL
)
5481 if (max_internal_reloc_count
!= 0)
5483 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5484 amt
*= sizeof (Elf_Internal_Rela
);
5485 finfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
5486 if (finfo
.internal_relocs
== NULL
)
5490 if (max_sym_count
!= 0)
5492 amt
= max_sym_count
* sizeof (Elf_External_Sym
);
5493 finfo
.external_syms
= (Elf_External_Sym
*) bfd_malloc (amt
);
5494 if (finfo
.external_syms
== NULL
)
5497 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
5498 finfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
5499 if (finfo
.internal_syms
== NULL
)
5502 amt
= max_sym_count
* sizeof (long);
5503 finfo
.indices
= (long *) bfd_malloc (amt
);
5504 if (finfo
.indices
== NULL
)
5507 amt
= max_sym_count
* sizeof (asection
*);
5508 finfo
.sections
= (asection
**) bfd_malloc (amt
);
5509 if (finfo
.sections
== NULL
)
5513 if (max_sym_shndx_count
!= 0)
5515 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
5516 finfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
5517 if (finfo
.locsym_shndx
== NULL
)
5521 if (finfo
.first_tls_sec
)
5523 unsigned int align
= 0;
5524 bfd_vma base
= finfo
.first_tls_sec
->vma
, end
= 0;
5527 for (sec
= finfo
.first_tls_sec
;
5528 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
5531 bfd_vma size
= sec
->_raw_size
;
5533 if (bfd_get_section_alignment (abfd
, sec
) > align
)
5534 align
= bfd_get_section_alignment (abfd
, sec
);
5535 if (sec
->_raw_size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
5537 struct bfd_link_order
*o
;
5540 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
5541 if (size
< o
->offset
+ o
->size
)
5542 size
= o
->offset
+ o
->size
;
5544 end
= sec
->vma
+ size
;
5546 elf_hash_table (info
)->tls_segment
5547 = bfd_zalloc (abfd
, sizeof (struct elf_link_tls_segment
));
5548 if (elf_hash_table (info
)->tls_segment
== NULL
)
5550 elf_hash_table (info
)->tls_segment
->start
= base
;
5551 elf_hash_table (info
)->tls_segment
->size
= end
- base
;
5552 elf_hash_table (info
)->tls_segment
->align
= align
;
5555 /* Since ELF permits relocations to be against local symbols, we
5556 must have the local symbols available when we do the relocations.
5557 Since we would rather only read the local symbols once, and we
5558 would rather not keep them in memory, we handle all the
5559 relocations for a single input file at the same time.
5561 Unfortunately, there is no way to know the total number of local
5562 symbols until we have seen all of them, and the local symbol
5563 indices precede the global symbol indices. This means that when
5564 we are generating relocateable output, and we see a reloc against
5565 a global symbol, we can not know the symbol index until we have
5566 finished examining all the local symbols to see which ones we are
5567 going to output. To deal with this, we keep the relocations in
5568 memory, and don't output them until the end of the link. This is
5569 an unfortunate waste of memory, but I don't see a good way around
5570 it. Fortunately, it only happens when performing a relocateable
5571 link, which is not the common case. FIXME: If keep_memory is set
5572 we could write the relocs out and then read them again; I don't
5573 know how bad the memory loss will be. */
5575 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5576 sub
->output_has_begun
= FALSE
;
5577 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5579 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5581 if (p
->type
== bfd_indirect_link_order
5582 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
5583 == bfd_target_elf_flavour
)
5584 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
5586 if (! sub
->output_has_begun
)
5588 if (! elf_link_input_bfd (&finfo
, sub
))
5590 sub
->output_has_begun
= TRUE
;
5593 else if (p
->type
== bfd_section_reloc_link_order
5594 || p
->type
== bfd_symbol_reloc_link_order
)
5596 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
5601 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
5607 /* Output any global symbols that got converted to local in a
5608 version script or due to symbol visibility. We do this in a
5609 separate step since ELF requires all local symbols to appear
5610 prior to any global symbols. FIXME: We should only do this if
5611 some global symbols were, in fact, converted to become local.
5612 FIXME: Will this work correctly with the Irix 5 linker? */
5613 eoinfo
.failed
= FALSE
;
5614 eoinfo
.finfo
= &finfo
;
5615 eoinfo
.localsyms
= TRUE
;
5616 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5621 /* That wrote out all the local symbols. Finish up the symbol table
5622 with the global symbols. Even if we want to strip everything we
5623 can, we still need to deal with those global symbols that got
5624 converted to local in a version script. */
5626 /* The sh_info field records the index of the first non local symbol. */
5627 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
5630 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
5632 Elf_Internal_Sym sym
;
5633 Elf_External_Sym
*dynsym
=
5634 (Elf_External_Sym
*) finfo
.dynsym_sec
->contents
;
5635 long last_local
= 0;
5637 /* Write out the section symbols for the output sections. */
5644 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5647 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5650 Elf_External_Sym
*dest
;
5652 indx
= elf_section_data (s
)->this_idx
;
5653 BFD_ASSERT (indx
> 0);
5654 sym
.st_shndx
= indx
;
5655 sym
.st_value
= s
->vma
;
5656 dest
= dynsym
+ elf_section_data (s
)->dynindx
;
5657 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5660 last_local
= bfd_count_sections (abfd
);
5663 /* Write out the local dynsyms. */
5664 if (elf_hash_table (info
)->dynlocal
)
5666 struct elf_link_local_dynamic_entry
*e
;
5667 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
5670 Elf_External_Sym
*dest
;
5672 sym
.st_size
= e
->isym
.st_size
;
5673 sym
.st_other
= e
->isym
.st_other
;
5675 /* Copy the internal symbol as is.
5676 Note that we saved a word of storage and overwrote
5677 the original st_name with the dynstr_index. */
5680 if (e
->isym
.st_shndx
!= SHN_UNDEF
5681 && (e
->isym
.st_shndx
< SHN_LORESERVE
5682 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
5684 s
= bfd_section_from_elf_index (e
->input_bfd
,
5688 elf_section_data (s
->output_section
)->this_idx
;
5689 sym
.st_value
= (s
->output_section
->vma
5691 + e
->isym
.st_value
);
5694 if (last_local
< e
->dynindx
)
5695 last_local
= e
->dynindx
;
5697 dest
= dynsym
+ e
->dynindx
;
5698 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5702 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
5706 /* We get the global symbols from the hash table. */
5707 eoinfo
.failed
= FALSE
;
5708 eoinfo
.localsyms
= FALSE
;
5709 eoinfo
.finfo
= &finfo
;
5710 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5715 /* If backend needs to output some symbols not present in the hash
5716 table, do it now. */
5717 if (bed
->elf_backend_output_arch_syms
)
5719 typedef bfd_boolean (*out_sym_func
)
5720 PARAMS ((PTR
, const char *, Elf_Internal_Sym
*, asection
*));
5722 if (! ((*bed
->elf_backend_output_arch_syms
)
5723 (abfd
, info
, (PTR
) &finfo
, (out_sym_func
) elf_link_output_sym
)))
5727 /* Flush all symbols to the file. */
5728 if (! elf_link_flush_output_syms (&finfo
))
5731 /* Now we know the size of the symtab section. */
5732 off
+= symtab_hdr
->sh_size
;
5734 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
5735 if (symtab_shndx_hdr
->sh_name
!= 0)
5737 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
5738 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
5739 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
5740 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
5741 symtab_shndx_hdr
->sh_size
= amt
;
5743 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
5746 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
5747 || (bfd_bwrite ((PTR
) finfo
.symshndxbuf
, amt
, abfd
) != amt
))
5752 /* Finish up and write out the symbol string table (.strtab)
5754 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5755 /* sh_name was set in prep_headers. */
5756 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5757 symstrtab_hdr
->sh_flags
= 0;
5758 symstrtab_hdr
->sh_addr
= 0;
5759 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
5760 symstrtab_hdr
->sh_entsize
= 0;
5761 symstrtab_hdr
->sh_link
= 0;
5762 symstrtab_hdr
->sh_info
= 0;
5763 /* sh_offset is set just below. */
5764 symstrtab_hdr
->sh_addralign
= 1;
5766 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
5767 elf_tdata (abfd
)->next_file_pos
= off
;
5769 if (bfd_get_symcount (abfd
) > 0)
5771 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
5772 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
5776 /* Adjust the relocs to have the correct symbol indices. */
5777 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5779 if ((o
->flags
& SEC_RELOC
) == 0)
5782 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
5783 elf_section_data (o
)->rel_count
,
5784 elf_section_data (o
)->rel_hashes
);
5785 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
5786 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
5787 elf_section_data (o
)->rel_count2
,
5788 (elf_section_data (o
)->rel_hashes
5789 + elf_section_data (o
)->rel_count
));
5791 /* Set the reloc_count field to 0 to prevent write_relocs from
5792 trying to swap the relocs out itself. */
5796 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
5797 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
5799 /* If we are linking against a dynamic object, or generating a
5800 shared library, finish up the dynamic linking information. */
5803 Elf_External_Dyn
*dyncon
, *dynconend
;
5805 /* Fix up .dynamic entries. */
5806 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
5807 BFD_ASSERT (o
!= NULL
);
5809 dyncon
= (Elf_External_Dyn
*) o
->contents
;
5810 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
5811 for (; dyncon
< dynconend
; dyncon
++)
5813 Elf_Internal_Dyn dyn
;
5817 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
5824 if (relativecount
> 0 && dyncon
+ 1 < dynconend
)
5826 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
5828 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
5829 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
5832 if (dyn
.d_tag
!= DT_NULL
)
5834 dyn
.d_un
.d_val
= relativecount
;
5835 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5841 name
= info
->init_function
;
5844 name
= info
->fini_function
;
5847 struct elf_link_hash_entry
*h
;
5849 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
5850 FALSE
, FALSE
, TRUE
);
5852 && (h
->root
.type
== bfd_link_hash_defined
5853 || h
->root
.type
== bfd_link_hash_defweak
))
5855 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
5856 o
= h
->root
.u
.def
.section
;
5857 if (o
->output_section
!= NULL
)
5858 dyn
.d_un
.d_val
+= (o
->output_section
->vma
5859 + o
->output_offset
);
5862 /* The symbol is imported from another shared
5863 library and does not apply to this one. */
5867 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5872 case DT_PREINIT_ARRAYSZ
:
5873 name
= ".preinit_array";
5875 case DT_INIT_ARRAYSZ
:
5876 name
= ".init_array";
5878 case DT_FINI_ARRAYSZ
:
5879 name
= ".fini_array";
5881 o
= bfd_get_section_by_name (abfd
, name
);
5884 (*_bfd_error_handler
)
5885 (_("%s: could not find output section %s"),
5886 bfd_get_filename (abfd
), name
);
5889 if (o
->_raw_size
== 0)
5890 (*_bfd_error_handler
)
5891 (_("warning: %s section has zero size"), name
);
5892 dyn
.d_un
.d_val
= o
->_raw_size
;
5893 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5896 case DT_PREINIT_ARRAY
:
5897 name
= ".preinit_array";
5900 name
= ".init_array";
5903 name
= ".fini_array";
5916 name
= ".gnu.version_d";
5919 name
= ".gnu.version_r";
5922 name
= ".gnu.version";
5924 o
= bfd_get_section_by_name (abfd
, name
);
5927 (*_bfd_error_handler
)
5928 (_("%s: could not find output section %s"),
5929 bfd_get_filename (abfd
), name
);
5932 dyn
.d_un
.d_ptr
= o
->vma
;
5933 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5940 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
5945 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5947 Elf_Internal_Shdr
*hdr
;
5949 hdr
= elf_elfsections (abfd
)[i
];
5950 if (hdr
->sh_type
== type
5951 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
5953 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
5954 dyn
.d_un
.d_val
+= hdr
->sh_size
;
5957 if (dyn
.d_un
.d_val
== 0
5958 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
5959 dyn
.d_un
.d_val
= hdr
->sh_addr
;
5963 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5969 /* If we have created any dynamic sections, then output them. */
5972 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
5975 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
5977 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5978 || o
->_raw_size
== 0
5979 || o
->output_section
== bfd_abs_section_ptr
)
5981 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
5983 /* At this point, we are only interested in sections
5984 created by elf_link_create_dynamic_sections. */
5987 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
5989 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
5991 if (! bfd_set_section_contents (abfd
, o
->output_section
,
5993 (file_ptr
) o
->output_offset
,
5999 /* The contents of the .dynstr section are actually in a
6001 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
6002 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
6003 || ! _bfd_elf_strtab_emit (abfd
,
6004 elf_hash_table (info
)->dynstr
))
6010 if (info
->relocateable
)
6012 bfd_boolean failed
= FALSE
;
6014 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
6019 /* If we have optimized stabs strings, output them. */
6020 if (elf_hash_table (info
)->stab_info
!= NULL
)
6022 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
6026 if (info
->eh_frame_hdr
)
6028 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
6032 if (finfo
.symstrtab
!= NULL
)
6033 _bfd_stringtab_free (finfo
.symstrtab
);
6034 if (finfo
.contents
!= NULL
)
6035 free (finfo
.contents
);
6036 if (finfo
.external_relocs
!= NULL
)
6037 free (finfo
.external_relocs
);
6038 if (finfo
.internal_relocs
!= NULL
)
6039 free (finfo
.internal_relocs
);
6040 if (finfo
.external_syms
!= NULL
)
6041 free (finfo
.external_syms
);
6042 if (finfo
.locsym_shndx
!= NULL
)
6043 free (finfo
.locsym_shndx
);
6044 if (finfo
.internal_syms
!= NULL
)
6045 free (finfo
.internal_syms
);
6046 if (finfo
.indices
!= NULL
)
6047 free (finfo
.indices
);
6048 if (finfo
.sections
!= NULL
)
6049 free (finfo
.sections
);
6050 if (finfo
.symbuf
!= NULL
)
6051 free (finfo
.symbuf
);
6052 if (finfo
.symshndxbuf
!= NULL
)
6053 free (finfo
.symshndxbuf
);
6054 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6056 if ((o
->flags
& SEC_RELOC
) != 0
6057 && elf_section_data (o
)->rel_hashes
!= NULL
)
6058 free (elf_section_data (o
)->rel_hashes
);
6061 elf_tdata (abfd
)->linker
= TRUE
;
6066 if (finfo
.symstrtab
!= NULL
)
6067 _bfd_stringtab_free (finfo
.symstrtab
);
6068 if (finfo
.contents
!= NULL
)
6069 free (finfo
.contents
);
6070 if (finfo
.external_relocs
!= NULL
)
6071 free (finfo
.external_relocs
);
6072 if (finfo
.internal_relocs
!= NULL
)
6073 free (finfo
.internal_relocs
);
6074 if (finfo
.external_syms
!= NULL
)
6075 free (finfo
.external_syms
);
6076 if (finfo
.locsym_shndx
!= NULL
)
6077 free (finfo
.locsym_shndx
);
6078 if (finfo
.internal_syms
!= NULL
)
6079 free (finfo
.internal_syms
);
6080 if (finfo
.indices
!= NULL
)
6081 free (finfo
.indices
);
6082 if (finfo
.sections
!= NULL
)
6083 free (finfo
.sections
);
6084 if (finfo
.symbuf
!= NULL
)
6085 free (finfo
.symbuf
);
6086 if (finfo
.symshndxbuf
!= NULL
)
6087 free (finfo
.symshndxbuf
);
6088 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6090 if ((o
->flags
& SEC_RELOC
) != 0
6091 && elf_section_data (o
)->rel_hashes
!= NULL
)
6092 free (elf_section_data (o
)->rel_hashes
);
6098 /* Add a symbol to the output symbol table. */
6101 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
6102 struct elf_final_link_info
*finfo
;
6104 Elf_Internal_Sym
*elfsym
;
6105 asection
*input_sec
;
6107 Elf_External_Sym
*dest
;
6108 Elf_External_Sym_Shndx
*destshndx
;
6109 bfd_boolean (*output_symbol_hook
)
6110 PARAMS ((bfd
*, struct bfd_link_info
*info
, const char *,
6111 Elf_Internal_Sym
*, asection
*));
6113 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
6114 elf_backend_link_output_symbol_hook
;
6115 if (output_symbol_hook
!= NULL
)
6117 if (! ((*output_symbol_hook
)
6118 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
6122 if (name
== (const char *) NULL
|| *name
== '\0')
6123 elfsym
->st_name
= 0;
6124 else if (input_sec
->flags
& SEC_EXCLUDE
)
6125 elfsym
->st_name
= 0;
6128 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
6130 if (elfsym
->st_name
== (unsigned long) -1)
6134 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
6136 if (! elf_link_flush_output_syms (finfo
))
6140 dest
= finfo
->symbuf
+ finfo
->symbuf_count
;
6141 destshndx
= finfo
->symshndxbuf
;
6142 if (destshndx
!= NULL
)
6144 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
6148 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
6149 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
6150 if (destshndx
== NULL
)
6152 memset ((char *) destshndx
+ amt
, 0, amt
);
6153 finfo
->shndxbuf_size
*= 2;
6155 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
6158 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
, (PTR
) dest
, (PTR
) destshndx
);
6159 finfo
->symbuf_count
+= 1;
6160 bfd_get_symcount (finfo
->output_bfd
) += 1;
6165 /* Flush the output symbols to the file. */
6168 elf_link_flush_output_syms (finfo
)
6169 struct elf_final_link_info
*finfo
;
6171 if (finfo
->symbuf_count
> 0)
6173 Elf_Internal_Shdr
*hdr
;
6177 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6178 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6179 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
6180 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6181 || bfd_bwrite ((PTR
) finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6184 hdr
->sh_size
+= amt
;
6185 finfo
->symbuf_count
= 0;
6191 /* Adjust all external symbols pointing into SEC_MERGE sections
6192 to reflect the object merging within the sections. */
6195 elf_link_sec_merge_syms (h
, data
)
6196 struct elf_link_hash_entry
*h
;
6201 if (h
->root
.type
== bfd_link_hash_warning
)
6202 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6204 if ((h
->root
.type
== bfd_link_hash_defined
6205 || h
->root
.type
== bfd_link_hash_defweak
)
6206 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
6207 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
6209 bfd
*output_bfd
= (bfd
*) data
;
6211 h
->root
.u
.def
.value
=
6212 _bfd_merged_section_offset (output_bfd
,
6213 &h
->root
.u
.def
.section
,
6214 elf_section_data (sec
)->sec_info
,
6215 h
->root
.u
.def
.value
, (bfd_vma
) 0);
6221 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6222 allowing an unsatisfied unversioned symbol in the DSO to match a
6223 versioned symbol that would normally require an explicit version. */
6226 elf_link_check_versioned_symbol (info
, h
)
6227 struct bfd_link_info
*info
;
6228 struct elf_link_hash_entry
*h
;
6230 bfd
*undef_bfd
= h
->root
.u
.undef
.abfd
;
6231 struct elf_link_loaded_list
*loaded
;
6233 if ((undef_bfd
->flags
& DYNAMIC
) == 0
6234 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
6235 || elf_dt_soname (undef_bfd
) == NULL
)
6238 for (loaded
= elf_hash_table (info
)->loaded
;
6240 loaded
= loaded
->next
)
6243 Elf_Internal_Shdr
*hdr
;
6244 bfd_size_type symcount
;
6245 bfd_size_type extsymcount
;
6246 bfd_size_type extsymoff
;
6247 Elf_Internal_Shdr
*versymhdr
;
6248 Elf_Internal_Sym
*isym
;
6249 Elf_Internal_Sym
*isymend
;
6250 Elf_Internal_Sym
*isymbuf
;
6251 Elf_External_Versym
*ever
;
6252 Elf_External_Versym
*extversym
;
6254 input
= loaded
->abfd
;
6256 /* We check each DSO for a possible hidden versioned definition. */
6257 if (input
== undef_bfd
6258 || (input
->flags
& DYNAMIC
) == 0
6259 || elf_dynversym (input
) == 0)
6262 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6264 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6265 if (elf_bad_symtab (input
))
6267 extsymcount
= symcount
;
6272 extsymcount
= symcount
- hdr
->sh_info
;
6273 extsymoff
= hdr
->sh_info
;
6276 if (extsymcount
== 0)
6279 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6281 if (isymbuf
== NULL
)
6284 /* Read in any version definitions. */
6285 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6286 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
6287 if (extversym
== NULL
)
6290 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6291 || (bfd_bread ((PTR
) extversym
, versymhdr
->sh_size
, input
)
6292 != versymhdr
->sh_size
))
6300 ever
= extversym
+ extsymoff
;
6301 isymend
= isymbuf
+ extsymcount
;
6302 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6305 Elf_Internal_Versym iver
;
6307 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6308 || isym
->st_shndx
== SHN_UNDEF
)
6311 name
= bfd_elf_string_from_elf_section (input
,
6314 if (strcmp (name
, h
->root
.root
.string
) != 0)
6317 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6319 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6321 /* If we have a non-hidden versioned sym, then it should
6322 have provided a definition for the undefined sym. */
6326 if ((iver
.vs_vers
& VERSYM_VERSION
) == 2)
6328 /* This is the oldest (default) sym. We can use it. */
6342 /* Add an external symbol to the symbol table. This is called from
6343 the hash table traversal routine. When generating a shared object,
6344 we go through the symbol table twice. The first time we output
6345 anything that might have been forced to local scope in a version
6346 script. The second time we output the symbols that are still
6350 elf_link_output_extsym (h
, data
)
6351 struct elf_link_hash_entry
*h
;
6354 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
6355 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6357 Elf_Internal_Sym sym
;
6358 asection
*input_sec
;
6360 if (h
->root
.type
== bfd_link_hash_warning
)
6362 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6363 if (h
->root
.type
== bfd_link_hash_new
)
6367 /* Decide whether to output this symbol in this pass. */
6368 if (eoinfo
->localsyms
)
6370 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6375 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6379 /* If we are not creating a shared library, and this symbol is
6380 referenced by a shared library but is not defined anywhere, then
6381 warn that it is undefined. If we do not do this, the runtime
6382 linker will complain that the symbol is undefined when the
6383 program is run. We don't have to worry about symbols that are
6384 referenced by regular files, because we will already have issued
6385 warnings for them. */
6386 if (! finfo
->info
->relocateable
6387 && (! finfo
->info
->shared
|| ! finfo
->info
->allow_shlib_undefined
)
6388 && h
->root
.type
== bfd_link_hash_undefined
6389 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
6390 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
6391 && ! elf_link_check_versioned_symbol (finfo
->info
, h
))
6393 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6394 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6395 (asection
*) NULL
, (bfd_vma
) 0, TRUE
)))
6397 eoinfo
->failed
= TRUE
;
6402 /* We should also warn if a forced local symbol is referenced from
6403 shared libraries. */
6404 if (! finfo
->info
->relocateable
6405 && (! finfo
->info
->shared
|| ! finfo
->info
->allow_shlib_undefined
)
6406 && (h
->elf_link_hash_flags
6407 & (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
6408 | ELF_LINK_DYNAMIC_DEF
| ELF_LINK_DYNAMIC_WEAK
))
6409 == (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
))
6411 (*_bfd_error_handler
)
6412 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
6413 bfd_get_filename (finfo
->output_bfd
),
6414 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6416 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6417 ? "hidden" : "local",
6418 h
->root
.root
.string
,
6419 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
6420 eoinfo
->failed
= TRUE
;
6424 /* We don't want to output symbols that have never been mentioned by
6425 a regular file, or that we have been told to strip. However, if
6426 h->indx is set to -2, the symbol is used by a reloc and we must
6430 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
6431 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
6432 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
6433 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
6435 else if (finfo
->info
->strip
== strip_all
)
6437 else if (finfo
->info
->strip
== strip_some
6438 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6439 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6441 else if (finfo
->info
->strip_discarded
6442 && (h
->root
.type
== bfd_link_hash_defined
6443 || h
->root
.type
== bfd_link_hash_defweak
)
6444 && elf_discarded_section (h
->root
.u
.def
.section
))
6449 /* If we're stripping it, and it's not a dynamic symbol, there's
6450 nothing else to do unless it is a forced local symbol. */
6453 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6457 sym
.st_size
= h
->size
;
6458 sym
.st_other
= h
->other
;
6459 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6460 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6461 else if (h
->root
.type
== bfd_link_hash_undefweak
6462 || h
->root
.type
== bfd_link_hash_defweak
)
6463 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6465 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6467 switch (h
->root
.type
)
6470 case bfd_link_hash_new
:
6471 case bfd_link_hash_warning
:
6475 case bfd_link_hash_undefined
:
6476 case bfd_link_hash_undefweak
:
6477 input_sec
= bfd_und_section_ptr
;
6478 sym
.st_shndx
= SHN_UNDEF
;
6481 case bfd_link_hash_defined
:
6482 case bfd_link_hash_defweak
:
6484 input_sec
= h
->root
.u
.def
.section
;
6485 if (input_sec
->output_section
!= NULL
)
6488 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6489 input_sec
->output_section
);
6490 if (sym
.st_shndx
== SHN_BAD
)
6492 (*_bfd_error_handler
)
6493 (_("%s: could not find output section %s for input section %s"),
6494 bfd_get_filename (finfo
->output_bfd
),
6495 input_sec
->output_section
->name
,
6497 eoinfo
->failed
= TRUE
;
6501 /* ELF symbols in relocateable files are section relative,
6502 but in nonrelocateable files they are virtual
6504 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6505 if (! finfo
->info
->relocateable
)
6507 sym
.st_value
+= input_sec
->output_section
->vma
;
6508 if (h
->type
== STT_TLS
)
6510 /* STT_TLS symbols are relative to PT_TLS segment
6512 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6513 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
6519 BFD_ASSERT (input_sec
->owner
== NULL
6520 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6521 sym
.st_shndx
= SHN_UNDEF
;
6522 input_sec
= bfd_und_section_ptr
;
6527 case bfd_link_hash_common
:
6528 input_sec
= h
->root
.u
.c
.p
->section
;
6529 sym
.st_shndx
= SHN_COMMON
;
6530 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6533 case bfd_link_hash_indirect
:
6534 /* These symbols are created by symbol versioning. They point
6535 to the decorated version of the name. For example, if the
6536 symbol foo@@GNU_1.2 is the default, which should be used when
6537 foo is used with no version, then we add an indirect symbol
6538 foo which points to foo@@GNU_1.2. We ignore these symbols,
6539 since the indirected symbol is already in the hash table. */
6543 /* Give the processor backend a chance to tweak the symbol value,
6544 and also to finish up anything that needs to be done for this
6545 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6546 forced local syms when non-shared is due to a historical quirk. */
6547 if ((h
->dynindx
!= -1
6548 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6549 && (finfo
->info
->shared
6550 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6551 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6553 struct elf_backend_data
*bed
;
6555 bed
= get_elf_backend_data (finfo
->output_bfd
);
6556 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6557 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6559 eoinfo
->failed
= TRUE
;
6564 /* If we are marking the symbol as undefined, and there are no
6565 non-weak references to this symbol from a regular object, then
6566 mark the symbol as weak undefined; if there are non-weak
6567 references, mark the symbol as strong. We can't do this earlier,
6568 because it might not be marked as undefined until the
6569 finish_dynamic_symbol routine gets through with it. */
6570 if (sym
.st_shndx
== SHN_UNDEF
6571 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
6572 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6573 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6577 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
6578 bindtype
= STB_GLOBAL
;
6580 bindtype
= STB_WEAK
;
6581 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6584 /* If a non-weak symbol with non-default visibility is not defined
6585 locally, it is a fatal error. */
6586 if (! finfo
->info
->relocateable
6587 && ELF_ST_VISIBILITY (sym
.st_other
)
6588 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6589 && h
->root
.type
!= bfd_link_hash_undefweak
6590 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6592 (*_bfd_error_handler
)
6593 (_("%s: %s symbol `%s' isn't defined"),
6594 bfd_get_filename (finfo
->output_bfd
),
6595 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6597 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6598 ? "internal" : "hidden",
6599 h
->root
.root
.string
);
6600 eoinfo
->failed
= TRUE
;
6604 /* If this symbol should be put in the .dynsym section, then put it
6605 there now. We already know the symbol index. We also fill in
6606 the entry in the .hash section. */
6607 if (h
->dynindx
!= -1
6608 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6612 size_t hash_entry_size
;
6613 bfd_byte
*bucketpos
;
6615 Elf_External_Sym
*esym
;
6617 sym
.st_name
= h
->dynstr_index
;
6618 esym
= (Elf_External_Sym
*) finfo
->dynsym_sec
->contents
+ h
->dynindx
;
6619 elf_swap_symbol_out (finfo
->output_bfd
, &sym
, (PTR
) esym
, (PTR
) 0);
6621 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6622 bucket
= h
->elf_hash_value
% bucketcount
;
6624 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6625 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6626 + (bucket
+ 2) * hash_entry_size
);
6627 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6628 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, (bfd_vma
) h
->dynindx
,
6630 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6631 ((bfd_byte
*) finfo
->hash_sec
->contents
6632 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6634 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6636 Elf_Internal_Versym iversym
;
6637 Elf_External_Versym
*eversym
;
6639 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6641 if (h
->verinfo
.verdef
== NULL
)
6642 iversym
.vs_vers
= 0;
6644 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6648 if (h
->verinfo
.vertree
== NULL
)
6649 iversym
.vs_vers
= 1;
6651 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6654 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
6655 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6657 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6658 eversym
+= h
->dynindx
;
6659 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6663 /* If we're stripping it, then it was just a dynamic symbol, and
6664 there's nothing else to do. */
6665 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6668 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6670 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
6672 eoinfo
->failed
= TRUE
;
6679 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6680 originated from the section given by INPUT_REL_HDR) to the
6684 elf_link_output_relocs (output_bfd
, input_section
, input_rel_hdr
,
6687 asection
*input_section
;
6688 Elf_Internal_Shdr
*input_rel_hdr
;
6689 Elf_Internal_Rela
*internal_relocs
;
6691 Elf_Internal_Rela
*irela
;
6692 Elf_Internal_Rela
*irelaend
;
6694 Elf_Internal_Shdr
*output_rel_hdr
;
6695 asection
*output_section
;
6696 unsigned int *rel_countp
= NULL
;
6697 struct elf_backend_data
*bed
;
6698 void (*swap_out
) PARAMS ((bfd
*, const Elf_Internal_Rela
*, bfd_byte
*));
6700 output_section
= input_section
->output_section
;
6701 output_rel_hdr
= NULL
;
6703 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
6704 == input_rel_hdr
->sh_entsize
)
6706 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
6707 rel_countp
= &elf_section_data (output_section
)->rel_count
;
6709 else if (elf_section_data (output_section
)->rel_hdr2
6710 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
6711 == input_rel_hdr
->sh_entsize
))
6713 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
6714 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
6718 (*_bfd_error_handler
)
6719 (_("%s: relocation size mismatch in %s section %s"),
6720 bfd_get_filename (output_bfd
),
6721 bfd_archive_filename (input_section
->owner
),
6722 input_section
->name
);
6723 bfd_set_error (bfd_error_wrong_object_format
);
6727 bed
= get_elf_backend_data (output_bfd
);
6728 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
6729 swap_out
= bed
->s
->swap_reloc_out
;
6730 else if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
))
6731 swap_out
= bed
->s
->swap_reloca_out
;
6735 erel
= output_rel_hdr
->contents
;
6736 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
6737 irela
= internal_relocs
;
6738 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
6739 * bed
->s
->int_rels_per_ext_rel
);
6740 while (irela
< irelaend
)
6742 (*swap_out
) (output_bfd
, irela
, erel
);
6743 irela
+= bed
->s
->int_rels_per_ext_rel
;
6744 erel
+= input_rel_hdr
->sh_entsize
;
6747 /* Bump the counter, so that we know where to add the next set of
6749 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
6754 /* Link an input file into the linker output file. This function
6755 handles all the sections and relocations of the input file at once.
6756 This is so that we only have to read the local symbols once, and
6757 don't have to keep them in memory. */
6760 elf_link_input_bfd (finfo
, input_bfd
)
6761 struct elf_final_link_info
*finfo
;
6764 bfd_boolean (*relocate_section
)
6765 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6766 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
6768 Elf_Internal_Shdr
*symtab_hdr
;
6771 Elf_Internal_Sym
*isymbuf
;
6772 Elf_Internal_Sym
*isym
;
6773 Elf_Internal_Sym
*isymend
;
6775 asection
**ppsection
;
6777 struct elf_backend_data
*bed
;
6778 bfd_boolean emit_relocs
;
6779 struct elf_link_hash_entry
**sym_hashes
;
6781 output_bfd
= finfo
->output_bfd
;
6782 bed
= get_elf_backend_data (output_bfd
);
6783 relocate_section
= bed
->elf_backend_relocate_section
;
6785 /* If this is a dynamic object, we don't want to do anything here:
6786 we don't want the local symbols, and we don't want the section
6788 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6791 emit_relocs
= (finfo
->info
->relocateable
6792 || finfo
->info
->emitrelocations
6793 || bed
->elf_backend_emit_relocs
);
6795 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6796 if (elf_bad_symtab (input_bfd
))
6798 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6803 locsymcount
= symtab_hdr
->sh_info
;
6804 extsymoff
= symtab_hdr
->sh_info
;
6807 /* Read the local symbols. */
6808 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6809 if (isymbuf
== NULL
&& locsymcount
!= 0)
6811 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6812 finfo
->internal_syms
,
6813 finfo
->external_syms
,
6814 finfo
->locsym_shndx
);
6815 if (isymbuf
== NULL
)
6819 /* Find local symbol sections and adjust values of symbols in
6820 SEC_MERGE sections. Write out those local symbols we know are
6821 going into the output file. */
6822 isymend
= isymbuf
+ locsymcount
;
6823 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6825 isym
++, pindex
++, ppsection
++)
6829 Elf_Internal_Sym osym
;
6833 if (elf_bad_symtab (input_bfd
))
6835 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6842 if (isym
->st_shndx
== SHN_UNDEF
)
6843 isec
= bfd_und_section_ptr
;
6844 else if (isym
->st_shndx
< SHN_LORESERVE
6845 || isym
->st_shndx
> SHN_HIRESERVE
)
6847 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
6849 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6850 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6852 _bfd_merged_section_offset (output_bfd
, &isec
,
6853 elf_section_data (isec
)->sec_info
,
6854 isym
->st_value
, (bfd_vma
) 0);
6856 else if (isym
->st_shndx
== SHN_ABS
)
6857 isec
= bfd_abs_section_ptr
;
6858 else if (isym
->st_shndx
== SHN_COMMON
)
6859 isec
= bfd_com_section_ptr
;
6868 /* Don't output the first, undefined, symbol. */
6869 if (ppsection
== finfo
->sections
)
6872 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6874 /* We never output section symbols. Instead, we use the
6875 section symbol of the corresponding section in the output
6880 /* If we are stripping all symbols, we don't want to output this
6882 if (finfo
->info
->strip
== strip_all
)
6885 /* If we are discarding all local symbols, we don't want to
6886 output this one. If we are generating a relocateable output
6887 file, then some of the local symbols may be required by
6888 relocs; we output them below as we discover that they are
6890 if (finfo
->info
->discard
== discard_all
)
6893 /* If this symbol is defined in a section which we are
6894 discarding, we don't need to keep it, but note that
6895 linker_mark is only reliable for sections that have contents.
6896 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6897 as well as linker_mark. */
6898 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6900 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6901 || (! finfo
->info
->relocateable
6902 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6905 /* Get the name of the symbol. */
6906 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6911 /* See if we are discarding symbols with this name. */
6912 if ((finfo
->info
->strip
== strip_some
6913 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6915 || (((finfo
->info
->discard
== discard_sec_merge
6916 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocateable
)
6917 || finfo
->info
->discard
== discard_l
)
6918 && bfd_is_local_label_name (input_bfd
, name
)))
6921 /* If we get here, we are going to output this symbol. */
6925 /* Adjust the section index for the output file. */
6926 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6927 isec
->output_section
);
6928 if (osym
.st_shndx
== SHN_BAD
)
6931 *pindex
= bfd_get_symcount (output_bfd
);
6933 /* ELF symbols in relocateable files are section relative, but
6934 in executable files they are virtual addresses. Note that
6935 this code assumes that all ELF sections have an associated
6936 BFD section with a reasonable value for output_offset; below
6937 we assume that they also have a reasonable value for
6938 output_section. Any special sections must be set up to meet
6939 these requirements. */
6940 osym
.st_value
+= isec
->output_offset
;
6941 if (! finfo
->info
->relocateable
)
6943 osym
.st_value
+= isec
->output_section
->vma
;
6944 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6946 /* STT_TLS symbols are relative to PT_TLS segment base. */
6947 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6948 osym
.st_value
-= finfo
->first_tls_sec
->vma
;
6952 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
6956 /* Relocate the contents of each section. */
6957 sym_hashes
= elf_sym_hashes (input_bfd
);
6958 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6962 if (! o
->linker_mark
)
6964 /* This section was omitted from the link. */
6968 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6969 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6972 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6974 /* Section was created by elf_link_create_dynamic_sections
6979 /* Get the contents of the section. They have been cached by a
6980 relaxation routine. Note that o is a section in an input
6981 file, so the contents field will not have been set by any of
6982 the routines which work on output files. */
6983 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6984 contents
= elf_section_data (o
)->this_hdr
.contents
;
6987 contents
= finfo
->contents
;
6988 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
6989 (file_ptr
) 0, o
->_raw_size
))
6993 if ((o
->flags
& SEC_RELOC
) != 0)
6995 Elf_Internal_Rela
*internal_relocs
;
6997 /* Get the swapped relocs. */
6998 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
6999 (input_bfd
, o
, finfo
->external_relocs
,
7000 finfo
->internal_relocs
, FALSE
));
7001 if (internal_relocs
== NULL
7002 && o
->reloc_count
> 0)
7005 /* Run through the relocs looking for any against symbols
7006 from discarded sections and section symbols from
7007 removed link-once sections. Complain about relocs
7008 against discarded sections. Zero relocs against removed
7009 link-once sections. */
7010 if (!finfo
->info
->relocateable
7011 && !elf_section_ignore_discarded_relocs (o
))
7013 Elf_Internal_Rela
*rel
, *relend
;
7015 rel
= internal_relocs
;
7016 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7017 for ( ; rel
< relend
; rel
++)
7019 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7021 if (r_symndx
>= locsymcount
7022 || (elf_bad_symtab (input_bfd
)
7023 && finfo
->sections
[r_symndx
] == NULL
))
7025 struct elf_link_hash_entry
*h
;
7027 h
= sym_hashes
[r_symndx
- extsymoff
];
7028 while (h
->root
.type
== bfd_link_hash_indirect
7029 || h
->root
.type
== bfd_link_hash_warning
)
7030 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7032 /* Complain if the definition comes from a
7033 discarded section. */
7034 if ((h
->root
.type
== bfd_link_hash_defined
7035 || h
->root
.type
== bfd_link_hash_defweak
)
7036 && elf_discarded_section (h
->root
.u
.def
.section
))
7038 if ((o
->flags
& SEC_DEBUGGING
) != 0)
7040 BFD_ASSERT (r_symndx
!= 0);
7041 memset (rel
, 0, sizeof (*rel
));
7045 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
7046 (finfo
->info
, h
->root
.root
.string
,
7047 input_bfd
, o
, rel
->r_offset
,
7055 asection
*sec
= finfo
->sections
[r_symndx
];
7057 if (sec
!= NULL
&& elf_discarded_section (sec
))
7059 if ((o
->flags
& SEC_DEBUGGING
) != 0
7060 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
7062 BFD_ASSERT (r_symndx
!= 0);
7064 = ELF_R_INFO (0, ELF_R_TYPE (rel
->r_info
));
7071 = _("local symbols in discarded section %s");
7073 = strlen (sec
->name
) + strlen (msg
) - 1;
7074 char *buf
= (char *) bfd_malloc (amt
);
7077 sprintf (buf
, msg
, sec
->name
);
7079 buf
= (char *) sec
->name
;
7080 ok
= (*finfo
->info
->callbacks
7081 ->undefined_symbol
) (finfo
->info
, buf
,
7085 if (buf
!= sec
->name
)
7095 /* Relocate the section by invoking a back end routine.
7097 The back end routine is responsible for adjusting the
7098 section contents as necessary, and (if using Rela relocs
7099 and generating a relocateable output file) adjusting the
7100 reloc addend as necessary.
7102 The back end routine does not have to worry about setting
7103 the reloc address or the reloc symbol index.
7105 The back end routine is given a pointer to the swapped in
7106 internal symbols, and can access the hash table entries
7107 for the external symbols via elf_sym_hashes (input_bfd).
7109 When generating relocateable output, the back end routine
7110 must handle STB_LOCAL/STT_SECTION symbols specially. The
7111 output symbol is going to be a section symbol
7112 corresponding to the output section, which will require
7113 the addend to be adjusted. */
7115 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
7116 input_bfd
, o
, contents
,
7124 Elf_Internal_Rela
*irela
;
7125 Elf_Internal_Rela
*irelaend
;
7126 bfd_vma last_offset
;
7127 struct elf_link_hash_entry
**rel_hash
;
7128 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
7129 unsigned int next_erel
;
7130 bfd_boolean (*reloc_emitter
)
7131 PARAMS ((bfd
*, asection
*, Elf_Internal_Shdr
*,
7132 Elf_Internal_Rela
*));
7133 bfd_boolean rela_normal
;
7135 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7136 rela_normal
= (bed
->rela_normal
7137 && (input_rel_hdr
->sh_entsize
7138 == sizeof (Elf_External_Rela
)));
7140 /* Adjust the reloc addresses and symbol indices. */
7142 irela
= internal_relocs
;
7143 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7144 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7145 + elf_section_data (o
->output_section
)->rel_count
7146 + elf_section_data (o
->output_section
)->rel_count2
);
7147 last_offset
= o
->output_offset
;
7148 if (!finfo
->info
->relocateable
)
7149 last_offset
+= o
->output_section
->vma
;
7150 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7152 unsigned long r_symndx
;
7154 Elf_Internal_Sym sym
;
7156 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7162 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
7165 if (irela
->r_offset
>= (bfd_vma
) -2)
7167 /* This is a reloc for a deleted entry or somesuch.
7168 Turn it into an R_*_NONE reloc, at the same
7169 offset as the last reloc. elf_eh_frame.c and
7170 elf_bfd_discard_info rely on reloc offsets
7172 irela
->r_offset
= last_offset
;
7174 irela
->r_addend
= 0;
7178 irela
->r_offset
+= o
->output_offset
;
7180 /* Relocs in an executable have to be virtual addresses. */
7181 if (!finfo
->info
->relocateable
)
7182 irela
->r_offset
+= o
->output_section
->vma
;
7184 last_offset
= irela
->r_offset
;
7186 r_symndx
= ELF_R_SYM (irela
->r_info
);
7187 if (r_symndx
== STN_UNDEF
)
7190 if (r_symndx
>= locsymcount
7191 || (elf_bad_symtab (input_bfd
)
7192 && finfo
->sections
[r_symndx
] == NULL
))
7194 struct elf_link_hash_entry
*rh
;
7197 /* This is a reloc against a global symbol. We
7198 have not yet output all the local symbols, so
7199 we do not know the symbol index of any global
7200 symbol. We set the rel_hash entry for this
7201 reloc to point to the global hash table entry
7202 for this symbol. The symbol index is then
7203 set at the end of elf_bfd_final_link. */
7204 indx
= r_symndx
- extsymoff
;
7205 rh
= elf_sym_hashes (input_bfd
)[indx
];
7206 while (rh
->root
.type
== bfd_link_hash_indirect
7207 || rh
->root
.type
== bfd_link_hash_warning
)
7208 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7210 /* Setting the index to -2 tells
7211 elf_link_output_extsym that this symbol is
7213 BFD_ASSERT (rh
->indx
< 0);
7221 /* This is a reloc against a local symbol. */
7224 sym
= isymbuf
[r_symndx
];
7225 sec
= finfo
->sections
[r_symndx
];
7226 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7228 /* I suppose the backend ought to fill in the
7229 section of any STT_SECTION symbol against a
7230 processor specific section. If we have
7231 discarded a section, the output_section will
7232 be the absolute section. */
7233 if (bfd_is_abs_section (sec
)
7235 && bfd_is_abs_section (sec
->output_section
)))
7237 else if (sec
== NULL
|| sec
->owner
== NULL
)
7239 bfd_set_error (bfd_error_bad_value
);
7244 r_symndx
= sec
->output_section
->target_index
;
7245 BFD_ASSERT (r_symndx
!= 0);
7248 /* Adjust the addend according to where the
7249 section winds up in the output section. */
7251 irela
->r_addend
+= sec
->output_offset
;
7255 if (finfo
->indices
[r_symndx
] == -1)
7257 unsigned long shlink
;
7261 if (finfo
->info
->strip
== strip_all
)
7263 /* You can't do ld -r -s. */
7264 bfd_set_error (bfd_error_invalid_operation
);
7268 /* This symbol was skipped earlier, but
7269 since it is needed by a reloc, we
7270 must output it now. */
7271 shlink
= symtab_hdr
->sh_link
;
7272 name
= (bfd_elf_string_from_elf_section
7273 (input_bfd
, shlink
, sym
.st_name
));
7277 osec
= sec
->output_section
;
7279 _bfd_elf_section_from_bfd_section (output_bfd
,
7281 if (sym
.st_shndx
== SHN_BAD
)
7284 sym
.st_value
+= sec
->output_offset
;
7285 if (! finfo
->info
->relocateable
)
7287 sym
.st_value
+= osec
->vma
;
7288 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7290 /* STT_TLS symbols are relative to PT_TLS
7292 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
7293 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
7297 finfo
->indices
[r_symndx
]
7298 = bfd_get_symcount (output_bfd
);
7300 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
))
7304 r_symndx
= finfo
->indices
[r_symndx
];
7307 irela
->r_info
= ELF_R_INFO (r_symndx
,
7308 ELF_R_TYPE (irela
->r_info
));
7311 /* Swap out the relocs. */
7312 if (bed
->elf_backend_emit_relocs
7313 && !(finfo
->info
->relocateable
7314 || finfo
->info
->emitrelocations
))
7315 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7317 reloc_emitter
= elf_link_output_relocs
;
7319 if (input_rel_hdr
->sh_size
!= 0
7320 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7324 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7325 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7327 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7328 * bed
->s
->int_rels_per_ext_rel
);
7329 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
7336 /* Write out the modified section contents. */
7337 if (bed
->elf_backend_write_section
7338 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7340 /* Section written out. */
7342 else switch (o
->sec_info_type
)
7344 case ELF_INFO_TYPE_STABS
:
7345 if (! (_bfd_write_section_stabs
7347 &elf_hash_table (finfo
->info
)->stab_info
,
7348 o
, &elf_section_data (o
)->sec_info
, contents
)))
7351 case ELF_INFO_TYPE_MERGE
:
7352 if (! _bfd_write_merged_section (output_bfd
, o
,
7353 elf_section_data (o
)->sec_info
))
7356 case ELF_INFO_TYPE_EH_FRAME
:
7358 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7365 bfd_size_type sec_size
;
7367 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
7368 if (! (o
->flags
& SEC_EXCLUDE
)
7369 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7371 (file_ptr
) o
->output_offset
,
7382 /* Generate a reloc when linking an ELF file. This is a reloc
7383 requested by the linker, and does come from any input file. This
7384 is used to build constructor and destructor tables when linking
7388 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
7390 struct bfd_link_info
*info
;
7391 asection
*output_section
;
7392 struct bfd_link_order
*link_order
;
7394 reloc_howto_type
*howto
;
7398 struct elf_link_hash_entry
**rel_hash_ptr
;
7399 Elf_Internal_Shdr
*rel_hdr
;
7400 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7401 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7405 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7408 bfd_set_error (bfd_error_bad_value
);
7412 addend
= link_order
->u
.reloc
.p
->addend
;
7414 /* Figure out the symbol index. */
7415 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7416 + elf_section_data (output_section
)->rel_count
7417 + elf_section_data (output_section
)->rel_count2
);
7418 if (link_order
->type
== bfd_section_reloc_link_order
)
7420 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7421 BFD_ASSERT (indx
!= 0);
7422 *rel_hash_ptr
= NULL
;
7426 struct elf_link_hash_entry
*h
;
7428 /* Treat a reloc against a defined symbol as though it were
7429 actually against the section. */
7430 h
= ((struct elf_link_hash_entry
*)
7431 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7432 link_order
->u
.reloc
.p
->u
.name
,
7433 FALSE
, FALSE
, TRUE
));
7435 && (h
->root
.type
== bfd_link_hash_defined
7436 || h
->root
.type
== bfd_link_hash_defweak
))
7440 section
= h
->root
.u
.def
.section
;
7441 indx
= section
->output_section
->target_index
;
7442 *rel_hash_ptr
= NULL
;
7443 /* It seems that we ought to add the symbol value to the
7444 addend here, but in practice it has already been added
7445 because it was passed to constructor_callback. */
7446 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7450 /* Setting the index to -2 tells elf_link_output_extsym that
7451 this symbol is used by a reloc. */
7458 if (! ((*info
->callbacks
->unattached_reloc
)
7459 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
7460 (asection
*) NULL
, (bfd_vma
) 0)))
7466 /* If this is an inplace reloc, we must write the addend into the
7468 if (howto
->partial_inplace
&& addend
!= 0)
7471 bfd_reloc_status_type rstat
;
7474 const char *sym_name
;
7476 size
= bfd_get_reloc_size (howto
);
7477 buf
= (bfd_byte
*) bfd_zmalloc (size
);
7478 if (buf
== (bfd_byte
*) NULL
)
7480 rstat
= _bfd_relocate_contents (howto
, output_bfd
, (bfd_vma
) addend
, buf
);
7487 case bfd_reloc_outofrange
:
7490 case bfd_reloc_overflow
:
7491 if (link_order
->type
== bfd_section_reloc_link_order
)
7492 sym_name
= bfd_section_name (output_bfd
,
7493 link_order
->u
.reloc
.p
->u
.section
);
7495 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7496 if (! ((*info
->callbacks
->reloc_overflow
)
7497 (info
, sym_name
, howto
->name
, addend
,
7498 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
7505 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
7506 (file_ptr
) link_order
->offset
, size
);
7512 /* The address of a reloc is relative to the section in a
7513 relocateable file, and is a virtual address in an executable
7515 offset
= link_order
->offset
;
7516 if (! info
->relocateable
)
7517 offset
+= output_section
->vma
;
7519 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7521 irel
[i
].r_offset
= offset
;
7523 irel
[i
].r_addend
= 0;
7525 irel
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
7527 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7528 erel
= rel_hdr
->contents
;
7529 if (rel_hdr
->sh_type
== SHT_REL
)
7531 erel
+= (elf_section_data (output_section
)->rel_count
7532 * sizeof (Elf_External_Rel
));
7533 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7537 irel
[0].r_addend
= addend
;
7538 erel
+= (elf_section_data (output_section
)->rel_count
7539 * sizeof (Elf_External_Rela
));
7540 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7543 ++elf_section_data (output_section
)->rel_count
;
7548 /* Allocate a pointer to live in a linker created section. */
7551 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
7553 struct bfd_link_info
*info
;
7554 elf_linker_section_t
*lsect
;
7555 struct elf_link_hash_entry
*h
;
7556 const Elf_Internal_Rela
*rel
;
7558 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
7559 elf_linker_section_pointers_t
*linker_section_ptr
;
7560 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7563 BFD_ASSERT (lsect
!= NULL
);
7565 /* Is this a global symbol? */
7568 /* Has this symbol already been allocated? If so, our work is done. */
7569 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
7574 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
7575 /* Make sure this symbol is output as a dynamic symbol. */
7576 if (h
->dynindx
== -1)
7578 if (! elf_link_record_dynamic_symbol (info
, h
))
7582 if (lsect
->rel_section
)
7583 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7587 /* Allocation of a pointer to a local symbol. */
7588 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
7590 /* Allocate a table to hold the local symbols if first time. */
7593 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
7594 register unsigned int i
;
7597 amt
*= sizeof (elf_linker_section_pointers_t
*);
7598 ptr
= (elf_linker_section_pointers_t
**) bfd_alloc (abfd
, amt
);
7603 elf_local_ptr_offsets (abfd
) = ptr
;
7604 for (i
= 0; i
< num_symbols
; i
++)
7605 ptr
[i
] = (elf_linker_section_pointers_t
*) 0;
7608 /* Has this symbol already been allocated? If so, our work is done. */
7609 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
7614 ptr_linker_section_ptr
= &ptr
[r_symndx
];
7618 /* If we are generating a shared object, we need to
7619 output a R_<xxx>_RELATIVE reloc so that the
7620 dynamic linker can adjust this GOT entry. */
7621 BFD_ASSERT (lsect
->rel_section
!= NULL
);
7622 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7626 /* Allocate space for a pointer in the linker section, and allocate
7627 a new pointer record from internal memory. */
7628 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
7629 amt
= sizeof (elf_linker_section_pointers_t
);
7630 linker_section_ptr
= (elf_linker_section_pointers_t
*) bfd_alloc (abfd
, amt
);
7632 if (!linker_section_ptr
)
7635 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
7636 linker_section_ptr
->addend
= rel
->r_addend
;
7637 linker_section_ptr
->which
= lsect
->which
;
7638 linker_section_ptr
->written_address_p
= FALSE
;
7639 *ptr_linker_section_ptr
= linker_section_ptr
;
7642 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
7644 linker_section_ptr
->offset
= (lsect
->section
->_raw_size
7645 - lsect
->hole_size
+ (ARCH_SIZE
/ 8));
7646 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
7647 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
7648 if (lsect
->sym_hash
)
7650 /* Bump up symbol value if needed. */
7651 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
7653 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
7654 lsect
->sym_hash
->root
.root
.string
,
7655 (long) ARCH_SIZE
/ 8,
7656 (long) lsect
->sym_hash
->root
.u
.def
.value
);
7662 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
7664 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
7668 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7669 lsect
->name
, (long) linker_section_ptr
->offset
,
7670 (long) lsect
->section
->_raw_size
);
7677 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7680 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7683 /* Fill in the address for a pointer generated in a linker section. */
7686 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
,
7687 relocation
, rel
, relative_reloc
)
7690 struct bfd_link_info
*info
;
7691 elf_linker_section_t
*lsect
;
7692 struct elf_link_hash_entry
*h
;
7694 const Elf_Internal_Rela
*rel
;
7697 elf_linker_section_pointers_t
*linker_section_ptr
;
7699 BFD_ASSERT (lsect
!= NULL
);
7703 /* Handle global symbol. */
7704 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7705 (h
->linker_section_pointer
,
7709 BFD_ASSERT (linker_section_ptr
!= NULL
);
7711 if (! elf_hash_table (info
)->dynamic_sections_created
7714 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
7716 /* This is actually a static link, or it is a
7717 -Bsymbolic link and the symbol is defined
7718 locally. We must initialize this entry in the
7721 When doing a dynamic link, we create a .rela.<xxx>
7722 relocation entry to initialize the value. This
7723 is done in the finish_dynamic_symbol routine. */
7724 if (!linker_section_ptr
->written_address_p
)
7726 linker_section_ptr
->written_address_p
= TRUE
;
7727 bfd_put_ptr (output_bfd
,
7728 relocation
+ linker_section_ptr
->addend
,
7729 (lsect
->section
->contents
7730 + linker_section_ptr
->offset
));
7736 /* Handle local symbol. */
7737 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7738 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
7739 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
7740 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7741 (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
7745 BFD_ASSERT (linker_section_ptr
!= NULL
);
7747 /* Write out pointer if it hasn't been rewritten out before. */
7748 if (!linker_section_ptr
->written_address_p
)
7750 linker_section_ptr
->written_address_p
= TRUE
;
7751 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
7752 lsect
->section
->contents
+ linker_section_ptr
->offset
);
7756 asection
*srel
= lsect
->rel_section
;
7757 Elf_Internal_Rela outrel
[MAX_INT_RELS_PER_EXT_REL
];
7759 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7762 /* We need to generate a relative reloc for the dynamic
7766 srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
7768 lsect
->rel_section
= srel
;
7771 BFD_ASSERT (srel
!= NULL
);
7773 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7775 outrel
[i
].r_offset
= (lsect
->section
->output_section
->vma
7776 + lsect
->section
->output_offset
7777 + linker_section_ptr
->offset
);
7778 outrel
[i
].r_info
= 0;
7779 outrel
[i
].r_addend
= 0;
7781 outrel
[0].r_info
= ELF_R_INFO (0, relative_reloc
);
7782 erel
= lsect
->section
->contents
;
7783 erel
+= (elf_section_data (lsect
->section
)->rel_count
++
7784 * sizeof (Elf_External_Rela
));
7785 elf_swap_reloca_out (output_bfd
, outrel
, erel
);
7790 relocation
= (lsect
->section
->output_offset
7791 + linker_section_ptr
->offset
7792 - lsect
->hole_offset
7793 - lsect
->sym_offset
);
7797 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7798 lsect
->name
, (long) relocation
, (long) relocation
);
7801 /* Subtract out the addend, because it will get added back in by the normal
7803 return relocation
- linker_section_ptr
->addend
;
7806 /* Garbage collect unused sections. */
7808 static bfd_boolean elf_gc_mark
7809 PARAMS ((struct bfd_link_info
*, asection
*,
7810 asection
* (*) (asection
*, struct bfd_link_info
*,
7811 Elf_Internal_Rela
*, struct elf_link_hash_entry
*,
7812 Elf_Internal_Sym
*)));
7814 static bfd_boolean elf_gc_sweep
7815 PARAMS ((struct bfd_link_info
*,
7816 bfd_boolean (*) (bfd
*, struct bfd_link_info
*, asection
*,
7817 const Elf_Internal_Rela
*)));
7819 static bfd_boolean elf_gc_sweep_symbol
7820 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7822 static bfd_boolean elf_gc_allocate_got_offsets
7823 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7825 static bfd_boolean elf_gc_propagate_vtable_entries_used
7826 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7828 static bfd_boolean elf_gc_smash_unused_vtentry_relocs
7829 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7831 /* The mark phase of garbage collection. For a given section, mark
7832 it and any sections in this section's group, and all the sections
7833 which define symbols to which it refers. */
7835 typedef asection
* (*gc_mark_hook_fn
)
7836 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7837 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
7840 elf_gc_mark (info
, sec
, gc_mark_hook
)
7841 struct bfd_link_info
*info
;
7843 gc_mark_hook_fn gc_mark_hook
;
7846 asection
*group_sec
;
7850 /* Mark all the sections in the group. */
7851 group_sec
= elf_section_data (sec
)->next_in_group
;
7852 if (group_sec
&& !group_sec
->gc_mark
)
7853 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
7856 /* Look through the section relocs. */
7858 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
7860 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
7861 Elf_Internal_Shdr
*symtab_hdr
;
7862 struct elf_link_hash_entry
**sym_hashes
;
7865 bfd
*input_bfd
= sec
->owner
;
7866 struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
7867 Elf_Internal_Sym
*isym
= NULL
;
7869 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
7870 sym_hashes
= elf_sym_hashes (input_bfd
);
7872 /* Read the local symbols. */
7873 if (elf_bad_symtab (input_bfd
))
7875 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
7879 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
7881 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
7882 if (isym
== NULL
&& nlocsyms
!= 0)
7884 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
7890 /* Read the relocations. */
7891 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
7892 (input_bfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
7893 info
->keep_memory
));
7894 if (relstart
== NULL
)
7899 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7901 for (rel
= relstart
; rel
< relend
; rel
++)
7903 unsigned long r_symndx
;
7905 struct elf_link_hash_entry
*h
;
7907 r_symndx
= ELF_R_SYM (rel
->r_info
);
7911 if (r_symndx
>= nlocsyms
7912 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
7914 h
= sym_hashes
[r_symndx
- extsymoff
];
7915 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
7919 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
7922 if (rsec
&& !rsec
->gc_mark
)
7924 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
7926 else if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
7935 if (elf_section_data (sec
)->relocs
!= relstart
)
7938 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
7940 if (! info
->keep_memory
)
7943 symtab_hdr
->contents
= (unsigned char *) isym
;
7950 /* The sweep phase of garbage collection. Remove all garbage sections. */
7952 typedef bfd_boolean (*gc_sweep_hook_fn
)
7953 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
7954 const Elf_Internal_Rela
*));
7957 elf_gc_sweep (info
, gc_sweep_hook
)
7958 struct bfd_link_info
*info
;
7959 gc_sweep_hook_fn gc_sweep_hook
;
7963 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7967 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
7970 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7972 /* Keep special sections. Keep .debug sections. */
7973 if ((o
->flags
& SEC_LINKER_CREATED
)
7974 || (o
->flags
& SEC_DEBUGGING
))
7980 /* Skip sweeping sections already excluded. */
7981 if (o
->flags
& SEC_EXCLUDE
)
7984 /* Since this is early in the link process, it is simple
7985 to remove a section from the output. */
7986 o
->flags
|= SEC_EXCLUDE
;
7988 /* But we also have to update some of the relocation
7989 info we collected before. */
7991 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
7993 Elf_Internal_Rela
*internal_relocs
;
7996 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
7997 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
7998 if (internal_relocs
== NULL
)
8001 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8003 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8004 free (internal_relocs
);
8012 /* Remove the symbols that were in the swept sections from the dynamic
8013 symbol table. GCFIXME: Anyone know how to get them out of the
8014 static symbol table as well? */
8018 elf_link_hash_traverse (elf_hash_table (info
),
8019 elf_gc_sweep_symbol
,
8022 elf_hash_table (info
)->dynsymcount
= i
;
8028 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8031 elf_gc_sweep_symbol (h
, idxptr
)
8032 struct elf_link_hash_entry
*h
;
8035 int *idx
= (int *) idxptr
;
8037 if (h
->root
.type
== bfd_link_hash_warning
)
8038 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8040 if (h
->dynindx
!= -1
8041 && ((h
->root
.type
!= bfd_link_hash_defined
8042 && h
->root
.type
!= bfd_link_hash_defweak
)
8043 || h
->root
.u
.def
.section
->gc_mark
))
8044 h
->dynindx
= (*idx
)++;
8049 /* Propogate collected vtable information. This is called through
8050 elf_link_hash_traverse. */
8053 elf_gc_propagate_vtable_entries_used (h
, okp
)
8054 struct elf_link_hash_entry
*h
;
8057 if (h
->root
.type
== bfd_link_hash_warning
)
8058 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8060 /* Those that are not vtables. */
8061 if (h
->vtable_parent
== NULL
)
8064 /* Those vtables that do not have parents, we cannot merge. */
8065 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
8068 /* If we've already been done, exit. */
8069 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
8072 /* Make sure the parent's table is up to date. */
8073 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
8075 if (h
->vtable_entries_used
== NULL
)
8077 /* None of this table's entries were referenced. Re-use the
8079 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
8080 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
8085 bfd_boolean
*cu
, *pu
;
8087 /* Or the parent's entries into ours. */
8088 cu
= h
->vtable_entries_used
;
8090 pu
= h
->vtable_parent
->vtable_entries_used
;
8093 asection
*sec
= h
->root
.u
.def
.section
;
8094 struct elf_backend_data
*bed
= get_elf_backend_data (sec
->owner
);
8095 int file_align
= bed
->s
->file_align
;
8097 n
= h
->vtable_parent
->vtable_entries_size
/ file_align
;
8112 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
8113 struct elf_link_hash_entry
*h
;
8117 bfd_vma hstart
, hend
;
8118 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8119 struct elf_backend_data
*bed
;
8122 if (h
->root
.type
== bfd_link_hash_warning
)
8123 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8125 /* Take care of both those symbols that do not describe vtables as
8126 well as those that are not loaded. */
8127 if (h
->vtable_parent
== NULL
)
8130 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8131 || h
->root
.type
== bfd_link_hash_defweak
);
8133 sec
= h
->root
.u
.def
.section
;
8134 hstart
= h
->root
.u
.def
.value
;
8135 hend
= hstart
+ h
->size
;
8137 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
8138 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, TRUE
));
8140 return *(bfd_boolean
*) okp
= FALSE
;
8141 bed
= get_elf_backend_data (sec
->owner
);
8142 file_align
= bed
->s
->file_align
;
8144 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8146 for (rel
= relstart
; rel
< relend
; ++rel
)
8147 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8149 /* If the entry is in use, do nothing. */
8150 if (h
->vtable_entries_used
8151 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
8153 bfd_vma entry
= (rel
->r_offset
- hstart
) / file_align
;
8154 if (h
->vtable_entries_used
[entry
])
8157 /* Otherwise, kill it. */
8158 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8164 /* Do mark and sweep of unused sections. */
8167 elf_gc_sections (abfd
, info
)
8169 struct bfd_link_info
*info
;
8171 bfd_boolean ok
= TRUE
;
8173 asection
* (*gc_mark_hook
)
8174 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8175 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
8177 if (!get_elf_backend_data (abfd
)->can_gc_sections
8178 || info
->relocateable
|| info
->emitrelocations
8179 || elf_hash_table (info
)->dynamic_sections_created
)
8182 /* Apply transitive closure to the vtable entry usage info. */
8183 elf_link_hash_traverse (elf_hash_table (info
),
8184 elf_gc_propagate_vtable_entries_used
,
8189 /* Kill the vtable relocations that were not used. */
8190 elf_link_hash_traverse (elf_hash_table (info
),
8191 elf_gc_smash_unused_vtentry_relocs
,
8196 /* Grovel through relocs to find out who stays ... */
8198 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8199 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8203 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8206 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8208 if (o
->flags
& SEC_KEEP
)
8209 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
8214 /* ... and mark SEC_EXCLUDE for those that go. */
8215 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8221 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
8224 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
8227 struct elf_link_hash_entry
*h
;
8230 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8231 struct elf_link_hash_entry
**search
, *child
;
8232 bfd_size_type extsymcount
;
8234 /* The sh_info field of the symtab header tells us where the
8235 external symbols start. We don't care about the local symbols at
8237 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
8238 if (!elf_bad_symtab (abfd
))
8239 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8241 sym_hashes
= elf_sym_hashes (abfd
);
8242 sym_hashes_end
= sym_hashes
+ extsymcount
;
8244 /* Hunt down the child symbol, which is in this section at the same
8245 offset as the relocation. */
8246 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8248 if ((child
= *search
) != NULL
8249 && (child
->root
.type
== bfd_link_hash_defined
8250 || child
->root
.type
== bfd_link_hash_defweak
)
8251 && child
->root
.u
.def
.section
== sec
8252 && child
->root
.u
.def
.value
== offset
)
8256 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
8257 bfd_archive_filename (abfd
), sec
->name
,
8258 (unsigned long) offset
);
8259 bfd_set_error (bfd_error_invalid_operation
);
8265 /* This *should* only be the absolute section. It could potentially
8266 be that someone has defined a non-global vtable though, which
8267 would be bad. It isn't worth paging in the local symbols to be
8268 sure though; that case should simply be handled by the assembler. */
8270 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
8273 child
->vtable_parent
= h
;
8278 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
8281 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
8282 bfd
*abfd ATTRIBUTE_UNUSED
;
8283 asection
*sec ATTRIBUTE_UNUSED
;
8284 struct elf_link_hash_entry
*h
;
8287 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8288 int file_align
= bed
->s
->file_align
;
8290 if (addend
>= h
->vtable_entries_size
)
8293 bfd_boolean
*ptr
= h
->vtable_entries_used
;
8295 /* While the symbol is undefined, we have to be prepared to handle
8297 if (h
->root
.type
== bfd_link_hash_undefined
)
8304 /* Oops! We've got a reference past the defined end of
8305 the table. This is probably a bug -- shall we warn? */
8310 /* Allocate one extra entry for use as a "done" flag for the
8311 consolidation pass. */
8312 bytes
= (size
/ file_align
+ 1) * sizeof (bfd_boolean
);
8316 ptr
= bfd_realloc (ptr
- 1, (bfd_size_type
) bytes
);
8322 oldbytes
= ((h
->vtable_entries_size
/ file_align
+ 1)
8323 * sizeof (bfd_boolean
));
8324 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8328 ptr
= bfd_zmalloc ((bfd_size_type
) bytes
);
8333 /* And arrange for that done flag to be at index -1. */
8334 h
->vtable_entries_used
= ptr
+ 1;
8335 h
->vtable_entries_size
= size
;
8338 h
->vtable_entries_used
[addend
/ file_align
] = TRUE
;
8343 /* And an accompanying bit to work out final got entry offsets once
8344 we're done. Should be called from final_link. */
8347 elf_gc_common_finalize_got_offsets (abfd
, info
)
8349 struct bfd_link_info
*info
;
8352 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8355 /* The GOT offset is relative to the .got section, but the GOT header is
8356 put into the .got.plt section, if the backend uses it. */
8357 if (bed
->want_got_plt
)
8360 gotoff
= bed
->got_header_size
;
8362 /* Do the local .got entries first. */
8363 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
8365 bfd_signed_vma
*local_got
;
8366 bfd_size_type j
, locsymcount
;
8367 Elf_Internal_Shdr
*symtab_hdr
;
8369 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
8372 local_got
= elf_local_got_refcounts (i
);
8376 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
8377 if (elf_bad_symtab (i
))
8378 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8380 locsymcount
= symtab_hdr
->sh_info
;
8382 for (j
= 0; j
< locsymcount
; ++j
)
8384 if (local_got
[j
] > 0)
8386 local_got
[j
] = gotoff
;
8387 gotoff
+= ARCH_SIZE
/ 8;
8390 local_got
[j
] = (bfd_vma
) -1;
8394 /* Then the global .got entries. .plt refcounts are handled by
8395 adjust_dynamic_symbol */
8396 elf_link_hash_traverse (elf_hash_table (info
),
8397 elf_gc_allocate_got_offsets
,
8402 /* We need a special top-level link routine to convert got reference counts
8403 to real got offsets. */
8406 elf_gc_allocate_got_offsets (h
, offarg
)
8407 struct elf_link_hash_entry
*h
;
8410 bfd_vma
*off
= (bfd_vma
*) offarg
;
8412 if (h
->root
.type
== bfd_link_hash_warning
)
8413 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8415 if (h
->got
.refcount
> 0)
8417 h
->got
.offset
= off
[0];
8418 off
[0] += ARCH_SIZE
/ 8;
8421 h
->got
.offset
= (bfd_vma
) -1;
8426 /* Many folk need no more in the way of final link than this, once
8427 got entry reference counting is enabled. */
8430 elf_gc_common_final_link (abfd
, info
)
8432 struct bfd_link_info
*info
;
8434 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
8437 /* Invoke the regular ELF backend linker to do all the work. */
8438 return elf_bfd_final_link (abfd
, info
);
8441 /* This function will be called though elf_link_hash_traverse to store
8442 all hash value of the exported symbols in an array. */
8445 elf_collect_hash_codes (h
, data
)
8446 struct elf_link_hash_entry
*h
;
8449 unsigned long **valuep
= (unsigned long **) data
;
8455 if (h
->root
.type
== bfd_link_hash_warning
)
8456 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8458 /* Ignore indirect symbols. These are added by the versioning code. */
8459 if (h
->dynindx
== -1)
8462 name
= h
->root
.root
.string
;
8463 p
= strchr (name
, ELF_VER_CHR
);
8466 alc
= bfd_malloc ((bfd_size_type
) (p
- name
+ 1));
8467 memcpy (alc
, name
, (size_t) (p
- name
));
8468 alc
[p
- name
] = '\0';
8472 /* Compute the hash value. */
8473 ha
= bfd_elf_hash (name
);
8475 /* Store the found hash value in the array given as the argument. */
8478 /* And store it in the struct so that we can put it in the hash table
8480 h
->elf_hash_value
= ha
;
8489 elf_reloc_symbol_deleted_p (offset
, cookie
)
8493 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
8495 if (rcookie
->bad_symtab
)
8496 rcookie
->rel
= rcookie
->rels
;
8498 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
8500 unsigned long r_symndx
;
8502 if (! rcookie
->bad_symtab
)
8503 if (rcookie
->rel
->r_offset
> offset
)
8505 if (rcookie
->rel
->r_offset
!= offset
)
8508 r_symndx
= ELF_R_SYM (rcookie
->rel
->r_info
);
8509 if (r_symndx
== SHN_UNDEF
)
8512 if (r_symndx
>= rcookie
->locsymcount
8513 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
8515 struct elf_link_hash_entry
*h
;
8517 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
8519 while (h
->root
.type
== bfd_link_hash_indirect
8520 || h
->root
.type
== bfd_link_hash_warning
)
8521 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8523 if ((h
->root
.type
== bfd_link_hash_defined
8524 || h
->root
.type
== bfd_link_hash_defweak
)
8525 && elf_discarded_section (h
->root
.u
.def
.section
))
8532 /* It's not a relocation against a global symbol,
8533 but it could be a relocation against a local
8534 symbol for a discarded section. */
8536 Elf_Internal_Sym
*isym
;
8538 /* Need to: get the symbol; get the section. */
8539 isym
= &rcookie
->locsyms
[r_symndx
];
8540 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
8542 isec
= section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
8543 if (isec
!= NULL
&& elf_discarded_section (isec
))
8552 /* Discard unneeded references to discarded sections.
8553 Returns TRUE if any section's size was changed. */
8554 /* This function assumes that the relocations are in sorted order,
8555 which is true for all known assemblers. */
8558 elf_bfd_discard_info (output_bfd
, info
)
8560 struct bfd_link_info
*info
;
8562 struct elf_reloc_cookie cookie
;
8563 asection
*stab
, *eh
;
8564 Elf_Internal_Shdr
*symtab_hdr
;
8565 struct elf_backend_data
*bed
;
8568 bfd_boolean ret
= FALSE
;
8570 if (info
->traditional_format
8571 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
8572 || ! is_elf_hash_table (info
))
8575 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
8577 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
8580 bed
= get_elf_backend_data (abfd
);
8582 if ((abfd
->flags
& DYNAMIC
) != 0)
8585 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
8586 if (info
->relocateable
8588 && (eh
->_raw_size
== 0
8589 || bfd_is_abs_section (eh
->output_section
))))
8592 stab
= bfd_get_section_by_name (abfd
, ".stab");
8594 && (stab
->_raw_size
== 0
8595 || bfd_is_abs_section (stab
->output_section
)
8596 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
8601 && bed
->elf_backend_discard_info
== NULL
)
8604 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8606 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
8607 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
8608 if (cookie
.bad_symtab
)
8610 cookie
.locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8611 cookie
.extsymoff
= 0;
8615 cookie
.locsymcount
= symtab_hdr
->sh_info
;
8616 cookie
.extsymoff
= symtab_hdr
->sh_info
;
8619 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8620 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
8622 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
8623 cookie
.locsymcount
, 0,
8625 if (cookie
.locsyms
== NULL
)
8632 count
= stab
->reloc_count
;
8634 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8635 (abfd
, stab
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
8636 info
->keep_memory
));
8637 if (cookie
.rels
!= NULL
)
8639 cookie
.rel
= cookie
.rels
;
8640 cookie
.relend
= cookie
.rels
;
8641 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
8642 if (_bfd_discard_section_stabs (abfd
, stab
,
8643 elf_section_data (stab
)->sec_info
,
8644 elf_reloc_symbol_deleted_p
,
8647 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
8655 count
= eh
->reloc_count
;
8657 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8658 (abfd
, eh
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
8659 info
->keep_memory
));
8660 cookie
.rel
= cookie
.rels
;
8661 cookie
.relend
= cookie
.rels
;
8662 if (cookie
.rels
!= NULL
)
8663 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
8665 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
8666 elf_reloc_symbol_deleted_p
,
8670 if (cookie
.rels
!= NULL
8671 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
8675 if (bed
->elf_backend_discard_info
!= NULL
8676 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
8679 if (cookie
.locsyms
!= NULL
8680 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
8682 if (! info
->keep_memory
)
8683 free (cookie
.locsyms
);
8685 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
8689 if (info
->eh_frame_hdr
8690 && !info
->relocateable
8691 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
8698 elf_section_ignore_discarded_relocs (sec
)
8701 struct elf_backend_data
*bed
;
8703 switch (sec
->sec_info_type
)
8705 case ELF_INFO_TYPE_STABS
:
8706 case ELF_INFO_TYPE_EH_FRAME
:
8712 bed
= get_elf_backend_data (sec
->owner
);
8713 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
8714 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))