2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
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 #include "safe-ctype.h"
25 static bfd_boolean
elf_link_add_object_symbols (bfd
*, struct bfd_link_info
*);
26 static bfd_boolean
elf_section_ignore_discarded_relocs (asection
*);
28 /* Given an ELF BFD, add symbols to the global hash table as
32 elf_bfd_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
34 switch (bfd_get_format (abfd
))
37 return elf_link_add_object_symbols (abfd
, info
);
39 return _bfd_elf_link_add_archive_symbols (abfd
, info
);
41 bfd_set_error (bfd_error_wrong_format
);
46 /* Add symbols from an ELF object file to the linker hash table. */
49 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
51 bfd_boolean (*add_symbol_hook
)
52 (bfd
*, struct bfd_link_info
*, const Elf_Internal_Sym
*,
53 const char **, flagword
*, asection
**, bfd_vma
*);
54 bfd_boolean (*check_relocs
)
55 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
57 Elf_Internal_Shdr
*hdr
;
58 bfd_size_type symcount
;
59 bfd_size_type extsymcount
;
60 bfd_size_type extsymoff
;
61 struct elf_link_hash_entry
**sym_hash
;
63 Elf_External_Versym
*extversym
= NULL
;
64 Elf_External_Versym
*ever
;
65 struct elf_link_hash_entry
*weaks
;
66 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
67 bfd_size_type nondeflt_vers_cnt
= 0;
68 Elf_Internal_Sym
*isymbuf
= NULL
;
69 Elf_Internal_Sym
*isym
;
70 Elf_Internal_Sym
*isymend
;
71 const struct elf_backend_data
*bed
;
72 bfd_boolean add_needed
;
73 struct elf_link_hash_table
* hash_table
;
76 hash_table
= elf_hash_table (info
);
78 bed
= get_elf_backend_data (abfd
);
79 add_symbol_hook
= bed
->elf_add_symbol_hook
;
80 collect
= bed
->collect
;
82 if ((abfd
->flags
& DYNAMIC
) == 0)
88 /* You can't use -r against a dynamic object. Also, there's no
89 hope of using a dynamic object which does not exactly match
90 the format of the output file. */
92 || !is_elf_hash_table (hash_table
)
93 || hash_table
->root
.creator
!= abfd
->xvec
)
95 bfd_set_error (bfd_error_invalid_operation
);
100 /* As a GNU extension, any input sections which are named
101 .gnu.warning.SYMBOL are treated as warning symbols for the given
102 symbol. This differs from .gnu.warning sections, which generate
103 warnings when they are included in an output file. */
104 if (info
->executable
)
108 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
112 name
= bfd_get_section_name (abfd
, s
);
113 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
117 bfd_size_type prefix_len
;
118 const char * gnu_warning_prefix
= _("warning: ");
120 name
+= sizeof ".gnu.warning." - 1;
122 /* If this is a shared object, then look up the symbol
123 in the hash table. If it is there, and it is already
124 been defined, then we will not be using the entry
125 from this shared object, so we don't need to warn.
126 FIXME: If we see the definition in a regular object
127 later on, we will warn, but we shouldn't. The only
128 fix is to keep track of what warnings we are supposed
129 to emit, and then handle them all at the end of the
133 struct elf_link_hash_entry
*h
;
135 h
= elf_link_hash_lookup (hash_table
, name
,
138 /* FIXME: What about bfd_link_hash_common? */
140 && (h
->root
.type
== bfd_link_hash_defined
141 || h
->root
.type
== bfd_link_hash_defweak
))
143 /* We don't want to issue this warning. Clobber
144 the section size so that the warning does not
145 get copied into the output file. */
151 sz
= bfd_section_size (abfd
, s
);
152 prefix_len
= strlen (gnu_warning_prefix
);
153 msg
= bfd_alloc (abfd
, prefix_len
+ sz
+ 1);
157 strcpy (msg
, gnu_warning_prefix
);
158 if (! bfd_get_section_contents (abfd
, s
, msg
+ prefix_len
, 0, sz
))
161 msg
[prefix_len
+ sz
] = '\0';
163 if (! (_bfd_generic_link_add_one_symbol
164 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
165 FALSE
, collect
, NULL
)))
168 if (! info
->relocatable
)
170 /* Clobber the section size so that the warning does
171 not get copied into the output file. */
181 /* If we are creating a shared library, create all the dynamic
182 sections immediately. We need to attach them to something,
183 so we attach them to this BFD, provided it is the right
184 format. FIXME: If there are no input BFD's of the same
185 format as the output, we can't make a shared library. */
187 && is_elf_hash_table (hash_table
)
188 && hash_table
->root
.creator
== abfd
->xvec
189 && ! hash_table
->dynamic_sections_created
)
191 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
195 else if (!is_elf_hash_table (hash_table
))
200 const char *soname
= NULL
;
201 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
204 /* ld --just-symbols and dynamic objects don't mix very well.
205 Test for --just-symbols by looking at info set up by
206 _bfd_elf_link_just_syms. */
207 if ((s
= abfd
->sections
) != NULL
208 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
211 /* If this dynamic lib was specified on the command line with
212 --as-needed in effect, then we don't want to add a DT_NEEDED
213 tag unless the lib is actually used. Similary for libs brought
214 in by another lib's DT_NEEDED. */
215 add_needed
= elf_dyn_lib_class (abfd
) == DYN_NORMAL
;
217 s
= bfd_get_section_by_name (abfd
, ".dynamic");
220 Elf_External_Dyn
*dynbuf
= NULL
;
221 Elf_External_Dyn
*extdyn
;
222 Elf_External_Dyn
*extdynend
;
224 unsigned long shlink
;
226 dynbuf
= bfd_malloc (s
->_raw_size
);
230 if (! bfd_get_section_contents (abfd
, s
, dynbuf
, 0, s
->_raw_size
))
233 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
236 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
239 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
240 for (; extdyn
< extdynend
; extdyn
++)
242 Elf_Internal_Dyn dyn
;
244 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
245 if (dyn
.d_tag
== DT_SONAME
)
247 unsigned int tagv
= dyn
.d_un
.d_val
;
248 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
252 if (dyn
.d_tag
== DT_NEEDED
)
254 struct bfd_link_needed_list
*n
, **pn
;
256 unsigned int tagv
= dyn
.d_un
.d_val
;
258 amt
= sizeof (struct bfd_link_needed_list
);
259 n
= bfd_alloc (abfd
, amt
);
260 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
261 if (n
== NULL
|| fnm
== NULL
)
263 amt
= strlen (fnm
) + 1;
264 anm
= bfd_alloc (abfd
, amt
);
267 memcpy (anm
, fnm
, amt
);
271 for (pn
= & hash_table
->needed
;
277 if (dyn
.d_tag
== DT_RUNPATH
)
279 struct bfd_link_needed_list
*n
, **pn
;
281 unsigned int tagv
= dyn
.d_un
.d_val
;
283 amt
= sizeof (struct bfd_link_needed_list
);
284 n
= bfd_alloc (abfd
, amt
);
285 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
286 if (n
== NULL
|| fnm
== NULL
)
288 amt
= strlen (fnm
) + 1;
289 anm
= bfd_alloc (abfd
, amt
);
292 memcpy (anm
, fnm
, amt
);
302 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
303 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
305 struct bfd_link_needed_list
*n
, **pn
;
307 unsigned int tagv
= dyn
.d_un
.d_val
;
309 amt
= sizeof (struct bfd_link_needed_list
);
310 n
= bfd_alloc (abfd
, amt
);
311 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
312 if (n
== NULL
|| fnm
== NULL
)
314 amt
= strlen (fnm
) + 1;
315 anm
= bfd_alloc (abfd
, amt
);
322 memcpy (anm
, fnm
, amt
);
337 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
338 frees all more recently bfd_alloc'd blocks as well. */
344 struct bfd_link_needed_list
**pn
;
345 for (pn
= & hash_table
->runpath
;
352 /* We do not want to include any of the sections in a dynamic
353 object in the output file. We hack by simply clobbering the
354 list of sections in the BFD. This could be handled more
355 cleanly by, say, a new section flag; the existing
356 SEC_NEVER_LOAD flag is not the one we want, because that one
357 still implies that the section takes up space in the output
359 bfd_section_list_clear (abfd
);
361 /* If this is the first dynamic object found in the link, create
362 the special sections required for dynamic linking. */
363 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
366 /* Find the name to use in a DT_NEEDED entry that refers to this
367 object. If the object has a DT_SONAME entry, we use it.
368 Otherwise, if the generic linker stuck something in
369 elf_dt_name, we use that. Otherwise, we just use the file
371 if (soname
== NULL
|| *soname
== '\0')
373 soname
= elf_dt_name (abfd
);
374 if (soname
== NULL
|| *soname
== '\0')
375 soname
= bfd_get_filename (abfd
);
378 /* Save the SONAME because sometimes the linker emulation code
379 will need to know it. */
380 elf_dt_name (abfd
) = soname
;
382 ret
= _bfd_elf_add_dt_needed_tag (info
, soname
, add_needed
);
386 /* If we have already included this dynamic object in the
387 link, just ignore it. There is no reason to include a
388 particular dynamic object more than once. */
393 /* If this is a dynamic object, we always link against the .dynsym
394 symbol table, not the .symtab symbol table. The dynamic linker
395 will only see the .dynsym symbol table, so there is no reason to
396 look at .symtab for a dynamic object. */
398 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
399 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
401 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
403 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
405 /* The sh_info field of the symtab header tells us where the
406 external symbols start. We don't care about the local symbols at
408 if (elf_bad_symtab (abfd
))
410 extsymcount
= symcount
;
415 extsymcount
= symcount
- hdr
->sh_info
;
416 extsymoff
= hdr
->sh_info
;
420 if (extsymcount
!= 0)
422 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
427 /* We store a pointer to the hash table entry for each external
429 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
430 sym_hash
= bfd_alloc (abfd
, amt
);
431 if (sym_hash
== NULL
)
433 elf_sym_hashes (abfd
) = sym_hash
;
438 /* Read in any version definitions. */
439 if (! _bfd_elf_slurp_version_tables (abfd
))
442 /* Read in the symbol versions, but don't bother to convert them
443 to internal format. */
444 if (elf_dynversym (abfd
) != 0)
446 Elf_Internal_Shdr
*versymhdr
;
448 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
449 extversym
= bfd_malloc (versymhdr
->sh_size
);
450 if (extversym
== NULL
)
452 amt
= versymhdr
->sh_size
;
453 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
454 || bfd_bread (extversym
, amt
, abfd
) != amt
)
455 goto error_free_vers
;
461 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
462 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
464 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
471 struct elf_link_hash_entry
*h
;
472 bfd_boolean definition
;
473 bfd_boolean size_change_ok
;
474 bfd_boolean type_change_ok
;
475 bfd_boolean new_weakdef
;
476 bfd_boolean override
;
477 unsigned int old_alignment
;
482 flags
= BSF_NO_FLAGS
;
484 value
= isym
->st_value
;
487 bind
= ELF_ST_BIND (isym
->st_info
);
488 if (bind
== STB_LOCAL
)
490 /* This should be impossible, since ELF requires that all
491 global symbols follow all local symbols, and that sh_info
492 point to the first global symbol. Unfortunately, Irix 5
496 else if (bind
== STB_GLOBAL
)
498 if (isym
->st_shndx
!= SHN_UNDEF
499 && isym
->st_shndx
!= SHN_COMMON
)
502 else if (bind
== STB_WEAK
)
506 /* Leave it up to the processor backend. */
509 if (isym
->st_shndx
== SHN_UNDEF
)
510 sec
= bfd_und_section_ptr
;
511 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
513 sec
= section_from_elf_index (abfd
, isym
->st_shndx
);
515 sec
= bfd_abs_section_ptr
;
516 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
519 else if (isym
->st_shndx
== SHN_ABS
)
520 sec
= bfd_abs_section_ptr
;
521 else if (isym
->st_shndx
== SHN_COMMON
)
523 sec
= bfd_com_section_ptr
;
524 /* What ELF calls the size we call the value. What ELF
525 calls the value we call the alignment. */
526 value
= isym
->st_size
;
530 /* Leave it up to the processor backend. */
533 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
536 goto error_free_vers
;
538 if (isym
->st_shndx
== SHN_COMMON
539 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
541 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
545 tcomm
= bfd_make_section (abfd
, ".tcommon");
547 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
550 | SEC_THREAD_LOCAL
)))
551 goto error_free_vers
;
555 else if (add_symbol_hook
)
557 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
559 goto error_free_vers
;
561 /* The hook function sets the name to NULL if this symbol
562 should be skipped for some reason. */
567 /* Sanity check that all possibilities were handled. */
570 bfd_set_error (bfd_error_bad_value
);
571 goto error_free_vers
;
574 if (bfd_is_und_section (sec
)
575 || bfd_is_com_section (sec
))
580 size_change_ok
= FALSE
;
581 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
585 if (is_elf_hash_table (hash_table
))
587 Elf_Internal_Versym iver
;
588 unsigned int vernum
= 0;
593 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
594 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
596 /* If this is a hidden symbol, or if it is not version
597 1, we append the version name to the symbol name.
598 However, we do not modify a non-hidden absolute
599 symbol, because it might be the version symbol
600 itself. FIXME: What if it isn't? */
601 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
602 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
605 size_t namelen
, verlen
, newlen
;
608 if (isym
->st_shndx
!= SHN_UNDEF
)
610 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
612 (*_bfd_error_handler
)
613 (_("%s: %s: invalid version %u (max %d)"),
614 bfd_archive_filename (abfd
), name
, vernum
,
615 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
616 bfd_set_error (bfd_error_bad_value
);
617 goto error_free_vers
;
621 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
627 /* We cannot simply test for the number of
628 entries in the VERNEED section since the
629 numbers for the needed versions do not start
631 Elf_Internal_Verneed
*t
;
634 for (t
= elf_tdata (abfd
)->verref
;
638 Elf_Internal_Vernaux
*a
;
640 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
642 if (a
->vna_other
== vernum
)
644 verstr
= a
->vna_nodename
;
653 (*_bfd_error_handler
)
654 (_("%s: %s: invalid needed version %d"),
655 bfd_archive_filename (abfd
), name
, vernum
);
656 bfd_set_error (bfd_error_bad_value
);
657 goto error_free_vers
;
661 namelen
= strlen (name
);
662 verlen
= strlen (verstr
);
663 newlen
= namelen
+ verlen
+ 2;
664 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
665 && isym
->st_shndx
!= SHN_UNDEF
)
668 newname
= bfd_alloc (abfd
, newlen
);
670 goto error_free_vers
;
671 memcpy (newname
, name
, namelen
);
672 p
= newname
+ namelen
;
674 /* If this is a defined non-hidden version symbol,
675 we add another @ to the name. This indicates the
676 default version of the symbol. */
677 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
678 && isym
->st_shndx
!= SHN_UNDEF
)
680 memcpy (p
, verstr
, verlen
+ 1);
686 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
687 sym_hash
, &skip
, &override
,
688 &type_change_ok
, &size_change_ok
))
689 goto error_free_vers
;
698 while (h
->root
.type
== bfd_link_hash_indirect
699 || h
->root
.type
== bfd_link_hash_warning
)
700 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
702 /* Remember the old alignment if this is a common symbol, so
703 that we don't reduce the alignment later on. We can't
704 check later, because _bfd_generic_link_add_one_symbol
705 will set a default for the alignment which we want to
706 override. We also remember the old bfd where the existing
707 definition comes from. */
708 switch (h
->root
.type
)
713 case bfd_link_hash_defined
:
714 case bfd_link_hash_defweak
:
715 old_bfd
= h
->root
.u
.def
.section
->owner
;
718 case bfd_link_hash_common
:
719 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
720 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
724 if (elf_tdata (abfd
)->verdef
!= NULL
728 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
731 if (! (_bfd_generic_link_add_one_symbol
732 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
733 (struct bfd_link_hash_entry
**) sym_hash
)))
734 goto error_free_vers
;
737 while (h
->root
.type
== bfd_link_hash_indirect
738 || h
->root
.type
== bfd_link_hash_warning
)
739 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
745 && (flags
& BSF_WEAK
) != 0
746 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
747 && is_elf_hash_table (hash_table
)
748 && h
->weakdef
== NULL
)
750 /* Keep a list of all weak defined non function symbols from
751 a dynamic object, using the weakdef field. Later in this
752 function we will set the weakdef field to the correct
753 value. We only put non-function symbols from dynamic
754 objects on this list, because that happens to be the only
755 time we need to know the normal symbol corresponding to a
756 weak symbol, and the information is time consuming to
757 figure out. If the weakdef field is not already NULL,
758 then this symbol was already defined by some previous
759 dynamic object, and we will be using that previous
760 definition anyhow. */
767 /* Set the alignment of a common symbol. */
768 if (isym
->st_shndx
== SHN_COMMON
769 && h
->root
.type
== bfd_link_hash_common
)
773 align
= bfd_log2 (isym
->st_value
);
774 if (align
> old_alignment
775 /* Permit an alignment power of zero if an alignment of one
776 is specified and no other alignments have been specified. */
777 || (isym
->st_value
== 1 && old_alignment
== 0))
778 h
->root
.u
.c
.p
->alignment_power
= align
;
780 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
783 if (is_elf_hash_table (hash_table
))
789 /* Check the alignment when a common symbol is involved. This
790 can change when a common symbol is overridden by a normal
791 definition or a common symbol is ignored due to the old
792 normal definition. We need to make sure the maximum
793 alignment is maintained. */
794 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
795 && h
->root
.type
!= bfd_link_hash_common
)
797 unsigned int common_align
;
798 unsigned int normal_align
;
799 unsigned int symbol_align
;
803 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
804 if (h
->root
.u
.def
.section
->owner
!= NULL
805 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
807 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
808 if (normal_align
> symbol_align
)
809 normal_align
= symbol_align
;
812 normal_align
= symbol_align
;
816 common_align
= old_alignment
;
817 common_bfd
= old_bfd
;
822 common_align
= bfd_log2 (isym
->st_value
);
824 normal_bfd
= old_bfd
;
827 if (normal_align
< common_align
)
828 (*_bfd_error_handler
)
829 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
832 bfd_archive_filename (normal_bfd
),
834 bfd_archive_filename (common_bfd
));
837 /* Remember the symbol size and type. */
838 if (isym
->st_size
!= 0
839 && (definition
|| h
->size
== 0))
841 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
842 (*_bfd_error_handler
)
843 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
844 name
, (unsigned long) h
->size
,
845 bfd_archive_filename (old_bfd
),
846 (unsigned long) isym
->st_size
,
847 bfd_archive_filename (abfd
));
849 h
->size
= isym
->st_size
;
852 /* If this is a common symbol, then we always want H->SIZE
853 to be the size of the common symbol. The code just above
854 won't fix the size if a common symbol becomes larger. We
855 don't warn about a size change here, because that is
856 covered by --warn-common. */
857 if (h
->root
.type
== bfd_link_hash_common
)
858 h
->size
= h
->root
.u
.c
.size
;
860 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
861 && (definition
|| h
->type
== STT_NOTYPE
))
863 if (h
->type
!= STT_NOTYPE
864 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
866 (*_bfd_error_handler
)
867 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
868 name
, h
->type
, ELF_ST_TYPE (isym
->st_info
),
869 bfd_archive_filename (abfd
));
871 h
->type
= ELF_ST_TYPE (isym
->st_info
);
874 /* If st_other has a processor-specific meaning, specific
875 code might be needed here. We never merge the visibility
876 attribute with the one from a dynamic object. */
877 if (bed
->elf_backend_merge_symbol_attribute
)
878 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
881 if (isym
->st_other
!= 0 && !dynamic
)
883 unsigned char hvis
, symvis
, other
, nvis
;
885 /* Take the balance of OTHER from the definition. */
886 other
= (definition
? isym
->st_other
: h
->other
);
887 other
&= ~ ELF_ST_VISIBILITY (-1);
889 /* Combine visibilities, using the most constraining one. */
890 hvis
= ELF_ST_VISIBILITY (h
->other
);
891 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
897 nvis
= hvis
< symvis
? hvis
: symvis
;
899 h
->other
= other
| nvis
;
902 /* Set a flag in the hash table entry indicating the type of
903 reference or definition we just found. Keep a count of
904 the number of dynamic symbols we find. A dynamic symbol
905 is one which is referenced or defined by both a regular
906 object and a shared object. */
907 old_flags
= h
->elf_link_hash_flags
;
913 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
914 if (bind
!= STB_WEAK
)
915 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
918 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
919 if (! info
->executable
920 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
921 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
927 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
929 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
930 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
931 | ELF_LINK_HASH_REF_REGULAR
)) != 0
932 || (h
->weakdef
!= NULL
934 && h
->weakdef
->dynindx
!= -1))
938 h
->elf_link_hash_flags
|= new_flag
;
940 /* Check to see if we need to add an indirect symbol for
942 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
943 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
944 &sec
, &value
, &dynsym
,
946 goto error_free_vers
;
948 if (definition
&& !dynamic
)
950 char *p
= strchr (name
, ELF_VER_CHR
);
951 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
953 /* Queue non-default versions so that .symver x, x@FOO
954 aliases can be checked. */
957 amt
= (isymend
- isym
+ 1)
958 * sizeof (struct elf_link_hash_entry
*);
959 nondeflt_vers
= bfd_malloc (amt
);
961 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
965 if (dynsym
&& h
->dynindx
== -1)
967 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
968 goto error_free_vers
;
969 if (h
->weakdef
!= NULL
971 && h
->weakdef
->dynindx
== -1)
973 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
974 goto error_free_vers
;
977 else if (dynsym
&& h
->dynindx
!= -1)
978 /* If the symbol already has a dynamic index, but
979 visibility says it should not be visible, turn it into
981 switch (ELF_ST_VISIBILITY (h
->other
))
985 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
993 && (h
->elf_link_hash_flags
994 & ELF_LINK_HASH_REF_REGULAR
) != 0)
997 const char *soname
= elf_dt_name (abfd
);
999 /* A symbol from a library loaded via DT_NEEDED of some
1000 other library is referenced by a regular object.
1001 Add a DT_NEEDED entry for it. */
1003 ret
= _bfd_elf_add_dt_needed_tag (info
, soname
, add_needed
);
1005 goto error_free_vers
;
1007 BFD_ASSERT (ret
== 0);
1012 /* Now that all the symbols from this input file are created, handle
1013 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
1014 if (nondeflt_vers
!= NULL
)
1016 bfd_size_type cnt
, symidx
;
1018 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
1020 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
1021 char *shortname
, *p
;
1023 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1025 || (h
->root
.type
!= bfd_link_hash_defined
1026 && h
->root
.type
!= bfd_link_hash_defweak
))
1029 amt
= p
- h
->root
.root
.string
;
1030 shortname
= bfd_malloc (amt
+ 1);
1031 memcpy (shortname
, h
->root
.root
.string
, amt
);
1032 shortname
[amt
] = '\0';
1034 hi
= (struct elf_link_hash_entry
*)
1035 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
1036 FALSE
, FALSE
, FALSE
);
1038 && hi
->root
.type
== h
->root
.type
1039 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
1040 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
1042 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1043 hi
->root
.type
= bfd_link_hash_indirect
;
1044 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
1045 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1046 sym_hash
= elf_sym_hashes (abfd
);
1048 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
1049 if (sym_hash
[symidx
] == hi
)
1051 sym_hash
[symidx
] = h
;
1057 free (nondeflt_vers
);
1058 nondeflt_vers
= NULL
;
1061 if (extversym
!= NULL
)
1067 if (isymbuf
!= NULL
)
1071 /* Now set the weakdefs field correctly for all the weak defined
1072 symbols we found. The only way to do this is to search all the
1073 symbols. Since we only need the information for non functions in
1074 dynamic objects, that's the only time we actually put anything on
1075 the list WEAKS. We need this information so that if a regular
1076 object refers to a symbol defined weakly in a dynamic object, the
1077 real symbol in the dynamic object is also put in the dynamic
1078 symbols; we also must arrange for both symbols to point to the
1079 same memory location. We could handle the general case of symbol
1080 aliasing, but a general symbol alias can only be generated in
1081 assembler code, handling it correctly would be very time
1082 consuming, and other ELF linkers don't handle general aliasing
1086 struct elf_link_hash_entry
**hpp
;
1087 struct elf_link_hash_entry
**hppend
;
1088 struct elf_link_hash_entry
**sorted_sym_hash
;
1089 struct elf_link_hash_entry
*h
;
1092 /* Since we have to search the whole symbol list for each weak
1093 defined symbol, search time for N weak defined symbols will be
1094 O(N^2). Binary search will cut it down to O(NlogN). */
1095 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
1096 sorted_sym_hash
= bfd_malloc (amt
);
1097 if (sorted_sym_hash
== NULL
)
1099 sym_hash
= sorted_sym_hash
;
1100 hpp
= elf_sym_hashes (abfd
);
1101 hppend
= hpp
+ extsymcount
;
1103 for (; hpp
< hppend
; hpp
++)
1107 && h
->root
.type
== bfd_link_hash_defined
1108 && h
->type
!= STT_FUNC
)
1116 qsort (sorted_sym_hash
, sym_count
,
1117 sizeof (struct elf_link_hash_entry
*),
1118 _bfd_elf_sort_symbol
);
1120 while (weaks
!= NULL
)
1122 struct elf_link_hash_entry
*hlook
;
1129 weaks
= hlook
->weakdef
;
1130 hlook
->weakdef
= NULL
;
1132 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
1133 || hlook
->root
.type
== bfd_link_hash_defweak
1134 || hlook
->root
.type
== bfd_link_hash_common
1135 || hlook
->root
.type
== bfd_link_hash_indirect
);
1136 slook
= hlook
->root
.u
.def
.section
;
1137 vlook
= hlook
->root
.u
.def
.value
;
1144 bfd_signed_vma vdiff
;
1146 h
= sorted_sym_hash
[idx
];
1147 vdiff
= vlook
- h
->root
.u
.def
.value
;
1154 long sdiff
= slook
- h
->root
.u
.def
.section
;
1167 /* We didn't find a value/section match. */
1171 for (i
= ilook
; i
< sym_count
; i
++)
1173 h
= sorted_sym_hash
[i
];
1175 /* Stop if value or section doesn't match. */
1176 if (h
->root
.u
.def
.value
!= vlook
1177 || h
->root
.u
.def
.section
!= slook
)
1179 else if (h
!= hlook
)
1183 /* If the weak definition is in the list of dynamic
1184 symbols, make sure the real definition is put
1186 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
1188 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1193 /* If the real definition is in the list of dynamic
1194 symbols, make sure the weak definition is put
1195 there as well. If we don't do this, then the
1196 dynamic loader might not merge the entries for the
1197 real definition and the weak definition. */
1198 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
1200 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1209 free (sorted_sym_hash
);
1212 /* If this object is the same format as the output object, and it is
1213 not a shared library, then let the backend look through the
1216 This is required to build global offset table entries and to
1217 arrange for dynamic relocs. It is not required for the
1218 particular common case of linking non PIC code, even when linking
1219 against shared libraries, but unfortunately there is no way of
1220 knowing whether an object file has been compiled PIC or not.
1221 Looking through the relocs is not particularly time consuming.
1222 The problem is that we must either (1) keep the relocs in memory,
1223 which causes the linker to require additional runtime memory or
1224 (2) read the relocs twice from the input file, which wastes time.
1225 This would be a good case for using mmap.
1227 I have no idea how to handle linking PIC code into a file of a
1228 different format. It probably can't be done. */
1229 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
1231 && is_elf_hash_table (hash_table
)
1232 && hash_table
->root
.creator
== abfd
->xvec
1233 && check_relocs
!= NULL
)
1237 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1239 Elf_Internal_Rela
*internal_relocs
;
1242 if ((o
->flags
& SEC_RELOC
) == 0
1243 || o
->reloc_count
== 0
1244 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
1245 && (o
->flags
& SEC_DEBUGGING
) != 0)
1246 || bfd_is_abs_section (o
->output_section
))
1249 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
1251 if (internal_relocs
== NULL
)
1254 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
1256 if (elf_section_data (o
)->relocs
!= internal_relocs
)
1257 free (internal_relocs
);
1264 /* If this is a non-traditional link, try to optimize the handling
1265 of the .stab/.stabstr sections. */
1267 && ! info
->traditional_format
1268 && is_elf_hash_table (hash_table
)
1269 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
1273 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
1274 if (stabstr
!= NULL
)
1276 bfd_size_type string_offset
= 0;
1279 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
1280 if (strncmp (".stab", stab
->name
, 5) == 0
1281 && (!stab
->name
[5] ||
1282 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
1283 && (stab
->flags
& SEC_MERGE
) == 0
1284 && !bfd_is_abs_section (stab
->output_section
))
1286 struct bfd_elf_section_data
*secdata
;
1288 secdata
= elf_section_data (stab
);
1289 if (! _bfd_link_section_stabs (abfd
,
1290 & hash_table
->stab_info
,
1295 if (secdata
->sec_info
)
1296 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
1301 if (! info
->relocatable
1303 && is_elf_hash_table (hash_table
))
1307 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
1308 if ((s
->flags
& SEC_MERGE
) != 0
1309 && !bfd_is_abs_section (s
->output_section
))
1311 struct bfd_elf_section_data
*secdata
;
1313 secdata
= elf_section_data (s
);
1314 if (! _bfd_merge_section (abfd
,
1315 & hash_table
->merge_info
,
1316 s
, &secdata
->sec_info
))
1318 else if (secdata
->sec_info
)
1319 s
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
1323 if (is_elf_hash_table (hash_table
))
1325 /* Add this bfd to the loaded list. */
1326 struct elf_link_loaded_list
*n
;
1328 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
1332 n
->next
= hash_table
->loaded
;
1333 hash_table
->loaded
= n
;
1339 if (nondeflt_vers
!= NULL
)
1340 free (nondeflt_vers
);
1341 if (extversym
!= NULL
)
1344 if (isymbuf
!= NULL
)
1350 /* Final phase of ELF linker. */
1352 /* A structure we use to avoid passing large numbers of arguments. */
1354 struct elf_final_link_info
1356 /* General link information. */
1357 struct bfd_link_info
*info
;
1360 /* Symbol string table. */
1361 struct bfd_strtab_hash
*symstrtab
;
1362 /* .dynsym section. */
1363 asection
*dynsym_sec
;
1364 /* .hash section. */
1366 /* symbol version section (.gnu.version). */
1367 asection
*symver_sec
;
1368 /* Buffer large enough to hold contents of any section. */
1370 /* Buffer large enough to hold external relocs of any section. */
1371 void *external_relocs
;
1372 /* Buffer large enough to hold internal relocs of any section. */
1373 Elf_Internal_Rela
*internal_relocs
;
1374 /* Buffer large enough to hold external local symbols of any input
1376 Elf_External_Sym
*external_syms
;
1377 /* And a buffer for symbol section indices. */
1378 Elf_External_Sym_Shndx
*locsym_shndx
;
1379 /* Buffer large enough to hold internal local symbols of any input
1381 Elf_Internal_Sym
*internal_syms
;
1382 /* Array large enough to hold a symbol index for each local symbol
1383 of any input BFD. */
1385 /* Array large enough to hold a section pointer for each local
1386 symbol of any input BFD. */
1387 asection
**sections
;
1388 /* Buffer to hold swapped out symbols. */
1389 Elf_External_Sym
*symbuf
;
1390 /* And one for symbol section indices. */
1391 Elf_External_Sym_Shndx
*symshndxbuf
;
1392 /* Number of swapped out symbols in buffer. */
1393 size_t symbuf_count
;
1394 /* Number of symbols which fit in symbuf. */
1396 /* And same for symshndxbuf. */
1397 size_t shndxbuf_size
;
1400 static bfd_boolean elf_link_output_sym
1401 (struct elf_final_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
1402 struct elf_link_hash_entry
*);
1403 static bfd_boolean elf_link_flush_output_syms
1404 (struct elf_final_link_info
*);
1405 static bfd_boolean elf_link_output_extsym
1406 (struct elf_link_hash_entry
*, void *);
1407 static bfd_boolean elf_link_input_bfd
1408 (struct elf_final_link_info
*, bfd
*);
1409 static bfd_boolean elf_reloc_link_order
1410 (bfd
*, struct bfd_link_info
*, asection
*, struct bfd_link_order
*);
1412 /* This struct is used to pass information to elf_link_output_extsym. */
1414 struct elf_outext_info
1417 bfd_boolean localsyms
;
1418 struct elf_final_link_info
*finfo
;
1421 /* When performing a relocatable link, the input relocations are
1422 preserved. But, if they reference global symbols, the indices
1423 referenced must be updated. Update all the relocations in
1424 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
1427 elf_link_adjust_relocs (bfd
*abfd
,
1428 Elf_Internal_Shdr
*rel_hdr
,
1430 struct elf_link_hash_entry
**rel_hash
)
1433 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1435 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1436 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
1438 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
1440 swap_in
= bed
->s
->swap_reloc_in
;
1441 swap_out
= bed
->s
->swap_reloc_out
;
1443 else if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
))
1445 swap_in
= bed
->s
->swap_reloca_in
;
1446 swap_out
= bed
->s
->swap_reloca_out
;
1451 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
1454 erela
= rel_hdr
->contents
;
1455 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
1457 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
1460 if (*rel_hash
== NULL
)
1463 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
1465 (*swap_in
) (abfd
, erela
, irela
);
1466 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
1467 irela
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
1468 ELF_R_TYPE (irela
[j
].r_info
));
1469 (*swap_out
) (abfd
, irela
, erela
);
1473 struct elf_link_sort_rela
1476 enum elf_reloc_type_class type
;
1477 /* We use this as an array of size int_rels_per_ext_rel. */
1478 Elf_Internal_Rela rela
[1];
1482 elf_link_sort_cmp1 (const void *A
, const void *B
)
1484 const struct elf_link_sort_rela
*a
= A
;
1485 const struct elf_link_sort_rela
*b
= B
;
1486 int relativea
, relativeb
;
1488 relativea
= a
->type
== reloc_class_relative
;
1489 relativeb
= b
->type
== reloc_class_relative
;
1491 if (relativea
< relativeb
)
1493 if (relativea
> relativeb
)
1495 if (ELF_R_SYM (a
->rela
->r_info
) < ELF_R_SYM (b
->rela
->r_info
))
1497 if (ELF_R_SYM (a
->rela
->r_info
) > ELF_R_SYM (b
->rela
->r_info
))
1499 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
1501 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
1507 elf_link_sort_cmp2 (const void *A
, const void *B
)
1509 const struct elf_link_sort_rela
*a
= A
;
1510 const struct elf_link_sort_rela
*b
= B
;
1513 if (a
->offset
< b
->offset
)
1515 if (a
->offset
> b
->offset
)
1517 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
1518 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
1523 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
1525 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
1531 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
1534 bfd_size_type count
, size
;
1535 size_t i
, ret
, sort_elt
, ext_size
;
1536 bfd_byte
*sort
, *s_non_relative
, *p
;
1537 struct elf_link_sort_rela
*sq
;
1538 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1539 int i2e
= bed
->s
->int_rels_per_ext_rel
;
1540 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1541 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
1542 struct bfd_link_order
*lo
;
1544 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
1545 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
1547 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
1548 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
1550 ext_size
= sizeof (Elf_External_Rel
);
1551 swap_in
= bed
->s
->swap_reloc_in
;
1552 swap_out
= bed
->s
->swap_reloc_out
;
1556 ext_size
= sizeof (Elf_External_Rela
);
1557 swap_in
= bed
->s
->swap_reloca_in
;
1558 swap_out
= bed
->s
->swap_reloca_out
;
1560 count
= reldyn
->_raw_size
/ ext_size
;
1563 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
1564 if (lo
->type
== bfd_indirect_link_order
)
1566 asection
*o
= lo
->u
.indirect
.section
;
1567 size
+= o
->_raw_size
;
1570 if (size
!= reldyn
->_raw_size
)
1573 sort_elt
= (sizeof (struct elf_link_sort_rela
)
1574 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
1575 sort
= bfd_zmalloc (sort_elt
* count
);
1578 (*info
->callbacks
->warning
)
1579 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
1583 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
1584 if (lo
->type
== bfd_indirect_link_order
)
1586 bfd_byte
*erel
, *erelend
;
1587 asection
*o
= lo
->u
.indirect
.section
;
1590 erelend
= o
->contents
+ o
->_raw_size
;
1591 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
1592 while (erel
< erelend
)
1594 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
1595 (*swap_in
) (abfd
, erel
, s
->rela
);
1596 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
1602 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
1604 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
1606 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
1607 if (s
->type
!= reloc_class_relative
)
1613 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
1614 for (; i
< count
; i
++, p
+= sort_elt
)
1616 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
1617 if (ELF_R_SYM (sp
->rela
->r_info
) != ELF_R_SYM (sq
->rela
->r_info
))
1619 sp
->offset
= sq
->rela
->r_offset
;
1622 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
1624 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
1625 if (lo
->type
== bfd_indirect_link_order
)
1627 bfd_byte
*erel
, *erelend
;
1628 asection
*o
= lo
->u
.indirect
.section
;
1631 erelend
= o
->contents
+ o
->_raw_size
;
1632 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
1633 while (erel
< erelend
)
1635 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
1636 (*swap_out
) (abfd
, s
->rela
, erel
);
1647 /* Do the final step of an ELF link. */
1650 elf_bfd_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
1652 bfd_boolean dynamic
;
1653 bfd_boolean emit_relocs
;
1655 struct elf_final_link_info finfo
;
1656 register asection
*o
;
1657 register struct bfd_link_order
*p
;
1659 bfd_size_type max_contents_size
;
1660 bfd_size_type max_external_reloc_size
;
1661 bfd_size_type max_internal_reloc_count
;
1662 bfd_size_type max_sym_count
;
1663 bfd_size_type max_sym_shndx_count
;
1665 Elf_Internal_Sym elfsym
;
1667 Elf_Internal_Shdr
*symtab_hdr
;
1668 Elf_Internal_Shdr
*symtab_shndx_hdr
;
1669 Elf_Internal_Shdr
*symstrtab_hdr
;
1670 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1671 struct elf_outext_info eoinfo
;
1673 size_t relativecount
= 0;
1674 asection
*reldyn
= 0;
1677 if (! is_elf_hash_table (info
->hash
))
1681 abfd
->flags
|= DYNAMIC
;
1683 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
1684 dynobj
= elf_hash_table (info
)->dynobj
;
1686 emit_relocs
= (info
->relocatable
1687 || info
->emitrelocations
1688 || bed
->elf_backend_emit_relocs
);
1691 finfo
.output_bfd
= abfd
;
1692 finfo
.symstrtab
= elf_stringtab_init ();
1693 if (finfo
.symstrtab
== NULL
)
1698 finfo
.dynsym_sec
= NULL
;
1699 finfo
.hash_sec
= NULL
;
1700 finfo
.symver_sec
= NULL
;
1704 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
1705 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
1706 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
1707 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
1708 /* Note that it is OK if symver_sec is NULL. */
1711 finfo
.contents
= NULL
;
1712 finfo
.external_relocs
= NULL
;
1713 finfo
.internal_relocs
= NULL
;
1714 finfo
.external_syms
= NULL
;
1715 finfo
.locsym_shndx
= NULL
;
1716 finfo
.internal_syms
= NULL
;
1717 finfo
.indices
= NULL
;
1718 finfo
.sections
= NULL
;
1719 finfo
.symbuf
= NULL
;
1720 finfo
.symshndxbuf
= NULL
;
1721 finfo
.symbuf_count
= 0;
1722 finfo
.shndxbuf_size
= 0;
1724 /* Count up the number of relocations we will output for each output
1725 section, so that we know the sizes of the reloc sections. We
1726 also figure out some maximum sizes. */
1727 max_contents_size
= 0;
1728 max_external_reloc_size
= 0;
1729 max_internal_reloc_count
= 0;
1731 max_sym_shndx_count
= 0;
1733 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1735 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
1738 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
1740 unsigned int reloc_count
= 0;
1741 struct bfd_elf_section_data
*esdi
= NULL
;
1742 unsigned int *rel_count1
;
1744 if (p
->type
== bfd_section_reloc_link_order
1745 || p
->type
== bfd_symbol_reloc_link_order
)
1747 else if (p
->type
== bfd_indirect_link_order
)
1751 sec
= p
->u
.indirect
.section
;
1752 esdi
= elf_section_data (sec
);
1754 /* Mark all sections which are to be included in the
1755 link. This will normally be every section. We need
1756 to do this so that we can identify any sections which
1757 the linker has decided to not include. */
1758 sec
->linker_mark
= TRUE
;
1760 if (sec
->flags
& SEC_MERGE
)
1763 if (info
->relocatable
|| info
->emitrelocations
)
1764 reloc_count
= sec
->reloc_count
;
1765 else if (bed
->elf_backend_count_relocs
)
1767 Elf_Internal_Rela
* relocs
;
1769 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
1772 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
1774 if (elf_section_data (o
)->relocs
!= relocs
)
1778 if (sec
->_raw_size
> max_contents_size
)
1779 max_contents_size
= sec
->_raw_size
;
1780 if (sec
->_cooked_size
> max_contents_size
)
1781 max_contents_size
= sec
->_cooked_size
;
1783 /* We are interested in just local symbols, not all
1785 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
1786 && (sec
->owner
->flags
& DYNAMIC
) == 0)
1790 if (elf_bad_symtab (sec
->owner
))
1791 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
1792 / sizeof (Elf_External_Sym
));
1794 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
1796 if (sym_count
> max_sym_count
)
1797 max_sym_count
= sym_count
;
1799 if (sym_count
> max_sym_shndx_count
1800 && elf_symtab_shndx (sec
->owner
) != 0)
1801 max_sym_shndx_count
= sym_count
;
1803 if ((sec
->flags
& SEC_RELOC
) != 0)
1807 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
1808 if (ext_size
> max_external_reloc_size
)
1809 max_external_reloc_size
= ext_size
;
1810 if (sec
->reloc_count
> max_internal_reloc_count
)
1811 max_internal_reloc_count
= sec
->reloc_count
;
1816 if (reloc_count
== 0)
1819 o
->reloc_count
+= reloc_count
;
1821 /* MIPS may have a mix of REL and RELA relocs on sections.
1822 To support this curious ABI we keep reloc counts in
1823 elf_section_data too. We must be careful to add the
1824 relocations from the input section to the right output
1825 count. FIXME: Get rid of one count. We have
1826 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
1827 rel_count1
= &esdo
->rel_count
;
1830 bfd_boolean same_size
;
1831 bfd_size_type entsize1
;
1833 entsize1
= esdi
->rel_hdr
.sh_entsize
;
1834 BFD_ASSERT (entsize1
== sizeof (Elf_External_Rel
)
1835 || entsize1
== sizeof (Elf_External_Rela
));
1836 same_size
= (!o
->use_rela_p
1837 == (entsize1
== sizeof (Elf_External_Rel
)));
1840 rel_count1
= &esdo
->rel_count2
;
1842 if (esdi
->rel_hdr2
!= NULL
)
1844 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
1845 unsigned int alt_count
;
1846 unsigned int *rel_count2
;
1848 BFD_ASSERT (entsize2
!= entsize1
1849 && (entsize2
== sizeof (Elf_External_Rel
)
1850 || entsize2
== sizeof (Elf_External_Rela
)));
1852 rel_count2
= &esdo
->rel_count2
;
1854 rel_count2
= &esdo
->rel_count
;
1856 /* The following is probably too simplistic if the
1857 backend counts output relocs unusually. */
1858 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
1859 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
1860 *rel_count2
+= alt_count
;
1861 reloc_count
-= alt_count
;
1864 *rel_count1
+= reloc_count
;
1867 if (o
->reloc_count
> 0)
1868 o
->flags
|= SEC_RELOC
;
1871 /* Explicitly clear the SEC_RELOC flag. The linker tends to
1872 set it (this is probably a bug) and if it is set
1873 assign_section_numbers will create a reloc section. */
1874 o
->flags
&=~ SEC_RELOC
;
1877 /* If the SEC_ALLOC flag is not set, force the section VMA to
1878 zero. This is done in elf_fake_sections as well, but forcing
1879 the VMA to 0 here will ensure that relocs against these
1880 sections are handled correctly. */
1881 if ((o
->flags
& SEC_ALLOC
) == 0
1882 && ! o
->user_set_vma
)
1886 if (! info
->relocatable
&& merged
)
1887 elf_link_hash_traverse (elf_hash_table (info
),
1888 _bfd_elf_link_sec_merge_syms
, abfd
);
1890 /* Figure out the file positions for everything but the symbol table
1891 and the relocs. We set symcount to force assign_section_numbers
1892 to create a symbol table. */
1893 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
1894 BFD_ASSERT (! abfd
->output_has_begun
);
1895 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
1898 /* That created the reloc sections. Set their sizes, and assign
1899 them file positions, and allocate some buffers. */
1900 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1902 if ((o
->flags
& SEC_RELOC
) != 0)
1904 if (!(_bfd_elf_link_size_reloc_section
1905 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
1908 if (elf_section_data (o
)->rel_hdr2
1909 && !(_bfd_elf_link_size_reloc_section
1910 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
1914 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
1915 to count upwards while actually outputting the relocations. */
1916 elf_section_data (o
)->rel_count
= 0;
1917 elf_section_data (o
)->rel_count2
= 0;
1920 _bfd_elf_assign_file_positions_for_relocs (abfd
);
1922 /* We have now assigned file positions for all the sections except
1923 .symtab and .strtab. We start the .symtab section at the current
1924 file position, and write directly to it. We build the .strtab
1925 section in memory. */
1926 bfd_get_symcount (abfd
) = 0;
1927 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1928 /* sh_name is set in prep_headers. */
1929 symtab_hdr
->sh_type
= SHT_SYMTAB
;
1930 /* sh_flags, sh_addr and sh_size all start off zero. */
1931 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
1932 /* sh_link is set in assign_section_numbers. */
1933 /* sh_info is set below. */
1934 /* sh_offset is set just below. */
1935 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
1937 off
= elf_tdata (abfd
)->next_file_pos
;
1938 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
1940 /* Note that at this point elf_tdata (abfd)->next_file_pos is
1941 incorrect. We do not yet know the size of the .symtab section.
1942 We correct next_file_pos below, after we do know the size. */
1944 /* Allocate a buffer to hold swapped out symbols. This is to avoid
1945 continuously seeking to the right position in the file. */
1946 if (! info
->keep_memory
|| max_sym_count
< 20)
1947 finfo
.symbuf_size
= 20;
1949 finfo
.symbuf_size
= max_sym_count
;
1950 amt
= finfo
.symbuf_size
;
1951 amt
*= sizeof (Elf_External_Sym
);
1952 finfo
.symbuf
= bfd_malloc (amt
);
1953 if (finfo
.symbuf
== NULL
)
1955 if (elf_numsections (abfd
) > SHN_LORESERVE
)
1957 /* Wild guess at number of output symbols. realloc'd as needed. */
1958 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
1959 finfo
.shndxbuf_size
= amt
;
1960 amt
*= sizeof (Elf_External_Sym_Shndx
);
1961 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
1962 if (finfo
.symshndxbuf
== NULL
)
1966 /* Start writing out the symbol table. The first symbol is always a
1968 if (info
->strip
!= strip_all
1971 elfsym
.st_value
= 0;
1974 elfsym
.st_other
= 0;
1975 elfsym
.st_shndx
= SHN_UNDEF
;
1976 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
1982 /* Some standard ELF linkers do this, but we don't because it causes
1983 bootstrap comparison failures. */
1984 /* Output a file symbol for the output file as the second symbol.
1985 We output this even if we are discarding local symbols, although
1986 I'm not sure if this is correct. */
1987 elfsym
.st_value
= 0;
1989 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
1990 elfsym
.st_other
= 0;
1991 elfsym
.st_shndx
= SHN_ABS
;
1992 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
1993 &elfsym
, bfd_abs_section_ptr
, NULL
))
1997 /* Output a symbol for each section. We output these even if we are
1998 discarding local symbols, since they are used for relocs. These
1999 symbols have no names. We store the index of each one in the
2000 index field of the section, so that we can find it again when
2001 outputting relocs. */
2002 if (info
->strip
!= strip_all
2006 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
2007 elfsym
.st_other
= 0;
2008 for (i
= 1; i
< elf_numsections (abfd
); i
++)
2010 o
= section_from_elf_index (abfd
, i
);
2012 o
->target_index
= bfd_get_symcount (abfd
);
2013 elfsym
.st_shndx
= i
;
2014 if (info
->relocatable
|| o
== NULL
)
2015 elfsym
.st_value
= 0;
2017 elfsym
.st_value
= o
->vma
;
2018 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
2020 if (i
== SHN_LORESERVE
- 1)
2021 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2025 /* Allocate some memory to hold information read in from the input
2027 if (max_contents_size
!= 0)
2029 finfo
.contents
= bfd_malloc (max_contents_size
);
2030 if (finfo
.contents
== NULL
)
2034 if (max_external_reloc_size
!= 0)
2036 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
2037 if (finfo
.external_relocs
== NULL
)
2041 if (max_internal_reloc_count
!= 0)
2043 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
2044 amt
*= sizeof (Elf_Internal_Rela
);
2045 finfo
.internal_relocs
= bfd_malloc (amt
);
2046 if (finfo
.internal_relocs
== NULL
)
2050 if (max_sym_count
!= 0)
2052 amt
= max_sym_count
* sizeof (Elf_External_Sym
);
2053 finfo
.external_syms
= bfd_malloc (amt
);
2054 if (finfo
.external_syms
== NULL
)
2057 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
2058 finfo
.internal_syms
= bfd_malloc (amt
);
2059 if (finfo
.internal_syms
== NULL
)
2062 amt
= max_sym_count
* sizeof (long);
2063 finfo
.indices
= bfd_malloc (amt
);
2064 if (finfo
.indices
== NULL
)
2067 amt
= max_sym_count
* sizeof (asection
*);
2068 finfo
.sections
= bfd_malloc (amt
);
2069 if (finfo
.sections
== NULL
)
2073 if (max_sym_shndx_count
!= 0)
2075 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
2076 finfo
.locsym_shndx
= bfd_malloc (amt
);
2077 if (finfo
.locsym_shndx
== NULL
)
2081 if (elf_hash_table (info
)->tls_sec
)
2083 bfd_vma base
, end
= 0;
2086 for (sec
= elf_hash_table (info
)->tls_sec
;
2087 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
2090 bfd_vma size
= sec
->_raw_size
;
2092 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
2094 struct bfd_link_order
*o
;
2096 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
2097 if (size
< o
->offset
+ o
->size
)
2098 size
= o
->offset
+ o
->size
;
2100 end
= sec
->vma
+ size
;
2102 base
= elf_hash_table (info
)->tls_sec
->vma
;
2103 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
2104 elf_hash_table (info
)->tls_size
= end
- base
;
2107 /* Since ELF permits relocations to be against local symbols, we
2108 must have the local symbols available when we do the relocations.
2109 Since we would rather only read the local symbols once, and we
2110 would rather not keep them in memory, we handle all the
2111 relocations for a single input file at the same time.
2113 Unfortunately, there is no way to know the total number of local
2114 symbols until we have seen all of them, and the local symbol
2115 indices precede the global symbol indices. This means that when
2116 we are generating relocatable output, and we see a reloc against
2117 a global symbol, we can not know the symbol index until we have
2118 finished examining all the local symbols to see which ones we are
2119 going to output. To deal with this, we keep the relocations in
2120 memory, and don't output them until the end of the link. This is
2121 an unfortunate waste of memory, but I don't see a good way around
2122 it. Fortunately, it only happens when performing a relocatable
2123 link, which is not the common case. FIXME: If keep_memory is set
2124 we could write the relocs out and then read them again; I don't
2125 know how bad the memory loss will be. */
2127 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2128 sub
->output_has_begun
= FALSE
;
2129 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2131 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
2133 if (p
->type
== bfd_indirect_link_order
2134 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
2135 == bfd_target_elf_flavour
)
2136 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
2138 if (! sub
->output_has_begun
)
2140 if (! elf_link_input_bfd (&finfo
, sub
))
2142 sub
->output_has_begun
= TRUE
;
2145 else if (p
->type
== bfd_section_reloc_link_order
2146 || p
->type
== bfd_symbol_reloc_link_order
)
2148 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
2153 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
2159 /* Output any global symbols that got converted to local in a
2160 version script or due to symbol visibility. We do this in a
2161 separate step since ELF requires all local symbols to appear
2162 prior to any global symbols. FIXME: We should only do this if
2163 some global symbols were, in fact, converted to become local.
2164 FIXME: Will this work correctly with the Irix 5 linker? */
2165 eoinfo
.failed
= FALSE
;
2166 eoinfo
.finfo
= &finfo
;
2167 eoinfo
.localsyms
= TRUE
;
2168 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
2173 /* That wrote out all the local symbols. Finish up the symbol table
2174 with the global symbols. Even if we want to strip everything we
2175 can, we still need to deal with those global symbols that got
2176 converted to local in a version script. */
2178 /* The sh_info field records the index of the first non local symbol. */
2179 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
2182 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
2184 Elf_Internal_Sym sym
;
2185 Elf_External_Sym
*dynsym
=
2186 (Elf_External_Sym
*) finfo
.dynsym_sec
->contents
;
2187 long last_local
= 0;
2189 /* Write out the section symbols for the output sections. */
2196 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
2199 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2202 Elf_External_Sym
*dest
;
2204 indx
= elf_section_data (s
)->this_idx
;
2205 BFD_ASSERT (indx
> 0);
2206 sym
.st_shndx
= indx
;
2207 sym
.st_value
= s
->vma
;
2208 dest
= dynsym
+ elf_section_data (s
)->dynindx
;
2209 elf_swap_symbol_out (abfd
, &sym
, dest
, 0);
2212 last_local
= bfd_count_sections (abfd
);
2215 /* Write out the local dynsyms. */
2216 if (elf_hash_table (info
)->dynlocal
)
2218 struct elf_link_local_dynamic_entry
*e
;
2219 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
2222 Elf_External_Sym
*dest
;
2224 sym
.st_size
= e
->isym
.st_size
;
2225 sym
.st_other
= e
->isym
.st_other
;
2227 /* Copy the internal symbol as is.
2228 Note that we saved a word of storage and overwrote
2229 the original st_name with the dynstr_index. */
2232 if (e
->isym
.st_shndx
!= SHN_UNDEF
2233 && (e
->isym
.st_shndx
< SHN_LORESERVE
2234 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
2236 s
= bfd_section_from_elf_index (e
->input_bfd
,
2240 elf_section_data (s
->output_section
)->this_idx
;
2241 sym
.st_value
= (s
->output_section
->vma
2243 + e
->isym
.st_value
);
2246 if (last_local
< e
->dynindx
)
2247 last_local
= e
->dynindx
;
2249 dest
= dynsym
+ e
->dynindx
;
2250 elf_swap_symbol_out (abfd
, &sym
, dest
, 0);
2254 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
2258 /* We get the global symbols from the hash table. */
2259 eoinfo
.failed
= FALSE
;
2260 eoinfo
.localsyms
= FALSE
;
2261 eoinfo
.finfo
= &finfo
;
2262 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
2267 /* If backend needs to output some symbols not present in the hash
2268 table, do it now. */
2269 if (bed
->elf_backend_output_arch_syms
)
2271 typedef bfd_boolean (*out_sym_func
)
2272 (void *, const char *, Elf_Internal_Sym
*, asection
*,
2273 struct elf_link_hash_entry
*);
2275 if (! ((*bed
->elf_backend_output_arch_syms
)
2276 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
2280 /* Flush all symbols to the file. */
2281 if (! elf_link_flush_output_syms (&finfo
))
2284 /* Now we know the size of the symtab section. */
2285 off
+= symtab_hdr
->sh_size
;
2287 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
2288 if (symtab_shndx_hdr
->sh_name
!= 0)
2290 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
2291 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
2292 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
2293 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
2294 symtab_shndx_hdr
->sh_size
= amt
;
2296 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
2299 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
2300 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
2305 /* Finish up and write out the symbol string table (.strtab)
2307 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2308 /* sh_name was set in prep_headers. */
2309 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2310 symstrtab_hdr
->sh_flags
= 0;
2311 symstrtab_hdr
->sh_addr
= 0;
2312 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
2313 symstrtab_hdr
->sh_entsize
= 0;
2314 symstrtab_hdr
->sh_link
= 0;
2315 symstrtab_hdr
->sh_info
= 0;
2316 /* sh_offset is set just below. */
2317 symstrtab_hdr
->sh_addralign
= 1;
2319 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
2320 elf_tdata (abfd
)->next_file_pos
= off
;
2322 if (bfd_get_symcount (abfd
) > 0)
2324 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
2325 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
2329 /* Adjust the relocs to have the correct symbol indices. */
2330 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2332 if ((o
->flags
& SEC_RELOC
) == 0)
2335 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
2336 elf_section_data (o
)->rel_count
,
2337 elf_section_data (o
)->rel_hashes
);
2338 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
2339 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
2340 elf_section_data (o
)->rel_count2
,
2341 (elf_section_data (o
)->rel_hashes
2342 + elf_section_data (o
)->rel_count
));
2344 /* Set the reloc_count field to 0 to prevent write_relocs from
2345 trying to swap the relocs out itself. */
2349 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
2350 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
2352 /* If we are linking against a dynamic object, or generating a
2353 shared library, finish up the dynamic linking information. */
2356 Elf_External_Dyn
*dyncon
, *dynconend
;
2358 /* Fix up .dynamic entries. */
2359 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
2360 BFD_ASSERT (o
!= NULL
);
2362 dyncon
= (Elf_External_Dyn
*) o
->contents
;
2363 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
2364 for (; dyncon
< dynconend
; dyncon
++)
2366 Elf_Internal_Dyn dyn
;
2370 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
2377 if (relativecount
> 0 && dyncon
+ 1 < dynconend
)
2379 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
2381 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
2382 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
2385 if (dyn
.d_tag
!= DT_NULL
)
2387 dyn
.d_un
.d_val
= relativecount
;
2388 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
2394 name
= info
->init_function
;
2397 name
= info
->fini_function
;
2400 struct elf_link_hash_entry
*h
;
2402 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
2403 FALSE
, FALSE
, TRUE
);
2405 && (h
->root
.type
== bfd_link_hash_defined
2406 || h
->root
.type
== bfd_link_hash_defweak
))
2408 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
2409 o
= h
->root
.u
.def
.section
;
2410 if (o
->output_section
!= NULL
)
2411 dyn
.d_un
.d_val
+= (o
->output_section
->vma
2412 + o
->output_offset
);
2415 /* The symbol is imported from another shared
2416 library and does not apply to this one. */
2420 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
2425 case DT_PREINIT_ARRAYSZ
:
2426 name
= ".preinit_array";
2428 case DT_INIT_ARRAYSZ
:
2429 name
= ".init_array";
2431 case DT_FINI_ARRAYSZ
:
2432 name
= ".fini_array";
2434 o
= bfd_get_section_by_name (abfd
, name
);
2437 (*_bfd_error_handler
)
2438 (_("%s: could not find output section %s"),
2439 bfd_get_filename (abfd
), name
);
2442 if (o
->_raw_size
== 0)
2443 (*_bfd_error_handler
)
2444 (_("warning: %s section has zero size"), name
);
2445 dyn
.d_un
.d_val
= o
->_raw_size
;
2446 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
2449 case DT_PREINIT_ARRAY
:
2450 name
= ".preinit_array";
2453 name
= ".init_array";
2456 name
= ".fini_array";
2469 name
= ".gnu.version_d";
2472 name
= ".gnu.version_r";
2475 name
= ".gnu.version";
2477 o
= bfd_get_section_by_name (abfd
, name
);
2480 (*_bfd_error_handler
)
2481 (_("%s: could not find output section %s"),
2482 bfd_get_filename (abfd
), name
);
2485 dyn
.d_un
.d_ptr
= o
->vma
;
2486 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
2493 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
2498 for (i
= 1; i
< elf_numsections (abfd
); i
++)
2500 Elf_Internal_Shdr
*hdr
;
2502 hdr
= elf_elfsections (abfd
)[i
];
2503 if (hdr
->sh_type
== type
2504 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
2506 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
2507 dyn
.d_un
.d_val
+= hdr
->sh_size
;
2510 if (dyn
.d_un
.d_val
== 0
2511 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
2512 dyn
.d_un
.d_val
= hdr
->sh_addr
;
2516 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
2522 /* If we have created any dynamic sections, then output them. */
2525 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
2528 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
2530 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2531 || o
->_raw_size
== 0
2532 || o
->output_section
== bfd_abs_section_ptr
)
2534 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
2536 /* At this point, we are only interested in sections
2537 created by _bfd_elf_link_create_dynamic_sections. */
2540 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
2542 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
2544 if (! bfd_set_section_contents (abfd
, o
->output_section
,
2546 (file_ptr
) o
->output_offset
,
2552 /* The contents of the .dynstr section are actually in a
2554 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
2555 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
2556 || ! _bfd_elf_strtab_emit (abfd
,
2557 elf_hash_table (info
)->dynstr
))
2563 if (info
->relocatable
)
2565 bfd_boolean failed
= FALSE
;
2567 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
2572 /* If we have optimized stabs strings, output them. */
2573 if (elf_hash_table (info
)->stab_info
!= NULL
)
2575 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
2579 if (info
->eh_frame_hdr
)
2581 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
2585 if (finfo
.symstrtab
!= NULL
)
2586 _bfd_stringtab_free (finfo
.symstrtab
);
2587 if (finfo
.contents
!= NULL
)
2588 free (finfo
.contents
);
2589 if (finfo
.external_relocs
!= NULL
)
2590 free (finfo
.external_relocs
);
2591 if (finfo
.internal_relocs
!= NULL
)
2592 free (finfo
.internal_relocs
);
2593 if (finfo
.external_syms
!= NULL
)
2594 free (finfo
.external_syms
);
2595 if (finfo
.locsym_shndx
!= NULL
)
2596 free (finfo
.locsym_shndx
);
2597 if (finfo
.internal_syms
!= NULL
)
2598 free (finfo
.internal_syms
);
2599 if (finfo
.indices
!= NULL
)
2600 free (finfo
.indices
);
2601 if (finfo
.sections
!= NULL
)
2602 free (finfo
.sections
);
2603 if (finfo
.symbuf
!= NULL
)
2604 free (finfo
.symbuf
);
2605 if (finfo
.symshndxbuf
!= NULL
)
2606 free (finfo
.symshndxbuf
);
2607 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2609 if ((o
->flags
& SEC_RELOC
) != 0
2610 && elf_section_data (o
)->rel_hashes
!= NULL
)
2611 free (elf_section_data (o
)->rel_hashes
);
2614 elf_tdata (abfd
)->linker
= TRUE
;
2619 if (finfo
.symstrtab
!= NULL
)
2620 _bfd_stringtab_free (finfo
.symstrtab
);
2621 if (finfo
.contents
!= NULL
)
2622 free (finfo
.contents
);
2623 if (finfo
.external_relocs
!= NULL
)
2624 free (finfo
.external_relocs
);
2625 if (finfo
.internal_relocs
!= NULL
)
2626 free (finfo
.internal_relocs
);
2627 if (finfo
.external_syms
!= NULL
)
2628 free (finfo
.external_syms
);
2629 if (finfo
.locsym_shndx
!= NULL
)
2630 free (finfo
.locsym_shndx
);
2631 if (finfo
.internal_syms
!= NULL
)
2632 free (finfo
.internal_syms
);
2633 if (finfo
.indices
!= NULL
)
2634 free (finfo
.indices
);
2635 if (finfo
.sections
!= NULL
)
2636 free (finfo
.sections
);
2637 if (finfo
.symbuf
!= NULL
)
2638 free (finfo
.symbuf
);
2639 if (finfo
.symshndxbuf
!= NULL
)
2640 free (finfo
.symshndxbuf
);
2641 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2643 if ((o
->flags
& SEC_RELOC
) != 0
2644 && elf_section_data (o
)->rel_hashes
!= NULL
)
2645 free (elf_section_data (o
)->rel_hashes
);
2651 /* Add a symbol to the output symbol table. */
2654 elf_link_output_sym (struct elf_final_link_info
*finfo
,
2656 Elf_Internal_Sym
*elfsym
,
2657 asection
*input_sec
,
2658 struct elf_link_hash_entry
*h
)
2660 Elf_External_Sym
*dest
;
2661 Elf_External_Sym_Shndx
*destshndx
;
2662 bfd_boolean (*output_symbol_hook
)
2663 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
2664 struct elf_link_hash_entry
*);
2666 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
2667 elf_backend_link_output_symbol_hook
;
2668 if (output_symbol_hook
!= NULL
)
2670 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
2674 if (name
== NULL
|| *name
== '\0')
2675 elfsym
->st_name
= 0;
2676 else if (input_sec
->flags
& SEC_EXCLUDE
)
2677 elfsym
->st_name
= 0;
2680 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
2682 if (elfsym
->st_name
== (unsigned long) -1)
2686 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
2688 if (! elf_link_flush_output_syms (finfo
))
2692 dest
= finfo
->symbuf
+ finfo
->symbuf_count
;
2693 destshndx
= finfo
->symshndxbuf
;
2694 if (destshndx
!= NULL
)
2696 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
2700 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
2701 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
2702 if (destshndx
== NULL
)
2704 memset ((char *) destshndx
+ amt
, 0, amt
);
2705 finfo
->shndxbuf_size
*= 2;
2707 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
2710 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
2711 finfo
->symbuf_count
+= 1;
2712 bfd_get_symcount (finfo
->output_bfd
) += 1;
2717 /* Flush the output symbols to the file. */
2720 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
)
2722 if (finfo
->symbuf_count
> 0)
2724 Elf_Internal_Shdr
*hdr
;
2728 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
2729 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
2730 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
2731 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
2732 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
2735 hdr
->sh_size
+= amt
;
2736 finfo
->symbuf_count
= 0;
2742 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
2743 allowing an unsatisfied unversioned symbol in the DSO to match a
2744 versioned symbol that would normally require an explicit version.
2745 We also handle the case that a DSO references a hidden symbol
2746 which may be satisfied by a versioned symbol in another DSO. */
2749 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
2750 struct elf_link_hash_entry
*h
)
2753 struct elf_link_loaded_list
*loaded
;
2755 if (!is_elf_hash_table (info
->hash
))
2758 switch (h
->root
.type
)
2764 case bfd_link_hash_undefined
:
2765 case bfd_link_hash_undefweak
:
2766 abfd
= h
->root
.u
.undef
.abfd
;
2767 if ((abfd
->flags
& DYNAMIC
) == 0
2768 || elf_dyn_lib_class (abfd
) != DYN_DT_NEEDED
)
2772 case bfd_link_hash_defined
:
2773 case bfd_link_hash_defweak
:
2774 abfd
= h
->root
.u
.def
.section
->owner
;
2777 case bfd_link_hash_common
:
2778 abfd
= h
->root
.u
.c
.p
->section
->owner
;
2781 BFD_ASSERT (abfd
!= NULL
);
2783 for (loaded
= elf_hash_table (info
)->loaded
;
2785 loaded
= loaded
->next
)
2788 Elf_Internal_Shdr
*hdr
;
2789 bfd_size_type symcount
;
2790 bfd_size_type extsymcount
;
2791 bfd_size_type extsymoff
;
2792 Elf_Internal_Shdr
*versymhdr
;
2793 Elf_Internal_Sym
*isym
;
2794 Elf_Internal_Sym
*isymend
;
2795 Elf_Internal_Sym
*isymbuf
;
2796 Elf_External_Versym
*ever
;
2797 Elf_External_Versym
*extversym
;
2799 input
= loaded
->abfd
;
2801 /* We check each DSO for a possible hidden versioned definition. */
2803 || (input
->flags
& DYNAMIC
) == 0
2804 || elf_dynversym (input
) == 0)
2807 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
2809 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2810 if (elf_bad_symtab (input
))
2812 extsymcount
= symcount
;
2817 extsymcount
= symcount
- hdr
->sh_info
;
2818 extsymoff
= hdr
->sh_info
;
2821 if (extsymcount
== 0)
2824 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
2826 if (isymbuf
== NULL
)
2829 /* Read in any version definitions. */
2830 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
2831 extversym
= bfd_malloc (versymhdr
->sh_size
);
2832 if (extversym
== NULL
)
2835 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
2836 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
2837 != versymhdr
->sh_size
))
2845 ever
= extversym
+ extsymoff
;
2846 isymend
= isymbuf
+ extsymcount
;
2847 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
2850 Elf_Internal_Versym iver
;
2851 unsigned short version_index
;
2853 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
2854 || isym
->st_shndx
== SHN_UNDEF
)
2857 name
= bfd_elf_string_from_elf_section (input
,
2860 if (strcmp (name
, h
->root
.root
.string
) != 0)
2863 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
2865 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
2867 /* If we have a non-hidden versioned sym, then it should
2868 have provided a definition for the undefined sym. */
2872 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
2873 if (version_index
== 1 || version_index
== 2)
2875 /* This is the base or first version. We can use it. */
2889 /* Add an external symbol to the symbol table. This is called from
2890 the hash table traversal routine. When generating a shared object,
2891 we go through the symbol table twice. The first time we output
2892 anything that might have been forced to local scope in a version
2893 script. The second time we output the symbols that are still
2897 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
2899 struct elf_outext_info
*eoinfo
= data
;
2900 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
2902 Elf_Internal_Sym sym
;
2903 asection
*input_sec
;
2905 if (h
->root
.type
== bfd_link_hash_warning
)
2907 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2908 if (h
->root
.type
== bfd_link_hash_new
)
2912 /* Decide whether to output this symbol in this pass. */
2913 if (eoinfo
->localsyms
)
2915 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
2920 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
2924 /* If we have an undefined symbol reference here then it must have
2925 come from a shared library that is being linked in. (Undefined
2926 references in regular files have already been handled). If we
2927 are reporting errors for this situation then do so now. */
2928 if (h
->root
.type
== bfd_link_hash_undefined
2929 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
2930 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
2931 && ! elf_link_check_versioned_symbol (finfo
->info
, h
)
2932 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
2934 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
2935 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
2936 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
2938 eoinfo
->failed
= TRUE
;
2943 /* We should also warn if a forced local symbol is referenced from
2944 shared libraries. */
2945 if (! finfo
->info
->relocatable
2946 && (! finfo
->info
->shared
)
2947 && (h
->elf_link_hash_flags
2948 & (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
| ELF_LINK_DYNAMIC_DEF
| ELF_LINK_DYNAMIC_WEAK
))
2949 == (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
)
2950 && ! elf_link_check_versioned_symbol (finfo
->info
, h
))
2952 (*_bfd_error_handler
)
2953 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
2954 bfd_get_filename (finfo
->output_bfd
),
2955 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2957 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
2958 ? "hidden" : "local",
2959 h
->root
.root
.string
,
2960 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
2961 eoinfo
->failed
= TRUE
;
2965 /* We don't want to output symbols that have never been mentioned by
2966 a regular file, or that we have been told to strip. However, if
2967 h->indx is set to -2, the symbol is used by a reloc and we must
2971 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2972 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
2973 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2974 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
2976 else if (finfo
->info
->strip
== strip_all
)
2978 else if (finfo
->info
->strip
== strip_some
2979 && bfd_hash_lookup (finfo
->info
->keep_hash
,
2980 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
2982 else if (finfo
->info
->strip_discarded
2983 && (h
->root
.type
== bfd_link_hash_defined
2984 || h
->root
.type
== bfd_link_hash_defweak
)
2985 && elf_discarded_section (h
->root
.u
.def
.section
))
2990 /* If we're stripping it, and it's not a dynamic symbol, there's
2991 nothing else to do unless it is a forced local symbol. */
2994 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
2998 sym
.st_size
= h
->size
;
2999 sym
.st_other
= h
->other
;
3000 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
3001 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
3002 else if (h
->root
.type
== bfd_link_hash_undefweak
3003 || h
->root
.type
== bfd_link_hash_defweak
)
3004 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
3006 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
3008 switch (h
->root
.type
)
3011 case bfd_link_hash_new
:
3012 case bfd_link_hash_warning
:
3016 case bfd_link_hash_undefined
:
3017 case bfd_link_hash_undefweak
:
3018 input_sec
= bfd_und_section_ptr
;
3019 sym
.st_shndx
= SHN_UNDEF
;
3022 case bfd_link_hash_defined
:
3023 case bfd_link_hash_defweak
:
3025 input_sec
= h
->root
.u
.def
.section
;
3026 if (input_sec
->output_section
!= NULL
)
3029 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
3030 input_sec
->output_section
);
3031 if (sym
.st_shndx
== SHN_BAD
)
3033 (*_bfd_error_handler
)
3034 (_("%s: could not find output section %s for input section %s"),
3035 bfd_get_filename (finfo
->output_bfd
),
3036 input_sec
->output_section
->name
,
3038 eoinfo
->failed
= TRUE
;
3042 /* ELF symbols in relocatable files are section relative,
3043 but in nonrelocatable files they are virtual
3045 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
3046 if (! finfo
->info
->relocatable
)
3048 sym
.st_value
+= input_sec
->output_section
->vma
;
3049 if (h
->type
== STT_TLS
)
3051 /* STT_TLS symbols are relative to PT_TLS segment
3053 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
3054 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
3060 BFD_ASSERT (input_sec
->owner
== NULL
3061 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
3062 sym
.st_shndx
= SHN_UNDEF
;
3063 input_sec
= bfd_und_section_ptr
;
3068 case bfd_link_hash_common
:
3069 input_sec
= h
->root
.u
.c
.p
->section
;
3070 sym
.st_shndx
= SHN_COMMON
;
3071 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
3074 case bfd_link_hash_indirect
:
3075 /* These symbols are created by symbol versioning. They point
3076 to the decorated version of the name. For example, if the
3077 symbol foo@@GNU_1.2 is the default, which should be used when
3078 foo is used with no version, then we add an indirect symbol
3079 foo which points to foo@@GNU_1.2. We ignore these symbols,
3080 since the indirected symbol is already in the hash table. */
3084 /* Give the processor backend a chance to tweak the symbol value,
3085 and also to finish up anything that needs to be done for this
3086 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3087 forced local syms when non-shared is due to a historical quirk. */
3088 if ((h
->dynindx
!= -1
3089 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
3090 && ((finfo
->info
->shared
3091 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3092 || h
->root
.type
!= bfd_link_hash_undefweak
))
3093 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
3094 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
3096 const struct elf_backend_data
*bed
;
3098 bed
= get_elf_backend_data (finfo
->output_bfd
);
3099 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
3100 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
3102 eoinfo
->failed
= TRUE
;
3107 /* If we are marking the symbol as undefined, and there are no
3108 non-weak references to this symbol from a regular object, then
3109 mark the symbol as weak undefined; if there are non-weak
3110 references, mark the symbol as strong. We can't do this earlier,
3111 because it might not be marked as undefined until the
3112 finish_dynamic_symbol routine gets through with it. */
3113 if (sym
.st_shndx
== SHN_UNDEF
3114 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
3115 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
3116 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
3120 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
3121 bindtype
= STB_GLOBAL
;
3123 bindtype
= STB_WEAK
;
3124 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
3127 /* If a non-weak symbol with non-default visibility is not defined
3128 locally, it is a fatal error. */
3129 if (! finfo
->info
->relocatable
3130 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
3131 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
3132 && h
->root
.type
== bfd_link_hash_undefined
3133 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
3135 (*_bfd_error_handler
)
3136 (_("%s: %s symbol `%s' isn't defined"),
3137 bfd_get_filename (finfo
->output_bfd
),
3138 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
3140 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
3141 ? "internal" : "hidden",
3142 h
->root
.root
.string
);
3143 eoinfo
->failed
= TRUE
;
3147 /* If this symbol should be put in the .dynsym section, then put it
3148 there now. We already know the symbol index. We also fill in
3149 the entry in the .hash section. */
3150 if (h
->dynindx
!= -1
3151 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
3155 size_t hash_entry_size
;
3156 bfd_byte
*bucketpos
;
3158 Elf_External_Sym
*esym
;
3160 sym
.st_name
= h
->dynstr_index
;
3161 esym
= (Elf_External_Sym
*) finfo
->dynsym_sec
->contents
+ h
->dynindx
;
3162 elf_swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
3164 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
3165 bucket
= h
->elf_hash_value
% bucketcount
;
3167 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
3168 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
3169 + (bucket
+ 2) * hash_entry_size
);
3170 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
3171 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
3172 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
3173 ((bfd_byte
*) finfo
->hash_sec
->contents
3174 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
3176 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
3178 Elf_Internal_Versym iversym
;
3179 Elf_External_Versym
*eversym
;
3181 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
3183 if (h
->verinfo
.verdef
== NULL
)
3184 iversym
.vs_vers
= 0;
3186 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
3190 if (h
->verinfo
.vertree
== NULL
)
3191 iversym
.vs_vers
= 1;
3193 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
3196 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
3197 iversym
.vs_vers
|= VERSYM_HIDDEN
;
3199 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
3200 eversym
+= h
->dynindx
;
3201 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
3205 /* If we're stripping it, then it was just a dynamic symbol, and
3206 there's nothing else to do. */
3207 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
3210 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
3212 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
3214 eoinfo
->failed
= TRUE
;
3221 /* Link an input file into the linker output file. This function
3222 handles all the sections and relocations of the input file at once.
3223 This is so that we only have to read the local symbols once, and
3224 don't have to keep them in memory. */
3227 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
3229 bfd_boolean (*relocate_section
)
3230 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
3231 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
3233 Elf_Internal_Shdr
*symtab_hdr
;
3236 Elf_Internal_Sym
*isymbuf
;
3237 Elf_Internal_Sym
*isym
;
3238 Elf_Internal_Sym
*isymend
;
3240 asection
**ppsection
;
3242 const struct elf_backend_data
*bed
;
3243 bfd_boolean emit_relocs
;
3244 struct elf_link_hash_entry
**sym_hashes
;
3246 output_bfd
= finfo
->output_bfd
;
3247 bed
= get_elf_backend_data (output_bfd
);
3248 relocate_section
= bed
->elf_backend_relocate_section
;
3250 /* If this is a dynamic object, we don't want to do anything here:
3251 we don't want the local symbols, and we don't want the section
3253 if ((input_bfd
->flags
& DYNAMIC
) != 0)
3256 emit_relocs
= (finfo
->info
->relocatable
3257 || finfo
->info
->emitrelocations
3258 || bed
->elf_backend_emit_relocs
);
3260 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3261 if (elf_bad_symtab (input_bfd
))
3263 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
3268 locsymcount
= symtab_hdr
->sh_info
;
3269 extsymoff
= symtab_hdr
->sh_info
;
3272 /* Read the local symbols. */
3273 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
3274 if (isymbuf
== NULL
&& locsymcount
!= 0)
3276 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
3277 finfo
->internal_syms
,
3278 finfo
->external_syms
,
3279 finfo
->locsym_shndx
);
3280 if (isymbuf
== NULL
)
3284 /* Find local symbol sections and adjust values of symbols in
3285 SEC_MERGE sections. Write out those local symbols we know are
3286 going into the output file. */
3287 isymend
= isymbuf
+ locsymcount
;
3288 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
3290 isym
++, pindex
++, ppsection
++)
3294 Elf_Internal_Sym osym
;
3298 if (elf_bad_symtab (input_bfd
))
3300 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
3307 if (isym
->st_shndx
== SHN_UNDEF
)
3308 isec
= bfd_und_section_ptr
;
3309 else if (isym
->st_shndx
< SHN_LORESERVE
3310 || isym
->st_shndx
> SHN_HIRESERVE
)
3312 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
3314 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
3315 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
3317 _bfd_merged_section_offset (output_bfd
, &isec
,
3318 elf_section_data (isec
)->sec_info
,
3321 else if (isym
->st_shndx
== SHN_ABS
)
3322 isec
= bfd_abs_section_ptr
;
3323 else if (isym
->st_shndx
== SHN_COMMON
)
3324 isec
= bfd_com_section_ptr
;
3333 /* Don't output the first, undefined, symbol. */
3334 if (ppsection
== finfo
->sections
)
3337 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
3339 /* We never output section symbols. Instead, we use the
3340 section symbol of the corresponding section in the output
3345 /* If we are stripping all symbols, we don't want to output this
3347 if (finfo
->info
->strip
== strip_all
)
3350 /* If we are discarding all local symbols, we don't want to
3351 output this one. If we are generating a relocatable output
3352 file, then some of the local symbols may be required by
3353 relocs; we output them below as we discover that they are
3355 if (finfo
->info
->discard
== discard_all
)
3358 /* If this symbol is defined in a section which we are
3359 discarding, we don't need to keep it, but note that
3360 linker_mark is only reliable for sections that have contents.
3361 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
3362 as well as linker_mark. */
3363 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3365 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
3366 || (! finfo
->info
->relocatable
3367 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
3370 /* Get the name of the symbol. */
3371 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
3376 /* See if we are discarding symbols with this name. */
3377 if ((finfo
->info
->strip
== strip_some
3378 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
3380 || (((finfo
->info
->discard
== discard_sec_merge
3381 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
3382 || finfo
->info
->discard
== discard_l
)
3383 && bfd_is_local_label_name (input_bfd
, name
)))
3386 /* If we get here, we are going to output this symbol. */
3390 /* Adjust the section index for the output file. */
3391 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
3392 isec
->output_section
);
3393 if (osym
.st_shndx
== SHN_BAD
)
3396 *pindex
= bfd_get_symcount (output_bfd
);
3398 /* ELF symbols in relocatable files are section relative, but
3399 in executable files they are virtual addresses. Note that
3400 this code assumes that all ELF sections have an associated
3401 BFD section with a reasonable value for output_offset; below
3402 we assume that they also have a reasonable value for
3403 output_section. Any special sections must be set up to meet
3404 these requirements. */
3405 osym
.st_value
+= isec
->output_offset
;
3406 if (! finfo
->info
->relocatable
)
3408 osym
.st_value
+= isec
->output_section
->vma
;
3409 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
3411 /* STT_TLS symbols are relative to PT_TLS segment base. */
3412 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
3413 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
3417 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
3421 /* Relocate the contents of each section. */
3422 sym_hashes
= elf_sym_hashes (input_bfd
);
3423 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
3427 if (! o
->linker_mark
)
3429 /* This section was omitted from the link. */
3433 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
3434 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
3437 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
3439 /* Section was created by _bfd_elf_link_create_dynamic_sections
3444 /* Get the contents of the section. They have been cached by a
3445 relaxation routine. Note that o is a section in an input
3446 file, so the contents field will not have been set by any of
3447 the routines which work on output files. */
3448 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
3449 contents
= elf_section_data (o
)->this_hdr
.contents
;
3452 contents
= finfo
->contents
;
3453 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0,
3458 if ((o
->flags
& SEC_RELOC
) != 0)
3460 Elf_Internal_Rela
*internal_relocs
;
3462 /* Get the swapped relocs. */
3464 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
3465 finfo
->internal_relocs
, FALSE
);
3466 if (internal_relocs
== NULL
3467 && o
->reloc_count
> 0)
3470 /* Run through the relocs looking for any against symbols
3471 from discarded sections and section symbols from
3472 removed link-once sections. Complain about relocs
3473 against discarded sections. Zero relocs against removed
3474 link-once sections. Preserve debug information as much
3476 if (!elf_section_ignore_discarded_relocs (o
))
3478 Elf_Internal_Rela
*rel
, *relend
;
3480 rel
= internal_relocs
;
3481 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
3482 for ( ; rel
< relend
; rel
++)
3484 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
3487 if (r_symndx
>= locsymcount
3488 || (elf_bad_symtab (input_bfd
)
3489 && finfo
->sections
[r_symndx
] == NULL
))
3491 struct elf_link_hash_entry
*h
;
3493 h
= sym_hashes
[r_symndx
- extsymoff
];
3494 while (h
->root
.type
== bfd_link_hash_indirect
3495 || h
->root
.type
== bfd_link_hash_warning
)
3496 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3498 /* Complain if the definition comes from a
3499 discarded section. */
3500 sec
= h
->root
.u
.def
.section
;
3501 if ((h
->root
.type
== bfd_link_hash_defined
3502 || h
->root
.type
== bfd_link_hash_defweak
)
3503 && elf_discarded_section (sec
))
3505 if ((o
->flags
& SEC_DEBUGGING
) != 0)
3507 BFD_ASSERT (r_symndx
!= 0);
3508 /* Try to preserve debug information. */
3509 if ((o
->flags
& SEC_DEBUGGING
) != 0
3510 && sec
->kept_section
!= NULL
3511 && sec
->_raw_size
== sec
->kept_section
->_raw_size
)
3512 h
->root
.u
.def
.section
3513 = sec
->kept_section
;
3515 memset (rel
, 0, sizeof (*rel
));
3518 finfo
->info
->callbacks
->error_handler
3519 (LD_DEFINITION_IN_DISCARDED_SECTION
,
3520 _("%T: discarded in section `%s' from %s\n"),
3521 h
->root
.root
.string
,
3522 h
->root
.root
.string
,
3523 h
->root
.u
.def
.section
->name
,
3524 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
3529 sec
= finfo
->sections
[r_symndx
];
3531 if (sec
!= NULL
&& elf_discarded_section (sec
))
3533 if ((o
->flags
& SEC_DEBUGGING
) != 0
3534 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
3536 BFD_ASSERT (r_symndx
!= 0);
3537 /* Try to preserve debug information. */
3538 if ((o
->flags
& SEC_DEBUGGING
) != 0
3539 && sec
->kept_section
!= NULL
3540 && sec
->_raw_size
== sec
->kept_section
->_raw_size
)
3541 finfo
->sections
[r_symndx
]
3542 = sec
->kept_section
;
3546 = ELF_R_INFO (0, ELF_R_TYPE (rel
->r_info
));
3556 ok
= asprintf (&buf
, "local symbol %d",
3559 buf
= (char *) "local symbol";
3560 finfo
->info
->callbacks
->error_handler
3561 (LD_DEFINITION_IN_DISCARDED_SECTION
,
3562 _("%T: discarded in section `%s' from %s\n"),
3563 buf
, buf
, sec
->name
,
3564 bfd_archive_filename (input_bfd
));
3573 /* Relocate the section by invoking a back end routine.
3575 The back end routine is responsible for adjusting the
3576 section contents as necessary, and (if using Rela relocs
3577 and generating a relocatable output file) adjusting the
3578 reloc addend as necessary.
3580 The back end routine does not have to worry about setting
3581 the reloc address or the reloc symbol index.
3583 The back end routine is given a pointer to the swapped in
3584 internal symbols, and can access the hash table entries
3585 for the external symbols via elf_sym_hashes (input_bfd).
3587 When generating relocatable output, the back end routine
3588 must handle STB_LOCAL/STT_SECTION symbols specially. The
3589 output symbol is going to be a section symbol
3590 corresponding to the output section, which will require
3591 the addend to be adjusted. */
3593 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
3594 input_bfd
, o
, contents
,
3602 Elf_Internal_Rela
*irela
;
3603 Elf_Internal_Rela
*irelaend
;
3604 bfd_vma last_offset
;
3605 struct elf_link_hash_entry
**rel_hash
;
3606 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
3607 unsigned int next_erel
;
3608 bfd_boolean (*reloc_emitter
)
3609 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
3610 bfd_boolean rela_normal
;
3612 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
3613 rela_normal
= (bed
->rela_normal
3614 && (input_rel_hdr
->sh_entsize
3615 == sizeof (Elf_External_Rela
)));
3617 /* Adjust the reloc addresses and symbol indices. */
3619 irela
= internal_relocs
;
3620 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
3621 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
3622 + elf_section_data (o
->output_section
)->rel_count
3623 + elf_section_data (o
->output_section
)->rel_count2
);
3624 last_offset
= o
->output_offset
;
3625 if (!finfo
->info
->relocatable
)
3626 last_offset
+= o
->output_section
->vma
;
3627 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
3629 unsigned long r_symndx
;
3631 Elf_Internal_Sym sym
;
3633 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
3639 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
3642 if (irela
->r_offset
>= (bfd_vma
) -2)
3644 /* This is a reloc for a deleted entry or somesuch.
3645 Turn it into an R_*_NONE reloc, at the same
3646 offset as the last reloc. elf_eh_frame.c and
3647 elf_bfd_discard_info rely on reloc offsets
3649 irela
->r_offset
= last_offset
;
3651 irela
->r_addend
= 0;
3655 irela
->r_offset
+= o
->output_offset
;
3657 /* Relocs in an executable have to be virtual addresses. */
3658 if (!finfo
->info
->relocatable
)
3659 irela
->r_offset
+= o
->output_section
->vma
;
3661 last_offset
= irela
->r_offset
;
3663 r_symndx
= ELF_R_SYM (irela
->r_info
);
3664 if (r_symndx
== STN_UNDEF
)
3667 if (r_symndx
>= locsymcount
3668 || (elf_bad_symtab (input_bfd
)
3669 && finfo
->sections
[r_symndx
] == NULL
))
3671 struct elf_link_hash_entry
*rh
;
3674 /* This is a reloc against a global symbol. We
3675 have not yet output all the local symbols, so
3676 we do not know the symbol index of any global
3677 symbol. We set the rel_hash entry for this
3678 reloc to point to the global hash table entry
3679 for this symbol. The symbol index is then
3680 set at the end of elf_bfd_final_link. */
3681 indx
= r_symndx
- extsymoff
;
3682 rh
= elf_sym_hashes (input_bfd
)[indx
];
3683 while (rh
->root
.type
== bfd_link_hash_indirect
3684 || rh
->root
.type
== bfd_link_hash_warning
)
3685 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
3687 /* Setting the index to -2 tells
3688 elf_link_output_extsym that this symbol is
3690 BFD_ASSERT (rh
->indx
< 0);
3698 /* This is a reloc against a local symbol. */
3701 sym
= isymbuf
[r_symndx
];
3702 sec
= finfo
->sections
[r_symndx
];
3703 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
3705 /* I suppose the backend ought to fill in the
3706 section of any STT_SECTION symbol against a
3707 processor specific section. If we have
3708 discarded a section, the output_section will
3709 be the absolute section. */
3710 if (bfd_is_abs_section (sec
)
3712 && bfd_is_abs_section (sec
->output_section
)))
3714 else if (sec
== NULL
|| sec
->owner
== NULL
)
3716 bfd_set_error (bfd_error_bad_value
);
3721 r_symndx
= sec
->output_section
->target_index
;
3722 BFD_ASSERT (r_symndx
!= 0);
3725 /* Adjust the addend according to where the
3726 section winds up in the output section. */
3728 irela
->r_addend
+= sec
->output_offset
;
3732 if (finfo
->indices
[r_symndx
] == -1)
3734 unsigned long shlink
;
3738 if (finfo
->info
->strip
== strip_all
)
3740 /* You can't do ld -r -s. */
3741 bfd_set_error (bfd_error_invalid_operation
);
3745 /* This symbol was skipped earlier, but
3746 since it is needed by a reloc, we
3747 must output it now. */
3748 shlink
= symtab_hdr
->sh_link
;
3749 name
= (bfd_elf_string_from_elf_section
3750 (input_bfd
, shlink
, sym
.st_name
));
3754 osec
= sec
->output_section
;
3756 _bfd_elf_section_from_bfd_section (output_bfd
,
3758 if (sym
.st_shndx
== SHN_BAD
)
3761 sym
.st_value
+= sec
->output_offset
;
3762 if (! finfo
->info
->relocatable
)
3764 sym
.st_value
+= osec
->vma
;
3765 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
3767 /* STT_TLS symbols are relative to PT_TLS
3769 BFD_ASSERT (elf_hash_table (finfo
->info
)
3771 sym
.st_value
-= (elf_hash_table (finfo
->info
)
3776 finfo
->indices
[r_symndx
]
3777 = bfd_get_symcount (output_bfd
);
3779 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
3784 r_symndx
= finfo
->indices
[r_symndx
];
3787 irela
->r_info
= ELF_R_INFO (r_symndx
,
3788 ELF_R_TYPE (irela
->r_info
));
3791 /* Swap out the relocs. */
3792 if (bed
->elf_backend_emit_relocs
3793 && !(finfo
->info
->relocatable
3794 || finfo
->info
->emitrelocations
))
3795 reloc_emitter
= bed
->elf_backend_emit_relocs
;
3797 reloc_emitter
= _bfd_elf_link_output_relocs
;
3799 if (input_rel_hdr
->sh_size
!= 0
3800 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
3804 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
3805 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
3807 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
3808 * bed
->s
->int_rels_per_ext_rel
);
3809 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
3816 /* Write out the modified section contents. */
3817 if (bed
->elf_backend_write_section
3818 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
3820 /* Section written out. */
3822 else switch (o
->sec_info_type
)
3824 case ELF_INFO_TYPE_STABS
:
3825 if (! (_bfd_write_section_stabs
3827 &elf_hash_table (finfo
->info
)->stab_info
,
3828 o
, &elf_section_data (o
)->sec_info
, contents
)))
3831 case ELF_INFO_TYPE_MERGE
:
3832 if (! _bfd_write_merged_section (output_bfd
, o
,
3833 elf_section_data (o
)->sec_info
))
3836 case ELF_INFO_TYPE_EH_FRAME
:
3838 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
3845 bfd_size_type sec_size
;
3847 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
3848 if (! (o
->flags
& SEC_EXCLUDE
)
3849 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
3851 (file_ptr
) o
->output_offset
,
3862 /* Generate a reloc when linking an ELF file. This is a reloc
3863 requested by the linker, and does come from any input file. This
3864 is used to build constructor and destructor tables when linking
3868 elf_reloc_link_order (bfd
*output_bfd
,
3869 struct bfd_link_info
*info
,
3870 asection
*output_section
,
3871 struct bfd_link_order
*link_order
)
3873 reloc_howto_type
*howto
;
3877 struct elf_link_hash_entry
**rel_hash_ptr
;
3878 Elf_Internal_Shdr
*rel_hdr
;
3879 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
3880 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
3884 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
3887 bfd_set_error (bfd_error_bad_value
);
3891 addend
= link_order
->u
.reloc
.p
->addend
;
3893 /* Figure out the symbol index. */
3894 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
3895 + elf_section_data (output_section
)->rel_count
3896 + elf_section_data (output_section
)->rel_count2
);
3897 if (link_order
->type
== bfd_section_reloc_link_order
)
3899 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
3900 BFD_ASSERT (indx
!= 0);
3901 *rel_hash_ptr
= NULL
;
3905 struct elf_link_hash_entry
*h
;
3907 /* Treat a reloc against a defined symbol as though it were
3908 actually against the section. */
3909 h
= ((struct elf_link_hash_entry
*)
3910 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
3911 link_order
->u
.reloc
.p
->u
.name
,
3912 FALSE
, FALSE
, TRUE
));
3914 && (h
->root
.type
== bfd_link_hash_defined
3915 || h
->root
.type
== bfd_link_hash_defweak
))
3919 section
= h
->root
.u
.def
.section
;
3920 indx
= section
->output_section
->target_index
;
3921 *rel_hash_ptr
= NULL
;
3922 /* It seems that we ought to add the symbol value to the
3923 addend here, but in practice it has already been added
3924 because it was passed to constructor_callback. */
3925 addend
+= section
->output_section
->vma
+ section
->output_offset
;
3929 /* Setting the index to -2 tells elf_link_output_extsym that
3930 this symbol is used by a reloc. */
3937 if (! ((*info
->callbacks
->unattached_reloc
)
3938 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
3944 /* If this is an inplace reloc, we must write the addend into the
3946 if (howto
->partial_inplace
&& addend
!= 0)
3949 bfd_reloc_status_type rstat
;
3952 const char *sym_name
;
3954 size
= bfd_get_reloc_size (howto
);
3955 buf
= bfd_zmalloc (size
);
3958 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
3965 case bfd_reloc_outofrange
:
3968 case bfd_reloc_overflow
:
3969 if (link_order
->type
== bfd_section_reloc_link_order
)
3970 sym_name
= bfd_section_name (output_bfd
,
3971 link_order
->u
.reloc
.p
->u
.section
);
3973 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
3974 if (! ((*info
->callbacks
->reloc_overflow
)
3975 (info
, sym_name
, howto
->name
, addend
, NULL
, NULL
, 0)))
3982 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
3983 link_order
->offset
, size
);
3989 /* The address of a reloc is relative to the section in a
3990 relocatable file, and is a virtual address in an executable
3992 offset
= link_order
->offset
;
3993 if (! info
->relocatable
)
3994 offset
+= output_section
->vma
;
3996 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
3998 irel
[i
].r_offset
= offset
;
4000 irel
[i
].r_addend
= 0;
4002 irel
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
4004 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
4005 erel
= rel_hdr
->contents
;
4006 if (rel_hdr
->sh_type
== SHT_REL
)
4008 erel
+= (elf_section_data (output_section
)->rel_count
4009 * sizeof (Elf_External_Rel
));
4010 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
4014 irel
[0].r_addend
= addend
;
4015 erel
+= (elf_section_data (output_section
)->rel_count
4016 * sizeof (Elf_External_Rela
));
4017 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
4020 ++elf_section_data (output_section
)->rel_count
;
4025 /* Garbage collect unused sections. */
4027 static bfd_boolean elf_gc_sweep_symbol
4028 (struct elf_link_hash_entry
*, void *);
4030 static bfd_boolean elf_gc_allocate_got_offsets
4031 (struct elf_link_hash_entry
*, void *);
4033 /* The mark phase of garbage collection. For a given section, mark
4034 it and any sections in this section's group, and all the sections
4035 which define symbols to which it refers. */
4037 typedef asection
* (*gc_mark_hook_fn
)
4038 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
4039 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
4042 elf_gc_mark (struct bfd_link_info
*info
,
4044 gc_mark_hook_fn gc_mark_hook
)
4047 asection
*group_sec
;
4051 /* Mark all the sections in the group. */
4052 group_sec
= elf_section_data (sec
)->next_in_group
;
4053 if (group_sec
&& !group_sec
->gc_mark
)
4054 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
4057 /* Look through the section relocs. */
4059 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
4061 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
4062 Elf_Internal_Shdr
*symtab_hdr
;
4063 struct elf_link_hash_entry
**sym_hashes
;
4066 bfd
*input_bfd
= sec
->owner
;
4067 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
4068 Elf_Internal_Sym
*isym
= NULL
;
4070 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
4071 sym_hashes
= elf_sym_hashes (input_bfd
);
4073 /* Read the local symbols. */
4074 if (elf_bad_symtab (input_bfd
))
4076 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
4080 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
4082 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
4083 if (isym
== NULL
&& nlocsyms
!= 0)
4085 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
4091 /* Read the relocations. */
4092 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
4094 if (relstart
== NULL
)
4099 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
4101 for (rel
= relstart
; rel
< relend
; rel
++)
4103 unsigned long r_symndx
;
4105 struct elf_link_hash_entry
*h
;
4107 r_symndx
= ELF_R_SYM (rel
->r_info
);
4111 if (r_symndx
>= nlocsyms
4112 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
4114 h
= sym_hashes
[r_symndx
- extsymoff
];
4115 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
4119 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
4122 if (rsec
&& !rsec
->gc_mark
)
4124 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
4126 else if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
4135 if (elf_section_data (sec
)->relocs
!= relstart
)
4138 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
4140 if (! info
->keep_memory
)
4143 symtab_hdr
->contents
= (unsigned char *) isym
;
4150 /* The sweep phase of garbage collection. Remove all garbage sections. */
4152 typedef bfd_boolean (*gc_sweep_hook_fn
)
4153 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
4156 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
4160 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
4164 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
4167 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
4169 /* Keep special sections. Keep .debug sections. */
4170 if ((o
->flags
& SEC_LINKER_CREATED
)
4171 || (o
->flags
& SEC_DEBUGGING
))
4177 /* Skip sweeping sections already excluded. */
4178 if (o
->flags
& SEC_EXCLUDE
)
4181 /* Since this is early in the link process, it is simple
4182 to remove a section from the output. */
4183 o
->flags
|= SEC_EXCLUDE
;
4185 /* But we also have to update some of the relocation
4186 info we collected before. */
4188 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
4190 Elf_Internal_Rela
*internal_relocs
;
4194 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
4196 if (internal_relocs
== NULL
)
4199 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
4201 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4202 free (internal_relocs
);
4210 /* Remove the symbols that were in the swept sections from the dynamic
4211 symbol table. GCFIXME: Anyone know how to get them out of the
4212 static symbol table as well? */
4216 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
4218 elf_hash_table (info
)->dynsymcount
= i
;
4224 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
4227 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
4231 if (h
->root
.type
== bfd_link_hash_warning
)
4232 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4234 if (h
->dynindx
!= -1
4235 && ((h
->root
.type
!= bfd_link_hash_defined
4236 && h
->root
.type
!= bfd_link_hash_defweak
)
4237 || h
->root
.u
.def
.section
->gc_mark
))
4238 h
->dynindx
= (*idx
)++;
4243 /* Propagate collected vtable information. This is called through
4244 elf_link_hash_traverse. */
4247 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
4249 if (h
->root
.type
== bfd_link_hash_warning
)
4250 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4252 /* Those that are not vtables. */
4253 if (h
->vtable_parent
== NULL
)
4256 /* Those vtables that do not have parents, we cannot merge. */
4257 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
4260 /* If we've already been done, exit. */
4261 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
4264 /* Make sure the parent's table is up to date. */
4265 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
4267 if (h
->vtable_entries_used
== NULL
)
4269 /* None of this table's entries were referenced. Re-use the
4271 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
4272 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
4277 bfd_boolean
*cu
, *pu
;
4279 /* Or the parent's entries into ours. */
4280 cu
= h
->vtable_entries_used
;
4282 pu
= h
->vtable_parent
->vtable_entries_used
;
4285 const struct elf_backend_data
*bed
;
4286 unsigned int log_file_align
;
4288 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
4289 log_file_align
= bed
->s
->log_file_align
;
4290 n
= h
->vtable_parent
->vtable_entries_size
>> log_file_align
;
4305 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
4308 bfd_vma hstart
, hend
;
4309 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
4310 const struct elf_backend_data
*bed
;
4311 unsigned int log_file_align
;
4313 if (h
->root
.type
== bfd_link_hash_warning
)
4314 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4316 /* Take care of both those symbols that do not describe vtables as
4317 well as those that are not loaded. */
4318 if (h
->vtable_parent
== NULL
)
4321 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4322 || h
->root
.type
== bfd_link_hash_defweak
);
4324 sec
= h
->root
.u
.def
.section
;
4325 hstart
= h
->root
.u
.def
.value
;
4326 hend
= hstart
+ h
->size
;
4328 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
4330 return *(bfd_boolean
*) okp
= FALSE
;
4331 bed
= get_elf_backend_data (sec
->owner
);
4332 log_file_align
= bed
->s
->log_file_align
;
4334 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
4336 for (rel
= relstart
; rel
< relend
; ++rel
)
4337 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
4339 /* If the entry is in use, do nothing. */
4340 if (h
->vtable_entries_used
4341 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
4343 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
4344 if (h
->vtable_entries_used
[entry
])
4347 /* Otherwise, kill it. */
4348 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
4354 /* Do mark and sweep of unused sections. */
4357 elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
4359 bfd_boolean ok
= TRUE
;
4361 asection
* (*gc_mark_hook
)
4362 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
4363 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
4365 if (!get_elf_backend_data (abfd
)->can_gc_sections
4366 || info
->relocatable
4367 || info
->emitrelocations
4368 || !is_elf_hash_table (info
->hash
)
4369 || elf_hash_table (info
)->dynamic_sections_created
)
4371 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
4375 /* Apply transitive closure to the vtable entry usage info. */
4376 elf_link_hash_traverse (elf_hash_table (info
),
4377 elf_gc_propagate_vtable_entries_used
,
4382 /* Kill the vtable relocations that were not used. */
4383 elf_link_hash_traverse (elf_hash_table (info
),
4384 elf_gc_smash_unused_vtentry_relocs
,
4389 /* Grovel through relocs to find out who stays ... */
4391 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
4392 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
4396 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
4399 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
4401 if (o
->flags
& SEC_KEEP
)
4402 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
4407 /* ... and mark SEC_EXCLUDE for those that go. */
4408 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
4414 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
4417 elf_gc_record_vtinherit (bfd
*abfd
,
4419 struct elf_link_hash_entry
*h
,
4422 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
4423 struct elf_link_hash_entry
**search
, *child
;
4424 bfd_size_type extsymcount
;
4426 /* The sh_info field of the symtab header tells us where the
4427 external symbols start. We don't care about the local symbols at
4429 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
4430 if (!elf_bad_symtab (abfd
))
4431 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
4433 sym_hashes
= elf_sym_hashes (abfd
);
4434 sym_hashes_end
= sym_hashes
+ extsymcount
;
4436 /* Hunt down the child symbol, which is in this section at the same
4437 offset as the relocation. */
4438 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
4440 if ((child
= *search
) != NULL
4441 && (child
->root
.type
== bfd_link_hash_defined
4442 || child
->root
.type
== bfd_link_hash_defweak
)
4443 && child
->root
.u
.def
.section
== sec
4444 && child
->root
.u
.def
.value
== offset
)
4448 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
4449 bfd_archive_filename (abfd
), sec
->name
,
4450 (unsigned long) offset
);
4451 bfd_set_error (bfd_error_invalid_operation
);
4457 /* This *should* only be the absolute section. It could potentially
4458 be that someone has defined a non-global vtable though, which
4459 would be bad. It isn't worth paging in the local symbols to be
4460 sure though; that case should simply be handled by the assembler. */
4462 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
4465 child
->vtable_parent
= h
;
4470 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
4473 elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
4474 asection
*sec ATTRIBUTE_UNUSED
,
4475 struct elf_link_hash_entry
*h
,
4478 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4479 unsigned int log_file_align
= bed
->s
->log_file_align
;
4481 if (addend
>= h
->vtable_entries_size
)
4483 size_t size
, bytes
, file_align
;
4484 bfd_boolean
*ptr
= h
->vtable_entries_used
;
4486 /* While the symbol is undefined, we have to be prepared to handle
4488 file_align
= 1 << log_file_align
;
4489 if (h
->root
.type
== bfd_link_hash_undefined
)
4490 size
= addend
+ file_align
;
4496 /* Oops! We've got a reference past the defined end of
4497 the table. This is probably a bug -- shall we warn? */
4498 size
= addend
+ file_align
;
4501 size
= (size
+ file_align
- 1) & -file_align
;
4503 /* Allocate one extra entry for use as a "done" flag for the
4504 consolidation pass. */
4505 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
4509 ptr
= bfd_realloc (ptr
- 1, bytes
);
4515 oldbytes
= (((h
->vtable_entries_size
>> log_file_align
) + 1)
4516 * sizeof (bfd_boolean
));
4517 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
4521 ptr
= bfd_zmalloc (bytes
);
4526 /* And arrange for that done flag to be at index -1. */
4527 h
->vtable_entries_used
= ptr
+ 1;
4528 h
->vtable_entries_size
= size
;
4531 h
->vtable_entries_used
[addend
>> log_file_align
] = TRUE
;
4536 /* And an accompanying bit to work out final got entry offsets once
4537 we're done. Should be called from final_link. */
4540 elf_gc_common_finalize_got_offsets (bfd
*abfd
,
4541 struct bfd_link_info
*info
)
4544 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4547 if (! is_elf_hash_table (info
->hash
))
4550 /* The GOT offset is relative to the .got section, but the GOT header is
4551 put into the .got.plt section, if the backend uses it. */
4552 if (bed
->want_got_plt
)
4555 gotoff
= bed
->got_header_size
;
4557 /* Do the local .got entries first. */
4558 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
4560 bfd_signed_vma
*local_got
;
4561 bfd_size_type j
, locsymcount
;
4562 Elf_Internal_Shdr
*symtab_hdr
;
4564 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
4567 local_got
= elf_local_got_refcounts (i
);
4571 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
4572 if (elf_bad_symtab (i
))
4573 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
4575 locsymcount
= symtab_hdr
->sh_info
;
4577 for (j
= 0; j
< locsymcount
; ++j
)
4579 if (local_got
[j
] > 0)
4581 local_got
[j
] = gotoff
;
4582 gotoff
+= ARCH_SIZE
/ 8;
4585 local_got
[j
] = (bfd_vma
) -1;
4589 /* Then the global .got entries. .plt refcounts are handled by
4590 adjust_dynamic_symbol */
4591 elf_link_hash_traverse (elf_hash_table (info
),
4592 elf_gc_allocate_got_offsets
,
4597 /* We need a special top-level link routine to convert got reference counts
4598 to real got offsets. */
4601 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *offarg
)
4603 bfd_vma
*off
= offarg
;
4605 if (h
->root
.type
== bfd_link_hash_warning
)
4606 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4608 if (h
->got
.refcount
> 0)
4610 h
->got
.offset
= off
[0];
4611 off
[0] += ARCH_SIZE
/ 8;
4614 h
->got
.offset
= (bfd_vma
) -1;
4619 /* Many folk need no more in the way of final link than this, once
4620 got entry reference counting is enabled. */
4623 elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
4625 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
4628 /* Invoke the regular ELF backend linker to do all the work. */
4629 return elf_bfd_final_link (abfd
, info
);
4633 elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
4635 struct elf_reloc_cookie
*rcookie
= cookie
;
4637 if (rcookie
->bad_symtab
)
4638 rcookie
->rel
= rcookie
->rels
;
4640 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
4642 unsigned long r_symndx
;
4644 if (! rcookie
->bad_symtab
)
4645 if (rcookie
->rel
->r_offset
> offset
)
4647 if (rcookie
->rel
->r_offset
!= offset
)
4650 r_symndx
= ELF_R_SYM (rcookie
->rel
->r_info
);
4651 if (r_symndx
== SHN_UNDEF
)
4654 if (r_symndx
>= rcookie
->locsymcount
4655 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
4657 struct elf_link_hash_entry
*h
;
4659 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
4661 while (h
->root
.type
== bfd_link_hash_indirect
4662 || h
->root
.type
== bfd_link_hash_warning
)
4663 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4665 if ((h
->root
.type
== bfd_link_hash_defined
4666 || h
->root
.type
== bfd_link_hash_defweak
)
4667 && elf_discarded_section (h
->root
.u
.def
.section
))
4674 /* It's not a relocation against a global symbol,
4675 but it could be a relocation against a local
4676 symbol for a discarded section. */
4678 Elf_Internal_Sym
*isym
;
4680 /* Need to: get the symbol; get the section. */
4681 isym
= &rcookie
->locsyms
[r_symndx
];
4682 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
4684 isec
= section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
4685 if (isec
!= NULL
&& elf_discarded_section (isec
))
4694 /* Discard unneeded references to discarded sections.
4695 Returns TRUE if any section's size was changed. */
4696 /* This function assumes that the relocations are in sorted order,
4697 which is true for all known assemblers. */
4700 elf_bfd_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
4702 struct elf_reloc_cookie cookie
;
4703 asection
*stab
, *eh
;
4704 Elf_Internal_Shdr
*symtab_hdr
;
4705 const struct elf_backend_data
*bed
;
4708 bfd_boolean ret
= FALSE
;
4710 if (info
->traditional_format
4711 || !is_elf_hash_table (info
->hash
))
4714 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
4716 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
4719 bed
= get_elf_backend_data (abfd
);
4721 if ((abfd
->flags
& DYNAMIC
) != 0)
4724 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
4725 if (info
->relocatable
4727 && (eh
->_raw_size
== 0
4728 || bfd_is_abs_section (eh
->output_section
))))
4731 stab
= bfd_get_section_by_name (abfd
, ".stab");
4733 && (stab
->_raw_size
== 0
4734 || bfd_is_abs_section (stab
->output_section
)
4735 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
4740 && bed
->elf_backend_discard_info
== NULL
)
4743 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4745 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
4746 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
4747 if (cookie
.bad_symtab
)
4749 cookie
.locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
4750 cookie
.extsymoff
= 0;
4754 cookie
.locsymcount
= symtab_hdr
->sh_info
;
4755 cookie
.extsymoff
= symtab_hdr
->sh_info
;
4758 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
4759 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
4761 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
4762 cookie
.locsymcount
, 0,
4764 if (cookie
.locsyms
== NULL
)
4771 count
= stab
->reloc_count
;
4773 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
4775 if (cookie
.rels
!= NULL
)
4777 cookie
.rel
= cookie
.rels
;
4778 cookie
.relend
= cookie
.rels
;
4779 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
4780 if (_bfd_discard_section_stabs (abfd
, stab
,
4781 elf_section_data (stab
)->sec_info
,
4782 elf_reloc_symbol_deleted_p
,
4785 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
4793 count
= eh
->reloc_count
;
4795 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
4797 cookie
.rel
= cookie
.rels
;
4798 cookie
.relend
= cookie
.rels
;
4799 if (cookie
.rels
!= NULL
)
4800 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
4802 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
4803 elf_reloc_symbol_deleted_p
,
4807 if (cookie
.rels
!= NULL
4808 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
4812 if (bed
->elf_backend_discard_info
!= NULL
4813 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
4816 if (cookie
.locsyms
!= NULL
4817 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
4819 if (! info
->keep_memory
)
4820 free (cookie
.locsyms
);
4822 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
4826 if (info
->eh_frame_hdr
4827 && !info
->relocatable
4828 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
4835 elf_section_ignore_discarded_relocs (asection
*sec
)
4837 const struct elf_backend_data
*bed
;
4839 switch (sec
->sec_info_type
)
4841 case ELF_INFO_TYPE_STABS
:
4842 case ELF_INFO_TYPE_EH_FRAME
:
4848 bed
= get_elf_backend_data (sec
->owner
);
4849 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
4850 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))