2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001
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 *, Elf_Internal_Sym
*,
43 asection
**, bfd_vma
*, struct elf_link_hash_entry
**,
44 boolean
*, boolean
*, boolean
*, boolean
));
45 static boolean elf_export_symbol
46 PARAMS ((struct elf_link_hash_entry
*, PTR
));
47 static boolean elf_fix_symbol_flags
48 PARAMS ((struct elf_link_hash_entry
*, struct elf_info_failed
*));
49 static boolean elf_adjust_dynamic_symbol
50 PARAMS ((struct elf_link_hash_entry
*, PTR
));
51 static boolean elf_link_find_version_dependencies
52 PARAMS ((struct elf_link_hash_entry
*, PTR
));
53 static boolean elf_link_find_version_dependencies
54 PARAMS ((struct elf_link_hash_entry
*, PTR
));
55 static boolean elf_link_assign_sym_version
56 PARAMS ((struct elf_link_hash_entry
*, PTR
));
57 static boolean elf_collect_hash_codes
58 PARAMS ((struct elf_link_hash_entry
*, PTR
));
59 static boolean elf_link_read_relocs_from_section
60 PARAMS ((bfd
*, Elf_Internal_Shdr
*, PTR
, Elf_Internal_Rela
*));
61 static size_t compute_bucket_count
62 PARAMS ((struct bfd_link_info
*));
63 static void elf_link_output_relocs
64 PARAMS ((bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*));
65 static boolean elf_link_size_reloc_section
66 PARAMS ((bfd
*, Elf_Internal_Shdr
*, asection
*));
67 static void elf_link_adjust_relocs
68 PARAMS ((bfd
*, Elf_Internal_Shdr
*, unsigned int,
69 struct elf_link_hash_entry
**));
71 /* Given an ELF BFD, add symbols to the global hash table as
75 elf_bfd_link_add_symbols (abfd
, info
)
77 struct bfd_link_info
*info
;
79 switch (bfd_get_format (abfd
))
82 return elf_link_add_object_symbols (abfd
, info
);
84 return elf_link_add_archive_symbols (abfd
, info
);
86 bfd_set_error (bfd_error_wrong_format
);
91 /* Return true iff this is a non-common, definition of a non-function symbol. */
93 is_global_data_symbol_definition (abfd
, sym
)
94 bfd
* abfd ATTRIBUTE_UNUSED
;
95 Elf_Internal_Sym
* sym
;
97 /* Local symbols do not count, but target specific ones might. */
98 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
99 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
102 /* Function symbols do not count. */
103 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
106 /* If the section is undefined, then so is the symbol. */
107 if (sym
->st_shndx
== SHN_UNDEF
)
110 /* If the symbol is defined in the common section, then
111 it is a common definition and so does not count. */
112 if (sym
->st_shndx
== SHN_COMMON
)
115 /* If the symbol is in a target specific section then we
116 must rely upon the backend to tell us what it is. */
117 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
118 /* FIXME - this function is not coded yet:
120 return _bfd_is_global_symbol_definition (abfd, sym);
122 Instead for now assume that the definition is not global,
123 Even if this is wrong, at least the linker will behave
124 in the same way that it used to do. */
130 /* Search the symbol table of the archive element of the archive ABFD
131 whose archive map contains a mention of SYMDEF, and determine if
132 the symbol is defined in this element. */
134 elf_link_is_defined_archive_symbol (abfd
, symdef
)
138 Elf_Internal_Shdr
* hdr
;
139 Elf_External_Sym
* esym
;
140 Elf_External_Sym
* esymend
;
141 Elf_External_Sym
* buf
= NULL
;
145 boolean result
= false;
147 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
148 if (abfd
== (bfd
*) NULL
)
151 if (! bfd_check_format (abfd
, bfd_object
))
154 /* If we have already included the element containing this symbol in the
155 link then we do not need to include it again. Just claim that any symbol
156 it contains is not a definition, so that our caller will not decide to
157 (re)include this element. */
158 if (abfd
->archive_pass
)
161 /* Select the appropriate symbol table. */
162 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
163 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
165 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
167 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
169 /* The sh_info field of the symtab header tells us where the
170 external symbols start. We don't care about the local symbols. */
171 if (elf_bad_symtab (abfd
))
173 extsymcount
= symcount
;
178 extsymcount
= symcount
- hdr
->sh_info
;
179 extsymoff
= hdr
->sh_info
;
182 buf
= ((Elf_External_Sym
*)
183 bfd_malloc (extsymcount
* sizeof (Elf_External_Sym
)));
184 if (buf
== NULL
&& extsymcount
!= 0)
187 /* Read in the symbol table.
188 FIXME: This ought to be cached somewhere. */
190 hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
),
192 || (bfd_read ((PTR
) buf
, sizeof (Elf_External_Sym
), extsymcount
, abfd
)
193 != extsymcount
* sizeof (Elf_External_Sym
)))
199 /* Scan the symbol table looking for SYMDEF. */
200 esymend
= buf
+ extsymcount
;
205 Elf_Internal_Sym sym
;
208 elf_swap_symbol_in (abfd
, esym
, & sym
);
210 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
211 if (name
== (const char *) NULL
)
214 if (strcmp (name
, symdef
->name
) == 0)
216 result
= is_global_data_symbol_definition (abfd
, & sym
);
226 /* Add symbols from an ELF archive file to the linker hash table. We
227 don't use _bfd_generic_link_add_archive_symbols because of a
228 problem which arises on UnixWare. The UnixWare libc.so is an
229 archive which includes an entry libc.so.1 which defines a bunch of
230 symbols. The libc.so archive also includes a number of other
231 object files, which also define symbols, some of which are the same
232 as those defined in libc.so.1. Correct linking requires that we
233 consider each object file in turn, and include it if it defines any
234 symbols we need. _bfd_generic_link_add_archive_symbols does not do
235 this; it looks through the list of undefined symbols, and includes
236 any object file which defines them. When this algorithm is used on
237 UnixWare, it winds up pulling in libc.so.1 early and defining a
238 bunch of symbols. This means that some of the other objects in the
239 archive are not included in the link, which is incorrect since they
240 precede libc.so.1 in the archive.
242 Fortunately, ELF archive handling is simpler than that done by
243 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
244 oddities. In ELF, if we find a symbol in the archive map, and the
245 symbol is currently undefined, we know that we must pull in that
248 Unfortunately, we do have to make multiple passes over the symbol
249 table until nothing further is resolved. */
252 elf_link_add_archive_symbols (abfd
, info
)
254 struct bfd_link_info
*info
;
257 boolean
*defined
= NULL
;
258 boolean
*included
= NULL
;
262 if (! bfd_has_map (abfd
))
264 /* An empty archive is a special case. */
265 if (bfd_openr_next_archived_file (abfd
, (bfd
*) NULL
) == NULL
)
267 bfd_set_error (bfd_error_no_armap
);
271 /* Keep track of all symbols we know to be already defined, and all
272 files we know to be already included. This is to speed up the
273 second and subsequent passes. */
274 c
= bfd_ardata (abfd
)->symdef_count
;
277 defined
= (boolean
*) bfd_malloc (c
* sizeof (boolean
));
278 included
= (boolean
*) bfd_malloc (c
* sizeof (boolean
));
279 if (defined
== (boolean
*) NULL
|| included
== (boolean
*) NULL
)
281 memset (defined
, 0, c
* sizeof (boolean
));
282 memset (included
, 0, c
* sizeof (boolean
));
284 symdefs
= bfd_ardata (abfd
)->symdefs
;
297 symdefend
= symdef
+ c
;
298 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
300 struct elf_link_hash_entry
*h
;
302 struct bfd_link_hash_entry
*undefs_tail
;
305 if (defined
[i
] || included
[i
])
307 if (symdef
->file_offset
== last
)
313 h
= elf_link_hash_lookup (elf_hash_table (info
), symdef
->name
,
314 false, false, false);
320 /* If this is a default version (the name contains @@),
321 look up the symbol again without the version. The
322 effect is that references to the symbol without the
323 version will be matched by the default symbol in the
326 p
= strchr (symdef
->name
, ELF_VER_CHR
);
327 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
330 copy
= bfd_alloc (abfd
, p
- symdef
->name
+ 1);
333 memcpy (copy
, symdef
->name
, p
- symdef
->name
);
334 copy
[p
- symdef
->name
] = '\0';
336 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
337 false, false, false);
339 bfd_release (abfd
, copy
);
345 if (h
->root
.type
== bfd_link_hash_common
)
347 /* We currently have a common symbol. The archive map contains
348 a reference to this symbol, so we may want to include it. We
349 only want to include it however, if this archive element
350 contains a definition of the symbol, not just another common
353 Unfortunately some archivers (including GNU ar) will put
354 declarations of common symbols into their archive maps, as
355 well as real definitions, so we cannot just go by the archive
356 map alone. Instead we must read in the element's symbol
357 table and check that to see what kind of symbol definition
359 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
362 else if (h
->root
.type
!= bfd_link_hash_undefined
)
364 if (h
->root
.type
!= bfd_link_hash_undefweak
)
369 /* We need to include this archive member. */
370 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
371 if (element
== (bfd
*) NULL
)
374 if (! bfd_check_format (element
, bfd_object
))
377 /* Doublecheck that we have not included this object
378 already--it should be impossible, but there may be
379 something wrong with the archive. */
380 if (element
->archive_pass
!= 0)
382 bfd_set_error (bfd_error_bad_value
);
385 element
->archive_pass
= 1;
387 undefs_tail
= info
->hash
->undefs_tail
;
389 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
392 if (! elf_link_add_object_symbols (element
, info
))
395 /* If there are any new undefined symbols, we need to make
396 another pass through the archive in order to see whether
397 they can be defined. FIXME: This isn't perfect, because
398 common symbols wind up on undefs_tail and because an
399 undefined symbol which is defined later on in this pass
400 does not require another pass. This isn't a bug, but it
401 does make the code less efficient than it could be. */
402 if (undefs_tail
!= info
->hash
->undefs_tail
)
405 /* Look backward to mark all symbols from this object file
406 which we have already seen in this pass. */
410 included
[mark
] = true;
415 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
417 /* We mark subsequent symbols from this object file as we go
418 on through the loop. */
419 last
= symdef
->file_offset
;
430 if (defined
!= (boolean
*) NULL
)
432 if (included
!= (boolean
*) NULL
)
437 /* This function is called when we want to define a new symbol. It
438 handles the various cases which arise when we find a definition in
439 a dynamic object, or when there is already a definition in a
440 dynamic object. The new symbol is described by NAME, SYM, PSEC,
441 and PVALUE. We set SYM_HASH to the hash table entry. We set
442 OVERRIDE if the old symbol is overriding a new definition. We set
443 TYPE_CHANGE_OK if it is OK for the type to change. We set
444 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
445 change, we mean that we shouldn't warn if the type or size does
446 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
450 elf_merge_symbol (abfd
, info
, name
, sym
, psec
, pvalue
, sym_hash
,
451 override
, type_change_ok
, size_change_ok
, dt_needed
)
453 struct bfd_link_info
*info
;
455 Elf_Internal_Sym
*sym
;
458 struct elf_link_hash_entry
**sym_hash
;
460 boolean
*type_change_ok
;
461 boolean
*size_change_ok
;
465 struct elf_link_hash_entry
*h
;
468 boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
473 bind
= ELF_ST_BIND (sym
->st_info
);
475 if (! bfd_is_und_section (sec
))
476 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, false, false);
478 h
= ((struct elf_link_hash_entry
*)
479 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true, false, false));
484 /* This code is for coping with dynamic objects, and is only useful
485 if we are doing an ELF link. */
486 if (info
->hash
->creator
!= abfd
->xvec
)
489 /* For merging, we only care about real symbols. */
491 while (h
->root
.type
== bfd_link_hash_indirect
492 || h
->root
.type
== bfd_link_hash_warning
)
493 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
495 /* If we just created the symbol, mark it as being an ELF symbol.
496 Other than that, there is nothing to do--there is no merge issue
497 with a newly defined symbol--so we just return. */
499 if (h
->root
.type
== bfd_link_hash_new
)
501 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
505 /* OLDBFD is a BFD associated with the existing symbol. */
507 switch (h
->root
.type
)
513 case bfd_link_hash_undefined
:
514 case bfd_link_hash_undefweak
:
515 oldbfd
= h
->root
.u
.undef
.abfd
;
518 case bfd_link_hash_defined
:
519 case bfd_link_hash_defweak
:
520 oldbfd
= h
->root
.u
.def
.section
->owner
;
523 case bfd_link_hash_common
:
524 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
528 /* In cases involving weak versioned symbols, we may wind up trying
529 to merge a symbol with itself. Catch that here, to avoid the
530 confusion that results if we try to override a symbol with
531 itself. The additional tests catch cases like
532 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
533 dynamic object, which we do want to handle here. */
535 && ((abfd
->flags
& DYNAMIC
) == 0
536 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0))
539 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
540 respectively, is from a dynamic object. */
542 if ((abfd
->flags
& DYNAMIC
) != 0)
548 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
553 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
554 indices used by MIPS ELF. */
555 switch (h
->root
.type
)
561 case bfd_link_hash_defined
:
562 case bfd_link_hash_defweak
:
563 hsec
= h
->root
.u
.def
.section
;
566 case bfd_link_hash_common
:
567 hsec
= h
->root
.u
.c
.p
->section
;
574 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
577 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
578 respectively, appear to be a definition rather than reference. */
580 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
585 if (h
->root
.type
== bfd_link_hash_undefined
586 || h
->root
.type
== bfd_link_hash_undefweak
587 || h
->root
.type
== bfd_link_hash_common
)
592 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
593 symbol, respectively, appears to be a common symbol in a dynamic
594 object. If a symbol appears in an uninitialized section, and is
595 not weak, and is not a function, then it may be a common symbol
596 which was resolved when the dynamic object was created. We want
597 to treat such symbols specially, because they raise special
598 considerations when setting the symbol size: if the symbol
599 appears as a common symbol in a regular object, and the size in
600 the regular object is larger, we must make sure that we use the
601 larger size. This problematic case can always be avoided in C,
602 but it must be handled correctly when using Fortran shared
605 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
606 likewise for OLDDYNCOMMON and OLDDEF.
608 Note that this test is just a heuristic, and that it is quite
609 possible to have an uninitialized symbol in a shared object which
610 is really a definition, rather than a common symbol. This could
611 lead to some minor confusion when the symbol really is a common
612 symbol in some regular object. However, I think it will be
617 && (sec
->flags
& SEC_ALLOC
) != 0
618 && (sec
->flags
& SEC_LOAD
) == 0
621 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
624 newdyncommon
= false;
628 && h
->root
.type
== bfd_link_hash_defined
629 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
630 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
631 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
633 && h
->type
!= STT_FUNC
)
636 olddyncommon
= false;
638 /* It's OK to change the type if either the existing symbol or the
639 new symbol is weak unless it comes from a DT_NEEDED entry of
640 a shared object, in which case, the DT_NEEDED entry may not be
641 required at the run time. */
643 if ((! dt_needed
&& h
->root
.type
== bfd_link_hash_defweak
)
644 || h
->root
.type
== bfd_link_hash_undefweak
646 *type_change_ok
= true;
648 /* It's OK to change the size if either the existing symbol or the
649 new symbol is weak, or if the old symbol is undefined. */
652 || h
->root
.type
== bfd_link_hash_undefined
)
653 *size_change_ok
= true;
655 /* If both the old and the new symbols look like common symbols in a
656 dynamic object, set the size of the symbol to the larger of the
661 && sym
->st_size
!= h
->size
)
663 /* Since we think we have two common symbols, issue a multiple
664 common warning if desired. Note that we only warn if the
665 size is different. If the size is the same, we simply let
666 the old symbol override the new one as normally happens with
667 symbols defined in dynamic objects. */
669 if (! ((*info
->callbacks
->multiple_common
)
670 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
671 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
674 if (sym
->st_size
> h
->size
)
675 h
->size
= sym
->st_size
;
677 *size_change_ok
= true;
680 /* If we are looking at a dynamic object, and we have found a
681 definition, we need to see if the symbol was already defined by
682 some other object. If so, we want to use the existing
683 definition, and we do not want to report a multiple symbol
684 definition error; we do this by clobbering *PSEC to be
687 We treat a common symbol as a definition if the symbol in the
688 shared library is a function, since common symbols always
689 represent variables; this can cause confusion in principle, but
690 any such confusion would seem to indicate an erroneous program or
691 shared library. We also permit a common symbol in a regular
692 object to override a weak symbol in a shared object.
694 We prefer a non-weak definition in a shared library to a weak
695 definition in the executable unless it comes from a DT_NEEDED
696 entry of a shared object, in which case, the DT_NEEDED entry
697 may not be required at the run time. */
702 || (h
->root
.type
== bfd_link_hash_common
704 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)))
705 && (h
->root
.type
!= bfd_link_hash_defweak
707 || bind
== STB_WEAK
))
711 newdyncommon
= false;
713 *psec
= sec
= bfd_und_section_ptr
;
714 *size_change_ok
= true;
716 /* If we get here when the old symbol is a common symbol, then
717 we are explicitly letting it override a weak symbol or
718 function in a dynamic object, and we don't want to warn about
719 a type change. If the old symbol is a defined symbol, a type
720 change warning may still be appropriate. */
722 if (h
->root
.type
== bfd_link_hash_common
)
723 *type_change_ok
= true;
726 /* Handle the special case of an old common symbol merging with a
727 new symbol which looks like a common symbol in a shared object.
728 We change *PSEC and *PVALUE to make the new symbol look like a
729 common symbol, and let _bfd_generic_link_add_one_symbol will do
733 && h
->root
.type
== bfd_link_hash_common
)
737 newdyncommon
= false;
738 *pvalue
= sym
->st_size
;
739 *psec
= sec
= bfd_com_section_ptr
;
740 *size_change_ok
= true;
743 /* If the old symbol is from a dynamic object, and the new symbol is
744 a definition which is not from a dynamic object, then the new
745 symbol overrides the old symbol. Symbols from regular files
746 always take precedence over symbols from dynamic objects, even if
747 they are defined after the dynamic object in the link.
749 As above, we again permit a common symbol in a regular object to
750 override a definition in a shared object if the shared object
751 symbol is a function or is weak.
753 As above, we permit a non-weak definition in a shared object to
754 override a weak definition in a regular object. */
758 || (bfd_is_com_section (sec
)
759 && (h
->root
.type
== bfd_link_hash_defweak
760 || h
->type
== STT_FUNC
)))
763 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
765 || h
->root
.type
== bfd_link_hash_defweak
))
767 /* Change the hash table entry to undefined, and let
768 _bfd_generic_link_add_one_symbol do the right thing with the
771 h
->root
.type
= bfd_link_hash_undefined
;
772 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
773 *size_change_ok
= true;
776 olddyncommon
= false;
778 /* We again permit a type change when a common symbol may be
779 overriding a function. */
781 if (bfd_is_com_section (sec
))
782 *type_change_ok
= true;
784 /* This union may have been set to be non-NULL when this symbol
785 was seen in a dynamic object. We must force the union to be
786 NULL, so that it is correct for a regular symbol. */
788 h
->verinfo
.vertree
= NULL
;
790 /* In this special case, if H is the target of an indirection,
791 we want the caller to frob with H rather than with the
792 indirect symbol. That will permit the caller to redefine the
793 target of the indirection, rather than the indirect symbol
794 itself. FIXME: This will break the -y option if we store a
795 symbol with a different name. */
799 /* Handle the special case of a new common symbol merging with an
800 old symbol that looks like it might be a common symbol defined in
801 a shared object. Note that we have already handled the case in
802 which a new common symbol should simply override the definition
803 in the shared library. */
806 && bfd_is_com_section (sec
)
809 /* It would be best if we could set the hash table entry to a
810 common symbol, but we don't know what to use for the section
812 if (! ((*info
->callbacks
->multiple_common
)
813 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
814 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
817 /* If the predumed common symbol in the dynamic object is
818 larger, pretend that the new symbol has its size. */
820 if (h
->size
> *pvalue
)
823 /* FIXME: We no longer know the alignment required by the symbol
824 in the dynamic object, so we just wind up using the one from
825 the regular object. */
828 olddyncommon
= false;
830 h
->root
.type
= bfd_link_hash_undefined
;
831 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
833 *size_change_ok
= true;
834 *type_change_ok
= true;
836 h
->verinfo
.vertree
= NULL
;
839 /* Handle the special case of a weak definition in a regular object
840 followed by a non-weak definition in a shared object. In this
841 case, we prefer the definition in the shared object unless it
842 comes from a DT_NEEDED entry of a shared object, in which case,
843 the DT_NEEDED entry may not be required at the run time. */
846 && h
->root
.type
== bfd_link_hash_defweak
851 /* To make this work we have to frob the flags so that the rest
852 of the code does not think we are using the regular
854 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
855 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
856 else if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
857 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
858 h
->elf_link_hash_flags
&= ~ (ELF_LINK_HASH_DEF_REGULAR
859 | ELF_LINK_HASH_DEF_DYNAMIC
);
861 /* If H is the target of an indirection, we want the caller to
862 use H rather than the indirect symbol. Otherwise if we are
863 defining a new indirect symbol we will wind up attaching it
864 to the entry we are overriding. */
868 /* Handle the special case of a non-weak definition in a shared
869 object followed by a weak definition in a regular object. In
870 this case we prefer to definition in the shared object. To make
871 this work we have to tell the caller to not treat the new symbol
875 && h
->root
.type
!= bfd_link_hash_defweak
884 /* Add symbols from an ELF object file to the linker hash table. */
887 elf_link_add_object_symbols (abfd
, info
)
889 struct bfd_link_info
*info
;
891 boolean (*add_symbol_hook
) PARAMS ((bfd
*, struct bfd_link_info
*,
892 const Elf_Internal_Sym
*,
893 const char **, flagword
*,
894 asection
**, bfd_vma
*));
895 boolean (*check_relocs
) PARAMS ((bfd
*, struct bfd_link_info
*,
896 asection
*, const Elf_Internal_Rela
*));
898 Elf_Internal_Shdr
*hdr
;
902 Elf_External_Sym
*buf
= NULL
;
903 struct elf_link_hash_entry
**sym_hash
;
905 Elf_External_Versym
*extversym
= NULL
;
906 Elf_External_Versym
*ever
;
907 Elf_External_Dyn
*dynbuf
= NULL
;
908 struct elf_link_hash_entry
*weaks
;
909 Elf_External_Sym
*esym
;
910 Elf_External_Sym
*esymend
;
911 struct elf_backend_data
*bed
;
914 bed
= get_elf_backend_data (abfd
);
915 add_symbol_hook
= bed
->elf_add_symbol_hook
;
916 collect
= bed
->collect
;
918 if ((abfd
->flags
& DYNAMIC
) == 0)
924 /* You can't use -r against a dynamic object. Also, there's no
925 hope of using a dynamic object which does not exactly match
926 the format of the output file. */
927 if (info
->relocateable
|| info
->hash
->creator
!= abfd
->xvec
)
929 bfd_set_error (bfd_error_invalid_operation
);
934 /* As a GNU extension, any input sections which are named
935 .gnu.warning.SYMBOL are treated as warning symbols for the given
936 symbol. This differs from .gnu.warning sections, which generate
937 warnings when they are included in an output file. */
942 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
946 name
= bfd_get_section_name (abfd
, s
);
947 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
952 name
+= sizeof ".gnu.warning." - 1;
954 /* If this is a shared object, then look up the symbol
955 in the hash table. If it is there, and it is already
956 been defined, then we will not be using the entry
957 from this shared object, so we don't need to warn.
958 FIXME: If we see the definition in a regular object
959 later on, we will warn, but we shouldn't. The only
960 fix is to keep track of what warnings we are supposed
961 to emit, and then handle them all at the end of the
963 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
965 struct elf_link_hash_entry
*h
;
967 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
970 /* FIXME: What about bfd_link_hash_common? */
972 && (h
->root
.type
== bfd_link_hash_defined
973 || h
->root
.type
== bfd_link_hash_defweak
))
975 /* We don't want to issue this warning. Clobber
976 the section size so that the warning does not
977 get copied into the output file. */
983 sz
= bfd_section_size (abfd
, s
);
984 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
988 if (! bfd_get_section_contents (abfd
, s
, msg
, (file_ptr
) 0, sz
))
993 if (! (_bfd_generic_link_add_one_symbol
994 (info
, abfd
, name
, BSF_WARNING
, s
, (bfd_vma
) 0, msg
,
995 false, collect
, (struct bfd_link_hash_entry
**) NULL
)))
998 if (! info
->relocateable
)
1000 /* Clobber the section size so that the warning does
1001 not get copied into the output file. */
1008 /* If this is a dynamic object, we always link against the .dynsym
1009 symbol table, not the .symtab symbol table. The dynamic linker
1010 will only see the .dynsym symbol table, so there is no reason to
1011 look at .symtab for a dynamic object. */
1013 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
1014 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1016 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1020 /* Read in any version definitions. */
1022 if (! _bfd_elf_slurp_version_tables (abfd
))
1025 /* Read in the symbol versions, but don't bother to convert them
1026 to internal format. */
1027 if (elf_dynversym (abfd
) != 0)
1029 Elf_Internal_Shdr
*versymhdr
;
1031 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
1032 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
1033 if (extversym
== NULL
)
1035 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
1036 || (bfd_read ((PTR
) extversym
, 1, versymhdr
->sh_size
, abfd
)
1037 != versymhdr
->sh_size
))
1042 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
1044 /* The sh_info field of the symtab header tells us where the
1045 external symbols start. We don't care about the local symbols at
1047 if (elf_bad_symtab (abfd
))
1049 extsymcount
= symcount
;
1054 extsymcount
= symcount
- hdr
->sh_info
;
1055 extsymoff
= hdr
->sh_info
;
1058 buf
= ((Elf_External_Sym
*)
1059 bfd_malloc (extsymcount
* sizeof (Elf_External_Sym
)));
1060 if (buf
== NULL
&& extsymcount
!= 0)
1063 /* We store a pointer to the hash table entry for each external
1065 sym_hash
= ((struct elf_link_hash_entry
**)
1067 extsymcount
* sizeof (struct elf_link_hash_entry
*)));
1068 if (sym_hash
== NULL
)
1070 elf_sym_hashes (abfd
) = sym_hash
;
1076 /* If we are creating a shared library, create all the dynamic
1077 sections immediately. We need to attach them to something,
1078 so we attach them to this BFD, provided it is the right
1079 format. FIXME: If there are no input BFD's of the same
1080 format as the output, we can't make a shared library. */
1082 && ! elf_hash_table (info
)->dynamic_sections_created
1083 && abfd
->xvec
== info
->hash
->creator
)
1085 if (! elf_link_create_dynamic_sections (abfd
, info
))
1094 bfd_size_type oldsize
;
1095 bfd_size_type strindex
;
1097 /* Find the name to use in a DT_NEEDED entry that refers to this
1098 object. If the object has a DT_SONAME entry, we use it.
1099 Otherwise, if the generic linker stuck something in
1100 elf_dt_name, we use that. Otherwise, we just use the file
1101 name. If the generic linker put a null string into
1102 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1103 there is a DT_SONAME entry. */
1105 name
= bfd_get_filename (abfd
);
1106 if (elf_dt_name (abfd
) != NULL
)
1108 name
= elf_dt_name (abfd
);
1111 if (elf_dt_soname (abfd
) != NULL
)
1117 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1120 Elf_External_Dyn
*extdyn
;
1121 Elf_External_Dyn
*extdynend
;
1127 dynbuf
= (Elf_External_Dyn
*) bfd_malloc ((size_t) s
->_raw_size
);
1131 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
1132 (file_ptr
) 0, s
->_raw_size
))
1135 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1138 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1141 /* The shared libraries distributed with hpux11 have a bogus
1142 sh_link field for the ".dynamic" section. This code detects
1143 when LINK refers to a section that is not a string table and
1144 tries to find the string table for the ".dynsym" section
1146 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[link
];
1147 if (hdr
->sh_type
!= SHT_STRTAB
)
1149 asection
*s
= bfd_get_section_by_name (abfd
, ".dynsym");
1150 int elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1153 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1158 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
1161 for (; extdyn
< extdynend
; extdyn
++)
1163 Elf_Internal_Dyn dyn
;
1165 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
1166 if (dyn
.d_tag
== DT_SONAME
)
1168 name
= bfd_elf_string_from_elf_section (abfd
, link
,
1173 if (dyn
.d_tag
== DT_NEEDED
)
1175 struct bfd_link_needed_list
*n
, **pn
;
1178 n
= ((struct bfd_link_needed_list
*)
1179 bfd_alloc (abfd
, sizeof (struct bfd_link_needed_list
)));
1180 fnm
= bfd_elf_string_from_elf_section (abfd
, link
,
1182 if (n
== NULL
|| fnm
== NULL
)
1184 anm
= bfd_alloc (abfd
, strlen (fnm
) + 1);
1191 for (pn
= &elf_hash_table (info
)->needed
;
1197 if (dyn
.d_tag
== DT_RUNPATH
)
1199 struct bfd_link_needed_list
*n
, **pn
;
1202 /* When we see DT_RPATH before DT_RUNPATH, we have
1203 to clear runpath. Do _NOT_ bfd_release, as that
1204 frees all more recently bfd_alloc'd blocks as
1206 if (rpath
&& elf_hash_table (info
)->runpath
)
1207 elf_hash_table (info
)->runpath
= NULL
;
1209 n
= ((struct bfd_link_needed_list
*)
1210 bfd_alloc (abfd
, sizeof (struct bfd_link_needed_list
)));
1211 fnm
= bfd_elf_string_from_elf_section (abfd
, link
,
1213 if (n
== NULL
|| fnm
== NULL
)
1215 anm
= bfd_alloc (abfd
, strlen (fnm
) + 1);
1222 for (pn
= &elf_hash_table (info
)->runpath
;
1230 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1231 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
1233 struct bfd_link_needed_list
*n
, **pn
;
1236 n
= ((struct bfd_link_needed_list
*)
1237 bfd_alloc (abfd
, sizeof (struct bfd_link_needed_list
)));
1238 fnm
= bfd_elf_string_from_elf_section (abfd
, link
,
1240 if (n
== NULL
|| fnm
== NULL
)
1242 anm
= bfd_alloc (abfd
, strlen (fnm
) + 1);
1249 for (pn
= &elf_hash_table (info
)->runpath
;
1262 /* We do not want to include any of the sections in a dynamic
1263 object in the output file. We hack by simply clobbering the
1264 list of sections in the BFD. This could be handled more
1265 cleanly by, say, a new section flag; the existing
1266 SEC_NEVER_LOAD flag is not the one we want, because that one
1267 still implies that the section takes up space in the output
1269 abfd
->sections
= NULL
;
1270 abfd
->section_count
= 0;
1272 /* If this is the first dynamic object found in the link, create
1273 the special sections required for dynamic linking. */
1274 if (! elf_hash_table (info
)->dynamic_sections_created
)
1276 if (! elf_link_create_dynamic_sections (abfd
, info
))
1282 /* Add a DT_NEEDED entry for this dynamic object. */
1283 oldsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
1284 strindex
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, name
,
1286 if (strindex
== (bfd_size_type
) -1)
1289 if (oldsize
== _bfd_stringtab_size (elf_hash_table (info
)->dynstr
))
1292 Elf_External_Dyn
*dyncon
, *dynconend
;
1294 /* The hash table size did not change, which means that
1295 the dynamic object name was already entered. If we
1296 have already included this dynamic object in the
1297 link, just ignore it. There is no reason to include
1298 a particular dynamic object more than once. */
1299 sdyn
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
1301 BFD_ASSERT (sdyn
!= NULL
);
1303 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1304 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1306 for (; dyncon
< dynconend
; dyncon
++)
1308 Elf_Internal_Dyn dyn
;
1310 elf_swap_dyn_in (elf_hash_table (info
)->dynobj
, dyncon
,
1312 if (dyn
.d_tag
== DT_NEEDED
1313 && dyn
.d_un
.d_val
== strindex
)
1317 if (extversym
!= NULL
)
1324 if (! elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
1328 /* Save the SONAME, if there is one, because sometimes the
1329 linker emulation code will need to know it. */
1331 name
= basename (bfd_get_filename (abfd
));
1332 elf_dt_name (abfd
) = name
;
1336 hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
),
1338 || (bfd_read ((PTR
) buf
, sizeof (Elf_External_Sym
), extsymcount
, abfd
)
1339 != extsymcount
* sizeof (Elf_External_Sym
)))
1344 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
1345 esymend
= buf
+ extsymcount
;
1348 esym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
1350 Elf_Internal_Sym sym
;
1356 struct elf_link_hash_entry
*h
;
1358 boolean size_change_ok
, type_change_ok
;
1359 boolean new_weakdef
;
1360 unsigned int old_alignment
;
1362 elf_swap_symbol_in (abfd
, esym
, &sym
);
1364 flags
= BSF_NO_FLAGS
;
1366 value
= sym
.st_value
;
1369 bind
= ELF_ST_BIND (sym
.st_info
);
1370 if (bind
== STB_LOCAL
)
1372 /* This should be impossible, since ELF requires that all
1373 global symbols follow all local symbols, and that sh_info
1374 point to the first global symbol. Unfortunatealy, Irix 5
1378 else if (bind
== STB_GLOBAL
)
1380 if (sym
.st_shndx
!= SHN_UNDEF
1381 && sym
.st_shndx
!= SHN_COMMON
)
1384 else if (bind
== STB_WEAK
)
1388 /* Leave it up to the processor backend. */
1391 if (sym
.st_shndx
== SHN_UNDEF
)
1392 sec
= bfd_und_section_ptr
;
1393 else if (sym
.st_shndx
> 0 && sym
.st_shndx
< SHN_LORESERVE
)
1395 sec
= section_from_elf_index (abfd
, sym
.st_shndx
);
1397 sec
= bfd_abs_section_ptr
;
1398 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
1401 else if (sym
.st_shndx
== SHN_ABS
)
1402 sec
= bfd_abs_section_ptr
;
1403 else if (sym
.st_shndx
== SHN_COMMON
)
1405 sec
= bfd_com_section_ptr
;
1406 /* What ELF calls the size we call the value. What ELF
1407 calls the value we call the alignment. */
1408 value
= sym
.st_size
;
1412 /* Leave it up to the processor backend. */
1415 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
1416 if (name
== (const char *) NULL
)
1419 if (add_symbol_hook
)
1421 if (! (*add_symbol_hook
) (abfd
, info
, &sym
, &name
, &flags
, &sec
,
1425 /* The hook function sets the name to NULL if this symbol
1426 should be skipped for some reason. */
1427 if (name
== (const char *) NULL
)
1431 /* Sanity check that all possibilities were handled. */
1432 if (sec
== (asection
*) NULL
)
1434 bfd_set_error (bfd_error_bad_value
);
1438 if (bfd_is_und_section (sec
)
1439 || bfd_is_com_section (sec
))
1444 size_change_ok
= false;
1445 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
1447 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1449 Elf_Internal_Versym iver
;
1450 unsigned int vernum
= 0;
1455 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
1456 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
1458 /* If this is a hidden symbol, or if it is not version
1459 1, we append the version name to the symbol name.
1460 However, we do not modify a non-hidden absolute
1461 symbol, because it might be the version symbol
1462 itself. FIXME: What if it isn't? */
1463 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
1464 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
1467 int namelen
, newlen
;
1470 if (sym
.st_shndx
!= SHN_UNDEF
)
1472 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
1474 (*_bfd_error_handler
)
1475 (_("%s: %s: invalid version %u (max %d)"),
1476 bfd_get_filename (abfd
), name
, vernum
,
1477 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1478 bfd_set_error (bfd_error_bad_value
);
1481 else if (vernum
> 1)
1483 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1489 /* We cannot simply test for the number of
1490 entries in the VERNEED section since the
1491 numbers for the needed versions do not start
1493 Elf_Internal_Verneed
*t
;
1496 for (t
= elf_tdata (abfd
)->verref
;
1500 Elf_Internal_Vernaux
*a
;
1502 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1504 if (a
->vna_other
== vernum
)
1506 verstr
= a
->vna_nodename
;
1515 (*_bfd_error_handler
)
1516 (_("%s: %s: invalid needed version %d"),
1517 bfd_get_filename (abfd
), name
, vernum
);
1518 bfd_set_error (bfd_error_bad_value
);
1523 namelen
= strlen (name
);
1524 newlen
= namelen
+ strlen (verstr
) + 2;
1525 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
1528 newname
= (char *) bfd_alloc (abfd
, newlen
);
1529 if (newname
== NULL
)
1531 strcpy (newname
, name
);
1532 p
= newname
+ namelen
;
1534 /* If this is a defined non-hidden version symbol,
1535 we add another @ to the name. This indicates the
1536 default version of the symbol. */
1537 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1538 && sym
.st_shndx
!= SHN_UNDEF
)
1546 if (! elf_merge_symbol (abfd
, info
, name
, &sym
, &sec
, &value
,
1547 sym_hash
, &override
, &type_change_ok
,
1548 &size_change_ok
, dt_needed
))
1555 while (h
->root
.type
== bfd_link_hash_indirect
1556 || h
->root
.type
== bfd_link_hash_warning
)
1557 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1559 /* Remember the old alignment if this is a common symbol, so
1560 that we don't reduce the alignment later on. We can't
1561 check later, because _bfd_generic_link_add_one_symbol
1562 will set a default for the alignment which we want to
1564 if (h
->root
.type
== bfd_link_hash_common
)
1565 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1567 if (elf_tdata (abfd
)->verdef
!= NULL
1571 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1574 if (! (_bfd_generic_link_add_one_symbol
1575 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
1576 false, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
1580 while (h
->root
.type
== bfd_link_hash_indirect
1581 || h
->root
.type
== bfd_link_hash_warning
)
1582 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1585 new_weakdef
= false;
1588 && (flags
& BSF_WEAK
) != 0
1589 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
1590 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1591 && h
->weakdef
== NULL
)
1593 /* Keep a list of all weak defined non function symbols from
1594 a dynamic object, using the weakdef field. Later in this
1595 function we will set the weakdef field to the correct
1596 value. We only put non-function symbols from dynamic
1597 objects on this list, because that happens to be the only
1598 time we need to know the normal symbol corresponding to a
1599 weak symbol, and the information is time consuming to
1600 figure out. If the weakdef field is not already NULL,
1601 then this symbol was already defined by some previous
1602 dynamic object, and we will be using that previous
1603 definition anyhow. */
1610 /* Set the alignment of a common symbol. */
1611 if (sym
.st_shndx
== SHN_COMMON
1612 && h
->root
.type
== bfd_link_hash_common
)
1616 align
= bfd_log2 (sym
.st_value
);
1617 if (align
> old_alignment
1618 /* Permit an alignment power of zero if an alignment of one
1619 is specified and no other alignments have been specified. */
1620 || (sym
.st_value
== 1 && old_alignment
== 0))
1621 h
->root
.u
.c
.p
->alignment_power
= align
;
1624 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1630 /* Remember the symbol size and type. */
1631 if (sym
.st_size
!= 0
1632 && (definition
|| h
->size
== 0))
1634 if (h
->size
!= 0 && h
->size
!= sym
.st_size
&& ! size_change_ok
)
1635 (*_bfd_error_handler
)
1636 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1637 name
, (unsigned long) h
->size
, (unsigned long) sym
.st_size
,
1638 bfd_get_filename (abfd
));
1640 h
->size
= sym
.st_size
;
1643 /* If this is a common symbol, then we always want H->SIZE
1644 to be the size of the common symbol. The code just above
1645 won't fix the size if a common symbol becomes larger. We
1646 don't warn about a size change here, because that is
1647 covered by --warn-common. */
1648 if (h
->root
.type
== bfd_link_hash_common
)
1649 h
->size
= h
->root
.u
.c
.size
;
1651 if (ELF_ST_TYPE (sym
.st_info
) != STT_NOTYPE
1652 && (definition
|| h
->type
== STT_NOTYPE
))
1654 if (h
->type
!= STT_NOTYPE
1655 && h
->type
!= ELF_ST_TYPE (sym
.st_info
)
1656 && ! type_change_ok
)
1657 (*_bfd_error_handler
)
1658 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1659 name
, h
->type
, ELF_ST_TYPE (sym
.st_info
),
1660 bfd_get_filename (abfd
));
1662 h
->type
= ELF_ST_TYPE (sym
.st_info
);
1665 /* If st_other has a processor-specific meaning, specific code
1666 might be needed here. */
1667 if (sym
.st_other
!= 0)
1669 /* Combine visibilities, using the most constraining one. */
1670 unsigned char hvis
= ELF_ST_VISIBILITY (h
->other
);
1671 unsigned char symvis
= ELF_ST_VISIBILITY (sym
.st_other
);
1673 if (symvis
&& (hvis
> symvis
|| hvis
== 0))
1674 h
->other
= sym
.st_other
;
1676 /* If neither has visibility, use the st_other of the
1677 definition. This is an arbitrary choice, since the
1678 other bits have no general meaning. */
1679 if (!symvis
&& !hvis
1680 && (definition
|| h
->other
== 0))
1681 h
->other
= sym
.st_other
;
1684 /* Set a flag in the hash table entry indicating the type of
1685 reference or definition we just found. Keep a count of
1686 the number of dynamic symbols we find. A dynamic symbol
1687 is one which is referenced or defined by both a regular
1688 object and a shared object. */
1689 old_flags
= h
->elf_link_hash_flags
;
1695 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
1696 if (bind
!= STB_WEAK
)
1697 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
1700 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
1702 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1703 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
1709 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
1711 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
1712 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
1713 | ELF_LINK_HASH_REF_REGULAR
)) != 0
1714 || (h
->weakdef
!= NULL
1716 && h
->weakdef
->dynindx
!= -1))
1720 h
->elf_link_hash_flags
|= new_flag
;
1722 /* If this symbol has a version, and it is the default
1723 version, we create an indirect symbol from the default
1724 name to the fully decorated name. This will cause
1725 external references which do not specify a version to be
1726 bound to this version of the symbol. */
1727 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
1731 p
= strchr (name
, ELF_VER_CHR
);
1732 if (p
!= NULL
&& p
[1] == ELF_VER_CHR
)
1735 struct elf_link_hash_entry
*hi
;
1738 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1740 if (shortname
== NULL
)
1742 strncpy (shortname
, name
, p
- name
);
1743 shortname
[p
- name
] = '\0';
1745 /* We are going to create a new symbol. Merge it
1746 with any existing symbol with this name. For the
1747 purposes of the merge, act as though we were
1748 defining the symbol we just defined, although we
1749 actually going to define an indirect symbol. */
1750 type_change_ok
= false;
1751 size_change_ok
= false;
1752 if (! elf_merge_symbol (abfd
, info
, shortname
, &sym
, &sec
,
1753 &value
, &hi
, &override
,
1755 &size_change_ok
, dt_needed
))
1760 if (! (_bfd_generic_link_add_one_symbol
1761 (info
, abfd
, shortname
, BSF_INDIRECT
,
1762 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1763 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1768 /* In this case the symbol named SHORTNAME is
1769 overriding the indirect symbol we want to
1770 add. We were planning on making SHORTNAME an
1771 indirect symbol referring to NAME. SHORTNAME
1772 is the name without a version. NAME is the
1773 fully versioned name, and it is the default
1776 Overriding means that we already saw a
1777 definition for the symbol SHORTNAME in a
1778 regular object, and it is overriding the
1779 symbol defined in the dynamic object.
1781 When this happens, we actually want to change
1782 NAME, the symbol we just added, to refer to
1783 SHORTNAME. This will cause references to
1784 NAME in the shared object to become
1785 references to SHORTNAME in the regular
1786 object. This is what we expect when we
1787 override a function in a shared object: that
1788 the references in the shared object will be
1789 mapped to the definition in the regular
1792 while (hi
->root
.type
== bfd_link_hash_indirect
1793 || hi
->root
.type
== bfd_link_hash_warning
)
1794 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1796 h
->root
.type
= bfd_link_hash_indirect
;
1797 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1798 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1800 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1801 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1802 if (hi
->elf_link_hash_flags
1803 & (ELF_LINK_HASH_REF_REGULAR
1804 | ELF_LINK_HASH_DEF_REGULAR
))
1806 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1812 /* Now set HI to H, so that the following code
1813 will set the other fields correctly. */
1817 /* If there is a duplicate definition somewhere,
1818 then HI may not point to an indirect symbol. We
1819 will have reported an error to the user in that
1822 if (hi
->root
.type
== bfd_link_hash_indirect
)
1824 struct elf_link_hash_entry
*ht
;
1826 /* If the symbol became indirect, then we assume
1827 that we have not seen a definition before. */
1828 BFD_ASSERT ((hi
->elf_link_hash_flags
1829 & (ELF_LINK_HASH_DEF_DYNAMIC
1830 | ELF_LINK_HASH_DEF_REGULAR
))
1833 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1834 (*bed
->elf_backend_copy_indirect_symbol
) (ht
, hi
);
1836 /* See if the new flags lead us to realize that
1837 the symbol must be dynamic. */
1843 || ((hi
->elf_link_hash_flags
1844 & ELF_LINK_HASH_REF_DYNAMIC
)
1850 if ((hi
->elf_link_hash_flags
1851 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1857 /* We also need to define an indirection from the
1858 nondefault version of the symbol. */
1860 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1862 if (shortname
== NULL
)
1864 strncpy (shortname
, name
, p
- name
);
1865 strcpy (shortname
+ (p
- name
), p
+ 1);
1867 /* Once again, merge with any existing symbol. */
1868 type_change_ok
= false;
1869 size_change_ok
= false;
1870 if (! elf_merge_symbol (abfd
, info
, shortname
, &sym
, &sec
,
1871 &value
, &hi
, &override
,
1873 &size_change_ok
, dt_needed
))
1878 /* Here SHORTNAME is a versioned name, so we
1879 don't expect to see the type of override we
1880 do in the case above. */
1881 (*_bfd_error_handler
)
1882 (_("%s: warning: unexpected redefinition of `%s'"),
1883 bfd_get_filename (abfd
), shortname
);
1887 if (! (_bfd_generic_link_add_one_symbol
1888 (info
, abfd
, shortname
, BSF_INDIRECT
,
1889 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1890 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1893 /* If there is a duplicate definition somewhere,
1894 then HI may not point to an indirect symbol.
1895 We will have reported an error to the user in
1898 if (hi
->root
.type
== bfd_link_hash_indirect
)
1900 /* If the symbol became indirect, then we
1901 assume that we have not seen a definition
1903 BFD_ASSERT ((hi
->elf_link_hash_flags
1904 & (ELF_LINK_HASH_DEF_DYNAMIC
1905 | ELF_LINK_HASH_DEF_REGULAR
))
1908 (*bed
->elf_backend_copy_indirect_symbol
) (h
, hi
);
1910 /* See if the new flags lead us to realize
1911 that the symbol must be dynamic. */
1917 || ((hi
->elf_link_hash_flags
1918 & ELF_LINK_HASH_REF_DYNAMIC
)
1924 if ((hi
->elf_link_hash_flags
1925 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1934 if (dynsym
&& h
->dynindx
== -1)
1936 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1938 if (h
->weakdef
!= NULL
1940 && h
->weakdef
->dynindx
== -1)
1942 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
1946 else if (dynsym
&& h
->dynindx
!= -1)
1947 /* If the symbol already has a dynamic index, but
1948 visibility says it should not be visible, turn it into
1950 switch (ELF_ST_VISIBILITY (h
->other
))
1954 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
1955 (*bed
->elf_backend_hide_symbol
) (info
, h
);
1959 if (dt_needed
&& definition
1960 && (h
->elf_link_hash_flags
1961 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1963 bfd_size_type oldsize
;
1964 bfd_size_type strindex
;
1966 /* The symbol from a DT_NEEDED object is referenced from
1967 the regular object to create a dynamic executable. We
1968 have to make sure there is a DT_NEEDED entry for it. */
1971 oldsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
1972 strindex
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
1973 elf_dt_soname (abfd
),
1975 if (strindex
== (bfd_size_type
) -1)
1979 == _bfd_stringtab_size (elf_hash_table (info
)->dynstr
))
1982 Elf_External_Dyn
*dyncon
, *dynconend
;
1984 sdyn
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
1986 BFD_ASSERT (sdyn
!= NULL
);
1988 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1989 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1991 for (; dyncon
< dynconend
; dyncon
++)
1993 Elf_Internal_Dyn dyn
;
1995 elf_swap_dyn_in (elf_hash_table (info
)->dynobj
,
1997 BFD_ASSERT (dyn
.d_tag
!= DT_NEEDED
||
1998 dyn
.d_un
.d_val
!= strindex
);
2002 if (! elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2008 /* Now set the weakdefs field correctly for all the weak defined
2009 symbols we found. The only way to do this is to search all the
2010 symbols. Since we only need the information for non functions in
2011 dynamic objects, that's the only time we actually put anything on
2012 the list WEAKS. We need this information so that if a regular
2013 object refers to a symbol defined weakly in a dynamic object, the
2014 real symbol in the dynamic object is also put in the dynamic
2015 symbols; we also must arrange for both symbols to point to the
2016 same memory location. We could handle the general case of symbol
2017 aliasing, but a general symbol alias can only be generated in
2018 assembler code, handling it correctly would be very time
2019 consuming, and other ELF linkers don't handle general aliasing
2021 while (weaks
!= NULL
)
2023 struct elf_link_hash_entry
*hlook
;
2026 struct elf_link_hash_entry
**hpp
;
2027 struct elf_link_hash_entry
**hppend
;
2030 weaks
= hlook
->weakdef
;
2031 hlook
->weakdef
= NULL
;
2033 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
2034 || hlook
->root
.type
== bfd_link_hash_defweak
2035 || hlook
->root
.type
== bfd_link_hash_common
2036 || hlook
->root
.type
== bfd_link_hash_indirect
);
2037 slook
= hlook
->root
.u
.def
.section
;
2038 vlook
= hlook
->root
.u
.def
.value
;
2040 hpp
= elf_sym_hashes (abfd
);
2041 hppend
= hpp
+ extsymcount
;
2042 for (; hpp
< hppend
; hpp
++)
2044 struct elf_link_hash_entry
*h
;
2047 if (h
!= NULL
&& h
!= hlook
2048 && h
->root
.type
== bfd_link_hash_defined
2049 && h
->root
.u
.def
.section
== slook
2050 && h
->root
.u
.def
.value
== vlook
)
2054 /* If the weak definition is in the list of dynamic
2055 symbols, make sure the real definition is put there
2057 if (hlook
->dynindx
!= -1
2058 && h
->dynindx
== -1)
2060 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2064 /* If the real definition is in the list of dynamic
2065 symbols, make sure the weak definition is put there
2066 as well. If we don't do this, then the dynamic
2067 loader might not merge the entries for the real
2068 definition and the weak definition. */
2069 if (h
->dynindx
!= -1
2070 && hlook
->dynindx
== -1)
2072 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
2087 if (extversym
!= NULL
)
2093 /* If this object is the same format as the output object, and it is
2094 not a shared library, then let the backend look through the
2097 This is required to build global offset table entries and to
2098 arrange for dynamic relocs. It is not required for the
2099 particular common case of linking non PIC code, even when linking
2100 against shared libraries, but unfortunately there is no way of
2101 knowing whether an object file has been compiled PIC or not.
2102 Looking through the relocs is not particularly time consuming.
2103 The problem is that we must either (1) keep the relocs in memory,
2104 which causes the linker to require additional runtime memory or
2105 (2) read the relocs twice from the input file, which wastes time.
2106 This would be a good case for using mmap.
2108 I have no idea how to handle linking PIC code into a file of a
2109 different format. It probably can't be done. */
2110 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
2112 && abfd
->xvec
== info
->hash
->creator
2113 && check_relocs
!= NULL
)
2117 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2119 Elf_Internal_Rela
*internal_relocs
;
2122 if ((o
->flags
& SEC_RELOC
) == 0
2123 || o
->reloc_count
== 0
2124 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
2125 && (o
->flags
& SEC_DEBUGGING
) != 0)
2126 || bfd_is_abs_section (o
->output_section
))
2129 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
2130 (abfd
, o
, (PTR
) NULL
,
2131 (Elf_Internal_Rela
*) NULL
,
2132 info
->keep_memory
));
2133 if (internal_relocs
== NULL
)
2136 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
2138 if (! info
->keep_memory
)
2139 free (internal_relocs
);
2146 /* If this is a non-traditional, non-relocateable link, try to
2147 optimize the handling of the .stab/.stabstr sections. */
2149 && ! info
->relocateable
2150 && ! info
->traditional_format
2151 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
2152 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
2154 asection
*stab
, *stabstr
;
2156 stab
= bfd_get_section_by_name (abfd
, ".stab");
2159 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
2161 if (stabstr
!= NULL
)
2163 struct bfd_elf_section_data
*secdata
;
2165 secdata
= elf_section_data (stab
);
2166 if (! _bfd_link_section_stabs (abfd
,
2167 &elf_hash_table (info
)->stab_info
,
2169 &secdata
->stab_info
))
2175 if (! info
->relocateable
&& ! dynamic
)
2179 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2180 if ((s
->flags
& SEC_MERGE
)
2181 && ! _bfd_merge_section (abfd
,
2182 &elf_hash_table (info
)->merge_info
,
2183 s
, &elf_section_data (s
)->merge_info
))
2194 if (extversym
!= NULL
)
2199 /* Create some sections which will be filled in with dynamic linking
2200 information. ABFD is an input file which requires dynamic sections
2201 to be created. The dynamic sections take up virtual memory space
2202 when the final executable is run, so we need to create them before
2203 addresses are assigned to the output sections. We work out the
2204 actual contents and size of these sections later. */
2207 elf_link_create_dynamic_sections (abfd
, info
)
2209 struct bfd_link_info
*info
;
2212 register asection
*s
;
2213 struct elf_link_hash_entry
*h
;
2214 struct elf_backend_data
*bed
;
2216 if (elf_hash_table (info
)->dynamic_sections_created
)
2219 /* Make sure that all dynamic sections use the same input BFD. */
2220 if (elf_hash_table (info
)->dynobj
== NULL
)
2221 elf_hash_table (info
)->dynobj
= abfd
;
2223 abfd
= elf_hash_table (info
)->dynobj
;
2225 /* Note that we set the SEC_IN_MEMORY flag for all of these
2227 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
2228 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
2230 /* A dynamically linked executable has a .interp section, but a
2231 shared library does not. */
2234 s
= bfd_make_section (abfd
, ".interp");
2236 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2240 /* Create sections to hold version informations. These are removed
2241 if they are not needed. */
2242 s
= bfd_make_section (abfd
, ".gnu.version_d");
2244 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2245 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2248 s
= bfd_make_section (abfd
, ".gnu.version");
2250 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2251 || ! bfd_set_section_alignment (abfd
, s
, 1))
2254 s
= bfd_make_section (abfd
, ".gnu.version_r");
2256 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2257 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2260 s
= bfd_make_section (abfd
, ".dynsym");
2262 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2263 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2266 s
= bfd_make_section (abfd
, ".dynstr");
2268 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2271 /* Create a strtab to hold the dynamic symbol names. */
2272 if (elf_hash_table (info
)->dynstr
== NULL
)
2274 elf_hash_table (info
)->dynstr
= elf_stringtab_init ();
2275 if (elf_hash_table (info
)->dynstr
== NULL
)
2279 s
= bfd_make_section (abfd
, ".dynamic");
2281 || ! bfd_set_section_flags (abfd
, s
, flags
)
2282 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2285 /* The special symbol _DYNAMIC is always set to the start of the
2286 .dynamic section. This call occurs before we have processed the
2287 symbols for any dynamic object, so we don't have to worry about
2288 overriding a dynamic definition. We could set _DYNAMIC in a
2289 linker script, but we only want to define it if we are, in fact,
2290 creating a .dynamic section. We don't want to define it if there
2291 is no .dynamic section, since on some ELF platforms the start up
2292 code examines it to decide how to initialize the process. */
2294 if (! (_bfd_generic_link_add_one_symbol
2295 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
2296 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
2297 (struct bfd_link_hash_entry
**) &h
)))
2299 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2300 h
->type
= STT_OBJECT
;
2303 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2306 bed
= get_elf_backend_data (abfd
);
2308 s
= bfd_make_section (abfd
, ".hash");
2310 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2311 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2313 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
2315 /* Let the backend create the rest of the sections. This lets the
2316 backend set the right flags. The backend will normally create
2317 the .got and .plt sections. */
2318 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
2321 elf_hash_table (info
)->dynamic_sections_created
= true;
2326 /* Add an entry to the .dynamic table. */
2329 elf_add_dynamic_entry (info
, tag
, val
)
2330 struct bfd_link_info
*info
;
2334 Elf_Internal_Dyn dyn
;
2338 bfd_byte
*newcontents
;
2340 dynobj
= elf_hash_table (info
)->dynobj
;
2342 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
2343 BFD_ASSERT (s
!= NULL
);
2345 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
2346 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
2347 if (newcontents
== NULL
)
2351 dyn
.d_un
.d_val
= val
;
2352 elf_swap_dyn_out (dynobj
, &dyn
,
2353 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
2355 s
->_raw_size
= newsize
;
2356 s
->contents
= newcontents
;
2361 /* Record a new local dynamic symbol. */
2364 elf_link_record_local_dynamic_symbol (info
, input_bfd
, input_indx
)
2365 struct bfd_link_info
*info
;
2369 struct elf_link_local_dynamic_entry
*entry
;
2370 struct elf_link_hash_table
*eht
;
2371 struct bfd_strtab_hash
*dynstr
;
2372 Elf_External_Sym esym
;
2373 unsigned long dynstr_index
;
2376 /* See if the entry exists already. */
2377 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
2378 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
2381 entry
= (struct elf_link_local_dynamic_entry
*)
2382 bfd_alloc (input_bfd
, sizeof (*entry
));
2386 /* Go find the symbol, so that we can find it's name. */
2387 if (bfd_seek (input_bfd
,
2388 (elf_tdata (input_bfd
)->symtab_hdr
.sh_offset
2389 + input_indx
* sizeof (Elf_External_Sym
)),
2391 || (bfd_read (&esym
, sizeof (Elf_External_Sym
), 1, input_bfd
)
2392 != sizeof (Elf_External_Sym
)))
2394 elf_swap_symbol_in (input_bfd
, &esym
, &entry
->isym
);
2396 name
= (bfd_elf_string_from_elf_section
2397 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
2398 entry
->isym
.st_name
));
2400 dynstr
= elf_hash_table (info
)->dynstr
;
2403 /* Create a strtab to hold the dynamic symbol names. */
2404 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_stringtab_init ();
2409 dynstr_index
= _bfd_stringtab_add (dynstr
, name
, true, false);
2410 if (dynstr_index
== (unsigned long) -1)
2412 entry
->isym
.st_name
= dynstr_index
;
2414 eht
= elf_hash_table (info
);
2416 entry
->next
= eht
->dynlocal
;
2417 eht
->dynlocal
= entry
;
2418 entry
->input_bfd
= input_bfd
;
2419 entry
->input_indx
= input_indx
;
2422 /* Whatever binding the symbol had before, it's now local. */
2424 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
2426 /* The dynindx will be set at the end of size_dynamic_sections. */
2431 /* Read and swap the relocs from the section indicated by SHDR. This
2432 may be either a REL or a RELA section. The relocations are
2433 translated into RELA relocations and stored in INTERNAL_RELOCS,
2434 which should have already been allocated to contain enough space.
2435 The EXTERNAL_RELOCS are a buffer where the external form of the
2436 relocations should be stored.
2438 Returns false if something goes wrong. */
2441 elf_link_read_relocs_from_section (abfd
, shdr
, external_relocs
,
2444 Elf_Internal_Shdr
*shdr
;
2445 PTR external_relocs
;
2446 Elf_Internal_Rela
*internal_relocs
;
2448 struct elf_backend_data
*bed
;
2450 /* If there aren't any relocations, that's OK. */
2454 /* Position ourselves at the start of the section. */
2455 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2458 /* Read the relocations. */
2459 if (bfd_read (external_relocs
, 1, shdr
->sh_size
, abfd
)
2463 bed
= get_elf_backend_data (abfd
);
2465 /* Convert the external relocations to the internal format. */
2466 if (shdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2468 Elf_External_Rel
*erel
;
2469 Elf_External_Rel
*erelend
;
2470 Elf_Internal_Rela
*irela
;
2471 Elf_Internal_Rel
*irel
;
2473 erel
= (Elf_External_Rel
*) external_relocs
;
2474 erelend
= erel
+ NUM_SHDR_ENTRIES (shdr
);
2475 irela
= internal_relocs
;
2476 irel
= bfd_alloc (abfd
, (bed
->s
->int_rels_per_ext_rel
2477 * sizeof (Elf_Internal_Rel
)));
2478 for (; erel
< erelend
; erel
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2482 if (bed
->s
->swap_reloc_in
)
2483 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
2485 elf_swap_reloc_in (abfd
, erel
, irel
);
2487 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; ++i
)
2489 irela
[i
].r_offset
= irel
[i
].r_offset
;
2490 irela
[i
].r_info
= irel
[i
].r_info
;
2491 irela
[i
].r_addend
= 0;
2497 Elf_External_Rela
*erela
;
2498 Elf_External_Rela
*erelaend
;
2499 Elf_Internal_Rela
*irela
;
2501 BFD_ASSERT (shdr
->sh_entsize
== sizeof (Elf_External_Rela
));
2503 erela
= (Elf_External_Rela
*) external_relocs
;
2504 erelaend
= erela
+ NUM_SHDR_ENTRIES (shdr
);
2505 irela
= internal_relocs
;
2506 for (; erela
< erelaend
; erela
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2508 if (bed
->s
->swap_reloca_in
)
2509 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
2511 elf_swap_reloca_in (abfd
, erela
, irela
);
2518 /* Read and swap the relocs for a section O. They may have been
2519 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2520 not NULL, they are used as buffers to read into. They are known to
2521 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2522 the return value is allocated using either malloc or bfd_alloc,
2523 according to the KEEP_MEMORY argument. If O has two relocation
2524 sections (both REL and RELA relocations), then the REL_HDR
2525 relocations will appear first in INTERNAL_RELOCS, followed by the
2526 REL_HDR2 relocations. */
2529 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
2533 PTR external_relocs
;
2534 Elf_Internal_Rela
*internal_relocs
;
2535 boolean keep_memory
;
2537 Elf_Internal_Shdr
*rel_hdr
;
2539 Elf_Internal_Rela
*alloc2
= NULL
;
2540 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2542 if (elf_section_data (o
)->relocs
!= NULL
)
2543 return elf_section_data (o
)->relocs
;
2545 if (o
->reloc_count
== 0)
2548 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2550 if (internal_relocs
== NULL
)
2554 size
= (o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
2555 * sizeof (Elf_Internal_Rela
));
2557 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2559 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2560 if (internal_relocs
== NULL
)
2564 if (external_relocs
== NULL
)
2566 size_t size
= (size_t) rel_hdr
->sh_size
;
2568 if (elf_section_data (o
)->rel_hdr2
)
2569 size
+= (size_t) elf_section_data (o
)->rel_hdr2
->sh_size
;
2570 alloc1
= (PTR
) bfd_malloc (size
);
2573 external_relocs
= alloc1
;
2576 if (!elf_link_read_relocs_from_section (abfd
, rel_hdr
,
2580 if (!elf_link_read_relocs_from_section
2582 elf_section_data (o
)->rel_hdr2
,
2583 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2584 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2585 * bed
->s
->int_rels_per_ext_rel
)))
2588 /* Cache the results for next time, if we can. */
2590 elf_section_data (o
)->relocs
= internal_relocs
;
2595 /* Don't free alloc2, since if it was allocated we are passing it
2596 back (under the name of internal_relocs). */
2598 return internal_relocs
;
2608 /* Record an assignment to a symbol made by a linker script. We need
2609 this in case some dynamic object refers to this symbol. */
2612 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
2613 bfd
*output_bfd ATTRIBUTE_UNUSED
;
2614 struct bfd_link_info
*info
;
2618 struct elf_link_hash_entry
*h
;
2620 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2623 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, true, false);
2627 if (h
->root
.type
== bfd_link_hash_new
)
2628 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_ELF
;
2630 /* If this symbol is being provided by the linker script, and it is
2631 currently defined by a dynamic object, but not by a regular
2632 object, then mark it as undefined so that the generic linker will
2633 force the correct value. */
2635 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2636 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2637 h
->root
.type
= bfd_link_hash_undefined
;
2639 /* If this symbol is not being provided by the linker script, and it is
2640 currently defined by a dynamic object, but not by a regular object,
2641 then clear out any version information because the symbol will not be
2642 associated with the dynamic object any more. */
2644 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2645 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2646 h
->verinfo
.verdef
= NULL
;
2648 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2650 /* When possible, keep the original type of the symbol. */
2651 if (h
->type
== STT_NOTYPE
)
2652 h
->type
= STT_OBJECT
;
2654 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2655 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
2657 && h
->dynindx
== -1)
2659 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2662 /* If this is a weak defined symbol, and we know a corresponding
2663 real symbol from the same dynamic object, make sure the real
2664 symbol is also made into a dynamic symbol. */
2665 if (h
->weakdef
!= NULL
2666 && h
->weakdef
->dynindx
== -1)
2668 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2676 /* This structure is used to pass information to
2677 elf_link_assign_sym_version. */
2679 struct elf_assign_sym_version_info
2683 /* General link information. */
2684 struct bfd_link_info
*info
;
2686 struct bfd_elf_version_tree
*verdefs
;
2687 /* Whether we had a failure. */
2691 /* This structure is used to pass information to
2692 elf_link_find_version_dependencies. */
2694 struct elf_find_verdep_info
2698 /* General link information. */
2699 struct bfd_link_info
*info
;
2700 /* The number of dependencies. */
2702 /* Whether we had a failure. */
2706 /* Array used to determine the number of hash table buckets to use
2707 based on the number of symbols there are. If there are fewer than
2708 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2709 fewer than 37 we use 17 buckets, and so forth. We never use more
2710 than 32771 buckets. */
2712 static const size_t elf_buckets
[] =
2714 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2718 /* Compute bucket count for hashing table. We do not use a static set
2719 of possible tables sizes anymore. Instead we determine for all
2720 possible reasonable sizes of the table the outcome (i.e., the
2721 number of collisions etc) and choose the best solution. The
2722 weighting functions are not too simple to allow the table to grow
2723 without bounds. Instead one of the weighting factors is the size.
2724 Therefore the result is always a good payoff between few collisions
2725 (= short chain lengths) and table size. */
2727 compute_bucket_count (info
)
2728 struct bfd_link_info
*info
;
2730 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2731 size_t best_size
= 0;
2732 unsigned long int *hashcodes
;
2733 unsigned long int *hashcodesp
;
2734 unsigned long int i
;
2736 /* Compute the hash values for all exported symbols. At the same
2737 time store the values in an array so that we could use them for
2739 hashcodes
= (unsigned long int *) bfd_malloc (dynsymcount
2740 * sizeof (unsigned long int));
2741 if (hashcodes
== NULL
)
2743 hashcodesp
= hashcodes
;
2745 /* Put all hash values in HASHCODES. */
2746 elf_link_hash_traverse (elf_hash_table (info
),
2747 elf_collect_hash_codes
, &hashcodesp
);
2749 /* We have a problem here. The following code to optimize the table
2750 size requires an integer type with more the 32 bits. If
2751 BFD_HOST_U_64_BIT is set we know about such a type. */
2752 #ifdef BFD_HOST_U_64_BIT
2753 if (info
->optimize
== true)
2755 unsigned long int nsyms
= hashcodesp
- hashcodes
;
2758 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
2759 unsigned long int *counts
;
2761 /* Possible optimization parameters: if we have NSYMS symbols we say
2762 that the hashing table must at least have NSYMS/4 and at most
2764 minsize
= nsyms
/ 4;
2767 best_size
= maxsize
= nsyms
* 2;
2769 /* Create array where we count the collisions in. We must use bfd_malloc
2770 since the size could be large. */
2771 counts
= (unsigned long int *) bfd_malloc (maxsize
2772 * sizeof (unsigned long int));
2779 /* Compute the "optimal" size for the hash table. The criteria is a
2780 minimal chain length. The minor criteria is (of course) the size
2782 for (i
= minsize
; i
< maxsize
; ++i
)
2784 /* Walk through the array of hashcodes and count the collisions. */
2785 BFD_HOST_U_64_BIT max
;
2786 unsigned long int j
;
2787 unsigned long int fact
;
2789 memset (counts
, '\0', i
* sizeof (unsigned long int));
2791 /* Determine how often each hash bucket is used. */
2792 for (j
= 0; j
< nsyms
; ++j
)
2793 ++counts
[hashcodes
[j
] % i
];
2795 /* For the weight function we need some information about the
2796 pagesize on the target. This is information need not be 100%
2797 accurate. Since this information is not available (so far) we
2798 define it here to a reasonable default value. If it is crucial
2799 to have a better value some day simply define this value. */
2800 # ifndef BFD_TARGET_PAGESIZE
2801 # define BFD_TARGET_PAGESIZE (4096)
2804 /* We in any case need 2 + NSYMS entries for the size values and
2806 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
2809 /* Variant 1: optimize for short chains. We add the squares
2810 of all the chain lengths (which favous many small chain
2811 over a few long chains). */
2812 for (j
= 0; j
< i
; ++j
)
2813 max
+= counts
[j
] * counts
[j
];
2815 /* This adds penalties for the overall size of the table. */
2816 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2819 /* Variant 2: Optimize a lot more for small table. Here we
2820 also add squares of the size but we also add penalties for
2821 empty slots (the +1 term). */
2822 for (j
= 0; j
< i
; ++j
)
2823 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
2825 /* The overall size of the table is considered, but not as
2826 strong as in variant 1, where it is squared. */
2827 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2831 /* Compare with current best results. */
2832 if (max
< best_chlen
)
2842 #endif /* defined (BFD_HOST_U_64_BIT) */
2844 /* This is the fallback solution if no 64bit type is available or if we
2845 are not supposed to spend much time on optimizations. We select the
2846 bucket count using a fixed set of numbers. */
2847 for (i
= 0; elf_buckets
[i
] != 0; i
++)
2849 best_size
= elf_buckets
[i
];
2850 if (dynsymcount
< elf_buckets
[i
+ 1])
2855 /* Free the arrays we needed. */
2861 /* Set up the sizes and contents of the ELF dynamic sections. This is
2862 called by the ELF linker emulation before_allocation routine. We
2863 must set the sizes of the sections before the linker sets the
2864 addresses of the various sections. */
2867 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
2869 auxiliary_filters
, info
, sinterpptr
,
2874 const char *filter_shlib
;
2875 const char * const *auxiliary_filters
;
2876 struct bfd_link_info
*info
;
2877 asection
**sinterpptr
;
2878 struct bfd_elf_version_tree
*verdefs
;
2880 bfd_size_type soname_indx
;
2882 struct elf_backend_data
*bed
;
2883 struct elf_assign_sym_version_info asvinfo
;
2887 soname_indx
= (bfd_size_type
) -1;
2889 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2892 /* The backend may have to create some sections regardless of whether
2893 we're dynamic or not. */
2894 bed
= get_elf_backend_data (output_bfd
);
2895 if (bed
->elf_backend_always_size_sections
2896 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
2899 dynobj
= elf_hash_table (info
)->dynobj
;
2901 /* If there were no dynamic objects in the link, there is nothing to
2906 if (elf_hash_table (info
)->dynamic_sections_created
)
2908 struct elf_info_failed eif
;
2909 struct elf_link_hash_entry
*h
;
2912 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
2913 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
2917 soname_indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2918 soname
, true, true);
2919 if (soname_indx
== (bfd_size_type
) -1
2920 || ! elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
2926 if (! elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
2928 info
->flags
|= DF_SYMBOLIC
;
2935 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, rpath
,
2937 if (indx
== (bfd_size_type
) -1
2938 || ! elf_add_dynamic_entry (info
, DT_RPATH
, indx
)
2940 && ! elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
)))
2944 if (filter_shlib
!= NULL
)
2948 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2949 filter_shlib
, true, true);
2950 if (indx
== (bfd_size_type
) -1
2951 || ! elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
2955 if (auxiliary_filters
!= NULL
)
2957 const char * const *p
;
2959 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
2963 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2965 if (indx
== (bfd_size_type
) -1
2966 || ! elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
2972 eif
.verdefs
= verdefs
;
2975 /* If we are supposed to export all symbols into the dynamic symbol
2976 table (this is not the normal case), then do so. */
2977 if (info
->export_dynamic
)
2979 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
2985 /* Attach all the symbols to their version information. */
2986 asvinfo
.output_bfd
= output_bfd
;
2987 asvinfo
.info
= info
;
2988 asvinfo
.verdefs
= verdefs
;
2989 asvinfo
.failed
= false;
2991 elf_link_hash_traverse (elf_hash_table (info
),
2992 elf_link_assign_sym_version
,
2997 /* Find all symbols which were defined in a dynamic object and make
2998 the backend pick a reasonable value for them. */
2999 elf_link_hash_traverse (elf_hash_table (info
),
3000 elf_adjust_dynamic_symbol
,
3005 /* Add some entries to the .dynamic section. We fill in some of the
3006 values later, in elf_bfd_final_link, but we must add the entries
3007 now so that we know the final size of the .dynamic section. */
3009 /* If there are initialization and/or finalization functions to
3010 call then add the corresponding DT_INIT/DT_FINI entries. */
3011 h
= (info
->init_function
3012 ? elf_link_hash_lookup (elf_hash_table (info
),
3013 info
->init_function
, false,
3017 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3018 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3020 if (! elf_add_dynamic_entry (info
, DT_INIT
, 0))
3023 h
= (info
->fini_function
3024 ? elf_link_hash_lookup (elf_hash_table (info
),
3025 info
->fini_function
, false,
3029 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3030 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3032 if (! elf_add_dynamic_entry (info
, DT_FINI
, 0))
3036 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
3037 /* If .dynstr is excluded from the link, we don't want any of
3038 these tags. Strictly, we should be checking each section
3039 individually; This quick check covers for the case where
3040 someone does a /DISCARD/ : { *(*) }. */
3041 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
3043 bfd_size_type strsize
;
3045 strsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
3046 if (! elf_add_dynamic_entry (info
, DT_HASH
, 0)
3047 || ! elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
3048 || ! elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
3049 || ! elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
3050 || ! elf_add_dynamic_entry (info
, DT_SYMENT
,
3051 sizeof (Elf_External_Sym
)))
3056 /* The backend must work out the sizes of all the other dynamic
3058 if (bed
->elf_backend_size_dynamic_sections
3059 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
3062 if (elf_hash_table (info
)->dynamic_sections_created
)
3066 size_t bucketcount
= 0;
3067 size_t hash_entry_size
;
3069 /* Set up the version definition section. */
3070 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3071 BFD_ASSERT (s
!= NULL
);
3073 /* We may have created additional version definitions if we are
3074 just linking a regular application. */
3075 verdefs
= asvinfo
.verdefs
;
3077 if (verdefs
== NULL
)
3078 _bfd_strip_section_from_output (info
, s
);
3083 struct bfd_elf_version_tree
*t
;
3085 Elf_Internal_Verdef def
;
3086 Elf_Internal_Verdaux defaux
;
3091 /* Make space for the base version. */
3092 size
+= sizeof (Elf_External_Verdef
);
3093 size
+= sizeof (Elf_External_Verdaux
);
3096 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3098 struct bfd_elf_version_deps
*n
;
3100 size
+= sizeof (Elf_External_Verdef
);
3101 size
+= sizeof (Elf_External_Verdaux
);
3104 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3105 size
+= sizeof (Elf_External_Verdaux
);
3108 s
->_raw_size
= size
;
3109 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3110 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3113 /* Fill in the version definition section. */
3117 def
.vd_version
= VER_DEF_CURRENT
;
3118 def
.vd_flags
= VER_FLG_BASE
;
3121 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3122 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3123 + sizeof (Elf_External_Verdaux
));
3125 if (soname_indx
!= (bfd_size_type
) -1)
3127 def
.vd_hash
= bfd_elf_hash (soname
);
3128 defaux
.vda_name
= soname_indx
;
3135 name
= basename (output_bfd
->filename
);
3136 def
.vd_hash
= bfd_elf_hash (name
);
3137 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3139 if (indx
== (bfd_size_type
) -1)
3141 defaux
.vda_name
= indx
;
3143 defaux
.vda_next
= 0;
3145 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3146 (Elf_External_Verdef
*) p
);
3147 p
+= sizeof (Elf_External_Verdef
);
3148 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3149 (Elf_External_Verdaux
*) p
);
3150 p
+= sizeof (Elf_External_Verdaux
);
3152 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3155 struct bfd_elf_version_deps
*n
;
3156 struct elf_link_hash_entry
*h
;
3159 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3162 /* Add a symbol representing this version. */
3164 if (! (_bfd_generic_link_add_one_symbol
3165 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
3166 (bfd_vma
) 0, (const char *) NULL
, false,
3167 get_elf_backend_data (dynobj
)->collect
,
3168 (struct bfd_link_hash_entry
**) &h
)))
3170 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
3171 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3172 h
->type
= STT_OBJECT
;
3173 h
->verinfo
.vertree
= t
;
3175 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
3178 def
.vd_version
= VER_DEF_CURRENT
;
3180 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
3181 def
.vd_flags
|= VER_FLG_WEAK
;
3182 def
.vd_ndx
= t
->vernum
+ 1;
3183 def
.vd_cnt
= cdeps
+ 1;
3184 def
.vd_hash
= bfd_elf_hash (t
->name
);
3185 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3186 if (t
->next
!= NULL
)
3187 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3188 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
3192 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3193 (Elf_External_Verdef
*) p
);
3194 p
+= sizeof (Elf_External_Verdef
);
3196 defaux
.vda_name
= h
->dynstr_index
;
3197 if (t
->deps
== NULL
)
3198 defaux
.vda_next
= 0;
3200 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3201 t
->name_indx
= defaux
.vda_name
;
3203 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3204 (Elf_External_Verdaux
*) p
);
3205 p
+= sizeof (Elf_External_Verdaux
);
3207 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3209 if (n
->version_needed
== NULL
)
3211 /* This can happen if there was an error in the
3213 defaux
.vda_name
= 0;
3216 defaux
.vda_name
= n
->version_needed
->name_indx
;
3217 if (n
->next
== NULL
)
3218 defaux
.vda_next
= 0;
3220 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3222 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3223 (Elf_External_Verdaux
*) p
);
3224 p
+= sizeof (Elf_External_Verdaux
);
3228 if (! elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
3229 || ! elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
3232 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
3235 if (info
->new_dtags
&& info
->flags
)
3237 if (! elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
3244 info
->flags_1
&= ~ (DF_1_INITFIRST
3247 if (! elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
3251 /* Work out the size of the version reference section. */
3253 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3254 BFD_ASSERT (s
!= NULL
);
3256 struct elf_find_verdep_info sinfo
;
3258 sinfo
.output_bfd
= output_bfd
;
3260 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
3261 if (sinfo
.vers
== 0)
3263 sinfo
.failed
= false;
3265 elf_link_hash_traverse (elf_hash_table (info
),
3266 elf_link_find_version_dependencies
,
3269 if (elf_tdata (output_bfd
)->verref
== NULL
)
3270 _bfd_strip_section_from_output (info
, s
);
3273 Elf_Internal_Verneed
*t
;
3278 /* Build the version definition section. */
3281 for (t
= elf_tdata (output_bfd
)->verref
;
3285 Elf_Internal_Vernaux
*a
;
3287 size
+= sizeof (Elf_External_Verneed
);
3289 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3290 size
+= sizeof (Elf_External_Vernaux
);
3293 s
->_raw_size
= size
;
3294 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, size
);
3295 if (s
->contents
== NULL
)
3299 for (t
= elf_tdata (output_bfd
)->verref
;
3304 Elf_Internal_Vernaux
*a
;
3308 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3311 t
->vn_version
= VER_NEED_CURRENT
;
3313 if (elf_dt_name (t
->vn_bfd
) != NULL
)
3314 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3315 elf_dt_name (t
->vn_bfd
),
3318 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3319 basename (t
->vn_bfd
->filename
),
3321 if (indx
== (bfd_size_type
) -1)
3324 t
->vn_aux
= sizeof (Elf_External_Verneed
);
3325 if (t
->vn_nextref
== NULL
)
3328 t
->vn_next
= (sizeof (Elf_External_Verneed
)
3329 + caux
* sizeof (Elf_External_Vernaux
));
3331 _bfd_elf_swap_verneed_out (output_bfd
, t
,
3332 (Elf_External_Verneed
*) p
);
3333 p
+= sizeof (Elf_External_Verneed
);
3335 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3337 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
3338 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3339 a
->vna_nodename
, true, false);
3340 if (indx
== (bfd_size_type
) -1)
3343 if (a
->vna_nextptr
== NULL
)
3346 a
->vna_next
= sizeof (Elf_External_Vernaux
);
3348 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
3349 (Elf_External_Vernaux
*) p
);
3350 p
+= sizeof (Elf_External_Vernaux
);
3354 if (! elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
3355 || ! elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
3358 elf_tdata (output_bfd
)->cverrefs
= crefs
;
3362 /* Assign dynsym indicies. In a shared library we generate a
3363 section symbol for each output section, which come first.
3364 Next come all of the back-end allocated local dynamic syms,
3365 followed by the rest of the global symbols. */
3367 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3369 /* Work out the size of the symbol version section. */
3370 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3371 BFD_ASSERT (s
!= NULL
);
3372 if (dynsymcount
== 0
3373 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
3375 _bfd_strip_section_from_output (info
, s
);
3376 /* The DYNSYMCOUNT might have changed if we were going to
3377 output a dynamic symbol table entry for S. */
3378 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3382 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
3383 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3384 if (s
->contents
== NULL
)
3387 if (! elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
3391 /* Set the size of the .dynsym and .hash sections. We counted
3392 the number of dynamic symbols in elf_link_add_object_symbols.
3393 We will build the contents of .dynsym and .hash when we build
3394 the final symbol table, because until then we do not know the
3395 correct value to give the symbols. We built the .dynstr
3396 section as we went along in elf_link_add_object_symbols. */
3397 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
3398 BFD_ASSERT (s
!= NULL
);
3399 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
3400 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3401 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3404 if (dynsymcount
!= 0)
3406 Elf_Internal_Sym isym
;
3408 /* The first entry in .dynsym is a dummy symbol. */
3415 elf_swap_symbol_out (output_bfd
, &isym
,
3416 (PTR
) (Elf_External_Sym
*) s
->contents
);
3419 /* Compute the size of the hashing table. As a side effect this
3420 computes the hash values for all the names we export. */
3421 bucketcount
= compute_bucket_count (info
);
3423 s
= bfd_get_section_by_name (dynobj
, ".hash");
3424 BFD_ASSERT (s
!= NULL
);
3425 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
3426 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
3427 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3428 if (s
->contents
== NULL
)
3430 memset (s
->contents
, 0, (size_t) s
->_raw_size
);
3432 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
3433 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
3434 s
->contents
+ hash_entry_size
);
3436 elf_hash_table (info
)->bucketcount
= bucketcount
;
3438 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
3439 BFD_ASSERT (s
!= NULL
);
3440 s
->_raw_size
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
3442 if (! elf_add_dynamic_entry (info
, DT_NULL
, 0))
3449 /* Fix up the flags for a symbol. This handles various cases which
3450 can only be fixed after all the input files are seen. This is
3451 currently called by both adjust_dynamic_symbol and
3452 assign_sym_version, which is unnecessary but perhaps more robust in
3453 the face of future changes. */
3456 elf_fix_symbol_flags (h
, eif
)
3457 struct elf_link_hash_entry
*h
;
3458 struct elf_info_failed
*eif
;
3460 /* If this symbol was mentioned in a non-ELF file, try to set
3461 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3462 permit a non-ELF file to correctly refer to a symbol defined in
3463 an ELF dynamic object. */
3464 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
3466 while (h
->root
.type
== bfd_link_hash_indirect
)
3467 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3469 if (h
->root
.type
!= bfd_link_hash_defined
3470 && h
->root
.type
!= bfd_link_hash_defweak
)
3471 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3472 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3475 if (h
->root
.u
.def
.section
->owner
!= NULL
3476 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3477 == bfd_target_elf_flavour
))
3478 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3479 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3481 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3484 if (h
->dynindx
== -1
3485 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
3486 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
3488 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3497 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3498 was first seen in a non-ELF file. Fortunately, if the symbol
3499 was first seen in an ELF file, we're probably OK unless the
3500 symbol was defined in a non-ELF file. Catch that case here.
3501 FIXME: We're still in trouble if the symbol was first seen in
3502 a dynamic object, and then later in a non-ELF regular object. */
3503 if ((h
->root
.type
== bfd_link_hash_defined
3504 || h
->root
.type
== bfd_link_hash_defweak
)
3505 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3506 && (h
->root
.u
.def
.section
->owner
!= NULL
3507 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3508 != bfd_target_elf_flavour
)
3509 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3510 && (h
->elf_link_hash_flags
3511 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
3512 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3515 /* If this is a final link, and the symbol was defined as a common
3516 symbol in a regular object file, and there was no definition in
3517 any dynamic object, then the linker will have allocated space for
3518 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3519 flag will not have been set. */
3520 if (h
->root
.type
== bfd_link_hash_defined
3521 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3522 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
3523 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3524 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3525 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3527 /* If -Bsymbolic was used (which means to bind references to global
3528 symbols to the definition within the shared object), and this
3529 symbol was defined in a regular object, then it actually doesn't
3530 need a PLT entry, and we can accomplish that by forcing it local.
3531 Likewise, if the symbol has hidden or internal visibility.
3532 FIXME: It might be that we also do not need a PLT for other
3533 non-hidden visibilities, but we would have to tell that to the
3534 backend specifically; we can't just clear PLT-related data here. */
3535 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
3536 && eif
->info
->shared
3537 && (eif
->info
->symbolic
3538 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3539 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3540 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3542 struct elf_backend_data
*bed
;
3543 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3544 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3545 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3546 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3547 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
);
3550 /* If this is a weak defined symbol in a dynamic object, and we know
3551 the real definition in the dynamic object, copy interesting flags
3552 over to the real definition. */
3553 if (h
->weakdef
!= NULL
)
3555 struct elf_link_hash_entry
*weakdef
;
3557 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3558 || h
->root
.type
== bfd_link_hash_defweak
);
3559 weakdef
= h
->weakdef
;
3560 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
3561 || weakdef
->root
.type
== bfd_link_hash_defweak
);
3562 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
3564 /* If the real definition is defined by a regular object file,
3565 don't do anything special. See the longer description in
3566 elf_adjust_dynamic_symbol, below. */
3567 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3570 weakdef
->elf_link_hash_flags
|=
3571 (h
->elf_link_hash_flags
3572 & (ELF_LINK_HASH_REF_REGULAR
3573 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
3574 | ELF_LINK_NON_GOT_REF
));
3580 /* Make the backend pick a good value for a dynamic symbol. This is
3581 called via elf_link_hash_traverse, and also calls itself
3585 elf_adjust_dynamic_symbol (h
, data
)
3586 struct elf_link_hash_entry
*h
;
3589 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3591 struct elf_backend_data
*bed
;
3593 /* Ignore indirect symbols. These are added by the versioning code. */
3594 if (h
->root
.type
== bfd_link_hash_indirect
)
3597 /* Fix the symbol flags. */
3598 if (! elf_fix_symbol_flags (h
, eif
))
3601 /* If this symbol does not require a PLT entry, and it is not
3602 defined by a dynamic object, or is not referenced by a regular
3603 object, ignore it. We do have to handle a weak defined symbol,
3604 even if no regular object refers to it, if we decided to add it
3605 to the dynamic symbol table. FIXME: Do we normally need to worry
3606 about symbols which are defined by one dynamic object and
3607 referenced by another one? */
3608 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
3609 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
3610 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3611 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
3612 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
3614 h
->plt
.offset
= (bfd_vma
) -1;
3618 /* If we've already adjusted this symbol, don't do it again. This
3619 can happen via a recursive call. */
3620 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
3623 /* Don't look at this symbol again. Note that we must set this
3624 after checking the above conditions, because we may look at a
3625 symbol once, decide not to do anything, and then get called
3626 recursively later after REF_REGULAR is set below. */
3627 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
3629 /* If this is a weak definition, and we know a real definition, and
3630 the real symbol is not itself defined by a regular object file,
3631 then get a good value for the real definition. We handle the
3632 real symbol first, for the convenience of the backend routine.
3634 Note that there is a confusing case here. If the real definition
3635 is defined by a regular object file, we don't get the real symbol
3636 from the dynamic object, but we do get the weak symbol. If the
3637 processor backend uses a COPY reloc, then if some routine in the
3638 dynamic object changes the real symbol, we will not see that
3639 change in the corresponding weak symbol. This is the way other
3640 ELF linkers work as well, and seems to be a result of the shared
3643 I will clarify this issue. Most SVR4 shared libraries define the
3644 variable _timezone and define timezone as a weak synonym. The
3645 tzset call changes _timezone. If you write
3646 extern int timezone;
3648 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3649 you might expect that, since timezone is a synonym for _timezone,
3650 the same number will print both times. However, if the processor
3651 backend uses a COPY reloc, then actually timezone will be copied
3652 into your process image, and, since you define _timezone
3653 yourself, _timezone will not. Thus timezone and _timezone will
3654 wind up at different memory locations. The tzset call will set
3655 _timezone, leaving timezone unchanged. */
3657 if (h
->weakdef
!= NULL
)
3659 /* If we get to this point, we know there is an implicit
3660 reference by a regular object file via the weak symbol H.
3661 FIXME: Is this really true? What if the traversal finds
3662 H->WEAKDEF before it finds H? */
3663 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
3665 if (! elf_adjust_dynamic_symbol (h
->weakdef
, (PTR
) eif
))
3669 /* If a symbol has no type and no size and does not require a PLT
3670 entry, then we are probably about to do the wrong thing here: we
3671 are probably going to create a COPY reloc for an empty object.
3672 This case can arise when a shared object is built with assembly
3673 code, and the assembly code fails to set the symbol type. */
3675 && h
->type
== STT_NOTYPE
3676 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
3677 (*_bfd_error_handler
)
3678 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3679 h
->root
.root
.string
);
3681 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
3682 bed
= get_elf_backend_data (dynobj
);
3683 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3692 /* This routine is used to export all defined symbols into the dynamic
3693 symbol table. It is called via elf_link_hash_traverse. */
3696 elf_export_symbol (h
, data
)
3697 struct elf_link_hash_entry
*h
;
3700 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3702 /* Ignore indirect symbols. These are added by the versioning code. */
3703 if (h
->root
.type
== bfd_link_hash_indirect
)
3706 if (h
->dynindx
== -1
3707 && (h
->elf_link_hash_flags
3708 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
3710 struct bfd_elf_version_tree
*t
;
3711 struct bfd_elf_version_expr
*d
;
3713 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
3715 if (t
->globals
!= NULL
)
3717 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3719 if ((*d
->match
) (d
, h
->root
.root
.string
))
3724 if (t
->locals
!= NULL
)
3726 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3728 if ((*d
->match
) (d
, h
->root
.root
.string
))
3737 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3748 /* Look through the symbols which are defined in other shared
3749 libraries and referenced here. Update the list of version
3750 dependencies. This will be put into the .gnu.version_r section.
3751 This function is called via elf_link_hash_traverse. */
3754 elf_link_find_version_dependencies (h
, data
)
3755 struct elf_link_hash_entry
*h
;
3758 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
3759 Elf_Internal_Verneed
*t
;
3760 Elf_Internal_Vernaux
*a
;
3762 /* We only care about symbols defined in shared objects with version
3764 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3765 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
3767 || h
->verinfo
.verdef
== NULL
)
3770 /* See if we already know about this version. */
3771 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
3773 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
3776 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3777 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
3783 /* This is a new version. Add it to tree we are building. */
3787 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *t
);
3790 rinfo
->failed
= true;
3794 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
3795 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
3796 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
3799 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *a
);
3801 /* Note that we are copying a string pointer here, and testing it
3802 above. If bfd_elf_string_from_elf_section is ever changed to
3803 discard the string data when low in memory, this will have to be
3805 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
3807 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
3808 a
->vna_nextptr
= t
->vn_auxptr
;
3810 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
3813 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
3820 /* Figure out appropriate versions for all the symbols. We may not
3821 have the version number script until we have read all of the input
3822 files, so until that point we don't know which symbols should be
3823 local. This function is called via elf_link_hash_traverse. */
3826 elf_link_assign_sym_version (h
, data
)
3827 struct elf_link_hash_entry
*h
;
3830 struct elf_assign_sym_version_info
*sinfo
=
3831 (struct elf_assign_sym_version_info
*) data
;
3832 struct bfd_link_info
*info
= sinfo
->info
;
3833 struct elf_backend_data
*bed
;
3834 struct elf_info_failed eif
;
3837 /* Fix the symbol flags. */
3840 if (! elf_fix_symbol_flags (h
, &eif
))
3843 sinfo
->failed
= true;
3847 /* We only need version numbers for symbols defined in regular
3849 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
3852 bed
= get_elf_backend_data (sinfo
->output_bfd
);
3853 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3854 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
3856 struct bfd_elf_version_tree
*t
;
3861 /* There are two consecutive ELF_VER_CHR characters if this is
3862 not a hidden symbol. */
3864 if (*p
== ELF_VER_CHR
)
3870 /* If there is no version string, we can just return out. */
3874 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
3878 /* Look for the version. If we find it, it is no longer weak. */
3879 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
3881 if (strcmp (t
->name
, p
) == 0)
3885 struct bfd_elf_version_expr
*d
;
3887 len
= p
- h
->root
.root
.string
;
3888 alc
= bfd_alloc (sinfo
->output_bfd
, len
);
3891 strncpy (alc
, h
->root
.root
.string
, len
- 1);
3892 alc
[len
- 1] = '\0';
3893 if (alc
[len
- 2] == ELF_VER_CHR
)
3894 alc
[len
- 2] = '\0';
3896 h
->verinfo
.vertree
= t
;
3900 if (t
->globals
!= NULL
)
3902 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3903 if ((*d
->match
) (d
, alc
))
3907 /* See if there is anything to force this symbol to
3909 if (d
== NULL
&& t
->locals
!= NULL
)
3911 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3913 if ((*d
->match
) (d
, alc
))
3915 if (h
->dynindx
!= -1
3917 && ! info
->export_dynamic
)
3919 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3920 (*bed
->elf_backend_hide_symbol
) (info
, h
);
3921 /* FIXME: The name of the symbol has
3922 already been recorded in the dynamic
3923 string table section. */
3931 bfd_release (sinfo
->output_bfd
, alc
);
3936 /* If we are building an application, we need to create a
3937 version node for this version. */
3938 if (t
== NULL
&& ! info
->shared
)
3940 struct bfd_elf_version_tree
**pp
;
3943 /* If we aren't going to export this symbol, we don't need
3944 to worry about it. */
3945 if (h
->dynindx
== -1)
3948 t
= ((struct bfd_elf_version_tree
*)
3949 bfd_alloc (sinfo
->output_bfd
, sizeof *t
));
3952 sinfo
->failed
= true;
3961 t
->name_indx
= (unsigned int) -1;
3965 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
3967 t
->vernum
= version_index
;
3971 h
->verinfo
.vertree
= t
;
3975 /* We could not find the version for a symbol when
3976 generating a shared archive. Return an error. */
3977 (*_bfd_error_handler
)
3978 (_("%s: undefined versioned symbol name %s"),
3979 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
3980 bfd_set_error (bfd_error_bad_value
);
3981 sinfo
->failed
= true;
3986 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
3989 /* If we don't have a version for this symbol, see if we can find
3991 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
3993 struct bfd_elf_version_tree
*t
;
3994 struct bfd_elf_version_tree
*deflt
;
3995 struct bfd_elf_version_expr
*d
;
3997 /* See if can find what version this symbol is in. If the
3998 symbol is supposed to be local, then don't actually register
4001 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4003 if (t
->globals
!= NULL
)
4005 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4007 if ((*d
->match
) (d
, h
->root
.root
.string
))
4009 h
->verinfo
.vertree
= t
;
4018 if (t
->locals
!= NULL
)
4020 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4022 if (d
->pattern
[0] == '*' && d
->pattern
[1] == '\0')
4024 else if ((*d
->match
) (d
, h
->root
.root
.string
))
4026 h
->verinfo
.vertree
= t
;
4027 if (h
->dynindx
!= -1
4029 && ! info
->export_dynamic
)
4031 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
4032 (*bed
->elf_backend_hide_symbol
) (info
, h
);
4033 /* FIXME: The name of the symbol has already
4034 been recorded in the dynamic string table
4046 if (deflt
!= NULL
&& h
->verinfo
.vertree
== NULL
)
4048 h
->verinfo
.vertree
= deflt
;
4049 if (h
->dynindx
!= -1
4051 && ! info
->export_dynamic
)
4053 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
4054 (*bed
->elf_backend_hide_symbol
) (info
, h
);
4055 /* FIXME: The name of the symbol has already been
4056 recorded in the dynamic string table section. */
4064 /* Final phase of ELF linker. */
4066 /* A structure we use to avoid passing large numbers of arguments. */
4068 struct elf_final_link_info
4070 /* General link information. */
4071 struct bfd_link_info
*info
;
4074 /* Symbol string table. */
4075 struct bfd_strtab_hash
*symstrtab
;
4076 /* .dynsym section. */
4077 asection
*dynsym_sec
;
4078 /* .hash section. */
4080 /* symbol version section (.gnu.version). */
4081 asection
*symver_sec
;
4082 /* Buffer large enough to hold contents of any section. */
4084 /* Buffer large enough to hold external relocs of any section. */
4085 PTR external_relocs
;
4086 /* Buffer large enough to hold internal relocs of any section. */
4087 Elf_Internal_Rela
*internal_relocs
;
4088 /* Buffer large enough to hold external local symbols of any input
4090 Elf_External_Sym
*external_syms
;
4091 /* Buffer large enough to hold internal local symbols of any input
4093 Elf_Internal_Sym
*internal_syms
;
4094 /* Array large enough to hold a symbol index for each local symbol
4095 of any input BFD. */
4097 /* Array large enough to hold a section pointer for each local
4098 symbol of any input BFD. */
4099 asection
**sections
;
4100 /* Buffer to hold swapped out symbols. */
4101 Elf_External_Sym
*symbuf
;
4102 /* Number of swapped out symbols in buffer. */
4103 size_t symbuf_count
;
4104 /* Number of symbols which fit in symbuf. */
4108 static boolean elf_link_output_sym
4109 PARAMS ((struct elf_final_link_info
*, const char *,
4110 Elf_Internal_Sym
*, asection
*));
4111 static boolean elf_link_flush_output_syms
4112 PARAMS ((struct elf_final_link_info
*));
4113 static boolean elf_link_output_extsym
4114 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4115 static boolean elf_link_sec_merge_syms
4116 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4117 static boolean elf_link_input_bfd
4118 PARAMS ((struct elf_final_link_info
*, bfd
*));
4119 static boolean elf_reloc_link_order
4120 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
4121 struct bfd_link_order
*));
4123 /* This struct is used to pass information to elf_link_output_extsym. */
4125 struct elf_outext_info
4129 struct elf_final_link_info
*finfo
;
4132 /* Compute the size of, and allocate space for, REL_HDR which is the
4133 section header for a section containing relocations for O. */
4136 elf_link_size_reloc_section (abfd
, rel_hdr
, o
)
4138 Elf_Internal_Shdr
*rel_hdr
;
4141 unsigned reloc_count
;
4142 unsigned num_rel_hashes
;
4144 /* Figure out how many relocations there will be. */
4145 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
4146 reloc_count
= elf_section_data (o
)->rel_count
;
4148 reloc_count
= elf_section_data (o
)->rel_count2
;
4150 num_rel_hashes
= o
->reloc_count
;
4151 if (num_rel_hashes
< reloc_count
)
4152 num_rel_hashes
= reloc_count
;
4154 /* That allows us to calculate the size of the section. */
4155 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
4157 /* The contents field must last into write_object_contents, so we
4158 allocate it with bfd_alloc rather than malloc. Also since we
4159 cannot be sure that the contents will actually be filled in,
4160 we zero the allocated space. */
4161 rel_hdr
->contents
= (PTR
) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
4162 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
4165 /* We only allocate one set of hash entries, so we only do it the
4166 first time we are called. */
4167 if (elf_section_data (o
)->rel_hashes
== NULL
4170 struct elf_link_hash_entry
**p
;
4172 p
= ((struct elf_link_hash_entry
**)
4173 bfd_zmalloc (num_rel_hashes
4174 * sizeof (struct elf_link_hash_entry
*)));
4178 elf_section_data (o
)->rel_hashes
= p
;
4184 /* When performing a relocateable link, the input relocations are
4185 preserved. But, if they reference global symbols, the indices
4186 referenced must be updated. Update all the relocations in
4187 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4190 elf_link_adjust_relocs (abfd
, rel_hdr
, count
, rel_hash
)
4192 Elf_Internal_Shdr
*rel_hdr
;
4194 struct elf_link_hash_entry
**rel_hash
;
4197 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4198 Elf_Internal_Rel
*irel
;
4199 Elf_Internal_Rela
*irela
;
4201 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (sizeof (Elf_Internal_Rel
)
4202 * bed
->s
->int_rels_per_ext_rel
);
4205 (*_bfd_error_handler
) (_("Error: out of memory"));
4209 irela
= (Elf_Internal_Rela
*) bfd_zmalloc (sizeof (Elf_Internal_Rela
)
4210 * bed
->s
->int_rels_per_ext_rel
);
4213 (*_bfd_error_handler
) (_("Error: out of memory"));
4217 for (i
= 0; i
< count
; i
++, rel_hash
++)
4219 if (*rel_hash
== NULL
)
4222 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
4224 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4226 Elf_External_Rel
*erel
;
4229 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
4230 if (bed
->s
->swap_reloc_in
)
4231 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
4233 elf_swap_reloc_in (abfd
, erel
, irel
);
4235 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4236 irel
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4237 ELF_R_TYPE (irel
[j
].r_info
));
4239 if (bed
->s
->swap_reloc_out
)
4240 (*bed
->s
->swap_reloc_out
) (abfd
, irel
, (bfd_byte
*) erel
);
4242 elf_swap_reloc_out (abfd
, irel
, erel
);
4246 Elf_External_Rela
*erela
;
4249 BFD_ASSERT (rel_hdr
->sh_entsize
4250 == sizeof (Elf_External_Rela
));
4252 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
4253 if (bed
->s
->swap_reloca_in
)
4254 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
4256 elf_swap_reloca_in (abfd
, erela
, irela
);
4258 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4259 irela
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4260 ELF_R_TYPE (irela
[j
].r_info
));
4262 if (bed
->s
->swap_reloca_out
)
4263 (*bed
->s
->swap_reloca_out
) (abfd
, irela
, (bfd_byte
*) erela
);
4265 elf_swap_reloca_out (abfd
, irela
, erela
);
4273 /* Do the final step of an ELF link. */
4276 elf_bfd_final_link (abfd
, info
)
4278 struct bfd_link_info
*info
;
4281 boolean emit_relocs
;
4283 struct elf_final_link_info finfo
;
4284 register asection
*o
;
4285 register struct bfd_link_order
*p
;
4287 size_t max_contents_size
;
4288 size_t max_external_reloc_size
;
4289 size_t max_internal_reloc_count
;
4290 size_t max_sym_count
;
4292 Elf_Internal_Sym elfsym
;
4294 Elf_Internal_Shdr
*symtab_hdr
;
4295 Elf_Internal_Shdr
*symstrtab_hdr
;
4296 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4297 struct elf_outext_info eoinfo
;
4301 abfd
->flags
|= DYNAMIC
;
4303 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
4304 dynobj
= elf_hash_table (info
)->dynobj
;
4306 emit_relocs
= (info
->relocateable
4307 || info
->emitrelocations
4308 || bed
->elf_backend_emit_relocs
);
4311 finfo
.output_bfd
= abfd
;
4312 finfo
.symstrtab
= elf_stringtab_init ();
4313 if (finfo
.symstrtab
== NULL
)
4318 finfo
.dynsym_sec
= NULL
;
4319 finfo
.hash_sec
= NULL
;
4320 finfo
.symver_sec
= NULL
;
4324 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
4325 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
4326 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
4327 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
4328 /* Note that it is OK if symver_sec is NULL. */
4331 finfo
.contents
= NULL
;
4332 finfo
.external_relocs
= NULL
;
4333 finfo
.internal_relocs
= NULL
;
4334 finfo
.external_syms
= NULL
;
4335 finfo
.internal_syms
= NULL
;
4336 finfo
.indices
= NULL
;
4337 finfo
.sections
= NULL
;
4338 finfo
.symbuf
= NULL
;
4339 finfo
.symbuf_count
= 0;
4341 /* Count up the number of relocations we will output for each output
4342 section, so that we know the sizes of the reloc sections. We
4343 also figure out some maximum sizes. */
4344 max_contents_size
= 0;
4345 max_external_reloc_size
= 0;
4346 max_internal_reloc_count
= 0;
4349 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
4353 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
4355 if (p
->type
== bfd_section_reloc_link_order
4356 || p
->type
== bfd_symbol_reloc_link_order
)
4358 else if (p
->type
== bfd_indirect_link_order
)
4362 sec
= p
->u
.indirect
.section
;
4364 /* Mark all sections which are to be included in the
4365 link. This will normally be every section. We need
4366 to do this so that we can identify any sections which
4367 the linker has decided to not include. */
4368 sec
->linker_mark
= true;
4370 if (sec
->flags
& SEC_MERGE
)
4373 if (info
->relocateable
|| info
->emitrelocations
)
4374 o
->reloc_count
+= sec
->reloc_count
;
4375 else if (bed
->elf_backend_count_relocs
)
4377 Elf_Internal_Rela
* relocs
;
4379 relocs
= (NAME(_bfd_elf
,link_read_relocs
)
4380 (abfd
, sec
, (PTR
) NULL
,
4381 (Elf_Internal_Rela
*) NULL
, info
->keep_memory
));
4383 o
->reloc_count
+= (*bed
->elf_backend_count_relocs
)
4386 if (!info
->keep_memory
)
4390 if (sec
->_raw_size
> max_contents_size
)
4391 max_contents_size
= sec
->_raw_size
;
4392 if (sec
->_cooked_size
> max_contents_size
)
4393 max_contents_size
= sec
->_cooked_size
;
4395 /* We are interested in just local symbols, not all
4397 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
4398 && (sec
->owner
->flags
& DYNAMIC
) == 0)
4402 if (elf_bad_symtab (sec
->owner
))
4403 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
4404 / sizeof (Elf_External_Sym
));
4406 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
4408 if (sym_count
> max_sym_count
)
4409 max_sym_count
= sym_count
;
4411 if ((sec
->flags
& SEC_RELOC
) != 0)
4415 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
4416 if (ext_size
> max_external_reloc_size
)
4417 max_external_reloc_size
= ext_size
;
4418 if (sec
->reloc_count
> max_internal_reloc_count
)
4419 max_internal_reloc_count
= sec
->reloc_count
;
4425 if (o
->reloc_count
> 0)
4426 o
->flags
|= SEC_RELOC
;
4429 /* Explicitly clear the SEC_RELOC flag. The linker tends to
4430 set it (this is probably a bug) and if it is set
4431 assign_section_numbers will create a reloc section. */
4432 o
->flags
&=~ SEC_RELOC
;
4435 /* If the SEC_ALLOC flag is not set, force the section VMA to
4436 zero. This is done in elf_fake_sections as well, but forcing
4437 the VMA to 0 here will ensure that relocs against these
4438 sections are handled correctly. */
4439 if ((o
->flags
& SEC_ALLOC
) == 0
4440 && ! o
->user_set_vma
)
4444 if (! info
->relocateable
&& merged
)
4445 elf_link_hash_traverse (elf_hash_table (info
),
4446 elf_link_sec_merge_syms
, (PTR
) abfd
);
4448 /* Figure out the file positions for everything but the symbol table
4449 and the relocs. We set symcount to force assign_section_numbers
4450 to create a symbol table. */
4451 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
4452 BFD_ASSERT (! abfd
->output_has_begun
);
4453 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
4456 /* Figure out how many relocations we will have in each section.
4457 Just using RELOC_COUNT isn't good enough since that doesn't
4458 maintain a separate value for REL vs. RELA relocations. */
4460 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
4461 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
4463 asection
*output_section
;
4465 if (! o
->linker_mark
)
4467 /* This section was omitted from the link. */
4471 output_section
= o
->output_section
;
4473 if (output_section
!= NULL
4474 && (o
->flags
& SEC_RELOC
) != 0)
4476 struct bfd_elf_section_data
*esdi
4477 = elf_section_data (o
);
4478 struct bfd_elf_section_data
*esdo
4479 = elf_section_data (output_section
);
4480 unsigned int *rel_count
;
4481 unsigned int *rel_count2
;
4483 /* We must be careful to add the relocation froms the
4484 input section to the right output count. */
4485 if (esdi
->rel_hdr
.sh_entsize
== esdo
->rel_hdr
.sh_entsize
)
4487 rel_count
= &esdo
->rel_count
;
4488 rel_count2
= &esdo
->rel_count2
;
4492 rel_count
= &esdo
->rel_count2
;
4493 rel_count2
= &esdo
->rel_count
;
4496 *rel_count
+= NUM_SHDR_ENTRIES (& esdi
->rel_hdr
);
4498 *rel_count2
+= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
4499 output_section
->flags
|= SEC_RELOC
;
4503 /* That created the reloc sections. Set their sizes, and assign
4504 them file positions, and allocate some buffers. */
4505 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4507 if ((o
->flags
& SEC_RELOC
) != 0)
4509 if (!elf_link_size_reloc_section (abfd
,
4510 &elf_section_data (o
)->rel_hdr
,
4514 if (elf_section_data (o
)->rel_hdr2
4515 && !elf_link_size_reloc_section (abfd
,
4516 elf_section_data (o
)->rel_hdr2
,
4521 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
4522 to count upwards while actually outputting the relocations. */
4523 elf_section_data (o
)->rel_count
= 0;
4524 elf_section_data (o
)->rel_count2
= 0;
4527 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4529 /* We have now assigned file positions for all the sections except
4530 .symtab and .strtab. We start the .symtab section at the current
4531 file position, and write directly to it. We build the .strtab
4532 section in memory. */
4533 bfd_get_symcount (abfd
) = 0;
4534 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4535 /* sh_name is set in prep_headers. */
4536 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4537 symtab_hdr
->sh_flags
= 0;
4538 symtab_hdr
->sh_addr
= 0;
4539 symtab_hdr
->sh_size
= 0;
4540 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
4541 /* sh_link is set in assign_section_numbers. */
4542 /* sh_info is set below. */
4543 /* sh_offset is set just below. */
4544 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
4546 off
= elf_tdata (abfd
)->next_file_pos
;
4547 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
4549 /* Note that at this point elf_tdata (abfd)->next_file_pos is
4550 incorrect. We do not yet know the size of the .symtab section.
4551 We correct next_file_pos below, after we do know the size. */
4553 /* Allocate a buffer to hold swapped out symbols. This is to avoid
4554 continuously seeking to the right position in the file. */
4555 if (! info
->keep_memory
|| max_sym_count
< 20)
4556 finfo
.symbuf_size
= 20;
4558 finfo
.symbuf_size
= max_sym_count
;
4559 finfo
.symbuf
= ((Elf_External_Sym
*)
4560 bfd_malloc (finfo
.symbuf_size
* sizeof (Elf_External_Sym
)));
4561 if (finfo
.symbuf
== NULL
)
4564 /* Start writing out the symbol table. The first symbol is always a
4566 if (info
->strip
!= strip_all
4569 elfsym
.st_value
= 0;
4572 elfsym
.st_other
= 0;
4573 elfsym
.st_shndx
= SHN_UNDEF
;
4574 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
4575 &elfsym
, bfd_und_section_ptr
))
4580 /* Some standard ELF linkers do this, but we don't because it causes
4581 bootstrap comparison failures. */
4582 /* Output a file symbol for the output file as the second symbol.
4583 We output this even if we are discarding local symbols, although
4584 I'm not sure if this is correct. */
4585 elfsym
.st_value
= 0;
4587 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4588 elfsym
.st_other
= 0;
4589 elfsym
.st_shndx
= SHN_ABS
;
4590 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
4591 &elfsym
, bfd_abs_section_ptr
))
4595 /* Output a symbol for each section. We output these even if we are
4596 discarding local symbols, since they are used for relocs. These
4597 symbols have no names. We store the index of each one in the
4598 index field of the section, so that we can find it again when
4599 outputting relocs. */
4600 if (info
->strip
!= strip_all
4604 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4605 elfsym
.st_other
= 0;
4606 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
4608 o
= section_from_elf_index (abfd
, i
);
4610 o
->target_index
= bfd_get_symcount (abfd
);
4611 elfsym
.st_shndx
= i
;
4612 if (info
->relocateable
|| o
== NULL
)
4613 elfsym
.st_value
= 0;
4615 elfsym
.st_value
= o
->vma
;
4616 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
4622 /* Allocate some memory to hold information read in from the input
4624 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
4625 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
4626 finfo
.internal_relocs
= ((Elf_Internal_Rela
*)
4627 bfd_malloc (max_internal_reloc_count
4628 * sizeof (Elf_Internal_Rela
)
4629 * bed
->s
->int_rels_per_ext_rel
));
4630 finfo
.external_syms
= ((Elf_External_Sym
*)
4631 bfd_malloc (max_sym_count
4632 * sizeof (Elf_External_Sym
)));
4633 finfo
.internal_syms
= ((Elf_Internal_Sym
*)
4634 bfd_malloc (max_sym_count
4635 * sizeof (Elf_Internal_Sym
)));
4636 finfo
.indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
4637 finfo
.sections
= ((asection
**)
4638 bfd_malloc (max_sym_count
* sizeof (asection
*)));
4639 if ((finfo
.contents
== NULL
&& max_contents_size
!= 0)
4640 || (finfo
.external_relocs
== NULL
&& max_external_reloc_size
!= 0)
4641 || (finfo
.internal_relocs
== NULL
&& max_internal_reloc_count
!= 0)
4642 || (finfo
.external_syms
== NULL
&& max_sym_count
!= 0)
4643 || (finfo
.internal_syms
== NULL
&& max_sym_count
!= 0)
4644 || (finfo
.indices
== NULL
&& max_sym_count
!= 0)
4645 || (finfo
.sections
== NULL
&& max_sym_count
!= 0))
4648 /* Since ELF permits relocations to be against local symbols, we
4649 must have the local symbols available when we do the relocations.
4650 Since we would rather only read the local symbols once, and we
4651 would rather not keep them in memory, we handle all the
4652 relocations for a single input file at the same time.
4654 Unfortunately, there is no way to know the total number of local
4655 symbols until we have seen all of them, and the local symbol
4656 indices precede the global symbol indices. This means that when
4657 we are generating relocateable output, and we see a reloc against
4658 a global symbol, we can not know the symbol index until we have
4659 finished examining all the local symbols to see which ones we are
4660 going to output. To deal with this, we keep the relocations in
4661 memory, and don't output them until the end of the link. This is
4662 an unfortunate waste of memory, but I don't see a good way around
4663 it. Fortunately, it only happens when performing a relocateable
4664 link, which is not the common case. FIXME: If keep_memory is set
4665 we could write the relocs out and then read them again; I don't
4666 know how bad the memory loss will be. */
4668 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
4669 sub
->output_has_begun
= false;
4670 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4672 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
4674 if (p
->type
== bfd_indirect_link_order
4675 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
4676 == bfd_target_elf_flavour
))
4678 sub
= p
->u
.indirect
.section
->owner
;
4679 if (! sub
->output_has_begun
)
4681 if (! elf_link_input_bfd (&finfo
, sub
))
4683 sub
->output_has_begun
= true;
4686 else if (p
->type
== bfd_section_reloc_link_order
4687 || p
->type
== bfd_symbol_reloc_link_order
)
4689 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
4694 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
4700 /* That wrote out all the local symbols. Finish up the symbol table
4701 with the global symbols. Even if we want to strip everything we
4702 can, we still need to deal with those global symbols that got
4703 converted to local in a version script. */
4707 /* Output any global symbols that got converted to local in a
4708 version script. We do this in a separate step since ELF
4709 requires all local symbols to appear prior to any global
4710 symbols. FIXME: We should only do this if some global
4711 symbols were, in fact, converted to become local. FIXME:
4712 Will this work correctly with the Irix 5 linker? */
4713 eoinfo
.failed
= false;
4714 eoinfo
.finfo
= &finfo
;
4715 eoinfo
.localsyms
= true;
4716 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
4722 /* The sh_info field records the index of the first non local symbol. */
4723 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
4726 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
4728 Elf_Internal_Sym sym
;
4729 Elf_External_Sym
*dynsym
=
4730 (Elf_External_Sym
*) finfo
.dynsym_sec
->contents
;
4731 long last_local
= 0;
4733 /* Write out the section symbols for the output sections. */
4740 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4743 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4746 indx
= elf_section_data (s
)->this_idx
;
4747 BFD_ASSERT (indx
> 0);
4748 sym
.st_shndx
= indx
;
4749 sym
.st_value
= s
->vma
;
4751 elf_swap_symbol_out (abfd
, &sym
,
4752 dynsym
+ elf_section_data (s
)->dynindx
);
4755 last_local
= bfd_count_sections (abfd
);
4758 /* Write out the local dynsyms. */
4759 if (elf_hash_table (info
)->dynlocal
)
4761 struct elf_link_local_dynamic_entry
*e
;
4762 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
4766 sym
.st_size
= e
->isym
.st_size
;
4767 sym
.st_other
= e
->isym
.st_other
;
4769 /* Copy the internal symbol as is.
4770 Note that we saved a word of storage and overwrote
4771 the original st_name with the dynstr_index. */
4774 if (e
->isym
.st_shndx
> 0 && e
->isym
.st_shndx
< SHN_LORESERVE
)
4776 s
= bfd_section_from_elf_index (e
->input_bfd
,
4780 elf_section_data (s
->output_section
)->this_idx
;
4781 sym
.st_value
= (s
->output_section
->vma
4783 + e
->isym
.st_value
);
4786 if (last_local
< e
->dynindx
)
4787 last_local
= e
->dynindx
;
4789 elf_swap_symbol_out (abfd
, &sym
, dynsym
+ e
->dynindx
);
4793 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
4797 /* We get the global symbols from the hash table. */
4798 eoinfo
.failed
= false;
4799 eoinfo
.localsyms
= false;
4800 eoinfo
.finfo
= &finfo
;
4801 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
4806 /* If backend needs to output some symbols not present in the hash
4807 table, do it now. */
4808 if (bed
->elf_backend_output_arch_syms
)
4810 if (! (*bed
->elf_backend_output_arch_syms
)
4811 (abfd
, info
, (PTR
) &finfo
,
4812 (boolean (*) PARAMS ((PTR
, const char *,
4813 Elf_Internal_Sym
*, asection
*)))
4814 elf_link_output_sym
))
4818 /* Flush all symbols to the file. */
4819 if (! elf_link_flush_output_syms (&finfo
))
4822 /* Now we know the size of the symtab section. */
4823 off
+= symtab_hdr
->sh_size
;
4825 /* Finish up and write out the symbol string table (.strtab)
4827 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4828 /* sh_name was set in prep_headers. */
4829 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4830 symstrtab_hdr
->sh_flags
= 0;
4831 symstrtab_hdr
->sh_addr
= 0;
4832 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
4833 symstrtab_hdr
->sh_entsize
= 0;
4834 symstrtab_hdr
->sh_link
= 0;
4835 symstrtab_hdr
->sh_info
= 0;
4836 /* sh_offset is set just below. */
4837 symstrtab_hdr
->sh_addralign
= 1;
4839 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, true);
4840 elf_tdata (abfd
)->next_file_pos
= off
;
4842 if (bfd_get_symcount (abfd
) > 0)
4844 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
4845 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
4849 /* Adjust the relocs to have the correct symbol indices. */
4850 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4852 if ((o
->flags
& SEC_RELOC
) == 0)
4855 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
4856 elf_section_data (o
)->rel_count
,
4857 elf_section_data (o
)->rel_hashes
);
4858 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
4859 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
4860 elf_section_data (o
)->rel_count2
,
4861 (elf_section_data (o
)->rel_hashes
4862 + elf_section_data (o
)->rel_count
));
4864 /* Set the reloc_count field to 0 to prevent write_relocs from
4865 trying to swap the relocs out itself. */
4869 /* If we are linking against a dynamic object, or generating a
4870 shared library, finish up the dynamic linking information. */
4873 Elf_External_Dyn
*dyncon
, *dynconend
;
4875 /* Fix up .dynamic entries. */
4876 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
4877 BFD_ASSERT (o
!= NULL
);
4879 dyncon
= (Elf_External_Dyn
*) o
->contents
;
4880 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
4881 for (; dyncon
< dynconend
; dyncon
++)
4883 Elf_Internal_Dyn dyn
;
4887 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
4894 name
= info
->init_function
;
4897 name
= info
->fini_function
;
4900 struct elf_link_hash_entry
*h
;
4902 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
4903 false, false, true);
4905 && (h
->root
.type
== bfd_link_hash_defined
4906 || h
->root
.type
== bfd_link_hash_defweak
))
4908 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
4909 o
= h
->root
.u
.def
.section
;
4910 if (o
->output_section
!= NULL
)
4911 dyn
.d_un
.d_val
+= (o
->output_section
->vma
4912 + o
->output_offset
);
4915 /* The symbol is imported from another shared
4916 library and does not apply to this one. */
4920 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
4935 name
= ".gnu.version_d";
4938 name
= ".gnu.version_r";
4941 name
= ".gnu.version";
4943 o
= bfd_get_section_by_name (abfd
, name
);
4944 BFD_ASSERT (o
!= NULL
);
4945 dyn
.d_un
.d_ptr
= o
->vma
;
4946 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
4953 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
4958 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
4960 Elf_Internal_Shdr
*hdr
;
4962 hdr
= elf_elfsections (abfd
)[i
];
4963 if (hdr
->sh_type
== type
4964 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
4966 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
4967 dyn
.d_un
.d_val
+= hdr
->sh_size
;
4970 if (dyn
.d_un
.d_val
== 0
4971 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
4972 dyn
.d_un
.d_val
= hdr
->sh_addr
;
4976 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
4982 /* If we have created any dynamic sections, then output them. */
4985 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
4988 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4990 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4991 || o
->_raw_size
== 0
4992 || o
->output_section
== bfd_abs_section_ptr
)
4994 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
4996 /* At this point, we are only interested in sections
4997 created by elf_link_create_dynamic_sections. */
5000 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
5002 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
5004 if (! bfd_set_section_contents (abfd
, o
->output_section
,
5005 o
->contents
, o
->output_offset
,
5013 /* The contents of the .dynstr section are actually in a
5015 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
5016 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
5017 || ! _bfd_stringtab_emit (abfd
,
5018 elf_hash_table (info
)->dynstr
))
5024 /* If we have optimized stabs strings, output them. */
5025 if (elf_hash_table (info
)->stab_info
!= NULL
)
5027 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
5031 if (finfo
.symstrtab
!= NULL
)
5032 _bfd_stringtab_free (finfo
.symstrtab
);
5033 if (finfo
.contents
!= NULL
)
5034 free (finfo
.contents
);
5035 if (finfo
.external_relocs
!= NULL
)
5036 free (finfo
.external_relocs
);
5037 if (finfo
.internal_relocs
!= NULL
)
5038 free (finfo
.internal_relocs
);
5039 if (finfo
.external_syms
!= NULL
)
5040 free (finfo
.external_syms
);
5041 if (finfo
.internal_syms
!= NULL
)
5042 free (finfo
.internal_syms
);
5043 if (finfo
.indices
!= NULL
)
5044 free (finfo
.indices
);
5045 if (finfo
.sections
!= NULL
)
5046 free (finfo
.sections
);
5047 if (finfo
.symbuf
!= NULL
)
5048 free (finfo
.symbuf
);
5049 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5051 if ((o
->flags
& SEC_RELOC
) != 0
5052 && elf_section_data (o
)->rel_hashes
!= NULL
)
5053 free (elf_section_data (o
)->rel_hashes
);
5056 elf_tdata (abfd
)->linker
= true;
5061 if (finfo
.symstrtab
!= NULL
)
5062 _bfd_stringtab_free (finfo
.symstrtab
);
5063 if (finfo
.contents
!= NULL
)
5064 free (finfo
.contents
);
5065 if (finfo
.external_relocs
!= NULL
)
5066 free (finfo
.external_relocs
);
5067 if (finfo
.internal_relocs
!= NULL
)
5068 free (finfo
.internal_relocs
);
5069 if (finfo
.external_syms
!= NULL
)
5070 free (finfo
.external_syms
);
5071 if (finfo
.internal_syms
!= NULL
)
5072 free (finfo
.internal_syms
);
5073 if (finfo
.indices
!= NULL
)
5074 free (finfo
.indices
);
5075 if (finfo
.sections
!= NULL
)
5076 free (finfo
.sections
);
5077 if (finfo
.symbuf
!= NULL
)
5078 free (finfo
.symbuf
);
5079 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5081 if ((o
->flags
& SEC_RELOC
) != 0
5082 && elf_section_data (o
)->rel_hashes
!= NULL
)
5083 free (elf_section_data (o
)->rel_hashes
);
5089 /* Add a symbol to the output symbol table. */
5092 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
5093 struct elf_final_link_info
*finfo
;
5095 Elf_Internal_Sym
*elfsym
;
5096 asection
*input_sec
;
5098 boolean (*output_symbol_hook
) PARAMS ((bfd
*,
5099 struct bfd_link_info
*info
,
5104 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
5105 elf_backend_link_output_symbol_hook
;
5106 if (output_symbol_hook
!= NULL
)
5108 if (! ((*output_symbol_hook
)
5109 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
5113 if (name
== (const char *) NULL
|| *name
== '\0')
5114 elfsym
->st_name
= 0;
5115 else if (input_sec
->flags
& SEC_EXCLUDE
)
5116 elfsym
->st_name
= 0;
5119 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5121 if (elfsym
->st_name
== (unsigned long) -1)
5125 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5127 if (! elf_link_flush_output_syms (finfo
))
5131 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
,
5132 (PTR
) (finfo
->symbuf
+ finfo
->symbuf_count
));
5133 ++finfo
->symbuf_count
;
5135 ++ bfd_get_symcount (finfo
->output_bfd
);
5140 /* Flush the output symbols to the file. */
5143 elf_link_flush_output_syms (finfo
)
5144 struct elf_final_link_info
*finfo
;
5146 if (finfo
->symbuf_count
> 0)
5148 Elf_Internal_Shdr
*symtab
;
5150 symtab
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5152 if (bfd_seek (finfo
->output_bfd
, symtab
->sh_offset
+ symtab
->sh_size
,
5154 || (bfd_write ((PTR
) finfo
->symbuf
, finfo
->symbuf_count
,
5155 sizeof (Elf_External_Sym
), finfo
->output_bfd
)
5156 != finfo
->symbuf_count
* sizeof (Elf_External_Sym
)))
5159 symtab
->sh_size
+= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
5161 finfo
->symbuf_count
= 0;
5167 /* Adjust all external symbols pointing into SEC_MERGE sections
5168 to reflect the object merging within the sections. */
5171 elf_link_sec_merge_syms (h
, data
)
5172 struct elf_link_hash_entry
*h
;
5177 if ((h
->root
.type
== bfd_link_hash_defined
5178 || h
->root
.type
== bfd_link_hash_defweak
)
5179 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
5180 && elf_section_data (sec
)->merge_info
)
5182 bfd
*output_bfd
= (bfd
*) data
;
5184 h
->root
.u
.def
.value
=
5185 _bfd_merged_section_offset (output_bfd
,
5186 &h
->root
.u
.def
.section
,
5187 elf_section_data (sec
)->merge_info
,
5188 h
->root
.u
.def
.value
, (bfd_vma
) 0);
5194 /* Add an external symbol to the symbol table. This is called from
5195 the hash table traversal routine. When generating a shared object,
5196 we go through the symbol table twice. The first time we output
5197 anything that might have been forced to local scope in a version
5198 script. The second time we output the symbols that are still
5202 elf_link_output_extsym (h
, data
)
5203 struct elf_link_hash_entry
*h
;
5206 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
5207 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
5209 Elf_Internal_Sym sym
;
5210 asection
*input_sec
;
5212 /* Decide whether to output this symbol in this pass. */
5213 if (eoinfo
->localsyms
)
5215 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
5220 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
5224 /* If we are not creating a shared library, and this symbol is
5225 referenced by a shared library but is not defined anywhere, then
5226 warn that it is undefined. If we do not do this, the runtime
5227 linker will complain that the symbol is undefined when the
5228 program is run. We don't have to worry about symbols that are
5229 referenced by regular files, because we will already have issued
5230 warnings for them. */
5231 if (! finfo
->info
->relocateable
5232 && ! finfo
->info
->allow_shlib_undefined
5233 && ! finfo
->info
->shared
5234 && h
->root
.type
== bfd_link_hash_undefined
5235 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
5236 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
5238 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
5239 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
5240 (asection
*) NULL
, 0, true)))
5242 eoinfo
->failed
= true;
5247 /* We don't want to output symbols that have never been mentioned by
5248 a regular file, or that we have been told to strip. However, if
5249 h->indx is set to -2, the symbol is used by a reloc and we must
5253 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
5254 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
5255 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
5256 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
5258 else if (finfo
->info
->strip
== strip_all
5259 || (finfo
->info
->strip
== strip_some
5260 && bfd_hash_lookup (finfo
->info
->keep_hash
,
5261 h
->root
.root
.string
,
5262 false, false) == NULL
))
5267 /* If we're stripping it, and it's not a dynamic symbol, there's
5268 nothing else to do unless it is a forced local symbol. */
5271 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
5275 sym
.st_size
= h
->size
;
5276 sym
.st_other
= h
->other
;
5277 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
5278 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
5279 else if (h
->root
.type
== bfd_link_hash_undefweak
5280 || h
->root
.type
== bfd_link_hash_defweak
)
5281 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
5283 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
5285 switch (h
->root
.type
)
5288 case bfd_link_hash_new
:
5292 case bfd_link_hash_undefined
:
5293 input_sec
= bfd_und_section_ptr
;
5294 sym
.st_shndx
= SHN_UNDEF
;
5297 case bfd_link_hash_undefweak
:
5298 input_sec
= bfd_und_section_ptr
;
5299 sym
.st_shndx
= SHN_UNDEF
;
5302 case bfd_link_hash_defined
:
5303 case bfd_link_hash_defweak
:
5305 input_sec
= h
->root
.u
.def
.section
;
5306 if (input_sec
->output_section
!= NULL
)
5309 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
5310 input_sec
->output_section
);
5311 if (sym
.st_shndx
== (unsigned short) -1)
5313 (*_bfd_error_handler
)
5314 (_("%s: could not find output section %s for input section %s"),
5315 bfd_get_filename (finfo
->output_bfd
),
5316 input_sec
->output_section
->name
,
5318 eoinfo
->failed
= true;
5322 /* ELF symbols in relocateable files are section relative,
5323 but in nonrelocateable files they are virtual
5325 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
5326 if (! finfo
->info
->relocateable
)
5327 sym
.st_value
+= input_sec
->output_section
->vma
;
5331 BFD_ASSERT (input_sec
->owner
== NULL
5332 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
5333 sym
.st_shndx
= SHN_UNDEF
;
5334 input_sec
= bfd_und_section_ptr
;
5339 case bfd_link_hash_common
:
5340 input_sec
= h
->root
.u
.c
.p
->section
;
5341 sym
.st_shndx
= SHN_COMMON
;
5342 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
5345 case bfd_link_hash_indirect
:
5346 /* These symbols are created by symbol versioning. They point
5347 to the decorated version of the name. For example, if the
5348 symbol foo@@GNU_1.2 is the default, which should be used when
5349 foo is used with no version, then we add an indirect symbol
5350 foo which points to foo@@GNU_1.2. We ignore these symbols,
5351 since the indirected symbol is already in the hash table. */
5354 case bfd_link_hash_warning
:
5355 /* We can't represent these symbols in ELF, although a warning
5356 symbol may have come from a .gnu.warning.SYMBOL section. We
5357 just put the target symbol in the hash table. If the target
5358 symbol does not really exist, don't do anything. */
5359 if (h
->root
.u
.i
.link
->type
== bfd_link_hash_new
)
5361 return (elf_link_output_extsym
5362 ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
));
5365 /* Give the processor backend a chance to tweak the symbol value,
5366 and also to finish up anything that needs to be done for this
5368 if ((h
->dynindx
!= -1
5369 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
5370 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
5372 struct elf_backend_data
*bed
;
5374 bed
= get_elf_backend_data (finfo
->output_bfd
);
5375 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
5376 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
5378 eoinfo
->failed
= true;
5383 /* If we are marking the symbol as undefined, and there are no
5384 non-weak references to this symbol from a regular object, then
5385 mark the symbol as weak undefined; if there are non-weak
5386 references, mark the symbol as strong. We can't do this earlier,
5387 because it might not be marked as undefined until the
5388 finish_dynamic_symbol routine gets through with it. */
5389 if (sym
.st_shndx
== SHN_UNDEF
5390 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
5391 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
5392 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
5396 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
5397 bindtype
= STB_GLOBAL
;
5399 bindtype
= STB_WEAK
;
5400 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
5403 /* If a symbol is not defined locally, we clear the visibility
5405 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
5406 sym
.st_other
^= ELF_ST_VISIBILITY (sym
.st_other
);
5408 /* If this symbol should be put in the .dynsym section, then put it
5409 there now. We have already know the symbol index. We also fill
5410 in the entry in the .hash section. */
5411 if (h
->dynindx
!= -1
5412 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
5416 size_t hash_entry_size
;
5417 bfd_byte
*bucketpos
;
5420 sym
.st_name
= h
->dynstr_index
;
5422 elf_swap_symbol_out (finfo
->output_bfd
, &sym
,
5423 (PTR
) (((Elf_External_Sym
*)
5424 finfo
->dynsym_sec
->contents
)
5427 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
5428 bucket
= h
->elf_hash_value
% bucketcount
;
5430 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
5431 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
5432 + (bucket
+ 2) * hash_entry_size
);
5433 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
5434 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
5435 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
5436 ((bfd_byte
*) finfo
->hash_sec
->contents
5437 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
5439 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
5441 Elf_Internal_Versym iversym
;
5443 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
5445 if (h
->verinfo
.verdef
== NULL
)
5446 iversym
.vs_vers
= 0;
5448 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
5452 if (h
->verinfo
.vertree
== NULL
)
5453 iversym
.vs_vers
= 1;
5455 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
5458 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
5459 iversym
.vs_vers
|= VERSYM_HIDDEN
;
5461 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
,
5462 (((Elf_External_Versym
*)
5463 finfo
->symver_sec
->contents
)
5468 /* If we're stripping it, then it was just a dynamic symbol, and
5469 there's nothing else to do. */
5473 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
5475 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
5477 eoinfo
->failed
= true;
5484 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
5485 originated from the section given by INPUT_REL_HDR) to the
5489 elf_link_output_relocs (output_bfd
, input_section
, input_rel_hdr
,
5492 asection
*input_section
;
5493 Elf_Internal_Shdr
*input_rel_hdr
;
5494 Elf_Internal_Rela
*internal_relocs
;
5496 Elf_Internal_Rela
*irela
;
5497 Elf_Internal_Rela
*irelaend
;
5498 Elf_Internal_Shdr
*output_rel_hdr
;
5499 asection
*output_section
;
5500 unsigned int *rel_countp
= NULL
;
5501 struct elf_backend_data
*bed
;
5503 output_section
= input_section
->output_section
;
5504 output_rel_hdr
= NULL
;
5506 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
5507 == input_rel_hdr
->sh_entsize
)
5509 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
5510 rel_countp
= &elf_section_data (output_section
)->rel_count
;
5512 else if (elf_section_data (output_section
)->rel_hdr2
5513 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
5514 == input_rel_hdr
->sh_entsize
))
5516 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
5517 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
5520 BFD_ASSERT (output_rel_hdr
!= NULL
);
5522 bed
= get_elf_backend_data (output_bfd
);
5523 irela
= internal_relocs
;
5524 irelaend
= irela
+ NUM_SHDR_ENTRIES (input_rel_hdr
)
5525 * bed
->s
->int_rels_per_ext_rel
;
5527 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
5529 Elf_External_Rel
*erel
;
5530 Elf_Internal_Rel
*irel
;
5532 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (bed
->s
->int_rels_per_ext_rel
5533 * sizeof (Elf_Internal_Rel
));
5536 (*_bfd_error_handler
) (_("Error: out of memory"));
5540 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
+ *rel_countp
);
5541 for (; irela
< irelaend
; irela
+= bed
->s
->int_rels_per_ext_rel
, erel
++)
5545 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
5547 irel
[i
].r_offset
= irela
[i
].r_offset
;
5548 irel
[i
].r_info
= irela
[i
].r_info
;
5549 BFD_ASSERT (irela
[i
].r_addend
== 0);
5552 if (bed
->s
->swap_reloc_out
)
5553 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, (PTR
) erel
);
5555 elf_swap_reloc_out (output_bfd
, irel
, erel
);
5562 Elf_External_Rela
*erela
;
5564 BFD_ASSERT (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
));
5566 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
+ *rel_countp
);
5567 for (; irela
< irelaend
; irela
+= bed
->s
->int_rels_per_ext_rel
, erela
++)
5568 if (bed
->s
->swap_reloca_out
)
5569 (*bed
->s
->swap_reloca_out
) (output_bfd
, irela
, (PTR
) erela
);
5571 elf_swap_reloca_out (output_bfd
, irela
, erela
);
5574 /* Bump the counter, so that we know where to add the next set of
5576 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
5579 /* Link an input file into the linker output file. This function
5580 handles all the sections and relocations of the input file at once.
5581 This is so that we only have to read the local symbols once, and
5582 don't have to keep them in memory. */
5585 elf_link_input_bfd (finfo
, input_bfd
)
5586 struct elf_final_link_info
*finfo
;
5589 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
5590 bfd
*, asection
*, bfd_byte
*,
5591 Elf_Internal_Rela
*,
5592 Elf_Internal_Sym
*, asection
**));
5594 Elf_Internal_Shdr
*symtab_hdr
;
5597 Elf_External_Sym
*external_syms
;
5598 Elf_External_Sym
*esym
;
5599 Elf_External_Sym
*esymend
;
5600 Elf_Internal_Sym
*isym
;
5602 asection
**ppsection
;
5604 struct elf_backend_data
*bed
;
5605 boolean emit_relocs
;
5607 output_bfd
= finfo
->output_bfd
;
5608 bed
= get_elf_backend_data (output_bfd
);
5609 relocate_section
= bed
->elf_backend_relocate_section
;
5611 /* If this is a dynamic object, we don't want to do anything here:
5612 we don't want the local symbols, and we don't want the section
5614 if ((input_bfd
->flags
& DYNAMIC
) != 0)
5617 emit_relocs
= (finfo
->info
->relocateable
5618 || finfo
->info
->emitrelocations
5619 || bed
->elf_backend_emit_relocs
);
5621 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
5622 if (elf_bad_symtab (input_bfd
))
5624 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
5629 locsymcount
= symtab_hdr
->sh_info
;
5630 extsymoff
= symtab_hdr
->sh_info
;
5633 /* Read the local symbols. */
5634 if (symtab_hdr
->contents
!= NULL
)
5635 external_syms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
5636 else if (locsymcount
== 0)
5637 external_syms
= NULL
;
5640 external_syms
= finfo
->external_syms
;
5641 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
5642 || (bfd_read (external_syms
, sizeof (Elf_External_Sym
),
5643 locsymcount
, input_bfd
)
5644 != locsymcount
* sizeof (Elf_External_Sym
)))
5648 /* Swap in the local symbols and write out the ones which we know
5649 are going into the output file. */
5650 esym
= external_syms
;
5651 esymend
= esym
+ locsymcount
;
5652 isym
= finfo
->internal_syms
;
5653 pindex
= finfo
->indices
;
5654 ppsection
= finfo
->sections
;
5655 for (; esym
< esymend
; esym
++, isym
++, pindex
++, ppsection
++)
5659 Elf_Internal_Sym osym
;
5661 elf_swap_symbol_in (input_bfd
, esym
, isym
);
5664 if (elf_bad_symtab (input_bfd
))
5666 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
5674 if (isym
->st_shndx
== SHN_UNDEF
)
5676 isec
= bfd_und_section_ptr
;
5679 else if (isym
->st_shndx
> 0 && isym
->st_shndx
< SHN_LORESERVE
)
5681 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
5682 if (isec
&& elf_section_data (isec
)->merge_info
5683 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
5685 _bfd_merged_section_offset (output_bfd
, &isec
,
5686 elf_section_data (isec
)->merge_info
,
5687 isym
->st_value
, (bfd_vma
) 0);
5689 else if (isym
->st_shndx
== SHN_ABS
)
5691 isec
= bfd_abs_section_ptr
;
5694 else if (isym
->st_shndx
== SHN_COMMON
)
5696 isec
= bfd_com_section_ptr
;
5707 /* Don't output the first, undefined, symbol. */
5708 if (esym
== external_syms
)
5711 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
5715 /* Save away all section symbol values. */
5720 if (isec
->symbol
->value
!= isym
->st_value
)
5721 (*_bfd_error_handler
)
5722 (_("%s: invalid section symbol index 0x%x (%s) ingored"),
5723 bfd_get_filename (input_bfd
), isym
->st_shndx
,
5727 isec
->symbol
->value
= isym
->st_value
;
5730 /* If this is a discarded link-once section symbol, update
5731 it's value to that of the kept section symbol. The
5732 linker will keep the first of any matching link-once
5733 sections, so we should have already seen it's section
5734 symbol. I trust no-one will have the bright idea of
5735 re-ordering the bfd list... */
5737 && (bfd_get_section_flags (input_bfd
, isec
) & SEC_LINK_ONCE
) != 0
5738 && (ksec
= isec
->kept_section
) != NULL
)
5740 isym
->st_value
= ksec
->symbol
->value
;
5742 /* That put the value right, but the section info is all
5743 wrong. I hope this works. */
5744 isec
->output_offset
= ksec
->output_offset
;
5745 isec
->output_section
= ksec
->output_section
;
5748 /* We never output section symbols. Instead, we use the
5749 section symbol of the corresponding section in the output
5754 /* If we are stripping all symbols, we don't want to output this
5756 if (finfo
->info
->strip
== strip_all
)
5759 /* If we are discarding all local symbols, we don't want to
5760 output this one. If we are generating a relocateable output
5761 file, then some of the local symbols may be required by
5762 relocs; we output them below as we discover that they are
5764 if (finfo
->info
->discard
== discard_all
)
5767 /* If this symbol is defined in a section which we are
5768 discarding, we don't need to keep it, but note that
5769 linker_mark is only reliable for sections that have contents.
5770 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
5771 as well as linker_mark. */
5772 if (isym
->st_shndx
> 0
5773 && isym
->st_shndx
< SHN_LORESERVE
5775 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
5776 || (! finfo
->info
->relocateable
5777 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
5780 /* Get the name of the symbol. */
5781 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
5786 /* See if we are discarding symbols with this name. */
5787 if ((finfo
->info
->strip
== strip_some
5788 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
5790 || (((finfo
->info
->discard
== discard_sec_merge
5791 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocateable
)
5792 || finfo
->info
->discard
== discard_l
)
5793 && bfd_is_local_label_name (input_bfd
, name
)))
5796 /* If we get here, we are going to output this symbol. */
5800 /* Adjust the section index for the output file. */
5801 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
5802 isec
->output_section
);
5803 if (osym
.st_shndx
== (unsigned short) -1)
5806 *pindex
= bfd_get_symcount (output_bfd
);
5808 /* ELF symbols in relocateable files are section relative, but
5809 in executable files they are virtual addresses. Note that
5810 this code assumes that all ELF sections have an associated
5811 BFD section with a reasonable value for output_offset; below
5812 we assume that they also have a reasonable value for
5813 output_section. Any special sections must be set up to meet
5814 these requirements. */
5815 osym
.st_value
+= isec
->output_offset
;
5816 if (! finfo
->info
->relocateable
)
5817 osym
.st_value
+= isec
->output_section
->vma
;
5819 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
5823 /* Relocate the contents of each section. */
5824 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
5828 if (! o
->linker_mark
)
5830 /* This section was omitted from the link. */
5834 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5835 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
5838 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
5840 /* Section was created by elf_link_create_dynamic_sections
5845 /* Get the contents of the section. They have been cached by a
5846 relaxation routine. Note that o is a section in an input
5847 file, so the contents field will not have been set by any of
5848 the routines which work on output files. */
5849 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
5850 contents
= elf_section_data (o
)->this_hdr
.contents
;
5853 contents
= finfo
->contents
;
5854 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
5855 (file_ptr
) 0, o
->_raw_size
))
5859 if ((o
->flags
& SEC_RELOC
) != 0)
5861 Elf_Internal_Rela
*internal_relocs
;
5863 /* Get the swapped relocs. */
5864 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
5865 (input_bfd
, o
, finfo
->external_relocs
,
5866 finfo
->internal_relocs
, false));
5867 if (internal_relocs
== NULL
5868 && o
->reloc_count
> 0)
5871 /* Relocate the section by invoking a back end routine.
5873 The back end routine is responsible for adjusting the
5874 section contents as necessary, and (if using Rela relocs
5875 and generating a relocateable output file) adjusting the
5876 reloc addend as necessary.
5878 The back end routine does not have to worry about setting
5879 the reloc address or the reloc symbol index.
5881 The back end routine is given a pointer to the swapped in
5882 internal symbols, and can access the hash table entries
5883 for the external symbols via elf_sym_hashes (input_bfd).
5885 When generating relocateable output, the back end routine
5886 must handle STB_LOCAL/STT_SECTION symbols specially. The
5887 output symbol is going to be a section symbol
5888 corresponding to the output section, which will require
5889 the addend to be adjusted. */
5891 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
5892 input_bfd
, o
, contents
,
5894 finfo
->internal_syms
,
5900 Elf_Internal_Rela
*irela
;
5901 Elf_Internal_Rela
*irelaend
;
5902 struct elf_link_hash_entry
**rel_hash
;
5903 Elf_Internal_Shdr
*input_rel_hdr
;
5904 unsigned int next_erel
;
5905 void (* reloc_emitter
) PARAMS ((bfd
*, asection
*,
5906 Elf_Internal_Shdr
*,
5907 Elf_Internal_Rela
*));
5909 /* Adjust the reloc addresses and symbol indices. */
5911 irela
= internal_relocs
;
5913 + o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5914 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
5915 + elf_section_data (o
->output_section
)->rel_count
5916 + elf_section_data (o
->output_section
)->rel_count2
);
5917 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
5919 unsigned long r_symndx
;
5920 Elf_Internal_Sym
*isym
;
5923 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
5929 irela
->r_offset
+= o
->output_offset
;
5931 /* Relocs in an executable have to be virtual addresses. */
5932 if (finfo
->info
->emitrelocations
)
5933 irela
->r_offset
+= o
->output_section
->vma
;
5935 r_symndx
= ELF_R_SYM (irela
->r_info
);
5940 if (r_symndx
>= locsymcount
5941 || (elf_bad_symtab (input_bfd
)
5942 && finfo
->sections
[r_symndx
] == NULL
))
5944 struct elf_link_hash_entry
*rh
;
5947 /* This is a reloc against a global symbol. We
5948 have not yet output all the local symbols, so
5949 we do not know the symbol index of any global
5950 symbol. We set the rel_hash entry for this
5951 reloc to point to the global hash table entry
5952 for this symbol. The symbol index is then
5953 set at the end of elf_bfd_final_link. */
5954 indx
= r_symndx
- extsymoff
;
5955 rh
= elf_sym_hashes (input_bfd
)[indx
];
5956 while (rh
->root
.type
== bfd_link_hash_indirect
5957 || rh
->root
.type
== bfd_link_hash_warning
)
5958 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
5960 /* Setting the index to -2 tells
5961 elf_link_output_extsym that this symbol is
5963 BFD_ASSERT (rh
->indx
< 0);
5971 /* This is a reloc against a local symbol. */
5974 isym
= finfo
->internal_syms
+ r_symndx
;
5975 sec
= finfo
->sections
[r_symndx
];
5976 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
5978 /* I suppose the backend ought to fill in the
5979 section of any STT_SECTION symbol against a
5980 processor specific section. If we have
5981 discarded a section, the output_section will
5982 be the absolute section. */
5984 && (bfd_is_abs_section (sec
)
5985 || (sec
->output_section
!= NULL
5986 && bfd_is_abs_section (sec
->output_section
))))
5988 else if (sec
== NULL
|| sec
->owner
== NULL
)
5990 bfd_set_error (bfd_error_bad_value
);
5995 r_symndx
= sec
->output_section
->target_index
;
5996 BFD_ASSERT (r_symndx
!= 0);
6001 if (finfo
->indices
[r_symndx
] == -1)
6007 if (finfo
->info
->strip
== strip_all
)
6009 /* You can't do ld -r -s. */
6010 bfd_set_error (bfd_error_invalid_operation
);
6014 /* This symbol was skipped earlier, but
6015 since it is needed by a reloc, we
6016 must output it now. */
6017 link
= symtab_hdr
->sh_link
;
6018 name
= (bfd_elf_string_from_elf_section
6019 (input_bfd
, link
, isym
->st_name
));
6023 osec
= sec
->output_section
;
6025 _bfd_elf_section_from_bfd_section (output_bfd
,
6027 if (isym
->st_shndx
== (unsigned short) -1)
6030 isym
->st_value
+= sec
->output_offset
;
6031 if (! finfo
->info
->relocateable
)
6032 isym
->st_value
+= osec
->vma
;
6034 finfo
->indices
[r_symndx
]
6035 = bfd_get_symcount (output_bfd
);
6037 if (! elf_link_output_sym (finfo
, name
, isym
, sec
))
6041 r_symndx
= finfo
->indices
[r_symndx
];
6044 irela
->r_info
= ELF_R_INFO (r_symndx
,
6045 ELF_R_TYPE (irela
->r_info
));
6048 /* Swap out the relocs. */
6049 if (bed
->elf_backend_emit_relocs
6050 && !(finfo
->info
->relocateable
6051 || finfo
->info
->emitrelocations
))
6052 reloc_emitter
= bed
->elf_backend_emit_relocs
;
6054 reloc_emitter
= elf_link_output_relocs
;
6056 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6057 (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
, internal_relocs
);
6059 input_rel_hdr
= elf_section_data (o
)->rel_hdr2
;
6062 internal_relocs
+= NUM_SHDR_ENTRIES (input_rel_hdr
)
6063 * bed
->s
->int_rels_per_ext_rel
;
6064 reloc_emitter (output_bfd
, o
, input_rel_hdr
, internal_relocs
);
6070 /* Write out the modified section contents. */
6071 if (elf_section_data (o
)->stab_info
)
6073 if (! (_bfd_write_section_stabs
6074 (output_bfd
, &elf_hash_table (finfo
->info
)->stab_info
,
6075 o
, &elf_section_data (o
)->stab_info
, contents
)))
6078 else if (elf_section_data (o
)->merge_info
)
6080 if (! (_bfd_write_merged_section
6081 (output_bfd
, o
, elf_section_data (o
)->merge_info
)))
6086 if (! (o
->flags
& SEC_EXCLUDE
) &&
6087 ! bfd_set_section_contents (output_bfd
, o
->output_section
,
6088 contents
, o
->output_offset
,
6089 (o
->_cooked_size
!= 0
6099 /* Generate a reloc when linking an ELF file. This is a reloc
6100 requested by the linker, and does come from any input file. This
6101 is used to build constructor and destructor tables when linking
6105 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
6107 struct bfd_link_info
*info
;
6108 asection
*output_section
;
6109 struct bfd_link_order
*link_order
;
6111 reloc_howto_type
*howto
;
6115 struct elf_link_hash_entry
**rel_hash_ptr
;
6116 Elf_Internal_Shdr
*rel_hdr
;
6117 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
6119 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
6122 bfd_set_error (bfd_error_bad_value
);
6126 addend
= link_order
->u
.reloc
.p
->addend
;
6128 /* Figure out the symbol index. */
6129 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
6130 + elf_section_data (output_section
)->rel_count
6131 + elf_section_data (output_section
)->rel_count2
);
6132 if (link_order
->type
== bfd_section_reloc_link_order
)
6134 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
6135 BFD_ASSERT (indx
!= 0);
6136 *rel_hash_ptr
= NULL
;
6140 struct elf_link_hash_entry
*h
;
6142 /* Treat a reloc against a defined symbol as though it were
6143 actually against the section. */
6144 h
= ((struct elf_link_hash_entry
*)
6145 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
6146 link_order
->u
.reloc
.p
->u
.name
,
6147 false, false, true));
6149 && (h
->root
.type
== bfd_link_hash_defined
6150 || h
->root
.type
== bfd_link_hash_defweak
))
6154 section
= h
->root
.u
.def
.section
;
6155 indx
= section
->output_section
->target_index
;
6156 *rel_hash_ptr
= NULL
;
6157 /* It seems that we ought to add the symbol value to the
6158 addend here, but in practice it has already been added
6159 because it was passed to constructor_callback. */
6160 addend
+= section
->output_section
->vma
+ section
->output_offset
;
6164 /* Setting the index to -2 tells elf_link_output_extsym that
6165 this symbol is used by a reloc. */
6172 if (! ((*info
->callbacks
->unattached_reloc
)
6173 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
6174 (asection
*) NULL
, (bfd_vma
) 0)))
6180 /* If this is an inplace reloc, we must write the addend into the
6182 if (howto
->partial_inplace
&& addend
!= 0)
6185 bfd_reloc_status_type rstat
;
6189 size
= bfd_get_reloc_size (howto
);
6190 buf
= (bfd_byte
*) bfd_zmalloc (size
);
6191 if (buf
== (bfd_byte
*) NULL
)
6193 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
6199 case bfd_reloc_outofrange
:
6201 case bfd_reloc_overflow
:
6202 if (! ((*info
->callbacks
->reloc_overflow
)
6204 (link_order
->type
== bfd_section_reloc_link_order
6205 ? bfd_section_name (output_bfd
,
6206 link_order
->u
.reloc
.p
->u
.section
)
6207 : link_order
->u
.reloc
.p
->u
.name
),
6208 howto
->name
, addend
, (bfd
*) NULL
, (asection
*) NULL
,
6216 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
6217 (file_ptr
) link_order
->offset
, size
);
6223 /* The address of a reloc is relative to the section in a
6224 relocateable file, and is a virtual address in an executable
6226 offset
= link_order
->offset
;
6227 if (! info
->relocateable
)
6228 offset
+= output_section
->vma
;
6230 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
6232 if (rel_hdr
->sh_type
== SHT_REL
)
6234 Elf_Internal_Rel
*irel
;
6235 Elf_External_Rel
*erel
;
6238 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (bed
->s
->int_rels_per_ext_rel
6239 * sizeof (Elf_Internal_Rel
));
6243 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
6244 irel
[i
].r_offset
= offset
;
6245 irel
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
6247 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
6248 + elf_section_data (output_section
)->rel_count
);
6250 if (bed
->s
->swap_reloc_out
)
6251 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, (bfd_byte
*) erel
);
6253 elf_swap_reloc_out (output_bfd
, irel
, erel
);
6259 Elf_Internal_Rela
*irela
;
6260 Elf_External_Rela
*erela
;
6263 irela
= (Elf_Internal_Rela
*) bfd_zmalloc (bed
->s
->int_rels_per_ext_rel
6264 * sizeof (Elf_Internal_Rela
));
6268 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
6269 irela
[i
].r_offset
= offset
;
6270 irela
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
6271 irela
[0].r_addend
= addend
;
6273 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
6274 + elf_section_data (output_section
)->rel_count
);
6276 if (bed
->s
->swap_reloca_out
)
6277 (*bed
->s
->swap_reloca_out
) (output_bfd
, irela
, (bfd_byte
*) erela
);
6279 elf_swap_reloca_out (output_bfd
, irela
, erela
);
6282 ++elf_section_data (output_section
)->rel_count
;
6287 /* Allocate a pointer to live in a linker created section. */
6290 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
6292 struct bfd_link_info
*info
;
6293 elf_linker_section_t
*lsect
;
6294 struct elf_link_hash_entry
*h
;
6295 const Elf_Internal_Rela
*rel
;
6297 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
6298 elf_linker_section_pointers_t
*linker_section_ptr
;
6299 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);;
6301 BFD_ASSERT (lsect
!= NULL
);
6303 /* Is this a global symbol? */
6306 /* Has this symbol already been allocated? If so, our work is done. */
6307 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
6312 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
6313 /* Make sure this symbol is output as a dynamic symbol. */
6314 if (h
->dynindx
== -1)
6316 if (! elf_link_record_dynamic_symbol (info
, h
))
6320 if (lsect
->rel_section
)
6321 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
6325 /* Allocation of a pointer to a local symbol. */
6326 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
6328 /* Allocate a table to hold the local symbols if first time. */
6331 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
6332 register unsigned int i
;
6334 ptr
= (elf_linker_section_pointers_t
**)
6336 num_symbols
* sizeof (elf_linker_section_pointers_t
*));
6341 elf_local_ptr_offsets (abfd
) = ptr
;
6342 for (i
= 0; i
< num_symbols
; i
++)
6343 ptr
[i
] = (elf_linker_section_pointers_t
*) 0;
6346 /* Has this symbol already been allocated? If so, our work is done. */
6347 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
6352 ptr_linker_section_ptr
= &ptr
[r_symndx
];
6356 /* If we are generating a shared object, we need to
6357 output a R_<xxx>_RELATIVE reloc so that the
6358 dynamic linker can adjust this GOT entry. */
6359 BFD_ASSERT (lsect
->rel_section
!= NULL
);
6360 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
6364 /* Allocate space for a pointer in the linker section, and allocate
6365 a new pointer record from internal memory. */
6366 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
6367 linker_section_ptr
= (elf_linker_section_pointers_t
*)
6368 bfd_alloc (abfd
, sizeof (elf_linker_section_pointers_t
));
6370 if (!linker_section_ptr
)
6373 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
6374 linker_section_ptr
->addend
= rel
->r_addend
;
6375 linker_section_ptr
->which
= lsect
->which
;
6376 linker_section_ptr
->written_address_p
= false;
6377 *ptr_linker_section_ptr
= linker_section_ptr
;
6380 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
6382 linker_section_ptr
->offset
= (lsect
->section
->_raw_size
6383 - lsect
->hole_size
+ (ARCH_SIZE
/ 8));
6384 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
6385 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
6386 if (lsect
->sym_hash
)
6388 /* Bump up symbol value if needed. */
6389 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
6391 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
6392 lsect
->sym_hash
->root
.root
.string
,
6393 (long) ARCH_SIZE
/ 8,
6394 (long) lsect
->sym_hash
->root
.u
.def
.value
);
6400 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
6402 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
6406 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
6407 lsect
->name
, (long) linker_section_ptr
->offset
,
6408 (long) lsect
->section
->_raw_size
);
6415 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
6418 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
6421 /* Fill in the address for a pointer generated in a linker section. */
6424 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
,
6425 relocation
, rel
, relative_reloc
)
6428 struct bfd_link_info
*info
;
6429 elf_linker_section_t
*lsect
;
6430 struct elf_link_hash_entry
*h
;
6432 const Elf_Internal_Rela
*rel
;
6435 elf_linker_section_pointers_t
*linker_section_ptr
;
6437 BFD_ASSERT (lsect
!= NULL
);
6441 /* Handle global symbol. */
6442 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
6443 (h
->linker_section_pointer
,
6447 BFD_ASSERT (linker_section_ptr
!= NULL
);
6449 if (! elf_hash_table (info
)->dynamic_sections_created
6452 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
6454 /* This is actually a static link, or it is a
6455 -Bsymbolic link and the symbol is defined
6456 locally. We must initialize this entry in the
6459 When doing a dynamic link, we create a .rela.<xxx>
6460 relocation entry to initialize the value. This
6461 is done in the finish_dynamic_symbol routine. */
6462 if (!linker_section_ptr
->written_address_p
)
6464 linker_section_ptr
->written_address_p
= true;
6465 bfd_put_ptr (output_bfd
,
6466 relocation
+ linker_section_ptr
->addend
,
6467 (lsect
->section
->contents
6468 + linker_section_ptr
->offset
));
6474 /* Handle local symbol. */
6475 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
6476 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
6477 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
6478 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
6479 (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
6483 BFD_ASSERT (linker_section_ptr
!= NULL
);
6485 /* Write out pointer if it hasn't been rewritten out before. */
6486 if (!linker_section_ptr
->written_address_p
)
6488 linker_section_ptr
->written_address_p
= true;
6489 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
6490 lsect
->section
->contents
+ linker_section_ptr
->offset
);
6494 asection
*srel
= lsect
->rel_section
;
6495 Elf_Internal_Rela
*outrel
;
6496 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
6499 outrel
= (Elf_Internal_Rela
*)
6500 bfd_zmalloc (sizeof (Elf_Internal_Rela
)
6501 * bed
->s
->int_rels_per_ext_rel
);
6504 (*_bfd_error_handler
) (_("Error: out of memory"));
6508 /* We need to generate a relative reloc for the dynamic
6512 srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
6514 lsect
->rel_section
= srel
;
6517 BFD_ASSERT (srel
!= NULL
);
6519 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
6520 outrel
[i
].r_offset
= (lsect
->section
->output_section
->vma
6521 + lsect
->section
->output_offset
6522 + linker_section_ptr
->offset
);
6523 outrel
[0].r_info
= ELF_R_INFO (0, relative_reloc
);
6524 outrel
[0].r_addend
= 0;
6525 elf_swap_reloca_out (output_bfd
, outrel
,
6526 (((Elf_External_Rela
*)
6527 lsect
->section
->contents
)
6528 + (elf_section_data (lsect
->section
)
6530 ++elf_section_data (lsect
->section
)->rel_count
;
6537 relocation
= (lsect
->section
->output_offset
6538 + linker_section_ptr
->offset
6539 - lsect
->hole_offset
6540 - lsect
->sym_offset
);
6544 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
6545 lsect
->name
, (long) relocation
, (long) relocation
);
6548 /* Subtract out the addend, because it will get added back in by the normal
6550 return relocation
- linker_section_ptr
->addend
;
6553 /* Garbage collect unused sections. */
6555 static boolean elf_gc_mark
6556 PARAMS ((struct bfd_link_info
*info
, asection
*sec
,
6557 asection
* (*gc_mark_hook
)
6558 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
6559 struct elf_link_hash_entry
*, Elf_Internal_Sym
*))));
6561 static boolean elf_gc_sweep
6562 PARAMS ((struct bfd_link_info
*info
,
6563 boolean (*gc_sweep_hook
)
6564 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
6565 const Elf_Internal_Rela
*relocs
))));
6567 static boolean elf_gc_sweep_symbol
6568 PARAMS ((struct elf_link_hash_entry
*h
, PTR idxptr
));
6570 static boolean elf_gc_allocate_got_offsets
6571 PARAMS ((struct elf_link_hash_entry
*h
, PTR offarg
));
6573 static boolean elf_gc_propagate_vtable_entries_used
6574 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
6576 static boolean elf_gc_smash_unused_vtentry_relocs
6577 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
6579 /* The mark phase of garbage collection. For a given section, mark
6580 it, and all the sections which define symbols to which it refers. */
6583 elf_gc_mark (info
, sec
, gc_mark_hook
)
6584 struct bfd_link_info
*info
;
6586 asection
* (*gc_mark_hook
)
6587 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
6588 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
6594 /* Look through the section relocs. */
6596 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
6598 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
6599 Elf_Internal_Shdr
*symtab_hdr
;
6600 struct elf_link_hash_entry
**sym_hashes
;
6603 Elf_External_Sym
*locsyms
, *freesyms
= NULL
;
6604 bfd
*input_bfd
= sec
->owner
;
6605 struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
6607 /* GCFIXME: how to arrange so that relocs and symbols are not
6608 reread continually? */
6610 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6611 sym_hashes
= elf_sym_hashes (input_bfd
);
6613 /* Read the local symbols. */
6614 if (elf_bad_symtab (input_bfd
))
6616 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6620 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
6621 if (symtab_hdr
->contents
)
6622 locsyms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
6623 else if (nlocsyms
== 0)
6627 locsyms
= freesyms
=
6628 bfd_malloc (nlocsyms
* sizeof (Elf_External_Sym
));
6629 if (freesyms
== NULL
6630 || bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
6631 || (bfd_read (locsyms
, sizeof (Elf_External_Sym
),
6632 nlocsyms
, input_bfd
)
6633 != nlocsyms
* sizeof (Elf_External_Sym
)))
6640 /* Read the relocations. */
6641 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
6642 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
6643 info
->keep_memory
));
6644 if (relstart
== NULL
)
6649 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6651 for (rel
= relstart
; rel
< relend
; rel
++)
6653 unsigned long r_symndx
;
6655 struct elf_link_hash_entry
*h
;
6658 r_symndx
= ELF_R_SYM (rel
->r_info
);
6662 if (elf_bad_symtab (sec
->owner
))
6664 elf_swap_symbol_in (input_bfd
, &locsyms
[r_symndx
], &s
);
6665 if (ELF_ST_BIND (s
.st_info
) == STB_LOCAL
)
6666 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, NULL
, &s
);
6669 h
= sym_hashes
[r_symndx
- extsymoff
];
6670 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, h
, NULL
);
6673 else if (r_symndx
>= nlocsyms
)
6675 h
= sym_hashes
[r_symndx
- extsymoff
];
6676 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, h
, NULL
);
6680 elf_swap_symbol_in (input_bfd
, &locsyms
[r_symndx
], &s
);
6681 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, NULL
, &s
);
6684 if (rsec
&& !rsec
->gc_mark
)
6685 if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
6693 if (!info
->keep_memory
)
6703 /* The sweep phase of garbage collection. Remove all garbage sections. */
6706 elf_gc_sweep (info
, gc_sweep_hook
)
6707 struct bfd_link_info
*info
;
6708 boolean (*gc_sweep_hook
)
6709 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
6710 const Elf_Internal_Rela
*relocs
));
6714 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
6718 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
6721 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6723 /* Keep special sections. Keep .debug sections. */
6724 if ((o
->flags
& SEC_LINKER_CREATED
)
6725 || (o
->flags
& SEC_DEBUGGING
))
6731 /* Skip sweeping sections already excluded. */
6732 if (o
->flags
& SEC_EXCLUDE
)
6735 /* Since this is early in the link process, it is simple
6736 to remove a section from the output. */
6737 o
->flags
|= SEC_EXCLUDE
;
6739 /* But we also have to update some of the relocation
6740 info we collected before. */
6742 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
6744 Elf_Internal_Rela
*internal_relocs
;
6747 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
6748 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
6749 if (internal_relocs
== NULL
)
6752 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
6754 if (!info
->keep_memory
)
6755 free (internal_relocs
);
6763 /* Remove the symbols that were in the swept sections from the dynamic
6764 symbol table. GCFIXME: Anyone know how to get them out of the
6765 static symbol table as well? */
6769 elf_link_hash_traverse (elf_hash_table (info
),
6770 elf_gc_sweep_symbol
,
6773 elf_hash_table (info
)->dynsymcount
= i
;
6779 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6782 elf_gc_sweep_symbol (h
, idxptr
)
6783 struct elf_link_hash_entry
*h
;
6786 int *idx
= (int *) idxptr
;
6788 if (h
->dynindx
!= -1
6789 && ((h
->root
.type
!= bfd_link_hash_defined
6790 && h
->root
.type
!= bfd_link_hash_defweak
)
6791 || h
->root
.u
.def
.section
->gc_mark
))
6792 h
->dynindx
= (*idx
)++;
6797 /* Propogate collected vtable information. This is called through
6798 elf_link_hash_traverse. */
6801 elf_gc_propagate_vtable_entries_used (h
, okp
)
6802 struct elf_link_hash_entry
*h
;
6805 /* Those that are not vtables. */
6806 if (h
->vtable_parent
== NULL
)
6809 /* Those vtables that do not have parents, we cannot merge. */
6810 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
6813 /* If we've already been done, exit. */
6814 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
6817 /* Make sure the parent's table is up to date. */
6818 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
6820 if (h
->vtable_entries_used
== NULL
)
6822 /* None of this table's entries were referenced. Re-use the
6824 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
6825 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
6832 /* Or the parent's entries into ours. */
6833 cu
= h
->vtable_entries_used
;
6835 pu
= h
->vtable_parent
->vtable_entries_used
;
6838 asection
*sec
= h
->root
.u
.def
.section
;
6839 struct elf_backend_data
*bed
= get_elf_backend_data (sec
->owner
);
6840 int file_align
= bed
->s
->file_align
;
6842 n
= h
->vtable_parent
->vtable_entries_size
/ file_align
;
6845 if (*pu
) *cu
= true;
6855 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
6856 struct elf_link_hash_entry
*h
;
6860 bfd_vma hstart
, hend
;
6861 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
6862 struct elf_backend_data
*bed
;
6865 /* Take care of both those symbols that do not describe vtables as
6866 well as those that are not loaded. */
6867 if (h
->vtable_parent
== NULL
)
6870 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
6871 || h
->root
.type
== bfd_link_hash_defweak
);
6873 sec
= h
->root
.u
.def
.section
;
6874 hstart
= h
->root
.u
.def
.value
;
6875 hend
= hstart
+ h
->size
;
6877 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
6878 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, true));
6880 return *(boolean
*) okp
= false;
6881 bed
= get_elf_backend_data (sec
->owner
);
6882 file_align
= bed
->s
->file_align
;
6884 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6886 for (rel
= relstart
; rel
< relend
; ++rel
)
6887 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
6889 /* If the entry is in use, do nothing. */
6890 if (h
->vtable_entries_used
6891 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
6893 bfd_vma entry
= (rel
->r_offset
- hstart
) / file_align
;
6894 if (h
->vtable_entries_used
[entry
])
6897 /* Otherwise, kill it. */
6898 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
6904 /* Do mark and sweep of unused sections. */
6907 elf_gc_sections (abfd
, info
)
6909 struct bfd_link_info
*info
;
6913 asection
* (*gc_mark_hook
)
6914 PARAMS ((bfd
*abfd
, struct bfd_link_info
*, Elf_Internal_Rela
*,
6915 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
6917 if (!get_elf_backend_data (abfd
)->can_gc_sections
6918 || info
->relocateable
|| info
->emitrelocations
6919 || elf_hash_table (info
)->dynamic_sections_created
)
6922 /* Apply transitive closure to the vtable entry usage info. */
6923 elf_link_hash_traverse (elf_hash_table (info
),
6924 elf_gc_propagate_vtable_entries_used
,
6929 /* Kill the vtable relocations that were not used. */
6930 elf_link_hash_traverse (elf_hash_table (info
),
6931 elf_gc_smash_unused_vtentry_relocs
,
6936 /* Grovel through relocs to find out who stays ... */
6938 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
6939 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
6943 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
6946 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6948 if (o
->flags
& SEC_KEEP
)
6949 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
6954 /* ... and mark SEC_EXCLUDE for those that go. */
6955 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
6961 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
6964 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
6967 struct elf_link_hash_entry
*h
;
6970 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
6971 struct elf_link_hash_entry
**search
, *child
;
6972 bfd_size_type extsymcount
;
6974 /* The sh_info field of the symtab header tells us where the
6975 external symbols start. We don't care about the local symbols at
6977 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
6978 if (!elf_bad_symtab (abfd
))
6979 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
6981 sym_hashes
= elf_sym_hashes (abfd
);
6982 sym_hashes_end
= sym_hashes
+ extsymcount
;
6984 /* Hunt down the child symbol, which is in this section at the same
6985 offset as the relocation. */
6986 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
6988 if ((child
= *search
) != NULL
6989 && (child
->root
.type
== bfd_link_hash_defined
6990 || child
->root
.type
== bfd_link_hash_defweak
)
6991 && child
->root
.u
.def
.section
== sec
6992 && child
->root
.u
.def
.value
== offset
)
6996 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
6997 bfd_get_filename (abfd
), sec
->name
,
6998 (unsigned long) offset
);
6999 bfd_set_error (bfd_error_invalid_operation
);
7005 /* This *should* only be the absolute section. It could potentially
7006 be that someone has defined a non-global vtable though, which
7007 would be bad. It isn't worth paging in the local symbols to be
7008 sure though; that case should simply be handled by the assembler. */
7010 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
7013 child
->vtable_parent
= h
;
7018 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
7021 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
7022 bfd
*abfd ATTRIBUTE_UNUSED
;
7023 asection
*sec ATTRIBUTE_UNUSED
;
7024 struct elf_link_hash_entry
*h
;
7027 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7028 int file_align
= bed
->s
->file_align
;
7030 if (addend
>= h
->vtable_entries_size
)
7033 boolean
*ptr
= h
->vtable_entries_used
;
7035 /* While the symbol is undefined, we have to be prepared to handle
7037 if (h
->root
.type
== bfd_link_hash_undefined
)
7044 /* Oops! We've got a reference past the defined end of
7045 the table. This is probably a bug -- shall we warn? */
7050 /* Allocate one extra entry for use as a "done" flag for the
7051 consolidation pass. */
7052 bytes
= (size
/ file_align
+ 1) * sizeof (boolean
);
7056 ptr
= bfd_realloc (ptr
- 1, bytes
);
7062 oldbytes
= ((h
->vtable_entries_size
/ file_align
+ 1)
7063 * sizeof (boolean
));
7064 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
7068 ptr
= bfd_zmalloc (bytes
);
7073 /* And arrange for that done flag to be at index -1. */
7074 h
->vtable_entries_used
= ptr
+ 1;
7075 h
->vtable_entries_size
= size
;
7078 h
->vtable_entries_used
[addend
/ file_align
] = true;
7083 /* And an accompanying bit to work out final got entry offsets once
7084 we're done. Should be called from final_link. */
7087 elf_gc_common_finalize_got_offsets (abfd
, info
)
7089 struct bfd_link_info
*info
;
7092 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7095 /* The GOT offset is relative to the .got section, but the GOT header is
7096 put into the .got.plt section, if the backend uses it. */
7097 if (bed
->want_got_plt
)
7100 gotoff
= bed
->got_header_size
;
7102 /* Do the local .got entries first. */
7103 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
7105 bfd_signed_vma
*local_got
;
7106 bfd_size_type j
, locsymcount
;
7107 Elf_Internal_Shdr
*symtab_hdr
;
7109 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
7112 local_got
= elf_local_got_refcounts (i
);
7116 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
7117 if (elf_bad_symtab (i
))
7118 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
7120 locsymcount
= symtab_hdr
->sh_info
;
7122 for (j
= 0; j
< locsymcount
; ++j
)
7124 if (local_got
[j
] > 0)
7126 local_got
[j
] = gotoff
;
7127 gotoff
+= ARCH_SIZE
/ 8;
7130 local_got
[j
] = (bfd_vma
) -1;
7134 /* Then the global .got entries. .plt refcounts are handled by
7135 adjust_dynamic_symbol */
7136 elf_link_hash_traverse (elf_hash_table (info
),
7137 elf_gc_allocate_got_offsets
,
7142 /* We need a special top-level link routine to convert got reference counts
7143 to real got offsets. */
7146 elf_gc_allocate_got_offsets (h
, offarg
)
7147 struct elf_link_hash_entry
*h
;
7150 bfd_vma
*off
= (bfd_vma
*) offarg
;
7152 if (h
->got
.refcount
> 0)
7154 h
->got
.offset
= off
[0];
7155 off
[0] += ARCH_SIZE
/ 8;
7158 h
->got
.offset
= (bfd_vma
) -1;
7163 /* Many folk need no more in the way of final link than this, once
7164 got entry reference counting is enabled. */
7167 elf_gc_common_final_link (abfd
, info
)
7169 struct bfd_link_info
*info
;
7171 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
7174 /* Invoke the regular ELF backend linker to do all the work. */
7175 return elf_bfd_final_link (abfd
, info
);
7178 /* This function will be called though elf_link_hash_traverse to store
7179 all hash value of the exported symbols in an array. */
7182 elf_collect_hash_codes (h
, data
)
7183 struct elf_link_hash_entry
*h
;
7186 unsigned long **valuep
= (unsigned long **) data
;
7192 /* Ignore indirect symbols. These are added by the versioning code. */
7193 if (h
->dynindx
== -1)
7196 name
= h
->root
.root
.string
;
7197 p
= strchr (name
, ELF_VER_CHR
);
7200 alc
= bfd_malloc (p
- name
+ 1);
7201 memcpy (alc
, name
, p
- name
);
7202 alc
[p
- name
] = '\0';
7206 /* Compute the hash value. */
7207 ha
= bfd_elf_hash (name
);
7209 /* Store the found hash value in the array given as the argument. */
7212 /* And store it in the struct so that we can put it in the hash table
7214 h
->elf_hash_value
= ha
;