2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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_link_add_archive_symbols (bfd
*,
27 struct bfd_link_info
*);
28 static bfd_boolean
elf_finalize_dynstr (bfd
*, struct bfd_link_info
*);
29 static bfd_boolean
elf_collect_hash_codes (struct elf_link_hash_entry
*,
31 static bfd_boolean
elf_section_ignore_discarded_relocs (asection
*);
33 /* Given an ELF BFD, add symbols to the global hash table as
37 elf_bfd_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
39 switch (bfd_get_format (abfd
))
42 return elf_link_add_object_symbols (abfd
, info
);
44 return elf_link_add_archive_symbols (abfd
, info
);
46 bfd_set_error (bfd_error_wrong_format
);
51 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
53 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
54 Elf_Internal_Sym
*sym
)
56 /* Local symbols do not count, but target specific ones might. */
57 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
58 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
61 /* Function symbols do not count. */
62 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
65 /* If the section is undefined, then so is the symbol. */
66 if (sym
->st_shndx
== SHN_UNDEF
)
69 /* If the symbol is defined in the common section, then
70 it is a common definition and so does not count. */
71 if (sym
->st_shndx
== SHN_COMMON
)
74 /* If the symbol is in a target specific section then we
75 must rely upon the backend to tell us what it is. */
76 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
77 /* FIXME - this function is not coded yet:
79 return _bfd_is_global_symbol_definition (abfd, sym);
81 Instead for now assume that the definition is not global,
82 Even if this is wrong, at least the linker will behave
83 in the same way that it used to do. */
89 /* Search the symbol table of the archive element of the archive ABFD
90 whose archive map contains a mention of SYMDEF, and determine if
91 the symbol is defined in this element. */
93 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
95 Elf_Internal_Shdr
* hdr
;
96 bfd_size_type symcount
;
97 bfd_size_type extsymcount
;
98 bfd_size_type extsymoff
;
99 Elf_Internal_Sym
*isymbuf
;
100 Elf_Internal_Sym
*isym
;
101 Elf_Internal_Sym
*isymend
;
104 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
108 if (! bfd_check_format (abfd
, bfd_object
))
111 /* If we have already included the element containing this symbol in the
112 link then we do not need to include it again. Just claim that any symbol
113 it contains is not a definition, so that our caller will not decide to
114 (re)include this element. */
115 if (abfd
->archive_pass
)
118 /* Select the appropriate symbol table. */
119 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
120 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
122 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
124 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
126 /* The sh_info field of the symtab header tells us where the
127 external symbols start. We don't care about the local symbols. */
128 if (elf_bad_symtab (abfd
))
130 extsymcount
= symcount
;
135 extsymcount
= symcount
- hdr
->sh_info
;
136 extsymoff
= hdr
->sh_info
;
139 if (extsymcount
== 0)
142 /* Read in the symbol table. */
143 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
148 /* Scan the symbol table looking for SYMDEF. */
150 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
154 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
159 if (strcmp (name
, symdef
->name
) == 0)
161 result
= is_global_data_symbol_definition (abfd
, isym
);
171 /* Add symbols from an ELF archive file to the linker hash table. We
172 don't use _bfd_generic_link_add_archive_symbols because of a
173 problem which arises on UnixWare. The UnixWare libc.so is an
174 archive which includes an entry libc.so.1 which defines a bunch of
175 symbols. The libc.so archive also includes a number of other
176 object files, which also define symbols, some of which are the same
177 as those defined in libc.so.1. Correct linking requires that we
178 consider each object file in turn, and include it if it defines any
179 symbols we need. _bfd_generic_link_add_archive_symbols does not do
180 this; it looks through the list of undefined symbols, and includes
181 any object file which defines them. When this algorithm is used on
182 UnixWare, it winds up pulling in libc.so.1 early and defining a
183 bunch of symbols. This means that some of the other objects in the
184 archive are not included in the link, which is incorrect since they
185 precede libc.so.1 in the archive.
187 Fortunately, ELF archive handling is simpler than that done by
188 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
189 oddities. In ELF, if we find a symbol in the archive map, and the
190 symbol is currently undefined, we know that we must pull in that
193 Unfortunately, we do have to make multiple passes over the symbol
194 table until nothing further is resolved. */
197 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
200 bfd_boolean
*defined
= NULL
;
201 bfd_boolean
*included
= NULL
;
206 if (! bfd_has_map (abfd
))
208 /* An empty archive is a special case. */
209 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
211 bfd_set_error (bfd_error_no_armap
);
215 /* Keep track of all symbols we know to be already defined, and all
216 files we know to be already included. This is to speed up the
217 second and subsequent passes. */
218 c
= bfd_ardata (abfd
)->symdef_count
;
222 amt
*= sizeof (bfd_boolean
);
223 defined
= bfd_zmalloc (amt
);
224 included
= bfd_zmalloc (amt
);
225 if (defined
== NULL
|| included
== NULL
)
228 symdefs
= bfd_ardata (abfd
)->symdefs
;
241 symdefend
= symdef
+ c
;
242 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
244 struct elf_link_hash_entry
*h
;
246 struct bfd_link_hash_entry
*undefs_tail
;
249 if (defined
[i
] || included
[i
])
251 if (symdef
->file_offset
== last
)
257 h
= elf_link_hash_lookup (elf_hash_table (info
), symdef
->name
,
258 FALSE
, FALSE
, FALSE
);
265 /* If this is a default version (the name contains @@),
266 look up the symbol again with only one `@' as well
267 as without the version. The effect is that references
268 to the symbol with and without the version will be
269 matched by the default symbol in the archive. */
271 p
= strchr (symdef
->name
, ELF_VER_CHR
);
272 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
275 /* First check with only one `@'. */
276 len
= strlen (symdef
->name
);
277 copy
= bfd_alloc (abfd
, len
);
280 first
= p
- symdef
->name
+ 1;
281 memcpy (copy
, symdef
->name
, first
);
282 memcpy (copy
+ first
, symdef
->name
+ first
+ 1, len
- first
);
284 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
285 FALSE
, FALSE
, FALSE
);
289 /* We also need to check references to the symbol
290 without the version. */
292 copy
[first
- 1] = '\0';
293 h
= elf_link_hash_lookup (elf_hash_table (info
),
294 copy
, FALSE
, FALSE
, FALSE
);
297 bfd_release (abfd
, copy
);
303 if (h
->root
.type
== bfd_link_hash_common
)
305 /* We currently have a common symbol. The archive map contains
306 a reference to this symbol, so we may want to include it. We
307 only want to include it however, if this archive element
308 contains a definition of the symbol, not just another common
311 Unfortunately some archivers (including GNU ar) will put
312 declarations of common symbols into their archive maps, as
313 well as real definitions, so we cannot just go by the archive
314 map alone. Instead we must read in the element's symbol
315 table and check that to see what kind of symbol definition
317 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
320 else if (h
->root
.type
!= bfd_link_hash_undefined
)
322 if (h
->root
.type
!= bfd_link_hash_undefweak
)
327 /* We need to include this archive member. */
328 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
332 if (! bfd_check_format (element
, bfd_object
))
335 /* Doublecheck that we have not included this object
336 already--it should be impossible, but there may be
337 something wrong with the archive. */
338 if (element
->archive_pass
!= 0)
340 bfd_set_error (bfd_error_bad_value
);
343 element
->archive_pass
= 1;
345 undefs_tail
= info
->hash
->undefs_tail
;
347 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
350 if (! elf_link_add_object_symbols (element
, info
))
353 /* If there are any new undefined symbols, we need to make
354 another pass through the archive in order to see whether
355 they can be defined. FIXME: This isn't perfect, because
356 common symbols wind up on undefs_tail and because an
357 undefined symbol which is defined later on in this pass
358 does not require another pass. This isn't a bug, but it
359 does make the code less efficient than it could be. */
360 if (undefs_tail
!= info
->hash
->undefs_tail
)
363 /* Look backward to mark all symbols from this object file
364 which we have already seen in this pass. */
368 included
[mark
] = TRUE
;
373 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
375 /* We mark subsequent symbols from this object file as we go
376 on through the loop. */
377 last
= symdef
->file_offset
;
390 if (included
!= NULL
)
395 /* Add symbols from an ELF object file to the linker hash table. */
398 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
400 bfd_boolean (*add_symbol_hook
)
401 (bfd
*, struct bfd_link_info
*, const Elf_Internal_Sym
*,
402 const char **, flagword
*, asection
**, bfd_vma
*);
403 bfd_boolean (*check_relocs
)
404 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
406 Elf_Internal_Shdr
*hdr
;
407 bfd_size_type symcount
;
408 bfd_size_type extsymcount
;
409 bfd_size_type extsymoff
;
410 struct elf_link_hash_entry
**sym_hash
;
412 Elf_External_Versym
*extversym
= NULL
;
413 Elf_External_Versym
*ever
;
414 struct elf_link_hash_entry
*weaks
;
415 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
416 bfd_size_type nondeflt_vers_cnt
= 0;
417 Elf_Internal_Sym
*isymbuf
= NULL
;
418 Elf_Internal_Sym
*isym
;
419 Elf_Internal_Sym
*isymend
;
420 const struct elf_backend_data
*bed
;
421 bfd_boolean dt_needed
;
422 struct elf_link_hash_table
* hash_table
;
425 hash_table
= elf_hash_table (info
);
427 bed
= get_elf_backend_data (abfd
);
428 add_symbol_hook
= bed
->elf_add_symbol_hook
;
429 collect
= bed
->collect
;
431 if ((abfd
->flags
& DYNAMIC
) == 0)
437 /* You can't use -r against a dynamic object. Also, there's no
438 hope of using a dynamic object which does not exactly match
439 the format of the output file. */
440 if (info
->relocatable
|| info
->hash
->creator
!= abfd
->xvec
)
442 bfd_set_error (bfd_error_invalid_operation
);
447 /* As a GNU extension, any input sections which are named
448 .gnu.warning.SYMBOL are treated as warning symbols for the given
449 symbol. This differs from .gnu.warning sections, which generate
450 warnings when they are included in an output file. */
451 if (info
->executable
)
455 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
459 name
= bfd_get_section_name (abfd
, s
);
460 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
464 bfd_size_type prefix_len
;
465 const char * gnu_warning_prefix
= _("warning: ");
467 name
+= sizeof ".gnu.warning." - 1;
469 /* If this is a shared object, then look up the symbol
470 in the hash table. If it is there, and it is already
471 been defined, then we will not be using the entry
472 from this shared object, so we don't need to warn.
473 FIXME: If we see the definition in a regular object
474 later on, we will warn, but we shouldn't. The only
475 fix is to keep track of what warnings we are supposed
476 to emit, and then handle them all at the end of the
478 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
480 struct elf_link_hash_entry
*h
;
482 h
= elf_link_hash_lookup (hash_table
, name
,
485 /* FIXME: What about bfd_link_hash_common? */
487 && (h
->root
.type
== bfd_link_hash_defined
488 || h
->root
.type
== bfd_link_hash_defweak
))
490 /* We don't want to issue this warning. Clobber
491 the section size so that the warning does not
492 get copied into the output file. */
498 sz
= bfd_section_size (abfd
, s
);
499 prefix_len
= strlen (gnu_warning_prefix
);
500 msg
= bfd_alloc (abfd
, prefix_len
+ sz
+ 1);
504 strcpy (msg
, gnu_warning_prefix
);
505 if (! bfd_get_section_contents (abfd
, s
, msg
+ prefix_len
, 0, sz
))
508 msg
[prefix_len
+ sz
] = '\0';
510 if (! (_bfd_generic_link_add_one_symbol
511 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
512 FALSE
, collect
, NULL
)))
515 if (! info
->relocatable
)
517 /* Clobber the section size so that the warning does
518 not get copied into the output file. */
528 /* If we are creating a shared library, create all the dynamic
529 sections immediately. We need to attach them to something,
530 so we attach them to this BFD, provided it is the right
531 format. FIXME: If there are no input BFD's of the same
532 format as the output, we can't make a shared library. */
534 && is_elf_hash_table (info
)
535 && ! hash_table
->dynamic_sections_created
536 && abfd
->xvec
== info
->hash
->creator
)
538 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
542 else if (! is_elf_hash_table (info
))
547 bfd_boolean add_needed
;
549 bfd_size_type oldsize
;
550 bfd_size_type strindex
;
551 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
553 /* ld --just-symbols and dynamic objects don't mix very well.
554 Test for --just-symbols by looking at info set up by
555 _bfd_elf_link_just_syms. */
556 if ((s
= abfd
->sections
) != NULL
557 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
560 /* Find the name to use in a DT_NEEDED entry that refers to this
561 object. If the object has a DT_SONAME entry, we use it.
562 Otherwise, if the generic linker stuck something in
563 elf_dt_name, we use that. Otherwise, we just use the file
564 name. If the generic linker put a null string into
565 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
566 there is a DT_SONAME entry. */
568 name
= bfd_get_filename (abfd
);
569 if (elf_dt_name (abfd
) != NULL
)
571 name
= elf_dt_name (abfd
);
574 if (elf_dt_soname (abfd
) != NULL
)
580 s
= bfd_get_section_by_name (abfd
, ".dynamic");
583 Elf_External_Dyn
*dynbuf
= NULL
;
584 Elf_External_Dyn
*extdyn
;
585 Elf_External_Dyn
*extdynend
;
587 unsigned long shlink
;
589 dynbuf
= bfd_malloc (s
->_raw_size
);
593 if (! bfd_get_section_contents (abfd
, s
, dynbuf
, 0, s
->_raw_size
))
596 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
599 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
602 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
603 for (; extdyn
< extdynend
; extdyn
++)
605 Elf_Internal_Dyn dyn
;
607 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
608 if (dyn
.d_tag
== DT_SONAME
)
610 unsigned int tagv
= dyn
.d_un
.d_val
;
611 name
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
615 if (dyn
.d_tag
== DT_NEEDED
)
617 struct bfd_link_needed_list
*n
, **pn
;
619 unsigned int tagv
= dyn
.d_un
.d_val
;
621 amt
= sizeof (struct bfd_link_needed_list
);
622 n
= bfd_alloc (abfd
, amt
);
623 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
624 if (n
== NULL
|| fnm
== NULL
)
626 amt
= strlen (fnm
) + 1;
627 anm
= bfd_alloc (abfd
, amt
);
630 memcpy (anm
, fnm
, amt
);
634 for (pn
= & hash_table
->needed
;
640 if (dyn
.d_tag
== DT_RUNPATH
)
642 struct bfd_link_needed_list
*n
, **pn
;
644 unsigned int tagv
= dyn
.d_un
.d_val
;
646 amt
= sizeof (struct bfd_link_needed_list
);
647 n
= bfd_alloc (abfd
, amt
);
648 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
649 if (n
== NULL
|| fnm
== NULL
)
651 amt
= strlen (fnm
) + 1;
652 anm
= bfd_alloc (abfd
, amt
);
655 memcpy (anm
, fnm
, amt
);
665 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
666 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
668 struct bfd_link_needed_list
*n
, **pn
;
670 unsigned int tagv
= dyn
.d_un
.d_val
;
672 amt
= sizeof (struct bfd_link_needed_list
);
673 n
= bfd_alloc (abfd
, amt
);
674 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
675 if (n
== NULL
|| fnm
== NULL
)
677 amt
= strlen (fnm
) + 1;
678 anm
= bfd_alloc (abfd
, amt
);
685 memcpy (anm
, fnm
, amt
);
700 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
701 frees all more recently bfd_alloc'd blocks as well. */
707 struct bfd_link_needed_list
**pn
;
708 for (pn
= & hash_table
->runpath
;
715 /* We do not want to include any of the sections in a dynamic
716 object in the output file. We hack by simply clobbering the
717 list of sections in the BFD. This could be handled more
718 cleanly by, say, a new section flag; the existing
719 SEC_NEVER_LOAD flag is not the one we want, because that one
720 still implies that the section takes up space in the output
722 bfd_section_list_clear (abfd
);
724 /* If this is the first dynamic object found in the link, create
725 the special sections required for dynamic linking. */
726 if (! hash_table
->dynamic_sections_created
)
727 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
732 /* Add a DT_NEEDED entry for this dynamic object. */
733 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
734 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, name
, FALSE
);
735 if (strindex
== (bfd_size_type
) -1)
738 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
741 Elf_External_Dyn
*dyncon
, *dynconend
;
743 /* The hash table size did not change, which means that
744 the dynamic object name was already entered. If we
745 have already included this dynamic object in the
746 link, just ignore it. There is no reason to include
747 a particular dynamic object more than once. */
748 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
749 BFD_ASSERT (sdyn
!= NULL
);
751 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
752 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
754 for (; dyncon
< dynconend
; dyncon
++)
756 Elf_Internal_Dyn dyn
;
758 elf_swap_dyn_in (hash_table
->dynobj
, dyncon
, & dyn
);
759 if (dyn
.d_tag
== DT_NEEDED
760 && dyn
.d_un
.d_val
== strindex
)
762 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
768 if (! elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
772 /* Save the SONAME, if there is one, because sometimes the
773 linker emulation code will need to know it. */
775 name
= basename (bfd_get_filename (abfd
));
776 elf_dt_name (abfd
) = name
;
779 /* If this is a dynamic object, we always link against the .dynsym
780 symbol table, not the .symtab symbol table. The dynamic linker
781 will only see the .dynsym symbol table, so there is no reason to
782 look at .symtab for a dynamic object. */
784 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
785 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
787 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
789 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
791 /* The sh_info field of the symtab header tells us where the
792 external symbols start. We don't care about the local symbols at
794 if (elf_bad_symtab (abfd
))
796 extsymcount
= symcount
;
801 extsymcount
= symcount
- hdr
->sh_info
;
802 extsymoff
= hdr
->sh_info
;
806 if (extsymcount
!= 0)
808 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
813 /* We store a pointer to the hash table entry for each external
815 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
816 sym_hash
= bfd_alloc (abfd
, amt
);
817 if (sym_hash
== NULL
)
819 elf_sym_hashes (abfd
) = sym_hash
;
824 /* Read in any version definitions. */
825 if (! _bfd_elf_slurp_version_tables (abfd
))
828 /* Read in the symbol versions, but don't bother to convert them
829 to internal format. */
830 if (elf_dynversym (abfd
) != 0)
832 Elf_Internal_Shdr
*versymhdr
;
834 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
835 extversym
= bfd_malloc (versymhdr
->sh_size
);
836 if (extversym
== NULL
)
838 amt
= versymhdr
->sh_size
;
839 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
840 || bfd_bread (extversym
, amt
, abfd
) != amt
)
841 goto error_free_vers
;
847 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
848 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
850 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
857 struct elf_link_hash_entry
*h
;
858 bfd_boolean definition
;
859 bfd_boolean size_change_ok
;
860 bfd_boolean type_change_ok
;
861 bfd_boolean new_weakdef
;
862 bfd_boolean override
;
863 unsigned int old_alignment
;
868 flags
= BSF_NO_FLAGS
;
870 value
= isym
->st_value
;
873 bind
= ELF_ST_BIND (isym
->st_info
);
874 if (bind
== STB_LOCAL
)
876 /* This should be impossible, since ELF requires that all
877 global symbols follow all local symbols, and that sh_info
878 point to the first global symbol. Unfortunatealy, Irix 5
882 else if (bind
== STB_GLOBAL
)
884 if (isym
->st_shndx
!= SHN_UNDEF
885 && isym
->st_shndx
!= SHN_COMMON
)
888 else if (bind
== STB_WEAK
)
892 /* Leave it up to the processor backend. */
895 if (isym
->st_shndx
== SHN_UNDEF
)
896 sec
= bfd_und_section_ptr
;
897 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
899 sec
= section_from_elf_index (abfd
, isym
->st_shndx
);
901 sec
= bfd_abs_section_ptr
;
902 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
905 else if (isym
->st_shndx
== SHN_ABS
)
906 sec
= bfd_abs_section_ptr
;
907 else if (isym
->st_shndx
== SHN_COMMON
)
909 sec
= bfd_com_section_ptr
;
910 /* What ELF calls the size we call the value. What ELF
911 calls the value we call the alignment. */
912 value
= isym
->st_size
;
916 /* Leave it up to the processor backend. */
919 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
922 goto error_free_vers
;
924 if (isym
->st_shndx
== SHN_COMMON
925 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
927 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
931 tcomm
= bfd_make_section (abfd
, ".tcommon");
933 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
936 | SEC_THREAD_LOCAL
)))
937 goto error_free_vers
;
941 else if (add_symbol_hook
)
943 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
945 goto error_free_vers
;
947 /* The hook function sets the name to NULL if this symbol
948 should be skipped for some reason. */
953 /* Sanity check that all possibilities were handled. */
956 bfd_set_error (bfd_error_bad_value
);
957 goto error_free_vers
;
960 if (bfd_is_und_section (sec
)
961 || bfd_is_com_section (sec
))
966 size_change_ok
= FALSE
;
967 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
971 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
973 Elf_Internal_Versym iver
;
974 unsigned int vernum
= 0;
979 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
980 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
982 /* If this is a hidden symbol, or if it is not version
983 1, we append the version name to the symbol name.
984 However, we do not modify a non-hidden absolute
985 symbol, because it might be the version symbol
986 itself. FIXME: What if it isn't? */
987 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
988 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
991 size_t namelen
, verlen
, newlen
;
994 if (isym
->st_shndx
!= SHN_UNDEF
)
996 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
998 (*_bfd_error_handler
)
999 (_("%s: %s: invalid version %u (max %d)"),
1000 bfd_archive_filename (abfd
), name
, vernum
,
1001 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1002 bfd_set_error (bfd_error_bad_value
);
1003 goto error_free_vers
;
1005 else if (vernum
> 1)
1007 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1013 /* We cannot simply test for the number of
1014 entries in the VERNEED section since the
1015 numbers for the needed versions do not start
1017 Elf_Internal_Verneed
*t
;
1020 for (t
= elf_tdata (abfd
)->verref
;
1024 Elf_Internal_Vernaux
*a
;
1026 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1028 if (a
->vna_other
== vernum
)
1030 verstr
= a
->vna_nodename
;
1039 (*_bfd_error_handler
)
1040 (_("%s: %s: invalid needed version %d"),
1041 bfd_archive_filename (abfd
), name
, vernum
);
1042 bfd_set_error (bfd_error_bad_value
);
1043 goto error_free_vers
;
1047 namelen
= strlen (name
);
1048 verlen
= strlen (verstr
);
1049 newlen
= namelen
+ verlen
+ 2;
1050 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1051 && isym
->st_shndx
!= SHN_UNDEF
)
1054 newname
= bfd_alloc (abfd
, newlen
);
1055 if (newname
== NULL
)
1056 goto error_free_vers
;
1057 memcpy (newname
, name
, namelen
);
1058 p
= newname
+ namelen
;
1060 /* If this is a defined non-hidden version symbol,
1061 we add another @ to the name. This indicates the
1062 default version of the symbol. */
1063 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1064 && isym
->st_shndx
!= SHN_UNDEF
)
1066 memcpy (p
, verstr
, verlen
+ 1);
1072 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
1073 sym_hash
, &skip
, &override
,
1074 &type_change_ok
, &size_change_ok
,
1076 goto error_free_vers
;
1085 while (h
->root
.type
== bfd_link_hash_indirect
1086 || h
->root
.type
== bfd_link_hash_warning
)
1087 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1089 /* Remember the old alignment if this is a common symbol, so
1090 that we don't reduce the alignment later on. We can't
1091 check later, because _bfd_generic_link_add_one_symbol
1092 will set a default for the alignment which we want to
1093 override. We also remember the old bfd where the existing
1094 definition comes from. */
1095 switch (h
->root
.type
)
1100 case bfd_link_hash_defined
:
1101 case bfd_link_hash_defweak
:
1102 old_bfd
= h
->root
.u
.def
.section
->owner
;
1105 case bfd_link_hash_common
:
1106 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
1107 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1111 if (elf_tdata (abfd
)->verdef
!= NULL
1115 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1118 if (! (_bfd_generic_link_add_one_symbol
1119 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
1120 (struct bfd_link_hash_entry
**) sym_hash
)))
1121 goto error_free_vers
;
1124 while (h
->root
.type
== bfd_link_hash_indirect
1125 || h
->root
.type
== bfd_link_hash_warning
)
1126 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1129 new_weakdef
= FALSE
;
1132 && (flags
& BSF_WEAK
) != 0
1133 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
1134 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1135 && h
->weakdef
== NULL
)
1137 /* Keep a list of all weak defined non function symbols from
1138 a dynamic object, using the weakdef field. Later in this
1139 function we will set the weakdef field to the correct
1140 value. We only put non-function symbols from dynamic
1141 objects on this list, because that happens to be the only
1142 time we need to know the normal symbol corresponding to a
1143 weak symbol, and the information is time consuming to
1144 figure out. If the weakdef field is not already NULL,
1145 then this symbol was already defined by some previous
1146 dynamic object, and we will be using that previous
1147 definition anyhow. */
1154 /* Set the alignment of a common symbol. */
1155 if (isym
->st_shndx
== SHN_COMMON
1156 && h
->root
.type
== bfd_link_hash_common
)
1160 align
= bfd_log2 (isym
->st_value
);
1161 if (align
> old_alignment
1162 /* Permit an alignment power of zero if an alignment of one
1163 is specified and no other alignments have been specified. */
1164 || (isym
->st_value
== 1 && old_alignment
== 0))
1165 h
->root
.u
.c
.p
->alignment_power
= align
;
1167 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
1170 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1176 /* Check the alignment when a common symbol is involved. This
1177 can change when a common symbol is overriden by a normal
1178 definition or a common symbol is ignored due to the old
1179 normal definition. We need to make sure the maximum
1180 alignment is maintained. */
1181 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
1182 && h
->root
.type
!= bfd_link_hash_common
)
1184 unsigned int common_align
;
1185 unsigned int normal_align
;
1186 unsigned int symbol_align
;
1190 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
1191 if (h
->root
.u
.def
.section
->owner
!= NULL
1192 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
1194 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
1195 if (normal_align
> symbol_align
)
1196 normal_align
= symbol_align
;
1199 normal_align
= symbol_align
;
1203 common_align
= old_alignment
;
1204 common_bfd
= old_bfd
;
1209 common_align
= bfd_log2 (isym
->st_value
);
1211 normal_bfd
= old_bfd
;
1214 if (normal_align
< common_align
)
1215 (*_bfd_error_handler
)
1216 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
1219 bfd_archive_filename (normal_bfd
),
1221 bfd_archive_filename (common_bfd
));
1224 /* Remember the symbol size and type. */
1225 if (isym
->st_size
!= 0
1226 && (definition
|| h
->size
== 0))
1228 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
1229 (*_bfd_error_handler
)
1230 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
1231 name
, (unsigned long) h
->size
,
1232 bfd_archive_filename (old_bfd
),
1233 (unsigned long) isym
->st_size
,
1234 bfd_archive_filename (abfd
));
1236 h
->size
= isym
->st_size
;
1239 /* If this is a common symbol, then we always want H->SIZE
1240 to be the size of the common symbol. The code just above
1241 won't fix the size if a common symbol becomes larger. We
1242 don't warn about a size change here, because that is
1243 covered by --warn-common. */
1244 if (h
->root
.type
== bfd_link_hash_common
)
1245 h
->size
= h
->root
.u
.c
.size
;
1247 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
1248 && (definition
|| h
->type
== STT_NOTYPE
))
1250 if (h
->type
!= STT_NOTYPE
1251 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
1252 && ! type_change_ok
)
1253 (*_bfd_error_handler
)
1254 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1255 name
, h
->type
, ELF_ST_TYPE (isym
->st_info
),
1256 bfd_archive_filename (abfd
));
1258 h
->type
= ELF_ST_TYPE (isym
->st_info
);
1261 /* If st_other has a processor-specific meaning, specific
1262 code might be needed here. We never merge the visibility
1263 attribute with the one from a dynamic object. */
1264 if (bed
->elf_backend_merge_symbol_attribute
)
1265 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
1268 if (isym
->st_other
!= 0 && !dynamic
)
1270 unsigned char hvis
, symvis
, other
, nvis
;
1272 /* Take the balance of OTHER from the definition. */
1273 other
= (definition
? isym
->st_other
: h
->other
);
1274 other
&= ~ ELF_ST_VISIBILITY (-1);
1276 /* Combine visibilities, using the most constraining one. */
1277 hvis
= ELF_ST_VISIBILITY (h
->other
);
1278 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
1284 nvis
= hvis
< symvis
? hvis
: symvis
;
1286 h
->other
= other
| nvis
;
1289 /* Set a flag in the hash table entry indicating the type of
1290 reference or definition we just found. Keep a count of
1291 the number of dynamic symbols we find. A dynamic symbol
1292 is one which is referenced or defined by both a regular
1293 object and a shared object. */
1294 old_flags
= h
->elf_link_hash_flags
;
1300 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
1301 if (bind
!= STB_WEAK
)
1302 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
1305 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
1306 if (! info
->executable
1307 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1308 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
1314 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
1316 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
1317 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
1318 | ELF_LINK_HASH_REF_REGULAR
)) != 0
1319 || (h
->weakdef
!= NULL
1321 && h
->weakdef
->dynindx
!= -1))
1325 h
->elf_link_hash_flags
|= new_flag
;
1327 /* Check to see if we need to add an indirect symbol for
1328 the default name. */
1329 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
1330 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
1331 &sec
, &value
, &dynsym
,
1332 override
, dt_needed
))
1333 goto error_free_vers
;
1335 if (definition
&& !dynamic
)
1337 char *p
= strchr (name
, ELF_VER_CHR
);
1338 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
1340 /* Queue non-default versions so that .symver x, x@FOO
1341 aliases can be checked. */
1342 if (! nondeflt_vers
)
1344 amt
= (isymend
- isym
+ 1)
1345 * sizeof (struct elf_link_hash_entry
*);
1346 nondeflt_vers
= bfd_malloc (amt
);
1348 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
1352 if (dynsym
&& h
->dynindx
== -1)
1354 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1355 goto error_free_vers
;
1356 if (h
->weakdef
!= NULL
1358 && h
->weakdef
->dynindx
== -1)
1360 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
1361 goto error_free_vers
;
1364 else if (dynsym
&& h
->dynindx
!= -1)
1365 /* If the symbol already has a dynamic index, but
1366 visibility says it should not be visible, turn it into
1368 switch (ELF_ST_VISIBILITY (h
->other
))
1372 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1376 if (dt_needed
&& definition
1377 && (h
->elf_link_hash_flags
1378 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1380 bfd_size_type oldsize
;
1381 bfd_size_type strindex
;
1383 if (! is_elf_hash_table (info
))
1384 goto error_free_vers
;
1386 /* The symbol from a DT_NEEDED object is referenced from
1387 the regular object to create a dynamic executable. We
1388 have to make sure there is a DT_NEEDED entry for it. */
1391 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
1392 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
,
1393 elf_dt_soname (abfd
), FALSE
);
1394 if (strindex
== (bfd_size_type
) -1)
1395 goto error_free_vers
;
1397 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
1400 Elf_External_Dyn
*dyncon
, *dynconend
;
1402 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
,
1404 BFD_ASSERT (sdyn
!= NULL
);
1406 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1407 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1409 for (; dyncon
< dynconend
; dyncon
++)
1411 Elf_Internal_Dyn dyn
;
1413 elf_swap_dyn_in (hash_table
->dynobj
,
1415 BFD_ASSERT (dyn
.d_tag
!= DT_NEEDED
||
1416 dyn
.d_un
.d_val
!= strindex
);
1420 if (! elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
1421 goto error_free_vers
;
1426 /* Now that all the symbols from this input file are created, handle
1427 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
1428 if (nondeflt_vers
!= NULL
)
1430 bfd_size_type cnt
, symidx
;
1432 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
1434 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
1435 char *shortname
, *p
;
1437 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1439 || (h
->root
.type
!= bfd_link_hash_defined
1440 && h
->root
.type
!= bfd_link_hash_defweak
))
1443 amt
= p
- h
->root
.root
.string
;
1444 shortname
= bfd_malloc (amt
+ 1);
1445 memcpy (shortname
, h
->root
.root
.string
, amt
);
1446 shortname
[amt
] = '\0';
1448 hi
= (struct elf_link_hash_entry
*)
1449 bfd_link_hash_lookup (info
->hash
, shortname
,
1450 FALSE
, FALSE
, FALSE
);
1452 && hi
->root
.type
== h
->root
.type
1453 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
1454 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
1456 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1457 hi
->root
.type
= bfd_link_hash_indirect
;
1458 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
1459 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1460 sym_hash
= elf_sym_hashes (abfd
);
1462 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
1463 if (sym_hash
[symidx
] == hi
)
1465 sym_hash
[symidx
] = h
;
1471 free (nondeflt_vers
);
1472 nondeflt_vers
= NULL
;
1475 if (extversym
!= NULL
)
1481 if (isymbuf
!= NULL
)
1485 /* Now set the weakdefs field correctly for all the weak defined
1486 symbols we found. The only way to do this is to search all the
1487 symbols. Since we only need the information for non functions in
1488 dynamic objects, that's the only time we actually put anything on
1489 the list WEAKS. We need this information so that if a regular
1490 object refers to a symbol defined weakly in a dynamic object, the
1491 real symbol in the dynamic object is also put in the dynamic
1492 symbols; we also must arrange for both symbols to point to the
1493 same memory location. We could handle the general case of symbol
1494 aliasing, but a general symbol alias can only be generated in
1495 assembler code, handling it correctly would be very time
1496 consuming, and other ELF linkers don't handle general aliasing
1498 while (weaks
!= NULL
)
1500 struct elf_link_hash_entry
*hlook
;
1503 struct elf_link_hash_entry
**hpp
;
1504 struct elf_link_hash_entry
**hppend
;
1507 weaks
= hlook
->weakdef
;
1508 hlook
->weakdef
= NULL
;
1510 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
1511 || hlook
->root
.type
== bfd_link_hash_defweak
1512 || hlook
->root
.type
== bfd_link_hash_common
1513 || hlook
->root
.type
== bfd_link_hash_indirect
);
1514 slook
= hlook
->root
.u
.def
.section
;
1515 vlook
= hlook
->root
.u
.def
.value
;
1517 hpp
= elf_sym_hashes (abfd
);
1518 hppend
= hpp
+ extsymcount
;
1519 for (; hpp
< hppend
; hpp
++)
1521 struct elf_link_hash_entry
*h
;
1524 if (h
!= NULL
&& h
!= hlook
1525 && h
->root
.type
== bfd_link_hash_defined
1526 && h
->root
.u
.def
.section
== slook
1527 && h
->root
.u
.def
.value
== vlook
)
1531 /* If the weak definition is in the list of dynamic
1532 symbols, make sure the real definition is put there
1534 if (hlook
->dynindx
!= -1
1535 && h
->dynindx
== -1)
1537 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1541 /* If the real definition is in the list of dynamic
1542 symbols, make sure the weak definition is put there
1543 as well. If we don't do this, then the dynamic
1544 loader might not merge the entries for the real
1545 definition and the weak definition. */
1546 if (h
->dynindx
!= -1
1547 && hlook
->dynindx
== -1)
1549 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
1557 /* If this object is the same format as the output object, and it is
1558 not a shared library, then let the backend look through the
1561 This is required to build global offset table entries and to
1562 arrange for dynamic relocs. It is not required for the
1563 particular common case of linking non PIC code, even when linking
1564 against shared libraries, but unfortunately there is no way of
1565 knowing whether an object file has been compiled PIC or not.
1566 Looking through the relocs is not particularly time consuming.
1567 The problem is that we must either (1) keep the relocs in memory,
1568 which causes the linker to require additional runtime memory or
1569 (2) read the relocs twice from the input file, which wastes time.
1570 This would be a good case for using mmap.
1572 I have no idea how to handle linking PIC code into a file of a
1573 different format. It probably can't be done. */
1574 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
1576 && abfd
->xvec
== info
->hash
->creator
1577 && check_relocs
!= NULL
)
1581 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1583 Elf_Internal_Rela
*internal_relocs
;
1586 if ((o
->flags
& SEC_RELOC
) == 0
1587 || o
->reloc_count
== 0
1588 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
1589 && (o
->flags
& SEC_DEBUGGING
) != 0)
1590 || bfd_is_abs_section (o
->output_section
))
1593 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
1595 if (internal_relocs
== NULL
)
1598 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
1600 if (elf_section_data (o
)->relocs
!= internal_relocs
)
1601 free (internal_relocs
);
1608 /* If this is a non-traditional link, try to optimize the handling
1609 of the .stab/.stabstr sections. */
1611 && ! info
->traditional_format
1612 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1613 && is_elf_hash_table (info
)
1614 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
1618 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
1619 if (stabstr
!= NULL
)
1621 bfd_size_type string_offset
= 0;
1624 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
1625 if (strncmp (".stab", stab
->name
, 5) == 0
1626 && (!stab
->name
[5] ||
1627 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
1628 && (stab
->flags
& SEC_MERGE
) == 0
1629 && !bfd_is_abs_section (stab
->output_section
))
1631 struct bfd_elf_section_data
*secdata
;
1633 secdata
= elf_section_data (stab
);
1634 if (! _bfd_link_section_stabs (abfd
,
1635 & hash_table
->stab_info
,
1640 if (secdata
->sec_info
)
1641 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
1646 if (! info
->relocatable
&& ! dynamic
1647 && is_elf_hash_table (info
))
1651 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
1652 if ((s
->flags
& SEC_MERGE
) != 0
1653 && !bfd_is_abs_section (s
->output_section
))
1655 struct bfd_elf_section_data
*secdata
;
1657 secdata
= elf_section_data (s
);
1658 if (! _bfd_merge_section (abfd
,
1659 & hash_table
->merge_info
,
1660 s
, &secdata
->sec_info
))
1662 else if (secdata
->sec_info
)
1663 s
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
1667 if (is_elf_hash_table (info
))
1669 /* Add this bfd to the loaded list. */
1670 struct elf_link_loaded_list
*n
;
1672 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
1676 n
->next
= hash_table
->loaded
;
1677 hash_table
->loaded
= n
;
1683 if (nondeflt_vers
!= NULL
)
1684 free (nondeflt_vers
);
1685 if (extversym
!= NULL
)
1688 if (isymbuf
!= NULL
)
1694 /* Add an entry to the .dynamic table. */
1697 elf_add_dynamic_entry (struct bfd_link_info
*info
, bfd_vma tag
, bfd_vma val
)
1699 Elf_Internal_Dyn dyn
;
1702 bfd_size_type newsize
;
1703 bfd_byte
*newcontents
;
1705 if (! is_elf_hash_table (info
))
1708 dynobj
= elf_hash_table (info
)->dynobj
;
1710 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
1711 BFD_ASSERT (s
!= NULL
);
1713 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
1714 newcontents
= bfd_realloc (s
->contents
, newsize
);
1715 if (newcontents
== NULL
)
1719 dyn
.d_un
.d_val
= val
;
1720 elf_swap_dyn_out (dynobj
, &dyn
,
1721 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
1723 s
->_raw_size
= newsize
;
1724 s
->contents
= newcontents
;
1729 /* Array used to determine the number of hash table buckets to use
1730 based on the number of symbols there are. If there are fewer than
1731 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
1732 fewer than 37 we use 17 buckets, and so forth. We never use more
1733 than 32771 buckets. */
1735 static const size_t elf_buckets
[] =
1737 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
1741 /* Compute bucket count for hashing table. We do not use a static set
1742 of possible tables sizes anymore. Instead we determine for all
1743 possible reasonable sizes of the table the outcome (i.e., the
1744 number of collisions etc) and choose the best solution. The
1745 weighting functions are not too simple to allow the table to grow
1746 without bounds. Instead one of the weighting factors is the size.
1747 Therefore the result is always a good payoff between few collisions
1748 (= short chain lengths) and table size. */
1750 compute_bucket_count (struct bfd_link_info
*info
)
1752 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
1753 size_t best_size
= 0;
1754 unsigned long int *hashcodes
;
1755 unsigned long int *hashcodesp
;
1756 unsigned long int i
;
1759 /* Compute the hash values for all exported symbols. At the same
1760 time store the values in an array so that we could use them for
1763 amt
*= sizeof (unsigned long int);
1764 hashcodes
= bfd_malloc (amt
);
1765 if (hashcodes
== NULL
)
1767 hashcodesp
= hashcodes
;
1769 /* Put all hash values in HASHCODES. */
1770 elf_link_hash_traverse (elf_hash_table (info
),
1771 elf_collect_hash_codes
, &hashcodesp
);
1773 /* We have a problem here. The following code to optimize the table
1774 size requires an integer type with more the 32 bits. If
1775 BFD_HOST_U_64_BIT is set we know about such a type. */
1776 #ifdef BFD_HOST_U_64_BIT
1779 unsigned long int nsyms
= hashcodesp
- hashcodes
;
1782 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
1783 unsigned long int *counts
;
1785 /* Possible optimization parameters: if we have NSYMS symbols we say
1786 that the hashing table must at least have NSYMS/4 and at most
1788 minsize
= nsyms
/ 4;
1791 best_size
= maxsize
= nsyms
* 2;
1793 /* Create array where we count the collisions in. We must use bfd_malloc
1794 since the size could be large. */
1796 amt
*= sizeof (unsigned long int);
1797 counts
= bfd_malloc (amt
);
1804 /* Compute the "optimal" size for the hash table. The criteria is a
1805 minimal chain length. The minor criteria is (of course) the size
1807 for (i
= minsize
; i
< maxsize
; ++i
)
1809 /* Walk through the array of hashcodes and count the collisions. */
1810 BFD_HOST_U_64_BIT max
;
1811 unsigned long int j
;
1812 unsigned long int fact
;
1814 memset (counts
, '\0', i
* sizeof (unsigned long int));
1816 /* Determine how often each hash bucket is used. */
1817 for (j
= 0; j
< nsyms
; ++j
)
1818 ++counts
[hashcodes
[j
] % i
];
1820 /* For the weight function we need some information about the
1821 pagesize on the target. This is information need not be 100%
1822 accurate. Since this information is not available (so far) we
1823 define it here to a reasonable default value. If it is crucial
1824 to have a better value some day simply define this value. */
1825 # ifndef BFD_TARGET_PAGESIZE
1826 # define BFD_TARGET_PAGESIZE (4096)
1829 /* We in any case need 2 + NSYMS entries for the size values and
1831 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
1834 /* Variant 1: optimize for short chains. We add the squares
1835 of all the chain lengths (which favous many small chain
1836 over a few long chains). */
1837 for (j
= 0; j
< i
; ++j
)
1838 max
+= counts
[j
] * counts
[j
];
1840 /* This adds penalties for the overall size of the table. */
1841 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
1844 /* Variant 2: Optimize a lot more for small table. Here we
1845 also add squares of the size but we also add penalties for
1846 empty slots (the +1 term). */
1847 for (j
= 0; j
< i
; ++j
)
1848 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
1850 /* The overall size of the table is considered, but not as
1851 strong as in variant 1, where it is squared. */
1852 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
1856 /* Compare with current best results. */
1857 if (max
< best_chlen
)
1867 #endif /* defined (BFD_HOST_U_64_BIT) */
1869 /* This is the fallback solution if no 64bit type is available or if we
1870 are not supposed to spend much time on optimizations. We select the
1871 bucket count using a fixed set of numbers. */
1872 for (i
= 0; elf_buckets
[i
] != 0; i
++)
1874 best_size
= elf_buckets
[i
];
1875 if (dynsymcount
< elf_buckets
[i
+ 1])
1880 /* Free the arrays we needed. */
1886 /* Set up the sizes and contents of the ELF dynamic sections. This is
1887 called by the ELF linker emulation before_allocation routine. We
1888 must set the sizes of the sections before the linker sets the
1889 addresses of the various sections. */
1892 NAME(bfd_elf
,size_dynamic_sections
) (bfd
*output_bfd
,
1895 const char *filter_shlib
,
1896 const char * const *auxiliary_filters
,
1897 struct bfd_link_info
*info
,
1898 asection
**sinterpptr
,
1899 struct bfd_elf_version_tree
*verdefs
)
1901 bfd_size_type soname_indx
;
1903 const struct elf_backend_data
*bed
;
1904 struct elf_assign_sym_version_info asvinfo
;
1908 soname_indx
= (bfd_size_type
) -1;
1910 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1913 if (! is_elf_hash_table (info
))
1916 if (info
->execstack
)
1917 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
1918 else if (info
->noexecstack
)
1919 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
1923 asection
*notesec
= NULL
;
1926 for (inputobj
= info
->input_bfds
;
1928 inputobj
= inputobj
->link_next
)
1932 if (inputobj
->flags
& DYNAMIC
)
1934 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
1937 if (s
->flags
& SEC_CODE
)
1946 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
1947 if (exec
&& info
->relocatable
1948 && notesec
->output_section
!= bfd_abs_section_ptr
)
1949 notesec
->output_section
->flags
|= SEC_CODE
;
1953 /* Any syms created from now on start with -1 in
1954 got.refcount/offset and plt.refcount/offset. */
1955 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
1957 /* The backend may have to create some sections regardless of whether
1958 we're dynamic or not. */
1959 bed
= get_elf_backend_data (output_bfd
);
1960 if (bed
->elf_backend_always_size_sections
1961 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
1964 dynobj
= elf_hash_table (info
)->dynobj
;
1966 /* If there were no dynamic objects in the link, there is nothing to
1971 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
1974 if (elf_hash_table (info
)->dynamic_sections_created
)
1976 struct elf_info_failed eif
;
1977 struct elf_link_hash_entry
*h
;
1979 struct bfd_elf_version_tree
*t
;
1980 struct bfd_elf_version_expr
*d
;
1981 bfd_boolean all_defined
;
1983 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
1984 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
1988 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
1990 if (soname_indx
== (bfd_size_type
) -1
1991 || ! elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
1997 if (! elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
1999 info
->flags
|= DF_SYMBOLIC
;
2006 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
2008 if (info
->new_dtags
)
2009 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
2010 if (indx
== (bfd_size_type
) -1
2011 || ! elf_add_dynamic_entry (info
, DT_RPATH
, indx
)
2013 && ! elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
)))
2017 if (filter_shlib
!= NULL
)
2021 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2022 filter_shlib
, TRUE
);
2023 if (indx
== (bfd_size_type
) -1
2024 || ! elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
2028 if (auxiliary_filters
!= NULL
)
2030 const char * const *p
;
2032 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
2036 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2038 if (indx
== (bfd_size_type
) -1
2039 || ! elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
2045 eif
.verdefs
= verdefs
;
2048 /* If we are supposed to export all symbols into the dynamic symbol
2049 table (this is not the normal case), then do so. */
2050 if (info
->export_dynamic
)
2052 elf_link_hash_traverse (elf_hash_table (info
),
2053 _bfd_elf_export_symbol
,
2059 /* Make all global versions with definiton. */
2060 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2061 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
2062 if (!d
->symver
&& d
->symbol
)
2064 const char *verstr
, *name
;
2065 size_t namelen
, verlen
, newlen
;
2067 struct elf_link_hash_entry
*newh
;
2070 namelen
= strlen (name
);
2072 verlen
= strlen (verstr
);
2073 newlen
= namelen
+ verlen
+ 3;
2075 newname
= bfd_malloc (newlen
);
2076 if (newname
== NULL
)
2078 memcpy (newname
, name
, namelen
);
2080 /* Check the hidden versioned definition. */
2081 p
= newname
+ namelen
;
2083 memcpy (p
, verstr
, verlen
+ 1);
2084 newh
= elf_link_hash_lookup (elf_hash_table (info
),
2085 newname
, FALSE
, FALSE
,
2088 || (newh
->root
.type
!= bfd_link_hash_defined
2089 && newh
->root
.type
!= bfd_link_hash_defweak
))
2091 /* Check the default versioned definition. */
2093 memcpy (p
, verstr
, verlen
+ 1);
2094 newh
= elf_link_hash_lookup (elf_hash_table (info
),
2095 newname
, FALSE
, FALSE
,
2100 /* Mark this version if there is a definition and it is
2101 not defined in a shared object. */
2103 && ((newh
->elf_link_hash_flags
2104 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)
2105 && (newh
->root
.type
== bfd_link_hash_defined
2106 || newh
->root
.type
== bfd_link_hash_defweak
))
2110 /* Attach all the symbols to their version information. */
2111 asvinfo
.output_bfd
= output_bfd
;
2112 asvinfo
.info
= info
;
2113 asvinfo
.verdefs
= verdefs
;
2114 asvinfo
.failed
= FALSE
;
2116 elf_link_hash_traverse (elf_hash_table (info
),
2117 _bfd_elf_link_assign_sym_version
,
2122 if (!info
->allow_undefined_version
)
2124 /* Check if all global versions have a definiton. */
2126 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2127 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
2128 if (!d
->symver
&& !d
->script
)
2130 (*_bfd_error_handler
)
2131 (_("%s: undefined version: %s"),
2132 d
->pattern
, t
->name
);
2133 all_defined
= FALSE
;
2138 bfd_set_error (bfd_error_bad_value
);
2143 /* Find all symbols which were defined in a dynamic object and make
2144 the backend pick a reasonable value for them. */
2145 elf_link_hash_traverse (elf_hash_table (info
),
2146 _bfd_elf_adjust_dynamic_symbol
,
2151 /* Add some entries to the .dynamic section. We fill in some of the
2152 values later, in elf_bfd_final_link, but we must add the entries
2153 now so that we know the final size of the .dynamic section. */
2155 /* If there are initialization and/or finalization functions to
2156 call then add the corresponding DT_INIT/DT_FINI entries. */
2157 h
= (info
->init_function
2158 ? elf_link_hash_lookup (elf_hash_table (info
),
2159 info
->init_function
, FALSE
,
2163 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2164 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2166 if (! elf_add_dynamic_entry (info
, DT_INIT
, 0))
2169 h
= (info
->fini_function
2170 ? elf_link_hash_lookup (elf_hash_table (info
),
2171 info
->fini_function
, FALSE
,
2175 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2176 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2178 if (! elf_add_dynamic_entry (info
, DT_FINI
, 0))
2182 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
2184 /* DT_PREINIT_ARRAY is not allowed in shared library. */
2185 if (! info
->executable
)
2190 for (sub
= info
->input_bfds
; sub
!= NULL
;
2191 sub
= sub
->link_next
)
2192 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2193 if (elf_section_data (o
)->this_hdr
.sh_type
2194 == SHT_PREINIT_ARRAY
)
2196 (*_bfd_error_handler
)
2197 (_("%s: .preinit_array section is not allowed in DSO"),
2198 bfd_archive_filename (sub
));
2202 bfd_set_error (bfd_error_nonrepresentable_section
);
2206 if (!elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
2207 || !elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
2210 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
2212 if (!elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
2213 || !elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
2216 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
2218 if (!elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
2219 || !elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
2223 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
2224 /* If .dynstr is excluded from the link, we don't want any of
2225 these tags. Strictly, we should be checking each section
2226 individually; This quick check covers for the case where
2227 someone does a /DISCARD/ : { *(*) }. */
2228 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
2230 bfd_size_type strsize
;
2232 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
2233 if (! elf_add_dynamic_entry (info
, DT_HASH
, 0)
2234 || ! elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
2235 || ! elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
2236 || ! elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
2237 || ! elf_add_dynamic_entry (info
, DT_SYMENT
,
2238 sizeof (Elf_External_Sym
)))
2243 /* The backend must work out the sizes of all the other dynamic
2245 if (bed
->elf_backend_size_dynamic_sections
2246 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
2249 if (elf_hash_table (info
)->dynamic_sections_created
)
2251 bfd_size_type dynsymcount
;
2253 size_t bucketcount
= 0;
2254 size_t hash_entry_size
;
2255 unsigned int dtagcount
;
2257 /* Set up the version definition section. */
2258 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2259 BFD_ASSERT (s
!= NULL
);
2261 /* We may have created additional version definitions if we are
2262 just linking a regular application. */
2263 verdefs
= asvinfo
.verdefs
;
2265 /* Skip anonymous version tag. */
2266 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
2267 verdefs
= verdefs
->next
;
2269 if (verdefs
== NULL
)
2270 _bfd_strip_section_from_output (info
, s
);
2275 struct bfd_elf_version_tree
*t
;
2277 Elf_Internal_Verdef def
;
2278 Elf_Internal_Verdaux defaux
;
2283 /* Make space for the base version. */
2284 size
+= sizeof (Elf_External_Verdef
);
2285 size
+= sizeof (Elf_External_Verdaux
);
2288 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2290 struct bfd_elf_version_deps
*n
;
2292 size
+= sizeof (Elf_External_Verdef
);
2293 size
+= sizeof (Elf_External_Verdaux
);
2296 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2297 size
+= sizeof (Elf_External_Verdaux
);
2300 s
->_raw_size
= size
;
2301 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
2302 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2305 /* Fill in the version definition section. */
2309 def
.vd_version
= VER_DEF_CURRENT
;
2310 def
.vd_flags
= VER_FLG_BASE
;
2313 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2314 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2315 + sizeof (Elf_External_Verdaux
));
2317 if (soname_indx
!= (bfd_size_type
) -1)
2319 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
2321 def
.vd_hash
= bfd_elf_hash (soname
);
2322 defaux
.vda_name
= soname_indx
;
2329 name
= basename (output_bfd
->filename
);
2330 def
.vd_hash
= bfd_elf_hash (name
);
2331 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2333 if (indx
== (bfd_size_type
) -1)
2335 defaux
.vda_name
= indx
;
2337 defaux
.vda_next
= 0;
2339 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2340 (Elf_External_Verdef
*) p
);
2341 p
+= sizeof (Elf_External_Verdef
);
2342 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2343 (Elf_External_Verdaux
*) p
);
2344 p
+= sizeof (Elf_External_Verdaux
);
2346 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2349 struct bfd_elf_version_deps
*n
;
2350 struct elf_link_hash_entry
*h
;
2351 struct bfd_link_hash_entry
*bh
;
2354 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2357 /* Add a symbol representing this version. */
2359 if (! (_bfd_generic_link_add_one_symbol
2360 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
2362 get_elf_backend_data (dynobj
)->collect
, &bh
)))
2364 h
= (struct elf_link_hash_entry
*) bh
;
2365 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
2366 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2367 h
->type
= STT_OBJECT
;
2368 h
->verinfo
.vertree
= t
;
2370 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2373 def
.vd_version
= VER_DEF_CURRENT
;
2375 if (t
->globals
.list
== NULL
&& t
->locals
.list
== NULL
&& ! t
->used
)
2376 def
.vd_flags
|= VER_FLG_WEAK
;
2377 def
.vd_ndx
= t
->vernum
+ 1;
2378 def
.vd_cnt
= cdeps
+ 1;
2379 def
.vd_hash
= bfd_elf_hash (t
->name
);
2380 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2381 if (t
->next
!= NULL
)
2382 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2383 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
2387 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2388 (Elf_External_Verdef
*) p
);
2389 p
+= sizeof (Elf_External_Verdef
);
2391 defaux
.vda_name
= h
->dynstr_index
;
2392 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
2394 if (t
->deps
== NULL
)
2395 defaux
.vda_next
= 0;
2397 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2398 t
->name_indx
= defaux
.vda_name
;
2400 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2401 (Elf_External_Verdaux
*) p
);
2402 p
+= sizeof (Elf_External_Verdaux
);
2404 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2406 if (n
->version_needed
== NULL
)
2408 /* This can happen if there was an error in the
2410 defaux
.vda_name
= 0;
2414 defaux
.vda_name
= n
->version_needed
->name_indx
;
2415 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
2418 if (n
->next
== NULL
)
2419 defaux
.vda_next
= 0;
2421 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2423 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2424 (Elf_External_Verdaux
*) p
);
2425 p
+= sizeof (Elf_External_Verdaux
);
2429 if (! elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
2430 || ! elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
2433 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
2436 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
2438 if (! elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
2444 if (info
->executable
)
2445 info
->flags_1
&= ~ (DF_1_INITFIRST
2448 if (! elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
2452 /* Work out the size of the version reference section. */
2454 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2455 BFD_ASSERT (s
!= NULL
);
2457 struct elf_find_verdep_info sinfo
;
2459 sinfo
.output_bfd
= output_bfd
;
2461 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
2462 if (sinfo
.vers
== 0)
2464 sinfo
.failed
= FALSE
;
2466 elf_link_hash_traverse (elf_hash_table (info
),
2467 _bfd_elf_link_find_version_dependencies
,
2470 if (elf_tdata (output_bfd
)->verref
== NULL
)
2471 _bfd_strip_section_from_output (info
, s
);
2474 Elf_Internal_Verneed
*t
;
2479 /* Build the version definition section. */
2482 for (t
= elf_tdata (output_bfd
)->verref
;
2486 Elf_Internal_Vernaux
*a
;
2488 size
+= sizeof (Elf_External_Verneed
);
2490 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2491 size
+= sizeof (Elf_External_Vernaux
);
2494 s
->_raw_size
= size
;
2495 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
2496 if (s
->contents
== NULL
)
2500 for (t
= elf_tdata (output_bfd
)->verref
;
2505 Elf_Internal_Vernaux
*a
;
2509 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2512 t
->vn_version
= VER_NEED_CURRENT
;
2514 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2515 elf_dt_name (t
->vn_bfd
) != NULL
2516 ? elf_dt_name (t
->vn_bfd
)
2517 : basename (t
->vn_bfd
->filename
),
2519 if (indx
== (bfd_size_type
) -1)
2522 t
->vn_aux
= sizeof (Elf_External_Verneed
);
2523 if (t
->vn_nextref
== NULL
)
2526 t
->vn_next
= (sizeof (Elf_External_Verneed
)
2527 + caux
* sizeof (Elf_External_Vernaux
));
2529 _bfd_elf_swap_verneed_out (output_bfd
, t
,
2530 (Elf_External_Verneed
*) p
);
2531 p
+= sizeof (Elf_External_Verneed
);
2533 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2535 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
2536 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2537 a
->vna_nodename
, FALSE
);
2538 if (indx
== (bfd_size_type
) -1)
2541 if (a
->vna_nextptr
== NULL
)
2544 a
->vna_next
= sizeof (Elf_External_Vernaux
);
2546 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
2547 (Elf_External_Vernaux
*) p
);
2548 p
+= sizeof (Elf_External_Vernaux
);
2552 if (! elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
2553 || ! elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
2556 elf_tdata (output_bfd
)->cverrefs
= crefs
;
2560 /* Assign dynsym indicies. In a shared library we generate a
2561 section symbol for each output section, which come first.
2562 Next come all of the back-end allocated local dynamic syms,
2563 followed by the rest of the global symbols. */
2565 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
2567 /* Work out the size of the symbol version section. */
2568 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
2569 BFD_ASSERT (s
!= NULL
);
2570 if (dynsymcount
== 0
2571 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
2573 _bfd_strip_section_from_output (info
, s
);
2574 /* The DYNSYMCOUNT might have changed if we were going to
2575 output a dynamic symbol table entry for S. */
2576 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
2580 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
2581 s
->contents
= bfd_zalloc (output_bfd
, s
->_raw_size
);
2582 if (s
->contents
== NULL
)
2585 if (! elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
2589 /* Set the size of the .dynsym and .hash sections. We counted
2590 the number of dynamic symbols in elf_link_add_object_symbols.
2591 We will build the contents of .dynsym and .hash when we build
2592 the final symbol table, because until then we do not know the
2593 correct value to give the symbols. We built the .dynstr
2594 section as we went along in elf_link_add_object_symbols. */
2595 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
2596 BFD_ASSERT (s
!= NULL
);
2597 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
2598 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
2599 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2602 if (dynsymcount
!= 0)
2604 Elf_Internal_Sym isym
;
2606 /* The first entry in .dynsym is a dummy symbol. */
2613 elf_swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
2616 /* Compute the size of the hashing table. As a side effect this
2617 computes the hash values for all the names we export. */
2618 bucketcount
= compute_bucket_count (info
);
2620 s
= bfd_get_section_by_name (dynobj
, ".hash");
2621 BFD_ASSERT (s
!= NULL
);
2622 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
2623 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
2624 s
->contents
= bfd_zalloc (output_bfd
, s
->_raw_size
);
2625 if (s
->contents
== NULL
)
2628 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
2629 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
2630 s
->contents
+ hash_entry_size
);
2632 elf_hash_table (info
)->bucketcount
= bucketcount
;
2634 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
2635 BFD_ASSERT (s
!= NULL
);
2637 elf_finalize_dynstr (output_bfd
, info
);
2639 s
->_raw_size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
2641 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
2642 if (! elf_add_dynamic_entry (info
, DT_NULL
, 0))
2649 /* This function is used to adjust offsets into .dynstr for
2650 dynamic symbols. This is called via elf_link_hash_traverse. */
2653 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2655 struct elf_strtab_hash
*dynstr
= data
;
2657 if (h
->root
.type
== bfd_link_hash_warning
)
2658 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2660 if (h
->dynindx
!= -1)
2661 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2665 /* Assign string offsets in .dynstr, update all structures referencing
2669 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2671 struct elf_link_local_dynamic_entry
*entry
;
2672 struct elf_strtab_hash
*dynstr
= elf_hash_table (info
)->dynstr
;
2673 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
2676 Elf_External_Dyn
*dyncon
, *dynconend
;
2678 _bfd_elf_strtab_finalize (dynstr
);
2679 size
= _bfd_elf_strtab_size (dynstr
);
2681 /* Update all .dynamic entries referencing .dynstr strings. */
2682 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2683 BFD_ASSERT (sdyn
!= NULL
);
2685 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
2686 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
2688 for (; dyncon
< dynconend
; dyncon
++)
2690 Elf_Internal_Dyn dyn
;
2692 elf_swap_dyn_in (dynobj
, dyncon
, & dyn
);
2696 dyn
.d_un
.d_val
= size
;
2697 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
2705 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2706 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
2713 /* Now update local dynamic symbols. */
2714 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
2715 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2716 entry
->isym
.st_name
);
2718 /* And the rest of dynamic symbols. */
2719 elf_link_hash_traverse (elf_hash_table (info
),
2720 elf_adjust_dynstr_offsets
, dynstr
);
2722 /* Adjust version definitions. */
2723 if (elf_tdata (output_bfd
)->cverdefs
)
2728 Elf_Internal_Verdef def
;
2729 Elf_Internal_Verdaux defaux
;
2731 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2732 p
= (bfd_byte
*) s
->contents
;
2735 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
2737 p
+= sizeof (Elf_External_Verdef
);
2738 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2740 _bfd_elf_swap_verdaux_in (output_bfd
,
2741 (Elf_External_Verdaux
*) p
, &defaux
);
2742 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2744 _bfd_elf_swap_verdaux_out (output_bfd
,
2745 &defaux
, (Elf_External_Verdaux
*) p
);
2746 p
+= sizeof (Elf_External_Verdaux
);
2749 while (def
.vd_next
);
2752 /* Adjust version references. */
2753 if (elf_tdata (output_bfd
)->verref
)
2758 Elf_Internal_Verneed need
;
2759 Elf_Internal_Vernaux needaux
;
2761 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2762 p
= (bfd_byte
*) s
->contents
;
2765 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
2767 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
2768 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
2769 (Elf_External_Verneed
*) p
);
2770 p
+= sizeof (Elf_External_Verneed
);
2771 for (i
= 0; i
< need
.vn_cnt
; ++i
)
2773 _bfd_elf_swap_vernaux_in (output_bfd
,
2774 (Elf_External_Vernaux
*) p
, &needaux
);
2775 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
2777 _bfd_elf_swap_vernaux_out (output_bfd
,
2779 (Elf_External_Vernaux
*) p
);
2780 p
+= sizeof (Elf_External_Vernaux
);
2783 while (need
.vn_next
);
2789 /* Final phase of ELF linker. */
2791 /* A structure we use to avoid passing large numbers of arguments. */
2793 struct elf_final_link_info
2795 /* General link information. */
2796 struct bfd_link_info
*info
;
2799 /* Symbol string table. */
2800 struct bfd_strtab_hash
*symstrtab
;
2801 /* .dynsym section. */
2802 asection
*dynsym_sec
;
2803 /* .hash section. */
2805 /* symbol version section (.gnu.version). */
2806 asection
*symver_sec
;
2807 /* Buffer large enough to hold contents of any section. */
2809 /* Buffer large enough to hold external relocs of any section. */
2810 void *external_relocs
;
2811 /* Buffer large enough to hold internal relocs of any section. */
2812 Elf_Internal_Rela
*internal_relocs
;
2813 /* Buffer large enough to hold external local symbols of any input
2815 Elf_External_Sym
*external_syms
;
2816 /* And a buffer for symbol section indices. */
2817 Elf_External_Sym_Shndx
*locsym_shndx
;
2818 /* Buffer large enough to hold internal local symbols of any input
2820 Elf_Internal_Sym
*internal_syms
;
2821 /* Array large enough to hold a symbol index for each local symbol
2822 of any input BFD. */
2824 /* Array large enough to hold a section pointer for each local
2825 symbol of any input BFD. */
2826 asection
**sections
;
2827 /* Buffer to hold swapped out symbols. */
2828 Elf_External_Sym
*symbuf
;
2829 /* And one for symbol section indices. */
2830 Elf_External_Sym_Shndx
*symshndxbuf
;
2831 /* Number of swapped out symbols in buffer. */
2832 size_t symbuf_count
;
2833 /* Number of symbols which fit in symbuf. */
2835 /* And same for symshndxbuf. */
2836 size_t shndxbuf_size
;
2839 static bfd_boolean elf_link_output_sym
2840 (struct elf_final_link_info
*, const char *, Elf_Internal_Sym
*, asection
*);
2841 static bfd_boolean elf_link_flush_output_syms
2842 (struct elf_final_link_info
*);
2843 static bfd_boolean elf_link_output_extsym
2844 (struct elf_link_hash_entry
*, void *);
2845 static bfd_boolean elf_link_input_bfd
2846 (struct elf_final_link_info
*, bfd
*);
2847 static bfd_boolean elf_reloc_link_order
2848 (bfd
*, struct bfd_link_info
*, asection
*, struct bfd_link_order
*);
2850 /* This struct is used to pass information to elf_link_output_extsym. */
2852 struct elf_outext_info
2855 bfd_boolean localsyms
;
2856 struct elf_final_link_info
*finfo
;
2859 /* When performing a relocatable link, the input relocations are
2860 preserved. But, if they reference global symbols, the indices
2861 referenced must be updated. Update all the relocations in
2862 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
2865 elf_link_adjust_relocs (bfd
*abfd
,
2866 Elf_Internal_Shdr
*rel_hdr
,
2868 struct elf_link_hash_entry
**rel_hash
)
2871 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2873 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2874 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2876 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2878 swap_in
= bed
->s
->swap_reloc_in
;
2879 swap_out
= bed
->s
->swap_reloc_out
;
2881 else if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
))
2883 swap_in
= bed
->s
->swap_reloca_in
;
2884 swap_out
= bed
->s
->swap_reloca_out
;
2889 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
2892 erela
= rel_hdr
->contents
;
2893 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
2895 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
2898 if (*rel_hash
== NULL
)
2901 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
2903 (*swap_in
) (abfd
, erela
, irela
);
2904 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
2905 irela
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
2906 ELF_R_TYPE (irela
[j
].r_info
));
2907 (*swap_out
) (abfd
, irela
, erela
);
2911 struct elf_link_sort_rela
2914 enum elf_reloc_type_class type
;
2915 /* We use this as an array of size int_rels_per_ext_rel. */
2916 Elf_Internal_Rela rela
[1];
2920 elf_link_sort_cmp1 (const void *A
, const void *B
)
2922 const struct elf_link_sort_rela
*a
= A
;
2923 const struct elf_link_sort_rela
*b
= B
;
2924 int relativea
, relativeb
;
2926 relativea
= a
->type
== reloc_class_relative
;
2927 relativeb
= b
->type
== reloc_class_relative
;
2929 if (relativea
< relativeb
)
2931 if (relativea
> relativeb
)
2933 if (ELF_R_SYM (a
->rela
->r_info
) < ELF_R_SYM (b
->rela
->r_info
))
2935 if (ELF_R_SYM (a
->rela
->r_info
) > ELF_R_SYM (b
->rela
->r_info
))
2937 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
2939 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
2945 elf_link_sort_cmp2 (const void *A
, const void *B
)
2947 const struct elf_link_sort_rela
*a
= A
;
2948 const struct elf_link_sort_rela
*b
= B
;
2951 if (a
->offset
< b
->offset
)
2953 if (a
->offset
> b
->offset
)
2955 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
2956 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
2961 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
2963 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
2969 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
2972 bfd_size_type count
, size
;
2973 size_t i
, ret
, sort_elt
, ext_size
;
2974 bfd_byte
*sort
, *s_non_relative
, *p
;
2975 struct elf_link_sort_rela
*sq
;
2976 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2977 int i2e
= bed
->s
->int_rels_per_ext_rel
;
2978 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2979 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2980 struct bfd_link_order
*lo
;
2982 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
2983 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
2985 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
2986 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
2988 ext_size
= sizeof (Elf_External_Rel
);
2989 swap_in
= bed
->s
->swap_reloc_in
;
2990 swap_out
= bed
->s
->swap_reloc_out
;
2994 ext_size
= sizeof (Elf_External_Rela
);
2995 swap_in
= bed
->s
->swap_reloca_in
;
2996 swap_out
= bed
->s
->swap_reloca_out
;
2998 count
= reldyn
->_raw_size
/ ext_size
;
3001 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
3002 if (lo
->type
== bfd_indirect_link_order
)
3004 asection
*o
= lo
->u
.indirect
.section
;
3005 size
+= o
->_raw_size
;
3008 if (size
!= reldyn
->_raw_size
)
3011 sort_elt
= (sizeof (struct elf_link_sort_rela
)
3012 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
3013 sort
= bfd_zmalloc (sort_elt
* count
);
3016 (*info
->callbacks
->warning
)
3017 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
3021 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
3022 if (lo
->type
== bfd_indirect_link_order
)
3024 bfd_byte
*erel
, *erelend
;
3025 asection
*o
= lo
->u
.indirect
.section
;
3028 erelend
= o
->contents
+ o
->_raw_size
;
3029 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
3030 while (erel
< erelend
)
3032 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
3033 (*swap_in
) (abfd
, erel
, s
->rela
);
3034 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
3040 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
3042 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
3044 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
3045 if (s
->type
!= reloc_class_relative
)
3051 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
3052 for (; i
< count
; i
++, p
+= sort_elt
)
3054 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
3055 if (ELF_R_SYM (sp
->rela
->r_info
) != ELF_R_SYM (sq
->rela
->r_info
))
3057 sp
->offset
= sq
->rela
->r_offset
;
3060 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
3062 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
3063 if (lo
->type
== bfd_indirect_link_order
)
3065 bfd_byte
*erel
, *erelend
;
3066 asection
*o
= lo
->u
.indirect
.section
;
3069 erelend
= o
->contents
+ o
->_raw_size
;
3070 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
3071 while (erel
< erelend
)
3073 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
3074 (*swap_out
) (abfd
, s
->rela
, erel
);
3085 /* Do the final step of an ELF link. */
3088 elf_bfd_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
3090 bfd_boolean dynamic
;
3091 bfd_boolean emit_relocs
;
3093 struct elf_final_link_info finfo
;
3094 register asection
*o
;
3095 register struct bfd_link_order
*p
;
3097 bfd_size_type max_contents_size
;
3098 bfd_size_type max_external_reloc_size
;
3099 bfd_size_type max_internal_reloc_count
;
3100 bfd_size_type max_sym_count
;
3101 bfd_size_type max_sym_shndx_count
;
3103 Elf_Internal_Sym elfsym
;
3105 Elf_Internal_Shdr
*symtab_hdr
;
3106 Elf_Internal_Shdr
*symtab_shndx_hdr
;
3107 Elf_Internal_Shdr
*symstrtab_hdr
;
3108 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3109 struct elf_outext_info eoinfo
;
3111 size_t relativecount
= 0;
3112 asection
*reldyn
= 0;
3115 if (! is_elf_hash_table (info
))
3119 abfd
->flags
|= DYNAMIC
;
3121 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
3122 dynobj
= elf_hash_table (info
)->dynobj
;
3124 emit_relocs
= (info
->relocatable
3125 || info
->emitrelocations
3126 || bed
->elf_backend_emit_relocs
);
3129 finfo
.output_bfd
= abfd
;
3130 finfo
.symstrtab
= elf_stringtab_init ();
3131 if (finfo
.symstrtab
== NULL
)
3136 finfo
.dynsym_sec
= NULL
;
3137 finfo
.hash_sec
= NULL
;
3138 finfo
.symver_sec
= NULL
;
3142 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
3143 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
3144 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
3145 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3146 /* Note that it is OK if symver_sec is NULL. */
3149 finfo
.contents
= NULL
;
3150 finfo
.external_relocs
= NULL
;
3151 finfo
.internal_relocs
= NULL
;
3152 finfo
.external_syms
= NULL
;
3153 finfo
.locsym_shndx
= NULL
;
3154 finfo
.internal_syms
= NULL
;
3155 finfo
.indices
= NULL
;
3156 finfo
.sections
= NULL
;
3157 finfo
.symbuf
= NULL
;
3158 finfo
.symshndxbuf
= NULL
;
3159 finfo
.symbuf_count
= 0;
3160 finfo
.shndxbuf_size
= 0;
3162 /* Count up the number of relocations we will output for each output
3163 section, so that we know the sizes of the reloc sections. We
3164 also figure out some maximum sizes. */
3165 max_contents_size
= 0;
3166 max_external_reloc_size
= 0;
3167 max_internal_reloc_count
= 0;
3169 max_sym_shndx_count
= 0;
3171 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3173 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
3176 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3178 unsigned int reloc_count
= 0;
3179 struct bfd_elf_section_data
*esdi
= NULL
;
3180 unsigned int *rel_count1
;
3182 if (p
->type
== bfd_section_reloc_link_order
3183 || p
->type
== bfd_symbol_reloc_link_order
)
3185 else if (p
->type
== bfd_indirect_link_order
)
3189 sec
= p
->u
.indirect
.section
;
3190 esdi
= elf_section_data (sec
);
3192 /* Mark all sections which are to be included in the
3193 link. This will normally be every section. We need
3194 to do this so that we can identify any sections which
3195 the linker has decided to not include. */
3196 sec
->linker_mark
= TRUE
;
3198 if (sec
->flags
& SEC_MERGE
)
3201 if (info
->relocatable
|| info
->emitrelocations
)
3202 reloc_count
= sec
->reloc_count
;
3203 else if (bed
->elf_backend_count_relocs
)
3205 Elf_Internal_Rela
* relocs
;
3207 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
3210 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
3212 if (elf_section_data (o
)->relocs
!= relocs
)
3216 if (sec
->_raw_size
> max_contents_size
)
3217 max_contents_size
= sec
->_raw_size
;
3218 if (sec
->_cooked_size
> max_contents_size
)
3219 max_contents_size
= sec
->_cooked_size
;
3221 /* We are interested in just local symbols, not all
3223 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
3224 && (sec
->owner
->flags
& DYNAMIC
) == 0)
3228 if (elf_bad_symtab (sec
->owner
))
3229 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
3230 / sizeof (Elf_External_Sym
));
3232 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
3234 if (sym_count
> max_sym_count
)
3235 max_sym_count
= sym_count
;
3237 if (sym_count
> max_sym_shndx_count
3238 && elf_symtab_shndx (sec
->owner
) != 0)
3239 max_sym_shndx_count
= sym_count
;
3241 if ((sec
->flags
& SEC_RELOC
) != 0)
3245 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
3246 if (ext_size
> max_external_reloc_size
)
3247 max_external_reloc_size
= ext_size
;
3248 if (sec
->reloc_count
> max_internal_reloc_count
)
3249 max_internal_reloc_count
= sec
->reloc_count
;
3254 if (reloc_count
== 0)
3257 o
->reloc_count
+= reloc_count
;
3259 /* MIPS may have a mix of REL and RELA relocs on sections.
3260 To support this curious ABI we keep reloc counts in
3261 elf_section_data too. We must be careful to add the
3262 relocations from the input section to the right output
3263 count. FIXME: Get rid of one count. We have
3264 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
3265 rel_count1
= &esdo
->rel_count
;
3268 bfd_boolean same_size
;
3269 bfd_size_type entsize1
;
3271 entsize1
= esdi
->rel_hdr
.sh_entsize
;
3272 BFD_ASSERT (entsize1
== sizeof (Elf_External_Rel
)
3273 || entsize1
== sizeof (Elf_External_Rela
));
3274 same_size
= (!o
->use_rela_p
3275 == (entsize1
== sizeof (Elf_External_Rel
)));
3278 rel_count1
= &esdo
->rel_count2
;
3280 if (esdi
->rel_hdr2
!= NULL
)
3282 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
3283 unsigned int alt_count
;
3284 unsigned int *rel_count2
;
3286 BFD_ASSERT (entsize2
!= entsize1
3287 && (entsize2
== sizeof (Elf_External_Rel
)
3288 || entsize2
== sizeof (Elf_External_Rela
)));
3290 rel_count2
= &esdo
->rel_count2
;
3292 rel_count2
= &esdo
->rel_count
;
3294 /* The following is probably too simplistic if the
3295 backend counts output relocs unusually. */
3296 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
3297 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
3298 *rel_count2
+= alt_count
;
3299 reloc_count
-= alt_count
;
3302 *rel_count1
+= reloc_count
;
3305 if (o
->reloc_count
> 0)
3306 o
->flags
|= SEC_RELOC
;
3309 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3310 set it (this is probably a bug) and if it is set
3311 assign_section_numbers will create a reloc section. */
3312 o
->flags
&=~ SEC_RELOC
;
3315 /* If the SEC_ALLOC flag is not set, force the section VMA to
3316 zero. This is done in elf_fake_sections as well, but forcing
3317 the VMA to 0 here will ensure that relocs against these
3318 sections are handled correctly. */
3319 if ((o
->flags
& SEC_ALLOC
) == 0
3320 && ! o
->user_set_vma
)
3324 if (! info
->relocatable
&& merged
)
3325 elf_link_hash_traverse (elf_hash_table (info
),
3326 _bfd_elf_link_sec_merge_syms
, abfd
);
3328 /* Figure out the file positions for everything but the symbol table
3329 and the relocs. We set symcount to force assign_section_numbers
3330 to create a symbol table. */
3331 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
3332 BFD_ASSERT (! abfd
->output_has_begun
);
3333 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
3336 /* That created the reloc sections. Set their sizes, and assign
3337 them file positions, and allocate some buffers. */
3338 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3340 if ((o
->flags
& SEC_RELOC
) != 0)
3342 if (!(_bfd_elf_link_size_reloc_section
3343 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
3346 if (elf_section_data (o
)->rel_hdr2
3347 && !(_bfd_elf_link_size_reloc_section
3348 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
3352 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
3353 to count upwards while actually outputting the relocations. */
3354 elf_section_data (o
)->rel_count
= 0;
3355 elf_section_data (o
)->rel_count2
= 0;
3358 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3360 /* We have now assigned file positions for all the sections except
3361 .symtab and .strtab. We start the .symtab section at the current
3362 file position, and write directly to it. We build the .strtab
3363 section in memory. */
3364 bfd_get_symcount (abfd
) = 0;
3365 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3366 /* sh_name is set in prep_headers. */
3367 symtab_hdr
->sh_type
= SHT_SYMTAB
;
3368 /* sh_flags, sh_addr and sh_size all start off zero. */
3369 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
3370 /* sh_link is set in assign_section_numbers. */
3371 /* sh_info is set below. */
3372 /* sh_offset is set just below. */
3373 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
3375 off
= elf_tdata (abfd
)->next_file_pos
;
3376 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
3378 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3379 incorrect. We do not yet know the size of the .symtab section.
3380 We correct next_file_pos below, after we do know the size. */
3382 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3383 continuously seeking to the right position in the file. */
3384 if (! info
->keep_memory
|| max_sym_count
< 20)
3385 finfo
.symbuf_size
= 20;
3387 finfo
.symbuf_size
= max_sym_count
;
3388 amt
= finfo
.symbuf_size
;
3389 amt
*= sizeof (Elf_External_Sym
);
3390 finfo
.symbuf
= bfd_malloc (amt
);
3391 if (finfo
.symbuf
== NULL
)
3393 if (elf_numsections (abfd
) > SHN_LORESERVE
)
3395 /* Wild guess at number of output symbols. realloc'd as needed. */
3396 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
3397 finfo
.shndxbuf_size
= amt
;
3398 amt
*= sizeof (Elf_External_Sym_Shndx
);
3399 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
3400 if (finfo
.symshndxbuf
== NULL
)
3404 /* Start writing out the symbol table. The first symbol is always a
3406 if (info
->strip
!= strip_all
3409 elfsym
.st_value
= 0;
3412 elfsym
.st_other
= 0;
3413 elfsym
.st_shndx
= SHN_UNDEF
;
3414 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
))
3419 /* Some standard ELF linkers do this, but we don't because it causes
3420 bootstrap comparison failures. */
3421 /* Output a file symbol for the output file as the second symbol.
3422 We output this even if we are discarding local symbols, although
3423 I'm not sure if this is correct. */
3424 elfsym
.st_value
= 0;
3426 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
3427 elfsym
.st_other
= 0;
3428 elfsym
.st_shndx
= SHN_ABS
;
3429 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
3430 &elfsym
, bfd_abs_section_ptr
))
3434 /* Output a symbol for each section. We output these even if we are
3435 discarding local symbols, since they are used for relocs. These
3436 symbols have no names. We store the index of each one in the
3437 index field of the section, so that we can find it again when
3438 outputting relocs. */
3439 if (info
->strip
!= strip_all
3443 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
3444 elfsym
.st_other
= 0;
3445 for (i
= 1; i
< elf_numsections (abfd
); i
++)
3447 o
= section_from_elf_index (abfd
, i
);
3449 o
->target_index
= bfd_get_symcount (abfd
);
3450 elfsym
.st_shndx
= i
;
3451 if (info
->relocatable
|| o
== NULL
)
3452 elfsym
.st_value
= 0;
3454 elfsym
.st_value
= o
->vma
;
3455 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
))
3457 if (i
== SHN_LORESERVE
- 1)
3458 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3462 /* Allocate some memory to hold information read in from the input
3464 if (max_contents_size
!= 0)
3466 finfo
.contents
= bfd_malloc (max_contents_size
);
3467 if (finfo
.contents
== NULL
)
3471 if (max_external_reloc_size
!= 0)
3473 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
3474 if (finfo
.external_relocs
== NULL
)
3478 if (max_internal_reloc_count
!= 0)
3480 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
3481 amt
*= sizeof (Elf_Internal_Rela
);
3482 finfo
.internal_relocs
= bfd_malloc (amt
);
3483 if (finfo
.internal_relocs
== NULL
)
3487 if (max_sym_count
!= 0)
3489 amt
= max_sym_count
* sizeof (Elf_External_Sym
);
3490 finfo
.external_syms
= bfd_malloc (amt
);
3491 if (finfo
.external_syms
== NULL
)
3494 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
3495 finfo
.internal_syms
= bfd_malloc (amt
);
3496 if (finfo
.internal_syms
== NULL
)
3499 amt
= max_sym_count
* sizeof (long);
3500 finfo
.indices
= bfd_malloc (amt
);
3501 if (finfo
.indices
== NULL
)
3504 amt
= max_sym_count
* sizeof (asection
*);
3505 finfo
.sections
= bfd_malloc (amt
);
3506 if (finfo
.sections
== NULL
)
3510 if (max_sym_shndx_count
!= 0)
3512 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
3513 finfo
.locsym_shndx
= bfd_malloc (amt
);
3514 if (finfo
.locsym_shndx
== NULL
)
3518 if (elf_hash_table (info
)->tls_sec
)
3520 bfd_vma base
, end
= 0;
3523 for (sec
= elf_hash_table (info
)->tls_sec
;
3524 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
3527 bfd_vma size
= sec
->_raw_size
;
3529 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
3531 struct bfd_link_order
*o
;
3533 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
3534 if (size
< o
->offset
+ o
->size
)
3535 size
= o
->offset
+ o
->size
;
3537 end
= sec
->vma
+ size
;
3539 base
= elf_hash_table (info
)->tls_sec
->vma
;
3540 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
3541 elf_hash_table (info
)->tls_size
= end
- base
;
3544 /* Since ELF permits relocations to be against local symbols, we
3545 must have the local symbols available when we do the relocations.
3546 Since we would rather only read the local symbols once, and we
3547 would rather not keep them in memory, we handle all the
3548 relocations for a single input file at the same time.
3550 Unfortunately, there is no way to know the total number of local
3551 symbols until we have seen all of them, and the local symbol
3552 indices precede the global symbol indices. This means that when
3553 we are generating relocatable output, and we see a reloc against
3554 a global symbol, we can not know the symbol index until we have
3555 finished examining all the local symbols to see which ones we are
3556 going to output. To deal with this, we keep the relocations in
3557 memory, and don't output them until the end of the link. This is
3558 an unfortunate waste of memory, but I don't see a good way around
3559 it. Fortunately, it only happens when performing a relocatable
3560 link, which is not the common case. FIXME: If keep_memory is set
3561 we could write the relocs out and then read them again; I don't
3562 know how bad the memory loss will be. */
3564 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3565 sub
->output_has_begun
= FALSE
;
3566 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3568 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3570 if (p
->type
== bfd_indirect_link_order
3571 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
3572 == bfd_target_elf_flavour
)
3573 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
3575 if (! sub
->output_has_begun
)
3577 if (! elf_link_input_bfd (&finfo
, sub
))
3579 sub
->output_has_begun
= TRUE
;
3582 else if (p
->type
== bfd_section_reloc_link_order
3583 || p
->type
== bfd_symbol_reloc_link_order
)
3585 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
3590 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
3596 /* Output any global symbols that got converted to local in a
3597 version script or due to symbol visibility. We do this in a
3598 separate step since ELF requires all local symbols to appear
3599 prior to any global symbols. FIXME: We should only do this if
3600 some global symbols were, in fact, converted to become local.
3601 FIXME: Will this work correctly with the Irix 5 linker? */
3602 eoinfo
.failed
= FALSE
;
3603 eoinfo
.finfo
= &finfo
;
3604 eoinfo
.localsyms
= TRUE
;
3605 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3610 /* That wrote out all the local symbols. Finish up the symbol table
3611 with the global symbols. Even if we want to strip everything we
3612 can, we still need to deal with those global symbols that got
3613 converted to local in a version script. */
3615 /* The sh_info field records the index of the first non local symbol. */
3616 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
3619 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
3621 Elf_Internal_Sym sym
;
3622 Elf_External_Sym
*dynsym
=
3623 (Elf_External_Sym
*) finfo
.dynsym_sec
->contents
;
3624 long last_local
= 0;
3626 /* Write out the section symbols for the output sections. */
3633 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
3636 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3639 Elf_External_Sym
*dest
;
3641 indx
= elf_section_data (s
)->this_idx
;
3642 BFD_ASSERT (indx
> 0);
3643 sym
.st_shndx
= indx
;
3644 sym
.st_value
= s
->vma
;
3645 dest
= dynsym
+ elf_section_data (s
)->dynindx
;
3646 elf_swap_symbol_out (abfd
, &sym
, dest
, 0);
3649 last_local
= bfd_count_sections (abfd
);
3652 /* Write out the local dynsyms. */
3653 if (elf_hash_table (info
)->dynlocal
)
3655 struct elf_link_local_dynamic_entry
*e
;
3656 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
3659 Elf_External_Sym
*dest
;
3661 sym
.st_size
= e
->isym
.st_size
;
3662 sym
.st_other
= e
->isym
.st_other
;
3664 /* Copy the internal symbol as is.
3665 Note that we saved a word of storage and overwrote
3666 the original st_name with the dynstr_index. */
3669 if (e
->isym
.st_shndx
!= SHN_UNDEF
3670 && (e
->isym
.st_shndx
< SHN_LORESERVE
3671 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
3673 s
= bfd_section_from_elf_index (e
->input_bfd
,
3677 elf_section_data (s
->output_section
)->this_idx
;
3678 sym
.st_value
= (s
->output_section
->vma
3680 + e
->isym
.st_value
);
3683 if (last_local
< e
->dynindx
)
3684 last_local
= e
->dynindx
;
3686 dest
= dynsym
+ e
->dynindx
;
3687 elf_swap_symbol_out (abfd
, &sym
, dest
, 0);
3691 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
3695 /* We get the global symbols from the hash table. */
3696 eoinfo
.failed
= FALSE
;
3697 eoinfo
.localsyms
= FALSE
;
3698 eoinfo
.finfo
= &finfo
;
3699 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3704 /* If backend needs to output some symbols not present in the hash
3705 table, do it now. */
3706 if (bed
->elf_backend_output_arch_syms
)
3708 typedef bfd_boolean (*out_sym_func
)
3709 (void *, const char *, Elf_Internal_Sym
*, asection
*);
3711 if (! ((*bed
->elf_backend_output_arch_syms
)
3712 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
3716 /* Flush all symbols to the file. */
3717 if (! elf_link_flush_output_syms (&finfo
))
3720 /* Now we know the size of the symtab section. */
3721 off
+= symtab_hdr
->sh_size
;
3723 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3724 if (symtab_shndx_hdr
->sh_name
!= 0)
3726 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
3727 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
3728 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
3729 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
3730 symtab_shndx_hdr
->sh_size
= amt
;
3732 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
3735 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
3736 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
3741 /* Finish up and write out the symbol string table (.strtab)
3743 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3744 /* sh_name was set in prep_headers. */
3745 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
3746 symstrtab_hdr
->sh_flags
= 0;
3747 symstrtab_hdr
->sh_addr
= 0;
3748 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
3749 symstrtab_hdr
->sh_entsize
= 0;
3750 symstrtab_hdr
->sh_link
= 0;
3751 symstrtab_hdr
->sh_info
= 0;
3752 /* sh_offset is set just below. */
3753 symstrtab_hdr
->sh_addralign
= 1;
3755 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
3756 elf_tdata (abfd
)->next_file_pos
= off
;
3758 if (bfd_get_symcount (abfd
) > 0)
3760 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
3761 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
3765 /* Adjust the relocs to have the correct symbol indices. */
3766 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3768 if ((o
->flags
& SEC_RELOC
) == 0)
3771 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
3772 elf_section_data (o
)->rel_count
,
3773 elf_section_data (o
)->rel_hashes
);
3774 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
3775 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
3776 elf_section_data (o
)->rel_count2
,
3777 (elf_section_data (o
)->rel_hashes
3778 + elf_section_data (o
)->rel_count
));
3780 /* Set the reloc_count field to 0 to prevent write_relocs from
3781 trying to swap the relocs out itself. */
3785 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
3786 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
3788 /* If we are linking against a dynamic object, or generating a
3789 shared library, finish up the dynamic linking information. */
3792 Elf_External_Dyn
*dyncon
, *dynconend
;
3794 /* Fix up .dynamic entries. */
3795 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
3796 BFD_ASSERT (o
!= NULL
);
3798 dyncon
= (Elf_External_Dyn
*) o
->contents
;
3799 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
3800 for (; dyncon
< dynconend
; dyncon
++)
3802 Elf_Internal_Dyn dyn
;
3806 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
3813 if (relativecount
> 0 && dyncon
+ 1 < dynconend
)
3815 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
3817 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
3818 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
3821 if (dyn
.d_tag
!= DT_NULL
)
3823 dyn
.d_un
.d_val
= relativecount
;
3824 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3830 name
= info
->init_function
;
3833 name
= info
->fini_function
;
3836 struct elf_link_hash_entry
*h
;
3838 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
3839 FALSE
, FALSE
, TRUE
);
3841 && (h
->root
.type
== bfd_link_hash_defined
3842 || h
->root
.type
== bfd_link_hash_defweak
))
3844 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
3845 o
= h
->root
.u
.def
.section
;
3846 if (o
->output_section
!= NULL
)
3847 dyn
.d_un
.d_val
+= (o
->output_section
->vma
3848 + o
->output_offset
);
3851 /* The symbol is imported from another shared
3852 library and does not apply to this one. */
3856 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3861 case DT_PREINIT_ARRAYSZ
:
3862 name
= ".preinit_array";
3864 case DT_INIT_ARRAYSZ
:
3865 name
= ".init_array";
3867 case DT_FINI_ARRAYSZ
:
3868 name
= ".fini_array";
3870 o
= bfd_get_section_by_name (abfd
, name
);
3873 (*_bfd_error_handler
)
3874 (_("%s: could not find output section %s"),
3875 bfd_get_filename (abfd
), name
);
3878 if (o
->_raw_size
== 0)
3879 (*_bfd_error_handler
)
3880 (_("warning: %s section has zero size"), name
);
3881 dyn
.d_un
.d_val
= o
->_raw_size
;
3882 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3885 case DT_PREINIT_ARRAY
:
3886 name
= ".preinit_array";
3889 name
= ".init_array";
3892 name
= ".fini_array";
3905 name
= ".gnu.version_d";
3908 name
= ".gnu.version_r";
3911 name
= ".gnu.version";
3913 o
= bfd_get_section_by_name (abfd
, name
);
3916 (*_bfd_error_handler
)
3917 (_("%s: could not find output section %s"),
3918 bfd_get_filename (abfd
), name
);
3921 dyn
.d_un
.d_ptr
= o
->vma
;
3922 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3929 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
3934 for (i
= 1; i
< elf_numsections (abfd
); i
++)
3936 Elf_Internal_Shdr
*hdr
;
3938 hdr
= elf_elfsections (abfd
)[i
];
3939 if (hdr
->sh_type
== type
3940 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
3942 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
3943 dyn
.d_un
.d_val
+= hdr
->sh_size
;
3946 if (dyn
.d_un
.d_val
== 0
3947 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
3948 dyn
.d_un
.d_val
= hdr
->sh_addr
;
3952 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3958 /* If we have created any dynamic sections, then output them. */
3961 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
3964 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
3966 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
3967 || o
->_raw_size
== 0
3968 || o
->output_section
== bfd_abs_section_ptr
)
3970 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
3972 /* At this point, we are only interested in sections
3973 created by _bfd_elf_link_create_dynamic_sections. */
3976 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
3978 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
3980 if (! bfd_set_section_contents (abfd
, o
->output_section
,
3982 (file_ptr
) o
->output_offset
,
3988 /* The contents of the .dynstr section are actually in a
3990 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
3991 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
3992 || ! _bfd_elf_strtab_emit (abfd
,
3993 elf_hash_table (info
)->dynstr
))
3999 if (info
->relocatable
)
4001 bfd_boolean failed
= FALSE
;
4003 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4008 /* If we have optimized stabs strings, output them. */
4009 if (elf_hash_table (info
)->stab_info
!= NULL
)
4011 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
4015 if (info
->eh_frame_hdr
)
4017 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
4021 if (finfo
.symstrtab
!= NULL
)
4022 _bfd_stringtab_free (finfo
.symstrtab
);
4023 if (finfo
.contents
!= NULL
)
4024 free (finfo
.contents
);
4025 if (finfo
.external_relocs
!= NULL
)
4026 free (finfo
.external_relocs
);
4027 if (finfo
.internal_relocs
!= NULL
)
4028 free (finfo
.internal_relocs
);
4029 if (finfo
.external_syms
!= NULL
)
4030 free (finfo
.external_syms
);
4031 if (finfo
.locsym_shndx
!= NULL
)
4032 free (finfo
.locsym_shndx
);
4033 if (finfo
.internal_syms
!= NULL
)
4034 free (finfo
.internal_syms
);
4035 if (finfo
.indices
!= NULL
)
4036 free (finfo
.indices
);
4037 if (finfo
.sections
!= NULL
)
4038 free (finfo
.sections
);
4039 if (finfo
.symbuf
!= NULL
)
4040 free (finfo
.symbuf
);
4041 if (finfo
.symshndxbuf
!= NULL
)
4042 free (finfo
.symshndxbuf
);
4043 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4045 if ((o
->flags
& SEC_RELOC
) != 0
4046 && elf_section_data (o
)->rel_hashes
!= NULL
)
4047 free (elf_section_data (o
)->rel_hashes
);
4050 elf_tdata (abfd
)->linker
= TRUE
;
4055 if (finfo
.symstrtab
!= NULL
)
4056 _bfd_stringtab_free (finfo
.symstrtab
);
4057 if (finfo
.contents
!= NULL
)
4058 free (finfo
.contents
);
4059 if (finfo
.external_relocs
!= NULL
)
4060 free (finfo
.external_relocs
);
4061 if (finfo
.internal_relocs
!= NULL
)
4062 free (finfo
.internal_relocs
);
4063 if (finfo
.external_syms
!= NULL
)
4064 free (finfo
.external_syms
);
4065 if (finfo
.locsym_shndx
!= NULL
)
4066 free (finfo
.locsym_shndx
);
4067 if (finfo
.internal_syms
!= NULL
)
4068 free (finfo
.internal_syms
);
4069 if (finfo
.indices
!= NULL
)
4070 free (finfo
.indices
);
4071 if (finfo
.sections
!= NULL
)
4072 free (finfo
.sections
);
4073 if (finfo
.symbuf
!= NULL
)
4074 free (finfo
.symbuf
);
4075 if (finfo
.symshndxbuf
!= NULL
)
4076 free (finfo
.symshndxbuf
);
4077 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4079 if ((o
->flags
& SEC_RELOC
) != 0
4080 && elf_section_data (o
)->rel_hashes
!= NULL
)
4081 free (elf_section_data (o
)->rel_hashes
);
4087 /* Add a symbol to the output symbol table. */
4090 elf_link_output_sym (struct elf_final_link_info
*finfo
,
4092 Elf_Internal_Sym
*elfsym
,
4093 asection
*input_sec
)
4095 Elf_External_Sym
*dest
;
4096 Elf_External_Sym_Shndx
*destshndx
;
4097 bfd_boolean (*output_symbol_hook
)
4098 (bfd
*, struct bfd_link_info
*info
, const char *,
4099 Elf_Internal_Sym
*, asection
*);
4101 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
4102 elf_backend_link_output_symbol_hook
;
4103 if (output_symbol_hook
!= NULL
)
4105 if (! ((*output_symbol_hook
)
4106 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
4110 if (name
== NULL
|| *name
== '\0')
4111 elfsym
->st_name
= 0;
4112 else if (input_sec
->flags
& SEC_EXCLUDE
)
4113 elfsym
->st_name
= 0;
4116 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
4118 if (elfsym
->st_name
== (unsigned long) -1)
4122 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
4124 if (! elf_link_flush_output_syms (finfo
))
4128 dest
= finfo
->symbuf
+ finfo
->symbuf_count
;
4129 destshndx
= finfo
->symshndxbuf
;
4130 if (destshndx
!= NULL
)
4132 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
4136 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
4137 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
4138 if (destshndx
== NULL
)
4140 memset ((char *) destshndx
+ amt
, 0, amt
);
4141 finfo
->shndxbuf_size
*= 2;
4143 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
4146 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
4147 finfo
->symbuf_count
+= 1;
4148 bfd_get_symcount (finfo
->output_bfd
) += 1;
4153 /* Flush the output symbols to the file. */
4156 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
)
4158 if (finfo
->symbuf_count
> 0)
4160 Elf_Internal_Shdr
*hdr
;
4164 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
4165 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
4166 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
4167 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
4168 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
4171 hdr
->sh_size
+= amt
;
4172 finfo
->symbuf_count
= 0;
4178 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
4179 allowing an unsatisfied unversioned symbol in the DSO to match a
4180 versioned symbol that would normally require an explicit version.
4181 We also handle the case that a DSO references a hidden symbol
4182 which may be satisfied by a versioned symbol in another DSO. */
4185 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
4186 struct elf_link_hash_entry
*h
)
4189 struct elf_link_loaded_list
*loaded
;
4191 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
4194 switch (h
->root
.type
)
4200 case bfd_link_hash_undefined
:
4201 case bfd_link_hash_undefweak
:
4202 abfd
= h
->root
.u
.undef
.abfd
;
4203 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dt_soname (abfd
) == NULL
)
4207 case bfd_link_hash_defined
:
4208 case bfd_link_hash_defweak
:
4209 abfd
= h
->root
.u
.def
.section
->owner
;
4212 case bfd_link_hash_common
:
4213 abfd
= h
->root
.u
.c
.p
->section
->owner
;
4216 BFD_ASSERT (abfd
!= NULL
);
4218 for (loaded
= elf_hash_table (info
)->loaded
;
4220 loaded
= loaded
->next
)
4223 Elf_Internal_Shdr
*hdr
;
4224 bfd_size_type symcount
;
4225 bfd_size_type extsymcount
;
4226 bfd_size_type extsymoff
;
4227 Elf_Internal_Shdr
*versymhdr
;
4228 Elf_Internal_Sym
*isym
;
4229 Elf_Internal_Sym
*isymend
;
4230 Elf_Internal_Sym
*isymbuf
;
4231 Elf_External_Versym
*ever
;
4232 Elf_External_Versym
*extversym
;
4234 input
= loaded
->abfd
;
4236 /* We check each DSO for a possible hidden versioned definition. */
4238 || (input
->flags
& DYNAMIC
) == 0
4239 || elf_dynversym (input
) == 0)
4242 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
4244 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
4245 if (elf_bad_symtab (input
))
4247 extsymcount
= symcount
;
4252 extsymcount
= symcount
- hdr
->sh_info
;
4253 extsymoff
= hdr
->sh_info
;
4256 if (extsymcount
== 0)
4259 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
4261 if (isymbuf
== NULL
)
4264 /* Read in any version definitions. */
4265 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
4266 extversym
= bfd_malloc (versymhdr
->sh_size
);
4267 if (extversym
== NULL
)
4270 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
4271 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
4272 != versymhdr
->sh_size
))
4280 ever
= extversym
+ extsymoff
;
4281 isymend
= isymbuf
+ extsymcount
;
4282 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
4285 Elf_Internal_Versym iver
;
4286 unsigned short version_index
;
4288 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
4289 || isym
->st_shndx
== SHN_UNDEF
)
4292 name
= bfd_elf_string_from_elf_section (input
,
4295 if (strcmp (name
, h
->root
.root
.string
) != 0)
4298 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
4300 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
4302 /* If we have a non-hidden versioned sym, then it should
4303 have provided a definition for the undefined sym. */
4307 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
4308 if (version_index
== 1 || version_index
== 2)
4310 /* This is the base or first version. We can use it. */
4324 /* Add an external symbol to the symbol table. This is called from
4325 the hash table traversal routine. When generating a shared object,
4326 we go through the symbol table twice. The first time we output
4327 anything that might have been forced to local scope in a version
4328 script. The second time we output the symbols that are still
4332 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
4334 struct elf_outext_info
*eoinfo
= data
;
4335 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
4337 Elf_Internal_Sym sym
;
4338 asection
*input_sec
;
4340 if (h
->root
.type
== bfd_link_hash_warning
)
4342 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4343 if (h
->root
.type
== bfd_link_hash_new
)
4347 /* Decide whether to output this symbol in this pass. */
4348 if (eoinfo
->localsyms
)
4350 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
4355 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4359 /* If we have an undefined symbol reference here then it must have
4360 come from a shared library that is being linked in. (Undefined
4361 references in regular files have already been handled). If we
4362 are reporting errors for this situation then do so now. */
4363 if (h
->root
.type
== bfd_link_hash_undefined
4364 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
4365 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
4366 && ! elf_link_check_versioned_symbol (finfo
->info
, h
)
4367 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
4369 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
4370 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
4371 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
4373 eoinfo
->failed
= TRUE
;
4378 /* We should also warn if a forced local symbol is referenced from
4379 shared libraries. */
4380 if (! finfo
->info
->relocatable
4381 && (! finfo
->info
->shared
)
4382 && (h
->elf_link_hash_flags
4383 & (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
| ELF_LINK_DYNAMIC_DEF
| ELF_LINK_DYNAMIC_WEAK
))
4384 == (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
)
4385 && ! elf_link_check_versioned_symbol (finfo
->info
, h
))
4387 (*_bfd_error_handler
)
4388 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
4389 bfd_get_filename (finfo
->output_bfd
),
4390 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
4392 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
4393 ? "hidden" : "local",
4394 h
->root
.root
.string
,
4395 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
4396 eoinfo
->failed
= TRUE
;
4400 /* We don't want to output symbols that have never been mentioned by
4401 a regular file, or that we have been told to strip. However, if
4402 h->indx is set to -2, the symbol is used by a reloc and we must
4406 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
4407 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
4408 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
4409 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
4411 else if (finfo
->info
->strip
== strip_all
)
4413 else if (finfo
->info
->strip
== strip_some
4414 && bfd_hash_lookup (finfo
->info
->keep_hash
,
4415 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
4417 else if (finfo
->info
->strip_discarded
4418 && (h
->root
.type
== bfd_link_hash_defined
4419 || h
->root
.type
== bfd_link_hash_defweak
)
4420 && elf_discarded_section (h
->root
.u
.def
.section
))
4425 /* If we're stripping it, and it's not a dynamic symbol, there's
4426 nothing else to do unless it is a forced local symbol. */
4429 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
4433 sym
.st_size
= h
->size
;
4434 sym
.st_other
= h
->other
;
4435 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4436 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
4437 else if (h
->root
.type
== bfd_link_hash_undefweak
4438 || h
->root
.type
== bfd_link_hash_defweak
)
4439 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
4441 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
4443 switch (h
->root
.type
)
4446 case bfd_link_hash_new
:
4447 case bfd_link_hash_warning
:
4451 case bfd_link_hash_undefined
:
4452 case bfd_link_hash_undefweak
:
4453 input_sec
= bfd_und_section_ptr
;
4454 sym
.st_shndx
= SHN_UNDEF
;
4457 case bfd_link_hash_defined
:
4458 case bfd_link_hash_defweak
:
4460 input_sec
= h
->root
.u
.def
.section
;
4461 if (input_sec
->output_section
!= NULL
)
4464 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
4465 input_sec
->output_section
);
4466 if (sym
.st_shndx
== SHN_BAD
)
4468 (*_bfd_error_handler
)
4469 (_("%s: could not find output section %s for input section %s"),
4470 bfd_get_filename (finfo
->output_bfd
),
4471 input_sec
->output_section
->name
,
4473 eoinfo
->failed
= TRUE
;
4477 /* ELF symbols in relocatable files are section relative,
4478 but in nonrelocatable files they are virtual
4480 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
4481 if (! finfo
->info
->relocatable
)
4483 sym
.st_value
+= input_sec
->output_section
->vma
;
4484 if (h
->type
== STT_TLS
)
4486 /* STT_TLS symbols are relative to PT_TLS segment
4488 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
4489 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
4495 BFD_ASSERT (input_sec
->owner
== NULL
4496 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
4497 sym
.st_shndx
= SHN_UNDEF
;
4498 input_sec
= bfd_und_section_ptr
;
4503 case bfd_link_hash_common
:
4504 input_sec
= h
->root
.u
.c
.p
->section
;
4505 sym
.st_shndx
= SHN_COMMON
;
4506 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
4509 case bfd_link_hash_indirect
:
4510 /* These symbols are created by symbol versioning. They point
4511 to the decorated version of the name. For example, if the
4512 symbol foo@@GNU_1.2 is the default, which should be used when
4513 foo is used with no version, then we add an indirect symbol
4514 foo which points to foo@@GNU_1.2. We ignore these symbols,
4515 since the indirected symbol is already in the hash table. */
4519 /* Give the processor backend a chance to tweak the symbol value,
4520 and also to finish up anything that needs to be done for this
4521 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
4522 forced local syms when non-shared is due to a historical quirk. */
4523 if ((h
->dynindx
!= -1
4524 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4525 && ((finfo
->info
->shared
4526 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4527 || h
->root
.type
!= bfd_link_hash_undefweak
))
4528 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
4529 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4531 const struct elf_backend_data
*bed
;
4533 bed
= get_elf_backend_data (finfo
->output_bfd
);
4534 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
4535 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
4537 eoinfo
->failed
= TRUE
;
4542 /* If we are marking the symbol as undefined, and there are no
4543 non-weak references to this symbol from a regular object, then
4544 mark the symbol as weak undefined; if there are non-weak
4545 references, mark the symbol as strong. We can't do this earlier,
4546 because it might not be marked as undefined until the
4547 finish_dynamic_symbol routine gets through with it. */
4548 if (sym
.st_shndx
== SHN_UNDEF
4549 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
4550 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
4551 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
4555 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
4556 bindtype
= STB_GLOBAL
;
4558 bindtype
= STB_WEAK
;
4559 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
4562 /* If a non-weak symbol with non-default visibility is not defined
4563 locally, it is a fatal error. */
4564 if (! finfo
->info
->relocatable
4565 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
4566 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
4567 && h
->root
.type
== bfd_link_hash_undefined
4568 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4570 (*_bfd_error_handler
)
4571 (_("%s: %s symbol `%s' isn't defined"),
4572 bfd_get_filename (finfo
->output_bfd
),
4573 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
4575 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
4576 ? "internal" : "hidden",
4577 h
->root
.root
.string
);
4578 eoinfo
->failed
= TRUE
;
4582 /* If this symbol should be put in the .dynsym section, then put it
4583 there now. We already know the symbol index. We also fill in
4584 the entry in the .hash section. */
4585 if (h
->dynindx
!= -1
4586 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4590 size_t hash_entry_size
;
4591 bfd_byte
*bucketpos
;
4593 Elf_External_Sym
*esym
;
4595 sym
.st_name
= h
->dynstr_index
;
4596 esym
= (Elf_External_Sym
*) finfo
->dynsym_sec
->contents
+ h
->dynindx
;
4597 elf_swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
4599 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
4600 bucket
= h
->elf_hash_value
% bucketcount
;
4602 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
4603 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
4604 + (bucket
+ 2) * hash_entry_size
);
4605 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
4606 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
4607 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
4608 ((bfd_byte
*) finfo
->hash_sec
->contents
4609 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
4611 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
4613 Elf_Internal_Versym iversym
;
4614 Elf_External_Versym
*eversym
;
4616 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4618 if (h
->verinfo
.verdef
== NULL
)
4619 iversym
.vs_vers
= 0;
4621 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4625 if (h
->verinfo
.vertree
== NULL
)
4626 iversym
.vs_vers
= 1;
4628 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
4631 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
4632 iversym
.vs_vers
|= VERSYM_HIDDEN
;
4634 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
4635 eversym
+= h
->dynindx
;
4636 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
4640 /* If we're stripping it, then it was just a dynamic symbol, and
4641 there's nothing else to do. */
4642 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
4645 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
4647 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
4649 eoinfo
->failed
= TRUE
;
4656 /* Link an input file into the linker output file. This function
4657 handles all the sections and relocations of the input file at once.
4658 This is so that we only have to read the local symbols once, and
4659 don't have to keep them in memory. */
4662 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
4664 bfd_boolean (*relocate_section
)
4665 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
4666 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
4668 Elf_Internal_Shdr
*symtab_hdr
;
4671 Elf_Internal_Sym
*isymbuf
;
4672 Elf_Internal_Sym
*isym
;
4673 Elf_Internal_Sym
*isymend
;
4675 asection
**ppsection
;
4677 const struct elf_backend_data
*bed
;
4678 bfd_boolean emit_relocs
;
4679 struct elf_link_hash_entry
**sym_hashes
;
4681 output_bfd
= finfo
->output_bfd
;
4682 bed
= get_elf_backend_data (output_bfd
);
4683 relocate_section
= bed
->elf_backend_relocate_section
;
4685 /* If this is a dynamic object, we don't want to do anything here:
4686 we don't want the local symbols, and we don't want the section
4688 if ((input_bfd
->flags
& DYNAMIC
) != 0)
4691 emit_relocs
= (finfo
->info
->relocatable
4692 || finfo
->info
->emitrelocations
4693 || bed
->elf_backend_emit_relocs
);
4695 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
4696 if (elf_bad_symtab (input_bfd
))
4698 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
4703 locsymcount
= symtab_hdr
->sh_info
;
4704 extsymoff
= symtab_hdr
->sh_info
;
4707 /* Read the local symbols. */
4708 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
4709 if (isymbuf
== NULL
&& locsymcount
!= 0)
4711 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
4712 finfo
->internal_syms
,
4713 finfo
->external_syms
,
4714 finfo
->locsym_shndx
);
4715 if (isymbuf
== NULL
)
4719 /* Find local symbol sections and adjust values of symbols in
4720 SEC_MERGE sections. Write out those local symbols we know are
4721 going into the output file. */
4722 isymend
= isymbuf
+ locsymcount
;
4723 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
4725 isym
++, pindex
++, ppsection
++)
4729 Elf_Internal_Sym osym
;
4733 if (elf_bad_symtab (input_bfd
))
4735 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
4742 if (isym
->st_shndx
== SHN_UNDEF
)
4743 isec
= bfd_und_section_ptr
;
4744 else if (isym
->st_shndx
< SHN_LORESERVE
4745 || isym
->st_shndx
> SHN_HIRESERVE
)
4747 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
4749 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
4750 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
4752 _bfd_merged_section_offset (output_bfd
, &isec
,
4753 elf_section_data (isec
)->sec_info
,
4756 else if (isym
->st_shndx
== SHN_ABS
)
4757 isec
= bfd_abs_section_ptr
;
4758 else if (isym
->st_shndx
== SHN_COMMON
)
4759 isec
= bfd_com_section_ptr
;
4768 /* Don't output the first, undefined, symbol. */
4769 if (ppsection
== finfo
->sections
)
4772 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
4774 /* We never output section symbols. Instead, we use the
4775 section symbol of the corresponding section in the output
4780 /* If we are stripping all symbols, we don't want to output this
4782 if (finfo
->info
->strip
== strip_all
)
4785 /* If we are discarding all local symbols, we don't want to
4786 output this one. If we are generating a relocatable output
4787 file, then some of the local symbols may be required by
4788 relocs; we output them below as we discover that they are
4790 if (finfo
->info
->discard
== discard_all
)
4793 /* If this symbol is defined in a section which we are
4794 discarding, we don't need to keep it, but note that
4795 linker_mark is only reliable for sections that have contents.
4796 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4797 as well as linker_mark. */
4798 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
4800 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
4801 || (! finfo
->info
->relocatable
4802 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
4805 /* Get the name of the symbol. */
4806 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
4811 /* See if we are discarding symbols with this name. */
4812 if ((finfo
->info
->strip
== strip_some
4813 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
4815 || (((finfo
->info
->discard
== discard_sec_merge
4816 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
4817 || finfo
->info
->discard
== discard_l
)
4818 && bfd_is_local_label_name (input_bfd
, name
)))
4821 /* If we get here, we are going to output this symbol. */
4825 /* Adjust the section index for the output file. */
4826 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
4827 isec
->output_section
);
4828 if (osym
.st_shndx
== SHN_BAD
)
4831 *pindex
= bfd_get_symcount (output_bfd
);
4833 /* ELF symbols in relocatable files are section relative, but
4834 in executable files they are virtual addresses. Note that
4835 this code assumes that all ELF sections have an associated
4836 BFD section with a reasonable value for output_offset; below
4837 we assume that they also have a reasonable value for
4838 output_section. Any special sections must be set up to meet
4839 these requirements. */
4840 osym
.st_value
+= isec
->output_offset
;
4841 if (! finfo
->info
->relocatable
)
4843 osym
.st_value
+= isec
->output_section
->vma
;
4844 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
4846 /* STT_TLS symbols are relative to PT_TLS segment base. */
4847 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
4848 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
4852 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
4856 /* Relocate the contents of each section. */
4857 sym_hashes
= elf_sym_hashes (input_bfd
);
4858 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4862 if (! o
->linker_mark
)
4864 /* This section was omitted from the link. */
4868 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4869 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
4872 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
4874 /* Section was created by _bfd_elf_link_create_dynamic_sections
4879 /* Get the contents of the section. They have been cached by a
4880 relaxation routine. Note that o is a section in an input
4881 file, so the contents field will not have been set by any of
4882 the routines which work on output files. */
4883 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
4884 contents
= elf_section_data (o
)->this_hdr
.contents
;
4887 contents
= finfo
->contents
;
4888 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0,
4893 if ((o
->flags
& SEC_RELOC
) != 0)
4895 Elf_Internal_Rela
*internal_relocs
;
4897 /* Get the swapped relocs. */
4899 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
4900 finfo
->internal_relocs
, FALSE
);
4901 if (internal_relocs
== NULL
4902 && o
->reloc_count
> 0)
4905 /* Run through the relocs looking for any against symbols
4906 from discarded sections and section symbols from
4907 removed link-once sections. Complain about relocs
4908 against discarded sections. Zero relocs against removed
4909 link-once sections. Preserve debug information as much
4911 if (!elf_section_ignore_discarded_relocs (o
))
4913 Elf_Internal_Rela
*rel
, *relend
;
4915 rel
= internal_relocs
;
4916 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
4917 for ( ; rel
< relend
; rel
++)
4919 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
4922 if (r_symndx
>= locsymcount
4923 || (elf_bad_symtab (input_bfd
)
4924 && finfo
->sections
[r_symndx
] == NULL
))
4926 struct elf_link_hash_entry
*h
;
4928 h
= sym_hashes
[r_symndx
- extsymoff
];
4929 while (h
->root
.type
== bfd_link_hash_indirect
4930 || h
->root
.type
== bfd_link_hash_warning
)
4931 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4933 /* Complain if the definition comes from a
4934 discarded section. */
4935 sec
= h
->root
.u
.def
.section
;
4936 if ((h
->root
.type
== bfd_link_hash_defined
4937 || h
->root
.type
== bfd_link_hash_defweak
)
4938 && elf_discarded_section (sec
))
4940 if ((o
->flags
& SEC_DEBUGGING
) != 0)
4942 BFD_ASSERT (r_symndx
!= 0);
4943 /* Try to preserve debug information. */
4944 if ((o
->flags
& SEC_DEBUGGING
) != 0
4945 && sec
->kept_section
!= NULL
4946 && sec
->_raw_size
== sec
->kept_section
->_raw_size
)
4947 h
->root
.u
.def
.section
4948 = sec
->kept_section
;
4950 memset (rel
, 0, sizeof (*rel
));
4953 finfo
->info
->callbacks
->error_handler
4954 (LD_DEFINITION_IN_DISCARDED_SECTION
,
4955 _("%T: discarded in section `%s' from %s\n"),
4956 h
->root
.root
.string
,
4957 h
->root
.root
.string
,
4958 h
->root
.u
.def
.section
->name
,
4959 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
4964 sec
= finfo
->sections
[r_symndx
];
4966 if (sec
!= NULL
&& elf_discarded_section (sec
))
4968 if ((o
->flags
& SEC_DEBUGGING
) != 0
4969 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
4971 BFD_ASSERT (r_symndx
!= 0);
4972 /* Try to preserve debug information. */
4973 if ((o
->flags
& SEC_DEBUGGING
) != 0
4974 && sec
->kept_section
!= NULL
4975 && sec
->_raw_size
== sec
->kept_section
->_raw_size
)
4976 finfo
->sections
[r_symndx
]
4977 = sec
->kept_section
;
4981 = ELF_R_INFO (0, ELF_R_TYPE (rel
->r_info
));
4991 ok
= asprintf (&buf
, "local symbol %d",
4994 buf
= (char *) "local symbol";
4995 finfo
->info
->callbacks
->error_handler
4996 (LD_DEFINITION_IN_DISCARDED_SECTION
,
4997 _("%T: discarded in section `%s' from %s\n"),
4998 buf
, buf
, sec
->name
,
4999 bfd_archive_filename (input_bfd
));
5008 /* Relocate the section by invoking a back end routine.
5010 The back end routine is responsible for adjusting the
5011 section contents as necessary, and (if using Rela relocs
5012 and generating a relocatable output file) adjusting the
5013 reloc addend as necessary.
5015 The back end routine does not have to worry about setting
5016 the reloc address or the reloc symbol index.
5018 The back end routine is given a pointer to the swapped in
5019 internal symbols, and can access the hash table entries
5020 for the external symbols via elf_sym_hashes (input_bfd).
5022 When generating relocatable output, the back end routine
5023 must handle STB_LOCAL/STT_SECTION symbols specially. The
5024 output symbol is going to be a section symbol
5025 corresponding to the output section, which will require
5026 the addend to be adjusted. */
5028 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
5029 input_bfd
, o
, contents
,
5037 Elf_Internal_Rela
*irela
;
5038 Elf_Internal_Rela
*irelaend
;
5039 bfd_vma last_offset
;
5040 struct elf_link_hash_entry
**rel_hash
;
5041 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
5042 unsigned int next_erel
;
5043 bfd_boolean (*reloc_emitter
)
5044 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
5045 bfd_boolean rela_normal
;
5047 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
5048 rela_normal
= (bed
->rela_normal
5049 && (input_rel_hdr
->sh_entsize
5050 == sizeof (Elf_External_Rela
)));
5052 /* Adjust the reloc addresses and symbol indices. */
5054 irela
= internal_relocs
;
5055 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5056 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
5057 + elf_section_data (o
->output_section
)->rel_count
5058 + elf_section_data (o
->output_section
)->rel_count2
);
5059 last_offset
= o
->output_offset
;
5060 if (!finfo
->info
->relocatable
)
5061 last_offset
+= o
->output_section
->vma
;
5062 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
5064 unsigned long r_symndx
;
5066 Elf_Internal_Sym sym
;
5068 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
5074 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
5077 if (irela
->r_offset
>= (bfd_vma
) -2)
5079 /* This is a reloc for a deleted entry or somesuch.
5080 Turn it into an R_*_NONE reloc, at the same
5081 offset as the last reloc. elf_eh_frame.c and
5082 elf_bfd_discard_info rely on reloc offsets
5084 irela
->r_offset
= last_offset
;
5086 irela
->r_addend
= 0;
5090 irela
->r_offset
+= o
->output_offset
;
5092 /* Relocs in an executable have to be virtual addresses. */
5093 if (!finfo
->info
->relocatable
)
5094 irela
->r_offset
+= o
->output_section
->vma
;
5096 last_offset
= irela
->r_offset
;
5098 r_symndx
= ELF_R_SYM (irela
->r_info
);
5099 if (r_symndx
== STN_UNDEF
)
5102 if (r_symndx
>= locsymcount
5103 || (elf_bad_symtab (input_bfd
)
5104 && finfo
->sections
[r_symndx
] == NULL
))
5106 struct elf_link_hash_entry
*rh
;
5109 /* This is a reloc against a global symbol. We
5110 have not yet output all the local symbols, so
5111 we do not know the symbol index of any global
5112 symbol. We set the rel_hash entry for this
5113 reloc to point to the global hash table entry
5114 for this symbol. The symbol index is then
5115 set at the end of elf_bfd_final_link. */
5116 indx
= r_symndx
- extsymoff
;
5117 rh
= elf_sym_hashes (input_bfd
)[indx
];
5118 while (rh
->root
.type
== bfd_link_hash_indirect
5119 || rh
->root
.type
== bfd_link_hash_warning
)
5120 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
5122 /* Setting the index to -2 tells
5123 elf_link_output_extsym that this symbol is
5125 BFD_ASSERT (rh
->indx
< 0);
5133 /* This is a reloc against a local symbol. */
5136 sym
= isymbuf
[r_symndx
];
5137 sec
= finfo
->sections
[r_symndx
];
5138 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
5140 /* I suppose the backend ought to fill in the
5141 section of any STT_SECTION symbol against a
5142 processor specific section. If we have
5143 discarded a section, the output_section will
5144 be the absolute section. */
5145 if (bfd_is_abs_section (sec
)
5147 && bfd_is_abs_section (sec
->output_section
)))
5149 else if (sec
== NULL
|| sec
->owner
== NULL
)
5151 bfd_set_error (bfd_error_bad_value
);
5156 r_symndx
= sec
->output_section
->target_index
;
5157 BFD_ASSERT (r_symndx
!= 0);
5160 /* Adjust the addend according to where the
5161 section winds up in the output section. */
5163 irela
->r_addend
+= sec
->output_offset
;
5167 if (finfo
->indices
[r_symndx
] == -1)
5169 unsigned long shlink
;
5173 if (finfo
->info
->strip
== strip_all
)
5175 /* You can't do ld -r -s. */
5176 bfd_set_error (bfd_error_invalid_operation
);
5180 /* This symbol was skipped earlier, but
5181 since it is needed by a reloc, we
5182 must output it now. */
5183 shlink
= symtab_hdr
->sh_link
;
5184 name
= (bfd_elf_string_from_elf_section
5185 (input_bfd
, shlink
, sym
.st_name
));
5189 osec
= sec
->output_section
;
5191 _bfd_elf_section_from_bfd_section (output_bfd
,
5193 if (sym
.st_shndx
== SHN_BAD
)
5196 sym
.st_value
+= sec
->output_offset
;
5197 if (! finfo
->info
->relocatable
)
5199 sym
.st_value
+= osec
->vma
;
5200 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
5202 /* STT_TLS symbols are relative to PT_TLS
5204 BFD_ASSERT (elf_hash_table (finfo
->info
)
5206 sym
.st_value
-= (elf_hash_table (finfo
->info
)
5211 finfo
->indices
[r_symndx
]
5212 = bfd_get_symcount (output_bfd
);
5214 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
))
5218 r_symndx
= finfo
->indices
[r_symndx
];
5221 irela
->r_info
= ELF_R_INFO (r_symndx
,
5222 ELF_R_TYPE (irela
->r_info
));
5225 /* Swap out the relocs. */
5226 if (bed
->elf_backend_emit_relocs
5227 && !(finfo
->info
->relocatable
5228 || finfo
->info
->emitrelocations
))
5229 reloc_emitter
= bed
->elf_backend_emit_relocs
;
5231 reloc_emitter
= _bfd_elf_link_output_relocs
;
5233 if (input_rel_hdr
->sh_size
!= 0
5234 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
5238 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
5239 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
5241 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
5242 * bed
->s
->int_rels_per_ext_rel
);
5243 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
5250 /* Write out the modified section contents. */
5251 if (bed
->elf_backend_write_section
5252 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
5254 /* Section written out. */
5256 else switch (o
->sec_info_type
)
5258 case ELF_INFO_TYPE_STABS
:
5259 if (! (_bfd_write_section_stabs
5261 &elf_hash_table (finfo
->info
)->stab_info
,
5262 o
, &elf_section_data (o
)->sec_info
, contents
)))
5265 case ELF_INFO_TYPE_MERGE
:
5266 if (! _bfd_write_merged_section (output_bfd
, o
,
5267 elf_section_data (o
)->sec_info
))
5270 case ELF_INFO_TYPE_EH_FRAME
:
5272 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
5279 bfd_size_type sec_size
;
5281 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
5282 if (! (o
->flags
& SEC_EXCLUDE
)
5283 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
5285 (file_ptr
) o
->output_offset
,
5296 /* Generate a reloc when linking an ELF file. This is a reloc
5297 requested by the linker, and does come from any input file. This
5298 is used to build constructor and destructor tables when linking
5302 elf_reloc_link_order (bfd
*output_bfd
,
5303 struct bfd_link_info
*info
,
5304 asection
*output_section
,
5305 struct bfd_link_order
*link_order
)
5307 reloc_howto_type
*howto
;
5311 struct elf_link_hash_entry
**rel_hash_ptr
;
5312 Elf_Internal_Shdr
*rel_hdr
;
5313 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
5314 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
5318 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5321 bfd_set_error (bfd_error_bad_value
);
5325 addend
= link_order
->u
.reloc
.p
->addend
;
5327 /* Figure out the symbol index. */
5328 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
5329 + elf_section_data (output_section
)->rel_count
5330 + elf_section_data (output_section
)->rel_count2
);
5331 if (link_order
->type
== bfd_section_reloc_link_order
)
5333 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
5334 BFD_ASSERT (indx
!= 0);
5335 *rel_hash_ptr
= NULL
;
5339 struct elf_link_hash_entry
*h
;
5341 /* Treat a reloc against a defined symbol as though it were
5342 actually against the section. */
5343 h
= ((struct elf_link_hash_entry
*)
5344 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
5345 link_order
->u
.reloc
.p
->u
.name
,
5346 FALSE
, FALSE
, TRUE
));
5348 && (h
->root
.type
== bfd_link_hash_defined
5349 || h
->root
.type
== bfd_link_hash_defweak
))
5353 section
= h
->root
.u
.def
.section
;
5354 indx
= section
->output_section
->target_index
;
5355 *rel_hash_ptr
= NULL
;
5356 /* It seems that we ought to add the symbol value to the
5357 addend here, but in practice it has already been added
5358 because it was passed to constructor_callback. */
5359 addend
+= section
->output_section
->vma
+ section
->output_offset
;
5363 /* Setting the index to -2 tells elf_link_output_extsym that
5364 this symbol is used by a reloc. */
5371 if (! ((*info
->callbacks
->unattached_reloc
)
5372 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
5378 /* If this is an inplace reloc, we must write the addend into the
5380 if (howto
->partial_inplace
&& addend
!= 0)
5383 bfd_reloc_status_type rstat
;
5386 const char *sym_name
;
5388 size
= bfd_get_reloc_size (howto
);
5389 buf
= bfd_zmalloc (size
);
5392 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5399 case bfd_reloc_outofrange
:
5402 case bfd_reloc_overflow
:
5403 if (link_order
->type
== bfd_section_reloc_link_order
)
5404 sym_name
= bfd_section_name (output_bfd
,
5405 link_order
->u
.reloc
.p
->u
.section
);
5407 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
5408 if (! ((*info
->callbacks
->reloc_overflow
)
5409 (info
, sym_name
, howto
->name
, addend
, NULL
, NULL
, 0)))
5416 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
5417 link_order
->offset
, size
);
5423 /* The address of a reloc is relative to the section in a
5424 relocatable file, and is a virtual address in an executable
5426 offset
= link_order
->offset
;
5427 if (! info
->relocatable
)
5428 offset
+= output_section
->vma
;
5430 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
5432 irel
[i
].r_offset
= offset
;
5434 irel
[i
].r_addend
= 0;
5436 irel
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
5438 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
5439 erel
= rel_hdr
->contents
;
5440 if (rel_hdr
->sh_type
== SHT_REL
)
5442 erel
+= (elf_section_data (output_section
)->rel_count
5443 * sizeof (Elf_External_Rel
));
5444 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
5448 irel
[0].r_addend
= addend
;
5449 erel
+= (elf_section_data (output_section
)->rel_count
5450 * sizeof (Elf_External_Rela
));
5451 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
5454 ++elf_section_data (output_section
)->rel_count
;
5459 /* Garbage collect unused sections. */
5461 static bfd_boolean elf_gc_sweep_symbol
5462 (struct elf_link_hash_entry
*, void *);
5464 static bfd_boolean elf_gc_allocate_got_offsets
5465 (struct elf_link_hash_entry
*, void *);
5467 /* The mark phase of garbage collection. For a given section, mark
5468 it and any sections in this section's group, and all the sections
5469 which define symbols to which it refers. */
5471 typedef asection
* (*gc_mark_hook_fn
)
5472 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
5473 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
5476 elf_gc_mark (struct bfd_link_info
*info
,
5478 gc_mark_hook_fn gc_mark_hook
)
5481 asection
*group_sec
;
5485 /* Mark all the sections in the group. */
5486 group_sec
= elf_section_data (sec
)->next_in_group
;
5487 if (group_sec
&& !group_sec
->gc_mark
)
5488 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
5491 /* Look through the section relocs. */
5493 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
5495 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
5496 Elf_Internal_Shdr
*symtab_hdr
;
5497 struct elf_link_hash_entry
**sym_hashes
;
5500 bfd
*input_bfd
= sec
->owner
;
5501 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
5502 Elf_Internal_Sym
*isym
= NULL
;
5504 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
5505 sym_hashes
= elf_sym_hashes (input_bfd
);
5507 /* Read the local symbols. */
5508 if (elf_bad_symtab (input_bfd
))
5510 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
5514 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
5516 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
5517 if (isym
== NULL
&& nlocsyms
!= 0)
5519 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
5525 /* Read the relocations. */
5526 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
5528 if (relstart
== NULL
)
5533 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5535 for (rel
= relstart
; rel
< relend
; rel
++)
5537 unsigned long r_symndx
;
5539 struct elf_link_hash_entry
*h
;
5541 r_symndx
= ELF_R_SYM (rel
->r_info
);
5545 if (r_symndx
>= nlocsyms
5546 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
5548 h
= sym_hashes
[r_symndx
- extsymoff
];
5549 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
5553 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
5556 if (rsec
&& !rsec
->gc_mark
)
5558 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
5560 else if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
5569 if (elf_section_data (sec
)->relocs
!= relstart
)
5572 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
5574 if (! info
->keep_memory
)
5577 symtab_hdr
->contents
= (unsigned char *) isym
;
5584 /* The sweep phase of garbage collection. Remove all garbage sections. */
5586 typedef bfd_boolean (*gc_sweep_hook_fn
)
5587 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
5590 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
5594 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5598 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
5601 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5603 /* Keep special sections. Keep .debug sections. */
5604 if ((o
->flags
& SEC_LINKER_CREATED
)
5605 || (o
->flags
& SEC_DEBUGGING
))
5611 /* Skip sweeping sections already excluded. */
5612 if (o
->flags
& SEC_EXCLUDE
)
5615 /* Since this is early in the link process, it is simple
5616 to remove a section from the output. */
5617 o
->flags
|= SEC_EXCLUDE
;
5619 /* But we also have to update some of the relocation
5620 info we collected before. */
5622 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
5624 Elf_Internal_Rela
*internal_relocs
;
5628 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
5630 if (internal_relocs
== NULL
)
5633 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
5635 if (elf_section_data (o
)->relocs
!= internal_relocs
)
5636 free (internal_relocs
);
5644 /* Remove the symbols that were in the swept sections from the dynamic
5645 symbol table. GCFIXME: Anyone know how to get them out of the
5646 static symbol table as well? */
5650 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
5652 elf_hash_table (info
)->dynsymcount
= i
;
5658 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5661 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
5665 if (h
->root
.type
== bfd_link_hash_warning
)
5666 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5668 if (h
->dynindx
!= -1
5669 && ((h
->root
.type
!= bfd_link_hash_defined
5670 && h
->root
.type
!= bfd_link_hash_defweak
)
5671 || h
->root
.u
.def
.section
->gc_mark
))
5672 h
->dynindx
= (*idx
)++;
5677 /* Propogate collected vtable information. This is called through
5678 elf_link_hash_traverse. */
5681 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
5683 if (h
->root
.type
== bfd_link_hash_warning
)
5684 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5686 /* Those that are not vtables. */
5687 if (h
->vtable_parent
== NULL
)
5690 /* Those vtables that do not have parents, we cannot merge. */
5691 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
5694 /* If we've already been done, exit. */
5695 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
5698 /* Make sure the parent's table is up to date. */
5699 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
5701 if (h
->vtable_entries_used
== NULL
)
5703 /* None of this table's entries were referenced. Re-use the
5705 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
5706 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
5711 bfd_boolean
*cu
, *pu
;
5713 /* Or the parent's entries into ours. */
5714 cu
= h
->vtable_entries_used
;
5716 pu
= h
->vtable_parent
->vtable_entries_used
;
5719 const struct elf_backend_data
*bed
;
5720 unsigned int log_file_align
;
5722 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
5723 log_file_align
= bed
->s
->log_file_align
;
5724 n
= h
->vtable_parent
->vtable_entries_size
>> log_file_align
;
5739 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
5742 bfd_vma hstart
, hend
;
5743 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
5744 const struct elf_backend_data
*bed
;
5745 unsigned int log_file_align
;
5747 if (h
->root
.type
== bfd_link_hash_warning
)
5748 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5750 /* Take care of both those symbols that do not describe vtables as
5751 well as those that are not loaded. */
5752 if (h
->vtable_parent
== NULL
)
5755 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5756 || h
->root
.type
== bfd_link_hash_defweak
);
5758 sec
= h
->root
.u
.def
.section
;
5759 hstart
= h
->root
.u
.def
.value
;
5760 hend
= hstart
+ h
->size
;
5762 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
5764 return *(bfd_boolean
*) okp
= FALSE
;
5765 bed
= get_elf_backend_data (sec
->owner
);
5766 log_file_align
= bed
->s
->log_file_align
;
5768 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5770 for (rel
= relstart
; rel
< relend
; ++rel
)
5771 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
5773 /* If the entry is in use, do nothing. */
5774 if (h
->vtable_entries_used
5775 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
5777 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
5778 if (h
->vtable_entries_used
[entry
])
5781 /* Otherwise, kill it. */
5782 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
5788 /* Do mark and sweep of unused sections. */
5791 elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
5793 bfd_boolean ok
= TRUE
;
5795 asection
* (*gc_mark_hook
)
5796 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
5797 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
5799 if (!get_elf_backend_data (abfd
)->can_gc_sections
5800 || info
->relocatable
|| info
->emitrelocations
5801 || elf_hash_table (info
)->dynamic_sections_created
)
5804 /* Apply transitive closure to the vtable entry usage info. */
5805 elf_link_hash_traverse (elf_hash_table (info
),
5806 elf_gc_propagate_vtable_entries_used
,
5811 /* Kill the vtable relocations that were not used. */
5812 elf_link_hash_traverse (elf_hash_table (info
),
5813 elf_gc_smash_unused_vtentry_relocs
,
5818 /* Grovel through relocs to find out who stays ... */
5820 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
5821 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5825 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
5828 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5830 if (o
->flags
& SEC_KEEP
)
5831 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
5836 /* ... and mark SEC_EXCLUDE for those that go. */
5837 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
5843 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
5846 elf_gc_record_vtinherit (bfd
*abfd
,
5848 struct elf_link_hash_entry
*h
,
5851 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
5852 struct elf_link_hash_entry
**search
, *child
;
5853 bfd_size_type extsymcount
;
5855 /* The sh_info field of the symtab header tells us where the
5856 external symbols start. We don't care about the local symbols at
5858 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
5859 if (!elf_bad_symtab (abfd
))
5860 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
5862 sym_hashes
= elf_sym_hashes (abfd
);
5863 sym_hashes_end
= sym_hashes
+ extsymcount
;
5865 /* Hunt down the child symbol, which is in this section at the same
5866 offset as the relocation. */
5867 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
5869 if ((child
= *search
) != NULL
5870 && (child
->root
.type
== bfd_link_hash_defined
5871 || child
->root
.type
== bfd_link_hash_defweak
)
5872 && child
->root
.u
.def
.section
== sec
5873 && child
->root
.u
.def
.value
== offset
)
5877 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
5878 bfd_archive_filename (abfd
), sec
->name
,
5879 (unsigned long) offset
);
5880 bfd_set_error (bfd_error_invalid_operation
);
5886 /* This *should* only be the absolute section. It could potentially
5887 be that someone has defined a non-global vtable though, which
5888 would be bad. It isn't worth paging in the local symbols to be
5889 sure though; that case should simply be handled by the assembler. */
5891 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
5894 child
->vtable_parent
= h
;
5899 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
5902 elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
5903 asection
*sec ATTRIBUTE_UNUSED
,
5904 struct elf_link_hash_entry
*h
,
5907 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5908 unsigned int log_file_align
= bed
->s
->log_file_align
;
5910 if (addend
>= h
->vtable_entries_size
)
5912 size_t size
, bytes
, file_align
;
5913 bfd_boolean
*ptr
= h
->vtable_entries_used
;
5915 /* While the symbol is undefined, we have to be prepared to handle
5917 file_align
= 1 << log_file_align
;
5918 if (h
->root
.type
== bfd_link_hash_undefined
)
5919 size
= addend
+ file_align
;
5925 /* Oops! We've got a reference past the defined end of
5926 the table. This is probably a bug -- shall we warn? */
5927 size
= addend
+ file_align
;
5930 size
= (size
+ file_align
- 1) & -file_align
;
5932 /* Allocate one extra entry for use as a "done" flag for the
5933 consolidation pass. */
5934 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
5938 ptr
= bfd_realloc (ptr
- 1, bytes
);
5944 oldbytes
= (((h
->vtable_entries_size
>> log_file_align
) + 1)
5945 * sizeof (bfd_boolean
));
5946 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
5950 ptr
= bfd_zmalloc (bytes
);
5955 /* And arrange for that done flag to be at index -1. */
5956 h
->vtable_entries_used
= ptr
+ 1;
5957 h
->vtable_entries_size
= size
;
5960 h
->vtable_entries_used
[addend
>> log_file_align
] = TRUE
;
5965 /* And an accompanying bit to work out final got entry offsets once
5966 we're done. Should be called from final_link. */
5969 elf_gc_common_finalize_got_offsets (bfd
*abfd
,
5970 struct bfd_link_info
*info
)
5973 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5976 /* The GOT offset is relative to the .got section, but the GOT header is
5977 put into the .got.plt section, if the backend uses it. */
5978 if (bed
->want_got_plt
)
5981 gotoff
= bed
->got_header_size
;
5983 /* Do the local .got entries first. */
5984 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
5986 bfd_signed_vma
*local_got
;
5987 bfd_size_type j
, locsymcount
;
5988 Elf_Internal_Shdr
*symtab_hdr
;
5990 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
5993 local_got
= elf_local_got_refcounts (i
);
5997 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
5998 if (elf_bad_symtab (i
))
5999 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6001 locsymcount
= symtab_hdr
->sh_info
;
6003 for (j
= 0; j
< locsymcount
; ++j
)
6005 if (local_got
[j
] > 0)
6007 local_got
[j
] = gotoff
;
6008 gotoff
+= ARCH_SIZE
/ 8;
6011 local_got
[j
] = (bfd_vma
) -1;
6015 /* Then the global .got entries. .plt refcounts are handled by
6016 adjust_dynamic_symbol */
6017 elf_link_hash_traverse (elf_hash_table (info
),
6018 elf_gc_allocate_got_offsets
,
6023 /* We need a special top-level link routine to convert got reference counts
6024 to real got offsets. */
6027 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *offarg
)
6029 bfd_vma
*off
= offarg
;
6031 if (h
->root
.type
== bfd_link_hash_warning
)
6032 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6034 if (h
->got
.refcount
> 0)
6036 h
->got
.offset
= off
[0];
6037 off
[0] += ARCH_SIZE
/ 8;
6040 h
->got
.offset
= (bfd_vma
) -1;
6045 /* Many folk need no more in the way of final link than this, once
6046 got entry reference counting is enabled. */
6049 elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
6051 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
6054 /* Invoke the regular ELF backend linker to do all the work. */
6055 return elf_bfd_final_link (abfd
, info
);
6058 /* This function will be called though elf_link_hash_traverse to store
6059 all hash value of the exported symbols in an array. */
6062 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6064 unsigned long **valuep
= data
;
6070 if (h
->root
.type
== bfd_link_hash_warning
)
6071 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6073 /* Ignore indirect symbols. These are added by the versioning code. */
6074 if (h
->dynindx
== -1)
6077 name
= h
->root
.root
.string
;
6078 p
= strchr (name
, ELF_VER_CHR
);
6081 alc
= bfd_malloc (p
- name
+ 1);
6082 memcpy (alc
, name
, p
- name
);
6083 alc
[p
- name
] = '\0';
6087 /* Compute the hash value. */
6088 ha
= bfd_elf_hash (name
);
6090 /* Store the found hash value in the array given as the argument. */
6093 /* And store it in the struct so that we can put it in the hash table
6095 h
->elf_hash_value
= ha
;
6104 elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
6106 struct elf_reloc_cookie
*rcookie
= cookie
;
6108 if (rcookie
->bad_symtab
)
6109 rcookie
->rel
= rcookie
->rels
;
6111 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
6113 unsigned long r_symndx
;
6115 if (! rcookie
->bad_symtab
)
6116 if (rcookie
->rel
->r_offset
> offset
)
6118 if (rcookie
->rel
->r_offset
!= offset
)
6121 r_symndx
= ELF_R_SYM (rcookie
->rel
->r_info
);
6122 if (r_symndx
== SHN_UNDEF
)
6125 if (r_symndx
>= rcookie
->locsymcount
6126 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
6128 struct elf_link_hash_entry
*h
;
6130 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
6132 while (h
->root
.type
== bfd_link_hash_indirect
6133 || h
->root
.type
== bfd_link_hash_warning
)
6134 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6136 if ((h
->root
.type
== bfd_link_hash_defined
6137 || h
->root
.type
== bfd_link_hash_defweak
)
6138 && elf_discarded_section (h
->root
.u
.def
.section
))
6145 /* It's not a relocation against a global symbol,
6146 but it could be a relocation against a local
6147 symbol for a discarded section. */
6149 Elf_Internal_Sym
*isym
;
6151 /* Need to: get the symbol; get the section. */
6152 isym
= &rcookie
->locsyms
[r_symndx
];
6153 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6155 isec
= section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
6156 if (isec
!= NULL
&& elf_discarded_section (isec
))
6165 /* Discard unneeded references to discarded sections.
6166 Returns TRUE if any section's size was changed. */
6167 /* This function assumes that the relocations are in sorted order,
6168 which is true for all known assemblers. */
6171 elf_bfd_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
6173 struct elf_reloc_cookie cookie
;
6174 asection
*stab
, *eh
;
6175 Elf_Internal_Shdr
*symtab_hdr
;
6176 const struct elf_backend_data
*bed
;
6179 bfd_boolean ret
= FALSE
;
6181 if (info
->traditional_format
6182 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
6183 || ! is_elf_hash_table (info
))
6186 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
6188 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6191 bed
= get_elf_backend_data (abfd
);
6193 if ((abfd
->flags
& DYNAMIC
) != 0)
6196 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
6197 if (info
->relocatable
6199 && (eh
->_raw_size
== 0
6200 || bfd_is_abs_section (eh
->output_section
))))
6203 stab
= bfd_get_section_by_name (abfd
, ".stab");
6205 && (stab
->_raw_size
== 0
6206 || bfd_is_abs_section (stab
->output_section
)
6207 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
6212 && bed
->elf_backend_discard_info
== NULL
)
6215 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6217 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
6218 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
6219 if (cookie
.bad_symtab
)
6221 cookie
.locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6222 cookie
.extsymoff
= 0;
6226 cookie
.locsymcount
= symtab_hdr
->sh_info
;
6227 cookie
.extsymoff
= symtab_hdr
->sh_info
;
6230 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6231 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
6233 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
6234 cookie
.locsymcount
, 0,
6236 if (cookie
.locsyms
== NULL
)
6243 count
= stab
->reloc_count
;
6245 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
6247 if (cookie
.rels
!= NULL
)
6249 cookie
.rel
= cookie
.rels
;
6250 cookie
.relend
= cookie
.rels
;
6251 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
6252 if (_bfd_discard_section_stabs (abfd
, stab
,
6253 elf_section_data (stab
)->sec_info
,
6254 elf_reloc_symbol_deleted_p
,
6257 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
6265 count
= eh
->reloc_count
;
6267 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
6269 cookie
.rel
= cookie
.rels
;
6270 cookie
.relend
= cookie
.rels
;
6271 if (cookie
.rels
!= NULL
)
6272 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
6274 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
6275 elf_reloc_symbol_deleted_p
,
6279 if (cookie
.rels
!= NULL
6280 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
6284 if (bed
->elf_backend_discard_info
!= NULL
6285 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
6288 if (cookie
.locsyms
!= NULL
6289 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
6291 if (! info
->keep_memory
)
6292 free (cookie
.locsyms
);
6294 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
6298 if (info
->eh_frame_hdr
6299 && !info
->relocatable
6300 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
6307 elf_section_ignore_discarded_relocs (asection
*sec
)
6309 const struct elf_backend_data
*bed
;
6311 switch (sec
->sec_info_type
)
6313 case ELF_INFO_TYPE_STABS
:
6314 case ELF_INFO_TYPE_EH_FRAME
:
6320 bed
= get_elf_backend_data (sec
->owner
);
6321 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6322 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))