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 Elf_Internal_Shdr
* shndx_hdr
;
153 Elf_External_Sym
* esym
;
154 Elf_External_Sym
* esymend
;
155 Elf_External_Sym
* buf
= NULL
;
156 Elf_External_Sym_Shndx
* shndx_buf
= NULL
;
157 Elf_External_Sym_Shndx
* shndx
;
158 bfd_size_type symcount
;
159 bfd_size_type extsymcount
;
160 bfd_size_type extsymoff
;
161 boolean result
= false;
165 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
166 if (abfd
== (bfd
*) NULL
)
169 if (! bfd_check_format (abfd
, bfd_object
))
172 /* If we have already included the element containing this symbol in the
173 link then we do not need to include it again. Just claim that any symbol
174 it contains is not a definition, so that our caller will not decide to
175 (re)include this element. */
176 if (abfd
->archive_pass
)
179 /* Select the appropriate symbol table. */
180 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
182 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
183 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
187 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
191 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
193 /* The sh_info field of the symtab header tells us where the
194 external symbols start. We don't care about the local symbols. */
195 if (elf_bad_symtab (abfd
))
197 extsymcount
= symcount
;
202 extsymcount
= symcount
- hdr
->sh_info
;
203 extsymoff
= hdr
->sh_info
;
206 amt
= extsymcount
* sizeof (Elf_External_Sym
);
207 buf
= (Elf_External_Sym
*) bfd_malloc (amt
);
208 if (buf
== NULL
&& extsymcount
!= 0)
211 /* Read in the symbol table.
212 FIXME: This ought to be cached somewhere. */
213 pos
= hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
);
214 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
215 || bfd_bread ((PTR
) buf
, amt
, abfd
) != amt
)
218 if (shndx_hdr
!= NULL
&& shndx_hdr
->sh_size
!= 0)
220 amt
= extsymcount
* sizeof (Elf_External_Sym_Shndx
);
221 shndx_buf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
222 if (shndx_buf
== NULL
&& extsymcount
!= 0)
225 pos
= shndx_hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym_Shndx
);
226 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
227 || bfd_bread ((PTR
) shndx_buf
, amt
, abfd
) != amt
)
231 /* Scan the symbol table looking for SYMDEF. */
232 esymend
= buf
+ extsymcount
;
233 for (esym
= buf
, shndx
= shndx_buf
;
235 esym
++, shndx
= (shndx
!= NULL
? shndx
+ 1 : NULL
))
237 Elf_Internal_Sym sym
;
240 elf_swap_symbol_in (abfd
, (const PTR
) esym
, (const PTR
) shndx
, &sym
);
242 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
243 if (name
== (const char *) NULL
)
246 if (strcmp (name
, symdef
->name
) == 0)
248 result
= is_global_data_symbol_definition (abfd
, & sym
);
254 if (shndx_buf
!= NULL
)
262 /* Add symbols from an ELF archive file to the linker hash table. We
263 don't use _bfd_generic_link_add_archive_symbols because of a
264 problem which arises on UnixWare. The UnixWare libc.so is an
265 archive which includes an entry libc.so.1 which defines a bunch of
266 symbols. The libc.so archive also includes a number of other
267 object files, which also define symbols, some of which are the same
268 as those defined in libc.so.1. Correct linking requires that we
269 consider each object file in turn, and include it if it defines any
270 symbols we need. _bfd_generic_link_add_archive_symbols does not do
271 this; it looks through the list of undefined symbols, and includes
272 any object file which defines them. When this algorithm is used on
273 UnixWare, it winds up pulling in libc.so.1 early and defining a
274 bunch of symbols. This means that some of the other objects in the
275 archive are not included in the link, which is incorrect since they
276 precede libc.so.1 in the archive.
278 Fortunately, ELF archive handling is simpler than that done by
279 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
280 oddities. In ELF, if we find a symbol in the archive map, and the
281 symbol is currently undefined, we know that we must pull in that
284 Unfortunately, we do have to make multiple passes over the symbol
285 table until nothing further is resolved. */
288 elf_link_add_archive_symbols (abfd
, info
)
290 struct bfd_link_info
*info
;
293 boolean
*defined
= NULL
;
294 boolean
*included
= NULL
;
299 if (! bfd_has_map (abfd
))
301 /* An empty archive is a special case. */
302 if (bfd_openr_next_archived_file (abfd
, (bfd
*) NULL
) == NULL
)
304 bfd_set_error (bfd_error_no_armap
);
308 /* Keep track of all symbols we know to be already defined, and all
309 files we know to be already included. This is to speed up the
310 second and subsequent passes. */
311 c
= bfd_ardata (abfd
)->symdef_count
;
315 amt
*= sizeof (boolean
);
316 defined
= (boolean
*) bfd_zmalloc (amt
);
317 included
= (boolean
*) bfd_zmalloc (amt
);
318 if (defined
== (boolean
*) NULL
|| included
== (boolean
*) NULL
)
321 symdefs
= bfd_ardata (abfd
)->symdefs
;
334 symdefend
= symdef
+ c
;
335 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
337 struct elf_link_hash_entry
*h
;
339 struct bfd_link_hash_entry
*undefs_tail
;
342 if (defined
[i
] || included
[i
])
344 if (symdef
->file_offset
== last
)
350 h
= elf_link_hash_lookup (elf_hash_table (info
), symdef
->name
,
351 false, false, false);
358 /* If this is a default version (the name contains @@),
359 look up the symbol again with only one `@' as well
360 as without the version. The effect is that references
361 to the symbol with and without the version will be
362 matched by the default symbol in the archive. */
364 p
= strchr (symdef
->name
, ELF_VER_CHR
);
365 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
368 /* First check with only one `@'. */
369 len
= strlen (symdef
->name
);
370 copy
= bfd_alloc (abfd
, (bfd_size_type
) len
);
373 first
= p
- symdef
->name
+ 1;
374 memcpy (copy
, symdef
->name
, first
);
375 memcpy (copy
+ first
, symdef
->name
+ first
+ 1, len
- first
);
377 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
378 false, false, false);
382 /* We also need to check references to the symbol
383 without the version. */
385 copy
[first
- 1] = '\0';
386 h
= elf_link_hash_lookup (elf_hash_table (info
),
387 copy
, false, false, false);
390 bfd_release (abfd
, copy
);
396 if (h
->root
.type
== bfd_link_hash_common
)
398 /* We currently have a common symbol. The archive map contains
399 a reference to this symbol, so we may want to include it. We
400 only want to include it however, if this archive element
401 contains a definition of the symbol, not just another common
404 Unfortunately some archivers (including GNU ar) will put
405 declarations of common symbols into their archive maps, as
406 well as real definitions, so we cannot just go by the archive
407 map alone. Instead we must read in the element's symbol
408 table and check that to see what kind of symbol definition
410 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
413 else if (h
->root
.type
!= bfd_link_hash_undefined
)
415 if (h
->root
.type
!= bfd_link_hash_undefweak
)
420 /* We need to include this archive member. */
421 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
422 if (element
== (bfd
*) NULL
)
425 if (! bfd_check_format (element
, bfd_object
))
428 /* Doublecheck that we have not included this object
429 already--it should be impossible, but there may be
430 something wrong with the archive. */
431 if (element
->archive_pass
!= 0)
433 bfd_set_error (bfd_error_bad_value
);
436 element
->archive_pass
= 1;
438 undefs_tail
= info
->hash
->undefs_tail
;
440 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
443 if (! elf_link_add_object_symbols (element
, info
))
446 /* If there are any new undefined symbols, we need to make
447 another pass through the archive in order to see whether
448 they can be defined. FIXME: This isn't perfect, because
449 common symbols wind up on undefs_tail and because an
450 undefined symbol which is defined later on in this pass
451 does not require another pass. This isn't a bug, but it
452 does make the code less efficient than it could be. */
453 if (undefs_tail
!= info
->hash
->undefs_tail
)
456 /* Look backward to mark all symbols from this object file
457 which we have already seen in this pass. */
461 included
[mark
] = true;
466 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
468 /* We mark subsequent symbols from this object file as we go
469 on through the loop. */
470 last
= symdef
->file_offset
;
481 if (defined
!= (boolean
*) NULL
)
483 if (included
!= (boolean
*) NULL
)
488 /* This function is called when we want to define a new symbol. It
489 handles the various cases which arise when we find a definition in
490 a dynamic object, or when there is already a definition in a
491 dynamic object. The new symbol is described by NAME, SYM, PSEC,
492 and PVALUE. We set SYM_HASH to the hash table entry. We set
493 OVERRIDE if the old symbol is overriding a new definition. We set
494 TYPE_CHANGE_OK if it is OK for the type to change. We set
495 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
496 change, we mean that we shouldn't warn if the type or size does
497 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
501 elf_merge_symbol (abfd
, info
, name
, sym
, psec
, pvalue
, sym_hash
,
502 override
, type_change_ok
, size_change_ok
, dt_needed
)
504 struct bfd_link_info
*info
;
506 Elf_Internal_Sym
*sym
;
509 struct elf_link_hash_entry
**sym_hash
;
511 boolean
*type_change_ok
;
512 boolean
*size_change_ok
;
516 struct elf_link_hash_entry
*h
;
519 boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
524 bind
= ELF_ST_BIND (sym
->st_info
);
526 if (! bfd_is_und_section (sec
))
527 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, false, false);
529 h
= ((struct elf_link_hash_entry
*)
530 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true, false, false));
535 /* This code is for coping with dynamic objects, and is only useful
536 if we are doing an ELF link. */
537 if (info
->hash
->creator
!= abfd
->xvec
)
540 /* For merging, we only care about real symbols. */
542 while (h
->root
.type
== bfd_link_hash_indirect
543 || h
->root
.type
== bfd_link_hash_warning
)
544 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
546 /* If we just created the symbol, mark it as being an ELF symbol.
547 Other than that, there is nothing to do--there is no merge issue
548 with a newly defined symbol--so we just return. */
550 if (h
->root
.type
== bfd_link_hash_new
)
552 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
556 /* OLDBFD is a BFD associated with the existing symbol. */
558 switch (h
->root
.type
)
564 case bfd_link_hash_undefined
:
565 case bfd_link_hash_undefweak
:
566 oldbfd
= h
->root
.u
.undef
.abfd
;
569 case bfd_link_hash_defined
:
570 case bfd_link_hash_defweak
:
571 oldbfd
= h
->root
.u
.def
.section
->owner
;
574 case bfd_link_hash_common
:
575 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
579 /* In cases involving weak versioned symbols, we may wind up trying
580 to merge a symbol with itself. Catch that here, to avoid the
581 confusion that results if we try to override a symbol with
582 itself. The additional tests catch cases like
583 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
584 dynamic object, which we do want to handle here. */
586 && ((abfd
->flags
& DYNAMIC
) == 0
587 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0))
590 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
591 respectively, is from a dynamic object. */
593 if ((abfd
->flags
& DYNAMIC
) != 0)
599 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
604 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
605 indices used by MIPS ELF. */
606 switch (h
->root
.type
)
612 case bfd_link_hash_defined
:
613 case bfd_link_hash_defweak
:
614 hsec
= h
->root
.u
.def
.section
;
617 case bfd_link_hash_common
:
618 hsec
= h
->root
.u
.c
.p
->section
;
625 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
628 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
629 respectively, appear to be a definition rather than reference. */
631 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
636 if (h
->root
.type
== bfd_link_hash_undefined
637 || h
->root
.type
== bfd_link_hash_undefweak
638 || h
->root
.type
== bfd_link_hash_common
)
643 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
644 symbol, respectively, appears to be a common symbol in a dynamic
645 object. If a symbol appears in an uninitialized section, and is
646 not weak, and is not a function, then it may be a common symbol
647 which was resolved when the dynamic object was created. We want
648 to treat such symbols specially, because they raise special
649 considerations when setting the symbol size: if the symbol
650 appears as a common symbol in a regular object, and the size in
651 the regular object is larger, we must make sure that we use the
652 larger size. This problematic case can always be avoided in C,
653 but it must be handled correctly when using Fortran shared
656 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
657 likewise for OLDDYNCOMMON and OLDDEF.
659 Note that this test is just a heuristic, and that it is quite
660 possible to have an uninitialized symbol in a shared object which
661 is really a definition, rather than a common symbol. This could
662 lead to some minor confusion when the symbol really is a common
663 symbol in some regular object. However, I think it will be
668 && (sec
->flags
& SEC_ALLOC
) != 0
669 && (sec
->flags
& SEC_LOAD
) == 0
672 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
675 newdyncommon
= false;
679 && h
->root
.type
== bfd_link_hash_defined
680 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
681 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
682 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
684 && h
->type
!= STT_FUNC
)
687 olddyncommon
= false;
689 /* It's OK to change the type if either the existing symbol or the
690 new symbol is weak unless it comes from a DT_NEEDED entry of
691 a shared object, in which case, the DT_NEEDED entry may not be
692 required at the run time. */
694 if ((! dt_needed
&& h
->root
.type
== bfd_link_hash_defweak
)
695 || h
->root
.type
== bfd_link_hash_undefweak
697 *type_change_ok
= true;
699 /* It's OK to change the size if either the existing symbol or the
700 new symbol is weak, or if the old symbol is undefined. */
703 || h
->root
.type
== bfd_link_hash_undefined
)
704 *size_change_ok
= true;
706 /* If both the old and the new symbols look like common symbols in a
707 dynamic object, set the size of the symbol to the larger of the
712 && sym
->st_size
!= h
->size
)
714 /* Since we think we have two common symbols, issue a multiple
715 common warning if desired. Note that we only warn if the
716 size is different. If the size is the same, we simply let
717 the old symbol override the new one as normally happens with
718 symbols defined in dynamic objects. */
720 if (! ((*info
->callbacks
->multiple_common
)
721 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
722 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
725 if (sym
->st_size
> h
->size
)
726 h
->size
= sym
->st_size
;
728 *size_change_ok
= true;
731 /* If we are looking at a dynamic object, and we have found a
732 definition, we need to see if the symbol was already defined by
733 some other object. If so, we want to use the existing
734 definition, and we do not want to report a multiple symbol
735 definition error; we do this by clobbering *PSEC to be
738 We treat a common symbol as a definition if the symbol in the
739 shared library is a function, since common symbols always
740 represent variables; this can cause confusion in principle, but
741 any such confusion would seem to indicate an erroneous program or
742 shared library. We also permit a common symbol in a regular
743 object to override a weak symbol in a shared object.
745 We prefer a non-weak definition in a shared library to a weak
746 definition in the executable unless it comes from a DT_NEEDED
747 entry of a shared object, in which case, the DT_NEEDED entry
748 may not be required at the run time. */
753 || (h
->root
.type
== bfd_link_hash_common
755 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)))
756 && (h
->root
.type
!= bfd_link_hash_defweak
758 || bind
== STB_WEAK
))
762 newdyncommon
= false;
764 *psec
= sec
= bfd_und_section_ptr
;
765 *size_change_ok
= true;
767 /* If we get here when the old symbol is a common symbol, then
768 we are explicitly letting it override a weak symbol or
769 function in a dynamic object, and we don't want to warn about
770 a type change. If the old symbol is a defined symbol, a type
771 change warning may still be appropriate. */
773 if (h
->root
.type
== bfd_link_hash_common
)
774 *type_change_ok
= true;
777 /* Handle the special case of an old common symbol merging with a
778 new symbol which looks like a common symbol in a shared object.
779 We change *PSEC and *PVALUE to make the new symbol look like a
780 common symbol, and let _bfd_generic_link_add_one_symbol will do
784 && h
->root
.type
== bfd_link_hash_common
)
788 newdyncommon
= false;
789 *pvalue
= sym
->st_size
;
790 *psec
= sec
= bfd_com_section_ptr
;
791 *size_change_ok
= true;
794 /* If the old symbol is from a dynamic object, and the new symbol is
795 a definition which is not from a dynamic object, then the new
796 symbol overrides the old symbol. Symbols from regular files
797 always take precedence over symbols from dynamic objects, even if
798 they are defined after the dynamic object in the link.
800 As above, we again permit a common symbol in a regular object to
801 override a definition in a shared object if the shared object
802 symbol is a function or is weak.
804 As above, we permit a non-weak definition in a shared object to
805 override a weak definition in a regular object. */
809 || (bfd_is_com_section (sec
)
810 && (h
->root
.type
== bfd_link_hash_defweak
811 || h
->type
== STT_FUNC
)))
814 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
816 || h
->root
.type
== bfd_link_hash_defweak
))
818 /* Change the hash table entry to undefined, and let
819 _bfd_generic_link_add_one_symbol do the right thing with the
822 h
->root
.type
= bfd_link_hash_undefined
;
823 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
824 *size_change_ok
= true;
827 olddyncommon
= false;
829 /* We again permit a type change when a common symbol may be
830 overriding a function. */
832 if (bfd_is_com_section (sec
))
833 *type_change_ok
= true;
835 /* This union may have been set to be non-NULL when this symbol
836 was seen in a dynamic object. We must force the union to be
837 NULL, so that it is correct for a regular symbol. */
839 h
->verinfo
.vertree
= NULL
;
841 /* In this special case, if H is the target of an indirection,
842 we want the caller to frob with H rather than with the
843 indirect symbol. That will permit the caller to redefine the
844 target of the indirection, rather than the indirect symbol
845 itself. FIXME: This will break the -y option if we store a
846 symbol with a different name. */
850 /* Handle the special case of a new common symbol merging with an
851 old symbol that looks like it might be a common symbol defined in
852 a shared object. Note that we have already handled the case in
853 which a new common symbol should simply override the definition
854 in the shared library. */
857 && bfd_is_com_section (sec
)
860 /* It would be best if we could set the hash table entry to a
861 common symbol, but we don't know what to use for the section
863 if (! ((*info
->callbacks
->multiple_common
)
864 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
865 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
868 /* If the predumed common symbol in the dynamic object is
869 larger, pretend that the new symbol has its size. */
871 if (h
->size
> *pvalue
)
874 /* FIXME: We no longer know the alignment required by the symbol
875 in the dynamic object, so we just wind up using the one from
876 the regular object. */
879 olddyncommon
= false;
881 h
->root
.type
= bfd_link_hash_undefined
;
882 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
884 *size_change_ok
= true;
885 *type_change_ok
= true;
887 h
->verinfo
.vertree
= NULL
;
890 /* Handle the special case of a weak definition in a regular object
891 followed by a non-weak definition in a shared object. In this
892 case, we prefer the definition in the shared object unless it
893 comes from a DT_NEEDED entry of a shared object, in which case,
894 the DT_NEEDED entry may not be required at the run time. */
897 && h
->root
.type
== bfd_link_hash_defweak
902 /* To make this work we have to frob the flags so that the rest
903 of the code does not think we are using the regular
905 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
906 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
907 else if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
908 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
909 h
->elf_link_hash_flags
&= ~ (ELF_LINK_HASH_DEF_REGULAR
910 | ELF_LINK_HASH_DEF_DYNAMIC
);
912 /* If H is the target of an indirection, we want the caller to
913 use H rather than the indirect symbol. Otherwise if we are
914 defining a new indirect symbol we will wind up attaching it
915 to the entry we are overriding. */
919 /* Handle the special case of a non-weak definition in a shared
920 object followed by a weak definition in a regular object. In
921 this case we prefer to definition in the shared object. To make
922 this work we have to tell the caller to not treat the new symbol
926 && h
->root
.type
!= bfd_link_hash_defweak
935 /* This function is called to create an indirect symbol from the
936 default for the symbol with the default version if needed. The
937 symbol is described by H, NAME, SYM, SEC, VALUE, and OVERRIDE. We
938 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
939 indicates if it comes from a DT_NEEDED entry of a shared object. */
942 elf_add_default_symbol (abfd
, info
, h
, name
, sym
, sec
, value
,
943 dynsym
, override
, dt_needed
)
945 struct bfd_link_info
*info
;
946 struct elf_link_hash_entry
*h
;
948 Elf_Internal_Sym
*sym
;
955 boolean type_change_ok
;
956 boolean size_change_ok
;
958 struct elf_link_hash_entry
*hi
;
959 struct elf_backend_data
*bed
;
963 size_t len
, shortlen
;
965 /* If this symbol has a version, and it is the default version, we
966 create an indirect symbol from the default name to the fully
967 decorated name. This will cause external references which do not
968 specify a version to be bound to this version of the symbol. */
969 p
= strchr (name
, ELF_VER_CHR
);
970 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
975 /* We are overridden by an old defition. We need to check if we
976 need to create the indirect symbol from the default name. */
977 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, true,
979 BFD_ASSERT (hi
!= NULL
);
982 while (hi
->root
.type
== bfd_link_hash_indirect
983 || hi
->root
.type
== bfd_link_hash_warning
)
985 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
991 bed
= get_elf_backend_data (abfd
);
992 collect
= bed
->collect
;
993 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
996 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
997 if (shortname
== NULL
)
999 memcpy (shortname
, name
, shortlen
);
1000 shortname
[shortlen
] = '\0';
1002 /* We are going to create a new symbol. Merge it with any existing
1003 symbol with this name. For the purposes of the merge, act as
1004 though we were defining the symbol we just defined, although we
1005 actually going to define an indirect symbol. */
1006 type_change_ok
= false;
1007 size_change_ok
= false;
1008 if (! elf_merge_symbol (abfd
, info
, shortname
, sym
, sec
, value
,
1009 &hi
, &override
, &type_change_ok
,
1010 &size_change_ok
, dt_needed
))
1015 if (! (_bfd_generic_link_add_one_symbol
1016 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1017 (bfd_vma
) 0, name
, false, collect
,
1018 (struct bfd_link_hash_entry
**) &hi
)))
1023 /* In this case the symbol named SHORTNAME is overriding the
1024 indirect symbol we want to add. We were planning on making
1025 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1026 is the name without a version. NAME is the fully versioned
1027 name, and it is the default version.
1029 Overriding means that we already saw a definition for the
1030 symbol SHORTNAME in a regular object, and it is overriding
1031 the symbol defined in the dynamic object.
1033 When this happens, we actually want to change NAME, the
1034 symbol we just added, to refer to SHORTNAME. This will cause
1035 references to NAME in the shared object to become references
1036 to SHORTNAME in the regular object. This is what we expect
1037 when we override a function in a shared object: that the
1038 references in the shared object will be mapped to the
1039 definition in the regular object. */
1041 while (hi
->root
.type
== bfd_link_hash_indirect
1042 || hi
->root
.type
== bfd_link_hash_warning
)
1043 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1045 h
->root
.type
= bfd_link_hash_indirect
;
1046 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1047 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1049 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1050 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1051 if (hi
->elf_link_hash_flags
1052 & (ELF_LINK_HASH_REF_REGULAR
1053 | ELF_LINK_HASH_DEF_REGULAR
))
1055 if (! _bfd_elf_link_record_dynamic_symbol (info
, hi
))
1060 /* Now set HI to H, so that the following code will set the
1061 other fields correctly. */
1065 /* If there is a duplicate definition somewhere, then HI may not
1066 point to an indirect symbol. We will have reported an error to
1067 the user in that case. */
1069 if (hi
->root
.type
== bfd_link_hash_indirect
)
1071 struct elf_link_hash_entry
*ht
;
1073 /* If the symbol became indirect, then we assume that we have
1074 not seen a definition before. */
1075 BFD_ASSERT ((hi
->elf_link_hash_flags
1076 & (ELF_LINK_HASH_DEF_DYNAMIC
1077 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1079 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1080 (*bed
->elf_backend_copy_indirect_symbol
) (ht
, hi
);
1082 /* See if the new flags lead us to realize that the symbol must
1089 || ((hi
->elf_link_hash_flags
1090 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1095 if ((hi
->elf_link_hash_flags
1096 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1102 /* We also need to define an indirection from the nondefault version
1105 len
= strlen (name
);
1106 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1107 if (shortname
== NULL
)
1109 memcpy (shortname
, name
, shortlen
);
1110 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1112 /* Once again, merge with any existing symbol. */
1113 type_change_ok
= false;
1114 size_change_ok
= false;
1115 if (! elf_merge_symbol (abfd
, info
, shortname
, sym
, sec
, value
,
1116 &hi
, &override
, &type_change_ok
,
1117 &size_change_ok
, dt_needed
))
1122 /* Here SHORTNAME is a versioned name, so we don't expect to see
1123 the type of override we do in the case above. */
1124 (*_bfd_error_handler
)
1125 (_("%s: warning: unexpected redefinition of `%s'"),
1126 bfd_archive_filename (abfd
), shortname
);
1130 if (! (_bfd_generic_link_add_one_symbol
1131 (info
, abfd
, shortname
, BSF_INDIRECT
,
1132 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1133 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1136 /* If there is a duplicate definition somewhere, then HI may not
1137 point to an indirect symbol. We will have reported an error
1138 to the user in that case. */
1140 if (hi
->root
.type
== bfd_link_hash_indirect
)
1142 /* If the symbol became indirect, then we assume that we have
1143 not seen a definition before. */
1144 BFD_ASSERT ((hi
->elf_link_hash_flags
1145 & (ELF_LINK_HASH_DEF_DYNAMIC
1146 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1148 (*bed
->elf_backend_copy_indirect_symbol
) (h
, hi
);
1150 /* See if the new flags lead us to realize that the symbol
1157 || ((hi
->elf_link_hash_flags
1158 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1163 if ((hi
->elf_link_hash_flags
1164 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1174 /* Add symbols from an ELF object file to the linker hash table. */
1177 elf_link_add_object_symbols (abfd
, info
)
1179 struct bfd_link_info
*info
;
1181 boolean (*add_symbol_hook
) PARAMS ((bfd
*, struct bfd_link_info
*,
1182 const Elf_Internal_Sym
*,
1183 const char **, flagword
*,
1184 asection
**, bfd_vma
*));
1185 boolean (*check_relocs
) PARAMS ((bfd
*, struct bfd_link_info
*,
1186 asection
*, const Elf_Internal_Rela
*));
1188 Elf_Internal_Shdr
*hdr
;
1189 Elf_Internal_Shdr
*shndx_hdr
;
1190 bfd_size_type symcount
;
1191 bfd_size_type extsymcount
;
1192 bfd_size_type extsymoff
;
1193 Elf_External_Sym
*buf
= NULL
;
1194 Elf_External_Sym_Shndx
*shndx_buf
= NULL
;
1195 Elf_External_Sym_Shndx
*shndx
;
1196 struct elf_link_hash_entry
**sym_hash
;
1198 Elf_External_Versym
*extversym
= NULL
;
1199 Elf_External_Versym
*ever
;
1200 Elf_External_Dyn
*dynbuf
= NULL
;
1201 struct elf_link_hash_entry
*weaks
;
1202 Elf_External_Sym
*esym
;
1203 Elf_External_Sym
*esymend
;
1204 struct elf_backend_data
*bed
;
1206 struct elf_link_hash_table
* hash_table
;
1210 hash_table
= elf_hash_table (info
);
1212 bed
= get_elf_backend_data (abfd
);
1213 add_symbol_hook
= bed
->elf_add_symbol_hook
;
1214 collect
= bed
->collect
;
1216 if ((abfd
->flags
& DYNAMIC
) == 0)
1222 /* You can't use -r against a dynamic object. Also, there's no
1223 hope of using a dynamic object which does not exactly match
1224 the format of the output file. */
1225 if (info
->relocateable
|| info
->hash
->creator
!= abfd
->xvec
)
1227 bfd_set_error (bfd_error_invalid_operation
);
1232 /* As a GNU extension, any input sections which are named
1233 .gnu.warning.SYMBOL are treated as warning symbols for the given
1234 symbol. This differs from .gnu.warning sections, which generate
1235 warnings when they are included in an output file. */
1240 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
1244 name
= bfd_get_section_name (abfd
, s
);
1245 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1250 name
+= sizeof ".gnu.warning." - 1;
1252 /* If this is a shared object, then look up the symbol
1253 in the hash table. If it is there, and it is already
1254 been defined, then we will not be using the entry
1255 from this shared object, so we don't need to warn.
1256 FIXME: If we see the definition in a regular object
1257 later on, we will warn, but we shouldn't. The only
1258 fix is to keep track of what warnings we are supposed
1259 to emit, and then handle them all at the end of the
1261 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
1263 struct elf_link_hash_entry
*h
;
1265 h
= elf_link_hash_lookup (hash_table
, name
,
1266 false, false, true);
1268 /* FIXME: What about bfd_link_hash_common? */
1270 && (h
->root
.type
== bfd_link_hash_defined
1271 || h
->root
.type
== bfd_link_hash_defweak
))
1273 /* We don't want to issue this warning. Clobber
1274 the section size so that the warning does not
1275 get copied into the output file. */
1281 sz
= bfd_section_size (abfd
, s
);
1282 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
1286 if (! bfd_get_section_contents (abfd
, s
, msg
, (file_ptr
) 0, sz
))
1291 if (! (_bfd_generic_link_add_one_symbol
1292 (info
, abfd
, name
, BSF_WARNING
, s
, (bfd_vma
) 0, msg
,
1293 false, collect
, (struct bfd_link_hash_entry
**) NULL
)))
1296 if (! info
->relocateable
)
1298 /* Clobber the section size so that the warning does
1299 not get copied into the output file. */
1306 /* If this is a dynamic object, we always link against the .dynsym
1307 symbol table, not the .symtab symbol table. The dynamic linker
1308 will only see the .dynsym symbol table, so there is no reason to
1309 look at .symtab for a dynamic object. */
1311 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
1313 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1314 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
1318 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1324 /* Read in any version definitions. */
1326 if (! _bfd_elf_slurp_version_tables (abfd
))
1329 /* Read in the symbol versions, but don't bother to convert them
1330 to internal format. */
1331 if (elf_dynversym (abfd
) != 0)
1333 Elf_Internal_Shdr
*versymhdr
;
1335 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
1336 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
1337 if (extversym
== NULL
)
1339 amt
= versymhdr
->sh_size
;
1340 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
1341 || bfd_bread ((PTR
) extversym
, amt
, abfd
) != amt
)
1346 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
1348 /* The sh_info field of the symtab header tells us where the
1349 external symbols start. We don't care about the local symbols at
1351 if (elf_bad_symtab (abfd
))
1353 extsymcount
= symcount
;
1358 extsymcount
= symcount
- hdr
->sh_info
;
1359 extsymoff
= hdr
->sh_info
;
1362 amt
= extsymcount
* sizeof (Elf_External_Sym
);
1363 buf
= (Elf_External_Sym
*) bfd_malloc (amt
);
1364 if (buf
== NULL
&& extsymcount
!= 0)
1367 if (shndx_hdr
!= NULL
&& shndx_hdr
->sh_size
!= 0)
1369 amt
= extsymcount
* sizeof (Elf_External_Sym_Shndx
);
1370 shndx_buf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
1371 if (shndx_buf
== NULL
&& extsymcount
!= 0)
1375 /* We store a pointer to the hash table entry for each external
1377 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
1378 sym_hash
= (struct elf_link_hash_entry
**) bfd_alloc (abfd
, amt
);
1379 if (sym_hash
== NULL
)
1381 elf_sym_hashes (abfd
) = sym_hash
;
1387 /* If we are creating a shared library, create all the dynamic
1388 sections immediately. We need to attach them to something,
1389 so we attach them to this BFD, provided it is the right
1390 format. FIXME: If there are no input BFD's of the same
1391 format as the output, we can't make a shared library. */
1393 && is_elf_hash_table (info
)
1394 && ! hash_table
->dynamic_sections_created
1395 && abfd
->xvec
== info
->hash
->creator
)
1397 if (! elf_link_create_dynamic_sections (abfd
, info
))
1401 else if (! is_elf_hash_table (info
))
1408 bfd_size_type oldsize
;
1409 bfd_size_type strindex
;
1411 /* Find the name to use in a DT_NEEDED entry that refers to this
1412 object. If the object has a DT_SONAME entry, we use it.
1413 Otherwise, if the generic linker stuck something in
1414 elf_dt_name, we use that. Otherwise, we just use the file
1415 name. If the generic linker put a null string into
1416 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1417 there is a DT_SONAME entry. */
1419 name
= bfd_get_filename (abfd
);
1420 if (elf_dt_name (abfd
) != NULL
)
1422 name
= elf_dt_name (abfd
);
1425 if (elf_dt_soname (abfd
) != NULL
)
1431 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1434 Elf_External_Dyn
*extdyn
;
1435 Elf_External_Dyn
*extdynend
;
1437 unsigned long shlink
;
1441 dynbuf
= (Elf_External_Dyn
*) bfd_malloc (s
->_raw_size
);
1445 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
1446 (file_ptr
) 0, s
->_raw_size
))
1449 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1452 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1455 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
1458 for (; extdyn
< extdynend
; extdyn
++)
1460 Elf_Internal_Dyn dyn
;
1462 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
1463 if (dyn
.d_tag
== DT_SONAME
)
1465 unsigned int tagv
= dyn
.d_un
.d_val
;
1466 name
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1470 if (dyn
.d_tag
== DT_NEEDED
)
1472 struct bfd_link_needed_list
*n
, **pn
;
1474 unsigned int tagv
= dyn
.d_un
.d_val
;
1476 amt
= sizeof (struct bfd_link_needed_list
);
1477 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1478 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1479 if (n
== NULL
|| fnm
== NULL
)
1481 amt
= strlen (fnm
) + 1;
1482 anm
= bfd_alloc (abfd
, amt
);
1485 memcpy (anm
, fnm
, (size_t) amt
);
1489 for (pn
= & hash_table
->needed
;
1495 if (dyn
.d_tag
== DT_RUNPATH
)
1497 struct bfd_link_needed_list
*n
, **pn
;
1499 unsigned int tagv
= dyn
.d_un
.d_val
;
1501 /* When we see DT_RPATH before DT_RUNPATH, we have
1502 to clear runpath. Do _NOT_ bfd_release, as that
1503 frees all more recently bfd_alloc'd blocks as
1505 if (rpath
&& hash_table
->runpath
)
1506 hash_table
->runpath
= NULL
;
1508 amt
= sizeof (struct bfd_link_needed_list
);
1509 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1510 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1511 if (n
== NULL
|| fnm
== NULL
)
1513 amt
= strlen (fnm
) + 1;
1514 anm
= bfd_alloc (abfd
, amt
);
1517 memcpy (anm
, fnm
, (size_t) amt
);
1521 for (pn
= & hash_table
->runpath
;
1529 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1530 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
1532 struct bfd_link_needed_list
*n
, **pn
;
1534 unsigned int tagv
= dyn
.d_un
.d_val
;
1536 amt
= sizeof (struct bfd_link_needed_list
);
1537 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1538 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1539 if (n
== NULL
|| fnm
== NULL
)
1541 amt
= strlen (fnm
) + 1;
1542 anm
= bfd_alloc (abfd
, amt
);
1545 memcpy (anm
, fnm
, (size_t) amt
);
1549 for (pn
= & hash_table
->runpath
;
1562 /* We do not want to include any of the sections in a dynamic
1563 object in the output file. We hack by simply clobbering the
1564 list of sections in the BFD. This could be handled more
1565 cleanly by, say, a new section flag; the existing
1566 SEC_NEVER_LOAD flag is not the one we want, because that one
1567 still implies that the section takes up space in the output
1569 bfd_section_list_clear (abfd
);
1571 /* If this is the first dynamic object found in the link, create
1572 the special sections required for dynamic linking. */
1573 if (! hash_table
->dynamic_sections_created
)
1574 if (! elf_link_create_dynamic_sections (abfd
, info
))
1579 /* Add a DT_NEEDED entry for this dynamic object. */
1580 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
1581 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, name
, false);
1582 if (strindex
== (bfd_size_type
) -1)
1585 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
1588 Elf_External_Dyn
*dyncon
, *dynconend
;
1590 /* The hash table size did not change, which means that
1591 the dynamic object name was already entered. If we
1592 have already included this dynamic object in the
1593 link, just ignore it. There is no reason to include
1594 a particular dynamic object more than once. */
1595 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
1596 BFD_ASSERT (sdyn
!= NULL
);
1598 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1599 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1601 for (; dyncon
< dynconend
; dyncon
++)
1603 Elf_Internal_Dyn dyn
;
1605 elf_swap_dyn_in (hash_table
->dynobj
, dyncon
, & dyn
);
1606 if (dyn
.d_tag
== DT_NEEDED
1607 && dyn
.d_un
.d_val
== strindex
)
1611 if (extversym
!= NULL
)
1613 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
1619 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
1623 /* Save the SONAME, if there is one, because sometimes the
1624 linker emulation code will need to know it. */
1626 name
= basename (bfd_get_filename (abfd
));
1627 elf_dt_name (abfd
) = name
;
1630 pos
= hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
);
1631 amt
= extsymcount
* sizeof (Elf_External_Sym
);
1632 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1633 || bfd_bread ((PTR
) buf
, amt
, abfd
) != amt
)
1636 if (shndx_hdr
!= NULL
&& shndx_hdr
->sh_size
!= 0)
1638 amt
= extsymcount
* sizeof (Elf_External_Sym_Shndx
);
1639 pos
= shndx_hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym_Shndx
);
1640 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1641 || bfd_bread ((PTR
) shndx_buf
, amt
, abfd
) != amt
)
1647 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
1648 esymend
= buf
+ extsymcount
;
1649 for (esym
= buf
, shndx
= shndx_buf
;
1651 esym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
),
1652 shndx
= (shndx
!= NULL
? shndx
+ 1 : NULL
))
1654 Elf_Internal_Sym sym
;
1660 struct elf_link_hash_entry
*h
;
1662 boolean size_change_ok
, type_change_ok
;
1663 boolean new_weakdef
;
1664 unsigned int old_alignment
;
1669 elf_swap_symbol_in (abfd
, (const PTR
) esym
, (const PTR
) shndx
, &sym
);
1671 flags
= BSF_NO_FLAGS
;
1673 value
= sym
.st_value
;
1676 bind
= ELF_ST_BIND (sym
.st_info
);
1677 if (bind
== STB_LOCAL
)
1679 /* This should be impossible, since ELF requires that all
1680 global symbols follow all local symbols, and that sh_info
1681 point to the first global symbol. Unfortunatealy, Irix 5
1685 else if (bind
== STB_GLOBAL
)
1687 if (sym
.st_shndx
!= SHN_UNDEF
1688 && sym
.st_shndx
!= SHN_COMMON
)
1691 else if (bind
== STB_WEAK
)
1695 /* Leave it up to the processor backend. */
1698 if (sym
.st_shndx
== SHN_UNDEF
)
1699 sec
= bfd_und_section_ptr
;
1700 else if (sym
.st_shndx
< SHN_LORESERVE
|| sym
.st_shndx
> SHN_HIRESERVE
)
1702 sec
= section_from_elf_index (abfd
, sym
.st_shndx
);
1704 sec
= bfd_abs_section_ptr
;
1705 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
1708 else if (sym
.st_shndx
== SHN_ABS
)
1709 sec
= bfd_abs_section_ptr
;
1710 else if (sym
.st_shndx
== SHN_COMMON
)
1712 sec
= bfd_com_section_ptr
;
1713 /* What ELF calls the size we call the value. What ELF
1714 calls the value we call the alignment. */
1715 value
= sym
.st_size
;
1719 /* Leave it up to the processor backend. */
1722 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
1723 if (name
== (const char *) NULL
)
1726 if (sym
.st_shndx
== SHN_COMMON
&& ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
1728 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
1732 tcomm
= bfd_make_section (abfd
, ".tcommon");
1734 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
1736 | SEC_LINKER_CREATED
1737 | SEC_THREAD_LOCAL
)))
1742 else if (add_symbol_hook
)
1744 if (! (*add_symbol_hook
) (abfd
, info
, &sym
, &name
, &flags
, &sec
,
1748 /* The hook function sets the name to NULL if this symbol
1749 should be skipped for some reason. */
1750 if (name
== (const char *) NULL
)
1754 /* Sanity check that all possibilities were handled. */
1755 if (sec
== (asection
*) NULL
)
1757 bfd_set_error (bfd_error_bad_value
);
1761 if (bfd_is_und_section (sec
)
1762 || bfd_is_com_section (sec
))
1767 size_change_ok
= false;
1768 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
1770 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1772 Elf_Internal_Versym iver
;
1773 unsigned int vernum
= 0;
1777 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
1778 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
1780 /* If this is a hidden symbol, or if it is not version
1781 1, we append the version name to the symbol name.
1782 However, we do not modify a non-hidden absolute
1783 symbol, because it might be the version symbol
1784 itself. FIXME: What if it isn't? */
1785 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
1786 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
1789 size_t namelen
, verlen
, newlen
;
1792 if (sym
.st_shndx
!= SHN_UNDEF
)
1794 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
1796 (*_bfd_error_handler
)
1797 (_("%s: %s: invalid version %u (max %d)"),
1798 bfd_archive_filename (abfd
), name
, vernum
,
1799 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1800 bfd_set_error (bfd_error_bad_value
);
1803 else if (vernum
> 1)
1805 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1811 /* We cannot simply test for the number of
1812 entries in the VERNEED section since the
1813 numbers for the needed versions do not start
1815 Elf_Internal_Verneed
*t
;
1818 for (t
= elf_tdata (abfd
)->verref
;
1822 Elf_Internal_Vernaux
*a
;
1824 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1826 if (a
->vna_other
== vernum
)
1828 verstr
= a
->vna_nodename
;
1837 (*_bfd_error_handler
)
1838 (_("%s: %s: invalid needed version %d"),
1839 bfd_archive_filename (abfd
), name
, vernum
);
1840 bfd_set_error (bfd_error_bad_value
);
1845 namelen
= strlen (name
);
1846 verlen
= strlen (verstr
);
1847 newlen
= namelen
+ verlen
+ 2;
1848 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1849 && sym
.st_shndx
!= SHN_UNDEF
)
1852 newname
= (char *) bfd_alloc (abfd
, (bfd_size_type
) newlen
);
1853 if (newname
== NULL
)
1855 memcpy (newname
, name
, namelen
);
1856 p
= newname
+ namelen
;
1858 /* If this is a defined non-hidden version symbol,
1859 we add another @ to the name. This indicates the
1860 default version of the symbol. */
1861 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1862 && sym
.st_shndx
!= SHN_UNDEF
)
1864 memcpy (p
, verstr
, verlen
+ 1);
1870 if (! elf_merge_symbol (abfd
, info
, name
, &sym
, &sec
, &value
,
1871 sym_hash
, &override
, &type_change_ok
,
1872 &size_change_ok
, dt_needed
))
1879 while (h
->root
.type
== bfd_link_hash_indirect
1880 || h
->root
.type
== bfd_link_hash_warning
)
1881 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1883 /* Remember the old alignment if this is a common symbol, so
1884 that we don't reduce the alignment later on. We can't
1885 check later, because _bfd_generic_link_add_one_symbol
1886 will set a default for the alignment which we want to
1888 if (h
->root
.type
== bfd_link_hash_common
)
1889 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1891 if (elf_tdata (abfd
)->verdef
!= NULL
1895 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1898 if (! (_bfd_generic_link_add_one_symbol
1899 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
1900 false, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
1904 while (h
->root
.type
== bfd_link_hash_indirect
1905 || h
->root
.type
== bfd_link_hash_warning
)
1906 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1909 new_weakdef
= false;
1912 && (flags
& BSF_WEAK
) != 0
1913 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
1914 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1915 && h
->weakdef
== NULL
)
1917 /* Keep a list of all weak defined non function symbols from
1918 a dynamic object, using the weakdef field. Later in this
1919 function we will set the weakdef field to the correct
1920 value. We only put non-function symbols from dynamic
1921 objects on this list, because that happens to be the only
1922 time we need to know the normal symbol corresponding to a
1923 weak symbol, and the information is time consuming to
1924 figure out. If the weakdef field is not already NULL,
1925 then this symbol was already defined by some previous
1926 dynamic object, and we will be using that previous
1927 definition anyhow. */
1934 /* Set the alignment of a common symbol. */
1935 if (sym
.st_shndx
== SHN_COMMON
1936 && h
->root
.type
== bfd_link_hash_common
)
1940 align
= bfd_log2 (sym
.st_value
);
1941 if (align
> old_alignment
1942 /* Permit an alignment power of zero if an alignment of one
1943 is specified and no other alignments have been specified. */
1944 || (sym
.st_value
== 1 && old_alignment
== 0))
1945 h
->root
.u
.c
.p
->alignment_power
= align
;
1948 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1954 /* Remember the symbol size and type. */
1955 if (sym
.st_size
!= 0
1956 && (definition
|| h
->size
== 0))
1958 if (h
->size
!= 0 && h
->size
!= sym
.st_size
&& ! size_change_ok
)
1959 (*_bfd_error_handler
)
1960 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1961 name
, (unsigned long) h
->size
, (unsigned long) sym
.st_size
,
1962 bfd_archive_filename (abfd
));
1964 h
->size
= sym
.st_size
;
1967 /* If this is a common symbol, then we always want H->SIZE
1968 to be the size of the common symbol. The code just above
1969 won't fix the size if a common symbol becomes larger. We
1970 don't warn about a size change here, because that is
1971 covered by --warn-common. */
1972 if (h
->root
.type
== bfd_link_hash_common
)
1973 h
->size
= h
->root
.u
.c
.size
;
1975 if (ELF_ST_TYPE (sym
.st_info
) != STT_NOTYPE
1976 && (definition
|| h
->type
== STT_NOTYPE
))
1978 if (h
->type
!= STT_NOTYPE
1979 && h
->type
!= ELF_ST_TYPE (sym
.st_info
)
1980 && ! type_change_ok
)
1981 (*_bfd_error_handler
)
1982 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1983 name
, h
->type
, ELF_ST_TYPE (sym
.st_info
),
1984 bfd_archive_filename (abfd
));
1986 h
->type
= ELF_ST_TYPE (sym
.st_info
);
1989 /* If st_other has a processor-specific meaning, specific code
1990 might be needed here. */
1991 if (sym
.st_other
!= 0)
1993 /* Combine visibilities, using the most constraining one. */
1994 unsigned char hvis
= ELF_ST_VISIBILITY (h
->other
);
1995 unsigned char symvis
= ELF_ST_VISIBILITY (sym
.st_other
);
1997 if (symvis
&& (hvis
> symvis
|| hvis
== 0))
1998 h
->other
= sym
.st_other
;
2000 /* If neither has visibility, use the st_other of the
2001 definition. This is an arbitrary choice, since the
2002 other bits have no general meaning. */
2003 if (!symvis
&& !hvis
2004 && (definition
|| h
->other
== 0))
2005 h
->other
= sym
.st_other
;
2008 /* Set a flag in the hash table entry indicating the type of
2009 reference or definition we just found. Keep a count of
2010 the number of dynamic symbols we find. A dynamic symbol
2011 is one which is referenced or defined by both a regular
2012 object and a shared object. */
2013 old_flags
= h
->elf_link_hash_flags
;
2019 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
2020 if (bind
!= STB_WEAK
)
2021 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
2024 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
2026 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2027 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
2033 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
2035 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
2036 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
2037 | ELF_LINK_HASH_REF_REGULAR
)) != 0
2038 || (h
->weakdef
!= NULL
2040 && h
->weakdef
->dynindx
!= -1))
2044 h
->elf_link_hash_flags
|= new_flag
;
2046 /* Check to see if we need to add an indirect symbol for
2047 the default name. */
2048 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
2049 if (! elf_add_default_symbol (abfd
, info
, h
, name
, &sym
,
2050 &sec
, &value
, &dynsym
,
2051 override
, dt_needed
))
2054 if (dynsym
&& h
->dynindx
== -1)
2056 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2058 if (h
->weakdef
!= NULL
2060 && h
->weakdef
->dynindx
== -1)
2062 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2066 else if (dynsym
&& h
->dynindx
!= -1)
2067 /* If the symbol already has a dynamic index, but
2068 visibility says it should not be visible, turn it into
2070 switch (ELF_ST_VISIBILITY (h
->other
))
2074 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2078 if (dt_needed
&& definition
2079 && (h
->elf_link_hash_flags
2080 & ELF_LINK_HASH_REF_REGULAR
) != 0)
2082 bfd_size_type oldsize
;
2083 bfd_size_type strindex
;
2085 if (! is_elf_hash_table (info
))
2088 /* The symbol from a DT_NEEDED object is referenced from
2089 the regular object to create a dynamic executable. We
2090 have to make sure there is a DT_NEEDED entry for it. */
2093 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2094 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
,
2095 elf_dt_soname (abfd
), false);
2096 if (strindex
== (bfd_size_type
) -1)
2099 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2102 Elf_External_Dyn
*dyncon
, *dynconend
;
2104 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
,
2106 BFD_ASSERT (sdyn
!= NULL
);
2108 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
2109 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
2111 for (; dyncon
< dynconend
; dyncon
++)
2113 Elf_Internal_Dyn dyn
;
2115 elf_swap_dyn_in (hash_table
->dynobj
,
2117 BFD_ASSERT (dyn
.d_tag
!= DT_NEEDED
||
2118 dyn
.d_un
.d_val
!= strindex
);
2122 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
2128 /* Now set the weakdefs field correctly for all the weak defined
2129 symbols we found. The only way to do this is to search all the
2130 symbols. Since we only need the information for non functions in
2131 dynamic objects, that's the only time we actually put anything on
2132 the list WEAKS. We need this information so that if a regular
2133 object refers to a symbol defined weakly in a dynamic object, the
2134 real symbol in the dynamic object is also put in the dynamic
2135 symbols; we also must arrange for both symbols to point to the
2136 same memory location. We could handle the general case of symbol
2137 aliasing, but a general symbol alias can only be generated in
2138 assembler code, handling it correctly would be very time
2139 consuming, and other ELF linkers don't handle general aliasing
2141 while (weaks
!= NULL
)
2143 struct elf_link_hash_entry
*hlook
;
2146 struct elf_link_hash_entry
**hpp
;
2147 struct elf_link_hash_entry
**hppend
;
2150 weaks
= hlook
->weakdef
;
2151 hlook
->weakdef
= NULL
;
2153 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
2154 || hlook
->root
.type
== bfd_link_hash_defweak
2155 || hlook
->root
.type
== bfd_link_hash_common
2156 || hlook
->root
.type
== bfd_link_hash_indirect
);
2157 slook
= hlook
->root
.u
.def
.section
;
2158 vlook
= hlook
->root
.u
.def
.value
;
2160 hpp
= elf_sym_hashes (abfd
);
2161 hppend
= hpp
+ extsymcount
;
2162 for (; hpp
< hppend
; hpp
++)
2164 struct elf_link_hash_entry
*h
;
2167 if (h
!= NULL
&& h
!= hlook
2168 && h
->root
.type
== bfd_link_hash_defined
2169 && h
->root
.u
.def
.section
== slook
2170 && h
->root
.u
.def
.value
== vlook
)
2174 /* If the weak definition is in the list of dynamic
2175 symbols, make sure the real definition is put there
2177 if (hlook
->dynindx
!= -1
2178 && h
->dynindx
== -1)
2180 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2184 /* If the real definition is in the list of dynamic
2185 symbols, make sure the weak definition is put there
2186 as well. If we don't do this, then the dynamic
2187 loader might not merge the entries for the real
2188 definition and the weak definition. */
2189 if (h
->dynindx
!= -1
2190 && hlook
->dynindx
== -1)
2192 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
2207 if (extversym
!= NULL
)
2213 /* If this object is the same format as the output object, and it is
2214 not a shared library, then let the backend look through the
2217 This is required to build global offset table entries and to
2218 arrange for dynamic relocs. It is not required for the
2219 particular common case of linking non PIC code, even when linking
2220 against shared libraries, but unfortunately there is no way of
2221 knowing whether an object file has been compiled PIC or not.
2222 Looking through the relocs is not particularly time consuming.
2223 The problem is that we must either (1) keep the relocs in memory,
2224 which causes the linker to require additional runtime memory or
2225 (2) read the relocs twice from the input file, which wastes time.
2226 This would be a good case for using mmap.
2228 I have no idea how to handle linking PIC code into a file of a
2229 different format. It probably can't be done. */
2230 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
2232 && abfd
->xvec
== info
->hash
->creator
2233 && check_relocs
!= NULL
)
2237 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2239 Elf_Internal_Rela
*internal_relocs
;
2242 if ((o
->flags
& SEC_RELOC
) == 0
2243 || o
->reloc_count
== 0
2244 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
2245 && (o
->flags
& SEC_DEBUGGING
) != 0)
2246 || bfd_is_abs_section (o
->output_section
))
2249 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
2250 (abfd
, o
, (PTR
) NULL
,
2251 (Elf_Internal_Rela
*) NULL
,
2252 info
->keep_memory
));
2253 if (internal_relocs
== NULL
)
2256 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
2258 if (! info
->keep_memory
)
2259 free (internal_relocs
);
2266 /* If this is a non-traditional, non-relocateable link, try to
2267 optimize the handling of the .stab/.stabstr sections. */
2269 && ! info
->relocateable
2270 && ! info
->traditional_format
2271 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
2272 && is_elf_hash_table (info
)
2273 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
2275 asection
*stab
, *stabstr
;
2277 stab
= bfd_get_section_by_name (abfd
, ".stab");
2279 && (stab
->flags
& SEC_MERGE
) == 0
2280 && !bfd_is_abs_section (stab
->output_section
))
2282 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
2284 if (stabstr
!= NULL
)
2286 struct bfd_elf_section_data
*secdata
;
2288 secdata
= elf_section_data (stab
);
2289 if (! _bfd_link_section_stabs (abfd
,
2290 & hash_table
->stab_info
,
2292 &secdata
->sec_info
))
2294 if (secdata
->sec_info
)
2295 secdata
->sec_info_type
= ELF_INFO_TYPE_STABS
;
2300 if (! info
->relocateable
&& ! dynamic
2301 && is_elf_hash_table (info
))
2305 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2306 if ((s
->flags
& SEC_MERGE
) != 0
2307 && !bfd_is_abs_section (s
->output_section
))
2309 struct bfd_elf_section_data
*secdata
;
2311 secdata
= elf_section_data (s
);
2312 if (! _bfd_merge_section (abfd
,
2313 & hash_table
->merge_info
,
2314 s
, &secdata
->sec_info
))
2316 else if (secdata
->sec_info
)
2317 secdata
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
2321 if (is_elf_hash_table (info
))
2323 /* Add this bfd to the loaded list. */
2324 struct elf_link_loaded_list
*n
;
2326 n
= ((struct elf_link_loaded_list
*)
2327 bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
)));
2331 n
->next
= hash_table
->loaded
;
2332 hash_table
->loaded
= n
;
2342 if (extversym
!= NULL
)
2347 /* Create some sections which will be filled in with dynamic linking
2348 information. ABFD is an input file which requires dynamic sections
2349 to be created. The dynamic sections take up virtual memory space
2350 when the final executable is run, so we need to create them before
2351 addresses are assigned to the output sections. We work out the
2352 actual contents and size of these sections later. */
2355 elf_link_create_dynamic_sections (abfd
, info
)
2357 struct bfd_link_info
*info
;
2360 register asection
*s
;
2361 struct elf_link_hash_entry
*h
;
2362 struct elf_backend_data
*bed
;
2364 if (! is_elf_hash_table (info
))
2367 if (elf_hash_table (info
)->dynamic_sections_created
)
2370 /* Make sure that all dynamic sections use the same input BFD. */
2371 if (elf_hash_table (info
)->dynobj
== NULL
)
2372 elf_hash_table (info
)->dynobj
= abfd
;
2374 abfd
= elf_hash_table (info
)->dynobj
;
2376 /* Note that we set the SEC_IN_MEMORY flag for all of these
2378 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
2379 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
2381 /* A dynamically linked executable has a .interp section, but a
2382 shared library does not. */
2385 s
= bfd_make_section (abfd
, ".interp");
2387 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2391 if (! info
->traditional_format
2392 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
2394 s
= bfd_make_section (abfd
, ".eh_frame_hdr");
2396 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2397 || ! bfd_set_section_alignment (abfd
, s
, 2))
2401 /* Create sections to hold version informations. These are removed
2402 if they are not needed. */
2403 s
= bfd_make_section (abfd
, ".gnu.version_d");
2405 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2406 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2409 s
= bfd_make_section (abfd
, ".gnu.version");
2411 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2412 || ! bfd_set_section_alignment (abfd
, s
, 1))
2415 s
= bfd_make_section (abfd
, ".gnu.version_r");
2417 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2418 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2421 s
= bfd_make_section (abfd
, ".dynsym");
2423 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2424 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2427 s
= bfd_make_section (abfd
, ".dynstr");
2429 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2432 /* Create a strtab to hold the dynamic symbol names. */
2433 if (elf_hash_table (info
)->dynstr
== NULL
)
2435 elf_hash_table (info
)->dynstr
= _bfd_elf_strtab_init ();
2436 if (elf_hash_table (info
)->dynstr
== NULL
)
2440 s
= bfd_make_section (abfd
, ".dynamic");
2442 || ! bfd_set_section_flags (abfd
, s
, flags
)
2443 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2446 /* The special symbol _DYNAMIC is always set to the start of the
2447 .dynamic section. This call occurs before we have processed the
2448 symbols for any dynamic object, so we don't have to worry about
2449 overriding a dynamic definition. We could set _DYNAMIC in a
2450 linker script, but we only want to define it if we are, in fact,
2451 creating a .dynamic section. We don't want to define it if there
2452 is no .dynamic section, since on some ELF platforms the start up
2453 code examines it to decide how to initialize the process. */
2455 if (! (_bfd_generic_link_add_one_symbol
2456 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
2457 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
2458 (struct bfd_link_hash_entry
**) &h
)))
2460 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2461 h
->type
= STT_OBJECT
;
2464 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2467 bed
= get_elf_backend_data (abfd
);
2469 s
= bfd_make_section (abfd
, ".hash");
2471 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2472 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2474 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
2476 /* Let the backend create the rest of the sections. This lets the
2477 backend set the right flags. The backend will normally create
2478 the .got and .plt sections. */
2479 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
2482 elf_hash_table (info
)->dynamic_sections_created
= true;
2487 /* Add an entry to the .dynamic table. */
2490 elf_add_dynamic_entry (info
, tag
, val
)
2491 struct bfd_link_info
*info
;
2495 Elf_Internal_Dyn dyn
;
2498 bfd_size_type newsize
;
2499 bfd_byte
*newcontents
;
2501 if (! is_elf_hash_table (info
))
2504 dynobj
= elf_hash_table (info
)->dynobj
;
2506 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
2507 BFD_ASSERT (s
!= NULL
);
2509 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
2510 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
2511 if (newcontents
== NULL
)
2515 dyn
.d_un
.d_val
= val
;
2516 elf_swap_dyn_out (dynobj
, &dyn
,
2517 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
2519 s
->_raw_size
= newsize
;
2520 s
->contents
= newcontents
;
2525 /* Record a new local dynamic symbol. */
2528 elf_link_record_local_dynamic_symbol (info
, input_bfd
, input_indx
)
2529 struct bfd_link_info
*info
;
2533 struct elf_link_local_dynamic_entry
*entry
;
2534 struct elf_link_hash_table
*eht
;
2535 struct elf_strtab_hash
*dynstr
;
2536 Elf_External_Sym esym
;
2537 Elf_External_Sym_Shndx eshndx
;
2538 Elf_External_Sym_Shndx
*shndx
;
2539 unsigned long dynstr_index
;
2544 if (! is_elf_hash_table (info
))
2547 /* See if the entry exists already. */
2548 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
2549 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
2552 entry
= (struct elf_link_local_dynamic_entry
*)
2553 bfd_alloc (input_bfd
, (bfd_size_type
) sizeof (*entry
));
2557 /* Go find the symbol, so that we can find it's name. */
2558 amt
= sizeof (Elf_External_Sym
);
2559 pos
= elf_tdata (input_bfd
)->symtab_hdr
.sh_offset
+ input_indx
* amt
;
2560 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
2561 || bfd_bread ((PTR
) &esym
, amt
, input_bfd
) != amt
)
2564 if (elf_tdata (input_bfd
)->symtab_shndx_hdr
.sh_size
!= 0)
2566 amt
= sizeof (Elf_External_Sym_Shndx
);
2567 pos
= elf_tdata (input_bfd
)->symtab_shndx_hdr
.sh_offset
;
2568 pos
+= input_indx
* amt
;
2570 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
2571 || bfd_bread ((PTR
) shndx
, amt
, input_bfd
) != amt
)
2574 elf_swap_symbol_in (input_bfd
, (const PTR
) &esym
, (const PTR
) shndx
,
2577 name
= (bfd_elf_string_from_elf_section
2578 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
2579 entry
->isym
.st_name
));
2581 dynstr
= elf_hash_table (info
)->dynstr
;
2584 /* Create a strtab to hold the dynamic symbol names. */
2585 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
2590 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, false);
2591 if (dynstr_index
== (unsigned long) -1)
2593 entry
->isym
.st_name
= dynstr_index
;
2595 eht
= elf_hash_table (info
);
2597 entry
->next
= eht
->dynlocal
;
2598 eht
->dynlocal
= entry
;
2599 entry
->input_bfd
= input_bfd
;
2600 entry
->input_indx
= input_indx
;
2603 /* Whatever binding the symbol had before, it's now local. */
2605 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
2607 /* The dynindx will be set at the end of size_dynamic_sections. */
2612 /* Read and swap the relocs from the section indicated by SHDR. This
2613 may be either a REL or a RELA section. The relocations are
2614 translated into RELA relocations and stored in INTERNAL_RELOCS,
2615 which should have already been allocated to contain enough space.
2616 The EXTERNAL_RELOCS are a buffer where the external form of the
2617 relocations should be stored.
2619 Returns false if something goes wrong. */
2622 elf_link_read_relocs_from_section (abfd
, shdr
, external_relocs
,
2625 Elf_Internal_Shdr
*shdr
;
2626 PTR external_relocs
;
2627 Elf_Internal_Rela
*internal_relocs
;
2629 struct elf_backend_data
*bed
;
2632 /* If there aren't any relocations, that's OK. */
2636 /* Position ourselves at the start of the section. */
2637 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2640 /* Read the relocations. */
2641 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2644 bed
= get_elf_backend_data (abfd
);
2646 /* Convert the external relocations to the internal format. */
2647 if (shdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2649 Elf_External_Rel
*erel
;
2650 Elf_External_Rel
*erelend
;
2651 Elf_Internal_Rela
*irela
;
2652 Elf_Internal_Rel
*irel
;
2654 erel
= (Elf_External_Rel
*) external_relocs
;
2655 erelend
= erel
+ NUM_SHDR_ENTRIES (shdr
);
2656 irela
= internal_relocs
;
2657 amt
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
2658 irel
= bfd_alloc (abfd
, amt
);
2659 for (; erel
< erelend
; erel
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2663 if (bed
->s
->swap_reloc_in
)
2664 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
2666 elf_swap_reloc_in (abfd
, erel
, irel
);
2668 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; ++i
)
2670 irela
[i
].r_offset
= irel
[i
].r_offset
;
2671 irela
[i
].r_info
= irel
[i
].r_info
;
2672 irela
[i
].r_addend
= 0;
2678 Elf_External_Rela
*erela
;
2679 Elf_External_Rela
*erelaend
;
2680 Elf_Internal_Rela
*irela
;
2682 BFD_ASSERT (shdr
->sh_entsize
== sizeof (Elf_External_Rela
));
2684 erela
= (Elf_External_Rela
*) external_relocs
;
2685 erelaend
= erela
+ NUM_SHDR_ENTRIES (shdr
);
2686 irela
= internal_relocs
;
2687 for (; erela
< erelaend
; erela
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2689 if (bed
->s
->swap_reloca_in
)
2690 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
2692 elf_swap_reloca_in (abfd
, erela
, irela
);
2699 /* Read and swap the relocs for a section O. They may have been
2700 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2701 not NULL, they are used as buffers to read into. They are known to
2702 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2703 the return value is allocated using either malloc or bfd_alloc,
2704 according to the KEEP_MEMORY argument. If O has two relocation
2705 sections (both REL and RELA relocations), then the REL_HDR
2706 relocations will appear first in INTERNAL_RELOCS, followed by the
2707 REL_HDR2 relocations. */
2710 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
2714 PTR external_relocs
;
2715 Elf_Internal_Rela
*internal_relocs
;
2716 boolean keep_memory
;
2718 Elf_Internal_Shdr
*rel_hdr
;
2720 Elf_Internal_Rela
*alloc2
= NULL
;
2721 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2723 if (elf_section_data (o
)->relocs
!= NULL
)
2724 return elf_section_data (o
)->relocs
;
2726 if (o
->reloc_count
== 0)
2729 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2731 if (internal_relocs
== NULL
)
2735 size
= o
->reloc_count
;
2736 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2738 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2740 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2741 if (internal_relocs
== NULL
)
2745 if (external_relocs
== NULL
)
2747 bfd_size_type size
= rel_hdr
->sh_size
;
2749 if (elf_section_data (o
)->rel_hdr2
)
2750 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2751 alloc1
= (PTR
) bfd_malloc (size
);
2754 external_relocs
= alloc1
;
2757 if (!elf_link_read_relocs_from_section (abfd
, rel_hdr
,
2761 if (!elf_link_read_relocs_from_section
2763 elf_section_data (o
)->rel_hdr2
,
2764 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2765 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2766 * bed
->s
->int_rels_per_ext_rel
)))
2769 /* Cache the results for next time, if we can. */
2771 elf_section_data (o
)->relocs
= internal_relocs
;
2776 /* Don't free alloc2, since if it was allocated we are passing it
2777 back (under the name of internal_relocs). */
2779 return internal_relocs
;
2789 /* Record an assignment to a symbol made by a linker script. We need
2790 this in case some dynamic object refers to this symbol. */
2793 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
2794 bfd
*output_bfd ATTRIBUTE_UNUSED
;
2795 struct bfd_link_info
*info
;
2799 struct elf_link_hash_entry
*h
;
2801 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2804 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, true, false);
2808 if (h
->root
.type
== bfd_link_hash_new
)
2809 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_ELF
;
2811 /* If this symbol is being provided by the linker script, and it is
2812 currently defined by a dynamic object, but not by a regular
2813 object, then mark it as undefined so that the generic linker will
2814 force the correct value. */
2816 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2817 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2818 h
->root
.type
= bfd_link_hash_undefined
;
2820 /* If this symbol is not being provided by the linker script, and it is
2821 currently defined by a dynamic object, but not by a regular object,
2822 then clear out any version information because the symbol will not be
2823 associated with the dynamic object any more. */
2825 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2826 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2827 h
->verinfo
.verdef
= NULL
;
2829 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2831 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2832 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
2834 && h
->dynindx
== -1)
2836 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2839 /* If this is a weak defined symbol, and we know a corresponding
2840 real symbol from the same dynamic object, make sure the real
2841 symbol is also made into a dynamic symbol. */
2842 if (h
->weakdef
!= NULL
2843 && h
->weakdef
->dynindx
== -1)
2845 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2853 /* This structure is used to pass information to
2854 elf_link_assign_sym_version. */
2856 struct elf_assign_sym_version_info
2860 /* General link information. */
2861 struct bfd_link_info
*info
;
2863 struct bfd_elf_version_tree
*verdefs
;
2864 /* Whether we had a failure. */
2868 /* This structure is used to pass information to
2869 elf_link_find_version_dependencies. */
2871 struct elf_find_verdep_info
2875 /* General link information. */
2876 struct bfd_link_info
*info
;
2877 /* The number of dependencies. */
2879 /* Whether we had a failure. */
2883 /* Array used to determine the number of hash table buckets to use
2884 based on the number of symbols there are. If there are fewer than
2885 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2886 fewer than 37 we use 17 buckets, and so forth. We never use more
2887 than 32771 buckets. */
2889 static const size_t elf_buckets
[] =
2891 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2895 /* Compute bucket count for hashing table. We do not use a static set
2896 of possible tables sizes anymore. Instead we determine for all
2897 possible reasonable sizes of the table the outcome (i.e., the
2898 number of collisions etc) and choose the best solution. The
2899 weighting functions are not too simple to allow the table to grow
2900 without bounds. Instead one of the weighting factors is the size.
2901 Therefore the result is always a good payoff between few collisions
2902 (= short chain lengths) and table size. */
2904 compute_bucket_count (info
)
2905 struct bfd_link_info
*info
;
2907 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2908 size_t best_size
= 0;
2909 unsigned long int *hashcodes
;
2910 unsigned long int *hashcodesp
;
2911 unsigned long int i
;
2914 /* Compute the hash values for all exported symbols. At the same
2915 time store the values in an array so that we could use them for
2918 amt
*= sizeof (unsigned long int);
2919 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
2920 if (hashcodes
== NULL
)
2922 hashcodesp
= hashcodes
;
2924 /* Put all hash values in HASHCODES. */
2925 elf_link_hash_traverse (elf_hash_table (info
),
2926 elf_collect_hash_codes
, &hashcodesp
);
2928 /* We have a problem here. The following code to optimize the table
2929 size requires an integer type with more the 32 bits. If
2930 BFD_HOST_U_64_BIT is set we know about such a type. */
2931 #ifdef BFD_HOST_U_64_BIT
2934 unsigned long int nsyms
= hashcodesp
- hashcodes
;
2937 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
2938 unsigned long int *counts
;
2940 /* Possible optimization parameters: if we have NSYMS symbols we say
2941 that the hashing table must at least have NSYMS/4 and at most
2943 minsize
= nsyms
/ 4;
2946 best_size
= maxsize
= nsyms
* 2;
2948 /* Create array where we count the collisions in. We must use bfd_malloc
2949 since the size could be large. */
2951 amt
*= sizeof (unsigned long int);
2952 counts
= (unsigned long int *) bfd_malloc (amt
);
2959 /* Compute the "optimal" size for the hash table. The criteria is a
2960 minimal chain length. The minor criteria is (of course) the size
2962 for (i
= minsize
; i
< maxsize
; ++i
)
2964 /* Walk through the array of hashcodes and count the collisions. */
2965 BFD_HOST_U_64_BIT max
;
2966 unsigned long int j
;
2967 unsigned long int fact
;
2969 memset (counts
, '\0', i
* sizeof (unsigned long int));
2971 /* Determine how often each hash bucket is used. */
2972 for (j
= 0; j
< nsyms
; ++j
)
2973 ++counts
[hashcodes
[j
] % i
];
2975 /* For the weight function we need some information about the
2976 pagesize on the target. This is information need not be 100%
2977 accurate. Since this information is not available (so far) we
2978 define it here to a reasonable default value. If it is crucial
2979 to have a better value some day simply define this value. */
2980 # ifndef BFD_TARGET_PAGESIZE
2981 # define BFD_TARGET_PAGESIZE (4096)
2984 /* We in any case need 2 + NSYMS entries for the size values and
2986 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
2989 /* Variant 1: optimize for short chains. We add the squares
2990 of all the chain lengths (which favous many small chain
2991 over a few long chains). */
2992 for (j
= 0; j
< i
; ++j
)
2993 max
+= counts
[j
] * counts
[j
];
2995 /* This adds penalties for the overall size of the table. */
2996 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2999 /* Variant 2: Optimize a lot more for small table. Here we
3000 also add squares of the size but we also add penalties for
3001 empty slots (the +1 term). */
3002 for (j
= 0; j
< i
; ++j
)
3003 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
3005 /* The overall size of the table is considered, but not as
3006 strong as in variant 1, where it is squared. */
3007 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
3011 /* Compare with current best results. */
3012 if (max
< best_chlen
)
3022 #endif /* defined (BFD_HOST_U_64_BIT) */
3024 /* This is the fallback solution if no 64bit type is available or if we
3025 are not supposed to spend much time on optimizations. We select the
3026 bucket count using a fixed set of numbers. */
3027 for (i
= 0; elf_buckets
[i
] != 0; i
++)
3029 best_size
= elf_buckets
[i
];
3030 if (dynsymcount
< elf_buckets
[i
+ 1])
3035 /* Free the arrays we needed. */
3041 /* Set up the sizes and contents of the ELF dynamic sections. This is
3042 called by the ELF linker emulation before_allocation routine. We
3043 must set the sizes of the sections before the linker sets the
3044 addresses of the various sections. */
3047 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
3049 auxiliary_filters
, info
, sinterpptr
,
3054 const char *filter_shlib
;
3055 const char * const *auxiliary_filters
;
3056 struct bfd_link_info
*info
;
3057 asection
**sinterpptr
;
3058 struct bfd_elf_version_tree
*verdefs
;
3060 bfd_size_type soname_indx
;
3062 struct elf_backend_data
*bed
;
3063 struct elf_assign_sym_version_info asvinfo
;
3067 soname_indx
= (bfd_size_type
) -1;
3069 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
3072 if (! is_elf_hash_table (info
))
3075 /* Any syms created from now on start with -1 in
3076 got.refcount/offset and plt.refcount/offset. */
3077 elf_hash_table (info
)->init_refcount
= -1;
3079 /* The backend may have to create some sections regardless of whether
3080 we're dynamic or not. */
3081 bed
= get_elf_backend_data (output_bfd
);
3082 if (bed
->elf_backend_always_size_sections
3083 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
3086 dynobj
= elf_hash_table (info
)->dynobj
;
3088 /* If there were no dynamic objects in the link, there is nothing to
3093 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
3096 if (elf_hash_table (info
)->dynamic_sections_created
)
3098 struct elf_info_failed eif
;
3099 struct elf_link_hash_entry
*h
;
3102 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
3103 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
3107 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3109 if (soname_indx
== (bfd_size_type
) -1
3110 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SONAME
,
3117 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMBOLIC
,
3120 info
->flags
|= DF_SYMBOLIC
;
3127 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
3129 if (info
->new_dtags
)
3130 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
3131 if (indx
== (bfd_size_type
) -1
3132 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RPATH
, indx
)
3134 && ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RUNPATH
,
3139 if (filter_shlib
!= NULL
)
3143 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3144 filter_shlib
, true);
3145 if (indx
== (bfd_size_type
) -1
3146 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FILTER
, indx
))
3150 if (auxiliary_filters
!= NULL
)
3152 const char * const *p
;
3154 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
3158 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3160 if (indx
== (bfd_size_type
) -1
3161 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_AUXILIARY
,
3168 eif
.verdefs
= verdefs
;
3171 /* If we are supposed to export all symbols into the dynamic symbol
3172 table (this is not the normal case), then do so. */
3173 if (info
->export_dynamic
)
3175 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
3181 /* Attach all the symbols to their version information. */
3182 asvinfo
.output_bfd
= output_bfd
;
3183 asvinfo
.info
= info
;
3184 asvinfo
.verdefs
= verdefs
;
3185 asvinfo
.failed
= false;
3187 elf_link_hash_traverse (elf_hash_table (info
),
3188 elf_link_assign_sym_version
,
3193 /* Find all symbols which were defined in a dynamic object and make
3194 the backend pick a reasonable value for them. */
3195 elf_link_hash_traverse (elf_hash_table (info
),
3196 elf_adjust_dynamic_symbol
,
3201 /* Add some entries to the .dynamic section. We fill in some of the
3202 values later, in elf_bfd_final_link, but we must add the entries
3203 now so that we know the final size of the .dynamic section. */
3205 /* If there are initialization and/or finalization functions to
3206 call then add the corresponding DT_INIT/DT_FINI entries. */
3207 h
= (info
->init_function
3208 ? elf_link_hash_lookup (elf_hash_table (info
),
3209 info
->init_function
, false,
3213 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3214 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3216 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT
, (bfd_vma
) 0))
3219 h
= (info
->fini_function
3220 ? elf_link_hash_lookup (elf_hash_table (info
),
3221 info
->fini_function
, false,
3225 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3226 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3228 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI
, (bfd_vma
) 0))
3232 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
3234 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3240 for (sub
= info
->input_bfds
; sub
!= NULL
;
3241 sub
= sub
->link_next
)
3242 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3243 if (elf_section_data (o
)->this_hdr
.sh_type
3244 == SHT_PREINIT_ARRAY
)
3246 (*_bfd_error_handler
)
3247 (_("%s: .preinit_array section is not allowed in DSO"),
3248 bfd_archive_filename (sub
));
3252 bfd_set_error (bfd_error_nonrepresentable_section
);
3256 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAY
,
3258 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAYSZ
,
3262 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
3264 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAY
,
3266 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAYSZ
,
3270 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
3272 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAY
,
3274 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAYSZ
,
3279 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
3280 /* If .dynstr is excluded from the link, we don't want any of
3281 these tags. Strictly, we should be checking each section
3282 individually; This quick check covers for the case where
3283 someone does a /DISCARD/ : { *(*) }. */
3284 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
3286 bfd_size_type strsize
;
3288 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3289 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_HASH
, (bfd_vma
) 0)
3290 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRTAB
, (bfd_vma
) 0)
3291 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMTAB
, (bfd_vma
) 0)
3292 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRSZ
, strsize
)
3293 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMENT
,
3294 (bfd_vma
) sizeof (Elf_External_Sym
)))
3299 /* The backend must work out the sizes of all the other dynamic
3301 if (bed
->elf_backend_size_dynamic_sections
3302 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
3305 if (elf_hash_table (info
)->dynamic_sections_created
)
3307 bfd_size_type dynsymcount
;
3309 size_t bucketcount
= 0;
3310 size_t hash_entry_size
;
3311 unsigned int dtagcount
;
3313 /* Set up the version definition section. */
3314 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3315 BFD_ASSERT (s
!= NULL
);
3317 /* We may have created additional version definitions if we are
3318 just linking a regular application. */
3319 verdefs
= asvinfo
.verdefs
;
3321 /* Skip anonymous version tag. */
3322 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
3323 verdefs
= verdefs
->next
;
3325 if (verdefs
== NULL
)
3326 _bfd_strip_section_from_output (info
, s
);
3331 struct bfd_elf_version_tree
*t
;
3333 Elf_Internal_Verdef def
;
3334 Elf_Internal_Verdaux defaux
;
3339 /* Make space for the base version. */
3340 size
+= sizeof (Elf_External_Verdef
);
3341 size
+= sizeof (Elf_External_Verdaux
);
3344 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3346 struct bfd_elf_version_deps
*n
;
3348 size
+= sizeof (Elf_External_Verdef
);
3349 size
+= sizeof (Elf_External_Verdaux
);
3352 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3353 size
+= sizeof (Elf_External_Verdaux
);
3356 s
->_raw_size
= size
;
3357 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3358 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3361 /* Fill in the version definition section. */
3365 def
.vd_version
= VER_DEF_CURRENT
;
3366 def
.vd_flags
= VER_FLG_BASE
;
3369 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3370 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3371 + sizeof (Elf_External_Verdaux
));
3373 if (soname_indx
!= (bfd_size_type
) -1)
3375 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3377 def
.vd_hash
= bfd_elf_hash (soname
);
3378 defaux
.vda_name
= soname_indx
;
3385 name
= basename (output_bfd
->filename
);
3386 def
.vd_hash
= bfd_elf_hash (name
);
3387 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3389 if (indx
== (bfd_size_type
) -1)
3391 defaux
.vda_name
= indx
;
3393 defaux
.vda_next
= 0;
3395 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3396 (Elf_External_Verdef
*) p
);
3397 p
+= sizeof (Elf_External_Verdef
);
3398 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3399 (Elf_External_Verdaux
*) p
);
3400 p
+= sizeof (Elf_External_Verdaux
);
3402 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3405 struct bfd_elf_version_deps
*n
;
3406 struct elf_link_hash_entry
*h
;
3409 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3412 /* Add a symbol representing this version. */
3414 if (! (_bfd_generic_link_add_one_symbol
3415 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
3416 (bfd_vma
) 0, (const char *) NULL
, false,
3417 get_elf_backend_data (dynobj
)->collect
,
3418 (struct bfd_link_hash_entry
**) &h
)))
3420 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
3421 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3422 h
->type
= STT_OBJECT
;
3423 h
->verinfo
.vertree
= t
;
3425 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
3428 def
.vd_version
= VER_DEF_CURRENT
;
3430 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
3431 def
.vd_flags
|= VER_FLG_WEAK
;
3432 def
.vd_ndx
= t
->vernum
+ 1;
3433 def
.vd_cnt
= cdeps
+ 1;
3434 def
.vd_hash
= bfd_elf_hash (t
->name
);
3435 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3436 if (t
->next
!= NULL
)
3437 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3438 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
3442 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3443 (Elf_External_Verdef
*) p
);
3444 p
+= sizeof (Elf_External_Verdef
);
3446 defaux
.vda_name
= h
->dynstr_index
;
3447 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3449 if (t
->deps
== NULL
)
3450 defaux
.vda_next
= 0;
3452 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3453 t
->name_indx
= defaux
.vda_name
;
3455 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3456 (Elf_External_Verdaux
*) p
);
3457 p
+= sizeof (Elf_External_Verdaux
);
3459 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3461 if (n
->version_needed
== NULL
)
3463 /* This can happen if there was an error in the
3465 defaux
.vda_name
= 0;
3469 defaux
.vda_name
= n
->version_needed
->name_indx
;
3470 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3473 if (n
->next
== NULL
)
3474 defaux
.vda_next
= 0;
3476 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3478 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3479 (Elf_External_Verdaux
*) p
);
3480 p
+= sizeof (Elf_External_Verdaux
);
3484 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEF
, (bfd_vma
) 0)
3485 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEFNUM
,
3489 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
3492 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
3494 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS
, info
->flags
))
3501 info
->flags_1
&= ~ (DF_1_INITFIRST
3504 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS_1
,
3509 /* Work out the size of the version reference section. */
3511 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3512 BFD_ASSERT (s
!= NULL
);
3514 struct elf_find_verdep_info sinfo
;
3516 sinfo
.output_bfd
= output_bfd
;
3518 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
3519 if (sinfo
.vers
== 0)
3521 sinfo
.failed
= false;
3523 elf_link_hash_traverse (elf_hash_table (info
),
3524 elf_link_find_version_dependencies
,
3527 if (elf_tdata (output_bfd
)->verref
== NULL
)
3528 _bfd_strip_section_from_output (info
, s
);
3531 Elf_Internal_Verneed
*t
;
3536 /* Build the version definition section. */
3539 for (t
= elf_tdata (output_bfd
)->verref
;
3543 Elf_Internal_Vernaux
*a
;
3545 size
+= sizeof (Elf_External_Verneed
);
3547 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3548 size
+= sizeof (Elf_External_Vernaux
);
3551 s
->_raw_size
= size
;
3552 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3553 if (s
->contents
== NULL
)
3557 for (t
= elf_tdata (output_bfd
)->verref
;
3562 Elf_Internal_Vernaux
*a
;
3566 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3569 t
->vn_version
= VER_NEED_CURRENT
;
3571 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3572 elf_dt_name (t
->vn_bfd
) != NULL
3573 ? elf_dt_name (t
->vn_bfd
)
3574 : basename (t
->vn_bfd
->filename
),
3576 if (indx
== (bfd_size_type
) -1)
3579 t
->vn_aux
= sizeof (Elf_External_Verneed
);
3580 if (t
->vn_nextref
== NULL
)
3583 t
->vn_next
= (sizeof (Elf_External_Verneed
)
3584 + caux
* sizeof (Elf_External_Vernaux
));
3586 _bfd_elf_swap_verneed_out (output_bfd
, t
,
3587 (Elf_External_Verneed
*) p
);
3588 p
+= sizeof (Elf_External_Verneed
);
3590 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3592 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
3593 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3594 a
->vna_nodename
, false);
3595 if (indx
== (bfd_size_type
) -1)
3598 if (a
->vna_nextptr
== NULL
)
3601 a
->vna_next
= sizeof (Elf_External_Vernaux
);
3603 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
3604 (Elf_External_Vernaux
*) p
);
3605 p
+= sizeof (Elf_External_Vernaux
);
3609 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEED
,
3611 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEEDNUM
,
3615 elf_tdata (output_bfd
)->cverrefs
= crefs
;
3619 /* Assign dynsym indicies. In a shared library we generate a
3620 section symbol for each output section, which come first.
3621 Next come all of the back-end allocated local dynamic syms,
3622 followed by the rest of the global symbols. */
3624 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3626 /* Work out the size of the symbol version section. */
3627 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3628 BFD_ASSERT (s
!= NULL
);
3629 if (dynsymcount
== 0
3630 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
3632 _bfd_strip_section_from_output (info
, s
);
3633 /* The DYNSYMCOUNT might have changed if we were going to
3634 output a dynamic symbol table entry for S. */
3635 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3639 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
3640 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3641 if (s
->contents
== NULL
)
3644 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERSYM
, (bfd_vma
) 0))
3648 /* Set the size of the .dynsym and .hash sections. We counted
3649 the number of dynamic symbols in elf_link_add_object_symbols.
3650 We will build the contents of .dynsym and .hash when we build
3651 the final symbol table, because until then we do not know the
3652 correct value to give the symbols. We built the .dynstr
3653 section as we went along in elf_link_add_object_symbols. */
3654 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
3655 BFD_ASSERT (s
!= NULL
);
3656 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
3657 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3658 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3661 if (dynsymcount
!= 0)
3663 Elf_Internal_Sym isym
;
3665 /* The first entry in .dynsym is a dummy symbol. */
3672 elf_swap_symbol_out (output_bfd
, &isym
, (PTR
) s
->contents
, (PTR
) 0);
3675 /* Compute the size of the hashing table. As a side effect this
3676 computes the hash values for all the names we export. */
3677 bucketcount
= compute_bucket_count (info
);
3679 s
= bfd_get_section_by_name (dynobj
, ".hash");
3680 BFD_ASSERT (s
!= NULL
);
3681 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
3682 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
3683 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3684 if (s
->contents
== NULL
)
3687 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) bucketcount
,
3689 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) dynsymcount
,
3690 s
->contents
+ hash_entry_size
);
3692 elf_hash_table (info
)->bucketcount
= bucketcount
;
3694 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
3695 BFD_ASSERT (s
!= NULL
);
3697 elf_finalize_dynstr (output_bfd
, info
);
3699 s
->_raw_size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3701 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
3702 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NULL
, (bfd_vma
) 0))
3709 /* This function is used to adjust offsets into .dynstr for
3710 dynamic symbols. This is called via elf_link_hash_traverse. */
3712 static boolean elf_adjust_dynstr_offsets
3713 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3716 elf_adjust_dynstr_offsets (h
, data
)
3717 struct elf_link_hash_entry
*h
;
3720 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3722 if (h
->root
.type
== bfd_link_hash_warning
)
3723 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3725 if (h
->dynindx
!= -1)
3726 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3730 /* Assign string offsets in .dynstr, update all structures referencing
3734 elf_finalize_dynstr (output_bfd
, info
)
3736 struct bfd_link_info
*info
;
3738 struct elf_link_local_dynamic_entry
*entry
;
3739 struct elf_strtab_hash
*dynstr
= elf_hash_table (info
)->dynstr
;
3740 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
3743 Elf_External_Dyn
*dyncon
, *dynconend
;
3745 _bfd_elf_strtab_finalize (dynstr
);
3746 size
= _bfd_elf_strtab_size (dynstr
);
3748 /* Update all .dynamic entries referencing .dynstr strings. */
3749 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3750 BFD_ASSERT (sdyn
!= NULL
);
3752 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
3753 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
3755 for (; dyncon
< dynconend
; dyncon
++)
3757 Elf_Internal_Dyn dyn
;
3759 elf_swap_dyn_in (dynobj
, dyncon
, & dyn
);
3763 dyn
.d_un
.d_val
= size
;
3764 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3772 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3773 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3780 /* Now update local dynamic symbols. */
3781 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
3782 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3783 entry
->isym
.st_name
);
3785 /* And the rest of dynamic symbols. */
3786 elf_link_hash_traverse (elf_hash_table (info
),
3787 elf_adjust_dynstr_offsets
, dynstr
);
3789 /* Adjust version definitions. */
3790 if (elf_tdata (output_bfd
)->cverdefs
)
3795 Elf_Internal_Verdef def
;
3796 Elf_Internal_Verdaux defaux
;
3798 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3799 p
= (bfd_byte
*) s
->contents
;
3802 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3804 p
+= sizeof (Elf_External_Verdef
);
3805 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3807 _bfd_elf_swap_verdaux_in (output_bfd
,
3808 (Elf_External_Verdaux
*) p
, &defaux
);
3809 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3811 _bfd_elf_swap_verdaux_out (output_bfd
,
3812 &defaux
, (Elf_External_Verdaux
*) p
);
3813 p
+= sizeof (Elf_External_Verdaux
);
3816 while (def
.vd_next
);
3819 /* Adjust version references. */
3820 if (elf_tdata (output_bfd
)->verref
)
3825 Elf_Internal_Verneed need
;
3826 Elf_Internal_Vernaux needaux
;
3828 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3829 p
= (bfd_byte
*) s
->contents
;
3832 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3834 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3835 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3836 (Elf_External_Verneed
*) p
);
3837 p
+= sizeof (Elf_External_Verneed
);
3838 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3840 _bfd_elf_swap_vernaux_in (output_bfd
,
3841 (Elf_External_Vernaux
*) p
, &needaux
);
3842 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3844 _bfd_elf_swap_vernaux_out (output_bfd
,
3846 (Elf_External_Vernaux
*) p
);
3847 p
+= sizeof (Elf_External_Vernaux
);
3850 while (need
.vn_next
);
3856 /* Fix up the flags for a symbol. This handles various cases which
3857 can only be fixed after all the input files are seen. This is
3858 currently called by both adjust_dynamic_symbol and
3859 assign_sym_version, which is unnecessary but perhaps more robust in
3860 the face of future changes. */
3863 elf_fix_symbol_flags (h
, eif
)
3864 struct elf_link_hash_entry
*h
;
3865 struct elf_info_failed
*eif
;
3867 /* If this symbol was mentioned in a non-ELF file, try to set
3868 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3869 permit a non-ELF file to correctly refer to a symbol defined in
3870 an ELF dynamic object. */
3871 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
3873 while (h
->root
.type
== bfd_link_hash_indirect
)
3874 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3876 if (h
->root
.type
!= bfd_link_hash_defined
3877 && h
->root
.type
!= bfd_link_hash_defweak
)
3878 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3879 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3882 if (h
->root
.u
.def
.section
->owner
!= NULL
3883 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3884 == bfd_target_elf_flavour
))
3885 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3886 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3888 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3891 if (h
->dynindx
== -1
3892 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
3893 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
3895 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3904 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3905 was first seen in a non-ELF file. Fortunately, if the symbol
3906 was first seen in an ELF file, we're probably OK unless the
3907 symbol was defined in a non-ELF file. Catch that case here.
3908 FIXME: We're still in trouble if the symbol was first seen in
3909 a dynamic object, and then later in a non-ELF regular object. */
3910 if ((h
->root
.type
== bfd_link_hash_defined
3911 || h
->root
.type
== bfd_link_hash_defweak
)
3912 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3913 && (h
->root
.u
.def
.section
->owner
!= NULL
3914 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3915 != bfd_target_elf_flavour
)
3916 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3917 && (h
->elf_link_hash_flags
3918 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
3919 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3922 /* If this is a final link, and the symbol was defined as a common
3923 symbol in a regular object file, and there was no definition in
3924 any dynamic object, then the linker will have allocated space for
3925 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3926 flag will not have been set. */
3927 if (h
->root
.type
== bfd_link_hash_defined
3928 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3929 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
3930 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3931 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3932 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3934 /* If -Bsymbolic was used (which means to bind references to global
3935 symbols to the definition within the shared object), and this
3936 symbol was defined in a regular object, then it actually doesn't
3937 need a PLT entry, and we can accomplish that by forcing it local.
3938 Likewise, if the symbol has hidden or internal visibility.
3939 FIXME: It might be that we also do not need a PLT for other
3940 non-hidden visibilities, but we would have to tell that to the
3941 backend specifically; we can't just clear PLT-related data here. */
3942 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
3943 && eif
->info
->shared
3944 && is_elf_hash_table (eif
->info
)
3945 && (eif
->info
->symbolic
3946 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3947 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3948 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3950 struct elf_backend_data
*bed
;
3951 boolean force_local
;
3953 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3955 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3956 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
3957 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
3960 /* If this is a weak defined symbol in a dynamic object, and we know
3961 the real definition in the dynamic object, copy interesting flags
3962 over to the real definition. */
3963 if (h
->weakdef
!= NULL
)
3965 struct elf_link_hash_entry
*weakdef
;
3967 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3968 || h
->root
.type
== bfd_link_hash_defweak
);
3969 weakdef
= h
->weakdef
;
3970 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
3971 || weakdef
->root
.type
== bfd_link_hash_defweak
);
3972 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
3974 /* If the real definition is defined by a regular object file,
3975 don't do anything special. See the longer description in
3976 elf_adjust_dynamic_symbol, below. */
3977 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3981 struct elf_backend_data
*bed
;
3983 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3984 (*bed
->elf_backend_copy_indirect_symbol
) (weakdef
, h
);
3991 /* Make the backend pick a good value for a dynamic symbol. This is
3992 called via elf_link_hash_traverse, and also calls itself
3996 elf_adjust_dynamic_symbol (h
, data
)
3997 struct elf_link_hash_entry
*h
;
4000 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
4002 struct elf_backend_data
*bed
;
4004 if (h
->root
.type
== bfd_link_hash_warning
)
4006 h
->plt
.offset
= (bfd_vma
) -1;
4007 h
->got
.offset
= (bfd_vma
) -1;
4009 /* When warning symbols are created, they **replace** the "real"
4010 entry in the hash table, thus we never get to see the real
4011 symbol in a hash traversal. So look at it now. */
4012 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4015 /* Ignore indirect symbols. These are added by the versioning code. */
4016 if (h
->root
.type
== bfd_link_hash_indirect
)
4019 if (! is_elf_hash_table (eif
->info
))
4022 /* Fix the symbol flags. */
4023 if (! elf_fix_symbol_flags (h
, eif
))
4026 /* If this symbol does not require a PLT entry, and it is not
4027 defined by a dynamic object, or is not referenced by a regular
4028 object, ignore it. We do have to handle a weak defined symbol,
4029 even if no regular object refers to it, if we decided to add it
4030 to the dynamic symbol table. FIXME: Do we normally need to worry
4031 about symbols which are defined by one dynamic object and
4032 referenced by another one? */
4033 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
4034 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4035 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4036 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
4037 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
4039 h
->plt
.offset
= (bfd_vma
) -1;
4043 /* If we've already adjusted this symbol, don't do it again. This
4044 can happen via a recursive call. */
4045 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
4048 /* Don't look at this symbol again. Note that we must set this
4049 after checking the above conditions, because we may look at a
4050 symbol once, decide not to do anything, and then get called
4051 recursively later after REF_REGULAR is set below. */
4052 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
4054 /* If this is a weak definition, and we know a real definition, and
4055 the real symbol is not itself defined by a regular object file,
4056 then get a good value for the real definition. We handle the
4057 real symbol first, for the convenience of the backend routine.
4059 Note that there is a confusing case here. If the real definition
4060 is defined by a regular object file, we don't get the real symbol
4061 from the dynamic object, but we do get the weak symbol. If the
4062 processor backend uses a COPY reloc, then if some routine in the
4063 dynamic object changes the real symbol, we will not see that
4064 change in the corresponding weak symbol. This is the way other
4065 ELF linkers work as well, and seems to be a result of the shared
4068 I will clarify this issue. Most SVR4 shared libraries define the
4069 variable _timezone and define timezone as a weak synonym. The
4070 tzset call changes _timezone. If you write
4071 extern int timezone;
4073 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4074 you might expect that, since timezone is a synonym for _timezone,
4075 the same number will print both times. However, if the processor
4076 backend uses a COPY reloc, then actually timezone will be copied
4077 into your process image, and, since you define _timezone
4078 yourself, _timezone will not. Thus timezone and _timezone will
4079 wind up at different memory locations. The tzset call will set
4080 _timezone, leaving timezone unchanged. */
4082 if (h
->weakdef
!= NULL
)
4084 /* If we get to this point, we know there is an implicit
4085 reference by a regular object file via the weak symbol H.
4086 FIXME: Is this really true? What if the traversal finds
4087 H->WEAKDEF before it finds H? */
4088 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
4090 if (! elf_adjust_dynamic_symbol (h
->weakdef
, (PTR
) eif
))
4094 /* If a symbol has no type and no size and does not require a PLT
4095 entry, then we are probably about to do the wrong thing here: we
4096 are probably going to create a COPY reloc for an empty object.
4097 This case can arise when a shared object is built with assembly
4098 code, and the assembly code fails to set the symbol type. */
4100 && h
->type
== STT_NOTYPE
4101 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
4102 (*_bfd_error_handler
)
4103 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4104 h
->root
.root
.string
);
4106 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
4107 bed
= get_elf_backend_data (dynobj
);
4108 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
4117 /* This routine is used to export all defined symbols into the dynamic
4118 symbol table. It is called via elf_link_hash_traverse. */
4121 elf_export_symbol (h
, data
)
4122 struct elf_link_hash_entry
*h
;
4125 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
4127 /* Ignore indirect symbols. These are added by the versioning code. */
4128 if (h
->root
.type
== bfd_link_hash_indirect
)
4131 if (h
->root
.type
== bfd_link_hash_warning
)
4132 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4134 if (h
->dynindx
== -1
4135 && (h
->elf_link_hash_flags
4136 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
4138 struct bfd_elf_version_tree
*t
;
4139 struct bfd_elf_version_expr
*d
;
4141 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
4143 if (t
->globals
!= NULL
)
4145 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4147 if ((*d
->match
) (d
, h
->root
.root
.string
))
4152 if (t
->locals
!= NULL
)
4154 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4156 if ((*d
->match
) (d
, h
->root
.root
.string
))
4165 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
4176 /* Look through the symbols which are defined in other shared
4177 libraries and referenced here. Update the list of version
4178 dependencies. This will be put into the .gnu.version_r section.
4179 This function is called via elf_link_hash_traverse. */
4182 elf_link_find_version_dependencies (h
, data
)
4183 struct elf_link_hash_entry
*h
;
4186 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
4187 Elf_Internal_Verneed
*t
;
4188 Elf_Internal_Vernaux
*a
;
4191 if (h
->root
.type
== bfd_link_hash_warning
)
4192 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4194 /* We only care about symbols defined in shared objects with version
4196 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4197 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4199 || h
->verinfo
.verdef
== NULL
)
4202 /* See if we already know about this version. */
4203 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
4205 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
4208 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4209 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
4215 /* This is a new version. Add it to tree we are building. */
4220 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4223 rinfo
->failed
= true;
4227 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
4228 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
4229 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
4233 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4235 /* Note that we are copying a string pointer here, and testing it
4236 above. If bfd_elf_string_from_elf_section is ever changed to
4237 discard the string data when low in memory, this will have to be
4239 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
4241 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
4242 a
->vna_nextptr
= t
->vn_auxptr
;
4244 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
4247 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4254 /* Figure out appropriate versions for all the symbols. We may not
4255 have the version number script until we have read all of the input
4256 files, so until that point we don't know which symbols should be
4257 local. This function is called via elf_link_hash_traverse. */
4260 elf_link_assign_sym_version (h
, data
)
4261 struct elf_link_hash_entry
*h
;
4264 struct elf_assign_sym_version_info
*sinfo
;
4265 struct bfd_link_info
*info
;
4266 struct elf_backend_data
*bed
;
4267 struct elf_info_failed eif
;
4271 sinfo
= (struct elf_assign_sym_version_info
*) data
;
4274 if (h
->root
.type
== bfd_link_hash_warning
)
4275 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4277 /* Fix the symbol flags. */
4280 if (! elf_fix_symbol_flags (h
, &eif
))
4283 sinfo
->failed
= true;
4287 /* We only need version numbers for symbols defined in regular
4289 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4292 bed
= get_elf_backend_data (sinfo
->output_bfd
);
4293 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4294 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
4296 struct bfd_elf_version_tree
*t
;
4301 /* There are two consecutive ELF_VER_CHR characters if this is
4302 not a hidden symbol. */
4304 if (*p
== ELF_VER_CHR
)
4310 /* If there is no version string, we can just return out. */
4314 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4318 /* Look for the version. If we find it, it is no longer weak. */
4319 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4321 if (strcmp (t
->name
, p
) == 0)
4325 struct bfd_elf_version_expr
*d
;
4327 len
= p
- h
->root
.root
.string
;
4328 alc
= bfd_malloc ((bfd_size_type
) len
);
4331 memcpy (alc
, h
->root
.root
.string
, len
- 1);
4332 alc
[len
- 1] = '\0';
4333 if (alc
[len
- 2] == ELF_VER_CHR
)
4334 alc
[len
- 2] = '\0';
4336 h
->verinfo
.vertree
= t
;
4340 if (t
->globals
!= NULL
)
4342 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4343 if ((*d
->match
) (d
, alc
))
4347 /* See if there is anything to force this symbol to
4349 if (d
== NULL
&& t
->locals
!= NULL
)
4351 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4353 if ((*d
->match
) (d
, alc
))
4355 if (h
->dynindx
!= -1
4357 && ! info
->export_dynamic
)
4359 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
4372 /* If we are building an application, we need to create a
4373 version node for this version. */
4374 if (t
== NULL
&& ! info
->shared
)
4376 struct bfd_elf_version_tree
**pp
;
4379 /* If we aren't going to export this symbol, we don't need
4380 to worry about it. */
4381 if (h
->dynindx
== -1)
4385 t
= ((struct bfd_elf_version_tree
*)
4386 bfd_alloc (sinfo
->output_bfd
, amt
));
4389 sinfo
->failed
= true;
4398 t
->name_indx
= (unsigned int) -1;
4402 /* Don't count anonymous version tag. */
4403 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
4405 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4407 t
->vernum
= version_index
;
4411 h
->verinfo
.vertree
= t
;
4415 /* We could not find the version for a symbol when
4416 generating a shared archive. Return an error. */
4417 (*_bfd_error_handler
)
4418 (_("%s: undefined versioned symbol name %s"),
4419 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
4420 bfd_set_error (bfd_error_bad_value
);
4421 sinfo
->failed
= true;
4426 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4429 /* If we don't have a version for this symbol, see if we can find
4431 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
4433 struct bfd_elf_version_tree
*t
;
4434 struct bfd_elf_version_tree
*deflt
;
4435 struct bfd_elf_version_expr
*d
;
4437 /* See if can find what version this symbol is in. If the
4438 symbol is supposed to be local, then don't actually register
4441 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4443 if (t
->globals
!= NULL
)
4445 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4447 if ((*d
->match
) (d
, h
->root
.root
.string
))
4449 h
->verinfo
.vertree
= t
;
4458 if (t
->locals
!= NULL
)
4460 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4462 if (d
->pattern
[0] == '*' && d
->pattern
[1] == '\0')
4464 else if ((*d
->match
) (d
, h
->root
.root
.string
))
4466 h
->verinfo
.vertree
= t
;
4467 if (h
->dynindx
!= -1
4469 && ! info
->export_dynamic
)
4471 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
4482 if (deflt
!= NULL
&& h
->verinfo
.vertree
== NULL
)
4484 h
->verinfo
.vertree
= deflt
;
4485 if (h
->dynindx
!= -1
4487 && ! info
->export_dynamic
)
4489 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
4497 /* Final phase of ELF linker. */
4499 /* A structure we use to avoid passing large numbers of arguments. */
4501 struct elf_final_link_info
4503 /* General link information. */
4504 struct bfd_link_info
*info
;
4507 /* Symbol string table. */
4508 struct bfd_strtab_hash
*symstrtab
;
4509 /* .dynsym section. */
4510 asection
*dynsym_sec
;
4511 /* .hash section. */
4513 /* symbol version section (.gnu.version). */
4514 asection
*symver_sec
;
4515 /* first SHF_TLS section (if any). */
4516 asection
*first_tls_sec
;
4517 /* Buffer large enough to hold contents of any section. */
4519 /* Buffer large enough to hold external relocs of any section. */
4520 PTR external_relocs
;
4521 /* Buffer large enough to hold internal relocs of any section. */
4522 Elf_Internal_Rela
*internal_relocs
;
4523 /* Buffer large enough to hold external local symbols of any input
4525 Elf_External_Sym
*external_syms
;
4526 /* And a buffer for symbol section indices. */
4527 Elf_External_Sym_Shndx
*locsym_shndx
;
4528 /* Buffer large enough to hold internal local symbols of any input
4530 Elf_Internal_Sym
*internal_syms
;
4531 /* Array large enough to hold a symbol index for each local symbol
4532 of any input BFD. */
4534 /* Array large enough to hold a section pointer for each local
4535 symbol of any input BFD. */
4536 asection
**sections
;
4537 /* Buffer to hold swapped out symbols. */
4538 Elf_External_Sym
*symbuf
;
4539 /* And one for symbol section indices. */
4540 Elf_External_Sym_Shndx
*symshndxbuf
;
4541 /* Number of swapped out symbols in buffer. */
4542 size_t symbuf_count
;
4543 /* Number of symbols which fit in symbuf. */
4547 static boolean elf_link_output_sym
4548 PARAMS ((struct elf_final_link_info
*, const char *,
4549 Elf_Internal_Sym
*, asection
*));
4550 static boolean elf_link_flush_output_syms
4551 PARAMS ((struct elf_final_link_info
*));
4552 static boolean elf_link_output_extsym
4553 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4554 static boolean elf_link_sec_merge_syms
4555 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4556 static boolean elf_link_check_versioned_symbol
4557 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
4558 static boolean elf_link_input_bfd
4559 PARAMS ((struct elf_final_link_info
*, bfd
*));
4560 static boolean elf_reloc_link_order
4561 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
4562 struct bfd_link_order
*));
4564 /* This struct is used to pass information to elf_link_output_extsym. */
4566 struct elf_outext_info
4570 struct elf_final_link_info
*finfo
;
4573 /* Compute the size of, and allocate space for, REL_HDR which is the
4574 section header for a section containing relocations for O. */
4577 elf_link_size_reloc_section (abfd
, rel_hdr
, o
)
4579 Elf_Internal_Shdr
*rel_hdr
;
4582 bfd_size_type reloc_count
;
4583 bfd_size_type num_rel_hashes
;
4585 /* Figure out how many relocations there will be. */
4586 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
4587 reloc_count
= elf_section_data (o
)->rel_count
;
4589 reloc_count
= elf_section_data (o
)->rel_count2
;
4591 num_rel_hashes
= o
->reloc_count
;
4592 if (num_rel_hashes
< reloc_count
)
4593 num_rel_hashes
= reloc_count
;
4595 /* That allows us to calculate the size of the section. */
4596 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
4598 /* The contents field must last into write_object_contents, so we
4599 allocate it with bfd_alloc rather than malloc. Also since we
4600 cannot be sure that the contents will actually be filled in,
4601 we zero the allocated space. */
4602 rel_hdr
->contents
= (PTR
) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
4603 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
4606 /* We only allocate one set of hash entries, so we only do it the
4607 first time we are called. */
4608 if (elf_section_data (o
)->rel_hashes
== NULL
4611 struct elf_link_hash_entry
**p
;
4613 p
= ((struct elf_link_hash_entry
**)
4614 bfd_zmalloc (num_rel_hashes
4615 * sizeof (struct elf_link_hash_entry
*)));
4619 elf_section_data (o
)->rel_hashes
= p
;
4625 /* When performing a relocateable link, the input relocations are
4626 preserved. But, if they reference global symbols, the indices
4627 referenced must be updated. Update all the relocations in
4628 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4631 elf_link_adjust_relocs (abfd
, rel_hdr
, count
, rel_hash
)
4633 Elf_Internal_Shdr
*rel_hdr
;
4635 struct elf_link_hash_entry
**rel_hash
;
4638 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4639 Elf_Internal_Rel
*irel
;
4640 Elf_Internal_Rela
*irela
;
4641 bfd_size_type amt
= sizeof (Elf_Internal_Rel
) * bed
->s
->int_rels_per_ext_rel
;
4643 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (amt
);
4646 (*_bfd_error_handler
) (_("Error: out of memory"));
4650 amt
= sizeof (Elf_Internal_Rela
) * bed
->s
->int_rels_per_ext_rel
;
4651 irela
= (Elf_Internal_Rela
*) bfd_zmalloc (amt
);
4654 (*_bfd_error_handler
) (_("Error: out of memory"));
4658 for (i
= 0; i
< count
; i
++, rel_hash
++)
4660 if (*rel_hash
== NULL
)
4663 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
4665 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4667 Elf_External_Rel
*erel
;
4670 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
4671 if (bed
->s
->swap_reloc_in
)
4672 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
4674 elf_swap_reloc_in (abfd
, erel
, irel
);
4676 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4677 irel
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4678 ELF_R_TYPE (irel
[j
].r_info
));
4680 if (bed
->s
->swap_reloc_out
)
4681 (*bed
->s
->swap_reloc_out
) (abfd
, irel
, (bfd_byte
*) erel
);
4683 elf_swap_reloc_out (abfd
, irel
, erel
);
4687 Elf_External_Rela
*erela
;
4690 BFD_ASSERT (rel_hdr
->sh_entsize
4691 == sizeof (Elf_External_Rela
));
4693 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
4694 if (bed
->s
->swap_reloca_in
)
4695 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
4697 elf_swap_reloca_in (abfd
, erela
, irela
);
4699 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4700 irela
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4701 ELF_R_TYPE (irela
[j
].r_info
));
4703 if (bed
->s
->swap_reloca_out
)
4704 (*bed
->s
->swap_reloca_out
) (abfd
, irela
, (bfd_byte
*) erela
);
4706 elf_swap_reloca_out (abfd
, irela
, erela
);
4714 struct elf_link_sort_rela
{
4716 enum elf_reloc_type_class type
;
4718 Elf_Internal_Rel rel
;
4719 Elf_Internal_Rela rela
;
4724 elf_link_sort_cmp1 (A
, B
)
4728 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4729 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4730 int relativea
, relativeb
;
4732 relativea
= a
->type
== reloc_class_relative
;
4733 relativeb
= b
->type
== reloc_class_relative
;
4735 if (relativea
< relativeb
)
4737 if (relativea
> relativeb
)
4739 if (ELF_R_SYM (a
->u
.rel
.r_info
) < ELF_R_SYM (b
->u
.rel
.r_info
))
4741 if (ELF_R_SYM (a
->u
.rel
.r_info
) > ELF_R_SYM (b
->u
.rel
.r_info
))
4743 if (a
->u
.rel
.r_offset
< b
->u
.rel
.r_offset
)
4745 if (a
->u
.rel
.r_offset
> b
->u
.rel
.r_offset
)
4751 elf_link_sort_cmp2 (A
, B
)
4755 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4756 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4759 if (a
->offset
< b
->offset
)
4761 if (a
->offset
> b
->offset
)
4763 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
4764 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
4769 if (a
->u
.rel
.r_offset
< b
->u
.rel
.r_offset
)
4771 if (a
->u
.rel
.r_offset
> b
->u
.rel
.r_offset
)
4777 elf_link_sort_relocs (abfd
, info
, psec
)
4779 struct bfd_link_info
*info
;
4782 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4783 asection
*reldyn
, *o
;
4784 boolean rel
= false;
4785 bfd_size_type count
, size
;
4787 struct elf_link_sort_rela
*rela
;
4788 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4790 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
4791 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4793 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
4794 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4797 count
= reldyn
->_raw_size
/ sizeof (Elf_External_Rel
);
4800 count
= reldyn
->_raw_size
/ sizeof (Elf_External_Rela
);
4803 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4804 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4805 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4806 && o
->output_section
== reldyn
)
4807 size
+= o
->_raw_size
;
4809 if (size
!= reldyn
->_raw_size
)
4812 rela
= (struct elf_link_sort_rela
*) bfd_zmalloc (sizeof (*rela
) * count
);
4815 (*info
->callbacks
->warning
)
4816 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0,
4821 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4822 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4823 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4824 && o
->output_section
== reldyn
)
4828 Elf_External_Rel
*erel
, *erelend
;
4829 struct elf_link_sort_rela
*s
;
4831 erel
= (Elf_External_Rel
*) o
->contents
;
4832 erelend
= (Elf_External_Rel
*) (o
->contents
+ o
->_raw_size
);
4833 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rel
);
4834 for (; erel
< erelend
; erel
++, s
++)
4836 if (bed
->s
->swap_reloc_in
)
4837 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, &s
->u
.rel
);
4839 elf_swap_reloc_in (abfd
, erel
, &s
->u
.rel
);
4841 s
->type
= (*bed
->elf_backend_reloc_type_class
) (&s
->u
.rela
);
4846 Elf_External_Rela
*erela
, *erelaend
;
4847 struct elf_link_sort_rela
*s
;
4849 erela
= (Elf_External_Rela
*) o
->contents
;
4850 erelaend
= (Elf_External_Rela
*) (o
->contents
+ o
->_raw_size
);
4851 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rela
);
4852 for (; erela
< erelaend
; erela
++, s
++)
4854 if (bed
->s
->swap_reloca_in
)
4855 (*bed
->s
->swap_reloca_in
) (dynobj
, (bfd_byte
*) erela
,
4858 elf_swap_reloca_in (dynobj
, erela
, &s
->u
.rela
);
4860 s
->type
= (*bed
->elf_backend_reloc_type_class
) (&s
->u
.rela
);
4865 qsort (rela
, (size_t) count
, sizeof (*rela
), elf_link_sort_cmp1
);
4866 for (ret
= 0; ret
< count
&& rela
[ret
].type
== reloc_class_relative
; ret
++)
4868 for (i
= ret
, j
= ret
; i
< count
; i
++)
4870 if (ELF_R_SYM (rela
[i
].u
.rel
.r_info
) != ELF_R_SYM (rela
[j
].u
.rel
.r_info
))
4872 rela
[i
].offset
= rela
[j
].u
.rel
.r_offset
;
4874 qsort (rela
+ ret
, (size_t) count
- ret
, sizeof (*rela
), elf_link_sort_cmp2
);
4876 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4877 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4878 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4879 && o
->output_section
== reldyn
)
4883 Elf_External_Rel
*erel
, *erelend
;
4884 struct elf_link_sort_rela
*s
;
4886 erel
= (Elf_External_Rel
*) o
->contents
;
4887 erelend
= (Elf_External_Rel
*) (o
->contents
+ o
->_raw_size
);
4888 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rel
);
4889 for (; erel
< erelend
; erel
++, s
++)
4891 if (bed
->s
->swap_reloc_out
)
4892 (*bed
->s
->swap_reloc_out
) (abfd
, &s
->u
.rel
,
4895 elf_swap_reloc_out (abfd
, &s
->u
.rel
, erel
);
4900 Elf_External_Rela
*erela
, *erelaend
;
4901 struct elf_link_sort_rela
*s
;
4903 erela
= (Elf_External_Rela
*) o
->contents
;
4904 erelaend
= (Elf_External_Rela
*) (o
->contents
+ o
->_raw_size
);
4905 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rela
);
4906 for (; erela
< erelaend
; erela
++, s
++)
4908 if (bed
->s
->swap_reloca_out
)
4909 (*bed
->s
->swap_reloca_out
) (dynobj
, &s
->u
.rela
,
4910 (bfd_byte
*) erela
);
4912 elf_swap_reloca_out (dynobj
, &s
->u
.rela
, erela
);
4922 /* Do the final step of an ELF link. */
4925 elf_bfd_final_link (abfd
, info
)
4927 struct bfd_link_info
*info
;
4930 boolean emit_relocs
;
4932 struct elf_final_link_info finfo
;
4933 register asection
*o
;
4934 register struct bfd_link_order
*p
;
4936 bfd_size_type max_contents_size
;
4937 bfd_size_type max_external_reloc_size
;
4938 bfd_size_type max_internal_reloc_count
;
4939 bfd_size_type max_sym_count
;
4940 bfd_size_type max_sym_shndx_count
;
4942 Elf_Internal_Sym elfsym
;
4944 Elf_Internal_Shdr
*symtab_hdr
;
4945 Elf_Internal_Shdr
*symstrtab_hdr
;
4946 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4947 struct elf_outext_info eoinfo
;
4949 size_t relativecount
= 0;
4950 asection
*reldyn
= 0;
4953 if (! is_elf_hash_table (info
))
4957 abfd
->flags
|= DYNAMIC
;
4959 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
4960 dynobj
= elf_hash_table (info
)->dynobj
;
4962 emit_relocs
= (info
->relocateable
4963 || info
->emitrelocations
4964 || bed
->elf_backend_emit_relocs
);
4967 finfo
.output_bfd
= abfd
;
4968 finfo
.symstrtab
= elf_stringtab_init ();
4969 if (finfo
.symstrtab
== NULL
)
4974 finfo
.dynsym_sec
= NULL
;
4975 finfo
.hash_sec
= NULL
;
4976 finfo
.symver_sec
= NULL
;
4980 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
4981 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
4982 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
4983 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
4984 /* Note that it is OK if symver_sec is NULL. */
4987 finfo
.contents
= NULL
;
4988 finfo
.external_relocs
= NULL
;
4989 finfo
.internal_relocs
= NULL
;
4990 finfo
.external_syms
= NULL
;
4991 finfo
.locsym_shndx
= NULL
;
4992 finfo
.internal_syms
= NULL
;
4993 finfo
.indices
= NULL
;
4994 finfo
.sections
= NULL
;
4995 finfo
.symbuf
= NULL
;
4996 finfo
.symshndxbuf
= NULL
;
4997 finfo
.symbuf_count
= 0;
4998 finfo
.first_tls_sec
= NULL
;
4999 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
5000 if ((o
->flags
& SEC_THREAD_LOCAL
) != 0
5001 && (o
->flags
& SEC_LOAD
) != 0)
5003 finfo
.first_tls_sec
= o
;
5007 /* Count up the number of relocations we will output for each output
5008 section, so that we know the sizes of the reloc sections. We
5009 also figure out some maximum sizes. */
5010 max_contents_size
= 0;
5011 max_external_reloc_size
= 0;
5012 max_internal_reloc_count
= 0;
5014 max_sym_shndx_count
= 0;
5016 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
5020 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5022 if (p
->type
== bfd_section_reloc_link_order
5023 || p
->type
== bfd_symbol_reloc_link_order
)
5025 else if (p
->type
== bfd_indirect_link_order
)
5029 sec
= p
->u
.indirect
.section
;
5031 /* Mark all sections which are to be included in the
5032 link. This will normally be every section. We need
5033 to do this so that we can identify any sections which
5034 the linker has decided to not include. */
5035 sec
->linker_mark
= true;
5037 if (sec
->flags
& SEC_MERGE
)
5040 if (info
->relocateable
|| info
->emitrelocations
)
5041 o
->reloc_count
+= sec
->reloc_count
;
5042 else if (bed
->elf_backend_count_relocs
)
5044 Elf_Internal_Rela
* relocs
;
5046 relocs
= (NAME(_bfd_elf
,link_read_relocs
)
5047 (abfd
, sec
, (PTR
) NULL
,
5048 (Elf_Internal_Rela
*) NULL
, info
->keep_memory
));
5051 += (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
5053 if (!info
->keep_memory
)
5057 if (sec
->_raw_size
> max_contents_size
)
5058 max_contents_size
= sec
->_raw_size
;
5059 if (sec
->_cooked_size
> max_contents_size
)
5060 max_contents_size
= sec
->_cooked_size
;
5062 /* We are interested in just local symbols, not all
5064 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
5065 && (sec
->owner
->flags
& DYNAMIC
) == 0)
5069 if (elf_bad_symtab (sec
->owner
))
5070 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
5071 / sizeof (Elf_External_Sym
));
5073 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
5075 if (sym_count
> max_sym_count
)
5076 max_sym_count
= sym_count
;
5078 if (sym_count
> max_sym_shndx_count
5079 && elf_symtab_shndx (sec
->owner
) != 0)
5080 max_sym_shndx_count
= sym_count
;
5082 if ((sec
->flags
& SEC_RELOC
) != 0)
5086 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
5087 if (ext_size
> max_external_reloc_size
)
5088 max_external_reloc_size
= ext_size
;
5089 if (sec
->reloc_count
> max_internal_reloc_count
)
5090 max_internal_reloc_count
= sec
->reloc_count
;
5096 if (o
->reloc_count
> 0)
5097 o
->flags
|= SEC_RELOC
;
5100 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5101 set it (this is probably a bug) and if it is set
5102 assign_section_numbers will create a reloc section. */
5103 o
->flags
&=~ SEC_RELOC
;
5106 /* If the SEC_ALLOC flag is not set, force the section VMA to
5107 zero. This is done in elf_fake_sections as well, but forcing
5108 the VMA to 0 here will ensure that relocs against these
5109 sections are handled correctly. */
5110 if ((o
->flags
& SEC_ALLOC
) == 0
5111 && ! o
->user_set_vma
)
5115 if (! info
->relocateable
&& merged
)
5116 elf_link_hash_traverse (elf_hash_table (info
),
5117 elf_link_sec_merge_syms
, (PTR
) abfd
);
5119 /* Figure out the file positions for everything but the symbol table
5120 and the relocs. We set symcount to force assign_section_numbers
5121 to create a symbol table. */
5122 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
5123 BFD_ASSERT (! abfd
->output_has_begun
);
5124 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
5127 /* Figure out how many relocations we will have in each section.
5128 Just using RELOC_COUNT isn't good enough since that doesn't
5129 maintain a separate value for REL vs. RELA relocations. */
5131 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5132 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5134 asection
*output_section
;
5136 if (! o
->linker_mark
)
5138 /* This section was omitted from the link. */
5142 output_section
= o
->output_section
;
5144 if (output_section
!= NULL
5145 && (o
->flags
& SEC_RELOC
) != 0)
5147 struct bfd_elf_section_data
*esdi
5148 = elf_section_data (o
);
5149 struct bfd_elf_section_data
*esdo
5150 = elf_section_data (output_section
);
5151 unsigned int *rel_count
;
5152 unsigned int *rel_count2
;
5153 bfd_size_type entsize
;
5154 bfd_size_type entsize2
;
5156 /* We must be careful to add the relocations from the
5157 input section to the right output count. */
5158 entsize
= esdi
->rel_hdr
.sh_entsize
;
5159 entsize2
= esdi
->rel_hdr2
? esdi
->rel_hdr2
->sh_entsize
: 0;
5160 BFD_ASSERT ((entsize
== sizeof (Elf_External_Rel
)
5161 || entsize
== sizeof (Elf_External_Rela
))
5162 && entsize2
!= entsize
5164 || entsize2
== sizeof (Elf_External_Rel
)
5165 || entsize2
== sizeof (Elf_External_Rela
)));
5166 if (entsize
== esdo
->rel_hdr
.sh_entsize
)
5168 rel_count
= &esdo
->rel_count
;
5169 rel_count2
= &esdo
->rel_count2
;
5173 rel_count
= &esdo
->rel_count2
;
5174 rel_count2
= &esdo
->rel_count
;
5177 *rel_count
+= NUM_SHDR_ENTRIES (& esdi
->rel_hdr
);
5179 *rel_count2
+= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
5180 output_section
->flags
|= SEC_RELOC
;
5184 /* That created the reloc sections. Set their sizes, and assign
5185 them file positions, and allocate some buffers. */
5186 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5188 if ((o
->flags
& SEC_RELOC
) != 0)
5190 if (!elf_link_size_reloc_section (abfd
,
5191 &elf_section_data (o
)->rel_hdr
,
5195 if (elf_section_data (o
)->rel_hdr2
5196 && !elf_link_size_reloc_section (abfd
,
5197 elf_section_data (o
)->rel_hdr2
,
5202 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5203 to count upwards while actually outputting the relocations. */
5204 elf_section_data (o
)->rel_count
= 0;
5205 elf_section_data (o
)->rel_count2
= 0;
5208 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5210 /* We have now assigned file positions for all the sections except
5211 .symtab and .strtab. We start the .symtab section at the current
5212 file position, and write directly to it. We build the .strtab
5213 section in memory. */
5214 bfd_get_symcount (abfd
) = 0;
5215 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5216 /* sh_name is set in prep_headers. */
5217 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5218 symtab_hdr
->sh_flags
= 0;
5219 symtab_hdr
->sh_addr
= 0;
5220 symtab_hdr
->sh_size
= 0;
5221 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
5222 /* sh_link is set in assign_section_numbers. */
5223 /* sh_info is set below. */
5224 /* sh_offset is set just below. */
5225 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
5227 off
= elf_tdata (abfd
)->next_file_pos
;
5228 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
5230 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5231 incorrect. We do not yet know the size of the .symtab section.
5232 We correct next_file_pos below, after we do know the size. */
5234 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5235 continuously seeking to the right position in the file. */
5236 if (! info
->keep_memory
|| max_sym_count
< 20)
5237 finfo
.symbuf_size
= 20;
5239 finfo
.symbuf_size
= max_sym_count
;
5240 amt
= finfo
.symbuf_size
;
5241 amt
*= sizeof (Elf_External_Sym
);
5242 finfo
.symbuf
= (Elf_External_Sym
*) bfd_malloc (amt
);
5243 if (finfo
.symbuf
== NULL
)
5245 if (elf_numsections (abfd
) > SHN_LORESERVE
)
5247 amt
= finfo
.symbuf_size
;
5248 amt
*= sizeof (Elf_External_Sym_Shndx
);
5249 finfo
.symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
5250 if (finfo
.symshndxbuf
== NULL
)
5254 /* Start writing out the symbol table. The first symbol is always a
5256 if (info
->strip
!= strip_all
5259 elfsym
.st_value
= 0;
5262 elfsym
.st_other
= 0;
5263 elfsym
.st_shndx
= SHN_UNDEF
;
5264 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5265 &elfsym
, bfd_und_section_ptr
))
5270 /* Some standard ELF linkers do this, but we don't because it causes
5271 bootstrap comparison failures. */
5272 /* Output a file symbol for the output file as the second symbol.
5273 We output this even if we are discarding local symbols, although
5274 I'm not sure if this is correct. */
5275 elfsym
.st_value
= 0;
5277 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5278 elfsym
.st_other
= 0;
5279 elfsym
.st_shndx
= SHN_ABS
;
5280 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
5281 &elfsym
, bfd_abs_section_ptr
))
5285 /* Output a symbol for each section. We output these even if we are
5286 discarding local symbols, since they are used for relocs. These
5287 symbols have no names. We store the index of each one in the
5288 index field of the section, so that we can find it again when
5289 outputting relocs. */
5290 if (info
->strip
!= strip_all
5294 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5295 elfsym
.st_other
= 0;
5296 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5298 o
= section_from_elf_index (abfd
, i
);
5300 o
->target_index
= bfd_get_symcount (abfd
);
5301 elfsym
.st_shndx
= i
;
5302 if (info
->relocateable
|| o
== NULL
)
5303 elfsym
.st_value
= 0;
5305 elfsym
.st_value
= o
->vma
;
5306 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5309 if (i
== SHN_LORESERVE
)
5310 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
5314 /* Allocate some memory to hold information read in from the input
5316 if (max_contents_size
!= 0)
5318 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
5319 if (finfo
.contents
== NULL
)
5323 if (max_external_reloc_size
!= 0)
5325 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
5326 if (finfo
.external_relocs
== NULL
)
5330 if (max_internal_reloc_count
!= 0)
5332 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5333 amt
*= sizeof (Elf_Internal_Rela
);
5334 finfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
5335 if (finfo
.internal_relocs
== NULL
)
5339 if (max_sym_count
!= 0)
5341 amt
= max_sym_count
* sizeof (Elf_External_Sym
);
5342 finfo
.external_syms
= (Elf_External_Sym
*) bfd_malloc (amt
);
5343 if (finfo
.external_syms
== NULL
)
5346 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
5347 finfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
5348 if (finfo
.internal_syms
== NULL
)
5351 amt
= max_sym_count
* sizeof (long);
5352 finfo
.indices
= (long *) bfd_malloc (amt
);
5353 if (finfo
.indices
== NULL
)
5356 amt
= max_sym_count
* sizeof (asection
*);
5357 finfo
.sections
= (asection
**) bfd_malloc (amt
);
5358 if (finfo
.sections
== NULL
)
5362 if (max_sym_shndx_count
!= 0)
5364 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
5365 finfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
5366 if (finfo
.locsym_shndx
== NULL
)
5370 if (finfo
.first_tls_sec
)
5372 unsigned int align
= 0;
5373 bfd_vma base
= finfo
.first_tls_sec
->vma
, end
= 0;
5376 for (sec
= finfo
.first_tls_sec
;
5377 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
5380 bfd_vma size
= sec
->_raw_size
;
5382 if (bfd_get_section_alignment (abfd
, sec
) > align
)
5383 align
= bfd_get_section_alignment (abfd
, sec
);
5384 if (sec
->_raw_size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
5386 struct bfd_link_order
*o
;
5389 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
5390 if (size
< o
->offset
+ o
->size
)
5391 size
= o
->offset
+ o
->size
;
5393 end
= sec
->vma
+ size
;
5395 elf_hash_table (info
)->tls_segment
5396 = bfd_zalloc (abfd
, sizeof (struct elf_link_tls_segment
));
5397 if (elf_hash_table (info
)->tls_segment
== NULL
)
5399 elf_hash_table (info
)->tls_segment
->start
= base
;
5400 elf_hash_table (info
)->tls_segment
->size
= end
- base
;
5401 elf_hash_table (info
)->tls_segment
->align
= align
;
5404 /* Since ELF permits relocations to be against local symbols, we
5405 must have the local symbols available when we do the relocations.
5406 Since we would rather only read the local symbols once, and we
5407 would rather not keep them in memory, we handle all the
5408 relocations for a single input file at the same time.
5410 Unfortunately, there is no way to know the total number of local
5411 symbols until we have seen all of them, and the local symbol
5412 indices precede the global symbol indices. This means that when
5413 we are generating relocateable output, and we see a reloc against
5414 a global symbol, we can not know the symbol index until we have
5415 finished examining all the local symbols to see which ones we are
5416 going to output. To deal with this, we keep the relocations in
5417 memory, and don't output them until the end of the link. This is
5418 an unfortunate waste of memory, but I don't see a good way around
5419 it. Fortunately, it only happens when performing a relocateable
5420 link, which is not the common case. FIXME: If keep_memory is set
5421 we could write the relocs out and then read them again; I don't
5422 know how bad the memory loss will be. */
5424 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5425 sub
->output_has_begun
= false;
5426 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5428 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5430 if (p
->type
== bfd_indirect_link_order
5431 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
5432 == bfd_target_elf_flavour
)
5433 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
5435 if (! sub
->output_has_begun
)
5437 if (! elf_link_input_bfd (&finfo
, sub
))
5439 sub
->output_has_begun
= true;
5442 else if (p
->type
== bfd_section_reloc_link_order
5443 || p
->type
== bfd_symbol_reloc_link_order
)
5445 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
5450 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
5456 /* Output any global symbols that got converted to local in a
5457 version script or due to symbol visibility. We do this in a
5458 separate step since ELF requires all local symbols to appear
5459 prior to any global symbols. FIXME: We should only do this if
5460 some global symbols were, in fact, converted to become local.
5461 FIXME: Will this work correctly with the Irix 5 linker? */
5462 eoinfo
.failed
= false;
5463 eoinfo
.finfo
= &finfo
;
5464 eoinfo
.localsyms
= true;
5465 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5470 /* That wrote out all the local symbols. Finish up the symbol table
5471 with the global symbols. Even if we want to strip everything we
5472 can, we still need to deal with those global symbols that got
5473 converted to local in a version script. */
5475 /* The sh_info field records the index of the first non local symbol. */
5476 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
5479 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
5481 Elf_Internal_Sym sym
;
5482 Elf_External_Sym
*dynsym
=
5483 (Elf_External_Sym
*) finfo
.dynsym_sec
->contents
;
5484 long last_local
= 0;
5486 /* Write out the section symbols for the output sections. */
5493 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5496 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5499 Elf_External_Sym
*dest
;
5501 indx
= elf_section_data (s
)->this_idx
;
5502 BFD_ASSERT (indx
> 0);
5503 sym
.st_shndx
= indx
;
5504 sym
.st_value
= s
->vma
;
5505 dest
= dynsym
+ elf_section_data (s
)->dynindx
;
5506 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5509 last_local
= bfd_count_sections (abfd
);
5512 /* Write out the local dynsyms. */
5513 if (elf_hash_table (info
)->dynlocal
)
5515 struct elf_link_local_dynamic_entry
*e
;
5516 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
5519 Elf_External_Sym
*dest
;
5521 sym
.st_size
= e
->isym
.st_size
;
5522 sym
.st_other
= e
->isym
.st_other
;
5524 /* Copy the internal symbol as is.
5525 Note that we saved a word of storage and overwrote
5526 the original st_name with the dynstr_index. */
5529 if (e
->isym
.st_shndx
!= SHN_UNDEF
5530 && (e
->isym
.st_shndx
< SHN_LORESERVE
5531 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
5533 s
= bfd_section_from_elf_index (e
->input_bfd
,
5537 elf_section_data (s
->output_section
)->this_idx
;
5538 sym
.st_value
= (s
->output_section
->vma
5540 + e
->isym
.st_value
);
5543 if (last_local
< e
->dynindx
)
5544 last_local
= e
->dynindx
;
5546 dest
= dynsym
+ e
->dynindx
;
5547 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5551 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
5555 /* We get the global symbols from the hash table. */
5556 eoinfo
.failed
= false;
5557 eoinfo
.localsyms
= false;
5558 eoinfo
.finfo
= &finfo
;
5559 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5564 /* If backend needs to output some symbols not present in the hash
5565 table, do it now. */
5566 if (bed
->elf_backend_output_arch_syms
)
5568 typedef boolean (*out_sym_func
) PARAMS ((PTR
, const char *,
5572 if (! ((*bed
->elf_backend_output_arch_syms
)
5573 (abfd
, info
, (PTR
) &finfo
, (out_sym_func
) elf_link_output_sym
)))
5577 /* Flush all symbols to the file. */
5578 if (! elf_link_flush_output_syms (&finfo
))
5581 /* Now we know the size of the symtab section. */
5582 off
+= symtab_hdr
->sh_size
;
5584 /* Finish up and write out the symbol string table (.strtab)
5586 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5587 /* sh_name was set in prep_headers. */
5588 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5589 symstrtab_hdr
->sh_flags
= 0;
5590 symstrtab_hdr
->sh_addr
= 0;
5591 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
5592 symstrtab_hdr
->sh_entsize
= 0;
5593 symstrtab_hdr
->sh_link
= 0;
5594 symstrtab_hdr
->sh_info
= 0;
5595 /* sh_offset is set just below. */
5596 symstrtab_hdr
->sh_addralign
= 1;
5598 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, true);
5599 elf_tdata (abfd
)->next_file_pos
= off
;
5601 if (bfd_get_symcount (abfd
) > 0)
5603 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
5604 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
5608 /* Adjust the relocs to have the correct symbol indices. */
5609 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5611 if ((o
->flags
& SEC_RELOC
) == 0)
5614 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
5615 elf_section_data (o
)->rel_count
,
5616 elf_section_data (o
)->rel_hashes
);
5617 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
5618 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
5619 elf_section_data (o
)->rel_count2
,
5620 (elf_section_data (o
)->rel_hashes
5621 + elf_section_data (o
)->rel_count
));
5623 /* Set the reloc_count field to 0 to prevent write_relocs from
5624 trying to swap the relocs out itself. */
5628 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
5629 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
5631 /* If we are linking against a dynamic object, or generating a
5632 shared library, finish up the dynamic linking information. */
5635 Elf_External_Dyn
*dyncon
, *dynconend
;
5637 /* Fix up .dynamic entries. */
5638 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
5639 BFD_ASSERT (o
!= NULL
);
5641 dyncon
= (Elf_External_Dyn
*) o
->contents
;
5642 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
5643 for (; dyncon
< dynconend
; dyncon
++)
5645 Elf_Internal_Dyn dyn
;
5649 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
5656 if (relativecount
> 0 && dyncon
+ 1 < dynconend
)
5658 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
5660 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
5661 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
5664 if (dyn
.d_tag
!= DT_NULL
)
5666 dyn
.d_un
.d_val
= relativecount
;
5667 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5673 name
= info
->init_function
;
5676 name
= info
->fini_function
;
5679 struct elf_link_hash_entry
*h
;
5681 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
5682 false, false, true);
5684 && (h
->root
.type
== bfd_link_hash_defined
5685 || h
->root
.type
== bfd_link_hash_defweak
))
5687 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
5688 o
= h
->root
.u
.def
.section
;
5689 if (o
->output_section
!= NULL
)
5690 dyn
.d_un
.d_val
+= (o
->output_section
->vma
5691 + o
->output_offset
);
5694 /* The symbol is imported from another shared
5695 library and does not apply to this one. */
5699 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5704 case DT_PREINIT_ARRAYSZ
:
5705 name
= ".preinit_array";
5707 case DT_INIT_ARRAYSZ
:
5708 name
= ".init_array";
5710 case DT_FINI_ARRAYSZ
:
5711 name
= ".fini_array";
5713 o
= bfd_get_section_by_name (abfd
, name
);
5716 (*_bfd_error_handler
)
5717 (_("%s: could not find output section %s"),
5718 bfd_get_filename (abfd
), name
);
5721 if (o
->_raw_size
== 0)
5722 (*_bfd_error_handler
)
5723 (_("warning: %s section has zero size"), name
);
5724 dyn
.d_un
.d_val
= o
->_raw_size
;
5725 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5728 case DT_PREINIT_ARRAY
:
5729 name
= ".preinit_array";
5732 name
= ".init_array";
5735 name
= ".fini_array";
5748 name
= ".gnu.version_d";
5751 name
= ".gnu.version_r";
5754 name
= ".gnu.version";
5756 o
= bfd_get_section_by_name (abfd
, name
);
5759 (*_bfd_error_handler
)
5760 (_("%s: could not find output section %s"),
5761 bfd_get_filename (abfd
), name
);
5764 dyn
.d_un
.d_ptr
= o
->vma
;
5765 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5772 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
5777 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5779 Elf_Internal_Shdr
*hdr
;
5781 hdr
= elf_elfsections (abfd
)[i
];
5782 if (hdr
->sh_type
== type
5783 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
5785 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
5786 dyn
.d_un
.d_val
+= hdr
->sh_size
;
5789 if (dyn
.d_un
.d_val
== 0
5790 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
5791 dyn
.d_un
.d_val
= hdr
->sh_addr
;
5795 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5801 /* If we have created any dynamic sections, then output them. */
5804 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
5807 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
5809 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5810 || o
->_raw_size
== 0
5811 || o
->output_section
== bfd_abs_section_ptr
)
5813 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
5815 /* At this point, we are only interested in sections
5816 created by elf_link_create_dynamic_sections. */
5819 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
5821 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
5823 if (! bfd_set_section_contents (abfd
, o
->output_section
,
5825 (file_ptr
) o
->output_offset
,
5831 /* The contents of the .dynstr section are actually in a
5833 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
5834 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
5835 || ! _bfd_elf_strtab_emit (abfd
,
5836 elf_hash_table (info
)->dynstr
))
5842 if (info
->relocateable
)
5844 boolean failed
= false;
5846 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
5851 /* If we have optimized stabs strings, output them. */
5852 if (elf_hash_table (info
)->stab_info
!= NULL
)
5854 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
5858 if (info
->eh_frame_hdr
&& elf_hash_table (info
)->dynobj
)
5860 o
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
5863 && (elf_section_data (o
)->sec_info_type
5864 == ELF_INFO_TYPE_EH_FRAME_HDR
))
5866 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, o
))
5871 if (finfo
.symstrtab
!= NULL
)
5872 _bfd_stringtab_free (finfo
.symstrtab
);
5873 if (finfo
.contents
!= NULL
)
5874 free (finfo
.contents
);
5875 if (finfo
.external_relocs
!= NULL
)
5876 free (finfo
.external_relocs
);
5877 if (finfo
.internal_relocs
!= NULL
)
5878 free (finfo
.internal_relocs
);
5879 if (finfo
.external_syms
!= NULL
)
5880 free (finfo
.external_syms
);
5881 if (finfo
.locsym_shndx
!= NULL
)
5882 free (finfo
.locsym_shndx
);
5883 if (finfo
.internal_syms
!= NULL
)
5884 free (finfo
.internal_syms
);
5885 if (finfo
.indices
!= NULL
)
5886 free (finfo
.indices
);
5887 if (finfo
.sections
!= NULL
)
5888 free (finfo
.sections
);
5889 if (finfo
.symbuf
!= NULL
)
5890 free (finfo
.symbuf
);
5891 if (finfo
.symshndxbuf
!= NULL
)
5892 free (finfo
.symbuf
);
5893 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5895 if ((o
->flags
& SEC_RELOC
) != 0
5896 && elf_section_data (o
)->rel_hashes
!= NULL
)
5897 free (elf_section_data (o
)->rel_hashes
);
5900 elf_tdata (abfd
)->linker
= true;
5905 if (finfo
.symstrtab
!= NULL
)
5906 _bfd_stringtab_free (finfo
.symstrtab
);
5907 if (finfo
.contents
!= NULL
)
5908 free (finfo
.contents
);
5909 if (finfo
.external_relocs
!= NULL
)
5910 free (finfo
.external_relocs
);
5911 if (finfo
.internal_relocs
!= NULL
)
5912 free (finfo
.internal_relocs
);
5913 if (finfo
.external_syms
!= NULL
)
5914 free (finfo
.external_syms
);
5915 if (finfo
.locsym_shndx
!= NULL
)
5916 free (finfo
.locsym_shndx
);
5917 if (finfo
.internal_syms
!= NULL
)
5918 free (finfo
.internal_syms
);
5919 if (finfo
.indices
!= NULL
)
5920 free (finfo
.indices
);
5921 if (finfo
.sections
!= NULL
)
5922 free (finfo
.sections
);
5923 if (finfo
.symbuf
!= NULL
)
5924 free (finfo
.symbuf
);
5925 if (finfo
.symshndxbuf
!= NULL
)
5926 free (finfo
.symbuf
);
5927 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5929 if ((o
->flags
& SEC_RELOC
) != 0
5930 && elf_section_data (o
)->rel_hashes
!= NULL
)
5931 free (elf_section_data (o
)->rel_hashes
);
5937 /* Add a symbol to the output symbol table. */
5940 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
5941 struct elf_final_link_info
*finfo
;
5943 Elf_Internal_Sym
*elfsym
;
5944 asection
*input_sec
;
5946 Elf_External_Sym
*dest
;
5947 Elf_External_Sym_Shndx
*destshndx
;
5949 boolean (*output_symbol_hook
) PARAMS ((bfd
*,
5950 struct bfd_link_info
*info
,
5955 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
5956 elf_backend_link_output_symbol_hook
;
5957 if (output_symbol_hook
!= NULL
)
5959 if (! ((*output_symbol_hook
)
5960 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
5964 if (name
== (const char *) NULL
|| *name
== '\0')
5965 elfsym
->st_name
= 0;
5966 else if (input_sec
->flags
& SEC_EXCLUDE
)
5967 elfsym
->st_name
= 0;
5970 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5972 if (elfsym
->st_name
== (unsigned long) -1)
5976 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5978 if (! elf_link_flush_output_syms (finfo
))
5982 dest
= finfo
->symbuf
+ finfo
->symbuf_count
;
5983 destshndx
= finfo
->symshndxbuf
;
5984 if (destshndx
!= NULL
)
5985 destshndx
+= finfo
->symbuf_count
;
5986 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
, (PTR
) dest
, (PTR
) destshndx
);
5987 ++finfo
->symbuf_count
;
5989 ++ bfd_get_symcount (finfo
->output_bfd
);
5994 /* Flush the output symbols to the file. */
5997 elf_link_flush_output_syms (finfo
)
5998 struct elf_final_link_info
*finfo
;
6000 if (finfo
->symbuf_count
> 0)
6002 Elf_Internal_Shdr
*hdr
;
6006 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6007 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6008 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
6009 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6010 || bfd_bwrite ((PTR
) finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6013 hdr
->sh_size
+= amt
;
6015 if (finfo
->symshndxbuf
!= NULL
)
6017 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_shndx_hdr
;
6018 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6019 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym_Shndx
);
6020 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6021 || (bfd_bwrite ((PTR
) finfo
->symshndxbuf
, amt
, finfo
->output_bfd
)
6025 hdr
->sh_size
+= amt
;
6028 finfo
->symbuf_count
= 0;
6034 /* Adjust all external symbols pointing into SEC_MERGE sections
6035 to reflect the object merging within the sections. */
6038 elf_link_sec_merge_syms (h
, data
)
6039 struct elf_link_hash_entry
*h
;
6044 if (h
->root
.type
== bfd_link_hash_warning
)
6045 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6047 if ((h
->root
.type
== bfd_link_hash_defined
6048 || h
->root
.type
== bfd_link_hash_defweak
)
6049 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
6050 && elf_section_data (sec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
)
6052 bfd
*output_bfd
= (bfd
*) data
;
6054 h
->root
.u
.def
.value
=
6055 _bfd_merged_section_offset (output_bfd
,
6056 &h
->root
.u
.def
.section
,
6057 elf_section_data (sec
)->sec_info
,
6058 h
->root
.u
.def
.value
, (bfd_vma
) 0);
6064 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6065 allowing an unsatisfied unversioned symbol in the DSO to match a
6066 versioned symbol that would normally require an explicit version. */
6069 elf_link_check_versioned_symbol (info
, h
)
6070 struct bfd_link_info
*info
;
6071 struct elf_link_hash_entry
*h
;
6073 bfd
*undef_bfd
= h
->root
.u
.undef
.abfd
;
6074 struct elf_link_loaded_list
*loaded
;
6075 Elf_External_Sym
*buf
;
6076 Elf_External_Versym
*extversym
;
6078 if ((undef_bfd
->flags
& DYNAMIC
) == 0
6079 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
6080 || elf_dt_soname (h
->root
.u
.undef
.abfd
) == NULL
)
6083 for (loaded
= elf_hash_table (info
)->loaded
;
6085 loaded
= loaded
->next
)
6088 Elf_Internal_Shdr
*hdr
;
6089 bfd_size_type symcount
;
6090 bfd_size_type extsymcount
;
6091 bfd_size_type extsymoff
;
6092 Elf_Internal_Shdr
*versymhdr
;
6093 Elf_External_Versym
*ever
;
6094 Elf_External_Sym
*esym
;
6095 Elf_External_Sym
*esymend
;
6096 bfd_size_type count
;
6099 input
= loaded
->abfd
;
6101 /* We check each DSO for a possible hidden versioned definition. */
6102 if (input
== undef_bfd
6103 || (input
->flags
& DYNAMIC
) == 0
6104 || elf_dynversym (input
) == 0)
6107 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6109 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6110 if (elf_bad_symtab (input
))
6112 extsymcount
= symcount
;
6117 extsymcount
= symcount
- hdr
->sh_info
;
6118 extsymoff
= hdr
->sh_info
;
6121 if (extsymcount
== 0)
6124 count
= extsymcount
* sizeof (Elf_External_Sym
);
6125 buf
= (Elf_External_Sym
*) bfd_malloc (count
);
6129 /* Read in the symbol table. */
6130 pos
= hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
);
6131 if (bfd_seek (input
, pos
, SEEK_SET
) != 0
6132 || bfd_bread ((PTR
) buf
, count
, input
) != count
)
6135 /* Read in any version definitions. */
6136 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6137 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
6138 if (extversym
== NULL
)
6141 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6142 || (bfd_bread ((PTR
) extversym
, versymhdr
->sh_size
, input
)
6143 != versymhdr
->sh_size
))
6151 ever
= extversym
+ extsymoff
;
6152 esymend
= buf
+ extsymcount
;
6153 for (esym
= buf
; esym
< esymend
; esym
++, ever
++)
6156 Elf_Internal_Sym sym
;
6157 Elf_Internal_Versym iver
;
6159 elf_swap_symbol_in (input
, esym
, NULL
, &sym
);
6160 if (ELF_ST_BIND (sym
.st_info
) == STB_LOCAL
6161 || sym
.st_shndx
== SHN_UNDEF
)
6164 name
= bfd_elf_string_from_elf_section (input
,
6167 if (strcmp (name
, h
->root
.root
.string
) != 0)
6170 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6172 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6174 /* If we have a non-hidden versioned sym, then it should
6175 have provided a definition for the undefined sym. */
6179 if ((iver
.vs_vers
& VERSYM_VERSION
) == 2)
6181 /* This is the oldest (default) sym. We can use it. */
6195 /* Add an external symbol to the symbol table. This is called from
6196 the hash table traversal routine. When generating a shared object,
6197 we go through the symbol table twice. The first time we output
6198 anything that might have been forced to local scope in a version
6199 script. The second time we output the symbols that are still
6203 elf_link_output_extsym (h
, data
)
6204 struct elf_link_hash_entry
*h
;
6207 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
6208 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6210 Elf_Internal_Sym sym
;
6211 asection
*input_sec
;
6213 if (h
->root
.type
== bfd_link_hash_warning
)
6215 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6216 if (h
->root
.type
== bfd_link_hash_new
)
6220 /* Decide whether to output this symbol in this pass. */
6221 if (eoinfo
->localsyms
)
6223 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6228 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6232 /* If we are not creating a shared library, and this symbol is
6233 referenced by a shared library but is not defined anywhere, then
6234 warn that it is undefined. If we do not do this, the runtime
6235 linker will complain that the symbol is undefined when the
6236 program is run. We don't have to worry about symbols that are
6237 referenced by regular files, because we will already have issued
6238 warnings for them. */
6239 if (! finfo
->info
->relocateable
6240 && ! finfo
->info
->allow_shlib_undefined
6241 && ! finfo
->info
->shared
6242 && h
->root
.type
== bfd_link_hash_undefined
6243 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
6244 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
6245 && ! elf_link_check_versioned_symbol (finfo
->info
, h
))
6247 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6248 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6249 (asection
*) NULL
, (bfd_vma
) 0, true)))
6251 eoinfo
->failed
= true;
6256 /* We don't want to output symbols that have never been mentioned by
6257 a regular file, or that we have been told to strip. However, if
6258 h->indx is set to -2, the symbol is used by a reloc and we must
6262 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
6263 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
6264 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
6265 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
6267 else if (finfo
->info
->strip
== strip_all
6268 || (finfo
->info
->strip
== strip_some
6269 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6270 h
->root
.root
.string
,
6271 false, false) == NULL
))
6276 /* If we're stripping it, and it's not a dynamic symbol, there's
6277 nothing else to do unless it is a forced local symbol. */
6280 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6284 sym
.st_size
= h
->size
;
6285 sym
.st_other
= h
->other
;
6286 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6287 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6288 else if (h
->root
.type
== bfd_link_hash_undefweak
6289 || h
->root
.type
== bfd_link_hash_defweak
)
6290 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6292 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6294 switch (h
->root
.type
)
6297 case bfd_link_hash_new
:
6298 case bfd_link_hash_warning
:
6302 case bfd_link_hash_undefined
:
6303 case bfd_link_hash_undefweak
:
6304 input_sec
= bfd_und_section_ptr
;
6305 sym
.st_shndx
= SHN_UNDEF
;
6308 case bfd_link_hash_defined
:
6309 case bfd_link_hash_defweak
:
6311 input_sec
= h
->root
.u
.def
.section
;
6312 if (input_sec
->output_section
!= NULL
)
6315 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6316 input_sec
->output_section
);
6317 if (sym
.st_shndx
== SHN_BAD
)
6319 (*_bfd_error_handler
)
6320 (_("%s: could not find output section %s for input section %s"),
6321 bfd_get_filename (finfo
->output_bfd
),
6322 input_sec
->output_section
->name
,
6324 eoinfo
->failed
= true;
6328 /* ELF symbols in relocateable files are section relative,
6329 but in nonrelocateable files they are virtual
6331 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6332 if (! finfo
->info
->relocateable
)
6334 sym
.st_value
+= input_sec
->output_section
->vma
;
6335 if (h
->type
== STT_TLS
)
6337 /* STT_TLS symbols are relative to PT_TLS segment
6339 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6340 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
6346 BFD_ASSERT (input_sec
->owner
== NULL
6347 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6348 sym
.st_shndx
= SHN_UNDEF
;
6349 input_sec
= bfd_und_section_ptr
;
6354 case bfd_link_hash_common
:
6355 input_sec
= h
->root
.u
.c
.p
->section
;
6356 sym
.st_shndx
= SHN_COMMON
;
6357 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6360 case bfd_link_hash_indirect
:
6361 /* These symbols are created by symbol versioning. They point
6362 to the decorated version of the name. For example, if the
6363 symbol foo@@GNU_1.2 is the default, which should be used when
6364 foo is used with no version, then we add an indirect symbol
6365 foo which points to foo@@GNU_1.2. We ignore these symbols,
6366 since the indirected symbol is already in the hash table. */
6370 /* Give the processor backend a chance to tweak the symbol value,
6371 and also to finish up anything that needs to be done for this
6372 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6373 forced local syms when non-shared is due to a historical quirk. */
6374 if ((h
->dynindx
!= -1
6375 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6376 && (finfo
->info
->shared
6377 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6378 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6380 struct elf_backend_data
*bed
;
6382 bed
= get_elf_backend_data (finfo
->output_bfd
);
6383 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6384 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6386 eoinfo
->failed
= true;
6391 /* If we are marking the symbol as undefined, and there are no
6392 non-weak references to this symbol from a regular object, then
6393 mark the symbol as weak undefined; if there are non-weak
6394 references, mark the symbol as strong. We can't do this earlier,
6395 because it might not be marked as undefined until the
6396 finish_dynamic_symbol routine gets through with it. */
6397 if (sym
.st_shndx
== SHN_UNDEF
6398 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
6399 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6400 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6404 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
6405 bindtype
= STB_GLOBAL
;
6407 bindtype
= STB_WEAK
;
6408 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6411 /* If a symbol is not defined locally, we clear the visibility
6413 if (! finfo
->info
->relocateable
6414 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6415 sym
.st_other
^= ELF_ST_VISIBILITY (sym
.st_other
);
6417 /* If this symbol should be put in the .dynsym section, then put it
6418 there now. We already know the symbol index. We also fill in
6419 the entry in the .hash section. */
6420 if (h
->dynindx
!= -1
6421 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6425 size_t hash_entry_size
;
6426 bfd_byte
*bucketpos
;
6428 Elf_External_Sym
*esym
;
6430 sym
.st_name
= h
->dynstr_index
;
6431 esym
= (Elf_External_Sym
*) finfo
->dynsym_sec
->contents
+ h
->dynindx
;
6432 elf_swap_symbol_out (finfo
->output_bfd
, &sym
, (PTR
) esym
, (PTR
) 0);
6434 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6435 bucket
= h
->elf_hash_value
% bucketcount
;
6437 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6438 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6439 + (bucket
+ 2) * hash_entry_size
);
6440 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6441 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, (bfd_vma
) h
->dynindx
,
6443 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6444 ((bfd_byte
*) finfo
->hash_sec
->contents
6445 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6447 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6449 Elf_Internal_Versym iversym
;
6450 Elf_External_Versym
*eversym
;
6452 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6454 if (h
->verinfo
.verdef
== NULL
)
6455 iversym
.vs_vers
= 0;
6457 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6461 if (h
->verinfo
.vertree
== NULL
)
6462 iversym
.vs_vers
= 1;
6464 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6467 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
6468 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6470 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6471 eversym
+= h
->dynindx
;
6472 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6476 /* If we're stripping it, then it was just a dynamic symbol, and
6477 there's nothing else to do. */
6481 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6483 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
6485 eoinfo
->failed
= true;
6492 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6493 originated from the section given by INPUT_REL_HDR) to the
6497 elf_link_output_relocs (output_bfd
, input_section
, input_rel_hdr
,
6500 asection
*input_section
;
6501 Elf_Internal_Shdr
*input_rel_hdr
;
6502 Elf_Internal_Rela
*internal_relocs
;
6504 Elf_Internal_Rela
*irela
;
6505 Elf_Internal_Rela
*irelaend
;
6506 Elf_Internal_Shdr
*output_rel_hdr
;
6507 asection
*output_section
;
6508 unsigned int *rel_countp
= NULL
;
6509 struct elf_backend_data
*bed
;
6512 output_section
= input_section
->output_section
;
6513 output_rel_hdr
= NULL
;
6515 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
6516 == input_rel_hdr
->sh_entsize
)
6518 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
6519 rel_countp
= &elf_section_data (output_section
)->rel_count
;
6521 else if (elf_section_data (output_section
)->rel_hdr2
6522 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
6523 == input_rel_hdr
->sh_entsize
))
6525 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
6526 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
6530 (*_bfd_error_handler
) (
6531 _("%s: relocation size mismatch in %s section %s"),
6532 bfd_get_filename (output_bfd
),
6533 bfd_archive_filename (input_section
->owner
),
6534 input_section
->name
);
6535 bfd_set_error (bfd_error_wrong_object_format
);
6539 bed
= get_elf_backend_data (output_bfd
);
6540 irela
= internal_relocs
;
6541 irelaend
= irela
+ NUM_SHDR_ENTRIES (input_rel_hdr
)
6542 * bed
->s
->int_rels_per_ext_rel
;
6544 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
6546 Elf_External_Rel
*erel
;
6547 Elf_Internal_Rel
*irel
;
6549 amt
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
6550 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (amt
);
6553 (*_bfd_error_handler
) (_("Error: out of memory"));
6557 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
+ *rel_countp
);
6558 for (; irela
< irelaend
; irela
+= bed
->s
->int_rels_per_ext_rel
, erel
++)
6562 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
6564 irel
[i
].r_offset
= irela
[i
].r_offset
;
6565 irel
[i
].r_info
= irela
[i
].r_info
;
6566 BFD_ASSERT (irela
[i
].r_addend
== 0);
6569 if (bed
->s
->swap_reloc_out
)
6570 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, (PTR
) erel
);
6572 elf_swap_reloc_out (output_bfd
, irel
, erel
);
6579 Elf_External_Rela
*erela
;
6581 BFD_ASSERT (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
));
6583 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
+ *rel_countp
);
6584 for (; irela
< irelaend
; irela
+= bed
->s
->int_rels_per_ext_rel
, erela
++)
6585 if (bed
->s
->swap_reloca_out
)
6586 (*bed
->s
->swap_reloca_out
) (output_bfd
, irela
, (PTR
) erela
);
6588 elf_swap_reloca_out (output_bfd
, irela
, erela
);
6591 /* Bump the counter, so that we know where to add the next set of
6593 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
6598 /* Link an input file into the linker output file. This function
6599 handles all the sections and relocations of the input file at once.
6600 This is so that we only have to read the local symbols once, and
6601 don't have to keep them in memory. */
6604 elf_link_input_bfd (finfo
, input_bfd
)
6605 struct elf_final_link_info
*finfo
;
6608 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
6609 bfd
*, asection
*, bfd_byte
*,
6610 Elf_Internal_Rela
*,
6611 Elf_Internal_Sym
*, asection
**));
6613 Elf_Internal_Shdr
*symtab_hdr
;
6614 Elf_Internal_Shdr
*shndx_hdr
;
6617 Elf_External_Sym
*external_syms
;
6618 Elf_External_Sym
*esym
;
6619 Elf_External_Sym
*esymend
;
6620 Elf_External_Sym_Shndx
*shndx_buf
;
6621 Elf_External_Sym_Shndx
*shndx
;
6622 Elf_Internal_Sym
*isym
;
6624 asection
**ppsection
;
6626 struct elf_backend_data
*bed
;
6627 boolean emit_relocs
;
6628 struct elf_link_hash_entry
**sym_hashes
;
6630 output_bfd
= finfo
->output_bfd
;
6631 bed
= get_elf_backend_data (output_bfd
);
6632 relocate_section
= bed
->elf_backend_relocate_section
;
6634 /* If this is a dynamic object, we don't want to do anything here:
6635 we don't want the local symbols, and we don't want the section
6637 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6640 emit_relocs
= (finfo
->info
->relocateable
6641 || finfo
->info
->emitrelocations
6642 || bed
->elf_backend_emit_relocs
);
6644 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6645 if (elf_bad_symtab (input_bfd
))
6647 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6652 locsymcount
= symtab_hdr
->sh_info
;
6653 extsymoff
= symtab_hdr
->sh_info
;
6656 /* Read the local symbols. */
6657 if (symtab_hdr
->contents
!= NULL
)
6658 external_syms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
6659 else if (locsymcount
== 0)
6660 external_syms
= NULL
;
6663 bfd_size_type amt
= locsymcount
* sizeof (Elf_External_Sym
);
6664 external_syms
= finfo
->external_syms
;
6665 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
6666 || bfd_bread (external_syms
, amt
, input_bfd
) != amt
)
6670 shndx_hdr
= &elf_tdata (input_bfd
)->symtab_shndx_hdr
;
6672 if (shndx_hdr
->sh_size
!= 0 && locsymcount
!= 0)
6674 bfd_size_type amt
= locsymcount
* sizeof (Elf_External_Sym_Shndx
);
6675 shndx_buf
= finfo
->locsym_shndx
;
6676 if (bfd_seek (input_bfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
6677 || bfd_bread (shndx_buf
, amt
, input_bfd
) != amt
)
6681 /* Swap in the local symbols and write out the ones which we know
6682 are going into the output file. */
6683 for (esym
= external_syms
, esymend
= esym
+ locsymcount
,
6684 isym
= finfo
->internal_syms
, pindex
= finfo
->indices
,
6685 ppsection
= finfo
->sections
, shndx
= shndx_buf
;
6687 esym
++, isym
++, pindex
++, ppsection
++,
6688 shndx
= (shndx
!= NULL
? shndx
+ 1 : NULL
))
6692 Elf_Internal_Sym osym
;
6694 elf_swap_symbol_in (input_bfd
, (const PTR
) esym
, (const PTR
) shndx
,
6698 if (elf_bad_symtab (input_bfd
))
6700 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6707 if (isym
->st_shndx
== SHN_UNDEF
)
6708 isec
= bfd_und_section_ptr
;
6709 else if (isym
->st_shndx
< SHN_LORESERVE
6710 || isym
->st_shndx
> SHN_HIRESERVE
)
6712 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
6714 && elf_section_data (isec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
6715 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6717 _bfd_merged_section_offset (output_bfd
, &isec
,
6718 elf_section_data (isec
)->sec_info
,
6719 isym
->st_value
, (bfd_vma
) 0);
6721 else if (isym
->st_shndx
== SHN_ABS
)
6722 isec
= bfd_abs_section_ptr
;
6723 else if (isym
->st_shndx
== SHN_COMMON
)
6724 isec
= bfd_com_section_ptr
;
6733 /* Don't output the first, undefined, symbol. */
6734 if (esym
== external_syms
)
6737 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6739 /* We never output section symbols. Instead, we use the
6740 section symbol of the corresponding section in the output
6745 /* If we are stripping all symbols, we don't want to output this
6747 if (finfo
->info
->strip
== strip_all
)
6750 /* If we are discarding all local symbols, we don't want to
6751 output this one. If we are generating a relocateable output
6752 file, then some of the local symbols may be required by
6753 relocs; we output them below as we discover that they are
6755 if (finfo
->info
->discard
== discard_all
)
6758 /* If this symbol is defined in a section which we are
6759 discarding, we don't need to keep it, but note that
6760 linker_mark is only reliable for sections that have contents.
6761 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6762 as well as linker_mark. */
6763 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6765 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6766 || (! finfo
->info
->relocateable
6767 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6770 /* Get the name of the symbol. */
6771 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6776 /* See if we are discarding symbols with this name. */
6777 if ((finfo
->info
->strip
== strip_some
6778 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
6780 || (((finfo
->info
->discard
== discard_sec_merge
6781 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocateable
)
6782 || finfo
->info
->discard
== discard_l
)
6783 && bfd_is_local_label_name (input_bfd
, name
)))
6786 /* If we get here, we are going to output this symbol. */
6790 /* Adjust the section index for the output file. */
6791 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6792 isec
->output_section
);
6793 if (osym
.st_shndx
== SHN_BAD
)
6796 *pindex
= bfd_get_symcount (output_bfd
);
6798 /* ELF symbols in relocateable files are section relative, but
6799 in executable files they are virtual addresses. Note that
6800 this code assumes that all ELF sections have an associated
6801 BFD section with a reasonable value for output_offset; below
6802 we assume that they also have a reasonable value for
6803 output_section. Any special sections must be set up to meet
6804 these requirements. */
6805 osym
.st_value
+= isec
->output_offset
;
6806 if (! finfo
->info
->relocateable
)
6808 osym
.st_value
+= isec
->output_section
->vma
;
6809 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6811 /* STT_TLS symbols are relative to PT_TLS segment base. */
6812 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6813 osym
.st_value
-= finfo
->first_tls_sec
->vma
;
6817 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
6821 /* Relocate the contents of each section. */
6822 sym_hashes
= elf_sym_hashes (input_bfd
);
6823 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6827 if (! o
->linker_mark
)
6829 /* This section was omitted from the link. */
6833 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6834 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6837 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6839 /* Section was created by elf_link_create_dynamic_sections
6844 /* Get the contents of the section. They have been cached by a
6845 relaxation routine. Note that o is a section in an input
6846 file, so the contents field will not have been set by any of
6847 the routines which work on output files. */
6848 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6849 contents
= elf_section_data (o
)->this_hdr
.contents
;
6852 contents
= finfo
->contents
;
6853 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
6854 (file_ptr
) 0, o
->_raw_size
))
6858 if ((o
->flags
& SEC_RELOC
) != 0)
6860 Elf_Internal_Rela
*internal_relocs
;
6862 /* Get the swapped relocs. */
6863 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
6864 (input_bfd
, o
, finfo
->external_relocs
,
6865 finfo
->internal_relocs
, false));
6866 if (internal_relocs
== NULL
6867 && o
->reloc_count
> 0)
6870 /* Run through the relocs looking for any against symbols
6871 from discarded sections and section symbols from
6872 removed link-once sections. Complain about relocs
6873 against discarded sections. Zero relocs against removed
6874 link-once sections. We should really complain if
6875 anything in the final link tries to use it, but
6876 DWARF-based exception handling might have an entry in
6877 .eh_frame to describe a routine in the linkonce section,
6878 and it turns out to be hard to remove the .eh_frame
6879 entry too. FIXME. */
6880 if (!finfo
->info
->relocateable
6881 && !elf_section_ignore_discarded_relocs (o
))
6883 Elf_Internal_Rela
*rel
, *relend
;
6885 rel
= internal_relocs
;
6886 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6887 for ( ; rel
< relend
; rel
++)
6889 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
6891 if (r_symndx
>= locsymcount
6892 || (elf_bad_symtab (input_bfd
)
6893 && finfo
->sections
[r_symndx
] == NULL
))
6895 struct elf_link_hash_entry
*h
;
6897 h
= sym_hashes
[r_symndx
- extsymoff
];
6898 while (h
->root
.type
== bfd_link_hash_indirect
6899 || h
->root
.type
== bfd_link_hash_warning
)
6900 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6902 /* Complain if the definition comes from a
6903 discarded section. */
6904 if ((h
->root
.type
== bfd_link_hash_defined
6905 || h
->root
.type
== bfd_link_hash_defweak
)
6906 && elf_discarded_section (h
->root
.u
.def
.section
))
6908 #if BFD_VERSION_DATE < 20031005
6909 if ((o
->flags
& SEC_DEBUGGING
) != 0)
6911 #if BFD_VERSION_DATE > 20021005
6912 (*finfo
->info
->callbacks
->warning
)
6914 _("warning: relocation against removed section; zeroing"),
6915 NULL
, input_bfd
, o
, rel
->r_offset
);
6917 BFD_ASSERT (r_symndx
!= 0);
6918 memset (rel
, 0, sizeof (*rel
));
6923 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6924 (finfo
->info
, h
->root
.root
.string
,
6925 input_bfd
, o
, rel
->r_offset
,
6933 asection
*sec
= finfo
->sections
[r_symndx
];
6935 if (sec
!= NULL
&& elf_discarded_section (sec
))
6937 #if BFD_VERSION_DATE < 20031005
6938 if ((o
->flags
& SEC_DEBUGGING
) != 0
6939 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
6941 #if BFD_VERSION_DATE > 20021005
6942 (*finfo
->info
->callbacks
->warning
)
6944 _("warning: relocation against removed section"),
6945 NULL
, input_bfd
, o
, rel
->r_offset
);
6947 BFD_ASSERT (r_symndx
!= 0);
6949 = ELF_R_INFO (0, ELF_R_TYPE (rel
->r_info
));
6957 = _("local symbols in discarded section %s");
6959 = strlen (sec
->name
) + strlen (msg
) - 1;
6960 char *buf
= (char *) bfd_malloc (amt
);
6963 sprintf (buf
, msg
, sec
->name
);
6965 buf
= (char *) sec
->name
;
6966 ok
= (*finfo
->info
->callbacks
6967 ->undefined_symbol
) (finfo
->info
, buf
,
6971 if (buf
!= sec
->name
)
6981 /* Relocate the section by invoking a back end routine.
6983 The back end routine is responsible for adjusting the
6984 section contents as necessary, and (if using Rela relocs
6985 and generating a relocateable output file) adjusting the
6986 reloc addend as necessary.
6988 The back end routine does not have to worry about setting
6989 the reloc address or the reloc symbol index.
6991 The back end routine is given a pointer to the swapped in
6992 internal symbols, and can access the hash table entries
6993 for the external symbols via elf_sym_hashes (input_bfd).
6995 When generating relocateable output, the back end routine
6996 must handle STB_LOCAL/STT_SECTION symbols specially. The
6997 output symbol is going to be a section symbol
6998 corresponding to the output section, which will require
6999 the addend to be adjusted. */
7001 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
7002 input_bfd
, o
, contents
,
7004 finfo
->internal_syms
,
7010 Elf_Internal_Rela
*irela
;
7011 Elf_Internal_Rela
*irelaend
;
7012 struct elf_link_hash_entry
**rel_hash
;
7013 Elf_Internal_Shdr
*input_rel_hdr
;
7014 unsigned int next_erel
;
7015 boolean (*reloc_emitter
) PARAMS ((bfd
*, asection
*,
7016 Elf_Internal_Shdr
*,
7017 Elf_Internal_Rela
*));
7018 boolean rela_normal
;
7020 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7021 rela_normal
= (bed
->rela_normal
7022 && (input_rel_hdr
->sh_entsize
7023 == sizeof (Elf_External_Rela
)));
7025 /* Adjust the reloc addresses and symbol indices. */
7027 irela
= internal_relocs
;
7028 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7029 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7030 + elf_section_data (o
->output_section
)->rel_count
7031 + elf_section_data (o
->output_section
)->rel_count2
);
7032 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7034 unsigned long r_symndx
;
7037 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7043 irela
->r_offset
+= o
->output_offset
;
7045 /* Relocs in an executable have to be virtual addresses. */
7046 if (!finfo
->info
->relocateable
)
7047 irela
->r_offset
+= o
->output_section
->vma
;
7049 r_symndx
= ELF_R_SYM (irela
->r_info
);
7054 if (r_symndx
>= locsymcount
7055 || (elf_bad_symtab (input_bfd
)
7056 && finfo
->sections
[r_symndx
] == NULL
))
7058 struct elf_link_hash_entry
*rh
;
7061 /* This is a reloc against a global symbol. We
7062 have not yet output all the local symbols, so
7063 we do not know the symbol index of any global
7064 symbol. We set the rel_hash entry for this
7065 reloc to point to the global hash table entry
7066 for this symbol. The symbol index is then
7067 set at the end of elf_bfd_final_link. */
7068 indx
= r_symndx
- extsymoff
;
7069 rh
= elf_sym_hashes (input_bfd
)[indx
];
7070 while (rh
->root
.type
== bfd_link_hash_indirect
7071 || rh
->root
.type
== bfd_link_hash_warning
)
7072 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7074 /* Setting the index to -2 tells
7075 elf_link_output_extsym that this symbol is
7077 BFD_ASSERT (rh
->indx
< 0);
7085 /* This is a reloc against a local symbol. */
7088 isym
= finfo
->internal_syms
+ r_symndx
;
7089 sec
= finfo
->sections
[r_symndx
];
7090 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
7092 /* I suppose the backend ought to fill in the
7093 section of any STT_SECTION symbol against a
7094 processor specific section. If we have
7095 discarded a section, the output_section will
7096 be the absolute section. */
7097 if (bfd_is_abs_section (sec
)
7099 && bfd_is_abs_section (sec
->output_section
)))
7101 else if (sec
== NULL
|| sec
->owner
== NULL
)
7103 bfd_set_error (bfd_error_bad_value
);
7108 r_symndx
= sec
->output_section
->target_index
;
7109 BFD_ASSERT (r_symndx
!= 0);
7112 /* Adjust the addend according to where the
7113 section winds up in the output section. */
7115 irela
->r_addend
+= sec
->output_offset
;
7119 if (finfo
->indices
[r_symndx
] == -1)
7121 unsigned long shlink
;
7125 if (finfo
->info
->strip
== strip_all
)
7127 /* You can't do ld -r -s. */
7128 bfd_set_error (bfd_error_invalid_operation
);
7132 /* This symbol was skipped earlier, but
7133 since it is needed by a reloc, we
7134 must output it now. */
7135 shlink
= symtab_hdr
->sh_link
;
7136 name
= (bfd_elf_string_from_elf_section
7137 (input_bfd
, shlink
, isym
->st_name
));
7141 osec
= sec
->output_section
;
7143 _bfd_elf_section_from_bfd_section (output_bfd
,
7145 if (isym
->st_shndx
== SHN_BAD
)
7148 isym
->st_value
+= sec
->output_offset
;
7149 if (! finfo
->info
->relocateable
)
7151 isym
->st_value
+= osec
->vma
;
7152 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
7154 /* STT_TLS symbols are relative to PT_TLS
7156 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
7157 isym
->st_value
-= finfo
->first_tls_sec
->vma
;
7161 finfo
->indices
[r_symndx
]
7162 = bfd_get_symcount (output_bfd
);
7164 if (! elf_link_output_sym (finfo
, name
, isym
, sec
))
7168 r_symndx
= finfo
->indices
[r_symndx
];
7171 irela
->r_info
= ELF_R_INFO (r_symndx
,
7172 ELF_R_TYPE (irela
->r_info
));
7175 /* Swap out the relocs. */
7176 if (bed
->elf_backend_emit_relocs
7177 && !(finfo
->info
->relocateable
7178 || finfo
->info
->emitrelocations
))
7179 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7181 reloc_emitter
= elf_link_output_relocs
;
7183 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7187 input_rel_hdr
= elf_section_data (o
)->rel_hdr2
;
7190 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7191 * bed
->s
->int_rels_per_ext_rel
);
7192 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7200 /* Write out the modified section contents. */
7201 if (bed
->elf_backend_write_section
7202 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7204 /* Section written out. */
7206 else switch (elf_section_data (o
)->sec_info_type
)
7208 case ELF_INFO_TYPE_STABS
:
7209 if (! (_bfd_write_section_stabs
7211 &elf_hash_table (finfo
->info
)->stab_info
,
7212 o
, &elf_section_data (o
)->sec_info
, contents
)))
7215 case ELF_INFO_TYPE_MERGE
:
7216 if (! (_bfd_write_merged_section
7217 (output_bfd
, o
, elf_section_data (o
)->sec_info
)))
7220 case ELF_INFO_TYPE_EH_FRAME
:
7225 = bfd_get_section_by_name (elf_hash_table (finfo
->info
)->dynobj
,
7227 if (! (_bfd_elf_write_section_eh_frame (output_bfd
, o
, ehdrsec
,
7234 bfd_size_type sec_size
;
7236 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
7237 if (! (o
->flags
& SEC_EXCLUDE
)
7238 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7240 (file_ptr
) o
->output_offset
,
7251 /* Generate a reloc when linking an ELF file. This is a reloc
7252 requested by the linker, and does come from any input file. This
7253 is used to build constructor and destructor tables when linking
7257 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
7259 struct bfd_link_info
*info
;
7260 asection
*output_section
;
7261 struct bfd_link_order
*link_order
;
7263 reloc_howto_type
*howto
;
7267 struct elf_link_hash_entry
**rel_hash_ptr
;
7268 Elf_Internal_Shdr
*rel_hdr
;
7269 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7271 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7274 bfd_set_error (bfd_error_bad_value
);
7278 addend
= link_order
->u
.reloc
.p
->addend
;
7280 /* Figure out the symbol index. */
7281 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7282 + elf_section_data (output_section
)->rel_count
7283 + elf_section_data (output_section
)->rel_count2
);
7284 if (link_order
->type
== bfd_section_reloc_link_order
)
7286 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7287 BFD_ASSERT (indx
!= 0);
7288 *rel_hash_ptr
= NULL
;
7292 struct elf_link_hash_entry
*h
;
7294 /* Treat a reloc against a defined symbol as though it were
7295 actually against the section. */
7296 h
= ((struct elf_link_hash_entry
*)
7297 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7298 link_order
->u
.reloc
.p
->u
.name
,
7299 false, false, true));
7301 && (h
->root
.type
== bfd_link_hash_defined
7302 || h
->root
.type
== bfd_link_hash_defweak
))
7306 section
= h
->root
.u
.def
.section
;
7307 indx
= section
->output_section
->target_index
;
7308 *rel_hash_ptr
= NULL
;
7309 /* It seems that we ought to add the symbol value to the
7310 addend here, but in practice it has already been added
7311 because it was passed to constructor_callback. */
7312 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7316 /* Setting the index to -2 tells elf_link_output_extsym that
7317 this symbol is used by a reloc. */
7324 if (! ((*info
->callbacks
->unattached_reloc
)
7325 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
7326 (asection
*) NULL
, (bfd_vma
) 0)))
7332 /* If this is an inplace reloc, we must write the addend into the
7334 if (howto
->partial_inplace
&& addend
!= 0)
7337 bfd_reloc_status_type rstat
;
7340 const char *sym_name
;
7342 size
= bfd_get_reloc_size (howto
);
7343 buf
= (bfd_byte
*) bfd_zmalloc (size
);
7344 if (buf
== (bfd_byte
*) NULL
)
7346 rstat
= _bfd_relocate_contents (howto
, output_bfd
, (bfd_vma
) addend
, buf
);
7353 case bfd_reloc_outofrange
:
7356 case bfd_reloc_overflow
:
7357 if (link_order
->type
== bfd_section_reloc_link_order
)
7358 sym_name
= bfd_section_name (output_bfd
,
7359 link_order
->u
.reloc
.p
->u
.section
);
7361 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7362 if (! ((*info
->callbacks
->reloc_overflow
)
7363 (info
, sym_name
, howto
->name
, addend
,
7364 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
7371 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
7372 (file_ptr
) link_order
->offset
, size
);
7378 /* The address of a reloc is relative to the section in a
7379 relocateable file, and is a virtual address in an executable
7381 offset
= link_order
->offset
;
7382 if (! info
->relocateable
)
7383 offset
+= output_section
->vma
;
7385 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7387 if (rel_hdr
->sh_type
== SHT_REL
)
7390 Elf_Internal_Rel
*irel
;
7391 Elf_External_Rel
*erel
;
7394 size
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
7395 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (size
);
7399 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7400 irel
[i
].r_offset
= offset
;
7401 irel
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
7403 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
7404 + elf_section_data (output_section
)->rel_count
);
7406 if (bed
->s
->swap_reloc_out
)
7407 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, (bfd_byte
*) erel
);
7409 elf_swap_reloc_out (output_bfd
, irel
, erel
);
7416 Elf_Internal_Rela
*irela
;
7417 Elf_External_Rela
*erela
;
7420 size
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
7421 irela
= (Elf_Internal_Rela
*) bfd_zmalloc (size
);
7425 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7426 irela
[i
].r_offset
= offset
;
7427 irela
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
7428 irela
[0].r_addend
= addend
;
7430 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
7431 + elf_section_data (output_section
)->rel_count
);
7433 if (bed
->s
->swap_reloca_out
)
7434 (*bed
->s
->swap_reloca_out
) (output_bfd
, irela
, (bfd_byte
*) erela
);
7436 elf_swap_reloca_out (output_bfd
, irela
, erela
);
7439 ++elf_section_data (output_section
)->rel_count
;
7444 /* Allocate a pointer to live in a linker created section. */
7447 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
7449 struct bfd_link_info
*info
;
7450 elf_linker_section_t
*lsect
;
7451 struct elf_link_hash_entry
*h
;
7452 const Elf_Internal_Rela
*rel
;
7454 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
7455 elf_linker_section_pointers_t
*linker_section_ptr
;
7456 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7459 BFD_ASSERT (lsect
!= NULL
);
7461 /* Is this a global symbol? */
7464 /* Has this symbol already been allocated? If so, our work is done. */
7465 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
7470 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
7471 /* Make sure this symbol is output as a dynamic symbol. */
7472 if (h
->dynindx
== -1)
7474 if (! elf_link_record_dynamic_symbol (info
, h
))
7478 if (lsect
->rel_section
)
7479 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7483 /* Allocation of a pointer to a local symbol. */
7484 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
7486 /* Allocate a table to hold the local symbols if first time. */
7489 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
7490 register unsigned int i
;
7493 amt
*= sizeof (elf_linker_section_pointers_t
*);
7494 ptr
= (elf_linker_section_pointers_t
**) bfd_alloc (abfd
, amt
);
7499 elf_local_ptr_offsets (abfd
) = ptr
;
7500 for (i
= 0; i
< num_symbols
; i
++)
7501 ptr
[i
] = (elf_linker_section_pointers_t
*) 0;
7504 /* Has this symbol already been allocated? If so, our work is done. */
7505 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
7510 ptr_linker_section_ptr
= &ptr
[r_symndx
];
7514 /* If we are generating a shared object, we need to
7515 output a R_<xxx>_RELATIVE reloc so that the
7516 dynamic linker can adjust this GOT entry. */
7517 BFD_ASSERT (lsect
->rel_section
!= NULL
);
7518 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7522 /* Allocate space for a pointer in the linker section, and allocate
7523 a new pointer record from internal memory. */
7524 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
7525 amt
= sizeof (elf_linker_section_pointers_t
);
7526 linker_section_ptr
= (elf_linker_section_pointers_t
*) bfd_alloc (abfd
, amt
);
7528 if (!linker_section_ptr
)
7531 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
7532 linker_section_ptr
->addend
= rel
->r_addend
;
7533 linker_section_ptr
->which
= lsect
->which
;
7534 linker_section_ptr
->written_address_p
= false;
7535 *ptr_linker_section_ptr
= linker_section_ptr
;
7538 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
7540 linker_section_ptr
->offset
= (lsect
->section
->_raw_size
7541 - lsect
->hole_size
+ (ARCH_SIZE
/ 8));
7542 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
7543 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
7544 if (lsect
->sym_hash
)
7546 /* Bump up symbol value if needed. */
7547 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
7549 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
7550 lsect
->sym_hash
->root
.root
.string
,
7551 (long) ARCH_SIZE
/ 8,
7552 (long) lsect
->sym_hash
->root
.u
.def
.value
);
7558 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
7560 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
7564 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7565 lsect
->name
, (long) linker_section_ptr
->offset
,
7566 (long) lsect
->section
->_raw_size
);
7573 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7576 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7579 /* Fill in the address for a pointer generated in a linker section. */
7582 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
,
7583 relocation
, rel
, relative_reloc
)
7586 struct bfd_link_info
*info
;
7587 elf_linker_section_t
*lsect
;
7588 struct elf_link_hash_entry
*h
;
7590 const Elf_Internal_Rela
*rel
;
7593 elf_linker_section_pointers_t
*linker_section_ptr
;
7595 BFD_ASSERT (lsect
!= NULL
);
7599 /* Handle global symbol. */
7600 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7601 (h
->linker_section_pointer
,
7605 BFD_ASSERT (linker_section_ptr
!= NULL
);
7607 if (! elf_hash_table (info
)->dynamic_sections_created
7610 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
7612 /* This is actually a static link, or it is a
7613 -Bsymbolic link and the symbol is defined
7614 locally. We must initialize this entry in the
7617 When doing a dynamic link, we create a .rela.<xxx>
7618 relocation entry to initialize the value. This
7619 is done in the finish_dynamic_symbol routine. */
7620 if (!linker_section_ptr
->written_address_p
)
7622 linker_section_ptr
->written_address_p
= true;
7623 bfd_put_ptr (output_bfd
,
7624 relocation
+ linker_section_ptr
->addend
,
7625 (lsect
->section
->contents
7626 + linker_section_ptr
->offset
));
7632 /* Handle local symbol. */
7633 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7634 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
7635 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
7636 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7637 (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
7641 BFD_ASSERT (linker_section_ptr
!= NULL
);
7643 /* Write out pointer if it hasn't been rewritten out before. */
7644 if (!linker_section_ptr
->written_address_p
)
7646 linker_section_ptr
->written_address_p
= true;
7647 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
7648 lsect
->section
->contents
+ linker_section_ptr
->offset
);
7652 asection
*srel
= lsect
->rel_section
;
7653 Elf_Internal_Rela
*outrel
;
7654 Elf_External_Rela
*erel
;
7655 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7659 amt
= sizeof (Elf_Internal_Rela
) * bed
->s
->int_rels_per_ext_rel
;
7660 outrel
= (Elf_Internal_Rela
*) bfd_zmalloc (amt
);
7663 (*_bfd_error_handler
) (_("Error: out of memory"));
7667 /* We need to generate a relative reloc for the dynamic
7671 srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
7673 lsect
->rel_section
= srel
;
7676 BFD_ASSERT (srel
!= NULL
);
7678 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7679 outrel
[i
].r_offset
= (lsect
->section
->output_section
->vma
7680 + lsect
->section
->output_offset
7681 + linker_section_ptr
->offset
);
7682 outrel
[0].r_info
= ELF_R_INFO (0, relative_reloc
);
7683 outrel
[0].r_addend
= 0;
7684 erel
= (Elf_External_Rela
*) lsect
->section
->contents
;
7685 erel
+= elf_section_data (lsect
->section
)->rel_count
;
7686 elf_swap_reloca_out (output_bfd
, outrel
, erel
);
7687 ++elf_section_data (lsect
->section
)->rel_count
;
7694 relocation
= (lsect
->section
->output_offset
7695 + linker_section_ptr
->offset
7696 - lsect
->hole_offset
7697 - lsect
->sym_offset
);
7701 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7702 lsect
->name
, (long) relocation
, (long) relocation
);
7705 /* Subtract out the addend, because it will get added back in by the normal
7707 return relocation
- linker_section_ptr
->addend
;
7710 /* Garbage collect unused sections. */
7712 static boolean elf_gc_mark
7713 PARAMS ((struct bfd_link_info
*info
, asection
*sec
,
7714 asection
* (*gc_mark_hook
)
7715 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7716 struct elf_link_hash_entry
*, Elf_Internal_Sym
*))));
7718 static boolean elf_gc_sweep
7719 PARAMS ((struct bfd_link_info
*info
,
7720 boolean (*gc_sweep_hook
)
7721 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
7722 const Elf_Internal_Rela
*relocs
))));
7724 static boolean elf_gc_sweep_symbol
7725 PARAMS ((struct elf_link_hash_entry
*h
, PTR idxptr
));
7727 static boolean elf_gc_allocate_got_offsets
7728 PARAMS ((struct elf_link_hash_entry
*h
, PTR offarg
));
7730 static boolean elf_gc_propagate_vtable_entries_used
7731 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
7733 static boolean elf_gc_smash_unused_vtentry_relocs
7734 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
7736 /* The mark phase of garbage collection. For a given section, mark
7737 it and any sections in this section's group, and all the sections
7738 which define symbols to which it refers. */
7741 elf_gc_mark (info
, sec
, gc_mark_hook
)
7742 struct bfd_link_info
*info
;
7744 asection
* (*gc_mark_hook
)
7745 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7746 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
7749 asection
*group_sec
;
7753 /* Mark all the sections in the group. */
7754 group_sec
= elf_section_data (sec
)->next_in_group
;
7755 if (group_sec
&& !group_sec
->gc_mark
)
7756 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
7759 /* Look through the section relocs. */
7761 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
7763 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
7764 Elf_Internal_Shdr
*symtab_hdr
;
7765 Elf_Internal_Shdr
*shndx_hdr
;
7766 struct elf_link_hash_entry
**sym_hashes
;
7769 Elf_External_Sym
*locsyms
, *freesyms
= NULL
;
7770 Elf_External_Sym_Shndx
*locsym_shndx
;
7771 bfd
*input_bfd
= sec
->owner
;
7772 struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
7774 /* GCFIXME: how to arrange so that relocs and symbols are not
7775 reread continually? */
7777 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
7778 sym_hashes
= elf_sym_hashes (input_bfd
);
7780 /* Read the local symbols. */
7781 if (elf_bad_symtab (input_bfd
))
7783 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
7787 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
7789 if (symtab_hdr
->contents
)
7790 locsyms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
7791 else if (nlocsyms
== 0)
7795 bfd_size_type amt
= nlocsyms
* sizeof (Elf_External_Sym
);
7796 locsyms
= freesyms
= bfd_malloc (amt
);
7797 if (freesyms
== NULL
7798 || bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
7799 || bfd_bread (locsyms
, amt
, input_bfd
) != amt
)
7806 shndx_hdr
= &elf_tdata (input_bfd
)->symtab_shndx_hdr
;
7807 locsym_shndx
= NULL
;
7808 if (shndx_hdr
->sh_size
!= 0 && nlocsyms
!= 0)
7810 bfd_size_type amt
= nlocsyms
* sizeof (Elf_External_Sym_Shndx
);
7811 locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
7812 if (bfd_seek (input_bfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
7813 || bfd_bread (locsym_shndx
, amt
, input_bfd
) != amt
)
7817 /* Read the relocations. */
7818 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
7819 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
7820 info
->keep_memory
));
7821 if (relstart
== NULL
)
7826 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7828 for (rel
= relstart
; rel
< relend
; rel
++)
7830 unsigned long r_symndx
;
7832 struct elf_link_hash_entry
*h
;
7834 Elf_External_Sym_Shndx
*locshndx
;
7836 r_symndx
= ELF_R_SYM (rel
->r_info
);
7840 if (elf_bad_symtab (sec
->owner
))
7842 locshndx
= locsym_shndx
+ (locsym_shndx
? r_symndx
: 0);
7843 elf_swap_symbol_in (input_bfd
,
7844 (const PTR
) (locsyms
+ r_symndx
),
7845 (const PTR
) locshndx
,
7847 if (ELF_ST_BIND (s
.st_info
) == STB_LOCAL
)
7848 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &s
);
7851 h
= sym_hashes
[r_symndx
- extsymoff
];
7852 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
7855 else if (r_symndx
>= nlocsyms
)
7857 h
= sym_hashes
[r_symndx
- extsymoff
];
7858 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
7862 locshndx
= locsym_shndx
+ (locsym_shndx
? r_symndx
: 0);
7863 elf_swap_symbol_in (input_bfd
,
7864 (const PTR
) (locsyms
+ r_symndx
),
7865 (const PTR
) locshndx
,
7867 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &s
);
7870 if (rsec
&& !rsec
->gc_mark
)
7872 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
7874 else if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
7883 if (!info
->keep_memory
)
7893 /* The sweep phase of garbage collection. Remove all garbage sections. */
7896 elf_gc_sweep (info
, gc_sweep_hook
)
7897 struct bfd_link_info
*info
;
7898 boolean (*gc_sweep_hook
)
7899 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
7900 const Elf_Internal_Rela
*relocs
));
7904 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7908 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
7911 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7913 /* Keep special sections. Keep .debug sections. */
7914 if ((o
->flags
& SEC_LINKER_CREATED
)
7915 || (o
->flags
& SEC_DEBUGGING
))
7921 /* Skip sweeping sections already excluded. */
7922 if (o
->flags
& SEC_EXCLUDE
)
7925 /* Since this is early in the link process, it is simple
7926 to remove a section from the output. */
7927 o
->flags
|= SEC_EXCLUDE
;
7929 /* But we also have to update some of the relocation
7930 info we collected before. */
7932 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
7934 Elf_Internal_Rela
*internal_relocs
;
7937 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
7938 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
7939 if (internal_relocs
== NULL
)
7942 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
7944 if (!info
->keep_memory
)
7945 free (internal_relocs
);
7953 /* Remove the symbols that were in the swept sections from the dynamic
7954 symbol table. GCFIXME: Anyone know how to get them out of the
7955 static symbol table as well? */
7959 elf_link_hash_traverse (elf_hash_table (info
),
7960 elf_gc_sweep_symbol
,
7963 elf_hash_table (info
)->dynsymcount
= i
;
7969 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7972 elf_gc_sweep_symbol (h
, idxptr
)
7973 struct elf_link_hash_entry
*h
;
7976 int *idx
= (int *) idxptr
;
7978 if (h
->root
.type
== bfd_link_hash_warning
)
7979 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7981 if (h
->dynindx
!= -1
7982 && ((h
->root
.type
!= bfd_link_hash_defined
7983 && h
->root
.type
!= bfd_link_hash_defweak
)
7984 || h
->root
.u
.def
.section
->gc_mark
))
7985 h
->dynindx
= (*idx
)++;
7990 /* Propogate collected vtable information. This is called through
7991 elf_link_hash_traverse. */
7994 elf_gc_propagate_vtable_entries_used (h
, okp
)
7995 struct elf_link_hash_entry
*h
;
7998 if (h
->root
.type
== bfd_link_hash_warning
)
7999 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8001 /* Those that are not vtables. */
8002 if (h
->vtable_parent
== NULL
)
8005 /* Those vtables that do not have parents, we cannot merge. */
8006 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
8009 /* If we've already been done, exit. */
8010 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
8013 /* Make sure the parent's table is up to date. */
8014 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
8016 if (h
->vtable_entries_used
== NULL
)
8018 /* None of this table's entries were referenced. Re-use the
8020 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
8021 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
8028 /* Or the parent's entries into ours. */
8029 cu
= h
->vtable_entries_used
;
8031 pu
= h
->vtable_parent
->vtable_entries_used
;
8034 asection
*sec
= h
->root
.u
.def
.section
;
8035 struct elf_backend_data
*bed
= get_elf_backend_data (sec
->owner
);
8036 int file_align
= bed
->s
->file_align
;
8038 n
= h
->vtable_parent
->vtable_entries_size
/ file_align
;
8053 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
8054 struct elf_link_hash_entry
*h
;
8058 bfd_vma hstart
, hend
;
8059 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8060 struct elf_backend_data
*bed
;
8063 if (h
->root
.type
== bfd_link_hash_warning
)
8064 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8066 /* Take care of both those symbols that do not describe vtables as
8067 well as those that are not loaded. */
8068 if (h
->vtable_parent
== NULL
)
8071 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8072 || h
->root
.type
== bfd_link_hash_defweak
);
8074 sec
= h
->root
.u
.def
.section
;
8075 hstart
= h
->root
.u
.def
.value
;
8076 hend
= hstart
+ h
->size
;
8078 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
8079 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, true));
8081 return *(boolean
*) okp
= false;
8082 bed
= get_elf_backend_data (sec
->owner
);
8083 file_align
= bed
->s
->file_align
;
8085 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8087 for (rel
= relstart
; rel
< relend
; ++rel
)
8088 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8090 /* If the entry is in use, do nothing. */
8091 if (h
->vtable_entries_used
8092 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
8094 bfd_vma entry
= (rel
->r_offset
- hstart
) / file_align
;
8095 if (h
->vtable_entries_used
[entry
])
8098 /* Otherwise, kill it. */
8099 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8105 /* Do mark and sweep of unused sections. */
8108 elf_gc_sections (abfd
, info
)
8110 struct bfd_link_info
*info
;
8114 asection
* (*gc_mark_hook
)
8115 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8116 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
8118 if (!get_elf_backend_data (abfd
)->can_gc_sections
8119 || info
->relocateable
|| info
->emitrelocations
8120 || elf_hash_table (info
)->dynamic_sections_created
)
8123 /* Apply transitive closure to the vtable entry usage info. */
8124 elf_link_hash_traverse (elf_hash_table (info
),
8125 elf_gc_propagate_vtable_entries_used
,
8130 /* Kill the vtable relocations that were not used. */
8131 elf_link_hash_traverse (elf_hash_table (info
),
8132 elf_gc_smash_unused_vtentry_relocs
,
8137 /* Grovel through relocs to find out who stays ... */
8139 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8140 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8144 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8147 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8149 if (o
->flags
& SEC_KEEP
)
8150 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
8155 /* ... and mark SEC_EXCLUDE for those that go. */
8156 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8162 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
8165 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
8168 struct elf_link_hash_entry
*h
;
8171 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8172 struct elf_link_hash_entry
**search
, *child
;
8173 bfd_size_type extsymcount
;
8175 /* The sh_info field of the symtab header tells us where the
8176 external symbols start. We don't care about the local symbols at
8178 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
8179 if (!elf_bad_symtab (abfd
))
8180 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8182 sym_hashes
= elf_sym_hashes (abfd
);
8183 sym_hashes_end
= sym_hashes
+ extsymcount
;
8185 /* Hunt down the child symbol, which is in this section at the same
8186 offset as the relocation. */
8187 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8189 if ((child
= *search
) != NULL
8190 && (child
->root
.type
== bfd_link_hash_defined
8191 || child
->root
.type
== bfd_link_hash_defweak
)
8192 && child
->root
.u
.def
.section
== sec
8193 && child
->root
.u
.def
.value
== offset
)
8197 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
8198 bfd_archive_filename (abfd
), sec
->name
,
8199 (unsigned long) offset
);
8200 bfd_set_error (bfd_error_invalid_operation
);
8206 /* This *should* only be the absolute section. It could potentially
8207 be that someone has defined a non-global vtable though, which
8208 would be bad. It isn't worth paging in the local symbols to be
8209 sure though; that case should simply be handled by the assembler. */
8211 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
8214 child
->vtable_parent
= h
;
8219 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
8222 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
8223 bfd
*abfd ATTRIBUTE_UNUSED
;
8224 asection
*sec ATTRIBUTE_UNUSED
;
8225 struct elf_link_hash_entry
*h
;
8228 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8229 int file_align
= bed
->s
->file_align
;
8231 if (addend
>= h
->vtable_entries_size
)
8234 boolean
*ptr
= h
->vtable_entries_used
;
8236 /* While the symbol is undefined, we have to be prepared to handle
8238 if (h
->root
.type
== bfd_link_hash_undefined
)
8245 /* Oops! We've got a reference past the defined end of
8246 the table. This is probably a bug -- shall we warn? */
8251 /* Allocate one extra entry for use as a "done" flag for the
8252 consolidation pass. */
8253 bytes
= (size
/ file_align
+ 1) * sizeof (boolean
);
8257 ptr
= bfd_realloc (ptr
- 1, (bfd_size_type
) bytes
);
8263 oldbytes
= ((h
->vtable_entries_size
/ file_align
+ 1)
8264 * sizeof (boolean
));
8265 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8269 ptr
= bfd_zmalloc ((bfd_size_type
) bytes
);
8274 /* And arrange for that done flag to be at index -1. */
8275 h
->vtable_entries_used
= ptr
+ 1;
8276 h
->vtable_entries_size
= size
;
8279 h
->vtable_entries_used
[addend
/ file_align
] = true;
8284 /* And an accompanying bit to work out final got entry offsets once
8285 we're done. Should be called from final_link. */
8288 elf_gc_common_finalize_got_offsets (abfd
, info
)
8290 struct bfd_link_info
*info
;
8293 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8296 /* The GOT offset is relative to the .got section, but the GOT header is
8297 put into the .got.plt section, if the backend uses it. */
8298 if (bed
->want_got_plt
)
8301 gotoff
= bed
->got_header_size
;
8303 /* Do the local .got entries first. */
8304 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
8306 bfd_signed_vma
*local_got
;
8307 bfd_size_type j
, locsymcount
;
8308 Elf_Internal_Shdr
*symtab_hdr
;
8310 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
8313 local_got
= elf_local_got_refcounts (i
);
8317 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
8318 if (elf_bad_symtab (i
))
8319 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8321 locsymcount
= symtab_hdr
->sh_info
;
8323 for (j
= 0; j
< locsymcount
; ++j
)
8325 if (local_got
[j
] > 0)
8327 local_got
[j
] = gotoff
;
8328 gotoff
+= ARCH_SIZE
/ 8;
8331 local_got
[j
] = (bfd_vma
) -1;
8335 /* Then the global .got entries. .plt refcounts are handled by
8336 adjust_dynamic_symbol */
8337 elf_link_hash_traverse (elf_hash_table (info
),
8338 elf_gc_allocate_got_offsets
,
8343 /* We need a special top-level link routine to convert got reference counts
8344 to real got offsets. */
8347 elf_gc_allocate_got_offsets (h
, offarg
)
8348 struct elf_link_hash_entry
*h
;
8351 bfd_vma
*off
= (bfd_vma
*) offarg
;
8353 if (h
->root
.type
== bfd_link_hash_warning
)
8354 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8356 if (h
->got
.refcount
> 0)
8358 h
->got
.offset
= off
[0];
8359 off
[0] += ARCH_SIZE
/ 8;
8362 h
->got
.offset
= (bfd_vma
) -1;
8367 /* Many folk need no more in the way of final link than this, once
8368 got entry reference counting is enabled. */
8371 elf_gc_common_final_link (abfd
, info
)
8373 struct bfd_link_info
*info
;
8375 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
8378 /* Invoke the regular ELF backend linker to do all the work. */
8379 return elf_bfd_final_link (abfd
, info
);
8382 /* This function will be called though elf_link_hash_traverse to store
8383 all hash value of the exported symbols in an array. */
8386 elf_collect_hash_codes (h
, data
)
8387 struct elf_link_hash_entry
*h
;
8390 unsigned long **valuep
= (unsigned long **) data
;
8396 if (h
->root
.type
== bfd_link_hash_warning
)
8397 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8399 /* Ignore indirect symbols. These are added by the versioning code. */
8400 if (h
->dynindx
== -1)
8403 name
= h
->root
.root
.string
;
8404 p
= strchr (name
, ELF_VER_CHR
);
8407 alc
= bfd_malloc ((bfd_size_type
) (p
- name
+ 1));
8408 memcpy (alc
, name
, (size_t) (p
- name
));
8409 alc
[p
- name
] = '\0';
8413 /* Compute the hash value. */
8414 ha
= bfd_elf_hash (name
);
8416 /* Store the found hash value in the array given as the argument. */
8419 /* And store it in the struct so that we can put it in the hash table
8421 h
->elf_hash_value
= ha
;
8430 elf_reloc_symbol_deleted_p (offset
, cookie
)
8434 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
8436 if (rcookie
->bad_symtab
)
8437 rcookie
->rel
= rcookie
->rels
;
8439 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
8441 unsigned long r_symndx
= ELF_R_SYM (rcookie
->rel
->r_info
);
8442 Elf_Internal_Sym isym
;
8444 if (! rcookie
->bad_symtab
)
8445 if (rcookie
->rel
->r_offset
> offset
)
8447 if (rcookie
->rel
->r_offset
!= offset
)
8450 if (rcookie
->locsyms
&& r_symndx
< rcookie
->locsymcount
)
8452 Elf_External_Sym
*lsym
;
8453 Elf_External_Sym_Shndx
*lshndx
;
8455 lsym
= (Elf_External_Sym
*) rcookie
->locsyms
+ r_symndx
;
8456 lshndx
= (Elf_External_Sym_Shndx
*) rcookie
->locsym_shndx
;
8459 elf_swap_symbol_in (rcookie
->abfd
, (const PTR
) lsym
,
8460 (const PTR
) lshndx
, &isym
);
8463 if (r_symndx
>= rcookie
->locsymcount
8464 || (rcookie
->locsyms
8465 && ELF_ST_BIND (isym
.st_info
) != STB_LOCAL
))
8467 struct elf_link_hash_entry
*h
;
8469 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
8471 while (h
->root
.type
== bfd_link_hash_indirect
8472 || h
->root
.type
== bfd_link_hash_warning
)
8473 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8475 if ((h
->root
.type
== bfd_link_hash_defined
8476 || h
->root
.type
== bfd_link_hash_defweak
)
8477 && elf_discarded_section (h
->root
.u
.def
.section
))
8482 else if (rcookie
->locsyms
)
8484 /* It's not a relocation against a global symbol,
8485 but it could be a relocation against a local
8486 symbol for a discarded section. */
8489 /* Need to: get the symbol; get the section. */
8490 if (isym
.st_shndx
< SHN_LORESERVE
|| isym
.st_shndx
> SHN_HIRESERVE
)
8492 isec
= section_from_elf_index (rcookie
->abfd
, isym
.st_shndx
);
8493 if (isec
!= NULL
&& elf_discarded_section (isec
))
8502 /* Discard unneeded references to discarded sections.
8503 Returns true if any section's size was changed. */
8504 /* This function assumes that the relocations are in sorted order,
8505 which is true for all known assemblers. */
8508 elf_bfd_discard_info (output_bfd
, info
)
8510 struct bfd_link_info
*info
;
8512 struct elf_reloc_cookie cookie
;
8513 asection
*stab
, *eh
, *ehdr
;
8514 Elf_Internal_Shdr
*symtab_hdr
;
8515 Elf_Internal_Shdr
*shndx_hdr
;
8516 Elf_External_Sym
*freesyms
;
8517 struct elf_backend_data
*bed
;
8519 boolean ret
= false;
8520 boolean strip
= info
->strip
== strip_all
|| info
->strip
== strip_debugger
;
8522 if (info
->relocateable
8523 || info
->traditional_format
8524 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
8525 || ! is_elf_hash_table (info
))
8529 if (elf_hash_table (info
)->dynobj
!= NULL
)
8530 ehdr
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
8533 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
8535 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
8538 bed
= get_elf_backend_data (abfd
);
8540 if ((abfd
->flags
& DYNAMIC
) != 0)
8546 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
8547 if (eh
&& (eh
->_raw_size
== 0
8548 || bfd_is_abs_section (eh
->output_section
)))
8555 stab
= bfd_get_section_by_name (abfd
, ".stab");
8556 if (stab
&& (stab
->_raw_size
== 0
8557 || bfd_is_abs_section (stab
->output_section
)))
8561 || elf_section_data(stab
)->sec_info_type
!= ELF_INFO_TYPE_STABS
)
8563 && (strip
|| ! bed
->elf_backend_discard_info
))
8566 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8567 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8570 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
8571 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
8572 if (cookie
.bad_symtab
)
8574 cookie
.locsymcount
=
8575 symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8576 cookie
.extsymoff
= 0;
8580 cookie
.locsymcount
= symtab_hdr
->sh_info
;
8581 cookie
.extsymoff
= symtab_hdr
->sh_info
;
8585 if (symtab_hdr
->contents
)
8586 cookie
.locsyms
= (void *) symtab_hdr
->contents
;
8587 else if (cookie
.locsymcount
== 0)
8588 cookie
.locsyms
= NULL
;
8591 bfd_size_type amt
= cookie
.locsymcount
* sizeof (Elf_External_Sym
);
8592 cookie
.locsyms
= bfd_malloc (amt
);
8593 if (cookie
.locsyms
== NULL
)
8595 freesyms
= cookie
.locsyms
;
8596 if (bfd_seek (abfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
8597 || bfd_bread (cookie
.locsyms
, amt
, abfd
) != amt
)
8600 free (cookie
.locsyms
);
8605 cookie
.locsym_shndx
= NULL
;
8606 if (shndx_hdr
->sh_size
!= 0 && cookie
.locsymcount
!= 0)
8609 amt
= cookie
.locsymcount
* sizeof (Elf_External_Sym_Shndx
);
8610 cookie
.locsym_shndx
= bfd_malloc (amt
);
8611 if (cookie
.locsym_shndx
== NULL
)
8612 goto error_ret_free_loc
;
8613 if (bfd_seek (abfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8614 || bfd_bread (cookie
.locsym_shndx
, amt
, abfd
) != amt
)
8616 free (cookie
.locsym_shndx
);
8617 goto error_ret_free_loc
;
8623 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8624 (abfd
, stab
, (PTR
) NULL
,
8625 (Elf_Internal_Rela
*) NULL
,
8626 info
->keep_memory
));
8629 cookie
.rel
= cookie
.rels
;
8631 cookie
.rels
+ stab
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8632 if (_bfd_discard_section_stabs (abfd
, stab
,
8633 elf_section_data (stab
)->sec_info
,
8634 elf_reloc_symbol_deleted_p
,
8637 if (! info
->keep_memory
)
8646 cookie
.relend
= NULL
;
8647 if (eh
->reloc_count
)
8648 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8649 (abfd
, eh
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
8650 info
->keep_memory
));
8653 cookie
.rel
= cookie
.rels
;
8655 cookie
.rels
+ eh
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8657 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
, ehdr
,
8658 elf_reloc_symbol_deleted_p
,
8661 if (! info
->keep_memory
)
8665 if (bed
->elf_backend_discard_info
)
8667 if (bed
->elf_backend_discard_info (abfd
, &cookie
, info
))
8671 if (cookie
.locsym_shndx
!= NULL
)
8672 free (cookie
.locsym_shndx
);
8674 if (freesyms
!= NULL
)
8678 if (ehdr
&& _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
, ehdr
))
8684 elf_section_ignore_discarded_relocs (sec
)
8687 struct elf_backend_data
*bed
;
8689 switch (elf_section_data (sec
)->sec_info_type
)
8691 case ELF_INFO_TYPE_STABS
:
8692 case ELF_INFO_TYPE_EH_FRAME
:
8698 bed
= get_elf_backend_data (sec
->owner
);
8699 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
8700 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))