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
;
964 /* If this symbol has a version, and it is the default version, we
965 create an indirect symbol from the default name to the fully
966 decorated name. This will cause external references which do not
967 specify a version to be bound to this version of the symbol. */
968 p
= strchr (name
, ELF_VER_CHR
);
969 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
974 /* We are overridden by an old defition. We need to check if we
975 need to crreate the indirect symbol from the default name. */
976 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, true,
978 BFD_ASSERT (hi
!= NULL
);
981 while (hi
->root
.type
== bfd_link_hash_indirect
982 || hi
->root
.type
== bfd_link_hash_warning
)
984 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
990 bed
= get_elf_backend_data (abfd
);
991 collect
= bed
->collect
;
992 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
994 shortname
= bfd_hash_allocate (&info
->hash
->table
,
995 (size_t) (p
- name
+ 1));
996 if (shortname
== NULL
)
998 strncpy (shortname
, name
, (size_t) (p
- name
));
999 shortname
[p
- name
] = '\0';
1001 /* We are going to create a new symbol. Merge it with any existing
1002 symbol with this name. For the purposes of the merge, act as
1003 though we were defining the symbol we just defined, although we
1004 actually going to define an indirect symbol. */
1005 type_change_ok
= false;
1006 size_change_ok
= false;
1007 if (! elf_merge_symbol (abfd
, info
, shortname
, sym
, sec
, value
,
1008 &hi
, &override
, &type_change_ok
,
1009 &size_change_ok
, dt_needed
))
1014 if (! (_bfd_generic_link_add_one_symbol
1015 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1016 (bfd_vma
) 0, name
, false, collect
,
1017 (struct bfd_link_hash_entry
**) &hi
)))
1022 /* In this case the symbol named SHORTNAME is overriding the
1023 indirect symbol we want to add. We were planning on making
1024 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1025 is the name without a version. NAME is the fully versioned
1026 name, and it is the default version.
1028 Overriding means that we already saw a definition for the
1029 symbol SHORTNAME in a regular object, and it is overriding
1030 the symbol defined in the dynamic object.
1032 When this happens, we actually want to change NAME, the
1033 symbol we just added, to refer to SHORTNAME. This will cause
1034 references to NAME in the shared object to become references
1035 to SHORTNAME in the regular object. This is what we expect
1036 when we override a function in a shared object: that the
1037 references in the shared object will be mapped to the
1038 definition in the regular object. */
1040 while (hi
->root
.type
== bfd_link_hash_indirect
1041 || hi
->root
.type
== bfd_link_hash_warning
)
1042 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1044 h
->root
.type
= bfd_link_hash_indirect
;
1045 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1046 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1048 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1049 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1050 if (hi
->elf_link_hash_flags
1051 & (ELF_LINK_HASH_REF_REGULAR
1052 | ELF_LINK_HASH_DEF_REGULAR
))
1054 if (! _bfd_elf_link_record_dynamic_symbol (info
, hi
))
1059 /* Now set HI to H, so that the following code will set the
1060 other fields correctly. */
1064 /* If there is a duplicate definition somewhere, then HI may not
1065 point to an indirect symbol. We will have reported an error to
1066 the user in that case. */
1068 if (hi
->root
.type
== bfd_link_hash_indirect
)
1070 struct elf_link_hash_entry
*ht
;
1072 /* If the symbol became indirect, then we assume that we have
1073 not seen a definition before. */
1074 BFD_ASSERT ((hi
->elf_link_hash_flags
1075 & (ELF_LINK_HASH_DEF_DYNAMIC
1076 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1078 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1079 (*bed
->elf_backend_copy_indirect_symbol
) (ht
, hi
);
1081 /* See if the new flags lead us to realize that the symbol must
1088 || ((hi
->elf_link_hash_flags
1089 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1094 if ((hi
->elf_link_hash_flags
1095 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1101 /* We also need to define an indirection from the nondefault version
1104 shortname
= bfd_hash_allocate (&info
->hash
->table
, strlen (name
));
1105 if (shortname
== NULL
)
1107 strncpy (shortname
, name
, (size_t) (p
- name
));
1108 strcpy (shortname
+ (p
- name
), p
+ 1);
1110 /* Once again, merge with any existing symbol. */
1111 type_change_ok
= false;
1112 size_change_ok
= false;
1113 if (! elf_merge_symbol (abfd
, info
, shortname
, sym
, sec
, value
,
1114 &hi
, &override
, &type_change_ok
,
1115 &size_change_ok
, dt_needed
))
1120 /* Here SHORTNAME is a versioned name, so we don't expect to see
1121 the type of override we do in the case above. */
1122 (*_bfd_error_handler
)
1123 (_("%s: warning: unexpected redefinition of `%s'"),
1124 bfd_archive_filename (abfd
), shortname
);
1128 if (! (_bfd_generic_link_add_one_symbol
1129 (info
, abfd
, shortname
, BSF_INDIRECT
,
1130 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1131 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1134 /* If there is a duplicate definition somewhere, then HI may not
1135 point to an indirect symbol. We will have reported an error
1136 to the user in that case. */
1138 if (hi
->root
.type
== bfd_link_hash_indirect
)
1140 /* If the symbol became indirect, then we assume that we have
1141 not seen a definition before. */
1142 BFD_ASSERT ((hi
->elf_link_hash_flags
1143 & (ELF_LINK_HASH_DEF_DYNAMIC
1144 | ELF_LINK_HASH_DEF_REGULAR
)) == 0);
1146 (*bed
->elf_backend_copy_indirect_symbol
) (h
, hi
);
1148 /* See if the new flags lead us to realize that the symbol
1155 || ((hi
->elf_link_hash_flags
1156 & ELF_LINK_HASH_REF_DYNAMIC
) != 0))
1161 if ((hi
->elf_link_hash_flags
1162 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1172 /* Add symbols from an ELF object file to the linker hash table. */
1175 elf_link_add_object_symbols (abfd
, info
)
1177 struct bfd_link_info
*info
;
1179 boolean (*add_symbol_hook
) PARAMS ((bfd
*, struct bfd_link_info
*,
1180 const Elf_Internal_Sym
*,
1181 const char **, flagword
*,
1182 asection
**, bfd_vma
*));
1183 boolean (*check_relocs
) PARAMS ((bfd
*, struct bfd_link_info
*,
1184 asection
*, const Elf_Internal_Rela
*));
1186 Elf_Internal_Shdr
*hdr
;
1187 Elf_Internal_Shdr
*shndx_hdr
;
1188 bfd_size_type symcount
;
1189 bfd_size_type extsymcount
;
1190 bfd_size_type extsymoff
;
1191 Elf_External_Sym
*buf
= NULL
;
1192 Elf_External_Sym_Shndx
*shndx_buf
= NULL
;
1193 Elf_External_Sym_Shndx
*shndx
;
1194 struct elf_link_hash_entry
**sym_hash
;
1196 Elf_External_Versym
*extversym
= NULL
;
1197 Elf_External_Versym
*ever
;
1198 Elf_External_Dyn
*dynbuf
= NULL
;
1199 struct elf_link_hash_entry
*weaks
;
1200 Elf_External_Sym
*esym
;
1201 Elf_External_Sym
*esymend
;
1202 struct elf_backend_data
*bed
;
1204 struct elf_link_hash_table
* hash_table
;
1208 hash_table
= elf_hash_table (info
);
1210 bed
= get_elf_backend_data (abfd
);
1211 add_symbol_hook
= bed
->elf_add_symbol_hook
;
1212 collect
= bed
->collect
;
1214 if ((abfd
->flags
& DYNAMIC
) == 0)
1220 /* You can't use -r against a dynamic object. Also, there's no
1221 hope of using a dynamic object which does not exactly match
1222 the format of the output file. */
1223 if (info
->relocateable
|| info
->hash
->creator
!= abfd
->xvec
)
1225 bfd_set_error (bfd_error_invalid_operation
);
1230 /* As a GNU extension, any input sections which are named
1231 .gnu.warning.SYMBOL are treated as warning symbols for the given
1232 symbol. This differs from .gnu.warning sections, which generate
1233 warnings when they are included in an output file. */
1238 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
1242 name
= bfd_get_section_name (abfd
, s
);
1243 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1248 name
+= sizeof ".gnu.warning." - 1;
1250 /* If this is a shared object, then look up the symbol
1251 in the hash table. If it is there, and it is already
1252 been defined, then we will not be using the entry
1253 from this shared object, so we don't need to warn.
1254 FIXME: If we see the definition in a regular object
1255 later on, we will warn, but we shouldn't. The only
1256 fix is to keep track of what warnings we are supposed
1257 to emit, and then handle them all at the end of the
1259 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
1261 struct elf_link_hash_entry
*h
;
1263 h
= elf_link_hash_lookup (hash_table
, name
,
1264 false, false, true);
1266 /* FIXME: What about bfd_link_hash_common? */
1268 && (h
->root
.type
== bfd_link_hash_defined
1269 || h
->root
.type
== bfd_link_hash_defweak
))
1271 /* We don't want to issue this warning. Clobber
1272 the section size so that the warning does not
1273 get copied into the output file. */
1279 sz
= bfd_section_size (abfd
, s
);
1280 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
1284 if (! bfd_get_section_contents (abfd
, s
, msg
, (file_ptr
) 0, sz
))
1289 if (! (_bfd_generic_link_add_one_symbol
1290 (info
, abfd
, name
, BSF_WARNING
, s
, (bfd_vma
) 0, msg
,
1291 false, collect
, (struct bfd_link_hash_entry
**) NULL
)))
1294 if (! info
->relocateable
)
1296 /* Clobber the section size so that the warning does
1297 not get copied into the output file. */
1304 /* If this is a dynamic object, we always link against the .dynsym
1305 symbol table, not the .symtab symbol table. The dynamic linker
1306 will only see the .dynsym symbol table, so there is no reason to
1307 look at .symtab for a dynamic object. */
1309 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
1311 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1312 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
1316 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1322 /* Read in any version definitions. */
1324 if (! _bfd_elf_slurp_version_tables (abfd
))
1327 /* Read in the symbol versions, but don't bother to convert them
1328 to internal format. */
1329 if (elf_dynversym (abfd
) != 0)
1331 Elf_Internal_Shdr
*versymhdr
;
1333 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
1334 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
1335 if (extversym
== NULL
)
1337 amt
= versymhdr
->sh_size
;
1338 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
1339 || bfd_bread ((PTR
) extversym
, amt
, abfd
) != amt
)
1344 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
1346 /* The sh_info field of the symtab header tells us where the
1347 external symbols start. We don't care about the local symbols at
1349 if (elf_bad_symtab (abfd
))
1351 extsymcount
= symcount
;
1356 extsymcount
= symcount
- hdr
->sh_info
;
1357 extsymoff
= hdr
->sh_info
;
1360 amt
= extsymcount
* sizeof (Elf_External_Sym
);
1361 buf
= (Elf_External_Sym
*) bfd_malloc (amt
);
1362 if (buf
== NULL
&& extsymcount
!= 0)
1365 if (shndx_hdr
!= NULL
&& shndx_hdr
->sh_size
!= 0)
1367 amt
= extsymcount
* sizeof (Elf_External_Sym_Shndx
);
1368 shndx_buf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
1369 if (shndx_buf
== NULL
&& extsymcount
!= 0)
1373 /* We store a pointer to the hash table entry for each external
1375 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
1376 sym_hash
= (struct elf_link_hash_entry
**) bfd_alloc (abfd
, amt
);
1377 if (sym_hash
== NULL
)
1379 elf_sym_hashes (abfd
) = sym_hash
;
1385 /* If we are creating a shared library, create all the dynamic
1386 sections immediately. We need to attach them to something,
1387 so we attach them to this BFD, provided it is the right
1388 format. FIXME: If there are no input BFD's of the same
1389 format as the output, we can't make a shared library. */
1391 && is_elf_hash_table (info
)
1392 && ! hash_table
->dynamic_sections_created
1393 && abfd
->xvec
== info
->hash
->creator
)
1395 if (! elf_link_create_dynamic_sections (abfd
, info
))
1399 else if (! is_elf_hash_table (info
))
1406 bfd_size_type oldsize
;
1407 bfd_size_type strindex
;
1409 /* Find the name to use in a DT_NEEDED entry that refers to this
1410 object. If the object has a DT_SONAME entry, we use it.
1411 Otherwise, if the generic linker stuck something in
1412 elf_dt_name, we use that. Otherwise, we just use the file
1413 name. If the generic linker put a null string into
1414 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1415 there is a DT_SONAME entry. */
1417 name
= bfd_get_filename (abfd
);
1418 if (elf_dt_name (abfd
) != NULL
)
1420 name
= elf_dt_name (abfd
);
1423 if (elf_dt_soname (abfd
) != NULL
)
1429 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1432 Elf_External_Dyn
*extdyn
;
1433 Elf_External_Dyn
*extdynend
;
1435 unsigned long shlink
;
1439 dynbuf
= (Elf_External_Dyn
*) bfd_malloc (s
->_raw_size
);
1443 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
1444 (file_ptr
) 0, s
->_raw_size
))
1447 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1450 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1453 /* The shared libraries distributed with hpux11 have a bogus
1454 sh_link field for the ".dynamic" section. This code detects
1455 when SHLINK refers to a section that is not a string table
1456 and tries to find the string table for the ".dynsym" section
1458 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[shlink
];
1459 if (shdr
->sh_type
!= SHT_STRTAB
)
1461 asection
*ds
= bfd_get_section_by_name (abfd
, ".dynsym");
1462 int elfdsec
= _bfd_elf_section_from_bfd_section (abfd
, ds
);
1465 shlink
= elf_elfsections (abfd
)[elfdsec
]->sh_link
;
1470 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
1473 for (; extdyn
< extdynend
; extdyn
++)
1475 Elf_Internal_Dyn dyn
;
1477 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
1478 if (dyn
.d_tag
== DT_SONAME
)
1480 unsigned int tagv
= dyn
.d_un
.d_val
;
1481 name
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1485 if (dyn
.d_tag
== DT_NEEDED
)
1487 struct bfd_link_needed_list
*n
, **pn
;
1489 unsigned int tagv
= dyn
.d_un
.d_val
;
1491 amt
= sizeof (struct bfd_link_needed_list
);
1492 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1493 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1494 if (n
== NULL
|| fnm
== NULL
)
1496 anm
= bfd_alloc (abfd
, (bfd_size_type
) strlen (fnm
) + 1);
1503 for (pn
= & hash_table
->needed
;
1509 if (dyn
.d_tag
== DT_RUNPATH
)
1511 struct bfd_link_needed_list
*n
, **pn
;
1513 unsigned int tagv
= dyn
.d_un
.d_val
;
1515 /* When we see DT_RPATH before DT_RUNPATH, we have
1516 to clear runpath. Do _NOT_ bfd_release, as that
1517 frees all more recently bfd_alloc'd blocks as
1519 if (rpath
&& hash_table
->runpath
)
1520 hash_table
->runpath
= NULL
;
1522 amt
= sizeof (struct bfd_link_needed_list
);
1523 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1524 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1525 if (n
== NULL
|| fnm
== NULL
)
1527 anm
= bfd_alloc (abfd
, (bfd_size_type
) strlen (fnm
) + 1);
1534 for (pn
= & hash_table
->runpath
;
1542 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1543 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
1545 struct bfd_link_needed_list
*n
, **pn
;
1547 unsigned int tagv
= dyn
.d_un
.d_val
;
1549 amt
= sizeof (struct bfd_link_needed_list
);
1550 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1551 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1552 if (n
== NULL
|| fnm
== NULL
)
1554 anm
= bfd_alloc (abfd
, (bfd_size_type
) strlen (fnm
) + 1);
1561 for (pn
= & hash_table
->runpath
;
1574 /* We do not want to include any of the sections in a dynamic
1575 object in the output file. We hack by simply clobbering the
1576 list of sections in the BFD. This could be handled more
1577 cleanly by, say, a new section flag; the existing
1578 SEC_NEVER_LOAD flag is not the one we want, because that one
1579 still implies that the section takes up space in the output
1581 bfd_section_list_clear (abfd
);
1583 /* If this is the first dynamic object found in the link, create
1584 the special sections required for dynamic linking. */
1585 if (! hash_table
->dynamic_sections_created
)
1586 if (! elf_link_create_dynamic_sections (abfd
, info
))
1591 /* Add a DT_NEEDED entry for this dynamic object. */
1592 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
1593 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, name
, false);
1594 if (strindex
== (bfd_size_type
) -1)
1597 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
1600 Elf_External_Dyn
*dyncon
, *dynconend
;
1602 /* The hash table size did not change, which means that
1603 the dynamic object name was already entered. If we
1604 have already included this dynamic object in the
1605 link, just ignore it. There is no reason to include
1606 a particular dynamic object more than once. */
1607 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
1608 BFD_ASSERT (sdyn
!= NULL
);
1610 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1611 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1613 for (; dyncon
< dynconend
; dyncon
++)
1615 Elf_Internal_Dyn dyn
;
1617 elf_swap_dyn_in (hash_table
->dynobj
, dyncon
, & dyn
);
1618 if (dyn
.d_tag
== DT_NEEDED
1619 && dyn
.d_un
.d_val
== strindex
)
1623 if (extversym
!= NULL
)
1625 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
1631 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
1635 /* Save the SONAME, if there is one, because sometimes the
1636 linker emulation code will need to know it. */
1638 name
= basename (bfd_get_filename (abfd
));
1639 elf_dt_name (abfd
) = name
;
1642 pos
= hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
);
1643 amt
= extsymcount
* sizeof (Elf_External_Sym
);
1644 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1645 || bfd_bread ((PTR
) buf
, amt
, abfd
) != amt
)
1648 if (shndx_hdr
!= NULL
&& shndx_hdr
->sh_size
!= 0)
1650 amt
= extsymcount
* sizeof (Elf_External_Sym_Shndx
);
1651 pos
= shndx_hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym_Shndx
);
1652 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1653 || bfd_bread ((PTR
) shndx_buf
, amt
, abfd
) != amt
)
1659 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
1660 esymend
= buf
+ extsymcount
;
1661 for (esym
= buf
, shndx
= shndx_buf
;
1663 esym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
),
1664 shndx
= (shndx
!= NULL
? shndx
+ 1 : NULL
))
1666 Elf_Internal_Sym sym
;
1672 struct elf_link_hash_entry
*h
;
1674 boolean size_change_ok
, type_change_ok
;
1675 boolean new_weakdef
;
1676 unsigned int old_alignment
;
1681 elf_swap_symbol_in (abfd
, (const PTR
) esym
, (const PTR
) shndx
, &sym
);
1683 flags
= BSF_NO_FLAGS
;
1685 value
= sym
.st_value
;
1688 bind
= ELF_ST_BIND (sym
.st_info
);
1689 if (bind
== STB_LOCAL
)
1691 /* This should be impossible, since ELF requires that all
1692 global symbols follow all local symbols, and that sh_info
1693 point to the first global symbol. Unfortunatealy, Irix 5
1697 else if (bind
== STB_GLOBAL
)
1699 if (sym
.st_shndx
!= SHN_UNDEF
1700 && sym
.st_shndx
!= SHN_COMMON
)
1703 else if (bind
== STB_WEAK
)
1707 /* Leave it up to the processor backend. */
1710 if (sym
.st_shndx
== SHN_UNDEF
)
1711 sec
= bfd_und_section_ptr
;
1712 else if (sym
.st_shndx
< SHN_LORESERVE
|| sym
.st_shndx
> SHN_HIRESERVE
)
1714 sec
= section_from_elf_index (abfd
, sym
.st_shndx
);
1716 sec
= bfd_abs_section_ptr
;
1717 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
1720 else if (sym
.st_shndx
== SHN_ABS
)
1721 sec
= bfd_abs_section_ptr
;
1722 else if (sym
.st_shndx
== SHN_COMMON
)
1724 sec
= bfd_com_section_ptr
;
1725 /* What ELF calls the size we call the value. What ELF
1726 calls the value we call the alignment. */
1727 value
= sym
.st_size
;
1731 /* Leave it up to the processor backend. */
1734 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
1735 if (name
== (const char *) NULL
)
1738 if (sym
.st_shndx
== SHN_COMMON
&& ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
1740 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
1744 tcomm
= bfd_make_section (abfd
, ".tcommon");
1746 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
1748 | SEC_LINKER_CREATED
1749 | SEC_THREAD_LOCAL
)))
1754 else if (add_symbol_hook
)
1756 if (! (*add_symbol_hook
) (abfd
, info
, &sym
, &name
, &flags
, &sec
,
1760 /* The hook function sets the name to NULL if this symbol
1761 should be skipped for some reason. */
1762 if (name
== (const char *) NULL
)
1766 /* Sanity check that all possibilities were handled. */
1767 if (sec
== (asection
*) NULL
)
1769 bfd_set_error (bfd_error_bad_value
);
1773 if (bfd_is_und_section (sec
)
1774 || bfd_is_com_section (sec
))
1779 size_change_ok
= false;
1780 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
1782 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1784 Elf_Internal_Versym iver
;
1785 unsigned int vernum
= 0;
1789 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
1790 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
1792 /* If this is a hidden symbol, or if it is not version
1793 1, we append the version name to the symbol name.
1794 However, we do not modify a non-hidden absolute
1795 symbol, because it might be the version symbol
1796 itself. FIXME: What if it isn't? */
1797 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
1798 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
1801 unsigned int namelen
;
1802 bfd_size_type newlen
;
1805 if (sym
.st_shndx
!= SHN_UNDEF
)
1807 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
1809 (*_bfd_error_handler
)
1810 (_("%s: %s: invalid version %u (max %d)"),
1811 bfd_archive_filename (abfd
), name
, vernum
,
1812 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1813 bfd_set_error (bfd_error_bad_value
);
1816 else if (vernum
> 1)
1818 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1824 /* We cannot simply test for the number of
1825 entries in the VERNEED section since the
1826 numbers for the needed versions do not start
1828 Elf_Internal_Verneed
*t
;
1831 for (t
= elf_tdata (abfd
)->verref
;
1835 Elf_Internal_Vernaux
*a
;
1837 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1839 if (a
->vna_other
== vernum
)
1841 verstr
= a
->vna_nodename
;
1850 (*_bfd_error_handler
)
1851 (_("%s: %s: invalid needed version %d"),
1852 bfd_archive_filename (abfd
), name
, vernum
);
1853 bfd_set_error (bfd_error_bad_value
);
1858 namelen
= strlen (name
);
1859 newlen
= namelen
+ strlen (verstr
) + 2;
1860 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
1863 newname
= (char *) bfd_alloc (abfd
, newlen
);
1864 if (newname
== NULL
)
1866 strcpy (newname
, name
);
1867 p
= newname
+ namelen
;
1869 /* If this is a defined non-hidden version symbol,
1870 we add another @ to the name. This indicates the
1871 default version of the symbol. */
1872 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1873 && sym
.st_shndx
!= SHN_UNDEF
)
1881 if (! elf_merge_symbol (abfd
, info
, name
, &sym
, &sec
, &value
,
1882 sym_hash
, &override
, &type_change_ok
,
1883 &size_change_ok
, dt_needed
))
1890 while (h
->root
.type
== bfd_link_hash_indirect
1891 || h
->root
.type
== bfd_link_hash_warning
)
1892 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1894 /* Remember the old alignment if this is a common symbol, so
1895 that we don't reduce the alignment later on. We can't
1896 check later, because _bfd_generic_link_add_one_symbol
1897 will set a default for the alignment which we want to
1899 if (h
->root
.type
== bfd_link_hash_common
)
1900 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1902 if (elf_tdata (abfd
)->verdef
!= NULL
1906 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1909 if (! (_bfd_generic_link_add_one_symbol
1910 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
1911 false, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
1915 while (h
->root
.type
== bfd_link_hash_indirect
1916 || h
->root
.type
== bfd_link_hash_warning
)
1917 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1920 new_weakdef
= false;
1923 && (flags
& BSF_WEAK
) != 0
1924 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
1925 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1926 && h
->weakdef
== NULL
)
1928 /* Keep a list of all weak defined non function symbols from
1929 a dynamic object, using the weakdef field. Later in this
1930 function we will set the weakdef field to the correct
1931 value. We only put non-function symbols from dynamic
1932 objects on this list, because that happens to be the only
1933 time we need to know the normal symbol corresponding to a
1934 weak symbol, and the information is time consuming to
1935 figure out. If the weakdef field is not already NULL,
1936 then this symbol was already defined by some previous
1937 dynamic object, and we will be using that previous
1938 definition anyhow. */
1945 /* Set the alignment of a common symbol. */
1946 if (sym
.st_shndx
== SHN_COMMON
1947 && h
->root
.type
== bfd_link_hash_common
)
1951 align
= bfd_log2 (sym
.st_value
);
1952 if (align
> old_alignment
1953 /* Permit an alignment power of zero if an alignment of one
1954 is specified and no other alignments have been specified. */
1955 || (sym
.st_value
== 1 && old_alignment
== 0))
1956 h
->root
.u
.c
.p
->alignment_power
= align
;
1959 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1965 /* Remember the symbol size and type. */
1966 if (sym
.st_size
!= 0
1967 && (definition
|| h
->size
== 0))
1969 if (h
->size
!= 0 && h
->size
!= sym
.st_size
&& ! size_change_ok
)
1970 (*_bfd_error_handler
)
1971 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1972 name
, (unsigned long) h
->size
, (unsigned long) sym
.st_size
,
1973 bfd_archive_filename (abfd
));
1975 h
->size
= sym
.st_size
;
1978 /* If this is a common symbol, then we always want H->SIZE
1979 to be the size of the common symbol. The code just above
1980 won't fix the size if a common symbol becomes larger. We
1981 don't warn about a size change here, because that is
1982 covered by --warn-common. */
1983 if (h
->root
.type
== bfd_link_hash_common
)
1984 h
->size
= h
->root
.u
.c
.size
;
1986 if (ELF_ST_TYPE (sym
.st_info
) != STT_NOTYPE
1987 && (definition
|| h
->type
== STT_NOTYPE
))
1989 if (h
->type
!= STT_NOTYPE
1990 && h
->type
!= ELF_ST_TYPE (sym
.st_info
)
1991 && ! type_change_ok
)
1992 (*_bfd_error_handler
)
1993 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1994 name
, h
->type
, ELF_ST_TYPE (sym
.st_info
),
1995 bfd_archive_filename (abfd
));
1997 h
->type
= ELF_ST_TYPE (sym
.st_info
);
2000 /* If st_other has a processor-specific meaning, specific code
2001 might be needed here. */
2002 if (sym
.st_other
!= 0)
2004 /* Combine visibilities, using the most constraining one. */
2005 unsigned char hvis
= ELF_ST_VISIBILITY (h
->other
);
2006 unsigned char symvis
= ELF_ST_VISIBILITY (sym
.st_other
);
2008 if (symvis
&& (hvis
> symvis
|| hvis
== 0))
2009 h
->other
= sym
.st_other
;
2011 /* If neither has visibility, use the st_other of the
2012 definition. This is an arbitrary choice, since the
2013 other bits have no general meaning. */
2014 if (!symvis
&& !hvis
2015 && (definition
|| h
->other
== 0))
2016 h
->other
= sym
.st_other
;
2019 /* Set a flag in the hash table entry indicating the type of
2020 reference or definition we just found. Keep a count of
2021 the number of dynamic symbols we find. A dynamic symbol
2022 is one which is referenced or defined by both a regular
2023 object and a shared object. */
2024 old_flags
= h
->elf_link_hash_flags
;
2030 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
2031 if (bind
!= STB_WEAK
)
2032 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
2035 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
2037 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2038 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
2044 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
2046 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
2047 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
2048 | ELF_LINK_HASH_REF_REGULAR
)) != 0
2049 || (h
->weakdef
!= NULL
2051 && h
->weakdef
->dynindx
!= -1))
2055 h
->elf_link_hash_flags
|= new_flag
;
2057 /* Check to see if we need to add an indirect symbol for
2058 the default name. */
2059 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
2060 if (! elf_add_default_symbol (abfd
, info
, h
, name
, &sym
,
2061 &sec
, &value
, &dynsym
,
2062 override
, dt_needed
))
2065 if (dynsym
&& h
->dynindx
== -1)
2067 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2069 if (h
->weakdef
!= NULL
2071 && h
->weakdef
->dynindx
== -1)
2073 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2077 else if (dynsym
&& h
->dynindx
!= -1)
2078 /* If the symbol already has a dynamic index, but
2079 visibility says it should not be visible, turn it into
2081 switch (ELF_ST_VISIBILITY (h
->other
))
2085 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2089 if (dt_needed
&& definition
2090 && (h
->elf_link_hash_flags
2091 & ELF_LINK_HASH_REF_REGULAR
) != 0)
2093 bfd_size_type oldsize
;
2094 bfd_size_type strindex
;
2096 if (! is_elf_hash_table (info
))
2099 /* The symbol from a DT_NEEDED object is referenced from
2100 the regular object to create a dynamic executable. We
2101 have to make sure there is a DT_NEEDED entry for it. */
2104 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2105 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
,
2106 elf_dt_soname (abfd
), false);
2107 if (strindex
== (bfd_size_type
) -1)
2110 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2113 Elf_External_Dyn
*dyncon
, *dynconend
;
2115 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
,
2117 BFD_ASSERT (sdyn
!= NULL
);
2119 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
2120 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
2122 for (; dyncon
< dynconend
; dyncon
++)
2124 Elf_Internal_Dyn dyn
;
2126 elf_swap_dyn_in (hash_table
->dynobj
,
2128 BFD_ASSERT (dyn
.d_tag
!= DT_NEEDED
||
2129 dyn
.d_un
.d_val
!= strindex
);
2133 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
2139 /* Now set the weakdefs field correctly for all the weak defined
2140 symbols we found. The only way to do this is to search all the
2141 symbols. Since we only need the information for non functions in
2142 dynamic objects, that's the only time we actually put anything on
2143 the list WEAKS. We need this information so that if a regular
2144 object refers to a symbol defined weakly in a dynamic object, the
2145 real symbol in the dynamic object is also put in the dynamic
2146 symbols; we also must arrange for both symbols to point to the
2147 same memory location. We could handle the general case of symbol
2148 aliasing, but a general symbol alias can only be generated in
2149 assembler code, handling it correctly would be very time
2150 consuming, and other ELF linkers don't handle general aliasing
2152 while (weaks
!= NULL
)
2154 struct elf_link_hash_entry
*hlook
;
2157 struct elf_link_hash_entry
**hpp
;
2158 struct elf_link_hash_entry
**hppend
;
2161 weaks
= hlook
->weakdef
;
2162 hlook
->weakdef
= NULL
;
2164 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
2165 || hlook
->root
.type
== bfd_link_hash_defweak
2166 || hlook
->root
.type
== bfd_link_hash_common
2167 || hlook
->root
.type
== bfd_link_hash_indirect
);
2168 slook
= hlook
->root
.u
.def
.section
;
2169 vlook
= hlook
->root
.u
.def
.value
;
2171 hpp
= elf_sym_hashes (abfd
);
2172 hppend
= hpp
+ extsymcount
;
2173 for (; hpp
< hppend
; hpp
++)
2175 struct elf_link_hash_entry
*h
;
2178 if (h
!= NULL
&& h
!= hlook
2179 && h
->root
.type
== bfd_link_hash_defined
2180 && h
->root
.u
.def
.section
== slook
2181 && h
->root
.u
.def
.value
== vlook
)
2185 /* If the weak definition is in the list of dynamic
2186 symbols, make sure the real definition is put there
2188 if (hlook
->dynindx
!= -1
2189 && h
->dynindx
== -1)
2191 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2195 /* If the real definition is in the list of dynamic
2196 symbols, make sure the weak definition is put there
2197 as well. If we don't do this, then the dynamic
2198 loader might not merge the entries for the real
2199 definition and the weak definition. */
2200 if (h
->dynindx
!= -1
2201 && hlook
->dynindx
== -1)
2203 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
2218 if (extversym
!= NULL
)
2224 /* If this object is the same format as the output object, and it is
2225 not a shared library, then let the backend look through the
2228 This is required to build global offset table entries and to
2229 arrange for dynamic relocs. It is not required for the
2230 particular common case of linking non PIC code, even when linking
2231 against shared libraries, but unfortunately there is no way of
2232 knowing whether an object file has been compiled PIC or not.
2233 Looking through the relocs is not particularly time consuming.
2234 The problem is that we must either (1) keep the relocs in memory,
2235 which causes the linker to require additional runtime memory or
2236 (2) read the relocs twice from the input file, which wastes time.
2237 This would be a good case for using mmap.
2239 I have no idea how to handle linking PIC code into a file of a
2240 different format. It probably can't be done. */
2241 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
2243 && abfd
->xvec
== info
->hash
->creator
2244 && check_relocs
!= NULL
)
2248 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2250 Elf_Internal_Rela
*internal_relocs
;
2253 if ((o
->flags
& SEC_RELOC
) == 0
2254 || o
->reloc_count
== 0
2255 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
2256 && (o
->flags
& SEC_DEBUGGING
) != 0)
2257 || bfd_is_abs_section (o
->output_section
))
2260 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
2261 (abfd
, o
, (PTR
) NULL
,
2262 (Elf_Internal_Rela
*) NULL
,
2263 info
->keep_memory
));
2264 if (internal_relocs
== NULL
)
2267 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
2269 if (! info
->keep_memory
)
2270 free (internal_relocs
);
2277 /* If this is a non-traditional, non-relocateable link, try to
2278 optimize the handling of the .stab/.stabstr sections. */
2280 && ! info
->relocateable
2281 && ! info
->traditional_format
2282 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
2283 && is_elf_hash_table (info
)
2284 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
2286 asection
*stab
, *stabstr
;
2288 stab
= bfd_get_section_by_name (abfd
, ".stab");
2290 && (stab
->flags
& SEC_MERGE
) == 0
2291 && !bfd_is_abs_section (stab
->output_section
))
2293 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
2295 if (stabstr
!= NULL
)
2297 struct bfd_elf_section_data
*secdata
;
2299 secdata
= elf_section_data (stab
);
2300 if (! _bfd_link_section_stabs (abfd
,
2301 & hash_table
->stab_info
,
2303 &secdata
->sec_info
))
2305 if (secdata
->sec_info
)
2306 secdata
->sec_info_type
= ELF_INFO_TYPE_STABS
;
2311 if (! info
->relocateable
&& ! dynamic
2312 && is_elf_hash_table (info
))
2316 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2317 if ((s
->flags
& SEC_MERGE
) != 0
2318 && !bfd_is_abs_section (s
->output_section
))
2320 struct bfd_elf_section_data
*secdata
;
2322 secdata
= elf_section_data (s
);
2323 if (! _bfd_merge_section (abfd
,
2324 & hash_table
->merge_info
,
2325 s
, &secdata
->sec_info
))
2327 else if (secdata
->sec_info
)
2328 secdata
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
2332 if (is_elf_hash_table (info
))
2334 /* Add this bfd to the loaded list. */
2335 struct elf_link_loaded_list
*n
;
2337 n
= ((struct elf_link_loaded_list
*)
2338 bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
)));
2342 n
->next
= hash_table
->loaded
;
2343 hash_table
->loaded
= n
;
2353 if (extversym
!= NULL
)
2358 /* Create some sections which will be filled in with dynamic linking
2359 information. ABFD is an input file which requires dynamic sections
2360 to be created. The dynamic sections take up virtual memory space
2361 when the final executable is run, so we need to create them before
2362 addresses are assigned to the output sections. We work out the
2363 actual contents and size of these sections later. */
2366 elf_link_create_dynamic_sections (abfd
, info
)
2368 struct bfd_link_info
*info
;
2371 register asection
*s
;
2372 struct elf_link_hash_entry
*h
;
2373 struct elf_backend_data
*bed
;
2375 if (! is_elf_hash_table (info
))
2378 if (elf_hash_table (info
)->dynamic_sections_created
)
2381 /* Make sure that all dynamic sections use the same input BFD. */
2382 if (elf_hash_table (info
)->dynobj
== NULL
)
2383 elf_hash_table (info
)->dynobj
= abfd
;
2385 abfd
= elf_hash_table (info
)->dynobj
;
2387 /* Note that we set the SEC_IN_MEMORY flag for all of these
2389 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
2390 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
2392 /* A dynamically linked executable has a .interp section, but a
2393 shared library does not. */
2396 s
= bfd_make_section (abfd
, ".interp");
2398 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2402 if (! info
->traditional_format
2403 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
2405 s
= bfd_make_section (abfd
, ".eh_frame_hdr");
2407 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2408 || ! bfd_set_section_alignment (abfd
, s
, 2))
2412 /* Create sections to hold version informations. These are removed
2413 if they are not needed. */
2414 s
= bfd_make_section (abfd
, ".gnu.version_d");
2416 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2417 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2420 s
= bfd_make_section (abfd
, ".gnu.version");
2422 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2423 || ! bfd_set_section_alignment (abfd
, s
, 1))
2426 s
= bfd_make_section (abfd
, ".gnu.version_r");
2428 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2429 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2432 s
= bfd_make_section (abfd
, ".dynsym");
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
, ".dynstr");
2440 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2443 /* Create a strtab to hold the dynamic symbol names. */
2444 if (elf_hash_table (info
)->dynstr
== NULL
)
2446 elf_hash_table (info
)->dynstr
= _bfd_elf_strtab_init ();
2447 if (elf_hash_table (info
)->dynstr
== NULL
)
2451 s
= bfd_make_section (abfd
, ".dynamic");
2453 || ! bfd_set_section_flags (abfd
, s
, flags
)
2454 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2457 /* The special symbol _DYNAMIC is always set to the start of the
2458 .dynamic section. This call occurs before we have processed the
2459 symbols for any dynamic object, so we don't have to worry about
2460 overriding a dynamic definition. We could set _DYNAMIC in a
2461 linker script, but we only want to define it if we are, in fact,
2462 creating a .dynamic section. We don't want to define it if there
2463 is no .dynamic section, since on some ELF platforms the start up
2464 code examines it to decide how to initialize the process. */
2466 if (! (_bfd_generic_link_add_one_symbol
2467 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
2468 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
2469 (struct bfd_link_hash_entry
**) &h
)))
2471 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2472 h
->type
= STT_OBJECT
;
2475 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2478 bed
= get_elf_backend_data (abfd
);
2480 s
= bfd_make_section (abfd
, ".hash");
2482 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2483 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2485 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
2487 /* Let the backend create the rest of the sections. This lets the
2488 backend set the right flags. The backend will normally create
2489 the .got and .plt sections. */
2490 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
2493 elf_hash_table (info
)->dynamic_sections_created
= true;
2498 /* Add an entry to the .dynamic table. */
2501 elf_add_dynamic_entry (info
, tag
, val
)
2502 struct bfd_link_info
*info
;
2506 Elf_Internal_Dyn dyn
;
2509 bfd_size_type newsize
;
2510 bfd_byte
*newcontents
;
2512 if (! is_elf_hash_table (info
))
2515 dynobj
= elf_hash_table (info
)->dynobj
;
2517 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
2518 BFD_ASSERT (s
!= NULL
);
2520 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
2521 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
2522 if (newcontents
== NULL
)
2526 dyn
.d_un
.d_val
= val
;
2527 elf_swap_dyn_out (dynobj
, &dyn
,
2528 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
2530 s
->_raw_size
= newsize
;
2531 s
->contents
= newcontents
;
2536 /* Record a new local dynamic symbol. */
2539 elf_link_record_local_dynamic_symbol (info
, input_bfd
, input_indx
)
2540 struct bfd_link_info
*info
;
2544 struct elf_link_local_dynamic_entry
*entry
;
2545 struct elf_link_hash_table
*eht
;
2546 struct elf_strtab_hash
*dynstr
;
2547 Elf_External_Sym esym
;
2548 Elf_External_Sym_Shndx eshndx
;
2549 Elf_External_Sym_Shndx
*shndx
;
2550 unsigned long dynstr_index
;
2555 if (! is_elf_hash_table (info
))
2558 /* See if the entry exists already. */
2559 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
2560 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
2563 entry
= (struct elf_link_local_dynamic_entry
*)
2564 bfd_alloc (input_bfd
, (bfd_size_type
) sizeof (*entry
));
2568 /* Go find the symbol, so that we can find it's name. */
2569 amt
= sizeof (Elf_External_Sym
);
2570 pos
= elf_tdata (input_bfd
)->symtab_hdr
.sh_offset
+ input_indx
* amt
;
2571 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
2572 || bfd_bread ((PTR
) &esym
, amt
, input_bfd
) != amt
)
2575 if (elf_tdata (input_bfd
)->symtab_shndx_hdr
.sh_size
!= 0)
2577 amt
= sizeof (Elf_External_Sym_Shndx
);
2578 pos
= elf_tdata (input_bfd
)->symtab_shndx_hdr
.sh_offset
;
2579 pos
+= input_indx
* amt
;
2581 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
2582 || bfd_bread ((PTR
) shndx
, amt
, input_bfd
) != amt
)
2585 elf_swap_symbol_in (input_bfd
, (const PTR
) &esym
, (const PTR
) shndx
,
2588 name
= (bfd_elf_string_from_elf_section
2589 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
2590 entry
->isym
.st_name
));
2592 dynstr
= elf_hash_table (info
)->dynstr
;
2595 /* Create a strtab to hold the dynamic symbol names. */
2596 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
2601 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, false);
2602 if (dynstr_index
== (unsigned long) -1)
2604 entry
->isym
.st_name
= dynstr_index
;
2606 eht
= elf_hash_table (info
);
2608 entry
->next
= eht
->dynlocal
;
2609 eht
->dynlocal
= entry
;
2610 entry
->input_bfd
= input_bfd
;
2611 entry
->input_indx
= input_indx
;
2614 /* Whatever binding the symbol had before, it's now local. */
2616 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
2618 /* The dynindx will be set at the end of size_dynamic_sections. */
2623 /* Read and swap the relocs from the section indicated by SHDR. This
2624 may be either a REL or a RELA section. The relocations are
2625 translated into RELA relocations and stored in INTERNAL_RELOCS,
2626 which should have already been allocated to contain enough space.
2627 The EXTERNAL_RELOCS are a buffer where the external form of the
2628 relocations should be stored.
2630 Returns false if something goes wrong. */
2633 elf_link_read_relocs_from_section (abfd
, shdr
, external_relocs
,
2636 Elf_Internal_Shdr
*shdr
;
2637 PTR external_relocs
;
2638 Elf_Internal_Rela
*internal_relocs
;
2640 struct elf_backend_data
*bed
;
2643 /* If there aren't any relocations, that's OK. */
2647 /* Position ourselves at the start of the section. */
2648 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2651 /* Read the relocations. */
2652 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2655 bed
= get_elf_backend_data (abfd
);
2657 /* Convert the external relocations to the internal format. */
2658 if (shdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2660 Elf_External_Rel
*erel
;
2661 Elf_External_Rel
*erelend
;
2662 Elf_Internal_Rela
*irela
;
2663 Elf_Internal_Rel
*irel
;
2665 erel
= (Elf_External_Rel
*) external_relocs
;
2666 erelend
= erel
+ NUM_SHDR_ENTRIES (shdr
);
2667 irela
= internal_relocs
;
2668 amt
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
2669 irel
= bfd_alloc (abfd
, amt
);
2670 for (; erel
< erelend
; erel
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2674 if (bed
->s
->swap_reloc_in
)
2675 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
2677 elf_swap_reloc_in (abfd
, erel
, irel
);
2679 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; ++i
)
2681 irela
[i
].r_offset
= irel
[i
].r_offset
;
2682 irela
[i
].r_info
= irel
[i
].r_info
;
2683 irela
[i
].r_addend
= 0;
2689 Elf_External_Rela
*erela
;
2690 Elf_External_Rela
*erelaend
;
2691 Elf_Internal_Rela
*irela
;
2693 BFD_ASSERT (shdr
->sh_entsize
== sizeof (Elf_External_Rela
));
2695 erela
= (Elf_External_Rela
*) external_relocs
;
2696 erelaend
= erela
+ NUM_SHDR_ENTRIES (shdr
);
2697 irela
= internal_relocs
;
2698 for (; erela
< erelaend
; erela
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2700 if (bed
->s
->swap_reloca_in
)
2701 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
2703 elf_swap_reloca_in (abfd
, erela
, irela
);
2710 /* Read and swap the relocs for a section O. They may have been
2711 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2712 not NULL, they are used as buffers to read into. They are known to
2713 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2714 the return value is allocated using either malloc or bfd_alloc,
2715 according to the KEEP_MEMORY argument. If O has two relocation
2716 sections (both REL and RELA relocations), then the REL_HDR
2717 relocations will appear first in INTERNAL_RELOCS, followed by the
2718 REL_HDR2 relocations. */
2721 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
2725 PTR external_relocs
;
2726 Elf_Internal_Rela
*internal_relocs
;
2727 boolean keep_memory
;
2729 Elf_Internal_Shdr
*rel_hdr
;
2731 Elf_Internal_Rela
*alloc2
= NULL
;
2732 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2734 if (elf_section_data (o
)->relocs
!= NULL
)
2735 return elf_section_data (o
)->relocs
;
2737 if (o
->reloc_count
== 0)
2740 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2742 if (internal_relocs
== NULL
)
2746 size
= o
->reloc_count
;
2747 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2749 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2751 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2752 if (internal_relocs
== NULL
)
2756 if (external_relocs
== NULL
)
2758 bfd_size_type size
= rel_hdr
->sh_size
;
2760 if (elf_section_data (o
)->rel_hdr2
)
2761 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2762 alloc1
= (PTR
) bfd_malloc (size
);
2765 external_relocs
= alloc1
;
2768 if (!elf_link_read_relocs_from_section (abfd
, rel_hdr
,
2772 if (!elf_link_read_relocs_from_section
2774 elf_section_data (o
)->rel_hdr2
,
2775 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2776 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2777 * bed
->s
->int_rels_per_ext_rel
)))
2780 /* Cache the results for next time, if we can. */
2782 elf_section_data (o
)->relocs
= internal_relocs
;
2787 /* Don't free alloc2, since if it was allocated we are passing it
2788 back (under the name of internal_relocs). */
2790 return internal_relocs
;
2800 /* Record an assignment to a symbol made by a linker script. We need
2801 this in case some dynamic object refers to this symbol. */
2804 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
2805 bfd
*output_bfd ATTRIBUTE_UNUSED
;
2806 struct bfd_link_info
*info
;
2810 struct elf_link_hash_entry
*h
;
2812 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2815 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, true, false);
2819 if (h
->root
.type
== bfd_link_hash_new
)
2820 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_ELF
;
2822 /* If this symbol is being provided by the linker script, and it is
2823 currently defined by a dynamic object, but not by a regular
2824 object, then mark it as undefined so that the generic linker will
2825 force the correct value. */
2827 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2828 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2829 h
->root
.type
= bfd_link_hash_undefined
;
2831 /* If this symbol is not being provided by the linker script, and it is
2832 currently defined by a dynamic object, but not by a regular object,
2833 then clear out any version information because the symbol will not be
2834 associated with the dynamic object any more. */
2836 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2837 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2838 h
->verinfo
.verdef
= NULL
;
2840 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2842 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2843 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
2845 && h
->dynindx
== -1)
2847 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2850 /* If this is a weak defined symbol, and we know a corresponding
2851 real symbol from the same dynamic object, make sure the real
2852 symbol is also made into a dynamic symbol. */
2853 if (h
->weakdef
!= NULL
2854 && h
->weakdef
->dynindx
== -1)
2856 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2864 /* This structure is used to pass information to
2865 elf_link_assign_sym_version. */
2867 struct elf_assign_sym_version_info
2871 /* General link information. */
2872 struct bfd_link_info
*info
;
2874 struct bfd_elf_version_tree
*verdefs
;
2875 /* Whether we had a failure. */
2879 /* This structure is used to pass information to
2880 elf_link_find_version_dependencies. */
2882 struct elf_find_verdep_info
2886 /* General link information. */
2887 struct bfd_link_info
*info
;
2888 /* The number of dependencies. */
2890 /* Whether we had a failure. */
2894 /* Array used to determine the number of hash table buckets to use
2895 based on the number of symbols there are. If there are fewer than
2896 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2897 fewer than 37 we use 17 buckets, and so forth. We never use more
2898 than 32771 buckets. */
2900 static const size_t elf_buckets
[] =
2902 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2906 /* Compute bucket count for hashing table. We do not use a static set
2907 of possible tables sizes anymore. Instead we determine for all
2908 possible reasonable sizes of the table the outcome (i.e., the
2909 number of collisions etc) and choose the best solution. The
2910 weighting functions are not too simple to allow the table to grow
2911 without bounds. Instead one of the weighting factors is the size.
2912 Therefore the result is always a good payoff between few collisions
2913 (= short chain lengths) and table size. */
2915 compute_bucket_count (info
)
2916 struct bfd_link_info
*info
;
2918 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2919 size_t best_size
= 0;
2920 unsigned long int *hashcodes
;
2921 unsigned long int *hashcodesp
;
2922 unsigned long int i
;
2925 /* Compute the hash values for all exported symbols. At the same
2926 time store the values in an array so that we could use them for
2929 amt
*= sizeof (unsigned long int);
2930 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
2931 if (hashcodes
== NULL
)
2933 hashcodesp
= hashcodes
;
2935 /* Put all hash values in HASHCODES. */
2936 elf_link_hash_traverse (elf_hash_table (info
),
2937 elf_collect_hash_codes
, &hashcodesp
);
2939 /* We have a problem here. The following code to optimize the table
2940 size requires an integer type with more the 32 bits. If
2941 BFD_HOST_U_64_BIT is set we know about such a type. */
2942 #ifdef BFD_HOST_U_64_BIT
2945 unsigned long int nsyms
= hashcodesp
- hashcodes
;
2948 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
2949 unsigned long int *counts
;
2951 /* Possible optimization parameters: if we have NSYMS symbols we say
2952 that the hashing table must at least have NSYMS/4 and at most
2954 minsize
= nsyms
/ 4;
2957 best_size
= maxsize
= nsyms
* 2;
2959 /* Create array where we count the collisions in. We must use bfd_malloc
2960 since the size could be large. */
2962 amt
*= sizeof (unsigned long int);
2963 counts
= (unsigned long int *) bfd_malloc (amt
);
2970 /* Compute the "optimal" size for the hash table. The criteria is a
2971 minimal chain length. The minor criteria is (of course) the size
2973 for (i
= minsize
; i
< maxsize
; ++i
)
2975 /* Walk through the array of hashcodes and count the collisions. */
2976 BFD_HOST_U_64_BIT max
;
2977 unsigned long int j
;
2978 unsigned long int fact
;
2980 memset (counts
, '\0', i
* sizeof (unsigned long int));
2982 /* Determine how often each hash bucket is used. */
2983 for (j
= 0; j
< nsyms
; ++j
)
2984 ++counts
[hashcodes
[j
] % i
];
2986 /* For the weight function we need some information about the
2987 pagesize on the target. This is information need not be 100%
2988 accurate. Since this information is not available (so far) we
2989 define it here to a reasonable default value. If it is crucial
2990 to have a better value some day simply define this value. */
2991 # ifndef BFD_TARGET_PAGESIZE
2992 # define BFD_TARGET_PAGESIZE (4096)
2995 /* We in any case need 2 + NSYMS entries for the size values and
2997 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
3000 /* Variant 1: optimize for short chains. We add the squares
3001 of all the chain lengths (which favous many small chain
3002 over a few long chains). */
3003 for (j
= 0; j
< i
; ++j
)
3004 max
+= counts
[j
] * counts
[j
];
3006 /* This adds penalties for the overall size of the table. */
3007 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
3010 /* Variant 2: Optimize a lot more for small table. Here we
3011 also add squares of the size but we also add penalties for
3012 empty slots (the +1 term). */
3013 for (j
= 0; j
< i
; ++j
)
3014 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
3016 /* The overall size of the table is considered, but not as
3017 strong as in variant 1, where it is squared. */
3018 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
3022 /* Compare with current best results. */
3023 if (max
< best_chlen
)
3033 #endif /* defined (BFD_HOST_U_64_BIT) */
3035 /* This is the fallback solution if no 64bit type is available or if we
3036 are not supposed to spend much time on optimizations. We select the
3037 bucket count using a fixed set of numbers. */
3038 for (i
= 0; elf_buckets
[i
] != 0; i
++)
3040 best_size
= elf_buckets
[i
];
3041 if (dynsymcount
< elf_buckets
[i
+ 1])
3046 /* Free the arrays we needed. */
3052 /* Set up the sizes and contents of the ELF dynamic sections. This is
3053 called by the ELF linker emulation before_allocation routine. We
3054 must set the sizes of the sections before the linker sets the
3055 addresses of the various sections. */
3058 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
3060 auxiliary_filters
, info
, sinterpptr
,
3065 const char *filter_shlib
;
3066 const char * const *auxiliary_filters
;
3067 struct bfd_link_info
*info
;
3068 asection
**sinterpptr
;
3069 struct bfd_elf_version_tree
*verdefs
;
3071 bfd_size_type soname_indx
;
3073 struct elf_backend_data
*bed
;
3074 struct elf_assign_sym_version_info asvinfo
;
3078 soname_indx
= (bfd_size_type
) -1;
3080 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
3083 if (! is_elf_hash_table (info
))
3086 /* Any syms created from now on start with -1 in
3087 got.refcount/offset and plt.refcount/offset. */
3088 elf_hash_table (info
)->init_refcount
= -1;
3090 /* The backend may have to create some sections regardless of whether
3091 we're dynamic or not. */
3092 bed
= get_elf_backend_data (output_bfd
);
3093 if (bed
->elf_backend_always_size_sections
3094 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
3097 dynobj
= elf_hash_table (info
)->dynobj
;
3099 /* If there were no dynamic objects in the link, there is nothing to
3104 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
3107 if (elf_hash_table (info
)->dynamic_sections_created
)
3109 struct elf_info_failed eif
;
3110 struct elf_link_hash_entry
*h
;
3113 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
3114 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
3118 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3120 if (soname_indx
== (bfd_size_type
) -1
3121 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SONAME
,
3128 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMBOLIC
,
3131 info
->flags
|= DF_SYMBOLIC
;
3138 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
3140 if (info
->new_dtags
)
3141 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
3142 if (indx
== (bfd_size_type
) -1
3143 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RPATH
, indx
)
3145 && ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RUNPATH
,
3150 if (filter_shlib
!= NULL
)
3154 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3155 filter_shlib
, true);
3156 if (indx
== (bfd_size_type
) -1
3157 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FILTER
, indx
))
3161 if (auxiliary_filters
!= NULL
)
3163 const char * const *p
;
3165 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
3169 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3171 if (indx
== (bfd_size_type
) -1
3172 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_AUXILIARY
,
3179 eif
.verdefs
= verdefs
;
3182 /* If we are supposed to export all symbols into the dynamic symbol
3183 table (this is not the normal case), then do so. */
3184 if (info
->export_dynamic
)
3186 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
3192 /* Attach all the symbols to their version information. */
3193 asvinfo
.output_bfd
= output_bfd
;
3194 asvinfo
.info
= info
;
3195 asvinfo
.verdefs
= verdefs
;
3196 asvinfo
.failed
= false;
3198 elf_link_hash_traverse (elf_hash_table (info
),
3199 elf_link_assign_sym_version
,
3204 /* Find all symbols which were defined in a dynamic object and make
3205 the backend pick a reasonable value for them. */
3206 elf_link_hash_traverse (elf_hash_table (info
),
3207 elf_adjust_dynamic_symbol
,
3212 /* Add some entries to the .dynamic section. We fill in some of the
3213 values later, in elf_bfd_final_link, but we must add the entries
3214 now so that we know the final size of the .dynamic section. */
3216 /* If there are initialization and/or finalization functions to
3217 call then add the corresponding DT_INIT/DT_FINI entries. */
3218 h
= (info
->init_function
3219 ? elf_link_hash_lookup (elf_hash_table (info
),
3220 info
->init_function
, false,
3224 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3225 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3227 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT
, (bfd_vma
) 0))
3230 h
= (info
->fini_function
3231 ? elf_link_hash_lookup (elf_hash_table (info
),
3232 info
->fini_function
, false,
3236 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3237 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3239 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI
, (bfd_vma
) 0))
3243 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
3245 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3251 for (sub
= info
->input_bfds
; sub
!= NULL
;
3252 sub
= sub
->link_next
)
3253 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3254 if (elf_section_data (o
)->this_hdr
.sh_type
3255 == SHT_PREINIT_ARRAY
)
3257 (*_bfd_error_handler
)
3258 (_("%s: .preinit_array section is not allowed in DSO"),
3259 bfd_archive_filename (sub
));
3263 bfd_set_error (bfd_error_nonrepresentable_section
);
3267 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAY
,
3269 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_PREINIT_ARRAYSZ
,
3273 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
3275 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAY
,
3277 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT_ARRAYSZ
,
3281 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
3283 if (!elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAY
,
3285 || !elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI_ARRAYSZ
,
3290 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
3291 /* If .dynstr is excluded from the link, we don't want any of
3292 these tags. Strictly, we should be checking each section
3293 individually; This quick check covers for the case where
3294 someone does a /DISCARD/ : { *(*) }. */
3295 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
3297 bfd_size_type strsize
;
3299 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3300 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_HASH
, (bfd_vma
) 0)
3301 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRTAB
, (bfd_vma
) 0)
3302 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMTAB
, (bfd_vma
) 0)
3303 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRSZ
, strsize
)
3304 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMENT
,
3305 (bfd_vma
) sizeof (Elf_External_Sym
)))
3310 /* The backend must work out the sizes of all the other dynamic
3312 if (bed
->elf_backend_size_dynamic_sections
3313 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
3316 if (elf_hash_table (info
)->dynamic_sections_created
)
3318 bfd_size_type dynsymcount
;
3320 size_t bucketcount
= 0;
3321 size_t hash_entry_size
;
3322 unsigned int dtagcount
;
3324 /* Set up the version definition section. */
3325 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3326 BFD_ASSERT (s
!= NULL
);
3328 /* We may have created additional version definitions if we are
3329 just linking a regular application. */
3330 verdefs
= asvinfo
.verdefs
;
3332 /* Skip anonymous version tag. */
3333 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
3334 verdefs
= verdefs
->next
;
3336 if (verdefs
== NULL
)
3337 _bfd_strip_section_from_output (info
, s
);
3342 struct bfd_elf_version_tree
*t
;
3344 Elf_Internal_Verdef def
;
3345 Elf_Internal_Verdaux defaux
;
3350 /* Make space for the base version. */
3351 size
+= sizeof (Elf_External_Verdef
);
3352 size
+= sizeof (Elf_External_Verdaux
);
3355 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3357 struct bfd_elf_version_deps
*n
;
3359 size
+= sizeof (Elf_External_Verdef
);
3360 size
+= sizeof (Elf_External_Verdaux
);
3363 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3364 size
+= sizeof (Elf_External_Verdaux
);
3367 s
->_raw_size
= size
;
3368 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3369 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3372 /* Fill in the version definition section. */
3376 def
.vd_version
= VER_DEF_CURRENT
;
3377 def
.vd_flags
= VER_FLG_BASE
;
3380 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3381 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3382 + sizeof (Elf_External_Verdaux
));
3384 if (soname_indx
!= (bfd_size_type
) -1)
3386 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3388 def
.vd_hash
= bfd_elf_hash (soname
);
3389 defaux
.vda_name
= soname_indx
;
3396 name
= basename (output_bfd
->filename
);
3397 def
.vd_hash
= bfd_elf_hash (name
);
3398 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3400 if (indx
== (bfd_size_type
) -1)
3402 defaux
.vda_name
= indx
;
3404 defaux
.vda_next
= 0;
3406 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3407 (Elf_External_Verdef
*) p
);
3408 p
+= sizeof (Elf_External_Verdef
);
3409 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3410 (Elf_External_Verdaux
*) p
);
3411 p
+= sizeof (Elf_External_Verdaux
);
3413 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3416 struct bfd_elf_version_deps
*n
;
3417 struct elf_link_hash_entry
*h
;
3420 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3423 /* Add a symbol representing this version. */
3425 if (! (_bfd_generic_link_add_one_symbol
3426 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
3427 (bfd_vma
) 0, (const char *) NULL
, false,
3428 get_elf_backend_data (dynobj
)->collect
,
3429 (struct bfd_link_hash_entry
**) &h
)))
3431 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
3432 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3433 h
->type
= STT_OBJECT
;
3434 h
->verinfo
.vertree
= t
;
3436 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
3439 def
.vd_version
= VER_DEF_CURRENT
;
3441 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
3442 def
.vd_flags
|= VER_FLG_WEAK
;
3443 def
.vd_ndx
= t
->vernum
+ 1;
3444 def
.vd_cnt
= cdeps
+ 1;
3445 def
.vd_hash
= bfd_elf_hash (t
->name
);
3446 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3447 if (t
->next
!= NULL
)
3448 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3449 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
3453 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3454 (Elf_External_Verdef
*) p
);
3455 p
+= sizeof (Elf_External_Verdef
);
3457 defaux
.vda_name
= h
->dynstr_index
;
3458 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3460 if (t
->deps
== NULL
)
3461 defaux
.vda_next
= 0;
3463 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3464 t
->name_indx
= defaux
.vda_name
;
3466 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3467 (Elf_External_Verdaux
*) p
);
3468 p
+= sizeof (Elf_External_Verdaux
);
3470 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3472 if (n
->version_needed
== NULL
)
3474 /* This can happen if there was an error in the
3476 defaux
.vda_name
= 0;
3480 defaux
.vda_name
= n
->version_needed
->name_indx
;
3481 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3484 if (n
->next
== NULL
)
3485 defaux
.vda_next
= 0;
3487 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3489 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3490 (Elf_External_Verdaux
*) p
);
3491 p
+= sizeof (Elf_External_Verdaux
);
3495 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEF
, (bfd_vma
) 0)
3496 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEFNUM
,
3500 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
3503 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
3505 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS
, info
->flags
))
3512 info
->flags_1
&= ~ (DF_1_INITFIRST
3515 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS_1
,
3520 /* Work out the size of the version reference section. */
3522 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3523 BFD_ASSERT (s
!= NULL
);
3525 struct elf_find_verdep_info sinfo
;
3527 sinfo
.output_bfd
= output_bfd
;
3529 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
3530 if (sinfo
.vers
== 0)
3532 sinfo
.failed
= false;
3534 elf_link_hash_traverse (elf_hash_table (info
),
3535 elf_link_find_version_dependencies
,
3538 if (elf_tdata (output_bfd
)->verref
== NULL
)
3539 _bfd_strip_section_from_output (info
, s
);
3542 Elf_Internal_Verneed
*t
;
3547 /* Build the version definition section. */
3550 for (t
= elf_tdata (output_bfd
)->verref
;
3554 Elf_Internal_Vernaux
*a
;
3556 size
+= sizeof (Elf_External_Verneed
);
3558 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3559 size
+= sizeof (Elf_External_Vernaux
);
3562 s
->_raw_size
= size
;
3563 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3564 if (s
->contents
== NULL
)
3568 for (t
= elf_tdata (output_bfd
)->verref
;
3573 Elf_Internal_Vernaux
*a
;
3577 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3580 t
->vn_version
= VER_NEED_CURRENT
;
3582 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3583 elf_dt_name (t
->vn_bfd
) != NULL
3584 ? elf_dt_name (t
->vn_bfd
)
3585 : basename (t
->vn_bfd
->filename
),
3587 if (indx
== (bfd_size_type
) -1)
3590 t
->vn_aux
= sizeof (Elf_External_Verneed
);
3591 if (t
->vn_nextref
== NULL
)
3594 t
->vn_next
= (sizeof (Elf_External_Verneed
)
3595 + caux
* sizeof (Elf_External_Vernaux
));
3597 _bfd_elf_swap_verneed_out (output_bfd
, t
,
3598 (Elf_External_Verneed
*) p
);
3599 p
+= sizeof (Elf_External_Verneed
);
3601 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3603 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
3604 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3605 a
->vna_nodename
, false);
3606 if (indx
== (bfd_size_type
) -1)
3609 if (a
->vna_nextptr
== NULL
)
3612 a
->vna_next
= sizeof (Elf_External_Vernaux
);
3614 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
3615 (Elf_External_Vernaux
*) p
);
3616 p
+= sizeof (Elf_External_Vernaux
);
3620 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEED
,
3622 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEEDNUM
,
3626 elf_tdata (output_bfd
)->cverrefs
= crefs
;
3630 /* Assign dynsym indicies. In a shared library we generate a
3631 section symbol for each output section, which come first.
3632 Next come all of the back-end allocated local dynamic syms,
3633 followed by the rest of the global symbols. */
3635 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3637 /* Work out the size of the symbol version section. */
3638 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3639 BFD_ASSERT (s
!= NULL
);
3640 if (dynsymcount
== 0
3641 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
3643 _bfd_strip_section_from_output (info
, s
);
3644 /* The DYNSYMCOUNT might have changed if we were going to
3645 output a dynamic symbol table entry for S. */
3646 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3650 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
3651 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3652 if (s
->contents
== NULL
)
3655 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERSYM
, (bfd_vma
) 0))
3659 /* Set the size of the .dynsym and .hash sections. We counted
3660 the number of dynamic symbols in elf_link_add_object_symbols.
3661 We will build the contents of .dynsym and .hash when we build
3662 the final symbol table, because until then we do not know the
3663 correct value to give the symbols. We built the .dynstr
3664 section as we went along in elf_link_add_object_symbols. */
3665 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
3666 BFD_ASSERT (s
!= NULL
);
3667 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
3668 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3669 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3672 if (dynsymcount
!= 0)
3674 Elf_Internal_Sym isym
;
3676 /* The first entry in .dynsym is a dummy symbol. */
3683 elf_swap_symbol_out (output_bfd
, &isym
, (PTR
) s
->contents
, (PTR
) 0);
3686 /* Compute the size of the hashing table. As a side effect this
3687 computes the hash values for all the names we export. */
3688 bucketcount
= compute_bucket_count (info
);
3690 s
= bfd_get_section_by_name (dynobj
, ".hash");
3691 BFD_ASSERT (s
!= NULL
);
3692 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
3693 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
3694 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3695 if (s
->contents
== NULL
)
3698 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) bucketcount
,
3700 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) dynsymcount
,
3701 s
->contents
+ hash_entry_size
);
3703 elf_hash_table (info
)->bucketcount
= bucketcount
;
3705 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
3706 BFD_ASSERT (s
!= NULL
);
3708 elf_finalize_dynstr (output_bfd
, info
);
3710 s
->_raw_size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3712 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
3713 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NULL
, (bfd_vma
) 0))
3720 /* This function is used to adjust offsets into .dynstr for
3721 dynamic symbols. This is called via elf_link_hash_traverse. */
3723 static boolean elf_adjust_dynstr_offsets
3724 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3727 elf_adjust_dynstr_offsets (h
, data
)
3728 struct elf_link_hash_entry
*h
;
3731 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3733 if (h
->root
.type
== bfd_link_hash_warning
)
3734 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3736 if (h
->dynindx
!= -1)
3737 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3741 /* Assign string offsets in .dynstr, update all structures referencing
3745 elf_finalize_dynstr (output_bfd
, info
)
3747 struct bfd_link_info
*info
;
3749 struct elf_link_local_dynamic_entry
*entry
;
3750 struct elf_strtab_hash
*dynstr
= elf_hash_table (info
)->dynstr
;
3751 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
3754 Elf_External_Dyn
*dyncon
, *dynconend
;
3756 _bfd_elf_strtab_finalize (dynstr
);
3757 size
= _bfd_elf_strtab_size (dynstr
);
3759 /* Update all .dynamic entries referencing .dynstr strings. */
3760 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3761 BFD_ASSERT (sdyn
!= NULL
);
3763 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
3764 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
3766 for (; dyncon
< dynconend
; dyncon
++)
3768 Elf_Internal_Dyn dyn
;
3770 elf_swap_dyn_in (dynobj
, dyncon
, & dyn
);
3774 dyn
.d_un
.d_val
= size
;
3775 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3783 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3784 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3791 /* Now update local dynamic symbols. */
3792 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
3793 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3794 entry
->isym
.st_name
);
3796 /* And the rest of dynamic symbols. */
3797 elf_link_hash_traverse (elf_hash_table (info
),
3798 elf_adjust_dynstr_offsets
, dynstr
);
3800 /* Adjust version definitions. */
3801 if (elf_tdata (output_bfd
)->cverdefs
)
3806 Elf_Internal_Verdef def
;
3807 Elf_Internal_Verdaux defaux
;
3809 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3810 p
= (bfd_byte
*) s
->contents
;
3813 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3815 p
+= sizeof (Elf_External_Verdef
);
3816 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3818 _bfd_elf_swap_verdaux_in (output_bfd
,
3819 (Elf_External_Verdaux
*) p
, &defaux
);
3820 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3822 _bfd_elf_swap_verdaux_out (output_bfd
,
3823 &defaux
, (Elf_External_Verdaux
*) p
);
3824 p
+= sizeof (Elf_External_Verdaux
);
3827 while (def
.vd_next
);
3830 /* Adjust version references. */
3831 if (elf_tdata (output_bfd
)->verref
)
3836 Elf_Internal_Verneed need
;
3837 Elf_Internal_Vernaux needaux
;
3839 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3840 p
= (bfd_byte
*) s
->contents
;
3843 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3845 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3846 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3847 (Elf_External_Verneed
*) p
);
3848 p
+= sizeof (Elf_External_Verneed
);
3849 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3851 _bfd_elf_swap_vernaux_in (output_bfd
,
3852 (Elf_External_Vernaux
*) p
, &needaux
);
3853 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3855 _bfd_elf_swap_vernaux_out (output_bfd
,
3857 (Elf_External_Vernaux
*) p
);
3858 p
+= sizeof (Elf_External_Vernaux
);
3861 while (need
.vn_next
);
3867 /* Fix up the flags for a symbol. This handles various cases which
3868 can only be fixed after all the input files are seen. This is
3869 currently called by both adjust_dynamic_symbol and
3870 assign_sym_version, which is unnecessary but perhaps more robust in
3871 the face of future changes. */
3874 elf_fix_symbol_flags (h
, eif
)
3875 struct elf_link_hash_entry
*h
;
3876 struct elf_info_failed
*eif
;
3878 /* If this symbol was mentioned in a non-ELF file, try to set
3879 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3880 permit a non-ELF file to correctly refer to a symbol defined in
3881 an ELF dynamic object. */
3882 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
3884 while (h
->root
.type
== bfd_link_hash_indirect
)
3885 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3887 if (h
->root
.type
!= bfd_link_hash_defined
3888 && h
->root
.type
!= bfd_link_hash_defweak
)
3889 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3890 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3893 if (h
->root
.u
.def
.section
->owner
!= NULL
3894 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3895 == bfd_target_elf_flavour
))
3896 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3897 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3899 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3902 if (h
->dynindx
== -1
3903 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
3904 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
3906 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3915 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3916 was first seen in a non-ELF file. Fortunately, if the symbol
3917 was first seen in an ELF file, we're probably OK unless the
3918 symbol was defined in a non-ELF file. Catch that case here.
3919 FIXME: We're still in trouble if the symbol was first seen in
3920 a dynamic object, and then later in a non-ELF regular object. */
3921 if ((h
->root
.type
== bfd_link_hash_defined
3922 || h
->root
.type
== bfd_link_hash_defweak
)
3923 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3924 && (h
->root
.u
.def
.section
->owner
!= NULL
3925 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3926 != bfd_target_elf_flavour
)
3927 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3928 && (h
->elf_link_hash_flags
3929 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
3930 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3933 /* If this is a final link, and the symbol was defined as a common
3934 symbol in a regular object file, and there was no definition in
3935 any dynamic object, then the linker will have allocated space for
3936 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3937 flag will not have been set. */
3938 if (h
->root
.type
== bfd_link_hash_defined
3939 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3940 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
3941 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3942 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3943 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3945 /* If -Bsymbolic was used (which means to bind references to global
3946 symbols to the definition within the shared object), and this
3947 symbol was defined in a regular object, then it actually doesn't
3948 need a PLT entry, and we can accomplish that by forcing it local.
3949 Likewise, if the symbol has hidden or internal visibility.
3950 FIXME: It might be that we also do not need a PLT for other
3951 non-hidden visibilities, but we would have to tell that to the
3952 backend specifically; we can't just clear PLT-related data here. */
3953 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
3954 && eif
->info
->shared
3955 && is_elf_hash_table (eif
->info
)
3956 && (eif
->info
->symbolic
3957 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3958 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3959 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3961 struct elf_backend_data
*bed
;
3962 boolean force_local
;
3964 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3966 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3967 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
3968 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
3971 /* If this is a weak defined symbol in a dynamic object, and we know
3972 the real definition in the dynamic object, copy interesting flags
3973 over to the real definition. */
3974 if (h
->weakdef
!= NULL
)
3976 struct elf_link_hash_entry
*weakdef
;
3978 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3979 || h
->root
.type
== bfd_link_hash_defweak
);
3980 weakdef
= h
->weakdef
;
3981 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
3982 || weakdef
->root
.type
== bfd_link_hash_defweak
);
3983 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
3985 /* If the real definition is defined by a regular object file,
3986 don't do anything special. See the longer description in
3987 elf_adjust_dynamic_symbol, below. */
3988 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3992 struct elf_backend_data
*bed
;
3994 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3995 (*bed
->elf_backend_copy_indirect_symbol
) (weakdef
, h
);
4002 /* Make the backend pick a good value for a dynamic symbol. This is
4003 called via elf_link_hash_traverse, and also calls itself
4007 elf_adjust_dynamic_symbol (h
, data
)
4008 struct elf_link_hash_entry
*h
;
4011 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
4013 struct elf_backend_data
*bed
;
4015 if (h
->root
.type
== bfd_link_hash_warning
)
4017 h
->plt
.offset
= (bfd_vma
) -1;
4018 h
->got
.offset
= (bfd_vma
) -1;
4020 /* When warning symbols are created, they **replace** the "real"
4021 entry in the hash table, thus we never get to see the real
4022 symbol in a hash traversal. So look at it now. */
4023 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4026 /* Ignore indirect symbols. These are added by the versioning code. */
4027 if (h
->root
.type
== bfd_link_hash_indirect
)
4030 if (! is_elf_hash_table (eif
->info
))
4033 /* Fix the symbol flags. */
4034 if (! elf_fix_symbol_flags (h
, eif
))
4037 /* If this symbol does not require a PLT entry, and it is not
4038 defined by a dynamic object, or is not referenced by a regular
4039 object, ignore it. We do have to handle a weak defined symbol,
4040 even if no regular object refers to it, if we decided to add it
4041 to the dynamic symbol table. FIXME: Do we normally need to worry
4042 about symbols which are defined by one dynamic object and
4043 referenced by another one? */
4044 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
4045 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4046 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4047 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
4048 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
4050 h
->plt
.offset
= (bfd_vma
) -1;
4054 /* If we've already adjusted this symbol, don't do it again. This
4055 can happen via a recursive call. */
4056 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
4059 /* Don't look at this symbol again. Note that we must set this
4060 after checking the above conditions, because we may look at a
4061 symbol once, decide not to do anything, and then get called
4062 recursively later after REF_REGULAR is set below. */
4063 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
4065 /* If this is a weak definition, and we know a real definition, and
4066 the real symbol is not itself defined by a regular object file,
4067 then get a good value for the real definition. We handle the
4068 real symbol first, for the convenience of the backend routine.
4070 Note that there is a confusing case here. If the real definition
4071 is defined by a regular object file, we don't get the real symbol
4072 from the dynamic object, but we do get the weak symbol. If the
4073 processor backend uses a COPY reloc, then if some routine in the
4074 dynamic object changes the real symbol, we will not see that
4075 change in the corresponding weak symbol. This is the way other
4076 ELF linkers work as well, and seems to be a result of the shared
4079 I will clarify this issue. Most SVR4 shared libraries define the
4080 variable _timezone and define timezone as a weak synonym. The
4081 tzset call changes _timezone. If you write
4082 extern int timezone;
4084 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4085 you might expect that, since timezone is a synonym for _timezone,
4086 the same number will print both times. However, if the processor
4087 backend uses a COPY reloc, then actually timezone will be copied
4088 into your process image, and, since you define _timezone
4089 yourself, _timezone will not. Thus timezone and _timezone will
4090 wind up at different memory locations. The tzset call will set
4091 _timezone, leaving timezone unchanged. */
4093 if (h
->weakdef
!= NULL
)
4095 /* If we get to this point, we know there is an implicit
4096 reference by a regular object file via the weak symbol H.
4097 FIXME: Is this really true? What if the traversal finds
4098 H->WEAKDEF before it finds H? */
4099 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
4101 if (! elf_adjust_dynamic_symbol (h
->weakdef
, (PTR
) eif
))
4105 /* If a symbol has no type and no size and does not require a PLT
4106 entry, then we are probably about to do the wrong thing here: we
4107 are probably going to create a COPY reloc for an empty object.
4108 This case can arise when a shared object is built with assembly
4109 code, and the assembly code fails to set the symbol type. */
4111 && h
->type
== STT_NOTYPE
4112 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
4113 (*_bfd_error_handler
)
4114 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4115 h
->root
.root
.string
);
4117 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
4118 bed
= get_elf_backend_data (dynobj
);
4119 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
4128 /* This routine is used to export all defined symbols into the dynamic
4129 symbol table. It is called via elf_link_hash_traverse. */
4132 elf_export_symbol (h
, data
)
4133 struct elf_link_hash_entry
*h
;
4136 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
4138 /* Ignore indirect symbols. These are added by the versioning code. */
4139 if (h
->root
.type
== bfd_link_hash_indirect
)
4142 if (h
->root
.type
== bfd_link_hash_warning
)
4143 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4145 if (h
->dynindx
== -1
4146 && (h
->elf_link_hash_flags
4147 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
4149 struct bfd_elf_version_tree
*t
;
4150 struct bfd_elf_version_expr
*d
;
4152 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
4154 if (t
->globals
!= NULL
)
4156 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4158 if ((*d
->match
) (d
, h
->root
.root
.string
))
4163 if (t
->locals
!= NULL
)
4165 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4167 if ((*d
->match
) (d
, h
->root
.root
.string
))
4176 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
4187 /* Look through the symbols which are defined in other shared
4188 libraries and referenced here. Update the list of version
4189 dependencies. This will be put into the .gnu.version_r section.
4190 This function is called via elf_link_hash_traverse. */
4193 elf_link_find_version_dependencies (h
, data
)
4194 struct elf_link_hash_entry
*h
;
4197 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
4198 Elf_Internal_Verneed
*t
;
4199 Elf_Internal_Vernaux
*a
;
4202 if (h
->root
.type
== bfd_link_hash_warning
)
4203 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4205 /* We only care about symbols defined in shared objects with version
4207 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4208 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4210 || h
->verinfo
.verdef
== NULL
)
4213 /* See if we already know about this version. */
4214 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
4216 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
4219 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4220 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
4226 /* This is a new version. Add it to tree we are building. */
4231 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4234 rinfo
->failed
= true;
4238 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
4239 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
4240 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
4244 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4246 /* Note that we are copying a string pointer here, and testing it
4247 above. If bfd_elf_string_from_elf_section is ever changed to
4248 discard the string data when low in memory, this will have to be
4250 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
4252 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
4253 a
->vna_nextptr
= t
->vn_auxptr
;
4255 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
4258 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4265 /* Figure out appropriate versions for all the symbols. We may not
4266 have the version number script until we have read all of the input
4267 files, so until that point we don't know which symbols should be
4268 local. This function is called via elf_link_hash_traverse. */
4271 elf_link_assign_sym_version (h
, data
)
4272 struct elf_link_hash_entry
*h
;
4275 struct elf_assign_sym_version_info
*sinfo
;
4276 struct bfd_link_info
*info
;
4277 struct elf_backend_data
*bed
;
4278 struct elf_info_failed eif
;
4282 sinfo
= (struct elf_assign_sym_version_info
*) data
;
4285 if (h
->root
.type
== bfd_link_hash_warning
)
4286 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4288 /* Fix the symbol flags. */
4291 if (! elf_fix_symbol_flags (h
, &eif
))
4294 sinfo
->failed
= true;
4298 /* We only need version numbers for symbols defined in regular
4300 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4303 bed
= get_elf_backend_data (sinfo
->output_bfd
);
4304 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4305 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
4307 struct bfd_elf_version_tree
*t
;
4312 /* There are two consecutive ELF_VER_CHR characters if this is
4313 not a hidden symbol. */
4315 if (*p
== ELF_VER_CHR
)
4321 /* If there is no version string, we can just return out. */
4325 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4329 /* Look for the version. If we find it, it is no longer weak. */
4330 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4332 if (strcmp (t
->name
, p
) == 0)
4336 struct bfd_elf_version_expr
*d
;
4338 len
= p
- h
->root
.root
.string
;
4339 alc
= bfd_malloc ((bfd_size_type
) len
);
4342 strncpy (alc
, h
->root
.root
.string
, len
- 1);
4343 alc
[len
- 1] = '\0';
4344 if (alc
[len
- 2] == ELF_VER_CHR
)
4345 alc
[len
- 2] = '\0';
4347 h
->verinfo
.vertree
= t
;
4351 if (t
->globals
!= NULL
)
4353 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4354 if ((*d
->match
) (d
, alc
))
4358 /* See if there is anything to force this symbol to
4360 if (d
== NULL
&& t
->locals
!= NULL
)
4362 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4364 if ((*d
->match
) (d
, alc
))
4366 if (h
->dynindx
!= -1
4368 && ! info
->export_dynamic
)
4370 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
4383 /* If we are building an application, we need to create a
4384 version node for this version. */
4385 if (t
== NULL
&& ! info
->shared
)
4387 struct bfd_elf_version_tree
**pp
;
4390 /* If we aren't going to export this symbol, we don't need
4391 to worry about it. */
4392 if (h
->dynindx
== -1)
4396 t
= ((struct bfd_elf_version_tree
*)
4397 bfd_alloc (sinfo
->output_bfd
, amt
));
4400 sinfo
->failed
= true;
4409 t
->name_indx
= (unsigned int) -1;
4413 /* Don't count anonymous version tag. */
4414 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
4416 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4418 t
->vernum
= version_index
;
4422 h
->verinfo
.vertree
= t
;
4426 /* We could not find the version for a symbol when
4427 generating a shared archive. Return an error. */
4428 (*_bfd_error_handler
)
4429 (_("%s: undefined versioned symbol name %s"),
4430 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
4431 bfd_set_error (bfd_error_bad_value
);
4432 sinfo
->failed
= true;
4437 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4440 /* If we don't have a version for this symbol, see if we can find
4442 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
4444 struct bfd_elf_version_tree
*t
;
4445 struct bfd_elf_version_tree
*deflt
;
4446 struct bfd_elf_version_expr
*d
;
4448 /* See if can find what version this symbol is in. If the
4449 symbol is supposed to be local, then don't actually register
4452 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4454 if (t
->globals
!= NULL
)
4456 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4458 if ((*d
->match
) (d
, h
->root
.root
.string
))
4460 h
->verinfo
.vertree
= t
;
4469 if (t
->locals
!= NULL
)
4471 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4473 if (d
->pattern
[0] == '*' && d
->pattern
[1] == '\0')
4475 else if ((*d
->match
) (d
, h
->root
.root
.string
))
4477 h
->verinfo
.vertree
= t
;
4478 if (h
->dynindx
!= -1
4480 && ! info
->export_dynamic
)
4482 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
4493 if (deflt
!= NULL
&& h
->verinfo
.vertree
== NULL
)
4495 h
->verinfo
.vertree
= deflt
;
4496 if (h
->dynindx
!= -1
4498 && ! info
->export_dynamic
)
4500 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
4508 /* Final phase of ELF linker. */
4510 /* A structure we use to avoid passing large numbers of arguments. */
4512 struct elf_final_link_info
4514 /* General link information. */
4515 struct bfd_link_info
*info
;
4518 /* Symbol string table. */
4519 struct bfd_strtab_hash
*symstrtab
;
4520 /* .dynsym section. */
4521 asection
*dynsym_sec
;
4522 /* .hash section. */
4524 /* symbol version section (.gnu.version). */
4525 asection
*symver_sec
;
4526 /* first SHF_TLS section (if any). */
4527 asection
*first_tls_sec
;
4528 /* Buffer large enough to hold contents of any section. */
4530 /* Buffer large enough to hold external relocs of any section. */
4531 PTR external_relocs
;
4532 /* Buffer large enough to hold internal relocs of any section. */
4533 Elf_Internal_Rela
*internal_relocs
;
4534 /* Buffer large enough to hold external local symbols of any input
4536 Elf_External_Sym
*external_syms
;
4537 /* And a buffer for symbol section indices. */
4538 Elf_External_Sym_Shndx
*locsym_shndx
;
4539 /* Buffer large enough to hold internal local symbols of any input
4541 Elf_Internal_Sym
*internal_syms
;
4542 /* Array large enough to hold a symbol index for each local symbol
4543 of any input BFD. */
4545 /* Array large enough to hold a section pointer for each local
4546 symbol of any input BFD. */
4547 asection
**sections
;
4548 /* Buffer to hold swapped out symbols. */
4549 Elf_External_Sym
*symbuf
;
4550 /* And one for symbol section indices. */
4551 Elf_External_Sym_Shndx
*symshndxbuf
;
4552 /* Number of swapped out symbols in buffer. */
4553 size_t symbuf_count
;
4554 /* Number of symbols which fit in symbuf. */
4558 static boolean elf_link_output_sym
4559 PARAMS ((struct elf_final_link_info
*, const char *,
4560 Elf_Internal_Sym
*, asection
*));
4561 static boolean elf_link_flush_output_syms
4562 PARAMS ((struct elf_final_link_info
*));
4563 static boolean elf_link_output_extsym
4564 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4565 static boolean elf_link_sec_merge_syms
4566 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4567 static boolean elf_link_check_versioned_symbol
4568 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
4569 static boolean elf_link_input_bfd
4570 PARAMS ((struct elf_final_link_info
*, bfd
*));
4571 static boolean elf_reloc_link_order
4572 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
4573 struct bfd_link_order
*));
4575 /* This struct is used to pass information to elf_link_output_extsym. */
4577 struct elf_outext_info
4581 struct elf_final_link_info
*finfo
;
4584 /* Compute the size of, and allocate space for, REL_HDR which is the
4585 section header for a section containing relocations for O. */
4588 elf_link_size_reloc_section (abfd
, rel_hdr
, o
)
4590 Elf_Internal_Shdr
*rel_hdr
;
4593 bfd_size_type reloc_count
;
4594 bfd_size_type num_rel_hashes
;
4596 /* Figure out how many relocations there will be. */
4597 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
4598 reloc_count
= elf_section_data (o
)->rel_count
;
4600 reloc_count
= elf_section_data (o
)->rel_count2
;
4602 num_rel_hashes
= o
->reloc_count
;
4603 if (num_rel_hashes
< reloc_count
)
4604 num_rel_hashes
= reloc_count
;
4606 /* That allows us to calculate the size of the section. */
4607 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
4609 /* The contents field must last into write_object_contents, so we
4610 allocate it with bfd_alloc rather than malloc. Also since we
4611 cannot be sure that the contents will actually be filled in,
4612 we zero the allocated space. */
4613 rel_hdr
->contents
= (PTR
) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
4614 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
4617 /* We only allocate one set of hash entries, so we only do it the
4618 first time we are called. */
4619 if (elf_section_data (o
)->rel_hashes
== NULL
4622 struct elf_link_hash_entry
**p
;
4624 p
= ((struct elf_link_hash_entry
**)
4625 bfd_zmalloc (num_rel_hashes
4626 * sizeof (struct elf_link_hash_entry
*)));
4630 elf_section_data (o
)->rel_hashes
= p
;
4636 /* When performing a relocateable link, the input relocations are
4637 preserved. But, if they reference global symbols, the indices
4638 referenced must be updated. Update all the relocations in
4639 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4642 elf_link_adjust_relocs (abfd
, rel_hdr
, count
, rel_hash
)
4644 Elf_Internal_Shdr
*rel_hdr
;
4646 struct elf_link_hash_entry
**rel_hash
;
4649 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4650 Elf_Internal_Rel
*irel
;
4651 Elf_Internal_Rela
*irela
;
4652 bfd_size_type amt
= sizeof (Elf_Internal_Rel
) * bed
->s
->int_rels_per_ext_rel
;
4654 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (amt
);
4657 (*_bfd_error_handler
) (_("Error: out of memory"));
4661 amt
= sizeof (Elf_Internal_Rela
) * bed
->s
->int_rels_per_ext_rel
;
4662 irela
= (Elf_Internal_Rela
*) bfd_zmalloc (amt
);
4665 (*_bfd_error_handler
) (_("Error: out of memory"));
4669 for (i
= 0; i
< count
; i
++, rel_hash
++)
4671 if (*rel_hash
== NULL
)
4674 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
4676 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4678 Elf_External_Rel
*erel
;
4681 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
4682 if (bed
->s
->swap_reloc_in
)
4683 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
4685 elf_swap_reloc_in (abfd
, erel
, irel
);
4687 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4688 irel
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4689 ELF_R_TYPE (irel
[j
].r_info
));
4691 if (bed
->s
->swap_reloc_out
)
4692 (*bed
->s
->swap_reloc_out
) (abfd
, irel
, (bfd_byte
*) erel
);
4694 elf_swap_reloc_out (abfd
, irel
, erel
);
4698 Elf_External_Rela
*erela
;
4701 BFD_ASSERT (rel_hdr
->sh_entsize
4702 == sizeof (Elf_External_Rela
));
4704 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
4705 if (bed
->s
->swap_reloca_in
)
4706 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
4708 elf_swap_reloca_in (abfd
, erela
, irela
);
4710 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4711 irela
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4712 ELF_R_TYPE (irela
[j
].r_info
));
4714 if (bed
->s
->swap_reloca_out
)
4715 (*bed
->s
->swap_reloca_out
) (abfd
, irela
, (bfd_byte
*) erela
);
4717 elf_swap_reloca_out (abfd
, irela
, erela
);
4725 struct elf_link_sort_rela
{
4727 enum elf_reloc_type_class type
;
4729 Elf_Internal_Rel rel
;
4730 Elf_Internal_Rela rela
;
4735 elf_link_sort_cmp1 (A
, B
)
4739 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4740 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4741 int relativea
, relativeb
;
4743 relativea
= a
->type
== reloc_class_relative
;
4744 relativeb
= b
->type
== reloc_class_relative
;
4746 if (relativea
< relativeb
)
4748 if (relativea
> relativeb
)
4750 if (ELF_R_SYM (a
->u
.rel
.r_info
) < ELF_R_SYM (b
->u
.rel
.r_info
))
4752 if (ELF_R_SYM (a
->u
.rel
.r_info
) > ELF_R_SYM (b
->u
.rel
.r_info
))
4754 if (a
->u
.rel
.r_offset
< b
->u
.rel
.r_offset
)
4756 if (a
->u
.rel
.r_offset
> b
->u
.rel
.r_offset
)
4762 elf_link_sort_cmp2 (A
, B
)
4766 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4767 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4770 if (a
->offset
< b
->offset
)
4772 if (a
->offset
> b
->offset
)
4774 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
4775 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
4780 if (a
->u
.rel
.r_offset
< b
->u
.rel
.r_offset
)
4782 if (a
->u
.rel
.r_offset
> b
->u
.rel
.r_offset
)
4788 elf_link_sort_relocs (abfd
, info
, psec
)
4790 struct bfd_link_info
*info
;
4793 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4794 asection
*reldyn
, *o
;
4795 boolean rel
= false;
4796 bfd_size_type count
, size
;
4798 struct elf_link_sort_rela
*rela
;
4799 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4801 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
4802 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4804 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
4805 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4808 count
= reldyn
->_raw_size
/ sizeof (Elf_External_Rel
);
4811 count
= reldyn
->_raw_size
/ sizeof (Elf_External_Rela
);
4814 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4815 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4816 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4817 && o
->output_section
== reldyn
)
4818 size
+= o
->_raw_size
;
4820 if (size
!= reldyn
->_raw_size
)
4823 rela
= (struct elf_link_sort_rela
*) bfd_zmalloc (sizeof (*rela
) * count
);
4826 (*info
->callbacks
->warning
)
4827 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0,
4832 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4833 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4834 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4835 && o
->output_section
== reldyn
)
4839 Elf_External_Rel
*erel
, *erelend
;
4840 struct elf_link_sort_rela
*s
;
4842 erel
= (Elf_External_Rel
*) o
->contents
;
4843 erelend
= (Elf_External_Rel
*) (o
->contents
+ o
->_raw_size
);
4844 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rel
);
4845 for (; erel
< erelend
; erel
++, s
++)
4847 if (bed
->s
->swap_reloc_in
)
4848 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, &s
->u
.rel
);
4850 elf_swap_reloc_in (abfd
, erel
, &s
->u
.rel
);
4852 s
->type
= (*bed
->elf_backend_reloc_type_class
) (&s
->u
.rela
);
4857 Elf_External_Rela
*erela
, *erelaend
;
4858 struct elf_link_sort_rela
*s
;
4860 erela
= (Elf_External_Rela
*) o
->contents
;
4861 erelaend
= (Elf_External_Rela
*) (o
->contents
+ o
->_raw_size
);
4862 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rela
);
4863 for (; erela
< erelaend
; erela
++, s
++)
4865 if (bed
->s
->swap_reloca_in
)
4866 (*bed
->s
->swap_reloca_in
) (dynobj
, (bfd_byte
*) erela
,
4869 elf_swap_reloca_in (dynobj
, erela
, &s
->u
.rela
);
4871 s
->type
= (*bed
->elf_backend_reloc_type_class
) (&s
->u
.rela
);
4876 qsort (rela
, (size_t) count
, sizeof (*rela
), elf_link_sort_cmp1
);
4877 for (ret
= 0; ret
< count
&& rela
[ret
].type
== reloc_class_relative
; ret
++)
4879 for (i
= ret
, j
= ret
; i
< count
; i
++)
4881 if (ELF_R_SYM (rela
[i
].u
.rel
.r_info
) != ELF_R_SYM (rela
[j
].u
.rel
.r_info
))
4883 rela
[i
].offset
= rela
[j
].u
.rel
.r_offset
;
4885 qsort (rela
+ ret
, (size_t) count
- ret
, sizeof (*rela
), elf_link_sort_cmp2
);
4887 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4888 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4889 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4890 && o
->output_section
== reldyn
)
4894 Elf_External_Rel
*erel
, *erelend
;
4895 struct elf_link_sort_rela
*s
;
4897 erel
= (Elf_External_Rel
*) o
->contents
;
4898 erelend
= (Elf_External_Rel
*) (o
->contents
+ o
->_raw_size
);
4899 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rel
);
4900 for (; erel
< erelend
; erel
++, s
++)
4902 if (bed
->s
->swap_reloc_out
)
4903 (*bed
->s
->swap_reloc_out
) (abfd
, &s
->u
.rel
,
4906 elf_swap_reloc_out (abfd
, &s
->u
.rel
, erel
);
4911 Elf_External_Rela
*erela
, *erelaend
;
4912 struct elf_link_sort_rela
*s
;
4914 erela
= (Elf_External_Rela
*) o
->contents
;
4915 erelaend
= (Elf_External_Rela
*) (o
->contents
+ o
->_raw_size
);
4916 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rela
);
4917 for (; erela
< erelaend
; erela
++, s
++)
4919 if (bed
->s
->swap_reloca_out
)
4920 (*bed
->s
->swap_reloca_out
) (dynobj
, &s
->u
.rela
,
4921 (bfd_byte
*) erela
);
4923 elf_swap_reloca_out (dynobj
, &s
->u
.rela
, erela
);
4933 /* Do the final step of an ELF link. */
4936 elf_bfd_final_link (abfd
, info
)
4938 struct bfd_link_info
*info
;
4941 boolean emit_relocs
;
4943 struct elf_final_link_info finfo
;
4944 register asection
*o
;
4945 register struct bfd_link_order
*p
;
4947 bfd_size_type max_contents_size
;
4948 bfd_size_type max_external_reloc_size
;
4949 bfd_size_type max_internal_reloc_count
;
4950 bfd_size_type max_sym_count
;
4951 bfd_size_type max_sym_shndx_count
;
4953 Elf_Internal_Sym elfsym
;
4955 Elf_Internal_Shdr
*symtab_hdr
;
4956 Elf_Internal_Shdr
*symstrtab_hdr
;
4957 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4958 struct elf_outext_info eoinfo
;
4960 size_t relativecount
= 0;
4961 asection
*reldyn
= 0;
4964 if (! is_elf_hash_table (info
))
4968 abfd
->flags
|= DYNAMIC
;
4970 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
4971 dynobj
= elf_hash_table (info
)->dynobj
;
4973 emit_relocs
= (info
->relocateable
4974 || info
->emitrelocations
4975 || bed
->elf_backend_emit_relocs
);
4978 finfo
.output_bfd
= abfd
;
4979 finfo
.symstrtab
= elf_stringtab_init ();
4980 if (finfo
.symstrtab
== NULL
)
4985 finfo
.dynsym_sec
= NULL
;
4986 finfo
.hash_sec
= NULL
;
4987 finfo
.symver_sec
= NULL
;
4991 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
4992 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
4993 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
4994 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
4995 /* Note that it is OK if symver_sec is NULL. */
4998 finfo
.contents
= NULL
;
4999 finfo
.external_relocs
= NULL
;
5000 finfo
.internal_relocs
= NULL
;
5001 finfo
.external_syms
= NULL
;
5002 finfo
.locsym_shndx
= NULL
;
5003 finfo
.internal_syms
= NULL
;
5004 finfo
.indices
= NULL
;
5005 finfo
.sections
= NULL
;
5006 finfo
.symbuf
= NULL
;
5007 finfo
.symshndxbuf
= NULL
;
5008 finfo
.symbuf_count
= 0;
5009 finfo
.first_tls_sec
= NULL
;
5010 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
5011 if ((o
->flags
& SEC_THREAD_LOCAL
) != 0
5012 && (o
->flags
& SEC_LOAD
) != 0)
5014 finfo
.first_tls_sec
= o
;
5018 /* Count up the number of relocations we will output for each output
5019 section, so that we know the sizes of the reloc sections. We
5020 also figure out some maximum sizes. */
5021 max_contents_size
= 0;
5022 max_external_reloc_size
= 0;
5023 max_internal_reloc_count
= 0;
5025 max_sym_shndx_count
= 0;
5027 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
5031 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5033 if (p
->type
== bfd_section_reloc_link_order
5034 || p
->type
== bfd_symbol_reloc_link_order
)
5036 else if (p
->type
== bfd_indirect_link_order
)
5040 sec
= p
->u
.indirect
.section
;
5042 /* Mark all sections which are to be included in the
5043 link. This will normally be every section. We need
5044 to do this so that we can identify any sections which
5045 the linker has decided to not include. */
5046 sec
->linker_mark
= true;
5048 if (sec
->flags
& SEC_MERGE
)
5051 if (info
->relocateable
|| info
->emitrelocations
)
5052 o
->reloc_count
+= sec
->reloc_count
;
5053 else if (bed
->elf_backend_count_relocs
)
5055 Elf_Internal_Rela
* relocs
;
5057 relocs
= (NAME(_bfd_elf
,link_read_relocs
)
5058 (abfd
, sec
, (PTR
) NULL
,
5059 (Elf_Internal_Rela
*) NULL
, info
->keep_memory
));
5062 += (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
5064 if (!info
->keep_memory
)
5068 if (sec
->_raw_size
> max_contents_size
)
5069 max_contents_size
= sec
->_raw_size
;
5070 if (sec
->_cooked_size
> max_contents_size
)
5071 max_contents_size
= sec
->_cooked_size
;
5073 /* We are interested in just local symbols, not all
5075 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
5076 && (sec
->owner
->flags
& DYNAMIC
) == 0)
5080 if (elf_bad_symtab (sec
->owner
))
5081 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
5082 / sizeof (Elf_External_Sym
));
5084 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
5086 if (sym_count
> max_sym_count
)
5087 max_sym_count
= sym_count
;
5089 if (sym_count
> max_sym_shndx_count
5090 && elf_symtab_shndx (sec
->owner
) != 0)
5091 max_sym_shndx_count
= sym_count
;
5093 if ((sec
->flags
& SEC_RELOC
) != 0)
5097 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
5098 if (ext_size
> max_external_reloc_size
)
5099 max_external_reloc_size
= ext_size
;
5100 if (sec
->reloc_count
> max_internal_reloc_count
)
5101 max_internal_reloc_count
= sec
->reloc_count
;
5107 if (o
->reloc_count
> 0)
5108 o
->flags
|= SEC_RELOC
;
5111 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5112 set it (this is probably a bug) and if it is set
5113 assign_section_numbers will create a reloc section. */
5114 o
->flags
&=~ SEC_RELOC
;
5117 /* If the SEC_ALLOC flag is not set, force the section VMA to
5118 zero. This is done in elf_fake_sections as well, but forcing
5119 the VMA to 0 here will ensure that relocs against these
5120 sections are handled correctly. */
5121 if ((o
->flags
& SEC_ALLOC
) == 0
5122 && ! o
->user_set_vma
)
5126 if (! info
->relocateable
&& merged
)
5127 elf_link_hash_traverse (elf_hash_table (info
),
5128 elf_link_sec_merge_syms
, (PTR
) abfd
);
5130 /* Figure out the file positions for everything but the symbol table
5131 and the relocs. We set symcount to force assign_section_numbers
5132 to create a symbol table. */
5133 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
5134 BFD_ASSERT (! abfd
->output_has_begun
);
5135 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
5138 /* Figure out how many relocations we will have in each section.
5139 Just using RELOC_COUNT isn't good enough since that doesn't
5140 maintain a separate value for REL vs. RELA relocations. */
5142 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5143 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5145 asection
*output_section
;
5147 if (! o
->linker_mark
)
5149 /* This section was omitted from the link. */
5153 output_section
= o
->output_section
;
5155 if (output_section
!= NULL
5156 && (o
->flags
& SEC_RELOC
) != 0)
5158 struct bfd_elf_section_data
*esdi
5159 = elf_section_data (o
);
5160 struct bfd_elf_section_data
*esdo
5161 = elf_section_data (output_section
);
5162 unsigned int *rel_count
;
5163 unsigned int *rel_count2
;
5164 bfd_size_type entsize
;
5165 bfd_size_type entsize2
;
5167 /* We must be careful to add the relocations from the
5168 input section to the right output count. */
5169 entsize
= esdi
->rel_hdr
.sh_entsize
;
5170 entsize2
= esdi
->rel_hdr2
? esdi
->rel_hdr2
->sh_entsize
: 0;
5171 BFD_ASSERT ((entsize
== sizeof (Elf_External_Rel
)
5172 || entsize
== sizeof (Elf_External_Rela
))
5173 && entsize2
!= entsize
5175 || entsize2
== sizeof (Elf_External_Rel
)
5176 || entsize2
== sizeof (Elf_External_Rela
)));
5177 if (entsize
== esdo
->rel_hdr
.sh_entsize
)
5179 rel_count
= &esdo
->rel_count
;
5180 rel_count2
= &esdo
->rel_count2
;
5184 rel_count
= &esdo
->rel_count2
;
5185 rel_count2
= &esdo
->rel_count
;
5188 *rel_count
+= NUM_SHDR_ENTRIES (& esdi
->rel_hdr
);
5190 *rel_count2
+= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
5191 output_section
->flags
|= SEC_RELOC
;
5195 /* That created the reloc sections. Set their sizes, and assign
5196 them file positions, and allocate some buffers. */
5197 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5199 if ((o
->flags
& SEC_RELOC
) != 0)
5201 if (!elf_link_size_reloc_section (abfd
,
5202 &elf_section_data (o
)->rel_hdr
,
5206 if (elf_section_data (o
)->rel_hdr2
5207 && !elf_link_size_reloc_section (abfd
,
5208 elf_section_data (o
)->rel_hdr2
,
5213 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
5214 to count upwards while actually outputting the relocations. */
5215 elf_section_data (o
)->rel_count
= 0;
5216 elf_section_data (o
)->rel_count2
= 0;
5219 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5221 /* We have now assigned file positions for all the sections except
5222 .symtab and .strtab. We start the .symtab section at the current
5223 file position, and write directly to it. We build the .strtab
5224 section in memory. */
5225 bfd_get_symcount (abfd
) = 0;
5226 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5227 /* sh_name is set in prep_headers. */
5228 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5229 symtab_hdr
->sh_flags
= 0;
5230 symtab_hdr
->sh_addr
= 0;
5231 symtab_hdr
->sh_size
= 0;
5232 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
5233 /* sh_link is set in assign_section_numbers. */
5234 /* sh_info is set below. */
5235 /* sh_offset is set just below. */
5236 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
5238 off
= elf_tdata (abfd
)->next_file_pos
;
5239 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
5241 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5242 incorrect. We do not yet know the size of the .symtab section.
5243 We correct next_file_pos below, after we do know the size. */
5245 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5246 continuously seeking to the right position in the file. */
5247 if (! info
->keep_memory
|| max_sym_count
< 20)
5248 finfo
.symbuf_size
= 20;
5250 finfo
.symbuf_size
= max_sym_count
;
5251 amt
= finfo
.symbuf_size
;
5252 amt
*= sizeof (Elf_External_Sym
);
5253 finfo
.symbuf
= (Elf_External_Sym
*) bfd_malloc (amt
);
5254 if (finfo
.symbuf
== NULL
)
5256 if (elf_numsections (abfd
) > SHN_LORESERVE
)
5258 amt
= finfo
.symbuf_size
;
5259 amt
*= sizeof (Elf_External_Sym_Shndx
);
5260 finfo
.symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
5261 if (finfo
.symshndxbuf
== NULL
)
5265 /* Start writing out the symbol table. The first symbol is always a
5267 if (info
->strip
!= strip_all
5270 elfsym
.st_value
= 0;
5273 elfsym
.st_other
= 0;
5274 elfsym
.st_shndx
= SHN_UNDEF
;
5275 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5276 &elfsym
, bfd_und_section_ptr
))
5281 /* Some standard ELF linkers do this, but we don't because it causes
5282 bootstrap comparison failures. */
5283 /* Output a file symbol for the output file as the second symbol.
5284 We output this even if we are discarding local symbols, although
5285 I'm not sure if this is correct. */
5286 elfsym
.st_value
= 0;
5288 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5289 elfsym
.st_other
= 0;
5290 elfsym
.st_shndx
= SHN_ABS
;
5291 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
5292 &elfsym
, bfd_abs_section_ptr
))
5296 /* Output a symbol for each section. We output these even if we are
5297 discarding local symbols, since they are used for relocs. These
5298 symbols have no names. We store the index of each one in the
5299 index field of the section, so that we can find it again when
5300 outputting relocs. */
5301 if (info
->strip
!= strip_all
5305 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5306 elfsym
.st_other
= 0;
5307 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5309 o
= section_from_elf_index (abfd
, i
);
5311 o
->target_index
= bfd_get_symcount (abfd
);
5312 elfsym
.st_shndx
= i
;
5313 if (info
->relocateable
|| o
== NULL
)
5314 elfsym
.st_value
= 0;
5316 elfsym
.st_value
= o
->vma
;
5317 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5320 if (i
== SHN_LORESERVE
)
5321 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
5325 /* Allocate some memory to hold information read in from the input
5327 if (max_contents_size
!= 0)
5329 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
5330 if (finfo
.contents
== NULL
)
5334 if (max_external_reloc_size
!= 0)
5336 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
5337 if (finfo
.external_relocs
== NULL
)
5341 if (max_internal_reloc_count
!= 0)
5343 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5344 amt
*= sizeof (Elf_Internal_Rela
);
5345 finfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
5346 if (finfo
.internal_relocs
== NULL
)
5350 if (max_sym_count
!= 0)
5352 amt
= max_sym_count
* sizeof (Elf_External_Sym
);
5353 finfo
.external_syms
= (Elf_External_Sym
*) bfd_malloc (amt
);
5354 if (finfo
.external_syms
== NULL
)
5357 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
5358 finfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
5359 if (finfo
.internal_syms
== NULL
)
5362 amt
= max_sym_count
* sizeof (long);
5363 finfo
.indices
= (long *) bfd_malloc (amt
);
5364 if (finfo
.indices
== NULL
)
5367 amt
= max_sym_count
* sizeof (asection
*);
5368 finfo
.sections
= (asection
**) bfd_malloc (amt
);
5369 if (finfo
.sections
== NULL
)
5373 if (max_sym_shndx_count
!= 0)
5375 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
5376 finfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
5377 if (finfo
.locsym_shndx
== NULL
)
5381 if (finfo
.first_tls_sec
)
5383 unsigned int align
= 0;
5384 bfd_vma base
= finfo
.first_tls_sec
->vma
, end
= 0;
5387 for (sec
= finfo
.first_tls_sec
;
5388 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
5391 bfd_vma size
= sec
->_raw_size
;
5393 if (bfd_get_section_alignment (abfd
, sec
) > align
)
5394 align
= bfd_get_section_alignment (abfd
, sec
);
5395 if (sec
->_raw_size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
5397 struct bfd_link_order
*o
;
5400 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
5401 if (size
< o
->offset
+ o
->size
)
5402 size
= o
->offset
+ o
->size
;
5404 end
= sec
->vma
+ size
;
5406 elf_hash_table (info
)->tls_segment
5407 = bfd_zalloc (abfd
, sizeof (struct elf_link_tls_segment
));
5408 if (elf_hash_table (info
)->tls_segment
== NULL
)
5410 elf_hash_table (info
)->tls_segment
->start
= base
;
5411 elf_hash_table (info
)->tls_segment
->size
= end
- base
;
5412 elf_hash_table (info
)->tls_segment
->align
= align
;
5415 /* Since ELF permits relocations to be against local symbols, we
5416 must have the local symbols available when we do the relocations.
5417 Since we would rather only read the local symbols once, and we
5418 would rather not keep them in memory, we handle all the
5419 relocations for a single input file at the same time.
5421 Unfortunately, there is no way to know the total number of local
5422 symbols until we have seen all of them, and the local symbol
5423 indices precede the global symbol indices. This means that when
5424 we are generating relocateable output, and we see a reloc against
5425 a global symbol, we can not know the symbol index until we have
5426 finished examining all the local symbols to see which ones we are
5427 going to output. To deal with this, we keep the relocations in
5428 memory, and don't output them until the end of the link. This is
5429 an unfortunate waste of memory, but I don't see a good way around
5430 it. Fortunately, it only happens when performing a relocateable
5431 link, which is not the common case. FIXME: If keep_memory is set
5432 we could write the relocs out and then read them again; I don't
5433 know how bad the memory loss will be. */
5435 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5436 sub
->output_has_begun
= false;
5437 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5439 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5441 if (p
->type
== bfd_indirect_link_order
5442 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
5443 == bfd_target_elf_flavour
)
5444 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
5446 if (! sub
->output_has_begun
)
5448 if (! elf_link_input_bfd (&finfo
, sub
))
5450 sub
->output_has_begun
= true;
5453 else if (p
->type
== bfd_section_reloc_link_order
5454 || p
->type
== bfd_symbol_reloc_link_order
)
5456 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
5461 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
5467 /* Output any global symbols that got converted to local in a
5468 version script or due to symbol visibility. We do this in a
5469 separate step since ELF requires all local symbols to appear
5470 prior to any global symbols. FIXME: We should only do this if
5471 some global symbols were, in fact, converted to become local.
5472 FIXME: Will this work correctly with the Irix 5 linker? */
5473 eoinfo
.failed
= false;
5474 eoinfo
.finfo
= &finfo
;
5475 eoinfo
.localsyms
= true;
5476 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5481 /* That wrote out all the local symbols. Finish up the symbol table
5482 with the global symbols. Even if we want to strip everything we
5483 can, we still need to deal with those global symbols that got
5484 converted to local in a version script. */
5486 /* The sh_info field records the index of the first non local symbol. */
5487 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
5490 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
5492 Elf_Internal_Sym sym
;
5493 Elf_External_Sym
*dynsym
=
5494 (Elf_External_Sym
*) finfo
.dynsym_sec
->contents
;
5495 long last_local
= 0;
5497 /* Write out the section symbols for the output sections. */
5504 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5507 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5510 Elf_External_Sym
*dest
;
5512 indx
= elf_section_data (s
)->this_idx
;
5513 BFD_ASSERT (indx
> 0);
5514 sym
.st_shndx
= indx
;
5515 sym
.st_value
= s
->vma
;
5516 dest
= dynsym
+ elf_section_data (s
)->dynindx
;
5517 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5520 last_local
= bfd_count_sections (abfd
);
5523 /* Write out the local dynsyms. */
5524 if (elf_hash_table (info
)->dynlocal
)
5526 struct elf_link_local_dynamic_entry
*e
;
5527 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
5530 Elf_External_Sym
*dest
;
5532 sym
.st_size
= e
->isym
.st_size
;
5533 sym
.st_other
= e
->isym
.st_other
;
5535 /* Copy the internal symbol as is.
5536 Note that we saved a word of storage and overwrote
5537 the original st_name with the dynstr_index. */
5540 if (e
->isym
.st_shndx
!= SHN_UNDEF
5541 && (e
->isym
.st_shndx
< SHN_LORESERVE
5542 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
5544 s
= bfd_section_from_elf_index (e
->input_bfd
,
5548 elf_section_data (s
->output_section
)->this_idx
;
5549 sym
.st_value
= (s
->output_section
->vma
5551 + e
->isym
.st_value
);
5554 if (last_local
< e
->dynindx
)
5555 last_local
= e
->dynindx
;
5557 dest
= dynsym
+ e
->dynindx
;
5558 elf_swap_symbol_out (abfd
, &sym
, (PTR
) dest
, (PTR
) 0);
5562 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
5566 /* We get the global symbols from the hash table. */
5567 eoinfo
.failed
= false;
5568 eoinfo
.localsyms
= false;
5569 eoinfo
.finfo
= &finfo
;
5570 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5575 /* If backend needs to output some symbols not present in the hash
5576 table, do it now. */
5577 if (bed
->elf_backend_output_arch_syms
)
5579 typedef boolean (*out_sym_func
) PARAMS ((PTR
, const char *,
5583 if (! ((*bed
->elf_backend_output_arch_syms
)
5584 (abfd
, info
, (PTR
) &finfo
, (out_sym_func
) elf_link_output_sym
)))
5588 /* Flush all symbols to the file. */
5589 if (! elf_link_flush_output_syms (&finfo
))
5592 /* Now we know the size of the symtab section. */
5593 off
+= symtab_hdr
->sh_size
;
5595 /* Finish up and write out the symbol string table (.strtab)
5597 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5598 /* sh_name was set in prep_headers. */
5599 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5600 symstrtab_hdr
->sh_flags
= 0;
5601 symstrtab_hdr
->sh_addr
= 0;
5602 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
5603 symstrtab_hdr
->sh_entsize
= 0;
5604 symstrtab_hdr
->sh_link
= 0;
5605 symstrtab_hdr
->sh_info
= 0;
5606 /* sh_offset is set just below. */
5607 symstrtab_hdr
->sh_addralign
= 1;
5609 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, true);
5610 elf_tdata (abfd
)->next_file_pos
= off
;
5612 if (bfd_get_symcount (abfd
) > 0)
5614 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
5615 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
5619 /* Adjust the relocs to have the correct symbol indices. */
5620 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5622 if ((o
->flags
& SEC_RELOC
) == 0)
5625 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
5626 elf_section_data (o
)->rel_count
,
5627 elf_section_data (o
)->rel_hashes
);
5628 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
5629 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
5630 elf_section_data (o
)->rel_count2
,
5631 (elf_section_data (o
)->rel_hashes
5632 + elf_section_data (o
)->rel_count
));
5634 /* Set the reloc_count field to 0 to prevent write_relocs from
5635 trying to swap the relocs out itself. */
5639 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
5640 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
5642 /* If we are linking against a dynamic object, or generating a
5643 shared library, finish up the dynamic linking information. */
5646 Elf_External_Dyn
*dyncon
, *dynconend
;
5648 /* Fix up .dynamic entries. */
5649 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
5650 BFD_ASSERT (o
!= NULL
);
5652 dyncon
= (Elf_External_Dyn
*) o
->contents
;
5653 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
5654 for (; dyncon
< dynconend
; dyncon
++)
5656 Elf_Internal_Dyn dyn
;
5660 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
5667 if (relativecount
> 0 && dyncon
+ 1 < dynconend
)
5669 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
5671 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
5672 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
5675 if (dyn
.d_tag
!= DT_NULL
)
5677 dyn
.d_un
.d_val
= relativecount
;
5678 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5684 name
= info
->init_function
;
5687 name
= info
->fini_function
;
5690 struct elf_link_hash_entry
*h
;
5692 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
5693 false, false, true);
5695 && (h
->root
.type
== bfd_link_hash_defined
5696 || h
->root
.type
== bfd_link_hash_defweak
))
5698 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
5699 o
= h
->root
.u
.def
.section
;
5700 if (o
->output_section
!= NULL
)
5701 dyn
.d_un
.d_val
+= (o
->output_section
->vma
5702 + o
->output_offset
);
5705 /* The symbol is imported from another shared
5706 library and does not apply to this one. */
5710 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5715 case DT_PREINIT_ARRAYSZ
:
5716 name
= ".preinit_array";
5718 case DT_INIT_ARRAYSZ
:
5719 name
= ".init_array";
5721 case DT_FINI_ARRAYSZ
:
5722 name
= ".fini_array";
5724 o
= bfd_get_section_by_name (abfd
, name
);
5727 (*_bfd_error_handler
)
5728 (_("%s: could not find output section %s"),
5729 bfd_get_filename (abfd
), name
);
5732 if (o
->_raw_size
== 0)
5733 (*_bfd_error_handler
)
5734 (_("warning: %s section has zero size"), name
);
5735 dyn
.d_un
.d_val
= o
->_raw_size
;
5736 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5739 case DT_PREINIT_ARRAY
:
5740 name
= ".preinit_array";
5743 name
= ".init_array";
5746 name
= ".fini_array";
5759 name
= ".gnu.version_d";
5762 name
= ".gnu.version_r";
5765 name
= ".gnu.version";
5767 o
= bfd_get_section_by_name (abfd
, name
);
5770 (*_bfd_error_handler
)
5771 (_("%s: could not find output section %s"),
5772 bfd_get_filename (abfd
), name
);
5775 dyn
.d_un
.d_ptr
= o
->vma
;
5776 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5783 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
5788 for (i
= 1; i
< elf_numsections (abfd
); i
++)
5790 Elf_Internal_Shdr
*hdr
;
5792 hdr
= elf_elfsections (abfd
)[i
];
5793 if (hdr
->sh_type
== type
5794 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
5796 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
5797 dyn
.d_un
.d_val
+= hdr
->sh_size
;
5800 if (dyn
.d_un
.d_val
== 0
5801 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
5802 dyn
.d_un
.d_val
= hdr
->sh_addr
;
5806 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5812 /* If we have created any dynamic sections, then output them. */
5815 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
5818 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
5820 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5821 || o
->_raw_size
== 0
5822 || o
->output_section
== bfd_abs_section_ptr
)
5824 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
5826 /* At this point, we are only interested in sections
5827 created by elf_link_create_dynamic_sections. */
5830 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
5832 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
5834 if (! bfd_set_section_contents (abfd
, o
->output_section
,
5836 (file_ptr
) o
->output_offset
,
5842 /* The contents of the .dynstr section are actually in a
5844 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
5845 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
5846 || ! _bfd_elf_strtab_emit (abfd
,
5847 elf_hash_table (info
)->dynstr
))
5853 if (info
->relocateable
)
5855 boolean failed
= false;
5857 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
5862 /* If we have optimized stabs strings, output them. */
5863 if (elf_hash_table (info
)->stab_info
!= NULL
)
5865 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
5869 if (info
->eh_frame_hdr
&& elf_hash_table (info
)->dynobj
)
5871 o
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
5874 && (elf_section_data (o
)->sec_info_type
5875 == ELF_INFO_TYPE_EH_FRAME_HDR
))
5877 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, o
))
5882 if (finfo
.symstrtab
!= NULL
)
5883 _bfd_stringtab_free (finfo
.symstrtab
);
5884 if (finfo
.contents
!= NULL
)
5885 free (finfo
.contents
);
5886 if (finfo
.external_relocs
!= NULL
)
5887 free (finfo
.external_relocs
);
5888 if (finfo
.internal_relocs
!= NULL
)
5889 free (finfo
.internal_relocs
);
5890 if (finfo
.external_syms
!= NULL
)
5891 free (finfo
.external_syms
);
5892 if (finfo
.locsym_shndx
!= NULL
)
5893 free (finfo
.locsym_shndx
);
5894 if (finfo
.internal_syms
!= NULL
)
5895 free (finfo
.internal_syms
);
5896 if (finfo
.indices
!= NULL
)
5897 free (finfo
.indices
);
5898 if (finfo
.sections
!= NULL
)
5899 free (finfo
.sections
);
5900 if (finfo
.symbuf
!= NULL
)
5901 free (finfo
.symbuf
);
5902 if (finfo
.symshndxbuf
!= NULL
)
5903 free (finfo
.symbuf
);
5904 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5906 if ((o
->flags
& SEC_RELOC
) != 0
5907 && elf_section_data (o
)->rel_hashes
!= NULL
)
5908 free (elf_section_data (o
)->rel_hashes
);
5911 elf_tdata (abfd
)->linker
= true;
5916 if (finfo
.symstrtab
!= NULL
)
5917 _bfd_stringtab_free (finfo
.symstrtab
);
5918 if (finfo
.contents
!= NULL
)
5919 free (finfo
.contents
);
5920 if (finfo
.external_relocs
!= NULL
)
5921 free (finfo
.external_relocs
);
5922 if (finfo
.internal_relocs
!= NULL
)
5923 free (finfo
.internal_relocs
);
5924 if (finfo
.external_syms
!= NULL
)
5925 free (finfo
.external_syms
);
5926 if (finfo
.locsym_shndx
!= NULL
)
5927 free (finfo
.locsym_shndx
);
5928 if (finfo
.internal_syms
!= NULL
)
5929 free (finfo
.internal_syms
);
5930 if (finfo
.indices
!= NULL
)
5931 free (finfo
.indices
);
5932 if (finfo
.sections
!= NULL
)
5933 free (finfo
.sections
);
5934 if (finfo
.symbuf
!= NULL
)
5935 free (finfo
.symbuf
);
5936 if (finfo
.symshndxbuf
!= NULL
)
5937 free (finfo
.symbuf
);
5938 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5940 if ((o
->flags
& SEC_RELOC
) != 0
5941 && elf_section_data (o
)->rel_hashes
!= NULL
)
5942 free (elf_section_data (o
)->rel_hashes
);
5948 /* Add a symbol to the output symbol table. */
5951 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
5952 struct elf_final_link_info
*finfo
;
5954 Elf_Internal_Sym
*elfsym
;
5955 asection
*input_sec
;
5957 Elf_External_Sym
*dest
;
5958 Elf_External_Sym_Shndx
*destshndx
;
5960 boolean (*output_symbol_hook
) PARAMS ((bfd
*,
5961 struct bfd_link_info
*info
,
5966 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
5967 elf_backend_link_output_symbol_hook
;
5968 if (output_symbol_hook
!= NULL
)
5970 if (! ((*output_symbol_hook
)
5971 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
5975 if (name
== (const char *) NULL
|| *name
== '\0')
5976 elfsym
->st_name
= 0;
5977 else if (input_sec
->flags
& SEC_EXCLUDE
)
5978 elfsym
->st_name
= 0;
5981 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5983 if (elfsym
->st_name
== (unsigned long) -1)
5987 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5989 if (! elf_link_flush_output_syms (finfo
))
5993 dest
= finfo
->symbuf
+ finfo
->symbuf_count
;
5994 destshndx
= finfo
->symshndxbuf
;
5995 if (destshndx
!= NULL
)
5996 destshndx
+= finfo
->symbuf_count
;
5997 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
, (PTR
) dest
, (PTR
) destshndx
);
5998 ++finfo
->symbuf_count
;
6000 ++ bfd_get_symcount (finfo
->output_bfd
);
6005 /* Flush the output symbols to the file. */
6008 elf_link_flush_output_syms (finfo
)
6009 struct elf_final_link_info
*finfo
;
6011 if (finfo
->symbuf_count
> 0)
6013 Elf_Internal_Shdr
*hdr
;
6017 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6018 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6019 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
6020 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6021 || bfd_bwrite ((PTR
) finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6024 hdr
->sh_size
+= amt
;
6026 if (finfo
->symshndxbuf
!= NULL
)
6028 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_shndx_hdr
;
6029 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6030 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym_Shndx
);
6031 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6032 || (bfd_bwrite ((PTR
) finfo
->symshndxbuf
, amt
, finfo
->output_bfd
)
6036 hdr
->sh_size
+= amt
;
6039 finfo
->symbuf_count
= 0;
6045 /* Adjust all external symbols pointing into SEC_MERGE sections
6046 to reflect the object merging within the sections. */
6049 elf_link_sec_merge_syms (h
, data
)
6050 struct elf_link_hash_entry
*h
;
6055 if (h
->root
.type
== bfd_link_hash_warning
)
6056 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6058 if ((h
->root
.type
== bfd_link_hash_defined
6059 || h
->root
.type
== bfd_link_hash_defweak
)
6060 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
6061 && elf_section_data (sec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
)
6063 bfd
*output_bfd
= (bfd
*) data
;
6065 h
->root
.u
.def
.value
=
6066 _bfd_merged_section_offset (output_bfd
,
6067 &h
->root
.u
.def
.section
,
6068 elf_section_data (sec
)->sec_info
,
6069 h
->root
.u
.def
.value
, (bfd_vma
) 0);
6075 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6076 allowing an unsatisfied unversioned symbol in the DSO to match a
6077 versioned symbol that would normally require an explicit version. */
6080 elf_link_check_versioned_symbol (info
, h
)
6081 struct bfd_link_info
*info
;
6082 struct elf_link_hash_entry
*h
;
6084 bfd
*undef_bfd
= h
->root
.u
.undef
.abfd
;
6085 struct elf_link_loaded_list
*loaded
;
6086 Elf_External_Sym
*buf
;
6087 Elf_External_Versym
*extversym
;
6089 if ((undef_bfd
->flags
& DYNAMIC
) == 0
6090 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
6091 || elf_dt_soname (h
->root
.u
.undef
.abfd
) == NULL
)
6094 for (loaded
= elf_hash_table (info
)->loaded
;
6096 loaded
= loaded
->next
)
6099 Elf_Internal_Shdr
*hdr
;
6100 bfd_size_type symcount
;
6101 bfd_size_type extsymcount
;
6102 bfd_size_type extsymoff
;
6103 Elf_Internal_Shdr
*versymhdr
;
6104 Elf_External_Versym
*ever
;
6105 Elf_External_Sym
*esym
;
6106 Elf_External_Sym
*esymend
;
6107 bfd_size_type count
;
6110 input
= loaded
->abfd
;
6112 /* We check each DSO for a possible hidden versioned definition. */
6113 if (input
== undef_bfd
6114 || (input
->flags
& DYNAMIC
) == 0
6115 || elf_dynversym (input
) == 0)
6118 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6120 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6121 if (elf_bad_symtab (input
))
6123 extsymcount
= symcount
;
6128 extsymcount
= symcount
- hdr
->sh_info
;
6129 extsymoff
= hdr
->sh_info
;
6132 if (extsymcount
== 0)
6135 count
= extsymcount
* sizeof (Elf_External_Sym
);
6136 buf
= (Elf_External_Sym
*) bfd_malloc (count
);
6140 /* Read in the symbol table. */
6141 pos
= hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
);
6142 if (bfd_seek (input
, pos
, SEEK_SET
) != 0
6143 || bfd_bread ((PTR
) buf
, count
, input
) != count
)
6146 /* Read in any version definitions. */
6147 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6148 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
6149 if (extversym
== NULL
)
6152 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6153 || (bfd_bread ((PTR
) extversym
, versymhdr
->sh_size
, input
)
6154 != versymhdr
->sh_size
))
6162 ever
= extversym
+ extsymoff
;
6163 esymend
= buf
+ extsymcount
;
6164 for (esym
= buf
; esym
< esymend
; esym
++, ever
++)
6167 Elf_Internal_Sym sym
;
6168 Elf_Internal_Versym iver
;
6170 elf_swap_symbol_in (input
, esym
, NULL
, &sym
);
6171 if (ELF_ST_BIND (sym
.st_info
) == STB_LOCAL
6172 || sym
.st_shndx
== SHN_UNDEF
)
6175 name
= bfd_elf_string_from_elf_section (input
,
6178 if (strcmp (name
, h
->root
.root
.string
) != 0)
6181 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6183 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6185 /* If we have a non-hidden versioned sym, then it should
6186 have provided a definition for the undefined sym. */
6190 if ((iver
.vs_vers
& VERSYM_VERSION
) == 2)
6192 /* This is the oldest (default) sym. We can use it. */
6206 /* Add an external symbol to the symbol table. This is called from
6207 the hash table traversal routine. When generating a shared object,
6208 we go through the symbol table twice. The first time we output
6209 anything that might have been forced to local scope in a version
6210 script. The second time we output the symbols that are still
6214 elf_link_output_extsym (h
, data
)
6215 struct elf_link_hash_entry
*h
;
6218 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
6219 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6221 Elf_Internal_Sym sym
;
6222 asection
*input_sec
;
6224 if (h
->root
.type
== bfd_link_hash_warning
)
6226 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6227 if (h
->root
.type
== bfd_link_hash_new
)
6231 /* Decide whether to output this symbol in this pass. */
6232 if (eoinfo
->localsyms
)
6234 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6239 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6243 /* If we are not creating a shared library, and this symbol is
6244 referenced by a shared library but is not defined anywhere, then
6245 warn that it is undefined. If we do not do this, the runtime
6246 linker will complain that the symbol is undefined when the
6247 program is run. We don't have to worry about symbols that are
6248 referenced by regular files, because we will already have issued
6249 warnings for them. */
6250 if (! finfo
->info
->relocateable
6251 && ! finfo
->info
->allow_shlib_undefined
6252 && ! finfo
->info
->shared
6253 && h
->root
.type
== bfd_link_hash_undefined
6254 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
6255 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
6256 && ! elf_link_check_versioned_symbol (finfo
->info
, h
))
6258 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6259 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6260 (asection
*) NULL
, (bfd_vma
) 0, true)))
6262 eoinfo
->failed
= true;
6267 /* We don't want to output symbols that have never been mentioned by
6268 a regular file, or that we have been told to strip. However, if
6269 h->indx is set to -2, the symbol is used by a reloc and we must
6273 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
6274 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
6275 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
6276 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
6278 else if (finfo
->info
->strip
== strip_all
6279 || (finfo
->info
->strip
== strip_some
6280 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6281 h
->root
.root
.string
,
6282 false, false) == NULL
))
6287 /* If we're stripping it, and it's not a dynamic symbol, there's
6288 nothing else to do unless it is a forced local symbol. */
6291 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6295 sym
.st_size
= h
->size
;
6296 sym
.st_other
= h
->other
;
6297 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6298 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6299 else if (h
->root
.type
== bfd_link_hash_undefweak
6300 || h
->root
.type
== bfd_link_hash_defweak
)
6301 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6303 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6305 switch (h
->root
.type
)
6308 case bfd_link_hash_new
:
6309 case bfd_link_hash_warning
:
6313 case bfd_link_hash_undefined
:
6314 case bfd_link_hash_undefweak
:
6315 input_sec
= bfd_und_section_ptr
;
6316 sym
.st_shndx
= SHN_UNDEF
;
6319 case bfd_link_hash_defined
:
6320 case bfd_link_hash_defweak
:
6322 input_sec
= h
->root
.u
.def
.section
;
6323 if (input_sec
->output_section
!= NULL
)
6326 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6327 input_sec
->output_section
);
6328 if (sym
.st_shndx
== SHN_BAD
)
6330 (*_bfd_error_handler
)
6331 (_("%s: could not find output section %s for input section %s"),
6332 bfd_get_filename (finfo
->output_bfd
),
6333 input_sec
->output_section
->name
,
6335 eoinfo
->failed
= true;
6339 /* ELF symbols in relocateable files are section relative,
6340 but in nonrelocateable files they are virtual
6342 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6343 if (! finfo
->info
->relocateable
)
6345 sym
.st_value
+= input_sec
->output_section
->vma
;
6346 if (h
->type
== STT_TLS
)
6348 /* STT_TLS symbols are relative to PT_TLS segment
6350 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6351 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
6357 BFD_ASSERT (input_sec
->owner
== NULL
6358 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6359 sym
.st_shndx
= SHN_UNDEF
;
6360 input_sec
= bfd_und_section_ptr
;
6365 case bfd_link_hash_common
:
6366 input_sec
= h
->root
.u
.c
.p
->section
;
6367 sym
.st_shndx
= SHN_COMMON
;
6368 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6371 case bfd_link_hash_indirect
:
6372 /* These symbols are created by symbol versioning. They point
6373 to the decorated version of the name. For example, if the
6374 symbol foo@@GNU_1.2 is the default, which should be used when
6375 foo is used with no version, then we add an indirect symbol
6376 foo which points to foo@@GNU_1.2. We ignore these symbols,
6377 since the indirected symbol is already in the hash table. */
6381 /* Give the processor backend a chance to tweak the symbol value,
6382 and also to finish up anything that needs to be done for this
6383 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6384 forced local syms when non-shared is due to a historical quirk. */
6385 if ((h
->dynindx
!= -1
6386 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
6387 && (finfo
->info
->shared
6388 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
6389 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6391 struct elf_backend_data
*bed
;
6393 bed
= get_elf_backend_data (finfo
->output_bfd
);
6394 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6395 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6397 eoinfo
->failed
= true;
6402 /* If we are marking the symbol as undefined, and there are no
6403 non-weak references to this symbol from a regular object, then
6404 mark the symbol as weak undefined; if there are non-weak
6405 references, mark the symbol as strong. We can't do this earlier,
6406 because it might not be marked as undefined until the
6407 finish_dynamic_symbol routine gets through with it. */
6408 if (sym
.st_shndx
== SHN_UNDEF
6409 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
6410 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6411 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6415 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
6416 bindtype
= STB_GLOBAL
;
6418 bindtype
= STB_WEAK
;
6419 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6422 /* If a symbol is not defined locally, we clear the visibility
6424 if (! finfo
->info
->relocateable
6425 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6426 sym
.st_other
^= ELF_ST_VISIBILITY (sym
.st_other
);
6428 /* If this symbol should be put in the .dynsym section, then put it
6429 there now. We already know the symbol index. We also fill in
6430 the entry in the .hash section. */
6431 if (h
->dynindx
!= -1
6432 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6436 size_t hash_entry_size
;
6437 bfd_byte
*bucketpos
;
6439 Elf_External_Sym
*esym
;
6441 sym
.st_name
= h
->dynstr_index
;
6442 esym
= (Elf_External_Sym
*) finfo
->dynsym_sec
->contents
+ h
->dynindx
;
6443 elf_swap_symbol_out (finfo
->output_bfd
, &sym
, (PTR
) esym
, (PTR
) 0);
6445 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6446 bucket
= h
->elf_hash_value
% bucketcount
;
6448 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6449 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6450 + (bucket
+ 2) * hash_entry_size
);
6451 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6452 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, (bfd_vma
) h
->dynindx
,
6454 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6455 ((bfd_byte
*) finfo
->hash_sec
->contents
6456 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6458 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6460 Elf_Internal_Versym iversym
;
6461 Elf_External_Versym
*eversym
;
6463 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
6465 if (h
->verinfo
.verdef
== NULL
)
6466 iversym
.vs_vers
= 0;
6468 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6472 if (h
->verinfo
.vertree
== NULL
)
6473 iversym
.vs_vers
= 1;
6475 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6478 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
6479 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6481 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6482 eversym
+= h
->dynindx
;
6483 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6487 /* If we're stripping it, then it was just a dynamic symbol, and
6488 there's nothing else to do. */
6492 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6494 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
6496 eoinfo
->failed
= true;
6503 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
6504 originated from the section given by INPUT_REL_HDR) to the
6508 elf_link_output_relocs (output_bfd
, input_section
, input_rel_hdr
,
6511 asection
*input_section
;
6512 Elf_Internal_Shdr
*input_rel_hdr
;
6513 Elf_Internal_Rela
*internal_relocs
;
6515 Elf_Internal_Rela
*irela
;
6516 Elf_Internal_Rela
*irelaend
;
6517 Elf_Internal_Shdr
*output_rel_hdr
;
6518 asection
*output_section
;
6519 unsigned int *rel_countp
= NULL
;
6520 struct elf_backend_data
*bed
;
6523 output_section
= input_section
->output_section
;
6524 output_rel_hdr
= NULL
;
6526 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
6527 == input_rel_hdr
->sh_entsize
)
6529 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
6530 rel_countp
= &elf_section_data (output_section
)->rel_count
;
6532 else if (elf_section_data (output_section
)->rel_hdr2
6533 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
6534 == input_rel_hdr
->sh_entsize
))
6536 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
6537 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
6541 (*_bfd_error_handler
) (
6542 _("%s: relocation size mismatch in %s section %s"),
6543 bfd_get_filename (output_bfd
),
6544 bfd_archive_filename (input_section
->owner
),
6545 input_section
->name
);
6546 bfd_set_error (bfd_error_wrong_object_format
);
6550 bed
= get_elf_backend_data (output_bfd
);
6551 irela
= internal_relocs
;
6552 irelaend
= irela
+ NUM_SHDR_ENTRIES (input_rel_hdr
)
6553 * bed
->s
->int_rels_per_ext_rel
;
6555 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
6557 Elf_External_Rel
*erel
;
6558 Elf_Internal_Rel
*irel
;
6560 amt
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
6561 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (amt
);
6564 (*_bfd_error_handler
) (_("Error: out of memory"));
6568 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
+ *rel_countp
);
6569 for (; irela
< irelaend
; irela
+= bed
->s
->int_rels_per_ext_rel
, erel
++)
6573 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
6575 irel
[i
].r_offset
= irela
[i
].r_offset
;
6576 irel
[i
].r_info
= irela
[i
].r_info
;
6577 BFD_ASSERT (irela
[i
].r_addend
== 0);
6580 if (bed
->s
->swap_reloc_out
)
6581 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, (PTR
) erel
);
6583 elf_swap_reloc_out (output_bfd
, irel
, erel
);
6590 Elf_External_Rela
*erela
;
6592 BFD_ASSERT (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
));
6594 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
+ *rel_countp
);
6595 for (; irela
< irelaend
; irela
+= bed
->s
->int_rels_per_ext_rel
, erela
++)
6596 if (bed
->s
->swap_reloca_out
)
6597 (*bed
->s
->swap_reloca_out
) (output_bfd
, irela
, (PTR
) erela
);
6599 elf_swap_reloca_out (output_bfd
, irela
, erela
);
6602 /* Bump the counter, so that we know where to add the next set of
6604 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
6609 /* Link an input file into the linker output file. This function
6610 handles all the sections and relocations of the input file at once.
6611 This is so that we only have to read the local symbols once, and
6612 don't have to keep them in memory. */
6615 elf_link_input_bfd (finfo
, input_bfd
)
6616 struct elf_final_link_info
*finfo
;
6619 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
6620 bfd
*, asection
*, bfd_byte
*,
6621 Elf_Internal_Rela
*,
6622 Elf_Internal_Sym
*, asection
**));
6624 Elf_Internal_Shdr
*symtab_hdr
;
6625 Elf_Internal_Shdr
*shndx_hdr
;
6628 Elf_External_Sym
*external_syms
;
6629 Elf_External_Sym
*esym
;
6630 Elf_External_Sym
*esymend
;
6631 Elf_External_Sym_Shndx
*shndx_buf
;
6632 Elf_External_Sym_Shndx
*shndx
;
6633 Elf_Internal_Sym
*isym
;
6635 asection
**ppsection
;
6637 struct elf_backend_data
*bed
;
6638 boolean emit_relocs
;
6639 struct elf_link_hash_entry
**sym_hashes
;
6641 output_bfd
= finfo
->output_bfd
;
6642 bed
= get_elf_backend_data (output_bfd
);
6643 relocate_section
= bed
->elf_backend_relocate_section
;
6645 /* If this is a dynamic object, we don't want to do anything here:
6646 we don't want the local symbols, and we don't want the section
6648 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6651 emit_relocs
= (finfo
->info
->relocateable
6652 || finfo
->info
->emitrelocations
6653 || bed
->elf_backend_emit_relocs
);
6655 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6656 if (elf_bad_symtab (input_bfd
))
6658 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6663 locsymcount
= symtab_hdr
->sh_info
;
6664 extsymoff
= symtab_hdr
->sh_info
;
6667 /* Read the local symbols. */
6668 if (symtab_hdr
->contents
!= NULL
)
6669 external_syms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
6670 else if (locsymcount
== 0)
6671 external_syms
= NULL
;
6674 bfd_size_type amt
= locsymcount
* sizeof (Elf_External_Sym
);
6675 external_syms
= finfo
->external_syms
;
6676 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
6677 || bfd_bread (external_syms
, amt
, input_bfd
) != amt
)
6681 shndx_hdr
= &elf_tdata (input_bfd
)->symtab_shndx_hdr
;
6683 if (shndx_hdr
->sh_size
!= 0 && locsymcount
!= 0)
6685 bfd_size_type amt
= locsymcount
* sizeof (Elf_External_Sym_Shndx
);
6686 shndx_buf
= finfo
->locsym_shndx
;
6687 if (bfd_seek (input_bfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
6688 || bfd_bread (shndx_buf
, amt
, input_bfd
) != amt
)
6692 /* Swap in the local symbols and write out the ones which we know
6693 are going into the output file. */
6694 for (esym
= external_syms
, esymend
= esym
+ locsymcount
,
6695 isym
= finfo
->internal_syms
, pindex
= finfo
->indices
,
6696 ppsection
= finfo
->sections
, shndx
= shndx_buf
;
6698 esym
++, isym
++, pindex
++, ppsection
++,
6699 shndx
= (shndx
!= NULL
? shndx
+ 1 : NULL
))
6703 Elf_Internal_Sym osym
;
6705 elf_swap_symbol_in (input_bfd
, (const PTR
) esym
, (const PTR
) shndx
,
6709 if (elf_bad_symtab (input_bfd
))
6711 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6718 if (isym
->st_shndx
== SHN_UNDEF
)
6719 isec
= bfd_und_section_ptr
;
6720 else if (isym
->st_shndx
< SHN_LORESERVE
6721 || isym
->st_shndx
> SHN_HIRESERVE
)
6723 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
6725 && elf_section_data (isec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
6726 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6728 _bfd_merged_section_offset (output_bfd
, &isec
,
6729 elf_section_data (isec
)->sec_info
,
6730 isym
->st_value
, (bfd_vma
) 0);
6732 else if (isym
->st_shndx
== SHN_ABS
)
6733 isec
= bfd_abs_section_ptr
;
6734 else if (isym
->st_shndx
== SHN_COMMON
)
6735 isec
= bfd_com_section_ptr
;
6744 /* Don't output the first, undefined, symbol. */
6745 if (esym
== external_syms
)
6748 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6750 /* We never output section symbols. Instead, we use the
6751 section symbol of the corresponding section in the output
6756 /* If we are stripping all symbols, we don't want to output this
6758 if (finfo
->info
->strip
== strip_all
)
6761 /* If we are discarding all local symbols, we don't want to
6762 output this one. If we are generating a relocateable output
6763 file, then some of the local symbols may be required by
6764 relocs; we output them below as we discover that they are
6766 if (finfo
->info
->discard
== discard_all
)
6769 /* If this symbol is defined in a section which we are
6770 discarding, we don't need to keep it, but note that
6771 linker_mark is only reliable for sections that have contents.
6772 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6773 as well as linker_mark. */
6774 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6776 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6777 || (! finfo
->info
->relocateable
6778 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6781 /* Get the name of the symbol. */
6782 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6787 /* See if we are discarding symbols with this name. */
6788 if ((finfo
->info
->strip
== strip_some
6789 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
6791 || (((finfo
->info
->discard
== discard_sec_merge
6792 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocateable
)
6793 || finfo
->info
->discard
== discard_l
)
6794 && bfd_is_local_label_name (input_bfd
, name
)))
6797 /* If we get here, we are going to output this symbol. */
6801 /* Adjust the section index for the output file. */
6802 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6803 isec
->output_section
);
6804 if (osym
.st_shndx
== SHN_BAD
)
6807 *pindex
= bfd_get_symcount (output_bfd
);
6809 /* ELF symbols in relocateable files are section relative, but
6810 in executable files they are virtual addresses. Note that
6811 this code assumes that all ELF sections have an associated
6812 BFD section with a reasonable value for output_offset; below
6813 we assume that they also have a reasonable value for
6814 output_section. Any special sections must be set up to meet
6815 these requirements. */
6816 osym
.st_value
+= isec
->output_offset
;
6817 if (! finfo
->info
->relocateable
)
6819 osym
.st_value
+= isec
->output_section
->vma
;
6820 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6822 /* STT_TLS symbols are relative to PT_TLS segment base. */
6823 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
6824 osym
.st_value
-= finfo
->first_tls_sec
->vma
;
6828 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
6832 /* Relocate the contents of each section. */
6833 sym_hashes
= elf_sym_hashes (input_bfd
);
6834 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6838 if (! o
->linker_mark
)
6840 /* This section was omitted from the link. */
6844 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6845 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6848 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6850 /* Section was created by elf_link_create_dynamic_sections
6855 /* Get the contents of the section. They have been cached by a
6856 relaxation routine. Note that o is a section in an input
6857 file, so the contents field will not have been set by any of
6858 the routines which work on output files. */
6859 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6860 contents
= elf_section_data (o
)->this_hdr
.contents
;
6863 contents
= finfo
->contents
;
6864 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
6865 (file_ptr
) 0, o
->_raw_size
))
6869 if ((o
->flags
& SEC_RELOC
) != 0)
6871 Elf_Internal_Rela
*internal_relocs
;
6873 /* Get the swapped relocs. */
6874 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
6875 (input_bfd
, o
, finfo
->external_relocs
,
6876 finfo
->internal_relocs
, false));
6877 if (internal_relocs
== NULL
6878 && o
->reloc_count
> 0)
6881 /* Run through the relocs looking for any against symbols
6882 from discarded sections and section symbols from
6883 removed link-once sections. Complain about relocs
6884 against discarded sections. Zero relocs against removed
6885 link-once sections. We should really complain if
6886 anything in the final link tries to use it, but
6887 DWARF-based exception handling might have an entry in
6888 .eh_frame to describe a routine in the linkonce section,
6889 and it turns out to be hard to remove the .eh_frame
6890 entry too. FIXME. */
6891 if (!finfo
->info
->relocateable
6892 && !elf_section_ignore_discarded_relocs (o
))
6894 Elf_Internal_Rela
*rel
, *relend
;
6896 rel
= internal_relocs
;
6897 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6898 for ( ; rel
< relend
; rel
++)
6900 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
6902 if (r_symndx
>= locsymcount
6903 || (elf_bad_symtab (input_bfd
)
6904 && finfo
->sections
[r_symndx
] == NULL
))
6906 struct elf_link_hash_entry
*h
;
6908 h
= sym_hashes
[r_symndx
- extsymoff
];
6909 while (h
->root
.type
== bfd_link_hash_indirect
6910 || h
->root
.type
== bfd_link_hash_warning
)
6911 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6913 /* Complain if the definition comes from a
6914 discarded section. */
6915 if ((h
->root
.type
== bfd_link_hash_defined
6916 || h
->root
.type
== bfd_link_hash_defweak
)
6917 && elf_discarded_section (h
->root
.u
.def
.section
))
6919 #if BFD_VERSION_DATE < 20031005
6920 if ((o
->flags
& SEC_DEBUGGING
) != 0)
6922 #if BFD_VERSION_DATE > 20021005
6923 (*finfo
->info
->callbacks
->warning
)
6925 _("warning: relocation against removed section; zeroing"),
6926 NULL
, input_bfd
, o
, rel
->r_offset
);
6928 BFD_ASSERT (r_symndx
!= 0);
6929 memset (rel
, 0, sizeof (*rel
));
6934 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6935 (finfo
->info
, h
->root
.root
.string
,
6936 input_bfd
, o
, rel
->r_offset
,
6944 asection
*sec
= finfo
->sections
[r_symndx
];
6946 if (sec
!= NULL
&& elf_discarded_section (sec
))
6948 #if BFD_VERSION_DATE < 20031005
6949 if ((o
->flags
& SEC_DEBUGGING
) != 0
6950 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
6952 #if BFD_VERSION_DATE > 20021005
6953 (*finfo
->info
->callbacks
->warning
)
6955 _("warning: relocation against removed section"),
6956 NULL
, input_bfd
, o
, rel
->r_offset
);
6958 BFD_ASSERT (r_symndx
!= 0);
6960 = ELF_R_INFO (0, ELF_R_TYPE (rel
->r_info
));
6968 = _("local symbols in discarded section %s");
6970 = strlen (sec
->name
) + strlen (msg
) - 1;
6971 char *buf
= (char *) bfd_malloc (amt
);
6974 sprintf (buf
, msg
, sec
->name
);
6976 buf
= (char *) sec
->name
;
6977 ok
= (*finfo
->info
->callbacks
6978 ->undefined_symbol
) (finfo
->info
, buf
,
6982 if (buf
!= sec
->name
)
6992 /* Relocate the section by invoking a back end routine.
6994 The back end routine is responsible for adjusting the
6995 section contents as necessary, and (if using Rela relocs
6996 and generating a relocateable output file) adjusting the
6997 reloc addend as necessary.
6999 The back end routine does not have to worry about setting
7000 the reloc address or the reloc symbol index.
7002 The back end routine is given a pointer to the swapped in
7003 internal symbols, and can access the hash table entries
7004 for the external symbols via elf_sym_hashes (input_bfd).
7006 When generating relocateable output, the back end routine
7007 must handle STB_LOCAL/STT_SECTION symbols specially. The
7008 output symbol is going to be a section symbol
7009 corresponding to the output section, which will require
7010 the addend to be adjusted. */
7012 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
7013 input_bfd
, o
, contents
,
7015 finfo
->internal_syms
,
7021 Elf_Internal_Rela
*irela
;
7022 Elf_Internal_Rela
*irelaend
;
7023 struct elf_link_hash_entry
**rel_hash
;
7024 Elf_Internal_Shdr
*input_rel_hdr
;
7025 unsigned int next_erel
;
7026 boolean (*reloc_emitter
) PARAMS ((bfd
*, asection
*,
7027 Elf_Internal_Shdr
*,
7028 Elf_Internal_Rela
*));
7029 boolean rela_normal
;
7031 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7032 rela_normal
= (bed
->rela_normal
7033 && (input_rel_hdr
->sh_entsize
7034 == sizeof (Elf_External_Rela
)));
7036 /* Adjust the reloc addresses and symbol indices. */
7038 irela
= internal_relocs
;
7039 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7040 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7041 + elf_section_data (o
->output_section
)->rel_count
7042 + elf_section_data (o
->output_section
)->rel_count2
);
7043 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7045 unsigned long r_symndx
;
7048 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7054 irela
->r_offset
+= o
->output_offset
;
7056 /* Relocs in an executable have to be virtual addresses. */
7057 if (!finfo
->info
->relocateable
)
7058 irela
->r_offset
+= o
->output_section
->vma
;
7060 r_symndx
= ELF_R_SYM (irela
->r_info
);
7065 if (r_symndx
>= locsymcount
7066 || (elf_bad_symtab (input_bfd
)
7067 && finfo
->sections
[r_symndx
] == NULL
))
7069 struct elf_link_hash_entry
*rh
;
7072 /* This is a reloc against a global symbol. We
7073 have not yet output all the local symbols, so
7074 we do not know the symbol index of any global
7075 symbol. We set the rel_hash entry for this
7076 reloc to point to the global hash table entry
7077 for this symbol. The symbol index is then
7078 set at the end of elf_bfd_final_link. */
7079 indx
= r_symndx
- extsymoff
;
7080 rh
= elf_sym_hashes (input_bfd
)[indx
];
7081 while (rh
->root
.type
== bfd_link_hash_indirect
7082 || rh
->root
.type
== bfd_link_hash_warning
)
7083 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7085 /* Setting the index to -2 tells
7086 elf_link_output_extsym that this symbol is
7088 BFD_ASSERT (rh
->indx
< 0);
7096 /* This is a reloc against a local symbol. */
7099 isym
= finfo
->internal_syms
+ r_symndx
;
7100 sec
= finfo
->sections
[r_symndx
];
7101 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
7103 /* I suppose the backend ought to fill in the
7104 section of any STT_SECTION symbol against a
7105 processor specific section. If we have
7106 discarded a section, the output_section will
7107 be the absolute section. */
7108 if (bfd_is_abs_section (sec
)
7110 && bfd_is_abs_section (sec
->output_section
)))
7112 else if (sec
== NULL
|| sec
->owner
== NULL
)
7114 bfd_set_error (bfd_error_bad_value
);
7119 r_symndx
= sec
->output_section
->target_index
;
7120 BFD_ASSERT (r_symndx
!= 0);
7123 /* Adjust the addend according to where the
7124 section winds up in the output section. */
7126 irela
->r_addend
+= sec
->output_offset
;
7130 if (finfo
->indices
[r_symndx
] == -1)
7132 unsigned long shlink
;
7136 if (finfo
->info
->strip
== strip_all
)
7138 /* You can't do ld -r -s. */
7139 bfd_set_error (bfd_error_invalid_operation
);
7143 /* This symbol was skipped earlier, but
7144 since it is needed by a reloc, we
7145 must output it now. */
7146 shlink
= symtab_hdr
->sh_link
;
7147 name
= (bfd_elf_string_from_elf_section
7148 (input_bfd
, shlink
, isym
->st_name
));
7152 osec
= sec
->output_section
;
7154 _bfd_elf_section_from_bfd_section (output_bfd
,
7156 if (isym
->st_shndx
== SHN_BAD
)
7159 isym
->st_value
+= sec
->output_offset
;
7160 if (! finfo
->info
->relocateable
)
7162 isym
->st_value
+= osec
->vma
;
7163 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
7165 /* STT_TLS symbols are relative to PT_TLS
7167 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
7168 isym
->st_value
-= finfo
->first_tls_sec
->vma
;
7172 finfo
->indices
[r_symndx
]
7173 = bfd_get_symcount (output_bfd
);
7175 if (! elf_link_output_sym (finfo
, name
, isym
, sec
))
7179 r_symndx
= finfo
->indices
[r_symndx
];
7182 irela
->r_info
= ELF_R_INFO (r_symndx
,
7183 ELF_R_TYPE (irela
->r_info
));
7186 /* Swap out the relocs. */
7187 if (bed
->elf_backend_emit_relocs
7188 && !(finfo
->info
->relocateable
7189 || finfo
->info
->emitrelocations
))
7190 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7192 reloc_emitter
= elf_link_output_relocs
;
7194 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7198 input_rel_hdr
= elf_section_data (o
)->rel_hdr2
;
7201 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7202 * bed
->s
->int_rels_per_ext_rel
);
7203 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7211 /* Write out the modified section contents. */
7212 if (bed
->elf_backend_write_section
7213 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7215 /* Section written out. */
7217 else switch (elf_section_data (o
)->sec_info_type
)
7219 case ELF_INFO_TYPE_STABS
:
7220 if (! (_bfd_write_section_stabs
7222 &elf_hash_table (finfo
->info
)->stab_info
,
7223 o
, &elf_section_data (o
)->sec_info
, contents
)))
7226 case ELF_INFO_TYPE_MERGE
:
7227 if (! (_bfd_write_merged_section
7228 (output_bfd
, o
, elf_section_data (o
)->sec_info
)))
7231 case ELF_INFO_TYPE_EH_FRAME
:
7236 = bfd_get_section_by_name (elf_hash_table (finfo
->info
)->dynobj
,
7238 if (! (_bfd_elf_write_section_eh_frame (output_bfd
, o
, ehdrsec
,
7245 bfd_size_type sec_size
;
7247 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
7248 if (! (o
->flags
& SEC_EXCLUDE
)
7249 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7251 (file_ptr
) o
->output_offset
,
7262 /* Generate a reloc when linking an ELF file. This is a reloc
7263 requested by the linker, and does come from any input file. This
7264 is used to build constructor and destructor tables when linking
7268 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
7270 struct bfd_link_info
*info
;
7271 asection
*output_section
;
7272 struct bfd_link_order
*link_order
;
7274 reloc_howto_type
*howto
;
7278 struct elf_link_hash_entry
**rel_hash_ptr
;
7279 Elf_Internal_Shdr
*rel_hdr
;
7280 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7282 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7285 bfd_set_error (bfd_error_bad_value
);
7289 addend
= link_order
->u
.reloc
.p
->addend
;
7291 /* Figure out the symbol index. */
7292 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7293 + elf_section_data (output_section
)->rel_count
7294 + elf_section_data (output_section
)->rel_count2
);
7295 if (link_order
->type
== bfd_section_reloc_link_order
)
7297 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7298 BFD_ASSERT (indx
!= 0);
7299 *rel_hash_ptr
= NULL
;
7303 struct elf_link_hash_entry
*h
;
7305 /* Treat a reloc against a defined symbol as though it were
7306 actually against the section. */
7307 h
= ((struct elf_link_hash_entry
*)
7308 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7309 link_order
->u
.reloc
.p
->u
.name
,
7310 false, false, true));
7312 && (h
->root
.type
== bfd_link_hash_defined
7313 || h
->root
.type
== bfd_link_hash_defweak
))
7317 section
= h
->root
.u
.def
.section
;
7318 indx
= section
->output_section
->target_index
;
7319 *rel_hash_ptr
= NULL
;
7320 /* It seems that we ought to add the symbol value to the
7321 addend here, but in practice it has already been added
7322 because it was passed to constructor_callback. */
7323 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7327 /* Setting the index to -2 tells elf_link_output_extsym that
7328 this symbol is used by a reloc. */
7335 if (! ((*info
->callbacks
->unattached_reloc
)
7336 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
7337 (asection
*) NULL
, (bfd_vma
) 0)))
7343 /* If this is an inplace reloc, we must write the addend into the
7345 if (howto
->partial_inplace
&& addend
!= 0)
7348 bfd_reloc_status_type rstat
;
7351 const char *sym_name
;
7353 size
= bfd_get_reloc_size (howto
);
7354 buf
= (bfd_byte
*) bfd_zmalloc (size
);
7355 if (buf
== (bfd_byte
*) NULL
)
7357 rstat
= _bfd_relocate_contents (howto
, output_bfd
, (bfd_vma
) addend
, buf
);
7364 case bfd_reloc_outofrange
:
7367 case bfd_reloc_overflow
:
7368 if (link_order
->type
== bfd_section_reloc_link_order
)
7369 sym_name
= bfd_section_name (output_bfd
,
7370 link_order
->u
.reloc
.p
->u
.section
);
7372 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7373 if (! ((*info
->callbacks
->reloc_overflow
)
7374 (info
, sym_name
, howto
->name
, addend
,
7375 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
7382 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
7383 (file_ptr
) link_order
->offset
, size
);
7389 /* The address of a reloc is relative to the section in a
7390 relocateable file, and is a virtual address in an executable
7392 offset
= link_order
->offset
;
7393 if (! info
->relocateable
)
7394 offset
+= output_section
->vma
;
7396 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7398 if (rel_hdr
->sh_type
== SHT_REL
)
7401 Elf_Internal_Rel
*irel
;
7402 Elf_External_Rel
*erel
;
7405 size
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
7406 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (size
);
7410 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7411 irel
[i
].r_offset
= offset
;
7412 irel
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
7414 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
7415 + elf_section_data (output_section
)->rel_count
);
7417 if (bed
->s
->swap_reloc_out
)
7418 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, (bfd_byte
*) erel
);
7420 elf_swap_reloc_out (output_bfd
, irel
, erel
);
7427 Elf_Internal_Rela
*irela
;
7428 Elf_External_Rela
*erela
;
7431 size
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
7432 irela
= (Elf_Internal_Rela
*) bfd_zmalloc (size
);
7436 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7437 irela
[i
].r_offset
= offset
;
7438 irela
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
7439 irela
[0].r_addend
= addend
;
7441 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
7442 + elf_section_data (output_section
)->rel_count
);
7444 if (bed
->s
->swap_reloca_out
)
7445 (*bed
->s
->swap_reloca_out
) (output_bfd
, irela
, (bfd_byte
*) erela
);
7447 elf_swap_reloca_out (output_bfd
, irela
, erela
);
7450 ++elf_section_data (output_section
)->rel_count
;
7455 /* Allocate a pointer to live in a linker created section. */
7458 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
7460 struct bfd_link_info
*info
;
7461 elf_linker_section_t
*lsect
;
7462 struct elf_link_hash_entry
*h
;
7463 const Elf_Internal_Rela
*rel
;
7465 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
7466 elf_linker_section_pointers_t
*linker_section_ptr
;
7467 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7470 BFD_ASSERT (lsect
!= NULL
);
7472 /* Is this a global symbol? */
7475 /* Has this symbol already been allocated? If so, our work is done. */
7476 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
7481 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
7482 /* Make sure this symbol is output as a dynamic symbol. */
7483 if (h
->dynindx
== -1)
7485 if (! elf_link_record_dynamic_symbol (info
, h
))
7489 if (lsect
->rel_section
)
7490 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7494 /* Allocation of a pointer to a local symbol. */
7495 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
7497 /* Allocate a table to hold the local symbols if first time. */
7500 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
7501 register unsigned int i
;
7504 amt
*= sizeof (elf_linker_section_pointers_t
*);
7505 ptr
= (elf_linker_section_pointers_t
**) bfd_alloc (abfd
, amt
);
7510 elf_local_ptr_offsets (abfd
) = ptr
;
7511 for (i
= 0; i
< num_symbols
; i
++)
7512 ptr
[i
] = (elf_linker_section_pointers_t
*) 0;
7515 /* Has this symbol already been allocated? If so, our work is done. */
7516 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
7521 ptr_linker_section_ptr
= &ptr
[r_symndx
];
7525 /* If we are generating a shared object, we need to
7526 output a R_<xxx>_RELATIVE reloc so that the
7527 dynamic linker can adjust this GOT entry. */
7528 BFD_ASSERT (lsect
->rel_section
!= NULL
);
7529 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
7533 /* Allocate space for a pointer in the linker section, and allocate
7534 a new pointer record from internal memory. */
7535 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
7536 amt
= sizeof (elf_linker_section_pointers_t
);
7537 linker_section_ptr
= (elf_linker_section_pointers_t
*) bfd_alloc (abfd
, amt
);
7539 if (!linker_section_ptr
)
7542 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
7543 linker_section_ptr
->addend
= rel
->r_addend
;
7544 linker_section_ptr
->which
= lsect
->which
;
7545 linker_section_ptr
->written_address_p
= false;
7546 *ptr_linker_section_ptr
= linker_section_ptr
;
7549 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
7551 linker_section_ptr
->offset
= (lsect
->section
->_raw_size
7552 - lsect
->hole_size
+ (ARCH_SIZE
/ 8));
7553 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
7554 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
7555 if (lsect
->sym_hash
)
7557 /* Bump up symbol value if needed. */
7558 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
7560 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
7561 lsect
->sym_hash
->root
.root
.string
,
7562 (long) ARCH_SIZE
/ 8,
7563 (long) lsect
->sym_hash
->root
.u
.def
.value
);
7569 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
7571 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
7575 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7576 lsect
->name
, (long) linker_section_ptr
->offset
,
7577 (long) lsect
->section
->_raw_size
);
7584 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7587 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7590 /* Fill in the address for a pointer generated in a linker section. */
7593 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
,
7594 relocation
, rel
, relative_reloc
)
7597 struct bfd_link_info
*info
;
7598 elf_linker_section_t
*lsect
;
7599 struct elf_link_hash_entry
*h
;
7601 const Elf_Internal_Rela
*rel
;
7604 elf_linker_section_pointers_t
*linker_section_ptr
;
7606 BFD_ASSERT (lsect
!= NULL
);
7610 /* Handle global symbol. */
7611 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7612 (h
->linker_section_pointer
,
7616 BFD_ASSERT (linker_section_ptr
!= NULL
);
7618 if (! elf_hash_table (info
)->dynamic_sections_created
7621 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
7623 /* This is actually a static link, or it is a
7624 -Bsymbolic link and the symbol is defined
7625 locally. We must initialize this entry in the
7628 When doing a dynamic link, we create a .rela.<xxx>
7629 relocation entry to initialize the value. This
7630 is done in the finish_dynamic_symbol routine. */
7631 if (!linker_section_ptr
->written_address_p
)
7633 linker_section_ptr
->written_address_p
= true;
7634 bfd_put_ptr (output_bfd
,
7635 relocation
+ linker_section_ptr
->addend
,
7636 (lsect
->section
->contents
7637 + linker_section_ptr
->offset
));
7643 /* Handle local symbol. */
7644 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7645 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
7646 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
7647 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7648 (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
7652 BFD_ASSERT (linker_section_ptr
!= NULL
);
7654 /* Write out pointer if it hasn't been rewritten out before. */
7655 if (!linker_section_ptr
->written_address_p
)
7657 linker_section_ptr
->written_address_p
= true;
7658 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
7659 lsect
->section
->contents
+ linker_section_ptr
->offset
);
7663 asection
*srel
= lsect
->rel_section
;
7664 Elf_Internal_Rela
*outrel
;
7665 Elf_External_Rela
*erel
;
7666 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7670 amt
= sizeof (Elf_Internal_Rela
) * bed
->s
->int_rels_per_ext_rel
;
7671 outrel
= (Elf_Internal_Rela
*) bfd_zmalloc (amt
);
7674 (*_bfd_error_handler
) (_("Error: out of memory"));
7678 /* We need to generate a relative reloc for the dynamic
7682 srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
7684 lsect
->rel_section
= srel
;
7687 BFD_ASSERT (srel
!= NULL
);
7689 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7690 outrel
[i
].r_offset
= (lsect
->section
->output_section
->vma
7691 + lsect
->section
->output_offset
7692 + linker_section_ptr
->offset
);
7693 outrel
[0].r_info
= ELF_R_INFO (0, relative_reloc
);
7694 outrel
[0].r_addend
= 0;
7695 erel
= (Elf_External_Rela
*) lsect
->section
->contents
;
7696 erel
+= elf_section_data (lsect
->section
)->rel_count
;
7697 elf_swap_reloca_out (output_bfd
, outrel
, erel
);
7698 ++elf_section_data (lsect
->section
)->rel_count
;
7705 relocation
= (lsect
->section
->output_offset
7706 + linker_section_ptr
->offset
7707 - lsect
->hole_offset
7708 - lsect
->sym_offset
);
7712 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7713 lsect
->name
, (long) relocation
, (long) relocation
);
7716 /* Subtract out the addend, because it will get added back in by the normal
7718 return relocation
- linker_section_ptr
->addend
;
7721 /* Garbage collect unused sections. */
7723 static boolean elf_gc_mark
7724 PARAMS ((struct bfd_link_info
*info
, asection
*sec
,
7725 asection
* (*gc_mark_hook
)
7726 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7727 struct elf_link_hash_entry
*, Elf_Internal_Sym
*))));
7729 static boolean elf_gc_sweep
7730 PARAMS ((struct bfd_link_info
*info
,
7731 boolean (*gc_sweep_hook
)
7732 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
7733 const Elf_Internal_Rela
*relocs
))));
7735 static boolean elf_gc_sweep_symbol
7736 PARAMS ((struct elf_link_hash_entry
*h
, PTR idxptr
));
7738 static boolean elf_gc_allocate_got_offsets
7739 PARAMS ((struct elf_link_hash_entry
*h
, PTR offarg
));
7741 static boolean elf_gc_propagate_vtable_entries_used
7742 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
7744 static boolean elf_gc_smash_unused_vtentry_relocs
7745 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
7747 /* The mark phase of garbage collection. For a given section, mark
7748 it and any sections in this section's group, and all the sections
7749 which define symbols to which it refers. */
7752 elf_gc_mark (info
, sec
, gc_mark_hook
)
7753 struct bfd_link_info
*info
;
7755 asection
* (*gc_mark_hook
)
7756 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7757 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
7760 asection
*group_sec
;
7764 /* Mark all the sections in the group. */
7765 group_sec
= elf_section_data (sec
)->next_in_group
;
7766 if (group_sec
&& !group_sec
->gc_mark
)
7767 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
7770 /* Look through the section relocs. */
7772 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
7774 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
7775 Elf_Internal_Shdr
*symtab_hdr
;
7776 Elf_Internal_Shdr
*shndx_hdr
;
7777 struct elf_link_hash_entry
**sym_hashes
;
7780 Elf_External_Sym
*locsyms
, *freesyms
= NULL
;
7781 Elf_External_Sym_Shndx
*locsym_shndx
;
7782 bfd
*input_bfd
= sec
->owner
;
7783 struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
7785 /* GCFIXME: how to arrange so that relocs and symbols are not
7786 reread continually? */
7788 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
7789 sym_hashes
= elf_sym_hashes (input_bfd
);
7791 /* Read the local symbols. */
7792 if (elf_bad_symtab (input_bfd
))
7794 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
7798 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
7800 if (symtab_hdr
->contents
)
7801 locsyms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
7802 else if (nlocsyms
== 0)
7806 bfd_size_type amt
= nlocsyms
* sizeof (Elf_External_Sym
);
7807 locsyms
= freesyms
= bfd_malloc (amt
);
7808 if (freesyms
== NULL
7809 || bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
7810 || bfd_bread (locsyms
, amt
, input_bfd
) != amt
)
7817 shndx_hdr
= &elf_tdata (input_bfd
)->symtab_shndx_hdr
;
7818 locsym_shndx
= NULL
;
7819 if (shndx_hdr
->sh_size
!= 0 && nlocsyms
!= 0)
7821 bfd_size_type amt
= nlocsyms
* sizeof (Elf_External_Sym_Shndx
);
7822 locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
7823 if (bfd_seek (input_bfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
7824 || bfd_bread (locsym_shndx
, amt
, input_bfd
) != amt
)
7828 /* Read the relocations. */
7829 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
7830 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
7831 info
->keep_memory
));
7832 if (relstart
== NULL
)
7837 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7839 for (rel
= relstart
; rel
< relend
; rel
++)
7841 unsigned long r_symndx
;
7843 struct elf_link_hash_entry
*h
;
7845 Elf_External_Sym_Shndx
*locshndx
;
7847 r_symndx
= ELF_R_SYM (rel
->r_info
);
7851 if (elf_bad_symtab (sec
->owner
))
7853 locshndx
= locsym_shndx
+ (locsym_shndx
? r_symndx
: 0);
7854 elf_swap_symbol_in (input_bfd
,
7855 (const PTR
) (locsyms
+ r_symndx
),
7856 (const PTR
) locshndx
,
7858 if (ELF_ST_BIND (s
.st_info
) == STB_LOCAL
)
7859 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, NULL
, &s
);
7862 h
= sym_hashes
[r_symndx
- extsymoff
];
7863 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, h
, NULL
);
7866 else if (r_symndx
>= nlocsyms
)
7868 h
= sym_hashes
[r_symndx
- extsymoff
];
7869 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, h
, NULL
);
7873 locshndx
= locsym_shndx
+ (locsym_shndx
? r_symndx
: 0);
7874 elf_swap_symbol_in (input_bfd
,
7875 (const PTR
) (locsyms
+ r_symndx
),
7876 (const PTR
) locshndx
,
7878 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, NULL
, &s
);
7881 if (rsec
&& !rsec
->gc_mark
)
7883 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
7885 else if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
7894 if (!info
->keep_memory
)
7904 /* The sweep phase of garbage collection. Remove all garbage sections. */
7907 elf_gc_sweep (info
, gc_sweep_hook
)
7908 struct bfd_link_info
*info
;
7909 boolean (*gc_sweep_hook
)
7910 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
7911 const Elf_Internal_Rela
*relocs
));
7915 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7919 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
7922 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7924 /* Keep special sections. Keep .debug sections. */
7925 if ((o
->flags
& SEC_LINKER_CREATED
)
7926 || (o
->flags
& SEC_DEBUGGING
))
7932 /* Skip sweeping sections already excluded. */
7933 if (o
->flags
& SEC_EXCLUDE
)
7936 /* Since this is early in the link process, it is simple
7937 to remove a section from the output. */
7938 o
->flags
|= SEC_EXCLUDE
;
7940 /* But we also have to update some of the relocation
7941 info we collected before. */
7943 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
7945 Elf_Internal_Rela
*internal_relocs
;
7948 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
7949 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
7950 if (internal_relocs
== NULL
)
7953 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
7955 if (!info
->keep_memory
)
7956 free (internal_relocs
);
7964 /* Remove the symbols that were in the swept sections from the dynamic
7965 symbol table. GCFIXME: Anyone know how to get them out of the
7966 static symbol table as well? */
7970 elf_link_hash_traverse (elf_hash_table (info
),
7971 elf_gc_sweep_symbol
,
7974 elf_hash_table (info
)->dynsymcount
= i
;
7980 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7983 elf_gc_sweep_symbol (h
, idxptr
)
7984 struct elf_link_hash_entry
*h
;
7987 int *idx
= (int *) idxptr
;
7989 if (h
->root
.type
== bfd_link_hash_warning
)
7990 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7992 if (h
->dynindx
!= -1
7993 && ((h
->root
.type
!= bfd_link_hash_defined
7994 && h
->root
.type
!= bfd_link_hash_defweak
)
7995 || h
->root
.u
.def
.section
->gc_mark
))
7996 h
->dynindx
= (*idx
)++;
8001 /* Propogate collected vtable information. This is called through
8002 elf_link_hash_traverse. */
8005 elf_gc_propagate_vtable_entries_used (h
, okp
)
8006 struct elf_link_hash_entry
*h
;
8009 if (h
->root
.type
== bfd_link_hash_warning
)
8010 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8012 /* Those that are not vtables. */
8013 if (h
->vtable_parent
== NULL
)
8016 /* Those vtables that do not have parents, we cannot merge. */
8017 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
8020 /* If we've already been done, exit. */
8021 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
8024 /* Make sure the parent's table is up to date. */
8025 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
8027 if (h
->vtable_entries_used
== NULL
)
8029 /* None of this table's entries were referenced. Re-use the
8031 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
8032 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
8039 /* Or the parent's entries into ours. */
8040 cu
= h
->vtable_entries_used
;
8042 pu
= h
->vtable_parent
->vtable_entries_used
;
8045 asection
*sec
= h
->root
.u
.def
.section
;
8046 struct elf_backend_data
*bed
= get_elf_backend_data (sec
->owner
);
8047 int file_align
= bed
->s
->file_align
;
8049 n
= h
->vtable_parent
->vtable_entries_size
/ file_align
;
8064 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
8065 struct elf_link_hash_entry
*h
;
8069 bfd_vma hstart
, hend
;
8070 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8071 struct elf_backend_data
*bed
;
8074 if (h
->root
.type
== bfd_link_hash_warning
)
8075 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8077 /* Take care of both those symbols that do not describe vtables as
8078 well as those that are not loaded. */
8079 if (h
->vtable_parent
== NULL
)
8082 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8083 || h
->root
.type
== bfd_link_hash_defweak
);
8085 sec
= h
->root
.u
.def
.section
;
8086 hstart
= h
->root
.u
.def
.value
;
8087 hend
= hstart
+ h
->size
;
8089 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
8090 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, true));
8092 return *(boolean
*) okp
= false;
8093 bed
= get_elf_backend_data (sec
->owner
);
8094 file_align
= bed
->s
->file_align
;
8096 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8098 for (rel
= relstart
; rel
< relend
; ++rel
)
8099 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8101 /* If the entry is in use, do nothing. */
8102 if (h
->vtable_entries_used
8103 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
8105 bfd_vma entry
= (rel
->r_offset
- hstart
) / file_align
;
8106 if (h
->vtable_entries_used
[entry
])
8109 /* Otherwise, kill it. */
8110 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8116 /* Do mark and sweep of unused sections. */
8119 elf_gc_sections (abfd
, info
)
8121 struct bfd_link_info
*info
;
8125 asection
* (*gc_mark_hook
)
8126 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8127 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
8129 if (!get_elf_backend_data (abfd
)->can_gc_sections
8130 || info
->relocateable
|| info
->emitrelocations
8131 || elf_hash_table (info
)->dynamic_sections_created
)
8134 /* Apply transitive closure to the vtable entry usage info. */
8135 elf_link_hash_traverse (elf_hash_table (info
),
8136 elf_gc_propagate_vtable_entries_used
,
8141 /* Kill the vtable relocations that were not used. */
8142 elf_link_hash_traverse (elf_hash_table (info
),
8143 elf_gc_smash_unused_vtentry_relocs
,
8148 /* Grovel through relocs to find out who stays ... */
8150 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8151 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8155 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8158 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8160 if (o
->flags
& SEC_KEEP
)
8161 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
8166 /* ... and mark SEC_EXCLUDE for those that go. */
8167 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8173 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
8176 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
8179 struct elf_link_hash_entry
*h
;
8182 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8183 struct elf_link_hash_entry
**search
, *child
;
8184 bfd_size_type extsymcount
;
8186 /* The sh_info field of the symtab header tells us where the
8187 external symbols start. We don't care about the local symbols at
8189 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
8190 if (!elf_bad_symtab (abfd
))
8191 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8193 sym_hashes
= elf_sym_hashes (abfd
);
8194 sym_hashes_end
= sym_hashes
+ extsymcount
;
8196 /* Hunt down the child symbol, which is in this section at the same
8197 offset as the relocation. */
8198 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8200 if ((child
= *search
) != NULL
8201 && (child
->root
.type
== bfd_link_hash_defined
8202 || child
->root
.type
== bfd_link_hash_defweak
)
8203 && child
->root
.u
.def
.section
== sec
8204 && child
->root
.u
.def
.value
== offset
)
8208 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
8209 bfd_archive_filename (abfd
), sec
->name
,
8210 (unsigned long) offset
);
8211 bfd_set_error (bfd_error_invalid_operation
);
8217 /* This *should* only be the absolute section. It could potentially
8218 be that someone has defined a non-global vtable though, which
8219 would be bad. It isn't worth paging in the local symbols to be
8220 sure though; that case should simply be handled by the assembler. */
8222 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
8225 child
->vtable_parent
= h
;
8230 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
8233 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
8234 bfd
*abfd ATTRIBUTE_UNUSED
;
8235 asection
*sec ATTRIBUTE_UNUSED
;
8236 struct elf_link_hash_entry
*h
;
8239 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8240 int file_align
= bed
->s
->file_align
;
8242 if (addend
>= h
->vtable_entries_size
)
8245 boolean
*ptr
= h
->vtable_entries_used
;
8247 /* While the symbol is undefined, we have to be prepared to handle
8249 if (h
->root
.type
== bfd_link_hash_undefined
)
8256 /* Oops! We've got a reference past the defined end of
8257 the table. This is probably a bug -- shall we warn? */
8262 /* Allocate one extra entry for use as a "done" flag for the
8263 consolidation pass. */
8264 bytes
= (size
/ file_align
+ 1) * sizeof (boolean
);
8268 ptr
= bfd_realloc (ptr
- 1, (bfd_size_type
) bytes
);
8274 oldbytes
= ((h
->vtable_entries_size
/ file_align
+ 1)
8275 * sizeof (boolean
));
8276 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8280 ptr
= bfd_zmalloc ((bfd_size_type
) bytes
);
8285 /* And arrange for that done flag to be at index -1. */
8286 h
->vtable_entries_used
= ptr
+ 1;
8287 h
->vtable_entries_size
= size
;
8290 h
->vtable_entries_used
[addend
/ file_align
] = true;
8295 /* And an accompanying bit to work out final got entry offsets once
8296 we're done. Should be called from final_link. */
8299 elf_gc_common_finalize_got_offsets (abfd
, info
)
8301 struct bfd_link_info
*info
;
8304 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8307 /* The GOT offset is relative to the .got section, but the GOT header is
8308 put into the .got.plt section, if the backend uses it. */
8309 if (bed
->want_got_plt
)
8312 gotoff
= bed
->got_header_size
;
8314 /* Do the local .got entries first. */
8315 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
8317 bfd_signed_vma
*local_got
;
8318 bfd_size_type j
, locsymcount
;
8319 Elf_Internal_Shdr
*symtab_hdr
;
8321 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
8324 local_got
= elf_local_got_refcounts (i
);
8328 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
8329 if (elf_bad_symtab (i
))
8330 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8332 locsymcount
= symtab_hdr
->sh_info
;
8334 for (j
= 0; j
< locsymcount
; ++j
)
8336 if (local_got
[j
] > 0)
8338 local_got
[j
] = gotoff
;
8339 gotoff
+= ARCH_SIZE
/ 8;
8342 local_got
[j
] = (bfd_vma
) -1;
8346 /* Then the global .got entries. .plt refcounts are handled by
8347 adjust_dynamic_symbol */
8348 elf_link_hash_traverse (elf_hash_table (info
),
8349 elf_gc_allocate_got_offsets
,
8354 /* We need a special top-level link routine to convert got reference counts
8355 to real got offsets. */
8358 elf_gc_allocate_got_offsets (h
, offarg
)
8359 struct elf_link_hash_entry
*h
;
8362 bfd_vma
*off
= (bfd_vma
*) offarg
;
8364 if (h
->root
.type
== bfd_link_hash_warning
)
8365 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8367 if (h
->got
.refcount
> 0)
8369 h
->got
.offset
= off
[0];
8370 off
[0] += ARCH_SIZE
/ 8;
8373 h
->got
.offset
= (bfd_vma
) -1;
8378 /* Many folk need no more in the way of final link than this, once
8379 got entry reference counting is enabled. */
8382 elf_gc_common_final_link (abfd
, info
)
8384 struct bfd_link_info
*info
;
8386 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
8389 /* Invoke the regular ELF backend linker to do all the work. */
8390 return elf_bfd_final_link (abfd
, info
);
8393 /* This function will be called though elf_link_hash_traverse to store
8394 all hash value of the exported symbols in an array. */
8397 elf_collect_hash_codes (h
, data
)
8398 struct elf_link_hash_entry
*h
;
8401 unsigned long **valuep
= (unsigned long **) data
;
8407 if (h
->root
.type
== bfd_link_hash_warning
)
8408 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8410 /* Ignore indirect symbols. These are added by the versioning code. */
8411 if (h
->dynindx
== -1)
8414 name
= h
->root
.root
.string
;
8415 p
= strchr (name
, ELF_VER_CHR
);
8418 alc
= bfd_malloc ((bfd_size_type
) (p
- name
+ 1));
8419 memcpy (alc
, name
, (size_t) (p
- name
));
8420 alc
[p
- name
] = '\0';
8424 /* Compute the hash value. */
8425 ha
= bfd_elf_hash (name
);
8427 /* Store the found hash value in the array given as the argument. */
8430 /* And store it in the struct so that we can put it in the hash table
8432 h
->elf_hash_value
= ha
;
8441 elf_reloc_symbol_deleted_p (offset
, cookie
)
8445 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
8447 if (rcookie
->bad_symtab
)
8448 rcookie
->rel
= rcookie
->rels
;
8450 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
8452 unsigned long r_symndx
= ELF_R_SYM (rcookie
->rel
->r_info
);
8453 Elf_Internal_Sym isym
;
8455 if (! rcookie
->bad_symtab
)
8456 if (rcookie
->rel
->r_offset
> offset
)
8458 if (rcookie
->rel
->r_offset
!= offset
)
8461 if (rcookie
->locsyms
&& r_symndx
< rcookie
->locsymcount
)
8463 Elf_External_Sym
*lsym
;
8464 Elf_External_Sym_Shndx
*lshndx
;
8466 lsym
= (Elf_External_Sym
*) rcookie
->locsyms
+ r_symndx
;
8467 lshndx
= (Elf_External_Sym_Shndx
*) rcookie
->locsym_shndx
;
8470 elf_swap_symbol_in (rcookie
->abfd
, (const PTR
) lsym
,
8471 (const PTR
) lshndx
, &isym
);
8474 if (r_symndx
>= rcookie
->locsymcount
8475 || (rcookie
->locsyms
8476 && ELF_ST_BIND (isym
.st_info
) != STB_LOCAL
))
8478 struct elf_link_hash_entry
*h
;
8480 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
8482 while (h
->root
.type
== bfd_link_hash_indirect
8483 || h
->root
.type
== bfd_link_hash_warning
)
8484 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8486 if ((h
->root
.type
== bfd_link_hash_defined
8487 || h
->root
.type
== bfd_link_hash_defweak
)
8488 && elf_discarded_section (h
->root
.u
.def
.section
))
8493 else if (rcookie
->locsyms
)
8495 /* It's not a relocation against a global symbol,
8496 but it could be a relocation against a local
8497 symbol for a discarded section. */
8500 /* Need to: get the symbol; get the section. */
8501 if (isym
.st_shndx
< SHN_LORESERVE
|| isym
.st_shndx
> SHN_HIRESERVE
)
8503 isec
= section_from_elf_index (rcookie
->abfd
, isym
.st_shndx
);
8504 if (isec
!= NULL
&& elf_discarded_section (isec
))
8513 /* Discard unneeded references to discarded sections.
8514 Returns true if any section's size was changed. */
8515 /* This function assumes that the relocations are in sorted order,
8516 which is true for all known assemblers. */
8519 elf_bfd_discard_info (output_bfd
, info
)
8521 struct bfd_link_info
*info
;
8523 struct elf_reloc_cookie cookie
;
8524 asection
*stab
, *eh
, *ehdr
;
8525 Elf_Internal_Shdr
*symtab_hdr
;
8526 Elf_Internal_Shdr
*shndx_hdr
;
8527 Elf_External_Sym
*freesyms
;
8528 struct elf_backend_data
*bed
;
8530 boolean ret
= false;
8531 boolean strip
= info
->strip
== strip_all
|| info
->strip
== strip_debugger
;
8533 if (info
->relocateable
8534 || info
->traditional_format
8535 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
8536 || ! is_elf_hash_table (info
))
8540 if (elf_hash_table (info
)->dynobj
!= NULL
)
8541 ehdr
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
8544 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
8546 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
8549 bed
= get_elf_backend_data (abfd
);
8551 if ((abfd
->flags
& DYNAMIC
) != 0)
8557 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
8558 if (eh
&& (eh
->_raw_size
== 0
8559 || bfd_is_abs_section (eh
->output_section
)))
8566 stab
= bfd_get_section_by_name (abfd
, ".stab");
8567 if (stab
&& (stab
->_raw_size
== 0
8568 || bfd_is_abs_section (stab
->output_section
)))
8572 || elf_section_data(stab
)->sec_info_type
!= ELF_INFO_TYPE_STABS
)
8574 && (strip
|| ! bed
->elf_backend_discard_info
))
8577 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8578 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8581 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
8582 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
8583 if (cookie
.bad_symtab
)
8585 cookie
.locsymcount
=
8586 symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
8587 cookie
.extsymoff
= 0;
8591 cookie
.locsymcount
= symtab_hdr
->sh_info
;
8592 cookie
.extsymoff
= symtab_hdr
->sh_info
;
8596 if (symtab_hdr
->contents
)
8597 cookie
.locsyms
= (void *) symtab_hdr
->contents
;
8598 else if (cookie
.locsymcount
== 0)
8599 cookie
.locsyms
= NULL
;
8602 bfd_size_type amt
= cookie
.locsymcount
* sizeof (Elf_External_Sym
);
8603 cookie
.locsyms
= bfd_malloc (amt
);
8604 if (cookie
.locsyms
== NULL
)
8606 freesyms
= cookie
.locsyms
;
8607 if (bfd_seek (abfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
8608 || bfd_bread (cookie
.locsyms
, amt
, abfd
) != amt
)
8611 free (cookie
.locsyms
);
8616 cookie
.locsym_shndx
= NULL
;
8617 if (shndx_hdr
->sh_size
!= 0 && cookie
.locsymcount
!= 0)
8620 amt
= cookie
.locsymcount
* sizeof (Elf_External_Sym_Shndx
);
8621 cookie
.locsym_shndx
= bfd_malloc (amt
);
8622 if (cookie
.locsym_shndx
== NULL
)
8623 goto error_ret_free_loc
;
8624 if (bfd_seek (abfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8625 || bfd_bread (cookie
.locsym_shndx
, amt
, abfd
) != amt
)
8627 free (cookie
.locsym_shndx
);
8628 goto error_ret_free_loc
;
8634 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8635 (abfd
, stab
, (PTR
) NULL
,
8636 (Elf_Internal_Rela
*) NULL
,
8637 info
->keep_memory
));
8640 cookie
.rel
= cookie
.rels
;
8642 cookie
.rels
+ stab
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8643 if (_bfd_discard_section_stabs (abfd
, stab
,
8644 elf_section_data (stab
)->sec_info
,
8645 elf_reloc_symbol_deleted_p
,
8648 if (! info
->keep_memory
)
8657 cookie
.relend
= NULL
;
8658 if (eh
->reloc_count
)
8659 cookie
.rels
= (NAME(_bfd_elf
,link_read_relocs
)
8660 (abfd
, eh
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
8661 info
->keep_memory
));
8664 cookie
.rel
= cookie
.rels
;
8666 cookie
.rels
+ eh
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8668 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
, ehdr
,
8669 elf_reloc_symbol_deleted_p
,
8672 if (! info
->keep_memory
)
8676 if (bed
->elf_backend_discard_info
)
8678 if (bed
->elf_backend_discard_info (abfd
, &cookie
, info
))
8682 if (cookie
.locsym_shndx
!= NULL
)
8683 free (cookie
.locsym_shndx
);
8685 if (freesyms
!= NULL
)
8689 if (ehdr
&& _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
, ehdr
))
8695 elf_section_ignore_discarded_relocs (sec
)
8698 struct elf_backend_data
*bed
;
8700 switch (elf_section_data (sec
)->sec_info_type
)
8702 case ELF_INFO_TYPE_STABS
:
8703 case ELF_INFO_TYPE_EH_FRAME
:
8709 bed
= get_elf_backend_data (sec
->owner
);
8710 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
8711 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))