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
));
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
,
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
;
475 bfd_boolean
*override
;
476 bfd_boolean
*type_change_ok
;
477 bfd_boolean
*size_change_ok
;
478 bfd_boolean dt_needed
;
481 struct elf_link_hash_entry
*h
;
484 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
489 bind
= ELF_ST_BIND (sym
->st_info
);
491 if (! bfd_is_und_section (sec
))
492 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
494 h
= ((struct elf_link_hash_entry
*)
495 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
500 /* This code is for coping with dynamic objects, and is only useful
501 if we are doing an ELF link. */
502 if (info
->hash
->creator
!= abfd
->xvec
)
505 /* For merging, we only care about real symbols. */
507 while (h
->root
.type
== bfd_link_hash_indirect
508 || h
->root
.type
== bfd_link_hash_warning
)
509 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
511 /* If we just created the symbol, mark it as being an ELF symbol.
512 Other than that, there is nothing to do--there is no merge issue
513 with a newly defined symbol--so we just return. */
515 if (h
->root
.type
== bfd_link_hash_new
)
517 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
521 /* OLDBFD is a BFD associated with the existing symbol. */
523 switch (h
->root
.type
)
529 case bfd_link_hash_undefined
:
530 case bfd_link_hash_undefweak
:
531 oldbfd
= h
->root
.u
.undef
.abfd
;
534 case bfd_link_hash_defined
:
535 case bfd_link_hash_defweak
:
536 oldbfd
= h
->root
.u
.def
.section
->owner
;
539 case bfd_link_hash_common
:
540 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
544 /* In cases involving weak versioned symbols, we may wind up trying
545 to merge a symbol with itself. Catch that here, to avoid the
546 confusion that results if we try to override a symbol with
547 itself. The additional tests catch cases like
548 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
549 dynamic object, which we do want to handle here. */
551 && ((abfd
->flags
& DYNAMIC
) == 0
552 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0))
555 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
556 respectively, is from a dynamic object. */
558 if ((abfd
->flags
& DYNAMIC
) != 0)
564 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
569 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
570 indices used by MIPS ELF. */
571 switch (h
->root
.type
)
577 case bfd_link_hash_defined
:
578 case bfd_link_hash_defweak
:
579 hsec
= h
->root
.u
.def
.section
;
582 case bfd_link_hash_common
:
583 hsec
= h
->root
.u
.c
.p
->section
;
590 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
593 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
594 respectively, appear to be a definition rather than reference. */
596 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
601 if (h
->root
.type
== bfd_link_hash_undefined
602 || h
->root
.type
== bfd_link_hash_undefweak
603 || h
->root
.type
== bfd_link_hash_common
)
608 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
609 symbol, respectively, appears to be a common symbol in a dynamic
610 object. If a symbol appears in an uninitialized section, and is
611 not weak, and is not a function, then it may be a common symbol
612 which was resolved when the dynamic object was created. We want
613 to treat such symbols specially, because they raise special
614 considerations when setting the symbol size: if the symbol
615 appears as a common symbol in a regular object, and the size in
616 the regular object is larger, we must make sure that we use the
617 larger size. This problematic case can always be avoided in C,
618 but it must be handled correctly when using Fortran shared
621 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
622 likewise for OLDDYNCOMMON and OLDDEF.
624 Note that this test is just a heuristic, and that it is quite
625 possible to have an uninitialized symbol in a shared object which
626 is really a definition, rather than a common symbol. This could
627 lead to some minor confusion when the symbol really is a common
628 symbol in some regular object. However, I think it will be
633 && (sec
->flags
& SEC_ALLOC
) != 0
634 && (sec
->flags
& SEC_LOAD
) == 0
637 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
640 newdyncommon
= FALSE
;
644 && h
->root
.type
== bfd_link_hash_defined
645 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
646 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
647 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
649 && h
->type
!= STT_FUNC
)
652 olddyncommon
= FALSE
;
654 /* It's OK to change the type if either the existing symbol or the
655 new symbol is weak unless it comes from a DT_NEEDED entry of
656 a shared object, in which case, the DT_NEEDED entry may not be
657 required at the run time. */
659 if ((! dt_needed
&& h
->root
.type
== bfd_link_hash_defweak
)
660 || h
->root
.type
== bfd_link_hash_undefweak
662 *type_change_ok
= TRUE
;
664 /* It's OK to change the size if either the existing symbol or the
665 new symbol is weak, or if the old symbol is undefined. */
668 || h
->root
.type
== bfd_link_hash_undefined
)
669 *size_change_ok
= TRUE
;
671 /* If both the old and the new symbols look like common symbols in a
672 dynamic object, set the size of the symbol to the larger of the
677 && sym
->st_size
!= h
->size
)
679 /* Since we think we have two common symbols, issue a multiple
680 common warning if desired. Note that we only warn if the
681 size is different. If the size is the same, we simply let
682 the old symbol override the new one as normally happens with
683 symbols defined in dynamic objects. */
685 if (! ((*info
->callbacks
->multiple_common
)
686 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
687 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
690 if (sym
->st_size
> h
->size
)
691 h
->size
= sym
->st_size
;
693 *size_change_ok
= TRUE
;
696 /* If we are looking at a dynamic object, and we have found a
697 definition, we need to see if the symbol was already defined by
698 some other object. If so, we want to use the existing
699 definition, and we do not want to report a multiple symbol
700 definition error; we do this by clobbering *PSEC to be
703 We treat a common symbol as a definition if the symbol in the
704 shared library is a function, since common symbols always
705 represent variables; this can cause confusion in principle, but
706 any such confusion would seem to indicate an erroneous program or
707 shared library. We also permit a common symbol in a regular
708 object to override a weak symbol in a shared object.
710 We prefer a non-weak definition in a shared library to a weak
711 definition in the executable unless it comes from a DT_NEEDED
712 entry of a shared object, in which case, the DT_NEEDED entry
713 may not be required at the run time. */
718 || (h
->root
.type
== bfd_link_hash_common
720 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)))
721 && (h
->root
.type
!= bfd_link_hash_defweak
723 || bind
== STB_WEAK
))
727 newdyncommon
= FALSE
;
729 *psec
= sec
= bfd_und_section_ptr
;
730 *size_change_ok
= TRUE
;
732 /* If we get here when the old symbol is a common symbol, then
733 we are explicitly letting it override a weak symbol or
734 function in a dynamic object, and we don't want to warn about
735 a type change. If the old symbol is a defined symbol, a type
736 change warning may still be appropriate. */
738 if (h
->root
.type
== bfd_link_hash_common
)
739 *type_change_ok
= TRUE
;
742 /* Handle the special case of an old common symbol merging with a
743 new symbol which looks like a common symbol in a shared object.
744 We change *PSEC and *PVALUE to make the new symbol look like a
745 common symbol, and let _bfd_generic_link_add_one_symbol will do
749 && h
->root
.type
== bfd_link_hash_common
)
753 newdyncommon
= FALSE
;
754 *pvalue
= sym
->st_size
;
755 *psec
= sec
= bfd_com_section_ptr
;
756 *size_change_ok
= TRUE
;
759 /* If the old symbol is from a dynamic object, and the new symbol is
760 a definition which is not from a dynamic object, then the new
761 symbol overrides the old symbol. Symbols from regular files
762 always take precedence over symbols from dynamic objects, even if
763 they are defined after the dynamic object in the link.
765 As above, we again permit a common symbol in a regular object to
766 override a definition in a shared object if the shared object
767 symbol is a function or is weak.
769 As above, we permit a non-weak definition in a shared object to
770 override a weak definition in a regular object. */
774 || (bfd_is_com_section (sec
)
775 && (h
->root
.type
== bfd_link_hash_defweak
776 || h
->type
== STT_FUNC
)))
779 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
781 || h
->root
.type
== bfd_link_hash_defweak
))
783 /* Change the hash table entry to undefined, and let
784 _bfd_generic_link_add_one_symbol do the right thing with the
787 h
->root
.type
= bfd_link_hash_undefined
;
788 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
789 *size_change_ok
= TRUE
;
792 olddyncommon
= FALSE
;
794 /* We again permit a type change when a common symbol may be
795 overriding a function. */
797 if (bfd_is_com_section (sec
))
798 *type_change_ok
= TRUE
;
800 /* This union may have been set to be non-NULL when this symbol
801 was seen in a dynamic object. We must force the union to be
802 NULL, so that it is correct for a regular symbol. */
804 h
->verinfo
.vertree
= NULL
;
806 /* In this special case, if H is the target of an indirection,
807 we want the caller to frob with H rather than with the
808 indirect symbol. That will permit the caller to redefine the
809 target of the indirection, rather than the indirect symbol
810 itself. FIXME: This will break the -y option if we store a
811 symbol with a different name. */
815 /* Handle the special case of a new common symbol merging with an
816 old symbol that looks like it might be a common symbol defined in
817 a shared object. Note that we have already handled the case in
818 which a new common symbol should simply override the definition
819 in the shared library. */
822 && bfd_is_com_section (sec
)
825 /* It would be best if we could set the hash table entry to a
826 common symbol, but we don't know what to use for the section
828 if (! ((*info
->callbacks
->multiple_common
)
829 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
830 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
833 /* If the predumed common symbol in the dynamic object is
834 larger, pretend that the new symbol has its size. */
836 if (h
->size
> *pvalue
)
839 /* FIXME: We no longer know the alignment required by the symbol
840 in the dynamic object, so we just wind up using the one from
841 the regular object. */
844 olddyncommon
= FALSE
;
846 h
->root
.type
= bfd_link_hash_undefined
;
847 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
849 *size_change_ok
= TRUE
;
850 *type_change_ok
= TRUE
;
852 h
->verinfo
.vertree
= NULL
;
855 /* Handle the special case of a weak definition in a regular object
856 followed by a non-weak definition in a shared object. In this
857 case, we prefer the definition in the shared object unless it
858 comes from a DT_NEEDED entry of a shared object, in which case,
859 the DT_NEEDED entry may not be required at the run time. */
862 && h
->root
.type
== bfd_link_hash_defweak
867 /* To make this work we have to frob the flags so that the rest
868 of the code does not think we are using the regular
870 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
871 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
872 else if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
873 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
874 h
->elf_link_hash_flags
&= ~ (ELF_LINK_HASH_DEF_REGULAR
875 | ELF_LINK_HASH_DEF_DYNAMIC
);
877 /* If H is the target of an indirection, we want the caller to
878 use H rather than the indirect symbol. Otherwise if we are
879 defining a new indirect symbol we will wind up attaching it
880 to the entry we are overriding. */
884 /* Handle the special case of a non-weak definition in a shared
885 object followed by a weak definition in a regular object. In
886 this case we prefer to definition in the shared object. To make
887 this work we have to tell the caller to not treat the new symbol
891 && h
->root
.type
!= bfd_link_hash_defweak
900 /* This function is called to create an indirect symbol from the
901 default for the symbol with the default version if needed. The
902 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
903 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
904 indicates if it comes from a DT_NEEDED entry of a shared object. */
907 elf_add_default_symbol (abfd
, info
, h
, name
, sym
, psec
, value
,
908 dynsym
, override
, dt_needed
)
910 struct bfd_link_info
*info
;
911 struct elf_link_hash_entry
*h
;
913 Elf_Internal_Sym
*sym
;
917 bfd_boolean override
;
918 bfd_boolean dt_needed
;
920 bfd_boolean type_change_ok
;
921 bfd_boolean size_change_ok
;
923 struct elf_link_hash_entry
*hi
;
924 struct bfd_link_hash_entry
*bh
;
925 struct elf_backend_data
*bed
;
929 size_t len
, shortlen
;
932 /* If this symbol has a version, and it is the default version, we
933 create an indirect symbol from the default name to the fully
934 decorated name. This will cause external references which do not
935 specify a version to be bound to this version of the symbol. */
936 p
= strchr (name
, ELF_VER_CHR
);
937 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
942 /* We are overridden by an old defition. We need to check if we
943 need to create the indirect symbol from the default name. */
944 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
946 BFD_ASSERT (hi
!= NULL
);
949 while (hi
->root
.type
== bfd_link_hash_indirect
950 || hi
->root
.type
== bfd_link_hash_warning
)
952 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
958 bed
= get_elf_backend_data (abfd
);
959 collect
= bed
->collect
;
960 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
963 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
964 if (shortname
== NULL
)
966 memcpy (shortname
, name
, shortlen
);
967 shortname
[shortlen
] = '\0';
969 /* We are going to create a new symbol. Merge it with any existing
970 symbol with this name. For the purposes of the merge, act as
971 though we were defining the symbol we just defined, although we
972 actually going to define an indirect symbol. */
973 type_change_ok
= FALSE
;
974 size_change_ok
= FALSE
;
976 if (! elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
977 &hi
, &override
, &type_change_ok
,
978 &size_change_ok
, dt_needed
))
984 if (! (_bfd_generic_link_add_one_symbol
985 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
986 (bfd_vma
) 0, name
, FALSE
, collect
, &bh
)))
988 hi
= (struct elf_link_hash_entry
*) bh
;
992 /* In this case the symbol named SHORTNAME is overriding the
993 indirect symbol we want to add. We were planning on making
994 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
995 is the name without a version. NAME is the fully versioned
996 name, and it is the default version.
998 Overriding means that we already saw a definition for the
999 symbol SHORTNAME in a regular object, and it is overriding
1000 the symbol defined in the dynamic object.
1002 When this happens, we actually want to change NAME, the
1003 symbol we just added, to refer to SHORTNAME. This will cause
1004 references to NAME in the shared object to become references
1005 to SHORTNAME in the regular object. This is what we expect
1006 when we override a function in a shared object: that the
1007 references in the shared object will be mapped to the
1008 definition in the regular object. */
1010 while (hi
->root
.type
== bfd_link_hash_indirect
1011 || hi
->root
.type
== bfd_link_hash_warning
)
1012 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1014 h
->root
.type
= bfd_link_hash_indirect
;
1015 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1016 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1018 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1019 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1020 if (hi
->elf_link_hash_flags
1021 & (ELF_LINK_HASH_REF_REGULAR
1022 | ELF_LINK_HASH_DEF_REGULAR
))
1024 if (! _bfd_elf_link_record_dynamic_symbol (info
, hi
))
1029 /* Now set HI to H, so that the following code will set the
1030 other fields correctly. */
1034 /* If there is a duplicate definition somewhere, then HI may not
1035 point to an indirect symbol. We will have reported an error to
1036 the user in that case. */
1038 if (hi
->root
.type
== bfd_link_hash_indirect
)
1040 struct elf_link_hash_entry
*ht
;
1042 /* If the symbol became indirect, then we assume that we have
1043 not seen a definition before. */
1044 BFD_ASSERT ((hi
->elf_link_hash_flags
1045 & (ELF_LINK_HASH_DEF_DYNAMIC
1046 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1048 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1049 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1051 /* See if the new flags lead us to realize that the symbol must
1058 || ((hi
->elf_link_hash_flags
1059 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1064 if ((hi
->elf_link_hash_flags
1065 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1071 /* We also need to define an indirection from the nondefault version
1074 len
= strlen (name
);
1075 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1076 if (shortname
== NULL
)
1078 memcpy (shortname
, name
, shortlen
);
1079 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1081 /* Once again, merge with any existing symbol. */
1082 type_change_ok
= FALSE
;
1083 size_change_ok
= FALSE
;
1085 if (! elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1086 &hi
, &override
, &type_change_ok
,
1087 &size_change_ok
, dt_needed
))
1092 /* Here SHORTNAME is a versioned name, so we don't expect to see
1093 the type of override we do in the case above unless it is
1094 overridden by a versioned definiton. */
1095 if (hi
->root
.type
!= bfd_link_hash_defined
1096 && hi
->root
.type
!= bfd_link_hash_defweak
)
1097 (*_bfd_error_handler
)
1098 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1099 bfd_archive_filename (abfd
), shortname
);
1104 if (! (_bfd_generic_link_add_one_symbol
1105 (info
, abfd
, shortname
, BSF_INDIRECT
,
1106 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, FALSE
, collect
, &bh
)))
1108 hi
= (struct elf_link_hash_entry
*) bh
;
1110 /* If there is a duplicate definition somewhere, then HI may not
1111 point to an indirect symbol. We will have reported an error
1112 to the user in that case. */
1114 if (hi
->root
.type
== bfd_link_hash_indirect
)
1116 /* If the symbol became indirect, then we assume that we have
1117 not seen a definition before. */
1118 BFD_ASSERT ((hi
->elf_link_hash_flags
1119 & (ELF_LINK_HASH_DEF_DYNAMIC
1120 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1122 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1124 /* See if the new flags lead us to realize that the symbol
1131 || ((hi
->elf_link_hash_flags
1132 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1137 if ((hi
->elf_link_hash_flags
1138 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1148 /* Add symbols from an ELF object file to the linker hash table. */
1151 elf_link_add_object_symbols (abfd
, info
)
1153 struct bfd_link_info
*info
;
1155 bfd_boolean (*add_symbol_hook
)
1156 PARAMS ((bfd
*, struct bfd_link_info
*, const Elf_Internal_Sym
*,
1157 const char **, flagword
*, asection
**, bfd_vma
*));
1158 bfd_boolean (*check_relocs
)
1159 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
1160 const Elf_Internal_Rela
*));
1161 bfd_boolean collect
;
1162 Elf_Internal_Shdr
*hdr
;
1163 bfd_size_type symcount
;
1164 bfd_size_type extsymcount
;
1165 bfd_size_type extsymoff
;
1166 struct elf_link_hash_entry
**sym_hash
;
1167 bfd_boolean dynamic
;
1168 Elf_External_Versym
*extversym
= NULL
;
1169 Elf_External_Versym
*ever
;
1170 struct elf_link_hash_entry
*weaks
;
1171 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
1172 bfd_size_type nondeflt_vers_cnt
= 0;
1173 Elf_Internal_Sym
*isymbuf
= NULL
;
1174 Elf_Internal_Sym
*isym
;
1175 Elf_Internal_Sym
*isymend
;
1176 struct elf_backend_data
*bed
;
1177 bfd_boolean dt_needed
;
1178 struct elf_link_hash_table
* hash_table
;
1181 hash_table
= elf_hash_table (info
);
1183 bed
= get_elf_backend_data (abfd
);
1184 add_symbol_hook
= bed
->elf_add_symbol_hook
;
1185 collect
= bed
->collect
;
1187 if ((abfd
->flags
& DYNAMIC
) == 0)
1193 /* You can't use -r against a dynamic object. Also, there's no
1194 hope of using a dynamic object which does not exactly match
1195 the format of the output file. */
1196 if (info
->relocateable
|| info
->hash
->creator
!= abfd
->xvec
)
1198 bfd_set_error (bfd_error_invalid_operation
);
1203 /* As a GNU extension, any input sections which are named
1204 .gnu.warning.SYMBOL are treated as warning symbols for the given
1205 symbol. This differs from .gnu.warning sections, which generate
1206 warnings when they are included in an output file. */
1211 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
1215 name
= bfd_get_section_name (abfd
, s
);
1216 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1221 name
+= sizeof ".gnu.warning." - 1;
1223 /* If this is a shared object, then look up the symbol
1224 in the hash table. If it is there, and it is already
1225 been defined, then we will not be using the entry
1226 from this shared object, so we don't need to warn.
1227 FIXME: If we see the definition in a regular object
1228 later on, we will warn, but we shouldn't. The only
1229 fix is to keep track of what warnings we are supposed
1230 to emit, and then handle them all at the end of the
1232 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
1234 struct elf_link_hash_entry
*h
;
1236 h
= elf_link_hash_lookup (hash_table
, name
,
1237 FALSE
, FALSE
, TRUE
);
1239 /* FIXME: What about bfd_link_hash_common? */
1241 && (h
->root
.type
== bfd_link_hash_defined
1242 || h
->root
.type
== bfd_link_hash_defweak
))
1244 /* We don't want to issue this warning. Clobber
1245 the section size so that the warning does not
1246 get copied into the output file. */
1252 sz
= bfd_section_size (abfd
, s
);
1253 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
1257 if (! bfd_get_section_contents (abfd
, s
, msg
, (file_ptr
) 0, sz
))
1262 if (! (_bfd_generic_link_add_one_symbol
1263 (info
, abfd
, name
, BSF_WARNING
, s
, (bfd_vma
) 0, msg
,
1264 FALSE
, collect
, (struct bfd_link_hash_entry
**) NULL
)))
1267 if (! info
->relocateable
)
1269 /* Clobber the section size so that the warning does
1270 not get copied into the output file. */
1280 /* If we are creating a shared library, create all the dynamic
1281 sections immediately. We need to attach them to something,
1282 so we attach them to this BFD, provided it is the right
1283 format. FIXME: If there are no input BFD's of the same
1284 format as the output, we can't make a shared library. */
1286 && is_elf_hash_table (info
)
1287 && ! hash_table
->dynamic_sections_created
1288 && abfd
->xvec
== info
->hash
->creator
)
1290 if (! elf_link_create_dynamic_sections (abfd
, info
))
1294 else if (! is_elf_hash_table (info
))
1299 bfd_boolean add_needed
;
1301 bfd_size_type oldsize
;
1302 bfd_size_type strindex
;
1303 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
1305 /* ld --just-symbols and dynamic objects don't mix very well.
1306 Test for --just-symbols by looking at info set up by
1307 _bfd_elf_link_just_syms. */
1308 if ((s
= abfd
->sections
) != NULL
1309 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
1312 /* Find the name to use in a DT_NEEDED entry that refers to this
1313 object. If the object has a DT_SONAME entry, we use it.
1314 Otherwise, if the generic linker stuck something in
1315 elf_dt_name, we use that. Otherwise, we just use the file
1316 name. If the generic linker put a null string into
1317 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1318 there is a DT_SONAME entry. */
1320 name
= bfd_get_filename (abfd
);
1321 if (elf_dt_name (abfd
) != NULL
)
1323 name
= elf_dt_name (abfd
);
1326 if (elf_dt_soname (abfd
) != NULL
)
1332 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1335 Elf_External_Dyn
*dynbuf
= NULL
;
1336 Elf_External_Dyn
*extdyn
;
1337 Elf_External_Dyn
*extdynend
;
1339 unsigned long shlink
;
1341 dynbuf
= (Elf_External_Dyn
*) bfd_malloc (s
->_raw_size
);
1345 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
1346 (file_ptr
) 0, s
->_raw_size
))
1347 goto error_free_dyn
;
1349 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1351 goto error_free_dyn
;
1352 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1355 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
1356 for (; extdyn
< extdynend
; extdyn
++)
1358 Elf_Internal_Dyn dyn
;
1360 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
1361 if (dyn
.d_tag
== DT_SONAME
)
1363 unsigned int tagv
= dyn
.d_un
.d_val
;
1364 name
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1366 goto error_free_dyn
;
1368 if (dyn
.d_tag
== DT_NEEDED
)
1370 struct bfd_link_needed_list
*n
, **pn
;
1372 unsigned int tagv
= dyn
.d_un
.d_val
;
1374 amt
= sizeof (struct bfd_link_needed_list
);
1375 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1376 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1377 if (n
== NULL
|| fnm
== NULL
)
1378 goto error_free_dyn
;
1379 amt
= strlen (fnm
) + 1;
1380 anm
= bfd_alloc (abfd
, amt
);
1382 goto error_free_dyn
;
1383 memcpy (anm
, fnm
, (size_t) amt
);
1387 for (pn
= & hash_table
->needed
;
1393 if (dyn
.d_tag
== DT_RUNPATH
)
1395 struct bfd_link_needed_list
*n
, **pn
;
1397 unsigned int tagv
= dyn
.d_un
.d_val
;
1399 amt
= sizeof (struct bfd_link_needed_list
);
1400 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1401 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1402 if (n
== NULL
|| fnm
== NULL
)
1403 goto error_free_dyn
;
1404 amt
= strlen (fnm
) + 1;
1405 anm
= bfd_alloc (abfd
, amt
);
1407 goto error_free_dyn
;
1408 memcpy (anm
, fnm
, (size_t) amt
);
1412 for (pn
= & runpath
;
1418 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1419 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
1421 struct bfd_link_needed_list
*n
, **pn
;
1423 unsigned int tagv
= dyn
.d_un
.d_val
;
1425 amt
= sizeof (struct bfd_link_needed_list
);
1426 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1427 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1428 if (n
== NULL
|| fnm
== NULL
)
1429 goto error_free_dyn
;
1430 amt
= strlen (fnm
) + 1;
1431 anm
= bfd_alloc (abfd
, amt
);
1438 memcpy (anm
, fnm
, (size_t) amt
);
1453 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
1454 frees all more recently bfd_alloc'd blocks as well. */
1460 struct bfd_link_needed_list
**pn
;
1461 for (pn
= & hash_table
->runpath
;
1468 /* We do not want to include any of the sections in a dynamic
1469 object in the output file. We hack by simply clobbering the
1470 list of sections in the BFD. This could be handled more
1471 cleanly by, say, a new section flag; the existing
1472 SEC_NEVER_LOAD flag is not the one we want, because that one
1473 still implies that the section takes up space in the output
1475 bfd_section_list_clear (abfd
);
1477 /* If this is the first dynamic object found in the link, create
1478 the special sections required for dynamic linking. */
1479 if (! hash_table
->dynamic_sections_created
)
1480 if (! elf_link_create_dynamic_sections (abfd
, info
))
1485 /* Add a DT_NEEDED entry for this dynamic object. */
1486 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
1487 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, name
, FALSE
);
1488 if (strindex
== (bfd_size_type
) -1)
1491 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
1494 Elf_External_Dyn
*dyncon
, *dynconend
;
1496 /* The hash table size did not change, which means that
1497 the dynamic object name was already entered. If we
1498 have already included this dynamic object in the
1499 link, just ignore it. There is no reason to include
1500 a particular dynamic object more than once. */
1501 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
1502 BFD_ASSERT (sdyn
!= NULL
);
1504 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1505 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1507 for (; dyncon
< dynconend
; dyncon
++)
1509 Elf_Internal_Dyn dyn
;
1511 elf_swap_dyn_in (hash_table
->dynobj
, dyncon
, & dyn
);
1512 if (dyn
.d_tag
== DT_NEEDED
1513 && dyn
.d_un
.d_val
== strindex
)
1515 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
1521 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
1525 /* Save the SONAME, if there is one, because sometimes the
1526 linker emulation code will need to know it. */
1528 name
= basename (bfd_get_filename (abfd
));
1529 elf_dt_name (abfd
) = name
;
1532 /* If this is a dynamic object, we always link against the .dynsym
1533 symbol table, not the .symtab symbol table. The dynamic linker
1534 will only see the .dynsym symbol table, so there is no reason to
1535 look at .symtab for a dynamic object. */
1537 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
1538 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1540 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1542 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
1544 /* The sh_info field of the symtab header tells us where the
1545 external symbols start. We don't care about the local symbols at
1547 if (elf_bad_symtab (abfd
))
1549 extsymcount
= symcount
;
1554 extsymcount
= symcount
- hdr
->sh_info
;
1555 extsymoff
= hdr
->sh_info
;
1559 if (extsymcount
!= 0)
1561 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
1563 if (isymbuf
== NULL
)
1566 /* We store a pointer to the hash table entry for each external
1568 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
1569 sym_hash
= (struct elf_link_hash_entry
**) bfd_alloc (abfd
, amt
);
1570 if (sym_hash
== NULL
)
1571 goto error_free_sym
;
1572 elf_sym_hashes (abfd
) = sym_hash
;
1577 /* Read in any version definitions. */
1578 if (! _bfd_elf_slurp_version_tables (abfd
))
1579 goto error_free_sym
;
1581 /* Read in the symbol versions, but don't bother to convert them
1582 to internal format. */
1583 if (elf_dynversym (abfd
) != 0)
1585 Elf_Internal_Shdr
*versymhdr
;
1587 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
1588 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
1589 if (extversym
== NULL
)
1590 goto error_free_sym
;
1591 amt
= versymhdr
->sh_size
;
1592 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
1593 || bfd_bread ((PTR
) extversym
, amt
, abfd
) != amt
)
1594 goto error_free_vers
;
1600 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
1601 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
1603 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
1610 struct elf_link_hash_entry
*h
;
1611 bfd_boolean definition
;
1612 bfd_boolean size_change_ok
, type_change_ok
;
1613 bfd_boolean new_weakdef
;
1614 unsigned int old_alignment
;
1615 bfd_boolean override
;
1619 flags
= BSF_NO_FLAGS
;
1621 value
= isym
->st_value
;
1624 bind
= ELF_ST_BIND (isym
->st_info
);
1625 if (bind
== STB_LOCAL
)
1627 /* This should be impossible, since ELF requires that all
1628 global symbols follow all local symbols, and that sh_info
1629 point to the first global symbol. Unfortunatealy, Irix 5
1633 else if (bind
== STB_GLOBAL
)
1635 if (isym
->st_shndx
!= SHN_UNDEF
1636 && isym
->st_shndx
!= SHN_COMMON
)
1639 else if (bind
== STB_WEAK
)
1643 /* Leave it up to the processor backend. */
1646 if (isym
->st_shndx
== SHN_UNDEF
)
1647 sec
= bfd_und_section_ptr
;
1648 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
1650 sec
= section_from_elf_index (abfd
, isym
->st_shndx
);
1652 sec
= bfd_abs_section_ptr
;
1653 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
1656 else if (isym
->st_shndx
== SHN_ABS
)
1657 sec
= bfd_abs_section_ptr
;
1658 else if (isym
->st_shndx
== SHN_COMMON
)
1660 sec
= bfd_com_section_ptr
;
1661 /* What ELF calls the size we call the value. What ELF
1662 calls the value we call the alignment. */
1663 value
= isym
->st_size
;
1667 /* Leave it up to the processor backend. */
1670 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
1672 if (name
== (const char *) NULL
)
1673 goto error_free_vers
;
1675 if (isym
->st_shndx
== SHN_COMMON
1676 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
1678 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
1682 tcomm
= bfd_make_section (abfd
, ".tcommon");
1684 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
1686 | SEC_LINKER_CREATED
1687 | SEC_THREAD_LOCAL
)))
1688 goto error_free_vers
;
1692 else if (add_symbol_hook
)
1694 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
1696 goto error_free_vers
;
1698 /* The hook function sets the name to NULL if this symbol
1699 should be skipped for some reason. */
1700 if (name
== (const char *) NULL
)
1704 /* Sanity check that all possibilities were handled. */
1705 if (sec
== (asection
*) NULL
)
1707 bfd_set_error (bfd_error_bad_value
);
1708 goto error_free_vers
;
1711 if (bfd_is_und_section (sec
)
1712 || bfd_is_com_section (sec
))
1717 size_change_ok
= FALSE
;
1718 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
1720 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1722 Elf_Internal_Versym iver
;
1723 unsigned int vernum
= 0;
1727 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
1728 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
1730 /* If this is a hidden symbol, or if it is not version
1731 1, we append the version name to the symbol name.
1732 However, we do not modify a non-hidden absolute
1733 symbol, because it might be the version symbol
1734 itself. FIXME: What if it isn't? */
1735 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
1736 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
1739 size_t namelen
, verlen
, newlen
;
1742 if (isym
->st_shndx
!= SHN_UNDEF
)
1744 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
1746 (*_bfd_error_handler
)
1747 (_("%s: %s: invalid version %u (max %d)"),
1748 bfd_archive_filename (abfd
), name
, vernum
,
1749 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1750 bfd_set_error (bfd_error_bad_value
);
1751 goto error_free_vers
;
1753 else if (vernum
> 1)
1755 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1761 /* We cannot simply test for the number of
1762 entries in the VERNEED section since the
1763 numbers for the needed versions do not start
1765 Elf_Internal_Verneed
*t
;
1768 for (t
= elf_tdata (abfd
)->verref
;
1772 Elf_Internal_Vernaux
*a
;
1774 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1776 if (a
->vna_other
== vernum
)
1778 verstr
= a
->vna_nodename
;
1787 (*_bfd_error_handler
)
1788 (_("%s: %s: invalid needed version %d"),
1789 bfd_archive_filename (abfd
), name
, vernum
);
1790 bfd_set_error (bfd_error_bad_value
);
1791 goto error_free_vers
;
1795 namelen
= strlen (name
);
1796 verlen
= strlen (verstr
);
1797 newlen
= namelen
+ verlen
+ 2;
1798 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1799 && isym
->st_shndx
!= SHN_UNDEF
)
1802 newname
= (char *) bfd_alloc (abfd
, (bfd_size_type
) newlen
);
1803 if (newname
== NULL
)
1804 goto error_free_vers
;
1805 memcpy (newname
, name
, namelen
);
1806 p
= newname
+ namelen
;
1808 /* If this is a defined non-hidden version symbol,
1809 we add another @ to the name. This indicates the
1810 default version of the symbol. */
1811 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1812 && isym
->st_shndx
!= SHN_UNDEF
)
1814 memcpy (p
, verstr
, verlen
+ 1);
1820 if (! elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
1821 sym_hash
, &override
, &type_change_ok
,
1822 &size_change_ok
, dt_needed
))
1823 goto error_free_vers
;
1829 while (h
->root
.type
== bfd_link_hash_indirect
1830 || h
->root
.type
== bfd_link_hash_warning
)
1831 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1833 /* Remember the old alignment if this is a common symbol, so
1834 that we don't reduce the alignment later on. We can't
1835 check later, because _bfd_generic_link_add_one_symbol
1836 will set a default for the alignment which we want to
1838 if (h
->root
.type
== bfd_link_hash_common
)
1839 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1841 if (elf_tdata (abfd
)->verdef
!= NULL
1845 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1848 if (! (_bfd_generic_link_add_one_symbol
1849 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
1850 FALSE
, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
1851 goto error_free_vers
;
1854 while (h
->root
.type
== bfd_link_hash_indirect
1855 || h
->root
.type
== bfd_link_hash_warning
)
1856 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1859 new_weakdef
= FALSE
;
1862 && (flags
& BSF_WEAK
) != 0
1863 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
1864 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1865 && h
->weakdef
== NULL
)
1867 /* Keep a list of all weak defined non function symbols from
1868 a dynamic object, using the weakdef field. Later in this
1869 function we will set the weakdef field to the correct
1870 value. We only put non-function symbols from dynamic
1871 objects on this list, because that happens to be the only
1872 time we need to know the normal symbol corresponding to a
1873 weak symbol, and the information is time consuming to
1874 figure out. If the weakdef field is not already NULL,
1875 then this symbol was already defined by some previous
1876 dynamic object, and we will be using that previous
1877 definition anyhow. */
1884 /* Set the alignment of a common symbol. */
1885 if (isym
->st_shndx
== SHN_COMMON
1886 && h
->root
.type
== bfd_link_hash_common
)
1890 align
= bfd_log2 (isym
->st_value
);
1891 if (align
> old_alignment
1892 /* Permit an alignment power of zero if an alignment of one
1893 is specified and no other alignments have been specified. */
1894 || (isym
->st_value
== 1 && old_alignment
== 0))
1895 h
->root
.u
.c
.p
->alignment_power
= align
;
1898 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1904 /* Remember the symbol size and type. */
1905 if (isym
->st_size
!= 0
1906 && (definition
|| h
->size
== 0))
1908 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
1909 (*_bfd_error_handler
)
1910 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1911 name
, (unsigned long) h
->size
,
1912 (unsigned long) isym
->st_size
, bfd_archive_filename (abfd
));
1914 h
->size
= isym
->st_size
;
1917 /* If this is a common symbol, then we always want H->SIZE
1918 to be the size of the common symbol. The code just above
1919 won't fix the size if a common symbol becomes larger. We
1920 don't warn about a size change here, because that is
1921 covered by --warn-common. */
1922 if (h
->root
.type
== bfd_link_hash_common
)
1923 h
->size
= h
->root
.u
.c
.size
;
1925 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
1926 && (definition
|| h
->type
== STT_NOTYPE
))
1928 if (h
->type
!= STT_NOTYPE
1929 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
1930 && ! type_change_ok
)
1931 (*_bfd_error_handler
)
1932 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1933 name
, h
->type
, ELF_ST_TYPE (isym
->st_info
),
1934 bfd_archive_filename (abfd
));
1936 h
->type
= ELF_ST_TYPE (isym
->st_info
);
1939 /* If st_other has a processor-specific meaning, specific code
1940 might be needed here. */
1941 if (isym
->st_other
!= 0)
1943 unsigned char hvis
, symvis
, other
, nvis
;
1945 /* Take the balance of OTHER from the definition. */
1946 other
= (definition
? isym
->st_other
: h
->other
);
1947 other
&= ~ ELF_ST_VISIBILITY (-1);
1949 /* Combine visibilities, using the most constraining one. */
1950 hvis
= ELF_ST_VISIBILITY (h
->other
);
1951 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
1957 nvis
= hvis
< symvis
? hvis
: symvis
;
1959 h
->other
= other
| nvis
;
1962 /* Set a flag in the hash table entry indicating the type of
1963 reference or definition we just found. Keep a count of
1964 the number of dynamic symbols we find. A dynamic symbol
1965 is one which is referenced or defined by both a regular
1966 object and a shared object. */
1967 old_flags
= h
->elf_link_hash_flags
;
1973 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
1974 if (bind
!= STB_WEAK
)
1975 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
1978 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
1980 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1981 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
1987 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
1989 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
1990 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
1991 | ELF_LINK_HASH_REF_REGULAR
)) != 0
1992 || (h
->weakdef
!= NULL
1994 && h
->weakdef
->dynindx
!= -1))
1998 h
->elf_link_hash_flags
|= new_flag
;
2000 /* Check to see if we need to add an indirect symbol for
2001 the default name. */
2002 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
2003 if (! elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
2004 &sec
, &value
, &dynsym
,
2005 override
, dt_needed
))
2006 goto error_free_vers
;
2008 if (definition
&& (abfd
->flags
& DYNAMIC
) == 0)
2010 char *p
= strchr (name
, ELF_VER_CHR
);
2011 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
2013 /* Queue non-default versions so that .symver x, x@FOO
2014 aliases can be checked. */
2015 if (! nondeflt_vers
)
2017 amt
= (isymend
- isym
+ 1)
2018 * sizeof (struct elf_link_hash_entry
*);
2019 nondeflt_vers
= bfd_malloc (amt
);
2021 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
2025 if (dynsym
&& h
->dynindx
== -1)
2027 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2028 goto error_free_vers
;
2029 if (h
->weakdef
!= NULL
2031 && h
->weakdef
->dynindx
== -1)
2033 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2034 goto error_free_vers
;
2037 else if (dynsym
&& h
->dynindx
!= -1)
2038 /* If the symbol already has a dynamic index, but
2039 visibility says it should not be visible, turn it into
2041 switch (ELF_ST_VISIBILITY (h
->other
))
2045 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2049 if (dt_needed
&& definition
2050 && (h
->elf_link_hash_flags
2051 & ELF_LINK_HASH_REF_REGULAR
) != 0)
2053 bfd_size_type oldsize
;
2054 bfd_size_type strindex
;
2056 if (! is_elf_hash_table (info
))
2057 goto error_free_vers
;
2059 /* The symbol from a DT_NEEDED object is referenced from
2060 the regular object to create a dynamic executable. We
2061 have to make sure there is a DT_NEEDED entry for it. */
2064 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2065 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
,
2066 elf_dt_soname (abfd
), FALSE
);
2067 if (strindex
== (bfd_size_type
) -1)
2068 goto error_free_vers
;
2070 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2073 Elf_External_Dyn
*dyncon
, *dynconend
;
2075 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
,
2077 BFD_ASSERT (sdyn
!= NULL
);
2079 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
2080 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
2082 for (; dyncon
< dynconend
; dyncon
++)
2084 Elf_Internal_Dyn dyn
;
2086 elf_swap_dyn_in (hash_table
->dynobj
,
2088 BFD_ASSERT (dyn
.d_tag
!= DT_NEEDED
||
2089 dyn
.d_un
.d_val
!= strindex
);
2093 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
2094 goto error_free_vers
;
2099 /* Now that all the symbols from this input file are created, handle
2100 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
2101 if (nondeflt_vers
!= NULL
)
2103 bfd_size_type cnt
, symidx
;
2105 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
2107 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
2108 char *shortname
, *p
;
2110 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2112 || (h
->root
.type
!= bfd_link_hash_defined
2113 && h
->root
.type
!= bfd_link_hash_defweak
))
2116 amt
= p
- h
->root
.root
.string
;
2117 shortname
= bfd_malloc (amt
+ 1);
2118 memcpy (shortname
, h
->root
.root
.string
, amt
);
2119 shortname
[amt
] = '\0';
2121 hi
= (struct elf_link_hash_entry
*)
2122 bfd_link_hash_lookup (info
->hash
, shortname
,
2123 FALSE
, FALSE
, FALSE
);
2125 && hi
->root
.type
== h
->root
.type
2126 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
2127 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
2129 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
2130 hi
->root
.type
= bfd_link_hash_indirect
;
2131 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
2132 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
2133 sym_hash
= elf_sym_hashes (abfd
);
2135 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
2136 if (sym_hash
[symidx
] == hi
)
2138 sym_hash
[symidx
] = h
;
2144 free (nondeflt_vers
);
2145 nondeflt_vers
= NULL
;
2148 if (extversym
!= NULL
)
2154 if (isymbuf
!= NULL
)
2158 /* Now set the weakdefs field correctly for all the weak defined
2159 symbols we found. The only way to do this is to search all the
2160 symbols. Since we only need the information for non functions in
2161 dynamic objects, that's the only time we actually put anything on
2162 the list WEAKS. We need this information so that if a regular
2163 object refers to a symbol defined weakly in a dynamic object, the
2164 real symbol in the dynamic object is also put in the dynamic
2165 symbols; we also must arrange for both symbols to point to the
2166 same memory location. We could handle the general case of symbol
2167 aliasing, but a general symbol alias can only be generated in
2168 assembler code, handling it correctly would be very time
2169 consuming, and other ELF linkers don't handle general aliasing
2171 while (weaks
!= NULL
)
2173 struct elf_link_hash_entry
*hlook
;
2176 struct elf_link_hash_entry
**hpp
;
2177 struct elf_link_hash_entry
**hppend
;
2180 weaks
= hlook
->weakdef
;
2181 hlook
->weakdef
= NULL
;
2183 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
2184 || hlook
->root
.type
== bfd_link_hash_defweak
2185 || hlook
->root
.type
== bfd_link_hash_common
2186 || hlook
->root
.type
== bfd_link_hash_indirect
);
2187 slook
= hlook
->root
.u
.def
.section
;
2188 vlook
= hlook
->root
.u
.def
.value
;
2190 hpp
= elf_sym_hashes (abfd
);
2191 hppend
= hpp
+ extsymcount
;
2192 for (; hpp
< hppend
; hpp
++)
2194 struct elf_link_hash_entry
*h
;
2197 if (h
!= NULL
&& h
!= hlook
2198 && h
->root
.type
== bfd_link_hash_defined
2199 && h
->root
.u
.def
.section
== slook
2200 && h
->root
.u
.def
.value
== vlook
)
2204 /* If the weak definition is in the list of dynamic
2205 symbols, make sure the real definition is put there
2207 if (hlook
->dynindx
!= -1
2208 && h
->dynindx
== -1)
2210 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2214 /* If the real definition is in the list of dynamic
2215 symbols, make sure the weak definition is put there
2216 as well. If we don't do this, then the dynamic
2217 loader might not merge the entries for the real
2218 definition and the weak definition. */
2219 if (h
->dynindx
!= -1
2220 && hlook
->dynindx
== -1)
2222 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
2230 /* If this object is the same format as the output object, and it is
2231 not a shared library, then let the backend look through the
2234 This is required to build global offset table entries and to
2235 arrange for dynamic relocs. It is not required for the
2236 particular common case of linking non PIC code, even when linking
2237 against shared libraries, but unfortunately there is no way of
2238 knowing whether an object file has been compiled PIC or not.
2239 Looking through the relocs is not particularly time consuming.
2240 The problem is that we must either (1) keep the relocs in memory,
2241 which causes the linker to require additional runtime memory or
2242 (2) read the relocs twice from the input file, which wastes time.
2243 This would be a good case for using mmap.
2245 I have no idea how to handle linking PIC code into a file of a
2246 different format. It probably can't be done. */
2247 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
2249 && abfd
->xvec
== info
->hash
->creator
2250 && check_relocs
!= NULL
)
2254 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2256 Elf_Internal_Rela
*internal_relocs
;
2259 if ((o
->flags
& SEC_RELOC
) == 0
2260 || o
->reloc_count
== 0
2261 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
2262 && (o
->flags
& SEC_DEBUGGING
) != 0)
2263 || bfd_is_abs_section (o
->output_section
))
2266 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
2267 (abfd
, o
, (PTR
) NULL
,
2268 (Elf_Internal_Rela
*) NULL
,
2269 info
->keep_memory
));
2270 if (internal_relocs
== NULL
)
2273 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
2275 if (elf_section_data (o
)->relocs
!= internal_relocs
)
2276 free (internal_relocs
);
2283 /* If this is a non-traditional link, try to optimize the handling
2284 of the .stab/.stabstr sections. */
2286 && ! info
->traditional_format
2287 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
2288 && is_elf_hash_table (info
)
2289 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
2291 asection
*stab
, *stabstr
;
2293 stab
= bfd_get_section_by_name (abfd
, ".stab");
2295 && (stab
->flags
& SEC_MERGE
) == 0
2296 && !bfd_is_abs_section (stab
->output_section
))
2298 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
2300 if (stabstr
!= NULL
)
2302 struct bfd_elf_section_data
*secdata
;
2304 secdata
= elf_section_data (stab
);
2305 if (! _bfd_link_section_stabs (abfd
,
2306 & hash_table
->stab_info
,
2308 &secdata
->sec_info
))
2310 if (secdata
->sec_info
)
2311 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
2316 if (! info
->relocateable
&& ! dynamic
2317 && is_elf_hash_table (info
))
2321 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2322 if ((s
->flags
& SEC_MERGE
) != 0
2323 && !bfd_is_abs_section (s
->output_section
))
2325 struct bfd_elf_section_data
*secdata
;
2327 secdata
= elf_section_data (s
);
2328 if (! _bfd_merge_section (abfd
,
2329 & hash_table
->merge_info
,
2330 s
, &secdata
->sec_info
))
2332 else if (secdata
->sec_info
)
2333 s
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
2337 if (is_elf_hash_table (info
))
2339 /* Add this bfd to the loaded list. */
2340 struct elf_link_loaded_list
*n
;
2342 n
= ((struct elf_link_loaded_list
*)
2343 bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
)));
2347 n
->next
= hash_table
->loaded
;
2348 hash_table
->loaded
= n
;
2354 if (nondeflt_vers
!= NULL
)
2355 free (nondeflt_vers
);
2356 if (extversym
!= NULL
)
2359 if (isymbuf
!= NULL
)
2365 /* Create some sections which will be filled in with dynamic linking
2366 information. ABFD is an input file which requires dynamic sections
2367 to be created. The dynamic sections take up virtual memory space
2368 when the final executable is run, so we need to create them before
2369 addresses are assigned to the output sections. We work out the
2370 actual contents and size of these sections later. */
2373 elf_link_create_dynamic_sections (abfd
, info
)
2375 struct bfd_link_info
*info
;
2378 register asection
*s
;
2379 struct elf_link_hash_entry
*h
;
2380 struct bfd_link_hash_entry
*bh
;
2381 struct elf_backend_data
*bed
;
2383 if (! is_elf_hash_table (info
))
2386 if (elf_hash_table (info
)->dynamic_sections_created
)
2389 /* Make sure that all dynamic sections use the same input BFD. */
2390 if (elf_hash_table (info
)->dynobj
== NULL
)
2391 elf_hash_table (info
)->dynobj
= abfd
;
2393 abfd
= elf_hash_table (info
)->dynobj
;
2395 /* Note that we set the SEC_IN_MEMORY flag for all of these
2397 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
2398 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
2400 /* A dynamically linked executable has a .interp section, but a
2401 shared library does not. */
2404 s
= bfd_make_section (abfd
, ".interp");
2406 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2410 if (! info
->traditional_format
2411 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
2413 s
= bfd_make_section (abfd
, ".eh_frame_hdr");
2415 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2416 || ! bfd_set_section_alignment (abfd
, s
, 2))
2418 elf_hash_table (info
)->eh_info
.hdr_sec
= s
;
2421 /* Create sections to hold version informations. These are removed
2422 if they are not needed. */
2423 s
= bfd_make_section (abfd
, ".gnu.version_d");
2425 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2426 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2429 s
= bfd_make_section (abfd
, ".gnu.version");
2431 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2432 || ! bfd_set_section_alignment (abfd
, s
, 1))
2435 s
= bfd_make_section (abfd
, ".gnu.version_r");
2437 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2438 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2441 s
= bfd_make_section (abfd
, ".dynsym");
2443 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2444 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2447 s
= bfd_make_section (abfd
, ".dynstr");
2449 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2452 /* Create a strtab to hold the dynamic symbol names. */
2453 if (elf_hash_table (info
)->dynstr
== NULL
)
2455 elf_hash_table (info
)->dynstr
= _bfd_elf_strtab_init ();
2456 if (elf_hash_table (info
)->dynstr
== NULL
)
2460 s
= bfd_make_section (abfd
, ".dynamic");
2462 || ! bfd_set_section_flags (abfd
, s
, flags
)
2463 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2466 /* The special symbol _DYNAMIC is always set to the start of the
2467 .dynamic section. This call occurs before we have processed the
2468 symbols for any dynamic object, so we don't have to worry about
2469 overriding a dynamic definition. We could set _DYNAMIC in a
2470 linker script, but we only want to define it if we are, in fact,
2471 creating a .dynamic section. We don't want to define it if there
2472 is no .dynamic section, since on some ELF platforms the start up
2473 code examines it to decide how to initialize the process. */
2475 if (! (_bfd_generic_link_add_one_symbol
2476 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
2477 (const char *) 0, FALSE
, get_elf_backend_data (abfd
)->collect
, &bh
)))
2479 h
= (struct elf_link_hash_entry
*) bh
;
2480 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2481 h
->type
= STT_OBJECT
;
2484 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2487 bed
= get_elf_backend_data (abfd
);
2489 s
= bfd_make_section (abfd
, ".hash");
2491 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2492 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2494 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
2496 /* Let the backend create the rest of the sections. This lets the
2497 backend set the right flags. The backend will normally create
2498 the .got and .plt sections. */
2499 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
2502 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
2507 /* Add an entry to the .dynamic table. */
2510 elf_add_dynamic_entry (info
, tag
, val
)
2511 struct bfd_link_info
*info
;
2515 Elf_Internal_Dyn dyn
;
2518 bfd_size_type newsize
;
2519 bfd_byte
*newcontents
;
2521 if (! is_elf_hash_table (info
))
2524 dynobj
= elf_hash_table (info
)->dynobj
;
2526 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
2527 BFD_ASSERT (s
!= NULL
);
2529 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
2530 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
2531 if (newcontents
== NULL
)
2535 dyn
.d_un
.d_val
= val
;
2536 elf_swap_dyn_out (dynobj
, &dyn
,
2537 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
2539 s
->_raw_size
= newsize
;
2540 s
->contents
= newcontents
;
2545 /* Read and swap the relocs from the section indicated by SHDR. This
2546 may be either a REL or a RELA section. The relocations are
2547 translated into RELA relocations and stored in INTERNAL_RELOCS,
2548 which should have already been allocated to contain enough space.
2549 The EXTERNAL_RELOCS are a buffer where the external form of the
2550 relocations should be stored.
2552 Returns FALSE if something goes wrong. */
2555 elf_link_read_relocs_from_section (abfd
, shdr
, external_relocs
,
2558 Elf_Internal_Shdr
*shdr
;
2559 PTR external_relocs
;
2560 Elf_Internal_Rela
*internal_relocs
;
2562 struct elf_backend_data
*bed
;
2563 void (*swap_in
) PARAMS ((bfd
*, const bfd_byte
*, Elf_Internal_Rela
*));
2564 const bfd_byte
*erela
;
2565 const bfd_byte
*erelaend
;
2566 Elf_Internal_Rela
*irela
;
2568 /* If there aren't any relocations, that's OK. */
2572 /* Position ourselves at the start of the section. */
2573 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2576 /* Read the relocations. */
2577 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2580 bed
= get_elf_backend_data (abfd
);
2582 /* Convert the external relocations to the internal format. */
2583 if (shdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2584 swap_in
= bed
->s
->swap_reloc_in
;
2585 else if (shdr
->sh_entsize
== sizeof (Elf_External_Rela
))
2586 swap_in
= bed
->s
->swap_reloca_in
;
2589 bfd_set_error (bfd_error_wrong_format
);
2593 erela
= external_relocs
;
2594 erelaend
= erela
+ NUM_SHDR_ENTRIES (shdr
) * shdr
->sh_entsize
;
2595 irela
= internal_relocs
;
2596 while (erela
< erelaend
)
2598 (*swap_in
) (abfd
, erela
, irela
);
2599 irela
+= bed
->s
->int_rels_per_ext_rel
;
2600 erela
+= shdr
->sh_entsize
;
2606 /* Read and swap the relocs for a section O. They may have been
2607 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2608 not NULL, they are used as buffers to read into. They are known to
2609 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2610 the return value is allocated using either malloc or bfd_alloc,
2611 according to the KEEP_MEMORY argument. If O has two relocation
2612 sections (both REL and RELA relocations), then the REL_HDR
2613 relocations will appear first in INTERNAL_RELOCS, followed by the
2614 REL_HDR2 relocations. */
2617 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
2621 PTR external_relocs
;
2622 Elf_Internal_Rela
*internal_relocs
;
2623 bfd_boolean keep_memory
;
2625 Elf_Internal_Shdr
*rel_hdr
;
2627 Elf_Internal_Rela
*alloc2
= NULL
;
2628 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2630 if (elf_section_data (o
)->relocs
!= NULL
)
2631 return elf_section_data (o
)->relocs
;
2633 if (o
->reloc_count
== 0)
2636 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2638 if (internal_relocs
== NULL
)
2642 size
= o
->reloc_count
;
2643 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2645 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2647 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2648 if (internal_relocs
== NULL
)
2652 if (external_relocs
== NULL
)
2654 bfd_size_type size
= rel_hdr
->sh_size
;
2656 if (elf_section_data (o
)->rel_hdr2
)
2657 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2658 alloc1
= (PTR
) bfd_malloc (size
);
2661 external_relocs
= alloc1
;
2664 if (!elf_link_read_relocs_from_section (abfd
, rel_hdr
,
2668 if (!elf_link_read_relocs_from_section
2670 elf_section_data (o
)->rel_hdr2
,
2671 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2672 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2673 * bed
->s
->int_rels_per_ext_rel
)))
2676 /* Cache the results for next time, if we can. */
2678 elf_section_data (o
)->relocs
= internal_relocs
;
2683 /* Don't free alloc2, since if it was allocated we are passing it
2684 back (under the name of internal_relocs). */
2686 return internal_relocs
;
2696 /* Record an assignment to a symbol made by a linker script. We need
2697 this in case some dynamic object refers to this symbol. */
2700 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
2701 bfd
*output_bfd ATTRIBUTE_UNUSED
;
2702 struct bfd_link_info
*info
;
2704 bfd_boolean provide
;
2706 struct elf_link_hash_entry
*h
;
2708 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2711 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, TRUE
, FALSE
);
2715 if (h
->root
.type
== bfd_link_hash_new
)
2716 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_ELF
;
2718 /* If this symbol is being provided by the linker script, and it is
2719 currently defined by a dynamic object, but not by a regular
2720 object, then mark it as undefined so that the generic linker will
2721 force the correct value. */
2723 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2724 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2725 h
->root
.type
= bfd_link_hash_undefined
;
2727 /* If this symbol is not being provided by the linker script, and it is
2728 currently defined by a dynamic object, but not by a regular object,
2729 then clear out any version information because the symbol will not be
2730 associated with the dynamic object any more. */
2732 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2733 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2734 h
->verinfo
.verdef
= NULL
;
2736 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2738 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2739 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
2741 && h
->dynindx
== -1)
2743 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2746 /* If this is a weak defined symbol, and we know a corresponding
2747 real symbol from the same dynamic object, make sure the real
2748 symbol is also made into a dynamic symbol. */
2749 if (h
->weakdef
!= NULL
2750 && h
->weakdef
->dynindx
== -1)
2752 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2760 /* This structure is used to pass information to
2761 elf_link_assign_sym_version. */
2763 struct elf_assign_sym_version_info
2767 /* General link information. */
2768 struct bfd_link_info
*info
;
2770 struct bfd_elf_version_tree
*verdefs
;
2771 /* Whether we had a failure. */
2775 /* This structure is used to pass information to
2776 elf_link_find_version_dependencies. */
2778 struct elf_find_verdep_info
2782 /* General link information. */
2783 struct bfd_link_info
*info
;
2784 /* The number of dependencies. */
2786 /* Whether we had a failure. */
2790 /* Array used to determine the number of hash table buckets to use
2791 based on the number of symbols there are. If there are fewer than
2792 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2793 fewer than 37 we use 17 buckets, and so forth. We never use more
2794 than 32771 buckets. */
2796 static const size_t elf_buckets
[] =
2798 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2802 /* Compute bucket count for hashing table. We do not use a static set
2803 of possible tables sizes anymore. Instead we determine for all
2804 possible reasonable sizes of the table the outcome (i.e., the
2805 number of collisions etc) and choose the best solution. The
2806 weighting functions are not too simple to allow the table to grow
2807 without bounds. Instead one of the weighting factors is the size.
2808 Therefore the result is always a good payoff between few collisions
2809 (= short chain lengths) and table size. */
2811 compute_bucket_count (info
)
2812 struct bfd_link_info
*info
;
2814 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2815 size_t best_size
= 0;
2816 unsigned long int *hashcodes
;
2817 unsigned long int *hashcodesp
;
2818 unsigned long int i
;
2821 /* Compute the hash values for all exported symbols. At the same
2822 time store the values in an array so that we could use them for
2825 amt
*= sizeof (unsigned long int);
2826 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
2827 if (hashcodes
== NULL
)
2829 hashcodesp
= hashcodes
;
2831 /* Put all hash values in HASHCODES. */
2832 elf_link_hash_traverse (elf_hash_table (info
),
2833 elf_collect_hash_codes
, &hashcodesp
);
2835 /* We have a problem here. The following code to optimize the table
2836 size requires an integer type with more the 32 bits. If
2837 BFD_HOST_U_64_BIT is set we know about such a type. */
2838 #ifdef BFD_HOST_U_64_BIT
2841 unsigned long int nsyms
= hashcodesp
- hashcodes
;
2844 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
2845 unsigned long int *counts
;
2847 /* Possible optimization parameters: if we have NSYMS symbols we say
2848 that the hashing table must at least have NSYMS/4 and at most
2850 minsize
= nsyms
/ 4;
2853 best_size
= maxsize
= nsyms
* 2;
2855 /* Create array where we count the collisions in. We must use bfd_malloc
2856 since the size could be large. */
2858 amt
*= sizeof (unsigned long int);
2859 counts
= (unsigned long int *) bfd_malloc (amt
);
2866 /* Compute the "optimal" size for the hash table. The criteria is a
2867 minimal chain length. The minor criteria is (of course) the size
2869 for (i
= minsize
; i
< maxsize
; ++i
)
2871 /* Walk through the array of hashcodes and count the collisions. */
2872 BFD_HOST_U_64_BIT max
;
2873 unsigned long int j
;
2874 unsigned long int fact
;
2876 memset (counts
, '\0', i
* sizeof (unsigned long int));
2878 /* Determine how often each hash bucket is used. */
2879 for (j
= 0; j
< nsyms
; ++j
)
2880 ++counts
[hashcodes
[j
] % i
];
2882 /* For the weight function we need some information about the
2883 pagesize on the target. This is information need not be 100%
2884 accurate. Since this information is not available (so far) we
2885 define it here to a reasonable default value. If it is crucial
2886 to have a better value some day simply define this value. */
2887 # ifndef BFD_TARGET_PAGESIZE
2888 # define BFD_TARGET_PAGESIZE (4096)
2891 /* We in any case need 2 + NSYMS entries for the size values and
2893 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
2896 /* Variant 1: optimize for short chains. We add the squares
2897 of all the chain lengths (which favous many small chain
2898 over a few long chains). */
2899 for (j
= 0; j
< i
; ++j
)
2900 max
+= counts
[j
] * counts
[j
];
2902 /* This adds penalties for the overall size of the table. */
2903 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2906 /* Variant 2: Optimize a lot more for small table. Here we
2907 also add squares of the size but we also add penalties for
2908 empty slots (the +1 term). */
2909 for (j
= 0; j
< i
; ++j
)
2910 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
2912 /* The overall size of the table is considered, but not as
2913 strong as in variant 1, where it is squared. */
2914 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2918 /* Compare with current best results. */
2919 if (max
< best_chlen
)
2929 #endif /* defined (BFD_HOST_U_64_BIT) */
2931 /* This is the fallback solution if no 64bit type is available or if we
2932 are not supposed to spend much time on optimizations. We select the
2933 bucket count using a fixed set of numbers. */
2934 for (i
= 0; elf_buckets
[i
] != 0; i
++)
2936 best_size
= elf_buckets
[i
];
2937 if (dynsymcount
< elf_buckets
[i
+ 1])
2942 /* Free the arrays we needed. */
2948 /* Set up the sizes and contents of the ELF dynamic sections. This is
2949 called by the ELF linker emulation before_allocation routine. We
2950 must set the sizes of the sections before the linker sets the
2951 addresses of the various sections. */
2954 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
2956 auxiliary_filters
, info
, sinterpptr
,
2961 const char *filter_shlib
;
2962 const char * const *auxiliary_filters
;
2963 struct bfd_link_info
*info
;
2964 asection
**sinterpptr
;
2965 struct bfd_elf_version_tree
*verdefs
;
2967 bfd_size_type soname_indx
;
2969 struct elf_backend_data
*bed
;
2970 struct elf_assign_sym_version_info asvinfo
;
2974 soname_indx
= (bfd_size_type
) -1;
2976 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2979 if (! is_elf_hash_table (info
))
2982 /* Any syms created from now on start with -1 in
2983 got.refcount/offset and plt.refcount/offset. */
2984 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
2986 /* The backend may have to create some sections regardless of whether
2987 we're dynamic or not. */
2988 bed
= get_elf_backend_data (output_bfd
);
2989 if (bed
->elf_backend_always_size_sections
2990 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
2993 dynobj
= elf_hash_table (info
)->dynobj
;
2995 /* If there were no dynamic objects in the link, there is nothing to
3000 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
3003 if (elf_hash_table (info
)->dynamic_sections_created
)
3005 struct elf_info_failed eif
;
3006 struct elf_link_hash_entry
*h
;
3008 struct bfd_elf_version_tree
*t
;
3009 struct bfd_elf_version_expr
*d
;
3010 bfd_boolean all_defined
;
3012 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
3013 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
3017 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3019 if (soname_indx
== (bfd_size_type
) -1
3020 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SONAME
,
3027 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMBOLIC
,
3030 info
->flags
|= DF_SYMBOLIC
;
3037 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
3039 if (info
->new_dtags
)
3040 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
3041 if (indx
== (bfd_size_type
) -1
3042 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RPATH
, indx
)
3044 && ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RUNPATH
,
3049 if (filter_shlib
!= NULL
)
3053 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3054 filter_shlib
, TRUE
);
3055 if (indx
== (bfd_size_type
) -1
3056 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FILTER
, indx
))
3060 if (auxiliary_filters
!= NULL
)
3062 const char * const *p
;
3064 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
3068 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3070 if (indx
== (bfd_size_type
) -1
3071 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_AUXILIARY
,
3078 eif
.verdefs
= verdefs
;
3081 /* If we are supposed to export all symbols into the dynamic symbol
3082 table (this is not the normal case), then do so. */
3083 if (info
->export_dynamic
)
3085 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
3091 /* Make all global versions with definiton. */
3092 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3093 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3094 if (!d
->symver
&& strchr (d
->pattern
, '*') == NULL
)
3096 const char *verstr
, *name
;
3097 size_t namelen
, verlen
, newlen
;
3099 struct elf_link_hash_entry
*newh
;
3102 namelen
= strlen (name
);
3104 verlen
= strlen (verstr
);
3105 newlen
= namelen
+ verlen
+ 3;
3107 newname
= (char *) bfd_malloc ((bfd_size_type
) newlen
);
3108 if (newname
== NULL
)
3110 memcpy (newname
, name
, namelen
);
3112 /* Check the hidden versioned definition. */
3113 p
= newname
+ namelen
;
3115 memcpy (p
, verstr
, verlen
+ 1);
3116 newh
= elf_link_hash_lookup (elf_hash_table (info
),
3117 newname
, FALSE
, FALSE
,
3120 || (newh
->root
.type
!= bfd_link_hash_defined
3121 && newh
->root
.type
!= bfd_link_hash_defweak
))
3123 /* Check the default versioned definition. */
3125 memcpy (p
, verstr
, verlen
+ 1);
3126 newh
= elf_link_hash_lookup (elf_hash_table (info
),
3127 newname
, FALSE
, FALSE
,
3132 /* Mark this version if there is a definition and it is
3133 not defined in a shared object. */
3135 && ((newh
->elf_link_hash_flags
3136 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)
3137 && (newh
->root
.type
== bfd_link_hash_defined
3138 || newh
->root
.type
== bfd_link_hash_defweak
))
3142 /* Attach all the symbols to their version information. */
3143 asvinfo
.output_bfd
= output_bfd
;
3144 asvinfo
.info
= info
;
3145 asvinfo
.verdefs
= verdefs
;
3146 asvinfo
.failed
= FALSE
;
3148 elf_link_hash_traverse (elf_hash_table (info
),
3149 elf_link_assign_sym_version
,
3154 if (!info
->allow_undefined_version
)
3156 /* Check if all global versions have a definiton. */
3158 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3159 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3160 if (!d
->symver
&& !d
->script
3161 && strchr (d
->pattern
, '*') == NULL
)
3163 (*_bfd_error_handler
)
3164 (_("%s: undefined version: %s"),
3165 d
->pattern
, t
->name
);
3166 all_defined
= FALSE
;
3171 bfd_set_error (bfd_error_bad_value
);
3176 /* Find all symbols which were defined in a dynamic object and make
3177 the backend pick a reasonable value for them. */
3178 elf_link_hash_traverse (elf_hash_table (info
),
3179 elf_adjust_dynamic_symbol
,
3184 /* Add some entries to the .dynamic section. We fill in some of the
3185 values later, in elf_bfd_final_link, but we must add the entries
3186 now so that we know the final size of the .dynamic section. */
3188 /* If there are initialization and/or finalization functions to
3189 call then add the corresponding DT_INIT/DT_FINI entries. */
3190 h
= (info
->init_function
3191 ? elf_link_hash_lookup (elf_hash_table (info
),
3192 info
->init_function
, FALSE
,
3196 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3197 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3199 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT
, (bfd_vma
) 0))
3202 h
= (info
->fini_function
3203 ? elf_link_hash_lookup (elf_hash_table (info
),
3204 info
->fini_function
, FALSE
,
3208 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3209 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3211 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI
, (bfd_vma
) 0))
3215 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
3217 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3223 for (sub
= info
->input_bfds
; sub
!= NULL
;
3224 sub
= sub
->link_next
)
3225 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3226 if (elf_section_data (o
)->this_hdr
.sh_type
3227 == SHT_PREINIT_ARRAY
)
3229 (*_bfd_error_handler
)
3230 (_("%s: .preinit_array section is not allowed in DSO"),
3231 bfd_archive_filename (sub
));
3235 bfd_set_error (bfd_error_nonrepresentable_section
);
3239 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAY
,
3241 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAYSZ
,
3245 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
3247 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAY
,
3249 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAYSZ
,
3253 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
3255 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAY
,
3257 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAYSZ
,
3262 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
3263 /* If .dynstr is excluded from the link, we don't want any of
3264 these tags. Strictly, we should be checking each section
3265 individually; This quick check covers for the case where
3266 someone does a /DISCARD/ : { *(*) }. */
3267 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
3269 bfd_size_type strsize
;
3271 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3272 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_HASH
, (bfd_vma
) 0)
3273 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRTAB
, (bfd_vma
) 0)
3274 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMTAB
, (bfd_vma
) 0)
3275 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRSZ
, strsize
)
3276 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMENT
,
3277 (bfd_vma
) sizeof (Elf_External_Sym
)))
3282 /* The backend must work out the sizes of all the other dynamic
3284 if (bed
->elf_backend_size_dynamic_sections
3285 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
3288 if (elf_hash_table (info
)->dynamic_sections_created
)
3290 bfd_size_type dynsymcount
;
3292 size_t bucketcount
= 0;
3293 size_t hash_entry_size
;
3294 unsigned int dtagcount
;
3296 /* Set up the version definition section. */
3297 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3298 BFD_ASSERT (s
!= NULL
);
3300 /* We may have created additional version definitions if we are
3301 just linking a regular application. */
3302 verdefs
= asvinfo
.verdefs
;
3304 /* Skip anonymous version tag. */
3305 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
3306 verdefs
= verdefs
->next
;
3308 if (verdefs
== NULL
)
3309 _bfd_strip_section_from_output (info
, s
);
3314 struct bfd_elf_version_tree
*t
;
3316 Elf_Internal_Verdef def
;
3317 Elf_Internal_Verdaux defaux
;
3322 /* Make space for the base version. */
3323 size
+= sizeof (Elf_External_Verdef
);
3324 size
+= sizeof (Elf_External_Verdaux
);
3327 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3329 struct bfd_elf_version_deps
*n
;
3331 size
+= sizeof (Elf_External_Verdef
);
3332 size
+= sizeof (Elf_External_Verdaux
);
3335 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3336 size
+= sizeof (Elf_External_Verdaux
);
3339 s
->_raw_size
= size
;
3340 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3341 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3344 /* Fill in the version definition section. */
3348 def
.vd_version
= VER_DEF_CURRENT
;
3349 def
.vd_flags
= VER_FLG_BASE
;
3352 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3353 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3354 + sizeof (Elf_External_Verdaux
));
3356 if (soname_indx
!= (bfd_size_type
) -1)
3358 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3360 def
.vd_hash
= bfd_elf_hash (soname
);
3361 defaux
.vda_name
= soname_indx
;
3368 name
= basename (output_bfd
->filename
);
3369 def
.vd_hash
= bfd_elf_hash (name
);
3370 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3372 if (indx
== (bfd_size_type
) -1)
3374 defaux
.vda_name
= indx
;
3376 defaux
.vda_next
= 0;
3378 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3379 (Elf_External_Verdef
*) p
);
3380 p
+= sizeof (Elf_External_Verdef
);
3381 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3382 (Elf_External_Verdaux
*) p
);
3383 p
+= sizeof (Elf_External_Verdaux
);
3385 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3388 struct bfd_elf_version_deps
*n
;
3389 struct elf_link_hash_entry
*h
;
3390 struct bfd_link_hash_entry
*bh
;
3393 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3396 /* Add a symbol representing this version. */
3398 if (! (_bfd_generic_link_add_one_symbol
3399 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
3400 (bfd_vma
) 0, (const char *) NULL
, FALSE
,
3401 get_elf_backend_data (dynobj
)->collect
, &bh
)))
3403 h
= (struct elf_link_hash_entry
*) bh
;
3404 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
3405 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3406 h
->type
= STT_OBJECT
;
3407 h
->verinfo
.vertree
= t
;
3409 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
3412 def
.vd_version
= VER_DEF_CURRENT
;
3414 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
3415 def
.vd_flags
|= VER_FLG_WEAK
;
3416 def
.vd_ndx
= t
->vernum
+ 1;
3417 def
.vd_cnt
= cdeps
+ 1;
3418 def
.vd_hash
= bfd_elf_hash (t
->name
);
3419 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3420 if (t
->next
!= NULL
)
3421 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3422 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
3426 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3427 (Elf_External_Verdef
*) p
);
3428 p
+= sizeof (Elf_External_Verdef
);
3430 defaux
.vda_name
= h
->dynstr_index
;
3431 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3433 if (t
->deps
== NULL
)
3434 defaux
.vda_next
= 0;
3436 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3437 t
->name_indx
= defaux
.vda_name
;
3439 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3440 (Elf_External_Verdaux
*) p
);
3441 p
+= sizeof (Elf_External_Verdaux
);
3443 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3445 if (n
->version_needed
== NULL
)
3447 /* This can happen if there was an error in the
3449 defaux
.vda_name
= 0;
3453 defaux
.vda_name
= n
->version_needed
->name_indx
;
3454 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3457 if (n
->next
== NULL
)
3458 defaux
.vda_next
= 0;
3460 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3462 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3463 (Elf_External_Verdaux
*) p
);
3464 p
+= sizeof (Elf_External_Verdaux
);
3468 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEF
, (bfd_vma
) 0)
3469 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEFNUM
,
3473 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
3476 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
3478 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS
, info
->flags
))
3485 info
->flags_1
&= ~ (DF_1_INITFIRST
3488 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS_1
,
3493 /* Work out the size of the version reference section. */
3495 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3496 BFD_ASSERT (s
!= NULL
);
3498 struct elf_find_verdep_info sinfo
;
3500 sinfo
.output_bfd
= output_bfd
;
3502 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
3503 if (sinfo
.vers
== 0)
3505 sinfo
.failed
= FALSE
;
3507 elf_link_hash_traverse (elf_hash_table (info
),
3508 elf_link_find_version_dependencies
,
3511 if (elf_tdata (output_bfd
)->verref
== NULL
)
3512 _bfd_strip_section_from_output (info
, s
);
3515 Elf_Internal_Verneed
*t
;
3520 /* Build the version definition section. */
3523 for (t
= elf_tdata (output_bfd
)->verref
;
3527 Elf_Internal_Vernaux
*a
;
3529 size
+= sizeof (Elf_External_Verneed
);
3531 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3532 size
+= sizeof (Elf_External_Vernaux
);
3535 s
->_raw_size
= size
;
3536 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3537 if (s
->contents
== NULL
)
3541 for (t
= elf_tdata (output_bfd
)->verref
;
3546 Elf_Internal_Vernaux
*a
;
3550 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3553 t
->vn_version
= VER_NEED_CURRENT
;
3555 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3556 elf_dt_name (t
->vn_bfd
) != NULL
3557 ? elf_dt_name (t
->vn_bfd
)
3558 : basename (t
->vn_bfd
->filename
),
3560 if (indx
== (bfd_size_type
) -1)
3563 t
->vn_aux
= sizeof (Elf_External_Verneed
);
3564 if (t
->vn_nextref
== NULL
)
3567 t
->vn_next
= (sizeof (Elf_External_Verneed
)
3568 + caux
* sizeof (Elf_External_Vernaux
));
3570 _bfd_elf_swap_verneed_out (output_bfd
, t
,
3571 (Elf_External_Verneed
*) p
);
3572 p
+= sizeof (Elf_External_Verneed
);
3574 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3576 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
3577 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3578 a
->vna_nodename
, FALSE
);
3579 if (indx
== (bfd_size_type
) -1)
3582 if (a
->vna_nextptr
== NULL
)
3585 a
->vna_next
= sizeof (Elf_External_Vernaux
);
3587 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
3588 (Elf_External_Vernaux
*) p
);
3589 p
+= sizeof (Elf_External_Vernaux
);
3593 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEED
,
3595 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEEDNUM
,
3599 elf_tdata (output_bfd
)->cverrefs
= crefs
;
3603 /* Assign dynsym indicies. In a shared library we generate a
3604 section symbol for each output section, which come first.
3605 Next come all of the back-end allocated local dynamic syms,
3606 followed by the rest of the global symbols. */
3608 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3610 /* Work out the size of the symbol version section. */
3611 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3612 BFD_ASSERT (s
!= NULL
);
3613 if (dynsymcount
== 0
3614 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
3616 _bfd_strip_section_from_output (info
, s
);
3617 /* The DYNSYMCOUNT might have changed if we were going to
3618 output a dynamic symbol table entry for S. */
3619 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3623 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
3624 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3625 if (s
->contents
== NULL
)
3628 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERSYM
, (bfd_vma
) 0))
3632 /* Set the size of the .dynsym and .hash sections. We counted
3633 the number of dynamic symbols in elf_link_add_object_symbols.
3634 We will build the contents of .dynsym and .hash when we build
3635 the final symbol table, because until then we do not know the
3636 correct value to give the symbols. We built the .dynstr
3637 section as we went along in elf_link_add_object_symbols. */
3638 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
3639 BFD_ASSERT (s
!= NULL
);
3640 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
3641 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3642 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3645 if (dynsymcount
!= 0)
3647 Elf_Internal_Sym isym
;
3649 /* The first entry in .dynsym is a dummy symbol. */
3656 elf_swap_symbol_out (output_bfd
, &isym
, (PTR
) s
->contents
, (PTR
) 0);
3659 /* Compute the size of the hashing table. As a side effect this
3660 computes the hash values for all the names we export. */
3661 bucketcount
= compute_bucket_count (info
);
3663 s
= bfd_get_section_by_name (dynobj
, ".hash");
3664 BFD_ASSERT (s
!= NULL
);
3665 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
3666 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
3667 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3668 if (s
->contents
== NULL
)
3671 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) bucketcount
,
3673 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) dynsymcount
,
3674 s
->contents
+ hash_entry_size
);
3676 elf_hash_table (info
)->bucketcount
= bucketcount
;
3678 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
3679 BFD_ASSERT (s
!= NULL
);
3681 elf_finalize_dynstr (output_bfd
, info
);
3683 s
->_raw_size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3685 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
3686 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NULL
, (bfd_vma
) 0))
3693 /* This function is used to adjust offsets into .dynstr for
3694 dynamic symbols. This is called via elf_link_hash_traverse. */
3696 static bfd_boolean elf_adjust_dynstr_offsets
3697 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3700 elf_adjust_dynstr_offsets (h
, data
)
3701 struct elf_link_hash_entry
*h
;
3704 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3706 if (h
->root
.type
== bfd_link_hash_warning
)
3707 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3709 if (h
->dynindx
!= -1)
3710 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3714 /* Assign string offsets in .dynstr, update all structures referencing
3718 elf_finalize_dynstr (output_bfd
, info
)
3720 struct bfd_link_info
*info
;
3722 struct elf_link_local_dynamic_entry
*entry
;
3723 struct elf_strtab_hash
*dynstr
= elf_hash_table (info
)->dynstr
;
3724 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
3727 Elf_External_Dyn
*dyncon
, *dynconend
;
3729 _bfd_elf_strtab_finalize (dynstr
);
3730 size
= _bfd_elf_strtab_size (dynstr
);
3732 /* Update all .dynamic entries referencing .dynstr strings. */
3733 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3734 BFD_ASSERT (sdyn
!= NULL
);
3736 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
3737 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
3739 for (; dyncon
< dynconend
; dyncon
++)
3741 Elf_Internal_Dyn dyn
;
3743 elf_swap_dyn_in (dynobj
, dyncon
, & dyn
);
3747 dyn
.d_un
.d_val
= size
;
3748 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3756 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3757 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3764 /* Now update local dynamic symbols. */
3765 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
3766 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3767 entry
->isym
.st_name
);
3769 /* And the rest of dynamic symbols. */
3770 elf_link_hash_traverse (elf_hash_table (info
),
3771 elf_adjust_dynstr_offsets
, dynstr
);
3773 /* Adjust version definitions. */
3774 if (elf_tdata (output_bfd
)->cverdefs
)
3779 Elf_Internal_Verdef def
;
3780 Elf_Internal_Verdaux defaux
;
3782 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3783 p
= (bfd_byte
*) s
->contents
;
3786 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3788 p
+= sizeof (Elf_External_Verdef
);
3789 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3791 _bfd_elf_swap_verdaux_in (output_bfd
,
3792 (Elf_External_Verdaux
*) p
, &defaux
);
3793 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3795 _bfd_elf_swap_verdaux_out (output_bfd
,
3796 &defaux
, (Elf_External_Verdaux
*) p
);
3797 p
+= sizeof (Elf_External_Verdaux
);
3800 while (def
.vd_next
);
3803 /* Adjust version references. */
3804 if (elf_tdata (output_bfd
)->verref
)
3809 Elf_Internal_Verneed need
;
3810 Elf_Internal_Vernaux needaux
;
3812 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3813 p
= (bfd_byte
*) s
->contents
;
3816 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3818 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3819 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3820 (Elf_External_Verneed
*) p
);
3821 p
+= sizeof (Elf_External_Verneed
);
3822 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3824 _bfd_elf_swap_vernaux_in (output_bfd
,
3825 (Elf_External_Vernaux
*) p
, &needaux
);
3826 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3828 _bfd_elf_swap_vernaux_out (output_bfd
,
3830 (Elf_External_Vernaux
*) p
);
3831 p
+= sizeof (Elf_External_Vernaux
);
3834 while (need
.vn_next
);
3840 /* Fix up the flags for a symbol. This handles various cases which
3841 can only be fixed after all the input files are seen. This is
3842 currently called by both adjust_dynamic_symbol and
3843 assign_sym_version, which is unnecessary but perhaps more robust in
3844 the face of future changes. */
3847 elf_fix_symbol_flags (h
, eif
)
3848 struct elf_link_hash_entry
*h
;
3849 struct elf_info_failed
*eif
;
3851 /* If this symbol was mentioned in a non-ELF file, try to set
3852 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3853 permit a non-ELF file to correctly refer to a symbol defined in
3854 an ELF dynamic object. */
3855 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
3857 while (h
->root
.type
== bfd_link_hash_indirect
)
3858 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3860 if (h
->root
.type
!= bfd_link_hash_defined
3861 && h
->root
.type
!= bfd_link_hash_defweak
)
3862 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3863 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3866 if (h
->root
.u
.def
.section
->owner
!= NULL
3867 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3868 == bfd_target_elf_flavour
))
3869 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3870 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3872 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3875 if (h
->dynindx
== -1
3876 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
3877 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
3879 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3888 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3889 was first seen in a non-ELF file. Fortunately, if the symbol
3890 was first seen in an ELF file, we're probably OK unless the
3891 symbol was defined in a non-ELF file. Catch that case here.
3892 FIXME: We're still in trouble if the symbol was first seen in
3893 a dynamic object, and then later in a non-ELF regular object. */
3894 if ((h
->root
.type
== bfd_link_hash_defined
3895 || h
->root
.type
== bfd_link_hash_defweak
)
3896 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3897 && (h
->root
.u
.def
.section
->owner
!= NULL
3898 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3899 != bfd_target_elf_flavour
)
3900 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3901 && (h
->elf_link_hash_flags
3902 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
3903 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3906 /* If this is a final link, and the symbol was defined as a common
3907 symbol in a regular object file, and there was no definition in
3908 any dynamic object, then the linker will have allocated space for
3909 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3910 flag will not have been set. */
3911 if (h
->root
.type
== bfd_link_hash_defined
3912 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3913 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
3914 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3915 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3916 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3918 /* If -Bsymbolic was used (which means to bind references to global
3919 symbols to the definition within the shared object), and this
3920 symbol was defined in a regular object, then it actually doesn't
3921 need a PLT entry, and we can accomplish that by forcing it local.
3922 Likewise, if the symbol has hidden or internal visibility.
3923 FIXME: It might be that we also do not need a PLT for other
3924 non-hidden visibilities, but we would have to tell that to the
3925 backend specifically; we can't just clear PLT-related data here. */
3926 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
3927 && eif
->info
->shared
3928 && is_elf_hash_table (eif
->info
)
3929 && (eif
->info
->symbolic
3930 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3931 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3932 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3934 struct elf_backend_data
*bed
;
3935 bfd_boolean force_local
;
3937 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3939 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3940 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
3941 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
3944 /* If this is a weak defined symbol in a dynamic object, and we know
3945 the real definition in the dynamic object, copy interesting flags
3946 over to the real definition. */
3947 if (h
->weakdef
!= NULL
)
3949 struct elf_link_hash_entry
*weakdef
;
3951 weakdef
= h
->weakdef
;
3952 if (h
->root
.type
== bfd_link_hash_indirect
)
3953 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3955 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3956 || h
->root
.type
== bfd_link_hash_defweak
);
3957 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
3958 || weakdef
->root
.type
== bfd_link_hash_defweak
);
3959 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
3961 /* If the real definition is defined by a regular object file,
3962 don't do anything special. See the longer description in
3963 elf_adjust_dynamic_symbol, below. */
3964 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3968 struct elf_backend_data
*bed
;
3970 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3971 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
3978 /* Make the backend pick a good value for a dynamic symbol. This is
3979 called via elf_link_hash_traverse, and also calls itself
3983 elf_adjust_dynamic_symbol (h
, data
)
3984 struct elf_link_hash_entry
*h
;
3987 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3989 struct elf_backend_data
*bed
;
3991 if (! is_elf_hash_table (eif
->info
))
3994 if (h
->root
.type
== bfd_link_hash_warning
)
3996 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
3997 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
3999 /* When warning symbols are created, they **replace** the "real"
4000 entry in the hash table, thus we never get to see the real
4001 symbol in a hash traversal. So look at it now. */
4002 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4005 /* Ignore indirect symbols. These are added by the versioning code. */
4006 if (h
->root
.type
== bfd_link_hash_indirect
)
4009 /* Fix the symbol flags. */
4010 if (! elf_fix_symbol_flags (h
, eif
))
4013 /* If this symbol does not require a PLT entry, and it is not
4014 defined by a dynamic object, or is not referenced by a regular
4015 object, ignore it. We do have to handle a weak defined symbol,
4016 even if no regular object refers to it, if we decided to add it
4017 to the dynamic symbol table. FIXME: Do we normally need to worry
4018 about symbols which are defined by one dynamic object and
4019 referenced by another one? */
4020 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
4021 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4022 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4023 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
4024 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
4026 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
4030 /* If we've already adjusted this symbol, don't do it again. This
4031 can happen via a recursive call. */
4032 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
4035 /* Don't look at this symbol again. Note that we must set this
4036 after checking the above conditions, because we may look at a
4037 symbol once, decide not to do anything, and then get called
4038 recursively later after REF_REGULAR is set below. */
4039 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
4041 /* If this is a weak definition, and we know a real definition, and
4042 the real symbol is not itself defined by a regular object file,
4043 then get a good value for the real definition. We handle the
4044 real symbol first, for the convenience of the backend routine.
4046 Note that there is a confusing case here. If the real definition
4047 is defined by a regular object file, we don't get the real symbol
4048 from the dynamic object, but we do get the weak symbol. If the
4049 processor backend uses a COPY reloc, then if some routine in the
4050 dynamic object changes the real symbol, we will not see that
4051 change in the corresponding weak symbol. This is the way other
4052 ELF linkers work as well, and seems to be a result of the shared
4055 I will clarify this issue. Most SVR4 shared libraries define the
4056 variable _timezone and define timezone as a weak synonym. The
4057 tzset call changes _timezone. If you write
4058 extern int timezone;
4060 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4061 you might expect that, since timezone is a synonym for _timezone,
4062 the same number will print both times. However, if the processor
4063 backend uses a COPY reloc, then actually timezone will be copied
4064 into your process image, and, since you define _timezone
4065 yourself, _timezone will not. Thus timezone and _timezone will
4066 wind up at different memory locations. The tzset call will set
4067 _timezone, leaving timezone unchanged. */
4069 if (h
->weakdef
!= NULL
)
4071 /* If we get to this point, we know there is an implicit
4072 reference by a regular object file via the weak symbol H.
4073 FIXME: Is this really true? What if the traversal finds
4074 H->WEAKDEF before it finds H? */
4075 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
4077 if (! elf_adjust_dynamic_symbol (h
->weakdef
, (PTR
) eif
))
4081 /* If a symbol has no type and no size and does not require a PLT
4082 entry, then we are probably about to do the wrong thing here: we
4083 are probably going to create a COPY reloc for an empty object.
4084 This case can arise when a shared object is built with assembly
4085 code, and the assembly code fails to set the symbol type. */
4087 && h
->type
== STT_NOTYPE
4088 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
4089 (*_bfd_error_handler
)
4090 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4091 h
->root
.root
.string
);
4093 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
4094 bed
= get_elf_backend_data (dynobj
);
4095 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
4104 /* This routine is used to export all defined symbols into the dynamic
4105 symbol table. It is called via elf_link_hash_traverse. */
4108 elf_export_symbol (h
, data
)
4109 struct elf_link_hash_entry
*h
;
4112 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
4114 /* Ignore indirect symbols. These are added by the versioning code. */
4115 if (h
->root
.type
== bfd_link_hash_indirect
)
4118 if (h
->root
.type
== bfd_link_hash_warning
)
4119 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4121 if (h
->dynindx
== -1
4122 && (h
->elf_link_hash_flags
4123 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
4125 struct bfd_elf_version_tree
*t
;
4126 struct bfd_elf_version_expr
*d
;
4128 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
4130 if (t
->globals
!= NULL
)
4132 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4134 if ((*d
->match
) (d
, h
->root
.root
.string
))
4139 if (t
->locals
!= NULL
)
4141 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4143 if ((*d
->match
) (d
, h
->root
.root
.string
))
4152 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
4163 /* Look through the symbols which are defined in other shared
4164 libraries and referenced here. Update the list of version
4165 dependencies. This will be put into the .gnu.version_r section.
4166 This function is called via elf_link_hash_traverse. */
4169 elf_link_find_version_dependencies (h
, data
)
4170 struct elf_link_hash_entry
*h
;
4173 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
4174 Elf_Internal_Verneed
*t
;
4175 Elf_Internal_Vernaux
*a
;
4178 if (h
->root
.type
== bfd_link_hash_warning
)
4179 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4181 /* We only care about symbols defined in shared objects with version
4183 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4184 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4186 || h
->verinfo
.verdef
== NULL
)
4189 /* See if we already know about this version. */
4190 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
4192 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
4195 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4196 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
4202 /* This is a new version. Add it to tree we are building. */
4207 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4210 rinfo
->failed
= TRUE
;
4214 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
4215 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
4216 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
4220 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4222 /* Note that we are copying a string pointer here, and testing it
4223 above. If bfd_elf_string_from_elf_section is ever changed to
4224 discard the string data when low in memory, this will have to be
4226 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
4228 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
4229 a
->vna_nextptr
= t
->vn_auxptr
;
4231 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
4234 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4241 /* Figure out appropriate versions for all the symbols. We may not
4242 have the version number script until we have read all of the input
4243 files, so until that point we don't know which symbols should be
4244 local. This function is called via elf_link_hash_traverse. */
4247 elf_link_assign_sym_version (h
, data
)
4248 struct elf_link_hash_entry
*h
;
4251 struct elf_assign_sym_version_info
*sinfo
;
4252 struct bfd_link_info
*info
;
4253 struct elf_backend_data
*bed
;
4254 struct elf_info_failed eif
;
4258 sinfo
= (struct elf_assign_sym_version_info
*) data
;
4261 if (h
->root
.type
== bfd_link_hash_warning
)
4262 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4264 /* Fix the symbol flags. */
4267 if (! elf_fix_symbol_flags (h
, &eif
))
4270 sinfo
->failed
= TRUE
;
4274 /* We only need version numbers for symbols defined in regular
4276 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4279 bed
= get_elf_backend_data (sinfo
->output_bfd
);
4280 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4281 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
4283 struct bfd_elf_version_tree
*t
;
4288 /* There are two consecutive ELF_VER_CHR characters if this is
4289 not a hidden symbol. */
4291 if (*p
== ELF_VER_CHR
)
4297 /* If there is no version string, we can just return out. */
4301 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4305 /* Look for the version. If we find it, it is no longer weak. */
4306 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4308 if (strcmp (t
->name
, p
) == 0)
4312 struct bfd_elf_version_expr
*d
;
4314 len
= p
- h
->root
.root
.string
;
4315 alc
= bfd_malloc ((bfd_size_type
) len
);
4318 memcpy (alc
, h
->root
.root
.string
, len
- 1);
4319 alc
[len
- 1] = '\0';
4320 if (alc
[len
- 2] == ELF_VER_CHR
)
4321 alc
[len
- 2] = '\0';
4323 h
->verinfo
.vertree
= t
;
4327 if (t
->globals
!= NULL
)
4329 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4330 if ((*d
->match
) (d
, alc
))
4334 /* See if there is anything to force this symbol to
4336 if (d
== NULL
&& t
->locals
!= NULL
)
4338 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4340 if ((*d
->match
) (d
, alc
))
4342 if (h
->dynindx
!= -1
4344 && ! info
->export_dynamic
)
4346 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4359 /* If we are building an application, we need to create a
4360 version node for this version. */
4361 if (t
== NULL
&& ! info
->shared
)
4363 struct bfd_elf_version_tree
**pp
;
4366 /* If we aren't going to export this symbol, we don't need
4367 to worry about it. */
4368 if (h
->dynindx
== -1)
4372 t
= ((struct bfd_elf_version_tree
*)
4373 bfd_alloc (sinfo
->output_bfd
, amt
));
4376 sinfo
->failed
= TRUE
;
4385 t
->name_indx
= (unsigned int) -1;
4389 /* Don't count anonymous version tag. */
4390 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
4392 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4394 t
->vernum
= version_index
;
4398 h
->verinfo
.vertree
= t
;
4402 /* We could not find the version for a symbol when
4403 generating a shared archive. Return an error. */
4404 (*_bfd_error_handler
)
4405 (_("%s: undefined versioned symbol name %s"),
4406 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
4407 bfd_set_error (bfd_error_bad_value
);
4408 sinfo
->failed
= TRUE
;
4413 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4416 /* If we don't have a version for this symbol, see if we can find
4418 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
4420 struct bfd_elf_version_tree
*t
;
4421 struct bfd_elf_version_tree
*local_ver
;
4422 struct bfd_elf_version_expr
*d
;
4424 /* See if can find what version this symbol is in. If the
4425 symbol is supposed to be local, then don't actually register
4428 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4430 if (t
->globals
!= NULL
)
4432 bfd_boolean matched
;
4435 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4437 if ((*d
->match
) (d
, h
->root
.root
.string
))
4443 /* There is a version without definition. Make
4444 the symbol the default definition for this
4446 h
->verinfo
.vertree
= t
;
4457 /* There is no undefined version for this symbol. Hide the
4459 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4462 if (t
->locals
!= NULL
)
4464 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4466 /* If the match is "*", keep looking for a more
4467 explicit, perhaps even global, match. */
4468 if (d
->pattern
[0] == '*' && d
->pattern
[1] == '\0')
4470 else if ((*d
->match
) (d
, h
->root
.root
.string
))
4482 if (local_ver
!= NULL
)
4484 h
->verinfo
.vertree
= local_ver
;
4485 if (h
->dynindx
!= -1
4487 && ! info
->export_dynamic
)
4489 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4497 /* Final phase of ELF linker. */
4499 /* A structure we use to avoid passing large numbers of arguments. */
4501 struct elf_final_link_info
4503 /* General link information. */
4504 struct bfd_link_info
*info
;
4507 /* Symbol string table. */
4508 struct bfd_strtab_hash
*symstrtab
;
4509 /* .dynsym section. */
4510 asection
*dynsym_sec
;
4511 /* .hash section. */
4513 /* symbol version section (.gnu.version). */
4514 asection
*symver_sec
;
4515 /* first SHF_TLS section (if any). */
4516 asection
*first_tls_sec
;
4517 /* Buffer large enough to hold contents of any section. */
4519 /* Buffer large enough to hold external relocs of any section. */
4520 PTR external_relocs
;
4521 /* Buffer large enough to hold internal relocs of any section. */
4522 Elf_Internal_Rela
*internal_relocs
;
4523 /* Buffer large enough to hold external local symbols of any input
4525 Elf_External_Sym
*external_syms
;
4526 /* And a buffer for symbol section indices. */
4527 Elf_External_Sym_Shndx
*locsym_shndx
;
4528 /* Buffer large enough to hold internal local symbols of any input
4530 Elf_Internal_Sym
*internal_syms
;
4531 /* Array large enough to hold a symbol index for each local symbol
4532 of any input BFD. */
4534 /* Array large enough to hold a section pointer for each local
4535 symbol of any input BFD. */
4536 asection
**sections
;
4537 /* Buffer to hold swapped out symbols. */
4538 Elf_External_Sym
*symbuf
;
4539 /* And one for symbol section indices. */
4540 Elf_External_Sym_Shndx
*symshndxbuf
;
4541 /* Number of swapped out symbols in buffer. */
4542 size_t symbuf_count
;
4543 /* Number of symbols which fit in symbuf. */
4545 /* And same for symshndxbuf. */
4546 size_t shndxbuf_size
;
4549 static bfd_boolean elf_link_output_sym
4550 PARAMS ((struct elf_final_link_info
*, const char *,
4551 Elf_Internal_Sym
*, asection
*));
4552 static bfd_boolean elf_link_flush_output_syms
4553 PARAMS ((struct elf_final_link_info
*));
4554 static bfd_boolean elf_link_output_extsym
4555 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4556 static bfd_boolean elf_link_sec_merge_syms
4557 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4558 static bfd_boolean elf_link_check_versioned_symbol
4559 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
4560 static bfd_boolean elf_link_input_bfd
4561 PARAMS ((struct elf_final_link_info
*, bfd
*));
4562 static bfd_boolean elf_reloc_link_order
4563 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
4564 struct bfd_link_order
*));
4566 /* This struct is used to pass information to elf_link_output_extsym. */
4568 struct elf_outext_info
4571 bfd_boolean localsyms
;
4572 struct elf_final_link_info
*finfo
;
4575 /* Compute the size of, and allocate space for, REL_HDR which is the
4576 section header for a section containing relocations for O. */
4579 elf_link_size_reloc_section (abfd
, rel_hdr
, o
)
4581 Elf_Internal_Shdr
*rel_hdr
;
4584 bfd_size_type reloc_count
;
4585 bfd_size_type num_rel_hashes
;
4587 /* Figure out how many relocations there will be. */
4588 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
4589 reloc_count
= elf_section_data (o
)->rel_count
;
4591 reloc_count
= elf_section_data (o
)->rel_count2
;
4593 num_rel_hashes
= o
->reloc_count
;
4594 if (num_rel_hashes
< reloc_count
)
4595 num_rel_hashes
= reloc_count
;
4597 /* That allows us to calculate the size of the section. */
4598 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
4600 /* The contents field must last into write_object_contents, so we
4601 allocate it with bfd_alloc rather than malloc. Also since we
4602 cannot be sure that the contents will actually be filled in,
4603 we zero the allocated space. */
4604 rel_hdr
->contents
= (PTR
) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
4605 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
4608 /* We only allocate one set of hash entries, so we only do it the
4609 first time we are called. */
4610 if (elf_section_data (o
)->rel_hashes
== NULL
4613 struct elf_link_hash_entry
**p
;
4615 p
= ((struct elf_link_hash_entry
**)
4616 bfd_zmalloc (num_rel_hashes
4617 * sizeof (struct elf_link_hash_entry
*)));
4621 elf_section_data (o
)->rel_hashes
= p
;
4627 /* When performing a relocateable link, the input relocations are
4628 preserved. But, if they reference global symbols, the indices
4629 referenced must be updated. Update all the relocations in
4630 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4633 elf_link_adjust_relocs (abfd
, rel_hdr
, count
, rel_hash
)
4635 Elf_Internal_Shdr
*rel_hdr
;
4637 struct elf_link_hash_entry
**rel_hash
;
4640 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4642 void (*swap_in
) PARAMS ((bfd
*, const bfd_byte
*, Elf_Internal_Rela
*));
4643 void (*swap_out
) PARAMS ((bfd
*, const Elf_Internal_Rela
*, bfd_byte
*));
4645 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4647 swap_in
= bed
->s
->swap_reloc_in
;
4648 swap_out
= bed
->s
->swap_reloc_out
;
4650 else if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
))
4652 swap_in
= bed
->s
->swap_reloca_in
;
4653 swap_out
= bed
->s
->swap_reloca_out
;
4658 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
4661 erela
= rel_hdr
->contents
;
4662 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
4664 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
4667 if (*rel_hash
== NULL
)
4670 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
4672 (*swap_in
) (abfd
, erela
, irela
);
4673 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4674 irela
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4675 ELF_R_TYPE (irela
[j
].r_info
));
4676 (*swap_out
) (abfd
, irela
, erela
);
4680 struct elf_link_sort_rela
4683 enum elf_reloc_type_class type
;
4684 /* We use this as an array of size int_rels_per_ext_rel. */
4685 Elf_Internal_Rela rela
[1];
4689 elf_link_sort_cmp1 (A
, B
)
4693 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4694 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4695 int relativea
, relativeb
;
4697 relativea
= a
->type
== reloc_class_relative
;
4698 relativeb
= b
->type
== reloc_class_relative
;
4700 if (relativea
< relativeb
)
4702 if (relativea
> relativeb
)
4704 if (ELF_R_SYM (a
->rela
->r_info
) < ELF_R_SYM (b
->rela
->r_info
))
4706 if (ELF_R_SYM (a
->rela
->r_info
) > ELF_R_SYM (b
->rela
->r_info
))
4708 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
4710 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
4716 elf_link_sort_cmp2 (A
, B
)
4720 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4721 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4724 if (a
->offset
< b
->offset
)
4726 if (a
->offset
> b
->offset
)
4728 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
4729 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
4734 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
4736 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
4742 elf_link_sort_relocs (abfd
, info
, psec
)
4744 struct bfd_link_info
*info
;
4747 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4748 asection
*reldyn
, *o
;
4749 bfd_size_type count
, size
;
4750 size_t i
, ret
, sort_elt
, ext_size
;
4751 bfd_byte
*sort
, *s_non_relative
, *p
;
4752 struct elf_link_sort_rela
*sq
;
4753 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4754 int i2e
= bed
->s
->int_rels_per_ext_rel
;
4755 void (*swap_in
) PARAMS ((bfd
*, const bfd_byte
*, Elf_Internal_Rela
*));
4756 void (*swap_out
) PARAMS ((bfd
*, const Elf_Internal_Rela
*, bfd_byte
*));
4758 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
4759 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4761 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
4762 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4764 ext_size
= sizeof (Elf_External_Rel
);
4765 swap_in
= bed
->s
->swap_reloc_in
;
4766 swap_out
= bed
->s
->swap_reloc_out
;
4770 ext_size
= sizeof (Elf_External_Rela
);
4771 swap_in
= bed
->s
->swap_reloca_in
;
4772 swap_out
= bed
->s
->swap_reloca_out
;
4774 count
= reldyn
->_raw_size
/ ext_size
;
4777 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4778 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4779 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4780 && o
->output_section
== reldyn
)
4781 size
+= o
->_raw_size
;
4783 if (size
!= reldyn
->_raw_size
)
4786 sort_elt
= (sizeof (struct elf_link_sort_rela
)
4787 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
4788 sort
= bfd_zmalloc (sort_elt
* count
);
4791 (*info
->callbacks
->warning
)
4792 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0,
4797 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4798 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4799 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4800 && o
->output_section
== reldyn
)
4802 bfd_byte
*erel
, *erelend
;
4805 erelend
= o
->contents
+ o
->_raw_size
;
4806 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
4807 while (erel
< erelend
)
4809 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
4810 (*swap_in
) (abfd
, erel
, s
->rela
);
4811 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
4817 qsort (sort
, (size_t) count
, sort_elt
, elf_link_sort_cmp1
);
4819 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
4821 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
4822 if (s
->type
!= reloc_class_relative
)
4828 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
4829 for (; i
< count
; i
++, p
+= sort_elt
)
4831 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
4832 if (ELF_R_SYM (sp
->rela
->r_info
) != ELF_R_SYM (sq
->rela
->r_info
))
4834 sp
->offset
= sq
->rela
->r_offset
;
4837 qsort (s_non_relative
, (size_t) count
- ret
, sort_elt
, elf_link_sort_cmp2
);
4839 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4840 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4841 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4842 && o
->output_section
== reldyn
)
4844 bfd_byte
*erel
, *erelend
;
4847 erelend
= o
->contents
+ o
->_raw_size
;
4848 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
4849 while (erel
< erelend
)
4851 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
4852 (*swap_out
) (abfd
, s
->rela
, erel
);
4863 /* Do the final step of an ELF link. */
4866 elf_bfd_final_link (abfd
, info
)
4868 struct bfd_link_info
*info
;
4870 bfd_boolean dynamic
;
4871 bfd_boolean emit_relocs
;
4873 struct elf_final_link_info finfo
;
4874 register asection
*o
;
4875 register struct bfd_link_order
*p
;
4877 bfd_size_type max_contents_size
;
4878 bfd_size_type max_external_reloc_size
;
4879 bfd_size_type max_internal_reloc_count
;
4880 bfd_size_type max_sym_count
;
4881 bfd_size_type max_sym_shndx_count
;
4883 Elf_Internal_Sym elfsym
;
4885 Elf_Internal_Shdr
*symtab_hdr
;
4886 Elf_Internal_Shdr
*symtab_shndx_hdr
;
4887 Elf_Internal_Shdr
*symstrtab_hdr
;
4888 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4889 struct elf_outext_info eoinfo
;
4891 size_t relativecount
= 0;
4892 asection
*reldyn
= 0;
4895 if (! is_elf_hash_table (info
))
4899 abfd
->flags
|= DYNAMIC
;
4901 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
4902 dynobj
= elf_hash_table (info
)->dynobj
;
4904 emit_relocs
= (info
->relocateable
4905 || info
->emitrelocations
4906 || bed
->elf_backend_emit_relocs
);
4909 finfo
.output_bfd
= abfd
;
4910 finfo
.symstrtab
= elf_stringtab_init ();
4911 if (finfo
.symstrtab
== NULL
)
4916 finfo
.dynsym_sec
= NULL
;
4917 finfo
.hash_sec
= NULL
;
4918 finfo
.symver_sec
= NULL
;
4922 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
4923 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
4924 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
4925 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
4926 /* Note that it is OK if symver_sec is NULL. */
4929 finfo
.contents
= NULL
;
4930 finfo
.external_relocs
= NULL
;
4931 finfo
.internal_relocs
= NULL
;
4932 finfo
.external_syms
= NULL
;
4933 finfo
.locsym_shndx
= NULL
;
4934 finfo
.internal_syms
= NULL
;
4935 finfo
.indices
= NULL
;
4936 finfo
.sections
= NULL
;
4937 finfo
.symbuf
= NULL
;
4938 finfo
.symshndxbuf
= NULL
;
4939 finfo
.symbuf_count
= 0;
4940 finfo
.shndxbuf_size
= 0;
4941 finfo
.first_tls_sec
= NULL
;
4942 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
4943 if ((o
->flags
& SEC_THREAD_LOCAL
) != 0
4944 && (o
->flags
& SEC_LOAD
) != 0)
4946 finfo
.first_tls_sec
= o
;
4950 /* Count up the number of relocations we will output for each output
4951 section, so that we know the sizes of the reloc sections. We
4952 also figure out some maximum sizes. */
4953 max_contents_size
= 0;
4954 max_external_reloc_size
= 0;
4955 max_internal_reloc_count
= 0;
4957 max_sym_shndx_count
= 0;
4959 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
4961 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
4964 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
4966 unsigned int reloc_count
= 0;
4967 struct bfd_elf_section_data
*esdi
= NULL
;
4968 unsigned int *rel_count1
;
4970 if (p
->type
== bfd_section_reloc_link_order
4971 || p
->type
== bfd_symbol_reloc_link_order
)
4973 else if (p
->type
== bfd_indirect_link_order
)
4977 sec
= p
->u
.indirect
.section
;
4978 esdi
= elf_section_data (sec
);
4980 /* Mark all sections which are to be included in the
4981 link. This will normally be every section. We need
4982 to do this so that we can identify any sections which
4983 the linker has decided to not include. */
4984 sec
->linker_mark
= TRUE
;
4986 if (sec
->flags
& SEC_MERGE
)
4989 if (info
->relocateable
|| info
->emitrelocations
)
4990 reloc_count
= sec
->reloc_count
;
4991 else if (bed
->elf_backend_count_relocs
)
4993 Elf_Internal_Rela
* relocs
;
4995 relocs
= (NAME(_bfd_elf
,link_read_relocs
)
4996 (abfd
, sec
, (PTR
) NULL
,
4997 (Elf_Internal_Rela
*) NULL
, info
->keep_memory
));
4999 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
5001 if (elf_section_data (o
)->relocs
!= relocs
)
5005 if (sec
->_raw_size
> max_contents_size
)
5006 max_contents_size
= sec
->_raw_size
;
5007 if (sec
->_cooked_size
> max_contents_size
)
5008 max_contents_size
= sec
->_cooked_size
;
5010 /* We are interested in just local symbols, not all
5012 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
5013 && (sec
->owner
->flags
& DYNAMIC
) == 0)
5017 if (elf_bad_symtab (sec
->owner
))
5018 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
5019 / sizeof (Elf_External_Sym
));
5021 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
5023 if (sym_count
> max_sym_count
)
5024 max_sym_count
= sym_count
;
5026 if (sym_count
> max_sym_shndx_count
5027 && elf_symtab_shndx (sec
->owner
) != 0)
5028 max_sym_shndx_count
= sym_count
;
5030 if ((sec
->flags
& SEC_RELOC
) != 0)
5034 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
5035 if (ext_size
> max_external_reloc_size
)
5036 max_external_reloc_size
= ext_size
;
5037 if (sec
->reloc_count
> max_internal_reloc_count
)
5038 max_internal_reloc_count
= sec
->reloc_count
;
5043 if (reloc_count
== 0)
5046 o
->reloc_count
+= reloc_count
;
5048 /* MIPS may have a mix of REL and RELA relocs on sections.
5049 To support this curious ABI we keep reloc counts in
5050 elf_section_data too. We must be careful to add the
5051 relocations from the input section to the right output
5052 count. FIXME: Get rid of one count. We have
5053 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
5054 rel_count1
= &esdo
->rel_count
;
5057 bfd_boolean same_size
;
5058 bfd_size_type entsize1
;
5060 entsize1
= esdi
->rel_hdr
.sh_entsize
;
5061 BFD_ASSERT (entsize1
== sizeof (Elf_External_Rel
)
5062 || entsize1
== sizeof (Elf_External_Rela
));
5063 same_size
= (!o
->use_rela_p
5064 == (entsize1
== sizeof (Elf_External_Rel
)));
5067 rel_count1
= &esdo
->rel_count2
;
5069 if (esdi
->rel_hdr2
!= NULL
)
5071 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
5072 unsigned int alt_count
;
5073 unsigned int *rel_count2
;
5075 BFD_ASSERT (entsize2
!= entsize1
5076 && (entsize2
== sizeof (Elf_External_Rel
)
5077 || entsize2
== sizeof (Elf_External_Rela
)));
5079 rel_count2
= &esdo
->rel_count2
;
5081 rel_count2
= &esdo
->rel_count
;
5083 /* The following is probably too simplistic if the
5084 backend counts output relocs unusually. */
5085 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
5086 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
5087 *rel_count2
+= alt_count
;
5088 reloc_count
-= alt_count
;
5091 *rel_count1
+= reloc_count
;
5094 if (o
->reloc_count
> 0)
5095 o
->flags
|= SEC_RELOC
;
5098 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5099 set it (this is probably a bug) and if it is set
5100 assign_section_numbers will create a reloc section. */
5101 o
->flags
&=~ SEC_RELOC
;
5104 /* If the SEC_ALLOC flag is not set, force the section VMA to
5105 zero. This is done in elf_fake_sections as well, but forcing
5106 the VMA to 0 here will ensure that relocs against these
5107 sections are handled correctly. */
5108 if ((o
->flags
& SEC_ALLOC
) == 0
5109 && ! o
->user_set_vma
)
5113 if (! info
->relocateable
&& merged
)
5114 elf_link_hash_traverse (elf_hash_table (info
),
5115 elf_link_sec_merge_syms
, (PTR
) abfd
);
5117 /* Figure out the file positions for everything but the symbol table
5118 and the relocs. We set symcount to force assign_section_numbers
5119 to create a symbol table. */
5120 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
5121 BFD_ASSERT (! abfd
->output_has_begun
);
5122 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
5125 /* That created the reloc sections. Set their sizes, and assign
5126 them file positions, and allocate some buffers. */
5127 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5129 if ((o
->flags
& SEC_RELOC
) != 0)
5131 if (!elf_link_size_reloc_section (abfd
,
5132 &elf_section_data (o
)->rel_hdr
,
5136 if (elf_section_data (o
)->rel_hdr2
5137 && !elf_link_size_reloc_section (abfd
,
5138 elf_section_data (o
)->rel_hdr2
,
5143 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5144 to count upwards while actually outputting the relocations. */
5145 elf_section_data (o
)->rel_count
= 0;
5146 elf_section_data (o
)->rel_count2
= 0;
5149 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5151 /* We have now assigned file positions for all the sections except
5152 .symtab and .strtab. We start the .symtab section at the current
5153 file position, and write directly to it. We build the .strtab
5154 section in memory. */
5155 bfd_get_symcount (abfd
) = 0;
5156 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5157 /* sh_name is set in prep_headers. */
5158 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5159 /* sh_flags, sh_addr and sh_size all start off zero. */
5160 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
5161 /* sh_link is set in assign_section_numbers. */
5162 /* sh_info is set below. */
5163 /* sh_offset is set just below. */
5164 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
5166 off
= elf_tdata (abfd
)->next_file_pos
;
5167 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
5169 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5170 incorrect. We do not yet know the size of the .symtab section.
5171 We correct next_file_pos below, after we do know the size. */
5173 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5174 continuously seeking to the right position in the file. */
5175 if (! info
->keep_memory
|| max_sym_count
< 20)
5176 finfo
.symbuf_size
= 20;
5178 finfo
.symbuf_size
= max_sym_count
;
5179 amt
= finfo
.symbuf_size
;
5180 amt
*= sizeof (Elf_External_Sym
);
5181 finfo
.symbuf
= (Elf_External_Sym
*) bfd_malloc (amt
);
5182 if (finfo
.symbuf
== NULL
)
5184 if (elf_numsections (abfd
) > SHN_LORESERVE
)
5186 /* Wild guess at number of output symbols. realloc'd as needed. */
5187 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
5188 finfo
.shndxbuf_size
= amt
;
5189 amt
*= sizeof (Elf_External_Sym_Shndx
);
5190 finfo
.symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
5191 if (finfo
.symshndxbuf
== NULL
)
5195 /* Start writing out the symbol table. The first symbol is always a
5197 if (info
->strip
!= strip_all
5200 elfsym
.st_value
= 0;
5203 elfsym
.st_other
= 0;
5204 elfsym
.st_shndx
= SHN_UNDEF
;
5205 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5206 &elfsym
, bfd_und_section_ptr
))
5211 /* Some standard ELF linkers do this, but we don't because it causes
5212 bootstrap comparison failures. */
5213 /* Output a file symbol for the output file as the second symbol.
5214 We output this even if we are discarding local symbols, although
5215 I'm not sure if this is correct. */
5216 elfsym
.st_value
= 0;
5218 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5219 elfsym
.st_other
= 0;
5220 elfsym
.st_shndx
= SHN_ABS
;
5221 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
5222 &elfsym
, bfd_abs_section_ptr
))
5226 /* Output a symbol for each section. We output these even if we are
5227 discarding local symbols, since they are used for relocs. These
5228 symbols have no names. We store the index of each one in the
5229 index field of the section, so that we can find it again when
5230 outputting relocs. */
5231 if (info
->strip
!= strip_all
5235 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5236 elfsym
.st_other
= 0;
5237 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5239 o
= section_from_elf_index (abfd
, i
);
5241 o
->target_index
= bfd_get_symcount (abfd
);
5242 elfsym
.st_shndx
= i
;
5243 if (info
->relocateable
|| o
== NULL
)
5244 elfsym
.st_value
= 0;
5246 elfsym
.st_value
= o
->vma
;
5247 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5250 if (i
== SHN_LORESERVE
- 1)
5251 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
5255 /* Allocate some memory to hold information read in from the input
5257 if (max_contents_size
!= 0)
5259 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
5260 if (finfo
.contents
== NULL
)
5264 if (max_external_reloc_size
!= 0)
5266 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
5267 if (finfo
.external_relocs
== NULL
)
5271 if (max_internal_reloc_count
!= 0)
5273 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5274 amt
*= sizeof (Elf_Internal_Rela
);
5275 finfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
5276 if (finfo
.internal_relocs
== NULL
)
5280 if (max_sym_count
!= 0)
5282 amt
= max_sym_count
* sizeof (Elf_External_Sym
);
5283 finfo
.external_syms
= (Elf_External_Sym
*) bfd_malloc (amt
);
5284 if (finfo
.external_syms
== NULL
)
5287 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
5288 finfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
5289 if (finfo
.internal_syms
== NULL
)
5292 amt
= max_sym_count
* sizeof (long);
5293 finfo
.indices
= (long *) bfd_malloc (amt
);
5294 if (finfo
.indices
== NULL
)
5297 amt
= max_sym_count
* sizeof (asection
*);
5298 finfo
.sections
= (asection
**) bfd_malloc (amt
);
5299 if (finfo
.sections
== NULL
)
5303 if (max_sym_shndx_count
!= 0)
5305 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
5306 finfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
5307 if (finfo
.locsym_shndx
== NULL
)
5311 if (finfo
.first_tls_sec
)
5313 unsigned int align
= 0;
5314 bfd_vma base
= finfo
.first_tls_sec
->vma
, end
= 0;
5317 for (sec
= finfo
.first_tls_sec
;
5318 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
5321 bfd_vma size
= sec
->_raw_size
;
5323 if (bfd_get_section_alignment (abfd
, sec
) > align
)
5324 align
= bfd_get_section_alignment (abfd
, sec
);
5325 if (sec
->_raw_size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
5327 struct bfd_link_order
*o
;
5330 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
5331 if (size
< o
->offset
+ o
->size
)
5332 size
= o
->offset
+ o
->size
;
5334 end
= sec
->vma
+ size
;
5336 elf_hash_table (info
)->tls_segment
5337 = bfd_zalloc (abfd
, sizeof (struct elf_link_tls_segment
));
5338 if (elf_hash_table (info
)->tls_segment
== NULL
)
5340 elf_hash_table (info
)->tls_segment
->start
= base
;
5341 elf_hash_table (info
)->tls_segment
->size
= end
- base
;
5342 elf_hash_table (info
)->tls_segment
->align
= align
;
5345 /* Since ELF permits relocations to be against local symbols, we
5346 must have the local symbols available when we do the relocations.
5347 Since we would rather only read the local symbols once, and we
5348 would rather not keep them in memory, we handle all the
5349 relocations for a single input file at the same time.
5351 Unfortunately, there is no way to know the total number of local
5352 symbols until we have seen all of them, and the local symbol
5353 indices precede the global symbol indices. This means that when
5354 we are generating relocateable output, and we see a reloc against
5355 a global symbol, we can not know the symbol index until we have
5356 finished examining all the local symbols to see which ones we are
5357 going to output. To deal with this, we keep the relocations in
5358 memory, and don't output them until the end of the link. This is
5359 an unfortunate waste of memory, but I don't see a good way around
5360 it. Fortunately, it only happens when performing a relocateable
5361 link, which is not the common case. FIXME: If keep_memory is set
5362 we could write the relocs out and then read them again; I don't
5363 know how bad the memory loss will be. */
5365 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5366 sub
->output_has_begun
= FALSE
;
5367 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5369 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5371 if (p
->type
== bfd_indirect_link_order
5372 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
5373 == bfd_target_elf_flavour
)
5374 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
5376 if (! sub
->output_has_begun
)
5378 if (! elf_link_input_bfd (&finfo
, sub
))
5380 sub
->output_has_begun
= TRUE
;
5383 else if (p
->type
== bfd_section_reloc_link_order
5384 || p
->type
== bfd_symbol_reloc_link_order
)
5386 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
5391 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
5397 /* Output any global symbols that got converted to local in a
5398 version script or due to symbol visibility. We do this in a
5399 separate step since ELF requires all local symbols to appear
5400 prior to any global symbols. FIXME: We should only do this if
5401 some global symbols were, in fact, converted to become local.
5402 FIXME: Will this work correctly with the Irix 5 linker? */
5403 eoinfo
.failed
= FALSE
;
5404 eoinfo
.finfo
= &finfo
;
5405 eoinfo
.localsyms
= TRUE
;
5406 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5411 /* That wrote out all the local symbols. Finish up the symbol table
5412 with the global symbols. Even if we want to strip everything we
5413 can, we still need to deal with those global symbols that got
5414 converted to local in a version script. */
5416 /* The sh_info field records the index of the first non local symbol. */
5417 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
5420 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
5422 Elf_Internal_Sym sym
;
5423 Elf_External_Sym
*dynsym
=
5424 (Elf_External_Sym
*) finfo
.dynsym_sec
->contents
;
5425 long last_local
= 0;
5427 /* Write out the section symbols for the output sections. */
5434 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5437 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5440 Elf_External_Sym
*dest
;
5442 indx
= elf_section_data (s
)->this_idx
;
5443 BFD_ASSERT (indx
> 0);
5444 sym
.st_shndx
= indx
;
5445 sym
.st_value
= s
->vma
;
5446 dest
= dynsym
+ elf_section_data (s
)->dynindx
;
5447 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5450 last_local
= bfd_count_sections (abfd
);
5453 /* Write out the local dynsyms. */
5454 if (elf_hash_table (info
)->dynlocal
)
5456 struct elf_link_local_dynamic_entry
*e
;
5457 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
5460 Elf_External_Sym
*dest
;
5462 sym
.st_size
= e
->isym
.st_size
;
5463 sym
.st_other
= e
->isym
.st_other
;
5465 /* Copy the internal symbol as is.
5466 Note that we saved a word of storage and overwrote
5467 the original st_name with the dynstr_index. */
5470 if (e
->isym
.st_shndx
!= SHN_UNDEF
5471 && (e
->isym
.st_shndx
< SHN_LORESERVE
5472 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
5474 s
= bfd_section_from_elf_index (e
->input_bfd
,
5478 elf_section_data (s
->output_section
)->this_idx
;
5479 sym
.st_value
= (s
->output_section
->vma
5481 + e
->isym
.st_value
);
5484 if (last_local
< e
->dynindx
)
5485 last_local
= e
->dynindx
;
5487 dest
= dynsym
+ e
->dynindx
;
5488 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5492 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
5496 /* We get the global symbols from the hash table. */
5497 eoinfo
.failed
= FALSE
;
5498 eoinfo
.localsyms
= FALSE
;
5499 eoinfo
.finfo
= &finfo
;
5500 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5505 /* If backend needs to output some symbols not present in the hash
5506 table, do it now. */
5507 if (bed
->elf_backend_output_arch_syms
)
5509 typedef bfd_boolean (*out_sym_func
)
5510 PARAMS ((PTR
, const char *, Elf_Internal_Sym
*, asection
*));
5512 if (! ((*bed
->elf_backend_output_arch_syms
)
5513 (abfd
, info
, (PTR
) &finfo
, (out_sym_func
) elf_link_output_sym
)))
5517 /* Flush all symbols to the file. */
5518 if (! elf_link_flush_output_syms (&finfo
))
5521 /* Now we know the size of the symtab section. */
5522 off
+= symtab_hdr
->sh_size
;
5524 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
5525 if (symtab_shndx_hdr
->sh_name
!= 0)
5527 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
5528 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
5529 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
5530 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
5531 symtab_shndx_hdr
->sh_size
= amt
;
5533 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
5536 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
5537 || (bfd_bwrite ((PTR
) finfo
.symshndxbuf
, amt
, abfd
) != amt
))
5542 /* Finish up and write out the symbol string table (.strtab)
5544 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5545 /* sh_name was set in prep_headers. */
5546 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5547 symstrtab_hdr
->sh_flags
= 0;
5548 symstrtab_hdr
->sh_addr
= 0;
5549 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
5550 symstrtab_hdr
->sh_entsize
= 0;
5551 symstrtab_hdr
->sh_link
= 0;
5552 symstrtab_hdr
->sh_info
= 0;
5553 /* sh_offset is set just below. */
5554 symstrtab_hdr
->sh_addralign
= 1;
5556 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
5557 elf_tdata (abfd
)->next_file_pos
= off
;
5559 if (bfd_get_symcount (abfd
) > 0)
5561 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
5562 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
5566 /* Adjust the relocs to have the correct symbol indices. */
5567 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5569 if ((o
->flags
& SEC_RELOC
) == 0)
5572 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
5573 elf_section_data (o
)->rel_count
,
5574 elf_section_data (o
)->rel_hashes
);
5575 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
5576 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
5577 elf_section_data (o
)->rel_count2
,
5578 (elf_section_data (o
)->rel_hashes
5579 + elf_section_data (o
)->rel_count
));
5581 /* Set the reloc_count field to 0 to prevent write_relocs from
5582 trying to swap the relocs out itself. */
5586 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
5587 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
5589 /* If we are linking against a dynamic object, or generating a
5590 shared library, finish up the dynamic linking information. */
5593 Elf_External_Dyn
*dyncon
, *dynconend
;
5595 /* Fix up .dynamic entries. */
5596 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
5597 BFD_ASSERT (o
!= NULL
);
5599 dyncon
= (Elf_External_Dyn
*) o
->contents
;
5600 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
5601 for (; dyncon
< dynconend
; dyncon
++)
5603 Elf_Internal_Dyn dyn
;
5607 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
5614 if (relativecount
> 0 && dyncon
+ 1 < dynconend
)
5616 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
5618 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
5619 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
5622 if (dyn
.d_tag
!= DT_NULL
)
5624 dyn
.d_un
.d_val
= relativecount
;
5625 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5631 name
= info
->init_function
;
5634 name
= info
->fini_function
;
5637 struct elf_link_hash_entry
*h
;
5639 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
5640 FALSE
, FALSE
, TRUE
);
5642 && (h
->root
.type
== bfd_link_hash_defined
5643 || h
->root
.type
== bfd_link_hash_defweak
))
5645 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
5646 o
= h
->root
.u
.def
.section
;
5647 if (o
->output_section
!= NULL
)
5648 dyn
.d_un
.d_val
+= (o
->output_section
->vma
5649 + o
->output_offset
);
5652 /* The symbol is imported from another shared
5653 library and does not apply to this one. */
5657 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5662 case DT_PREINIT_ARRAYSZ
:
5663 name
= ".preinit_array";
5665 case DT_INIT_ARRAYSZ
:
5666 name
= ".init_array";
5668 case DT_FINI_ARRAYSZ
:
5669 name
= ".fini_array";
5671 o
= bfd_get_section_by_name (abfd
, name
);
5674 (*_bfd_error_handler
)
5675 (_("%s: could not find output section %s"),
5676 bfd_get_filename (abfd
), name
);
5679 if (o
->_raw_size
== 0)
5680 (*_bfd_error_handler
)
5681 (_("warning: %s section has zero size"), name
);
5682 dyn
.d_un
.d_val
= o
->_raw_size
;
5683 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5686 case DT_PREINIT_ARRAY
:
5687 name
= ".preinit_array";
5690 name
= ".init_array";
5693 name
= ".fini_array";
5706 name
= ".gnu.version_d";
5709 name
= ".gnu.version_r";
5712 name
= ".gnu.version";
5714 o
= bfd_get_section_by_name (abfd
, name
);
5717 (*_bfd_error_handler
)
5718 (_("%s: could not find output section %s"),
5719 bfd_get_filename (abfd
), name
);
5722 dyn
.d_un
.d_ptr
= o
->vma
;
5723 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5730 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
5735 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5737 Elf_Internal_Shdr
*hdr
;
5739 hdr
= elf_elfsections (abfd
)[i
];
5740 if (hdr
->sh_type
== type
5741 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
5743 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
5744 dyn
.d_un
.d_val
+= hdr
->sh_size
;
5747 if (dyn
.d_un
.d_val
== 0
5748 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
5749 dyn
.d_un
.d_val
= hdr
->sh_addr
;
5753 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5759 /* If we have created any dynamic sections, then output them. */
5762 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
5765 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
5767 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5768 || o
->_raw_size
== 0
5769 || o
->output_section
== bfd_abs_section_ptr
)
5771 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
5773 /* At this point, we are only interested in sections
5774 created by elf_link_create_dynamic_sections. */
5777 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
5779 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
5781 if (! bfd_set_section_contents (abfd
, o
->output_section
,
5783 (file_ptr
) o
->output_offset
,
5789 /* The contents of the .dynstr section are actually in a
5791 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
5792 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
5793 || ! _bfd_elf_strtab_emit (abfd
,
5794 elf_hash_table (info
)->dynstr
))
5800 if (info
->relocateable
)
5802 bfd_boolean failed
= FALSE
;
5804 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
5809 /* If we have optimized stabs strings, output them. */
5810 if (elf_hash_table (info
)->stab_info
!= NULL
)
5812 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
5816 if (info
->eh_frame_hdr
)
5818 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
5822 if (finfo
.symstrtab
!= NULL
)
5823 _bfd_stringtab_free (finfo
.symstrtab
);
5824 if (finfo
.contents
!= NULL
)
5825 free (finfo
.contents
);
5826 if (finfo
.external_relocs
!= NULL
)
5827 free (finfo
.external_relocs
);
5828 if (finfo
.internal_relocs
!= NULL
)
5829 free (finfo
.internal_relocs
);
5830 if (finfo
.external_syms
!= NULL
)
5831 free (finfo
.external_syms
);
5832 if (finfo
.locsym_shndx
!= NULL
)
5833 free (finfo
.locsym_shndx
);
5834 if (finfo
.internal_syms
!= NULL
)
5835 free (finfo
.internal_syms
);
5836 if (finfo
.indices
!= NULL
)
5837 free (finfo
.indices
);
5838 if (finfo
.sections
!= NULL
)
5839 free (finfo
.sections
);
5840 if (finfo
.symbuf
!= NULL
)
5841 free (finfo
.symbuf
);
5842 if (finfo
.symshndxbuf
!= NULL
)
5843 free (finfo
.symshndxbuf
);
5844 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5846 if ((o
->flags
& SEC_RELOC
) != 0
5847 && elf_section_data (o
)->rel_hashes
!= NULL
)
5848 free (elf_section_data (o
)->rel_hashes
);
5851 elf_tdata (abfd
)->linker
= TRUE
;
5856 if (finfo
.symstrtab
!= NULL
)
5857 _bfd_stringtab_free (finfo
.symstrtab
);
5858 if (finfo
.contents
!= NULL
)
5859 free (finfo
.contents
);
5860 if (finfo
.external_relocs
!= NULL
)
5861 free (finfo
.external_relocs
);
5862 if (finfo
.internal_relocs
!= NULL
)
5863 free (finfo
.internal_relocs
);
5864 if (finfo
.external_syms
!= NULL
)
5865 free (finfo
.external_syms
);
5866 if (finfo
.locsym_shndx
!= NULL
)
5867 free (finfo
.locsym_shndx
);
5868 if (finfo
.internal_syms
!= NULL
)
5869 free (finfo
.internal_syms
);
5870 if (finfo
.indices
!= NULL
)
5871 free (finfo
.indices
);
5872 if (finfo
.sections
!= NULL
)
5873 free (finfo
.sections
);
5874 if (finfo
.symbuf
!= NULL
)
5875 free (finfo
.symbuf
);
5876 if (finfo
.symshndxbuf
!= NULL
)
5877 free (finfo
.symshndxbuf
);
5878 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5880 if ((o
->flags
& SEC_RELOC
) != 0
5881 && elf_section_data (o
)->rel_hashes
!= NULL
)
5882 free (elf_section_data (o
)->rel_hashes
);
5888 /* Add a symbol to the output symbol table. */
5891 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
5892 struct elf_final_link_info
*finfo
;
5894 Elf_Internal_Sym
*elfsym
;
5895 asection
*input_sec
;
5897 Elf_External_Sym
*dest
;
5898 Elf_External_Sym_Shndx
*destshndx
;
5899 bfd_boolean (*output_symbol_hook
)
5900 PARAMS ((bfd
*, struct bfd_link_info
*info
, const char *,
5901 Elf_Internal_Sym
*, asection
*));
5903 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
5904 elf_backend_link_output_symbol_hook
;
5905 if (output_symbol_hook
!= NULL
)
5907 if (! ((*output_symbol_hook
)
5908 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
5912 if (name
== (const char *) NULL
|| *name
== '\0')
5913 elfsym
->st_name
= 0;
5914 else if (input_sec
->flags
& SEC_EXCLUDE
)
5915 elfsym
->st_name
= 0;
5918 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5920 if (elfsym
->st_name
== (unsigned long) -1)
5924 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5926 if (! elf_link_flush_output_syms (finfo
))
5930 dest
= finfo
->symbuf
+ finfo
->symbuf_count
;
5931 destshndx
= finfo
->symshndxbuf
;
5932 if (destshndx
!= NULL
)
5934 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
5938 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
5939 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
5940 if (destshndx
== NULL
)
5942 memset ((char *) destshndx
+ amt
, 0, amt
);
5943 finfo
->shndxbuf_size
*= 2;
5945 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
5948 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
, (PTR
) dest
, (PTR
) destshndx
);
5949 finfo
->symbuf_count
+= 1;
5950 bfd_get_symcount (finfo
->output_bfd
) += 1;
5955 /* Flush the output symbols to the file. */
5958 elf_link_flush_output_syms (finfo
)
5959 struct elf_final_link_info
*finfo
;
5961 if (finfo
->symbuf_count
> 0)
5963 Elf_Internal_Shdr
*hdr
;
5967 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5968 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5969 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
5970 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5971 || bfd_bwrite ((PTR
) finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5974 hdr
->sh_size
+= amt
;
5975 finfo
->symbuf_count
= 0;
5981 /* Adjust all external symbols pointing into SEC_MERGE sections
5982 to reflect the object merging within the sections. */
5985 elf_link_sec_merge_syms (h
, data
)
5986 struct elf_link_hash_entry
*h
;
5991 if (h
->root
.type
== bfd_link_hash_warning
)
5992 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5994 if ((h
->root
.type
== bfd_link_hash_defined
5995 || h
->root
.type
== bfd_link_hash_defweak
)
5996 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
5997 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
5999 bfd
*output_bfd
= (bfd
*) data
;
6001 h
->root
.u
.def
.value
=
6002 _bfd_merged_section_offset (output_bfd
,
6003 &h
->root
.u
.def
.section
,
6004 elf_section_data (sec
)->sec_info
,
6005 h
->root
.u
.def
.value
, (bfd_vma
) 0);
6011 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6012 allowing an unsatisfied unversioned symbol in the DSO to match a
6013 versioned symbol that would normally require an explicit version. */
6016 elf_link_check_versioned_symbol (info
, h
)
6017 struct bfd_link_info
*info
;
6018 struct elf_link_hash_entry
*h
;
6020 bfd
*undef_bfd
= h
->root
.u
.undef
.abfd
;
6021 struct elf_link_loaded_list
*loaded
;
6023 if ((undef_bfd
->flags
& DYNAMIC
) == 0
6024 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
6025 || elf_dt_soname (h
->root
.u
.undef
.abfd
) == NULL
)
6028 for (loaded
= elf_hash_table (info
)->loaded
;
6030 loaded
= loaded
->next
)
6033 Elf_Internal_Shdr
*hdr
;
6034 bfd_size_type symcount
;
6035 bfd_size_type extsymcount
;
6036 bfd_size_type extsymoff
;
6037 Elf_Internal_Shdr
*versymhdr
;
6038 Elf_Internal_Sym
*isym
;
6039 Elf_Internal_Sym
*isymend
;
6040 Elf_Internal_Sym
*isymbuf
;
6041 Elf_External_Versym
*ever
;
6042 Elf_External_Versym
*extversym
;
6044 input
= loaded
->abfd
;
6046 /* We check each DSO for a possible hidden versioned definition. */
6047 if (input
== undef_bfd
6048 || (input
->flags
& DYNAMIC
) == 0
6049 || elf_dynversym (input
) == 0)
6052 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6054 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6055 if (elf_bad_symtab (input
))
6057 extsymcount
= symcount
;
6062 extsymcount
= symcount
- hdr
->sh_info
;
6063 extsymoff
= hdr
->sh_info
;
6066 if (extsymcount
== 0)
6069 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6071 if (isymbuf
== NULL
)
6074 /* Read in any version definitions. */
6075 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6076 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
6077 if (extversym
== NULL
)
6080 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6081 || (bfd_bread ((PTR
) extversym
, versymhdr
->sh_size
, input
)
6082 != versymhdr
->sh_size
))
6090 ever
= extversym
+ extsymoff
;
6091 isymend
= isymbuf
+ extsymcount
;
6092 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6095 Elf_Internal_Versym iver
;
6097 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6098 || isym
->st_shndx
== SHN_UNDEF
)
6101 name
= bfd_elf_string_from_elf_section (input
,
6104 if (strcmp (name
, h
->root
.root
.string
) != 0)
6107 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6109 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6111 /* If we have a non-hidden versioned sym, then it should
6112 have provided a definition for the undefined sym. */
6116 if ((iver
.vs_vers
& VERSYM_VERSION
) == 2)
6118 /* This is the oldest (default) sym. We can use it. */
6132 /* Add an external symbol to the symbol table. This is called from
6133 the hash table traversal routine. When generating a shared object,
6134 we go through the symbol table twice. The first time we output
6135 anything that might have been forced to local scope in a version
6136 script. The second time we output the symbols that are still
6140 elf_link_output_extsym (h
, data
)
6141 struct elf_link_hash_entry
*h
;
6144 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
6145 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6147 Elf_Internal_Sym sym
;
6148 asection
*input_sec
;
6150 if (h
->root
.type
== bfd_link_hash_warning
)
6152 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6153 if (h
->root
.type
== bfd_link_hash_new
)
6157 /* Decide whether to output this symbol in this pass. */
6158 if (eoinfo
->localsyms
)
6160 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6165 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6169 /* If we are not creating a shared library, and this symbol is
6170 referenced by a shared library but is not defined anywhere, then
6171 warn that it is undefined. If we do not do this, the runtime
6172 linker will complain that the symbol is undefined when the
6173 program is run. We don't have to worry about symbols that are
6174 referenced by regular files, because we will already have issued
6175 warnings for them. */
6176 if (! finfo
->info
->relocateable
6177 && (! finfo
->info
->shared
|| ! finfo
->info
->allow_shlib_undefined
)
6178 && h
->root
.type
== bfd_link_hash_undefined
6179 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
6180 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
6181 && ! elf_link_check_versioned_symbol (finfo
->info
, h
))
6183 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6184 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6185 (asection
*) NULL
, (bfd_vma
) 0, TRUE
)))
6187 eoinfo
->failed
= TRUE
;
6192 /* We don't want to output symbols that have never been mentioned by
6193 a regular file, or that we have been told to strip. However, if
6194 h->indx is set to -2, the symbol is used by a reloc and we must
6198 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
6199 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
6200 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
6201 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
6203 else if (finfo
->info
->strip
== strip_all
)
6205 else if (finfo
->info
->strip
== strip_some
6206 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6207 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6209 else if (finfo
->info
->strip_discarded
6210 && (h
->root
.type
== bfd_link_hash_defined
6211 || h
->root
.type
== bfd_link_hash_defweak
)
6212 && elf_discarded_section (h
->root
.u
.def
.section
))
6217 /* If we're stripping it, and it's not a dynamic symbol, there's
6218 nothing else to do unless it is a forced local symbol. */
6221 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6225 sym
.st_size
= h
->size
;
6226 sym
.st_other
= h
->other
;
6227 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6228 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6229 else if (h
->root
.type
== bfd_link_hash_undefweak
6230 || h
->root
.type
== bfd_link_hash_defweak
)
6231 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6233 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6235 switch (h
->root
.type
)
6238 case bfd_link_hash_new
:
6239 case bfd_link_hash_warning
:
6243 case bfd_link_hash_undefined
:
6244 case bfd_link_hash_undefweak
:
6245 input_sec
= bfd_und_section_ptr
;
6246 sym
.st_shndx
= SHN_UNDEF
;
6249 case bfd_link_hash_defined
:
6250 case bfd_link_hash_defweak
:
6252 input_sec
= h
->root
.u
.def
.section
;
6253 if (input_sec
->output_section
!= NULL
)
6256 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6257 input_sec
->output_section
);
6258 if (sym
.st_shndx
== SHN_BAD
)
6260 (*_bfd_error_handler
)
6261 (_("%s: could not find output section %s for input section %s"),
6262 bfd_get_filename (finfo
->output_bfd
),
6263 input_sec
->output_section
->name
,
6265 eoinfo
->failed
= TRUE
;
6269 /* ELF symbols in relocateable files are section relative,
6270 but in nonrelocateable files they are virtual
6272 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6273 if (! finfo
->info
->relocateable
)
6275 sym
.st_value
+= input_sec
->output_section
->vma
;
6276 if (h
->type
== STT_TLS
)
6278 /* STT_TLS symbols are relative to PT_TLS segment
6280 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6281 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
6287 BFD_ASSERT (input_sec
->owner
== NULL
6288 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6289 sym
.st_shndx
= SHN_UNDEF
;
6290 input_sec
= bfd_und_section_ptr
;
6295 case bfd_link_hash_common
:
6296 input_sec
= h
->root
.u
.c
.p
->section
;
6297 sym
.st_shndx
= SHN_COMMON
;
6298 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6301 case bfd_link_hash_indirect
:
6302 /* These symbols are created by symbol versioning. They point
6303 to the decorated version of the name. For example, if the
6304 symbol foo@@GNU_1.2 is the default, which should be used when
6305 foo is used with no version, then we add an indirect symbol
6306 foo which points to foo@@GNU_1.2. We ignore these symbols,
6307 since the indirected symbol is already in the hash table. */
6311 /* Give the processor backend a chance to tweak the symbol value,
6312 and also to finish up anything that needs to be done for this
6313 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6314 forced local syms when non-shared is due to a historical quirk. */
6315 if ((h
->dynindx
!= -1
6316 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6317 && (finfo
->info
->shared
6318 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6319 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6321 struct elf_backend_data
*bed
;
6323 bed
= get_elf_backend_data (finfo
->output_bfd
);
6324 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6325 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6327 eoinfo
->failed
= TRUE
;
6332 /* If we are marking the symbol as undefined, and there are no
6333 non-weak references to this symbol from a regular object, then
6334 mark the symbol as weak undefined; if there are non-weak
6335 references, mark the symbol as strong. We can't do this earlier,
6336 because it might not be marked as undefined until the
6337 finish_dynamic_symbol routine gets through with it. */
6338 if (sym
.st_shndx
== SHN_UNDEF
6339 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
6340 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6341 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6345 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
6346 bindtype
= STB_GLOBAL
;
6348 bindtype
= STB_WEAK
;
6349 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6352 /* If a symbol is not defined locally, we clear the visibility field. */
6353 if (! finfo
->info
->relocateable
6354 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6355 sym
.st_other
&= ~ ELF_ST_VISIBILITY (-1);
6357 /* If this symbol should be put in the .dynsym section, then put it
6358 there now. We already know the symbol index. We also fill in
6359 the entry in the .hash section. */
6360 if (h
->dynindx
!= -1
6361 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6365 size_t hash_entry_size
;
6366 bfd_byte
*bucketpos
;
6368 Elf_External_Sym
*esym
;
6370 sym
.st_name
= h
->dynstr_index
;
6371 esym
= (Elf_External_Sym
*) finfo
->dynsym_sec
->contents
+ h
->dynindx
;
6372 elf_swap_symbol_out (finfo
->output_bfd
, &sym
, (PTR
) esym
, (PTR
) 0);
6374 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6375 bucket
= h
->elf_hash_value
% bucketcount
;
6377 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6378 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6379 + (bucket
+ 2) * hash_entry_size
);
6380 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6381 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, (bfd_vma
) h
->dynindx
,
6383 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6384 ((bfd_byte
*) finfo
->hash_sec
->contents
6385 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6387 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6389 Elf_Internal_Versym iversym
;
6390 Elf_External_Versym
*eversym
;
6392 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6394 if (h
->verinfo
.verdef
== NULL
)
6395 iversym
.vs_vers
= 0;
6397 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6401 if (h
->verinfo
.vertree
== NULL
)
6402 iversym
.vs_vers
= 1;
6404 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6407 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
6408 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6410 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6411 eversym
+= h
->dynindx
;
6412 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6416 /* If we're stripping it, then it was just a dynamic symbol, and
6417 there's nothing else to do. */
6418 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6421 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6423 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
6425 eoinfo
->failed
= TRUE
;
6432 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6433 originated from the section given by INPUT_REL_HDR) to the
6437 elf_link_output_relocs (output_bfd
, input_section
, input_rel_hdr
,
6440 asection
*input_section
;
6441 Elf_Internal_Shdr
*input_rel_hdr
;
6442 Elf_Internal_Rela
*internal_relocs
;
6444 Elf_Internal_Rela
*irela
;
6445 Elf_Internal_Rela
*irelaend
;
6447 Elf_Internal_Shdr
*output_rel_hdr
;
6448 asection
*output_section
;
6449 unsigned int *rel_countp
= NULL
;
6450 struct elf_backend_data
*bed
;
6451 void (*swap_out
) PARAMS ((bfd
*, const Elf_Internal_Rela
*, bfd_byte
*));
6453 output_section
= input_section
->output_section
;
6454 output_rel_hdr
= NULL
;
6456 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
6457 == input_rel_hdr
->sh_entsize
)
6459 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
6460 rel_countp
= &elf_section_data (output_section
)->rel_count
;
6462 else if (elf_section_data (output_section
)->rel_hdr2
6463 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
6464 == input_rel_hdr
->sh_entsize
))
6466 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
6467 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
6471 (*_bfd_error_handler
)
6472 (_("%s: relocation size mismatch in %s section %s"),
6473 bfd_get_filename (output_bfd
),
6474 bfd_archive_filename (input_section
->owner
),
6475 input_section
->name
);
6476 bfd_set_error (bfd_error_wrong_object_format
);
6480 bed
= get_elf_backend_data (output_bfd
);
6481 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
6482 swap_out
= bed
->s
->swap_reloc_out
;
6483 else if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
))
6484 swap_out
= bed
->s
->swap_reloca_out
;
6488 erel
= output_rel_hdr
->contents
;
6489 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
6490 irela
= internal_relocs
;
6491 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
6492 * bed
->s
->int_rels_per_ext_rel
);
6493 while (irela
< irelaend
)
6495 (*swap_out
) (output_bfd
, irela
, erel
);
6496 irela
+= bed
->s
->int_rels_per_ext_rel
;
6497 erel
+= input_rel_hdr
->sh_entsize
;
6500 /* Bump the counter, so that we know where to add the next set of
6502 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
6507 /* Link an input file into the linker output file. This function
6508 handles all the sections and relocations of the input file at once.
6509 This is so that we only have to read the local symbols once, and
6510 don't have to keep them in memory. */
6513 elf_link_input_bfd (finfo
, input_bfd
)
6514 struct elf_final_link_info
*finfo
;
6517 bfd_boolean (*relocate_section
)
6518 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6519 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
6521 Elf_Internal_Shdr
*symtab_hdr
;
6524 Elf_Internal_Sym
*isymbuf
;
6525 Elf_Internal_Sym
*isym
;
6526 Elf_Internal_Sym
*isymend
;
6528 asection
**ppsection
;
6530 struct elf_backend_data
*bed
;
6531 bfd_boolean emit_relocs
;
6532 struct elf_link_hash_entry
**sym_hashes
;
6534 output_bfd
= finfo
->output_bfd
;
6535 bed
= get_elf_backend_data (output_bfd
);
6536 relocate_section
= bed
->elf_backend_relocate_section
;
6538 /* If this is a dynamic object, we don't want to do anything here:
6539 we don't want the local symbols, and we don't want the section
6541 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6544 emit_relocs
= (finfo
->info
->relocateable
6545 || finfo
->info
->emitrelocations
6546 || bed
->elf_backend_emit_relocs
);
6548 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6549 if (elf_bad_symtab (input_bfd
))
6551 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6556 locsymcount
= symtab_hdr
->sh_info
;
6557 extsymoff
= symtab_hdr
->sh_info
;
6560 /* Read the local symbols. */
6561 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6562 if (isymbuf
== NULL
&& locsymcount
!= 0)
6564 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6565 finfo
->internal_syms
,
6566 finfo
->external_syms
,
6567 finfo
->locsym_shndx
);
6568 if (isymbuf
== NULL
)
6572 /* Find local symbol sections and adjust values of symbols in
6573 SEC_MERGE sections. Write out those local symbols we know are
6574 going into the output file. */
6575 isymend
= isymbuf
+ locsymcount
;
6576 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6578 isym
++, pindex
++, ppsection
++)
6582 Elf_Internal_Sym osym
;
6586 if (elf_bad_symtab (input_bfd
))
6588 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6595 if (isym
->st_shndx
== SHN_UNDEF
)
6596 isec
= bfd_und_section_ptr
;
6597 else if (isym
->st_shndx
< SHN_LORESERVE
6598 || isym
->st_shndx
> SHN_HIRESERVE
)
6600 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
6602 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6603 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6605 _bfd_merged_section_offset (output_bfd
, &isec
,
6606 elf_section_data (isec
)->sec_info
,
6607 isym
->st_value
, (bfd_vma
) 0);
6609 else if (isym
->st_shndx
== SHN_ABS
)
6610 isec
= bfd_abs_section_ptr
;
6611 else if (isym
->st_shndx
== SHN_COMMON
)
6612 isec
= bfd_com_section_ptr
;
6621 /* Don't output the first, undefined, symbol. */
6622 if (ppsection
== finfo
->sections
)
6625 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6627 /* We never output section symbols. Instead, we use the
6628 section symbol of the corresponding section in the output
6633 /* If we are stripping all symbols, we don't want to output this
6635 if (finfo
->info
->strip
== strip_all
)
6638 /* If we are discarding all local symbols, we don't want to
6639 output this one. If we are generating a relocateable output
6640 file, then some of the local symbols may be required by
6641 relocs; we output them below as we discover that they are
6643 if (finfo
->info
->discard
== discard_all
)
6646 /* If this symbol is defined in a section which we are
6647 discarding, we don't need to keep it, but note that
6648 linker_mark is only reliable for sections that have contents.
6649 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6650 as well as linker_mark. */
6651 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6653 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6654 || (! finfo
->info
->relocateable
6655 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6658 /* Get the name of the symbol. */
6659 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6664 /* See if we are discarding symbols with this name. */
6665 if ((finfo
->info
->strip
== strip_some
6666 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6668 || (((finfo
->info
->discard
== discard_sec_merge
6669 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocateable
)
6670 || finfo
->info
->discard
== discard_l
)
6671 && bfd_is_local_label_name (input_bfd
, name
)))
6674 /* If we get here, we are going to output this symbol. */
6678 /* Adjust the section index for the output file. */
6679 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6680 isec
->output_section
);
6681 if (osym
.st_shndx
== SHN_BAD
)
6684 *pindex
= bfd_get_symcount (output_bfd
);
6686 /* ELF symbols in relocateable files are section relative, but
6687 in executable files they are virtual addresses. Note that
6688 this code assumes that all ELF sections have an associated
6689 BFD section with a reasonable value for output_offset; below
6690 we assume that they also have a reasonable value for
6691 output_section. Any special sections must be set up to meet
6692 these requirements. */
6693 osym
.st_value
+= isec
->output_offset
;
6694 if (! finfo
->info
->relocateable
)
6696 osym
.st_value
+= isec
->output_section
->vma
;
6697 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6699 /* STT_TLS symbols are relative to PT_TLS segment base. */
6700 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6701 osym
.st_value
-= finfo
->first_tls_sec
->vma
;
6705 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
6709 /* Relocate the contents of each section. */
6710 sym_hashes
= elf_sym_hashes (input_bfd
);
6711 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6715 if (! o
->linker_mark
)
6717 /* This section was omitted from the link. */
6721 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6722 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6725 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6727 /* Section was created by elf_link_create_dynamic_sections
6732 /* Get the contents of the section. They have been cached by a
6733 relaxation routine. Note that o is a section in an input
6734 file, so the contents field will not have been set by any of
6735 the routines which work on output files. */
6736 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6737 contents
= elf_section_data (o
)->this_hdr
.contents
;
6740 contents
= finfo
->contents
;
6741 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
6742 (file_ptr
) 0, o
->_raw_size
))
6746 if ((o
->flags
& SEC_RELOC
) != 0)
6748 Elf_Internal_Rela
*internal_relocs
;
6750 /* Get the swapped relocs. */
6751 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
6752 (input_bfd
, o
, finfo
->external_relocs
,
6753 finfo
->internal_relocs
, FALSE
));
6754 if (internal_relocs
== NULL
6755 && o
->reloc_count
> 0)
6758 /* Run through the relocs looking for any against symbols
6759 from discarded sections and section symbols from
6760 removed link-once sections. Complain about relocs
6761 against discarded sections. Zero relocs against removed
6762 link-once sections. */
6763 if (!finfo
->info
->relocateable
6764 && !elf_section_ignore_discarded_relocs (o
))
6766 Elf_Internal_Rela
*rel
, *relend
;
6768 rel
= internal_relocs
;
6769 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6770 for ( ; rel
< relend
; rel
++)
6772 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
6774 if (r_symndx
>= locsymcount
6775 || (elf_bad_symtab (input_bfd
)
6776 && finfo
->sections
[r_symndx
] == NULL
))
6778 struct elf_link_hash_entry
*h
;
6780 h
= sym_hashes
[r_symndx
- extsymoff
];
6781 while (h
->root
.type
== bfd_link_hash_indirect
6782 || h
->root
.type
== bfd_link_hash_warning
)
6783 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6785 /* Complain if the definition comes from a
6786 discarded section. */
6787 if ((h
->root
.type
== bfd_link_hash_defined
6788 || h
->root
.type
== bfd_link_hash_defweak
)
6789 && elf_discarded_section (h
->root
.u
.def
.section
))
6791 if ((o
->flags
& SEC_DEBUGGING
) != 0)
6793 BFD_ASSERT (r_symndx
!= 0);
6794 memset (rel
, 0, sizeof (*rel
));
6798 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6799 (finfo
->info
, h
->root
.root
.string
,
6800 input_bfd
, o
, rel
->r_offset
,
6808 asection
*sec
= finfo
->sections
[r_symndx
];
6810 if (sec
!= NULL
&& elf_discarded_section (sec
))
6812 if ((o
->flags
& SEC_DEBUGGING
) != 0
6813 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
6815 BFD_ASSERT (r_symndx
!= 0);
6817 = ELF_R_INFO (0, ELF_R_TYPE (rel
->r_info
));
6824 = _("local symbols in discarded section %s");
6826 = strlen (sec
->name
) + strlen (msg
) - 1;
6827 char *buf
= (char *) bfd_malloc (amt
);
6830 sprintf (buf
, msg
, sec
->name
);
6832 buf
= (char *) sec
->name
;
6833 ok
= (*finfo
->info
->callbacks
6834 ->undefined_symbol
) (finfo
->info
, buf
,
6838 if (buf
!= sec
->name
)
6848 /* Relocate the section by invoking a back end routine.
6850 The back end routine is responsible for adjusting the
6851 section contents as necessary, and (if using Rela relocs
6852 and generating a relocateable output file) adjusting the
6853 reloc addend as necessary.
6855 The back end routine does not have to worry about setting
6856 the reloc address or the reloc symbol index.
6858 The back end routine is given a pointer to the swapped in
6859 internal symbols, and can access the hash table entries
6860 for the external symbols via elf_sym_hashes (input_bfd).
6862 When generating relocateable output, the back end routine
6863 must handle STB_LOCAL/STT_SECTION symbols specially. The
6864 output symbol is going to be a section symbol
6865 corresponding to the output section, which will require
6866 the addend to be adjusted. */
6868 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6869 input_bfd
, o
, contents
,
6877 Elf_Internal_Rela
*irela
;
6878 Elf_Internal_Rela
*irelaend
;
6879 bfd_vma last_offset
;
6880 struct elf_link_hash_entry
**rel_hash
;
6881 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6882 unsigned int next_erel
;
6883 bfd_boolean (*reloc_emitter
)
6884 PARAMS ((bfd
*, asection
*, Elf_Internal_Shdr
*,
6885 Elf_Internal_Rela
*));
6886 bfd_boolean rela_normal
;
6888 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6889 rela_normal
= (bed
->rela_normal
6890 && (input_rel_hdr
->sh_entsize
6891 == sizeof (Elf_External_Rela
)));
6893 /* Adjust the reloc addresses and symbol indices. */
6895 irela
= internal_relocs
;
6896 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6897 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6898 + elf_section_data (o
->output_section
)->rel_count
6899 + elf_section_data (o
->output_section
)->rel_count2
);
6900 last_offset
= o
->output_offset
;
6901 if (!finfo
->info
->relocateable
)
6902 last_offset
+= o
->output_section
->vma
;
6903 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6905 unsigned long r_symndx
;
6907 Elf_Internal_Sym sym
;
6909 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6915 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
6918 if (irela
->r_offset
>= (bfd_vma
) -2)
6920 /* This is a reloc for a deleted entry or somesuch.
6921 Turn it into an R_*_NONE reloc, at the same
6922 offset as the last reloc. elf_eh_frame.c and
6923 elf_bfd_discard_info rely on reloc offsets
6925 irela
->r_offset
= last_offset
;
6927 irela
->r_addend
= 0;
6931 irela
->r_offset
+= o
->output_offset
;
6933 /* Relocs in an executable have to be virtual addresses. */
6934 if (!finfo
->info
->relocateable
)
6935 irela
->r_offset
+= o
->output_section
->vma
;
6937 last_offset
= irela
->r_offset
;
6939 r_symndx
= ELF_R_SYM (irela
->r_info
);
6940 if (r_symndx
== STN_UNDEF
)
6943 if (r_symndx
>= locsymcount
6944 || (elf_bad_symtab (input_bfd
)
6945 && finfo
->sections
[r_symndx
] == NULL
))
6947 struct elf_link_hash_entry
*rh
;
6950 /* This is a reloc against a global symbol. We
6951 have not yet output all the local symbols, so
6952 we do not know the symbol index of any global
6953 symbol. We set the rel_hash entry for this
6954 reloc to point to the global hash table entry
6955 for this symbol. The symbol index is then
6956 set at the end of elf_bfd_final_link. */
6957 indx
= r_symndx
- extsymoff
;
6958 rh
= elf_sym_hashes (input_bfd
)[indx
];
6959 while (rh
->root
.type
== bfd_link_hash_indirect
6960 || rh
->root
.type
== bfd_link_hash_warning
)
6961 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6963 /* Setting the index to -2 tells
6964 elf_link_output_extsym that this symbol is
6966 BFD_ASSERT (rh
->indx
< 0);
6974 /* This is a reloc against a local symbol. */
6977 sym
= isymbuf
[r_symndx
];
6978 sec
= finfo
->sections
[r_symndx
];
6979 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
6981 /* I suppose the backend ought to fill in the
6982 section of any STT_SECTION symbol against a
6983 processor specific section. If we have
6984 discarded a section, the output_section will
6985 be the absolute section. */
6986 if (bfd_is_abs_section (sec
)
6988 && bfd_is_abs_section (sec
->output_section
)))
6990 else if (sec
== NULL
|| sec
->owner
== NULL
)
6992 bfd_set_error (bfd_error_bad_value
);
6997 r_symndx
= sec
->output_section
->target_index
;
6998 BFD_ASSERT (r_symndx
!= 0);
7001 /* Adjust the addend according to where the
7002 section winds up in the output section. */
7004 irela
->r_addend
+= sec
->output_offset
;
7008 if (finfo
->indices
[r_symndx
] == -1)
7010 unsigned long shlink
;
7014 if (finfo
->info
->strip
== strip_all
)
7016 /* You can't do ld -r -s. */
7017 bfd_set_error (bfd_error_invalid_operation
);
7021 /* This symbol was skipped earlier, but
7022 since it is needed by a reloc, we
7023 must output it now. */
7024 shlink
= symtab_hdr
->sh_link
;
7025 name
= (bfd_elf_string_from_elf_section
7026 (input_bfd
, shlink
, sym
.st_name
));
7030 osec
= sec
->output_section
;
7032 _bfd_elf_section_from_bfd_section (output_bfd
,
7034 if (sym
.st_shndx
== SHN_BAD
)
7037 sym
.st_value
+= sec
->output_offset
;
7038 if (! finfo
->info
->relocateable
)
7040 sym
.st_value
+= osec
->vma
;
7041 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7043 /* STT_TLS symbols are relative to PT_TLS
7045 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
7046 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
7050 finfo
->indices
[r_symndx
]
7051 = bfd_get_symcount (output_bfd
);
7053 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
))
7057 r_symndx
= finfo
->indices
[r_symndx
];
7060 irela
->r_info
= ELF_R_INFO (r_symndx
,
7061 ELF_R_TYPE (irela
->r_info
));
7064 /* Swap out the relocs. */
7065 if (bed
->elf_backend_emit_relocs
7066 && !(finfo
->info
->relocateable
7067 || finfo
->info
->emitrelocations
))
7068 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7070 reloc_emitter
= elf_link_output_relocs
;
7072 if (input_rel_hdr
->sh_size
!= 0
7073 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7077 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7078 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7080 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7081 * bed
->s
->int_rels_per_ext_rel
);
7082 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
7089 /* Write out the modified section contents. */
7090 if (bed
->elf_backend_write_section
7091 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7093 /* Section written out. */
7095 else switch (o
->sec_info_type
)
7097 case ELF_INFO_TYPE_STABS
:
7098 if (! (_bfd_write_section_stabs
7100 &elf_hash_table (finfo
->info
)->stab_info
,
7101 o
, &elf_section_data (o
)->sec_info
, contents
)))
7104 case ELF_INFO_TYPE_MERGE
:
7105 if (! _bfd_write_merged_section (output_bfd
, o
,
7106 elf_section_data (o
)->sec_info
))
7109 case ELF_INFO_TYPE_EH_FRAME
:
7111 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7118 bfd_size_type sec_size
;
7120 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
7121 if (! (o
->flags
& SEC_EXCLUDE
)
7122 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7124 (file_ptr
) o
->output_offset
,
7135 /* Generate a reloc when linking an ELF file. This is a reloc
7136 requested by the linker, and does come from any input file. This
7137 is used to build constructor and destructor tables when linking
7141 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
7143 struct bfd_link_info
*info
;
7144 asection
*output_section
;
7145 struct bfd_link_order
*link_order
;
7147 reloc_howto_type
*howto
;
7151 struct elf_link_hash_entry
**rel_hash_ptr
;
7152 Elf_Internal_Shdr
*rel_hdr
;
7153 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7154 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7158 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7161 bfd_set_error (bfd_error_bad_value
);
7165 addend
= link_order
->u
.reloc
.p
->addend
;
7167 /* Figure out the symbol index. */
7168 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7169 + elf_section_data (output_section
)->rel_count
7170 + elf_section_data (output_section
)->rel_count2
);
7171 if (link_order
->type
== bfd_section_reloc_link_order
)
7173 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7174 BFD_ASSERT (indx
!= 0);
7175 *rel_hash_ptr
= NULL
;
7179 struct elf_link_hash_entry
*h
;
7181 /* Treat a reloc against a defined symbol as though it were
7182 actually against the section. */
7183 h
= ((struct elf_link_hash_entry
*)
7184 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7185 link_order
->u
.reloc
.p
->u
.name
,
7186 FALSE
, FALSE
, TRUE
));
7188 && (h
->root
.type
== bfd_link_hash_defined
7189 || h
->root
.type
== bfd_link_hash_defweak
))
7193 section
= h
->root
.u
.def
.section
;
7194 indx
= section
->output_section
->target_index
;
7195 *rel_hash_ptr
= NULL
;
7196 /* It seems that we ought to add the symbol value to the
7197 addend here, but in practice it has already been added
7198 because it was passed to constructor_callback. */
7199 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7203 /* Setting the index to -2 tells elf_link_output_extsym that
7204 this symbol is used by a reloc. */
7211 if (! ((*info
->callbacks
->unattached_reloc
)
7212 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
7213 (asection
*) NULL
, (bfd_vma
) 0)))
7219 /* If this is an inplace reloc, we must write the addend into the
7221 if (howto
->partial_inplace
&& addend
!= 0)
7224 bfd_reloc_status_type rstat
;
7227 const char *sym_name
;
7229 size
= bfd_get_reloc_size (howto
);
7230 buf
= (bfd_byte
*) bfd_zmalloc (size
);
7231 if (buf
== (bfd_byte
*) NULL
)
7233 rstat
= _bfd_relocate_contents (howto
, output_bfd
, (bfd_vma
) addend
, buf
);
7240 case bfd_reloc_outofrange
:
7243 case bfd_reloc_overflow
:
7244 if (link_order
->type
== bfd_section_reloc_link_order
)
7245 sym_name
= bfd_section_name (output_bfd
,
7246 link_order
->u
.reloc
.p
->u
.section
);
7248 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7249 if (! ((*info
->callbacks
->reloc_overflow
)
7250 (info
, sym_name
, howto
->name
, addend
,
7251 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
7258 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
7259 (file_ptr
) link_order
->offset
, size
);
7265 /* The address of a reloc is relative to the section in a
7266 relocateable file, and is a virtual address in an executable
7268 offset
= link_order
->offset
;
7269 if (! info
->relocateable
)
7270 offset
+= output_section
->vma
;
7272 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7274 irel
[i
].r_offset
= offset
;
7276 irel
[i
].r_addend
= 0;
7278 irel
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
7280 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7281 erel
= rel_hdr
->contents
;
7282 if (rel_hdr
->sh_type
== SHT_REL
)
7284 erel
+= (elf_section_data (output_section
)->rel_count
7285 * sizeof (Elf_External_Rel
));
7286 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7290 irel
[0].r_addend
= addend
;
7291 erel
+= (elf_section_data (output_section
)->rel_count
7292 * sizeof (Elf_External_Rela
));
7293 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7296 ++elf_section_data (output_section
)->rel_count
;
7301 /* Allocate a pointer to live in a linker created section. */
7304 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
7306 struct bfd_link_info
*info
;
7307 elf_linker_section_t
*lsect
;
7308 struct elf_link_hash_entry
*h
;
7309 const Elf_Internal_Rela
*rel
;
7311 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
7312 elf_linker_section_pointers_t
*linker_section_ptr
;
7313 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7316 BFD_ASSERT (lsect
!= NULL
);
7318 /* Is this a global symbol? */
7321 /* Has this symbol already been allocated? If so, our work is done. */
7322 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
7327 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
7328 /* Make sure this symbol is output as a dynamic symbol. */
7329 if (h
->dynindx
== -1)
7331 if (! elf_link_record_dynamic_symbol (info
, h
))
7335 if (lsect
->rel_section
)
7336 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7340 /* Allocation of a pointer to a local symbol. */
7341 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
7343 /* Allocate a table to hold the local symbols if first time. */
7346 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
7347 register unsigned int i
;
7350 amt
*= sizeof (elf_linker_section_pointers_t
*);
7351 ptr
= (elf_linker_section_pointers_t
**) bfd_alloc (abfd
, amt
);
7356 elf_local_ptr_offsets (abfd
) = ptr
;
7357 for (i
= 0; i
< num_symbols
; i
++)
7358 ptr
[i
] = (elf_linker_section_pointers_t
*) 0;
7361 /* Has this symbol already been allocated? If so, our work is done. */
7362 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
7367 ptr_linker_section_ptr
= &ptr
[r_symndx
];
7371 /* If we are generating a shared object, we need to
7372 output a R_<xxx>_RELATIVE reloc so that the
7373 dynamic linker can adjust this GOT entry. */
7374 BFD_ASSERT (lsect
->rel_section
!= NULL
);
7375 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7379 /* Allocate space for a pointer in the linker section, and allocate
7380 a new pointer record from internal memory. */
7381 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
7382 amt
= sizeof (elf_linker_section_pointers_t
);
7383 linker_section_ptr
= (elf_linker_section_pointers_t
*) bfd_alloc (abfd
, amt
);
7385 if (!linker_section_ptr
)
7388 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
7389 linker_section_ptr
->addend
= rel
->r_addend
;
7390 linker_section_ptr
->which
= lsect
->which
;
7391 linker_section_ptr
->written_address_p
= FALSE
;
7392 *ptr_linker_section_ptr
= linker_section_ptr
;
7395 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
7397 linker_section_ptr
->offset
= (lsect
->section
->_raw_size
7398 - lsect
->hole_size
+ (ARCH_SIZE
/ 8));
7399 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
7400 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
7401 if (lsect
->sym_hash
)
7403 /* Bump up symbol value if needed. */
7404 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
7406 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
7407 lsect
->sym_hash
->root
.root
.string
,
7408 (long) ARCH_SIZE
/ 8,
7409 (long) lsect
->sym_hash
->root
.u
.def
.value
);
7415 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
7417 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
7421 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7422 lsect
->name
, (long) linker_section_ptr
->offset
,
7423 (long) lsect
->section
->_raw_size
);
7430 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7433 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7436 /* Fill in the address for a pointer generated in a linker section. */
7439 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
,
7440 relocation
, rel
, relative_reloc
)
7443 struct bfd_link_info
*info
;
7444 elf_linker_section_t
*lsect
;
7445 struct elf_link_hash_entry
*h
;
7447 const Elf_Internal_Rela
*rel
;
7450 elf_linker_section_pointers_t
*linker_section_ptr
;
7452 BFD_ASSERT (lsect
!= NULL
);
7456 /* Handle global symbol. */
7457 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7458 (h
->linker_section_pointer
,
7462 BFD_ASSERT (linker_section_ptr
!= NULL
);
7464 if (! elf_hash_table (info
)->dynamic_sections_created
7467 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
7469 /* This is actually a static link, or it is a
7470 -Bsymbolic link and the symbol is defined
7471 locally. We must initialize this entry in the
7474 When doing a dynamic link, we create a .rela.<xxx>
7475 relocation entry to initialize the value. This
7476 is done in the finish_dynamic_symbol routine. */
7477 if (!linker_section_ptr
->written_address_p
)
7479 linker_section_ptr
->written_address_p
= TRUE
;
7480 bfd_put_ptr (output_bfd
,
7481 relocation
+ linker_section_ptr
->addend
,
7482 (lsect
->section
->contents
7483 + linker_section_ptr
->offset
));
7489 /* Handle local symbol. */
7490 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7491 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
7492 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
7493 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7494 (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
7498 BFD_ASSERT (linker_section_ptr
!= NULL
);
7500 /* Write out pointer if it hasn't been rewritten out before. */
7501 if (!linker_section_ptr
->written_address_p
)
7503 linker_section_ptr
->written_address_p
= TRUE
;
7504 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
7505 lsect
->section
->contents
+ linker_section_ptr
->offset
);
7509 asection
*srel
= lsect
->rel_section
;
7510 Elf_Internal_Rela outrel
[MAX_INT_RELS_PER_EXT_REL
];
7512 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7515 /* We need to generate a relative reloc for the dynamic
7519 srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
7521 lsect
->rel_section
= srel
;
7524 BFD_ASSERT (srel
!= NULL
);
7526 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7528 outrel
[i
].r_offset
= (lsect
->section
->output_section
->vma
7529 + lsect
->section
->output_offset
7530 + linker_section_ptr
->offset
);
7531 outrel
[i
].r_info
= 0;
7532 outrel
[i
].r_addend
= 0;
7534 outrel
[0].r_info
= ELF_R_INFO (0, relative_reloc
);
7535 erel
= lsect
->section
->contents
;
7536 erel
+= (elf_section_data (lsect
->section
)->rel_count
++
7537 * sizeof (Elf_External_Rela
));
7538 elf_swap_reloca_out (output_bfd
, outrel
, erel
);
7543 relocation
= (lsect
->section
->output_offset
7544 + linker_section_ptr
->offset
7545 - lsect
->hole_offset
7546 - lsect
->sym_offset
);
7550 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7551 lsect
->name
, (long) relocation
, (long) relocation
);
7554 /* Subtract out the addend, because it will get added back in by the normal
7556 return relocation
- linker_section_ptr
->addend
;
7559 /* Garbage collect unused sections. */
7561 static bfd_boolean elf_gc_mark
7562 PARAMS ((struct bfd_link_info
*, asection
*,
7563 asection
* (*) (asection
*, struct bfd_link_info
*,
7564 Elf_Internal_Rela
*, struct elf_link_hash_entry
*,
7565 Elf_Internal_Sym
*)));
7567 static bfd_boolean elf_gc_sweep
7568 PARAMS ((struct bfd_link_info
*,
7569 bfd_boolean (*) (bfd
*, struct bfd_link_info
*, asection
*,
7570 const Elf_Internal_Rela
*)));
7572 static bfd_boolean elf_gc_sweep_symbol
7573 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7575 static bfd_boolean elf_gc_allocate_got_offsets
7576 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7578 static bfd_boolean elf_gc_propagate_vtable_entries_used
7579 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7581 static bfd_boolean elf_gc_smash_unused_vtentry_relocs
7582 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7584 /* The mark phase of garbage collection. For a given section, mark
7585 it and any sections in this section's group, and all the sections
7586 which define symbols to which it refers. */
7588 typedef asection
* (*gc_mark_hook_fn
)
7589 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7590 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
7593 elf_gc_mark (info
, sec
, gc_mark_hook
)
7594 struct bfd_link_info
*info
;
7596 gc_mark_hook_fn gc_mark_hook
;
7599 asection
*group_sec
;
7603 /* Mark all the sections in the group. */
7604 group_sec
= elf_section_data (sec
)->next_in_group
;
7605 if (group_sec
&& !group_sec
->gc_mark
)
7606 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
7609 /* Look through the section relocs. */
7611 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
7613 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
7614 Elf_Internal_Shdr
*symtab_hdr
;
7615 struct elf_link_hash_entry
**sym_hashes
;
7618 bfd
*input_bfd
= sec
->owner
;
7619 struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
7620 Elf_Internal_Sym
*isym
= NULL
;
7622 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
7623 sym_hashes
= elf_sym_hashes (input_bfd
);
7625 /* Read the local symbols. */
7626 if (elf_bad_symtab (input_bfd
))
7628 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
7632 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
7634 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
7635 if (isym
== NULL
&& nlocsyms
!= 0)
7637 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
7643 /* Read the relocations. */
7644 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
7645 (input_bfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
7646 info
->keep_memory
));
7647 if (relstart
== NULL
)
7652 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7654 for (rel
= relstart
; rel
< relend
; rel
++)
7656 unsigned long r_symndx
;
7658 struct elf_link_hash_entry
*h
;
7660 r_symndx
= ELF_R_SYM (rel
->r_info
);
7664 if (r_symndx
>= nlocsyms
7665 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
7667 h
= sym_hashes
[r_symndx
- extsymoff
];
7668 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
7672 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
7675 if (rsec
&& !rsec
->gc_mark
)
7677 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
7679 else if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
7688 if (elf_section_data (sec
)->relocs
!= relstart
)
7691 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
7693 if (! info
->keep_memory
)
7696 symtab_hdr
->contents
= (unsigned char *) isym
;
7703 /* The sweep phase of garbage collection. Remove all garbage sections. */
7705 typedef bfd_boolean (*gc_sweep_hook_fn
)
7706 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
7707 const Elf_Internal_Rela
*));
7710 elf_gc_sweep (info
, gc_sweep_hook
)
7711 struct bfd_link_info
*info
;
7712 gc_sweep_hook_fn gc_sweep_hook
;
7716 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7720 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
7723 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7725 /* Keep special sections. Keep .debug sections. */
7726 if ((o
->flags
& SEC_LINKER_CREATED
)
7727 || (o
->flags
& SEC_DEBUGGING
))
7733 /* Skip sweeping sections already excluded. */
7734 if (o
->flags
& SEC_EXCLUDE
)
7737 /* Since this is early in the link process, it is simple
7738 to remove a section from the output. */
7739 o
->flags
|= SEC_EXCLUDE
;
7741 /* But we also have to update some of the relocation
7742 info we collected before. */
7744 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
7746 Elf_Internal_Rela
*internal_relocs
;
7749 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
7750 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
7751 if (internal_relocs
== NULL
)
7754 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
7756 if (elf_section_data (o
)->relocs
!= internal_relocs
)
7757 free (internal_relocs
);
7765 /* Remove the symbols that were in the swept sections from the dynamic
7766 symbol table. GCFIXME: Anyone know how to get them out of the
7767 static symbol table as well? */
7771 elf_link_hash_traverse (elf_hash_table (info
),
7772 elf_gc_sweep_symbol
,
7775 elf_hash_table (info
)->dynsymcount
= i
;
7781 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7784 elf_gc_sweep_symbol (h
, idxptr
)
7785 struct elf_link_hash_entry
*h
;
7788 int *idx
= (int *) idxptr
;
7790 if (h
->root
.type
== bfd_link_hash_warning
)
7791 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7793 if (h
->dynindx
!= -1
7794 && ((h
->root
.type
!= bfd_link_hash_defined
7795 && h
->root
.type
!= bfd_link_hash_defweak
)
7796 || h
->root
.u
.def
.section
->gc_mark
))
7797 h
->dynindx
= (*idx
)++;
7802 /* Propogate collected vtable information. This is called through
7803 elf_link_hash_traverse. */
7806 elf_gc_propagate_vtable_entries_used (h
, okp
)
7807 struct elf_link_hash_entry
*h
;
7810 if (h
->root
.type
== bfd_link_hash_warning
)
7811 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7813 /* Those that are not vtables. */
7814 if (h
->vtable_parent
== NULL
)
7817 /* Those vtables that do not have parents, we cannot merge. */
7818 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
7821 /* If we've already been done, exit. */
7822 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
7825 /* Make sure the parent's table is up to date. */
7826 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
7828 if (h
->vtable_entries_used
== NULL
)
7830 /* None of this table's entries were referenced. Re-use the
7832 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
7833 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
7838 bfd_boolean
*cu
, *pu
;
7840 /* Or the parent's entries into ours. */
7841 cu
= h
->vtable_entries_used
;
7843 pu
= h
->vtable_parent
->vtable_entries_used
;
7846 asection
*sec
= h
->root
.u
.def
.section
;
7847 struct elf_backend_data
*bed
= get_elf_backend_data (sec
->owner
);
7848 int file_align
= bed
->s
->file_align
;
7850 n
= h
->vtable_parent
->vtable_entries_size
/ file_align
;
7865 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
7866 struct elf_link_hash_entry
*h
;
7870 bfd_vma hstart
, hend
;
7871 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
7872 struct elf_backend_data
*bed
;
7875 if (h
->root
.type
== bfd_link_hash_warning
)
7876 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7878 /* Take care of both those symbols that do not describe vtables as
7879 well as those that are not loaded. */
7880 if (h
->vtable_parent
== NULL
)
7883 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
7884 || h
->root
.type
== bfd_link_hash_defweak
);
7886 sec
= h
->root
.u
.def
.section
;
7887 hstart
= h
->root
.u
.def
.value
;
7888 hend
= hstart
+ h
->size
;
7890 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
7891 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, TRUE
));
7893 return *(bfd_boolean
*) okp
= FALSE
;
7894 bed
= get_elf_backend_data (sec
->owner
);
7895 file_align
= bed
->s
->file_align
;
7897 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7899 for (rel
= relstart
; rel
< relend
; ++rel
)
7900 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
7902 /* If the entry is in use, do nothing. */
7903 if (h
->vtable_entries_used
7904 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
7906 bfd_vma entry
= (rel
->r_offset
- hstart
) / file_align
;
7907 if (h
->vtable_entries_used
[entry
])
7910 /* Otherwise, kill it. */
7911 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
7917 /* Do mark and sweep of unused sections. */
7920 elf_gc_sections (abfd
, info
)
7922 struct bfd_link_info
*info
;
7924 bfd_boolean ok
= TRUE
;
7926 asection
* (*gc_mark_hook
)
7927 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7928 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
7930 if (!get_elf_backend_data (abfd
)->can_gc_sections
7931 || info
->relocateable
|| info
->emitrelocations
7932 || elf_hash_table (info
)->dynamic_sections_created
)
7935 /* Apply transitive closure to the vtable entry usage info. */
7936 elf_link_hash_traverse (elf_hash_table (info
),
7937 elf_gc_propagate_vtable_entries_used
,
7942 /* Kill the vtable relocations that were not used. */
7943 elf_link_hash_traverse (elf_hash_table (info
),
7944 elf_gc_smash_unused_vtentry_relocs
,
7949 /* Grovel through relocs to find out who stays ... */
7951 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
7952 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7956 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
7959 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7961 if (o
->flags
& SEC_KEEP
)
7962 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
7967 /* ... and mark SEC_EXCLUDE for those that go. */
7968 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
7974 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
7977 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
7980 struct elf_link_hash_entry
*h
;
7983 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
7984 struct elf_link_hash_entry
**search
, *child
;
7985 bfd_size_type extsymcount
;
7987 /* The sh_info field of the symtab header tells us where the
7988 external symbols start. We don't care about the local symbols at
7990 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
7991 if (!elf_bad_symtab (abfd
))
7992 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
7994 sym_hashes
= elf_sym_hashes (abfd
);
7995 sym_hashes_end
= sym_hashes
+ extsymcount
;
7997 /* Hunt down the child symbol, which is in this section at the same
7998 offset as the relocation. */
7999 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8001 if ((child
= *search
) != NULL
8002 && (child
->root
.type
== bfd_link_hash_defined
8003 || child
->root
.type
== bfd_link_hash_defweak
)
8004 && child
->root
.u
.def
.section
== sec
8005 && child
->root
.u
.def
.value
== offset
)
8009 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
8010 bfd_archive_filename (abfd
), sec
->name
,
8011 (unsigned long) offset
);
8012 bfd_set_error (bfd_error_invalid_operation
);
8018 /* This *should* only be the absolute section. It could potentially
8019 be that someone has defined a non-global vtable though, which
8020 would be bad. It isn't worth paging in the local symbols to be
8021 sure though; that case should simply be handled by the assembler. */
8023 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
8026 child
->vtable_parent
= h
;
8031 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
8034 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
8035 bfd
*abfd ATTRIBUTE_UNUSED
;
8036 asection
*sec ATTRIBUTE_UNUSED
;
8037 struct elf_link_hash_entry
*h
;
8040 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8041 int file_align
= bed
->s
->file_align
;
8043 if (addend
>= h
->vtable_entries_size
)
8046 bfd_boolean
*ptr
= h
->vtable_entries_used
;
8048 /* While the symbol is undefined, we have to be prepared to handle
8050 if (h
->root
.type
== bfd_link_hash_undefined
)
8057 /* Oops! We've got a reference past the defined end of
8058 the table. This is probably a bug -- shall we warn? */
8063 /* Allocate one extra entry for use as a "done" flag for the
8064 consolidation pass. */
8065 bytes
= (size
/ file_align
+ 1) * sizeof (bfd_boolean
);
8069 ptr
= bfd_realloc (ptr
- 1, (bfd_size_type
) bytes
);
8075 oldbytes
= ((h
->vtable_entries_size
/ file_align
+ 1)
8076 * sizeof (bfd_boolean
));
8077 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8081 ptr
= bfd_zmalloc ((bfd_size_type
) bytes
);
8086 /* And arrange for that done flag to be at index -1. */
8087 h
->vtable_entries_used
= ptr
+ 1;
8088 h
->vtable_entries_size
= size
;
8091 h
->vtable_entries_used
[addend
/ file_align
] = TRUE
;
8096 /* And an accompanying bit to work out final got entry offsets once
8097 we're done. Should be called from final_link. */
8100 elf_gc_common_finalize_got_offsets (abfd
, info
)
8102 struct bfd_link_info
*info
;
8105 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8108 /* The GOT offset is relative to the .got section, but the GOT header is
8109 put into the .got.plt section, if the backend uses it. */
8110 if (bed
->want_got_plt
)
8113 gotoff
= bed
->got_header_size
;
8115 /* Do the local .got entries first. */
8116 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
8118 bfd_signed_vma
*local_got
;
8119 bfd_size_type j
, locsymcount
;
8120 Elf_Internal_Shdr
*symtab_hdr
;
8122 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
8125 local_got
= elf_local_got_refcounts (i
);
8129 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
8130 if (elf_bad_symtab (i
))
8131 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8133 locsymcount
= symtab_hdr
->sh_info
;
8135 for (j
= 0; j
< locsymcount
; ++j
)
8137 if (local_got
[j
] > 0)
8139 local_got
[j
] = gotoff
;
8140 gotoff
+= ARCH_SIZE
/ 8;
8143 local_got
[j
] = (bfd_vma
) -1;
8147 /* Then the global .got entries. .plt refcounts are handled by
8148 adjust_dynamic_symbol */
8149 elf_link_hash_traverse (elf_hash_table (info
),
8150 elf_gc_allocate_got_offsets
,
8155 /* We need a special top-level link routine to convert got reference counts
8156 to real got offsets. */
8159 elf_gc_allocate_got_offsets (h
, offarg
)
8160 struct elf_link_hash_entry
*h
;
8163 bfd_vma
*off
= (bfd_vma
*) offarg
;
8165 if (h
->root
.type
== bfd_link_hash_warning
)
8166 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8168 if (h
->got
.refcount
> 0)
8170 h
->got
.offset
= off
[0];
8171 off
[0] += ARCH_SIZE
/ 8;
8174 h
->got
.offset
= (bfd_vma
) -1;
8179 /* Many folk need no more in the way of final link than this, once
8180 got entry reference counting is enabled. */
8183 elf_gc_common_final_link (abfd
, info
)
8185 struct bfd_link_info
*info
;
8187 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
8190 /* Invoke the regular ELF backend linker to do all the work. */
8191 return elf_bfd_final_link (abfd
, info
);
8194 /* This function will be called though elf_link_hash_traverse to store
8195 all hash value of the exported symbols in an array. */
8198 elf_collect_hash_codes (h
, data
)
8199 struct elf_link_hash_entry
*h
;
8202 unsigned long **valuep
= (unsigned long **) data
;
8208 if (h
->root
.type
== bfd_link_hash_warning
)
8209 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8211 /* Ignore indirect symbols. These are added by the versioning code. */
8212 if (h
->dynindx
== -1)
8215 name
= h
->root
.root
.string
;
8216 p
= strchr (name
, ELF_VER_CHR
);
8219 alc
= bfd_malloc ((bfd_size_type
) (p
- name
+ 1));
8220 memcpy (alc
, name
, (size_t) (p
- name
));
8221 alc
[p
- name
] = '\0';
8225 /* Compute the hash value. */
8226 ha
= bfd_elf_hash (name
);
8228 /* Store the found hash value in the array given as the argument. */
8231 /* And store it in the struct so that we can put it in the hash table
8233 h
->elf_hash_value
= ha
;
8242 elf_reloc_symbol_deleted_p (offset
, cookie
)
8246 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
8248 if (rcookie
->bad_symtab
)
8249 rcookie
->rel
= rcookie
->rels
;
8251 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
8253 unsigned long r_symndx
;
8255 if (! rcookie
->bad_symtab
)
8256 if (rcookie
->rel
->r_offset
> offset
)
8258 if (rcookie
->rel
->r_offset
!= offset
)
8261 r_symndx
= ELF_R_SYM (rcookie
->rel
->r_info
);
8262 if (r_symndx
== SHN_UNDEF
)
8265 if (r_symndx
>= rcookie
->locsymcount
8266 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
8268 struct elf_link_hash_entry
*h
;
8270 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
8272 while (h
->root
.type
== bfd_link_hash_indirect
8273 || h
->root
.type
== bfd_link_hash_warning
)
8274 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8276 if ((h
->root
.type
== bfd_link_hash_defined
8277 || h
->root
.type
== bfd_link_hash_defweak
)
8278 && elf_discarded_section (h
->root
.u
.def
.section
))
8285 /* It's not a relocation against a global symbol,
8286 but it could be a relocation against a local
8287 symbol for a discarded section. */
8289 Elf_Internal_Sym
*isym
;
8291 /* Need to: get the symbol; get the section. */
8292 isym
= &rcookie
->locsyms
[r_symndx
];
8293 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
8295 isec
= section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
8296 if (isec
!= NULL
&& elf_discarded_section (isec
))
8305 /* Discard unneeded references to discarded sections.
8306 Returns TRUE if any section's size was changed. */
8307 /* This function assumes that the relocations are in sorted order,
8308 which is true for all known assemblers. */
8311 elf_bfd_discard_info (output_bfd
, info
)
8313 struct bfd_link_info
*info
;
8315 struct elf_reloc_cookie cookie
;
8316 asection
*stab
, *eh
;
8317 Elf_Internal_Shdr
*symtab_hdr
;
8318 struct elf_backend_data
*bed
;
8321 bfd_boolean ret
= FALSE
;
8323 if (info
->traditional_format
8324 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
8325 || ! is_elf_hash_table (info
))
8328 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
8330 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
8333 bed
= get_elf_backend_data (abfd
);
8335 if ((abfd
->flags
& DYNAMIC
) != 0)
8338 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
8339 if (info
->relocateable
8341 && (eh
->_raw_size
== 0
8342 || bfd_is_abs_section (eh
->output_section
))))
8345 stab
= bfd_get_section_by_name (abfd
, ".stab");
8347 && (stab
->_raw_size
== 0
8348 || bfd_is_abs_section (stab
->output_section
)
8349 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
8354 && bed
->elf_backend_discard_info
== NULL
)
8357 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8359 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
8360 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
8361 if (cookie
.bad_symtab
)
8363 cookie
.locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8364 cookie
.extsymoff
= 0;
8368 cookie
.locsymcount
= symtab_hdr
->sh_info
;
8369 cookie
.extsymoff
= symtab_hdr
->sh_info
;
8372 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8373 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
8375 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
8376 cookie
.locsymcount
, 0,
8378 if (cookie
.locsyms
== NULL
)
8385 count
= stab
->reloc_count
;
8387 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8388 (abfd
, stab
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
8389 info
->keep_memory
));
8390 if (cookie
.rels
!= NULL
)
8392 cookie
.rel
= cookie
.rels
;
8393 cookie
.relend
= cookie
.rels
;
8394 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
8395 if (_bfd_discard_section_stabs (abfd
, stab
,
8396 elf_section_data (stab
)->sec_info
,
8397 elf_reloc_symbol_deleted_p
,
8400 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
8408 count
= eh
->reloc_count
;
8410 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8411 (abfd
, eh
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
8412 info
->keep_memory
));
8413 cookie
.rel
= cookie
.rels
;
8414 cookie
.relend
= cookie
.rels
;
8415 if (cookie
.rels
!= NULL
)
8416 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
8418 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
8419 elf_reloc_symbol_deleted_p
,
8423 if (cookie
.rels
!= NULL
8424 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
8428 if (bed
->elf_backend_discard_info
!= NULL
8429 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
8432 if (cookie
.locsyms
!= NULL
8433 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
8435 if (! info
->keep_memory
)
8436 free (cookie
.locsyms
);
8438 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
8442 if (info
->eh_frame_hdr
8443 && !info
->relocateable
8444 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
8451 elf_section_ignore_discarded_relocs (sec
)
8454 struct elf_backend_data
*bed
;
8456 switch (sec
->sec_info_type
)
8458 case ELF_INFO_TYPE_STABS
:
8459 case ELF_INFO_TYPE_EH_FRAME
:
8465 bed
= get_elf_backend_data (sec
->owner
);
8466 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
8467 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))