2 Copyright 1995, 1996, 1997 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* ELF linker code. */
22 /* This struct is used to pass information to routines called via
23 elf_link_hash_traverse which must return failure. */
25 struct elf_info_failed
28 struct bfd_link_info
*info
;
31 static boolean elf_link_add_object_symbols
32 PARAMS ((bfd
*, struct bfd_link_info
*));
33 static boolean elf_link_add_archive_symbols
34 PARAMS ((bfd
*, struct bfd_link_info
*));
35 static boolean elf_export_symbol
36 PARAMS ((struct elf_link_hash_entry
*, PTR
));
37 static boolean elf_fix_symbol_flags
38 PARAMS ((struct elf_link_hash_entry
*, struct elf_info_failed
*));
39 static boolean elf_adjust_dynamic_symbol
40 PARAMS ((struct elf_link_hash_entry
*, PTR
));
41 static boolean elf_link_find_version_dependencies
42 PARAMS ((struct elf_link_hash_entry
*, PTR
));
43 static boolean elf_link_find_version_dependencies
44 PARAMS ((struct elf_link_hash_entry
*, PTR
));
45 static boolean elf_link_assign_sym_version
46 PARAMS ((struct elf_link_hash_entry
*, PTR
));
47 static boolean elf_link_renumber_dynsyms
48 PARAMS ((struct elf_link_hash_entry
*, PTR
));
50 /* Given an ELF BFD, add symbols to the global hash table as
54 elf_bfd_link_add_symbols (abfd
, info
)
56 struct bfd_link_info
*info
;
58 switch (bfd_get_format (abfd
))
61 return elf_link_add_object_symbols (abfd
, info
);
63 return elf_link_add_archive_symbols (abfd
, info
);
65 bfd_set_error (bfd_error_wrong_format
);
71 /* Add symbols from an ELF archive file to the linker hash table. We
72 don't use _bfd_generic_link_add_archive_symbols because of a
73 problem which arises on UnixWare. The UnixWare libc.so is an
74 archive which includes an entry libc.so.1 which defines a bunch of
75 symbols. The libc.so archive also includes a number of other
76 object files, which also define symbols, some of which are the same
77 as those defined in libc.so.1. Correct linking requires that we
78 consider each object file in turn, and include it if it defines any
79 symbols we need. _bfd_generic_link_add_archive_symbols does not do
80 this; it looks through the list of undefined symbols, and includes
81 any object file which defines them. When this algorithm is used on
82 UnixWare, it winds up pulling in libc.so.1 early and defining a
83 bunch of symbols. This means that some of the other objects in the
84 archive are not included in the link, which is incorrect since they
85 precede libc.so.1 in the archive.
87 Fortunately, ELF archive handling is simpler than that done by
88 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
89 oddities. In ELF, if we find a symbol in the archive map, and the
90 symbol is currently undefined, we know that we must pull in that
93 Unfortunately, we do have to make multiple passes over the symbol
94 table until nothing further is resolved. */
97 elf_link_add_archive_symbols (abfd
, info
)
99 struct bfd_link_info
*info
;
102 boolean
*defined
= NULL
;
103 boolean
*included
= NULL
;
107 if (! bfd_has_map (abfd
))
109 /* An empty archive is a special case. */
110 if (bfd_openr_next_archived_file (abfd
, (bfd
*) NULL
) == NULL
)
112 bfd_set_error (bfd_error_no_armap
);
116 /* Keep track of all symbols we know to be already defined, and all
117 files we know to be already included. This is to speed up the
118 second and subsequent passes. */
119 c
= bfd_ardata (abfd
)->symdef_count
;
122 defined
= (boolean
*) bfd_malloc (c
* sizeof (boolean
));
123 included
= (boolean
*) bfd_malloc (c
* sizeof (boolean
));
124 if (defined
== (boolean
*) NULL
|| included
== (boolean
*) NULL
)
126 memset (defined
, 0, c
* sizeof (boolean
));
127 memset (included
, 0, c
* sizeof (boolean
));
129 symdefs
= bfd_ardata (abfd
)->symdefs
;
142 symdefend
= symdef
+ c
;
143 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
145 struct elf_link_hash_entry
*h
;
147 struct bfd_link_hash_entry
*undefs_tail
;
150 if (defined
[i
] || included
[i
])
152 if (symdef
->file_offset
== last
)
158 h
= elf_link_hash_lookup (elf_hash_table (info
), symdef
->name
,
159 false, false, false);
165 /* If this is a default version (the name contains @@),
166 look up the symbol again without the version. The
167 effect is that references to the symbol without the
168 version will be matched by the default symbol in the
171 p
= strchr (symdef
->name
, ELF_VER_CHR
);
172 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
175 copy
= bfd_alloc (abfd
, p
- symdef
->name
+ 1);
178 memcpy (copy
, symdef
->name
, p
- symdef
->name
);
179 copy
[p
- symdef
->name
] = '\0';
181 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
182 false, false, false);
184 bfd_release (abfd
, copy
);
190 if (h
->root
.type
!= bfd_link_hash_undefined
)
192 if (h
->root
.type
!= bfd_link_hash_undefweak
)
197 /* We need to include this archive member. */
199 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
200 if (element
== (bfd
*) NULL
)
203 if (! bfd_check_format (element
, bfd_object
))
206 /* Doublecheck that we have not included this object
207 already--it should be impossible, but there may be
208 something wrong with the archive. */
209 if (element
->archive_pass
!= 0)
211 bfd_set_error (bfd_error_bad_value
);
214 element
->archive_pass
= 1;
216 undefs_tail
= info
->hash
->undefs_tail
;
218 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
221 if (! elf_link_add_object_symbols (element
, info
))
224 /* If there are any new undefined symbols, we need to make
225 another pass through the archive in order to see whether
226 they can be defined. FIXME: This isn't perfect, because
227 common symbols wind up on undefs_tail and because an
228 undefined symbol which is defined later on in this pass
229 does not require another pass. This isn't a bug, but it
230 does make the code less efficient than it could be. */
231 if (undefs_tail
!= info
->hash
->undefs_tail
)
234 /* Look backward to mark all symbols from this object file
235 which we have already seen in this pass. */
239 included
[mark
] = true;
244 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
246 /* We mark subsequent symbols from this object file as we go
247 on through the loop. */
248 last
= symdef
->file_offset
;
259 if (defined
!= (boolean
*) NULL
)
261 if (included
!= (boolean
*) NULL
)
266 /* Add symbols from an ELF object file to the linker hash table. */
269 elf_link_add_object_symbols (abfd
, info
)
271 struct bfd_link_info
*info
;
273 boolean (*add_symbol_hook
) PARAMS ((bfd
*, struct bfd_link_info
*,
274 const Elf_Internal_Sym
*,
275 const char **, flagword
*,
276 asection
**, bfd_vma
*));
277 boolean (*check_relocs
) PARAMS ((bfd
*, struct bfd_link_info
*,
278 asection
*, const Elf_Internal_Rela
*));
280 Elf_Internal_Shdr
*hdr
;
284 Elf_External_Sym
*buf
= NULL
;
285 struct elf_link_hash_entry
**sym_hash
;
287 bfd_byte
*dynver
= NULL
;
288 Elf_External_Versym
*extversym
= NULL
;
289 Elf_External_Versym
*ever
;
290 Elf_External_Dyn
*dynbuf
= NULL
;
291 struct elf_link_hash_entry
*weaks
;
292 Elf_External_Sym
*esym
;
293 Elf_External_Sym
*esymend
;
295 add_symbol_hook
= get_elf_backend_data (abfd
)->elf_add_symbol_hook
;
296 collect
= get_elf_backend_data (abfd
)->collect
;
298 if ((abfd
->flags
& DYNAMIC
) == 0)
304 /* You can't use -r against a dynamic object. Also, there's no
305 hope of using a dynamic object which does not exactly match
306 the format of the output file. */
307 if (info
->relocateable
|| info
->hash
->creator
!= abfd
->xvec
)
309 bfd_set_error (bfd_error_invalid_operation
);
314 /* As a GNU extension, any input sections which are named
315 .gnu.warning.SYMBOL are treated as warning symbols for the given
316 symbol. This differs from .gnu.warning sections, which generate
317 warnings when they are included in an output file. */
322 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
326 name
= bfd_get_section_name (abfd
, s
);
327 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
332 name
+= sizeof ".gnu.warning." - 1;
334 /* If this is a shared object, then look up the symbol
335 in the hash table. If it is there, and it is already
336 been defined, then we will not be using the entry
337 from this shared object, so we don't need to warn.
338 FIXME: If we see the definition in a regular object
339 later on, we will warn, but we shouldn't. The only
340 fix is to keep track of what warnings we are supposed
341 to emit, and then handle them all at the end of the
343 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
345 struct elf_link_hash_entry
*h
;
347 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
350 /* FIXME: What about bfd_link_hash_common? */
352 && (h
->root
.type
== bfd_link_hash_defined
353 || h
->root
.type
== bfd_link_hash_defweak
))
355 /* We don't want to issue this warning. Clobber
356 the section size so that the warning does not
357 get copied into the output file. */
363 sz
= bfd_section_size (abfd
, s
);
364 msg
= (char *) bfd_alloc (abfd
, sz
);
368 if (! bfd_get_section_contents (abfd
, s
, msg
, (file_ptr
) 0, sz
))
371 if (! (_bfd_generic_link_add_one_symbol
372 (info
, abfd
, name
, BSF_WARNING
, s
, (bfd_vma
) 0, msg
,
373 false, collect
, (struct bfd_link_hash_entry
**) NULL
)))
376 if (! info
->relocateable
)
378 /* Clobber the section size so that the warning does
379 not get copied into the output file. */
386 /* If this is a dynamic object, we always link against the .dynsym
387 symbol table, not the .symtab symbol table. The dynamic linker
388 will only see the .dynsym symbol table, so there is no reason to
389 look at .symtab for a dynamic object. */
391 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
392 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
394 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
398 /* Read in any version definitions. */
400 if (! _bfd_elf_slurp_version_tables (abfd
))
403 /* Read in the symbol versions, but don't bother to convert them
404 to internal format. */
405 if (elf_dynversym (abfd
) != 0)
407 Elf_Internal_Shdr
*versymhdr
;
409 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
410 extversym
= (Elf_External_Versym
*) bfd_malloc (hdr
->sh_size
);
411 if (extversym
== NULL
)
413 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
414 || (bfd_read ((PTR
) extversym
, 1, versymhdr
->sh_size
, abfd
)
415 != versymhdr
->sh_size
))
420 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
422 /* The sh_info field of the symtab header tells us where the
423 external symbols start. We don't care about the local symbols at
425 if (elf_bad_symtab (abfd
))
427 extsymcount
= symcount
;
432 extsymcount
= symcount
- hdr
->sh_info
;
433 extsymoff
= hdr
->sh_info
;
436 buf
= ((Elf_External_Sym
*)
437 bfd_malloc (extsymcount
* sizeof (Elf_External_Sym
)));
438 if (buf
== NULL
&& extsymcount
!= 0)
441 /* We store a pointer to the hash table entry for each external
443 sym_hash
= ((struct elf_link_hash_entry
**)
445 extsymcount
* sizeof (struct elf_link_hash_entry
*)));
446 if (sym_hash
== NULL
)
448 elf_sym_hashes (abfd
) = sym_hash
;
452 /* If we are creating a shared library, create all the dynamic
453 sections immediately. We need to attach them to something,
454 so we attach them to this BFD, provided it is the right
455 format. FIXME: If there are no input BFD's of the same
456 format as the output, we can't make a shared library. */
458 && ! elf_hash_table (info
)->dynamic_sections_created
459 && abfd
->xvec
== info
->hash
->creator
)
461 if (! elf_link_create_dynamic_sections (abfd
, info
))
470 bfd_size_type oldsize
;
471 bfd_size_type strindex
;
473 /* Find the name to use in a DT_NEEDED entry that refers to this
474 object. If the object has a DT_SONAME entry, we use it.
475 Otherwise, if the generic linker stuck something in
476 elf_dt_name, we use that. Otherwise, we just use the file
477 name. If the generic linker put a null string into
478 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
479 there is a DT_SONAME entry. */
481 name
= bfd_get_filename (abfd
);
482 if (elf_dt_name (abfd
) != NULL
)
484 name
= elf_dt_name (abfd
);
488 s
= bfd_get_section_by_name (abfd
, ".dynamic");
491 Elf_External_Dyn
*extdyn
;
492 Elf_External_Dyn
*extdynend
;
496 dynbuf
= (Elf_External_Dyn
*) bfd_malloc ((size_t) s
->_raw_size
);
500 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
501 (file_ptr
) 0, s
->_raw_size
))
504 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
507 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
510 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
511 for (; extdyn
< extdynend
; extdyn
++)
513 Elf_Internal_Dyn dyn
;
515 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
516 if (dyn
.d_tag
== DT_SONAME
)
518 name
= bfd_elf_string_from_elf_section (abfd
, link
,
523 if (dyn
.d_tag
== DT_NEEDED
)
525 struct bfd_link_needed_list
*n
, **pn
;
528 n
= ((struct bfd_link_needed_list
*)
529 bfd_alloc (abfd
, sizeof (struct bfd_link_needed_list
)));
530 fnm
= bfd_elf_string_from_elf_section (abfd
, link
,
532 if (n
== NULL
|| fnm
== NULL
)
534 anm
= bfd_alloc (abfd
, strlen (fnm
) + 1);
541 for (pn
= &elf_hash_table (info
)->needed
;
553 /* We do not want to include any of the sections in a dynamic
554 object in the output file. We hack by simply clobbering the
555 list of sections in the BFD. This could be handled more
556 cleanly by, say, a new section flag; the existing
557 SEC_NEVER_LOAD flag is not the one we want, because that one
558 still implies that the section takes up space in the output
560 abfd
->sections
= NULL
;
561 abfd
->section_count
= 0;
563 /* If this is the first dynamic object found in the link, create
564 the special sections required for dynamic linking. */
565 if (! elf_hash_table (info
)->dynamic_sections_created
)
567 if (! elf_link_create_dynamic_sections (abfd
, info
))
573 /* Add a DT_NEEDED entry for this dynamic object. */
574 oldsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
575 strindex
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, name
,
577 if (strindex
== (bfd_size_type
) -1)
580 if (oldsize
== _bfd_stringtab_size (elf_hash_table (info
)->dynstr
))
583 Elf_External_Dyn
*dyncon
, *dynconend
;
585 /* The hash table size did not change, which means that
586 the dynamic object name was already entered. If we
587 have already included this dynamic object in the
588 link, just ignore it. There is no reason to include
589 a particular dynamic object more than once. */
590 sdyn
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
592 BFD_ASSERT (sdyn
!= NULL
);
594 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
595 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
597 for (; dyncon
< dynconend
; dyncon
++)
599 Elf_Internal_Dyn dyn
;
601 elf_swap_dyn_in (elf_hash_table (info
)->dynobj
, dyncon
,
603 if (dyn
.d_tag
== DT_NEEDED
604 && dyn
.d_un
.d_val
== strindex
)
608 if (extversym
!= NULL
)
615 if (! elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
619 /* Save the SONAME, if there is one, because sometimes the
620 linker emulation code will need to know it. */
622 name
= bfd_get_filename (abfd
);
623 elf_dt_name (abfd
) = name
;
627 hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
),
629 || (bfd_read ((PTR
) buf
, sizeof (Elf_External_Sym
), extsymcount
, abfd
)
630 != extsymcount
* sizeof (Elf_External_Sym
)))
635 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
636 esymend
= buf
+ extsymcount
;
639 esym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
641 Elf_Internal_Sym sym
;
647 struct elf_link_hash_entry
*h
;
649 boolean size_change_ok
, type_change_ok
;
651 unsigned int old_alignment
;
653 elf_swap_symbol_in (abfd
, esym
, &sym
);
655 flags
= BSF_NO_FLAGS
;
657 value
= sym
.st_value
;
660 bind
= ELF_ST_BIND (sym
.st_info
);
661 if (bind
== STB_LOCAL
)
663 /* This should be impossible, since ELF requires that all
664 global symbols follow all local symbols, and that sh_info
665 point to the first global symbol. Unfortunatealy, Irix 5
669 else if (bind
== STB_GLOBAL
)
671 if (sym
.st_shndx
!= SHN_UNDEF
672 && sym
.st_shndx
!= SHN_COMMON
)
677 else if (bind
== STB_WEAK
)
681 /* Leave it up to the processor backend. */
684 if (sym
.st_shndx
== SHN_UNDEF
)
685 sec
= bfd_und_section_ptr
;
686 else if (sym
.st_shndx
> 0 && sym
.st_shndx
< SHN_LORESERVE
)
688 sec
= section_from_elf_index (abfd
, sym
.st_shndx
);
690 sec
= bfd_abs_section_ptr
;
691 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
694 else if (sym
.st_shndx
== SHN_ABS
)
695 sec
= bfd_abs_section_ptr
;
696 else if (sym
.st_shndx
== SHN_COMMON
)
698 sec
= bfd_com_section_ptr
;
699 /* What ELF calls the size we call the value. What ELF
700 calls the value we call the alignment. */
705 /* Leave it up to the processor backend. */
708 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
709 if (name
== (const char *) NULL
)
714 if (! (*add_symbol_hook
) (abfd
, info
, &sym
, &name
, &flags
, &sec
,
718 /* The hook function sets the name to NULL if this symbol
719 should be skipped for some reason. */
720 if (name
== (const char *) NULL
)
724 /* Sanity check that all possibilities were handled. */
725 if (sec
== (asection
*) NULL
)
727 bfd_set_error (bfd_error_bad_value
);
731 if (bfd_is_und_section (sec
)
732 || bfd_is_com_section (sec
))
737 size_change_ok
= false;
738 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
740 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
742 Elf_Internal_Versym iver
;
749 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
750 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
752 /* If this is a hidden symbol, or if it is not version
753 1, we append the version name to the symbol name.
754 However, we do not modify a non-hidden absolute
755 symbol, because it might be the version symbol
756 itself. FIXME: What if it isn't? */
757 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
758 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
764 if (sym
.st_shndx
!= SHN_UNDEF
)
766 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
768 (*_bfd_error_handler
)
769 ("%s: %s: invalid version %d (max %d)",
770 abfd
->filename
, name
, vernum
,
771 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
772 bfd_set_error (bfd_error_bad_value
);
777 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
783 /* We cannot simply test for the number of
784 entries in the VERNEED section since the
785 numbers for the needed versions do not start
787 Elf_Internal_Verneed
*t
;
790 for (t
= elf_tdata (abfd
)->verref
;
794 Elf_Internal_Vernaux
*a
;
796 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
798 if (a
->vna_other
== vernum
)
800 verstr
= a
->vna_nodename
;
809 (*_bfd_error_handler
)
810 ("%s: %s: invalid needed version %d",
811 abfd
->filename
, name
, vernum
);
812 bfd_set_error (bfd_error_bad_value
);
817 namelen
= strlen (name
);
818 newlen
= namelen
+ strlen (verstr
) + 2;
819 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
822 newname
= (char *) bfd_alloc (abfd
, newlen
);
825 strcpy (newname
, name
);
826 p
= newname
+ namelen
;
828 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
836 /* We need to look up the symbol now in order to get some of
837 the dynamic object handling right. We pass the hash
838 table entry in to _bfd_generic_link_add_one_symbol so
839 that it does not have to look it up again. */
840 if (! bfd_is_und_section (sec
))
841 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
844 h
= ((struct elf_link_hash_entry
*)
845 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true,
851 if (h
->root
.type
== bfd_link_hash_new
)
852 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
855 while (h
->root
.type
== bfd_link_hash_indirect
856 || h
->root
.type
== bfd_link_hash_warning
)
858 if (h
->root
.type
== bfd_link_hash_indirect
)
860 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
863 /* FIXME: There are too many cases here, and it's too
864 confusing. This code needs to be reorganized somehow. */
866 /* It's OK to change the type if it used to be a weak
867 definition, or if the current definition is weak (and
868 hence might be ignored). */
869 if (h
->root
.type
== bfd_link_hash_defweak
870 || h
->root
.type
== bfd_link_hash_undefweak
872 type_change_ok
= true;
874 /* It's OK to change the size if it used to be a weak
875 definition, or if it used to be undefined, or if we will
876 be overriding an old definition. */
878 || h
->root
.type
== bfd_link_hash_undefined
)
879 size_change_ok
= true;
881 if (h
->root
.type
== bfd_link_hash_common
)
882 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
886 /* If we are looking at a dynamic object, and this is a
887 definition, we need to see if it has already been defined
888 by some other object. If it has, we want to use the
889 existing definition, and we do not want to report a
890 multiple symbol definition error; we do this by
891 clobbering sec to be bfd_und_section_ptr. We treat a
892 common symbol as a definition if the symbol in the shared
893 library is a function, since common symbols always
894 represent variables; this can cause confusion in
895 principle, but any such confusion would seem to indicate
896 an erroneous program or shared library. */
897 if (dynamic
&& definition
)
899 if (h
->root
.type
== bfd_link_hash_defined
900 || h
->root
.type
== bfd_link_hash_defweak
901 || (h
->root
.type
== bfd_link_hash_common
903 || ELF_ST_TYPE (sym
.st_info
) == STT_FUNC
)))
905 /* In the special case of two symbols which look
906 like common symbols in a dynamic object, set the
907 size of the symbol to the larger of the two. */
908 if ((sec
->flags
& SEC_ALLOC
) != 0
909 && (sec
->flags
& SEC_LOAD
) == 0
912 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
913 && h
->root
.type
== bfd_link_hash_defined
914 && (h
->elf_link_hash_flags
915 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
916 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) != 0
917 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
918 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
920 && h
->type
!= STT_FUNC
921 && sym
.st_size
!= h
->size
)
923 /* Note that we only warn if the size is
924 different. If the size is the same, then we
925 simply let the first shared library override
927 if (! ((*info
->callbacks
->multiple_common
)
928 (info
, h
->root
.root
.string
,
929 h
->root
.u
.def
.section
->owner
,
930 bfd_link_hash_common
,
931 h
->size
, abfd
, bfd_link_hash_common
,
934 if (sym
.st_size
> h
->size
)
935 h
->size
= sym
.st_size
;
939 sec
= bfd_und_section_ptr
;
941 size_change_ok
= true;
942 if (h
->root
.type
== bfd_link_hash_common
)
943 type_change_ok
= true;
947 /* If we already have a common symbol, and the symbol in the
948 shared library is in an uninitialized section, then treat
949 the shared library symbol as a common symbol. This will
950 not always be correct, but it should do little harm. */
953 && h
->root
.type
== bfd_link_hash_common
954 && (sec
->flags
& SEC_ALLOC
) != 0
955 && (sec
->flags
& SEC_LOAD
) == 0
958 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
)
961 sec
= bfd_com_section_ptr
;
964 size_change_ok
= true;
967 /* If we are not looking at a dynamic object, and we have a
968 definition, we want to override any definition we may
969 have from a dynamic object. Symbols from regular files
970 always take precedence over symbols from dynamic objects,
971 even if they are defined after the dynamic object in the
975 || (bfd_is_com_section (sec
)
976 && (h
->root
.type
== bfd_link_hash_defweak
977 || h
->type
== STT_FUNC
)))
978 && (h
->root
.type
== bfd_link_hash_defined
979 || h
->root
.type
== bfd_link_hash_defweak
)
980 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
981 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) != 0)
984 /* Change the hash table entry to undefined, and let
985 _bfd_generic_link_add_one_symbol do the right thing
986 with the new definition. */
987 h
->root
.type
= bfd_link_hash_undefined
;
988 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
989 size_change_ok
= true;
990 if (bfd_is_com_section (sec
))
991 type_change_ok
= true;
993 /* This union may have been set to be non-NULL when this
994 symbol was seen in a dynamic object. We must force
995 the union to be NULL, so that it is correct for a
997 h
->verinfo
.vertree
= NULL
;
1000 /* If we are not looking at a shared library and we have a
1001 common symbol, and the symbol in the shared library is in
1002 an uninitialized section, then treat the shared library
1003 symbol as a common symbol. This will not always be
1004 correct, but it should do little harm. Note that the
1005 above condition already handled cases in which a common
1006 symbol should simply override the definition in the
1010 && bfd_is_com_section (sec
)
1011 && h
->root
.type
== bfd_link_hash_defined
1012 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1013 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) != 0
1014 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1015 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1017 && h
->type
!= STT_FUNC
1020 /* It would be best if we could set the hash table entry
1021 to a common symbol, but we don't know what to use for
1022 the section or the alignment. */
1023 if (! ((*info
->callbacks
->multiple_common
)
1024 (info
, h
->root
.root
.string
,
1025 h
->root
.u
.def
.section
->owner
, bfd_link_hash_common
,
1026 h
->size
, abfd
, bfd_link_hash_common
, value
)))
1029 if (h
->size
> value
)
1032 /* FIXME: We no longer know the alignment required by
1033 the symbol in the shared library, so we just wind up
1034 using the one from the regular object. */
1037 h
->root
.type
= bfd_link_hash_undefined
;
1038 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1039 size_change_ok
= true;
1040 type_change_ok
= true;
1041 h
->verinfo
.vertree
= NULL
;
1047 && (h
->verinfo
.verdef
== NULL
|| definition
))
1048 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1051 if (! (_bfd_generic_link_add_one_symbol
1052 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
1053 false, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
1057 while (h
->root
.type
== bfd_link_hash_indirect
1058 || h
->root
.type
== bfd_link_hash_warning
)
1059 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1062 new_weakdef
= false;
1065 && (flags
& BSF_WEAK
) != 0
1066 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
1067 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1068 && h
->weakdef
== NULL
)
1070 /* Keep a list of all weak defined non function symbols from
1071 a dynamic object, using the weakdef field. Later in this
1072 function we will set the weakdef field to the correct
1073 value. We only put non-function symbols from dynamic
1074 objects on this list, because that happens to be the only
1075 time we need to know the normal symbol corresponding to a
1076 weak symbol, and the information is time consuming to
1077 figure out. If the weakdef field is not already NULL,
1078 then this symbol was already defined by some previous
1079 dynamic object, and we will be using that previous
1080 definition anyhow. */
1087 /* Set the alignment of a common symbol. */
1088 if (sym
.st_shndx
== SHN_COMMON
1089 && h
->root
.type
== bfd_link_hash_common
)
1093 align
= bfd_log2 (sym
.st_value
);
1094 if (align
> old_alignment
)
1095 h
->root
.u
.c
.p
->alignment_power
= align
;
1098 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1104 /* Remember the symbol size and type. */
1105 if (sym
.st_size
!= 0
1106 && (definition
|| h
->size
== 0))
1108 if (h
->size
!= 0 && h
->size
!= sym
.st_size
&& ! size_change_ok
)
1109 (*_bfd_error_handler
)
1110 ("Warning: size of symbol `%s' changed from %lu to %lu in %s",
1111 name
, (unsigned long) h
->size
, (unsigned long) sym
.st_size
,
1112 bfd_get_filename (abfd
));
1114 h
->size
= sym
.st_size
;
1117 /* If this is a common symbol, then we always want H->SIZE
1118 to be the size of the common symbol. The code just above
1119 won't fix the size if a common symbol becomes larger. We
1120 don't warn about a size change here, because that is
1121 covered by --warn-common. */
1122 if (h
->root
.type
== bfd_link_hash_common
)
1123 h
->size
= h
->root
.u
.c
.size
;
1125 if (ELF_ST_TYPE (sym
.st_info
) != STT_NOTYPE
1126 && (definition
|| h
->type
== STT_NOTYPE
))
1128 if (h
->type
!= STT_NOTYPE
1129 && h
->type
!= ELF_ST_TYPE (sym
.st_info
)
1130 && ! type_change_ok
)
1131 (*_bfd_error_handler
)
1132 ("Warning: type of symbol `%s' changed from %d to %d in %s",
1133 name
, h
->type
, ELF_ST_TYPE (sym
.st_info
),
1134 bfd_get_filename (abfd
));
1136 h
->type
= ELF_ST_TYPE (sym
.st_info
);
1139 if (sym
.st_other
!= 0
1140 && (definition
|| h
->other
== 0))
1141 h
->other
= sym
.st_other
;
1143 /* Set a flag in the hash table entry indicating the type of
1144 reference or definition we just found. Keep a count of
1145 the number of dynamic symbols we find. A dynamic symbol
1146 is one which is referenced or defined by both a regular
1147 object and a shared object. */
1148 old_flags
= h
->elf_link_hash_flags
;
1153 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
1155 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
1157 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1158 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
1164 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
1166 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
1167 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
1168 | ELF_LINK_HASH_REF_REGULAR
)) != 0
1169 || (h
->weakdef
!= NULL
1171 && h
->weakdef
->dynindx
!= -1))
1175 h
->elf_link_hash_flags
|= new_flag
;
1177 /* If this symbol has a version, and it is the default
1178 version, we create an indirect symbol from the default
1179 name to the fully decorated name. This will cause
1180 external references which do not specify a version to be
1181 bound to this version of the symbol. */
1186 p
= strchr (name
, ELF_VER_CHR
);
1187 if (p
!= NULL
&& p
[1] == ELF_VER_CHR
)
1190 struct elf_link_hash_entry
*hold
;
1192 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1194 if (shortname
== NULL
)
1196 strncpy (shortname
, name
, p
- name
);
1197 shortname
[p
- name
] = '\0';
1199 /* First look to see if we have an existing symbol
1201 hold
= elf_link_hash_lookup (elf_hash_table (info
),
1202 shortname
, false, false,
1205 /* If we are looking at a normal object, and the
1206 symbol was seen in a shared object, clobber the
1207 definition in the shared object. */
1210 && (hold
->root
.type
== bfd_link_hash_defined
1211 || hold
->root
.type
== bfd_link_hash_defweak
)
1212 && (hold
->elf_link_hash_flags
1213 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1214 && ((hold
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
)
1217 /* Change the hash table entry to undefined, so
1218 that _bfd_generic_link_add_one_symbol will do
1220 hold
->root
.type
= bfd_link_hash_undefined
;
1221 hold
->root
.u
.undef
.abfd
=
1222 hold
->root
.u
.def
.section
->owner
;
1223 hold
->verinfo
.vertree
= NULL
;
1227 /* If we are looking at a shared object, and we have
1228 already seen this symbol defined elsewhere, then
1229 don't try to define it again. */
1232 && (hold
->root
.type
== bfd_link_hash_defined
1233 || hold
->root
.type
== bfd_link_hash_defweak
1234 || hold
->root
.type
== bfd_link_hash_indirect
1235 || (hold
->root
.type
== bfd_link_hash_common
1236 && (bind
== STB_WEAK
1237 || ELF_ST_TYPE (sym
.st_info
) == STT_FUNC
))))
1239 /* Don't add an indirect symbol. */
1243 struct elf_link_hash_entry
*hi
;
1246 if (! (_bfd_generic_link_add_one_symbol
1247 (info
, abfd
, shortname
, BSF_INDIRECT
,
1248 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1249 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1252 /* If there is a duplicate definition somewhere,
1253 then HI may not point to an indirect symbol.
1254 We will have reported an error to the user in
1257 if (hi
->root
.type
== bfd_link_hash_indirect
)
1259 hi
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
1261 /* If the symbol became indirect, then we
1262 assume that we have not seen a definition
1264 BFD_ASSERT ((hi
->elf_link_hash_flags
1265 & (ELF_LINK_HASH_DEF_DYNAMIC
1266 | ELF_LINK_HASH_DEF_REGULAR
))
1269 /* Copy down any references that we may have
1270 already seen to the symbol which just
1272 h
->elf_link_hash_flags
|=
1273 (hi
->elf_link_hash_flags
1274 & (ELF_LINK_HASH_REF_DYNAMIC
1275 | ELF_LINK_HASH_REF_REGULAR
));
1277 /* Copy over the global table offset entry.
1278 This may have been already set up by a
1279 check_relocs routine. */
1280 if (h
->got_offset
== (bfd_vma
) -1)
1282 h
->got_offset
= hi
->got_offset
;
1283 hi
->got_offset
= (bfd_vma
) -1;
1285 BFD_ASSERT (hi
->got_offset
== (bfd_vma
) -1);
1287 if (h
->dynindx
== -1)
1289 h
->dynindx
= hi
->dynindx
;
1290 h
->dynstr_index
= hi
->dynstr_index
;
1292 hi
->dynstr_index
= 0;
1294 BFD_ASSERT (hi
->dynindx
== -1);
1296 /* FIXME: There may be other information to
1297 copy over for particular targets. */
1299 /* See if the new flags lead us to realize
1300 that the symbol must be dynamic. */
1306 || ((hi
->elf_link_hash_flags
1307 & ELF_LINK_HASH_REF_DYNAMIC
)
1313 if ((hi
->elf_link_hash_flags
1314 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1321 /* We also need to define an indirection from the
1322 nondefault version of the symbol. */
1324 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1326 if (shortname
== NULL
)
1328 strncpy (shortname
, name
, p
- name
);
1329 strcpy (shortname
+ (p
- name
), p
+ 1);
1331 /* First look to see if we have an existing symbol
1333 hold
= elf_link_hash_lookup (elf_hash_table (info
),
1334 shortname
, false, false,
1337 /* If we are looking at a normal object, and the
1338 symbol was seen in a shared object, clobber the
1339 definition in the shared object. */
1342 && (hold
->root
.type
== bfd_link_hash_defined
1343 || hold
->root
.type
== bfd_link_hash_defweak
)
1344 && (hold
->elf_link_hash_flags
1345 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1346 && ((hold
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
)
1349 /* Change the hash table entry to undefined, so
1350 that _bfd_generic_link_add_one_symbol will do
1352 hold
->root
.type
= bfd_link_hash_undefined
;
1353 hold
->root
.u
.undef
.abfd
=
1354 hold
->root
.u
.def
.section
->owner
;
1355 hold
->verinfo
.vertree
= NULL
;
1359 /* If we are looking at a shared object, and we have
1360 already seen this symbol defined elsewhere, then
1361 don't try to define it again. */
1364 && (hold
->root
.type
== bfd_link_hash_defined
1365 || hold
->root
.type
== bfd_link_hash_defweak
1366 || hold
->root
.type
== bfd_link_hash_indirect
1367 || (hold
->root
.type
== bfd_link_hash_common
1368 && (bind
== STB_WEAK
1369 || ELF_ST_TYPE (sym
.st_info
) == STT_FUNC
))))
1371 /* Don't add an indirect symbol. */
1375 struct elf_link_hash_entry
*hi
;
1378 if (! (_bfd_generic_link_add_one_symbol
1379 (info
, abfd
, shortname
, BSF_INDIRECT
,
1380 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1381 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1384 /* If there is a duplicate definition somewhere,
1385 then HI may not point to an indirect symbol.
1386 We will have reported an error to the user in
1389 if (hi
->root
.type
== bfd_link_hash_indirect
)
1391 hi
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
1393 /* If the symbol became indirect, then we
1394 assume that we have not seen a definition
1396 BFD_ASSERT ((hi
->elf_link_hash_flags
1397 & (ELF_LINK_HASH_DEF_DYNAMIC
1398 | ELF_LINK_HASH_DEF_REGULAR
))
1401 /* Copy down any references that we may have
1402 already seen to the symbol which just
1404 h
->elf_link_hash_flags
|=
1405 (hi
->elf_link_hash_flags
1406 & (ELF_LINK_HASH_REF_DYNAMIC
1407 | ELF_LINK_HASH_REF_REGULAR
));
1409 /* Copy over the global table offset entry.
1410 This may have been already set up by a
1411 check_relocs routine. */
1412 if (h
->got_offset
== (bfd_vma
) -1)
1414 h
->got_offset
= hi
->got_offset
;
1415 hi
->got_offset
= (bfd_vma
) -1;
1417 BFD_ASSERT (hi
->got_offset
== (bfd_vma
) -1);
1419 if (h
->dynindx
== -1)
1421 h
->dynindx
= hi
->dynindx
;
1422 h
->dynstr_index
= hi
->dynstr_index
;
1424 hi
->dynstr_index
= 0;
1426 BFD_ASSERT (hi
->dynindx
== -1);
1428 /* FIXME: There may be other information to
1429 copy over for particular targets. */
1431 /* See if the new flags lead us to realize
1432 that the symbol must be dynamic. */
1438 || ((hi
->elf_link_hash_flags
1439 & ELF_LINK_HASH_REF_DYNAMIC
)
1445 if ((hi
->elf_link_hash_flags
1446 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1455 if (dynsym
&& h
->dynindx
== -1)
1457 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1459 if (h
->weakdef
!= NULL
1461 && h
->weakdef
->dynindx
== -1)
1463 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1471 /* Now set the weakdefs field correctly for all the weak defined
1472 symbols we found. The only way to do this is to search all the
1473 symbols. Since we only need the information for non functions in
1474 dynamic objects, that's the only time we actually put anything on
1475 the list WEAKS. We need this information so that if a regular
1476 object refers to a symbol defined weakly in a dynamic object, the
1477 real symbol in the dynamic object is also put in the dynamic
1478 symbols; we also must arrange for both symbols to point to the
1479 same memory location. We could handle the general case of symbol
1480 aliasing, but a general symbol alias can only be generated in
1481 assembler code, handling it correctly would be very time
1482 consuming, and other ELF linkers don't handle general aliasing
1484 while (weaks
!= NULL
)
1486 struct elf_link_hash_entry
*hlook
;
1489 struct elf_link_hash_entry
**hpp
;
1490 struct elf_link_hash_entry
**hppend
;
1493 weaks
= hlook
->weakdef
;
1494 hlook
->weakdef
= NULL
;
1496 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
1497 || hlook
->root
.type
== bfd_link_hash_defweak
1498 || hlook
->root
.type
== bfd_link_hash_common
1499 || hlook
->root
.type
== bfd_link_hash_indirect
);
1500 slook
= hlook
->root
.u
.def
.section
;
1501 vlook
= hlook
->root
.u
.def
.value
;
1503 hpp
= elf_sym_hashes (abfd
);
1504 hppend
= hpp
+ extsymcount
;
1505 for (; hpp
< hppend
; hpp
++)
1507 struct elf_link_hash_entry
*h
;
1510 if (h
!= NULL
&& h
!= hlook
1511 && h
->root
.type
== bfd_link_hash_defined
1512 && h
->root
.u
.def
.section
== slook
1513 && h
->root
.u
.def
.value
== vlook
)
1517 /* If the weak definition is in the list of dynamic
1518 symbols, make sure the real definition is put there
1520 if (hlook
->dynindx
!= -1
1521 && h
->dynindx
== -1)
1523 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1527 /* If the real definition is in the list of dynamic
1528 symbols, make sure the weak definition is put there
1529 as well. If we don't do this, then the dynamic
1530 loader might not merge the entries for the real
1531 definition and the weak definition. */
1532 if (h
->dynindx
!= -1
1533 && hlook
->dynindx
== -1)
1535 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
1550 if (extversym
!= NULL
)
1556 /* If this object is the same format as the output object, and it is
1557 not a shared library, then let the backend look through the
1560 This is required to build global offset table entries and to
1561 arrange for dynamic relocs. It is not required for the
1562 particular common case of linking non PIC code, even when linking
1563 against shared libraries, but unfortunately there is no way of
1564 knowing whether an object file has been compiled PIC or not.
1565 Looking through the relocs is not particularly time consuming.
1566 The problem is that we must either (1) keep the relocs in memory,
1567 which causes the linker to require additional runtime memory or
1568 (2) read the relocs twice from the input file, which wastes time.
1569 This would be a good case for using mmap.
1571 I have no idea how to handle linking PIC code into a file of a
1572 different format. It probably can't be done. */
1573 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
1575 && abfd
->xvec
== info
->hash
->creator
1576 && check_relocs
!= NULL
)
1580 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1582 Elf_Internal_Rela
*internal_relocs
;
1585 if ((o
->flags
& SEC_RELOC
) == 0
1586 || o
->reloc_count
== 0
1587 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
1588 && (o
->flags
& SEC_DEBUGGING
) != 0)
1589 || bfd_is_abs_section (o
->output_section
))
1592 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
1593 (abfd
, o
, (PTR
) NULL
,
1594 (Elf_Internal_Rela
*) NULL
,
1595 info
->keep_memory
));
1596 if (internal_relocs
== NULL
)
1599 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
1601 if (! info
->keep_memory
)
1602 free (internal_relocs
);
1609 /* If this is a non-traditional, non-relocateable link, try to
1610 optimize the handling of the .stab/.stabstr sections. */
1612 && ! info
->relocateable
1613 && ! info
->traditional_format
1614 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1615 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
1617 asection
*stab
, *stabstr
;
1619 stab
= bfd_get_section_by_name (abfd
, ".stab");
1622 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
1624 if (stabstr
!= NULL
)
1626 struct bfd_elf_section_data
*secdata
;
1628 secdata
= elf_section_data (stab
);
1629 if (! _bfd_link_section_stabs (abfd
,
1630 &elf_hash_table (info
)->stab_info
,
1632 &secdata
->stab_info
))
1647 if (extversym
!= NULL
)
1652 /* Create some sections which will be filled in with dynamic linking
1653 information. ABFD is an input file which requires dynamic sections
1654 to be created. The dynamic sections take up virtual memory space
1655 when the final executable is run, so we need to create them before
1656 addresses are assigned to the output sections. We work out the
1657 actual contents and size of these sections later. */
1660 elf_link_create_dynamic_sections (abfd
, info
)
1662 struct bfd_link_info
*info
;
1665 register asection
*s
;
1666 struct elf_link_hash_entry
*h
;
1667 struct elf_backend_data
*bed
;
1669 if (elf_hash_table (info
)->dynamic_sections_created
)
1672 /* Make sure that all dynamic sections use the same input BFD. */
1673 if (elf_hash_table (info
)->dynobj
== NULL
)
1674 elf_hash_table (info
)->dynobj
= abfd
;
1676 abfd
= elf_hash_table (info
)->dynobj
;
1678 /* Note that we set the SEC_IN_MEMORY flag for all of these
1680 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1681 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
1683 /* A dynamically linked executable has a .interp section, but a
1684 shared library does not. */
1687 s
= bfd_make_section (abfd
, ".interp");
1689 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
1693 /* Create sections to hold version informations. These are removed
1694 if they are not needed. */
1695 s
= bfd_make_section (abfd
, ".gnu.version_d");
1697 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1698 || ! bfd_set_section_alignment (abfd
, s
, 2))
1701 s
= bfd_make_section (abfd
, ".gnu.version");
1703 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1704 || ! bfd_set_section_alignment (abfd
, s
, 1))
1707 s
= bfd_make_section (abfd
, ".gnu.version_r");
1709 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1710 || ! bfd_set_section_alignment (abfd
, s
, 2))
1713 s
= bfd_make_section (abfd
, ".dynsym");
1715 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1716 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1719 s
= bfd_make_section (abfd
, ".dynstr");
1721 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
1724 /* Create a strtab to hold the dynamic symbol names. */
1725 if (elf_hash_table (info
)->dynstr
== NULL
)
1727 elf_hash_table (info
)->dynstr
= elf_stringtab_init ();
1728 if (elf_hash_table (info
)->dynstr
== NULL
)
1732 s
= bfd_make_section (abfd
, ".dynamic");
1734 || ! bfd_set_section_flags (abfd
, s
, flags
)
1735 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1738 /* The special symbol _DYNAMIC is always set to the start of the
1739 .dynamic section. This call occurs before we have processed the
1740 symbols for any dynamic object, so we don't have to worry about
1741 overriding a dynamic definition. We could set _DYNAMIC in a
1742 linker script, but we only want to define it if we are, in fact,
1743 creating a .dynamic section. We don't want to define it if there
1744 is no .dynamic section, since on some ELF platforms the start up
1745 code examines it to decide how to initialize the process. */
1747 if (! (_bfd_generic_link_add_one_symbol
1748 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
1749 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
1750 (struct bfd_link_hash_entry
**) &h
)))
1752 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
1753 h
->type
= STT_OBJECT
;
1756 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1759 s
= bfd_make_section (abfd
, ".hash");
1761 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1762 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1765 /* Let the backend create the rest of the sections. This lets the
1766 backend set the right flags. The backend will normally create
1767 the .got and .plt sections. */
1768 bed
= get_elf_backend_data (abfd
);
1769 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
1772 elf_hash_table (info
)->dynamic_sections_created
= true;
1777 /* Add an entry to the .dynamic table. */
1780 elf_add_dynamic_entry (info
, tag
, val
)
1781 struct bfd_link_info
*info
;
1785 Elf_Internal_Dyn dyn
;
1789 bfd_byte
*newcontents
;
1791 dynobj
= elf_hash_table (info
)->dynobj
;
1793 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
1794 BFD_ASSERT (s
!= NULL
);
1796 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
1797 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
1798 if (newcontents
== NULL
)
1802 dyn
.d_un
.d_val
= val
;
1803 elf_swap_dyn_out (dynobj
, &dyn
,
1804 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
1806 s
->_raw_size
= newsize
;
1807 s
->contents
= newcontents
;
1813 /* Read and swap the relocs for a section. They may have been cached.
1814 If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are not NULL,
1815 they are used as buffers to read into. They are known to be large
1816 enough. If the INTERNAL_RELOCS relocs argument is NULL, the return
1817 value is allocated using either malloc or bfd_alloc, according to
1818 the KEEP_MEMORY argument. */
1821 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
1825 PTR external_relocs
;
1826 Elf_Internal_Rela
*internal_relocs
;
1827 boolean keep_memory
;
1829 Elf_Internal_Shdr
*rel_hdr
;
1831 Elf_Internal_Rela
*alloc2
= NULL
;
1833 if (elf_section_data (o
)->relocs
!= NULL
)
1834 return elf_section_data (o
)->relocs
;
1836 if (o
->reloc_count
== 0)
1839 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
1841 if (internal_relocs
== NULL
)
1845 size
= o
->reloc_count
* sizeof (Elf_Internal_Rela
);
1847 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
1849 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
1850 if (internal_relocs
== NULL
)
1854 if (external_relocs
== NULL
)
1856 alloc1
= (PTR
) bfd_malloc ((size_t) rel_hdr
->sh_size
);
1859 external_relocs
= alloc1
;
1862 if ((bfd_seek (abfd
, rel_hdr
->sh_offset
, SEEK_SET
) != 0)
1863 || (bfd_read (external_relocs
, 1, rel_hdr
->sh_size
, abfd
)
1864 != rel_hdr
->sh_size
))
1867 /* Swap in the relocs. For convenience, we always produce an
1868 Elf_Internal_Rela array; if the relocs are Rel, we set the addend
1870 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
1872 Elf_External_Rel
*erel
;
1873 Elf_External_Rel
*erelend
;
1874 Elf_Internal_Rela
*irela
;
1876 erel
= (Elf_External_Rel
*) external_relocs
;
1877 erelend
= erel
+ o
->reloc_count
;
1878 irela
= internal_relocs
;
1879 for (; erel
< erelend
; erel
++, irela
++)
1881 Elf_Internal_Rel irel
;
1883 elf_swap_reloc_in (abfd
, erel
, &irel
);
1884 irela
->r_offset
= irel
.r_offset
;
1885 irela
->r_info
= irel
.r_info
;
1886 irela
->r_addend
= 0;
1891 Elf_External_Rela
*erela
;
1892 Elf_External_Rela
*erelaend
;
1893 Elf_Internal_Rela
*irela
;
1895 BFD_ASSERT (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
));
1897 erela
= (Elf_External_Rela
*) external_relocs
;
1898 erelaend
= erela
+ o
->reloc_count
;
1899 irela
= internal_relocs
;
1900 for (; erela
< erelaend
; erela
++, irela
++)
1901 elf_swap_reloca_in (abfd
, erela
, irela
);
1904 /* Cache the results for next time, if we can. */
1906 elf_section_data (o
)->relocs
= internal_relocs
;
1911 /* Don't free alloc2, since if it was allocated we are passing it
1912 back (under the name of internal_relocs). */
1914 return internal_relocs
;
1925 /* Record an assignment to a symbol made by a linker script. We need
1926 this in case some dynamic object refers to this symbol. */
1930 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
1932 struct bfd_link_info
*info
;
1936 struct elf_link_hash_entry
*h
;
1938 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1941 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, true, false);
1945 if (h
->root
.type
== bfd_link_hash_new
)
1946 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
1948 /* If this symbol is being provided by the linker script, and it is
1949 currently defined by a dynamic object, but not by a regular
1950 object, then mark it as undefined so that the generic linker will
1951 force the correct value. */
1953 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1954 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1955 h
->root
.type
= bfd_link_hash_undefined
;
1957 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
1958 h
->type
= STT_OBJECT
;
1960 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1961 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
1963 && h
->dynindx
== -1)
1965 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1968 /* If this is a weak defined symbol, and we know a corresponding
1969 real symbol from the same dynamic object, make sure the real
1970 symbol is also made into a dynamic symbol. */
1971 if (h
->weakdef
!= NULL
1972 && h
->weakdef
->dynindx
== -1)
1974 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
1982 /* This structure is used to pass information to
1983 elf_link_assign_sym_version. */
1985 struct elf_assign_sym_version_info
1989 /* General link information. */
1990 struct bfd_link_info
*info
;
1992 struct bfd_elf_version_tree
*verdefs
;
1993 /* Whether we are exporting all dynamic symbols. */
1994 boolean export_dynamic
;
1995 /* Whether we removed any symbols from the dynamic symbol table. */
1996 boolean removed_dynamic
;
1997 /* Whether we had a failure. */
2001 /* This structure is used to pass information to
2002 elf_link_find_version_dependencies. */
2004 struct elf_find_verdep_info
2008 /* General link information. */
2009 struct bfd_link_info
*info
;
2010 /* The number of dependencies. */
2012 /* Whether we had a failure. */
2016 /* Array used to determine the number of hash table buckets to use
2017 based on the number of symbols there are. If there are fewer than
2018 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2019 fewer than 37 we use 17 buckets, and so forth. We never use more
2020 than 32771 buckets. */
2022 static const size_t elf_buckets
[] =
2024 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2028 /* Set up the sizes and contents of the ELF dynamic sections. This is
2029 called by the ELF linker emulation before_allocation routine. We
2030 must set the sizes of the sections before the linker sets the
2031 addresses of the various sections. */
2034 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
2035 export_dynamic
, filter_shlib
,
2036 auxiliary_filters
, info
, sinterpptr
,
2041 boolean export_dynamic
;
2042 const char *filter_shlib
;
2043 const char * const *auxiliary_filters
;
2044 struct bfd_link_info
*info
;
2045 asection
**sinterpptr
;
2046 struct bfd_elf_version_tree
*verdefs
;
2048 bfd_size_type soname_indx
;
2050 struct elf_backend_data
*bed
;
2051 bfd_size_type old_dynsymcount
;
2052 struct elf_assign_sym_version_info asvinfo
;
2058 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2061 /* The backend may have to create some sections regardless of whether
2062 we're dynamic or not. */
2063 bed
= get_elf_backend_data (output_bfd
);
2064 if (bed
->elf_backend_always_size_sections
2065 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
2068 dynobj
= elf_hash_table (info
)->dynobj
;
2070 /* If there were no dynamic objects in the link, there is nothing to
2075 /* If we are supposed to export all symbols into the dynamic symbol
2076 table (this is not the normal case), then do so. */
2079 struct elf_info_failed eif
;
2083 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
2089 if (elf_hash_table (info
)->dynamic_sections_created
)
2091 struct elf_info_failed eif
;
2092 struct elf_link_hash_entry
*h
;
2093 bfd_size_type strsize
;
2095 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
2096 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
2100 soname_indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2101 soname
, true, true);
2102 if (soname_indx
== (bfd_size_type
) -1
2103 || ! elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
2109 if (! elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
2117 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, rpath
,
2119 if (indx
== (bfd_size_type
) -1
2120 || ! elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
2124 if (filter_shlib
!= NULL
)
2128 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2129 filter_shlib
, true, true);
2130 if (indx
== (bfd_size_type
) -1
2131 || ! elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
2135 if (auxiliary_filters
!= NULL
)
2137 const char * const *p
;
2139 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
2143 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2145 if (indx
== (bfd_size_type
) -1
2146 || ! elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
2151 /* Attach all the symbols to their version information. */
2152 asvinfo
.output_bfd
= output_bfd
;
2153 asvinfo
.info
= info
;
2154 asvinfo
.verdefs
= verdefs
;
2155 asvinfo
.export_dynamic
= export_dynamic
;
2156 asvinfo
.removed_dynamic
= false;
2157 asvinfo
.failed
= false;
2159 elf_link_hash_traverse (elf_hash_table (info
),
2160 elf_link_assign_sym_version
,
2165 /* Find all symbols which were defined in a dynamic object and make
2166 the backend pick a reasonable value for them. */
2169 elf_link_hash_traverse (elf_hash_table (info
),
2170 elf_adjust_dynamic_symbol
,
2175 /* Add some entries to the .dynamic section. We fill in some of the
2176 values later, in elf_bfd_final_link, but we must add the entries
2177 now so that we know the final size of the .dynamic section. */
2178 h
= elf_link_hash_lookup (elf_hash_table (info
), "_init", false,
2181 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2182 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2184 if (! elf_add_dynamic_entry (info
, DT_INIT
, 0))
2187 h
= elf_link_hash_lookup (elf_hash_table (info
), "_fini", false,
2190 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2191 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2193 if (! elf_add_dynamic_entry (info
, DT_FINI
, 0))
2196 strsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
2197 if (! elf_add_dynamic_entry (info
, DT_HASH
, 0)
2198 || ! elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
2199 || ! elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
2200 || ! elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
2201 || ! elf_add_dynamic_entry (info
, DT_SYMENT
,
2202 sizeof (Elf_External_Sym
)))
2206 /* The backend must work out the sizes of all the other dynamic
2208 old_dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2209 if (! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
2212 if (elf_hash_table (info
)->dynamic_sections_created
)
2217 size_t bucketcount
= 0;
2218 Elf_Internal_Sym isym
;
2220 /* Set up the version definition section. */
2221 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2222 BFD_ASSERT (s
!= NULL
);
2224 /* We may have created additional version definitions if we are
2225 just linking a regular application. */
2226 verdefs
= asvinfo
.verdefs
;
2228 if (verdefs
== NULL
)
2232 /* Don't include this section in the output file. */
2233 for (spp
= &output_bfd
->sections
;
2234 *spp
!= s
->output_section
;
2235 spp
= &(*spp
)->next
)
2237 *spp
= s
->output_section
->next
;
2238 --output_bfd
->section_count
;
2244 struct bfd_elf_version_tree
*t
;
2246 Elf_Internal_Verdef def
;
2247 Elf_Internal_Verdaux defaux
;
2249 if (asvinfo
.removed_dynamic
)
2251 /* Some dynamic symbols were changed to be local
2252 symbols. In this case, we renumber all of the
2253 dynamic symbols, so that we don't have a hole. If
2254 the backend changed dynsymcount, then assume that the
2255 new symbols are at the start. This is the case on
2256 the MIPS. FIXME: The names of the removed symbols
2257 will still be in the dynamic string table, wasting
2259 elf_hash_table (info
)->dynsymcount
=
2260 1 + (elf_hash_table (info
)->dynsymcount
- old_dynsymcount
);
2261 elf_link_hash_traverse (elf_hash_table (info
),
2262 elf_link_renumber_dynsyms
,
2269 /* Make space for the base version. */
2270 size
+= sizeof (Elf_External_Verdef
);
2271 size
+= sizeof (Elf_External_Verdaux
);
2274 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2276 struct bfd_elf_version_deps
*n
;
2278 size
+= sizeof (Elf_External_Verdef
);
2279 size
+= sizeof (Elf_External_Verdaux
);
2282 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2283 size
+= sizeof (Elf_External_Verdaux
);
2286 s
->_raw_size
= size
;
2287 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2288 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2291 /* Fill in the version definition section. */
2295 def
.vd_version
= VER_DEF_CURRENT
;
2296 def
.vd_flags
= VER_FLG_BASE
;
2299 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2300 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2301 + sizeof (Elf_External_Verdaux
));
2303 if (soname_indx
!= -1)
2305 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) soname
);
2306 defaux
.vda_name
= soname_indx
;
2313 name
= output_bfd
->filename
;
2314 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) name
);
2315 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2317 if (indx
== (bfd_size_type
) -1)
2319 defaux
.vda_name
= indx
;
2321 defaux
.vda_next
= 0;
2323 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2324 (Elf_External_Verdef
*)p
);
2325 p
+= sizeof (Elf_External_Verdef
);
2326 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2327 (Elf_External_Verdaux
*) p
);
2328 p
+= sizeof (Elf_External_Verdaux
);
2330 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2333 struct bfd_elf_version_deps
*n
;
2334 struct elf_link_hash_entry
*h
;
2337 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2340 /* Add a symbol representing this version. */
2342 if (! (_bfd_generic_link_add_one_symbol
2343 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
2344 (bfd_vma
) 0, (const char *) NULL
, false,
2345 get_elf_backend_data (dynobj
)->collect
,
2346 (struct bfd_link_hash_entry
**) &h
)))
2348 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
2349 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2350 h
->type
= STT_OBJECT
;
2351 h
->verinfo
.vertree
= t
;
2353 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2356 def
.vd_version
= VER_DEF_CURRENT
;
2358 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
2359 def
.vd_flags
|= VER_FLG_WEAK
;
2360 def
.vd_ndx
= t
->vernum
+ 1;
2361 def
.vd_cnt
= cdeps
+ 1;
2362 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) t
->name
);
2363 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2364 if (t
->next
!= NULL
)
2365 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2366 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
2370 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2371 (Elf_External_Verdef
*) p
);
2372 p
+= sizeof (Elf_External_Verdef
);
2374 defaux
.vda_name
= h
->dynstr_index
;
2375 if (t
->deps
== NULL
)
2376 defaux
.vda_next
= 0;
2378 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2379 t
->name_indx
= defaux
.vda_name
;
2381 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2382 (Elf_External_Verdaux
*) p
);
2383 p
+= sizeof (Elf_External_Verdaux
);
2385 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2387 defaux
.vda_name
= n
->version_needed
->name_indx
;
2388 if (n
->next
== NULL
)
2389 defaux
.vda_next
= 0;
2391 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2393 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2394 (Elf_External_Verdaux
*) p
);
2395 p
+= sizeof (Elf_External_Verdaux
);
2399 if (! elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
2400 || ! elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
2403 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
2406 /* Work out the size of the version reference section. */
2408 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2409 BFD_ASSERT (s
!= NULL
);
2411 struct elf_find_verdep_info sinfo
;
2413 sinfo
.output_bfd
= output_bfd
;
2415 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
2416 if (sinfo
.vers
== 0)
2418 sinfo
.failed
= false;
2420 elf_link_hash_traverse (elf_hash_table (info
),
2421 elf_link_find_version_dependencies
,
2424 if (elf_tdata (output_bfd
)->verref
== NULL
)
2428 /* We don't have any version definitions, so we can just
2429 remove the section. */
2431 for (spp
= &output_bfd
->sections
;
2432 *spp
!= s
->output_section
;
2433 spp
= &(*spp
)->next
)
2435 *spp
= s
->output_section
->next
;
2436 --output_bfd
->section_count
;
2440 Elf_Internal_Verneed
*t
;
2445 /* Build the version definition section. */
2448 for (t
= elf_tdata (output_bfd
)->verref
;
2452 Elf_Internal_Vernaux
*a
;
2454 size
+= sizeof (Elf_External_Verneed
);
2456 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2457 size
+= sizeof (Elf_External_Vernaux
);
2460 s
->_raw_size
= size
;
2461 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, size
);
2462 if (s
->contents
== NULL
)
2466 for (t
= elf_tdata (output_bfd
)->verref
;
2471 Elf_Internal_Vernaux
*a
;
2475 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2478 t
->vn_version
= VER_NEED_CURRENT
;
2480 if (elf_dt_name (t
->vn_bfd
) != NULL
)
2481 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2482 elf_dt_name (t
->vn_bfd
),
2485 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2486 t
->vn_bfd
->filename
, true, false);
2487 if (indx
== (bfd_size_type
) -1)
2490 t
->vn_aux
= sizeof (Elf_External_Verneed
);
2491 if (t
->vn_nextref
== NULL
)
2494 t
->vn_next
= (sizeof (Elf_External_Verneed
)
2495 + caux
* sizeof (Elf_External_Vernaux
));
2497 _bfd_elf_swap_verneed_out (output_bfd
, t
,
2498 (Elf_External_Verneed
*) p
);
2499 p
+= sizeof (Elf_External_Verneed
);
2501 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2503 a
->vna_hash
= bfd_elf_hash ((const unsigned char *)
2505 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2506 a
->vna_nodename
, true, false);
2507 if (indx
== (bfd_size_type
) -1)
2510 if (a
->vna_nextptr
== NULL
)
2513 a
->vna_next
= sizeof (Elf_External_Vernaux
);
2515 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
2516 (Elf_External_Vernaux
*) p
);
2517 p
+= sizeof (Elf_External_Vernaux
);
2521 if (! elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
2522 || ! elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
2525 elf_tdata (output_bfd
)->cverrefs
= crefs
;
2529 dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2531 /* Work out the size of the symbol version section. */
2532 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
2533 BFD_ASSERT (s
!= NULL
);
2534 if (dynsymcount
== 0
2535 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
2539 /* We don't need any symbol versions; just discard the
2541 for (spp
= &output_bfd
->sections
;
2542 *spp
!= s
->output_section
;
2543 spp
= &(*spp
)->next
)
2545 *spp
= s
->output_section
->next
;
2546 --output_bfd
->section_count
;
2550 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
2551 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
2552 if (s
->contents
== NULL
)
2555 if (! elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
2559 /* Set the size of the .dynsym and .hash sections. We counted
2560 the number of dynamic symbols in elf_link_add_object_symbols.
2561 We will build the contents of .dynsym and .hash when we build
2562 the final symbol table, because until then we do not know the
2563 correct value to give the symbols. We built the .dynstr
2564 section as we went along in elf_link_add_object_symbols. */
2565 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
2566 BFD_ASSERT (s
!= NULL
);
2567 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
2568 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2569 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2572 /* The first entry in .dynsym is a dummy symbol. */
2579 elf_swap_symbol_out (output_bfd
, &isym
,
2580 (PTR
) (Elf_External_Sym
*) s
->contents
);
2582 for (i
= 0; elf_buckets
[i
] != 0; i
++)
2584 bucketcount
= elf_buckets
[i
];
2585 if (dynsymcount
< elf_buckets
[i
+ 1])
2589 s
= bfd_get_section_by_name (dynobj
, ".hash");
2590 BFD_ASSERT (s
!= NULL
);
2591 s
->_raw_size
= (2 + bucketcount
+ dynsymcount
) * (ARCH_SIZE
/ 8);
2592 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2593 if (s
->contents
== NULL
)
2595 memset (s
->contents
, 0, (size_t) s
->_raw_size
);
2597 put_word (output_bfd
, bucketcount
, s
->contents
);
2598 put_word (output_bfd
, dynsymcount
, s
->contents
+ (ARCH_SIZE
/ 8));
2600 elf_hash_table (info
)->bucketcount
= bucketcount
;
2602 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
2603 BFD_ASSERT (s
!= NULL
);
2604 s
->_raw_size
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
2606 if (! elf_add_dynamic_entry (info
, DT_NULL
, 0))
2613 /* Fix up the flags for a symbol. This handles various cases which
2614 can only be fixed after all the input files are seen. This is
2615 currently called by both adjust_dynamic_symbol and
2616 assign_sym_version, which is unnecessary but perhaps more robust in
2617 the face of future changes. */
2620 elf_fix_symbol_flags (h
, eif
)
2621 struct elf_link_hash_entry
*h
;
2622 struct elf_info_failed
*eif
;
2624 /* If this symbol was mentioned in a non-ELF file, try to set
2625 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2626 permit a non-ELF file to correctly refer to a symbol defined in
2627 an ELF dynamic object. */
2628 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
2630 if (h
->root
.type
!= bfd_link_hash_defined
2631 && h
->root
.type
!= bfd_link_hash_defweak
)
2632 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2635 if (h
->root
.u
.def
.section
->owner
!= NULL
2636 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2637 == bfd_target_elf_flavour
))
2638 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2640 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2643 if (h
->dynindx
== -1
2644 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2645 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
2647 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2655 /* If this is a final link, and the symbol was defined as a common
2656 symbol in a regular object file, and there was no definition in
2657 any dynamic object, then the linker will have allocated space for
2658 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2659 flag will not have been set. */
2660 if (h
->root
.type
== bfd_link_hash_defined
2661 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2662 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
2663 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2664 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2665 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2667 /* If -Bsymbolic was used (which means to bind references to global
2668 symbols to the definition within the shared object), and this
2669 symbol was defined in a regular object, then it actually doesn't
2670 need a PLT entry. */
2671 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
2672 && eif
->info
->shared
2673 && eif
->info
->symbolic
2674 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2675 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
2680 /* Make the backend pick a good value for a dynamic symbol. This is
2681 called via elf_link_hash_traverse, and also calls itself
2685 elf_adjust_dynamic_symbol (h
, data
)
2686 struct elf_link_hash_entry
*h
;
2689 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2691 struct elf_backend_data
*bed
;
2693 /* Ignore indirect symbols. These are added by the versioning code. */
2694 if (h
->root
.type
== bfd_link_hash_indirect
)
2697 /* Fix the symbol flags. */
2698 if (! elf_fix_symbol_flags (h
, eif
))
2701 /* If this symbol does not require a PLT entry, and it is not
2702 defined by a dynamic object, or is not referenced by a regular
2703 object, ignore it. We do have to handle a weak defined symbol,
2704 even if no regular object refers to it, if we decided to add it
2705 to the dynamic symbol table. FIXME: Do we normally need to worry
2706 about symbols which are defined by one dynamic object and
2707 referenced by another one? */
2708 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
2709 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
2710 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2711 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
2712 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
2715 /* If we've already adjusted this symbol, don't do it again. This
2716 can happen via a recursive call. */
2717 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
2720 /* Don't look at this symbol again. Note that we must set this
2721 after checking the above conditions, because we may look at a
2722 symbol once, decide not to do anything, and then get called
2723 recursively later after REF_REGULAR is set below. */
2724 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
2726 /* If this is a weak definition, and we know a real definition, and
2727 the real symbol is not itself defined by a regular object file,
2728 then get a good value for the real definition. We handle the
2729 real symbol first, for the convenience of the backend routine.
2731 Note that there is a confusing case here. If the real definition
2732 is defined by a regular object file, we don't get the real symbol
2733 from the dynamic object, but we do get the weak symbol. If the
2734 processor backend uses a COPY reloc, then if some routine in the
2735 dynamic object changes the real symbol, we will not see that
2736 change in the corresponding weak symbol. This is the way other
2737 ELF linkers work as well, and seems to be a result of the shared
2740 I will clarify this issue. Most SVR4 shared libraries define the
2741 variable _timezone and define timezone as a weak synonym. The
2742 tzset call changes _timezone. If you write
2743 extern int timezone;
2745 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2746 you might expect that, since timezone is a synonym for _timezone,
2747 the same number will print both times. However, if the processor
2748 backend uses a COPY reloc, then actually timezone will be copied
2749 into your process image, and, since you define _timezone
2750 yourself, _timezone will not. Thus timezone and _timezone will
2751 wind up at different memory locations. The tzset call will set
2752 _timezone, leaving timezone unchanged. */
2754 if (h
->weakdef
!= NULL
)
2756 struct elf_link_hash_entry
*weakdef
;
2758 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2759 || h
->root
.type
== bfd_link_hash_defweak
);
2760 weakdef
= h
->weakdef
;
2761 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2762 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2763 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
2764 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2766 /* This symbol is defined by a regular object file, so we
2767 will not do anything special. Clear weakdef for the
2768 convenience of the processor backend. */
2773 /* There is an implicit reference by a regular object file
2774 via the weak symbol. */
2775 weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2776 if (! elf_adjust_dynamic_symbol (weakdef
, (PTR
) eif
))
2781 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2782 bed
= get_elf_backend_data (dynobj
);
2783 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2792 /* This routine is used to export all defined symbols into the dynamic
2793 symbol table. It is called via elf_link_hash_traverse. */
2796 elf_export_symbol (h
, data
)
2797 struct elf_link_hash_entry
*h
;
2800 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2802 /* Ignore indirect symbols. These are added by the versioning code. */
2803 if (h
->root
.type
== bfd_link_hash_indirect
)
2806 if (h
->dynindx
== -1
2807 && (h
->elf_link_hash_flags
2808 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
2810 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2820 /* Look through the symbols which are defined in other shared
2821 libraries and referenced here. Update the list of version
2822 dependencies. This will be put into the .gnu.version_r section.
2823 This function is called via elf_link_hash_traverse. */
2826 elf_link_find_version_dependencies (h
, data
)
2827 struct elf_link_hash_entry
*h
;
2830 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2831 Elf_Internal_Verneed
*t
;
2832 Elf_Internal_Vernaux
*a
;
2834 /* We only care about symbols defined in shared objects with version
2836 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2837 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
2839 || h
->verinfo
.verdef
== NULL
)
2842 /* See if we already know about this version. */
2843 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
2845 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2848 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2849 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2855 /* This is a new version. Add it to tree we are building. */
2859 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *t
);
2862 rinfo
->failed
= true;
2866 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2867 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
2868 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
2871 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *a
);
2873 /* Note that we are copying a string pointer here, and testing it
2874 above. If bfd_elf_string_from_elf_section is ever changed to
2875 discard the string data when low in memory, this will have to be
2877 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2879 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2880 a
->vna_nextptr
= t
->vn_auxptr
;
2882 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2885 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2892 /* Figure out appropriate versions for all the symbols. We may not
2893 have the version number script until we have read all of the input
2894 files, so until that point we don't know which symbols should be
2895 local. This function is called via elf_link_hash_traverse. */
2898 elf_link_assign_sym_version (h
, data
)
2899 struct elf_link_hash_entry
*h
;
2902 struct elf_assign_sym_version_info
*sinfo
=
2903 (struct elf_assign_sym_version_info
*) data
;
2904 struct bfd_link_info
*info
= sinfo
->info
;
2905 struct elf_info_failed eif
;
2908 /* Fix the symbol flags. */
2911 if (! elf_fix_symbol_flags (h
, &eif
))
2914 sinfo
->failed
= true;
2918 /* We only need version numbers for symbols defined in regular
2920 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2923 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2924 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2926 struct bfd_elf_version_tree
*t
;
2931 /* There are two consecutive ELF_VER_CHR characters if this is
2932 not a hidden symbol. */
2934 if (*p
== ELF_VER_CHR
)
2940 /* If there is no version string, we can just return out. */
2944 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
2948 /* Look for the version. If we find it, it is no longer weak. */
2949 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
2951 if (strcmp (t
->name
, p
) == 0)
2953 h
->verinfo
.vertree
= t
;
2956 /* See if there is anything to force this symbol to
2958 if (t
->locals
!= NULL
)
2962 struct bfd_elf_version_expr
*d
;
2964 len
= p
- h
->root
.root
.string
;
2965 alc
= bfd_alloc (sinfo
->output_bfd
, len
);
2968 strncpy (alc
, h
->root
.root
.string
, len
- 1);
2969 alc
[len
- 1] = '\0';
2970 if (alc
[len
- 2] == ELF_VER_CHR
)
2971 alc
[len
- 2] = '\0';
2973 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
2975 if ((d
->match
[0] == '*' && d
->match
[1] == '\0')
2976 || fnmatch (d
->match
, alc
, 0) == 0)
2978 if (h
->dynindx
!= -1
2980 && ! sinfo
->export_dynamic
)
2982 sinfo
->removed_dynamic
= true;
2983 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
2984 h
->elf_link_hash_flags
&=~
2985 ELF_LINK_HASH_NEEDS_PLT
;
2987 /* FIXME: The name of the symbol has
2988 already been recorded in the dynamic
2989 string table section. */
2996 bfd_release (sinfo
->output_bfd
, alc
);
3003 /* If we are building an application, we need to create a
3004 version node for this version. */
3005 if (t
== NULL
&& ! info
->shared
)
3007 struct bfd_elf_version_tree
**pp
;
3010 /* If we aren't going to export this symbol, we don't need
3011 to worry about it. */
3012 if (h
->dynindx
== -1)
3015 t
= ((struct bfd_elf_version_tree
*)
3016 bfd_alloc (sinfo
->output_bfd
, sizeof *t
));
3019 sinfo
->failed
= true;
3028 t
->name_indx
= (unsigned int) -1;
3032 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
3034 t
->vernum
= version_index
;
3038 h
->verinfo
.vertree
= t
;
3042 /* We could not find the version for a symbol when
3043 generating a shared archive. Return an error. */
3044 (*_bfd_error_handler
)
3045 ("%s: undefined version name %s",
3046 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
3047 bfd_set_error (bfd_error_bad_value
);
3048 sinfo
->failed
= true;
3053 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
3056 /* If we don't have a version for this symbol, see if we can find
3058 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
3060 struct bfd_elf_version_tree
*t
;
3061 struct bfd_elf_version_tree
*deflt
;
3062 struct bfd_elf_version_expr
*d
;
3064 /* See if can find what version this symbol is in. If the
3065 symbol is supposed to be local, then don't actually register
3068 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
3070 if (t
->globals
!= NULL
)
3072 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3074 if (fnmatch (d
->match
, h
->root
.root
.string
, 0) == 0)
3076 h
->verinfo
.vertree
= t
;
3085 if (t
->locals
!= NULL
)
3087 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3089 if (d
->match
[0] == '*' && d
->match
[1] == '\0')
3091 else if (fnmatch (d
->match
, h
->root
.root
.string
, 0) == 0)
3093 h
->verinfo
.vertree
= t
;
3094 if (h
->dynindx
!= -1
3096 && ! sinfo
->export_dynamic
)
3098 sinfo
->removed_dynamic
= true;
3099 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3100 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
3102 /* FIXME: The name of the symbol has already
3103 been recorded in the dynamic string table
3115 if (deflt
!= NULL
&& h
->verinfo
.vertree
== NULL
)
3117 h
->verinfo
.vertree
= deflt
;
3118 if (h
->dynindx
!= -1
3120 && ! sinfo
->export_dynamic
)
3122 sinfo
->removed_dynamic
= true;
3123 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3124 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
3126 /* FIXME: The name of the symbol has already been
3127 recorded in the dynamic string table section. */
3135 /* This function is used to renumber the dynamic symbols, if some of
3136 them are removed because they are marked as local. This is called
3137 via elf_link_hash_traverse. */
3140 elf_link_renumber_dynsyms (h
, data
)
3141 struct elf_link_hash_entry
*h
;
3144 struct bfd_link_info
*info
= (struct bfd_link_info
*) data
;
3146 if (h
->dynindx
!= -1)
3148 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
3149 ++elf_hash_table (info
)->dynsymcount
;
3155 /* Final phase of ELF linker. */
3157 /* A structure we use to avoid passing large numbers of arguments. */
3159 struct elf_final_link_info
3161 /* General link information. */
3162 struct bfd_link_info
*info
;
3165 /* Symbol string table. */
3166 struct bfd_strtab_hash
*symstrtab
;
3167 /* .dynsym section. */
3168 asection
*dynsym_sec
;
3169 /* .hash section. */
3171 /* symbol version section (.gnu.version). */
3172 asection
*symver_sec
;
3173 /* Buffer large enough to hold contents of any section. */
3175 /* Buffer large enough to hold external relocs of any section. */
3176 PTR external_relocs
;
3177 /* Buffer large enough to hold internal relocs of any section. */
3178 Elf_Internal_Rela
*internal_relocs
;
3179 /* Buffer large enough to hold external local symbols of any input
3181 Elf_External_Sym
*external_syms
;
3182 /* Buffer large enough to hold internal local symbols of any input
3184 Elf_Internal_Sym
*internal_syms
;
3185 /* Array large enough to hold a symbol index for each local symbol
3186 of any input BFD. */
3188 /* Array large enough to hold a section pointer for each local
3189 symbol of any input BFD. */
3190 asection
**sections
;
3191 /* Buffer to hold swapped out symbols. */
3192 Elf_External_Sym
*symbuf
;
3193 /* Number of swapped out symbols in buffer. */
3194 size_t symbuf_count
;
3195 /* Number of symbols which fit in symbuf. */
3199 static boolean elf_link_output_sym
3200 PARAMS ((struct elf_final_link_info
*, const char *,
3201 Elf_Internal_Sym
*, asection
*));
3202 static boolean elf_link_flush_output_syms
3203 PARAMS ((struct elf_final_link_info
*));
3204 static boolean elf_link_output_extsym
3205 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3206 static boolean elf_link_input_bfd
3207 PARAMS ((struct elf_final_link_info
*, bfd
*));
3208 static boolean elf_reloc_link_order
3209 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
3210 struct bfd_link_order
*));
3212 /* This struct is used to pass information to elf_link_output_extsym. */
3214 struct elf_outext_info
3218 struct elf_final_link_info
*finfo
;
3221 /* Do the final step of an ELF link. */
3224 elf_bfd_final_link (abfd
, info
)
3226 struct bfd_link_info
*info
;
3230 struct elf_final_link_info finfo
;
3231 register asection
*o
;
3232 register struct bfd_link_order
*p
;
3234 size_t max_contents_size
;
3235 size_t max_external_reloc_size
;
3236 size_t max_internal_reloc_count
;
3237 size_t max_sym_count
;
3239 Elf_Internal_Sym elfsym
;
3241 Elf_Internal_Shdr
*symtab_hdr
;
3242 Elf_Internal_Shdr
*symstrtab_hdr
;
3243 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3244 struct elf_outext_info eoinfo
;
3247 abfd
->flags
|= DYNAMIC
;
3249 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
3250 dynobj
= elf_hash_table (info
)->dynobj
;
3253 finfo
.output_bfd
= abfd
;
3254 finfo
.symstrtab
= elf_stringtab_init ();
3255 if (finfo
.symstrtab
== NULL
)
3260 finfo
.dynsym_sec
= NULL
;
3261 finfo
.hash_sec
= NULL
;
3262 finfo
.symver_sec
= NULL
;
3266 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
3267 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
3268 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
3269 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3270 /* Note that it is OK if symver_sec is NULL. */
3273 finfo
.contents
= NULL
;
3274 finfo
.external_relocs
= NULL
;
3275 finfo
.internal_relocs
= NULL
;
3276 finfo
.external_syms
= NULL
;
3277 finfo
.internal_syms
= NULL
;
3278 finfo
.indices
= NULL
;
3279 finfo
.sections
= NULL
;
3280 finfo
.symbuf
= NULL
;
3281 finfo
.symbuf_count
= 0;
3283 /* Count up the number of relocations we will output for each output
3284 section, so that we know the sizes of the reloc sections. We
3285 also figure out some maximum sizes. */
3286 max_contents_size
= 0;
3287 max_external_reloc_size
= 0;
3288 max_internal_reloc_count
= 0;
3290 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3294 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3296 if (p
->type
== bfd_section_reloc_link_order
3297 || p
->type
== bfd_symbol_reloc_link_order
)
3299 else if (p
->type
== bfd_indirect_link_order
)
3303 sec
= p
->u
.indirect
.section
;
3305 /* Mark all sections which are to be included in the
3306 link. This will normally be every section. We need
3307 to do this so that we can identify any sections which
3308 the linker has decided to not include. */
3309 sec
->linker_mark
= true;
3311 if (info
->relocateable
)
3312 o
->reloc_count
+= sec
->reloc_count
;
3314 if (sec
->_raw_size
> max_contents_size
)
3315 max_contents_size
= sec
->_raw_size
;
3316 if (sec
->_cooked_size
> max_contents_size
)
3317 max_contents_size
= sec
->_cooked_size
;
3319 /* We are interested in just local symbols, not all
3321 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
3322 && (sec
->owner
->flags
& DYNAMIC
) == 0)
3326 if (elf_bad_symtab (sec
->owner
))
3327 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
3328 / sizeof (Elf_External_Sym
));
3330 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
3332 if (sym_count
> max_sym_count
)
3333 max_sym_count
= sym_count
;
3335 if ((sec
->flags
& SEC_RELOC
) != 0)
3339 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
3340 if (ext_size
> max_external_reloc_size
)
3341 max_external_reloc_size
= ext_size
;
3342 if (sec
->reloc_count
> max_internal_reloc_count
)
3343 max_internal_reloc_count
= sec
->reloc_count
;
3349 if (o
->reloc_count
> 0)
3350 o
->flags
|= SEC_RELOC
;
3353 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3354 set it (this is probably a bug) and if it is set
3355 assign_section_numbers will create a reloc section. */
3356 o
->flags
&=~ SEC_RELOC
;
3359 /* If the SEC_ALLOC flag is not set, force the section VMA to
3360 zero. This is done in elf_fake_sections as well, but forcing
3361 the VMA to 0 here will ensure that relocs against these
3362 sections are handled correctly. */
3363 if ((o
->flags
& SEC_ALLOC
) == 0
3364 && ! o
->user_set_vma
)
3368 /* Figure out the file positions for everything but the symbol table
3369 and the relocs. We set symcount to force assign_section_numbers
3370 to create a symbol table. */
3371 abfd
->symcount
= info
->strip
== strip_all
? 0 : 1;
3372 BFD_ASSERT (! abfd
->output_has_begun
);
3373 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
3376 /* That created the reloc sections. Set their sizes, and assign
3377 them file positions, and allocate some buffers. */
3378 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3380 if ((o
->flags
& SEC_RELOC
) != 0)
3382 Elf_Internal_Shdr
*rel_hdr
;
3383 register struct elf_link_hash_entry
**p
, **pend
;
3385 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
3387 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* o
->reloc_count
;
3389 /* The contents field must last into write_object_contents,
3390 so we allocate it with bfd_alloc rather than malloc. */
3391 rel_hdr
->contents
= (PTR
) bfd_alloc (abfd
, rel_hdr
->sh_size
);
3392 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
3395 p
= ((struct elf_link_hash_entry
**)
3396 bfd_malloc (o
->reloc_count
3397 * sizeof (struct elf_link_hash_entry
*)));
3398 if (p
== NULL
&& o
->reloc_count
!= 0)
3400 elf_section_data (o
)->rel_hashes
= p
;
3401 pend
= p
+ o
->reloc_count
;
3402 for (; p
< pend
; p
++)
3405 /* Use the reloc_count field as an index when outputting the
3411 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3413 /* We have now assigned file positions for all the sections except
3414 .symtab and .strtab. We start the .symtab section at the current
3415 file position, and write directly to it. We build the .strtab
3416 section in memory. */
3418 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3419 /* sh_name is set in prep_headers. */
3420 symtab_hdr
->sh_type
= SHT_SYMTAB
;
3421 symtab_hdr
->sh_flags
= 0;
3422 symtab_hdr
->sh_addr
= 0;
3423 symtab_hdr
->sh_size
= 0;
3424 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
3425 /* sh_link is set in assign_section_numbers. */
3426 /* sh_info is set below. */
3427 /* sh_offset is set just below. */
3428 symtab_hdr
->sh_addralign
= 4; /* FIXME: system dependent? */
3430 off
= elf_tdata (abfd
)->next_file_pos
;
3431 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
3433 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3434 incorrect. We do not yet know the size of the .symtab section.
3435 We correct next_file_pos below, after we do know the size. */
3437 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3438 continuously seeking to the right position in the file. */
3439 if (! info
->keep_memory
|| max_sym_count
< 20)
3440 finfo
.symbuf_size
= 20;
3442 finfo
.symbuf_size
= max_sym_count
;
3443 finfo
.symbuf
= ((Elf_External_Sym
*)
3444 bfd_malloc (finfo
.symbuf_size
* sizeof (Elf_External_Sym
)));
3445 if (finfo
.symbuf
== NULL
)
3448 /* Start writing out the symbol table. The first symbol is always a
3450 if (info
->strip
!= strip_all
|| info
->relocateable
)
3452 elfsym
.st_value
= 0;
3455 elfsym
.st_other
= 0;
3456 elfsym
.st_shndx
= SHN_UNDEF
;
3457 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
3458 &elfsym
, bfd_und_section_ptr
))
3463 /* Some standard ELF linkers do this, but we don't because it causes
3464 bootstrap comparison failures. */
3465 /* Output a file symbol for the output file as the second symbol.
3466 We output this even if we are discarding local symbols, although
3467 I'm not sure if this is correct. */
3468 elfsym
.st_value
= 0;
3470 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
3471 elfsym
.st_other
= 0;
3472 elfsym
.st_shndx
= SHN_ABS
;
3473 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
3474 &elfsym
, bfd_abs_section_ptr
))
3478 /* Output a symbol for each section. We output these even if we are
3479 discarding local symbols, since they are used for relocs. These
3480 symbols have no names. We store the index of each one in the
3481 index field of the section, so that we can find it again when
3482 outputting relocs. */
3483 if (info
->strip
!= strip_all
|| info
->relocateable
)
3486 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
3487 elfsym
.st_other
= 0;
3488 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
3490 o
= section_from_elf_index (abfd
, i
);
3492 o
->target_index
= abfd
->symcount
;
3493 elfsym
.st_shndx
= i
;
3494 if (info
->relocateable
|| o
== NULL
)
3495 elfsym
.st_value
= 0;
3497 elfsym
.st_value
= o
->vma
;
3498 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
3504 /* Allocate some memory to hold information read in from the input
3506 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
3507 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
3508 finfo
.internal_relocs
= ((Elf_Internal_Rela
*)
3509 bfd_malloc (max_internal_reloc_count
3510 * sizeof (Elf_Internal_Rela
)));
3511 finfo
.external_syms
= ((Elf_External_Sym
*)
3512 bfd_malloc (max_sym_count
3513 * sizeof (Elf_External_Sym
)));
3514 finfo
.internal_syms
= ((Elf_Internal_Sym
*)
3515 bfd_malloc (max_sym_count
3516 * sizeof (Elf_Internal_Sym
)));
3517 finfo
.indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
3518 finfo
.sections
= ((asection
**)
3519 bfd_malloc (max_sym_count
* sizeof (asection
*)));
3520 if ((finfo
.contents
== NULL
&& max_contents_size
!= 0)
3521 || (finfo
.external_relocs
== NULL
&& max_external_reloc_size
!= 0)
3522 || (finfo
.internal_relocs
== NULL
&& max_internal_reloc_count
!= 0)
3523 || (finfo
.external_syms
== NULL
&& max_sym_count
!= 0)
3524 || (finfo
.internal_syms
== NULL
&& max_sym_count
!= 0)
3525 || (finfo
.indices
== NULL
&& max_sym_count
!= 0)
3526 || (finfo
.sections
== NULL
&& max_sym_count
!= 0))
3529 /* Since ELF permits relocations to be against local symbols, we
3530 must have the local symbols available when we do the relocations.
3531 Since we would rather only read the local symbols once, and we
3532 would rather not keep them in memory, we handle all the
3533 relocations for a single input file at the same time.
3535 Unfortunately, there is no way to know the total number of local
3536 symbols until we have seen all of them, and the local symbol
3537 indices precede the global symbol indices. This means that when
3538 we are generating relocateable output, and we see a reloc against
3539 a global symbol, we can not know the symbol index until we have
3540 finished examining all the local symbols to see which ones we are
3541 going to output. To deal with this, we keep the relocations in
3542 memory, and don't output them until the end of the link. This is
3543 an unfortunate waste of memory, but I don't see a good way around
3544 it. Fortunately, it only happens when performing a relocateable
3545 link, which is not the common case. FIXME: If keep_memory is set
3546 we could write the relocs out and then read them again; I don't
3547 know how bad the memory loss will be. */
3549 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->next
)
3550 sub
->output_has_begun
= false;
3551 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3553 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3555 if (p
->type
== bfd_indirect_link_order
3556 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
3557 == bfd_target_elf_flavour
))
3559 sub
= p
->u
.indirect
.section
->owner
;
3560 if (! sub
->output_has_begun
)
3562 if (! elf_link_input_bfd (&finfo
, sub
))
3564 sub
->output_has_begun
= true;
3567 else if (p
->type
== bfd_section_reloc_link_order
3568 || p
->type
== bfd_symbol_reloc_link_order
)
3570 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
3575 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
3581 /* That wrote out all the local symbols. Finish up the symbol table
3582 with the global symbols. */
3584 if (info
->strip
!= strip_all
&& info
->shared
)
3586 /* Output any global symbols that got converted to local in a
3587 version script. We do this in a separate step since ELF
3588 requires all local symbols to appear prior to any global
3589 symbols. FIXME: We should only do this if some global
3590 symbols were, in fact, converted to become local. FIXME:
3591 Will this work correctly with the Irix 5 linker? */
3592 eoinfo
.failed
= false;
3593 eoinfo
.finfo
= &finfo
;
3594 eoinfo
.localsyms
= true;
3595 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3601 /* The sh_info field records the index of the first non local
3603 symtab_hdr
->sh_info
= abfd
->symcount
;
3605 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
= 1;
3607 /* We get the global symbols from the hash table. */
3608 eoinfo
.failed
= false;
3609 eoinfo
.localsyms
= false;
3610 eoinfo
.finfo
= &finfo
;
3611 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3616 /* Flush all symbols to the file. */
3617 if (! elf_link_flush_output_syms (&finfo
))
3620 /* Now we know the size of the symtab section. */
3621 off
+= symtab_hdr
->sh_size
;
3623 /* Finish up and write out the symbol string table (.strtab)
3625 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3626 /* sh_name was set in prep_headers. */
3627 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
3628 symstrtab_hdr
->sh_flags
= 0;
3629 symstrtab_hdr
->sh_addr
= 0;
3630 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
3631 symstrtab_hdr
->sh_entsize
= 0;
3632 symstrtab_hdr
->sh_link
= 0;
3633 symstrtab_hdr
->sh_info
= 0;
3634 /* sh_offset is set just below. */
3635 symstrtab_hdr
->sh_addralign
= 1;
3637 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, true);
3638 elf_tdata (abfd
)->next_file_pos
= off
;
3640 if (abfd
->symcount
> 0)
3642 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
3643 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
3647 /* Adjust the relocs to have the correct symbol indices. */
3648 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3650 struct elf_link_hash_entry
**rel_hash
;
3651 Elf_Internal_Shdr
*rel_hdr
;
3653 if ((o
->flags
& SEC_RELOC
) == 0)
3656 rel_hash
= elf_section_data (o
)->rel_hashes
;
3657 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
3658 for (i
= 0; i
< o
->reloc_count
; i
++, rel_hash
++)
3660 if (*rel_hash
== NULL
)
3663 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
3665 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
3667 Elf_External_Rel
*erel
;
3668 Elf_Internal_Rel irel
;
3670 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
3671 elf_swap_reloc_in (abfd
, erel
, &irel
);
3672 irel
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
3673 ELF_R_TYPE (irel
.r_info
));
3674 elf_swap_reloc_out (abfd
, &irel
, erel
);
3678 Elf_External_Rela
*erela
;
3679 Elf_Internal_Rela irela
;
3681 BFD_ASSERT (rel_hdr
->sh_entsize
3682 == sizeof (Elf_External_Rela
));
3684 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
3685 elf_swap_reloca_in (abfd
, erela
, &irela
);
3686 irela
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
3687 ELF_R_TYPE (irela
.r_info
));
3688 elf_swap_reloca_out (abfd
, &irela
, erela
);
3692 /* Set the reloc_count field to 0 to prevent write_relocs from
3693 trying to swap the relocs out itself. */
3697 /* If we are linking against a dynamic object, or generating a
3698 shared library, finish up the dynamic linking information. */
3701 Elf_External_Dyn
*dyncon
, *dynconend
;
3703 /* Fix up .dynamic entries. */
3704 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
3705 BFD_ASSERT (o
!= NULL
);
3707 dyncon
= (Elf_External_Dyn
*) o
->contents
;
3708 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
3709 for (; dyncon
< dynconend
; dyncon
++)
3711 Elf_Internal_Dyn dyn
;
3715 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
3722 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on
3723 magic _init and _fini symbols. This is pretty ugly,
3724 but we are compatible. */
3732 struct elf_link_hash_entry
*h
;
3734 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
3735 false, false, true);
3737 && (h
->root
.type
== bfd_link_hash_defined
3738 || h
->root
.type
== bfd_link_hash_defweak
))
3740 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
3741 o
= h
->root
.u
.def
.section
;
3742 if (o
->output_section
!= NULL
)
3743 dyn
.d_un
.d_val
+= (o
->output_section
->vma
3744 + o
->output_offset
);
3747 /* The symbol is imported from another shared
3748 library and does not apply to this one. */
3752 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3767 name
= ".gnu.version_d";
3770 name
= ".gnu.version_r";
3773 name
= ".gnu.version";
3775 o
= bfd_get_section_by_name (abfd
, name
);
3776 BFD_ASSERT (o
!= NULL
);
3777 dyn
.d_un
.d_ptr
= o
->vma
;
3778 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3785 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
3790 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
3792 Elf_Internal_Shdr
*hdr
;
3794 hdr
= elf_elfsections (abfd
)[i
];
3795 if (hdr
->sh_type
== type
3796 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
3798 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
3799 dyn
.d_un
.d_val
+= hdr
->sh_size
;
3802 if (dyn
.d_un
.d_val
== 0
3803 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
3804 dyn
.d_un
.d_val
= hdr
->sh_addr
;
3808 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3814 /* If we have created any dynamic sections, then output them. */
3817 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
3820 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
3822 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
3823 || o
->_raw_size
== 0)
3825 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
3827 /* At this point, we are only interested in sections
3828 created by elf_link_create_dynamic_sections. */
3831 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
3833 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
3835 if (! bfd_set_section_contents (abfd
, o
->output_section
,
3836 o
->contents
, o
->output_offset
,
3844 /* The contents of the .dynstr section are actually in a
3846 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
3847 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
3848 || ! _bfd_stringtab_emit (abfd
,
3849 elf_hash_table (info
)->dynstr
))
3855 /* If we have optimized stabs strings, output them. */
3856 if (elf_hash_table (info
)->stab_info
!= NULL
)
3858 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
3862 if (finfo
.symstrtab
!= NULL
)
3863 _bfd_stringtab_free (finfo
.symstrtab
);
3864 if (finfo
.contents
!= NULL
)
3865 free (finfo
.contents
);
3866 if (finfo
.external_relocs
!= NULL
)
3867 free (finfo
.external_relocs
);
3868 if (finfo
.internal_relocs
!= NULL
)
3869 free (finfo
.internal_relocs
);
3870 if (finfo
.external_syms
!= NULL
)
3871 free (finfo
.external_syms
);
3872 if (finfo
.internal_syms
!= NULL
)
3873 free (finfo
.internal_syms
);
3874 if (finfo
.indices
!= NULL
)
3875 free (finfo
.indices
);
3876 if (finfo
.sections
!= NULL
)
3877 free (finfo
.sections
);
3878 if (finfo
.symbuf
!= NULL
)
3879 free (finfo
.symbuf
);
3880 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3882 if ((o
->flags
& SEC_RELOC
) != 0
3883 && elf_section_data (o
)->rel_hashes
!= NULL
)
3884 free (elf_section_data (o
)->rel_hashes
);
3887 elf_tdata (abfd
)->linker
= true;
3892 if (finfo
.symstrtab
!= NULL
)
3893 _bfd_stringtab_free (finfo
.symstrtab
);
3894 if (finfo
.contents
!= NULL
)
3895 free (finfo
.contents
);
3896 if (finfo
.external_relocs
!= NULL
)
3897 free (finfo
.external_relocs
);
3898 if (finfo
.internal_relocs
!= NULL
)
3899 free (finfo
.internal_relocs
);
3900 if (finfo
.external_syms
!= NULL
)
3901 free (finfo
.external_syms
);
3902 if (finfo
.internal_syms
!= NULL
)
3903 free (finfo
.internal_syms
);
3904 if (finfo
.indices
!= NULL
)
3905 free (finfo
.indices
);
3906 if (finfo
.sections
!= NULL
)
3907 free (finfo
.sections
);
3908 if (finfo
.symbuf
!= NULL
)
3909 free (finfo
.symbuf
);
3910 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3912 if ((o
->flags
& SEC_RELOC
) != 0
3913 && elf_section_data (o
)->rel_hashes
!= NULL
)
3914 free (elf_section_data (o
)->rel_hashes
);
3920 /* Add a symbol to the output symbol table. */
3923 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
3924 struct elf_final_link_info
*finfo
;
3926 Elf_Internal_Sym
*elfsym
;
3927 asection
*input_sec
;
3929 boolean (*output_symbol_hook
) PARAMS ((bfd
*,
3930 struct bfd_link_info
*info
,
3935 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
3936 elf_backend_link_output_symbol_hook
;
3937 if (output_symbol_hook
!= NULL
)
3939 if (! ((*output_symbol_hook
)
3940 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
3944 if (name
== (const char *) NULL
|| *name
== '\0')
3945 elfsym
->st_name
= 0;
3948 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
3951 if (elfsym
->st_name
== (unsigned long) -1)
3955 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
3957 if (! elf_link_flush_output_syms (finfo
))
3961 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
,
3962 (PTR
) (finfo
->symbuf
+ finfo
->symbuf_count
));
3963 ++finfo
->symbuf_count
;
3965 ++finfo
->output_bfd
->symcount
;
3970 /* Flush the output symbols to the file. */
3973 elf_link_flush_output_syms (finfo
)
3974 struct elf_final_link_info
*finfo
;
3976 if (finfo
->symbuf_count
> 0)
3978 Elf_Internal_Shdr
*symtab
;
3980 symtab
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
3982 if (bfd_seek (finfo
->output_bfd
, symtab
->sh_offset
+ symtab
->sh_size
,
3984 || (bfd_write ((PTR
) finfo
->symbuf
, finfo
->symbuf_count
,
3985 sizeof (Elf_External_Sym
), finfo
->output_bfd
)
3986 != finfo
->symbuf_count
* sizeof (Elf_External_Sym
)))
3989 symtab
->sh_size
+= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
3991 finfo
->symbuf_count
= 0;
3997 /* Add an external symbol to the symbol table. This is called from
3998 the hash table traversal routine. When generating a shared object,
3999 we go through the symbol table twice. The first time we output
4000 anything that might have been forced to local scope in a version
4001 script. The second time we output the symbols that are still
4005 elf_link_output_extsym (h
, data
)
4006 struct elf_link_hash_entry
*h
;
4009 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
4010 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
4012 Elf_Internal_Sym sym
;
4013 asection
*input_sec
;
4015 /* Decide whether to output this symbol in this pass. */
4016 if (eoinfo
->localsyms
)
4018 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
4023 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4027 /* If we are not creating a shared library, and this symbol is
4028 referenced by a shared library but is not defined anywhere, then
4029 warn that it is undefined. If we do not do this, the runtime
4030 linker will complain that the symbol is undefined when the
4031 program is run. We don't have to worry about symbols that are
4032 referenced by regular files, because we will already have issued
4033 warnings for them. */
4034 if (! finfo
->info
->relocateable
4035 && ! finfo
->info
->shared
4036 && h
->root
.type
== bfd_link_hash_undefined
4037 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
4038 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
4040 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
4041 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
4042 (asection
*) NULL
, 0)))
4044 eoinfo
->failed
= true;
4049 /* We don't want to output symbols that have never been mentioned by
4050 a regular file, or that we have been told to strip. However, if
4051 h->indx is set to -2, the symbol is used by a reloc and we must
4055 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
4056 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
4057 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
4058 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
4060 else if (finfo
->info
->strip
== strip_all
4061 || (finfo
->info
->strip
== strip_some
4062 && bfd_hash_lookup (finfo
->info
->keep_hash
,
4063 h
->root
.root
.string
,
4064 false, false) == NULL
))
4069 /* If we're stripping it, and it's not a dynamic symbol, there's
4070 nothing else to do. */
4071 if (strip
&& h
->dynindx
== -1)
4075 sym
.st_size
= h
->size
;
4076 sym
.st_other
= h
->other
;
4077 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4078 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
4079 else if (h
->root
.type
== bfd_link_hash_undefweak
4080 || h
->root
.type
== bfd_link_hash_defweak
)
4081 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
4083 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
4085 switch (h
->root
.type
)
4088 case bfd_link_hash_new
:
4092 case bfd_link_hash_undefined
:
4093 input_sec
= bfd_und_section_ptr
;
4094 sym
.st_shndx
= SHN_UNDEF
;
4097 case bfd_link_hash_undefweak
:
4098 input_sec
= bfd_und_section_ptr
;
4099 sym
.st_shndx
= SHN_UNDEF
;
4102 case bfd_link_hash_defined
:
4103 case bfd_link_hash_defweak
:
4105 input_sec
= h
->root
.u
.def
.section
;
4106 if (input_sec
->output_section
!= NULL
)
4109 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
4110 input_sec
->output_section
);
4111 if (sym
.st_shndx
== (unsigned short) -1)
4113 eoinfo
->failed
= true;
4117 /* ELF symbols in relocateable files are section relative,
4118 but in nonrelocateable files they are virtual
4120 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
4121 if (! finfo
->info
->relocateable
)
4122 sym
.st_value
+= input_sec
->output_section
->vma
;
4126 BFD_ASSERT (input_sec
->owner
== NULL
4127 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
4128 sym
.st_shndx
= SHN_UNDEF
;
4129 input_sec
= bfd_und_section_ptr
;
4134 case bfd_link_hash_common
:
4135 input_sec
= bfd_com_section_ptr
;
4136 sym
.st_shndx
= SHN_COMMON
;
4137 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
4140 case bfd_link_hash_indirect
:
4141 /* These symbols are created by symbol versioning. They point
4142 to the decorated version of the name. For example, if the
4143 symbol foo@@GNU_1.2 is the default, which should be used when
4144 foo is used with no version, then we add an indirect symbol
4145 foo which points to foo@@GNU_1.2. We ignore these symbols,
4146 since the indirected symbol is already in the hash table. If
4147 the indirect symbol is non-ELF, fall through and output it. */
4148 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) == 0)
4152 case bfd_link_hash_warning
:
4153 /* We can't represent these symbols in ELF, although a warning
4154 symbol may have come from a .gnu.warning.SYMBOL section. We
4155 just put the target symbol in the hash table. If the target
4156 symbol does not really exist, don't do anything. */
4157 if (h
->root
.u
.i
.link
->type
== bfd_link_hash_new
)
4159 return (elf_link_output_extsym
4160 ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
));
4163 /* Give the processor backend a chance to tweak the symbol value,
4164 and also to finish up anything that needs to be done for this
4166 if ((h
->dynindx
!= -1
4167 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4168 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4170 struct elf_backend_data
*bed
;
4172 bed
= get_elf_backend_data (finfo
->output_bfd
);
4173 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
4174 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
4176 eoinfo
->failed
= true;
4181 /* If this symbol should be put in the .dynsym section, then put it
4182 there now. We have already know the symbol index. We also fill
4183 in the entry in the .hash section. */
4184 if (h
->dynindx
!= -1
4185 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4191 bfd_byte
*bucketpos
;
4194 sym
.st_name
= h
->dynstr_index
;
4196 elf_swap_symbol_out (finfo
->output_bfd
, &sym
,
4197 (PTR
) (((Elf_External_Sym
*)
4198 finfo
->dynsym_sec
->contents
)
4201 /* We didn't include the version string in the dynamic string
4202 table, so we must not consider it in the hash table. */
4203 name
= h
->root
.root
.string
;
4204 p
= strchr (name
, ELF_VER_CHR
);
4209 copy
= bfd_alloc (finfo
->output_bfd
, p
- name
+ 1);
4210 strncpy (copy
, name
, p
- name
);
4211 copy
[p
- name
] = '\0';
4215 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
4216 bucket
= bfd_elf_hash ((const unsigned char *) name
) % bucketcount
;
4217 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
4218 + (bucket
+ 2) * (ARCH_SIZE
/ 8));
4219 chain
= get_word (finfo
->output_bfd
, bucketpos
);
4220 put_word (finfo
->output_bfd
, h
->dynindx
, bucketpos
);
4221 put_word (finfo
->output_bfd
, chain
,
4222 ((bfd_byte
*) finfo
->hash_sec
->contents
4223 + (bucketcount
+ 2 + h
->dynindx
) * (ARCH_SIZE
/ 8)));
4226 bfd_release (finfo
->output_bfd
, copy
);
4228 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
4230 Elf_Internal_Versym iversym
;
4232 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4234 if (h
->verinfo
.verdef
== NULL
)
4235 iversym
.vs_vers
= 0;
4237 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4241 if (h
->verinfo
.vertree
== NULL
)
4242 iversym
.vs_vers
= 1;
4244 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
4247 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
4248 iversym
.vs_vers
|= VERSYM_HIDDEN
;
4250 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
,
4251 (((Elf_External_Versym
*)
4252 finfo
->symver_sec
->contents
)
4257 /* If we're stripping it, then it was just a dynamic symbol, and
4258 there's nothing else to do. */
4262 h
->indx
= finfo
->output_bfd
->symcount
;
4264 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
4266 eoinfo
->failed
= true;
4273 /* Link an input file into the linker output file. This function
4274 handles all the sections and relocations of the input file at once.
4275 This is so that we only have to read the local symbols once, and
4276 don't have to keep them in memory. */
4279 elf_link_input_bfd (finfo
, input_bfd
)
4280 struct elf_final_link_info
*finfo
;
4283 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
4284 bfd
*, asection
*, bfd_byte
*,
4285 Elf_Internal_Rela
*,
4286 Elf_Internal_Sym
*, asection
**));
4288 Elf_Internal_Shdr
*symtab_hdr
;
4291 Elf_External_Sym
*external_syms
;
4292 Elf_External_Sym
*esym
;
4293 Elf_External_Sym
*esymend
;
4294 Elf_Internal_Sym
*isym
;
4296 asection
**ppsection
;
4299 output_bfd
= finfo
->output_bfd
;
4301 get_elf_backend_data (output_bfd
)->elf_backend_relocate_section
;
4303 /* If this is a dynamic object, we don't want to do anything here:
4304 we don't want the local symbols, and we don't want the section
4306 if ((input_bfd
->flags
& DYNAMIC
) != 0)
4309 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
4310 if (elf_bad_symtab (input_bfd
))
4312 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
4317 locsymcount
= symtab_hdr
->sh_info
;
4318 extsymoff
= symtab_hdr
->sh_info
;
4321 /* Read the local symbols. */
4322 if (symtab_hdr
->contents
!= NULL
)
4323 external_syms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
4324 else if (locsymcount
== 0)
4325 external_syms
= NULL
;
4328 external_syms
= finfo
->external_syms
;
4329 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
4330 || (bfd_read (external_syms
, sizeof (Elf_External_Sym
),
4331 locsymcount
, input_bfd
)
4332 != locsymcount
* sizeof (Elf_External_Sym
)))
4336 /* Swap in the local symbols and write out the ones which we know
4337 are going into the output file. */
4338 esym
= external_syms
;
4339 esymend
= esym
+ locsymcount
;
4340 isym
= finfo
->internal_syms
;
4341 pindex
= finfo
->indices
;
4342 ppsection
= finfo
->sections
;
4343 for (; esym
< esymend
; esym
++, isym
++, pindex
++, ppsection
++)
4347 Elf_Internal_Sym osym
;
4349 elf_swap_symbol_in (input_bfd
, esym
, isym
);
4352 if (elf_bad_symtab (input_bfd
))
4354 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
4361 if (isym
->st_shndx
== SHN_UNDEF
)
4362 isec
= bfd_und_section_ptr
;
4363 else if (isym
->st_shndx
> 0 && isym
->st_shndx
< SHN_LORESERVE
)
4364 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
4365 else if (isym
->st_shndx
== SHN_ABS
)
4366 isec
= bfd_abs_section_ptr
;
4367 else if (isym
->st_shndx
== SHN_COMMON
)
4368 isec
= bfd_com_section_ptr
;
4377 /* Don't output the first, undefined, symbol. */
4378 if (esym
== external_syms
)
4381 /* If we are stripping all symbols, we don't want to output this
4383 if (finfo
->info
->strip
== strip_all
)
4386 /* We never output section symbols. Instead, we use the section
4387 symbol of the corresponding section in the output file. */
4388 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
4391 /* If we are discarding all local symbols, we don't want to
4392 output this one. If we are generating a relocateable output
4393 file, then some of the local symbols may be required by
4394 relocs; we output them below as we discover that they are
4396 if (finfo
->info
->discard
== discard_all
)
4399 /* If this symbol is defined in a section which we are
4400 discarding, we don't need to keep it, but note that
4401 linker_mark is only reliable for sections that have contents.
4402 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4403 as well as linker_mark. */
4404 if (isym
->st_shndx
> 0
4405 && isym
->st_shndx
< SHN_LORESERVE
4407 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
4408 || (! finfo
->info
->relocateable
4409 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
4412 /* Get the name of the symbol. */
4413 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
4418 /* See if we are discarding symbols with this name. */
4419 if ((finfo
->info
->strip
== strip_some
4420 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
4422 || (finfo
->info
->discard
== discard_l
4423 && bfd_is_local_label_name (input_bfd
, name
)))
4426 /* If we get here, we are going to output this symbol. */
4430 /* Adjust the section index for the output file. */
4431 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
4432 isec
->output_section
);
4433 if (osym
.st_shndx
== (unsigned short) -1)
4436 *pindex
= output_bfd
->symcount
;
4438 /* ELF symbols in relocateable files are section relative, but
4439 in executable files they are virtual addresses. Note that
4440 this code assumes that all ELF sections have an associated
4441 BFD section with a reasonable value for output_offset; below
4442 we assume that they also have a reasonable value for
4443 output_section. Any special sections must be set up to meet
4444 these requirements. */
4445 osym
.st_value
+= isec
->output_offset
;
4446 if (! finfo
->info
->relocateable
)
4447 osym
.st_value
+= isec
->output_section
->vma
;
4449 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
4453 /* Relocate the contents of each section. */
4454 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4458 if (! o
->linker_mark
)
4460 /* This section was omitted from the link. */
4464 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4465 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
4468 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
4470 /* Section was created by elf_link_create_dynamic_sections
4475 /* Get the contents of the section. They have been cached by a
4476 relaxation routine. Note that o is a section in an input
4477 file, so the contents field will not have been set by any of
4478 the routines which work on output files. */
4479 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
4480 contents
= elf_section_data (o
)->this_hdr
.contents
;
4483 contents
= finfo
->contents
;
4484 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
4485 (file_ptr
) 0, o
->_raw_size
))
4489 if ((o
->flags
& SEC_RELOC
) != 0)
4491 Elf_Internal_Rela
*internal_relocs
;
4493 /* Get the swapped relocs. */
4494 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
4495 (input_bfd
, o
, finfo
->external_relocs
,
4496 finfo
->internal_relocs
, false));
4497 if (internal_relocs
== NULL
4498 && o
->reloc_count
> 0)
4501 /* Relocate the section by invoking a back end routine.
4503 The back end routine is responsible for adjusting the
4504 section contents as necessary, and (if using Rela relocs
4505 and generating a relocateable output file) adjusting the
4506 reloc addend as necessary.
4508 The back end routine does not have to worry about setting
4509 the reloc address or the reloc symbol index.
4511 The back end routine is given a pointer to the swapped in
4512 internal symbols, and can access the hash table entries
4513 for the external symbols via elf_sym_hashes (input_bfd).
4515 When generating relocateable output, the back end routine
4516 must handle STB_LOCAL/STT_SECTION symbols specially. The
4517 output symbol is going to be a section symbol
4518 corresponding to the output section, which will require
4519 the addend to be adjusted. */
4521 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
4522 input_bfd
, o
, contents
,
4524 finfo
->internal_syms
,
4528 if (finfo
->info
->relocateable
)
4530 Elf_Internal_Rela
*irela
;
4531 Elf_Internal_Rela
*irelaend
;
4532 struct elf_link_hash_entry
**rel_hash
;
4533 Elf_Internal_Shdr
*input_rel_hdr
;
4534 Elf_Internal_Shdr
*output_rel_hdr
;
4536 /* Adjust the reloc addresses and symbol indices. */
4538 irela
= internal_relocs
;
4539 irelaend
= irela
+ o
->reloc_count
;
4540 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
4541 + o
->output_section
->reloc_count
);
4542 for (; irela
< irelaend
; irela
++, rel_hash
++)
4544 unsigned long r_symndx
;
4545 Elf_Internal_Sym
*isym
;
4548 irela
->r_offset
+= o
->output_offset
;
4550 r_symndx
= ELF_R_SYM (irela
->r_info
);
4555 if (r_symndx
>= locsymcount
4556 || (elf_bad_symtab (input_bfd
)
4557 && finfo
->sections
[r_symndx
] == NULL
))
4561 /* This is a reloc against a global symbol. We
4562 have not yet output all the local symbols, so
4563 we do not know the symbol index of any global
4564 symbol. We set the rel_hash entry for this
4565 reloc to point to the global hash table entry
4566 for this symbol. The symbol index is then
4567 set at the end of elf_bfd_final_link. */
4568 indx
= r_symndx
- extsymoff
;
4569 *rel_hash
= elf_sym_hashes (input_bfd
)[indx
];
4571 /* Setting the index to -2 tells
4572 elf_link_output_extsym that this symbol is
4574 BFD_ASSERT ((*rel_hash
)->indx
< 0);
4575 (*rel_hash
)->indx
= -2;
4580 /* This is a reloc against a local symbol. */
4583 isym
= finfo
->internal_syms
+ r_symndx
;
4584 sec
= finfo
->sections
[r_symndx
];
4585 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
4587 /* I suppose the backend ought to fill in the
4588 section of any STT_SECTION symbol against a
4589 processor specific section. If we have
4590 discarded a section, the output_section will
4591 be the absolute section. */
4593 && (bfd_is_abs_section (sec
)
4594 || (sec
->output_section
!= NULL
4595 && bfd_is_abs_section (sec
->output_section
))))
4597 else if (sec
== NULL
|| sec
->owner
== NULL
)
4599 bfd_set_error (bfd_error_bad_value
);
4604 r_symndx
= sec
->output_section
->target_index
;
4605 BFD_ASSERT (r_symndx
!= 0);
4610 if (finfo
->indices
[r_symndx
] == -1)
4616 if (finfo
->info
->strip
== strip_all
)
4618 /* You can't do ld -r -s. */
4619 bfd_set_error (bfd_error_invalid_operation
);
4623 /* This symbol was skipped earlier, but
4624 since it is needed by a reloc, we
4625 must output it now. */
4626 link
= symtab_hdr
->sh_link
;
4627 name
= bfd_elf_string_from_elf_section (input_bfd
,
4633 osec
= sec
->output_section
;
4635 _bfd_elf_section_from_bfd_section (output_bfd
,
4637 if (isym
->st_shndx
== (unsigned short) -1)
4640 isym
->st_value
+= sec
->output_offset
;
4641 if (! finfo
->info
->relocateable
)
4642 isym
->st_value
+= osec
->vma
;
4644 finfo
->indices
[r_symndx
] = output_bfd
->symcount
;
4646 if (! elf_link_output_sym (finfo
, name
, isym
, sec
))
4650 r_symndx
= finfo
->indices
[r_symndx
];
4653 irela
->r_info
= ELF_R_INFO (r_symndx
,
4654 ELF_R_TYPE (irela
->r_info
));
4657 /* Swap out the relocs. */
4658 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
4659 output_rel_hdr
= &elf_section_data (o
->output_section
)->rel_hdr
;
4660 BFD_ASSERT (output_rel_hdr
->sh_entsize
4661 == input_rel_hdr
->sh_entsize
);
4662 irela
= internal_relocs
;
4663 irelaend
= irela
+ o
->reloc_count
;
4664 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4666 Elf_External_Rel
*erel
;
4668 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
4669 + o
->output_section
->reloc_count
);
4670 for (; irela
< irelaend
; irela
++, erel
++)
4672 Elf_Internal_Rel irel
;
4674 irel
.r_offset
= irela
->r_offset
;
4675 irel
.r_info
= irela
->r_info
;
4676 BFD_ASSERT (irela
->r_addend
== 0);
4677 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
4682 Elf_External_Rela
*erela
;
4684 BFD_ASSERT (input_rel_hdr
->sh_entsize
4685 == sizeof (Elf_External_Rela
));
4686 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
4687 + o
->output_section
->reloc_count
);
4688 for (; irela
< irelaend
; irela
++, erela
++)
4689 elf_swap_reloca_out (output_bfd
, irela
, erela
);
4692 o
->output_section
->reloc_count
+= o
->reloc_count
;
4696 /* Write out the modified section contents. */
4697 if (elf_section_data (o
)->stab_info
== NULL
)
4699 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
4700 contents
, o
->output_offset
,
4701 (o
->_cooked_size
!= 0
4708 if (! (_bfd_write_section_stabs
4709 (output_bfd
, &elf_hash_table (finfo
->info
)->stab_info
,
4710 o
, &elf_section_data (o
)->stab_info
, contents
)))
4718 /* Generate a reloc when linking an ELF file. This is a reloc
4719 requested by the linker, and does come from any input file. This
4720 is used to build constructor and destructor tables when linking
4724 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
4726 struct bfd_link_info
*info
;
4727 asection
*output_section
;
4728 struct bfd_link_order
*link_order
;
4730 reloc_howto_type
*howto
;
4734 struct elf_link_hash_entry
**rel_hash_ptr
;
4735 Elf_Internal_Shdr
*rel_hdr
;
4737 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
4740 bfd_set_error (bfd_error_bad_value
);
4744 addend
= link_order
->u
.reloc
.p
->addend
;
4746 /* Figure out the symbol index. */
4747 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
4748 + output_section
->reloc_count
);
4749 if (link_order
->type
== bfd_section_reloc_link_order
)
4751 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
4752 BFD_ASSERT (indx
!= 0);
4753 *rel_hash_ptr
= NULL
;
4757 struct elf_link_hash_entry
*h
;
4759 /* Treat a reloc against a defined symbol as though it were
4760 actually against the section. */
4761 h
= ((struct elf_link_hash_entry
*)
4762 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
4763 link_order
->u
.reloc
.p
->u
.name
,
4764 false, false, true));
4766 && (h
->root
.type
== bfd_link_hash_defined
4767 || h
->root
.type
== bfd_link_hash_defweak
))
4771 section
= h
->root
.u
.def
.section
;
4772 indx
= section
->output_section
->target_index
;
4773 *rel_hash_ptr
= NULL
;
4774 /* It seems that we ought to add the symbol value to the
4775 addend here, but in practice it has already been added
4776 because it was passed to constructor_callback. */
4777 addend
+= section
->output_section
->vma
+ section
->output_offset
;
4781 /* Setting the index to -2 tells elf_link_output_extsym that
4782 this symbol is used by a reloc. */
4789 if (! ((*info
->callbacks
->unattached_reloc
)
4790 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
4791 (asection
*) NULL
, (bfd_vma
) 0)))
4797 /* If this is an inplace reloc, we must write the addend into the
4799 if (howto
->partial_inplace
&& addend
!= 0)
4802 bfd_reloc_status_type rstat
;
4806 size
= bfd_get_reloc_size (howto
);
4807 buf
= (bfd_byte
*) bfd_zmalloc (size
);
4808 if (buf
== (bfd_byte
*) NULL
)
4810 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
4816 case bfd_reloc_outofrange
:
4818 case bfd_reloc_overflow
:
4819 if (! ((*info
->callbacks
->reloc_overflow
)
4821 (link_order
->type
== bfd_section_reloc_link_order
4822 ? bfd_section_name (output_bfd
,
4823 link_order
->u
.reloc
.p
->u
.section
)
4824 : link_order
->u
.reloc
.p
->u
.name
),
4825 howto
->name
, addend
, (bfd
*) NULL
, (asection
*) NULL
,
4833 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
4834 (file_ptr
) link_order
->offset
, size
);
4840 /* The address of a reloc is relative to the section in a
4841 relocateable file, and is a virtual address in an executable
4843 offset
= link_order
->offset
;
4844 if (! info
->relocateable
)
4845 offset
+= output_section
->vma
;
4847 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
4849 if (rel_hdr
->sh_type
== SHT_REL
)
4851 Elf_Internal_Rel irel
;
4852 Elf_External_Rel
*erel
;
4854 irel
.r_offset
= offset
;
4855 irel
.r_info
= ELF_R_INFO (indx
, howto
->type
);
4856 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
4857 + output_section
->reloc_count
);
4858 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
4862 Elf_Internal_Rela irela
;
4863 Elf_External_Rela
*erela
;
4865 irela
.r_offset
= offset
;
4866 irela
.r_info
= ELF_R_INFO (indx
, howto
->type
);
4867 irela
.r_addend
= addend
;
4868 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
4869 + output_section
->reloc_count
);
4870 elf_swap_reloca_out (output_bfd
, &irela
, erela
);
4873 ++output_section
->reloc_count
;
4879 /* Allocate a pointer to live in a linker created section. */
4882 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
4884 struct bfd_link_info
*info
;
4885 elf_linker_section_t
*lsect
;
4886 struct elf_link_hash_entry
*h
;
4887 const Elf_Internal_Rela
*rel
;
4889 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
4890 elf_linker_section_pointers_t
*linker_section_ptr
;
4891 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);;
4893 BFD_ASSERT (lsect
!= NULL
);
4895 /* Is this a global symbol? */
4898 /* Has this symbol already been allocated, if so, our work is done */
4899 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
4904 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
4905 /* Make sure this symbol is output as a dynamic symbol. */
4906 if (h
->dynindx
== -1)
4908 if (! elf_link_record_dynamic_symbol (info
, h
))
4912 if (lsect
->rel_section
)
4913 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
4916 else /* Allocation of a pointer to a local symbol */
4918 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
4920 /* Allocate a table to hold the local symbols if first time */
4923 int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
4924 register unsigned int i
;
4926 ptr
= (elf_linker_section_pointers_t
**)
4927 bfd_alloc (abfd
, num_symbols
* sizeof (elf_linker_section_pointers_t
*));
4932 elf_local_ptr_offsets (abfd
) = ptr
;
4933 for (i
= 0; i
< num_symbols
; i
++)
4934 ptr
[i
] = (elf_linker_section_pointers_t
*)0;
4937 /* Has this symbol already been allocated, if so, our work is done */
4938 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
4943 ptr_linker_section_ptr
= &ptr
[r_symndx
];
4947 /* If we are generating a shared object, we need to
4948 output a R_<xxx>_RELATIVE reloc so that the
4949 dynamic linker can adjust this GOT entry. */
4950 BFD_ASSERT (lsect
->rel_section
!= NULL
);
4951 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
4955 /* Allocate space for a pointer in the linker section, and allocate a new pointer record
4956 from internal memory. */
4957 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
4958 linker_section_ptr
= (elf_linker_section_pointers_t
*)
4959 bfd_alloc (abfd
, sizeof (elf_linker_section_pointers_t
));
4961 if (!linker_section_ptr
)
4964 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
4965 linker_section_ptr
->addend
= rel
->r_addend
;
4966 linker_section_ptr
->which
= lsect
->which
;
4967 linker_section_ptr
->written_address_p
= false;
4968 *ptr_linker_section_ptr
= linker_section_ptr
;
4971 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
4973 linker_section_ptr
->offset
= lsect
->section
->_raw_size
- lsect
->hole_size
+ (ARCH_SIZE
/ 8);
4974 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
4975 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
4976 if (lsect
->sym_hash
) /* Bump up symbol value if needed */
4978 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
4980 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
4981 lsect
->sym_hash
->root
.root
.string
,
4982 (long)ARCH_SIZE
/ 8,
4983 (long)lsect
->sym_hash
->root
.u
.def
.value
);
4989 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
4991 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
4994 fprintf (stderr
, "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
4995 lsect
->name
, (long)linker_section_ptr
->offset
, (long)lsect
->section
->_raw_size
);
5003 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
5006 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
5009 /* Fill in the address for a pointer generated in alinker section. */
5012 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
, relocation
, rel
, relative_reloc
)
5015 struct bfd_link_info
*info
;
5016 elf_linker_section_t
*lsect
;
5017 struct elf_link_hash_entry
*h
;
5019 const Elf_Internal_Rela
*rel
;
5022 elf_linker_section_pointers_t
*linker_section_ptr
;
5024 BFD_ASSERT (lsect
!= NULL
);
5026 if (h
!= NULL
) /* global symbol */
5028 linker_section_ptr
= _bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
5032 BFD_ASSERT (linker_section_ptr
!= NULL
);
5034 if (! elf_hash_table (info
)->dynamic_sections_created
5037 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
5039 /* This is actually a static link, or it is a
5040 -Bsymbolic link and the symbol is defined
5041 locally. We must initialize this entry in the
5044 When doing a dynamic link, we create a .rela.<xxx>
5045 relocation entry to initialize the value. This
5046 is done in the finish_dynamic_symbol routine. */
5047 if (!linker_section_ptr
->written_address_p
)
5049 linker_section_ptr
->written_address_p
= true;
5050 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
5051 lsect
->section
->contents
+ linker_section_ptr
->offset
);
5055 else /* local symbol */
5057 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
5058 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
5059 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
5060 linker_section_ptr
= _bfd_elf_find_pointer_linker_section (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
5064 BFD_ASSERT (linker_section_ptr
!= NULL
);
5066 /* Write out pointer if it hasn't been rewritten out before */
5067 if (!linker_section_ptr
->written_address_p
)
5069 linker_section_ptr
->written_address_p
= true;
5070 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
5071 lsect
->section
->contents
+ linker_section_ptr
->offset
);
5075 asection
*srel
= lsect
->rel_section
;
5076 Elf_Internal_Rela outrel
;
5078 /* We need to generate a relative reloc for the dynamic linker. */
5080 lsect
->rel_section
= srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
5083 BFD_ASSERT (srel
!= NULL
);
5085 outrel
.r_offset
= (lsect
->section
->output_section
->vma
5086 + lsect
->section
->output_offset
5087 + linker_section_ptr
->offset
);
5088 outrel
.r_info
= ELF_R_INFO (0, relative_reloc
);
5089 outrel
.r_addend
= 0;
5090 elf_swap_reloca_out (output_bfd
, &outrel
,
5091 (((Elf_External_Rela
*)
5092 lsect
->section
->contents
)
5093 + lsect
->section
->reloc_count
));
5094 ++lsect
->section
->reloc_count
;
5099 relocation
= (lsect
->section
->output_offset
5100 + linker_section_ptr
->offset
5101 - lsect
->hole_offset
5102 - lsect
->sym_offset
);
5105 fprintf (stderr
, "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
5106 lsect
->name
, (long)relocation
, (long)relocation
);
5109 /* Subtract out the addend, because it will get added back in by the normal
5111 return relocation
- linker_section_ptr
->addend
;