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_finalize_dynstr (bfd
*, struct bfd_link_info
*);
27 static bfd_boolean
elf_collect_hash_codes (struct elf_link_hash_entry
*,
29 static bfd_boolean
elf_section_ignore_discarded_relocs (asection
*);
31 /* Given an ELF BFD, add symbols to the global hash table as
35 elf_bfd_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
37 switch (bfd_get_format (abfd
))
40 return elf_link_add_object_symbols (abfd
, info
);
42 return _bfd_elf_link_add_archive_symbols (abfd
, info
);
44 bfd_set_error (bfd_error_wrong_format
);
49 /* Sort symbol by value and section. */
51 sort_symbol (const void *arg1
, const void *arg2
)
53 const struct elf_link_hash_entry
*h1
54 = *(const struct elf_link_hash_entry
**) arg1
;
55 const struct elf_link_hash_entry
*h2
56 = *(const struct elf_link_hash_entry
**) arg2
;
57 bfd_signed_vma vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
60 return vdiff
> 0 ? 1 : -1;
63 long sdiff
= h1
->root
.u
.def
.section
- h2
->root
.u
.def
.section
;
65 return sdiff
> 0 ? 1 : -1;
71 /* Add symbols from an ELF object file to the linker hash table. */
74 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
76 bfd_boolean (*add_symbol_hook
)
77 (bfd
*, struct bfd_link_info
*, const Elf_Internal_Sym
*,
78 const char **, flagword
*, asection
**, bfd_vma
*);
79 bfd_boolean (*check_relocs
)
80 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
82 Elf_Internal_Shdr
*hdr
;
83 bfd_size_type symcount
;
84 bfd_size_type extsymcount
;
85 bfd_size_type extsymoff
;
86 struct elf_link_hash_entry
**sym_hash
;
88 Elf_External_Versym
*extversym
= NULL
;
89 Elf_External_Versym
*ever
;
90 struct elf_link_hash_entry
*weaks
;
91 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
92 bfd_size_type nondeflt_vers_cnt
= 0;
93 Elf_Internal_Sym
*isymbuf
= NULL
;
94 Elf_Internal_Sym
*isym
;
95 Elf_Internal_Sym
*isymend
;
96 const struct elf_backend_data
*bed
;
97 bfd_boolean dt_needed
;
98 bfd_boolean add_needed
;
99 struct elf_link_hash_table
* hash_table
;
102 hash_table
= elf_hash_table (info
);
104 bed
= get_elf_backend_data (abfd
);
105 add_symbol_hook
= bed
->elf_add_symbol_hook
;
106 collect
= bed
->collect
;
108 if ((abfd
->flags
& DYNAMIC
) == 0)
114 /* You can't use -r against a dynamic object. Also, there's no
115 hope of using a dynamic object which does not exactly match
116 the format of the output file. */
117 if (info
->relocatable
118 || !is_elf_hash_table (hash_table
)
119 || hash_table
->root
.creator
!= abfd
->xvec
)
121 bfd_set_error (bfd_error_invalid_operation
);
126 /* As a GNU extension, any input sections which are named
127 .gnu.warning.SYMBOL are treated as warning symbols for the given
128 symbol. This differs from .gnu.warning sections, which generate
129 warnings when they are included in an output file. */
130 if (info
->executable
)
134 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
138 name
= bfd_get_section_name (abfd
, s
);
139 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
143 bfd_size_type prefix_len
;
144 const char * gnu_warning_prefix
= _("warning: ");
146 name
+= sizeof ".gnu.warning." - 1;
148 /* If this is a shared object, then look up the symbol
149 in the hash table. If it is there, and it is already
150 been defined, then we will not be using the entry
151 from this shared object, so we don't need to warn.
152 FIXME: If we see the definition in a regular object
153 later on, we will warn, but we shouldn't. The only
154 fix is to keep track of what warnings we are supposed
155 to emit, and then handle them all at the end of the
159 struct elf_link_hash_entry
*h
;
161 h
= elf_link_hash_lookup (hash_table
, name
,
164 /* FIXME: What about bfd_link_hash_common? */
166 && (h
->root
.type
== bfd_link_hash_defined
167 || h
->root
.type
== bfd_link_hash_defweak
))
169 /* We don't want to issue this warning. Clobber
170 the section size so that the warning does not
171 get copied into the output file. */
177 sz
= bfd_section_size (abfd
, s
);
178 prefix_len
= strlen (gnu_warning_prefix
);
179 msg
= bfd_alloc (abfd
, prefix_len
+ sz
+ 1);
183 strcpy (msg
, gnu_warning_prefix
);
184 if (! bfd_get_section_contents (abfd
, s
, msg
+ prefix_len
, 0, sz
))
187 msg
[prefix_len
+ sz
] = '\0';
189 if (! (_bfd_generic_link_add_one_symbol
190 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
191 FALSE
, collect
, NULL
)))
194 if (! info
->relocatable
)
196 /* Clobber the section size so that the warning does
197 not get copied into the output file. */
208 /* If we are creating a shared library, create all the dynamic
209 sections immediately. We need to attach them to something,
210 so we attach them to this BFD, provided it is the right
211 format. FIXME: If there are no input BFD's of the same
212 format as the output, we can't make a shared library. */
214 && is_elf_hash_table (hash_table
)
215 && hash_table
->root
.creator
== abfd
->xvec
216 && ! hash_table
->dynamic_sections_created
)
218 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
222 else if (!is_elf_hash_table (hash_table
))
228 bfd_size_type oldsize
;
229 bfd_size_type strindex
;
230 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
232 /* ld --just-symbols and dynamic objects don't mix very well.
233 Test for --just-symbols by looking at info set up by
234 _bfd_elf_link_just_syms. */
235 if ((s
= abfd
->sections
) != NULL
236 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
239 /* Find the name to use in a DT_NEEDED entry that refers to this
240 object. If the object has a DT_SONAME entry, we use it.
241 Otherwise, if the generic linker stuck something in
242 elf_dt_name, we use that. Otherwise, we just use the file
243 name. If the generic linker put a null string into
244 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
245 there is a DT_SONAME entry. */
247 name
= bfd_get_filename (abfd
);
248 if (elf_dt_name (abfd
) != NULL
)
250 name
= elf_dt_name (abfd
);
253 if (elf_dt_soname (abfd
) != NULL
)
259 s
= bfd_get_section_by_name (abfd
, ".dynamic");
262 Elf_External_Dyn
*dynbuf
= NULL
;
263 Elf_External_Dyn
*extdyn
;
264 Elf_External_Dyn
*extdynend
;
266 unsigned long shlink
;
268 dynbuf
= bfd_malloc (s
->_raw_size
);
272 if (! bfd_get_section_contents (abfd
, s
, dynbuf
, 0, s
->_raw_size
))
275 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
278 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
281 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
282 for (; extdyn
< extdynend
; extdyn
++)
284 Elf_Internal_Dyn dyn
;
286 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
287 if (dyn
.d_tag
== DT_SONAME
)
289 unsigned int tagv
= dyn
.d_un
.d_val
;
290 name
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
294 if (dyn
.d_tag
== DT_NEEDED
)
296 struct bfd_link_needed_list
*n
, **pn
;
298 unsigned int tagv
= dyn
.d_un
.d_val
;
300 amt
= sizeof (struct bfd_link_needed_list
);
301 n
= bfd_alloc (abfd
, amt
);
302 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
303 if (n
== NULL
|| fnm
== NULL
)
305 amt
= strlen (fnm
) + 1;
306 anm
= bfd_alloc (abfd
, amt
);
309 memcpy (anm
, fnm
, amt
);
313 for (pn
= & hash_table
->needed
;
319 if (dyn
.d_tag
== DT_RUNPATH
)
321 struct bfd_link_needed_list
*n
, **pn
;
323 unsigned int tagv
= dyn
.d_un
.d_val
;
325 amt
= sizeof (struct bfd_link_needed_list
);
326 n
= bfd_alloc (abfd
, amt
);
327 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
328 if (n
== NULL
|| fnm
== NULL
)
330 amt
= strlen (fnm
) + 1;
331 anm
= bfd_alloc (abfd
, amt
);
334 memcpy (anm
, fnm
, amt
);
344 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
345 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
347 struct bfd_link_needed_list
*n
, **pn
;
349 unsigned int tagv
= dyn
.d_un
.d_val
;
351 amt
= sizeof (struct bfd_link_needed_list
);
352 n
= bfd_alloc (abfd
, amt
);
353 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
354 if (n
== NULL
|| fnm
== NULL
)
356 amt
= strlen (fnm
) + 1;
357 anm
= bfd_alloc (abfd
, amt
);
364 memcpy (anm
, fnm
, amt
);
379 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
380 frees all more recently bfd_alloc'd blocks as well. */
386 struct bfd_link_needed_list
**pn
;
387 for (pn
= & hash_table
->runpath
;
394 /* We do not want to include any of the sections in a dynamic
395 object in the output file. We hack by simply clobbering the
396 list of sections in the BFD. This could be handled more
397 cleanly by, say, a new section flag; the existing
398 SEC_NEVER_LOAD flag is not the one we want, because that one
399 still implies that the section takes up space in the output
401 bfd_section_list_clear (abfd
);
403 /* If this is the first dynamic object found in the link, create
404 the special sections required for dynamic linking. */
405 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
410 /* Add a DT_NEEDED entry for this dynamic object. */
411 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
412 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, name
, FALSE
);
413 if (strindex
== (bfd_size_type
) -1)
416 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
419 Elf_External_Dyn
*dyncon
, *dynconend
;
421 /* The hash table size did not change, which means that
422 the dynamic object name was already entered. If we
423 have already included this dynamic object in the
424 link, just ignore it. There is no reason to include
425 a particular dynamic object more than once. */
426 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
427 BFD_ASSERT (sdyn
!= NULL
);
429 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
430 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
432 for (; dyncon
< dynconend
; dyncon
++)
434 Elf_Internal_Dyn dyn
;
436 elf_swap_dyn_in (hash_table
->dynobj
, dyncon
, & dyn
);
437 if (dyn
.d_tag
== DT_NEEDED
438 && dyn
.d_un
.d_val
== strindex
)
440 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
446 if (! elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
450 /* Save the SONAME, if there is one, because sometimes the
451 linker emulation code will need to know it. */
453 name
= basename (bfd_get_filename (abfd
));
454 elf_dt_name (abfd
) = name
;
457 /* If this is a dynamic object, we always link against the .dynsym
458 symbol table, not the .symtab symbol table. The dynamic linker
459 will only see the .dynsym symbol table, so there is no reason to
460 look at .symtab for a dynamic object. */
462 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
463 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
465 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
467 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
469 /* The sh_info field of the symtab header tells us where the
470 external symbols start. We don't care about the local symbols at
472 if (elf_bad_symtab (abfd
))
474 extsymcount
= symcount
;
479 extsymcount
= symcount
- hdr
->sh_info
;
480 extsymoff
= hdr
->sh_info
;
484 if (extsymcount
!= 0)
486 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
491 /* We store a pointer to the hash table entry for each external
493 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
494 sym_hash
= bfd_alloc (abfd
, amt
);
495 if (sym_hash
== NULL
)
497 elf_sym_hashes (abfd
) = sym_hash
;
502 /* Read in any version definitions. */
503 if (! _bfd_elf_slurp_version_tables (abfd
))
506 /* Read in the symbol versions, but don't bother to convert them
507 to internal format. */
508 if (elf_dynversym (abfd
) != 0)
510 Elf_Internal_Shdr
*versymhdr
;
512 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
513 extversym
= bfd_malloc (versymhdr
->sh_size
);
514 if (extversym
== NULL
)
516 amt
= versymhdr
->sh_size
;
517 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
518 || bfd_bread (extversym
, amt
, abfd
) != amt
)
519 goto error_free_vers
;
525 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
526 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
528 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
535 struct elf_link_hash_entry
*h
;
536 bfd_boolean definition
;
537 bfd_boolean size_change_ok
;
538 bfd_boolean type_change_ok
;
539 bfd_boolean new_weakdef
;
540 bfd_boolean override
;
541 unsigned int old_alignment
;
546 flags
= BSF_NO_FLAGS
;
548 value
= isym
->st_value
;
551 bind
= ELF_ST_BIND (isym
->st_info
);
552 if (bind
== STB_LOCAL
)
554 /* This should be impossible, since ELF requires that all
555 global symbols follow all local symbols, and that sh_info
556 point to the first global symbol. Unfortunately, Irix 5
560 else if (bind
== STB_GLOBAL
)
562 if (isym
->st_shndx
!= SHN_UNDEF
563 && isym
->st_shndx
!= SHN_COMMON
)
566 else if (bind
== STB_WEAK
)
570 /* Leave it up to the processor backend. */
573 if (isym
->st_shndx
== SHN_UNDEF
)
574 sec
= bfd_und_section_ptr
;
575 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
577 sec
= section_from_elf_index (abfd
, isym
->st_shndx
);
579 sec
= bfd_abs_section_ptr
;
580 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
583 else if (isym
->st_shndx
== SHN_ABS
)
584 sec
= bfd_abs_section_ptr
;
585 else if (isym
->st_shndx
== SHN_COMMON
)
587 sec
= bfd_com_section_ptr
;
588 /* What ELF calls the size we call the value. What ELF
589 calls the value we call the alignment. */
590 value
= isym
->st_size
;
594 /* Leave it up to the processor backend. */
597 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
600 goto error_free_vers
;
602 if (isym
->st_shndx
== SHN_COMMON
603 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
605 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
609 tcomm
= bfd_make_section (abfd
, ".tcommon");
611 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
614 | SEC_THREAD_LOCAL
)))
615 goto error_free_vers
;
619 else if (add_symbol_hook
)
621 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
623 goto error_free_vers
;
625 /* The hook function sets the name to NULL if this symbol
626 should be skipped for some reason. */
631 /* Sanity check that all possibilities were handled. */
634 bfd_set_error (bfd_error_bad_value
);
635 goto error_free_vers
;
638 if (bfd_is_und_section (sec
)
639 || bfd_is_com_section (sec
))
644 size_change_ok
= FALSE
;
645 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
649 if (is_elf_hash_table (hash_table
))
651 Elf_Internal_Versym iver
;
652 unsigned int vernum
= 0;
657 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
658 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
660 /* If this is a hidden symbol, or if it is not version
661 1, we append the version name to the symbol name.
662 However, we do not modify a non-hidden absolute
663 symbol, because it might be the version symbol
664 itself. FIXME: What if it isn't? */
665 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
666 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
669 size_t namelen
, verlen
, newlen
;
672 if (isym
->st_shndx
!= SHN_UNDEF
)
674 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
676 (*_bfd_error_handler
)
677 (_("%s: %s: invalid version %u (max %d)"),
678 bfd_archive_filename (abfd
), name
, vernum
,
679 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
680 bfd_set_error (bfd_error_bad_value
);
681 goto error_free_vers
;
685 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
691 /* We cannot simply test for the number of
692 entries in the VERNEED section since the
693 numbers for the needed versions do not start
695 Elf_Internal_Verneed
*t
;
698 for (t
= elf_tdata (abfd
)->verref
;
702 Elf_Internal_Vernaux
*a
;
704 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
706 if (a
->vna_other
== vernum
)
708 verstr
= a
->vna_nodename
;
717 (*_bfd_error_handler
)
718 (_("%s: %s: invalid needed version %d"),
719 bfd_archive_filename (abfd
), name
, vernum
);
720 bfd_set_error (bfd_error_bad_value
);
721 goto error_free_vers
;
725 namelen
= strlen (name
);
726 verlen
= strlen (verstr
);
727 newlen
= namelen
+ verlen
+ 2;
728 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
729 && isym
->st_shndx
!= SHN_UNDEF
)
732 newname
= bfd_alloc (abfd
, newlen
);
734 goto error_free_vers
;
735 memcpy (newname
, name
, namelen
);
736 p
= newname
+ namelen
;
738 /* If this is a defined non-hidden version symbol,
739 we add another @ to the name. This indicates the
740 default version of the symbol. */
741 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
742 && isym
->st_shndx
!= SHN_UNDEF
)
744 memcpy (p
, verstr
, verlen
+ 1);
750 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
751 sym_hash
, &skip
, &override
,
752 &type_change_ok
, &size_change_ok
,
754 goto error_free_vers
;
763 while (h
->root
.type
== bfd_link_hash_indirect
764 || h
->root
.type
== bfd_link_hash_warning
)
765 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
767 /* Remember the old alignment if this is a common symbol, so
768 that we don't reduce the alignment later on. We can't
769 check later, because _bfd_generic_link_add_one_symbol
770 will set a default for the alignment which we want to
771 override. We also remember the old bfd where the existing
772 definition comes from. */
773 switch (h
->root
.type
)
778 case bfd_link_hash_defined
:
779 case bfd_link_hash_defweak
:
780 old_bfd
= h
->root
.u
.def
.section
->owner
;
783 case bfd_link_hash_common
:
784 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
785 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
789 if (elf_tdata (abfd
)->verdef
!= NULL
793 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
796 if (! (_bfd_generic_link_add_one_symbol
797 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
798 (struct bfd_link_hash_entry
**) sym_hash
)))
799 goto error_free_vers
;
802 while (h
->root
.type
== bfd_link_hash_indirect
803 || h
->root
.type
== bfd_link_hash_warning
)
804 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
810 && (flags
& BSF_WEAK
) != 0
811 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
812 && is_elf_hash_table (hash_table
)
813 && h
->weakdef
== NULL
)
815 /* Keep a list of all weak defined non function symbols from
816 a dynamic object, using the weakdef field. Later in this
817 function we will set the weakdef field to the correct
818 value. We only put non-function symbols from dynamic
819 objects on this list, because that happens to be the only
820 time we need to know the normal symbol corresponding to a
821 weak symbol, and the information is time consuming to
822 figure out. If the weakdef field is not already NULL,
823 then this symbol was already defined by some previous
824 dynamic object, and we will be using that previous
825 definition anyhow. */
832 /* Set the alignment of a common symbol. */
833 if (isym
->st_shndx
== SHN_COMMON
834 && h
->root
.type
== bfd_link_hash_common
)
838 align
= bfd_log2 (isym
->st_value
);
839 if (align
> old_alignment
840 /* Permit an alignment power of zero if an alignment of one
841 is specified and no other alignments have been specified. */
842 || (isym
->st_value
== 1 && old_alignment
== 0))
843 h
->root
.u
.c
.p
->alignment_power
= align
;
845 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
848 if (is_elf_hash_table (hash_table
))
854 /* Check the alignment when a common symbol is involved. This
855 can change when a common symbol is overridden by a normal
856 definition or a common symbol is ignored due to the old
857 normal definition. We need to make sure the maximum
858 alignment is maintained. */
859 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
860 && h
->root
.type
!= bfd_link_hash_common
)
862 unsigned int common_align
;
863 unsigned int normal_align
;
864 unsigned int symbol_align
;
868 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
869 if (h
->root
.u
.def
.section
->owner
!= NULL
870 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
872 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
873 if (normal_align
> symbol_align
)
874 normal_align
= symbol_align
;
877 normal_align
= symbol_align
;
881 common_align
= old_alignment
;
882 common_bfd
= old_bfd
;
887 common_align
= bfd_log2 (isym
->st_value
);
889 normal_bfd
= old_bfd
;
892 if (normal_align
< common_align
)
893 (*_bfd_error_handler
)
894 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
897 bfd_archive_filename (normal_bfd
),
899 bfd_archive_filename (common_bfd
));
902 /* Remember the symbol size and type. */
903 if (isym
->st_size
!= 0
904 && (definition
|| h
->size
== 0))
906 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
907 (*_bfd_error_handler
)
908 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
909 name
, (unsigned long) h
->size
,
910 bfd_archive_filename (old_bfd
),
911 (unsigned long) isym
->st_size
,
912 bfd_archive_filename (abfd
));
914 h
->size
= isym
->st_size
;
917 /* If this is a common symbol, then we always want H->SIZE
918 to be the size of the common symbol. The code just above
919 won't fix the size if a common symbol becomes larger. We
920 don't warn about a size change here, because that is
921 covered by --warn-common. */
922 if (h
->root
.type
== bfd_link_hash_common
)
923 h
->size
= h
->root
.u
.c
.size
;
925 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
926 && (definition
|| h
->type
== STT_NOTYPE
))
928 if (h
->type
!= STT_NOTYPE
929 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
931 (*_bfd_error_handler
)
932 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
933 name
, h
->type
, ELF_ST_TYPE (isym
->st_info
),
934 bfd_archive_filename (abfd
));
936 h
->type
= ELF_ST_TYPE (isym
->st_info
);
939 /* If st_other has a processor-specific meaning, specific
940 code might be needed here. We never merge the visibility
941 attribute with the one from a dynamic object. */
942 if (bed
->elf_backend_merge_symbol_attribute
)
943 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
946 if (isym
->st_other
!= 0 && !dynamic
)
948 unsigned char hvis
, symvis
, other
, nvis
;
950 /* Take the balance of OTHER from the definition. */
951 other
= (definition
? isym
->st_other
: h
->other
);
952 other
&= ~ ELF_ST_VISIBILITY (-1);
954 /* Combine visibilities, using the most constraining one. */
955 hvis
= ELF_ST_VISIBILITY (h
->other
);
956 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
962 nvis
= hvis
< symvis
? hvis
: symvis
;
964 h
->other
= other
| nvis
;
967 /* Set a flag in the hash table entry indicating the type of
968 reference or definition we just found. Keep a count of
969 the number of dynamic symbols we find. A dynamic symbol
970 is one which is referenced or defined by both a regular
971 object and a shared object. */
972 old_flags
= h
->elf_link_hash_flags
;
978 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
979 if (bind
!= STB_WEAK
)
980 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
983 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
984 if (! info
->executable
985 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
986 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
992 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
994 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
995 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
996 | ELF_LINK_HASH_REF_REGULAR
)) != 0
997 || (h
->weakdef
!= NULL
999 && h
->weakdef
->dynindx
!= -1))
1003 h
->elf_link_hash_flags
|= new_flag
;
1005 /* Check to see if we need to add an indirect symbol for
1006 the default name. */
1007 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
1008 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
1009 &sec
, &value
, &dynsym
,
1010 override
, dt_needed
))
1011 goto error_free_vers
;
1013 if (definition
&& !dynamic
)
1015 char *p
= strchr (name
, ELF_VER_CHR
);
1016 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
1018 /* Queue non-default versions so that .symver x, x@FOO
1019 aliases can be checked. */
1020 if (! nondeflt_vers
)
1022 amt
= (isymend
- isym
+ 1)
1023 * sizeof (struct elf_link_hash_entry
*);
1024 nondeflt_vers
= bfd_malloc (amt
);
1026 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
1030 if (dynsym
&& h
->dynindx
== -1)
1032 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1033 goto error_free_vers
;
1034 if (h
->weakdef
!= NULL
1036 && h
->weakdef
->dynindx
== -1)
1038 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
1039 goto error_free_vers
;
1042 else if (dynsym
&& h
->dynindx
!= -1)
1043 /* If the symbol already has a dynamic index, but
1044 visibility says it should not be visible, turn it into
1046 switch (ELF_ST_VISIBILITY (h
->other
))
1050 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1054 if (dt_needed
&& !add_needed
&& definition
1055 && (h
->elf_link_hash_flags
1056 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1058 bfd_size_type oldsize
;
1059 bfd_size_type strindex
;
1061 /* The symbol from a DT_NEEDED object is referenced from
1062 the regular object to create a dynamic executable. We
1063 have to make sure there is a DT_NEEDED entry for it. */
1066 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
1067 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
,
1068 elf_dt_soname (abfd
), FALSE
);
1069 if (strindex
== (bfd_size_type
) -1)
1070 goto error_free_vers
;
1072 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
1075 Elf_External_Dyn
*dyncon
, *dynconend
;
1077 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
,
1079 BFD_ASSERT (sdyn
!= NULL
);
1081 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1082 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1084 for (; dyncon
< dynconend
; dyncon
++)
1086 Elf_Internal_Dyn dyn
;
1088 elf_swap_dyn_in (hash_table
->dynobj
,
1090 BFD_ASSERT (dyn
.d_tag
!= DT_NEEDED
||
1091 dyn
.d_un
.d_val
!= strindex
);
1095 if (! elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
1096 goto error_free_vers
;
1101 /* Now that all the symbols from this input file are created, handle
1102 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
1103 if (nondeflt_vers
!= NULL
)
1105 bfd_size_type cnt
, symidx
;
1107 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
1109 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
1110 char *shortname
, *p
;
1112 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1114 || (h
->root
.type
!= bfd_link_hash_defined
1115 && h
->root
.type
!= bfd_link_hash_defweak
))
1118 amt
= p
- h
->root
.root
.string
;
1119 shortname
= bfd_malloc (amt
+ 1);
1120 memcpy (shortname
, h
->root
.root
.string
, amt
);
1121 shortname
[amt
] = '\0';
1123 hi
= (struct elf_link_hash_entry
*)
1124 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
1125 FALSE
, FALSE
, FALSE
);
1127 && hi
->root
.type
== h
->root
.type
1128 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
1129 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
1131 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1132 hi
->root
.type
= bfd_link_hash_indirect
;
1133 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
1134 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1135 sym_hash
= elf_sym_hashes (abfd
);
1137 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
1138 if (sym_hash
[symidx
] == hi
)
1140 sym_hash
[symidx
] = h
;
1146 free (nondeflt_vers
);
1147 nondeflt_vers
= NULL
;
1150 if (extversym
!= NULL
)
1156 if (isymbuf
!= NULL
)
1160 /* Now set the weakdefs field correctly for all the weak defined
1161 symbols we found. The only way to do this is to search all the
1162 symbols. Since we only need the information for non functions in
1163 dynamic objects, that's the only time we actually put anything on
1164 the list WEAKS. We need this information so that if a regular
1165 object refers to a symbol defined weakly in a dynamic object, the
1166 real symbol in the dynamic object is also put in the dynamic
1167 symbols; we also must arrange for both symbols to point to the
1168 same memory location. We could handle the general case of symbol
1169 aliasing, but a general symbol alias can only be generated in
1170 assembler code, handling it correctly would be very time
1171 consuming, and other ELF linkers don't handle general aliasing
1175 struct elf_link_hash_entry
**hpp
;
1176 struct elf_link_hash_entry
**hppend
;
1177 struct elf_link_hash_entry
**sorted_sym_hash
;
1178 struct elf_link_hash_entry
*h
;
1181 /* Since we have to search the whole symbol list for each weak
1182 defined symbol, search time for N weak defined symbols will be
1183 O(N^2). Binary search will cut it down to O(NlogN). */
1184 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
1185 sorted_sym_hash
= bfd_malloc (amt
);
1186 if (sorted_sym_hash
== NULL
)
1188 sym_hash
= sorted_sym_hash
;
1189 hpp
= elf_sym_hashes (abfd
);
1190 hppend
= hpp
+ extsymcount
;
1192 for (; hpp
< hppend
; hpp
++)
1196 && h
->root
.type
== bfd_link_hash_defined
1197 && h
->type
!= STT_FUNC
)
1205 qsort (sorted_sym_hash
, sym_count
,
1206 sizeof (struct elf_link_hash_entry
*),
1209 while (weaks
!= NULL
)
1211 struct elf_link_hash_entry
*hlook
;
1218 weaks
= hlook
->weakdef
;
1219 hlook
->weakdef
= NULL
;
1221 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
1222 || hlook
->root
.type
== bfd_link_hash_defweak
1223 || hlook
->root
.type
== bfd_link_hash_common
1224 || hlook
->root
.type
== bfd_link_hash_indirect
);
1225 slook
= hlook
->root
.u
.def
.section
;
1226 vlook
= hlook
->root
.u
.def
.value
;
1233 bfd_signed_vma vdiff
;
1235 h
= sorted_sym_hash
[idx
];
1236 vdiff
= vlook
- h
->root
.u
.def
.value
;
1243 long sdiff
= slook
- h
->root
.u
.def
.section
;
1256 /* We didn't find a value/section match. */
1260 for (i
= ilook
; i
< sym_count
; i
++)
1262 h
= sorted_sym_hash
[i
];
1264 /* Stop if value or section doesn't match. */
1265 if (h
->root
.u
.def
.value
!= vlook
1266 || h
->root
.u
.def
.section
!= slook
)
1268 else if (h
!= hlook
)
1272 /* If the weak definition is in the list of dynamic
1273 symbols, make sure the real definition is put
1275 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
1277 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1282 /* If the real definition is in the list of dynamic
1283 symbols, make sure the weak definition is put
1284 there as well. If we don't do this, then the
1285 dynamic loader might not merge the entries for the
1286 real definition and the weak definition. */
1287 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
1289 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1298 free (sorted_sym_hash
);
1301 /* If this object is the same format as the output object, and it is
1302 not a shared library, then let the backend look through the
1305 This is required to build global offset table entries and to
1306 arrange for dynamic relocs. It is not required for the
1307 particular common case of linking non PIC code, even when linking
1308 against shared libraries, but unfortunately there is no way of
1309 knowing whether an object file has been compiled PIC or not.
1310 Looking through the relocs is not particularly time consuming.
1311 The problem is that we must either (1) keep the relocs in memory,
1312 which causes the linker to require additional runtime memory or
1313 (2) read the relocs twice from the input file, which wastes time.
1314 This would be a good case for using mmap.
1316 I have no idea how to handle linking PIC code into a file of a
1317 different format. It probably can't be done. */
1318 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
1320 && is_elf_hash_table (hash_table
)
1321 && hash_table
->root
.creator
== abfd
->xvec
1322 && check_relocs
!= NULL
)
1326 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1328 Elf_Internal_Rela
*internal_relocs
;
1331 if ((o
->flags
& SEC_RELOC
) == 0
1332 || o
->reloc_count
== 0
1333 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
1334 && (o
->flags
& SEC_DEBUGGING
) != 0)
1335 || bfd_is_abs_section (o
->output_section
))
1338 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
1340 if (internal_relocs
== NULL
)
1343 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
1345 if (elf_section_data (o
)->relocs
!= internal_relocs
)
1346 free (internal_relocs
);
1353 /* If this is a non-traditional link, try to optimize the handling
1354 of the .stab/.stabstr sections. */
1356 && ! info
->traditional_format
1357 && is_elf_hash_table (hash_table
)
1358 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
1362 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
1363 if (stabstr
!= NULL
)
1365 bfd_size_type string_offset
= 0;
1368 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
1369 if (strncmp (".stab", stab
->name
, 5) == 0
1370 && (!stab
->name
[5] ||
1371 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
1372 && (stab
->flags
& SEC_MERGE
) == 0
1373 && !bfd_is_abs_section (stab
->output_section
))
1375 struct bfd_elf_section_data
*secdata
;
1377 secdata
= elf_section_data (stab
);
1378 if (! _bfd_link_section_stabs (abfd
,
1379 & hash_table
->stab_info
,
1384 if (secdata
->sec_info
)
1385 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
1390 if (! info
->relocatable
1392 && is_elf_hash_table (hash_table
))
1396 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
1397 if ((s
->flags
& SEC_MERGE
) != 0
1398 && !bfd_is_abs_section (s
->output_section
))
1400 struct bfd_elf_section_data
*secdata
;
1402 secdata
= elf_section_data (s
);
1403 if (! _bfd_merge_section (abfd
,
1404 & hash_table
->merge_info
,
1405 s
, &secdata
->sec_info
))
1407 else if (secdata
->sec_info
)
1408 s
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
1412 if (is_elf_hash_table (hash_table
))
1414 /* Add this bfd to the loaded list. */
1415 struct elf_link_loaded_list
*n
;
1417 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
1421 n
->next
= hash_table
->loaded
;
1422 hash_table
->loaded
= n
;
1428 if (nondeflt_vers
!= NULL
)
1429 free (nondeflt_vers
);
1430 if (extversym
!= NULL
)
1433 if (isymbuf
!= NULL
)
1439 /* Add an entry to the .dynamic table. */
1442 elf_add_dynamic_entry (struct bfd_link_info
*info
, bfd_vma tag
, bfd_vma val
)
1444 Elf_Internal_Dyn dyn
;
1447 bfd_size_type newsize
;
1448 bfd_byte
*newcontents
;
1450 if (! is_elf_hash_table (info
->hash
))
1453 dynobj
= elf_hash_table (info
)->dynobj
;
1455 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
1456 BFD_ASSERT (s
!= NULL
);
1458 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
1459 newcontents
= bfd_realloc (s
->contents
, newsize
);
1460 if (newcontents
== NULL
)
1464 dyn
.d_un
.d_val
= val
;
1465 elf_swap_dyn_out (dynobj
, &dyn
,
1466 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
1468 s
->_raw_size
= newsize
;
1469 s
->contents
= newcontents
;
1474 /* Array used to determine the number of hash table buckets to use
1475 based on the number of symbols there are. If there are fewer than
1476 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
1477 fewer than 37 we use 17 buckets, and so forth. We never use more
1478 than 32771 buckets. */
1480 static const size_t elf_buckets
[] =
1482 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
1486 /* Compute bucket count for hashing table. We do not use a static set
1487 of possible tables sizes anymore. Instead we determine for all
1488 possible reasonable sizes of the table the outcome (i.e., the
1489 number of collisions etc) and choose the best solution. The
1490 weighting functions are not too simple to allow the table to grow
1491 without bounds. Instead one of the weighting factors is the size.
1492 Therefore the result is always a good payoff between few collisions
1493 (= short chain lengths) and table size. */
1495 compute_bucket_count (struct bfd_link_info
*info
)
1497 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
1498 size_t best_size
= 0;
1499 unsigned long int *hashcodes
;
1500 unsigned long int *hashcodesp
;
1501 unsigned long int i
;
1504 /* Compute the hash values for all exported symbols. At the same
1505 time store the values in an array so that we could use them for
1508 amt
*= sizeof (unsigned long int);
1509 hashcodes
= bfd_malloc (amt
);
1510 if (hashcodes
== NULL
)
1512 hashcodesp
= hashcodes
;
1514 /* Put all hash values in HASHCODES. */
1515 elf_link_hash_traverse (elf_hash_table (info
),
1516 elf_collect_hash_codes
, &hashcodesp
);
1518 /* We have a problem here. The following code to optimize the table
1519 size requires an integer type with more the 32 bits. If
1520 BFD_HOST_U_64_BIT is set we know about such a type. */
1521 #ifdef BFD_HOST_U_64_BIT
1524 unsigned long int nsyms
= hashcodesp
- hashcodes
;
1527 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
1528 unsigned long int *counts
;
1530 /* Possible optimization parameters: if we have NSYMS symbols we say
1531 that the hashing table must at least have NSYMS/4 and at most
1533 minsize
= nsyms
/ 4;
1536 best_size
= maxsize
= nsyms
* 2;
1538 /* Create array where we count the collisions in. We must use bfd_malloc
1539 since the size could be large. */
1541 amt
*= sizeof (unsigned long int);
1542 counts
= bfd_malloc (amt
);
1549 /* Compute the "optimal" size for the hash table. The criteria is a
1550 minimal chain length. The minor criteria is (of course) the size
1552 for (i
= minsize
; i
< maxsize
; ++i
)
1554 /* Walk through the array of hashcodes and count the collisions. */
1555 BFD_HOST_U_64_BIT max
;
1556 unsigned long int j
;
1557 unsigned long int fact
;
1559 memset (counts
, '\0', i
* sizeof (unsigned long int));
1561 /* Determine how often each hash bucket is used. */
1562 for (j
= 0; j
< nsyms
; ++j
)
1563 ++counts
[hashcodes
[j
] % i
];
1565 /* For the weight function we need some information about the
1566 pagesize on the target. This is information need not be 100%
1567 accurate. Since this information is not available (so far) we
1568 define it here to a reasonable default value. If it is crucial
1569 to have a better value some day simply define this value. */
1570 # ifndef BFD_TARGET_PAGESIZE
1571 # define BFD_TARGET_PAGESIZE (4096)
1574 /* We in any case need 2 + NSYMS entries for the size values and
1576 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
1579 /* Variant 1: optimize for short chains. We add the squares
1580 of all the chain lengths (which favors many small chain
1581 over a few long chains). */
1582 for (j
= 0; j
< i
; ++j
)
1583 max
+= counts
[j
] * counts
[j
];
1585 /* This adds penalties for the overall size of the table. */
1586 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
1589 /* Variant 2: Optimize a lot more for small table. Here we
1590 also add squares of the size but we also add penalties for
1591 empty slots (the +1 term). */
1592 for (j
= 0; j
< i
; ++j
)
1593 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
1595 /* The overall size of the table is considered, but not as
1596 strong as in variant 1, where it is squared. */
1597 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
1601 /* Compare with current best results. */
1602 if (max
< best_chlen
)
1612 #endif /* defined (BFD_HOST_U_64_BIT) */
1614 /* This is the fallback solution if no 64bit type is available or if we
1615 are not supposed to spend much time on optimizations. We select the
1616 bucket count using a fixed set of numbers. */
1617 for (i
= 0; elf_buckets
[i
] != 0; i
++)
1619 best_size
= elf_buckets
[i
];
1620 if (dynsymcount
< elf_buckets
[i
+ 1])
1625 /* Free the arrays we needed. */
1631 /* Set up the sizes and contents of the ELF dynamic sections. This is
1632 called by the ELF linker emulation before_allocation routine. We
1633 must set the sizes of the sections before the linker sets the
1634 addresses of the various sections. */
1637 NAME(bfd_elf
,size_dynamic_sections
) (bfd
*output_bfd
,
1640 const char *filter_shlib
,
1641 const char * const *auxiliary_filters
,
1642 struct bfd_link_info
*info
,
1643 asection
**sinterpptr
,
1644 struct bfd_elf_version_tree
*verdefs
)
1646 bfd_size_type soname_indx
;
1648 const struct elf_backend_data
*bed
;
1649 struct elf_assign_sym_version_info asvinfo
;
1653 soname_indx
= (bfd_size_type
) -1;
1655 if (!is_elf_hash_table (info
->hash
))
1658 if (info
->execstack
)
1659 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
1660 else if (info
->noexecstack
)
1661 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
1665 asection
*notesec
= NULL
;
1668 for (inputobj
= info
->input_bfds
;
1670 inputobj
= inputobj
->link_next
)
1674 if (inputobj
->flags
& DYNAMIC
)
1676 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
1679 if (s
->flags
& SEC_CODE
)
1688 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
1689 if (exec
&& info
->relocatable
1690 && notesec
->output_section
!= bfd_abs_section_ptr
)
1691 notesec
->output_section
->flags
|= SEC_CODE
;
1695 /* Any syms created from now on start with -1 in
1696 got.refcount/offset and plt.refcount/offset. */
1697 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
1699 /* The backend may have to create some sections regardless of whether
1700 we're dynamic or not. */
1701 bed
= get_elf_backend_data (output_bfd
);
1702 if (bed
->elf_backend_always_size_sections
1703 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
1706 dynobj
= elf_hash_table (info
)->dynobj
;
1708 /* If there were no dynamic objects in the link, there is nothing to
1713 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
1716 if (elf_hash_table (info
)->dynamic_sections_created
)
1718 struct elf_info_failed eif
;
1719 struct elf_link_hash_entry
*h
;
1721 struct bfd_elf_version_tree
*t
;
1722 struct bfd_elf_version_expr
*d
;
1723 bfd_boolean all_defined
;
1725 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
1726 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
1730 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
1732 if (soname_indx
== (bfd_size_type
) -1
1733 || ! elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
1739 if (! elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
1741 info
->flags
|= DF_SYMBOLIC
;
1748 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
1750 if (info
->new_dtags
)
1751 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
1752 if (indx
== (bfd_size_type
) -1
1753 || ! elf_add_dynamic_entry (info
, DT_RPATH
, indx
)
1755 && ! elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
)))
1759 if (filter_shlib
!= NULL
)
1763 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
1764 filter_shlib
, TRUE
);
1765 if (indx
== (bfd_size_type
) -1
1766 || ! elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
1770 if (auxiliary_filters
!= NULL
)
1772 const char * const *p
;
1774 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
1778 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
1780 if (indx
== (bfd_size_type
) -1
1781 || ! elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
1787 eif
.verdefs
= verdefs
;
1790 /* If we are supposed to export all symbols into the dynamic symbol
1791 table (this is not the normal case), then do so. */
1792 if (info
->export_dynamic
)
1794 elf_link_hash_traverse (elf_hash_table (info
),
1795 _bfd_elf_export_symbol
,
1801 /* Make all global versions with definition. */
1802 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
1803 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
1804 if (!d
->symver
&& d
->symbol
)
1806 const char *verstr
, *name
;
1807 size_t namelen
, verlen
, newlen
;
1809 struct elf_link_hash_entry
*newh
;
1812 namelen
= strlen (name
);
1814 verlen
= strlen (verstr
);
1815 newlen
= namelen
+ verlen
+ 3;
1817 newname
= bfd_malloc (newlen
);
1818 if (newname
== NULL
)
1820 memcpy (newname
, name
, namelen
);
1822 /* Check the hidden versioned definition. */
1823 p
= newname
+ namelen
;
1825 memcpy (p
, verstr
, verlen
+ 1);
1826 newh
= elf_link_hash_lookup (elf_hash_table (info
),
1827 newname
, FALSE
, FALSE
,
1830 || (newh
->root
.type
!= bfd_link_hash_defined
1831 && newh
->root
.type
!= bfd_link_hash_defweak
))
1833 /* Check the default versioned definition. */
1835 memcpy (p
, verstr
, verlen
+ 1);
1836 newh
= elf_link_hash_lookup (elf_hash_table (info
),
1837 newname
, FALSE
, FALSE
,
1842 /* Mark this version if there is a definition and it is
1843 not defined in a shared object. */
1845 && ((newh
->elf_link_hash_flags
1846 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)
1847 && (newh
->root
.type
== bfd_link_hash_defined
1848 || newh
->root
.type
== bfd_link_hash_defweak
))
1852 /* Attach all the symbols to their version information. */
1853 asvinfo
.output_bfd
= output_bfd
;
1854 asvinfo
.info
= info
;
1855 asvinfo
.verdefs
= verdefs
;
1856 asvinfo
.failed
= FALSE
;
1858 elf_link_hash_traverse (elf_hash_table (info
),
1859 _bfd_elf_link_assign_sym_version
,
1864 if (!info
->allow_undefined_version
)
1866 /* Check if all global versions have a definition. */
1868 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
1869 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
1870 if (!d
->symver
&& !d
->script
)
1872 (*_bfd_error_handler
)
1873 (_("%s: undefined version: %s"),
1874 d
->pattern
, t
->name
);
1875 all_defined
= FALSE
;
1880 bfd_set_error (bfd_error_bad_value
);
1885 /* Find all symbols which were defined in a dynamic object and make
1886 the backend pick a reasonable value for them. */
1887 elf_link_hash_traverse (elf_hash_table (info
),
1888 _bfd_elf_adjust_dynamic_symbol
,
1893 /* Add some entries to the .dynamic section. We fill in some of the
1894 values later, in elf_bfd_final_link, but we must add the entries
1895 now so that we know the final size of the .dynamic section. */
1897 /* If there are initialization and/or finalization functions to
1898 call then add the corresponding DT_INIT/DT_FINI entries. */
1899 h
= (info
->init_function
1900 ? elf_link_hash_lookup (elf_hash_table (info
),
1901 info
->init_function
, FALSE
,
1905 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
1906 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
1908 if (! elf_add_dynamic_entry (info
, DT_INIT
, 0))
1911 h
= (info
->fini_function
1912 ? elf_link_hash_lookup (elf_hash_table (info
),
1913 info
->fini_function
, FALSE
,
1917 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
1918 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
1920 if (! elf_add_dynamic_entry (info
, DT_FINI
, 0))
1924 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
1926 /* DT_PREINIT_ARRAY is not allowed in shared library. */
1927 if (! info
->executable
)
1932 for (sub
= info
->input_bfds
; sub
!= NULL
;
1933 sub
= sub
->link_next
)
1934 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
1935 if (elf_section_data (o
)->this_hdr
.sh_type
1936 == SHT_PREINIT_ARRAY
)
1938 (*_bfd_error_handler
)
1939 (_("%s: .preinit_array section is not allowed in DSO"),
1940 bfd_archive_filename (sub
));
1944 bfd_set_error (bfd_error_nonrepresentable_section
);
1948 if (!elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
1949 || !elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
1952 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
1954 if (!elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
1955 || !elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
1958 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
1960 if (!elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
1961 || !elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
1965 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
1966 /* If .dynstr is excluded from the link, we don't want any of
1967 these tags. Strictly, we should be checking each section
1968 individually; This quick check covers for the case where
1969 someone does a /DISCARD/ : { *(*) }. */
1970 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
1972 bfd_size_type strsize
;
1974 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
1975 if (! elf_add_dynamic_entry (info
, DT_HASH
, 0)
1976 || ! elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
1977 || ! elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
1978 || ! elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
1979 || ! elf_add_dynamic_entry (info
, DT_SYMENT
,
1980 sizeof (Elf_External_Sym
)))
1985 /* The backend must work out the sizes of all the other dynamic
1987 if (bed
->elf_backend_size_dynamic_sections
1988 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
1991 if (elf_hash_table (info
)->dynamic_sections_created
)
1993 bfd_size_type dynsymcount
;
1995 size_t bucketcount
= 0;
1996 size_t hash_entry_size
;
1997 unsigned int dtagcount
;
1999 /* Set up the version definition section. */
2000 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2001 BFD_ASSERT (s
!= NULL
);
2003 /* We may have created additional version definitions if we are
2004 just linking a regular application. */
2005 verdefs
= asvinfo
.verdefs
;
2007 /* Skip anonymous version tag. */
2008 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
2009 verdefs
= verdefs
->next
;
2011 if (verdefs
== NULL
)
2012 _bfd_strip_section_from_output (info
, s
);
2017 struct bfd_elf_version_tree
*t
;
2019 Elf_Internal_Verdef def
;
2020 Elf_Internal_Verdaux defaux
;
2025 /* Make space for the base version. */
2026 size
+= sizeof (Elf_External_Verdef
);
2027 size
+= sizeof (Elf_External_Verdaux
);
2030 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2032 struct bfd_elf_version_deps
*n
;
2034 size
+= sizeof (Elf_External_Verdef
);
2035 size
+= sizeof (Elf_External_Verdaux
);
2038 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2039 size
+= sizeof (Elf_External_Verdaux
);
2042 s
->_raw_size
= size
;
2043 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
2044 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2047 /* Fill in the version definition section. */
2051 def
.vd_version
= VER_DEF_CURRENT
;
2052 def
.vd_flags
= VER_FLG_BASE
;
2055 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2056 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2057 + sizeof (Elf_External_Verdaux
));
2059 if (soname_indx
!= (bfd_size_type
) -1)
2061 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
2063 def
.vd_hash
= bfd_elf_hash (soname
);
2064 defaux
.vda_name
= soname_indx
;
2071 name
= basename (output_bfd
->filename
);
2072 def
.vd_hash
= bfd_elf_hash (name
);
2073 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2075 if (indx
== (bfd_size_type
) -1)
2077 defaux
.vda_name
= indx
;
2079 defaux
.vda_next
= 0;
2081 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2082 (Elf_External_Verdef
*) p
);
2083 p
+= sizeof (Elf_External_Verdef
);
2084 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2085 (Elf_External_Verdaux
*) p
);
2086 p
+= sizeof (Elf_External_Verdaux
);
2088 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2091 struct bfd_elf_version_deps
*n
;
2092 struct elf_link_hash_entry
*h
;
2093 struct bfd_link_hash_entry
*bh
;
2096 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2099 /* Add a symbol representing this version. */
2101 if (! (_bfd_generic_link_add_one_symbol
2102 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
2104 get_elf_backend_data (dynobj
)->collect
, &bh
)))
2106 h
= (struct elf_link_hash_entry
*) bh
;
2107 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
2108 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2109 h
->type
= STT_OBJECT
;
2110 h
->verinfo
.vertree
= t
;
2112 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2115 def
.vd_version
= VER_DEF_CURRENT
;
2117 if (t
->globals
.list
== NULL
&& t
->locals
.list
== NULL
&& ! t
->used
)
2118 def
.vd_flags
|= VER_FLG_WEAK
;
2119 def
.vd_ndx
= t
->vernum
+ 1;
2120 def
.vd_cnt
= cdeps
+ 1;
2121 def
.vd_hash
= bfd_elf_hash (t
->name
);
2122 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2123 if (t
->next
!= NULL
)
2124 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2125 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
2129 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2130 (Elf_External_Verdef
*) p
);
2131 p
+= sizeof (Elf_External_Verdef
);
2133 defaux
.vda_name
= h
->dynstr_index
;
2134 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
2136 if (t
->deps
== NULL
)
2137 defaux
.vda_next
= 0;
2139 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2140 t
->name_indx
= defaux
.vda_name
;
2142 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2143 (Elf_External_Verdaux
*) p
);
2144 p
+= sizeof (Elf_External_Verdaux
);
2146 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2148 if (n
->version_needed
== NULL
)
2150 /* This can happen if there was an error in the
2152 defaux
.vda_name
= 0;
2156 defaux
.vda_name
= n
->version_needed
->name_indx
;
2157 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
2160 if (n
->next
== NULL
)
2161 defaux
.vda_next
= 0;
2163 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2165 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2166 (Elf_External_Verdaux
*) p
);
2167 p
+= sizeof (Elf_External_Verdaux
);
2171 if (! elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
2172 || ! elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
2175 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
2178 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
2180 if (! elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
2183 else if (info
->flags
& DF_BIND_NOW
)
2185 if (! elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
2191 if (info
->executable
)
2192 info
->flags_1
&= ~ (DF_1_INITFIRST
2195 if (! elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
2199 /* Work out the size of the version reference section. */
2201 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2202 BFD_ASSERT (s
!= NULL
);
2204 struct elf_find_verdep_info sinfo
;
2206 sinfo
.output_bfd
= output_bfd
;
2208 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
2209 if (sinfo
.vers
== 0)
2211 sinfo
.failed
= FALSE
;
2213 elf_link_hash_traverse (elf_hash_table (info
),
2214 _bfd_elf_link_find_version_dependencies
,
2217 if (elf_tdata (output_bfd
)->verref
== NULL
)
2218 _bfd_strip_section_from_output (info
, s
);
2221 Elf_Internal_Verneed
*t
;
2226 /* Build the version definition section. */
2229 for (t
= elf_tdata (output_bfd
)->verref
;
2233 Elf_Internal_Vernaux
*a
;
2235 size
+= sizeof (Elf_External_Verneed
);
2237 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2238 size
+= sizeof (Elf_External_Vernaux
);
2241 s
->_raw_size
= size
;
2242 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
2243 if (s
->contents
== NULL
)
2247 for (t
= elf_tdata (output_bfd
)->verref
;
2252 Elf_Internal_Vernaux
*a
;
2256 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2259 t
->vn_version
= VER_NEED_CURRENT
;
2261 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2262 elf_dt_name (t
->vn_bfd
) != NULL
2263 ? elf_dt_name (t
->vn_bfd
)
2264 : basename (t
->vn_bfd
->filename
),
2266 if (indx
== (bfd_size_type
) -1)
2269 t
->vn_aux
= sizeof (Elf_External_Verneed
);
2270 if (t
->vn_nextref
== NULL
)
2273 t
->vn_next
= (sizeof (Elf_External_Verneed
)
2274 + caux
* sizeof (Elf_External_Vernaux
));
2276 _bfd_elf_swap_verneed_out (output_bfd
, t
,
2277 (Elf_External_Verneed
*) p
);
2278 p
+= sizeof (Elf_External_Verneed
);
2280 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2282 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
2283 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2284 a
->vna_nodename
, FALSE
);
2285 if (indx
== (bfd_size_type
) -1)
2288 if (a
->vna_nextptr
== NULL
)
2291 a
->vna_next
= sizeof (Elf_External_Vernaux
);
2293 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
2294 (Elf_External_Vernaux
*) p
);
2295 p
+= sizeof (Elf_External_Vernaux
);
2299 if (! elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
2300 || ! elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
2303 elf_tdata (output_bfd
)->cverrefs
= crefs
;
2307 /* Assign dynsym indicies. In a shared library we generate a
2308 section symbol for each output section, which come first.
2309 Next come all of the back-end allocated local dynamic syms,
2310 followed by the rest of the global symbols. */
2312 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
2314 /* Work out the size of the symbol version section. */
2315 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
2316 BFD_ASSERT (s
!= NULL
);
2317 if (dynsymcount
== 0
2318 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
2320 _bfd_strip_section_from_output (info
, s
);
2321 /* The DYNSYMCOUNT might have changed if we were going to
2322 output a dynamic symbol table entry for S. */
2323 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
2327 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
2328 s
->contents
= bfd_zalloc (output_bfd
, s
->_raw_size
);
2329 if (s
->contents
== NULL
)
2332 if (! elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
2336 /* Set the size of the .dynsym and .hash sections. We counted
2337 the number of dynamic symbols in elf_link_add_object_symbols.
2338 We will build the contents of .dynsym and .hash when we build
2339 the final symbol table, because until then we do not know the
2340 correct value to give the symbols. We built the .dynstr
2341 section as we went along in elf_link_add_object_symbols. */
2342 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
2343 BFD_ASSERT (s
!= NULL
);
2344 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
2345 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
2346 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2349 if (dynsymcount
!= 0)
2351 Elf_Internal_Sym isym
;
2353 /* The first entry in .dynsym is a dummy symbol. */
2360 elf_swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
2363 /* Compute the size of the hashing table. As a side effect this
2364 computes the hash values for all the names we export. */
2365 bucketcount
= compute_bucket_count (info
);
2367 s
= bfd_get_section_by_name (dynobj
, ".hash");
2368 BFD_ASSERT (s
!= NULL
);
2369 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
2370 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
2371 s
->contents
= bfd_zalloc (output_bfd
, s
->_raw_size
);
2372 if (s
->contents
== NULL
)
2375 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
2376 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
2377 s
->contents
+ hash_entry_size
);
2379 elf_hash_table (info
)->bucketcount
= bucketcount
;
2381 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
2382 BFD_ASSERT (s
!= NULL
);
2384 elf_finalize_dynstr (output_bfd
, info
);
2386 s
->_raw_size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
2388 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
2389 if (! elf_add_dynamic_entry (info
, DT_NULL
, 0))
2396 /* This function is used to adjust offsets into .dynstr for
2397 dynamic symbols. This is called via elf_link_hash_traverse. */
2400 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2402 struct elf_strtab_hash
*dynstr
= data
;
2404 if (h
->root
.type
== bfd_link_hash_warning
)
2405 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2407 if (h
->dynindx
!= -1)
2408 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2412 /* Assign string offsets in .dynstr, update all structures referencing
2416 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2418 struct elf_link_local_dynamic_entry
*entry
;
2419 struct elf_strtab_hash
*dynstr
= elf_hash_table (info
)->dynstr
;
2420 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
2423 Elf_External_Dyn
*dyncon
, *dynconend
;
2425 _bfd_elf_strtab_finalize (dynstr
);
2426 size
= _bfd_elf_strtab_size (dynstr
);
2428 /* Update all .dynamic entries referencing .dynstr strings. */
2429 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2430 BFD_ASSERT (sdyn
!= NULL
);
2432 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
2433 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
2435 for (; dyncon
< dynconend
; dyncon
++)
2437 Elf_Internal_Dyn dyn
;
2439 elf_swap_dyn_in (dynobj
, dyncon
, & dyn
);
2443 dyn
.d_un
.d_val
= size
;
2444 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
2452 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2453 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
2460 /* Now update local dynamic symbols. */
2461 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
2462 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2463 entry
->isym
.st_name
);
2465 /* And the rest of dynamic symbols. */
2466 elf_link_hash_traverse (elf_hash_table (info
),
2467 elf_adjust_dynstr_offsets
, dynstr
);
2469 /* Adjust version definitions. */
2470 if (elf_tdata (output_bfd
)->cverdefs
)
2475 Elf_Internal_Verdef def
;
2476 Elf_Internal_Verdaux defaux
;
2478 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2479 p
= (bfd_byte
*) s
->contents
;
2482 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
2484 p
+= sizeof (Elf_External_Verdef
);
2485 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2487 _bfd_elf_swap_verdaux_in (output_bfd
,
2488 (Elf_External_Verdaux
*) p
, &defaux
);
2489 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2491 _bfd_elf_swap_verdaux_out (output_bfd
,
2492 &defaux
, (Elf_External_Verdaux
*) p
);
2493 p
+= sizeof (Elf_External_Verdaux
);
2496 while (def
.vd_next
);
2499 /* Adjust version references. */
2500 if (elf_tdata (output_bfd
)->verref
)
2505 Elf_Internal_Verneed need
;
2506 Elf_Internal_Vernaux needaux
;
2508 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2509 p
= (bfd_byte
*) s
->contents
;
2512 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
2514 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
2515 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
2516 (Elf_External_Verneed
*) p
);
2517 p
+= sizeof (Elf_External_Verneed
);
2518 for (i
= 0; i
< need
.vn_cnt
; ++i
)
2520 _bfd_elf_swap_vernaux_in (output_bfd
,
2521 (Elf_External_Vernaux
*) p
, &needaux
);
2522 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
2524 _bfd_elf_swap_vernaux_out (output_bfd
,
2526 (Elf_External_Vernaux
*) p
);
2527 p
+= sizeof (Elf_External_Vernaux
);
2530 while (need
.vn_next
);
2536 /* Final phase of ELF linker. */
2538 /* A structure we use to avoid passing large numbers of arguments. */
2540 struct elf_final_link_info
2542 /* General link information. */
2543 struct bfd_link_info
*info
;
2546 /* Symbol string table. */
2547 struct bfd_strtab_hash
*symstrtab
;
2548 /* .dynsym section. */
2549 asection
*dynsym_sec
;
2550 /* .hash section. */
2552 /* symbol version section (.gnu.version). */
2553 asection
*symver_sec
;
2554 /* Buffer large enough to hold contents of any section. */
2556 /* Buffer large enough to hold external relocs of any section. */
2557 void *external_relocs
;
2558 /* Buffer large enough to hold internal relocs of any section. */
2559 Elf_Internal_Rela
*internal_relocs
;
2560 /* Buffer large enough to hold external local symbols of any input
2562 Elf_External_Sym
*external_syms
;
2563 /* And a buffer for symbol section indices. */
2564 Elf_External_Sym_Shndx
*locsym_shndx
;
2565 /* Buffer large enough to hold internal local symbols of any input
2567 Elf_Internal_Sym
*internal_syms
;
2568 /* Array large enough to hold a symbol index for each local symbol
2569 of any input BFD. */
2571 /* Array large enough to hold a section pointer for each local
2572 symbol of any input BFD. */
2573 asection
**sections
;
2574 /* Buffer to hold swapped out symbols. */
2575 Elf_External_Sym
*symbuf
;
2576 /* And one for symbol section indices. */
2577 Elf_External_Sym_Shndx
*symshndxbuf
;
2578 /* Number of swapped out symbols in buffer. */
2579 size_t symbuf_count
;
2580 /* Number of symbols which fit in symbuf. */
2582 /* And same for symshndxbuf. */
2583 size_t shndxbuf_size
;
2586 static bfd_boolean elf_link_output_sym
2587 (struct elf_final_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
2588 struct elf_link_hash_entry
*);
2589 static bfd_boolean elf_link_flush_output_syms
2590 (struct elf_final_link_info
*);
2591 static bfd_boolean elf_link_output_extsym
2592 (struct elf_link_hash_entry
*, void *);
2593 static bfd_boolean elf_link_input_bfd
2594 (struct elf_final_link_info
*, bfd
*);
2595 static bfd_boolean elf_reloc_link_order
2596 (bfd
*, struct bfd_link_info
*, asection
*, struct bfd_link_order
*);
2598 /* This struct is used to pass information to elf_link_output_extsym. */
2600 struct elf_outext_info
2603 bfd_boolean localsyms
;
2604 struct elf_final_link_info
*finfo
;
2607 /* When performing a relocatable link, the input relocations are
2608 preserved. But, if they reference global symbols, the indices
2609 referenced must be updated. Update all the relocations in
2610 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
2613 elf_link_adjust_relocs (bfd
*abfd
,
2614 Elf_Internal_Shdr
*rel_hdr
,
2616 struct elf_link_hash_entry
**rel_hash
)
2619 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2621 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2622 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2624 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2626 swap_in
= bed
->s
->swap_reloc_in
;
2627 swap_out
= bed
->s
->swap_reloc_out
;
2629 else if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
))
2631 swap_in
= bed
->s
->swap_reloca_in
;
2632 swap_out
= bed
->s
->swap_reloca_out
;
2637 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
2640 erela
= rel_hdr
->contents
;
2641 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
2643 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
2646 if (*rel_hash
== NULL
)
2649 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
2651 (*swap_in
) (abfd
, erela
, irela
);
2652 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
2653 irela
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
2654 ELF_R_TYPE (irela
[j
].r_info
));
2655 (*swap_out
) (abfd
, irela
, erela
);
2659 struct elf_link_sort_rela
2662 enum elf_reloc_type_class type
;
2663 /* We use this as an array of size int_rels_per_ext_rel. */
2664 Elf_Internal_Rela rela
[1];
2668 elf_link_sort_cmp1 (const void *A
, const void *B
)
2670 const struct elf_link_sort_rela
*a
= A
;
2671 const struct elf_link_sort_rela
*b
= B
;
2672 int relativea
, relativeb
;
2674 relativea
= a
->type
== reloc_class_relative
;
2675 relativeb
= b
->type
== reloc_class_relative
;
2677 if (relativea
< relativeb
)
2679 if (relativea
> relativeb
)
2681 if (ELF_R_SYM (a
->rela
->r_info
) < ELF_R_SYM (b
->rela
->r_info
))
2683 if (ELF_R_SYM (a
->rela
->r_info
) > ELF_R_SYM (b
->rela
->r_info
))
2685 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
2687 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
2693 elf_link_sort_cmp2 (const void *A
, const void *B
)
2695 const struct elf_link_sort_rela
*a
= A
;
2696 const struct elf_link_sort_rela
*b
= B
;
2699 if (a
->offset
< b
->offset
)
2701 if (a
->offset
> b
->offset
)
2703 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
2704 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
2709 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
2711 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
2717 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
2720 bfd_size_type count
, size
;
2721 size_t i
, ret
, sort_elt
, ext_size
;
2722 bfd_byte
*sort
, *s_non_relative
, *p
;
2723 struct elf_link_sort_rela
*sq
;
2724 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2725 int i2e
= bed
->s
->int_rels_per_ext_rel
;
2726 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2727 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2728 struct bfd_link_order
*lo
;
2730 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
2731 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
2733 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
2734 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
2736 ext_size
= sizeof (Elf_External_Rel
);
2737 swap_in
= bed
->s
->swap_reloc_in
;
2738 swap_out
= bed
->s
->swap_reloc_out
;
2742 ext_size
= sizeof (Elf_External_Rela
);
2743 swap_in
= bed
->s
->swap_reloca_in
;
2744 swap_out
= bed
->s
->swap_reloca_out
;
2746 count
= reldyn
->_raw_size
/ ext_size
;
2749 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
2750 if (lo
->type
== bfd_indirect_link_order
)
2752 asection
*o
= lo
->u
.indirect
.section
;
2753 size
+= o
->_raw_size
;
2756 if (size
!= reldyn
->_raw_size
)
2759 sort_elt
= (sizeof (struct elf_link_sort_rela
)
2760 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
2761 sort
= bfd_zmalloc (sort_elt
* count
);
2764 (*info
->callbacks
->warning
)
2765 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
2769 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
2770 if (lo
->type
== bfd_indirect_link_order
)
2772 bfd_byte
*erel
, *erelend
;
2773 asection
*o
= lo
->u
.indirect
.section
;
2776 erelend
= o
->contents
+ o
->_raw_size
;
2777 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
2778 while (erel
< erelend
)
2780 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
2781 (*swap_in
) (abfd
, erel
, s
->rela
);
2782 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
2788 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
2790 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
2792 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
2793 if (s
->type
!= reloc_class_relative
)
2799 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
2800 for (; i
< count
; i
++, p
+= sort_elt
)
2802 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
2803 if (ELF_R_SYM (sp
->rela
->r_info
) != ELF_R_SYM (sq
->rela
->r_info
))
2805 sp
->offset
= sq
->rela
->r_offset
;
2808 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
2810 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
2811 if (lo
->type
== bfd_indirect_link_order
)
2813 bfd_byte
*erel
, *erelend
;
2814 asection
*o
= lo
->u
.indirect
.section
;
2817 erelend
= o
->contents
+ o
->_raw_size
;
2818 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
2819 while (erel
< erelend
)
2821 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
2822 (*swap_out
) (abfd
, s
->rela
, erel
);
2833 /* Do the final step of an ELF link. */
2836 elf_bfd_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
2838 bfd_boolean dynamic
;
2839 bfd_boolean emit_relocs
;
2841 struct elf_final_link_info finfo
;
2842 register asection
*o
;
2843 register struct bfd_link_order
*p
;
2845 bfd_size_type max_contents_size
;
2846 bfd_size_type max_external_reloc_size
;
2847 bfd_size_type max_internal_reloc_count
;
2848 bfd_size_type max_sym_count
;
2849 bfd_size_type max_sym_shndx_count
;
2851 Elf_Internal_Sym elfsym
;
2853 Elf_Internal_Shdr
*symtab_hdr
;
2854 Elf_Internal_Shdr
*symtab_shndx_hdr
;
2855 Elf_Internal_Shdr
*symstrtab_hdr
;
2856 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2857 struct elf_outext_info eoinfo
;
2859 size_t relativecount
= 0;
2860 asection
*reldyn
= 0;
2863 if (! is_elf_hash_table (info
->hash
))
2867 abfd
->flags
|= DYNAMIC
;
2869 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
2870 dynobj
= elf_hash_table (info
)->dynobj
;
2872 emit_relocs
= (info
->relocatable
2873 || info
->emitrelocations
2874 || bed
->elf_backend_emit_relocs
);
2877 finfo
.output_bfd
= abfd
;
2878 finfo
.symstrtab
= elf_stringtab_init ();
2879 if (finfo
.symstrtab
== NULL
)
2884 finfo
.dynsym_sec
= NULL
;
2885 finfo
.hash_sec
= NULL
;
2886 finfo
.symver_sec
= NULL
;
2890 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
2891 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
2892 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
2893 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
2894 /* Note that it is OK if symver_sec is NULL. */
2897 finfo
.contents
= NULL
;
2898 finfo
.external_relocs
= NULL
;
2899 finfo
.internal_relocs
= NULL
;
2900 finfo
.external_syms
= NULL
;
2901 finfo
.locsym_shndx
= NULL
;
2902 finfo
.internal_syms
= NULL
;
2903 finfo
.indices
= NULL
;
2904 finfo
.sections
= NULL
;
2905 finfo
.symbuf
= NULL
;
2906 finfo
.symshndxbuf
= NULL
;
2907 finfo
.symbuf_count
= 0;
2908 finfo
.shndxbuf_size
= 0;
2910 /* Count up the number of relocations we will output for each output
2911 section, so that we know the sizes of the reloc sections. We
2912 also figure out some maximum sizes. */
2913 max_contents_size
= 0;
2914 max_external_reloc_size
= 0;
2915 max_internal_reloc_count
= 0;
2917 max_sym_shndx_count
= 0;
2919 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2921 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2924 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
2926 unsigned int reloc_count
= 0;
2927 struct bfd_elf_section_data
*esdi
= NULL
;
2928 unsigned int *rel_count1
;
2930 if (p
->type
== bfd_section_reloc_link_order
2931 || p
->type
== bfd_symbol_reloc_link_order
)
2933 else if (p
->type
== bfd_indirect_link_order
)
2937 sec
= p
->u
.indirect
.section
;
2938 esdi
= elf_section_data (sec
);
2940 /* Mark all sections which are to be included in the
2941 link. This will normally be every section. We need
2942 to do this so that we can identify any sections which
2943 the linker has decided to not include. */
2944 sec
->linker_mark
= TRUE
;
2946 if (sec
->flags
& SEC_MERGE
)
2949 if (info
->relocatable
|| info
->emitrelocations
)
2950 reloc_count
= sec
->reloc_count
;
2951 else if (bed
->elf_backend_count_relocs
)
2953 Elf_Internal_Rela
* relocs
;
2955 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
2958 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
2960 if (elf_section_data (o
)->relocs
!= relocs
)
2964 if (sec
->_raw_size
> max_contents_size
)
2965 max_contents_size
= sec
->_raw_size
;
2966 if (sec
->_cooked_size
> max_contents_size
)
2967 max_contents_size
= sec
->_cooked_size
;
2969 /* We are interested in just local symbols, not all
2971 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
2972 && (sec
->owner
->flags
& DYNAMIC
) == 0)
2976 if (elf_bad_symtab (sec
->owner
))
2977 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
2978 / sizeof (Elf_External_Sym
));
2980 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
2982 if (sym_count
> max_sym_count
)
2983 max_sym_count
= sym_count
;
2985 if (sym_count
> max_sym_shndx_count
2986 && elf_symtab_shndx (sec
->owner
) != 0)
2987 max_sym_shndx_count
= sym_count
;
2989 if ((sec
->flags
& SEC_RELOC
) != 0)
2993 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
2994 if (ext_size
> max_external_reloc_size
)
2995 max_external_reloc_size
= ext_size
;
2996 if (sec
->reloc_count
> max_internal_reloc_count
)
2997 max_internal_reloc_count
= sec
->reloc_count
;
3002 if (reloc_count
== 0)
3005 o
->reloc_count
+= reloc_count
;
3007 /* MIPS may have a mix of REL and RELA relocs on sections.
3008 To support this curious ABI we keep reloc counts in
3009 elf_section_data too. We must be careful to add the
3010 relocations from the input section to the right output
3011 count. FIXME: Get rid of one count. We have
3012 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
3013 rel_count1
= &esdo
->rel_count
;
3016 bfd_boolean same_size
;
3017 bfd_size_type entsize1
;
3019 entsize1
= esdi
->rel_hdr
.sh_entsize
;
3020 BFD_ASSERT (entsize1
== sizeof (Elf_External_Rel
)
3021 || entsize1
== sizeof (Elf_External_Rela
));
3022 same_size
= (!o
->use_rela_p
3023 == (entsize1
== sizeof (Elf_External_Rel
)));
3026 rel_count1
= &esdo
->rel_count2
;
3028 if (esdi
->rel_hdr2
!= NULL
)
3030 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
3031 unsigned int alt_count
;
3032 unsigned int *rel_count2
;
3034 BFD_ASSERT (entsize2
!= entsize1
3035 && (entsize2
== sizeof (Elf_External_Rel
)
3036 || entsize2
== sizeof (Elf_External_Rela
)));
3038 rel_count2
= &esdo
->rel_count2
;
3040 rel_count2
= &esdo
->rel_count
;
3042 /* The following is probably too simplistic if the
3043 backend counts output relocs unusually. */
3044 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
3045 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
3046 *rel_count2
+= alt_count
;
3047 reloc_count
-= alt_count
;
3050 *rel_count1
+= reloc_count
;
3053 if (o
->reloc_count
> 0)
3054 o
->flags
|= SEC_RELOC
;
3057 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3058 set it (this is probably a bug) and if it is set
3059 assign_section_numbers will create a reloc section. */
3060 o
->flags
&=~ SEC_RELOC
;
3063 /* If the SEC_ALLOC flag is not set, force the section VMA to
3064 zero. This is done in elf_fake_sections as well, but forcing
3065 the VMA to 0 here will ensure that relocs against these
3066 sections are handled correctly. */
3067 if ((o
->flags
& SEC_ALLOC
) == 0
3068 && ! o
->user_set_vma
)
3072 if (! info
->relocatable
&& merged
)
3073 elf_link_hash_traverse (elf_hash_table (info
),
3074 _bfd_elf_link_sec_merge_syms
, abfd
);
3076 /* Figure out the file positions for everything but the symbol table
3077 and the relocs. We set symcount to force assign_section_numbers
3078 to create a symbol table. */
3079 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
3080 BFD_ASSERT (! abfd
->output_has_begun
);
3081 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
3084 /* That created the reloc sections. Set their sizes, and assign
3085 them file positions, and allocate some buffers. */
3086 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3088 if ((o
->flags
& SEC_RELOC
) != 0)
3090 if (!(_bfd_elf_link_size_reloc_section
3091 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
3094 if (elf_section_data (o
)->rel_hdr2
3095 && !(_bfd_elf_link_size_reloc_section
3096 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
3100 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
3101 to count upwards while actually outputting the relocations. */
3102 elf_section_data (o
)->rel_count
= 0;
3103 elf_section_data (o
)->rel_count2
= 0;
3106 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3108 /* We have now assigned file positions for all the sections except
3109 .symtab and .strtab. We start the .symtab section at the current
3110 file position, and write directly to it. We build the .strtab
3111 section in memory. */
3112 bfd_get_symcount (abfd
) = 0;
3113 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3114 /* sh_name is set in prep_headers. */
3115 symtab_hdr
->sh_type
= SHT_SYMTAB
;
3116 /* sh_flags, sh_addr and sh_size all start off zero. */
3117 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
3118 /* sh_link is set in assign_section_numbers. */
3119 /* sh_info is set below. */
3120 /* sh_offset is set just below. */
3121 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
3123 off
= elf_tdata (abfd
)->next_file_pos
;
3124 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
3126 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3127 incorrect. We do not yet know the size of the .symtab section.
3128 We correct next_file_pos below, after we do know the size. */
3130 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3131 continuously seeking to the right position in the file. */
3132 if (! info
->keep_memory
|| max_sym_count
< 20)
3133 finfo
.symbuf_size
= 20;
3135 finfo
.symbuf_size
= max_sym_count
;
3136 amt
= finfo
.symbuf_size
;
3137 amt
*= sizeof (Elf_External_Sym
);
3138 finfo
.symbuf
= bfd_malloc (amt
);
3139 if (finfo
.symbuf
== NULL
)
3141 if (elf_numsections (abfd
) > SHN_LORESERVE
)
3143 /* Wild guess at number of output symbols. realloc'd as needed. */
3144 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
3145 finfo
.shndxbuf_size
= amt
;
3146 amt
*= sizeof (Elf_External_Sym_Shndx
);
3147 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
3148 if (finfo
.symshndxbuf
== NULL
)
3152 /* Start writing out the symbol table. The first symbol is always a
3154 if (info
->strip
!= strip_all
3157 elfsym
.st_value
= 0;
3160 elfsym
.st_other
= 0;
3161 elfsym
.st_shndx
= SHN_UNDEF
;
3162 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
3168 /* Some standard ELF linkers do this, but we don't because it causes
3169 bootstrap comparison failures. */
3170 /* Output a file symbol for the output file as the second symbol.
3171 We output this even if we are discarding local symbols, although
3172 I'm not sure if this is correct. */
3173 elfsym
.st_value
= 0;
3175 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
3176 elfsym
.st_other
= 0;
3177 elfsym
.st_shndx
= SHN_ABS
;
3178 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
3179 &elfsym
, bfd_abs_section_ptr
, NULL
))
3183 /* Output a symbol for each section. We output these even if we are
3184 discarding local symbols, since they are used for relocs. These
3185 symbols have no names. We store the index of each one in the
3186 index field of the section, so that we can find it again when
3187 outputting relocs. */
3188 if (info
->strip
!= strip_all
3192 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
3193 elfsym
.st_other
= 0;
3194 for (i
= 1; i
< elf_numsections (abfd
); i
++)
3196 o
= section_from_elf_index (abfd
, i
);
3198 o
->target_index
= bfd_get_symcount (abfd
);
3199 elfsym
.st_shndx
= i
;
3200 if (info
->relocatable
|| o
== NULL
)
3201 elfsym
.st_value
= 0;
3203 elfsym
.st_value
= o
->vma
;
3204 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
3206 if (i
== SHN_LORESERVE
- 1)
3207 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3211 /* Allocate some memory to hold information read in from the input
3213 if (max_contents_size
!= 0)
3215 finfo
.contents
= bfd_malloc (max_contents_size
);
3216 if (finfo
.contents
== NULL
)
3220 if (max_external_reloc_size
!= 0)
3222 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
3223 if (finfo
.external_relocs
== NULL
)
3227 if (max_internal_reloc_count
!= 0)
3229 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
3230 amt
*= sizeof (Elf_Internal_Rela
);
3231 finfo
.internal_relocs
= bfd_malloc (amt
);
3232 if (finfo
.internal_relocs
== NULL
)
3236 if (max_sym_count
!= 0)
3238 amt
= max_sym_count
* sizeof (Elf_External_Sym
);
3239 finfo
.external_syms
= bfd_malloc (amt
);
3240 if (finfo
.external_syms
== NULL
)
3243 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
3244 finfo
.internal_syms
= bfd_malloc (amt
);
3245 if (finfo
.internal_syms
== NULL
)
3248 amt
= max_sym_count
* sizeof (long);
3249 finfo
.indices
= bfd_malloc (amt
);
3250 if (finfo
.indices
== NULL
)
3253 amt
= max_sym_count
* sizeof (asection
*);
3254 finfo
.sections
= bfd_malloc (amt
);
3255 if (finfo
.sections
== NULL
)
3259 if (max_sym_shndx_count
!= 0)
3261 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
3262 finfo
.locsym_shndx
= bfd_malloc (amt
);
3263 if (finfo
.locsym_shndx
== NULL
)
3267 if (elf_hash_table (info
)->tls_sec
)
3269 bfd_vma base
, end
= 0;
3272 for (sec
= elf_hash_table (info
)->tls_sec
;
3273 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
3276 bfd_vma size
= sec
->_raw_size
;
3278 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
3280 struct bfd_link_order
*o
;
3282 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
3283 if (size
< o
->offset
+ o
->size
)
3284 size
= o
->offset
+ o
->size
;
3286 end
= sec
->vma
+ size
;
3288 base
= elf_hash_table (info
)->tls_sec
->vma
;
3289 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
3290 elf_hash_table (info
)->tls_size
= end
- base
;
3293 /* Since ELF permits relocations to be against local symbols, we
3294 must have the local symbols available when we do the relocations.
3295 Since we would rather only read the local symbols once, and we
3296 would rather not keep them in memory, we handle all the
3297 relocations for a single input file at the same time.
3299 Unfortunately, there is no way to know the total number of local
3300 symbols until we have seen all of them, and the local symbol
3301 indices precede the global symbol indices. This means that when
3302 we are generating relocatable output, and we see a reloc against
3303 a global symbol, we can not know the symbol index until we have
3304 finished examining all the local symbols to see which ones we are
3305 going to output. To deal with this, we keep the relocations in
3306 memory, and don't output them until the end of the link. This is
3307 an unfortunate waste of memory, but I don't see a good way around
3308 it. Fortunately, it only happens when performing a relocatable
3309 link, which is not the common case. FIXME: If keep_memory is set
3310 we could write the relocs out and then read them again; I don't
3311 know how bad the memory loss will be. */
3313 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3314 sub
->output_has_begun
= FALSE
;
3315 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3317 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3319 if (p
->type
== bfd_indirect_link_order
3320 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
3321 == bfd_target_elf_flavour
)
3322 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
3324 if (! sub
->output_has_begun
)
3326 if (! elf_link_input_bfd (&finfo
, sub
))
3328 sub
->output_has_begun
= TRUE
;
3331 else if (p
->type
== bfd_section_reloc_link_order
3332 || p
->type
== bfd_symbol_reloc_link_order
)
3334 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
3339 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
3345 /* Output any global symbols that got converted to local in a
3346 version script or due to symbol visibility. We do this in a
3347 separate step since ELF requires all local symbols to appear
3348 prior to any global symbols. FIXME: We should only do this if
3349 some global symbols were, in fact, converted to become local.
3350 FIXME: Will this work correctly with the Irix 5 linker? */
3351 eoinfo
.failed
= FALSE
;
3352 eoinfo
.finfo
= &finfo
;
3353 eoinfo
.localsyms
= TRUE
;
3354 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3359 /* That wrote out all the local symbols. Finish up the symbol table
3360 with the global symbols. Even if we want to strip everything we
3361 can, we still need to deal with those global symbols that got
3362 converted to local in a version script. */
3364 /* The sh_info field records the index of the first non local symbol. */
3365 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
3368 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
3370 Elf_Internal_Sym sym
;
3371 Elf_External_Sym
*dynsym
=
3372 (Elf_External_Sym
*) finfo
.dynsym_sec
->contents
;
3373 long last_local
= 0;
3375 /* Write out the section symbols for the output sections. */
3382 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
3385 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3388 Elf_External_Sym
*dest
;
3390 indx
= elf_section_data (s
)->this_idx
;
3391 BFD_ASSERT (indx
> 0);
3392 sym
.st_shndx
= indx
;
3393 sym
.st_value
= s
->vma
;
3394 dest
= dynsym
+ elf_section_data (s
)->dynindx
;
3395 elf_swap_symbol_out (abfd
, &sym
, dest
, 0);
3398 last_local
= bfd_count_sections (abfd
);
3401 /* Write out the local dynsyms. */
3402 if (elf_hash_table (info
)->dynlocal
)
3404 struct elf_link_local_dynamic_entry
*e
;
3405 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
3408 Elf_External_Sym
*dest
;
3410 sym
.st_size
= e
->isym
.st_size
;
3411 sym
.st_other
= e
->isym
.st_other
;
3413 /* Copy the internal symbol as is.
3414 Note that we saved a word of storage and overwrote
3415 the original st_name with the dynstr_index. */
3418 if (e
->isym
.st_shndx
!= SHN_UNDEF
3419 && (e
->isym
.st_shndx
< SHN_LORESERVE
3420 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
3422 s
= bfd_section_from_elf_index (e
->input_bfd
,
3426 elf_section_data (s
->output_section
)->this_idx
;
3427 sym
.st_value
= (s
->output_section
->vma
3429 + e
->isym
.st_value
);
3432 if (last_local
< e
->dynindx
)
3433 last_local
= e
->dynindx
;
3435 dest
= dynsym
+ e
->dynindx
;
3436 elf_swap_symbol_out (abfd
, &sym
, dest
, 0);
3440 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
3444 /* We get the global symbols from the hash table. */
3445 eoinfo
.failed
= FALSE
;
3446 eoinfo
.localsyms
= FALSE
;
3447 eoinfo
.finfo
= &finfo
;
3448 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3453 /* If backend needs to output some symbols not present in the hash
3454 table, do it now. */
3455 if (bed
->elf_backend_output_arch_syms
)
3457 typedef bfd_boolean (*out_sym_func
)
3458 (void *, const char *, Elf_Internal_Sym
*, asection
*,
3459 struct elf_link_hash_entry
*);
3461 if (! ((*bed
->elf_backend_output_arch_syms
)
3462 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
3466 /* Flush all symbols to the file. */
3467 if (! elf_link_flush_output_syms (&finfo
))
3470 /* Now we know the size of the symtab section. */
3471 off
+= symtab_hdr
->sh_size
;
3473 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3474 if (symtab_shndx_hdr
->sh_name
!= 0)
3476 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
3477 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
3478 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
3479 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
3480 symtab_shndx_hdr
->sh_size
= amt
;
3482 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
3485 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
3486 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
3491 /* Finish up and write out the symbol string table (.strtab)
3493 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3494 /* sh_name was set in prep_headers. */
3495 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
3496 symstrtab_hdr
->sh_flags
= 0;
3497 symstrtab_hdr
->sh_addr
= 0;
3498 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
3499 symstrtab_hdr
->sh_entsize
= 0;
3500 symstrtab_hdr
->sh_link
= 0;
3501 symstrtab_hdr
->sh_info
= 0;
3502 /* sh_offset is set just below. */
3503 symstrtab_hdr
->sh_addralign
= 1;
3505 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
3506 elf_tdata (abfd
)->next_file_pos
= off
;
3508 if (bfd_get_symcount (abfd
) > 0)
3510 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
3511 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
3515 /* Adjust the relocs to have the correct symbol indices. */
3516 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3518 if ((o
->flags
& SEC_RELOC
) == 0)
3521 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
3522 elf_section_data (o
)->rel_count
,
3523 elf_section_data (o
)->rel_hashes
);
3524 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
3525 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
3526 elf_section_data (o
)->rel_count2
,
3527 (elf_section_data (o
)->rel_hashes
3528 + elf_section_data (o
)->rel_count
));
3530 /* Set the reloc_count field to 0 to prevent write_relocs from
3531 trying to swap the relocs out itself. */
3535 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
3536 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
3538 /* If we are linking against a dynamic object, or generating a
3539 shared library, finish up the dynamic linking information. */
3542 Elf_External_Dyn
*dyncon
, *dynconend
;
3544 /* Fix up .dynamic entries. */
3545 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
3546 BFD_ASSERT (o
!= NULL
);
3548 dyncon
= (Elf_External_Dyn
*) o
->contents
;
3549 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
3550 for (; dyncon
< dynconend
; dyncon
++)
3552 Elf_Internal_Dyn dyn
;
3556 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
3563 if (relativecount
> 0 && dyncon
+ 1 < dynconend
)
3565 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
3567 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
3568 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
3571 if (dyn
.d_tag
!= DT_NULL
)
3573 dyn
.d_un
.d_val
= relativecount
;
3574 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3580 name
= info
->init_function
;
3583 name
= info
->fini_function
;
3586 struct elf_link_hash_entry
*h
;
3588 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
3589 FALSE
, FALSE
, TRUE
);
3591 && (h
->root
.type
== bfd_link_hash_defined
3592 || h
->root
.type
== bfd_link_hash_defweak
))
3594 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
3595 o
= h
->root
.u
.def
.section
;
3596 if (o
->output_section
!= NULL
)
3597 dyn
.d_un
.d_val
+= (o
->output_section
->vma
3598 + o
->output_offset
);
3601 /* The symbol is imported from another shared
3602 library and does not apply to this one. */
3606 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3611 case DT_PREINIT_ARRAYSZ
:
3612 name
= ".preinit_array";
3614 case DT_INIT_ARRAYSZ
:
3615 name
= ".init_array";
3617 case DT_FINI_ARRAYSZ
:
3618 name
= ".fini_array";
3620 o
= bfd_get_section_by_name (abfd
, name
);
3623 (*_bfd_error_handler
)
3624 (_("%s: could not find output section %s"),
3625 bfd_get_filename (abfd
), name
);
3628 if (o
->_raw_size
== 0)
3629 (*_bfd_error_handler
)
3630 (_("warning: %s section has zero size"), name
);
3631 dyn
.d_un
.d_val
= o
->_raw_size
;
3632 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3635 case DT_PREINIT_ARRAY
:
3636 name
= ".preinit_array";
3639 name
= ".init_array";
3642 name
= ".fini_array";
3655 name
= ".gnu.version_d";
3658 name
= ".gnu.version_r";
3661 name
= ".gnu.version";
3663 o
= bfd_get_section_by_name (abfd
, name
);
3666 (*_bfd_error_handler
)
3667 (_("%s: could not find output section %s"),
3668 bfd_get_filename (abfd
), name
);
3671 dyn
.d_un
.d_ptr
= o
->vma
;
3672 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3679 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
3684 for (i
= 1; i
< elf_numsections (abfd
); i
++)
3686 Elf_Internal_Shdr
*hdr
;
3688 hdr
= elf_elfsections (abfd
)[i
];
3689 if (hdr
->sh_type
== type
3690 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
3692 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
3693 dyn
.d_un
.d_val
+= hdr
->sh_size
;
3696 if (dyn
.d_un
.d_val
== 0
3697 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
3698 dyn
.d_un
.d_val
= hdr
->sh_addr
;
3702 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3708 /* If we have created any dynamic sections, then output them. */
3711 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
3714 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
3716 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
3717 || o
->_raw_size
== 0
3718 || o
->output_section
== bfd_abs_section_ptr
)
3720 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
3722 /* At this point, we are only interested in sections
3723 created by _bfd_elf_link_create_dynamic_sections. */
3726 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
3728 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
3730 if (! bfd_set_section_contents (abfd
, o
->output_section
,
3732 (file_ptr
) o
->output_offset
,
3738 /* The contents of the .dynstr section are actually in a
3740 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
3741 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
3742 || ! _bfd_elf_strtab_emit (abfd
,
3743 elf_hash_table (info
)->dynstr
))
3749 if (info
->relocatable
)
3751 bfd_boolean failed
= FALSE
;
3753 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3758 /* If we have optimized stabs strings, output them. */
3759 if (elf_hash_table (info
)->stab_info
!= NULL
)
3761 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
3765 if (info
->eh_frame_hdr
)
3767 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
3771 if (finfo
.symstrtab
!= NULL
)
3772 _bfd_stringtab_free (finfo
.symstrtab
);
3773 if (finfo
.contents
!= NULL
)
3774 free (finfo
.contents
);
3775 if (finfo
.external_relocs
!= NULL
)
3776 free (finfo
.external_relocs
);
3777 if (finfo
.internal_relocs
!= NULL
)
3778 free (finfo
.internal_relocs
);
3779 if (finfo
.external_syms
!= NULL
)
3780 free (finfo
.external_syms
);
3781 if (finfo
.locsym_shndx
!= NULL
)
3782 free (finfo
.locsym_shndx
);
3783 if (finfo
.internal_syms
!= NULL
)
3784 free (finfo
.internal_syms
);
3785 if (finfo
.indices
!= NULL
)
3786 free (finfo
.indices
);
3787 if (finfo
.sections
!= NULL
)
3788 free (finfo
.sections
);
3789 if (finfo
.symbuf
!= NULL
)
3790 free (finfo
.symbuf
);
3791 if (finfo
.symshndxbuf
!= NULL
)
3792 free (finfo
.symshndxbuf
);
3793 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3795 if ((o
->flags
& SEC_RELOC
) != 0
3796 && elf_section_data (o
)->rel_hashes
!= NULL
)
3797 free (elf_section_data (o
)->rel_hashes
);
3800 elf_tdata (abfd
)->linker
= TRUE
;
3805 if (finfo
.symstrtab
!= NULL
)
3806 _bfd_stringtab_free (finfo
.symstrtab
);
3807 if (finfo
.contents
!= NULL
)
3808 free (finfo
.contents
);
3809 if (finfo
.external_relocs
!= NULL
)
3810 free (finfo
.external_relocs
);
3811 if (finfo
.internal_relocs
!= NULL
)
3812 free (finfo
.internal_relocs
);
3813 if (finfo
.external_syms
!= NULL
)
3814 free (finfo
.external_syms
);
3815 if (finfo
.locsym_shndx
!= NULL
)
3816 free (finfo
.locsym_shndx
);
3817 if (finfo
.internal_syms
!= NULL
)
3818 free (finfo
.internal_syms
);
3819 if (finfo
.indices
!= NULL
)
3820 free (finfo
.indices
);
3821 if (finfo
.sections
!= NULL
)
3822 free (finfo
.sections
);
3823 if (finfo
.symbuf
!= NULL
)
3824 free (finfo
.symbuf
);
3825 if (finfo
.symshndxbuf
!= NULL
)
3826 free (finfo
.symshndxbuf
);
3827 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3829 if ((o
->flags
& SEC_RELOC
) != 0
3830 && elf_section_data (o
)->rel_hashes
!= NULL
)
3831 free (elf_section_data (o
)->rel_hashes
);
3837 /* Add a symbol to the output symbol table. */
3840 elf_link_output_sym (struct elf_final_link_info
*finfo
,
3842 Elf_Internal_Sym
*elfsym
,
3843 asection
*input_sec
,
3844 struct elf_link_hash_entry
*h
)
3846 Elf_External_Sym
*dest
;
3847 Elf_External_Sym_Shndx
*destshndx
;
3848 bfd_boolean (*output_symbol_hook
)
3849 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
3850 struct elf_link_hash_entry
*);
3852 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
3853 elf_backend_link_output_symbol_hook
;
3854 if (output_symbol_hook
!= NULL
)
3856 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
3860 if (name
== NULL
|| *name
== '\0')
3861 elfsym
->st_name
= 0;
3862 else if (input_sec
->flags
& SEC_EXCLUDE
)
3863 elfsym
->st_name
= 0;
3866 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
3868 if (elfsym
->st_name
== (unsigned long) -1)
3872 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
3874 if (! elf_link_flush_output_syms (finfo
))
3878 dest
= finfo
->symbuf
+ finfo
->symbuf_count
;
3879 destshndx
= finfo
->symshndxbuf
;
3880 if (destshndx
!= NULL
)
3882 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
3886 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
3887 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
3888 if (destshndx
== NULL
)
3890 memset ((char *) destshndx
+ amt
, 0, amt
);
3891 finfo
->shndxbuf_size
*= 2;
3893 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
3896 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
3897 finfo
->symbuf_count
+= 1;
3898 bfd_get_symcount (finfo
->output_bfd
) += 1;
3903 /* Flush the output symbols to the file. */
3906 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
)
3908 if (finfo
->symbuf_count
> 0)
3910 Elf_Internal_Shdr
*hdr
;
3914 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
3915 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
3916 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
3917 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
3918 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
3921 hdr
->sh_size
+= amt
;
3922 finfo
->symbuf_count
= 0;
3928 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
3929 allowing an unsatisfied unversioned symbol in the DSO to match a
3930 versioned symbol that would normally require an explicit version.
3931 We also handle the case that a DSO references a hidden symbol
3932 which may be satisfied by a versioned symbol in another DSO. */
3935 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
3936 struct elf_link_hash_entry
*h
)
3939 struct elf_link_loaded_list
*loaded
;
3941 if (!is_elf_hash_table (info
->hash
))
3944 switch (h
->root
.type
)
3950 case bfd_link_hash_undefined
:
3951 case bfd_link_hash_undefweak
:
3952 abfd
= h
->root
.u
.undef
.abfd
;
3953 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dt_soname (abfd
) == NULL
)
3957 case bfd_link_hash_defined
:
3958 case bfd_link_hash_defweak
:
3959 abfd
= h
->root
.u
.def
.section
->owner
;
3962 case bfd_link_hash_common
:
3963 abfd
= h
->root
.u
.c
.p
->section
->owner
;
3966 BFD_ASSERT (abfd
!= NULL
);
3968 for (loaded
= elf_hash_table (info
)->loaded
;
3970 loaded
= loaded
->next
)
3973 Elf_Internal_Shdr
*hdr
;
3974 bfd_size_type symcount
;
3975 bfd_size_type extsymcount
;
3976 bfd_size_type extsymoff
;
3977 Elf_Internal_Shdr
*versymhdr
;
3978 Elf_Internal_Sym
*isym
;
3979 Elf_Internal_Sym
*isymend
;
3980 Elf_Internal_Sym
*isymbuf
;
3981 Elf_External_Versym
*ever
;
3982 Elf_External_Versym
*extversym
;
3984 input
= loaded
->abfd
;
3986 /* We check each DSO for a possible hidden versioned definition. */
3988 || (input
->flags
& DYNAMIC
) == 0
3989 || elf_dynversym (input
) == 0)
3992 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
3994 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
3995 if (elf_bad_symtab (input
))
3997 extsymcount
= symcount
;
4002 extsymcount
= symcount
- hdr
->sh_info
;
4003 extsymoff
= hdr
->sh_info
;
4006 if (extsymcount
== 0)
4009 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
4011 if (isymbuf
== NULL
)
4014 /* Read in any version definitions. */
4015 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
4016 extversym
= bfd_malloc (versymhdr
->sh_size
);
4017 if (extversym
== NULL
)
4020 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
4021 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
4022 != versymhdr
->sh_size
))
4030 ever
= extversym
+ extsymoff
;
4031 isymend
= isymbuf
+ extsymcount
;
4032 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
4035 Elf_Internal_Versym iver
;
4036 unsigned short version_index
;
4038 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
4039 || isym
->st_shndx
== SHN_UNDEF
)
4042 name
= bfd_elf_string_from_elf_section (input
,
4045 if (strcmp (name
, h
->root
.root
.string
) != 0)
4048 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
4050 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
4052 /* If we have a non-hidden versioned sym, then it should
4053 have provided a definition for the undefined sym. */
4057 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
4058 if (version_index
== 1 || version_index
== 2)
4060 /* This is the base or first version. We can use it. */
4074 /* Add an external symbol to the symbol table. This is called from
4075 the hash table traversal routine. When generating a shared object,
4076 we go through the symbol table twice. The first time we output
4077 anything that might have been forced to local scope in a version
4078 script. The second time we output the symbols that are still
4082 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
4084 struct elf_outext_info
*eoinfo
= data
;
4085 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
4087 Elf_Internal_Sym sym
;
4088 asection
*input_sec
;
4090 if (h
->root
.type
== bfd_link_hash_warning
)
4092 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4093 if (h
->root
.type
== bfd_link_hash_new
)
4097 /* Decide whether to output this symbol in this pass. */
4098 if (eoinfo
->localsyms
)
4100 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
4105 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4109 /* If we have an undefined symbol reference here then it must have
4110 come from a shared library that is being linked in. (Undefined
4111 references in regular files have already been handled). If we
4112 are reporting errors for this situation then do so now. */
4113 if (h
->root
.type
== bfd_link_hash_undefined
4114 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
4115 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
4116 && ! elf_link_check_versioned_symbol (finfo
->info
, h
)
4117 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
4119 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
4120 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
4121 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
4123 eoinfo
->failed
= TRUE
;
4128 /* We should also warn if a forced local symbol is referenced from
4129 shared libraries. */
4130 if (! finfo
->info
->relocatable
4131 && (! finfo
->info
->shared
)
4132 && (h
->elf_link_hash_flags
4133 & (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
| ELF_LINK_DYNAMIC_DEF
| ELF_LINK_DYNAMIC_WEAK
))
4134 == (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
)
4135 && ! elf_link_check_versioned_symbol (finfo
->info
, h
))
4137 (*_bfd_error_handler
)
4138 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
4139 bfd_get_filename (finfo
->output_bfd
),
4140 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
4142 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
4143 ? "hidden" : "local",
4144 h
->root
.root
.string
,
4145 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
4146 eoinfo
->failed
= TRUE
;
4150 /* We don't want to output symbols that have never been mentioned by
4151 a regular file, or that we have been told to strip. However, if
4152 h->indx is set to -2, the symbol is used by a reloc and we must
4156 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
4157 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
4158 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
4159 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
4161 else if (finfo
->info
->strip
== strip_all
)
4163 else if (finfo
->info
->strip
== strip_some
4164 && bfd_hash_lookup (finfo
->info
->keep_hash
,
4165 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
4167 else if (finfo
->info
->strip_discarded
4168 && (h
->root
.type
== bfd_link_hash_defined
4169 || h
->root
.type
== bfd_link_hash_defweak
)
4170 && elf_discarded_section (h
->root
.u
.def
.section
))
4175 /* If we're stripping it, and it's not a dynamic symbol, there's
4176 nothing else to do unless it is a forced local symbol. */
4179 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
4183 sym
.st_size
= h
->size
;
4184 sym
.st_other
= h
->other
;
4185 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4186 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
4187 else if (h
->root
.type
== bfd_link_hash_undefweak
4188 || h
->root
.type
== bfd_link_hash_defweak
)
4189 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
4191 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
4193 switch (h
->root
.type
)
4196 case bfd_link_hash_new
:
4197 case bfd_link_hash_warning
:
4201 case bfd_link_hash_undefined
:
4202 case bfd_link_hash_undefweak
:
4203 input_sec
= bfd_und_section_ptr
;
4204 sym
.st_shndx
= SHN_UNDEF
;
4207 case bfd_link_hash_defined
:
4208 case bfd_link_hash_defweak
:
4210 input_sec
= h
->root
.u
.def
.section
;
4211 if (input_sec
->output_section
!= NULL
)
4214 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
4215 input_sec
->output_section
);
4216 if (sym
.st_shndx
== SHN_BAD
)
4218 (*_bfd_error_handler
)
4219 (_("%s: could not find output section %s for input section %s"),
4220 bfd_get_filename (finfo
->output_bfd
),
4221 input_sec
->output_section
->name
,
4223 eoinfo
->failed
= TRUE
;
4227 /* ELF symbols in relocatable files are section relative,
4228 but in nonrelocatable files they are virtual
4230 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
4231 if (! finfo
->info
->relocatable
)
4233 sym
.st_value
+= input_sec
->output_section
->vma
;
4234 if (h
->type
== STT_TLS
)
4236 /* STT_TLS symbols are relative to PT_TLS segment
4238 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
4239 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
4245 BFD_ASSERT (input_sec
->owner
== NULL
4246 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
4247 sym
.st_shndx
= SHN_UNDEF
;
4248 input_sec
= bfd_und_section_ptr
;
4253 case bfd_link_hash_common
:
4254 input_sec
= h
->root
.u
.c
.p
->section
;
4255 sym
.st_shndx
= SHN_COMMON
;
4256 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
4259 case bfd_link_hash_indirect
:
4260 /* These symbols are created by symbol versioning. They point
4261 to the decorated version of the name. For example, if the
4262 symbol foo@@GNU_1.2 is the default, which should be used when
4263 foo is used with no version, then we add an indirect symbol
4264 foo which points to foo@@GNU_1.2. We ignore these symbols,
4265 since the indirected symbol is already in the hash table. */
4269 /* Give the processor backend a chance to tweak the symbol value,
4270 and also to finish up anything that needs to be done for this
4271 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
4272 forced local syms when non-shared is due to a historical quirk. */
4273 if ((h
->dynindx
!= -1
4274 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4275 && ((finfo
->info
->shared
4276 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4277 || h
->root
.type
!= bfd_link_hash_undefweak
))
4278 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
4279 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4281 const struct elf_backend_data
*bed
;
4283 bed
= get_elf_backend_data (finfo
->output_bfd
);
4284 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
4285 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
4287 eoinfo
->failed
= TRUE
;
4292 /* If we are marking the symbol as undefined, and there are no
4293 non-weak references to this symbol from a regular object, then
4294 mark the symbol as weak undefined; if there are non-weak
4295 references, mark the symbol as strong. We can't do this earlier,
4296 because it might not be marked as undefined until the
4297 finish_dynamic_symbol routine gets through with it. */
4298 if (sym
.st_shndx
== SHN_UNDEF
4299 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
4300 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
4301 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
4305 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
4306 bindtype
= STB_GLOBAL
;
4308 bindtype
= STB_WEAK
;
4309 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
4312 /* If a non-weak symbol with non-default visibility is not defined
4313 locally, it is a fatal error. */
4314 if (! finfo
->info
->relocatable
4315 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
4316 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
4317 && h
->root
.type
== bfd_link_hash_undefined
4318 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4320 (*_bfd_error_handler
)
4321 (_("%s: %s symbol `%s' isn't defined"),
4322 bfd_get_filename (finfo
->output_bfd
),
4323 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
4325 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
4326 ? "internal" : "hidden",
4327 h
->root
.root
.string
);
4328 eoinfo
->failed
= TRUE
;
4332 /* If this symbol should be put in the .dynsym section, then put it
4333 there now. We already know the symbol index. We also fill in
4334 the entry in the .hash section. */
4335 if (h
->dynindx
!= -1
4336 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4340 size_t hash_entry_size
;
4341 bfd_byte
*bucketpos
;
4343 Elf_External_Sym
*esym
;
4345 sym
.st_name
= h
->dynstr_index
;
4346 esym
= (Elf_External_Sym
*) finfo
->dynsym_sec
->contents
+ h
->dynindx
;
4347 elf_swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
4349 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
4350 bucket
= h
->elf_hash_value
% bucketcount
;
4352 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
4353 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
4354 + (bucket
+ 2) * hash_entry_size
);
4355 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
4356 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
4357 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
4358 ((bfd_byte
*) finfo
->hash_sec
->contents
4359 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
4361 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
4363 Elf_Internal_Versym iversym
;
4364 Elf_External_Versym
*eversym
;
4366 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4368 if (h
->verinfo
.verdef
== NULL
)
4369 iversym
.vs_vers
= 0;
4371 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4375 if (h
->verinfo
.vertree
== NULL
)
4376 iversym
.vs_vers
= 1;
4378 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
4381 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
4382 iversym
.vs_vers
|= VERSYM_HIDDEN
;
4384 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
4385 eversym
+= h
->dynindx
;
4386 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
4390 /* If we're stripping it, then it was just a dynamic symbol, and
4391 there's nothing else to do. */
4392 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
4395 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
4397 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
4399 eoinfo
->failed
= TRUE
;
4406 /* Link an input file into the linker output file. This function
4407 handles all the sections and relocations of the input file at once.
4408 This is so that we only have to read the local symbols once, and
4409 don't have to keep them in memory. */
4412 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
4414 bfd_boolean (*relocate_section
)
4415 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
4416 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
4418 Elf_Internal_Shdr
*symtab_hdr
;
4421 Elf_Internal_Sym
*isymbuf
;
4422 Elf_Internal_Sym
*isym
;
4423 Elf_Internal_Sym
*isymend
;
4425 asection
**ppsection
;
4427 const struct elf_backend_data
*bed
;
4428 bfd_boolean emit_relocs
;
4429 struct elf_link_hash_entry
**sym_hashes
;
4431 output_bfd
= finfo
->output_bfd
;
4432 bed
= get_elf_backend_data (output_bfd
);
4433 relocate_section
= bed
->elf_backend_relocate_section
;
4435 /* If this is a dynamic object, we don't want to do anything here:
4436 we don't want the local symbols, and we don't want the section
4438 if ((input_bfd
->flags
& DYNAMIC
) != 0)
4441 emit_relocs
= (finfo
->info
->relocatable
4442 || finfo
->info
->emitrelocations
4443 || bed
->elf_backend_emit_relocs
);
4445 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
4446 if (elf_bad_symtab (input_bfd
))
4448 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
4453 locsymcount
= symtab_hdr
->sh_info
;
4454 extsymoff
= symtab_hdr
->sh_info
;
4457 /* Read the local symbols. */
4458 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
4459 if (isymbuf
== NULL
&& locsymcount
!= 0)
4461 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
4462 finfo
->internal_syms
,
4463 finfo
->external_syms
,
4464 finfo
->locsym_shndx
);
4465 if (isymbuf
== NULL
)
4469 /* Find local symbol sections and adjust values of symbols in
4470 SEC_MERGE sections. Write out those local symbols we know are
4471 going into the output file. */
4472 isymend
= isymbuf
+ locsymcount
;
4473 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
4475 isym
++, pindex
++, ppsection
++)
4479 Elf_Internal_Sym osym
;
4483 if (elf_bad_symtab (input_bfd
))
4485 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
4492 if (isym
->st_shndx
== SHN_UNDEF
)
4493 isec
= bfd_und_section_ptr
;
4494 else if (isym
->st_shndx
< SHN_LORESERVE
4495 || isym
->st_shndx
> SHN_HIRESERVE
)
4497 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
4499 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
4500 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
4502 _bfd_merged_section_offset (output_bfd
, &isec
,
4503 elf_section_data (isec
)->sec_info
,
4506 else if (isym
->st_shndx
== SHN_ABS
)
4507 isec
= bfd_abs_section_ptr
;
4508 else if (isym
->st_shndx
== SHN_COMMON
)
4509 isec
= bfd_com_section_ptr
;
4518 /* Don't output the first, undefined, symbol. */
4519 if (ppsection
== finfo
->sections
)
4522 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
4524 /* We never output section symbols. Instead, we use the
4525 section symbol of the corresponding section in the output
4530 /* If we are stripping all symbols, we don't want to output this
4532 if (finfo
->info
->strip
== strip_all
)
4535 /* If we are discarding all local symbols, we don't want to
4536 output this one. If we are generating a relocatable output
4537 file, then some of the local symbols may be required by
4538 relocs; we output them below as we discover that they are
4540 if (finfo
->info
->discard
== discard_all
)
4543 /* If this symbol is defined in a section which we are
4544 discarding, we don't need to keep it, but note that
4545 linker_mark is only reliable for sections that have contents.
4546 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4547 as well as linker_mark. */
4548 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
4550 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
4551 || (! finfo
->info
->relocatable
4552 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
4555 /* Get the name of the symbol. */
4556 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
4561 /* See if we are discarding symbols with this name. */
4562 if ((finfo
->info
->strip
== strip_some
4563 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
4565 || (((finfo
->info
->discard
== discard_sec_merge
4566 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
4567 || finfo
->info
->discard
== discard_l
)
4568 && bfd_is_local_label_name (input_bfd
, name
)))
4571 /* If we get here, we are going to output this symbol. */
4575 /* Adjust the section index for the output file. */
4576 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
4577 isec
->output_section
);
4578 if (osym
.st_shndx
== SHN_BAD
)
4581 *pindex
= bfd_get_symcount (output_bfd
);
4583 /* ELF symbols in relocatable files are section relative, but
4584 in executable files they are virtual addresses. Note that
4585 this code assumes that all ELF sections have an associated
4586 BFD section with a reasonable value for output_offset; below
4587 we assume that they also have a reasonable value for
4588 output_section. Any special sections must be set up to meet
4589 these requirements. */
4590 osym
.st_value
+= isec
->output_offset
;
4591 if (! finfo
->info
->relocatable
)
4593 osym
.st_value
+= isec
->output_section
->vma
;
4594 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
4596 /* STT_TLS symbols are relative to PT_TLS segment base. */
4597 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
4598 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
4602 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
4606 /* Relocate the contents of each section. */
4607 sym_hashes
= elf_sym_hashes (input_bfd
);
4608 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4612 if (! o
->linker_mark
)
4614 /* This section was omitted from the link. */
4618 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4619 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
4622 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
4624 /* Section was created by _bfd_elf_link_create_dynamic_sections
4629 /* Get the contents of the section. They have been cached by a
4630 relaxation routine. Note that o is a section in an input
4631 file, so the contents field will not have been set by any of
4632 the routines which work on output files. */
4633 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
4634 contents
= elf_section_data (o
)->this_hdr
.contents
;
4637 contents
= finfo
->contents
;
4638 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0,
4643 if ((o
->flags
& SEC_RELOC
) != 0)
4645 Elf_Internal_Rela
*internal_relocs
;
4647 /* Get the swapped relocs. */
4649 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
4650 finfo
->internal_relocs
, FALSE
);
4651 if (internal_relocs
== NULL
4652 && o
->reloc_count
> 0)
4655 /* Run through the relocs looking for any against symbols
4656 from discarded sections and section symbols from
4657 removed link-once sections. Complain about relocs
4658 against discarded sections. Zero relocs against removed
4659 link-once sections. Preserve debug information as much
4661 if (!elf_section_ignore_discarded_relocs (o
))
4663 Elf_Internal_Rela
*rel
, *relend
;
4665 rel
= internal_relocs
;
4666 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
4667 for ( ; rel
< relend
; rel
++)
4669 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
4672 if (r_symndx
>= locsymcount
4673 || (elf_bad_symtab (input_bfd
)
4674 && finfo
->sections
[r_symndx
] == NULL
))
4676 struct elf_link_hash_entry
*h
;
4678 h
= sym_hashes
[r_symndx
- extsymoff
];
4679 while (h
->root
.type
== bfd_link_hash_indirect
4680 || h
->root
.type
== bfd_link_hash_warning
)
4681 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4683 /* Complain if the definition comes from a
4684 discarded section. */
4685 sec
= h
->root
.u
.def
.section
;
4686 if ((h
->root
.type
== bfd_link_hash_defined
4687 || h
->root
.type
== bfd_link_hash_defweak
)
4688 && elf_discarded_section (sec
))
4690 if ((o
->flags
& SEC_DEBUGGING
) != 0)
4692 BFD_ASSERT (r_symndx
!= 0);
4693 /* Try to preserve debug information. */
4694 if ((o
->flags
& SEC_DEBUGGING
) != 0
4695 && sec
->kept_section
!= NULL
4696 && sec
->_raw_size
== sec
->kept_section
->_raw_size
)
4697 h
->root
.u
.def
.section
4698 = sec
->kept_section
;
4700 memset (rel
, 0, sizeof (*rel
));
4703 finfo
->info
->callbacks
->error_handler
4704 (LD_DEFINITION_IN_DISCARDED_SECTION
,
4705 _("%T: discarded in section `%s' from %s\n"),
4706 h
->root
.root
.string
,
4707 h
->root
.root
.string
,
4708 h
->root
.u
.def
.section
->name
,
4709 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
4714 sec
= finfo
->sections
[r_symndx
];
4716 if (sec
!= NULL
&& elf_discarded_section (sec
))
4718 if ((o
->flags
& SEC_DEBUGGING
) != 0
4719 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
4721 BFD_ASSERT (r_symndx
!= 0);
4722 /* Try to preserve debug information. */
4723 if ((o
->flags
& SEC_DEBUGGING
) != 0
4724 && sec
->kept_section
!= NULL
4725 && sec
->_raw_size
== sec
->kept_section
->_raw_size
)
4726 finfo
->sections
[r_symndx
]
4727 = sec
->kept_section
;
4731 = ELF_R_INFO (0, ELF_R_TYPE (rel
->r_info
));
4741 ok
= asprintf (&buf
, "local symbol %d",
4744 buf
= (char *) "local symbol";
4745 finfo
->info
->callbacks
->error_handler
4746 (LD_DEFINITION_IN_DISCARDED_SECTION
,
4747 _("%T: discarded in section `%s' from %s\n"),
4748 buf
, buf
, sec
->name
,
4749 bfd_archive_filename (input_bfd
));
4758 /* Relocate the section by invoking a back end routine.
4760 The back end routine is responsible for adjusting the
4761 section contents as necessary, and (if using Rela relocs
4762 and generating a relocatable output file) adjusting the
4763 reloc addend as necessary.
4765 The back end routine does not have to worry about setting
4766 the reloc address or the reloc symbol index.
4768 The back end routine is given a pointer to the swapped in
4769 internal symbols, and can access the hash table entries
4770 for the external symbols via elf_sym_hashes (input_bfd).
4772 When generating relocatable output, the back end routine
4773 must handle STB_LOCAL/STT_SECTION symbols specially. The
4774 output symbol is going to be a section symbol
4775 corresponding to the output section, which will require
4776 the addend to be adjusted. */
4778 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
4779 input_bfd
, o
, contents
,
4787 Elf_Internal_Rela
*irela
;
4788 Elf_Internal_Rela
*irelaend
;
4789 bfd_vma last_offset
;
4790 struct elf_link_hash_entry
**rel_hash
;
4791 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
4792 unsigned int next_erel
;
4793 bfd_boolean (*reloc_emitter
)
4794 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
4795 bfd_boolean rela_normal
;
4797 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
4798 rela_normal
= (bed
->rela_normal
4799 && (input_rel_hdr
->sh_entsize
4800 == sizeof (Elf_External_Rela
)));
4802 /* Adjust the reloc addresses and symbol indices. */
4804 irela
= internal_relocs
;
4805 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
4806 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
4807 + elf_section_data (o
->output_section
)->rel_count
4808 + elf_section_data (o
->output_section
)->rel_count2
);
4809 last_offset
= o
->output_offset
;
4810 if (!finfo
->info
->relocatable
)
4811 last_offset
+= o
->output_section
->vma
;
4812 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
4814 unsigned long r_symndx
;
4816 Elf_Internal_Sym sym
;
4818 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
4824 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
4827 if (irela
->r_offset
>= (bfd_vma
) -2)
4829 /* This is a reloc for a deleted entry or somesuch.
4830 Turn it into an R_*_NONE reloc, at the same
4831 offset as the last reloc. elf_eh_frame.c and
4832 elf_bfd_discard_info rely on reloc offsets
4834 irela
->r_offset
= last_offset
;
4836 irela
->r_addend
= 0;
4840 irela
->r_offset
+= o
->output_offset
;
4842 /* Relocs in an executable have to be virtual addresses. */
4843 if (!finfo
->info
->relocatable
)
4844 irela
->r_offset
+= o
->output_section
->vma
;
4846 last_offset
= irela
->r_offset
;
4848 r_symndx
= ELF_R_SYM (irela
->r_info
);
4849 if (r_symndx
== STN_UNDEF
)
4852 if (r_symndx
>= locsymcount
4853 || (elf_bad_symtab (input_bfd
)
4854 && finfo
->sections
[r_symndx
] == NULL
))
4856 struct elf_link_hash_entry
*rh
;
4859 /* This is a reloc against a global symbol. We
4860 have not yet output all the local symbols, so
4861 we do not know the symbol index of any global
4862 symbol. We set the rel_hash entry for this
4863 reloc to point to the global hash table entry
4864 for this symbol. The symbol index is then
4865 set at the end of elf_bfd_final_link. */
4866 indx
= r_symndx
- extsymoff
;
4867 rh
= elf_sym_hashes (input_bfd
)[indx
];
4868 while (rh
->root
.type
== bfd_link_hash_indirect
4869 || rh
->root
.type
== bfd_link_hash_warning
)
4870 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
4872 /* Setting the index to -2 tells
4873 elf_link_output_extsym that this symbol is
4875 BFD_ASSERT (rh
->indx
< 0);
4883 /* This is a reloc against a local symbol. */
4886 sym
= isymbuf
[r_symndx
];
4887 sec
= finfo
->sections
[r_symndx
];
4888 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
4890 /* I suppose the backend ought to fill in the
4891 section of any STT_SECTION symbol against a
4892 processor specific section. If we have
4893 discarded a section, the output_section will
4894 be the absolute section. */
4895 if (bfd_is_abs_section (sec
)
4897 && bfd_is_abs_section (sec
->output_section
)))
4899 else if (sec
== NULL
|| sec
->owner
== NULL
)
4901 bfd_set_error (bfd_error_bad_value
);
4906 r_symndx
= sec
->output_section
->target_index
;
4907 BFD_ASSERT (r_symndx
!= 0);
4910 /* Adjust the addend according to where the
4911 section winds up in the output section. */
4913 irela
->r_addend
+= sec
->output_offset
;
4917 if (finfo
->indices
[r_symndx
] == -1)
4919 unsigned long shlink
;
4923 if (finfo
->info
->strip
== strip_all
)
4925 /* You can't do ld -r -s. */
4926 bfd_set_error (bfd_error_invalid_operation
);
4930 /* This symbol was skipped earlier, but
4931 since it is needed by a reloc, we
4932 must output it now. */
4933 shlink
= symtab_hdr
->sh_link
;
4934 name
= (bfd_elf_string_from_elf_section
4935 (input_bfd
, shlink
, sym
.st_name
));
4939 osec
= sec
->output_section
;
4941 _bfd_elf_section_from_bfd_section (output_bfd
,
4943 if (sym
.st_shndx
== SHN_BAD
)
4946 sym
.st_value
+= sec
->output_offset
;
4947 if (! finfo
->info
->relocatable
)
4949 sym
.st_value
+= osec
->vma
;
4950 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
4952 /* STT_TLS symbols are relative to PT_TLS
4954 BFD_ASSERT (elf_hash_table (finfo
->info
)
4956 sym
.st_value
-= (elf_hash_table (finfo
->info
)
4961 finfo
->indices
[r_symndx
]
4962 = bfd_get_symcount (output_bfd
);
4964 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
4969 r_symndx
= finfo
->indices
[r_symndx
];
4972 irela
->r_info
= ELF_R_INFO (r_symndx
,
4973 ELF_R_TYPE (irela
->r_info
));
4976 /* Swap out the relocs. */
4977 if (bed
->elf_backend_emit_relocs
4978 && !(finfo
->info
->relocatable
4979 || finfo
->info
->emitrelocations
))
4980 reloc_emitter
= bed
->elf_backend_emit_relocs
;
4982 reloc_emitter
= _bfd_elf_link_output_relocs
;
4984 if (input_rel_hdr
->sh_size
!= 0
4985 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
4989 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
4990 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
4992 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
4993 * bed
->s
->int_rels_per_ext_rel
);
4994 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
5001 /* Write out the modified section contents. */
5002 if (bed
->elf_backend_write_section
5003 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
5005 /* Section written out. */
5007 else switch (o
->sec_info_type
)
5009 case ELF_INFO_TYPE_STABS
:
5010 if (! (_bfd_write_section_stabs
5012 &elf_hash_table (finfo
->info
)->stab_info
,
5013 o
, &elf_section_data (o
)->sec_info
, contents
)))
5016 case ELF_INFO_TYPE_MERGE
:
5017 if (! _bfd_write_merged_section (output_bfd
, o
,
5018 elf_section_data (o
)->sec_info
))
5021 case ELF_INFO_TYPE_EH_FRAME
:
5023 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
5030 bfd_size_type sec_size
;
5032 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
5033 if (! (o
->flags
& SEC_EXCLUDE
)
5034 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
5036 (file_ptr
) o
->output_offset
,
5047 /* Generate a reloc when linking an ELF file. This is a reloc
5048 requested by the linker, and does come from any input file. This
5049 is used to build constructor and destructor tables when linking
5053 elf_reloc_link_order (bfd
*output_bfd
,
5054 struct bfd_link_info
*info
,
5055 asection
*output_section
,
5056 struct bfd_link_order
*link_order
)
5058 reloc_howto_type
*howto
;
5062 struct elf_link_hash_entry
**rel_hash_ptr
;
5063 Elf_Internal_Shdr
*rel_hdr
;
5064 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
5065 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
5069 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5072 bfd_set_error (bfd_error_bad_value
);
5076 addend
= link_order
->u
.reloc
.p
->addend
;
5078 /* Figure out the symbol index. */
5079 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
5080 + elf_section_data (output_section
)->rel_count
5081 + elf_section_data (output_section
)->rel_count2
);
5082 if (link_order
->type
== bfd_section_reloc_link_order
)
5084 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
5085 BFD_ASSERT (indx
!= 0);
5086 *rel_hash_ptr
= NULL
;
5090 struct elf_link_hash_entry
*h
;
5092 /* Treat a reloc against a defined symbol as though it were
5093 actually against the section. */
5094 h
= ((struct elf_link_hash_entry
*)
5095 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
5096 link_order
->u
.reloc
.p
->u
.name
,
5097 FALSE
, FALSE
, TRUE
));
5099 && (h
->root
.type
== bfd_link_hash_defined
5100 || h
->root
.type
== bfd_link_hash_defweak
))
5104 section
= h
->root
.u
.def
.section
;
5105 indx
= section
->output_section
->target_index
;
5106 *rel_hash_ptr
= NULL
;
5107 /* It seems that we ought to add the symbol value to the
5108 addend here, but in practice it has already been added
5109 because it was passed to constructor_callback. */
5110 addend
+= section
->output_section
->vma
+ section
->output_offset
;
5114 /* Setting the index to -2 tells elf_link_output_extsym that
5115 this symbol is used by a reloc. */
5122 if (! ((*info
->callbacks
->unattached_reloc
)
5123 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
5129 /* If this is an inplace reloc, we must write the addend into the
5131 if (howto
->partial_inplace
&& addend
!= 0)
5134 bfd_reloc_status_type rstat
;
5137 const char *sym_name
;
5139 size
= bfd_get_reloc_size (howto
);
5140 buf
= bfd_zmalloc (size
);
5143 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5150 case bfd_reloc_outofrange
:
5153 case bfd_reloc_overflow
:
5154 if (link_order
->type
== bfd_section_reloc_link_order
)
5155 sym_name
= bfd_section_name (output_bfd
,
5156 link_order
->u
.reloc
.p
->u
.section
);
5158 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
5159 if (! ((*info
->callbacks
->reloc_overflow
)
5160 (info
, sym_name
, howto
->name
, addend
, NULL
, NULL
, 0)))
5167 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
5168 link_order
->offset
, size
);
5174 /* The address of a reloc is relative to the section in a
5175 relocatable file, and is a virtual address in an executable
5177 offset
= link_order
->offset
;
5178 if (! info
->relocatable
)
5179 offset
+= output_section
->vma
;
5181 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
5183 irel
[i
].r_offset
= offset
;
5185 irel
[i
].r_addend
= 0;
5187 irel
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
5189 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
5190 erel
= rel_hdr
->contents
;
5191 if (rel_hdr
->sh_type
== SHT_REL
)
5193 erel
+= (elf_section_data (output_section
)->rel_count
5194 * sizeof (Elf_External_Rel
));
5195 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
5199 irel
[0].r_addend
= addend
;
5200 erel
+= (elf_section_data (output_section
)->rel_count
5201 * sizeof (Elf_External_Rela
));
5202 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
5205 ++elf_section_data (output_section
)->rel_count
;
5210 /* Garbage collect unused sections. */
5212 static bfd_boolean elf_gc_sweep_symbol
5213 (struct elf_link_hash_entry
*, void *);
5215 static bfd_boolean elf_gc_allocate_got_offsets
5216 (struct elf_link_hash_entry
*, void *);
5218 /* The mark phase of garbage collection. For a given section, mark
5219 it and any sections in this section's group, and all the sections
5220 which define symbols to which it refers. */
5222 typedef asection
* (*gc_mark_hook_fn
)
5223 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
5224 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
5227 elf_gc_mark (struct bfd_link_info
*info
,
5229 gc_mark_hook_fn gc_mark_hook
)
5232 asection
*group_sec
;
5236 /* Mark all the sections in the group. */
5237 group_sec
= elf_section_data (sec
)->next_in_group
;
5238 if (group_sec
&& !group_sec
->gc_mark
)
5239 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
5242 /* Look through the section relocs. */
5244 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
5246 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
5247 Elf_Internal_Shdr
*symtab_hdr
;
5248 struct elf_link_hash_entry
**sym_hashes
;
5251 bfd
*input_bfd
= sec
->owner
;
5252 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
5253 Elf_Internal_Sym
*isym
= NULL
;
5255 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
5256 sym_hashes
= elf_sym_hashes (input_bfd
);
5258 /* Read the local symbols. */
5259 if (elf_bad_symtab (input_bfd
))
5261 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
5265 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
5267 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
5268 if (isym
== NULL
&& nlocsyms
!= 0)
5270 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
5276 /* Read the relocations. */
5277 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
5279 if (relstart
== NULL
)
5284 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5286 for (rel
= relstart
; rel
< relend
; rel
++)
5288 unsigned long r_symndx
;
5290 struct elf_link_hash_entry
*h
;
5292 r_symndx
= ELF_R_SYM (rel
->r_info
);
5296 if (r_symndx
>= nlocsyms
5297 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
5299 h
= sym_hashes
[r_symndx
- extsymoff
];
5300 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
5304 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
5307 if (rsec
&& !rsec
->gc_mark
)
5309 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
5311 else if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
5320 if (elf_section_data (sec
)->relocs
!= relstart
)
5323 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
5325 if (! info
->keep_memory
)
5328 symtab_hdr
->contents
= (unsigned char *) isym
;
5335 /* The sweep phase of garbage collection. Remove all garbage sections. */
5337 typedef bfd_boolean (*gc_sweep_hook_fn
)
5338 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
5341 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
5345 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5349 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
5352 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5354 /* Keep special sections. Keep .debug sections. */
5355 if ((o
->flags
& SEC_LINKER_CREATED
)
5356 || (o
->flags
& SEC_DEBUGGING
))
5362 /* Skip sweeping sections already excluded. */
5363 if (o
->flags
& SEC_EXCLUDE
)
5366 /* Since this is early in the link process, it is simple
5367 to remove a section from the output. */
5368 o
->flags
|= SEC_EXCLUDE
;
5370 /* But we also have to update some of the relocation
5371 info we collected before. */
5373 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
5375 Elf_Internal_Rela
*internal_relocs
;
5379 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
5381 if (internal_relocs
== NULL
)
5384 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
5386 if (elf_section_data (o
)->relocs
!= internal_relocs
)
5387 free (internal_relocs
);
5395 /* Remove the symbols that were in the swept sections from the dynamic
5396 symbol table. GCFIXME: Anyone know how to get them out of the
5397 static symbol table as well? */
5401 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
5403 elf_hash_table (info
)->dynsymcount
= i
;
5409 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5412 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
5416 if (h
->root
.type
== bfd_link_hash_warning
)
5417 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5419 if (h
->dynindx
!= -1
5420 && ((h
->root
.type
!= bfd_link_hash_defined
5421 && h
->root
.type
!= bfd_link_hash_defweak
)
5422 || h
->root
.u
.def
.section
->gc_mark
))
5423 h
->dynindx
= (*idx
)++;
5428 /* Propagate collected vtable information. This is called through
5429 elf_link_hash_traverse. */
5432 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
5434 if (h
->root
.type
== bfd_link_hash_warning
)
5435 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5437 /* Those that are not vtables. */
5438 if (h
->vtable_parent
== NULL
)
5441 /* Those vtables that do not have parents, we cannot merge. */
5442 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
5445 /* If we've already been done, exit. */
5446 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
5449 /* Make sure the parent's table is up to date. */
5450 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
5452 if (h
->vtable_entries_used
== NULL
)
5454 /* None of this table's entries were referenced. Re-use the
5456 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
5457 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
5462 bfd_boolean
*cu
, *pu
;
5464 /* Or the parent's entries into ours. */
5465 cu
= h
->vtable_entries_used
;
5467 pu
= h
->vtable_parent
->vtable_entries_used
;
5470 const struct elf_backend_data
*bed
;
5471 unsigned int log_file_align
;
5473 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
5474 log_file_align
= bed
->s
->log_file_align
;
5475 n
= h
->vtable_parent
->vtable_entries_size
>> log_file_align
;
5490 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
5493 bfd_vma hstart
, hend
;
5494 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
5495 const struct elf_backend_data
*bed
;
5496 unsigned int log_file_align
;
5498 if (h
->root
.type
== bfd_link_hash_warning
)
5499 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5501 /* Take care of both those symbols that do not describe vtables as
5502 well as those that are not loaded. */
5503 if (h
->vtable_parent
== NULL
)
5506 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5507 || h
->root
.type
== bfd_link_hash_defweak
);
5509 sec
= h
->root
.u
.def
.section
;
5510 hstart
= h
->root
.u
.def
.value
;
5511 hend
= hstart
+ h
->size
;
5513 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
5515 return *(bfd_boolean
*) okp
= FALSE
;
5516 bed
= get_elf_backend_data (sec
->owner
);
5517 log_file_align
= bed
->s
->log_file_align
;
5519 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5521 for (rel
= relstart
; rel
< relend
; ++rel
)
5522 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
5524 /* If the entry is in use, do nothing. */
5525 if (h
->vtable_entries_used
5526 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
5528 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
5529 if (h
->vtable_entries_used
[entry
])
5532 /* Otherwise, kill it. */
5533 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
5539 /* Do mark and sweep of unused sections. */
5542 elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
5544 bfd_boolean ok
= TRUE
;
5546 asection
* (*gc_mark_hook
)
5547 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
5548 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
5550 if (!get_elf_backend_data (abfd
)->can_gc_sections
5551 || info
->relocatable
5552 || info
->emitrelocations
5553 || !is_elf_hash_table (info
->hash
)
5554 || elf_hash_table (info
)->dynamic_sections_created
)
5556 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
5560 /* Apply transitive closure to the vtable entry usage info. */
5561 elf_link_hash_traverse (elf_hash_table (info
),
5562 elf_gc_propagate_vtable_entries_used
,
5567 /* Kill the vtable relocations that were not used. */
5568 elf_link_hash_traverse (elf_hash_table (info
),
5569 elf_gc_smash_unused_vtentry_relocs
,
5574 /* Grovel through relocs to find out who stays ... */
5576 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
5577 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5581 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
5584 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5586 if (o
->flags
& SEC_KEEP
)
5587 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
5592 /* ... and mark SEC_EXCLUDE for those that go. */
5593 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
5599 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
5602 elf_gc_record_vtinherit (bfd
*abfd
,
5604 struct elf_link_hash_entry
*h
,
5607 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
5608 struct elf_link_hash_entry
**search
, *child
;
5609 bfd_size_type extsymcount
;
5611 /* The sh_info field of the symtab header tells us where the
5612 external symbols start. We don't care about the local symbols at
5614 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
5615 if (!elf_bad_symtab (abfd
))
5616 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
5618 sym_hashes
= elf_sym_hashes (abfd
);
5619 sym_hashes_end
= sym_hashes
+ extsymcount
;
5621 /* Hunt down the child symbol, which is in this section at the same
5622 offset as the relocation. */
5623 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
5625 if ((child
= *search
) != NULL
5626 && (child
->root
.type
== bfd_link_hash_defined
5627 || child
->root
.type
== bfd_link_hash_defweak
)
5628 && child
->root
.u
.def
.section
== sec
5629 && child
->root
.u
.def
.value
== offset
)
5633 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
5634 bfd_archive_filename (abfd
), sec
->name
,
5635 (unsigned long) offset
);
5636 bfd_set_error (bfd_error_invalid_operation
);
5642 /* This *should* only be the absolute section. It could potentially
5643 be that someone has defined a non-global vtable though, which
5644 would be bad. It isn't worth paging in the local symbols to be
5645 sure though; that case should simply be handled by the assembler. */
5647 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
5650 child
->vtable_parent
= h
;
5655 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
5658 elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
5659 asection
*sec ATTRIBUTE_UNUSED
,
5660 struct elf_link_hash_entry
*h
,
5663 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5664 unsigned int log_file_align
= bed
->s
->log_file_align
;
5666 if (addend
>= h
->vtable_entries_size
)
5668 size_t size
, bytes
, file_align
;
5669 bfd_boolean
*ptr
= h
->vtable_entries_used
;
5671 /* While the symbol is undefined, we have to be prepared to handle
5673 file_align
= 1 << log_file_align
;
5674 if (h
->root
.type
== bfd_link_hash_undefined
)
5675 size
= addend
+ file_align
;
5681 /* Oops! We've got a reference past the defined end of
5682 the table. This is probably a bug -- shall we warn? */
5683 size
= addend
+ file_align
;
5686 size
= (size
+ file_align
- 1) & -file_align
;
5688 /* Allocate one extra entry for use as a "done" flag for the
5689 consolidation pass. */
5690 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
5694 ptr
= bfd_realloc (ptr
- 1, bytes
);
5700 oldbytes
= (((h
->vtable_entries_size
>> log_file_align
) + 1)
5701 * sizeof (bfd_boolean
));
5702 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
5706 ptr
= bfd_zmalloc (bytes
);
5711 /* And arrange for that done flag to be at index -1. */
5712 h
->vtable_entries_used
= ptr
+ 1;
5713 h
->vtable_entries_size
= size
;
5716 h
->vtable_entries_used
[addend
>> log_file_align
] = TRUE
;
5721 /* And an accompanying bit to work out final got entry offsets once
5722 we're done. Should be called from final_link. */
5725 elf_gc_common_finalize_got_offsets (bfd
*abfd
,
5726 struct bfd_link_info
*info
)
5729 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5732 if (! is_elf_hash_table (info
->hash
))
5735 /* The GOT offset is relative to the .got section, but the GOT header is
5736 put into the .got.plt section, if the backend uses it. */
5737 if (bed
->want_got_plt
)
5740 gotoff
= bed
->got_header_size
;
5742 /* Do the local .got entries first. */
5743 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
5745 bfd_signed_vma
*local_got
;
5746 bfd_size_type j
, locsymcount
;
5747 Elf_Internal_Shdr
*symtab_hdr
;
5749 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
5752 local_got
= elf_local_got_refcounts (i
);
5756 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
5757 if (elf_bad_symtab (i
))
5758 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
5760 locsymcount
= symtab_hdr
->sh_info
;
5762 for (j
= 0; j
< locsymcount
; ++j
)
5764 if (local_got
[j
] > 0)
5766 local_got
[j
] = gotoff
;
5767 gotoff
+= ARCH_SIZE
/ 8;
5770 local_got
[j
] = (bfd_vma
) -1;
5774 /* Then the global .got entries. .plt refcounts are handled by
5775 adjust_dynamic_symbol */
5776 elf_link_hash_traverse (elf_hash_table (info
),
5777 elf_gc_allocate_got_offsets
,
5782 /* We need a special top-level link routine to convert got reference counts
5783 to real got offsets. */
5786 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *offarg
)
5788 bfd_vma
*off
= offarg
;
5790 if (h
->root
.type
== bfd_link_hash_warning
)
5791 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5793 if (h
->got
.refcount
> 0)
5795 h
->got
.offset
= off
[0];
5796 off
[0] += ARCH_SIZE
/ 8;
5799 h
->got
.offset
= (bfd_vma
) -1;
5804 /* Many folk need no more in the way of final link than this, once
5805 got entry reference counting is enabled. */
5808 elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
5810 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
5813 /* Invoke the regular ELF backend linker to do all the work. */
5814 return elf_bfd_final_link (abfd
, info
);
5817 /* This function will be called though elf_link_hash_traverse to store
5818 all hash value of the exported symbols in an array. */
5821 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5823 unsigned long **valuep
= data
;
5829 if (h
->root
.type
== bfd_link_hash_warning
)
5830 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5832 /* Ignore indirect symbols. These are added by the versioning code. */
5833 if (h
->dynindx
== -1)
5836 name
= h
->root
.root
.string
;
5837 p
= strchr (name
, ELF_VER_CHR
);
5840 alc
= bfd_malloc (p
- name
+ 1);
5841 memcpy (alc
, name
, p
- name
);
5842 alc
[p
- name
] = '\0';
5846 /* Compute the hash value. */
5847 ha
= bfd_elf_hash (name
);
5849 /* Store the found hash value in the array given as the argument. */
5852 /* And store it in the struct so that we can put it in the hash table
5854 h
->elf_hash_value
= ha
;
5863 elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
5865 struct elf_reloc_cookie
*rcookie
= cookie
;
5867 if (rcookie
->bad_symtab
)
5868 rcookie
->rel
= rcookie
->rels
;
5870 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
5872 unsigned long r_symndx
;
5874 if (! rcookie
->bad_symtab
)
5875 if (rcookie
->rel
->r_offset
> offset
)
5877 if (rcookie
->rel
->r_offset
!= offset
)
5880 r_symndx
= ELF_R_SYM (rcookie
->rel
->r_info
);
5881 if (r_symndx
== SHN_UNDEF
)
5884 if (r_symndx
>= rcookie
->locsymcount
5885 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
5887 struct elf_link_hash_entry
*h
;
5889 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
5891 while (h
->root
.type
== bfd_link_hash_indirect
5892 || h
->root
.type
== bfd_link_hash_warning
)
5893 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5895 if ((h
->root
.type
== bfd_link_hash_defined
5896 || h
->root
.type
== bfd_link_hash_defweak
)
5897 && elf_discarded_section (h
->root
.u
.def
.section
))
5904 /* It's not a relocation against a global symbol,
5905 but it could be a relocation against a local
5906 symbol for a discarded section. */
5908 Elf_Internal_Sym
*isym
;
5910 /* Need to: get the symbol; get the section. */
5911 isym
= &rcookie
->locsyms
[r_symndx
];
5912 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
5914 isec
= section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
5915 if (isec
!= NULL
&& elf_discarded_section (isec
))
5924 /* Discard unneeded references to discarded sections.
5925 Returns TRUE if any section's size was changed. */
5926 /* This function assumes that the relocations are in sorted order,
5927 which is true for all known assemblers. */
5930 elf_bfd_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
5932 struct elf_reloc_cookie cookie
;
5933 asection
*stab
, *eh
;
5934 Elf_Internal_Shdr
*symtab_hdr
;
5935 const struct elf_backend_data
*bed
;
5938 bfd_boolean ret
= FALSE
;
5940 if (info
->traditional_format
5941 || !is_elf_hash_table (info
->hash
))
5944 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
5946 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
5949 bed
= get_elf_backend_data (abfd
);
5951 if ((abfd
->flags
& DYNAMIC
) != 0)
5954 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
5955 if (info
->relocatable
5957 && (eh
->_raw_size
== 0
5958 || bfd_is_abs_section (eh
->output_section
))))
5961 stab
= bfd_get_section_by_name (abfd
, ".stab");
5963 && (stab
->_raw_size
== 0
5964 || bfd_is_abs_section (stab
->output_section
)
5965 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
5970 && bed
->elf_backend_discard_info
== NULL
)
5973 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5975 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
5976 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
5977 if (cookie
.bad_symtab
)
5979 cookie
.locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
5980 cookie
.extsymoff
= 0;
5984 cookie
.locsymcount
= symtab_hdr
->sh_info
;
5985 cookie
.extsymoff
= symtab_hdr
->sh_info
;
5988 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
5989 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
5991 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
5992 cookie
.locsymcount
, 0,
5994 if (cookie
.locsyms
== NULL
)
6001 count
= stab
->reloc_count
;
6003 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
6005 if (cookie
.rels
!= NULL
)
6007 cookie
.rel
= cookie
.rels
;
6008 cookie
.relend
= cookie
.rels
;
6009 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
6010 if (_bfd_discard_section_stabs (abfd
, stab
,
6011 elf_section_data (stab
)->sec_info
,
6012 elf_reloc_symbol_deleted_p
,
6015 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
6023 count
= eh
->reloc_count
;
6025 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
6027 cookie
.rel
= cookie
.rels
;
6028 cookie
.relend
= cookie
.rels
;
6029 if (cookie
.rels
!= NULL
)
6030 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
6032 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
6033 elf_reloc_symbol_deleted_p
,
6037 if (cookie
.rels
!= NULL
6038 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
6042 if (bed
->elf_backend_discard_info
!= NULL
6043 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
6046 if (cookie
.locsyms
!= NULL
6047 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
6049 if (! info
->keep_memory
)
6050 free (cookie
.locsyms
);
6052 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
6056 if (info
->eh_frame_hdr
6057 && !info
->relocatable
6058 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
6065 elf_section_ignore_discarded_relocs (asection
*sec
)
6067 const struct elf_backend_data
*bed
;
6069 switch (sec
->sec_info_type
)
6071 case ELF_INFO_TYPE_STABS
:
6072 case ELF_INFO_TYPE_EH_FRAME
:
6078 bed
= get_elf_backend_data (sec
->owner
);
6079 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6080 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))