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 /* Any syms created from now on start with -1 in
2924 got.refcount/offset and plt.refcount/offset. */
2925 elf_hash_table (info
)->init_refcount
= -1;
2927 /* The backend may have to create some sections regardless of whether
2928 we're dynamic or not. */
2929 bed
= get_elf_backend_data (output_bfd
);
2930 if (bed
->elf_backend_always_size_sections
2931 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
2934 dynobj
= elf_hash_table (info
)->dynobj
;
2936 /* If there were no dynamic objects in the link, there is nothing to
2941 if (elf_hash_table (info
)->dynamic_sections_created
)
2943 struct elf_info_failed eif
;
2944 struct elf_link_hash_entry
*h
;
2947 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
2948 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
2952 soname_indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2953 soname
, true, true);
2954 if (soname_indx
== (bfd_size_type
) -1
2955 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SONAME
,
2962 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMBOLIC
,
2965 info
->flags
|= DF_SYMBOLIC
;
2972 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, rpath
,
2974 if (indx
== (bfd_size_type
) -1
2975 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RPATH
, indx
)
2977 && ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RUNPATH
,
2982 if (filter_shlib
!= NULL
)
2986 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2987 filter_shlib
, true, true);
2988 if (indx
== (bfd_size_type
) -1
2989 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FILTER
, indx
))
2993 if (auxiliary_filters
!= NULL
)
2995 const char * const *p
;
2997 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
3001 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3003 if (indx
== (bfd_size_type
) -1
3004 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_AUXILIARY
,
3011 eif
.verdefs
= verdefs
;
3014 /* If we are supposed to export all symbols into the dynamic symbol
3015 table (this is not the normal case), then do so. */
3016 if (info
->export_dynamic
)
3018 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
3024 /* Attach all the symbols to their version information. */
3025 asvinfo
.output_bfd
= output_bfd
;
3026 asvinfo
.info
= info
;
3027 asvinfo
.verdefs
= verdefs
;
3028 asvinfo
.failed
= false;
3030 elf_link_hash_traverse (elf_hash_table (info
),
3031 elf_link_assign_sym_version
,
3036 /* Find all symbols which were defined in a dynamic object and make
3037 the backend pick a reasonable value for them. */
3038 elf_link_hash_traverse (elf_hash_table (info
),
3039 elf_adjust_dynamic_symbol
,
3044 /* Add some entries to the .dynamic section. We fill in some of the
3045 values later, in elf_bfd_final_link, but we must add the entries
3046 now so that we know the final size of the .dynamic section. */
3048 /* If there are initialization and/or finalization functions to
3049 call then add the corresponding DT_INIT/DT_FINI entries. */
3050 h
= (info
->init_function
3051 ? elf_link_hash_lookup (elf_hash_table (info
),
3052 info
->init_function
, false,
3056 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3057 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3059 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT
, (bfd_vma
) 0))
3062 h
= (info
->fini_function
3063 ? elf_link_hash_lookup (elf_hash_table (info
),
3064 info
->fini_function
, false,
3068 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3069 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3071 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI
, (bfd_vma
) 0))
3075 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
3076 /* If .dynstr is excluded from the link, we don't want any of
3077 these tags. Strictly, we should be checking each section
3078 individually; This quick check covers for the case where
3079 someone does a /DISCARD/ : { *(*) }. */
3080 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
3082 bfd_size_type strsize
;
3084 strsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
3085 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_HASH
, (bfd_vma
) 0)
3086 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRTAB
, (bfd_vma
) 0)
3087 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMTAB
, (bfd_vma
) 0)
3088 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRSZ
, strsize
)
3089 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMENT
,
3090 (bfd_vma
) sizeof (Elf_External_Sym
)))
3095 /* The backend must work out the sizes of all the other dynamic
3097 if (bed
->elf_backend_size_dynamic_sections
3098 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
3101 if (elf_hash_table (info
)->dynamic_sections_created
)
3103 bfd_size_type dynsymcount
;
3105 size_t bucketcount
= 0;
3106 size_t hash_entry_size
;
3107 unsigned int dtagcount
;
3109 /* Set up the version definition section. */
3110 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3111 BFD_ASSERT (s
!= NULL
);
3113 /* We may have created additional version definitions if we are
3114 just linking a regular application. */
3115 verdefs
= asvinfo
.verdefs
;
3117 if (verdefs
== NULL
)
3118 _bfd_strip_section_from_output (info
, s
);
3123 struct bfd_elf_version_tree
*t
;
3125 Elf_Internal_Verdef def
;
3126 Elf_Internal_Verdaux defaux
;
3131 /* Make space for the base version. */
3132 size
+= sizeof (Elf_External_Verdef
);
3133 size
+= sizeof (Elf_External_Verdaux
);
3136 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3138 struct bfd_elf_version_deps
*n
;
3140 size
+= sizeof (Elf_External_Verdef
);
3141 size
+= sizeof (Elf_External_Verdaux
);
3144 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3145 size
+= sizeof (Elf_External_Verdaux
);
3148 s
->_raw_size
= size
;
3149 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3150 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3153 /* Fill in the version definition section. */
3157 def
.vd_version
= VER_DEF_CURRENT
;
3158 def
.vd_flags
= VER_FLG_BASE
;
3161 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3162 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3163 + sizeof (Elf_External_Verdaux
));
3165 if (soname_indx
!= (bfd_size_type
) -1)
3167 def
.vd_hash
= bfd_elf_hash (soname
);
3168 defaux
.vda_name
= soname_indx
;
3175 name
= basename (output_bfd
->filename
);
3176 def
.vd_hash
= bfd_elf_hash (name
);
3177 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3179 if (indx
== (bfd_size_type
) -1)
3181 defaux
.vda_name
= indx
;
3183 defaux
.vda_next
= 0;
3185 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3186 (Elf_External_Verdef
*) p
);
3187 p
+= sizeof (Elf_External_Verdef
);
3188 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3189 (Elf_External_Verdaux
*) p
);
3190 p
+= sizeof (Elf_External_Verdaux
);
3192 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3195 struct bfd_elf_version_deps
*n
;
3196 struct elf_link_hash_entry
*h
;
3199 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3202 /* Add a symbol representing this version. */
3204 if (! (_bfd_generic_link_add_one_symbol
3205 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
3206 (bfd_vma
) 0, (const char *) NULL
, false,
3207 get_elf_backend_data (dynobj
)->collect
,
3208 (struct bfd_link_hash_entry
**) &h
)))
3210 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
3211 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3212 h
->type
= STT_OBJECT
;
3213 h
->verinfo
.vertree
= t
;
3215 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
3218 def
.vd_version
= VER_DEF_CURRENT
;
3220 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
3221 def
.vd_flags
|= VER_FLG_WEAK
;
3222 def
.vd_ndx
= t
->vernum
+ 1;
3223 def
.vd_cnt
= cdeps
+ 1;
3224 def
.vd_hash
= bfd_elf_hash (t
->name
);
3225 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3226 if (t
->next
!= NULL
)
3227 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3228 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
3232 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3233 (Elf_External_Verdef
*) p
);
3234 p
+= sizeof (Elf_External_Verdef
);
3236 defaux
.vda_name
= h
->dynstr_index
;
3237 if (t
->deps
== NULL
)
3238 defaux
.vda_next
= 0;
3240 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3241 t
->name_indx
= defaux
.vda_name
;
3243 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3244 (Elf_External_Verdaux
*) p
);
3245 p
+= sizeof (Elf_External_Verdaux
);
3247 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3249 if (n
->version_needed
== NULL
)
3251 /* This can happen if there was an error in the
3253 defaux
.vda_name
= 0;
3256 defaux
.vda_name
= n
->version_needed
->name_indx
;
3257 if (n
->next
== NULL
)
3258 defaux
.vda_next
= 0;
3260 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3262 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3263 (Elf_External_Verdaux
*) p
);
3264 p
+= sizeof (Elf_External_Verdaux
);
3268 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEF
, (bfd_vma
) 0)
3269 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEFNUM
,
3273 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
3276 if (info
->new_dtags
&& info
->flags
)
3278 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS
, info
->flags
))
3285 info
->flags_1
&= ~ (DF_1_INITFIRST
3288 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS_1
,
3293 /* Work out the size of the version reference section. */
3295 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3296 BFD_ASSERT (s
!= NULL
);
3298 struct elf_find_verdep_info sinfo
;
3300 sinfo
.output_bfd
= output_bfd
;
3302 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
3303 if (sinfo
.vers
== 0)
3305 sinfo
.failed
= false;
3307 elf_link_hash_traverse (elf_hash_table (info
),
3308 elf_link_find_version_dependencies
,
3311 if (elf_tdata (output_bfd
)->verref
== NULL
)
3312 _bfd_strip_section_from_output (info
, s
);
3315 Elf_Internal_Verneed
*t
;
3320 /* Build the version definition section. */
3323 for (t
= elf_tdata (output_bfd
)->verref
;
3327 Elf_Internal_Vernaux
*a
;
3329 size
+= sizeof (Elf_External_Verneed
);
3331 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3332 size
+= sizeof (Elf_External_Vernaux
);
3335 s
->_raw_size
= size
;
3336 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3337 if (s
->contents
== NULL
)
3341 for (t
= elf_tdata (output_bfd
)->verref
;
3346 Elf_Internal_Vernaux
*a
;
3350 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3353 t
->vn_version
= VER_NEED_CURRENT
;
3355 if (elf_dt_name (t
->vn_bfd
) != NULL
)
3356 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3357 elf_dt_name (t
->vn_bfd
),
3360 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3361 basename (t
->vn_bfd
->filename
),
3363 if (indx
== (bfd_size_type
) -1)
3366 t
->vn_aux
= sizeof (Elf_External_Verneed
);
3367 if (t
->vn_nextref
== NULL
)
3370 t
->vn_next
= (sizeof (Elf_External_Verneed
)
3371 + caux
* sizeof (Elf_External_Vernaux
));
3373 _bfd_elf_swap_verneed_out (output_bfd
, t
,
3374 (Elf_External_Verneed
*) p
);
3375 p
+= sizeof (Elf_External_Verneed
);
3377 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3379 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
3380 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3381 a
->vna_nodename
, true, false);
3382 if (indx
== (bfd_size_type
) -1)
3385 if (a
->vna_nextptr
== NULL
)
3388 a
->vna_next
= sizeof (Elf_External_Vernaux
);
3390 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
3391 (Elf_External_Vernaux
*) p
);
3392 p
+= sizeof (Elf_External_Vernaux
);
3396 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEED
,
3398 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEEDNUM
,
3402 elf_tdata (output_bfd
)->cverrefs
= crefs
;
3406 /* Assign dynsym indicies. In a shared library we generate a
3407 section symbol for each output section, which come first.
3408 Next come all of the back-end allocated local dynamic syms,
3409 followed by the rest of the global symbols. */
3411 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3413 /* Work out the size of the symbol version section. */
3414 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3415 BFD_ASSERT (s
!= NULL
);
3416 if (dynsymcount
== 0
3417 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
3419 _bfd_strip_section_from_output (info
, s
);
3420 /* The DYNSYMCOUNT might have changed if we were going to
3421 output a dynamic symbol table entry for S. */
3422 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3426 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
3427 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3428 if (s
->contents
== NULL
)
3431 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERSYM
, (bfd_vma
) 0))
3435 /* Set the size of the .dynsym and .hash sections. We counted
3436 the number of dynamic symbols in elf_link_add_object_symbols.
3437 We will build the contents of .dynsym and .hash when we build
3438 the final symbol table, because until then we do not know the
3439 correct value to give the symbols. We built the .dynstr
3440 section as we went along in elf_link_add_object_symbols. */
3441 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
3442 BFD_ASSERT (s
!= NULL
);
3443 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
3444 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3445 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3448 if (dynsymcount
!= 0)
3450 Elf_Internal_Sym isym
;
3452 /* The first entry in .dynsym is a dummy symbol. */
3459 elf_swap_symbol_out (output_bfd
, &isym
,
3460 (PTR
) (Elf_External_Sym
*) s
->contents
);
3463 /* Compute the size of the hashing table. As a side effect this
3464 computes the hash values for all the names we export. */
3465 bucketcount
= compute_bucket_count (info
);
3467 s
= bfd_get_section_by_name (dynobj
, ".hash");
3468 BFD_ASSERT (s
!= NULL
);
3469 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
3470 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
3471 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3472 if (s
->contents
== NULL
)
3474 memset (s
->contents
, 0, (size_t) s
->_raw_size
);
3476 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) bucketcount
,
3478 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) dynsymcount
,
3479 s
->contents
+ hash_entry_size
);
3481 elf_hash_table (info
)->bucketcount
= bucketcount
;
3483 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
3484 BFD_ASSERT (s
!= NULL
);
3485 s
->_raw_size
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
3487 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
3488 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NULL
, (bfd_vma
) 0))
3495 /* Fix up the flags for a symbol. This handles various cases which
3496 can only be fixed after all the input files are seen. This is
3497 currently called by both adjust_dynamic_symbol and
3498 assign_sym_version, which is unnecessary but perhaps more robust in
3499 the face of future changes. */
3502 elf_fix_symbol_flags (h
, eif
)
3503 struct elf_link_hash_entry
*h
;
3504 struct elf_info_failed
*eif
;
3506 /* If this symbol was mentioned in a non-ELF file, try to set
3507 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3508 permit a non-ELF file to correctly refer to a symbol defined in
3509 an ELF dynamic object. */
3510 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
3512 while (h
->root
.type
== bfd_link_hash_indirect
)
3513 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3515 if (h
->root
.type
!= bfd_link_hash_defined
3516 && h
->root
.type
!= bfd_link_hash_defweak
)
3517 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3518 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3521 if (h
->root
.u
.def
.section
->owner
!= NULL
3522 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3523 == bfd_target_elf_flavour
))
3524 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3525 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3527 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3530 if (h
->dynindx
== -1
3531 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
3532 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
3534 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3543 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3544 was first seen in a non-ELF file. Fortunately, if the symbol
3545 was first seen in an ELF file, we're probably OK unless the
3546 symbol was defined in a non-ELF file. Catch that case here.
3547 FIXME: We're still in trouble if the symbol was first seen in
3548 a dynamic object, and then later in a non-ELF regular object. */
3549 if ((h
->root
.type
== bfd_link_hash_defined
3550 || h
->root
.type
== bfd_link_hash_defweak
)
3551 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3552 && (h
->root
.u
.def
.section
->owner
!= NULL
3553 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3554 != bfd_target_elf_flavour
)
3555 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3556 && (h
->elf_link_hash_flags
3557 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
3558 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3561 /* If this is a final link, and the symbol was defined as a common
3562 symbol in a regular object file, and there was no definition in
3563 any dynamic object, then the linker will have allocated space for
3564 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3565 flag will not have been set. */
3566 if (h
->root
.type
== bfd_link_hash_defined
3567 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3568 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
3569 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3570 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3571 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3573 /* If -Bsymbolic was used (which means to bind references to global
3574 symbols to the definition within the shared object), and this
3575 symbol was defined in a regular object, then it actually doesn't
3576 need a PLT entry, and we can accomplish that by forcing it local.
3577 Likewise, if the symbol has hidden or internal visibility.
3578 FIXME: It might be that we also do not need a PLT for other
3579 non-hidden visibilities, but we would have to tell that to the
3580 backend specifically; we can't just clear PLT-related data here. */
3581 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
3582 && eif
->info
->shared
3583 && is_elf_hash_table (eif
->info
)
3584 && (eif
->info
->symbolic
3585 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3586 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3587 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3589 struct elf_backend_data
*bed
;
3591 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3592 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3593 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3594 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3595 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
);
3598 /* If this is a weak defined symbol in a dynamic object, and we know
3599 the real definition in the dynamic object, copy interesting flags
3600 over to the real definition. */
3601 if (h
->weakdef
!= NULL
)
3603 struct elf_link_hash_entry
*weakdef
;
3605 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3606 || h
->root
.type
== bfd_link_hash_defweak
);
3607 weakdef
= h
->weakdef
;
3608 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
3609 || weakdef
->root
.type
== bfd_link_hash_defweak
);
3610 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
3612 /* If the real definition is defined by a regular object file,
3613 don't do anything special. See the longer description in
3614 elf_adjust_dynamic_symbol, below. */
3615 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3618 weakdef
->elf_link_hash_flags
|=
3619 (h
->elf_link_hash_flags
3620 & (ELF_LINK_HASH_REF_REGULAR
3621 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
3622 | ELF_LINK_NON_GOT_REF
));
3628 /* Make the backend pick a good value for a dynamic symbol. This is
3629 called via elf_link_hash_traverse, and also calls itself
3633 elf_adjust_dynamic_symbol (h
, data
)
3634 struct elf_link_hash_entry
*h
;
3637 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3639 struct elf_backend_data
*bed
;
3641 /* Ignore indirect symbols. These are added by the versioning code. */
3642 if (h
->root
.type
== bfd_link_hash_indirect
)
3645 if (! is_elf_hash_table (eif
->info
))
3648 /* Fix the symbol flags. */
3649 if (! elf_fix_symbol_flags (h
, eif
))
3652 /* If this symbol does not require a PLT entry, and it is not
3653 defined by a dynamic object, or is not referenced by a regular
3654 object, ignore it. We do have to handle a weak defined symbol,
3655 even if no regular object refers to it, if we decided to add it
3656 to the dynamic symbol table. FIXME: Do we normally need to worry
3657 about symbols which are defined by one dynamic object and
3658 referenced by another one? */
3659 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
3660 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
3661 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3662 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
3663 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
3665 h
->plt
.offset
= (bfd_vma
) -1;
3669 /* If we've already adjusted this symbol, don't do it again. This
3670 can happen via a recursive call. */
3671 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
3674 /* Don't look at this symbol again. Note that we must set this
3675 after checking the above conditions, because we may look at a
3676 symbol once, decide not to do anything, and then get called
3677 recursively later after REF_REGULAR is set below. */
3678 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
3680 /* If this is a weak definition, and we know a real definition, and
3681 the real symbol is not itself defined by a regular object file,
3682 then get a good value for the real definition. We handle the
3683 real symbol first, for the convenience of the backend routine.
3685 Note that there is a confusing case here. If the real definition
3686 is defined by a regular object file, we don't get the real symbol
3687 from the dynamic object, but we do get the weak symbol. If the
3688 processor backend uses a COPY reloc, then if some routine in the
3689 dynamic object changes the real symbol, we will not see that
3690 change in the corresponding weak symbol. This is the way other
3691 ELF linkers work as well, and seems to be a result of the shared
3694 I will clarify this issue. Most SVR4 shared libraries define the
3695 variable _timezone and define timezone as a weak synonym. The
3696 tzset call changes _timezone. If you write
3697 extern int timezone;
3699 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3700 you might expect that, since timezone is a synonym for _timezone,
3701 the same number will print both times. However, if the processor
3702 backend uses a COPY reloc, then actually timezone will be copied
3703 into your process image, and, since you define _timezone
3704 yourself, _timezone will not. Thus timezone and _timezone will
3705 wind up at different memory locations. The tzset call will set
3706 _timezone, leaving timezone unchanged. */
3708 if (h
->weakdef
!= NULL
)
3710 /* If we get to this point, we know there is an implicit
3711 reference by a regular object file via the weak symbol H.
3712 FIXME: Is this really true? What if the traversal finds
3713 H->WEAKDEF before it finds H? */
3714 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
3716 if (! elf_adjust_dynamic_symbol (h
->weakdef
, (PTR
) eif
))
3720 /* If a symbol has no type and no size and does not require a PLT
3721 entry, then we are probably about to do the wrong thing here: we
3722 are probably going to create a COPY reloc for an empty object.
3723 This case can arise when a shared object is built with assembly
3724 code, and the assembly code fails to set the symbol type. */
3726 && h
->type
== STT_NOTYPE
3727 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
3728 (*_bfd_error_handler
)
3729 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3730 h
->root
.root
.string
);
3732 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
3733 bed
= get_elf_backend_data (dynobj
);
3734 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3743 /* This routine is used to export all defined symbols into the dynamic
3744 symbol table. It is called via elf_link_hash_traverse. */
3747 elf_export_symbol (h
, data
)
3748 struct elf_link_hash_entry
*h
;
3751 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3753 /* Ignore indirect symbols. These are added by the versioning code. */
3754 if (h
->root
.type
== bfd_link_hash_indirect
)
3757 if (h
->dynindx
== -1
3758 && (h
->elf_link_hash_flags
3759 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
3761 struct bfd_elf_version_tree
*t
;
3762 struct bfd_elf_version_expr
*d
;
3764 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
3766 if (t
->globals
!= NULL
)
3768 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3770 if ((*d
->match
) (d
, h
->root
.root
.string
))
3775 if (t
->locals
!= NULL
)
3777 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3779 if ((*d
->match
) (d
, h
->root
.root
.string
))
3788 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3799 /* Look through the symbols which are defined in other shared
3800 libraries and referenced here. Update the list of version
3801 dependencies. This will be put into the .gnu.version_r section.
3802 This function is called via elf_link_hash_traverse. */
3805 elf_link_find_version_dependencies (h
, data
)
3806 struct elf_link_hash_entry
*h
;
3809 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
3810 Elf_Internal_Verneed
*t
;
3811 Elf_Internal_Vernaux
*a
;
3814 /* We only care about symbols defined in shared objects with version
3816 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3817 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
3819 || h
->verinfo
.verdef
== NULL
)
3822 /* See if we already know about this version. */
3823 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
3825 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
3828 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3829 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
3835 /* This is a new version. Add it to tree we are building. */
3840 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
3843 rinfo
->failed
= true;
3847 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
3848 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
3849 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
3853 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
3855 /* Note that we are copying a string pointer here, and testing it
3856 above. If bfd_elf_string_from_elf_section is ever changed to
3857 discard the string data when low in memory, this will have to be
3859 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
3861 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
3862 a
->vna_nextptr
= t
->vn_auxptr
;
3864 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
3867 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
3874 /* Figure out appropriate versions for all the symbols. We may not
3875 have the version number script until we have read all of the input
3876 files, so until that point we don't know which symbols should be
3877 local. This function is called via elf_link_hash_traverse. */
3880 elf_link_assign_sym_version (h
, data
)
3881 struct elf_link_hash_entry
*h
;
3884 struct elf_assign_sym_version_info
*sinfo
;
3885 struct bfd_link_info
*info
;
3886 struct elf_backend_data
*bed
;
3887 struct elf_info_failed eif
;
3891 sinfo
= (struct elf_assign_sym_version_info
*) data
;
3894 /* Fix the symbol flags. */
3897 if (! elf_fix_symbol_flags (h
, &eif
))
3900 sinfo
->failed
= true;
3904 /* We only need version numbers for symbols defined in regular
3906 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
3909 bed
= get_elf_backend_data (sinfo
->output_bfd
);
3910 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3911 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
3913 struct bfd_elf_version_tree
*t
;
3918 /* There are two consecutive ELF_VER_CHR characters if this is
3919 not a hidden symbol. */
3921 if (*p
== ELF_VER_CHR
)
3927 /* If there is no version string, we can just return out. */
3931 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
3935 /* Look for the version. If we find it, it is no longer weak. */
3936 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
3938 if (strcmp (t
->name
, p
) == 0)
3942 struct bfd_elf_version_expr
*d
;
3944 len
= p
- h
->root
.root
.string
;
3945 alc
= bfd_alloc (sinfo
->output_bfd
, (bfd_size_type
) len
);
3948 strncpy (alc
, h
->root
.root
.string
, len
- 1);
3949 alc
[len
- 1] = '\0';
3950 if (alc
[len
- 2] == ELF_VER_CHR
)
3951 alc
[len
- 2] = '\0';
3953 h
->verinfo
.vertree
= t
;
3957 if (t
->globals
!= NULL
)
3959 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3960 if ((*d
->match
) (d
, alc
))
3964 /* See if there is anything to force this symbol to
3966 if (d
== NULL
&& t
->locals
!= NULL
)
3968 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3970 if ((*d
->match
) (d
, alc
))
3972 if (h
->dynindx
!= -1
3974 && ! info
->export_dynamic
)
3976 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3977 (*bed
->elf_backend_hide_symbol
) (info
, h
);
3978 /* FIXME: The name of the symbol has
3979 already been recorded in the dynamic
3980 string table section. */
3988 bfd_release (sinfo
->output_bfd
, alc
);
3993 /* If we are building an application, we need to create a
3994 version node for this version. */
3995 if (t
== NULL
&& ! info
->shared
)
3997 struct bfd_elf_version_tree
**pp
;
4000 /* If we aren't going to export this symbol, we don't need
4001 to worry about it. */
4002 if (h
->dynindx
== -1)
4006 t
= ((struct bfd_elf_version_tree
*)
4007 bfd_alloc (sinfo
->output_bfd
, amt
));
4010 sinfo
->failed
= true;
4019 t
->name_indx
= (unsigned int) -1;
4023 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4025 t
->vernum
= version_index
;
4029 h
->verinfo
.vertree
= t
;
4033 /* We could not find the version for a symbol when
4034 generating a shared archive. Return an error. */
4035 (*_bfd_error_handler
)
4036 (_("%s: undefined versioned symbol name %s"),
4037 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
4038 bfd_set_error (bfd_error_bad_value
);
4039 sinfo
->failed
= true;
4044 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4047 /* If we don't have a version for this symbol, see if we can find
4049 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
4051 struct bfd_elf_version_tree
*t
;
4052 struct bfd_elf_version_tree
*deflt
;
4053 struct bfd_elf_version_expr
*d
;
4055 /* See if can find what version this symbol is in. If the
4056 symbol is supposed to be local, then don't actually register
4059 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4061 if (t
->globals
!= NULL
)
4063 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4065 if ((*d
->match
) (d
, h
->root
.root
.string
))
4067 h
->verinfo
.vertree
= t
;
4076 if (t
->locals
!= NULL
)
4078 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4080 if (d
->pattern
[0] == '*' && d
->pattern
[1] == '\0')
4082 else if ((*d
->match
) (d
, h
->root
.root
.string
))
4084 h
->verinfo
.vertree
= t
;
4085 if (h
->dynindx
!= -1
4087 && ! info
->export_dynamic
)
4089 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
4090 (*bed
->elf_backend_hide_symbol
) (info
, h
);
4091 /* FIXME: The name of the symbol has already
4092 been recorded in the dynamic string table
4104 if (deflt
!= NULL
&& h
->verinfo
.vertree
== NULL
)
4106 h
->verinfo
.vertree
= deflt
;
4107 if (h
->dynindx
!= -1
4109 && ! info
->export_dynamic
)
4111 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
4112 (*bed
->elf_backend_hide_symbol
) (info
, h
);
4113 /* FIXME: The name of the symbol has already been
4114 recorded in the dynamic string table section. */
4122 /* Final phase of ELF linker. */
4124 /* A structure we use to avoid passing large numbers of arguments. */
4126 struct elf_final_link_info
4128 /* General link information. */
4129 struct bfd_link_info
*info
;
4132 /* Symbol string table. */
4133 struct bfd_strtab_hash
*symstrtab
;
4134 /* .dynsym section. */
4135 asection
*dynsym_sec
;
4136 /* .hash section. */
4138 /* symbol version section (.gnu.version). */
4139 asection
*symver_sec
;
4140 /* Buffer large enough to hold contents of any section. */
4142 /* Buffer large enough to hold external relocs of any section. */
4143 PTR external_relocs
;
4144 /* Buffer large enough to hold internal relocs of any section. */
4145 Elf_Internal_Rela
*internal_relocs
;
4146 /* Buffer large enough to hold external local symbols of any input
4148 Elf_External_Sym
*external_syms
;
4149 /* Buffer large enough to hold internal local symbols of any input
4151 Elf_Internal_Sym
*internal_syms
;
4152 /* Array large enough to hold a symbol index for each local symbol
4153 of any input BFD. */
4155 /* Array large enough to hold a section pointer for each local
4156 symbol of any input BFD. */
4157 asection
**sections
;
4158 /* Buffer to hold swapped out symbols. */
4159 Elf_External_Sym
*symbuf
;
4160 /* Number of swapped out symbols in buffer. */
4161 size_t symbuf_count
;
4162 /* Number of symbols which fit in symbuf. */
4166 static boolean elf_link_output_sym
4167 PARAMS ((struct elf_final_link_info
*, const char *,
4168 Elf_Internal_Sym
*, asection
*));
4169 static boolean elf_link_flush_output_syms
4170 PARAMS ((struct elf_final_link_info
*));
4171 static boolean elf_link_output_extsym
4172 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4173 static boolean elf_link_sec_merge_syms
4174 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4175 static boolean elf_link_input_bfd
4176 PARAMS ((struct elf_final_link_info
*, bfd
*));
4177 static boolean elf_reloc_link_order
4178 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
4179 struct bfd_link_order
*));
4181 /* This struct is used to pass information to elf_link_output_extsym. */
4183 struct elf_outext_info
4187 struct elf_final_link_info
*finfo
;
4190 /* Compute the size of, and allocate space for, REL_HDR which is the
4191 section header for a section containing relocations for O. */
4194 elf_link_size_reloc_section (abfd
, rel_hdr
, o
)
4196 Elf_Internal_Shdr
*rel_hdr
;
4199 bfd_size_type reloc_count
;
4200 bfd_size_type num_rel_hashes
;
4202 /* Figure out how many relocations there will be. */
4203 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
4204 reloc_count
= elf_section_data (o
)->rel_count
;
4206 reloc_count
= elf_section_data (o
)->rel_count2
;
4208 num_rel_hashes
= o
->reloc_count
;
4209 if (num_rel_hashes
< reloc_count
)
4210 num_rel_hashes
= reloc_count
;
4212 /* That allows us to calculate the size of the section. */
4213 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
4215 /* The contents field must last into write_object_contents, so we
4216 allocate it with bfd_alloc rather than malloc. Also since we
4217 cannot be sure that the contents will actually be filled in,
4218 we zero the allocated space. */
4219 rel_hdr
->contents
= (PTR
) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
4220 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
4223 /* We only allocate one set of hash entries, so we only do it the
4224 first time we are called. */
4225 if (elf_section_data (o
)->rel_hashes
== NULL
4228 struct elf_link_hash_entry
**p
;
4230 p
= ((struct elf_link_hash_entry
**)
4231 bfd_zmalloc (num_rel_hashes
4232 * sizeof (struct elf_link_hash_entry
*)));
4236 elf_section_data (o
)->rel_hashes
= p
;
4242 /* When performing a relocateable link, the input relocations are
4243 preserved. But, if they reference global symbols, the indices
4244 referenced must be updated. Update all the relocations in
4245 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4248 elf_link_adjust_relocs (abfd
, rel_hdr
, count
, rel_hash
)
4250 Elf_Internal_Shdr
*rel_hdr
;
4252 struct elf_link_hash_entry
**rel_hash
;
4255 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4256 Elf_Internal_Rel
*irel
;
4257 Elf_Internal_Rela
*irela
;
4258 bfd_size_type amt
= sizeof (Elf_Internal_Rel
) * bed
->s
->int_rels_per_ext_rel
;
4260 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (amt
);
4263 (*_bfd_error_handler
) (_("Error: out of memory"));
4267 amt
= sizeof (Elf_Internal_Rela
) * bed
->s
->int_rels_per_ext_rel
;
4268 irela
= (Elf_Internal_Rela
*) bfd_zmalloc (amt
);
4271 (*_bfd_error_handler
) (_("Error: out of memory"));
4275 for (i
= 0; i
< count
; i
++, rel_hash
++)
4277 if (*rel_hash
== NULL
)
4280 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
4282 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4284 Elf_External_Rel
*erel
;
4287 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
4288 if (bed
->s
->swap_reloc_in
)
4289 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
4291 elf_swap_reloc_in (abfd
, erel
, irel
);
4293 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4294 irel
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4295 ELF_R_TYPE (irel
[j
].r_info
));
4297 if (bed
->s
->swap_reloc_out
)
4298 (*bed
->s
->swap_reloc_out
) (abfd
, irel
, (bfd_byte
*) erel
);
4300 elf_swap_reloc_out (abfd
, irel
, erel
);
4304 Elf_External_Rela
*erela
;
4307 BFD_ASSERT (rel_hdr
->sh_entsize
4308 == sizeof (Elf_External_Rela
));
4310 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
4311 if (bed
->s
->swap_reloca_in
)
4312 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
4314 elf_swap_reloca_in (abfd
, erela
, irela
);
4316 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4317 irela
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4318 ELF_R_TYPE (irela
[j
].r_info
));
4320 if (bed
->s
->swap_reloca_out
)
4321 (*bed
->s
->swap_reloca_out
) (abfd
, irela
, (bfd_byte
*) erela
);
4323 elf_swap_reloca_out (abfd
, irela
, erela
);
4331 struct elf_link_sort_rela
{
4333 enum elf_reloc_type_class type
;
4335 Elf_Internal_Rel rel
;
4336 Elf_Internal_Rela rela
;
4341 elf_link_sort_cmp1 (A
, B
)
4345 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4346 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4347 int relativea
, relativeb
;
4349 relativea
= a
->type
== reloc_class_relative
;
4350 relativeb
= b
->type
== reloc_class_relative
;
4352 if (relativea
< relativeb
)
4354 if (relativea
> relativeb
)
4356 if (ELF_R_SYM (a
->u
.rel
.r_info
) < ELF_R_SYM (b
->u
.rel
.r_info
))
4358 if (ELF_R_SYM (a
->u
.rel
.r_info
) > ELF_R_SYM (b
->u
.rel
.r_info
))
4360 if (a
->u
.rel
.r_offset
< b
->u
.rel
.r_offset
)
4362 if (a
->u
.rel
.r_offset
> b
->u
.rel
.r_offset
)
4368 elf_link_sort_cmp2 (A
, B
)
4372 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4373 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4376 if (a
->offset
< b
->offset
)
4378 if (a
->offset
> b
->offset
)
4380 copya
= a
->type
== reloc_class_copy
;
4381 copyb
= b
->type
== reloc_class_copy
;
4386 if (a
->u
.rel
.r_offset
< b
->u
.rel
.r_offset
)
4388 if (a
->u
.rel
.r_offset
> b
->u
.rel
.r_offset
)
4394 elf_link_sort_relocs (abfd
, info
, psec
)
4396 struct bfd_link_info
*info
;
4399 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4400 asection
*reldyn
, *o
;
4401 boolean rel
= false;
4402 bfd_size_type count
, size
;
4404 struct elf_link_sort_rela
*rela
;
4405 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4407 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
4408 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4410 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
4411 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4414 count
= reldyn
->_raw_size
/ sizeof (Elf_External_Rel
);
4417 count
= reldyn
->_raw_size
/ sizeof (Elf_External_Rela
);
4420 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4421 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4422 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4423 && o
->output_section
== reldyn
)
4424 size
+= o
->_raw_size
;
4426 if (size
!= reldyn
->_raw_size
)
4429 rela
= (struct elf_link_sort_rela
*) bfd_zmalloc (sizeof (*rela
) * count
);
4432 (*info
->callbacks
->warning
)
4433 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0,
4438 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4439 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4440 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4441 && o
->output_section
== reldyn
)
4445 Elf_External_Rel
*erel
, *erelend
;
4446 struct elf_link_sort_rela
*s
;
4448 erel
= (Elf_External_Rel
*) o
->contents
;
4449 erelend
= (Elf_External_Rel
*) (o
->contents
+ o
->_raw_size
);
4450 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rel
);
4451 for (; erel
< erelend
; erel
++, s
++)
4453 if (bed
->s
->swap_reloc_in
)
4454 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, &s
->u
.rel
);
4456 elf_swap_reloc_in (abfd
, erel
, &s
->u
.rel
);
4458 s
->type
= (*bed
->elf_backend_reloc_type_class
) (&s
->u
.rela
);
4463 Elf_External_Rela
*erela
, *erelaend
;
4464 struct elf_link_sort_rela
*s
;
4466 erela
= (Elf_External_Rela
*) o
->contents
;
4467 erelaend
= (Elf_External_Rela
*) (o
->contents
+ o
->_raw_size
);
4468 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rela
);
4469 for (; erela
< erelaend
; erela
++, s
++)
4471 if (bed
->s
->swap_reloca_in
)
4472 (*bed
->s
->swap_reloca_in
) (dynobj
, (bfd_byte
*) erela
,
4475 elf_swap_reloca_in (dynobj
, erela
, &s
->u
.rela
);
4477 s
->type
= (*bed
->elf_backend_reloc_type_class
) (&s
->u
.rela
);
4482 qsort (rela
, count
, sizeof (*rela
), elf_link_sort_cmp1
);
4483 for (ret
= 0; ret
< count
&& rela
[ret
].type
== reloc_class_relative
; ret
++)
4485 for (i
= ret
, j
= ret
; i
< count
; i
++)
4487 if (ELF_R_SYM (rela
[i
].u
.rel
.r_info
) != ELF_R_SYM (rela
[j
].u
.rel
.r_info
))
4489 rela
[i
].offset
= rela
[j
].u
.rel
.r_offset
;
4491 qsort (rela
+ ret
, count
- ret
, sizeof (*rela
), elf_link_sort_cmp2
);
4493 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4494 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4495 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4496 && o
->output_section
== reldyn
)
4500 Elf_External_Rel
*erel
, *erelend
;
4501 struct elf_link_sort_rela
*s
;
4503 erel
= (Elf_External_Rel
*) o
->contents
;
4504 erelend
= (Elf_External_Rel
*) ((PTR
) o
->contents
+ o
->_raw_size
);
4505 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rel
);
4506 for (; erel
< erelend
; erel
++, s
++)
4508 if (bed
->s
->swap_reloc_out
)
4509 (*bed
->s
->swap_reloc_out
) (abfd
, &s
->u
.rel
,
4512 elf_swap_reloc_out (abfd
, &s
->u
.rel
, erel
);
4517 Elf_External_Rela
*erela
, *erelaend
;
4518 struct elf_link_sort_rela
*s
;
4520 erela
= (Elf_External_Rela
*) o
->contents
;
4521 erelaend
= (Elf_External_Rela
*) ((PTR
) o
->contents
+ o
->_raw_size
);
4522 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rela
);
4523 for (; erela
< erelaend
; erela
++, s
++)
4525 if (bed
->s
->swap_reloca_out
)
4526 (*bed
->s
->swap_reloca_out
) (dynobj
, &s
->u
.rela
,
4527 (bfd_byte
*) erela
);
4529 elf_swap_reloca_out (dynobj
, &s
->u
.rela
, erela
);
4539 /* Do the final step of an ELF link. */
4542 elf_bfd_final_link (abfd
, info
)
4544 struct bfd_link_info
*info
;
4547 boolean emit_relocs
;
4549 struct elf_final_link_info finfo
;
4550 register asection
*o
;
4551 register struct bfd_link_order
*p
;
4553 bfd_size_type max_contents_size
;
4554 bfd_size_type max_external_reloc_size
;
4555 bfd_size_type max_internal_reloc_count
;
4556 bfd_size_type max_sym_count
;
4558 Elf_Internal_Sym elfsym
;
4560 Elf_Internal_Shdr
*symtab_hdr
;
4561 Elf_Internal_Shdr
*symstrtab_hdr
;
4562 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4563 struct elf_outext_info eoinfo
;
4565 size_t relativecount
= 0;
4566 asection
*reldyn
= 0;
4569 if (! is_elf_hash_table (info
))
4573 abfd
->flags
|= DYNAMIC
;
4575 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
4576 dynobj
= elf_hash_table (info
)->dynobj
;
4578 emit_relocs
= (info
->relocateable
4579 || info
->emitrelocations
4580 || bed
->elf_backend_emit_relocs
);
4583 finfo
.output_bfd
= abfd
;
4584 finfo
.symstrtab
= elf_stringtab_init ();
4585 if (finfo
.symstrtab
== NULL
)
4590 finfo
.dynsym_sec
= NULL
;
4591 finfo
.hash_sec
= NULL
;
4592 finfo
.symver_sec
= NULL
;
4596 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
4597 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
4598 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
4599 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
4600 /* Note that it is OK if symver_sec is NULL. */
4603 finfo
.contents
= NULL
;
4604 finfo
.external_relocs
= NULL
;
4605 finfo
.internal_relocs
= NULL
;
4606 finfo
.external_syms
= NULL
;
4607 finfo
.internal_syms
= NULL
;
4608 finfo
.indices
= NULL
;
4609 finfo
.sections
= NULL
;
4610 finfo
.symbuf
= NULL
;
4611 finfo
.symbuf_count
= 0;
4613 /* Count up the number of relocations we will output for each output
4614 section, so that we know the sizes of the reloc sections. We
4615 also figure out some maximum sizes. */
4616 max_contents_size
= 0;
4617 max_external_reloc_size
= 0;
4618 max_internal_reloc_count
= 0;
4621 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
4625 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
4627 if (p
->type
== bfd_section_reloc_link_order
4628 || p
->type
== bfd_symbol_reloc_link_order
)
4630 else if (p
->type
== bfd_indirect_link_order
)
4634 sec
= p
->u
.indirect
.section
;
4636 /* Mark all sections which are to be included in the
4637 link. This will normally be every section. We need
4638 to do this so that we can identify any sections which
4639 the linker has decided to not include. */
4640 sec
->linker_mark
= true;
4642 if (sec
->flags
& SEC_MERGE
)
4645 if (info
->relocateable
|| info
->emitrelocations
)
4646 o
->reloc_count
+= sec
->reloc_count
;
4647 else if (bed
->elf_backend_count_relocs
)
4649 Elf_Internal_Rela
* relocs
;
4651 relocs
= (NAME(_bfd_elf
,link_read_relocs
)
4652 (abfd
, sec
, (PTR
) NULL
,
4653 (Elf_Internal_Rela
*) NULL
, info
->keep_memory
));
4655 o
->reloc_count
+= (*bed
->elf_backend_count_relocs
)
4658 if (!info
->keep_memory
)
4662 if (sec
->_raw_size
> max_contents_size
)
4663 max_contents_size
= sec
->_raw_size
;
4664 if (sec
->_cooked_size
> max_contents_size
)
4665 max_contents_size
= sec
->_cooked_size
;
4667 /* We are interested in just local symbols, not all
4669 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
4670 && (sec
->owner
->flags
& DYNAMIC
) == 0)
4674 if (elf_bad_symtab (sec
->owner
))
4675 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
4676 / sizeof (Elf_External_Sym
));
4678 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
4680 if (sym_count
> max_sym_count
)
4681 max_sym_count
= sym_count
;
4683 if ((sec
->flags
& SEC_RELOC
) != 0)
4687 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
4688 if (ext_size
> max_external_reloc_size
)
4689 max_external_reloc_size
= ext_size
;
4690 if (sec
->reloc_count
> max_internal_reloc_count
)
4691 max_internal_reloc_count
= sec
->reloc_count
;
4697 if (o
->reloc_count
> 0)
4698 o
->flags
|= SEC_RELOC
;
4701 /* Explicitly clear the SEC_RELOC flag. The linker tends to
4702 set it (this is probably a bug) and if it is set
4703 assign_section_numbers will create a reloc section. */
4704 o
->flags
&=~ SEC_RELOC
;
4707 /* If the SEC_ALLOC flag is not set, force the section VMA to
4708 zero. This is done in elf_fake_sections as well, but forcing
4709 the VMA to 0 here will ensure that relocs against these
4710 sections are handled correctly. */
4711 if ((o
->flags
& SEC_ALLOC
) == 0
4712 && ! o
->user_set_vma
)
4716 if (! info
->relocateable
&& merged
)
4717 elf_link_hash_traverse (elf_hash_table (info
),
4718 elf_link_sec_merge_syms
, (PTR
) abfd
);
4720 /* Figure out the file positions for everything but the symbol table
4721 and the relocs. We set symcount to force assign_section_numbers
4722 to create a symbol table. */
4723 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
4724 BFD_ASSERT (! abfd
->output_has_begun
);
4725 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
4728 /* Figure out how many relocations we will have in each section.
4729 Just using RELOC_COUNT isn't good enough since that doesn't
4730 maintain a separate value for REL vs. RELA relocations. */
4732 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
4733 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
4735 asection
*output_section
;
4737 if (! o
->linker_mark
)
4739 /* This section was omitted from the link. */
4743 output_section
= o
->output_section
;
4745 if (output_section
!= NULL
4746 && (o
->flags
& SEC_RELOC
) != 0)
4748 struct bfd_elf_section_data
*esdi
4749 = elf_section_data (o
);
4750 struct bfd_elf_section_data
*esdo
4751 = elf_section_data (output_section
);
4752 unsigned int *rel_count
;
4753 unsigned int *rel_count2
;
4755 /* We must be careful to add the relocation froms the
4756 input section to the right output count. */
4757 if (esdi
->rel_hdr
.sh_entsize
== esdo
->rel_hdr
.sh_entsize
)
4759 rel_count
= &esdo
->rel_count
;
4760 rel_count2
= &esdo
->rel_count2
;
4764 rel_count
= &esdo
->rel_count2
;
4765 rel_count2
= &esdo
->rel_count
;
4768 *rel_count
+= NUM_SHDR_ENTRIES (& esdi
->rel_hdr
);
4770 *rel_count2
+= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
4771 output_section
->flags
|= SEC_RELOC
;
4775 /* That created the reloc sections. Set their sizes, and assign
4776 them file positions, and allocate some buffers. */
4777 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4779 if ((o
->flags
& SEC_RELOC
) != 0)
4781 if (!elf_link_size_reloc_section (abfd
,
4782 &elf_section_data (o
)->rel_hdr
,
4786 if (elf_section_data (o
)->rel_hdr2
4787 && !elf_link_size_reloc_section (abfd
,
4788 elf_section_data (o
)->rel_hdr2
,
4793 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
4794 to count upwards while actually outputting the relocations. */
4795 elf_section_data (o
)->rel_count
= 0;
4796 elf_section_data (o
)->rel_count2
= 0;
4799 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4801 /* We have now assigned file positions for all the sections except
4802 .symtab and .strtab. We start the .symtab section at the current
4803 file position, and write directly to it. We build the .strtab
4804 section in memory. */
4805 bfd_get_symcount (abfd
) = 0;
4806 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4807 /* sh_name is set in prep_headers. */
4808 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4809 symtab_hdr
->sh_flags
= 0;
4810 symtab_hdr
->sh_addr
= 0;
4811 symtab_hdr
->sh_size
= 0;
4812 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
4813 /* sh_link is set in assign_section_numbers. */
4814 /* sh_info is set below. */
4815 /* sh_offset is set just below. */
4816 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
4818 off
= elf_tdata (abfd
)->next_file_pos
;
4819 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
4821 /* Note that at this point elf_tdata (abfd)->next_file_pos is
4822 incorrect. We do not yet know the size of the .symtab section.
4823 We correct next_file_pos below, after we do know the size. */
4825 /* Allocate a buffer to hold swapped out symbols. This is to avoid
4826 continuously seeking to the right position in the file. */
4827 if (! info
->keep_memory
|| max_sym_count
< 20)
4828 finfo
.symbuf_size
= 20;
4830 finfo
.symbuf_size
= max_sym_count
;
4831 amt
= finfo
.symbuf_size
;
4832 amt
*= sizeof (Elf_External_Sym
);
4833 finfo
.symbuf
= (Elf_External_Sym
*) bfd_malloc (amt
);
4834 if (finfo
.symbuf
== NULL
)
4837 /* Start writing out the symbol table. The first symbol is always a
4839 if (info
->strip
!= strip_all
4842 elfsym
.st_value
= 0;
4845 elfsym
.st_other
= 0;
4846 elfsym
.st_shndx
= SHN_UNDEF
;
4847 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
4848 &elfsym
, bfd_und_section_ptr
))
4853 /* Some standard ELF linkers do this, but we don't because it causes
4854 bootstrap comparison failures. */
4855 /* Output a file symbol for the output file as the second symbol.
4856 We output this even if we are discarding local symbols, although
4857 I'm not sure if this is correct. */
4858 elfsym
.st_value
= 0;
4860 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4861 elfsym
.st_other
= 0;
4862 elfsym
.st_shndx
= SHN_ABS
;
4863 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
4864 &elfsym
, bfd_abs_section_ptr
))
4868 /* Output a symbol for each section. We output these even if we are
4869 discarding local symbols, since they are used for relocs. These
4870 symbols have no names. We store the index of each one in the
4871 index field of the section, so that we can find it again when
4872 outputting relocs. */
4873 if (info
->strip
!= strip_all
4877 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4878 elfsym
.st_other
= 0;
4879 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
4881 o
= section_from_elf_index (abfd
, i
);
4883 o
->target_index
= bfd_get_symcount (abfd
);
4884 elfsym
.st_shndx
= i
;
4885 if (info
->relocateable
|| o
== NULL
)
4886 elfsym
.st_value
= 0;
4888 elfsym
.st_value
= o
->vma
;
4889 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
4895 /* Allocate some memory to hold information read in from the input
4897 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
4898 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
4899 finfo
.internal_relocs
= ((Elf_Internal_Rela
*)
4900 bfd_malloc (max_internal_reloc_count
4901 * sizeof (Elf_Internal_Rela
)
4902 * bed
->s
->int_rels_per_ext_rel
));
4903 finfo
.external_syms
= ((Elf_External_Sym
*)
4904 bfd_malloc (max_sym_count
4905 * sizeof (Elf_External_Sym
)));
4906 finfo
.internal_syms
= ((Elf_Internal_Sym
*)
4907 bfd_malloc (max_sym_count
4908 * sizeof (Elf_Internal_Sym
)));
4909 finfo
.indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
4910 finfo
.sections
= ((asection
**)
4911 bfd_malloc (max_sym_count
* sizeof (asection
*)));
4912 if ((finfo
.contents
== NULL
&& max_contents_size
!= 0)
4913 || (finfo
.external_relocs
== NULL
&& max_external_reloc_size
!= 0)
4914 || (finfo
.internal_relocs
== NULL
&& max_internal_reloc_count
!= 0)
4915 || (finfo
.external_syms
== NULL
&& max_sym_count
!= 0)
4916 || (finfo
.internal_syms
== NULL
&& max_sym_count
!= 0)
4917 || (finfo
.indices
== NULL
&& max_sym_count
!= 0)
4918 || (finfo
.sections
== NULL
&& max_sym_count
!= 0))
4921 /* Since ELF permits relocations to be against local symbols, we
4922 must have the local symbols available when we do the relocations.
4923 Since we would rather only read the local symbols once, and we
4924 would rather not keep them in memory, we handle all the
4925 relocations for a single input file at the same time.
4927 Unfortunately, there is no way to know the total number of local
4928 symbols until we have seen all of them, and the local symbol
4929 indices precede the global symbol indices. This means that when
4930 we are generating relocateable output, and we see a reloc against
4931 a global symbol, we can not know the symbol index until we have
4932 finished examining all the local symbols to see which ones we are
4933 going to output. To deal with this, we keep the relocations in
4934 memory, and don't output them until the end of the link. This is
4935 an unfortunate waste of memory, but I don't see a good way around
4936 it. Fortunately, it only happens when performing a relocateable
4937 link, which is not the common case. FIXME: If keep_memory is set
4938 we could write the relocs out and then read them again; I don't
4939 know how bad the memory loss will be. */
4941 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
4942 sub
->output_has_begun
= false;
4943 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4945 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
4947 if (p
->type
== bfd_indirect_link_order
4948 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
4949 == bfd_target_elf_flavour
))
4951 sub
= p
->u
.indirect
.section
->owner
;
4952 if (! sub
->output_has_begun
)
4954 if (! elf_link_input_bfd (&finfo
, sub
))
4956 sub
->output_has_begun
= true;
4959 else if (p
->type
== bfd_section_reloc_link_order
4960 || p
->type
== bfd_symbol_reloc_link_order
)
4962 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
4967 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
4973 /* That wrote out all the local symbols. Finish up the symbol table
4974 with the global symbols. Even if we want to strip everything we
4975 can, we still need to deal with those global symbols that got
4976 converted to local in a version script. */
4980 /* Output any global symbols that got converted to local in a
4981 version script. We do this in a separate step since ELF
4982 requires all local symbols to appear prior to any global
4983 symbols. FIXME: We should only do this if some global
4984 symbols were, in fact, converted to become local. FIXME:
4985 Will this work correctly with the Irix 5 linker? */
4986 eoinfo
.failed
= false;
4987 eoinfo
.finfo
= &finfo
;
4988 eoinfo
.localsyms
= true;
4989 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
4995 /* The sh_info field records the index of the first non local symbol. */
4996 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
4999 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
5001 Elf_Internal_Sym sym
;
5002 Elf_External_Sym
*dynsym
=
5003 (Elf_External_Sym
*) finfo
.dynsym_sec
->contents
;
5004 long last_local
= 0;
5006 /* Write out the section symbols for the output sections. */
5013 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5016 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5019 indx
= elf_section_data (s
)->this_idx
;
5020 BFD_ASSERT (indx
> 0);
5021 sym
.st_shndx
= indx
;
5022 sym
.st_value
= s
->vma
;
5024 elf_swap_symbol_out (abfd
, &sym
,
5025 dynsym
+ elf_section_data (s
)->dynindx
);
5028 last_local
= bfd_count_sections (abfd
);
5031 /* Write out the local dynsyms. */
5032 if (elf_hash_table (info
)->dynlocal
)
5034 struct elf_link_local_dynamic_entry
*e
;
5035 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
5039 sym
.st_size
= e
->isym
.st_size
;
5040 sym
.st_other
= e
->isym
.st_other
;
5042 /* Copy the internal symbol as is.
5043 Note that we saved a word of storage and overwrote
5044 the original st_name with the dynstr_index. */
5047 if (e
->isym
.st_shndx
> 0 && e
->isym
.st_shndx
< SHN_LORESERVE
)
5049 s
= bfd_section_from_elf_index (e
->input_bfd
,
5053 elf_section_data (s
->output_section
)->this_idx
;
5054 sym
.st_value
= (s
->output_section
->vma
5056 + e
->isym
.st_value
);
5059 if (last_local
< e
->dynindx
)
5060 last_local
= e
->dynindx
;
5062 elf_swap_symbol_out (abfd
, &sym
, dynsym
+ e
->dynindx
);
5066 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
5070 /* We get the global symbols from the hash table. */
5071 eoinfo
.failed
= false;
5072 eoinfo
.localsyms
= false;
5073 eoinfo
.finfo
= &finfo
;
5074 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5079 /* If backend needs to output some symbols not present in the hash
5080 table, do it now. */
5081 if (bed
->elf_backend_output_arch_syms
)
5083 typedef boolean (*out_sym_func
) PARAMS ((PTR
, const char *,
5087 if (! ((*bed
->elf_backend_output_arch_syms
)
5088 (abfd
, info
, (PTR
) &finfo
, (out_sym_func
) elf_link_output_sym
)))
5092 /* Flush all symbols to the file. */
5093 if (! elf_link_flush_output_syms (&finfo
))
5096 /* Now we know the size of the symtab section. */
5097 off
+= symtab_hdr
->sh_size
;
5099 /* Finish up and write out the symbol string table (.strtab)
5101 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5102 /* sh_name was set in prep_headers. */
5103 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5104 symstrtab_hdr
->sh_flags
= 0;
5105 symstrtab_hdr
->sh_addr
= 0;
5106 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
5107 symstrtab_hdr
->sh_entsize
= 0;
5108 symstrtab_hdr
->sh_link
= 0;
5109 symstrtab_hdr
->sh_info
= 0;
5110 /* sh_offset is set just below. */
5111 symstrtab_hdr
->sh_addralign
= 1;
5113 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, true);
5114 elf_tdata (abfd
)->next_file_pos
= off
;
5116 if (bfd_get_symcount (abfd
) > 0)
5118 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
5119 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
5123 /* Adjust the relocs to have the correct symbol indices. */
5124 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5126 if ((o
->flags
& SEC_RELOC
) == 0)
5129 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
5130 elf_section_data (o
)->rel_count
,
5131 elf_section_data (o
)->rel_hashes
);
5132 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
5133 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
5134 elf_section_data (o
)->rel_count2
,
5135 (elf_section_data (o
)->rel_hashes
5136 + elf_section_data (o
)->rel_count
));
5138 /* Set the reloc_count field to 0 to prevent write_relocs from
5139 trying to swap the relocs out itself. */
5143 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
5144 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
5146 /* If we are linking against a dynamic object, or generating a
5147 shared library, finish up the dynamic linking information. */
5150 Elf_External_Dyn
*dyncon
, *dynconend
;
5152 /* Fix up .dynamic entries. */
5153 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
5154 BFD_ASSERT (o
!= NULL
);
5156 dyncon
= (Elf_External_Dyn
*) o
->contents
;
5157 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
5158 for (; dyncon
< dynconend
; dyncon
++)
5160 Elf_Internal_Dyn dyn
;
5164 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
5171 if (relativecount
> 0 && dyncon
+ 1 < dynconend
)
5173 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
5175 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
5176 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
5179 if (dyn
.d_tag
!= DT_NULL
)
5181 dyn
.d_un
.d_val
= relativecount
;
5182 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5188 name
= info
->init_function
;
5191 name
= info
->fini_function
;
5194 struct elf_link_hash_entry
*h
;
5196 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
5197 false, false, true);
5199 && (h
->root
.type
== bfd_link_hash_defined
5200 || h
->root
.type
== bfd_link_hash_defweak
))
5202 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
5203 o
= h
->root
.u
.def
.section
;
5204 if (o
->output_section
!= NULL
)
5205 dyn
.d_un
.d_val
+= (o
->output_section
->vma
5206 + o
->output_offset
);
5209 /* The symbol is imported from another shared
5210 library and does not apply to this one. */
5214 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5229 name
= ".gnu.version_d";
5232 name
= ".gnu.version_r";
5235 name
= ".gnu.version";
5237 o
= bfd_get_section_by_name (abfd
, name
);
5238 BFD_ASSERT (o
!= NULL
);
5239 dyn
.d_un
.d_ptr
= o
->vma
;
5240 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5247 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
5252 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
5254 Elf_Internal_Shdr
*hdr
;
5256 hdr
= elf_elfsections (abfd
)[i
];
5257 if (hdr
->sh_type
== type
5258 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
5260 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
5261 dyn
.d_un
.d_val
+= hdr
->sh_size
;
5264 if (dyn
.d_un
.d_val
== 0
5265 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
5266 dyn
.d_un
.d_val
= hdr
->sh_addr
;
5270 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5276 /* If we have created any dynamic sections, then output them. */
5279 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
5282 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
5284 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5285 || o
->_raw_size
== 0
5286 || o
->output_section
== bfd_abs_section_ptr
)
5288 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
5290 /* At this point, we are only interested in sections
5291 created by elf_link_create_dynamic_sections. */
5294 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
5296 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
5298 if (! bfd_set_section_contents (abfd
, o
->output_section
,
5300 (file_ptr
) o
->output_offset
,
5306 /* The contents of the .dynstr section are actually in a
5308 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
5309 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
5310 || ! _bfd_stringtab_emit (abfd
,
5311 elf_hash_table (info
)->dynstr
))
5317 /* If we have optimized stabs strings, output them. */
5318 if (elf_hash_table (info
)->stab_info
!= NULL
)
5320 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
5324 if (finfo
.symstrtab
!= NULL
)
5325 _bfd_stringtab_free (finfo
.symstrtab
);
5326 if (finfo
.contents
!= NULL
)
5327 free (finfo
.contents
);
5328 if (finfo
.external_relocs
!= NULL
)
5329 free (finfo
.external_relocs
);
5330 if (finfo
.internal_relocs
!= NULL
)
5331 free (finfo
.internal_relocs
);
5332 if (finfo
.external_syms
!= NULL
)
5333 free (finfo
.external_syms
);
5334 if (finfo
.internal_syms
!= NULL
)
5335 free (finfo
.internal_syms
);
5336 if (finfo
.indices
!= NULL
)
5337 free (finfo
.indices
);
5338 if (finfo
.sections
!= NULL
)
5339 free (finfo
.sections
);
5340 if (finfo
.symbuf
!= NULL
)
5341 free (finfo
.symbuf
);
5342 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5344 if ((o
->flags
& SEC_RELOC
) != 0
5345 && elf_section_data (o
)->rel_hashes
!= NULL
)
5346 free (elf_section_data (o
)->rel_hashes
);
5349 elf_tdata (abfd
)->linker
= true;
5354 if (finfo
.symstrtab
!= NULL
)
5355 _bfd_stringtab_free (finfo
.symstrtab
);
5356 if (finfo
.contents
!= NULL
)
5357 free (finfo
.contents
);
5358 if (finfo
.external_relocs
!= NULL
)
5359 free (finfo
.external_relocs
);
5360 if (finfo
.internal_relocs
!= NULL
)
5361 free (finfo
.internal_relocs
);
5362 if (finfo
.external_syms
!= NULL
)
5363 free (finfo
.external_syms
);
5364 if (finfo
.internal_syms
!= NULL
)
5365 free (finfo
.internal_syms
);
5366 if (finfo
.indices
!= NULL
)
5367 free (finfo
.indices
);
5368 if (finfo
.sections
!= NULL
)
5369 free (finfo
.sections
);
5370 if (finfo
.symbuf
!= NULL
)
5371 free (finfo
.symbuf
);
5372 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5374 if ((o
->flags
& SEC_RELOC
) != 0
5375 && elf_section_data (o
)->rel_hashes
!= NULL
)
5376 free (elf_section_data (o
)->rel_hashes
);
5382 /* Add a symbol to the output symbol table. */
5385 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
5386 struct elf_final_link_info
*finfo
;
5388 Elf_Internal_Sym
*elfsym
;
5389 asection
*input_sec
;
5391 boolean (*output_symbol_hook
) PARAMS ((bfd
*,
5392 struct bfd_link_info
*info
,
5397 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
5398 elf_backend_link_output_symbol_hook
;
5399 if (output_symbol_hook
!= NULL
)
5401 if (! ((*output_symbol_hook
)
5402 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
5406 if (name
== (const char *) NULL
|| *name
== '\0')
5407 elfsym
->st_name
= 0;
5408 else if (input_sec
->flags
& SEC_EXCLUDE
)
5409 elfsym
->st_name
= 0;
5412 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5414 if (elfsym
->st_name
== (unsigned long) -1)
5418 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5420 if (! elf_link_flush_output_syms (finfo
))
5424 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
,
5425 (PTR
) (finfo
->symbuf
+ finfo
->symbuf_count
));
5426 ++finfo
->symbuf_count
;
5428 ++ bfd_get_symcount (finfo
->output_bfd
);
5433 /* Flush the output symbols to the file. */
5436 elf_link_flush_output_syms (finfo
)
5437 struct elf_final_link_info
*finfo
;
5439 if (finfo
->symbuf_count
> 0)
5441 Elf_Internal_Shdr
*symtab
;
5445 symtab
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5446 pos
= symtab
->sh_offset
+ symtab
->sh_size
;
5447 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
5448 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5449 || bfd_bwrite ((PTR
) finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5452 symtab
->sh_size
+= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
5454 finfo
->symbuf_count
= 0;
5460 /* Adjust all external symbols pointing into SEC_MERGE sections
5461 to reflect the object merging within the sections. */
5464 elf_link_sec_merge_syms (h
, data
)
5465 struct elf_link_hash_entry
*h
;
5470 if ((h
->root
.type
== bfd_link_hash_defined
5471 || h
->root
.type
== bfd_link_hash_defweak
)
5472 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
5473 && elf_section_data (sec
)->merge_info
)
5475 bfd
*output_bfd
= (bfd
*) data
;
5477 h
->root
.u
.def
.value
=
5478 _bfd_merged_section_offset (output_bfd
,
5479 &h
->root
.u
.def
.section
,
5480 elf_section_data (sec
)->merge_info
,
5481 h
->root
.u
.def
.value
, (bfd_vma
) 0);
5487 /* Add an external symbol to the symbol table. This is called from
5488 the hash table traversal routine. When generating a shared object,
5489 we go through the symbol table twice. The first time we output
5490 anything that might have been forced to local scope in a version
5491 script. The second time we output the symbols that are still
5495 elf_link_output_extsym (h
, data
)
5496 struct elf_link_hash_entry
*h
;
5499 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
5500 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
5502 Elf_Internal_Sym sym
;
5503 asection
*input_sec
;
5505 /* Decide whether to output this symbol in this pass. */
5506 if (eoinfo
->localsyms
)
5508 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
5513 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
5517 /* If we are not creating a shared library, and this symbol is
5518 referenced by a shared library but is not defined anywhere, then
5519 warn that it is undefined. If we do not do this, the runtime
5520 linker will complain that the symbol is undefined when the
5521 program is run. We don't have to worry about symbols that are
5522 referenced by regular files, because we will already have issued
5523 warnings for them. */
5524 if (! finfo
->info
->relocateable
5525 && ! finfo
->info
->allow_shlib_undefined
5526 && ! finfo
->info
->shared
5527 && h
->root
.type
== bfd_link_hash_undefined
5528 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
5529 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
5531 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
5532 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
5533 (asection
*) NULL
, (bfd_vma
) 0, true)))
5535 eoinfo
->failed
= true;
5540 /* We don't want to output symbols that have never been mentioned by
5541 a regular file, or that we have been told to strip. However, if
5542 h->indx is set to -2, the symbol is used by a reloc and we must
5546 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
5547 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
5548 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
5549 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
5551 else if (finfo
->info
->strip
== strip_all
5552 || (finfo
->info
->strip
== strip_some
5553 && bfd_hash_lookup (finfo
->info
->keep_hash
,
5554 h
->root
.root
.string
,
5555 false, false) == NULL
))
5560 /* If we're stripping it, and it's not a dynamic symbol, there's
5561 nothing else to do unless it is a forced local symbol. */
5564 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
5568 sym
.st_size
= h
->size
;
5569 sym
.st_other
= h
->other
;
5570 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
5571 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
5572 else if (h
->root
.type
== bfd_link_hash_undefweak
5573 || h
->root
.type
== bfd_link_hash_defweak
)
5574 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
5576 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
5578 switch (h
->root
.type
)
5581 case bfd_link_hash_new
:
5585 case bfd_link_hash_undefined
:
5586 input_sec
= bfd_und_section_ptr
;
5587 sym
.st_shndx
= SHN_UNDEF
;
5590 case bfd_link_hash_undefweak
:
5591 input_sec
= bfd_und_section_ptr
;
5592 sym
.st_shndx
= SHN_UNDEF
;
5595 case bfd_link_hash_defined
:
5596 case bfd_link_hash_defweak
:
5598 input_sec
= h
->root
.u
.def
.section
;
5599 if (input_sec
->output_section
!= NULL
)
5602 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
5603 input_sec
->output_section
);
5604 if (sym
.st_shndx
== (unsigned short) -1)
5606 (*_bfd_error_handler
)
5607 (_("%s: could not find output section %s for input section %s"),
5608 bfd_get_filename (finfo
->output_bfd
),
5609 input_sec
->output_section
->name
,
5611 eoinfo
->failed
= true;
5615 /* ELF symbols in relocateable files are section relative,
5616 but in nonrelocateable files they are virtual
5618 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
5619 if (! finfo
->info
->relocateable
)
5620 sym
.st_value
+= input_sec
->output_section
->vma
;
5624 BFD_ASSERT (input_sec
->owner
== NULL
5625 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
5626 sym
.st_shndx
= SHN_UNDEF
;
5627 input_sec
= bfd_und_section_ptr
;
5632 case bfd_link_hash_common
:
5633 input_sec
= h
->root
.u
.c
.p
->section
;
5634 sym
.st_shndx
= SHN_COMMON
;
5635 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
5638 case bfd_link_hash_indirect
:
5639 /* These symbols are created by symbol versioning. They point
5640 to the decorated version of the name. For example, if the
5641 symbol foo@@GNU_1.2 is the default, which should be used when
5642 foo is used with no version, then we add an indirect symbol
5643 foo which points to foo@@GNU_1.2. We ignore these symbols,
5644 since the indirected symbol is already in the hash table. */
5647 case bfd_link_hash_warning
:
5648 /* We can't represent these symbols in ELF, although a warning
5649 symbol may have come from a .gnu.warning.SYMBOL section. We
5650 just put the target symbol in the hash table. If the target
5651 symbol does not really exist, don't do anything. */
5652 if (h
->root
.u
.i
.link
->type
== bfd_link_hash_new
)
5654 return (elf_link_output_extsym
5655 ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
));
5658 /* Give the processor backend a chance to tweak the symbol value,
5659 and also to finish up anything that needs to be done for this
5661 if ((h
->dynindx
!= -1
5662 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
5663 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
5665 struct elf_backend_data
*bed
;
5667 bed
= get_elf_backend_data (finfo
->output_bfd
);
5668 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
5669 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
5671 eoinfo
->failed
= true;
5676 /* If we are marking the symbol as undefined, and there are no
5677 non-weak references to this symbol from a regular object, then
5678 mark the symbol as weak undefined; if there are non-weak
5679 references, mark the symbol as strong. We can't do this earlier,
5680 because it might not be marked as undefined until the
5681 finish_dynamic_symbol routine gets through with it. */
5682 if (sym
.st_shndx
== SHN_UNDEF
5683 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
5684 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
5685 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
5689 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
5690 bindtype
= STB_GLOBAL
;
5692 bindtype
= STB_WEAK
;
5693 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
5696 /* If a symbol is not defined locally, we clear the visibility
5698 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
5699 sym
.st_other
^= ELF_ST_VISIBILITY (sym
.st_other
);
5701 /* If this symbol should be put in the .dynsym section, then put it
5702 there now. We have already know the symbol index. We also fill
5703 in the entry in the .hash section. */
5704 if (h
->dynindx
!= -1
5705 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
5709 size_t hash_entry_size
;
5710 bfd_byte
*bucketpos
;
5712 Elf_External_Sym
*esym
;
5714 sym
.st_name
= h
->dynstr_index
;
5715 esym
= (Elf_External_Sym
*) finfo
->dynsym_sec
->contents
+ h
->dynindx
;
5716 elf_swap_symbol_out (finfo
->output_bfd
, &sym
, (PTR
) esym
);
5718 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
5719 bucket
= h
->elf_hash_value
% bucketcount
;
5721 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
5722 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
5723 + (bucket
+ 2) * hash_entry_size
);
5724 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
5725 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, (bfd_vma
) h
->dynindx
,
5727 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
5728 ((bfd_byte
*) finfo
->hash_sec
->contents
5729 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
5731 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
5733 Elf_Internal_Versym iversym
;
5734 Elf_External_Versym
*eversym
;
5736 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
5738 if (h
->verinfo
.verdef
== NULL
)
5739 iversym
.vs_vers
= 0;
5741 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
5745 if (h
->verinfo
.vertree
== NULL
)
5746 iversym
.vs_vers
= 1;
5748 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
5751 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
5752 iversym
.vs_vers
|= VERSYM_HIDDEN
;
5754 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
5755 eversym
+= h
->dynindx
;
5756 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
5760 /* If we're stripping it, then it was just a dynamic symbol, and
5761 there's nothing else to do. */
5765 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
5767 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
5769 eoinfo
->failed
= true;
5776 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
5777 originated from the section given by INPUT_REL_HDR) to the
5781 elf_link_output_relocs (output_bfd
, input_section
, input_rel_hdr
,
5784 asection
*input_section
;
5785 Elf_Internal_Shdr
*input_rel_hdr
;
5786 Elf_Internal_Rela
*internal_relocs
;
5788 Elf_Internal_Rela
*irela
;
5789 Elf_Internal_Rela
*irelaend
;
5790 Elf_Internal_Shdr
*output_rel_hdr
;
5791 asection
*output_section
;
5792 unsigned int *rel_countp
= NULL
;
5793 struct elf_backend_data
*bed
;
5796 output_section
= input_section
->output_section
;
5797 output_rel_hdr
= NULL
;
5799 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
5800 == input_rel_hdr
->sh_entsize
)
5802 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
5803 rel_countp
= &elf_section_data (output_section
)->rel_count
;
5805 else if (elf_section_data (output_section
)->rel_hdr2
5806 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
5807 == input_rel_hdr
->sh_entsize
))
5809 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
5810 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
5813 BFD_ASSERT (output_rel_hdr
!= NULL
);
5815 bed
= get_elf_backend_data (output_bfd
);
5816 irela
= internal_relocs
;
5817 irelaend
= irela
+ NUM_SHDR_ENTRIES (input_rel_hdr
)
5818 * bed
->s
->int_rels_per_ext_rel
;
5820 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
5822 Elf_External_Rel
*erel
;
5823 Elf_Internal_Rel
*irel
;
5825 amt
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
5826 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (amt
);
5829 (*_bfd_error_handler
) (_("Error: out of memory"));
5833 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
+ *rel_countp
);
5834 for (; irela
< irelaend
; irela
+= bed
->s
->int_rels_per_ext_rel
, erel
++)
5838 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
5840 irel
[i
].r_offset
= irela
[i
].r_offset
;
5841 irel
[i
].r_info
= irela
[i
].r_info
;
5842 BFD_ASSERT (irela
[i
].r_addend
== 0);
5845 if (bed
->s
->swap_reloc_out
)
5846 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, (PTR
) erel
);
5848 elf_swap_reloc_out (output_bfd
, irel
, erel
);
5855 Elf_External_Rela
*erela
;
5857 BFD_ASSERT (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
));
5859 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
+ *rel_countp
);
5860 for (; irela
< irelaend
; irela
+= bed
->s
->int_rels_per_ext_rel
, erela
++)
5861 if (bed
->s
->swap_reloca_out
)
5862 (*bed
->s
->swap_reloca_out
) (output_bfd
, irela
, (PTR
) erela
);
5864 elf_swap_reloca_out (output_bfd
, irela
, erela
);
5867 /* Bump the counter, so that we know where to add the next set of
5869 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
5872 /* Link an input file into the linker output file. This function
5873 handles all the sections and relocations of the input file at once.
5874 This is so that we only have to read the local symbols once, and
5875 don't have to keep them in memory. */
5878 elf_link_input_bfd (finfo
, input_bfd
)
5879 struct elf_final_link_info
*finfo
;
5882 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
5883 bfd
*, asection
*, bfd_byte
*,
5884 Elf_Internal_Rela
*,
5885 Elf_Internal_Sym
*, asection
**));
5887 Elf_Internal_Shdr
*symtab_hdr
;
5890 Elf_External_Sym
*external_syms
;
5891 Elf_External_Sym
*esym
;
5892 Elf_External_Sym
*esymend
;
5893 Elf_Internal_Sym
*isym
;
5895 asection
**ppsection
;
5897 struct elf_backend_data
*bed
;
5898 boolean emit_relocs
;
5900 output_bfd
= finfo
->output_bfd
;
5901 bed
= get_elf_backend_data (output_bfd
);
5902 relocate_section
= bed
->elf_backend_relocate_section
;
5904 /* If this is a dynamic object, we don't want to do anything here:
5905 we don't want the local symbols, and we don't want the section
5907 if ((input_bfd
->flags
& DYNAMIC
) != 0)
5910 emit_relocs
= (finfo
->info
->relocateable
5911 || finfo
->info
->emitrelocations
5912 || bed
->elf_backend_emit_relocs
);
5914 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
5915 if (elf_bad_symtab (input_bfd
))
5917 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
5922 locsymcount
= symtab_hdr
->sh_info
;
5923 extsymoff
= symtab_hdr
->sh_info
;
5926 /* Read the local symbols. */
5927 if (symtab_hdr
->contents
!= NULL
)
5928 external_syms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
5929 else if (locsymcount
== 0)
5930 external_syms
= NULL
;
5933 bfd_size_type amt
= locsymcount
* sizeof (Elf_External_Sym
);
5934 external_syms
= finfo
->external_syms
;
5935 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
5936 || bfd_bread (external_syms
, amt
, input_bfd
) != amt
)
5940 /* Swap in the local symbols and write out the ones which we know
5941 are going into the output file. */
5942 esym
= external_syms
;
5943 esymend
= esym
+ locsymcount
;
5944 isym
= finfo
->internal_syms
;
5945 pindex
= finfo
->indices
;
5946 ppsection
= finfo
->sections
;
5947 for (; esym
< esymend
; esym
++, isym
++, pindex
++, ppsection
++)
5951 Elf_Internal_Sym osym
;
5953 elf_swap_symbol_in (input_bfd
, esym
, isym
);
5956 if (elf_bad_symtab (input_bfd
))
5958 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
5965 if (isym
->st_shndx
== SHN_UNDEF
)
5966 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
)
5978 isec
= bfd_abs_section_ptr
;
5979 else if (isym
->st_shndx
== SHN_COMMON
)
5980 isec
= bfd_com_section_ptr
;
5989 /* Don't output the first, undefined, symbol. */
5990 if (esym
== external_syms
)
5993 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
5995 /* If this is a discarded link-once section symbol, set its
5996 value to 0. We should really undefine it, and complain
5997 if anything in the final link tries to use it, but
5998 DWARF-based exception handling might have an entry in
5999 .eh_frame to describe a routine in the linkonce section,
6000 and it turns out to be hard to remove the .eh_frame entry
6003 && (bfd_get_section_flags (input_bfd
, isec
) & SEC_LINK_ONCE
) != 0
6004 && bfd_is_abs_section (isec
->output_section
))
6009 /* We never output section symbols. Instead, we use the
6010 section symbol of the corresponding section in the output
6015 /* If we are stripping all symbols, we don't want to output this
6017 if (finfo
->info
->strip
== strip_all
)
6020 /* If we are discarding all local symbols, we don't want to
6021 output this one. If we are generating a relocateable output
6022 file, then some of the local symbols may be required by
6023 relocs; we output them below as we discover that they are
6025 if (finfo
->info
->discard
== discard_all
)
6028 /* If this symbol is defined in a section which we are
6029 discarding, we don't need to keep it, but note that
6030 linker_mark is only reliable for sections that have contents.
6031 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6032 as well as linker_mark. */
6033 if (isym
->st_shndx
> 0
6034 && isym
->st_shndx
< SHN_LORESERVE
6036 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6037 || (! finfo
->info
->relocateable
6038 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6041 /* Get the name of the symbol. */
6042 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6047 /* See if we are discarding symbols with this name. */
6048 if ((finfo
->info
->strip
== strip_some
6049 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
6051 || (((finfo
->info
->discard
== discard_sec_merge
6052 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocateable
)
6053 || finfo
->info
->discard
== discard_l
)
6054 && bfd_is_local_label_name (input_bfd
, name
)))
6057 /* If we get here, we are going to output this symbol. */
6061 /* Adjust the section index for the output file. */
6062 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6063 isec
->output_section
);
6064 if (osym
.st_shndx
== (unsigned short) -1)
6067 *pindex
= bfd_get_symcount (output_bfd
);
6069 /* ELF symbols in relocateable files are section relative, but
6070 in executable files they are virtual addresses. Note that
6071 this code assumes that all ELF sections have an associated
6072 BFD section with a reasonable value for output_offset; below
6073 we assume that they also have a reasonable value for
6074 output_section. Any special sections must be set up to meet
6075 these requirements. */
6076 osym
.st_value
+= isec
->output_offset
;
6077 if (! finfo
->info
->relocateable
)
6078 osym
.st_value
+= isec
->output_section
->vma
;
6080 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
6084 /* Relocate the contents of each section. */
6085 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6089 if (! o
->linker_mark
)
6091 /* This section was omitted from the link. */
6095 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6096 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6099 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6101 /* Section was created by elf_link_create_dynamic_sections
6106 /* Get the contents of the section. They have been cached by a
6107 relaxation routine. Note that o is a section in an input
6108 file, so the contents field will not have been set by any of
6109 the routines which work on output files. */
6110 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6111 contents
= elf_section_data (o
)->this_hdr
.contents
;
6114 contents
= finfo
->contents
;
6115 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
6116 (file_ptr
) 0, o
->_raw_size
))
6120 if ((o
->flags
& SEC_RELOC
) != 0)
6122 Elf_Internal_Rela
*internal_relocs
;
6124 /* Get the swapped relocs. */
6125 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
6126 (input_bfd
, o
, finfo
->external_relocs
,
6127 finfo
->internal_relocs
, false));
6128 if (internal_relocs
== NULL
6129 && o
->reloc_count
> 0)
6132 /* Relocate the section by invoking a back end routine.
6134 The back end routine is responsible for adjusting the
6135 section contents as necessary, and (if using Rela relocs
6136 and generating a relocateable output file) adjusting the
6137 reloc addend as necessary.
6139 The back end routine does not have to worry about setting
6140 the reloc address or the reloc symbol index.
6142 The back end routine is given a pointer to the swapped in
6143 internal symbols, and can access the hash table entries
6144 for the external symbols via elf_sym_hashes (input_bfd).
6146 When generating relocateable output, the back end routine
6147 must handle STB_LOCAL/STT_SECTION symbols specially. The
6148 output symbol is going to be a section symbol
6149 corresponding to the output section, which will require
6150 the addend to be adjusted. */
6152 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6153 input_bfd
, o
, contents
,
6155 finfo
->internal_syms
,
6161 Elf_Internal_Rela
*irela
;
6162 Elf_Internal_Rela
*irelaend
;
6163 struct elf_link_hash_entry
**rel_hash
;
6164 Elf_Internal_Shdr
*input_rel_hdr
;
6165 unsigned int next_erel
;
6166 void (*reloc_emitter
) PARAMS ((bfd
*, asection
*,
6167 Elf_Internal_Shdr
*,
6168 Elf_Internal_Rela
*));
6170 /* Adjust the reloc addresses and symbol indices. */
6172 irela
= internal_relocs
;
6173 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6174 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6175 + elf_section_data (o
->output_section
)->rel_count
6176 + elf_section_data (o
->output_section
)->rel_count2
);
6177 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6179 unsigned long r_symndx
;
6182 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6188 irela
->r_offset
+= o
->output_offset
;
6190 /* Relocs in an executable have to be virtual addresses. */
6191 if (finfo
->info
->emitrelocations
)
6192 irela
->r_offset
+= o
->output_section
->vma
;
6194 r_symndx
= ELF_R_SYM (irela
->r_info
);
6199 if (r_symndx
>= locsymcount
6200 || (elf_bad_symtab (input_bfd
)
6201 && finfo
->sections
[r_symndx
] == NULL
))
6203 struct elf_link_hash_entry
*rh
;
6206 /* This is a reloc against a global symbol. We
6207 have not yet output all the local symbols, so
6208 we do not know the symbol index of any global
6209 symbol. We set the rel_hash entry for this
6210 reloc to point to the global hash table entry
6211 for this symbol. The symbol index is then
6212 set at the end of elf_bfd_final_link. */
6213 indx
= r_symndx
- extsymoff
;
6214 rh
= elf_sym_hashes (input_bfd
)[indx
];
6215 while (rh
->root
.type
== bfd_link_hash_indirect
6216 || rh
->root
.type
== bfd_link_hash_warning
)
6217 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6219 /* Setting the index to -2 tells
6220 elf_link_output_extsym that this symbol is
6222 BFD_ASSERT (rh
->indx
< 0);
6230 /* This is a reloc against a local symbol. */
6233 isym
= finfo
->internal_syms
+ r_symndx
;
6234 sec
= finfo
->sections
[r_symndx
];
6235 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6237 /* I suppose the backend ought to fill in the
6238 section of any STT_SECTION symbol against a
6239 processor specific section. If we have
6240 discarded a section, the output_section will
6241 be the absolute section. */
6243 && (bfd_is_abs_section (sec
)
6244 || (sec
->output_section
!= NULL
6245 && bfd_is_abs_section (sec
->output_section
))))
6247 else if (sec
== NULL
|| sec
->owner
== NULL
)
6249 bfd_set_error (bfd_error_bad_value
);
6254 r_symndx
= sec
->output_section
->target_index
;
6255 BFD_ASSERT (r_symndx
!= 0);
6260 if (finfo
->indices
[r_symndx
] == -1)
6262 unsigned long shlink
;
6266 if (finfo
->info
->strip
== strip_all
)
6268 /* You can't do ld -r -s. */
6269 bfd_set_error (bfd_error_invalid_operation
);
6273 /* This symbol was skipped earlier, but
6274 since it is needed by a reloc, we
6275 must output it now. */
6276 shlink
= symtab_hdr
->sh_link
;
6277 name
= (bfd_elf_string_from_elf_section
6278 (input_bfd
, shlink
, isym
->st_name
));
6282 osec
= sec
->output_section
;
6284 _bfd_elf_section_from_bfd_section (output_bfd
,
6286 if (isym
->st_shndx
== (unsigned short) -1)
6289 isym
->st_value
+= sec
->output_offset
;
6290 if (! finfo
->info
->relocateable
)
6291 isym
->st_value
+= osec
->vma
;
6293 finfo
->indices
[r_symndx
]
6294 = bfd_get_symcount (output_bfd
);
6296 if (! elf_link_output_sym (finfo
, name
, isym
, sec
))
6300 r_symndx
= finfo
->indices
[r_symndx
];
6303 irela
->r_info
= ELF_R_INFO (r_symndx
,
6304 ELF_R_TYPE (irela
->r_info
));
6307 /* Swap out the relocs. */
6308 if (bed
->elf_backend_emit_relocs
6309 && !(finfo
->info
->relocateable
6310 || finfo
->info
->emitrelocations
))
6311 reloc_emitter
= bed
->elf_backend_emit_relocs
;
6313 reloc_emitter
= elf_link_output_relocs
;
6315 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6316 (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
, internal_relocs
);
6318 input_rel_hdr
= elf_section_data (o
)->rel_hdr2
;
6321 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
6322 * bed
->s
->int_rels_per_ext_rel
);
6323 reloc_emitter (output_bfd
, o
, input_rel_hdr
, internal_relocs
);
6329 /* Write out the modified section contents. */
6330 if (elf_section_data (o
)->stab_info
)
6332 if (! (_bfd_write_section_stabs
6333 (output_bfd
, &elf_hash_table (finfo
->info
)->stab_info
,
6334 o
, &elf_section_data (o
)->stab_info
, contents
)))
6337 else if (elf_section_data (o
)->merge_info
)
6339 if (! (_bfd_write_merged_section
6340 (output_bfd
, o
, elf_section_data (o
)->merge_info
)))
6345 bfd_size_type sec_size
;
6347 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
6348 if (! (o
->flags
& SEC_EXCLUDE
)
6349 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
6351 (file_ptr
) o
->output_offset
,
6360 /* Generate a reloc when linking an ELF file. This is a reloc
6361 requested by the linker, and does come from any input file. This
6362 is used to build constructor and destructor tables when linking
6366 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
6368 struct bfd_link_info
*info
;
6369 asection
*output_section
;
6370 struct bfd_link_order
*link_order
;
6372 reloc_howto_type
*howto
;
6376 struct elf_link_hash_entry
**rel_hash_ptr
;
6377 Elf_Internal_Shdr
*rel_hdr
;
6378 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
6380 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
6383 bfd_set_error (bfd_error_bad_value
);
6387 addend
= link_order
->u
.reloc
.p
->addend
;
6389 /* Figure out the symbol index. */
6390 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
6391 + elf_section_data (output_section
)->rel_count
6392 + elf_section_data (output_section
)->rel_count2
);
6393 if (link_order
->type
== bfd_section_reloc_link_order
)
6395 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
6396 BFD_ASSERT (indx
!= 0);
6397 *rel_hash_ptr
= NULL
;
6401 struct elf_link_hash_entry
*h
;
6403 /* Treat a reloc against a defined symbol as though it were
6404 actually against the section. */
6405 h
= ((struct elf_link_hash_entry
*)
6406 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
6407 link_order
->u
.reloc
.p
->u
.name
,
6408 false, false, true));
6410 && (h
->root
.type
== bfd_link_hash_defined
6411 || h
->root
.type
== bfd_link_hash_defweak
))
6415 section
= h
->root
.u
.def
.section
;
6416 indx
= section
->output_section
->target_index
;
6417 *rel_hash_ptr
= NULL
;
6418 /* It seems that we ought to add the symbol value to the
6419 addend here, but in practice it has already been added
6420 because it was passed to constructor_callback. */
6421 addend
+= section
->output_section
->vma
+ section
->output_offset
;
6425 /* Setting the index to -2 tells elf_link_output_extsym that
6426 this symbol is used by a reloc. */
6433 if (! ((*info
->callbacks
->unattached_reloc
)
6434 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
6435 (asection
*) NULL
, (bfd_vma
) 0)))
6441 /* If this is an inplace reloc, we must write the addend into the
6443 if (howto
->partial_inplace
&& addend
!= 0)
6446 bfd_reloc_status_type rstat
;
6449 const char *sym_name
;
6451 size
= bfd_get_reloc_size (howto
);
6452 buf
= (bfd_byte
*) bfd_zmalloc (size
);
6453 if (buf
== (bfd_byte
*) NULL
)
6455 rstat
= _bfd_relocate_contents (howto
, output_bfd
, (bfd_vma
) addend
, buf
);
6462 case bfd_reloc_outofrange
:
6465 case bfd_reloc_overflow
:
6466 if (link_order
->type
== bfd_section_reloc_link_order
)
6467 sym_name
= bfd_section_name (output_bfd
,
6468 link_order
->u
.reloc
.p
->u
.section
);
6470 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
6471 if (! ((*info
->callbacks
->reloc_overflow
)
6472 (info
, sym_name
, howto
->name
, addend
,
6473 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
6480 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
6481 (file_ptr
) link_order
->offset
, size
);
6487 /* The address of a reloc is relative to the section in a
6488 relocateable file, and is a virtual address in an executable
6490 offset
= link_order
->offset
;
6491 if (! info
->relocateable
)
6492 offset
+= output_section
->vma
;
6494 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
6496 if (rel_hdr
->sh_type
== SHT_REL
)
6499 Elf_Internal_Rel
*irel
;
6500 Elf_External_Rel
*erel
;
6503 size
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
6504 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (size
);
6508 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
6509 irel
[i
].r_offset
= offset
;
6510 irel
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
6512 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
6513 + elf_section_data (output_section
)->rel_count
);
6515 if (bed
->s
->swap_reloc_out
)
6516 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, (bfd_byte
*) erel
);
6518 elf_swap_reloc_out (output_bfd
, irel
, erel
);
6525 Elf_Internal_Rela
*irela
;
6526 Elf_External_Rela
*erela
;
6529 size
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
6530 irela
= (Elf_Internal_Rela
*) bfd_zmalloc (size
);
6534 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
6535 irela
[i
].r_offset
= offset
;
6536 irela
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
6537 irela
[0].r_addend
= addend
;
6539 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
6540 + elf_section_data (output_section
)->rel_count
);
6542 if (bed
->s
->swap_reloca_out
)
6543 (*bed
->s
->swap_reloca_out
) (output_bfd
, irela
, (bfd_byte
*) erela
);
6545 elf_swap_reloca_out (output_bfd
, irela
, erela
);
6548 ++elf_section_data (output_section
)->rel_count
;
6553 /* Allocate a pointer to live in a linker created section. */
6556 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
6558 struct bfd_link_info
*info
;
6559 elf_linker_section_t
*lsect
;
6560 struct elf_link_hash_entry
*h
;
6561 const Elf_Internal_Rela
*rel
;
6563 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
6564 elf_linker_section_pointers_t
*linker_section_ptr
;
6565 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
6568 BFD_ASSERT (lsect
!= NULL
);
6570 /* Is this a global symbol? */
6573 /* Has this symbol already been allocated? If so, our work is done. */
6574 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
6579 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
6580 /* Make sure this symbol is output as a dynamic symbol. */
6581 if (h
->dynindx
== -1)
6583 if (! elf_link_record_dynamic_symbol (info
, h
))
6587 if (lsect
->rel_section
)
6588 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
6592 /* Allocation of a pointer to a local symbol. */
6593 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
6595 /* Allocate a table to hold the local symbols if first time. */
6598 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
6599 register unsigned int i
;
6602 amt
*= sizeof (elf_linker_section_pointers_t
*);
6603 ptr
= (elf_linker_section_pointers_t
**) bfd_alloc (abfd
, amt
);
6608 elf_local_ptr_offsets (abfd
) = ptr
;
6609 for (i
= 0; i
< num_symbols
; i
++)
6610 ptr
[i
] = (elf_linker_section_pointers_t
*) 0;
6613 /* Has this symbol already been allocated? If so, our work is done. */
6614 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
6619 ptr_linker_section_ptr
= &ptr
[r_symndx
];
6623 /* If we are generating a shared object, we need to
6624 output a R_<xxx>_RELATIVE reloc so that the
6625 dynamic linker can adjust this GOT entry. */
6626 BFD_ASSERT (lsect
->rel_section
!= NULL
);
6627 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
6631 /* Allocate space for a pointer in the linker section, and allocate
6632 a new pointer record from internal memory. */
6633 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
6634 amt
= sizeof (elf_linker_section_pointers_t
);
6635 linker_section_ptr
= (elf_linker_section_pointers_t
*) bfd_alloc (abfd
, amt
);
6637 if (!linker_section_ptr
)
6640 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
6641 linker_section_ptr
->addend
= rel
->r_addend
;
6642 linker_section_ptr
->which
= lsect
->which
;
6643 linker_section_ptr
->written_address_p
= false;
6644 *ptr_linker_section_ptr
= linker_section_ptr
;
6647 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
6649 linker_section_ptr
->offset
= (lsect
->section
->_raw_size
6650 - lsect
->hole_size
+ (ARCH_SIZE
/ 8));
6651 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
6652 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
6653 if (lsect
->sym_hash
)
6655 /* Bump up symbol value if needed. */
6656 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
6658 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
6659 lsect
->sym_hash
->root
.root
.string
,
6660 (long) ARCH_SIZE
/ 8,
6661 (long) lsect
->sym_hash
->root
.u
.def
.value
);
6667 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
6669 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
6673 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
6674 lsect
->name
, (long) linker_section_ptr
->offset
,
6675 (long) lsect
->section
->_raw_size
);
6682 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
6685 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
6688 /* Fill in the address for a pointer generated in a linker section. */
6691 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
,
6692 relocation
, rel
, relative_reloc
)
6695 struct bfd_link_info
*info
;
6696 elf_linker_section_t
*lsect
;
6697 struct elf_link_hash_entry
*h
;
6699 const Elf_Internal_Rela
*rel
;
6702 elf_linker_section_pointers_t
*linker_section_ptr
;
6704 BFD_ASSERT (lsect
!= NULL
);
6708 /* Handle global symbol. */
6709 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
6710 (h
->linker_section_pointer
,
6714 BFD_ASSERT (linker_section_ptr
!= NULL
);
6716 if (! elf_hash_table (info
)->dynamic_sections_created
6719 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
6721 /* This is actually a static link, or it is a
6722 -Bsymbolic link and the symbol is defined
6723 locally. We must initialize this entry in the
6726 When doing a dynamic link, we create a .rela.<xxx>
6727 relocation entry to initialize the value. This
6728 is done in the finish_dynamic_symbol routine. */
6729 if (!linker_section_ptr
->written_address_p
)
6731 linker_section_ptr
->written_address_p
= true;
6732 bfd_put_ptr (output_bfd
,
6733 relocation
+ linker_section_ptr
->addend
,
6734 (lsect
->section
->contents
6735 + linker_section_ptr
->offset
));
6741 /* Handle local symbol. */
6742 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
6743 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
6744 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
6745 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
6746 (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
6750 BFD_ASSERT (linker_section_ptr
!= NULL
);
6752 /* Write out pointer if it hasn't been rewritten out before. */
6753 if (!linker_section_ptr
->written_address_p
)
6755 linker_section_ptr
->written_address_p
= true;
6756 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
6757 lsect
->section
->contents
+ linker_section_ptr
->offset
);
6761 asection
*srel
= lsect
->rel_section
;
6762 Elf_Internal_Rela
*outrel
;
6763 Elf_External_Rela
*erel
;
6764 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
6768 amt
= sizeof (Elf_Internal_Rela
) * bed
->s
->int_rels_per_ext_rel
;
6769 outrel
= (Elf_Internal_Rela
*) bfd_zmalloc (amt
);
6772 (*_bfd_error_handler
) (_("Error: out of memory"));
6776 /* We need to generate a relative reloc for the dynamic
6780 srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
6782 lsect
->rel_section
= srel
;
6785 BFD_ASSERT (srel
!= NULL
);
6787 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
6788 outrel
[i
].r_offset
= (lsect
->section
->output_section
->vma
6789 + lsect
->section
->output_offset
6790 + linker_section_ptr
->offset
);
6791 outrel
[0].r_info
= ELF_R_INFO (0, relative_reloc
);
6792 outrel
[0].r_addend
= 0;
6793 erel
= (Elf_External_Rela
*) lsect
->section
->contents
;
6794 erel
+= elf_section_data (lsect
->section
)->rel_count
;
6795 elf_swap_reloca_out (output_bfd
, outrel
, erel
);
6796 ++elf_section_data (lsect
->section
)->rel_count
;
6803 relocation
= (lsect
->section
->output_offset
6804 + linker_section_ptr
->offset
6805 - lsect
->hole_offset
6806 - lsect
->sym_offset
);
6810 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
6811 lsect
->name
, (long) relocation
, (long) relocation
);
6814 /* Subtract out the addend, because it will get added back in by the normal
6816 return relocation
- linker_section_ptr
->addend
;
6819 /* Garbage collect unused sections. */
6821 static boolean elf_gc_mark
6822 PARAMS ((struct bfd_link_info
*info
, asection
*sec
,
6823 asection
* (*gc_mark_hook
)
6824 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
6825 struct elf_link_hash_entry
*, Elf_Internal_Sym
*))));
6827 static boolean elf_gc_sweep
6828 PARAMS ((struct bfd_link_info
*info
,
6829 boolean (*gc_sweep_hook
)
6830 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
6831 const Elf_Internal_Rela
*relocs
))));
6833 static boolean elf_gc_sweep_symbol
6834 PARAMS ((struct elf_link_hash_entry
*h
, PTR idxptr
));
6836 static boolean elf_gc_allocate_got_offsets
6837 PARAMS ((struct elf_link_hash_entry
*h
, PTR offarg
));
6839 static boolean elf_gc_propagate_vtable_entries_used
6840 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
6842 static boolean elf_gc_smash_unused_vtentry_relocs
6843 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
6845 /* The mark phase of garbage collection. For a given section, mark
6846 it, and all the sections which define symbols to which it refers. */
6849 elf_gc_mark (info
, sec
, gc_mark_hook
)
6850 struct bfd_link_info
*info
;
6852 asection
* (*gc_mark_hook
)
6853 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
6854 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
6860 /* Look through the section relocs. */
6862 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
6864 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
6865 Elf_Internal_Shdr
*symtab_hdr
;
6866 struct elf_link_hash_entry
**sym_hashes
;
6869 Elf_External_Sym
*locsyms
, *freesyms
= NULL
;
6870 bfd
*input_bfd
= sec
->owner
;
6871 struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
6873 /* GCFIXME: how to arrange so that relocs and symbols are not
6874 reread continually? */
6876 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6877 sym_hashes
= elf_sym_hashes (input_bfd
);
6879 /* Read the local symbols. */
6880 if (elf_bad_symtab (input_bfd
))
6882 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6886 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
6887 if (symtab_hdr
->contents
)
6888 locsyms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
6889 else if (nlocsyms
== 0)
6893 bfd_size_type amt
= nlocsyms
* sizeof (Elf_External_Sym
);
6894 locsyms
= freesyms
= bfd_malloc (amt
);
6895 if (freesyms
== NULL
6896 || bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
6897 || bfd_bread (locsyms
, amt
, input_bfd
) != amt
)
6904 /* Read the relocations. */
6905 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
6906 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
6907 info
->keep_memory
));
6908 if (relstart
== NULL
)
6913 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6915 for (rel
= relstart
; rel
< relend
; rel
++)
6917 unsigned long r_symndx
;
6919 struct elf_link_hash_entry
*h
;
6922 r_symndx
= ELF_R_SYM (rel
->r_info
);
6926 if (elf_bad_symtab (sec
->owner
))
6928 elf_swap_symbol_in (input_bfd
, &locsyms
[r_symndx
], &s
);
6929 if (ELF_ST_BIND (s
.st_info
) == STB_LOCAL
)
6930 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, NULL
, &s
);
6933 h
= sym_hashes
[r_symndx
- extsymoff
];
6934 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, h
, NULL
);
6937 else if (r_symndx
>= nlocsyms
)
6939 h
= sym_hashes
[r_symndx
- extsymoff
];
6940 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, h
, NULL
);
6944 elf_swap_symbol_in (input_bfd
, &locsyms
[r_symndx
], &s
);
6945 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, NULL
, &s
);
6948 if (rsec
&& !rsec
->gc_mark
)
6949 if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
6957 if (!info
->keep_memory
)
6967 /* The sweep phase of garbage collection. Remove all garbage sections. */
6970 elf_gc_sweep (info
, gc_sweep_hook
)
6971 struct bfd_link_info
*info
;
6972 boolean (*gc_sweep_hook
)
6973 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
6974 const Elf_Internal_Rela
*relocs
));
6978 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
6982 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
6985 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6987 /* Keep special sections. Keep .debug sections. */
6988 if ((o
->flags
& SEC_LINKER_CREATED
)
6989 || (o
->flags
& SEC_DEBUGGING
))
6995 /* Skip sweeping sections already excluded. */
6996 if (o
->flags
& SEC_EXCLUDE
)
6999 /* Since this is early in the link process, it is simple
7000 to remove a section from the output. */
7001 o
->flags
|= SEC_EXCLUDE
;
7003 /* But we also have to update some of the relocation
7004 info we collected before. */
7006 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
7008 Elf_Internal_Rela
*internal_relocs
;
7011 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
7012 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
7013 if (internal_relocs
== NULL
)
7016 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
7018 if (!info
->keep_memory
)
7019 free (internal_relocs
);
7027 /* Remove the symbols that were in the swept sections from the dynamic
7028 symbol table. GCFIXME: Anyone know how to get them out of the
7029 static symbol table as well? */
7033 elf_link_hash_traverse (elf_hash_table (info
),
7034 elf_gc_sweep_symbol
,
7037 elf_hash_table (info
)->dynsymcount
= i
;
7043 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7046 elf_gc_sweep_symbol (h
, idxptr
)
7047 struct elf_link_hash_entry
*h
;
7050 int *idx
= (int *) idxptr
;
7052 if (h
->dynindx
!= -1
7053 && ((h
->root
.type
!= bfd_link_hash_defined
7054 && h
->root
.type
!= bfd_link_hash_defweak
)
7055 || h
->root
.u
.def
.section
->gc_mark
))
7056 h
->dynindx
= (*idx
)++;
7061 /* Propogate collected vtable information. This is called through
7062 elf_link_hash_traverse. */
7065 elf_gc_propagate_vtable_entries_used (h
, okp
)
7066 struct elf_link_hash_entry
*h
;
7069 /* Those that are not vtables. */
7070 if (h
->vtable_parent
== NULL
)
7073 /* Those vtables that do not have parents, we cannot merge. */
7074 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
7077 /* If we've already been done, exit. */
7078 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
7081 /* Make sure the parent's table is up to date. */
7082 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
7084 if (h
->vtable_entries_used
== NULL
)
7086 /* None of this table's entries were referenced. Re-use the
7088 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
7089 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
7096 /* Or the parent's entries into ours. */
7097 cu
= h
->vtable_entries_used
;
7099 pu
= h
->vtable_parent
->vtable_entries_used
;
7102 asection
*sec
= h
->root
.u
.def
.section
;
7103 struct elf_backend_data
*bed
= get_elf_backend_data (sec
->owner
);
7104 int file_align
= bed
->s
->file_align
;
7106 n
= h
->vtable_parent
->vtable_entries_size
/ file_align
;
7121 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
7122 struct elf_link_hash_entry
*h
;
7126 bfd_vma hstart
, hend
;
7127 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
7128 struct elf_backend_data
*bed
;
7131 /* Take care of both those symbols that do not describe vtables as
7132 well as those that are not loaded. */
7133 if (h
->vtable_parent
== NULL
)
7136 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
7137 || h
->root
.type
== bfd_link_hash_defweak
);
7139 sec
= h
->root
.u
.def
.section
;
7140 hstart
= h
->root
.u
.def
.value
;
7141 hend
= hstart
+ h
->size
;
7143 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
7144 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, true));
7146 return *(boolean
*) okp
= false;
7147 bed
= get_elf_backend_data (sec
->owner
);
7148 file_align
= bed
->s
->file_align
;
7150 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7152 for (rel
= relstart
; rel
< relend
; ++rel
)
7153 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
7155 /* If the entry is in use, do nothing. */
7156 if (h
->vtable_entries_used
7157 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
7159 bfd_vma entry
= (rel
->r_offset
- hstart
) / file_align
;
7160 if (h
->vtable_entries_used
[entry
])
7163 /* Otherwise, kill it. */
7164 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
7170 /* Do mark and sweep of unused sections. */
7173 elf_gc_sections (abfd
, info
)
7175 struct bfd_link_info
*info
;
7179 asection
* (*gc_mark_hook
)
7180 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7181 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
7183 if (!get_elf_backend_data (abfd
)->can_gc_sections
7184 || info
->relocateable
|| info
->emitrelocations
7185 || elf_hash_table (info
)->dynamic_sections_created
)
7188 /* Apply transitive closure to the vtable entry usage info. */
7189 elf_link_hash_traverse (elf_hash_table (info
),
7190 elf_gc_propagate_vtable_entries_used
,
7195 /* Kill the vtable relocations that were not used. */
7196 elf_link_hash_traverse (elf_hash_table (info
),
7197 elf_gc_smash_unused_vtentry_relocs
,
7202 /* Grovel through relocs to find out who stays ... */
7204 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
7205 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7209 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
7212 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7214 if (o
->flags
& SEC_KEEP
)
7215 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
7220 /* ... and mark SEC_EXCLUDE for those that go. */
7221 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
7227 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
7230 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
7233 struct elf_link_hash_entry
*h
;
7236 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
7237 struct elf_link_hash_entry
**search
, *child
;
7238 bfd_size_type extsymcount
;
7240 /* The sh_info field of the symtab header tells us where the
7241 external symbols start. We don't care about the local symbols at
7243 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
7244 if (!elf_bad_symtab (abfd
))
7245 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
7247 sym_hashes
= elf_sym_hashes (abfd
);
7248 sym_hashes_end
= sym_hashes
+ extsymcount
;
7250 /* Hunt down the child symbol, which is in this section at the same
7251 offset as the relocation. */
7252 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
7254 if ((child
= *search
) != NULL
7255 && (child
->root
.type
== bfd_link_hash_defined
7256 || child
->root
.type
== bfd_link_hash_defweak
)
7257 && child
->root
.u
.def
.section
== sec
7258 && child
->root
.u
.def
.value
== offset
)
7262 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
7263 bfd_archive_filename (abfd
), sec
->name
,
7264 (unsigned long) offset
);
7265 bfd_set_error (bfd_error_invalid_operation
);
7271 /* This *should* only be the absolute section. It could potentially
7272 be that someone has defined a non-global vtable though, which
7273 would be bad. It isn't worth paging in the local symbols to be
7274 sure though; that case should simply be handled by the assembler. */
7276 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
7279 child
->vtable_parent
= h
;
7284 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
7287 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
7288 bfd
*abfd ATTRIBUTE_UNUSED
;
7289 asection
*sec ATTRIBUTE_UNUSED
;
7290 struct elf_link_hash_entry
*h
;
7293 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7294 int file_align
= bed
->s
->file_align
;
7296 if (addend
>= h
->vtable_entries_size
)
7299 boolean
*ptr
= h
->vtable_entries_used
;
7301 /* While the symbol is undefined, we have to be prepared to handle
7303 if (h
->root
.type
== bfd_link_hash_undefined
)
7310 /* Oops! We've got a reference past the defined end of
7311 the table. This is probably a bug -- shall we warn? */
7316 /* Allocate one extra entry for use as a "done" flag for the
7317 consolidation pass. */
7318 bytes
= (size
/ file_align
+ 1) * sizeof (boolean
);
7322 ptr
= bfd_realloc (ptr
- 1, (bfd_size_type
) bytes
);
7328 oldbytes
= ((h
->vtable_entries_size
/ file_align
+ 1)
7329 * sizeof (boolean
));
7330 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
7334 ptr
= bfd_zmalloc ((bfd_size_type
) bytes
);
7339 /* And arrange for that done flag to be at index -1. */
7340 h
->vtable_entries_used
= ptr
+ 1;
7341 h
->vtable_entries_size
= size
;
7344 h
->vtable_entries_used
[addend
/ file_align
] = true;
7349 /* And an accompanying bit to work out final got entry offsets once
7350 we're done. Should be called from final_link. */
7353 elf_gc_common_finalize_got_offsets (abfd
, info
)
7355 struct bfd_link_info
*info
;
7358 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7361 /* The GOT offset is relative to the .got section, but the GOT header is
7362 put into the .got.plt section, if the backend uses it. */
7363 if (bed
->want_got_plt
)
7366 gotoff
= bed
->got_header_size
;
7368 /* Do the local .got entries first. */
7369 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
7371 bfd_signed_vma
*local_got
;
7372 bfd_size_type j
, locsymcount
;
7373 Elf_Internal_Shdr
*symtab_hdr
;
7375 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
7378 local_got
= elf_local_got_refcounts (i
);
7382 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
7383 if (elf_bad_symtab (i
))
7384 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
7386 locsymcount
= symtab_hdr
->sh_info
;
7388 for (j
= 0; j
< locsymcount
; ++j
)
7390 if (local_got
[j
] > 0)
7392 local_got
[j
] = gotoff
;
7393 gotoff
+= ARCH_SIZE
/ 8;
7396 local_got
[j
] = (bfd_vma
) -1;
7400 /* Then the global .got entries. .plt refcounts are handled by
7401 adjust_dynamic_symbol */
7402 elf_link_hash_traverse (elf_hash_table (info
),
7403 elf_gc_allocate_got_offsets
,
7408 /* We need a special top-level link routine to convert got reference counts
7409 to real got offsets. */
7412 elf_gc_allocate_got_offsets (h
, offarg
)
7413 struct elf_link_hash_entry
*h
;
7416 bfd_vma
*off
= (bfd_vma
*) offarg
;
7418 if (h
->got
.refcount
> 0)
7420 h
->got
.offset
= off
[0];
7421 off
[0] += ARCH_SIZE
/ 8;
7424 h
->got
.offset
= (bfd_vma
) -1;
7429 /* Many folk need no more in the way of final link than this, once
7430 got entry reference counting is enabled. */
7433 elf_gc_common_final_link (abfd
, info
)
7435 struct bfd_link_info
*info
;
7437 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
7440 /* Invoke the regular ELF backend linker to do all the work. */
7441 return elf_bfd_final_link (abfd
, info
);
7444 /* This function will be called though elf_link_hash_traverse to store
7445 all hash value of the exported symbols in an array. */
7448 elf_collect_hash_codes (h
, data
)
7449 struct elf_link_hash_entry
*h
;
7452 unsigned long **valuep
= (unsigned long **) data
;
7458 /* Ignore indirect symbols. These are added by the versioning code. */
7459 if (h
->dynindx
== -1)
7462 name
= h
->root
.root
.string
;
7463 p
= strchr (name
, ELF_VER_CHR
);
7466 alc
= bfd_malloc ((bfd_size_type
) (p
- name
+ 1));
7467 memcpy (alc
, name
, (size_t) (p
- name
));
7468 alc
[p
- name
] = '\0';
7472 /* Compute the hash value. */
7473 ha
= bfd_elf_hash (name
);
7475 /* Store the found hash value in the array given as the argument. */
7478 /* And store it in the struct so that we can put it in the hash table
7480 h
->elf_hash_value
= ha
;