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
;
482 struct elf_link_hash_entry
*flip
;
485 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
490 bind
= ELF_ST_BIND (sym
->st_info
);
492 if (! bfd_is_und_section (sec
))
493 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
495 h
= ((struct elf_link_hash_entry
*)
496 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
501 /* This code is for coping with dynamic objects, and is only useful
502 if we are doing an ELF link. */
503 if (info
->hash
->creator
!= abfd
->xvec
)
506 /* For merging, we only care about real symbols. */
508 while (h
->root
.type
== bfd_link_hash_indirect
509 || h
->root
.type
== bfd_link_hash_warning
)
510 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
512 /* If we just created the symbol, mark it as being an ELF symbol.
513 Other than that, there is nothing to do--there is no merge issue
514 with a newly defined symbol--so we just return. */
516 if (h
->root
.type
== bfd_link_hash_new
)
518 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
522 /* OLDBFD is a BFD associated with the existing symbol. */
524 switch (h
->root
.type
)
530 case bfd_link_hash_undefined
:
531 case bfd_link_hash_undefweak
:
532 oldbfd
= h
->root
.u
.undef
.abfd
;
535 case bfd_link_hash_defined
:
536 case bfd_link_hash_defweak
:
537 oldbfd
= h
->root
.u
.def
.section
->owner
;
540 case bfd_link_hash_common
:
541 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
545 /* In cases involving weak versioned symbols, we may wind up trying
546 to merge a symbol with itself. Catch that here, to avoid the
547 confusion that results if we try to override a symbol with
548 itself. The additional tests catch cases like
549 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
550 dynamic object, which we do want to handle here. */
552 && ((abfd
->flags
& DYNAMIC
) == 0
553 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0))
556 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
557 respectively, is from a dynamic object. */
559 if ((abfd
->flags
& DYNAMIC
) != 0)
565 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
570 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
571 indices used by MIPS ELF. */
572 switch (h
->root
.type
)
578 case bfd_link_hash_defined
:
579 case bfd_link_hash_defweak
:
580 hsec
= h
->root
.u
.def
.section
;
583 case bfd_link_hash_common
:
584 hsec
= h
->root
.u
.c
.p
->section
;
591 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
594 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
595 respectively, appear to be a definition rather than reference. */
597 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
602 if (h
->root
.type
== bfd_link_hash_undefined
603 || h
->root
.type
== bfd_link_hash_undefweak
604 || h
->root
.type
== bfd_link_hash_common
)
609 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
610 symbol, respectively, appears to be a common symbol in a dynamic
611 object. If a symbol appears in an uninitialized section, and is
612 not weak, and is not a function, then it may be a common symbol
613 which was resolved when the dynamic object was created. We want
614 to treat such symbols specially, because they raise special
615 considerations when setting the symbol size: if the symbol
616 appears as a common symbol in a regular object, and the size in
617 the regular object is larger, we must make sure that we use the
618 larger size. This problematic case can always be avoided in C,
619 but it must be handled correctly when using Fortran shared
622 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
623 likewise for OLDDYNCOMMON and OLDDEF.
625 Note that this test is just a heuristic, and that it is quite
626 possible to have an uninitialized symbol in a shared object which
627 is really a definition, rather than a common symbol. This could
628 lead to some minor confusion when the symbol really is a common
629 symbol in some regular object. However, I think it will be
634 && (sec
->flags
& SEC_ALLOC
) != 0
635 && (sec
->flags
& SEC_LOAD
) == 0
638 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
641 newdyncommon
= FALSE
;
645 && h
->root
.type
== bfd_link_hash_defined
646 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
647 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
648 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
650 && h
->type
!= STT_FUNC
)
653 olddyncommon
= FALSE
;
655 /* It's OK to change the type if either the existing symbol or the
656 new symbol is weak unless it comes from a DT_NEEDED entry of
657 a shared object, in which case, the DT_NEEDED entry may not be
658 required at the run time. */
660 if ((! dt_needed
&& h
->root
.type
== bfd_link_hash_defweak
)
661 || h
->root
.type
== bfd_link_hash_undefweak
663 *type_change_ok
= TRUE
;
665 /* It's OK to change the size if either the existing symbol or the
666 new symbol is weak, or if the old symbol is undefined. */
669 || h
->root
.type
== bfd_link_hash_undefined
)
670 *size_change_ok
= TRUE
;
672 /* If both the old and the new symbols look like common symbols in a
673 dynamic object, set the size of the symbol to the larger of the
678 && sym
->st_size
!= h
->size
)
680 /* Since we think we have two common symbols, issue a multiple
681 common warning if desired. Note that we only warn if the
682 size is different. If the size is the same, we simply let
683 the old symbol override the new one as normally happens with
684 symbols defined in dynamic objects. */
686 if (! ((*info
->callbacks
->multiple_common
)
687 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
688 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
691 if (sym
->st_size
> h
->size
)
692 h
->size
= sym
->st_size
;
694 *size_change_ok
= TRUE
;
697 /* If we are looking at a dynamic object, and we have found a
698 definition, we need to see if the symbol was already defined by
699 some other object. If so, we want to use the existing
700 definition, and we do not want to report a multiple symbol
701 definition error; we do this by clobbering *PSEC to be
704 We treat a common symbol as a definition if the symbol in the
705 shared library is a function, since common symbols always
706 represent variables; this can cause confusion in principle, but
707 any such confusion would seem to indicate an erroneous program or
708 shared library. We also permit a common symbol in a regular
709 object to override a weak symbol in a shared object.
711 We prefer a non-weak definition in a shared library to a weak
712 definition in the executable unless it comes from a DT_NEEDED
713 entry of a shared object, in which case, the DT_NEEDED entry
714 may not be required at the run time. */
719 || (h
->root
.type
== bfd_link_hash_common
721 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)))
722 && (h
->root
.type
!= bfd_link_hash_defweak
724 || bind
== STB_WEAK
))
728 newdyncommon
= FALSE
;
730 *psec
= sec
= bfd_und_section_ptr
;
731 *size_change_ok
= TRUE
;
733 /* If we get here when the old symbol is a common symbol, then
734 we are explicitly letting it override a weak symbol or
735 function in a dynamic object, and we don't want to warn about
736 a type change. If the old symbol is a defined symbol, a type
737 change warning may still be appropriate. */
739 if (h
->root
.type
== bfd_link_hash_common
)
740 *type_change_ok
= TRUE
;
743 /* Handle the special case of an old common symbol merging with a
744 new symbol which looks like a common symbol in a shared object.
745 We change *PSEC and *PVALUE to make the new symbol look like a
746 common symbol, and let _bfd_generic_link_add_one_symbol will do
750 && h
->root
.type
== bfd_link_hash_common
)
754 newdyncommon
= FALSE
;
755 *pvalue
= sym
->st_size
;
756 *psec
= sec
= bfd_com_section_ptr
;
757 *size_change_ok
= TRUE
;
760 /* If the old symbol is from a dynamic object, and the new symbol is
761 a definition which is not from a dynamic object, then the new
762 symbol overrides the old symbol. Symbols from regular files
763 always take precedence over symbols from dynamic objects, even if
764 they are defined after the dynamic object in the link.
766 As above, we again permit a common symbol in a regular object to
767 override a definition in a shared object if the shared object
768 symbol is a function or is weak.
770 As above, we permit a non-weak definition in a shared object to
771 override a weak definition in a regular object. */
776 || (bfd_is_com_section (sec
)
777 && (h
->root
.type
== bfd_link_hash_defweak
778 || h
->type
== STT_FUNC
)))
781 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
783 || h
->root
.type
== bfd_link_hash_defweak
))
785 /* Change the hash table entry to undefined, and let
786 _bfd_generic_link_add_one_symbol do the right thing with the
789 h
->root
.type
= bfd_link_hash_undefined
;
790 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
791 *size_change_ok
= TRUE
;
794 olddyncommon
= FALSE
;
796 /* We again permit a type change when a common symbol may be
797 overriding a function. */
799 if (bfd_is_com_section (sec
))
800 *type_change_ok
= TRUE
;
802 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
805 /* This union may have been set to be non-NULL when this symbol
806 was seen in a dynamic object. We must force the union to be
807 NULL, so that it is correct for a regular symbol. */
808 h
->verinfo
.vertree
= NULL
;
811 /* Handle the special case of a new common symbol merging with an
812 old symbol that looks like it might be a common symbol defined in
813 a shared object. Note that we have already handled the case in
814 which a new common symbol should simply override the definition
815 in the shared library. */
818 && bfd_is_com_section (sec
)
821 /* It would be best if we could set the hash table entry to a
822 common symbol, but we don't know what to use for the section
824 if (! ((*info
->callbacks
->multiple_common
)
825 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
826 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
829 /* If the predumed common symbol in the dynamic object is
830 larger, pretend that the new symbol has its size. */
832 if (h
->size
> *pvalue
)
835 /* FIXME: We no longer know the alignment required by the symbol
836 in the dynamic object, so we just wind up using the one from
837 the regular object. */
840 olddyncommon
= FALSE
;
842 h
->root
.type
= bfd_link_hash_undefined
;
843 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
845 *size_change_ok
= TRUE
;
846 *type_change_ok
= TRUE
;
848 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
851 h
->verinfo
.vertree
= NULL
;
856 /* Handle the case where we had a versioned symbol in a dynamic
857 library and now find a definition in a normal object. In this
858 case, we make the versioned symbol point to the normal one. */
859 flip
->root
.type
= h
->root
.type
;
860 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
861 h
->root
.type
= bfd_link_hash_indirect
;
862 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
863 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
865 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_DEF_DYNAMIC
;
866 flip
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
870 /* Handle the special case of a weak definition in a regular object
871 followed by a non-weak definition in a shared object. In this
872 case, we prefer the definition in the shared object unless it
873 comes from a DT_NEEDED entry of a shared object, in which case,
874 the DT_NEEDED entry may not be required at the run time. */
877 && h
->root
.type
== bfd_link_hash_defweak
882 /* To make this work we have to frob the flags so that the rest
883 of the code does not think we are using the regular
885 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
886 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
887 else if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
888 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
889 h
->elf_link_hash_flags
&= ~ (ELF_LINK_HASH_DEF_REGULAR
890 | ELF_LINK_HASH_DEF_DYNAMIC
);
892 /* If H is the target of an indirection, we want the caller to
893 use H rather than the indirect symbol. Otherwise if we are
894 defining a new indirect symbol we will wind up attaching it
895 to the entry we are overriding. */
899 /* Handle the special case of a non-weak definition in a shared
900 object followed by a weak definition in a regular object. In
901 this case we prefer the definition in the shared object. To make
902 this work we have to tell the caller to not treat the new symbol
906 && h
->root
.type
!= bfd_link_hash_defweak
915 /* This function is called to create an indirect symbol from the
916 default for the symbol with the default version if needed. The
917 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
918 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
919 indicates if it comes from a DT_NEEDED entry of a shared object. */
922 elf_add_default_symbol (abfd
, info
, h
, name
, sym
, psec
, value
,
923 dynsym
, override
, dt_needed
)
925 struct bfd_link_info
*info
;
926 struct elf_link_hash_entry
*h
;
928 Elf_Internal_Sym
*sym
;
932 bfd_boolean override
;
933 bfd_boolean dt_needed
;
935 bfd_boolean type_change_ok
;
936 bfd_boolean size_change_ok
;
938 struct elf_link_hash_entry
*hi
;
939 struct bfd_link_hash_entry
*bh
;
940 struct elf_backend_data
*bed
;
944 size_t len
, shortlen
;
947 /* If this symbol has a version, and it is the default version, we
948 create an indirect symbol from the default name to the fully
949 decorated name. This will cause external references which do not
950 specify a version to be bound to this version of the symbol. */
951 p
= strchr (name
, ELF_VER_CHR
);
952 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
957 /* We are overridden by an old defition. We need to check if we
958 need to create the indirect symbol from the default name. */
959 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
961 BFD_ASSERT (hi
!= NULL
);
964 while (hi
->root
.type
== bfd_link_hash_indirect
965 || hi
->root
.type
== bfd_link_hash_warning
)
967 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
973 bed
= get_elf_backend_data (abfd
);
974 collect
= bed
->collect
;
975 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
978 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
979 if (shortname
== NULL
)
981 memcpy (shortname
, name
, shortlen
);
982 shortname
[shortlen
] = '\0';
984 /* We are going to create a new symbol. Merge it with any existing
985 symbol with this name. For the purposes of the merge, act as
986 though we were defining the symbol we just defined, although we
987 actually going to define an indirect symbol. */
988 type_change_ok
= FALSE
;
989 size_change_ok
= FALSE
;
991 if (! elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
992 &hi
, &override
, &type_change_ok
,
993 &size_change_ok
, dt_needed
))
999 if (! (_bfd_generic_link_add_one_symbol
1000 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1001 (bfd_vma
) 0, name
, FALSE
, collect
, &bh
)))
1003 hi
= (struct elf_link_hash_entry
*) bh
;
1007 /* In this case the symbol named SHORTNAME is overriding the
1008 indirect symbol we want to add. We were planning on making
1009 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1010 is the name without a version. NAME is the fully versioned
1011 name, and it is the default version.
1013 Overriding means that we already saw a definition for the
1014 symbol SHORTNAME in a regular object, and it is overriding
1015 the symbol defined in the dynamic object.
1017 When this happens, we actually want to change NAME, the
1018 symbol we just added, to refer to SHORTNAME. This will cause
1019 references to NAME in the shared object to become references
1020 to SHORTNAME in the regular object. This is what we expect
1021 when we override a function in a shared object: that the
1022 references in the shared object will be mapped to the
1023 definition in the regular object. */
1025 while (hi
->root
.type
== bfd_link_hash_indirect
1026 || hi
->root
.type
== bfd_link_hash_warning
)
1027 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1029 h
->root
.type
= bfd_link_hash_indirect
;
1030 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1031 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1033 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1034 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1035 if (hi
->elf_link_hash_flags
1036 & (ELF_LINK_HASH_REF_REGULAR
1037 | ELF_LINK_HASH_DEF_REGULAR
))
1039 if (! _bfd_elf_link_record_dynamic_symbol (info
, hi
))
1044 /* Now set HI to H, so that the following code will set the
1045 other fields correctly. */
1049 /* If there is a duplicate definition somewhere, then HI may not
1050 point to an indirect symbol. We will have reported an error to
1051 the user in that case. */
1053 if (hi
->root
.type
== bfd_link_hash_indirect
)
1055 struct elf_link_hash_entry
*ht
;
1057 /* If the symbol became indirect, then we assume that we have
1058 not seen a definition before. */
1059 BFD_ASSERT ((hi
->elf_link_hash_flags
1060 & (ELF_LINK_HASH_DEF_DYNAMIC
1061 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1063 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1064 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1066 /* See if the new flags lead us to realize that the symbol must
1073 || ((hi
->elf_link_hash_flags
1074 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1079 if ((hi
->elf_link_hash_flags
1080 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1086 /* We also need to define an indirection from the nondefault version
1089 len
= strlen (name
);
1090 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1091 if (shortname
== NULL
)
1093 memcpy (shortname
, name
, shortlen
);
1094 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1096 /* Once again, merge with any existing symbol. */
1097 type_change_ok
= FALSE
;
1098 size_change_ok
= FALSE
;
1100 if (! elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1101 &hi
, &override
, &type_change_ok
,
1102 &size_change_ok
, dt_needed
))
1107 /* Here SHORTNAME is a versioned name, so we don't expect to see
1108 the type of override we do in the case above unless it is
1109 overridden by a versioned definiton. */
1110 if (hi
->root
.type
!= bfd_link_hash_defined
1111 && hi
->root
.type
!= bfd_link_hash_defweak
)
1112 (*_bfd_error_handler
)
1113 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1114 bfd_archive_filename (abfd
), shortname
);
1119 if (! (_bfd_generic_link_add_one_symbol
1120 (info
, abfd
, shortname
, BSF_INDIRECT
,
1121 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, FALSE
, collect
, &bh
)))
1123 hi
= (struct elf_link_hash_entry
*) bh
;
1125 /* If there is a duplicate definition somewhere, then HI may not
1126 point to an indirect symbol. We will have reported an error
1127 to the user in that case. */
1129 if (hi
->root
.type
== bfd_link_hash_indirect
)
1131 /* If the symbol became indirect, then we assume that we have
1132 not seen a definition before. */
1133 BFD_ASSERT ((hi
->elf_link_hash_flags
1134 & (ELF_LINK_HASH_DEF_DYNAMIC
1135 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1137 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1139 /* See if the new flags lead us to realize that the symbol
1146 || ((hi
->elf_link_hash_flags
1147 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1152 if ((hi
->elf_link_hash_flags
1153 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1163 /* Add symbols from an ELF object file to the linker hash table. */
1166 elf_link_add_object_symbols (abfd
, info
)
1168 struct bfd_link_info
*info
;
1170 bfd_boolean (*add_symbol_hook
)
1171 PARAMS ((bfd
*, struct bfd_link_info
*, const Elf_Internal_Sym
*,
1172 const char **, flagword
*, asection
**, bfd_vma
*));
1173 bfd_boolean (*check_relocs
)
1174 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
1175 const Elf_Internal_Rela
*));
1176 bfd_boolean collect
;
1177 Elf_Internal_Shdr
*hdr
;
1178 bfd_size_type symcount
;
1179 bfd_size_type extsymcount
;
1180 bfd_size_type extsymoff
;
1181 struct elf_link_hash_entry
**sym_hash
;
1182 bfd_boolean dynamic
;
1183 Elf_External_Versym
*extversym
= NULL
;
1184 Elf_External_Versym
*ever
;
1185 struct elf_link_hash_entry
*weaks
;
1186 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
1187 bfd_size_type nondeflt_vers_cnt
= 0;
1188 Elf_Internal_Sym
*isymbuf
= NULL
;
1189 Elf_Internal_Sym
*isym
;
1190 Elf_Internal_Sym
*isymend
;
1191 struct elf_backend_data
*bed
;
1192 bfd_boolean dt_needed
;
1193 struct elf_link_hash_table
* hash_table
;
1196 hash_table
= elf_hash_table (info
);
1198 bed
= get_elf_backend_data (abfd
);
1199 add_symbol_hook
= bed
->elf_add_symbol_hook
;
1200 collect
= bed
->collect
;
1202 if ((abfd
->flags
& DYNAMIC
) == 0)
1208 /* You can't use -r against a dynamic object. Also, there's no
1209 hope of using a dynamic object which does not exactly match
1210 the format of the output file. */
1211 if (info
->relocateable
|| info
->hash
->creator
!= abfd
->xvec
)
1213 bfd_set_error (bfd_error_invalid_operation
);
1218 /* As a GNU extension, any input sections which are named
1219 .gnu.warning.SYMBOL are treated as warning symbols for the given
1220 symbol. This differs from .gnu.warning sections, which generate
1221 warnings when they are included in an output file. */
1226 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
1230 name
= bfd_get_section_name (abfd
, s
);
1231 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1236 name
+= sizeof ".gnu.warning." - 1;
1238 /* If this is a shared object, then look up the symbol
1239 in the hash table. If it is there, and it is already
1240 been defined, then we will not be using the entry
1241 from this shared object, so we don't need to warn.
1242 FIXME: If we see the definition in a regular object
1243 later on, we will warn, but we shouldn't. The only
1244 fix is to keep track of what warnings we are supposed
1245 to emit, and then handle them all at the end of the
1247 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
1249 struct elf_link_hash_entry
*h
;
1251 h
= elf_link_hash_lookup (hash_table
, name
,
1252 FALSE
, FALSE
, TRUE
);
1254 /* FIXME: What about bfd_link_hash_common? */
1256 && (h
->root
.type
== bfd_link_hash_defined
1257 || h
->root
.type
== bfd_link_hash_defweak
))
1259 /* We don't want to issue this warning. Clobber
1260 the section size so that the warning does not
1261 get copied into the output file. */
1267 sz
= bfd_section_size (abfd
, s
);
1268 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
1272 if (! bfd_get_section_contents (abfd
, s
, msg
, (file_ptr
) 0, sz
))
1277 if (! (_bfd_generic_link_add_one_symbol
1278 (info
, abfd
, name
, BSF_WARNING
, s
, (bfd_vma
) 0, msg
,
1279 FALSE
, collect
, (struct bfd_link_hash_entry
**) NULL
)))
1282 if (! info
->relocateable
)
1284 /* Clobber the section size so that the warning does
1285 not get copied into the output file. */
1295 /* If we are creating a shared library, create all the dynamic
1296 sections immediately. We need to attach them to something,
1297 so we attach them to this BFD, provided it is the right
1298 format. FIXME: If there are no input BFD's of the same
1299 format as the output, we can't make a shared library. */
1301 && is_elf_hash_table (info
)
1302 && ! hash_table
->dynamic_sections_created
1303 && abfd
->xvec
== info
->hash
->creator
)
1305 if (! elf_link_create_dynamic_sections (abfd
, info
))
1309 else if (! is_elf_hash_table (info
))
1314 bfd_boolean add_needed
;
1316 bfd_size_type oldsize
;
1317 bfd_size_type strindex
;
1318 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
1320 /* ld --just-symbols and dynamic objects don't mix very well.
1321 Test for --just-symbols by looking at info set up by
1322 _bfd_elf_link_just_syms. */
1323 if ((s
= abfd
->sections
) != NULL
1324 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
1327 /* Find the name to use in a DT_NEEDED entry that refers to this
1328 object. If the object has a DT_SONAME entry, we use it.
1329 Otherwise, if the generic linker stuck something in
1330 elf_dt_name, we use that. Otherwise, we just use the file
1331 name. If the generic linker put a null string into
1332 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1333 there is a DT_SONAME entry. */
1335 name
= bfd_get_filename (abfd
);
1336 if (elf_dt_name (abfd
) != NULL
)
1338 name
= elf_dt_name (abfd
);
1341 if (elf_dt_soname (abfd
) != NULL
)
1347 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1350 Elf_External_Dyn
*dynbuf
= NULL
;
1351 Elf_External_Dyn
*extdyn
;
1352 Elf_External_Dyn
*extdynend
;
1354 unsigned long shlink
;
1356 dynbuf
= (Elf_External_Dyn
*) bfd_malloc (s
->_raw_size
);
1360 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
1361 (file_ptr
) 0, s
->_raw_size
))
1362 goto error_free_dyn
;
1364 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1366 goto error_free_dyn
;
1367 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1370 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
1371 for (; extdyn
< extdynend
; extdyn
++)
1373 Elf_Internal_Dyn dyn
;
1375 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
1376 if (dyn
.d_tag
== DT_SONAME
)
1378 unsigned int tagv
= dyn
.d_un
.d_val
;
1379 name
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1381 goto error_free_dyn
;
1383 if (dyn
.d_tag
== DT_NEEDED
)
1385 struct bfd_link_needed_list
*n
, **pn
;
1387 unsigned int tagv
= dyn
.d_un
.d_val
;
1389 amt
= sizeof (struct bfd_link_needed_list
);
1390 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1391 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1392 if (n
== NULL
|| fnm
== NULL
)
1393 goto error_free_dyn
;
1394 amt
= strlen (fnm
) + 1;
1395 anm
= bfd_alloc (abfd
, amt
);
1397 goto error_free_dyn
;
1398 memcpy (anm
, fnm
, (size_t) amt
);
1402 for (pn
= & hash_table
->needed
;
1408 if (dyn
.d_tag
== DT_RUNPATH
)
1410 struct bfd_link_needed_list
*n
, **pn
;
1412 unsigned int tagv
= dyn
.d_un
.d_val
;
1414 amt
= sizeof (struct bfd_link_needed_list
);
1415 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1416 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1417 if (n
== NULL
|| fnm
== NULL
)
1418 goto error_free_dyn
;
1419 amt
= strlen (fnm
) + 1;
1420 anm
= bfd_alloc (abfd
, amt
);
1422 goto error_free_dyn
;
1423 memcpy (anm
, fnm
, (size_t) amt
);
1427 for (pn
= & runpath
;
1433 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1434 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
1436 struct bfd_link_needed_list
*n
, **pn
;
1438 unsigned int tagv
= dyn
.d_un
.d_val
;
1440 amt
= sizeof (struct bfd_link_needed_list
);
1441 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1442 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1443 if (n
== NULL
|| fnm
== NULL
)
1444 goto error_free_dyn
;
1445 amt
= strlen (fnm
) + 1;
1446 anm
= bfd_alloc (abfd
, amt
);
1453 memcpy (anm
, fnm
, (size_t) amt
);
1468 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
1469 frees all more recently bfd_alloc'd blocks as well. */
1475 struct bfd_link_needed_list
**pn
;
1476 for (pn
= & hash_table
->runpath
;
1483 /* We do not want to include any of the sections in a dynamic
1484 object in the output file. We hack by simply clobbering the
1485 list of sections in the BFD. This could be handled more
1486 cleanly by, say, a new section flag; the existing
1487 SEC_NEVER_LOAD flag is not the one we want, because that one
1488 still implies that the section takes up space in the output
1490 bfd_section_list_clear (abfd
);
1492 /* If this is the first dynamic object found in the link, create
1493 the special sections required for dynamic linking. */
1494 if (! hash_table
->dynamic_sections_created
)
1495 if (! elf_link_create_dynamic_sections (abfd
, info
))
1500 /* Add a DT_NEEDED entry for this dynamic object. */
1501 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
1502 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, name
, FALSE
);
1503 if (strindex
== (bfd_size_type
) -1)
1506 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
1509 Elf_External_Dyn
*dyncon
, *dynconend
;
1511 /* The hash table size did not change, which means that
1512 the dynamic object name was already entered. If we
1513 have already included this dynamic object in the
1514 link, just ignore it. There is no reason to include
1515 a particular dynamic object more than once. */
1516 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
1517 BFD_ASSERT (sdyn
!= NULL
);
1519 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1520 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1522 for (; dyncon
< dynconend
; dyncon
++)
1524 Elf_Internal_Dyn dyn
;
1526 elf_swap_dyn_in (hash_table
->dynobj
, dyncon
, & dyn
);
1527 if (dyn
.d_tag
== DT_NEEDED
1528 && dyn
.d_un
.d_val
== strindex
)
1530 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
1536 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
1540 /* Save the SONAME, if there is one, because sometimes the
1541 linker emulation code will need to know it. */
1543 name
= basename (bfd_get_filename (abfd
));
1544 elf_dt_name (abfd
) = name
;
1547 /* If this is a dynamic object, we always link against the .dynsym
1548 symbol table, not the .symtab symbol table. The dynamic linker
1549 will only see the .dynsym symbol table, so there is no reason to
1550 look at .symtab for a dynamic object. */
1552 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
1553 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1555 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1557 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
1559 /* The sh_info field of the symtab header tells us where the
1560 external symbols start. We don't care about the local symbols at
1562 if (elf_bad_symtab (abfd
))
1564 extsymcount
= symcount
;
1569 extsymcount
= symcount
- hdr
->sh_info
;
1570 extsymoff
= hdr
->sh_info
;
1574 if (extsymcount
!= 0)
1576 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
1578 if (isymbuf
== NULL
)
1581 /* We store a pointer to the hash table entry for each external
1583 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
1584 sym_hash
= (struct elf_link_hash_entry
**) bfd_alloc (abfd
, amt
);
1585 if (sym_hash
== NULL
)
1586 goto error_free_sym
;
1587 elf_sym_hashes (abfd
) = sym_hash
;
1592 /* Read in any version definitions. */
1593 if (! _bfd_elf_slurp_version_tables (abfd
))
1594 goto error_free_sym
;
1596 /* Read in the symbol versions, but don't bother to convert them
1597 to internal format. */
1598 if (elf_dynversym (abfd
) != 0)
1600 Elf_Internal_Shdr
*versymhdr
;
1602 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
1603 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
1604 if (extversym
== NULL
)
1605 goto error_free_sym
;
1606 amt
= versymhdr
->sh_size
;
1607 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
1608 || bfd_bread ((PTR
) extversym
, amt
, abfd
) != amt
)
1609 goto error_free_vers
;
1615 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
1616 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
1618 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
1625 struct elf_link_hash_entry
*h
;
1626 bfd_boolean definition
;
1627 bfd_boolean size_change_ok
;
1628 bfd_boolean type_change_ok
;
1629 bfd_boolean new_weakdef
;
1630 bfd_boolean override
;
1631 unsigned int old_alignment
;
1636 flags
= BSF_NO_FLAGS
;
1638 value
= isym
->st_value
;
1641 bind
= ELF_ST_BIND (isym
->st_info
);
1642 if (bind
== STB_LOCAL
)
1644 /* This should be impossible, since ELF requires that all
1645 global symbols follow all local symbols, and that sh_info
1646 point to the first global symbol. Unfortunatealy, Irix 5
1650 else if (bind
== STB_GLOBAL
)
1652 if (isym
->st_shndx
!= SHN_UNDEF
1653 && isym
->st_shndx
!= SHN_COMMON
)
1656 else if (bind
== STB_WEAK
)
1660 /* Leave it up to the processor backend. */
1663 if (isym
->st_shndx
== SHN_UNDEF
)
1664 sec
= bfd_und_section_ptr
;
1665 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
1667 sec
= section_from_elf_index (abfd
, isym
->st_shndx
);
1669 sec
= bfd_abs_section_ptr
;
1670 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
1673 else if (isym
->st_shndx
== SHN_ABS
)
1674 sec
= bfd_abs_section_ptr
;
1675 else if (isym
->st_shndx
== SHN_COMMON
)
1677 sec
= bfd_com_section_ptr
;
1678 /* What ELF calls the size we call the value. What ELF
1679 calls the value we call the alignment. */
1680 value
= isym
->st_size
;
1684 /* Leave it up to the processor backend. */
1687 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
1689 if (name
== (const char *) NULL
)
1690 goto error_free_vers
;
1692 if (isym
->st_shndx
== SHN_COMMON
1693 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
1695 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
1699 tcomm
= bfd_make_section (abfd
, ".tcommon");
1701 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
1703 | SEC_LINKER_CREATED
1704 | SEC_THREAD_LOCAL
)))
1705 goto error_free_vers
;
1709 else if (add_symbol_hook
)
1711 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
1713 goto error_free_vers
;
1715 /* The hook function sets the name to NULL if this symbol
1716 should be skipped for some reason. */
1717 if (name
== (const char *) NULL
)
1721 /* Sanity check that all possibilities were handled. */
1722 if (sec
== (asection
*) NULL
)
1724 bfd_set_error (bfd_error_bad_value
);
1725 goto error_free_vers
;
1728 if (bfd_is_und_section (sec
)
1729 || bfd_is_com_section (sec
))
1734 size_change_ok
= FALSE
;
1735 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
1739 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1741 Elf_Internal_Versym iver
;
1742 unsigned int vernum
= 0;
1746 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
1747 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
1749 /* If this is a hidden symbol, or if it is not version
1750 1, we append the version name to the symbol name.
1751 However, we do not modify a non-hidden absolute
1752 symbol, because it might be the version symbol
1753 itself. FIXME: What if it isn't? */
1754 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
1755 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
1758 size_t namelen
, verlen
, newlen
;
1761 if (isym
->st_shndx
!= SHN_UNDEF
)
1763 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
1765 (*_bfd_error_handler
)
1766 (_("%s: %s: invalid version %u (max %d)"),
1767 bfd_archive_filename (abfd
), name
, vernum
,
1768 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1769 bfd_set_error (bfd_error_bad_value
);
1770 goto error_free_vers
;
1772 else if (vernum
> 1)
1774 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1780 /* We cannot simply test for the number of
1781 entries in the VERNEED section since the
1782 numbers for the needed versions do not start
1784 Elf_Internal_Verneed
*t
;
1787 for (t
= elf_tdata (abfd
)->verref
;
1791 Elf_Internal_Vernaux
*a
;
1793 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1795 if (a
->vna_other
== vernum
)
1797 verstr
= a
->vna_nodename
;
1806 (*_bfd_error_handler
)
1807 (_("%s: %s: invalid needed version %d"),
1808 bfd_archive_filename (abfd
), name
, vernum
);
1809 bfd_set_error (bfd_error_bad_value
);
1810 goto error_free_vers
;
1814 namelen
= strlen (name
);
1815 verlen
= strlen (verstr
);
1816 newlen
= namelen
+ verlen
+ 2;
1817 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1818 && isym
->st_shndx
!= SHN_UNDEF
)
1821 newname
= (char *) bfd_alloc (abfd
, (bfd_size_type
) newlen
);
1822 if (newname
== NULL
)
1823 goto error_free_vers
;
1824 memcpy (newname
, name
, namelen
);
1825 p
= newname
+ namelen
;
1827 /* If this is a defined non-hidden version symbol,
1828 we add another @ to the name. This indicates the
1829 default version of the symbol. */
1830 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1831 && isym
->st_shndx
!= SHN_UNDEF
)
1833 memcpy (p
, verstr
, verlen
+ 1);
1839 if (! elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
1840 sym_hash
, &override
, &type_change_ok
,
1841 &size_change_ok
, dt_needed
))
1842 goto error_free_vers
;
1848 while (h
->root
.type
== bfd_link_hash_indirect
1849 || h
->root
.type
== bfd_link_hash_warning
)
1850 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1852 /* Remember the old alignment if this is a common symbol, so
1853 that we don't reduce the alignment later on. We can't
1854 check later, because _bfd_generic_link_add_one_symbol
1855 will set a default for the alignment which we want to
1856 override. We also remember the old bfd where the existing
1857 definition comes from. */
1858 switch (h
->root
.type
)
1863 case bfd_link_hash_defined
:
1864 case bfd_link_hash_defweak
:
1865 old_bfd
= h
->root
.u
.def
.section
->owner
;
1868 case bfd_link_hash_common
:
1869 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
1870 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1874 if (elf_tdata (abfd
)->verdef
!= NULL
1878 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1881 if (! (_bfd_generic_link_add_one_symbol
1882 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
1883 FALSE
, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
1884 goto error_free_vers
;
1887 while (h
->root
.type
== bfd_link_hash_indirect
1888 || h
->root
.type
== bfd_link_hash_warning
)
1889 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1892 new_weakdef
= FALSE
;
1895 && (flags
& BSF_WEAK
) != 0
1896 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
1897 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1898 && h
->weakdef
== NULL
)
1900 /* Keep a list of all weak defined non function symbols from
1901 a dynamic object, using the weakdef field. Later in this
1902 function we will set the weakdef field to the correct
1903 value. We only put non-function symbols from dynamic
1904 objects on this list, because that happens to be the only
1905 time we need to know the normal symbol corresponding to a
1906 weak symbol, and the information is time consuming to
1907 figure out. If the weakdef field is not already NULL,
1908 then this symbol was already defined by some previous
1909 dynamic object, and we will be using that previous
1910 definition anyhow. */
1917 /* Set the alignment of a common symbol. */
1918 if (isym
->st_shndx
== SHN_COMMON
1919 && h
->root
.type
== bfd_link_hash_common
)
1923 align
= bfd_log2 (isym
->st_value
);
1924 if (align
> old_alignment
1925 /* Permit an alignment power of zero if an alignment of one
1926 is specified and no other alignments have been specified. */
1927 || (isym
->st_value
== 1 && old_alignment
== 0))
1928 h
->root
.u
.c
.p
->alignment_power
= align
;
1930 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
1933 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1939 /* Check the alignment when a common symbol is involved. This
1940 can change when a common symbol is overriden by a normal
1941 definition or a common symbol is ignored due to the old
1942 normal definition. We need to make sure the maximum
1943 alignment is maintained. */
1944 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
1945 && h
->root
.type
!= bfd_link_hash_common
)
1947 unsigned int common_align
;
1948 unsigned int normal_align
;
1949 unsigned int symbol_align
;
1953 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
1954 if ((h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
1956 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
1957 if (normal_align
> symbol_align
)
1958 normal_align
= symbol_align
;
1961 normal_align
= symbol_align
;
1965 common_align
= old_alignment
;
1966 common_bfd
= old_bfd
;
1971 common_align
= bfd_log2 (isym
->st_value
);
1973 normal_bfd
= old_bfd
;
1976 if (normal_align
< common_align
)
1977 (*_bfd_error_handler
)
1978 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
1981 bfd_archive_filename (normal_bfd
),
1983 bfd_archive_filename (common_bfd
));
1986 /* Remember the symbol size and type. */
1987 if (isym
->st_size
!= 0
1988 && (definition
|| h
->size
== 0))
1990 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
1991 (*_bfd_error_handler
)
1992 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
1993 name
, (unsigned long) h
->size
,
1994 bfd_archive_filename (old_bfd
),
1995 (unsigned long) isym
->st_size
,
1996 bfd_archive_filename (abfd
));
1998 h
->size
= isym
->st_size
;
2001 /* If this is a common symbol, then we always want H->SIZE
2002 to be the size of the common symbol. The code just above
2003 won't fix the size if a common symbol becomes larger. We
2004 don't warn about a size change here, because that is
2005 covered by --warn-common. */
2006 if (h
->root
.type
== bfd_link_hash_common
)
2007 h
->size
= h
->root
.u
.c
.size
;
2009 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
2010 && (definition
|| h
->type
== STT_NOTYPE
))
2012 if (h
->type
!= STT_NOTYPE
2013 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
2014 && ! type_change_ok
)
2015 (*_bfd_error_handler
)
2016 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
2017 name
, h
->type
, ELF_ST_TYPE (isym
->st_info
),
2018 bfd_archive_filename (abfd
));
2020 h
->type
= ELF_ST_TYPE (isym
->st_info
);
2023 /* If st_other has a processor-specific meaning, specific code
2024 might be needed here. */
2025 if (isym
->st_other
!= 0)
2027 unsigned char hvis
, symvis
, other
, nvis
;
2029 /* Take the balance of OTHER from the definition. */
2030 other
= (definition
? isym
->st_other
: h
->other
);
2031 other
&= ~ ELF_ST_VISIBILITY (-1);
2033 /* Combine visibilities, using the most constraining one. */
2034 hvis
= ELF_ST_VISIBILITY (h
->other
);
2035 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
2041 nvis
= hvis
< symvis
? hvis
: symvis
;
2043 h
->other
= other
| nvis
;
2046 /* Set a flag in the hash table entry indicating the type of
2047 reference or definition we just found. Keep a count of
2048 the number of dynamic symbols we find. A dynamic symbol
2049 is one which is referenced or defined by both a regular
2050 object and a shared object. */
2051 old_flags
= h
->elf_link_hash_flags
;
2057 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
2058 if (bind
!= STB_WEAK
)
2059 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
2062 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
2064 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2065 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
2071 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
2073 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
2074 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
2075 | ELF_LINK_HASH_REF_REGULAR
)) != 0
2076 || (h
->weakdef
!= NULL
2078 && h
->weakdef
->dynindx
!= -1))
2082 h
->elf_link_hash_flags
|= new_flag
;
2084 /* Check to see if we need to add an indirect symbol for
2085 the default name. */
2086 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
2087 if (! elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
2088 &sec
, &value
, &dynsym
,
2089 override
, dt_needed
))
2090 goto error_free_vers
;
2092 if (definition
&& (abfd
->flags
& DYNAMIC
) == 0)
2094 char *p
= strchr (name
, ELF_VER_CHR
);
2095 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
2097 /* Queue non-default versions so that .symver x, x@FOO
2098 aliases can be checked. */
2099 if (! nondeflt_vers
)
2101 amt
= (isymend
- isym
+ 1)
2102 * sizeof (struct elf_link_hash_entry
*);
2103 nondeflt_vers
= bfd_malloc (amt
);
2105 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
2109 if (dynsym
&& h
->dynindx
== -1)
2111 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2112 goto error_free_vers
;
2113 if (h
->weakdef
!= NULL
2115 && h
->weakdef
->dynindx
== -1)
2117 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2118 goto error_free_vers
;
2121 else if (dynsym
&& h
->dynindx
!= -1)
2122 /* If the symbol already has a dynamic index, but
2123 visibility says it should not be visible, turn it into
2125 switch (ELF_ST_VISIBILITY (h
->other
))
2129 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2133 if (dt_needed
&& definition
2134 && (h
->elf_link_hash_flags
2135 & ELF_LINK_HASH_REF_REGULAR
) != 0)
2137 bfd_size_type oldsize
;
2138 bfd_size_type strindex
;
2140 if (! is_elf_hash_table (info
))
2141 goto error_free_vers
;
2143 /* The symbol from a DT_NEEDED object is referenced from
2144 the regular object to create a dynamic executable. We
2145 have to make sure there is a DT_NEEDED entry for it. */
2148 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2149 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
,
2150 elf_dt_soname (abfd
), FALSE
);
2151 if (strindex
== (bfd_size_type
) -1)
2152 goto error_free_vers
;
2154 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2157 Elf_External_Dyn
*dyncon
, *dynconend
;
2159 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
,
2161 BFD_ASSERT (sdyn
!= NULL
);
2163 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
2164 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
2166 for (; dyncon
< dynconend
; dyncon
++)
2168 Elf_Internal_Dyn dyn
;
2170 elf_swap_dyn_in (hash_table
->dynobj
,
2172 BFD_ASSERT (dyn
.d_tag
!= DT_NEEDED
||
2173 dyn
.d_un
.d_val
!= strindex
);
2177 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
2178 goto error_free_vers
;
2183 /* Now that all the symbols from this input file are created, handle
2184 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
2185 if (nondeflt_vers
!= NULL
)
2187 bfd_size_type cnt
, symidx
;
2189 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
2191 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
2192 char *shortname
, *p
;
2194 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2196 || (h
->root
.type
!= bfd_link_hash_defined
2197 && h
->root
.type
!= bfd_link_hash_defweak
))
2200 amt
= p
- h
->root
.root
.string
;
2201 shortname
= bfd_malloc (amt
+ 1);
2202 memcpy (shortname
, h
->root
.root
.string
, amt
);
2203 shortname
[amt
] = '\0';
2205 hi
= (struct elf_link_hash_entry
*)
2206 bfd_link_hash_lookup (info
->hash
, shortname
,
2207 FALSE
, FALSE
, FALSE
);
2209 && hi
->root
.type
== h
->root
.type
2210 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
2211 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
2213 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
2214 hi
->root
.type
= bfd_link_hash_indirect
;
2215 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
2216 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
2217 sym_hash
= elf_sym_hashes (abfd
);
2219 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
2220 if (sym_hash
[symidx
] == hi
)
2222 sym_hash
[symidx
] = h
;
2228 free (nondeflt_vers
);
2229 nondeflt_vers
= NULL
;
2232 if (extversym
!= NULL
)
2238 if (isymbuf
!= NULL
)
2242 /* Now set the weakdefs field correctly for all the weak defined
2243 symbols we found. The only way to do this is to search all the
2244 symbols. Since we only need the information for non functions in
2245 dynamic objects, that's the only time we actually put anything on
2246 the list WEAKS. We need this information so that if a regular
2247 object refers to a symbol defined weakly in a dynamic object, the
2248 real symbol in the dynamic object is also put in the dynamic
2249 symbols; we also must arrange for both symbols to point to the
2250 same memory location. We could handle the general case of symbol
2251 aliasing, but a general symbol alias can only be generated in
2252 assembler code, handling it correctly would be very time
2253 consuming, and other ELF linkers don't handle general aliasing
2255 while (weaks
!= NULL
)
2257 struct elf_link_hash_entry
*hlook
;
2260 struct elf_link_hash_entry
**hpp
;
2261 struct elf_link_hash_entry
**hppend
;
2264 weaks
= hlook
->weakdef
;
2265 hlook
->weakdef
= NULL
;
2267 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
2268 || hlook
->root
.type
== bfd_link_hash_defweak
2269 || hlook
->root
.type
== bfd_link_hash_common
2270 || hlook
->root
.type
== bfd_link_hash_indirect
);
2271 slook
= hlook
->root
.u
.def
.section
;
2272 vlook
= hlook
->root
.u
.def
.value
;
2274 hpp
= elf_sym_hashes (abfd
);
2275 hppend
= hpp
+ extsymcount
;
2276 for (; hpp
< hppend
; hpp
++)
2278 struct elf_link_hash_entry
*h
;
2281 if (h
!= NULL
&& h
!= hlook
2282 && h
->root
.type
== bfd_link_hash_defined
2283 && h
->root
.u
.def
.section
== slook
2284 && h
->root
.u
.def
.value
== vlook
)
2288 /* If the weak definition is in the list of dynamic
2289 symbols, make sure the real definition is put there
2291 if (hlook
->dynindx
!= -1
2292 && h
->dynindx
== -1)
2294 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2298 /* If the real definition is in the list of dynamic
2299 symbols, make sure the weak definition is put there
2300 as well. If we don't do this, then the dynamic
2301 loader might not merge the entries for the real
2302 definition and the weak definition. */
2303 if (h
->dynindx
!= -1
2304 && hlook
->dynindx
== -1)
2306 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
2314 /* If this object is the same format as the output object, and it is
2315 not a shared library, then let the backend look through the
2318 This is required to build global offset table entries and to
2319 arrange for dynamic relocs. It is not required for the
2320 particular common case of linking non PIC code, even when linking
2321 against shared libraries, but unfortunately there is no way of
2322 knowing whether an object file has been compiled PIC or not.
2323 Looking through the relocs is not particularly time consuming.
2324 The problem is that we must either (1) keep the relocs in memory,
2325 which causes the linker to require additional runtime memory or
2326 (2) read the relocs twice from the input file, which wastes time.
2327 This would be a good case for using mmap.
2329 I have no idea how to handle linking PIC code into a file of a
2330 different format. It probably can't be done. */
2331 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
2333 && abfd
->xvec
== info
->hash
->creator
2334 && check_relocs
!= NULL
)
2338 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2340 Elf_Internal_Rela
*internal_relocs
;
2343 if ((o
->flags
& SEC_RELOC
) == 0
2344 || o
->reloc_count
== 0
2345 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
2346 && (o
->flags
& SEC_DEBUGGING
) != 0)
2347 || bfd_is_abs_section (o
->output_section
))
2350 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
2351 (abfd
, o
, (PTR
) NULL
,
2352 (Elf_Internal_Rela
*) NULL
,
2353 info
->keep_memory
));
2354 if (internal_relocs
== NULL
)
2357 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
2359 if (elf_section_data (o
)->relocs
!= internal_relocs
)
2360 free (internal_relocs
);
2367 /* If this is a non-traditional link, try to optimize the handling
2368 of the .stab/.stabstr sections. */
2370 && ! info
->traditional_format
2371 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
2372 && is_elf_hash_table (info
)
2373 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
2375 asection
*stab
, *stabstr
;
2377 stab
= bfd_get_section_by_name (abfd
, ".stab");
2379 && (stab
->flags
& SEC_MERGE
) == 0
2380 && !bfd_is_abs_section (stab
->output_section
))
2382 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
2384 if (stabstr
!= NULL
)
2386 struct bfd_elf_section_data
*secdata
;
2388 secdata
= elf_section_data (stab
);
2389 if (! _bfd_link_section_stabs (abfd
,
2390 & hash_table
->stab_info
,
2392 &secdata
->sec_info
))
2394 if (secdata
->sec_info
)
2395 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
2400 if (! info
->relocateable
&& ! dynamic
2401 && is_elf_hash_table (info
))
2405 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2406 if ((s
->flags
& SEC_MERGE
) != 0
2407 && !bfd_is_abs_section (s
->output_section
))
2409 struct bfd_elf_section_data
*secdata
;
2411 secdata
= elf_section_data (s
);
2412 if (! _bfd_merge_section (abfd
,
2413 & hash_table
->merge_info
,
2414 s
, &secdata
->sec_info
))
2416 else if (secdata
->sec_info
)
2417 s
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
2421 if (is_elf_hash_table (info
))
2423 /* Add this bfd to the loaded list. */
2424 struct elf_link_loaded_list
*n
;
2426 n
= ((struct elf_link_loaded_list
*)
2427 bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
)));
2431 n
->next
= hash_table
->loaded
;
2432 hash_table
->loaded
= n
;
2438 if (nondeflt_vers
!= NULL
)
2439 free (nondeflt_vers
);
2440 if (extversym
!= NULL
)
2443 if (isymbuf
!= NULL
)
2449 /* Create some sections which will be filled in with dynamic linking
2450 information. ABFD is an input file which requires dynamic sections
2451 to be created. The dynamic sections take up virtual memory space
2452 when the final executable is run, so we need to create them before
2453 addresses are assigned to the output sections. We work out the
2454 actual contents and size of these sections later. */
2457 elf_link_create_dynamic_sections (abfd
, info
)
2459 struct bfd_link_info
*info
;
2462 register asection
*s
;
2463 struct elf_link_hash_entry
*h
;
2464 struct bfd_link_hash_entry
*bh
;
2465 struct elf_backend_data
*bed
;
2467 if (! is_elf_hash_table (info
))
2470 if (elf_hash_table (info
)->dynamic_sections_created
)
2473 /* Make sure that all dynamic sections use the same input BFD. */
2474 if (elf_hash_table (info
)->dynobj
== NULL
)
2475 elf_hash_table (info
)->dynobj
= abfd
;
2477 abfd
= elf_hash_table (info
)->dynobj
;
2479 /* Note that we set the SEC_IN_MEMORY flag for all of these
2481 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
2482 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
2484 /* A dynamically linked executable has a .interp section, but a
2485 shared library does not. */
2488 s
= bfd_make_section (abfd
, ".interp");
2490 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2494 if (! info
->traditional_format
2495 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
2497 s
= bfd_make_section (abfd
, ".eh_frame_hdr");
2499 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2500 || ! bfd_set_section_alignment (abfd
, s
, 2))
2502 elf_hash_table (info
)->eh_info
.hdr_sec
= s
;
2505 /* Create sections to hold version informations. These are removed
2506 if they are not needed. */
2507 s
= bfd_make_section (abfd
, ".gnu.version_d");
2509 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2510 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2513 s
= bfd_make_section (abfd
, ".gnu.version");
2515 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2516 || ! bfd_set_section_alignment (abfd
, s
, 1))
2519 s
= bfd_make_section (abfd
, ".gnu.version_r");
2521 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2522 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2525 s
= bfd_make_section (abfd
, ".dynsym");
2527 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2528 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2531 s
= bfd_make_section (abfd
, ".dynstr");
2533 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2536 /* Create a strtab to hold the dynamic symbol names. */
2537 if (elf_hash_table (info
)->dynstr
== NULL
)
2539 elf_hash_table (info
)->dynstr
= _bfd_elf_strtab_init ();
2540 if (elf_hash_table (info
)->dynstr
== NULL
)
2544 s
= bfd_make_section (abfd
, ".dynamic");
2546 || ! bfd_set_section_flags (abfd
, s
, flags
)
2547 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2550 /* The special symbol _DYNAMIC is always set to the start of the
2551 .dynamic section. This call occurs before we have processed the
2552 symbols for any dynamic object, so we don't have to worry about
2553 overriding a dynamic definition. We could set _DYNAMIC in a
2554 linker script, but we only want to define it if we are, in fact,
2555 creating a .dynamic section. We don't want to define it if there
2556 is no .dynamic section, since on some ELF platforms the start up
2557 code examines it to decide how to initialize the process. */
2559 if (! (_bfd_generic_link_add_one_symbol
2560 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
2561 (const char *) 0, FALSE
, get_elf_backend_data (abfd
)->collect
, &bh
)))
2563 h
= (struct elf_link_hash_entry
*) bh
;
2564 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2565 h
->type
= STT_OBJECT
;
2568 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2571 bed
= get_elf_backend_data (abfd
);
2573 s
= bfd_make_section (abfd
, ".hash");
2575 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2576 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2578 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
2580 /* Let the backend create the rest of the sections. This lets the
2581 backend set the right flags. The backend will normally create
2582 the .got and .plt sections. */
2583 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
2586 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
2591 /* Add an entry to the .dynamic table. */
2594 elf_add_dynamic_entry (info
, tag
, val
)
2595 struct bfd_link_info
*info
;
2599 Elf_Internal_Dyn dyn
;
2602 bfd_size_type newsize
;
2603 bfd_byte
*newcontents
;
2605 if (! is_elf_hash_table (info
))
2608 dynobj
= elf_hash_table (info
)->dynobj
;
2610 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
2611 BFD_ASSERT (s
!= NULL
);
2613 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
2614 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
2615 if (newcontents
== NULL
)
2619 dyn
.d_un
.d_val
= val
;
2620 elf_swap_dyn_out (dynobj
, &dyn
,
2621 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
2623 s
->_raw_size
= newsize
;
2624 s
->contents
= newcontents
;
2629 /* Read and swap the relocs from the section indicated by SHDR. This
2630 may be either a REL or a RELA section. The relocations are
2631 translated into RELA relocations and stored in INTERNAL_RELOCS,
2632 which should have already been allocated to contain enough space.
2633 The EXTERNAL_RELOCS are a buffer where the external form of the
2634 relocations should be stored.
2636 Returns FALSE if something goes wrong. */
2639 elf_link_read_relocs_from_section (abfd
, shdr
, external_relocs
,
2642 Elf_Internal_Shdr
*shdr
;
2643 PTR external_relocs
;
2644 Elf_Internal_Rela
*internal_relocs
;
2646 struct elf_backend_data
*bed
;
2647 void (*swap_in
) PARAMS ((bfd
*, const bfd_byte
*, Elf_Internal_Rela
*));
2648 const bfd_byte
*erela
;
2649 const bfd_byte
*erelaend
;
2650 Elf_Internal_Rela
*irela
;
2652 /* If there aren't any relocations, that's OK. */
2656 /* Position ourselves at the start of the section. */
2657 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2660 /* Read the relocations. */
2661 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2664 bed
= get_elf_backend_data (abfd
);
2666 /* Convert the external relocations to the internal format. */
2667 if (shdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2668 swap_in
= bed
->s
->swap_reloc_in
;
2669 else if (shdr
->sh_entsize
== sizeof (Elf_External_Rela
))
2670 swap_in
= bed
->s
->swap_reloca_in
;
2673 bfd_set_error (bfd_error_wrong_format
);
2677 erela
= external_relocs
;
2678 erelaend
= erela
+ NUM_SHDR_ENTRIES (shdr
) * shdr
->sh_entsize
;
2679 irela
= internal_relocs
;
2680 while (erela
< erelaend
)
2682 (*swap_in
) (abfd
, erela
, irela
);
2683 irela
+= bed
->s
->int_rels_per_ext_rel
;
2684 erela
+= shdr
->sh_entsize
;
2690 /* Read and swap the relocs for a section O. They may have been
2691 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2692 not NULL, they are used as buffers to read into. They are known to
2693 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2694 the return value is allocated using either malloc or bfd_alloc,
2695 according to the KEEP_MEMORY argument. If O has two relocation
2696 sections (both REL and RELA relocations), then the REL_HDR
2697 relocations will appear first in INTERNAL_RELOCS, followed by the
2698 REL_HDR2 relocations. */
2701 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
2705 PTR external_relocs
;
2706 Elf_Internal_Rela
*internal_relocs
;
2707 bfd_boolean keep_memory
;
2709 Elf_Internal_Shdr
*rel_hdr
;
2711 Elf_Internal_Rela
*alloc2
= NULL
;
2712 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2714 if (elf_section_data (o
)->relocs
!= NULL
)
2715 return elf_section_data (o
)->relocs
;
2717 if (o
->reloc_count
== 0)
2720 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2722 if (internal_relocs
== NULL
)
2726 size
= o
->reloc_count
;
2727 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2729 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2731 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2732 if (internal_relocs
== NULL
)
2736 if (external_relocs
== NULL
)
2738 bfd_size_type size
= rel_hdr
->sh_size
;
2740 if (elf_section_data (o
)->rel_hdr2
)
2741 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2742 alloc1
= (PTR
) bfd_malloc (size
);
2745 external_relocs
= alloc1
;
2748 if (!elf_link_read_relocs_from_section (abfd
, rel_hdr
,
2752 if (!elf_link_read_relocs_from_section
2754 elf_section_data (o
)->rel_hdr2
,
2755 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2756 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2757 * bed
->s
->int_rels_per_ext_rel
)))
2760 /* Cache the results for next time, if we can. */
2762 elf_section_data (o
)->relocs
= internal_relocs
;
2767 /* Don't free alloc2, since if it was allocated we are passing it
2768 back (under the name of internal_relocs). */
2770 return internal_relocs
;
2780 /* Record an assignment to a symbol made by a linker script. We need
2781 this in case some dynamic object refers to this symbol. */
2784 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
2785 bfd
*output_bfd ATTRIBUTE_UNUSED
;
2786 struct bfd_link_info
*info
;
2788 bfd_boolean provide
;
2790 struct elf_link_hash_entry
*h
;
2792 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2795 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, TRUE
, FALSE
);
2799 if (h
->root
.type
== bfd_link_hash_new
)
2800 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_ELF
;
2802 /* If this symbol is being provided by the linker script, and it is
2803 currently defined by a dynamic object, but not by a regular
2804 object, then mark it as undefined so that the generic linker will
2805 force the correct value. */
2807 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2808 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2809 h
->root
.type
= bfd_link_hash_undefined
;
2811 /* If this symbol is not being provided by the linker script, and it is
2812 currently defined by a dynamic object, but not by a regular object,
2813 then clear out any version information because the symbol will not be
2814 associated with the dynamic object any more. */
2816 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2817 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2818 h
->verinfo
.verdef
= NULL
;
2820 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2822 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2823 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
2825 && h
->dynindx
== -1)
2827 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2830 /* If this is a weak defined symbol, and we know a corresponding
2831 real symbol from the same dynamic object, make sure the real
2832 symbol is also made into a dynamic symbol. */
2833 if (h
->weakdef
!= NULL
2834 && h
->weakdef
->dynindx
== -1)
2836 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2844 /* This structure is used to pass information to
2845 elf_link_assign_sym_version. */
2847 struct elf_assign_sym_version_info
2851 /* General link information. */
2852 struct bfd_link_info
*info
;
2854 struct bfd_elf_version_tree
*verdefs
;
2855 /* Whether we had a failure. */
2859 /* This structure is used to pass information to
2860 elf_link_find_version_dependencies. */
2862 struct elf_find_verdep_info
2866 /* General link information. */
2867 struct bfd_link_info
*info
;
2868 /* The number of dependencies. */
2870 /* Whether we had a failure. */
2874 /* Array used to determine the number of hash table buckets to use
2875 based on the number of symbols there are. If there are fewer than
2876 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2877 fewer than 37 we use 17 buckets, and so forth. We never use more
2878 than 32771 buckets. */
2880 static const size_t elf_buckets
[] =
2882 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2886 /* Compute bucket count for hashing table. We do not use a static set
2887 of possible tables sizes anymore. Instead we determine for all
2888 possible reasonable sizes of the table the outcome (i.e., the
2889 number of collisions etc) and choose the best solution. The
2890 weighting functions are not too simple to allow the table to grow
2891 without bounds. Instead one of the weighting factors is the size.
2892 Therefore the result is always a good payoff between few collisions
2893 (= short chain lengths) and table size. */
2895 compute_bucket_count (info
)
2896 struct bfd_link_info
*info
;
2898 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2899 size_t best_size
= 0;
2900 unsigned long int *hashcodes
;
2901 unsigned long int *hashcodesp
;
2902 unsigned long int i
;
2905 /* Compute the hash values for all exported symbols. At the same
2906 time store the values in an array so that we could use them for
2909 amt
*= sizeof (unsigned long int);
2910 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
2911 if (hashcodes
== NULL
)
2913 hashcodesp
= hashcodes
;
2915 /* Put all hash values in HASHCODES. */
2916 elf_link_hash_traverse (elf_hash_table (info
),
2917 elf_collect_hash_codes
, &hashcodesp
);
2919 /* We have a problem here. The following code to optimize the table
2920 size requires an integer type with more the 32 bits. If
2921 BFD_HOST_U_64_BIT is set we know about such a type. */
2922 #ifdef BFD_HOST_U_64_BIT
2925 unsigned long int nsyms
= hashcodesp
- hashcodes
;
2928 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
2929 unsigned long int *counts
;
2931 /* Possible optimization parameters: if we have NSYMS symbols we say
2932 that the hashing table must at least have NSYMS/4 and at most
2934 minsize
= nsyms
/ 4;
2937 best_size
= maxsize
= nsyms
* 2;
2939 /* Create array where we count the collisions in. We must use bfd_malloc
2940 since the size could be large. */
2942 amt
*= sizeof (unsigned long int);
2943 counts
= (unsigned long int *) bfd_malloc (amt
);
2950 /* Compute the "optimal" size for the hash table. The criteria is a
2951 minimal chain length. The minor criteria is (of course) the size
2953 for (i
= minsize
; i
< maxsize
; ++i
)
2955 /* Walk through the array of hashcodes and count the collisions. */
2956 BFD_HOST_U_64_BIT max
;
2957 unsigned long int j
;
2958 unsigned long int fact
;
2960 memset (counts
, '\0', i
* sizeof (unsigned long int));
2962 /* Determine how often each hash bucket is used. */
2963 for (j
= 0; j
< nsyms
; ++j
)
2964 ++counts
[hashcodes
[j
] % i
];
2966 /* For the weight function we need some information about the
2967 pagesize on the target. This is information need not be 100%
2968 accurate. Since this information is not available (so far) we
2969 define it here to a reasonable default value. If it is crucial
2970 to have a better value some day simply define this value. */
2971 # ifndef BFD_TARGET_PAGESIZE
2972 # define BFD_TARGET_PAGESIZE (4096)
2975 /* We in any case need 2 + NSYMS entries for the size values and
2977 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
2980 /* Variant 1: optimize for short chains. We add the squares
2981 of all the chain lengths (which favous many small chain
2982 over a few long chains). */
2983 for (j
= 0; j
< i
; ++j
)
2984 max
+= counts
[j
] * counts
[j
];
2986 /* This adds penalties for the overall size of the table. */
2987 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2990 /* Variant 2: Optimize a lot more for small table. Here we
2991 also add squares of the size but we also add penalties for
2992 empty slots (the +1 term). */
2993 for (j
= 0; j
< i
; ++j
)
2994 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
2996 /* The overall size of the table is considered, but not as
2997 strong as in variant 1, where it is squared. */
2998 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
3002 /* Compare with current best results. */
3003 if (max
< best_chlen
)
3013 #endif /* defined (BFD_HOST_U_64_BIT) */
3015 /* This is the fallback solution if no 64bit type is available or if we
3016 are not supposed to spend much time on optimizations. We select the
3017 bucket count using a fixed set of numbers. */
3018 for (i
= 0; elf_buckets
[i
] != 0; i
++)
3020 best_size
= elf_buckets
[i
];
3021 if (dynsymcount
< elf_buckets
[i
+ 1])
3026 /* Free the arrays we needed. */
3032 /* Set up the sizes and contents of the ELF dynamic sections. This is
3033 called by the ELF linker emulation before_allocation routine. We
3034 must set the sizes of the sections before the linker sets the
3035 addresses of the various sections. */
3038 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
3040 auxiliary_filters
, info
, sinterpptr
,
3045 const char *filter_shlib
;
3046 const char * const *auxiliary_filters
;
3047 struct bfd_link_info
*info
;
3048 asection
**sinterpptr
;
3049 struct bfd_elf_version_tree
*verdefs
;
3051 bfd_size_type soname_indx
;
3053 struct elf_backend_data
*bed
;
3054 struct elf_assign_sym_version_info asvinfo
;
3058 soname_indx
= (bfd_size_type
) -1;
3060 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
3063 if (! is_elf_hash_table (info
))
3066 /* Any syms created from now on start with -1 in
3067 got.refcount/offset and plt.refcount/offset. */
3068 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
3070 /* The backend may have to create some sections regardless of whether
3071 we're dynamic or not. */
3072 bed
= get_elf_backend_data (output_bfd
);
3073 if (bed
->elf_backend_always_size_sections
3074 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
3077 dynobj
= elf_hash_table (info
)->dynobj
;
3079 /* If there were no dynamic objects in the link, there is nothing to
3084 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
3087 if (elf_hash_table (info
)->dynamic_sections_created
)
3089 struct elf_info_failed eif
;
3090 struct elf_link_hash_entry
*h
;
3092 struct bfd_elf_version_tree
*t
;
3093 struct bfd_elf_version_expr
*d
;
3094 bfd_boolean all_defined
;
3096 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
3097 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
3101 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3103 if (soname_indx
== (bfd_size_type
) -1
3104 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SONAME
,
3111 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMBOLIC
,
3114 info
->flags
|= DF_SYMBOLIC
;
3121 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
3123 if (info
->new_dtags
)
3124 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
3125 if (indx
== (bfd_size_type
) -1
3126 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RPATH
, indx
)
3128 && ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RUNPATH
,
3133 if (filter_shlib
!= NULL
)
3137 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3138 filter_shlib
, TRUE
);
3139 if (indx
== (bfd_size_type
) -1
3140 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FILTER
, indx
))
3144 if (auxiliary_filters
!= NULL
)
3146 const char * const *p
;
3148 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
3152 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3154 if (indx
== (bfd_size_type
) -1
3155 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_AUXILIARY
,
3162 eif
.verdefs
= verdefs
;
3165 /* If we are supposed to export all symbols into the dynamic symbol
3166 table (this is not the normal case), then do so. */
3167 if (info
->export_dynamic
)
3169 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
3175 /* Make all global versions with definiton. */
3176 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3177 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3178 if (!d
->symver
&& strchr (d
->pattern
, '*') == NULL
)
3180 const char *verstr
, *name
;
3181 size_t namelen
, verlen
, newlen
;
3183 struct elf_link_hash_entry
*newh
;
3186 namelen
= strlen (name
);
3188 verlen
= strlen (verstr
);
3189 newlen
= namelen
+ verlen
+ 3;
3191 newname
= (char *) bfd_malloc ((bfd_size_type
) newlen
);
3192 if (newname
== NULL
)
3194 memcpy (newname
, name
, namelen
);
3196 /* Check the hidden versioned definition. */
3197 p
= newname
+ namelen
;
3199 memcpy (p
, verstr
, verlen
+ 1);
3200 newh
= elf_link_hash_lookup (elf_hash_table (info
),
3201 newname
, FALSE
, FALSE
,
3204 || (newh
->root
.type
!= bfd_link_hash_defined
3205 && newh
->root
.type
!= bfd_link_hash_defweak
))
3207 /* Check the default versioned definition. */
3209 memcpy (p
, verstr
, verlen
+ 1);
3210 newh
= elf_link_hash_lookup (elf_hash_table (info
),
3211 newname
, FALSE
, FALSE
,
3216 /* Mark this version if there is a definition and it is
3217 not defined in a shared object. */
3219 && ((newh
->elf_link_hash_flags
3220 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)
3221 && (newh
->root
.type
== bfd_link_hash_defined
3222 || newh
->root
.type
== bfd_link_hash_defweak
))
3226 /* Attach all the symbols to their version information. */
3227 asvinfo
.output_bfd
= output_bfd
;
3228 asvinfo
.info
= info
;
3229 asvinfo
.verdefs
= verdefs
;
3230 asvinfo
.failed
= FALSE
;
3232 elf_link_hash_traverse (elf_hash_table (info
),
3233 elf_link_assign_sym_version
,
3238 if (!info
->allow_undefined_version
)
3240 /* Check if all global versions have a definiton. */
3242 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3243 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3244 if (!d
->symver
&& !d
->script
3245 && strchr (d
->pattern
, '*') == NULL
)
3247 (*_bfd_error_handler
)
3248 (_("%s: undefined version: %s"),
3249 d
->pattern
, t
->name
);
3250 all_defined
= FALSE
;
3255 bfd_set_error (bfd_error_bad_value
);
3260 /* Find all symbols which were defined in a dynamic object and make
3261 the backend pick a reasonable value for them. */
3262 elf_link_hash_traverse (elf_hash_table (info
),
3263 elf_adjust_dynamic_symbol
,
3268 /* Add some entries to the .dynamic section. We fill in some of the
3269 values later, in elf_bfd_final_link, but we must add the entries
3270 now so that we know the final size of the .dynamic section. */
3272 /* If there are initialization and/or finalization functions to
3273 call then add the corresponding DT_INIT/DT_FINI entries. */
3274 h
= (info
->init_function
3275 ? elf_link_hash_lookup (elf_hash_table (info
),
3276 info
->init_function
, FALSE
,
3280 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3281 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3283 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT
, (bfd_vma
) 0))
3286 h
= (info
->fini_function
3287 ? elf_link_hash_lookup (elf_hash_table (info
),
3288 info
->fini_function
, FALSE
,
3292 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3293 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3295 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI
, (bfd_vma
) 0))
3299 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
3301 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3307 for (sub
= info
->input_bfds
; sub
!= NULL
;
3308 sub
= sub
->link_next
)
3309 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3310 if (elf_section_data (o
)->this_hdr
.sh_type
3311 == SHT_PREINIT_ARRAY
)
3313 (*_bfd_error_handler
)
3314 (_("%s: .preinit_array section is not allowed in DSO"),
3315 bfd_archive_filename (sub
));
3319 bfd_set_error (bfd_error_nonrepresentable_section
);
3323 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAY
,
3325 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAYSZ
,
3329 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
3331 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAY
,
3333 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAYSZ
,
3337 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
3339 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAY
,
3341 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAYSZ
,
3346 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
3347 /* If .dynstr is excluded from the link, we don't want any of
3348 these tags. Strictly, we should be checking each section
3349 individually; This quick check covers for the case where
3350 someone does a /DISCARD/ : { *(*) }. */
3351 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
3353 bfd_size_type strsize
;
3355 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3356 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_HASH
, (bfd_vma
) 0)
3357 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRTAB
, (bfd_vma
) 0)
3358 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMTAB
, (bfd_vma
) 0)
3359 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRSZ
, strsize
)
3360 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMENT
,
3361 (bfd_vma
) sizeof (Elf_External_Sym
)))
3366 /* The backend must work out the sizes of all the other dynamic
3368 if (bed
->elf_backend_size_dynamic_sections
3369 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
3372 if (elf_hash_table (info
)->dynamic_sections_created
)
3374 bfd_size_type dynsymcount
;
3376 size_t bucketcount
= 0;
3377 size_t hash_entry_size
;
3378 unsigned int dtagcount
;
3380 /* Set up the version definition section. */
3381 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3382 BFD_ASSERT (s
!= NULL
);
3384 /* We may have created additional version definitions if we are
3385 just linking a regular application. */
3386 verdefs
= asvinfo
.verdefs
;
3388 /* Skip anonymous version tag. */
3389 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
3390 verdefs
= verdefs
->next
;
3392 if (verdefs
== NULL
)
3393 _bfd_strip_section_from_output (info
, s
);
3398 struct bfd_elf_version_tree
*t
;
3400 Elf_Internal_Verdef def
;
3401 Elf_Internal_Verdaux defaux
;
3406 /* Make space for the base version. */
3407 size
+= sizeof (Elf_External_Verdef
);
3408 size
+= sizeof (Elf_External_Verdaux
);
3411 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3413 struct bfd_elf_version_deps
*n
;
3415 size
+= sizeof (Elf_External_Verdef
);
3416 size
+= sizeof (Elf_External_Verdaux
);
3419 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3420 size
+= sizeof (Elf_External_Verdaux
);
3423 s
->_raw_size
= size
;
3424 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3425 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3428 /* Fill in the version definition section. */
3432 def
.vd_version
= VER_DEF_CURRENT
;
3433 def
.vd_flags
= VER_FLG_BASE
;
3436 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3437 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3438 + sizeof (Elf_External_Verdaux
));
3440 if (soname_indx
!= (bfd_size_type
) -1)
3442 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3444 def
.vd_hash
= bfd_elf_hash (soname
);
3445 defaux
.vda_name
= soname_indx
;
3452 name
= basename (output_bfd
->filename
);
3453 def
.vd_hash
= bfd_elf_hash (name
);
3454 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3456 if (indx
== (bfd_size_type
) -1)
3458 defaux
.vda_name
= indx
;
3460 defaux
.vda_next
= 0;
3462 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3463 (Elf_External_Verdef
*) p
);
3464 p
+= sizeof (Elf_External_Verdef
);
3465 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3466 (Elf_External_Verdaux
*) p
);
3467 p
+= sizeof (Elf_External_Verdaux
);
3469 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3472 struct bfd_elf_version_deps
*n
;
3473 struct elf_link_hash_entry
*h
;
3474 struct bfd_link_hash_entry
*bh
;
3477 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3480 /* Add a symbol representing this version. */
3482 if (! (_bfd_generic_link_add_one_symbol
3483 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
3484 (bfd_vma
) 0, (const char *) NULL
, FALSE
,
3485 get_elf_backend_data (dynobj
)->collect
, &bh
)))
3487 h
= (struct elf_link_hash_entry
*) bh
;
3488 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
3489 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3490 h
->type
= STT_OBJECT
;
3491 h
->verinfo
.vertree
= t
;
3493 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
3496 def
.vd_version
= VER_DEF_CURRENT
;
3498 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
3499 def
.vd_flags
|= VER_FLG_WEAK
;
3500 def
.vd_ndx
= t
->vernum
+ 1;
3501 def
.vd_cnt
= cdeps
+ 1;
3502 def
.vd_hash
= bfd_elf_hash (t
->name
);
3503 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3504 if (t
->next
!= NULL
)
3505 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3506 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
3510 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3511 (Elf_External_Verdef
*) p
);
3512 p
+= sizeof (Elf_External_Verdef
);
3514 defaux
.vda_name
= h
->dynstr_index
;
3515 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3517 if (t
->deps
== NULL
)
3518 defaux
.vda_next
= 0;
3520 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3521 t
->name_indx
= defaux
.vda_name
;
3523 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3524 (Elf_External_Verdaux
*) p
);
3525 p
+= sizeof (Elf_External_Verdaux
);
3527 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3529 if (n
->version_needed
== NULL
)
3531 /* This can happen if there was an error in the
3533 defaux
.vda_name
= 0;
3537 defaux
.vda_name
= n
->version_needed
->name_indx
;
3538 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3541 if (n
->next
== NULL
)
3542 defaux
.vda_next
= 0;
3544 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3546 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3547 (Elf_External_Verdaux
*) p
);
3548 p
+= sizeof (Elf_External_Verdaux
);
3552 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEF
, (bfd_vma
) 0)
3553 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEFNUM
,
3557 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
3560 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
3562 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS
, info
->flags
))
3569 info
->flags_1
&= ~ (DF_1_INITFIRST
3572 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS_1
,
3577 /* Work out the size of the version reference section. */
3579 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3580 BFD_ASSERT (s
!= NULL
);
3582 struct elf_find_verdep_info sinfo
;
3584 sinfo
.output_bfd
= output_bfd
;
3586 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
3587 if (sinfo
.vers
== 0)
3589 sinfo
.failed
= FALSE
;
3591 elf_link_hash_traverse (elf_hash_table (info
),
3592 elf_link_find_version_dependencies
,
3595 if (elf_tdata (output_bfd
)->verref
== NULL
)
3596 _bfd_strip_section_from_output (info
, s
);
3599 Elf_Internal_Verneed
*t
;
3604 /* Build the version definition section. */
3607 for (t
= elf_tdata (output_bfd
)->verref
;
3611 Elf_Internal_Vernaux
*a
;
3613 size
+= sizeof (Elf_External_Verneed
);
3615 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3616 size
+= sizeof (Elf_External_Vernaux
);
3619 s
->_raw_size
= size
;
3620 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3621 if (s
->contents
== NULL
)
3625 for (t
= elf_tdata (output_bfd
)->verref
;
3630 Elf_Internal_Vernaux
*a
;
3634 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3637 t
->vn_version
= VER_NEED_CURRENT
;
3639 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3640 elf_dt_name (t
->vn_bfd
) != NULL
3641 ? elf_dt_name (t
->vn_bfd
)
3642 : basename (t
->vn_bfd
->filename
),
3644 if (indx
== (bfd_size_type
) -1)
3647 t
->vn_aux
= sizeof (Elf_External_Verneed
);
3648 if (t
->vn_nextref
== NULL
)
3651 t
->vn_next
= (sizeof (Elf_External_Verneed
)
3652 + caux
* sizeof (Elf_External_Vernaux
));
3654 _bfd_elf_swap_verneed_out (output_bfd
, t
,
3655 (Elf_External_Verneed
*) p
);
3656 p
+= sizeof (Elf_External_Verneed
);
3658 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3660 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
3661 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3662 a
->vna_nodename
, FALSE
);
3663 if (indx
== (bfd_size_type
) -1)
3666 if (a
->vna_nextptr
== NULL
)
3669 a
->vna_next
= sizeof (Elf_External_Vernaux
);
3671 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
3672 (Elf_External_Vernaux
*) p
);
3673 p
+= sizeof (Elf_External_Vernaux
);
3677 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEED
,
3679 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEEDNUM
,
3683 elf_tdata (output_bfd
)->cverrefs
= crefs
;
3687 /* Assign dynsym indicies. In a shared library we generate a
3688 section symbol for each output section, which come first.
3689 Next come all of the back-end allocated local dynamic syms,
3690 followed by the rest of the global symbols. */
3692 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3694 /* Work out the size of the symbol version section. */
3695 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3696 BFD_ASSERT (s
!= NULL
);
3697 if (dynsymcount
== 0
3698 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
3700 _bfd_strip_section_from_output (info
, s
);
3701 /* The DYNSYMCOUNT might have changed if we were going to
3702 output a dynamic symbol table entry for S. */
3703 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3707 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
3708 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3709 if (s
->contents
== NULL
)
3712 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERSYM
, (bfd_vma
) 0))
3716 /* Set the size of the .dynsym and .hash sections. We counted
3717 the number of dynamic symbols in elf_link_add_object_symbols.
3718 We will build the contents of .dynsym and .hash when we build
3719 the final symbol table, because until then we do not know the
3720 correct value to give the symbols. We built the .dynstr
3721 section as we went along in elf_link_add_object_symbols. */
3722 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
3723 BFD_ASSERT (s
!= NULL
);
3724 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
3725 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3726 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3729 if (dynsymcount
!= 0)
3731 Elf_Internal_Sym isym
;
3733 /* The first entry in .dynsym is a dummy symbol. */
3740 elf_swap_symbol_out (output_bfd
, &isym
, (PTR
) s
->contents
, (PTR
) 0);
3743 /* Compute the size of the hashing table. As a side effect this
3744 computes the hash values for all the names we export. */
3745 bucketcount
= compute_bucket_count (info
);
3747 s
= bfd_get_section_by_name (dynobj
, ".hash");
3748 BFD_ASSERT (s
!= NULL
);
3749 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
3750 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
3751 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3752 if (s
->contents
== NULL
)
3755 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) bucketcount
,
3757 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) dynsymcount
,
3758 s
->contents
+ hash_entry_size
);
3760 elf_hash_table (info
)->bucketcount
= bucketcount
;
3762 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
3763 BFD_ASSERT (s
!= NULL
);
3765 elf_finalize_dynstr (output_bfd
, info
);
3767 s
->_raw_size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3769 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
3770 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NULL
, (bfd_vma
) 0))
3777 /* This function is used to adjust offsets into .dynstr for
3778 dynamic symbols. This is called via elf_link_hash_traverse. */
3780 static bfd_boolean elf_adjust_dynstr_offsets
3781 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3784 elf_adjust_dynstr_offsets (h
, data
)
3785 struct elf_link_hash_entry
*h
;
3788 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3790 if (h
->root
.type
== bfd_link_hash_warning
)
3791 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3793 if (h
->dynindx
!= -1)
3794 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3798 /* Assign string offsets in .dynstr, update all structures referencing
3802 elf_finalize_dynstr (output_bfd
, info
)
3804 struct bfd_link_info
*info
;
3806 struct elf_link_local_dynamic_entry
*entry
;
3807 struct elf_strtab_hash
*dynstr
= elf_hash_table (info
)->dynstr
;
3808 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
3811 Elf_External_Dyn
*dyncon
, *dynconend
;
3813 _bfd_elf_strtab_finalize (dynstr
);
3814 size
= _bfd_elf_strtab_size (dynstr
);
3816 /* Update all .dynamic entries referencing .dynstr strings. */
3817 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3818 BFD_ASSERT (sdyn
!= NULL
);
3820 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
3821 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
3823 for (; dyncon
< dynconend
; dyncon
++)
3825 Elf_Internal_Dyn dyn
;
3827 elf_swap_dyn_in (dynobj
, dyncon
, & dyn
);
3831 dyn
.d_un
.d_val
= size
;
3832 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3840 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3841 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3848 /* Now update local dynamic symbols. */
3849 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
3850 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3851 entry
->isym
.st_name
);
3853 /* And the rest of dynamic symbols. */
3854 elf_link_hash_traverse (elf_hash_table (info
),
3855 elf_adjust_dynstr_offsets
, dynstr
);
3857 /* Adjust version definitions. */
3858 if (elf_tdata (output_bfd
)->cverdefs
)
3863 Elf_Internal_Verdef def
;
3864 Elf_Internal_Verdaux defaux
;
3866 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3867 p
= (bfd_byte
*) s
->contents
;
3870 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3872 p
+= sizeof (Elf_External_Verdef
);
3873 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3875 _bfd_elf_swap_verdaux_in (output_bfd
,
3876 (Elf_External_Verdaux
*) p
, &defaux
);
3877 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3879 _bfd_elf_swap_verdaux_out (output_bfd
,
3880 &defaux
, (Elf_External_Verdaux
*) p
);
3881 p
+= sizeof (Elf_External_Verdaux
);
3884 while (def
.vd_next
);
3887 /* Adjust version references. */
3888 if (elf_tdata (output_bfd
)->verref
)
3893 Elf_Internal_Verneed need
;
3894 Elf_Internal_Vernaux needaux
;
3896 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3897 p
= (bfd_byte
*) s
->contents
;
3900 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3902 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3903 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3904 (Elf_External_Verneed
*) p
);
3905 p
+= sizeof (Elf_External_Verneed
);
3906 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3908 _bfd_elf_swap_vernaux_in (output_bfd
,
3909 (Elf_External_Vernaux
*) p
, &needaux
);
3910 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3912 _bfd_elf_swap_vernaux_out (output_bfd
,
3914 (Elf_External_Vernaux
*) p
);
3915 p
+= sizeof (Elf_External_Vernaux
);
3918 while (need
.vn_next
);
3924 /* Fix up the flags for a symbol. This handles various cases which
3925 can only be fixed after all the input files are seen. This is
3926 currently called by both adjust_dynamic_symbol and
3927 assign_sym_version, which is unnecessary but perhaps more robust in
3928 the face of future changes. */
3931 elf_fix_symbol_flags (h
, eif
)
3932 struct elf_link_hash_entry
*h
;
3933 struct elf_info_failed
*eif
;
3935 /* If this symbol was mentioned in a non-ELF file, try to set
3936 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3937 permit a non-ELF file to correctly refer to a symbol defined in
3938 an ELF dynamic object. */
3939 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
3941 while (h
->root
.type
== bfd_link_hash_indirect
)
3942 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3944 if (h
->root
.type
!= bfd_link_hash_defined
3945 && h
->root
.type
!= bfd_link_hash_defweak
)
3946 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3947 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3950 if (h
->root
.u
.def
.section
->owner
!= NULL
3951 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3952 == bfd_target_elf_flavour
))
3953 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3954 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3956 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3959 if (h
->dynindx
== -1
3960 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
3961 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
3963 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3972 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3973 was first seen in a non-ELF file. Fortunately, if the symbol
3974 was first seen in an ELF file, we're probably OK unless the
3975 symbol was defined in a non-ELF file. Catch that case here.
3976 FIXME: We're still in trouble if the symbol was first seen in
3977 a dynamic object, and then later in a non-ELF regular object. */
3978 if ((h
->root
.type
== bfd_link_hash_defined
3979 || h
->root
.type
== bfd_link_hash_defweak
)
3980 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3981 && (h
->root
.u
.def
.section
->owner
!= NULL
3982 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3983 != bfd_target_elf_flavour
)
3984 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3985 && (h
->elf_link_hash_flags
3986 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
3987 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3990 /* If this is a final link, and the symbol was defined as a common
3991 symbol in a regular object file, and there was no definition in
3992 any dynamic object, then the linker will have allocated space for
3993 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3994 flag will not have been set. */
3995 if (h
->root
.type
== bfd_link_hash_defined
3996 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3997 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
3998 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3999 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
4000 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4002 /* If -Bsymbolic was used (which means to bind references to global
4003 symbols to the definition within the shared object), and this
4004 symbol was defined in a regular object, then it actually doesn't
4005 need a PLT entry, and we can accomplish that by forcing it local.
4006 Likewise, if the symbol has hidden or internal visibility.
4007 FIXME: It might be that we also do not need a PLT for other
4008 non-hidden visibilities, but we would have to tell that to the
4009 backend specifically; we can't just clear PLT-related data here. */
4010 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
4011 && eif
->info
->shared
4012 && is_elf_hash_table (eif
->info
)
4013 && (eif
->info
->symbolic
4014 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
4015 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
4016 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
4018 struct elf_backend_data
*bed
;
4019 bfd_boolean force_local
;
4021 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
4023 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
4024 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
4025 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
4028 /* If this is a weak defined symbol in a dynamic object, and we know
4029 the real definition in the dynamic object, copy interesting flags
4030 over to the real definition. */
4031 if (h
->weakdef
!= NULL
)
4033 struct elf_link_hash_entry
*weakdef
;
4035 weakdef
= h
->weakdef
;
4036 if (h
->root
.type
== bfd_link_hash_indirect
)
4037 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4039 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4040 || h
->root
.type
== bfd_link_hash_defweak
);
4041 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
4042 || weakdef
->root
.type
== bfd_link_hash_defweak
);
4043 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
4045 /* If the real definition is defined by a regular object file,
4046 don't do anything special. See the longer description in
4047 elf_adjust_dynamic_symbol, below. */
4048 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
4052 struct elf_backend_data
*bed
;
4054 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
4055 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
4062 /* Make the backend pick a good value for a dynamic symbol. This is
4063 called via elf_link_hash_traverse, and also calls itself
4067 elf_adjust_dynamic_symbol (h
, data
)
4068 struct elf_link_hash_entry
*h
;
4071 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
4073 struct elf_backend_data
*bed
;
4075 if (! is_elf_hash_table (eif
->info
))
4078 if (h
->root
.type
== bfd_link_hash_warning
)
4080 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
4081 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
4083 /* When warning symbols are created, they **replace** the "real"
4084 entry in the hash table, thus we never get to see the real
4085 symbol in a hash traversal. So look at it now. */
4086 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4089 /* Ignore indirect symbols. These are added by the versioning code. */
4090 if (h
->root
.type
== bfd_link_hash_indirect
)
4093 /* Fix the symbol flags. */
4094 if (! elf_fix_symbol_flags (h
, eif
))
4097 /* If this symbol does not require a PLT entry, and it is not
4098 defined by a dynamic object, or is not referenced by a regular
4099 object, ignore it. We do have to handle a weak defined symbol,
4100 even if no regular object refers to it, if we decided to add it
4101 to the dynamic symbol table. FIXME: Do we normally need to worry
4102 about symbols which are defined by one dynamic object and
4103 referenced by another one? */
4104 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
4105 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4106 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4107 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
4108 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
4110 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
4114 /* If we've already adjusted this symbol, don't do it again. This
4115 can happen via a recursive call. */
4116 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
4119 /* Don't look at this symbol again. Note that we must set this
4120 after checking the above conditions, because we may look at a
4121 symbol once, decide not to do anything, and then get called
4122 recursively later after REF_REGULAR is set below. */
4123 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
4125 /* If this is a weak definition, and we know a real definition, and
4126 the real symbol is not itself defined by a regular object file,
4127 then get a good value for the real definition. We handle the
4128 real symbol first, for the convenience of the backend routine.
4130 Note that there is a confusing case here. If the real definition
4131 is defined by a regular object file, we don't get the real symbol
4132 from the dynamic object, but we do get the weak symbol. If the
4133 processor backend uses a COPY reloc, then if some routine in the
4134 dynamic object changes the real symbol, we will not see that
4135 change in the corresponding weak symbol. This is the way other
4136 ELF linkers work as well, and seems to be a result of the shared
4139 I will clarify this issue. Most SVR4 shared libraries define the
4140 variable _timezone and define timezone as a weak synonym. The
4141 tzset call changes _timezone. If you write
4142 extern int timezone;
4144 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4145 you might expect that, since timezone is a synonym for _timezone,
4146 the same number will print both times. However, if the processor
4147 backend uses a COPY reloc, then actually timezone will be copied
4148 into your process image, and, since you define _timezone
4149 yourself, _timezone will not. Thus timezone and _timezone will
4150 wind up at different memory locations. The tzset call will set
4151 _timezone, leaving timezone unchanged. */
4153 if (h
->weakdef
!= NULL
)
4155 /* If we get to this point, we know there is an implicit
4156 reference by a regular object file via the weak symbol H.
4157 FIXME: Is this really true? What if the traversal finds
4158 H->WEAKDEF before it finds H? */
4159 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
4161 if (! elf_adjust_dynamic_symbol (h
->weakdef
, (PTR
) eif
))
4165 /* If a symbol has no type and no size and does not require a PLT
4166 entry, then we are probably about to do the wrong thing here: we
4167 are probably going to create a COPY reloc for an empty object.
4168 This case can arise when a shared object is built with assembly
4169 code, and the assembly code fails to set the symbol type. */
4171 && h
->type
== STT_NOTYPE
4172 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
4173 (*_bfd_error_handler
)
4174 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4175 h
->root
.root
.string
);
4177 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
4178 bed
= get_elf_backend_data (dynobj
);
4179 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
4188 /* This routine is used to export all defined symbols into the dynamic
4189 symbol table. It is called via elf_link_hash_traverse. */
4192 elf_export_symbol (h
, data
)
4193 struct elf_link_hash_entry
*h
;
4196 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
4198 /* Ignore indirect symbols. These are added by the versioning code. */
4199 if (h
->root
.type
== bfd_link_hash_indirect
)
4202 if (h
->root
.type
== bfd_link_hash_warning
)
4203 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4205 if (h
->dynindx
== -1
4206 && (h
->elf_link_hash_flags
4207 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
4209 struct bfd_elf_version_tree
*t
;
4210 struct bfd_elf_version_expr
*d
;
4212 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
4214 if (t
->globals
!= NULL
)
4216 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4218 if ((*d
->match
) (d
, h
->root
.root
.string
))
4223 if (t
->locals
!= NULL
)
4225 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4227 if ((*d
->match
) (d
, h
->root
.root
.string
))
4236 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
4247 /* Look through the symbols which are defined in other shared
4248 libraries and referenced here. Update the list of version
4249 dependencies. This will be put into the .gnu.version_r section.
4250 This function is called via elf_link_hash_traverse. */
4253 elf_link_find_version_dependencies (h
, data
)
4254 struct elf_link_hash_entry
*h
;
4257 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
4258 Elf_Internal_Verneed
*t
;
4259 Elf_Internal_Vernaux
*a
;
4262 if (h
->root
.type
== bfd_link_hash_warning
)
4263 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4265 /* We only care about symbols defined in shared objects with version
4267 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4268 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4270 || h
->verinfo
.verdef
== NULL
)
4273 /* See if we already know about this version. */
4274 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
4276 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
4279 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4280 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
4286 /* This is a new version. Add it to tree we are building. */
4291 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4294 rinfo
->failed
= TRUE
;
4298 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
4299 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
4300 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
4304 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4306 /* Note that we are copying a string pointer here, and testing it
4307 above. If bfd_elf_string_from_elf_section is ever changed to
4308 discard the string data when low in memory, this will have to be
4310 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
4312 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
4313 a
->vna_nextptr
= t
->vn_auxptr
;
4315 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
4318 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4325 /* Figure out appropriate versions for all the symbols. We may not
4326 have the version number script until we have read all of the input
4327 files, so until that point we don't know which symbols should be
4328 local. This function is called via elf_link_hash_traverse. */
4331 elf_link_assign_sym_version (h
, data
)
4332 struct elf_link_hash_entry
*h
;
4335 struct elf_assign_sym_version_info
*sinfo
;
4336 struct bfd_link_info
*info
;
4337 struct elf_backend_data
*bed
;
4338 struct elf_info_failed eif
;
4342 sinfo
= (struct elf_assign_sym_version_info
*) data
;
4345 if (h
->root
.type
== bfd_link_hash_warning
)
4346 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4348 /* Fix the symbol flags. */
4351 if (! elf_fix_symbol_flags (h
, &eif
))
4354 sinfo
->failed
= TRUE
;
4358 /* We only need version numbers for symbols defined in regular
4360 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4363 bed
= get_elf_backend_data (sinfo
->output_bfd
);
4364 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4365 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
4367 struct bfd_elf_version_tree
*t
;
4372 /* There are two consecutive ELF_VER_CHR characters if this is
4373 not a hidden symbol. */
4375 if (*p
== ELF_VER_CHR
)
4381 /* If there is no version string, we can just return out. */
4385 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4389 /* Look for the version. If we find it, it is no longer weak. */
4390 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4392 if (strcmp (t
->name
, p
) == 0)
4396 struct bfd_elf_version_expr
*d
;
4398 len
= p
- h
->root
.root
.string
;
4399 alc
= bfd_malloc ((bfd_size_type
) len
);
4402 memcpy (alc
, h
->root
.root
.string
, len
- 1);
4403 alc
[len
- 1] = '\0';
4404 if (alc
[len
- 2] == ELF_VER_CHR
)
4405 alc
[len
- 2] = '\0';
4407 h
->verinfo
.vertree
= t
;
4411 if (t
->globals
!= NULL
)
4413 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4414 if ((*d
->match
) (d
, alc
))
4418 /* See if there is anything to force this symbol to
4420 if (d
== NULL
&& t
->locals
!= NULL
)
4422 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4424 if ((*d
->match
) (d
, alc
))
4426 if (h
->dynindx
!= -1
4428 && ! info
->export_dynamic
)
4430 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4443 /* If we are building an application, we need to create a
4444 version node for this version. */
4445 if (t
== NULL
&& ! info
->shared
)
4447 struct bfd_elf_version_tree
**pp
;
4450 /* If we aren't going to export this symbol, we don't need
4451 to worry about it. */
4452 if (h
->dynindx
== -1)
4456 t
= ((struct bfd_elf_version_tree
*)
4457 bfd_alloc (sinfo
->output_bfd
, amt
));
4460 sinfo
->failed
= TRUE
;
4469 t
->name_indx
= (unsigned int) -1;
4473 /* Don't count anonymous version tag. */
4474 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
4476 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4478 t
->vernum
= version_index
;
4482 h
->verinfo
.vertree
= t
;
4486 /* We could not find the version for a symbol when
4487 generating a shared archive. Return an error. */
4488 (*_bfd_error_handler
)
4489 (_("%s: undefined versioned symbol name %s"),
4490 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
4491 bfd_set_error (bfd_error_bad_value
);
4492 sinfo
->failed
= TRUE
;
4497 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4500 /* If we don't have a version for this symbol, see if we can find
4502 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
4504 struct bfd_elf_version_tree
*t
;
4505 struct bfd_elf_version_tree
*local_ver
;
4506 struct bfd_elf_version_expr
*d
;
4508 /* See if can find what version this symbol is in. If the
4509 symbol is supposed to be local, then don't actually register
4512 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4514 if (t
->globals
!= NULL
)
4516 bfd_boolean matched
;
4519 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4521 if ((*d
->match
) (d
, h
->root
.root
.string
))
4527 /* There is a version without definition. Make
4528 the symbol the default definition for this
4530 h
->verinfo
.vertree
= t
;
4541 /* There is no undefined version for this symbol. Hide the
4543 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4546 if (t
->locals
!= NULL
)
4548 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4550 /* If the match is "*", keep looking for a more
4551 explicit, perhaps even global, match. */
4552 if (d
->pattern
[0] == '*' && d
->pattern
[1] == '\0')
4554 else if ((*d
->match
) (d
, h
->root
.root
.string
))
4566 if (local_ver
!= NULL
)
4568 h
->verinfo
.vertree
= local_ver
;
4569 if (h
->dynindx
!= -1
4571 && ! info
->export_dynamic
)
4573 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4581 /* Final phase of ELF linker. */
4583 /* A structure we use to avoid passing large numbers of arguments. */
4585 struct elf_final_link_info
4587 /* General link information. */
4588 struct bfd_link_info
*info
;
4591 /* Symbol string table. */
4592 struct bfd_strtab_hash
*symstrtab
;
4593 /* .dynsym section. */
4594 asection
*dynsym_sec
;
4595 /* .hash section. */
4597 /* symbol version section (.gnu.version). */
4598 asection
*symver_sec
;
4599 /* first SHF_TLS section (if any). */
4600 asection
*first_tls_sec
;
4601 /* Buffer large enough to hold contents of any section. */
4603 /* Buffer large enough to hold external relocs of any section. */
4604 PTR external_relocs
;
4605 /* Buffer large enough to hold internal relocs of any section. */
4606 Elf_Internal_Rela
*internal_relocs
;
4607 /* Buffer large enough to hold external local symbols of any input
4609 Elf_External_Sym
*external_syms
;
4610 /* And a buffer for symbol section indices. */
4611 Elf_External_Sym_Shndx
*locsym_shndx
;
4612 /* Buffer large enough to hold internal local symbols of any input
4614 Elf_Internal_Sym
*internal_syms
;
4615 /* Array large enough to hold a symbol index for each local symbol
4616 of any input BFD. */
4618 /* Array large enough to hold a section pointer for each local
4619 symbol of any input BFD. */
4620 asection
**sections
;
4621 /* Buffer to hold swapped out symbols. */
4622 Elf_External_Sym
*symbuf
;
4623 /* And one for symbol section indices. */
4624 Elf_External_Sym_Shndx
*symshndxbuf
;
4625 /* Number of swapped out symbols in buffer. */
4626 size_t symbuf_count
;
4627 /* Number of symbols which fit in symbuf. */
4629 /* And same for symshndxbuf. */
4630 size_t shndxbuf_size
;
4633 static bfd_boolean elf_link_output_sym
4634 PARAMS ((struct elf_final_link_info
*, const char *,
4635 Elf_Internal_Sym
*, asection
*));
4636 static bfd_boolean elf_link_flush_output_syms
4637 PARAMS ((struct elf_final_link_info
*));
4638 static bfd_boolean elf_link_output_extsym
4639 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4640 static bfd_boolean elf_link_sec_merge_syms
4641 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4642 static bfd_boolean elf_link_check_versioned_symbol
4643 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
4644 static bfd_boolean elf_link_input_bfd
4645 PARAMS ((struct elf_final_link_info
*, bfd
*));
4646 static bfd_boolean elf_reloc_link_order
4647 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
4648 struct bfd_link_order
*));
4650 /* This struct is used to pass information to elf_link_output_extsym. */
4652 struct elf_outext_info
4655 bfd_boolean localsyms
;
4656 struct elf_final_link_info
*finfo
;
4659 /* Compute the size of, and allocate space for, REL_HDR which is the
4660 section header for a section containing relocations for O. */
4663 elf_link_size_reloc_section (abfd
, rel_hdr
, o
)
4665 Elf_Internal_Shdr
*rel_hdr
;
4668 bfd_size_type reloc_count
;
4669 bfd_size_type num_rel_hashes
;
4671 /* Figure out how many relocations there will be. */
4672 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
4673 reloc_count
= elf_section_data (o
)->rel_count
;
4675 reloc_count
= elf_section_data (o
)->rel_count2
;
4677 num_rel_hashes
= o
->reloc_count
;
4678 if (num_rel_hashes
< reloc_count
)
4679 num_rel_hashes
= reloc_count
;
4681 /* That allows us to calculate the size of the section. */
4682 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
4684 /* The contents field must last into write_object_contents, so we
4685 allocate it with bfd_alloc rather than malloc. Also since we
4686 cannot be sure that the contents will actually be filled in,
4687 we zero the allocated space. */
4688 rel_hdr
->contents
= (PTR
) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
4689 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
4692 /* We only allocate one set of hash entries, so we only do it the
4693 first time we are called. */
4694 if (elf_section_data (o
)->rel_hashes
== NULL
4697 struct elf_link_hash_entry
**p
;
4699 p
= ((struct elf_link_hash_entry
**)
4700 bfd_zmalloc (num_rel_hashes
4701 * sizeof (struct elf_link_hash_entry
*)));
4705 elf_section_data (o
)->rel_hashes
= p
;
4711 /* When performing a relocateable link, the input relocations are
4712 preserved. But, if they reference global symbols, the indices
4713 referenced must be updated. Update all the relocations in
4714 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4717 elf_link_adjust_relocs (abfd
, rel_hdr
, count
, rel_hash
)
4719 Elf_Internal_Shdr
*rel_hdr
;
4721 struct elf_link_hash_entry
**rel_hash
;
4724 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4726 void (*swap_in
) PARAMS ((bfd
*, const bfd_byte
*, Elf_Internal_Rela
*));
4727 void (*swap_out
) PARAMS ((bfd
*, const Elf_Internal_Rela
*, bfd_byte
*));
4729 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4731 swap_in
= bed
->s
->swap_reloc_in
;
4732 swap_out
= bed
->s
->swap_reloc_out
;
4734 else if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
))
4736 swap_in
= bed
->s
->swap_reloca_in
;
4737 swap_out
= bed
->s
->swap_reloca_out
;
4742 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
4745 erela
= rel_hdr
->contents
;
4746 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
4748 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
4751 if (*rel_hash
== NULL
)
4754 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
4756 (*swap_in
) (abfd
, erela
, irela
);
4757 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4758 irela
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4759 ELF_R_TYPE (irela
[j
].r_info
));
4760 (*swap_out
) (abfd
, irela
, erela
);
4764 struct elf_link_sort_rela
4767 enum elf_reloc_type_class type
;
4768 /* We use this as an array of size int_rels_per_ext_rel. */
4769 Elf_Internal_Rela rela
[1];
4773 elf_link_sort_cmp1 (A
, B
)
4777 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4778 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4779 int relativea
, relativeb
;
4781 relativea
= a
->type
== reloc_class_relative
;
4782 relativeb
= b
->type
== reloc_class_relative
;
4784 if (relativea
< relativeb
)
4786 if (relativea
> relativeb
)
4788 if (ELF_R_SYM (a
->rela
->r_info
) < ELF_R_SYM (b
->rela
->r_info
))
4790 if (ELF_R_SYM (a
->rela
->r_info
) > ELF_R_SYM (b
->rela
->r_info
))
4792 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
4794 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
4800 elf_link_sort_cmp2 (A
, B
)
4804 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4805 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4808 if (a
->offset
< b
->offset
)
4810 if (a
->offset
> b
->offset
)
4812 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
4813 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
4818 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
4820 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
4826 elf_link_sort_relocs (abfd
, info
, psec
)
4828 struct bfd_link_info
*info
;
4831 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4832 asection
*reldyn
, *o
;
4833 bfd_size_type count
, size
;
4834 size_t i
, ret
, sort_elt
, ext_size
;
4835 bfd_byte
*sort
, *s_non_relative
, *p
;
4836 struct elf_link_sort_rela
*sq
;
4837 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4838 int i2e
= bed
->s
->int_rels_per_ext_rel
;
4839 void (*swap_in
) PARAMS ((bfd
*, const bfd_byte
*, Elf_Internal_Rela
*));
4840 void (*swap_out
) PARAMS ((bfd
*, const Elf_Internal_Rela
*, bfd_byte
*));
4842 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
4843 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4845 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
4846 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4848 ext_size
= sizeof (Elf_External_Rel
);
4849 swap_in
= bed
->s
->swap_reloc_in
;
4850 swap_out
= bed
->s
->swap_reloc_out
;
4854 ext_size
= sizeof (Elf_External_Rela
);
4855 swap_in
= bed
->s
->swap_reloca_in
;
4856 swap_out
= bed
->s
->swap_reloca_out
;
4858 count
= reldyn
->_raw_size
/ ext_size
;
4861 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4862 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4863 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4864 && o
->output_section
== reldyn
)
4865 size
+= o
->_raw_size
;
4867 if (size
!= reldyn
->_raw_size
)
4870 sort_elt
= (sizeof (struct elf_link_sort_rela
)
4871 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
4872 sort
= bfd_zmalloc (sort_elt
* count
);
4875 (*info
->callbacks
->warning
)
4876 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0,
4881 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4882 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4883 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4884 && o
->output_section
== reldyn
)
4886 bfd_byte
*erel
, *erelend
;
4889 erelend
= o
->contents
+ o
->_raw_size
;
4890 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
4891 while (erel
< erelend
)
4893 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
4894 (*swap_in
) (abfd
, erel
, s
->rela
);
4895 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
4901 qsort (sort
, (size_t) count
, sort_elt
, elf_link_sort_cmp1
);
4903 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
4905 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
4906 if (s
->type
!= reloc_class_relative
)
4912 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
4913 for (; i
< count
; i
++, p
+= sort_elt
)
4915 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
4916 if (ELF_R_SYM (sp
->rela
->r_info
) != ELF_R_SYM (sq
->rela
->r_info
))
4918 sp
->offset
= sq
->rela
->r_offset
;
4921 qsort (s_non_relative
, (size_t) count
- ret
, sort_elt
, elf_link_sort_cmp2
);
4923 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4924 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4925 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4926 && o
->output_section
== reldyn
)
4928 bfd_byte
*erel
, *erelend
;
4931 erelend
= o
->contents
+ o
->_raw_size
;
4932 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
4933 while (erel
< erelend
)
4935 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
4936 (*swap_out
) (abfd
, s
->rela
, erel
);
4947 /* Do the final step of an ELF link. */
4950 elf_bfd_final_link (abfd
, info
)
4952 struct bfd_link_info
*info
;
4954 bfd_boolean dynamic
;
4955 bfd_boolean emit_relocs
;
4957 struct elf_final_link_info finfo
;
4958 register asection
*o
;
4959 register struct bfd_link_order
*p
;
4961 bfd_size_type max_contents_size
;
4962 bfd_size_type max_external_reloc_size
;
4963 bfd_size_type max_internal_reloc_count
;
4964 bfd_size_type max_sym_count
;
4965 bfd_size_type max_sym_shndx_count
;
4967 Elf_Internal_Sym elfsym
;
4969 Elf_Internal_Shdr
*symtab_hdr
;
4970 Elf_Internal_Shdr
*symtab_shndx_hdr
;
4971 Elf_Internal_Shdr
*symstrtab_hdr
;
4972 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4973 struct elf_outext_info eoinfo
;
4975 size_t relativecount
= 0;
4976 asection
*reldyn
= 0;
4979 if (! is_elf_hash_table (info
))
4983 abfd
->flags
|= DYNAMIC
;
4985 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
4986 dynobj
= elf_hash_table (info
)->dynobj
;
4988 emit_relocs
= (info
->relocateable
4989 || info
->emitrelocations
4990 || bed
->elf_backend_emit_relocs
);
4993 finfo
.output_bfd
= abfd
;
4994 finfo
.symstrtab
= elf_stringtab_init ();
4995 if (finfo
.symstrtab
== NULL
)
5000 finfo
.dynsym_sec
= NULL
;
5001 finfo
.hash_sec
= NULL
;
5002 finfo
.symver_sec
= NULL
;
5006 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
5007 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
5008 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
5009 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5010 /* Note that it is OK if symver_sec is NULL. */
5013 finfo
.contents
= NULL
;
5014 finfo
.external_relocs
= NULL
;
5015 finfo
.internal_relocs
= NULL
;
5016 finfo
.external_syms
= NULL
;
5017 finfo
.locsym_shndx
= NULL
;
5018 finfo
.internal_syms
= NULL
;
5019 finfo
.indices
= NULL
;
5020 finfo
.sections
= NULL
;
5021 finfo
.symbuf
= NULL
;
5022 finfo
.symshndxbuf
= NULL
;
5023 finfo
.symbuf_count
= 0;
5024 finfo
.shndxbuf_size
= 0;
5025 finfo
.first_tls_sec
= NULL
;
5026 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
5027 if ((o
->flags
& SEC_THREAD_LOCAL
) != 0
5028 && (o
->flags
& SEC_LOAD
) != 0)
5030 finfo
.first_tls_sec
= o
;
5034 /* Count up the number of relocations we will output for each output
5035 section, so that we know the sizes of the reloc sections. We
5036 also figure out some maximum sizes. */
5037 max_contents_size
= 0;
5038 max_external_reloc_size
= 0;
5039 max_internal_reloc_count
= 0;
5041 max_sym_shndx_count
= 0;
5043 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
5045 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
5048 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5050 unsigned int reloc_count
= 0;
5051 struct bfd_elf_section_data
*esdi
= NULL
;
5052 unsigned int *rel_count1
;
5054 if (p
->type
== bfd_section_reloc_link_order
5055 || p
->type
== bfd_symbol_reloc_link_order
)
5057 else if (p
->type
== bfd_indirect_link_order
)
5061 sec
= p
->u
.indirect
.section
;
5062 esdi
= elf_section_data (sec
);
5064 /* Mark all sections which are to be included in the
5065 link. This will normally be every section. We need
5066 to do this so that we can identify any sections which
5067 the linker has decided to not include. */
5068 sec
->linker_mark
= TRUE
;
5070 if (sec
->flags
& SEC_MERGE
)
5073 if (info
->relocateable
|| info
->emitrelocations
)
5074 reloc_count
= sec
->reloc_count
;
5075 else if (bed
->elf_backend_count_relocs
)
5077 Elf_Internal_Rela
* relocs
;
5079 relocs
= (NAME(_bfd_elf
,link_read_relocs
)
5080 (abfd
, sec
, (PTR
) NULL
,
5081 (Elf_Internal_Rela
*) NULL
, info
->keep_memory
));
5083 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
5085 if (elf_section_data (o
)->relocs
!= relocs
)
5089 if (sec
->_raw_size
> max_contents_size
)
5090 max_contents_size
= sec
->_raw_size
;
5091 if (sec
->_cooked_size
> max_contents_size
)
5092 max_contents_size
= sec
->_cooked_size
;
5094 /* We are interested in just local symbols, not all
5096 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
5097 && (sec
->owner
->flags
& DYNAMIC
) == 0)
5101 if (elf_bad_symtab (sec
->owner
))
5102 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
5103 / sizeof (Elf_External_Sym
));
5105 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
5107 if (sym_count
> max_sym_count
)
5108 max_sym_count
= sym_count
;
5110 if (sym_count
> max_sym_shndx_count
5111 && elf_symtab_shndx (sec
->owner
) != 0)
5112 max_sym_shndx_count
= sym_count
;
5114 if ((sec
->flags
& SEC_RELOC
) != 0)
5118 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
5119 if (ext_size
> max_external_reloc_size
)
5120 max_external_reloc_size
= ext_size
;
5121 if (sec
->reloc_count
> max_internal_reloc_count
)
5122 max_internal_reloc_count
= sec
->reloc_count
;
5127 if (reloc_count
== 0)
5130 o
->reloc_count
+= reloc_count
;
5132 /* MIPS may have a mix of REL and RELA relocs on sections.
5133 To support this curious ABI we keep reloc counts in
5134 elf_section_data too. We must be careful to add the
5135 relocations from the input section to the right output
5136 count. FIXME: Get rid of one count. We have
5137 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
5138 rel_count1
= &esdo
->rel_count
;
5141 bfd_boolean same_size
;
5142 bfd_size_type entsize1
;
5144 entsize1
= esdi
->rel_hdr
.sh_entsize
;
5145 BFD_ASSERT (entsize1
== sizeof (Elf_External_Rel
)
5146 || entsize1
== sizeof (Elf_External_Rela
));
5147 same_size
= (!o
->use_rela_p
5148 == (entsize1
== sizeof (Elf_External_Rel
)));
5151 rel_count1
= &esdo
->rel_count2
;
5153 if (esdi
->rel_hdr2
!= NULL
)
5155 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
5156 unsigned int alt_count
;
5157 unsigned int *rel_count2
;
5159 BFD_ASSERT (entsize2
!= entsize1
5160 && (entsize2
== sizeof (Elf_External_Rel
)
5161 || entsize2
== sizeof (Elf_External_Rela
)));
5163 rel_count2
= &esdo
->rel_count2
;
5165 rel_count2
= &esdo
->rel_count
;
5167 /* The following is probably too simplistic if the
5168 backend counts output relocs unusually. */
5169 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
5170 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
5171 *rel_count2
+= alt_count
;
5172 reloc_count
-= alt_count
;
5175 *rel_count1
+= reloc_count
;
5178 if (o
->reloc_count
> 0)
5179 o
->flags
|= SEC_RELOC
;
5182 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5183 set it (this is probably a bug) and if it is set
5184 assign_section_numbers will create a reloc section. */
5185 o
->flags
&=~ SEC_RELOC
;
5188 /* If the SEC_ALLOC flag is not set, force the section VMA to
5189 zero. This is done in elf_fake_sections as well, but forcing
5190 the VMA to 0 here will ensure that relocs against these
5191 sections are handled correctly. */
5192 if ((o
->flags
& SEC_ALLOC
) == 0
5193 && ! o
->user_set_vma
)
5197 if (! info
->relocateable
&& merged
)
5198 elf_link_hash_traverse (elf_hash_table (info
),
5199 elf_link_sec_merge_syms
, (PTR
) abfd
);
5201 /* Figure out the file positions for everything but the symbol table
5202 and the relocs. We set symcount to force assign_section_numbers
5203 to create a symbol table. */
5204 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
5205 BFD_ASSERT (! abfd
->output_has_begun
);
5206 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
5209 /* That created the reloc sections. Set their sizes, and assign
5210 them file positions, and allocate some buffers. */
5211 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5213 if ((o
->flags
& SEC_RELOC
) != 0)
5215 if (!elf_link_size_reloc_section (abfd
,
5216 &elf_section_data (o
)->rel_hdr
,
5220 if (elf_section_data (o
)->rel_hdr2
5221 && !elf_link_size_reloc_section (abfd
,
5222 elf_section_data (o
)->rel_hdr2
,
5227 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5228 to count upwards while actually outputting the relocations. */
5229 elf_section_data (o
)->rel_count
= 0;
5230 elf_section_data (o
)->rel_count2
= 0;
5233 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5235 /* We have now assigned file positions for all the sections except
5236 .symtab and .strtab. We start the .symtab section at the current
5237 file position, and write directly to it. We build the .strtab
5238 section in memory. */
5239 bfd_get_symcount (abfd
) = 0;
5240 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5241 /* sh_name is set in prep_headers. */
5242 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5243 /* sh_flags, sh_addr and sh_size all start off zero. */
5244 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
5245 /* sh_link is set in assign_section_numbers. */
5246 /* sh_info is set below. */
5247 /* sh_offset is set just below. */
5248 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
5250 off
= elf_tdata (abfd
)->next_file_pos
;
5251 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
5253 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5254 incorrect. We do not yet know the size of the .symtab section.
5255 We correct next_file_pos below, after we do know the size. */
5257 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5258 continuously seeking to the right position in the file. */
5259 if (! info
->keep_memory
|| max_sym_count
< 20)
5260 finfo
.symbuf_size
= 20;
5262 finfo
.symbuf_size
= max_sym_count
;
5263 amt
= finfo
.symbuf_size
;
5264 amt
*= sizeof (Elf_External_Sym
);
5265 finfo
.symbuf
= (Elf_External_Sym
*) bfd_malloc (amt
);
5266 if (finfo
.symbuf
== NULL
)
5268 if (elf_numsections (abfd
) > SHN_LORESERVE
)
5270 /* Wild guess at number of output symbols. realloc'd as needed. */
5271 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
5272 finfo
.shndxbuf_size
= amt
;
5273 amt
*= sizeof (Elf_External_Sym_Shndx
);
5274 finfo
.symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
5275 if (finfo
.symshndxbuf
== NULL
)
5279 /* Start writing out the symbol table. The first symbol is always a
5281 if (info
->strip
!= strip_all
5284 elfsym
.st_value
= 0;
5287 elfsym
.st_other
= 0;
5288 elfsym
.st_shndx
= SHN_UNDEF
;
5289 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5290 &elfsym
, bfd_und_section_ptr
))
5295 /* Some standard ELF linkers do this, but we don't because it causes
5296 bootstrap comparison failures. */
5297 /* Output a file symbol for the output file as the second symbol.
5298 We output this even if we are discarding local symbols, although
5299 I'm not sure if this is correct. */
5300 elfsym
.st_value
= 0;
5302 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5303 elfsym
.st_other
= 0;
5304 elfsym
.st_shndx
= SHN_ABS
;
5305 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
5306 &elfsym
, bfd_abs_section_ptr
))
5310 /* Output a symbol for each section. We output these even if we are
5311 discarding local symbols, since they are used for relocs. These
5312 symbols have no names. We store the index of each one in the
5313 index field of the section, so that we can find it again when
5314 outputting relocs. */
5315 if (info
->strip
!= strip_all
5319 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5320 elfsym
.st_other
= 0;
5321 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5323 o
= section_from_elf_index (abfd
, i
);
5325 o
->target_index
= bfd_get_symcount (abfd
);
5326 elfsym
.st_shndx
= i
;
5327 if (info
->relocateable
|| o
== NULL
)
5328 elfsym
.st_value
= 0;
5330 elfsym
.st_value
= o
->vma
;
5331 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5334 if (i
== SHN_LORESERVE
- 1)
5335 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
5339 /* Allocate some memory to hold information read in from the input
5341 if (max_contents_size
!= 0)
5343 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
5344 if (finfo
.contents
== NULL
)
5348 if (max_external_reloc_size
!= 0)
5350 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
5351 if (finfo
.external_relocs
== NULL
)
5355 if (max_internal_reloc_count
!= 0)
5357 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5358 amt
*= sizeof (Elf_Internal_Rela
);
5359 finfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
5360 if (finfo
.internal_relocs
== NULL
)
5364 if (max_sym_count
!= 0)
5366 amt
= max_sym_count
* sizeof (Elf_External_Sym
);
5367 finfo
.external_syms
= (Elf_External_Sym
*) bfd_malloc (amt
);
5368 if (finfo
.external_syms
== NULL
)
5371 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
5372 finfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
5373 if (finfo
.internal_syms
== NULL
)
5376 amt
= max_sym_count
* sizeof (long);
5377 finfo
.indices
= (long *) bfd_malloc (amt
);
5378 if (finfo
.indices
== NULL
)
5381 amt
= max_sym_count
* sizeof (asection
*);
5382 finfo
.sections
= (asection
**) bfd_malloc (amt
);
5383 if (finfo
.sections
== NULL
)
5387 if (max_sym_shndx_count
!= 0)
5389 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
5390 finfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
5391 if (finfo
.locsym_shndx
== NULL
)
5395 if (finfo
.first_tls_sec
)
5397 unsigned int align
= 0;
5398 bfd_vma base
= finfo
.first_tls_sec
->vma
, end
= 0;
5401 for (sec
= finfo
.first_tls_sec
;
5402 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
5405 bfd_vma size
= sec
->_raw_size
;
5407 if (bfd_get_section_alignment (abfd
, sec
) > align
)
5408 align
= bfd_get_section_alignment (abfd
, sec
);
5409 if (sec
->_raw_size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
5411 struct bfd_link_order
*o
;
5414 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
5415 if (size
< o
->offset
+ o
->size
)
5416 size
= o
->offset
+ o
->size
;
5418 end
= sec
->vma
+ size
;
5420 elf_hash_table (info
)->tls_segment
5421 = bfd_zalloc (abfd
, sizeof (struct elf_link_tls_segment
));
5422 if (elf_hash_table (info
)->tls_segment
== NULL
)
5424 elf_hash_table (info
)->tls_segment
->start
= base
;
5425 elf_hash_table (info
)->tls_segment
->size
= end
- base
;
5426 elf_hash_table (info
)->tls_segment
->align
= align
;
5429 /* Since ELF permits relocations to be against local symbols, we
5430 must have the local symbols available when we do the relocations.
5431 Since we would rather only read the local symbols once, and we
5432 would rather not keep them in memory, we handle all the
5433 relocations for a single input file at the same time.
5435 Unfortunately, there is no way to know the total number of local
5436 symbols until we have seen all of them, and the local symbol
5437 indices precede the global symbol indices. This means that when
5438 we are generating relocateable output, and we see a reloc against
5439 a global symbol, we can not know the symbol index until we have
5440 finished examining all the local symbols to see which ones we are
5441 going to output. To deal with this, we keep the relocations in
5442 memory, and don't output them until the end of the link. This is
5443 an unfortunate waste of memory, but I don't see a good way around
5444 it. Fortunately, it only happens when performing a relocateable
5445 link, which is not the common case. FIXME: If keep_memory is set
5446 we could write the relocs out and then read them again; I don't
5447 know how bad the memory loss will be. */
5449 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5450 sub
->output_has_begun
= FALSE
;
5451 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5453 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5455 if (p
->type
== bfd_indirect_link_order
5456 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
5457 == bfd_target_elf_flavour
)
5458 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
5460 if (! sub
->output_has_begun
)
5462 if (! elf_link_input_bfd (&finfo
, sub
))
5464 sub
->output_has_begun
= TRUE
;
5467 else if (p
->type
== bfd_section_reloc_link_order
5468 || p
->type
== bfd_symbol_reloc_link_order
)
5470 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
5475 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
5481 /* Output any global symbols that got converted to local in a
5482 version script or due to symbol visibility. We do this in a
5483 separate step since ELF requires all local symbols to appear
5484 prior to any global symbols. FIXME: We should only do this if
5485 some global symbols were, in fact, converted to become local.
5486 FIXME: Will this work correctly with the Irix 5 linker? */
5487 eoinfo
.failed
= FALSE
;
5488 eoinfo
.finfo
= &finfo
;
5489 eoinfo
.localsyms
= TRUE
;
5490 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5495 /* That wrote out all the local symbols. Finish up the symbol table
5496 with the global symbols. Even if we want to strip everything we
5497 can, we still need to deal with those global symbols that got
5498 converted to local in a version script. */
5500 /* The sh_info field records the index of the first non local symbol. */
5501 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
5504 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
5506 Elf_Internal_Sym sym
;
5507 Elf_External_Sym
*dynsym
=
5508 (Elf_External_Sym
*) finfo
.dynsym_sec
->contents
;
5509 long last_local
= 0;
5511 /* Write out the section symbols for the output sections. */
5518 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5521 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5524 Elf_External_Sym
*dest
;
5526 indx
= elf_section_data (s
)->this_idx
;
5527 BFD_ASSERT (indx
> 0);
5528 sym
.st_shndx
= indx
;
5529 sym
.st_value
= s
->vma
;
5530 dest
= dynsym
+ elf_section_data (s
)->dynindx
;
5531 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5534 last_local
= bfd_count_sections (abfd
);
5537 /* Write out the local dynsyms. */
5538 if (elf_hash_table (info
)->dynlocal
)
5540 struct elf_link_local_dynamic_entry
*e
;
5541 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
5544 Elf_External_Sym
*dest
;
5546 sym
.st_size
= e
->isym
.st_size
;
5547 sym
.st_other
= e
->isym
.st_other
;
5549 /* Copy the internal symbol as is.
5550 Note that we saved a word of storage and overwrote
5551 the original st_name with the dynstr_index. */
5554 if (e
->isym
.st_shndx
!= SHN_UNDEF
5555 && (e
->isym
.st_shndx
< SHN_LORESERVE
5556 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
5558 s
= bfd_section_from_elf_index (e
->input_bfd
,
5562 elf_section_data (s
->output_section
)->this_idx
;
5563 sym
.st_value
= (s
->output_section
->vma
5565 + e
->isym
.st_value
);
5568 if (last_local
< e
->dynindx
)
5569 last_local
= e
->dynindx
;
5571 dest
= dynsym
+ e
->dynindx
;
5572 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5576 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
5580 /* We get the global symbols from the hash table. */
5581 eoinfo
.failed
= FALSE
;
5582 eoinfo
.localsyms
= FALSE
;
5583 eoinfo
.finfo
= &finfo
;
5584 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5589 /* If backend needs to output some symbols not present in the hash
5590 table, do it now. */
5591 if (bed
->elf_backend_output_arch_syms
)
5593 typedef bfd_boolean (*out_sym_func
)
5594 PARAMS ((PTR
, const char *, Elf_Internal_Sym
*, asection
*));
5596 if (! ((*bed
->elf_backend_output_arch_syms
)
5597 (abfd
, info
, (PTR
) &finfo
, (out_sym_func
) elf_link_output_sym
)))
5601 /* Flush all symbols to the file. */
5602 if (! elf_link_flush_output_syms (&finfo
))
5605 /* Now we know the size of the symtab section. */
5606 off
+= symtab_hdr
->sh_size
;
5608 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
5609 if (symtab_shndx_hdr
->sh_name
!= 0)
5611 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
5612 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
5613 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
5614 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
5615 symtab_shndx_hdr
->sh_size
= amt
;
5617 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
5620 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
5621 || (bfd_bwrite ((PTR
) finfo
.symshndxbuf
, amt
, abfd
) != amt
))
5626 /* Finish up and write out the symbol string table (.strtab)
5628 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5629 /* sh_name was set in prep_headers. */
5630 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5631 symstrtab_hdr
->sh_flags
= 0;
5632 symstrtab_hdr
->sh_addr
= 0;
5633 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
5634 symstrtab_hdr
->sh_entsize
= 0;
5635 symstrtab_hdr
->sh_link
= 0;
5636 symstrtab_hdr
->sh_info
= 0;
5637 /* sh_offset is set just below. */
5638 symstrtab_hdr
->sh_addralign
= 1;
5640 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
5641 elf_tdata (abfd
)->next_file_pos
= off
;
5643 if (bfd_get_symcount (abfd
) > 0)
5645 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
5646 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
5650 /* Adjust the relocs to have the correct symbol indices. */
5651 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5653 if ((o
->flags
& SEC_RELOC
) == 0)
5656 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
5657 elf_section_data (o
)->rel_count
,
5658 elf_section_data (o
)->rel_hashes
);
5659 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
5660 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
5661 elf_section_data (o
)->rel_count2
,
5662 (elf_section_data (o
)->rel_hashes
5663 + elf_section_data (o
)->rel_count
));
5665 /* Set the reloc_count field to 0 to prevent write_relocs from
5666 trying to swap the relocs out itself. */
5670 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
5671 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
5673 /* If we are linking against a dynamic object, or generating a
5674 shared library, finish up the dynamic linking information. */
5677 Elf_External_Dyn
*dyncon
, *dynconend
;
5679 /* Fix up .dynamic entries. */
5680 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
5681 BFD_ASSERT (o
!= NULL
);
5683 dyncon
= (Elf_External_Dyn
*) o
->contents
;
5684 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
5685 for (; dyncon
< dynconend
; dyncon
++)
5687 Elf_Internal_Dyn dyn
;
5691 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
5698 if (relativecount
> 0 && dyncon
+ 1 < dynconend
)
5700 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
5702 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
5703 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
5706 if (dyn
.d_tag
!= DT_NULL
)
5708 dyn
.d_un
.d_val
= relativecount
;
5709 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5715 name
= info
->init_function
;
5718 name
= info
->fini_function
;
5721 struct elf_link_hash_entry
*h
;
5723 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
5724 FALSE
, FALSE
, TRUE
);
5726 && (h
->root
.type
== bfd_link_hash_defined
5727 || h
->root
.type
== bfd_link_hash_defweak
))
5729 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
5730 o
= h
->root
.u
.def
.section
;
5731 if (o
->output_section
!= NULL
)
5732 dyn
.d_un
.d_val
+= (o
->output_section
->vma
5733 + o
->output_offset
);
5736 /* The symbol is imported from another shared
5737 library and does not apply to this one. */
5741 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5746 case DT_PREINIT_ARRAYSZ
:
5747 name
= ".preinit_array";
5749 case DT_INIT_ARRAYSZ
:
5750 name
= ".init_array";
5752 case DT_FINI_ARRAYSZ
:
5753 name
= ".fini_array";
5755 o
= bfd_get_section_by_name (abfd
, name
);
5758 (*_bfd_error_handler
)
5759 (_("%s: could not find output section %s"),
5760 bfd_get_filename (abfd
), name
);
5763 if (o
->_raw_size
== 0)
5764 (*_bfd_error_handler
)
5765 (_("warning: %s section has zero size"), name
);
5766 dyn
.d_un
.d_val
= o
->_raw_size
;
5767 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5770 case DT_PREINIT_ARRAY
:
5771 name
= ".preinit_array";
5774 name
= ".init_array";
5777 name
= ".fini_array";
5790 name
= ".gnu.version_d";
5793 name
= ".gnu.version_r";
5796 name
= ".gnu.version";
5798 o
= bfd_get_section_by_name (abfd
, name
);
5801 (*_bfd_error_handler
)
5802 (_("%s: could not find output section %s"),
5803 bfd_get_filename (abfd
), name
);
5806 dyn
.d_un
.d_ptr
= o
->vma
;
5807 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5814 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
5819 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5821 Elf_Internal_Shdr
*hdr
;
5823 hdr
= elf_elfsections (abfd
)[i
];
5824 if (hdr
->sh_type
== type
5825 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
5827 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
5828 dyn
.d_un
.d_val
+= hdr
->sh_size
;
5831 if (dyn
.d_un
.d_val
== 0
5832 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
5833 dyn
.d_un
.d_val
= hdr
->sh_addr
;
5837 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5843 /* If we have created any dynamic sections, then output them. */
5846 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
5849 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
5851 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5852 || o
->_raw_size
== 0
5853 || o
->output_section
== bfd_abs_section_ptr
)
5855 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
5857 /* At this point, we are only interested in sections
5858 created by elf_link_create_dynamic_sections. */
5861 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
5863 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
5865 if (! bfd_set_section_contents (abfd
, o
->output_section
,
5867 (file_ptr
) o
->output_offset
,
5873 /* The contents of the .dynstr section are actually in a
5875 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
5876 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
5877 || ! _bfd_elf_strtab_emit (abfd
,
5878 elf_hash_table (info
)->dynstr
))
5884 if (info
->relocateable
)
5886 bfd_boolean failed
= FALSE
;
5888 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
5893 /* If we have optimized stabs strings, output them. */
5894 if (elf_hash_table (info
)->stab_info
!= NULL
)
5896 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
5900 if (info
->eh_frame_hdr
)
5902 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
5906 if (finfo
.symstrtab
!= NULL
)
5907 _bfd_stringtab_free (finfo
.symstrtab
);
5908 if (finfo
.contents
!= NULL
)
5909 free (finfo
.contents
);
5910 if (finfo
.external_relocs
!= NULL
)
5911 free (finfo
.external_relocs
);
5912 if (finfo
.internal_relocs
!= NULL
)
5913 free (finfo
.internal_relocs
);
5914 if (finfo
.external_syms
!= NULL
)
5915 free (finfo
.external_syms
);
5916 if (finfo
.locsym_shndx
!= NULL
)
5917 free (finfo
.locsym_shndx
);
5918 if (finfo
.internal_syms
!= NULL
)
5919 free (finfo
.internal_syms
);
5920 if (finfo
.indices
!= NULL
)
5921 free (finfo
.indices
);
5922 if (finfo
.sections
!= NULL
)
5923 free (finfo
.sections
);
5924 if (finfo
.symbuf
!= NULL
)
5925 free (finfo
.symbuf
);
5926 if (finfo
.symshndxbuf
!= NULL
)
5927 free (finfo
.symshndxbuf
);
5928 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5930 if ((o
->flags
& SEC_RELOC
) != 0
5931 && elf_section_data (o
)->rel_hashes
!= NULL
)
5932 free (elf_section_data (o
)->rel_hashes
);
5935 elf_tdata (abfd
)->linker
= TRUE
;
5940 if (finfo
.symstrtab
!= NULL
)
5941 _bfd_stringtab_free (finfo
.symstrtab
);
5942 if (finfo
.contents
!= NULL
)
5943 free (finfo
.contents
);
5944 if (finfo
.external_relocs
!= NULL
)
5945 free (finfo
.external_relocs
);
5946 if (finfo
.internal_relocs
!= NULL
)
5947 free (finfo
.internal_relocs
);
5948 if (finfo
.external_syms
!= NULL
)
5949 free (finfo
.external_syms
);
5950 if (finfo
.locsym_shndx
!= NULL
)
5951 free (finfo
.locsym_shndx
);
5952 if (finfo
.internal_syms
!= NULL
)
5953 free (finfo
.internal_syms
);
5954 if (finfo
.indices
!= NULL
)
5955 free (finfo
.indices
);
5956 if (finfo
.sections
!= NULL
)
5957 free (finfo
.sections
);
5958 if (finfo
.symbuf
!= NULL
)
5959 free (finfo
.symbuf
);
5960 if (finfo
.symshndxbuf
!= NULL
)
5961 free (finfo
.symshndxbuf
);
5962 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5964 if ((o
->flags
& SEC_RELOC
) != 0
5965 && elf_section_data (o
)->rel_hashes
!= NULL
)
5966 free (elf_section_data (o
)->rel_hashes
);
5972 /* Add a symbol to the output symbol table. */
5975 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
5976 struct elf_final_link_info
*finfo
;
5978 Elf_Internal_Sym
*elfsym
;
5979 asection
*input_sec
;
5981 Elf_External_Sym
*dest
;
5982 Elf_External_Sym_Shndx
*destshndx
;
5983 bfd_boolean (*output_symbol_hook
)
5984 PARAMS ((bfd
*, struct bfd_link_info
*info
, const char *,
5985 Elf_Internal_Sym
*, asection
*));
5987 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
5988 elf_backend_link_output_symbol_hook
;
5989 if (output_symbol_hook
!= NULL
)
5991 if (! ((*output_symbol_hook
)
5992 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
5996 if (name
== (const char *) NULL
|| *name
== '\0')
5997 elfsym
->st_name
= 0;
5998 else if (input_sec
->flags
& SEC_EXCLUDE
)
5999 elfsym
->st_name
= 0;
6002 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
6004 if (elfsym
->st_name
== (unsigned long) -1)
6008 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
6010 if (! elf_link_flush_output_syms (finfo
))
6014 dest
= finfo
->symbuf
+ finfo
->symbuf_count
;
6015 destshndx
= finfo
->symshndxbuf
;
6016 if (destshndx
!= NULL
)
6018 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
6022 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
6023 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
6024 if (destshndx
== NULL
)
6026 memset ((char *) destshndx
+ amt
, 0, amt
);
6027 finfo
->shndxbuf_size
*= 2;
6029 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
6032 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
, (PTR
) dest
, (PTR
) destshndx
);
6033 finfo
->symbuf_count
+= 1;
6034 bfd_get_symcount (finfo
->output_bfd
) += 1;
6039 /* Flush the output symbols to the file. */
6042 elf_link_flush_output_syms (finfo
)
6043 struct elf_final_link_info
*finfo
;
6045 if (finfo
->symbuf_count
> 0)
6047 Elf_Internal_Shdr
*hdr
;
6051 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6052 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6053 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
6054 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6055 || bfd_bwrite ((PTR
) finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6058 hdr
->sh_size
+= amt
;
6059 finfo
->symbuf_count
= 0;
6065 /* Adjust all external symbols pointing into SEC_MERGE sections
6066 to reflect the object merging within the sections. */
6069 elf_link_sec_merge_syms (h
, data
)
6070 struct elf_link_hash_entry
*h
;
6075 if (h
->root
.type
== bfd_link_hash_warning
)
6076 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6078 if ((h
->root
.type
== bfd_link_hash_defined
6079 || h
->root
.type
== bfd_link_hash_defweak
)
6080 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
6081 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
6083 bfd
*output_bfd
= (bfd
*) data
;
6085 h
->root
.u
.def
.value
=
6086 _bfd_merged_section_offset (output_bfd
,
6087 &h
->root
.u
.def
.section
,
6088 elf_section_data (sec
)->sec_info
,
6089 h
->root
.u
.def
.value
, (bfd_vma
) 0);
6095 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6096 allowing an unsatisfied unversioned symbol in the DSO to match a
6097 versioned symbol that would normally require an explicit version. */
6100 elf_link_check_versioned_symbol (info
, h
)
6101 struct bfd_link_info
*info
;
6102 struct elf_link_hash_entry
*h
;
6104 bfd
*undef_bfd
= h
->root
.u
.undef
.abfd
;
6105 struct elf_link_loaded_list
*loaded
;
6107 if ((undef_bfd
->flags
& DYNAMIC
) == 0
6108 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
6109 || elf_dt_soname (h
->root
.u
.undef
.abfd
) == NULL
)
6112 for (loaded
= elf_hash_table (info
)->loaded
;
6114 loaded
= loaded
->next
)
6117 Elf_Internal_Shdr
*hdr
;
6118 bfd_size_type symcount
;
6119 bfd_size_type extsymcount
;
6120 bfd_size_type extsymoff
;
6121 Elf_Internal_Shdr
*versymhdr
;
6122 Elf_Internal_Sym
*isym
;
6123 Elf_Internal_Sym
*isymend
;
6124 Elf_Internal_Sym
*isymbuf
;
6125 Elf_External_Versym
*ever
;
6126 Elf_External_Versym
*extversym
;
6128 input
= loaded
->abfd
;
6130 /* We check each DSO for a possible hidden versioned definition. */
6131 if (input
== undef_bfd
6132 || (input
->flags
& DYNAMIC
) == 0
6133 || elf_dynversym (input
) == 0)
6136 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6138 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6139 if (elf_bad_symtab (input
))
6141 extsymcount
= symcount
;
6146 extsymcount
= symcount
- hdr
->sh_info
;
6147 extsymoff
= hdr
->sh_info
;
6150 if (extsymcount
== 0)
6153 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6155 if (isymbuf
== NULL
)
6158 /* Read in any version definitions. */
6159 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6160 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
6161 if (extversym
== NULL
)
6164 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6165 || (bfd_bread ((PTR
) extversym
, versymhdr
->sh_size
, input
)
6166 != versymhdr
->sh_size
))
6174 ever
= extversym
+ extsymoff
;
6175 isymend
= isymbuf
+ extsymcount
;
6176 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6179 Elf_Internal_Versym iver
;
6181 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6182 || isym
->st_shndx
== SHN_UNDEF
)
6185 name
= bfd_elf_string_from_elf_section (input
,
6188 if (strcmp (name
, h
->root
.root
.string
) != 0)
6191 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6193 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6195 /* If we have a non-hidden versioned sym, then it should
6196 have provided a definition for the undefined sym. */
6200 if ((iver
.vs_vers
& VERSYM_VERSION
) == 2)
6202 /* This is the oldest (default) sym. We can use it. */
6216 /* Add an external symbol to the symbol table. This is called from
6217 the hash table traversal routine. When generating a shared object,
6218 we go through the symbol table twice. The first time we output
6219 anything that might have been forced to local scope in a version
6220 script. The second time we output the symbols that are still
6224 elf_link_output_extsym (h
, data
)
6225 struct elf_link_hash_entry
*h
;
6228 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
6229 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6231 Elf_Internal_Sym sym
;
6232 asection
*input_sec
;
6234 if (h
->root
.type
== bfd_link_hash_warning
)
6236 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6237 if (h
->root
.type
== bfd_link_hash_new
)
6241 /* Decide whether to output this symbol in this pass. */
6242 if (eoinfo
->localsyms
)
6244 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6249 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6253 /* If we are not creating a shared library, and this symbol is
6254 referenced by a shared library but is not defined anywhere, then
6255 warn that it is undefined. If we do not do this, the runtime
6256 linker will complain that the symbol is undefined when the
6257 program is run. We don't have to worry about symbols that are
6258 referenced by regular files, because we will already have issued
6259 warnings for them. */
6260 if (! finfo
->info
->relocateable
6261 && (! finfo
->info
->shared
|| ! finfo
->info
->allow_shlib_undefined
)
6262 && h
->root
.type
== bfd_link_hash_undefined
6263 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
6264 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
6265 && ! elf_link_check_versioned_symbol (finfo
->info
, h
))
6267 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6268 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6269 (asection
*) NULL
, (bfd_vma
) 0, TRUE
)))
6271 eoinfo
->failed
= TRUE
;
6276 /* We don't want to output symbols that have never been mentioned by
6277 a regular file, or that we have been told to strip. However, if
6278 h->indx is set to -2, the symbol is used by a reloc and we must
6282 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
6283 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
6284 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
6285 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
6287 else if (finfo
->info
->strip
== strip_all
)
6289 else if (finfo
->info
->strip
== strip_some
6290 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6291 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6293 else if (finfo
->info
->strip_discarded
6294 && (h
->root
.type
== bfd_link_hash_defined
6295 || h
->root
.type
== bfd_link_hash_defweak
)
6296 && elf_discarded_section (h
->root
.u
.def
.section
))
6301 /* If we're stripping it, and it's not a dynamic symbol, there's
6302 nothing else to do unless it is a forced local symbol. */
6305 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6309 sym
.st_size
= h
->size
;
6310 sym
.st_other
= h
->other
;
6311 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6312 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6313 else if (h
->root
.type
== bfd_link_hash_undefweak
6314 || h
->root
.type
== bfd_link_hash_defweak
)
6315 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6317 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6319 switch (h
->root
.type
)
6322 case bfd_link_hash_new
:
6323 case bfd_link_hash_warning
:
6327 case bfd_link_hash_undefined
:
6328 case bfd_link_hash_undefweak
:
6329 input_sec
= bfd_und_section_ptr
;
6330 sym
.st_shndx
= SHN_UNDEF
;
6333 case bfd_link_hash_defined
:
6334 case bfd_link_hash_defweak
:
6336 input_sec
= h
->root
.u
.def
.section
;
6337 if (input_sec
->output_section
!= NULL
)
6340 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6341 input_sec
->output_section
);
6342 if (sym
.st_shndx
== SHN_BAD
)
6344 (*_bfd_error_handler
)
6345 (_("%s: could not find output section %s for input section %s"),
6346 bfd_get_filename (finfo
->output_bfd
),
6347 input_sec
->output_section
->name
,
6349 eoinfo
->failed
= TRUE
;
6353 /* ELF symbols in relocateable files are section relative,
6354 but in nonrelocateable files they are virtual
6356 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6357 if (! finfo
->info
->relocateable
)
6359 sym
.st_value
+= input_sec
->output_section
->vma
;
6360 if (h
->type
== STT_TLS
)
6362 /* STT_TLS symbols are relative to PT_TLS segment
6364 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6365 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
6371 BFD_ASSERT (input_sec
->owner
== NULL
6372 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6373 sym
.st_shndx
= SHN_UNDEF
;
6374 input_sec
= bfd_und_section_ptr
;
6379 case bfd_link_hash_common
:
6380 input_sec
= h
->root
.u
.c
.p
->section
;
6381 sym
.st_shndx
= SHN_COMMON
;
6382 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6385 case bfd_link_hash_indirect
:
6386 /* These symbols are created by symbol versioning. They point
6387 to the decorated version of the name. For example, if the
6388 symbol foo@@GNU_1.2 is the default, which should be used when
6389 foo is used with no version, then we add an indirect symbol
6390 foo which points to foo@@GNU_1.2. We ignore these symbols,
6391 since the indirected symbol is already in the hash table. */
6395 /* Give the processor backend a chance to tweak the symbol value,
6396 and also to finish up anything that needs to be done for this
6397 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6398 forced local syms when non-shared is due to a historical quirk. */
6399 if ((h
->dynindx
!= -1
6400 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6401 && (finfo
->info
->shared
6402 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6403 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6405 struct elf_backend_data
*bed
;
6407 bed
= get_elf_backend_data (finfo
->output_bfd
);
6408 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6409 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6411 eoinfo
->failed
= TRUE
;
6416 /* If we are marking the symbol as undefined, and there are no
6417 non-weak references to this symbol from a regular object, then
6418 mark the symbol as weak undefined; if there are non-weak
6419 references, mark the symbol as strong. We can't do this earlier,
6420 because it might not be marked as undefined until the
6421 finish_dynamic_symbol routine gets through with it. */
6422 if (sym
.st_shndx
== SHN_UNDEF
6423 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
6424 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6425 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6429 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
6430 bindtype
= STB_GLOBAL
;
6432 bindtype
= STB_WEAK
;
6433 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6436 /* If a symbol is not defined locally, we clear the visibility field. */
6437 if (! finfo
->info
->relocateable
6438 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6439 sym
.st_other
&= ~ ELF_ST_VISIBILITY (-1);
6441 /* If this symbol should be put in the .dynsym section, then put it
6442 there now. We already know the symbol index. We also fill in
6443 the entry in the .hash section. */
6444 if (h
->dynindx
!= -1
6445 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6449 size_t hash_entry_size
;
6450 bfd_byte
*bucketpos
;
6452 Elf_External_Sym
*esym
;
6454 sym
.st_name
= h
->dynstr_index
;
6455 esym
= (Elf_External_Sym
*) finfo
->dynsym_sec
->contents
+ h
->dynindx
;
6456 elf_swap_symbol_out (finfo
->output_bfd
, &sym
, (PTR
) esym
, (PTR
) 0);
6458 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6459 bucket
= h
->elf_hash_value
% bucketcount
;
6461 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6462 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6463 + (bucket
+ 2) * hash_entry_size
);
6464 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6465 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, (bfd_vma
) h
->dynindx
,
6467 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6468 ((bfd_byte
*) finfo
->hash_sec
->contents
6469 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6471 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6473 Elf_Internal_Versym iversym
;
6474 Elf_External_Versym
*eversym
;
6476 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6478 if (h
->verinfo
.verdef
== NULL
)
6479 iversym
.vs_vers
= 0;
6481 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6485 if (h
->verinfo
.vertree
== NULL
)
6486 iversym
.vs_vers
= 1;
6488 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6491 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
6492 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6494 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6495 eversym
+= h
->dynindx
;
6496 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6500 /* If we're stripping it, then it was just a dynamic symbol, and
6501 there's nothing else to do. */
6502 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6505 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6507 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
6509 eoinfo
->failed
= TRUE
;
6516 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6517 originated from the section given by INPUT_REL_HDR) to the
6521 elf_link_output_relocs (output_bfd
, input_section
, input_rel_hdr
,
6524 asection
*input_section
;
6525 Elf_Internal_Shdr
*input_rel_hdr
;
6526 Elf_Internal_Rela
*internal_relocs
;
6528 Elf_Internal_Rela
*irela
;
6529 Elf_Internal_Rela
*irelaend
;
6531 Elf_Internal_Shdr
*output_rel_hdr
;
6532 asection
*output_section
;
6533 unsigned int *rel_countp
= NULL
;
6534 struct elf_backend_data
*bed
;
6535 void (*swap_out
) PARAMS ((bfd
*, const Elf_Internal_Rela
*, bfd_byte
*));
6537 output_section
= input_section
->output_section
;
6538 output_rel_hdr
= NULL
;
6540 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
6541 == input_rel_hdr
->sh_entsize
)
6543 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
6544 rel_countp
= &elf_section_data (output_section
)->rel_count
;
6546 else if (elf_section_data (output_section
)->rel_hdr2
6547 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
6548 == input_rel_hdr
->sh_entsize
))
6550 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
6551 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
6555 (*_bfd_error_handler
)
6556 (_("%s: relocation size mismatch in %s section %s"),
6557 bfd_get_filename (output_bfd
),
6558 bfd_archive_filename (input_section
->owner
),
6559 input_section
->name
);
6560 bfd_set_error (bfd_error_wrong_object_format
);
6564 bed
= get_elf_backend_data (output_bfd
);
6565 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
6566 swap_out
= bed
->s
->swap_reloc_out
;
6567 else if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
))
6568 swap_out
= bed
->s
->swap_reloca_out
;
6572 erel
= output_rel_hdr
->contents
;
6573 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
6574 irela
= internal_relocs
;
6575 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
6576 * bed
->s
->int_rels_per_ext_rel
);
6577 while (irela
< irelaend
)
6579 (*swap_out
) (output_bfd
, irela
, erel
);
6580 irela
+= bed
->s
->int_rels_per_ext_rel
;
6581 erel
+= input_rel_hdr
->sh_entsize
;
6584 /* Bump the counter, so that we know where to add the next set of
6586 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
6591 /* Link an input file into the linker output file. This function
6592 handles all the sections and relocations of the input file at once.
6593 This is so that we only have to read the local symbols once, and
6594 don't have to keep them in memory. */
6597 elf_link_input_bfd (finfo
, input_bfd
)
6598 struct elf_final_link_info
*finfo
;
6601 bfd_boolean (*relocate_section
)
6602 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6603 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
6605 Elf_Internal_Shdr
*symtab_hdr
;
6608 Elf_Internal_Sym
*isymbuf
;
6609 Elf_Internal_Sym
*isym
;
6610 Elf_Internal_Sym
*isymend
;
6612 asection
**ppsection
;
6614 struct elf_backend_data
*bed
;
6615 bfd_boolean emit_relocs
;
6616 struct elf_link_hash_entry
**sym_hashes
;
6618 output_bfd
= finfo
->output_bfd
;
6619 bed
= get_elf_backend_data (output_bfd
);
6620 relocate_section
= bed
->elf_backend_relocate_section
;
6622 /* If this is a dynamic object, we don't want to do anything here:
6623 we don't want the local symbols, and we don't want the section
6625 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6628 emit_relocs
= (finfo
->info
->relocateable
6629 || finfo
->info
->emitrelocations
6630 || bed
->elf_backend_emit_relocs
);
6632 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6633 if (elf_bad_symtab (input_bfd
))
6635 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6640 locsymcount
= symtab_hdr
->sh_info
;
6641 extsymoff
= symtab_hdr
->sh_info
;
6644 /* Read the local symbols. */
6645 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6646 if (isymbuf
== NULL
&& locsymcount
!= 0)
6648 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6649 finfo
->internal_syms
,
6650 finfo
->external_syms
,
6651 finfo
->locsym_shndx
);
6652 if (isymbuf
== NULL
)
6656 /* Find local symbol sections and adjust values of symbols in
6657 SEC_MERGE sections. Write out those local symbols we know are
6658 going into the output file. */
6659 isymend
= isymbuf
+ locsymcount
;
6660 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6662 isym
++, pindex
++, ppsection
++)
6666 Elf_Internal_Sym osym
;
6670 if (elf_bad_symtab (input_bfd
))
6672 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6679 if (isym
->st_shndx
== SHN_UNDEF
)
6680 isec
= bfd_und_section_ptr
;
6681 else if (isym
->st_shndx
< SHN_LORESERVE
6682 || isym
->st_shndx
> SHN_HIRESERVE
)
6684 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
6686 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6687 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6689 _bfd_merged_section_offset (output_bfd
, &isec
,
6690 elf_section_data (isec
)->sec_info
,
6691 isym
->st_value
, (bfd_vma
) 0);
6693 else if (isym
->st_shndx
== SHN_ABS
)
6694 isec
= bfd_abs_section_ptr
;
6695 else if (isym
->st_shndx
== SHN_COMMON
)
6696 isec
= bfd_com_section_ptr
;
6705 /* Don't output the first, undefined, symbol. */
6706 if (ppsection
== finfo
->sections
)
6709 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6711 /* We never output section symbols. Instead, we use the
6712 section symbol of the corresponding section in the output
6717 /* If we are stripping all symbols, we don't want to output this
6719 if (finfo
->info
->strip
== strip_all
)
6722 /* If we are discarding all local symbols, we don't want to
6723 output this one. If we are generating a relocateable output
6724 file, then some of the local symbols may be required by
6725 relocs; we output them below as we discover that they are
6727 if (finfo
->info
->discard
== discard_all
)
6730 /* If this symbol is defined in a section which we are
6731 discarding, we don't need to keep it, but note that
6732 linker_mark is only reliable for sections that have contents.
6733 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6734 as well as linker_mark. */
6735 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6737 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6738 || (! finfo
->info
->relocateable
6739 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6742 /* Get the name of the symbol. */
6743 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6748 /* See if we are discarding symbols with this name. */
6749 if ((finfo
->info
->strip
== strip_some
6750 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6752 || (((finfo
->info
->discard
== discard_sec_merge
6753 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocateable
)
6754 || finfo
->info
->discard
== discard_l
)
6755 && bfd_is_local_label_name (input_bfd
, name
)))
6758 /* If we get here, we are going to output this symbol. */
6762 /* Adjust the section index for the output file. */
6763 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6764 isec
->output_section
);
6765 if (osym
.st_shndx
== SHN_BAD
)
6768 *pindex
= bfd_get_symcount (output_bfd
);
6770 /* ELF symbols in relocateable files are section relative, but
6771 in executable files they are virtual addresses. Note that
6772 this code assumes that all ELF sections have an associated
6773 BFD section with a reasonable value for output_offset; below
6774 we assume that they also have a reasonable value for
6775 output_section. Any special sections must be set up to meet
6776 these requirements. */
6777 osym
.st_value
+= isec
->output_offset
;
6778 if (! finfo
->info
->relocateable
)
6780 osym
.st_value
+= isec
->output_section
->vma
;
6781 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6783 /* STT_TLS symbols are relative to PT_TLS segment base. */
6784 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6785 osym
.st_value
-= finfo
->first_tls_sec
->vma
;
6789 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
6793 /* Relocate the contents of each section. */
6794 sym_hashes
= elf_sym_hashes (input_bfd
);
6795 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6799 if (! o
->linker_mark
)
6801 /* This section was omitted from the link. */
6805 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6806 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6809 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6811 /* Section was created by elf_link_create_dynamic_sections
6816 /* Get the contents of the section. They have been cached by a
6817 relaxation routine. Note that o is a section in an input
6818 file, so the contents field will not have been set by any of
6819 the routines which work on output files. */
6820 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6821 contents
= elf_section_data (o
)->this_hdr
.contents
;
6824 contents
= finfo
->contents
;
6825 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
6826 (file_ptr
) 0, o
->_raw_size
))
6830 if ((o
->flags
& SEC_RELOC
) != 0)
6832 Elf_Internal_Rela
*internal_relocs
;
6834 /* Get the swapped relocs. */
6835 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
6836 (input_bfd
, o
, finfo
->external_relocs
,
6837 finfo
->internal_relocs
, FALSE
));
6838 if (internal_relocs
== NULL
6839 && o
->reloc_count
> 0)
6842 /* Run through the relocs looking for any against symbols
6843 from discarded sections and section symbols from
6844 removed link-once sections. Complain about relocs
6845 against discarded sections. Zero relocs against removed
6846 link-once sections. */
6847 if (!finfo
->info
->relocateable
6848 && !elf_section_ignore_discarded_relocs (o
))
6850 Elf_Internal_Rela
*rel
, *relend
;
6852 rel
= internal_relocs
;
6853 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6854 for ( ; rel
< relend
; rel
++)
6856 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
6858 if (r_symndx
>= locsymcount
6859 || (elf_bad_symtab (input_bfd
)
6860 && finfo
->sections
[r_symndx
] == NULL
))
6862 struct elf_link_hash_entry
*h
;
6864 h
= sym_hashes
[r_symndx
- extsymoff
];
6865 while (h
->root
.type
== bfd_link_hash_indirect
6866 || h
->root
.type
== bfd_link_hash_warning
)
6867 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6869 /* Complain if the definition comes from a
6870 discarded section. */
6871 if ((h
->root
.type
== bfd_link_hash_defined
6872 || h
->root
.type
== bfd_link_hash_defweak
)
6873 && elf_discarded_section (h
->root
.u
.def
.section
))
6875 if ((o
->flags
& SEC_DEBUGGING
) != 0)
6877 BFD_ASSERT (r_symndx
!= 0);
6878 memset (rel
, 0, sizeof (*rel
));
6882 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6883 (finfo
->info
, h
->root
.root
.string
,
6884 input_bfd
, o
, rel
->r_offset
,
6892 asection
*sec
= finfo
->sections
[r_symndx
];
6894 if (sec
!= NULL
&& elf_discarded_section (sec
))
6896 if ((o
->flags
& SEC_DEBUGGING
) != 0
6897 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
6899 BFD_ASSERT (r_symndx
!= 0);
6901 = ELF_R_INFO (0, ELF_R_TYPE (rel
->r_info
));
6908 = _("local symbols in discarded section %s");
6910 = strlen (sec
->name
) + strlen (msg
) - 1;
6911 char *buf
= (char *) bfd_malloc (amt
);
6914 sprintf (buf
, msg
, sec
->name
);
6916 buf
= (char *) sec
->name
;
6917 ok
= (*finfo
->info
->callbacks
6918 ->undefined_symbol
) (finfo
->info
, buf
,
6922 if (buf
!= sec
->name
)
6932 /* Relocate the section by invoking a back end routine.
6934 The back end routine is responsible for adjusting the
6935 section contents as necessary, and (if using Rela relocs
6936 and generating a relocateable output file) adjusting the
6937 reloc addend as necessary.
6939 The back end routine does not have to worry about setting
6940 the reloc address or the reloc symbol index.
6942 The back end routine is given a pointer to the swapped in
6943 internal symbols, and can access the hash table entries
6944 for the external symbols via elf_sym_hashes (input_bfd).
6946 When generating relocateable output, the back end routine
6947 must handle STB_LOCAL/STT_SECTION symbols specially. The
6948 output symbol is going to be a section symbol
6949 corresponding to the output section, which will require
6950 the addend to be adjusted. */
6952 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6953 input_bfd
, o
, contents
,
6961 Elf_Internal_Rela
*irela
;
6962 Elf_Internal_Rela
*irelaend
;
6963 bfd_vma last_offset
;
6964 struct elf_link_hash_entry
**rel_hash
;
6965 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6966 unsigned int next_erel
;
6967 bfd_boolean (*reloc_emitter
)
6968 PARAMS ((bfd
*, asection
*, Elf_Internal_Shdr
*,
6969 Elf_Internal_Rela
*));
6970 bfd_boolean rela_normal
;
6972 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6973 rela_normal
= (bed
->rela_normal
6974 && (input_rel_hdr
->sh_entsize
6975 == sizeof (Elf_External_Rela
)));
6977 /* Adjust the reloc addresses and symbol indices. */
6979 irela
= internal_relocs
;
6980 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6981 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6982 + elf_section_data (o
->output_section
)->rel_count
6983 + elf_section_data (o
->output_section
)->rel_count2
);
6984 last_offset
= o
->output_offset
;
6985 if (!finfo
->info
->relocateable
)
6986 last_offset
+= o
->output_section
->vma
;
6987 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6989 unsigned long r_symndx
;
6991 Elf_Internal_Sym sym
;
6993 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6999 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
7002 if (irela
->r_offset
>= (bfd_vma
) -2)
7004 /* This is a reloc for a deleted entry or somesuch.
7005 Turn it into an R_*_NONE reloc, at the same
7006 offset as the last reloc. elf_eh_frame.c and
7007 elf_bfd_discard_info rely on reloc offsets
7009 irela
->r_offset
= last_offset
;
7011 irela
->r_addend
= 0;
7015 irela
->r_offset
+= o
->output_offset
;
7017 /* Relocs in an executable have to be virtual addresses. */
7018 if (!finfo
->info
->relocateable
)
7019 irela
->r_offset
+= o
->output_section
->vma
;
7021 last_offset
= irela
->r_offset
;
7023 r_symndx
= ELF_R_SYM (irela
->r_info
);
7024 if (r_symndx
== STN_UNDEF
)
7027 if (r_symndx
>= locsymcount
7028 || (elf_bad_symtab (input_bfd
)
7029 && finfo
->sections
[r_symndx
] == NULL
))
7031 struct elf_link_hash_entry
*rh
;
7034 /* This is a reloc against a global symbol. We
7035 have not yet output all the local symbols, so
7036 we do not know the symbol index of any global
7037 symbol. We set the rel_hash entry for this
7038 reloc to point to the global hash table entry
7039 for this symbol. The symbol index is then
7040 set at the end of elf_bfd_final_link. */
7041 indx
= r_symndx
- extsymoff
;
7042 rh
= elf_sym_hashes (input_bfd
)[indx
];
7043 while (rh
->root
.type
== bfd_link_hash_indirect
7044 || rh
->root
.type
== bfd_link_hash_warning
)
7045 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7047 /* Setting the index to -2 tells
7048 elf_link_output_extsym that this symbol is
7050 BFD_ASSERT (rh
->indx
< 0);
7058 /* This is a reloc against a local symbol. */
7061 sym
= isymbuf
[r_symndx
];
7062 sec
= finfo
->sections
[r_symndx
];
7063 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7065 /* I suppose the backend ought to fill in the
7066 section of any STT_SECTION symbol against a
7067 processor specific section. If we have
7068 discarded a section, the output_section will
7069 be the absolute section. */
7070 if (bfd_is_abs_section (sec
)
7072 && bfd_is_abs_section (sec
->output_section
)))
7074 else if (sec
== NULL
|| sec
->owner
== NULL
)
7076 bfd_set_error (bfd_error_bad_value
);
7081 r_symndx
= sec
->output_section
->target_index
;
7082 BFD_ASSERT (r_symndx
!= 0);
7085 /* Adjust the addend according to where the
7086 section winds up in the output section. */
7088 irela
->r_addend
+= sec
->output_offset
;
7092 if (finfo
->indices
[r_symndx
] == -1)
7094 unsigned long shlink
;
7098 if (finfo
->info
->strip
== strip_all
)
7100 /* You can't do ld -r -s. */
7101 bfd_set_error (bfd_error_invalid_operation
);
7105 /* This symbol was skipped earlier, but
7106 since it is needed by a reloc, we
7107 must output it now. */
7108 shlink
= symtab_hdr
->sh_link
;
7109 name
= (bfd_elf_string_from_elf_section
7110 (input_bfd
, shlink
, sym
.st_name
));
7114 osec
= sec
->output_section
;
7116 _bfd_elf_section_from_bfd_section (output_bfd
,
7118 if (sym
.st_shndx
== SHN_BAD
)
7121 sym
.st_value
+= sec
->output_offset
;
7122 if (! finfo
->info
->relocateable
)
7124 sym
.st_value
+= osec
->vma
;
7125 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7127 /* STT_TLS symbols are relative to PT_TLS
7129 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
7130 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
7134 finfo
->indices
[r_symndx
]
7135 = bfd_get_symcount (output_bfd
);
7137 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
))
7141 r_symndx
= finfo
->indices
[r_symndx
];
7144 irela
->r_info
= ELF_R_INFO (r_symndx
,
7145 ELF_R_TYPE (irela
->r_info
));
7148 /* Swap out the relocs. */
7149 if (bed
->elf_backend_emit_relocs
7150 && !(finfo
->info
->relocateable
7151 || finfo
->info
->emitrelocations
))
7152 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7154 reloc_emitter
= elf_link_output_relocs
;
7156 if (input_rel_hdr
->sh_size
!= 0
7157 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7161 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7162 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7164 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7165 * bed
->s
->int_rels_per_ext_rel
);
7166 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
7173 /* Write out the modified section contents. */
7174 if (bed
->elf_backend_write_section
7175 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7177 /* Section written out. */
7179 else switch (o
->sec_info_type
)
7181 case ELF_INFO_TYPE_STABS
:
7182 if (! (_bfd_write_section_stabs
7184 &elf_hash_table (finfo
->info
)->stab_info
,
7185 o
, &elf_section_data (o
)->sec_info
, contents
)))
7188 case ELF_INFO_TYPE_MERGE
:
7189 if (! _bfd_write_merged_section (output_bfd
, o
,
7190 elf_section_data (o
)->sec_info
))
7193 case ELF_INFO_TYPE_EH_FRAME
:
7195 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7202 bfd_size_type sec_size
;
7204 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
7205 if (! (o
->flags
& SEC_EXCLUDE
)
7206 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7208 (file_ptr
) o
->output_offset
,
7219 /* Generate a reloc when linking an ELF file. This is a reloc
7220 requested by the linker, and does come from any input file. This
7221 is used to build constructor and destructor tables when linking
7225 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
7227 struct bfd_link_info
*info
;
7228 asection
*output_section
;
7229 struct bfd_link_order
*link_order
;
7231 reloc_howto_type
*howto
;
7235 struct elf_link_hash_entry
**rel_hash_ptr
;
7236 Elf_Internal_Shdr
*rel_hdr
;
7237 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7238 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7242 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7245 bfd_set_error (bfd_error_bad_value
);
7249 addend
= link_order
->u
.reloc
.p
->addend
;
7251 /* Figure out the symbol index. */
7252 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7253 + elf_section_data (output_section
)->rel_count
7254 + elf_section_data (output_section
)->rel_count2
);
7255 if (link_order
->type
== bfd_section_reloc_link_order
)
7257 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7258 BFD_ASSERT (indx
!= 0);
7259 *rel_hash_ptr
= NULL
;
7263 struct elf_link_hash_entry
*h
;
7265 /* Treat a reloc against a defined symbol as though it were
7266 actually against the section. */
7267 h
= ((struct elf_link_hash_entry
*)
7268 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7269 link_order
->u
.reloc
.p
->u
.name
,
7270 FALSE
, FALSE
, TRUE
));
7272 && (h
->root
.type
== bfd_link_hash_defined
7273 || h
->root
.type
== bfd_link_hash_defweak
))
7277 section
= h
->root
.u
.def
.section
;
7278 indx
= section
->output_section
->target_index
;
7279 *rel_hash_ptr
= NULL
;
7280 /* It seems that we ought to add the symbol value to the
7281 addend here, but in practice it has already been added
7282 because it was passed to constructor_callback. */
7283 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7287 /* Setting the index to -2 tells elf_link_output_extsym that
7288 this symbol is used by a reloc. */
7295 if (! ((*info
->callbacks
->unattached_reloc
)
7296 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
7297 (asection
*) NULL
, (bfd_vma
) 0)))
7303 /* If this is an inplace reloc, we must write the addend into the
7305 if (howto
->partial_inplace
&& addend
!= 0)
7308 bfd_reloc_status_type rstat
;
7311 const char *sym_name
;
7313 size
= bfd_get_reloc_size (howto
);
7314 buf
= (bfd_byte
*) bfd_zmalloc (size
);
7315 if (buf
== (bfd_byte
*) NULL
)
7317 rstat
= _bfd_relocate_contents (howto
, output_bfd
, (bfd_vma
) addend
, buf
);
7324 case bfd_reloc_outofrange
:
7327 case bfd_reloc_overflow
:
7328 if (link_order
->type
== bfd_section_reloc_link_order
)
7329 sym_name
= bfd_section_name (output_bfd
,
7330 link_order
->u
.reloc
.p
->u
.section
);
7332 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7333 if (! ((*info
->callbacks
->reloc_overflow
)
7334 (info
, sym_name
, howto
->name
, addend
,
7335 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
7342 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
7343 (file_ptr
) link_order
->offset
, size
);
7349 /* The address of a reloc is relative to the section in a
7350 relocateable file, and is a virtual address in an executable
7352 offset
= link_order
->offset
;
7353 if (! info
->relocateable
)
7354 offset
+= output_section
->vma
;
7356 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7358 irel
[i
].r_offset
= offset
;
7360 irel
[i
].r_addend
= 0;
7362 irel
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
7364 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7365 erel
= rel_hdr
->contents
;
7366 if (rel_hdr
->sh_type
== SHT_REL
)
7368 erel
+= (elf_section_data (output_section
)->rel_count
7369 * sizeof (Elf_External_Rel
));
7370 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7374 irel
[0].r_addend
= addend
;
7375 erel
+= (elf_section_data (output_section
)->rel_count
7376 * sizeof (Elf_External_Rela
));
7377 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7380 ++elf_section_data (output_section
)->rel_count
;
7385 /* Allocate a pointer to live in a linker created section. */
7388 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
7390 struct bfd_link_info
*info
;
7391 elf_linker_section_t
*lsect
;
7392 struct elf_link_hash_entry
*h
;
7393 const Elf_Internal_Rela
*rel
;
7395 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
7396 elf_linker_section_pointers_t
*linker_section_ptr
;
7397 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7400 BFD_ASSERT (lsect
!= NULL
);
7402 /* Is this a global symbol? */
7405 /* Has this symbol already been allocated? If so, our work is done. */
7406 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
7411 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
7412 /* Make sure this symbol is output as a dynamic symbol. */
7413 if (h
->dynindx
== -1)
7415 if (! elf_link_record_dynamic_symbol (info
, h
))
7419 if (lsect
->rel_section
)
7420 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7424 /* Allocation of a pointer to a local symbol. */
7425 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
7427 /* Allocate a table to hold the local symbols if first time. */
7430 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
7431 register unsigned int i
;
7434 amt
*= sizeof (elf_linker_section_pointers_t
*);
7435 ptr
= (elf_linker_section_pointers_t
**) bfd_alloc (abfd
, amt
);
7440 elf_local_ptr_offsets (abfd
) = ptr
;
7441 for (i
= 0; i
< num_symbols
; i
++)
7442 ptr
[i
] = (elf_linker_section_pointers_t
*) 0;
7445 /* Has this symbol already been allocated? If so, our work is done. */
7446 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
7451 ptr_linker_section_ptr
= &ptr
[r_symndx
];
7455 /* If we are generating a shared object, we need to
7456 output a R_<xxx>_RELATIVE reloc so that the
7457 dynamic linker can adjust this GOT entry. */
7458 BFD_ASSERT (lsect
->rel_section
!= NULL
);
7459 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7463 /* Allocate space for a pointer in the linker section, and allocate
7464 a new pointer record from internal memory. */
7465 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
7466 amt
= sizeof (elf_linker_section_pointers_t
);
7467 linker_section_ptr
= (elf_linker_section_pointers_t
*) bfd_alloc (abfd
, amt
);
7469 if (!linker_section_ptr
)
7472 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
7473 linker_section_ptr
->addend
= rel
->r_addend
;
7474 linker_section_ptr
->which
= lsect
->which
;
7475 linker_section_ptr
->written_address_p
= FALSE
;
7476 *ptr_linker_section_ptr
= linker_section_ptr
;
7479 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
7481 linker_section_ptr
->offset
= (lsect
->section
->_raw_size
7482 - lsect
->hole_size
+ (ARCH_SIZE
/ 8));
7483 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
7484 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
7485 if (lsect
->sym_hash
)
7487 /* Bump up symbol value if needed. */
7488 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
7490 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
7491 lsect
->sym_hash
->root
.root
.string
,
7492 (long) ARCH_SIZE
/ 8,
7493 (long) lsect
->sym_hash
->root
.u
.def
.value
);
7499 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
7501 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
7505 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7506 lsect
->name
, (long) linker_section_ptr
->offset
,
7507 (long) lsect
->section
->_raw_size
);
7514 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7517 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7520 /* Fill in the address for a pointer generated in a linker section. */
7523 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
,
7524 relocation
, rel
, relative_reloc
)
7527 struct bfd_link_info
*info
;
7528 elf_linker_section_t
*lsect
;
7529 struct elf_link_hash_entry
*h
;
7531 const Elf_Internal_Rela
*rel
;
7534 elf_linker_section_pointers_t
*linker_section_ptr
;
7536 BFD_ASSERT (lsect
!= NULL
);
7540 /* Handle global symbol. */
7541 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7542 (h
->linker_section_pointer
,
7546 BFD_ASSERT (linker_section_ptr
!= NULL
);
7548 if (! elf_hash_table (info
)->dynamic_sections_created
7551 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
7553 /* This is actually a static link, or it is a
7554 -Bsymbolic link and the symbol is defined
7555 locally. We must initialize this entry in the
7558 When doing a dynamic link, we create a .rela.<xxx>
7559 relocation entry to initialize the value. This
7560 is done in the finish_dynamic_symbol routine. */
7561 if (!linker_section_ptr
->written_address_p
)
7563 linker_section_ptr
->written_address_p
= TRUE
;
7564 bfd_put_ptr (output_bfd
,
7565 relocation
+ linker_section_ptr
->addend
,
7566 (lsect
->section
->contents
7567 + linker_section_ptr
->offset
));
7573 /* Handle local symbol. */
7574 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7575 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
7576 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
7577 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7578 (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
7582 BFD_ASSERT (linker_section_ptr
!= NULL
);
7584 /* Write out pointer if it hasn't been rewritten out before. */
7585 if (!linker_section_ptr
->written_address_p
)
7587 linker_section_ptr
->written_address_p
= TRUE
;
7588 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
7589 lsect
->section
->contents
+ linker_section_ptr
->offset
);
7593 asection
*srel
= lsect
->rel_section
;
7594 Elf_Internal_Rela outrel
[MAX_INT_RELS_PER_EXT_REL
];
7596 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7599 /* We need to generate a relative reloc for the dynamic
7603 srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
7605 lsect
->rel_section
= srel
;
7608 BFD_ASSERT (srel
!= NULL
);
7610 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7612 outrel
[i
].r_offset
= (lsect
->section
->output_section
->vma
7613 + lsect
->section
->output_offset
7614 + linker_section_ptr
->offset
);
7615 outrel
[i
].r_info
= 0;
7616 outrel
[i
].r_addend
= 0;
7618 outrel
[0].r_info
= ELF_R_INFO (0, relative_reloc
);
7619 erel
= lsect
->section
->contents
;
7620 erel
+= (elf_section_data (lsect
->section
)->rel_count
++
7621 * sizeof (Elf_External_Rela
));
7622 elf_swap_reloca_out (output_bfd
, outrel
, erel
);
7627 relocation
= (lsect
->section
->output_offset
7628 + linker_section_ptr
->offset
7629 - lsect
->hole_offset
7630 - lsect
->sym_offset
);
7634 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7635 lsect
->name
, (long) relocation
, (long) relocation
);
7638 /* Subtract out the addend, because it will get added back in by the normal
7640 return relocation
- linker_section_ptr
->addend
;
7643 /* Garbage collect unused sections. */
7645 static bfd_boolean elf_gc_mark
7646 PARAMS ((struct bfd_link_info
*, asection
*,
7647 asection
* (*) (asection
*, struct bfd_link_info
*,
7648 Elf_Internal_Rela
*, struct elf_link_hash_entry
*,
7649 Elf_Internal_Sym
*)));
7651 static bfd_boolean elf_gc_sweep
7652 PARAMS ((struct bfd_link_info
*,
7653 bfd_boolean (*) (bfd
*, struct bfd_link_info
*, asection
*,
7654 const Elf_Internal_Rela
*)));
7656 static bfd_boolean elf_gc_sweep_symbol
7657 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7659 static bfd_boolean elf_gc_allocate_got_offsets
7660 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7662 static bfd_boolean elf_gc_propagate_vtable_entries_used
7663 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7665 static bfd_boolean elf_gc_smash_unused_vtentry_relocs
7666 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7668 /* The mark phase of garbage collection. For a given section, mark
7669 it and any sections in this section's group, and all the sections
7670 which define symbols to which it refers. */
7672 typedef asection
* (*gc_mark_hook_fn
)
7673 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7674 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
7677 elf_gc_mark (info
, sec
, gc_mark_hook
)
7678 struct bfd_link_info
*info
;
7680 gc_mark_hook_fn gc_mark_hook
;
7683 asection
*group_sec
;
7687 /* Mark all the sections in the group. */
7688 group_sec
= elf_section_data (sec
)->next_in_group
;
7689 if (group_sec
&& !group_sec
->gc_mark
)
7690 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
7693 /* Look through the section relocs. */
7695 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
7697 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
7698 Elf_Internal_Shdr
*symtab_hdr
;
7699 struct elf_link_hash_entry
**sym_hashes
;
7702 bfd
*input_bfd
= sec
->owner
;
7703 struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
7704 Elf_Internal_Sym
*isym
= NULL
;
7706 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
7707 sym_hashes
= elf_sym_hashes (input_bfd
);
7709 /* Read the local symbols. */
7710 if (elf_bad_symtab (input_bfd
))
7712 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
7716 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
7718 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
7719 if (isym
== NULL
&& nlocsyms
!= 0)
7721 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
7727 /* Read the relocations. */
7728 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
7729 (input_bfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
7730 info
->keep_memory
));
7731 if (relstart
== NULL
)
7736 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7738 for (rel
= relstart
; rel
< relend
; rel
++)
7740 unsigned long r_symndx
;
7742 struct elf_link_hash_entry
*h
;
7744 r_symndx
= ELF_R_SYM (rel
->r_info
);
7748 if (r_symndx
>= nlocsyms
7749 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
7751 h
= sym_hashes
[r_symndx
- extsymoff
];
7752 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
7756 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
7759 if (rsec
&& !rsec
->gc_mark
)
7761 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
7763 else if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
7772 if (elf_section_data (sec
)->relocs
!= relstart
)
7775 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
7777 if (! info
->keep_memory
)
7780 symtab_hdr
->contents
= (unsigned char *) isym
;
7787 /* The sweep phase of garbage collection. Remove all garbage sections. */
7789 typedef bfd_boolean (*gc_sweep_hook_fn
)
7790 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
7791 const Elf_Internal_Rela
*));
7794 elf_gc_sweep (info
, gc_sweep_hook
)
7795 struct bfd_link_info
*info
;
7796 gc_sweep_hook_fn gc_sweep_hook
;
7800 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7804 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
7807 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7809 /* Keep special sections. Keep .debug sections. */
7810 if ((o
->flags
& SEC_LINKER_CREATED
)
7811 || (o
->flags
& SEC_DEBUGGING
))
7817 /* Skip sweeping sections already excluded. */
7818 if (o
->flags
& SEC_EXCLUDE
)
7821 /* Since this is early in the link process, it is simple
7822 to remove a section from the output. */
7823 o
->flags
|= SEC_EXCLUDE
;
7825 /* But we also have to update some of the relocation
7826 info we collected before. */
7828 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
7830 Elf_Internal_Rela
*internal_relocs
;
7833 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
7834 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
7835 if (internal_relocs
== NULL
)
7838 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
7840 if (elf_section_data (o
)->relocs
!= internal_relocs
)
7841 free (internal_relocs
);
7849 /* Remove the symbols that were in the swept sections from the dynamic
7850 symbol table. GCFIXME: Anyone know how to get them out of the
7851 static symbol table as well? */
7855 elf_link_hash_traverse (elf_hash_table (info
),
7856 elf_gc_sweep_symbol
,
7859 elf_hash_table (info
)->dynsymcount
= i
;
7865 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7868 elf_gc_sweep_symbol (h
, idxptr
)
7869 struct elf_link_hash_entry
*h
;
7872 int *idx
= (int *) idxptr
;
7874 if (h
->root
.type
== bfd_link_hash_warning
)
7875 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7877 if (h
->dynindx
!= -1
7878 && ((h
->root
.type
!= bfd_link_hash_defined
7879 && h
->root
.type
!= bfd_link_hash_defweak
)
7880 || h
->root
.u
.def
.section
->gc_mark
))
7881 h
->dynindx
= (*idx
)++;
7886 /* Propogate collected vtable information. This is called through
7887 elf_link_hash_traverse. */
7890 elf_gc_propagate_vtable_entries_used (h
, okp
)
7891 struct elf_link_hash_entry
*h
;
7894 if (h
->root
.type
== bfd_link_hash_warning
)
7895 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7897 /* Those that are not vtables. */
7898 if (h
->vtable_parent
== NULL
)
7901 /* Those vtables that do not have parents, we cannot merge. */
7902 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
7905 /* If we've already been done, exit. */
7906 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
7909 /* Make sure the parent's table is up to date. */
7910 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
7912 if (h
->vtable_entries_used
== NULL
)
7914 /* None of this table's entries were referenced. Re-use the
7916 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
7917 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
7922 bfd_boolean
*cu
, *pu
;
7924 /* Or the parent's entries into ours. */
7925 cu
= h
->vtable_entries_used
;
7927 pu
= h
->vtable_parent
->vtable_entries_used
;
7930 asection
*sec
= h
->root
.u
.def
.section
;
7931 struct elf_backend_data
*bed
= get_elf_backend_data (sec
->owner
);
7932 int file_align
= bed
->s
->file_align
;
7934 n
= h
->vtable_parent
->vtable_entries_size
/ file_align
;
7949 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
7950 struct elf_link_hash_entry
*h
;
7954 bfd_vma hstart
, hend
;
7955 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
7956 struct elf_backend_data
*bed
;
7959 if (h
->root
.type
== bfd_link_hash_warning
)
7960 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7962 /* Take care of both those symbols that do not describe vtables as
7963 well as those that are not loaded. */
7964 if (h
->vtable_parent
== NULL
)
7967 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
7968 || h
->root
.type
== bfd_link_hash_defweak
);
7970 sec
= h
->root
.u
.def
.section
;
7971 hstart
= h
->root
.u
.def
.value
;
7972 hend
= hstart
+ h
->size
;
7974 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
7975 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, TRUE
));
7977 return *(bfd_boolean
*) okp
= FALSE
;
7978 bed
= get_elf_backend_data (sec
->owner
);
7979 file_align
= bed
->s
->file_align
;
7981 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7983 for (rel
= relstart
; rel
< relend
; ++rel
)
7984 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
7986 /* If the entry is in use, do nothing. */
7987 if (h
->vtable_entries_used
7988 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
7990 bfd_vma entry
= (rel
->r_offset
- hstart
) / file_align
;
7991 if (h
->vtable_entries_used
[entry
])
7994 /* Otherwise, kill it. */
7995 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8001 /* Do mark and sweep of unused sections. */
8004 elf_gc_sections (abfd
, info
)
8006 struct bfd_link_info
*info
;
8008 bfd_boolean ok
= TRUE
;
8010 asection
* (*gc_mark_hook
)
8011 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8012 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
8014 if (!get_elf_backend_data (abfd
)->can_gc_sections
8015 || info
->relocateable
|| info
->emitrelocations
8016 || elf_hash_table (info
)->dynamic_sections_created
)
8019 /* Apply transitive closure to the vtable entry usage info. */
8020 elf_link_hash_traverse (elf_hash_table (info
),
8021 elf_gc_propagate_vtable_entries_used
,
8026 /* Kill the vtable relocations that were not used. */
8027 elf_link_hash_traverse (elf_hash_table (info
),
8028 elf_gc_smash_unused_vtentry_relocs
,
8033 /* Grovel through relocs to find out who stays ... */
8035 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8036 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8040 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8043 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8045 if (o
->flags
& SEC_KEEP
)
8046 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
8051 /* ... and mark SEC_EXCLUDE for those that go. */
8052 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8058 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
8061 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
8064 struct elf_link_hash_entry
*h
;
8067 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8068 struct elf_link_hash_entry
**search
, *child
;
8069 bfd_size_type extsymcount
;
8071 /* The sh_info field of the symtab header tells us where the
8072 external symbols start. We don't care about the local symbols at
8074 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
8075 if (!elf_bad_symtab (abfd
))
8076 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8078 sym_hashes
= elf_sym_hashes (abfd
);
8079 sym_hashes_end
= sym_hashes
+ extsymcount
;
8081 /* Hunt down the child symbol, which is in this section at the same
8082 offset as the relocation. */
8083 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8085 if ((child
= *search
) != NULL
8086 && (child
->root
.type
== bfd_link_hash_defined
8087 || child
->root
.type
== bfd_link_hash_defweak
)
8088 && child
->root
.u
.def
.section
== sec
8089 && child
->root
.u
.def
.value
== offset
)
8093 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
8094 bfd_archive_filename (abfd
), sec
->name
,
8095 (unsigned long) offset
);
8096 bfd_set_error (bfd_error_invalid_operation
);
8102 /* This *should* only be the absolute section. It could potentially
8103 be that someone has defined a non-global vtable though, which
8104 would be bad. It isn't worth paging in the local symbols to be
8105 sure though; that case should simply be handled by the assembler. */
8107 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
8110 child
->vtable_parent
= h
;
8115 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
8118 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
8119 bfd
*abfd ATTRIBUTE_UNUSED
;
8120 asection
*sec ATTRIBUTE_UNUSED
;
8121 struct elf_link_hash_entry
*h
;
8124 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8125 int file_align
= bed
->s
->file_align
;
8127 if (addend
>= h
->vtable_entries_size
)
8130 bfd_boolean
*ptr
= h
->vtable_entries_used
;
8132 /* While the symbol is undefined, we have to be prepared to handle
8134 if (h
->root
.type
== bfd_link_hash_undefined
)
8141 /* Oops! We've got a reference past the defined end of
8142 the table. This is probably a bug -- shall we warn? */
8147 /* Allocate one extra entry for use as a "done" flag for the
8148 consolidation pass. */
8149 bytes
= (size
/ file_align
+ 1) * sizeof (bfd_boolean
);
8153 ptr
= bfd_realloc (ptr
- 1, (bfd_size_type
) bytes
);
8159 oldbytes
= ((h
->vtable_entries_size
/ file_align
+ 1)
8160 * sizeof (bfd_boolean
));
8161 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8165 ptr
= bfd_zmalloc ((bfd_size_type
) bytes
);
8170 /* And arrange for that done flag to be at index -1. */
8171 h
->vtable_entries_used
= ptr
+ 1;
8172 h
->vtable_entries_size
= size
;
8175 h
->vtable_entries_used
[addend
/ file_align
] = TRUE
;
8180 /* And an accompanying bit to work out final got entry offsets once
8181 we're done. Should be called from final_link. */
8184 elf_gc_common_finalize_got_offsets (abfd
, info
)
8186 struct bfd_link_info
*info
;
8189 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8192 /* The GOT offset is relative to the .got section, but the GOT header is
8193 put into the .got.plt section, if the backend uses it. */
8194 if (bed
->want_got_plt
)
8197 gotoff
= bed
->got_header_size
;
8199 /* Do the local .got entries first. */
8200 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
8202 bfd_signed_vma
*local_got
;
8203 bfd_size_type j
, locsymcount
;
8204 Elf_Internal_Shdr
*symtab_hdr
;
8206 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
8209 local_got
= elf_local_got_refcounts (i
);
8213 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
8214 if (elf_bad_symtab (i
))
8215 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8217 locsymcount
= symtab_hdr
->sh_info
;
8219 for (j
= 0; j
< locsymcount
; ++j
)
8221 if (local_got
[j
] > 0)
8223 local_got
[j
] = gotoff
;
8224 gotoff
+= ARCH_SIZE
/ 8;
8227 local_got
[j
] = (bfd_vma
) -1;
8231 /* Then the global .got entries. .plt refcounts are handled by
8232 adjust_dynamic_symbol */
8233 elf_link_hash_traverse (elf_hash_table (info
),
8234 elf_gc_allocate_got_offsets
,
8239 /* We need a special top-level link routine to convert got reference counts
8240 to real got offsets. */
8243 elf_gc_allocate_got_offsets (h
, offarg
)
8244 struct elf_link_hash_entry
*h
;
8247 bfd_vma
*off
= (bfd_vma
*) offarg
;
8249 if (h
->root
.type
== bfd_link_hash_warning
)
8250 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8252 if (h
->got
.refcount
> 0)
8254 h
->got
.offset
= off
[0];
8255 off
[0] += ARCH_SIZE
/ 8;
8258 h
->got
.offset
= (bfd_vma
) -1;
8263 /* Many folk need no more in the way of final link than this, once
8264 got entry reference counting is enabled. */
8267 elf_gc_common_final_link (abfd
, info
)
8269 struct bfd_link_info
*info
;
8271 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
8274 /* Invoke the regular ELF backend linker to do all the work. */
8275 return elf_bfd_final_link (abfd
, info
);
8278 /* This function will be called though elf_link_hash_traverse to store
8279 all hash value of the exported symbols in an array. */
8282 elf_collect_hash_codes (h
, data
)
8283 struct elf_link_hash_entry
*h
;
8286 unsigned long **valuep
= (unsigned long **) data
;
8292 if (h
->root
.type
== bfd_link_hash_warning
)
8293 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8295 /* Ignore indirect symbols. These are added by the versioning code. */
8296 if (h
->dynindx
== -1)
8299 name
= h
->root
.root
.string
;
8300 p
= strchr (name
, ELF_VER_CHR
);
8303 alc
= bfd_malloc ((bfd_size_type
) (p
- name
+ 1));
8304 memcpy (alc
, name
, (size_t) (p
- name
));
8305 alc
[p
- name
] = '\0';
8309 /* Compute the hash value. */
8310 ha
= bfd_elf_hash (name
);
8312 /* Store the found hash value in the array given as the argument. */
8315 /* And store it in the struct so that we can put it in the hash table
8317 h
->elf_hash_value
= ha
;
8326 elf_reloc_symbol_deleted_p (offset
, cookie
)
8330 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
8332 if (rcookie
->bad_symtab
)
8333 rcookie
->rel
= rcookie
->rels
;
8335 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
8337 unsigned long r_symndx
;
8339 if (! rcookie
->bad_symtab
)
8340 if (rcookie
->rel
->r_offset
> offset
)
8342 if (rcookie
->rel
->r_offset
!= offset
)
8345 r_symndx
= ELF_R_SYM (rcookie
->rel
->r_info
);
8346 if (r_symndx
== SHN_UNDEF
)
8349 if (r_symndx
>= rcookie
->locsymcount
8350 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
8352 struct elf_link_hash_entry
*h
;
8354 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
8356 while (h
->root
.type
== bfd_link_hash_indirect
8357 || h
->root
.type
== bfd_link_hash_warning
)
8358 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8360 if ((h
->root
.type
== bfd_link_hash_defined
8361 || h
->root
.type
== bfd_link_hash_defweak
)
8362 && elf_discarded_section (h
->root
.u
.def
.section
))
8369 /* It's not a relocation against a global symbol,
8370 but it could be a relocation against a local
8371 symbol for a discarded section. */
8373 Elf_Internal_Sym
*isym
;
8375 /* Need to: get the symbol; get the section. */
8376 isym
= &rcookie
->locsyms
[r_symndx
];
8377 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
8379 isec
= section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
8380 if (isec
!= NULL
&& elf_discarded_section (isec
))
8389 /* Discard unneeded references to discarded sections.
8390 Returns TRUE if any section's size was changed. */
8391 /* This function assumes that the relocations are in sorted order,
8392 which is true for all known assemblers. */
8395 elf_bfd_discard_info (output_bfd
, info
)
8397 struct bfd_link_info
*info
;
8399 struct elf_reloc_cookie cookie
;
8400 asection
*stab
, *eh
;
8401 Elf_Internal_Shdr
*symtab_hdr
;
8402 struct elf_backend_data
*bed
;
8405 bfd_boolean ret
= FALSE
;
8407 if (info
->traditional_format
8408 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
8409 || ! is_elf_hash_table (info
))
8412 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
8414 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
8417 bed
= get_elf_backend_data (abfd
);
8419 if ((abfd
->flags
& DYNAMIC
) != 0)
8422 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
8423 if (info
->relocateable
8425 && (eh
->_raw_size
== 0
8426 || bfd_is_abs_section (eh
->output_section
))))
8429 stab
= bfd_get_section_by_name (abfd
, ".stab");
8431 && (stab
->_raw_size
== 0
8432 || bfd_is_abs_section (stab
->output_section
)
8433 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
8438 && bed
->elf_backend_discard_info
== NULL
)
8441 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8443 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
8444 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
8445 if (cookie
.bad_symtab
)
8447 cookie
.locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8448 cookie
.extsymoff
= 0;
8452 cookie
.locsymcount
= symtab_hdr
->sh_info
;
8453 cookie
.extsymoff
= symtab_hdr
->sh_info
;
8456 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8457 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
8459 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
8460 cookie
.locsymcount
, 0,
8462 if (cookie
.locsyms
== NULL
)
8469 count
= stab
->reloc_count
;
8471 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8472 (abfd
, stab
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
8473 info
->keep_memory
));
8474 if (cookie
.rels
!= NULL
)
8476 cookie
.rel
= cookie
.rels
;
8477 cookie
.relend
= cookie
.rels
;
8478 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
8479 if (_bfd_discard_section_stabs (abfd
, stab
,
8480 elf_section_data (stab
)->sec_info
,
8481 elf_reloc_symbol_deleted_p
,
8484 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
8492 count
= eh
->reloc_count
;
8494 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8495 (abfd
, eh
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
8496 info
->keep_memory
));
8497 cookie
.rel
= cookie
.rels
;
8498 cookie
.relend
= cookie
.rels
;
8499 if (cookie
.rels
!= NULL
)
8500 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
8502 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
8503 elf_reloc_symbol_deleted_p
,
8507 if (cookie
.rels
!= NULL
8508 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
8512 if (bed
->elf_backend_discard_info
!= NULL
8513 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
8516 if (cookie
.locsyms
!= NULL
8517 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
8519 if (! info
->keep_memory
)
8520 free (cookie
.locsyms
);
8522 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
8526 if (info
->eh_frame_hdr
8527 && !info
->relocateable
8528 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
8535 elf_section_ignore_discarded_relocs (sec
)
8538 struct elf_backend_data
*bed
;
8540 switch (sec
->sec_info_type
)
8542 case ELF_INFO_TYPE_STABS
:
8543 case ELF_INFO_TYPE_EH_FRAME
:
8549 bed
= get_elf_backend_data (sec
->owner
);
8550 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
8551 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))