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, SEC, 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
, sec
, 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
;
930 /* If this symbol has a version, and it is the default version, we
931 create an indirect symbol from the default name to the fully
932 decorated name. This will cause external references which do not
933 specify a version to be bound to this version of the symbol. */
934 p
= strchr (name
, ELF_VER_CHR
);
935 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
940 /* We are overridden by an old defition. We need to check if we
941 need to create the indirect symbol from the default name. */
942 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, true,
944 BFD_ASSERT (hi
!= NULL
);
947 while (hi
->root
.type
== bfd_link_hash_indirect
948 || hi
->root
.type
== bfd_link_hash_warning
)
950 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
956 bed
= get_elf_backend_data (abfd
);
957 collect
= bed
->collect
;
958 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
961 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
962 if (shortname
== NULL
)
964 memcpy (shortname
, name
, shortlen
);
965 shortname
[shortlen
] = '\0';
967 /* We are going to create a new symbol. Merge it with any existing
968 symbol with this name. For the purposes of the merge, act as
969 though we were defining the symbol we just defined, although we
970 actually going to define an indirect symbol. */
971 type_change_ok
= false;
972 size_change_ok
= false;
973 if (! elf_merge_symbol (abfd
, info
, shortname
, sym
, sec
, value
,
974 &hi
, &override
, &type_change_ok
,
975 &size_change_ok
, dt_needed
))
980 if (! (_bfd_generic_link_add_one_symbol
981 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
982 (bfd_vma
) 0, name
, false, collect
,
983 (struct bfd_link_hash_entry
**) &hi
)))
988 /* In this case the symbol named SHORTNAME is overriding the
989 indirect symbol we want to add. We were planning on making
990 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
991 is the name without a version. NAME is the fully versioned
992 name, and it is the default version.
994 Overriding means that we already saw a definition for the
995 symbol SHORTNAME in a regular object, and it is overriding
996 the symbol defined in the dynamic object.
998 When this happens, we actually want to change NAME, the
999 symbol we just added, to refer to SHORTNAME. This will cause
1000 references to NAME in the shared object to become references
1001 to SHORTNAME in the regular object. This is what we expect
1002 when we override a function in a shared object: that the
1003 references in the shared object will be mapped to the
1004 definition in the regular object. */
1006 while (hi
->root
.type
== bfd_link_hash_indirect
1007 || hi
->root
.type
== bfd_link_hash_warning
)
1008 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1010 h
->root
.type
= bfd_link_hash_indirect
;
1011 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1012 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1014 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1015 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1016 if (hi
->elf_link_hash_flags
1017 & (ELF_LINK_HASH_REF_REGULAR
1018 | ELF_LINK_HASH_DEF_REGULAR
))
1020 if (! _bfd_elf_link_record_dynamic_symbol (info
, hi
))
1025 /* Now set HI to H, so that the following code will set the
1026 other fields correctly. */
1030 /* If there is a duplicate definition somewhere, then HI may not
1031 point to an indirect symbol. We will have reported an error to
1032 the user in that case. */
1034 if (hi
->root
.type
== bfd_link_hash_indirect
)
1036 struct elf_link_hash_entry
*ht
;
1038 /* If the symbol became indirect, then we assume that we have
1039 not seen a definition before. */
1040 BFD_ASSERT ((hi
->elf_link_hash_flags
1041 & (ELF_LINK_HASH_DEF_DYNAMIC
1042 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1044 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1045 (*bed
->elf_backend_copy_indirect_symbol
) (ht
, hi
);
1047 /* See if the new flags lead us to realize that the symbol must
1054 || ((hi
->elf_link_hash_flags
1055 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1060 if ((hi
->elf_link_hash_flags
1061 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1067 /* We also need to define an indirection from the nondefault version
1070 len
= strlen (name
);
1071 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1072 if (shortname
== NULL
)
1074 memcpy (shortname
, name
, shortlen
);
1075 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1077 /* Once again, merge with any existing symbol. */
1078 type_change_ok
= false;
1079 size_change_ok
= false;
1080 if (! elf_merge_symbol (abfd
, info
, shortname
, sym
, sec
, value
,
1081 &hi
, &override
, &type_change_ok
,
1082 &size_change_ok
, dt_needed
))
1087 /* Here SHORTNAME is a versioned name, so we don't expect to see
1088 the type of override we do in the case above. */
1089 (*_bfd_error_handler
)
1090 (_("%s: warning: unexpected redefinition of `%s'"),
1091 bfd_archive_filename (abfd
), shortname
);
1095 if (! (_bfd_generic_link_add_one_symbol
1096 (info
, abfd
, shortname
, BSF_INDIRECT
,
1097 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1098 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1101 /* If there is a duplicate definition somewhere, then HI may not
1102 point to an indirect symbol. We will have reported an error
1103 to the user in that case. */
1105 if (hi
->root
.type
== bfd_link_hash_indirect
)
1107 /* If the symbol became indirect, then we assume that we have
1108 not seen a definition before. */
1109 BFD_ASSERT ((hi
->elf_link_hash_flags
1110 & (ELF_LINK_HASH_DEF_DYNAMIC
1111 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1113 (*bed
->elf_backend_copy_indirect_symbol
) (h
, hi
);
1115 /* See if the new flags lead us to realize that the symbol
1122 || ((hi
->elf_link_hash_flags
1123 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1128 if ((hi
->elf_link_hash_flags
1129 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1139 /* Add symbols from an ELF object file to the linker hash table. */
1142 elf_link_add_object_symbols (abfd
, info
)
1144 struct bfd_link_info
*info
;
1146 boolean (*add_symbol_hook
) PARAMS ((bfd
*, struct bfd_link_info
*,
1147 const Elf_Internal_Sym
*,
1148 const char **, flagword
*,
1149 asection
**, bfd_vma
*));
1150 boolean (*check_relocs
) PARAMS ((bfd
*, struct bfd_link_info
*,
1151 asection
*, const Elf_Internal_Rela
*));
1153 Elf_Internal_Shdr
*hdr
;
1154 bfd_size_type symcount
;
1155 bfd_size_type extsymcount
;
1156 bfd_size_type extsymoff
;
1157 struct elf_link_hash_entry
**sym_hash
;
1159 Elf_External_Versym
*extversym
= NULL
;
1160 Elf_External_Versym
*ever
;
1161 struct elf_link_hash_entry
*weaks
;
1162 Elf_Internal_Sym
*isymbuf
= NULL
;
1163 Elf_Internal_Sym
*isym
;
1164 Elf_Internal_Sym
*isymend
;
1165 struct elf_backend_data
*bed
;
1167 struct elf_link_hash_table
* hash_table
;
1170 hash_table
= elf_hash_table (info
);
1172 bed
= get_elf_backend_data (abfd
);
1173 add_symbol_hook
= bed
->elf_add_symbol_hook
;
1174 collect
= bed
->collect
;
1176 if ((abfd
->flags
& DYNAMIC
) == 0)
1182 /* You can't use -r against a dynamic object. Also, there's no
1183 hope of using a dynamic object which does not exactly match
1184 the format of the output file. */
1185 if (info
->relocateable
|| info
->hash
->creator
!= abfd
->xvec
)
1187 bfd_set_error (bfd_error_invalid_operation
);
1192 /* As a GNU extension, any input sections which are named
1193 .gnu.warning.SYMBOL are treated as warning symbols for the given
1194 symbol. This differs from .gnu.warning sections, which generate
1195 warnings when they are included in an output file. */
1200 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
1204 name
= bfd_get_section_name (abfd
, s
);
1205 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1210 name
+= sizeof ".gnu.warning." - 1;
1212 /* If this is a shared object, then look up the symbol
1213 in the hash table. If it is there, and it is already
1214 been defined, then we will not be using the entry
1215 from this shared object, so we don't need to warn.
1216 FIXME: If we see the definition in a regular object
1217 later on, we will warn, but we shouldn't. The only
1218 fix is to keep track of what warnings we are supposed
1219 to emit, and then handle them all at the end of the
1221 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
1223 struct elf_link_hash_entry
*h
;
1225 h
= elf_link_hash_lookup (hash_table
, name
,
1226 false, false, true);
1228 /* FIXME: What about bfd_link_hash_common? */
1230 && (h
->root
.type
== bfd_link_hash_defined
1231 || h
->root
.type
== bfd_link_hash_defweak
))
1233 /* We don't want to issue this warning. Clobber
1234 the section size so that the warning does not
1235 get copied into the output file. */
1241 sz
= bfd_section_size (abfd
, s
);
1242 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
1246 if (! bfd_get_section_contents (abfd
, s
, msg
, (file_ptr
) 0, sz
))
1251 if (! (_bfd_generic_link_add_one_symbol
1252 (info
, abfd
, name
, BSF_WARNING
, s
, (bfd_vma
) 0, msg
,
1253 false, collect
, (struct bfd_link_hash_entry
**) NULL
)))
1256 if (! info
->relocateable
)
1258 /* Clobber the section size so that the warning does
1259 not get copied into the output file. */
1269 /* If we are creating a shared library, create all the dynamic
1270 sections immediately. We need to attach them to something,
1271 so we attach them to this BFD, provided it is the right
1272 format. FIXME: If there are no input BFD's of the same
1273 format as the output, we can't make a shared library. */
1275 && is_elf_hash_table (info
)
1276 && ! hash_table
->dynamic_sections_created
1277 && abfd
->xvec
== info
->hash
->creator
)
1279 if (! elf_link_create_dynamic_sections (abfd
, info
))
1283 else if (! is_elf_hash_table (info
))
1290 bfd_size_type oldsize
;
1291 bfd_size_type strindex
;
1293 /* Find the name to use in a DT_NEEDED entry that refers to this
1294 object. If the object has a DT_SONAME entry, we use it.
1295 Otherwise, if the generic linker stuck something in
1296 elf_dt_name, we use that. Otherwise, we just use the file
1297 name. If the generic linker put a null string into
1298 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1299 there is a DT_SONAME entry. */
1301 name
= bfd_get_filename (abfd
);
1302 if (elf_dt_name (abfd
) != NULL
)
1304 name
= elf_dt_name (abfd
);
1307 if (elf_dt_soname (abfd
) != NULL
)
1313 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1316 Elf_External_Dyn
*dynbuf
= NULL
;
1317 Elf_External_Dyn
*extdyn
;
1318 Elf_External_Dyn
*extdynend
;
1320 unsigned long shlink
;
1324 dynbuf
= (Elf_External_Dyn
*) bfd_malloc (s
->_raw_size
);
1328 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
1329 (file_ptr
) 0, s
->_raw_size
))
1330 goto error_free_dyn
;
1332 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1334 goto error_free_dyn
;
1335 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1338 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
1341 for (; extdyn
< extdynend
; extdyn
++)
1343 Elf_Internal_Dyn dyn
;
1345 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
1346 if (dyn
.d_tag
== DT_SONAME
)
1348 unsigned int tagv
= dyn
.d_un
.d_val
;
1349 name
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1351 goto error_free_dyn
;
1353 if (dyn
.d_tag
== DT_NEEDED
)
1355 struct bfd_link_needed_list
*n
, **pn
;
1357 unsigned int tagv
= dyn
.d_un
.d_val
;
1359 amt
= sizeof (struct bfd_link_needed_list
);
1360 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1361 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1362 if (n
== NULL
|| fnm
== NULL
)
1363 goto error_free_dyn
;
1364 amt
= strlen (fnm
) + 1;
1365 anm
= bfd_alloc (abfd
, amt
);
1367 goto error_free_dyn
;
1368 memcpy (anm
, fnm
, (size_t) amt
);
1372 for (pn
= & hash_table
->needed
;
1378 if (dyn
.d_tag
== DT_RUNPATH
)
1380 struct bfd_link_needed_list
*n
, **pn
;
1382 unsigned int tagv
= dyn
.d_un
.d_val
;
1384 /* When we see DT_RPATH before DT_RUNPATH, we have
1385 to clear runpath. Do _NOT_ bfd_release, as that
1386 frees all more recently bfd_alloc'd blocks as
1388 if (rpath
&& hash_table
->runpath
)
1389 hash_table
->runpath
= NULL
;
1391 amt
= sizeof (struct bfd_link_needed_list
);
1392 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1393 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1394 if (n
== NULL
|| fnm
== NULL
)
1395 goto error_free_dyn
;
1396 amt
= strlen (fnm
) + 1;
1397 anm
= bfd_alloc (abfd
, amt
);
1399 goto error_free_dyn
;
1400 memcpy (anm
, fnm
, (size_t) amt
);
1404 for (pn
= & hash_table
->runpath
;
1412 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1413 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
1415 struct bfd_link_needed_list
*n
, **pn
;
1417 unsigned int tagv
= dyn
.d_un
.d_val
;
1419 amt
= sizeof (struct bfd_link_needed_list
);
1420 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1421 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1422 if (n
== NULL
|| fnm
== NULL
)
1423 goto error_free_dyn
;
1424 amt
= strlen (fnm
) + 1;
1425 anm
= bfd_alloc (abfd
, amt
);
1432 memcpy (anm
, fnm
, (size_t) amt
);
1436 for (pn
= & hash_table
->runpath
;
1448 /* We do not want to include any of the sections in a dynamic
1449 object in the output file. We hack by simply clobbering the
1450 list of sections in the BFD. This could be handled more
1451 cleanly by, say, a new section flag; the existing
1452 SEC_NEVER_LOAD flag is not the one we want, because that one
1453 still implies that the section takes up space in the output
1455 bfd_section_list_clear (abfd
);
1457 /* If this is the first dynamic object found in the link, create
1458 the special sections required for dynamic linking. */
1459 if (! hash_table
->dynamic_sections_created
)
1460 if (! elf_link_create_dynamic_sections (abfd
, info
))
1465 /* Add a DT_NEEDED entry for this dynamic object. */
1466 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
1467 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, name
, false);
1468 if (strindex
== (bfd_size_type
) -1)
1471 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
1474 Elf_External_Dyn
*dyncon
, *dynconend
;
1476 /* The hash table size did not change, which means that
1477 the dynamic object name was already entered. If we
1478 have already included this dynamic object in the
1479 link, just ignore it. There is no reason to include
1480 a particular dynamic object more than once. */
1481 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
1482 BFD_ASSERT (sdyn
!= NULL
);
1484 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1485 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1487 for (; dyncon
< dynconend
; dyncon
++)
1489 Elf_Internal_Dyn dyn
;
1491 elf_swap_dyn_in (hash_table
->dynobj
, dyncon
, & dyn
);
1492 if (dyn
.d_tag
== DT_NEEDED
1493 && dyn
.d_un
.d_val
== strindex
)
1495 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
1501 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
1505 /* Save the SONAME, if there is one, because sometimes the
1506 linker emulation code will need to know it. */
1508 name
= basename (bfd_get_filename (abfd
));
1509 elf_dt_name (abfd
) = name
;
1512 /* If this is a dynamic object, we always link against the .dynsym
1513 symbol table, not the .symtab symbol table. The dynamic linker
1514 will only see the .dynsym symbol table, so there is no reason to
1515 look at .symtab for a dynamic object. */
1517 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
1518 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1520 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1522 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
1524 /* The sh_info field of the symtab header tells us where the
1525 external symbols start. We don't care about the local symbols at
1527 if (elf_bad_symtab (abfd
))
1529 extsymcount
= symcount
;
1534 extsymcount
= symcount
- hdr
->sh_info
;
1535 extsymoff
= hdr
->sh_info
;
1539 if (extsymcount
!= 0)
1541 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
1543 if (isymbuf
== NULL
)
1546 /* We store a pointer to the hash table entry for each external
1548 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
1549 sym_hash
= (struct elf_link_hash_entry
**) bfd_alloc (abfd
, amt
);
1550 if (sym_hash
== NULL
)
1551 goto error_free_sym
;
1552 elf_sym_hashes (abfd
) = sym_hash
;
1557 /* Read in any version definitions. */
1558 if (! _bfd_elf_slurp_version_tables (abfd
))
1559 goto error_free_sym
;
1561 /* Read in the symbol versions, but don't bother to convert them
1562 to internal format. */
1563 if (elf_dynversym (abfd
) != 0)
1565 Elf_Internal_Shdr
*versymhdr
;
1567 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
1568 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
1569 if (extversym
== NULL
)
1570 goto error_free_sym
;
1571 amt
= versymhdr
->sh_size
;
1572 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
1573 || bfd_bread ((PTR
) extversym
, amt
, abfd
) != amt
)
1574 goto error_free_vers
;
1580 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
1581 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
1583 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
1590 struct elf_link_hash_entry
*h
;
1592 boolean size_change_ok
, type_change_ok
;
1593 boolean new_weakdef
;
1594 unsigned int old_alignment
;
1599 flags
= BSF_NO_FLAGS
;
1601 value
= isym
->st_value
;
1604 bind
= ELF_ST_BIND (isym
->st_info
);
1605 if (bind
== STB_LOCAL
)
1607 /* This should be impossible, since ELF requires that all
1608 global symbols follow all local symbols, and that sh_info
1609 point to the first global symbol. Unfortunatealy, Irix 5
1613 else if (bind
== STB_GLOBAL
)
1615 if (isym
->st_shndx
!= SHN_UNDEF
1616 && isym
->st_shndx
!= SHN_COMMON
)
1619 else if (bind
== STB_WEAK
)
1623 /* Leave it up to the processor backend. */
1626 if (isym
->st_shndx
== SHN_UNDEF
)
1627 sec
= bfd_und_section_ptr
;
1628 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
1630 sec
= section_from_elf_index (abfd
, isym
->st_shndx
);
1632 sec
= bfd_abs_section_ptr
;
1633 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
1636 else if (isym
->st_shndx
== SHN_ABS
)
1637 sec
= bfd_abs_section_ptr
;
1638 else if (isym
->st_shndx
== SHN_COMMON
)
1640 sec
= bfd_com_section_ptr
;
1641 /* What ELF calls the size we call the value. What ELF
1642 calls the value we call the alignment. */
1643 value
= isym
->st_size
;
1647 /* Leave it up to the processor backend. */
1650 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
1652 if (name
== (const char *) NULL
)
1653 goto error_free_vers
;
1655 if (isym
->st_shndx
== SHN_COMMON
1656 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
1658 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
1662 tcomm
= bfd_make_section (abfd
, ".tcommon");
1664 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
1666 | SEC_LINKER_CREATED
1667 | SEC_THREAD_LOCAL
)))
1668 goto error_free_vers
;
1672 else if (add_symbol_hook
)
1674 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
1676 goto error_free_vers
;
1678 /* The hook function sets the name to NULL if this symbol
1679 should be skipped for some reason. */
1680 if (name
== (const char *) NULL
)
1684 /* Sanity check that all possibilities were handled. */
1685 if (sec
== (asection
*) NULL
)
1687 bfd_set_error (bfd_error_bad_value
);
1688 goto error_free_vers
;
1691 if (bfd_is_und_section (sec
)
1692 || bfd_is_com_section (sec
))
1697 size_change_ok
= false;
1698 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
1700 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1702 Elf_Internal_Versym iver
;
1703 unsigned int vernum
= 0;
1707 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
1708 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
1710 /* If this is a hidden symbol, or if it is not version
1711 1, we append the version name to the symbol name.
1712 However, we do not modify a non-hidden absolute
1713 symbol, because it might be the version symbol
1714 itself. FIXME: What if it isn't? */
1715 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
1716 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
1719 size_t namelen
, verlen
, newlen
;
1722 if (isym
->st_shndx
!= SHN_UNDEF
)
1724 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
1726 (*_bfd_error_handler
)
1727 (_("%s: %s: invalid version %u (max %d)"),
1728 bfd_archive_filename (abfd
), name
, vernum
,
1729 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1730 bfd_set_error (bfd_error_bad_value
);
1731 goto error_free_vers
;
1733 else if (vernum
> 1)
1735 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1741 /* We cannot simply test for the number of
1742 entries in the VERNEED section since the
1743 numbers for the needed versions do not start
1745 Elf_Internal_Verneed
*t
;
1748 for (t
= elf_tdata (abfd
)->verref
;
1752 Elf_Internal_Vernaux
*a
;
1754 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1756 if (a
->vna_other
== vernum
)
1758 verstr
= a
->vna_nodename
;
1767 (*_bfd_error_handler
)
1768 (_("%s: %s: invalid needed version %d"),
1769 bfd_archive_filename (abfd
), name
, vernum
);
1770 bfd_set_error (bfd_error_bad_value
);
1771 goto error_free_vers
;
1775 namelen
= strlen (name
);
1776 verlen
= strlen (verstr
);
1777 newlen
= namelen
+ verlen
+ 2;
1778 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1779 && isym
->st_shndx
!= SHN_UNDEF
)
1782 newname
= (char *) bfd_alloc (abfd
, (bfd_size_type
) newlen
);
1783 if (newname
== NULL
)
1784 goto error_free_vers
;
1785 memcpy (newname
, name
, namelen
);
1786 p
= newname
+ namelen
;
1788 /* If this is a defined non-hidden version symbol,
1789 we add another @ to the name. This indicates the
1790 default version of the symbol. */
1791 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1792 && isym
->st_shndx
!= SHN_UNDEF
)
1794 memcpy (p
, verstr
, verlen
+ 1);
1800 if (! elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
1801 sym_hash
, &override
, &type_change_ok
,
1802 &size_change_ok
, dt_needed
))
1803 goto error_free_vers
;
1809 while (h
->root
.type
== bfd_link_hash_indirect
1810 || h
->root
.type
== bfd_link_hash_warning
)
1811 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1813 /* Remember the old alignment if this is a common symbol, so
1814 that we don't reduce the alignment later on. We can't
1815 check later, because _bfd_generic_link_add_one_symbol
1816 will set a default for the alignment which we want to
1818 if (h
->root
.type
== bfd_link_hash_common
)
1819 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1821 if (elf_tdata (abfd
)->verdef
!= NULL
1825 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1828 if (! (_bfd_generic_link_add_one_symbol
1829 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
1830 false, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
1831 goto error_free_vers
;
1834 while (h
->root
.type
== bfd_link_hash_indirect
1835 || h
->root
.type
== bfd_link_hash_warning
)
1836 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1839 new_weakdef
= false;
1842 && (flags
& BSF_WEAK
) != 0
1843 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
1844 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1845 && h
->weakdef
== NULL
)
1847 /* Keep a list of all weak defined non function symbols from
1848 a dynamic object, using the weakdef field. Later in this
1849 function we will set the weakdef field to the correct
1850 value. We only put non-function symbols from dynamic
1851 objects on this list, because that happens to be the only
1852 time we need to know the normal symbol corresponding to a
1853 weak symbol, and the information is time consuming to
1854 figure out. If the weakdef field is not already NULL,
1855 then this symbol was already defined by some previous
1856 dynamic object, and we will be using that previous
1857 definition anyhow. */
1864 /* Set the alignment of a common symbol. */
1865 if (isym
->st_shndx
== SHN_COMMON
1866 && h
->root
.type
== bfd_link_hash_common
)
1870 align
= bfd_log2 (isym
->st_value
);
1871 if (align
> old_alignment
1872 /* Permit an alignment power of zero if an alignment of one
1873 is specified and no other alignments have been specified. */
1874 || (isym
->st_value
== 1 && old_alignment
== 0))
1875 h
->root
.u
.c
.p
->alignment_power
= align
;
1878 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1884 /* Remember the symbol size and type. */
1885 if (isym
->st_size
!= 0
1886 && (definition
|| h
->size
== 0))
1888 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
1889 (*_bfd_error_handler
)
1890 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1891 name
, (unsigned long) h
->size
,
1892 (unsigned long) isym
->st_size
, bfd_archive_filename (abfd
));
1894 h
->size
= isym
->st_size
;
1897 /* If this is a common symbol, then we always want H->SIZE
1898 to be the size of the common symbol. The code just above
1899 won't fix the size if a common symbol becomes larger. We
1900 don't warn about a size change here, because that is
1901 covered by --warn-common. */
1902 if (h
->root
.type
== bfd_link_hash_common
)
1903 h
->size
= h
->root
.u
.c
.size
;
1905 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
1906 && (definition
|| h
->type
== STT_NOTYPE
))
1908 if (h
->type
!= STT_NOTYPE
1909 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
1910 && ! type_change_ok
)
1911 (*_bfd_error_handler
)
1912 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1913 name
, h
->type
, ELF_ST_TYPE (isym
->st_info
),
1914 bfd_archive_filename (abfd
));
1916 h
->type
= ELF_ST_TYPE (isym
->st_info
);
1919 /* If st_other has a processor-specific meaning, specific code
1920 might be needed here. */
1921 if (isym
->st_other
!= 0)
1923 /* Combine visibilities, using the most constraining one. */
1924 unsigned char hvis
= ELF_ST_VISIBILITY (h
->other
);
1925 unsigned char symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
1927 if (symvis
&& (hvis
> symvis
|| hvis
== 0))
1928 h
->other
= isym
->st_other
;
1930 /* If neither has visibility, use the st_other of the
1931 definition. This is an arbitrary choice, since the
1932 other bits have no general meaning. */
1933 if (!symvis
&& !hvis
1934 && (definition
|| h
->other
== 0))
1935 h
->other
= isym
->st_other
;
1938 /* Set a flag in the hash table entry indicating the type of
1939 reference or definition we just found. Keep a count of
1940 the number of dynamic symbols we find. A dynamic symbol
1941 is one which is referenced or defined by both a regular
1942 object and a shared object. */
1943 old_flags
= h
->elf_link_hash_flags
;
1949 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
1950 if (bind
!= STB_WEAK
)
1951 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
1954 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
1956 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1957 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
1963 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
1965 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
1966 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
1967 | ELF_LINK_HASH_REF_REGULAR
)) != 0
1968 || (h
->weakdef
!= NULL
1970 && h
->weakdef
->dynindx
!= -1))
1974 h
->elf_link_hash_flags
|= new_flag
;
1976 /* Check to see if we need to add an indirect symbol for
1977 the default name. */
1978 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
1979 if (! elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
1980 &sec
, &value
, &dynsym
,
1981 override
, dt_needed
))
1982 goto error_free_vers
;
1984 if (dynsym
&& h
->dynindx
== -1)
1986 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1987 goto error_free_vers
;
1988 if (h
->weakdef
!= NULL
1990 && h
->weakdef
->dynindx
== -1)
1992 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
1993 goto error_free_vers
;
1996 else if (dynsym
&& h
->dynindx
!= -1)
1997 /* If the symbol already has a dynamic index, but
1998 visibility says it should not be visible, turn it into
2000 switch (ELF_ST_VISIBILITY (h
->other
))
2004 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2008 if (dt_needed
&& definition
2009 && (h
->elf_link_hash_flags
2010 & ELF_LINK_HASH_REF_REGULAR
) != 0)
2012 bfd_size_type oldsize
;
2013 bfd_size_type strindex
;
2015 if (! is_elf_hash_table (info
))
2016 goto error_free_vers
;
2018 /* The symbol from a DT_NEEDED object is referenced from
2019 the regular object to create a dynamic executable. We
2020 have to make sure there is a DT_NEEDED entry for it. */
2023 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2024 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
,
2025 elf_dt_soname (abfd
), false);
2026 if (strindex
== (bfd_size_type
) -1)
2027 goto error_free_vers
;
2029 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2032 Elf_External_Dyn
*dyncon
, *dynconend
;
2034 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
,
2036 BFD_ASSERT (sdyn
!= NULL
);
2038 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
2039 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
2041 for (; dyncon
< dynconend
; dyncon
++)
2043 Elf_Internal_Dyn dyn
;
2045 elf_swap_dyn_in (hash_table
->dynobj
,
2047 BFD_ASSERT (dyn
.d_tag
!= DT_NEEDED
||
2048 dyn
.d_un
.d_val
!= strindex
);
2052 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
2053 goto error_free_vers
;
2058 if (extversym
!= NULL
)
2064 if (isymbuf
!= NULL
)
2068 /* Now set the weakdefs field correctly for all the weak defined
2069 symbols we found. The only way to do this is to search all the
2070 symbols. Since we only need the information for non functions in
2071 dynamic objects, that's the only time we actually put anything on
2072 the list WEAKS. We need this information so that if a regular
2073 object refers to a symbol defined weakly in a dynamic object, the
2074 real symbol in the dynamic object is also put in the dynamic
2075 symbols; we also must arrange for both symbols to point to the
2076 same memory location. We could handle the general case of symbol
2077 aliasing, but a general symbol alias can only be generated in
2078 assembler code, handling it correctly would be very time
2079 consuming, and other ELF linkers don't handle general aliasing
2081 while (weaks
!= NULL
)
2083 struct elf_link_hash_entry
*hlook
;
2086 struct elf_link_hash_entry
**hpp
;
2087 struct elf_link_hash_entry
**hppend
;
2090 weaks
= hlook
->weakdef
;
2091 hlook
->weakdef
= NULL
;
2093 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
2094 || hlook
->root
.type
== bfd_link_hash_defweak
2095 || hlook
->root
.type
== bfd_link_hash_common
2096 || hlook
->root
.type
== bfd_link_hash_indirect
);
2097 slook
= hlook
->root
.u
.def
.section
;
2098 vlook
= hlook
->root
.u
.def
.value
;
2100 hpp
= elf_sym_hashes (abfd
);
2101 hppend
= hpp
+ extsymcount
;
2102 for (; hpp
< hppend
; hpp
++)
2104 struct elf_link_hash_entry
*h
;
2107 if (h
!= NULL
&& h
!= hlook
2108 && h
->root
.type
== bfd_link_hash_defined
2109 && h
->root
.u
.def
.section
== slook
2110 && h
->root
.u
.def
.value
== vlook
)
2114 /* If the weak definition is in the list of dynamic
2115 symbols, make sure the real definition is put there
2117 if (hlook
->dynindx
!= -1
2118 && h
->dynindx
== -1)
2120 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2124 /* If the real definition is in the list of dynamic
2125 symbols, make sure the weak definition is put there
2126 as well. If we don't do this, then the dynamic
2127 loader might not merge the entries for the real
2128 definition and the weak definition. */
2129 if (h
->dynindx
!= -1
2130 && hlook
->dynindx
== -1)
2132 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
2140 /* If this object is the same format as the output object, and it is
2141 not a shared library, then let the backend look through the
2144 This is required to build global offset table entries and to
2145 arrange for dynamic relocs. It is not required for the
2146 particular common case of linking non PIC code, even when linking
2147 against shared libraries, but unfortunately there is no way of
2148 knowing whether an object file has been compiled PIC or not.
2149 Looking through the relocs is not particularly time consuming.
2150 The problem is that we must either (1) keep the relocs in memory,
2151 which causes the linker to require additional runtime memory or
2152 (2) read the relocs twice from the input file, which wastes time.
2153 This would be a good case for using mmap.
2155 I have no idea how to handle linking PIC code into a file of a
2156 different format. It probably can't be done. */
2157 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
2159 && abfd
->xvec
== info
->hash
->creator
2160 && check_relocs
!= NULL
)
2164 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2166 Elf_Internal_Rela
*internal_relocs
;
2169 if ((o
->flags
& SEC_RELOC
) == 0
2170 || o
->reloc_count
== 0
2171 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
2172 && (o
->flags
& SEC_DEBUGGING
) != 0)
2173 || bfd_is_abs_section (o
->output_section
))
2176 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
2177 (abfd
, o
, (PTR
) NULL
,
2178 (Elf_Internal_Rela
*) NULL
,
2179 info
->keep_memory
));
2180 if (internal_relocs
== NULL
)
2183 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
2185 if (elf_section_data (o
)->relocs
!= internal_relocs
)
2186 free (internal_relocs
);
2193 /* If this is a non-traditional, non-relocateable link, try to
2194 optimize the handling of the .stab/.stabstr sections. */
2196 && ! info
->relocateable
2197 && ! info
->traditional_format
2198 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
2199 && is_elf_hash_table (info
)
2200 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
2202 asection
*stab
, *stabstr
;
2204 stab
= bfd_get_section_by_name (abfd
, ".stab");
2206 && (stab
->flags
& SEC_MERGE
) == 0
2207 && !bfd_is_abs_section (stab
->output_section
))
2209 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
2211 if (stabstr
!= NULL
)
2213 struct bfd_elf_section_data
*secdata
;
2215 secdata
= elf_section_data (stab
);
2216 if (! _bfd_link_section_stabs (abfd
,
2217 & hash_table
->stab_info
,
2219 &secdata
->sec_info
))
2221 if (secdata
->sec_info
)
2222 secdata
->sec_info_type
= ELF_INFO_TYPE_STABS
;
2227 if (! info
->relocateable
&& ! dynamic
2228 && is_elf_hash_table (info
))
2232 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2233 if ((s
->flags
& SEC_MERGE
) != 0
2234 && !bfd_is_abs_section (s
->output_section
))
2236 struct bfd_elf_section_data
*secdata
;
2238 secdata
= elf_section_data (s
);
2239 if (! _bfd_merge_section (abfd
,
2240 & hash_table
->merge_info
,
2241 s
, &secdata
->sec_info
))
2243 else if (secdata
->sec_info
)
2244 secdata
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
2248 if (is_elf_hash_table (info
))
2250 /* Add this bfd to the loaded list. */
2251 struct elf_link_loaded_list
*n
;
2253 n
= ((struct elf_link_loaded_list
*)
2254 bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
)));
2258 n
->next
= hash_table
->loaded
;
2259 hash_table
->loaded
= n
;
2265 if (extversym
!= NULL
)
2268 if (isymbuf
!= NULL
)
2274 /* Create some sections which will be filled in with dynamic linking
2275 information. ABFD is an input file which requires dynamic sections
2276 to be created. The dynamic sections take up virtual memory space
2277 when the final executable is run, so we need to create them before
2278 addresses are assigned to the output sections. We work out the
2279 actual contents and size of these sections later. */
2282 elf_link_create_dynamic_sections (abfd
, info
)
2284 struct bfd_link_info
*info
;
2287 register asection
*s
;
2288 struct elf_link_hash_entry
*h
;
2289 struct elf_backend_data
*bed
;
2291 if (! is_elf_hash_table (info
))
2294 if (elf_hash_table (info
)->dynamic_sections_created
)
2297 /* Make sure that all dynamic sections use the same input BFD. */
2298 if (elf_hash_table (info
)->dynobj
== NULL
)
2299 elf_hash_table (info
)->dynobj
= abfd
;
2301 abfd
= elf_hash_table (info
)->dynobj
;
2303 /* Note that we set the SEC_IN_MEMORY flag for all of these
2305 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
2306 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
2308 /* A dynamically linked executable has a .interp section, but a
2309 shared library does not. */
2312 s
= bfd_make_section (abfd
, ".interp");
2314 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2318 if (! info
->traditional_format
2319 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
2321 s
= bfd_make_section (abfd
, ".eh_frame_hdr");
2323 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2324 || ! bfd_set_section_alignment (abfd
, s
, 2))
2328 /* Create sections to hold version informations. These are removed
2329 if they are not needed. */
2330 s
= bfd_make_section (abfd
, ".gnu.version_d");
2332 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2333 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2336 s
= bfd_make_section (abfd
, ".gnu.version");
2338 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2339 || ! bfd_set_section_alignment (abfd
, s
, 1))
2342 s
= bfd_make_section (abfd
, ".gnu.version_r");
2344 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2345 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2348 s
= bfd_make_section (abfd
, ".dynsym");
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
, ".dynstr");
2356 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2359 /* Create a strtab to hold the dynamic symbol names. */
2360 if (elf_hash_table (info
)->dynstr
== NULL
)
2362 elf_hash_table (info
)->dynstr
= _bfd_elf_strtab_init ();
2363 if (elf_hash_table (info
)->dynstr
== NULL
)
2367 s
= bfd_make_section (abfd
, ".dynamic");
2369 || ! bfd_set_section_flags (abfd
, s
, flags
)
2370 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2373 /* The special symbol _DYNAMIC is always set to the start of the
2374 .dynamic section. This call occurs before we have processed the
2375 symbols for any dynamic object, so we don't have to worry about
2376 overriding a dynamic definition. We could set _DYNAMIC in a
2377 linker script, but we only want to define it if we are, in fact,
2378 creating a .dynamic section. We don't want to define it if there
2379 is no .dynamic section, since on some ELF platforms the start up
2380 code examines it to decide how to initialize the process. */
2382 if (! (_bfd_generic_link_add_one_symbol
2383 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
2384 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
2385 (struct bfd_link_hash_entry
**) &h
)))
2387 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2388 h
->type
= STT_OBJECT
;
2391 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2394 bed
= get_elf_backend_data (abfd
);
2396 s
= bfd_make_section (abfd
, ".hash");
2398 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2399 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2401 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
2403 /* Let the backend create the rest of the sections. This lets the
2404 backend set the right flags. The backend will normally create
2405 the .got and .plt sections. */
2406 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
2409 elf_hash_table (info
)->dynamic_sections_created
= true;
2414 /* Add an entry to the .dynamic table. */
2417 elf_add_dynamic_entry (info
, tag
, val
)
2418 struct bfd_link_info
*info
;
2422 Elf_Internal_Dyn dyn
;
2425 bfd_size_type newsize
;
2426 bfd_byte
*newcontents
;
2428 if (! is_elf_hash_table (info
))
2431 dynobj
= elf_hash_table (info
)->dynobj
;
2433 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
2434 BFD_ASSERT (s
!= NULL
);
2436 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
2437 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
2438 if (newcontents
== NULL
)
2442 dyn
.d_un
.d_val
= val
;
2443 elf_swap_dyn_out (dynobj
, &dyn
,
2444 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
2446 s
->_raw_size
= newsize
;
2447 s
->contents
= newcontents
;
2452 /* Read and swap the relocs from the section indicated by SHDR. This
2453 may be either a REL or a RELA section. The relocations are
2454 translated into RELA relocations and stored in INTERNAL_RELOCS,
2455 which should have already been allocated to contain enough space.
2456 The EXTERNAL_RELOCS are a buffer where the external form of the
2457 relocations should be stored.
2459 Returns false if something goes wrong. */
2462 elf_link_read_relocs_from_section (abfd
, shdr
, external_relocs
,
2465 Elf_Internal_Shdr
*shdr
;
2466 PTR external_relocs
;
2467 Elf_Internal_Rela
*internal_relocs
;
2469 struct elf_backend_data
*bed
;
2472 /* If there aren't any relocations, that's OK. */
2476 /* Position ourselves at the start of the section. */
2477 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2480 /* Read the relocations. */
2481 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2484 bed
= get_elf_backend_data (abfd
);
2486 /* Convert the external relocations to the internal format. */
2487 if (shdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2489 Elf_External_Rel
*erel
;
2490 Elf_External_Rel
*erelend
;
2491 Elf_Internal_Rela
*irela
;
2492 Elf_Internal_Rel
*irel
;
2494 erel
= (Elf_External_Rel
*) external_relocs
;
2495 erelend
= erel
+ NUM_SHDR_ENTRIES (shdr
);
2496 irela
= internal_relocs
;
2497 amt
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
2498 irel
= bfd_alloc (abfd
, amt
);
2499 for (; erel
< erelend
; erel
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2503 if (bed
->s
->swap_reloc_in
)
2504 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
2506 elf_swap_reloc_in (abfd
, erel
, irel
);
2508 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; ++i
)
2510 irela
[i
].r_offset
= irel
[i
].r_offset
;
2511 irela
[i
].r_info
= irel
[i
].r_info
;
2512 irela
[i
].r_addend
= 0;
2518 Elf_External_Rela
*erela
;
2519 Elf_External_Rela
*erelaend
;
2520 Elf_Internal_Rela
*irela
;
2522 BFD_ASSERT (shdr
->sh_entsize
== sizeof (Elf_External_Rela
));
2524 erela
= (Elf_External_Rela
*) external_relocs
;
2525 erelaend
= erela
+ NUM_SHDR_ENTRIES (shdr
);
2526 irela
= internal_relocs
;
2527 for (; erela
< erelaend
; erela
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2529 if (bed
->s
->swap_reloca_in
)
2530 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
2532 elf_swap_reloca_in (abfd
, erela
, irela
);
2539 /* Read and swap the relocs for a section O. They may have been
2540 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2541 not NULL, they are used as buffers to read into. They are known to
2542 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2543 the return value is allocated using either malloc or bfd_alloc,
2544 according to the KEEP_MEMORY argument. If O has two relocation
2545 sections (both REL and RELA relocations), then the REL_HDR
2546 relocations will appear first in INTERNAL_RELOCS, followed by the
2547 REL_HDR2 relocations. */
2550 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
2554 PTR external_relocs
;
2555 Elf_Internal_Rela
*internal_relocs
;
2556 boolean keep_memory
;
2558 Elf_Internal_Shdr
*rel_hdr
;
2560 Elf_Internal_Rela
*alloc2
= NULL
;
2561 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2563 if (elf_section_data (o
)->relocs
!= NULL
)
2564 return elf_section_data (o
)->relocs
;
2566 if (o
->reloc_count
== 0)
2569 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2571 if (internal_relocs
== NULL
)
2575 size
= o
->reloc_count
;
2576 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2578 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2580 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2581 if (internal_relocs
== NULL
)
2585 if (external_relocs
== NULL
)
2587 bfd_size_type size
= rel_hdr
->sh_size
;
2589 if (elf_section_data (o
)->rel_hdr2
)
2590 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2591 alloc1
= (PTR
) bfd_malloc (size
);
2594 external_relocs
= alloc1
;
2597 if (!elf_link_read_relocs_from_section (abfd
, rel_hdr
,
2601 if (!elf_link_read_relocs_from_section
2603 elf_section_data (o
)->rel_hdr2
,
2604 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2605 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2606 * bed
->s
->int_rels_per_ext_rel
)))
2609 /* Cache the results for next time, if we can. */
2611 elf_section_data (o
)->relocs
= internal_relocs
;
2616 /* Don't free alloc2, since if it was allocated we are passing it
2617 back (under the name of internal_relocs). */
2619 return internal_relocs
;
2629 /* Record an assignment to a symbol made by a linker script. We need
2630 this in case some dynamic object refers to this symbol. */
2633 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
2634 bfd
*output_bfd ATTRIBUTE_UNUSED
;
2635 struct bfd_link_info
*info
;
2639 struct elf_link_hash_entry
*h
;
2641 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2644 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, true, false);
2648 if (h
->root
.type
== bfd_link_hash_new
)
2649 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_ELF
;
2651 /* If this symbol is being provided by the linker script, and it is
2652 currently defined by a dynamic object, but not by a regular
2653 object, then mark it as undefined so that the generic linker will
2654 force the correct value. */
2656 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2657 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2658 h
->root
.type
= bfd_link_hash_undefined
;
2660 /* If this symbol is not being provided by the linker script, and it is
2661 currently defined by a dynamic object, but not by a regular object,
2662 then clear out any version information because the symbol will not be
2663 associated with the dynamic object any more. */
2665 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2666 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2667 h
->verinfo
.verdef
= NULL
;
2669 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2671 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2672 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
2674 && h
->dynindx
== -1)
2676 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2679 /* If this is a weak defined symbol, and we know a corresponding
2680 real symbol from the same dynamic object, make sure the real
2681 symbol is also made into a dynamic symbol. */
2682 if (h
->weakdef
!= NULL
2683 && h
->weakdef
->dynindx
== -1)
2685 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2693 /* This structure is used to pass information to
2694 elf_link_assign_sym_version. */
2696 struct elf_assign_sym_version_info
2700 /* General link information. */
2701 struct bfd_link_info
*info
;
2703 struct bfd_elf_version_tree
*verdefs
;
2704 /* Whether we had a failure. */
2708 /* This structure is used to pass information to
2709 elf_link_find_version_dependencies. */
2711 struct elf_find_verdep_info
2715 /* General link information. */
2716 struct bfd_link_info
*info
;
2717 /* The number of dependencies. */
2719 /* Whether we had a failure. */
2723 /* Array used to determine the number of hash table buckets to use
2724 based on the number of symbols there are. If there are fewer than
2725 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2726 fewer than 37 we use 17 buckets, and so forth. We never use more
2727 than 32771 buckets. */
2729 static const size_t elf_buckets
[] =
2731 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2735 /* Compute bucket count for hashing table. We do not use a static set
2736 of possible tables sizes anymore. Instead we determine for all
2737 possible reasonable sizes of the table the outcome (i.e., the
2738 number of collisions etc) and choose the best solution. The
2739 weighting functions are not too simple to allow the table to grow
2740 without bounds. Instead one of the weighting factors is the size.
2741 Therefore the result is always a good payoff between few collisions
2742 (= short chain lengths) and table size. */
2744 compute_bucket_count (info
)
2745 struct bfd_link_info
*info
;
2747 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2748 size_t best_size
= 0;
2749 unsigned long int *hashcodes
;
2750 unsigned long int *hashcodesp
;
2751 unsigned long int i
;
2754 /* Compute the hash values for all exported symbols. At the same
2755 time store the values in an array so that we could use them for
2758 amt
*= sizeof (unsigned long int);
2759 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
2760 if (hashcodes
== NULL
)
2762 hashcodesp
= hashcodes
;
2764 /* Put all hash values in HASHCODES. */
2765 elf_link_hash_traverse (elf_hash_table (info
),
2766 elf_collect_hash_codes
, &hashcodesp
);
2768 /* We have a problem here. The following code to optimize the table
2769 size requires an integer type with more the 32 bits. If
2770 BFD_HOST_U_64_BIT is set we know about such a type. */
2771 #ifdef BFD_HOST_U_64_BIT
2774 unsigned long int nsyms
= hashcodesp
- hashcodes
;
2777 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
2778 unsigned long int *counts
;
2780 /* Possible optimization parameters: if we have NSYMS symbols we say
2781 that the hashing table must at least have NSYMS/4 and at most
2783 minsize
= nsyms
/ 4;
2786 best_size
= maxsize
= nsyms
* 2;
2788 /* Create array where we count the collisions in. We must use bfd_malloc
2789 since the size could be large. */
2791 amt
*= sizeof (unsigned long int);
2792 counts
= (unsigned long int *) bfd_malloc (amt
);
2799 /* Compute the "optimal" size for the hash table. The criteria is a
2800 minimal chain length. The minor criteria is (of course) the size
2802 for (i
= minsize
; i
< maxsize
; ++i
)
2804 /* Walk through the array of hashcodes and count the collisions. */
2805 BFD_HOST_U_64_BIT max
;
2806 unsigned long int j
;
2807 unsigned long int fact
;
2809 memset (counts
, '\0', i
* sizeof (unsigned long int));
2811 /* Determine how often each hash bucket is used. */
2812 for (j
= 0; j
< nsyms
; ++j
)
2813 ++counts
[hashcodes
[j
] % i
];
2815 /* For the weight function we need some information about the
2816 pagesize on the target. This is information need not be 100%
2817 accurate. Since this information is not available (so far) we
2818 define it here to a reasonable default value. If it is crucial
2819 to have a better value some day simply define this value. */
2820 # ifndef BFD_TARGET_PAGESIZE
2821 # define BFD_TARGET_PAGESIZE (4096)
2824 /* We in any case need 2 + NSYMS entries for the size values and
2826 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
2829 /* Variant 1: optimize for short chains. We add the squares
2830 of all the chain lengths (which favous many small chain
2831 over a few long chains). */
2832 for (j
= 0; j
< i
; ++j
)
2833 max
+= counts
[j
] * counts
[j
];
2835 /* This adds penalties for the overall size of the table. */
2836 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2839 /* Variant 2: Optimize a lot more for small table. Here we
2840 also add squares of the size but we also add penalties for
2841 empty slots (the +1 term). */
2842 for (j
= 0; j
< i
; ++j
)
2843 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
2845 /* The overall size of the table is considered, but not as
2846 strong as in variant 1, where it is squared. */
2847 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2851 /* Compare with current best results. */
2852 if (max
< best_chlen
)
2862 #endif /* defined (BFD_HOST_U_64_BIT) */
2864 /* This is the fallback solution if no 64bit type is available or if we
2865 are not supposed to spend much time on optimizations. We select the
2866 bucket count using a fixed set of numbers. */
2867 for (i
= 0; elf_buckets
[i
] != 0; i
++)
2869 best_size
= elf_buckets
[i
];
2870 if (dynsymcount
< elf_buckets
[i
+ 1])
2875 /* Free the arrays we needed. */
2881 /* Set up the sizes and contents of the ELF dynamic sections. This is
2882 called by the ELF linker emulation before_allocation routine. We
2883 must set the sizes of the sections before the linker sets the
2884 addresses of the various sections. */
2887 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
2889 auxiliary_filters
, info
, sinterpptr
,
2894 const char *filter_shlib
;
2895 const char * const *auxiliary_filters
;
2896 struct bfd_link_info
*info
;
2897 asection
**sinterpptr
;
2898 struct bfd_elf_version_tree
*verdefs
;
2900 bfd_size_type soname_indx
;
2902 struct elf_backend_data
*bed
;
2903 struct elf_assign_sym_version_info asvinfo
;
2907 soname_indx
= (bfd_size_type
) -1;
2909 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2912 if (! is_elf_hash_table (info
))
2915 /* Any syms created from now on start with -1 in
2916 got.refcount/offset and plt.refcount/offset. */
2917 elf_hash_table (info
)->init_refcount
= -1;
2919 /* The backend may have to create some sections regardless of whether
2920 we're dynamic or not. */
2921 bed
= get_elf_backend_data (output_bfd
);
2922 if (bed
->elf_backend_always_size_sections
2923 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
2926 dynobj
= elf_hash_table (info
)->dynobj
;
2928 /* If there were no dynamic objects in the link, there is nothing to
2933 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
2936 if (elf_hash_table (info
)->dynamic_sections_created
)
2938 struct elf_info_failed eif
;
2939 struct elf_link_hash_entry
*h
;
2942 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
2943 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
2947 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2949 if (soname_indx
== (bfd_size_type
) -1
2950 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SONAME
,
2957 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMBOLIC
,
2960 info
->flags
|= DF_SYMBOLIC
;
2967 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
2969 if (info
->new_dtags
)
2970 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
2971 if (indx
== (bfd_size_type
) -1
2972 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RPATH
, indx
)
2974 && ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RUNPATH
,
2979 if (filter_shlib
!= NULL
)
2983 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2984 filter_shlib
, true);
2985 if (indx
== (bfd_size_type
) -1
2986 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FILTER
, indx
))
2990 if (auxiliary_filters
!= NULL
)
2992 const char * const *p
;
2994 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
2998 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3000 if (indx
== (bfd_size_type
) -1
3001 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_AUXILIARY
,
3008 eif
.verdefs
= verdefs
;
3011 /* If we are supposed to export all symbols into the dynamic symbol
3012 table (this is not the normal case), then do so. */
3013 if (info
->export_dynamic
)
3015 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
3021 /* Attach all the symbols to their version information. */
3022 asvinfo
.output_bfd
= output_bfd
;
3023 asvinfo
.info
= info
;
3024 asvinfo
.verdefs
= verdefs
;
3025 asvinfo
.failed
= false;
3027 elf_link_hash_traverse (elf_hash_table (info
),
3028 elf_link_assign_sym_version
,
3033 /* Find all symbols which were defined in a dynamic object and make
3034 the backend pick a reasonable value for them. */
3035 elf_link_hash_traverse (elf_hash_table (info
),
3036 elf_adjust_dynamic_symbol
,
3041 /* Add some entries to the .dynamic section. We fill in some of the
3042 values later, in elf_bfd_final_link, but we must add the entries
3043 now so that we know the final size of the .dynamic section. */
3045 /* If there are initialization and/or finalization functions to
3046 call then add the corresponding DT_INIT/DT_FINI entries. */
3047 h
= (info
->init_function
3048 ? elf_link_hash_lookup (elf_hash_table (info
),
3049 info
->init_function
, false,
3053 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3054 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3056 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT
, (bfd_vma
) 0))
3059 h
= (info
->fini_function
3060 ? elf_link_hash_lookup (elf_hash_table (info
),
3061 info
->fini_function
, false,
3065 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3066 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3068 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI
, (bfd_vma
) 0))
3072 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
3074 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3080 for (sub
= info
->input_bfds
; sub
!= NULL
;
3081 sub
= sub
->link_next
)
3082 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3083 if (elf_section_data (o
)->this_hdr
.sh_type
3084 == SHT_PREINIT_ARRAY
)
3086 (*_bfd_error_handler
)
3087 (_("%s: .preinit_array section is not allowed in DSO"),
3088 bfd_archive_filename (sub
));
3092 bfd_set_error (bfd_error_nonrepresentable_section
);
3096 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAY
,
3098 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAYSZ
,
3102 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
3104 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAY
,
3106 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAYSZ
,
3110 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
3112 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAY
,
3114 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAYSZ
,
3119 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
3120 /* If .dynstr is excluded from the link, we don't want any of
3121 these tags. Strictly, we should be checking each section
3122 individually; This quick check covers for the case where
3123 someone does a /DISCARD/ : { *(*) }. */
3124 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
3126 bfd_size_type strsize
;
3128 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3129 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_HASH
, (bfd_vma
) 0)
3130 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRTAB
, (bfd_vma
) 0)
3131 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMTAB
, (bfd_vma
) 0)
3132 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRSZ
, strsize
)
3133 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMENT
,
3134 (bfd_vma
) sizeof (Elf_External_Sym
)))
3139 /* The backend must work out the sizes of all the other dynamic
3141 if (bed
->elf_backend_size_dynamic_sections
3142 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
3145 if (elf_hash_table (info
)->dynamic_sections_created
)
3147 bfd_size_type dynsymcount
;
3149 size_t bucketcount
= 0;
3150 size_t hash_entry_size
;
3151 unsigned int dtagcount
;
3153 /* Set up the version definition section. */
3154 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3155 BFD_ASSERT (s
!= NULL
);
3157 /* We may have created additional version definitions if we are
3158 just linking a regular application. */
3159 verdefs
= asvinfo
.verdefs
;
3161 /* Skip anonymous version tag. */
3162 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
3163 verdefs
= verdefs
->next
;
3165 if (verdefs
== NULL
)
3166 _bfd_strip_section_from_output (info
, s
);
3171 struct bfd_elf_version_tree
*t
;
3173 Elf_Internal_Verdef def
;
3174 Elf_Internal_Verdaux defaux
;
3179 /* Make space for the base version. */
3180 size
+= sizeof (Elf_External_Verdef
);
3181 size
+= sizeof (Elf_External_Verdaux
);
3184 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3186 struct bfd_elf_version_deps
*n
;
3188 size
+= sizeof (Elf_External_Verdef
);
3189 size
+= sizeof (Elf_External_Verdaux
);
3192 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3193 size
+= sizeof (Elf_External_Verdaux
);
3196 s
->_raw_size
= size
;
3197 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3198 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3201 /* Fill in the version definition section. */
3205 def
.vd_version
= VER_DEF_CURRENT
;
3206 def
.vd_flags
= VER_FLG_BASE
;
3209 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3210 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3211 + sizeof (Elf_External_Verdaux
));
3213 if (soname_indx
!= (bfd_size_type
) -1)
3215 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3217 def
.vd_hash
= bfd_elf_hash (soname
);
3218 defaux
.vda_name
= soname_indx
;
3225 name
= basename (output_bfd
->filename
);
3226 def
.vd_hash
= bfd_elf_hash (name
);
3227 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3229 if (indx
== (bfd_size_type
) -1)
3231 defaux
.vda_name
= indx
;
3233 defaux
.vda_next
= 0;
3235 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3236 (Elf_External_Verdef
*) p
);
3237 p
+= sizeof (Elf_External_Verdef
);
3238 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3239 (Elf_External_Verdaux
*) p
);
3240 p
+= sizeof (Elf_External_Verdaux
);
3242 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3245 struct bfd_elf_version_deps
*n
;
3246 struct elf_link_hash_entry
*h
;
3249 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3252 /* Add a symbol representing this version. */
3254 if (! (_bfd_generic_link_add_one_symbol
3255 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
3256 (bfd_vma
) 0, (const char *) NULL
, false,
3257 get_elf_backend_data (dynobj
)->collect
,
3258 (struct bfd_link_hash_entry
**) &h
)))
3260 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
3261 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3262 h
->type
= STT_OBJECT
;
3263 h
->verinfo
.vertree
= t
;
3265 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
3268 def
.vd_version
= VER_DEF_CURRENT
;
3270 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
3271 def
.vd_flags
|= VER_FLG_WEAK
;
3272 def
.vd_ndx
= t
->vernum
+ 1;
3273 def
.vd_cnt
= cdeps
+ 1;
3274 def
.vd_hash
= bfd_elf_hash (t
->name
);
3275 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3276 if (t
->next
!= NULL
)
3277 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3278 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
3282 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3283 (Elf_External_Verdef
*) p
);
3284 p
+= sizeof (Elf_External_Verdef
);
3286 defaux
.vda_name
= h
->dynstr_index
;
3287 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3289 if (t
->deps
== NULL
)
3290 defaux
.vda_next
= 0;
3292 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3293 t
->name_indx
= defaux
.vda_name
;
3295 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3296 (Elf_External_Verdaux
*) p
);
3297 p
+= sizeof (Elf_External_Verdaux
);
3299 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3301 if (n
->version_needed
== NULL
)
3303 /* This can happen if there was an error in the
3305 defaux
.vda_name
= 0;
3309 defaux
.vda_name
= n
->version_needed
->name_indx
;
3310 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3313 if (n
->next
== NULL
)
3314 defaux
.vda_next
= 0;
3316 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3318 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3319 (Elf_External_Verdaux
*) p
);
3320 p
+= sizeof (Elf_External_Verdaux
);
3324 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEF
, (bfd_vma
) 0)
3325 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEFNUM
,
3329 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
3332 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
3334 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS
, info
->flags
))
3341 info
->flags_1
&= ~ (DF_1_INITFIRST
3344 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS_1
,
3349 /* Work out the size of the version reference section. */
3351 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3352 BFD_ASSERT (s
!= NULL
);
3354 struct elf_find_verdep_info sinfo
;
3356 sinfo
.output_bfd
= output_bfd
;
3358 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
3359 if (sinfo
.vers
== 0)
3361 sinfo
.failed
= false;
3363 elf_link_hash_traverse (elf_hash_table (info
),
3364 elf_link_find_version_dependencies
,
3367 if (elf_tdata (output_bfd
)->verref
== NULL
)
3368 _bfd_strip_section_from_output (info
, s
);
3371 Elf_Internal_Verneed
*t
;
3376 /* Build the version definition section. */
3379 for (t
= elf_tdata (output_bfd
)->verref
;
3383 Elf_Internal_Vernaux
*a
;
3385 size
+= sizeof (Elf_External_Verneed
);
3387 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3388 size
+= sizeof (Elf_External_Vernaux
);
3391 s
->_raw_size
= size
;
3392 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3393 if (s
->contents
== NULL
)
3397 for (t
= elf_tdata (output_bfd
)->verref
;
3402 Elf_Internal_Vernaux
*a
;
3406 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3409 t
->vn_version
= VER_NEED_CURRENT
;
3411 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3412 elf_dt_name (t
->vn_bfd
) != NULL
3413 ? elf_dt_name (t
->vn_bfd
)
3414 : basename (t
->vn_bfd
->filename
),
3416 if (indx
== (bfd_size_type
) -1)
3419 t
->vn_aux
= sizeof (Elf_External_Verneed
);
3420 if (t
->vn_nextref
== NULL
)
3423 t
->vn_next
= (sizeof (Elf_External_Verneed
)
3424 + caux
* sizeof (Elf_External_Vernaux
));
3426 _bfd_elf_swap_verneed_out (output_bfd
, t
,
3427 (Elf_External_Verneed
*) p
);
3428 p
+= sizeof (Elf_External_Verneed
);
3430 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3432 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
3433 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3434 a
->vna_nodename
, false);
3435 if (indx
== (bfd_size_type
) -1)
3438 if (a
->vna_nextptr
== NULL
)
3441 a
->vna_next
= sizeof (Elf_External_Vernaux
);
3443 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
3444 (Elf_External_Vernaux
*) p
);
3445 p
+= sizeof (Elf_External_Vernaux
);
3449 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEED
,
3451 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEEDNUM
,
3455 elf_tdata (output_bfd
)->cverrefs
= crefs
;
3459 /* Assign dynsym indicies. In a shared library we generate a
3460 section symbol for each output section, which come first.
3461 Next come all of the back-end allocated local dynamic syms,
3462 followed by the rest of the global symbols. */
3464 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3466 /* Work out the size of the symbol version section. */
3467 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3468 BFD_ASSERT (s
!= NULL
);
3469 if (dynsymcount
== 0
3470 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
3472 _bfd_strip_section_from_output (info
, s
);
3473 /* The DYNSYMCOUNT might have changed if we were going to
3474 output a dynamic symbol table entry for S. */
3475 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3479 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
3480 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3481 if (s
->contents
== NULL
)
3484 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERSYM
, (bfd_vma
) 0))
3488 /* Set the size of the .dynsym and .hash sections. We counted
3489 the number of dynamic symbols in elf_link_add_object_symbols.
3490 We will build the contents of .dynsym and .hash when we build
3491 the final symbol table, because until then we do not know the
3492 correct value to give the symbols. We built the .dynstr
3493 section as we went along in elf_link_add_object_symbols. */
3494 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
3495 BFD_ASSERT (s
!= NULL
);
3496 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
3497 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3498 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3501 if (dynsymcount
!= 0)
3503 Elf_Internal_Sym isym
;
3505 /* The first entry in .dynsym is a dummy symbol. */
3512 elf_swap_symbol_out (output_bfd
, &isym
, (PTR
) s
->contents
, (PTR
) 0);
3515 /* Compute the size of the hashing table. As a side effect this
3516 computes the hash values for all the names we export. */
3517 bucketcount
= compute_bucket_count (info
);
3519 s
= bfd_get_section_by_name (dynobj
, ".hash");
3520 BFD_ASSERT (s
!= NULL
);
3521 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
3522 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
3523 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3524 if (s
->contents
== NULL
)
3527 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) bucketcount
,
3529 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) dynsymcount
,
3530 s
->contents
+ hash_entry_size
);
3532 elf_hash_table (info
)->bucketcount
= bucketcount
;
3534 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
3535 BFD_ASSERT (s
!= NULL
);
3537 elf_finalize_dynstr (output_bfd
, info
);
3539 s
->_raw_size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3541 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
3542 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NULL
, (bfd_vma
) 0))
3549 /* This function is used to adjust offsets into .dynstr for
3550 dynamic symbols. This is called via elf_link_hash_traverse. */
3552 static boolean elf_adjust_dynstr_offsets
3553 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3556 elf_adjust_dynstr_offsets (h
, data
)
3557 struct elf_link_hash_entry
*h
;
3560 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3562 if (h
->root
.type
== bfd_link_hash_warning
)
3563 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3565 if (h
->dynindx
!= -1)
3566 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3570 /* Assign string offsets in .dynstr, update all structures referencing
3574 elf_finalize_dynstr (output_bfd
, info
)
3576 struct bfd_link_info
*info
;
3578 struct elf_link_local_dynamic_entry
*entry
;
3579 struct elf_strtab_hash
*dynstr
= elf_hash_table (info
)->dynstr
;
3580 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
3583 Elf_External_Dyn
*dyncon
, *dynconend
;
3585 _bfd_elf_strtab_finalize (dynstr
);
3586 size
= _bfd_elf_strtab_size (dynstr
);
3588 /* Update all .dynamic entries referencing .dynstr strings. */
3589 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3590 BFD_ASSERT (sdyn
!= NULL
);
3592 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
3593 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
3595 for (; dyncon
< dynconend
; dyncon
++)
3597 Elf_Internal_Dyn dyn
;
3599 elf_swap_dyn_in (dynobj
, dyncon
, & dyn
);
3603 dyn
.d_un
.d_val
= size
;
3604 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3612 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3613 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3620 /* Now update local dynamic symbols. */
3621 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
3622 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3623 entry
->isym
.st_name
);
3625 /* And the rest of dynamic symbols. */
3626 elf_link_hash_traverse (elf_hash_table (info
),
3627 elf_adjust_dynstr_offsets
, dynstr
);
3629 /* Adjust version definitions. */
3630 if (elf_tdata (output_bfd
)->cverdefs
)
3635 Elf_Internal_Verdef def
;
3636 Elf_Internal_Verdaux defaux
;
3638 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3639 p
= (bfd_byte
*) s
->contents
;
3642 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3644 p
+= sizeof (Elf_External_Verdef
);
3645 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3647 _bfd_elf_swap_verdaux_in (output_bfd
,
3648 (Elf_External_Verdaux
*) p
, &defaux
);
3649 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3651 _bfd_elf_swap_verdaux_out (output_bfd
,
3652 &defaux
, (Elf_External_Verdaux
*) p
);
3653 p
+= sizeof (Elf_External_Verdaux
);
3656 while (def
.vd_next
);
3659 /* Adjust version references. */
3660 if (elf_tdata (output_bfd
)->verref
)
3665 Elf_Internal_Verneed need
;
3666 Elf_Internal_Vernaux needaux
;
3668 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3669 p
= (bfd_byte
*) s
->contents
;
3672 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3674 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3675 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3676 (Elf_External_Verneed
*) p
);
3677 p
+= sizeof (Elf_External_Verneed
);
3678 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3680 _bfd_elf_swap_vernaux_in (output_bfd
,
3681 (Elf_External_Vernaux
*) p
, &needaux
);
3682 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3684 _bfd_elf_swap_vernaux_out (output_bfd
,
3686 (Elf_External_Vernaux
*) p
);
3687 p
+= sizeof (Elf_External_Vernaux
);
3690 while (need
.vn_next
);
3696 /* Fix up the flags for a symbol. This handles various cases which
3697 can only be fixed after all the input files are seen. This is
3698 currently called by both adjust_dynamic_symbol and
3699 assign_sym_version, which is unnecessary but perhaps more robust in
3700 the face of future changes. */
3703 elf_fix_symbol_flags (h
, eif
)
3704 struct elf_link_hash_entry
*h
;
3705 struct elf_info_failed
*eif
;
3707 /* If this symbol was mentioned in a non-ELF file, try to set
3708 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3709 permit a non-ELF file to correctly refer to a symbol defined in
3710 an ELF dynamic object. */
3711 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
3713 while (h
->root
.type
== bfd_link_hash_indirect
)
3714 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3716 if (h
->root
.type
!= bfd_link_hash_defined
3717 && h
->root
.type
!= bfd_link_hash_defweak
)
3718 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3719 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3722 if (h
->root
.u
.def
.section
->owner
!= NULL
3723 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3724 == bfd_target_elf_flavour
))
3725 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3726 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3728 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3731 if (h
->dynindx
== -1
3732 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
3733 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
3735 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3744 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3745 was first seen in a non-ELF file. Fortunately, if the symbol
3746 was first seen in an ELF file, we're probably OK unless the
3747 symbol was defined in a non-ELF file. Catch that case here.
3748 FIXME: We're still in trouble if the symbol was first seen in
3749 a dynamic object, and then later in a non-ELF regular object. */
3750 if ((h
->root
.type
== bfd_link_hash_defined
3751 || h
->root
.type
== bfd_link_hash_defweak
)
3752 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3753 && (h
->root
.u
.def
.section
->owner
!= NULL
3754 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3755 != bfd_target_elf_flavour
)
3756 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3757 && (h
->elf_link_hash_flags
3758 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
3759 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3762 /* If this is a final link, and the symbol was defined as a common
3763 symbol in a regular object file, and there was no definition in
3764 any dynamic object, then the linker will have allocated space for
3765 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3766 flag will not have been set. */
3767 if (h
->root
.type
== bfd_link_hash_defined
3768 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3769 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
3770 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3771 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3772 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3774 /* If -Bsymbolic was used (which means to bind references to global
3775 symbols to the definition within the shared object), and this
3776 symbol was defined in a regular object, then it actually doesn't
3777 need a PLT entry, and we can accomplish that by forcing it local.
3778 Likewise, if the symbol has hidden or internal visibility.
3779 FIXME: It might be that we also do not need a PLT for other
3780 non-hidden visibilities, but we would have to tell that to the
3781 backend specifically; we can't just clear PLT-related data here. */
3782 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
3783 && eif
->info
->shared
3784 && is_elf_hash_table (eif
->info
)
3785 && (eif
->info
->symbolic
3786 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3787 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3788 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3790 struct elf_backend_data
*bed
;
3791 boolean force_local
;
3793 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3795 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3796 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
3797 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
3800 /* If this is a weak defined symbol in a dynamic object, and we know
3801 the real definition in the dynamic object, copy interesting flags
3802 over to the real definition. */
3803 if (h
->weakdef
!= NULL
)
3805 struct elf_link_hash_entry
*weakdef
;
3807 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3808 || h
->root
.type
== bfd_link_hash_defweak
);
3809 weakdef
= h
->weakdef
;
3810 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
3811 || weakdef
->root
.type
== bfd_link_hash_defweak
);
3812 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
3814 /* If the real definition is defined by a regular object file,
3815 don't do anything special. See the longer description in
3816 elf_adjust_dynamic_symbol, below. */
3817 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3821 struct elf_backend_data
*bed
;
3823 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3824 (*bed
->elf_backend_copy_indirect_symbol
) (weakdef
, h
);
3831 /* Make the backend pick a good value for a dynamic symbol. This is
3832 called via elf_link_hash_traverse, and also calls itself
3836 elf_adjust_dynamic_symbol (h
, data
)
3837 struct elf_link_hash_entry
*h
;
3840 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3842 struct elf_backend_data
*bed
;
3844 if (h
->root
.type
== bfd_link_hash_warning
)
3846 h
->plt
.offset
= (bfd_vma
) -1;
3847 h
->got
.offset
= (bfd_vma
) -1;
3849 /* When warning symbols are created, they **replace** the "real"
3850 entry in the hash table, thus we never get to see the real
3851 symbol in a hash traversal. So look at it now. */
3852 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3855 /* Ignore indirect symbols. These are added by the versioning code. */
3856 if (h
->root
.type
== bfd_link_hash_indirect
)
3859 if (! is_elf_hash_table (eif
->info
))
3862 /* Fix the symbol flags. */
3863 if (! elf_fix_symbol_flags (h
, eif
))
3866 /* If this symbol does not require a PLT entry, and it is not
3867 defined by a dynamic object, or is not referenced by a regular
3868 object, ignore it. We do have to handle a weak defined symbol,
3869 even if no regular object refers to it, if we decided to add it
3870 to the dynamic symbol table. FIXME: Do we normally need to worry
3871 about symbols which are defined by one dynamic object and
3872 referenced by another one? */
3873 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
3874 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
3875 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3876 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
3877 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
3879 h
->plt
.offset
= (bfd_vma
) -1;
3883 /* If we've already adjusted this symbol, don't do it again. This
3884 can happen via a recursive call. */
3885 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
3888 /* Don't look at this symbol again. Note that we must set this
3889 after checking the above conditions, because we may look at a
3890 symbol once, decide not to do anything, and then get called
3891 recursively later after REF_REGULAR is set below. */
3892 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
3894 /* If this is a weak definition, and we know a real definition, and
3895 the real symbol is not itself defined by a regular object file,
3896 then get a good value for the real definition. We handle the
3897 real symbol first, for the convenience of the backend routine.
3899 Note that there is a confusing case here. If the real definition
3900 is defined by a regular object file, we don't get the real symbol
3901 from the dynamic object, but we do get the weak symbol. If the
3902 processor backend uses a COPY reloc, then if some routine in the
3903 dynamic object changes the real symbol, we will not see that
3904 change in the corresponding weak symbol. This is the way other
3905 ELF linkers work as well, and seems to be a result of the shared
3908 I will clarify this issue. Most SVR4 shared libraries define the
3909 variable _timezone and define timezone as a weak synonym. The
3910 tzset call changes _timezone. If you write
3911 extern int timezone;
3913 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3914 you might expect that, since timezone is a synonym for _timezone,
3915 the same number will print both times. However, if the processor
3916 backend uses a COPY reloc, then actually timezone will be copied
3917 into your process image, and, since you define _timezone
3918 yourself, _timezone will not. Thus timezone and _timezone will
3919 wind up at different memory locations. The tzset call will set
3920 _timezone, leaving timezone unchanged. */
3922 if (h
->weakdef
!= NULL
)
3924 /* If we get to this point, we know there is an implicit
3925 reference by a regular object file via the weak symbol H.
3926 FIXME: Is this really true? What if the traversal finds
3927 H->WEAKDEF before it finds H? */
3928 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
3930 if (! elf_adjust_dynamic_symbol (h
->weakdef
, (PTR
) eif
))
3934 /* If a symbol has no type and no size and does not require a PLT
3935 entry, then we are probably about to do the wrong thing here: we
3936 are probably going to create a COPY reloc for an empty object.
3937 This case can arise when a shared object is built with assembly
3938 code, and the assembly code fails to set the symbol type. */
3940 && h
->type
== STT_NOTYPE
3941 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
3942 (*_bfd_error_handler
)
3943 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3944 h
->root
.root
.string
);
3946 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
3947 bed
= get_elf_backend_data (dynobj
);
3948 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3957 /* This routine is used to export all defined symbols into the dynamic
3958 symbol table. It is called via elf_link_hash_traverse. */
3961 elf_export_symbol (h
, data
)
3962 struct elf_link_hash_entry
*h
;
3965 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3967 /* Ignore indirect symbols. These are added by the versioning code. */
3968 if (h
->root
.type
== bfd_link_hash_indirect
)
3971 if (h
->root
.type
== bfd_link_hash_warning
)
3972 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3974 if (h
->dynindx
== -1
3975 && (h
->elf_link_hash_flags
3976 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
3978 struct bfd_elf_version_tree
*t
;
3979 struct bfd_elf_version_expr
*d
;
3981 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
3983 if (t
->globals
!= NULL
)
3985 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3987 if ((*d
->match
) (d
, h
->root
.root
.string
))
3992 if (t
->locals
!= NULL
)
3994 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3996 if ((*d
->match
) (d
, h
->root
.root
.string
))
4005 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
4016 /* Look through the symbols which are defined in other shared
4017 libraries and referenced here. Update the list of version
4018 dependencies. This will be put into the .gnu.version_r section.
4019 This function is called via elf_link_hash_traverse. */
4022 elf_link_find_version_dependencies (h
, data
)
4023 struct elf_link_hash_entry
*h
;
4026 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
4027 Elf_Internal_Verneed
*t
;
4028 Elf_Internal_Vernaux
*a
;
4031 if (h
->root
.type
== bfd_link_hash_warning
)
4032 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4034 /* We only care about symbols defined in shared objects with version
4036 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4037 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4039 || h
->verinfo
.verdef
== NULL
)
4042 /* See if we already know about this version. */
4043 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
4045 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
4048 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4049 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
4055 /* This is a new version. Add it to tree we are building. */
4060 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4063 rinfo
->failed
= true;
4067 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
4068 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
4069 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
4073 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4075 /* Note that we are copying a string pointer here, and testing it
4076 above. If bfd_elf_string_from_elf_section is ever changed to
4077 discard the string data when low in memory, this will have to be
4079 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
4081 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
4082 a
->vna_nextptr
= t
->vn_auxptr
;
4084 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
4087 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4094 /* Figure out appropriate versions for all the symbols. We may not
4095 have the version number script until we have read all of the input
4096 files, so until that point we don't know which symbols should be
4097 local. This function is called via elf_link_hash_traverse. */
4100 elf_link_assign_sym_version (h
, data
)
4101 struct elf_link_hash_entry
*h
;
4104 struct elf_assign_sym_version_info
*sinfo
;
4105 struct bfd_link_info
*info
;
4106 struct elf_backend_data
*bed
;
4107 struct elf_info_failed eif
;
4111 sinfo
= (struct elf_assign_sym_version_info
*) data
;
4114 if (h
->root
.type
== bfd_link_hash_warning
)
4115 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4117 /* Fix the symbol flags. */
4120 if (! elf_fix_symbol_flags (h
, &eif
))
4123 sinfo
->failed
= true;
4127 /* We only need version numbers for symbols defined in regular
4129 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4132 bed
= get_elf_backend_data (sinfo
->output_bfd
);
4133 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4134 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
4136 struct bfd_elf_version_tree
*t
;
4141 /* There are two consecutive ELF_VER_CHR characters if this is
4142 not a hidden symbol. */
4144 if (*p
== ELF_VER_CHR
)
4150 /* If there is no version string, we can just return out. */
4154 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4158 /* Look for the version. If we find it, it is no longer weak. */
4159 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4161 if (strcmp (t
->name
, p
) == 0)
4165 struct bfd_elf_version_expr
*d
;
4167 len
= p
- h
->root
.root
.string
;
4168 alc
= bfd_malloc ((bfd_size_type
) len
);
4171 memcpy (alc
, h
->root
.root
.string
, len
- 1);
4172 alc
[len
- 1] = '\0';
4173 if (alc
[len
- 2] == ELF_VER_CHR
)
4174 alc
[len
- 2] = '\0';
4176 h
->verinfo
.vertree
= t
;
4180 if (t
->globals
!= NULL
)
4182 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4183 if ((*d
->match
) (d
, alc
))
4187 /* See if there is anything to force this symbol to
4189 if (d
== NULL
&& t
->locals
!= NULL
)
4191 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4193 if ((*d
->match
) (d
, alc
))
4195 if (h
->dynindx
!= -1
4197 && ! info
->export_dynamic
)
4199 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
4212 /* If we are building an application, we need to create a
4213 version node for this version. */
4214 if (t
== NULL
&& ! info
->shared
)
4216 struct bfd_elf_version_tree
**pp
;
4219 /* If we aren't going to export this symbol, we don't need
4220 to worry about it. */
4221 if (h
->dynindx
== -1)
4225 t
= ((struct bfd_elf_version_tree
*)
4226 bfd_alloc (sinfo
->output_bfd
, amt
));
4229 sinfo
->failed
= true;
4238 t
->name_indx
= (unsigned int) -1;
4242 /* Don't count anonymous version tag. */
4243 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
4245 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4247 t
->vernum
= version_index
;
4251 h
->verinfo
.vertree
= t
;
4255 /* We could not find the version for a symbol when
4256 generating a shared archive. Return an error. */
4257 (*_bfd_error_handler
)
4258 (_("%s: undefined versioned symbol name %s"),
4259 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
4260 bfd_set_error (bfd_error_bad_value
);
4262 sinfo
->failed
= true;
4267 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4270 /* If we don't have a version for this symbol, see if we can find
4272 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
4274 struct bfd_elf_version_tree
*t
;
4275 struct bfd_elf_version_tree
*local_ver
;
4276 struct bfd_elf_version_expr
*d
;
4278 /* See if can find what version this symbol is in. If the
4279 symbol is supposed to be local, then don't actually register
4282 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4284 if (t
->globals
!= NULL
)
4286 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4288 if ((*d
->match
) (d
, h
->root
.root
.string
))
4290 h
->verinfo
.vertree
= t
;
4300 if (t
->locals
!= NULL
)
4302 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4304 /* If the match is "*", keep looking for a more
4305 explicit, perhaps even global, match. */
4306 if (d
->pattern
[0] == '*' && d
->pattern
[1] == '\0')
4308 else if ((*d
->match
) (d
, h
->root
.root
.string
))
4320 if (local_ver
!= NULL
)
4322 h
->verinfo
.vertree
= local_ver
;
4323 if (h
->dynindx
!= -1
4325 && ! info
->export_dynamic
)
4327 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
4331 /* We need to check if a hidden versioned definition should
4332 hide the default one. */
4333 if (h
->dynindx
!= -1 && h
->verinfo
.vertree
!= NULL
)
4335 const char *verstr
, *name
;
4336 size_t namelen
, verlen
, newlen
;
4338 struct elf_link_hash_entry
*newh
;
4340 name
= h
->root
.root
.string
;
4341 namelen
= strlen (name
);
4342 verstr
= h
->verinfo
.vertree
->name
;
4343 verlen
= strlen (verstr
);
4344 newlen
= namelen
+ verlen
+ 2;
4346 newname
= (char *) bfd_malloc ((bfd_size_type
) newlen
);
4347 if (newname
== NULL
)
4349 memcpy (newname
, name
, namelen
);
4351 /* Check the hidden versioned definition. */
4352 p
= newname
+ namelen
;
4354 memcpy (p
, verstr
, verlen
+ 1);
4355 newh
= elf_link_hash_lookup (elf_hash_table (info
), newname
,
4356 false, false, false);
4359 && (newh
->root
.type
== bfd_link_hash_defined
4360 || newh
->root
.type
== bfd_link_hash_defweak
))
4361 /* We found a hidden versioned definition. Hide the
4363 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
4372 /* Final phase of ELF linker. */
4374 /* A structure we use to avoid passing large numbers of arguments. */
4376 struct elf_final_link_info
4378 /* General link information. */
4379 struct bfd_link_info
*info
;
4382 /* Symbol string table. */
4383 struct bfd_strtab_hash
*symstrtab
;
4384 /* .dynsym section. */
4385 asection
*dynsym_sec
;
4386 /* .hash section. */
4388 /* symbol version section (.gnu.version). */
4389 asection
*symver_sec
;
4390 /* first SHF_TLS section (if any). */
4391 asection
*first_tls_sec
;
4392 /* Buffer large enough to hold contents of any section. */
4394 /* Buffer large enough to hold external relocs of any section. */
4395 PTR external_relocs
;
4396 /* Buffer large enough to hold internal relocs of any section. */
4397 Elf_Internal_Rela
*internal_relocs
;
4398 /* Buffer large enough to hold external local symbols of any input
4400 Elf_External_Sym
*external_syms
;
4401 /* And a buffer for symbol section indices. */
4402 Elf_External_Sym_Shndx
*locsym_shndx
;
4403 /* Buffer large enough to hold internal local symbols of any input
4405 Elf_Internal_Sym
*internal_syms
;
4406 /* Array large enough to hold a symbol index for each local symbol
4407 of any input BFD. */
4409 /* Array large enough to hold a section pointer for each local
4410 symbol of any input BFD. */
4411 asection
**sections
;
4412 /* Buffer to hold swapped out symbols. */
4413 Elf_External_Sym
*symbuf
;
4414 /* And one for symbol section indices. */
4415 Elf_External_Sym_Shndx
*symshndxbuf
;
4416 /* Number of swapped out symbols in buffer. */
4417 size_t symbuf_count
;
4418 /* Number of symbols which fit in symbuf. */
4422 static boolean elf_link_output_sym
4423 PARAMS ((struct elf_final_link_info
*, const char *,
4424 Elf_Internal_Sym
*, asection
*));
4425 static boolean elf_link_flush_output_syms
4426 PARAMS ((struct elf_final_link_info
*));
4427 static boolean elf_link_output_extsym
4428 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4429 static boolean elf_link_sec_merge_syms
4430 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4431 static boolean elf_link_check_versioned_symbol
4432 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
4433 static boolean elf_link_input_bfd
4434 PARAMS ((struct elf_final_link_info
*, bfd
*));
4435 static boolean elf_reloc_link_order
4436 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
4437 struct bfd_link_order
*));
4439 /* This struct is used to pass information to elf_link_output_extsym. */
4441 struct elf_outext_info
4445 struct elf_final_link_info
*finfo
;
4448 /* Compute the size of, and allocate space for, REL_HDR which is the
4449 section header for a section containing relocations for O. */
4452 elf_link_size_reloc_section (abfd
, rel_hdr
, o
)
4454 Elf_Internal_Shdr
*rel_hdr
;
4457 bfd_size_type reloc_count
;
4458 bfd_size_type num_rel_hashes
;
4460 /* Figure out how many relocations there will be. */
4461 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
4462 reloc_count
= elf_section_data (o
)->rel_count
;
4464 reloc_count
= elf_section_data (o
)->rel_count2
;
4466 num_rel_hashes
= o
->reloc_count
;
4467 if (num_rel_hashes
< reloc_count
)
4468 num_rel_hashes
= reloc_count
;
4470 /* That allows us to calculate the size of the section. */
4471 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
4473 /* The contents field must last into write_object_contents, so we
4474 allocate it with bfd_alloc rather than malloc. Also since we
4475 cannot be sure that the contents will actually be filled in,
4476 we zero the allocated space. */
4477 rel_hdr
->contents
= (PTR
) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
4478 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
4481 /* We only allocate one set of hash entries, so we only do it the
4482 first time we are called. */
4483 if (elf_section_data (o
)->rel_hashes
== NULL
4486 struct elf_link_hash_entry
**p
;
4488 p
= ((struct elf_link_hash_entry
**)
4489 bfd_zmalloc (num_rel_hashes
4490 * sizeof (struct elf_link_hash_entry
*)));
4494 elf_section_data (o
)->rel_hashes
= p
;
4500 /* When performing a relocateable link, the input relocations are
4501 preserved. But, if they reference global symbols, the indices
4502 referenced must be updated. Update all the relocations in
4503 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4506 elf_link_adjust_relocs (abfd
, rel_hdr
, count
, rel_hash
)
4508 Elf_Internal_Shdr
*rel_hdr
;
4510 struct elf_link_hash_entry
**rel_hash
;
4513 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4514 Elf_Internal_Rel
*irel
;
4515 Elf_Internal_Rela
*irela
;
4516 bfd_size_type amt
= sizeof (Elf_Internal_Rel
) * bed
->s
->int_rels_per_ext_rel
;
4518 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (amt
);
4521 (*_bfd_error_handler
) (_("Error: out of memory"));
4525 amt
= sizeof (Elf_Internal_Rela
) * bed
->s
->int_rels_per_ext_rel
;
4526 irela
= (Elf_Internal_Rela
*) bfd_zmalloc (amt
);
4529 (*_bfd_error_handler
) (_("Error: out of memory"));
4533 for (i
= 0; i
< count
; i
++, rel_hash
++)
4535 if (*rel_hash
== NULL
)
4538 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
4540 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4542 Elf_External_Rel
*erel
;
4545 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
4546 if (bed
->s
->swap_reloc_in
)
4547 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
4549 elf_swap_reloc_in (abfd
, erel
, irel
);
4551 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4552 irel
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4553 ELF_R_TYPE (irel
[j
].r_info
));
4555 if (bed
->s
->swap_reloc_out
)
4556 (*bed
->s
->swap_reloc_out
) (abfd
, irel
, (bfd_byte
*) erel
);
4558 elf_swap_reloc_out (abfd
, irel
, erel
);
4562 Elf_External_Rela
*erela
;
4565 BFD_ASSERT (rel_hdr
->sh_entsize
4566 == sizeof (Elf_External_Rela
));
4568 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
4569 if (bed
->s
->swap_reloca_in
)
4570 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
4572 elf_swap_reloca_in (abfd
, erela
, irela
);
4574 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4575 irela
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4576 ELF_R_TYPE (irela
[j
].r_info
));
4578 if (bed
->s
->swap_reloca_out
)
4579 (*bed
->s
->swap_reloca_out
) (abfd
, irela
, (bfd_byte
*) erela
);
4581 elf_swap_reloca_out (abfd
, irela
, erela
);
4589 struct elf_link_sort_rela
4592 enum elf_reloc_type_class type
;
4595 Elf_Internal_Rel rel
;
4596 Elf_Internal_Rela rela
;
4601 elf_link_sort_cmp1 (A
, B
)
4605 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4606 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4607 int relativea
, relativeb
;
4609 relativea
= a
->type
== reloc_class_relative
;
4610 relativeb
= b
->type
== reloc_class_relative
;
4612 if (relativea
< relativeb
)
4614 if (relativea
> relativeb
)
4616 if (ELF_R_SYM (a
->u
.rel
.r_info
) < ELF_R_SYM (b
->u
.rel
.r_info
))
4618 if (ELF_R_SYM (a
->u
.rel
.r_info
) > ELF_R_SYM (b
->u
.rel
.r_info
))
4620 if (a
->u
.rel
.r_offset
< b
->u
.rel
.r_offset
)
4622 if (a
->u
.rel
.r_offset
> b
->u
.rel
.r_offset
)
4628 elf_link_sort_cmp2 (A
, B
)
4632 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4633 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4636 if (a
->offset
< b
->offset
)
4638 if (a
->offset
> b
->offset
)
4640 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
4641 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
4646 if (a
->u
.rel
.r_offset
< b
->u
.rel
.r_offset
)
4648 if (a
->u
.rel
.r_offset
> b
->u
.rel
.r_offset
)
4654 elf_link_sort_relocs (abfd
, info
, psec
)
4656 struct bfd_link_info
*info
;
4659 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4660 asection
*reldyn
, *o
;
4661 boolean rel
= false;
4662 bfd_size_type count
, size
;
4664 struct elf_link_sort_rela
*rela
;
4665 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4667 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
4668 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4670 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
4671 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4674 count
= reldyn
->_raw_size
/ sizeof (Elf_External_Rel
);
4677 count
= reldyn
->_raw_size
/ sizeof (Elf_External_Rela
);
4680 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4681 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4682 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4683 && o
->output_section
== reldyn
)
4684 size
+= o
->_raw_size
;
4686 if (size
!= reldyn
->_raw_size
)
4689 rela
= (struct elf_link_sort_rela
*) bfd_zmalloc (sizeof (*rela
) * count
);
4692 (*info
->callbacks
->warning
)
4693 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0,
4698 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4699 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4700 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4701 && o
->output_section
== reldyn
)
4705 Elf_External_Rel
*erel
, *erelend
;
4706 struct elf_link_sort_rela
*s
;
4708 erel
= (Elf_External_Rel
*) o
->contents
;
4709 erelend
= (Elf_External_Rel
*) (o
->contents
+ o
->_raw_size
);
4710 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rel
);
4711 for (; erel
< erelend
; erel
++, s
++)
4713 if (bed
->s
->swap_reloc_in
)
4714 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, &s
->u
.rel
);
4716 elf_swap_reloc_in (abfd
, erel
, &s
->u
.rel
);
4718 s
->type
= (*bed
->elf_backend_reloc_type_class
) (&s
->u
.rela
);
4723 Elf_External_Rela
*erela
, *erelaend
;
4724 struct elf_link_sort_rela
*s
;
4726 erela
= (Elf_External_Rela
*) o
->contents
;
4727 erelaend
= (Elf_External_Rela
*) (o
->contents
+ o
->_raw_size
);
4728 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rela
);
4729 for (; erela
< erelaend
; erela
++, s
++)
4731 if (bed
->s
->swap_reloca_in
)
4732 (*bed
->s
->swap_reloca_in
) (dynobj
, (bfd_byte
*) erela
,
4735 elf_swap_reloca_in (dynobj
, erela
, &s
->u
.rela
);
4737 s
->type
= (*bed
->elf_backend_reloc_type_class
) (&s
->u
.rela
);
4742 qsort (rela
, (size_t) count
, sizeof (*rela
), elf_link_sort_cmp1
);
4743 for (ret
= 0; ret
< count
&& rela
[ret
].type
== reloc_class_relative
; ret
++)
4745 for (i
= ret
, j
= ret
; i
< count
; i
++)
4747 if (ELF_R_SYM (rela
[i
].u
.rel
.r_info
) != ELF_R_SYM (rela
[j
].u
.rel
.r_info
))
4749 rela
[i
].offset
= rela
[j
].u
.rel
.r_offset
;
4751 qsort (rela
+ ret
, (size_t) count
- ret
, sizeof (*rela
), elf_link_sort_cmp2
);
4753 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4754 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4755 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4756 && o
->output_section
== reldyn
)
4760 Elf_External_Rel
*erel
, *erelend
;
4761 struct elf_link_sort_rela
*s
;
4763 erel
= (Elf_External_Rel
*) o
->contents
;
4764 erelend
= (Elf_External_Rel
*) (o
->contents
+ o
->_raw_size
);
4765 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rel
);
4766 for (; erel
< erelend
; erel
++, s
++)
4768 if (bed
->s
->swap_reloc_out
)
4769 (*bed
->s
->swap_reloc_out
) (abfd
, &s
->u
.rel
,
4772 elf_swap_reloc_out (abfd
, &s
->u
.rel
, erel
);
4777 Elf_External_Rela
*erela
, *erelaend
;
4778 struct elf_link_sort_rela
*s
;
4780 erela
= (Elf_External_Rela
*) o
->contents
;
4781 erelaend
= (Elf_External_Rela
*) (o
->contents
+ o
->_raw_size
);
4782 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rela
);
4783 for (; erela
< erelaend
; erela
++, s
++)
4785 if (bed
->s
->swap_reloca_out
)
4786 (*bed
->s
->swap_reloca_out
) (dynobj
, &s
->u
.rela
,
4787 (bfd_byte
*) erela
);
4789 elf_swap_reloca_out (dynobj
, &s
->u
.rela
, erela
);
4799 /* Do the final step of an ELF link. */
4802 elf_bfd_final_link (abfd
, info
)
4804 struct bfd_link_info
*info
;
4807 boolean emit_relocs
;
4809 struct elf_final_link_info finfo
;
4810 register asection
*o
;
4811 register struct bfd_link_order
*p
;
4813 bfd_size_type max_contents_size
;
4814 bfd_size_type max_external_reloc_size
;
4815 bfd_size_type max_internal_reloc_count
;
4816 bfd_size_type max_sym_count
;
4817 bfd_size_type max_sym_shndx_count
;
4819 Elf_Internal_Sym elfsym
;
4821 Elf_Internal_Shdr
*symtab_hdr
;
4822 Elf_Internal_Shdr
*symstrtab_hdr
;
4823 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4824 struct elf_outext_info eoinfo
;
4826 size_t relativecount
= 0;
4827 asection
*reldyn
= 0;
4830 if (! is_elf_hash_table (info
))
4834 abfd
->flags
|= DYNAMIC
;
4836 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
4837 dynobj
= elf_hash_table (info
)->dynobj
;
4839 emit_relocs
= (info
->relocateable
4840 || info
->emitrelocations
4841 || bed
->elf_backend_emit_relocs
);
4844 finfo
.output_bfd
= abfd
;
4845 finfo
.symstrtab
= elf_stringtab_init ();
4846 if (finfo
.symstrtab
== NULL
)
4851 finfo
.dynsym_sec
= NULL
;
4852 finfo
.hash_sec
= NULL
;
4853 finfo
.symver_sec
= NULL
;
4857 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
4858 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
4859 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
4860 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
4861 /* Note that it is OK if symver_sec is NULL. */
4864 finfo
.contents
= NULL
;
4865 finfo
.external_relocs
= NULL
;
4866 finfo
.internal_relocs
= NULL
;
4867 finfo
.external_syms
= NULL
;
4868 finfo
.locsym_shndx
= NULL
;
4869 finfo
.internal_syms
= NULL
;
4870 finfo
.indices
= NULL
;
4871 finfo
.sections
= NULL
;
4872 finfo
.symbuf
= NULL
;
4873 finfo
.symshndxbuf
= NULL
;
4874 finfo
.symbuf_count
= 0;
4875 finfo
.first_tls_sec
= NULL
;
4876 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
4877 if ((o
->flags
& SEC_THREAD_LOCAL
) != 0
4878 && (o
->flags
& SEC_LOAD
) != 0)
4880 finfo
.first_tls_sec
= o
;
4884 /* Count up the number of relocations we will output for each output
4885 section, so that we know the sizes of the reloc sections. We
4886 also figure out some maximum sizes. */
4887 max_contents_size
= 0;
4888 max_external_reloc_size
= 0;
4889 max_internal_reloc_count
= 0;
4891 max_sym_shndx_count
= 0;
4893 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
4897 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
4899 if (p
->type
== bfd_section_reloc_link_order
4900 || p
->type
== bfd_symbol_reloc_link_order
)
4902 else if (p
->type
== bfd_indirect_link_order
)
4906 sec
= p
->u
.indirect
.section
;
4908 /* Mark all sections which are to be included in the
4909 link. This will normally be every section. We need
4910 to do this so that we can identify any sections which
4911 the linker has decided to not include. */
4912 sec
->linker_mark
= true;
4914 if (sec
->flags
& SEC_MERGE
)
4917 if (info
->relocateable
|| info
->emitrelocations
)
4918 o
->reloc_count
+= sec
->reloc_count
;
4919 else if (bed
->elf_backend_count_relocs
)
4921 Elf_Internal_Rela
* relocs
;
4923 relocs
= (NAME(_bfd_elf
,link_read_relocs
)
4924 (abfd
, sec
, (PTR
) NULL
,
4925 (Elf_Internal_Rela
*) NULL
, info
->keep_memory
));
4928 += (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
4930 if (elf_section_data (o
)->relocs
!= relocs
)
4934 if (sec
->_raw_size
> max_contents_size
)
4935 max_contents_size
= sec
->_raw_size
;
4936 if (sec
->_cooked_size
> max_contents_size
)
4937 max_contents_size
= sec
->_cooked_size
;
4939 /* We are interested in just local symbols, not all
4941 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
4942 && (sec
->owner
->flags
& DYNAMIC
) == 0)
4946 if (elf_bad_symtab (sec
->owner
))
4947 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
4948 / sizeof (Elf_External_Sym
));
4950 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
4952 if (sym_count
> max_sym_count
)
4953 max_sym_count
= sym_count
;
4955 if (sym_count
> max_sym_shndx_count
4956 && elf_symtab_shndx (sec
->owner
) != 0)
4957 max_sym_shndx_count
= sym_count
;
4959 if ((sec
->flags
& SEC_RELOC
) != 0)
4963 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
4964 if (ext_size
> max_external_reloc_size
)
4965 max_external_reloc_size
= ext_size
;
4966 if (sec
->reloc_count
> max_internal_reloc_count
)
4967 max_internal_reloc_count
= sec
->reloc_count
;
4973 if (o
->reloc_count
> 0)
4974 o
->flags
|= SEC_RELOC
;
4977 /* Explicitly clear the SEC_RELOC flag. The linker tends to
4978 set it (this is probably a bug) and if it is set
4979 assign_section_numbers will create a reloc section. */
4980 o
->flags
&=~ SEC_RELOC
;
4983 /* If the SEC_ALLOC flag is not set, force the section VMA to
4984 zero. This is done in elf_fake_sections as well, but forcing
4985 the VMA to 0 here will ensure that relocs against these
4986 sections are handled correctly. */
4987 if ((o
->flags
& SEC_ALLOC
) == 0
4988 && ! o
->user_set_vma
)
4992 if (! info
->relocateable
&& merged
)
4993 elf_link_hash_traverse (elf_hash_table (info
),
4994 elf_link_sec_merge_syms
, (PTR
) abfd
);
4996 /* Figure out the file positions for everything but the symbol table
4997 and the relocs. We set symcount to force assign_section_numbers
4998 to create a symbol table. */
4999 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
5000 BFD_ASSERT (! abfd
->output_has_begun
);
5001 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
5004 /* Figure out how many relocations we will have in each section.
5005 Just using RELOC_COUNT isn't good enough since that doesn't
5006 maintain a separate value for REL vs. RELA relocations. */
5008 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5009 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5011 asection
*output_section
;
5013 if (! o
->linker_mark
)
5015 /* This section was omitted from the link. */
5019 output_section
= o
->output_section
;
5021 if (output_section
!= NULL
5022 && (o
->flags
& SEC_RELOC
) != 0)
5024 struct bfd_elf_section_data
*esdi
5025 = elf_section_data (o
);
5026 struct bfd_elf_section_data
*esdo
5027 = elf_section_data (output_section
);
5028 unsigned int *rel_count
;
5029 unsigned int *rel_count2
;
5030 bfd_size_type entsize
;
5031 bfd_size_type entsize2
;
5033 /* We must be careful to add the relocations from the
5034 input section to the right output count. */
5035 entsize
= esdi
->rel_hdr
.sh_entsize
;
5036 entsize2
= esdi
->rel_hdr2
? esdi
->rel_hdr2
->sh_entsize
: 0;
5037 BFD_ASSERT ((entsize
== sizeof (Elf_External_Rel
)
5038 || entsize
== sizeof (Elf_External_Rela
))
5039 && entsize2
!= entsize
5041 || entsize2
== sizeof (Elf_External_Rel
)
5042 || entsize2
== sizeof (Elf_External_Rela
)));
5043 if (entsize
== esdo
->rel_hdr
.sh_entsize
)
5045 rel_count
= &esdo
->rel_count
;
5046 rel_count2
= &esdo
->rel_count2
;
5050 rel_count
= &esdo
->rel_count2
;
5051 rel_count2
= &esdo
->rel_count
;
5054 *rel_count
+= NUM_SHDR_ENTRIES (& esdi
->rel_hdr
);
5056 *rel_count2
+= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
5057 output_section
->flags
|= SEC_RELOC
;
5061 /* That created the reloc sections. Set their sizes, and assign
5062 them file positions, and allocate some buffers. */
5063 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5065 if ((o
->flags
& SEC_RELOC
) != 0)
5067 if (!elf_link_size_reloc_section (abfd
,
5068 &elf_section_data (o
)->rel_hdr
,
5072 if (elf_section_data (o
)->rel_hdr2
5073 && !elf_link_size_reloc_section (abfd
,
5074 elf_section_data (o
)->rel_hdr2
,
5079 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5080 to count upwards while actually outputting the relocations. */
5081 elf_section_data (o
)->rel_count
= 0;
5082 elf_section_data (o
)->rel_count2
= 0;
5085 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5087 /* We have now assigned file positions for all the sections except
5088 .symtab and .strtab. We start the .symtab section at the current
5089 file position, and write directly to it. We build the .strtab
5090 section in memory. */
5091 bfd_get_symcount (abfd
) = 0;
5092 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5093 /* sh_name is set in prep_headers. */
5094 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5095 symtab_hdr
->sh_flags
= 0;
5096 symtab_hdr
->sh_addr
= 0;
5097 symtab_hdr
->sh_size
= 0;
5098 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
5099 /* sh_link is set in assign_section_numbers. */
5100 /* sh_info is set below. */
5101 /* sh_offset is set just below. */
5102 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
5104 off
= elf_tdata (abfd
)->next_file_pos
;
5105 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
5107 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5108 incorrect. We do not yet know the size of the .symtab section.
5109 We correct next_file_pos below, after we do know the size. */
5111 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5112 continuously seeking to the right position in the file. */
5113 if (! info
->keep_memory
|| max_sym_count
< 20)
5114 finfo
.symbuf_size
= 20;
5116 finfo
.symbuf_size
= max_sym_count
;
5117 amt
= finfo
.symbuf_size
;
5118 amt
*= sizeof (Elf_External_Sym
);
5119 finfo
.symbuf
= (Elf_External_Sym
*) bfd_malloc (amt
);
5120 if (finfo
.symbuf
== NULL
)
5122 if (elf_numsections (abfd
) > SHN_LORESERVE
)
5124 amt
= finfo
.symbuf_size
;
5125 amt
*= sizeof (Elf_External_Sym_Shndx
);
5126 finfo
.symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
5127 if (finfo
.symshndxbuf
== NULL
)
5131 /* Start writing out the symbol table. The first symbol is always a
5133 if (info
->strip
!= strip_all
5136 elfsym
.st_value
= 0;
5139 elfsym
.st_other
= 0;
5140 elfsym
.st_shndx
= SHN_UNDEF
;
5141 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5142 &elfsym
, bfd_und_section_ptr
))
5147 /* Some standard ELF linkers do this, but we don't because it causes
5148 bootstrap comparison failures. */
5149 /* Output a file symbol for the output file as the second symbol.
5150 We output this even if we are discarding local symbols, although
5151 I'm not sure if this is correct. */
5152 elfsym
.st_value
= 0;
5154 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5155 elfsym
.st_other
= 0;
5156 elfsym
.st_shndx
= SHN_ABS
;
5157 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
5158 &elfsym
, bfd_abs_section_ptr
))
5162 /* Output a symbol for each section. We output these even if we are
5163 discarding local symbols, since they are used for relocs. These
5164 symbols have no names. We store the index of each one in the
5165 index field of the section, so that we can find it again when
5166 outputting relocs. */
5167 if (info
->strip
!= strip_all
5171 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5172 elfsym
.st_other
= 0;
5173 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5175 o
= section_from_elf_index (abfd
, i
);
5177 o
->target_index
= bfd_get_symcount (abfd
);
5178 elfsym
.st_shndx
= i
;
5179 if (info
->relocateable
|| o
== NULL
)
5180 elfsym
.st_value
= 0;
5182 elfsym
.st_value
= o
->vma
;
5183 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5186 if (i
== SHN_LORESERVE
)
5187 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
5191 /* Allocate some memory to hold information read in from the input
5193 if (max_contents_size
!= 0)
5195 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
5196 if (finfo
.contents
== NULL
)
5200 if (max_external_reloc_size
!= 0)
5202 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
5203 if (finfo
.external_relocs
== NULL
)
5207 if (max_internal_reloc_count
!= 0)
5209 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5210 amt
*= sizeof (Elf_Internal_Rela
);
5211 finfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
5212 if (finfo
.internal_relocs
== NULL
)
5216 if (max_sym_count
!= 0)
5218 amt
= max_sym_count
* sizeof (Elf_External_Sym
);
5219 finfo
.external_syms
= (Elf_External_Sym
*) bfd_malloc (amt
);
5220 if (finfo
.external_syms
== NULL
)
5223 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
5224 finfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
5225 if (finfo
.internal_syms
== NULL
)
5228 amt
= max_sym_count
* sizeof (long);
5229 finfo
.indices
= (long *) bfd_malloc (amt
);
5230 if (finfo
.indices
== NULL
)
5233 amt
= max_sym_count
* sizeof (asection
*);
5234 finfo
.sections
= (asection
**) bfd_malloc (amt
);
5235 if (finfo
.sections
== NULL
)
5239 if (max_sym_shndx_count
!= 0)
5241 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
5242 finfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
5243 if (finfo
.locsym_shndx
== NULL
)
5247 if (finfo
.first_tls_sec
)
5249 unsigned int align
= 0;
5250 bfd_vma base
= finfo
.first_tls_sec
->vma
, end
= 0;
5253 for (sec
= finfo
.first_tls_sec
;
5254 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
5257 bfd_vma size
= sec
->_raw_size
;
5259 if (bfd_get_section_alignment (abfd
, sec
) > align
)
5260 align
= bfd_get_section_alignment (abfd
, sec
);
5261 if (sec
->_raw_size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
5263 struct bfd_link_order
*o
;
5266 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
5267 if (size
< o
->offset
+ o
->size
)
5268 size
= o
->offset
+ o
->size
;
5270 end
= sec
->vma
+ size
;
5272 elf_hash_table (info
)->tls_segment
5273 = bfd_zalloc (abfd
, sizeof (struct elf_link_tls_segment
));
5274 if (elf_hash_table (info
)->tls_segment
== NULL
)
5276 elf_hash_table (info
)->tls_segment
->start
= base
;
5277 elf_hash_table (info
)->tls_segment
->size
= end
- base
;
5278 elf_hash_table (info
)->tls_segment
->align
= align
;
5281 /* Since ELF permits relocations to be against local symbols, we
5282 must have the local symbols available when we do the relocations.
5283 Since we would rather only read the local symbols once, and we
5284 would rather not keep them in memory, we handle all the
5285 relocations for a single input file at the same time.
5287 Unfortunately, there is no way to know the total number of local
5288 symbols until we have seen all of them, and the local symbol
5289 indices precede the global symbol indices. This means that when
5290 we are generating relocateable output, and we see a reloc against
5291 a global symbol, we can not know the symbol index until we have
5292 finished examining all the local symbols to see which ones we are
5293 going to output. To deal with this, we keep the relocations in
5294 memory, and don't output them until the end of the link. This is
5295 an unfortunate waste of memory, but I don't see a good way around
5296 it. Fortunately, it only happens when performing a relocateable
5297 link, which is not the common case. FIXME: If keep_memory is set
5298 we could write the relocs out and then read them again; I don't
5299 know how bad the memory loss will be. */
5301 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5302 sub
->output_has_begun
= false;
5303 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5305 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5307 if (p
->type
== bfd_indirect_link_order
5308 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
5309 == bfd_target_elf_flavour
)
5310 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
5312 if (! sub
->output_has_begun
)
5314 if (! elf_link_input_bfd (&finfo
, sub
))
5316 sub
->output_has_begun
= true;
5319 else if (p
->type
== bfd_section_reloc_link_order
5320 || p
->type
== bfd_symbol_reloc_link_order
)
5322 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
5327 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
5333 /* Output any global symbols that got converted to local in a
5334 version script or due to symbol visibility. We do this in a
5335 separate step since ELF requires all local symbols to appear
5336 prior to any global symbols. FIXME: We should only do this if
5337 some global symbols were, in fact, converted to become local.
5338 FIXME: Will this work correctly with the Irix 5 linker? */
5339 eoinfo
.failed
= false;
5340 eoinfo
.finfo
= &finfo
;
5341 eoinfo
.localsyms
= true;
5342 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5347 /* That wrote out all the local symbols. Finish up the symbol table
5348 with the global symbols. Even if we want to strip everything we
5349 can, we still need to deal with those global symbols that got
5350 converted to local in a version script. */
5352 /* The sh_info field records the index of the first non local symbol. */
5353 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
5356 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
5358 Elf_Internal_Sym sym
;
5359 Elf_External_Sym
*dynsym
=
5360 (Elf_External_Sym
*) finfo
.dynsym_sec
->contents
;
5361 long last_local
= 0;
5363 /* Write out the section symbols for the output sections. */
5370 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5373 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5376 Elf_External_Sym
*dest
;
5378 indx
= elf_section_data (s
)->this_idx
;
5379 BFD_ASSERT (indx
> 0);
5380 sym
.st_shndx
= indx
;
5381 sym
.st_value
= s
->vma
;
5382 dest
= dynsym
+ elf_section_data (s
)->dynindx
;
5383 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5386 last_local
= bfd_count_sections (abfd
);
5389 /* Write out the local dynsyms. */
5390 if (elf_hash_table (info
)->dynlocal
)
5392 struct elf_link_local_dynamic_entry
*e
;
5393 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
5396 Elf_External_Sym
*dest
;
5398 sym
.st_size
= e
->isym
.st_size
;
5399 sym
.st_other
= e
->isym
.st_other
;
5401 /* Copy the internal symbol as is.
5402 Note that we saved a word of storage and overwrote
5403 the original st_name with the dynstr_index. */
5406 if (e
->isym
.st_shndx
!= SHN_UNDEF
5407 && (e
->isym
.st_shndx
< SHN_LORESERVE
5408 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
5410 s
= bfd_section_from_elf_index (e
->input_bfd
,
5414 elf_section_data (s
->output_section
)->this_idx
;
5415 sym
.st_value
= (s
->output_section
->vma
5417 + e
->isym
.st_value
);
5420 if (last_local
< e
->dynindx
)
5421 last_local
= e
->dynindx
;
5423 dest
= dynsym
+ e
->dynindx
;
5424 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5428 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
5432 /* We get the global symbols from the hash table. */
5433 eoinfo
.failed
= false;
5434 eoinfo
.localsyms
= false;
5435 eoinfo
.finfo
= &finfo
;
5436 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5441 /* If backend needs to output some symbols not present in the hash
5442 table, do it now. */
5443 if (bed
->elf_backend_output_arch_syms
)
5445 typedef boolean (*out_sym_func
) PARAMS ((PTR
, const char *,
5449 if (! ((*bed
->elf_backend_output_arch_syms
)
5450 (abfd
, info
, (PTR
) &finfo
, (out_sym_func
) elf_link_output_sym
)))
5454 /* Flush all symbols to the file. */
5455 if (! elf_link_flush_output_syms (&finfo
))
5458 /* Now we know the size of the symtab section. */
5459 off
+= symtab_hdr
->sh_size
;
5461 /* Finish up and write out the symbol string table (.strtab)
5463 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5464 /* sh_name was set in prep_headers. */
5465 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5466 symstrtab_hdr
->sh_flags
= 0;
5467 symstrtab_hdr
->sh_addr
= 0;
5468 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
5469 symstrtab_hdr
->sh_entsize
= 0;
5470 symstrtab_hdr
->sh_link
= 0;
5471 symstrtab_hdr
->sh_info
= 0;
5472 /* sh_offset is set just below. */
5473 symstrtab_hdr
->sh_addralign
= 1;
5475 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, true);
5476 elf_tdata (abfd
)->next_file_pos
= off
;
5478 if (bfd_get_symcount (abfd
) > 0)
5480 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
5481 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
5485 /* Adjust the relocs to have the correct symbol indices. */
5486 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5488 if ((o
->flags
& SEC_RELOC
) == 0)
5491 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
5492 elf_section_data (o
)->rel_count
,
5493 elf_section_data (o
)->rel_hashes
);
5494 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
5495 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
5496 elf_section_data (o
)->rel_count2
,
5497 (elf_section_data (o
)->rel_hashes
5498 + elf_section_data (o
)->rel_count
));
5500 /* Set the reloc_count field to 0 to prevent write_relocs from
5501 trying to swap the relocs out itself. */
5505 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
5506 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
5508 /* If we are linking against a dynamic object, or generating a
5509 shared library, finish up the dynamic linking information. */
5512 Elf_External_Dyn
*dyncon
, *dynconend
;
5514 /* Fix up .dynamic entries. */
5515 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
5516 BFD_ASSERT (o
!= NULL
);
5518 dyncon
= (Elf_External_Dyn
*) o
->contents
;
5519 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
5520 for (; dyncon
< dynconend
; dyncon
++)
5522 Elf_Internal_Dyn dyn
;
5526 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
5533 if (relativecount
> 0 && dyncon
+ 1 < dynconend
)
5535 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
5537 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
5538 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
5541 if (dyn
.d_tag
!= DT_NULL
)
5543 dyn
.d_un
.d_val
= relativecount
;
5544 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5550 name
= info
->init_function
;
5553 name
= info
->fini_function
;
5556 struct elf_link_hash_entry
*h
;
5558 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
5559 false, false, true);
5561 && (h
->root
.type
== bfd_link_hash_defined
5562 || h
->root
.type
== bfd_link_hash_defweak
))
5564 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
5565 o
= h
->root
.u
.def
.section
;
5566 if (o
->output_section
!= NULL
)
5567 dyn
.d_un
.d_val
+= (o
->output_section
->vma
5568 + o
->output_offset
);
5571 /* The symbol is imported from another shared
5572 library and does not apply to this one. */
5576 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5581 case DT_PREINIT_ARRAYSZ
:
5582 name
= ".preinit_array";
5584 case DT_INIT_ARRAYSZ
:
5585 name
= ".init_array";
5587 case DT_FINI_ARRAYSZ
:
5588 name
= ".fini_array";
5590 o
= bfd_get_section_by_name (abfd
, name
);
5593 (*_bfd_error_handler
)
5594 (_("%s: could not find output section %s"),
5595 bfd_get_filename (abfd
), name
);
5598 if (o
->_raw_size
== 0)
5599 (*_bfd_error_handler
)
5600 (_("warning: %s section has zero size"), name
);
5601 dyn
.d_un
.d_val
= o
->_raw_size
;
5602 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5605 case DT_PREINIT_ARRAY
:
5606 name
= ".preinit_array";
5609 name
= ".init_array";
5612 name
= ".fini_array";
5625 name
= ".gnu.version_d";
5628 name
= ".gnu.version_r";
5631 name
= ".gnu.version";
5633 o
= bfd_get_section_by_name (abfd
, name
);
5636 (*_bfd_error_handler
)
5637 (_("%s: could not find output section %s"),
5638 bfd_get_filename (abfd
), name
);
5641 dyn
.d_un
.d_ptr
= o
->vma
;
5642 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5649 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
5654 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5656 Elf_Internal_Shdr
*hdr
;
5658 hdr
= elf_elfsections (abfd
)[i
];
5659 if (hdr
->sh_type
== type
5660 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
5662 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
5663 dyn
.d_un
.d_val
+= hdr
->sh_size
;
5666 if (dyn
.d_un
.d_val
== 0
5667 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
5668 dyn
.d_un
.d_val
= hdr
->sh_addr
;
5672 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5678 /* If we have created any dynamic sections, then output them. */
5681 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
5684 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
5686 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5687 || o
->_raw_size
== 0
5688 || o
->output_section
== bfd_abs_section_ptr
)
5690 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
5692 /* At this point, we are only interested in sections
5693 created by elf_link_create_dynamic_sections. */
5696 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
5698 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
5700 if (! bfd_set_section_contents (abfd
, o
->output_section
,
5702 (file_ptr
) o
->output_offset
,
5708 /* The contents of the .dynstr section are actually in a
5710 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
5711 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
5712 || ! _bfd_elf_strtab_emit (abfd
,
5713 elf_hash_table (info
)->dynstr
))
5719 if (info
->relocateable
)
5721 boolean failed
= false;
5723 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
5728 /* If we have optimized stabs strings, output them. */
5729 if (elf_hash_table (info
)->stab_info
!= NULL
)
5731 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
5735 if (info
->eh_frame_hdr
&& elf_hash_table (info
)->dynobj
)
5737 o
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
5740 && (elf_section_data (o
)->sec_info_type
5741 == ELF_INFO_TYPE_EH_FRAME_HDR
))
5743 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, o
))
5748 if (finfo
.symstrtab
!= NULL
)
5749 _bfd_stringtab_free (finfo
.symstrtab
);
5750 if (finfo
.contents
!= NULL
)
5751 free (finfo
.contents
);
5752 if (finfo
.external_relocs
!= NULL
)
5753 free (finfo
.external_relocs
);
5754 if (finfo
.internal_relocs
!= NULL
)
5755 free (finfo
.internal_relocs
);
5756 if (finfo
.external_syms
!= NULL
)
5757 free (finfo
.external_syms
);
5758 if (finfo
.locsym_shndx
!= NULL
)
5759 free (finfo
.locsym_shndx
);
5760 if (finfo
.internal_syms
!= NULL
)
5761 free (finfo
.internal_syms
);
5762 if (finfo
.indices
!= NULL
)
5763 free (finfo
.indices
);
5764 if (finfo
.sections
!= NULL
)
5765 free (finfo
.sections
);
5766 if (finfo
.symbuf
!= NULL
)
5767 free (finfo
.symbuf
);
5768 if (finfo
.symshndxbuf
!= NULL
)
5769 free (finfo
.symbuf
);
5770 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5772 if ((o
->flags
& SEC_RELOC
) != 0
5773 && elf_section_data (o
)->rel_hashes
!= NULL
)
5774 free (elf_section_data (o
)->rel_hashes
);
5777 elf_tdata (abfd
)->linker
= true;
5782 if (finfo
.symstrtab
!= NULL
)
5783 _bfd_stringtab_free (finfo
.symstrtab
);
5784 if (finfo
.contents
!= NULL
)
5785 free (finfo
.contents
);
5786 if (finfo
.external_relocs
!= NULL
)
5787 free (finfo
.external_relocs
);
5788 if (finfo
.internal_relocs
!= NULL
)
5789 free (finfo
.internal_relocs
);
5790 if (finfo
.external_syms
!= NULL
)
5791 free (finfo
.external_syms
);
5792 if (finfo
.locsym_shndx
!= NULL
)
5793 free (finfo
.locsym_shndx
);
5794 if (finfo
.internal_syms
!= NULL
)
5795 free (finfo
.internal_syms
);
5796 if (finfo
.indices
!= NULL
)
5797 free (finfo
.indices
);
5798 if (finfo
.sections
!= NULL
)
5799 free (finfo
.sections
);
5800 if (finfo
.symbuf
!= NULL
)
5801 free (finfo
.symbuf
);
5802 if (finfo
.symshndxbuf
!= NULL
)
5803 free (finfo
.symbuf
);
5804 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5806 if ((o
->flags
& SEC_RELOC
) != 0
5807 && elf_section_data (o
)->rel_hashes
!= NULL
)
5808 free (elf_section_data (o
)->rel_hashes
);
5814 /* Add a symbol to the output symbol table. */
5817 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
5818 struct elf_final_link_info
*finfo
;
5820 Elf_Internal_Sym
*elfsym
;
5821 asection
*input_sec
;
5823 Elf_External_Sym
*dest
;
5824 Elf_External_Sym_Shndx
*destshndx
;
5826 boolean (*output_symbol_hook
) PARAMS ((bfd
*,
5827 struct bfd_link_info
*info
,
5832 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
5833 elf_backend_link_output_symbol_hook
;
5834 if (output_symbol_hook
!= NULL
)
5836 if (! ((*output_symbol_hook
)
5837 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
5841 if (name
== (const char *) NULL
|| *name
== '\0')
5842 elfsym
->st_name
= 0;
5843 else if (input_sec
->flags
& SEC_EXCLUDE
)
5844 elfsym
->st_name
= 0;
5847 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5849 if (elfsym
->st_name
== (unsigned long) -1)
5853 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5855 if (! elf_link_flush_output_syms (finfo
))
5859 dest
= finfo
->symbuf
+ finfo
->symbuf_count
;
5860 destshndx
= finfo
->symshndxbuf
;
5861 if (destshndx
!= NULL
)
5862 destshndx
+= finfo
->symbuf_count
;
5863 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
, (PTR
) dest
, (PTR
) destshndx
);
5864 ++finfo
->symbuf_count
;
5866 ++ bfd_get_symcount (finfo
->output_bfd
);
5871 /* Flush the output symbols to the file. */
5874 elf_link_flush_output_syms (finfo
)
5875 struct elf_final_link_info
*finfo
;
5877 if (finfo
->symbuf_count
> 0)
5879 Elf_Internal_Shdr
*hdr
;
5883 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5884 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5885 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
5886 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5887 || bfd_bwrite ((PTR
) finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5890 hdr
->sh_size
+= amt
;
5892 if (finfo
->symshndxbuf
!= NULL
)
5894 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_shndx_hdr
;
5895 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5896 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym_Shndx
);
5897 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5898 || (bfd_bwrite ((PTR
) finfo
->symshndxbuf
, amt
, finfo
->output_bfd
)
5902 hdr
->sh_size
+= amt
;
5905 finfo
->symbuf_count
= 0;
5911 /* Adjust all external symbols pointing into SEC_MERGE sections
5912 to reflect the object merging within the sections. */
5915 elf_link_sec_merge_syms (h
, data
)
5916 struct elf_link_hash_entry
*h
;
5921 if (h
->root
.type
== bfd_link_hash_warning
)
5922 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5924 if ((h
->root
.type
== bfd_link_hash_defined
5925 || h
->root
.type
== bfd_link_hash_defweak
)
5926 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
5927 && elf_section_data (sec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
)
5929 bfd
*output_bfd
= (bfd
*) data
;
5931 h
->root
.u
.def
.value
=
5932 _bfd_merged_section_offset (output_bfd
,
5933 &h
->root
.u
.def
.section
,
5934 elf_section_data (sec
)->sec_info
,
5935 h
->root
.u
.def
.value
, (bfd_vma
) 0);
5941 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5942 allowing an unsatisfied unversioned symbol in the DSO to match a
5943 versioned symbol that would normally require an explicit version. */
5946 elf_link_check_versioned_symbol (info
, h
)
5947 struct bfd_link_info
*info
;
5948 struct elf_link_hash_entry
*h
;
5950 bfd
*undef_bfd
= h
->root
.u
.undef
.abfd
;
5951 struct elf_link_loaded_list
*loaded
;
5953 if ((undef_bfd
->flags
& DYNAMIC
) == 0
5954 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
5955 || elf_dt_soname (h
->root
.u
.undef
.abfd
) == NULL
)
5958 for (loaded
= elf_hash_table (info
)->loaded
;
5960 loaded
= loaded
->next
)
5963 Elf_Internal_Shdr
*hdr
;
5964 bfd_size_type symcount
;
5965 bfd_size_type extsymcount
;
5966 bfd_size_type extsymoff
;
5967 Elf_Internal_Shdr
*versymhdr
;
5968 Elf_Internal_Sym
*isym
;
5969 Elf_Internal_Sym
*isymend
;
5970 Elf_Internal_Sym
*isymbuf
;
5971 Elf_External_Versym
*ever
;
5972 Elf_External_Versym
*extversym
;
5974 input
= loaded
->abfd
;
5976 /* We check each DSO for a possible hidden versioned definition. */
5977 if (input
== undef_bfd
5978 || (input
->flags
& DYNAMIC
) == 0
5979 || elf_dynversym (input
) == 0)
5982 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
5984 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
5985 if (elf_bad_symtab (input
))
5987 extsymcount
= symcount
;
5992 extsymcount
= symcount
- hdr
->sh_info
;
5993 extsymoff
= hdr
->sh_info
;
5996 if (extsymcount
== 0)
5999 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6001 if (isymbuf
== NULL
)
6004 /* Read in any version definitions. */
6005 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6006 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
6007 if (extversym
== NULL
)
6010 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6011 || (bfd_bread ((PTR
) extversym
, versymhdr
->sh_size
, input
)
6012 != versymhdr
->sh_size
))
6020 ever
= extversym
+ extsymoff
;
6021 isymend
= isymbuf
+ extsymcount
;
6022 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6025 Elf_Internal_Versym iver
;
6027 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6028 || isym
->st_shndx
== SHN_UNDEF
)
6031 name
= bfd_elf_string_from_elf_section (input
,
6034 if (strcmp (name
, h
->root
.root
.string
) != 0)
6037 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6039 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6041 /* If we have a non-hidden versioned sym, then it should
6042 have provided a definition for the undefined sym. */
6046 if ((iver
.vs_vers
& VERSYM_VERSION
) == 2)
6048 /* This is the oldest (default) sym. We can use it. */
6062 /* Add an external symbol to the symbol table. This is called from
6063 the hash table traversal routine. When generating a shared object,
6064 we go through the symbol table twice. The first time we output
6065 anything that might have been forced to local scope in a version
6066 script. The second time we output the symbols that are still
6070 elf_link_output_extsym (h
, data
)
6071 struct elf_link_hash_entry
*h
;
6074 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
6075 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6077 Elf_Internal_Sym sym
;
6078 asection
*input_sec
;
6080 if (h
->root
.type
== bfd_link_hash_warning
)
6082 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6083 if (h
->root
.type
== bfd_link_hash_new
)
6087 /* Decide whether to output this symbol in this pass. */
6088 if (eoinfo
->localsyms
)
6090 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6095 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6099 /* If we are not creating a shared library, and this symbol is
6100 referenced by a shared library but is not defined anywhere, then
6101 warn that it is undefined. If we do not do this, the runtime
6102 linker will complain that the symbol is undefined when the
6103 program is run. We don't have to worry about symbols that are
6104 referenced by regular files, because we will already have issued
6105 warnings for them. */
6106 if (! finfo
->info
->relocateable
6107 && ! finfo
->info
->allow_shlib_undefined
6108 && ! finfo
->info
->shared
6109 && h
->root
.type
== bfd_link_hash_undefined
6110 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
6111 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
6112 && ! elf_link_check_versioned_symbol (finfo
->info
, h
))
6114 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6115 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6116 (asection
*) NULL
, (bfd_vma
) 0, true)))
6118 eoinfo
->failed
= true;
6123 /* We don't want to output symbols that have never been mentioned by
6124 a regular file, or that we have been told to strip. However, if
6125 h->indx is set to -2, the symbol is used by a reloc and we must
6129 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
6130 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
6131 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
6132 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
6134 else if (finfo
->info
->strip
== strip_all
6135 || (finfo
->info
->strip
== strip_some
6136 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6137 h
->root
.root
.string
,
6138 false, false) == NULL
))
6143 /* If we're stripping it, and it's not a dynamic symbol, there's
6144 nothing else to do unless it is a forced local symbol. */
6147 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6151 sym
.st_size
= h
->size
;
6152 sym
.st_other
= h
->other
;
6153 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6154 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6155 else if (h
->root
.type
== bfd_link_hash_undefweak
6156 || h
->root
.type
== bfd_link_hash_defweak
)
6157 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6159 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6161 switch (h
->root
.type
)
6164 case bfd_link_hash_new
:
6165 case bfd_link_hash_warning
:
6169 case bfd_link_hash_undefined
:
6170 case bfd_link_hash_undefweak
:
6171 input_sec
= bfd_und_section_ptr
;
6172 sym
.st_shndx
= SHN_UNDEF
;
6175 case bfd_link_hash_defined
:
6176 case bfd_link_hash_defweak
:
6178 input_sec
= h
->root
.u
.def
.section
;
6179 if (input_sec
->output_section
!= NULL
)
6182 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6183 input_sec
->output_section
);
6184 if (sym
.st_shndx
== SHN_BAD
)
6186 (*_bfd_error_handler
)
6187 (_("%s: could not find output section %s for input section %s"),
6188 bfd_get_filename (finfo
->output_bfd
),
6189 input_sec
->output_section
->name
,
6191 eoinfo
->failed
= true;
6195 /* ELF symbols in relocateable files are section relative,
6196 but in nonrelocateable files they are virtual
6198 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6199 if (! finfo
->info
->relocateable
)
6201 sym
.st_value
+= input_sec
->output_section
->vma
;
6202 if (h
->type
== STT_TLS
)
6204 /* STT_TLS symbols are relative to PT_TLS segment
6206 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6207 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
6213 BFD_ASSERT (input_sec
->owner
== NULL
6214 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6215 sym
.st_shndx
= SHN_UNDEF
;
6216 input_sec
= bfd_und_section_ptr
;
6221 case bfd_link_hash_common
:
6222 input_sec
= h
->root
.u
.c
.p
->section
;
6223 sym
.st_shndx
= SHN_COMMON
;
6224 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6227 case bfd_link_hash_indirect
:
6228 /* These symbols are created by symbol versioning. They point
6229 to the decorated version of the name. For example, if the
6230 symbol foo@@GNU_1.2 is the default, which should be used when
6231 foo is used with no version, then we add an indirect symbol
6232 foo which points to foo@@GNU_1.2. We ignore these symbols,
6233 since the indirected symbol is already in the hash table. */
6237 /* Give the processor backend a chance to tweak the symbol value,
6238 and also to finish up anything that needs to be done for this
6239 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6240 forced local syms when non-shared is due to a historical quirk. */
6241 if ((h
->dynindx
!= -1
6242 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6243 && (finfo
->info
->shared
6244 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6245 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6247 struct elf_backend_data
*bed
;
6249 bed
= get_elf_backend_data (finfo
->output_bfd
);
6250 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6251 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6253 eoinfo
->failed
= true;
6258 /* If we are marking the symbol as undefined, and there are no
6259 non-weak references to this symbol from a regular object, then
6260 mark the symbol as weak undefined; if there are non-weak
6261 references, mark the symbol as strong. We can't do this earlier,
6262 because it might not be marked as undefined until the
6263 finish_dynamic_symbol routine gets through with it. */
6264 if (sym
.st_shndx
== SHN_UNDEF
6265 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
6266 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6267 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6271 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
6272 bindtype
= STB_GLOBAL
;
6274 bindtype
= STB_WEAK
;
6275 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6278 /* If a symbol is not defined locally, we clear the visibility
6280 if (! finfo
->info
->relocateable
6281 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6282 sym
.st_other
^= ELF_ST_VISIBILITY (sym
.st_other
);
6284 /* If this symbol should be put in the .dynsym section, then put it
6285 there now. We already know the symbol index. We also fill in
6286 the entry in the .hash section. */
6287 if (h
->dynindx
!= -1
6288 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6292 size_t hash_entry_size
;
6293 bfd_byte
*bucketpos
;
6295 Elf_External_Sym
*esym
;
6297 sym
.st_name
= h
->dynstr_index
;
6298 esym
= (Elf_External_Sym
*) finfo
->dynsym_sec
->contents
+ h
->dynindx
;
6299 elf_swap_symbol_out (finfo
->output_bfd
, &sym
, (PTR
) esym
, (PTR
) 0);
6301 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6302 bucket
= h
->elf_hash_value
% bucketcount
;
6304 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6305 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6306 + (bucket
+ 2) * hash_entry_size
);
6307 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6308 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, (bfd_vma
) h
->dynindx
,
6310 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6311 ((bfd_byte
*) finfo
->hash_sec
->contents
6312 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6314 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6316 Elf_Internal_Versym iversym
;
6317 Elf_External_Versym
*eversym
;
6319 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6321 if (h
->verinfo
.verdef
== NULL
)
6322 iversym
.vs_vers
= 0;
6324 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6328 if (h
->verinfo
.vertree
== NULL
)
6329 iversym
.vs_vers
= 1;
6331 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6334 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
6335 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6337 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6338 eversym
+= h
->dynindx
;
6339 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6343 /* If we're stripping it, then it was just a dynamic symbol, and
6344 there's nothing else to do. */
6348 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6350 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
6352 eoinfo
->failed
= true;
6359 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6360 originated from the section given by INPUT_REL_HDR) to the
6364 elf_link_output_relocs (output_bfd
, input_section
, input_rel_hdr
,
6367 asection
*input_section
;
6368 Elf_Internal_Shdr
*input_rel_hdr
;
6369 Elf_Internal_Rela
*internal_relocs
;
6371 Elf_Internal_Rela
*irela
;
6372 Elf_Internal_Rela
*irelaend
;
6373 Elf_Internal_Shdr
*output_rel_hdr
;
6374 asection
*output_section
;
6375 unsigned int *rel_countp
= NULL
;
6376 struct elf_backend_data
*bed
;
6379 output_section
= input_section
->output_section
;
6380 output_rel_hdr
= NULL
;
6382 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
6383 == input_rel_hdr
->sh_entsize
)
6385 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
6386 rel_countp
= &elf_section_data (output_section
)->rel_count
;
6388 else if (elf_section_data (output_section
)->rel_hdr2
6389 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
6390 == input_rel_hdr
->sh_entsize
))
6392 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
6393 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
6397 (*_bfd_error_handler
)
6398 (_("%s: relocation size mismatch in %s section %s"),
6399 bfd_get_filename (output_bfd
),
6400 bfd_archive_filename (input_section
->owner
),
6401 input_section
->name
);
6402 bfd_set_error (bfd_error_wrong_object_format
);
6406 bed
= get_elf_backend_data (output_bfd
);
6407 irela
= internal_relocs
;
6408 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
6409 * bed
->s
->int_rels_per_ext_rel
);
6411 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
6413 Elf_External_Rel
*erel
;
6414 Elf_Internal_Rel
*irel
;
6416 amt
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
6417 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (amt
);
6420 (*_bfd_error_handler
) (_("Error: out of memory"));
6424 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
+ *rel_countp
);
6425 for (; irela
< irelaend
; irela
+= bed
->s
->int_rels_per_ext_rel
, erel
++)
6429 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
6431 irel
[i
].r_offset
= irela
[i
].r_offset
;
6432 irel
[i
].r_info
= irela
[i
].r_info
;
6433 BFD_ASSERT (irela
[i
].r_addend
== 0);
6436 if (bed
->s
->swap_reloc_out
)
6437 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, (PTR
) erel
);
6439 elf_swap_reloc_out (output_bfd
, irel
, erel
);
6446 Elf_External_Rela
*erela
;
6448 BFD_ASSERT (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
));
6450 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
+ *rel_countp
);
6451 for (; irela
< irelaend
; irela
+= bed
->s
->int_rels_per_ext_rel
, erela
++)
6452 if (bed
->s
->swap_reloca_out
)
6453 (*bed
->s
->swap_reloca_out
) (output_bfd
, irela
, (PTR
) erela
);
6455 elf_swap_reloca_out (output_bfd
, irela
, erela
);
6458 /* Bump the counter, so that we know where to add the next set of
6460 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
6465 /* Link an input file into the linker output file. This function
6466 handles all the sections and relocations of the input file at once.
6467 This is so that we only have to read the local symbols once, and
6468 don't have to keep them in memory. */
6471 elf_link_input_bfd (finfo
, input_bfd
)
6472 struct elf_final_link_info
*finfo
;
6475 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
6476 bfd
*, asection
*, bfd_byte
*,
6477 Elf_Internal_Rela
*,
6478 Elf_Internal_Sym
*, asection
**));
6480 Elf_Internal_Shdr
*symtab_hdr
;
6483 Elf_Internal_Sym
*isymbuf
;
6484 Elf_Internal_Sym
*isym
;
6485 Elf_Internal_Sym
*isymend
;
6487 asection
**ppsection
;
6489 struct elf_backend_data
*bed
;
6490 boolean emit_relocs
;
6491 struct elf_link_hash_entry
**sym_hashes
;
6493 output_bfd
= finfo
->output_bfd
;
6494 bed
= get_elf_backend_data (output_bfd
);
6495 relocate_section
= bed
->elf_backend_relocate_section
;
6497 /* If this is a dynamic object, we don't want to do anything here:
6498 we don't want the local symbols, and we don't want the section
6500 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6503 emit_relocs
= (finfo
->info
->relocateable
6504 || finfo
->info
->emitrelocations
6505 || bed
->elf_backend_emit_relocs
);
6507 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6508 if (elf_bad_symtab (input_bfd
))
6510 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6515 locsymcount
= symtab_hdr
->sh_info
;
6516 extsymoff
= symtab_hdr
->sh_info
;
6519 /* Read the local symbols. */
6520 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6521 if (isymbuf
== NULL
&& locsymcount
!= 0)
6523 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6524 finfo
->internal_syms
,
6525 finfo
->external_syms
,
6526 finfo
->locsym_shndx
);
6527 if (isymbuf
== NULL
)
6531 /* Find local symbol sections and adjust values of symbols in
6532 SEC_MERGE sections. Write out those local symbols we know are
6533 going into the output file. */
6534 isymend
= isymbuf
+ locsymcount
;
6535 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6537 isym
++, pindex
++, ppsection
++)
6541 Elf_Internal_Sym osym
;
6545 if (elf_bad_symtab (input_bfd
))
6547 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6554 if (isym
->st_shndx
== SHN_UNDEF
)
6555 isec
= bfd_und_section_ptr
;
6556 else if (isym
->st_shndx
< SHN_LORESERVE
6557 || isym
->st_shndx
> SHN_HIRESERVE
)
6559 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
6561 && elf_section_data (isec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
6562 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6564 _bfd_merged_section_offset (output_bfd
, &isec
,
6565 elf_section_data (isec
)->sec_info
,
6566 isym
->st_value
, (bfd_vma
) 0);
6568 else if (isym
->st_shndx
== SHN_ABS
)
6569 isec
= bfd_abs_section_ptr
;
6570 else if (isym
->st_shndx
== SHN_COMMON
)
6571 isec
= bfd_com_section_ptr
;
6580 /* Don't output the first, undefined, symbol. */
6581 if (ppsection
== finfo
->sections
)
6584 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6586 /* We never output section symbols. Instead, we use the
6587 section symbol of the corresponding section in the output
6592 /* If we are stripping all symbols, we don't want to output this
6594 if (finfo
->info
->strip
== strip_all
)
6597 /* If we are discarding all local symbols, we don't want to
6598 output this one. If we are generating a relocateable output
6599 file, then some of the local symbols may be required by
6600 relocs; we output them below as we discover that they are
6602 if (finfo
->info
->discard
== discard_all
)
6605 /* If this symbol is defined in a section which we are
6606 discarding, we don't need to keep it, but note that
6607 linker_mark is only reliable for sections that have contents.
6608 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6609 as well as linker_mark. */
6610 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6612 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6613 || (! finfo
->info
->relocateable
6614 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6617 /* Get the name of the symbol. */
6618 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6623 /* See if we are discarding symbols with this name. */
6624 if ((finfo
->info
->strip
== strip_some
6625 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
6627 || (((finfo
->info
->discard
== discard_sec_merge
6628 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocateable
)
6629 || finfo
->info
->discard
== discard_l
)
6630 && bfd_is_local_label_name (input_bfd
, name
)))
6633 /* If we get here, we are going to output this symbol. */
6637 /* Adjust the section index for the output file. */
6638 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6639 isec
->output_section
);
6640 if (osym
.st_shndx
== SHN_BAD
)
6643 *pindex
= bfd_get_symcount (output_bfd
);
6645 /* ELF symbols in relocateable files are section relative, but
6646 in executable files they are virtual addresses. Note that
6647 this code assumes that all ELF sections have an associated
6648 BFD section with a reasonable value for output_offset; below
6649 we assume that they also have a reasonable value for
6650 output_section. Any special sections must be set up to meet
6651 these requirements. */
6652 osym
.st_value
+= isec
->output_offset
;
6653 if (! finfo
->info
->relocateable
)
6655 osym
.st_value
+= isec
->output_section
->vma
;
6656 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6658 /* STT_TLS symbols are relative to PT_TLS segment base. */
6659 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6660 osym
.st_value
-= finfo
->first_tls_sec
->vma
;
6664 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
6668 /* Relocate the contents of each section. */
6669 sym_hashes
= elf_sym_hashes (input_bfd
);
6670 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6674 if (! o
->linker_mark
)
6676 /* This section was omitted from the link. */
6680 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6681 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6684 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6686 /* Section was created by elf_link_create_dynamic_sections
6691 /* Get the contents of the section. They have been cached by a
6692 relaxation routine. Note that o is a section in an input
6693 file, so the contents field will not have been set by any of
6694 the routines which work on output files. */
6695 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6696 contents
= elf_section_data (o
)->this_hdr
.contents
;
6699 contents
= finfo
->contents
;
6700 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
6701 (file_ptr
) 0, o
->_raw_size
))
6705 if ((o
->flags
& SEC_RELOC
) != 0)
6707 Elf_Internal_Rela
*internal_relocs
;
6709 /* Get the swapped relocs. */
6710 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
6711 (input_bfd
, o
, finfo
->external_relocs
,
6712 finfo
->internal_relocs
, false));
6713 if (internal_relocs
== NULL
6714 && o
->reloc_count
> 0)
6717 /* Run through the relocs looking for any against symbols
6718 from discarded sections and section symbols from
6719 removed link-once sections. Complain about relocs
6720 against discarded sections. Zero relocs against removed
6721 link-once sections. We should really complain if
6722 anything in the final link tries to use it, but
6723 DWARF-based exception handling might have an entry in
6724 .eh_frame to describe a routine in the linkonce section,
6725 and it turns out to be hard to remove the .eh_frame
6726 entry too. FIXME. */
6727 if (!finfo
->info
->relocateable
6728 && !elf_section_ignore_discarded_relocs (o
))
6730 Elf_Internal_Rela
*rel
, *relend
;
6732 rel
= internal_relocs
;
6733 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6734 for ( ; rel
< relend
; rel
++)
6736 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
6738 if (r_symndx
>= locsymcount
6739 || (elf_bad_symtab (input_bfd
)
6740 && finfo
->sections
[r_symndx
] == NULL
))
6742 struct elf_link_hash_entry
*h
;
6744 h
= sym_hashes
[r_symndx
- extsymoff
];
6745 while (h
->root
.type
== bfd_link_hash_indirect
6746 || h
->root
.type
== bfd_link_hash_warning
)
6747 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6749 /* Complain if the definition comes from a
6750 discarded section. */
6751 if ((h
->root
.type
== bfd_link_hash_defined
6752 || h
->root
.type
== bfd_link_hash_defweak
)
6753 && elf_discarded_section (h
->root
.u
.def
.section
))
6755 #if BFD_VERSION_DATE < 20031005
6756 if ((o
->flags
& SEC_DEBUGGING
) != 0)
6758 #if BFD_VERSION_DATE > 20021005
6759 (*finfo
->info
->callbacks
->warning
)
6761 _("warning: relocation against removed section; zeroing"),
6762 NULL
, input_bfd
, o
, rel
->r_offset
);
6764 BFD_ASSERT (r_symndx
!= 0);
6765 memset (rel
, 0, sizeof (*rel
));
6770 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6771 (finfo
->info
, h
->root
.root
.string
,
6772 input_bfd
, o
, rel
->r_offset
,
6780 asection
*sec
= finfo
->sections
[r_symndx
];
6782 if (sec
!= NULL
&& elf_discarded_section (sec
))
6784 #if BFD_VERSION_DATE < 20031005
6785 if ((o
->flags
& SEC_DEBUGGING
) != 0
6786 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
6788 #if BFD_VERSION_DATE > 20021005
6789 (*finfo
->info
->callbacks
->warning
)
6791 _("warning: relocation against removed section"),
6792 NULL
, input_bfd
, o
, rel
->r_offset
);
6794 BFD_ASSERT (r_symndx
!= 0);
6796 = ELF_R_INFO (0, ELF_R_TYPE (rel
->r_info
));
6804 = _("local symbols in discarded section %s");
6806 = strlen (sec
->name
) + strlen (msg
) - 1;
6807 char *buf
= (char *) bfd_malloc (amt
);
6810 sprintf (buf
, msg
, sec
->name
);
6812 buf
= (char *) sec
->name
;
6813 ok
= (*finfo
->info
->callbacks
6814 ->undefined_symbol
) (finfo
->info
, buf
,
6818 if (buf
!= sec
->name
)
6828 /* Relocate the section by invoking a back end routine.
6830 The back end routine is responsible for adjusting the
6831 section contents as necessary, and (if using Rela relocs
6832 and generating a relocateable output file) adjusting the
6833 reloc addend as necessary.
6835 The back end routine does not have to worry about setting
6836 the reloc address or the reloc symbol index.
6838 The back end routine is given a pointer to the swapped in
6839 internal symbols, and can access the hash table entries
6840 for the external symbols via elf_sym_hashes (input_bfd).
6842 When generating relocateable output, the back end routine
6843 must handle STB_LOCAL/STT_SECTION symbols specially. The
6844 output symbol is going to be a section symbol
6845 corresponding to the output section, which will require
6846 the addend to be adjusted. */
6848 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6849 input_bfd
, o
, contents
,
6857 Elf_Internal_Rela
*irela
;
6858 Elf_Internal_Rela
*irelaend
;
6859 struct elf_link_hash_entry
**rel_hash
;
6860 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6861 unsigned int next_erel
;
6862 boolean (*reloc_emitter
) PARAMS ((bfd
*, asection
*,
6863 Elf_Internal_Shdr
*,
6864 Elf_Internal_Rela
*));
6865 boolean rela_normal
;
6867 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6868 rela_normal
= (bed
->rela_normal
6869 && (input_rel_hdr
->sh_entsize
6870 == sizeof (Elf_External_Rela
)));
6872 /* Adjust the reloc addresses and symbol indices. */
6874 irela
= internal_relocs
;
6875 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6876 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6877 + elf_section_data (o
->output_section
)->rel_count
6878 + elf_section_data (o
->output_section
)->rel_count2
);
6879 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6881 unsigned long r_symndx
;
6883 Elf_Internal_Sym sym
;
6885 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6891 irela
->r_offset
+= o
->output_offset
;
6893 /* Relocs in an executable have to be virtual addresses. */
6894 if (!finfo
->info
->relocateable
)
6895 irela
->r_offset
+= o
->output_section
->vma
;
6897 r_symndx
= ELF_R_SYM (irela
->r_info
);
6902 if (r_symndx
>= locsymcount
6903 || (elf_bad_symtab (input_bfd
)
6904 && finfo
->sections
[r_symndx
] == NULL
))
6906 struct elf_link_hash_entry
*rh
;
6909 /* This is a reloc against a global symbol. We
6910 have not yet output all the local symbols, so
6911 we do not know the symbol index of any global
6912 symbol. We set the rel_hash entry for this
6913 reloc to point to the global hash table entry
6914 for this symbol. The symbol index is then
6915 set at the end of elf_bfd_final_link. */
6916 indx
= r_symndx
- extsymoff
;
6917 rh
= elf_sym_hashes (input_bfd
)[indx
];
6918 while (rh
->root
.type
== bfd_link_hash_indirect
6919 || rh
->root
.type
== bfd_link_hash_warning
)
6920 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6922 /* Setting the index to -2 tells
6923 elf_link_output_extsym that this symbol is
6925 BFD_ASSERT (rh
->indx
< 0);
6933 /* This is a reloc against a local symbol. */
6936 sym
= isymbuf
[r_symndx
];
6937 sec
= finfo
->sections
[r_symndx
];
6938 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
6940 /* I suppose the backend ought to fill in the
6941 section of any STT_SECTION symbol against a
6942 processor specific section. If we have
6943 discarded a section, the output_section will
6944 be the absolute section. */
6945 if (bfd_is_abs_section (sec
)
6947 && bfd_is_abs_section (sec
->output_section
)))
6949 else if (sec
== NULL
|| sec
->owner
== NULL
)
6951 bfd_set_error (bfd_error_bad_value
);
6956 r_symndx
= sec
->output_section
->target_index
;
6957 BFD_ASSERT (r_symndx
!= 0);
6960 /* Adjust the addend according to where the
6961 section winds up in the output section. */
6963 irela
->r_addend
+= sec
->output_offset
;
6967 if (finfo
->indices
[r_symndx
] == -1)
6969 unsigned long shlink
;
6973 if (finfo
->info
->strip
== strip_all
)
6975 /* You can't do ld -r -s. */
6976 bfd_set_error (bfd_error_invalid_operation
);
6980 /* This symbol was skipped earlier, but
6981 since it is needed by a reloc, we
6982 must output it now. */
6983 shlink
= symtab_hdr
->sh_link
;
6984 name
= (bfd_elf_string_from_elf_section
6985 (input_bfd
, shlink
, sym
.st_name
));
6989 osec
= sec
->output_section
;
6991 _bfd_elf_section_from_bfd_section (output_bfd
,
6993 if (sym
.st_shndx
== SHN_BAD
)
6996 sym
.st_value
+= sec
->output_offset
;
6997 if (! finfo
->info
->relocateable
)
6999 sym
.st_value
+= osec
->vma
;
7000 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7002 /* STT_TLS symbols are relative to PT_TLS
7004 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
7005 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
7009 finfo
->indices
[r_symndx
]
7010 = bfd_get_symcount (output_bfd
);
7012 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
))
7016 r_symndx
= finfo
->indices
[r_symndx
];
7019 irela
->r_info
= ELF_R_INFO (r_symndx
,
7020 ELF_R_TYPE (irela
->r_info
));
7023 /* Swap out the relocs. */
7024 if (bed
->elf_backend_emit_relocs
7025 && !(finfo
->info
->relocateable
7026 || finfo
->info
->emitrelocations
))
7027 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7029 reloc_emitter
= elf_link_output_relocs
;
7031 if (input_rel_hdr
->sh_size
!= 0
7032 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7036 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7037 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7039 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7040 * bed
->s
->int_rels_per_ext_rel
);
7041 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
7048 /* Write out the modified section contents. */
7049 if (bed
->elf_backend_write_section
7050 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7052 /* Section written out. */
7054 else switch (elf_section_data (o
)->sec_info_type
)
7056 case ELF_INFO_TYPE_STABS
:
7057 if (! (_bfd_write_section_stabs
7059 &elf_hash_table (finfo
->info
)->stab_info
,
7060 o
, &elf_section_data (o
)->sec_info
, contents
)))
7063 case ELF_INFO_TYPE_MERGE
:
7064 if (! (_bfd_write_merged_section
7065 (output_bfd
, o
, elf_section_data (o
)->sec_info
)))
7068 case ELF_INFO_TYPE_EH_FRAME
:
7073 = bfd_get_section_by_name (elf_hash_table (finfo
->info
)->dynobj
,
7075 if (! (_bfd_elf_write_section_eh_frame (output_bfd
, o
, ehdrsec
,
7082 bfd_size_type sec_size
;
7084 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
7085 if (! (o
->flags
& SEC_EXCLUDE
)
7086 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7088 (file_ptr
) o
->output_offset
,
7099 /* Generate a reloc when linking an ELF file. This is a reloc
7100 requested by the linker, and does come from any input file. This
7101 is used to build constructor and destructor tables when linking
7105 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
7107 struct bfd_link_info
*info
;
7108 asection
*output_section
;
7109 struct bfd_link_order
*link_order
;
7111 reloc_howto_type
*howto
;
7115 struct elf_link_hash_entry
**rel_hash_ptr
;
7116 Elf_Internal_Shdr
*rel_hdr
;
7117 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7119 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7122 bfd_set_error (bfd_error_bad_value
);
7126 addend
= link_order
->u
.reloc
.p
->addend
;
7128 /* Figure out the symbol index. */
7129 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7130 + elf_section_data (output_section
)->rel_count
7131 + elf_section_data (output_section
)->rel_count2
);
7132 if (link_order
->type
== bfd_section_reloc_link_order
)
7134 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7135 BFD_ASSERT (indx
!= 0);
7136 *rel_hash_ptr
= NULL
;
7140 struct elf_link_hash_entry
*h
;
7142 /* Treat a reloc against a defined symbol as though it were
7143 actually against the section. */
7144 h
= ((struct elf_link_hash_entry
*)
7145 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7146 link_order
->u
.reloc
.p
->u
.name
,
7147 false, false, true));
7149 && (h
->root
.type
== bfd_link_hash_defined
7150 || h
->root
.type
== bfd_link_hash_defweak
))
7154 section
= h
->root
.u
.def
.section
;
7155 indx
= section
->output_section
->target_index
;
7156 *rel_hash_ptr
= NULL
;
7157 /* It seems that we ought to add the symbol value to the
7158 addend here, but in practice it has already been added
7159 because it was passed to constructor_callback. */
7160 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7164 /* Setting the index to -2 tells elf_link_output_extsym that
7165 this symbol is used by a reloc. */
7172 if (! ((*info
->callbacks
->unattached_reloc
)
7173 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
7174 (asection
*) NULL
, (bfd_vma
) 0)))
7180 /* If this is an inplace reloc, we must write the addend into the
7182 if (howto
->partial_inplace
&& addend
!= 0)
7185 bfd_reloc_status_type rstat
;
7188 const char *sym_name
;
7190 size
= bfd_get_reloc_size (howto
);
7191 buf
= (bfd_byte
*) bfd_zmalloc (size
);
7192 if (buf
== (bfd_byte
*) NULL
)
7194 rstat
= _bfd_relocate_contents (howto
, output_bfd
, (bfd_vma
) addend
, buf
);
7201 case bfd_reloc_outofrange
:
7204 case bfd_reloc_overflow
:
7205 if (link_order
->type
== bfd_section_reloc_link_order
)
7206 sym_name
= bfd_section_name (output_bfd
,
7207 link_order
->u
.reloc
.p
->u
.section
);
7209 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7210 if (! ((*info
->callbacks
->reloc_overflow
)
7211 (info
, sym_name
, howto
->name
, addend
,
7212 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
7219 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
7220 (file_ptr
) link_order
->offset
, size
);
7226 /* The address of a reloc is relative to the section in a
7227 relocateable file, and is a virtual address in an executable
7229 offset
= link_order
->offset
;
7230 if (! info
->relocateable
)
7231 offset
+= output_section
->vma
;
7233 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7235 if (rel_hdr
->sh_type
== SHT_REL
)
7238 Elf_Internal_Rel
*irel
;
7239 Elf_External_Rel
*erel
;
7242 size
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
7243 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (size
);
7247 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7248 irel
[i
].r_offset
= offset
;
7249 irel
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
7251 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
7252 + elf_section_data (output_section
)->rel_count
);
7254 if (bed
->s
->swap_reloc_out
)
7255 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, (bfd_byte
*) erel
);
7257 elf_swap_reloc_out (output_bfd
, irel
, erel
);
7264 Elf_Internal_Rela
*irela
;
7265 Elf_External_Rela
*erela
;
7268 size
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
7269 irela
= (Elf_Internal_Rela
*) bfd_zmalloc (size
);
7273 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7274 irela
[i
].r_offset
= offset
;
7275 irela
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
7276 irela
[0].r_addend
= addend
;
7278 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
7279 + elf_section_data (output_section
)->rel_count
);
7281 if (bed
->s
->swap_reloca_out
)
7282 (*bed
->s
->swap_reloca_out
) (output_bfd
, irela
, (bfd_byte
*) erela
);
7284 elf_swap_reloca_out (output_bfd
, irela
, erela
);
7287 ++elf_section_data (output_section
)->rel_count
;
7292 /* Allocate a pointer to live in a linker created section. */
7295 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
7297 struct bfd_link_info
*info
;
7298 elf_linker_section_t
*lsect
;
7299 struct elf_link_hash_entry
*h
;
7300 const Elf_Internal_Rela
*rel
;
7302 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
7303 elf_linker_section_pointers_t
*linker_section_ptr
;
7304 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7307 BFD_ASSERT (lsect
!= NULL
);
7309 /* Is this a global symbol? */
7312 /* Has this symbol already been allocated? If so, our work is done. */
7313 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
7318 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
7319 /* Make sure this symbol is output as a dynamic symbol. */
7320 if (h
->dynindx
== -1)
7322 if (! elf_link_record_dynamic_symbol (info
, h
))
7326 if (lsect
->rel_section
)
7327 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7331 /* Allocation of a pointer to a local symbol. */
7332 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
7334 /* Allocate a table to hold the local symbols if first time. */
7337 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
7338 register unsigned int i
;
7341 amt
*= sizeof (elf_linker_section_pointers_t
*);
7342 ptr
= (elf_linker_section_pointers_t
**) bfd_alloc (abfd
, amt
);
7347 elf_local_ptr_offsets (abfd
) = ptr
;
7348 for (i
= 0; i
< num_symbols
; i
++)
7349 ptr
[i
] = (elf_linker_section_pointers_t
*) 0;
7352 /* Has this symbol already been allocated? If so, our work is done. */
7353 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
7358 ptr_linker_section_ptr
= &ptr
[r_symndx
];
7362 /* If we are generating a shared object, we need to
7363 output a R_<xxx>_RELATIVE reloc so that the
7364 dynamic linker can adjust this GOT entry. */
7365 BFD_ASSERT (lsect
->rel_section
!= NULL
);
7366 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7370 /* Allocate space for a pointer in the linker section, and allocate
7371 a new pointer record from internal memory. */
7372 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
7373 amt
= sizeof (elf_linker_section_pointers_t
);
7374 linker_section_ptr
= (elf_linker_section_pointers_t
*) bfd_alloc (abfd
, amt
);
7376 if (!linker_section_ptr
)
7379 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
7380 linker_section_ptr
->addend
= rel
->r_addend
;
7381 linker_section_ptr
->which
= lsect
->which
;
7382 linker_section_ptr
->written_address_p
= false;
7383 *ptr_linker_section_ptr
= linker_section_ptr
;
7386 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
7388 linker_section_ptr
->offset
= (lsect
->section
->_raw_size
7389 - lsect
->hole_size
+ (ARCH_SIZE
/ 8));
7390 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
7391 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
7392 if (lsect
->sym_hash
)
7394 /* Bump up symbol value if needed. */
7395 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
7397 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
7398 lsect
->sym_hash
->root
.root
.string
,
7399 (long) ARCH_SIZE
/ 8,
7400 (long) lsect
->sym_hash
->root
.u
.def
.value
);
7406 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
7408 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
7412 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7413 lsect
->name
, (long) linker_section_ptr
->offset
,
7414 (long) lsect
->section
->_raw_size
);
7421 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7424 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7427 /* Fill in the address for a pointer generated in a linker section. */
7430 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
,
7431 relocation
, rel
, relative_reloc
)
7434 struct bfd_link_info
*info
;
7435 elf_linker_section_t
*lsect
;
7436 struct elf_link_hash_entry
*h
;
7438 const Elf_Internal_Rela
*rel
;
7441 elf_linker_section_pointers_t
*linker_section_ptr
;
7443 BFD_ASSERT (lsect
!= NULL
);
7447 /* Handle global symbol. */
7448 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7449 (h
->linker_section_pointer
,
7453 BFD_ASSERT (linker_section_ptr
!= NULL
);
7455 if (! elf_hash_table (info
)->dynamic_sections_created
7458 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
7460 /* This is actually a static link, or it is a
7461 -Bsymbolic link and the symbol is defined
7462 locally. We must initialize this entry in the
7465 When doing a dynamic link, we create a .rela.<xxx>
7466 relocation entry to initialize the value. This
7467 is done in the finish_dynamic_symbol routine. */
7468 if (!linker_section_ptr
->written_address_p
)
7470 linker_section_ptr
->written_address_p
= true;
7471 bfd_put_ptr (output_bfd
,
7472 relocation
+ linker_section_ptr
->addend
,
7473 (lsect
->section
->contents
7474 + linker_section_ptr
->offset
));
7480 /* Handle local symbol. */
7481 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7482 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
7483 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
7484 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7485 (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
7489 BFD_ASSERT (linker_section_ptr
!= NULL
);
7491 /* Write out pointer if it hasn't been rewritten out before. */
7492 if (!linker_section_ptr
->written_address_p
)
7494 linker_section_ptr
->written_address_p
= true;
7495 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
7496 lsect
->section
->contents
+ linker_section_ptr
->offset
);
7500 asection
*srel
= lsect
->rel_section
;
7501 Elf_Internal_Rela
*outrel
;
7502 Elf_External_Rela
*erel
;
7503 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7507 amt
= sizeof (Elf_Internal_Rela
) * bed
->s
->int_rels_per_ext_rel
;
7508 outrel
= (Elf_Internal_Rela
*) bfd_zmalloc (amt
);
7511 (*_bfd_error_handler
) (_("Error: out of memory"));
7515 /* We need to generate a relative reloc for the dynamic
7519 srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
7521 lsect
->rel_section
= srel
;
7524 BFD_ASSERT (srel
!= NULL
);
7526 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7527 outrel
[i
].r_offset
= (lsect
->section
->output_section
->vma
7528 + lsect
->section
->output_offset
7529 + linker_section_ptr
->offset
);
7530 outrel
[0].r_info
= ELF_R_INFO (0, relative_reloc
);
7531 outrel
[0].r_addend
= 0;
7532 erel
= (Elf_External_Rela
*) lsect
->section
->contents
;
7533 erel
+= elf_section_data (lsect
->section
)->rel_count
;
7534 elf_swap_reloca_out (output_bfd
, outrel
, erel
);
7535 ++elf_section_data (lsect
->section
)->rel_count
;
7542 relocation
= (lsect
->section
->output_offset
7543 + linker_section_ptr
->offset
7544 - lsect
->hole_offset
7545 - lsect
->sym_offset
);
7549 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7550 lsect
->name
, (long) relocation
, (long) relocation
);
7553 /* Subtract out the addend, because it will get added back in by the normal
7555 return relocation
- linker_section_ptr
->addend
;
7558 /* Garbage collect unused sections. */
7560 static boolean elf_gc_mark
7561 PARAMS ((struct bfd_link_info
*, asection
*,
7562 asection
* (*) (asection
*, struct bfd_link_info
*,
7563 Elf_Internal_Rela
*, struct elf_link_hash_entry
*,
7564 Elf_Internal_Sym
*)));
7566 static boolean elf_gc_sweep
7567 PARAMS ((struct bfd_link_info
*,
7568 boolean (*) (bfd
*, struct bfd_link_info
*, asection
*,
7569 const Elf_Internal_Rela
*)));
7571 static boolean elf_gc_sweep_symbol
7572 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7574 static boolean elf_gc_allocate_got_offsets
7575 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7577 static boolean elf_gc_propagate_vtable_entries_used
7578 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7580 static boolean elf_gc_smash_unused_vtentry_relocs
7581 PARAMS ((struct elf_link_hash_entry
*, PTR
));
7583 /* The mark phase of garbage collection. For a given section, mark
7584 it and any sections in this section's group, and all the sections
7585 which define symbols to which it refers. */
7588 elf_gc_mark (info
, sec
, gc_mark_hook
)
7589 struct bfd_link_info
*info
;
7591 asection
* (*gc_mark_hook
) PARAMS ((asection
*, struct bfd_link_info
*,
7592 Elf_Internal_Rela
*,
7593 struct elf_link_hash_entry
*,
7594 Elf_Internal_Sym
*));
7597 asection
*group_sec
;
7601 /* Mark all the sections in the group. */
7602 group_sec
= elf_section_data (sec
)->next_in_group
;
7603 if (group_sec
&& !group_sec
->gc_mark
)
7604 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
7607 /* Look through the section relocs. */
7609 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
7611 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
7612 Elf_Internal_Shdr
*symtab_hdr
;
7613 struct elf_link_hash_entry
**sym_hashes
;
7616 bfd
*input_bfd
= sec
->owner
;
7617 struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
7618 Elf_Internal_Sym
*isym
= NULL
;
7620 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
7621 sym_hashes
= elf_sym_hashes (input_bfd
);
7623 /* Read the local symbols. */
7624 if (elf_bad_symtab (input_bfd
))
7626 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
7630 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
7632 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
7633 if (isym
== NULL
&& nlocsyms
!= 0)
7635 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
7641 /* Read the relocations. */
7642 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
7643 (input_bfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
7644 info
->keep_memory
));
7645 if (relstart
== NULL
)
7650 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7652 for (rel
= relstart
; rel
< relend
; rel
++)
7654 unsigned long r_symndx
;
7656 struct elf_link_hash_entry
*h
;
7658 r_symndx
= ELF_R_SYM (rel
->r_info
);
7662 if (r_symndx
>= nlocsyms
7663 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
7665 h
= sym_hashes
[r_symndx
- extsymoff
];
7666 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
7670 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
7673 if (rsec
&& !rsec
->gc_mark
)
7675 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
7677 else if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
7686 if (elf_section_data (sec
)->relocs
!= relstart
)
7689 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
7691 if (! info
->keep_memory
)
7694 symtab_hdr
->contents
= (unsigned char *) isym
;
7701 /* The sweep phase of garbage collection. Remove all garbage sections. */
7704 elf_gc_sweep (info
, gc_sweep_hook
)
7705 struct bfd_link_info
*info
;
7706 boolean (*gc_sweep_hook
) PARAMS ((bfd
*, struct bfd_link_info
*,
7707 asection
*, const Elf_Internal_Rela
*));
7711 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7715 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
7718 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7720 /* Keep special sections. Keep .debug sections. */
7721 if ((o
->flags
& SEC_LINKER_CREATED
)
7722 || (o
->flags
& SEC_DEBUGGING
))
7728 /* Skip sweeping sections already excluded. */
7729 if (o
->flags
& SEC_EXCLUDE
)
7732 /* Since this is early in the link process, it is simple
7733 to remove a section from the output. */
7734 o
->flags
|= SEC_EXCLUDE
;
7736 /* But we also have to update some of the relocation
7737 info we collected before. */
7739 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
7741 Elf_Internal_Rela
*internal_relocs
;
7744 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
7745 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
7746 if (internal_relocs
== NULL
)
7749 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
7751 if (elf_section_data (o
)->relocs
!= internal_relocs
)
7752 free (internal_relocs
);
7760 /* Remove the symbols that were in the swept sections from the dynamic
7761 symbol table. GCFIXME: Anyone know how to get them out of the
7762 static symbol table as well? */
7766 elf_link_hash_traverse (elf_hash_table (info
),
7767 elf_gc_sweep_symbol
,
7770 elf_hash_table (info
)->dynsymcount
= i
;
7776 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7779 elf_gc_sweep_symbol (h
, idxptr
)
7780 struct elf_link_hash_entry
*h
;
7783 int *idx
= (int *) idxptr
;
7785 if (h
->root
.type
== bfd_link_hash_warning
)
7786 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7788 if (h
->dynindx
!= -1
7789 && ((h
->root
.type
!= bfd_link_hash_defined
7790 && h
->root
.type
!= bfd_link_hash_defweak
)
7791 || h
->root
.u
.def
.section
->gc_mark
))
7792 h
->dynindx
= (*idx
)++;
7797 /* Propogate collected vtable information. This is called through
7798 elf_link_hash_traverse. */
7801 elf_gc_propagate_vtable_entries_used (h
, okp
)
7802 struct elf_link_hash_entry
*h
;
7805 if (h
->root
.type
== bfd_link_hash_warning
)
7806 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7808 /* Those that are not vtables. */
7809 if (h
->vtable_parent
== NULL
)
7812 /* Those vtables that do not have parents, we cannot merge. */
7813 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
7816 /* If we've already been done, exit. */
7817 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
7820 /* Make sure the parent's table is up to date. */
7821 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
7823 if (h
->vtable_entries_used
== NULL
)
7825 /* None of this table's entries were referenced. Re-use the
7827 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
7828 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
7835 /* Or the parent's entries into ours. */
7836 cu
= h
->vtable_entries_used
;
7838 pu
= h
->vtable_parent
->vtable_entries_used
;
7841 asection
*sec
= h
->root
.u
.def
.section
;
7842 struct elf_backend_data
*bed
= get_elf_backend_data (sec
->owner
);
7843 int file_align
= bed
->s
->file_align
;
7845 n
= h
->vtable_parent
->vtable_entries_size
/ file_align
;
7860 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
7861 struct elf_link_hash_entry
*h
;
7865 bfd_vma hstart
, hend
;
7866 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
7867 struct elf_backend_data
*bed
;
7870 if (h
->root
.type
== bfd_link_hash_warning
)
7871 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7873 /* Take care of both those symbols that do not describe vtables as
7874 well as those that are not loaded. */
7875 if (h
->vtable_parent
== NULL
)
7878 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
7879 || h
->root
.type
== bfd_link_hash_defweak
);
7881 sec
= h
->root
.u
.def
.section
;
7882 hstart
= h
->root
.u
.def
.value
;
7883 hend
= hstart
+ h
->size
;
7885 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
7886 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, true));
7888 return *(boolean
*) okp
= false;
7889 bed
= get_elf_backend_data (sec
->owner
);
7890 file_align
= bed
->s
->file_align
;
7892 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7894 for (rel
= relstart
; rel
< relend
; ++rel
)
7895 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
7897 /* If the entry is in use, do nothing. */
7898 if (h
->vtable_entries_used
7899 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
7901 bfd_vma entry
= (rel
->r_offset
- hstart
) / file_align
;
7902 if (h
->vtable_entries_used
[entry
])
7905 /* Otherwise, kill it. */
7906 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
7912 /* Do mark and sweep of unused sections. */
7915 elf_gc_sections (abfd
, info
)
7917 struct bfd_link_info
*info
;
7921 asection
* (*gc_mark_hook
)
7922 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7923 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
7925 if (!get_elf_backend_data (abfd
)->can_gc_sections
7926 || info
->relocateable
|| info
->emitrelocations
7927 || elf_hash_table (info
)->dynamic_sections_created
)
7930 /* Apply transitive closure to the vtable entry usage info. */
7931 elf_link_hash_traverse (elf_hash_table (info
),
7932 elf_gc_propagate_vtable_entries_used
,
7937 /* Kill the vtable relocations that were not used. */
7938 elf_link_hash_traverse (elf_hash_table (info
),
7939 elf_gc_smash_unused_vtentry_relocs
,
7944 /* Grovel through relocs to find out who stays ... */
7946 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
7947 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7951 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
7954 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7956 if (o
->flags
& SEC_KEEP
)
7957 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
7962 /* ... and mark SEC_EXCLUDE for those that go. */
7963 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
7969 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
7972 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
7975 struct elf_link_hash_entry
*h
;
7978 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
7979 struct elf_link_hash_entry
**search
, *child
;
7980 bfd_size_type extsymcount
;
7982 /* The sh_info field of the symtab header tells us where the
7983 external symbols start. We don't care about the local symbols at
7985 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
7986 if (!elf_bad_symtab (abfd
))
7987 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
7989 sym_hashes
= elf_sym_hashes (abfd
);
7990 sym_hashes_end
= sym_hashes
+ extsymcount
;
7992 /* Hunt down the child symbol, which is in this section at the same
7993 offset as the relocation. */
7994 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
7996 if ((child
= *search
) != NULL
7997 && (child
->root
.type
== bfd_link_hash_defined
7998 || child
->root
.type
== bfd_link_hash_defweak
)
7999 && child
->root
.u
.def
.section
== sec
8000 && child
->root
.u
.def
.value
== offset
)
8004 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
8005 bfd_archive_filename (abfd
), sec
->name
,
8006 (unsigned long) offset
);
8007 bfd_set_error (bfd_error_invalid_operation
);
8013 /* This *should* only be the absolute section. It could potentially
8014 be that someone has defined a non-global vtable though, which
8015 would be bad. It isn't worth paging in the local symbols to be
8016 sure though; that case should simply be handled by the assembler. */
8018 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
8021 child
->vtable_parent
= h
;
8026 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
8029 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
8030 bfd
*abfd ATTRIBUTE_UNUSED
;
8031 asection
*sec ATTRIBUTE_UNUSED
;
8032 struct elf_link_hash_entry
*h
;
8035 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8036 int file_align
= bed
->s
->file_align
;
8038 if (addend
>= h
->vtable_entries_size
)
8041 boolean
*ptr
= h
->vtable_entries_used
;
8043 /* While the symbol is undefined, we have to be prepared to handle
8045 if (h
->root
.type
== bfd_link_hash_undefined
)
8052 /* Oops! We've got a reference past the defined end of
8053 the table. This is probably a bug -- shall we warn? */
8058 /* Allocate one extra entry for use as a "done" flag for the
8059 consolidation pass. */
8060 bytes
= (size
/ file_align
+ 1) * sizeof (boolean
);
8064 ptr
= bfd_realloc (ptr
- 1, (bfd_size_type
) bytes
);
8070 oldbytes
= ((h
->vtable_entries_size
/ file_align
+ 1)
8071 * sizeof (boolean
));
8072 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8076 ptr
= bfd_zmalloc ((bfd_size_type
) bytes
);
8081 /* And arrange for that done flag to be at index -1. */
8082 h
->vtable_entries_used
= ptr
+ 1;
8083 h
->vtable_entries_size
= size
;
8086 h
->vtable_entries_used
[addend
/ file_align
] = true;
8091 /* And an accompanying bit to work out final got entry offsets once
8092 we're done. Should be called from final_link. */
8095 elf_gc_common_finalize_got_offsets (abfd
, info
)
8097 struct bfd_link_info
*info
;
8100 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8103 /* The GOT offset is relative to the .got section, but the GOT header is
8104 put into the .got.plt section, if the backend uses it. */
8105 if (bed
->want_got_plt
)
8108 gotoff
= bed
->got_header_size
;
8110 /* Do the local .got entries first. */
8111 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
8113 bfd_signed_vma
*local_got
;
8114 bfd_size_type j
, locsymcount
;
8115 Elf_Internal_Shdr
*symtab_hdr
;
8117 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
8120 local_got
= elf_local_got_refcounts (i
);
8124 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
8125 if (elf_bad_symtab (i
))
8126 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8128 locsymcount
= symtab_hdr
->sh_info
;
8130 for (j
= 0; j
< locsymcount
; ++j
)
8132 if (local_got
[j
] > 0)
8134 local_got
[j
] = gotoff
;
8135 gotoff
+= ARCH_SIZE
/ 8;
8138 local_got
[j
] = (bfd_vma
) -1;
8142 /* Then the global .got entries. .plt refcounts are handled by
8143 adjust_dynamic_symbol */
8144 elf_link_hash_traverse (elf_hash_table (info
),
8145 elf_gc_allocate_got_offsets
,
8150 /* We need a special top-level link routine to convert got reference counts
8151 to real got offsets. */
8154 elf_gc_allocate_got_offsets (h
, offarg
)
8155 struct elf_link_hash_entry
*h
;
8158 bfd_vma
*off
= (bfd_vma
*) offarg
;
8160 if (h
->root
.type
== bfd_link_hash_warning
)
8161 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8163 if (h
->got
.refcount
> 0)
8165 h
->got
.offset
= off
[0];
8166 off
[0] += ARCH_SIZE
/ 8;
8169 h
->got
.offset
= (bfd_vma
) -1;
8174 /* Many folk need no more in the way of final link than this, once
8175 got entry reference counting is enabled. */
8178 elf_gc_common_final_link (abfd
, info
)
8180 struct bfd_link_info
*info
;
8182 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
8185 /* Invoke the regular ELF backend linker to do all the work. */
8186 return elf_bfd_final_link (abfd
, info
);
8189 /* This function will be called though elf_link_hash_traverse to store
8190 all hash value of the exported symbols in an array. */
8193 elf_collect_hash_codes (h
, data
)
8194 struct elf_link_hash_entry
*h
;
8197 unsigned long **valuep
= (unsigned long **) data
;
8203 if (h
->root
.type
== bfd_link_hash_warning
)
8204 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8206 /* Ignore indirect symbols. These are added by the versioning code. */
8207 if (h
->dynindx
== -1)
8210 name
= h
->root
.root
.string
;
8211 p
= strchr (name
, ELF_VER_CHR
);
8214 alc
= bfd_malloc ((bfd_size_type
) (p
- name
+ 1));
8215 memcpy (alc
, name
, (size_t) (p
- name
));
8216 alc
[p
- name
] = '\0';
8220 /* Compute the hash value. */
8221 ha
= bfd_elf_hash (name
);
8223 /* Store the found hash value in the array given as the argument. */
8226 /* And store it in the struct so that we can put it in the hash table
8228 h
->elf_hash_value
= ha
;
8237 elf_reloc_symbol_deleted_p (offset
, cookie
)
8241 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
8243 if (rcookie
->bad_symtab
)
8244 rcookie
->rel
= rcookie
->rels
;
8246 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
8248 unsigned long r_symndx
= ELF_R_SYM (rcookie
->rel
->r_info
);
8250 if (! rcookie
->bad_symtab
)
8251 if (rcookie
->rel
->r_offset
> offset
)
8253 if (rcookie
->rel
->r_offset
!= offset
)
8256 if (r_symndx
>= rcookie
->locsymcount
8257 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
8259 struct elf_link_hash_entry
*h
;
8261 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
8263 while (h
->root
.type
== bfd_link_hash_indirect
8264 || h
->root
.type
== bfd_link_hash_warning
)
8265 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8267 if ((h
->root
.type
== bfd_link_hash_defined
8268 || h
->root
.type
== bfd_link_hash_defweak
)
8269 && elf_discarded_section (h
->root
.u
.def
.section
))
8276 /* It's not a relocation against a global symbol,
8277 but it could be a relocation against a local
8278 symbol for a discarded section. */
8280 Elf_Internal_Sym
*isym
;
8282 /* Need to: get the symbol; get the section. */
8283 isym
= &rcookie
->locsyms
[r_symndx
];
8284 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
8286 isec
= section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
8287 if (isec
!= NULL
&& elf_discarded_section (isec
))
8296 /* Discard unneeded references to discarded sections.
8297 Returns true if any section's size was changed. */
8298 /* This function assumes that the relocations are in sorted order,
8299 which is true for all known assemblers. */
8302 elf_bfd_discard_info (output_bfd
, info
)
8304 struct bfd_link_info
*info
;
8306 struct elf_reloc_cookie cookie
;
8307 asection
*stab
, *eh
, *ehdr
;
8308 Elf_Internal_Shdr
*symtab_hdr
;
8309 struct elf_backend_data
*bed
;
8311 boolean ret
= false;
8312 boolean strip
= info
->strip
== strip_all
|| info
->strip
== strip_debugger
;
8314 if (info
->relocateable
8315 || info
->traditional_format
8316 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
8317 || ! is_elf_hash_table (info
))
8321 if (elf_hash_table (info
)->dynobj
!= NULL
)
8322 ehdr
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
8325 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
8327 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
8330 bed
= get_elf_backend_data (abfd
);
8332 if ((abfd
->flags
& DYNAMIC
) != 0)
8338 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
8339 if (eh
&& (eh
->_raw_size
== 0
8340 || bfd_is_abs_section (eh
->output_section
)))
8347 stab
= bfd_get_section_by_name (abfd
, ".stab");
8348 if (stab
&& (stab
->_raw_size
== 0
8349 || bfd_is_abs_section (stab
->output_section
)))
8353 || elf_section_data(stab
)->sec_info_type
!= ELF_INFO_TYPE_STABS
)
8355 && (strip
|| ! bed
->elf_backend_discard_info
))
8358 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8360 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
8361 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
8362 if (cookie
.bad_symtab
)
8364 cookie
.locsymcount
=
8365 symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8366 cookie
.extsymoff
= 0;
8370 cookie
.locsymcount
= symtab_hdr
->sh_info
;
8371 cookie
.extsymoff
= symtab_hdr
->sh_info
;
8374 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8375 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
8377 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
8378 cookie
.locsymcount
, 0,
8380 if (cookie
.locsyms
== NULL
)
8386 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8387 (abfd
, stab
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
8388 info
->keep_memory
));
8391 cookie
.rel
= cookie
.rels
;
8393 cookie
.rels
+ stab
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8394 if (_bfd_discard_section_stabs (abfd
, stab
,
8395 elf_section_data (stab
)->sec_info
,
8396 elf_reloc_symbol_deleted_p
,
8399 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
8408 cookie
.relend
= NULL
;
8409 if (eh
->reloc_count
)
8410 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8411 (abfd
, eh
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
8412 info
->keep_memory
));
8415 cookie
.rel
= cookie
.rels
;
8417 cookie
.rels
+ eh
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8419 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
, ehdr
,
8420 elf_reloc_symbol_deleted_p
,
8423 if (cookie
.rels
&& elf_section_data (eh
)->relocs
!= cookie
.rels
)
8427 if (bed
->elf_backend_discard_info
)
8429 if (bed
->elf_backend_discard_info (abfd
, &cookie
, info
))
8433 if (cookie
.locsyms
!= NULL
8434 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
8436 if (! info
->keep_memory
)
8437 free (cookie
.locsyms
);
8439 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
8443 if (ehdr
&& _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
, ehdr
))
8449 elf_section_ignore_discarded_relocs (sec
)
8452 struct elf_backend_data
*bed
;
8454 switch (elf_section_data (sec
)->sec_info_type
)
8456 case ELF_INFO_TYPE_STABS
:
8457 case ELF_INFO_TYPE_EH_FRAME
:
8463 bed
= get_elf_backend_data (sec
->owner
);
8464 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
8465 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))