2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
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 boolean is_global_data_symbol_definition
34 PARAMS ((bfd
*, Elf_Internal_Sym
*));
35 static boolean elf_link_is_defined_archive_symbol
36 PARAMS ((bfd
*, carsym
*));
37 static boolean elf_link_add_object_symbols
38 PARAMS ((bfd
*, struct bfd_link_info
*));
39 static boolean elf_link_add_archive_symbols
40 PARAMS ((bfd
*, struct bfd_link_info
*));
41 static 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
**, boolean
*, boolean
*,
46 static 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 boolean
*, boolean
, boolean
));
50 static boolean elf_export_symbol
51 PARAMS ((struct elf_link_hash_entry
*, PTR
));
52 static boolean elf_finalize_dynstr
53 PARAMS ((bfd
*, struct bfd_link_info
*));
54 static boolean elf_fix_symbol_flags
55 PARAMS ((struct elf_link_hash_entry
*, struct elf_info_failed
*));
56 static boolean elf_adjust_dynamic_symbol
57 PARAMS ((struct elf_link_hash_entry
*, PTR
));
58 static boolean elf_link_find_version_dependencies
59 PARAMS ((struct elf_link_hash_entry
*, PTR
));
60 static boolean elf_link_assign_sym_version
61 PARAMS ((struct elf_link_hash_entry
*, PTR
));
62 static boolean elf_collect_hash_codes
63 PARAMS ((struct elf_link_hash_entry
*, PTR
));
64 static 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 boolean elf_link_output_relocs
69 PARAMS ((bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*));
70 static 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 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 boolean
*defined
= NULL
;
259 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 (boolean
);
281 defined
= (boolean
*) bfd_zmalloc (amt
);
282 included
= (boolean
*) bfd_zmalloc (amt
);
283 if (defined
== (boolean
*) NULL
|| included
== (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
!= (boolean
*) NULL
)
448 if (included
!= (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
;
476 boolean
*type_change_ok
;
477 boolean
*size_change_ok
;
481 struct elf_link_hash_entry
*h
;
484 boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
489 bind
= ELF_ST_BIND (sym
->st_info
);
491 if (! bfd_is_und_section (sec
))
492 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, false, false);
494 h
= ((struct elf_link_hash_entry
*)
495 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true, false, false));
500 /* This code is for coping with dynamic objects, and is only useful
501 if we are doing an ELF link. */
502 if (info
->hash
->creator
!= abfd
->xvec
)
505 /* For merging, we only care about real symbols. */
507 while (h
->root
.type
== bfd_link_hash_indirect
508 || h
->root
.type
== bfd_link_hash_warning
)
509 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
511 /* If we just created the symbol, mark it as being an ELF symbol.
512 Other than that, there is nothing to do--there is no merge issue
513 with a newly defined symbol--so we just return. */
515 if (h
->root
.type
== bfd_link_hash_new
)
517 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
521 /* OLDBFD is a BFD associated with the existing symbol. */
523 switch (h
->root
.type
)
529 case bfd_link_hash_undefined
:
530 case bfd_link_hash_undefweak
:
531 oldbfd
= h
->root
.u
.undef
.abfd
;
534 case bfd_link_hash_defined
:
535 case bfd_link_hash_defweak
:
536 oldbfd
= h
->root
.u
.def
.section
->owner
;
539 case bfd_link_hash_common
:
540 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
544 /* In cases involving weak versioned symbols, we may wind up trying
545 to merge a symbol with itself. Catch that here, to avoid the
546 confusion that results if we try to override a symbol with
547 itself. The additional tests catch cases like
548 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
549 dynamic object, which we do want to handle here. */
551 && ((abfd
->flags
& DYNAMIC
) == 0
552 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0))
555 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
556 respectively, is from a dynamic object. */
558 if ((abfd
->flags
& DYNAMIC
) != 0)
564 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
569 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
570 indices used by MIPS ELF. */
571 switch (h
->root
.type
)
577 case bfd_link_hash_defined
:
578 case bfd_link_hash_defweak
:
579 hsec
= h
->root
.u
.def
.section
;
582 case bfd_link_hash_common
:
583 hsec
= h
->root
.u
.c
.p
->section
;
590 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
593 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
594 respectively, appear to be a definition rather than reference. */
596 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
601 if (h
->root
.type
== bfd_link_hash_undefined
602 || h
->root
.type
== bfd_link_hash_undefweak
603 || h
->root
.type
== bfd_link_hash_common
)
608 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
609 symbol, respectively, appears to be a common symbol in a dynamic
610 object. If a symbol appears in an uninitialized section, and is
611 not weak, and is not a function, then it may be a common symbol
612 which was resolved when the dynamic object was created. We want
613 to treat such symbols specially, because they raise special
614 considerations when setting the symbol size: if the symbol
615 appears as a common symbol in a regular object, and the size in
616 the regular object is larger, we must make sure that we use the
617 larger size. This problematic case can always be avoided in C,
618 but it must be handled correctly when using Fortran shared
621 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
622 likewise for OLDDYNCOMMON and OLDDEF.
624 Note that this test is just a heuristic, and that it is quite
625 possible to have an uninitialized symbol in a shared object which
626 is really a definition, rather than a common symbol. This could
627 lead to some minor confusion when the symbol really is a common
628 symbol in some regular object. However, I think it will be
633 && (sec
->flags
& SEC_ALLOC
) != 0
634 && (sec
->flags
& SEC_LOAD
) == 0
637 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
640 newdyncommon
= false;
644 && h
->root
.type
== bfd_link_hash_defined
645 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
646 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
647 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
649 && h
->type
!= STT_FUNC
)
652 olddyncommon
= false;
654 /* It's OK to change the type if either the existing symbol or the
655 new symbol is weak unless it comes from a DT_NEEDED entry of
656 a shared object, in which case, the DT_NEEDED entry may not be
657 required at the run time. */
659 if ((! dt_needed
&& h
->root
.type
== bfd_link_hash_defweak
)
660 || h
->root
.type
== bfd_link_hash_undefweak
662 *type_change_ok
= true;
664 /* It's OK to change the size if either the existing symbol or the
665 new symbol is weak, or if the old symbol is undefined. */
668 || h
->root
.type
== bfd_link_hash_undefined
)
669 *size_change_ok
= true;
671 /* If both the old and the new symbols look like common symbols in a
672 dynamic object, set the size of the symbol to the larger of the
677 && sym
->st_size
!= h
->size
)
679 /* Since we think we have two common symbols, issue a multiple
680 common warning if desired. Note that we only warn if the
681 size is different. If the size is the same, we simply let
682 the old symbol override the new one as normally happens with
683 symbols defined in dynamic objects. */
685 if (! ((*info
->callbacks
->multiple_common
)
686 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
687 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
690 if (sym
->st_size
> h
->size
)
691 h
->size
= sym
->st_size
;
693 *size_change_ok
= true;
696 /* If we are looking at a dynamic object, and we have found a
697 definition, we need to see if the symbol was already defined by
698 some other object. If so, we want to use the existing
699 definition, and we do not want to report a multiple symbol
700 definition error; we do this by clobbering *PSEC to be
703 We treat a common symbol as a definition if the symbol in the
704 shared library is a function, since common symbols always
705 represent variables; this can cause confusion in principle, but
706 any such confusion would seem to indicate an erroneous program or
707 shared library. We also permit a common symbol in a regular
708 object to override a weak symbol in a shared object.
710 We prefer a non-weak definition in a shared library to a weak
711 definition in the executable unless it comes from a DT_NEEDED
712 entry of a shared object, in which case, the DT_NEEDED entry
713 may not be required at the run time. */
718 || (h
->root
.type
== bfd_link_hash_common
720 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)))
721 && (h
->root
.type
!= bfd_link_hash_defweak
723 || bind
== STB_WEAK
))
727 newdyncommon
= false;
729 *psec
= sec
= bfd_und_section_ptr
;
730 *size_change_ok
= true;
732 /* If we get here when the old symbol is a common symbol, then
733 we are explicitly letting it override a weak symbol or
734 function in a dynamic object, and we don't want to warn about
735 a type change. If the old symbol is a defined symbol, a type
736 change warning may still be appropriate. */
738 if (h
->root
.type
== bfd_link_hash_common
)
739 *type_change_ok
= true;
742 /* Handle the special case of an old common symbol merging with a
743 new symbol which looks like a common symbol in a shared object.
744 We change *PSEC and *PVALUE to make the new symbol look like a
745 common symbol, and let _bfd_generic_link_add_one_symbol will do
749 && h
->root
.type
== bfd_link_hash_common
)
753 newdyncommon
= false;
754 *pvalue
= sym
->st_size
;
755 *psec
= sec
= bfd_com_section_ptr
;
756 *size_change_ok
= true;
759 /* If the old symbol is from a dynamic object, and the new symbol is
760 a definition which is not from a dynamic object, then the new
761 symbol overrides the old symbol. Symbols from regular files
762 always take precedence over symbols from dynamic objects, even if
763 they are defined after the dynamic object in the link.
765 As above, we again permit a common symbol in a regular object to
766 override a definition in a shared object if the shared object
767 symbol is a function or is weak.
769 As above, we permit a non-weak definition in a shared object to
770 override a weak definition in a regular object. */
774 || (bfd_is_com_section (sec
)
775 && (h
->root
.type
== bfd_link_hash_defweak
776 || h
->type
== STT_FUNC
)))
779 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
781 || h
->root
.type
== bfd_link_hash_defweak
))
783 /* Change the hash table entry to undefined, and let
784 _bfd_generic_link_add_one_symbol do the right thing with the
787 h
->root
.type
= bfd_link_hash_undefined
;
788 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
789 *size_change_ok
= true;
792 olddyncommon
= false;
794 /* We again permit a type change when a common symbol may be
795 overriding a function. */
797 if (bfd_is_com_section (sec
))
798 *type_change_ok
= true;
800 /* This union may have been set to be non-NULL when this symbol
801 was seen in a dynamic object. We must force the union to be
802 NULL, so that it is correct for a regular symbol. */
804 h
->verinfo
.vertree
= NULL
;
806 /* In this special case, if H is the target of an indirection,
807 we want the caller to frob with H rather than with the
808 indirect symbol. That will permit the caller to redefine the
809 target of the indirection, rather than the indirect symbol
810 itself. FIXME: This will break the -y option if we store a
811 symbol with a different name. */
815 /* Handle the special case of a new common symbol merging with an
816 old symbol that looks like it might be a common symbol defined in
817 a shared object. Note that we have already handled the case in
818 which a new common symbol should simply override the definition
819 in the shared library. */
822 && bfd_is_com_section (sec
)
825 /* It would be best if we could set the hash table entry to a
826 common symbol, but we don't know what to use for the section
828 if (! ((*info
->callbacks
->multiple_common
)
829 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
830 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
833 /* If the predumed common symbol in the dynamic object is
834 larger, pretend that the new symbol has its size. */
836 if (h
->size
> *pvalue
)
839 /* FIXME: We no longer know the alignment required by the symbol
840 in the dynamic object, so we just wind up using the one from
841 the regular object. */
844 olddyncommon
= false;
846 h
->root
.type
= bfd_link_hash_undefined
;
847 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
849 *size_change_ok
= true;
850 *type_change_ok
= true;
852 h
->verinfo
.vertree
= NULL
;
855 /* Handle the special case of a weak definition in a regular object
856 followed by a non-weak definition in a shared object. In this
857 case, we prefer the definition in the shared object unless it
858 comes from a DT_NEEDED entry of a shared object, in which case,
859 the DT_NEEDED entry may not be required at the run time. */
862 && h
->root
.type
== bfd_link_hash_defweak
867 /* To make this work we have to frob the flags so that the rest
868 of the code does not think we are using the regular
870 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
871 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
872 else if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
873 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
874 h
->elf_link_hash_flags
&= ~ (ELF_LINK_HASH_DEF_REGULAR
875 | ELF_LINK_HASH_DEF_DYNAMIC
);
877 /* If H is the target of an indirection, we want the caller to
878 use H rather than the indirect symbol. Otherwise if we are
879 defining a new indirect symbol we will wind up attaching it
880 to the entry we are overriding. */
884 /* Handle the special case of a non-weak definition in a shared
885 object followed by a weak definition in a regular object. In
886 this case we prefer to definition in the shared object. To make
887 this work we have to tell the caller to not treat the new symbol
891 && h
->root
.type
!= bfd_link_hash_defweak
900 /* This function is called to create an indirect symbol from the
901 default for the symbol with the default version if needed. The
902 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
903 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
904 indicates if it comes from a DT_NEEDED entry of a shared object. */
907 elf_add_default_symbol (abfd
, info
, h
, name
, sym
, psec
, value
,
908 dynsym
, override
, dt_needed
)
910 struct bfd_link_info
*info
;
911 struct elf_link_hash_entry
*h
;
913 Elf_Internal_Sym
*sym
;
920 boolean type_change_ok
;
921 boolean size_change_ok
;
923 struct elf_link_hash_entry
*hi
;
924 struct elf_backend_data
*bed
;
928 size_t len
, shortlen
;
931 /* If this symbol has a version, and it is the default version, we
932 create an indirect symbol from the default name to the fully
933 decorated name. This will cause external references which do not
934 specify a version to be bound to this version of the symbol. */
935 p
= strchr (name
, ELF_VER_CHR
);
936 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
941 /* We are overridden by an old defition. We need to check if we
942 need to create the indirect symbol from the default name. */
943 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, true,
945 BFD_ASSERT (hi
!= NULL
);
948 while (hi
->root
.type
== bfd_link_hash_indirect
949 || hi
->root
.type
== bfd_link_hash_warning
)
951 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
957 bed
= get_elf_backend_data (abfd
);
958 collect
= bed
->collect
;
959 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
962 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
963 if (shortname
== NULL
)
965 memcpy (shortname
, name
, shortlen
);
966 shortname
[shortlen
] = '\0';
968 /* We are going to create a new symbol. Merge it with any existing
969 symbol with this name. For the purposes of the merge, act as
970 though we were defining the symbol we just defined, although we
971 actually going to define an indirect symbol. */
972 type_change_ok
= false;
973 size_change_ok
= false;
975 if (! elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
976 &hi
, &override
, &type_change_ok
,
977 &size_change_ok
, dt_needed
))
982 if (! (_bfd_generic_link_add_one_symbol
983 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
984 (bfd_vma
) 0, name
, false, collect
,
985 (struct bfd_link_hash_entry
**) &hi
)))
990 /* In this case the symbol named SHORTNAME is overriding the
991 indirect symbol we want to add. We were planning on making
992 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
993 is the name without a version. NAME is the fully versioned
994 name, and it is the default version.
996 Overriding means that we already saw a definition for the
997 symbol SHORTNAME in a regular object, and it is overriding
998 the symbol defined in the dynamic object.
1000 When this happens, we actually want to change NAME, the
1001 symbol we just added, to refer to SHORTNAME. This will cause
1002 references to NAME in the shared object to become references
1003 to SHORTNAME in the regular object. This is what we expect
1004 when we override a function in a shared object: that the
1005 references in the shared object will be mapped to the
1006 definition in the regular object. */
1008 while (hi
->root
.type
== bfd_link_hash_indirect
1009 || hi
->root
.type
== bfd_link_hash_warning
)
1010 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1012 h
->root
.type
= bfd_link_hash_indirect
;
1013 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1014 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1016 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1017 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1018 if (hi
->elf_link_hash_flags
1019 & (ELF_LINK_HASH_REF_REGULAR
1020 | ELF_LINK_HASH_DEF_REGULAR
))
1022 if (! _bfd_elf_link_record_dynamic_symbol (info
, hi
))
1027 /* Now set HI to H, so that the following code will set the
1028 other fields correctly. */
1032 /* If there is a duplicate definition somewhere, then HI may not
1033 point to an indirect symbol. We will have reported an error to
1034 the user in that case. */
1036 if (hi
->root
.type
== bfd_link_hash_indirect
)
1038 struct elf_link_hash_entry
*ht
;
1040 /* If the symbol became indirect, then we assume that we have
1041 not seen a definition before. */
1042 BFD_ASSERT ((hi
->elf_link_hash_flags
1043 & (ELF_LINK_HASH_DEF_DYNAMIC
1044 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1046 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1047 (*bed
->elf_backend_copy_indirect_symbol
) (ht
, hi
);
1049 /* See if the new flags lead us to realize that the symbol must
1056 || ((hi
->elf_link_hash_flags
1057 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1062 if ((hi
->elf_link_hash_flags
1063 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1069 /* We also need to define an indirection from the nondefault version
1072 len
= strlen (name
);
1073 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1074 if (shortname
== NULL
)
1076 memcpy (shortname
, name
, shortlen
);
1077 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1079 /* Once again, merge with any existing symbol. */
1080 type_change_ok
= false;
1081 size_change_ok
= false;
1083 if (! elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1084 &hi
, &override
, &type_change_ok
,
1085 &size_change_ok
, dt_needed
))
1090 /* Here SHORTNAME is a versioned name, so we don't expect to see
1091 the type of override we do in the case above unless it is
1092 overridden by a versioned definiton. */
1093 if (hi
->root
.type
!= bfd_link_hash_defined
1094 && hi
->root
.type
!= bfd_link_hash_defweak
)
1095 (*_bfd_error_handler
)
1096 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1097 bfd_archive_filename (abfd
), shortname
);
1101 if (! (_bfd_generic_link_add_one_symbol
1102 (info
, abfd
, shortname
, BSF_INDIRECT
,
1103 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1104 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1107 /* If there is a duplicate definition somewhere, then HI may not
1108 point to an indirect symbol. We will have reported an error
1109 to the user in that case. */
1111 if (hi
->root
.type
== bfd_link_hash_indirect
)
1113 /* If the symbol became indirect, then we assume that we have
1114 not seen a definition before. */
1115 BFD_ASSERT ((hi
->elf_link_hash_flags
1116 & (ELF_LINK_HASH_DEF_DYNAMIC
1117 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1119 (*bed
->elf_backend_copy_indirect_symbol
) (h
, hi
);
1121 /* See if the new flags lead us to realize that the symbol
1128 || ((hi
->elf_link_hash_flags
1129 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1134 if ((hi
->elf_link_hash_flags
1135 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1145 /* Add symbols from an ELF object file to the linker hash table. */
1148 elf_link_add_object_symbols (abfd
, info
)
1150 struct bfd_link_info
*info
;
1152 boolean (*add_symbol_hook
) PARAMS ((bfd
*, struct bfd_link_info
*,
1153 const Elf_Internal_Sym
*,
1154 const char **, flagword
*,
1155 asection
**, bfd_vma
*));
1156 boolean (*check_relocs
) PARAMS ((bfd
*, struct bfd_link_info
*,
1157 asection
*, const Elf_Internal_Rela
*));
1159 Elf_Internal_Shdr
*hdr
;
1160 bfd_size_type symcount
;
1161 bfd_size_type extsymcount
;
1162 bfd_size_type extsymoff
;
1163 struct elf_link_hash_entry
**sym_hash
;
1165 Elf_External_Versym
*extversym
= NULL
;
1166 Elf_External_Versym
*ever
;
1167 struct elf_link_hash_entry
*weaks
;
1168 Elf_Internal_Sym
*isymbuf
= NULL
;
1169 Elf_Internal_Sym
*isym
;
1170 Elf_Internal_Sym
*isymend
;
1171 struct elf_backend_data
*bed
;
1173 struct elf_link_hash_table
* hash_table
;
1176 hash_table
= elf_hash_table (info
);
1178 bed
= get_elf_backend_data (abfd
);
1179 add_symbol_hook
= bed
->elf_add_symbol_hook
;
1180 collect
= bed
->collect
;
1182 if ((abfd
->flags
& DYNAMIC
) == 0)
1188 /* You can't use -r against a dynamic object. Also, there's no
1189 hope of using a dynamic object which does not exactly match
1190 the format of the output file. */
1191 if (info
->relocateable
|| info
->hash
->creator
!= abfd
->xvec
)
1193 bfd_set_error (bfd_error_invalid_operation
);
1198 /* As a GNU extension, any input sections which are named
1199 .gnu.warning.SYMBOL are treated as warning symbols for the given
1200 symbol. This differs from .gnu.warning sections, which generate
1201 warnings when they are included in an output file. */
1206 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
1210 name
= bfd_get_section_name (abfd
, s
);
1211 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1216 name
+= sizeof ".gnu.warning." - 1;
1218 /* If this is a shared object, then look up the symbol
1219 in the hash table. If it is there, and it is already
1220 been defined, then we will not be using the entry
1221 from this shared object, so we don't need to warn.
1222 FIXME: If we see the definition in a regular object
1223 later on, we will warn, but we shouldn't. The only
1224 fix is to keep track of what warnings we are supposed
1225 to emit, and then handle them all at the end of the
1227 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
1229 struct elf_link_hash_entry
*h
;
1231 h
= elf_link_hash_lookup (hash_table
, name
,
1232 false, false, true);
1234 /* FIXME: What about bfd_link_hash_common? */
1236 && (h
->root
.type
== bfd_link_hash_defined
1237 || h
->root
.type
== bfd_link_hash_defweak
))
1239 /* We don't want to issue this warning. Clobber
1240 the section size so that the warning does not
1241 get copied into the output file. */
1247 sz
= bfd_section_size (abfd
, s
);
1248 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
1252 if (! bfd_get_section_contents (abfd
, s
, msg
, (file_ptr
) 0, sz
))
1257 if (! (_bfd_generic_link_add_one_symbol
1258 (info
, abfd
, name
, BSF_WARNING
, s
, (bfd_vma
) 0, msg
,
1259 false, collect
, (struct bfd_link_hash_entry
**) NULL
)))
1262 if (! info
->relocateable
)
1264 /* Clobber the section size so that the warning does
1265 not get copied into the output file. */
1275 /* If we are creating a shared library, create all the dynamic
1276 sections immediately. We need to attach them to something,
1277 so we attach them to this BFD, provided it is the right
1278 format. FIXME: If there are no input BFD's of the same
1279 format as the output, we can't make a shared library. */
1281 && is_elf_hash_table (info
)
1282 && ! hash_table
->dynamic_sections_created
1283 && abfd
->xvec
== info
->hash
->creator
)
1285 if (! elf_link_create_dynamic_sections (abfd
, info
))
1289 else if (! is_elf_hash_table (info
))
1296 bfd_size_type oldsize
;
1297 bfd_size_type strindex
;
1299 /* Find the name to use in a DT_NEEDED entry that refers to this
1300 object. If the object has a DT_SONAME entry, we use it.
1301 Otherwise, if the generic linker stuck something in
1302 elf_dt_name, we use that. Otherwise, we just use the file
1303 name. If the generic linker put a null string into
1304 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1305 there is a DT_SONAME entry. */
1307 name
= bfd_get_filename (abfd
);
1308 if (elf_dt_name (abfd
) != NULL
)
1310 name
= elf_dt_name (abfd
);
1313 if (elf_dt_soname (abfd
) != NULL
)
1319 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1322 Elf_External_Dyn
*dynbuf
= NULL
;
1323 Elf_External_Dyn
*extdyn
;
1324 Elf_External_Dyn
*extdynend
;
1326 unsigned long shlink
;
1330 dynbuf
= (Elf_External_Dyn
*) bfd_malloc (s
->_raw_size
);
1334 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
1335 (file_ptr
) 0, s
->_raw_size
))
1336 goto error_free_dyn
;
1338 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1340 goto error_free_dyn
;
1341 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1344 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
1347 for (; extdyn
< extdynend
; extdyn
++)
1349 Elf_Internal_Dyn dyn
;
1351 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
1352 if (dyn
.d_tag
== DT_SONAME
)
1354 unsigned int tagv
= dyn
.d_un
.d_val
;
1355 name
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1357 goto error_free_dyn
;
1359 if (dyn
.d_tag
== DT_NEEDED
)
1361 struct bfd_link_needed_list
*n
, **pn
;
1363 unsigned int tagv
= dyn
.d_un
.d_val
;
1365 amt
= sizeof (struct bfd_link_needed_list
);
1366 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1367 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1368 if (n
== NULL
|| fnm
== NULL
)
1369 goto error_free_dyn
;
1370 amt
= strlen (fnm
) + 1;
1371 anm
= bfd_alloc (abfd
, amt
);
1373 goto error_free_dyn
;
1374 memcpy (anm
, fnm
, (size_t) amt
);
1378 for (pn
= & hash_table
->needed
;
1384 if (dyn
.d_tag
== DT_RUNPATH
)
1386 struct bfd_link_needed_list
*n
, **pn
;
1388 unsigned int tagv
= dyn
.d_un
.d_val
;
1390 /* When we see DT_RPATH before DT_RUNPATH, we have
1391 to clear runpath. Do _NOT_ bfd_release, as that
1392 frees all more recently bfd_alloc'd blocks as
1394 if (rpath
&& hash_table
->runpath
)
1395 hash_table
->runpath
= NULL
;
1397 amt
= sizeof (struct bfd_link_needed_list
);
1398 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1399 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1400 if (n
== NULL
|| fnm
== NULL
)
1401 goto error_free_dyn
;
1402 amt
= strlen (fnm
) + 1;
1403 anm
= bfd_alloc (abfd
, amt
);
1405 goto error_free_dyn
;
1406 memcpy (anm
, fnm
, (size_t) amt
);
1410 for (pn
= & hash_table
->runpath
;
1418 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1419 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
1421 struct bfd_link_needed_list
*n
, **pn
;
1423 unsigned int tagv
= dyn
.d_un
.d_val
;
1425 amt
= sizeof (struct bfd_link_needed_list
);
1426 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1427 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1428 if (n
== NULL
|| fnm
== NULL
)
1429 goto error_free_dyn
;
1430 amt
= strlen (fnm
) + 1;
1431 anm
= bfd_alloc (abfd
, amt
);
1438 memcpy (anm
, fnm
, (size_t) amt
);
1442 for (pn
= & hash_table
->runpath
;
1454 /* We do not want to include any of the sections in a dynamic
1455 object in the output file. We hack by simply clobbering the
1456 list of sections in the BFD. This could be handled more
1457 cleanly by, say, a new section flag; the existing
1458 SEC_NEVER_LOAD flag is not the one we want, because that one
1459 still implies that the section takes up space in the output
1461 bfd_section_list_clear (abfd
);
1463 /* If this is the first dynamic object found in the link, create
1464 the special sections required for dynamic linking. */
1465 if (! hash_table
->dynamic_sections_created
)
1466 if (! elf_link_create_dynamic_sections (abfd
, info
))
1471 /* Add a DT_NEEDED entry for this dynamic object. */
1472 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
1473 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, name
, false);
1474 if (strindex
== (bfd_size_type
) -1)
1477 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
1480 Elf_External_Dyn
*dyncon
, *dynconend
;
1482 /* The hash table size did not change, which means that
1483 the dynamic object name was already entered. If we
1484 have already included this dynamic object in the
1485 link, just ignore it. There is no reason to include
1486 a particular dynamic object more than once. */
1487 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
1488 BFD_ASSERT (sdyn
!= NULL
);
1490 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1491 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1493 for (; dyncon
< dynconend
; dyncon
++)
1495 Elf_Internal_Dyn dyn
;
1497 elf_swap_dyn_in (hash_table
->dynobj
, dyncon
, & dyn
);
1498 if (dyn
.d_tag
== DT_NEEDED
1499 && dyn
.d_un
.d_val
== strindex
)
1501 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
1507 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
1511 /* Save the SONAME, if there is one, because sometimes the
1512 linker emulation code will need to know it. */
1514 name
= basename (bfd_get_filename (abfd
));
1515 elf_dt_name (abfd
) = name
;
1518 /* If this is a dynamic object, we always link against the .dynsym
1519 symbol table, not the .symtab symbol table. The dynamic linker
1520 will only see the .dynsym symbol table, so there is no reason to
1521 look at .symtab for a dynamic object. */
1523 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
1524 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1526 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1528 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
1530 /* The sh_info field of the symtab header tells us where the
1531 external symbols start. We don't care about the local symbols at
1533 if (elf_bad_symtab (abfd
))
1535 extsymcount
= symcount
;
1540 extsymcount
= symcount
- hdr
->sh_info
;
1541 extsymoff
= hdr
->sh_info
;
1545 if (extsymcount
!= 0)
1547 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
1549 if (isymbuf
== NULL
)
1552 /* We store a pointer to the hash table entry for each external
1554 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
1555 sym_hash
= (struct elf_link_hash_entry
**) bfd_alloc (abfd
, amt
);
1556 if (sym_hash
== NULL
)
1557 goto error_free_sym
;
1558 elf_sym_hashes (abfd
) = sym_hash
;
1563 /* Read in any version definitions. */
1564 if (! _bfd_elf_slurp_version_tables (abfd
))
1565 goto error_free_sym
;
1567 /* Read in the symbol versions, but don't bother to convert them
1568 to internal format. */
1569 if (elf_dynversym (abfd
) != 0)
1571 Elf_Internal_Shdr
*versymhdr
;
1573 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
1574 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
1575 if (extversym
== NULL
)
1576 goto error_free_sym
;
1577 amt
= versymhdr
->sh_size
;
1578 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
1579 || bfd_bread ((PTR
) extversym
, amt
, abfd
) != amt
)
1580 goto error_free_vers
;
1586 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
1587 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
1589 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
1596 struct elf_link_hash_entry
*h
;
1598 boolean size_change_ok
, type_change_ok
;
1599 boolean new_weakdef
;
1600 unsigned int old_alignment
;
1605 flags
= BSF_NO_FLAGS
;
1607 value
= isym
->st_value
;
1610 bind
= ELF_ST_BIND (isym
->st_info
);
1611 if (bind
== STB_LOCAL
)
1613 /* This should be impossible, since ELF requires that all
1614 global symbols follow all local symbols, and that sh_info
1615 point to the first global symbol. Unfortunatealy, Irix 5
1619 else if (bind
== STB_GLOBAL
)
1621 if (isym
->st_shndx
!= SHN_UNDEF
1622 && isym
->st_shndx
!= SHN_COMMON
)
1625 else if (bind
== STB_WEAK
)
1629 /* Leave it up to the processor backend. */
1632 if (isym
->st_shndx
== SHN_UNDEF
)
1633 sec
= bfd_und_section_ptr
;
1634 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
1636 sec
= section_from_elf_index (abfd
, isym
->st_shndx
);
1638 sec
= bfd_abs_section_ptr
;
1639 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
1642 else if (isym
->st_shndx
== SHN_ABS
)
1643 sec
= bfd_abs_section_ptr
;
1644 else if (isym
->st_shndx
== SHN_COMMON
)
1646 sec
= bfd_com_section_ptr
;
1647 /* What ELF calls the size we call the value. What ELF
1648 calls the value we call the alignment. */
1649 value
= isym
->st_size
;
1653 /* Leave it up to the processor backend. */
1656 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
1658 if (name
== (const char *) NULL
)
1659 goto error_free_vers
;
1661 if (isym
->st_shndx
== SHN_COMMON
1662 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
1664 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
1668 tcomm
= bfd_make_section (abfd
, ".tcommon");
1670 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
1672 | SEC_LINKER_CREATED
1673 | SEC_THREAD_LOCAL
)))
1674 goto error_free_vers
;
1678 else if (add_symbol_hook
)
1680 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
1682 goto error_free_vers
;
1684 /* The hook function sets the name to NULL if this symbol
1685 should be skipped for some reason. */
1686 if (name
== (const char *) NULL
)
1690 /* Sanity check that all possibilities were handled. */
1691 if (sec
== (asection
*) NULL
)
1693 bfd_set_error (bfd_error_bad_value
);
1694 goto error_free_vers
;
1697 if (bfd_is_und_section (sec
)
1698 || bfd_is_com_section (sec
))
1703 size_change_ok
= false;
1704 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
1706 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1708 Elf_Internal_Versym iver
;
1709 unsigned int vernum
= 0;
1713 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
1714 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
1716 /* If this is a hidden symbol, or if it is not version
1717 1, we append the version name to the symbol name.
1718 However, we do not modify a non-hidden absolute
1719 symbol, because it might be the version symbol
1720 itself. FIXME: What if it isn't? */
1721 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
1722 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
1725 size_t namelen
, verlen
, newlen
;
1728 if (isym
->st_shndx
!= SHN_UNDEF
)
1730 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
1732 (*_bfd_error_handler
)
1733 (_("%s: %s: invalid version %u (max %d)"),
1734 bfd_archive_filename (abfd
), name
, vernum
,
1735 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1736 bfd_set_error (bfd_error_bad_value
);
1737 goto error_free_vers
;
1739 else if (vernum
> 1)
1741 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1747 /* We cannot simply test for the number of
1748 entries in the VERNEED section since the
1749 numbers for the needed versions do not start
1751 Elf_Internal_Verneed
*t
;
1754 for (t
= elf_tdata (abfd
)->verref
;
1758 Elf_Internal_Vernaux
*a
;
1760 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1762 if (a
->vna_other
== vernum
)
1764 verstr
= a
->vna_nodename
;
1773 (*_bfd_error_handler
)
1774 (_("%s: %s: invalid needed version %d"),
1775 bfd_archive_filename (abfd
), name
, vernum
);
1776 bfd_set_error (bfd_error_bad_value
);
1777 goto error_free_vers
;
1781 namelen
= strlen (name
);
1782 verlen
= strlen (verstr
);
1783 newlen
= namelen
+ verlen
+ 2;
1784 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1785 && isym
->st_shndx
!= SHN_UNDEF
)
1788 newname
= (char *) bfd_alloc (abfd
, (bfd_size_type
) newlen
);
1789 if (newname
== NULL
)
1790 goto error_free_vers
;
1791 memcpy (newname
, name
, namelen
);
1792 p
= newname
+ namelen
;
1794 /* If this is a defined non-hidden version symbol,
1795 we add another @ to the name. This indicates the
1796 default version of the symbol. */
1797 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1798 && isym
->st_shndx
!= SHN_UNDEF
)
1800 memcpy (p
, verstr
, verlen
+ 1);
1806 if (! elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
1807 sym_hash
, &override
, &type_change_ok
,
1808 &size_change_ok
, dt_needed
))
1809 goto error_free_vers
;
1815 while (h
->root
.type
== bfd_link_hash_indirect
1816 || h
->root
.type
== bfd_link_hash_warning
)
1817 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1819 /* Remember the old alignment if this is a common symbol, so
1820 that we don't reduce the alignment later on. We can't
1821 check later, because _bfd_generic_link_add_one_symbol
1822 will set a default for the alignment which we want to
1824 if (h
->root
.type
== bfd_link_hash_common
)
1825 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1827 if (elf_tdata (abfd
)->verdef
!= NULL
1831 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1834 if (! (_bfd_generic_link_add_one_symbol
1835 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
1836 false, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
1837 goto error_free_vers
;
1840 while (h
->root
.type
== bfd_link_hash_indirect
1841 || h
->root
.type
== bfd_link_hash_warning
)
1842 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1845 new_weakdef
= false;
1848 && (flags
& BSF_WEAK
) != 0
1849 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
1850 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1851 && h
->weakdef
== NULL
)
1853 /* Keep a list of all weak defined non function symbols from
1854 a dynamic object, using the weakdef field. Later in this
1855 function we will set the weakdef field to the correct
1856 value. We only put non-function symbols from dynamic
1857 objects on this list, because that happens to be the only
1858 time we need to know the normal symbol corresponding to a
1859 weak symbol, and the information is time consuming to
1860 figure out. If the weakdef field is not already NULL,
1861 then this symbol was already defined by some previous
1862 dynamic object, and we will be using that previous
1863 definition anyhow. */
1870 /* Set the alignment of a common symbol. */
1871 if (isym
->st_shndx
== SHN_COMMON
1872 && h
->root
.type
== bfd_link_hash_common
)
1876 align
= bfd_log2 (isym
->st_value
);
1877 if (align
> old_alignment
1878 /* Permit an alignment power of zero if an alignment of one
1879 is specified and no other alignments have been specified. */
1880 || (isym
->st_value
== 1 && old_alignment
== 0))
1881 h
->root
.u
.c
.p
->alignment_power
= align
;
1884 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1890 /* Remember the symbol size and type. */
1891 if (isym
->st_size
!= 0
1892 && (definition
|| h
->size
== 0))
1894 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
1895 (*_bfd_error_handler
)
1896 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1897 name
, (unsigned long) h
->size
,
1898 (unsigned long) isym
->st_size
, bfd_archive_filename (abfd
));
1900 h
->size
= isym
->st_size
;
1903 /* If this is a common symbol, then we always want H->SIZE
1904 to be the size of the common symbol. The code just above
1905 won't fix the size if a common symbol becomes larger. We
1906 don't warn about a size change here, because that is
1907 covered by --warn-common. */
1908 if (h
->root
.type
== bfd_link_hash_common
)
1909 h
->size
= h
->root
.u
.c
.size
;
1911 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
1912 && (definition
|| h
->type
== STT_NOTYPE
))
1914 if (h
->type
!= STT_NOTYPE
1915 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
1916 && ! type_change_ok
)
1917 (*_bfd_error_handler
)
1918 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1919 name
, h
->type
, ELF_ST_TYPE (isym
->st_info
),
1920 bfd_archive_filename (abfd
));
1922 h
->type
= ELF_ST_TYPE (isym
->st_info
);
1925 /* If st_other has a processor-specific meaning, specific code
1926 might be needed here. */
1927 if (isym
->st_other
!= 0)
1929 /* Combine visibilities, using the most constraining one. */
1930 unsigned char hvis
= ELF_ST_VISIBILITY (h
->other
);
1931 unsigned char symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
1933 if (symvis
&& (hvis
> symvis
|| hvis
== 0))
1934 h
->other
= isym
->st_other
;
1936 /* If neither has visibility, use the st_other of the
1937 definition. This is an arbitrary choice, since the
1938 other bits have no general meaning. */
1939 if (!symvis
&& !hvis
1940 && (definition
|| h
->other
== 0))
1941 h
->other
= isym
->st_other
;
1944 /* Set a flag in the hash table entry indicating the type of
1945 reference or definition we just found. Keep a count of
1946 the number of dynamic symbols we find. A dynamic symbol
1947 is one which is referenced or defined by both a regular
1948 object and a shared object. */
1949 old_flags
= h
->elf_link_hash_flags
;
1955 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
1956 if (bind
!= STB_WEAK
)
1957 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
1960 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
1962 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1963 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
1969 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
1971 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
1972 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
1973 | ELF_LINK_HASH_REF_REGULAR
)) != 0
1974 || (h
->weakdef
!= NULL
1976 && h
->weakdef
->dynindx
!= -1))
1980 h
->elf_link_hash_flags
|= new_flag
;
1982 /* Check to see if we need to add an indirect symbol for
1983 the default name. */
1984 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
1985 if (! elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
1986 &sec
, &value
, &dynsym
,
1987 override
, dt_needed
))
1988 goto error_free_vers
;
1990 if (dynsym
&& h
->dynindx
== -1)
1992 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1993 goto error_free_vers
;
1994 if (h
->weakdef
!= NULL
1996 && h
->weakdef
->dynindx
== -1)
1998 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
1999 goto error_free_vers
;
2002 else if (dynsym
&& h
->dynindx
!= -1)
2003 /* If the symbol already has a dynamic index, but
2004 visibility says it should not be visible, turn it into
2006 switch (ELF_ST_VISIBILITY (h
->other
))
2010 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2014 if (dt_needed
&& definition
2015 && (h
->elf_link_hash_flags
2016 & ELF_LINK_HASH_REF_REGULAR
) != 0)
2018 bfd_size_type oldsize
;
2019 bfd_size_type strindex
;
2021 if (! is_elf_hash_table (info
))
2022 goto error_free_vers
;
2024 /* The symbol from a DT_NEEDED object is referenced from
2025 the regular object to create a dynamic executable. We
2026 have to make sure there is a DT_NEEDED entry for it. */
2029 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2030 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
,
2031 elf_dt_soname (abfd
), false);
2032 if (strindex
== (bfd_size_type
) -1)
2033 goto error_free_vers
;
2035 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2038 Elf_External_Dyn
*dyncon
, *dynconend
;
2040 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
,
2042 BFD_ASSERT (sdyn
!= NULL
);
2044 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
2045 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
2047 for (; dyncon
< dynconend
; dyncon
++)
2049 Elf_Internal_Dyn dyn
;
2051 elf_swap_dyn_in (hash_table
->dynobj
,
2053 BFD_ASSERT (dyn
.d_tag
!= DT_NEEDED
||
2054 dyn
.d_un
.d_val
!= strindex
);
2058 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
2059 goto error_free_vers
;
2064 if (extversym
!= NULL
)
2070 if (isymbuf
!= NULL
)
2074 /* Now set the weakdefs field correctly for all the weak defined
2075 symbols we found. The only way to do this is to search all the
2076 symbols. Since we only need the information for non functions in
2077 dynamic objects, that's the only time we actually put anything on
2078 the list WEAKS. We need this information so that if a regular
2079 object refers to a symbol defined weakly in a dynamic object, the
2080 real symbol in the dynamic object is also put in the dynamic
2081 symbols; we also must arrange for both symbols to point to the
2082 same memory location. We could handle the general case of symbol
2083 aliasing, but a general symbol alias can only be generated in
2084 assembler code, handling it correctly would be very time
2085 consuming, and other ELF linkers don't handle general aliasing
2087 while (weaks
!= NULL
)
2089 struct elf_link_hash_entry
*hlook
;
2092 struct elf_link_hash_entry
**hpp
;
2093 struct elf_link_hash_entry
**hppend
;
2096 weaks
= hlook
->weakdef
;
2097 hlook
->weakdef
= NULL
;
2099 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
2100 || hlook
->root
.type
== bfd_link_hash_defweak
2101 || hlook
->root
.type
== bfd_link_hash_common
2102 || hlook
->root
.type
== bfd_link_hash_indirect
);
2103 slook
= hlook
->root
.u
.def
.section
;
2104 vlook
= hlook
->root
.u
.def
.value
;
2106 hpp
= elf_sym_hashes (abfd
);
2107 hppend
= hpp
+ extsymcount
;
2108 for (; hpp
< hppend
; hpp
++)
2110 struct elf_link_hash_entry
*h
;
2113 if (h
!= NULL
&& h
!= hlook
2114 && h
->root
.type
== bfd_link_hash_defined
2115 && h
->root
.u
.def
.section
== slook
2116 && h
->root
.u
.def
.value
== vlook
)
2120 /* If the weak definition is in the list of dynamic
2121 symbols, make sure the real definition is put there
2123 if (hlook
->dynindx
!= -1
2124 && h
->dynindx
== -1)
2126 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2130 /* If the real definition is in the list of dynamic
2131 symbols, make sure the weak definition is put there
2132 as well. If we don't do this, then the dynamic
2133 loader might not merge the entries for the real
2134 definition and the weak definition. */
2135 if (h
->dynindx
!= -1
2136 && hlook
->dynindx
== -1)
2138 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
2146 /* If this object is the same format as the output object, and it is
2147 not a shared library, then let the backend look through the
2150 This is required to build global offset table entries and to
2151 arrange for dynamic relocs. It is not required for the
2152 particular common case of linking non PIC code, even when linking
2153 against shared libraries, but unfortunately there is no way of
2154 knowing whether an object file has been compiled PIC or not.
2155 Looking through the relocs is not particularly time consuming.
2156 The problem is that we must either (1) keep the relocs in memory,
2157 which causes the linker to require additional runtime memory or
2158 (2) read the relocs twice from the input file, which wastes time.
2159 This would be a good case for using mmap.
2161 I have no idea how to handle linking PIC code into a file of a
2162 different format. It probably can't be done. */
2163 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
2165 && abfd
->xvec
== info
->hash
->creator
2166 && check_relocs
!= NULL
)
2170 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2172 Elf_Internal_Rela
*internal_relocs
;
2175 if ((o
->flags
& SEC_RELOC
) == 0
2176 || o
->reloc_count
== 0
2177 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
2178 && (o
->flags
& SEC_DEBUGGING
) != 0)
2179 || bfd_is_abs_section (o
->output_section
))
2182 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
2183 (abfd
, o
, (PTR
) NULL
,
2184 (Elf_Internal_Rela
*) NULL
,
2185 info
->keep_memory
));
2186 if (internal_relocs
== NULL
)
2189 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
2191 if (elf_section_data (o
)->relocs
!= internal_relocs
)
2192 free (internal_relocs
);
2199 /* If this is a non-traditional, non-relocateable link, try to
2200 optimize the handling of the .stab/.stabstr sections. */
2202 && ! info
->relocateable
2203 && ! info
->traditional_format
2204 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
2205 && is_elf_hash_table (info
)
2206 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
2208 asection
*stab
, *stabstr
;
2210 stab
= bfd_get_section_by_name (abfd
, ".stab");
2212 && (stab
->flags
& SEC_MERGE
) == 0
2213 && !bfd_is_abs_section (stab
->output_section
))
2215 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
2217 if (stabstr
!= NULL
)
2219 struct bfd_elf_section_data
*secdata
;
2221 secdata
= elf_section_data (stab
);
2222 if (! _bfd_link_section_stabs (abfd
,
2223 & hash_table
->stab_info
,
2225 &secdata
->sec_info
))
2227 if (secdata
->sec_info
)
2228 secdata
->sec_info_type
= ELF_INFO_TYPE_STABS
;
2233 if (! info
->relocateable
&& ! dynamic
2234 && is_elf_hash_table (info
))
2238 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2239 if ((s
->flags
& SEC_MERGE
) != 0
2240 && !bfd_is_abs_section (s
->output_section
))
2242 struct bfd_elf_section_data
*secdata
;
2244 secdata
= elf_section_data (s
);
2245 if (! _bfd_merge_section (abfd
,
2246 & hash_table
->merge_info
,
2247 s
, &secdata
->sec_info
))
2249 else if (secdata
->sec_info
)
2250 secdata
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
2254 if (is_elf_hash_table (info
))
2256 /* Add this bfd to the loaded list. */
2257 struct elf_link_loaded_list
*n
;
2259 n
= ((struct elf_link_loaded_list
*)
2260 bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
)));
2264 n
->next
= hash_table
->loaded
;
2265 hash_table
->loaded
= n
;
2271 if (extversym
!= NULL
)
2274 if (isymbuf
!= NULL
)
2280 /* Create some sections which will be filled in with dynamic linking
2281 information. ABFD is an input file which requires dynamic sections
2282 to be created. The dynamic sections take up virtual memory space
2283 when the final executable is run, so we need to create them before
2284 addresses are assigned to the output sections. We work out the
2285 actual contents and size of these sections later. */
2288 elf_link_create_dynamic_sections (abfd
, info
)
2290 struct bfd_link_info
*info
;
2293 register asection
*s
;
2294 struct elf_link_hash_entry
*h
;
2295 struct elf_backend_data
*bed
;
2297 if (! is_elf_hash_table (info
))
2300 if (elf_hash_table (info
)->dynamic_sections_created
)
2303 /* Make sure that all dynamic sections use the same input BFD. */
2304 if (elf_hash_table (info
)->dynobj
== NULL
)
2305 elf_hash_table (info
)->dynobj
= abfd
;
2307 abfd
= elf_hash_table (info
)->dynobj
;
2309 /* Note that we set the SEC_IN_MEMORY flag for all of these
2311 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
2312 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
2314 /* A dynamically linked executable has a .interp section, but a
2315 shared library does not. */
2318 s
= bfd_make_section (abfd
, ".interp");
2320 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2324 if (! info
->traditional_format
2325 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
2327 s
= bfd_make_section (abfd
, ".eh_frame_hdr");
2329 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2330 || ! bfd_set_section_alignment (abfd
, s
, 2))
2334 /* Create sections to hold version informations. These are removed
2335 if they are not needed. */
2336 s
= bfd_make_section (abfd
, ".gnu.version_d");
2338 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2339 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2342 s
= bfd_make_section (abfd
, ".gnu.version");
2344 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2345 || ! bfd_set_section_alignment (abfd
, s
, 1))
2348 s
= bfd_make_section (abfd
, ".gnu.version_r");
2350 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2351 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2354 s
= bfd_make_section (abfd
, ".dynsym");
2356 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2357 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2360 s
= bfd_make_section (abfd
, ".dynstr");
2362 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2365 /* Create a strtab to hold the dynamic symbol names. */
2366 if (elf_hash_table (info
)->dynstr
== NULL
)
2368 elf_hash_table (info
)->dynstr
= _bfd_elf_strtab_init ();
2369 if (elf_hash_table (info
)->dynstr
== NULL
)
2373 s
= bfd_make_section (abfd
, ".dynamic");
2375 || ! bfd_set_section_flags (abfd
, s
, flags
)
2376 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2379 /* The special symbol _DYNAMIC is always set to the start of the
2380 .dynamic section. This call occurs before we have processed the
2381 symbols for any dynamic object, so we don't have to worry about
2382 overriding a dynamic definition. We could set _DYNAMIC in a
2383 linker script, but we only want to define it if we are, in fact,
2384 creating a .dynamic section. We don't want to define it if there
2385 is no .dynamic section, since on some ELF platforms the start up
2386 code examines it to decide how to initialize the process. */
2388 if (! (_bfd_generic_link_add_one_symbol
2389 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
2390 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
2391 (struct bfd_link_hash_entry
**) &h
)))
2393 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2394 h
->type
= STT_OBJECT
;
2397 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2400 bed
= get_elf_backend_data (abfd
);
2402 s
= bfd_make_section (abfd
, ".hash");
2404 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2405 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2407 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
2409 /* Let the backend create the rest of the sections. This lets the
2410 backend set the right flags. The backend will normally create
2411 the .got and .plt sections. */
2412 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
2415 elf_hash_table (info
)->dynamic_sections_created
= true;
2420 /* Add an entry to the .dynamic table. */
2423 elf_add_dynamic_entry (info
, tag
, val
)
2424 struct bfd_link_info
*info
;
2428 Elf_Internal_Dyn dyn
;
2431 bfd_size_type newsize
;
2432 bfd_byte
*newcontents
;
2434 if (! is_elf_hash_table (info
))
2437 dynobj
= elf_hash_table (info
)->dynobj
;
2439 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
2440 BFD_ASSERT (s
!= NULL
);
2442 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
2443 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
2444 if (newcontents
== NULL
)
2448 dyn
.d_un
.d_val
= val
;
2449 elf_swap_dyn_out (dynobj
, &dyn
,
2450 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
2452 s
->_raw_size
= newsize
;
2453 s
->contents
= newcontents
;
2458 /* Read and swap the relocs from the section indicated by SHDR. This
2459 may be either a REL or a RELA section. The relocations are
2460 translated into RELA relocations and stored in INTERNAL_RELOCS,
2461 which should have already been allocated to contain enough space.
2462 The EXTERNAL_RELOCS are a buffer where the external form of the
2463 relocations should be stored.
2465 Returns false if something goes wrong. */
2468 elf_link_read_relocs_from_section (abfd
, shdr
, external_relocs
,
2471 Elf_Internal_Shdr
*shdr
;
2472 PTR external_relocs
;
2473 Elf_Internal_Rela
*internal_relocs
;
2475 struct elf_backend_data
*bed
;
2478 /* If there aren't any relocations, that's OK. */
2482 /* Position ourselves at the start of the section. */
2483 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2486 /* Read the relocations. */
2487 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2490 bed
= get_elf_backend_data (abfd
);
2492 /* Convert the external relocations to the internal format. */
2493 if (shdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2495 Elf_External_Rel
*erel
;
2496 Elf_External_Rel
*erelend
;
2497 Elf_Internal_Rela
*irela
;
2498 Elf_Internal_Rel
*irel
;
2500 erel
= (Elf_External_Rel
*) external_relocs
;
2501 erelend
= erel
+ NUM_SHDR_ENTRIES (shdr
);
2502 irela
= internal_relocs
;
2503 amt
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
2504 irel
= bfd_alloc (abfd
, amt
);
2505 for (; erel
< erelend
; erel
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2509 if (bed
->s
->swap_reloc_in
)
2510 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
2512 elf_swap_reloc_in (abfd
, erel
, irel
);
2514 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; ++i
)
2516 irela
[i
].r_offset
= irel
[i
].r_offset
;
2517 irela
[i
].r_info
= irel
[i
].r_info
;
2518 irela
[i
].r_addend
= 0;
2524 Elf_External_Rela
*erela
;
2525 Elf_External_Rela
*erelaend
;
2526 Elf_Internal_Rela
*irela
;
2528 BFD_ASSERT (shdr
->sh_entsize
== sizeof (Elf_External_Rela
));
2530 erela
= (Elf_External_Rela
*) external_relocs
;
2531 erelaend
= erela
+ NUM_SHDR_ENTRIES (shdr
);
2532 irela
= internal_relocs
;
2533 for (; erela
< erelaend
; erela
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2535 if (bed
->s
->swap_reloca_in
)
2536 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
2538 elf_swap_reloca_in (abfd
, erela
, irela
);
2545 /* Read and swap the relocs for a section O. They may have been
2546 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2547 not NULL, they are used as buffers to read into. They are known to
2548 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2549 the return value is allocated using either malloc or bfd_alloc,
2550 according to the KEEP_MEMORY argument. If O has two relocation
2551 sections (both REL and RELA relocations), then the REL_HDR
2552 relocations will appear first in INTERNAL_RELOCS, followed by the
2553 REL_HDR2 relocations. */
2556 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
2560 PTR external_relocs
;
2561 Elf_Internal_Rela
*internal_relocs
;
2562 boolean keep_memory
;
2564 Elf_Internal_Shdr
*rel_hdr
;
2566 Elf_Internal_Rela
*alloc2
= NULL
;
2567 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2569 if (elf_section_data (o
)->relocs
!= NULL
)
2570 return elf_section_data (o
)->relocs
;
2572 if (o
->reloc_count
== 0)
2575 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2577 if (internal_relocs
== NULL
)
2581 size
= o
->reloc_count
;
2582 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2584 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2586 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2587 if (internal_relocs
== NULL
)
2591 if (external_relocs
== NULL
)
2593 bfd_size_type size
= rel_hdr
->sh_size
;
2595 if (elf_section_data (o
)->rel_hdr2
)
2596 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2597 alloc1
= (PTR
) bfd_malloc (size
);
2600 external_relocs
= alloc1
;
2603 if (!elf_link_read_relocs_from_section (abfd
, rel_hdr
,
2607 if (!elf_link_read_relocs_from_section
2609 elf_section_data (o
)->rel_hdr2
,
2610 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2611 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2612 * bed
->s
->int_rels_per_ext_rel
)))
2615 /* Cache the results for next time, if we can. */
2617 elf_section_data (o
)->relocs
= internal_relocs
;
2622 /* Don't free alloc2, since if it was allocated we are passing it
2623 back (under the name of internal_relocs). */
2625 return internal_relocs
;
2635 /* Record an assignment to a symbol made by a linker script. We need
2636 this in case some dynamic object refers to this symbol. */
2639 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
2640 bfd
*output_bfd ATTRIBUTE_UNUSED
;
2641 struct bfd_link_info
*info
;
2645 struct elf_link_hash_entry
*h
;
2647 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2650 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, true, false);
2654 if (h
->root
.type
== bfd_link_hash_new
)
2655 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_ELF
;
2657 /* If this symbol is being provided by the linker script, and it is
2658 currently defined by a dynamic object, but not by a regular
2659 object, then mark it as undefined so that the generic linker will
2660 force the correct value. */
2662 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2663 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2664 h
->root
.type
= bfd_link_hash_undefined
;
2666 /* If this symbol is not being provided by the linker script, and it is
2667 currently defined by a dynamic object, but not by a regular object,
2668 then clear out any version information because the symbol will not be
2669 associated with the dynamic object any more. */
2671 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2672 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2673 h
->verinfo
.verdef
= NULL
;
2675 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2677 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2678 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
2680 && h
->dynindx
== -1)
2682 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2685 /* If this is a weak defined symbol, and we know a corresponding
2686 real symbol from the same dynamic object, make sure the real
2687 symbol is also made into a dynamic symbol. */
2688 if (h
->weakdef
!= NULL
2689 && h
->weakdef
->dynindx
== -1)
2691 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2699 /* This structure is used to pass information to
2700 elf_link_assign_sym_version. */
2702 struct elf_assign_sym_version_info
2706 /* General link information. */
2707 struct bfd_link_info
*info
;
2709 struct bfd_elf_version_tree
*verdefs
;
2710 /* Whether we had a failure. */
2714 /* This structure is used to pass information to
2715 elf_link_find_version_dependencies. */
2717 struct elf_find_verdep_info
2721 /* General link information. */
2722 struct bfd_link_info
*info
;
2723 /* The number of dependencies. */
2725 /* Whether we had a failure. */
2729 /* Array used to determine the number of hash table buckets to use
2730 based on the number of symbols there are. If there are fewer than
2731 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2732 fewer than 37 we use 17 buckets, and so forth. We never use more
2733 than 32771 buckets. */
2735 static const size_t elf_buckets
[] =
2737 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2741 /* Compute bucket count for hashing table. We do not use a static set
2742 of possible tables sizes anymore. Instead we determine for all
2743 possible reasonable sizes of the table the outcome (i.e., the
2744 number of collisions etc) and choose the best solution. The
2745 weighting functions are not too simple to allow the table to grow
2746 without bounds. Instead one of the weighting factors is the size.
2747 Therefore the result is always a good payoff between few collisions
2748 (= short chain lengths) and table size. */
2750 compute_bucket_count (info
)
2751 struct bfd_link_info
*info
;
2753 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2754 size_t best_size
= 0;
2755 unsigned long int *hashcodes
;
2756 unsigned long int *hashcodesp
;
2757 unsigned long int i
;
2760 /* Compute the hash values for all exported symbols. At the same
2761 time store the values in an array so that we could use them for
2764 amt
*= sizeof (unsigned long int);
2765 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
2766 if (hashcodes
== NULL
)
2768 hashcodesp
= hashcodes
;
2770 /* Put all hash values in HASHCODES. */
2771 elf_link_hash_traverse (elf_hash_table (info
),
2772 elf_collect_hash_codes
, &hashcodesp
);
2774 /* We have a problem here. The following code to optimize the table
2775 size requires an integer type with more the 32 bits. If
2776 BFD_HOST_U_64_BIT is set we know about such a type. */
2777 #ifdef BFD_HOST_U_64_BIT
2780 unsigned long int nsyms
= hashcodesp
- hashcodes
;
2783 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
2784 unsigned long int *counts
;
2786 /* Possible optimization parameters: if we have NSYMS symbols we say
2787 that the hashing table must at least have NSYMS/4 and at most
2789 minsize
= nsyms
/ 4;
2792 best_size
= maxsize
= nsyms
* 2;
2794 /* Create array where we count the collisions in. We must use bfd_malloc
2795 since the size could be large. */
2797 amt
*= sizeof (unsigned long int);
2798 counts
= (unsigned long int *) bfd_malloc (amt
);
2805 /* Compute the "optimal" size for the hash table. The criteria is a
2806 minimal chain length. The minor criteria is (of course) the size
2808 for (i
= minsize
; i
< maxsize
; ++i
)
2810 /* Walk through the array of hashcodes and count the collisions. */
2811 BFD_HOST_U_64_BIT max
;
2812 unsigned long int j
;
2813 unsigned long int fact
;
2815 memset (counts
, '\0', i
* sizeof (unsigned long int));
2817 /* Determine how often each hash bucket is used. */
2818 for (j
= 0; j
< nsyms
; ++j
)
2819 ++counts
[hashcodes
[j
] % i
];
2821 /* For the weight function we need some information about the
2822 pagesize on the target. This is information need not be 100%
2823 accurate. Since this information is not available (so far) we
2824 define it here to a reasonable default value. If it is crucial
2825 to have a better value some day simply define this value. */
2826 # ifndef BFD_TARGET_PAGESIZE
2827 # define BFD_TARGET_PAGESIZE (4096)
2830 /* We in any case need 2 + NSYMS entries for the size values and
2832 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
2835 /* Variant 1: optimize for short chains. We add the squares
2836 of all the chain lengths (which favous many small chain
2837 over a few long chains). */
2838 for (j
= 0; j
< i
; ++j
)
2839 max
+= counts
[j
] * counts
[j
];
2841 /* This adds penalties for the overall size of the table. */
2842 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2845 /* Variant 2: Optimize a lot more for small table. Here we
2846 also add squares of the size but we also add penalties for
2847 empty slots (the +1 term). */
2848 for (j
= 0; j
< i
; ++j
)
2849 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
2851 /* The overall size of the table is considered, but not as
2852 strong as in variant 1, where it is squared. */
2853 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2857 /* Compare with current best results. */
2858 if (max
< best_chlen
)
2868 #endif /* defined (BFD_HOST_U_64_BIT) */
2870 /* This is the fallback solution if no 64bit type is available or if we
2871 are not supposed to spend much time on optimizations. We select the
2872 bucket count using a fixed set of numbers. */
2873 for (i
= 0; elf_buckets
[i
] != 0; i
++)
2875 best_size
= elf_buckets
[i
];
2876 if (dynsymcount
< elf_buckets
[i
+ 1])
2881 /* Free the arrays we needed. */
2887 /* Set up the sizes and contents of the ELF dynamic sections. This is
2888 called by the ELF linker emulation before_allocation routine. We
2889 must set the sizes of the sections before the linker sets the
2890 addresses of the various sections. */
2893 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
2895 auxiliary_filters
, info
, sinterpptr
,
2900 const char *filter_shlib
;
2901 const char * const *auxiliary_filters
;
2902 struct bfd_link_info
*info
;
2903 asection
**sinterpptr
;
2904 struct bfd_elf_version_tree
*verdefs
;
2906 bfd_size_type soname_indx
;
2908 struct elf_backend_data
*bed
;
2909 struct elf_assign_sym_version_info asvinfo
;
2913 soname_indx
= (bfd_size_type
) -1;
2915 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2918 if (! is_elf_hash_table (info
))
2921 /* Any syms created from now on start with -1 in
2922 got.refcount/offset and plt.refcount/offset. */
2923 elf_hash_table (info
)->init_refcount
= -1;
2925 /* The backend may have to create some sections regardless of whether
2926 we're dynamic or not. */
2927 bed
= get_elf_backend_data (output_bfd
);
2928 if (bed
->elf_backend_always_size_sections
2929 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
2932 dynobj
= elf_hash_table (info
)->dynobj
;
2934 /* If there were no dynamic objects in the link, there is nothing to
2939 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
2942 if (elf_hash_table (info
)->dynamic_sections_created
)
2944 struct elf_info_failed eif
;
2945 struct elf_link_hash_entry
*h
;
2947 struct bfd_elf_version_tree
*t
;
2948 struct bfd_elf_version_expr
*d
;
2949 boolean all_defined
;
2951 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
2952 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
2956 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2958 if (soname_indx
== (bfd_size_type
) -1
2959 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SONAME
,
2966 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMBOLIC
,
2969 info
->flags
|= DF_SYMBOLIC
;
2976 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
2978 if (info
->new_dtags
)
2979 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
2980 if (indx
== (bfd_size_type
) -1
2981 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RPATH
, indx
)
2983 && ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RUNPATH
,
2988 if (filter_shlib
!= NULL
)
2992 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2993 filter_shlib
, true);
2994 if (indx
== (bfd_size_type
) -1
2995 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FILTER
, indx
))
2999 if (auxiliary_filters
!= NULL
)
3001 const char * const *p
;
3003 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
3007 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3009 if (indx
== (bfd_size_type
) -1
3010 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_AUXILIARY
,
3017 eif
.verdefs
= verdefs
;
3020 /* If we are supposed to export all symbols into the dynamic symbol
3021 table (this is not the normal case), then do so. */
3022 if (info
->export_dynamic
)
3024 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
3030 /* Make all global versions with definiton. */
3031 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3032 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3033 if (!d
->symver
&& strchr (d
->pattern
, '*') == NULL
)
3035 const char *verstr
, *name
;
3036 size_t namelen
, verlen
, newlen
;
3038 struct elf_link_hash_entry
*newh
;
3041 namelen
= strlen (name
);
3043 verlen
= strlen (verstr
);
3044 newlen
= namelen
+ verlen
+ 3;
3046 newname
= (char *) bfd_malloc ((bfd_size_type
) newlen
);
3047 if (newname
== NULL
)
3049 memcpy (newname
, name
, namelen
);
3051 /* Check the hidden versioned definition. */
3052 p
= newname
+ namelen
;
3054 memcpy (p
, verstr
, verlen
+ 1);
3055 newh
= elf_link_hash_lookup (elf_hash_table (info
),
3056 newname
, false, false,
3059 || (newh
->root
.type
!= bfd_link_hash_defined
3060 && newh
->root
.type
!= bfd_link_hash_defweak
))
3062 /* Check the default versioned definition. */
3064 memcpy (p
, verstr
, verlen
+ 1);
3065 newh
= elf_link_hash_lookup (elf_hash_table (info
),
3066 newname
, false, false,
3071 /* Mark this version if there is a definition. */
3073 && (newh
->root
.type
== bfd_link_hash_defined
3074 || newh
->root
.type
== bfd_link_hash_defweak
))
3078 /* Attach all the symbols to their version information. */
3079 asvinfo
.output_bfd
= output_bfd
;
3080 asvinfo
.info
= info
;
3081 asvinfo
.verdefs
= verdefs
;
3082 asvinfo
.failed
= false;
3084 elf_link_hash_traverse (elf_hash_table (info
),
3085 elf_link_assign_sym_version
,
3090 if (!info
->allow_undefined_version
)
3092 /* Check if all global versions have a definiton. */
3094 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3095 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3096 if (!d
->symver
&& !d
->script
3097 && strchr (d
->pattern
, '*') == NULL
)
3099 (*_bfd_error_handler
)
3100 (_("%s: undefined version: %s"),
3101 d
->pattern
, t
->name
);
3102 all_defined
= false;
3107 bfd_set_error (bfd_error_bad_value
);
3112 /* Find all symbols which were defined in a dynamic object and make
3113 the backend pick a reasonable value for them. */
3114 elf_link_hash_traverse (elf_hash_table (info
),
3115 elf_adjust_dynamic_symbol
,
3120 /* Add some entries to the .dynamic section. We fill in some of the
3121 values later, in elf_bfd_final_link, but we must add the entries
3122 now so that we know the final size of the .dynamic section. */
3124 /* If there are initialization and/or finalization functions to
3125 call then add the corresponding DT_INIT/DT_FINI entries. */
3126 h
= (info
->init_function
3127 ? elf_link_hash_lookup (elf_hash_table (info
),
3128 info
->init_function
, false,
3132 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3133 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3135 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT
, (bfd_vma
) 0))
3138 h
= (info
->fini_function
3139 ? elf_link_hash_lookup (elf_hash_table (info
),
3140 info
->fini_function
, false,
3144 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3145 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3147 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI
, (bfd_vma
) 0))
3151 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
3153 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3159 for (sub
= info
->input_bfds
; sub
!= NULL
;
3160 sub
= sub
->link_next
)
3161 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3162 if (elf_section_data (o
)->this_hdr
.sh_type
3163 == SHT_PREINIT_ARRAY
)
3165 (*_bfd_error_handler
)
3166 (_("%s: .preinit_array section is not allowed in DSO"),
3167 bfd_archive_filename (sub
));
3171 bfd_set_error (bfd_error_nonrepresentable_section
);
3175 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAY
,
3177 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAYSZ
,
3181 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
3183 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAY
,
3185 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAYSZ
,
3189 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
3191 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAY
,
3193 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAYSZ
,
3198 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
3199 /* If .dynstr is excluded from the link, we don't want any of
3200 these tags. Strictly, we should be checking each section
3201 individually; This quick check covers for the case where
3202 someone does a /DISCARD/ : { *(*) }. */
3203 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
3205 bfd_size_type strsize
;
3207 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3208 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_HASH
, (bfd_vma
) 0)
3209 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRTAB
, (bfd_vma
) 0)
3210 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMTAB
, (bfd_vma
) 0)
3211 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRSZ
, strsize
)
3212 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMENT
,
3213 (bfd_vma
) sizeof (Elf_External_Sym
)))
3218 /* The backend must work out the sizes of all the other dynamic
3220 if (bed
->elf_backend_size_dynamic_sections
3221 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
3224 if (elf_hash_table (info
)->dynamic_sections_created
)
3226 bfd_size_type dynsymcount
;
3228 size_t bucketcount
= 0;
3229 size_t hash_entry_size
;
3230 unsigned int dtagcount
;
3232 /* Set up the version definition section. */
3233 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3234 BFD_ASSERT (s
!= NULL
);
3236 /* We may have created additional version definitions if we are
3237 just linking a regular application. */
3238 verdefs
= asvinfo
.verdefs
;
3240 /* Skip anonymous version tag. */
3241 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
3242 verdefs
= verdefs
->next
;
3244 if (verdefs
== NULL
)
3245 _bfd_strip_section_from_output (info
, s
);
3250 struct bfd_elf_version_tree
*t
;
3252 Elf_Internal_Verdef def
;
3253 Elf_Internal_Verdaux defaux
;
3258 /* Make space for the base version. */
3259 size
+= sizeof (Elf_External_Verdef
);
3260 size
+= sizeof (Elf_External_Verdaux
);
3263 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3265 struct bfd_elf_version_deps
*n
;
3267 size
+= sizeof (Elf_External_Verdef
);
3268 size
+= sizeof (Elf_External_Verdaux
);
3271 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3272 size
+= sizeof (Elf_External_Verdaux
);
3275 s
->_raw_size
= size
;
3276 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3277 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3280 /* Fill in the version definition section. */
3284 def
.vd_version
= VER_DEF_CURRENT
;
3285 def
.vd_flags
= VER_FLG_BASE
;
3288 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3289 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3290 + sizeof (Elf_External_Verdaux
));
3292 if (soname_indx
!= (bfd_size_type
) -1)
3294 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3296 def
.vd_hash
= bfd_elf_hash (soname
);
3297 defaux
.vda_name
= soname_indx
;
3304 name
= basename (output_bfd
->filename
);
3305 def
.vd_hash
= bfd_elf_hash (name
);
3306 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3308 if (indx
== (bfd_size_type
) -1)
3310 defaux
.vda_name
= indx
;
3312 defaux
.vda_next
= 0;
3314 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3315 (Elf_External_Verdef
*) p
);
3316 p
+= sizeof (Elf_External_Verdef
);
3317 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3318 (Elf_External_Verdaux
*) p
);
3319 p
+= sizeof (Elf_External_Verdaux
);
3321 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3324 struct bfd_elf_version_deps
*n
;
3325 struct elf_link_hash_entry
*h
;
3328 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3331 /* Add a symbol representing this version. */
3333 if (! (_bfd_generic_link_add_one_symbol
3334 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
3335 (bfd_vma
) 0, (const char *) NULL
, false,
3336 get_elf_backend_data (dynobj
)->collect
,
3337 (struct bfd_link_hash_entry
**) &h
)))
3339 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
3340 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3341 h
->type
= STT_OBJECT
;
3342 h
->verinfo
.vertree
= t
;
3344 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
3347 def
.vd_version
= VER_DEF_CURRENT
;
3349 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
3350 def
.vd_flags
|= VER_FLG_WEAK
;
3351 def
.vd_ndx
= t
->vernum
+ 1;
3352 def
.vd_cnt
= cdeps
+ 1;
3353 def
.vd_hash
= bfd_elf_hash (t
->name
);
3354 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3355 if (t
->next
!= NULL
)
3356 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3357 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
3361 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3362 (Elf_External_Verdef
*) p
);
3363 p
+= sizeof (Elf_External_Verdef
);
3365 defaux
.vda_name
= h
->dynstr_index
;
3366 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3368 if (t
->deps
== NULL
)
3369 defaux
.vda_next
= 0;
3371 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3372 t
->name_indx
= defaux
.vda_name
;
3374 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3375 (Elf_External_Verdaux
*) p
);
3376 p
+= sizeof (Elf_External_Verdaux
);
3378 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3380 if (n
->version_needed
== NULL
)
3382 /* This can happen if there was an error in the
3384 defaux
.vda_name
= 0;
3388 defaux
.vda_name
= n
->version_needed
->name_indx
;
3389 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3392 if (n
->next
== NULL
)
3393 defaux
.vda_next
= 0;
3395 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3397 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3398 (Elf_External_Verdaux
*) p
);
3399 p
+= sizeof (Elf_External_Verdaux
);
3403 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEF
, (bfd_vma
) 0)
3404 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEFNUM
,
3408 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
3411 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
3413 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS
, info
->flags
))
3420 info
->flags_1
&= ~ (DF_1_INITFIRST
3423 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS_1
,
3428 /* Work out the size of the version reference section. */
3430 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3431 BFD_ASSERT (s
!= NULL
);
3433 struct elf_find_verdep_info sinfo
;
3435 sinfo
.output_bfd
= output_bfd
;
3437 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
3438 if (sinfo
.vers
== 0)
3440 sinfo
.failed
= false;
3442 elf_link_hash_traverse (elf_hash_table (info
),
3443 elf_link_find_version_dependencies
,
3446 if (elf_tdata (output_bfd
)->verref
== NULL
)
3447 _bfd_strip_section_from_output (info
, s
);
3450 Elf_Internal_Verneed
*t
;
3455 /* Build the version definition section. */
3458 for (t
= elf_tdata (output_bfd
)->verref
;
3462 Elf_Internal_Vernaux
*a
;
3464 size
+= sizeof (Elf_External_Verneed
);
3466 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3467 size
+= sizeof (Elf_External_Vernaux
);
3470 s
->_raw_size
= size
;
3471 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3472 if (s
->contents
== NULL
)
3476 for (t
= elf_tdata (output_bfd
)->verref
;
3481 Elf_Internal_Vernaux
*a
;
3485 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3488 t
->vn_version
= VER_NEED_CURRENT
;
3490 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3491 elf_dt_name (t
->vn_bfd
) != NULL
3492 ? elf_dt_name (t
->vn_bfd
)
3493 : basename (t
->vn_bfd
->filename
),
3495 if (indx
== (bfd_size_type
) -1)
3498 t
->vn_aux
= sizeof (Elf_External_Verneed
);
3499 if (t
->vn_nextref
== NULL
)
3502 t
->vn_next
= (sizeof (Elf_External_Verneed
)
3503 + caux
* sizeof (Elf_External_Vernaux
));
3505 _bfd_elf_swap_verneed_out (output_bfd
, t
,
3506 (Elf_External_Verneed
*) p
);
3507 p
+= sizeof (Elf_External_Verneed
);
3509 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3511 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
3512 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3513 a
->vna_nodename
, false);
3514 if (indx
== (bfd_size_type
) -1)
3517 if (a
->vna_nextptr
== NULL
)
3520 a
->vna_next
= sizeof (Elf_External_Vernaux
);
3522 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
3523 (Elf_External_Vernaux
*) p
);
3524 p
+= sizeof (Elf_External_Vernaux
);
3528 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEED
,
3530 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEEDNUM
,
3534 elf_tdata (output_bfd
)->cverrefs
= crefs
;
3538 /* Assign dynsym indicies. In a shared library we generate a
3539 section symbol for each output section, which come first.
3540 Next come all of the back-end allocated local dynamic syms,
3541 followed by the rest of the global symbols. */
3543 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3545 /* Work out the size of the symbol version section. */
3546 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3547 BFD_ASSERT (s
!= NULL
);
3548 if (dynsymcount
== 0
3549 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
3551 _bfd_strip_section_from_output (info
, s
);
3552 /* The DYNSYMCOUNT might have changed if we were going to
3553 output a dynamic symbol table entry for S. */
3554 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3558 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
3559 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3560 if (s
->contents
== NULL
)
3563 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERSYM
, (bfd_vma
) 0))
3567 /* Set the size of the .dynsym and .hash sections. We counted
3568 the number of dynamic symbols in elf_link_add_object_symbols.
3569 We will build the contents of .dynsym and .hash when we build
3570 the final symbol table, because until then we do not know the
3571 correct value to give the symbols. We built the .dynstr
3572 section as we went along in elf_link_add_object_symbols. */
3573 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
3574 BFD_ASSERT (s
!= NULL
);
3575 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
3576 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3577 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3580 if (dynsymcount
!= 0)
3582 Elf_Internal_Sym isym
;
3584 /* The first entry in .dynsym is a dummy symbol. */
3591 elf_swap_symbol_out (output_bfd
, &isym
, (PTR
) s
->contents
, (PTR
) 0);
3594 /* Compute the size of the hashing table. As a side effect this
3595 computes the hash values for all the names we export. */
3596 bucketcount
= compute_bucket_count (info
);
3598 s
= bfd_get_section_by_name (dynobj
, ".hash");
3599 BFD_ASSERT (s
!= NULL
);
3600 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
3601 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
3602 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3603 if (s
->contents
== NULL
)
3606 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) bucketcount
,
3608 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) dynsymcount
,
3609 s
->contents
+ hash_entry_size
);
3611 elf_hash_table (info
)->bucketcount
= bucketcount
;
3613 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
3614 BFD_ASSERT (s
!= NULL
);
3616 elf_finalize_dynstr (output_bfd
, info
);
3618 s
->_raw_size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3620 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
3621 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NULL
, (bfd_vma
) 0))
3628 /* This function is used to adjust offsets into .dynstr for
3629 dynamic symbols. This is called via elf_link_hash_traverse. */
3631 static boolean elf_adjust_dynstr_offsets
3632 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3635 elf_adjust_dynstr_offsets (h
, data
)
3636 struct elf_link_hash_entry
*h
;
3639 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3641 if (h
->root
.type
== bfd_link_hash_warning
)
3642 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3644 if (h
->dynindx
!= -1)
3645 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3649 /* Assign string offsets in .dynstr, update all structures referencing
3653 elf_finalize_dynstr (output_bfd
, info
)
3655 struct bfd_link_info
*info
;
3657 struct elf_link_local_dynamic_entry
*entry
;
3658 struct elf_strtab_hash
*dynstr
= elf_hash_table (info
)->dynstr
;
3659 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
3662 Elf_External_Dyn
*dyncon
, *dynconend
;
3664 _bfd_elf_strtab_finalize (dynstr
);
3665 size
= _bfd_elf_strtab_size (dynstr
);
3667 /* Update all .dynamic entries referencing .dynstr strings. */
3668 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3669 BFD_ASSERT (sdyn
!= NULL
);
3671 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
3672 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
3674 for (; dyncon
< dynconend
; dyncon
++)
3676 Elf_Internal_Dyn dyn
;
3678 elf_swap_dyn_in (dynobj
, dyncon
, & dyn
);
3682 dyn
.d_un
.d_val
= size
;
3683 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3691 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3692 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3699 /* Now update local dynamic symbols. */
3700 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
3701 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3702 entry
->isym
.st_name
);
3704 /* And the rest of dynamic symbols. */
3705 elf_link_hash_traverse (elf_hash_table (info
),
3706 elf_adjust_dynstr_offsets
, dynstr
);
3708 /* Adjust version definitions. */
3709 if (elf_tdata (output_bfd
)->cverdefs
)
3714 Elf_Internal_Verdef def
;
3715 Elf_Internal_Verdaux defaux
;
3717 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3718 p
= (bfd_byte
*) s
->contents
;
3721 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3723 p
+= sizeof (Elf_External_Verdef
);
3724 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3726 _bfd_elf_swap_verdaux_in (output_bfd
,
3727 (Elf_External_Verdaux
*) p
, &defaux
);
3728 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3730 _bfd_elf_swap_verdaux_out (output_bfd
,
3731 &defaux
, (Elf_External_Verdaux
*) p
);
3732 p
+= sizeof (Elf_External_Verdaux
);
3735 while (def
.vd_next
);
3738 /* Adjust version references. */
3739 if (elf_tdata (output_bfd
)->verref
)
3744 Elf_Internal_Verneed need
;
3745 Elf_Internal_Vernaux needaux
;
3747 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3748 p
= (bfd_byte
*) s
->contents
;
3751 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3753 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3754 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3755 (Elf_External_Verneed
*) p
);
3756 p
+= sizeof (Elf_External_Verneed
);
3757 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3759 _bfd_elf_swap_vernaux_in (output_bfd
,
3760 (Elf_External_Vernaux
*) p
, &needaux
);
3761 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3763 _bfd_elf_swap_vernaux_out (output_bfd
,
3765 (Elf_External_Vernaux
*) p
);
3766 p
+= sizeof (Elf_External_Vernaux
);
3769 while (need
.vn_next
);
3775 /* Fix up the flags for a symbol. This handles various cases which
3776 can only be fixed after all the input files are seen. This is
3777 currently called by both adjust_dynamic_symbol and
3778 assign_sym_version, which is unnecessary but perhaps more robust in
3779 the face of future changes. */
3782 elf_fix_symbol_flags (h
, eif
)
3783 struct elf_link_hash_entry
*h
;
3784 struct elf_info_failed
*eif
;
3786 /* If this symbol was mentioned in a non-ELF file, try to set
3787 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3788 permit a non-ELF file to correctly refer to a symbol defined in
3789 an ELF dynamic object. */
3790 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
3792 while (h
->root
.type
== bfd_link_hash_indirect
)
3793 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3795 if (h
->root
.type
!= bfd_link_hash_defined
3796 && h
->root
.type
!= bfd_link_hash_defweak
)
3797 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3798 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3801 if (h
->root
.u
.def
.section
->owner
!= NULL
3802 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3803 == bfd_target_elf_flavour
))
3804 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3805 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3807 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3810 if (h
->dynindx
== -1
3811 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
3812 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
3814 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3823 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3824 was first seen in a non-ELF file. Fortunately, if the symbol
3825 was first seen in an ELF file, we're probably OK unless the
3826 symbol was defined in a non-ELF file. Catch that case here.
3827 FIXME: We're still in trouble if the symbol was first seen in
3828 a dynamic object, and then later in a non-ELF regular object. */
3829 if ((h
->root
.type
== bfd_link_hash_defined
3830 || h
->root
.type
== bfd_link_hash_defweak
)
3831 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3832 && (h
->root
.u
.def
.section
->owner
!= NULL
3833 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3834 != bfd_target_elf_flavour
)
3835 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3836 && (h
->elf_link_hash_flags
3837 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
3838 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3841 /* If this is a final link, and the symbol was defined as a common
3842 symbol in a regular object file, and there was no definition in
3843 any dynamic object, then the linker will have allocated space for
3844 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3845 flag will not have been set. */
3846 if (h
->root
.type
== bfd_link_hash_defined
3847 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3848 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
3849 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3850 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3851 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3853 /* If -Bsymbolic was used (which means to bind references to global
3854 symbols to the definition within the shared object), and this
3855 symbol was defined in a regular object, then it actually doesn't
3856 need a PLT entry, and we can accomplish that by forcing it local.
3857 Likewise, if the symbol has hidden or internal visibility.
3858 FIXME: It might be that we also do not need a PLT for other
3859 non-hidden visibilities, but we would have to tell that to the
3860 backend specifically; we can't just clear PLT-related data here. */
3861 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
3862 && eif
->info
->shared
3863 && is_elf_hash_table (eif
->info
)
3864 && (eif
->info
->symbolic
3865 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3866 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3867 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3869 struct elf_backend_data
*bed
;
3870 boolean force_local
;
3872 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3874 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3875 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
3876 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
3879 /* If this is a weak defined symbol in a dynamic object, and we know
3880 the real definition in the dynamic object, copy interesting flags
3881 over to the real definition. */
3882 if (h
->weakdef
!= NULL
)
3884 struct elf_link_hash_entry
*weakdef
;
3886 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3887 || h
->root
.type
== bfd_link_hash_defweak
);
3888 weakdef
= h
->weakdef
;
3889 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
3890 || weakdef
->root
.type
== bfd_link_hash_defweak
);
3891 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
3893 /* If the real definition is defined by a regular object file,
3894 don't do anything special. See the longer description in
3895 elf_adjust_dynamic_symbol, below. */
3896 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3900 struct elf_backend_data
*bed
;
3902 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3903 (*bed
->elf_backend_copy_indirect_symbol
) (weakdef
, h
);
3910 /* Make the backend pick a good value for a dynamic symbol. This is
3911 called via elf_link_hash_traverse, and also calls itself
3915 elf_adjust_dynamic_symbol (h
, data
)
3916 struct elf_link_hash_entry
*h
;
3919 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3921 struct elf_backend_data
*bed
;
3923 if (h
->root
.type
== bfd_link_hash_warning
)
3925 h
->plt
.offset
= (bfd_vma
) -1;
3926 h
->got
.offset
= (bfd_vma
) -1;
3928 /* When warning symbols are created, they **replace** the "real"
3929 entry in the hash table, thus we never get to see the real
3930 symbol in a hash traversal. So look at it now. */
3931 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3934 /* Ignore indirect symbols. These are added by the versioning code. */
3935 if (h
->root
.type
== bfd_link_hash_indirect
)
3938 if (! is_elf_hash_table (eif
->info
))
3941 /* Fix the symbol flags. */
3942 if (! elf_fix_symbol_flags (h
, eif
))
3945 /* If this symbol does not require a PLT entry, and it is not
3946 defined by a dynamic object, or is not referenced by a regular
3947 object, ignore it. We do have to handle a weak defined symbol,
3948 even if no regular object refers to it, if we decided to add it
3949 to the dynamic symbol table. FIXME: Do we normally need to worry
3950 about symbols which are defined by one dynamic object and
3951 referenced by another one? */
3952 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
3953 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
3954 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3955 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
3956 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
3958 h
->plt
.offset
= (bfd_vma
) -1;
3962 /* If we've already adjusted this symbol, don't do it again. This
3963 can happen via a recursive call. */
3964 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
3967 /* Don't look at this symbol again. Note that we must set this
3968 after checking the above conditions, because we may look at a
3969 symbol once, decide not to do anything, and then get called
3970 recursively later after REF_REGULAR is set below. */
3971 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
3973 /* If this is a weak definition, and we know a real definition, and
3974 the real symbol is not itself defined by a regular object file,
3975 then get a good value for the real definition. We handle the
3976 real symbol first, for the convenience of the backend routine.
3978 Note that there is a confusing case here. If the real definition
3979 is defined by a regular object file, we don't get the real symbol
3980 from the dynamic object, but we do get the weak symbol. If the
3981 processor backend uses a COPY reloc, then if some routine in the
3982 dynamic object changes the real symbol, we will not see that
3983 change in the corresponding weak symbol. This is the way other
3984 ELF linkers work as well, and seems to be a result of the shared
3987 I will clarify this issue. Most SVR4 shared libraries define the
3988 variable _timezone and define timezone as a weak synonym. The
3989 tzset call changes _timezone. If you write
3990 extern int timezone;
3992 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3993 you might expect that, since timezone is a synonym for _timezone,
3994 the same number will print both times. However, if the processor
3995 backend uses a COPY reloc, then actually timezone will be copied
3996 into your process image, and, since you define _timezone
3997 yourself, _timezone will not. Thus timezone and _timezone will
3998 wind up at different memory locations. The tzset call will set
3999 _timezone, leaving timezone unchanged. */
4001 if (h
->weakdef
!= NULL
)
4003 /* If we get to this point, we know there is an implicit
4004 reference by a regular object file via the weak symbol H.
4005 FIXME: Is this really true? What if the traversal finds
4006 H->WEAKDEF before it finds H? */
4007 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
4009 if (! elf_adjust_dynamic_symbol (h
->weakdef
, (PTR
) eif
))
4013 /* If a symbol has no type and no size and does not require a PLT
4014 entry, then we are probably about to do the wrong thing here: we
4015 are probably going to create a COPY reloc for an empty object.
4016 This case can arise when a shared object is built with assembly
4017 code, and the assembly code fails to set the symbol type. */
4019 && h
->type
== STT_NOTYPE
4020 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
4021 (*_bfd_error_handler
)
4022 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4023 h
->root
.root
.string
);
4025 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
4026 bed
= get_elf_backend_data (dynobj
);
4027 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
4036 /* This routine is used to export all defined symbols into the dynamic
4037 symbol table. It is called via elf_link_hash_traverse. */
4040 elf_export_symbol (h
, data
)
4041 struct elf_link_hash_entry
*h
;
4044 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
4046 /* Ignore indirect symbols. These are added by the versioning code. */
4047 if (h
->root
.type
== bfd_link_hash_indirect
)
4050 if (h
->root
.type
== bfd_link_hash_warning
)
4051 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4053 if (h
->dynindx
== -1
4054 && (h
->elf_link_hash_flags
4055 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
4057 struct bfd_elf_version_tree
*t
;
4058 struct bfd_elf_version_expr
*d
;
4060 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
4062 if (t
->globals
!= NULL
)
4064 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4066 if ((*d
->match
) (d
, h
->root
.root
.string
))
4071 if (t
->locals
!= NULL
)
4073 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4075 if ((*d
->match
) (d
, h
->root
.root
.string
))
4084 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
4095 /* Look through the symbols which are defined in other shared
4096 libraries and referenced here. Update the list of version
4097 dependencies. This will be put into the .gnu.version_r section.
4098 This function is called via elf_link_hash_traverse. */
4101 elf_link_find_version_dependencies (h
, data
)
4102 struct elf_link_hash_entry
*h
;
4105 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
4106 Elf_Internal_Verneed
*t
;
4107 Elf_Internal_Vernaux
*a
;
4110 if (h
->root
.type
== bfd_link_hash_warning
)
4111 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4113 /* We only care about symbols defined in shared objects with version
4115 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4116 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4118 || h
->verinfo
.verdef
== NULL
)
4121 /* See if we already know about this version. */
4122 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
4124 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
4127 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4128 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
4134 /* This is a new version. Add it to tree we are building. */
4139 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4142 rinfo
->failed
= true;
4146 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
4147 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
4148 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
4152 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4154 /* Note that we are copying a string pointer here, and testing it
4155 above. If bfd_elf_string_from_elf_section is ever changed to
4156 discard the string data when low in memory, this will have to be
4158 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
4160 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
4161 a
->vna_nextptr
= t
->vn_auxptr
;
4163 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
4166 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4173 /* Figure out appropriate versions for all the symbols. We may not
4174 have the version number script until we have read all of the input
4175 files, so until that point we don't know which symbols should be
4176 local. This function is called via elf_link_hash_traverse. */
4179 elf_link_assign_sym_version (h
, data
)
4180 struct elf_link_hash_entry
*h
;
4183 struct elf_assign_sym_version_info
*sinfo
;
4184 struct bfd_link_info
*info
;
4185 struct elf_backend_data
*bed
;
4186 struct elf_info_failed eif
;
4190 sinfo
= (struct elf_assign_sym_version_info
*) data
;
4193 if (h
->root
.type
== bfd_link_hash_warning
)
4194 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4196 /* Fix the symbol flags. */
4199 if (! elf_fix_symbol_flags (h
, &eif
))
4202 sinfo
->failed
= true;
4206 /* We only need version numbers for symbols defined in regular
4208 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4211 bed
= get_elf_backend_data (sinfo
->output_bfd
);
4212 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4213 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
4215 struct bfd_elf_version_tree
*t
;
4220 /* There are two consecutive ELF_VER_CHR characters if this is
4221 not a hidden symbol. */
4223 if (*p
== ELF_VER_CHR
)
4229 /* If there is no version string, we can just return out. */
4233 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4237 /* Look for the version. If we find it, it is no longer weak. */
4238 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4240 if (strcmp (t
->name
, p
) == 0)
4244 struct bfd_elf_version_expr
*d
;
4246 len
= p
- h
->root
.root
.string
;
4247 alc
= bfd_malloc ((bfd_size_type
) len
);
4250 memcpy (alc
, h
->root
.root
.string
, len
- 1);
4251 alc
[len
- 1] = '\0';
4252 if (alc
[len
- 2] == ELF_VER_CHR
)
4253 alc
[len
- 2] = '\0';
4255 h
->verinfo
.vertree
= t
;
4259 if (t
->globals
!= NULL
)
4261 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4262 if ((*d
->match
) (d
, alc
))
4266 /* See if there is anything to force this symbol to
4268 if (d
== NULL
&& t
->locals
!= NULL
)
4270 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4272 if ((*d
->match
) (d
, alc
))
4274 if (h
->dynindx
!= -1
4276 && ! info
->export_dynamic
)
4278 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
4291 /* If we are building an application, we need to create a
4292 version node for this version. */
4293 if (t
== NULL
&& ! info
->shared
)
4295 struct bfd_elf_version_tree
**pp
;
4298 /* If we aren't going to export this symbol, we don't need
4299 to worry about it. */
4300 if (h
->dynindx
== -1)
4304 t
= ((struct bfd_elf_version_tree
*)
4305 bfd_alloc (sinfo
->output_bfd
, amt
));
4308 sinfo
->failed
= true;
4317 t
->name_indx
= (unsigned int) -1;
4321 /* Don't count anonymous version tag. */
4322 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
4324 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4326 t
->vernum
= version_index
;
4330 h
->verinfo
.vertree
= t
;
4334 /* We could not find the version for a symbol when
4335 generating a shared archive. Return an error. */
4336 (*_bfd_error_handler
)
4337 (_("%s: undefined versioned symbol name %s"),
4338 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
4339 bfd_set_error (bfd_error_bad_value
);
4340 sinfo
->failed
= true;
4345 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4348 /* If we don't have a version for this symbol, see if we can find
4350 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
4352 struct bfd_elf_version_tree
*t
;
4353 struct bfd_elf_version_tree
*local_ver
;
4354 struct bfd_elf_version_expr
*d
;
4356 /* See if can find what version this symbol is in. If the
4357 symbol is supposed to be local, then don't actually register
4360 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4362 if (t
->globals
!= NULL
)
4367 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4369 if ((*d
->match
) (d
, h
->root
.root
.string
))
4375 /* There is a version without definition. Make
4376 the symbol the default definition for this
4378 h
->verinfo
.vertree
= t
;
4389 /* There is no undefined version for this symbol. Hide the
4391 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
4394 if (t
->locals
!= NULL
)
4396 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4398 /* If the match is "*", keep looking for a more
4399 explicit, perhaps even global, match. */
4400 if (d
->pattern
[0] == '*' && d
->pattern
[1] == '\0')
4402 else if ((*d
->match
) (d
, h
->root
.root
.string
))
4414 if (local_ver
!= NULL
)
4416 h
->verinfo
.vertree
= local_ver
;
4417 if (h
->dynindx
!= -1
4419 && ! info
->export_dynamic
)
4421 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
4429 /* Final phase of ELF linker. */
4431 /* A structure we use to avoid passing large numbers of arguments. */
4433 struct elf_final_link_info
4435 /* General link information. */
4436 struct bfd_link_info
*info
;
4439 /* Symbol string table. */
4440 struct bfd_strtab_hash
*symstrtab
;
4441 /* .dynsym section. */
4442 asection
*dynsym_sec
;
4443 /* .hash section. */
4445 /* symbol version section (.gnu.version). */
4446 asection
*symver_sec
;
4447 /* first SHF_TLS section (if any). */
4448 asection
*first_tls_sec
;
4449 /* Buffer large enough to hold contents of any section. */
4451 /* Buffer large enough to hold external relocs of any section. */
4452 PTR external_relocs
;
4453 /* Buffer large enough to hold internal relocs of any section. */
4454 Elf_Internal_Rela
*internal_relocs
;
4455 /* Buffer large enough to hold external local symbols of any input
4457 Elf_External_Sym
*external_syms
;
4458 /* And a buffer for symbol section indices. */
4459 Elf_External_Sym_Shndx
*locsym_shndx
;
4460 /* Buffer large enough to hold internal local symbols of any input
4462 Elf_Internal_Sym
*internal_syms
;
4463 /* Array large enough to hold a symbol index for each local symbol
4464 of any input BFD. */
4466 /* Array large enough to hold a section pointer for each local
4467 symbol of any input BFD. */
4468 asection
**sections
;
4469 /* Buffer to hold swapped out symbols. */
4470 Elf_External_Sym
*symbuf
;
4471 /* And one for symbol section indices. */
4472 Elf_External_Sym_Shndx
*symshndxbuf
;
4473 /* Number of swapped out symbols in buffer. */
4474 size_t symbuf_count
;
4475 /* Number of symbols which fit in symbuf. */
4479 static boolean elf_link_output_sym
4480 PARAMS ((struct elf_final_link_info
*, const char *,
4481 Elf_Internal_Sym
*, asection
*));
4482 static boolean elf_link_flush_output_syms
4483 PARAMS ((struct elf_final_link_info
*));
4484 static boolean elf_link_output_extsym
4485 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4486 static boolean elf_link_sec_merge_syms
4487 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4488 static boolean elf_link_check_versioned_symbol
4489 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
4490 static boolean elf_link_input_bfd
4491 PARAMS ((struct elf_final_link_info
*, bfd
*));
4492 static boolean elf_reloc_link_order
4493 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
4494 struct bfd_link_order
*));
4496 /* This struct is used to pass information to elf_link_output_extsym. */
4498 struct elf_outext_info
4502 struct elf_final_link_info
*finfo
;
4505 /* Compute the size of, and allocate space for, REL_HDR which is the
4506 section header for a section containing relocations for O. */
4509 elf_link_size_reloc_section (abfd
, rel_hdr
, o
)
4511 Elf_Internal_Shdr
*rel_hdr
;
4514 bfd_size_type reloc_count
;
4515 bfd_size_type num_rel_hashes
;
4517 /* Figure out how many relocations there will be. */
4518 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
4519 reloc_count
= elf_section_data (o
)->rel_count
;
4521 reloc_count
= elf_section_data (o
)->rel_count2
;
4523 num_rel_hashes
= o
->reloc_count
;
4524 if (num_rel_hashes
< reloc_count
)
4525 num_rel_hashes
= reloc_count
;
4527 /* That allows us to calculate the size of the section. */
4528 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
4530 /* The contents field must last into write_object_contents, so we
4531 allocate it with bfd_alloc rather than malloc. Also since we
4532 cannot be sure that the contents will actually be filled in,
4533 we zero the allocated space. */
4534 rel_hdr
->contents
= (PTR
) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
4535 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
4538 /* We only allocate one set of hash entries, so we only do it the
4539 first time we are called. */
4540 if (elf_section_data (o
)->rel_hashes
== NULL
4543 struct elf_link_hash_entry
**p
;
4545 p
= ((struct elf_link_hash_entry
**)
4546 bfd_zmalloc (num_rel_hashes
4547 * sizeof (struct elf_link_hash_entry
*)));
4551 elf_section_data (o
)->rel_hashes
= p
;
4557 /* When performing a relocateable link, the input relocations are
4558 preserved. But, if they reference global symbols, the indices
4559 referenced must be updated. Update all the relocations in
4560 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4563 elf_link_adjust_relocs (abfd
, rel_hdr
, count
, rel_hash
)
4565 Elf_Internal_Shdr
*rel_hdr
;
4567 struct elf_link_hash_entry
**rel_hash
;
4570 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4571 Elf_Internal_Rel
*irel
;
4572 Elf_Internal_Rela
*irela
;
4573 bfd_size_type amt
= sizeof (Elf_Internal_Rel
) * bed
->s
->int_rels_per_ext_rel
;
4575 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (amt
);
4578 (*_bfd_error_handler
) (_("Error: out of memory"));
4582 amt
= sizeof (Elf_Internal_Rela
) * bed
->s
->int_rels_per_ext_rel
;
4583 irela
= (Elf_Internal_Rela
*) bfd_zmalloc (amt
);
4586 (*_bfd_error_handler
) (_("Error: out of memory"));
4590 for (i
= 0; i
< count
; i
++, rel_hash
++)
4592 if (*rel_hash
== NULL
)
4595 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
4597 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4599 Elf_External_Rel
*erel
;
4602 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
4603 if (bed
->s
->swap_reloc_in
)
4604 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
4606 elf_swap_reloc_in (abfd
, erel
, irel
);
4608 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4609 irel
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4610 ELF_R_TYPE (irel
[j
].r_info
));
4612 if (bed
->s
->swap_reloc_out
)
4613 (*bed
->s
->swap_reloc_out
) (abfd
, irel
, (bfd_byte
*) erel
);
4615 elf_swap_reloc_out (abfd
, irel
, erel
);
4619 Elf_External_Rela
*erela
;
4622 BFD_ASSERT (rel_hdr
->sh_entsize
4623 == sizeof (Elf_External_Rela
));
4625 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
4626 if (bed
->s
->swap_reloca_in
)
4627 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
4629 elf_swap_reloca_in (abfd
, erela
, irela
);
4631 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4632 irela
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4633 ELF_R_TYPE (irela
[j
].r_info
));
4635 if (bed
->s
->swap_reloca_out
)
4636 (*bed
->s
->swap_reloca_out
) (abfd
, irela
, (bfd_byte
*) erela
);
4638 elf_swap_reloca_out (abfd
, irela
, erela
);
4646 struct elf_link_sort_rela
4649 enum elf_reloc_type_class type
;
4652 Elf_Internal_Rel rel
;
4653 Elf_Internal_Rela rela
;
4658 elf_link_sort_cmp1 (A
, B
)
4662 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4663 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4664 int relativea
, relativeb
;
4666 relativea
= a
->type
== reloc_class_relative
;
4667 relativeb
= b
->type
== reloc_class_relative
;
4669 if (relativea
< relativeb
)
4671 if (relativea
> relativeb
)
4673 if (ELF_R_SYM (a
->u
.rel
.r_info
) < ELF_R_SYM (b
->u
.rel
.r_info
))
4675 if (ELF_R_SYM (a
->u
.rel
.r_info
) > ELF_R_SYM (b
->u
.rel
.r_info
))
4677 if (a
->u
.rel
.r_offset
< b
->u
.rel
.r_offset
)
4679 if (a
->u
.rel
.r_offset
> b
->u
.rel
.r_offset
)
4685 elf_link_sort_cmp2 (A
, B
)
4689 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4690 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4693 if (a
->offset
< b
->offset
)
4695 if (a
->offset
> b
->offset
)
4697 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
4698 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
4703 if (a
->u
.rel
.r_offset
< b
->u
.rel
.r_offset
)
4705 if (a
->u
.rel
.r_offset
> b
->u
.rel
.r_offset
)
4711 elf_link_sort_relocs (abfd
, info
, psec
)
4713 struct bfd_link_info
*info
;
4716 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4717 asection
*reldyn
, *o
;
4718 boolean rel
= false;
4719 bfd_size_type count
, size
;
4721 struct elf_link_sort_rela
*rela
;
4722 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4724 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
4725 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4727 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
4728 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4731 count
= reldyn
->_raw_size
/ sizeof (Elf_External_Rel
);
4734 count
= reldyn
->_raw_size
/ sizeof (Elf_External_Rela
);
4737 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4738 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4739 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4740 && o
->output_section
== reldyn
)
4741 size
+= o
->_raw_size
;
4743 if (size
!= reldyn
->_raw_size
)
4746 rela
= (struct elf_link_sort_rela
*) bfd_zmalloc (sizeof (*rela
) * count
);
4749 (*info
->callbacks
->warning
)
4750 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0,
4755 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4756 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4757 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4758 && o
->output_section
== reldyn
)
4762 Elf_External_Rel
*erel
, *erelend
;
4763 struct elf_link_sort_rela
*s
;
4765 erel
= (Elf_External_Rel
*) o
->contents
;
4766 erelend
= (Elf_External_Rel
*) (o
->contents
+ o
->_raw_size
);
4767 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rel
);
4768 for (; erel
< erelend
; erel
++, s
++)
4770 if (bed
->s
->swap_reloc_in
)
4771 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, &s
->u
.rel
);
4773 elf_swap_reloc_in (abfd
, erel
, &s
->u
.rel
);
4775 s
->type
= (*bed
->elf_backend_reloc_type_class
) (&s
->u
.rela
);
4780 Elf_External_Rela
*erela
, *erelaend
;
4781 struct elf_link_sort_rela
*s
;
4783 erela
= (Elf_External_Rela
*) o
->contents
;
4784 erelaend
= (Elf_External_Rela
*) (o
->contents
+ o
->_raw_size
);
4785 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rela
);
4786 for (; erela
< erelaend
; erela
++, s
++)
4788 if (bed
->s
->swap_reloca_in
)
4789 (*bed
->s
->swap_reloca_in
) (dynobj
, (bfd_byte
*) erela
,
4792 elf_swap_reloca_in (dynobj
, erela
, &s
->u
.rela
);
4794 s
->type
= (*bed
->elf_backend_reloc_type_class
) (&s
->u
.rela
);
4799 qsort (rela
, (size_t) count
, sizeof (*rela
), elf_link_sort_cmp1
);
4800 for (ret
= 0; ret
< count
&& rela
[ret
].type
== reloc_class_relative
; ret
++)
4802 for (i
= ret
, j
= ret
; i
< count
; i
++)
4804 if (ELF_R_SYM (rela
[i
].u
.rel
.r_info
) != ELF_R_SYM (rela
[j
].u
.rel
.r_info
))
4806 rela
[i
].offset
= rela
[j
].u
.rel
.r_offset
;
4808 qsort (rela
+ ret
, (size_t) count
- ret
, sizeof (*rela
), elf_link_sort_cmp2
);
4810 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4811 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4812 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4813 && o
->output_section
== reldyn
)
4817 Elf_External_Rel
*erel
, *erelend
;
4818 struct elf_link_sort_rela
*s
;
4820 erel
= (Elf_External_Rel
*) o
->contents
;
4821 erelend
= (Elf_External_Rel
*) (o
->contents
+ o
->_raw_size
);
4822 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rel
);
4823 for (; erel
< erelend
; erel
++, s
++)
4825 if (bed
->s
->swap_reloc_out
)
4826 (*bed
->s
->swap_reloc_out
) (abfd
, &s
->u
.rel
,
4829 elf_swap_reloc_out (abfd
, &s
->u
.rel
, erel
);
4834 Elf_External_Rela
*erela
, *erelaend
;
4835 struct elf_link_sort_rela
*s
;
4837 erela
= (Elf_External_Rela
*) o
->contents
;
4838 erelaend
= (Elf_External_Rela
*) (o
->contents
+ o
->_raw_size
);
4839 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rela
);
4840 for (; erela
< erelaend
; erela
++, s
++)
4842 if (bed
->s
->swap_reloca_out
)
4843 (*bed
->s
->swap_reloca_out
) (dynobj
, &s
->u
.rela
,
4844 (bfd_byte
*) erela
);
4846 elf_swap_reloca_out (dynobj
, &s
->u
.rela
, erela
);
4856 /* Do the final step of an ELF link. */
4859 elf_bfd_final_link (abfd
, info
)
4861 struct bfd_link_info
*info
;
4864 boolean emit_relocs
;
4866 struct elf_final_link_info finfo
;
4867 register asection
*o
;
4868 register struct bfd_link_order
*p
;
4870 bfd_size_type max_contents_size
;
4871 bfd_size_type max_external_reloc_size
;
4872 bfd_size_type max_internal_reloc_count
;
4873 bfd_size_type max_sym_count
;
4874 bfd_size_type max_sym_shndx_count
;
4876 Elf_Internal_Sym elfsym
;
4878 Elf_Internal_Shdr
*symtab_hdr
;
4879 Elf_Internal_Shdr
*symstrtab_hdr
;
4880 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4881 struct elf_outext_info eoinfo
;
4883 size_t relativecount
= 0;
4884 asection
*reldyn
= 0;
4887 if (! is_elf_hash_table (info
))
4891 abfd
->flags
|= DYNAMIC
;
4893 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
4894 dynobj
= elf_hash_table (info
)->dynobj
;
4896 emit_relocs
= (info
->relocateable
4897 || info
->emitrelocations
4898 || bed
->elf_backend_emit_relocs
);
4901 finfo
.output_bfd
= abfd
;
4902 finfo
.symstrtab
= elf_stringtab_init ();
4903 if (finfo
.symstrtab
== NULL
)
4908 finfo
.dynsym_sec
= NULL
;
4909 finfo
.hash_sec
= NULL
;
4910 finfo
.symver_sec
= NULL
;
4914 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
4915 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
4916 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
4917 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
4918 /* Note that it is OK if symver_sec is NULL. */
4921 finfo
.contents
= NULL
;
4922 finfo
.external_relocs
= NULL
;
4923 finfo
.internal_relocs
= NULL
;
4924 finfo
.external_syms
= NULL
;
4925 finfo
.locsym_shndx
= NULL
;
4926 finfo
.internal_syms
= NULL
;
4927 finfo
.indices
= NULL
;
4928 finfo
.sections
= NULL
;
4929 finfo
.symbuf
= NULL
;
4930 finfo
.symshndxbuf
= NULL
;
4931 finfo
.symbuf_count
= 0;
4932 finfo
.first_tls_sec
= NULL
;
4933 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
4934 if ((o
->flags
& SEC_THREAD_LOCAL
) != 0
4935 && (o
->flags
& SEC_LOAD
) != 0)
4937 finfo
.first_tls_sec
= o
;
4941 /* Count up the number of relocations we will output for each output
4942 section, so that we know the sizes of the reloc sections. We
4943 also figure out some maximum sizes. */
4944 max_contents_size
= 0;
4945 max_external_reloc_size
= 0;
4946 max_internal_reloc_count
= 0;
4948 max_sym_shndx_count
= 0;
4950 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
4954 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
4956 if (p
->type
== bfd_section_reloc_link_order
4957 || p
->type
== bfd_symbol_reloc_link_order
)
4959 else if (p
->type
== bfd_indirect_link_order
)
4963 sec
= p
->u
.indirect
.section
;
4965 /* Mark all sections which are to be included in the
4966 link. This will normally be every section. We need
4967 to do this so that we can identify any sections which
4968 the linker has decided to not include. */
4969 sec
->linker_mark
= true;
4971 if (sec
->flags
& SEC_MERGE
)
4974 if (info
->relocateable
|| info
->emitrelocations
)
4975 o
->reloc_count
+= sec
->reloc_count
;
4976 else if (bed
->elf_backend_count_relocs
)
4978 Elf_Internal_Rela
* relocs
;
4980 relocs
= (NAME(_bfd_elf
,link_read_relocs
)
4981 (abfd
, sec
, (PTR
) NULL
,
4982 (Elf_Internal_Rela
*) NULL
, info
->keep_memory
));
4985 += (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
4987 if (elf_section_data (o
)->relocs
!= relocs
)
4991 if (sec
->_raw_size
> max_contents_size
)
4992 max_contents_size
= sec
->_raw_size
;
4993 if (sec
->_cooked_size
> max_contents_size
)
4994 max_contents_size
= sec
->_cooked_size
;
4996 /* We are interested in just local symbols, not all
4998 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
4999 && (sec
->owner
->flags
& DYNAMIC
) == 0)
5003 if (elf_bad_symtab (sec
->owner
))
5004 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
5005 / sizeof (Elf_External_Sym
));
5007 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
5009 if (sym_count
> max_sym_count
)
5010 max_sym_count
= sym_count
;
5012 if (sym_count
> max_sym_shndx_count
5013 && elf_symtab_shndx (sec
->owner
) != 0)
5014 max_sym_shndx_count
= sym_count
;
5016 if ((sec
->flags
& SEC_RELOC
) != 0)
5020 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
5021 if (ext_size
> max_external_reloc_size
)
5022 max_external_reloc_size
= ext_size
;
5023 if (sec
->reloc_count
> max_internal_reloc_count
)
5024 max_internal_reloc_count
= sec
->reloc_count
;
5030 if (o
->reloc_count
> 0)
5031 o
->flags
|= SEC_RELOC
;
5034 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5035 set it (this is probably a bug) and if it is set
5036 assign_section_numbers will create a reloc section. */
5037 o
->flags
&=~ SEC_RELOC
;
5040 /* If the SEC_ALLOC flag is not set, force the section VMA to
5041 zero. This is done in elf_fake_sections as well, but forcing
5042 the VMA to 0 here will ensure that relocs against these
5043 sections are handled correctly. */
5044 if ((o
->flags
& SEC_ALLOC
) == 0
5045 && ! o
->user_set_vma
)
5049 if (! info
->relocateable
&& merged
)
5050 elf_link_hash_traverse (elf_hash_table (info
),
5051 elf_link_sec_merge_syms
, (PTR
) abfd
);
5053 /* Figure out the file positions for everything but the symbol table
5054 and the relocs. We set symcount to force assign_section_numbers
5055 to create a symbol table. */
5056 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
5057 BFD_ASSERT (! abfd
->output_has_begun
);
5058 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
5061 /* Figure out how many relocations we will have in each section.
5062 Just using RELOC_COUNT isn't good enough since that doesn't
5063 maintain a separate value for REL vs. RELA relocations. */
5065 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5066 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5068 asection
*output_section
;
5070 if (! o
->linker_mark
)
5072 /* This section was omitted from the link. */
5076 output_section
= o
->output_section
;
5078 if (output_section
!= NULL
5079 && (o
->flags
& SEC_RELOC
) != 0)
5081 struct bfd_elf_section_data
*esdi
5082 = elf_section_data (o
);
5083 struct bfd_elf_section_data
*esdo
5084 = elf_section_data (output_section
);
5085 unsigned int *rel_count
;
5086 unsigned int *rel_count2
;
5087 bfd_size_type entsize
;
5088 bfd_size_type entsize2
;
5090 /* We must be careful to add the relocations from the
5091 input section to the right output count. */
5092 entsize
= esdi
->rel_hdr
.sh_entsize
;
5093 entsize2
= esdi
->rel_hdr2
? esdi
->rel_hdr2
->sh_entsize
: 0;
5094 BFD_ASSERT ((entsize
== sizeof (Elf_External_Rel
)
5095 || entsize
== sizeof (Elf_External_Rela
))
5096 && entsize2
!= entsize
5098 || entsize2
== sizeof (Elf_External_Rel
)
5099 || entsize2
== sizeof (Elf_External_Rela
)));
5100 if (entsize
== esdo
->rel_hdr
.sh_entsize
)
5102 rel_count
= &esdo
->rel_count
;
5103 rel_count2
= &esdo
->rel_count2
;
5107 rel_count
= &esdo
->rel_count2
;
5108 rel_count2
= &esdo
->rel_count
;
5111 *rel_count
+= NUM_SHDR_ENTRIES (& esdi
->rel_hdr
);
5113 *rel_count2
+= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
5114 output_section
->flags
|= SEC_RELOC
;
5118 /* That created the reloc sections. Set their sizes, and assign
5119 them file positions, and allocate some buffers. */
5120 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5122 if ((o
->flags
& SEC_RELOC
) != 0)
5124 if (!elf_link_size_reloc_section (abfd
,
5125 &elf_section_data (o
)->rel_hdr
,
5129 if (elf_section_data (o
)->rel_hdr2
5130 && !elf_link_size_reloc_section (abfd
,
5131 elf_section_data (o
)->rel_hdr2
,
5136 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5137 to count upwards while actually outputting the relocations. */
5138 elf_section_data (o
)->rel_count
= 0;
5139 elf_section_data (o
)->rel_count2
= 0;
5142 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5144 /* We have now assigned file positions for all the sections except
5145 .symtab and .strtab. We start the .symtab section at the current
5146 file position, and write directly to it. We build the .strtab
5147 section in memory. */
5148 bfd_get_symcount (abfd
) = 0;
5149 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5150 /* sh_name is set in prep_headers. */
5151 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5152 symtab_hdr
->sh_flags
= 0;
5153 symtab_hdr
->sh_addr
= 0;
5154 symtab_hdr
->sh_size
= 0;
5155 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
5156 /* sh_link is set in assign_section_numbers. */
5157 /* sh_info is set below. */
5158 /* sh_offset is set just below. */
5159 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
5161 off
= elf_tdata (abfd
)->next_file_pos
;
5162 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
5164 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5165 incorrect. We do not yet know the size of the .symtab section.
5166 We correct next_file_pos below, after we do know the size. */
5168 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5169 continuously seeking to the right position in the file. */
5170 if (! info
->keep_memory
|| max_sym_count
< 20)
5171 finfo
.symbuf_size
= 20;
5173 finfo
.symbuf_size
= max_sym_count
;
5174 amt
= finfo
.symbuf_size
;
5175 amt
*= sizeof (Elf_External_Sym
);
5176 finfo
.symbuf
= (Elf_External_Sym
*) bfd_malloc (amt
);
5177 if (finfo
.symbuf
== NULL
)
5179 if (elf_numsections (abfd
) > SHN_LORESERVE
)
5181 amt
= finfo
.symbuf_size
;
5182 amt
*= sizeof (Elf_External_Sym_Shndx
);
5183 finfo
.symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
5184 if (finfo
.symshndxbuf
== NULL
)
5188 /* Start writing out the symbol table. The first symbol is always a
5190 if (info
->strip
!= strip_all
5193 elfsym
.st_value
= 0;
5196 elfsym
.st_other
= 0;
5197 elfsym
.st_shndx
= SHN_UNDEF
;
5198 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5199 &elfsym
, bfd_und_section_ptr
))
5204 /* Some standard ELF linkers do this, but we don't because it causes
5205 bootstrap comparison failures. */
5206 /* Output a file symbol for the output file as the second symbol.
5207 We output this even if we are discarding local symbols, although
5208 I'm not sure if this is correct. */
5209 elfsym
.st_value
= 0;
5211 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5212 elfsym
.st_other
= 0;
5213 elfsym
.st_shndx
= SHN_ABS
;
5214 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
5215 &elfsym
, bfd_abs_section_ptr
))
5219 /* Output a symbol for each section. We output these even if we are
5220 discarding local symbols, since they are used for relocs. These
5221 symbols have no names. We store the index of each one in the
5222 index field of the section, so that we can find it again when
5223 outputting relocs. */
5224 if (info
->strip
!= strip_all
5228 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5229 elfsym
.st_other
= 0;
5230 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5232 o
= section_from_elf_index (abfd
, i
);
5234 o
->target_index
= bfd_get_symcount (abfd
);
5235 elfsym
.st_shndx
= i
;
5236 if (info
->relocateable
|| o
== NULL
)
5237 elfsym
.st_value
= 0;
5239 elfsym
.st_value
= o
->vma
;
5240 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5243 if (i
== SHN_LORESERVE
)
5244 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
5248 /* Allocate some memory to hold information read in from the input
5250 if (max_contents_size
!= 0)
5252 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
5253 if (finfo
.contents
== NULL
)
5257 if (max_external_reloc_size
!= 0)
5259 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
5260 if (finfo
.external_relocs
== NULL
)
5264 if (max_internal_reloc_count
!= 0)
5266 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5267 amt
*= sizeof (Elf_Internal_Rela
);
5268 finfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
5269 if (finfo
.internal_relocs
== NULL
)
5273 if (max_sym_count
!= 0)
5275 amt
= max_sym_count
* sizeof (Elf_External_Sym
);
5276 finfo
.external_syms
= (Elf_External_Sym
*) bfd_malloc (amt
);
5277 if (finfo
.external_syms
== NULL
)
5280 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
5281 finfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
5282 if (finfo
.internal_syms
== NULL
)
5285 amt
= max_sym_count
* sizeof (long);
5286 finfo
.indices
= (long *) bfd_malloc (amt
);
5287 if (finfo
.indices
== NULL
)
5290 amt
= max_sym_count
* sizeof (asection
*);
5291 finfo
.sections
= (asection
**) bfd_malloc (amt
);
5292 if (finfo
.sections
== NULL
)
5296 if (max_sym_shndx_count
!= 0)
5298 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
5299 finfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
5300 if (finfo
.locsym_shndx
== NULL
)
5304 if (finfo
.first_tls_sec
)
5306 unsigned int align
= 0;
5307 bfd_vma base
= finfo
.first_tls_sec
->vma
, end
= 0;
5310 for (sec
= finfo
.first_tls_sec
;
5311 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
5314 bfd_vma size
= sec
->_raw_size
;
5316 if (bfd_get_section_alignment (abfd
, sec
) > align
)
5317 align
= bfd_get_section_alignment (abfd
, sec
);
5318 if (sec
->_raw_size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
5320 struct bfd_link_order
*o
;
5323 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
5324 if (size
< o
->offset
+ o
->size
)
5325 size
= o
->offset
+ o
->size
;
5327 end
= sec
->vma
+ size
;
5329 elf_hash_table (info
)->tls_segment
5330 = bfd_zalloc (abfd
, sizeof (struct elf_link_tls_segment
));
5331 if (elf_hash_table (info
)->tls_segment
== NULL
)
5333 elf_hash_table (info
)->tls_segment
->start
= base
;
5334 elf_hash_table (info
)->tls_segment
->size
= end
- base
;
5335 elf_hash_table (info
)->tls_segment
->align
= align
;
5338 /* Since ELF permits relocations to be against local symbols, we
5339 must have the local symbols available when we do the relocations.
5340 Since we would rather only read the local symbols once, and we
5341 would rather not keep them in memory, we handle all the
5342 relocations for a single input file at the same time.
5344 Unfortunately, there is no way to know the total number of local
5345 symbols until we have seen all of them, and the local symbol
5346 indices precede the global symbol indices. This means that when
5347 we are generating relocateable output, and we see a reloc against
5348 a global symbol, we can not know the symbol index until we have
5349 finished examining all the local symbols to see which ones we are
5350 going to output. To deal with this, we keep the relocations in
5351 memory, and don't output them until the end of the link. This is
5352 an unfortunate waste of memory, but I don't see a good way around
5353 it. Fortunately, it only happens when performing a relocateable
5354 link, which is not the common case. FIXME: If keep_memory is set
5355 we could write the relocs out and then read them again; I don't
5356 know how bad the memory loss will be. */
5358 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5359 sub
->output_has_begun
= false;
5360 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5362 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5364 if (p
->type
== bfd_indirect_link_order
5365 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
5366 == bfd_target_elf_flavour
)
5367 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
5369 if (! sub
->output_has_begun
)
5371 if (! elf_link_input_bfd (&finfo
, sub
))
5373 sub
->output_has_begun
= true;
5376 else if (p
->type
== bfd_section_reloc_link_order
5377 || p
->type
== bfd_symbol_reloc_link_order
)
5379 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
5384 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
5390 /* Output any global symbols that got converted to local in a
5391 version script or due to symbol visibility. We do this in a
5392 separate step since ELF requires all local symbols to appear
5393 prior to any global symbols. FIXME: We should only do this if
5394 some global symbols were, in fact, converted to become local.
5395 FIXME: Will this work correctly with the Irix 5 linker? */
5396 eoinfo
.failed
= false;
5397 eoinfo
.finfo
= &finfo
;
5398 eoinfo
.localsyms
= true;
5399 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5404 /* That wrote out all the local symbols. Finish up the symbol table
5405 with the global symbols. Even if we want to strip everything we
5406 can, we still need to deal with those global symbols that got
5407 converted to local in a version script. */
5409 /* The sh_info field records the index of the first non local symbol. */
5410 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
5413 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
5415 Elf_Internal_Sym sym
;
5416 Elf_External_Sym
*dynsym
=
5417 (Elf_External_Sym
*) finfo
.dynsym_sec
->contents
;
5418 long last_local
= 0;
5420 /* Write out the section symbols for the output sections. */
5427 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5430 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5433 Elf_External_Sym
*dest
;
5435 indx
= elf_section_data (s
)->this_idx
;
5436 BFD_ASSERT (indx
> 0);
5437 sym
.st_shndx
= indx
;
5438 sym
.st_value
= s
->vma
;
5439 dest
= dynsym
+ elf_section_data (s
)->dynindx
;
5440 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5443 last_local
= bfd_count_sections (abfd
);
5446 /* Write out the local dynsyms. */
5447 if (elf_hash_table (info
)->dynlocal
)
5449 struct elf_link_local_dynamic_entry
*e
;
5450 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
5453 Elf_External_Sym
*dest
;
5455 sym
.st_size
= e
->isym
.st_size
;
5456 sym
.st_other
= e
->isym
.st_other
;
5458 /* Copy the internal symbol as is.
5459 Note that we saved a word of storage and overwrote
5460 the original st_name with the dynstr_index. */
5463 if (e
->isym
.st_shndx
!= SHN_UNDEF
5464 && (e
->isym
.st_shndx
< SHN_LORESERVE
5465 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
5467 s
= bfd_section_from_elf_index (e
->input_bfd
,
5471 elf_section_data (s
->output_section
)->this_idx
;
5472 sym
.st_value
= (s
->output_section
->vma
5474 + e
->isym
.st_value
);
5477 if (last_local
< e
->dynindx
)
5478 last_local
= e
->dynindx
;
5480 dest
= dynsym
+ e
->dynindx
;
5481 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5485 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
5489 /* We get the global symbols from the hash table. */
5490 eoinfo
.failed
= false;
5491 eoinfo
.localsyms
= false;
5492 eoinfo
.finfo
= &finfo
;
5493 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5498 /* If backend needs to output some symbols not present in the hash
5499 table, do it now. */
5500 if (bed
->elf_backend_output_arch_syms
)
5502 typedef boolean (*out_sym_func
) PARAMS ((PTR
, const char *,
5506 if (! ((*bed
->elf_backend_output_arch_syms
)
5507 (abfd
, info
, (PTR
) &finfo
, (out_sym_func
) elf_link_output_sym
)))
5511 /* Flush all symbols to the file. */
5512 if (! elf_link_flush_output_syms (&finfo
))
5515 /* Now we know the size of the symtab section. */
5516 off
+= symtab_hdr
->sh_size
;
5518 /* Finish up and write out the symbol string table (.strtab)
5520 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5521 /* sh_name was set in prep_headers. */
5522 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5523 symstrtab_hdr
->sh_flags
= 0;
5524 symstrtab_hdr
->sh_addr
= 0;
5525 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
5526 symstrtab_hdr
->sh_entsize
= 0;
5527 symstrtab_hdr
->sh_link
= 0;
5528 symstrtab_hdr
->sh_info
= 0;
5529 /* sh_offset is set just below. */
5530 symstrtab_hdr
->sh_addralign
= 1;
5532 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, true);
5533 elf_tdata (abfd
)->next_file_pos
= off
;
5535 if (bfd_get_symcount (abfd
) > 0)
5537 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
5538 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
5542 /* Adjust the relocs to have the correct symbol indices. */
5543 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5545 if ((o
->flags
& SEC_RELOC
) == 0)
5548 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
5549 elf_section_data (o
)->rel_count
,
5550 elf_section_data (o
)->rel_hashes
);
5551 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
5552 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
5553 elf_section_data (o
)->rel_count2
,
5554 (elf_section_data (o
)->rel_hashes
5555 + elf_section_data (o
)->rel_count
));
5557 /* Set the reloc_count field to 0 to prevent write_relocs from
5558 trying to swap the relocs out itself. */
5562 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
5563 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
5565 /* If we are linking against a dynamic object, or generating a
5566 shared library, finish up the dynamic linking information. */
5569 Elf_External_Dyn
*dyncon
, *dynconend
;
5571 /* Fix up .dynamic entries. */
5572 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
5573 BFD_ASSERT (o
!= NULL
);
5575 dyncon
= (Elf_External_Dyn
*) o
->contents
;
5576 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
5577 for (; dyncon
< dynconend
; dyncon
++)
5579 Elf_Internal_Dyn dyn
;
5583 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
5590 if (relativecount
> 0 && dyncon
+ 1 < dynconend
)
5592 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
5594 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
5595 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
5598 if (dyn
.d_tag
!= DT_NULL
)
5600 dyn
.d_un
.d_val
= relativecount
;
5601 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5607 name
= info
->init_function
;
5610 name
= info
->fini_function
;
5613 struct elf_link_hash_entry
*h
;
5615 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
5616 false, false, true);
5618 && (h
->root
.type
== bfd_link_hash_defined
5619 || h
->root
.type
== bfd_link_hash_defweak
))
5621 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
5622 o
= h
->root
.u
.def
.section
;
5623 if (o
->output_section
!= NULL
)
5624 dyn
.d_un
.d_val
+= (o
->output_section
->vma
5625 + o
->output_offset
);
5628 /* The symbol is imported from another shared
5629 library and does not apply to this one. */
5633 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5638 case DT_PREINIT_ARRAYSZ
:
5639 name
= ".preinit_array";
5641 case DT_INIT_ARRAYSZ
:
5642 name
= ".init_array";
5644 case DT_FINI_ARRAYSZ
:
5645 name
= ".fini_array";
5647 o
= bfd_get_section_by_name (abfd
, name
);
5650 (*_bfd_error_handler
)
5651 (_("%s: could not find output section %s"),
5652 bfd_get_filename (abfd
), name
);
5655 if (o
->_raw_size
== 0)
5656 (*_bfd_error_handler
)
5657 (_("warning: %s section has zero size"), name
);
5658 dyn
.d_un
.d_val
= o
->_raw_size
;
5659 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5662 case DT_PREINIT_ARRAY
:
5663 name
= ".preinit_array";
5666 name
= ".init_array";
5669 name
= ".fini_array";
5682 name
= ".gnu.version_d";
5685 name
= ".gnu.version_r";
5688 name
= ".gnu.version";
5690 o
= bfd_get_section_by_name (abfd
, name
);
5693 (*_bfd_error_handler
)
5694 (_("%s: could not find output section %s"),
5695 bfd_get_filename (abfd
), name
);
5698 dyn
.d_un
.d_ptr
= o
->vma
;
5699 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5706 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
5711 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5713 Elf_Internal_Shdr
*hdr
;
5715 hdr
= elf_elfsections (abfd
)[i
];
5716 if (hdr
->sh_type
== type
5717 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
5719 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
5720 dyn
.d_un
.d_val
+= hdr
->sh_size
;
5723 if (dyn
.d_un
.d_val
== 0
5724 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
5725 dyn
.d_un
.d_val
= hdr
->sh_addr
;
5729 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5735 /* If we have created any dynamic sections, then output them. */
5738 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
5741 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
5743 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5744 || o
->_raw_size
== 0
5745 || o
->output_section
== bfd_abs_section_ptr
)
5747 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
5749 /* At this point, we are only interested in sections
5750 created by elf_link_create_dynamic_sections. */
5753 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
5755 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
5757 if (! bfd_set_section_contents (abfd
, o
->output_section
,
5759 (file_ptr
) o
->output_offset
,
5765 /* The contents of the .dynstr section are actually in a
5767 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
5768 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
5769 || ! _bfd_elf_strtab_emit (abfd
,
5770 elf_hash_table (info
)->dynstr
))
5776 if (info
->relocateable
)
5778 boolean failed
= false;
5780 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
5785 /* If we have optimized stabs strings, output them. */
5786 if (elf_hash_table (info
)->stab_info
!= NULL
)
5788 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
5792 if (info
->eh_frame_hdr
&& elf_hash_table (info
)->dynobj
)
5794 o
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
5797 && (elf_section_data (o
)->sec_info_type
5798 == ELF_INFO_TYPE_EH_FRAME_HDR
))
5800 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, o
))
5805 if (finfo
.symstrtab
!= NULL
)
5806 _bfd_stringtab_free (finfo
.symstrtab
);
5807 if (finfo
.contents
!= NULL
)
5808 free (finfo
.contents
);
5809 if (finfo
.external_relocs
!= NULL
)
5810 free (finfo
.external_relocs
);
5811 if (finfo
.internal_relocs
!= NULL
)
5812 free (finfo
.internal_relocs
);
5813 if (finfo
.external_syms
!= NULL
)
5814 free (finfo
.external_syms
);
5815 if (finfo
.locsym_shndx
!= NULL
)
5816 free (finfo
.locsym_shndx
);
5817 if (finfo
.internal_syms
!= NULL
)
5818 free (finfo
.internal_syms
);
5819 if (finfo
.indices
!= NULL
)
5820 free (finfo
.indices
);
5821 if (finfo
.sections
!= NULL
)
5822 free (finfo
.sections
);
5823 if (finfo
.symbuf
!= NULL
)
5824 free (finfo
.symbuf
);
5825 if (finfo
.symshndxbuf
!= NULL
)
5826 free (finfo
.symbuf
);
5827 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5829 if ((o
->flags
& SEC_RELOC
) != 0
5830 && elf_section_data (o
)->rel_hashes
!= NULL
)
5831 free (elf_section_data (o
)->rel_hashes
);
5834 elf_tdata (abfd
)->linker
= true;
5839 if (finfo
.symstrtab
!= NULL
)
5840 _bfd_stringtab_free (finfo
.symstrtab
);
5841 if (finfo
.contents
!= NULL
)
5842 free (finfo
.contents
);
5843 if (finfo
.external_relocs
!= NULL
)
5844 free (finfo
.external_relocs
);
5845 if (finfo
.internal_relocs
!= NULL
)
5846 free (finfo
.internal_relocs
);
5847 if (finfo
.external_syms
!= NULL
)
5848 free (finfo
.external_syms
);
5849 if (finfo
.locsym_shndx
!= NULL
)
5850 free (finfo
.locsym_shndx
);
5851 if (finfo
.internal_syms
!= NULL
)
5852 free (finfo
.internal_syms
);
5853 if (finfo
.indices
!= NULL
)
5854 free (finfo
.indices
);
5855 if (finfo
.sections
!= NULL
)
5856 free (finfo
.sections
);
5857 if (finfo
.symbuf
!= NULL
)
5858 free (finfo
.symbuf
);
5859 if (finfo
.symshndxbuf
!= NULL
)
5860 free (finfo
.symbuf
);
5861 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5863 if ((o
->flags
& SEC_RELOC
) != 0
5864 && elf_section_data (o
)->rel_hashes
!= NULL
)
5865 free (elf_section_data (o
)->rel_hashes
);
5871 /* Add a symbol to the output symbol table. */
5874 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
5875 struct elf_final_link_info
*finfo
;
5877 Elf_Internal_Sym
*elfsym
;
5878 asection
*input_sec
;
5880 Elf_External_Sym
*dest
;
5881 Elf_External_Sym_Shndx
*destshndx
;
5883 boolean (*output_symbol_hook
) PARAMS ((bfd
*,
5884 struct bfd_link_info
*info
,
5889 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
5890 elf_backend_link_output_symbol_hook
;
5891 if (output_symbol_hook
!= NULL
)
5893 if (! ((*output_symbol_hook
)
5894 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
5898 if (name
== (const char *) NULL
|| *name
== '\0')
5899 elfsym
->st_name
= 0;
5900 else if (input_sec
->flags
& SEC_EXCLUDE
)
5901 elfsym
->st_name
= 0;
5904 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5906 if (elfsym
->st_name
== (unsigned long) -1)
5910 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5912 if (! elf_link_flush_output_syms (finfo
))
5916 dest
= finfo
->symbuf
+ finfo
->symbuf_count
;
5917 destshndx
= finfo
->symshndxbuf
;
5918 if (destshndx
!= NULL
)
5919 destshndx
+= finfo
->symbuf_count
;
5920 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
, (PTR
) dest
, (PTR
) destshndx
);
5921 ++finfo
->symbuf_count
;
5923 ++ bfd_get_symcount (finfo
->output_bfd
);
5928 /* Flush the output symbols to the file. */
5931 elf_link_flush_output_syms (finfo
)
5932 struct elf_final_link_info
*finfo
;
5934 if (finfo
->symbuf_count
> 0)
5936 Elf_Internal_Shdr
*hdr
;
5940 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5941 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5942 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
5943 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5944 || bfd_bwrite ((PTR
) finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5947 hdr
->sh_size
+= amt
;
5949 if (finfo
->symshndxbuf
!= NULL
)
5951 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_shndx_hdr
;
5952 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5953 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym_Shndx
);
5954 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5955 || (bfd_bwrite ((PTR
) finfo
->symshndxbuf
, amt
, finfo
->output_bfd
)
5959 hdr
->sh_size
+= amt
;
5962 finfo
->symbuf_count
= 0;
5968 /* Adjust all external symbols pointing into SEC_MERGE sections
5969 to reflect the object merging within the sections. */
5972 elf_link_sec_merge_syms (h
, data
)
5973 struct elf_link_hash_entry
*h
;
5978 if (h
->root
.type
== bfd_link_hash_warning
)
5979 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5981 if ((h
->root
.type
== bfd_link_hash_defined
5982 || h
->root
.type
== bfd_link_hash_defweak
)
5983 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
5984 && elf_section_data (sec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
)
5986 bfd
*output_bfd
= (bfd
*) data
;
5988 h
->root
.u
.def
.value
=
5989 _bfd_merged_section_offset (output_bfd
,
5990 &h
->root
.u
.def
.section
,
5991 elf_section_data (sec
)->sec_info
,
5992 h
->root
.u
.def
.value
, (bfd_vma
) 0);
5998 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5999 allowing an unsatisfied unversioned symbol in the DSO to match a
6000 versioned symbol that would normally require an explicit version. */
6003 elf_link_check_versioned_symbol (info
, h
)
6004 struct bfd_link_info
*info
;
6005 struct elf_link_hash_entry
*h
;
6007 bfd
*undef_bfd
= h
->root
.u
.undef
.abfd
;
6008 struct elf_link_loaded_list
*loaded
;
6010 if ((undef_bfd
->flags
& DYNAMIC
) == 0
6011 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
6012 || elf_dt_soname (h
->root
.u
.undef
.abfd
) == NULL
)
6015 for (loaded
= elf_hash_table (info
)->loaded
;
6017 loaded
= loaded
->next
)
6020 Elf_Internal_Shdr
*hdr
;
6021 bfd_size_type symcount
;
6022 bfd_size_type extsymcount
;
6023 bfd_size_type extsymoff
;
6024 Elf_Internal_Shdr
*versymhdr
;
6025 Elf_Internal_Sym
*isym
;
6026 Elf_Internal_Sym
*isymend
;
6027 Elf_Internal_Sym
*isymbuf
;
6028 Elf_External_Versym
*ever
;
6029 Elf_External_Versym
*extversym
;
6031 input
= loaded
->abfd
;
6033 /* We check each DSO for a possible hidden versioned definition. */
6034 if (input
== undef_bfd
6035 || (input
->flags
& DYNAMIC
) == 0
6036 || elf_dynversym (input
) == 0)
6039 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6041 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6042 if (elf_bad_symtab (input
))
6044 extsymcount
= symcount
;
6049 extsymcount
= symcount
- hdr
->sh_info
;
6050 extsymoff
= hdr
->sh_info
;
6053 if (extsymcount
== 0)
6056 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6058 if (isymbuf
== NULL
)
6061 /* Read in any version definitions. */
6062 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6063 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
6064 if (extversym
== NULL
)
6067 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6068 || (bfd_bread ((PTR
) extversym
, versymhdr
->sh_size
, input
)
6069 != versymhdr
->sh_size
))
6077 ever
= extversym
+ extsymoff
;
6078 isymend
= isymbuf
+ extsymcount
;
6079 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6082 Elf_Internal_Versym iver
;
6084 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6085 || isym
->st_shndx
== SHN_UNDEF
)
6088 name
= bfd_elf_string_from_elf_section (input
,
6091 if (strcmp (name
, h
->root
.root
.string
) != 0)
6094 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6096 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6098 /* If we have a non-hidden versioned sym, then it should
6099 have provided a definition for the undefined sym. */
6103 if ((iver
.vs_vers
& VERSYM_VERSION
) == 2)
6105 /* This is the oldest (default) sym. We can use it. */
6119 /* Add an external symbol to the symbol table. This is called from
6120 the hash table traversal routine. When generating a shared object,
6121 we go through the symbol table twice. The first time we output
6122 anything that might have been forced to local scope in a version
6123 script. The second time we output the symbols that are still
6127 elf_link_output_extsym (h
, data
)
6128 struct elf_link_hash_entry
*h
;
6131 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
6132 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6134 Elf_Internal_Sym sym
;
6135 asection
*input_sec
;
6137 if (h
->root
.type
== bfd_link_hash_warning
)
6139 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6140 if (h
->root
.type
== bfd_link_hash_new
)
6144 /* Decide whether to output this symbol in this pass. */
6145 if (eoinfo
->localsyms
)
6147 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6152 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6156 /* If we are not creating a shared library, and this symbol is
6157 referenced by a shared library but is not defined anywhere, then
6158 warn that it is undefined. If we do not do this, the runtime
6159 linker will complain that the symbol is undefined when the
6160 program is run. We don't have to worry about symbols that are
6161 referenced by regular files, because we will already have issued
6162 warnings for them. */
6163 if (! finfo
->info
->relocateable
6164 && ! finfo
->info
->allow_shlib_undefined
6165 && ! finfo
->info
->shared
6166 && h
->root
.type
== bfd_link_hash_undefined
6167 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
6168 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
6169 && ! elf_link_check_versioned_symbol (finfo
->info
, h
))
6171 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6172 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6173 (asection
*) NULL
, (bfd_vma
) 0, true)))
6175 eoinfo
->failed
= true;
6180 /* We don't want to output symbols that have never been mentioned by
6181 a regular file, or that we have been told to strip. However, if
6182 h->indx is set to -2, the symbol is used by a reloc and we must
6186 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
6187 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
6188 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
6189 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
6191 else if (finfo
->info
->strip
== strip_all
6192 || (finfo
->info
->strip
== strip_some
6193 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6194 h
->root
.root
.string
,
6195 false, false) == NULL
))
6200 /* If we're stripping it, and it's not a dynamic symbol, there's
6201 nothing else to do unless it is a forced local symbol. */
6204 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6208 sym
.st_size
= h
->size
;
6209 sym
.st_other
= h
->other
;
6210 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6211 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6212 else if (h
->root
.type
== bfd_link_hash_undefweak
6213 || h
->root
.type
== bfd_link_hash_defweak
)
6214 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6216 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6218 switch (h
->root
.type
)
6221 case bfd_link_hash_new
:
6222 case bfd_link_hash_warning
:
6226 case bfd_link_hash_undefined
:
6227 case bfd_link_hash_undefweak
:
6228 input_sec
= bfd_und_section_ptr
;
6229 sym
.st_shndx
= SHN_UNDEF
;
6232 case bfd_link_hash_defined
:
6233 case bfd_link_hash_defweak
:
6235 input_sec
= h
->root
.u
.def
.section
;
6236 if (input_sec
->output_section
!= NULL
)
6239 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6240 input_sec
->output_section
);
6241 if (sym
.st_shndx
== SHN_BAD
)
6243 (*_bfd_error_handler
)
6244 (_("%s: could not find output section %s for input section %s"),
6245 bfd_get_filename (finfo
->output_bfd
),
6246 input_sec
->output_section
->name
,
6248 eoinfo
->failed
= true;
6252 /* ELF symbols in relocateable files are section relative,
6253 but in nonrelocateable files they are virtual
6255 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6256 if (! finfo
->info
->relocateable
)
6258 sym
.st_value
+= input_sec
->output_section
->vma
;
6259 if (h
->type
== STT_TLS
)
6261 /* STT_TLS symbols are relative to PT_TLS segment
6263 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6264 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
6270 BFD_ASSERT (input_sec
->owner
== NULL
6271 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6272 sym
.st_shndx
= SHN_UNDEF
;
6273 input_sec
= bfd_und_section_ptr
;
6278 case bfd_link_hash_common
:
6279 input_sec
= h
->root
.u
.c
.p
->section
;
6280 sym
.st_shndx
= SHN_COMMON
;
6281 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6284 case bfd_link_hash_indirect
:
6285 /* These symbols are created by symbol versioning. They point
6286 to the decorated version of the name. For example, if the
6287 symbol foo@@GNU_1.2 is the default, which should be used when
6288 foo is used with no version, then we add an indirect symbol
6289 foo which points to foo@@GNU_1.2. We ignore these symbols,
6290 since the indirected symbol is already in the hash table. */
6294 /* Give the processor backend a chance to tweak the symbol value,
6295 and also to finish up anything that needs to be done for this
6296 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6297 forced local syms when non-shared is due to a historical quirk. */
6298 if ((h
->dynindx
!= -1
6299 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6300 && (finfo
->info
->shared
6301 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6302 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6304 struct elf_backend_data
*bed
;
6306 bed
= get_elf_backend_data (finfo
->output_bfd
);
6307 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6308 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6310 eoinfo
->failed
= true;
6315 /* If we are marking the symbol as undefined, and there are no
6316 non-weak references to this symbol from a regular object, then
6317 mark the symbol as weak undefined; if there are non-weak
6318 references, mark the symbol as strong. We can't do this earlier,
6319 because it might not be marked as undefined until the
6320 finish_dynamic_symbol routine gets through with it. */
6321 if (sym
.st_shndx
== SHN_UNDEF
6322 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
6323 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6324 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6328 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
6329 bindtype
= STB_GLOBAL
;
6331 bindtype
= STB_WEAK
;
6332 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6335 /* If a symbol is not defined locally, we clear the visibility
6337 if (! finfo
->info
->relocateable
6338 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6339 sym
.st_other
^= ELF_ST_VISIBILITY (sym
.st_other
);
6341 /* If this symbol should be put in the .dynsym section, then put it
6342 there now. We already know the symbol index. We also fill in
6343 the entry in the .hash section. */
6344 if (h
->dynindx
!= -1
6345 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6349 size_t hash_entry_size
;
6350 bfd_byte
*bucketpos
;
6352 Elf_External_Sym
*esym
;
6354 sym
.st_name
= h
->dynstr_index
;
6355 esym
= (Elf_External_Sym
*) finfo
->dynsym_sec
->contents
+ h
->dynindx
;
6356 elf_swap_symbol_out (finfo
->output_bfd
, &sym
, (PTR
) esym
, (PTR
) 0);
6358 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6359 bucket
= h
->elf_hash_value
% bucketcount
;
6361 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6362 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6363 + (bucket
+ 2) * hash_entry_size
);
6364 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6365 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, (bfd_vma
) h
->dynindx
,
6367 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6368 ((bfd_byte
*) finfo
->hash_sec
->contents
6369 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6371 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6373 Elf_Internal_Versym iversym
;
6374 Elf_External_Versym
*eversym
;
6376 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6378 if (h
->verinfo
.verdef
== NULL
)
6379 iversym
.vs_vers
= 0;
6381 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6385 if (h
->verinfo
.vertree
== NULL
)
6386 iversym
.vs_vers
= 1;
6388 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6391 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
6392 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6394 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6395 eversym
+= h
->dynindx
;
6396 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6400 /* If we're stripping it, then it was just a dynamic symbol, and
6401 there's nothing else to do. */
6402 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6405 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6407 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
6409 eoinfo
->failed
= true;
6416 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6417 originated from the section given by INPUT_REL_HDR) to the
6421 elf_link_output_relocs (output_bfd
, input_section
, input_rel_hdr
,
6424 asection
*input_section
;
6425 Elf_Internal_Shdr
*input_rel_hdr
;
6426 Elf_Internal_Rela
*internal_relocs
;
6428 Elf_Internal_Rela
*irela
;
6429 Elf_Internal_Rela
*irelaend
;
6430 Elf_Internal_Shdr
*output_rel_hdr
;
6431 asection
*output_section
;
6432 unsigned int *rel_countp
= NULL
;
6433 struct elf_backend_data
*bed
;
6436 output_section
= input_section
->output_section
;
6437 output_rel_hdr
= NULL
;
6439 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
6440 == input_rel_hdr
->sh_entsize
)
6442 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
6443 rel_countp
= &elf_section_data (output_section
)->rel_count
;
6445 else if (elf_section_data (output_section
)->rel_hdr2
6446 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
6447 == input_rel_hdr
->sh_entsize
))
6449 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
6450 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
6454 (*_bfd_error_handler
)
6455 (_("%s: relocation size mismatch in %s section %s"),
6456 bfd_get_filename (output_bfd
),
6457 bfd_archive_filename (input_section
->owner
),
6458 input_section
->name
);
6459 bfd_set_error (bfd_error_wrong_object_format
);
6463 bed
= get_elf_backend_data (output_bfd
);
6464 irela
= internal_relocs
;
6465 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
6466 * bed
->s
->int_rels_per_ext_rel
);
6468 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
6470 Elf_External_Rel
*erel
;
6471 Elf_Internal_Rel
*irel
;
6473 amt
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
6474 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (amt
);
6477 (*_bfd_error_handler
) (_("Error: out of memory"));
6481 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
+ *rel_countp
);
6482 for (; irela
< irelaend
; irela
+= bed
->s
->int_rels_per_ext_rel
, erel
++)
6486 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
6488 irel
[i
].r_offset
= irela
[i
].r_offset
;
6489 irel
[i
].r_info
= irela
[i
].r_info
;
6490 BFD_ASSERT (irela
[i
].r_addend
== 0);
6493 if (bed
->s
->swap_reloc_out
)
6494 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, (PTR
) erel
);
6496 elf_swap_reloc_out (output_bfd
, irel
, erel
);
6503 Elf_External_Rela
*erela
;
6505 BFD_ASSERT (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
));
6507 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
+ *rel_countp
);
6508 for (; irela
< irelaend
; irela
+= bed
->s
->int_rels_per_ext_rel
, erela
++)
6509 if (bed
->s
->swap_reloca_out
)
6510 (*bed
->s
->swap_reloca_out
) (output_bfd
, irela
, (PTR
) erela
);
6512 elf_swap_reloca_out (output_bfd
, irela
, erela
);
6515 /* Bump the counter, so that we know where to add the next set of
6517 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
6522 /* Link an input file into the linker output file. This function
6523 handles all the sections and relocations of the input file at once.
6524 This is so that we only have to read the local symbols once, and
6525 don't have to keep them in memory. */
6528 elf_link_input_bfd (finfo
, input_bfd
)
6529 struct elf_final_link_info
*finfo
;
6532 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
6533 bfd
*, asection
*, bfd_byte
*,
6534 Elf_Internal_Rela
*,
6535 Elf_Internal_Sym
*, asection
**));
6537 Elf_Internal_Shdr
*symtab_hdr
;
6540 Elf_Internal_Sym
*isymbuf
;
6541 Elf_Internal_Sym
*isym
;
6542 Elf_Internal_Sym
*isymend
;
6544 asection
**ppsection
;
6546 struct elf_backend_data
*bed
;
6547 boolean emit_relocs
;
6548 struct elf_link_hash_entry
**sym_hashes
;
6550 output_bfd
= finfo
->output_bfd
;
6551 bed
= get_elf_backend_data (output_bfd
);
6552 relocate_section
= bed
->elf_backend_relocate_section
;
6554 /* If this is a dynamic object, we don't want to do anything here:
6555 we don't want the local symbols, and we don't want the section
6557 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6560 emit_relocs
= (finfo
->info
->relocateable
6561 || finfo
->info
->emitrelocations
6562 || bed
->elf_backend_emit_relocs
);
6564 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6565 if (elf_bad_symtab (input_bfd
))
6567 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6572 locsymcount
= symtab_hdr
->sh_info
;
6573 extsymoff
= symtab_hdr
->sh_info
;
6576 /* Read the local symbols. */
6577 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6578 if (isymbuf
== NULL
&& locsymcount
!= 0)
6580 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6581 finfo
->internal_syms
,
6582 finfo
->external_syms
,
6583 finfo
->locsym_shndx
);
6584 if (isymbuf
== NULL
)
6588 /* Find local symbol sections and adjust values of symbols in
6589 SEC_MERGE sections. Write out those local symbols we know are
6590 going into the output file. */
6591 isymend
= isymbuf
+ locsymcount
;
6592 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6594 isym
++, pindex
++, ppsection
++)
6598 Elf_Internal_Sym osym
;
6602 if (elf_bad_symtab (input_bfd
))
6604 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6611 if (isym
->st_shndx
== SHN_UNDEF
)
6612 isec
= bfd_und_section_ptr
;
6613 else if (isym
->st_shndx
< SHN_LORESERVE
6614 || isym
->st_shndx
> SHN_HIRESERVE
)
6616 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
6618 && elf_section_data (isec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
6619 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6621 _bfd_merged_section_offset (output_bfd
, &isec
,
6622 elf_section_data (isec
)->sec_info
,
6623 isym
->st_value
, (bfd_vma
) 0);
6625 else if (isym
->st_shndx
== SHN_ABS
)
6626 isec
= bfd_abs_section_ptr
;
6627 else if (isym
->st_shndx
== SHN_COMMON
)
6628 isec
= bfd_com_section_ptr
;
6637 /* Don't output the first, undefined, symbol. */
6638 if (ppsection
== finfo
->sections
)
6641 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6643 /* We never output section symbols. Instead, we use the
6644 section symbol of the corresponding section in the output
6649 /* If we are stripping all symbols, we don't want to output this
6651 if (finfo
->info
->strip
== strip_all
)
6654 /* If we are discarding all local symbols, we don't want to
6655 output this one. If we are generating a relocateable output
6656 file, then some of the local symbols may be required by
6657 relocs; we output them below as we discover that they are
6659 if (finfo
->info
->discard
== discard_all
)
6662 /* If this symbol is defined in a section which we are
6663 discarding, we don't need to keep it, but note that
6664 linker_mark is only reliable for sections that have contents.
6665 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6666 as well as linker_mark. */
6667 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6669 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6670 || (! finfo
->info
->relocateable
6671 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6674 /* Get the name of the symbol. */
6675 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6680 /* See if we are discarding symbols with this name. */
6681 if ((finfo
->info
->strip
== strip_some
6682 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
6684 || (((finfo
->info
->discard
== discard_sec_merge
6685 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocateable
)
6686 || finfo
->info
->discard
== discard_l
)
6687 && bfd_is_local_label_name (input_bfd
, name
)))
6690 /* If we get here, we are going to output this symbol. */
6694 /* Adjust the section index for the output file. */
6695 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6696 isec
->output_section
);
6697 if (osym
.st_shndx
== SHN_BAD
)
6700 *pindex
= bfd_get_symcount (output_bfd
);
6702 /* ELF symbols in relocateable files are section relative, but
6703 in executable files they are virtual addresses. Note that
6704 this code assumes that all ELF sections have an associated
6705 BFD section with a reasonable value for output_offset; below
6706 we assume that they also have a reasonable value for
6707 output_section. Any special sections must be set up to meet
6708 these requirements. */
6709 osym
.st_value
+= isec
->output_offset
;
6710 if (! finfo
->info
->relocateable
)
6712 osym
.st_value
+= isec
->output_section
->vma
;
6713 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6715 /* STT_TLS symbols are relative to PT_TLS segment base. */
6716 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6717 osym
.st_value
-= finfo
->first_tls_sec
->vma
;
6721 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
6725 /* Relocate the contents of each section. */
6726 sym_hashes
= elf_sym_hashes (input_bfd
);
6727 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6731 if (! o
->linker_mark
)
6733 /* This section was omitted from the link. */
6737 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6738 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6741 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6743 /* Section was created by elf_link_create_dynamic_sections
6748 /* Get the contents of the section. They have been cached by a
6749 relaxation routine. Note that o is a section in an input
6750 file, so the contents field will not have been set by any of
6751 the routines which work on output files. */
6752 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6753 contents
= elf_section_data (o
)->this_hdr
.contents
;
6756 contents
= finfo
->contents
;
6757 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
6758 (file_ptr
) 0, o
->_raw_size
))
6762 if ((o
->flags
& SEC_RELOC
) != 0)
6764 Elf_Internal_Rela
*internal_relocs
;
6766 /* Get the swapped relocs. */
6767 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
6768 (input_bfd
, o
, finfo
->external_relocs
,
6769 finfo
->internal_relocs
, false));
6770 if (internal_relocs
== NULL
6771 && o
->reloc_count
> 0)
6774 /* Run through the relocs looking for any against symbols
6775 from discarded sections and section symbols from
6776 removed link-once sections. Complain about relocs
6777 against discarded sections. Zero relocs against removed
6778 link-once sections. We should really complain if
6779 anything in the final link tries to use it, but
6780 DWARF-based exception handling might have an entry in
6781 .eh_frame to describe a routine in the linkonce section,
6782 and it turns out to be hard to remove the .eh_frame
6783 entry too. FIXME. */
6784 if (!finfo
->info
->relocateable
6785 && !elf_section_ignore_discarded_relocs (o
))
6787 Elf_Internal_Rela
*rel
, *relend
;
6789 rel
= internal_relocs
;
6790 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6791 for ( ; rel
< relend
; rel
++)
6793 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
6795 if (r_symndx
>= locsymcount
6796 || (elf_bad_symtab (input_bfd
)
6797 && finfo
->sections
[r_symndx
] == NULL
))
6799 struct elf_link_hash_entry
*h
;
6801 h
= sym_hashes
[r_symndx
- extsymoff
];
6802 while (h
->root
.type
== bfd_link_hash_indirect
6803 || h
->root
.type
== bfd_link_hash_warning
)
6804 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6806 /* Complain if the definition comes from a
6807 discarded section. */
6808 if ((h
->root
.type
== bfd_link_hash_defined
6809 || h
->root
.type
== bfd_link_hash_defweak
)
6810 && elf_discarded_section (h
->root
.u
.def
.section
))
6812 #if BFD_VERSION_DATE < 20031005
6813 if ((o
->flags
& SEC_DEBUGGING
) != 0)
6815 #if BFD_VERSION_DATE > 20021005
6816 (*finfo
->info
->callbacks
->warning
)
6818 _("warning: relocation against removed section; zeroing"),
6819 NULL
, input_bfd
, o
, rel
->r_offset
);
6821 BFD_ASSERT (r_symndx
!= 0);
6822 memset (rel
, 0, sizeof (*rel
));
6827 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6828 (finfo
->info
, h
->root
.root
.string
,
6829 input_bfd
, o
, rel
->r_offset
,
6837 asection
*sec
= finfo
->sections
[r_symndx
];
6839 if (sec
!= NULL
&& elf_discarded_section (sec
))
6841 #if BFD_VERSION_DATE < 20031005
6842 if ((o
->flags
& SEC_DEBUGGING
) != 0
6843 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
6845 #if BFD_VERSION_DATE > 20021005
6846 (*finfo
->info
->callbacks
->warning
)
6848 _("warning: relocation against removed section"),
6849 NULL
, input_bfd
, o
, rel
->r_offset
);
6851 BFD_ASSERT (r_symndx
!= 0);
6853 = ELF_R_INFO (0, ELF_R_TYPE (rel
->r_info
));
6861 = _("local symbols in discarded section %s");
6863 = strlen (sec
->name
) + strlen (msg
) - 1;
6864 char *buf
= (char *) bfd_malloc (amt
);
6867 sprintf (buf
, msg
, sec
->name
);
6869 buf
= (char *) sec
->name
;
6870 ok
= (*finfo
->info
->callbacks
6871 ->undefined_symbol
) (finfo
->info
, buf
,
6875 if (buf
!= sec
->name
)
6885 /* Relocate the section by invoking a back end routine.
6887 The back end routine is responsible for adjusting the
6888 section contents as necessary, and (if using Rela relocs
6889 and generating a relocateable output file) adjusting the
6890 reloc addend as necessary.
6892 The back end routine does not have to worry about setting
6893 the reloc address or the reloc symbol index.
6895 The back end routine is given a pointer to the swapped in
6896 internal symbols, and can access the hash table entries
6897 for the external symbols via elf_sym_hashes (input_bfd).
6899 When generating relocateable output, the back end routine
6900 must handle STB_LOCAL/STT_SECTION symbols specially. The
6901 output symbol is going to be a section symbol
6902 corresponding to the output section, which will require
6903 the addend to be adjusted. */
6905 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6906 input_bfd
, o
, contents
,
6914 Elf_Internal_Rela
*irela
;
6915 Elf_Internal_Rela
*irelaend
;
6916 struct elf_link_hash_entry
**rel_hash
;
6917 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6918 unsigned int next_erel
;
6919 boolean (*reloc_emitter
) PARAMS ((bfd
*, asection
*,
6920 Elf_Internal_Shdr
*,
6921 Elf_Internal_Rela
*));
6922 boolean rela_normal
;
6924 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6925 rela_normal
= (bed
->rela_normal
6926 && (input_rel_hdr
->sh_entsize
6927 == sizeof (Elf_External_Rela
)));
6929 /* Adjust the reloc addresses and symbol indices. */
6931 irela
= internal_relocs
;
6932 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6933 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6934 + elf_section_data (o
->output_section
)->rel_count
6935 + elf_section_data (o
->output_section
)->rel_count2
);
6936 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6938 unsigned long r_symndx
;
6940 Elf_Internal_Sym sym
;
6942 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6948 irela
->r_offset
+= o
->output_offset
;
6950 /* Relocs in an executable have to be virtual addresses. */
6951 if (!finfo
->info
->relocateable
)
6952 irela
->r_offset
+= o
->output_section
->vma
;
6954 r_symndx
= ELF_R_SYM (irela
->r_info
);
6959 if (r_symndx
>= locsymcount
6960 || (elf_bad_symtab (input_bfd
)
6961 && finfo
->sections
[r_symndx
] == NULL
))
6963 struct elf_link_hash_entry
*rh
;
6966 /* This is a reloc against a global symbol. We
6967 have not yet output all the local symbols, so
6968 we do not know the symbol index of any global
6969 symbol. We set the rel_hash entry for this
6970 reloc to point to the global hash table entry
6971 for this symbol. The symbol index is then
6972 set at the end of elf_bfd_final_link. */
6973 indx
= r_symndx
- extsymoff
;
6974 rh
= elf_sym_hashes (input_bfd
)[indx
];
6975 while (rh
->root
.type
== bfd_link_hash_indirect
6976 || rh
->root
.type
== bfd_link_hash_warning
)
6977 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6979 /* Setting the index to -2 tells
6980 elf_link_output_extsym that this symbol is
6982 BFD_ASSERT (rh
->indx
< 0);
6990 /* This is a reloc against a local symbol. */
6993 sym
= isymbuf
[r_symndx
];
6994 sec
= finfo
->sections
[r_symndx
];
6995 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
6997 /* I suppose the backend ought to fill in the
6998 section of any STT_SECTION symbol against a
6999 processor specific section. If we have
7000 discarded a section, the output_section will
7001 be the absolute section. */
7002 if (bfd_is_abs_section (sec
)
7004 && bfd_is_abs_section (sec
->output_section
)))
7006 else if (sec
== NULL
|| sec
->owner
== NULL
)
7008 bfd_set_error (bfd_error_bad_value
);
7013 r_symndx
= sec
->output_section
->target_index
;
7014 BFD_ASSERT (r_symndx
!= 0);
7017 /* Adjust the addend according to where the
7018 section winds up in the output section. */
7020 irela
->r_addend
+= sec
->output_offset
;
7024 if (finfo
->indices
[r_symndx
] == -1)
7026 unsigned long shlink
;
7030 if (finfo
->info
->strip
== strip_all
)
7032 /* You can't do ld -r -s. */
7033 bfd_set_error (bfd_error_invalid_operation
);
7037 /* This symbol was skipped earlier, but
7038 since it is needed by a reloc, we
7039 must output it now. */
7040 shlink
= symtab_hdr
->sh_link
;
7041 name
= (bfd_elf_string_from_elf_section
7042 (input_bfd
, shlink
, sym
.st_name
));
7046 osec
= sec
->output_section
;
7048 _bfd_elf_section_from_bfd_section (output_bfd
,
7050 if (sym
.st_shndx
== SHN_BAD
)
7053 sym
.st_value
+= sec
->output_offset
;
7054 if (! finfo
->info
->relocateable
)
7056 sym
.st_value
+= osec
->vma
;
7057 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7059 /* STT_TLS symbols are relative to PT_TLS
7061 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
7062 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
7066 finfo
->indices
[r_symndx
]
7067 = bfd_get_symcount (output_bfd
);
7069 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
))
7073 r_symndx
= finfo
->indices
[r_symndx
];
7076 irela
->r_info
= ELF_R_INFO (r_symndx
,
7077 ELF_R_TYPE (irela
->r_info
));
7080 /* Swap out the relocs. */
7081 if (bed
->elf_backend_emit_relocs
7082 && !(finfo
->info
->relocateable
7083 || finfo
->info
->emitrelocations
))
7084 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7086 reloc_emitter
= elf_link_output_relocs
;
7088 if (input_rel_hdr
->sh_size
!= 0
7089 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7093 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7094 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7096 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7097 * bed
->s
->int_rels_per_ext_rel
);
7098 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
7105 /* Write out the modified section contents. */
7106 if (bed
->elf_backend_write_section
7107 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7109 /* Section written out. */
7111 else switch (elf_section_data (o
)->sec_info_type
)
7113 case ELF_INFO_TYPE_STABS
:
7114 if (! (_bfd_write_section_stabs
7116 &elf_hash_table (finfo
->info
)->stab_info
,
7117 o
, &elf_section_data (o
)->sec_info
, contents
)))
7120 case ELF_INFO_TYPE_MERGE
:
7121 if (! (_bfd_write_merged_section
7122 (output_bfd
, o
, elf_section_data (o
)->sec_info
)))
7125 case ELF_INFO_TYPE_EH_FRAME
:
7130 = bfd_get_section_by_name (elf_hash_table (finfo
->info
)->dynobj
,
7132 if (! (_bfd_elf_write_section_eh_frame (output_bfd
, o
, ehdrsec
,
7139 bfd_size_type sec_size
;
7141 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
7142 if (! (o
->flags
& SEC_EXCLUDE
)
7143 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7145 (file_ptr
) o
->output_offset
,
7156 /* Generate a reloc when linking an ELF file. This is a reloc
7157 requested by the linker, and does come from any input file. This
7158 is used to build constructor and destructor tables when linking
7162 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
7164 struct bfd_link_info
*info
;
7165 asection
*output_section
;
7166 struct bfd_link_order
*link_order
;
7168 reloc_howto_type
*howto
;
7172 struct elf_link_hash_entry
**rel_hash_ptr
;
7173 Elf_Internal_Shdr
*rel_hdr
;
7174 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7176 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7179 bfd_set_error (bfd_error_bad_value
);
7183 addend
= link_order
->u
.reloc
.p
->addend
;
7185 /* Figure out the symbol index. */
7186 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7187 + elf_section_data (output_section
)->rel_count
7188 + elf_section_data (output_section
)->rel_count2
);
7189 if (link_order
->type
== bfd_section_reloc_link_order
)
7191 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7192 BFD_ASSERT (indx
!= 0);
7193 *rel_hash_ptr
= NULL
;
7197 struct elf_link_hash_entry
*h
;
7199 /* Treat a reloc against a defined symbol as though it were
7200 actually against the section. */
7201 h
= ((struct elf_link_hash_entry
*)
7202 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7203 link_order
->u
.reloc
.p
->u
.name
,
7204 false, false, true));
7206 && (h
->root
.type
== bfd_link_hash_defined
7207 || h
->root
.type
== bfd_link_hash_defweak
))
7211 section
= h
->root
.u
.def
.section
;
7212 indx
= section
->output_section
->target_index
;
7213 *rel_hash_ptr
= NULL
;
7214 /* It seems that we ought to add the symbol value to the
7215 addend here, but in practice it has already been added
7216 because it was passed to constructor_callback. */
7217 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7221 /* Setting the index to -2 tells elf_link_output_extsym that
7222 this symbol is used by a reloc. */
7229 if (! ((*info
->callbacks
->unattached_reloc
)
7230 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
7231 (asection
*) NULL
, (bfd_vma
) 0)))
7237 /* If this is an inplace reloc, we must write the addend into the
7239 if (howto
->partial_inplace
&& addend
!= 0)
7242 bfd_reloc_status_type rstat
;
7245 const char *sym_name
;
7247 size
= bfd_get_reloc_size (howto
);
7248 buf
= (bfd_byte
*) bfd_zmalloc (size
);
7249 if (buf
== (bfd_byte
*) NULL
)
7251 rstat
= _bfd_relocate_contents (howto
, output_bfd
, (bfd_vma
) addend
, buf
);
7258 case bfd_reloc_outofrange
:
7261 case bfd_reloc_overflow
:
7262 if (link_order
->type
== bfd_section_reloc_link_order
)
7263 sym_name
= bfd_section_name (output_bfd
,
7264 link_order
->u
.reloc
.p
->u
.section
);
7266 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7267 if (! ((*info
->callbacks
->reloc_overflow
)
7268 (info
, sym_name
, howto
->name
, addend
,
7269 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
7276 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
7277 (file_ptr
) link_order
->offset
, size
);
7283 /* The address of a reloc is relative to the section in a
7284 relocateable file, and is a virtual address in an executable
7286 offset
= link_order
->offset
;
7287 if (! info
->relocateable
)
7288 offset
+= output_section
->vma
;
7290 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7292 if (rel_hdr
->sh_type
== SHT_REL
)
7295 Elf_Internal_Rel
*irel
;
7296 Elf_External_Rel
*erel
;
7299 size
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
7300 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (size
);
7304 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7305 irel
[i
].r_offset
= offset
;
7306 irel
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
7308 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
7309 + elf_section_data (output_section
)->rel_count
);
7311 if (bed
->s
->swap_reloc_out
)
7312 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, (bfd_byte
*) erel
);
7314 elf_swap_reloc_out (output_bfd
, irel
, erel
);
7321 Elf_Internal_Rela
*irela
;
7322 Elf_External_Rela
*erela
;
7325 size
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
7326 irela
= (Elf_Internal_Rela
*) bfd_zmalloc (size
);
7330 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7331 irela
[i
].r_offset
= offset
;
7332 irela
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
7333 irela
[0].r_addend
= addend
;
7335 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
7336 + elf_section_data (output_section
)->rel_count
);
7338 if (bed
->s
->swap_reloca_out
)
7339 (*bed
->s
->swap_reloca_out
) (output_bfd
, irela
, (bfd_byte
*) erela
);
7341 elf_swap_reloca_out (output_bfd
, irela
, erela
);
7344 ++elf_section_data (output_section
)->rel_count
;
7349 /* Allocate a pointer to live in a linker created section. */
7352 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
7354 struct bfd_link_info
*info
;
7355 elf_linker_section_t
*lsect
;
7356 struct elf_link_hash_entry
*h
;
7357 const Elf_Internal_Rela
*rel
;
7359 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
7360 elf_linker_section_pointers_t
*linker_section_ptr
;
7361 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7364 BFD_ASSERT (lsect
!= NULL
);
7366 /* Is this a global symbol? */
7369 /* Has this symbol already been allocated? If so, our work is done. */
7370 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
7375 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
7376 /* Make sure this symbol is output as a dynamic symbol. */
7377 if (h
->dynindx
== -1)
7379 if (! elf_link_record_dynamic_symbol (info
, h
))
7383 if (lsect
->rel_section
)
7384 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7388 /* Allocation of a pointer to a local symbol. */
7389 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
7391 /* Allocate a table to hold the local symbols if first time. */
7394 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
7395 register unsigned int i
;
7398 amt
*= sizeof (elf_linker_section_pointers_t
*);
7399 ptr
= (elf_linker_section_pointers_t
**) bfd_alloc (abfd
, amt
);
7404 elf_local_ptr_offsets (abfd
) = ptr
;
7405 for (i
= 0; i
< num_symbols
; i
++)
7406 ptr
[i
] = (elf_linker_section_pointers_t
*) 0;
7409 /* Has this symbol already been allocated? If so, our work is done. */
7410 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
7415 ptr_linker_section_ptr
= &ptr
[r_symndx
];
7419 /* If we are generating a shared object, we need to
7420 output a R_<xxx>_RELATIVE reloc so that the
7421 dynamic linker can adjust this GOT entry. */
7422 BFD_ASSERT (lsect
->rel_section
!= NULL
);
7423 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7427 /* Allocate space for a pointer in the linker section, and allocate
7428 a new pointer record from internal memory. */
7429 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
7430 amt
= sizeof (elf_linker_section_pointers_t
);
7431 linker_section_ptr
= (elf_linker_section_pointers_t
*) bfd_alloc (abfd
, amt
);
7433 if (!linker_section_ptr
)
7436 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
7437 linker_section_ptr
->addend
= rel
->r_addend
;
7438 linker_section_ptr
->which
= lsect
->which
;
7439 linker_section_ptr
->written_address_p
= false;
7440 *ptr_linker_section_ptr
= linker_section_ptr
;
7443 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
7445 linker_section_ptr
->offset
= (lsect
->section
->_raw_size
7446 - lsect
->hole_size
+ (ARCH_SIZE
/ 8));
7447 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
7448 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
7449 if (lsect
->sym_hash
)
7451 /* Bump up symbol value if needed. */
7452 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
7454 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
7455 lsect
->sym_hash
->root
.root
.string
,
7456 (long) ARCH_SIZE
/ 8,
7457 (long) lsect
->sym_hash
->root
.u
.def
.value
);
7463 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
7465 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
7469 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7470 lsect
->name
, (long) linker_section_ptr
->offset
,
7471 (long) lsect
->section
->_raw_size
);
7478 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7481 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7484 /* Fill in the address for a pointer generated in a linker section. */
7487 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
,
7488 relocation
, rel
, relative_reloc
)
7491 struct bfd_link_info
*info
;
7492 elf_linker_section_t
*lsect
;
7493 struct elf_link_hash_entry
*h
;
7495 const Elf_Internal_Rela
*rel
;
7498 elf_linker_section_pointers_t
*linker_section_ptr
;
7500 BFD_ASSERT (lsect
!= NULL
);
7504 /* Handle global symbol. */
7505 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7506 (h
->linker_section_pointer
,
7510 BFD_ASSERT (linker_section_ptr
!= NULL
);
7512 if (! elf_hash_table (info
)->dynamic_sections_created
7515 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
7517 /* This is actually a static link, or it is a
7518 -Bsymbolic link and the symbol is defined
7519 locally. We must initialize this entry in the
7522 When doing a dynamic link, we create a .rela.<xxx>
7523 relocation entry to initialize the value. This
7524 is done in the finish_dynamic_symbol routine. */
7525 if (!linker_section_ptr
->written_address_p
)
7527 linker_section_ptr
->written_address_p
= true;
7528 bfd_put_ptr (output_bfd
,
7529 relocation
+ linker_section_ptr
->addend
,
7530 (lsect
->section
->contents
7531 + linker_section_ptr
->offset
));
7537 /* Handle local symbol. */
7538 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7539 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
7540 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
7541 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7542 (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
7546 BFD_ASSERT (linker_section_ptr
!= NULL
);
7548 /* Write out pointer if it hasn't been rewritten out before. */
7549 if (!linker_section_ptr
->written_address_p
)
7551 linker_section_ptr
->written_address_p
= true;
7552 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
7553 lsect
->section
->contents
+ linker_section_ptr
->offset
);
7557 asection
*srel
= lsect
->rel_section
;
7558 Elf_Internal_Rela
*outrel
;
7559 Elf_External_Rela
*erel
;
7560 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7564 amt
= sizeof (Elf_Internal_Rela
) * bed
->s
->int_rels_per_ext_rel
;
7565 outrel
= (Elf_Internal_Rela
*) bfd_zmalloc (amt
);
7568 (*_bfd_error_handler
) (_("Error: out of memory"));
7572 /* We need to generate a relative reloc for the dynamic
7576 srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
7578 lsect
->rel_section
= srel
;
7581 BFD_ASSERT (srel
!= NULL
);
7583 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7584 outrel
[i
].r_offset
= (lsect
->section
->output_section
->vma
7585 + lsect
->section
->output_offset
7586 + linker_section_ptr
->offset
);
7587 outrel
[0].r_info
= ELF_R_INFO (0, relative_reloc
);
7588 outrel
[0].r_addend
= 0;
7589 erel
= (Elf_External_Rela
*) lsect
->section
->contents
;
7590 erel
+= elf_section_data (lsect
->section
)->rel_count
;
7591 elf_swap_reloca_out (output_bfd
, outrel
, erel
);
7592 ++elf_section_data (lsect
->section
)->rel_count
;
7599 relocation
= (lsect
->section
->output_offset
7600 + linker_section_ptr
->offset
7601 - lsect
->hole_offset
7602 - lsect
->sym_offset
);
7606 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7607 lsect
->name
, (long) relocation
, (long) relocation
);
7610 /* Subtract out the addend, because it will get added back in by the normal
7612 return relocation
- linker_section_ptr
->addend
;
7615 /* Garbage collect unused sections. */
7617 static boolean elf_gc_mark
7618 PARAMS ((struct bfd_link_info
*, asection
*,
7619 asection
* (*) (asection
*, struct bfd_link_info
*,
7620 Elf_Internal_Rela
*, struct elf_link_hash_entry
*,
7621 Elf_Internal_Sym
*)));
7623 static boolean elf_gc_sweep
7624 PARAMS ((struct bfd_link_info
*,
7625 boolean (*) (bfd
*, struct bfd_link_info
*, asection
*,
7626 const Elf_Internal_Rela
*)));
7628 static boolean elf_gc_sweep_symbol
7629 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7631 static boolean elf_gc_allocate_got_offsets
7632 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7634 static boolean elf_gc_propagate_vtable_entries_used
7635 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7637 static boolean elf_gc_smash_unused_vtentry_relocs
7638 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7640 /* The mark phase of garbage collection. For a given section, mark
7641 it and any sections in this section's group, and all the sections
7642 which define symbols to which it refers. */
7645 elf_gc_mark (info
, sec
, gc_mark_hook
)
7646 struct bfd_link_info
*info
;
7648 asection
* (*gc_mark_hook
) PARAMS ((asection
*, struct bfd_link_info
*,
7649 Elf_Internal_Rela
*,
7650 struct elf_link_hash_entry
*,
7651 Elf_Internal_Sym
*));
7654 asection
*group_sec
;
7658 /* Mark all the sections in the group. */
7659 group_sec
= elf_section_data (sec
)->next_in_group
;
7660 if (group_sec
&& !group_sec
->gc_mark
)
7661 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
7664 /* Look through the section relocs. */
7666 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
7668 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
7669 Elf_Internal_Shdr
*symtab_hdr
;
7670 struct elf_link_hash_entry
**sym_hashes
;
7673 bfd
*input_bfd
= sec
->owner
;
7674 struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
7675 Elf_Internal_Sym
*isym
= NULL
;
7677 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
7678 sym_hashes
= elf_sym_hashes (input_bfd
);
7680 /* Read the local symbols. */
7681 if (elf_bad_symtab (input_bfd
))
7683 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
7687 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
7689 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
7690 if (isym
== NULL
&& nlocsyms
!= 0)
7692 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
7698 /* Read the relocations. */
7699 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
7700 (input_bfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
7701 info
->keep_memory
));
7702 if (relstart
== NULL
)
7707 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7709 for (rel
= relstart
; rel
< relend
; rel
++)
7711 unsigned long r_symndx
;
7713 struct elf_link_hash_entry
*h
;
7715 r_symndx
= ELF_R_SYM (rel
->r_info
);
7719 if (r_symndx
>= nlocsyms
7720 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
7722 h
= sym_hashes
[r_symndx
- extsymoff
];
7723 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
7727 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
7730 if (rsec
&& !rsec
->gc_mark
)
7732 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
7734 else if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
7743 if (elf_section_data (sec
)->relocs
!= relstart
)
7746 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
7748 if (! info
->keep_memory
)
7751 symtab_hdr
->contents
= (unsigned char *) isym
;
7758 /* The sweep phase of garbage collection. Remove all garbage sections. */
7761 elf_gc_sweep (info
, gc_sweep_hook
)
7762 struct bfd_link_info
*info
;
7763 boolean (*gc_sweep_hook
) PARAMS ((bfd
*, struct bfd_link_info
*,
7764 asection
*, const Elf_Internal_Rela
*));
7768 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7772 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
7775 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7777 /* Keep special sections. Keep .debug sections. */
7778 if ((o
->flags
& SEC_LINKER_CREATED
)
7779 || (o
->flags
& SEC_DEBUGGING
))
7785 /* Skip sweeping sections already excluded. */
7786 if (o
->flags
& SEC_EXCLUDE
)
7789 /* Since this is early in the link process, it is simple
7790 to remove a section from the output. */
7791 o
->flags
|= SEC_EXCLUDE
;
7793 /* But we also have to update some of the relocation
7794 info we collected before. */
7796 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
7798 Elf_Internal_Rela
*internal_relocs
;
7801 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
7802 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
7803 if (internal_relocs
== NULL
)
7806 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
7808 if (elf_section_data (o
)->relocs
!= internal_relocs
)
7809 free (internal_relocs
);
7817 /* Remove the symbols that were in the swept sections from the dynamic
7818 symbol table. GCFIXME: Anyone know how to get them out of the
7819 static symbol table as well? */
7823 elf_link_hash_traverse (elf_hash_table (info
),
7824 elf_gc_sweep_symbol
,
7827 elf_hash_table (info
)->dynsymcount
= i
;
7833 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7836 elf_gc_sweep_symbol (h
, idxptr
)
7837 struct elf_link_hash_entry
*h
;
7840 int *idx
= (int *) idxptr
;
7842 if (h
->root
.type
== bfd_link_hash_warning
)
7843 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7845 if (h
->dynindx
!= -1
7846 && ((h
->root
.type
!= bfd_link_hash_defined
7847 && h
->root
.type
!= bfd_link_hash_defweak
)
7848 || h
->root
.u
.def
.section
->gc_mark
))
7849 h
->dynindx
= (*idx
)++;
7854 /* Propogate collected vtable information. This is called through
7855 elf_link_hash_traverse. */
7858 elf_gc_propagate_vtable_entries_used (h
, okp
)
7859 struct elf_link_hash_entry
*h
;
7862 if (h
->root
.type
== bfd_link_hash_warning
)
7863 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7865 /* Those that are not vtables. */
7866 if (h
->vtable_parent
== NULL
)
7869 /* Those vtables that do not have parents, we cannot merge. */
7870 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
7873 /* If we've already been done, exit. */
7874 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
7877 /* Make sure the parent's table is up to date. */
7878 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
7880 if (h
->vtable_entries_used
== NULL
)
7882 /* None of this table's entries were referenced. Re-use the
7884 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
7885 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
7892 /* Or the parent's entries into ours. */
7893 cu
= h
->vtable_entries_used
;
7895 pu
= h
->vtable_parent
->vtable_entries_used
;
7898 asection
*sec
= h
->root
.u
.def
.section
;
7899 struct elf_backend_data
*bed
= get_elf_backend_data (sec
->owner
);
7900 int file_align
= bed
->s
->file_align
;
7902 n
= h
->vtable_parent
->vtable_entries_size
/ file_align
;
7917 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
7918 struct elf_link_hash_entry
*h
;
7922 bfd_vma hstart
, hend
;
7923 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
7924 struct elf_backend_data
*bed
;
7927 if (h
->root
.type
== bfd_link_hash_warning
)
7928 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7930 /* Take care of both those symbols that do not describe vtables as
7931 well as those that are not loaded. */
7932 if (h
->vtable_parent
== NULL
)
7935 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
7936 || h
->root
.type
== bfd_link_hash_defweak
);
7938 sec
= h
->root
.u
.def
.section
;
7939 hstart
= h
->root
.u
.def
.value
;
7940 hend
= hstart
+ h
->size
;
7942 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
7943 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, true));
7945 return *(boolean
*) okp
= false;
7946 bed
= get_elf_backend_data (sec
->owner
);
7947 file_align
= bed
->s
->file_align
;
7949 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7951 for (rel
= relstart
; rel
< relend
; ++rel
)
7952 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
7954 /* If the entry is in use, do nothing. */
7955 if (h
->vtable_entries_used
7956 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
7958 bfd_vma entry
= (rel
->r_offset
- hstart
) / file_align
;
7959 if (h
->vtable_entries_used
[entry
])
7962 /* Otherwise, kill it. */
7963 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
7969 /* Do mark and sweep of unused sections. */
7972 elf_gc_sections (abfd
, info
)
7974 struct bfd_link_info
*info
;
7978 asection
* (*gc_mark_hook
)
7979 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7980 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
7982 if (!get_elf_backend_data (abfd
)->can_gc_sections
7983 || info
->relocateable
|| info
->emitrelocations
7984 || elf_hash_table (info
)->dynamic_sections_created
)
7987 /* Apply transitive closure to the vtable entry usage info. */
7988 elf_link_hash_traverse (elf_hash_table (info
),
7989 elf_gc_propagate_vtable_entries_used
,
7994 /* Kill the vtable relocations that were not used. */
7995 elf_link_hash_traverse (elf_hash_table (info
),
7996 elf_gc_smash_unused_vtentry_relocs
,
8001 /* Grovel through relocs to find out who stays ... */
8003 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8004 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8008 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8011 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8013 if (o
->flags
& SEC_KEEP
)
8014 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
8019 /* ... and mark SEC_EXCLUDE for those that go. */
8020 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8026 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
8029 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
8032 struct elf_link_hash_entry
*h
;
8035 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8036 struct elf_link_hash_entry
**search
, *child
;
8037 bfd_size_type extsymcount
;
8039 /* The sh_info field of the symtab header tells us where the
8040 external symbols start. We don't care about the local symbols at
8042 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
8043 if (!elf_bad_symtab (abfd
))
8044 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8046 sym_hashes
= elf_sym_hashes (abfd
);
8047 sym_hashes_end
= sym_hashes
+ extsymcount
;
8049 /* Hunt down the child symbol, which is in this section at the same
8050 offset as the relocation. */
8051 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8053 if ((child
= *search
) != NULL
8054 && (child
->root
.type
== bfd_link_hash_defined
8055 || child
->root
.type
== bfd_link_hash_defweak
)
8056 && child
->root
.u
.def
.section
== sec
8057 && child
->root
.u
.def
.value
== offset
)
8061 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
8062 bfd_archive_filename (abfd
), sec
->name
,
8063 (unsigned long) offset
);
8064 bfd_set_error (bfd_error_invalid_operation
);
8070 /* This *should* only be the absolute section. It could potentially
8071 be that someone has defined a non-global vtable though, which
8072 would be bad. It isn't worth paging in the local symbols to be
8073 sure though; that case should simply be handled by the assembler. */
8075 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
8078 child
->vtable_parent
= h
;
8083 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
8086 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
8087 bfd
*abfd ATTRIBUTE_UNUSED
;
8088 asection
*sec ATTRIBUTE_UNUSED
;
8089 struct elf_link_hash_entry
*h
;
8092 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8093 int file_align
= bed
->s
->file_align
;
8095 if (addend
>= h
->vtable_entries_size
)
8098 boolean
*ptr
= h
->vtable_entries_used
;
8100 /* While the symbol is undefined, we have to be prepared to handle
8102 if (h
->root
.type
== bfd_link_hash_undefined
)
8109 /* Oops! We've got a reference past the defined end of
8110 the table. This is probably a bug -- shall we warn? */
8115 /* Allocate one extra entry for use as a "done" flag for the
8116 consolidation pass. */
8117 bytes
= (size
/ file_align
+ 1) * sizeof (boolean
);
8121 ptr
= bfd_realloc (ptr
- 1, (bfd_size_type
) bytes
);
8127 oldbytes
= ((h
->vtable_entries_size
/ file_align
+ 1)
8128 * sizeof (boolean
));
8129 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8133 ptr
= bfd_zmalloc ((bfd_size_type
) bytes
);
8138 /* And arrange for that done flag to be at index -1. */
8139 h
->vtable_entries_used
= ptr
+ 1;
8140 h
->vtable_entries_size
= size
;
8143 h
->vtable_entries_used
[addend
/ file_align
] = true;
8148 /* And an accompanying bit to work out final got entry offsets once
8149 we're done. Should be called from final_link. */
8152 elf_gc_common_finalize_got_offsets (abfd
, info
)
8154 struct bfd_link_info
*info
;
8157 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8160 /* The GOT offset is relative to the .got section, but the GOT header is
8161 put into the .got.plt section, if the backend uses it. */
8162 if (bed
->want_got_plt
)
8165 gotoff
= bed
->got_header_size
;
8167 /* Do the local .got entries first. */
8168 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
8170 bfd_signed_vma
*local_got
;
8171 bfd_size_type j
, locsymcount
;
8172 Elf_Internal_Shdr
*symtab_hdr
;
8174 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
8177 local_got
= elf_local_got_refcounts (i
);
8181 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
8182 if (elf_bad_symtab (i
))
8183 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8185 locsymcount
= symtab_hdr
->sh_info
;
8187 for (j
= 0; j
< locsymcount
; ++j
)
8189 if (local_got
[j
] > 0)
8191 local_got
[j
] = gotoff
;
8192 gotoff
+= ARCH_SIZE
/ 8;
8195 local_got
[j
] = (bfd_vma
) -1;
8199 /* Then the global .got entries. .plt refcounts are handled by
8200 adjust_dynamic_symbol */
8201 elf_link_hash_traverse (elf_hash_table (info
),
8202 elf_gc_allocate_got_offsets
,
8207 /* We need a special top-level link routine to convert got reference counts
8208 to real got offsets. */
8211 elf_gc_allocate_got_offsets (h
, offarg
)
8212 struct elf_link_hash_entry
*h
;
8215 bfd_vma
*off
= (bfd_vma
*) offarg
;
8217 if (h
->root
.type
== bfd_link_hash_warning
)
8218 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8220 if (h
->got
.refcount
> 0)
8222 h
->got
.offset
= off
[0];
8223 off
[0] += ARCH_SIZE
/ 8;
8226 h
->got
.offset
= (bfd_vma
) -1;
8231 /* Many folk need no more in the way of final link than this, once
8232 got entry reference counting is enabled. */
8235 elf_gc_common_final_link (abfd
, info
)
8237 struct bfd_link_info
*info
;
8239 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
8242 /* Invoke the regular ELF backend linker to do all the work. */
8243 return elf_bfd_final_link (abfd
, info
);
8246 /* This function will be called though elf_link_hash_traverse to store
8247 all hash value of the exported symbols in an array. */
8250 elf_collect_hash_codes (h
, data
)
8251 struct elf_link_hash_entry
*h
;
8254 unsigned long **valuep
= (unsigned long **) data
;
8260 if (h
->root
.type
== bfd_link_hash_warning
)
8261 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8263 /* Ignore indirect symbols. These are added by the versioning code. */
8264 if (h
->dynindx
== -1)
8267 name
= h
->root
.root
.string
;
8268 p
= strchr (name
, ELF_VER_CHR
);
8271 alc
= bfd_malloc ((bfd_size_type
) (p
- name
+ 1));
8272 memcpy (alc
, name
, (size_t) (p
- name
));
8273 alc
[p
- name
] = '\0';
8277 /* Compute the hash value. */
8278 ha
= bfd_elf_hash (name
);
8280 /* Store the found hash value in the array given as the argument. */
8283 /* And store it in the struct so that we can put it in the hash table
8285 h
->elf_hash_value
= ha
;
8294 elf_reloc_symbol_deleted_p (offset
, cookie
)
8298 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
8300 if (rcookie
->bad_symtab
)
8301 rcookie
->rel
= rcookie
->rels
;
8303 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
8305 unsigned long r_symndx
= ELF_R_SYM (rcookie
->rel
->r_info
);
8307 if (! rcookie
->bad_symtab
)
8308 if (rcookie
->rel
->r_offset
> offset
)
8310 if (rcookie
->rel
->r_offset
!= offset
)
8313 if (r_symndx
>= rcookie
->locsymcount
8314 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
8316 struct elf_link_hash_entry
*h
;
8318 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
8320 while (h
->root
.type
== bfd_link_hash_indirect
8321 || h
->root
.type
== bfd_link_hash_warning
)
8322 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8324 if ((h
->root
.type
== bfd_link_hash_defined
8325 || h
->root
.type
== bfd_link_hash_defweak
)
8326 && elf_discarded_section (h
->root
.u
.def
.section
))
8333 /* It's not a relocation against a global symbol,
8334 but it could be a relocation against a local
8335 symbol for a discarded section. */
8337 Elf_Internal_Sym
*isym
;
8339 /* Need to: get the symbol; get the section. */
8340 isym
= &rcookie
->locsyms
[r_symndx
];
8341 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
8343 isec
= section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
8344 if (isec
!= NULL
&& elf_discarded_section (isec
))
8353 /* Discard unneeded references to discarded sections.
8354 Returns true if any section's size was changed. */
8355 /* This function assumes that the relocations are in sorted order,
8356 which is true for all known assemblers. */
8359 elf_bfd_discard_info (output_bfd
, info
)
8361 struct bfd_link_info
*info
;
8363 struct elf_reloc_cookie cookie
;
8364 asection
*stab
, *eh
, *ehdr
;
8365 Elf_Internal_Shdr
*symtab_hdr
;
8366 struct elf_backend_data
*bed
;
8368 boolean ret
= false;
8369 boolean strip
= info
->strip
== strip_all
|| info
->strip
== strip_debugger
;
8371 if (info
->relocateable
8372 || info
->traditional_format
8373 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
8374 || ! is_elf_hash_table (info
))
8378 if (elf_hash_table (info
)->dynobj
!= NULL
)
8379 ehdr
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
8382 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
8384 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
8387 bed
= get_elf_backend_data (abfd
);
8389 if ((abfd
->flags
& DYNAMIC
) != 0)
8395 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
8396 if (eh
&& (eh
->_raw_size
== 0
8397 || bfd_is_abs_section (eh
->output_section
)))
8404 stab
= bfd_get_section_by_name (abfd
, ".stab");
8405 if (stab
&& (stab
->_raw_size
== 0
8406 || bfd_is_abs_section (stab
->output_section
)))
8410 || elf_section_data(stab
)->sec_info_type
!= ELF_INFO_TYPE_STABS
)
8412 && (strip
|| ! bed
->elf_backend_discard_info
))
8415 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8417 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
8418 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
8419 if (cookie
.bad_symtab
)
8421 cookie
.locsymcount
=
8422 symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8423 cookie
.extsymoff
= 0;
8427 cookie
.locsymcount
= symtab_hdr
->sh_info
;
8428 cookie
.extsymoff
= symtab_hdr
->sh_info
;
8431 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8432 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
8434 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
8435 cookie
.locsymcount
, 0,
8437 if (cookie
.locsyms
== NULL
)
8443 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8444 (abfd
, stab
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
8445 info
->keep_memory
));
8448 cookie
.rel
= cookie
.rels
;
8450 cookie
.rels
+ stab
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8451 if (_bfd_discard_section_stabs (abfd
, stab
,
8452 elf_section_data (stab
)->sec_info
,
8453 elf_reloc_symbol_deleted_p
,
8456 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
8465 cookie
.relend
= NULL
;
8466 if (eh
->reloc_count
)
8467 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8468 (abfd
, eh
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
8469 info
->keep_memory
));
8472 cookie
.rel
= cookie
.rels
;
8474 cookie
.rels
+ eh
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8476 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
, ehdr
,
8477 elf_reloc_symbol_deleted_p
,
8480 if (cookie
.rels
&& elf_section_data (eh
)->relocs
!= cookie
.rels
)
8484 if (bed
->elf_backend_discard_info
)
8486 if (bed
->elf_backend_discard_info (abfd
, &cookie
, info
))
8490 if (cookie
.locsyms
!= NULL
8491 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
8493 if (! info
->keep_memory
)
8494 free (cookie
.locsyms
);
8496 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
8500 if (ehdr
&& _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
, ehdr
))
8506 elf_section_ignore_discarded_relocs (sec
)
8509 struct elf_backend_data
*bed
;
8511 switch (elf_section_data (sec
)->sec_info_type
)
8513 case ELF_INFO_TYPE_STABS
:
8514 case ELF_INFO_TYPE_EH_FRAME
:
8520 bed
= get_elf_backend_data (sec
->owner
);
8521 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
8522 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))