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_finalize_dynstr
48 PARAMS ((bfd
*, struct bfd_link_info
*));
49 static boolean elf_fix_symbol_flags
50 PARAMS ((struct elf_link_hash_entry
*, struct elf_info_failed
*));
51 static boolean elf_adjust_dynamic_symbol
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_find_version_dependencies
56 PARAMS ((struct elf_link_hash_entry
*, PTR
));
57 static boolean elf_link_assign_sym_version
58 PARAMS ((struct elf_link_hash_entry
*, PTR
));
59 static boolean elf_collect_hash_codes
60 PARAMS ((struct elf_link_hash_entry
*, PTR
));
61 static boolean elf_link_read_relocs_from_section
62 PARAMS ((bfd
*, Elf_Internal_Shdr
*, PTR
, Elf_Internal_Rela
*));
63 static size_t compute_bucket_count
64 PARAMS ((struct bfd_link_info
*));
65 static void elf_link_output_relocs
66 PARAMS ((bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*));
67 static boolean elf_link_size_reloc_section
68 PARAMS ((bfd
*, Elf_Internal_Shdr
*, asection
*));
69 static void elf_link_adjust_relocs
70 PARAMS ((bfd
*, Elf_Internal_Shdr
*, unsigned int,
71 struct elf_link_hash_entry
**));
72 static int elf_link_sort_cmp1
73 PARAMS ((const void *, const void *));
74 static int elf_link_sort_cmp2
75 PARAMS ((const void *, const void *));
76 static size_t elf_link_sort_relocs
77 PARAMS ((bfd
*, struct bfd_link_info
*, asection
**));
79 /* Given an ELF BFD, add symbols to the global hash table as
83 elf_bfd_link_add_symbols (abfd
, info
)
85 struct bfd_link_info
*info
;
87 switch (bfd_get_format (abfd
))
90 return elf_link_add_object_symbols (abfd
, info
);
92 return elf_link_add_archive_symbols (abfd
, info
);
94 bfd_set_error (bfd_error_wrong_format
);
99 /* Return true iff this is a non-common, definition of a non-function symbol. */
101 is_global_data_symbol_definition (abfd
, sym
)
102 bfd
* abfd ATTRIBUTE_UNUSED
;
103 Elf_Internal_Sym
* sym
;
105 /* Local symbols do not count, but target specific ones might. */
106 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
107 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
110 /* Function symbols do not count. */
111 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
114 /* If the section is undefined, then so is the symbol. */
115 if (sym
->st_shndx
== SHN_UNDEF
)
118 /* If the symbol is defined in the common section, then
119 it is a common definition and so does not count. */
120 if (sym
->st_shndx
== SHN_COMMON
)
123 /* If the symbol is in a target specific section then we
124 must rely upon the backend to tell us what it is. */
125 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
126 /* FIXME - this function is not coded yet:
128 return _bfd_is_global_symbol_definition (abfd, sym);
130 Instead for now assume that the definition is not global,
131 Even if this is wrong, at least the linker will behave
132 in the same way that it used to do. */
138 /* Search the symbol table of the archive element of the archive ABFD
139 whose archive map contains a mention of SYMDEF, and determine if
140 the symbol is defined in this element. */
142 elf_link_is_defined_archive_symbol (abfd
, symdef
)
146 Elf_Internal_Shdr
* hdr
;
147 Elf_External_Sym
* esym
;
148 Elf_External_Sym
* esymend
;
149 Elf_External_Sym
* buf
= NULL
;
150 bfd_size_type symcount
;
151 bfd_size_type extsymcount
;
152 bfd_size_type extsymoff
;
153 boolean result
= false;
157 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
158 if (abfd
== (bfd
*) NULL
)
161 if (! bfd_check_format (abfd
, bfd_object
))
164 /* If we have already included the element containing this symbol in the
165 link then we do not need to include it again. Just claim that any symbol
166 it contains is not a definition, so that our caller will not decide to
167 (re)include this element. */
168 if (abfd
->archive_pass
)
171 /* Select the appropriate symbol table. */
172 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
173 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
175 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
177 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
179 /* The sh_info field of the symtab header tells us where the
180 external symbols start. We don't care about the local symbols. */
181 if (elf_bad_symtab (abfd
))
183 extsymcount
= symcount
;
188 extsymcount
= symcount
- hdr
->sh_info
;
189 extsymoff
= hdr
->sh_info
;
192 amt
= extsymcount
* sizeof (Elf_External_Sym
);
193 buf
= (Elf_External_Sym
*) bfd_malloc (amt
);
194 if (buf
== NULL
&& extsymcount
!= 0)
197 /* Read in the symbol table.
198 FIXME: This ought to be cached somewhere. */
199 pos
= hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
);
200 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
201 || bfd_bread ((PTR
) buf
, amt
, abfd
) != amt
)
207 /* Scan the symbol table looking for SYMDEF. */
208 esymend
= buf
+ extsymcount
;
213 Elf_Internal_Sym sym
;
216 elf_swap_symbol_in (abfd
, esym
, & sym
);
218 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
219 if (name
== (const char *) NULL
)
222 if (strcmp (name
, symdef
->name
) == 0)
224 result
= is_global_data_symbol_definition (abfd
, & sym
);
234 /* Add symbols from an ELF archive file to the linker hash table. We
235 don't use _bfd_generic_link_add_archive_symbols because of a
236 problem which arises on UnixWare. The UnixWare libc.so is an
237 archive which includes an entry libc.so.1 which defines a bunch of
238 symbols. The libc.so archive also includes a number of other
239 object files, which also define symbols, some of which are the same
240 as those defined in libc.so.1. Correct linking requires that we
241 consider each object file in turn, and include it if it defines any
242 symbols we need. _bfd_generic_link_add_archive_symbols does not do
243 this; it looks through the list of undefined symbols, and includes
244 any object file which defines them. When this algorithm is used on
245 UnixWare, it winds up pulling in libc.so.1 early and defining a
246 bunch of symbols. This means that some of the other objects in the
247 archive are not included in the link, which is incorrect since they
248 precede libc.so.1 in the archive.
250 Fortunately, ELF archive handling is simpler than that done by
251 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
252 oddities. In ELF, if we find a symbol in the archive map, and the
253 symbol is currently undefined, we know that we must pull in that
256 Unfortunately, we do have to make multiple passes over the symbol
257 table until nothing further is resolved. */
260 elf_link_add_archive_symbols (abfd
, info
)
262 struct bfd_link_info
*info
;
265 boolean
*defined
= NULL
;
266 boolean
*included
= NULL
;
271 if (! bfd_has_map (abfd
))
273 /* An empty archive is a special case. */
274 if (bfd_openr_next_archived_file (abfd
, (bfd
*) NULL
) == NULL
)
276 bfd_set_error (bfd_error_no_armap
);
280 /* Keep track of all symbols we know to be already defined, and all
281 files we know to be already included. This is to speed up the
282 second and subsequent passes. */
283 c
= bfd_ardata (abfd
)->symdef_count
;
287 amt
*= sizeof (boolean
);
288 defined
= (boolean
*) bfd_malloc (amt
);
289 included
= (boolean
*) bfd_malloc (amt
);
290 if (defined
== (boolean
*) NULL
|| included
== (boolean
*) NULL
)
292 memset (defined
, 0, (size_t) amt
);
293 memset (included
, 0, (size_t) amt
);
295 symdefs
= bfd_ardata (abfd
)->symdefs
;
308 symdefend
= symdef
+ c
;
309 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
311 struct elf_link_hash_entry
*h
;
313 struct bfd_link_hash_entry
*undefs_tail
;
316 if (defined
[i
] || included
[i
])
318 if (symdef
->file_offset
== last
)
324 h
= elf_link_hash_lookup (elf_hash_table (info
), symdef
->name
,
325 false, false, false);
331 /* If this is a default version (the name contains @@),
332 look up the symbol again without the version. The
333 effect is that references to the symbol without the
334 version will be matched by the default symbol in the
337 p
= strchr (symdef
->name
, ELF_VER_CHR
);
338 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
341 copy
= bfd_alloc (abfd
, (bfd_size_type
) (p
- symdef
->name
+ 1));
344 memcpy (copy
, symdef
->name
, (size_t) (p
- symdef
->name
));
345 copy
[p
- symdef
->name
] = '\0';
347 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
348 false, false, false);
350 bfd_release (abfd
, copy
);
356 if (h
->root
.type
== bfd_link_hash_common
)
358 /* We currently have a common symbol. The archive map contains
359 a reference to this symbol, so we may want to include it. We
360 only want to include it however, if this archive element
361 contains a definition of the symbol, not just another common
364 Unfortunately some archivers (including GNU ar) will put
365 declarations of common symbols into their archive maps, as
366 well as real definitions, so we cannot just go by the archive
367 map alone. Instead we must read in the element's symbol
368 table and check that to see what kind of symbol definition
370 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
373 else if (h
->root
.type
!= bfd_link_hash_undefined
)
375 if (h
->root
.type
!= bfd_link_hash_undefweak
)
380 /* We need to include this archive member. */
381 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
382 if (element
== (bfd
*) NULL
)
385 if (! bfd_check_format (element
, bfd_object
))
388 /* Doublecheck that we have not included this object
389 already--it should be impossible, but there may be
390 something wrong with the archive. */
391 if (element
->archive_pass
!= 0)
393 bfd_set_error (bfd_error_bad_value
);
396 element
->archive_pass
= 1;
398 undefs_tail
= info
->hash
->undefs_tail
;
400 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
403 if (! elf_link_add_object_symbols (element
, info
))
406 /* If there are any new undefined symbols, we need to make
407 another pass through the archive in order to see whether
408 they can be defined. FIXME: This isn't perfect, because
409 common symbols wind up on undefs_tail and because an
410 undefined symbol which is defined later on in this pass
411 does not require another pass. This isn't a bug, but it
412 does make the code less efficient than it could be. */
413 if (undefs_tail
!= info
->hash
->undefs_tail
)
416 /* Look backward to mark all symbols from this object file
417 which we have already seen in this pass. */
421 included
[mark
] = true;
426 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
428 /* We mark subsequent symbols from this object file as we go
429 on through the loop. */
430 last
= symdef
->file_offset
;
441 if (defined
!= (boolean
*) NULL
)
443 if (included
!= (boolean
*) NULL
)
448 /* This function is called when we want to define a new symbol. It
449 handles the various cases which arise when we find a definition in
450 a dynamic object, or when there is already a definition in a
451 dynamic object. The new symbol is described by NAME, SYM, PSEC,
452 and PVALUE. We set SYM_HASH to the hash table entry. We set
453 OVERRIDE if the old symbol is overriding a new definition. We set
454 TYPE_CHANGE_OK if it is OK for the type to change. We set
455 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
456 change, we mean that we shouldn't warn if the type or size does
457 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
461 elf_merge_symbol (abfd
, info
, name
, sym
, psec
, pvalue
, sym_hash
,
462 override
, type_change_ok
, size_change_ok
, dt_needed
)
464 struct bfd_link_info
*info
;
466 Elf_Internal_Sym
*sym
;
469 struct elf_link_hash_entry
**sym_hash
;
471 boolean
*type_change_ok
;
472 boolean
*size_change_ok
;
476 struct elf_link_hash_entry
*h
;
479 boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
484 bind
= ELF_ST_BIND (sym
->st_info
);
486 if (! bfd_is_und_section (sec
))
487 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, false, false);
489 h
= ((struct elf_link_hash_entry
*)
490 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true, false, false));
495 /* This code is for coping with dynamic objects, and is only useful
496 if we are doing an ELF link. */
497 if (info
->hash
->creator
!= abfd
->xvec
)
500 /* For merging, we only care about real symbols. */
502 while (h
->root
.type
== bfd_link_hash_indirect
503 || h
->root
.type
== bfd_link_hash_warning
)
504 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
506 /* If we just created the symbol, mark it as being an ELF symbol.
507 Other than that, there is nothing to do--there is no merge issue
508 with a newly defined symbol--so we just return. */
510 if (h
->root
.type
== bfd_link_hash_new
)
512 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
516 /* OLDBFD is a BFD associated with the existing symbol. */
518 switch (h
->root
.type
)
524 case bfd_link_hash_undefined
:
525 case bfd_link_hash_undefweak
:
526 oldbfd
= h
->root
.u
.undef
.abfd
;
529 case bfd_link_hash_defined
:
530 case bfd_link_hash_defweak
:
531 oldbfd
= h
->root
.u
.def
.section
->owner
;
534 case bfd_link_hash_common
:
535 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
539 /* In cases involving weak versioned symbols, we may wind up trying
540 to merge a symbol with itself. Catch that here, to avoid the
541 confusion that results if we try to override a symbol with
542 itself. The additional tests catch cases like
543 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
544 dynamic object, which we do want to handle here. */
546 && ((abfd
->flags
& DYNAMIC
) == 0
547 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0))
550 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
551 respectively, is from a dynamic object. */
553 if ((abfd
->flags
& DYNAMIC
) != 0)
559 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
564 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
565 indices used by MIPS ELF. */
566 switch (h
->root
.type
)
572 case bfd_link_hash_defined
:
573 case bfd_link_hash_defweak
:
574 hsec
= h
->root
.u
.def
.section
;
577 case bfd_link_hash_common
:
578 hsec
= h
->root
.u
.c
.p
->section
;
585 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
588 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
589 respectively, appear to be a definition rather than reference. */
591 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
596 if (h
->root
.type
== bfd_link_hash_undefined
597 || h
->root
.type
== bfd_link_hash_undefweak
598 || h
->root
.type
== bfd_link_hash_common
)
603 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
604 symbol, respectively, appears to be a common symbol in a dynamic
605 object. If a symbol appears in an uninitialized section, and is
606 not weak, and is not a function, then it may be a common symbol
607 which was resolved when the dynamic object was created. We want
608 to treat such symbols specially, because they raise special
609 considerations when setting the symbol size: if the symbol
610 appears as a common symbol in a regular object, and the size in
611 the regular object is larger, we must make sure that we use the
612 larger size. This problematic case can always be avoided in C,
613 but it must be handled correctly when using Fortran shared
616 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
617 likewise for OLDDYNCOMMON and OLDDEF.
619 Note that this test is just a heuristic, and that it is quite
620 possible to have an uninitialized symbol in a shared object which
621 is really a definition, rather than a common symbol. This could
622 lead to some minor confusion when the symbol really is a common
623 symbol in some regular object. However, I think it will be
628 && (sec
->flags
& SEC_ALLOC
) != 0
629 && (sec
->flags
& SEC_LOAD
) == 0
632 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
635 newdyncommon
= false;
639 && h
->root
.type
== bfd_link_hash_defined
640 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
641 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
642 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
644 && h
->type
!= STT_FUNC
)
647 olddyncommon
= false;
649 /* It's OK to change the type if either the existing symbol or the
650 new symbol is weak unless it comes from a DT_NEEDED entry of
651 a shared object, in which case, the DT_NEEDED entry may not be
652 required at the run time. */
654 if ((! dt_needed
&& h
->root
.type
== bfd_link_hash_defweak
)
655 || h
->root
.type
== bfd_link_hash_undefweak
657 *type_change_ok
= true;
659 /* It's OK to change the size if either the existing symbol or the
660 new symbol is weak, or if the old symbol is undefined. */
663 || h
->root
.type
== bfd_link_hash_undefined
)
664 *size_change_ok
= true;
666 /* If both the old and the new symbols look like common symbols in a
667 dynamic object, set the size of the symbol to the larger of the
672 && sym
->st_size
!= h
->size
)
674 /* Since we think we have two common symbols, issue a multiple
675 common warning if desired. Note that we only warn if the
676 size is different. If the size is the same, we simply let
677 the old symbol override the new one as normally happens with
678 symbols defined in dynamic objects. */
680 if (! ((*info
->callbacks
->multiple_common
)
681 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
682 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
685 if (sym
->st_size
> h
->size
)
686 h
->size
= sym
->st_size
;
688 *size_change_ok
= true;
691 /* If we are looking at a dynamic object, and we have found a
692 definition, we need to see if the symbol was already defined by
693 some other object. If so, we want to use the existing
694 definition, and we do not want to report a multiple symbol
695 definition error; we do this by clobbering *PSEC to be
698 We treat a common symbol as a definition if the symbol in the
699 shared library is a function, since common symbols always
700 represent variables; this can cause confusion in principle, but
701 any such confusion would seem to indicate an erroneous program or
702 shared library. We also permit a common symbol in a regular
703 object to override a weak symbol in a shared object.
705 We prefer a non-weak definition in a shared library to a weak
706 definition in the executable unless it comes from a DT_NEEDED
707 entry of a shared object, in which case, the DT_NEEDED entry
708 may not be required at the run time. */
713 || (h
->root
.type
== bfd_link_hash_common
715 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)))
716 && (h
->root
.type
!= bfd_link_hash_defweak
718 || bind
== STB_WEAK
))
722 newdyncommon
= false;
724 *psec
= sec
= bfd_und_section_ptr
;
725 *size_change_ok
= true;
727 /* If we get here when the old symbol is a common symbol, then
728 we are explicitly letting it override a weak symbol or
729 function in a dynamic object, and we don't want to warn about
730 a type change. If the old symbol is a defined symbol, a type
731 change warning may still be appropriate. */
733 if (h
->root
.type
== bfd_link_hash_common
)
734 *type_change_ok
= true;
737 /* Handle the special case of an old common symbol merging with a
738 new symbol which looks like a common symbol in a shared object.
739 We change *PSEC and *PVALUE to make the new symbol look like a
740 common symbol, and let _bfd_generic_link_add_one_symbol will do
744 && h
->root
.type
== bfd_link_hash_common
)
748 newdyncommon
= false;
749 *pvalue
= sym
->st_size
;
750 *psec
= sec
= bfd_com_section_ptr
;
751 *size_change_ok
= true;
754 /* If the old symbol is from a dynamic object, and the new symbol is
755 a definition which is not from a dynamic object, then the new
756 symbol overrides the old symbol. Symbols from regular files
757 always take precedence over symbols from dynamic objects, even if
758 they are defined after the dynamic object in the link.
760 As above, we again permit a common symbol in a regular object to
761 override a definition in a shared object if the shared object
762 symbol is a function or is weak.
764 As above, we permit a non-weak definition in a shared object to
765 override a weak definition in a regular object. */
769 || (bfd_is_com_section (sec
)
770 && (h
->root
.type
== bfd_link_hash_defweak
771 || h
->type
== STT_FUNC
)))
774 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
776 || h
->root
.type
== bfd_link_hash_defweak
))
778 /* Change the hash table entry to undefined, and let
779 _bfd_generic_link_add_one_symbol do the right thing with the
782 h
->root
.type
= bfd_link_hash_undefined
;
783 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
784 *size_change_ok
= true;
787 olddyncommon
= false;
789 /* We again permit a type change when a common symbol may be
790 overriding a function. */
792 if (bfd_is_com_section (sec
))
793 *type_change_ok
= true;
795 /* This union may have been set to be non-NULL when this symbol
796 was seen in a dynamic object. We must force the union to be
797 NULL, so that it is correct for a regular symbol. */
799 h
->verinfo
.vertree
= NULL
;
801 /* In this special case, if H is the target of an indirection,
802 we want the caller to frob with H rather than with the
803 indirect symbol. That will permit the caller to redefine the
804 target of the indirection, rather than the indirect symbol
805 itself. FIXME: This will break the -y option if we store a
806 symbol with a different name. */
810 /* Handle the special case of a new common symbol merging with an
811 old symbol that looks like it might be a common symbol defined in
812 a shared object. Note that we have already handled the case in
813 which a new common symbol should simply override the definition
814 in the shared library. */
817 && bfd_is_com_section (sec
)
820 /* It would be best if we could set the hash table entry to a
821 common symbol, but we don't know what to use for the section
823 if (! ((*info
->callbacks
->multiple_common
)
824 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
825 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
828 /* If the predumed common symbol in the dynamic object is
829 larger, pretend that the new symbol has its size. */
831 if (h
->size
> *pvalue
)
834 /* FIXME: We no longer know the alignment required by the symbol
835 in the dynamic object, so we just wind up using the one from
836 the regular object. */
839 olddyncommon
= false;
841 h
->root
.type
= bfd_link_hash_undefined
;
842 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
844 *size_change_ok
= true;
845 *type_change_ok
= true;
847 h
->verinfo
.vertree
= NULL
;
850 /* Handle the special case of a weak definition in a regular object
851 followed by a non-weak definition in a shared object. In this
852 case, we prefer the definition in the shared object unless it
853 comes from a DT_NEEDED entry of a shared object, in which case,
854 the DT_NEEDED entry may not be required at the run time. */
857 && h
->root
.type
== bfd_link_hash_defweak
862 /* To make this work we have to frob the flags so that the rest
863 of the code does not think we are using the regular
865 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
866 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
867 else if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
868 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
869 h
->elf_link_hash_flags
&= ~ (ELF_LINK_HASH_DEF_REGULAR
870 | ELF_LINK_HASH_DEF_DYNAMIC
);
872 /* If H is the target of an indirection, we want the caller to
873 use H rather than the indirect symbol. Otherwise if we are
874 defining a new indirect symbol we will wind up attaching it
875 to the entry we are overriding. */
879 /* Handle the special case of a non-weak definition in a shared
880 object followed by a weak definition in a regular object. In
881 this case we prefer to definition in the shared object. To make
882 this work we have to tell the caller to not treat the new symbol
886 && h
->root
.type
!= bfd_link_hash_defweak
895 /* Add symbols from an ELF object file to the linker hash table. */
898 elf_link_add_object_symbols (abfd
, info
)
900 struct bfd_link_info
*info
;
902 boolean (*add_symbol_hook
) PARAMS ((bfd
*, struct bfd_link_info
*,
903 const Elf_Internal_Sym
*,
904 const char **, flagword
*,
905 asection
**, bfd_vma
*));
906 boolean (*check_relocs
) PARAMS ((bfd
*, struct bfd_link_info
*,
907 asection
*, const Elf_Internal_Rela
*));
909 Elf_Internal_Shdr
*hdr
;
910 bfd_size_type symcount
;
911 bfd_size_type extsymcount
;
912 bfd_size_type extsymoff
;
913 Elf_External_Sym
*buf
= NULL
;
914 struct elf_link_hash_entry
**sym_hash
;
916 Elf_External_Versym
*extversym
= NULL
;
917 Elf_External_Versym
*ever
;
918 Elf_External_Dyn
*dynbuf
= NULL
;
919 struct elf_link_hash_entry
*weaks
;
920 Elf_External_Sym
*esym
;
921 Elf_External_Sym
*esymend
;
922 struct elf_backend_data
*bed
;
924 struct elf_link_hash_table
* hash_table
;
928 hash_table
= elf_hash_table (info
);
930 bed
= get_elf_backend_data (abfd
);
931 add_symbol_hook
= bed
->elf_add_symbol_hook
;
932 collect
= bed
->collect
;
934 if ((abfd
->flags
& DYNAMIC
) == 0)
940 /* You can't use -r against a dynamic object. Also, there's no
941 hope of using a dynamic object which does not exactly match
942 the format of the output file. */
943 if (info
->relocateable
|| info
->hash
->creator
!= abfd
->xvec
)
945 bfd_set_error (bfd_error_invalid_operation
);
950 /* As a GNU extension, any input sections which are named
951 .gnu.warning.SYMBOL are treated as warning symbols for the given
952 symbol. This differs from .gnu.warning sections, which generate
953 warnings when they are included in an output file. */
958 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
962 name
= bfd_get_section_name (abfd
, s
);
963 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
968 name
+= sizeof ".gnu.warning." - 1;
970 /* If this is a shared object, then look up the symbol
971 in the hash table. If it is there, and it is already
972 been defined, then we will not be using the entry
973 from this shared object, so we don't need to warn.
974 FIXME: If we see the definition in a regular object
975 later on, we will warn, but we shouldn't. The only
976 fix is to keep track of what warnings we are supposed
977 to emit, and then handle them all at the end of the
979 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
981 struct elf_link_hash_entry
*h
;
983 h
= elf_link_hash_lookup (hash_table
, name
,
986 /* FIXME: What about bfd_link_hash_common? */
988 && (h
->root
.type
== bfd_link_hash_defined
989 || h
->root
.type
== bfd_link_hash_defweak
))
991 /* We don't want to issue this warning. Clobber
992 the section size so that the warning does not
993 get copied into the output file. */
999 sz
= bfd_section_size (abfd
, s
);
1000 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
1004 if (! bfd_get_section_contents (abfd
, s
, msg
, (file_ptr
) 0, sz
))
1009 if (! (_bfd_generic_link_add_one_symbol
1010 (info
, abfd
, name
, BSF_WARNING
, s
, (bfd_vma
) 0, msg
,
1011 false, collect
, (struct bfd_link_hash_entry
**) NULL
)))
1014 if (! info
->relocateable
)
1016 /* Clobber the section size so that the warning does
1017 not get copied into the output file. */
1024 /* If this is a dynamic object, we always link against the .dynsym
1025 symbol table, not the .symtab symbol table. The dynamic linker
1026 will only see the .dynsym symbol table, so there is no reason to
1027 look at .symtab for a dynamic object. */
1029 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
1030 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1032 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1036 /* Read in any version definitions. */
1038 if (! _bfd_elf_slurp_version_tables (abfd
))
1041 /* Read in the symbol versions, but don't bother to convert them
1042 to internal format. */
1043 if (elf_dynversym (abfd
) != 0)
1045 Elf_Internal_Shdr
*versymhdr
;
1047 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
1048 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
1049 if (extversym
== NULL
)
1051 amt
= versymhdr
->sh_size
;
1052 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
1053 || bfd_bread ((PTR
) extversym
, amt
, abfd
) != amt
)
1058 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
1060 /* The sh_info field of the symtab header tells us where the
1061 external symbols start. We don't care about the local symbols at
1063 if (elf_bad_symtab (abfd
))
1065 extsymcount
= symcount
;
1070 extsymcount
= symcount
- hdr
->sh_info
;
1071 extsymoff
= hdr
->sh_info
;
1074 amt
= extsymcount
* sizeof (Elf_External_Sym
);
1075 buf
= (Elf_External_Sym
*) bfd_malloc (amt
);
1076 if (buf
== NULL
&& extsymcount
!= 0)
1079 /* We store a pointer to the hash table entry for each external
1081 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
1082 sym_hash
= (struct elf_link_hash_entry
**) bfd_alloc (abfd
, amt
);
1083 if (sym_hash
== NULL
)
1085 elf_sym_hashes (abfd
) = sym_hash
;
1091 /* If we are creating a shared library, create all the dynamic
1092 sections immediately. We need to attach them to something,
1093 so we attach them to this BFD, provided it is the right
1094 format. FIXME: If there are no input BFD's of the same
1095 format as the output, we can't make a shared library. */
1097 && is_elf_hash_table (info
)
1098 && ! hash_table
->dynamic_sections_created
1099 && abfd
->xvec
== info
->hash
->creator
)
1101 if (! elf_link_create_dynamic_sections (abfd
, info
))
1105 else if (! is_elf_hash_table (info
))
1112 bfd_size_type oldsize
;
1113 bfd_size_type strindex
;
1115 /* Find the name to use in a DT_NEEDED entry that refers to this
1116 object. If the object has a DT_SONAME entry, we use it.
1117 Otherwise, if the generic linker stuck something in
1118 elf_dt_name, we use that. Otherwise, we just use the file
1119 name. If the generic linker put a null string into
1120 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1121 there is a DT_SONAME entry. */
1123 name
= bfd_get_filename (abfd
);
1124 if (elf_dt_name (abfd
) != NULL
)
1126 name
= elf_dt_name (abfd
);
1129 if (elf_dt_soname (abfd
) != NULL
)
1135 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1138 Elf_External_Dyn
*extdyn
;
1139 Elf_External_Dyn
*extdynend
;
1141 unsigned long shlink
;
1145 dynbuf
= (Elf_External_Dyn
*) bfd_malloc (s
->_raw_size
);
1149 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
1150 (file_ptr
) 0, s
->_raw_size
))
1153 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1156 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1159 /* The shared libraries distributed with hpux11 have a bogus
1160 sh_link field for the ".dynamic" section. This code detects
1161 when SHLINK refers to a section that is not a string table
1162 and tries to find the string table for the ".dynsym" section
1164 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[shlink
];
1165 if (shdr
->sh_type
!= SHT_STRTAB
)
1167 asection
*ds
= bfd_get_section_by_name (abfd
, ".dynsym");
1168 int elfdsec
= _bfd_elf_section_from_bfd_section (abfd
, ds
);
1171 shlink
= elf_elfsections (abfd
)[elfdsec
]->sh_link
;
1176 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
1179 for (; extdyn
< extdynend
; extdyn
++)
1181 Elf_Internal_Dyn dyn
;
1183 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
1184 if (dyn
.d_tag
== DT_SONAME
)
1186 unsigned int tagv
= dyn
.d_un
.d_val
;
1187 name
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1191 if (dyn
.d_tag
== DT_NEEDED
)
1193 struct bfd_link_needed_list
*n
, **pn
;
1195 unsigned int tagv
= dyn
.d_un
.d_val
;
1197 amt
= sizeof (struct bfd_link_needed_list
);
1198 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1199 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1200 if (n
== NULL
|| fnm
== NULL
)
1202 anm
= bfd_alloc (abfd
, (bfd_size_type
) strlen (fnm
) + 1);
1209 for (pn
= & hash_table
->needed
;
1215 if (dyn
.d_tag
== DT_RUNPATH
)
1217 struct bfd_link_needed_list
*n
, **pn
;
1219 unsigned int tagv
= dyn
.d_un
.d_val
;
1221 /* When we see DT_RPATH before DT_RUNPATH, we have
1222 to clear runpath. Do _NOT_ bfd_release, as that
1223 frees all more recently bfd_alloc'd blocks as
1225 if (rpath
&& hash_table
->runpath
)
1226 hash_table
->runpath
= NULL
;
1228 amt
= sizeof (struct bfd_link_needed_list
);
1229 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1230 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1231 if (n
== NULL
|| fnm
== NULL
)
1233 anm
= bfd_alloc (abfd
, (bfd_size_type
) strlen (fnm
) + 1);
1240 for (pn
= & hash_table
->runpath
;
1248 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
1249 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
1251 struct bfd_link_needed_list
*n
, **pn
;
1253 unsigned int tagv
= dyn
.d_un
.d_val
;
1255 amt
= sizeof (struct bfd_link_needed_list
);
1256 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1257 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1258 if (n
== NULL
|| fnm
== NULL
)
1260 anm
= bfd_alloc (abfd
, (bfd_size_type
) strlen (fnm
) + 1);
1267 for (pn
= & hash_table
->runpath
;
1280 /* We do not want to include any of the sections in a dynamic
1281 object in the output file. We hack by simply clobbering the
1282 list of sections in the BFD. This could be handled more
1283 cleanly by, say, a new section flag; the existing
1284 SEC_NEVER_LOAD flag is not the one we want, because that one
1285 still implies that the section takes up space in the output
1287 abfd
->sections
= NULL
;
1288 abfd
->section_count
= 0;
1290 /* If this is the first dynamic object found in the link, create
1291 the special sections required for dynamic linking. */
1292 if (! hash_table
->dynamic_sections_created
)
1293 if (! elf_link_create_dynamic_sections (abfd
, info
))
1298 /* Add a DT_NEEDED entry for this dynamic object. */
1299 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
1300 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, name
, false);
1301 if (strindex
== (bfd_size_type
) -1)
1304 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
1307 Elf_External_Dyn
*dyncon
, *dynconend
;
1309 /* The hash table size did not change, which means that
1310 the dynamic object name was already entered. If we
1311 have already included this dynamic object in the
1312 link, just ignore it. There is no reason to include
1313 a particular dynamic object more than once. */
1314 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
1315 BFD_ASSERT (sdyn
!= NULL
);
1317 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1318 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1320 for (; dyncon
< dynconend
; dyncon
++)
1322 Elf_Internal_Dyn dyn
;
1324 elf_swap_dyn_in (hash_table
->dynobj
, dyncon
, & dyn
);
1325 if (dyn
.d_tag
== DT_NEEDED
1326 && dyn
.d_un
.d_val
== strindex
)
1330 if (extversym
!= NULL
)
1332 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
1338 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
1342 /* Save the SONAME, if there is one, because sometimes the
1343 linker emulation code will need to know it. */
1345 name
= basename (bfd_get_filename (abfd
));
1346 elf_dt_name (abfd
) = name
;
1349 pos
= hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
);
1350 amt
= extsymcount
* sizeof (Elf_External_Sym
);
1351 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1352 || bfd_bread ((PTR
) buf
, amt
, abfd
) != amt
)
1357 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
1358 esymend
= buf
+ extsymcount
;
1361 esym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
1363 Elf_Internal_Sym sym
;
1369 struct elf_link_hash_entry
*h
;
1371 boolean size_change_ok
, type_change_ok
;
1372 boolean new_weakdef
;
1373 unsigned int old_alignment
;
1375 elf_swap_symbol_in (abfd
, esym
, &sym
);
1377 flags
= BSF_NO_FLAGS
;
1379 value
= sym
.st_value
;
1382 bind
= ELF_ST_BIND (sym
.st_info
);
1383 if (bind
== STB_LOCAL
)
1385 /* This should be impossible, since ELF requires that all
1386 global symbols follow all local symbols, and that sh_info
1387 point to the first global symbol. Unfortunatealy, Irix 5
1391 else if (bind
== STB_GLOBAL
)
1393 if (sym
.st_shndx
!= SHN_UNDEF
1394 && sym
.st_shndx
!= SHN_COMMON
)
1397 else if (bind
== STB_WEAK
)
1401 /* Leave it up to the processor backend. */
1404 if (sym
.st_shndx
== SHN_UNDEF
)
1405 sec
= bfd_und_section_ptr
;
1406 else if (sym
.st_shndx
> 0 && sym
.st_shndx
< SHN_LORESERVE
)
1408 sec
= section_from_elf_index (abfd
, sym
.st_shndx
);
1410 sec
= bfd_abs_section_ptr
;
1411 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
1414 else if (sym
.st_shndx
== SHN_ABS
)
1415 sec
= bfd_abs_section_ptr
;
1416 else if (sym
.st_shndx
== SHN_COMMON
)
1418 sec
= bfd_com_section_ptr
;
1419 /* What ELF calls the size we call the value. What ELF
1420 calls the value we call the alignment. */
1421 value
= sym
.st_size
;
1425 /* Leave it up to the processor backend. */
1428 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
1429 if (name
== (const char *) NULL
)
1432 if (add_symbol_hook
)
1434 if (! (*add_symbol_hook
) (abfd
, info
, &sym
, &name
, &flags
, &sec
,
1438 /* The hook function sets the name to NULL if this symbol
1439 should be skipped for some reason. */
1440 if (name
== (const char *) NULL
)
1444 /* Sanity check that all possibilities were handled. */
1445 if (sec
== (asection
*) NULL
)
1447 bfd_set_error (bfd_error_bad_value
);
1451 if (bfd_is_und_section (sec
)
1452 || bfd_is_com_section (sec
))
1457 size_change_ok
= false;
1458 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
1460 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1462 Elf_Internal_Versym iver
;
1463 unsigned int vernum
= 0;
1468 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
1469 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
1471 /* If this is a hidden symbol, or if it is not version
1472 1, we append the version name to the symbol name.
1473 However, we do not modify a non-hidden absolute
1474 symbol, because it might be the version symbol
1475 itself. FIXME: What if it isn't? */
1476 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
1477 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
1480 unsigned int namelen
;
1481 bfd_size_type newlen
;
1484 if (sym
.st_shndx
!= SHN_UNDEF
)
1486 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
1488 (*_bfd_error_handler
)
1489 (_("%s: %s: invalid version %u (max %d)"),
1490 bfd_archive_filename (abfd
), name
, vernum
,
1491 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1492 bfd_set_error (bfd_error_bad_value
);
1495 else if (vernum
> 1)
1497 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1503 /* We cannot simply test for the number of
1504 entries in the VERNEED section since the
1505 numbers for the needed versions do not start
1507 Elf_Internal_Verneed
*t
;
1510 for (t
= elf_tdata (abfd
)->verref
;
1514 Elf_Internal_Vernaux
*a
;
1516 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1518 if (a
->vna_other
== vernum
)
1520 verstr
= a
->vna_nodename
;
1529 (*_bfd_error_handler
)
1530 (_("%s: %s: invalid needed version %d"),
1531 bfd_archive_filename (abfd
), name
, vernum
);
1532 bfd_set_error (bfd_error_bad_value
);
1537 namelen
= strlen (name
);
1538 newlen
= namelen
+ strlen (verstr
) + 2;
1539 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
1542 newname
= (char *) bfd_alloc (abfd
, newlen
);
1543 if (newname
== NULL
)
1545 strcpy (newname
, name
);
1546 p
= newname
+ namelen
;
1548 /* If this is a defined non-hidden version symbol,
1549 we add another @ to the name. This indicates the
1550 default version of the symbol. */
1551 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1552 && sym
.st_shndx
!= SHN_UNDEF
)
1560 if (! elf_merge_symbol (abfd
, info
, name
, &sym
, &sec
, &value
,
1561 sym_hash
, &override
, &type_change_ok
,
1562 &size_change_ok
, dt_needed
))
1569 while (h
->root
.type
== bfd_link_hash_indirect
1570 || h
->root
.type
== bfd_link_hash_warning
)
1571 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1573 /* Remember the old alignment if this is a common symbol, so
1574 that we don't reduce the alignment later on. We can't
1575 check later, because _bfd_generic_link_add_one_symbol
1576 will set a default for the alignment which we want to
1578 if (h
->root
.type
== bfd_link_hash_common
)
1579 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1581 if (elf_tdata (abfd
)->verdef
!= NULL
1585 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1588 if (! (_bfd_generic_link_add_one_symbol
1589 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
1590 false, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
1594 while (h
->root
.type
== bfd_link_hash_indirect
1595 || h
->root
.type
== bfd_link_hash_warning
)
1596 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1599 new_weakdef
= false;
1602 && (flags
& BSF_WEAK
) != 0
1603 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
1604 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1605 && h
->weakdef
== NULL
)
1607 /* Keep a list of all weak defined non function symbols from
1608 a dynamic object, using the weakdef field. Later in this
1609 function we will set the weakdef field to the correct
1610 value. We only put non-function symbols from dynamic
1611 objects on this list, because that happens to be the only
1612 time we need to know the normal symbol corresponding to a
1613 weak symbol, and the information is time consuming to
1614 figure out. If the weakdef field is not already NULL,
1615 then this symbol was already defined by some previous
1616 dynamic object, and we will be using that previous
1617 definition anyhow. */
1624 /* Set the alignment of a common symbol. */
1625 if (sym
.st_shndx
== SHN_COMMON
1626 && h
->root
.type
== bfd_link_hash_common
)
1630 align
= bfd_log2 (sym
.st_value
);
1631 if (align
> old_alignment
1632 /* Permit an alignment power of zero if an alignment of one
1633 is specified and no other alignments have been specified. */
1634 || (sym
.st_value
== 1 && old_alignment
== 0))
1635 h
->root
.u
.c
.p
->alignment_power
= align
;
1638 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1644 /* Remember the symbol size and type. */
1645 if (sym
.st_size
!= 0
1646 && (definition
|| h
->size
== 0))
1648 if (h
->size
!= 0 && h
->size
!= sym
.st_size
&& ! size_change_ok
)
1649 (*_bfd_error_handler
)
1650 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1651 name
, (unsigned long) h
->size
, (unsigned long) sym
.st_size
,
1652 bfd_archive_filename (abfd
));
1654 h
->size
= sym
.st_size
;
1657 /* If this is a common symbol, then we always want H->SIZE
1658 to be the size of the common symbol. The code just above
1659 won't fix the size if a common symbol becomes larger. We
1660 don't warn about a size change here, because that is
1661 covered by --warn-common. */
1662 if (h
->root
.type
== bfd_link_hash_common
)
1663 h
->size
= h
->root
.u
.c
.size
;
1665 if (ELF_ST_TYPE (sym
.st_info
) != STT_NOTYPE
1666 && (definition
|| h
->type
== STT_NOTYPE
))
1668 if (h
->type
!= STT_NOTYPE
1669 && h
->type
!= ELF_ST_TYPE (sym
.st_info
)
1670 && ! type_change_ok
)
1671 (*_bfd_error_handler
)
1672 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1673 name
, h
->type
, ELF_ST_TYPE (sym
.st_info
),
1674 bfd_archive_filename (abfd
));
1676 h
->type
= ELF_ST_TYPE (sym
.st_info
);
1679 /* If st_other has a processor-specific meaning, specific code
1680 might be needed here. */
1681 if (sym
.st_other
!= 0)
1683 /* Combine visibilities, using the most constraining one. */
1684 unsigned char hvis
= ELF_ST_VISIBILITY (h
->other
);
1685 unsigned char symvis
= ELF_ST_VISIBILITY (sym
.st_other
);
1687 if (symvis
&& (hvis
> symvis
|| hvis
== 0))
1688 h
->other
= sym
.st_other
;
1690 /* If neither has visibility, use the st_other of the
1691 definition. This is an arbitrary choice, since the
1692 other bits have no general meaning. */
1693 if (!symvis
&& !hvis
1694 && (definition
|| h
->other
== 0))
1695 h
->other
= sym
.st_other
;
1698 /* Set a flag in the hash table entry indicating the type of
1699 reference or definition we just found. Keep a count of
1700 the number of dynamic symbols we find. A dynamic symbol
1701 is one which is referenced or defined by both a regular
1702 object and a shared object. */
1703 old_flags
= h
->elf_link_hash_flags
;
1709 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
1710 if (bind
!= STB_WEAK
)
1711 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
1714 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
1716 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1717 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
1723 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
1725 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
1726 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
1727 | ELF_LINK_HASH_REF_REGULAR
)) != 0
1728 || (h
->weakdef
!= NULL
1730 && h
->weakdef
->dynindx
!= -1))
1734 h
->elf_link_hash_flags
|= new_flag
;
1736 /* If this symbol has a version, and it is the default
1737 version, we create an indirect symbol from the default
1738 name to the fully decorated name. This will cause
1739 external references which do not specify a version to be
1740 bound to this version of the symbol. */
1741 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
1745 p
= strchr (name
, ELF_VER_CHR
);
1746 if (p
!= NULL
&& p
[1] == ELF_VER_CHR
)
1749 struct elf_link_hash_entry
*hi
;
1752 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1753 (size_t) (p
- name
+ 1));
1754 if (shortname
== NULL
)
1756 strncpy (shortname
, name
, (size_t) (p
- name
));
1757 shortname
[p
- name
] = '\0';
1759 /* We are going to create a new symbol. Merge it
1760 with any existing symbol with this name. For the
1761 purposes of the merge, act as though we were
1762 defining the symbol we just defined, although we
1763 actually going to define an indirect symbol. */
1764 type_change_ok
= false;
1765 size_change_ok
= false;
1766 if (! elf_merge_symbol (abfd
, info
, shortname
, &sym
, &sec
,
1767 &value
, &hi
, &override
,
1769 &size_change_ok
, dt_needed
))
1774 if (! (_bfd_generic_link_add_one_symbol
1775 (info
, abfd
, shortname
, BSF_INDIRECT
,
1776 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1777 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1782 /* In this case the symbol named SHORTNAME is
1783 overriding the indirect symbol we want to
1784 add. We were planning on making SHORTNAME an
1785 indirect symbol referring to NAME. SHORTNAME
1786 is the name without a version. NAME is the
1787 fully versioned name, and it is the default
1790 Overriding means that we already saw a
1791 definition for the symbol SHORTNAME in a
1792 regular object, and it is overriding the
1793 symbol defined in the dynamic object.
1795 When this happens, we actually want to change
1796 NAME, the symbol we just added, to refer to
1797 SHORTNAME. This will cause references to
1798 NAME in the shared object to become
1799 references to SHORTNAME in the regular
1800 object. This is what we expect when we
1801 override a function in a shared object: that
1802 the references in the shared object will be
1803 mapped to the definition in the regular
1806 while (hi
->root
.type
== bfd_link_hash_indirect
1807 || hi
->root
.type
== bfd_link_hash_warning
)
1808 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1810 h
->root
.type
= bfd_link_hash_indirect
;
1811 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1812 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1814 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1815 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1816 if (hi
->elf_link_hash_flags
1817 & (ELF_LINK_HASH_REF_REGULAR
1818 | ELF_LINK_HASH_DEF_REGULAR
))
1820 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1826 /* Now set HI to H, so that the following code
1827 will set the other fields correctly. */
1831 /* If there is a duplicate definition somewhere,
1832 then HI may not point to an indirect symbol. We
1833 will have reported an error to the user in that
1836 if (hi
->root
.type
== bfd_link_hash_indirect
)
1838 struct elf_link_hash_entry
*ht
;
1840 /* If the symbol became indirect, then we assume
1841 that we have not seen a definition before. */
1842 BFD_ASSERT ((hi
->elf_link_hash_flags
1843 & (ELF_LINK_HASH_DEF_DYNAMIC
1844 | ELF_LINK_HASH_DEF_REGULAR
))
1847 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1848 (*bed
->elf_backend_copy_indirect_symbol
) (ht
, hi
);
1850 /* See if the new flags lead us to realize that
1851 the symbol must be dynamic. */
1857 || ((hi
->elf_link_hash_flags
1858 & ELF_LINK_HASH_REF_DYNAMIC
)
1864 if ((hi
->elf_link_hash_flags
1865 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1871 /* We also need to define an indirection from the
1872 nondefault version of the symbol. */
1874 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1876 if (shortname
== NULL
)
1878 strncpy (shortname
, name
, (size_t) (p
- name
));
1879 strcpy (shortname
+ (p
- name
), p
+ 1);
1881 /* Once again, merge with any existing symbol. */
1882 type_change_ok
= false;
1883 size_change_ok
= false;
1884 if (! elf_merge_symbol (abfd
, info
, shortname
, &sym
, &sec
,
1885 &value
, &hi
, &override
,
1887 &size_change_ok
, dt_needed
))
1892 /* Here SHORTNAME is a versioned name, so we
1893 don't expect to see the type of override we
1894 do in the case above. */
1895 (*_bfd_error_handler
)
1896 (_("%s: warning: unexpected redefinition of `%s'"),
1897 bfd_archive_filename (abfd
), shortname
);
1901 if (! (_bfd_generic_link_add_one_symbol
1902 (info
, abfd
, shortname
, BSF_INDIRECT
,
1903 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1904 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1907 /* If there is a duplicate definition somewhere,
1908 then HI may not point to an indirect symbol.
1909 We will have reported an error to the user in
1912 if (hi
->root
.type
== bfd_link_hash_indirect
)
1914 /* If the symbol became indirect, then we
1915 assume that we have not seen a definition
1917 BFD_ASSERT ((hi
->elf_link_hash_flags
1918 & (ELF_LINK_HASH_DEF_DYNAMIC
1919 | ELF_LINK_HASH_DEF_REGULAR
))
1922 (*bed
->elf_backend_copy_indirect_symbol
) (h
, hi
);
1924 /* See if the new flags lead us to realize
1925 that the symbol must be dynamic. */
1931 || ((hi
->elf_link_hash_flags
1932 & ELF_LINK_HASH_REF_DYNAMIC
)
1938 if ((hi
->elf_link_hash_flags
1939 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1948 if (dynsym
&& h
->dynindx
== -1)
1950 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1952 if (h
->weakdef
!= NULL
1954 && h
->weakdef
->dynindx
== -1)
1956 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
1960 else if (dynsym
&& h
->dynindx
!= -1)
1961 /* If the symbol already has a dynamic index, but
1962 visibility says it should not be visible, turn it into
1964 switch (ELF_ST_VISIBILITY (h
->other
))
1968 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
1969 (*bed
->elf_backend_hide_symbol
) (info
, h
);
1970 _bfd_elf_strtab_delref (hash_table
->dynstr
,
1975 if (dt_needed
&& definition
1976 && (h
->elf_link_hash_flags
1977 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1979 bfd_size_type oldsize
;
1980 bfd_size_type strindex
;
1982 if (! is_elf_hash_table (info
))
1985 /* The symbol from a DT_NEEDED object is referenced from
1986 the regular object to create a dynamic executable. We
1987 have to make sure there is a DT_NEEDED entry for it. */
1990 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
1991 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
,
1992 elf_dt_soname (abfd
), false);
1993 if (strindex
== (bfd_size_type
) -1)
1996 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
1999 Elf_External_Dyn
*dyncon
, *dynconend
;
2001 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
,
2003 BFD_ASSERT (sdyn
!= NULL
);
2005 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
2006 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
2008 for (; dyncon
< dynconend
; dyncon
++)
2010 Elf_Internal_Dyn dyn
;
2012 elf_swap_dyn_in (hash_table
->dynobj
,
2014 BFD_ASSERT (dyn
.d_tag
!= DT_NEEDED
||
2015 dyn
.d_un
.d_val
!= strindex
);
2019 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NEEDED
, strindex
))
2025 /* Now set the weakdefs field correctly for all the weak defined
2026 symbols we found. The only way to do this is to search all the
2027 symbols. Since we only need the information for non functions in
2028 dynamic objects, that's the only time we actually put anything on
2029 the list WEAKS. We need this information so that if a regular
2030 object refers to a symbol defined weakly in a dynamic object, the
2031 real symbol in the dynamic object is also put in the dynamic
2032 symbols; we also must arrange for both symbols to point to the
2033 same memory location. We could handle the general case of symbol
2034 aliasing, but a general symbol alias can only be generated in
2035 assembler code, handling it correctly would be very time
2036 consuming, and other ELF linkers don't handle general aliasing
2038 while (weaks
!= NULL
)
2040 struct elf_link_hash_entry
*hlook
;
2043 struct elf_link_hash_entry
**hpp
;
2044 struct elf_link_hash_entry
**hppend
;
2047 weaks
= hlook
->weakdef
;
2048 hlook
->weakdef
= NULL
;
2050 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
2051 || hlook
->root
.type
== bfd_link_hash_defweak
2052 || hlook
->root
.type
== bfd_link_hash_common
2053 || hlook
->root
.type
== bfd_link_hash_indirect
);
2054 slook
= hlook
->root
.u
.def
.section
;
2055 vlook
= hlook
->root
.u
.def
.value
;
2057 hpp
= elf_sym_hashes (abfd
);
2058 hppend
= hpp
+ extsymcount
;
2059 for (; hpp
< hppend
; hpp
++)
2061 struct elf_link_hash_entry
*h
;
2064 if (h
!= NULL
&& h
!= hlook
2065 && h
->root
.type
== bfd_link_hash_defined
2066 && h
->root
.u
.def
.section
== slook
2067 && h
->root
.u
.def
.value
== vlook
)
2071 /* If the weak definition is in the list of dynamic
2072 symbols, make sure the real definition is put there
2074 if (hlook
->dynindx
!= -1
2075 && h
->dynindx
== -1)
2077 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2081 /* If the real definition is in the list of dynamic
2082 symbols, make sure the weak definition is put there
2083 as well. If we don't do this, then the dynamic
2084 loader might not merge the entries for the real
2085 definition and the weak definition. */
2086 if (h
->dynindx
!= -1
2087 && hlook
->dynindx
== -1)
2089 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
2104 if (extversym
!= NULL
)
2110 /* If this object is the same format as the output object, and it is
2111 not a shared library, then let the backend look through the
2114 This is required to build global offset table entries and to
2115 arrange for dynamic relocs. It is not required for the
2116 particular common case of linking non PIC code, even when linking
2117 against shared libraries, but unfortunately there is no way of
2118 knowing whether an object file has been compiled PIC or not.
2119 Looking through the relocs is not particularly time consuming.
2120 The problem is that we must either (1) keep the relocs in memory,
2121 which causes the linker to require additional runtime memory or
2122 (2) read the relocs twice from the input file, which wastes time.
2123 This would be a good case for using mmap.
2125 I have no idea how to handle linking PIC code into a file of a
2126 different format. It probably can't be done. */
2127 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
2129 && abfd
->xvec
== info
->hash
->creator
2130 && check_relocs
!= NULL
)
2134 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2136 Elf_Internal_Rela
*internal_relocs
;
2139 if ((o
->flags
& SEC_RELOC
) == 0
2140 || o
->reloc_count
== 0
2141 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
2142 && (o
->flags
& SEC_DEBUGGING
) != 0)
2143 || bfd_is_abs_section (o
->output_section
))
2146 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
2147 (abfd
, o
, (PTR
) NULL
,
2148 (Elf_Internal_Rela
*) NULL
,
2149 info
->keep_memory
));
2150 if (internal_relocs
== NULL
)
2153 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
2155 if (! info
->keep_memory
)
2156 free (internal_relocs
);
2163 /* If this is a non-traditional, non-relocateable link, try to
2164 optimize the handling of the .stab/.stabstr sections. */
2166 && ! info
->relocateable
2167 && ! info
->traditional_format
2168 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
2169 && is_elf_hash_table (info
)
2170 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
2172 asection
*stab
, *stabstr
;
2174 stab
= bfd_get_section_by_name (abfd
, ".stab");
2177 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
2179 if (stabstr
!= NULL
)
2181 struct bfd_elf_section_data
*secdata
;
2183 secdata
= elf_section_data (stab
);
2184 if (! _bfd_link_section_stabs (abfd
,
2185 & hash_table
->stab_info
,
2187 &secdata
->stab_info
))
2193 if (! info
->relocateable
&& ! dynamic
2194 && is_elf_hash_table (info
))
2198 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2199 if ((s
->flags
& SEC_MERGE
)
2200 && ! _bfd_merge_section (abfd
, & hash_table
->merge_info
, s
,
2201 & elf_section_data (s
)->merge_info
))
2212 if (extversym
!= NULL
)
2217 /* Create some sections which will be filled in with dynamic linking
2218 information. ABFD is an input file which requires dynamic sections
2219 to be created. The dynamic sections take up virtual memory space
2220 when the final executable is run, so we need to create them before
2221 addresses are assigned to the output sections. We work out the
2222 actual contents and size of these sections later. */
2225 elf_link_create_dynamic_sections (abfd
, info
)
2227 struct bfd_link_info
*info
;
2230 register asection
*s
;
2231 struct elf_link_hash_entry
*h
;
2232 struct elf_backend_data
*bed
;
2234 if (! is_elf_hash_table (info
))
2237 if (elf_hash_table (info
)->dynamic_sections_created
)
2240 /* Make sure that all dynamic sections use the same input BFD. */
2241 if (elf_hash_table (info
)->dynobj
== NULL
)
2242 elf_hash_table (info
)->dynobj
= abfd
;
2244 abfd
= elf_hash_table (info
)->dynobj
;
2246 /* Note that we set the SEC_IN_MEMORY flag for all of these
2248 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
2249 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
2251 /* A dynamically linked executable has a .interp section, but a
2252 shared library does not. */
2255 s
= bfd_make_section (abfd
, ".interp");
2257 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2261 /* Create sections to hold version informations. These are removed
2262 if they are not needed. */
2263 s
= bfd_make_section (abfd
, ".gnu.version_d");
2265 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2266 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2269 s
= bfd_make_section (abfd
, ".gnu.version");
2271 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2272 || ! bfd_set_section_alignment (abfd
, s
, 1))
2275 s
= bfd_make_section (abfd
, ".gnu.version_r");
2277 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2278 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2281 s
= bfd_make_section (abfd
, ".dynsym");
2283 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2284 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2287 s
= bfd_make_section (abfd
, ".dynstr");
2289 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2292 /* Create a strtab to hold the dynamic symbol names. */
2293 if (elf_hash_table (info
)->dynstr
== NULL
)
2295 elf_hash_table (info
)->dynstr
= _bfd_elf_strtab_init ();
2296 if (elf_hash_table (info
)->dynstr
== NULL
)
2300 s
= bfd_make_section (abfd
, ".dynamic");
2302 || ! bfd_set_section_flags (abfd
, s
, flags
)
2303 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2306 /* The special symbol _DYNAMIC is always set to the start of the
2307 .dynamic section. This call occurs before we have processed the
2308 symbols for any dynamic object, so we don't have to worry about
2309 overriding a dynamic definition. We could set _DYNAMIC in a
2310 linker script, but we only want to define it if we are, in fact,
2311 creating a .dynamic section. We don't want to define it if there
2312 is no .dynamic section, since on some ELF platforms the start up
2313 code examines it to decide how to initialize the process. */
2315 if (! (_bfd_generic_link_add_one_symbol
2316 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
2317 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
2318 (struct bfd_link_hash_entry
**) &h
)))
2320 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2321 h
->type
= STT_OBJECT
;
2324 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2327 bed
= get_elf_backend_data (abfd
);
2329 s
= bfd_make_section (abfd
, ".hash");
2331 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2332 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2334 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
2336 /* Let the backend create the rest of the sections. This lets the
2337 backend set the right flags. The backend will normally create
2338 the .got and .plt sections. */
2339 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
2342 elf_hash_table (info
)->dynamic_sections_created
= true;
2347 /* Add an entry to the .dynamic table. */
2350 elf_add_dynamic_entry (info
, tag
, val
)
2351 struct bfd_link_info
*info
;
2355 Elf_Internal_Dyn dyn
;
2358 bfd_size_type newsize
;
2359 bfd_byte
*newcontents
;
2361 if (! is_elf_hash_table (info
))
2364 dynobj
= elf_hash_table (info
)->dynobj
;
2366 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
2367 BFD_ASSERT (s
!= NULL
);
2369 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
2370 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
2371 if (newcontents
== NULL
)
2375 dyn
.d_un
.d_val
= val
;
2376 elf_swap_dyn_out (dynobj
, &dyn
,
2377 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
2379 s
->_raw_size
= newsize
;
2380 s
->contents
= newcontents
;
2385 /* Record a new local dynamic symbol. */
2388 elf_link_record_local_dynamic_symbol (info
, input_bfd
, input_indx
)
2389 struct bfd_link_info
*info
;
2393 struct elf_link_local_dynamic_entry
*entry
;
2394 struct elf_link_hash_table
*eht
;
2395 struct elf_strtab_hash
*dynstr
;
2396 Elf_External_Sym esym
;
2397 unsigned long dynstr_index
;
2402 if (! is_elf_hash_table (info
))
2405 /* See if the entry exists already. */
2406 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
2407 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
2410 entry
= (struct elf_link_local_dynamic_entry
*)
2411 bfd_alloc (input_bfd
, (bfd_size_type
) sizeof (*entry
));
2415 /* Go find the symbol, so that we can find it's name. */
2416 amt
= sizeof (Elf_External_Sym
);
2417 pos
= elf_tdata (input_bfd
)->symtab_hdr
.sh_offset
+ input_indx
* amt
;
2418 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
2419 || bfd_bread (&esym
, amt
, input_bfd
) != amt
)
2421 elf_swap_symbol_in (input_bfd
, &esym
, &entry
->isym
);
2423 name
= (bfd_elf_string_from_elf_section
2424 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
2425 entry
->isym
.st_name
));
2427 dynstr
= elf_hash_table (info
)->dynstr
;
2430 /* Create a strtab to hold the dynamic symbol names. */
2431 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
2436 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, false);
2437 if (dynstr_index
== (unsigned long) -1)
2439 entry
->isym
.st_name
= dynstr_index
;
2441 eht
= elf_hash_table (info
);
2443 entry
->next
= eht
->dynlocal
;
2444 eht
->dynlocal
= entry
;
2445 entry
->input_bfd
= input_bfd
;
2446 entry
->input_indx
= input_indx
;
2449 /* Whatever binding the symbol had before, it's now local. */
2451 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
2453 /* The dynindx will be set at the end of size_dynamic_sections. */
2458 /* Read and swap the relocs from the section indicated by SHDR. This
2459 may be either a REL or a RELA section. The relocations are
2460 translated into RELA relocations and stored in INTERNAL_RELOCS,
2461 which should have already been allocated to contain enough space.
2462 The EXTERNAL_RELOCS are a buffer where the external form of the
2463 relocations should be stored.
2465 Returns false if something goes wrong. */
2468 elf_link_read_relocs_from_section (abfd
, shdr
, external_relocs
,
2471 Elf_Internal_Shdr
*shdr
;
2472 PTR external_relocs
;
2473 Elf_Internal_Rela
*internal_relocs
;
2475 struct elf_backend_data
*bed
;
2478 /* If there aren't any relocations, that's OK. */
2482 /* Position ourselves at the start of the section. */
2483 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2486 /* Read the relocations. */
2487 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2490 bed
= get_elf_backend_data (abfd
);
2492 /* Convert the external relocations to the internal format. */
2493 if (shdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2495 Elf_External_Rel
*erel
;
2496 Elf_External_Rel
*erelend
;
2497 Elf_Internal_Rela
*irela
;
2498 Elf_Internal_Rel
*irel
;
2500 erel
= (Elf_External_Rel
*) external_relocs
;
2501 erelend
= erel
+ NUM_SHDR_ENTRIES (shdr
);
2502 irela
= internal_relocs
;
2503 amt
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
2504 irel
= bfd_alloc (abfd
, amt
);
2505 for (; erel
< erelend
; erel
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2509 if (bed
->s
->swap_reloc_in
)
2510 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
2512 elf_swap_reloc_in (abfd
, erel
, irel
);
2514 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; ++i
)
2516 irela
[i
].r_offset
= irel
[i
].r_offset
;
2517 irela
[i
].r_info
= irel
[i
].r_info
;
2518 irela
[i
].r_addend
= 0;
2524 Elf_External_Rela
*erela
;
2525 Elf_External_Rela
*erelaend
;
2526 Elf_Internal_Rela
*irela
;
2528 BFD_ASSERT (shdr
->sh_entsize
== sizeof (Elf_External_Rela
));
2530 erela
= (Elf_External_Rela
*) external_relocs
;
2531 erelaend
= erela
+ NUM_SHDR_ENTRIES (shdr
);
2532 irela
= internal_relocs
;
2533 for (; erela
< erelaend
; erela
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2535 if (bed
->s
->swap_reloca_in
)
2536 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
2538 elf_swap_reloca_in (abfd
, erela
, irela
);
2545 /* Read and swap the relocs for a section O. They may have been
2546 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2547 not NULL, they are used as buffers to read into. They are known to
2548 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2549 the return value is allocated using either malloc or bfd_alloc,
2550 according to the KEEP_MEMORY argument. If O has two relocation
2551 sections (both REL and RELA relocations), then the REL_HDR
2552 relocations will appear first in INTERNAL_RELOCS, followed by the
2553 REL_HDR2 relocations. */
2556 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
2560 PTR external_relocs
;
2561 Elf_Internal_Rela
*internal_relocs
;
2562 boolean keep_memory
;
2564 Elf_Internal_Shdr
*rel_hdr
;
2566 Elf_Internal_Rela
*alloc2
= NULL
;
2567 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2569 if (elf_section_data (o
)->relocs
!= NULL
)
2570 return elf_section_data (o
)->relocs
;
2572 if (o
->reloc_count
== 0)
2575 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2577 if (internal_relocs
== NULL
)
2581 size
= o
->reloc_count
;
2582 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2584 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2586 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2587 if (internal_relocs
== NULL
)
2591 if (external_relocs
== NULL
)
2593 bfd_size_type size
= rel_hdr
->sh_size
;
2595 if (elf_section_data (o
)->rel_hdr2
)
2596 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2597 alloc1
= (PTR
) bfd_malloc (size
);
2600 external_relocs
= alloc1
;
2603 if (!elf_link_read_relocs_from_section (abfd
, rel_hdr
,
2607 if (!elf_link_read_relocs_from_section
2609 elf_section_data (o
)->rel_hdr2
,
2610 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2611 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2612 * bed
->s
->int_rels_per_ext_rel
)))
2615 /* Cache the results for next time, if we can. */
2617 elf_section_data (o
)->relocs
= internal_relocs
;
2622 /* Don't free alloc2, since if it was allocated we are passing it
2623 back (under the name of internal_relocs). */
2625 return internal_relocs
;
2635 /* Record an assignment to a symbol made by a linker script. We need
2636 this in case some dynamic object refers to this symbol. */
2639 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
2640 bfd
*output_bfd ATTRIBUTE_UNUSED
;
2641 struct bfd_link_info
*info
;
2645 struct elf_link_hash_entry
*h
;
2647 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2650 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, true, false);
2654 if (h
->root
.type
== bfd_link_hash_new
)
2655 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_ELF
;
2657 /* If this symbol is being provided by the linker script, and it is
2658 currently defined by a dynamic object, but not by a regular
2659 object, then mark it as undefined so that the generic linker will
2660 force the correct value. */
2662 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2663 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2664 h
->root
.type
= bfd_link_hash_undefined
;
2666 /* If this symbol is not being provided by the linker script, and it is
2667 currently defined by a dynamic object, but not by a regular object,
2668 then clear out any version information because the symbol will not be
2669 associated with the dynamic object any more. */
2671 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2672 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2673 h
->verinfo
.verdef
= NULL
;
2675 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2677 /* When possible, keep the original type of the symbol. */
2678 if (h
->type
== STT_NOTYPE
)
2679 h
->type
= STT_OBJECT
;
2681 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2682 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
2684 && h
->dynindx
== -1)
2686 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2689 /* If this is a weak defined symbol, and we know a corresponding
2690 real symbol from the same dynamic object, make sure the real
2691 symbol is also made into a dynamic symbol. */
2692 if (h
->weakdef
!= NULL
2693 && h
->weakdef
->dynindx
== -1)
2695 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2703 /* This structure is used to pass information to
2704 elf_link_assign_sym_version. */
2706 struct elf_assign_sym_version_info
2710 /* General link information. */
2711 struct bfd_link_info
*info
;
2713 struct bfd_elf_version_tree
*verdefs
;
2714 /* Whether we had a failure. */
2718 /* This structure is used to pass information to
2719 elf_link_find_version_dependencies. */
2721 struct elf_find_verdep_info
2725 /* General link information. */
2726 struct bfd_link_info
*info
;
2727 /* The number of dependencies. */
2729 /* Whether we had a failure. */
2733 /* Array used to determine the number of hash table buckets to use
2734 based on the number of symbols there are. If there are fewer than
2735 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2736 fewer than 37 we use 17 buckets, and so forth. We never use more
2737 than 32771 buckets. */
2739 static const size_t elf_buckets
[] =
2741 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2745 /* Compute bucket count for hashing table. We do not use a static set
2746 of possible tables sizes anymore. Instead we determine for all
2747 possible reasonable sizes of the table the outcome (i.e., the
2748 number of collisions etc) and choose the best solution. The
2749 weighting functions are not too simple to allow the table to grow
2750 without bounds. Instead one of the weighting factors is the size.
2751 Therefore the result is always a good payoff between few collisions
2752 (= short chain lengths) and table size. */
2754 compute_bucket_count (info
)
2755 struct bfd_link_info
*info
;
2757 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2758 size_t best_size
= 0;
2759 unsigned long int *hashcodes
;
2760 unsigned long int *hashcodesp
;
2761 unsigned long int i
;
2764 /* Compute the hash values for all exported symbols. At the same
2765 time store the values in an array so that we could use them for
2768 amt
*= sizeof (unsigned long int);
2769 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
2770 if (hashcodes
== NULL
)
2772 hashcodesp
= hashcodes
;
2774 /* Put all hash values in HASHCODES. */
2775 elf_link_hash_traverse (elf_hash_table (info
),
2776 elf_collect_hash_codes
, &hashcodesp
);
2778 /* We have a problem here. The following code to optimize the table
2779 size requires an integer type with more the 32 bits. If
2780 BFD_HOST_U_64_BIT is set we know about such a type. */
2781 #ifdef BFD_HOST_U_64_BIT
2782 if (info
->optimize
== true)
2784 unsigned long int nsyms
= hashcodesp
- hashcodes
;
2787 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
2788 unsigned long int *counts
;
2790 /* Possible optimization parameters: if we have NSYMS symbols we say
2791 that the hashing table must at least have NSYMS/4 and at most
2793 minsize
= nsyms
/ 4;
2796 best_size
= maxsize
= nsyms
* 2;
2798 /* Create array where we count the collisions in. We must use bfd_malloc
2799 since the size could be large. */
2801 amt
*= sizeof (unsigned long int);
2802 counts
= (unsigned long int *) bfd_malloc (amt
);
2809 /* Compute the "optimal" size for the hash table. The criteria is a
2810 minimal chain length. The minor criteria is (of course) the size
2812 for (i
= minsize
; i
< maxsize
; ++i
)
2814 /* Walk through the array of hashcodes and count the collisions. */
2815 BFD_HOST_U_64_BIT max
;
2816 unsigned long int j
;
2817 unsigned long int fact
;
2819 memset (counts
, '\0', i
* sizeof (unsigned long int));
2821 /* Determine how often each hash bucket is used. */
2822 for (j
= 0; j
< nsyms
; ++j
)
2823 ++counts
[hashcodes
[j
] % i
];
2825 /* For the weight function we need some information about the
2826 pagesize on the target. This is information need not be 100%
2827 accurate. Since this information is not available (so far) we
2828 define it here to a reasonable default value. If it is crucial
2829 to have a better value some day simply define this value. */
2830 # ifndef BFD_TARGET_PAGESIZE
2831 # define BFD_TARGET_PAGESIZE (4096)
2834 /* We in any case need 2 + NSYMS entries for the size values and
2836 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
2839 /* Variant 1: optimize for short chains. We add the squares
2840 of all the chain lengths (which favous many small chain
2841 over a few long chains). */
2842 for (j
= 0; j
< i
; ++j
)
2843 max
+= counts
[j
] * counts
[j
];
2845 /* This adds penalties for the overall size of the table. */
2846 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2849 /* Variant 2: Optimize a lot more for small table. Here we
2850 also add squares of the size but we also add penalties for
2851 empty slots (the +1 term). */
2852 for (j
= 0; j
< i
; ++j
)
2853 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
2855 /* The overall size of the table is considered, but not as
2856 strong as in variant 1, where it is squared. */
2857 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2861 /* Compare with current best results. */
2862 if (max
< best_chlen
)
2872 #endif /* defined (BFD_HOST_U_64_BIT) */
2874 /* This is the fallback solution if no 64bit type is available or if we
2875 are not supposed to spend much time on optimizations. We select the
2876 bucket count using a fixed set of numbers. */
2877 for (i
= 0; elf_buckets
[i
] != 0; i
++)
2879 best_size
= elf_buckets
[i
];
2880 if (dynsymcount
< elf_buckets
[i
+ 1])
2885 /* Free the arrays we needed. */
2891 /* Set up the sizes and contents of the ELF dynamic sections. This is
2892 called by the ELF linker emulation before_allocation routine. We
2893 must set the sizes of the sections before the linker sets the
2894 addresses of the various sections. */
2897 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
2899 auxiliary_filters
, info
, sinterpptr
,
2904 const char *filter_shlib
;
2905 const char * const *auxiliary_filters
;
2906 struct bfd_link_info
*info
;
2907 asection
**sinterpptr
;
2908 struct bfd_elf_version_tree
*verdefs
;
2910 bfd_size_type soname_indx
;
2912 struct elf_backend_data
*bed
;
2913 struct elf_assign_sym_version_info asvinfo
;
2917 soname_indx
= (bfd_size_type
) -1;
2919 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2922 if (! is_elf_hash_table (info
))
2925 /* Any syms created from now on start with -1 in
2926 got.refcount/offset and plt.refcount/offset. */
2927 elf_hash_table (info
)->init_refcount
= -1;
2929 /* The backend may have to create some sections regardless of whether
2930 we're dynamic or not. */
2931 bed
= get_elf_backend_data (output_bfd
);
2932 if (bed
->elf_backend_always_size_sections
2933 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
2936 dynobj
= elf_hash_table (info
)->dynobj
;
2938 /* If there were no dynamic objects in the link, there is nothing to
2943 if (elf_hash_table (info
)->dynamic_sections_created
)
2945 struct elf_info_failed eif
;
2946 struct elf_link_hash_entry
*h
;
2949 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
2950 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
2954 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2956 if (soname_indx
== (bfd_size_type
) -1
2957 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SONAME
,
2964 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMBOLIC
,
2967 info
->flags
|= DF_SYMBOLIC
;
2974 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
2976 if (info
->new_dtags
)
2977 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
2978 if (indx
== (bfd_size_type
) -1
2979 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RPATH
, indx
)
2981 && ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_RUNPATH
,
2986 if (filter_shlib
!= NULL
)
2990 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2991 filter_shlib
, true);
2992 if (indx
== (bfd_size_type
) -1
2993 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FILTER
, indx
))
2997 if (auxiliary_filters
!= NULL
)
2999 const char * const *p
;
3001 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
3005 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3007 if (indx
== (bfd_size_type
) -1
3008 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_AUXILIARY
,
3015 eif
.verdefs
= verdefs
;
3018 /* If we are supposed to export all symbols into the dynamic symbol
3019 table (this is not the normal case), then do so. */
3020 if (info
->export_dynamic
)
3022 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
3028 /* Attach all the symbols to their version information. */
3029 asvinfo
.output_bfd
= output_bfd
;
3030 asvinfo
.info
= info
;
3031 asvinfo
.verdefs
= verdefs
;
3032 asvinfo
.failed
= false;
3034 elf_link_hash_traverse (elf_hash_table (info
),
3035 elf_link_assign_sym_version
,
3040 /* Find all symbols which were defined in a dynamic object and make
3041 the backend pick a reasonable value for them. */
3042 elf_link_hash_traverse (elf_hash_table (info
),
3043 elf_adjust_dynamic_symbol
,
3048 /* Add some entries to the .dynamic section. We fill in some of the
3049 values later, in elf_bfd_final_link, but we must add the entries
3050 now so that we know the final size of the .dynamic section. */
3052 /* If there are initialization and/or finalization functions to
3053 call then add the corresponding DT_INIT/DT_FINI entries. */
3054 h
= (info
->init_function
3055 ? elf_link_hash_lookup (elf_hash_table (info
),
3056 info
->init_function
, false,
3060 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3061 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3063 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_INIT
, (bfd_vma
) 0))
3066 h
= (info
->fini_function
3067 ? elf_link_hash_lookup (elf_hash_table (info
),
3068 info
->fini_function
, false,
3072 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
3073 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
3075 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FINI
, (bfd_vma
) 0))
3079 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
3080 /* If .dynstr is excluded from the link, we don't want any of
3081 these tags. Strictly, we should be checking each section
3082 individually; This quick check covers for the case where
3083 someone does a /DISCARD/ : { *(*) }. */
3084 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
3086 bfd_size_type strsize
;
3088 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3089 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_HASH
, (bfd_vma
) 0)
3090 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRTAB
, (bfd_vma
) 0)
3091 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMTAB
, (bfd_vma
) 0)
3092 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_STRSZ
, strsize
)
3093 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_SYMENT
,
3094 (bfd_vma
) sizeof (Elf_External_Sym
)))
3099 /* The backend must work out the sizes of all the other dynamic
3101 if (bed
->elf_backend_size_dynamic_sections
3102 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
3105 if (elf_hash_table (info
)->dynamic_sections_created
)
3107 bfd_size_type dynsymcount
;
3109 size_t bucketcount
= 0;
3110 size_t hash_entry_size
;
3111 unsigned int dtagcount
;
3113 /* Set up the version definition section. */
3114 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3115 BFD_ASSERT (s
!= NULL
);
3117 /* We may have created additional version definitions if we are
3118 just linking a regular application. */
3119 verdefs
= asvinfo
.verdefs
;
3121 if (verdefs
== NULL
)
3122 _bfd_strip_section_from_output (info
, s
);
3127 struct bfd_elf_version_tree
*t
;
3129 Elf_Internal_Verdef def
;
3130 Elf_Internal_Verdaux defaux
;
3135 /* Make space for the base version. */
3136 size
+= sizeof (Elf_External_Verdef
);
3137 size
+= sizeof (Elf_External_Verdaux
);
3140 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3142 struct bfd_elf_version_deps
*n
;
3144 size
+= sizeof (Elf_External_Verdef
);
3145 size
+= sizeof (Elf_External_Verdaux
);
3148 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3149 size
+= sizeof (Elf_External_Verdaux
);
3152 s
->_raw_size
= size
;
3153 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3154 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3157 /* Fill in the version definition section. */
3161 def
.vd_version
= VER_DEF_CURRENT
;
3162 def
.vd_flags
= VER_FLG_BASE
;
3165 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3166 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3167 + sizeof (Elf_External_Verdaux
));
3169 if (soname_indx
!= (bfd_size_type
) -1)
3171 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3173 def
.vd_hash
= bfd_elf_hash (soname
);
3174 defaux
.vda_name
= soname_indx
;
3181 name
= basename (output_bfd
->filename
);
3182 def
.vd_hash
= bfd_elf_hash (name
);
3183 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3185 if (indx
== (bfd_size_type
) -1)
3187 defaux
.vda_name
= indx
;
3189 defaux
.vda_next
= 0;
3191 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3192 (Elf_External_Verdef
*) p
);
3193 p
+= sizeof (Elf_External_Verdef
);
3194 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3195 (Elf_External_Verdaux
*) p
);
3196 p
+= sizeof (Elf_External_Verdaux
);
3198 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
3201 struct bfd_elf_version_deps
*n
;
3202 struct elf_link_hash_entry
*h
;
3205 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3208 /* Add a symbol representing this version. */
3210 if (! (_bfd_generic_link_add_one_symbol
3211 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
3212 (bfd_vma
) 0, (const char *) NULL
, false,
3213 get_elf_backend_data (dynobj
)->collect
,
3214 (struct bfd_link_hash_entry
**) &h
)))
3216 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
3217 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3218 h
->type
= STT_OBJECT
;
3219 h
->verinfo
.vertree
= t
;
3221 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
3224 def
.vd_version
= VER_DEF_CURRENT
;
3226 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
3227 def
.vd_flags
|= VER_FLG_WEAK
;
3228 def
.vd_ndx
= t
->vernum
+ 1;
3229 def
.vd_cnt
= cdeps
+ 1;
3230 def
.vd_hash
= bfd_elf_hash (t
->name
);
3231 def
.vd_aux
= sizeof (Elf_External_Verdef
);
3232 if (t
->next
!= NULL
)
3233 def
.vd_next
= (sizeof (Elf_External_Verdef
)
3234 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
3238 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
3239 (Elf_External_Verdef
*) p
);
3240 p
+= sizeof (Elf_External_Verdef
);
3242 defaux
.vda_name
= h
->dynstr_index
;
3243 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3245 if (t
->deps
== NULL
)
3246 defaux
.vda_next
= 0;
3248 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3249 t
->name_indx
= defaux
.vda_name
;
3251 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3252 (Elf_External_Verdaux
*) p
);
3253 p
+= sizeof (Elf_External_Verdaux
);
3255 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
3257 if (n
->version_needed
== NULL
)
3259 /* This can happen if there was an error in the
3261 defaux
.vda_name
= 0;
3265 defaux
.vda_name
= n
->version_needed
->name_indx
;
3266 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
3269 if (n
->next
== NULL
)
3270 defaux
.vda_next
= 0;
3272 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
3274 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
3275 (Elf_External_Verdaux
*) p
);
3276 p
+= sizeof (Elf_External_Verdaux
);
3280 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEF
, (bfd_vma
) 0)
3281 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERDEFNUM
,
3285 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
3288 if (info
->new_dtags
&& info
->flags
)
3290 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS
, info
->flags
))
3297 info
->flags_1
&= ~ (DF_1_INITFIRST
3300 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_FLAGS_1
,
3305 /* Work out the size of the version reference section. */
3307 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3308 BFD_ASSERT (s
!= NULL
);
3310 struct elf_find_verdep_info sinfo
;
3312 sinfo
.output_bfd
= output_bfd
;
3314 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
3315 if (sinfo
.vers
== 0)
3317 sinfo
.failed
= false;
3319 elf_link_hash_traverse (elf_hash_table (info
),
3320 elf_link_find_version_dependencies
,
3323 if (elf_tdata (output_bfd
)->verref
== NULL
)
3324 _bfd_strip_section_from_output (info
, s
);
3327 Elf_Internal_Verneed
*t
;
3332 /* Build the version definition section. */
3335 for (t
= elf_tdata (output_bfd
)->verref
;
3339 Elf_Internal_Vernaux
*a
;
3341 size
+= sizeof (Elf_External_Verneed
);
3343 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3344 size
+= sizeof (Elf_External_Vernaux
);
3347 s
->_raw_size
= size
;
3348 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3349 if (s
->contents
== NULL
)
3353 for (t
= elf_tdata (output_bfd
)->verref
;
3358 Elf_Internal_Vernaux
*a
;
3362 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3365 t
->vn_version
= VER_NEED_CURRENT
;
3367 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3368 elf_dt_name (t
->vn_bfd
) != NULL
3369 ? elf_dt_name (t
->vn_bfd
)
3370 : basename (t
->vn_bfd
->filename
),
3372 if (indx
== (bfd_size_type
) -1)
3375 t
->vn_aux
= sizeof (Elf_External_Verneed
);
3376 if (t
->vn_nextref
== NULL
)
3379 t
->vn_next
= (sizeof (Elf_External_Verneed
)
3380 + caux
* sizeof (Elf_External_Vernaux
));
3382 _bfd_elf_swap_verneed_out (output_bfd
, t
,
3383 (Elf_External_Verneed
*) p
);
3384 p
+= sizeof (Elf_External_Verneed
);
3386 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3388 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
3389 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
3390 a
->vna_nodename
, false);
3391 if (indx
== (bfd_size_type
) -1)
3394 if (a
->vna_nextptr
== NULL
)
3397 a
->vna_next
= sizeof (Elf_External_Vernaux
);
3399 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
3400 (Elf_External_Vernaux
*) p
);
3401 p
+= sizeof (Elf_External_Vernaux
);
3405 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEED
,
3407 || ! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERNEEDNUM
,
3411 elf_tdata (output_bfd
)->cverrefs
= crefs
;
3415 /* Assign dynsym indicies. In a shared library we generate a
3416 section symbol for each output section, which come first.
3417 Next come all of the back-end allocated local dynamic syms,
3418 followed by the rest of the global symbols. */
3420 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3422 /* Work out the size of the symbol version section. */
3423 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3424 BFD_ASSERT (s
!= NULL
);
3425 if (dynsymcount
== 0
3426 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
3428 _bfd_strip_section_from_output (info
, s
);
3429 /* The DYNSYMCOUNT might have changed if we were going to
3430 output a dynamic symbol table entry for S. */
3431 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3435 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
3436 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3437 if (s
->contents
== NULL
)
3440 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_VERSYM
, (bfd_vma
) 0))
3444 /* Set the size of the .dynsym and .hash sections. We counted
3445 the number of dynamic symbols in elf_link_add_object_symbols.
3446 We will build the contents of .dynsym and .hash when we build
3447 the final symbol table, because until then we do not know the
3448 correct value to give the symbols. We built the .dynstr
3449 section as we went along in elf_link_add_object_symbols. */
3450 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
3451 BFD_ASSERT (s
!= NULL
);
3452 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
3453 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3454 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3457 if (dynsymcount
!= 0)
3459 Elf_Internal_Sym isym
;
3461 /* The first entry in .dynsym is a dummy symbol. */
3468 elf_swap_symbol_out (output_bfd
, &isym
,
3469 (PTR
) (Elf_External_Sym
*) s
->contents
);
3472 /* Compute the size of the hashing table. As a side effect this
3473 computes the hash values for all the names we export. */
3474 bucketcount
= compute_bucket_count (info
);
3476 s
= bfd_get_section_by_name (dynobj
, ".hash");
3477 BFD_ASSERT (s
!= NULL
);
3478 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
3479 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
3480 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3481 if (s
->contents
== NULL
)
3483 memset (s
->contents
, 0, (size_t) s
->_raw_size
);
3485 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) bucketcount
,
3487 bfd_put (8 * hash_entry_size
, output_bfd
, (bfd_vma
) dynsymcount
,
3488 s
->contents
+ hash_entry_size
);
3490 elf_hash_table (info
)->bucketcount
= bucketcount
;
3492 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
3493 BFD_ASSERT (s
!= NULL
);
3495 elf_finalize_dynstr (output_bfd
, info
);
3497 s
->_raw_size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
3499 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
3500 if (! elf_add_dynamic_entry (info
, (bfd_vma
) DT_NULL
, (bfd_vma
) 0))
3507 /* This function is used to adjust offsets into .dynstr for
3508 dynamic symbols. This is called via elf_link_hash_traverse. */
3510 static boolean elf_adjust_dynstr_offsets
3511 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3514 elf_adjust_dynstr_offsets (h
, data
)
3515 struct elf_link_hash_entry
*h
;
3518 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3520 if (h
->dynindx
!= -1)
3521 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3525 /* Assign string offsets in .dynstr, update all structures referencing
3529 elf_finalize_dynstr (output_bfd
, info
)
3531 struct bfd_link_info
*info
;
3533 struct elf_link_local_dynamic_entry
*entry
;
3534 struct elf_strtab_hash
*dynstr
= elf_hash_table (info
)->dynstr
;
3535 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
3538 Elf_External_Dyn
*dyncon
, *dynconend
;
3540 _bfd_elf_strtab_finalize (dynstr
);
3541 size
= _bfd_elf_strtab_size (dynstr
);
3543 /* Update all .dynamic entries referencing .dynstr strings. */
3544 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3545 BFD_ASSERT (sdyn
!= NULL
);
3547 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
3548 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
3550 for (; dyncon
< dynconend
; dyncon
++)
3552 Elf_Internal_Dyn dyn
;
3554 elf_swap_dyn_in (dynobj
, dyncon
, & dyn
);
3558 dyn
.d_un
.d_val
= size
;
3559 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3567 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3568 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
3575 /* Now update local dynamic symbols. */
3576 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
3577 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3578 entry
->isym
.st_name
);
3580 /* And the rest of dynamic symbols. */
3581 elf_link_hash_traverse (elf_hash_table (info
),
3582 elf_adjust_dynstr_offsets
, dynstr
);
3584 /* Adjust version definitions. */
3585 if (elf_tdata (output_bfd
)->cverdefs
)
3590 Elf_Internal_Verdef def
;
3591 Elf_Internal_Verdaux defaux
;
3593 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3594 p
= (bfd_byte
*) s
->contents
;
3597 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3599 p
+= sizeof (Elf_External_Verdef
);
3600 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3602 _bfd_elf_swap_verdaux_in (output_bfd
,
3603 (Elf_External_Verdaux
*) p
, &defaux
);
3604 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3606 _bfd_elf_swap_verdaux_out (output_bfd
,
3607 &defaux
, (Elf_External_Verdaux
*) p
);
3608 p
+= sizeof (Elf_External_Verdaux
);
3611 while (def
.vd_next
);
3614 /* Adjust version references. */
3615 if (elf_tdata (output_bfd
)->verref
)
3620 Elf_Internal_Verneed need
;
3621 Elf_Internal_Vernaux needaux
;
3623 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3624 p
= (bfd_byte
*) s
->contents
;
3627 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3629 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3630 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3631 (Elf_External_Verneed
*) p
);
3632 p
+= sizeof (Elf_External_Verneed
);
3633 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3635 _bfd_elf_swap_vernaux_in (output_bfd
,
3636 (Elf_External_Vernaux
*) p
, &needaux
);
3637 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3639 _bfd_elf_swap_vernaux_out (output_bfd
,
3641 (Elf_External_Vernaux
*) p
);
3642 p
+= sizeof (Elf_External_Vernaux
);
3645 while (need
.vn_next
);
3651 /* Fix up the flags for a symbol. This handles various cases which
3652 can only be fixed after all the input files are seen. This is
3653 currently called by both adjust_dynamic_symbol and
3654 assign_sym_version, which is unnecessary but perhaps more robust in
3655 the face of future changes. */
3658 elf_fix_symbol_flags (h
, eif
)
3659 struct elf_link_hash_entry
*h
;
3660 struct elf_info_failed
*eif
;
3662 /* If this symbol was mentioned in a non-ELF file, try to set
3663 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3664 permit a non-ELF file to correctly refer to a symbol defined in
3665 an ELF dynamic object. */
3666 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
3668 while (h
->root
.type
== bfd_link_hash_indirect
)
3669 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3671 if (h
->root
.type
!= bfd_link_hash_defined
3672 && h
->root
.type
!= bfd_link_hash_defweak
)
3673 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3674 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3677 if (h
->root
.u
.def
.section
->owner
!= NULL
3678 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3679 == bfd_target_elf_flavour
))
3680 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3681 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3683 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3686 if (h
->dynindx
== -1
3687 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
3688 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
3690 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3699 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3700 was first seen in a non-ELF file. Fortunately, if the symbol
3701 was first seen in an ELF file, we're probably OK unless the
3702 symbol was defined in a non-ELF file. Catch that case here.
3703 FIXME: We're still in trouble if the symbol was first seen in
3704 a dynamic object, and then later in a non-ELF regular object. */
3705 if ((h
->root
.type
== bfd_link_hash_defined
3706 || h
->root
.type
== bfd_link_hash_defweak
)
3707 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3708 && (h
->root
.u
.def
.section
->owner
!= NULL
3709 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3710 != bfd_target_elf_flavour
)
3711 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3712 && (h
->elf_link_hash_flags
3713 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
3714 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3717 /* If this is a final link, and the symbol was defined as a common
3718 symbol in a regular object file, and there was no definition in
3719 any dynamic object, then the linker will have allocated space for
3720 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3721 flag will not have been set. */
3722 if (h
->root
.type
== bfd_link_hash_defined
3723 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3724 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
3725 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3726 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3727 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3729 /* If -Bsymbolic was used (which means to bind references to global
3730 symbols to the definition within the shared object), and this
3731 symbol was defined in a regular object, then it actually doesn't
3732 need a PLT entry, and we can accomplish that by forcing it local.
3733 Likewise, if the symbol has hidden or internal visibility.
3734 FIXME: It might be that we also do not need a PLT for other
3735 non-hidden visibilities, but we would have to tell that to the
3736 backend specifically; we can't just clear PLT-related data here. */
3737 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
3738 && eif
->info
->shared
3739 && is_elf_hash_table (eif
->info
)
3740 && (eif
->info
->symbolic
3741 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3742 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3743 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3745 struct elf_backend_data
*bed
;
3747 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3748 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3749 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
3751 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3752 _bfd_elf_strtab_delref (elf_hash_table (eif
->info
)->dynstr
,
3755 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
);
3758 /* If this is a weak defined symbol in a dynamic object, and we know
3759 the real definition in the dynamic object, copy interesting flags
3760 over to the real definition. */
3761 if (h
->weakdef
!= NULL
)
3763 struct elf_link_hash_entry
*weakdef
;
3765 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3766 || h
->root
.type
== bfd_link_hash_defweak
);
3767 weakdef
= h
->weakdef
;
3768 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
3769 || weakdef
->root
.type
== bfd_link_hash_defweak
);
3770 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
3772 /* If the real definition is defined by a regular object file,
3773 don't do anything special. See the longer description in
3774 elf_adjust_dynamic_symbol, below. */
3775 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3779 struct elf_backend_data
*bed
;
3781 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3782 (*bed
->elf_backend_copy_indirect_symbol
) (weakdef
, h
);
3789 /* Make the backend pick a good value for a dynamic symbol. This is
3790 called via elf_link_hash_traverse, and also calls itself
3794 elf_adjust_dynamic_symbol (h
, data
)
3795 struct elf_link_hash_entry
*h
;
3798 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3800 struct elf_backend_data
*bed
;
3802 /* Ignore indirect symbols. These are added by the versioning code. */
3803 if (h
->root
.type
== bfd_link_hash_indirect
)
3806 if (! is_elf_hash_table (eif
->info
))
3809 /* Fix the symbol flags. */
3810 if (! elf_fix_symbol_flags (h
, eif
))
3813 /* If this symbol does not require a PLT entry, and it is not
3814 defined by a dynamic object, or is not referenced by a regular
3815 object, ignore it. We do have to handle a weak defined symbol,
3816 even if no regular object refers to it, if we decided to add it
3817 to the dynamic symbol table. FIXME: Do we normally need to worry
3818 about symbols which are defined by one dynamic object and
3819 referenced by another one? */
3820 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
3821 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
3822 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3823 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
3824 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
3826 h
->plt
.offset
= (bfd_vma
) -1;
3830 /* If we've already adjusted this symbol, don't do it again. This
3831 can happen via a recursive call. */
3832 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
3835 /* Don't look at this symbol again. Note that we must set this
3836 after checking the above conditions, because we may look at a
3837 symbol once, decide not to do anything, and then get called
3838 recursively later after REF_REGULAR is set below. */
3839 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
3841 /* If this is a weak definition, and we know a real definition, and
3842 the real symbol is not itself defined by a regular object file,
3843 then get a good value for the real definition. We handle the
3844 real symbol first, for the convenience of the backend routine.
3846 Note that there is a confusing case here. If the real definition
3847 is defined by a regular object file, we don't get the real symbol
3848 from the dynamic object, but we do get the weak symbol. If the
3849 processor backend uses a COPY reloc, then if some routine in the
3850 dynamic object changes the real symbol, we will not see that
3851 change in the corresponding weak symbol. This is the way other
3852 ELF linkers work as well, and seems to be a result of the shared
3855 I will clarify this issue. Most SVR4 shared libraries define the
3856 variable _timezone and define timezone as a weak synonym. The
3857 tzset call changes _timezone. If you write
3858 extern int timezone;
3860 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3861 you might expect that, since timezone is a synonym for _timezone,
3862 the same number will print both times. However, if the processor
3863 backend uses a COPY reloc, then actually timezone will be copied
3864 into your process image, and, since you define _timezone
3865 yourself, _timezone will not. Thus timezone and _timezone will
3866 wind up at different memory locations. The tzset call will set
3867 _timezone, leaving timezone unchanged. */
3869 if (h
->weakdef
!= NULL
)
3871 /* If we get to this point, we know there is an implicit
3872 reference by a regular object file via the weak symbol H.
3873 FIXME: Is this really true? What if the traversal finds
3874 H->WEAKDEF before it finds H? */
3875 h
->weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
3877 if (! elf_adjust_dynamic_symbol (h
->weakdef
, (PTR
) eif
))
3881 /* If a symbol has no type and no size and does not require a PLT
3882 entry, then we are probably about to do the wrong thing here: we
3883 are probably going to create a COPY reloc for an empty object.
3884 This case can arise when a shared object is built with assembly
3885 code, and the assembly code fails to set the symbol type. */
3887 && h
->type
== STT_NOTYPE
3888 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
3889 (*_bfd_error_handler
)
3890 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3891 h
->root
.root
.string
);
3893 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
3894 bed
= get_elf_backend_data (dynobj
);
3895 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3904 /* This routine is used to export all defined symbols into the dynamic
3905 symbol table. It is called via elf_link_hash_traverse. */
3908 elf_export_symbol (h
, data
)
3909 struct elf_link_hash_entry
*h
;
3912 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3914 /* Ignore indirect symbols. These are added by the versioning code. */
3915 if (h
->root
.type
== bfd_link_hash_indirect
)
3918 if (h
->dynindx
== -1
3919 && (h
->elf_link_hash_flags
3920 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
3922 struct bfd_elf_version_tree
*t
;
3923 struct bfd_elf_version_expr
*d
;
3925 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
3927 if (t
->globals
!= NULL
)
3929 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3931 if ((*d
->match
) (d
, h
->root
.root
.string
))
3936 if (t
->locals
!= NULL
)
3938 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3940 if ((*d
->match
) (d
, h
->root
.root
.string
))
3949 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3960 /* Look through the symbols which are defined in other shared
3961 libraries and referenced here. Update the list of version
3962 dependencies. This will be put into the .gnu.version_r section.
3963 This function is called via elf_link_hash_traverse. */
3966 elf_link_find_version_dependencies (h
, data
)
3967 struct elf_link_hash_entry
*h
;
3970 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
3971 Elf_Internal_Verneed
*t
;
3972 Elf_Internal_Vernaux
*a
;
3975 /* We only care about symbols defined in shared objects with version
3977 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3978 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
3980 || h
->verinfo
.verdef
== NULL
)
3983 /* See if we already know about this version. */
3984 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
3986 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
3989 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3990 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
3996 /* This is a new version. Add it to tree we are building. */
4001 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4004 rinfo
->failed
= true;
4008 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
4009 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
4010 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
4014 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, amt
);
4016 /* Note that we are copying a string pointer here, and testing it
4017 above. If bfd_elf_string_from_elf_section is ever changed to
4018 discard the string data when low in memory, this will have to be
4020 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
4022 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
4023 a
->vna_nextptr
= t
->vn_auxptr
;
4025 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
4028 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4035 /* Figure out appropriate versions for all the symbols. We may not
4036 have the version number script until we have read all of the input
4037 files, so until that point we don't know which symbols should be
4038 local. This function is called via elf_link_hash_traverse. */
4041 elf_link_assign_sym_version (h
, data
)
4042 struct elf_link_hash_entry
*h
;
4045 struct elf_assign_sym_version_info
*sinfo
;
4046 struct bfd_link_info
*info
;
4047 struct elf_backend_data
*bed
;
4048 struct elf_info_failed eif
;
4052 sinfo
= (struct elf_assign_sym_version_info
*) data
;
4055 /* Fix the symbol flags. */
4058 if (! elf_fix_symbol_flags (h
, &eif
))
4061 sinfo
->failed
= true;
4065 /* We only need version numbers for symbols defined in regular
4067 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4070 bed
= get_elf_backend_data (sinfo
->output_bfd
);
4071 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4072 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
4074 struct bfd_elf_version_tree
*t
;
4079 /* There are two consecutive ELF_VER_CHR characters if this is
4080 not a hidden symbol. */
4082 if (*p
== ELF_VER_CHR
)
4088 /* If there is no version string, we can just return out. */
4092 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4096 /* Look for the version. If we find it, it is no longer weak. */
4097 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4099 if (strcmp (t
->name
, p
) == 0)
4103 struct bfd_elf_version_expr
*d
;
4105 len
= p
- h
->root
.root
.string
;
4106 alc
= bfd_alloc (sinfo
->output_bfd
, (bfd_size_type
) len
);
4109 strncpy (alc
, h
->root
.root
.string
, len
- 1);
4110 alc
[len
- 1] = '\0';
4111 if (alc
[len
- 2] == ELF_VER_CHR
)
4112 alc
[len
- 2] = '\0';
4114 h
->verinfo
.vertree
= t
;
4118 if (t
->globals
!= NULL
)
4120 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4121 if ((*d
->match
) (d
, alc
))
4125 /* See if there is anything to force this symbol to
4127 if (d
== NULL
&& t
->locals
!= NULL
)
4129 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4131 if ((*d
->match
) (d
, alc
))
4133 if (h
->dynindx
!= -1
4135 && ! info
->export_dynamic
)
4137 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
4138 (*bed
->elf_backend_hide_symbol
) (info
, h
);
4139 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
4148 bfd_release (sinfo
->output_bfd
, alc
);
4153 /* If we are building an application, we need to create a
4154 version node for this version. */
4155 if (t
== NULL
&& ! info
->shared
)
4157 struct bfd_elf_version_tree
**pp
;
4160 /* If we aren't going to export this symbol, we don't need
4161 to worry about it. */
4162 if (h
->dynindx
== -1)
4166 t
= ((struct bfd_elf_version_tree
*)
4167 bfd_alloc (sinfo
->output_bfd
, amt
));
4170 sinfo
->failed
= true;
4179 t
->name_indx
= (unsigned int) -1;
4183 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4185 t
->vernum
= version_index
;
4189 h
->verinfo
.vertree
= t
;
4193 /* We could not find the version for a symbol when
4194 generating a shared archive. Return an error. */
4195 (*_bfd_error_handler
)
4196 (_("%s: undefined versioned symbol name %s"),
4197 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
4198 bfd_set_error (bfd_error_bad_value
);
4199 sinfo
->failed
= true;
4204 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
4207 /* If we don't have a version for this symbol, see if we can find
4209 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
4211 struct bfd_elf_version_tree
*t
;
4212 struct bfd_elf_version_tree
*deflt
;
4213 struct bfd_elf_version_expr
*d
;
4215 /* See if can find what version this symbol is in. If the
4216 symbol is supposed to be local, then don't actually register
4219 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
4221 if (t
->globals
!= NULL
)
4223 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
4225 if ((*d
->match
) (d
, h
->root
.root
.string
))
4227 h
->verinfo
.vertree
= t
;
4236 if (t
->locals
!= NULL
)
4238 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
4240 if (d
->pattern
[0] == '*' && d
->pattern
[1] == '\0')
4242 else if ((*d
->match
) (d
, h
->root
.root
.string
))
4244 h
->verinfo
.vertree
= t
;
4245 if (h
->dynindx
!= -1
4247 && ! info
->export_dynamic
)
4249 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
4250 (*bed
->elf_backend_hide_symbol
) (info
, h
);
4251 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
4263 if (deflt
!= NULL
&& h
->verinfo
.vertree
== NULL
)
4265 h
->verinfo
.vertree
= deflt
;
4266 if (h
->dynindx
!= -1
4268 && ! info
->export_dynamic
)
4270 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
4271 (*bed
->elf_backend_hide_symbol
) (info
, h
);
4272 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
4281 /* Final phase of ELF linker. */
4283 /* A structure we use to avoid passing large numbers of arguments. */
4285 struct elf_final_link_info
4287 /* General link information. */
4288 struct bfd_link_info
*info
;
4291 /* Symbol string table. */
4292 struct bfd_strtab_hash
*symstrtab
;
4293 /* .dynsym section. */
4294 asection
*dynsym_sec
;
4295 /* .hash section. */
4297 /* symbol version section (.gnu.version). */
4298 asection
*symver_sec
;
4299 /* Buffer large enough to hold contents of any section. */
4301 /* Buffer large enough to hold external relocs of any section. */
4302 PTR external_relocs
;
4303 /* Buffer large enough to hold internal relocs of any section. */
4304 Elf_Internal_Rela
*internal_relocs
;
4305 /* Buffer large enough to hold external local symbols of any input
4307 Elf_External_Sym
*external_syms
;
4308 /* Buffer large enough to hold internal local symbols of any input
4310 Elf_Internal_Sym
*internal_syms
;
4311 /* Array large enough to hold a symbol index for each local symbol
4312 of any input BFD. */
4314 /* Array large enough to hold a section pointer for each local
4315 symbol of any input BFD. */
4316 asection
**sections
;
4317 /* Buffer to hold swapped out symbols. */
4318 Elf_External_Sym
*symbuf
;
4319 /* Number of swapped out symbols in buffer. */
4320 size_t symbuf_count
;
4321 /* Number of symbols which fit in symbuf. */
4325 static boolean elf_link_output_sym
4326 PARAMS ((struct elf_final_link_info
*, const char *,
4327 Elf_Internal_Sym
*, asection
*));
4328 static boolean elf_link_flush_output_syms
4329 PARAMS ((struct elf_final_link_info
*));
4330 static boolean elf_link_output_extsym
4331 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4332 static boolean elf_link_sec_merge_syms
4333 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4334 static boolean elf_link_input_bfd
4335 PARAMS ((struct elf_final_link_info
*, bfd
*));
4336 static boolean elf_reloc_link_order
4337 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
4338 struct bfd_link_order
*));
4340 /* This struct is used to pass information to elf_link_output_extsym. */
4342 struct elf_outext_info
4346 struct elf_final_link_info
*finfo
;
4349 /* Compute the size of, and allocate space for, REL_HDR which is the
4350 section header for a section containing relocations for O. */
4353 elf_link_size_reloc_section (abfd
, rel_hdr
, o
)
4355 Elf_Internal_Shdr
*rel_hdr
;
4358 bfd_size_type reloc_count
;
4359 bfd_size_type num_rel_hashes
;
4361 /* Figure out how many relocations there will be. */
4362 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
4363 reloc_count
= elf_section_data (o
)->rel_count
;
4365 reloc_count
= elf_section_data (o
)->rel_count2
;
4367 num_rel_hashes
= o
->reloc_count
;
4368 if (num_rel_hashes
< reloc_count
)
4369 num_rel_hashes
= reloc_count
;
4371 /* That allows us to calculate the size of the section. */
4372 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
4374 /* The contents field must last into write_object_contents, so we
4375 allocate it with bfd_alloc rather than malloc. Also since we
4376 cannot be sure that the contents will actually be filled in,
4377 we zero the allocated space. */
4378 rel_hdr
->contents
= (PTR
) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
4379 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
4382 /* We only allocate one set of hash entries, so we only do it the
4383 first time we are called. */
4384 if (elf_section_data (o
)->rel_hashes
== NULL
4387 struct elf_link_hash_entry
**p
;
4389 p
= ((struct elf_link_hash_entry
**)
4390 bfd_zmalloc (num_rel_hashes
4391 * sizeof (struct elf_link_hash_entry
*)));
4395 elf_section_data (o
)->rel_hashes
= p
;
4401 /* When performing a relocateable link, the input relocations are
4402 preserved. But, if they reference global symbols, the indices
4403 referenced must be updated. Update all the relocations in
4404 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4407 elf_link_adjust_relocs (abfd
, rel_hdr
, count
, rel_hash
)
4409 Elf_Internal_Shdr
*rel_hdr
;
4411 struct elf_link_hash_entry
**rel_hash
;
4414 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4415 Elf_Internal_Rel
*irel
;
4416 Elf_Internal_Rela
*irela
;
4417 bfd_size_type amt
= sizeof (Elf_Internal_Rel
) * bed
->s
->int_rels_per_ext_rel
;
4419 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (amt
);
4422 (*_bfd_error_handler
) (_("Error: out of memory"));
4426 amt
= sizeof (Elf_Internal_Rela
) * bed
->s
->int_rels_per_ext_rel
;
4427 irela
= (Elf_Internal_Rela
*) bfd_zmalloc (amt
);
4430 (*_bfd_error_handler
) (_("Error: out of memory"));
4434 for (i
= 0; i
< count
; i
++, rel_hash
++)
4436 if (*rel_hash
== NULL
)
4439 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
4441 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4443 Elf_External_Rel
*erel
;
4446 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
4447 if (bed
->s
->swap_reloc_in
)
4448 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
4450 elf_swap_reloc_in (abfd
, erel
, irel
);
4452 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4453 irel
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4454 ELF_R_TYPE (irel
[j
].r_info
));
4456 if (bed
->s
->swap_reloc_out
)
4457 (*bed
->s
->swap_reloc_out
) (abfd
, irel
, (bfd_byte
*) erel
);
4459 elf_swap_reloc_out (abfd
, irel
, erel
);
4463 Elf_External_Rela
*erela
;
4466 BFD_ASSERT (rel_hdr
->sh_entsize
4467 == sizeof (Elf_External_Rela
));
4469 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
4470 if (bed
->s
->swap_reloca_in
)
4471 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
4473 elf_swap_reloca_in (abfd
, erela
, irela
);
4475 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
4476 irela
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4477 ELF_R_TYPE (irela
[j
].r_info
));
4479 if (bed
->s
->swap_reloca_out
)
4480 (*bed
->s
->swap_reloca_out
) (abfd
, irela
, (bfd_byte
*) erela
);
4482 elf_swap_reloca_out (abfd
, irela
, erela
);
4490 struct elf_link_sort_rela
{
4492 enum elf_reloc_type_class type
;
4494 Elf_Internal_Rel rel
;
4495 Elf_Internal_Rela rela
;
4500 elf_link_sort_cmp1 (A
, B
)
4504 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4505 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4506 int relativea
, relativeb
;
4508 relativea
= a
->type
== reloc_class_relative
;
4509 relativeb
= b
->type
== reloc_class_relative
;
4511 if (relativea
< relativeb
)
4513 if (relativea
> relativeb
)
4515 if (ELF_R_SYM (a
->u
.rel
.r_info
) < ELF_R_SYM (b
->u
.rel
.r_info
))
4517 if (ELF_R_SYM (a
->u
.rel
.r_info
) > ELF_R_SYM (b
->u
.rel
.r_info
))
4519 if (a
->u
.rel
.r_offset
< b
->u
.rel
.r_offset
)
4521 if (a
->u
.rel
.r_offset
> b
->u
.rel
.r_offset
)
4527 elf_link_sort_cmp2 (A
, B
)
4531 struct elf_link_sort_rela
*a
= (struct elf_link_sort_rela
*) A
;
4532 struct elf_link_sort_rela
*b
= (struct elf_link_sort_rela
*) B
;
4535 if (a
->offset
< b
->offset
)
4537 if (a
->offset
> b
->offset
)
4539 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
4540 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
4545 if (a
->u
.rel
.r_offset
< b
->u
.rel
.r_offset
)
4547 if (a
->u
.rel
.r_offset
> b
->u
.rel
.r_offset
)
4553 elf_link_sort_relocs (abfd
, info
, psec
)
4555 struct bfd_link_info
*info
;
4558 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4559 asection
*reldyn
, *o
;
4560 boolean rel
= false;
4561 bfd_size_type count
, size
;
4563 struct elf_link_sort_rela
*rela
;
4564 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4566 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
4567 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4569 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
4570 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
4573 count
= reldyn
->_raw_size
/ sizeof (Elf_External_Rel
);
4576 count
= reldyn
->_raw_size
/ sizeof (Elf_External_Rela
);
4579 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4580 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4581 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4582 && o
->output_section
== reldyn
)
4583 size
+= o
->_raw_size
;
4585 if (size
!= reldyn
->_raw_size
)
4588 rela
= (struct elf_link_sort_rela
*) bfd_zmalloc (sizeof (*rela
) * count
);
4591 (*info
->callbacks
->warning
)
4592 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0,
4597 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4598 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4599 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4600 && o
->output_section
== reldyn
)
4604 Elf_External_Rel
*erel
, *erelend
;
4605 struct elf_link_sort_rela
*s
;
4607 erel
= (Elf_External_Rel
*) o
->contents
;
4608 erelend
= (Elf_External_Rel
*) (o
->contents
+ o
->_raw_size
);
4609 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rel
);
4610 for (; erel
< erelend
; erel
++, s
++)
4612 if (bed
->s
->swap_reloc_in
)
4613 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, &s
->u
.rel
);
4615 elf_swap_reloc_in (abfd
, erel
, &s
->u
.rel
);
4617 s
->type
= (*bed
->elf_backend_reloc_type_class
) (&s
->u
.rela
);
4622 Elf_External_Rela
*erela
, *erelaend
;
4623 struct elf_link_sort_rela
*s
;
4625 erela
= (Elf_External_Rela
*) o
->contents
;
4626 erelaend
= (Elf_External_Rela
*) (o
->contents
+ o
->_raw_size
);
4627 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rela
);
4628 for (; erela
< erelaend
; erela
++, s
++)
4630 if (bed
->s
->swap_reloca_in
)
4631 (*bed
->s
->swap_reloca_in
) (dynobj
, (bfd_byte
*) erela
,
4634 elf_swap_reloca_in (dynobj
, erela
, &s
->u
.rela
);
4636 s
->type
= (*bed
->elf_backend_reloc_type_class
) (&s
->u
.rela
);
4641 qsort (rela
, (size_t) count
, sizeof (*rela
), elf_link_sort_cmp1
);
4642 for (ret
= 0; ret
< count
&& rela
[ret
].type
== reloc_class_relative
; ret
++)
4644 for (i
= ret
, j
= ret
; i
< count
; i
++)
4646 if (ELF_R_SYM (rela
[i
].u
.rel
.r_info
) != ELF_R_SYM (rela
[j
].u
.rel
.r_info
))
4648 rela
[i
].offset
= rela
[j
].u
.rel
.r_offset
;
4650 qsort (rela
+ ret
, (size_t) count
- ret
, sizeof (*rela
), elf_link_sort_cmp2
);
4652 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4653 if ((o
->flags
& (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
))
4654 == (SEC_HAS_CONTENTS
|SEC_LINKER_CREATED
)
4655 && o
->output_section
== reldyn
)
4659 Elf_External_Rel
*erel
, *erelend
;
4660 struct elf_link_sort_rela
*s
;
4662 erel
= (Elf_External_Rel
*) o
->contents
;
4663 erelend
= (Elf_External_Rel
*) (o
->contents
+ o
->_raw_size
);
4664 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rel
);
4665 for (; erel
< erelend
; erel
++, s
++)
4667 if (bed
->s
->swap_reloc_out
)
4668 (*bed
->s
->swap_reloc_out
) (abfd
, &s
->u
.rel
,
4671 elf_swap_reloc_out (abfd
, &s
->u
.rel
, erel
);
4676 Elf_External_Rela
*erela
, *erelaend
;
4677 struct elf_link_sort_rela
*s
;
4679 erela
= (Elf_External_Rela
*) o
->contents
;
4680 erelaend
= (Elf_External_Rela
*) (o
->contents
+ o
->_raw_size
);
4681 s
= rela
+ o
->output_offset
/ sizeof (Elf_External_Rela
);
4682 for (; erela
< erelaend
; erela
++, s
++)
4684 if (bed
->s
->swap_reloca_out
)
4685 (*bed
->s
->swap_reloca_out
) (dynobj
, &s
->u
.rela
,
4686 (bfd_byte
*) erela
);
4688 elf_swap_reloca_out (dynobj
, &s
->u
.rela
, erela
);
4698 /* Do the final step of an ELF link. */
4701 elf_bfd_final_link (abfd
, info
)
4703 struct bfd_link_info
*info
;
4706 boolean emit_relocs
;
4708 struct elf_final_link_info finfo
;
4709 register asection
*o
;
4710 register struct bfd_link_order
*p
;
4712 bfd_size_type max_contents_size
;
4713 bfd_size_type max_external_reloc_size
;
4714 bfd_size_type max_internal_reloc_count
;
4715 bfd_size_type max_sym_count
;
4717 Elf_Internal_Sym elfsym
;
4719 Elf_Internal_Shdr
*symtab_hdr
;
4720 Elf_Internal_Shdr
*symstrtab_hdr
;
4721 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4722 struct elf_outext_info eoinfo
;
4724 size_t relativecount
= 0;
4725 asection
*reldyn
= 0;
4728 if (! is_elf_hash_table (info
))
4732 abfd
->flags
|= DYNAMIC
;
4734 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
4735 dynobj
= elf_hash_table (info
)->dynobj
;
4737 emit_relocs
= (info
->relocateable
4738 || info
->emitrelocations
4739 || bed
->elf_backend_emit_relocs
);
4742 finfo
.output_bfd
= abfd
;
4743 finfo
.symstrtab
= elf_stringtab_init ();
4744 if (finfo
.symstrtab
== NULL
)
4749 finfo
.dynsym_sec
= NULL
;
4750 finfo
.hash_sec
= NULL
;
4751 finfo
.symver_sec
= NULL
;
4755 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
4756 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
4757 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
4758 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
4759 /* Note that it is OK if symver_sec is NULL. */
4762 finfo
.contents
= NULL
;
4763 finfo
.external_relocs
= NULL
;
4764 finfo
.internal_relocs
= NULL
;
4765 finfo
.external_syms
= NULL
;
4766 finfo
.internal_syms
= NULL
;
4767 finfo
.indices
= NULL
;
4768 finfo
.sections
= NULL
;
4769 finfo
.symbuf
= NULL
;
4770 finfo
.symbuf_count
= 0;
4772 /* Count up the number of relocations we will output for each output
4773 section, so that we know the sizes of the reloc sections. We
4774 also figure out some maximum sizes. */
4775 max_contents_size
= 0;
4776 max_external_reloc_size
= 0;
4777 max_internal_reloc_count
= 0;
4780 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
4784 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
4786 if (p
->type
== bfd_section_reloc_link_order
4787 || p
->type
== bfd_symbol_reloc_link_order
)
4789 else if (p
->type
== bfd_indirect_link_order
)
4793 sec
= p
->u
.indirect
.section
;
4795 /* Mark all sections which are to be included in the
4796 link. This will normally be every section. We need
4797 to do this so that we can identify any sections which
4798 the linker has decided to not include. */
4799 sec
->linker_mark
= true;
4801 if (sec
->flags
& SEC_MERGE
)
4804 if (info
->relocateable
|| info
->emitrelocations
)
4805 o
->reloc_count
+= sec
->reloc_count
;
4806 else if (bed
->elf_backend_count_relocs
)
4808 Elf_Internal_Rela
* relocs
;
4810 relocs
= (NAME(_bfd_elf
,link_read_relocs
)
4811 (abfd
, sec
, (PTR
) NULL
,
4812 (Elf_Internal_Rela
*) NULL
, info
->keep_memory
));
4814 o
->reloc_count
+= (*bed
->elf_backend_count_relocs
)
4817 if (!info
->keep_memory
)
4821 if (sec
->_raw_size
> max_contents_size
)
4822 max_contents_size
= sec
->_raw_size
;
4823 if (sec
->_cooked_size
> max_contents_size
)
4824 max_contents_size
= sec
->_cooked_size
;
4826 /* We are interested in just local symbols, not all
4828 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
4829 && (sec
->owner
->flags
& DYNAMIC
) == 0)
4833 if (elf_bad_symtab (sec
->owner
))
4834 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
4835 / sizeof (Elf_External_Sym
));
4837 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
4839 if (sym_count
> max_sym_count
)
4840 max_sym_count
= sym_count
;
4842 if ((sec
->flags
& SEC_RELOC
) != 0)
4846 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
4847 if (ext_size
> max_external_reloc_size
)
4848 max_external_reloc_size
= ext_size
;
4849 if (sec
->reloc_count
> max_internal_reloc_count
)
4850 max_internal_reloc_count
= sec
->reloc_count
;
4856 if (o
->reloc_count
> 0)
4857 o
->flags
|= SEC_RELOC
;
4860 /* Explicitly clear the SEC_RELOC flag. The linker tends to
4861 set it (this is probably a bug) and if it is set
4862 assign_section_numbers will create a reloc section. */
4863 o
->flags
&=~ SEC_RELOC
;
4866 /* If the SEC_ALLOC flag is not set, force the section VMA to
4867 zero. This is done in elf_fake_sections as well, but forcing
4868 the VMA to 0 here will ensure that relocs against these
4869 sections are handled correctly. */
4870 if ((o
->flags
& SEC_ALLOC
) == 0
4871 && ! o
->user_set_vma
)
4875 if (! info
->relocateable
&& merged
)
4876 elf_link_hash_traverse (elf_hash_table (info
),
4877 elf_link_sec_merge_syms
, (PTR
) abfd
);
4879 /* Figure out the file positions for everything but the symbol table
4880 and the relocs. We set symcount to force assign_section_numbers
4881 to create a symbol table. */
4882 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
4883 BFD_ASSERT (! abfd
->output_has_begun
);
4884 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
4887 /* Figure out how many relocations we will have in each section.
4888 Just using RELOC_COUNT isn't good enough since that doesn't
4889 maintain a separate value for REL vs. RELA relocations. */
4891 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
4892 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
4894 asection
*output_section
;
4896 if (! o
->linker_mark
)
4898 /* This section was omitted from the link. */
4902 output_section
= o
->output_section
;
4904 if (output_section
!= NULL
4905 && (o
->flags
& SEC_RELOC
) != 0)
4907 struct bfd_elf_section_data
*esdi
4908 = elf_section_data (o
);
4909 struct bfd_elf_section_data
*esdo
4910 = elf_section_data (output_section
);
4911 unsigned int *rel_count
;
4912 unsigned int *rel_count2
;
4914 /* We must be careful to add the relocation froms the
4915 input section to the right output count. */
4916 if (esdi
->rel_hdr
.sh_entsize
== esdo
->rel_hdr
.sh_entsize
)
4918 rel_count
= &esdo
->rel_count
;
4919 rel_count2
= &esdo
->rel_count2
;
4923 rel_count
= &esdo
->rel_count2
;
4924 rel_count2
= &esdo
->rel_count
;
4927 *rel_count
+= NUM_SHDR_ENTRIES (& esdi
->rel_hdr
);
4929 *rel_count2
+= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
4930 output_section
->flags
|= SEC_RELOC
;
4934 /* That created the reloc sections. Set their sizes, and assign
4935 them file positions, and allocate some buffers. */
4936 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4938 if ((o
->flags
& SEC_RELOC
) != 0)
4940 if (!elf_link_size_reloc_section (abfd
,
4941 &elf_section_data (o
)->rel_hdr
,
4945 if (elf_section_data (o
)->rel_hdr2
4946 && !elf_link_size_reloc_section (abfd
,
4947 elf_section_data (o
)->rel_hdr2
,
4952 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
4953 to count upwards while actually outputting the relocations. */
4954 elf_section_data (o
)->rel_count
= 0;
4955 elf_section_data (o
)->rel_count2
= 0;
4958 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4960 /* We have now assigned file positions for all the sections except
4961 .symtab and .strtab. We start the .symtab section at the current
4962 file position, and write directly to it. We build the .strtab
4963 section in memory. */
4964 bfd_get_symcount (abfd
) = 0;
4965 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4966 /* sh_name is set in prep_headers. */
4967 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4968 symtab_hdr
->sh_flags
= 0;
4969 symtab_hdr
->sh_addr
= 0;
4970 symtab_hdr
->sh_size
= 0;
4971 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
4972 /* sh_link is set in assign_section_numbers. */
4973 /* sh_info is set below. */
4974 /* sh_offset is set just below. */
4975 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
4977 off
= elf_tdata (abfd
)->next_file_pos
;
4978 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
4980 /* Note that at this point elf_tdata (abfd)->next_file_pos is
4981 incorrect. We do not yet know the size of the .symtab section.
4982 We correct next_file_pos below, after we do know the size. */
4984 /* Allocate a buffer to hold swapped out symbols. This is to avoid
4985 continuously seeking to the right position in the file. */
4986 if (! info
->keep_memory
|| max_sym_count
< 20)
4987 finfo
.symbuf_size
= 20;
4989 finfo
.symbuf_size
= max_sym_count
;
4990 amt
= finfo
.symbuf_size
;
4991 amt
*= sizeof (Elf_External_Sym
);
4992 finfo
.symbuf
= (Elf_External_Sym
*) bfd_malloc (amt
);
4993 if (finfo
.symbuf
== NULL
)
4996 /* Start writing out the symbol table. The first symbol is always a
4998 if (info
->strip
!= strip_all
5001 elfsym
.st_value
= 0;
5004 elfsym
.st_other
= 0;
5005 elfsym
.st_shndx
= SHN_UNDEF
;
5006 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5007 &elfsym
, bfd_und_section_ptr
))
5012 /* Some standard ELF linkers do this, but we don't because it causes
5013 bootstrap comparison failures. */
5014 /* Output a file symbol for the output file as the second symbol.
5015 We output this even if we are discarding local symbols, although
5016 I'm not sure if this is correct. */
5017 elfsym
.st_value
= 0;
5019 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5020 elfsym
.st_other
= 0;
5021 elfsym
.st_shndx
= SHN_ABS
;
5022 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
5023 &elfsym
, bfd_abs_section_ptr
))
5027 /* Output a symbol for each section. We output these even if we are
5028 discarding local symbols, since they are used for relocs. These
5029 symbols have no names. We store the index of each one in the
5030 index field of the section, so that we can find it again when
5031 outputting relocs. */
5032 if (info
->strip
!= strip_all
5036 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5037 elfsym
.st_other
= 0;
5038 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
5040 o
= section_from_elf_index (abfd
, i
);
5042 o
->target_index
= bfd_get_symcount (abfd
);
5043 elfsym
.st_shndx
= i
;
5044 if (info
->relocateable
|| o
== NULL
)
5045 elfsym
.st_value
= 0;
5047 elfsym
.st_value
= o
->vma
;
5048 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
5054 /* Allocate some memory to hold information read in from the input
5056 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
5057 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
5058 finfo
.internal_relocs
= ((Elf_Internal_Rela
*)
5059 bfd_malloc (max_internal_reloc_count
5060 * sizeof (Elf_Internal_Rela
)
5061 * bed
->s
->int_rels_per_ext_rel
));
5062 finfo
.external_syms
= ((Elf_External_Sym
*)
5063 bfd_malloc (max_sym_count
5064 * sizeof (Elf_External_Sym
)));
5065 finfo
.internal_syms
= ((Elf_Internal_Sym
*)
5066 bfd_malloc (max_sym_count
5067 * sizeof (Elf_Internal_Sym
)));
5068 finfo
.indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
5069 finfo
.sections
= ((asection
**)
5070 bfd_malloc (max_sym_count
* sizeof (asection
*)));
5071 if ((finfo
.contents
== NULL
&& max_contents_size
!= 0)
5072 || (finfo
.external_relocs
== NULL
&& max_external_reloc_size
!= 0)
5073 || (finfo
.internal_relocs
== NULL
&& max_internal_reloc_count
!= 0)
5074 || (finfo
.external_syms
== NULL
&& max_sym_count
!= 0)
5075 || (finfo
.internal_syms
== NULL
&& max_sym_count
!= 0)
5076 || (finfo
.indices
== NULL
&& max_sym_count
!= 0)
5077 || (finfo
.sections
== NULL
&& max_sym_count
!= 0))
5080 /* Since ELF permits relocations to be against local symbols, we
5081 must have the local symbols available when we do the relocations.
5082 Since we would rather only read the local symbols once, and we
5083 would rather not keep them in memory, we handle all the
5084 relocations for a single input file at the same time.
5086 Unfortunately, there is no way to know the total number of local
5087 symbols until we have seen all of them, and the local symbol
5088 indices precede the global symbol indices. This means that when
5089 we are generating relocateable output, and we see a reloc against
5090 a global symbol, we can not know the symbol index until we have
5091 finished examining all the local symbols to see which ones we are
5092 going to output. To deal with this, we keep the relocations in
5093 memory, and don't output them until the end of the link. This is
5094 an unfortunate waste of memory, but I don't see a good way around
5095 it. Fortunately, it only happens when performing a relocateable
5096 link, which is not the common case. FIXME: If keep_memory is set
5097 we could write the relocs out and then read them again; I don't
5098 know how bad the memory loss will be. */
5100 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5101 sub
->output_has_begun
= false;
5102 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5104 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5106 if (p
->type
== bfd_indirect_link_order
5107 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
5108 == bfd_target_elf_flavour
))
5110 sub
= p
->u
.indirect
.section
->owner
;
5111 if (! sub
->output_has_begun
)
5113 if (! elf_link_input_bfd (&finfo
, sub
))
5115 sub
->output_has_begun
= true;
5118 else if (p
->type
== bfd_section_reloc_link_order
5119 || p
->type
== bfd_symbol_reloc_link_order
)
5121 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
5126 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
5132 /* That wrote out all the local symbols. Finish up the symbol table
5133 with the global symbols. Even if we want to strip everything we
5134 can, we still need to deal with those global symbols that got
5135 converted to local in a version script. */
5139 /* Output any global symbols that got converted to local in a
5140 version script. We do this in a separate step since ELF
5141 requires all local symbols to appear prior to any global
5142 symbols. FIXME: We should only do this if some global
5143 symbols were, in fact, converted to become local. FIXME:
5144 Will this work correctly with the Irix 5 linker? */
5145 eoinfo
.failed
= false;
5146 eoinfo
.finfo
= &finfo
;
5147 eoinfo
.localsyms
= true;
5148 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5154 /* The sh_info field records the index of the first non local symbol. */
5155 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
5158 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
5160 Elf_Internal_Sym sym
;
5161 Elf_External_Sym
*dynsym
=
5162 (Elf_External_Sym
*) finfo
.dynsym_sec
->contents
;
5163 long last_local
= 0;
5165 /* Write out the section symbols for the output sections. */
5172 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5175 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5178 indx
= elf_section_data (s
)->this_idx
;
5179 BFD_ASSERT (indx
> 0);
5180 sym
.st_shndx
= indx
;
5181 sym
.st_value
= s
->vma
;
5183 elf_swap_symbol_out (abfd
, &sym
,
5184 dynsym
+ elf_section_data (s
)->dynindx
);
5187 last_local
= bfd_count_sections (abfd
);
5190 /* Write out the local dynsyms. */
5191 if (elf_hash_table (info
)->dynlocal
)
5193 struct elf_link_local_dynamic_entry
*e
;
5194 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
5198 sym
.st_size
= e
->isym
.st_size
;
5199 sym
.st_other
= e
->isym
.st_other
;
5201 /* Copy the internal symbol as is.
5202 Note that we saved a word of storage and overwrote
5203 the original st_name with the dynstr_index. */
5206 if (e
->isym
.st_shndx
> 0 && e
->isym
.st_shndx
< SHN_LORESERVE
)
5208 s
= bfd_section_from_elf_index (e
->input_bfd
,
5212 elf_section_data (s
->output_section
)->this_idx
;
5213 sym
.st_value
= (s
->output_section
->vma
5215 + e
->isym
.st_value
);
5218 if (last_local
< e
->dynindx
)
5219 last_local
= e
->dynindx
;
5221 elf_swap_symbol_out (abfd
, &sym
, dynsym
+ e
->dynindx
);
5225 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
5229 /* We get the global symbols from the hash table. */
5230 eoinfo
.failed
= false;
5231 eoinfo
.localsyms
= false;
5232 eoinfo
.finfo
= &finfo
;
5233 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5238 /* If backend needs to output some symbols not present in the hash
5239 table, do it now. */
5240 if (bed
->elf_backend_output_arch_syms
)
5242 typedef boolean (*out_sym_func
) PARAMS ((PTR
, const char *,
5246 if (! ((*bed
->elf_backend_output_arch_syms
)
5247 (abfd
, info
, (PTR
) &finfo
, (out_sym_func
) elf_link_output_sym
)))
5251 /* Flush all symbols to the file. */
5252 if (! elf_link_flush_output_syms (&finfo
))
5255 /* Now we know the size of the symtab section. */
5256 off
+= symtab_hdr
->sh_size
;
5258 /* Finish up and write out the symbol string table (.strtab)
5260 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5261 /* sh_name was set in prep_headers. */
5262 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5263 symstrtab_hdr
->sh_flags
= 0;
5264 symstrtab_hdr
->sh_addr
= 0;
5265 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
5266 symstrtab_hdr
->sh_entsize
= 0;
5267 symstrtab_hdr
->sh_link
= 0;
5268 symstrtab_hdr
->sh_info
= 0;
5269 /* sh_offset is set just below. */
5270 symstrtab_hdr
->sh_addralign
= 1;
5272 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, true);
5273 elf_tdata (abfd
)->next_file_pos
= off
;
5275 if (bfd_get_symcount (abfd
) > 0)
5277 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
5278 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
5282 /* Adjust the relocs to have the correct symbol indices. */
5283 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5285 if ((o
->flags
& SEC_RELOC
) == 0)
5288 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
5289 elf_section_data (o
)->rel_count
,
5290 elf_section_data (o
)->rel_hashes
);
5291 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
5292 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
5293 elf_section_data (o
)->rel_count2
,
5294 (elf_section_data (o
)->rel_hashes
5295 + elf_section_data (o
)->rel_count
));
5297 /* Set the reloc_count field to 0 to prevent write_relocs from
5298 trying to swap the relocs out itself. */
5302 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
5303 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
5305 /* If we are linking against a dynamic object, or generating a
5306 shared library, finish up the dynamic linking information. */
5309 Elf_External_Dyn
*dyncon
, *dynconend
;
5311 /* Fix up .dynamic entries. */
5312 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
5313 BFD_ASSERT (o
!= NULL
);
5315 dyncon
= (Elf_External_Dyn
*) o
->contents
;
5316 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
5317 for (; dyncon
< dynconend
; dyncon
++)
5319 Elf_Internal_Dyn dyn
;
5323 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
5330 if (relativecount
> 0 && dyncon
+ 1 < dynconend
)
5332 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
5334 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
5335 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
5338 if (dyn
.d_tag
!= DT_NULL
)
5340 dyn
.d_un
.d_val
= relativecount
;
5341 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5347 name
= info
->init_function
;
5350 name
= info
->fini_function
;
5353 struct elf_link_hash_entry
*h
;
5355 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
5356 false, false, true);
5358 && (h
->root
.type
== bfd_link_hash_defined
5359 || h
->root
.type
== bfd_link_hash_defweak
))
5361 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
5362 o
= h
->root
.u
.def
.section
;
5363 if (o
->output_section
!= NULL
)
5364 dyn
.d_un
.d_val
+= (o
->output_section
->vma
5365 + o
->output_offset
);
5368 /* The symbol is imported from another shared
5369 library and does not apply to this one. */
5373 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5388 name
= ".gnu.version_d";
5391 name
= ".gnu.version_r";
5394 name
= ".gnu.version";
5396 o
= bfd_get_section_by_name (abfd
, name
);
5397 BFD_ASSERT (o
!= NULL
);
5398 dyn
.d_un
.d_ptr
= o
->vma
;
5399 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5406 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
5411 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
5413 Elf_Internal_Shdr
*hdr
;
5415 hdr
= elf_elfsections (abfd
)[i
];
5416 if (hdr
->sh_type
== type
5417 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
5419 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
5420 dyn
.d_un
.d_val
+= hdr
->sh_size
;
5423 if (dyn
.d_un
.d_val
== 0
5424 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
5425 dyn
.d_un
.d_val
= hdr
->sh_addr
;
5429 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5435 /* If we have created any dynamic sections, then output them. */
5438 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
5441 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
5443 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5444 || o
->_raw_size
== 0
5445 || o
->output_section
== bfd_abs_section_ptr
)
5447 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
5449 /* At this point, we are only interested in sections
5450 created by elf_link_create_dynamic_sections. */
5453 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
5455 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
5457 if (! bfd_set_section_contents (abfd
, o
->output_section
,
5459 (file_ptr
) o
->output_offset
,
5465 /* The contents of the .dynstr section are actually in a
5467 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
5468 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
5469 || ! _bfd_elf_strtab_emit (abfd
,
5470 elf_hash_table (info
)->dynstr
))
5476 /* If we have optimized stabs strings, output them. */
5477 if (elf_hash_table (info
)->stab_info
!= NULL
)
5479 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
5483 if (finfo
.symstrtab
!= NULL
)
5484 _bfd_stringtab_free (finfo
.symstrtab
);
5485 if (finfo
.contents
!= NULL
)
5486 free (finfo
.contents
);
5487 if (finfo
.external_relocs
!= NULL
)
5488 free (finfo
.external_relocs
);
5489 if (finfo
.internal_relocs
!= NULL
)
5490 free (finfo
.internal_relocs
);
5491 if (finfo
.external_syms
!= NULL
)
5492 free (finfo
.external_syms
);
5493 if (finfo
.internal_syms
!= NULL
)
5494 free (finfo
.internal_syms
);
5495 if (finfo
.indices
!= NULL
)
5496 free (finfo
.indices
);
5497 if (finfo
.sections
!= NULL
)
5498 free (finfo
.sections
);
5499 if (finfo
.symbuf
!= NULL
)
5500 free (finfo
.symbuf
);
5501 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5503 if ((o
->flags
& SEC_RELOC
) != 0
5504 && elf_section_data (o
)->rel_hashes
!= NULL
)
5505 free (elf_section_data (o
)->rel_hashes
);
5508 elf_tdata (abfd
)->linker
= true;
5513 if (finfo
.symstrtab
!= NULL
)
5514 _bfd_stringtab_free (finfo
.symstrtab
);
5515 if (finfo
.contents
!= NULL
)
5516 free (finfo
.contents
);
5517 if (finfo
.external_relocs
!= NULL
)
5518 free (finfo
.external_relocs
);
5519 if (finfo
.internal_relocs
!= NULL
)
5520 free (finfo
.internal_relocs
);
5521 if (finfo
.external_syms
!= NULL
)
5522 free (finfo
.external_syms
);
5523 if (finfo
.internal_syms
!= NULL
)
5524 free (finfo
.internal_syms
);
5525 if (finfo
.indices
!= NULL
)
5526 free (finfo
.indices
);
5527 if (finfo
.sections
!= NULL
)
5528 free (finfo
.sections
);
5529 if (finfo
.symbuf
!= NULL
)
5530 free (finfo
.symbuf
);
5531 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5533 if ((o
->flags
& SEC_RELOC
) != 0
5534 && elf_section_data (o
)->rel_hashes
!= NULL
)
5535 free (elf_section_data (o
)->rel_hashes
);
5541 /* Add a symbol to the output symbol table. */
5544 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
5545 struct elf_final_link_info
*finfo
;
5547 Elf_Internal_Sym
*elfsym
;
5548 asection
*input_sec
;
5550 boolean (*output_symbol_hook
) PARAMS ((bfd
*,
5551 struct bfd_link_info
*info
,
5556 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
5557 elf_backend_link_output_symbol_hook
;
5558 if (output_symbol_hook
!= NULL
)
5560 if (! ((*output_symbol_hook
)
5561 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
5565 if (name
== (const char *) NULL
|| *name
== '\0')
5566 elfsym
->st_name
= 0;
5567 else if (input_sec
->flags
& SEC_EXCLUDE
)
5568 elfsym
->st_name
= 0;
5571 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5573 if (elfsym
->st_name
== (unsigned long) -1)
5577 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5579 if (! elf_link_flush_output_syms (finfo
))
5583 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
,
5584 (PTR
) (finfo
->symbuf
+ finfo
->symbuf_count
));
5585 ++finfo
->symbuf_count
;
5587 ++ bfd_get_symcount (finfo
->output_bfd
);
5592 /* Flush the output symbols to the file. */
5595 elf_link_flush_output_syms (finfo
)
5596 struct elf_final_link_info
*finfo
;
5598 if (finfo
->symbuf_count
> 0)
5600 Elf_Internal_Shdr
*symtab
;
5604 symtab
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5605 pos
= symtab
->sh_offset
+ symtab
->sh_size
;
5606 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
5607 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5608 || bfd_bwrite ((PTR
) finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5611 symtab
->sh_size
+= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
5613 finfo
->symbuf_count
= 0;
5619 /* Adjust all external symbols pointing into SEC_MERGE sections
5620 to reflect the object merging within the sections. */
5623 elf_link_sec_merge_syms (h
, data
)
5624 struct elf_link_hash_entry
*h
;
5629 if ((h
->root
.type
== bfd_link_hash_defined
5630 || h
->root
.type
== bfd_link_hash_defweak
)
5631 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
5632 && elf_section_data (sec
)->merge_info
)
5634 bfd
*output_bfd
= (bfd
*) data
;
5636 h
->root
.u
.def
.value
=
5637 _bfd_merged_section_offset (output_bfd
,
5638 &h
->root
.u
.def
.section
,
5639 elf_section_data (sec
)->merge_info
,
5640 h
->root
.u
.def
.value
, (bfd_vma
) 0);
5646 /* Add an external symbol to the symbol table. This is called from
5647 the hash table traversal routine. When generating a shared object,
5648 we go through the symbol table twice. The first time we output
5649 anything that might have been forced to local scope in a version
5650 script. The second time we output the symbols that are still
5654 elf_link_output_extsym (h
, data
)
5655 struct elf_link_hash_entry
*h
;
5658 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
5659 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
5661 Elf_Internal_Sym sym
;
5662 asection
*input_sec
;
5664 /* Decide whether to output this symbol in this pass. */
5665 if (eoinfo
->localsyms
)
5667 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
5672 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
5676 /* If we are not creating a shared library, and this symbol is
5677 referenced by a shared library but is not defined anywhere, then
5678 warn that it is undefined. If we do not do this, the runtime
5679 linker will complain that the symbol is undefined when the
5680 program is run. We don't have to worry about symbols that are
5681 referenced by regular files, because we will already have issued
5682 warnings for them. */
5683 if (! finfo
->info
->relocateable
5684 && ! finfo
->info
->allow_shlib_undefined
5685 && ! finfo
->info
->shared
5686 && h
->root
.type
== bfd_link_hash_undefined
5687 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
5688 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
5690 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
5691 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
5692 (asection
*) NULL
, (bfd_vma
) 0, true)))
5694 eoinfo
->failed
= true;
5699 /* We don't want to output symbols that have never been mentioned by
5700 a regular file, or that we have been told to strip. However, if
5701 h->indx is set to -2, the symbol is used by a reloc and we must
5705 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
5706 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
5707 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
5708 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
5710 else if (finfo
->info
->strip
== strip_all
5711 || (finfo
->info
->strip
== strip_some
5712 && bfd_hash_lookup (finfo
->info
->keep_hash
,
5713 h
->root
.root
.string
,
5714 false, false) == NULL
))
5719 /* If we're stripping it, and it's not a dynamic symbol, there's
5720 nothing else to do unless it is a forced local symbol. */
5723 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
5727 sym
.st_size
= h
->size
;
5728 sym
.st_other
= h
->other
;
5729 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
5730 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
5731 else if (h
->root
.type
== bfd_link_hash_undefweak
5732 || h
->root
.type
== bfd_link_hash_defweak
)
5733 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
5735 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
5737 switch (h
->root
.type
)
5740 case bfd_link_hash_new
:
5744 case bfd_link_hash_undefined
:
5745 input_sec
= bfd_und_section_ptr
;
5746 sym
.st_shndx
= SHN_UNDEF
;
5749 case bfd_link_hash_undefweak
:
5750 input_sec
= bfd_und_section_ptr
;
5751 sym
.st_shndx
= SHN_UNDEF
;
5754 case bfd_link_hash_defined
:
5755 case bfd_link_hash_defweak
:
5757 input_sec
= h
->root
.u
.def
.section
;
5758 if (input_sec
->output_section
!= NULL
)
5761 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
5762 input_sec
->output_section
);
5763 if (sym
.st_shndx
== (unsigned short) -1)
5765 (*_bfd_error_handler
)
5766 (_("%s: could not find output section %s for input section %s"),
5767 bfd_get_filename (finfo
->output_bfd
),
5768 input_sec
->output_section
->name
,
5770 eoinfo
->failed
= true;
5774 /* ELF symbols in relocateable files are section relative,
5775 but in nonrelocateable files they are virtual
5777 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
5778 if (! finfo
->info
->relocateable
)
5779 sym
.st_value
+= input_sec
->output_section
->vma
;
5783 BFD_ASSERT (input_sec
->owner
== NULL
5784 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
5785 sym
.st_shndx
= SHN_UNDEF
;
5786 input_sec
= bfd_und_section_ptr
;
5791 case bfd_link_hash_common
:
5792 input_sec
= h
->root
.u
.c
.p
->section
;
5793 sym
.st_shndx
= SHN_COMMON
;
5794 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
5797 case bfd_link_hash_indirect
:
5798 /* These symbols are created by symbol versioning. They point
5799 to the decorated version of the name. For example, if the
5800 symbol foo@@GNU_1.2 is the default, which should be used when
5801 foo is used with no version, then we add an indirect symbol
5802 foo which points to foo@@GNU_1.2. We ignore these symbols,
5803 since the indirected symbol is already in the hash table. */
5806 case bfd_link_hash_warning
:
5807 /* We can't represent these symbols in ELF, although a warning
5808 symbol may have come from a .gnu.warning.SYMBOL section. We
5809 just put the target symbol in the hash table. If the target
5810 symbol does not really exist, don't do anything. */
5811 if (h
->root
.u
.i
.link
->type
== bfd_link_hash_new
)
5813 return (elf_link_output_extsym
5814 ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
));
5817 /* Give the processor backend a chance to tweak the symbol value,
5818 and also to finish up anything that needs to be done for this
5820 if ((h
->dynindx
!= -1
5821 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
5822 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
5824 struct elf_backend_data
*bed
;
5826 bed
= get_elf_backend_data (finfo
->output_bfd
);
5827 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
5828 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
5830 eoinfo
->failed
= true;
5835 /* If we are marking the symbol as undefined, and there are no
5836 non-weak references to this symbol from a regular object, then
5837 mark the symbol as weak undefined; if there are non-weak
5838 references, mark the symbol as strong. We can't do this earlier,
5839 because it might not be marked as undefined until the
5840 finish_dynamic_symbol routine gets through with it. */
5841 if (sym
.st_shndx
== SHN_UNDEF
5842 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
5843 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
5844 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
5848 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
5849 bindtype
= STB_GLOBAL
;
5851 bindtype
= STB_WEAK
;
5852 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
5855 /* If a symbol is not defined locally, we clear the visibility
5857 if (! finfo
->info
->relocateable
5858 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
5859 sym
.st_other
^= ELF_ST_VISIBILITY (sym
.st_other
);
5861 /* If this symbol should be put in the .dynsym section, then put it
5862 there now. We have already know the symbol index. We also fill
5863 in the entry in the .hash section. */
5864 if (h
->dynindx
!= -1
5865 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
5869 size_t hash_entry_size
;
5870 bfd_byte
*bucketpos
;
5872 Elf_External_Sym
*esym
;
5874 sym
.st_name
= h
->dynstr_index
;
5875 esym
= (Elf_External_Sym
*) finfo
->dynsym_sec
->contents
+ h
->dynindx
;
5876 elf_swap_symbol_out (finfo
->output_bfd
, &sym
, (PTR
) esym
);
5878 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
5879 bucket
= h
->elf_hash_value
% bucketcount
;
5881 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
5882 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
5883 + (bucket
+ 2) * hash_entry_size
);
5884 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
5885 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, (bfd_vma
) h
->dynindx
,
5887 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
5888 ((bfd_byte
*) finfo
->hash_sec
->contents
5889 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
5891 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
5893 Elf_Internal_Versym iversym
;
5894 Elf_External_Versym
*eversym
;
5896 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
5898 if (h
->verinfo
.verdef
== NULL
)
5899 iversym
.vs_vers
= 0;
5901 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
5905 if (h
->verinfo
.vertree
== NULL
)
5906 iversym
.vs_vers
= 1;
5908 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
5911 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
5912 iversym
.vs_vers
|= VERSYM_HIDDEN
;
5914 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
5915 eversym
+= h
->dynindx
;
5916 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
5920 /* If we're stripping it, then it was just a dynamic symbol, and
5921 there's nothing else to do. */
5925 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
5927 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
5929 eoinfo
->failed
= true;
5936 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
5937 originated from the section given by INPUT_REL_HDR) to the
5941 elf_link_output_relocs (output_bfd
, input_section
, input_rel_hdr
,
5944 asection
*input_section
;
5945 Elf_Internal_Shdr
*input_rel_hdr
;
5946 Elf_Internal_Rela
*internal_relocs
;
5948 Elf_Internal_Rela
*irela
;
5949 Elf_Internal_Rela
*irelaend
;
5950 Elf_Internal_Shdr
*output_rel_hdr
;
5951 asection
*output_section
;
5952 unsigned int *rel_countp
= NULL
;
5953 struct elf_backend_data
*bed
;
5956 output_section
= input_section
->output_section
;
5957 output_rel_hdr
= NULL
;
5959 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
5960 == input_rel_hdr
->sh_entsize
)
5962 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
5963 rel_countp
= &elf_section_data (output_section
)->rel_count
;
5965 else if (elf_section_data (output_section
)->rel_hdr2
5966 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
5967 == input_rel_hdr
->sh_entsize
))
5969 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
5970 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
5973 BFD_ASSERT (output_rel_hdr
!= NULL
);
5975 bed
= get_elf_backend_data (output_bfd
);
5976 irela
= internal_relocs
;
5977 irelaend
= irela
+ NUM_SHDR_ENTRIES (input_rel_hdr
)
5978 * bed
->s
->int_rels_per_ext_rel
;
5980 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
5982 Elf_External_Rel
*erel
;
5983 Elf_Internal_Rel
*irel
;
5985 amt
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
5986 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (amt
);
5989 (*_bfd_error_handler
) (_("Error: out of memory"));
5993 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
+ *rel_countp
);
5994 for (; irela
< irelaend
; irela
+= bed
->s
->int_rels_per_ext_rel
, erel
++)
5998 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
6000 irel
[i
].r_offset
= irela
[i
].r_offset
;
6001 irel
[i
].r_info
= irela
[i
].r_info
;
6002 BFD_ASSERT (irela
[i
].r_addend
== 0);
6005 if (bed
->s
->swap_reloc_out
)
6006 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, (PTR
) erel
);
6008 elf_swap_reloc_out (output_bfd
, irel
, erel
);
6015 Elf_External_Rela
*erela
;
6017 BFD_ASSERT (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
));
6019 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
+ *rel_countp
);
6020 for (; irela
< irelaend
; irela
+= bed
->s
->int_rels_per_ext_rel
, erela
++)
6021 if (bed
->s
->swap_reloca_out
)
6022 (*bed
->s
->swap_reloca_out
) (output_bfd
, irela
, (PTR
) erela
);
6024 elf_swap_reloca_out (output_bfd
, irela
, erela
);
6027 /* Bump the counter, so that we know where to add the next set of
6029 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
6032 /* Link an input file into the linker output file. This function
6033 handles all the sections and relocations of the input file at once.
6034 This is so that we only have to read the local symbols once, and
6035 don't have to keep them in memory. */
6038 elf_link_input_bfd (finfo
, input_bfd
)
6039 struct elf_final_link_info
*finfo
;
6042 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
6043 bfd
*, asection
*, bfd_byte
*,
6044 Elf_Internal_Rela
*,
6045 Elf_Internal_Sym
*, asection
**));
6047 Elf_Internal_Shdr
*symtab_hdr
;
6050 Elf_External_Sym
*external_syms
;
6051 Elf_External_Sym
*esym
;
6052 Elf_External_Sym
*esymend
;
6053 Elf_Internal_Sym
*isym
;
6055 asection
**ppsection
;
6057 struct elf_backend_data
*bed
;
6058 boolean emit_relocs
;
6059 struct elf_link_hash_entry
**sym_hashes
;
6061 output_bfd
= finfo
->output_bfd
;
6062 bed
= get_elf_backend_data (output_bfd
);
6063 relocate_section
= bed
->elf_backend_relocate_section
;
6065 /* If this is a dynamic object, we don't want to do anything here:
6066 we don't want the local symbols, and we don't want the section
6068 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6071 emit_relocs
= (finfo
->info
->relocateable
6072 || finfo
->info
->emitrelocations
6073 || bed
->elf_backend_emit_relocs
);
6075 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6076 if (elf_bad_symtab (input_bfd
))
6078 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6083 locsymcount
= symtab_hdr
->sh_info
;
6084 extsymoff
= symtab_hdr
->sh_info
;
6087 /* Read the local symbols. */
6088 if (symtab_hdr
->contents
!= NULL
)
6089 external_syms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
6090 else if (locsymcount
== 0)
6091 external_syms
= NULL
;
6094 bfd_size_type amt
= locsymcount
* sizeof (Elf_External_Sym
);
6095 external_syms
= finfo
->external_syms
;
6096 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
6097 || bfd_bread (external_syms
, amt
, input_bfd
) != amt
)
6101 /* Swap in the local symbols and write out the ones which we know
6102 are going into the output file. */
6103 esym
= external_syms
;
6104 esymend
= esym
+ locsymcount
;
6105 isym
= finfo
->internal_syms
;
6106 pindex
= finfo
->indices
;
6107 ppsection
= finfo
->sections
;
6108 for (; esym
< esymend
; esym
++, isym
++, pindex
++, ppsection
++)
6112 Elf_Internal_Sym osym
;
6114 elf_swap_symbol_in (input_bfd
, esym
, isym
);
6117 if (elf_bad_symtab (input_bfd
))
6119 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6126 if (isym
->st_shndx
== SHN_UNDEF
)
6127 isec
= bfd_und_section_ptr
;
6128 else if (isym
->st_shndx
> 0 && isym
->st_shndx
< SHN_LORESERVE
)
6130 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
6131 if (isec
&& elf_section_data (isec
)->merge_info
6132 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6134 _bfd_merged_section_offset (output_bfd
, &isec
,
6135 elf_section_data (isec
)->merge_info
,
6136 isym
->st_value
, (bfd_vma
) 0);
6138 else if (isym
->st_shndx
== SHN_ABS
)
6139 isec
= bfd_abs_section_ptr
;
6140 else if (isym
->st_shndx
== SHN_COMMON
)
6141 isec
= bfd_com_section_ptr
;
6150 /* Don't output the first, undefined, symbol. */
6151 if (esym
== external_syms
)
6154 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6156 /* We never output section symbols. Instead, we use the
6157 section symbol of the corresponding section in the output
6162 /* If we are stripping all symbols, we don't want to output this
6164 if (finfo
->info
->strip
== strip_all
)
6167 /* If we are discarding all local symbols, we don't want to
6168 output this one. If we are generating a relocateable output
6169 file, then some of the local symbols may be required by
6170 relocs; we output them below as we discover that they are
6172 if (finfo
->info
->discard
== discard_all
)
6175 /* If this symbol is defined in a section which we are
6176 discarding, we don't need to keep it, but note that
6177 linker_mark is only reliable for sections that have contents.
6178 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6179 as well as linker_mark. */
6180 if (isym
->st_shndx
> 0
6181 && isym
->st_shndx
< SHN_LORESERVE
6183 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6184 || (! finfo
->info
->relocateable
6185 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6188 /* Get the name of the symbol. */
6189 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6194 /* See if we are discarding symbols with this name. */
6195 if ((finfo
->info
->strip
== strip_some
6196 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
6198 || (((finfo
->info
->discard
== discard_sec_merge
6199 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocateable
)
6200 || finfo
->info
->discard
== discard_l
)
6201 && bfd_is_local_label_name (input_bfd
, name
)))
6204 /* If we get here, we are going to output this symbol. */
6208 /* Adjust the section index for the output file. */
6209 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6210 isec
->output_section
);
6211 if (osym
.st_shndx
== (unsigned short) -1)
6214 *pindex
= bfd_get_symcount (output_bfd
);
6216 /* ELF symbols in relocateable files are section relative, but
6217 in executable files they are virtual addresses. Note that
6218 this code assumes that all ELF sections have an associated
6219 BFD section with a reasonable value for output_offset; below
6220 we assume that they also have a reasonable value for
6221 output_section. Any special sections must be set up to meet
6222 these requirements. */
6223 osym
.st_value
+= isec
->output_offset
;
6224 if (! finfo
->info
->relocateable
)
6225 osym
.st_value
+= isec
->output_section
->vma
;
6227 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
6231 /* Relocate the contents of each section. */
6232 sym_hashes
= elf_sym_hashes (input_bfd
);
6233 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6237 if (! o
->linker_mark
)
6239 /* This section was omitted from the link. */
6243 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6244 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6247 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6249 /* Section was created by elf_link_create_dynamic_sections
6254 /* Get the contents of the section. They have been cached by a
6255 relaxation routine. Note that o is a section in an input
6256 file, so the contents field will not have been set by any of
6257 the routines which work on output files. */
6258 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6259 contents
= elf_section_data (o
)->this_hdr
.contents
;
6262 contents
= finfo
->contents
;
6263 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
6264 (file_ptr
) 0, o
->_raw_size
))
6268 if ((o
->flags
& SEC_RELOC
) != 0)
6270 Elf_Internal_Rela
*internal_relocs
;
6272 /* Get the swapped relocs. */
6273 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
6274 (input_bfd
, o
, finfo
->external_relocs
,
6275 finfo
->internal_relocs
, false));
6276 if (internal_relocs
== NULL
6277 && o
->reloc_count
> 0)
6280 /* Run through the relocs looking for any against symbols
6281 from discarded sections and section symbols from
6282 removed link-once sections. Complain about relocs
6283 against discarded sections. Zero relocs against removed
6284 link-once sections. We should really complain if
6285 anything in the final link tries to use it, but
6286 DWARF-based exception handling might have an entry in
6287 .eh_frame to describe a routine in the linkonce section,
6288 and it turns out to be hard to remove the .eh_frame
6289 entry too. FIXME. */
6290 if (!finfo
->info
->relocateable
)
6292 Elf_Internal_Rela
*rel
, *relend
;
6294 rel
= internal_relocs
;
6295 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6296 for ( ; rel
< relend
; rel
++)
6298 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
6300 if (r_symndx
>= locsymcount
6301 || (elf_bad_symtab (input_bfd
)
6302 && finfo
->sections
[r_symndx
] == NULL
))
6304 struct elf_link_hash_entry
*h
;
6306 h
= sym_hashes
[r_symndx
- extsymoff
];
6307 while (h
->root
.type
== bfd_link_hash_indirect
6308 || h
->root
.type
== bfd_link_hash_warning
)
6309 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6311 /* Complain if the definition comes from a
6312 discarded section. */
6313 if ((h
->root
.type
== bfd_link_hash_defined
6314 || h
->root
.type
== bfd_link_hash_defweak
)
6315 && ! bfd_is_abs_section (h
->root
.u
.def
.section
)
6316 && bfd_is_abs_section (h
->root
.u
.def
.section
6319 #if BFD_VERSION_DATE < 20031005
6320 if ((o
->flags
& SEC_DEBUGGING
) != 0)
6322 #if BFD_VERSION_DATE > 20021005
6323 (*finfo
->info
->callbacks
->warning
)
6325 _("warning: relocation against removed section; zeroing"),
6326 NULL
, input_bfd
, o
, rel
->r_offset
);
6328 memset (rel
, 0, sizeof (*rel
));
6333 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6334 (finfo
->info
, h
->root
.root
.string
,
6335 input_bfd
, o
, rel
->r_offset
,
6343 isym
= finfo
->internal_syms
+ r_symndx
;
6344 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6346 asection
*sec
= finfo
->sections
[r_symndx
];
6349 && ! bfd_is_abs_section (sec
)
6350 && bfd_is_abs_section (sec
->output_section
))
6352 #if BFD_VERSION_DATE < 20031005
6353 if ((o
->flags
& SEC_DEBUGGING
) != 0
6354 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
6356 #if BFD_VERSION_DATE > 20021005
6357 (*finfo
->info
->callbacks
->warning
)
6359 _("warning: relocation against removed section"),
6360 NULL
, input_bfd
, o
, rel
->r_offset
);
6363 = ELF_R_INFO (0, ELF_R_TYPE (rel
->r_info
));
6371 = _("local symbols in discarded section %s");
6373 = strlen (sec
->name
) + strlen (msg
) - 1;
6374 char *buf
= (char *) bfd_malloc (amt
);
6377 sprintf (buf
, msg
, sec
->name
);
6379 buf
= (char *) sec
->name
;
6380 ok
= (*finfo
->info
->callbacks
6381 ->undefined_symbol
) (finfo
->info
, buf
,
6385 if (buf
!= sec
->name
)
6396 /* Relocate the section by invoking a back end routine.
6398 The back end routine is responsible for adjusting the
6399 section contents as necessary, and (if using Rela relocs
6400 and generating a relocateable output file) adjusting the
6401 reloc addend as necessary.
6403 The back end routine does not have to worry about setting
6404 the reloc address or the reloc symbol index.
6406 The back end routine is given a pointer to the swapped in
6407 internal symbols, and can access the hash table entries
6408 for the external symbols via elf_sym_hashes (input_bfd).
6410 When generating relocateable output, the back end routine
6411 must handle STB_LOCAL/STT_SECTION symbols specially. The
6412 output symbol is going to be a section symbol
6413 corresponding to the output section, which will require
6414 the addend to be adjusted. */
6416 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6417 input_bfd
, o
, contents
,
6419 finfo
->internal_syms
,
6425 Elf_Internal_Rela
*irela
;
6426 Elf_Internal_Rela
*irelaend
;
6427 struct elf_link_hash_entry
**rel_hash
;
6428 Elf_Internal_Shdr
*input_rel_hdr
;
6429 unsigned int next_erel
;
6430 void (*reloc_emitter
) PARAMS ((bfd
*, asection
*,
6431 Elf_Internal_Shdr
*,
6432 Elf_Internal_Rela
*));
6434 /* Adjust the reloc addresses and symbol indices. */
6436 irela
= internal_relocs
;
6437 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6438 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6439 + elf_section_data (o
->output_section
)->rel_count
6440 + elf_section_data (o
->output_section
)->rel_count2
);
6441 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6443 unsigned long r_symndx
;
6446 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6452 irela
->r_offset
+= o
->output_offset
;
6454 /* Relocs in an executable have to be virtual addresses. */
6455 if (finfo
->info
->emitrelocations
)
6456 irela
->r_offset
+= o
->output_section
->vma
;
6458 r_symndx
= ELF_R_SYM (irela
->r_info
);
6463 if (r_symndx
>= locsymcount
6464 || (elf_bad_symtab (input_bfd
)
6465 && finfo
->sections
[r_symndx
] == NULL
))
6467 struct elf_link_hash_entry
*rh
;
6470 /* This is a reloc against a global symbol. We
6471 have not yet output all the local symbols, so
6472 we do not know the symbol index of any global
6473 symbol. We set the rel_hash entry for this
6474 reloc to point to the global hash table entry
6475 for this symbol. The symbol index is then
6476 set at the end of elf_bfd_final_link. */
6477 indx
= r_symndx
- extsymoff
;
6478 rh
= elf_sym_hashes (input_bfd
)[indx
];
6479 while (rh
->root
.type
== bfd_link_hash_indirect
6480 || rh
->root
.type
== bfd_link_hash_warning
)
6481 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6483 /* Setting the index to -2 tells
6484 elf_link_output_extsym that this symbol is
6486 BFD_ASSERT (rh
->indx
< 0);
6494 /* This is a reloc against a local symbol. */
6497 isym
= finfo
->internal_syms
+ r_symndx
;
6498 sec
= finfo
->sections
[r_symndx
];
6499 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6501 /* I suppose the backend ought to fill in the
6502 section of any STT_SECTION symbol against a
6503 processor specific section. If we have
6504 discarded a section, the output_section will
6505 be the absolute section. */
6507 && (bfd_is_abs_section (sec
)
6508 || (sec
->output_section
!= NULL
6509 && bfd_is_abs_section (sec
->output_section
))))
6511 else if (sec
== NULL
|| sec
->owner
== NULL
)
6513 bfd_set_error (bfd_error_bad_value
);
6518 r_symndx
= sec
->output_section
->target_index
;
6519 BFD_ASSERT (r_symndx
!= 0);
6524 if (finfo
->indices
[r_symndx
] == -1)
6526 unsigned long shlink
;
6530 if (finfo
->info
->strip
== strip_all
)
6532 /* You can't do ld -r -s. */
6533 bfd_set_error (bfd_error_invalid_operation
);
6537 /* This symbol was skipped earlier, but
6538 since it is needed by a reloc, we
6539 must output it now. */
6540 shlink
= symtab_hdr
->sh_link
;
6541 name
= (bfd_elf_string_from_elf_section
6542 (input_bfd
, shlink
, isym
->st_name
));
6546 osec
= sec
->output_section
;
6548 _bfd_elf_section_from_bfd_section (output_bfd
,
6550 if (isym
->st_shndx
== (unsigned short) -1)
6553 isym
->st_value
+= sec
->output_offset
;
6554 if (! finfo
->info
->relocateable
)
6555 isym
->st_value
+= osec
->vma
;
6557 finfo
->indices
[r_symndx
]
6558 = bfd_get_symcount (output_bfd
);
6560 if (! elf_link_output_sym (finfo
, name
, isym
, sec
))
6564 r_symndx
= finfo
->indices
[r_symndx
];
6567 irela
->r_info
= ELF_R_INFO (r_symndx
,
6568 ELF_R_TYPE (irela
->r_info
));
6571 /* Swap out the relocs. */
6572 if (bed
->elf_backend_emit_relocs
6573 && !(finfo
->info
->relocateable
6574 || finfo
->info
->emitrelocations
))
6575 reloc_emitter
= bed
->elf_backend_emit_relocs
;
6577 reloc_emitter
= elf_link_output_relocs
;
6579 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6580 (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
, internal_relocs
);
6582 input_rel_hdr
= elf_section_data (o
)->rel_hdr2
;
6585 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
6586 * bed
->s
->int_rels_per_ext_rel
);
6587 reloc_emitter (output_bfd
, o
, input_rel_hdr
, internal_relocs
);
6593 /* Write out the modified section contents. */
6594 if (elf_section_data (o
)->stab_info
)
6596 if (! (_bfd_write_section_stabs
6597 (output_bfd
, &elf_hash_table (finfo
->info
)->stab_info
,
6598 o
, &elf_section_data (o
)->stab_info
, contents
)))
6601 else if (elf_section_data (o
)->merge_info
)
6603 if (! (_bfd_write_merged_section
6604 (output_bfd
, o
, elf_section_data (o
)->merge_info
)))
6609 bfd_size_type sec_size
;
6611 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
6612 if (! (o
->flags
& SEC_EXCLUDE
)
6613 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
6615 (file_ptr
) o
->output_offset
,
6624 /* Generate a reloc when linking an ELF file. This is a reloc
6625 requested by the linker, and does come from any input file. This
6626 is used to build constructor and destructor tables when linking
6630 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
6632 struct bfd_link_info
*info
;
6633 asection
*output_section
;
6634 struct bfd_link_order
*link_order
;
6636 reloc_howto_type
*howto
;
6640 struct elf_link_hash_entry
**rel_hash_ptr
;
6641 Elf_Internal_Shdr
*rel_hdr
;
6642 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
6644 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
6647 bfd_set_error (bfd_error_bad_value
);
6651 addend
= link_order
->u
.reloc
.p
->addend
;
6653 /* Figure out the symbol index. */
6654 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
6655 + elf_section_data (output_section
)->rel_count
6656 + elf_section_data (output_section
)->rel_count2
);
6657 if (link_order
->type
== bfd_section_reloc_link_order
)
6659 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
6660 BFD_ASSERT (indx
!= 0);
6661 *rel_hash_ptr
= NULL
;
6665 struct elf_link_hash_entry
*h
;
6667 /* Treat a reloc against a defined symbol as though it were
6668 actually against the section. */
6669 h
= ((struct elf_link_hash_entry
*)
6670 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
6671 link_order
->u
.reloc
.p
->u
.name
,
6672 false, false, true));
6674 && (h
->root
.type
== bfd_link_hash_defined
6675 || h
->root
.type
== bfd_link_hash_defweak
))
6679 section
= h
->root
.u
.def
.section
;
6680 indx
= section
->output_section
->target_index
;
6681 *rel_hash_ptr
= NULL
;
6682 /* It seems that we ought to add the symbol value to the
6683 addend here, but in practice it has already been added
6684 because it was passed to constructor_callback. */
6685 addend
+= section
->output_section
->vma
+ section
->output_offset
;
6689 /* Setting the index to -2 tells elf_link_output_extsym that
6690 this symbol is used by a reloc. */
6697 if (! ((*info
->callbacks
->unattached_reloc
)
6698 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
6699 (asection
*) NULL
, (bfd_vma
) 0)))
6705 /* If this is an inplace reloc, we must write the addend into the
6707 if (howto
->partial_inplace
&& addend
!= 0)
6710 bfd_reloc_status_type rstat
;
6713 const char *sym_name
;
6715 size
= bfd_get_reloc_size (howto
);
6716 buf
= (bfd_byte
*) bfd_zmalloc (size
);
6717 if (buf
== (bfd_byte
*) NULL
)
6719 rstat
= _bfd_relocate_contents (howto
, output_bfd
, (bfd_vma
) addend
, buf
);
6726 case bfd_reloc_outofrange
:
6729 case bfd_reloc_overflow
:
6730 if (link_order
->type
== bfd_section_reloc_link_order
)
6731 sym_name
= bfd_section_name (output_bfd
,
6732 link_order
->u
.reloc
.p
->u
.section
);
6734 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
6735 if (! ((*info
->callbacks
->reloc_overflow
)
6736 (info
, sym_name
, howto
->name
, addend
,
6737 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
6744 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
6745 (file_ptr
) link_order
->offset
, size
);
6751 /* The address of a reloc is relative to the section in a
6752 relocateable file, and is a virtual address in an executable
6754 offset
= link_order
->offset
;
6755 if (! info
->relocateable
)
6756 offset
+= output_section
->vma
;
6758 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
6760 if (rel_hdr
->sh_type
== SHT_REL
)
6763 Elf_Internal_Rel
*irel
;
6764 Elf_External_Rel
*erel
;
6767 size
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel
);
6768 irel
= (Elf_Internal_Rel
*) bfd_zmalloc (size
);
6772 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
6773 irel
[i
].r_offset
= offset
;
6774 irel
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
6776 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
6777 + elf_section_data (output_section
)->rel_count
);
6779 if (bed
->s
->swap_reloc_out
)
6780 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, (bfd_byte
*) erel
);
6782 elf_swap_reloc_out (output_bfd
, irel
, erel
);
6789 Elf_Internal_Rela
*irela
;
6790 Elf_External_Rela
*erela
;
6793 size
= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
6794 irela
= (Elf_Internal_Rela
*) bfd_zmalloc (size
);
6798 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
6799 irela
[i
].r_offset
= offset
;
6800 irela
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
6801 irela
[0].r_addend
= addend
;
6803 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
6804 + elf_section_data (output_section
)->rel_count
);
6806 if (bed
->s
->swap_reloca_out
)
6807 (*bed
->s
->swap_reloca_out
) (output_bfd
, irela
, (bfd_byte
*) erela
);
6809 elf_swap_reloca_out (output_bfd
, irela
, erela
);
6812 ++elf_section_data (output_section
)->rel_count
;
6817 /* Allocate a pointer to live in a linker created section. */
6820 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
6822 struct bfd_link_info
*info
;
6823 elf_linker_section_t
*lsect
;
6824 struct elf_link_hash_entry
*h
;
6825 const Elf_Internal_Rela
*rel
;
6827 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
6828 elf_linker_section_pointers_t
*linker_section_ptr
;
6829 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
6832 BFD_ASSERT (lsect
!= NULL
);
6834 /* Is this a global symbol? */
6837 /* Has this symbol already been allocated? If so, our work is done. */
6838 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
6843 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
6844 /* Make sure this symbol is output as a dynamic symbol. */
6845 if (h
->dynindx
== -1)
6847 if (! elf_link_record_dynamic_symbol (info
, h
))
6851 if (lsect
->rel_section
)
6852 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
6856 /* Allocation of a pointer to a local symbol. */
6857 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
6859 /* Allocate a table to hold the local symbols if first time. */
6862 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
6863 register unsigned int i
;
6866 amt
*= sizeof (elf_linker_section_pointers_t
*);
6867 ptr
= (elf_linker_section_pointers_t
**) bfd_alloc (abfd
, amt
);
6872 elf_local_ptr_offsets (abfd
) = ptr
;
6873 for (i
= 0; i
< num_symbols
; i
++)
6874 ptr
[i
] = (elf_linker_section_pointers_t
*) 0;
6877 /* Has this symbol already been allocated? If so, our work is done. */
6878 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
6883 ptr_linker_section_ptr
= &ptr
[r_symndx
];
6887 /* If we are generating a shared object, we need to
6888 output a R_<xxx>_RELATIVE reloc so that the
6889 dynamic linker can adjust this GOT entry. */
6890 BFD_ASSERT (lsect
->rel_section
!= NULL
);
6891 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
6895 /* Allocate space for a pointer in the linker section, and allocate
6896 a new pointer record from internal memory. */
6897 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
6898 amt
= sizeof (elf_linker_section_pointers_t
);
6899 linker_section_ptr
= (elf_linker_section_pointers_t
*) bfd_alloc (abfd
, amt
);
6901 if (!linker_section_ptr
)
6904 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
6905 linker_section_ptr
->addend
= rel
->r_addend
;
6906 linker_section_ptr
->which
= lsect
->which
;
6907 linker_section_ptr
->written_address_p
= false;
6908 *ptr_linker_section_ptr
= linker_section_ptr
;
6911 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
6913 linker_section_ptr
->offset
= (lsect
->section
->_raw_size
6914 - lsect
->hole_size
+ (ARCH_SIZE
/ 8));
6915 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
6916 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
6917 if (lsect
->sym_hash
)
6919 /* Bump up symbol value if needed. */
6920 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
6922 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
6923 lsect
->sym_hash
->root
.root
.string
,
6924 (long) ARCH_SIZE
/ 8,
6925 (long) lsect
->sym_hash
->root
.u
.def
.value
);
6931 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
6933 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
6937 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
6938 lsect
->name
, (long) linker_section_ptr
->offset
,
6939 (long) lsect
->section
->_raw_size
);
6946 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
6949 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
6952 /* Fill in the address for a pointer generated in a linker section. */
6955 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
,
6956 relocation
, rel
, relative_reloc
)
6959 struct bfd_link_info
*info
;
6960 elf_linker_section_t
*lsect
;
6961 struct elf_link_hash_entry
*h
;
6963 const Elf_Internal_Rela
*rel
;
6966 elf_linker_section_pointers_t
*linker_section_ptr
;
6968 BFD_ASSERT (lsect
!= NULL
);
6972 /* Handle global symbol. */
6973 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
6974 (h
->linker_section_pointer
,
6978 BFD_ASSERT (linker_section_ptr
!= NULL
);
6980 if (! elf_hash_table (info
)->dynamic_sections_created
6983 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
6985 /* This is actually a static link, or it is a
6986 -Bsymbolic link and the symbol is defined
6987 locally. We must initialize this entry in the
6990 When doing a dynamic link, we create a .rela.<xxx>
6991 relocation entry to initialize the value. This
6992 is done in the finish_dynamic_symbol routine. */
6993 if (!linker_section_ptr
->written_address_p
)
6995 linker_section_ptr
->written_address_p
= true;
6996 bfd_put_ptr (output_bfd
,
6997 relocation
+ linker_section_ptr
->addend
,
6998 (lsect
->section
->contents
6999 + linker_section_ptr
->offset
));
7005 /* Handle local symbol. */
7006 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
7007 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
7008 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
7009 linker_section_ptr
= (_bfd_elf_find_pointer_linker_section
7010 (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
7014 BFD_ASSERT (linker_section_ptr
!= NULL
);
7016 /* Write out pointer if it hasn't been rewritten out before. */
7017 if (!linker_section_ptr
->written_address_p
)
7019 linker_section_ptr
->written_address_p
= true;
7020 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
7021 lsect
->section
->contents
+ linker_section_ptr
->offset
);
7025 asection
*srel
= lsect
->rel_section
;
7026 Elf_Internal_Rela
*outrel
;
7027 Elf_External_Rela
*erel
;
7028 struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7032 amt
= sizeof (Elf_Internal_Rela
) * bed
->s
->int_rels_per_ext_rel
;
7033 outrel
= (Elf_Internal_Rela
*) bfd_zmalloc (amt
);
7036 (*_bfd_error_handler
) (_("Error: out of memory"));
7040 /* We need to generate a relative reloc for the dynamic
7044 srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
7046 lsect
->rel_section
= srel
;
7049 BFD_ASSERT (srel
!= NULL
);
7051 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7052 outrel
[i
].r_offset
= (lsect
->section
->output_section
->vma
7053 + lsect
->section
->output_offset
7054 + linker_section_ptr
->offset
);
7055 outrel
[0].r_info
= ELF_R_INFO (0, relative_reloc
);
7056 outrel
[0].r_addend
= 0;
7057 erel
= (Elf_External_Rela
*) lsect
->section
->contents
;
7058 erel
+= elf_section_data (lsect
->section
)->rel_count
;
7059 elf_swap_reloca_out (output_bfd
, outrel
, erel
);
7060 ++elf_section_data (lsect
->section
)->rel_count
;
7067 relocation
= (lsect
->section
->output_offset
7068 + linker_section_ptr
->offset
7069 - lsect
->hole_offset
7070 - lsect
->sym_offset
);
7074 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7075 lsect
->name
, (long) relocation
, (long) relocation
);
7078 /* Subtract out the addend, because it will get added back in by the normal
7080 return relocation
- linker_section_ptr
->addend
;
7083 /* Garbage collect unused sections. */
7085 static boolean elf_gc_mark
7086 PARAMS ((struct bfd_link_info
*info
, asection
*sec
,
7087 asection
* (*gc_mark_hook
)
7088 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7089 struct elf_link_hash_entry
*, Elf_Internal_Sym
*))));
7091 static boolean elf_gc_sweep
7092 PARAMS ((struct bfd_link_info
*info
,
7093 boolean (*gc_sweep_hook
)
7094 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
7095 const Elf_Internal_Rela
*relocs
))));
7097 static boolean elf_gc_sweep_symbol
7098 PARAMS ((struct elf_link_hash_entry
*h
, PTR idxptr
));
7100 static boolean elf_gc_allocate_got_offsets
7101 PARAMS ((struct elf_link_hash_entry
*h
, PTR offarg
));
7103 static boolean elf_gc_propagate_vtable_entries_used
7104 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
7106 static boolean elf_gc_smash_unused_vtentry_relocs
7107 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
7109 /* The mark phase of garbage collection. For a given section, mark
7110 it and any sections in this section's group, and all the sections
7111 which define symbols to which it refers. */
7114 elf_gc_mark (info
, sec
, gc_mark_hook
)
7115 struct bfd_link_info
*info
;
7117 asection
* (*gc_mark_hook
)
7118 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7119 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
7122 asection
*group_sec
;
7126 /* Mark all the sections in the group. */
7127 group_sec
= elf_section_data (sec
)->next_in_group
;
7128 if (group_sec
&& !group_sec
->gc_mark
)
7129 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
7132 /* Look through the section relocs. */
7134 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
7136 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
7137 Elf_Internal_Shdr
*symtab_hdr
;
7138 struct elf_link_hash_entry
**sym_hashes
;
7141 Elf_External_Sym
*locsyms
, *freesyms
= NULL
;
7142 bfd
*input_bfd
= sec
->owner
;
7143 struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
7145 /* GCFIXME: how to arrange so that relocs and symbols are not
7146 reread continually? */
7148 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
7149 sym_hashes
= elf_sym_hashes (input_bfd
);
7151 /* Read the local symbols. */
7152 if (elf_bad_symtab (input_bfd
))
7154 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
7158 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
7159 if (symtab_hdr
->contents
)
7160 locsyms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
7161 else if (nlocsyms
== 0)
7165 bfd_size_type amt
= nlocsyms
* sizeof (Elf_External_Sym
);
7166 locsyms
= freesyms
= bfd_malloc (amt
);
7167 if (freesyms
== NULL
7168 || bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
7169 || bfd_bread (locsyms
, amt
, input_bfd
) != amt
)
7176 /* Read the relocations. */
7177 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
7178 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
7179 info
->keep_memory
));
7180 if (relstart
== NULL
)
7185 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7187 for (rel
= relstart
; rel
< relend
; rel
++)
7189 unsigned long r_symndx
;
7191 struct elf_link_hash_entry
*h
;
7194 r_symndx
= ELF_R_SYM (rel
->r_info
);
7198 if (elf_bad_symtab (sec
->owner
))
7200 elf_swap_symbol_in (input_bfd
, &locsyms
[r_symndx
], &s
);
7201 if (ELF_ST_BIND (s
.st_info
) == STB_LOCAL
)
7202 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, NULL
, &s
);
7205 h
= sym_hashes
[r_symndx
- extsymoff
];
7206 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, h
, NULL
);
7209 else if (r_symndx
>= nlocsyms
)
7211 h
= sym_hashes
[r_symndx
- extsymoff
];
7212 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, h
, NULL
);
7216 elf_swap_symbol_in (input_bfd
, &locsyms
[r_symndx
], &s
);
7217 rsec
= (*gc_mark_hook
) (sec
->owner
, info
, rel
, NULL
, &s
);
7220 if (rsec
&& !rsec
->gc_mark
)
7221 if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
7229 if (!info
->keep_memory
)
7239 /* The sweep phase of garbage collection. Remove all garbage sections. */
7242 elf_gc_sweep (info
, gc_sweep_hook
)
7243 struct bfd_link_info
*info
;
7244 boolean (*gc_sweep_hook
)
7245 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
7246 const Elf_Internal_Rela
*relocs
));
7250 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7254 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
7257 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7259 /* Keep special sections. Keep .debug sections. */
7260 if ((o
->flags
& SEC_LINKER_CREATED
)
7261 || (o
->flags
& SEC_DEBUGGING
))
7267 /* Skip sweeping sections already excluded. */
7268 if (o
->flags
& SEC_EXCLUDE
)
7271 /* Since this is early in the link process, it is simple
7272 to remove a section from the output. */
7273 o
->flags
|= SEC_EXCLUDE
;
7275 /* But we also have to update some of the relocation
7276 info we collected before. */
7278 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
7280 Elf_Internal_Rela
*internal_relocs
;
7283 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
7284 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
7285 if (internal_relocs
== NULL
)
7288 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
7290 if (!info
->keep_memory
)
7291 free (internal_relocs
);
7299 /* Remove the symbols that were in the swept sections from the dynamic
7300 symbol table. GCFIXME: Anyone know how to get them out of the
7301 static symbol table as well? */
7305 elf_link_hash_traverse (elf_hash_table (info
),
7306 elf_gc_sweep_symbol
,
7309 elf_hash_table (info
)->dynsymcount
= i
;
7315 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7318 elf_gc_sweep_symbol (h
, idxptr
)
7319 struct elf_link_hash_entry
*h
;
7322 int *idx
= (int *) idxptr
;
7324 if (h
->dynindx
!= -1
7325 && ((h
->root
.type
!= bfd_link_hash_defined
7326 && h
->root
.type
!= bfd_link_hash_defweak
)
7327 || h
->root
.u
.def
.section
->gc_mark
))
7328 h
->dynindx
= (*idx
)++;
7333 /* Propogate collected vtable information. This is called through
7334 elf_link_hash_traverse. */
7337 elf_gc_propagate_vtable_entries_used (h
, okp
)
7338 struct elf_link_hash_entry
*h
;
7341 /* Those that are not vtables. */
7342 if (h
->vtable_parent
== NULL
)
7345 /* Those vtables that do not have parents, we cannot merge. */
7346 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
7349 /* If we've already been done, exit. */
7350 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
7353 /* Make sure the parent's table is up to date. */
7354 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
7356 if (h
->vtable_entries_used
== NULL
)
7358 /* None of this table's entries were referenced. Re-use the
7360 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
7361 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
7368 /* Or the parent's entries into ours. */
7369 cu
= h
->vtable_entries_used
;
7371 pu
= h
->vtable_parent
->vtable_entries_used
;
7374 asection
*sec
= h
->root
.u
.def
.section
;
7375 struct elf_backend_data
*bed
= get_elf_backend_data (sec
->owner
);
7376 int file_align
= bed
->s
->file_align
;
7378 n
= h
->vtable_parent
->vtable_entries_size
/ file_align
;
7393 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
7394 struct elf_link_hash_entry
*h
;
7398 bfd_vma hstart
, hend
;
7399 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
7400 struct elf_backend_data
*bed
;
7403 /* Take care of both those symbols that do not describe vtables as
7404 well as those that are not loaded. */
7405 if (h
->vtable_parent
== NULL
)
7408 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
7409 || h
->root
.type
== bfd_link_hash_defweak
);
7411 sec
= h
->root
.u
.def
.section
;
7412 hstart
= h
->root
.u
.def
.value
;
7413 hend
= hstart
+ h
->size
;
7415 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
7416 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, true));
7418 return *(boolean
*) okp
= false;
7419 bed
= get_elf_backend_data (sec
->owner
);
7420 file_align
= bed
->s
->file_align
;
7422 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7424 for (rel
= relstart
; rel
< relend
; ++rel
)
7425 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
7427 /* If the entry is in use, do nothing. */
7428 if (h
->vtable_entries_used
7429 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
7431 bfd_vma entry
= (rel
->r_offset
- hstart
) / file_align
;
7432 if (h
->vtable_entries_used
[entry
])
7435 /* Otherwise, kill it. */
7436 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
7442 /* Do mark and sweep of unused sections. */
7445 elf_gc_sections (abfd
, info
)
7447 struct bfd_link_info
*info
;
7451 asection
* (*gc_mark_hook
)
7452 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
7453 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
7455 if (!get_elf_backend_data (abfd
)->can_gc_sections
7456 || info
->relocateable
|| info
->emitrelocations
7457 || elf_hash_table (info
)->dynamic_sections_created
)
7460 /* Apply transitive closure to the vtable entry usage info. */
7461 elf_link_hash_traverse (elf_hash_table (info
),
7462 elf_gc_propagate_vtable_entries_used
,
7467 /* Kill the vtable relocations that were not used. */
7468 elf_link_hash_traverse (elf_hash_table (info
),
7469 elf_gc_smash_unused_vtentry_relocs
,
7474 /* Grovel through relocs to find out who stays ... */
7476 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
7477 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7481 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
7484 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7486 if (o
->flags
& SEC_KEEP
)
7487 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
7492 /* ... and mark SEC_EXCLUDE for those that go. */
7493 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
7499 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
7502 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
7505 struct elf_link_hash_entry
*h
;
7508 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
7509 struct elf_link_hash_entry
**search
, *child
;
7510 bfd_size_type extsymcount
;
7512 /* The sh_info field of the symtab header tells us where the
7513 external symbols start. We don't care about the local symbols at
7515 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
7516 if (!elf_bad_symtab (abfd
))
7517 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
7519 sym_hashes
= elf_sym_hashes (abfd
);
7520 sym_hashes_end
= sym_hashes
+ extsymcount
;
7522 /* Hunt down the child symbol, which is in this section at the same
7523 offset as the relocation. */
7524 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
7526 if ((child
= *search
) != NULL
7527 && (child
->root
.type
== bfd_link_hash_defined
7528 || child
->root
.type
== bfd_link_hash_defweak
)
7529 && child
->root
.u
.def
.section
== sec
7530 && child
->root
.u
.def
.value
== offset
)
7534 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
7535 bfd_archive_filename (abfd
), sec
->name
,
7536 (unsigned long) offset
);
7537 bfd_set_error (bfd_error_invalid_operation
);
7543 /* This *should* only be the absolute section. It could potentially
7544 be that someone has defined a non-global vtable though, which
7545 would be bad. It isn't worth paging in the local symbols to be
7546 sure though; that case should simply be handled by the assembler. */
7548 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
7551 child
->vtable_parent
= h
;
7556 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
7559 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
7560 bfd
*abfd ATTRIBUTE_UNUSED
;
7561 asection
*sec ATTRIBUTE_UNUSED
;
7562 struct elf_link_hash_entry
*h
;
7565 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7566 int file_align
= bed
->s
->file_align
;
7568 if (addend
>= h
->vtable_entries_size
)
7571 boolean
*ptr
= h
->vtable_entries_used
;
7573 /* While the symbol is undefined, we have to be prepared to handle
7575 if (h
->root
.type
== bfd_link_hash_undefined
)
7582 /* Oops! We've got a reference past the defined end of
7583 the table. This is probably a bug -- shall we warn? */
7588 /* Allocate one extra entry for use as a "done" flag for the
7589 consolidation pass. */
7590 bytes
= (size
/ file_align
+ 1) * sizeof (boolean
);
7594 ptr
= bfd_realloc (ptr
- 1, (bfd_size_type
) bytes
);
7600 oldbytes
= ((h
->vtable_entries_size
/ file_align
+ 1)
7601 * sizeof (boolean
));
7602 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
7606 ptr
= bfd_zmalloc ((bfd_size_type
) bytes
);
7611 /* And arrange for that done flag to be at index -1. */
7612 h
->vtable_entries_used
= ptr
+ 1;
7613 h
->vtable_entries_size
= size
;
7616 h
->vtable_entries_used
[addend
/ file_align
] = true;
7621 /* And an accompanying bit to work out final got entry offsets once
7622 we're done. Should be called from final_link. */
7625 elf_gc_common_finalize_got_offsets (abfd
, info
)
7627 struct bfd_link_info
*info
;
7630 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7633 /* The GOT offset is relative to the .got section, but the GOT header is
7634 put into the .got.plt section, if the backend uses it. */
7635 if (bed
->want_got_plt
)
7638 gotoff
= bed
->got_header_size
;
7640 /* Do the local .got entries first. */
7641 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
7643 bfd_signed_vma
*local_got
;
7644 bfd_size_type j
, locsymcount
;
7645 Elf_Internal_Shdr
*symtab_hdr
;
7647 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
7650 local_got
= elf_local_got_refcounts (i
);
7654 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
7655 if (elf_bad_symtab (i
))
7656 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
7658 locsymcount
= symtab_hdr
->sh_info
;
7660 for (j
= 0; j
< locsymcount
; ++j
)
7662 if (local_got
[j
] > 0)
7664 local_got
[j
] = gotoff
;
7665 gotoff
+= ARCH_SIZE
/ 8;
7668 local_got
[j
] = (bfd_vma
) -1;
7672 /* Then the global .got entries. .plt refcounts are handled by
7673 adjust_dynamic_symbol */
7674 elf_link_hash_traverse (elf_hash_table (info
),
7675 elf_gc_allocate_got_offsets
,
7680 /* We need a special top-level link routine to convert got reference counts
7681 to real got offsets. */
7684 elf_gc_allocate_got_offsets (h
, offarg
)
7685 struct elf_link_hash_entry
*h
;
7688 bfd_vma
*off
= (bfd_vma
*) offarg
;
7690 if (h
->got
.refcount
> 0)
7692 h
->got
.offset
= off
[0];
7693 off
[0] += ARCH_SIZE
/ 8;
7696 h
->got
.offset
= (bfd_vma
) -1;
7701 /* Many folk need no more in the way of final link than this, once
7702 got entry reference counting is enabled. */
7705 elf_gc_common_final_link (abfd
, info
)
7707 struct bfd_link_info
*info
;
7709 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
7712 /* Invoke the regular ELF backend linker to do all the work. */
7713 return elf_bfd_final_link (abfd
, info
);
7716 /* This function will be called though elf_link_hash_traverse to store
7717 all hash value of the exported symbols in an array. */
7720 elf_collect_hash_codes (h
, data
)
7721 struct elf_link_hash_entry
*h
;
7724 unsigned long **valuep
= (unsigned long **) data
;
7730 /* Ignore indirect symbols. These are added by the versioning code. */
7731 if (h
->dynindx
== -1)
7734 name
= h
->root
.root
.string
;
7735 p
= strchr (name
, ELF_VER_CHR
);
7738 alc
= bfd_malloc ((bfd_size_type
) (p
- name
+ 1));
7739 memcpy (alc
, name
, (size_t) (p
- name
));
7740 alc
[p
- name
] = '\0';
7744 /* Compute the hash value. */
7745 ha
= bfd_elf_hash (name
);
7747 /* Store the found hash value in the array given as the argument. */
7750 /* And store it in the struct so that we can put it in the hash table
7752 h
->elf_hash_value
= ha
;