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
**));
70 static int elf_link_sort_cmp1
71 PARAMS ((const void *, const void *));
72 static int elf_link_sort_cmp2
73 PARAMS ((const void *, const void *));
74 static size_t elf_link_sort_relocs
75 PARAMS ((bfd
*, struct bfd_link_info
*, asection
**));
77 /* Given an ELF BFD, add symbols to the global hash table as
81 elf_bfd_link_add_symbols (abfd
, info
)
83 struct bfd_link_info
*info
;
85 switch (bfd_get_format (abfd
))
88 return elf_link_add_object_symbols (abfd
, info
);
90 return elf_link_add_archive_symbols (abfd
, info
);
92 bfd_set_error (bfd_error_wrong_format
);
97 /* Return true iff this is a non-common, definition of a non-function symbol. */
99 is_global_data_symbol_definition (abfd
, sym
)
100 bfd
* abfd ATTRIBUTE_UNUSED
;
101 Elf_Internal_Sym
* sym
;
103 /* Local symbols do not count, but target specific ones might. */
104 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
105 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
108 /* Function symbols do not count. */
109 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
112 /* If the section is undefined, then so is the symbol. */
113 if (sym
->st_shndx
== SHN_UNDEF
)
116 /* If the symbol is defined in the common section, then
117 it is a common definition and so does not count. */
118 if (sym
->st_shndx
== SHN_COMMON
)
121 /* If the symbol is in a target specific section then we
122 must rely upon the backend to tell us what it is. */
123 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
124 /* FIXME - this function is not coded yet:
126 return _bfd_is_global_symbol_definition (abfd, sym);
128 Instead for now assume that the definition is not global,
129 Even if this is wrong, at least the linker will behave
130 in the same way that it used to do. */
136 /* Search the symbol table of the archive element of the archive ABFD
137 whose archive map contains a mention of SYMDEF, and determine if
138 the symbol is defined in this element. */
140 elf_link_is_defined_archive_symbol (abfd
, symdef
)
144 Elf_Internal_Shdr
* hdr
;
145 Elf_External_Sym
* esym
;
146 Elf_External_Sym
* esymend
;
147 Elf_External_Sym
* buf
= NULL
;
148 bfd_size_type symcount
;
149 bfd_size_type extsymcount
;
150 bfd_size_type extsymoff
;
151 boolean result
= false;
155 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
156 if (abfd
== (bfd
*) NULL
)
159 if (! bfd_check_format (abfd
, bfd_object
))
162 /* If we have already included the element containing this symbol in the
163 link then we do not need to include it again. Just claim that any symbol
164 it contains is not a definition, so that our caller will not decide to
165 (re)include this element. */
166 if (abfd
->archive_pass
)
169 /* Select the appropriate symbol table. */
170 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
171 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
173 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
175 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
177 /* The sh_info field of the symtab header tells us where the
178 external symbols start. We don't care about the local symbols. */
179 if (elf_bad_symtab (abfd
))
181 extsymcount
= symcount
;
186 extsymcount
= symcount
- hdr
->sh_info
;
187 extsymoff
= hdr
->sh_info
;
190 amt
= extsymcount
* sizeof (Elf_External_Sym
);
191 buf
= (Elf_External_Sym
*) bfd_malloc (amt
);
192 if (buf
== NULL
&& extsymcount
!= 0)
195 /* Read in the symbol table.
196 FIXME: This ought to be cached somewhere. */
197 pos
= hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
);
198 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
199 || bfd_bread ((PTR
) buf
, amt
, abfd
) != amt
)
205 /* Scan the symbol table looking for SYMDEF. */
206 esymend
= buf
+ extsymcount
;
211 Elf_Internal_Sym sym
;
214 elf_swap_symbol_in (abfd
, esym
, & sym
);
216 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
217 if (name
== (const char *) NULL
)
220 if (strcmp (name
, symdef
->name
) == 0)
222 result
= is_global_data_symbol_definition (abfd
, & sym
);
232 /* Add symbols from an ELF archive file to the linker hash table. We
233 don't use _bfd_generic_link_add_archive_symbols because of a
234 problem which arises on UnixWare. The UnixWare libc.so is an
235 archive which includes an entry libc.so.1 which defines a bunch of
236 symbols. The libc.so archive also includes a number of other
237 object files, which also define symbols, some of which are the same
238 as those defined in libc.so.1. Correct linking requires that we
239 consider each object file in turn, and include it if it defines any
240 symbols we need. _bfd_generic_link_add_archive_symbols does not do
241 this; it looks through the list of undefined symbols, and includes
242 any object file which defines them. When this algorithm is used on
243 UnixWare, it winds up pulling in libc.so.1 early and defining a
244 bunch of symbols. This means that some of the other objects in the
245 archive are not included in the link, which is incorrect since they
246 precede libc.so.1 in the archive.
248 Fortunately, ELF archive handling is simpler than that done by
249 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
250 oddities. In ELF, if we find a symbol in the archive map, and the
251 symbol is currently undefined, we know that we must pull in that
254 Unfortunately, we do have to make multiple passes over the symbol
255 table until nothing further is resolved. */
258 elf_link_add_archive_symbols (abfd
, info
)
260 struct bfd_link_info
*info
;
263 boolean
*defined
= NULL
;
264 boolean
*included
= NULL
;
269 if (! bfd_has_map (abfd
))
271 /* An empty archive is a special case. */
272 if (bfd_openr_next_archived_file (abfd
, (bfd
*) NULL
) == NULL
)
274 bfd_set_error (bfd_error_no_armap
);
278 /* Keep track of all symbols we know to be already defined, and all
279 files we know to be already included. This is to speed up the
280 second and subsequent passes. */
281 c
= bfd_ardata (abfd
)->symdef_count
;
285 amt
*= sizeof (boolean
);
286 defined
= (boolean
*) bfd_malloc (amt
);
287 included
= (boolean
*) bfd_malloc (amt
);
288 if (defined
== (boolean
*) NULL
|| included
== (boolean
*) NULL
)
290 memset (defined
, 0, (size_t) amt
);
291 memset (included
, 0, (size_t) amt
);
293 symdefs
= bfd_ardata (abfd
)->symdefs
;
306 symdefend
= symdef
+ c
;
307 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
309 struct elf_link_hash_entry
*h
;
311 struct bfd_link_hash_entry
*undefs_tail
;
314 if (defined
[i
] || included
[i
])
316 if (symdef
->file_offset
== last
)
322 h
= elf_link_hash_lookup (elf_hash_table (info
), symdef
->name
,
323 false, false, false);
329 /* If this is a default version (the name contains @@),
330 look up the symbol again without the version. The
331 effect is that references to the symbol without the
332 version will be matched by the default symbol in the
335 p
= strchr (symdef
->name
, ELF_VER_CHR
);
336 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
339 copy
= bfd_alloc (abfd
, (bfd_size_type
) (p
- symdef
->name
+ 1));
342 memcpy (copy
, symdef
->name
, (size_t) (p
- symdef
->name
));
343 copy
[p
- symdef
->name
] = '\0';
345 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
346 false, false, false);
348 bfd_release (abfd
, copy
);
354 if (h
->root
.type
== bfd_link_hash_common
)
356 /* We currently have a common symbol. The archive map contains
357 a reference to this symbol, so we may want to include it. We
358 only want to include it however, if this archive element
359 contains a definition of the symbol, not just another common
362 Unfortunately some archivers (including GNU ar) will put
363 declarations of common symbols into their archive maps, as
364 well as real definitions, so we cannot just go by the archive
365 map alone. Instead we must read in the element's symbol
366 table and check that to see what kind of symbol definition
368 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
371 else if (h
->root
.type
!= bfd_link_hash_undefined
)
373 if (h
->root
.type
!= bfd_link_hash_undefweak
)
378 /* We need to include this archive member. */
379 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
380 if (element
== (bfd
*) NULL
)
383 if (! bfd_check_format (element
, bfd_object
))
386 /* Doublecheck that we have not included this object
387 already--it should be impossible, but there may be
388 something wrong with the archive. */
389 if (element
->archive_pass
!= 0)
391 bfd_set_error (bfd_error_bad_value
);
394 element
->archive_pass
= 1;
396 undefs_tail
= info
->hash
->undefs_tail
;
398 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
401 if (! elf_link_add_object_symbols (element
, info
))
404 /* If there are any new undefined symbols, we need to make
405 another pass through the archive in order to see whether
406 they can be defined. FIXME: This isn't perfect, because
407 common symbols wind up on undefs_tail and because an
408 undefined symbol which is defined later on in this pass
409 does not require another pass. This isn't a bug, but it
410 does make the code less efficient than it could be. */
411 if (undefs_tail
!= info
->hash
->undefs_tail
)
414 /* Look backward to mark all symbols from this object file
415 which we have already seen in this pass. */
419 included
[mark
] = true;
424 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
426 /* We mark subsequent symbols from this object file as we go
427 on through the loop. */
428 last
= symdef
->file_offset
;
439 if (defined
!= (boolean
*) NULL
)
441 if (included
!= (boolean
*) NULL
)
446 /* This function is called when we want to define a new symbol. It
447 handles the various cases which arise when we find a definition in
448 a dynamic object, or when there is already a definition in a
449 dynamic object. The new symbol is described by NAME, SYM, PSEC,
450 and PVALUE. We set SYM_HASH to the hash table entry. We set
451 OVERRIDE if the old symbol is overriding a new definition. We set
452 TYPE_CHANGE_OK if it is OK for the type to change. We set
453 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
454 change, we mean that we shouldn't warn if the type or size does
455 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
459 elf_merge_symbol (abfd
, info
, name
, sym
, psec
, pvalue
, sym_hash
,
460 override
, type_change_ok
, size_change_ok
, dt_needed
)
462 struct bfd_link_info
*info
;
464 Elf_Internal_Sym
*sym
;
467 struct elf_link_hash_entry
**sym_hash
;
469 boolean
*type_change_ok
;
470 boolean
*size_change_ok
;
474 struct elf_link_hash_entry
*h
;
477 boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
482 bind
= ELF_ST_BIND (sym
->st_info
);
484 if (! bfd_is_und_section (sec
))
485 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, false, false);
487 h
= ((struct elf_link_hash_entry
*)
488 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true, false, false));
493 /* This code is for coping with dynamic objects, and is only useful
494 if we are doing an ELF link. */
495 if (info
->hash
->creator
!= abfd
->xvec
)
498 /* For merging, we only care about real symbols. */
500 while (h
->root
.type
== bfd_link_hash_indirect
501 || h
->root
.type
== bfd_link_hash_warning
)
502 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
504 /* If we just created the symbol, mark it as being an ELF symbol.
505 Other than that, there is nothing to do--there is no merge issue
506 with a newly defined symbol--so we just return. */
508 if (h
->root
.type
== bfd_link_hash_new
)
510 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
514 /* OLDBFD is a BFD associated with the existing symbol. */
516 switch (h
->root
.type
)
522 case bfd_link_hash_undefined
:
523 case bfd_link_hash_undefweak
:
524 oldbfd
= h
->root
.u
.undef
.abfd
;
527 case bfd_link_hash_defined
:
528 case bfd_link_hash_defweak
:
529 oldbfd
= h
->root
.u
.def
.section
->owner
;
532 case bfd_link_hash_common
:
533 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
537 /* In cases involving weak versioned symbols, we may wind up trying
538 to merge a symbol with itself. Catch that here, to avoid the
539 confusion that results if we try to override a symbol with
540 itself. The additional tests catch cases like
541 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
542 dynamic object, which we do want to handle here. */
544 && ((abfd
->flags
& DYNAMIC
) == 0
545 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0))
548 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
549 respectively, is from a dynamic object. */
551 if ((abfd
->flags
& DYNAMIC
) != 0)
557 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
562 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
563 indices used by MIPS ELF. */
564 switch (h
->root
.type
)
570 case bfd_link_hash_defined
:
571 case bfd_link_hash_defweak
:
572 hsec
= h
->root
.u
.def
.section
;
575 case bfd_link_hash_common
:
576 hsec
= h
->root
.u
.c
.p
->section
;
583 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
586 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
587 respectively, appear to be a definition rather than reference. */
589 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
594 if (h
->root
.type
== bfd_link_hash_undefined
595 || h
->root
.type
== bfd_link_hash_undefweak
596 || h
->root
.type
== bfd_link_hash_common
)
601 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
602 symbol, respectively, appears to be a common symbol in a dynamic
603 object. If a symbol appears in an uninitialized section, and is
604 not weak, and is not a function, then it may be a common symbol
605 which was resolved when the dynamic object was created. We want
606 to treat such symbols specially, because they raise special
607 considerations when setting the symbol size: if the symbol
608 appears as a common symbol in a regular object, and the size in
609 the regular object is larger, we must make sure that we use the
610 larger size. This problematic case can always be avoided in C,
611 but it must be handled correctly when using Fortran shared
614 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
615 likewise for OLDDYNCOMMON and OLDDEF.
617 Note that this test is just a heuristic, and that it is quite
618 possible to have an uninitialized symbol in a shared object which
619 is really a definition, rather than a common symbol. This could
620 lead to some minor confusion when the symbol really is a common
621 symbol in some regular object. However, I think it will be
626 && (sec
->flags
& SEC_ALLOC
) != 0
627 && (sec
->flags
& SEC_LOAD
) == 0
630 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
633 newdyncommon
= false;
637 && h
->root
.type
== bfd_link_hash_defined
638 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
639 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
640 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
642 && h
->type
!= STT_FUNC
)
645 olddyncommon
= false;
647 /* It's OK to change the type if either the existing symbol or the
648 new symbol is weak unless it comes from a DT_NEEDED entry of
649 a shared object, in which case, the DT_NEEDED entry may not be
650 required at the run time. */
652 if ((! dt_needed
&& h
->root
.type
== bfd_link_hash_defweak
)
653 || h
->root
.type
== bfd_link_hash_undefweak
655 *type_change_ok
= true;
657 /* It's OK to change the size if either the existing symbol or the
658 new symbol is weak, or if the old symbol is undefined. */
661 || h
->root
.type
== bfd_link_hash_undefined
)
662 *size_change_ok
= true;
664 /* If both the old and the new symbols look like common symbols in a
665 dynamic object, set the size of the symbol to the larger of the
670 && sym
->st_size
!= h
->size
)
672 /* Since we think we have two common symbols, issue a multiple
673 common warning if desired. Note that we only warn if the
674 size is different. If the size is the same, we simply let
675 the old symbol override the new one as normally happens with
676 symbols defined in dynamic objects. */
678 if (! ((*info
->callbacks
->multiple_common
)
679 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
680 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
683 if (sym
->st_size
> h
->size
)
684 h
->size
= sym
->st_size
;
686 *size_change_ok
= true;
689 /* If we are looking at a dynamic object, and we have found a
690 definition, we need to see if the symbol was already defined by
691 some other object. If so, we want to use the existing
692 definition, and we do not want to report a multiple symbol
693 definition error; we do this by clobbering *PSEC to be
696 We treat a common symbol as a definition if the symbol in the
697 shared library is a function, since common symbols always
698 represent variables; this can cause confusion in principle, but
699 any such confusion would seem to indicate an erroneous program or
700 shared library. We also permit a common symbol in a regular
701 object to override a weak symbol in a shared object.
703 We prefer a non-weak definition in a shared library to a weak
704 definition in the executable unless it comes from a DT_NEEDED
705 entry of a shared object, in which case, the DT_NEEDED entry
706 may not be required at the run time. */
711 || (h
->root
.type
== bfd_link_hash_common
713 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)))
714 && (h
->root
.type
!= bfd_link_hash_defweak
716 || bind
== STB_WEAK
))
720 newdyncommon
= false;
722 *psec
= sec
= bfd_und_section_ptr
;
723 *size_change_ok
= true;
725 /* If we get here when the old symbol is a common symbol, then
726 we are explicitly letting it override a weak symbol or
727 function in a dynamic object, and we don't want to warn about
728 a type change. If the old symbol is a defined symbol, a type
729 change warning may still be appropriate. */
731 if (h
->root
.type
== bfd_link_hash_common
)
732 *type_change_ok
= true;
735 /* Handle the special case of an old common symbol merging with a
736 new symbol which looks like a common symbol in a shared object.
737 We change *PSEC and *PVALUE to make the new symbol look like a
738 common symbol, and let _bfd_generic_link_add_one_symbol will do
742 && h
->root
.type
== bfd_link_hash_common
)
746 newdyncommon
= false;
747 *pvalue
= sym
->st_size
;
748 *psec
= sec
= bfd_com_section_ptr
;
749 *size_change_ok
= true;
752 /* If the old symbol is from a dynamic object, and the new symbol is
753 a definition which is not from a dynamic object, then the new
754 symbol overrides the old symbol. Symbols from regular files
755 always take precedence over symbols from dynamic objects, even if
756 they are defined after the dynamic object in the link.
758 As above, we again permit a common symbol in a regular object to
759 override a definition in a shared object if the shared object
760 symbol is a function or is weak.
762 As above, we permit a non-weak definition in a shared object to
763 override a weak definition in a regular object. */
767 || (bfd_is_com_section (sec
)
768 && (h
->root
.type
== bfd_link_hash_defweak
769 || h
->type
== STT_FUNC
)))
772 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
774 || h
->root
.type
== bfd_link_hash_defweak
))
776 /* Change the hash table entry to undefined, and let
777 _bfd_generic_link_add_one_symbol do the right thing with the
780 h
->root
.type
= bfd_link_hash_undefined
;
781 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
782 *size_change_ok
= true;
785 olddyncommon
= false;
787 /* We again permit a type change when a common symbol may be
788 overriding a function. */
790 if (bfd_is_com_section (sec
))
791 *type_change_ok
= true;
793 /* This union may have been set to be non-NULL when this symbol
794 was seen in a dynamic object. We must force the union to be
795 NULL, so that it is correct for a regular symbol. */
797 h
->verinfo
.vertree
= NULL
;
799 /* In this special case, if H is the target of an indirection,
800 we want the caller to frob with H rather than with the
801 indirect symbol. That will permit the caller to redefine the
802 target of the indirection, rather than the indirect symbol
803 itself. FIXME: This will break the -y option if we store a
804 symbol with a different name. */
808 /* Handle the special case of a new common symbol merging with an
809 old symbol that looks like it might be a common symbol defined in
810 a shared object. Note that we have already handled the case in
811 which a new common symbol should simply override the definition
812 in the shared library. */
815 && bfd_is_com_section (sec
)
818 /* It would be best if we could set the hash table entry to a
819 common symbol, but we don't know what to use for the section
821 if (! ((*info
->callbacks
->multiple_common
)
822 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
823 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
826 /* If the predumed common symbol in the dynamic object is
827 larger, pretend that the new symbol has its size. */
829 if (h
->size
> *pvalue
)
832 /* FIXME: We no longer know the alignment required by the symbol
833 in the dynamic object, so we just wind up using the one from
834 the regular object. */
837 olddyncommon
= false;
839 h
->root
.type
= bfd_link_hash_undefined
;
840 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
842 *size_change_ok
= true;
843 *type_change_ok
= true;
845 h
->verinfo
.vertree
= NULL
;
848 /* Handle the special case of a weak definition in a regular object
849 followed by a non-weak definition in a shared object. In this
850 case, we prefer the definition in the shared object unless it
851 comes from a DT_NEEDED entry of a shared object, in which case,
852 the DT_NEEDED entry may not be required at the run time. */
855 && h
->root
.type
== bfd_link_hash_defweak
860 /* To make this work we have to frob the flags so that the rest
861 of the code does not think we are using the regular
863 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
864 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
865 else if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
866 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
867 h
->elf_link_hash_flags
&= ~ (ELF_LINK_HASH_DEF_REGULAR
868 | ELF_LINK_HASH_DEF_DYNAMIC
);
870 /* If H is the target of an indirection, we want the caller to
871 use H rather than the indirect symbol. Otherwise if we are
872 defining a new indirect symbol we will wind up attaching it
873 to the entry we are overriding. */
877 /* Handle the special case of a non-weak definition in a shared
878 object followed by a weak definition in a regular object. In
879 this case we prefer to definition in the shared object. To make
880 this work we have to tell the caller to not treat the new symbol
884 && h
->root
.type
!= bfd_link_hash_defweak
893 /* Add symbols from an ELF object file to the linker hash table. */
896 elf_link_add_object_symbols (abfd
, info
)
898 struct bfd_link_info
*info
;
900 boolean (*add_symbol_hook
) PARAMS ((bfd
*, struct bfd_link_info
*,
901 const Elf_Internal_Sym
*,
902 const char **, flagword
*,
903 asection
**, bfd_vma
*));
904 boolean (*check_relocs
) PARAMS ((bfd
*, struct bfd_link_info
*,
905 asection
*, const Elf_Internal_Rela
*));
907 Elf_Internal_Shdr
*hdr
;
908 bfd_size_type symcount
;
909 bfd_size_type extsymcount
;
910 bfd_size_type extsymoff
;
911 Elf_External_Sym
*buf
= NULL
;
912 struct elf_link_hash_entry
**sym_hash
;
914 Elf_External_Versym
*extversym
= NULL
;
915 Elf_External_Versym
*ever
;
916 Elf_External_Dyn
*dynbuf
= NULL
;
917 struct elf_link_hash_entry
*weaks
;
918 Elf_External_Sym
*esym
;
919 Elf_External_Sym
*esymend
;
920 struct elf_backend_data
*bed
;
922 struct elf_link_hash_table
* hash_table
;
926 hash_table
= elf_hash_table (info
);
928 bed
= get_elf_backend_data (abfd
);
929 add_symbol_hook
= bed
->elf_add_symbol_hook
;
930 collect
= bed
->collect
;
932 if ((abfd
->flags
& DYNAMIC
) == 0)
938 /* You can't use -r against a dynamic object. Also, there's no
939 hope of using a dynamic object which does not exactly match
940 the format of the output file. */
941 if (info
->relocateable
|| info
->hash
->creator
!= abfd
->xvec
)
943 bfd_set_error (bfd_error_invalid_operation
);
948 /* As a GNU extension, any input sections which are named
949 .gnu.warning.SYMBOL are treated as warning symbols for the given
950 symbol. This differs from .gnu.warning sections, which generate
951 warnings when they are included in an output file. */
956 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
960 name
= bfd_get_section_name (abfd
, s
);
961 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
966 name
+= sizeof ".gnu.warning." - 1;
968 /* If this is a shared object, then look up the symbol
969 in the hash table. If it is there, and it is already
970 been defined, then we will not be using the entry
971 from this shared object, so we don't need to warn.
972 FIXME: If we see the definition in a regular object
973 later on, we will warn, but we shouldn't. The only
974 fix is to keep track of what warnings we are supposed
975 to emit, and then handle them all at the end of the
977 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
979 struct elf_link_hash_entry
*h
;
981 h
= elf_link_hash_lookup (hash_table
, name
,
984 /* FIXME: What about bfd_link_hash_common? */
986 && (h
->root
.type
== bfd_link_hash_defined
987 || h
->root
.type
== bfd_link_hash_defweak
))
989 /* We don't want to issue this warning. Clobber
990 the section size so that the warning does not
991 get copied into the output file. */
997 sz
= bfd_section_size (abfd
, s
);
998 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
1002 if (! bfd_get_section_contents (abfd
, s
, msg
, (file_ptr
) 0, sz
))
1007 if (! (_bfd_generic_link_add_one_symbol
1008 (info
, abfd
, name
, BSF_WARNING
, s
, (bfd_vma
) 0, msg
,
1009 false, collect
, (struct bfd_link_hash_entry
**) NULL
)))
1012 if (! info
->relocateable
)
1014 /* Clobber the section size so that the warning does
1015 not get copied into the output file. */
1022 /* If this is a dynamic object, we always link against the .dynsym
1023 symbol table, not the .symtab symbol table. The dynamic linker
1024 will only see the .dynsym symbol table, so there is no reason to
1025 look at .symtab for a dynamic object. */
1027 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
1028 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1030 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1034 /* Read in any version definitions. */
1036 if (! _bfd_elf_slurp_version_tables (abfd
))
1039 /* Read in the symbol versions, but don't bother to convert them
1040 to internal format. */
1041 if (elf_dynversym (abfd
) != 0)
1043 Elf_Internal_Shdr
*versymhdr
;
1045 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
1046 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
1047 if (extversym
== NULL
)
1049 amt
= versymhdr
->sh_size
;
1050 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
1051 || bfd_bread ((PTR
) extversym
, amt
, abfd
) != amt
)
1056 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
1058 /* The sh_info field of the symtab header tells us where the
1059 external symbols start. We don't care about the local symbols at
1061 if (elf_bad_symtab (abfd
))
1063 extsymcount
= symcount
;
1068 extsymcount
= symcount
- hdr
->sh_info
;
1069 extsymoff
= hdr
->sh_info
;
1072 amt
= extsymcount
* sizeof (Elf_External_Sym
);
1073 buf
= (Elf_External_Sym
*) bfd_malloc (amt
);
1074 if (buf
== NULL
&& extsymcount
!= 0)
1077 /* We store a pointer to the hash table entry for each external
1079 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
1080 sym_hash
= (struct elf_link_hash_entry
**) bfd_alloc (abfd
, amt
);
1081 if (sym_hash
== NULL
)
1083 elf_sym_hashes (abfd
) = sym_hash
;
1089 /* If we are creating a shared library, create all the dynamic
1090 sections immediately. We need to attach them to something,
1091 so we attach them to this BFD, provided it is the right
1092 format. FIXME: If there are no input BFD's of the same
1093 format as the output, we can't make a shared library. */
1095 && is_elf_hash_table (info
)
1096 && ! hash_table
->dynamic_sections_created
1097 && abfd
->xvec
== info
->hash
->creator
)
1099 if (! elf_link_create_dynamic_sections (abfd
, info
))
1103 else if (! is_elf_hash_table (info
))
1110 bfd_size_type oldsize
;
1111 bfd_size_type strindex
;
1113 /* Find the name to use in a DT_NEEDED entry that refers to this
1114 object. If the object has a DT_SONAME entry, we use it.
1115 Otherwise, if the generic linker stuck something in
1116 elf_dt_name, we use that. Otherwise, we just use the file
1117 name. If the generic linker put a null string into
1118 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1119 there is a DT_SONAME entry. */
1121 name
= bfd_get_filename (abfd
);
1122 if (elf_dt_name (abfd
) != NULL
)
1124 name
= elf_dt_name (abfd
);
1127 if (elf_dt_soname (abfd
) != NULL
)
1133 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1136 Elf_External_Dyn
*extdyn
;
1137 Elf_External_Dyn
*extdynend
;
1139 unsigned long shlink
;
1143 dynbuf
= (Elf_External_Dyn
*) bfd_malloc (s
->_raw_size
);
1147 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
1148 (file_ptr
) 0, s
->_raw_size
))
1151 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1154 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1157 /* The shared libraries distributed with hpux11 have a bogus
1158 sh_link field for the ".dynamic" section. This code detects
1159 when SHLINK refers to a section that is not a string table
1160 and tries to find the string table for the ".dynsym" section
1162 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[shlink
];
1163 if (shdr
->sh_type
!= SHT_STRTAB
)
1165 asection
*ds
= bfd_get_section_by_name (abfd
, ".dynsym");
1166 int elfdsec
= _bfd_elf_section_from_bfd_section (abfd
, ds
);
1169 shlink
= elf_elfsections (abfd
)[elfdsec
]->sh_link
;
1174 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
1177 for (; extdyn
< extdynend
; extdyn
++)
1179 Elf_Internal_Dyn dyn
;
1181 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
1182 if (dyn
.d_tag
== DT_SONAME
)
1184 unsigned int tagv
= dyn
.d_un
.d_val
;
1185 name
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1189 if (dyn
.d_tag
== DT_NEEDED
)
1191 struct bfd_link_needed_list
*n
, **pn
;
1193 unsigned int tagv
= dyn
.d_un
.d_val
;
1195 amt
= sizeof (struct bfd_link_needed_list
);
1196 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1197 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1198 if (n
== NULL
|| fnm
== NULL
)
1200 anm
= bfd_alloc (abfd
, (bfd_size_type
) strlen (fnm
) + 1);
1207 for (pn
= & hash_table
->needed
;
1213 if (dyn
.d_tag
== DT_RUNPATH
)
1215 struct bfd_link_needed_list
*n
, **pn
;
1217 unsigned int tagv
= dyn
.d_un
.d_val
;
1219 /* When we see DT_RPATH before DT_RUNPATH, we have
1220 to clear runpath. Do _NOT_ bfd_release, as that
1221 frees all more recently bfd_alloc'd blocks as
1223 if (rpath
&& hash_table
->runpath
)
1224 hash_table
->runpath
= NULL
;
1226 amt
= sizeof (struct bfd_link_needed_list
);
1227 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1228 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1229 if (n
== NULL
|| fnm
== NULL
)
1231 anm
= bfd_alloc (abfd
, (bfd_size_type
) strlen (fnm
) + 1);
1238 for (pn
= & hash_table
->runpath
;
1246 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1247 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
1249 struct bfd_link_needed_list
*n
, **pn
;
1251 unsigned int tagv
= dyn
.d_un
.d_val
;
1253 amt
= sizeof (struct bfd_link_needed_list
);
1254 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1255 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1256 if (n
== NULL
|| fnm
== NULL
)
1258 anm
= bfd_alloc (abfd
, (bfd_size_type
) strlen (fnm
) + 1);
1265 for (pn
= & hash_table
->runpath
;
1278 /* We do not want to include any of the sections in a dynamic
1279 object in the output file. We hack by simply clobbering the
1280 list of sections in the BFD. This could be handled more
1281 cleanly by, say, a new section flag; the existing
1282 SEC_NEVER_LOAD flag is not the one we want, because that one
1283 still implies that the section takes up space in the output
1285 abfd
->sections
= NULL
;
1286 abfd
->section_count
= 0;
1288 /* If this is the first dynamic object found in the link, create
1289 the special sections required for dynamic linking. */
1290 if (! hash_table
->dynamic_sections_created
)
1291 if (! elf_link_create_dynamic_sections (abfd
, info
))
1296 /* Add a DT_NEEDED entry for this dynamic object. */
1297 oldsize
= _bfd_stringtab_size (hash_table
->dynstr
);
1298 strindex
= _bfd_stringtab_add (hash_table
->dynstr
, name
,
1300 if (strindex
== (bfd_size_type
) -1)
1303 if (oldsize
== _bfd_stringtab_size (hash_table
->dynstr
))
1306 Elf_External_Dyn
*dyncon
, *dynconend
;
1308 /* The hash table size did not change, which means that
1309 the dynamic object name was already entered. If we
1310 have already included this dynamic object in the
1311 link, just ignore it. There is no reason to include
1312 a particular dynamic object more than once. */
1313 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
1314 BFD_ASSERT (sdyn
!= NULL
);
1316 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1317 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1319 for (; dyncon
< dynconend
; dyncon
++)
1321 Elf_Internal_Dyn dyn
;
1323 elf_swap_dyn_in (hash_table
->dynobj
, dyncon
, & dyn
);
1324 if (dyn
.d_tag
== DT_NEEDED
1325 && dyn
.d_un
.d_val
== strindex
)
1329 if (extversym
!= NULL
)
1336 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
1340 /* Save the SONAME, if there is one, because sometimes the
1341 linker emulation code will need to know it. */
1343 name
= basename (bfd_get_filename (abfd
));
1344 elf_dt_name (abfd
) = name
;
1347 pos
= hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
);
1348 amt
= extsymcount
* sizeof (Elf_External_Sym
);
1349 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1350 || bfd_bread ((PTR
) buf
, amt
, abfd
) != amt
)
1355 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
1356 esymend
= buf
+ extsymcount
;
1359 esym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
1361 Elf_Internal_Sym sym
;
1367 struct elf_link_hash_entry
*h
;
1369 boolean size_change_ok
, type_change_ok
;
1370 boolean new_weakdef
;
1371 unsigned int old_alignment
;
1373 elf_swap_symbol_in (abfd
, esym
, &sym
);
1375 flags
= BSF_NO_FLAGS
;
1377 value
= sym
.st_value
;
1380 bind
= ELF_ST_BIND (sym
.st_info
);
1381 if (bind
== STB_LOCAL
)
1383 /* This should be impossible, since ELF requires that all
1384 global symbols follow all local symbols, and that sh_info
1385 point to the first global symbol. Unfortunatealy, Irix 5
1389 else if (bind
== STB_GLOBAL
)
1391 if (sym
.st_shndx
!= SHN_UNDEF
1392 && sym
.st_shndx
!= SHN_COMMON
)
1395 else if (bind
== STB_WEAK
)
1399 /* Leave it up to the processor backend. */
1402 if (sym
.st_shndx
== SHN_UNDEF
)
1403 sec
= bfd_und_section_ptr
;
1404 else if (sym
.st_shndx
> 0 && sym
.st_shndx
< SHN_LORESERVE
)
1406 sec
= section_from_elf_index (abfd
, sym
.st_shndx
);
1408 sec
= bfd_abs_section_ptr
;
1409 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
1412 else if (sym
.st_shndx
== SHN_ABS
)
1413 sec
= bfd_abs_section_ptr
;
1414 else if (sym
.st_shndx
== SHN_COMMON
)
1416 sec
= bfd_com_section_ptr
;
1417 /* What ELF calls the size we call the value. What ELF
1418 calls the value we call the alignment. */
1419 value
= sym
.st_size
;
1423 /* Leave it up to the processor backend. */
1426 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
1427 if (name
== (const char *) NULL
)
1430 if (add_symbol_hook
)
1432 if (! (*add_symbol_hook
) (abfd
, info
, &sym
, &name
, &flags
, &sec
,
1436 /* The hook function sets the name to NULL if this symbol
1437 should be skipped for some reason. */
1438 if (name
== (const char *) NULL
)
1442 /* Sanity check that all possibilities were handled. */
1443 if (sec
== (asection
*) NULL
)
1445 bfd_set_error (bfd_error_bad_value
);
1449 if (bfd_is_und_section (sec
)
1450 || bfd_is_com_section (sec
))
1455 size_change_ok
= false;
1456 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
1458 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1460 Elf_Internal_Versym iver
;
1461 unsigned int vernum
= 0;
1466 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
1467 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
1469 /* If this is a hidden symbol, or if it is not version
1470 1, we append the version name to the symbol name.
1471 However, we do not modify a non-hidden absolute
1472 symbol, because it might be the version symbol
1473 itself. FIXME: What if it isn't? */
1474 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
1475 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
1478 unsigned int namelen
;
1479 bfd_size_type newlen
;
1482 if (sym
.st_shndx
!= SHN_UNDEF
)
1484 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
1486 (*_bfd_error_handler
)
1487 (_("%s: %s: invalid version %u (max %d)"),
1488 bfd_archive_filename (abfd
), name
, vernum
,
1489 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1490 bfd_set_error (bfd_error_bad_value
);
1493 else if (vernum
> 1)
1495 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1501 /* We cannot simply test for the number of
1502 entries in the VERNEED section since the
1503 numbers for the needed versions do not start
1505 Elf_Internal_Verneed
*t
;
1508 for (t
= elf_tdata (abfd
)->verref
;
1512 Elf_Internal_Vernaux
*a
;
1514 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1516 if (a
->vna_other
== vernum
)
1518 verstr
= a
->vna_nodename
;
1527 (*_bfd_error_handler
)
1528 (_("%s: %s: invalid needed version %d"),
1529 bfd_archive_filename (abfd
), name
, vernum
);
1530 bfd_set_error (bfd_error_bad_value
);
1535 namelen
= strlen (name
);
1536 newlen
= namelen
+ strlen (verstr
) + 2;
1537 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
1540 newname
= (char *) bfd_alloc (abfd
, newlen
);
1541 if (newname
== NULL
)
1543 strcpy (newname
, name
);
1544 p
= newname
+ namelen
;
1546 /* If this is a defined non-hidden version symbol,
1547 we add another @ to the name. This indicates the
1548 default version of the symbol. */
1549 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1550 && sym
.st_shndx
!= SHN_UNDEF
)
1558 if (! elf_merge_symbol (abfd
, info
, name
, &sym
, &sec
, &value
,
1559 sym_hash
, &override
, &type_change_ok
,
1560 &size_change_ok
, dt_needed
))
1567 while (h
->root
.type
== bfd_link_hash_indirect
1568 || h
->root
.type
== bfd_link_hash_warning
)
1569 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1571 /* Remember the old alignment if this is a common symbol, so
1572 that we don't reduce the alignment later on. We can't
1573 check later, because _bfd_generic_link_add_one_symbol
1574 will set a default for the alignment which we want to
1576 if (h
->root
.type
== bfd_link_hash_common
)
1577 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1579 if (elf_tdata (abfd
)->verdef
!= NULL
1583 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1586 if (! (_bfd_generic_link_add_one_symbol
1587 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
1588 false, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
1592 while (h
->root
.type
== bfd_link_hash_indirect
1593 || h
->root
.type
== bfd_link_hash_warning
)
1594 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1597 new_weakdef
= false;
1600 && (flags
& BSF_WEAK
) != 0
1601 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
1602 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1603 && h
->weakdef
== NULL
)
1605 /* Keep a list of all weak defined non function symbols from
1606 a dynamic object, using the weakdef field. Later in this
1607 function we will set the weakdef field to the correct
1608 value. We only put non-function symbols from dynamic
1609 objects on this list, because that happens to be the only
1610 time we need to know the normal symbol corresponding to a
1611 weak symbol, and the information is time consuming to
1612 figure out. If the weakdef field is not already NULL,
1613 then this symbol was already defined by some previous
1614 dynamic object, and we will be using that previous
1615 definition anyhow. */
1622 /* Set the alignment of a common symbol. */
1623 if (sym
.st_shndx
== SHN_COMMON
1624 && h
->root
.type
== bfd_link_hash_common
)
1628 align
= bfd_log2 (sym
.st_value
);
1629 if (align
> old_alignment
1630 /* Permit an alignment power of zero if an alignment of one
1631 is specified and no other alignments have been specified. */
1632 || (sym
.st_value
== 1 && old_alignment
== 0))
1633 h
->root
.u
.c
.p
->alignment_power
= align
;
1636 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1642 /* Remember the symbol size and type. */
1643 if (sym
.st_size
!= 0
1644 && (definition
|| h
->size
== 0))
1646 if (h
->size
!= 0 && h
->size
!= sym
.st_size
&& ! size_change_ok
)
1647 (*_bfd_error_handler
)
1648 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1649 name
, (unsigned long) h
->size
, (unsigned long) sym
.st_size
,
1650 bfd_archive_filename (abfd
));
1652 h
->size
= sym
.st_size
;
1655 /* If this is a common symbol, then we always want H->SIZE
1656 to be the size of the common symbol. The code just above
1657 won't fix the size if a common symbol becomes larger. We
1658 don't warn about a size change here, because that is
1659 covered by --warn-common. */
1660 if (h
->root
.type
== bfd_link_hash_common
)
1661 h
->size
= h
->root
.u
.c
.size
;
1663 if (ELF_ST_TYPE (sym
.st_info
) != STT_NOTYPE
1664 && (definition
|| h
->type
== STT_NOTYPE
))
1666 if (h
->type
!= STT_NOTYPE
1667 && h
->type
!= ELF_ST_TYPE (sym
.st_info
)
1668 && ! type_change_ok
)
1669 (*_bfd_error_handler
)
1670 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1671 name
, h
->type
, ELF_ST_TYPE (sym
.st_info
),
1672 bfd_archive_filename (abfd
));
1674 h
->type
= ELF_ST_TYPE (sym
.st_info
);
1677 /* If st_other has a processor-specific meaning, specific code
1678 might be needed here. */
1679 if (sym
.st_other
!= 0)
1681 /* Combine visibilities, using the most constraining one. */
1682 unsigned char hvis
= ELF_ST_VISIBILITY (h
->other
);
1683 unsigned char symvis
= ELF_ST_VISIBILITY (sym
.st_other
);
1685 if (symvis
&& (hvis
> symvis
|| hvis
== 0))
1686 h
->other
= sym
.st_other
;
1688 /* If neither has visibility, use the st_other of the
1689 definition. This is an arbitrary choice, since the
1690 other bits have no general meaning. */
1691 if (!symvis
&& !hvis
1692 && (definition
|| h
->other
== 0))
1693 h
->other
= sym
.st_other
;
1696 /* Set a flag in the hash table entry indicating the type of
1697 reference or definition we just found. Keep a count of
1698 the number of dynamic symbols we find. A dynamic symbol
1699 is one which is referenced or defined by both a regular
1700 object and a shared object. */
1701 old_flags
= h
->elf_link_hash_flags
;
1707 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
1708 if (bind
!= STB_WEAK
)
1709 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
1712 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
1714 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1715 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
1721 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
1723 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
1724 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
1725 | ELF_LINK_HASH_REF_REGULAR
)) != 0
1726 || (h
->weakdef
!= NULL
1728 && h
->weakdef
->dynindx
!= -1))
1732 h
->elf_link_hash_flags
|= new_flag
;
1734 /* If this symbol has a version, and it is the default
1735 version, we create an indirect symbol from the default
1736 name to the fully decorated name. This will cause
1737 external references which do not specify a version to be
1738 bound to this version of the symbol. */
1739 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
1743 p
= strchr (name
, ELF_VER_CHR
);
1744 if (p
!= NULL
&& p
[1] == ELF_VER_CHR
)
1747 struct elf_link_hash_entry
*hi
;
1750 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1751 (size_t) (p
- name
+ 1));
1752 if (shortname
== NULL
)
1754 strncpy (shortname
, name
, (size_t) (p
- name
));
1755 shortname
[p
- name
] = '\0';
1757 /* We are going to create a new symbol. Merge it
1758 with any existing symbol with this name. For the
1759 purposes of the merge, act as though we were
1760 defining the symbol we just defined, although we
1761 actually going to define an indirect symbol. */
1762 type_change_ok
= false;
1763 size_change_ok
= false;
1764 if (! elf_merge_symbol (abfd
, info
, shortname
, &sym
, &sec
,
1765 &value
, &hi
, &override
,
1767 &size_change_ok
, dt_needed
))
1772 if (! (_bfd_generic_link_add_one_symbol
1773 (info
, abfd
, shortname
, BSF_INDIRECT
,
1774 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1775 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1780 /* In this case the symbol named SHORTNAME is
1781 overriding the indirect symbol we want to
1782 add. We were planning on making SHORTNAME an
1783 indirect symbol referring to NAME. SHORTNAME
1784 is the name without a version. NAME is the
1785 fully versioned name, and it is the default
1788 Overriding means that we already saw a
1789 definition for the symbol SHORTNAME in a
1790 regular object, and it is overriding the
1791 symbol defined in the dynamic object.
1793 When this happens, we actually want to change
1794 NAME, the symbol we just added, to refer to
1795 SHORTNAME. This will cause references to
1796 NAME in the shared object to become
1797 references to SHORTNAME in the regular
1798 object. This is what we expect when we
1799 override a function in a shared object: that
1800 the references in the shared object will be
1801 mapped to the definition in the regular
1804 while (hi
->root
.type
== bfd_link_hash_indirect
1805 || hi
->root
.type
== bfd_link_hash_warning
)
1806 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1808 h
->root
.type
= bfd_link_hash_indirect
;
1809 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1810 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1812 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1813 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1814 if (hi
->elf_link_hash_flags
1815 & (ELF_LINK_HASH_REF_REGULAR
1816 | ELF_LINK_HASH_DEF_REGULAR
))
1818 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1824 /* Now set HI to H, so that the following code
1825 will set the other fields correctly. */
1829 /* If there is a duplicate definition somewhere,
1830 then HI may not point to an indirect symbol. We
1831 will have reported an error to the user in that
1834 if (hi
->root
.type
== bfd_link_hash_indirect
)
1836 struct elf_link_hash_entry
*ht
;
1838 /* If the symbol became indirect, then we assume
1839 that we have not seen a definition before. */
1840 BFD_ASSERT ((hi
->elf_link_hash_flags
1841 & (ELF_LINK_HASH_DEF_DYNAMIC
1842 | ELF_LINK_HASH_DEF_REGULAR
))
1845 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1846 (*bed
->elf_backend_copy_indirect_symbol
) (ht
, hi
);
1848 /* See if the new flags lead us to realize that
1849 the symbol must be dynamic. */
1855 || ((hi
->elf_link_hash_flags
1856 & ELF_LINK_HASH_REF_DYNAMIC
)
1862 if ((hi
->elf_link_hash_flags
1863 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1869 /* We also need to define an indirection from the
1870 nondefault version of the symbol. */
1872 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1874 if (shortname
== NULL
)
1876 strncpy (shortname
, name
, (size_t) (p
- name
));
1877 strcpy (shortname
+ (p
- name
), p
+ 1);
1879 /* Once again, merge with any existing symbol. */
1880 type_change_ok
= false;
1881 size_change_ok
= false;
1882 if (! elf_merge_symbol (abfd
, info
, shortname
, &sym
, &sec
,
1883 &value
, &hi
, &override
,
1885 &size_change_ok
, dt_needed
))
1890 /* Here SHORTNAME is a versioned name, so we
1891 don't expect to see the type of override we
1892 do in the case above. */
1893 (*_bfd_error_handler
)
1894 (_("%s: warning: unexpected redefinition of `%s'"),
1895 bfd_archive_filename (abfd
), shortname
);
1899 if (! (_bfd_generic_link_add_one_symbol
1900 (info
, abfd
, shortname
, BSF_INDIRECT
,
1901 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1902 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1905 /* If there is a duplicate definition somewhere,
1906 then HI may not point to an indirect symbol.
1907 We will have reported an error to the user in
1910 if (hi
->root
.type
== bfd_link_hash_indirect
)
1912 /* If the symbol became indirect, then we
1913 assume that we have not seen a definition
1915 BFD_ASSERT ((hi
->elf_link_hash_flags
1916 & (ELF_LINK_HASH_DEF_DYNAMIC
1917 | ELF_LINK_HASH_DEF_REGULAR
))
1920 (*bed
->elf_backend_copy_indirect_symbol
) (h
, hi
);
1922 /* See if the new flags lead us to realize
1923 that the symbol must be dynamic. */
1929 || ((hi
->elf_link_hash_flags
1930 & ELF_LINK_HASH_REF_DYNAMIC
)
1936 if ((hi
->elf_link_hash_flags
1937 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1946 if (dynsym
&& h
->dynindx
== -1)
1948 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1950 if (h
->weakdef
!= NULL
1952 && h
->weakdef
->dynindx
== -1)
1954 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
1958 else if (dynsym
&& h
->dynindx
!= -1)
1959 /* If the symbol already has a dynamic index, but
1960 visibility says it should not be visible, turn it into
1962 switch (ELF_ST_VISIBILITY (h
->other
))
1966 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
1967 (*bed
->elf_backend_hide_symbol
) (info
, h
);
1971 if (dt_needed
&& definition
1972 && (h
->elf_link_hash_flags
1973 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1975 bfd_size_type oldsize
;
1976 bfd_size_type strindex
;
1978 if (! is_elf_hash_table (info
))
1981 /* The symbol from a DT_NEEDED object is referenced from
1982 the regular object to create a dynamic executable. We
1983 have to make sure there is a DT_NEEDED entry for it. */
1986 oldsize
= _bfd_stringtab_size (hash_table
->dynstr
);
1987 strindex
= _bfd_stringtab_add (hash_table
->dynstr
,
1988 elf_dt_soname (abfd
),
1990 if (strindex
== (bfd_size_type
) -1)
1994 == _bfd_stringtab_size (hash_table
->dynstr
))
1997 Elf_External_Dyn
*dyncon
, *dynconend
;
1999 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
,
2001 BFD_ASSERT (sdyn
!= NULL
);
2003 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
2004 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
2006 for (; dyncon
< dynconend
; dyncon
++)
2008 Elf_Internal_Dyn dyn
;
2010 elf_swap_dyn_in (hash_table
->dynobj
,
2012 BFD_ASSERT (dyn
.d_tag
!= DT_NEEDED
||
2013 dyn
.d_un
.d_val
!= strindex
);
2017 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
2023 /* Now set the weakdefs field correctly for all the weak defined
2024 symbols we found. The only way to do this is to search all the
2025 symbols. Since we only need the information for non functions in
2026 dynamic objects, that's the only time we actually put anything on
2027 the list WEAKS. We need this information so that if a regular
2028 object refers to a symbol defined weakly in a dynamic object, the
2029 real symbol in the dynamic object is also put in the dynamic
2030 symbols; we also must arrange for both symbols to point to the
2031 same memory location. We could handle the general case of symbol
2032 aliasing, but a general symbol alias can only be generated in
2033 assembler code, handling it correctly would be very time
2034 consuming, and other ELF linkers don't handle general aliasing
2036 while (weaks
!= NULL
)
2038 struct elf_link_hash_entry
*hlook
;
2041 struct elf_link_hash_entry
**hpp
;
2042 struct elf_link_hash_entry
**hppend
;
2045 weaks
= hlook
->weakdef
;
2046 hlook
->weakdef
= NULL
;
2048 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
2049 || hlook
->root
.type
== bfd_link_hash_defweak
2050 || hlook
->root
.type
== bfd_link_hash_common
2051 || hlook
->root
.type
== bfd_link_hash_indirect
);
2052 slook
= hlook
->root
.u
.def
.section
;
2053 vlook
= hlook
->root
.u
.def
.value
;
2055 hpp
= elf_sym_hashes (abfd
);
2056 hppend
= hpp
+ extsymcount
;
2057 for (; hpp
< hppend
; hpp
++)
2059 struct elf_link_hash_entry
*h
;
2062 if (h
!= NULL
&& h
!= hlook
2063 && h
->root
.type
== bfd_link_hash_defined
2064 && h
->root
.u
.def
.section
== slook
2065 && h
->root
.u
.def
.value
== vlook
)
2069 /* If the weak definition is in the list of dynamic
2070 symbols, make sure the real definition is put there
2072 if (hlook
->dynindx
!= -1
2073 && h
->dynindx
== -1)
2075 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2079 /* If the real definition is in the list of dynamic
2080 symbols, make sure the weak definition is put there
2081 as well. If we don't do this, then the dynamic
2082 loader might not merge the entries for the real
2083 definition and the weak definition. */
2084 if (h
->dynindx
!= -1
2085 && hlook
->dynindx
== -1)
2087 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
2102 if (extversym
!= NULL
)
2108 /* If this object is the same format as the output object, and it is
2109 not a shared library, then let the backend look through the
2112 This is required to build global offset table entries and to
2113 arrange for dynamic relocs. It is not required for the
2114 particular common case of linking non PIC code, even when linking
2115 against shared libraries, but unfortunately there is no way of
2116 knowing whether an object file has been compiled PIC or not.
2117 Looking through the relocs is not particularly time consuming.
2118 The problem is that we must either (1) keep the relocs in memory,
2119 which causes the linker to require additional runtime memory or
2120 (2) read the relocs twice from the input file, which wastes time.
2121 This would be a good case for using mmap.
2123 I have no idea how to handle linking PIC code into a file of a
2124 different format. It probably can't be done. */
2125 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
2127 && abfd
->xvec
== info
->hash
->creator
2128 && check_relocs
!= NULL
)
2132 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2134 Elf_Internal_Rela
*internal_relocs
;
2137 if ((o
->flags
& SEC_RELOC
) == 0
2138 || o
->reloc_count
== 0
2139 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
2140 && (o
->flags
& SEC_DEBUGGING
) != 0)
2141 || bfd_is_abs_section (o
->output_section
))
2144 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
2145 (abfd
, o
, (PTR
) NULL
,
2146 (Elf_Internal_Rela
*) NULL
,
2147 info
->keep_memory
));
2148 if (internal_relocs
== NULL
)
2151 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
2153 if (! info
->keep_memory
)
2154 free (internal_relocs
);
2161 /* If this is a non-traditional, non-relocateable link, try to
2162 optimize the handling of the .stab/.stabstr sections. */
2164 && ! info
->relocateable
2165 && ! info
->traditional_format
2166 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
2167 && is_elf_hash_table (info
)
2168 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
2170 asection
*stab
, *stabstr
;
2172 stab
= bfd_get_section_by_name (abfd
, ".stab");
2175 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
2177 if (stabstr
!= NULL
)
2179 struct bfd_elf_section_data
*secdata
;
2181 secdata
= elf_section_data (stab
);
2182 if (! _bfd_link_section_stabs (abfd
,
2183 & hash_table
->stab_info
,
2185 &secdata
->stab_info
))
2191 if (! info
->relocateable
&& ! dynamic
2192 && is_elf_hash_table (info
))
2196 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2197 if ((s
->flags
& SEC_MERGE
)
2198 && ! _bfd_merge_section (abfd
, & hash_table
->merge_info
, s
,
2199 & elf_section_data (s
)->merge_info
))
2210 if (extversym
!= NULL
)
2215 /* Create some sections which will be filled in with dynamic linking
2216 information. ABFD is an input file which requires dynamic sections
2217 to be created. The dynamic sections take up virtual memory space
2218 when the final executable is run, so we need to create them before
2219 addresses are assigned to the output sections. We work out the
2220 actual contents and size of these sections later. */
2223 elf_link_create_dynamic_sections (abfd
, info
)
2225 struct bfd_link_info
*info
;
2228 register asection
*s
;
2229 struct elf_link_hash_entry
*h
;
2230 struct elf_backend_data
*bed
;
2232 if (! is_elf_hash_table (info
))
2235 if (elf_hash_table (info
)->dynamic_sections_created
)
2238 /* Make sure that all dynamic sections use the same input BFD. */
2239 if (elf_hash_table (info
)->dynobj
== NULL
)
2240 elf_hash_table (info
)->dynobj
= abfd
;
2242 abfd
= elf_hash_table (info
)->dynobj
;
2244 /* Note that we set the SEC_IN_MEMORY flag for all of these
2246 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
2247 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
2249 /* A dynamically linked executable has a .interp section, but a
2250 shared library does not. */
2253 s
= bfd_make_section (abfd
, ".interp");
2255 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2259 /* Create sections to hold version informations. These are removed
2260 if they are not needed. */
2261 s
= bfd_make_section (abfd
, ".gnu.version_d");
2263 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2264 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2267 s
= bfd_make_section (abfd
, ".gnu.version");
2269 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2270 || ! bfd_set_section_alignment (abfd
, s
, 1))
2273 s
= bfd_make_section (abfd
, ".gnu.version_r");
2275 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2276 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2279 s
= bfd_make_section (abfd
, ".dynsym");
2281 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2282 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2285 s
= bfd_make_section (abfd
, ".dynstr");
2287 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2290 /* Create a strtab to hold the dynamic symbol names. */
2291 if (elf_hash_table (info
)->dynstr
== NULL
)
2293 elf_hash_table (info
)->dynstr
= elf_stringtab_init ();
2294 if (elf_hash_table (info
)->dynstr
== NULL
)
2298 s
= bfd_make_section (abfd
, ".dynamic");
2300 || ! bfd_set_section_flags (abfd
, s
, flags
)
2301 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2304 /* The special symbol _DYNAMIC is always set to the start of the
2305 .dynamic section. This call occurs before we have processed the
2306 symbols for any dynamic object, so we don't have to worry about
2307 overriding a dynamic definition. We could set _DYNAMIC in a
2308 linker script, but we only want to define it if we are, in fact,
2309 creating a .dynamic section. We don't want to define it if there
2310 is no .dynamic section, since on some ELF platforms the start up
2311 code examines it to decide how to initialize the process. */
2313 if (! (_bfd_generic_link_add_one_symbol
2314 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
2315 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
2316 (struct bfd_link_hash_entry
**) &h
)))
2318 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2319 h
->type
= STT_OBJECT
;
2322 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2325 bed
= get_elf_backend_data (abfd
);
2327 s
= bfd_make_section (abfd
, ".hash");
2329 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2330 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2332 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
2334 /* Let the backend create the rest of the sections. This lets the
2335 backend set the right flags. The backend will normally create
2336 the .got and .plt sections. */
2337 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
2340 elf_hash_table (info
)->dynamic_sections_created
= true;
2345 /* Add an entry to the .dynamic table. */
2348 elf_add_dynamic_entry (info
, tag
, val
)
2349 struct bfd_link_info
*info
;
2353 Elf_Internal_Dyn dyn
;
2356 bfd_size_type newsize
;
2357 bfd_byte
*newcontents
;
2359 if (! is_elf_hash_table (info
))
2362 dynobj
= elf_hash_table (info
)->dynobj
;
2364 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
2365 BFD_ASSERT (s
!= NULL
);
2367 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
2368 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
2369 if (newcontents
== NULL
)
2373 dyn
.d_un
.d_val
= val
;
2374 elf_swap_dyn_out (dynobj
, &dyn
,
2375 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
2377 s
->_raw_size
= newsize
;
2378 s
->contents
= newcontents
;
2383 /* Record a new local dynamic symbol. */
2386 elf_link_record_local_dynamic_symbol (info
, input_bfd
, input_indx
)
2387 struct bfd_link_info
*info
;
2391 struct elf_link_local_dynamic_entry
*entry
;
2392 struct elf_link_hash_table
*eht
;
2393 struct bfd_strtab_hash
*dynstr
;
2394 Elf_External_Sym esym
;
2395 unsigned long dynstr_index
;
2400 if (! is_elf_hash_table (info
))
2403 /* See if the entry exists already. */
2404 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
2405 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
2408 entry
= (struct elf_link_local_dynamic_entry
*)
2409 bfd_alloc (input_bfd
, (bfd_size_type
) sizeof (*entry
));
2413 /* Go find the symbol, so that we can find it's name. */
2414 amt
= sizeof (Elf_External_Sym
);
2415 pos
= elf_tdata (input_bfd
)->symtab_hdr
.sh_offset
+ input_indx
* amt
;
2416 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
2417 || bfd_bread (&esym
, amt
, input_bfd
) != amt
)
2419 elf_swap_symbol_in (input_bfd
, &esym
, &entry
->isym
);
2421 name
= (bfd_elf_string_from_elf_section
2422 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
2423 entry
->isym
.st_name
));
2425 dynstr
= elf_hash_table (info
)->dynstr
;
2428 /* Create a strtab to hold the dynamic symbol names. */
2429 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_stringtab_init ();
2434 dynstr_index
= _bfd_stringtab_add (dynstr
, name
, true, false);
2435 if (dynstr_index
== (unsigned long) -1)
2437 entry
->isym
.st_name
= dynstr_index
;
2439 eht
= elf_hash_table (info
);
2441 entry
->next
= eht
->dynlocal
;
2442 eht
->dynlocal
= entry
;
2443 entry
->input_bfd
= input_bfd
;
2444 entry
->input_indx
= input_indx
;
2447 /* Whatever binding the symbol had before, it's now local. */
2449 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
2451 /* The dynindx will be set at the end of size_dynamic_sections. */
2456 /* Read and swap the relocs from the section indicated by SHDR. This
2457 may be either a REL or a RELA section. The relocations are
2458 translated into RELA relocations and stored in INTERNAL_RELOCS,
2459 which should have already been allocated to contain enough space.
2460 The EXTERNAL_RELOCS are a buffer where the external form of the
2461 relocations should be stored.
2463 Returns false if something goes wrong. */
2466 elf_link_read_relocs_from_section (abfd
, shdr
, external_relocs
,
2469 Elf_Internal_Shdr
*shdr
;
2470 PTR external_relocs
;
2471 Elf_Internal_Rela
*internal_relocs
;
2473 struct elf_backend_data
*bed
;
2476 /* If there aren't any relocations, that's OK. */
2480 /* Position ourselves at the start of the section. */
2481 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2484 /* Read the relocations. */
2485 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2488 bed
= get_elf_backend_data (abfd
);
2490 /* Convert the external relocations to the internal format. */
2491 if (shdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2493 Elf_External_Rel
*erel
;
2494 Elf_External_Rel
*erelend
;
2495 Elf_Internal_Rela
*irela
;
2496 Elf_Internal_Rel
*irel
;
2498 erel
= (Elf_External_Rel
*) external_relocs
;
2499 erelend
= erel
+ NUM_SHDR_ENTRIES (shdr
);
2500 irela
= internal_relocs
;
2501 amt
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
2502 irel
= bfd_alloc (abfd
, amt
);
2503 for (; erel
< erelend
; erel
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2507 if (bed
->s
->swap_reloc_in
)
2508 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
2510 elf_swap_reloc_in (abfd
, erel
, irel
);
2512 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; ++i
)
2514 irela
[i
].r_offset
= irel
[i
].r_offset
;
2515 irela
[i
].r_info
= irel
[i
].r_info
;
2516 irela
[i
].r_addend
= 0;
2522 Elf_External_Rela
*erela
;
2523 Elf_External_Rela
*erelaend
;
2524 Elf_Internal_Rela
*irela
;
2526 BFD_ASSERT (shdr
->sh_entsize
== sizeof (Elf_External_Rela
));
2528 erela
= (Elf_External_Rela
*) external_relocs
;
2529 erelaend
= erela
+ NUM_SHDR_ENTRIES (shdr
);
2530 irela
= internal_relocs
;
2531 for (; erela
< erelaend
; erela
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2533 if (bed
->s
->swap_reloca_in
)
2534 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
2536 elf_swap_reloca_in (abfd
, erela
, irela
);
2543 /* Read and swap the relocs for a section O. They may have been
2544 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2545 not NULL, they are used as buffers to read into. They are known to
2546 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2547 the return value is allocated using either malloc or bfd_alloc,
2548 according to the KEEP_MEMORY argument. If O has two relocation
2549 sections (both REL and RELA relocations), then the REL_HDR
2550 relocations will appear first in INTERNAL_RELOCS, followed by the
2551 REL_HDR2 relocations. */
2554 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
2558 PTR external_relocs
;
2559 Elf_Internal_Rela
*internal_relocs
;
2560 boolean keep_memory
;
2562 Elf_Internal_Shdr
*rel_hdr
;
2564 Elf_Internal_Rela
*alloc2
= NULL
;
2565 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2567 if (elf_section_data (o
)->relocs
!= NULL
)
2568 return elf_section_data (o
)->relocs
;
2570 if (o
->reloc_count
== 0)
2573 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2575 if (internal_relocs
== NULL
)
2579 size
= o
->reloc_count
;
2580 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2582 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2584 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2585 if (internal_relocs
== NULL
)
2589 if (external_relocs
== NULL
)
2591 bfd_size_type size
= rel_hdr
->sh_size
;
2593 if (elf_section_data (o
)->rel_hdr2
)
2594 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2595 alloc1
= (PTR
) bfd_malloc (size
);
2598 external_relocs
= alloc1
;
2601 if (!elf_link_read_relocs_from_section (abfd
, rel_hdr
,
2605 if (!elf_link_read_relocs_from_section
2607 elf_section_data (o
)->rel_hdr2
,
2608 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2609 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2610 * bed
->s
->int_rels_per_ext_rel
)))
2613 /* Cache the results for next time, if we can. */
2615 elf_section_data (o
)->relocs
= internal_relocs
;
2620 /* Don't free alloc2, since if it was allocated we are passing it
2621 back (under the name of internal_relocs). */
2623 return internal_relocs
;
2633 /* Record an assignment to a symbol made by a linker script. We need
2634 this in case some dynamic object refers to this symbol. */
2637 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
2638 bfd
*output_bfd ATTRIBUTE_UNUSED
;
2639 struct bfd_link_info
*info
;
2643 struct elf_link_hash_entry
*h
;
2645 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2648 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, true, false);
2652 if (h
->root
.type
== bfd_link_hash_new
)
2653 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_ELF
;
2655 /* If this symbol is being provided by the linker script, and it is
2656 currently defined by a dynamic object, but not by a regular
2657 object, then mark it as undefined so that the generic linker will
2658 force the correct value. */
2660 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2661 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2662 h
->root
.type
= bfd_link_hash_undefined
;
2664 /* If this symbol is not being provided by the linker script, and it is
2665 currently defined by a dynamic object, but not by a regular object,
2666 then clear out any version information because the symbol will not be
2667 associated with the dynamic object any more. */
2669 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2670 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2671 h
->verinfo
.verdef
= NULL
;
2673 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2675 /* When possible, keep the original type of the symbol. */
2676 if (h
->type
== STT_NOTYPE
)
2677 h
->type
= STT_OBJECT
;
2679 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2680 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
2682 && h
->dynindx
== -1)
2684 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2687 /* If this is a weak defined symbol, and we know a corresponding
2688 real symbol from the same dynamic object, make sure the real
2689 symbol is also made into a dynamic symbol. */
2690 if (h
->weakdef
!= NULL
2691 && h
->weakdef
->dynindx
== -1)
2693 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2701 /* This structure is used to pass information to
2702 elf_link_assign_sym_version. */
2704 struct elf_assign_sym_version_info
2708 /* General link information. */
2709 struct bfd_link_info
*info
;
2711 struct bfd_elf_version_tree
*verdefs
;
2712 /* Whether we had a failure. */
2716 /* This structure is used to pass information to
2717 elf_link_find_version_dependencies. */
2719 struct elf_find_verdep_info
2723 /* General link information. */
2724 struct bfd_link_info
*info
;
2725 /* The number of dependencies. */
2727 /* Whether we had a failure. */
2731 /* Array used to determine the number of hash table buckets to use
2732 based on the number of symbols there are. If there are fewer than
2733 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2734 fewer than 37 we use 17 buckets, and so forth. We never use more
2735 than 32771 buckets. */
2737 static const size_t elf_buckets
[] =
2739 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2743 /* Compute bucket count for hashing table. We do not use a static set
2744 of possible tables sizes anymore. Instead we determine for all
2745 possible reasonable sizes of the table the outcome (i.e., the
2746 number of collisions etc) and choose the best solution. The
2747 weighting functions are not too simple to allow the table to grow
2748 without bounds. Instead one of the weighting factors is the size.
2749 Therefore the result is always a good payoff between few collisions
2750 (= short chain lengths) and table size. */
2752 compute_bucket_count (info
)
2753 struct bfd_link_info
*info
;
2755 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2756 size_t best_size
= 0;
2757 unsigned long int *hashcodes
;
2758 unsigned long int *hashcodesp
;
2759 unsigned long int i
;
2762 /* Compute the hash values for all exported symbols. At the same
2763 time store the values in an array so that we could use them for
2766 amt
*= sizeof (unsigned long int);
2767 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
2768 if (hashcodes
== NULL
)
2770 hashcodesp
= hashcodes
;
2772 /* Put all hash values in HASHCODES. */
2773 elf_link_hash_traverse (elf_hash_table (info
),
2774 elf_collect_hash_codes
, &hashcodesp
);
2776 /* We have a problem here. The following code to optimize the table
2777 size requires an integer type with more the 32 bits. If
2778 BFD_HOST_U_64_BIT is set we know about such a type. */
2779 #ifdef BFD_HOST_U_64_BIT
2780 if (info
->optimize
== true)
2782 unsigned long int nsyms
= hashcodesp
- hashcodes
;
2785 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
2786 unsigned long int *counts
;
2788 /* Possible optimization parameters: if we have NSYMS symbols we say
2789 that the hashing table must at least have NSYMS/4 and at most
2791 minsize
= nsyms
/ 4;
2794 best_size
= maxsize
= nsyms
* 2;
2796 /* Create array where we count the collisions in. We must use bfd_malloc
2797 since the size could be large. */
2799 amt
*= sizeof (unsigned long int);
2800 counts
= (unsigned long int *) bfd_malloc (amt
);
2807 /* Compute the "optimal" size for the hash table. The criteria is a
2808 minimal chain length. The minor criteria is (of course) the size
2810 for (i
= minsize
; i
< maxsize
; ++i
)
2812 /* Walk through the array of hashcodes and count the collisions. */
2813 BFD_HOST_U_64_BIT max
;
2814 unsigned long int j
;
2815 unsigned long int fact
;
2817 memset (counts
, '\0', i
* sizeof (unsigned long int));
2819 /* Determine how often each hash bucket is used. */
2820 for (j
= 0; j
< nsyms
; ++j
)
2821 ++counts
[hashcodes
[j
] % i
];
2823 /* For the weight function we need some information about the
2824 pagesize on the target. This is information need not be 100%
2825 accurate. Since this information is not available (so far) we
2826 define it here to a reasonable default value. If it is crucial
2827 to have a better value some day simply define this value. */
2828 # ifndef BFD_TARGET_PAGESIZE
2829 # define BFD_TARGET_PAGESIZE (4096)
2832 /* We in any case need 2 + NSYMS entries for the size values and
2834 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
2837 /* Variant 1: optimize for short chains. We add the squares
2838 of all the chain lengths (which favous many small chain
2839 over a few long chains). */
2840 for (j
= 0; j
< i
; ++j
)
2841 max
+= counts
[j
] * counts
[j
];
2843 /* This adds penalties for the overall size of the table. */
2844 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2847 /* Variant 2: Optimize a lot more for small table. Here we
2848 also add squares of the size but we also add penalties for
2849 empty slots (the +1 term). */
2850 for (j
= 0; j
< i
; ++j
)
2851 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
2853 /* The overall size of the table is considered, but not as
2854 strong as in variant 1, where it is squared. */
2855 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2859 /* Compare with current best results. */
2860 if (max
< best_chlen
)
2870 #endif /* defined (BFD_HOST_U_64_BIT) */
2872 /* This is the fallback solution if no 64bit type is available or if we
2873 are not supposed to spend much time on optimizations. We select the
2874 bucket count using a fixed set of numbers. */
2875 for (i
= 0; elf_buckets
[i
] != 0; i
++)
2877 best_size
= elf_buckets
[i
];
2878 if (dynsymcount
< elf_buckets
[i
+ 1])
2883 /* Free the arrays we needed. */
2889 /* Set up the sizes and contents of the ELF dynamic sections. This is
2890 called by the ELF linker emulation before_allocation routine. We
2891 must set the sizes of the sections before the linker sets the
2892 addresses of the various sections. */
2895 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
2897 auxiliary_filters
, info
, sinterpptr
,
2902 const char *filter_shlib
;
2903 const char * const *auxiliary_filters
;
2904 struct bfd_link_info
*info
;
2905 asection
**sinterpptr
;
2906 struct bfd_elf_version_tree
*verdefs
;
2908 bfd_size_type soname_indx
;
2910 struct elf_backend_data
*bed
;
2911 struct elf_assign_sym_version_info asvinfo
;
2915 soname_indx
= (bfd_size_type
) -1;
2917 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2920 if (! is_elf_hash_table (info
))
2923 /* The backend may have to create some sections regardless of whether
2924 we're dynamic or not. */
2925 bed
= get_elf_backend_data (output_bfd
);
2926 if (bed
->elf_backend_always_size_sections
2927 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
2930 dynobj
= elf_hash_table (info
)->dynobj
;
2932 /* If there were no dynamic objects in the link, there is nothing to
2937 if (elf_hash_table (info
)->dynamic_sections_created
)
2939 struct elf_info_failed eif
;
2940 struct elf_link_hash_entry
*h
;
2943 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
2944 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
2948 soname_indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2949 soname
, true, true);
2950 if (soname_indx
== (bfd_size_type
) -1
2951 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SONAME
,
2958 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMBOLIC
,
2961 info
->flags
|= DF_SYMBOLIC
;
2968 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, rpath
,
2970 if (indx
== (bfd_size_type
) -1
2971 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RPATH
, indx
)
2973 && ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RUNPATH
,
2978 if (filter_shlib
!= NULL
)
2982 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2983 filter_shlib
, true, true);
2984 if (indx
== (bfd_size_type
) -1
2985 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FILTER
, indx
))
2989 if (auxiliary_filters
!= NULL
)
2991 const char * const *p
;
2993 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
2997 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2999 if (indx
== (bfd_size_type
) -1
3000 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_AUXILIARY
,
3007 eif
.verdefs
= verdefs
;
3010 /* If we are supposed to export all symbols into the dynamic symbol
3011 table (this is not the normal case), then do so. */
3012 if (info
->export_dynamic
)
3014 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
3020 /* Attach all the symbols to their version information. */
3021 asvinfo
.output_bfd
= output_bfd
;
3022 asvinfo
.info
= info
;
3023 asvinfo
.verdefs
= verdefs
;
3024 asvinfo
.failed
= false;
3026 elf_link_hash_traverse (elf_hash_table (info
),
3027 elf_link_assign_sym_version
,
3032 /* Find all symbols which were defined in a dynamic object and make
3033 the backend pick a reasonable value for them. */
3034 elf_link_hash_traverse (elf_hash_table (info
),
3035 elf_adjust_dynamic_symbol
,
3040 /* Add some entries to the .dynamic section. We fill in some of the
3041 values later, in elf_bfd_final_link, but we must add the entries
3042 now so that we know the final size of the .dynamic section. */
3044 /* If there are initialization and/or finalization functions to
3045 call then add the corresponding DT_INIT/DT_FINI entries. */
3046 h
= (info
->init_function
3047 ? elf_link_hash_lookup (elf_hash_table (info
),
3048 info
->init_function
, false,
3052 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3053 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3055 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT
, (bfd_vma
) 0))
3058 h
= (info
->fini_function
3059 ? elf_link_hash_lookup (elf_hash_table (info
),
3060 info
->fini_function
, false,
3064 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3065 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3067 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI
, (bfd_vma
) 0))
3071 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
3072 /* If .dynstr is excluded from the link, we don't want any of
3073 these tags. Strictly, we should be checking each section
3074 individually; This quick check covers for the case where
3075 someone does a /DISCARD/ : { *(*) }. */
3076 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
3078 bfd_size_type strsize
;
3080 strsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
3081 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_HASH
, (bfd_vma
) 0)
3082 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRTAB
, (bfd_vma
) 0)
3083 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMTAB
, (bfd_vma
) 0)
3084 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRSZ
, strsize
)
3085 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMENT
,
3086 (bfd_vma
) sizeof (Elf_External_Sym
)))
3091 /* The backend must work out the sizes of all the other dynamic
3093 if (bed
->elf_backend_size_dynamic_sections
3094 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
3097 if (elf_hash_table (info
)->dynamic_sections_created
)
3099 bfd_size_type dynsymcount
;
3101 size_t bucketcount
= 0;
3102 size_t hash_entry_size
;
3103 unsigned int dtagcount
;
3105 /* Set up the version definition section. */
3106 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3107 BFD_ASSERT (s
!= NULL
);
3109 /* We may have created additional version definitions if we are
3110 just linking a regular application. */
3111 verdefs
= asvinfo
.verdefs
;
3113 if (verdefs
== NULL
)
3114 _bfd_strip_section_from_output (info
, s
);
3119 struct bfd_elf_version_tree
*t
;
3121 Elf_Internal_Verdef def
;
3122 Elf_Internal_Verdaux defaux
;
3127 /* Make space for the base version. */
3128 size
+= sizeof (Elf_External_Verdef
);
3129 size
+= sizeof (Elf_External_Verdaux
);
3132 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3134 struct bfd_elf_version_deps
*n
;
3136 size
+= sizeof (Elf_External_Verdef
);
3137 size
+= sizeof (Elf_External_Verdaux
);
3140 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3141 size
+= sizeof (Elf_External_Verdaux
);
3144 s
->_raw_size
= size
;
3145 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3146 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3149 /* Fill in the version definition section. */
3153 def
.vd_version
= VER_DEF_CURRENT
;
3154 def
.vd_flags
= VER_FLG_BASE
;
3157 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3158 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3159 + sizeof (Elf_External_Verdaux
));
3161 if (soname_indx
!= (bfd_size_type
) -1)
3163 def
.vd_hash
= bfd_elf_hash (soname
);
3164 defaux
.vda_name
= soname_indx
;
3171 name
= basename (output_bfd
->filename
);
3172 def
.vd_hash
= bfd_elf_hash (name
);
3173 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3175 if (indx
== (bfd_size_type
) -1)
3177 defaux
.vda_name
= indx
;
3179 defaux
.vda_next
= 0;
3181 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3182 (Elf_External_Verdef
*) p
);
3183 p
+= sizeof (Elf_External_Verdef
);
3184 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3185 (Elf_External_Verdaux
*) p
);
3186 p
+= sizeof (Elf_External_Verdaux
);
3188 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3191 struct bfd_elf_version_deps
*n
;
3192 struct elf_link_hash_entry
*h
;
3195 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3198 /* Add a symbol representing this version. */
3200 if (! (_bfd_generic_link_add_one_symbol
3201 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
3202 (bfd_vma
) 0, (const char *) NULL
, false,
3203 get_elf_backend_data (dynobj
)->collect
,
3204 (struct bfd_link_hash_entry
**) &h
)))
3206 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
3207 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3208 h
->type
= STT_OBJECT
;
3209 h
->verinfo
.vertree
= t
;
3211 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
3214 def
.vd_version
= VER_DEF_CURRENT
;
3216 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
3217 def
.vd_flags
|= VER_FLG_WEAK
;
3218 def
.vd_ndx
= t
->vernum
+ 1;
3219 def
.vd_cnt
= cdeps
+ 1;
3220 def
.vd_hash
= bfd_elf_hash (t
->name
);
3221 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3222 if (t
->next
!= NULL
)
3223 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3224 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
3228 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3229 (Elf_External_Verdef
*) p
);
3230 p
+= sizeof (Elf_External_Verdef
);
3232 defaux
.vda_name
= h
->dynstr_index
;
3233 if (t
->deps
== NULL
)
3234 defaux
.vda_next
= 0;
3236 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3237 t
->name_indx
= defaux
.vda_name
;
3239 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3240 (Elf_External_Verdaux
*) p
);
3241 p
+= sizeof (Elf_External_Verdaux
);
3243 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3245 if (n
->version_needed
== NULL
)
3247 /* This can happen if there was an error in the
3249 defaux
.vda_name
= 0;
3252 defaux
.vda_name
= n
->version_needed
->name_indx
;
3253 if (n
->next
== NULL
)
3254 defaux
.vda_next
= 0;
3256 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3258 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3259 (Elf_External_Verdaux
*) p
);
3260 p
+= sizeof (Elf_External_Verdaux
);
3264 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEF
, (bfd_vma
) 0)
3265 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEFNUM
,
3269 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
3272 if (info
->new_dtags
&& info
->flags
)
3274 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS
, info
->flags
))
3281 info
->flags_1
&= ~ (DF_1_INITFIRST
3284 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS_1
,
3289 /* Work out the size of the version reference section. */
3291 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3292 BFD_ASSERT (s
!= NULL
);
3294 struct elf_find_verdep_info sinfo
;
3296 sinfo
.output_bfd
= output_bfd
;
3298 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
3299 if (sinfo
.vers
== 0)
3301 sinfo
.failed
= false;
3303 elf_link_hash_traverse (elf_hash_table (info
),
3304 elf_link_find_version_dependencies
,
3307 if (elf_tdata (output_bfd
)->verref
== NULL
)
3308 _bfd_strip_section_from_output (info
, s
);
3311 Elf_Internal_Verneed
*t
;
3316 /* Build the version definition section. */
3319 for (t
= elf_tdata (output_bfd
)->verref
;
3323 Elf_Internal_Vernaux
*a
;
3325 size
+= sizeof (Elf_External_Verneed
);
3327 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3328 size
+= sizeof (Elf_External_Vernaux
);
3331 s
->_raw_size
= size
;
3332 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3333 if (s
->contents
== NULL
)
3337 for (t
= elf_tdata (output_bfd
)->verref
;
3342 Elf_Internal_Vernaux
*a
;
3346 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3349 t
->vn_version
= VER_NEED_CURRENT
;
3351 if (elf_dt_name (t
->vn_bfd
) != NULL
)
3352 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3353 elf_dt_name (t
->vn_bfd
),
3356 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3357 basename (t
->vn_bfd
->filename
),
3359 if (indx
== (bfd_size_type
) -1)
3362 t
->vn_aux
= sizeof (Elf_External_Verneed
);
3363 if (t
->vn_nextref
== NULL
)
3366 t
->vn_next
= (sizeof (Elf_External_Verneed
)
3367 + caux
* sizeof (Elf_External_Vernaux
));
3369 _bfd_elf_swap_verneed_out (output_bfd
, t
,
3370 (Elf_External_Verneed
*) p
);
3371 p
+= sizeof (Elf_External_Verneed
);
3373 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3375 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
3376 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3377 a
->vna_nodename
, true, false);
3378 if (indx
== (bfd_size_type
) -1)
3381 if (a
->vna_nextptr
== NULL
)
3384 a
->vna_next
= sizeof (Elf_External_Vernaux
);
3386 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
3387 (Elf_External_Vernaux
*) p
);
3388 p
+= sizeof (Elf_External_Vernaux
);
3392 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEED
,
3394 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEEDNUM
,
3398 elf_tdata (output_bfd
)->cverrefs
= crefs
;
3402 /* Assign dynsym indicies. In a shared library we generate a
3403 section symbol for each output section, which come first.
3404 Next come all of the back-end allocated local dynamic syms,
3405 followed by the rest of the global symbols. */
3407 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3409 /* Work out the size of the symbol version section. */
3410 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3411 BFD_ASSERT (s
!= NULL
);
3412 if (dynsymcount
== 0
3413 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
3415 _bfd_strip_section_from_output (info
, s
);
3416 /* The DYNSYMCOUNT might have changed if we were going to
3417 output a dynamic symbol table entry for S. */
3418 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3422 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
3423 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3424 if (s
->contents
== NULL
)
3427 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERSYM
, (bfd_vma
) 0))
3431 /* Set the size of the .dynsym and .hash sections. We counted
3432 the number of dynamic symbols in elf_link_add_object_symbols.
3433 We will build the contents of .dynsym and .hash when we build
3434 the final symbol table, because until then we do not know the
3435 correct value to give the symbols. We built the .dynstr
3436 section as we went along in elf_link_add_object_symbols. */
3437 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
3438 BFD_ASSERT (s
!= NULL
);
3439 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
3440 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3441 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3444 if (dynsymcount
!= 0)
3446 Elf_Internal_Sym isym
;
3448 /* The first entry in .dynsym is a dummy symbol. */
3455 elf_swap_symbol_out (output_bfd
, &isym
,
3456 (PTR
) (Elf_External_Sym
*) s
->contents
);
3459 /* Compute the size of the hashing table. As a side effect this
3460 computes the hash values for all the names we export. */
3461 bucketcount
= compute_bucket_count (info
);
3463 s
= bfd_get_section_by_name (dynobj
, ".hash");
3464 BFD_ASSERT (s
!= NULL
);
3465 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
3466 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
3467 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3468 if (s
->contents
== NULL
)
3470 memset (s
->contents
, 0, (size_t) s
->_raw_size
);
3472 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) bucketcount
,
3474 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) dynsymcount
,
3475 s
->contents
+ hash_entry_size
);
3477 elf_hash_table (info
)->bucketcount
= bucketcount
;
3479 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
3480 BFD_ASSERT (s
!= NULL
);
3481 s
->_raw_size
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
3483 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
3484 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NULL
, (bfd_vma
) 0))
3491 /* Fix up the flags for a symbol. This handles various cases which
3492 can only be fixed after all the input files are seen. This is
3493 currently called by both adjust_dynamic_symbol and
3494 assign_sym_version, which is unnecessary but perhaps more robust in
3495 the face of future changes. */
3498 elf_fix_symbol_flags (h
, eif
)
3499 struct elf_link_hash_entry
*h
;
3500 struct elf_info_failed
*eif
;
3502 /* If this symbol was mentioned in a non-ELF file, try to set
3503 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3504 permit a non-ELF file to correctly refer to a symbol defined in
3505 an ELF dynamic object. */
3506 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
3508 while (h
->root
.type
== bfd_link_hash_indirect
)
3509 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3511 if (h
->root
.type
!= bfd_link_hash_defined
3512 && h
->root
.type
!= bfd_link_hash_defweak
)
3513 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3514 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3517 if (h
->root
.u
.def
.section
->owner
!= NULL
3518 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3519 == bfd_target_elf_flavour
))
3520 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3521 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3523 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3526 if (h
->dynindx
== -1
3527 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
3528 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
3530 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3539 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3540 was first seen in a non-ELF file. Fortunately, if the symbol
3541 was first seen in an ELF file, we're probably OK unless the
3542 symbol was defined in a non-ELF file. Catch that case here.
3543 FIXME: We're still in trouble if the symbol was first seen in
3544 a dynamic object, and then later in a non-ELF regular object. */
3545 if ((h
->root
.type
== bfd_link_hash_defined
3546 || h
->root
.type
== bfd_link_hash_defweak
)
3547 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3548 && (h
->root
.u
.def
.section
->owner
!= NULL
3549 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3550 != bfd_target_elf_flavour
)
3551 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3552 && (h
->elf_link_hash_flags
3553 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
3554 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3557 /* If this is a final link, and the symbol was defined as a common
3558 symbol in a regular object file, and there was no definition in
3559 any dynamic object, then the linker will have allocated space for
3560 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3561 flag will not have been set. */
3562 if (h
->root
.type
== bfd_link_hash_defined
3563 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3564 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
3565 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3566 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3567 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3569 /* If -Bsymbolic was used (which means to bind references to global
3570 symbols to the definition within the shared object), and this
3571 symbol was defined in a regular object, then it actually doesn't
3572 need a PLT entry, and we can accomplish that by forcing it local.
3573 Likewise, if the symbol has hidden or internal visibility.
3574 FIXME: It might be that we also do not need a PLT for other
3575 non-hidden visibilities, but we would have to tell that to the
3576 backend specifically; we can't just clear PLT-related data here. */
3577 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
3578 && eif
->info
->shared
3579 && is_elf_hash_table (eif
->info
)
3580 && (eif
->info
->symbolic
3581 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3582 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3583 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3585 struct elf_backend_data
*bed
;
3587 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3588 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3589 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3590 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3591 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
);
3594 /* If this is a weak defined symbol in a dynamic object, and we know
3595 the real definition in the dynamic object, copy interesting flags
3596 over to the real definition. */
3597 if (h
->weakdef
!= NULL
)
3599 struct elf_link_hash_entry
*weakdef
;
3601 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3602 || h
->root
.type
== bfd_link_hash_defweak
);
3603 weakdef
= h
->weakdef
;
3604 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
3605 || weakdef
->root
.type
== bfd_link_hash_defweak
);
3606 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
3608 /* If the real definition is defined by a regular object file,
3609 don't do anything special. See the longer description in
3610 elf_adjust_dynamic_symbol, below. */
3611 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3614 weakdef
->elf_link_hash_flags
|=
3615 (h
->elf_link_hash_flags
3616 & (ELF_LINK_HASH_REF_REGULAR
3617 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
3618 | ELF_LINK_NON_GOT_REF
));
3624 /* Make the backend pick a good value for a dynamic symbol. This is
3625 called via elf_link_hash_traverse, and also calls itself
3629 elf_adjust_dynamic_symbol (h
, data
)
3630 struct elf_link_hash_entry
*h
;
3633 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3635 struct elf_backend_data
*bed
;
3637 /* Ignore indirect symbols. These are added by the versioning code. */
3638 if (h
->root
.type
== bfd_link_hash_indirect
)
3641 if (! is_elf_hash_table (eif
->info
))
3644 /* Fix the symbol flags. */
3645 if (! elf_fix_symbol_flags (h
, eif
))
3648 /* If this symbol does not require a PLT entry, and it is not
3649 defined by a dynamic object, or is not referenced by a regular
3650 object, ignore it. We do have to handle a weak defined symbol,
3651 even if no regular object refers to it, if we decided to add it
3652 to the dynamic symbol table. FIXME: Do we normally need to worry
3653 about symbols which are defined by one dynamic object and
3654 referenced by another one? */
3655 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
3656 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
3657 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3658 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
3659 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
3661 h
->plt
.offset
= (bfd_vma
) -1;
3665 /* If we've already adjusted this symbol, don't do it again. This
3666 can happen via a recursive call. */
3667 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
3670 /* Don't look at this symbol again. Note that we must set this
3671 after checking the above conditions, because we may look at a
3672 symbol once, decide not to do anything, and then get called
3673 recursively later after REF_REGULAR is set below. */
3674 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
3676 /* If this is a weak definition, and we know a real definition, and
3677 the real symbol is not itself defined by a regular object file,
3678 then get a good value for the real definition. We handle the
3679 real symbol first, for the convenience of the backend routine.
3681 Note that there is a confusing case here. If the real definition
3682 is defined by a regular object file, we don't get the real symbol
3683 from the dynamic object, but we do get the weak symbol. If the
3684 processor backend uses a COPY reloc, then if some routine in the
3685 dynamic object changes the real symbol, we will not see that
3686 change in the corresponding weak symbol. This is the way other
3687 ELF linkers work as well, and seems to be a result of the shared
3690 I will clarify this issue. Most SVR4 shared libraries define the
3691 variable _timezone and define timezone as a weak synonym. The
3692 tzset call changes _timezone. If you write
3693 extern int timezone;
3695 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3696 you might expect that, since timezone is a synonym for _timezone,
3697 the same number will print both times. However, if the processor
3698 backend uses a COPY reloc, then actually timezone will be copied
3699 into your process image, and, since you define _timezone
3700 yourself, _timezone will not. Thus timezone and _timezone will
3701 wind up at different memory locations. The tzset call will set
3702 _timezone, leaving timezone unchanged. */
3704 if (h
->weakdef
!= NULL
)
3706 /* If we get to this point, we know there is an implicit
3707 reference by a regular object file via the weak symbol H.
3708 FIXME: Is this really true? What if the traversal finds
3709 H->WEAKDEF before it finds H? */
3710 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
3712 if (! elf_adjust_dynamic_symbol (h
->weakdef
, (PTR
) eif
))
3716 /* If a symbol has no type and no size and does not require a PLT
3717 entry, then we are probably about to do the wrong thing here: we
3718 are probably going to create a COPY reloc for an empty object.
3719 This case can arise when a shared object is built with assembly
3720 code, and the assembly code fails to set the symbol type. */
3722 && h
->type
== STT_NOTYPE
3723 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
3724 (*_bfd_error_handler
)
3725 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3726 h
->root
.root
.string
);
3728 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
3729 bed
= get_elf_backend_data (dynobj
);
3730 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3739 /* This routine is used to export all defined symbols into the dynamic
3740 symbol table. It is called via elf_link_hash_traverse. */
3743 elf_export_symbol (h
, data
)
3744 struct elf_link_hash_entry
*h
;
3747 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3749 /* Ignore indirect symbols. These are added by the versioning code. */
3750 if (h
->root
.type
== bfd_link_hash_indirect
)
3753 if (h
->dynindx
== -1
3754 && (h
->elf_link_hash_flags
3755 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
3757 struct bfd_elf_version_tree
*t
;
3758 struct bfd_elf_version_expr
*d
;
3760 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
3762 if (t
->globals
!= NULL
)
3764 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3766 if ((*d
->match
) (d
, h
->root
.root
.string
))
3771 if (t
->locals
!= NULL
)
3773 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3775 if ((*d
->match
) (d
, h
->root
.root
.string
))
3784 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3795 /* Look through the symbols which are defined in other shared
3796 libraries and referenced here. Update the list of version
3797 dependencies. This will be put into the .gnu.version_r section.
3798 This function is called via elf_link_hash_traverse. */
3801 elf_link_find_version_dependencies (h
, data
)
3802 struct elf_link_hash_entry
*h
;
3805 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
3806 Elf_Internal_Verneed
*t
;
3807 Elf_Internal_Vernaux
*a
;
3810 /* We only care about symbols defined in shared objects with version
3812 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3813 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
3815 || h
->verinfo
.verdef
== NULL
)
3818 /* See if we already know about this version. */
3819 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
3821 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
3824 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3825 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
3831 /* This is a new version. Add it to tree we are building. */
3836 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
3839 rinfo
->failed
= true;
3843 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
3844 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
3845 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
3849 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
3851 /* Note that we are copying a string pointer here, and testing it
3852 above. If bfd_elf_string_from_elf_section is ever changed to
3853 discard the string data when low in memory, this will have to be
3855 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
3857 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
3858 a
->vna_nextptr
= t
->vn_auxptr
;
3860 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
3863 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
3870 /* Figure out appropriate versions for all the symbols. We may not
3871 have the version number script until we have read all of the input
3872 files, so until that point we don't know which symbols should be
3873 local. This function is called via elf_link_hash_traverse. */
3876 elf_link_assign_sym_version (h
, data
)
3877 struct elf_link_hash_entry
*h
;
3880 struct elf_assign_sym_version_info
*sinfo
;
3881 struct bfd_link_info
*info
;
3882 struct elf_backend_data
*bed
;
3883 struct elf_info_failed eif
;
3887 sinfo
= (struct elf_assign_sym_version_info
*) data
;
3890 /* Fix the symbol flags. */
3893 if (! elf_fix_symbol_flags (h
, &eif
))
3896 sinfo
->failed
= true;
3900 /* We only need version numbers for symbols defined in regular
3902 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
3905 bed
= get_elf_backend_data (sinfo
->output_bfd
);
3906 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3907 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
3909 struct bfd_elf_version_tree
*t
;
3914 /* There are two consecutive ELF_VER_CHR characters if this is
3915 not a hidden symbol. */
3917 if (*p
== ELF_VER_CHR
)
3923 /* If there is no version string, we can just return out. */
3927 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
3931 /* Look for the version. If we find it, it is no longer weak. */
3932 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
3934 if (strcmp (t
->name
, p
) == 0)
3938 struct bfd_elf_version_expr
*d
;
3940 len
= p
- h
->root
.root
.string
;
3941 alc
= bfd_alloc (sinfo
->output_bfd
, (bfd_size_type
) len
);
3944 strncpy (alc
, h
->root
.root
.string
, len
- 1);
3945 alc
[len
- 1] = '\0';
3946 if (alc
[len
- 2] == ELF_VER_CHR
)
3947 alc
[len
- 2] = '\0';
3949 h
->verinfo
.vertree
= t
;
3953 if (t
->globals
!= NULL
)
3955 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3956 if ((*d
->match
) (d
, alc
))
3960 /* See if there is anything to force this symbol to
3962 if (d
== NULL
&& t
->locals
!= NULL
)
3964 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3966 if ((*d
->match
) (d
, alc
))
3968 if (h
->dynindx
!= -1
3970 && ! info
->export_dynamic
)
3972 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3973 (*bed
->elf_backend_hide_symbol
) (info
, h
);
3974 /* FIXME: The name of the symbol has
3975 already been recorded in the dynamic
3976 string table section. */
3984 bfd_release (sinfo
->output_bfd
, alc
);
3989 /* If we are building an application, we need to create a
3990 version node for this version. */
3991 if (t
== NULL
&& ! info
->shared
)
3993 struct bfd_elf_version_tree
**pp
;
3996 /* If we aren't going to export this symbol, we don't need
3997 to worry about it. */
3998 if (h
->dynindx
== -1)
4002 t
= ((struct bfd_elf_version_tree
*)
4003 bfd_alloc (sinfo
->output_bfd
, amt
));
4006 sinfo
->failed
= true;
4015 t
->name_indx
= (unsigned int) -1;
4019 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4021 t
->vernum
= version_index
;
4025 h
->verinfo
.vertree
= t
;
4029 /* We could not find the version for a symbol when
4030 generating a shared archive. Return an error. */
4031 (*_bfd_error_handler
)
4032 (_("%s: undefined versioned symbol name %s"),
4033 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
4034 bfd_set_error (bfd_error_bad_value
);
4035 sinfo
->failed
= true;
4040 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4043 /* If we don't have a version for this symbol, see if we can find
4045 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
4047 struct bfd_elf_version_tree
*t
;
4048 struct bfd_elf_version_tree
*deflt
;
4049 struct bfd_elf_version_expr
*d
;
4051 /* See if can find what version this symbol is in. If the
4052 symbol is supposed to be local, then don't actually register
4055 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4057 if (t
->globals
!= NULL
)
4059 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4061 if ((*d
->match
) (d
, h
->root
.root
.string
))
4063 h
->verinfo
.vertree
= t
;
4072 if (t
->locals
!= NULL
)
4074 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4076 if (d
->pattern
[0] == '*' && d
->pattern
[1] == '\0')
4078 else if ((*d
->match
) (d
, h
->root
.root
.string
))
4080 h
->verinfo
.vertree
= t
;
4081 if (h
->dynindx
!= -1
4083 && ! info
->export_dynamic
)
4085 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
4086 (*bed
->elf_backend_hide_symbol
) (info
, h
);
4087 /* FIXME: The name of the symbol has already
4088 been recorded in the dynamic string table
4100 if (deflt
!= NULL
&& h
->verinfo
.vertree
== NULL
)
4102 h
->verinfo
.vertree
= deflt
;
4103 if (h
->dynindx
!= -1
4105 && ! info
->export_dynamic
)
4107 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
4108 (*bed
->elf_backend_hide_symbol
) (info
, h
);
4109 /* FIXME: The name of the symbol has already been
4110 recorded in the dynamic string table section. */
4118 /* Final phase of ELF linker. */
4120 /* A structure we use to avoid passing large numbers of arguments. */
4122 struct elf_final_link_info
4124 /* General link information. */
4125 struct bfd_link_info
*info
;
4128 /* Symbol string table. */
4129 struct bfd_strtab_hash
*symstrtab
;
4130 /* .dynsym section. */
4131 asection
*dynsym_sec
;
4132 /* .hash section. */
4134 /* symbol version section (.gnu.version). */
4135 asection
*symver_sec
;
4136 /* Buffer large enough to hold contents of any section. */
4138 /* Buffer large enough to hold external relocs of any section. */
4139 PTR external_relocs
;
4140 /* Buffer large enough to hold internal relocs of any section. */
4141 Elf_Internal_Rela
*internal_relocs
;
4142 /* Buffer large enough to hold external local symbols of any input
4144 Elf_External_Sym
*external_syms
;
4145 /* Buffer large enough to hold internal local symbols of any input
4147 Elf_Internal_Sym
*internal_syms
;
4148 /* Array large enough to hold a symbol index for each local symbol
4149 of any input BFD. */
4151 /* Array large enough to hold a section pointer for each local
4152 symbol of any input BFD. */
4153 asection
**sections
;
4154 /* Buffer to hold swapped out symbols. */
4155 Elf_External_Sym
*symbuf
;
4156 /* Number of swapped out symbols in buffer. */
4157 size_t symbuf_count
;
4158 /* Number of symbols which fit in symbuf. */
4162 static boolean elf_link_output_sym
4163 PARAMS ((struct elf_final_link_info
*, const char *,
4164 Elf_Internal_Sym
*, asection
*));
4165 static boolean elf_link_flush_output_syms
4166 PARAMS ((struct elf_final_link_info
*));
4167 static boolean elf_link_output_extsym
4168 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4169 static boolean elf_link_sec_merge_syms
4170 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4171 static boolean elf_link_input_bfd
4172 PARAMS ((struct elf_final_link_info
*, bfd
*));
4173 static boolean elf_reloc_link_order
4174 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
4175 struct bfd_link_order
*));
4177 /* This struct is used to pass information to elf_link_output_extsym. */
4179 struct elf_outext_info
4183 struct elf_final_link_info
*finfo
;
4186 /* Compute the size of, and allocate space for, REL_HDR which is the
4187 section header for a section containing relocations for O. */
4190 elf_link_size_reloc_section (abfd
, rel_hdr
, o
)
4192 Elf_Internal_Shdr
*rel_hdr
;
4195 bfd_size_type reloc_count
;
4196 bfd_size_type num_rel_hashes
;
4198 /* Figure out how many relocations there will be. */
4199 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
4200 reloc_count
= elf_section_data (o
)->rel_count
;
4202 reloc_count
= elf_section_data (o
)->rel_count2
;
4204 num_rel_hashes
= o
->reloc_count
;
4205 if (num_rel_hashes
< reloc_count
)
4206 num_rel_hashes
= reloc_count
;
4208 /* That allows us to calculate the size of the section. */
4209 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
4211 /* The contents field must last into write_object_contents, so we
4212 allocate it with bfd_alloc rather than malloc. Also since we
4213 cannot be sure that the contents will actually be filled in,
4214 we zero the allocated space. */
4215 rel_hdr
->contents
= (PTR
) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
4216 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
4219 /* We only allocate one set of hash entries, so we only do it the
4220 first time we are called. */
4221 if (elf_section_data (o
)->rel_hashes
== NULL
4224 struct elf_link_hash_entry
**p
;
4226 p
= ((struct elf_link_hash_entry
**)
4227 bfd_zmalloc (num_rel_hashes
4228 * sizeof (struct elf_link_hash_entry
*)));
4232 elf_section_data (o
)->rel_hashes
= p
;
4238 /* When performing a relocateable link, the input relocations are
4239 preserved. But, if they reference global symbols, the indices
4240 referenced must be updated. Update all the relocations in
4241 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4244 elf_link_adjust_relocs (abfd
, rel_hdr
, count
, rel_hash
)
4246 Elf_Internal_Shdr
*rel_hdr
;
4248 struct elf_link_hash_entry
**rel_hash
;
4251 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4252 Elf_Internal_Rel
*irel
;
4253 Elf_Internal_Rela
*irela
;
4254 bfd_size_type amt
= sizeof (Elf_Internal_Rel
) * bed
->s
->int_rels_per_ext_rel
;
4256 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (amt
);
4259 (*_bfd_error_handler
) (_("Error: out of memory"));
4263 amt
= sizeof (Elf_Internal_Rela
) * bed
->s
->int_rels_per_ext_rel
;
4264 irela
= (Elf_Internal_Rela
*) bfd_zmalloc (amt
);
4267 (*_bfd_error_handler
) (_("Error: out of memory"));
4271 for (i
= 0; i
< count
; i
++, rel_hash
++)
4273 if (*rel_hash
== NULL
)
4276 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
4278 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4280 Elf_External_Rel
*erel
;
4283 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
4284 if (bed
->s
->swap_reloc_in
)
4285 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
4287 elf_swap_reloc_in (abfd
, erel
, irel
);
4289 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4290 irel
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4291 ELF_R_TYPE (irel
[j
].r_info
));
4293 if (bed
->s
->swap_reloc_out
)
4294 (*bed
->s
->swap_reloc_out
) (abfd
, irel
, (bfd_byte
*) erel
);
4296 elf_swap_reloc_out (abfd
, irel
, erel
);
4300 Elf_External_Rela
*erela
;
4303 BFD_ASSERT (rel_hdr
->sh_entsize
4304 == sizeof (Elf_External_Rela
));
4306 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
4307 if (bed
->s
->swap_reloca_in
)
4308 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
4310 elf_swap_reloca_in (abfd
, erela
, irela
);
4312 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4313 irela
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4314 ELF_R_TYPE (irela
[j
].r_info
));
4316 if (bed
->s
->swap_reloca_out
)
4317 (*bed
->s
->swap_reloca_out
) (abfd
, irela
, (bfd_byte
*) erela
);
4319 elf_swap_reloca_out (abfd
, irela
, erela
);
4327 struct elf_link_sort_rela
{
4329 enum elf_reloc_type_class type
;
4331 Elf_Internal_Rel rel
;
4332 Elf_Internal_Rela rela
;
4337 elf_link_sort_cmp1 (A
, B
)
4341 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4342 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4343 int relativea
, relativeb
;
4345 relativea
= a
->type
== reloc_class_relative
;
4346 relativeb
= b
->type
== reloc_class_relative
;
4348 if (relativea
< relativeb
)
4350 if (relativea
> relativeb
)
4352 if (ELF_R_SYM (a
->u
.rel
.r_info
) < ELF_R_SYM (b
->u
.rel
.r_info
))
4354 if (ELF_R_SYM (a
->u
.rel
.r_info
) > ELF_R_SYM (b
->u
.rel
.r_info
))
4356 if (a
->u
.rel
.r_offset
< b
->u
.rel
.r_offset
)
4358 if (a
->u
.rel
.r_offset
> b
->u
.rel
.r_offset
)
4364 elf_link_sort_cmp2 (A
, B
)
4368 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4369 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4372 if (a
->offset
< b
->offset
)
4374 if (a
->offset
> b
->offset
)
4376 copya
= a
->type
== reloc_class_copy
;
4377 copyb
= b
->type
== reloc_class_copy
;
4382 if (a
->u
.rel
.r_offset
< b
->u
.rel
.r_offset
)
4384 if (a
->u
.rel
.r_offset
> b
->u
.rel
.r_offset
)
4390 elf_link_sort_relocs (abfd
, info
, psec
)
4392 struct bfd_link_info
*info
;
4395 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4396 asection
*reldyn
, *o
;
4397 boolean rel
= false;
4398 bfd_size_type count
, size
;
4400 struct elf_link_sort_rela
*rela
;
4401 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4403 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
4404 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4406 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
4407 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4410 count
= reldyn
->_raw_size
/ sizeof (Elf_External_Rel
);
4413 count
= reldyn
->_raw_size
/ sizeof (Elf_External_Rela
);
4416 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4417 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4418 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4419 && o
->output_section
== reldyn
)
4420 size
+= o
->_raw_size
;
4422 if (size
!= reldyn
->_raw_size
)
4425 rela
= (struct elf_link_sort_rela
*) bfd_zmalloc (sizeof (*rela
) * count
);
4428 (*info
->callbacks
->warning
)
4429 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0,
4434 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4435 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4436 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4437 && o
->output_section
== reldyn
)
4441 Elf_External_Rel
*erel
, *erelend
;
4442 struct elf_link_sort_rela
*s
;
4444 erel
= (Elf_External_Rel
*) o
->contents
;
4445 erelend
= (Elf_External_Rel
*) (o
->contents
+ o
->_raw_size
);
4446 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rel
);
4447 for (; erel
< erelend
; erel
++, s
++)
4449 if (bed
->s
->swap_reloc_in
)
4450 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, &s
->u
.rel
);
4452 elf_swap_reloc_in (abfd
, erel
, &s
->u
.rel
);
4454 s
->type
= (*bed
->elf_backend_reloc_type_class
) (&s
->u
.rela
);
4459 Elf_External_Rela
*erela
, *erelaend
;
4460 struct elf_link_sort_rela
*s
;
4462 erela
= (Elf_External_Rela
*) o
->contents
;
4463 erelaend
= (Elf_External_Rela
*) (o
->contents
+ o
->_raw_size
);
4464 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rela
);
4465 for (; erela
< erelaend
; erela
++, s
++)
4467 if (bed
->s
->swap_reloca_in
)
4468 (*bed
->s
->swap_reloca_in
) (dynobj
, (bfd_byte
*) erela
,
4471 elf_swap_reloca_in (dynobj
, erela
, &s
->u
.rela
);
4473 s
->type
= (*bed
->elf_backend_reloc_type_class
) (&s
->u
.rela
);
4478 qsort (rela
, count
, sizeof (*rela
), elf_link_sort_cmp1
);
4479 for (ret
= 0; ret
< count
&& rela
[ret
].type
== reloc_class_relative
; ret
++)
4481 for (i
= ret
, j
= ret
; i
< count
; i
++)
4483 if (ELF_R_SYM (rela
[i
].u
.rel
.r_info
) != ELF_R_SYM (rela
[j
].u
.rel
.r_info
))
4485 rela
[i
].offset
= rela
[j
].u
.rel
.r_offset
;
4487 qsort (rela
+ ret
, count
- ret
, sizeof (*rela
), elf_link_sort_cmp2
);
4489 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4490 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4491 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4492 && o
->output_section
== reldyn
)
4496 Elf_External_Rel
*erel
, *erelend
;
4497 struct elf_link_sort_rela
*s
;
4499 erel
= (Elf_External_Rel
*) o
->contents
;
4500 erelend
= (Elf_External_Rel
*) ((PTR
) o
->contents
+ o
->_raw_size
);
4501 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rel
);
4502 for (; erel
< erelend
; erel
++, s
++)
4504 if (bed
->s
->swap_reloc_out
)
4505 (*bed
->s
->swap_reloc_out
) (abfd
, &s
->u
.rel
,
4508 elf_swap_reloc_out (abfd
, &s
->u
.rel
, erel
);
4513 Elf_External_Rela
*erela
, *erelaend
;
4514 struct elf_link_sort_rela
*s
;
4516 erela
= (Elf_External_Rela
*) o
->contents
;
4517 erelaend
= (Elf_External_Rela
*) ((PTR
) o
->contents
+ o
->_raw_size
);
4518 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rela
);
4519 for (; erela
< erelaend
; erela
++, s
++)
4521 if (bed
->s
->swap_reloca_out
)
4522 (*bed
->s
->swap_reloca_out
) (dynobj
, &s
->u
.rela
,
4523 (bfd_byte
*) erela
);
4525 elf_swap_reloca_out (dynobj
, &s
->u
.rela
, erela
);
4535 /* Do the final step of an ELF link. */
4538 elf_bfd_final_link (abfd
, info
)
4540 struct bfd_link_info
*info
;
4543 boolean emit_relocs
;
4545 struct elf_final_link_info finfo
;
4546 register asection
*o
;
4547 register struct bfd_link_order
*p
;
4549 bfd_size_type max_contents_size
;
4550 bfd_size_type max_external_reloc_size
;
4551 bfd_size_type max_internal_reloc_count
;
4552 bfd_size_type max_sym_count
;
4554 Elf_Internal_Sym elfsym
;
4556 Elf_Internal_Shdr
*symtab_hdr
;
4557 Elf_Internal_Shdr
*symstrtab_hdr
;
4558 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4559 struct elf_outext_info eoinfo
;
4561 size_t relativecount
= 0;
4562 asection
*reldyn
= 0;
4565 if (! is_elf_hash_table (info
))
4569 abfd
->flags
|= DYNAMIC
;
4571 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
4572 dynobj
= elf_hash_table (info
)->dynobj
;
4574 emit_relocs
= (info
->relocateable
4575 || info
->emitrelocations
4576 || bed
->elf_backend_emit_relocs
);
4579 finfo
.output_bfd
= abfd
;
4580 finfo
.symstrtab
= elf_stringtab_init ();
4581 if (finfo
.symstrtab
== NULL
)
4586 finfo
.dynsym_sec
= NULL
;
4587 finfo
.hash_sec
= NULL
;
4588 finfo
.symver_sec
= NULL
;
4592 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
4593 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
4594 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
4595 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
4596 /* Note that it is OK if symver_sec is NULL. */
4599 finfo
.contents
= NULL
;
4600 finfo
.external_relocs
= NULL
;
4601 finfo
.internal_relocs
= NULL
;
4602 finfo
.external_syms
= NULL
;
4603 finfo
.internal_syms
= NULL
;
4604 finfo
.indices
= NULL
;
4605 finfo
.sections
= NULL
;
4606 finfo
.symbuf
= NULL
;
4607 finfo
.symbuf_count
= 0;
4609 /* Count up the number of relocations we will output for each output
4610 section, so that we know the sizes of the reloc sections. We
4611 also figure out some maximum sizes. */
4612 max_contents_size
= 0;
4613 max_external_reloc_size
= 0;
4614 max_internal_reloc_count
= 0;
4617 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
4621 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
4623 if (p
->type
== bfd_section_reloc_link_order
4624 || p
->type
== bfd_symbol_reloc_link_order
)
4626 else if (p
->type
== bfd_indirect_link_order
)
4630 sec
= p
->u
.indirect
.section
;
4632 /* Mark all sections which are to be included in the
4633 link. This will normally be every section. We need
4634 to do this so that we can identify any sections which
4635 the linker has decided to not include. */
4636 sec
->linker_mark
= true;
4638 if (sec
->flags
& SEC_MERGE
)
4641 if (info
->relocateable
|| info
->emitrelocations
)
4642 o
->reloc_count
+= sec
->reloc_count
;
4643 else if (bed
->elf_backend_count_relocs
)
4645 Elf_Internal_Rela
* relocs
;
4647 relocs
= (NAME(_bfd_elf
,link_read_relocs
)
4648 (abfd
, sec
, (PTR
) NULL
,
4649 (Elf_Internal_Rela
*) NULL
, info
->keep_memory
));
4651 o
->reloc_count
+= (*bed
->elf_backend_count_relocs
)
4654 if (!info
->keep_memory
)
4658 if (sec
->_raw_size
> max_contents_size
)
4659 max_contents_size
= sec
->_raw_size
;
4660 if (sec
->_cooked_size
> max_contents_size
)
4661 max_contents_size
= sec
->_cooked_size
;
4663 /* We are interested in just local symbols, not all
4665 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
4666 && (sec
->owner
->flags
& DYNAMIC
) == 0)
4670 if (elf_bad_symtab (sec
->owner
))
4671 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
4672 / sizeof (Elf_External_Sym
));
4674 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
4676 if (sym_count
> max_sym_count
)
4677 max_sym_count
= sym_count
;
4679 if ((sec
->flags
& SEC_RELOC
) != 0)
4683 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
4684 if (ext_size
> max_external_reloc_size
)
4685 max_external_reloc_size
= ext_size
;
4686 if (sec
->reloc_count
> max_internal_reloc_count
)
4687 max_internal_reloc_count
= sec
->reloc_count
;
4693 if (o
->reloc_count
> 0)
4694 o
->flags
|= SEC_RELOC
;
4697 /* Explicitly clear the SEC_RELOC flag. The linker tends to
4698 set it (this is probably a bug) and if it is set
4699 assign_section_numbers will create a reloc section. */
4700 o
->flags
&=~ SEC_RELOC
;
4703 /* If the SEC_ALLOC flag is not set, force the section VMA to
4704 zero. This is done in elf_fake_sections as well, but forcing
4705 the VMA to 0 here will ensure that relocs against these
4706 sections are handled correctly. */
4707 if ((o
->flags
& SEC_ALLOC
) == 0
4708 && ! o
->user_set_vma
)
4712 if (! info
->relocateable
&& merged
)
4713 elf_link_hash_traverse (elf_hash_table (info
),
4714 elf_link_sec_merge_syms
, (PTR
) abfd
);
4716 /* Figure out the file positions for everything but the symbol table
4717 and the relocs. We set symcount to force assign_section_numbers
4718 to create a symbol table. */
4719 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
4720 BFD_ASSERT (! abfd
->output_has_begun
);
4721 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
4724 /* Figure out how many relocations we will have in each section.
4725 Just using RELOC_COUNT isn't good enough since that doesn't
4726 maintain a separate value for REL vs. RELA relocations. */
4728 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
4729 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
4731 asection
*output_section
;
4733 if (! o
->linker_mark
)
4735 /* This section was omitted from the link. */
4739 output_section
= o
->output_section
;
4741 if (output_section
!= NULL
4742 && (o
->flags
& SEC_RELOC
) != 0)
4744 struct bfd_elf_section_data
*esdi
4745 = elf_section_data (o
);
4746 struct bfd_elf_section_data
*esdo
4747 = elf_section_data (output_section
);
4748 unsigned int *rel_count
;
4749 unsigned int *rel_count2
;
4751 /* We must be careful to add the relocation froms the
4752 input section to the right output count. */
4753 if (esdi
->rel_hdr
.sh_entsize
== esdo
->rel_hdr
.sh_entsize
)
4755 rel_count
= &esdo
->rel_count
;
4756 rel_count2
= &esdo
->rel_count2
;
4760 rel_count
= &esdo
->rel_count2
;
4761 rel_count2
= &esdo
->rel_count
;
4764 *rel_count
+= NUM_SHDR_ENTRIES (& esdi
->rel_hdr
);
4766 *rel_count2
+= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
4767 output_section
->flags
|= SEC_RELOC
;
4771 /* That created the reloc sections. Set their sizes, and assign
4772 them file positions, and allocate some buffers. */
4773 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4775 if ((o
->flags
& SEC_RELOC
) != 0)
4777 if (!elf_link_size_reloc_section (abfd
,
4778 &elf_section_data (o
)->rel_hdr
,
4782 if (elf_section_data (o
)->rel_hdr2
4783 && !elf_link_size_reloc_section (abfd
,
4784 elf_section_data (o
)->rel_hdr2
,
4789 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
4790 to count upwards while actually outputting the relocations. */
4791 elf_section_data (o
)->rel_count
= 0;
4792 elf_section_data (o
)->rel_count2
= 0;
4795 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4797 /* We have now assigned file positions for all the sections except
4798 .symtab and .strtab. We start the .symtab section at the current
4799 file position, and write directly to it. We build the .strtab
4800 section in memory. */
4801 bfd_get_symcount (abfd
) = 0;
4802 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4803 /* sh_name is set in prep_headers. */
4804 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4805 symtab_hdr
->sh_flags
= 0;
4806 symtab_hdr
->sh_addr
= 0;
4807 symtab_hdr
->sh_size
= 0;
4808 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
4809 /* sh_link is set in assign_section_numbers. */
4810 /* sh_info is set below. */
4811 /* sh_offset is set just below. */
4812 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
4814 off
= elf_tdata (abfd
)->next_file_pos
;
4815 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
4817 /* Note that at this point elf_tdata (abfd)->next_file_pos is
4818 incorrect. We do not yet know the size of the .symtab section.
4819 We correct next_file_pos below, after we do know the size. */
4821 /* Allocate a buffer to hold swapped out symbols. This is to avoid
4822 continuously seeking to the right position in the file. */
4823 if (! info
->keep_memory
|| max_sym_count
< 20)
4824 finfo
.symbuf_size
= 20;
4826 finfo
.symbuf_size
= max_sym_count
;
4827 amt
= finfo
.symbuf_size
;
4828 amt
*= sizeof (Elf_External_Sym
);
4829 finfo
.symbuf
= (Elf_External_Sym
*) bfd_malloc (amt
);
4830 if (finfo
.symbuf
== NULL
)
4833 /* Start writing out the symbol table. The first symbol is always a
4835 if (info
->strip
!= strip_all
4838 elfsym
.st_value
= 0;
4841 elfsym
.st_other
= 0;
4842 elfsym
.st_shndx
= SHN_UNDEF
;
4843 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
4844 &elfsym
, bfd_und_section_ptr
))
4849 /* Some standard ELF linkers do this, but we don't because it causes
4850 bootstrap comparison failures. */
4851 /* Output a file symbol for the output file as the second symbol.
4852 We output this even if we are discarding local symbols, although
4853 I'm not sure if this is correct. */
4854 elfsym
.st_value
= 0;
4856 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4857 elfsym
.st_other
= 0;
4858 elfsym
.st_shndx
= SHN_ABS
;
4859 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
4860 &elfsym
, bfd_abs_section_ptr
))
4864 /* Output a symbol for each section. We output these even if we are
4865 discarding local symbols, since they are used for relocs. These
4866 symbols have no names. We store the index of each one in the
4867 index field of the section, so that we can find it again when
4868 outputting relocs. */
4869 if (info
->strip
!= strip_all
4873 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4874 elfsym
.st_other
= 0;
4875 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
4877 o
= section_from_elf_index (abfd
, i
);
4879 o
->target_index
= bfd_get_symcount (abfd
);
4880 elfsym
.st_shndx
= i
;
4881 if (info
->relocateable
|| o
== NULL
)
4882 elfsym
.st_value
= 0;
4884 elfsym
.st_value
= o
->vma
;
4885 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
4891 /* Allocate some memory to hold information read in from the input
4893 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
4894 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
4895 finfo
.internal_relocs
= ((Elf_Internal_Rela
*)
4896 bfd_malloc (max_internal_reloc_count
4897 * sizeof (Elf_Internal_Rela
)
4898 * bed
->s
->int_rels_per_ext_rel
));
4899 finfo
.external_syms
= ((Elf_External_Sym
*)
4900 bfd_malloc (max_sym_count
4901 * sizeof (Elf_External_Sym
)));
4902 finfo
.internal_syms
= ((Elf_Internal_Sym
*)
4903 bfd_malloc (max_sym_count
4904 * sizeof (Elf_Internal_Sym
)));
4905 finfo
.indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
4906 finfo
.sections
= ((asection
**)
4907 bfd_malloc (max_sym_count
* sizeof (asection
*)));
4908 if ((finfo
.contents
== NULL
&& max_contents_size
!= 0)
4909 || (finfo
.external_relocs
== NULL
&& max_external_reloc_size
!= 0)
4910 || (finfo
.internal_relocs
== NULL
&& max_internal_reloc_count
!= 0)
4911 || (finfo
.external_syms
== NULL
&& max_sym_count
!= 0)
4912 || (finfo
.internal_syms
== NULL
&& max_sym_count
!= 0)
4913 || (finfo
.indices
== NULL
&& max_sym_count
!= 0)
4914 || (finfo
.sections
== NULL
&& max_sym_count
!= 0))
4917 /* Since ELF permits relocations to be against local symbols, we
4918 must have the local symbols available when we do the relocations.
4919 Since we would rather only read the local symbols once, and we
4920 would rather not keep them in memory, we handle all the
4921 relocations for a single input file at the same time.
4923 Unfortunately, there is no way to know the total number of local
4924 symbols until we have seen all of them, and the local symbol
4925 indices precede the global symbol indices. This means that when
4926 we are generating relocateable output, and we see a reloc against
4927 a global symbol, we can not know the symbol index until we have
4928 finished examining all the local symbols to see which ones we are
4929 going to output. To deal with this, we keep the relocations in
4930 memory, and don't output them until the end of the link. This is
4931 an unfortunate waste of memory, but I don't see a good way around
4932 it. Fortunately, it only happens when performing a relocateable
4933 link, which is not the common case. FIXME: If keep_memory is set
4934 we could write the relocs out and then read them again; I don't
4935 know how bad the memory loss will be. */
4937 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
4938 sub
->output_has_begun
= false;
4939 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4941 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
4943 if (p
->type
== bfd_indirect_link_order
4944 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
4945 == bfd_target_elf_flavour
))
4947 sub
= p
->u
.indirect
.section
->owner
;
4948 if (! sub
->output_has_begun
)
4950 if (! elf_link_input_bfd (&finfo
, sub
))
4952 sub
->output_has_begun
= true;
4955 else if (p
->type
== bfd_section_reloc_link_order
4956 || p
->type
== bfd_symbol_reloc_link_order
)
4958 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
4963 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
4969 /* That wrote out all the local symbols. Finish up the symbol table
4970 with the global symbols. Even if we want to strip everything we
4971 can, we still need to deal with those global symbols that got
4972 converted to local in a version script. */
4976 /* Output any global symbols that got converted to local in a
4977 version script. We do this in a separate step since ELF
4978 requires all local symbols to appear prior to any global
4979 symbols. FIXME: We should only do this if some global
4980 symbols were, in fact, converted to become local. FIXME:
4981 Will this work correctly with the Irix 5 linker? */
4982 eoinfo
.failed
= false;
4983 eoinfo
.finfo
= &finfo
;
4984 eoinfo
.localsyms
= true;
4985 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
4991 /* The sh_info field records the index of the first non local symbol. */
4992 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
4995 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
4997 Elf_Internal_Sym sym
;
4998 Elf_External_Sym
*dynsym
=
4999 (Elf_External_Sym
*) finfo
.dynsym_sec
->contents
;
5000 long last_local
= 0;
5002 /* Write out the section symbols for the output sections. */
5009 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5012 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5015 indx
= elf_section_data (s
)->this_idx
;
5016 BFD_ASSERT (indx
> 0);
5017 sym
.st_shndx
= indx
;
5018 sym
.st_value
= s
->vma
;
5020 elf_swap_symbol_out (abfd
, &sym
,
5021 dynsym
+ elf_section_data (s
)->dynindx
);
5024 last_local
= bfd_count_sections (abfd
);
5027 /* Write out the local dynsyms. */
5028 if (elf_hash_table (info
)->dynlocal
)
5030 struct elf_link_local_dynamic_entry
*e
;
5031 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
5035 sym
.st_size
= e
->isym
.st_size
;
5036 sym
.st_other
= e
->isym
.st_other
;
5038 /* Copy the internal symbol as is.
5039 Note that we saved a word of storage and overwrote
5040 the original st_name with the dynstr_index. */
5043 if (e
->isym
.st_shndx
> 0 && e
->isym
.st_shndx
< SHN_LORESERVE
)
5045 s
= bfd_section_from_elf_index (e
->input_bfd
,
5049 elf_section_data (s
->output_section
)->this_idx
;
5050 sym
.st_value
= (s
->output_section
->vma
5052 + e
->isym
.st_value
);
5055 if (last_local
< e
->dynindx
)
5056 last_local
= e
->dynindx
;
5058 elf_swap_symbol_out (abfd
, &sym
, dynsym
+ e
->dynindx
);
5062 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
5066 /* We get the global symbols from the hash table. */
5067 eoinfo
.failed
= false;
5068 eoinfo
.localsyms
= false;
5069 eoinfo
.finfo
= &finfo
;
5070 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5075 /* If backend needs to output some symbols not present in the hash
5076 table, do it now. */
5077 if (bed
->elf_backend_output_arch_syms
)
5079 typedef boolean (*out_sym_func
) PARAMS ((PTR
, const char *,
5083 if (! ((*bed
->elf_backend_output_arch_syms
)
5084 (abfd
, info
, (PTR
) &finfo
, (out_sym_func
) elf_link_output_sym
)))
5088 /* Flush all symbols to the file. */
5089 if (! elf_link_flush_output_syms (&finfo
))
5092 /* Now we know the size of the symtab section. */
5093 off
+= symtab_hdr
->sh_size
;
5095 /* Finish up and write out the symbol string table (.strtab)
5097 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5098 /* sh_name was set in prep_headers. */
5099 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5100 symstrtab_hdr
->sh_flags
= 0;
5101 symstrtab_hdr
->sh_addr
= 0;
5102 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
5103 symstrtab_hdr
->sh_entsize
= 0;
5104 symstrtab_hdr
->sh_link
= 0;
5105 symstrtab_hdr
->sh_info
= 0;
5106 /* sh_offset is set just below. */
5107 symstrtab_hdr
->sh_addralign
= 1;
5109 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, true);
5110 elf_tdata (abfd
)->next_file_pos
= off
;
5112 if (bfd_get_symcount (abfd
) > 0)
5114 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
5115 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
5119 /* Adjust the relocs to have the correct symbol indices. */
5120 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5122 if ((o
->flags
& SEC_RELOC
) == 0)
5125 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
5126 elf_section_data (o
)->rel_count
,
5127 elf_section_data (o
)->rel_hashes
);
5128 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
5129 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
5130 elf_section_data (o
)->rel_count2
,
5131 (elf_section_data (o
)->rel_hashes
5132 + elf_section_data (o
)->rel_count
));
5134 /* Set the reloc_count field to 0 to prevent write_relocs from
5135 trying to swap the relocs out itself. */
5139 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
5140 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
5142 /* If we are linking against a dynamic object, or generating a
5143 shared library, finish up the dynamic linking information. */
5146 Elf_External_Dyn
*dyncon
, *dynconend
;
5148 /* Fix up .dynamic entries. */
5149 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
5150 BFD_ASSERT (o
!= NULL
);
5152 dyncon
= (Elf_External_Dyn
*) o
->contents
;
5153 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
5154 for (; dyncon
< dynconend
; dyncon
++)
5156 Elf_Internal_Dyn dyn
;
5160 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
5167 if (relativecount
> 0 && dyncon
+ 1 < dynconend
)
5169 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
5171 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
5172 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
5175 if (dyn
.d_tag
!= DT_NULL
)
5177 dyn
.d_un
.d_val
= relativecount
;
5178 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5184 name
= info
->init_function
;
5187 name
= info
->fini_function
;
5190 struct elf_link_hash_entry
*h
;
5192 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
5193 false, false, true);
5195 && (h
->root
.type
== bfd_link_hash_defined
5196 || h
->root
.type
== bfd_link_hash_defweak
))
5198 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
5199 o
= h
->root
.u
.def
.section
;
5200 if (o
->output_section
!= NULL
)
5201 dyn
.d_un
.d_val
+= (o
->output_section
->vma
5202 + o
->output_offset
);
5205 /* The symbol is imported from another shared
5206 library and does not apply to this one. */
5210 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5225 name
= ".gnu.version_d";
5228 name
= ".gnu.version_r";
5231 name
= ".gnu.version";
5233 o
= bfd_get_section_by_name (abfd
, name
);
5234 BFD_ASSERT (o
!= NULL
);
5235 dyn
.d_un
.d_ptr
= o
->vma
;
5236 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5243 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
5248 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
5250 Elf_Internal_Shdr
*hdr
;
5252 hdr
= elf_elfsections (abfd
)[i
];
5253 if (hdr
->sh_type
== type
5254 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
5256 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
5257 dyn
.d_un
.d_val
+= hdr
->sh_size
;
5260 if (dyn
.d_un
.d_val
== 0
5261 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
5262 dyn
.d_un
.d_val
= hdr
->sh_addr
;
5266 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5272 /* If we have created any dynamic sections, then output them. */
5275 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
5278 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
5280 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5281 || o
->_raw_size
== 0
5282 || o
->output_section
== bfd_abs_section_ptr
)
5284 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
5286 /* At this point, we are only interested in sections
5287 created by elf_link_create_dynamic_sections. */
5290 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
5292 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
5294 if (! bfd_set_section_contents (abfd
, o
->output_section
,
5296 (file_ptr
) o
->output_offset
,
5302 /* The contents of the .dynstr section are actually in a
5304 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
5305 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
5306 || ! _bfd_stringtab_emit (abfd
,
5307 elf_hash_table (info
)->dynstr
))
5313 /* If we have optimized stabs strings, output them. */
5314 if (elf_hash_table (info
)->stab_info
!= NULL
)
5316 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
5320 if (finfo
.symstrtab
!= NULL
)
5321 _bfd_stringtab_free (finfo
.symstrtab
);
5322 if (finfo
.contents
!= NULL
)
5323 free (finfo
.contents
);
5324 if (finfo
.external_relocs
!= NULL
)
5325 free (finfo
.external_relocs
);
5326 if (finfo
.internal_relocs
!= NULL
)
5327 free (finfo
.internal_relocs
);
5328 if (finfo
.external_syms
!= NULL
)
5329 free (finfo
.external_syms
);
5330 if (finfo
.internal_syms
!= NULL
)
5331 free (finfo
.internal_syms
);
5332 if (finfo
.indices
!= NULL
)
5333 free (finfo
.indices
);
5334 if (finfo
.sections
!= NULL
)
5335 free (finfo
.sections
);
5336 if (finfo
.symbuf
!= NULL
)
5337 free (finfo
.symbuf
);
5338 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5340 if ((o
->flags
& SEC_RELOC
) != 0
5341 && elf_section_data (o
)->rel_hashes
!= NULL
)
5342 free (elf_section_data (o
)->rel_hashes
);
5345 elf_tdata (abfd
)->linker
= true;
5350 if (finfo
.symstrtab
!= NULL
)
5351 _bfd_stringtab_free (finfo
.symstrtab
);
5352 if (finfo
.contents
!= NULL
)
5353 free (finfo
.contents
);
5354 if (finfo
.external_relocs
!= NULL
)
5355 free (finfo
.external_relocs
);
5356 if (finfo
.internal_relocs
!= NULL
)
5357 free (finfo
.internal_relocs
);
5358 if (finfo
.external_syms
!= NULL
)
5359 free (finfo
.external_syms
);
5360 if (finfo
.internal_syms
!= NULL
)
5361 free (finfo
.internal_syms
);
5362 if (finfo
.indices
!= NULL
)
5363 free (finfo
.indices
);
5364 if (finfo
.sections
!= NULL
)
5365 free (finfo
.sections
);
5366 if (finfo
.symbuf
!= NULL
)
5367 free (finfo
.symbuf
);
5368 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5370 if ((o
->flags
& SEC_RELOC
) != 0
5371 && elf_section_data (o
)->rel_hashes
!= NULL
)
5372 free (elf_section_data (o
)->rel_hashes
);
5378 /* Add a symbol to the output symbol table. */
5381 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
5382 struct elf_final_link_info
*finfo
;
5384 Elf_Internal_Sym
*elfsym
;
5385 asection
*input_sec
;
5387 boolean (*output_symbol_hook
) PARAMS ((bfd
*,
5388 struct bfd_link_info
*info
,
5393 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
5394 elf_backend_link_output_symbol_hook
;
5395 if (output_symbol_hook
!= NULL
)
5397 if (! ((*output_symbol_hook
)
5398 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
5402 if (name
== (const char *) NULL
|| *name
== '\0')
5403 elfsym
->st_name
= 0;
5404 else if (input_sec
->flags
& SEC_EXCLUDE
)
5405 elfsym
->st_name
= 0;
5408 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5410 if (elfsym
->st_name
== (unsigned long) -1)
5414 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5416 if (! elf_link_flush_output_syms (finfo
))
5420 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
,
5421 (PTR
) (finfo
->symbuf
+ finfo
->symbuf_count
));
5422 ++finfo
->symbuf_count
;
5424 ++ bfd_get_symcount (finfo
->output_bfd
);
5429 /* Flush the output symbols to the file. */
5432 elf_link_flush_output_syms (finfo
)
5433 struct elf_final_link_info
*finfo
;
5435 if (finfo
->symbuf_count
> 0)
5437 Elf_Internal_Shdr
*symtab
;
5441 symtab
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5442 pos
= symtab
->sh_offset
+ symtab
->sh_size
;
5443 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
5444 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5445 || bfd_bwrite ((PTR
) finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5448 symtab
->sh_size
+= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
5450 finfo
->symbuf_count
= 0;
5456 /* Adjust all external symbols pointing into SEC_MERGE sections
5457 to reflect the object merging within the sections. */
5460 elf_link_sec_merge_syms (h
, data
)
5461 struct elf_link_hash_entry
*h
;
5466 if ((h
->root
.type
== bfd_link_hash_defined
5467 || h
->root
.type
== bfd_link_hash_defweak
)
5468 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
5469 && elf_section_data (sec
)->merge_info
)
5471 bfd
*output_bfd
= (bfd
*) data
;
5473 h
->root
.u
.def
.value
=
5474 _bfd_merged_section_offset (output_bfd
,
5475 &h
->root
.u
.def
.section
,
5476 elf_section_data (sec
)->merge_info
,
5477 h
->root
.u
.def
.value
, (bfd_vma
) 0);
5483 /* Add an external symbol to the symbol table. This is called from
5484 the hash table traversal routine. When generating a shared object,
5485 we go through the symbol table twice. The first time we output
5486 anything that might have been forced to local scope in a version
5487 script. The second time we output the symbols that are still
5491 elf_link_output_extsym (h
, data
)
5492 struct elf_link_hash_entry
*h
;
5495 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
5496 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
5498 Elf_Internal_Sym sym
;
5499 asection
*input_sec
;
5501 /* Decide whether to output this symbol in this pass. */
5502 if (eoinfo
->localsyms
)
5504 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
5509 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
5513 /* If we are not creating a shared library, and this symbol is
5514 referenced by a shared library but is not defined anywhere, then
5515 warn that it is undefined. If we do not do this, the runtime
5516 linker will complain that the symbol is undefined when the
5517 program is run. We don't have to worry about symbols that are
5518 referenced by regular files, because we will already have issued
5519 warnings for them. */
5520 if (! finfo
->info
->relocateable
5521 && ! finfo
->info
->allow_shlib_undefined
5522 && ! finfo
->info
->shared
5523 && h
->root
.type
== bfd_link_hash_undefined
5524 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
5525 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
5527 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
5528 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
5529 (asection
*) NULL
, (bfd_vma
) 0, true)))
5531 eoinfo
->failed
= true;
5536 /* We don't want to output symbols that have never been mentioned by
5537 a regular file, or that we have been told to strip. However, if
5538 h->indx is set to -2, the symbol is used by a reloc and we must
5542 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
5543 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
5544 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
5545 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
5547 else if (finfo
->info
->strip
== strip_all
5548 || (finfo
->info
->strip
== strip_some
5549 && bfd_hash_lookup (finfo
->info
->keep_hash
,
5550 h
->root
.root
.string
,
5551 false, false) == NULL
))
5556 /* If we're stripping it, and it's not a dynamic symbol, there's
5557 nothing else to do unless it is a forced local symbol. */
5560 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
5564 sym
.st_size
= h
->size
;
5565 sym
.st_other
= h
->other
;
5566 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
5567 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
5568 else if (h
->root
.type
== bfd_link_hash_undefweak
5569 || h
->root
.type
== bfd_link_hash_defweak
)
5570 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
5572 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
5574 switch (h
->root
.type
)
5577 case bfd_link_hash_new
:
5581 case bfd_link_hash_undefined
:
5582 input_sec
= bfd_und_section_ptr
;
5583 sym
.st_shndx
= SHN_UNDEF
;
5586 case bfd_link_hash_undefweak
:
5587 input_sec
= bfd_und_section_ptr
;
5588 sym
.st_shndx
= SHN_UNDEF
;
5591 case bfd_link_hash_defined
:
5592 case bfd_link_hash_defweak
:
5594 input_sec
= h
->root
.u
.def
.section
;
5595 if (input_sec
->output_section
!= NULL
)
5598 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
5599 input_sec
->output_section
);
5600 if (sym
.st_shndx
== (unsigned short) -1)
5602 (*_bfd_error_handler
)
5603 (_("%s: could not find output section %s for input section %s"),
5604 bfd_get_filename (finfo
->output_bfd
),
5605 input_sec
->output_section
->name
,
5607 eoinfo
->failed
= true;
5611 /* ELF symbols in relocateable files are section relative,
5612 but in nonrelocateable files they are virtual
5614 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
5615 if (! finfo
->info
->relocateable
)
5616 sym
.st_value
+= input_sec
->output_section
->vma
;
5620 BFD_ASSERT (input_sec
->owner
== NULL
5621 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
5622 sym
.st_shndx
= SHN_UNDEF
;
5623 input_sec
= bfd_und_section_ptr
;
5628 case bfd_link_hash_common
:
5629 input_sec
= h
->root
.u
.c
.p
->section
;
5630 sym
.st_shndx
= SHN_COMMON
;
5631 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
5634 case bfd_link_hash_indirect
:
5635 /* These symbols are created by symbol versioning. They point
5636 to the decorated version of the name. For example, if the
5637 symbol foo@@GNU_1.2 is the default, which should be used when
5638 foo is used with no version, then we add an indirect symbol
5639 foo which points to foo@@GNU_1.2. We ignore these symbols,
5640 since the indirected symbol is already in the hash table. */
5643 case bfd_link_hash_warning
:
5644 /* We can't represent these symbols in ELF, although a warning
5645 symbol may have come from a .gnu.warning.SYMBOL section. We
5646 just put the target symbol in the hash table. If the target
5647 symbol does not really exist, don't do anything. */
5648 if (h
->root
.u
.i
.link
->type
== bfd_link_hash_new
)
5650 return (elf_link_output_extsym
5651 ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
));
5654 /* Give the processor backend a chance to tweak the symbol value,
5655 and also to finish up anything that needs to be done for this
5657 if ((h
->dynindx
!= -1
5658 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
5659 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
5661 struct elf_backend_data
*bed
;
5663 bed
= get_elf_backend_data (finfo
->output_bfd
);
5664 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
5665 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
5667 eoinfo
->failed
= true;
5672 /* If we are marking the symbol as undefined, and there are no
5673 non-weak references to this symbol from a regular object, then
5674 mark the symbol as weak undefined; if there are non-weak
5675 references, mark the symbol as strong. We can't do this earlier,
5676 because it might not be marked as undefined until the
5677 finish_dynamic_symbol routine gets through with it. */
5678 if (sym
.st_shndx
== SHN_UNDEF
5679 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
5680 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
5681 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
5685 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
5686 bindtype
= STB_GLOBAL
;
5688 bindtype
= STB_WEAK
;
5689 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
5692 /* If a symbol is not defined locally, we clear the visibility
5694 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
5695 sym
.st_other
^= ELF_ST_VISIBILITY (sym
.st_other
);
5697 /* If this symbol should be put in the .dynsym section, then put it
5698 there now. We have already know the symbol index. We also fill
5699 in the entry in the .hash section. */
5700 if (h
->dynindx
!= -1
5701 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
5705 size_t hash_entry_size
;
5706 bfd_byte
*bucketpos
;
5708 Elf_External_Sym
*esym
;
5710 sym
.st_name
= h
->dynstr_index
;
5711 esym
= (Elf_External_Sym
*) finfo
->dynsym_sec
->contents
+ h
->dynindx
;
5712 elf_swap_symbol_out (finfo
->output_bfd
, &sym
, (PTR
) esym
);
5714 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
5715 bucket
= h
->elf_hash_value
% bucketcount
;
5717 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
5718 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
5719 + (bucket
+ 2) * hash_entry_size
);
5720 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
5721 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, (bfd_vma
) h
->dynindx
,
5723 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
5724 ((bfd_byte
*) finfo
->hash_sec
->contents
5725 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
5727 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
5729 Elf_Internal_Versym iversym
;
5730 Elf_External_Versym
*eversym
;
5732 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
5734 if (h
->verinfo
.verdef
== NULL
)
5735 iversym
.vs_vers
= 0;
5737 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
5741 if (h
->verinfo
.vertree
== NULL
)
5742 iversym
.vs_vers
= 1;
5744 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
5747 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
5748 iversym
.vs_vers
|= VERSYM_HIDDEN
;
5750 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
5751 eversym
+= h
->dynindx
;
5752 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
5756 /* If we're stripping it, then it was just a dynamic symbol, and
5757 there's nothing else to do. */
5761 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
5763 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
5765 eoinfo
->failed
= true;
5772 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
5773 originated from the section given by INPUT_REL_HDR) to the
5777 elf_link_output_relocs (output_bfd
, input_section
, input_rel_hdr
,
5780 asection
*input_section
;
5781 Elf_Internal_Shdr
*input_rel_hdr
;
5782 Elf_Internal_Rela
*internal_relocs
;
5784 Elf_Internal_Rela
*irela
;
5785 Elf_Internal_Rela
*irelaend
;
5786 Elf_Internal_Shdr
*output_rel_hdr
;
5787 asection
*output_section
;
5788 unsigned int *rel_countp
= NULL
;
5789 struct elf_backend_data
*bed
;
5792 output_section
= input_section
->output_section
;
5793 output_rel_hdr
= NULL
;
5795 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
5796 == input_rel_hdr
->sh_entsize
)
5798 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
5799 rel_countp
= &elf_section_data (output_section
)->rel_count
;
5801 else if (elf_section_data (output_section
)->rel_hdr2
5802 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
5803 == input_rel_hdr
->sh_entsize
))
5805 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
5806 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
5809 BFD_ASSERT (output_rel_hdr
!= NULL
);
5811 bed
= get_elf_backend_data (output_bfd
);
5812 irela
= internal_relocs
;
5813 irelaend
= irela
+ NUM_SHDR_ENTRIES (input_rel_hdr
)
5814 * bed
->s
->int_rels_per_ext_rel
;
5816 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
5818 Elf_External_Rel
*erel
;
5819 Elf_Internal_Rel
*irel
;
5821 amt
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
5822 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (amt
);
5825 (*_bfd_error_handler
) (_("Error: out of memory"));
5829 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
+ *rel_countp
);
5830 for (; irela
< irelaend
; irela
+= bed
->s
->int_rels_per_ext_rel
, erel
++)
5834 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
5836 irel
[i
].r_offset
= irela
[i
].r_offset
;
5837 irel
[i
].r_info
= irela
[i
].r_info
;
5838 BFD_ASSERT (irela
[i
].r_addend
== 0);
5841 if (bed
->s
->swap_reloc_out
)
5842 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, (PTR
) erel
);
5844 elf_swap_reloc_out (output_bfd
, irel
, erel
);
5851 Elf_External_Rela
*erela
;
5853 BFD_ASSERT (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
));
5855 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
+ *rel_countp
);
5856 for (; irela
< irelaend
; irela
+= bed
->s
->int_rels_per_ext_rel
, erela
++)
5857 if (bed
->s
->swap_reloca_out
)
5858 (*bed
->s
->swap_reloca_out
) (output_bfd
, irela
, (PTR
) erela
);
5860 elf_swap_reloca_out (output_bfd
, irela
, erela
);
5863 /* Bump the counter, so that we know where to add the next set of
5865 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
5868 /* Link an input file into the linker output file. This function
5869 handles all the sections and relocations of the input file at once.
5870 This is so that we only have to read the local symbols once, and
5871 don't have to keep them in memory. */
5874 elf_link_input_bfd (finfo
, input_bfd
)
5875 struct elf_final_link_info
*finfo
;
5878 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
5879 bfd
*, asection
*, bfd_byte
*,
5880 Elf_Internal_Rela
*,
5881 Elf_Internal_Sym
*, asection
**));
5883 Elf_Internal_Shdr
*symtab_hdr
;
5886 Elf_External_Sym
*external_syms
;
5887 Elf_External_Sym
*esym
;
5888 Elf_External_Sym
*esymend
;
5889 Elf_Internal_Sym
*isym
;
5891 asection
**ppsection
;
5893 struct elf_backend_data
*bed
;
5894 boolean emit_relocs
;
5896 output_bfd
= finfo
->output_bfd
;
5897 bed
= get_elf_backend_data (output_bfd
);
5898 relocate_section
= bed
->elf_backend_relocate_section
;
5900 /* If this is a dynamic object, we don't want to do anything here:
5901 we don't want the local symbols, and we don't want the section
5903 if ((input_bfd
->flags
& DYNAMIC
) != 0)
5906 emit_relocs
= (finfo
->info
->relocateable
5907 || finfo
->info
->emitrelocations
5908 || bed
->elf_backend_emit_relocs
);
5910 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
5911 if (elf_bad_symtab (input_bfd
))
5913 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
5918 locsymcount
= symtab_hdr
->sh_info
;
5919 extsymoff
= symtab_hdr
->sh_info
;
5922 /* Read the local symbols. */
5923 if (symtab_hdr
->contents
!= NULL
)
5924 external_syms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
5925 else if (locsymcount
== 0)
5926 external_syms
= NULL
;
5929 bfd_size_type amt
= locsymcount
* sizeof (Elf_External_Sym
);
5930 external_syms
= finfo
->external_syms
;
5931 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
5932 || bfd_bread (external_syms
, amt
, input_bfd
) != amt
)
5936 /* Swap in the local symbols and write out the ones which we know
5937 are going into the output file. */
5938 esym
= external_syms
;
5939 esymend
= esym
+ locsymcount
;
5940 isym
= finfo
->internal_syms
;
5941 pindex
= finfo
->indices
;
5942 ppsection
= finfo
->sections
;
5943 for (; esym
< esymend
; esym
++, isym
++, pindex
++, ppsection
++)
5947 Elf_Internal_Sym osym
;
5949 elf_swap_symbol_in (input_bfd
, esym
, isym
);
5952 if (elf_bad_symtab (input_bfd
))
5954 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
5962 if (isym
->st_shndx
== SHN_UNDEF
)
5964 isec
= bfd_und_section_ptr
;
5967 else if (isym
->st_shndx
> 0 && isym
->st_shndx
< SHN_LORESERVE
)
5969 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
5970 if (isec
&& elf_section_data (isec
)->merge_info
5971 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
5973 _bfd_merged_section_offset (output_bfd
, &isec
,
5974 elf_section_data (isec
)->merge_info
,
5975 isym
->st_value
, (bfd_vma
) 0);
5977 else if (isym
->st_shndx
== SHN_ABS
)
5979 isec
= bfd_abs_section_ptr
;
5982 else if (isym
->st_shndx
== SHN_COMMON
)
5984 isec
= bfd_com_section_ptr
;
5995 /* Don't output the first, undefined, symbol. */
5996 if (esym
== external_syms
)
5999 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6003 /* Save away all section symbol values. */
6008 if (isec
->symbol
->value
!= isym
->st_value
)
6009 (*_bfd_error_handler
)
6010 (_("%s: invalid section symbol index 0x%x (%s) ignored"),
6011 bfd_archive_filename (input_bfd
), isym
->st_shndx
,
6015 isec
->symbol
->value
= isym
->st_value
;
6018 /* If this is a discarded link-once section symbol, update
6019 it's value to that of the kept section symbol. The
6020 linker will keep the first of any matching link-once
6021 sections, so we should have already seen it's section
6022 symbol. I trust no-one will have the bright idea of
6023 re-ordering the bfd list... */
6025 && (bfd_get_section_flags (input_bfd
, isec
) & SEC_LINK_ONCE
) != 0
6026 && (ksec
= isec
->kept_section
) != NULL
)
6028 isym
->st_value
= ksec
->symbol
->value
;
6030 /* That put the value right, but the section info is all
6031 wrong. I hope this works. */
6032 isec
->output_offset
= ksec
->output_offset
;
6033 isec
->output_section
= ksec
->output_section
;
6036 /* We never output section symbols. Instead, we use the
6037 section symbol of the corresponding section in the output
6042 /* If we are stripping all symbols, we don't want to output this
6044 if (finfo
->info
->strip
== strip_all
)
6047 /* If we are discarding all local symbols, we don't want to
6048 output this one. If we are generating a relocateable output
6049 file, then some of the local symbols may be required by
6050 relocs; we output them below as we discover that they are
6052 if (finfo
->info
->discard
== discard_all
)
6055 /* If this symbol is defined in a section which we are
6056 discarding, we don't need to keep it, but note that
6057 linker_mark is only reliable for sections that have contents.
6058 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6059 as well as linker_mark. */
6060 if (isym
->st_shndx
> 0
6061 && isym
->st_shndx
< SHN_LORESERVE
6063 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6064 || (! finfo
->info
->relocateable
6065 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6068 /* Get the name of the symbol. */
6069 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6074 /* See if we are discarding symbols with this name. */
6075 if ((finfo
->info
->strip
== strip_some
6076 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
6078 || (((finfo
->info
->discard
== discard_sec_merge
6079 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocateable
)
6080 || finfo
->info
->discard
== discard_l
)
6081 && bfd_is_local_label_name (input_bfd
, name
)))
6084 /* If we get here, we are going to output this symbol. */
6088 /* Adjust the section index for the output file. */
6089 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6090 isec
->output_section
);
6091 if (osym
.st_shndx
== (unsigned short) -1)
6094 *pindex
= bfd_get_symcount (output_bfd
);
6096 /* ELF symbols in relocateable files are section relative, but
6097 in executable files they are virtual addresses. Note that
6098 this code assumes that all ELF sections have an associated
6099 BFD section with a reasonable value for output_offset; below
6100 we assume that they also have a reasonable value for
6101 output_section. Any special sections must be set up to meet
6102 these requirements. */
6103 osym
.st_value
+= isec
->output_offset
;
6104 if (! finfo
->info
->relocateable
)
6105 osym
.st_value
+= isec
->output_section
->vma
;
6107 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
6111 /* Relocate the contents of each section. */
6112 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6116 if (! o
->linker_mark
)
6118 /* This section was omitted from the link. */
6122 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6123 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6126 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6128 /* Section was created by elf_link_create_dynamic_sections
6133 /* Get the contents of the section. They have been cached by a
6134 relaxation routine. Note that o is a section in an input
6135 file, so the contents field will not have been set by any of
6136 the routines which work on output files. */
6137 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6138 contents
= elf_section_data (o
)->this_hdr
.contents
;
6141 contents
= finfo
->contents
;
6142 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
6143 (file_ptr
) 0, o
->_raw_size
))
6147 if ((o
->flags
& SEC_RELOC
) != 0)
6149 Elf_Internal_Rela
*internal_relocs
;
6151 /* Get the swapped relocs. */
6152 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
6153 (input_bfd
, o
, finfo
->external_relocs
,
6154 finfo
->internal_relocs
, false));
6155 if (internal_relocs
== NULL
6156 && o
->reloc_count
> 0)
6159 /* Relocate the section by invoking a back end routine.
6161 The back end routine is responsible for adjusting the
6162 section contents as necessary, and (if using Rela relocs
6163 and generating a relocateable output file) adjusting the
6164 reloc addend as necessary.
6166 The back end routine does not have to worry about setting
6167 the reloc address or the reloc symbol index.
6169 The back end routine is given a pointer to the swapped in
6170 internal symbols, and can access the hash table entries
6171 for the external symbols via elf_sym_hashes (input_bfd).
6173 When generating relocateable output, the back end routine
6174 must handle STB_LOCAL/STT_SECTION symbols specially. The
6175 output symbol is going to be a section symbol
6176 corresponding to the output section, which will require
6177 the addend to be adjusted. */
6179 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6180 input_bfd
, o
, contents
,
6182 finfo
->internal_syms
,
6188 Elf_Internal_Rela
*irela
;
6189 Elf_Internal_Rela
*irelaend
;
6190 struct elf_link_hash_entry
**rel_hash
;
6191 Elf_Internal_Shdr
*input_rel_hdr
;
6192 unsigned int next_erel
;
6193 void (*reloc_emitter
) PARAMS ((bfd
*, asection
*,
6194 Elf_Internal_Shdr
*,
6195 Elf_Internal_Rela
*));
6197 /* Adjust the reloc addresses and symbol indices. */
6199 irela
= internal_relocs
;
6200 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6201 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6202 + elf_section_data (o
->output_section
)->rel_count
6203 + elf_section_data (o
->output_section
)->rel_count2
);
6204 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6206 unsigned long r_symndx
;
6209 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6215 irela
->r_offset
+= o
->output_offset
;
6217 /* Relocs in an executable have to be virtual addresses. */
6218 if (finfo
->info
->emitrelocations
)
6219 irela
->r_offset
+= o
->output_section
->vma
;
6221 r_symndx
= ELF_R_SYM (irela
->r_info
);
6226 if (r_symndx
>= locsymcount
6227 || (elf_bad_symtab (input_bfd
)
6228 && finfo
->sections
[r_symndx
] == NULL
))
6230 struct elf_link_hash_entry
*rh
;
6233 /* This is a reloc against a global symbol. We
6234 have not yet output all the local symbols, so
6235 we do not know the symbol index of any global
6236 symbol. We set the rel_hash entry for this
6237 reloc to point to the global hash table entry
6238 for this symbol. The symbol index is then
6239 set at the end of elf_bfd_final_link. */
6240 indx
= r_symndx
- extsymoff
;
6241 rh
= elf_sym_hashes (input_bfd
)[indx
];
6242 while (rh
->root
.type
== bfd_link_hash_indirect
6243 || rh
->root
.type
== bfd_link_hash_warning
)
6244 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6246 /* Setting the index to -2 tells
6247 elf_link_output_extsym that this symbol is
6249 BFD_ASSERT (rh
->indx
< 0);
6257 /* This is a reloc against a local symbol. */
6260 isym
= finfo
->internal_syms
+ r_symndx
;
6261 sec
= finfo
->sections
[r_symndx
];
6262 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6264 /* I suppose the backend ought to fill in the
6265 section of any STT_SECTION symbol against a
6266 processor specific section. If we have
6267 discarded a section, the output_section will
6268 be the absolute section. */
6270 && (bfd_is_abs_section (sec
)
6271 || (sec
->output_section
!= NULL
6272 && bfd_is_abs_section (sec
->output_section
))))
6274 else if (sec
== NULL
|| sec
->owner
== NULL
)
6276 bfd_set_error (bfd_error_bad_value
);
6281 r_symndx
= sec
->output_section
->target_index
;
6282 BFD_ASSERT (r_symndx
!= 0);
6287 if (finfo
->indices
[r_symndx
] == -1)
6289 unsigned long shlink
;
6293 if (finfo
->info
->strip
== strip_all
)
6295 /* You can't do ld -r -s. */
6296 bfd_set_error (bfd_error_invalid_operation
);
6300 /* This symbol was skipped earlier, but
6301 since it is needed by a reloc, we
6302 must output it now. */
6303 shlink
= symtab_hdr
->sh_link
;
6304 name
= (bfd_elf_string_from_elf_section
6305 (input_bfd
, shlink
, isym
->st_name
));
6309 osec
= sec
->output_section
;
6311 _bfd_elf_section_from_bfd_section (output_bfd
,
6313 if (isym
->st_shndx
== (unsigned short) -1)
6316 isym
->st_value
+= sec
->output_offset
;
6317 if (! finfo
->info
->relocateable
)
6318 isym
->st_value
+= osec
->vma
;
6320 finfo
->indices
[r_symndx
]
6321 = bfd_get_symcount (output_bfd
);
6323 if (! elf_link_output_sym (finfo
, name
, isym
, sec
))
6327 r_symndx
= finfo
->indices
[r_symndx
];
6330 irela
->r_info
= ELF_R_INFO (r_symndx
,
6331 ELF_R_TYPE (irela
->r_info
));
6334 /* Swap out the relocs. */
6335 if (bed
->elf_backend_emit_relocs
6336 && !(finfo
->info
->relocateable
6337 || finfo
->info
->emitrelocations
))
6338 reloc_emitter
= bed
->elf_backend_emit_relocs
;
6340 reloc_emitter
= elf_link_output_relocs
;
6342 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6343 (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
, internal_relocs
);
6345 input_rel_hdr
= elf_section_data (o
)->rel_hdr2
;
6348 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
6349 * bed
->s
->int_rels_per_ext_rel
);
6350 reloc_emitter (output_bfd
, o
, input_rel_hdr
, internal_relocs
);
6356 /* Write out the modified section contents. */
6357 if (elf_section_data (o
)->stab_info
)
6359 if (! (_bfd_write_section_stabs
6360 (output_bfd
, &elf_hash_table (finfo
->info
)->stab_info
,
6361 o
, &elf_section_data (o
)->stab_info
, contents
)))
6364 else if (elf_section_data (o
)->merge_info
)
6366 if (! (_bfd_write_merged_section
6367 (output_bfd
, o
, elf_section_data (o
)->merge_info
)))
6372 bfd_size_type sec_size
;
6374 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
6375 if (! (o
->flags
& SEC_EXCLUDE
)
6376 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
6378 (file_ptr
) o
->output_offset
,
6387 /* Generate a reloc when linking an ELF file. This is a reloc
6388 requested by the linker, and does come from any input file. This
6389 is used to build constructor and destructor tables when linking
6393 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
6395 struct bfd_link_info
*info
;
6396 asection
*output_section
;
6397 struct bfd_link_order
*link_order
;
6399 reloc_howto_type
*howto
;
6403 struct elf_link_hash_entry
**rel_hash_ptr
;
6404 Elf_Internal_Shdr
*rel_hdr
;
6405 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
6407 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
6410 bfd_set_error (bfd_error_bad_value
);
6414 addend
= link_order
->u
.reloc
.p
->addend
;
6416 /* Figure out the symbol index. */
6417 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
6418 + elf_section_data (output_section
)->rel_count
6419 + elf_section_data (output_section
)->rel_count2
);
6420 if (link_order
->type
== bfd_section_reloc_link_order
)
6422 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
6423 BFD_ASSERT (indx
!= 0);
6424 *rel_hash_ptr
= NULL
;
6428 struct elf_link_hash_entry
*h
;
6430 /* Treat a reloc against a defined symbol as though it were
6431 actually against the section. */
6432 h
= ((struct elf_link_hash_entry
*)
6433 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
6434 link_order
->u
.reloc
.p
->u
.name
,
6435 false, false, true));
6437 && (h
->root
.type
== bfd_link_hash_defined
6438 || h
->root
.type
== bfd_link_hash_defweak
))
6442 section
= h
->root
.u
.def
.section
;
6443 indx
= section
->output_section
->target_index
;
6444 *rel_hash_ptr
= NULL
;
6445 /* It seems that we ought to add the symbol value to the
6446 addend here, but in practice it has already been added
6447 because it was passed to constructor_callback. */
6448 addend
+= section
->output_section
->vma
+ section
->output_offset
;
6452 /* Setting the index to -2 tells elf_link_output_extsym that
6453 this symbol is used by a reloc. */
6460 if (! ((*info
->callbacks
->unattached_reloc
)
6461 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
6462 (asection
*) NULL
, (bfd_vma
) 0)))
6468 /* If this is an inplace reloc, we must write the addend into the
6470 if (howto
->partial_inplace
&& addend
!= 0)
6473 bfd_reloc_status_type rstat
;
6476 const char *sym_name
;
6478 size
= bfd_get_reloc_size (howto
);
6479 buf
= (bfd_byte
*) bfd_zmalloc (size
);
6480 if (buf
== (bfd_byte
*) NULL
)
6482 rstat
= _bfd_relocate_contents (howto
, output_bfd
, (bfd_vma
) addend
, buf
);
6489 case bfd_reloc_outofrange
:
6492 case bfd_reloc_overflow
:
6493 if (link_order
->type
== bfd_section_reloc_link_order
)
6494 sym_name
= bfd_section_name (output_bfd
,
6495 link_order
->u
.reloc
.p
->u
.section
);
6497 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
6498 if (! ((*info
->callbacks
->reloc_overflow
)
6499 (info
, sym_name
, howto
->name
, addend
,
6500 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
6507 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
6508 (file_ptr
) link_order
->offset
, size
);
6514 /* The address of a reloc is relative to the section in a
6515 relocateable file, and is a virtual address in an executable
6517 offset
= link_order
->offset
;
6518 if (! info
->relocateable
)
6519 offset
+= output_section
->vma
;
6521 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
6523 if (rel_hdr
->sh_type
== SHT_REL
)
6526 Elf_Internal_Rel
*irel
;
6527 Elf_External_Rel
*erel
;
6530 size
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
6531 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (size
);
6535 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
6536 irel
[i
].r_offset
= offset
;
6537 irel
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
6539 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
6540 + elf_section_data (output_section
)->rel_count
);
6542 if (bed
->s
->swap_reloc_out
)
6543 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, (bfd_byte
*) erel
);
6545 elf_swap_reloc_out (output_bfd
, irel
, erel
);
6552 Elf_Internal_Rela
*irela
;
6553 Elf_External_Rela
*erela
;
6556 size
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
6557 irela
= (Elf_Internal_Rela
*) bfd_zmalloc (size
);
6561 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
6562 irela
[i
].r_offset
= offset
;
6563 irela
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
6564 irela
[0].r_addend
= addend
;
6566 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
6567 + elf_section_data (output_section
)->rel_count
);
6569 if (bed
->s
->swap_reloca_out
)
6570 (*bed
->s
->swap_reloca_out
) (output_bfd
, irela
, (bfd_byte
*) erela
);
6572 elf_swap_reloca_out (output_bfd
, irela
, erela
);
6575 ++elf_section_data (output_section
)->rel_count
;
6580 /* Allocate a pointer to live in a linker created section. */
6583 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
6585 struct bfd_link_info
*info
;
6586 elf_linker_section_t
*lsect
;
6587 struct elf_link_hash_entry
*h
;
6588 const Elf_Internal_Rela
*rel
;
6590 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
6591 elf_linker_section_pointers_t
*linker_section_ptr
;
6592 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
6595 BFD_ASSERT (lsect
!= NULL
);
6597 /* Is this a global symbol? */
6600 /* Has this symbol already been allocated? If so, our work is done. */
6601 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
6606 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
6607 /* Make sure this symbol is output as a dynamic symbol. */
6608 if (h
->dynindx
== -1)
6610 if (! elf_link_record_dynamic_symbol (info
, h
))
6614 if (lsect
->rel_section
)
6615 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
6619 /* Allocation of a pointer to a local symbol. */
6620 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
6622 /* Allocate a table to hold the local symbols if first time. */
6625 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
6626 register unsigned int i
;
6629 amt
*= sizeof (elf_linker_section_pointers_t
*);
6630 ptr
= (elf_linker_section_pointers_t
**) bfd_alloc (abfd
, amt
);
6635 elf_local_ptr_offsets (abfd
) = ptr
;
6636 for (i
= 0; i
< num_symbols
; i
++)
6637 ptr
[i
] = (elf_linker_section_pointers_t
*) 0;
6640 /* Has this symbol already been allocated? If so, our work is done. */
6641 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
6646 ptr_linker_section_ptr
= &ptr
[r_symndx
];
6650 /* If we are generating a shared object, we need to
6651 output a R_<xxx>_RELATIVE reloc so that the
6652 dynamic linker can adjust this GOT entry. */
6653 BFD_ASSERT (lsect
->rel_section
!= NULL
);
6654 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
6658 /* Allocate space for a pointer in the linker section, and allocate
6659 a new pointer record from internal memory. */
6660 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
6661 amt
= sizeof (elf_linker_section_pointers_t
);
6662 linker_section_ptr
= (elf_linker_section_pointers_t
*) bfd_alloc (abfd
, amt
);
6664 if (!linker_section_ptr
)
6667 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
6668 linker_section_ptr
->addend
= rel
->r_addend
;
6669 linker_section_ptr
->which
= lsect
->which
;
6670 linker_section_ptr
->written_address_p
= false;
6671 *ptr_linker_section_ptr
= linker_section_ptr
;
6674 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
6676 linker_section_ptr
->offset
= (lsect
->section
->_raw_size
6677 - lsect
->hole_size
+ (ARCH_SIZE
/ 8));
6678 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
6679 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
6680 if (lsect
->sym_hash
)
6682 /* Bump up symbol value if needed. */
6683 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
6685 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
6686 lsect
->sym_hash
->root
.root
.string
,
6687 (long) ARCH_SIZE
/ 8,
6688 (long) lsect
->sym_hash
->root
.u
.def
.value
);
6694 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
6696 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
6700 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
6701 lsect
->name
, (long) linker_section_ptr
->offset
,
6702 (long) lsect
->section
->_raw_size
);
6709 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
6712 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
6715 /* Fill in the address for a pointer generated in a linker section. */
6718 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
,
6719 relocation
, rel
, relative_reloc
)
6722 struct bfd_link_info
*info
;
6723 elf_linker_section_t
*lsect
;
6724 struct elf_link_hash_entry
*h
;
6726 const Elf_Internal_Rela
*rel
;
6729 elf_linker_section_pointers_t
*linker_section_ptr
;
6731 BFD_ASSERT (lsect
!= NULL
);
6735 /* Handle global symbol. */
6736 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
6737 (h
->linker_section_pointer
,
6741 BFD_ASSERT (linker_section_ptr
!= NULL
);
6743 if (! elf_hash_table (info
)->dynamic_sections_created
6746 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
6748 /* This is actually a static link, or it is a
6749 -Bsymbolic link and the symbol is defined
6750 locally. We must initialize this entry in the
6753 When doing a dynamic link, we create a .rela.<xxx>
6754 relocation entry to initialize the value. This
6755 is done in the finish_dynamic_symbol routine. */
6756 if (!linker_section_ptr
->written_address_p
)
6758 linker_section_ptr
->written_address_p
= true;
6759 bfd_put_ptr (output_bfd
,
6760 relocation
+ linker_section_ptr
->addend
,
6761 (lsect
->section
->contents
6762 + linker_section_ptr
->offset
));
6768 /* Handle local symbol. */
6769 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
6770 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
6771 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
6772 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
6773 (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
6777 BFD_ASSERT (linker_section_ptr
!= NULL
);
6779 /* Write out pointer if it hasn't been rewritten out before. */
6780 if (!linker_section_ptr
->written_address_p
)
6782 linker_section_ptr
->written_address_p
= true;
6783 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
6784 lsect
->section
->contents
+ linker_section_ptr
->offset
);
6788 asection
*srel
= lsect
->rel_section
;
6789 Elf_Internal_Rela
*outrel
;
6790 Elf_External_Rela
*erel
;
6791 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
6795 amt
= sizeof (Elf_Internal_Rela
) * bed
->s
->int_rels_per_ext_rel
;
6796 outrel
= (Elf_Internal_Rela
*) bfd_zmalloc (amt
);
6799 (*_bfd_error_handler
) (_("Error: out of memory"));
6803 /* We need to generate a relative reloc for the dynamic
6807 srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
6809 lsect
->rel_section
= srel
;
6812 BFD_ASSERT (srel
!= NULL
);
6814 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
6815 outrel
[i
].r_offset
= (lsect
->section
->output_section
->vma
6816 + lsect
->section
->output_offset
6817 + linker_section_ptr
->offset
);
6818 outrel
[0].r_info
= ELF_R_INFO (0, relative_reloc
);
6819 outrel
[0].r_addend
= 0;
6820 erel
= (Elf_External_Rela
*) lsect
->section
->contents
;
6821 erel
+= elf_section_data (lsect
->section
)->rel_count
;
6822 elf_swap_reloca_out (output_bfd
, outrel
, erel
);
6823 ++elf_section_data (lsect
->section
)->rel_count
;
6830 relocation
= (lsect
->section
->output_offset
6831 + linker_section_ptr
->offset
6832 - lsect
->hole_offset
6833 - lsect
->sym_offset
);
6837 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
6838 lsect
->name
, (long) relocation
, (long) relocation
);
6841 /* Subtract out the addend, because it will get added back in by the normal
6843 return relocation
- linker_section_ptr
->addend
;
6846 /* Garbage collect unused sections. */
6848 static boolean elf_gc_mark
6849 PARAMS ((struct bfd_link_info
*info
, asection
*sec
,
6850 asection
* (*gc_mark_hook
)
6851 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
6852 struct elf_link_hash_entry
*, Elf_Internal_Sym
*))));
6854 static boolean elf_gc_sweep
6855 PARAMS ((struct bfd_link_info
*info
,
6856 boolean (*gc_sweep_hook
)
6857 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
6858 const Elf_Internal_Rela
*relocs
))));
6860 static boolean elf_gc_sweep_symbol
6861 PARAMS ((struct elf_link_hash_entry
*h
, PTR idxptr
));
6863 static boolean elf_gc_allocate_got_offsets
6864 PARAMS ((struct elf_link_hash_entry
*h
, PTR offarg
));
6866 static boolean elf_gc_propagate_vtable_entries_used
6867 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
6869 static boolean elf_gc_smash_unused_vtentry_relocs
6870 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
6872 /* The mark phase of garbage collection. For a given section, mark
6873 it, and all the sections which define symbols to which it refers. */
6876 elf_gc_mark (info
, sec
, gc_mark_hook
)
6877 struct bfd_link_info
*info
;
6879 asection
* (*gc_mark_hook
)
6880 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
6881 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
6887 /* Look through the section relocs. */
6889 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
6891 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
6892 Elf_Internal_Shdr
*symtab_hdr
;
6893 struct elf_link_hash_entry
**sym_hashes
;
6896 Elf_External_Sym
*locsyms
, *freesyms
= NULL
;
6897 bfd
*input_bfd
= sec
->owner
;
6898 struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
6900 /* GCFIXME: how to arrange so that relocs and symbols are not
6901 reread continually? */
6903 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6904 sym_hashes
= elf_sym_hashes (input_bfd
);
6906 /* Read the local symbols. */
6907 if (elf_bad_symtab (input_bfd
))
6909 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6913 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
6914 if (symtab_hdr
->contents
)
6915 locsyms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
6916 else if (nlocsyms
== 0)
6920 bfd_size_type amt
= nlocsyms
* sizeof (Elf_External_Sym
);
6921 locsyms
= freesyms
= bfd_malloc (amt
);
6922 if (freesyms
== NULL
6923 || bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
6924 || bfd_bread (locsyms
, amt
, input_bfd
) != amt
)
6931 /* Read the relocations. */
6932 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
6933 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
6934 info
->keep_memory
));
6935 if (relstart
== NULL
)
6940 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6942 for (rel
= relstart
; rel
< relend
; rel
++)
6944 unsigned long r_symndx
;
6946 struct elf_link_hash_entry
*h
;
6949 r_symndx
= ELF_R_SYM (rel
->r_info
);
6953 if (elf_bad_symtab (sec
->owner
))
6955 elf_swap_symbol_in (input_bfd
, &locsyms
[r_symndx
], &s
);
6956 if (ELF_ST_BIND (s
.st_info
) == STB_LOCAL
)
6957 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, NULL
, &s
);
6960 h
= sym_hashes
[r_symndx
- extsymoff
];
6961 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, h
, NULL
);
6964 else if (r_symndx
>= nlocsyms
)
6966 h
= sym_hashes
[r_symndx
- extsymoff
];
6967 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, h
, NULL
);
6971 elf_swap_symbol_in (input_bfd
, &locsyms
[r_symndx
], &s
);
6972 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, NULL
, &s
);
6975 if (rsec
&& !rsec
->gc_mark
)
6976 if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
6984 if (!info
->keep_memory
)
6994 /* The sweep phase of garbage collection. Remove all garbage sections. */
6997 elf_gc_sweep (info
, gc_sweep_hook
)
6998 struct bfd_link_info
*info
;
6999 boolean (*gc_sweep_hook
)
7000 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
7001 const Elf_Internal_Rela
*relocs
));
7005 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7009 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
7012 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7014 /* Keep special sections. Keep .debug sections. */
7015 if ((o
->flags
& SEC_LINKER_CREATED
)
7016 || (o
->flags
& SEC_DEBUGGING
))
7022 /* Skip sweeping sections already excluded. */
7023 if (o
->flags
& SEC_EXCLUDE
)
7026 /* Since this is early in the link process, it is simple
7027 to remove a section from the output. */
7028 o
->flags
|= SEC_EXCLUDE
;
7030 /* But we also have to update some of the relocation
7031 info we collected before. */
7033 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
7035 Elf_Internal_Rela
*internal_relocs
;
7038 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
7039 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
7040 if (internal_relocs
== NULL
)
7043 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
7045 if (!info
->keep_memory
)
7046 free (internal_relocs
);
7054 /* Remove the symbols that were in the swept sections from the dynamic
7055 symbol table. GCFIXME: Anyone know how to get them out of the
7056 static symbol table as well? */
7060 elf_link_hash_traverse (elf_hash_table (info
),
7061 elf_gc_sweep_symbol
,
7064 elf_hash_table (info
)->dynsymcount
= i
;
7070 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7073 elf_gc_sweep_symbol (h
, idxptr
)
7074 struct elf_link_hash_entry
*h
;
7077 int *idx
= (int *) idxptr
;
7079 if (h
->dynindx
!= -1
7080 && ((h
->root
.type
!= bfd_link_hash_defined
7081 && h
->root
.type
!= bfd_link_hash_defweak
)
7082 || h
->root
.u
.def
.section
->gc_mark
))
7083 h
->dynindx
= (*idx
)++;
7088 /* Propogate collected vtable information. This is called through
7089 elf_link_hash_traverse. */
7092 elf_gc_propagate_vtable_entries_used (h
, okp
)
7093 struct elf_link_hash_entry
*h
;
7096 /* Those that are not vtables. */
7097 if (h
->vtable_parent
== NULL
)
7100 /* Those vtables that do not have parents, we cannot merge. */
7101 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
7104 /* If we've already been done, exit. */
7105 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
7108 /* Make sure the parent's table is up to date. */
7109 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
7111 if (h
->vtable_entries_used
== NULL
)
7113 /* None of this table's entries were referenced. Re-use the
7115 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
7116 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
7123 /* Or the parent's entries into ours. */
7124 cu
= h
->vtable_entries_used
;
7126 pu
= h
->vtable_parent
->vtable_entries_used
;
7129 asection
*sec
= h
->root
.u
.def
.section
;
7130 struct elf_backend_data
*bed
= get_elf_backend_data (sec
->owner
);
7131 int file_align
= bed
->s
->file_align
;
7133 n
= h
->vtable_parent
->vtable_entries_size
/ file_align
;
7148 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
7149 struct elf_link_hash_entry
*h
;
7153 bfd_vma hstart
, hend
;
7154 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
7155 struct elf_backend_data
*bed
;
7158 /* Take care of both those symbols that do not describe vtables as
7159 well as those that are not loaded. */
7160 if (h
->vtable_parent
== NULL
)
7163 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
7164 || h
->root
.type
== bfd_link_hash_defweak
);
7166 sec
= h
->root
.u
.def
.section
;
7167 hstart
= h
->root
.u
.def
.value
;
7168 hend
= hstart
+ h
->size
;
7170 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
7171 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, true));
7173 return *(boolean
*) okp
= false;
7174 bed
= get_elf_backend_data (sec
->owner
);
7175 file_align
= bed
->s
->file_align
;
7177 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7179 for (rel
= relstart
; rel
< relend
; ++rel
)
7180 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
7182 /* If the entry is in use, do nothing. */
7183 if (h
->vtable_entries_used
7184 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
7186 bfd_vma entry
= (rel
->r_offset
- hstart
) / file_align
;
7187 if (h
->vtable_entries_used
[entry
])
7190 /* Otherwise, kill it. */
7191 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
7197 /* Do mark and sweep of unused sections. */
7200 elf_gc_sections (abfd
, info
)
7202 struct bfd_link_info
*info
;
7206 asection
* (*gc_mark_hook
)
7207 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7208 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
7210 if (!get_elf_backend_data (abfd
)->can_gc_sections
7211 || info
->relocateable
|| info
->emitrelocations
7212 || elf_hash_table (info
)->dynamic_sections_created
)
7215 /* Apply transitive closure to the vtable entry usage info. */
7216 elf_link_hash_traverse (elf_hash_table (info
),
7217 elf_gc_propagate_vtable_entries_used
,
7222 /* Kill the vtable relocations that were not used. */
7223 elf_link_hash_traverse (elf_hash_table (info
),
7224 elf_gc_smash_unused_vtentry_relocs
,
7229 /* Grovel through relocs to find out who stays ... */
7231 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
7232 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7236 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
7239 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7241 if (o
->flags
& SEC_KEEP
)
7242 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
7247 /* ... and mark SEC_EXCLUDE for those that go. */
7248 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
7254 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
7257 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
7260 struct elf_link_hash_entry
*h
;
7263 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
7264 struct elf_link_hash_entry
**search
, *child
;
7265 bfd_size_type extsymcount
;
7267 /* The sh_info field of the symtab header tells us where the
7268 external symbols start. We don't care about the local symbols at
7270 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
7271 if (!elf_bad_symtab (abfd
))
7272 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
7274 sym_hashes
= elf_sym_hashes (abfd
);
7275 sym_hashes_end
= sym_hashes
+ extsymcount
;
7277 /* Hunt down the child symbol, which is in this section at the same
7278 offset as the relocation. */
7279 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
7281 if ((child
= *search
) != NULL
7282 && (child
->root
.type
== bfd_link_hash_defined
7283 || child
->root
.type
== bfd_link_hash_defweak
)
7284 && child
->root
.u
.def
.section
== sec
7285 && child
->root
.u
.def
.value
== offset
)
7289 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
7290 bfd_archive_filename (abfd
), sec
->name
,
7291 (unsigned long) offset
);
7292 bfd_set_error (bfd_error_invalid_operation
);
7298 /* This *should* only be the absolute section. It could potentially
7299 be that someone has defined a non-global vtable though, which
7300 would be bad. It isn't worth paging in the local symbols to be
7301 sure though; that case should simply be handled by the assembler. */
7303 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
7306 child
->vtable_parent
= h
;
7311 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
7314 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
7315 bfd
*abfd ATTRIBUTE_UNUSED
;
7316 asection
*sec ATTRIBUTE_UNUSED
;
7317 struct elf_link_hash_entry
*h
;
7320 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7321 int file_align
= bed
->s
->file_align
;
7323 if (addend
>= h
->vtable_entries_size
)
7326 boolean
*ptr
= h
->vtable_entries_used
;
7328 /* While the symbol is undefined, we have to be prepared to handle
7330 if (h
->root
.type
== bfd_link_hash_undefined
)
7337 /* Oops! We've got a reference past the defined end of
7338 the table. This is probably a bug -- shall we warn? */
7343 /* Allocate one extra entry for use as a "done" flag for the
7344 consolidation pass. */
7345 bytes
= (size
/ file_align
+ 1) * sizeof (boolean
);
7349 ptr
= bfd_realloc (ptr
- 1, (bfd_size_type
) bytes
);
7355 oldbytes
= ((h
->vtable_entries_size
/ file_align
+ 1)
7356 * sizeof (boolean
));
7357 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
7361 ptr
= bfd_zmalloc ((bfd_size_type
) bytes
);
7366 /* And arrange for that done flag to be at index -1. */
7367 h
->vtable_entries_used
= ptr
+ 1;
7368 h
->vtable_entries_size
= size
;
7371 h
->vtable_entries_used
[addend
/ file_align
] = true;
7376 /* And an accompanying bit to work out final got entry offsets once
7377 we're done. Should be called from final_link. */
7380 elf_gc_common_finalize_got_offsets (abfd
, info
)
7382 struct bfd_link_info
*info
;
7385 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7388 /* The GOT offset is relative to the .got section, but the GOT header is
7389 put into the .got.plt section, if the backend uses it. */
7390 if (bed
->want_got_plt
)
7393 gotoff
= bed
->got_header_size
;
7395 /* Do the local .got entries first. */
7396 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
7398 bfd_signed_vma
*local_got
;
7399 bfd_size_type j
, locsymcount
;
7400 Elf_Internal_Shdr
*symtab_hdr
;
7402 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
7405 local_got
= elf_local_got_refcounts (i
);
7409 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
7410 if (elf_bad_symtab (i
))
7411 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
7413 locsymcount
= symtab_hdr
->sh_info
;
7415 for (j
= 0; j
< locsymcount
; ++j
)
7417 if (local_got
[j
] > 0)
7419 local_got
[j
] = gotoff
;
7420 gotoff
+= ARCH_SIZE
/ 8;
7423 local_got
[j
] = (bfd_vma
) -1;
7427 /* Then the global .got entries. .plt refcounts are handled by
7428 adjust_dynamic_symbol */
7429 elf_link_hash_traverse (elf_hash_table (info
),
7430 elf_gc_allocate_got_offsets
,
7435 /* We need a special top-level link routine to convert got reference counts
7436 to real got offsets. */
7439 elf_gc_allocate_got_offsets (h
, offarg
)
7440 struct elf_link_hash_entry
*h
;
7443 bfd_vma
*off
= (bfd_vma
*) offarg
;
7445 if (h
->got
.refcount
> 0)
7447 h
->got
.offset
= off
[0];
7448 off
[0] += ARCH_SIZE
/ 8;
7451 h
->got
.offset
= (bfd_vma
) -1;
7456 /* Many folk need no more in the way of final link than this, once
7457 got entry reference counting is enabled. */
7460 elf_gc_common_final_link (abfd
, info
)
7462 struct bfd_link_info
*info
;
7464 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
7467 /* Invoke the regular ELF backend linker to do all the work. */
7468 return elf_bfd_final_link (abfd
, info
);
7471 /* This function will be called though elf_link_hash_traverse to store
7472 all hash value of the exported symbols in an array. */
7475 elf_collect_hash_codes (h
, data
)
7476 struct elf_link_hash_entry
*h
;
7479 unsigned long **valuep
= (unsigned long **) data
;
7485 /* Ignore indirect symbols. These are added by the versioning code. */
7486 if (h
->dynindx
== -1)
7489 name
= h
->root
.root
.string
;
7490 p
= strchr (name
, ELF_VER_CHR
);
7493 alc
= bfd_malloc ((bfd_size_type
) (p
- name
+ 1));
7494 memcpy (alc
, name
, (size_t) (p
- name
));
7495 alc
[p
- name
] = '\0';
7499 /* Compute the hash value. */
7500 ha
= bfd_elf_hash (name
);
7502 /* Store the found hash value in the array given as the argument. */
7505 /* And store it in the struct so that we can put it in the hash table
7507 h
->elf_hash_value
= ha
;