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
;
492 bind
= ELF_ST_BIND (sym
->st_info
);
494 if (! bfd_is_und_section (sec
))
495 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
497 h
= ((struct elf_link_hash_entry
*)
498 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
503 /* This code is for coping with dynamic objects, and is only useful
504 if we are doing an ELF link. */
505 if (info
->hash
->creator
!= abfd
->xvec
)
508 /* For merging, we only care about real symbols. */
510 while (h
->root
.type
== bfd_link_hash_indirect
511 || h
->root
.type
== bfd_link_hash_warning
)
512 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
514 /* If we just created the symbol, mark it as being an ELF symbol.
515 Other than that, there is nothing to do--there is no merge issue
516 with a newly defined symbol--so we just return. */
518 if (h
->root
.type
== bfd_link_hash_new
)
520 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
524 /* OLDBFD is a BFD associated with the existing symbol. */
526 switch (h
->root
.type
)
532 case bfd_link_hash_undefined
:
533 case bfd_link_hash_undefweak
:
534 oldbfd
= h
->root
.u
.undef
.abfd
;
537 case bfd_link_hash_defined
:
538 case bfd_link_hash_defweak
:
539 oldbfd
= h
->root
.u
.def
.section
->owner
;
542 case bfd_link_hash_common
:
543 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
547 /* In cases involving weak versioned symbols, we may wind up trying
548 to merge a symbol with itself. Catch that here, to avoid the
549 confusion that results if we try to override a symbol with
550 itself. The additional tests catch cases like
551 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
552 dynamic object, which we do want to handle here. */
554 && ((abfd
->flags
& DYNAMIC
) == 0
555 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0))
558 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
559 respectively, is from a dynamic object. */
561 if ((abfd
->flags
& DYNAMIC
) != 0)
567 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
572 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
573 indices used by MIPS ELF. */
574 switch (h
->root
.type
)
580 case bfd_link_hash_defined
:
581 case bfd_link_hash_defweak
:
582 hsec
= h
->root
.u
.def
.section
;
585 case bfd_link_hash_common
:
586 hsec
= h
->root
.u
.c
.p
->section
;
593 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
596 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
597 respectively, appear to be a definition rather than reference. */
599 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
604 if (h
->root
.type
== bfd_link_hash_undefined
605 || h
->root
.type
== bfd_link_hash_undefweak
606 || h
->root
.type
== bfd_link_hash_common
)
611 /* We need to rememeber if a symbol has a definition in a dynamic
612 object or is weak in all dynamic objects. Internal and hidden
613 visibility will make it unavailable to dynamic objects. */
614 if (newdyn
&& (h
->elf_link_hash_flags
& ELF_LINK_DYNAMIC_DEF
) == 0)
616 if (!bfd_is_und_section (sec
))
617 h
->elf_link_hash_flags
|= ELF_LINK_DYNAMIC_DEF
;
620 /* Check if this symbol is weak in all dynamic objects. If it
621 is the first time we see it in a dynamic object, we mark
622 if it is weak. Otherwise, we clear it. */
623 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) == 0)
625 if (bind
== STB_WEAK
)
626 h
->elf_link_hash_flags
|= ELF_LINK_DYNAMIC_WEAK
;
628 else if (bind
!= STB_WEAK
)
629 h
->elf_link_hash_flags
&= ~ELF_LINK_DYNAMIC_WEAK
;
633 /* If the old symbol has non-default visibility, we ignore the new
634 definition from a dynamic object. */
636 && ELF_ST_VISIBILITY (h
->other
)
637 && !bfd_is_und_section (sec
))
640 /* Make sure this symbol is dynamic. */
641 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
642 /* FIXME: Should we check type and size for protected symbol? */
643 return _bfd_elf_link_record_dynamic_symbol (info
, h
);
646 && ELF_ST_VISIBILITY (sym
->st_other
)
647 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
649 /* If the new symbol with non-default visibility comes from a
650 relocatable file and the old definition comes from a dynamic
651 object, we remove the old definition. */
652 h
->root
.type
= bfd_link_hash_new
;
653 h
->root
.u
.undef
.abfd
= NULL
;
654 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
655 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
656 /* FIXME: Should we check type and size for protected symbol? */
662 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
663 symbol, respectively, appears to be a common symbol in a dynamic
664 object. If a symbol appears in an uninitialized section, and is
665 not weak, and is not a function, then it may be a common symbol
666 which was resolved when the dynamic object was created. We want
667 to treat such symbols specially, because they raise special
668 considerations when setting the symbol size: if the symbol
669 appears as a common symbol in a regular object, and the size in
670 the regular object is larger, we must make sure that we use the
671 larger size. This problematic case can always be avoided in C,
672 but it must be handled correctly when using Fortran shared
675 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
676 likewise for OLDDYNCOMMON and OLDDEF.
678 Note that this test is just a heuristic, and that it is quite
679 possible to have an uninitialized symbol in a shared object which
680 is really a definition, rather than a common symbol. This could
681 lead to some minor confusion when the symbol really is a common
682 symbol in some regular object. However, I think it will be
687 && (sec
->flags
& SEC_ALLOC
) != 0
688 && (sec
->flags
& SEC_LOAD
) == 0
691 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
694 newdyncommon
= FALSE
;
698 && h
->root
.type
== bfd_link_hash_defined
699 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
700 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
701 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
703 && h
->type
!= STT_FUNC
)
706 olddyncommon
= FALSE
;
708 /* It's OK to change the type if either the existing symbol or the
709 new symbol is weak unless it comes from a DT_NEEDED entry of
710 a shared object, in which case, the DT_NEEDED entry may not be
711 required at the run time. */
713 if ((! dt_needed
&& h
->root
.type
== bfd_link_hash_defweak
)
714 || h
->root
.type
== bfd_link_hash_undefweak
716 *type_change_ok
= TRUE
;
718 /* It's OK to change the size if either the existing symbol or the
719 new symbol is weak, or if the old symbol is undefined. */
722 || h
->root
.type
== bfd_link_hash_undefined
)
723 *size_change_ok
= TRUE
;
725 /* If both the old and the new symbols look like common symbols in a
726 dynamic object, set the size of the symbol to the larger of the
731 && sym
->st_size
!= h
->size
)
733 /* Since we think we have two common symbols, issue a multiple
734 common warning if desired. Note that we only warn if the
735 size is different. If the size is the same, we simply let
736 the old symbol override the new one as normally happens with
737 symbols defined in dynamic objects. */
739 if (! ((*info
->callbacks
->multiple_common
)
740 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
741 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
744 if (sym
->st_size
> h
->size
)
745 h
->size
= sym
->st_size
;
747 *size_change_ok
= TRUE
;
750 /* If we are looking at a dynamic object, and we have found a
751 definition, we need to see if the symbol was already defined by
752 some other object. If so, we want to use the existing
753 definition, and we do not want to report a multiple symbol
754 definition error; we do this by clobbering *PSEC to be
757 We treat a common symbol as a definition if the symbol in the
758 shared library is a function, since common symbols always
759 represent variables; this can cause confusion in principle, but
760 any such confusion would seem to indicate an erroneous program or
761 shared library. We also permit a common symbol in a regular
762 object to override a weak symbol in a shared object.
764 We prefer a non-weak definition in a shared library to a weak
765 definition in the executable unless it comes from a DT_NEEDED
766 entry of a shared object, in which case, the DT_NEEDED entry
767 may not be required at the run time. */
772 || (h
->root
.type
== bfd_link_hash_common
774 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)))
775 && (h
->root
.type
!= bfd_link_hash_defweak
777 || bind
== STB_WEAK
))
781 newdyncommon
= FALSE
;
783 *psec
= sec
= bfd_und_section_ptr
;
784 *size_change_ok
= TRUE
;
786 /* If we get here when the old symbol is a common symbol, then
787 we are explicitly letting it override a weak symbol or
788 function in a dynamic object, and we don't want to warn about
789 a type change. If the old symbol is a defined symbol, a type
790 change warning may still be appropriate. */
792 if (h
->root
.type
== bfd_link_hash_common
)
793 *type_change_ok
= TRUE
;
796 /* Handle the special case of an old common symbol merging with a
797 new symbol which looks like a common symbol in a shared object.
798 We change *PSEC and *PVALUE to make the new symbol look like a
799 common symbol, and let _bfd_generic_link_add_one_symbol will do
803 && h
->root
.type
== bfd_link_hash_common
)
807 newdyncommon
= FALSE
;
808 *pvalue
= sym
->st_size
;
809 *psec
= sec
= bfd_com_section_ptr
;
810 *size_change_ok
= TRUE
;
813 /* If the old symbol is from a dynamic object, and the new symbol is
814 a definition which is not from a dynamic object, then the new
815 symbol overrides the old symbol. Symbols from regular files
816 always take precedence over symbols from dynamic objects, even if
817 they are defined after the dynamic object in the link.
819 As above, we again permit a common symbol in a regular object to
820 override a definition in a shared object if the shared object
821 symbol is a function or is weak.
823 As above, we permit a non-weak definition in a shared object to
824 override a weak definition in a regular object. */
829 || (bfd_is_com_section (sec
)
830 && (h
->root
.type
== bfd_link_hash_defweak
831 || h
->type
== STT_FUNC
)))
834 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
836 || h
->root
.type
== bfd_link_hash_defweak
))
838 /* Change the hash table entry to undefined, and let
839 _bfd_generic_link_add_one_symbol do the right thing with the
842 h
->root
.type
= bfd_link_hash_undefined
;
843 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
844 *size_change_ok
= TRUE
;
847 olddyncommon
= FALSE
;
849 /* We again permit a type change when a common symbol may be
850 overriding a function. */
852 if (bfd_is_com_section (sec
))
853 *type_change_ok
= TRUE
;
855 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
858 /* This union may have been set to be non-NULL when this symbol
859 was seen in a dynamic object. We must force the union to be
860 NULL, so that it is correct for a regular symbol. */
861 h
->verinfo
.vertree
= NULL
;
864 /* Handle the special case of a new common symbol merging with an
865 old symbol that looks like it might be a common symbol defined in
866 a shared object. Note that we have already handled the case in
867 which a new common symbol should simply override the definition
868 in the shared library. */
871 && bfd_is_com_section (sec
)
874 /* It would be best if we could set the hash table entry to a
875 common symbol, but we don't know what to use for the section
877 if (! ((*info
->callbacks
->multiple_common
)
878 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
879 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
882 /* If the predumed common symbol in the dynamic object is
883 larger, pretend that the new symbol has its size. */
885 if (h
->size
> *pvalue
)
888 /* FIXME: We no longer know the alignment required by the symbol
889 in the dynamic object, so we just wind up using the one from
890 the regular object. */
893 olddyncommon
= FALSE
;
895 h
->root
.type
= bfd_link_hash_undefined
;
896 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
898 *size_change_ok
= TRUE
;
899 *type_change_ok
= TRUE
;
901 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
904 h
->verinfo
.vertree
= NULL
;
909 /* Handle the case where we had a versioned symbol in a dynamic
910 library and now find a definition in a normal object. In this
911 case, we make the versioned symbol point to the normal one. */
912 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
913 flip
->root
.type
= h
->root
.type
;
914 h
->root
.type
= bfd_link_hash_indirect
;
915 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
916 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
917 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
918 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
920 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
921 flip
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
925 /* Handle the special case of a weak definition in a regular object
926 followed by a non-weak definition in a shared object. In this
927 case, we prefer the definition in the shared object unless it
928 comes from a DT_NEEDED entry of a shared object, in which case,
929 the DT_NEEDED entry may not be required at the run time. */
932 && h
->root
.type
== bfd_link_hash_defweak
937 /* To make this work we have to frob the flags so that the rest
938 of the code does not think we are using the regular
940 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
941 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
942 else if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
943 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
944 h
->elf_link_hash_flags
&= ~ (ELF_LINK_HASH_DEF_REGULAR
945 | ELF_LINK_HASH_DEF_DYNAMIC
);
947 /* If H is the target of an indirection, we want the caller to
948 use H rather than the indirect symbol. Otherwise if we are
949 defining a new indirect symbol we will wind up attaching it
950 to the entry we are overriding. */
954 /* Handle the special case of a non-weak definition in a shared
955 object followed by a weak definition in a regular object. In
956 this case we prefer the definition in the shared object. To make
957 this work we have to tell the caller to not treat the new symbol
961 && h
->root
.type
!= bfd_link_hash_defweak
970 /* This function is called to create an indirect symbol from the
971 default for the symbol with the default version if needed. The
972 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
973 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
974 indicates if it comes from a DT_NEEDED entry of a shared object. */
977 elf_add_default_symbol (abfd
, info
, h
, name
, sym
, psec
, value
,
978 dynsym
, override
, dt_needed
)
980 struct bfd_link_info
*info
;
981 struct elf_link_hash_entry
*h
;
983 Elf_Internal_Sym
*sym
;
987 bfd_boolean override
;
988 bfd_boolean dt_needed
;
990 bfd_boolean type_change_ok
;
991 bfd_boolean size_change_ok
;
994 struct elf_link_hash_entry
*hi
;
995 struct bfd_link_hash_entry
*bh
;
996 struct elf_backend_data
*bed
;
1000 size_t len
, shortlen
;
1003 /* If this symbol has a version, and it is the default version, we
1004 create an indirect symbol from the default name to the fully
1005 decorated name. This will cause external references which do not
1006 specify a version to be bound to this version of the symbol. */
1007 p
= strchr (name
, ELF_VER_CHR
);
1008 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1013 /* We are overridden by an old defition. We need to check if we
1014 need to create the indirect symbol from the default name. */
1015 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1017 BFD_ASSERT (hi
!= NULL
);
1020 while (hi
->root
.type
== bfd_link_hash_indirect
1021 || hi
->root
.type
== bfd_link_hash_warning
)
1023 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1029 bed
= get_elf_backend_data (abfd
);
1030 collect
= bed
->collect
;
1031 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1033 shortlen
= p
- name
;
1034 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1035 if (shortname
== NULL
)
1037 memcpy (shortname
, name
, shortlen
);
1038 shortname
[shortlen
] = '\0';
1040 /* We are going to create a new symbol. Merge it with any existing
1041 symbol with this name. For the purposes of the merge, act as
1042 though we were defining the symbol we just defined, although we
1043 actually going to define an indirect symbol. */
1044 type_change_ok
= FALSE
;
1045 size_change_ok
= FALSE
;
1047 if (! elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1048 &hi
, &skip
, &override
, &type_change_ok
,
1049 &size_change_ok
, dt_needed
))
1055 if (! (_bfd_generic_link_add_one_symbol
1056 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1057 (bfd_vma
) 0, name
, FALSE
, collect
, &bh
)))
1059 hi
= (struct elf_link_hash_entry
*) bh
;
1063 /* In this case the symbol named SHORTNAME is overriding the
1064 indirect symbol we want to add. We were planning on making
1065 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1066 is the name without a version. NAME is the fully versioned
1067 name, and it is the default version.
1069 Overriding means that we already saw a definition for the
1070 symbol SHORTNAME in a regular object, and it is overriding
1071 the symbol defined in the dynamic object.
1073 When this happens, we actually want to change NAME, the
1074 symbol we just added, to refer to SHORTNAME. This will cause
1075 references to NAME in the shared object to become references
1076 to SHORTNAME in the regular object. This is what we expect
1077 when we override a function in a shared object: that the
1078 references in the shared object will be mapped to the
1079 definition in the regular object. */
1081 while (hi
->root
.type
== bfd_link_hash_indirect
1082 || hi
->root
.type
== bfd_link_hash_warning
)
1083 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1085 h
->root
.type
= bfd_link_hash_indirect
;
1086 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1087 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1089 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1090 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1091 if (hi
->elf_link_hash_flags
1092 & (ELF_LINK_HASH_REF_REGULAR
1093 | ELF_LINK_HASH_DEF_REGULAR
))
1095 if (! _bfd_elf_link_record_dynamic_symbol (info
, hi
))
1100 /* Now set HI to H, so that the following code will set the
1101 other fields correctly. */
1105 /* If there is a duplicate definition somewhere, then HI may not
1106 point to an indirect symbol. We will have reported an error to
1107 the user in that case. */
1109 if (hi
->root
.type
== bfd_link_hash_indirect
)
1111 struct elf_link_hash_entry
*ht
;
1113 /* If the symbol became indirect, then we assume that we have
1114 not seen a definition before. */
1115 BFD_ASSERT ((hi
->elf_link_hash_flags
1116 & (ELF_LINK_HASH_DEF_DYNAMIC
1117 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1119 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1120 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1122 /* See if the new flags lead us to realize that the symbol must
1129 || ((hi
->elf_link_hash_flags
1130 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1135 if ((hi
->elf_link_hash_flags
1136 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1142 /* We also need to define an indirection from the nondefault version
1145 len
= strlen (name
);
1146 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1147 if (shortname
== NULL
)
1149 memcpy (shortname
, name
, shortlen
);
1150 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1152 /* Once again, merge with any existing symbol. */
1153 type_change_ok
= FALSE
;
1154 size_change_ok
= FALSE
;
1156 if (! elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1157 &hi
, &skip
, &override
, &type_change_ok
,
1158 &size_change_ok
, dt_needed
))
1163 /* Here SHORTNAME is a versioned name, so we don't expect to see
1164 the type of override we do in the case above unless it is
1165 overridden by a versioned definiton. */
1166 if (hi
->root
.type
!= bfd_link_hash_defined
1167 && hi
->root
.type
!= bfd_link_hash_defweak
)
1168 (*_bfd_error_handler
)
1169 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1170 bfd_archive_filename (abfd
), shortname
);
1175 if (! (_bfd_generic_link_add_one_symbol
1176 (info
, abfd
, shortname
, BSF_INDIRECT
,
1177 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, FALSE
, collect
, &bh
)))
1179 hi
= (struct elf_link_hash_entry
*) bh
;
1181 /* If there is a duplicate definition somewhere, then HI may not
1182 point to an indirect symbol. We will have reported an error
1183 to the user in that case. */
1185 if (hi
->root
.type
== bfd_link_hash_indirect
)
1187 /* If the symbol became indirect, then we assume that we have
1188 not seen a definition before. */
1189 BFD_ASSERT ((hi
->elf_link_hash_flags
1190 & (ELF_LINK_HASH_DEF_DYNAMIC
1191 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1193 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1195 /* See if the new flags lead us to realize that the symbol
1202 || ((hi
->elf_link_hash_flags
1203 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1208 if ((hi
->elf_link_hash_flags
1209 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1219 /* Add symbols from an ELF object file to the linker hash table. */
1222 elf_link_add_object_symbols (abfd
, info
)
1224 struct bfd_link_info
*info
;
1226 bfd_boolean (*add_symbol_hook
)
1227 PARAMS ((bfd
*, struct bfd_link_info
*, const Elf_Internal_Sym
*,
1228 const char **, flagword
*, asection
**, bfd_vma
*));
1229 bfd_boolean (*check_relocs
)
1230 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
1231 const Elf_Internal_Rela
*));
1232 bfd_boolean collect
;
1233 Elf_Internal_Shdr
*hdr
;
1234 bfd_size_type symcount
;
1235 bfd_size_type extsymcount
;
1236 bfd_size_type extsymoff
;
1237 struct elf_link_hash_entry
**sym_hash
;
1238 bfd_boolean dynamic
;
1239 Elf_External_Versym
*extversym
= NULL
;
1240 Elf_External_Versym
*ever
;
1241 struct elf_link_hash_entry
*weaks
;
1242 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
1243 bfd_size_type nondeflt_vers_cnt
= 0;
1244 Elf_Internal_Sym
*isymbuf
= NULL
;
1245 Elf_Internal_Sym
*isym
;
1246 Elf_Internal_Sym
*isymend
;
1247 struct elf_backend_data
*bed
;
1248 bfd_boolean dt_needed
;
1249 struct elf_link_hash_table
* hash_table
;
1252 hash_table
= elf_hash_table (info
);
1254 bed
= get_elf_backend_data (abfd
);
1255 add_symbol_hook
= bed
->elf_add_symbol_hook
;
1256 collect
= bed
->collect
;
1258 if ((abfd
->flags
& DYNAMIC
) == 0)
1264 /* You can't use -r against a dynamic object. Also, there's no
1265 hope of using a dynamic object which does not exactly match
1266 the format of the output file. */
1267 if (info
->relocateable
|| info
->hash
->creator
!= abfd
->xvec
)
1269 bfd_set_error (bfd_error_invalid_operation
);
1274 /* As a GNU extension, any input sections which are named
1275 .gnu.warning.SYMBOL are treated as warning symbols for the given
1276 symbol. This differs from .gnu.warning sections, which generate
1277 warnings when they are included in an output file. */
1282 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
1286 name
= bfd_get_section_name (abfd
, s
);
1287 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1292 name
+= sizeof ".gnu.warning." - 1;
1294 /* If this is a shared object, then look up the symbol
1295 in the hash table. If it is there, and it is already
1296 been defined, then we will not be using the entry
1297 from this shared object, so we don't need to warn.
1298 FIXME: If we see the definition in a regular object
1299 later on, we will warn, but we shouldn't. The only
1300 fix is to keep track of what warnings we are supposed
1301 to emit, and then handle them all at the end of the
1303 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
1305 struct elf_link_hash_entry
*h
;
1307 h
= elf_link_hash_lookup (hash_table
, name
,
1308 FALSE
, FALSE
, TRUE
);
1310 /* FIXME: What about bfd_link_hash_common? */
1312 && (h
->root
.type
== bfd_link_hash_defined
1313 || h
->root
.type
== bfd_link_hash_defweak
))
1315 /* We don't want to issue this warning. Clobber
1316 the section size so that the warning does not
1317 get copied into the output file. */
1323 sz
= bfd_section_size (abfd
, s
);
1324 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
1328 if (! bfd_get_section_contents (abfd
, s
, msg
, (file_ptr
) 0, sz
))
1333 if (! (_bfd_generic_link_add_one_symbol
1334 (info
, abfd
, name
, BSF_WARNING
, s
, (bfd_vma
) 0, msg
,
1335 FALSE
, collect
, (struct bfd_link_hash_entry
**) NULL
)))
1338 if (! info
->relocateable
)
1340 /* Clobber the section size so that the warning does
1341 not get copied into the output file. */
1351 /* If we are creating a shared library, create all the dynamic
1352 sections immediately. We need to attach them to something,
1353 so we attach them to this BFD, provided it is the right
1354 format. FIXME: If there are no input BFD's of the same
1355 format as the output, we can't make a shared library. */
1357 && is_elf_hash_table (info
)
1358 && ! hash_table
->dynamic_sections_created
1359 && abfd
->xvec
== info
->hash
->creator
)
1361 if (! elf_link_create_dynamic_sections (abfd
, info
))
1365 else if (! is_elf_hash_table (info
))
1370 bfd_boolean add_needed
;
1372 bfd_size_type oldsize
;
1373 bfd_size_type strindex
;
1374 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
1376 /* ld --just-symbols and dynamic objects don't mix very well.
1377 Test for --just-symbols by looking at info set up by
1378 _bfd_elf_link_just_syms. */
1379 if ((s
= abfd
->sections
) != NULL
1380 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
1383 /* Find the name to use in a DT_NEEDED entry that refers to this
1384 object. If the object has a DT_SONAME entry, we use it.
1385 Otherwise, if the generic linker stuck something in
1386 elf_dt_name, we use that. Otherwise, we just use the file
1387 name. If the generic linker put a null string into
1388 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1389 there is a DT_SONAME entry. */
1391 name
= bfd_get_filename (abfd
);
1392 if (elf_dt_name (abfd
) != NULL
)
1394 name
= elf_dt_name (abfd
);
1397 if (elf_dt_soname (abfd
) != NULL
)
1403 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1406 Elf_External_Dyn
*dynbuf
= NULL
;
1407 Elf_External_Dyn
*extdyn
;
1408 Elf_External_Dyn
*extdynend
;
1410 unsigned long shlink
;
1412 dynbuf
= (Elf_External_Dyn
*) bfd_malloc (s
->_raw_size
);
1416 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
1417 (file_ptr
) 0, s
->_raw_size
))
1418 goto error_free_dyn
;
1420 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1422 goto error_free_dyn
;
1423 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1426 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
1427 for (; extdyn
< extdynend
; extdyn
++)
1429 Elf_Internal_Dyn dyn
;
1431 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
1432 if (dyn
.d_tag
== DT_SONAME
)
1434 unsigned int tagv
= dyn
.d_un
.d_val
;
1435 name
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1437 goto error_free_dyn
;
1439 if (dyn
.d_tag
== DT_NEEDED
)
1441 struct bfd_link_needed_list
*n
, **pn
;
1443 unsigned int tagv
= dyn
.d_un
.d_val
;
1445 amt
= sizeof (struct bfd_link_needed_list
);
1446 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1447 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1448 if (n
== NULL
|| fnm
== NULL
)
1449 goto error_free_dyn
;
1450 amt
= strlen (fnm
) + 1;
1451 anm
= bfd_alloc (abfd
, amt
);
1453 goto error_free_dyn
;
1454 memcpy (anm
, fnm
, (size_t) amt
);
1458 for (pn
= & hash_table
->needed
;
1464 if (dyn
.d_tag
== DT_RUNPATH
)
1466 struct bfd_link_needed_list
*n
, **pn
;
1468 unsigned int tagv
= dyn
.d_un
.d_val
;
1470 amt
= sizeof (struct bfd_link_needed_list
);
1471 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1472 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1473 if (n
== NULL
|| fnm
== NULL
)
1474 goto error_free_dyn
;
1475 amt
= strlen (fnm
) + 1;
1476 anm
= bfd_alloc (abfd
, amt
);
1478 goto error_free_dyn
;
1479 memcpy (anm
, fnm
, (size_t) amt
);
1483 for (pn
= & runpath
;
1489 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1490 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
1492 struct bfd_link_needed_list
*n
, **pn
;
1494 unsigned int tagv
= dyn
.d_un
.d_val
;
1496 amt
= sizeof (struct bfd_link_needed_list
);
1497 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1498 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1499 if (n
== NULL
|| fnm
== NULL
)
1500 goto error_free_dyn
;
1501 amt
= strlen (fnm
) + 1;
1502 anm
= bfd_alloc (abfd
, amt
);
1509 memcpy (anm
, fnm
, (size_t) amt
);
1524 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
1525 frees all more recently bfd_alloc'd blocks as well. */
1531 struct bfd_link_needed_list
**pn
;
1532 for (pn
= & hash_table
->runpath
;
1539 /* We do not want to include any of the sections in a dynamic
1540 object in the output file. We hack by simply clobbering the
1541 list of sections in the BFD. This could be handled more
1542 cleanly by, say, a new section flag; the existing
1543 SEC_NEVER_LOAD flag is not the one we want, because that one
1544 still implies that the section takes up space in the output
1546 bfd_section_list_clear (abfd
);
1548 /* If this is the first dynamic object found in the link, create
1549 the special sections required for dynamic linking. */
1550 if (! hash_table
->dynamic_sections_created
)
1551 if (! elf_link_create_dynamic_sections (abfd
, info
))
1556 /* Add a DT_NEEDED entry for this dynamic object. */
1557 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
1558 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, name
, FALSE
);
1559 if (strindex
== (bfd_size_type
) -1)
1562 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
1565 Elf_External_Dyn
*dyncon
, *dynconend
;
1567 /* The hash table size did not change, which means that
1568 the dynamic object name was already entered. If we
1569 have already included this dynamic object in the
1570 link, just ignore it. There is no reason to include
1571 a particular dynamic object more than once. */
1572 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
1573 BFD_ASSERT (sdyn
!= NULL
);
1575 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1576 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1578 for (; dyncon
< dynconend
; dyncon
++)
1580 Elf_Internal_Dyn dyn
;
1582 elf_swap_dyn_in (hash_table
->dynobj
, dyncon
, & dyn
);
1583 if (dyn
.d_tag
== DT_NEEDED
1584 && dyn
.d_un
.d_val
== strindex
)
1586 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
1592 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
1596 /* Save the SONAME, if there is one, because sometimes the
1597 linker emulation code will need to know it. */
1599 name
= basename (bfd_get_filename (abfd
));
1600 elf_dt_name (abfd
) = name
;
1603 /* If this is a dynamic object, we always link against the .dynsym
1604 symbol table, not the .symtab symbol table. The dynamic linker
1605 will only see the .dynsym symbol table, so there is no reason to
1606 look at .symtab for a dynamic object. */
1608 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
1609 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1611 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1613 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
1615 /* The sh_info field of the symtab header tells us where the
1616 external symbols start. We don't care about the local symbols at
1618 if (elf_bad_symtab (abfd
))
1620 extsymcount
= symcount
;
1625 extsymcount
= symcount
- hdr
->sh_info
;
1626 extsymoff
= hdr
->sh_info
;
1630 if (extsymcount
!= 0)
1632 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
1634 if (isymbuf
== NULL
)
1637 /* We store a pointer to the hash table entry for each external
1639 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
1640 sym_hash
= (struct elf_link_hash_entry
**) bfd_alloc (abfd
, amt
);
1641 if (sym_hash
== NULL
)
1642 goto error_free_sym
;
1643 elf_sym_hashes (abfd
) = sym_hash
;
1648 /* Read in any version definitions. */
1649 if (! _bfd_elf_slurp_version_tables (abfd
))
1650 goto error_free_sym
;
1652 /* Read in the symbol versions, but don't bother to convert them
1653 to internal format. */
1654 if (elf_dynversym (abfd
) != 0)
1656 Elf_Internal_Shdr
*versymhdr
;
1658 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
1659 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
1660 if (extversym
== NULL
)
1661 goto error_free_sym
;
1662 amt
= versymhdr
->sh_size
;
1663 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
1664 || bfd_bread ((PTR
) extversym
, amt
, abfd
) != amt
)
1665 goto error_free_vers
;
1671 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
1672 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
1674 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
1681 struct elf_link_hash_entry
*h
;
1682 bfd_boolean definition
;
1683 bfd_boolean size_change_ok
;
1684 bfd_boolean type_change_ok
;
1685 bfd_boolean new_weakdef
;
1686 bfd_boolean override
;
1687 unsigned int old_alignment
;
1692 flags
= BSF_NO_FLAGS
;
1694 value
= isym
->st_value
;
1697 bind
= ELF_ST_BIND (isym
->st_info
);
1698 if (bind
== STB_LOCAL
)
1700 /* This should be impossible, since ELF requires that all
1701 global symbols follow all local symbols, and that sh_info
1702 point to the first global symbol. Unfortunatealy, Irix 5
1706 else if (bind
== STB_GLOBAL
)
1708 if (isym
->st_shndx
!= SHN_UNDEF
1709 && isym
->st_shndx
!= SHN_COMMON
)
1712 else if (bind
== STB_WEAK
)
1716 /* Leave it up to the processor backend. */
1719 if (isym
->st_shndx
== SHN_UNDEF
)
1720 sec
= bfd_und_section_ptr
;
1721 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
1723 sec
= section_from_elf_index (abfd
, isym
->st_shndx
);
1725 sec
= bfd_abs_section_ptr
;
1726 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
1729 else if (isym
->st_shndx
== SHN_ABS
)
1730 sec
= bfd_abs_section_ptr
;
1731 else if (isym
->st_shndx
== SHN_COMMON
)
1733 sec
= bfd_com_section_ptr
;
1734 /* What ELF calls the size we call the value. What ELF
1735 calls the value we call the alignment. */
1736 value
= isym
->st_size
;
1740 /* Leave it up to the processor backend. */
1743 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
1745 if (name
== (const char *) NULL
)
1746 goto error_free_vers
;
1748 if (isym
->st_shndx
== SHN_COMMON
1749 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
1751 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
1755 tcomm
= bfd_make_section (abfd
, ".tcommon");
1757 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
1759 | SEC_LINKER_CREATED
1760 | SEC_THREAD_LOCAL
)))
1761 goto error_free_vers
;
1765 else if (add_symbol_hook
)
1767 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
1769 goto error_free_vers
;
1771 /* The hook function sets the name to NULL if this symbol
1772 should be skipped for some reason. */
1773 if (name
== (const char *) NULL
)
1777 /* Sanity check that all possibilities were handled. */
1778 if (sec
== (asection
*) NULL
)
1780 bfd_set_error (bfd_error_bad_value
);
1781 goto error_free_vers
;
1784 if (bfd_is_und_section (sec
)
1785 || bfd_is_com_section (sec
))
1790 size_change_ok
= FALSE
;
1791 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
1795 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1797 Elf_Internal_Versym iver
;
1798 unsigned int vernum
= 0;
1803 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
1804 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
1806 /* If this is a hidden symbol, or if it is not version
1807 1, we append the version name to the symbol name.
1808 However, we do not modify a non-hidden absolute
1809 symbol, because it might be the version symbol
1810 itself. FIXME: What if it isn't? */
1811 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
1812 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
1815 size_t namelen
, verlen
, newlen
;
1818 if (isym
->st_shndx
!= SHN_UNDEF
)
1820 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
1822 (*_bfd_error_handler
)
1823 (_("%s: %s: invalid version %u (max %d)"),
1824 bfd_archive_filename (abfd
), name
, vernum
,
1825 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1826 bfd_set_error (bfd_error_bad_value
);
1827 goto error_free_vers
;
1829 else if (vernum
> 1)
1831 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1837 /* We cannot simply test for the number of
1838 entries in the VERNEED section since the
1839 numbers for the needed versions do not start
1841 Elf_Internal_Verneed
*t
;
1844 for (t
= elf_tdata (abfd
)->verref
;
1848 Elf_Internal_Vernaux
*a
;
1850 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1852 if (a
->vna_other
== vernum
)
1854 verstr
= a
->vna_nodename
;
1863 (*_bfd_error_handler
)
1864 (_("%s: %s: invalid needed version %d"),
1865 bfd_archive_filename (abfd
), name
, vernum
);
1866 bfd_set_error (bfd_error_bad_value
);
1867 goto error_free_vers
;
1871 namelen
= strlen (name
);
1872 verlen
= strlen (verstr
);
1873 newlen
= namelen
+ verlen
+ 2;
1874 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1875 && isym
->st_shndx
!= SHN_UNDEF
)
1878 newname
= (char *) bfd_alloc (abfd
, (bfd_size_type
) newlen
);
1879 if (newname
== NULL
)
1880 goto error_free_vers
;
1881 memcpy (newname
, name
, namelen
);
1882 p
= newname
+ namelen
;
1884 /* If this is a defined non-hidden version symbol,
1885 we add another @ to the name. This indicates the
1886 default version of the symbol. */
1887 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1888 && isym
->st_shndx
!= SHN_UNDEF
)
1890 memcpy (p
, verstr
, verlen
+ 1);
1896 if (! elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
1897 sym_hash
, &skip
, &override
,
1898 &type_change_ok
, &size_change_ok
,
1900 goto error_free_vers
;
1909 while (h
->root
.type
== bfd_link_hash_indirect
1910 || h
->root
.type
== bfd_link_hash_warning
)
1911 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1913 /* Remember the old alignment if this is a common symbol, so
1914 that we don't reduce the alignment later on. We can't
1915 check later, because _bfd_generic_link_add_one_symbol
1916 will set a default for the alignment which we want to
1917 override. We also remember the old bfd where the existing
1918 definition comes from. */
1919 switch (h
->root
.type
)
1924 case bfd_link_hash_defined
:
1925 case bfd_link_hash_defweak
:
1926 old_bfd
= h
->root
.u
.def
.section
->owner
;
1929 case bfd_link_hash_common
:
1930 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
1931 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1935 if (elf_tdata (abfd
)->verdef
!= NULL
1939 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1942 if (! (_bfd_generic_link_add_one_symbol
1943 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
1944 FALSE
, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
1945 goto error_free_vers
;
1948 while (h
->root
.type
== bfd_link_hash_indirect
1949 || h
->root
.type
== bfd_link_hash_warning
)
1950 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1953 new_weakdef
= FALSE
;
1956 && (flags
& BSF_WEAK
) != 0
1957 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
1958 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1959 && h
->weakdef
== NULL
)
1961 /* Keep a list of all weak defined non function symbols from
1962 a dynamic object, using the weakdef field. Later in this
1963 function we will set the weakdef field to the correct
1964 value. We only put non-function symbols from dynamic
1965 objects on this list, because that happens to be the only
1966 time we need to know the normal symbol corresponding to a
1967 weak symbol, and the information is time consuming to
1968 figure out. If the weakdef field is not already NULL,
1969 then this symbol was already defined by some previous
1970 dynamic object, and we will be using that previous
1971 definition anyhow. */
1978 /* Set the alignment of a common symbol. */
1979 if (isym
->st_shndx
== SHN_COMMON
1980 && h
->root
.type
== bfd_link_hash_common
)
1984 align
= bfd_log2 (isym
->st_value
);
1985 if (align
> old_alignment
1986 /* Permit an alignment power of zero if an alignment of one
1987 is specified and no other alignments have been specified. */
1988 || (isym
->st_value
== 1 && old_alignment
== 0))
1989 h
->root
.u
.c
.p
->alignment_power
= align
;
1991 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
1994 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
2000 /* Check the alignment when a common symbol is involved. This
2001 can change when a common symbol is overriden by a normal
2002 definition or a common symbol is ignored due to the old
2003 normal definition. We need to make sure the maximum
2004 alignment is maintained. */
2005 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
2006 && h
->root
.type
!= bfd_link_hash_common
)
2008 unsigned int common_align
;
2009 unsigned int normal_align
;
2010 unsigned int symbol_align
;
2014 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
2015 if ((h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2017 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
2018 if (normal_align
> symbol_align
)
2019 normal_align
= symbol_align
;
2022 normal_align
= symbol_align
;
2026 common_align
= old_alignment
;
2027 common_bfd
= old_bfd
;
2032 common_align
= bfd_log2 (isym
->st_value
);
2034 normal_bfd
= old_bfd
;
2037 if (normal_align
< common_align
)
2038 (*_bfd_error_handler
)
2039 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
2042 bfd_archive_filename (normal_bfd
),
2044 bfd_archive_filename (common_bfd
));
2047 /* Remember the symbol size and type. */
2048 if (isym
->st_size
!= 0
2049 && (definition
|| h
->size
== 0))
2051 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
2052 (*_bfd_error_handler
)
2053 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
2054 name
, (unsigned long) h
->size
,
2055 bfd_archive_filename (old_bfd
),
2056 (unsigned long) isym
->st_size
,
2057 bfd_archive_filename (abfd
));
2059 h
->size
= isym
->st_size
;
2062 /* If this is a common symbol, then we always want H->SIZE
2063 to be the size of the common symbol. The code just above
2064 won't fix the size if a common symbol becomes larger. We
2065 don't warn about a size change here, because that is
2066 covered by --warn-common. */
2067 if (h
->root
.type
== bfd_link_hash_common
)
2068 h
->size
= h
->root
.u
.c
.size
;
2070 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
2071 && (definition
|| h
->type
== STT_NOTYPE
))
2073 if (h
->type
!= STT_NOTYPE
2074 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
2075 && ! type_change_ok
)
2076 (*_bfd_error_handler
)
2077 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
2078 name
, h
->type
, ELF_ST_TYPE (isym
->st_info
),
2079 bfd_archive_filename (abfd
));
2081 h
->type
= ELF_ST_TYPE (isym
->st_info
);
2084 /* If st_other has a processor-specific meaning, specific
2085 code might be needed here. We never merge the visibility
2086 attribute with the one from a dynamic object. */
2087 if (isym
->st_other
!= 0 && !dynamic
)
2089 unsigned char hvis
, symvis
, other
, nvis
;
2091 /* Take the balance of OTHER from the definition. */
2092 other
= (definition
? isym
->st_other
: h
->other
);
2093 other
&= ~ ELF_ST_VISIBILITY (-1);
2095 /* Combine visibilities, using the most constraining one. */
2096 hvis
= ELF_ST_VISIBILITY (h
->other
);
2097 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
2103 nvis
= hvis
< symvis
? hvis
: symvis
;
2105 h
->other
= other
| nvis
;
2108 /* Set a flag in the hash table entry indicating the type of
2109 reference or definition we just found. Keep a count of
2110 the number of dynamic symbols we find. A dynamic symbol
2111 is one which is referenced or defined by both a regular
2112 object and a shared object. */
2113 old_flags
= h
->elf_link_hash_flags
;
2119 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
2120 if (bind
!= STB_WEAK
)
2121 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
2124 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
2126 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2127 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
2133 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
2135 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
2136 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
2137 | ELF_LINK_HASH_REF_REGULAR
)) != 0
2138 || (h
->weakdef
!= NULL
2140 && h
->weakdef
->dynindx
!= -1))
2144 h
->elf_link_hash_flags
|= new_flag
;
2146 /* Check to see if we need to add an indirect symbol for
2147 the default name. */
2148 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
2149 if (! elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
2150 &sec
, &value
, &dynsym
,
2151 override
, dt_needed
))
2152 goto error_free_vers
;
2154 if (definition
&& !dynamic
)
2156 char *p
= strchr (name
, ELF_VER_CHR
);
2157 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
2159 /* Queue non-default versions so that .symver x, x@FOO
2160 aliases can be checked. */
2161 if (! nondeflt_vers
)
2163 amt
= (isymend
- isym
+ 1)
2164 * sizeof (struct elf_link_hash_entry
*);
2165 nondeflt_vers
= bfd_malloc (amt
);
2167 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
2171 if (dynsym
&& h
->dynindx
== -1)
2173 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2174 goto error_free_vers
;
2175 if (h
->weakdef
!= NULL
2177 && h
->weakdef
->dynindx
== -1)
2179 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2180 goto error_free_vers
;
2183 else if (dynsym
&& h
->dynindx
!= -1)
2184 /* If the symbol already has a dynamic index, but
2185 visibility says it should not be visible, turn it into
2187 switch (ELF_ST_VISIBILITY (h
->other
))
2191 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2195 if (dt_needed
&& definition
2196 && (h
->elf_link_hash_flags
2197 & ELF_LINK_HASH_REF_REGULAR
) != 0)
2199 bfd_size_type oldsize
;
2200 bfd_size_type strindex
;
2202 if (! is_elf_hash_table (info
))
2203 goto error_free_vers
;
2205 /* The symbol from a DT_NEEDED object is referenced from
2206 the regular object to create a dynamic executable. We
2207 have to make sure there is a DT_NEEDED entry for it. */
2210 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2211 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
,
2212 elf_dt_soname (abfd
), FALSE
);
2213 if (strindex
== (bfd_size_type
) -1)
2214 goto error_free_vers
;
2216 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2219 Elf_External_Dyn
*dyncon
, *dynconend
;
2221 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
,
2223 BFD_ASSERT (sdyn
!= NULL
);
2225 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
2226 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
2228 for (; dyncon
< dynconend
; dyncon
++)
2230 Elf_Internal_Dyn dyn
;
2232 elf_swap_dyn_in (hash_table
->dynobj
,
2234 BFD_ASSERT (dyn
.d_tag
!= DT_NEEDED
||
2235 dyn
.d_un
.d_val
!= strindex
);
2239 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
2240 goto error_free_vers
;
2245 /* Now that all the symbols from this input file are created, handle
2246 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
2247 if (nondeflt_vers
!= NULL
)
2249 bfd_size_type cnt
, symidx
;
2251 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
2253 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
2254 char *shortname
, *p
;
2256 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2258 || (h
->root
.type
!= bfd_link_hash_defined
2259 && h
->root
.type
!= bfd_link_hash_defweak
))
2262 amt
= p
- h
->root
.root
.string
;
2263 shortname
= bfd_malloc (amt
+ 1);
2264 memcpy (shortname
, h
->root
.root
.string
, amt
);
2265 shortname
[amt
] = '\0';
2267 hi
= (struct elf_link_hash_entry
*)
2268 bfd_link_hash_lookup (info
->hash
, shortname
,
2269 FALSE
, FALSE
, FALSE
);
2271 && hi
->root
.type
== h
->root
.type
2272 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
2273 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
2275 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
2276 hi
->root
.type
= bfd_link_hash_indirect
;
2277 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
2278 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
2279 sym_hash
= elf_sym_hashes (abfd
);
2281 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
2282 if (sym_hash
[symidx
] == hi
)
2284 sym_hash
[symidx
] = h
;
2290 free (nondeflt_vers
);
2291 nondeflt_vers
= NULL
;
2294 if (extversym
!= NULL
)
2300 if (isymbuf
!= NULL
)
2304 /* Now set the weakdefs field correctly for all the weak defined
2305 symbols we found. The only way to do this is to search all the
2306 symbols. Since we only need the information for non functions in
2307 dynamic objects, that's the only time we actually put anything on
2308 the list WEAKS. We need this information so that if a regular
2309 object refers to a symbol defined weakly in a dynamic object, the
2310 real symbol in the dynamic object is also put in the dynamic
2311 symbols; we also must arrange for both symbols to point to the
2312 same memory location. We could handle the general case of symbol
2313 aliasing, but a general symbol alias can only be generated in
2314 assembler code, handling it correctly would be very time
2315 consuming, and other ELF linkers don't handle general aliasing
2317 while (weaks
!= NULL
)
2319 struct elf_link_hash_entry
*hlook
;
2322 struct elf_link_hash_entry
**hpp
;
2323 struct elf_link_hash_entry
**hppend
;
2326 weaks
= hlook
->weakdef
;
2327 hlook
->weakdef
= NULL
;
2329 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
2330 || hlook
->root
.type
== bfd_link_hash_defweak
2331 || hlook
->root
.type
== bfd_link_hash_common
2332 || hlook
->root
.type
== bfd_link_hash_indirect
);
2333 slook
= hlook
->root
.u
.def
.section
;
2334 vlook
= hlook
->root
.u
.def
.value
;
2336 hpp
= elf_sym_hashes (abfd
);
2337 hppend
= hpp
+ extsymcount
;
2338 for (; hpp
< hppend
; hpp
++)
2340 struct elf_link_hash_entry
*h
;
2343 if (h
!= NULL
&& h
!= hlook
2344 && h
->root
.type
== bfd_link_hash_defined
2345 && h
->root
.u
.def
.section
== slook
2346 && h
->root
.u
.def
.value
== vlook
)
2350 /* If the weak definition is in the list of dynamic
2351 symbols, make sure the real definition is put there
2353 if (hlook
->dynindx
!= -1
2354 && h
->dynindx
== -1)
2356 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2360 /* If the real definition is in the list of dynamic
2361 symbols, make sure the weak definition is put there
2362 as well. If we don't do this, then the dynamic
2363 loader might not merge the entries for the real
2364 definition and the weak definition. */
2365 if (h
->dynindx
!= -1
2366 && hlook
->dynindx
== -1)
2368 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
2376 /* If this object is the same format as the output object, and it is
2377 not a shared library, then let the backend look through the
2380 This is required to build global offset table entries and to
2381 arrange for dynamic relocs. It is not required for the
2382 particular common case of linking non PIC code, even when linking
2383 against shared libraries, but unfortunately there is no way of
2384 knowing whether an object file has been compiled PIC or not.
2385 Looking through the relocs is not particularly time consuming.
2386 The problem is that we must either (1) keep the relocs in memory,
2387 which causes the linker to require additional runtime memory or
2388 (2) read the relocs twice from the input file, which wastes time.
2389 This would be a good case for using mmap.
2391 I have no idea how to handle linking PIC code into a file of a
2392 different format. It probably can't be done. */
2393 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
2395 && abfd
->xvec
== info
->hash
->creator
2396 && check_relocs
!= NULL
)
2400 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2402 Elf_Internal_Rela
*internal_relocs
;
2405 if ((o
->flags
& SEC_RELOC
) == 0
2406 || o
->reloc_count
== 0
2407 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
2408 && (o
->flags
& SEC_DEBUGGING
) != 0)
2409 || bfd_is_abs_section (o
->output_section
))
2412 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
2413 (abfd
, o
, (PTR
) NULL
,
2414 (Elf_Internal_Rela
*) NULL
,
2415 info
->keep_memory
));
2416 if (internal_relocs
== NULL
)
2419 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
2421 if (elf_section_data (o
)->relocs
!= internal_relocs
)
2422 free (internal_relocs
);
2429 /* If this is a non-traditional link, try to optimize the handling
2430 of the .stab/.stabstr sections. */
2432 && ! info
->traditional_format
2433 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
2434 && is_elf_hash_table (info
)
2435 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
2437 asection
*stab
, *stabstr
;
2439 stab
= bfd_get_section_by_name (abfd
, ".stab");
2441 && (stab
->flags
& SEC_MERGE
) == 0
2442 && !bfd_is_abs_section (stab
->output_section
))
2444 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
2446 if (stabstr
!= NULL
)
2448 struct bfd_elf_section_data
*secdata
;
2450 secdata
= elf_section_data (stab
);
2451 if (! _bfd_link_section_stabs (abfd
,
2452 & hash_table
->stab_info
,
2454 &secdata
->sec_info
))
2456 if (secdata
->sec_info
)
2457 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
2462 if (! info
->relocateable
&& ! dynamic
2463 && is_elf_hash_table (info
))
2467 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2468 if ((s
->flags
& SEC_MERGE
) != 0
2469 && !bfd_is_abs_section (s
->output_section
))
2471 struct bfd_elf_section_data
*secdata
;
2473 secdata
= elf_section_data (s
);
2474 if (! _bfd_merge_section (abfd
,
2475 & hash_table
->merge_info
,
2476 s
, &secdata
->sec_info
))
2478 else if (secdata
->sec_info
)
2479 s
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
2483 if (is_elf_hash_table (info
))
2485 /* Add this bfd to the loaded list. */
2486 struct elf_link_loaded_list
*n
;
2488 n
= ((struct elf_link_loaded_list
*)
2489 bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
)));
2493 n
->next
= hash_table
->loaded
;
2494 hash_table
->loaded
= n
;
2500 if (nondeflt_vers
!= NULL
)
2501 free (nondeflt_vers
);
2502 if (extversym
!= NULL
)
2505 if (isymbuf
!= NULL
)
2511 /* Create some sections which will be filled in with dynamic linking
2512 information. ABFD is an input file which requires dynamic sections
2513 to be created. The dynamic sections take up virtual memory space
2514 when the final executable is run, so we need to create them before
2515 addresses are assigned to the output sections. We work out the
2516 actual contents and size of these sections later. */
2519 elf_link_create_dynamic_sections (abfd
, info
)
2521 struct bfd_link_info
*info
;
2524 register asection
*s
;
2525 struct elf_link_hash_entry
*h
;
2526 struct bfd_link_hash_entry
*bh
;
2527 struct elf_backend_data
*bed
;
2529 if (! is_elf_hash_table (info
))
2532 if (elf_hash_table (info
)->dynamic_sections_created
)
2535 /* Make sure that all dynamic sections use the same input BFD. */
2536 if (elf_hash_table (info
)->dynobj
== NULL
)
2537 elf_hash_table (info
)->dynobj
= abfd
;
2539 abfd
= elf_hash_table (info
)->dynobj
;
2541 /* Note that we set the SEC_IN_MEMORY flag for all of these
2543 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
2544 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
2546 /* A dynamically linked executable has a .interp section, but a
2547 shared library does not. */
2550 s
= bfd_make_section (abfd
, ".interp");
2552 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2556 if (! info
->traditional_format
2557 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
2559 s
= bfd_make_section (abfd
, ".eh_frame_hdr");
2561 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2562 || ! bfd_set_section_alignment (abfd
, s
, 2))
2564 elf_hash_table (info
)->eh_info
.hdr_sec
= s
;
2567 /* Create sections to hold version informations. These are removed
2568 if they are not needed. */
2569 s
= bfd_make_section (abfd
, ".gnu.version_d");
2571 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2572 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2575 s
= bfd_make_section (abfd
, ".gnu.version");
2577 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2578 || ! bfd_set_section_alignment (abfd
, s
, 1))
2581 s
= bfd_make_section (abfd
, ".gnu.version_r");
2583 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2584 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2587 s
= bfd_make_section (abfd
, ".dynsym");
2589 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2590 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2593 s
= bfd_make_section (abfd
, ".dynstr");
2595 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2598 /* Create a strtab to hold the dynamic symbol names. */
2599 if (elf_hash_table (info
)->dynstr
== NULL
)
2601 elf_hash_table (info
)->dynstr
= _bfd_elf_strtab_init ();
2602 if (elf_hash_table (info
)->dynstr
== NULL
)
2606 s
= bfd_make_section (abfd
, ".dynamic");
2608 || ! bfd_set_section_flags (abfd
, s
, flags
)
2609 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2612 /* The special symbol _DYNAMIC is always set to the start of the
2613 .dynamic section. This call occurs before we have processed the
2614 symbols for any dynamic object, so we don't have to worry about
2615 overriding a dynamic definition. We could set _DYNAMIC in a
2616 linker script, but we only want to define it if we are, in fact,
2617 creating a .dynamic section. We don't want to define it if there
2618 is no .dynamic section, since on some ELF platforms the start up
2619 code examines it to decide how to initialize the process. */
2621 if (! (_bfd_generic_link_add_one_symbol
2622 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
2623 (const char *) 0, FALSE
, get_elf_backend_data (abfd
)->collect
, &bh
)))
2625 h
= (struct elf_link_hash_entry
*) bh
;
2626 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2627 h
->type
= STT_OBJECT
;
2630 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2633 bed
= get_elf_backend_data (abfd
);
2635 s
= bfd_make_section (abfd
, ".hash");
2637 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2638 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2640 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
2642 /* Let the backend create the rest of the sections. This lets the
2643 backend set the right flags. The backend will normally create
2644 the .got and .plt sections. */
2645 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
2648 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
2653 /* Add an entry to the .dynamic table. */
2656 elf_add_dynamic_entry (info
, tag
, val
)
2657 struct bfd_link_info
*info
;
2661 Elf_Internal_Dyn dyn
;
2664 bfd_size_type newsize
;
2665 bfd_byte
*newcontents
;
2667 if (! is_elf_hash_table (info
))
2670 dynobj
= elf_hash_table (info
)->dynobj
;
2672 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
2673 BFD_ASSERT (s
!= NULL
);
2675 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
2676 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
2677 if (newcontents
== NULL
)
2681 dyn
.d_un
.d_val
= val
;
2682 elf_swap_dyn_out (dynobj
, &dyn
,
2683 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
2685 s
->_raw_size
= newsize
;
2686 s
->contents
= newcontents
;
2691 /* Read and swap the relocs from the section indicated by SHDR. This
2692 may be either a REL or a RELA section. The relocations are
2693 translated into RELA relocations and stored in INTERNAL_RELOCS,
2694 which should have already been allocated to contain enough space.
2695 The EXTERNAL_RELOCS are a buffer where the external form of the
2696 relocations should be stored.
2698 Returns FALSE if something goes wrong. */
2701 elf_link_read_relocs_from_section (abfd
, shdr
, external_relocs
,
2704 Elf_Internal_Shdr
*shdr
;
2705 PTR external_relocs
;
2706 Elf_Internal_Rela
*internal_relocs
;
2708 struct elf_backend_data
*bed
;
2709 void (*swap_in
) PARAMS ((bfd
*, const bfd_byte
*, Elf_Internal_Rela
*));
2710 const bfd_byte
*erela
;
2711 const bfd_byte
*erelaend
;
2712 Elf_Internal_Rela
*irela
;
2714 /* If there aren't any relocations, that's OK. */
2718 /* Position ourselves at the start of the section. */
2719 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2722 /* Read the relocations. */
2723 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2726 bed
= get_elf_backend_data (abfd
);
2728 /* Convert the external relocations to the internal format. */
2729 if (shdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2730 swap_in
= bed
->s
->swap_reloc_in
;
2731 else if (shdr
->sh_entsize
== sizeof (Elf_External_Rela
))
2732 swap_in
= bed
->s
->swap_reloca_in
;
2735 bfd_set_error (bfd_error_wrong_format
);
2739 erela
= external_relocs
;
2740 erelaend
= erela
+ NUM_SHDR_ENTRIES (shdr
) * shdr
->sh_entsize
;
2741 irela
= internal_relocs
;
2742 while (erela
< erelaend
)
2744 (*swap_in
) (abfd
, erela
, irela
);
2745 irela
+= bed
->s
->int_rels_per_ext_rel
;
2746 erela
+= shdr
->sh_entsize
;
2752 /* Read and swap the relocs for a section O. They may have been
2753 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2754 not NULL, they are used as buffers to read into. They are known to
2755 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2756 the return value is allocated using either malloc or bfd_alloc,
2757 according to the KEEP_MEMORY argument. If O has two relocation
2758 sections (both REL and RELA relocations), then the REL_HDR
2759 relocations will appear first in INTERNAL_RELOCS, followed by the
2760 REL_HDR2 relocations. */
2763 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
2767 PTR external_relocs
;
2768 Elf_Internal_Rela
*internal_relocs
;
2769 bfd_boolean keep_memory
;
2771 Elf_Internal_Shdr
*rel_hdr
;
2773 Elf_Internal_Rela
*alloc2
= NULL
;
2774 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2776 if (elf_section_data (o
)->relocs
!= NULL
)
2777 return elf_section_data (o
)->relocs
;
2779 if (o
->reloc_count
== 0)
2782 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2784 if (internal_relocs
== NULL
)
2788 size
= o
->reloc_count
;
2789 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2791 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2793 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2794 if (internal_relocs
== NULL
)
2798 if (external_relocs
== NULL
)
2800 bfd_size_type size
= rel_hdr
->sh_size
;
2802 if (elf_section_data (o
)->rel_hdr2
)
2803 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2804 alloc1
= (PTR
) bfd_malloc (size
);
2807 external_relocs
= alloc1
;
2810 if (!elf_link_read_relocs_from_section (abfd
, rel_hdr
,
2814 if (!elf_link_read_relocs_from_section
2816 elf_section_data (o
)->rel_hdr2
,
2817 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2818 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2819 * bed
->s
->int_rels_per_ext_rel
)))
2822 /* Cache the results for next time, if we can. */
2824 elf_section_data (o
)->relocs
= internal_relocs
;
2829 /* Don't free alloc2, since if it was allocated we are passing it
2830 back (under the name of internal_relocs). */
2832 return internal_relocs
;
2842 /* Record an assignment to a symbol made by a linker script. We need
2843 this in case some dynamic object refers to this symbol. */
2846 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
2847 bfd
*output_bfd ATTRIBUTE_UNUSED
;
2848 struct bfd_link_info
*info
;
2850 bfd_boolean provide
;
2852 struct elf_link_hash_entry
*h
;
2854 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2857 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, TRUE
, FALSE
);
2861 if (h
->root
.type
== bfd_link_hash_new
)
2862 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_ELF
;
2864 /* If this symbol is being provided by the linker script, and it is
2865 currently defined by a dynamic object, but not by a regular
2866 object, then mark it as undefined so that the generic linker will
2867 force the correct value. */
2869 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2870 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2871 h
->root
.type
= bfd_link_hash_undefined
;
2873 /* If this symbol is not being provided by the linker script, and it is
2874 currently defined by a dynamic object, but not by a regular object,
2875 then clear out any version information because the symbol will not be
2876 associated with the dynamic object any more. */
2878 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2879 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2880 h
->verinfo
.verdef
= NULL
;
2882 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2884 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2885 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
2887 && h
->dynindx
== -1)
2889 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2892 /* If this is a weak defined symbol, and we know a corresponding
2893 real symbol from the same dynamic object, make sure the real
2894 symbol is also made into a dynamic symbol. */
2895 if (h
->weakdef
!= NULL
2896 && h
->weakdef
->dynindx
== -1)
2898 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2906 /* This structure is used to pass information to
2907 elf_link_assign_sym_version. */
2909 struct elf_assign_sym_version_info
2913 /* General link information. */
2914 struct bfd_link_info
*info
;
2916 struct bfd_elf_version_tree
*verdefs
;
2917 /* Whether we had a failure. */
2921 /* This structure is used to pass information to
2922 elf_link_find_version_dependencies. */
2924 struct elf_find_verdep_info
2928 /* General link information. */
2929 struct bfd_link_info
*info
;
2930 /* The number of dependencies. */
2932 /* Whether we had a failure. */
2936 /* Array used to determine the number of hash table buckets to use
2937 based on the number of symbols there are. If there are fewer than
2938 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2939 fewer than 37 we use 17 buckets, and so forth. We never use more
2940 than 32771 buckets. */
2942 static const size_t elf_buckets
[] =
2944 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2948 /* Compute bucket count for hashing table. We do not use a static set
2949 of possible tables sizes anymore. Instead we determine for all
2950 possible reasonable sizes of the table the outcome (i.e., the
2951 number of collisions etc) and choose the best solution. The
2952 weighting functions are not too simple to allow the table to grow
2953 without bounds. Instead one of the weighting factors is the size.
2954 Therefore the result is always a good payoff between few collisions
2955 (= short chain lengths) and table size. */
2957 compute_bucket_count (info
)
2958 struct bfd_link_info
*info
;
2960 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2961 size_t best_size
= 0;
2962 unsigned long int *hashcodes
;
2963 unsigned long int *hashcodesp
;
2964 unsigned long int i
;
2967 /* Compute the hash values for all exported symbols. At the same
2968 time store the values in an array so that we could use them for
2971 amt
*= sizeof (unsigned long int);
2972 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
2973 if (hashcodes
== NULL
)
2975 hashcodesp
= hashcodes
;
2977 /* Put all hash values in HASHCODES. */
2978 elf_link_hash_traverse (elf_hash_table (info
),
2979 elf_collect_hash_codes
, &hashcodesp
);
2981 /* We have a problem here. The following code to optimize the table
2982 size requires an integer type with more the 32 bits. If
2983 BFD_HOST_U_64_BIT is set we know about such a type. */
2984 #ifdef BFD_HOST_U_64_BIT
2987 unsigned long int nsyms
= hashcodesp
- hashcodes
;
2990 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
2991 unsigned long int *counts
;
2993 /* Possible optimization parameters: if we have NSYMS symbols we say
2994 that the hashing table must at least have NSYMS/4 and at most
2996 minsize
= nsyms
/ 4;
2999 best_size
= maxsize
= nsyms
* 2;
3001 /* Create array where we count the collisions in. We must use bfd_malloc
3002 since the size could be large. */
3004 amt
*= sizeof (unsigned long int);
3005 counts
= (unsigned long int *) bfd_malloc (amt
);
3012 /* Compute the "optimal" size for the hash table. The criteria is a
3013 minimal chain length. The minor criteria is (of course) the size
3015 for (i
= minsize
; i
< maxsize
; ++i
)
3017 /* Walk through the array of hashcodes and count the collisions. */
3018 BFD_HOST_U_64_BIT max
;
3019 unsigned long int j
;
3020 unsigned long int fact
;
3022 memset (counts
, '\0', i
* sizeof (unsigned long int));
3024 /* Determine how often each hash bucket is used. */
3025 for (j
= 0; j
< nsyms
; ++j
)
3026 ++counts
[hashcodes
[j
] % i
];
3028 /* For the weight function we need some information about the
3029 pagesize on the target. This is information need not be 100%
3030 accurate. Since this information is not available (so far) we
3031 define it here to a reasonable default value. If it is crucial
3032 to have a better value some day simply define this value. */
3033 # ifndef BFD_TARGET_PAGESIZE
3034 # define BFD_TARGET_PAGESIZE (4096)
3037 /* We in any case need 2 + NSYMS entries for the size values and
3039 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
3042 /* Variant 1: optimize for short chains. We add the squares
3043 of all the chain lengths (which favous many small chain
3044 over a few long chains). */
3045 for (j
= 0; j
< i
; ++j
)
3046 max
+= counts
[j
] * counts
[j
];
3048 /* This adds penalties for the overall size of the table. */
3049 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
3052 /* Variant 2: Optimize a lot more for small table. Here we
3053 also add squares of the size but we also add penalties for
3054 empty slots (the +1 term). */
3055 for (j
= 0; j
< i
; ++j
)
3056 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
3058 /* The overall size of the table is considered, but not as
3059 strong as in variant 1, where it is squared. */
3060 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
3064 /* Compare with current best results. */
3065 if (max
< best_chlen
)
3075 #endif /* defined (BFD_HOST_U_64_BIT) */
3077 /* This is the fallback solution if no 64bit type is available or if we
3078 are not supposed to spend much time on optimizations. We select the
3079 bucket count using a fixed set of numbers. */
3080 for (i
= 0; elf_buckets
[i
] != 0; i
++)
3082 best_size
= elf_buckets
[i
];
3083 if (dynsymcount
< elf_buckets
[i
+ 1])
3088 /* Free the arrays we needed. */
3094 /* Set up the sizes and contents of the ELF dynamic sections. This is
3095 called by the ELF linker emulation before_allocation routine. We
3096 must set the sizes of the sections before the linker sets the
3097 addresses of the various sections. */
3100 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
3102 auxiliary_filters
, info
, sinterpptr
,
3107 const char *filter_shlib
;
3108 const char * const *auxiliary_filters
;
3109 struct bfd_link_info
*info
;
3110 asection
**sinterpptr
;
3111 struct bfd_elf_version_tree
*verdefs
;
3113 bfd_size_type soname_indx
;
3115 struct elf_backend_data
*bed
;
3116 struct elf_assign_sym_version_info asvinfo
;
3120 soname_indx
= (bfd_size_type
) -1;
3122 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
3125 if (! is_elf_hash_table (info
))
3128 /* Any syms created from now on start with -1 in
3129 got.refcount/offset and plt.refcount/offset. */
3130 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
3132 /* The backend may have to create some sections regardless of whether
3133 we're dynamic or not. */
3134 bed
= get_elf_backend_data (output_bfd
);
3135 if (bed
->elf_backend_always_size_sections
3136 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
3139 dynobj
= elf_hash_table (info
)->dynobj
;
3141 /* If there were no dynamic objects in the link, there is nothing to
3146 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
3149 if (elf_hash_table (info
)->dynamic_sections_created
)
3151 struct elf_info_failed eif
;
3152 struct elf_link_hash_entry
*h
;
3154 struct bfd_elf_version_tree
*t
;
3155 struct bfd_elf_version_expr
*d
;
3156 bfd_boolean all_defined
;
3158 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
3159 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
3163 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3165 if (soname_indx
== (bfd_size_type
) -1
3166 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SONAME
,
3173 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMBOLIC
,
3176 info
->flags
|= DF_SYMBOLIC
;
3183 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
3185 if (info
->new_dtags
)
3186 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
3187 if (indx
== (bfd_size_type
) -1
3188 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RPATH
, indx
)
3190 && ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RUNPATH
,
3195 if (filter_shlib
!= NULL
)
3199 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3200 filter_shlib
, TRUE
);
3201 if (indx
== (bfd_size_type
) -1
3202 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FILTER
, indx
))
3206 if (auxiliary_filters
!= NULL
)
3208 const char * const *p
;
3210 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
3214 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3216 if (indx
== (bfd_size_type
) -1
3217 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_AUXILIARY
,
3224 eif
.verdefs
= verdefs
;
3227 /* If we are supposed to export all symbols into the dynamic symbol
3228 table (this is not the normal case), then do so. */
3229 if (info
->export_dynamic
)
3231 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
3237 /* Make all global versions with definiton. */
3238 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3239 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3240 if (!d
->symver
&& strchr (d
->pattern
, '*') == NULL
)
3242 const char *verstr
, *name
;
3243 size_t namelen
, verlen
, newlen
;
3245 struct elf_link_hash_entry
*newh
;
3248 namelen
= strlen (name
);
3250 verlen
= strlen (verstr
);
3251 newlen
= namelen
+ verlen
+ 3;
3253 newname
= (char *) bfd_malloc ((bfd_size_type
) newlen
);
3254 if (newname
== NULL
)
3256 memcpy (newname
, name
, namelen
);
3258 /* Check the hidden versioned definition. */
3259 p
= newname
+ namelen
;
3261 memcpy (p
, verstr
, verlen
+ 1);
3262 newh
= elf_link_hash_lookup (elf_hash_table (info
),
3263 newname
, FALSE
, FALSE
,
3266 || (newh
->root
.type
!= bfd_link_hash_defined
3267 && newh
->root
.type
!= bfd_link_hash_defweak
))
3269 /* Check the default versioned definition. */
3271 memcpy (p
, verstr
, verlen
+ 1);
3272 newh
= elf_link_hash_lookup (elf_hash_table (info
),
3273 newname
, FALSE
, FALSE
,
3278 /* Mark this version if there is a definition and it is
3279 not defined in a shared object. */
3281 && ((newh
->elf_link_hash_flags
3282 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)
3283 && (newh
->root
.type
== bfd_link_hash_defined
3284 || newh
->root
.type
== bfd_link_hash_defweak
))
3288 /* Attach all the symbols to their version information. */
3289 asvinfo
.output_bfd
= output_bfd
;
3290 asvinfo
.info
= info
;
3291 asvinfo
.verdefs
= verdefs
;
3292 asvinfo
.failed
= FALSE
;
3294 elf_link_hash_traverse (elf_hash_table (info
),
3295 elf_link_assign_sym_version
,
3300 if (!info
->allow_undefined_version
)
3302 /* Check if all global versions have a definiton. */
3304 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3305 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3306 if (!d
->symver
&& !d
->script
3307 && strchr (d
->pattern
, '*') == NULL
)
3309 (*_bfd_error_handler
)
3310 (_("%s: undefined version: %s"),
3311 d
->pattern
, t
->name
);
3312 all_defined
= FALSE
;
3317 bfd_set_error (bfd_error_bad_value
);
3322 /* Find all symbols which were defined in a dynamic object and make
3323 the backend pick a reasonable value for them. */
3324 elf_link_hash_traverse (elf_hash_table (info
),
3325 elf_adjust_dynamic_symbol
,
3330 /* Add some entries to the .dynamic section. We fill in some of the
3331 values later, in elf_bfd_final_link, but we must add the entries
3332 now so that we know the final size of the .dynamic section. */
3334 /* If there are initialization and/or finalization functions to
3335 call then add the corresponding DT_INIT/DT_FINI entries. */
3336 h
= (info
->init_function
3337 ? elf_link_hash_lookup (elf_hash_table (info
),
3338 info
->init_function
, FALSE
,
3342 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3343 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3345 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT
, (bfd_vma
) 0))
3348 h
= (info
->fini_function
3349 ? elf_link_hash_lookup (elf_hash_table (info
),
3350 info
->fini_function
, FALSE
,
3354 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3355 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3357 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI
, (bfd_vma
) 0))
3361 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
3363 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3369 for (sub
= info
->input_bfds
; sub
!= NULL
;
3370 sub
= sub
->link_next
)
3371 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3372 if (elf_section_data (o
)->this_hdr
.sh_type
3373 == SHT_PREINIT_ARRAY
)
3375 (*_bfd_error_handler
)
3376 (_("%s: .preinit_array section is not allowed in DSO"),
3377 bfd_archive_filename (sub
));
3381 bfd_set_error (bfd_error_nonrepresentable_section
);
3385 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAY
,
3387 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAYSZ
,
3391 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
3393 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAY
,
3395 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAYSZ
,
3399 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
3401 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAY
,
3403 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAYSZ
,
3408 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
3409 /* If .dynstr is excluded from the link, we don't want any of
3410 these tags. Strictly, we should be checking each section
3411 individually; This quick check covers for the case where
3412 someone does a /DISCARD/ : { *(*) }. */
3413 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
3415 bfd_size_type strsize
;
3417 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3418 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_HASH
, (bfd_vma
) 0)
3419 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRTAB
, (bfd_vma
) 0)
3420 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMTAB
, (bfd_vma
) 0)
3421 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRSZ
, strsize
)
3422 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMENT
,
3423 (bfd_vma
) sizeof (Elf_External_Sym
)))
3428 /* The backend must work out the sizes of all the other dynamic
3430 if (bed
->elf_backend_size_dynamic_sections
3431 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
3434 if (elf_hash_table (info
)->dynamic_sections_created
)
3436 bfd_size_type dynsymcount
;
3438 size_t bucketcount
= 0;
3439 size_t hash_entry_size
;
3440 unsigned int dtagcount
;
3442 /* Set up the version definition section. */
3443 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3444 BFD_ASSERT (s
!= NULL
);
3446 /* We may have created additional version definitions if we are
3447 just linking a regular application. */
3448 verdefs
= asvinfo
.verdefs
;
3450 /* Skip anonymous version tag. */
3451 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
3452 verdefs
= verdefs
->next
;
3454 if (verdefs
== NULL
)
3455 _bfd_strip_section_from_output (info
, s
);
3460 struct bfd_elf_version_tree
*t
;
3462 Elf_Internal_Verdef def
;
3463 Elf_Internal_Verdaux defaux
;
3468 /* Make space for the base version. */
3469 size
+= sizeof (Elf_External_Verdef
);
3470 size
+= sizeof (Elf_External_Verdaux
);
3473 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3475 struct bfd_elf_version_deps
*n
;
3477 size
+= sizeof (Elf_External_Verdef
);
3478 size
+= sizeof (Elf_External_Verdaux
);
3481 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3482 size
+= sizeof (Elf_External_Verdaux
);
3485 s
->_raw_size
= size
;
3486 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3487 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3490 /* Fill in the version definition section. */
3494 def
.vd_version
= VER_DEF_CURRENT
;
3495 def
.vd_flags
= VER_FLG_BASE
;
3498 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3499 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3500 + sizeof (Elf_External_Verdaux
));
3502 if (soname_indx
!= (bfd_size_type
) -1)
3504 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3506 def
.vd_hash
= bfd_elf_hash (soname
);
3507 defaux
.vda_name
= soname_indx
;
3514 name
= basename (output_bfd
->filename
);
3515 def
.vd_hash
= bfd_elf_hash (name
);
3516 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3518 if (indx
== (bfd_size_type
) -1)
3520 defaux
.vda_name
= indx
;
3522 defaux
.vda_next
= 0;
3524 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3525 (Elf_External_Verdef
*) p
);
3526 p
+= sizeof (Elf_External_Verdef
);
3527 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3528 (Elf_External_Verdaux
*) p
);
3529 p
+= sizeof (Elf_External_Verdaux
);
3531 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3534 struct bfd_elf_version_deps
*n
;
3535 struct elf_link_hash_entry
*h
;
3536 struct bfd_link_hash_entry
*bh
;
3539 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3542 /* Add a symbol representing this version. */
3544 if (! (_bfd_generic_link_add_one_symbol
3545 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
3546 (bfd_vma
) 0, (const char *) NULL
, FALSE
,
3547 get_elf_backend_data (dynobj
)->collect
, &bh
)))
3549 h
= (struct elf_link_hash_entry
*) bh
;
3550 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
3551 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3552 h
->type
= STT_OBJECT
;
3553 h
->verinfo
.vertree
= t
;
3555 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
3558 def
.vd_version
= VER_DEF_CURRENT
;
3560 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
3561 def
.vd_flags
|= VER_FLG_WEAK
;
3562 def
.vd_ndx
= t
->vernum
+ 1;
3563 def
.vd_cnt
= cdeps
+ 1;
3564 def
.vd_hash
= bfd_elf_hash (t
->name
);
3565 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3566 if (t
->next
!= NULL
)
3567 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3568 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
3572 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3573 (Elf_External_Verdef
*) p
);
3574 p
+= sizeof (Elf_External_Verdef
);
3576 defaux
.vda_name
= h
->dynstr_index
;
3577 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3579 if (t
->deps
== NULL
)
3580 defaux
.vda_next
= 0;
3582 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3583 t
->name_indx
= defaux
.vda_name
;
3585 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3586 (Elf_External_Verdaux
*) p
);
3587 p
+= sizeof (Elf_External_Verdaux
);
3589 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3591 if (n
->version_needed
== NULL
)
3593 /* This can happen if there was an error in the
3595 defaux
.vda_name
= 0;
3599 defaux
.vda_name
= n
->version_needed
->name_indx
;
3600 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3603 if (n
->next
== NULL
)
3604 defaux
.vda_next
= 0;
3606 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3608 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3609 (Elf_External_Verdaux
*) p
);
3610 p
+= sizeof (Elf_External_Verdaux
);
3614 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEF
, (bfd_vma
) 0)
3615 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEFNUM
,
3619 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
3622 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
3624 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS
, info
->flags
))
3631 info
->flags_1
&= ~ (DF_1_INITFIRST
3634 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS_1
,
3639 /* Work out the size of the version reference section. */
3641 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3642 BFD_ASSERT (s
!= NULL
);
3644 struct elf_find_verdep_info sinfo
;
3646 sinfo
.output_bfd
= output_bfd
;
3648 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
3649 if (sinfo
.vers
== 0)
3651 sinfo
.failed
= FALSE
;
3653 elf_link_hash_traverse (elf_hash_table (info
),
3654 elf_link_find_version_dependencies
,
3657 if (elf_tdata (output_bfd
)->verref
== NULL
)
3658 _bfd_strip_section_from_output (info
, s
);
3661 Elf_Internal_Verneed
*t
;
3666 /* Build the version definition section. */
3669 for (t
= elf_tdata (output_bfd
)->verref
;
3673 Elf_Internal_Vernaux
*a
;
3675 size
+= sizeof (Elf_External_Verneed
);
3677 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3678 size
+= sizeof (Elf_External_Vernaux
);
3681 s
->_raw_size
= size
;
3682 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3683 if (s
->contents
== NULL
)
3687 for (t
= elf_tdata (output_bfd
)->verref
;
3692 Elf_Internal_Vernaux
*a
;
3696 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3699 t
->vn_version
= VER_NEED_CURRENT
;
3701 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3702 elf_dt_name (t
->vn_bfd
) != NULL
3703 ? elf_dt_name (t
->vn_bfd
)
3704 : basename (t
->vn_bfd
->filename
),
3706 if (indx
== (bfd_size_type
) -1)
3709 t
->vn_aux
= sizeof (Elf_External_Verneed
);
3710 if (t
->vn_nextref
== NULL
)
3713 t
->vn_next
= (sizeof (Elf_External_Verneed
)
3714 + caux
* sizeof (Elf_External_Vernaux
));
3716 _bfd_elf_swap_verneed_out (output_bfd
, t
,
3717 (Elf_External_Verneed
*) p
);
3718 p
+= sizeof (Elf_External_Verneed
);
3720 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3722 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
3723 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3724 a
->vna_nodename
, FALSE
);
3725 if (indx
== (bfd_size_type
) -1)
3728 if (a
->vna_nextptr
== NULL
)
3731 a
->vna_next
= sizeof (Elf_External_Vernaux
);
3733 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
3734 (Elf_External_Vernaux
*) p
);
3735 p
+= sizeof (Elf_External_Vernaux
);
3739 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEED
,
3741 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEEDNUM
,
3745 elf_tdata (output_bfd
)->cverrefs
= crefs
;
3749 /* Assign dynsym indicies. In a shared library we generate a
3750 section symbol for each output section, which come first.
3751 Next come all of the back-end allocated local dynamic syms,
3752 followed by the rest of the global symbols. */
3754 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3756 /* Work out the size of the symbol version section. */
3757 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3758 BFD_ASSERT (s
!= NULL
);
3759 if (dynsymcount
== 0
3760 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
3762 _bfd_strip_section_from_output (info
, s
);
3763 /* The DYNSYMCOUNT might have changed if we were going to
3764 output a dynamic symbol table entry for S. */
3765 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3769 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
3770 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3771 if (s
->contents
== NULL
)
3774 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERSYM
, (bfd_vma
) 0))
3778 /* Set the size of the .dynsym and .hash sections. We counted
3779 the number of dynamic symbols in elf_link_add_object_symbols.
3780 We will build the contents of .dynsym and .hash when we build
3781 the final symbol table, because until then we do not know the
3782 correct value to give the symbols. We built the .dynstr
3783 section as we went along in elf_link_add_object_symbols. */
3784 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
3785 BFD_ASSERT (s
!= NULL
);
3786 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
3787 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3788 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3791 if (dynsymcount
!= 0)
3793 Elf_Internal_Sym isym
;
3795 /* The first entry in .dynsym is a dummy symbol. */
3802 elf_swap_symbol_out (output_bfd
, &isym
, (PTR
) s
->contents
, (PTR
) 0);
3805 /* Compute the size of the hashing table. As a side effect this
3806 computes the hash values for all the names we export. */
3807 bucketcount
= compute_bucket_count (info
);
3809 s
= bfd_get_section_by_name (dynobj
, ".hash");
3810 BFD_ASSERT (s
!= NULL
);
3811 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
3812 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
3813 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3814 if (s
->contents
== NULL
)
3817 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) bucketcount
,
3819 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) dynsymcount
,
3820 s
->contents
+ hash_entry_size
);
3822 elf_hash_table (info
)->bucketcount
= bucketcount
;
3824 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
3825 BFD_ASSERT (s
!= NULL
);
3827 elf_finalize_dynstr (output_bfd
, info
);
3829 s
->_raw_size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3831 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
3832 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NULL
, (bfd_vma
) 0))
3839 /* This function is used to adjust offsets into .dynstr for
3840 dynamic symbols. This is called via elf_link_hash_traverse. */
3842 static bfd_boolean elf_adjust_dynstr_offsets
3843 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3846 elf_adjust_dynstr_offsets (h
, data
)
3847 struct elf_link_hash_entry
*h
;
3850 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3852 if (h
->root
.type
== bfd_link_hash_warning
)
3853 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3855 if (h
->dynindx
!= -1)
3856 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3860 /* Assign string offsets in .dynstr, update all structures referencing
3864 elf_finalize_dynstr (output_bfd
, info
)
3866 struct bfd_link_info
*info
;
3868 struct elf_link_local_dynamic_entry
*entry
;
3869 struct elf_strtab_hash
*dynstr
= elf_hash_table (info
)->dynstr
;
3870 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
3873 Elf_External_Dyn
*dyncon
, *dynconend
;
3875 _bfd_elf_strtab_finalize (dynstr
);
3876 size
= _bfd_elf_strtab_size (dynstr
);
3878 /* Update all .dynamic entries referencing .dynstr strings. */
3879 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3880 BFD_ASSERT (sdyn
!= NULL
);
3882 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
3883 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
3885 for (; dyncon
< dynconend
; dyncon
++)
3887 Elf_Internal_Dyn dyn
;
3889 elf_swap_dyn_in (dynobj
, dyncon
, & dyn
);
3893 dyn
.d_un
.d_val
= size
;
3894 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3902 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3903 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3910 /* Now update local dynamic symbols. */
3911 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
3912 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3913 entry
->isym
.st_name
);
3915 /* And the rest of dynamic symbols. */
3916 elf_link_hash_traverse (elf_hash_table (info
),
3917 elf_adjust_dynstr_offsets
, dynstr
);
3919 /* Adjust version definitions. */
3920 if (elf_tdata (output_bfd
)->cverdefs
)
3925 Elf_Internal_Verdef def
;
3926 Elf_Internal_Verdaux defaux
;
3928 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3929 p
= (bfd_byte
*) s
->contents
;
3932 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3934 p
+= sizeof (Elf_External_Verdef
);
3935 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3937 _bfd_elf_swap_verdaux_in (output_bfd
,
3938 (Elf_External_Verdaux
*) p
, &defaux
);
3939 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3941 _bfd_elf_swap_verdaux_out (output_bfd
,
3942 &defaux
, (Elf_External_Verdaux
*) p
);
3943 p
+= sizeof (Elf_External_Verdaux
);
3946 while (def
.vd_next
);
3949 /* Adjust version references. */
3950 if (elf_tdata (output_bfd
)->verref
)
3955 Elf_Internal_Verneed need
;
3956 Elf_Internal_Vernaux needaux
;
3958 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3959 p
= (bfd_byte
*) s
->contents
;
3962 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3964 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3965 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3966 (Elf_External_Verneed
*) p
);
3967 p
+= sizeof (Elf_External_Verneed
);
3968 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3970 _bfd_elf_swap_vernaux_in (output_bfd
,
3971 (Elf_External_Vernaux
*) p
, &needaux
);
3972 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3974 _bfd_elf_swap_vernaux_out (output_bfd
,
3976 (Elf_External_Vernaux
*) p
);
3977 p
+= sizeof (Elf_External_Vernaux
);
3980 while (need
.vn_next
);
3986 /* Fix up the flags for a symbol. This handles various cases which
3987 can only be fixed after all the input files are seen. This is
3988 currently called by both adjust_dynamic_symbol and
3989 assign_sym_version, which is unnecessary but perhaps more robust in
3990 the face of future changes. */
3993 elf_fix_symbol_flags (h
, eif
)
3994 struct elf_link_hash_entry
*h
;
3995 struct elf_info_failed
*eif
;
3997 /* If this symbol was mentioned in a non-ELF file, try to set
3998 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3999 permit a non-ELF file to correctly refer to a symbol defined in
4000 an ELF dynamic object. */
4001 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
4003 while (h
->root
.type
== bfd_link_hash_indirect
)
4004 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4006 if (h
->root
.type
!= bfd_link_hash_defined
4007 && h
->root
.type
!= bfd_link_hash_defweak
)
4008 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
4009 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
4012 if (h
->root
.u
.def
.section
->owner
!= NULL
4013 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
4014 == bfd_target_elf_flavour
))
4015 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
4016 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
4018 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4021 if (h
->dynindx
== -1
4022 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
4023 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
4025 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
4034 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
4035 was first seen in a non-ELF file. Fortunately, if the symbol
4036 was first seen in an ELF file, we're probably OK unless the
4037 symbol was defined in a non-ELF file. Catch that case here.
4038 FIXME: We're still in trouble if the symbol was first seen in
4039 a dynamic object, and then later in a non-ELF regular object. */
4040 if ((h
->root
.type
== bfd_link_hash_defined
4041 || h
->root
.type
== bfd_link_hash_defweak
)
4042 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
4043 && (h
->root
.u
.def
.section
->owner
!= NULL
4044 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
4045 != bfd_target_elf_flavour
)
4046 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
4047 && (h
->elf_link_hash_flags
4048 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
4049 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4052 /* If this is a final link, and the symbol was defined as a common
4053 symbol in a regular object file, and there was no definition in
4054 any dynamic object, then the linker will have allocated space for
4055 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
4056 flag will not have been set. */
4057 if (h
->root
.type
== bfd_link_hash_defined
4058 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
4059 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
4060 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4061 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
4062 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4064 /* If -Bsymbolic was used (which means to bind references to global
4065 symbols to the definition within the shared object), and this
4066 symbol was defined in a regular object, then it actually doesn't
4067 need a PLT entry, and we can accomplish that by forcing it local.
4068 Likewise, if the symbol has hidden or internal visibility.
4069 FIXME: It might be that we also do not need a PLT for other
4070 non-hidden visibilities, but we would have to tell that to the
4071 backend specifically; we can't just clear PLT-related data here. */
4072 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
4073 && eif
->info
->shared
4074 && is_elf_hash_table (eif
->info
)
4075 && (eif
->info
->symbolic
4076 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
4077 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
4078 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
4080 struct elf_backend_data
*bed
;
4081 bfd_boolean force_local
;
4083 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
4085 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
4086 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
4087 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
4090 /* If this is a weak defined symbol in a dynamic object, and we know
4091 the real definition in the dynamic object, copy interesting flags
4092 over to the real definition. */
4093 if (h
->weakdef
!= NULL
)
4095 struct elf_link_hash_entry
*weakdef
;
4097 weakdef
= h
->weakdef
;
4098 if (h
->root
.type
== bfd_link_hash_indirect
)
4099 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4101 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4102 || h
->root
.type
== bfd_link_hash_defweak
);
4103 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
4104 || weakdef
->root
.type
== bfd_link_hash_defweak
);
4105 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
4107 /* If the real definition is defined by a regular object file,
4108 don't do anything special. See the longer description in
4109 elf_adjust_dynamic_symbol, below. */
4110 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
4114 struct elf_backend_data
*bed
;
4116 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
4117 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
4124 /* Make the backend pick a good value for a dynamic symbol. This is
4125 called via elf_link_hash_traverse, and also calls itself
4129 elf_adjust_dynamic_symbol (h
, data
)
4130 struct elf_link_hash_entry
*h
;
4133 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
4135 struct elf_backend_data
*bed
;
4137 if (! is_elf_hash_table (eif
->info
))
4140 if (h
->root
.type
== bfd_link_hash_warning
)
4142 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
4143 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
4145 /* When warning symbols are created, they **replace** the "real"
4146 entry in the hash table, thus we never get to see the real
4147 symbol in a hash traversal. So look at it now. */
4148 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4151 /* Ignore indirect symbols. These are added by the versioning code. */
4152 if (h
->root
.type
== bfd_link_hash_indirect
)
4155 /* Fix the symbol flags. */
4156 if (! elf_fix_symbol_flags (h
, eif
))
4159 /* If this symbol does not require a PLT entry, and it is not
4160 defined by a dynamic object, or is not referenced by a regular
4161 object, ignore it. We do have to handle a weak defined symbol,
4162 even if no regular object refers to it, if we decided to add it
4163 to the dynamic symbol table. FIXME: Do we normally need to worry
4164 about symbols which are defined by one dynamic object and
4165 referenced by another one? */
4166 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
4167 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4168 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4169 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
4170 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
4172 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
4176 /* If we've already adjusted this symbol, don't do it again. This
4177 can happen via a recursive call. */
4178 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
4181 /* Don't look at this symbol again. Note that we must set this
4182 after checking the above conditions, because we may look at a
4183 symbol once, decide not to do anything, and then get called
4184 recursively later after REF_REGULAR is set below. */
4185 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
4187 /* If this is a weak definition, and we know a real definition, and
4188 the real symbol is not itself defined by a regular object file,
4189 then get a good value for the real definition. We handle the
4190 real symbol first, for the convenience of the backend routine.
4192 Note that there is a confusing case here. If the real definition
4193 is defined by a regular object file, we don't get the real symbol
4194 from the dynamic object, but we do get the weak symbol. If the
4195 processor backend uses a COPY reloc, then if some routine in the
4196 dynamic object changes the real symbol, we will not see that
4197 change in the corresponding weak symbol. This is the way other
4198 ELF linkers work as well, and seems to be a result of the shared
4201 I will clarify this issue. Most SVR4 shared libraries define the
4202 variable _timezone and define timezone as a weak synonym. The
4203 tzset call changes _timezone. If you write
4204 extern int timezone;
4206 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4207 you might expect that, since timezone is a synonym for _timezone,
4208 the same number will print both times. However, if the processor
4209 backend uses a COPY reloc, then actually timezone will be copied
4210 into your process image, and, since you define _timezone
4211 yourself, _timezone will not. Thus timezone and _timezone will
4212 wind up at different memory locations. The tzset call will set
4213 _timezone, leaving timezone unchanged. */
4215 if (h
->weakdef
!= NULL
)
4217 /* If we get to this point, we know there is an implicit
4218 reference by a regular object file via the weak symbol H.
4219 FIXME: Is this really true? What if the traversal finds
4220 H->WEAKDEF before it finds H? */
4221 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
4223 if (! elf_adjust_dynamic_symbol (h
->weakdef
, (PTR
) eif
))
4227 /* If a symbol has no type and no size and does not require a PLT
4228 entry, then we are probably about to do the wrong thing here: we
4229 are probably going to create a COPY reloc for an empty object.
4230 This case can arise when a shared object is built with assembly
4231 code, and the assembly code fails to set the symbol type. */
4233 && h
->type
== STT_NOTYPE
4234 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
4235 (*_bfd_error_handler
)
4236 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4237 h
->root
.root
.string
);
4239 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
4240 bed
= get_elf_backend_data (dynobj
);
4241 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
4250 /* This routine is used to export all defined symbols into the dynamic
4251 symbol table. It is called via elf_link_hash_traverse. */
4254 elf_export_symbol (h
, data
)
4255 struct elf_link_hash_entry
*h
;
4258 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
4260 /* Ignore indirect symbols. These are added by the versioning code. */
4261 if (h
->root
.type
== bfd_link_hash_indirect
)
4264 if (h
->root
.type
== bfd_link_hash_warning
)
4265 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4267 if (h
->dynindx
== -1
4268 && (h
->elf_link_hash_flags
4269 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
4271 struct bfd_elf_version_tree
*t
;
4272 struct bfd_elf_version_expr
*d
;
4274 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
4276 if (t
->globals
!= NULL
)
4278 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4280 if ((*d
->match
) (d
, h
->root
.root
.string
))
4285 if (t
->locals
!= NULL
)
4287 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4289 if ((*d
->match
) (d
, h
->root
.root
.string
))
4298 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
4309 /* Look through the symbols which are defined in other shared
4310 libraries and referenced here. Update the list of version
4311 dependencies. This will be put into the .gnu.version_r section.
4312 This function is called via elf_link_hash_traverse. */
4315 elf_link_find_version_dependencies (h
, data
)
4316 struct elf_link_hash_entry
*h
;
4319 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
4320 Elf_Internal_Verneed
*t
;
4321 Elf_Internal_Vernaux
*a
;
4324 if (h
->root
.type
== bfd_link_hash_warning
)
4325 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4327 /* We only care about symbols defined in shared objects with version
4329 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4330 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4332 || h
->verinfo
.verdef
== NULL
)
4335 /* See if we already know about this version. */
4336 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
4338 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
4341 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4342 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
4348 /* This is a new version. Add it to tree we are building. */
4353 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4356 rinfo
->failed
= TRUE
;
4360 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
4361 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
4362 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
4366 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4368 /* Note that we are copying a string pointer here, and testing it
4369 above. If bfd_elf_string_from_elf_section is ever changed to
4370 discard the string data when low in memory, this will have to be
4372 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
4374 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
4375 a
->vna_nextptr
= t
->vn_auxptr
;
4377 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
4380 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4387 /* Figure out appropriate versions for all the symbols. We may not
4388 have the version number script until we have read all of the input
4389 files, so until that point we don't know which symbols should be
4390 local. This function is called via elf_link_hash_traverse. */
4393 elf_link_assign_sym_version (h
, data
)
4394 struct elf_link_hash_entry
*h
;
4397 struct elf_assign_sym_version_info
*sinfo
;
4398 struct bfd_link_info
*info
;
4399 struct elf_backend_data
*bed
;
4400 struct elf_info_failed eif
;
4404 sinfo
= (struct elf_assign_sym_version_info
*) data
;
4407 if (h
->root
.type
== bfd_link_hash_warning
)
4408 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4410 /* Fix the symbol flags. */
4413 if (! elf_fix_symbol_flags (h
, &eif
))
4416 sinfo
->failed
= TRUE
;
4420 /* We only need version numbers for symbols defined in regular
4422 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4425 bed
= get_elf_backend_data (sinfo
->output_bfd
);
4426 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4427 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
4429 struct bfd_elf_version_tree
*t
;
4434 /* There are two consecutive ELF_VER_CHR characters if this is
4435 not a hidden symbol. */
4437 if (*p
== ELF_VER_CHR
)
4443 /* If there is no version string, we can just return out. */
4447 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4451 /* Look for the version. If we find it, it is no longer weak. */
4452 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4454 if (strcmp (t
->name
, p
) == 0)
4458 struct bfd_elf_version_expr
*d
;
4460 len
= p
- h
->root
.root
.string
;
4461 alc
= bfd_malloc ((bfd_size_type
) len
);
4464 memcpy (alc
, h
->root
.root
.string
, len
- 1);
4465 alc
[len
- 1] = '\0';
4466 if (alc
[len
- 2] == ELF_VER_CHR
)
4467 alc
[len
- 2] = '\0';
4469 h
->verinfo
.vertree
= t
;
4473 if (t
->globals
!= NULL
)
4475 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4476 if ((*d
->match
) (d
, alc
))
4480 /* See if there is anything to force this symbol to
4482 if (d
== NULL
&& t
->locals
!= NULL
)
4484 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4486 if ((*d
->match
) (d
, alc
))
4488 if (h
->dynindx
!= -1
4490 && ! info
->export_dynamic
)
4492 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4505 /* If we are building an application, we need to create a
4506 version node for this version. */
4507 if (t
== NULL
&& ! info
->shared
)
4509 struct bfd_elf_version_tree
**pp
;
4512 /* If we aren't going to export this symbol, we don't need
4513 to worry about it. */
4514 if (h
->dynindx
== -1)
4518 t
= ((struct bfd_elf_version_tree
*)
4519 bfd_alloc (sinfo
->output_bfd
, amt
));
4522 sinfo
->failed
= TRUE
;
4531 t
->name_indx
= (unsigned int) -1;
4535 /* Don't count anonymous version tag. */
4536 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
4538 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4540 t
->vernum
= version_index
;
4544 h
->verinfo
.vertree
= t
;
4548 /* We could not find the version for a symbol when
4549 generating a shared archive. Return an error. */
4550 (*_bfd_error_handler
)
4551 (_("%s: undefined versioned symbol name %s"),
4552 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
4553 bfd_set_error (bfd_error_bad_value
);
4554 sinfo
->failed
= TRUE
;
4559 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4562 /* If we don't have a version for this symbol, see if we can find
4564 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
4566 struct bfd_elf_version_tree
*t
;
4567 struct bfd_elf_version_tree
*local_ver
;
4568 struct bfd_elf_version_expr
*d
;
4570 /* See if can find what version this symbol is in. If the
4571 symbol is supposed to be local, then don't actually register
4574 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4576 if (t
->globals
!= NULL
)
4578 bfd_boolean matched
;
4581 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4583 if ((*d
->match
) (d
, h
->root
.root
.string
))
4589 /* There is a version without definition. Make
4590 the symbol the default definition for this
4592 h
->verinfo
.vertree
= t
;
4603 /* There is no undefined version for this symbol. Hide the
4605 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4608 if (t
->locals
!= NULL
)
4610 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4612 /* If the match is "*", keep looking for a more
4613 explicit, perhaps even global, match. */
4614 if (d
->pattern
[0] == '*' && d
->pattern
[1] == '\0')
4616 else if ((*d
->match
) (d
, h
->root
.root
.string
))
4628 if (local_ver
!= NULL
)
4630 h
->verinfo
.vertree
= local_ver
;
4631 if (h
->dynindx
!= -1
4633 && ! info
->export_dynamic
)
4635 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4643 /* Final phase of ELF linker. */
4645 /* A structure we use to avoid passing large numbers of arguments. */
4647 struct elf_final_link_info
4649 /* General link information. */
4650 struct bfd_link_info
*info
;
4653 /* Symbol string table. */
4654 struct bfd_strtab_hash
*symstrtab
;
4655 /* .dynsym section. */
4656 asection
*dynsym_sec
;
4657 /* .hash section. */
4659 /* symbol version section (.gnu.version). */
4660 asection
*symver_sec
;
4661 /* first SHF_TLS section (if any). */
4662 asection
*first_tls_sec
;
4663 /* Buffer large enough to hold contents of any section. */
4665 /* Buffer large enough to hold external relocs of any section. */
4666 PTR external_relocs
;
4667 /* Buffer large enough to hold internal relocs of any section. */
4668 Elf_Internal_Rela
*internal_relocs
;
4669 /* Buffer large enough to hold external local symbols of any input
4671 Elf_External_Sym
*external_syms
;
4672 /* And a buffer for symbol section indices. */
4673 Elf_External_Sym_Shndx
*locsym_shndx
;
4674 /* Buffer large enough to hold internal local symbols of any input
4676 Elf_Internal_Sym
*internal_syms
;
4677 /* Array large enough to hold a symbol index for each local symbol
4678 of any input BFD. */
4680 /* Array large enough to hold a section pointer for each local
4681 symbol of any input BFD. */
4682 asection
**sections
;
4683 /* Buffer to hold swapped out symbols. */
4684 Elf_External_Sym
*symbuf
;
4685 /* And one for symbol section indices. */
4686 Elf_External_Sym_Shndx
*symshndxbuf
;
4687 /* Number of swapped out symbols in buffer. */
4688 size_t symbuf_count
;
4689 /* Number of symbols which fit in symbuf. */
4691 /* And same for symshndxbuf. */
4692 size_t shndxbuf_size
;
4695 static bfd_boolean elf_link_output_sym
4696 PARAMS ((struct elf_final_link_info
*, const char *,
4697 Elf_Internal_Sym
*, asection
*));
4698 static bfd_boolean elf_link_flush_output_syms
4699 PARAMS ((struct elf_final_link_info
*));
4700 static bfd_boolean elf_link_output_extsym
4701 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4702 static bfd_boolean elf_link_sec_merge_syms
4703 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4704 static bfd_boolean elf_link_check_versioned_symbol
4705 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
4706 static bfd_boolean elf_link_input_bfd
4707 PARAMS ((struct elf_final_link_info
*, bfd
*));
4708 static bfd_boolean elf_reloc_link_order
4709 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
4710 struct bfd_link_order
*));
4712 /* This struct is used to pass information to elf_link_output_extsym. */
4714 struct elf_outext_info
4717 bfd_boolean localsyms
;
4718 struct elf_final_link_info
*finfo
;
4721 /* Compute the size of, and allocate space for, REL_HDR which is the
4722 section header for a section containing relocations for O. */
4725 elf_link_size_reloc_section (abfd
, rel_hdr
, o
)
4727 Elf_Internal_Shdr
*rel_hdr
;
4730 bfd_size_type reloc_count
;
4731 bfd_size_type num_rel_hashes
;
4733 /* Figure out how many relocations there will be. */
4734 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
4735 reloc_count
= elf_section_data (o
)->rel_count
;
4737 reloc_count
= elf_section_data (o
)->rel_count2
;
4739 num_rel_hashes
= o
->reloc_count
;
4740 if (num_rel_hashes
< reloc_count
)
4741 num_rel_hashes
= reloc_count
;
4743 /* That allows us to calculate the size of the section. */
4744 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
4746 /* The contents field must last into write_object_contents, so we
4747 allocate it with bfd_alloc rather than malloc. Also since we
4748 cannot be sure that the contents will actually be filled in,
4749 we zero the allocated space. */
4750 rel_hdr
->contents
= (PTR
) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
4751 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
4754 /* We only allocate one set of hash entries, so we only do it the
4755 first time we are called. */
4756 if (elf_section_data (o
)->rel_hashes
== NULL
4759 struct elf_link_hash_entry
**p
;
4761 p
= ((struct elf_link_hash_entry
**)
4762 bfd_zmalloc (num_rel_hashes
4763 * sizeof (struct elf_link_hash_entry
*)));
4767 elf_section_data (o
)->rel_hashes
= p
;
4773 /* When performing a relocateable link, the input relocations are
4774 preserved. But, if they reference global symbols, the indices
4775 referenced must be updated. Update all the relocations in
4776 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4779 elf_link_adjust_relocs (abfd
, rel_hdr
, count
, rel_hash
)
4781 Elf_Internal_Shdr
*rel_hdr
;
4783 struct elf_link_hash_entry
**rel_hash
;
4786 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4788 void (*swap_in
) PARAMS ((bfd
*, const bfd_byte
*, Elf_Internal_Rela
*));
4789 void (*swap_out
) PARAMS ((bfd
*, const Elf_Internal_Rela
*, bfd_byte
*));
4791 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4793 swap_in
= bed
->s
->swap_reloc_in
;
4794 swap_out
= bed
->s
->swap_reloc_out
;
4796 else if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
))
4798 swap_in
= bed
->s
->swap_reloca_in
;
4799 swap_out
= bed
->s
->swap_reloca_out
;
4804 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
4807 erela
= rel_hdr
->contents
;
4808 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
4810 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
4813 if (*rel_hash
== NULL
)
4816 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
4818 (*swap_in
) (abfd
, erela
, irela
);
4819 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4820 irela
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4821 ELF_R_TYPE (irela
[j
].r_info
));
4822 (*swap_out
) (abfd
, irela
, erela
);
4826 struct elf_link_sort_rela
4829 enum elf_reloc_type_class type
;
4830 /* We use this as an array of size int_rels_per_ext_rel. */
4831 Elf_Internal_Rela rela
[1];
4835 elf_link_sort_cmp1 (A
, B
)
4839 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4840 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4841 int relativea
, relativeb
;
4843 relativea
= a
->type
== reloc_class_relative
;
4844 relativeb
= b
->type
== reloc_class_relative
;
4846 if (relativea
< relativeb
)
4848 if (relativea
> relativeb
)
4850 if (ELF_R_SYM (a
->rela
->r_info
) < ELF_R_SYM (b
->rela
->r_info
))
4852 if (ELF_R_SYM (a
->rela
->r_info
) > ELF_R_SYM (b
->rela
->r_info
))
4854 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
4856 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
4862 elf_link_sort_cmp2 (A
, B
)
4866 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4867 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4870 if (a
->offset
< b
->offset
)
4872 if (a
->offset
> b
->offset
)
4874 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
4875 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
4880 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
4882 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
4888 elf_link_sort_relocs (abfd
, info
, psec
)
4890 struct bfd_link_info
*info
;
4893 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4894 asection
*reldyn
, *o
;
4895 bfd_size_type count
, size
;
4896 size_t i
, ret
, sort_elt
, ext_size
;
4897 bfd_byte
*sort
, *s_non_relative
, *p
;
4898 struct elf_link_sort_rela
*sq
;
4899 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4900 int i2e
= bed
->s
->int_rels_per_ext_rel
;
4901 void (*swap_in
) PARAMS ((bfd
*, const bfd_byte
*, Elf_Internal_Rela
*));
4902 void (*swap_out
) PARAMS ((bfd
*, const Elf_Internal_Rela
*, bfd_byte
*));
4904 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
4905 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4907 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
4908 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4910 ext_size
= sizeof (Elf_External_Rel
);
4911 swap_in
= bed
->s
->swap_reloc_in
;
4912 swap_out
= bed
->s
->swap_reloc_out
;
4916 ext_size
= sizeof (Elf_External_Rela
);
4917 swap_in
= bed
->s
->swap_reloca_in
;
4918 swap_out
= bed
->s
->swap_reloca_out
;
4920 count
= reldyn
->_raw_size
/ ext_size
;
4923 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4924 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4925 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4926 && o
->output_section
== reldyn
)
4927 size
+= o
->_raw_size
;
4929 if (size
!= reldyn
->_raw_size
)
4932 sort_elt
= (sizeof (struct elf_link_sort_rela
)
4933 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
4934 sort
= bfd_zmalloc (sort_elt
* count
);
4937 (*info
->callbacks
->warning
)
4938 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0,
4943 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4944 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4945 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4946 && o
->output_section
== reldyn
)
4948 bfd_byte
*erel
, *erelend
;
4951 erelend
= o
->contents
+ o
->_raw_size
;
4952 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
4953 while (erel
< erelend
)
4955 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
4956 (*swap_in
) (abfd
, erel
, s
->rela
);
4957 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
4963 qsort (sort
, (size_t) count
, sort_elt
, elf_link_sort_cmp1
);
4965 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
4967 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
4968 if (s
->type
!= reloc_class_relative
)
4974 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
4975 for (; i
< count
; i
++, p
+= sort_elt
)
4977 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
4978 if (ELF_R_SYM (sp
->rela
->r_info
) != ELF_R_SYM (sq
->rela
->r_info
))
4980 sp
->offset
= sq
->rela
->r_offset
;
4983 qsort (s_non_relative
, (size_t) count
- ret
, sort_elt
, elf_link_sort_cmp2
);
4985 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4986 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4987 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4988 && o
->output_section
== reldyn
)
4990 bfd_byte
*erel
, *erelend
;
4993 erelend
= o
->contents
+ o
->_raw_size
;
4994 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
4995 while (erel
< erelend
)
4997 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
4998 (*swap_out
) (abfd
, s
->rela
, erel
);
5009 /* Do the final step of an ELF link. */
5012 elf_bfd_final_link (abfd
, info
)
5014 struct bfd_link_info
*info
;
5016 bfd_boolean dynamic
;
5017 bfd_boolean emit_relocs
;
5019 struct elf_final_link_info finfo
;
5020 register asection
*o
;
5021 register struct bfd_link_order
*p
;
5023 bfd_size_type max_contents_size
;
5024 bfd_size_type max_external_reloc_size
;
5025 bfd_size_type max_internal_reloc_count
;
5026 bfd_size_type max_sym_count
;
5027 bfd_size_type max_sym_shndx_count
;
5029 Elf_Internal_Sym elfsym
;
5031 Elf_Internal_Shdr
*symtab_hdr
;
5032 Elf_Internal_Shdr
*symtab_shndx_hdr
;
5033 Elf_Internal_Shdr
*symstrtab_hdr
;
5034 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5035 struct elf_outext_info eoinfo
;
5037 size_t relativecount
= 0;
5038 asection
*reldyn
= 0;
5041 if (! is_elf_hash_table (info
))
5045 abfd
->flags
|= DYNAMIC
;
5047 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
5048 dynobj
= elf_hash_table (info
)->dynobj
;
5050 emit_relocs
= (info
->relocateable
5051 || info
->emitrelocations
5052 || bed
->elf_backend_emit_relocs
);
5055 finfo
.output_bfd
= abfd
;
5056 finfo
.symstrtab
= elf_stringtab_init ();
5057 if (finfo
.symstrtab
== NULL
)
5062 finfo
.dynsym_sec
= NULL
;
5063 finfo
.hash_sec
= NULL
;
5064 finfo
.symver_sec
= NULL
;
5068 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
5069 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
5070 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
5071 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5072 /* Note that it is OK if symver_sec is NULL. */
5075 finfo
.contents
= NULL
;
5076 finfo
.external_relocs
= NULL
;
5077 finfo
.internal_relocs
= NULL
;
5078 finfo
.external_syms
= NULL
;
5079 finfo
.locsym_shndx
= NULL
;
5080 finfo
.internal_syms
= NULL
;
5081 finfo
.indices
= NULL
;
5082 finfo
.sections
= NULL
;
5083 finfo
.symbuf
= NULL
;
5084 finfo
.symshndxbuf
= NULL
;
5085 finfo
.symbuf_count
= 0;
5086 finfo
.shndxbuf_size
= 0;
5087 finfo
.first_tls_sec
= NULL
;
5088 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
5089 if ((o
->flags
& SEC_THREAD_LOCAL
) != 0
5090 && (o
->flags
& SEC_LOAD
) != 0)
5092 finfo
.first_tls_sec
= o
;
5096 /* Count up the number of relocations we will output for each output
5097 section, so that we know the sizes of the reloc sections. We
5098 also figure out some maximum sizes. */
5099 max_contents_size
= 0;
5100 max_external_reloc_size
= 0;
5101 max_internal_reloc_count
= 0;
5103 max_sym_shndx_count
= 0;
5105 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
5107 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
5110 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5112 unsigned int reloc_count
= 0;
5113 struct bfd_elf_section_data
*esdi
= NULL
;
5114 unsigned int *rel_count1
;
5116 if (p
->type
== bfd_section_reloc_link_order
5117 || p
->type
== bfd_symbol_reloc_link_order
)
5119 else if (p
->type
== bfd_indirect_link_order
)
5123 sec
= p
->u
.indirect
.section
;
5124 esdi
= elf_section_data (sec
);
5126 /* Mark all sections which are to be included in the
5127 link. This will normally be every section. We need
5128 to do this so that we can identify any sections which
5129 the linker has decided to not include. */
5130 sec
->linker_mark
= TRUE
;
5132 if (sec
->flags
& SEC_MERGE
)
5135 if (info
->relocateable
|| info
->emitrelocations
)
5136 reloc_count
= sec
->reloc_count
;
5137 else if (bed
->elf_backend_count_relocs
)
5139 Elf_Internal_Rela
* relocs
;
5141 relocs
= (NAME(_bfd_elf
,link_read_relocs
)
5142 (abfd
, sec
, (PTR
) NULL
,
5143 (Elf_Internal_Rela
*) NULL
, info
->keep_memory
));
5145 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
5147 if (elf_section_data (o
)->relocs
!= relocs
)
5151 if (sec
->_raw_size
> max_contents_size
)
5152 max_contents_size
= sec
->_raw_size
;
5153 if (sec
->_cooked_size
> max_contents_size
)
5154 max_contents_size
= sec
->_cooked_size
;
5156 /* We are interested in just local symbols, not all
5158 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
5159 && (sec
->owner
->flags
& DYNAMIC
) == 0)
5163 if (elf_bad_symtab (sec
->owner
))
5164 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
5165 / sizeof (Elf_External_Sym
));
5167 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
5169 if (sym_count
> max_sym_count
)
5170 max_sym_count
= sym_count
;
5172 if (sym_count
> max_sym_shndx_count
5173 && elf_symtab_shndx (sec
->owner
) != 0)
5174 max_sym_shndx_count
= sym_count
;
5176 if ((sec
->flags
& SEC_RELOC
) != 0)
5180 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
5181 if (ext_size
> max_external_reloc_size
)
5182 max_external_reloc_size
= ext_size
;
5183 if (sec
->reloc_count
> max_internal_reloc_count
)
5184 max_internal_reloc_count
= sec
->reloc_count
;
5189 if (reloc_count
== 0)
5192 o
->reloc_count
+= reloc_count
;
5194 /* MIPS may have a mix of REL and RELA relocs on sections.
5195 To support this curious ABI we keep reloc counts in
5196 elf_section_data too. We must be careful to add the
5197 relocations from the input section to the right output
5198 count. FIXME: Get rid of one count. We have
5199 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
5200 rel_count1
= &esdo
->rel_count
;
5203 bfd_boolean same_size
;
5204 bfd_size_type entsize1
;
5206 entsize1
= esdi
->rel_hdr
.sh_entsize
;
5207 BFD_ASSERT (entsize1
== sizeof (Elf_External_Rel
)
5208 || entsize1
== sizeof (Elf_External_Rela
));
5209 same_size
= (!o
->use_rela_p
5210 == (entsize1
== sizeof (Elf_External_Rel
)));
5213 rel_count1
= &esdo
->rel_count2
;
5215 if (esdi
->rel_hdr2
!= NULL
)
5217 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
5218 unsigned int alt_count
;
5219 unsigned int *rel_count2
;
5221 BFD_ASSERT (entsize2
!= entsize1
5222 && (entsize2
== sizeof (Elf_External_Rel
)
5223 || entsize2
== sizeof (Elf_External_Rela
)));
5225 rel_count2
= &esdo
->rel_count2
;
5227 rel_count2
= &esdo
->rel_count
;
5229 /* The following is probably too simplistic if the
5230 backend counts output relocs unusually. */
5231 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
5232 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
5233 *rel_count2
+= alt_count
;
5234 reloc_count
-= alt_count
;
5237 *rel_count1
+= reloc_count
;
5240 if (o
->reloc_count
> 0)
5241 o
->flags
|= SEC_RELOC
;
5244 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5245 set it (this is probably a bug) and if it is set
5246 assign_section_numbers will create a reloc section. */
5247 o
->flags
&=~ SEC_RELOC
;
5250 /* If the SEC_ALLOC flag is not set, force the section VMA to
5251 zero. This is done in elf_fake_sections as well, but forcing
5252 the VMA to 0 here will ensure that relocs against these
5253 sections are handled correctly. */
5254 if ((o
->flags
& SEC_ALLOC
) == 0
5255 && ! o
->user_set_vma
)
5259 if (! info
->relocateable
&& merged
)
5260 elf_link_hash_traverse (elf_hash_table (info
),
5261 elf_link_sec_merge_syms
, (PTR
) abfd
);
5263 /* Figure out the file positions for everything but the symbol table
5264 and the relocs. We set symcount to force assign_section_numbers
5265 to create a symbol table. */
5266 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
5267 BFD_ASSERT (! abfd
->output_has_begun
);
5268 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
5271 /* That created the reloc sections. Set their sizes, and assign
5272 them file positions, and allocate some buffers. */
5273 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5275 if ((o
->flags
& SEC_RELOC
) != 0)
5277 if (!elf_link_size_reloc_section (abfd
,
5278 &elf_section_data (o
)->rel_hdr
,
5282 if (elf_section_data (o
)->rel_hdr2
5283 && !elf_link_size_reloc_section (abfd
,
5284 elf_section_data (o
)->rel_hdr2
,
5289 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5290 to count upwards while actually outputting the relocations. */
5291 elf_section_data (o
)->rel_count
= 0;
5292 elf_section_data (o
)->rel_count2
= 0;
5295 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5297 /* We have now assigned file positions for all the sections except
5298 .symtab and .strtab. We start the .symtab section at the current
5299 file position, and write directly to it. We build the .strtab
5300 section in memory. */
5301 bfd_get_symcount (abfd
) = 0;
5302 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5303 /* sh_name is set in prep_headers. */
5304 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5305 /* sh_flags, sh_addr and sh_size all start off zero. */
5306 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
5307 /* sh_link is set in assign_section_numbers. */
5308 /* sh_info is set below. */
5309 /* sh_offset is set just below. */
5310 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
5312 off
= elf_tdata (abfd
)->next_file_pos
;
5313 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
5315 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5316 incorrect. We do not yet know the size of the .symtab section.
5317 We correct next_file_pos below, after we do know the size. */
5319 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5320 continuously seeking to the right position in the file. */
5321 if (! info
->keep_memory
|| max_sym_count
< 20)
5322 finfo
.symbuf_size
= 20;
5324 finfo
.symbuf_size
= max_sym_count
;
5325 amt
= finfo
.symbuf_size
;
5326 amt
*= sizeof (Elf_External_Sym
);
5327 finfo
.symbuf
= (Elf_External_Sym
*) bfd_malloc (amt
);
5328 if (finfo
.symbuf
== NULL
)
5330 if (elf_numsections (abfd
) > SHN_LORESERVE
)
5332 /* Wild guess at number of output symbols. realloc'd as needed. */
5333 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
5334 finfo
.shndxbuf_size
= amt
;
5335 amt
*= sizeof (Elf_External_Sym_Shndx
);
5336 finfo
.symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
5337 if (finfo
.symshndxbuf
== NULL
)
5341 /* Start writing out the symbol table. The first symbol is always a
5343 if (info
->strip
!= strip_all
5346 elfsym
.st_value
= 0;
5349 elfsym
.st_other
= 0;
5350 elfsym
.st_shndx
= SHN_UNDEF
;
5351 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5352 &elfsym
, bfd_und_section_ptr
))
5357 /* Some standard ELF linkers do this, but we don't because it causes
5358 bootstrap comparison failures. */
5359 /* Output a file symbol for the output file as the second symbol.
5360 We output this even if we are discarding local symbols, although
5361 I'm not sure if this is correct. */
5362 elfsym
.st_value
= 0;
5364 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5365 elfsym
.st_other
= 0;
5366 elfsym
.st_shndx
= SHN_ABS
;
5367 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
5368 &elfsym
, bfd_abs_section_ptr
))
5372 /* Output a symbol for each section. We output these even if we are
5373 discarding local symbols, since they are used for relocs. These
5374 symbols have no names. We store the index of each one in the
5375 index field of the section, so that we can find it again when
5376 outputting relocs. */
5377 if (info
->strip
!= strip_all
5381 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5382 elfsym
.st_other
= 0;
5383 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5385 o
= section_from_elf_index (abfd
, i
);
5387 o
->target_index
= bfd_get_symcount (abfd
);
5388 elfsym
.st_shndx
= i
;
5389 if (info
->relocateable
|| o
== NULL
)
5390 elfsym
.st_value
= 0;
5392 elfsym
.st_value
= o
->vma
;
5393 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5396 if (i
== SHN_LORESERVE
- 1)
5397 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
5401 /* Allocate some memory to hold information read in from the input
5403 if (max_contents_size
!= 0)
5405 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
5406 if (finfo
.contents
== NULL
)
5410 if (max_external_reloc_size
!= 0)
5412 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
5413 if (finfo
.external_relocs
== NULL
)
5417 if (max_internal_reloc_count
!= 0)
5419 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5420 amt
*= sizeof (Elf_Internal_Rela
);
5421 finfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
5422 if (finfo
.internal_relocs
== NULL
)
5426 if (max_sym_count
!= 0)
5428 amt
= max_sym_count
* sizeof (Elf_External_Sym
);
5429 finfo
.external_syms
= (Elf_External_Sym
*) bfd_malloc (amt
);
5430 if (finfo
.external_syms
== NULL
)
5433 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
5434 finfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
5435 if (finfo
.internal_syms
== NULL
)
5438 amt
= max_sym_count
* sizeof (long);
5439 finfo
.indices
= (long *) bfd_malloc (amt
);
5440 if (finfo
.indices
== NULL
)
5443 amt
= max_sym_count
* sizeof (asection
*);
5444 finfo
.sections
= (asection
**) bfd_malloc (amt
);
5445 if (finfo
.sections
== NULL
)
5449 if (max_sym_shndx_count
!= 0)
5451 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
5452 finfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
5453 if (finfo
.locsym_shndx
== NULL
)
5457 if (finfo
.first_tls_sec
)
5459 unsigned int align
= 0;
5460 bfd_vma base
= finfo
.first_tls_sec
->vma
, end
= 0;
5463 for (sec
= finfo
.first_tls_sec
;
5464 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
5467 bfd_vma size
= sec
->_raw_size
;
5469 if (bfd_get_section_alignment (abfd
, sec
) > align
)
5470 align
= bfd_get_section_alignment (abfd
, sec
);
5471 if (sec
->_raw_size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
5473 struct bfd_link_order
*o
;
5476 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
5477 if (size
< o
->offset
+ o
->size
)
5478 size
= o
->offset
+ o
->size
;
5480 end
= sec
->vma
+ size
;
5482 elf_hash_table (info
)->tls_segment
5483 = bfd_zalloc (abfd
, sizeof (struct elf_link_tls_segment
));
5484 if (elf_hash_table (info
)->tls_segment
== NULL
)
5486 elf_hash_table (info
)->tls_segment
->start
= base
;
5487 elf_hash_table (info
)->tls_segment
->size
= end
- base
;
5488 elf_hash_table (info
)->tls_segment
->align
= align
;
5491 /* Since ELF permits relocations to be against local symbols, we
5492 must have the local symbols available when we do the relocations.
5493 Since we would rather only read the local symbols once, and we
5494 would rather not keep them in memory, we handle all the
5495 relocations for a single input file at the same time.
5497 Unfortunately, there is no way to know the total number of local
5498 symbols until we have seen all of them, and the local symbol
5499 indices precede the global symbol indices. This means that when
5500 we are generating relocateable output, and we see a reloc against
5501 a global symbol, we can not know the symbol index until we have
5502 finished examining all the local symbols to see which ones we are
5503 going to output. To deal with this, we keep the relocations in
5504 memory, and don't output them until the end of the link. This is
5505 an unfortunate waste of memory, but I don't see a good way around
5506 it. Fortunately, it only happens when performing a relocateable
5507 link, which is not the common case. FIXME: If keep_memory is set
5508 we could write the relocs out and then read them again; I don't
5509 know how bad the memory loss will be. */
5511 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5512 sub
->output_has_begun
= FALSE
;
5513 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5515 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5517 if (p
->type
== bfd_indirect_link_order
5518 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
5519 == bfd_target_elf_flavour
)
5520 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
5522 if (! sub
->output_has_begun
)
5524 if (! elf_link_input_bfd (&finfo
, sub
))
5526 sub
->output_has_begun
= TRUE
;
5529 else if (p
->type
== bfd_section_reloc_link_order
5530 || p
->type
== bfd_symbol_reloc_link_order
)
5532 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
5537 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
5543 /* Output any global symbols that got converted to local in a
5544 version script or due to symbol visibility. We do this in a
5545 separate step since ELF requires all local symbols to appear
5546 prior to any global symbols. FIXME: We should only do this if
5547 some global symbols were, in fact, converted to become local.
5548 FIXME: Will this work correctly with the Irix 5 linker? */
5549 eoinfo
.failed
= FALSE
;
5550 eoinfo
.finfo
= &finfo
;
5551 eoinfo
.localsyms
= TRUE
;
5552 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5557 /* That wrote out all the local symbols. Finish up the symbol table
5558 with the global symbols. Even if we want to strip everything we
5559 can, we still need to deal with those global symbols that got
5560 converted to local in a version script. */
5562 /* The sh_info field records the index of the first non local symbol. */
5563 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
5566 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
5568 Elf_Internal_Sym sym
;
5569 Elf_External_Sym
*dynsym
=
5570 (Elf_External_Sym
*) finfo
.dynsym_sec
->contents
;
5571 long last_local
= 0;
5573 /* Write out the section symbols for the output sections. */
5580 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5583 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5586 Elf_External_Sym
*dest
;
5588 indx
= elf_section_data (s
)->this_idx
;
5589 BFD_ASSERT (indx
> 0);
5590 sym
.st_shndx
= indx
;
5591 sym
.st_value
= s
->vma
;
5592 dest
= dynsym
+ elf_section_data (s
)->dynindx
;
5593 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5596 last_local
= bfd_count_sections (abfd
);
5599 /* Write out the local dynsyms. */
5600 if (elf_hash_table (info
)->dynlocal
)
5602 struct elf_link_local_dynamic_entry
*e
;
5603 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
5606 Elf_External_Sym
*dest
;
5608 sym
.st_size
= e
->isym
.st_size
;
5609 sym
.st_other
= e
->isym
.st_other
;
5611 /* Copy the internal symbol as is.
5612 Note that we saved a word of storage and overwrote
5613 the original st_name with the dynstr_index. */
5616 if (e
->isym
.st_shndx
!= SHN_UNDEF
5617 && (e
->isym
.st_shndx
< SHN_LORESERVE
5618 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
5620 s
= bfd_section_from_elf_index (e
->input_bfd
,
5624 elf_section_data (s
->output_section
)->this_idx
;
5625 sym
.st_value
= (s
->output_section
->vma
5627 + e
->isym
.st_value
);
5630 if (last_local
< e
->dynindx
)
5631 last_local
= e
->dynindx
;
5633 dest
= dynsym
+ e
->dynindx
;
5634 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5638 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
5642 /* We get the global symbols from the hash table. */
5643 eoinfo
.failed
= FALSE
;
5644 eoinfo
.localsyms
= FALSE
;
5645 eoinfo
.finfo
= &finfo
;
5646 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5651 /* If backend needs to output some symbols not present in the hash
5652 table, do it now. */
5653 if (bed
->elf_backend_output_arch_syms
)
5655 typedef bfd_boolean (*out_sym_func
)
5656 PARAMS ((PTR
, const char *, Elf_Internal_Sym
*, asection
*));
5658 if (! ((*bed
->elf_backend_output_arch_syms
)
5659 (abfd
, info
, (PTR
) &finfo
, (out_sym_func
) elf_link_output_sym
)))
5663 /* Flush all symbols to the file. */
5664 if (! elf_link_flush_output_syms (&finfo
))
5667 /* Now we know the size of the symtab section. */
5668 off
+= symtab_hdr
->sh_size
;
5670 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
5671 if (symtab_shndx_hdr
->sh_name
!= 0)
5673 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
5674 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
5675 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
5676 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
5677 symtab_shndx_hdr
->sh_size
= amt
;
5679 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
5682 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
5683 || (bfd_bwrite ((PTR
) finfo
.symshndxbuf
, amt
, abfd
) != amt
))
5688 /* Finish up and write out the symbol string table (.strtab)
5690 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5691 /* sh_name was set in prep_headers. */
5692 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5693 symstrtab_hdr
->sh_flags
= 0;
5694 symstrtab_hdr
->sh_addr
= 0;
5695 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
5696 symstrtab_hdr
->sh_entsize
= 0;
5697 symstrtab_hdr
->sh_link
= 0;
5698 symstrtab_hdr
->sh_info
= 0;
5699 /* sh_offset is set just below. */
5700 symstrtab_hdr
->sh_addralign
= 1;
5702 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
5703 elf_tdata (abfd
)->next_file_pos
= off
;
5705 if (bfd_get_symcount (abfd
) > 0)
5707 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
5708 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
5712 /* Adjust the relocs to have the correct symbol indices. */
5713 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5715 if ((o
->flags
& SEC_RELOC
) == 0)
5718 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
5719 elf_section_data (o
)->rel_count
,
5720 elf_section_data (o
)->rel_hashes
);
5721 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
5722 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
5723 elf_section_data (o
)->rel_count2
,
5724 (elf_section_data (o
)->rel_hashes
5725 + elf_section_data (o
)->rel_count
));
5727 /* Set the reloc_count field to 0 to prevent write_relocs from
5728 trying to swap the relocs out itself. */
5732 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
5733 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
5735 /* If we are linking against a dynamic object, or generating a
5736 shared library, finish up the dynamic linking information. */
5739 Elf_External_Dyn
*dyncon
, *dynconend
;
5741 /* Fix up .dynamic entries. */
5742 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
5743 BFD_ASSERT (o
!= NULL
);
5745 dyncon
= (Elf_External_Dyn
*) o
->contents
;
5746 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
5747 for (; dyncon
< dynconend
; dyncon
++)
5749 Elf_Internal_Dyn dyn
;
5753 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
5760 if (relativecount
> 0 && dyncon
+ 1 < dynconend
)
5762 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
5764 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
5765 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
5768 if (dyn
.d_tag
!= DT_NULL
)
5770 dyn
.d_un
.d_val
= relativecount
;
5771 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5777 name
= info
->init_function
;
5780 name
= info
->fini_function
;
5783 struct elf_link_hash_entry
*h
;
5785 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
5786 FALSE
, FALSE
, TRUE
);
5788 && (h
->root
.type
== bfd_link_hash_defined
5789 || h
->root
.type
== bfd_link_hash_defweak
))
5791 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
5792 o
= h
->root
.u
.def
.section
;
5793 if (o
->output_section
!= NULL
)
5794 dyn
.d_un
.d_val
+= (o
->output_section
->vma
5795 + o
->output_offset
);
5798 /* The symbol is imported from another shared
5799 library and does not apply to this one. */
5803 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5808 case DT_PREINIT_ARRAYSZ
:
5809 name
= ".preinit_array";
5811 case DT_INIT_ARRAYSZ
:
5812 name
= ".init_array";
5814 case DT_FINI_ARRAYSZ
:
5815 name
= ".fini_array";
5817 o
= bfd_get_section_by_name (abfd
, name
);
5820 (*_bfd_error_handler
)
5821 (_("%s: could not find output section %s"),
5822 bfd_get_filename (abfd
), name
);
5825 if (o
->_raw_size
== 0)
5826 (*_bfd_error_handler
)
5827 (_("warning: %s section has zero size"), name
);
5828 dyn
.d_un
.d_val
= o
->_raw_size
;
5829 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5832 case DT_PREINIT_ARRAY
:
5833 name
= ".preinit_array";
5836 name
= ".init_array";
5839 name
= ".fini_array";
5852 name
= ".gnu.version_d";
5855 name
= ".gnu.version_r";
5858 name
= ".gnu.version";
5860 o
= bfd_get_section_by_name (abfd
, name
);
5863 (*_bfd_error_handler
)
5864 (_("%s: could not find output section %s"),
5865 bfd_get_filename (abfd
), name
);
5868 dyn
.d_un
.d_ptr
= o
->vma
;
5869 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5876 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
5881 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5883 Elf_Internal_Shdr
*hdr
;
5885 hdr
= elf_elfsections (abfd
)[i
];
5886 if (hdr
->sh_type
== type
5887 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
5889 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
5890 dyn
.d_un
.d_val
+= hdr
->sh_size
;
5893 if (dyn
.d_un
.d_val
== 0
5894 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
5895 dyn
.d_un
.d_val
= hdr
->sh_addr
;
5899 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5905 /* If we have created any dynamic sections, then output them. */
5908 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
5911 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
5913 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5914 || o
->_raw_size
== 0
5915 || o
->output_section
== bfd_abs_section_ptr
)
5917 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
5919 /* At this point, we are only interested in sections
5920 created by elf_link_create_dynamic_sections. */
5923 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
5925 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
5927 if (! bfd_set_section_contents (abfd
, o
->output_section
,
5929 (file_ptr
) o
->output_offset
,
5935 /* The contents of the .dynstr section are actually in a
5937 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
5938 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
5939 || ! _bfd_elf_strtab_emit (abfd
,
5940 elf_hash_table (info
)->dynstr
))
5946 if (info
->relocateable
)
5948 bfd_boolean failed
= FALSE
;
5950 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
5955 /* If we have optimized stabs strings, output them. */
5956 if (elf_hash_table (info
)->stab_info
!= NULL
)
5958 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
5962 if (info
->eh_frame_hdr
)
5964 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
5968 if (finfo
.symstrtab
!= NULL
)
5969 _bfd_stringtab_free (finfo
.symstrtab
);
5970 if (finfo
.contents
!= NULL
)
5971 free (finfo
.contents
);
5972 if (finfo
.external_relocs
!= NULL
)
5973 free (finfo
.external_relocs
);
5974 if (finfo
.internal_relocs
!= NULL
)
5975 free (finfo
.internal_relocs
);
5976 if (finfo
.external_syms
!= NULL
)
5977 free (finfo
.external_syms
);
5978 if (finfo
.locsym_shndx
!= NULL
)
5979 free (finfo
.locsym_shndx
);
5980 if (finfo
.internal_syms
!= NULL
)
5981 free (finfo
.internal_syms
);
5982 if (finfo
.indices
!= NULL
)
5983 free (finfo
.indices
);
5984 if (finfo
.sections
!= NULL
)
5985 free (finfo
.sections
);
5986 if (finfo
.symbuf
!= NULL
)
5987 free (finfo
.symbuf
);
5988 if (finfo
.symshndxbuf
!= NULL
)
5989 free (finfo
.symshndxbuf
);
5990 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5992 if ((o
->flags
& SEC_RELOC
) != 0
5993 && elf_section_data (o
)->rel_hashes
!= NULL
)
5994 free (elf_section_data (o
)->rel_hashes
);
5997 elf_tdata (abfd
)->linker
= TRUE
;
6002 if (finfo
.symstrtab
!= NULL
)
6003 _bfd_stringtab_free (finfo
.symstrtab
);
6004 if (finfo
.contents
!= NULL
)
6005 free (finfo
.contents
);
6006 if (finfo
.external_relocs
!= NULL
)
6007 free (finfo
.external_relocs
);
6008 if (finfo
.internal_relocs
!= NULL
)
6009 free (finfo
.internal_relocs
);
6010 if (finfo
.external_syms
!= NULL
)
6011 free (finfo
.external_syms
);
6012 if (finfo
.locsym_shndx
!= NULL
)
6013 free (finfo
.locsym_shndx
);
6014 if (finfo
.internal_syms
!= NULL
)
6015 free (finfo
.internal_syms
);
6016 if (finfo
.indices
!= NULL
)
6017 free (finfo
.indices
);
6018 if (finfo
.sections
!= NULL
)
6019 free (finfo
.sections
);
6020 if (finfo
.symbuf
!= NULL
)
6021 free (finfo
.symbuf
);
6022 if (finfo
.symshndxbuf
!= NULL
)
6023 free (finfo
.symshndxbuf
);
6024 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6026 if ((o
->flags
& SEC_RELOC
) != 0
6027 && elf_section_data (o
)->rel_hashes
!= NULL
)
6028 free (elf_section_data (o
)->rel_hashes
);
6034 /* Add a symbol to the output symbol table. */
6037 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
6038 struct elf_final_link_info
*finfo
;
6040 Elf_Internal_Sym
*elfsym
;
6041 asection
*input_sec
;
6043 Elf_External_Sym
*dest
;
6044 Elf_External_Sym_Shndx
*destshndx
;
6045 bfd_boolean (*output_symbol_hook
)
6046 PARAMS ((bfd
*, struct bfd_link_info
*info
, const char *,
6047 Elf_Internal_Sym
*, asection
*));
6049 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
6050 elf_backend_link_output_symbol_hook
;
6051 if (output_symbol_hook
!= NULL
)
6053 if (! ((*output_symbol_hook
)
6054 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
6058 if (name
== (const char *) NULL
|| *name
== '\0')
6059 elfsym
->st_name
= 0;
6060 else if (input_sec
->flags
& SEC_EXCLUDE
)
6061 elfsym
->st_name
= 0;
6064 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
6066 if (elfsym
->st_name
== (unsigned long) -1)
6070 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
6072 if (! elf_link_flush_output_syms (finfo
))
6076 dest
= finfo
->symbuf
+ finfo
->symbuf_count
;
6077 destshndx
= finfo
->symshndxbuf
;
6078 if (destshndx
!= NULL
)
6080 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
6084 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
6085 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
6086 if (destshndx
== NULL
)
6088 memset ((char *) destshndx
+ amt
, 0, amt
);
6089 finfo
->shndxbuf_size
*= 2;
6091 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
6094 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
, (PTR
) dest
, (PTR
) destshndx
);
6095 finfo
->symbuf_count
+= 1;
6096 bfd_get_symcount (finfo
->output_bfd
) += 1;
6101 /* Flush the output symbols to the file. */
6104 elf_link_flush_output_syms (finfo
)
6105 struct elf_final_link_info
*finfo
;
6107 if (finfo
->symbuf_count
> 0)
6109 Elf_Internal_Shdr
*hdr
;
6113 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6114 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6115 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
6116 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6117 || bfd_bwrite ((PTR
) finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6120 hdr
->sh_size
+= amt
;
6121 finfo
->symbuf_count
= 0;
6127 /* Adjust all external symbols pointing into SEC_MERGE sections
6128 to reflect the object merging within the sections. */
6131 elf_link_sec_merge_syms (h
, data
)
6132 struct elf_link_hash_entry
*h
;
6137 if (h
->root
.type
== bfd_link_hash_warning
)
6138 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6140 if ((h
->root
.type
== bfd_link_hash_defined
6141 || h
->root
.type
== bfd_link_hash_defweak
)
6142 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
6143 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
6145 bfd
*output_bfd
= (bfd
*) data
;
6147 h
->root
.u
.def
.value
=
6148 _bfd_merged_section_offset (output_bfd
,
6149 &h
->root
.u
.def
.section
,
6150 elf_section_data (sec
)->sec_info
,
6151 h
->root
.u
.def
.value
, (bfd_vma
) 0);
6157 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6158 allowing an unsatisfied unversioned symbol in the DSO to match a
6159 versioned symbol that would normally require an explicit version. */
6162 elf_link_check_versioned_symbol (info
, h
)
6163 struct bfd_link_info
*info
;
6164 struct elf_link_hash_entry
*h
;
6166 bfd
*undef_bfd
= h
->root
.u
.undef
.abfd
;
6167 struct elf_link_loaded_list
*loaded
;
6169 if ((undef_bfd
->flags
& DYNAMIC
) == 0
6170 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
6171 || elf_dt_soname (undef_bfd
) == NULL
)
6174 for (loaded
= elf_hash_table (info
)->loaded
;
6176 loaded
= loaded
->next
)
6179 Elf_Internal_Shdr
*hdr
;
6180 bfd_size_type symcount
;
6181 bfd_size_type extsymcount
;
6182 bfd_size_type extsymoff
;
6183 Elf_Internal_Shdr
*versymhdr
;
6184 Elf_Internal_Sym
*isym
;
6185 Elf_Internal_Sym
*isymend
;
6186 Elf_Internal_Sym
*isymbuf
;
6187 Elf_External_Versym
*ever
;
6188 Elf_External_Versym
*extversym
;
6190 input
= loaded
->abfd
;
6192 /* We check each DSO for a possible hidden versioned definition. */
6193 if (input
== undef_bfd
6194 || (input
->flags
& DYNAMIC
) == 0
6195 || elf_dynversym (input
) == 0)
6198 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6200 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6201 if (elf_bad_symtab (input
))
6203 extsymcount
= symcount
;
6208 extsymcount
= symcount
- hdr
->sh_info
;
6209 extsymoff
= hdr
->sh_info
;
6212 if (extsymcount
== 0)
6215 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6217 if (isymbuf
== NULL
)
6220 /* Read in any version definitions. */
6221 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6222 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
6223 if (extversym
== NULL
)
6226 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6227 || (bfd_bread ((PTR
) extversym
, versymhdr
->sh_size
, input
)
6228 != versymhdr
->sh_size
))
6236 ever
= extversym
+ extsymoff
;
6237 isymend
= isymbuf
+ extsymcount
;
6238 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6241 Elf_Internal_Versym iver
;
6243 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6244 || isym
->st_shndx
== SHN_UNDEF
)
6247 name
= bfd_elf_string_from_elf_section (input
,
6250 if (strcmp (name
, h
->root
.root
.string
) != 0)
6253 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6255 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6257 /* If we have a non-hidden versioned sym, then it should
6258 have provided a definition for the undefined sym. */
6262 if ((iver
.vs_vers
& VERSYM_VERSION
) == 2)
6264 /* This is the oldest (default) sym. We can use it. */
6278 /* Add an external symbol to the symbol table. This is called from
6279 the hash table traversal routine. When generating a shared object,
6280 we go through the symbol table twice. The first time we output
6281 anything that might have been forced to local scope in a version
6282 script. The second time we output the symbols that are still
6286 elf_link_output_extsym (h
, data
)
6287 struct elf_link_hash_entry
*h
;
6290 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
6291 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6293 Elf_Internal_Sym sym
;
6294 asection
*input_sec
;
6296 if (h
->root
.type
== bfd_link_hash_warning
)
6298 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6299 if (h
->root
.type
== bfd_link_hash_new
)
6303 /* Decide whether to output this symbol in this pass. */
6304 if (eoinfo
->localsyms
)
6306 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6311 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6315 /* If we are not creating a shared library, and this symbol is
6316 referenced by a shared library but is not defined anywhere, then
6317 warn that it is undefined. If we do not do this, the runtime
6318 linker will complain that the symbol is undefined when the
6319 program is run. We don't have to worry about symbols that are
6320 referenced by regular files, because we will already have issued
6321 warnings for them. */
6322 if (! finfo
->info
->relocateable
6323 && (! finfo
->info
->shared
|| ! finfo
->info
->allow_shlib_undefined
)
6324 && h
->root
.type
== bfd_link_hash_undefined
6325 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
6326 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
6327 && ! elf_link_check_versioned_symbol (finfo
->info
, h
))
6329 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6330 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6331 (asection
*) NULL
, (bfd_vma
) 0, TRUE
)))
6333 eoinfo
->failed
= TRUE
;
6338 /* We should also warn if a forced local symbol is referenced from
6339 shared libraries. */
6340 if (! finfo
->info
->relocateable
6341 && (! finfo
->info
->shared
|| ! finfo
->info
->allow_shlib_undefined
)
6342 && (h
->elf_link_hash_flags
6343 & (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
6344 | ELF_LINK_DYNAMIC_DEF
| ELF_LINK_DYNAMIC_WEAK
))
6345 == (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
))
6347 (*_bfd_error_handler
)
6348 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
6349 bfd_get_filename (finfo
->output_bfd
),
6350 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6352 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6353 ? "hidden" : "local",
6354 h
->root
.root
.string
,
6355 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
6356 eoinfo
->failed
= TRUE
;
6360 /* We don't want to output symbols that have never been mentioned by
6361 a regular file, or that we have been told to strip. However, if
6362 h->indx is set to -2, the symbol is used by a reloc and we must
6366 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
6367 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
6368 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
6369 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
6371 else if (finfo
->info
->strip
== strip_all
)
6373 else if (finfo
->info
->strip
== strip_some
6374 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6375 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6377 else if (finfo
->info
->strip_discarded
6378 && (h
->root
.type
== bfd_link_hash_defined
6379 || h
->root
.type
== bfd_link_hash_defweak
)
6380 && elf_discarded_section (h
->root
.u
.def
.section
))
6385 /* If we're stripping it, and it's not a dynamic symbol, there's
6386 nothing else to do unless it is a forced local symbol. */
6389 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6393 sym
.st_size
= h
->size
;
6394 sym
.st_other
= h
->other
;
6395 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6396 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6397 else if (h
->root
.type
== bfd_link_hash_undefweak
6398 || h
->root
.type
== bfd_link_hash_defweak
)
6399 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6401 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6403 switch (h
->root
.type
)
6406 case bfd_link_hash_new
:
6407 case bfd_link_hash_warning
:
6411 case bfd_link_hash_undefined
:
6412 case bfd_link_hash_undefweak
:
6413 input_sec
= bfd_und_section_ptr
;
6414 sym
.st_shndx
= SHN_UNDEF
;
6417 case bfd_link_hash_defined
:
6418 case bfd_link_hash_defweak
:
6420 input_sec
= h
->root
.u
.def
.section
;
6421 if (input_sec
->output_section
!= NULL
)
6424 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6425 input_sec
->output_section
);
6426 if (sym
.st_shndx
== SHN_BAD
)
6428 (*_bfd_error_handler
)
6429 (_("%s: could not find output section %s for input section %s"),
6430 bfd_get_filename (finfo
->output_bfd
),
6431 input_sec
->output_section
->name
,
6433 eoinfo
->failed
= TRUE
;
6437 /* ELF symbols in relocateable files are section relative,
6438 but in nonrelocateable files they are virtual
6440 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6441 if (! finfo
->info
->relocateable
)
6443 sym
.st_value
+= input_sec
->output_section
->vma
;
6444 if (h
->type
== STT_TLS
)
6446 /* STT_TLS symbols are relative to PT_TLS segment
6448 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6449 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
6455 BFD_ASSERT (input_sec
->owner
== NULL
6456 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6457 sym
.st_shndx
= SHN_UNDEF
;
6458 input_sec
= bfd_und_section_ptr
;
6463 case bfd_link_hash_common
:
6464 input_sec
= h
->root
.u
.c
.p
->section
;
6465 sym
.st_shndx
= SHN_COMMON
;
6466 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6469 case bfd_link_hash_indirect
:
6470 /* These symbols are created by symbol versioning. They point
6471 to the decorated version of the name. For example, if the
6472 symbol foo@@GNU_1.2 is the default, which should be used when
6473 foo is used with no version, then we add an indirect symbol
6474 foo which points to foo@@GNU_1.2. We ignore these symbols,
6475 since the indirected symbol is already in the hash table. */
6479 /* Give the processor backend a chance to tweak the symbol value,
6480 and also to finish up anything that needs to be done for this
6481 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6482 forced local syms when non-shared is due to a historical quirk. */
6483 if ((h
->dynindx
!= -1
6484 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6485 && (finfo
->info
->shared
6486 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6487 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6489 struct elf_backend_data
*bed
;
6491 bed
= get_elf_backend_data (finfo
->output_bfd
);
6492 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6493 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6495 eoinfo
->failed
= TRUE
;
6500 /* If we are marking the symbol as undefined, and there are no
6501 non-weak references to this symbol from a regular object, then
6502 mark the symbol as weak undefined; if there are non-weak
6503 references, mark the symbol as strong. We can't do this earlier,
6504 because it might not be marked as undefined until the
6505 finish_dynamic_symbol routine gets through with it. */
6506 if (sym
.st_shndx
== SHN_UNDEF
6507 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
6508 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6509 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6513 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
6514 bindtype
= STB_GLOBAL
;
6516 bindtype
= STB_WEAK
;
6517 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6520 /* If a non-weak symbol with non-default visibility is not defined
6521 locally, it is a fatal error. */
6522 if (! finfo
->info
->relocateable
6523 && ELF_ST_VISIBILITY (sym
.st_other
)
6524 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6525 && h
->root
.type
!= bfd_link_hash_undefweak
6526 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6528 (*_bfd_error_handler
)
6529 (_("%s: %s symbol `%s' isn't defined"),
6530 bfd_get_filename (finfo
->output_bfd
),
6531 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6533 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6534 ? "internal" : "hidden",
6535 h
->root
.root
.string
);
6536 eoinfo
->failed
= TRUE
;
6540 /* If this symbol should be put in the .dynsym section, then put it
6541 there now. We already know the symbol index. We also fill in
6542 the entry in the .hash section. */
6543 if (h
->dynindx
!= -1
6544 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6548 size_t hash_entry_size
;
6549 bfd_byte
*bucketpos
;
6551 Elf_External_Sym
*esym
;
6553 sym
.st_name
= h
->dynstr_index
;
6554 esym
= (Elf_External_Sym
*) finfo
->dynsym_sec
->contents
+ h
->dynindx
;
6555 elf_swap_symbol_out (finfo
->output_bfd
, &sym
, (PTR
) esym
, (PTR
) 0);
6557 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6558 bucket
= h
->elf_hash_value
% bucketcount
;
6560 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6561 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6562 + (bucket
+ 2) * hash_entry_size
);
6563 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6564 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, (bfd_vma
) h
->dynindx
,
6566 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6567 ((bfd_byte
*) finfo
->hash_sec
->contents
6568 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6570 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6572 Elf_Internal_Versym iversym
;
6573 Elf_External_Versym
*eversym
;
6575 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6577 if (h
->verinfo
.verdef
== NULL
)
6578 iversym
.vs_vers
= 0;
6580 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6584 if (h
->verinfo
.vertree
== NULL
)
6585 iversym
.vs_vers
= 1;
6587 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6590 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
6591 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6593 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6594 eversym
+= h
->dynindx
;
6595 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6599 /* If we're stripping it, then it was just a dynamic symbol, and
6600 there's nothing else to do. */
6601 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6604 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6606 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
6608 eoinfo
->failed
= TRUE
;
6615 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6616 originated from the section given by INPUT_REL_HDR) to the
6620 elf_link_output_relocs (output_bfd
, input_section
, input_rel_hdr
,
6623 asection
*input_section
;
6624 Elf_Internal_Shdr
*input_rel_hdr
;
6625 Elf_Internal_Rela
*internal_relocs
;
6627 Elf_Internal_Rela
*irela
;
6628 Elf_Internal_Rela
*irelaend
;
6630 Elf_Internal_Shdr
*output_rel_hdr
;
6631 asection
*output_section
;
6632 unsigned int *rel_countp
= NULL
;
6633 struct elf_backend_data
*bed
;
6634 void (*swap_out
) PARAMS ((bfd
*, const Elf_Internal_Rela
*, bfd_byte
*));
6636 output_section
= input_section
->output_section
;
6637 output_rel_hdr
= NULL
;
6639 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
6640 == input_rel_hdr
->sh_entsize
)
6642 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
6643 rel_countp
= &elf_section_data (output_section
)->rel_count
;
6645 else if (elf_section_data (output_section
)->rel_hdr2
6646 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
6647 == input_rel_hdr
->sh_entsize
))
6649 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
6650 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
6654 (*_bfd_error_handler
)
6655 (_("%s: relocation size mismatch in %s section %s"),
6656 bfd_get_filename (output_bfd
),
6657 bfd_archive_filename (input_section
->owner
),
6658 input_section
->name
);
6659 bfd_set_error (bfd_error_wrong_object_format
);
6663 bed
= get_elf_backend_data (output_bfd
);
6664 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
6665 swap_out
= bed
->s
->swap_reloc_out
;
6666 else if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
))
6667 swap_out
= bed
->s
->swap_reloca_out
;
6671 erel
= output_rel_hdr
->contents
;
6672 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
6673 irela
= internal_relocs
;
6674 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
6675 * bed
->s
->int_rels_per_ext_rel
);
6676 while (irela
< irelaend
)
6678 (*swap_out
) (output_bfd
, irela
, erel
);
6679 irela
+= bed
->s
->int_rels_per_ext_rel
;
6680 erel
+= input_rel_hdr
->sh_entsize
;
6683 /* Bump the counter, so that we know where to add the next set of
6685 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
6690 /* Link an input file into the linker output file. This function
6691 handles all the sections and relocations of the input file at once.
6692 This is so that we only have to read the local symbols once, and
6693 don't have to keep them in memory. */
6696 elf_link_input_bfd (finfo
, input_bfd
)
6697 struct elf_final_link_info
*finfo
;
6700 bfd_boolean (*relocate_section
)
6701 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6702 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
6704 Elf_Internal_Shdr
*symtab_hdr
;
6707 Elf_Internal_Sym
*isymbuf
;
6708 Elf_Internal_Sym
*isym
;
6709 Elf_Internal_Sym
*isymend
;
6711 asection
**ppsection
;
6713 struct elf_backend_data
*bed
;
6714 bfd_boolean emit_relocs
;
6715 struct elf_link_hash_entry
**sym_hashes
;
6717 output_bfd
= finfo
->output_bfd
;
6718 bed
= get_elf_backend_data (output_bfd
);
6719 relocate_section
= bed
->elf_backend_relocate_section
;
6721 /* If this is a dynamic object, we don't want to do anything here:
6722 we don't want the local symbols, and we don't want the section
6724 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6727 emit_relocs
= (finfo
->info
->relocateable
6728 || finfo
->info
->emitrelocations
6729 || bed
->elf_backend_emit_relocs
);
6731 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6732 if (elf_bad_symtab (input_bfd
))
6734 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6739 locsymcount
= symtab_hdr
->sh_info
;
6740 extsymoff
= symtab_hdr
->sh_info
;
6743 /* Read the local symbols. */
6744 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6745 if (isymbuf
== NULL
&& locsymcount
!= 0)
6747 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6748 finfo
->internal_syms
,
6749 finfo
->external_syms
,
6750 finfo
->locsym_shndx
);
6751 if (isymbuf
== NULL
)
6755 /* Find local symbol sections and adjust values of symbols in
6756 SEC_MERGE sections. Write out those local symbols we know are
6757 going into the output file. */
6758 isymend
= isymbuf
+ locsymcount
;
6759 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6761 isym
++, pindex
++, ppsection
++)
6765 Elf_Internal_Sym osym
;
6769 if (elf_bad_symtab (input_bfd
))
6771 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6778 if (isym
->st_shndx
== SHN_UNDEF
)
6779 isec
= bfd_und_section_ptr
;
6780 else if (isym
->st_shndx
< SHN_LORESERVE
6781 || isym
->st_shndx
> SHN_HIRESERVE
)
6783 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
6785 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6786 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6788 _bfd_merged_section_offset (output_bfd
, &isec
,
6789 elf_section_data (isec
)->sec_info
,
6790 isym
->st_value
, (bfd_vma
) 0);
6792 else if (isym
->st_shndx
== SHN_ABS
)
6793 isec
= bfd_abs_section_ptr
;
6794 else if (isym
->st_shndx
== SHN_COMMON
)
6795 isec
= bfd_com_section_ptr
;
6804 /* Don't output the first, undefined, symbol. */
6805 if (ppsection
== finfo
->sections
)
6808 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6810 /* We never output section symbols. Instead, we use the
6811 section symbol of the corresponding section in the output
6816 /* If we are stripping all symbols, we don't want to output this
6818 if (finfo
->info
->strip
== strip_all
)
6821 /* If we are discarding all local symbols, we don't want to
6822 output this one. If we are generating a relocateable output
6823 file, then some of the local symbols may be required by
6824 relocs; we output them below as we discover that they are
6826 if (finfo
->info
->discard
== discard_all
)
6829 /* If this symbol is defined in a section which we are
6830 discarding, we don't need to keep it, but note that
6831 linker_mark is only reliable for sections that have contents.
6832 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6833 as well as linker_mark. */
6834 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6836 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6837 || (! finfo
->info
->relocateable
6838 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6841 /* Get the name of the symbol. */
6842 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6847 /* See if we are discarding symbols with this name. */
6848 if ((finfo
->info
->strip
== strip_some
6849 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6851 || (((finfo
->info
->discard
== discard_sec_merge
6852 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocateable
)
6853 || finfo
->info
->discard
== discard_l
)
6854 && bfd_is_local_label_name (input_bfd
, name
)))
6857 /* If we get here, we are going to output this symbol. */
6861 /* Adjust the section index for the output file. */
6862 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6863 isec
->output_section
);
6864 if (osym
.st_shndx
== SHN_BAD
)
6867 *pindex
= bfd_get_symcount (output_bfd
);
6869 /* ELF symbols in relocateable files are section relative, but
6870 in executable files they are virtual addresses. Note that
6871 this code assumes that all ELF sections have an associated
6872 BFD section with a reasonable value for output_offset; below
6873 we assume that they also have a reasonable value for
6874 output_section. Any special sections must be set up to meet
6875 these requirements. */
6876 osym
.st_value
+= isec
->output_offset
;
6877 if (! finfo
->info
->relocateable
)
6879 osym
.st_value
+= isec
->output_section
->vma
;
6880 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6882 /* STT_TLS symbols are relative to PT_TLS segment base. */
6883 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6884 osym
.st_value
-= finfo
->first_tls_sec
->vma
;
6888 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
6892 /* Relocate the contents of each section. */
6893 sym_hashes
= elf_sym_hashes (input_bfd
);
6894 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6898 if (! o
->linker_mark
)
6900 /* This section was omitted from the link. */
6904 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6905 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6908 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6910 /* Section was created by elf_link_create_dynamic_sections
6915 /* Get the contents of the section. They have been cached by a
6916 relaxation routine. Note that o is a section in an input
6917 file, so the contents field will not have been set by any of
6918 the routines which work on output files. */
6919 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6920 contents
= elf_section_data (o
)->this_hdr
.contents
;
6923 contents
= finfo
->contents
;
6924 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
6925 (file_ptr
) 0, o
->_raw_size
))
6929 if ((o
->flags
& SEC_RELOC
) != 0)
6931 Elf_Internal_Rela
*internal_relocs
;
6933 /* Get the swapped relocs. */
6934 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
6935 (input_bfd
, o
, finfo
->external_relocs
,
6936 finfo
->internal_relocs
, FALSE
));
6937 if (internal_relocs
== NULL
6938 && o
->reloc_count
> 0)
6941 /* Run through the relocs looking for any against symbols
6942 from discarded sections and section symbols from
6943 removed link-once sections. Complain about relocs
6944 against discarded sections. Zero relocs against removed
6945 link-once sections. */
6946 if (!finfo
->info
->relocateable
6947 && !elf_section_ignore_discarded_relocs (o
))
6949 Elf_Internal_Rela
*rel
, *relend
;
6951 rel
= internal_relocs
;
6952 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6953 for ( ; rel
< relend
; rel
++)
6955 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
6957 if (r_symndx
>= locsymcount
6958 || (elf_bad_symtab (input_bfd
)
6959 && finfo
->sections
[r_symndx
] == NULL
))
6961 struct elf_link_hash_entry
*h
;
6963 h
= sym_hashes
[r_symndx
- extsymoff
];
6964 while (h
->root
.type
== bfd_link_hash_indirect
6965 || h
->root
.type
== bfd_link_hash_warning
)
6966 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6968 /* Complain if the definition comes from a
6969 discarded section. */
6970 if ((h
->root
.type
== bfd_link_hash_defined
6971 || h
->root
.type
== bfd_link_hash_defweak
)
6972 && elf_discarded_section (h
->root
.u
.def
.section
))
6974 if ((o
->flags
& SEC_DEBUGGING
) != 0)
6976 BFD_ASSERT (r_symndx
!= 0);
6977 memset (rel
, 0, sizeof (*rel
));
6981 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6982 (finfo
->info
, h
->root
.root
.string
,
6983 input_bfd
, o
, rel
->r_offset
,
6991 asection
*sec
= finfo
->sections
[r_symndx
];
6993 if (sec
!= NULL
&& elf_discarded_section (sec
))
6995 if ((o
->flags
& SEC_DEBUGGING
) != 0
6996 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
6998 BFD_ASSERT (r_symndx
!= 0);
7000 = ELF_R_INFO (0, ELF_R_TYPE (rel
->r_info
));
7007 = _("local symbols in discarded section %s");
7009 = strlen (sec
->name
) + strlen (msg
) - 1;
7010 char *buf
= (char *) bfd_malloc (amt
);
7013 sprintf (buf
, msg
, sec
->name
);
7015 buf
= (char *) sec
->name
;
7016 ok
= (*finfo
->info
->callbacks
7017 ->undefined_symbol
) (finfo
->info
, buf
,
7021 if (buf
!= sec
->name
)
7031 /* Relocate the section by invoking a back end routine.
7033 The back end routine is responsible for adjusting the
7034 section contents as necessary, and (if using Rela relocs
7035 and generating a relocateable output file) adjusting the
7036 reloc addend as necessary.
7038 The back end routine does not have to worry about setting
7039 the reloc address or the reloc symbol index.
7041 The back end routine is given a pointer to the swapped in
7042 internal symbols, and can access the hash table entries
7043 for the external symbols via elf_sym_hashes (input_bfd).
7045 When generating relocateable output, the back end routine
7046 must handle STB_LOCAL/STT_SECTION symbols specially. The
7047 output symbol is going to be a section symbol
7048 corresponding to the output section, which will require
7049 the addend to be adjusted. */
7051 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
7052 input_bfd
, o
, contents
,
7060 Elf_Internal_Rela
*irela
;
7061 Elf_Internal_Rela
*irelaend
;
7062 bfd_vma last_offset
;
7063 struct elf_link_hash_entry
**rel_hash
;
7064 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
7065 unsigned int next_erel
;
7066 bfd_boolean (*reloc_emitter
)
7067 PARAMS ((bfd
*, asection
*, Elf_Internal_Shdr
*,
7068 Elf_Internal_Rela
*));
7069 bfd_boolean rela_normal
;
7071 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7072 rela_normal
= (bed
->rela_normal
7073 && (input_rel_hdr
->sh_entsize
7074 == sizeof (Elf_External_Rela
)));
7076 /* Adjust the reloc addresses and symbol indices. */
7078 irela
= internal_relocs
;
7079 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7080 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7081 + elf_section_data (o
->output_section
)->rel_count
7082 + elf_section_data (o
->output_section
)->rel_count2
);
7083 last_offset
= o
->output_offset
;
7084 if (!finfo
->info
->relocateable
)
7085 last_offset
+= o
->output_section
->vma
;
7086 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7088 unsigned long r_symndx
;
7090 Elf_Internal_Sym sym
;
7092 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7098 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
7101 if (irela
->r_offset
>= (bfd_vma
) -2)
7103 /* This is a reloc for a deleted entry or somesuch.
7104 Turn it into an R_*_NONE reloc, at the same
7105 offset as the last reloc. elf_eh_frame.c and
7106 elf_bfd_discard_info rely on reloc offsets
7108 irela
->r_offset
= last_offset
;
7110 irela
->r_addend
= 0;
7114 irela
->r_offset
+= o
->output_offset
;
7116 /* Relocs in an executable have to be virtual addresses. */
7117 if (!finfo
->info
->relocateable
)
7118 irela
->r_offset
+= o
->output_section
->vma
;
7120 last_offset
= irela
->r_offset
;
7122 r_symndx
= ELF_R_SYM (irela
->r_info
);
7123 if (r_symndx
== STN_UNDEF
)
7126 if (r_symndx
>= locsymcount
7127 || (elf_bad_symtab (input_bfd
)
7128 && finfo
->sections
[r_symndx
] == NULL
))
7130 struct elf_link_hash_entry
*rh
;
7133 /* This is a reloc against a global symbol. We
7134 have not yet output all the local symbols, so
7135 we do not know the symbol index of any global
7136 symbol. We set the rel_hash entry for this
7137 reloc to point to the global hash table entry
7138 for this symbol. The symbol index is then
7139 set at the end of elf_bfd_final_link. */
7140 indx
= r_symndx
- extsymoff
;
7141 rh
= elf_sym_hashes (input_bfd
)[indx
];
7142 while (rh
->root
.type
== bfd_link_hash_indirect
7143 || rh
->root
.type
== bfd_link_hash_warning
)
7144 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7146 /* Setting the index to -2 tells
7147 elf_link_output_extsym that this symbol is
7149 BFD_ASSERT (rh
->indx
< 0);
7157 /* This is a reloc against a local symbol. */
7160 sym
= isymbuf
[r_symndx
];
7161 sec
= finfo
->sections
[r_symndx
];
7162 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7164 /* I suppose the backend ought to fill in the
7165 section of any STT_SECTION symbol against a
7166 processor specific section. If we have
7167 discarded a section, the output_section will
7168 be the absolute section. */
7169 if (bfd_is_abs_section (sec
)
7171 && bfd_is_abs_section (sec
->output_section
)))
7173 else if (sec
== NULL
|| sec
->owner
== NULL
)
7175 bfd_set_error (bfd_error_bad_value
);
7180 r_symndx
= sec
->output_section
->target_index
;
7181 BFD_ASSERT (r_symndx
!= 0);
7184 /* Adjust the addend according to where the
7185 section winds up in the output section. */
7187 irela
->r_addend
+= sec
->output_offset
;
7191 if (finfo
->indices
[r_symndx
] == -1)
7193 unsigned long shlink
;
7197 if (finfo
->info
->strip
== strip_all
)
7199 /* You can't do ld -r -s. */
7200 bfd_set_error (bfd_error_invalid_operation
);
7204 /* This symbol was skipped earlier, but
7205 since it is needed by a reloc, we
7206 must output it now. */
7207 shlink
= symtab_hdr
->sh_link
;
7208 name
= (bfd_elf_string_from_elf_section
7209 (input_bfd
, shlink
, sym
.st_name
));
7213 osec
= sec
->output_section
;
7215 _bfd_elf_section_from_bfd_section (output_bfd
,
7217 if (sym
.st_shndx
== SHN_BAD
)
7220 sym
.st_value
+= sec
->output_offset
;
7221 if (! finfo
->info
->relocateable
)
7223 sym
.st_value
+= osec
->vma
;
7224 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7226 /* STT_TLS symbols are relative to PT_TLS
7228 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
7229 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
7233 finfo
->indices
[r_symndx
]
7234 = bfd_get_symcount (output_bfd
);
7236 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
))
7240 r_symndx
= finfo
->indices
[r_symndx
];
7243 irela
->r_info
= ELF_R_INFO (r_symndx
,
7244 ELF_R_TYPE (irela
->r_info
));
7247 /* Swap out the relocs. */
7248 if (bed
->elf_backend_emit_relocs
7249 && !(finfo
->info
->relocateable
7250 || finfo
->info
->emitrelocations
))
7251 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7253 reloc_emitter
= elf_link_output_relocs
;
7255 if (input_rel_hdr
->sh_size
!= 0
7256 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7260 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7261 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7263 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7264 * bed
->s
->int_rels_per_ext_rel
);
7265 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
7272 /* Write out the modified section contents. */
7273 if (bed
->elf_backend_write_section
7274 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7276 /* Section written out. */
7278 else switch (o
->sec_info_type
)
7280 case ELF_INFO_TYPE_STABS
:
7281 if (! (_bfd_write_section_stabs
7283 &elf_hash_table (finfo
->info
)->stab_info
,
7284 o
, &elf_section_data (o
)->sec_info
, contents
)))
7287 case ELF_INFO_TYPE_MERGE
:
7288 if (! _bfd_write_merged_section (output_bfd
, o
,
7289 elf_section_data (o
)->sec_info
))
7292 case ELF_INFO_TYPE_EH_FRAME
:
7294 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7301 bfd_size_type sec_size
;
7303 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
7304 if (! (o
->flags
& SEC_EXCLUDE
)
7305 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7307 (file_ptr
) o
->output_offset
,
7318 /* Generate a reloc when linking an ELF file. This is a reloc
7319 requested by the linker, and does come from any input file. This
7320 is used to build constructor and destructor tables when linking
7324 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
7326 struct bfd_link_info
*info
;
7327 asection
*output_section
;
7328 struct bfd_link_order
*link_order
;
7330 reloc_howto_type
*howto
;
7334 struct elf_link_hash_entry
**rel_hash_ptr
;
7335 Elf_Internal_Shdr
*rel_hdr
;
7336 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7337 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7341 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7344 bfd_set_error (bfd_error_bad_value
);
7348 addend
= link_order
->u
.reloc
.p
->addend
;
7350 /* Figure out the symbol index. */
7351 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7352 + elf_section_data (output_section
)->rel_count
7353 + elf_section_data (output_section
)->rel_count2
);
7354 if (link_order
->type
== bfd_section_reloc_link_order
)
7356 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7357 BFD_ASSERT (indx
!= 0);
7358 *rel_hash_ptr
= NULL
;
7362 struct elf_link_hash_entry
*h
;
7364 /* Treat a reloc against a defined symbol as though it were
7365 actually against the section. */
7366 h
= ((struct elf_link_hash_entry
*)
7367 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7368 link_order
->u
.reloc
.p
->u
.name
,
7369 FALSE
, FALSE
, TRUE
));
7371 && (h
->root
.type
== bfd_link_hash_defined
7372 || h
->root
.type
== bfd_link_hash_defweak
))
7376 section
= h
->root
.u
.def
.section
;
7377 indx
= section
->output_section
->target_index
;
7378 *rel_hash_ptr
= NULL
;
7379 /* It seems that we ought to add the symbol value to the
7380 addend here, but in practice it has already been added
7381 because it was passed to constructor_callback. */
7382 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7386 /* Setting the index to -2 tells elf_link_output_extsym that
7387 this symbol is used by a reloc. */
7394 if (! ((*info
->callbacks
->unattached_reloc
)
7395 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
7396 (asection
*) NULL
, (bfd_vma
) 0)))
7402 /* If this is an inplace reloc, we must write the addend into the
7404 if (howto
->partial_inplace
&& addend
!= 0)
7407 bfd_reloc_status_type rstat
;
7410 const char *sym_name
;
7412 size
= bfd_get_reloc_size (howto
);
7413 buf
= (bfd_byte
*) bfd_zmalloc (size
);
7414 if (buf
== (bfd_byte
*) NULL
)
7416 rstat
= _bfd_relocate_contents (howto
, output_bfd
, (bfd_vma
) addend
, buf
);
7423 case bfd_reloc_outofrange
:
7426 case bfd_reloc_overflow
:
7427 if (link_order
->type
== bfd_section_reloc_link_order
)
7428 sym_name
= bfd_section_name (output_bfd
,
7429 link_order
->u
.reloc
.p
->u
.section
);
7431 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7432 if (! ((*info
->callbacks
->reloc_overflow
)
7433 (info
, sym_name
, howto
->name
, addend
,
7434 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
7441 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
7442 (file_ptr
) link_order
->offset
, size
);
7448 /* The address of a reloc is relative to the section in a
7449 relocateable file, and is a virtual address in an executable
7451 offset
= link_order
->offset
;
7452 if (! info
->relocateable
)
7453 offset
+= output_section
->vma
;
7455 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7457 irel
[i
].r_offset
= offset
;
7459 irel
[i
].r_addend
= 0;
7461 irel
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
7463 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7464 erel
= rel_hdr
->contents
;
7465 if (rel_hdr
->sh_type
== SHT_REL
)
7467 erel
+= (elf_section_data (output_section
)->rel_count
7468 * sizeof (Elf_External_Rel
));
7469 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7473 irel
[0].r_addend
= addend
;
7474 erel
+= (elf_section_data (output_section
)->rel_count
7475 * sizeof (Elf_External_Rela
));
7476 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7479 ++elf_section_data (output_section
)->rel_count
;
7484 /* Allocate a pointer to live in a linker created section. */
7487 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
7489 struct bfd_link_info
*info
;
7490 elf_linker_section_t
*lsect
;
7491 struct elf_link_hash_entry
*h
;
7492 const Elf_Internal_Rela
*rel
;
7494 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
7495 elf_linker_section_pointers_t
*linker_section_ptr
;
7496 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7499 BFD_ASSERT (lsect
!= NULL
);
7501 /* Is this a global symbol? */
7504 /* Has this symbol already been allocated? If so, our work is done. */
7505 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
7510 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
7511 /* Make sure this symbol is output as a dynamic symbol. */
7512 if (h
->dynindx
== -1)
7514 if (! elf_link_record_dynamic_symbol (info
, h
))
7518 if (lsect
->rel_section
)
7519 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7523 /* Allocation of a pointer to a local symbol. */
7524 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
7526 /* Allocate a table to hold the local symbols if first time. */
7529 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
7530 register unsigned int i
;
7533 amt
*= sizeof (elf_linker_section_pointers_t
*);
7534 ptr
= (elf_linker_section_pointers_t
**) bfd_alloc (abfd
, amt
);
7539 elf_local_ptr_offsets (abfd
) = ptr
;
7540 for (i
= 0; i
< num_symbols
; i
++)
7541 ptr
[i
] = (elf_linker_section_pointers_t
*) 0;
7544 /* Has this symbol already been allocated? If so, our work is done. */
7545 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
7550 ptr_linker_section_ptr
= &ptr
[r_symndx
];
7554 /* If we are generating a shared object, we need to
7555 output a R_<xxx>_RELATIVE reloc so that the
7556 dynamic linker can adjust this GOT entry. */
7557 BFD_ASSERT (lsect
->rel_section
!= NULL
);
7558 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7562 /* Allocate space for a pointer in the linker section, and allocate
7563 a new pointer record from internal memory. */
7564 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
7565 amt
= sizeof (elf_linker_section_pointers_t
);
7566 linker_section_ptr
= (elf_linker_section_pointers_t
*) bfd_alloc (abfd
, amt
);
7568 if (!linker_section_ptr
)
7571 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
7572 linker_section_ptr
->addend
= rel
->r_addend
;
7573 linker_section_ptr
->which
= lsect
->which
;
7574 linker_section_ptr
->written_address_p
= FALSE
;
7575 *ptr_linker_section_ptr
= linker_section_ptr
;
7578 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
7580 linker_section_ptr
->offset
= (lsect
->section
->_raw_size
7581 - lsect
->hole_size
+ (ARCH_SIZE
/ 8));
7582 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
7583 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
7584 if (lsect
->sym_hash
)
7586 /* Bump up symbol value if needed. */
7587 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
7589 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
7590 lsect
->sym_hash
->root
.root
.string
,
7591 (long) ARCH_SIZE
/ 8,
7592 (long) lsect
->sym_hash
->root
.u
.def
.value
);
7598 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
7600 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
7604 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7605 lsect
->name
, (long) linker_section_ptr
->offset
,
7606 (long) lsect
->section
->_raw_size
);
7613 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7616 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7619 /* Fill in the address for a pointer generated in a linker section. */
7622 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
,
7623 relocation
, rel
, relative_reloc
)
7626 struct bfd_link_info
*info
;
7627 elf_linker_section_t
*lsect
;
7628 struct elf_link_hash_entry
*h
;
7630 const Elf_Internal_Rela
*rel
;
7633 elf_linker_section_pointers_t
*linker_section_ptr
;
7635 BFD_ASSERT (lsect
!= NULL
);
7639 /* Handle global symbol. */
7640 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7641 (h
->linker_section_pointer
,
7645 BFD_ASSERT (linker_section_ptr
!= NULL
);
7647 if (! elf_hash_table (info
)->dynamic_sections_created
7650 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
7652 /* This is actually a static link, or it is a
7653 -Bsymbolic link and the symbol is defined
7654 locally. We must initialize this entry in the
7657 When doing a dynamic link, we create a .rela.<xxx>
7658 relocation entry to initialize the value. This
7659 is done in the finish_dynamic_symbol routine. */
7660 if (!linker_section_ptr
->written_address_p
)
7662 linker_section_ptr
->written_address_p
= TRUE
;
7663 bfd_put_ptr (output_bfd
,
7664 relocation
+ linker_section_ptr
->addend
,
7665 (lsect
->section
->contents
7666 + linker_section_ptr
->offset
));
7672 /* Handle local symbol. */
7673 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7674 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
7675 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
7676 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7677 (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
7681 BFD_ASSERT (linker_section_ptr
!= NULL
);
7683 /* Write out pointer if it hasn't been rewritten out before. */
7684 if (!linker_section_ptr
->written_address_p
)
7686 linker_section_ptr
->written_address_p
= TRUE
;
7687 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
7688 lsect
->section
->contents
+ linker_section_ptr
->offset
);
7692 asection
*srel
= lsect
->rel_section
;
7693 Elf_Internal_Rela outrel
[MAX_INT_RELS_PER_EXT_REL
];
7695 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7698 /* We need to generate a relative reloc for the dynamic
7702 srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
7704 lsect
->rel_section
= srel
;
7707 BFD_ASSERT (srel
!= NULL
);
7709 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7711 outrel
[i
].r_offset
= (lsect
->section
->output_section
->vma
7712 + lsect
->section
->output_offset
7713 + linker_section_ptr
->offset
);
7714 outrel
[i
].r_info
= 0;
7715 outrel
[i
].r_addend
= 0;
7717 outrel
[0].r_info
= ELF_R_INFO (0, relative_reloc
);
7718 erel
= lsect
->section
->contents
;
7719 erel
+= (elf_section_data (lsect
->section
)->rel_count
++
7720 * sizeof (Elf_External_Rela
));
7721 elf_swap_reloca_out (output_bfd
, outrel
, erel
);
7726 relocation
= (lsect
->section
->output_offset
7727 + linker_section_ptr
->offset
7728 - lsect
->hole_offset
7729 - lsect
->sym_offset
);
7733 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7734 lsect
->name
, (long) relocation
, (long) relocation
);
7737 /* Subtract out the addend, because it will get added back in by the normal
7739 return relocation
- linker_section_ptr
->addend
;
7742 /* Garbage collect unused sections. */
7744 static bfd_boolean elf_gc_mark
7745 PARAMS ((struct bfd_link_info
*, asection
*,
7746 asection
* (*) (asection
*, struct bfd_link_info
*,
7747 Elf_Internal_Rela
*, struct elf_link_hash_entry
*,
7748 Elf_Internal_Sym
*)));
7750 static bfd_boolean elf_gc_sweep
7751 PARAMS ((struct bfd_link_info
*,
7752 bfd_boolean (*) (bfd
*, struct bfd_link_info
*, asection
*,
7753 const Elf_Internal_Rela
*)));
7755 static bfd_boolean elf_gc_sweep_symbol
7756 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7758 static bfd_boolean elf_gc_allocate_got_offsets
7759 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7761 static bfd_boolean elf_gc_propagate_vtable_entries_used
7762 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7764 static bfd_boolean elf_gc_smash_unused_vtentry_relocs
7765 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7767 /* The mark phase of garbage collection. For a given section, mark
7768 it and any sections in this section's group, and all the sections
7769 which define symbols to which it refers. */
7771 typedef asection
* (*gc_mark_hook_fn
)
7772 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7773 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
7776 elf_gc_mark (info
, sec
, gc_mark_hook
)
7777 struct bfd_link_info
*info
;
7779 gc_mark_hook_fn gc_mark_hook
;
7782 asection
*group_sec
;
7786 /* Mark all the sections in the group. */
7787 group_sec
= elf_section_data (sec
)->next_in_group
;
7788 if (group_sec
&& !group_sec
->gc_mark
)
7789 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
7792 /* Look through the section relocs. */
7794 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
7796 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
7797 Elf_Internal_Shdr
*symtab_hdr
;
7798 struct elf_link_hash_entry
**sym_hashes
;
7801 bfd
*input_bfd
= sec
->owner
;
7802 struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
7803 Elf_Internal_Sym
*isym
= NULL
;
7805 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
7806 sym_hashes
= elf_sym_hashes (input_bfd
);
7808 /* Read the local symbols. */
7809 if (elf_bad_symtab (input_bfd
))
7811 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
7815 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
7817 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
7818 if (isym
== NULL
&& nlocsyms
!= 0)
7820 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
7826 /* Read the relocations. */
7827 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
7828 (input_bfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
7829 info
->keep_memory
));
7830 if (relstart
== NULL
)
7835 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7837 for (rel
= relstart
; rel
< relend
; rel
++)
7839 unsigned long r_symndx
;
7841 struct elf_link_hash_entry
*h
;
7843 r_symndx
= ELF_R_SYM (rel
->r_info
);
7847 if (r_symndx
>= nlocsyms
7848 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
7850 h
= sym_hashes
[r_symndx
- extsymoff
];
7851 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
7855 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
7858 if (rsec
&& !rsec
->gc_mark
)
7860 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
7862 else if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
7871 if (elf_section_data (sec
)->relocs
!= relstart
)
7874 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
7876 if (! info
->keep_memory
)
7879 symtab_hdr
->contents
= (unsigned char *) isym
;
7886 /* The sweep phase of garbage collection. Remove all garbage sections. */
7888 typedef bfd_boolean (*gc_sweep_hook_fn
)
7889 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
7890 const Elf_Internal_Rela
*));
7893 elf_gc_sweep (info
, gc_sweep_hook
)
7894 struct bfd_link_info
*info
;
7895 gc_sweep_hook_fn gc_sweep_hook
;
7899 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7903 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
7906 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7908 /* Keep special sections. Keep .debug sections. */
7909 if ((o
->flags
& SEC_LINKER_CREATED
)
7910 || (o
->flags
& SEC_DEBUGGING
))
7916 /* Skip sweeping sections already excluded. */
7917 if (o
->flags
& SEC_EXCLUDE
)
7920 /* Since this is early in the link process, it is simple
7921 to remove a section from the output. */
7922 o
->flags
|= SEC_EXCLUDE
;
7924 /* But we also have to update some of the relocation
7925 info we collected before. */
7927 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
7929 Elf_Internal_Rela
*internal_relocs
;
7932 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
7933 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
7934 if (internal_relocs
== NULL
)
7937 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
7939 if (elf_section_data (o
)->relocs
!= internal_relocs
)
7940 free (internal_relocs
);
7948 /* Remove the symbols that were in the swept sections from the dynamic
7949 symbol table. GCFIXME: Anyone know how to get them out of the
7950 static symbol table as well? */
7954 elf_link_hash_traverse (elf_hash_table (info
),
7955 elf_gc_sweep_symbol
,
7958 elf_hash_table (info
)->dynsymcount
= i
;
7964 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7967 elf_gc_sweep_symbol (h
, idxptr
)
7968 struct elf_link_hash_entry
*h
;
7971 int *idx
= (int *) idxptr
;
7973 if (h
->root
.type
== bfd_link_hash_warning
)
7974 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7976 if (h
->dynindx
!= -1
7977 && ((h
->root
.type
!= bfd_link_hash_defined
7978 && h
->root
.type
!= bfd_link_hash_defweak
)
7979 || h
->root
.u
.def
.section
->gc_mark
))
7980 h
->dynindx
= (*idx
)++;
7985 /* Propogate collected vtable information. This is called through
7986 elf_link_hash_traverse. */
7989 elf_gc_propagate_vtable_entries_used (h
, okp
)
7990 struct elf_link_hash_entry
*h
;
7993 if (h
->root
.type
== bfd_link_hash_warning
)
7994 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7996 /* Those that are not vtables. */
7997 if (h
->vtable_parent
== NULL
)
8000 /* Those vtables that do not have parents, we cannot merge. */
8001 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
8004 /* If we've already been done, exit. */
8005 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
8008 /* Make sure the parent's table is up to date. */
8009 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
8011 if (h
->vtable_entries_used
== NULL
)
8013 /* None of this table's entries were referenced. Re-use the
8015 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
8016 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
8021 bfd_boolean
*cu
, *pu
;
8023 /* Or the parent's entries into ours. */
8024 cu
= h
->vtable_entries_used
;
8026 pu
= h
->vtable_parent
->vtable_entries_used
;
8029 asection
*sec
= h
->root
.u
.def
.section
;
8030 struct elf_backend_data
*bed
= get_elf_backend_data (sec
->owner
);
8031 int file_align
= bed
->s
->file_align
;
8033 n
= h
->vtable_parent
->vtable_entries_size
/ file_align
;
8048 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
8049 struct elf_link_hash_entry
*h
;
8053 bfd_vma hstart
, hend
;
8054 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8055 struct elf_backend_data
*bed
;
8058 if (h
->root
.type
== bfd_link_hash_warning
)
8059 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8061 /* Take care of both those symbols that do not describe vtables as
8062 well as those that are not loaded. */
8063 if (h
->vtable_parent
== NULL
)
8066 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8067 || h
->root
.type
== bfd_link_hash_defweak
);
8069 sec
= h
->root
.u
.def
.section
;
8070 hstart
= h
->root
.u
.def
.value
;
8071 hend
= hstart
+ h
->size
;
8073 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
8074 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, TRUE
));
8076 return *(bfd_boolean
*) okp
= FALSE
;
8077 bed
= get_elf_backend_data (sec
->owner
);
8078 file_align
= bed
->s
->file_align
;
8080 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8082 for (rel
= relstart
; rel
< relend
; ++rel
)
8083 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8085 /* If the entry is in use, do nothing. */
8086 if (h
->vtable_entries_used
8087 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
8089 bfd_vma entry
= (rel
->r_offset
- hstart
) / file_align
;
8090 if (h
->vtable_entries_used
[entry
])
8093 /* Otherwise, kill it. */
8094 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8100 /* Do mark and sweep of unused sections. */
8103 elf_gc_sections (abfd
, info
)
8105 struct bfd_link_info
*info
;
8107 bfd_boolean ok
= TRUE
;
8109 asection
* (*gc_mark_hook
)
8110 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8111 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
8113 if (!get_elf_backend_data (abfd
)->can_gc_sections
8114 || info
->relocateable
|| info
->emitrelocations
8115 || elf_hash_table (info
)->dynamic_sections_created
)
8118 /* Apply transitive closure to the vtable entry usage info. */
8119 elf_link_hash_traverse (elf_hash_table (info
),
8120 elf_gc_propagate_vtable_entries_used
,
8125 /* Kill the vtable relocations that were not used. */
8126 elf_link_hash_traverse (elf_hash_table (info
),
8127 elf_gc_smash_unused_vtentry_relocs
,
8132 /* Grovel through relocs to find out who stays ... */
8134 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8135 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8139 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8142 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8144 if (o
->flags
& SEC_KEEP
)
8145 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
8150 /* ... and mark SEC_EXCLUDE for those that go. */
8151 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8157 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
8160 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
8163 struct elf_link_hash_entry
*h
;
8166 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8167 struct elf_link_hash_entry
**search
, *child
;
8168 bfd_size_type extsymcount
;
8170 /* The sh_info field of the symtab header tells us where the
8171 external symbols start. We don't care about the local symbols at
8173 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
8174 if (!elf_bad_symtab (abfd
))
8175 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8177 sym_hashes
= elf_sym_hashes (abfd
);
8178 sym_hashes_end
= sym_hashes
+ extsymcount
;
8180 /* Hunt down the child symbol, which is in this section at the same
8181 offset as the relocation. */
8182 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8184 if ((child
= *search
) != NULL
8185 && (child
->root
.type
== bfd_link_hash_defined
8186 || child
->root
.type
== bfd_link_hash_defweak
)
8187 && child
->root
.u
.def
.section
== sec
8188 && child
->root
.u
.def
.value
== offset
)
8192 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
8193 bfd_archive_filename (abfd
), sec
->name
,
8194 (unsigned long) offset
);
8195 bfd_set_error (bfd_error_invalid_operation
);
8201 /* This *should* only be the absolute section. It could potentially
8202 be that someone has defined a non-global vtable though, which
8203 would be bad. It isn't worth paging in the local symbols to be
8204 sure though; that case should simply be handled by the assembler. */
8206 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
8209 child
->vtable_parent
= h
;
8214 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
8217 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
8218 bfd
*abfd ATTRIBUTE_UNUSED
;
8219 asection
*sec ATTRIBUTE_UNUSED
;
8220 struct elf_link_hash_entry
*h
;
8223 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8224 int file_align
= bed
->s
->file_align
;
8226 if (addend
>= h
->vtable_entries_size
)
8229 bfd_boolean
*ptr
= h
->vtable_entries_used
;
8231 /* While the symbol is undefined, we have to be prepared to handle
8233 if (h
->root
.type
== bfd_link_hash_undefined
)
8240 /* Oops! We've got a reference past the defined end of
8241 the table. This is probably a bug -- shall we warn? */
8246 /* Allocate one extra entry for use as a "done" flag for the
8247 consolidation pass. */
8248 bytes
= (size
/ file_align
+ 1) * sizeof (bfd_boolean
);
8252 ptr
= bfd_realloc (ptr
- 1, (bfd_size_type
) bytes
);
8258 oldbytes
= ((h
->vtable_entries_size
/ file_align
+ 1)
8259 * sizeof (bfd_boolean
));
8260 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8264 ptr
= bfd_zmalloc ((bfd_size_type
) bytes
);
8269 /* And arrange for that done flag to be at index -1. */
8270 h
->vtable_entries_used
= ptr
+ 1;
8271 h
->vtable_entries_size
= size
;
8274 h
->vtable_entries_used
[addend
/ file_align
] = TRUE
;
8279 /* And an accompanying bit to work out final got entry offsets once
8280 we're done. Should be called from final_link. */
8283 elf_gc_common_finalize_got_offsets (abfd
, info
)
8285 struct bfd_link_info
*info
;
8288 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8291 /* The GOT offset is relative to the .got section, but the GOT header is
8292 put into the .got.plt section, if the backend uses it. */
8293 if (bed
->want_got_plt
)
8296 gotoff
= bed
->got_header_size
;
8298 /* Do the local .got entries first. */
8299 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
8301 bfd_signed_vma
*local_got
;
8302 bfd_size_type j
, locsymcount
;
8303 Elf_Internal_Shdr
*symtab_hdr
;
8305 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
8308 local_got
= elf_local_got_refcounts (i
);
8312 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
8313 if (elf_bad_symtab (i
))
8314 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8316 locsymcount
= symtab_hdr
->sh_info
;
8318 for (j
= 0; j
< locsymcount
; ++j
)
8320 if (local_got
[j
] > 0)
8322 local_got
[j
] = gotoff
;
8323 gotoff
+= ARCH_SIZE
/ 8;
8326 local_got
[j
] = (bfd_vma
) -1;
8330 /* Then the global .got entries. .plt refcounts are handled by
8331 adjust_dynamic_symbol */
8332 elf_link_hash_traverse (elf_hash_table (info
),
8333 elf_gc_allocate_got_offsets
,
8338 /* We need a special top-level link routine to convert got reference counts
8339 to real got offsets. */
8342 elf_gc_allocate_got_offsets (h
, offarg
)
8343 struct elf_link_hash_entry
*h
;
8346 bfd_vma
*off
= (bfd_vma
*) offarg
;
8348 if (h
->root
.type
== bfd_link_hash_warning
)
8349 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8351 if (h
->got
.refcount
> 0)
8353 h
->got
.offset
= off
[0];
8354 off
[0] += ARCH_SIZE
/ 8;
8357 h
->got
.offset
= (bfd_vma
) -1;
8362 /* Many folk need no more in the way of final link than this, once
8363 got entry reference counting is enabled. */
8366 elf_gc_common_final_link (abfd
, info
)
8368 struct bfd_link_info
*info
;
8370 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
8373 /* Invoke the regular ELF backend linker to do all the work. */
8374 return elf_bfd_final_link (abfd
, info
);
8377 /* This function will be called though elf_link_hash_traverse to store
8378 all hash value of the exported symbols in an array. */
8381 elf_collect_hash_codes (h
, data
)
8382 struct elf_link_hash_entry
*h
;
8385 unsigned long **valuep
= (unsigned long **) data
;
8391 if (h
->root
.type
== bfd_link_hash_warning
)
8392 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8394 /* Ignore indirect symbols. These are added by the versioning code. */
8395 if (h
->dynindx
== -1)
8398 name
= h
->root
.root
.string
;
8399 p
= strchr (name
, ELF_VER_CHR
);
8402 alc
= bfd_malloc ((bfd_size_type
) (p
- name
+ 1));
8403 memcpy (alc
, name
, (size_t) (p
- name
));
8404 alc
[p
- name
] = '\0';
8408 /* Compute the hash value. */
8409 ha
= bfd_elf_hash (name
);
8411 /* Store the found hash value in the array given as the argument. */
8414 /* And store it in the struct so that we can put it in the hash table
8416 h
->elf_hash_value
= ha
;
8425 elf_reloc_symbol_deleted_p (offset
, cookie
)
8429 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
8431 if (rcookie
->bad_symtab
)
8432 rcookie
->rel
= rcookie
->rels
;
8434 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
8436 unsigned long r_symndx
;
8438 if (! rcookie
->bad_symtab
)
8439 if (rcookie
->rel
->r_offset
> offset
)
8441 if (rcookie
->rel
->r_offset
!= offset
)
8444 r_symndx
= ELF_R_SYM (rcookie
->rel
->r_info
);
8445 if (r_symndx
== SHN_UNDEF
)
8448 if (r_symndx
>= rcookie
->locsymcount
8449 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
8451 struct elf_link_hash_entry
*h
;
8453 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
8455 while (h
->root
.type
== bfd_link_hash_indirect
8456 || h
->root
.type
== bfd_link_hash_warning
)
8457 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8459 if ((h
->root
.type
== bfd_link_hash_defined
8460 || h
->root
.type
== bfd_link_hash_defweak
)
8461 && elf_discarded_section (h
->root
.u
.def
.section
))
8468 /* It's not a relocation against a global symbol,
8469 but it could be a relocation against a local
8470 symbol for a discarded section. */
8472 Elf_Internal_Sym
*isym
;
8474 /* Need to: get the symbol; get the section. */
8475 isym
= &rcookie
->locsyms
[r_symndx
];
8476 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
8478 isec
= section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
8479 if (isec
!= NULL
&& elf_discarded_section (isec
))
8488 /* Discard unneeded references to discarded sections.
8489 Returns TRUE if any section's size was changed. */
8490 /* This function assumes that the relocations are in sorted order,
8491 which is true for all known assemblers. */
8494 elf_bfd_discard_info (output_bfd
, info
)
8496 struct bfd_link_info
*info
;
8498 struct elf_reloc_cookie cookie
;
8499 asection
*stab
, *eh
;
8500 Elf_Internal_Shdr
*symtab_hdr
;
8501 struct elf_backend_data
*bed
;
8504 bfd_boolean ret
= FALSE
;
8506 if (info
->traditional_format
8507 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
8508 || ! is_elf_hash_table (info
))
8511 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
8513 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
8516 bed
= get_elf_backend_data (abfd
);
8518 if ((abfd
->flags
& DYNAMIC
) != 0)
8521 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
8522 if (info
->relocateable
8524 && (eh
->_raw_size
== 0
8525 || bfd_is_abs_section (eh
->output_section
))))
8528 stab
= bfd_get_section_by_name (abfd
, ".stab");
8530 && (stab
->_raw_size
== 0
8531 || bfd_is_abs_section (stab
->output_section
)
8532 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
8537 && bed
->elf_backend_discard_info
== NULL
)
8540 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8542 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
8543 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
8544 if (cookie
.bad_symtab
)
8546 cookie
.locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8547 cookie
.extsymoff
= 0;
8551 cookie
.locsymcount
= symtab_hdr
->sh_info
;
8552 cookie
.extsymoff
= symtab_hdr
->sh_info
;
8555 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8556 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
8558 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
8559 cookie
.locsymcount
, 0,
8561 if (cookie
.locsyms
== NULL
)
8568 count
= stab
->reloc_count
;
8570 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8571 (abfd
, stab
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
8572 info
->keep_memory
));
8573 if (cookie
.rels
!= NULL
)
8575 cookie
.rel
= cookie
.rels
;
8576 cookie
.relend
= cookie
.rels
;
8577 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
8578 if (_bfd_discard_section_stabs (abfd
, stab
,
8579 elf_section_data (stab
)->sec_info
,
8580 elf_reloc_symbol_deleted_p
,
8583 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
8591 count
= eh
->reloc_count
;
8593 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8594 (abfd
, eh
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
8595 info
->keep_memory
));
8596 cookie
.rel
= cookie
.rels
;
8597 cookie
.relend
= cookie
.rels
;
8598 if (cookie
.rels
!= NULL
)
8599 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
8601 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
8602 elf_reloc_symbol_deleted_p
,
8606 if (cookie
.rels
!= NULL
8607 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
8611 if (bed
->elf_backend_discard_info
!= NULL
8612 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
8615 if (cookie
.locsyms
!= NULL
8616 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
8618 if (! info
->keep_memory
)
8619 free (cookie
.locsyms
);
8621 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
8625 if (info
->eh_frame_hdr
8626 && !info
->relocateable
8627 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
8634 elf_section_ignore_discarded_relocs (sec
)
8637 struct elf_backend_data
*bed
;
8639 switch (sec
->sec_info_type
)
8641 case ELF_INFO_TYPE_STABS
:
8642 case ELF_INFO_TYPE_EH_FRAME
:
8648 bed
= get_elf_backend_data (sec
->owner
);
8649 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
8650 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))