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 static bfd_boolean
elf_link_add_object_symbols (bfd
*, struct bfd_link_info
*);
24 static bfd_boolean
elf_link_add_archive_symbols (bfd
*,
25 struct bfd_link_info
*);
26 static bfd_boolean
elf_finalize_dynstr (bfd
*, struct bfd_link_info
*);
27 static bfd_boolean
elf_collect_hash_codes (struct elf_link_hash_entry
*,
29 static bfd_boolean
elf_section_ignore_discarded_relocs (asection
*);
31 /* Given an ELF BFD, add symbols to the global hash table as
35 elf_bfd_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
37 switch (bfd_get_format (abfd
))
40 return elf_link_add_object_symbols (abfd
, info
);
42 return elf_link_add_archive_symbols (abfd
, info
);
44 bfd_set_error (bfd_error_wrong_format
);
49 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
51 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
52 Elf_Internal_Sym
*sym
)
54 /* Local symbols do not count, but target specific ones might. */
55 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
56 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
59 /* Function symbols do not count. */
60 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
63 /* If the section is undefined, then so is the symbol. */
64 if (sym
->st_shndx
== SHN_UNDEF
)
67 /* If the symbol is defined in the common section, then
68 it is a common definition and so does not count. */
69 if (sym
->st_shndx
== SHN_COMMON
)
72 /* If the symbol is in a target specific section then we
73 must rely upon the backend to tell us what it is. */
74 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
75 /* FIXME - this function is not coded yet:
77 return _bfd_is_global_symbol_definition (abfd, sym);
79 Instead for now assume that the definition is not global,
80 Even if this is wrong, at least the linker will behave
81 in the same way that it used to do. */
87 /* Search the symbol table of the archive element of the archive ABFD
88 whose archive map contains a mention of SYMDEF, and determine if
89 the symbol is defined in this element. */
91 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
93 Elf_Internal_Shdr
* hdr
;
94 bfd_size_type symcount
;
95 bfd_size_type extsymcount
;
96 bfd_size_type extsymoff
;
97 Elf_Internal_Sym
*isymbuf
;
98 Elf_Internal_Sym
*isym
;
99 Elf_Internal_Sym
*isymend
;
102 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
106 if (! bfd_check_format (abfd
, bfd_object
))
109 /* If we have already included the element containing this symbol in the
110 link then we do not need to include it again. Just claim that any symbol
111 it contains is not a definition, so that our caller will not decide to
112 (re)include this element. */
113 if (abfd
->archive_pass
)
116 /* Select the appropriate symbol table. */
117 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
118 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
120 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
122 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
124 /* The sh_info field of the symtab header tells us where the
125 external symbols start. We don't care about the local symbols. */
126 if (elf_bad_symtab (abfd
))
128 extsymcount
= symcount
;
133 extsymcount
= symcount
- hdr
->sh_info
;
134 extsymoff
= hdr
->sh_info
;
137 if (extsymcount
== 0)
140 /* Read in the symbol table. */
141 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
146 /* Scan the symbol table looking for SYMDEF. */
148 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
152 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
157 if (strcmp (name
, symdef
->name
) == 0)
159 result
= is_global_data_symbol_definition (abfd
, isym
);
169 /* Add symbols from an ELF archive file to the linker hash table. We
170 don't use _bfd_generic_link_add_archive_symbols because of a
171 problem which arises on UnixWare. The UnixWare libc.so is an
172 archive which includes an entry libc.so.1 which defines a bunch of
173 symbols. The libc.so archive also includes a number of other
174 object files, which also define symbols, some of which are the same
175 as those defined in libc.so.1. Correct linking requires that we
176 consider each object file in turn, and include it if it defines any
177 symbols we need. _bfd_generic_link_add_archive_symbols does not do
178 this; it looks through the list of undefined symbols, and includes
179 any object file which defines them. When this algorithm is used on
180 UnixWare, it winds up pulling in libc.so.1 early and defining a
181 bunch of symbols. This means that some of the other objects in the
182 archive are not included in the link, which is incorrect since they
183 precede libc.so.1 in the archive.
185 Fortunately, ELF archive handling is simpler than that done by
186 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
187 oddities. In ELF, if we find a symbol in the archive map, and the
188 symbol is currently undefined, we know that we must pull in that
191 Unfortunately, we do have to make multiple passes over the symbol
192 table until nothing further is resolved. */
195 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
198 bfd_boolean
*defined
= NULL
;
199 bfd_boolean
*included
= NULL
;
204 if (! bfd_has_map (abfd
))
206 /* An empty archive is a special case. */
207 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
209 bfd_set_error (bfd_error_no_armap
);
213 /* Keep track of all symbols we know to be already defined, and all
214 files we know to be already included. This is to speed up the
215 second and subsequent passes. */
216 c
= bfd_ardata (abfd
)->symdef_count
;
220 amt
*= sizeof (bfd_boolean
);
221 defined
= bfd_zmalloc (amt
);
222 included
= bfd_zmalloc (amt
);
223 if (defined
== NULL
|| included
== NULL
)
226 symdefs
= bfd_ardata (abfd
)->symdefs
;
239 symdefend
= symdef
+ c
;
240 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
242 struct elf_link_hash_entry
*h
;
244 struct bfd_link_hash_entry
*undefs_tail
;
247 if (defined
[i
] || included
[i
])
249 if (symdef
->file_offset
== last
)
255 h
= elf_link_hash_lookup (elf_hash_table (info
), symdef
->name
,
256 FALSE
, FALSE
, FALSE
);
263 /* If this is a default version (the name contains @@),
264 look up the symbol again with only one `@' as well
265 as without the version. The effect is that references
266 to the symbol with and without the version will be
267 matched by the default symbol in the archive. */
269 p
= strchr (symdef
->name
, ELF_VER_CHR
);
270 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
273 /* First check with only one `@'. */
274 len
= strlen (symdef
->name
);
275 copy
= bfd_alloc (abfd
, len
);
278 first
= p
- symdef
->name
+ 1;
279 memcpy (copy
, symdef
->name
, first
);
280 memcpy (copy
+ first
, symdef
->name
+ first
+ 1, len
- first
);
282 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
283 FALSE
, FALSE
, FALSE
);
287 /* We also need to check references to the symbol
288 without the version. */
290 copy
[first
- 1] = '\0';
291 h
= elf_link_hash_lookup (elf_hash_table (info
),
292 copy
, FALSE
, FALSE
, FALSE
);
295 bfd_release (abfd
, copy
);
301 if (h
->root
.type
== bfd_link_hash_common
)
303 /* We currently have a common symbol. The archive map contains
304 a reference to this symbol, so we may want to include it. We
305 only want to include it however, if this archive element
306 contains a definition of the symbol, not just another common
309 Unfortunately some archivers (including GNU ar) will put
310 declarations of common symbols into their archive maps, as
311 well as real definitions, so we cannot just go by the archive
312 map alone. Instead we must read in the element's symbol
313 table and check that to see what kind of symbol definition
315 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
318 else if (h
->root
.type
!= bfd_link_hash_undefined
)
320 if (h
->root
.type
!= bfd_link_hash_undefweak
)
325 /* We need to include this archive member. */
326 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
330 if (! bfd_check_format (element
, bfd_object
))
333 /* Doublecheck that we have not included this object
334 already--it should be impossible, but there may be
335 something wrong with the archive. */
336 if (element
->archive_pass
!= 0)
338 bfd_set_error (bfd_error_bad_value
);
341 element
->archive_pass
= 1;
343 undefs_tail
= info
->hash
->undefs_tail
;
345 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
348 if (! elf_link_add_object_symbols (element
, info
))
351 /* If there are any new undefined symbols, we need to make
352 another pass through the archive in order to see whether
353 they can be defined. FIXME: This isn't perfect, because
354 common symbols wind up on undefs_tail and because an
355 undefined symbol which is defined later on in this pass
356 does not require another pass. This isn't a bug, but it
357 does make the code less efficient than it could be. */
358 if (undefs_tail
!= info
->hash
->undefs_tail
)
361 /* Look backward to mark all symbols from this object file
362 which we have already seen in this pass. */
366 included
[mark
] = TRUE
;
371 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
373 /* We mark subsequent symbols from this object file as we go
374 on through the loop. */
375 last
= symdef
->file_offset
;
388 if (included
!= NULL
)
393 /* Add symbols from an ELF object file to the linker hash table. */
396 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
398 bfd_boolean (*add_symbol_hook
)
399 (bfd
*, struct bfd_link_info
*, const Elf_Internal_Sym
*,
400 const char **, flagword
*, asection
**, bfd_vma
*);
401 bfd_boolean (*check_relocs
)
402 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
404 Elf_Internal_Shdr
*hdr
;
405 bfd_size_type symcount
;
406 bfd_size_type extsymcount
;
407 bfd_size_type extsymoff
;
408 struct elf_link_hash_entry
**sym_hash
;
410 Elf_External_Versym
*extversym
= NULL
;
411 Elf_External_Versym
*ever
;
412 struct elf_link_hash_entry
*weaks
;
413 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
414 bfd_size_type nondeflt_vers_cnt
= 0;
415 Elf_Internal_Sym
*isymbuf
= NULL
;
416 Elf_Internal_Sym
*isym
;
417 Elf_Internal_Sym
*isymend
;
418 const struct elf_backend_data
*bed
;
419 bfd_boolean dt_needed
;
420 struct elf_link_hash_table
* hash_table
;
423 hash_table
= elf_hash_table (info
);
425 bed
= get_elf_backend_data (abfd
);
426 add_symbol_hook
= bed
->elf_add_symbol_hook
;
427 collect
= bed
->collect
;
429 if ((abfd
->flags
& DYNAMIC
) == 0)
435 /* You can't use -r against a dynamic object. Also, there's no
436 hope of using a dynamic object which does not exactly match
437 the format of the output file. */
438 if (info
->relocatable
|| info
->hash
->creator
!= abfd
->xvec
)
440 bfd_set_error (bfd_error_invalid_operation
);
445 /* As a GNU extension, any input sections which are named
446 .gnu.warning.SYMBOL are treated as warning symbols for the given
447 symbol. This differs from .gnu.warning sections, which generate
448 warnings when they are included in an output file. */
449 if (info
->executable
)
453 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
457 name
= bfd_get_section_name (abfd
, s
);
458 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
462 bfd_size_type prefix_len
;
463 const char * gnu_warning_prefix
= _("warning: ");
465 name
+= sizeof ".gnu.warning." - 1;
467 /* If this is a shared object, then look up the symbol
468 in the hash table. If it is there, and it is already
469 been defined, then we will not be using the entry
470 from this shared object, so we don't need to warn.
471 FIXME: If we see the definition in a regular object
472 later on, we will warn, but we shouldn't. The only
473 fix is to keep track of what warnings we are supposed
474 to emit, and then handle them all at the end of the
476 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
478 struct elf_link_hash_entry
*h
;
480 h
= elf_link_hash_lookup (hash_table
, name
,
483 /* FIXME: What about bfd_link_hash_common? */
485 && (h
->root
.type
== bfd_link_hash_defined
486 || h
->root
.type
== bfd_link_hash_defweak
))
488 /* We don't want to issue this warning. Clobber
489 the section size so that the warning does not
490 get copied into the output file. */
496 sz
= bfd_section_size (abfd
, s
);
497 prefix_len
= strlen (gnu_warning_prefix
);
498 msg
= bfd_alloc (abfd
, prefix_len
+ sz
+ 1);
502 strcpy (msg
, gnu_warning_prefix
);
503 if (! bfd_get_section_contents (abfd
, s
, msg
+ prefix_len
, 0, sz
))
506 msg
[prefix_len
+ sz
] = '\0';
508 if (! (_bfd_generic_link_add_one_symbol
509 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
510 FALSE
, collect
, NULL
)))
513 if (! info
->relocatable
)
515 /* Clobber the section size so that the warning does
516 not get copied into the output file. */
526 /* If we are creating a shared library, create all the dynamic
527 sections immediately. We need to attach them to something,
528 so we attach them to this BFD, provided it is the right
529 format. FIXME: If there are no input BFD's of the same
530 format as the output, we can't make a shared library. */
532 && is_elf_hash_table (info
)
533 && ! hash_table
->dynamic_sections_created
534 && abfd
->xvec
== info
->hash
->creator
)
536 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
540 else if (! is_elf_hash_table (info
))
545 bfd_boolean add_needed
;
547 bfd_size_type oldsize
;
548 bfd_size_type strindex
;
549 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
551 /* ld --just-symbols and dynamic objects don't mix very well.
552 Test for --just-symbols by looking at info set up by
553 _bfd_elf_link_just_syms. */
554 if ((s
= abfd
->sections
) != NULL
555 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
558 /* Find the name to use in a DT_NEEDED entry that refers to this
559 object. If the object has a DT_SONAME entry, we use it.
560 Otherwise, if the generic linker stuck something in
561 elf_dt_name, we use that. Otherwise, we just use the file
562 name. If the generic linker put a null string into
563 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
564 there is a DT_SONAME entry. */
566 name
= bfd_get_filename (abfd
);
567 if (elf_dt_name (abfd
) != NULL
)
569 name
= elf_dt_name (abfd
);
572 if (elf_dt_soname (abfd
) != NULL
)
578 s
= bfd_get_section_by_name (abfd
, ".dynamic");
581 Elf_External_Dyn
*dynbuf
= NULL
;
582 Elf_External_Dyn
*extdyn
;
583 Elf_External_Dyn
*extdynend
;
585 unsigned long shlink
;
587 dynbuf
= bfd_malloc (s
->_raw_size
);
591 if (! bfd_get_section_contents (abfd
, s
, dynbuf
, 0, s
->_raw_size
))
594 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
597 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
600 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
601 for (; extdyn
< extdynend
; extdyn
++)
603 Elf_Internal_Dyn dyn
;
605 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
606 if (dyn
.d_tag
== DT_SONAME
)
608 unsigned int tagv
= dyn
.d_un
.d_val
;
609 name
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
613 if (dyn
.d_tag
== DT_NEEDED
)
615 struct bfd_link_needed_list
*n
, **pn
;
617 unsigned int tagv
= dyn
.d_un
.d_val
;
619 amt
= sizeof (struct bfd_link_needed_list
);
620 n
= bfd_alloc (abfd
, amt
);
621 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
622 if (n
== NULL
|| fnm
== NULL
)
624 amt
= strlen (fnm
) + 1;
625 anm
= bfd_alloc (abfd
, amt
);
628 memcpy (anm
, fnm
, amt
);
632 for (pn
= & hash_table
->needed
;
638 if (dyn
.d_tag
== DT_RUNPATH
)
640 struct bfd_link_needed_list
*n
, **pn
;
642 unsigned int tagv
= dyn
.d_un
.d_val
;
644 amt
= sizeof (struct bfd_link_needed_list
);
645 n
= bfd_alloc (abfd
, amt
);
646 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
647 if (n
== NULL
|| fnm
== NULL
)
649 amt
= strlen (fnm
) + 1;
650 anm
= bfd_alloc (abfd
, amt
);
653 memcpy (anm
, fnm
, amt
);
663 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
664 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
666 struct bfd_link_needed_list
*n
, **pn
;
668 unsigned int tagv
= dyn
.d_un
.d_val
;
670 amt
= sizeof (struct bfd_link_needed_list
);
671 n
= bfd_alloc (abfd
, amt
);
672 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
673 if (n
== NULL
|| fnm
== NULL
)
675 amt
= strlen (fnm
) + 1;
676 anm
= bfd_alloc (abfd
, amt
);
683 memcpy (anm
, fnm
, amt
);
698 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
699 frees all more recently bfd_alloc'd blocks as well. */
705 struct bfd_link_needed_list
**pn
;
706 for (pn
= & hash_table
->runpath
;
713 /* We do not want to include any of the sections in a dynamic
714 object in the output file. We hack by simply clobbering the
715 list of sections in the BFD. This could be handled more
716 cleanly by, say, a new section flag; the existing
717 SEC_NEVER_LOAD flag is not the one we want, because that one
718 still implies that the section takes up space in the output
720 bfd_section_list_clear (abfd
);
722 /* If this is the first dynamic object found in the link, create
723 the special sections required for dynamic linking. */
724 if (! hash_table
->dynamic_sections_created
)
725 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
730 /* Add a DT_NEEDED entry for this dynamic object. */
731 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
732 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, name
, FALSE
);
733 if (strindex
== (bfd_size_type
) -1)
736 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
739 Elf_External_Dyn
*dyncon
, *dynconend
;
741 /* The hash table size did not change, which means that
742 the dynamic object name was already entered. If we
743 have already included this dynamic object in the
744 link, just ignore it. There is no reason to include
745 a particular dynamic object more than once. */
746 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
747 BFD_ASSERT (sdyn
!= NULL
);
749 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
750 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
752 for (; dyncon
< dynconend
; dyncon
++)
754 Elf_Internal_Dyn dyn
;
756 elf_swap_dyn_in (hash_table
->dynobj
, dyncon
, & dyn
);
757 if (dyn
.d_tag
== DT_NEEDED
758 && dyn
.d_un
.d_val
== strindex
)
760 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
766 if (! elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
770 /* Save the SONAME, if there is one, because sometimes the
771 linker emulation code will need to know it. */
773 name
= basename (bfd_get_filename (abfd
));
774 elf_dt_name (abfd
) = name
;
777 /* If this is a dynamic object, we always link against the .dynsym
778 symbol table, not the .symtab symbol table. The dynamic linker
779 will only see the .dynsym symbol table, so there is no reason to
780 look at .symtab for a dynamic object. */
782 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
783 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
785 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
787 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
789 /* The sh_info field of the symtab header tells us where the
790 external symbols start. We don't care about the local symbols at
792 if (elf_bad_symtab (abfd
))
794 extsymcount
= symcount
;
799 extsymcount
= symcount
- hdr
->sh_info
;
800 extsymoff
= hdr
->sh_info
;
804 if (extsymcount
!= 0)
806 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
811 /* We store a pointer to the hash table entry for each external
813 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
814 sym_hash
= bfd_alloc (abfd
, amt
);
815 if (sym_hash
== NULL
)
817 elf_sym_hashes (abfd
) = sym_hash
;
822 /* Read in any version definitions. */
823 if (! _bfd_elf_slurp_version_tables (abfd
))
826 /* Read in the symbol versions, but don't bother to convert them
827 to internal format. */
828 if (elf_dynversym (abfd
) != 0)
830 Elf_Internal_Shdr
*versymhdr
;
832 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
833 extversym
= bfd_malloc (versymhdr
->sh_size
);
834 if (extversym
== NULL
)
836 amt
= versymhdr
->sh_size
;
837 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
838 || bfd_bread (extversym
, amt
, abfd
) != amt
)
839 goto error_free_vers
;
845 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
846 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
848 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
855 struct elf_link_hash_entry
*h
;
856 bfd_boolean definition
;
857 bfd_boolean size_change_ok
;
858 bfd_boolean type_change_ok
;
859 bfd_boolean new_weakdef
;
860 bfd_boolean override
;
861 unsigned int old_alignment
;
866 flags
= BSF_NO_FLAGS
;
868 value
= isym
->st_value
;
871 bind
= ELF_ST_BIND (isym
->st_info
);
872 if (bind
== STB_LOCAL
)
874 /* This should be impossible, since ELF requires that all
875 global symbols follow all local symbols, and that sh_info
876 point to the first global symbol. Unfortunatealy, Irix 5
880 else if (bind
== STB_GLOBAL
)
882 if (isym
->st_shndx
!= SHN_UNDEF
883 && isym
->st_shndx
!= SHN_COMMON
)
886 else if (bind
== STB_WEAK
)
890 /* Leave it up to the processor backend. */
893 if (isym
->st_shndx
== SHN_UNDEF
)
894 sec
= bfd_und_section_ptr
;
895 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
897 sec
= section_from_elf_index (abfd
, isym
->st_shndx
);
899 sec
= bfd_abs_section_ptr
;
900 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
903 else if (isym
->st_shndx
== SHN_ABS
)
904 sec
= bfd_abs_section_ptr
;
905 else if (isym
->st_shndx
== SHN_COMMON
)
907 sec
= bfd_com_section_ptr
;
908 /* What ELF calls the size we call the value. What ELF
909 calls the value we call the alignment. */
910 value
= isym
->st_size
;
914 /* Leave it up to the processor backend. */
917 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
920 goto error_free_vers
;
922 if (isym
->st_shndx
== SHN_COMMON
923 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
925 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
929 tcomm
= bfd_make_section (abfd
, ".tcommon");
931 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
934 | SEC_THREAD_LOCAL
)))
935 goto error_free_vers
;
939 else if (add_symbol_hook
)
941 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
943 goto error_free_vers
;
945 /* The hook function sets the name to NULL if this symbol
946 should be skipped for some reason. */
951 /* Sanity check that all possibilities were handled. */
954 bfd_set_error (bfd_error_bad_value
);
955 goto error_free_vers
;
958 if (bfd_is_und_section (sec
)
959 || bfd_is_com_section (sec
))
964 size_change_ok
= FALSE
;
965 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
969 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
971 Elf_Internal_Versym iver
;
972 unsigned int vernum
= 0;
977 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
978 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
980 /* If this is a hidden symbol, or if it is not version
981 1, we append the version name to the symbol name.
982 However, we do not modify a non-hidden absolute
983 symbol, because it might be the version symbol
984 itself. FIXME: What if it isn't? */
985 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
986 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
989 size_t namelen
, verlen
, newlen
;
992 if (isym
->st_shndx
!= SHN_UNDEF
)
994 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
996 (*_bfd_error_handler
)
997 (_("%s: %s: invalid version %u (max %d)"),
998 bfd_archive_filename (abfd
), name
, vernum
,
999 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1000 bfd_set_error (bfd_error_bad_value
);
1001 goto error_free_vers
;
1003 else if (vernum
> 1)
1005 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1011 /* We cannot simply test for the number of
1012 entries in the VERNEED section since the
1013 numbers for the needed versions do not start
1015 Elf_Internal_Verneed
*t
;
1018 for (t
= elf_tdata (abfd
)->verref
;
1022 Elf_Internal_Vernaux
*a
;
1024 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1026 if (a
->vna_other
== vernum
)
1028 verstr
= a
->vna_nodename
;
1037 (*_bfd_error_handler
)
1038 (_("%s: %s: invalid needed version %d"),
1039 bfd_archive_filename (abfd
), name
, vernum
);
1040 bfd_set_error (bfd_error_bad_value
);
1041 goto error_free_vers
;
1045 namelen
= strlen (name
);
1046 verlen
= strlen (verstr
);
1047 newlen
= namelen
+ verlen
+ 2;
1048 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1049 && isym
->st_shndx
!= SHN_UNDEF
)
1052 newname
= bfd_alloc (abfd
, newlen
);
1053 if (newname
== NULL
)
1054 goto error_free_vers
;
1055 memcpy (newname
, name
, namelen
);
1056 p
= newname
+ namelen
;
1058 /* If this is a defined non-hidden version symbol,
1059 we add another @ to the name. This indicates the
1060 default version of the symbol. */
1061 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
1062 && isym
->st_shndx
!= SHN_UNDEF
)
1064 memcpy (p
, verstr
, verlen
+ 1);
1070 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
1071 sym_hash
, &skip
, &override
,
1072 &type_change_ok
, &size_change_ok
,
1074 goto error_free_vers
;
1083 while (h
->root
.type
== bfd_link_hash_indirect
1084 || h
->root
.type
== bfd_link_hash_warning
)
1085 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1087 /* Remember the old alignment if this is a common symbol, so
1088 that we don't reduce the alignment later on. We can't
1089 check later, because _bfd_generic_link_add_one_symbol
1090 will set a default for the alignment which we want to
1091 override. We also remember the old bfd where the existing
1092 definition comes from. */
1093 switch (h
->root
.type
)
1098 case bfd_link_hash_defined
:
1099 case bfd_link_hash_defweak
:
1100 old_bfd
= h
->root
.u
.def
.section
->owner
;
1103 case bfd_link_hash_common
:
1104 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
1105 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1109 if (elf_tdata (abfd
)->verdef
!= NULL
1113 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1116 if (! (_bfd_generic_link_add_one_symbol
1117 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
1118 (struct bfd_link_hash_entry
**) sym_hash
)))
1119 goto error_free_vers
;
1122 while (h
->root
.type
== bfd_link_hash_indirect
1123 || h
->root
.type
== bfd_link_hash_warning
)
1124 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1127 new_weakdef
= FALSE
;
1130 && (flags
& BSF_WEAK
) != 0
1131 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
1132 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1133 && h
->weakdef
== NULL
)
1135 /* Keep a list of all weak defined non function symbols from
1136 a dynamic object, using the weakdef field. Later in this
1137 function we will set the weakdef field to the correct
1138 value. We only put non-function symbols from dynamic
1139 objects on this list, because that happens to be the only
1140 time we need to know the normal symbol corresponding to a
1141 weak symbol, and the information is time consuming to
1142 figure out. If the weakdef field is not already NULL,
1143 then this symbol was already defined by some previous
1144 dynamic object, and we will be using that previous
1145 definition anyhow. */
1152 /* Set the alignment of a common symbol. */
1153 if (isym
->st_shndx
== SHN_COMMON
1154 && h
->root
.type
== bfd_link_hash_common
)
1158 align
= bfd_log2 (isym
->st_value
);
1159 if (align
> old_alignment
1160 /* Permit an alignment power of zero if an alignment of one
1161 is specified and no other alignments have been specified. */
1162 || (isym
->st_value
== 1 && old_alignment
== 0))
1163 h
->root
.u
.c
.p
->alignment_power
= align
;
1165 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
1168 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1174 /* Check the alignment when a common symbol is involved. This
1175 can change when a common symbol is overriden by a normal
1176 definition or a common symbol is ignored due to the old
1177 normal definition. We need to make sure the maximum
1178 alignment is maintained. */
1179 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
1180 && h
->root
.type
!= bfd_link_hash_common
)
1182 unsigned int common_align
;
1183 unsigned int normal_align
;
1184 unsigned int symbol_align
;
1188 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
1189 if ((h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
1191 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
1192 if (normal_align
> symbol_align
)
1193 normal_align
= symbol_align
;
1196 normal_align
= symbol_align
;
1200 common_align
= old_alignment
;
1201 common_bfd
= old_bfd
;
1206 common_align
= bfd_log2 (isym
->st_value
);
1208 normal_bfd
= old_bfd
;
1211 if (normal_align
< common_align
)
1212 (*_bfd_error_handler
)
1213 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
1216 bfd_archive_filename (normal_bfd
),
1218 bfd_archive_filename (common_bfd
));
1221 /* Remember the symbol size and type. */
1222 if (isym
->st_size
!= 0
1223 && (definition
|| h
->size
== 0))
1225 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
1226 (*_bfd_error_handler
)
1227 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
1228 name
, (unsigned long) h
->size
,
1229 bfd_archive_filename (old_bfd
),
1230 (unsigned long) isym
->st_size
,
1231 bfd_archive_filename (abfd
));
1233 h
->size
= isym
->st_size
;
1236 /* If this is a common symbol, then we always want H->SIZE
1237 to be the size of the common symbol. The code just above
1238 won't fix the size if a common symbol becomes larger. We
1239 don't warn about a size change here, because that is
1240 covered by --warn-common. */
1241 if (h
->root
.type
== bfd_link_hash_common
)
1242 h
->size
= h
->root
.u
.c
.size
;
1244 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
1245 && (definition
|| h
->type
== STT_NOTYPE
))
1247 if (h
->type
!= STT_NOTYPE
1248 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
1249 && ! type_change_ok
)
1250 (*_bfd_error_handler
)
1251 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1252 name
, h
->type
, ELF_ST_TYPE (isym
->st_info
),
1253 bfd_archive_filename (abfd
));
1255 h
->type
= ELF_ST_TYPE (isym
->st_info
);
1258 /* If st_other has a processor-specific meaning, specific
1259 code might be needed here. We never merge the visibility
1260 attribute with the one from a dynamic object. */
1261 if (isym
->st_other
!= 0 && !dynamic
)
1263 unsigned char hvis
, symvis
, other
, nvis
;
1265 /* Take the balance of OTHER from the definition. */
1266 other
= (definition
? isym
->st_other
: h
->other
);
1267 other
&= ~ ELF_ST_VISIBILITY (-1);
1269 /* Combine visibilities, using the most constraining one. */
1270 hvis
= ELF_ST_VISIBILITY (h
->other
);
1271 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
1277 nvis
= hvis
< symvis
? hvis
: symvis
;
1279 h
->other
= other
| nvis
;
1282 /* Set a flag in the hash table entry indicating the type of
1283 reference or definition we just found. Keep a count of
1284 the number of dynamic symbols we find. A dynamic symbol
1285 is one which is referenced or defined by both a regular
1286 object and a shared object. */
1287 old_flags
= h
->elf_link_hash_flags
;
1293 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
1294 if (bind
!= STB_WEAK
)
1295 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
1298 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
1299 if (! info
->executable
1300 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1301 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
1307 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
1309 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
1310 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
1311 | ELF_LINK_HASH_REF_REGULAR
)) != 0
1312 || (h
->weakdef
!= NULL
1314 && h
->weakdef
->dynindx
!= -1))
1318 h
->elf_link_hash_flags
|= new_flag
;
1320 /* Check to see if we need to add an indirect symbol for
1321 the default name. */
1322 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
1323 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
1324 &sec
, &value
, &dynsym
,
1325 override
, dt_needed
))
1326 goto error_free_vers
;
1328 if (definition
&& !dynamic
)
1330 char *p
= strchr (name
, ELF_VER_CHR
);
1331 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
1333 /* Queue non-default versions so that .symver x, x@FOO
1334 aliases can be checked. */
1335 if (! nondeflt_vers
)
1337 amt
= (isymend
- isym
+ 1)
1338 * sizeof (struct elf_link_hash_entry
*);
1339 nondeflt_vers
= bfd_malloc (amt
);
1341 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
1345 if (dynsym
&& h
->dynindx
== -1)
1347 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1348 goto error_free_vers
;
1349 if (h
->weakdef
!= NULL
1351 && h
->weakdef
->dynindx
== -1)
1353 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
1354 goto error_free_vers
;
1357 else if (dynsym
&& h
->dynindx
!= -1)
1358 /* If the symbol already has a dynamic index, but
1359 visibility says it should not be visible, turn it into
1361 switch (ELF_ST_VISIBILITY (h
->other
))
1365 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1369 if (dt_needed
&& definition
1370 && (h
->elf_link_hash_flags
1371 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1373 bfd_size_type oldsize
;
1374 bfd_size_type strindex
;
1376 if (! is_elf_hash_table (info
))
1377 goto error_free_vers
;
1379 /* The symbol from a DT_NEEDED object is referenced from
1380 the regular object to create a dynamic executable. We
1381 have to make sure there is a DT_NEEDED entry for it. */
1384 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
1385 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
,
1386 elf_dt_soname (abfd
), FALSE
);
1387 if (strindex
== (bfd_size_type
) -1)
1388 goto error_free_vers
;
1390 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
1393 Elf_External_Dyn
*dyncon
, *dynconend
;
1395 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
,
1397 BFD_ASSERT (sdyn
!= NULL
);
1399 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1400 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1402 for (; dyncon
< dynconend
; dyncon
++)
1404 Elf_Internal_Dyn dyn
;
1406 elf_swap_dyn_in (hash_table
->dynobj
,
1408 BFD_ASSERT (dyn
.d_tag
!= DT_NEEDED
||
1409 dyn
.d_un
.d_val
!= strindex
);
1413 if (! elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
1414 goto error_free_vers
;
1419 /* Now that all the symbols from this input file are created, handle
1420 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
1421 if (nondeflt_vers
!= NULL
)
1423 bfd_size_type cnt
, symidx
;
1425 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
1427 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
1428 char *shortname
, *p
;
1430 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1432 || (h
->root
.type
!= bfd_link_hash_defined
1433 && h
->root
.type
!= bfd_link_hash_defweak
))
1436 amt
= p
- h
->root
.root
.string
;
1437 shortname
= bfd_malloc (amt
+ 1);
1438 memcpy (shortname
, h
->root
.root
.string
, amt
);
1439 shortname
[amt
] = '\0';
1441 hi
= (struct elf_link_hash_entry
*)
1442 bfd_link_hash_lookup (info
->hash
, shortname
,
1443 FALSE
, FALSE
, FALSE
);
1445 && hi
->root
.type
== h
->root
.type
1446 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
1447 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
1449 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1450 hi
->root
.type
= bfd_link_hash_indirect
;
1451 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
1452 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1453 sym_hash
= elf_sym_hashes (abfd
);
1455 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
1456 if (sym_hash
[symidx
] == hi
)
1458 sym_hash
[symidx
] = h
;
1464 free (nondeflt_vers
);
1465 nondeflt_vers
= NULL
;
1468 if (extversym
!= NULL
)
1474 if (isymbuf
!= NULL
)
1478 /* Now set the weakdefs field correctly for all the weak defined
1479 symbols we found. The only way to do this is to search all the
1480 symbols. Since we only need the information for non functions in
1481 dynamic objects, that's the only time we actually put anything on
1482 the list WEAKS. We need this information so that if a regular
1483 object refers to a symbol defined weakly in a dynamic object, the
1484 real symbol in the dynamic object is also put in the dynamic
1485 symbols; we also must arrange for both symbols to point to the
1486 same memory location. We could handle the general case of symbol
1487 aliasing, but a general symbol alias can only be generated in
1488 assembler code, handling it correctly would be very time
1489 consuming, and other ELF linkers don't handle general aliasing
1491 while (weaks
!= NULL
)
1493 struct elf_link_hash_entry
*hlook
;
1496 struct elf_link_hash_entry
**hpp
;
1497 struct elf_link_hash_entry
**hppend
;
1500 weaks
= hlook
->weakdef
;
1501 hlook
->weakdef
= NULL
;
1503 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
1504 || hlook
->root
.type
== bfd_link_hash_defweak
1505 || hlook
->root
.type
== bfd_link_hash_common
1506 || hlook
->root
.type
== bfd_link_hash_indirect
);
1507 slook
= hlook
->root
.u
.def
.section
;
1508 vlook
= hlook
->root
.u
.def
.value
;
1510 hpp
= elf_sym_hashes (abfd
);
1511 hppend
= hpp
+ extsymcount
;
1512 for (; hpp
< hppend
; hpp
++)
1514 struct elf_link_hash_entry
*h
;
1517 if (h
!= NULL
&& h
!= hlook
1518 && h
->root
.type
== bfd_link_hash_defined
1519 && h
->root
.u
.def
.section
== slook
1520 && h
->root
.u
.def
.value
== vlook
)
1524 /* If the weak definition is in the list of dynamic
1525 symbols, make sure the real definition is put there
1527 if (hlook
->dynindx
!= -1
1528 && h
->dynindx
== -1)
1530 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1534 /* If the real definition is in the list of dynamic
1535 symbols, make sure the weak definition is put there
1536 as well. If we don't do this, then the dynamic
1537 loader might not merge the entries for the real
1538 definition and the weak definition. */
1539 if (h
->dynindx
!= -1
1540 && hlook
->dynindx
== -1)
1542 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
1550 /* If this object is the same format as the output object, and it is
1551 not a shared library, then let the backend look through the
1554 This is required to build global offset table entries and to
1555 arrange for dynamic relocs. It is not required for the
1556 particular common case of linking non PIC code, even when linking
1557 against shared libraries, but unfortunately there is no way of
1558 knowing whether an object file has been compiled PIC or not.
1559 Looking through the relocs is not particularly time consuming.
1560 The problem is that we must either (1) keep the relocs in memory,
1561 which causes the linker to require additional runtime memory or
1562 (2) read the relocs twice from the input file, which wastes time.
1563 This would be a good case for using mmap.
1565 I have no idea how to handle linking PIC code into a file of a
1566 different format. It probably can't be done. */
1567 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
1569 && abfd
->xvec
== info
->hash
->creator
1570 && check_relocs
!= NULL
)
1574 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1576 Elf_Internal_Rela
*internal_relocs
;
1579 if ((o
->flags
& SEC_RELOC
) == 0
1580 || o
->reloc_count
== 0
1581 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
1582 && (o
->flags
& SEC_DEBUGGING
) != 0)
1583 || bfd_is_abs_section (o
->output_section
))
1586 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
1588 if (internal_relocs
== NULL
)
1591 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
1593 if (elf_section_data (o
)->relocs
!= internal_relocs
)
1594 free (internal_relocs
);
1601 /* If this is a non-traditional link, try to optimize the handling
1602 of the .stab/.stabstr sections. */
1604 && ! info
->traditional_format
1605 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1606 && is_elf_hash_table (info
)
1607 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
1609 asection
*stab
, *stabstr
;
1611 stab
= bfd_get_section_by_name (abfd
, ".stab");
1613 && (stab
->flags
& SEC_MERGE
) == 0
1614 && !bfd_is_abs_section (stab
->output_section
))
1616 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
1618 if (stabstr
!= NULL
)
1620 struct bfd_elf_section_data
*secdata
;
1622 secdata
= elf_section_data (stab
);
1623 if (! _bfd_link_section_stabs (abfd
,
1624 & hash_table
->stab_info
,
1626 &secdata
->sec_info
))
1628 if (secdata
->sec_info
)
1629 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
1634 if (! info
->relocatable
&& ! dynamic
1635 && is_elf_hash_table (info
))
1639 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
1640 if ((s
->flags
& SEC_MERGE
) != 0
1641 && !bfd_is_abs_section (s
->output_section
))
1643 struct bfd_elf_section_data
*secdata
;
1645 secdata
= elf_section_data (s
);
1646 if (! _bfd_merge_section (abfd
,
1647 & hash_table
->merge_info
,
1648 s
, &secdata
->sec_info
))
1650 else if (secdata
->sec_info
)
1651 s
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
1655 if (is_elf_hash_table (info
))
1657 /* Add this bfd to the loaded list. */
1658 struct elf_link_loaded_list
*n
;
1660 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
1664 n
->next
= hash_table
->loaded
;
1665 hash_table
->loaded
= n
;
1671 if (nondeflt_vers
!= NULL
)
1672 free (nondeflt_vers
);
1673 if (extversym
!= NULL
)
1676 if (isymbuf
!= NULL
)
1682 /* Add an entry to the .dynamic table. */
1685 elf_add_dynamic_entry (struct bfd_link_info
*info
, bfd_vma tag
, bfd_vma val
)
1687 Elf_Internal_Dyn dyn
;
1690 bfd_size_type newsize
;
1691 bfd_byte
*newcontents
;
1693 if (! is_elf_hash_table (info
))
1696 dynobj
= elf_hash_table (info
)->dynobj
;
1698 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
1699 BFD_ASSERT (s
!= NULL
);
1701 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
1702 newcontents
= bfd_realloc (s
->contents
, newsize
);
1703 if (newcontents
== NULL
)
1707 dyn
.d_un
.d_val
= val
;
1708 elf_swap_dyn_out (dynobj
, &dyn
,
1709 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
1711 s
->_raw_size
= newsize
;
1712 s
->contents
= newcontents
;
1717 /* Array used to determine the number of hash table buckets to use
1718 based on the number of symbols there are. If there are fewer than
1719 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
1720 fewer than 37 we use 17 buckets, and so forth. We never use more
1721 than 32771 buckets. */
1723 static const size_t elf_buckets
[] =
1725 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
1729 /* Compute bucket count for hashing table. We do not use a static set
1730 of possible tables sizes anymore. Instead we determine for all
1731 possible reasonable sizes of the table the outcome (i.e., the
1732 number of collisions etc) and choose the best solution. The
1733 weighting functions are not too simple to allow the table to grow
1734 without bounds. Instead one of the weighting factors is the size.
1735 Therefore the result is always a good payoff between few collisions
1736 (= short chain lengths) and table size. */
1738 compute_bucket_count (struct bfd_link_info
*info
)
1740 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
1741 size_t best_size
= 0;
1742 unsigned long int *hashcodes
;
1743 unsigned long int *hashcodesp
;
1744 unsigned long int i
;
1747 /* Compute the hash values for all exported symbols. At the same
1748 time store the values in an array so that we could use them for
1751 amt
*= sizeof (unsigned long int);
1752 hashcodes
= bfd_malloc (amt
);
1753 if (hashcodes
== NULL
)
1755 hashcodesp
= hashcodes
;
1757 /* Put all hash values in HASHCODES. */
1758 elf_link_hash_traverse (elf_hash_table (info
),
1759 elf_collect_hash_codes
, &hashcodesp
);
1761 /* We have a problem here. The following code to optimize the table
1762 size requires an integer type with more the 32 bits. If
1763 BFD_HOST_U_64_BIT is set we know about such a type. */
1764 #ifdef BFD_HOST_U_64_BIT
1767 unsigned long int nsyms
= hashcodesp
- hashcodes
;
1770 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
1771 unsigned long int *counts
;
1773 /* Possible optimization parameters: if we have NSYMS symbols we say
1774 that the hashing table must at least have NSYMS/4 and at most
1776 minsize
= nsyms
/ 4;
1779 best_size
= maxsize
= nsyms
* 2;
1781 /* Create array where we count the collisions in. We must use bfd_malloc
1782 since the size could be large. */
1784 amt
*= sizeof (unsigned long int);
1785 counts
= bfd_malloc (amt
);
1792 /* Compute the "optimal" size for the hash table. The criteria is a
1793 minimal chain length. The minor criteria is (of course) the size
1795 for (i
= minsize
; i
< maxsize
; ++i
)
1797 /* Walk through the array of hashcodes and count the collisions. */
1798 BFD_HOST_U_64_BIT max
;
1799 unsigned long int j
;
1800 unsigned long int fact
;
1802 memset (counts
, '\0', i
* sizeof (unsigned long int));
1804 /* Determine how often each hash bucket is used. */
1805 for (j
= 0; j
< nsyms
; ++j
)
1806 ++counts
[hashcodes
[j
] % i
];
1808 /* For the weight function we need some information about the
1809 pagesize on the target. This is information need not be 100%
1810 accurate. Since this information is not available (so far) we
1811 define it here to a reasonable default value. If it is crucial
1812 to have a better value some day simply define this value. */
1813 # ifndef BFD_TARGET_PAGESIZE
1814 # define BFD_TARGET_PAGESIZE (4096)
1817 /* We in any case need 2 + NSYMS entries for the size values and
1819 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
1822 /* Variant 1: optimize for short chains. We add the squares
1823 of all the chain lengths (which favous many small chain
1824 over a few long chains). */
1825 for (j
= 0; j
< i
; ++j
)
1826 max
+= counts
[j
] * counts
[j
];
1828 /* This adds penalties for the overall size of the table. */
1829 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
1832 /* Variant 2: Optimize a lot more for small table. Here we
1833 also add squares of the size but we also add penalties for
1834 empty slots (the +1 term). */
1835 for (j
= 0; j
< i
; ++j
)
1836 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
1838 /* The overall size of the table is considered, but not as
1839 strong as in variant 1, where it is squared. */
1840 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
1844 /* Compare with current best results. */
1845 if (max
< best_chlen
)
1855 #endif /* defined (BFD_HOST_U_64_BIT) */
1857 /* This is the fallback solution if no 64bit type is available or if we
1858 are not supposed to spend much time on optimizations. We select the
1859 bucket count using a fixed set of numbers. */
1860 for (i
= 0; elf_buckets
[i
] != 0; i
++)
1862 best_size
= elf_buckets
[i
];
1863 if (dynsymcount
< elf_buckets
[i
+ 1])
1868 /* Free the arrays we needed. */
1874 /* Set up the sizes and contents of the ELF dynamic sections. This is
1875 called by the ELF linker emulation before_allocation routine. We
1876 must set the sizes of the sections before the linker sets the
1877 addresses of the various sections. */
1880 NAME(bfd_elf
,size_dynamic_sections
) (bfd
*output_bfd
,
1883 const char *filter_shlib
,
1884 const char * const *auxiliary_filters
,
1885 struct bfd_link_info
*info
,
1886 asection
**sinterpptr
,
1887 struct bfd_elf_version_tree
*verdefs
)
1889 bfd_size_type soname_indx
;
1891 const struct elf_backend_data
*bed
;
1892 struct elf_assign_sym_version_info asvinfo
;
1896 soname_indx
= (bfd_size_type
) -1;
1898 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1901 if (! is_elf_hash_table (info
))
1904 if (info
->execstack
)
1905 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
1906 else if (info
->noexecstack
)
1907 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
1911 asection
*notesec
= NULL
;
1914 for (inputobj
= info
->input_bfds
;
1916 inputobj
= inputobj
->link_next
)
1920 if (inputobj
->flags
& DYNAMIC
)
1922 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
1925 if (s
->flags
& SEC_CODE
)
1934 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
1935 if (exec
&& info
->relocatable
1936 && notesec
->output_section
!= bfd_abs_section_ptr
)
1937 notesec
->output_section
->flags
|= SEC_CODE
;
1941 /* Any syms created from now on start with -1 in
1942 got.refcount/offset and plt.refcount/offset. */
1943 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
1945 /* The backend may have to create some sections regardless of whether
1946 we're dynamic or not. */
1947 bed
= get_elf_backend_data (output_bfd
);
1948 if (bed
->elf_backend_always_size_sections
1949 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
1952 dynobj
= elf_hash_table (info
)->dynobj
;
1954 /* If there were no dynamic objects in the link, there is nothing to
1959 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
1962 if (elf_hash_table (info
)->dynamic_sections_created
)
1964 struct elf_info_failed eif
;
1965 struct elf_link_hash_entry
*h
;
1967 struct bfd_elf_version_tree
*t
;
1968 struct bfd_elf_version_expr
*d
;
1969 bfd_boolean all_defined
;
1971 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
1972 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
1976 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
1978 if (soname_indx
== (bfd_size_type
) -1
1979 || ! elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
1985 if (! elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
1987 info
->flags
|= DF_SYMBOLIC
;
1994 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
1996 if (info
->new_dtags
)
1997 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
1998 if (indx
== (bfd_size_type
) -1
1999 || ! elf_add_dynamic_entry (info
, DT_RPATH
, indx
)
2001 && ! elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
)))
2005 if (filter_shlib
!= NULL
)
2009 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2010 filter_shlib
, TRUE
);
2011 if (indx
== (bfd_size_type
) -1
2012 || ! elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
2016 if (auxiliary_filters
!= NULL
)
2018 const char * const *p
;
2020 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
2024 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2026 if (indx
== (bfd_size_type
) -1
2027 || ! elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
2033 eif
.verdefs
= verdefs
;
2036 /* If we are supposed to export all symbols into the dynamic symbol
2037 table (this is not the normal case), then do so. */
2038 if (info
->export_dynamic
)
2040 elf_link_hash_traverse (elf_hash_table (info
),
2041 _bfd_elf_export_symbol
,
2047 /* Make all global versions with definiton. */
2048 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2049 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
2050 if (!d
->symver
&& strchr (d
->pattern
, '*') == NULL
)
2052 const char *verstr
, *name
;
2053 size_t namelen
, verlen
, newlen
;
2055 struct elf_link_hash_entry
*newh
;
2058 namelen
= strlen (name
);
2060 verlen
= strlen (verstr
);
2061 newlen
= namelen
+ verlen
+ 3;
2063 newname
= bfd_malloc (newlen
);
2064 if (newname
== NULL
)
2066 memcpy (newname
, name
, namelen
);
2068 /* Check the hidden versioned definition. */
2069 p
= newname
+ namelen
;
2071 memcpy (p
, verstr
, verlen
+ 1);
2072 newh
= elf_link_hash_lookup (elf_hash_table (info
),
2073 newname
, FALSE
, FALSE
,
2076 || (newh
->root
.type
!= bfd_link_hash_defined
2077 && newh
->root
.type
!= bfd_link_hash_defweak
))
2079 /* Check the default versioned definition. */
2081 memcpy (p
, verstr
, verlen
+ 1);
2082 newh
= elf_link_hash_lookup (elf_hash_table (info
),
2083 newname
, FALSE
, FALSE
,
2088 /* Mark this version if there is a definition and it is
2089 not defined in a shared object. */
2091 && ((newh
->elf_link_hash_flags
2092 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)
2093 && (newh
->root
.type
== bfd_link_hash_defined
2094 || newh
->root
.type
== bfd_link_hash_defweak
))
2098 /* Attach all the symbols to their version information. */
2099 asvinfo
.output_bfd
= output_bfd
;
2100 asvinfo
.info
= info
;
2101 asvinfo
.verdefs
= verdefs
;
2102 asvinfo
.failed
= FALSE
;
2104 elf_link_hash_traverse (elf_hash_table (info
),
2105 _bfd_elf_link_assign_sym_version
,
2110 if (!info
->allow_undefined_version
)
2112 /* Check if all global versions have a definiton. */
2114 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2115 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
2116 if (!d
->symver
&& !d
->script
2117 && strchr (d
->pattern
, '*') == NULL
)
2119 (*_bfd_error_handler
)
2120 (_("%s: undefined version: %s"),
2121 d
->pattern
, t
->name
);
2122 all_defined
= FALSE
;
2127 bfd_set_error (bfd_error_bad_value
);
2132 /* Find all symbols which were defined in a dynamic object and make
2133 the backend pick a reasonable value for them. */
2134 elf_link_hash_traverse (elf_hash_table (info
),
2135 _bfd_elf_adjust_dynamic_symbol
,
2140 /* Add some entries to the .dynamic section. We fill in some of the
2141 values later, in elf_bfd_final_link, but we must add the entries
2142 now so that we know the final size of the .dynamic section. */
2144 /* If there are initialization and/or finalization functions to
2145 call then add the corresponding DT_INIT/DT_FINI entries. */
2146 h
= (info
->init_function
2147 ? elf_link_hash_lookup (elf_hash_table (info
),
2148 info
->init_function
, FALSE
,
2152 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2153 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2155 if (! elf_add_dynamic_entry (info
, DT_INIT
, 0))
2158 h
= (info
->fini_function
2159 ? elf_link_hash_lookup (elf_hash_table (info
),
2160 info
->fini_function
, FALSE
,
2164 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2165 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2167 if (! elf_add_dynamic_entry (info
, DT_FINI
, 0))
2171 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
2173 /* DT_PREINIT_ARRAY is not allowed in shared library. */
2174 if (! info
->executable
)
2179 for (sub
= info
->input_bfds
; sub
!= NULL
;
2180 sub
= sub
->link_next
)
2181 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2182 if (elf_section_data (o
)->this_hdr
.sh_type
2183 == SHT_PREINIT_ARRAY
)
2185 (*_bfd_error_handler
)
2186 (_("%s: .preinit_array section is not allowed in DSO"),
2187 bfd_archive_filename (sub
));
2191 bfd_set_error (bfd_error_nonrepresentable_section
);
2195 if (!elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
2196 || !elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
2199 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
2201 if (!elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
2202 || !elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
2205 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
2207 if (!elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
2208 || !elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
2212 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
2213 /* If .dynstr is excluded from the link, we don't want any of
2214 these tags. Strictly, we should be checking each section
2215 individually; This quick check covers for the case where
2216 someone does a /DISCARD/ : { *(*) }. */
2217 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
2219 bfd_size_type strsize
;
2221 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
2222 if (! elf_add_dynamic_entry (info
, DT_HASH
, 0)
2223 || ! elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
2224 || ! elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
2225 || ! elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
2226 || ! elf_add_dynamic_entry (info
, DT_SYMENT
,
2227 sizeof (Elf_External_Sym
)))
2232 /* The backend must work out the sizes of all the other dynamic
2234 if (bed
->elf_backend_size_dynamic_sections
2235 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
2238 if (elf_hash_table (info
)->dynamic_sections_created
)
2240 bfd_size_type dynsymcount
;
2242 size_t bucketcount
= 0;
2243 size_t hash_entry_size
;
2244 unsigned int dtagcount
;
2246 /* Set up the version definition section. */
2247 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2248 BFD_ASSERT (s
!= NULL
);
2250 /* We may have created additional version definitions if we are
2251 just linking a regular application. */
2252 verdefs
= asvinfo
.verdefs
;
2254 /* Skip anonymous version tag. */
2255 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
2256 verdefs
= verdefs
->next
;
2258 if (verdefs
== NULL
)
2259 _bfd_strip_section_from_output (info
, s
);
2264 struct bfd_elf_version_tree
*t
;
2266 Elf_Internal_Verdef def
;
2267 Elf_Internal_Verdaux defaux
;
2272 /* Make space for the base version. */
2273 size
+= sizeof (Elf_External_Verdef
);
2274 size
+= sizeof (Elf_External_Verdaux
);
2277 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2279 struct bfd_elf_version_deps
*n
;
2281 size
+= sizeof (Elf_External_Verdef
);
2282 size
+= sizeof (Elf_External_Verdaux
);
2285 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2286 size
+= sizeof (Elf_External_Verdaux
);
2289 s
->_raw_size
= size
;
2290 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
2291 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2294 /* Fill in the version definition section. */
2298 def
.vd_version
= VER_DEF_CURRENT
;
2299 def
.vd_flags
= VER_FLG_BASE
;
2302 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2303 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2304 + sizeof (Elf_External_Verdaux
));
2306 if (soname_indx
!= (bfd_size_type
) -1)
2308 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
2310 def
.vd_hash
= bfd_elf_hash (soname
);
2311 defaux
.vda_name
= soname_indx
;
2318 name
= basename (output_bfd
->filename
);
2319 def
.vd_hash
= bfd_elf_hash (name
);
2320 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2322 if (indx
== (bfd_size_type
) -1)
2324 defaux
.vda_name
= indx
;
2326 defaux
.vda_next
= 0;
2328 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2329 (Elf_External_Verdef
*) p
);
2330 p
+= sizeof (Elf_External_Verdef
);
2331 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2332 (Elf_External_Verdaux
*) p
);
2333 p
+= sizeof (Elf_External_Verdaux
);
2335 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2338 struct bfd_elf_version_deps
*n
;
2339 struct elf_link_hash_entry
*h
;
2340 struct bfd_link_hash_entry
*bh
;
2343 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2346 /* Add a symbol representing this version. */
2348 if (! (_bfd_generic_link_add_one_symbol
2349 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
2351 get_elf_backend_data (dynobj
)->collect
, &bh
)))
2353 h
= (struct elf_link_hash_entry
*) bh
;
2354 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
2355 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2356 h
->type
= STT_OBJECT
;
2357 h
->verinfo
.vertree
= t
;
2359 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2362 def
.vd_version
= VER_DEF_CURRENT
;
2364 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
2365 def
.vd_flags
|= VER_FLG_WEAK
;
2366 def
.vd_ndx
= t
->vernum
+ 1;
2367 def
.vd_cnt
= cdeps
+ 1;
2368 def
.vd_hash
= bfd_elf_hash (t
->name
);
2369 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2370 if (t
->next
!= NULL
)
2371 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2372 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
2376 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2377 (Elf_External_Verdef
*) p
);
2378 p
+= sizeof (Elf_External_Verdef
);
2380 defaux
.vda_name
= h
->dynstr_index
;
2381 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
2383 if (t
->deps
== NULL
)
2384 defaux
.vda_next
= 0;
2386 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2387 t
->name_indx
= defaux
.vda_name
;
2389 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2390 (Elf_External_Verdaux
*) p
);
2391 p
+= sizeof (Elf_External_Verdaux
);
2393 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2395 if (n
->version_needed
== NULL
)
2397 /* This can happen if there was an error in the
2399 defaux
.vda_name
= 0;
2403 defaux
.vda_name
= n
->version_needed
->name_indx
;
2404 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
2407 if (n
->next
== NULL
)
2408 defaux
.vda_next
= 0;
2410 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2412 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2413 (Elf_External_Verdaux
*) p
);
2414 p
+= sizeof (Elf_External_Verdaux
);
2418 if (! elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
2419 || ! elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
2422 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
2425 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
2427 if (! elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
2433 if (info
->executable
)
2434 info
->flags_1
&= ~ (DF_1_INITFIRST
2437 if (! elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
2441 /* Work out the size of the version reference section. */
2443 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2444 BFD_ASSERT (s
!= NULL
);
2446 struct elf_find_verdep_info sinfo
;
2448 sinfo
.output_bfd
= output_bfd
;
2450 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
2451 if (sinfo
.vers
== 0)
2453 sinfo
.failed
= FALSE
;
2455 elf_link_hash_traverse (elf_hash_table (info
),
2456 _bfd_elf_link_find_version_dependencies
,
2459 if (elf_tdata (output_bfd
)->verref
== NULL
)
2460 _bfd_strip_section_from_output (info
, s
);
2463 Elf_Internal_Verneed
*t
;
2468 /* Build the version definition section. */
2471 for (t
= elf_tdata (output_bfd
)->verref
;
2475 Elf_Internal_Vernaux
*a
;
2477 size
+= sizeof (Elf_External_Verneed
);
2479 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2480 size
+= sizeof (Elf_External_Vernaux
);
2483 s
->_raw_size
= size
;
2484 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
2485 if (s
->contents
== NULL
)
2489 for (t
= elf_tdata (output_bfd
)->verref
;
2494 Elf_Internal_Vernaux
*a
;
2498 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2501 t
->vn_version
= VER_NEED_CURRENT
;
2503 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2504 elf_dt_name (t
->vn_bfd
) != NULL
2505 ? elf_dt_name (t
->vn_bfd
)
2506 : basename (t
->vn_bfd
->filename
),
2508 if (indx
== (bfd_size_type
) -1)
2511 t
->vn_aux
= sizeof (Elf_External_Verneed
);
2512 if (t
->vn_nextref
== NULL
)
2515 t
->vn_next
= (sizeof (Elf_External_Verneed
)
2516 + caux
* sizeof (Elf_External_Vernaux
));
2518 _bfd_elf_swap_verneed_out (output_bfd
, t
,
2519 (Elf_External_Verneed
*) p
);
2520 p
+= sizeof (Elf_External_Verneed
);
2522 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2524 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
2525 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
2526 a
->vna_nodename
, FALSE
);
2527 if (indx
== (bfd_size_type
) -1)
2530 if (a
->vna_nextptr
== NULL
)
2533 a
->vna_next
= sizeof (Elf_External_Vernaux
);
2535 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
2536 (Elf_External_Vernaux
*) p
);
2537 p
+= sizeof (Elf_External_Vernaux
);
2541 if (! elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
2542 || ! elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
2545 elf_tdata (output_bfd
)->cverrefs
= crefs
;
2549 /* Assign dynsym indicies. In a shared library we generate a
2550 section symbol for each output section, which come first.
2551 Next come all of the back-end allocated local dynamic syms,
2552 followed by the rest of the global symbols. */
2554 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
2556 /* Work out the size of the symbol version section. */
2557 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
2558 BFD_ASSERT (s
!= NULL
);
2559 if (dynsymcount
== 0
2560 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
2562 _bfd_strip_section_from_output (info
, s
);
2563 /* The DYNSYMCOUNT might have changed if we were going to
2564 output a dynamic symbol table entry for S. */
2565 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
2569 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
2570 s
->contents
= bfd_zalloc (output_bfd
, s
->_raw_size
);
2571 if (s
->contents
== NULL
)
2574 if (! elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
2578 /* Set the size of the .dynsym and .hash sections. We counted
2579 the number of dynamic symbols in elf_link_add_object_symbols.
2580 We will build the contents of .dynsym and .hash when we build
2581 the final symbol table, because until then we do not know the
2582 correct value to give the symbols. We built the .dynstr
2583 section as we went along in elf_link_add_object_symbols. */
2584 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
2585 BFD_ASSERT (s
!= NULL
);
2586 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
2587 s
->contents
= bfd_alloc (output_bfd
, s
->_raw_size
);
2588 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2591 if (dynsymcount
!= 0)
2593 Elf_Internal_Sym isym
;
2595 /* The first entry in .dynsym is a dummy symbol. */
2602 elf_swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
2605 /* Compute the size of the hashing table. As a side effect this
2606 computes the hash values for all the names we export. */
2607 bucketcount
= compute_bucket_count (info
);
2609 s
= bfd_get_section_by_name (dynobj
, ".hash");
2610 BFD_ASSERT (s
!= NULL
);
2611 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
2612 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
2613 s
->contents
= bfd_zalloc (output_bfd
, s
->_raw_size
);
2614 if (s
->contents
== NULL
)
2617 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
2618 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
2619 s
->contents
+ hash_entry_size
);
2621 elf_hash_table (info
)->bucketcount
= bucketcount
;
2623 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
2624 BFD_ASSERT (s
!= NULL
);
2626 elf_finalize_dynstr (output_bfd
, info
);
2628 s
->_raw_size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
2630 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
2631 if (! elf_add_dynamic_entry (info
, DT_NULL
, 0))
2638 /* This function is used to adjust offsets into .dynstr for
2639 dynamic symbols. This is called via elf_link_hash_traverse. */
2642 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2644 struct elf_strtab_hash
*dynstr
= data
;
2646 if (h
->root
.type
== bfd_link_hash_warning
)
2647 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2649 if (h
->dynindx
!= -1)
2650 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2654 /* Assign string offsets in .dynstr, update all structures referencing
2658 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2660 struct elf_link_local_dynamic_entry
*entry
;
2661 struct elf_strtab_hash
*dynstr
= elf_hash_table (info
)->dynstr
;
2662 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
2665 Elf_External_Dyn
*dyncon
, *dynconend
;
2667 _bfd_elf_strtab_finalize (dynstr
);
2668 size
= _bfd_elf_strtab_size (dynstr
);
2670 /* Update all .dynamic entries referencing .dynstr strings. */
2671 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2672 BFD_ASSERT (sdyn
!= NULL
);
2674 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
2675 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
2677 for (; dyncon
< dynconend
; dyncon
++)
2679 Elf_Internal_Dyn dyn
;
2681 elf_swap_dyn_in (dynobj
, dyncon
, & dyn
);
2685 dyn
.d_un
.d_val
= size
;
2686 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
2694 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2695 elf_swap_dyn_out (dynobj
, & dyn
, dyncon
);
2702 /* Now update local dynamic symbols. */
2703 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
2704 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2705 entry
->isym
.st_name
);
2707 /* And the rest of dynamic symbols. */
2708 elf_link_hash_traverse (elf_hash_table (info
),
2709 elf_adjust_dynstr_offsets
, dynstr
);
2711 /* Adjust version definitions. */
2712 if (elf_tdata (output_bfd
)->cverdefs
)
2717 Elf_Internal_Verdef def
;
2718 Elf_Internal_Verdaux defaux
;
2720 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2721 p
= (bfd_byte
*) s
->contents
;
2724 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
2726 p
+= sizeof (Elf_External_Verdef
);
2727 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2729 _bfd_elf_swap_verdaux_in (output_bfd
,
2730 (Elf_External_Verdaux
*) p
, &defaux
);
2731 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2733 _bfd_elf_swap_verdaux_out (output_bfd
,
2734 &defaux
, (Elf_External_Verdaux
*) p
);
2735 p
+= sizeof (Elf_External_Verdaux
);
2738 while (def
.vd_next
);
2741 /* Adjust version references. */
2742 if (elf_tdata (output_bfd
)->verref
)
2747 Elf_Internal_Verneed need
;
2748 Elf_Internal_Vernaux needaux
;
2750 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2751 p
= (bfd_byte
*) s
->contents
;
2754 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
2756 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
2757 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
2758 (Elf_External_Verneed
*) p
);
2759 p
+= sizeof (Elf_External_Verneed
);
2760 for (i
= 0; i
< need
.vn_cnt
; ++i
)
2762 _bfd_elf_swap_vernaux_in (output_bfd
,
2763 (Elf_External_Vernaux
*) p
, &needaux
);
2764 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
2766 _bfd_elf_swap_vernaux_out (output_bfd
,
2768 (Elf_External_Vernaux
*) p
);
2769 p
+= sizeof (Elf_External_Vernaux
);
2772 while (need
.vn_next
);
2778 /* Final phase of ELF linker. */
2780 /* A structure we use to avoid passing large numbers of arguments. */
2782 struct elf_final_link_info
2784 /* General link information. */
2785 struct bfd_link_info
*info
;
2788 /* Symbol string table. */
2789 struct bfd_strtab_hash
*symstrtab
;
2790 /* .dynsym section. */
2791 asection
*dynsym_sec
;
2792 /* .hash section. */
2794 /* symbol version section (.gnu.version). */
2795 asection
*symver_sec
;
2796 /* first SHF_TLS section (if any). */
2797 asection
*first_tls_sec
;
2798 /* Buffer large enough to hold contents of any section. */
2800 /* Buffer large enough to hold external relocs of any section. */
2801 void *external_relocs
;
2802 /* Buffer large enough to hold internal relocs of any section. */
2803 Elf_Internal_Rela
*internal_relocs
;
2804 /* Buffer large enough to hold external local symbols of any input
2806 Elf_External_Sym
*external_syms
;
2807 /* And a buffer for symbol section indices. */
2808 Elf_External_Sym_Shndx
*locsym_shndx
;
2809 /* Buffer large enough to hold internal local symbols of any input
2811 Elf_Internal_Sym
*internal_syms
;
2812 /* Array large enough to hold a symbol index for each local symbol
2813 of any input BFD. */
2815 /* Array large enough to hold a section pointer for each local
2816 symbol of any input BFD. */
2817 asection
**sections
;
2818 /* Buffer to hold swapped out symbols. */
2819 Elf_External_Sym
*symbuf
;
2820 /* And one for symbol section indices. */
2821 Elf_External_Sym_Shndx
*symshndxbuf
;
2822 /* Number of swapped out symbols in buffer. */
2823 size_t symbuf_count
;
2824 /* Number of symbols which fit in symbuf. */
2826 /* And same for symshndxbuf. */
2827 size_t shndxbuf_size
;
2830 static bfd_boolean elf_link_output_sym
2831 (struct elf_final_link_info
*, const char *, Elf_Internal_Sym
*, asection
*);
2832 static bfd_boolean elf_link_flush_output_syms
2833 (struct elf_final_link_info
*);
2834 static bfd_boolean elf_link_output_extsym
2835 (struct elf_link_hash_entry
*, void *);
2836 static bfd_boolean elf_link_input_bfd
2837 (struct elf_final_link_info
*, bfd
*);
2838 static bfd_boolean elf_reloc_link_order
2839 (bfd
*, struct bfd_link_info
*, asection
*, struct bfd_link_order
*);
2841 /* This struct is used to pass information to elf_link_output_extsym. */
2843 struct elf_outext_info
2846 bfd_boolean localsyms
;
2847 struct elf_final_link_info
*finfo
;
2850 /* When performing a relocatable link, the input relocations are
2851 preserved. But, if they reference global symbols, the indices
2852 referenced must be updated. Update all the relocations in
2853 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
2856 elf_link_adjust_relocs (bfd
*abfd
,
2857 Elf_Internal_Shdr
*rel_hdr
,
2859 struct elf_link_hash_entry
**rel_hash
)
2862 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2864 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2865 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2867 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2869 swap_in
= bed
->s
->swap_reloc_in
;
2870 swap_out
= bed
->s
->swap_reloc_out
;
2872 else if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
))
2874 swap_in
= bed
->s
->swap_reloca_in
;
2875 swap_out
= bed
->s
->swap_reloca_out
;
2880 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
2883 erela
= rel_hdr
->contents
;
2884 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
2886 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
2889 if (*rel_hash
== NULL
)
2892 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
2894 (*swap_in
) (abfd
, erela
, irela
);
2895 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
2896 irela
[j
].r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
2897 ELF_R_TYPE (irela
[j
].r_info
));
2898 (*swap_out
) (abfd
, irela
, erela
);
2902 struct elf_link_sort_rela
2905 enum elf_reloc_type_class type
;
2906 /* We use this as an array of size int_rels_per_ext_rel. */
2907 Elf_Internal_Rela rela
[1];
2911 elf_link_sort_cmp1 (const void *A
, const void *B
)
2913 const struct elf_link_sort_rela
*a
= A
;
2914 const struct elf_link_sort_rela
*b
= B
;
2915 int relativea
, relativeb
;
2917 relativea
= a
->type
== reloc_class_relative
;
2918 relativeb
= b
->type
== reloc_class_relative
;
2920 if (relativea
< relativeb
)
2922 if (relativea
> relativeb
)
2924 if (ELF_R_SYM (a
->rela
->r_info
) < ELF_R_SYM (b
->rela
->r_info
))
2926 if (ELF_R_SYM (a
->rela
->r_info
) > ELF_R_SYM (b
->rela
->r_info
))
2928 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
2930 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
2936 elf_link_sort_cmp2 (const void *A
, const void *B
)
2938 const struct elf_link_sort_rela
*a
= A
;
2939 const struct elf_link_sort_rela
*b
= B
;
2942 if (a
->offset
< b
->offset
)
2944 if (a
->offset
> b
->offset
)
2946 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
2947 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
2952 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
2954 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
2960 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
2963 bfd_size_type count
, size
;
2964 size_t i
, ret
, sort_elt
, ext_size
;
2965 bfd_byte
*sort
, *s_non_relative
, *p
;
2966 struct elf_link_sort_rela
*sq
;
2967 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2968 int i2e
= bed
->s
->int_rels_per_ext_rel
;
2969 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2970 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2971 struct bfd_link_order
*lo
;
2973 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
2974 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
2976 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
2977 if (reldyn
== NULL
|| reldyn
->_raw_size
== 0)
2979 ext_size
= sizeof (Elf_External_Rel
);
2980 swap_in
= bed
->s
->swap_reloc_in
;
2981 swap_out
= bed
->s
->swap_reloc_out
;
2985 ext_size
= sizeof (Elf_External_Rela
);
2986 swap_in
= bed
->s
->swap_reloca_in
;
2987 swap_out
= bed
->s
->swap_reloca_out
;
2989 count
= reldyn
->_raw_size
/ ext_size
;
2992 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
2993 if (lo
->type
== bfd_indirect_link_order
)
2995 asection
*o
= lo
->u
.indirect
.section
;
2996 size
+= o
->_raw_size
;
2999 if (size
!= reldyn
->_raw_size
)
3002 sort_elt
= (sizeof (struct elf_link_sort_rela
)
3003 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
3004 sort
= bfd_zmalloc (sort_elt
* count
);
3007 (*info
->callbacks
->warning
)
3008 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
3012 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
3013 if (lo
->type
== bfd_indirect_link_order
)
3015 bfd_byte
*erel
, *erelend
;
3016 asection
*o
= lo
->u
.indirect
.section
;
3019 erelend
= o
->contents
+ o
->_raw_size
;
3020 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
3021 while (erel
< erelend
)
3023 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
3024 (*swap_in
) (abfd
, erel
, s
->rela
);
3025 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
3031 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
3033 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
3035 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
3036 if (s
->type
!= reloc_class_relative
)
3042 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
3043 for (; i
< count
; i
++, p
+= sort_elt
)
3045 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
3046 if (ELF_R_SYM (sp
->rela
->r_info
) != ELF_R_SYM (sq
->rela
->r_info
))
3048 sp
->offset
= sq
->rela
->r_offset
;
3051 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
3053 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
3054 if (lo
->type
== bfd_indirect_link_order
)
3056 bfd_byte
*erel
, *erelend
;
3057 asection
*o
= lo
->u
.indirect
.section
;
3060 erelend
= o
->contents
+ o
->_raw_size
;
3061 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
3062 while (erel
< erelend
)
3064 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
3065 (*swap_out
) (abfd
, s
->rela
, erel
);
3076 /* Do the final step of an ELF link. */
3079 elf_bfd_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
3081 bfd_boolean dynamic
;
3082 bfd_boolean emit_relocs
;
3084 struct elf_final_link_info finfo
;
3085 register asection
*o
;
3086 register struct bfd_link_order
*p
;
3088 bfd_size_type max_contents_size
;
3089 bfd_size_type max_external_reloc_size
;
3090 bfd_size_type max_internal_reloc_count
;
3091 bfd_size_type max_sym_count
;
3092 bfd_size_type max_sym_shndx_count
;
3094 Elf_Internal_Sym elfsym
;
3096 Elf_Internal_Shdr
*symtab_hdr
;
3097 Elf_Internal_Shdr
*symtab_shndx_hdr
;
3098 Elf_Internal_Shdr
*symstrtab_hdr
;
3099 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3100 struct elf_outext_info eoinfo
;
3102 size_t relativecount
= 0;
3103 asection
*reldyn
= 0;
3106 if (! is_elf_hash_table (info
))
3110 abfd
->flags
|= DYNAMIC
;
3112 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
3113 dynobj
= elf_hash_table (info
)->dynobj
;
3115 emit_relocs
= (info
->relocatable
3116 || info
->emitrelocations
3117 || bed
->elf_backend_emit_relocs
);
3120 finfo
.output_bfd
= abfd
;
3121 finfo
.symstrtab
= elf_stringtab_init ();
3122 if (finfo
.symstrtab
== NULL
)
3127 finfo
.dynsym_sec
= NULL
;
3128 finfo
.hash_sec
= NULL
;
3129 finfo
.symver_sec
= NULL
;
3133 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
3134 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
3135 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
3136 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3137 /* Note that it is OK if symver_sec is NULL. */
3140 finfo
.contents
= NULL
;
3141 finfo
.external_relocs
= NULL
;
3142 finfo
.internal_relocs
= NULL
;
3143 finfo
.external_syms
= NULL
;
3144 finfo
.locsym_shndx
= NULL
;
3145 finfo
.internal_syms
= NULL
;
3146 finfo
.indices
= NULL
;
3147 finfo
.sections
= NULL
;
3148 finfo
.symbuf
= NULL
;
3149 finfo
.symshndxbuf
= NULL
;
3150 finfo
.symbuf_count
= 0;
3151 finfo
.shndxbuf_size
= 0;
3152 finfo
.first_tls_sec
= NULL
;
3153 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3154 if ((o
->flags
& SEC_THREAD_LOCAL
) != 0
3155 && (o
->flags
& SEC_LOAD
) != 0)
3157 finfo
.first_tls_sec
= o
;
3161 /* Count up the number of relocations we will output for each output
3162 section, so that we know the sizes of the reloc sections. We
3163 also figure out some maximum sizes. */
3164 max_contents_size
= 0;
3165 max_external_reloc_size
= 0;
3166 max_internal_reloc_count
= 0;
3168 max_sym_shndx_count
= 0;
3170 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3172 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
3175 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3177 unsigned int reloc_count
= 0;
3178 struct bfd_elf_section_data
*esdi
= NULL
;
3179 unsigned int *rel_count1
;
3181 if (p
->type
== bfd_section_reloc_link_order
3182 || p
->type
== bfd_symbol_reloc_link_order
)
3184 else if (p
->type
== bfd_indirect_link_order
)
3188 sec
= p
->u
.indirect
.section
;
3189 esdi
= elf_section_data (sec
);
3191 /* Mark all sections which are to be included in the
3192 link. This will normally be every section. We need
3193 to do this so that we can identify any sections which
3194 the linker has decided to not include. */
3195 sec
->linker_mark
= TRUE
;
3197 if (sec
->flags
& SEC_MERGE
)
3200 if (info
->relocatable
|| info
->emitrelocations
)
3201 reloc_count
= sec
->reloc_count
;
3202 else if (bed
->elf_backend_count_relocs
)
3204 Elf_Internal_Rela
* relocs
;
3206 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
3209 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
3211 if (elf_section_data (o
)->relocs
!= relocs
)
3215 if (sec
->_raw_size
> max_contents_size
)
3216 max_contents_size
= sec
->_raw_size
;
3217 if (sec
->_cooked_size
> max_contents_size
)
3218 max_contents_size
= sec
->_cooked_size
;
3220 /* We are interested in just local symbols, not all
3222 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
3223 && (sec
->owner
->flags
& DYNAMIC
) == 0)
3227 if (elf_bad_symtab (sec
->owner
))
3228 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
3229 / sizeof (Elf_External_Sym
));
3231 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
3233 if (sym_count
> max_sym_count
)
3234 max_sym_count
= sym_count
;
3236 if (sym_count
> max_sym_shndx_count
3237 && elf_symtab_shndx (sec
->owner
) != 0)
3238 max_sym_shndx_count
= sym_count
;
3240 if ((sec
->flags
& SEC_RELOC
) != 0)
3244 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
3245 if (ext_size
> max_external_reloc_size
)
3246 max_external_reloc_size
= ext_size
;
3247 if (sec
->reloc_count
> max_internal_reloc_count
)
3248 max_internal_reloc_count
= sec
->reloc_count
;
3253 if (reloc_count
== 0)
3256 o
->reloc_count
+= reloc_count
;
3258 /* MIPS may have a mix of REL and RELA relocs on sections.
3259 To support this curious ABI we keep reloc counts in
3260 elf_section_data too. We must be careful to add the
3261 relocations from the input section to the right output
3262 count. FIXME: Get rid of one count. We have
3263 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
3264 rel_count1
= &esdo
->rel_count
;
3267 bfd_boolean same_size
;
3268 bfd_size_type entsize1
;
3270 entsize1
= esdi
->rel_hdr
.sh_entsize
;
3271 BFD_ASSERT (entsize1
== sizeof (Elf_External_Rel
)
3272 || entsize1
== sizeof (Elf_External_Rela
));
3273 same_size
= (!o
->use_rela_p
3274 == (entsize1
== sizeof (Elf_External_Rel
)));
3277 rel_count1
= &esdo
->rel_count2
;
3279 if (esdi
->rel_hdr2
!= NULL
)
3281 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
3282 unsigned int alt_count
;
3283 unsigned int *rel_count2
;
3285 BFD_ASSERT (entsize2
!= entsize1
3286 && (entsize2
== sizeof (Elf_External_Rel
)
3287 || entsize2
== sizeof (Elf_External_Rela
)));
3289 rel_count2
= &esdo
->rel_count2
;
3291 rel_count2
= &esdo
->rel_count
;
3293 /* The following is probably too simplistic if the
3294 backend counts output relocs unusually. */
3295 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
3296 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
3297 *rel_count2
+= alt_count
;
3298 reloc_count
-= alt_count
;
3301 *rel_count1
+= reloc_count
;
3304 if (o
->reloc_count
> 0)
3305 o
->flags
|= SEC_RELOC
;
3308 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3309 set it (this is probably a bug) and if it is set
3310 assign_section_numbers will create a reloc section. */
3311 o
->flags
&=~ SEC_RELOC
;
3314 /* If the SEC_ALLOC flag is not set, force the section VMA to
3315 zero. This is done in elf_fake_sections as well, but forcing
3316 the VMA to 0 here will ensure that relocs against these
3317 sections are handled correctly. */
3318 if ((o
->flags
& SEC_ALLOC
) == 0
3319 && ! o
->user_set_vma
)
3323 if (! info
->relocatable
&& merged
)
3324 elf_link_hash_traverse (elf_hash_table (info
),
3325 _bfd_elf_link_sec_merge_syms
, abfd
);
3327 /* Figure out the file positions for everything but the symbol table
3328 and the relocs. We set symcount to force assign_section_numbers
3329 to create a symbol table. */
3330 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
3331 BFD_ASSERT (! abfd
->output_has_begun
);
3332 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
3335 /* That created the reloc sections. Set their sizes, and assign
3336 them file positions, and allocate some buffers. */
3337 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3339 if ((o
->flags
& SEC_RELOC
) != 0)
3341 if (!(_bfd_elf_link_size_reloc_section
3342 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
3345 if (elf_section_data (o
)->rel_hdr2
3346 && !(_bfd_elf_link_size_reloc_section
3347 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
3351 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
3352 to count upwards while actually outputting the relocations. */
3353 elf_section_data (o
)->rel_count
= 0;
3354 elf_section_data (o
)->rel_count2
= 0;
3357 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3359 /* We have now assigned file positions for all the sections except
3360 .symtab and .strtab. We start the .symtab section at the current
3361 file position, and write directly to it. We build the .strtab
3362 section in memory. */
3363 bfd_get_symcount (abfd
) = 0;
3364 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3365 /* sh_name is set in prep_headers. */
3366 symtab_hdr
->sh_type
= SHT_SYMTAB
;
3367 /* sh_flags, sh_addr and sh_size all start off zero. */
3368 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
3369 /* sh_link is set in assign_section_numbers. */
3370 /* sh_info is set below. */
3371 /* sh_offset is set just below. */
3372 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
3374 off
= elf_tdata (abfd
)->next_file_pos
;
3375 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
3377 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3378 incorrect. We do not yet know the size of the .symtab section.
3379 We correct next_file_pos below, after we do know the size. */
3381 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3382 continuously seeking to the right position in the file. */
3383 if (! info
->keep_memory
|| max_sym_count
< 20)
3384 finfo
.symbuf_size
= 20;
3386 finfo
.symbuf_size
= max_sym_count
;
3387 amt
= finfo
.symbuf_size
;
3388 amt
*= sizeof (Elf_External_Sym
);
3389 finfo
.symbuf
= bfd_malloc (amt
);
3390 if (finfo
.symbuf
== NULL
)
3392 if (elf_numsections (abfd
) > SHN_LORESERVE
)
3394 /* Wild guess at number of output symbols. realloc'd as needed. */
3395 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
3396 finfo
.shndxbuf_size
= amt
;
3397 amt
*= sizeof (Elf_External_Sym_Shndx
);
3398 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
3399 if (finfo
.symshndxbuf
== NULL
)
3403 /* Start writing out the symbol table. The first symbol is always a
3405 if (info
->strip
!= strip_all
3408 elfsym
.st_value
= 0;
3411 elfsym
.st_other
= 0;
3412 elfsym
.st_shndx
= SHN_UNDEF
;
3413 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
))
3418 /* Some standard ELF linkers do this, but we don't because it causes
3419 bootstrap comparison failures. */
3420 /* Output a file symbol for the output file as the second symbol.
3421 We output this even if we are discarding local symbols, although
3422 I'm not sure if this is correct. */
3423 elfsym
.st_value
= 0;
3425 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
3426 elfsym
.st_other
= 0;
3427 elfsym
.st_shndx
= SHN_ABS
;
3428 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
3429 &elfsym
, bfd_abs_section_ptr
))
3433 /* Output a symbol for each section. We output these even if we are
3434 discarding local symbols, since they are used for relocs. These
3435 symbols have no names. We store the index of each one in the
3436 index field of the section, so that we can find it again when
3437 outputting relocs. */
3438 if (info
->strip
!= strip_all
3442 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
3443 elfsym
.st_other
= 0;
3444 for (i
= 1; i
< elf_numsections (abfd
); i
++)
3446 o
= section_from_elf_index (abfd
, i
);
3448 o
->target_index
= bfd_get_symcount (abfd
);
3449 elfsym
.st_shndx
= i
;
3450 if (info
->relocatable
|| o
== NULL
)
3451 elfsym
.st_value
= 0;
3453 elfsym
.st_value
= o
->vma
;
3454 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
))
3456 if (i
== SHN_LORESERVE
- 1)
3457 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3461 /* Allocate some memory to hold information read in from the input
3463 if (max_contents_size
!= 0)
3465 finfo
.contents
= bfd_malloc (max_contents_size
);
3466 if (finfo
.contents
== NULL
)
3470 if (max_external_reloc_size
!= 0)
3472 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
3473 if (finfo
.external_relocs
== NULL
)
3477 if (max_internal_reloc_count
!= 0)
3479 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
3480 amt
*= sizeof (Elf_Internal_Rela
);
3481 finfo
.internal_relocs
= bfd_malloc (amt
);
3482 if (finfo
.internal_relocs
== NULL
)
3486 if (max_sym_count
!= 0)
3488 amt
= max_sym_count
* sizeof (Elf_External_Sym
);
3489 finfo
.external_syms
= bfd_malloc (amt
);
3490 if (finfo
.external_syms
== NULL
)
3493 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
3494 finfo
.internal_syms
= bfd_malloc (amt
);
3495 if (finfo
.internal_syms
== NULL
)
3498 amt
= max_sym_count
* sizeof (long);
3499 finfo
.indices
= bfd_malloc (amt
);
3500 if (finfo
.indices
== NULL
)
3503 amt
= max_sym_count
* sizeof (asection
*);
3504 finfo
.sections
= bfd_malloc (amt
);
3505 if (finfo
.sections
== NULL
)
3509 if (max_sym_shndx_count
!= 0)
3511 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
3512 finfo
.locsym_shndx
= bfd_malloc (amt
);
3513 if (finfo
.locsym_shndx
== NULL
)
3517 if (finfo
.first_tls_sec
)
3519 unsigned int align
= 0;
3520 bfd_vma base
= finfo
.first_tls_sec
->vma
, end
= 0;
3523 for (sec
= finfo
.first_tls_sec
;
3524 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
3527 bfd_vma size
= sec
->_raw_size
;
3529 if (bfd_get_section_alignment (abfd
, sec
) > align
)
3530 align
= bfd_get_section_alignment (abfd
, sec
);
3531 if (sec
->_raw_size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
3533 struct bfd_link_order
*o
;
3536 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
3537 if (size
< o
->offset
+ o
->size
)
3538 size
= o
->offset
+ o
->size
;
3540 end
= sec
->vma
+ size
;
3542 elf_hash_table (info
)->tls_segment
3543 = bfd_zalloc (abfd
, sizeof (struct elf_link_tls_segment
));
3544 if (elf_hash_table (info
)->tls_segment
== NULL
)
3546 elf_hash_table (info
)->tls_segment
->start
= base
;
3547 elf_hash_table (info
)->tls_segment
->size
= end
- base
;
3548 elf_hash_table (info
)->tls_segment
->align
= align
;
3551 /* Since ELF permits relocations to be against local symbols, we
3552 must have the local symbols available when we do the relocations.
3553 Since we would rather only read the local symbols once, and we
3554 would rather not keep them in memory, we handle all the
3555 relocations for a single input file at the same time.
3557 Unfortunately, there is no way to know the total number of local
3558 symbols until we have seen all of them, and the local symbol
3559 indices precede the global symbol indices. This means that when
3560 we are generating relocatable output, and we see a reloc against
3561 a global symbol, we can not know the symbol index until we have
3562 finished examining all the local symbols to see which ones we are
3563 going to output. To deal with this, we keep the relocations in
3564 memory, and don't output them until the end of the link. This is
3565 an unfortunate waste of memory, but I don't see a good way around
3566 it. Fortunately, it only happens when performing a relocatable
3567 link, which is not the common case. FIXME: If keep_memory is set
3568 we could write the relocs out and then read them again; I don't
3569 know how bad the memory loss will be. */
3571 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3572 sub
->output_has_begun
= FALSE
;
3573 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3575 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3577 if (p
->type
== bfd_indirect_link_order
3578 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
3579 == bfd_target_elf_flavour
)
3580 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
3582 if (! sub
->output_has_begun
)
3584 if (! elf_link_input_bfd (&finfo
, sub
))
3586 sub
->output_has_begun
= TRUE
;
3589 else if (p
->type
== bfd_section_reloc_link_order
3590 || p
->type
== bfd_symbol_reloc_link_order
)
3592 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
3597 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
3603 /* Output any global symbols that got converted to local in a
3604 version script or due to symbol visibility. We do this in a
3605 separate step since ELF requires all local symbols to appear
3606 prior to any global symbols. FIXME: We should only do this if
3607 some global symbols were, in fact, converted to become local.
3608 FIXME: Will this work correctly with the Irix 5 linker? */
3609 eoinfo
.failed
= FALSE
;
3610 eoinfo
.finfo
= &finfo
;
3611 eoinfo
.localsyms
= TRUE
;
3612 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3617 /* That wrote out all the local symbols. Finish up the symbol table
3618 with the global symbols. Even if we want to strip everything we
3619 can, we still need to deal with those global symbols that got
3620 converted to local in a version script. */
3622 /* The sh_info field records the index of the first non local symbol. */
3623 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
3626 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
3628 Elf_Internal_Sym sym
;
3629 Elf_External_Sym
*dynsym
=
3630 (Elf_External_Sym
*) finfo
.dynsym_sec
->contents
;
3631 long last_local
= 0;
3633 /* Write out the section symbols for the output sections. */
3640 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
3643 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3646 Elf_External_Sym
*dest
;
3648 indx
= elf_section_data (s
)->this_idx
;
3649 BFD_ASSERT (indx
> 0);
3650 sym
.st_shndx
= indx
;
3651 sym
.st_value
= s
->vma
;
3652 dest
= dynsym
+ elf_section_data (s
)->dynindx
;
3653 elf_swap_symbol_out (abfd
, &sym
, dest
, 0);
3656 last_local
= bfd_count_sections (abfd
);
3659 /* Write out the local dynsyms. */
3660 if (elf_hash_table (info
)->dynlocal
)
3662 struct elf_link_local_dynamic_entry
*e
;
3663 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
3666 Elf_External_Sym
*dest
;
3668 sym
.st_size
= e
->isym
.st_size
;
3669 sym
.st_other
= e
->isym
.st_other
;
3671 /* Copy the internal symbol as is.
3672 Note that we saved a word of storage and overwrote
3673 the original st_name with the dynstr_index. */
3676 if (e
->isym
.st_shndx
!= SHN_UNDEF
3677 && (e
->isym
.st_shndx
< SHN_LORESERVE
3678 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
3680 s
= bfd_section_from_elf_index (e
->input_bfd
,
3684 elf_section_data (s
->output_section
)->this_idx
;
3685 sym
.st_value
= (s
->output_section
->vma
3687 + e
->isym
.st_value
);
3690 if (last_local
< e
->dynindx
)
3691 last_local
= e
->dynindx
;
3693 dest
= dynsym
+ e
->dynindx
;
3694 elf_swap_symbol_out (abfd
, &sym
, dest
, 0);
3698 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
3702 /* We get the global symbols from the hash table. */
3703 eoinfo
.failed
= FALSE
;
3704 eoinfo
.localsyms
= FALSE
;
3705 eoinfo
.finfo
= &finfo
;
3706 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3711 /* If backend needs to output some symbols not present in the hash
3712 table, do it now. */
3713 if (bed
->elf_backend_output_arch_syms
)
3715 typedef bfd_boolean (*out_sym_func
)
3716 (void *, const char *, Elf_Internal_Sym
*, asection
*);
3718 if (! ((*bed
->elf_backend_output_arch_syms
)
3719 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
3723 /* Flush all symbols to the file. */
3724 if (! elf_link_flush_output_syms (&finfo
))
3727 /* Now we know the size of the symtab section. */
3728 off
+= symtab_hdr
->sh_size
;
3730 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3731 if (symtab_shndx_hdr
->sh_name
!= 0)
3733 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
3734 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
3735 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
3736 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
3737 symtab_shndx_hdr
->sh_size
= amt
;
3739 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
3742 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
3743 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
3748 /* Finish up and write out the symbol string table (.strtab)
3750 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3751 /* sh_name was set in prep_headers. */
3752 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
3753 symstrtab_hdr
->sh_flags
= 0;
3754 symstrtab_hdr
->sh_addr
= 0;
3755 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
3756 symstrtab_hdr
->sh_entsize
= 0;
3757 symstrtab_hdr
->sh_link
= 0;
3758 symstrtab_hdr
->sh_info
= 0;
3759 /* sh_offset is set just below. */
3760 symstrtab_hdr
->sh_addralign
= 1;
3762 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
3763 elf_tdata (abfd
)->next_file_pos
= off
;
3765 if (bfd_get_symcount (abfd
) > 0)
3767 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
3768 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
3772 /* Adjust the relocs to have the correct symbol indices. */
3773 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3775 if ((o
->flags
& SEC_RELOC
) == 0)
3778 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
3779 elf_section_data (o
)->rel_count
,
3780 elf_section_data (o
)->rel_hashes
);
3781 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
3782 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
3783 elf_section_data (o
)->rel_count2
,
3784 (elf_section_data (o
)->rel_hashes
3785 + elf_section_data (o
)->rel_count
));
3787 /* Set the reloc_count field to 0 to prevent write_relocs from
3788 trying to swap the relocs out itself. */
3792 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
3793 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
3795 /* If we are linking against a dynamic object, or generating a
3796 shared library, finish up the dynamic linking information. */
3799 Elf_External_Dyn
*dyncon
, *dynconend
;
3801 /* Fix up .dynamic entries. */
3802 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
3803 BFD_ASSERT (o
!= NULL
);
3805 dyncon
= (Elf_External_Dyn
*) o
->contents
;
3806 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
3807 for (; dyncon
< dynconend
; dyncon
++)
3809 Elf_Internal_Dyn dyn
;
3813 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
3820 if (relativecount
> 0 && dyncon
+ 1 < dynconend
)
3822 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
3824 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
3825 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
3828 if (dyn
.d_tag
!= DT_NULL
)
3830 dyn
.d_un
.d_val
= relativecount
;
3831 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3837 name
= info
->init_function
;
3840 name
= info
->fini_function
;
3843 struct elf_link_hash_entry
*h
;
3845 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
3846 FALSE
, FALSE
, TRUE
);
3848 && (h
->root
.type
== bfd_link_hash_defined
3849 || h
->root
.type
== bfd_link_hash_defweak
))
3851 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
3852 o
= h
->root
.u
.def
.section
;
3853 if (o
->output_section
!= NULL
)
3854 dyn
.d_un
.d_val
+= (o
->output_section
->vma
3855 + o
->output_offset
);
3858 /* The symbol is imported from another shared
3859 library and does not apply to this one. */
3863 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3868 case DT_PREINIT_ARRAYSZ
:
3869 name
= ".preinit_array";
3871 case DT_INIT_ARRAYSZ
:
3872 name
= ".init_array";
3874 case DT_FINI_ARRAYSZ
:
3875 name
= ".fini_array";
3877 o
= bfd_get_section_by_name (abfd
, name
);
3880 (*_bfd_error_handler
)
3881 (_("%s: could not find output section %s"),
3882 bfd_get_filename (abfd
), name
);
3885 if (o
->_raw_size
== 0)
3886 (*_bfd_error_handler
)
3887 (_("warning: %s section has zero size"), name
);
3888 dyn
.d_un
.d_val
= o
->_raw_size
;
3889 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3892 case DT_PREINIT_ARRAY
:
3893 name
= ".preinit_array";
3896 name
= ".init_array";
3899 name
= ".fini_array";
3912 name
= ".gnu.version_d";
3915 name
= ".gnu.version_r";
3918 name
= ".gnu.version";
3920 o
= bfd_get_section_by_name (abfd
, name
);
3923 (*_bfd_error_handler
)
3924 (_("%s: could not find output section %s"),
3925 bfd_get_filename (abfd
), name
);
3928 dyn
.d_un
.d_ptr
= o
->vma
;
3929 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3936 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
3941 for (i
= 1; i
< elf_numsections (abfd
); i
++)
3943 Elf_Internal_Shdr
*hdr
;
3945 hdr
= elf_elfsections (abfd
)[i
];
3946 if (hdr
->sh_type
== type
3947 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
3949 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
3950 dyn
.d_un
.d_val
+= hdr
->sh_size
;
3953 if (dyn
.d_un
.d_val
== 0
3954 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
3955 dyn
.d_un
.d_val
= hdr
->sh_addr
;
3959 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3965 /* If we have created any dynamic sections, then output them. */
3968 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
3971 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
3973 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
3974 || o
->_raw_size
== 0
3975 || o
->output_section
== bfd_abs_section_ptr
)
3977 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
3979 /* At this point, we are only interested in sections
3980 created by _bfd_elf_link_create_dynamic_sections. */
3983 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
3985 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
3987 if (! bfd_set_section_contents (abfd
, o
->output_section
,
3989 (file_ptr
) o
->output_offset
,
3995 /* The contents of the .dynstr section are actually in a
3997 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
3998 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
3999 || ! _bfd_elf_strtab_emit (abfd
,
4000 elf_hash_table (info
)->dynstr
))
4006 if (info
->relocatable
)
4008 bfd_boolean failed
= FALSE
;
4010 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4015 /* If we have optimized stabs strings, output them. */
4016 if (elf_hash_table (info
)->stab_info
!= NULL
)
4018 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
4022 if (info
->eh_frame_hdr
)
4024 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
4028 if (finfo
.symstrtab
!= NULL
)
4029 _bfd_stringtab_free (finfo
.symstrtab
);
4030 if (finfo
.contents
!= NULL
)
4031 free (finfo
.contents
);
4032 if (finfo
.external_relocs
!= NULL
)
4033 free (finfo
.external_relocs
);
4034 if (finfo
.internal_relocs
!= NULL
)
4035 free (finfo
.internal_relocs
);
4036 if (finfo
.external_syms
!= NULL
)
4037 free (finfo
.external_syms
);
4038 if (finfo
.locsym_shndx
!= NULL
)
4039 free (finfo
.locsym_shndx
);
4040 if (finfo
.internal_syms
!= NULL
)
4041 free (finfo
.internal_syms
);
4042 if (finfo
.indices
!= NULL
)
4043 free (finfo
.indices
);
4044 if (finfo
.sections
!= NULL
)
4045 free (finfo
.sections
);
4046 if (finfo
.symbuf
!= NULL
)
4047 free (finfo
.symbuf
);
4048 if (finfo
.symshndxbuf
!= NULL
)
4049 free (finfo
.symshndxbuf
);
4050 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4052 if ((o
->flags
& SEC_RELOC
) != 0
4053 && elf_section_data (o
)->rel_hashes
!= NULL
)
4054 free (elf_section_data (o
)->rel_hashes
);
4057 elf_tdata (abfd
)->linker
= TRUE
;
4062 if (finfo
.symstrtab
!= NULL
)
4063 _bfd_stringtab_free (finfo
.symstrtab
);
4064 if (finfo
.contents
!= NULL
)
4065 free (finfo
.contents
);
4066 if (finfo
.external_relocs
!= NULL
)
4067 free (finfo
.external_relocs
);
4068 if (finfo
.internal_relocs
!= NULL
)
4069 free (finfo
.internal_relocs
);
4070 if (finfo
.external_syms
!= NULL
)
4071 free (finfo
.external_syms
);
4072 if (finfo
.locsym_shndx
!= NULL
)
4073 free (finfo
.locsym_shndx
);
4074 if (finfo
.internal_syms
!= NULL
)
4075 free (finfo
.internal_syms
);
4076 if (finfo
.indices
!= NULL
)
4077 free (finfo
.indices
);
4078 if (finfo
.sections
!= NULL
)
4079 free (finfo
.sections
);
4080 if (finfo
.symbuf
!= NULL
)
4081 free (finfo
.symbuf
);
4082 if (finfo
.symshndxbuf
!= NULL
)
4083 free (finfo
.symshndxbuf
);
4084 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4086 if ((o
->flags
& SEC_RELOC
) != 0
4087 && elf_section_data (o
)->rel_hashes
!= NULL
)
4088 free (elf_section_data (o
)->rel_hashes
);
4094 /* Add a symbol to the output symbol table. */
4097 elf_link_output_sym (struct elf_final_link_info
*finfo
,
4099 Elf_Internal_Sym
*elfsym
,
4100 asection
*input_sec
)
4102 Elf_External_Sym
*dest
;
4103 Elf_External_Sym_Shndx
*destshndx
;
4104 bfd_boolean (*output_symbol_hook
)
4105 (bfd
*, struct bfd_link_info
*info
, const char *,
4106 Elf_Internal_Sym
*, asection
*);
4108 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
4109 elf_backend_link_output_symbol_hook
;
4110 if (output_symbol_hook
!= NULL
)
4112 if (! ((*output_symbol_hook
)
4113 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
4117 if (name
== NULL
|| *name
== '\0')
4118 elfsym
->st_name
= 0;
4119 else if (input_sec
->flags
& SEC_EXCLUDE
)
4120 elfsym
->st_name
= 0;
4123 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
4125 if (elfsym
->st_name
== (unsigned long) -1)
4129 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
4131 if (! elf_link_flush_output_syms (finfo
))
4135 dest
= finfo
->symbuf
+ finfo
->symbuf_count
;
4136 destshndx
= finfo
->symshndxbuf
;
4137 if (destshndx
!= NULL
)
4139 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
4143 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
4144 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
4145 if (destshndx
== NULL
)
4147 memset ((char *) destshndx
+ amt
, 0, amt
);
4148 finfo
->shndxbuf_size
*= 2;
4150 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
4153 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
4154 finfo
->symbuf_count
+= 1;
4155 bfd_get_symcount (finfo
->output_bfd
) += 1;
4160 /* Flush the output symbols to the file. */
4163 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
)
4165 if (finfo
->symbuf_count
> 0)
4167 Elf_Internal_Shdr
*hdr
;
4171 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
4172 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
4173 amt
= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
4174 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
4175 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
4178 hdr
->sh_size
+= amt
;
4179 finfo
->symbuf_count
= 0;
4185 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
4186 allowing an unsatisfied unversioned symbol in the DSO to match a
4187 versioned symbol that would normally require an explicit version.
4188 We also handle the case that a DSO references a hidden symbol
4189 which may be satisfied by a versioned symbol in another DSO. */
4192 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
4193 struct elf_link_hash_entry
*h
)
4196 struct elf_link_loaded_list
*loaded
;
4198 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
4201 switch (h
->root
.type
)
4207 case bfd_link_hash_undefined
:
4208 case bfd_link_hash_undefweak
:
4209 abfd
= h
->root
.u
.undef
.abfd
;
4210 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dt_soname (abfd
) == NULL
)
4214 case bfd_link_hash_defined
:
4215 case bfd_link_hash_defweak
:
4216 abfd
= h
->root
.u
.def
.section
->owner
;
4219 case bfd_link_hash_common
:
4220 abfd
= h
->root
.u
.c
.p
->section
->owner
;
4223 BFD_ASSERT (abfd
!= NULL
);
4225 for (loaded
= elf_hash_table (info
)->loaded
;
4227 loaded
= loaded
->next
)
4230 Elf_Internal_Shdr
*hdr
;
4231 bfd_size_type symcount
;
4232 bfd_size_type extsymcount
;
4233 bfd_size_type extsymoff
;
4234 Elf_Internal_Shdr
*versymhdr
;
4235 Elf_Internal_Sym
*isym
;
4236 Elf_Internal_Sym
*isymend
;
4237 Elf_Internal_Sym
*isymbuf
;
4238 Elf_External_Versym
*ever
;
4239 Elf_External_Versym
*extversym
;
4241 input
= loaded
->abfd
;
4243 /* We check each DSO for a possible hidden versioned definition. */
4245 || (input
->flags
& DYNAMIC
) == 0
4246 || elf_dynversym (input
) == 0)
4249 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
4251 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
4252 if (elf_bad_symtab (input
))
4254 extsymcount
= symcount
;
4259 extsymcount
= symcount
- hdr
->sh_info
;
4260 extsymoff
= hdr
->sh_info
;
4263 if (extsymcount
== 0)
4266 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
4268 if (isymbuf
== NULL
)
4271 /* Read in any version definitions. */
4272 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
4273 extversym
= bfd_malloc (versymhdr
->sh_size
);
4274 if (extversym
== NULL
)
4277 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
4278 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
4279 != versymhdr
->sh_size
))
4287 ever
= extversym
+ extsymoff
;
4288 isymend
= isymbuf
+ extsymcount
;
4289 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
4292 Elf_Internal_Versym iver
;
4293 unsigned short version_index
;
4295 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
4296 || isym
->st_shndx
== SHN_UNDEF
)
4299 name
= bfd_elf_string_from_elf_section (input
,
4302 if (strcmp (name
, h
->root
.root
.string
) != 0)
4305 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
4307 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
4309 /* If we have a non-hidden versioned sym, then it should
4310 have provided a definition for the undefined sym. */
4314 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
4315 if (version_index
== 1 || version_index
== 2)
4317 /* This is the base or first version. We can use it. */
4331 /* Add an external symbol to the symbol table. This is called from
4332 the hash table traversal routine. When generating a shared object,
4333 we go through the symbol table twice. The first time we output
4334 anything that might have been forced to local scope in a version
4335 script. The second time we output the symbols that are still
4339 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
4341 struct elf_outext_info
*eoinfo
= data
;
4342 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
4344 Elf_Internal_Sym sym
;
4345 asection
*input_sec
;
4347 if (h
->root
.type
== bfd_link_hash_warning
)
4349 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4350 if (h
->root
.type
== bfd_link_hash_new
)
4354 /* Decide whether to output this symbol in this pass. */
4355 if (eoinfo
->localsyms
)
4357 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
4362 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4366 /* If we have an undefined symbol reference here then it must have
4367 come from a shared library that is being linked in. (Undefined
4368 references in regular files have already been handled). If we
4369 are reporting errors for this situation then do so now. */
4370 if (h
->root
.type
== bfd_link_hash_undefined
4371 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
4372 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
4373 && ! elf_link_check_versioned_symbol (finfo
->info
, h
)
4374 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
4376 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
4377 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
4378 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
4380 eoinfo
->failed
= TRUE
;
4385 /* We should also warn if a forced local symbol is referenced from
4386 shared libraries. */
4387 if (! finfo
->info
->relocatable
4388 && (! finfo
->info
->shared
)
4389 && (h
->elf_link_hash_flags
4390 & (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
| ELF_LINK_DYNAMIC_DEF
| ELF_LINK_DYNAMIC_WEAK
))
4391 == (ELF_LINK_FORCED_LOCAL
| ELF_LINK_HASH_REF_DYNAMIC
)
4392 && ! elf_link_check_versioned_symbol (finfo
->info
, h
))
4394 (*_bfd_error_handler
)
4395 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
4396 bfd_get_filename (finfo
->output_bfd
),
4397 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
4399 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
4400 ? "hidden" : "local",
4401 h
->root
.root
.string
,
4402 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
4403 eoinfo
->failed
= TRUE
;
4407 /* We don't want to output symbols that have never been mentioned by
4408 a regular file, or that we have been told to strip. However, if
4409 h->indx is set to -2, the symbol is used by a reloc and we must
4413 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
4414 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
4415 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
4416 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
4418 else if (finfo
->info
->strip
== strip_all
)
4420 else if (finfo
->info
->strip
== strip_some
4421 && bfd_hash_lookup (finfo
->info
->keep_hash
,
4422 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
4424 else if (finfo
->info
->strip_discarded
4425 && (h
->root
.type
== bfd_link_hash_defined
4426 || h
->root
.type
== bfd_link_hash_defweak
)
4427 && elf_discarded_section (h
->root
.u
.def
.section
))
4432 /* If we're stripping it, and it's not a dynamic symbol, there's
4433 nothing else to do unless it is a forced local symbol. */
4436 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
4440 sym
.st_size
= h
->size
;
4441 sym
.st_other
= h
->other
;
4442 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4443 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
4444 else if (h
->root
.type
== bfd_link_hash_undefweak
4445 || h
->root
.type
== bfd_link_hash_defweak
)
4446 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
4448 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
4450 switch (h
->root
.type
)
4453 case bfd_link_hash_new
:
4454 case bfd_link_hash_warning
:
4458 case bfd_link_hash_undefined
:
4459 case bfd_link_hash_undefweak
:
4460 input_sec
= bfd_und_section_ptr
;
4461 sym
.st_shndx
= SHN_UNDEF
;
4464 case bfd_link_hash_defined
:
4465 case bfd_link_hash_defweak
:
4467 input_sec
= h
->root
.u
.def
.section
;
4468 if (input_sec
->output_section
!= NULL
)
4471 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
4472 input_sec
->output_section
);
4473 if (sym
.st_shndx
== SHN_BAD
)
4475 (*_bfd_error_handler
)
4476 (_("%s: could not find output section %s for input section %s"),
4477 bfd_get_filename (finfo
->output_bfd
),
4478 input_sec
->output_section
->name
,
4480 eoinfo
->failed
= TRUE
;
4484 /* ELF symbols in relocatable files are section relative,
4485 but in nonrelocatable files they are virtual
4487 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
4488 if (! finfo
->info
->relocatable
)
4490 sym
.st_value
+= input_sec
->output_section
->vma
;
4491 if (h
->type
== STT_TLS
)
4493 /* STT_TLS symbols are relative to PT_TLS segment
4495 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
4496 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
4502 BFD_ASSERT (input_sec
->owner
== NULL
4503 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
4504 sym
.st_shndx
= SHN_UNDEF
;
4505 input_sec
= bfd_und_section_ptr
;
4510 case bfd_link_hash_common
:
4511 input_sec
= h
->root
.u
.c
.p
->section
;
4512 sym
.st_shndx
= SHN_COMMON
;
4513 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
4516 case bfd_link_hash_indirect
:
4517 /* These symbols are created by symbol versioning. They point
4518 to the decorated version of the name. For example, if the
4519 symbol foo@@GNU_1.2 is the default, which should be used when
4520 foo is used with no version, then we add an indirect symbol
4521 foo which points to foo@@GNU_1.2. We ignore these symbols,
4522 since the indirected symbol is already in the hash table. */
4526 /* Give the processor backend a chance to tweak the symbol value,
4527 and also to finish up anything that needs to be done for this
4528 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
4529 forced local syms when non-shared is due to a historical quirk. */
4530 if ((h
->dynindx
!= -1
4531 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4532 && ((finfo
->info
->shared
4533 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4534 || h
->root
.type
!= bfd_link_hash_undefweak
))
4535 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
4536 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4538 const struct elf_backend_data
*bed
;
4540 bed
= get_elf_backend_data (finfo
->output_bfd
);
4541 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
4542 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
4544 eoinfo
->failed
= TRUE
;
4549 /* If we are marking the symbol as undefined, and there are no
4550 non-weak references to this symbol from a regular object, then
4551 mark the symbol as weak undefined; if there are non-weak
4552 references, mark the symbol as strong. We can't do this earlier,
4553 because it might not be marked as undefined until the
4554 finish_dynamic_symbol routine gets through with it. */
4555 if (sym
.st_shndx
== SHN_UNDEF
4556 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
4557 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
4558 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
4562 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
4563 bindtype
= STB_GLOBAL
;
4565 bindtype
= STB_WEAK
;
4566 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
4569 /* If a non-weak symbol with non-default visibility is not defined
4570 locally, it is a fatal error. */
4571 if (! finfo
->info
->relocatable
4572 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
4573 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
4574 && h
->root
.type
== bfd_link_hash_undefined
4575 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4577 (*_bfd_error_handler
)
4578 (_("%s: %s symbol `%s' isn't defined"),
4579 bfd_get_filename (finfo
->output_bfd
),
4580 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
4582 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
4583 ? "internal" : "hidden",
4584 h
->root
.root
.string
);
4585 eoinfo
->failed
= TRUE
;
4589 /* If this symbol should be put in the .dynsym section, then put it
4590 there now. We already know the symbol index. We also fill in
4591 the entry in the .hash section. */
4592 if (h
->dynindx
!= -1
4593 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4597 size_t hash_entry_size
;
4598 bfd_byte
*bucketpos
;
4600 Elf_External_Sym
*esym
;
4602 sym
.st_name
= h
->dynstr_index
;
4603 esym
= (Elf_External_Sym
*) finfo
->dynsym_sec
->contents
+ h
->dynindx
;
4604 elf_swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
4606 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
4607 bucket
= h
->elf_hash_value
% bucketcount
;
4609 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
4610 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
4611 + (bucket
+ 2) * hash_entry_size
);
4612 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
4613 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
4614 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
4615 ((bfd_byte
*) finfo
->hash_sec
->contents
4616 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
4618 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
4620 Elf_Internal_Versym iversym
;
4621 Elf_External_Versym
*eversym
;
4623 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4625 if (h
->verinfo
.verdef
== NULL
)
4626 iversym
.vs_vers
= 0;
4628 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4632 if (h
->verinfo
.vertree
== NULL
)
4633 iversym
.vs_vers
= 1;
4635 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
4638 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
4639 iversym
.vs_vers
|= VERSYM_HIDDEN
;
4641 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
4642 eversym
+= h
->dynindx
;
4643 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
4647 /* If we're stripping it, then it was just a dynamic symbol, and
4648 there's nothing else to do. */
4649 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
4652 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
4654 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
4656 eoinfo
->failed
= TRUE
;
4663 /* Link an input file into the linker output file. This function
4664 handles all the sections and relocations of the input file at once.
4665 This is so that we only have to read the local symbols once, and
4666 don't have to keep them in memory. */
4669 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
4671 bfd_boolean (*relocate_section
)
4672 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
4673 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
4675 Elf_Internal_Shdr
*symtab_hdr
;
4678 Elf_Internal_Sym
*isymbuf
;
4679 Elf_Internal_Sym
*isym
;
4680 Elf_Internal_Sym
*isymend
;
4682 asection
**ppsection
;
4684 const struct elf_backend_data
*bed
;
4685 bfd_boolean emit_relocs
;
4686 struct elf_link_hash_entry
**sym_hashes
;
4688 output_bfd
= finfo
->output_bfd
;
4689 bed
= get_elf_backend_data (output_bfd
);
4690 relocate_section
= bed
->elf_backend_relocate_section
;
4692 /* If this is a dynamic object, we don't want to do anything here:
4693 we don't want the local symbols, and we don't want the section
4695 if ((input_bfd
->flags
& DYNAMIC
) != 0)
4698 emit_relocs
= (finfo
->info
->relocatable
4699 || finfo
->info
->emitrelocations
4700 || bed
->elf_backend_emit_relocs
);
4702 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
4703 if (elf_bad_symtab (input_bfd
))
4705 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
4710 locsymcount
= symtab_hdr
->sh_info
;
4711 extsymoff
= symtab_hdr
->sh_info
;
4714 /* Read the local symbols. */
4715 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
4716 if (isymbuf
== NULL
&& locsymcount
!= 0)
4718 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
4719 finfo
->internal_syms
,
4720 finfo
->external_syms
,
4721 finfo
->locsym_shndx
);
4722 if (isymbuf
== NULL
)
4726 /* Find local symbol sections and adjust values of symbols in
4727 SEC_MERGE sections. Write out those local symbols we know are
4728 going into the output file. */
4729 isymend
= isymbuf
+ locsymcount
;
4730 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
4732 isym
++, pindex
++, ppsection
++)
4736 Elf_Internal_Sym osym
;
4740 if (elf_bad_symtab (input_bfd
))
4742 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
4749 if (isym
->st_shndx
== SHN_UNDEF
)
4750 isec
= bfd_und_section_ptr
;
4751 else if (isym
->st_shndx
< SHN_LORESERVE
4752 || isym
->st_shndx
> SHN_HIRESERVE
)
4754 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
4756 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
4757 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
4759 _bfd_merged_section_offset (output_bfd
, &isec
,
4760 elf_section_data (isec
)->sec_info
,
4763 else if (isym
->st_shndx
== SHN_ABS
)
4764 isec
= bfd_abs_section_ptr
;
4765 else if (isym
->st_shndx
== SHN_COMMON
)
4766 isec
= bfd_com_section_ptr
;
4775 /* Don't output the first, undefined, symbol. */
4776 if (ppsection
== finfo
->sections
)
4779 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
4781 /* We never output section symbols. Instead, we use the
4782 section symbol of the corresponding section in the output
4787 /* If we are stripping all symbols, we don't want to output this
4789 if (finfo
->info
->strip
== strip_all
)
4792 /* If we are discarding all local symbols, we don't want to
4793 output this one. If we are generating a relocatable output
4794 file, then some of the local symbols may be required by
4795 relocs; we output them below as we discover that they are
4797 if (finfo
->info
->discard
== discard_all
)
4800 /* If this symbol is defined in a section which we are
4801 discarding, we don't need to keep it, but note that
4802 linker_mark is only reliable for sections that have contents.
4803 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4804 as well as linker_mark. */
4805 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
4807 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
4808 || (! finfo
->info
->relocatable
4809 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
4812 /* Get the name of the symbol. */
4813 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
4818 /* See if we are discarding symbols with this name. */
4819 if ((finfo
->info
->strip
== strip_some
4820 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
4822 || (((finfo
->info
->discard
== discard_sec_merge
4823 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
4824 || finfo
->info
->discard
== discard_l
)
4825 && bfd_is_local_label_name (input_bfd
, name
)))
4828 /* If we get here, we are going to output this symbol. */
4832 /* Adjust the section index for the output file. */
4833 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
4834 isec
->output_section
);
4835 if (osym
.st_shndx
== SHN_BAD
)
4838 *pindex
= bfd_get_symcount (output_bfd
);
4840 /* ELF symbols in relocatable files are section relative, but
4841 in executable files they are virtual addresses. Note that
4842 this code assumes that all ELF sections have an associated
4843 BFD section with a reasonable value for output_offset; below
4844 we assume that they also have a reasonable value for
4845 output_section. Any special sections must be set up to meet
4846 these requirements. */
4847 osym
.st_value
+= isec
->output_offset
;
4848 if (! finfo
->info
->relocatable
)
4850 osym
.st_value
+= isec
->output_section
->vma
;
4851 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
4853 /* STT_TLS symbols are relative to PT_TLS segment base. */
4854 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
4855 osym
.st_value
-= finfo
->first_tls_sec
->vma
;
4859 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
4863 /* Relocate the contents of each section. */
4864 sym_hashes
= elf_sym_hashes (input_bfd
);
4865 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4869 if (! o
->linker_mark
)
4871 /* This section was omitted from the link. */
4875 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4876 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
4879 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
4881 /* Section was created by _bfd_elf_link_create_dynamic_sections
4886 /* Get the contents of the section. They have been cached by a
4887 relaxation routine. Note that o is a section in an input
4888 file, so the contents field will not have been set by any of
4889 the routines which work on output files. */
4890 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
4891 contents
= elf_section_data (o
)->this_hdr
.contents
;
4894 contents
= finfo
->contents
;
4895 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0,
4900 if ((o
->flags
& SEC_RELOC
) != 0)
4902 Elf_Internal_Rela
*internal_relocs
;
4904 /* Get the swapped relocs. */
4906 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
4907 finfo
->internal_relocs
, FALSE
);
4908 if (internal_relocs
== NULL
4909 && o
->reloc_count
> 0)
4912 /* Run through the relocs looking for any against symbols
4913 from discarded sections and section symbols from
4914 removed link-once sections. Complain about relocs
4915 against discarded sections. Zero relocs against removed
4916 link-once sections. Preserve debug information as much
4918 if (!elf_section_ignore_discarded_relocs (o
))
4920 Elf_Internal_Rela
*rel
, *relend
;
4922 rel
= internal_relocs
;
4923 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
4924 for ( ; rel
< relend
; rel
++)
4926 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
4929 if (r_symndx
>= locsymcount
4930 || (elf_bad_symtab (input_bfd
)
4931 && finfo
->sections
[r_symndx
] == NULL
))
4933 struct elf_link_hash_entry
*h
;
4935 h
= sym_hashes
[r_symndx
- extsymoff
];
4936 while (h
->root
.type
== bfd_link_hash_indirect
4937 || h
->root
.type
== bfd_link_hash_warning
)
4938 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4940 /* Complain if the definition comes from a
4941 discarded section. */
4942 sec
= h
->root
.u
.def
.section
;
4943 if ((h
->root
.type
== bfd_link_hash_defined
4944 || h
->root
.type
== bfd_link_hash_defweak
)
4945 && elf_discarded_section (sec
))
4947 if ((o
->flags
& SEC_DEBUGGING
) != 0)
4949 BFD_ASSERT (r_symndx
!= 0);
4950 /* Try to preserve debug information. */
4951 if ((o
->flags
& SEC_DEBUGGING
) != 0
4952 && sec
->kept_section
!= NULL
4953 && sec
->_raw_size
== sec
->kept_section
->_raw_size
)
4954 h
->root
.u
.def
.section
4955 = sec
->kept_section
;
4957 memset (rel
, 0, sizeof (*rel
));
4960 finfo
->info
->callbacks
->error_handler
4961 (LD_DEFINITION_IN_DISCARDED_SECTION
,
4962 _("%T: discarded in section `%s' from %s\n"),
4963 h
->root
.root
.string
,
4964 h
->root
.root
.string
,
4965 h
->root
.u
.def
.section
->name
,
4966 bfd_archive_filename (h
->root
.u
.def
.section
->owner
));
4971 sec
= finfo
->sections
[r_symndx
];
4973 if (sec
!= NULL
&& elf_discarded_section (sec
))
4975 if ((o
->flags
& SEC_DEBUGGING
) != 0
4976 || (sec
->flags
& SEC_LINK_ONCE
) != 0)
4978 BFD_ASSERT (r_symndx
!= 0);
4979 /* Try to preserve debug information. */
4980 if ((o
->flags
& SEC_DEBUGGING
) != 0
4981 && sec
->kept_section
!= NULL
4982 && sec
->_raw_size
== sec
->kept_section
->_raw_size
)
4983 finfo
->sections
[r_symndx
]
4984 = sec
->kept_section
;
4988 = ELF_R_INFO (0, ELF_R_TYPE (rel
->r_info
));
4998 ok
= asprintf (&buf
, "local symbol %d",
5001 buf
= (char *) "local symbol";
5002 finfo
->info
->callbacks
->error_handler
5003 (LD_DEFINITION_IN_DISCARDED_SECTION
,
5004 _("%T: discarded in section `%s' from %s\n"),
5005 buf
, buf
, sec
->name
,
5006 bfd_archive_filename (input_bfd
));
5015 /* Relocate the section by invoking a back end routine.
5017 The back end routine is responsible for adjusting the
5018 section contents as necessary, and (if using Rela relocs
5019 and generating a relocatable output file) adjusting the
5020 reloc addend as necessary.
5022 The back end routine does not have to worry about setting
5023 the reloc address or the reloc symbol index.
5025 The back end routine is given a pointer to the swapped in
5026 internal symbols, and can access the hash table entries
5027 for the external symbols via elf_sym_hashes (input_bfd).
5029 When generating relocatable output, the back end routine
5030 must handle STB_LOCAL/STT_SECTION symbols specially. The
5031 output symbol is going to be a section symbol
5032 corresponding to the output section, which will require
5033 the addend to be adjusted. */
5035 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
5036 input_bfd
, o
, contents
,
5044 Elf_Internal_Rela
*irela
;
5045 Elf_Internal_Rela
*irelaend
;
5046 bfd_vma last_offset
;
5047 struct elf_link_hash_entry
**rel_hash
;
5048 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
5049 unsigned int next_erel
;
5050 bfd_boolean (*reloc_emitter
)
5051 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
5052 bfd_boolean rela_normal
;
5054 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
5055 rela_normal
= (bed
->rela_normal
5056 && (input_rel_hdr
->sh_entsize
5057 == sizeof (Elf_External_Rela
)));
5059 /* Adjust the reloc addresses and symbol indices. */
5061 irela
= internal_relocs
;
5062 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5063 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
5064 + elf_section_data (o
->output_section
)->rel_count
5065 + elf_section_data (o
->output_section
)->rel_count2
);
5066 last_offset
= o
->output_offset
;
5067 if (!finfo
->info
->relocatable
)
5068 last_offset
+= o
->output_section
->vma
;
5069 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
5071 unsigned long r_symndx
;
5073 Elf_Internal_Sym sym
;
5075 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
5081 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
5084 if (irela
->r_offset
>= (bfd_vma
) -2)
5086 /* This is a reloc for a deleted entry or somesuch.
5087 Turn it into an R_*_NONE reloc, at the same
5088 offset as the last reloc. elf_eh_frame.c and
5089 elf_bfd_discard_info rely on reloc offsets
5091 irela
->r_offset
= last_offset
;
5093 irela
->r_addend
= 0;
5097 irela
->r_offset
+= o
->output_offset
;
5099 /* Relocs in an executable have to be virtual addresses. */
5100 if (!finfo
->info
->relocatable
)
5101 irela
->r_offset
+= o
->output_section
->vma
;
5103 last_offset
= irela
->r_offset
;
5105 r_symndx
= ELF_R_SYM (irela
->r_info
);
5106 if (r_symndx
== STN_UNDEF
)
5109 if (r_symndx
>= locsymcount
5110 || (elf_bad_symtab (input_bfd
)
5111 && finfo
->sections
[r_symndx
] == NULL
))
5113 struct elf_link_hash_entry
*rh
;
5116 /* This is a reloc against a global symbol. We
5117 have not yet output all the local symbols, so
5118 we do not know the symbol index of any global
5119 symbol. We set the rel_hash entry for this
5120 reloc to point to the global hash table entry
5121 for this symbol. The symbol index is then
5122 set at the end of elf_bfd_final_link. */
5123 indx
= r_symndx
- extsymoff
;
5124 rh
= elf_sym_hashes (input_bfd
)[indx
];
5125 while (rh
->root
.type
== bfd_link_hash_indirect
5126 || rh
->root
.type
== bfd_link_hash_warning
)
5127 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
5129 /* Setting the index to -2 tells
5130 elf_link_output_extsym that this symbol is
5132 BFD_ASSERT (rh
->indx
< 0);
5140 /* This is a reloc against a local symbol. */
5143 sym
= isymbuf
[r_symndx
];
5144 sec
= finfo
->sections
[r_symndx
];
5145 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
5147 /* I suppose the backend ought to fill in the
5148 section of any STT_SECTION symbol against a
5149 processor specific section. If we have
5150 discarded a section, the output_section will
5151 be the absolute section. */
5152 if (bfd_is_abs_section (sec
)
5154 && bfd_is_abs_section (sec
->output_section
)))
5156 else if (sec
== NULL
|| sec
->owner
== NULL
)
5158 bfd_set_error (bfd_error_bad_value
);
5163 r_symndx
= sec
->output_section
->target_index
;
5164 BFD_ASSERT (r_symndx
!= 0);
5167 /* Adjust the addend according to where the
5168 section winds up in the output section. */
5170 irela
->r_addend
+= sec
->output_offset
;
5174 if (finfo
->indices
[r_symndx
] == -1)
5176 unsigned long shlink
;
5180 if (finfo
->info
->strip
== strip_all
)
5182 /* You can't do ld -r -s. */
5183 bfd_set_error (bfd_error_invalid_operation
);
5187 /* This symbol was skipped earlier, but
5188 since it is needed by a reloc, we
5189 must output it now. */
5190 shlink
= symtab_hdr
->sh_link
;
5191 name
= (bfd_elf_string_from_elf_section
5192 (input_bfd
, shlink
, sym
.st_name
));
5196 osec
= sec
->output_section
;
5198 _bfd_elf_section_from_bfd_section (output_bfd
,
5200 if (sym
.st_shndx
== SHN_BAD
)
5203 sym
.st_value
+= sec
->output_offset
;
5204 if (! finfo
->info
->relocatable
)
5206 sym
.st_value
+= osec
->vma
;
5207 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
5209 /* STT_TLS symbols are relative to PT_TLS
5211 BFD_ASSERT (finfo
->first_tls_sec
!= NULL
);
5212 sym
.st_value
-= finfo
->first_tls_sec
->vma
;
5216 finfo
->indices
[r_symndx
]
5217 = bfd_get_symcount (output_bfd
);
5219 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
))
5223 r_symndx
= finfo
->indices
[r_symndx
];
5226 irela
->r_info
= ELF_R_INFO (r_symndx
,
5227 ELF_R_TYPE (irela
->r_info
));
5230 /* Swap out the relocs. */
5231 if (bed
->elf_backend_emit_relocs
5232 && !(finfo
->info
->relocatable
5233 || finfo
->info
->emitrelocations
))
5234 reloc_emitter
= bed
->elf_backend_emit_relocs
;
5236 reloc_emitter
= _bfd_elf_link_output_relocs
;
5238 if (input_rel_hdr
->sh_size
!= 0
5239 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
5243 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
5244 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
5246 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
5247 * bed
->s
->int_rels_per_ext_rel
);
5248 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
5255 /* Write out the modified section contents. */
5256 if (bed
->elf_backend_write_section
5257 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
5259 /* Section written out. */
5261 else switch (o
->sec_info_type
)
5263 case ELF_INFO_TYPE_STABS
:
5264 if (! (_bfd_write_section_stabs
5266 &elf_hash_table (finfo
->info
)->stab_info
,
5267 o
, &elf_section_data (o
)->sec_info
, contents
)))
5270 case ELF_INFO_TYPE_MERGE
:
5271 if (! _bfd_write_merged_section (output_bfd
, o
,
5272 elf_section_data (o
)->sec_info
))
5275 case ELF_INFO_TYPE_EH_FRAME
:
5277 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
5284 bfd_size_type sec_size
;
5286 sec_size
= (o
->_cooked_size
!= 0 ? o
->_cooked_size
: o
->_raw_size
);
5287 if (! (o
->flags
& SEC_EXCLUDE
)
5288 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
5290 (file_ptr
) o
->output_offset
,
5301 /* Generate a reloc when linking an ELF file. This is a reloc
5302 requested by the linker, and does come from any input file. This
5303 is used to build constructor and destructor tables when linking
5307 elf_reloc_link_order (bfd
*output_bfd
,
5308 struct bfd_link_info
*info
,
5309 asection
*output_section
,
5310 struct bfd_link_order
*link_order
)
5312 reloc_howto_type
*howto
;
5316 struct elf_link_hash_entry
**rel_hash_ptr
;
5317 Elf_Internal_Shdr
*rel_hdr
;
5318 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
5319 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
5323 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5326 bfd_set_error (bfd_error_bad_value
);
5330 addend
= link_order
->u
.reloc
.p
->addend
;
5332 /* Figure out the symbol index. */
5333 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
5334 + elf_section_data (output_section
)->rel_count
5335 + elf_section_data (output_section
)->rel_count2
);
5336 if (link_order
->type
== bfd_section_reloc_link_order
)
5338 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
5339 BFD_ASSERT (indx
!= 0);
5340 *rel_hash_ptr
= NULL
;
5344 struct elf_link_hash_entry
*h
;
5346 /* Treat a reloc against a defined symbol as though it were
5347 actually against the section. */
5348 h
= ((struct elf_link_hash_entry
*)
5349 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
5350 link_order
->u
.reloc
.p
->u
.name
,
5351 FALSE
, FALSE
, TRUE
));
5353 && (h
->root
.type
== bfd_link_hash_defined
5354 || h
->root
.type
== bfd_link_hash_defweak
))
5358 section
= h
->root
.u
.def
.section
;
5359 indx
= section
->output_section
->target_index
;
5360 *rel_hash_ptr
= NULL
;
5361 /* It seems that we ought to add the symbol value to the
5362 addend here, but in practice it has already been added
5363 because it was passed to constructor_callback. */
5364 addend
+= section
->output_section
->vma
+ section
->output_offset
;
5368 /* Setting the index to -2 tells elf_link_output_extsym that
5369 this symbol is used by a reloc. */
5376 if (! ((*info
->callbacks
->unattached_reloc
)
5377 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
5383 /* If this is an inplace reloc, we must write the addend into the
5385 if (howto
->partial_inplace
&& addend
!= 0)
5388 bfd_reloc_status_type rstat
;
5391 const char *sym_name
;
5393 size
= bfd_get_reloc_size (howto
);
5394 buf
= bfd_zmalloc (size
);
5397 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5404 case bfd_reloc_outofrange
:
5407 case bfd_reloc_overflow
:
5408 if (link_order
->type
== bfd_section_reloc_link_order
)
5409 sym_name
= bfd_section_name (output_bfd
,
5410 link_order
->u
.reloc
.p
->u
.section
);
5412 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
5413 if (! ((*info
->callbacks
->reloc_overflow
)
5414 (info
, sym_name
, howto
->name
, addend
, NULL
, NULL
, 0)))
5421 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
5422 link_order
->offset
, size
);
5428 /* The address of a reloc is relative to the section in a
5429 relocatable file, and is a virtual address in an executable
5431 offset
= link_order
->offset
;
5432 if (! info
->relocatable
)
5433 offset
+= output_section
->vma
;
5435 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
5437 irel
[i
].r_offset
= offset
;
5439 irel
[i
].r_addend
= 0;
5441 irel
[0].r_info
= ELF_R_INFO (indx
, howto
->type
);
5443 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
5444 erel
= rel_hdr
->contents
;
5445 if (rel_hdr
->sh_type
== SHT_REL
)
5447 erel
+= (elf_section_data (output_section
)->rel_count
5448 * sizeof (Elf_External_Rel
));
5449 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
5453 irel
[0].r_addend
= addend
;
5454 erel
+= (elf_section_data (output_section
)->rel_count
5455 * sizeof (Elf_External_Rela
));
5456 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
5459 ++elf_section_data (output_section
)->rel_count
;
5464 /* Garbage collect unused sections. */
5466 static bfd_boolean elf_gc_sweep_symbol
5467 (struct elf_link_hash_entry
*, void *);
5469 static bfd_boolean elf_gc_allocate_got_offsets
5470 (struct elf_link_hash_entry
*, void *);
5472 /* The mark phase of garbage collection. For a given section, mark
5473 it and any sections in this section's group, and all the sections
5474 which define symbols to which it refers. */
5476 typedef asection
* (*gc_mark_hook_fn
)
5477 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
5478 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
5481 elf_gc_mark (struct bfd_link_info
*info
,
5483 gc_mark_hook_fn gc_mark_hook
)
5486 asection
*group_sec
;
5490 /* Mark all the sections in the group. */
5491 group_sec
= elf_section_data (sec
)->next_in_group
;
5492 if (group_sec
&& !group_sec
->gc_mark
)
5493 if (!elf_gc_mark (info
, group_sec
, gc_mark_hook
))
5496 /* Look through the section relocs. */
5498 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
5500 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
5501 Elf_Internal_Shdr
*symtab_hdr
;
5502 struct elf_link_hash_entry
**sym_hashes
;
5505 bfd
*input_bfd
= sec
->owner
;
5506 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
5507 Elf_Internal_Sym
*isym
= NULL
;
5509 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
5510 sym_hashes
= elf_sym_hashes (input_bfd
);
5512 /* Read the local symbols. */
5513 if (elf_bad_symtab (input_bfd
))
5515 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
5519 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
5521 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
5522 if (isym
== NULL
&& nlocsyms
!= 0)
5524 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
5530 /* Read the relocations. */
5531 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
5533 if (relstart
== NULL
)
5538 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5540 for (rel
= relstart
; rel
< relend
; rel
++)
5542 unsigned long r_symndx
;
5544 struct elf_link_hash_entry
*h
;
5546 r_symndx
= ELF_R_SYM (rel
->r_info
);
5550 if (r_symndx
>= nlocsyms
5551 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
5553 h
= sym_hashes
[r_symndx
- extsymoff
];
5554 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
5558 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
5561 if (rsec
&& !rsec
->gc_mark
)
5563 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
5565 else if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
5574 if (elf_section_data (sec
)->relocs
!= relstart
)
5577 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
5579 if (! info
->keep_memory
)
5582 symtab_hdr
->contents
= (unsigned char *) isym
;
5589 /* The sweep phase of garbage collection. Remove all garbage sections. */
5591 typedef bfd_boolean (*gc_sweep_hook_fn
)
5592 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
5595 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
5599 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5603 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
5606 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5608 /* Keep special sections. Keep .debug sections. */
5609 if ((o
->flags
& SEC_LINKER_CREATED
)
5610 || (o
->flags
& SEC_DEBUGGING
))
5616 /* Skip sweeping sections already excluded. */
5617 if (o
->flags
& SEC_EXCLUDE
)
5620 /* Since this is early in the link process, it is simple
5621 to remove a section from the output. */
5622 o
->flags
|= SEC_EXCLUDE
;
5624 /* But we also have to update some of the relocation
5625 info we collected before. */
5627 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
5629 Elf_Internal_Rela
*internal_relocs
;
5633 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
5635 if (internal_relocs
== NULL
)
5638 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
5640 if (elf_section_data (o
)->relocs
!= internal_relocs
)
5641 free (internal_relocs
);
5649 /* Remove the symbols that were in the swept sections from the dynamic
5650 symbol table. GCFIXME: Anyone know how to get them out of the
5651 static symbol table as well? */
5655 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
5657 elf_hash_table (info
)->dynsymcount
= i
;
5663 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5666 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
5670 if (h
->root
.type
== bfd_link_hash_warning
)
5671 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5673 if (h
->dynindx
!= -1
5674 && ((h
->root
.type
!= bfd_link_hash_defined
5675 && h
->root
.type
!= bfd_link_hash_defweak
)
5676 || h
->root
.u
.def
.section
->gc_mark
))
5677 h
->dynindx
= (*idx
)++;
5682 /* Propogate collected vtable information. This is called through
5683 elf_link_hash_traverse. */
5686 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
5688 if (h
->root
.type
== bfd_link_hash_warning
)
5689 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5691 /* Those that are not vtables. */
5692 if (h
->vtable_parent
== NULL
)
5695 /* Those vtables that do not have parents, we cannot merge. */
5696 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
5699 /* If we've already been done, exit. */
5700 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
5703 /* Make sure the parent's table is up to date. */
5704 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
5706 if (h
->vtable_entries_used
== NULL
)
5708 /* None of this table's entries were referenced. Re-use the
5710 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
5711 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
5716 bfd_boolean
*cu
, *pu
;
5718 /* Or the parent's entries into ours. */
5719 cu
= h
->vtable_entries_used
;
5721 pu
= h
->vtable_parent
->vtable_entries_used
;
5724 const struct elf_backend_data
*bed
;
5725 unsigned int log_file_align
;
5727 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
5728 log_file_align
= bed
->s
->log_file_align
;
5729 n
= h
->vtable_parent
->vtable_entries_size
>> log_file_align
;
5744 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
5747 bfd_vma hstart
, hend
;
5748 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
5749 const struct elf_backend_data
*bed
;
5750 unsigned int log_file_align
;
5752 if (h
->root
.type
== bfd_link_hash_warning
)
5753 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5755 /* Take care of both those symbols that do not describe vtables as
5756 well as those that are not loaded. */
5757 if (h
->vtable_parent
== NULL
)
5760 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5761 || h
->root
.type
== bfd_link_hash_defweak
);
5763 sec
= h
->root
.u
.def
.section
;
5764 hstart
= h
->root
.u
.def
.value
;
5765 hend
= hstart
+ h
->size
;
5767 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
5769 return *(bfd_boolean
*) okp
= FALSE
;
5770 bed
= get_elf_backend_data (sec
->owner
);
5771 log_file_align
= bed
->s
->log_file_align
;
5773 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5775 for (rel
= relstart
; rel
< relend
; ++rel
)
5776 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
5778 /* If the entry is in use, do nothing. */
5779 if (h
->vtable_entries_used
5780 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
5782 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
5783 if (h
->vtable_entries_used
[entry
])
5786 /* Otherwise, kill it. */
5787 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
5793 /* Do mark and sweep of unused sections. */
5796 elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
5798 bfd_boolean ok
= TRUE
;
5800 asection
* (*gc_mark_hook
)
5801 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
5802 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
5804 if (!get_elf_backend_data (abfd
)->can_gc_sections
5805 || info
->relocatable
|| info
->emitrelocations
5806 || elf_hash_table (info
)->dynamic_sections_created
)
5809 /* Apply transitive closure to the vtable entry usage info. */
5810 elf_link_hash_traverse (elf_hash_table (info
),
5811 elf_gc_propagate_vtable_entries_used
,
5816 /* Kill the vtable relocations that were not used. */
5817 elf_link_hash_traverse (elf_hash_table (info
),
5818 elf_gc_smash_unused_vtentry_relocs
,
5823 /* Grovel through relocs to find out who stays ... */
5825 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
5826 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5830 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
5833 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5835 if (o
->flags
& SEC_KEEP
)
5836 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
5841 /* ... and mark SEC_EXCLUDE for those that go. */
5842 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
5848 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
5851 elf_gc_record_vtinherit (bfd
*abfd
,
5853 struct elf_link_hash_entry
*h
,
5856 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
5857 struct elf_link_hash_entry
**search
, *child
;
5858 bfd_size_type extsymcount
;
5860 /* The sh_info field of the symtab header tells us where the
5861 external symbols start. We don't care about the local symbols at
5863 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
5864 if (!elf_bad_symtab (abfd
))
5865 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
5867 sym_hashes
= elf_sym_hashes (abfd
);
5868 sym_hashes_end
= sym_hashes
+ extsymcount
;
5870 /* Hunt down the child symbol, which is in this section at the same
5871 offset as the relocation. */
5872 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
5874 if ((child
= *search
) != NULL
5875 && (child
->root
.type
== bfd_link_hash_defined
5876 || child
->root
.type
== bfd_link_hash_defweak
)
5877 && child
->root
.u
.def
.section
== sec
5878 && child
->root
.u
.def
.value
== offset
)
5882 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
5883 bfd_archive_filename (abfd
), sec
->name
,
5884 (unsigned long) offset
);
5885 bfd_set_error (bfd_error_invalid_operation
);
5891 /* This *should* only be the absolute section. It could potentially
5892 be that someone has defined a non-global vtable though, which
5893 would be bad. It isn't worth paging in the local symbols to be
5894 sure though; that case should simply be handled by the assembler. */
5896 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
5899 child
->vtable_parent
= h
;
5904 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
5907 elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
5908 asection
*sec ATTRIBUTE_UNUSED
,
5909 struct elf_link_hash_entry
*h
,
5912 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5913 unsigned int log_file_align
= bed
->s
->log_file_align
;
5915 if (addend
>= h
->vtable_entries_size
)
5917 size_t size
, bytes
, file_align
;
5918 bfd_boolean
*ptr
= h
->vtable_entries_used
;
5920 /* While the symbol is undefined, we have to be prepared to handle
5922 file_align
= 1 << log_file_align
;
5923 if (h
->root
.type
== bfd_link_hash_undefined
)
5924 size
= addend
+ file_align
;
5930 /* Oops! We've got a reference past the defined end of
5931 the table. This is probably a bug -- shall we warn? */
5932 size
= addend
+ file_align
;
5935 size
= (size
+ file_align
- 1) & -file_align
;
5937 /* Allocate one extra entry for use as a "done" flag for the
5938 consolidation pass. */
5939 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
5943 ptr
= bfd_realloc (ptr
- 1, bytes
);
5949 oldbytes
= (((h
->vtable_entries_size
>> log_file_align
) + 1)
5950 * sizeof (bfd_boolean
));
5951 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
5955 ptr
= bfd_zmalloc (bytes
);
5960 /* And arrange for that done flag to be at index -1. */
5961 h
->vtable_entries_used
= ptr
+ 1;
5962 h
->vtable_entries_size
= size
;
5965 h
->vtable_entries_used
[addend
>> log_file_align
] = TRUE
;
5970 /* And an accompanying bit to work out final got entry offsets once
5971 we're done. Should be called from final_link. */
5974 elf_gc_common_finalize_got_offsets (bfd
*abfd
,
5975 struct bfd_link_info
*info
)
5978 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5981 /* The GOT offset is relative to the .got section, but the GOT header is
5982 put into the .got.plt section, if the backend uses it. */
5983 if (bed
->want_got_plt
)
5986 gotoff
= bed
->got_header_size
;
5988 /* Do the local .got entries first. */
5989 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
5991 bfd_signed_vma
*local_got
;
5992 bfd_size_type j
, locsymcount
;
5993 Elf_Internal_Shdr
*symtab_hdr
;
5995 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
5998 local_got
= elf_local_got_refcounts (i
);
6002 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
6003 if (elf_bad_symtab (i
))
6004 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6006 locsymcount
= symtab_hdr
->sh_info
;
6008 for (j
= 0; j
< locsymcount
; ++j
)
6010 if (local_got
[j
] > 0)
6012 local_got
[j
] = gotoff
;
6013 gotoff
+= ARCH_SIZE
/ 8;
6016 local_got
[j
] = (bfd_vma
) -1;
6020 /* Then the global .got entries. .plt refcounts are handled by
6021 adjust_dynamic_symbol */
6022 elf_link_hash_traverse (elf_hash_table (info
),
6023 elf_gc_allocate_got_offsets
,
6028 /* We need a special top-level link routine to convert got reference counts
6029 to real got offsets. */
6032 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *offarg
)
6034 bfd_vma
*off
= offarg
;
6036 if (h
->root
.type
== bfd_link_hash_warning
)
6037 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6039 if (h
->got
.refcount
> 0)
6041 h
->got
.offset
= off
[0];
6042 off
[0] += ARCH_SIZE
/ 8;
6045 h
->got
.offset
= (bfd_vma
) -1;
6050 /* Many folk need no more in the way of final link than this, once
6051 got entry reference counting is enabled. */
6054 elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
6056 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
6059 /* Invoke the regular ELF backend linker to do all the work. */
6060 return elf_bfd_final_link (abfd
, info
);
6063 /* This function will be called though elf_link_hash_traverse to store
6064 all hash value of the exported symbols in an array. */
6067 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6069 unsigned long **valuep
= data
;
6075 if (h
->root
.type
== bfd_link_hash_warning
)
6076 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6078 /* Ignore indirect symbols. These are added by the versioning code. */
6079 if (h
->dynindx
== -1)
6082 name
= h
->root
.root
.string
;
6083 p
= strchr (name
, ELF_VER_CHR
);
6086 alc
= bfd_malloc (p
- name
+ 1);
6087 memcpy (alc
, name
, p
- name
);
6088 alc
[p
- name
] = '\0';
6092 /* Compute the hash value. */
6093 ha
= bfd_elf_hash (name
);
6095 /* Store the found hash value in the array given as the argument. */
6098 /* And store it in the struct so that we can put it in the hash table
6100 h
->elf_hash_value
= ha
;
6109 elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
6111 struct elf_reloc_cookie
*rcookie
= cookie
;
6113 if (rcookie
->bad_symtab
)
6114 rcookie
->rel
= rcookie
->rels
;
6116 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
6118 unsigned long r_symndx
;
6120 if (! rcookie
->bad_symtab
)
6121 if (rcookie
->rel
->r_offset
> offset
)
6123 if (rcookie
->rel
->r_offset
!= offset
)
6126 r_symndx
= ELF_R_SYM (rcookie
->rel
->r_info
);
6127 if (r_symndx
== SHN_UNDEF
)
6130 if (r_symndx
>= rcookie
->locsymcount
6131 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
6133 struct elf_link_hash_entry
*h
;
6135 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
6137 while (h
->root
.type
== bfd_link_hash_indirect
6138 || h
->root
.type
== bfd_link_hash_warning
)
6139 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6141 if ((h
->root
.type
== bfd_link_hash_defined
6142 || h
->root
.type
== bfd_link_hash_defweak
)
6143 && elf_discarded_section (h
->root
.u
.def
.section
))
6150 /* It's not a relocation against a global symbol,
6151 but it could be a relocation against a local
6152 symbol for a discarded section. */
6154 Elf_Internal_Sym
*isym
;
6156 /* Need to: get the symbol; get the section. */
6157 isym
= &rcookie
->locsyms
[r_symndx
];
6158 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6160 isec
= section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
6161 if (isec
!= NULL
&& elf_discarded_section (isec
))
6170 /* Discard unneeded references to discarded sections.
6171 Returns TRUE if any section's size was changed. */
6172 /* This function assumes that the relocations are in sorted order,
6173 which is true for all known assemblers. */
6176 elf_bfd_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
6178 struct elf_reloc_cookie cookie
;
6179 asection
*stab
, *eh
;
6180 Elf_Internal_Shdr
*symtab_hdr
;
6181 const struct elf_backend_data
*bed
;
6184 bfd_boolean ret
= FALSE
;
6186 if (info
->traditional_format
6187 || info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
6188 || ! is_elf_hash_table (info
))
6191 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
6193 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6196 bed
= get_elf_backend_data (abfd
);
6198 if ((abfd
->flags
& DYNAMIC
) != 0)
6201 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
6202 if (info
->relocatable
6204 && (eh
->_raw_size
== 0
6205 || bfd_is_abs_section (eh
->output_section
))))
6208 stab
= bfd_get_section_by_name (abfd
, ".stab");
6210 && (stab
->_raw_size
== 0
6211 || bfd_is_abs_section (stab
->output_section
)
6212 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
6217 && bed
->elf_backend_discard_info
== NULL
)
6220 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6222 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
6223 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
6224 if (cookie
.bad_symtab
)
6226 cookie
.locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6227 cookie
.extsymoff
= 0;
6231 cookie
.locsymcount
= symtab_hdr
->sh_info
;
6232 cookie
.extsymoff
= symtab_hdr
->sh_info
;
6235 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6236 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
6238 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
6239 cookie
.locsymcount
, 0,
6241 if (cookie
.locsyms
== NULL
)
6248 count
= stab
->reloc_count
;
6250 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
6252 if (cookie
.rels
!= NULL
)
6254 cookie
.rel
= cookie
.rels
;
6255 cookie
.relend
= cookie
.rels
;
6256 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
6257 if (_bfd_discard_section_stabs (abfd
, stab
,
6258 elf_section_data (stab
)->sec_info
,
6259 elf_reloc_symbol_deleted_p
,
6262 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
6270 count
= eh
->reloc_count
;
6272 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
6274 cookie
.rel
= cookie
.rels
;
6275 cookie
.relend
= cookie
.rels
;
6276 if (cookie
.rels
!= NULL
)
6277 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
6279 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
6280 elf_reloc_symbol_deleted_p
,
6284 if (cookie
.rels
!= NULL
6285 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
6289 if (bed
->elf_backend_discard_info
!= NULL
6290 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
6293 if (cookie
.locsyms
!= NULL
6294 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
6296 if (! info
->keep_memory
)
6297 free (cookie
.locsyms
);
6299 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
6303 if (info
->eh_frame_hdr
6304 && !info
->relocatable
6305 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
6312 elf_section_ignore_discarded_relocs (asection
*sec
)
6314 const struct elf_backend_data
*bed
;
6316 switch (sec
->sec_info_type
)
6318 case ELF_INFO_TYPE_STABS
:
6319 case ELF_INFO_TYPE_EH_FRAME
:
6325 bed
= get_elf_backend_data (sec
->owner
);
6326 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6327 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))