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 /* The shared libraries distributed with hpux11 have a bogus
1456 sh_link field for the ".dynamic" section. This code detects
1457 when SHLINK refers to a section that is not a string table
1458 and tries to find the string table for the ".dynsym" section
1460 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[shlink
];
1461 if (shdr
->sh_type
!= SHT_STRTAB
)
1463 asection
*ds
= bfd_get_section_by_name (abfd
, ".dynsym");
1464 int elfdsec
= _bfd_elf_section_from_bfd_section (abfd
, ds
);
1467 shlink
= elf_elfsections (abfd
)[elfdsec
]->sh_link
;
1472 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
1475 for (; extdyn
< extdynend
; extdyn
++)
1477 Elf_Internal_Dyn dyn
;
1479 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
1480 if (dyn
.d_tag
== DT_SONAME
)
1482 unsigned int tagv
= dyn
.d_un
.d_val
;
1483 name
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1487 if (dyn
.d_tag
== DT_NEEDED
)
1489 struct bfd_link_needed_list
*n
, **pn
;
1491 unsigned int tagv
= dyn
.d_un
.d_val
;
1493 amt
= sizeof (struct bfd_link_needed_list
);
1494 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1495 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1496 if (n
== NULL
|| fnm
== NULL
)
1498 amt
= strlen (fnm
) + 1;
1499 anm
= bfd_alloc (abfd
, amt
);
1502 memcpy (anm
, fnm
, (size_t) amt
);
1506 for (pn
= & hash_table
->needed
;
1512 if (dyn
.d_tag
== DT_RUNPATH
)
1514 struct bfd_link_needed_list
*n
, **pn
;
1516 unsigned int tagv
= dyn
.d_un
.d_val
;
1518 /* When we see DT_RPATH before DT_RUNPATH, we have
1519 to clear runpath. Do _NOT_ bfd_release, as that
1520 frees all more recently bfd_alloc'd blocks as
1522 if (rpath
&& hash_table
->runpath
)
1523 hash_table
->runpath
= NULL
;
1525 amt
= sizeof (struct bfd_link_needed_list
);
1526 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1527 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1528 if (n
== NULL
|| fnm
== NULL
)
1530 amt
= strlen (fnm
) + 1;
1531 anm
= bfd_alloc (abfd
, amt
);
1534 memcpy (anm
, fnm
, (size_t) amt
);
1538 for (pn
= & hash_table
->runpath
;
1546 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1547 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
1549 struct bfd_link_needed_list
*n
, **pn
;
1551 unsigned int tagv
= dyn
.d_un
.d_val
;
1553 amt
= sizeof (struct bfd_link_needed_list
);
1554 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1555 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1556 if (n
== NULL
|| fnm
== NULL
)
1558 amt
= strlen (fnm
) + 1;
1559 anm
= bfd_alloc (abfd
, amt
);
1562 memcpy (anm
, fnm
, (size_t) amt
);
1566 for (pn
= & hash_table
->runpath
;
1579 /* We do not want to include any of the sections in a dynamic
1580 object in the output file. We hack by simply clobbering the
1581 list of sections in the BFD. This could be handled more
1582 cleanly by, say, a new section flag; the existing
1583 SEC_NEVER_LOAD flag is not the one we want, because that one
1584 still implies that the section takes up space in the output
1586 bfd_section_list_clear (abfd
);
1588 /* If this is the first dynamic object found in the link, create
1589 the special sections required for dynamic linking. */
1590 if (! hash_table
->dynamic_sections_created
)
1591 if (! elf_link_create_dynamic_sections (abfd
, info
))
1596 /* Add a DT_NEEDED entry for this dynamic object. */
1597 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
1598 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, name
, false);
1599 if (strindex
== (bfd_size_type
) -1)
1602 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
1605 Elf_External_Dyn
*dyncon
, *dynconend
;
1607 /* The hash table size did not change, which means that
1608 the dynamic object name was already entered. If we
1609 have already included this dynamic object in the
1610 link, just ignore it. There is no reason to include
1611 a particular dynamic object more than once. */
1612 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
1613 BFD_ASSERT (sdyn
!= NULL
);
1615 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1616 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1618 for (; dyncon
< dynconend
; dyncon
++)
1620 Elf_Internal_Dyn dyn
;
1622 elf_swap_dyn_in (hash_table
->dynobj
, dyncon
, & dyn
);
1623 if (dyn
.d_tag
== DT_NEEDED
1624 && dyn
.d_un
.d_val
== strindex
)
1628 if (extversym
!= NULL
)
1630 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
1636 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
1640 /* Save the SONAME, if there is one, because sometimes the
1641 linker emulation code will need to know it. */
1643 name
= basename (bfd_get_filename (abfd
));
1644 elf_dt_name (abfd
) = name
;
1647 pos
= hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
);
1648 amt
= extsymcount
* sizeof (Elf_External_Sym
);
1649 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1650 || bfd_bread ((PTR
) buf
, amt
, abfd
) != amt
)
1653 if (shndx_hdr
!= NULL
&& shndx_hdr
->sh_size
!= 0)
1655 amt
= extsymcount
* sizeof (Elf_External_Sym_Shndx
);
1656 pos
= shndx_hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym_Shndx
);
1657 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1658 || bfd_bread ((PTR
) shndx_buf
, amt
, abfd
) != amt
)
1664 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
1665 esymend
= buf
+ extsymcount
;
1666 for (esym
= buf
, shndx
= shndx_buf
;
1668 esym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
),
1669 shndx
= (shndx
!= NULL
? shndx
+ 1 : NULL
))
1671 Elf_Internal_Sym sym
;
1677 struct elf_link_hash_entry
*h
;
1679 boolean size_change_ok
, type_change_ok
;
1680 boolean new_weakdef
;
1681 unsigned int old_alignment
;
1686 elf_swap_symbol_in (abfd
, (const PTR
) esym
, (const PTR
) shndx
, &sym
);
1688 flags
= BSF_NO_FLAGS
;
1690 value
= sym
.st_value
;
1693 bind
= ELF_ST_BIND (sym
.st_info
);
1694 if (bind
== STB_LOCAL
)
1696 /* This should be impossible, since ELF requires that all
1697 global symbols follow all local symbols, and that sh_info
1698 point to the first global symbol. Unfortunatealy, Irix 5
1702 else if (bind
== STB_GLOBAL
)
1704 if (sym
.st_shndx
!= SHN_UNDEF
1705 && sym
.st_shndx
!= SHN_COMMON
)
1708 else if (bind
== STB_WEAK
)
1712 /* Leave it up to the processor backend. */
1715 if (sym
.st_shndx
== SHN_UNDEF
)
1716 sec
= bfd_und_section_ptr
;
1717 else if (sym
.st_shndx
< SHN_LORESERVE
|| sym
.st_shndx
> SHN_HIRESERVE
)
1719 sec
= section_from_elf_index (abfd
, sym
.st_shndx
);
1721 sec
= bfd_abs_section_ptr
;
1722 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
1725 else if (sym
.st_shndx
== SHN_ABS
)
1726 sec
= bfd_abs_section_ptr
;
1727 else if (sym
.st_shndx
== SHN_COMMON
)
1729 sec
= bfd_com_section_ptr
;
1730 /* What ELF calls the size we call the value. What ELF
1731 calls the value we call the alignment. */
1732 value
= sym
.st_size
;
1736 /* Leave it up to the processor backend. */
1739 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
1740 if (name
== (const char *) NULL
)
1743 if (sym
.st_shndx
== SHN_COMMON
&& ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
1745 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
1749 tcomm
= bfd_make_section (abfd
, ".tcommon");
1751 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
1753 | SEC_LINKER_CREATED
1754 | SEC_THREAD_LOCAL
)))
1759 else if (add_symbol_hook
)
1761 if (! (*add_symbol_hook
) (abfd
, info
, &sym
, &name
, &flags
, &sec
,
1765 /* The hook function sets the name to NULL if this symbol
1766 should be skipped for some reason. */
1767 if (name
== (const char *) NULL
)
1771 /* Sanity check that all possibilities were handled. */
1772 if (sec
== (asection
*) NULL
)
1774 bfd_set_error (bfd_error_bad_value
);
1778 if (bfd_is_und_section (sec
)
1779 || bfd_is_com_section (sec
))
1784 size_change_ok
= false;
1785 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
1787 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1789 Elf_Internal_Versym iver
;
1790 unsigned int vernum
= 0;
1794 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
1795 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
1797 /* If this is a hidden symbol, or if it is not version
1798 1, we append the version name to the symbol name.
1799 However, we do not modify a non-hidden absolute
1800 symbol, because it might be the version symbol
1801 itself. FIXME: What if it isn't? */
1802 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
1803 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
1806 size_t namelen
, verlen
, newlen
;
1809 if (sym
.st_shndx
!= SHN_UNDEF
)
1811 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
1813 (*_bfd_error_handler
)
1814 (_("%s: %s: invalid version %u (max %d)"),
1815 bfd_archive_filename (abfd
), name
, vernum
,
1816 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1817 bfd_set_error (bfd_error_bad_value
);
1820 else if (vernum
> 1)
1822 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1828 /* We cannot simply test for the number of
1829 entries in the VERNEED section since the
1830 numbers for the needed versions do not start
1832 Elf_Internal_Verneed
*t
;
1835 for (t
= elf_tdata (abfd
)->verref
;
1839 Elf_Internal_Vernaux
*a
;
1841 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1843 if (a
->vna_other
== vernum
)
1845 verstr
= a
->vna_nodename
;
1854 (*_bfd_error_handler
)
1855 (_("%s: %s: invalid needed version %d"),
1856 bfd_archive_filename (abfd
), name
, vernum
);
1857 bfd_set_error (bfd_error_bad_value
);
1862 namelen
= strlen (name
);
1863 verlen
= strlen (verstr
);
1864 newlen
= namelen
+ verlen
+ 2;
1865 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1866 && sym
.st_shndx
!= SHN_UNDEF
)
1869 newname
= (char *) bfd_alloc (abfd
, (bfd_size_type
) newlen
);
1870 if (newname
== NULL
)
1872 memcpy (newname
, name
, namelen
);
1873 p
= newname
+ namelen
;
1875 /* If this is a defined non-hidden version symbol,
1876 we add another @ to the name. This indicates the
1877 default version of the symbol. */
1878 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1879 && sym
.st_shndx
!= SHN_UNDEF
)
1881 memcpy (p
, verstr
, verlen
+ 1);
1887 if (! elf_merge_symbol (abfd
, info
, name
, &sym
, &sec
, &value
,
1888 sym_hash
, &override
, &type_change_ok
,
1889 &size_change_ok
, dt_needed
))
1896 while (h
->root
.type
== bfd_link_hash_indirect
1897 || h
->root
.type
== bfd_link_hash_warning
)
1898 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1900 /* Remember the old alignment if this is a common symbol, so
1901 that we don't reduce the alignment later on. We can't
1902 check later, because _bfd_generic_link_add_one_symbol
1903 will set a default for the alignment which we want to
1905 if (h
->root
.type
== bfd_link_hash_common
)
1906 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1908 if (elf_tdata (abfd
)->verdef
!= NULL
1912 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1915 if (! (_bfd_generic_link_add_one_symbol
1916 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
1917 false, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
1921 while (h
->root
.type
== bfd_link_hash_indirect
1922 || h
->root
.type
== bfd_link_hash_warning
)
1923 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1926 new_weakdef
= false;
1929 && (flags
& BSF_WEAK
) != 0
1930 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
1931 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1932 && h
->weakdef
== NULL
)
1934 /* Keep a list of all weak defined non function symbols from
1935 a dynamic object, using the weakdef field. Later in this
1936 function we will set the weakdef field to the correct
1937 value. We only put non-function symbols from dynamic
1938 objects on this list, because that happens to be the only
1939 time we need to know the normal symbol corresponding to a
1940 weak symbol, and the information is time consuming to
1941 figure out. If the weakdef field is not already NULL,
1942 then this symbol was already defined by some previous
1943 dynamic object, and we will be using that previous
1944 definition anyhow. */
1951 /* Set the alignment of a common symbol. */
1952 if (sym
.st_shndx
== SHN_COMMON
1953 && h
->root
.type
== bfd_link_hash_common
)
1957 align
= bfd_log2 (sym
.st_value
);
1958 if (align
> old_alignment
1959 /* Permit an alignment power of zero if an alignment of one
1960 is specified and no other alignments have been specified. */
1961 || (sym
.st_value
== 1 && old_alignment
== 0))
1962 h
->root
.u
.c
.p
->alignment_power
= align
;
1965 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1971 /* Remember the symbol size and type. */
1972 if (sym
.st_size
!= 0
1973 && (definition
|| h
->size
== 0))
1975 if (h
->size
!= 0 && h
->size
!= sym
.st_size
&& ! size_change_ok
)
1976 (*_bfd_error_handler
)
1977 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1978 name
, (unsigned long) h
->size
, (unsigned long) sym
.st_size
,
1979 bfd_archive_filename (abfd
));
1981 h
->size
= sym
.st_size
;
1984 /* If this is a common symbol, then we always want H->SIZE
1985 to be the size of the common symbol. The code just above
1986 won't fix the size if a common symbol becomes larger. We
1987 don't warn about a size change here, because that is
1988 covered by --warn-common. */
1989 if (h
->root
.type
== bfd_link_hash_common
)
1990 h
->size
= h
->root
.u
.c
.size
;
1992 if (ELF_ST_TYPE (sym
.st_info
) != STT_NOTYPE
1993 && (definition
|| h
->type
== STT_NOTYPE
))
1995 if (h
->type
!= STT_NOTYPE
1996 && h
->type
!= ELF_ST_TYPE (sym
.st_info
)
1997 && ! type_change_ok
)
1998 (*_bfd_error_handler
)
1999 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
2000 name
, h
->type
, ELF_ST_TYPE (sym
.st_info
),
2001 bfd_archive_filename (abfd
));
2003 h
->type
= ELF_ST_TYPE (sym
.st_info
);
2006 /* If st_other has a processor-specific meaning, specific code
2007 might be needed here. */
2008 if (sym
.st_other
!= 0)
2010 /* Combine visibilities, using the most constraining one. */
2011 unsigned char hvis
= ELF_ST_VISIBILITY (h
->other
);
2012 unsigned char symvis
= ELF_ST_VISIBILITY (sym
.st_other
);
2014 if (symvis
&& (hvis
> symvis
|| hvis
== 0))
2015 h
->other
= sym
.st_other
;
2017 /* If neither has visibility, use the st_other of the
2018 definition. This is an arbitrary choice, since the
2019 other bits have no general meaning. */
2020 if (!symvis
&& !hvis
2021 && (definition
|| h
->other
== 0))
2022 h
->other
= sym
.st_other
;
2025 /* Set a flag in the hash table entry indicating the type of
2026 reference or definition we just found. Keep a count of
2027 the number of dynamic symbols we find. A dynamic symbol
2028 is one which is referenced or defined by both a regular
2029 object and a shared object. */
2030 old_flags
= h
->elf_link_hash_flags
;
2036 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
2037 if (bind
!= STB_WEAK
)
2038 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
2041 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
2043 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2044 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
2050 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
2052 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
2053 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
2054 | ELF_LINK_HASH_REF_REGULAR
)) != 0
2055 || (h
->weakdef
!= NULL
2057 && h
->weakdef
->dynindx
!= -1))
2061 h
->elf_link_hash_flags
|= new_flag
;
2063 /* Check to see if we need to add an indirect symbol for
2064 the default name. */
2065 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
2066 if (! elf_add_default_symbol (abfd
, info
, h
, name
, &sym
,
2067 &sec
, &value
, &dynsym
,
2068 override
, dt_needed
))
2071 if (dynsym
&& h
->dynindx
== -1)
2073 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2075 if (h
->weakdef
!= NULL
2077 && h
->weakdef
->dynindx
== -1)
2079 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2083 else if (dynsym
&& h
->dynindx
!= -1)
2084 /* If the symbol already has a dynamic index, but
2085 visibility says it should not be visible, turn it into
2087 switch (ELF_ST_VISIBILITY (h
->other
))
2091 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2095 if (dt_needed
&& definition
2096 && (h
->elf_link_hash_flags
2097 & ELF_LINK_HASH_REF_REGULAR
) != 0)
2099 bfd_size_type oldsize
;
2100 bfd_size_type strindex
;
2102 if (! is_elf_hash_table (info
))
2105 /* The symbol from a DT_NEEDED object is referenced from
2106 the regular object to create a dynamic executable. We
2107 have to make sure there is a DT_NEEDED entry for it. */
2110 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2111 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
,
2112 elf_dt_soname (abfd
), false);
2113 if (strindex
== (bfd_size_type
) -1)
2116 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2119 Elf_External_Dyn
*dyncon
, *dynconend
;
2121 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
,
2123 BFD_ASSERT (sdyn
!= NULL
);
2125 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
2126 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
2128 for (; dyncon
< dynconend
; dyncon
++)
2130 Elf_Internal_Dyn dyn
;
2132 elf_swap_dyn_in (hash_table
->dynobj
,
2134 BFD_ASSERT (dyn
.d_tag
!= DT_NEEDED
||
2135 dyn
.d_un
.d_val
!= strindex
);
2139 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
2145 /* Now set the weakdefs field correctly for all the weak defined
2146 symbols we found. The only way to do this is to search all the
2147 symbols. Since we only need the information for non functions in
2148 dynamic objects, that's the only time we actually put anything on
2149 the list WEAKS. We need this information so that if a regular
2150 object refers to a symbol defined weakly in a dynamic object, the
2151 real symbol in the dynamic object is also put in the dynamic
2152 symbols; we also must arrange for both symbols to point to the
2153 same memory location. We could handle the general case of symbol
2154 aliasing, but a general symbol alias can only be generated in
2155 assembler code, handling it correctly would be very time
2156 consuming, and other ELF linkers don't handle general aliasing
2158 while (weaks
!= NULL
)
2160 struct elf_link_hash_entry
*hlook
;
2163 struct elf_link_hash_entry
**hpp
;
2164 struct elf_link_hash_entry
**hppend
;
2167 weaks
= hlook
->weakdef
;
2168 hlook
->weakdef
= NULL
;
2170 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
2171 || hlook
->root
.type
== bfd_link_hash_defweak
2172 || hlook
->root
.type
== bfd_link_hash_common
2173 || hlook
->root
.type
== bfd_link_hash_indirect
);
2174 slook
= hlook
->root
.u
.def
.section
;
2175 vlook
= hlook
->root
.u
.def
.value
;
2177 hpp
= elf_sym_hashes (abfd
);
2178 hppend
= hpp
+ extsymcount
;
2179 for (; hpp
< hppend
; hpp
++)
2181 struct elf_link_hash_entry
*h
;
2184 if (h
!= NULL
&& h
!= hlook
2185 && h
->root
.type
== bfd_link_hash_defined
2186 && h
->root
.u
.def
.section
== slook
2187 && h
->root
.u
.def
.value
== vlook
)
2191 /* If the weak definition is in the list of dynamic
2192 symbols, make sure the real definition is put there
2194 if (hlook
->dynindx
!= -1
2195 && h
->dynindx
== -1)
2197 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2201 /* If the real definition is in the list of dynamic
2202 symbols, make sure the weak definition is put there
2203 as well. If we don't do this, then the dynamic
2204 loader might not merge the entries for the real
2205 definition and the weak definition. */
2206 if (h
->dynindx
!= -1
2207 && hlook
->dynindx
== -1)
2209 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
2224 if (extversym
!= NULL
)
2230 /* If this object is the same format as the output object, and it is
2231 not a shared library, then let the backend look through the
2234 This is required to build global offset table entries and to
2235 arrange for dynamic relocs. It is not required for the
2236 particular common case of linking non PIC code, even when linking
2237 against shared libraries, but unfortunately there is no way of
2238 knowing whether an object file has been compiled PIC or not.
2239 Looking through the relocs is not particularly time consuming.
2240 The problem is that we must either (1) keep the relocs in memory,
2241 which causes the linker to require additional runtime memory or
2242 (2) read the relocs twice from the input file, which wastes time.
2243 This would be a good case for using mmap.
2245 I have no idea how to handle linking PIC code into a file of a
2246 different format. It probably can't be done. */
2247 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
2249 && abfd
->xvec
== info
->hash
->creator
2250 && check_relocs
!= NULL
)
2254 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2256 Elf_Internal_Rela
*internal_relocs
;
2259 if ((o
->flags
& SEC_RELOC
) == 0
2260 || o
->reloc_count
== 0
2261 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
2262 && (o
->flags
& SEC_DEBUGGING
) != 0)
2263 || bfd_is_abs_section (o
->output_section
))
2266 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
2267 (abfd
, o
, (PTR
) NULL
,
2268 (Elf_Internal_Rela
*) NULL
,
2269 info
->keep_memory
));
2270 if (internal_relocs
== NULL
)
2273 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
2275 if (! info
->keep_memory
)
2276 free (internal_relocs
);
2283 /* If this is a non-traditional, non-relocateable link, try to
2284 optimize the handling of the .stab/.stabstr sections. */
2286 && ! info
->relocateable
2287 && ! info
->traditional_format
2288 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
2289 && is_elf_hash_table (info
)
2290 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
2292 asection
*stab
, *stabstr
;
2294 stab
= bfd_get_section_by_name (abfd
, ".stab");
2296 && (stab
->flags
& SEC_MERGE
) == 0
2297 && !bfd_is_abs_section (stab
->output_section
))
2299 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
2301 if (stabstr
!= NULL
)
2303 struct bfd_elf_section_data
*secdata
;
2305 secdata
= elf_section_data (stab
);
2306 if (! _bfd_link_section_stabs (abfd
,
2307 & hash_table
->stab_info
,
2309 &secdata
->sec_info
))
2311 if (secdata
->sec_info
)
2312 secdata
->sec_info_type
= ELF_INFO_TYPE_STABS
;
2317 if (! info
->relocateable
&& ! dynamic
2318 && is_elf_hash_table (info
))
2322 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2323 if ((s
->flags
& SEC_MERGE
) != 0
2324 && !bfd_is_abs_section (s
->output_section
))
2326 struct bfd_elf_section_data
*secdata
;
2328 secdata
= elf_section_data (s
);
2329 if (! _bfd_merge_section (abfd
,
2330 & hash_table
->merge_info
,
2331 s
, &secdata
->sec_info
))
2333 else if (secdata
->sec_info
)
2334 secdata
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
2338 if (is_elf_hash_table (info
))
2340 /* Add this bfd to the loaded list. */
2341 struct elf_link_loaded_list
*n
;
2343 n
= ((struct elf_link_loaded_list
*)
2344 bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
)));
2348 n
->next
= hash_table
->loaded
;
2349 hash_table
->loaded
= n
;
2359 if (extversym
!= NULL
)
2364 /* Create some sections which will be filled in with dynamic linking
2365 information. ABFD is an input file which requires dynamic sections
2366 to be created. The dynamic sections take up virtual memory space
2367 when the final executable is run, so we need to create them before
2368 addresses are assigned to the output sections. We work out the
2369 actual contents and size of these sections later. */
2372 elf_link_create_dynamic_sections (abfd
, info
)
2374 struct bfd_link_info
*info
;
2377 register asection
*s
;
2378 struct elf_link_hash_entry
*h
;
2379 struct elf_backend_data
*bed
;
2381 if (! is_elf_hash_table (info
))
2384 if (elf_hash_table (info
)->dynamic_sections_created
)
2387 /* Make sure that all dynamic sections use the same input BFD. */
2388 if (elf_hash_table (info
)->dynobj
== NULL
)
2389 elf_hash_table (info
)->dynobj
= abfd
;
2391 abfd
= elf_hash_table (info
)->dynobj
;
2393 /* Note that we set the SEC_IN_MEMORY flag for all of these
2395 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
2396 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
2398 /* A dynamically linked executable has a .interp section, but a
2399 shared library does not. */
2402 s
= bfd_make_section (abfd
, ".interp");
2404 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2408 if (! info
->traditional_format
2409 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
2411 s
= bfd_make_section (abfd
, ".eh_frame_hdr");
2413 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2414 || ! bfd_set_section_alignment (abfd
, s
, 2))
2418 /* Create sections to hold version informations. These are removed
2419 if they are not needed. */
2420 s
= bfd_make_section (abfd
, ".gnu.version_d");
2422 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2423 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2426 s
= bfd_make_section (abfd
, ".gnu.version");
2428 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2429 || ! bfd_set_section_alignment (abfd
, s
, 1))
2432 s
= bfd_make_section (abfd
, ".gnu.version_r");
2434 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2435 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2438 s
= bfd_make_section (abfd
, ".dynsym");
2440 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2441 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2444 s
= bfd_make_section (abfd
, ".dynstr");
2446 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2449 /* Create a strtab to hold the dynamic symbol names. */
2450 if (elf_hash_table (info
)->dynstr
== NULL
)
2452 elf_hash_table (info
)->dynstr
= _bfd_elf_strtab_init ();
2453 if (elf_hash_table (info
)->dynstr
== NULL
)
2457 s
= bfd_make_section (abfd
, ".dynamic");
2459 || ! bfd_set_section_flags (abfd
, s
, flags
)
2460 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2463 /* The special symbol _DYNAMIC is always set to the start of the
2464 .dynamic section. This call occurs before we have processed the
2465 symbols for any dynamic object, so we don't have to worry about
2466 overriding a dynamic definition. We could set _DYNAMIC in a
2467 linker script, but we only want to define it if we are, in fact,
2468 creating a .dynamic section. We don't want to define it if there
2469 is no .dynamic section, since on some ELF platforms the start up
2470 code examines it to decide how to initialize the process. */
2472 if (! (_bfd_generic_link_add_one_symbol
2473 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
2474 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
2475 (struct bfd_link_hash_entry
**) &h
)))
2477 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2478 h
->type
= STT_OBJECT
;
2481 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2484 bed
= get_elf_backend_data (abfd
);
2486 s
= bfd_make_section (abfd
, ".hash");
2488 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2489 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2491 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
2493 /* Let the backend create the rest of the sections. This lets the
2494 backend set the right flags. The backend will normally create
2495 the .got and .plt sections. */
2496 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
2499 elf_hash_table (info
)->dynamic_sections_created
= true;
2504 /* Add an entry to the .dynamic table. */
2507 elf_add_dynamic_entry (info
, tag
, val
)
2508 struct bfd_link_info
*info
;
2512 Elf_Internal_Dyn dyn
;
2515 bfd_size_type newsize
;
2516 bfd_byte
*newcontents
;
2518 if (! is_elf_hash_table (info
))
2521 dynobj
= elf_hash_table (info
)->dynobj
;
2523 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
2524 BFD_ASSERT (s
!= NULL
);
2526 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
2527 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
2528 if (newcontents
== NULL
)
2532 dyn
.d_un
.d_val
= val
;
2533 elf_swap_dyn_out (dynobj
, &dyn
,
2534 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
2536 s
->_raw_size
= newsize
;
2537 s
->contents
= newcontents
;
2542 /* Record a new local dynamic symbol. */
2545 elf_link_record_local_dynamic_symbol (info
, input_bfd
, input_indx
)
2546 struct bfd_link_info
*info
;
2550 struct elf_link_local_dynamic_entry
*entry
;
2551 struct elf_link_hash_table
*eht
;
2552 struct elf_strtab_hash
*dynstr
;
2553 Elf_External_Sym esym
;
2554 Elf_External_Sym_Shndx eshndx
;
2555 Elf_External_Sym_Shndx
*shndx
;
2556 unsigned long dynstr_index
;
2561 if (! is_elf_hash_table (info
))
2564 /* See if the entry exists already. */
2565 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
2566 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
2569 entry
= (struct elf_link_local_dynamic_entry
*)
2570 bfd_alloc (input_bfd
, (bfd_size_type
) sizeof (*entry
));
2574 /* Go find the symbol, so that we can find it's name. */
2575 amt
= sizeof (Elf_External_Sym
);
2576 pos
= elf_tdata (input_bfd
)->symtab_hdr
.sh_offset
+ input_indx
* amt
;
2577 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
2578 || bfd_bread ((PTR
) &esym
, amt
, input_bfd
) != amt
)
2581 if (elf_tdata (input_bfd
)->symtab_shndx_hdr
.sh_size
!= 0)
2583 amt
= sizeof (Elf_External_Sym_Shndx
);
2584 pos
= elf_tdata (input_bfd
)->symtab_shndx_hdr
.sh_offset
;
2585 pos
+= input_indx
* amt
;
2587 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
2588 || bfd_bread ((PTR
) shndx
, amt
, input_bfd
) != amt
)
2591 elf_swap_symbol_in (input_bfd
, (const PTR
) &esym
, (const PTR
) shndx
,
2594 name
= (bfd_elf_string_from_elf_section
2595 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
2596 entry
->isym
.st_name
));
2598 dynstr
= elf_hash_table (info
)->dynstr
;
2601 /* Create a strtab to hold the dynamic symbol names. */
2602 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
2607 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, false);
2608 if (dynstr_index
== (unsigned long) -1)
2610 entry
->isym
.st_name
= dynstr_index
;
2612 eht
= elf_hash_table (info
);
2614 entry
->next
= eht
->dynlocal
;
2615 eht
->dynlocal
= entry
;
2616 entry
->input_bfd
= input_bfd
;
2617 entry
->input_indx
= input_indx
;
2620 /* Whatever binding the symbol had before, it's now local. */
2622 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
2624 /* The dynindx will be set at the end of size_dynamic_sections. */
2629 /* Read and swap the relocs from the section indicated by SHDR. This
2630 may be either a REL or a RELA section. The relocations are
2631 translated into RELA relocations and stored in INTERNAL_RELOCS,
2632 which should have already been allocated to contain enough space.
2633 The EXTERNAL_RELOCS are a buffer where the external form of the
2634 relocations should be stored.
2636 Returns false if something goes wrong. */
2639 elf_link_read_relocs_from_section (abfd
, shdr
, external_relocs
,
2642 Elf_Internal_Shdr
*shdr
;
2643 PTR external_relocs
;
2644 Elf_Internal_Rela
*internal_relocs
;
2646 struct elf_backend_data
*bed
;
2649 /* If there aren't any relocations, that's OK. */
2653 /* Position ourselves at the start of the section. */
2654 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2657 /* Read the relocations. */
2658 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2661 bed
= get_elf_backend_data (abfd
);
2663 /* Convert the external relocations to the internal format. */
2664 if (shdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2666 Elf_External_Rel
*erel
;
2667 Elf_External_Rel
*erelend
;
2668 Elf_Internal_Rela
*irela
;
2669 Elf_Internal_Rel
*irel
;
2671 erel
= (Elf_External_Rel
*) external_relocs
;
2672 erelend
= erel
+ NUM_SHDR_ENTRIES (shdr
);
2673 irela
= internal_relocs
;
2674 amt
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
2675 irel
= bfd_alloc (abfd
, amt
);
2676 for (; erel
< erelend
; erel
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2680 if (bed
->s
->swap_reloc_in
)
2681 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
2683 elf_swap_reloc_in (abfd
, erel
, irel
);
2685 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; ++i
)
2687 irela
[i
].r_offset
= irel
[i
].r_offset
;
2688 irela
[i
].r_info
= irel
[i
].r_info
;
2689 irela
[i
].r_addend
= 0;
2695 Elf_External_Rela
*erela
;
2696 Elf_External_Rela
*erelaend
;
2697 Elf_Internal_Rela
*irela
;
2699 BFD_ASSERT (shdr
->sh_entsize
== sizeof (Elf_External_Rela
));
2701 erela
= (Elf_External_Rela
*) external_relocs
;
2702 erelaend
= erela
+ NUM_SHDR_ENTRIES (shdr
);
2703 irela
= internal_relocs
;
2704 for (; erela
< erelaend
; erela
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2706 if (bed
->s
->swap_reloca_in
)
2707 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
2709 elf_swap_reloca_in (abfd
, erela
, irela
);
2716 /* Read and swap the relocs for a section O. They may have been
2717 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2718 not NULL, they are used as buffers to read into. They are known to
2719 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2720 the return value is allocated using either malloc or bfd_alloc,
2721 according to the KEEP_MEMORY argument. If O has two relocation
2722 sections (both REL and RELA relocations), then the REL_HDR
2723 relocations will appear first in INTERNAL_RELOCS, followed by the
2724 REL_HDR2 relocations. */
2727 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
2731 PTR external_relocs
;
2732 Elf_Internal_Rela
*internal_relocs
;
2733 boolean keep_memory
;
2735 Elf_Internal_Shdr
*rel_hdr
;
2737 Elf_Internal_Rela
*alloc2
= NULL
;
2738 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2740 if (elf_section_data (o
)->relocs
!= NULL
)
2741 return elf_section_data (o
)->relocs
;
2743 if (o
->reloc_count
== 0)
2746 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2748 if (internal_relocs
== NULL
)
2752 size
= o
->reloc_count
;
2753 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2755 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2757 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2758 if (internal_relocs
== NULL
)
2762 if (external_relocs
== NULL
)
2764 bfd_size_type size
= rel_hdr
->sh_size
;
2766 if (elf_section_data (o
)->rel_hdr2
)
2767 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2768 alloc1
= (PTR
) bfd_malloc (size
);
2771 external_relocs
= alloc1
;
2774 if (!elf_link_read_relocs_from_section (abfd
, rel_hdr
,
2778 if (!elf_link_read_relocs_from_section
2780 elf_section_data (o
)->rel_hdr2
,
2781 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2782 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2783 * bed
->s
->int_rels_per_ext_rel
)))
2786 /* Cache the results for next time, if we can. */
2788 elf_section_data (o
)->relocs
= internal_relocs
;
2793 /* Don't free alloc2, since if it was allocated we are passing it
2794 back (under the name of internal_relocs). */
2796 return internal_relocs
;
2806 /* Record an assignment to a symbol made by a linker script. We need
2807 this in case some dynamic object refers to this symbol. */
2810 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
2811 bfd
*output_bfd ATTRIBUTE_UNUSED
;
2812 struct bfd_link_info
*info
;
2816 struct elf_link_hash_entry
*h
;
2818 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2821 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, true, false);
2825 if (h
->root
.type
== bfd_link_hash_new
)
2826 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_ELF
;
2828 /* If this symbol is being provided by the linker script, and it is
2829 currently defined by a dynamic object, but not by a regular
2830 object, then mark it as undefined so that the generic linker will
2831 force the correct value. */
2833 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2834 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2835 h
->root
.type
= bfd_link_hash_undefined
;
2837 /* If this symbol is not being provided by the linker script, and it is
2838 currently defined by a dynamic object, but not by a regular object,
2839 then clear out any version information because the symbol will not be
2840 associated with the dynamic object any more. */
2842 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2843 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2844 h
->verinfo
.verdef
= NULL
;
2846 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2848 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2849 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
2851 && h
->dynindx
== -1)
2853 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2856 /* If this is a weak defined symbol, and we know a corresponding
2857 real symbol from the same dynamic object, make sure the real
2858 symbol is also made into a dynamic symbol. */
2859 if (h
->weakdef
!= NULL
2860 && h
->weakdef
->dynindx
== -1)
2862 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2870 /* This structure is used to pass information to
2871 elf_link_assign_sym_version. */
2873 struct elf_assign_sym_version_info
2877 /* General link information. */
2878 struct bfd_link_info
*info
;
2880 struct bfd_elf_version_tree
*verdefs
;
2881 /* Whether we had a failure. */
2885 /* This structure is used to pass information to
2886 elf_link_find_version_dependencies. */
2888 struct elf_find_verdep_info
2892 /* General link information. */
2893 struct bfd_link_info
*info
;
2894 /* The number of dependencies. */
2896 /* Whether we had a failure. */
2900 /* Array used to determine the number of hash table buckets to use
2901 based on the number of symbols there are. If there are fewer than
2902 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2903 fewer than 37 we use 17 buckets, and so forth. We never use more
2904 than 32771 buckets. */
2906 static const size_t elf_buckets
[] =
2908 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2912 /* Compute bucket count for hashing table. We do not use a static set
2913 of possible tables sizes anymore. Instead we determine for all
2914 possible reasonable sizes of the table the outcome (i.e., the
2915 number of collisions etc) and choose the best solution. The
2916 weighting functions are not too simple to allow the table to grow
2917 without bounds. Instead one of the weighting factors is the size.
2918 Therefore the result is always a good payoff between few collisions
2919 (= short chain lengths) and table size. */
2921 compute_bucket_count (info
)
2922 struct bfd_link_info
*info
;
2924 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2925 size_t best_size
= 0;
2926 unsigned long int *hashcodes
;
2927 unsigned long int *hashcodesp
;
2928 unsigned long int i
;
2931 /* Compute the hash values for all exported symbols. At the same
2932 time store the values in an array so that we could use them for
2935 amt
*= sizeof (unsigned long int);
2936 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
2937 if (hashcodes
== NULL
)
2939 hashcodesp
= hashcodes
;
2941 /* Put all hash values in HASHCODES. */
2942 elf_link_hash_traverse (elf_hash_table (info
),
2943 elf_collect_hash_codes
, &hashcodesp
);
2945 /* We have a problem here. The following code to optimize the table
2946 size requires an integer type with more the 32 bits. If
2947 BFD_HOST_U_64_BIT is set we know about such a type. */
2948 #ifdef BFD_HOST_U_64_BIT
2951 unsigned long int nsyms
= hashcodesp
- hashcodes
;
2954 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
2955 unsigned long int *counts
;
2957 /* Possible optimization parameters: if we have NSYMS symbols we say
2958 that the hashing table must at least have NSYMS/4 and at most
2960 minsize
= nsyms
/ 4;
2963 best_size
= maxsize
= nsyms
* 2;
2965 /* Create array where we count the collisions in. We must use bfd_malloc
2966 since the size could be large. */
2968 amt
*= sizeof (unsigned long int);
2969 counts
= (unsigned long int *) bfd_malloc (amt
);
2976 /* Compute the "optimal" size for the hash table. The criteria is a
2977 minimal chain length. The minor criteria is (of course) the size
2979 for (i
= minsize
; i
< maxsize
; ++i
)
2981 /* Walk through the array of hashcodes and count the collisions. */
2982 BFD_HOST_U_64_BIT max
;
2983 unsigned long int j
;
2984 unsigned long int fact
;
2986 memset (counts
, '\0', i
* sizeof (unsigned long int));
2988 /* Determine how often each hash bucket is used. */
2989 for (j
= 0; j
< nsyms
; ++j
)
2990 ++counts
[hashcodes
[j
] % i
];
2992 /* For the weight function we need some information about the
2993 pagesize on the target. This is information need not be 100%
2994 accurate. Since this information is not available (so far) we
2995 define it here to a reasonable default value. If it is crucial
2996 to have a better value some day simply define this value. */
2997 # ifndef BFD_TARGET_PAGESIZE
2998 # define BFD_TARGET_PAGESIZE (4096)
3001 /* We in any case need 2 + NSYMS entries for the size values and
3003 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
3006 /* Variant 1: optimize for short chains. We add the squares
3007 of all the chain lengths (which favous many small chain
3008 over a few long chains). */
3009 for (j
= 0; j
< i
; ++j
)
3010 max
+= counts
[j
] * counts
[j
];
3012 /* This adds penalties for the overall size of the table. */
3013 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
3016 /* Variant 2: Optimize a lot more for small table. Here we
3017 also add squares of the size but we also add penalties for
3018 empty slots (the +1 term). */
3019 for (j
= 0; j
< i
; ++j
)
3020 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
3022 /* The overall size of the table is considered, but not as
3023 strong as in variant 1, where it is squared. */
3024 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
3028 /* Compare with current best results. */
3029 if (max
< best_chlen
)
3039 #endif /* defined (BFD_HOST_U_64_BIT) */
3041 /* This is the fallback solution if no 64bit type is available or if we
3042 are not supposed to spend much time on optimizations. We select the
3043 bucket count using a fixed set of numbers. */
3044 for (i
= 0; elf_buckets
[i
] != 0; i
++)
3046 best_size
= elf_buckets
[i
];
3047 if (dynsymcount
< elf_buckets
[i
+ 1])
3052 /* Free the arrays we needed. */
3058 /* Set up the sizes and contents of the ELF dynamic sections. This is
3059 called by the ELF linker emulation before_allocation routine. We
3060 must set the sizes of the sections before the linker sets the
3061 addresses of the various sections. */
3064 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
3066 auxiliary_filters
, info
, sinterpptr
,
3071 const char *filter_shlib
;
3072 const char * const *auxiliary_filters
;
3073 struct bfd_link_info
*info
;
3074 asection
**sinterpptr
;
3075 struct bfd_elf_version_tree
*verdefs
;
3077 bfd_size_type soname_indx
;
3079 struct elf_backend_data
*bed
;
3080 struct elf_assign_sym_version_info asvinfo
;
3084 soname_indx
= (bfd_size_type
) -1;
3086 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
3089 if (! is_elf_hash_table (info
))
3092 /* Any syms created from now on start with -1 in
3093 got.refcount/offset and plt.refcount/offset. */
3094 elf_hash_table (info
)->init_refcount
= -1;
3096 /* The backend may have to create some sections regardless of whether
3097 we're dynamic or not. */
3098 bed
= get_elf_backend_data (output_bfd
);
3099 if (bed
->elf_backend_always_size_sections
3100 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
3103 dynobj
= elf_hash_table (info
)->dynobj
;
3105 /* If there were no dynamic objects in the link, there is nothing to
3110 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
3113 if (elf_hash_table (info
)->dynamic_sections_created
)
3115 struct elf_info_failed eif
;
3116 struct elf_link_hash_entry
*h
;
3119 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
3120 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
3124 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3126 if (soname_indx
== (bfd_size_type
) -1
3127 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SONAME
,
3134 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMBOLIC
,
3137 info
->flags
|= DF_SYMBOLIC
;
3144 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
3146 if (info
->new_dtags
)
3147 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
3148 if (indx
== (bfd_size_type
) -1
3149 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RPATH
, indx
)
3151 && ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RUNPATH
,
3156 if (filter_shlib
!= NULL
)
3160 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3161 filter_shlib
, true);
3162 if (indx
== (bfd_size_type
) -1
3163 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FILTER
, indx
))
3167 if (auxiliary_filters
!= NULL
)
3169 const char * const *p
;
3171 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
3175 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3177 if (indx
== (bfd_size_type
) -1
3178 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_AUXILIARY
,
3185 eif
.verdefs
= verdefs
;
3188 /* If we are supposed to export all symbols into the dynamic symbol
3189 table (this is not the normal case), then do so. */
3190 if (info
->export_dynamic
)
3192 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
3198 /* Attach all the symbols to their version information. */
3199 asvinfo
.output_bfd
= output_bfd
;
3200 asvinfo
.info
= info
;
3201 asvinfo
.verdefs
= verdefs
;
3202 asvinfo
.failed
= false;
3204 elf_link_hash_traverse (elf_hash_table (info
),
3205 elf_link_assign_sym_version
,
3210 /* Find all symbols which were defined in a dynamic object and make
3211 the backend pick a reasonable value for them. */
3212 elf_link_hash_traverse (elf_hash_table (info
),
3213 elf_adjust_dynamic_symbol
,
3218 /* Add some entries to the .dynamic section. We fill in some of the
3219 values later, in elf_bfd_final_link, but we must add the entries
3220 now so that we know the final size of the .dynamic section. */
3222 /* If there are initialization and/or finalization functions to
3223 call then add the corresponding DT_INIT/DT_FINI entries. */
3224 h
= (info
->init_function
3225 ? elf_link_hash_lookup (elf_hash_table (info
),
3226 info
->init_function
, false,
3230 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3231 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3233 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT
, (bfd_vma
) 0))
3236 h
= (info
->fini_function
3237 ? elf_link_hash_lookup (elf_hash_table (info
),
3238 info
->fini_function
, false,
3242 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3243 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3245 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI
, (bfd_vma
) 0))
3249 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
3251 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3257 for (sub
= info
->input_bfds
; sub
!= NULL
;
3258 sub
= sub
->link_next
)
3259 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3260 if (elf_section_data (o
)->this_hdr
.sh_type
3261 == SHT_PREINIT_ARRAY
)
3263 (*_bfd_error_handler
)
3264 (_("%s: .preinit_array section is not allowed in DSO"),
3265 bfd_archive_filename (sub
));
3269 bfd_set_error (bfd_error_nonrepresentable_section
);
3273 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAY
,
3275 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAYSZ
,
3279 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
3281 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAY
,
3283 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAYSZ
,
3287 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
3289 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAY
,
3291 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAYSZ
,
3296 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
3297 /* If .dynstr is excluded from the link, we don't want any of
3298 these tags. Strictly, we should be checking each section
3299 individually; This quick check covers for the case where
3300 someone does a /DISCARD/ : { *(*) }. */
3301 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
3303 bfd_size_type strsize
;
3305 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3306 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_HASH
, (bfd_vma
) 0)
3307 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRTAB
, (bfd_vma
) 0)
3308 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMTAB
, (bfd_vma
) 0)
3309 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRSZ
, strsize
)
3310 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMENT
,
3311 (bfd_vma
) sizeof (Elf_External_Sym
)))
3316 /* The backend must work out the sizes of all the other dynamic
3318 if (bed
->elf_backend_size_dynamic_sections
3319 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
3322 if (elf_hash_table (info
)->dynamic_sections_created
)
3324 bfd_size_type dynsymcount
;
3326 size_t bucketcount
= 0;
3327 size_t hash_entry_size
;
3328 unsigned int dtagcount
;
3330 /* Set up the version definition section. */
3331 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3332 BFD_ASSERT (s
!= NULL
);
3334 /* We may have created additional version definitions if we are
3335 just linking a regular application. */
3336 verdefs
= asvinfo
.verdefs
;
3338 /* Skip anonymous version tag. */
3339 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
3340 verdefs
= verdefs
->next
;
3342 if (verdefs
== NULL
)
3343 _bfd_strip_section_from_output (info
, s
);
3348 struct bfd_elf_version_tree
*t
;
3350 Elf_Internal_Verdef def
;
3351 Elf_Internal_Verdaux defaux
;
3356 /* Make space for the base version. */
3357 size
+= sizeof (Elf_External_Verdef
);
3358 size
+= sizeof (Elf_External_Verdaux
);
3361 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3363 struct bfd_elf_version_deps
*n
;
3365 size
+= sizeof (Elf_External_Verdef
);
3366 size
+= sizeof (Elf_External_Verdaux
);
3369 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3370 size
+= sizeof (Elf_External_Verdaux
);
3373 s
->_raw_size
= size
;
3374 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3375 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3378 /* Fill in the version definition section. */
3382 def
.vd_version
= VER_DEF_CURRENT
;
3383 def
.vd_flags
= VER_FLG_BASE
;
3386 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3387 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3388 + sizeof (Elf_External_Verdaux
));
3390 if (soname_indx
!= (bfd_size_type
) -1)
3392 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3394 def
.vd_hash
= bfd_elf_hash (soname
);
3395 defaux
.vda_name
= soname_indx
;
3402 name
= basename (output_bfd
->filename
);
3403 def
.vd_hash
= bfd_elf_hash (name
);
3404 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3406 if (indx
== (bfd_size_type
) -1)
3408 defaux
.vda_name
= indx
;
3410 defaux
.vda_next
= 0;
3412 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3413 (Elf_External_Verdef
*) p
);
3414 p
+= sizeof (Elf_External_Verdef
);
3415 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3416 (Elf_External_Verdaux
*) p
);
3417 p
+= sizeof (Elf_External_Verdaux
);
3419 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3422 struct bfd_elf_version_deps
*n
;
3423 struct elf_link_hash_entry
*h
;
3426 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3429 /* Add a symbol representing this version. */
3431 if (! (_bfd_generic_link_add_one_symbol
3432 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
3433 (bfd_vma
) 0, (const char *) NULL
, false,
3434 get_elf_backend_data (dynobj
)->collect
,
3435 (struct bfd_link_hash_entry
**) &h
)))
3437 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
3438 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3439 h
->type
= STT_OBJECT
;
3440 h
->verinfo
.vertree
= t
;
3442 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
3445 def
.vd_version
= VER_DEF_CURRENT
;
3447 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
3448 def
.vd_flags
|= VER_FLG_WEAK
;
3449 def
.vd_ndx
= t
->vernum
+ 1;
3450 def
.vd_cnt
= cdeps
+ 1;
3451 def
.vd_hash
= bfd_elf_hash (t
->name
);
3452 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3453 if (t
->next
!= NULL
)
3454 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3455 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
3459 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3460 (Elf_External_Verdef
*) p
);
3461 p
+= sizeof (Elf_External_Verdef
);
3463 defaux
.vda_name
= h
->dynstr_index
;
3464 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3466 if (t
->deps
== NULL
)
3467 defaux
.vda_next
= 0;
3469 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3470 t
->name_indx
= defaux
.vda_name
;
3472 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3473 (Elf_External_Verdaux
*) p
);
3474 p
+= sizeof (Elf_External_Verdaux
);
3476 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3478 if (n
->version_needed
== NULL
)
3480 /* This can happen if there was an error in the
3482 defaux
.vda_name
= 0;
3486 defaux
.vda_name
= n
->version_needed
->name_indx
;
3487 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3490 if (n
->next
== NULL
)
3491 defaux
.vda_next
= 0;
3493 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3495 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3496 (Elf_External_Verdaux
*) p
);
3497 p
+= sizeof (Elf_External_Verdaux
);
3501 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEF
, (bfd_vma
) 0)
3502 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEFNUM
,
3506 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
3509 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
3511 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS
, info
->flags
))
3518 info
->flags_1
&= ~ (DF_1_INITFIRST
3521 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS_1
,
3526 /* Work out the size of the version reference section. */
3528 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3529 BFD_ASSERT (s
!= NULL
);
3531 struct elf_find_verdep_info sinfo
;
3533 sinfo
.output_bfd
= output_bfd
;
3535 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
3536 if (sinfo
.vers
== 0)
3538 sinfo
.failed
= false;
3540 elf_link_hash_traverse (elf_hash_table (info
),
3541 elf_link_find_version_dependencies
,
3544 if (elf_tdata (output_bfd
)->verref
== NULL
)
3545 _bfd_strip_section_from_output (info
, s
);
3548 Elf_Internal_Verneed
*t
;
3553 /* Build the version definition section. */
3556 for (t
= elf_tdata (output_bfd
)->verref
;
3560 Elf_Internal_Vernaux
*a
;
3562 size
+= sizeof (Elf_External_Verneed
);
3564 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3565 size
+= sizeof (Elf_External_Vernaux
);
3568 s
->_raw_size
= size
;
3569 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3570 if (s
->contents
== NULL
)
3574 for (t
= elf_tdata (output_bfd
)->verref
;
3579 Elf_Internal_Vernaux
*a
;
3583 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3586 t
->vn_version
= VER_NEED_CURRENT
;
3588 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3589 elf_dt_name (t
->vn_bfd
) != NULL
3590 ? elf_dt_name (t
->vn_bfd
)
3591 : basename (t
->vn_bfd
->filename
),
3593 if (indx
== (bfd_size_type
) -1)
3596 t
->vn_aux
= sizeof (Elf_External_Verneed
);
3597 if (t
->vn_nextref
== NULL
)
3600 t
->vn_next
= (sizeof (Elf_External_Verneed
)
3601 + caux
* sizeof (Elf_External_Vernaux
));
3603 _bfd_elf_swap_verneed_out (output_bfd
, t
,
3604 (Elf_External_Verneed
*) p
);
3605 p
+= sizeof (Elf_External_Verneed
);
3607 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3609 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
3610 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3611 a
->vna_nodename
, false);
3612 if (indx
== (bfd_size_type
) -1)
3615 if (a
->vna_nextptr
== NULL
)
3618 a
->vna_next
= sizeof (Elf_External_Vernaux
);
3620 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
3621 (Elf_External_Vernaux
*) p
);
3622 p
+= sizeof (Elf_External_Vernaux
);
3626 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEED
,
3628 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEEDNUM
,
3632 elf_tdata (output_bfd
)->cverrefs
= crefs
;
3636 /* Assign dynsym indicies. In a shared library we generate a
3637 section symbol for each output section, which come first.
3638 Next come all of the back-end allocated local dynamic syms,
3639 followed by the rest of the global symbols. */
3641 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3643 /* Work out the size of the symbol version section. */
3644 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3645 BFD_ASSERT (s
!= NULL
);
3646 if (dynsymcount
== 0
3647 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
3649 _bfd_strip_section_from_output (info
, s
);
3650 /* The DYNSYMCOUNT might have changed if we were going to
3651 output a dynamic symbol table entry for S. */
3652 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3656 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
3657 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3658 if (s
->contents
== NULL
)
3661 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERSYM
, (bfd_vma
) 0))
3665 /* Set the size of the .dynsym and .hash sections. We counted
3666 the number of dynamic symbols in elf_link_add_object_symbols.
3667 We will build the contents of .dynsym and .hash when we build
3668 the final symbol table, because until then we do not know the
3669 correct value to give the symbols. We built the .dynstr
3670 section as we went along in elf_link_add_object_symbols. */
3671 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
3672 BFD_ASSERT (s
!= NULL
);
3673 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
3674 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3675 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3678 if (dynsymcount
!= 0)
3680 Elf_Internal_Sym isym
;
3682 /* The first entry in .dynsym is a dummy symbol. */
3689 elf_swap_symbol_out (output_bfd
, &isym
, (PTR
) s
->contents
, (PTR
) 0);
3692 /* Compute the size of the hashing table. As a side effect this
3693 computes the hash values for all the names we export. */
3694 bucketcount
= compute_bucket_count (info
);
3696 s
= bfd_get_section_by_name (dynobj
, ".hash");
3697 BFD_ASSERT (s
!= NULL
);
3698 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
3699 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
3700 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3701 if (s
->contents
== NULL
)
3704 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) bucketcount
,
3706 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) dynsymcount
,
3707 s
->contents
+ hash_entry_size
);
3709 elf_hash_table (info
)->bucketcount
= bucketcount
;
3711 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
3712 BFD_ASSERT (s
!= NULL
);
3714 elf_finalize_dynstr (output_bfd
, info
);
3716 s
->_raw_size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3718 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
3719 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NULL
, (bfd_vma
) 0))
3726 /* This function is used to adjust offsets into .dynstr for
3727 dynamic symbols. This is called via elf_link_hash_traverse. */
3729 static boolean elf_adjust_dynstr_offsets
3730 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3733 elf_adjust_dynstr_offsets (h
, data
)
3734 struct elf_link_hash_entry
*h
;
3737 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3739 if (h
->root
.type
== bfd_link_hash_warning
)
3740 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3742 if (h
->dynindx
!= -1)
3743 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3747 /* Assign string offsets in .dynstr, update all structures referencing
3751 elf_finalize_dynstr (output_bfd
, info
)
3753 struct bfd_link_info
*info
;
3755 struct elf_link_local_dynamic_entry
*entry
;
3756 struct elf_strtab_hash
*dynstr
= elf_hash_table (info
)->dynstr
;
3757 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
3760 Elf_External_Dyn
*dyncon
, *dynconend
;
3762 _bfd_elf_strtab_finalize (dynstr
);
3763 size
= _bfd_elf_strtab_size (dynstr
);
3765 /* Update all .dynamic entries referencing .dynstr strings. */
3766 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3767 BFD_ASSERT (sdyn
!= NULL
);
3769 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
3770 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
3772 for (; dyncon
< dynconend
; dyncon
++)
3774 Elf_Internal_Dyn dyn
;
3776 elf_swap_dyn_in (dynobj
, dyncon
, & dyn
);
3780 dyn
.d_un
.d_val
= size
;
3781 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3789 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3790 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3797 /* Now update local dynamic symbols. */
3798 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
3799 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3800 entry
->isym
.st_name
);
3802 /* And the rest of dynamic symbols. */
3803 elf_link_hash_traverse (elf_hash_table (info
),
3804 elf_adjust_dynstr_offsets
, dynstr
);
3806 /* Adjust version definitions. */
3807 if (elf_tdata (output_bfd
)->cverdefs
)
3812 Elf_Internal_Verdef def
;
3813 Elf_Internal_Verdaux defaux
;
3815 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3816 p
= (bfd_byte
*) s
->contents
;
3819 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3821 p
+= sizeof (Elf_External_Verdef
);
3822 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3824 _bfd_elf_swap_verdaux_in (output_bfd
,
3825 (Elf_External_Verdaux
*) p
, &defaux
);
3826 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3828 _bfd_elf_swap_verdaux_out (output_bfd
,
3829 &defaux
, (Elf_External_Verdaux
*) p
);
3830 p
+= sizeof (Elf_External_Verdaux
);
3833 while (def
.vd_next
);
3836 /* Adjust version references. */
3837 if (elf_tdata (output_bfd
)->verref
)
3842 Elf_Internal_Verneed need
;
3843 Elf_Internal_Vernaux needaux
;
3845 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3846 p
= (bfd_byte
*) s
->contents
;
3849 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3851 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3852 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3853 (Elf_External_Verneed
*) p
);
3854 p
+= sizeof (Elf_External_Verneed
);
3855 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3857 _bfd_elf_swap_vernaux_in (output_bfd
,
3858 (Elf_External_Vernaux
*) p
, &needaux
);
3859 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3861 _bfd_elf_swap_vernaux_out (output_bfd
,
3863 (Elf_External_Vernaux
*) p
);
3864 p
+= sizeof (Elf_External_Vernaux
);
3867 while (need
.vn_next
);
3873 /* Fix up the flags for a symbol. This handles various cases which
3874 can only be fixed after all the input files are seen. This is
3875 currently called by both adjust_dynamic_symbol and
3876 assign_sym_version, which is unnecessary but perhaps more robust in
3877 the face of future changes. */
3880 elf_fix_symbol_flags (h
, eif
)
3881 struct elf_link_hash_entry
*h
;
3882 struct elf_info_failed
*eif
;
3884 /* If this symbol was mentioned in a non-ELF file, try to set
3885 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3886 permit a non-ELF file to correctly refer to a symbol defined in
3887 an ELF dynamic object. */
3888 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
3890 while (h
->root
.type
== bfd_link_hash_indirect
)
3891 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3893 if (h
->root
.type
!= bfd_link_hash_defined
3894 && h
->root
.type
!= bfd_link_hash_defweak
)
3895 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3896 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3899 if (h
->root
.u
.def
.section
->owner
!= NULL
3900 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3901 == bfd_target_elf_flavour
))
3902 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3903 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3905 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3908 if (h
->dynindx
== -1
3909 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
3910 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
3912 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3921 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3922 was first seen in a non-ELF file. Fortunately, if the symbol
3923 was first seen in an ELF file, we're probably OK unless the
3924 symbol was defined in a non-ELF file. Catch that case here.
3925 FIXME: We're still in trouble if the symbol was first seen in
3926 a dynamic object, and then later in a non-ELF regular object. */
3927 if ((h
->root
.type
== bfd_link_hash_defined
3928 || h
->root
.type
== bfd_link_hash_defweak
)
3929 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3930 && (h
->root
.u
.def
.section
->owner
!= NULL
3931 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3932 != bfd_target_elf_flavour
)
3933 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3934 && (h
->elf_link_hash_flags
3935 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
3936 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3939 /* If this is a final link, and the symbol was defined as a common
3940 symbol in a regular object file, and there was no definition in
3941 any dynamic object, then the linker will have allocated space for
3942 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3943 flag will not have been set. */
3944 if (h
->root
.type
== bfd_link_hash_defined
3945 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3946 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
3947 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3948 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3949 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3951 /* If -Bsymbolic was used (which means to bind references to global
3952 symbols to the definition within the shared object), and this
3953 symbol was defined in a regular object, then it actually doesn't
3954 need a PLT entry, and we can accomplish that by forcing it local.
3955 Likewise, if the symbol has hidden or internal visibility.
3956 FIXME: It might be that we also do not need a PLT for other
3957 non-hidden visibilities, but we would have to tell that to the
3958 backend specifically; we can't just clear PLT-related data here. */
3959 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
3960 && eif
->info
->shared
3961 && is_elf_hash_table (eif
->info
)
3962 && (eif
->info
->symbolic
3963 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3964 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3965 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3967 struct elf_backend_data
*bed
;
3968 boolean force_local
;
3970 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3972 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3973 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
3974 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
3977 /* If this is a weak defined symbol in a dynamic object, and we know
3978 the real definition in the dynamic object, copy interesting flags
3979 over to the real definition. */
3980 if (h
->weakdef
!= NULL
)
3982 struct elf_link_hash_entry
*weakdef
;
3984 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3985 || h
->root
.type
== bfd_link_hash_defweak
);
3986 weakdef
= h
->weakdef
;
3987 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
3988 || weakdef
->root
.type
== bfd_link_hash_defweak
);
3989 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
3991 /* If the real definition is defined by a regular object file,
3992 don't do anything special. See the longer description in
3993 elf_adjust_dynamic_symbol, below. */
3994 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3998 struct elf_backend_data
*bed
;
4000 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
4001 (*bed
->elf_backend_copy_indirect_symbol
) (weakdef
, h
);
4008 /* Make the backend pick a good value for a dynamic symbol. This is
4009 called via elf_link_hash_traverse, and also calls itself
4013 elf_adjust_dynamic_symbol (h
, data
)
4014 struct elf_link_hash_entry
*h
;
4017 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
4019 struct elf_backend_data
*bed
;
4021 if (h
->root
.type
== bfd_link_hash_warning
)
4023 h
->plt
.offset
= (bfd_vma
) -1;
4024 h
->got
.offset
= (bfd_vma
) -1;
4026 /* When warning symbols are created, they **replace** the "real"
4027 entry in the hash table, thus we never get to see the real
4028 symbol in a hash traversal. So look at it now. */
4029 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4032 /* Ignore indirect symbols. These are added by the versioning code. */
4033 if (h
->root
.type
== bfd_link_hash_indirect
)
4036 if (! is_elf_hash_table (eif
->info
))
4039 /* Fix the symbol flags. */
4040 if (! elf_fix_symbol_flags (h
, eif
))
4043 /* If this symbol does not require a PLT entry, and it is not
4044 defined by a dynamic object, or is not referenced by a regular
4045 object, ignore it. We do have to handle a weak defined symbol,
4046 even if no regular object refers to it, if we decided to add it
4047 to the dynamic symbol table. FIXME: Do we normally need to worry
4048 about symbols which are defined by one dynamic object and
4049 referenced by another one? */
4050 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
4051 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4052 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4053 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
4054 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
4056 h
->plt
.offset
= (bfd_vma
) -1;
4060 /* If we've already adjusted this symbol, don't do it again. This
4061 can happen via a recursive call. */
4062 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
4065 /* Don't look at this symbol again. Note that we must set this
4066 after checking the above conditions, because we may look at a
4067 symbol once, decide not to do anything, and then get called
4068 recursively later after REF_REGULAR is set below. */
4069 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
4071 /* If this is a weak definition, and we know a real definition, and
4072 the real symbol is not itself defined by a regular object file,
4073 then get a good value for the real definition. We handle the
4074 real symbol first, for the convenience of the backend routine.
4076 Note that there is a confusing case here. If the real definition
4077 is defined by a regular object file, we don't get the real symbol
4078 from the dynamic object, but we do get the weak symbol. If the
4079 processor backend uses a COPY reloc, then if some routine in the
4080 dynamic object changes the real symbol, we will not see that
4081 change in the corresponding weak symbol. This is the way other
4082 ELF linkers work as well, and seems to be a result of the shared
4085 I will clarify this issue. Most SVR4 shared libraries define the
4086 variable _timezone and define timezone as a weak synonym. The
4087 tzset call changes _timezone. If you write
4088 extern int timezone;
4090 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4091 you might expect that, since timezone is a synonym for _timezone,
4092 the same number will print both times. However, if the processor
4093 backend uses a COPY reloc, then actually timezone will be copied
4094 into your process image, and, since you define _timezone
4095 yourself, _timezone will not. Thus timezone and _timezone will
4096 wind up at different memory locations. The tzset call will set
4097 _timezone, leaving timezone unchanged. */
4099 if (h
->weakdef
!= NULL
)
4101 /* If we get to this point, we know there is an implicit
4102 reference by a regular object file via the weak symbol H.
4103 FIXME: Is this really true? What if the traversal finds
4104 H->WEAKDEF before it finds H? */
4105 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
4107 if (! elf_adjust_dynamic_symbol (h
->weakdef
, (PTR
) eif
))
4111 /* If a symbol has no type and no size and does not require a PLT
4112 entry, then we are probably about to do the wrong thing here: we
4113 are probably going to create a COPY reloc for an empty object.
4114 This case can arise when a shared object is built with assembly
4115 code, and the assembly code fails to set the symbol type. */
4117 && h
->type
== STT_NOTYPE
4118 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
4119 (*_bfd_error_handler
)
4120 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4121 h
->root
.root
.string
);
4123 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
4124 bed
= get_elf_backend_data (dynobj
);
4125 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
4134 /* This routine is used to export all defined symbols into the dynamic
4135 symbol table. It is called via elf_link_hash_traverse. */
4138 elf_export_symbol (h
, data
)
4139 struct elf_link_hash_entry
*h
;
4142 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
4144 /* Ignore indirect symbols. These are added by the versioning code. */
4145 if (h
->root
.type
== bfd_link_hash_indirect
)
4148 if (h
->root
.type
== bfd_link_hash_warning
)
4149 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4151 if (h
->dynindx
== -1
4152 && (h
->elf_link_hash_flags
4153 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
4155 struct bfd_elf_version_tree
*t
;
4156 struct bfd_elf_version_expr
*d
;
4158 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
4160 if (t
->globals
!= NULL
)
4162 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4164 if ((*d
->match
) (d
, h
->root
.root
.string
))
4169 if (t
->locals
!= NULL
)
4171 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4173 if ((*d
->match
) (d
, h
->root
.root
.string
))
4182 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
4193 /* Look through the symbols which are defined in other shared
4194 libraries and referenced here. Update the list of version
4195 dependencies. This will be put into the .gnu.version_r section.
4196 This function is called via elf_link_hash_traverse. */
4199 elf_link_find_version_dependencies (h
, data
)
4200 struct elf_link_hash_entry
*h
;
4203 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
4204 Elf_Internal_Verneed
*t
;
4205 Elf_Internal_Vernaux
*a
;
4208 if (h
->root
.type
== bfd_link_hash_warning
)
4209 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4211 /* We only care about symbols defined in shared objects with version
4213 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4214 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4216 || h
->verinfo
.verdef
== NULL
)
4219 /* See if we already know about this version. */
4220 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
4222 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
4225 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4226 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
4232 /* This is a new version. Add it to tree we are building. */
4237 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4240 rinfo
->failed
= true;
4244 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
4245 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
4246 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
4250 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4252 /* Note that we are copying a string pointer here, and testing it
4253 above. If bfd_elf_string_from_elf_section is ever changed to
4254 discard the string data when low in memory, this will have to be
4256 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
4258 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
4259 a
->vna_nextptr
= t
->vn_auxptr
;
4261 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
4264 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4271 /* Figure out appropriate versions for all the symbols. We may not
4272 have the version number script until we have read all of the input
4273 files, so until that point we don't know which symbols should be
4274 local. This function is called via elf_link_hash_traverse. */
4277 elf_link_assign_sym_version (h
, data
)
4278 struct elf_link_hash_entry
*h
;
4281 struct elf_assign_sym_version_info
*sinfo
;
4282 struct bfd_link_info
*info
;
4283 struct elf_backend_data
*bed
;
4284 struct elf_info_failed eif
;
4288 sinfo
= (struct elf_assign_sym_version_info
*) data
;
4291 if (h
->root
.type
== bfd_link_hash_warning
)
4292 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4294 /* Fix the symbol flags. */
4297 if (! elf_fix_symbol_flags (h
, &eif
))
4300 sinfo
->failed
= true;
4304 /* We only need version numbers for symbols defined in regular
4306 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4309 bed
= get_elf_backend_data (sinfo
->output_bfd
);
4310 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4311 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
4313 struct bfd_elf_version_tree
*t
;
4318 /* There are two consecutive ELF_VER_CHR characters if this is
4319 not a hidden symbol. */
4321 if (*p
== ELF_VER_CHR
)
4327 /* If there is no version string, we can just return out. */
4331 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4335 /* Look for the version. If we find it, it is no longer weak. */
4336 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4338 if (strcmp (t
->name
, p
) == 0)
4342 struct bfd_elf_version_expr
*d
;
4344 len
= p
- h
->root
.root
.string
;
4345 alc
= bfd_malloc ((bfd_size_type
) len
);
4348 memcpy (alc
, h
->root
.root
.string
, len
- 1);
4349 alc
[len
- 1] = '\0';
4350 if (alc
[len
- 2] == ELF_VER_CHR
)
4351 alc
[len
- 2] = '\0';
4353 h
->verinfo
.vertree
= t
;
4357 if (t
->globals
!= NULL
)
4359 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4360 if ((*d
->match
) (d
, alc
))
4364 /* See if there is anything to force this symbol to
4366 if (d
== NULL
&& t
->locals
!= NULL
)
4368 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4370 if ((*d
->match
) (d
, alc
))
4372 if (h
->dynindx
!= -1
4374 && ! info
->export_dynamic
)
4376 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
4389 /* If we are building an application, we need to create a
4390 version node for this version. */
4391 if (t
== NULL
&& ! info
->shared
)
4393 struct bfd_elf_version_tree
**pp
;
4396 /* If we aren't going to export this symbol, we don't need
4397 to worry about it. */
4398 if (h
->dynindx
== -1)
4402 t
= ((struct bfd_elf_version_tree
*)
4403 bfd_alloc (sinfo
->output_bfd
, amt
));
4406 sinfo
->failed
= true;
4415 t
->name_indx
= (unsigned int) -1;
4419 /* Don't count anonymous version tag. */
4420 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
4422 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4424 t
->vernum
= version_index
;
4428 h
->verinfo
.vertree
= t
;
4432 /* We could not find the version for a symbol when
4433 generating a shared archive. Return an error. */
4434 (*_bfd_error_handler
)
4435 (_("%s: undefined versioned symbol name %s"),
4436 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
4437 bfd_set_error (bfd_error_bad_value
);
4438 sinfo
->failed
= true;
4443 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4446 /* If we don't have a version for this symbol, see if we can find
4448 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
4450 struct bfd_elf_version_tree
*t
;
4451 struct bfd_elf_version_tree
*deflt
;
4452 struct bfd_elf_version_expr
*d
;
4454 /* See if can find what version this symbol is in. If the
4455 symbol is supposed to be local, then don't actually register
4458 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4460 if (t
->globals
!= NULL
)
4462 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4464 if ((*d
->match
) (d
, h
->root
.root
.string
))
4466 h
->verinfo
.vertree
= t
;
4475 if (t
->locals
!= NULL
)
4477 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4479 if (d
->pattern
[0] == '*' && d
->pattern
[1] == '\0')
4481 else if ((*d
->match
) (d
, h
->root
.root
.string
))
4483 h
->verinfo
.vertree
= t
;
4484 if (h
->dynindx
!= -1
4486 && ! info
->export_dynamic
)
4488 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
4499 if (deflt
!= NULL
&& h
->verinfo
.vertree
== NULL
)
4501 h
->verinfo
.vertree
= deflt
;
4502 if (h
->dynindx
!= -1
4504 && ! info
->export_dynamic
)
4506 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
4514 /* Final phase of ELF linker. */
4516 /* A structure we use to avoid passing large numbers of arguments. */
4518 struct elf_final_link_info
4520 /* General link information. */
4521 struct bfd_link_info
*info
;
4524 /* Symbol string table. */
4525 struct bfd_strtab_hash
*symstrtab
;
4526 /* .dynsym section. */
4527 asection
*dynsym_sec
;
4528 /* .hash section. */
4530 /* symbol version section (.gnu.version). */
4531 asection
*symver_sec
;
4532 /* first SHF_TLS section (if any). */
4533 asection
*first_tls_sec
;
4534 /* Buffer large enough to hold contents of any section. */
4536 /* Buffer large enough to hold external relocs of any section. */
4537 PTR external_relocs
;
4538 /* Buffer large enough to hold internal relocs of any section. */
4539 Elf_Internal_Rela
*internal_relocs
;
4540 /* Buffer large enough to hold external local symbols of any input
4542 Elf_External_Sym
*external_syms
;
4543 /* And a buffer for symbol section indices. */
4544 Elf_External_Sym_Shndx
*locsym_shndx
;
4545 /* Buffer large enough to hold internal local symbols of any input
4547 Elf_Internal_Sym
*internal_syms
;
4548 /* Array large enough to hold a symbol index for each local symbol
4549 of any input BFD. */
4551 /* Array large enough to hold a section pointer for each local
4552 symbol of any input BFD. */
4553 asection
**sections
;
4554 /* Buffer to hold swapped out symbols. */
4555 Elf_External_Sym
*symbuf
;
4556 /* And one for symbol section indices. */
4557 Elf_External_Sym_Shndx
*symshndxbuf
;
4558 /* Number of swapped out symbols in buffer. */
4559 size_t symbuf_count
;
4560 /* Number of symbols which fit in symbuf. */
4564 static boolean elf_link_output_sym
4565 PARAMS ((struct elf_final_link_info
*, const char *,
4566 Elf_Internal_Sym
*, asection
*));
4567 static boolean elf_link_flush_output_syms
4568 PARAMS ((struct elf_final_link_info
*));
4569 static boolean elf_link_output_extsym
4570 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4571 static boolean elf_link_sec_merge_syms
4572 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4573 static boolean elf_link_check_versioned_symbol
4574 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
4575 static boolean elf_link_input_bfd
4576 PARAMS ((struct elf_final_link_info
*, bfd
*));
4577 static boolean elf_reloc_link_order
4578 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
4579 struct bfd_link_order
*));
4581 /* This struct is used to pass information to elf_link_output_extsym. */
4583 struct elf_outext_info
4587 struct elf_final_link_info
*finfo
;
4590 /* Compute the size of, and allocate space for, REL_HDR which is the
4591 section header for a section containing relocations for O. */
4594 elf_link_size_reloc_section (abfd
, rel_hdr
, o
)
4596 Elf_Internal_Shdr
*rel_hdr
;
4599 bfd_size_type reloc_count
;
4600 bfd_size_type num_rel_hashes
;
4602 /* Figure out how many relocations there will be. */
4603 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
4604 reloc_count
= elf_section_data (o
)->rel_count
;
4606 reloc_count
= elf_section_data (o
)->rel_count2
;
4608 num_rel_hashes
= o
->reloc_count
;
4609 if (num_rel_hashes
< reloc_count
)
4610 num_rel_hashes
= reloc_count
;
4612 /* That allows us to calculate the size of the section. */
4613 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
4615 /* The contents field must last into write_object_contents, so we
4616 allocate it with bfd_alloc rather than malloc. Also since we
4617 cannot be sure that the contents will actually be filled in,
4618 we zero the allocated space. */
4619 rel_hdr
->contents
= (PTR
) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
4620 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
4623 /* We only allocate one set of hash entries, so we only do it the
4624 first time we are called. */
4625 if (elf_section_data (o
)->rel_hashes
== NULL
4628 struct elf_link_hash_entry
**p
;
4630 p
= ((struct elf_link_hash_entry
**)
4631 bfd_zmalloc (num_rel_hashes
4632 * sizeof (struct elf_link_hash_entry
*)));
4636 elf_section_data (o
)->rel_hashes
= p
;
4642 /* When performing a relocateable link, the input relocations are
4643 preserved. But, if they reference global symbols, the indices
4644 referenced must be updated. Update all the relocations in
4645 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4648 elf_link_adjust_relocs (abfd
, rel_hdr
, count
, rel_hash
)
4650 Elf_Internal_Shdr
*rel_hdr
;
4652 struct elf_link_hash_entry
**rel_hash
;
4655 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4656 Elf_Internal_Rel
*irel
;
4657 Elf_Internal_Rela
*irela
;
4658 bfd_size_type amt
= sizeof (Elf_Internal_Rel
) * bed
->s
->int_rels_per_ext_rel
;
4660 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (amt
);
4663 (*_bfd_error_handler
) (_("Error: out of memory"));
4667 amt
= sizeof (Elf_Internal_Rela
) * bed
->s
->int_rels_per_ext_rel
;
4668 irela
= (Elf_Internal_Rela
*) bfd_zmalloc (amt
);
4671 (*_bfd_error_handler
) (_("Error: out of memory"));
4675 for (i
= 0; i
< count
; i
++, rel_hash
++)
4677 if (*rel_hash
== NULL
)
4680 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
4682 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4684 Elf_External_Rel
*erel
;
4687 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
4688 if (bed
->s
->swap_reloc_in
)
4689 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
4691 elf_swap_reloc_in (abfd
, erel
, irel
);
4693 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4694 irel
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4695 ELF_R_TYPE (irel
[j
].r_info
));
4697 if (bed
->s
->swap_reloc_out
)
4698 (*bed
->s
->swap_reloc_out
) (abfd
, irel
, (bfd_byte
*) erel
);
4700 elf_swap_reloc_out (abfd
, irel
, erel
);
4704 Elf_External_Rela
*erela
;
4707 BFD_ASSERT (rel_hdr
->sh_entsize
4708 == sizeof (Elf_External_Rela
));
4710 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
4711 if (bed
->s
->swap_reloca_in
)
4712 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
4714 elf_swap_reloca_in (abfd
, erela
, irela
);
4716 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4717 irela
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4718 ELF_R_TYPE (irela
[j
].r_info
));
4720 if (bed
->s
->swap_reloca_out
)
4721 (*bed
->s
->swap_reloca_out
) (abfd
, irela
, (bfd_byte
*) erela
);
4723 elf_swap_reloca_out (abfd
, irela
, erela
);
4731 struct elf_link_sort_rela
{
4733 enum elf_reloc_type_class type
;
4735 Elf_Internal_Rel rel
;
4736 Elf_Internal_Rela rela
;
4741 elf_link_sort_cmp1 (A
, B
)
4745 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4746 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4747 int relativea
, relativeb
;
4749 relativea
= a
->type
== reloc_class_relative
;
4750 relativeb
= b
->type
== reloc_class_relative
;
4752 if (relativea
< relativeb
)
4754 if (relativea
> relativeb
)
4756 if (ELF_R_SYM (a
->u
.rel
.r_info
) < ELF_R_SYM (b
->u
.rel
.r_info
))
4758 if (ELF_R_SYM (a
->u
.rel
.r_info
) > ELF_R_SYM (b
->u
.rel
.r_info
))
4760 if (a
->u
.rel
.r_offset
< b
->u
.rel
.r_offset
)
4762 if (a
->u
.rel
.r_offset
> b
->u
.rel
.r_offset
)
4768 elf_link_sort_cmp2 (A
, B
)
4772 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4773 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4776 if (a
->offset
< b
->offset
)
4778 if (a
->offset
> b
->offset
)
4780 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
4781 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
4786 if (a
->u
.rel
.r_offset
< b
->u
.rel
.r_offset
)
4788 if (a
->u
.rel
.r_offset
> b
->u
.rel
.r_offset
)
4794 elf_link_sort_relocs (abfd
, info
, psec
)
4796 struct bfd_link_info
*info
;
4799 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4800 asection
*reldyn
, *o
;
4801 boolean rel
= false;
4802 bfd_size_type count
, size
;
4804 struct elf_link_sort_rela
*rela
;
4805 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4807 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
4808 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4810 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
4811 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4814 count
= reldyn
->_raw_size
/ sizeof (Elf_External_Rel
);
4817 count
= reldyn
->_raw_size
/ sizeof (Elf_External_Rela
);
4820 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4821 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4822 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4823 && o
->output_section
== reldyn
)
4824 size
+= o
->_raw_size
;
4826 if (size
!= reldyn
->_raw_size
)
4829 rela
= (struct elf_link_sort_rela
*) bfd_zmalloc (sizeof (*rela
) * count
);
4832 (*info
->callbacks
->warning
)
4833 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0,
4838 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4839 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4840 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4841 && o
->output_section
== reldyn
)
4845 Elf_External_Rel
*erel
, *erelend
;
4846 struct elf_link_sort_rela
*s
;
4848 erel
= (Elf_External_Rel
*) o
->contents
;
4849 erelend
= (Elf_External_Rel
*) (o
->contents
+ o
->_raw_size
);
4850 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rel
);
4851 for (; erel
< erelend
; erel
++, s
++)
4853 if (bed
->s
->swap_reloc_in
)
4854 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, &s
->u
.rel
);
4856 elf_swap_reloc_in (abfd
, erel
, &s
->u
.rel
);
4858 s
->type
= (*bed
->elf_backend_reloc_type_class
) (&s
->u
.rela
);
4863 Elf_External_Rela
*erela
, *erelaend
;
4864 struct elf_link_sort_rela
*s
;
4866 erela
= (Elf_External_Rela
*) o
->contents
;
4867 erelaend
= (Elf_External_Rela
*) (o
->contents
+ o
->_raw_size
);
4868 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rela
);
4869 for (; erela
< erelaend
; erela
++, s
++)
4871 if (bed
->s
->swap_reloca_in
)
4872 (*bed
->s
->swap_reloca_in
) (dynobj
, (bfd_byte
*) erela
,
4875 elf_swap_reloca_in (dynobj
, erela
, &s
->u
.rela
);
4877 s
->type
= (*bed
->elf_backend_reloc_type_class
) (&s
->u
.rela
);
4882 qsort (rela
, (size_t) count
, sizeof (*rela
), elf_link_sort_cmp1
);
4883 for (ret
= 0; ret
< count
&& rela
[ret
].type
== reloc_class_relative
; ret
++)
4885 for (i
= ret
, j
= ret
; i
< count
; i
++)
4887 if (ELF_R_SYM (rela
[i
].u
.rel
.r_info
) != ELF_R_SYM (rela
[j
].u
.rel
.r_info
))
4889 rela
[i
].offset
= rela
[j
].u
.rel
.r_offset
;
4891 qsort (rela
+ ret
, (size_t) count
- ret
, sizeof (*rela
), elf_link_sort_cmp2
);
4893 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4894 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4895 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4896 && o
->output_section
== reldyn
)
4900 Elf_External_Rel
*erel
, *erelend
;
4901 struct elf_link_sort_rela
*s
;
4903 erel
= (Elf_External_Rel
*) o
->contents
;
4904 erelend
= (Elf_External_Rel
*) (o
->contents
+ o
->_raw_size
);
4905 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rel
);
4906 for (; erel
< erelend
; erel
++, s
++)
4908 if (bed
->s
->swap_reloc_out
)
4909 (*bed
->s
->swap_reloc_out
) (abfd
, &s
->u
.rel
,
4912 elf_swap_reloc_out (abfd
, &s
->u
.rel
, erel
);
4917 Elf_External_Rela
*erela
, *erelaend
;
4918 struct elf_link_sort_rela
*s
;
4920 erela
= (Elf_External_Rela
*) o
->contents
;
4921 erelaend
= (Elf_External_Rela
*) (o
->contents
+ o
->_raw_size
);
4922 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rela
);
4923 for (; erela
< erelaend
; erela
++, s
++)
4925 if (bed
->s
->swap_reloca_out
)
4926 (*bed
->s
->swap_reloca_out
) (dynobj
, &s
->u
.rela
,
4927 (bfd_byte
*) erela
);
4929 elf_swap_reloca_out (dynobj
, &s
->u
.rela
, erela
);
4939 /* Do the final step of an ELF link. */
4942 elf_bfd_final_link (abfd
, info
)
4944 struct bfd_link_info
*info
;
4947 boolean emit_relocs
;
4949 struct elf_final_link_info finfo
;
4950 register asection
*o
;
4951 register struct bfd_link_order
*p
;
4953 bfd_size_type max_contents_size
;
4954 bfd_size_type max_external_reloc_size
;
4955 bfd_size_type max_internal_reloc_count
;
4956 bfd_size_type max_sym_count
;
4957 bfd_size_type max_sym_shndx_count
;
4959 Elf_Internal_Sym elfsym
;
4961 Elf_Internal_Shdr
*symtab_hdr
;
4962 Elf_Internal_Shdr
*symstrtab_hdr
;
4963 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4964 struct elf_outext_info eoinfo
;
4966 size_t relativecount
= 0;
4967 asection
*reldyn
= 0;
4970 if (! is_elf_hash_table (info
))
4974 abfd
->flags
|= DYNAMIC
;
4976 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
4977 dynobj
= elf_hash_table (info
)->dynobj
;
4979 emit_relocs
= (info
->relocateable
4980 || info
->emitrelocations
4981 || bed
->elf_backend_emit_relocs
);
4984 finfo
.output_bfd
= abfd
;
4985 finfo
.symstrtab
= elf_stringtab_init ();
4986 if (finfo
.symstrtab
== NULL
)
4991 finfo
.dynsym_sec
= NULL
;
4992 finfo
.hash_sec
= NULL
;
4993 finfo
.symver_sec
= NULL
;
4997 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
4998 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
4999 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
5000 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5001 /* Note that it is OK if symver_sec is NULL. */
5004 finfo
.contents
= NULL
;
5005 finfo
.external_relocs
= NULL
;
5006 finfo
.internal_relocs
= NULL
;
5007 finfo
.external_syms
= NULL
;
5008 finfo
.locsym_shndx
= NULL
;
5009 finfo
.internal_syms
= NULL
;
5010 finfo
.indices
= NULL
;
5011 finfo
.sections
= NULL
;
5012 finfo
.symbuf
= NULL
;
5013 finfo
.symshndxbuf
= NULL
;
5014 finfo
.symbuf_count
= 0;
5015 finfo
.first_tls_sec
= NULL
;
5016 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
5017 if ((o
->flags
& SEC_THREAD_LOCAL
) != 0
5018 && (o
->flags
& SEC_LOAD
) != 0)
5020 finfo
.first_tls_sec
= o
;
5024 /* Count up the number of relocations we will output for each output
5025 section, so that we know the sizes of the reloc sections. We
5026 also figure out some maximum sizes. */
5027 max_contents_size
= 0;
5028 max_external_reloc_size
= 0;
5029 max_internal_reloc_count
= 0;
5031 max_sym_shndx_count
= 0;
5033 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
5037 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5039 if (p
->type
== bfd_section_reloc_link_order
5040 || p
->type
== bfd_symbol_reloc_link_order
)
5042 else if (p
->type
== bfd_indirect_link_order
)
5046 sec
= p
->u
.indirect
.section
;
5048 /* Mark all sections which are to be included in the
5049 link. This will normally be every section. We need
5050 to do this so that we can identify any sections which
5051 the linker has decided to not include. */
5052 sec
->linker_mark
= true;
5054 if (sec
->flags
& SEC_MERGE
)
5057 if (info
->relocateable
|| info
->emitrelocations
)
5058 o
->reloc_count
+= sec
->reloc_count
;
5059 else if (bed
->elf_backend_count_relocs
)
5061 Elf_Internal_Rela
* relocs
;
5063 relocs
= (NAME(_bfd_elf
,link_read_relocs
)
5064 (abfd
, sec
, (PTR
) NULL
,
5065 (Elf_Internal_Rela
*) NULL
, info
->keep_memory
));
5068 += (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
5070 if (!info
->keep_memory
)
5074 if (sec
->_raw_size
> max_contents_size
)
5075 max_contents_size
= sec
->_raw_size
;
5076 if (sec
->_cooked_size
> max_contents_size
)
5077 max_contents_size
= sec
->_cooked_size
;
5079 /* We are interested in just local symbols, not all
5081 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
5082 && (sec
->owner
->flags
& DYNAMIC
) == 0)
5086 if (elf_bad_symtab (sec
->owner
))
5087 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
5088 / sizeof (Elf_External_Sym
));
5090 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
5092 if (sym_count
> max_sym_count
)
5093 max_sym_count
= sym_count
;
5095 if (sym_count
> max_sym_shndx_count
5096 && elf_symtab_shndx (sec
->owner
) != 0)
5097 max_sym_shndx_count
= sym_count
;
5099 if ((sec
->flags
& SEC_RELOC
) != 0)
5103 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
5104 if (ext_size
> max_external_reloc_size
)
5105 max_external_reloc_size
= ext_size
;
5106 if (sec
->reloc_count
> max_internal_reloc_count
)
5107 max_internal_reloc_count
= sec
->reloc_count
;
5113 if (o
->reloc_count
> 0)
5114 o
->flags
|= SEC_RELOC
;
5117 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5118 set it (this is probably a bug) and if it is set
5119 assign_section_numbers will create a reloc section. */
5120 o
->flags
&=~ SEC_RELOC
;
5123 /* If the SEC_ALLOC flag is not set, force the section VMA to
5124 zero. This is done in elf_fake_sections as well, but forcing
5125 the VMA to 0 here will ensure that relocs against these
5126 sections are handled correctly. */
5127 if ((o
->flags
& SEC_ALLOC
) == 0
5128 && ! o
->user_set_vma
)
5132 if (! info
->relocateable
&& merged
)
5133 elf_link_hash_traverse (elf_hash_table (info
),
5134 elf_link_sec_merge_syms
, (PTR
) abfd
);
5136 /* Figure out the file positions for everything but the symbol table
5137 and the relocs. We set symcount to force assign_section_numbers
5138 to create a symbol table. */
5139 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
5140 BFD_ASSERT (! abfd
->output_has_begun
);
5141 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
5144 /* Figure out how many relocations we will have in each section.
5145 Just using RELOC_COUNT isn't good enough since that doesn't
5146 maintain a separate value for REL vs. RELA relocations. */
5148 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5149 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5151 asection
*output_section
;
5153 if (! o
->linker_mark
)
5155 /* This section was omitted from the link. */
5159 output_section
= o
->output_section
;
5161 if (output_section
!= NULL
5162 && (o
->flags
& SEC_RELOC
) != 0)
5164 struct bfd_elf_section_data
*esdi
5165 = elf_section_data (o
);
5166 struct bfd_elf_section_data
*esdo
5167 = elf_section_data (output_section
);
5168 unsigned int *rel_count
;
5169 unsigned int *rel_count2
;
5170 bfd_size_type entsize
;
5171 bfd_size_type entsize2
;
5173 /* We must be careful to add the relocations from the
5174 input section to the right output count. */
5175 entsize
= esdi
->rel_hdr
.sh_entsize
;
5176 entsize2
= esdi
->rel_hdr2
? esdi
->rel_hdr2
->sh_entsize
: 0;
5177 BFD_ASSERT ((entsize
== sizeof (Elf_External_Rel
)
5178 || entsize
== sizeof (Elf_External_Rela
))
5179 && entsize2
!= entsize
5181 || entsize2
== sizeof (Elf_External_Rel
)
5182 || entsize2
== sizeof (Elf_External_Rela
)));
5183 if (entsize
== esdo
->rel_hdr
.sh_entsize
)
5185 rel_count
= &esdo
->rel_count
;
5186 rel_count2
= &esdo
->rel_count2
;
5190 rel_count
= &esdo
->rel_count2
;
5191 rel_count2
= &esdo
->rel_count
;
5194 *rel_count
+= NUM_SHDR_ENTRIES (& esdi
->rel_hdr
);
5196 *rel_count2
+= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
5197 output_section
->flags
|= SEC_RELOC
;
5201 /* That created the reloc sections. Set their sizes, and assign
5202 them file positions, and allocate some buffers. */
5203 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5205 if ((o
->flags
& SEC_RELOC
) != 0)
5207 if (!elf_link_size_reloc_section (abfd
,
5208 &elf_section_data (o
)->rel_hdr
,
5212 if (elf_section_data (o
)->rel_hdr2
5213 && !elf_link_size_reloc_section (abfd
,
5214 elf_section_data (o
)->rel_hdr2
,
5219 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5220 to count upwards while actually outputting the relocations. */
5221 elf_section_data (o
)->rel_count
= 0;
5222 elf_section_data (o
)->rel_count2
= 0;
5225 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5227 /* We have now assigned file positions for all the sections except
5228 .symtab and .strtab. We start the .symtab section at the current
5229 file position, and write directly to it. We build the .strtab
5230 section in memory. */
5231 bfd_get_symcount (abfd
) = 0;
5232 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5233 /* sh_name is set in prep_headers. */
5234 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5235 symtab_hdr
->sh_flags
= 0;
5236 symtab_hdr
->sh_addr
= 0;
5237 symtab_hdr
->sh_size
= 0;
5238 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
5239 /* sh_link is set in assign_section_numbers. */
5240 /* sh_info is set below. */
5241 /* sh_offset is set just below. */
5242 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
5244 off
= elf_tdata (abfd
)->next_file_pos
;
5245 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
5247 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5248 incorrect. We do not yet know the size of the .symtab section.
5249 We correct next_file_pos below, after we do know the size. */
5251 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5252 continuously seeking to the right position in the file. */
5253 if (! info
->keep_memory
|| max_sym_count
< 20)
5254 finfo
.symbuf_size
= 20;
5256 finfo
.symbuf_size
= max_sym_count
;
5257 amt
= finfo
.symbuf_size
;
5258 amt
*= sizeof (Elf_External_Sym
);
5259 finfo
.symbuf
= (Elf_External_Sym
*) bfd_malloc (amt
);
5260 if (finfo
.symbuf
== NULL
)
5262 if (elf_numsections (abfd
) > SHN_LORESERVE
)
5264 amt
= finfo
.symbuf_size
;
5265 amt
*= sizeof (Elf_External_Sym_Shndx
);
5266 finfo
.symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
5267 if (finfo
.symshndxbuf
== NULL
)
5271 /* Start writing out the symbol table. The first symbol is always a
5273 if (info
->strip
!= strip_all
5276 elfsym
.st_value
= 0;
5279 elfsym
.st_other
= 0;
5280 elfsym
.st_shndx
= SHN_UNDEF
;
5281 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5282 &elfsym
, bfd_und_section_ptr
))
5287 /* Some standard ELF linkers do this, but we don't because it causes
5288 bootstrap comparison failures. */
5289 /* Output a file symbol for the output file as the second symbol.
5290 We output this even if we are discarding local symbols, although
5291 I'm not sure if this is correct. */
5292 elfsym
.st_value
= 0;
5294 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5295 elfsym
.st_other
= 0;
5296 elfsym
.st_shndx
= SHN_ABS
;
5297 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
5298 &elfsym
, bfd_abs_section_ptr
))
5302 /* Output a symbol for each section. We output these even if we are
5303 discarding local symbols, since they are used for relocs. These
5304 symbols have no names. We store the index of each one in the
5305 index field of the section, so that we can find it again when
5306 outputting relocs. */
5307 if (info
->strip
!= strip_all
5311 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5312 elfsym
.st_other
= 0;
5313 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5315 o
= section_from_elf_index (abfd
, i
);
5317 o
->target_index
= bfd_get_symcount (abfd
);
5318 elfsym
.st_shndx
= i
;
5319 if (info
->relocateable
|| o
== NULL
)
5320 elfsym
.st_value
= 0;
5322 elfsym
.st_value
= o
->vma
;
5323 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5326 if (i
== SHN_LORESERVE
)
5327 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
5331 /* Allocate some memory to hold information read in from the input
5333 if (max_contents_size
!= 0)
5335 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
5336 if (finfo
.contents
== NULL
)
5340 if (max_external_reloc_size
!= 0)
5342 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
5343 if (finfo
.external_relocs
== NULL
)
5347 if (max_internal_reloc_count
!= 0)
5349 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5350 amt
*= sizeof (Elf_Internal_Rela
);
5351 finfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
5352 if (finfo
.internal_relocs
== NULL
)
5356 if (max_sym_count
!= 0)
5358 amt
= max_sym_count
* sizeof (Elf_External_Sym
);
5359 finfo
.external_syms
= (Elf_External_Sym
*) bfd_malloc (amt
);
5360 if (finfo
.external_syms
== NULL
)
5363 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
5364 finfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
5365 if (finfo
.internal_syms
== NULL
)
5368 amt
= max_sym_count
* sizeof (long);
5369 finfo
.indices
= (long *) bfd_malloc (amt
);
5370 if (finfo
.indices
== NULL
)
5373 amt
= max_sym_count
* sizeof (asection
*);
5374 finfo
.sections
= (asection
**) bfd_malloc (amt
);
5375 if (finfo
.sections
== NULL
)
5379 if (max_sym_shndx_count
!= 0)
5381 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
5382 finfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
5383 if (finfo
.locsym_shndx
== NULL
)
5387 if (finfo
.first_tls_sec
)
5389 unsigned int align
= 0;
5390 bfd_vma base
= finfo
.first_tls_sec
->vma
, end
= 0;
5393 for (sec
= finfo
.first_tls_sec
;
5394 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
5397 bfd_vma size
= sec
->_raw_size
;
5399 if (bfd_get_section_alignment (abfd
, sec
) > align
)
5400 align
= bfd_get_section_alignment (abfd
, sec
);
5401 if (sec
->_raw_size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
5403 struct bfd_link_order
*o
;
5406 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
5407 if (size
< o
->offset
+ o
->size
)
5408 size
= o
->offset
+ o
->size
;
5410 end
= sec
->vma
+ size
;
5412 elf_hash_table (info
)->tls_segment
5413 = bfd_zalloc (abfd
, sizeof (struct elf_link_tls_segment
));
5414 if (elf_hash_table (info
)->tls_segment
== NULL
)
5416 elf_hash_table (info
)->tls_segment
->start
= base
;
5417 elf_hash_table (info
)->tls_segment
->size
= end
- base
;
5418 elf_hash_table (info
)->tls_segment
->align
= align
;
5421 /* Since ELF permits relocations to be against local symbols, we
5422 must have the local symbols available when we do the relocations.
5423 Since we would rather only read the local symbols once, and we
5424 would rather not keep them in memory, we handle all the
5425 relocations for a single input file at the same time.
5427 Unfortunately, there is no way to know the total number of local
5428 symbols until we have seen all of them, and the local symbol
5429 indices precede the global symbol indices. This means that when
5430 we are generating relocateable output, and we see a reloc against
5431 a global symbol, we can not know the symbol index until we have
5432 finished examining all the local symbols to see which ones we are
5433 going to output. To deal with this, we keep the relocations in
5434 memory, and don't output them until the end of the link. This is
5435 an unfortunate waste of memory, but I don't see a good way around
5436 it. Fortunately, it only happens when performing a relocateable
5437 link, which is not the common case. FIXME: If keep_memory is set
5438 we could write the relocs out and then read them again; I don't
5439 know how bad the memory loss will be. */
5441 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5442 sub
->output_has_begun
= false;
5443 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5445 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5447 if (p
->type
== bfd_indirect_link_order
5448 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
5449 == bfd_target_elf_flavour
)
5450 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
5452 if (! sub
->output_has_begun
)
5454 if (! elf_link_input_bfd (&finfo
, sub
))
5456 sub
->output_has_begun
= true;
5459 else if (p
->type
== bfd_section_reloc_link_order
5460 || p
->type
== bfd_symbol_reloc_link_order
)
5462 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
5467 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
5473 /* Output any global symbols that got converted to local in a
5474 version script or due to symbol visibility. We do this in a
5475 separate step since ELF requires all local symbols to appear
5476 prior to any global symbols. FIXME: We should only do this if
5477 some global symbols were, in fact, converted to become local.
5478 FIXME: Will this work correctly with the Irix 5 linker? */
5479 eoinfo
.failed
= false;
5480 eoinfo
.finfo
= &finfo
;
5481 eoinfo
.localsyms
= true;
5482 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5487 /* That wrote out all the local symbols. Finish up the symbol table
5488 with the global symbols. Even if we want to strip everything we
5489 can, we still need to deal with those global symbols that got
5490 converted to local in a version script. */
5492 /* The sh_info field records the index of the first non local symbol. */
5493 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
5496 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
5498 Elf_Internal_Sym sym
;
5499 Elf_External_Sym
*dynsym
=
5500 (Elf_External_Sym
*) finfo
.dynsym_sec
->contents
;
5501 long last_local
= 0;
5503 /* Write out the section symbols for the output sections. */
5510 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5513 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5516 Elf_External_Sym
*dest
;
5518 indx
= elf_section_data (s
)->this_idx
;
5519 BFD_ASSERT (indx
> 0);
5520 sym
.st_shndx
= indx
;
5521 sym
.st_value
= s
->vma
;
5522 dest
= dynsym
+ elf_section_data (s
)->dynindx
;
5523 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5526 last_local
= bfd_count_sections (abfd
);
5529 /* Write out the local dynsyms. */
5530 if (elf_hash_table (info
)->dynlocal
)
5532 struct elf_link_local_dynamic_entry
*e
;
5533 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
5536 Elf_External_Sym
*dest
;
5538 sym
.st_size
= e
->isym
.st_size
;
5539 sym
.st_other
= e
->isym
.st_other
;
5541 /* Copy the internal symbol as is.
5542 Note that we saved a word of storage and overwrote
5543 the original st_name with the dynstr_index. */
5546 if (e
->isym
.st_shndx
!= SHN_UNDEF
5547 && (e
->isym
.st_shndx
< SHN_LORESERVE
5548 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
5550 s
= bfd_section_from_elf_index (e
->input_bfd
,
5554 elf_section_data (s
->output_section
)->this_idx
;
5555 sym
.st_value
= (s
->output_section
->vma
5557 + e
->isym
.st_value
);
5560 if (last_local
< e
->dynindx
)
5561 last_local
= e
->dynindx
;
5563 dest
= dynsym
+ e
->dynindx
;
5564 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5568 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
5572 /* We get the global symbols from the hash table. */
5573 eoinfo
.failed
= false;
5574 eoinfo
.localsyms
= false;
5575 eoinfo
.finfo
= &finfo
;
5576 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5581 /* If backend needs to output some symbols not present in the hash
5582 table, do it now. */
5583 if (bed
->elf_backend_output_arch_syms
)
5585 typedef boolean (*out_sym_func
) PARAMS ((PTR
, const char *,
5589 if (! ((*bed
->elf_backend_output_arch_syms
)
5590 (abfd
, info
, (PTR
) &finfo
, (out_sym_func
) elf_link_output_sym
)))
5594 /* Flush all symbols to the file. */
5595 if (! elf_link_flush_output_syms (&finfo
))
5598 /* Now we know the size of the symtab section. */
5599 off
+= symtab_hdr
->sh_size
;
5601 /* Finish up and write out the symbol string table (.strtab)
5603 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5604 /* sh_name was set in prep_headers. */
5605 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5606 symstrtab_hdr
->sh_flags
= 0;
5607 symstrtab_hdr
->sh_addr
= 0;
5608 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
5609 symstrtab_hdr
->sh_entsize
= 0;
5610 symstrtab_hdr
->sh_link
= 0;
5611 symstrtab_hdr
->sh_info
= 0;
5612 /* sh_offset is set just below. */
5613 symstrtab_hdr
->sh_addralign
= 1;
5615 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, true);
5616 elf_tdata (abfd
)->next_file_pos
= off
;
5618 if (bfd_get_symcount (abfd
) > 0)
5620 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
5621 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
5625 /* Adjust the relocs to have the correct symbol indices. */
5626 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5628 if ((o
->flags
& SEC_RELOC
) == 0)
5631 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
5632 elf_section_data (o
)->rel_count
,
5633 elf_section_data (o
)->rel_hashes
);
5634 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
5635 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
5636 elf_section_data (o
)->rel_count2
,
5637 (elf_section_data (o
)->rel_hashes
5638 + elf_section_data (o
)->rel_count
));
5640 /* Set the reloc_count field to 0 to prevent write_relocs from
5641 trying to swap the relocs out itself. */
5645 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
5646 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
5648 /* If we are linking against a dynamic object, or generating a
5649 shared library, finish up the dynamic linking information. */
5652 Elf_External_Dyn
*dyncon
, *dynconend
;
5654 /* Fix up .dynamic entries. */
5655 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
5656 BFD_ASSERT (o
!= NULL
);
5658 dyncon
= (Elf_External_Dyn
*) o
->contents
;
5659 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
5660 for (; dyncon
< dynconend
; dyncon
++)
5662 Elf_Internal_Dyn dyn
;
5666 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
5673 if (relativecount
> 0 && dyncon
+ 1 < dynconend
)
5675 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
5677 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
5678 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
5681 if (dyn
.d_tag
!= DT_NULL
)
5683 dyn
.d_un
.d_val
= relativecount
;
5684 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5690 name
= info
->init_function
;
5693 name
= info
->fini_function
;
5696 struct elf_link_hash_entry
*h
;
5698 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
5699 false, false, true);
5701 && (h
->root
.type
== bfd_link_hash_defined
5702 || h
->root
.type
== bfd_link_hash_defweak
))
5704 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
5705 o
= h
->root
.u
.def
.section
;
5706 if (o
->output_section
!= NULL
)
5707 dyn
.d_un
.d_val
+= (o
->output_section
->vma
5708 + o
->output_offset
);
5711 /* The symbol is imported from another shared
5712 library and does not apply to this one. */
5716 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5721 case DT_PREINIT_ARRAYSZ
:
5722 name
= ".preinit_array";
5724 case DT_INIT_ARRAYSZ
:
5725 name
= ".init_array";
5727 case DT_FINI_ARRAYSZ
:
5728 name
= ".fini_array";
5730 o
= bfd_get_section_by_name (abfd
, name
);
5733 (*_bfd_error_handler
)
5734 (_("%s: could not find output section %s"),
5735 bfd_get_filename (abfd
), name
);
5738 if (o
->_raw_size
== 0)
5739 (*_bfd_error_handler
)
5740 (_("warning: %s section has zero size"), name
);
5741 dyn
.d_un
.d_val
= o
->_raw_size
;
5742 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5745 case DT_PREINIT_ARRAY
:
5746 name
= ".preinit_array";
5749 name
= ".init_array";
5752 name
= ".fini_array";
5765 name
= ".gnu.version_d";
5768 name
= ".gnu.version_r";
5771 name
= ".gnu.version";
5773 o
= bfd_get_section_by_name (abfd
, name
);
5776 (*_bfd_error_handler
)
5777 (_("%s: could not find output section %s"),
5778 bfd_get_filename (abfd
), name
);
5781 dyn
.d_un
.d_ptr
= o
->vma
;
5782 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5789 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
5794 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5796 Elf_Internal_Shdr
*hdr
;
5798 hdr
= elf_elfsections (abfd
)[i
];
5799 if (hdr
->sh_type
== type
5800 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
5802 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
5803 dyn
.d_un
.d_val
+= hdr
->sh_size
;
5806 if (dyn
.d_un
.d_val
== 0
5807 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
5808 dyn
.d_un
.d_val
= hdr
->sh_addr
;
5812 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5818 /* If we have created any dynamic sections, then output them. */
5821 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
5824 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
5826 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5827 || o
->_raw_size
== 0
5828 || o
->output_section
== bfd_abs_section_ptr
)
5830 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
5832 /* At this point, we are only interested in sections
5833 created by elf_link_create_dynamic_sections. */
5836 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
5838 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
5840 if (! bfd_set_section_contents (abfd
, o
->output_section
,
5842 (file_ptr
) o
->output_offset
,
5848 /* The contents of the .dynstr section are actually in a
5850 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
5851 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
5852 || ! _bfd_elf_strtab_emit (abfd
,
5853 elf_hash_table (info
)->dynstr
))
5859 if (info
->relocateable
)
5861 boolean failed
= false;
5863 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
5868 /* If we have optimized stabs strings, output them. */
5869 if (elf_hash_table (info
)->stab_info
!= NULL
)
5871 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
5875 if (info
->eh_frame_hdr
&& elf_hash_table (info
)->dynobj
)
5877 o
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
5880 && (elf_section_data (o
)->sec_info_type
5881 == ELF_INFO_TYPE_EH_FRAME_HDR
))
5883 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, o
))
5888 if (finfo
.symstrtab
!= NULL
)
5889 _bfd_stringtab_free (finfo
.symstrtab
);
5890 if (finfo
.contents
!= NULL
)
5891 free (finfo
.contents
);
5892 if (finfo
.external_relocs
!= NULL
)
5893 free (finfo
.external_relocs
);
5894 if (finfo
.internal_relocs
!= NULL
)
5895 free (finfo
.internal_relocs
);
5896 if (finfo
.external_syms
!= NULL
)
5897 free (finfo
.external_syms
);
5898 if (finfo
.locsym_shndx
!= NULL
)
5899 free (finfo
.locsym_shndx
);
5900 if (finfo
.internal_syms
!= NULL
)
5901 free (finfo
.internal_syms
);
5902 if (finfo
.indices
!= NULL
)
5903 free (finfo
.indices
);
5904 if (finfo
.sections
!= NULL
)
5905 free (finfo
.sections
);
5906 if (finfo
.symbuf
!= NULL
)
5907 free (finfo
.symbuf
);
5908 if (finfo
.symshndxbuf
!= NULL
)
5909 free (finfo
.symbuf
);
5910 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5912 if ((o
->flags
& SEC_RELOC
) != 0
5913 && elf_section_data (o
)->rel_hashes
!= NULL
)
5914 free (elf_section_data (o
)->rel_hashes
);
5917 elf_tdata (abfd
)->linker
= true;
5922 if (finfo
.symstrtab
!= NULL
)
5923 _bfd_stringtab_free (finfo
.symstrtab
);
5924 if (finfo
.contents
!= NULL
)
5925 free (finfo
.contents
);
5926 if (finfo
.external_relocs
!= NULL
)
5927 free (finfo
.external_relocs
);
5928 if (finfo
.internal_relocs
!= NULL
)
5929 free (finfo
.internal_relocs
);
5930 if (finfo
.external_syms
!= NULL
)
5931 free (finfo
.external_syms
);
5932 if (finfo
.locsym_shndx
!= NULL
)
5933 free (finfo
.locsym_shndx
);
5934 if (finfo
.internal_syms
!= NULL
)
5935 free (finfo
.internal_syms
);
5936 if (finfo
.indices
!= NULL
)
5937 free (finfo
.indices
);
5938 if (finfo
.sections
!= NULL
)
5939 free (finfo
.sections
);
5940 if (finfo
.symbuf
!= NULL
)
5941 free (finfo
.symbuf
);
5942 if (finfo
.symshndxbuf
!= NULL
)
5943 free (finfo
.symbuf
);
5944 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5946 if ((o
->flags
& SEC_RELOC
) != 0
5947 && elf_section_data (o
)->rel_hashes
!= NULL
)
5948 free (elf_section_data (o
)->rel_hashes
);
5954 /* Add a symbol to the output symbol table. */
5957 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
5958 struct elf_final_link_info
*finfo
;
5960 Elf_Internal_Sym
*elfsym
;
5961 asection
*input_sec
;
5963 Elf_External_Sym
*dest
;
5964 Elf_External_Sym_Shndx
*destshndx
;
5966 boolean (*output_symbol_hook
) PARAMS ((bfd
*,
5967 struct bfd_link_info
*info
,
5972 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
5973 elf_backend_link_output_symbol_hook
;
5974 if (output_symbol_hook
!= NULL
)
5976 if (! ((*output_symbol_hook
)
5977 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
5981 if (name
== (const char *) NULL
|| *name
== '\0')
5982 elfsym
->st_name
= 0;
5983 else if (input_sec
->flags
& SEC_EXCLUDE
)
5984 elfsym
->st_name
= 0;
5987 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5989 if (elfsym
->st_name
== (unsigned long) -1)
5993 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5995 if (! elf_link_flush_output_syms (finfo
))
5999 dest
= finfo
->symbuf
+ finfo
->symbuf_count
;
6000 destshndx
= finfo
->symshndxbuf
;
6001 if (destshndx
!= NULL
)
6002 destshndx
+= finfo
->symbuf_count
;
6003 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
, (PTR
) dest
, (PTR
) destshndx
);
6004 ++finfo
->symbuf_count
;
6006 ++ bfd_get_symcount (finfo
->output_bfd
);
6011 /* Flush the output symbols to the file. */
6014 elf_link_flush_output_syms (finfo
)
6015 struct elf_final_link_info
*finfo
;
6017 if (finfo
->symbuf_count
> 0)
6019 Elf_Internal_Shdr
*hdr
;
6023 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6024 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6025 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
6026 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6027 || bfd_bwrite ((PTR
) finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6030 hdr
->sh_size
+= amt
;
6032 if (finfo
->symshndxbuf
!= NULL
)
6034 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_shndx_hdr
;
6035 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6036 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym_Shndx
);
6037 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6038 || (bfd_bwrite ((PTR
) finfo
->symshndxbuf
, amt
, finfo
->output_bfd
)
6042 hdr
->sh_size
+= amt
;
6045 finfo
->symbuf_count
= 0;
6051 /* Adjust all external symbols pointing into SEC_MERGE sections
6052 to reflect the object merging within the sections. */
6055 elf_link_sec_merge_syms (h
, data
)
6056 struct elf_link_hash_entry
*h
;
6061 if (h
->root
.type
== bfd_link_hash_warning
)
6062 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6064 if ((h
->root
.type
== bfd_link_hash_defined
6065 || h
->root
.type
== bfd_link_hash_defweak
)
6066 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
6067 && elf_section_data (sec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
)
6069 bfd
*output_bfd
= (bfd
*) data
;
6071 h
->root
.u
.def
.value
=
6072 _bfd_merged_section_offset (output_bfd
,
6073 &h
->root
.u
.def
.section
,
6074 elf_section_data (sec
)->sec_info
,
6075 h
->root
.u
.def
.value
, (bfd_vma
) 0);
6081 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6082 allowing an unsatisfied unversioned symbol in the DSO to match a
6083 versioned symbol that would normally require an explicit version. */
6086 elf_link_check_versioned_symbol (info
, h
)
6087 struct bfd_link_info
*info
;
6088 struct elf_link_hash_entry
*h
;
6090 bfd
*undef_bfd
= h
->root
.u
.undef
.abfd
;
6091 struct elf_link_loaded_list
*loaded
;
6092 Elf_External_Sym
*buf
;
6093 Elf_External_Versym
*extversym
;
6095 if ((undef_bfd
->flags
& DYNAMIC
) == 0
6096 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
6097 || elf_dt_soname (h
->root
.u
.undef
.abfd
) == NULL
)
6100 for (loaded
= elf_hash_table (info
)->loaded
;
6102 loaded
= loaded
->next
)
6105 Elf_Internal_Shdr
*hdr
;
6106 bfd_size_type symcount
;
6107 bfd_size_type extsymcount
;
6108 bfd_size_type extsymoff
;
6109 Elf_Internal_Shdr
*versymhdr
;
6110 Elf_External_Versym
*ever
;
6111 Elf_External_Sym
*esym
;
6112 Elf_External_Sym
*esymend
;
6113 bfd_size_type count
;
6116 input
= loaded
->abfd
;
6118 /* We check each DSO for a possible hidden versioned definition. */
6119 if (input
== undef_bfd
6120 || (input
->flags
& DYNAMIC
) == 0
6121 || elf_dynversym (input
) == 0)
6124 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6126 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6127 if (elf_bad_symtab (input
))
6129 extsymcount
= symcount
;
6134 extsymcount
= symcount
- hdr
->sh_info
;
6135 extsymoff
= hdr
->sh_info
;
6138 if (extsymcount
== 0)
6141 count
= extsymcount
* sizeof (Elf_External_Sym
);
6142 buf
= (Elf_External_Sym
*) bfd_malloc (count
);
6146 /* Read in the symbol table. */
6147 pos
= hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
);
6148 if (bfd_seek (input
, pos
, SEEK_SET
) != 0
6149 || bfd_bread ((PTR
) buf
, count
, input
) != count
)
6152 /* Read in any version definitions. */
6153 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6154 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
6155 if (extversym
== NULL
)
6158 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6159 || (bfd_bread ((PTR
) extversym
, versymhdr
->sh_size
, input
)
6160 != versymhdr
->sh_size
))
6168 ever
= extversym
+ extsymoff
;
6169 esymend
= buf
+ extsymcount
;
6170 for (esym
= buf
; esym
< esymend
; esym
++, ever
++)
6173 Elf_Internal_Sym sym
;
6174 Elf_Internal_Versym iver
;
6176 elf_swap_symbol_in (input
, esym
, NULL
, &sym
);
6177 if (ELF_ST_BIND (sym
.st_info
) == STB_LOCAL
6178 || sym
.st_shndx
== SHN_UNDEF
)
6181 name
= bfd_elf_string_from_elf_section (input
,
6184 if (strcmp (name
, h
->root
.root
.string
) != 0)
6187 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6189 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6191 /* If we have a non-hidden versioned sym, then it should
6192 have provided a definition for the undefined sym. */
6196 if ((iver
.vs_vers
& VERSYM_VERSION
) == 2)
6198 /* This is the oldest (default) sym. We can use it. */
6212 /* Add an external symbol to the symbol table. This is called from
6213 the hash table traversal routine. When generating a shared object,
6214 we go through the symbol table twice. The first time we output
6215 anything that might have been forced to local scope in a version
6216 script. The second time we output the symbols that are still
6220 elf_link_output_extsym (h
, data
)
6221 struct elf_link_hash_entry
*h
;
6224 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
6225 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6227 Elf_Internal_Sym sym
;
6228 asection
*input_sec
;
6230 if (h
->root
.type
== bfd_link_hash_warning
)
6232 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6233 if (h
->root
.type
== bfd_link_hash_new
)
6237 /* Decide whether to output this symbol in this pass. */
6238 if (eoinfo
->localsyms
)
6240 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6245 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6249 /* If we are not creating a shared library, and this symbol is
6250 referenced by a shared library but is not defined anywhere, then
6251 warn that it is undefined. If we do not do this, the runtime
6252 linker will complain that the symbol is undefined when the
6253 program is run. We don't have to worry about symbols that are
6254 referenced by regular files, because we will already have issued
6255 warnings for them. */
6256 if (! finfo
->info
->relocateable
6257 && ! finfo
->info
->allow_shlib_undefined
6258 && ! finfo
->info
->shared
6259 && h
->root
.type
== bfd_link_hash_undefined
6260 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
6261 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
6262 && ! elf_link_check_versioned_symbol (finfo
->info
, h
))
6264 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6265 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6266 (asection
*) NULL
, (bfd_vma
) 0, true)))
6268 eoinfo
->failed
= true;
6273 /* We don't want to output symbols that have never been mentioned by
6274 a regular file, or that we have been told to strip. However, if
6275 h->indx is set to -2, the symbol is used by a reloc and we must
6279 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
6280 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
6281 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
6282 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
6284 else if (finfo
->info
->strip
== strip_all
6285 || (finfo
->info
->strip
== strip_some
6286 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6287 h
->root
.root
.string
,
6288 false, false) == NULL
))
6293 /* If we're stripping it, and it's not a dynamic symbol, there's
6294 nothing else to do unless it is a forced local symbol. */
6297 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6301 sym
.st_size
= h
->size
;
6302 sym
.st_other
= h
->other
;
6303 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6304 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6305 else if (h
->root
.type
== bfd_link_hash_undefweak
6306 || h
->root
.type
== bfd_link_hash_defweak
)
6307 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6309 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6311 switch (h
->root
.type
)
6314 case bfd_link_hash_new
:
6315 case bfd_link_hash_warning
:
6319 case bfd_link_hash_undefined
:
6320 case bfd_link_hash_undefweak
:
6321 input_sec
= bfd_und_section_ptr
;
6322 sym
.st_shndx
= SHN_UNDEF
;
6325 case bfd_link_hash_defined
:
6326 case bfd_link_hash_defweak
:
6328 input_sec
= h
->root
.u
.def
.section
;
6329 if (input_sec
->output_section
!= NULL
)
6332 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6333 input_sec
->output_section
);
6334 if (sym
.st_shndx
== SHN_BAD
)
6336 (*_bfd_error_handler
)
6337 (_("%s: could not find output section %s for input section %s"),
6338 bfd_get_filename (finfo
->output_bfd
),
6339 input_sec
->output_section
->name
,
6341 eoinfo
->failed
= true;
6345 /* ELF symbols in relocateable files are section relative,
6346 but in nonrelocateable files they are virtual
6348 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6349 if (! finfo
->info
->relocateable
)
6351 sym
.st_value
+= input_sec
->output_section
->vma
;
6352 if (h
->type
== STT_TLS
)
6354 /* STT_TLS symbols are relative to PT_TLS segment
6356 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6357 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
6363 BFD_ASSERT (input_sec
->owner
== NULL
6364 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6365 sym
.st_shndx
= SHN_UNDEF
;
6366 input_sec
= bfd_und_section_ptr
;
6371 case bfd_link_hash_common
:
6372 input_sec
= h
->root
.u
.c
.p
->section
;
6373 sym
.st_shndx
= SHN_COMMON
;
6374 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6377 case bfd_link_hash_indirect
:
6378 /* These symbols are created by symbol versioning. They point
6379 to the decorated version of the name. For example, if the
6380 symbol foo@@GNU_1.2 is the default, which should be used when
6381 foo is used with no version, then we add an indirect symbol
6382 foo which points to foo@@GNU_1.2. We ignore these symbols,
6383 since the indirected symbol is already in the hash table. */
6387 /* Give the processor backend a chance to tweak the symbol value,
6388 and also to finish up anything that needs to be done for this
6389 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6390 forced local syms when non-shared is due to a historical quirk. */
6391 if ((h
->dynindx
!= -1
6392 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6393 && (finfo
->info
->shared
6394 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6395 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6397 struct elf_backend_data
*bed
;
6399 bed
= get_elf_backend_data (finfo
->output_bfd
);
6400 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6401 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6403 eoinfo
->failed
= true;
6408 /* If we are marking the symbol as undefined, and there are no
6409 non-weak references to this symbol from a regular object, then
6410 mark the symbol as weak undefined; if there are non-weak
6411 references, mark the symbol as strong. We can't do this earlier,
6412 because it might not be marked as undefined until the
6413 finish_dynamic_symbol routine gets through with it. */
6414 if (sym
.st_shndx
== SHN_UNDEF
6415 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
6416 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6417 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6421 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
6422 bindtype
= STB_GLOBAL
;
6424 bindtype
= STB_WEAK
;
6425 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6428 /* If a symbol is not defined locally, we clear the visibility
6430 if (! finfo
->info
->relocateable
6431 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6432 sym
.st_other
^= ELF_ST_VISIBILITY (sym
.st_other
);
6434 /* If this symbol should be put in the .dynsym section, then put it
6435 there now. We already know the symbol index. We also fill in
6436 the entry in the .hash section. */
6437 if (h
->dynindx
!= -1
6438 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6442 size_t hash_entry_size
;
6443 bfd_byte
*bucketpos
;
6445 Elf_External_Sym
*esym
;
6447 sym
.st_name
= h
->dynstr_index
;
6448 esym
= (Elf_External_Sym
*) finfo
->dynsym_sec
->contents
+ h
->dynindx
;
6449 elf_swap_symbol_out (finfo
->output_bfd
, &sym
, (PTR
) esym
, (PTR
) 0);
6451 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6452 bucket
= h
->elf_hash_value
% bucketcount
;
6454 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6455 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6456 + (bucket
+ 2) * hash_entry_size
);
6457 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6458 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, (bfd_vma
) h
->dynindx
,
6460 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6461 ((bfd_byte
*) finfo
->hash_sec
->contents
6462 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6464 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6466 Elf_Internal_Versym iversym
;
6467 Elf_External_Versym
*eversym
;
6469 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6471 if (h
->verinfo
.verdef
== NULL
)
6472 iversym
.vs_vers
= 0;
6474 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6478 if (h
->verinfo
.vertree
== NULL
)
6479 iversym
.vs_vers
= 1;
6481 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6484 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
6485 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6487 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6488 eversym
+= h
->dynindx
;
6489 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6493 /* If we're stripping it, then it was just a dynamic symbol, and
6494 there's nothing else to do. */
6498 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6500 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
6502 eoinfo
->failed
= true;
6509 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6510 originated from the section given by INPUT_REL_HDR) to the
6514 elf_link_output_relocs (output_bfd
, input_section
, input_rel_hdr
,
6517 asection
*input_section
;
6518 Elf_Internal_Shdr
*input_rel_hdr
;
6519 Elf_Internal_Rela
*internal_relocs
;
6521 Elf_Internal_Rela
*irela
;
6522 Elf_Internal_Rela
*irelaend
;
6523 Elf_Internal_Shdr
*output_rel_hdr
;
6524 asection
*output_section
;
6525 unsigned int *rel_countp
= NULL
;
6526 struct elf_backend_data
*bed
;
6529 output_section
= input_section
->output_section
;
6530 output_rel_hdr
= NULL
;
6532 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
6533 == input_rel_hdr
->sh_entsize
)
6535 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
6536 rel_countp
= &elf_section_data (output_section
)->rel_count
;
6538 else if (elf_section_data (output_section
)->rel_hdr2
6539 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
6540 == input_rel_hdr
->sh_entsize
))
6542 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
6543 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
6547 (*_bfd_error_handler
) (
6548 _("%s: relocation size mismatch in %s section %s"),
6549 bfd_get_filename (output_bfd
),
6550 bfd_archive_filename (input_section
->owner
),
6551 input_section
->name
);
6552 bfd_set_error (bfd_error_wrong_object_format
);
6556 bed
= get_elf_backend_data (output_bfd
);
6557 irela
= internal_relocs
;
6558 irelaend
= irela
+ NUM_SHDR_ENTRIES (input_rel_hdr
)
6559 * bed
->s
->int_rels_per_ext_rel
;
6561 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
6563 Elf_External_Rel
*erel
;
6564 Elf_Internal_Rel
*irel
;
6566 amt
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
6567 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (amt
);
6570 (*_bfd_error_handler
) (_("Error: out of memory"));
6574 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
+ *rel_countp
);
6575 for (; irela
< irelaend
; irela
+= bed
->s
->int_rels_per_ext_rel
, erel
++)
6579 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
6581 irel
[i
].r_offset
= irela
[i
].r_offset
;
6582 irel
[i
].r_info
= irela
[i
].r_info
;
6583 BFD_ASSERT (irela
[i
].r_addend
== 0);
6586 if (bed
->s
->swap_reloc_out
)
6587 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, (PTR
) erel
);
6589 elf_swap_reloc_out (output_bfd
, irel
, erel
);
6596 Elf_External_Rela
*erela
;
6598 BFD_ASSERT (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
));
6600 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
+ *rel_countp
);
6601 for (; irela
< irelaend
; irela
+= bed
->s
->int_rels_per_ext_rel
, erela
++)
6602 if (bed
->s
->swap_reloca_out
)
6603 (*bed
->s
->swap_reloca_out
) (output_bfd
, irela
, (PTR
) erela
);
6605 elf_swap_reloca_out (output_bfd
, irela
, erela
);
6608 /* Bump the counter, so that we know where to add the next set of
6610 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
6615 /* Link an input file into the linker output file. This function
6616 handles all the sections and relocations of the input file at once.
6617 This is so that we only have to read the local symbols once, and
6618 don't have to keep them in memory. */
6621 elf_link_input_bfd (finfo
, input_bfd
)
6622 struct elf_final_link_info
*finfo
;
6625 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
6626 bfd
*, asection
*, bfd_byte
*,
6627 Elf_Internal_Rela
*,
6628 Elf_Internal_Sym
*, asection
**));
6630 Elf_Internal_Shdr
*symtab_hdr
;
6631 Elf_Internal_Shdr
*shndx_hdr
;
6634 Elf_External_Sym
*external_syms
;
6635 Elf_External_Sym
*esym
;
6636 Elf_External_Sym
*esymend
;
6637 Elf_External_Sym_Shndx
*shndx_buf
;
6638 Elf_External_Sym_Shndx
*shndx
;
6639 Elf_Internal_Sym
*isym
;
6641 asection
**ppsection
;
6643 struct elf_backend_data
*bed
;
6644 boolean emit_relocs
;
6645 struct elf_link_hash_entry
**sym_hashes
;
6647 output_bfd
= finfo
->output_bfd
;
6648 bed
= get_elf_backend_data (output_bfd
);
6649 relocate_section
= bed
->elf_backend_relocate_section
;
6651 /* If this is a dynamic object, we don't want to do anything here:
6652 we don't want the local symbols, and we don't want the section
6654 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6657 emit_relocs
= (finfo
->info
->relocateable
6658 || finfo
->info
->emitrelocations
6659 || bed
->elf_backend_emit_relocs
);
6661 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6662 if (elf_bad_symtab (input_bfd
))
6664 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6669 locsymcount
= symtab_hdr
->sh_info
;
6670 extsymoff
= symtab_hdr
->sh_info
;
6673 /* Read the local symbols. */
6674 if (symtab_hdr
->contents
!= NULL
)
6675 external_syms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
6676 else if (locsymcount
== 0)
6677 external_syms
= NULL
;
6680 bfd_size_type amt
= locsymcount
* sizeof (Elf_External_Sym
);
6681 external_syms
= finfo
->external_syms
;
6682 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
6683 || bfd_bread (external_syms
, amt
, input_bfd
) != amt
)
6687 shndx_hdr
= &elf_tdata (input_bfd
)->symtab_shndx_hdr
;
6689 if (shndx_hdr
->sh_size
!= 0 && locsymcount
!= 0)
6691 bfd_size_type amt
= locsymcount
* sizeof (Elf_External_Sym_Shndx
);
6692 shndx_buf
= finfo
->locsym_shndx
;
6693 if (bfd_seek (input_bfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
6694 || bfd_bread (shndx_buf
, amt
, input_bfd
) != amt
)
6698 /* Swap in the local symbols and write out the ones which we know
6699 are going into the output file. */
6700 for (esym
= external_syms
, esymend
= esym
+ locsymcount
,
6701 isym
= finfo
->internal_syms
, pindex
= finfo
->indices
,
6702 ppsection
= finfo
->sections
, shndx
= shndx_buf
;
6704 esym
++, isym
++, pindex
++, ppsection
++,
6705 shndx
= (shndx
!= NULL
? shndx
+ 1 : NULL
))
6709 Elf_Internal_Sym osym
;
6711 elf_swap_symbol_in (input_bfd
, (const PTR
) esym
, (const PTR
) shndx
,
6715 if (elf_bad_symtab (input_bfd
))
6717 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6724 if (isym
->st_shndx
== SHN_UNDEF
)
6725 isec
= bfd_und_section_ptr
;
6726 else if (isym
->st_shndx
< SHN_LORESERVE
6727 || isym
->st_shndx
> SHN_HIRESERVE
)
6729 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
6731 && elf_section_data (isec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
6732 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6734 _bfd_merged_section_offset (output_bfd
, &isec
,
6735 elf_section_data (isec
)->sec_info
,
6736 isym
->st_value
, (bfd_vma
) 0);
6738 else if (isym
->st_shndx
== SHN_ABS
)
6739 isec
= bfd_abs_section_ptr
;
6740 else if (isym
->st_shndx
== SHN_COMMON
)
6741 isec
= bfd_com_section_ptr
;
6750 /* Don't output the first, undefined, symbol. */
6751 if (esym
== external_syms
)
6754 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6756 /* We never output section symbols. Instead, we use the
6757 section symbol of the corresponding section in the output
6762 /* If we are stripping all symbols, we don't want to output this
6764 if (finfo
->info
->strip
== strip_all
)
6767 /* If we are discarding all local symbols, we don't want to
6768 output this one. If we are generating a relocateable output
6769 file, then some of the local symbols may be required by
6770 relocs; we output them below as we discover that they are
6772 if (finfo
->info
->discard
== discard_all
)
6775 /* If this symbol is defined in a section which we are
6776 discarding, we don't need to keep it, but note that
6777 linker_mark is only reliable for sections that have contents.
6778 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6779 as well as linker_mark. */
6780 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6782 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6783 || (! finfo
->info
->relocateable
6784 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6787 /* Get the name of the symbol. */
6788 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6793 /* See if we are discarding symbols with this name. */
6794 if ((finfo
->info
->strip
== strip_some
6795 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
6797 || (((finfo
->info
->discard
== discard_sec_merge
6798 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocateable
)
6799 || finfo
->info
->discard
== discard_l
)
6800 && bfd_is_local_label_name (input_bfd
, name
)))
6803 /* If we get here, we are going to output this symbol. */
6807 /* Adjust the section index for the output file. */
6808 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6809 isec
->output_section
);
6810 if (osym
.st_shndx
== SHN_BAD
)
6813 *pindex
= bfd_get_symcount (output_bfd
);
6815 /* ELF symbols in relocateable files are section relative, but
6816 in executable files they are virtual addresses. Note that
6817 this code assumes that all ELF sections have an associated
6818 BFD section with a reasonable value for output_offset; below
6819 we assume that they also have a reasonable value for
6820 output_section. Any special sections must be set up to meet
6821 these requirements. */
6822 osym
.st_value
+= isec
->output_offset
;
6823 if (! finfo
->info
->relocateable
)
6825 osym
.st_value
+= isec
->output_section
->vma
;
6826 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6828 /* STT_TLS symbols are relative to PT_TLS segment base. */
6829 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6830 osym
.st_value
-= finfo
->first_tls_sec
->vma
;
6834 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
6838 /* Relocate the contents of each section. */
6839 sym_hashes
= elf_sym_hashes (input_bfd
);
6840 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6844 if (! o
->linker_mark
)
6846 /* This section was omitted from the link. */
6850 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6851 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6854 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6856 /* Section was created by elf_link_create_dynamic_sections
6861 /* Get the contents of the section. They have been cached by a
6862 relaxation routine. Note that o is a section in an input
6863 file, so the contents field will not have been set by any of
6864 the routines which work on output files. */
6865 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6866 contents
= elf_section_data (o
)->this_hdr
.contents
;
6869 contents
= finfo
->contents
;
6870 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
6871 (file_ptr
) 0, o
->_raw_size
))
6875 if ((o
->flags
& SEC_RELOC
) != 0)
6877 Elf_Internal_Rela
*internal_relocs
;
6879 /* Get the swapped relocs. */
6880 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
6881 (input_bfd
, o
, finfo
->external_relocs
,
6882 finfo
->internal_relocs
, false));
6883 if (internal_relocs
== NULL
6884 && o
->reloc_count
> 0)
6887 /* Run through the relocs looking for any against symbols
6888 from discarded sections and section symbols from
6889 removed link-once sections. Complain about relocs
6890 against discarded sections. Zero relocs against removed
6891 link-once sections. We should really complain if
6892 anything in the final link tries to use it, but
6893 DWARF-based exception handling might have an entry in
6894 .eh_frame to describe a routine in the linkonce section,
6895 and it turns out to be hard to remove the .eh_frame
6896 entry too. FIXME. */
6897 if (!finfo
->info
->relocateable
6898 && !elf_section_ignore_discarded_relocs (o
))
6900 Elf_Internal_Rela
*rel
, *relend
;
6902 rel
= internal_relocs
;
6903 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6904 for ( ; rel
< relend
; rel
++)
6906 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
6908 if (r_symndx
>= locsymcount
6909 || (elf_bad_symtab (input_bfd
)
6910 && finfo
->sections
[r_symndx
] == NULL
))
6912 struct elf_link_hash_entry
*h
;
6914 h
= sym_hashes
[r_symndx
- extsymoff
];
6915 while (h
->root
.type
== bfd_link_hash_indirect
6916 || h
->root
.type
== bfd_link_hash_warning
)
6917 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6919 /* Complain if the definition comes from a
6920 discarded section. */
6921 if ((h
->root
.type
== bfd_link_hash_defined
6922 || h
->root
.type
== bfd_link_hash_defweak
)
6923 && elf_discarded_section (h
->root
.u
.def
.section
))
6925 #if BFD_VERSION_DATE < 20031005
6926 if ((o
->flags
& SEC_DEBUGGING
) != 0)
6928 #if BFD_VERSION_DATE > 20021005
6929 (*finfo
->info
->callbacks
->warning
)
6931 _("warning: relocation against removed section; zeroing"),
6932 NULL
, input_bfd
, o
, rel
->r_offset
);
6934 BFD_ASSERT (r_symndx
!= 0);
6935 memset (rel
, 0, sizeof (*rel
));
6940 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6941 (finfo
->info
, h
->root
.root
.string
,
6942 input_bfd
, o
, rel
->r_offset
,
6950 asection
*sec
= finfo
->sections
[r_symndx
];
6952 if (sec
!= NULL
&& elf_discarded_section (sec
))
6954 #if BFD_VERSION_DATE < 20031005
6955 if ((o
->flags
& SEC_DEBUGGING
) != 0
6956 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
6958 #if BFD_VERSION_DATE > 20021005
6959 (*finfo
->info
->callbacks
->warning
)
6961 _("warning: relocation against removed section"),
6962 NULL
, input_bfd
, o
, rel
->r_offset
);
6964 BFD_ASSERT (r_symndx
!= 0);
6966 = ELF_R_INFO (0, ELF_R_TYPE (rel
->r_info
));
6974 = _("local symbols in discarded section %s");
6976 = strlen (sec
->name
) + strlen (msg
) - 1;
6977 char *buf
= (char *) bfd_malloc (amt
);
6980 sprintf (buf
, msg
, sec
->name
);
6982 buf
= (char *) sec
->name
;
6983 ok
= (*finfo
->info
->callbacks
6984 ->undefined_symbol
) (finfo
->info
, buf
,
6988 if (buf
!= sec
->name
)
6998 /* Relocate the section by invoking a back end routine.
7000 The back end routine is responsible for adjusting the
7001 section contents as necessary, and (if using Rela relocs
7002 and generating a relocateable output file) adjusting the
7003 reloc addend as necessary.
7005 The back end routine does not have to worry about setting
7006 the reloc address or the reloc symbol index.
7008 The back end routine is given a pointer to the swapped in
7009 internal symbols, and can access the hash table entries
7010 for the external symbols via elf_sym_hashes (input_bfd).
7012 When generating relocateable output, the back end routine
7013 must handle STB_LOCAL/STT_SECTION symbols specially. The
7014 output symbol is going to be a section symbol
7015 corresponding to the output section, which will require
7016 the addend to be adjusted. */
7018 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
7019 input_bfd
, o
, contents
,
7021 finfo
->internal_syms
,
7027 Elf_Internal_Rela
*irela
;
7028 Elf_Internal_Rela
*irelaend
;
7029 struct elf_link_hash_entry
**rel_hash
;
7030 Elf_Internal_Shdr
*input_rel_hdr
;
7031 unsigned int next_erel
;
7032 boolean (*reloc_emitter
) PARAMS ((bfd
*, asection
*,
7033 Elf_Internal_Shdr
*,
7034 Elf_Internal_Rela
*));
7035 boolean rela_normal
;
7037 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7038 rela_normal
= (bed
->rela_normal
7039 && (input_rel_hdr
->sh_entsize
7040 == sizeof (Elf_External_Rela
)));
7042 /* Adjust the reloc addresses and symbol indices. */
7044 irela
= internal_relocs
;
7045 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7046 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7047 + elf_section_data (o
->output_section
)->rel_count
7048 + elf_section_data (o
->output_section
)->rel_count2
);
7049 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7051 unsigned long r_symndx
;
7054 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7060 irela
->r_offset
+= o
->output_offset
;
7062 /* Relocs in an executable have to be virtual addresses. */
7063 if (!finfo
->info
->relocateable
)
7064 irela
->r_offset
+= o
->output_section
->vma
;
7066 r_symndx
= ELF_R_SYM (irela
->r_info
);
7071 if (r_symndx
>= locsymcount
7072 || (elf_bad_symtab (input_bfd
)
7073 && finfo
->sections
[r_symndx
] == NULL
))
7075 struct elf_link_hash_entry
*rh
;
7078 /* This is a reloc against a global symbol. We
7079 have not yet output all the local symbols, so
7080 we do not know the symbol index of any global
7081 symbol. We set the rel_hash entry for this
7082 reloc to point to the global hash table entry
7083 for this symbol. The symbol index is then
7084 set at the end of elf_bfd_final_link. */
7085 indx
= r_symndx
- extsymoff
;
7086 rh
= elf_sym_hashes (input_bfd
)[indx
];
7087 while (rh
->root
.type
== bfd_link_hash_indirect
7088 || rh
->root
.type
== bfd_link_hash_warning
)
7089 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7091 /* Setting the index to -2 tells
7092 elf_link_output_extsym that this symbol is
7094 BFD_ASSERT (rh
->indx
< 0);
7102 /* This is a reloc against a local symbol. */
7105 isym
= finfo
->internal_syms
+ r_symndx
;
7106 sec
= finfo
->sections
[r_symndx
];
7107 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
7109 /* I suppose the backend ought to fill in the
7110 section of any STT_SECTION symbol against a
7111 processor specific section. If we have
7112 discarded a section, the output_section will
7113 be the absolute section. */
7114 if (bfd_is_abs_section (sec
)
7116 && bfd_is_abs_section (sec
->output_section
)))
7118 else if (sec
== NULL
|| sec
->owner
== NULL
)
7120 bfd_set_error (bfd_error_bad_value
);
7125 r_symndx
= sec
->output_section
->target_index
;
7126 BFD_ASSERT (r_symndx
!= 0);
7129 /* Adjust the addend according to where the
7130 section winds up in the output section. */
7132 irela
->r_addend
+= sec
->output_offset
;
7136 if (finfo
->indices
[r_symndx
] == -1)
7138 unsigned long shlink
;
7142 if (finfo
->info
->strip
== strip_all
)
7144 /* You can't do ld -r -s. */
7145 bfd_set_error (bfd_error_invalid_operation
);
7149 /* This symbol was skipped earlier, but
7150 since it is needed by a reloc, we
7151 must output it now. */
7152 shlink
= symtab_hdr
->sh_link
;
7153 name
= (bfd_elf_string_from_elf_section
7154 (input_bfd
, shlink
, isym
->st_name
));
7158 osec
= sec
->output_section
;
7160 _bfd_elf_section_from_bfd_section (output_bfd
,
7162 if (isym
->st_shndx
== SHN_BAD
)
7165 isym
->st_value
+= sec
->output_offset
;
7166 if (! finfo
->info
->relocateable
)
7168 isym
->st_value
+= osec
->vma
;
7169 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
7171 /* STT_TLS symbols are relative to PT_TLS
7173 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
7174 isym
->st_value
-= finfo
->first_tls_sec
->vma
;
7178 finfo
->indices
[r_symndx
]
7179 = bfd_get_symcount (output_bfd
);
7181 if (! elf_link_output_sym (finfo
, name
, isym
, sec
))
7185 r_symndx
= finfo
->indices
[r_symndx
];
7188 irela
->r_info
= ELF_R_INFO (r_symndx
,
7189 ELF_R_TYPE (irela
->r_info
));
7192 /* Swap out the relocs. */
7193 if (bed
->elf_backend_emit_relocs
7194 && !(finfo
->info
->relocateable
7195 || finfo
->info
->emitrelocations
))
7196 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7198 reloc_emitter
= elf_link_output_relocs
;
7200 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7204 input_rel_hdr
= elf_section_data (o
)->rel_hdr2
;
7207 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7208 * bed
->s
->int_rels_per_ext_rel
);
7209 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7217 /* Write out the modified section contents. */
7218 if (bed
->elf_backend_write_section
7219 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7221 /* Section written out. */
7223 else switch (elf_section_data (o
)->sec_info_type
)
7225 case ELF_INFO_TYPE_STABS
:
7226 if (! (_bfd_write_section_stabs
7228 &elf_hash_table (finfo
->info
)->stab_info
,
7229 o
, &elf_section_data (o
)->sec_info
, contents
)))
7232 case ELF_INFO_TYPE_MERGE
:
7233 if (! (_bfd_write_merged_section
7234 (output_bfd
, o
, elf_section_data (o
)->sec_info
)))
7237 case ELF_INFO_TYPE_EH_FRAME
:
7242 = bfd_get_section_by_name (elf_hash_table (finfo
->info
)->dynobj
,
7244 if (! (_bfd_elf_write_section_eh_frame (output_bfd
, o
, ehdrsec
,
7251 bfd_size_type sec_size
;
7253 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
7254 if (! (o
->flags
& SEC_EXCLUDE
)
7255 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7257 (file_ptr
) o
->output_offset
,
7268 /* Generate a reloc when linking an ELF file. This is a reloc
7269 requested by the linker, and does come from any input file. This
7270 is used to build constructor and destructor tables when linking
7274 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
7276 struct bfd_link_info
*info
;
7277 asection
*output_section
;
7278 struct bfd_link_order
*link_order
;
7280 reloc_howto_type
*howto
;
7284 struct elf_link_hash_entry
**rel_hash_ptr
;
7285 Elf_Internal_Shdr
*rel_hdr
;
7286 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7288 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7291 bfd_set_error (bfd_error_bad_value
);
7295 addend
= link_order
->u
.reloc
.p
->addend
;
7297 /* Figure out the symbol index. */
7298 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7299 + elf_section_data (output_section
)->rel_count
7300 + elf_section_data (output_section
)->rel_count2
);
7301 if (link_order
->type
== bfd_section_reloc_link_order
)
7303 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7304 BFD_ASSERT (indx
!= 0);
7305 *rel_hash_ptr
= NULL
;
7309 struct elf_link_hash_entry
*h
;
7311 /* Treat a reloc against a defined symbol as though it were
7312 actually against the section. */
7313 h
= ((struct elf_link_hash_entry
*)
7314 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7315 link_order
->u
.reloc
.p
->u
.name
,
7316 false, false, true));
7318 && (h
->root
.type
== bfd_link_hash_defined
7319 || h
->root
.type
== bfd_link_hash_defweak
))
7323 section
= h
->root
.u
.def
.section
;
7324 indx
= section
->output_section
->target_index
;
7325 *rel_hash_ptr
= NULL
;
7326 /* It seems that we ought to add the symbol value to the
7327 addend here, but in practice it has already been added
7328 because it was passed to constructor_callback. */
7329 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7333 /* Setting the index to -2 tells elf_link_output_extsym that
7334 this symbol is used by a reloc. */
7341 if (! ((*info
->callbacks
->unattached_reloc
)
7342 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
7343 (asection
*) NULL
, (bfd_vma
) 0)))
7349 /* If this is an inplace reloc, we must write the addend into the
7351 if (howto
->partial_inplace
&& addend
!= 0)
7354 bfd_reloc_status_type rstat
;
7357 const char *sym_name
;
7359 size
= bfd_get_reloc_size (howto
);
7360 buf
= (bfd_byte
*) bfd_zmalloc (size
);
7361 if (buf
== (bfd_byte
*) NULL
)
7363 rstat
= _bfd_relocate_contents (howto
, output_bfd
, (bfd_vma
) addend
, buf
);
7370 case bfd_reloc_outofrange
:
7373 case bfd_reloc_overflow
:
7374 if (link_order
->type
== bfd_section_reloc_link_order
)
7375 sym_name
= bfd_section_name (output_bfd
,
7376 link_order
->u
.reloc
.p
->u
.section
);
7378 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7379 if (! ((*info
->callbacks
->reloc_overflow
)
7380 (info
, sym_name
, howto
->name
, addend
,
7381 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
7388 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
7389 (file_ptr
) link_order
->offset
, size
);
7395 /* The address of a reloc is relative to the section in a
7396 relocateable file, and is a virtual address in an executable
7398 offset
= link_order
->offset
;
7399 if (! info
->relocateable
)
7400 offset
+= output_section
->vma
;
7402 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7404 if (rel_hdr
->sh_type
== SHT_REL
)
7407 Elf_Internal_Rel
*irel
;
7408 Elf_External_Rel
*erel
;
7411 size
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
7412 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (size
);
7416 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7417 irel
[i
].r_offset
= offset
;
7418 irel
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
7420 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
7421 + elf_section_data (output_section
)->rel_count
);
7423 if (bed
->s
->swap_reloc_out
)
7424 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, (bfd_byte
*) erel
);
7426 elf_swap_reloc_out (output_bfd
, irel
, erel
);
7433 Elf_Internal_Rela
*irela
;
7434 Elf_External_Rela
*erela
;
7437 size
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
7438 irela
= (Elf_Internal_Rela
*) bfd_zmalloc (size
);
7442 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7443 irela
[i
].r_offset
= offset
;
7444 irela
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
7445 irela
[0].r_addend
= addend
;
7447 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
7448 + elf_section_data (output_section
)->rel_count
);
7450 if (bed
->s
->swap_reloca_out
)
7451 (*bed
->s
->swap_reloca_out
) (output_bfd
, irela
, (bfd_byte
*) erela
);
7453 elf_swap_reloca_out (output_bfd
, irela
, erela
);
7456 ++elf_section_data (output_section
)->rel_count
;
7461 /* Allocate a pointer to live in a linker created section. */
7464 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
7466 struct bfd_link_info
*info
;
7467 elf_linker_section_t
*lsect
;
7468 struct elf_link_hash_entry
*h
;
7469 const Elf_Internal_Rela
*rel
;
7471 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
7472 elf_linker_section_pointers_t
*linker_section_ptr
;
7473 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7476 BFD_ASSERT (lsect
!= NULL
);
7478 /* Is this a global symbol? */
7481 /* Has this symbol already been allocated? If so, our work is done. */
7482 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
7487 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
7488 /* Make sure this symbol is output as a dynamic symbol. */
7489 if (h
->dynindx
== -1)
7491 if (! elf_link_record_dynamic_symbol (info
, h
))
7495 if (lsect
->rel_section
)
7496 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7500 /* Allocation of a pointer to a local symbol. */
7501 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
7503 /* Allocate a table to hold the local symbols if first time. */
7506 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
7507 register unsigned int i
;
7510 amt
*= sizeof (elf_linker_section_pointers_t
*);
7511 ptr
= (elf_linker_section_pointers_t
**) bfd_alloc (abfd
, amt
);
7516 elf_local_ptr_offsets (abfd
) = ptr
;
7517 for (i
= 0; i
< num_symbols
; i
++)
7518 ptr
[i
] = (elf_linker_section_pointers_t
*) 0;
7521 /* Has this symbol already been allocated? If so, our work is done. */
7522 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
7527 ptr_linker_section_ptr
= &ptr
[r_symndx
];
7531 /* If we are generating a shared object, we need to
7532 output a R_<xxx>_RELATIVE reloc so that the
7533 dynamic linker can adjust this GOT entry. */
7534 BFD_ASSERT (lsect
->rel_section
!= NULL
);
7535 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7539 /* Allocate space for a pointer in the linker section, and allocate
7540 a new pointer record from internal memory. */
7541 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
7542 amt
= sizeof (elf_linker_section_pointers_t
);
7543 linker_section_ptr
= (elf_linker_section_pointers_t
*) bfd_alloc (abfd
, amt
);
7545 if (!linker_section_ptr
)
7548 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
7549 linker_section_ptr
->addend
= rel
->r_addend
;
7550 linker_section_ptr
->which
= lsect
->which
;
7551 linker_section_ptr
->written_address_p
= false;
7552 *ptr_linker_section_ptr
= linker_section_ptr
;
7555 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
7557 linker_section_ptr
->offset
= (lsect
->section
->_raw_size
7558 - lsect
->hole_size
+ (ARCH_SIZE
/ 8));
7559 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
7560 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
7561 if (lsect
->sym_hash
)
7563 /* Bump up symbol value if needed. */
7564 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
7566 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
7567 lsect
->sym_hash
->root
.root
.string
,
7568 (long) ARCH_SIZE
/ 8,
7569 (long) lsect
->sym_hash
->root
.u
.def
.value
);
7575 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
7577 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
7581 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7582 lsect
->name
, (long) linker_section_ptr
->offset
,
7583 (long) lsect
->section
->_raw_size
);
7590 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7593 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7596 /* Fill in the address for a pointer generated in a linker section. */
7599 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
,
7600 relocation
, rel
, relative_reloc
)
7603 struct bfd_link_info
*info
;
7604 elf_linker_section_t
*lsect
;
7605 struct elf_link_hash_entry
*h
;
7607 const Elf_Internal_Rela
*rel
;
7610 elf_linker_section_pointers_t
*linker_section_ptr
;
7612 BFD_ASSERT (lsect
!= NULL
);
7616 /* Handle global symbol. */
7617 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7618 (h
->linker_section_pointer
,
7622 BFD_ASSERT (linker_section_ptr
!= NULL
);
7624 if (! elf_hash_table (info
)->dynamic_sections_created
7627 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
7629 /* This is actually a static link, or it is a
7630 -Bsymbolic link and the symbol is defined
7631 locally. We must initialize this entry in the
7634 When doing a dynamic link, we create a .rela.<xxx>
7635 relocation entry to initialize the value. This
7636 is done in the finish_dynamic_symbol routine. */
7637 if (!linker_section_ptr
->written_address_p
)
7639 linker_section_ptr
->written_address_p
= true;
7640 bfd_put_ptr (output_bfd
,
7641 relocation
+ linker_section_ptr
->addend
,
7642 (lsect
->section
->contents
7643 + linker_section_ptr
->offset
));
7649 /* Handle local symbol. */
7650 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7651 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
7652 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
7653 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7654 (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
7658 BFD_ASSERT (linker_section_ptr
!= NULL
);
7660 /* Write out pointer if it hasn't been rewritten out before. */
7661 if (!linker_section_ptr
->written_address_p
)
7663 linker_section_ptr
->written_address_p
= true;
7664 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
7665 lsect
->section
->contents
+ linker_section_ptr
->offset
);
7669 asection
*srel
= lsect
->rel_section
;
7670 Elf_Internal_Rela
*outrel
;
7671 Elf_External_Rela
*erel
;
7672 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7676 amt
= sizeof (Elf_Internal_Rela
) * bed
->s
->int_rels_per_ext_rel
;
7677 outrel
= (Elf_Internal_Rela
*) bfd_zmalloc (amt
);
7680 (*_bfd_error_handler
) (_("Error: out of memory"));
7684 /* We need to generate a relative reloc for the dynamic
7688 srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
7690 lsect
->rel_section
= srel
;
7693 BFD_ASSERT (srel
!= NULL
);
7695 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7696 outrel
[i
].r_offset
= (lsect
->section
->output_section
->vma
7697 + lsect
->section
->output_offset
7698 + linker_section_ptr
->offset
);
7699 outrel
[0].r_info
= ELF_R_INFO (0, relative_reloc
);
7700 outrel
[0].r_addend
= 0;
7701 erel
= (Elf_External_Rela
*) lsect
->section
->contents
;
7702 erel
+= elf_section_data (lsect
->section
)->rel_count
;
7703 elf_swap_reloca_out (output_bfd
, outrel
, erel
);
7704 ++elf_section_data (lsect
->section
)->rel_count
;
7711 relocation
= (lsect
->section
->output_offset
7712 + linker_section_ptr
->offset
7713 - lsect
->hole_offset
7714 - lsect
->sym_offset
);
7718 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7719 lsect
->name
, (long) relocation
, (long) relocation
);
7722 /* Subtract out the addend, because it will get added back in by the normal
7724 return relocation
- linker_section_ptr
->addend
;
7727 /* Garbage collect unused sections. */
7729 static boolean elf_gc_mark
7730 PARAMS ((struct bfd_link_info
*info
, asection
*sec
,
7731 asection
* (*gc_mark_hook
)
7732 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7733 struct elf_link_hash_entry
*, Elf_Internal_Sym
*))));
7735 static boolean elf_gc_sweep
7736 PARAMS ((struct bfd_link_info
*info
,
7737 boolean (*gc_sweep_hook
)
7738 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
7739 const Elf_Internal_Rela
*relocs
))));
7741 static boolean elf_gc_sweep_symbol
7742 PARAMS ((struct elf_link_hash_entry
*h
, PTR idxptr
));
7744 static boolean elf_gc_allocate_got_offsets
7745 PARAMS ((struct elf_link_hash_entry
*h
, PTR offarg
));
7747 static boolean elf_gc_propagate_vtable_entries_used
7748 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
7750 static boolean elf_gc_smash_unused_vtentry_relocs
7751 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
7753 /* The mark phase of garbage collection. For a given section, mark
7754 it and any sections in this section's group, and all the sections
7755 which define symbols to which it refers. */
7758 elf_gc_mark (info
, sec
, gc_mark_hook
)
7759 struct bfd_link_info
*info
;
7761 asection
* (*gc_mark_hook
)
7762 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7763 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
7766 asection
*group_sec
;
7770 /* Mark all the sections in the group. */
7771 group_sec
= elf_section_data (sec
)->next_in_group
;
7772 if (group_sec
&& !group_sec
->gc_mark
)
7773 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
7776 /* Look through the section relocs. */
7778 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
7780 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
7781 Elf_Internal_Shdr
*symtab_hdr
;
7782 Elf_Internal_Shdr
*shndx_hdr
;
7783 struct elf_link_hash_entry
**sym_hashes
;
7786 Elf_External_Sym
*locsyms
, *freesyms
= NULL
;
7787 Elf_External_Sym_Shndx
*locsym_shndx
;
7788 bfd
*input_bfd
= sec
->owner
;
7789 struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
7791 /* GCFIXME: how to arrange so that relocs and symbols are not
7792 reread continually? */
7794 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
7795 sym_hashes
= elf_sym_hashes (input_bfd
);
7797 /* Read the local symbols. */
7798 if (elf_bad_symtab (input_bfd
))
7800 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
7804 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
7806 if (symtab_hdr
->contents
)
7807 locsyms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
7808 else if (nlocsyms
== 0)
7812 bfd_size_type amt
= nlocsyms
* sizeof (Elf_External_Sym
);
7813 locsyms
= freesyms
= bfd_malloc (amt
);
7814 if (freesyms
== NULL
7815 || bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
7816 || bfd_bread (locsyms
, amt
, input_bfd
) != amt
)
7823 shndx_hdr
= &elf_tdata (input_bfd
)->symtab_shndx_hdr
;
7824 locsym_shndx
= NULL
;
7825 if (shndx_hdr
->sh_size
!= 0 && nlocsyms
!= 0)
7827 bfd_size_type amt
= nlocsyms
* sizeof (Elf_External_Sym_Shndx
);
7828 locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
7829 if (bfd_seek (input_bfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
7830 || bfd_bread (locsym_shndx
, amt
, input_bfd
) != amt
)
7834 /* Read the relocations. */
7835 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
7836 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
7837 info
->keep_memory
));
7838 if (relstart
== NULL
)
7843 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7845 for (rel
= relstart
; rel
< relend
; rel
++)
7847 unsigned long r_symndx
;
7849 struct elf_link_hash_entry
*h
;
7851 Elf_External_Sym_Shndx
*locshndx
;
7853 r_symndx
= ELF_R_SYM (rel
->r_info
);
7857 if (elf_bad_symtab (sec
->owner
))
7859 locshndx
= locsym_shndx
+ (locsym_shndx
? r_symndx
: 0);
7860 elf_swap_symbol_in (input_bfd
,
7861 (const PTR
) (locsyms
+ r_symndx
),
7862 (const PTR
) locshndx
,
7864 if (ELF_ST_BIND (s
.st_info
) == STB_LOCAL
)
7865 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &s
);
7868 h
= sym_hashes
[r_symndx
- extsymoff
];
7869 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
7872 else if (r_symndx
>= nlocsyms
)
7874 h
= sym_hashes
[r_symndx
- extsymoff
];
7875 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
7879 locshndx
= locsym_shndx
+ (locsym_shndx
? r_symndx
: 0);
7880 elf_swap_symbol_in (input_bfd
,
7881 (const PTR
) (locsyms
+ r_symndx
),
7882 (const PTR
) locshndx
,
7884 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &s
);
7887 if (rsec
&& !rsec
->gc_mark
)
7889 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
7891 else if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
7900 if (!info
->keep_memory
)
7910 /* The sweep phase of garbage collection. Remove all garbage sections. */
7913 elf_gc_sweep (info
, gc_sweep_hook
)
7914 struct bfd_link_info
*info
;
7915 boolean (*gc_sweep_hook
)
7916 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
7917 const Elf_Internal_Rela
*relocs
));
7921 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7925 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
7928 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7930 /* Keep special sections. Keep .debug sections. */
7931 if ((o
->flags
& SEC_LINKER_CREATED
)
7932 || (o
->flags
& SEC_DEBUGGING
))
7938 /* Skip sweeping sections already excluded. */
7939 if (o
->flags
& SEC_EXCLUDE
)
7942 /* Since this is early in the link process, it is simple
7943 to remove a section from the output. */
7944 o
->flags
|= SEC_EXCLUDE
;
7946 /* But we also have to update some of the relocation
7947 info we collected before. */
7949 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
7951 Elf_Internal_Rela
*internal_relocs
;
7954 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
7955 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
7956 if (internal_relocs
== NULL
)
7959 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
7961 if (!info
->keep_memory
)
7962 free (internal_relocs
);
7970 /* Remove the symbols that were in the swept sections from the dynamic
7971 symbol table. GCFIXME: Anyone know how to get them out of the
7972 static symbol table as well? */
7976 elf_link_hash_traverse (elf_hash_table (info
),
7977 elf_gc_sweep_symbol
,
7980 elf_hash_table (info
)->dynsymcount
= i
;
7986 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7989 elf_gc_sweep_symbol (h
, idxptr
)
7990 struct elf_link_hash_entry
*h
;
7993 int *idx
= (int *) idxptr
;
7995 if (h
->root
.type
== bfd_link_hash_warning
)
7996 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7998 if (h
->dynindx
!= -1
7999 && ((h
->root
.type
!= bfd_link_hash_defined
8000 && h
->root
.type
!= bfd_link_hash_defweak
)
8001 || h
->root
.u
.def
.section
->gc_mark
))
8002 h
->dynindx
= (*idx
)++;
8007 /* Propogate collected vtable information. This is called through
8008 elf_link_hash_traverse. */
8011 elf_gc_propagate_vtable_entries_used (h
, okp
)
8012 struct elf_link_hash_entry
*h
;
8015 if (h
->root
.type
== bfd_link_hash_warning
)
8016 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8018 /* Those that are not vtables. */
8019 if (h
->vtable_parent
== NULL
)
8022 /* Those vtables that do not have parents, we cannot merge. */
8023 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
8026 /* If we've already been done, exit. */
8027 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
8030 /* Make sure the parent's table is up to date. */
8031 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
8033 if (h
->vtable_entries_used
== NULL
)
8035 /* None of this table's entries were referenced. Re-use the
8037 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
8038 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
8045 /* Or the parent's entries into ours. */
8046 cu
= h
->vtable_entries_used
;
8048 pu
= h
->vtable_parent
->vtable_entries_used
;
8051 asection
*sec
= h
->root
.u
.def
.section
;
8052 struct elf_backend_data
*bed
= get_elf_backend_data (sec
->owner
);
8053 int file_align
= bed
->s
->file_align
;
8055 n
= h
->vtable_parent
->vtable_entries_size
/ file_align
;
8070 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
8071 struct elf_link_hash_entry
*h
;
8075 bfd_vma hstart
, hend
;
8076 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8077 struct elf_backend_data
*bed
;
8080 if (h
->root
.type
== bfd_link_hash_warning
)
8081 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8083 /* Take care of both those symbols that do not describe vtables as
8084 well as those that are not loaded. */
8085 if (h
->vtable_parent
== NULL
)
8088 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8089 || h
->root
.type
== bfd_link_hash_defweak
);
8091 sec
= h
->root
.u
.def
.section
;
8092 hstart
= h
->root
.u
.def
.value
;
8093 hend
= hstart
+ h
->size
;
8095 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
8096 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, true));
8098 return *(boolean
*) okp
= false;
8099 bed
= get_elf_backend_data (sec
->owner
);
8100 file_align
= bed
->s
->file_align
;
8102 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8104 for (rel
= relstart
; rel
< relend
; ++rel
)
8105 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8107 /* If the entry is in use, do nothing. */
8108 if (h
->vtable_entries_used
8109 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
8111 bfd_vma entry
= (rel
->r_offset
- hstart
) / file_align
;
8112 if (h
->vtable_entries_used
[entry
])
8115 /* Otherwise, kill it. */
8116 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8122 /* Do mark and sweep of unused sections. */
8125 elf_gc_sections (abfd
, info
)
8127 struct bfd_link_info
*info
;
8131 asection
* (*gc_mark_hook
)
8132 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8133 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
8135 if (!get_elf_backend_data (abfd
)->can_gc_sections
8136 || info
->relocateable
|| info
->emitrelocations
8137 || elf_hash_table (info
)->dynamic_sections_created
)
8140 /* Apply transitive closure to the vtable entry usage info. */
8141 elf_link_hash_traverse (elf_hash_table (info
),
8142 elf_gc_propagate_vtable_entries_used
,
8147 /* Kill the vtable relocations that were not used. */
8148 elf_link_hash_traverse (elf_hash_table (info
),
8149 elf_gc_smash_unused_vtentry_relocs
,
8154 /* Grovel through relocs to find out who stays ... */
8156 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8157 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8161 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8164 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8166 if (o
->flags
& SEC_KEEP
)
8167 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
8172 /* ... and mark SEC_EXCLUDE for those that go. */
8173 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8179 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
8182 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
8185 struct elf_link_hash_entry
*h
;
8188 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8189 struct elf_link_hash_entry
**search
, *child
;
8190 bfd_size_type extsymcount
;
8192 /* The sh_info field of the symtab header tells us where the
8193 external symbols start. We don't care about the local symbols at
8195 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
8196 if (!elf_bad_symtab (abfd
))
8197 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8199 sym_hashes
= elf_sym_hashes (abfd
);
8200 sym_hashes_end
= sym_hashes
+ extsymcount
;
8202 /* Hunt down the child symbol, which is in this section at the same
8203 offset as the relocation. */
8204 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8206 if ((child
= *search
) != NULL
8207 && (child
->root
.type
== bfd_link_hash_defined
8208 || child
->root
.type
== bfd_link_hash_defweak
)
8209 && child
->root
.u
.def
.section
== sec
8210 && child
->root
.u
.def
.value
== offset
)
8214 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
8215 bfd_archive_filename (abfd
), sec
->name
,
8216 (unsigned long) offset
);
8217 bfd_set_error (bfd_error_invalid_operation
);
8223 /* This *should* only be the absolute section. It could potentially
8224 be that someone has defined a non-global vtable though, which
8225 would be bad. It isn't worth paging in the local symbols to be
8226 sure though; that case should simply be handled by the assembler. */
8228 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
8231 child
->vtable_parent
= h
;
8236 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
8239 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
8240 bfd
*abfd ATTRIBUTE_UNUSED
;
8241 asection
*sec ATTRIBUTE_UNUSED
;
8242 struct elf_link_hash_entry
*h
;
8245 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8246 int file_align
= bed
->s
->file_align
;
8248 if (addend
>= h
->vtable_entries_size
)
8251 boolean
*ptr
= h
->vtable_entries_used
;
8253 /* While the symbol is undefined, we have to be prepared to handle
8255 if (h
->root
.type
== bfd_link_hash_undefined
)
8262 /* Oops! We've got a reference past the defined end of
8263 the table. This is probably a bug -- shall we warn? */
8268 /* Allocate one extra entry for use as a "done" flag for the
8269 consolidation pass. */
8270 bytes
= (size
/ file_align
+ 1) * sizeof (boolean
);
8274 ptr
= bfd_realloc (ptr
- 1, (bfd_size_type
) bytes
);
8280 oldbytes
= ((h
->vtable_entries_size
/ file_align
+ 1)
8281 * sizeof (boolean
));
8282 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8286 ptr
= bfd_zmalloc ((bfd_size_type
) bytes
);
8291 /* And arrange for that done flag to be at index -1. */
8292 h
->vtable_entries_used
= ptr
+ 1;
8293 h
->vtable_entries_size
= size
;
8296 h
->vtable_entries_used
[addend
/ file_align
] = true;
8301 /* And an accompanying bit to work out final got entry offsets once
8302 we're done. Should be called from final_link. */
8305 elf_gc_common_finalize_got_offsets (abfd
, info
)
8307 struct bfd_link_info
*info
;
8310 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8313 /* The GOT offset is relative to the .got section, but the GOT header is
8314 put into the .got.plt section, if the backend uses it. */
8315 if (bed
->want_got_plt
)
8318 gotoff
= bed
->got_header_size
;
8320 /* Do the local .got entries first. */
8321 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
8323 bfd_signed_vma
*local_got
;
8324 bfd_size_type j
, locsymcount
;
8325 Elf_Internal_Shdr
*symtab_hdr
;
8327 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
8330 local_got
= elf_local_got_refcounts (i
);
8334 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
8335 if (elf_bad_symtab (i
))
8336 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8338 locsymcount
= symtab_hdr
->sh_info
;
8340 for (j
= 0; j
< locsymcount
; ++j
)
8342 if (local_got
[j
] > 0)
8344 local_got
[j
] = gotoff
;
8345 gotoff
+= ARCH_SIZE
/ 8;
8348 local_got
[j
] = (bfd_vma
) -1;
8352 /* Then the global .got entries. .plt refcounts are handled by
8353 adjust_dynamic_symbol */
8354 elf_link_hash_traverse (elf_hash_table (info
),
8355 elf_gc_allocate_got_offsets
,
8360 /* We need a special top-level link routine to convert got reference counts
8361 to real got offsets. */
8364 elf_gc_allocate_got_offsets (h
, offarg
)
8365 struct elf_link_hash_entry
*h
;
8368 bfd_vma
*off
= (bfd_vma
*) offarg
;
8370 if (h
->root
.type
== bfd_link_hash_warning
)
8371 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8373 if (h
->got
.refcount
> 0)
8375 h
->got
.offset
= off
[0];
8376 off
[0] += ARCH_SIZE
/ 8;
8379 h
->got
.offset
= (bfd_vma
) -1;
8384 /* Many folk need no more in the way of final link than this, once
8385 got entry reference counting is enabled. */
8388 elf_gc_common_final_link (abfd
, info
)
8390 struct bfd_link_info
*info
;
8392 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
8395 /* Invoke the regular ELF backend linker to do all the work. */
8396 return elf_bfd_final_link (abfd
, info
);
8399 /* This function will be called though elf_link_hash_traverse to store
8400 all hash value of the exported symbols in an array. */
8403 elf_collect_hash_codes (h
, data
)
8404 struct elf_link_hash_entry
*h
;
8407 unsigned long **valuep
= (unsigned long **) data
;
8413 if (h
->root
.type
== bfd_link_hash_warning
)
8414 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8416 /* Ignore indirect symbols. These are added by the versioning code. */
8417 if (h
->dynindx
== -1)
8420 name
= h
->root
.root
.string
;
8421 p
= strchr (name
, ELF_VER_CHR
);
8424 alc
= bfd_malloc ((bfd_size_type
) (p
- name
+ 1));
8425 memcpy (alc
, name
, (size_t) (p
- name
));
8426 alc
[p
- name
] = '\0';
8430 /* Compute the hash value. */
8431 ha
= bfd_elf_hash (name
);
8433 /* Store the found hash value in the array given as the argument. */
8436 /* And store it in the struct so that we can put it in the hash table
8438 h
->elf_hash_value
= ha
;
8447 elf_reloc_symbol_deleted_p (offset
, cookie
)
8451 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
8453 if (rcookie
->bad_symtab
)
8454 rcookie
->rel
= rcookie
->rels
;
8456 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
8458 unsigned long r_symndx
= ELF_R_SYM (rcookie
->rel
->r_info
);
8459 Elf_Internal_Sym isym
;
8461 if (! rcookie
->bad_symtab
)
8462 if (rcookie
->rel
->r_offset
> offset
)
8464 if (rcookie
->rel
->r_offset
!= offset
)
8467 if (rcookie
->locsyms
&& r_symndx
< rcookie
->locsymcount
)
8469 Elf_External_Sym
*lsym
;
8470 Elf_External_Sym_Shndx
*lshndx
;
8472 lsym
= (Elf_External_Sym
*) rcookie
->locsyms
+ r_symndx
;
8473 lshndx
= (Elf_External_Sym_Shndx
*) rcookie
->locsym_shndx
;
8476 elf_swap_symbol_in (rcookie
->abfd
, (const PTR
) lsym
,
8477 (const PTR
) lshndx
, &isym
);
8480 if (r_symndx
>= rcookie
->locsymcount
8481 || (rcookie
->locsyms
8482 && ELF_ST_BIND (isym
.st_info
) != STB_LOCAL
))
8484 struct elf_link_hash_entry
*h
;
8486 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
8488 while (h
->root
.type
== bfd_link_hash_indirect
8489 || h
->root
.type
== bfd_link_hash_warning
)
8490 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8492 if ((h
->root
.type
== bfd_link_hash_defined
8493 || h
->root
.type
== bfd_link_hash_defweak
)
8494 && elf_discarded_section (h
->root
.u
.def
.section
))
8499 else if (rcookie
->locsyms
)
8501 /* It's not a relocation against a global symbol,
8502 but it could be a relocation against a local
8503 symbol for a discarded section. */
8506 /* Need to: get the symbol; get the section. */
8507 if (isym
.st_shndx
< SHN_LORESERVE
|| isym
.st_shndx
> SHN_HIRESERVE
)
8509 isec
= section_from_elf_index (rcookie
->abfd
, isym
.st_shndx
);
8510 if (isec
!= NULL
&& elf_discarded_section (isec
))
8519 /* Discard unneeded references to discarded sections.
8520 Returns true if any section's size was changed. */
8521 /* This function assumes that the relocations are in sorted order,
8522 which is true for all known assemblers. */
8525 elf_bfd_discard_info (output_bfd
, info
)
8527 struct bfd_link_info
*info
;
8529 struct elf_reloc_cookie cookie
;
8530 asection
*stab
, *eh
, *ehdr
;
8531 Elf_Internal_Shdr
*symtab_hdr
;
8532 Elf_Internal_Shdr
*shndx_hdr
;
8533 Elf_External_Sym
*freesyms
;
8534 struct elf_backend_data
*bed
;
8536 boolean ret
= false;
8537 boolean strip
= info
->strip
== strip_all
|| info
->strip
== strip_debugger
;
8539 if (info
->relocateable
8540 || info
->traditional_format
8541 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
8542 || ! is_elf_hash_table (info
))
8546 if (elf_hash_table (info
)->dynobj
!= NULL
)
8547 ehdr
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
8550 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
8552 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
8555 bed
= get_elf_backend_data (abfd
);
8557 if ((abfd
->flags
& DYNAMIC
) != 0)
8563 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
8564 if (eh
&& (eh
->_raw_size
== 0
8565 || bfd_is_abs_section (eh
->output_section
)))
8572 stab
= bfd_get_section_by_name (abfd
, ".stab");
8573 if (stab
&& (stab
->_raw_size
== 0
8574 || bfd_is_abs_section (stab
->output_section
)))
8578 || elf_section_data(stab
)->sec_info_type
!= ELF_INFO_TYPE_STABS
)
8580 && (strip
|| ! bed
->elf_backend_discard_info
))
8583 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8584 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8587 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
8588 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
8589 if (cookie
.bad_symtab
)
8591 cookie
.locsymcount
=
8592 symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8593 cookie
.extsymoff
= 0;
8597 cookie
.locsymcount
= symtab_hdr
->sh_info
;
8598 cookie
.extsymoff
= symtab_hdr
->sh_info
;
8602 if (symtab_hdr
->contents
)
8603 cookie
.locsyms
= (void *) symtab_hdr
->contents
;
8604 else if (cookie
.locsymcount
== 0)
8605 cookie
.locsyms
= NULL
;
8608 bfd_size_type amt
= cookie
.locsymcount
* sizeof (Elf_External_Sym
);
8609 cookie
.locsyms
= bfd_malloc (amt
);
8610 if (cookie
.locsyms
== NULL
)
8612 freesyms
= cookie
.locsyms
;
8613 if (bfd_seek (abfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
8614 || bfd_bread (cookie
.locsyms
, amt
, abfd
) != amt
)
8617 free (cookie
.locsyms
);
8622 cookie
.locsym_shndx
= NULL
;
8623 if (shndx_hdr
->sh_size
!= 0 && cookie
.locsymcount
!= 0)
8626 amt
= cookie
.locsymcount
* sizeof (Elf_External_Sym_Shndx
);
8627 cookie
.locsym_shndx
= bfd_malloc (amt
);
8628 if (cookie
.locsym_shndx
== NULL
)
8629 goto error_ret_free_loc
;
8630 if (bfd_seek (abfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8631 || bfd_bread (cookie
.locsym_shndx
, amt
, abfd
) != amt
)
8633 free (cookie
.locsym_shndx
);
8634 goto error_ret_free_loc
;
8640 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8641 (abfd
, stab
, (PTR
) NULL
,
8642 (Elf_Internal_Rela
*) NULL
,
8643 info
->keep_memory
));
8646 cookie
.rel
= cookie
.rels
;
8648 cookie
.rels
+ stab
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8649 if (_bfd_discard_section_stabs (abfd
, stab
,
8650 elf_section_data (stab
)->sec_info
,
8651 elf_reloc_symbol_deleted_p
,
8654 if (! info
->keep_memory
)
8663 cookie
.relend
= NULL
;
8664 if (eh
->reloc_count
)
8665 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8666 (abfd
, eh
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
8667 info
->keep_memory
));
8670 cookie
.rel
= cookie
.rels
;
8672 cookie
.rels
+ eh
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8674 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
, ehdr
,
8675 elf_reloc_symbol_deleted_p
,
8678 if (! info
->keep_memory
)
8682 if (bed
->elf_backend_discard_info
)
8684 if (bed
->elf_backend_discard_info (abfd
, &cookie
, info
))
8688 if (cookie
.locsym_shndx
!= NULL
)
8689 free (cookie
.locsym_shndx
);
8691 if (freesyms
!= NULL
)
8695 if (ehdr
&& _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
, ehdr
))
8701 elf_section_ignore_discarded_relocs (sec
)
8704 struct elf_backend_data
*bed
;
8706 switch (elf_section_data (sec
)->sec_info_type
)
8708 case ELF_INFO_TYPE_STABS
:
8709 case ELF_INFO_TYPE_EH_FRAME
:
8715 bed
= get_elf_backend_data (sec
->owner
);
8716 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
8717 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))