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 static boolean elf_link_add_object_symbols
23 PARAMS ((bfd
*, struct bfd_link_info
*));
24 static boolean elf_link_add_archive_symbols
25 PARAMS ((bfd
*, struct bfd_link_info
*));
26 static boolean elf_export_symbol
27 PARAMS ((struct elf_link_hash_entry
*, PTR
));
28 static boolean elf_adjust_dynamic_symbol
29 PARAMS ((struct elf_link_hash_entry
*, PTR
));
30 static boolean elf_link_find_version_dependencies
31 PARAMS ((struct elf_link_hash_entry
*, PTR
));
32 static boolean elf_link_find_version_dependencies
33 PARAMS ((struct elf_link_hash_entry
*, PTR
));
34 static boolean elf_link_assign_sym_version
35 PARAMS ((struct elf_link_hash_entry
*, PTR
));
36 static boolean elf_link_renumber_dynsyms
37 PARAMS ((struct elf_link_hash_entry
*, PTR
));
39 /* This struct is used to pass information to routines called via
40 elf_link_hash_traverse which must return failure. */
42 struct elf_info_failed
45 struct bfd_link_info
*info
;
48 /* Given an ELF BFD, add symbols to the global hash table as
52 elf_bfd_link_add_symbols (abfd
, info
)
54 struct bfd_link_info
*info
;
56 switch (bfd_get_format (abfd
))
59 return elf_link_add_object_symbols (abfd
, info
);
61 return elf_link_add_archive_symbols (abfd
, info
);
63 bfd_set_error (bfd_error_wrong_format
);
69 /* Add symbols from an ELF archive file to the linker hash table. We
70 don't use _bfd_generic_link_add_archive_symbols because of a
71 problem which arises on UnixWare. The UnixWare libc.so is an
72 archive which includes an entry libc.so.1 which defines a bunch of
73 symbols. The libc.so archive also includes a number of other
74 object files, which also define symbols, some of which are the same
75 as those defined in libc.so.1. Correct linking requires that we
76 consider each object file in turn, and include it if it defines any
77 symbols we need. _bfd_generic_link_add_archive_symbols does not do
78 this; it looks through the list of undefined symbols, and includes
79 any object file which defines them. When this algorithm is used on
80 UnixWare, it winds up pulling in libc.so.1 early and defining a
81 bunch of symbols. This means that some of the other objects in the
82 archive are not included in the link, which is incorrect since they
83 precede libc.so.1 in the archive.
85 Fortunately, ELF archive handling is simpler than that done by
86 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
87 oddities. In ELF, if we find a symbol in the archive map, and the
88 symbol is currently undefined, we know that we must pull in that
91 Unfortunately, we do have to make multiple passes over the symbol
92 table until nothing further is resolved. */
95 elf_link_add_archive_symbols (abfd
, info
)
97 struct bfd_link_info
*info
;
100 boolean
*defined
= NULL
;
101 boolean
*included
= NULL
;
105 if (! bfd_has_map (abfd
))
107 /* An empty archive is a special case. */
108 if (bfd_openr_next_archived_file (abfd
, (bfd
*) NULL
) == NULL
)
110 bfd_set_error (bfd_error_no_armap
);
114 /* Keep track of all symbols we know to be already defined, and all
115 files we know to be already included. This is to speed up the
116 second and subsequent passes. */
117 c
= bfd_ardata (abfd
)->symdef_count
;
120 defined
= (boolean
*) bfd_malloc (c
* sizeof (boolean
));
121 included
= (boolean
*) bfd_malloc (c
* sizeof (boolean
));
122 if (defined
== (boolean
*) NULL
|| included
== (boolean
*) NULL
)
124 memset (defined
, 0, c
* sizeof (boolean
));
125 memset (included
, 0, c
* sizeof (boolean
));
127 symdefs
= bfd_ardata (abfd
)->symdefs
;
140 symdefend
= symdef
+ c
;
141 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
143 struct elf_link_hash_entry
*h
;
145 struct bfd_link_hash_entry
*undefs_tail
;
148 if (defined
[i
] || included
[i
])
150 if (symdef
->file_offset
== last
)
156 h
= elf_link_hash_lookup (elf_hash_table (info
), symdef
->name
,
157 false, false, false);
163 /* If this is a default version (the name contains @@),
164 look up the symbol again without the version. The
165 effect is that references to the symbol without the
166 version will be matched by the default symbol in the
169 p
= strchr (symdef
->name
, ELF_VER_CHR
);
170 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
173 copy
= bfd_alloc (abfd
, p
- symdef
->name
+ 1);
176 memcpy (copy
, symdef
->name
, p
- symdef
->name
);
177 copy
[p
- symdef
->name
] = '\0';
179 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
180 false, false, false);
182 bfd_release (abfd
, copy
);
188 if (h
->root
.type
!= bfd_link_hash_undefined
)
190 if (h
->root
.type
!= bfd_link_hash_undefweak
)
195 /* We need to include this archive member. */
197 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
198 if (element
== (bfd
*) NULL
)
201 if (! bfd_check_format (element
, bfd_object
))
204 /* Doublecheck that we have not included this object
205 already--it should be impossible, but there may be
206 something wrong with the archive. */
207 if (element
->archive_pass
!= 0)
209 bfd_set_error (bfd_error_bad_value
);
212 element
->archive_pass
= 1;
214 undefs_tail
= info
->hash
->undefs_tail
;
216 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
219 if (! elf_link_add_object_symbols (element
, info
))
222 /* If there are any new undefined symbols, we need to make
223 another pass through the archive in order to see whether
224 they can be defined. FIXME: This isn't perfect, because
225 common symbols wind up on undefs_tail and because an
226 undefined symbol which is defined later on in this pass
227 does not require another pass. This isn't a bug, but it
228 does make the code less efficient than it could be. */
229 if (undefs_tail
!= info
->hash
->undefs_tail
)
232 /* Look backward to mark all symbols from this object file
233 which we have already seen in this pass. */
237 included
[mark
] = true;
242 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
244 /* We mark subsequent symbols from this object file as we go
245 on through the loop. */
246 last
= symdef
->file_offset
;
257 if (defined
!= (boolean
*) NULL
)
259 if (included
!= (boolean
*) NULL
)
264 /* Add symbols from an ELF object file to the linker hash table. */
267 elf_link_add_object_symbols (abfd
, info
)
269 struct bfd_link_info
*info
;
271 boolean (*add_symbol_hook
) PARAMS ((bfd
*, struct bfd_link_info
*,
272 const Elf_Internal_Sym
*,
273 const char **, flagword
*,
274 asection
**, bfd_vma
*));
275 boolean (*check_relocs
) PARAMS ((bfd
*, struct bfd_link_info
*,
276 asection
*, const Elf_Internal_Rela
*));
278 Elf_Internal_Shdr
*hdr
;
282 Elf_External_Sym
*buf
= NULL
;
283 struct elf_link_hash_entry
**sym_hash
;
285 bfd_byte
*dynver
= NULL
;
286 Elf_External_Versym
*extversym
= NULL
;
287 Elf_External_Versym
*ever
;
288 Elf_External_Dyn
*dynbuf
= NULL
;
289 struct elf_link_hash_entry
*weaks
;
290 Elf_External_Sym
*esym
;
291 Elf_External_Sym
*esymend
;
293 add_symbol_hook
= get_elf_backend_data (abfd
)->elf_add_symbol_hook
;
294 collect
= get_elf_backend_data (abfd
)->collect
;
296 if ((abfd
->flags
& DYNAMIC
) == 0)
302 /* You can't use -r against a dynamic object. Also, there's no
303 hope of using a dynamic object which does not exactly match
304 the format of the output file. */
305 if (info
->relocateable
|| info
->hash
->creator
!= abfd
->xvec
)
307 bfd_set_error (bfd_error_invalid_operation
);
312 /* As a GNU extension, any input sections which are named
313 .gnu.warning.SYMBOL are treated as warning symbols for the given
314 symbol. This differs from .gnu.warning sections, which generate
315 warnings when they are included in an output file. */
320 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
324 name
= bfd_get_section_name (abfd
, s
);
325 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
330 name
+= sizeof ".gnu.warning." - 1;
332 /* If this is a shared object, then look up the symbol
333 in the hash table. If it is there, and it is already
334 been defined, then we will not be using the entry
335 from this shared object, so we don't need to warn.
336 FIXME: If we see the definition in a regular object
337 later on, we will warn, but we shouldn't. The only
338 fix is to keep track of what warnings we are supposed
339 to emit, and then handle them all at the end of the
341 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
343 struct elf_link_hash_entry
*h
;
345 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
348 /* FIXME: What about bfd_link_hash_common? */
350 && (h
->root
.type
== bfd_link_hash_defined
351 || h
->root
.type
== bfd_link_hash_defweak
))
353 /* We don't want to issue this warning. Clobber
354 the section size so that the warning does not
355 get copied into the output file. */
361 sz
= bfd_section_size (abfd
, s
);
362 msg
= (char *) bfd_alloc (abfd
, sz
);
366 if (! bfd_get_section_contents (abfd
, s
, msg
, (file_ptr
) 0, sz
))
369 if (! (_bfd_generic_link_add_one_symbol
370 (info
, abfd
, name
, BSF_WARNING
, s
, (bfd_vma
) 0, msg
,
371 false, collect
, (struct bfd_link_hash_entry
**) NULL
)))
374 if (! info
->relocateable
)
376 /* Clobber the section size so that the warning does
377 not get copied into the output file. */
384 /* If this is a dynamic object, we always link against the .dynsym
385 symbol table, not the .symtab symbol table. The dynamic linker
386 will only see the .dynsym symbol table, so there is no reason to
387 look at .symtab for a dynamic object. */
389 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
390 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
392 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
396 /* Read in any version definitions. */
398 if (elf_dynverdef (abfd
) != 0)
400 Elf_Internal_Shdr
*verdefhdr
;
403 const Elf_External_Verdef
*extverdef
;
404 Elf_Internal_Verdef
*intverdef
;
406 verdefhdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
407 elf_tdata (abfd
)->verdef
=
408 ((Elf_Internal_Verdef
*)
410 verdefhdr
->sh_info
* sizeof (Elf_Internal_Verdef
)));
411 if (elf_tdata (abfd
)->verdef
== NULL
)
414 dynver
= (bfd_byte
*) bfd_malloc (verdefhdr
->sh_size
);
418 if (bfd_seek (abfd
, verdefhdr
->sh_offset
, SEEK_SET
) != 0
419 || (bfd_read ((PTR
) dynver
, 1, verdefhdr
->sh_size
, abfd
)
420 != verdefhdr
->sh_size
))
423 extverdef
= (const Elf_External_Verdef
*) dynver
;
424 intverdef
= elf_tdata (abfd
)->verdef
;
425 for (i
= 0; i
< verdefhdr
->sh_info
; i
++, intverdef
++)
427 const Elf_External_Verdaux
*extverdaux
;
428 Elf_Internal_Verdaux intverdaux
;
430 _bfd_elf_swap_verdef_in (abfd
, extverdef
, intverdef
);
432 /* Pick up the name of the version. */
433 extverdaux
= ((const Elf_External_Verdaux
*)
434 ((bfd_byte
*) extverdef
+ intverdef
->vd_aux
));
435 _bfd_elf_swap_verdaux_in (abfd
, extverdaux
, &intverdaux
);
437 intverdef
->vd_bfd
= abfd
;
438 intverdef
->vd_nodename
=
439 bfd_elf_string_from_elf_section (abfd
, verdefhdr
->sh_link
,
440 intverdaux
.vda_name
);
442 extverdef
= ((const Elf_External_Verdef
*)
443 ((bfd_byte
*) extverdef
+ intverdef
->vd_next
));
450 /* Read in the symbol versions, but don't bother to convert them
451 to internal format. */
452 if (elf_dynversym (abfd
) != 0)
454 Elf_Internal_Shdr
*versymhdr
;
456 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
457 extversym
= (Elf_External_Versym
*) bfd_malloc (hdr
->sh_size
);
458 if (extversym
== NULL
)
460 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
461 || (bfd_read ((PTR
) extversym
, 1, versymhdr
->sh_size
, abfd
)
462 != versymhdr
->sh_size
))
467 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
469 /* The sh_info field of the symtab header tells us where the
470 external symbols start. We don't care about the local symbols at
472 if (elf_bad_symtab (abfd
))
474 extsymcount
= symcount
;
479 extsymcount
= symcount
- hdr
->sh_info
;
480 extsymoff
= hdr
->sh_info
;
483 buf
= ((Elf_External_Sym
*)
484 bfd_malloc (extsymcount
* sizeof (Elf_External_Sym
)));
485 if (buf
== NULL
&& extsymcount
!= 0)
488 /* We store a pointer to the hash table entry for each external
490 sym_hash
= ((struct elf_link_hash_entry
**)
492 extsymcount
* sizeof (struct elf_link_hash_entry
*)));
493 if (sym_hash
== NULL
)
495 elf_sym_hashes (abfd
) = sym_hash
;
499 /* If we are creating a shared library, create all the dynamic
500 sections immediately. We need to attach them to something,
501 so we attach them to this BFD, provided it is the right
502 format. FIXME: If there are no input BFD's of the same
503 format as the output, we can't make a shared library. */
505 && ! elf_hash_table (info
)->dynamic_sections_created
506 && abfd
->xvec
== info
->hash
->creator
)
508 if (! elf_link_create_dynamic_sections (abfd
, info
))
517 bfd_size_type oldsize
;
518 bfd_size_type strindex
;
520 /* Find the name to use in a DT_NEEDED entry that refers to this
521 object. If the object has a DT_SONAME entry, we use it.
522 Otherwise, if the generic linker stuck something in
523 elf_dt_name, we use that. Otherwise, we just use the file
524 name. If the generic linker put a null string into
525 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
526 there is a DT_SONAME entry. */
528 name
= bfd_get_filename (abfd
);
529 if (elf_dt_name (abfd
) != NULL
)
531 name
= elf_dt_name (abfd
);
535 s
= bfd_get_section_by_name (abfd
, ".dynamic");
538 Elf_External_Dyn
*extdyn
;
539 Elf_External_Dyn
*extdynend
;
543 dynbuf
= (Elf_External_Dyn
*) bfd_malloc ((size_t) s
->_raw_size
);
547 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
548 (file_ptr
) 0, s
->_raw_size
))
551 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
554 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
557 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
558 for (; extdyn
< extdynend
; extdyn
++)
560 Elf_Internal_Dyn dyn
;
562 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
563 if (dyn
.d_tag
== DT_SONAME
)
565 name
= bfd_elf_string_from_elf_section (abfd
, link
,
570 if (dyn
.d_tag
== DT_NEEDED
)
572 struct bfd_link_needed_list
*n
, **pn
;
575 n
= ((struct bfd_link_needed_list
*)
576 bfd_alloc (abfd
, sizeof (struct bfd_link_needed_list
)));
577 fnm
= bfd_elf_string_from_elf_section (abfd
, link
,
579 if (n
== NULL
|| fnm
== NULL
)
581 anm
= bfd_alloc (abfd
, strlen (fnm
) + 1);
588 for (pn
= &elf_hash_table (info
)->needed
;
600 /* We do not want to include any of the sections in a dynamic
601 object in the output file. We hack by simply clobbering the
602 list of sections in the BFD. This could be handled more
603 cleanly by, say, a new section flag; the existing
604 SEC_NEVER_LOAD flag is not the one we want, because that one
605 still implies that the section takes up space in the output
607 abfd
->sections
= NULL
;
608 abfd
->section_count
= 0;
610 /* If this is the first dynamic object found in the link, create
611 the special sections required for dynamic linking. */
612 if (! elf_hash_table (info
)->dynamic_sections_created
)
614 if (! elf_link_create_dynamic_sections (abfd
, info
))
620 /* Add a DT_NEEDED entry for this dynamic object. */
621 oldsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
622 strindex
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, name
,
624 if (strindex
== (bfd_size_type
) -1)
627 if (oldsize
== _bfd_stringtab_size (elf_hash_table (info
)->dynstr
))
630 Elf_External_Dyn
*dyncon
, *dynconend
;
632 /* The hash table size did not change, which means that
633 the dynamic object name was already entered. If we
634 have already included this dynamic object in the
635 link, just ignore it. There is no reason to include
636 a particular dynamic object more than once. */
637 sdyn
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
639 BFD_ASSERT (sdyn
!= NULL
);
641 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
642 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
644 for (; dyncon
< dynconend
; dyncon
++)
646 Elf_Internal_Dyn dyn
;
648 elf_swap_dyn_in (elf_hash_table (info
)->dynobj
, dyncon
,
650 if (dyn
.d_tag
== DT_NEEDED
651 && dyn
.d_un
.d_val
== strindex
)
655 if (extversym
!= NULL
)
662 if (! elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
666 /* Save the SONAME, if there is one, because sometimes the
667 linker emulation code will need to know it. */
669 name
= bfd_get_filename (abfd
);
670 elf_dt_name (abfd
) = name
;
674 hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
),
676 || (bfd_read ((PTR
) buf
, sizeof (Elf_External_Sym
), extsymcount
, abfd
)
677 != extsymcount
* sizeof (Elf_External_Sym
)))
682 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
683 esymend
= buf
+ extsymcount
;
686 esym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
688 Elf_Internal_Sym sym
;
694 struct elf_link_hash_entry
*h
;
696 boolean size_change_ok
, type_change_ok
;
698 unsigned int old_alignment
;
700 elf_swap_symbol_in (abfd
, esym
, &sym
);
702 flags
= BSF_NO_FLAGS
;
704 value
= sym
.st_value
;
707 bind
= ELF_ST_BIND (sym
.st_info
);
708 if (bind
== STB_LOCAL
)
710 /* This should be impossible, since ELF requires that all
711 global symbols follow all local symbols, and that sh_info
712 point to the first global symbol. Unfortunatealy, Irix 5
716 else if (bind
== STB_GLOBAL
)
718 if (sym
.st_shndx
!= SHN_UNDEF
719 && sym
.st_shndx
!= SHN_COMMON
)
724 else if (bind
== STB_WEAK
)
728 /* Leave it up to the processor backend. */
731 if (sym
.st_shndx
== SHN_UNDEF
)
732 sec
= bfd_und_section_ptr
;
733 else if (sym
.st_shndx
> 0 && sym
.st_shndx
< SHN_LORESERVE
)
735 sec
= section_from_elf_index (abfd
, sym
.st_shndx
);
737 sec
= bfd_abs_section_ptr
;
738 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
741 else if (sym
.st_shndx
== SHN_ABS
)
742 sec
= bfd_abs_section_ptr
;
743 else if (sym
.st_shndx
== SHN_COMMON
)
745 sec
= bfd_com_section_ptr
;
746 /* What ELF calls the size we call the value. What ELF
747 calls the value we call the alignment. */
752 /* Leave it up to the processor backend. */
755 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
756 if (name
== (const char *) NULL
)
761 if (! (*add_symbol_hook
) (abfd
, info
, &sym
, &name
, &flags
, &sec
,
765 /* The hook function sets the name to NULL if this symbol
766 should be skipped for some reason. */
767 if (name
== (const char *) NULL
)
771 /* Sanity check that all possibilities were handled. */
772 if (sec
== (asection
*) NULL
)
774 bfd_set_error (bfd_error_bad_value
);
778 if (bfd_is_und_section (sec
)
779 || bfd_is_com_section (sec
))
784 size_change_ok
= false;
785 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
787 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
789 Elf_Internal_Versym iver
;
795 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
796 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
798 /* If this is a hidden symbol, or if it is not version
799 1, we append the version name to the symbol name.
800 However, we do not modify a non-hidden absolute
801 symbol, because it might be the version symbol
802 itself. FIXME: What if it isn't? */
803 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
804 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
810 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
812 (*_bfd_error_handler
)
813 ("%s: %s: invalid version %d (max %d)",
814 abfd
->filename
, name
, vernum
,
815 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
816 bfd_set_error (bfd_error_bad_value
);
820 verstr
= elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
824 namelen
= strlen (name
);
825 newlen
= namelen
+ strlen (verstr
) + 2;
826 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
829 newname
= (char *) bfd_alloc (abfd
, newlen
);
832 strcpy (newname
, name
);
833 p
= newname
+ namelen
;
835 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
843 /* We need to look up the symbol now in order to get some of
844 the dynamic object handling right. We pass the hash
845 table entry in to _bfd_generic_link_add_one_symbol so
846 that it does not have to look it up again. */
847 if (! bfd_is_und_section (sec
))
848 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
851 h
= ((struct elf_link_hash_entry
*)
852 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true,
858 if (h
->root
.type
== bfd_link_hash_new
)
859 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
861 while (h
->root
.type
== bfd_link_hash_indirect
862 || h
->root
.type
== bfd_link_hash_warning
)
863 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
865 /* FIXME: There are too many cases here, and it's too
866 confusing. This code needs to be reorganized somehow. */
868 /* It's OK to change the type if it used to be a weak
869 definition, or if the current definition is weak (and
870 hence might be ignored). */
871 if (h
->root
.type
== bfd_link_hash_defweak
872 || h
->root
.type
== bfd_link_hash_undefweak
874 type_change_ok
= true;
876 /* It's OK to change the size if it used to be a weak
877 definition, or if it used to be undefined, or if we will
878 be overriding an old definition. */
880 || h
->root
.type
== bfd_link_hash_undefined
)
881 size_change_ok
= true;
883 if (h
->root
.type
== bfd_link_hash_common
)
884 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
888 /* If we are looking at a dynamic object, and this is a
889 definition, we need to see if it has already been defined
890 by some other object. If it has, we want to use the
891 existing definition, and we do not want to report a
892 multiple symbol definition error; we do this by
893 clobbering sec to be bfd_und_section_ptr. We treat a
894 common symbol as a definition if the symbol in the shared
895 library is a function, since common symbols always
896 represent variables; this can cause confusion in
897 principle, but any such confusion would seem to indicate
898 an erroneous program or shared library. */
899 if (dynamic
&& definition
)
901 if (h
->root
.type
== bfd_link_hash_defined
902 || h
->root
.type
== bfd_link_hash_defweak
903 || (h
->root
.type
== bfd_link_hash_common
905 || ELF_ST_TYPE (sym
.st_info
) == STT_FUNC
)))
907 /* In the special case of two symbols which look
908 like common symbols in a dynamic object, set the
909 size of the symbol to the larger of the two. */
910 if ((sec
->flags
& SEC_ALLOC
) != 0
911 && (sec
->flags
& SEC_LOAD
) == 0
914 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
915 && h
->root
.type
== bfd_link_hash_defined
916 && (h
->elf_link_hash_flags
917 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
918 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) != 0
919 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
920 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
922 && h
->type
!= STT_FUNC
923 && sym
.st_size
!= h
->size
)
925 /* Note that we only warn if the size is
926 different. If the size is the same, then we
927 simply let the first shared library override
929 if (! ((*info
->callbacks
->multiple_common
)
930 (info
, h
->root
.root
.string
,
931 h
->root
.u
.def
.section
->owner
,
932 bfd_link_hash_common
,
933 h
->size
, abfd
, bfd_link_hash_common
,
936 if (sym
.st_size
> h
->size
)
937 h
->size
= sym
.st_size
;
941 sec
= bfd_und_section_ptr
;
943 size_change_ok
= true;
944 if (h
->root
.type
== bfd_link_hash_common
)
945 type_change_ok
= true;
949 /* If we already have a common symbol, and the symbol in the
950 shared library is in an uninitialized section, then treat
951 the shared library symbol as a common symbol. This will
952 not always be correct, but it should do little harm. */
955 && h
->root
.type
== bfd_link_hash_common
956 && (sec
->flags
& SEC_ALLOC
) != 0
957 && (sec
->flags
& SEC_LOAD
) == 0
960 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
)
963 sec
= bfd_com_section_ptr
;
966 size_change_ok
= true;
969 /* If we are not looking at a dynamic object, and we have a
970 definition, we want to override any definition we may
971 have from a dynamic object. Symbols from regular files
972 always take precedence over symbols from dynamic objects,
973 even if they are defined after the dynamic object in the
977 || (bfd_is_com_section (sec
)
978 && (h
->root
.type
== bfd_link_hash_defweak
979 || h
->type
== STT_FUNC
)))
980 && (h
->root
.type
== bfd_link_hash_defined
981 || h
->root
.type
== bfd_link_hash_defweak
)
982 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
983 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) != 0)
986 /* Change the hash table entry to undefined, and let
987 _bfd_generic_link_add_one_symbol do the right thing
988 with the new definition. */
989 h
->root
.type
= bfd_link_hash_undefined
;
990 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
991 size_change_ok
= true;
992 if (bfd_is_com_section (sec
))
993 type_change_ok
= true;
995 /* This union may have been set to be non-NULL when this
996 symbol was seen in a dynamic object. We must force
997 the union to be NULL, so that it is correct for a
999 h
->verinfo
.vertree
= NULL
;
1002 /* If we are not looking at a shared library and we have a
1003 common symbol, and the symbol in the shared library is in
1004 an uninitialized section, then treat the shared library
1005 symbol as a common symbol. This will not always be
1006 correct, but it should do little harm. Note that the
1007 above condition already handled cases in which a common
1008 symbol should simply override the definition in the
1012 && bfd_is_com_section (sec
)
1013 && h
->root
.type
== bfd_link_hash_defined
1014 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1015 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) != 0
1016 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1017 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1019 && h
->type
!= STT_FUNC
)
1021 /* It would be best if we could set the hash table entry
1022 to a common symbol, but we don't know what to use for
1023 the section or the alignment. */
1024 if (! ((*info
->callbacks
->multiple_common
)
1025 (info
, h
->root
.root
.string
,
1026 h
->root
.u
.def
.section
->owner
, bfd_link_hash_common
,
1027 h
->size
, abfd
, bfd_link_hash_common
, value
)))
1030 if (h
->size
> value
)
1033 /* FIXME: We no longer know the alignment required by
1034 the symbol in the shared library, so we just wind up
1035 using the one from the regular object. */
1038 h
->root
.type
= bfd_link_hash_undefined
;
1039 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1040 size_change_ok
= true;
1041 type_change_ok
= true;
1042 h
->verinfo
.vertree
= NULL
;
1048 && (h
->verinfo
.verdef
== NULL
|| definition
))
1049 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1052 if (! (_bfd_generic_link_add_one_symbol
1053 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
1054 false, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
1058 while (h
->root
.type
== bfd_link_hash_indirect
1059 || h
->root
.type
== bfd_link_hash_warning
)
1060 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1063 new_weakdef
= false;
1066 && (flags
& BSF_WEAK
) != 0
1067 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
1068 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1069 && h
->weakdef
== NULL
)
1071 /* Keep a list of all weak defined non function symbols from
1072 a dynamic object, using the weakdef field. Later in this
1073 function we will set the weakdef field to the correct
1074 value. We only put non-function symbols from dynamic
1075 objects on this list, because that happens to be the only
1076 time we need to know the normal symbol corresponding to a
1077 weak symbol, and the information is time consuming to
1078 figure out. If the weakdef field is not already NULL,
1079 then this symbol was already defined by some previous
1080 dynamic object, and we will be using that previous
1081 definition anyhow. */
1088 /* Set the alignment of a common symbol. */
1089 if (sym
.st_shndx
== SHN_COMMON
1090 && h
->root
.type
== bfd_link_hash_common
)
1094 align
= bfd_log2 (sym
.st_value
);
1095 if (align
> old_alignment
)
1096 h
->root
.u
.c
.p
->alignment_power
= align
;
1099 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1105 /* Remember the symbol size and type. */
1106 if (sym
.st_size
!= 0
1107 && (definition
|| h
->size
== 0))
1109 if (h
->size
!= 0 && h
->size
!= sym
.st_size
&& ! size_change_ok
)
1110 (*_bfd_error_handler
)
1111 ("Warning: size of symbol `%s' changed from %lu to %lu in %s",
1112 name
, (unsigned long) h
->size
, (unsigned long) sym
.st_size
,
1113 bfd_get_filename (abfd
));
1115 h
->size
= sym
.st_size
;
1118 /* If this is a common symbol, then we always want H->SIZE
1119 to be the size of the common symbol. The code just above
1120 won't fix the size if a common symbol becomes larger. We
1121 don't warn about a size change here, because that is
1122 covered by --warn-common. */
1123 if (h
->root
.type
== bfd_link_hash_common
)
1124 h
->size
= h
->root
.u
.c
.size
;
1126 if (ELF_ST_TYPE (sym
.st_info
) != STT_NOTYPE
1127 && (definition
|| h
->type
== STT_NOTYPE
))
1129 if (h
->type
!= STT_NOTYPE
1130 && h
->type
!= ELF_ST_TYPE (sym
.st_info
)
1131 && ! type_change_ok
)
1132 (*_bfd_error_handler
)
1133 ("Warning: type of symbol `%s' changed from %d to %d in %s",
1134 name
, h
->type
, ELF_ST_TYPE (sym
.st_info
),
1135 bfd_get_filename (abfd
));
1137 h
->type
= ELF_ST_TYPE (sym
.st_info
);
1140 if (sym
.st_other
!= 0
1141 && (definition
|| h
->other
== 0))
1142 h
->other
= sym
.st_other
;
1144 /* Set a flag in the hash table entry indicating the type of
1145 reference or definition we just found. Keep a count of
1146 the number of dynamic symbols we find. A dynamic symbol
1147 is one which is referenced or defined by both a regular
1148 object and a shared object. */
1149 old_flags
= h
->elf_link_hash_flags
;
1154 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
1156 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
1158 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1159 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
1165 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
1167 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
1168 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
1169 | ELF_LINK_HASH_REF_REGULAR
)) != 0
1170 || (h
->weakdef
!= NULL
1172 && h
->weakdef
->dynindx
!= -1))
1176 h
->elf_link_hash_flags
|= new_flag
;
1178 /* If this symbol has a version, and it is the default
1179 version, we create an indirect symbol from the default
1180 name to the fully decorated name. This will cause
1181 external references which do not specify a version to be
1182 bound to this version of the symbol. */
1187 p
= strchr (name
, ELF_VER_CHR
);
1188 if (p
!= NULL
&& p
[1] == ELF_VER_CHR
)
1191 struct elf_link_hash_entry
*hold
;
1193 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1195 if (shortname
== NULL
)
1197 strncpy (shortname
, name
, p
- name
);
1198 shortname
[p
- name
] = '\0';
1200 /* First look to see if we have an existing symbol
1202 hold
= elf_link_hash_lookup (elf_hash_table (info
),
1203 shortname
, false, false,
1206 /* If we are looking at a normal object, and the
1207 symbol was seen in a shared object, clobber the
1208 definition in the shared object. */
1211 && (hold
->root
.type
== bfd_link_hash_defined
1212 || hold
->root
.type
== bfd_link_hash_defweak
)
1213 && (hold
->elf_link_hash_flags
1214 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1215 && ((hold
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
)
1218 /* Change the hash table entry to undefined, so
1219 that _bfd_generic_link_add_one_symbol will do
1221 hold
->root
.type
= bfd_link_hash_undefined
;
1222 hold
->root
.u
.undef
.abfd
=
1223 hold
->root
.u
.def
.section
->owner
;
1224 hold
->verinfo
.vertree
= NULL
;
1228 /* If we are looking at a shared object, and we have
1229 already seen this symbol defined elsewhere, then
1230 don't try to define it again. */
1233 && (hold
->root
.type
== bfd_link_hash_defined
1234 || hold
->root
.type
== bfd_link_hash_defweak
1235 || hold
->root
.type
== bfd_link_hash_indirect
1236 || (hold
->root
.type
== bfd_link_hash_common
1237 && (bind
== STB_WEAK
1238 || ELF_ST_TYPE (sym
.st_info
) == STT_FUNC
))))
1240 /* Don't add an indirect symbol. */
1244 struct elf_link_hash_entry
*hi
;
1247 if (! (_bfd_generic_link_add_one_symbol
1248 (info
, abfd
, shortname
, BSF_INDIRECT
,
1249 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1250 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1253 /* If there is a duplicate definition somewhere,
1254 then HI may not point to an indirect symbol.
1255 We will have reported an error to the user in
1258 if (hi
->root
.type
== bfd_link_hash_indirect
)
1260 hi
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
1262 /* If the symbol became indirect, then we
1263 assume that we have not seen a definition
1265 BFD_ASSERT ((hi
->elf_link_hash_flags
1266 & (ELF_LINK_HASH_DEF_DYNAMIC
1267 | ELF_LINK_HASH_DEF_REGULAR
))
1270 /* Copy down any references that we may have
1271 already seen to the symbol which just
1273 h
->elf_link_hash_flags
|=
1274 (hi
->elf_link_hash_flags
1275 & (ELF_LINK_HASH_REF_DYNAMIC
1276 | ELF_LINK_HASH_REF_REGULAR
));
1278 /* Copy over the global table offset entry.
1279 This may have been already set up by a
1280 check_relocs routine. */
1281 if (h
->got_offset
== (bfd_vma
) -1)
1283 h
->got_offset
= hi
->got_offset
;
1284 hi
->got_offset
= (bfd_vma
) -1;
1286 BFD_ASSERT (hi
->got_offset
== (bfd_vma
) -1);
1288 if (h
->dynindx
== -1)
1290 h
->dynindx
= hi
->dynindx
;
1291 h
->dynstr_index
= hi
->dynstr_index
;
1293 hi
->dynstr_index
= 0;
1295 BFD_ASSERT (hi
->dynindx
== -1);
1297 /* FIXME: There may be other information to
1298 copy over for particular targets. */
1300 /* See if the new flags lead us to realize
1301 that the symbol must be dynamic. */
1307 || ((hi
->elf_link_hash_flags
1308 & ELF_LINK_HASH_REF_DYNAMIC
)
1314 if ((hi
->elf_link_hash_flags
1315 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1322 /* We also need to define an indirection from the
1323 nondefault version of the symbol. */
1325 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1327 if (shortname
== NULL
)
1329 strncpy (shortname
, name
, p
- name
);
1330 strcpy (shortname
+ (p
- name
), p
+ 1);
1332 /* First look to see if we have an existing symbol
1334 hold
= elf_link_hash_lookup (elf_hash_table (info
),
1335 shortname
, false, false,
1338 /* If we are looking at a normal object, and the
1339 symbol was seen in a shared object, clobber the
1340 definition in the shared object. */
1343 && (hold
->root
.type
== bfd_link_hash_defined
1344 || hold
->root
.type
== bfd_link_hash_defweak
)
1345 && (hold
->elf_link_hash_flags
1346 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1347 && ((hold
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
)
1350 /* Change the hash table entry to undefined, so
1351 that _bfd_generic_link_add_one_symbol will do
1353 hold
->root
.type
= bfd_link_hash_undefined
;
1354 hold
->root
.u
.undef
.abfd
=
1355 hold
->root
.u
.def
.section
->owner
;
1356 hold
->verinfo
.vertree
= NULL
;
1360 /* If we are looking at a shared object, and we have
1361 already seen this symbol defined elsewhere, then
1362 don't try to define it again. */
1365 && (hold
->root
.type
== bfd_link_hash_defined
1366 || hold
->root
.type
== bfd_link_hash_defweak
1367 || hold
->root
.type
== bfd_link_hash_indirect
1368 || (hold
->root
.type
== bfd_link_hash_common
1369 && (bind
== STB_WEAK
1370 || ELF_ST_TYPE (sym
.st_info
) == STT_FUNC
))))
1372 /* Don't add an indirect symbol. */
1376 struct elf_link_hash_entry
*hi
;
1379 if (! (_bfd_generic_link_add_one_symbol
1380 (info
, abfd
, shortname
, BSF_INDIRECT
,
1381 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1382 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1385 /* If there is a duplicate definition somewhere,
1386 then HI may not point to an indirect symbol.
1387 We will have reported an error to the user in
1390 if (hi
->root
.type
== bfd_link_hash_indirect
)
1392 hi
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
1394 /* If the symbol became indirect, then we
1395 assume that we have not seen a definition
1397 BFD_ASSERT ((hi
->elf_link_hash_flags
1398 & (ELF_LINK_HASH_DEF_DYNAMIC
1399 | ELF_LINK_HASH_DEF_REGULAR
))
1402 /* Copy down any references that we may have
1403 already seen to the symbol which just
1405 h
->elf_link_hash_flags
|=
1406 (hi
->elf_link_hash_flags
1407 & (ELF_LINK_HASH_REF_DYNAMIC
1408 | ELF_LINK_HASH_REF_REGULAR
));
1410 /* Copy over the global table offset entry.
1411 This may have been already set up by a
1412 check_relocs routine. */
1413 if (h
->got_offset
== (bfd_vma
) -1)
1415 h
->got_offset
= hi
->got_offset
;
1416 hi
->got_offset
= (bfd_vma
) -1;
1418 BFD_ASSERT (hi
->got_offset
== (bfd_vma
) -1);
1420 if (h
->dynindx
== -1)
1422 h
->dynindx
= hi
->dynindx
;
1423 h
->dynstr_index
= hi
->dynstr_index
;
1425 hi
->dynstr_index
= 0;
1427 BFD_ASSERT (hi
->dynindx
== -1);
1429 /* FIXME: There may be other information to
1430 copy over for particular targets. */
1432 /* See if the new flags lead us to realize
1433 that the symbol must be dynamic. */
1439 || ((hi
->elf_link_hash_flags
1440 & ELF_LINK_HASH_REF_DYNAMIC
)
1446 if ((hi
->elf_link_hash_flags
1447 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1456 if (dynsym
&& h
->dynindx
== -1)
1458 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1460 if (h
->weakdef
!= NULL
1462 && h
->weakdef
->dynindx
== -1)
1464 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1472 /* Now set the weakdefs field correctly for all the weak defined
1473 symbols we found. The only way to do this is to search all the
1474 symbols. Since we only need the information for non functions in
1475 dynamic objects, that's the only time we actually put anything on
1476 the list WEAKS. We need this information so that if a regular
1477 object refers to a symbol defined weakly in a dynamic object, the
1478 real symbol in the dynamic object is also put in the dynamic
1479 symbols; we also must arrange for both symbols to point to the
1480 same memory location. We could handle the general case of symbol
1481 aliasing, but a general symbol alias can only be generated in
1482 assembler code, handling it correctly would be very time
1483 consuming, and other ELF linkers don't handle general aliasing
1485 while (weaks
!= NULL
)
1487 struct elf_link_hash_entry
*hlook
;
1490 struct elf_link_hash_entry
**hpp
;
1491 struct elf_link_hash_entry
**hppend
;
1494 weaks
= hlook
->weakdef
;
1495 hlook
->weakdef
= NULL
;
1497 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
1498 || hlook
->root
.type
== bfd_link_hash_defweak
1499 || hlook
->root
.type
== bfd_link_hash_common
1500 || hlook
->root
.type
== bfd_link_hash_indirect
);
1501 slook
= hlook
->root
.u
.def
.section
;
1502 vlook
= hlook
->root
.u
.def
.value
;
1504 hpp
= elf_sym_hashes (abfd
);
1505 hppend
= hpp
+ extsymcount
;
1506 for (; hpp
< hppend
; hpp
++)
1508 struct elf_link_hash_entry
*h
;
1511 if (h
!= NULL
&& h
!= hlook
1512 && h
->root
.type
== bfd_link_hash_defined
1513 && h
->root
.u
.def
.section
== slook
1514 && h
->root
.u
.def
.value
== vlook
)
1518 /* If the weak definition is in the list of dynamic
1519 symbols, make sure the real definition is put there
1521 if (hlook
->dynindx
!= -1
1522 && h
->dynindx
== -1)
1524 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1528 /* If the real definition is in the list of dynamic
1529 symbols, make sure the weak definition is put there
1530 as well. If we don't do this, then the dynamic
1531 loader might not merge the entries for the real
1532 definition and the weak definition. */
1533 if (h
->dynindx
!= -1
1534 && hlook
->dynindx
== -1)
1536 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
1551 if (extversym
!= NULL
)
1557 /* If this object is the same format as the output object, and it is
1558 not a shared library, then let the backend look through the
1561 This is required to build global offset table entries and to
1562 arrange for dynamic relocs. It is not required for the
1563 particular common case of linking non PIC code, even when linking
1564 against shared libraries, but unfortunately there is no way of
1565 knowing whether an object file has been compiled PIC or not.
1566 Looking through the relocs is not particularly time consuming.
1567 The problem is that we must either (1) keep the relocs in memory,
1568 which causes the linker to require additional runtime memory or
1569 (2) read the relocs twice from the input file, which wastes time.
1570 This would be a good case for using mmap.
1572 I have no idea how to handle linking PIC code into a file of a
1573 different format. It probably can't be done. */
1574 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
1576 && abfd
->xvec
== info
->hash
->creator
1577 && check_relocs
!= NULL
)
1581 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1583 Elf_Internal_Rela
*internal_relocs
;
1586 if ((o
->flags
& SEC_RELOC
) == 0
1587 || o
->reloc_count
== 0
1588 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
1589 && (o
->flags
& SEC_DEBUGGING
) != 0))
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
;
2057 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2060 /* The backend may have to create some sections regardless of whether
2061 we're dynamic or not. */
2062 bed
= get_elf_backend_data (output_bfd
);
2063 if (bed
->elf_backend_always_size_sections
2064 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
2067 dynobj
= elf_hash_table (info
)->dynobj
;
2069 /* If there were no dynamic objects in the link, there is nothing to
2074 /* If we are supposed to export all symbols into the dynamic symbol
2075 table (this is not the normal case), then do so. */
2078 struct elf_info_failed eif
;
2082 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
2088 if (elf_hash_table (info
)->dynamic_sections_created
)
2090 struct elf_info_failed eif
;
2091 struct elf_link_hash_entry
*h
;
2092 bfd_size_type strsize
;
2094 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
2095 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
2099 soname_indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2100 soname
, true, true);
2101 if (soname_indx
== (bfd_size_type
) -1
2102 || ! elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
2108 if (! elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
2116 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, rpath
,
2118 if (indx
== (bfd_size_type
) -1
2119 || ! elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
2123 if (filter_shlib
!= NULL
)
2127 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2128 filter_shlib
, true, true);
2129 if (indx
== (bfd_size_type
) -1
2130 || ! elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
2134 if (auxiliary_filters
!= NULL
)
2136 const char * const *p
;
2138 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
2142 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2144 if (indx
== (bfd_size_type
) -1
2145 || ! elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
2150 /* Find all symbols which were defined in a dynamic object and make
2151 the backend pick a reasonable value for them. */
2154 elf_link_hash_traverse (elf_hash_table (info
),
2155 elf_adjust_dynamic_symbol
,
2160 /* Add some entries to the .dynamic section. We fill in some of the
2161 values later, in elf_bfd_final_link, but we must add the entries
2162 now so that we know the final size of the .dynamic section. */
2163 h
= elf_link_hash_lookup (elf_hash_table (info
), "_init", false,
2166 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2167 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2169 if (! elf_add_dynamic_entry (info
, DT_INIT
, 0))
2172 h
= elf_link_hash_lookup (elf_hash_table (info
), "_fini", false,
2175 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2176 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2178 if (! elf_add_dynamic_entry (info
, DT_FINI
, 0))
2181 strsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
2182 if (! elf_add_dynamic_entry (info
, DT_HASH
, 0)
2183 || ! elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
2184 || ! elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
2185 || ! elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
2186 || ! elf_add_dynamic_entry (info
, DT_SYMENT
,
2187 sizeof (Elf_External_Sym
)))
2191 /* The backend must work out the sizes of all the other dynamic
2193 old_dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2194 if (! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
2197 if (elf_hash_table (info
)->dynamic_sections_created
)
2202 size_t bucketcount
= 0;
2203 Elf_Internal_Sym isym
;
2204 struct elf_assign_sym_version_info sinfo
;
2206 /* Set up the version definition section. */
2207 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2208 BFD_ASSERT (s
!= NULL
);
2210 /* Attach all the symbols to their version information. This
2211 may cause some symbols to be unexported. */
2212 sinfo
.output_bfd
= output_bfd
;
2214 sinfo
.verdefs
= verdefs
;
2215 sinfo
.export_dynamic
= export_dynamic
;
2216 sinfo
.removed_dynamic
= false;
2217 sinfo
.failed
= false;
2219 elf_link_hash_traverse (elf_hash_table (info
),
2220 elf_link_assign_sym_version
,
2225 /* We may have created additional version definitions if we are
2226 just linking a regular application. */
2227 verdefs
= sinfo
.verdefs
;
2229 if (verdefs
== NULL
)
2233 /* Don't include this section in the output file. */
2234 for (spp
= &output_bfd
->sections
;
2235 *spp
!= s
->output_section
;
2236 spp
= &(*spp
)->next
)
2238 *spp
= s
->output_section
->next
;
2239 --output_bfd
->section_count
;
2245 struct bfd_elf_version_tree
*t
;
2247 Elf_Internal_Verdef def
;
2248 Elf_Internal_Verdaux defaux
;
2250 if (sinfo
.removed_dynamic
)
2252 /* Some dynamic symbols were changed to be local
2253 symbols. In this case, we renumber all of the
2254 dynamic symbols, so that we don't have a hole. If
2255 the backend changed dynsymcount, then assume that the
2256 new symbols are at the start. This is the case on
2257 the MIPS. FIXME: The names of the removed symbols
2258 will still be in the dynamic string table, wasting
2260 elf_hash_table (info
)->dynsymcount
=
2261 1 + (elf_hash_table (info
)->dynsymcount
- old_dynsymcount
);
2262 elf_link_hash_traverse (elf_hash_table (info
),
2263 elf_link_renumber_dynsyms
,
2270 /* Make space for the base version. */
2271 size
+= sizeof (Elf_External_Verdef
);
2272 size
+= sizeof (Elf_External_Verdaux
);
2275 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2277 struct bfd_elf_version_deps
*n
;
2279 size
+= sizeof (Elf_External_Verdef
);
2280 size
+= sizeof (Elf_External_Verdaux
);
2283 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2284 size
+= sizeof (Elf_External_Verdaux
);
2287 s
->_raw_size
= size
;
2288 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2289 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2292 /* Fill in the version definition section. */
2296 def
.vd_version
= VER_DEF_CURRENT
;
2297 def
.vd_flags
= VER_FLG_BASE
;
2300 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2301 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2302 + sizeof (Elf_External_Verdaux
));
2304 if (soname_indx
!= -1)
2306 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) soname
);
2307 defaux
.vda_name
= soname_indx
;
2314 name
= output_bfd
->filename
;
2315 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) name
);
2316 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2318 if (indx
== (bfd_size_type
) -1)
2320 defaux
.vda_name
= indx
;
2322 defaux
.vda_next
= 0;
2324 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2325 (Elf_External_Verdef
*)p
);
2326 p
+= sizeof (Elf_External_Verdef
);
2327 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2328 (Elf_External_Verdaux
*) p
);
2329 p
+= sizeof (Elf_External_Verdaux
);
2331 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2334 struct bfd_elf_version_deps
*n
;
2335 struct elf_link_hash_entry
*h
;
2338 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2341 /* Add a symbol representing this version. */
2343 if (! (_bfd_generic_link_add_one_symbol
2344 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
2345 (bfd_vma
) 0, (const char *) NULL
, false,
2346 get_elf_backend_data (dynobj
)->collect
,
2347 (struct bfd_link_hash_entry
**) &h
)))
2349 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
2350 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2351 h
->type
= STT_OBJECT
;
2352 h
->verinfo
.vertree
= t
;
2354 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2357 def
.vd_version
= VER_DEF_CURRENT
;
2359 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
2360 def
.vd_flags
|= VER_FLG_WEAK
;
2361 def
.vd_ndx
= t
->vernum
+ 1;
2362 def
.vd_cnt
= cdeps
+ 1;
2363 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) t
->name
);
2364 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2365 if (t
->next
!= NULL
)
2366 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2367 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
2371 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2372 (Elf_External_Verdef
*) p
);
2373 p
+= sizeof (Elf_External_Verdef
);
2375 defaux
.vda_name
= h
->dynstr_index
;
2376 if (t
->deps
== NULL
)
2377 defaux
.vda_next
= 0;
2379 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2380 t
->name_indx
= defaux
.vda_name
;
2382 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2383 (Elf_External_Verdaux
*) p
);
2384 p
+= sizeof (Elf_External_Verdaux
);
2386 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2388 defaux
.vda_name
= n
->version_needed
->name_indx
;
2389 if (n
->next
== NULL
)
2390 defaux
.vda_next
= 0;
2392 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2394 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2395 (Elf_External_Verdaux
*) p
);
2396 p
+= sizeof (Elf_External_Verdaux
);
2400 if (! elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
2401 || ! elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
2404 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
2407 /* Work out the size of the version reference section. */
2409 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2410 BFD_ASSERT (s
!= NULL
);
2412 struct elf_find_verdep_info sinfo
;
2414 sinfo
.output_bfd
= output_bfd
;
2416 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
2417 if (sinfo
.vers
== 0)
2419 sinfo
.failed
= false;
2421 elf_link_hash_traverse (elf_hash_table (info
),
2422 elf_link_find_version_dependencies
,
2425 if (elf_tdata (output_bfd
)->verref
== NULL
)
2429 /* We don't have any version definitions, so we can just
2430 remove the section. */
2432 for (spp
= &output_bfd
->sections
;
2433 *spp
!= s
->output_section
;
2434 spp
= &(*spp
)->next
)
2436 *spp
= s
->output_section
->next
;
2437 --output_bfd
->section_count
;
2441 Elf_Internal_Verneed
*t
;
2446 /* Build the version definition section. */
2449 for (t
= elf_tdata (output_bfd
)->verref
;
2453 Elf_Internal_Vernaux
*a
;
2455 size
+= sizeof (Elf_External_Verneed
);
2457 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2458 size
+= sizeof (Elf_External_Vernaux
);
2461 s
->_raw_size
= size
;
2462 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, size
);
2463 if (s
->contents
== NULL
)
2467 for (t
= elf_tdata (output_bfd
)->verref
;
2472 Elf_Internal_Vernaux
*a
;
2476 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2479 t
->vn_version
= VER_NEED_CURRENT
;
2481 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2482 t
->vn_bfd
->filename
, true, false);
2483 if (indx
== (bfd_size_type
) -1)
2486 t
->vn_aux
= sizeof (Elf_External_Verneed
);
2487 if (t
->vn_nextref
== NULL
)
2490 t
->vn_next
= (sizeof (Elf_External_Verneed
)
2491 + caux
* sizeof (Elf_External_Vernaux
));
2493 _bfd_elf_swap_verneed_out (output_bfd
, t
,
2494 (Elf_External_Verneed
*) p
);
2495 p
+= sizeof (Elf_External_Verneed
);
2497 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2499 a
->vna_hash
= bfd_elf_hash ((const unsigned char *)
2501 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2502 a
->vna_nodename
, true, false);
2503 if (indx
== (bfd_size_type
) -1)
2506 if (a
->vna_nextptr
== NULL
)
2509 a
->vna_next
= sizeof (Elf_External_Vernaux
);
2511 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
2512 (Elf_External_Vernaux
*) p
);
2513 p
+= sizeof (Elf_External_Vernaux
);
2517 if (! elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
2518 || ! elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
2521 elf_tdata (output_bfd
)->cverrefs
= crefs
;
2525 dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2527 /* Work out the size of the symbol version section. */
2528 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
2529 BFD_ASSERT (s
!= NULL
);
2530 if (dynsymcount
== 0
2531 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
2535 /* We don't need any symbol versions; just discard the
2537 for (spp
= &output_bfd
->sections
;
2538 *spp
!= s
->output_section
;
2539 spp
= &(*spp
)->next
)
2541 *spp
= s
->output_section
->next
;
2542 --output_bfd
->section_count
;
2546 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
2547 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
2548 if (s
->contents
== NULL
)
2551 if (! elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
2555 /* Set the size of the .dynsym and .hash sections. We counted
2556 the number of dynamic symbols in elf_link_add_object_symbols.
2557 We will build the contents of .dynsym and .hash when we build
2558 the final symbol table, because until then we do not know the
2559 correct value to give the symbols. We built the .dynstr
2560 section as we went along in elf_link_add_object_symbols. */
2561 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
2562 BFD_ASSERT (s
!= NULL
);
2563 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
2564 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2565 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2568 /* The first entry in .dynsym is a dummy symbol. */
2575 elf_swap_symbol_out (output_bfd
, &isym
,
2576 (PTR
) (Elf_External_Sym
*) s
->contents
);
2578 for (i
= 0; elf_buckets
[i
] != 0; i
++)
2580 bucketcount
= elf_buckets
[i
];
2581 if (dynsymcount
< elf_buckets
[i
+ 1])
2585 s
= bfd_get_section_by_name (dynobj
, ".hash");
2586 BFD_ASSERT (s
!= NULL
);
2587 s
->_raw_size
= (2 + bucketcount
+ dynsymcount
) * (ARCH_SIZE
/ 8);
2588 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2589 if (s
->contents
== NULL
)
2591 memset (s
->contents
, 0, (size_t) s
->_raw_size
);
2593 put_word (output_bfd
, bucketcount
, s
->contents
);
2594 put_word (output_bfd
, dynsymcount
, s
->contents
+ (ARCH_SIZE
/ 8));
2596 elf_hash_table (info
)->bucketcount
= bucketcount
;
2598 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
2599 BFD_ASSERT (s
!= NULL
);
2600 s
->_raw_size
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
2602 if (! elf_add_dynamic_entry (info
, DT_NULL
, 0))
2609 /* Make the backend pick a good value for a dynamic symbol. This is
2610 called via elf_link_hash_traverse, and also calls itself
2614 elf_adjust_dynamic_symbol (h
, data
)
2615 struct elf_link_hash_entry
*h
;
2618 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2620 struct elf_backend_data
*bed
;
2622 /* Ignore indirect symbols. These are added by the versioning code. */
2623 if (h
->root
.type
== bfd_link_hash_indirect
)
2626 /* If this symbol was mentioned in a non-ELF file, try to set
2627 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2628 permit a non-ELF file to correctly refer to a symbol defined in
2629 an ELF dynamic object. */
2630 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
2632 if (h
->root
.type
!= bfd_link_hash_defined
2633 && h
->root
.type
!= bfd_link_hash_defweak
)
2634 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2637 if (h
->root
.u
.def
.section
->owner
!= NULL
2638 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2639 == bfd_target_elf_flavour
))
2640 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2642 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2645 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2646 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
2648 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2656 /* If this is a final link, and the symbol was defined as a common
2657 symbol in a regular object file, and there was no definition in
2658 any dynamic object, then the linker will have allocated space for
2659 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2660 flag will not have been set. */
2661 if (h
->root
.type
== bfd_link_hash_defined
2662 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2663 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
2664 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2665 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2666 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2668 /* If -Bsymbolic was used (which means to bind references to global
2669 symbols to the definition within the shared object), and this
2670 symbol was defined in a regular object, then it actually doesn't
2671 need a PLT entry. */
2672 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
2673 && eif
->info
->shared
2674 && eif
->info
->symbolic
2675 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2676 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
2678 /* If this symbol does not require a PLT entry, and it is not
2679 defined by a dynamic object, or is not referenced by a regular
2680 object, ignore it. We do have to handle a weak defined symbol,
2681 even if no regular object refers to it, if we decided to add it
2682 to the dynamic symbol table. FIXME: Do we normally need to worry
2683 about symbols which are defined by one dynamic object and
2684 referenced by another one? */
2685 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
2686 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
2687 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2688 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
2689 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
2692 /* If we've already adjusted this symbol, don't do it again. This
2693 can happen via a recursive call. */
2694 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
2697 /* Don't look at this symbol again. Note that we must set this
2698 after checking the above conditions, because we may look at a
2699 symbol once, decide not to do anything, and then get called
2700 recursively later after REF_REGULAR is set below. */
2701 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
2703 /* If this is a weak definition, and we know a real definition, and
2704 the real symbol is not itself defined by a regular object file,
2705 then get a good value for the real definition. We handle the
2706 real symbol first, for the convenience of the backend routine.
2708 Note that there is a confusing case here. If the real definition
2709 is defined by a regular object file, we don't get the real symbol
2710 from the dynamic object, but we do get the weak symbol. If the
2711 processor backend uses a COPY reloc, then if some routine in the
2712 dynamic object changes the real symbol, we will not see that
2713 change in the corresponding weak symbol. This is the way other
2714 ELF linkers work as well, and seems to be a result of the shared
2717 I will clarify this issue. Most SVR4 shared libraries define the
2718 variable _timezone and define timezone as a weak synonym. The
2719 tzset call changes _timezone. If you write
2720 extern int timezone;
2722 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2723 you might expect that, since timezone is a synonym for _timezone,
2724 the same number will print both times. However, if the processor
2725 backend uses a COPY reloc, then actually timezone will be copied
2726 into your process image, and, since you define _timezone
2727 yourself, _timezone will not. Thus timezone and _timezone will
2728 wind up at different memory locations. The tzset call will set
2729 _timezone, leaving timezone unchanged. */
2731 if (h
->weakdef
!= NULL
)
2733 struct elf_link_hash_entry
*weakdef
;
2735 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2736 || h
->root
.type
== bfd_link_hash_defweak
);
2737 weakdef
= h
->weakdef
;
2738 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2739 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2740 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
2741 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2743 /* This symbol is defined by a regular object file, so we
2744 will not do anything special. Clear weakdef for the
2745 convenience of the processor backend. */
2750 /* There is an implicit reference by a regular object file
2751 via the weak symbol. */
2752 weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2753 if (! elf_adjust_dynamic_symbol (weakdef
, (PTR
) eif
))
2758 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2759 bed
= get_elf_backend_data (dynobj
);
2760 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2769 /* This routine is used to export all defined symbols into the dynamic
2770 symbol table. It is called via elf_link_hash_traverse. */
2773 elf_export_symbol (h
, data
)
2774 struct elf_link_hash_entry
*h
;
2777 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2779 /* Ignore indirect symbols. These are added by the versioning code. */
2780 if (h
->root
.type
== bfd_link_hash_indirect
)
2783 if (h
->dynindx
== -1
2784 && (h
->elf_link_hash_flags
2785 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
2787 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2797 /* Look through the symbols which are defined in other shared
2798 libraries and referenced here. Update the list of version
2799 dependencies. This will be put into the .gnu.version_r section.
2800 This function is called via elf_link_hash_traverse. */
2803 elf_link_find_version_dependencies (h
, data
)
2804 struct elf_link_hash_entry
*h
;
2807 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2808 Elf_Internal_Verneed
*t
;
2809 Elf_Internal_Vernaux
*a
;
2811 /* We only care about symbols defined in shared objects with version
2813 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2814 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
2816 || h
->verinfo
.verdef
== NULL
)
2819 /* See if we already know about this version. */
2820 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
2822 if (t
->vn_bfd
== h
->verinfo
.verdef
->vd_bfd
)
2825 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2826 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2832 /* This is a new version. Add it to tree we are building. */
2836 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *t
);
2839 rinfo
->failed
= true;
2843 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2844 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
2845 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
2848 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *a
);
2850 /* Note that we are copying a string pointer here, and testing it
2851 above. If bfd_elf_string_from_elf_section is ever changed to
2852 discard the string data when low in memory, this will have to be
2854 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2856 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2857 a
->vna_nextptr
= t
->vn_auxptr
;
2859 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2862 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2869 /* Figure out appropriate versions for all the symbols. We may not
2870 have the version number script until we have read all of the input
2871 files, so until that point we don't know which symbols should be
2872 local. This function is called via elf_link_hash_traverse. */
2875 elf_link_assign_sym_version (h
, data
)
2876 struct elf_link_hash_entry
*h
;
2879 struct elf_assign_sym_version_info
*sinfo
=
2880 (struct elf_assign_sym_version_info
*) data
;
2881 struct bfd_link_info
*info
= sinfo
->info
;
2884 /* We only need version numbers for symbols defined in regular
2886 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2889 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2890 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2892 struct bfd_elf_version_tree
*t
;
2897 /* There are two consecutive ELF_VER_CHR characters if this is
2898 not a hidden symbol. */
2900 if (*p
== ELF_VER_CHR
)
2906 /* If there is no version string, we can just return out. */
2910 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
2914 /* Look for the version. If we find it, it is no longer weak. */
2915 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
2917 if (strcmp (t
->name
, p
) == 0)
2919 h
->verinfo
.vertree
= t
;
2922 /* See if there is anything to force this symbol to
2924 if (t
->locals
!= NULL
)
2928 struct bfd_elf_version_expr
*d
;
2930 len
= p
- h
->root
.root
.string
;
2931 alc
= bfd_alloc (sinfo
->output_bfd
, len
);
2934 strncpy (alc
, h
->root
.root
.string
, len
- 1);
2935 alc
[len
- 1] = '\0';
2936 if (alc
[len
- 2] == ELF_VER_CHR
)
2937 alc
[len
- 2] = '\0';
2939 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
2941 if ((d
->match
[0] == '*' && d
->match
[1] == '\0')
2942 || fnmatch (d
->match
, alc
, 0) == 0)
2944 if (h
->dynindx
!= -1
2946 && ! sinfo
->export_dynamic
2947 && (h
->elf_link_hash_flags
2948 & ELF_LINK_HASH_NEEDS_PLT
) == 0)
2950 sinfo
->removed_dynamic
= true;
2951 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
2953 /* FIXME: The name of the symbol has
2954 already been recorded in the dynamic
2955 string table section. */
2962 bfd_release (sinfo
->output_bfd
, alc
);
2969 /* If we are building an application, we need to create a
2970 version node for this version. */
2971 if (t
== NULL
&& ! info
->shared
)
2973 struct bfd_elf_version_tree
**pp
;
2976 /* If we aren't going to export this symbol, we don't need
2977 to worry about it. */
2978 if (h
->dynindx
== -1)
2981 t
= ((struct bfd_elf_version_tree
*)
2982 bfd_alloc (sinfo
->output_bfd
, sizeof *t
));
2985 sinfo
->failed
= true;
2994 t
->name_indx
= (unsigned int) -1;
2998 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
3000 t
->vernum
= version_index
;
3004 h
->verinfo
.vertree
= t
;
3008 /* We could not find the version for a symbol when
3009 generating a shared archive. Return an error. */
3010 (*_bfd_error_handler
)
3011 ("%s: undefined version name %s",
3012 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
3013 bfd_set_error (bfd_error_bad_value
);
3014 sinfo
->failed
= true;
3019 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
3022 /* If we don't have a version for this symbol, see if we can find
3024 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
3026 struct bfd_elf_version_tree
*t
;
3027 struct bfd_elf_version_tree
*deflt
;
3028 struct bfd_elf_version_expr
*d
;
3030 /* See if can find what version this symbol is in. If the
3031 symbol is supposed to eb local, then don't actually register
3034 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
3036 if (t
->globals
!= NULL
)
3038 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3040 if (fnmatch (d
->match
, h
->root
.root
.string
, 0) == 0)
3042 h
->verinfo
.vertree
= t
;
3051 if (t
->locals
!= NULL
)
3053 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3055 if (d
->match
[0] == '*' && d
->match
[1] == '\0')
3057 else if (fnmatch (d
->match
, h
->root
.root
.string
, 0) == 0)
3059 h
->verinfo
.vertree
= t
;
3060 if (h
->dynindx
!= -1
3062 && ! sinfo
->export_dynamic
3063 && (h
->elf_link_hash_flags
3064 & ELF_LINK_HASH_NEEDS_PLT
) == 0)
3066 sinfo
->removed_dynamic
= true;
3067 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3069 /* FIXME: The name of the symbol has already
3070 been recorded in the dynamic string table
3082 if (deflt
!= NULL
&& h
->verinfo
.vertree
== NULL
)
3084 h
->verinfo
.vertree
= deflt
;
3085 if (h
->dynindx
!= -1
3087 && ! sinfo
->export_dynamic
3088 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
3090 sinfo
->removed_dynamic
= true;
3091 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3093 /* FIXME: The name of the symbol has already been
3094 recorded in the dynamic string table section. */
3102 /* This function is used to renumber the dynamic symbols, if some of
3103 them are removed because they are marked as local. This is called
3104 via elf_link_hash_traverse. */
3107 elf_link_renumber_dynsyms (h
, data
)
3108 struct elf_link_hash_entry
*h
;
3111 struct bfd_link_info
*info
= (struct bfd_link_info
*) data
;
3113 if (h
->dynindx
!= -1)
3115 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
3116 ++elf_hash_table (info
)->dynsymcount
;
3122 /* Final phase of ELF linker. */
3124 /* A structure we use to avoid passing large numbers of arguments. */
3126 struct elf_final_link_info
3128 /* General link information. */
3129 struct bfd_link_info
*info
;
3132 /* Symbol string table. */
3133 struct bfd_strtab_hash
*symstrtab
;
3134 /* .dynsym section. */
3135 asection
*dynsym_sec
;
3136 /* .hash section. */
3138 /* symbol version section (.gnu.version). */
3139 asection
*symver_sec
;
3140 /* Buffer large enough to hold contents of any section. */
3142 /* Buffer large enough to hold external relocs of any section. */
3143 PTR external_relocs
;
3144 /* Buffer large enough to hold internal relocs of any section. */
3145 Elf_Internal_Rela
*internal_relocs
;
3146 /* Buffer large enough to hold external local symbols of any input
3148 Elf_External_Sym
*external_syms
;
3149 /* Buffer large enough to hold internal local symbols of any input
3151 Elf_Internal_Sym
*internal_syms
;
3152 /* Array large enough to hold a symbol index for each local symbol
3153 of any input BFD. */
3155 /* Array large enough to hold a section pointer for each local
3156 symbol of any input BFD. */
3157 asection
**sections
;
3158 /* Buffer to hold swapped out symbols. */
3159 Elf_External_Sym
*symbuf
;
3160 /* Number of swapped out symbols in buffer. */
3161 size_t symbuf_count
;
3162 /* Number of symbols which fit in symbuf. */
3166 static boolean elf_link_output_sym
3167 PARAMS ((struct elf_final_link_info
*, const char *,
3168 Elf_Internal_Sym
*, asection
*));
3169 static boolean elf_link_flush_output_syms
3170 PARAMS ((struct elf_final_link_info
*));
3171 static boolean elf_link_output_extsym
3172 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3173 static boolean elf_link_input_bfd
3174 PARAMS ((struct elf_final_link_info
*, bfd
*));
3175 static boolean elf_reloc_link_order
3176 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
3177 struct bfd_link_order
*));
3179 /* This struct is used to pass information to elf_link_output_extsym. */
3181 struct elf_outext_info
3185 struct elf_final_link_info
*finfo
;
3188 /* Do the final step of an ELF link. */
3191 elf_bfd_final_link (abfd
, info
)
3193 struct bfd_link_info
*info
;
3197 struct elf_final_link_info finfo
;
3198 register asection
*o
;
3199 register struct bfd_link_order
*p
;
3201 size_t max_contents_size
;
3202 size_t max_external_reloc_size
;
3203 size_t max_internal_reloc_count
;
3204 size_t max_sym_count
;
3206 Elf_Internal_Sym elfsym
;
3208 Elf_Internal_Shdr
*symtab_hdr
;
3209 Elf_Internal_Shdr
*symstrtab_hdr
;
3210 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3211 struct elf_outext_info eoinfo
;
3214 abfd
->flags
|= DYNAMIC
;
3216 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
3217 dynobj
= elf_hash_table (info
)->dynobj
;
3220 finfo
.output_bfd
= abfd
;
3221 finfo
.symstrtab
= elf_stringtab_init ();
3222 if (finfo
.symstrtab
== NULL
)
3227 finfo
.dynsym_sec
= NULL
;
3228 finfo
.hash_sec
= NULL
;
3229 finfo
.symver_sec
= NULL
;
3233 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
3234 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
3235 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
3236 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3237 /* Note that it is OK if symver_sec is NULL. */
3240 finfo
.contents
= NULL
;
3241 finfo
.external_relocs
= NULL
;
3242 finfo
.internal_relocs
= NULL
;
3243 finfo
.external_syms
= NULL
;
3244 finfo
.internal_syms
= NULL
;
3245 finfo
.indices
= NULL
;
3246 finfo
.sections
= NULL
;
3247 finfo
.symbuf
= NULL
;
3248 finfo
.symbuf_count
= 0;
3250 /* Count up the number of relocations we will output for each output
3251 section, so that we know the sizes of the reloc sections. We
3252 also figure out some maximum sizes. */
3253 max_contents_size
= 0;
3254 max_external_reloc_size
= 0;
3255 max_internal_reloc_count
= 0;
3257 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3261 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3263 if (p
->type
== bfd_section_reloc_link_order
3264 || p
->type
== bfd_symbol_reloc_link_order
)
3266 else if (p
->type
== bfd_indirect_link_order
)
3270 sec
= p
->u
.indirect
.section
;
3272 /* Mark all sections which are to be included in the
3273 link. This will normally be every section. We need
3274 to do this so that we can identify any sections which
3275 the linker has decided to not include. */
3276 sec
->linker_mark
= true;
3278 if (info
->relocateable
)
3279 o
->reloc_count
+= sec
->reloc_count
;
3281 if (sec
->_raw_size
> max_contents_size
)
3282 max_contents_size
= sec
->_raw_size
;
3283 if (sec
->_cooked_size
> max_contents_size
)
3284 max_contents_size
= sec
->_cooked_size
;
3286 /* We are interested in just local symbols, not all
3288 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
3289 && (sec
->owner
->flags
& DYNAMIC
) == 0)
3293 if (elf_bad_symtab (sec
->owner
))
3294 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
3295 / sizeof (Elf_External_Sym
));
3297 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
3299 if (sym_count
> max_sym_count
)
3300 max_sym_count
= sym_count
;
3302 if ((sec
->flags
& SEC_RELOC
) != 0)
3306 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
3307 if (ext_size
> max_external_reloc_size
)
3308 max_external_reloc_size
= ext_size
;
3309 if (sec
->reloc_count
> max_internal_reloc_count
)
3310 max_internal_reloc_count
= sec
->reloc_count
;
3316 if (o
->reloc_count
> 0)
3317 o
->flags
|= SEC_RELOC
;
3320 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3321 set it (this is probably a bug) and if it is set
3322 assign_section_numbers will create a reloc section. */
3323 o
->flags
&=~ SEC_RELOC
;
3326 /* If the SEC_ALLOC flag is not set, force the section VMA to
3327 zero. This is done in elf_fake_sections as well, but forcing
3328 the VMA to 0 here will ensure that relocs against these
3329 sections are handled correctly. */
3330 if ((o
->flags
& SEC_ALLOC
) == 0
3331 && ! o
->user_set_vma
)
3335 /* Figure out the file positions for everything but the symbol table
3336 and the relocs. We set symcount to force assign_section_numbers
3337 to create a symbol table. */
3338 abfd
->symcount
= info
->strip
== strip_all
? 0 : 1;
3339 BFD_ASSERT (! abfd
->output_has_begun
);
3340 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
3343 /* That created the reloc sections. Set their sizes, and assign
3344 them file positions, and allocate some buffers. */
3345 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3347 if ((o
->flags
& SEC_RELOC
) != 0)
3349 Elf_Internal_Shdr
*rel_hdr
;
3350 register struct elf_link_hash_entry
**p
, **pend
;
3352 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
3354 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* o
->reloc_count
;
3356 /* The contents field must last into write_object_contents,
3357 so we allocate it with bfd_alloc rather than malloc. */
3358 rel_hdr
->contents
= (PTR
) bfd_alloc (abfd
, rel_hdr
->sh_size
);
3359 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
3362 p
= ((struct elf_link_hash_entry
**)
3363 bfd_malloc (o
->reloc_count
3364 * sizeof (struct elf_link_hash_entry
*)));
3365 if (p
== NULL
&& o
->reloc_count
!= 0)
3367 elf_section_data (o
)->rel_hashes
= p
;
3368 pend
= p
+ o
->reloc_count
;
3369 for (; p
< pend
; p
++)
3372 /* Use the reloc_count field as an index when outputting the
3378 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3380 /* We have now assigned file positions for all the sections except
3381 .symtab and .strtab. We start the .symtab section at the current
3382 file position, and write directly to it. We build the .strtab
3383 section in memory. */
3385 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3386 /* sh_name is set in prep_headers. */
3387 symtab_hdr
->sh_type
= SHT_SYMTAB
;
3388 symtab_hdr
->sh_flags
= 0;
3389 symtab_hdr
->sh_addr
= 0;
3390 symtab_hdr
->sh_size
= 0;
3391 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
3392 /* sh_link is set in assign_section_numbers. */
3393 /* sh_info is set below. */
3394 /* sh_offset is set just below. */
3395 symtab_hdr
->sh_addralign
= 4; /* FIXME: system dependent? */
3397 off
= elf_tdata (abfd
)->next_file_pos
;
3398 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
3400 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3401 incorrect. We do not yet know the size of the .symtab section.
3402 We correct next_file_pos below, after we do know the size. */
3404 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3405 continuously seeking to the right position in the file. */
3406 if (! info
->keep_memory
|| max_sym_count
< 20)
3407 finfo
.symbuf_size
= 20;
3409 finfo
.symbuf_size
= max_sym_count
;
3410 finfo
.symbuf
= ((Elf_External_Sym
*)
3411 bfd_malloc (finfo
.symbuf_size
* sizeof (Elf_External_Sym
)));
3412 if (finfo
.symbuf
== NULL
)
3415 /* Start writing out the symbol table. The first symbol is always a
3417 if (info
->strip
!= strip_all
|| info
->relocateable
)
3419 elfsym
.st_value
= 0;
3422 elfsym
.st_other
= 0;
3423 elfsym
.st_shndx
= SHN_UNDEF
;
3424 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
3425 &elfsym
, bfd_und_section_ptr
))
3430 /* Some standard ELF linkers do this, but we don't because it causes
3431 bootstrap comparison failures. */
3432 /* Output a file symbol for the output file as the second symbol.
3433 We output this even if we are discarding local symbols, although
3434 I'm not sure if this is correct. */
3435 elfsym
.st_value
= 0;
3437 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
3438 elfsym
.st_other
= 0;
3439 elfsym
.st_shndx
= SHN_ABS
;
3440 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
3441 &elfsym
, bfd_abs_section_ptr
))
3445 /* Output a symbol for each section. We output these even if we are
3446 discarding local symbols, since they are used for relocs. These
3447 symbols have no names. We store the index of each one in the
3448 index field of the section, so that we can find it again when
3449 outputting relocs. */
3450 if (info
->strip
!= strip_all
|| info
->relocateable
)
3453 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
3454 elfsym
.st_other
= 0;
3455 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
3457 o
= section_from_elf_index (abfd
, i
);
3459 o
->target_index
= abfd
->symcount
;
3460 elfsym
.st_shndx
= i
;
3461 if (info
->relocateable
|| o
== NULL
)
3462 elfsym
.st_value
= 0;
3464 elfsym
.st_value
= o
->vma
;
3465 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
3471 /* Allocate some memory to hold information read in from the input
3473 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
3474 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
3475 finfo
.internal_relocs
= ((Elf_Internal_Rela
*)
3476 bfd_malloc (max_internal_reloc_count
3477 * sizeof (Elf_Internal_Rela
)));
3478 finfo
.external_syms
= ((Elf_External_Sym
*)
3479 bfd_malloc (max_sym_count
3480 * sizeof (Elf_External_Sym
)));
3481 finfo
.internal_syms
= ((Elf_Internal_Sym
*)
3482 bfd_malloc (max_sym_count
3483 * sizeof (Elf_Internal_Sym
)));
3484 finfo
.indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
3485 finfo
.sections
= ((asection
**)
3486 bfd_malloc (max_sym_count
* sizeof (asection
*)));
3487 if ((finfo
.contents
== NULL
&& max_contents_size
!= 0)
3488 || (finfo
.external_relocs
== NULL
&& max_external_reloc_size
!= 0)
3489 || (finfo
.internal_relocs
== NULL
&& max_internal_reloc_count
!= 0)
3490 || (finfo
.external_syms
== NULL
&& max_sym_count
!= 0)
3491 || (finfo
.internal_syms
== NULL
&& max_sym_count
!= 0)
3492 || (finfo
.indices
== NULL
&& max_sym_count
!= 0)
3493 || (finfo
.sections
== NULL
&& max_sym_count
!= 0))
3496 /* Since ELF permits relocations to be against local symbols, we
3497 must have the local symbols available when we do the relocations.
3498 Since we would rather only read the local symbols once, and we
3499 would rather not keep them in memory, we handle all the
3500 relocations for a single input file at the same time.
3502 Unfortunately, there is no way to know the total number of local
3503 symbols until we have seen all of them, and the local symbol
3504 indices precede the global symbol indices. This means that when
3505 we are generating relocateable output, and we see a reloc against
3506 a global symbol, we can not know the symbol index until we have
3507 finished examining all the local symbols to see which ones we are
3508 going to output. To deal with this, we keep the relocations in
3509 memory, and don't output them until the end of the link. This is
3510 an unfortunate waste of memory, but I don't see a good way around
3511 it. Fortunately, it only happens when performing a relocateable
3512 link, which is not the common case. FIXME: If keep_memory is set
3513 we could write the relocs out and then read them again; I don't
3514 know how bad the memory loss will be. */
3516 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->next
)
3517 sub
->output_has_begun
= false;
3518 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3520 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3522 if (p
->type
== bfd_indirect_link_order
3523 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
3524 == bfd_target_elf_flavour
))
3526 sub
= p
->u
.indirect
.section
->owner
;
3527 if (! sub
->output_has_begun
)
3529 if (! elf_link_input_bfd (&finfo
, sub
))
3531 sub
->output_has_begun
= true;
3534 else if (p
->type
== bfd_section_reloc_link_order
3535 || p
->type
== bfd_symbol_reloc_link_order
)
3537 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
3542 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
3548 /* That wrote out all the local symbols. Finish up the symbol table
3549 with the global symbols. */
3551 if (info
->strip
!= strip_all
&& info
->shared
)
3553 /* Output any global symbols that got converted to local in a
3554 version script. We do this in a separate step since ELF
3555 requires all local symbols to appear prior to any global
3556 symbols. FIXME: We should only do this if some global
3557 symbols were, in fact, converted to become local. FIXME:
3558 Will this work correctly with the Irix 5 linker? */
3559 eoinfo
.failed
= false;
3560 eoinfo
.finfo
= &finfo
;
3561 eoinfo
.localsyms
= true;
3562 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3568 /* The sh_info field records the index of the first non local
3570 symtab_hdr
->sh_info
= abfd
->symcount
;
3572 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
= 1;
3574 /* We get the global symbols from the hash table. */
3575 eoinfo
.failed
= false;
3576 eoinfo
.localsyms
= false;
3577 eoinfo
.finfo
= &finfo
;
3578 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3583 /* Flush all symbols to the file. */
3584 if (! elf_link_flush_output_syms (&finfo
))
3587 /* Now we know the size of the symtab section. */
3588 off
+= symtab_hdr
->sh_size
;
3590 /* Finish up and write out the symbol string table (.strtab)
3592 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3593 /* sh_name was set in prep_headers. */
3594 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
3595 symstrtab_hdr
->sh_flags
= 0;
3596 symstrtab_hdr
->sh_addr
= 0;
3597 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
3598 symstrtab_hdr
->sh_entsize
= 0;
3599 symstrtab_hdr
->sh_link
= 0;
3600 symstrtab_hdr
->sh_info
= 0;
3601 /* sh_offset is set just below. */
3602 symstrtab_hdr
->sh_addralign
= 1;
3604 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, true);
3605 elf_tdata (abfd
)->next_file_pos
= off
;
3607 if (abfd
->symcount
> 0)
3609 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
3610 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
3614 /* Adjust the relocs to have the correct symbol indices. */
3615 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3617 struct elf_link_hash_entry
**rel_hash
;
3618 Elf_Internal_Shdr
*rel_hdr
;
3620 if ((o
->flags
& SEC_RELOC
) == 0)
3623 rel_hash
= elf_section_data (o
)->rel_hashes
;
3624 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
3625 for (i
= 0; i
< o
->reloc_count
; i
++, rel_hash
++)
3627 if (*rel_hash
== NULL
)
3630 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
3632 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
3634 Elf_External_Rel
*erel
;
3635 Elf_Internal_Rel irel
;
3637 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
3638 elf_swap_reloc_in (abfd
, erel
, &irel
);
3639 irel
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
3640 ELF_R_TYPE (irel
.r_info
));
3641 elf_swap_reloc_out (abfd
, &irel
, erel
);
3645 Elf_External_Rela
*erela
;
3646 Elf_Internal_Rela irela
;
3648 BFD_ASSERT (rel_hdr
->sh_entsize
3649 == sizeof (Elf_External_Rela
));
3651 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
3652 elf_swap_reloca_in (abfd
, erela
, &irela
);
3653 irela
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
3654 ELF_R_TYPE (irela
.r_info
));
3655 elf_swap_reloca_out (abfd
, &irela
, erela
);
3659 /* Set the reloc_count field to 0 to prevent write_relocs from
3660 trying to swap the relocs out itself. */
3664 /* If we are linking against a dynamic object, or generating a
3665 shared library, finish up the dynamic linking information. */
3668 Elf_External_Dyn
*dyncon
, *dynconend
;
3670 /* Fix up .dynamic entries. */
3671 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
3672 BFD_ASSERT (o
!= NULL
);
3674 dyncon
= (Elf_External_Dyn
*) o
->contents
;
3675 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
3676 for (; dyncon
< dynconend
; dyncon
++)
3678 Elf_Internal_Dyn dyn
;
3682 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
3689 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on
3690 magic _init and _fini symbols. This is pretty ugly,
3691 but we are compatible. */
3699 struct elf_link_hash_entry
*h
;
3701 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
3702 false, false, true);
3704 && (h
->root
.type
== bfd_link_hash_defined
3705 || h
->root
.type
== bfd_link_hash_defweak
))
3707 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
3708 o
= h
->root
.u
.def
.section
;
3709 if (o
->output_section
!= NULL
)
3710 dyn
.d_un
.d_val
+= (o
->output_section
->vma
3711 + o
->output_offset
);
3714 /* The symbol is imported from another shared
3715 library and does not apply to this one. */
3719 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3734 name
= ".gnu.version_d";
3737 name
= ".gnu.version_r";
3740 name
= ".gnu.version";
3742 o
= bfd_get_section_by_name (abfd
, name
);
3743 BFD_ASSERT (o
!= NULL
);
3744 dyn
.d_un
.d_ptr
= o
->vma
;
3745 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3752 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
3757 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
3759 Elf_Internal_Shdr
*hdr
;
3761 hdr
= elf_elfsections (abfd
)[i
];
3762 if (hdr
->sh_type
== type
3763 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
3765 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
3766 dyn
.d_un
.d_val
+= hdr
->sh_size
;
3769 if (dyn
.d_un
.d_val
== 0
3770 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
3771 dyn
.d_un
.d_val
= hdr
->sh_addr
;
3775 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3781 /* If we have created any dynamic sections, then output them. */
3784 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
3787 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
3789 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
3790 || o
->_raw_size
== 0)
3792 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
3794 /* At this point, we are only interested in sections
3795 created by elf_link_create_dynamic_sections. */
3798 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
3800 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
3802 if (! bfd_set_section_contents (abfd
, o
->output_section
,
3803 o
->contents
, o
->output_offset
,
3811 /* The contents of the .dynstr section are actually in a
3813 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
3814 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
3815 || ! _bfd_stringtab_emit (abfd
,
3816 elf_hash_table (info
)->dynstr
))
3822 /* If we have optimized stabs strings, output them. */
3823 if (elf_hash_table (info
)->stab_info
!= NULL
)
3825 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
3829 if (finfo
.symstrtab
!= NULL
)
3830 _bfd_stringtab_free (finfo
.symstrtab
);
3831 if (finfo
.contents
!= NULL
)
3832 free (finfo
.contents
);
3833 if (finfo
.external_relocs
!= NULL
)
3834 free (finfo
.external_relocs
);
3835 if (finfo
.internal_relocs
!= NULL
)
3836 free (finfo
.internal_relocs
);
3837 if (finfo
.external_syms
!= NULL
)
3838 free (finfo
.external_syms
);
3839 if (finfo
.internal_syms
!= NULL
)
3840 free (finfo
.internal_syms
);
3841 if (finfo
.indices
!= NULL
)
3842 free (finfo
.indices
);
3843 if (finfo
.sections
!= NULL
)
3844 free (finfo
.sections
);
3845 if (finfo
.symbuf
!= NULL
)
3846 free (finfo
.symbuf
);
3847 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3849 if ((o
->flags
& SEC_RELOC
) != 0
3850 && elf_section_data (o
)->rel_hashes
!= NULL
)
3851 free (elf_section_data (o
)->rel_hashes
);
3854 elf_tdata (abfd
)->linker
= true;
3859 if (finfo
.symstrtab
!= NULL
)
3860 _bfd_stringtab_free (finfo
.symstrtab
);
3861 if (finfo
.contents
!= NULL
)
3862 free (finfo
.contents
);
3863 if (finfo
.external_relocs
!= NULL
)
3864 free (finfo
.external_relocs
);
3865 if (finfo
.internal_relocs
!= NULL
)
3866 free (finfo
.internal_relocs
);
3867 if (finfo
.external_syms
!= NULL
)
3868 free (finfo
.external_syms
);
3869 if (finfo
.internal_syms
!= NULL
)
3870 free (finfo
.internal_syms
);
3871 if (finfo
.indices
!= NULL
)
3872 free (finfo
.indices
);
3873 if (finfo
.sections
!= NULL
)
3874 free (finfo
.sections
);
3875 if (finfo
.symbuf
!= NULL
)
3876 free (finfo
.symbuf
);
3877 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3879 if ((o
->flags
& SEC_RELOC
) != 0
3880 && elf_section_data (o
)->rel_hashes
!= NULL
)
3881 free (elf_section_data (o
)->rel_hashes
);
3887 /* Add a symbol to the output symbol table. */
3890 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
3891 struct elf_final_link_info
*finfo
;
3893 Elf_Internal_Sym
*elfsym
;
3894 asection
*input_sec
;
3896 boolean (*output_symbol_hook
) PARAMS ((bfd
*,
3897 struct bfd_link_info
*info
,
3902 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
3903 elf_backend_link_output_symbol_hook
;
3904 if (output_symbol_hook
!= NULL
)
3906 if (! ((*output_symbol_hook
)
3907 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
3911 if (name
== (const char *) NULL
|| *name
== '\0')
3912 elfsym
->st_name
= 0;
3915 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
3918 if (elfsym
->st_name
== (unsigned long) -1)
3922 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
3924 if (! elf_link_flush_output_syms (finfo
))
3928 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
,
3929 (PTR
) (finfo
->symbuf
+ finfo
->symbuf_count
));
3930 ++finfo
->symbuf_count
;
3932 ++finfo
->output_bfd
->symcount
;
3937 /* Flush the output symbols to the file. */
3940 elf_link_flush_output_syms (finfo
)
3941 struct elf_final_link_info
*finfo
;
3943 if (finfo
->symbuf_count
> 0)
3945 Elf_Internal_Shdr
*symtab
;
3947 symtab
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
3949 if (bfd_seek (finfo
->output_bfd
, symtab
->sh_offset
+ symtab
->sh_size
,
3951 || (bfd_write ((PTR
) finfo
->symbuf
, finfo
->symbuf_count
,
3952 sizeof (Elf_External_Sym
), finfo
->output_bfd
)
3953 != finfo
->symbuf_count
* sizeof (Elf_External_Sym
)))
3956 symtab
->sh_size
+= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
3958 finfo
->symbuf_count
= 0;
3964 /* Add an external symbol to the symbol table. This is called from
3965 the hash table traversal routine. When generating a shared object,
3966 we go through the symbol table twice. The first time we output
3967 anything that might have been forced to local scope in a version
3968 script. The second time we output the symbols that are still
3972 elf_link_output_extsym (h
, data
)
3973 struct elf_link_hash_entry
*h
;
3976 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
3977 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
3979 Elf_Internal_Sym sym
;
3980 asection
*input_sec
;
3982 /* Decide whether to output this symbol in this pass. */
3983 if (eoinfo
->localsyms
)
3985 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
3990 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
3994 /* If we are not creating a shared library, and this symbol is
3995 referenced by a shared library but is not defined anywhere, then
3996 warn that it is undefined. If we do not do this, the runtime
3997 linker will complain that the symbol is undefined when the
3998 program is run. We don't have to worry about symbols that are
3999 referenced by regular files, because we will already have issued
4000 warnings for them. */
4001 if (! finfo
->info
->relocateable
4002 && ! finfo
->info
->shared
4003 && h
->root
.type
== bfd_link_hash_undefined
4004 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
4005 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
4007 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
4008 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
4009 (asection
*) NULL
, 0)))
4011 eoinfo
->failed
= true;
4016 /* We don't want to output symbols that have never been mentioned by
4017 a regular file, or that we have been told to strip. However, if
4018 h->indx is set to -2, the symbol is used by a reloc and we must
4022 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
4023 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
4024 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
4025 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
4027 else if (finfo
->info
->strip
== strip_all
4028 || (finfo
->info
->strip
== strip_some
4029 && bfd_hash_lookup (finfo
->info
->keep_hash
,
4030 h
->root
.root
.string
,
4031 false, false) == NULL
))
4036 /* If we're stripping it, and it's not a dynamic symbol, there's
4037 nothing else to do. */
4038 if (strip
&& h
->dynindx
== -1)
4042 sym
.st_size
= h
->size
;
4043 sym
.st_other
= h
->other
;
4044 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4045 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
4046 else if (h
->root
.type
== bfd_link_hash_undefweak
4047 || h
->root
.type
== bfd_link_hash_defweak
)
4048 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
4050 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
4052 switch (h
->root
.type
)
4055 case bfd_link_hash_new
:
4059 case bfd_link_hash_undefined
:
4060 input_sec
= bfd_und_section_ptr
;
4061 sym
.st_shndx
= SHN_UNDEF
;
4064 case bfd_link_hash_undefweak
:
4065 input_sec
= bfd_und_section_ptr
;
4066 sym
.st_shndx
= SHN_UNDEF
;
4069 case bfd_link_hash_defined
:
4070 case bfd_link_hash_defweak
:
4072 input_sec
= h
->root
.u
.def
.section
;
4073 if (input_sec
->output_section
!= NULL
)
4076 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
4077 input_sec
->output_section
);
4078 if (sym
.st_shndx
== (unsigned short) -1)
4080 eoinfo
->failed
= true;
4084 /* ELF symbols in relocateable files are section relative,
4085 but in nonrelocateable files they are virtual
4087 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
4088 if (! finfo
->info
->relocateable
)
4089 sym
.st_value
+= input_sec
->output_section
->vma
;
4093 BFD_ASSERT (input_sec
->owner
== NULL
4094 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
4095 sym
.st_shndx
= SHN_UNDEF
;
4096 input_sec
= bfd_und_section_ptr
;
4101 case bfd_link_hash_common
:
4102 input_sec
= bfd_com_section_ptr
;
4103 sym
.st_shndx
= SHN_COMMON
;
4104 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
4107 case bfd_link_hash_indirect
:
4108 /* These symbols are created by symbol versioning. They point
4109 to the decorated version of the name. For example, if the
4110 symbol foo@@GNU_1.2 is the default, which should be used when
4111 foo is used with no version, then we add an indirect symbol
4112 foo which points to foo@@GNU_1.2. We ignore these symbols,
4113 since the indirected symbol is already in the hash table. If
4114 the indirect symbol is non-ELF, fall through and output it. */
4115 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) == 0)
4119 case bfd_link_hash_warning
:
4120 /* We can't represent these symbols in ELF, although a warning
4121 symbol may have come from a .gnu.warning.SYMBOL section. We
4122 just put the target symbol in the hash table. If the target
4123 symbol does not really exist, don't do anything. */
4124 if (h
->root
.u
.i
.link
->type
== bfd_link_hash_new
)
4126 return (elf_link_output_extsym
4127 ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
));
4130 /* If this symbol should be put in the .dynsym section, then put it
4131 there now. We have already know the symbol index. We also fill
4132 in the entry in the .hash section. */
4133 if (h
->dynindx
!= -1
4134 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4136 struct elf_backend_data
*bed
;
4141 bfd_byte
*bucketpos
;
4144 sym
.st_name
= h
->dynstr_index
;
4146 /* Give the processor backend a chance to tweak the symbol
4147 value, and also to finish up anything that needs to be done
4149 bed
= get_elf_backend_data (finfo
->output_bfd
);
4150 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
4151 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
4153 eoinfo
->failed
= true;
4157 elf_swap_symbol_out (finfo
->output_bfd
, &sym
,
4158 (PTR
) (((Elf_External_Sym
*)
4159 finfo
->dynsym_sec
->contents
)
4162 /* We didn't include the version string in the dynamic string
4163 table, so we must not consider it in the hash table. */
4164 name
= h
->root
.root
.string
;
4165 p
= strchr (name
, ELF_VER_CHR
);
4170 copy
= bfd_alloc (finfo
->output_bfd
, p
- name
+ 1);
4171 strncpy (copy
, name
, p
- name
);
4172 copy
[p
- name
] = '\0';
4176 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
4177 bucket
= bfd_elf_hash ((const unsigned char *) name
) % bucketcount
;
4178 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
4179 + (bucket
+ 2) * (ARCH_SIZE
/ 8));
4180 chain
= get_word (finfo
->output_bfd
, bucketpos
);
4181 put_word (finfo
->output_bfd
, h
->dynindx
, bucketpos
);
4182 put_word (finfo
->output_bfd
, chain
,
4183 ((bfd_byte
*) finfo
->hash_sec
->contents
4184 + (bucketcount
+ 2 + h
->dynindx
) * (ARCH_SIZE
/ 8)));
4187 bfd_release (finfo
->output_bfd
, copy
);
4189 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
4191 Elf_Internal_Versym iversym
;
4193 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4195 if (h
->verinfo
.verdef
== NULL
)
4196 iversym
.vs_vers
= 0;
4198 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4202 if (h
->verinfo
.vertree
== NULL
)
4203 iversym
.vs_vers
= 1;
4205 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
4208 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
4209 iversym
.vs_vers
|= VERSYM_HIDDEN
;
4211 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
,
4212 (((Elf_External_Versym
*)
4213 finfo
->symver_sec
->contents
)
4218 /* If we're stripping it, then it was just a dynamic symbol, and
4219 there's nothing else to do. */
4223 h
->indx
= finfo
->output_bfd
->symcount
;
4225 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
4227 eoinfo
->failed
= true;
4234 /* Link an input file into the linker output file. This function
4235 handles all the sections and relocations of the input file at once.
4236 This is so that we only have to read the local symbols once, and
4237 don't have to keep them in memory. */
4240 elf_link_input_bfd (finfo
, input_bfd
)
4241 struct elf_final_link_info
*finfo
;
4244 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
4245 bfd
*, asection
*, bfd_byte
*,
4246 Elf_Internal_Rela
*,
4247 Elf_Internal_Sym
*, asection
**));
4249 Elf_Internal_Shdr
*symtab_hdr
;
4252 Elf_External_Sym
*external_syms
;
4253 Elf_External_Sym
*esym
;
4254 Elf_External_Sym
*esymend
;
4255 Elf_Internal_Sym
*isym
;
4257 asection
**ppsection
;
4260 output_bfd
= finfo
->output_bfd
;
4262 get_elf_backend_data (output_bfd
)->elf_backend_relocate_section
;
4264 /* If this is a dynamic object, we don't want to do anything here:
4265 we don't want the local symbols, and we don't want the section
4267 if ((input_bfd
->flags
& DYNAMIC
) != 0)
4270 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
4271 if (elf_bad_symtab (input_bfd
))
4273 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
4278 locsymcount
= symtab_hdr
->sh_info
;
4279 extsymoff
= symtab_hdr
->sh_info
;
4282 /* Read the local symbols. */
4283 if (symtab_hdr
->contents
!= NULL
)
4284 external_syms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
4285 else if (locsymcount
== 0)
4286 external_syms
= NULL
;
4289 external_syms
= finfo
->external_syms
;
4290 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
4291 || (bfd_read (external_syms
, sizeof (Elf_External_Sym
),
4292 locsymcount
, input_bfd
)
4293 != locsymcount
* sizeof (Elf_External_Sym
)))
4297 /* Swap in the local symbols and write out the ones which we know
4298 are going into the output file. */
4299 esym
= external_syms
;
4300 esymend
= esym
+ locsymcount
;
4301 isym
= finfo
->internal_syms
;
4302 pindex
= finfo
->indices
;
4303 ppsection
= finfo
->sections
;
4304 for (; esym
< esymend
; esym
++, isym
++, pindex
++, ppsection
++)
4308 Elf_Internal_Sym osym
;
4310 elf_swap_symbol_in (input_bfd
, esym
, isym
);
4313 if (elf_bad_symtab (input_bfd
))
4315 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
4322 if (isym
->st_shndx
== SHN_UNDEF
)
4323 isec
= bfd_und_section_ptr
;
4324 else if (isym
->st_shndx
> 0 && isym
->st_shndx
< SHN_LORESERVE
)
4325 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
4326 else if (isym
->st_shndx
== SHN_ABS
)
4327 isec
= bfd_abs_section_ptr
;
4328 else if (isym
->st_shndx
== SHN_COMMON
)
4329 isec
= bfd_com_section_ptr
;
4338 /* Don't output the first, undefined, symbol. */
4339 if (esym
== external_syms
)
4342 /* If we are stripping all symbols, we don't want to output this
4344 if (finfo
->info
->strip
== strip_all
)
4347 /* We never output section symbols. Instead, we use the section
4348 symbol of the corresponding section in the output file. */
4349 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
4352 /* If we are discarding all local symbols, we don't want to
4353 output this one. If we are generating a relocateable output
4354 file, then some of the local symbols may be required by
4355 relocs; we output them below as we discover that they are
4357 if (finfo
->info
->discard
== discard_all
)
4360 /* If this symbol is defined in a section which we are
4361 discarding, we don't need to keep it, but note that
4362 linker_mark is only reliable for sections that have contents.
4363 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4364 as well as linker_mark. */
4365 if (isym
->st_shndx
> 0
4366 && isym
->st_shndx
< SHN_LORESERVE
4368 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
4369 || (! finfo
->info
->relocateable
4370 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
4373 /* Get the name of the symbol. */
4374 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
4379 /* See if we are discarding symbols with this name. */
4380 if ((finfo
->info
->strip
== strip_some
4381 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
4383 || (finfo
->info
->discard
== discard_l
4384 && bfd_is_local_label_name (input_bfd
, name
)))
4387 /* If we get here, we are going to output this symbol. */
4391 /* Adjust the section index for the output file. */
4392 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
4393 isec
->output_section
);
4394 if (osym
.st_shndx
== (unsigned short) -1)
4397 *pindex
= output_bfd
->symcount
;
4399 /* ELF symbols in relocateable files are section relative, but
4400 in executable files they are virtual addresses. Note that
4401 this code assumes that all ELF sections have an associated
4402 BFD section with a reasonable value for output_offset; below
4403 we assume that they also have a reasonable value for
4404 output_section. Any special sections must be set up to meet
4405 these requirements. */
4406 osym
.st_value
+= isec
->output_offset
;
4407 if (! finfo
->info
->relocateable
)
4408 osym
.st_value
+= isec
->output_section
->vma
;
4410 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
4414 /* Relocate the contents of each section. */
4415 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4419 if (! o
->linker_mark
)
4421 /* This section was omitted from the link. */
4425 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4426 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
4429 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
4431 /* Section was created by elf_link_create_dynamic_sections
4436 /* Get the contents of the section. They have been cached by a
4437 relaxation routine. Note that o is a section in an input
4438 file, so the contents field will not have been set by any of
4439 the routines which work on output files. */
4440 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
4441 contents
= elf_section_data (o
)->this_hdr
.contents
;
4444 contents
= finfo
->contents
;
4445 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
4446 (file_ptr
) 0, o
->_raw_size
))
4450 if ((o
->flags
& SEC_RELOC
) != 0)
4452 Elf_Internal_Rela
*internal_relocs
;
4454 /* Get the swapped relocs. */
4455 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
4456 (input_bfd
, o
, finfo
->external_relocs
,
4457 finfo
->internal_relocs
, false));
4458 if (internal_relocs
== NULL
4459 && o
->reloc_count
> 0)
4462 /* Relocate the section by invoking a back end routine.
4464 The back end routine is responsible for adjusting the
4465 section contents as necessary, and (if using Rela relocs
4466 and generating a relocateable output file) adjusting the
4467 reloc addend as necessary.
4469 The back end routine does not have to worry about setting
4470 the reloc address or the reloc symbol index.
4472 The back end routine is given a pointer to the swapped in
4473 internal symbols, and can access the hash table entries
4474 for the external symbols via elf_sym_hashes (input_bfd).
4476 When generating relocateable output, the back end routine
4477 must handle STB_LOCAL/STT_SECTION symbols specially. The
4478 output symbol is going to be a section symbol
4479 corresponding to the output section, which will require
4480 the addend to be adjusted. */
4482 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
4483 input_bfd
, o
, contents
,
4485 finfo
->internal_syms
,
4489 if (finfo
->info
->relocateable
)
4491 Elf_Internal_Rela
*irela
;
4492 Elf_Internal_Rela
*irelaend
;
4493 struct elf_link_hash_entry
**rel_hash
;
4494 Elf_Internal_Shdr
*input_rel_hdr
;
4495 Elf_Internal_Shdr
*output_rel_hdr
;
4497 /* Adjust the reloc addresses and symbol indices. */
4499 irela
= internal_relocs
;
4500 irelaend
= irela
+ o
->reloc_count
;
4501 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
4502 + o
->output_section
->reloc_count
);
4503 for (; irela
< irelaend
; irela
++, rel_hash
++)
4505 unsigned long r_symndx
;
4506 Elf_Internal_Sym
*isym
;
4509 irela
->r_offset
+= o
->output_offset
;
4511 r_symndx
= ELF_R_SYM (irela
->r_info
);
4516 if (r_symndx
>= locsymcount
4517 || (elf_bad_symtab (input_bfd
)
4518 && finfo
->sections
[r_symndx
] == NULL
))
4522 /* This is a reloc against a global symbol. We
4523 have not yet output all the local symbols, so
4524 we do not know the symbol index of any global
4525 symbol. We set the rel_hash entry for this
4526 reloc to point to the global hash table entry
4527 for this symbol. The symbol index is then
4528 set at the end of elf_bfd_final_link. */
4529 indx
= r_symndx
- extsymoff
;
4530 *rel_hash
= elf_sym_hashes (input_bfd
)[indx
];
4532 /* Setting the index to -2 tells
4533 elf_link_output_extsym that this symbol is
4535 BFD_ASSERT ((*rel_hash
)->indx
< 0);
4536 (*rel_hash
)->indx
= -2;
4541 /* This is a reloc against a local symbol. */
4544 isym
= finfo
->internal_syms
+ r_symndx
;
4545 sec
= finfo
->sections
[r_symndx
];
4546 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
4548 /* I suppose the backend ought to fill in the
4549 section of any STT_SECTION symbol against a
4550 processor specific section. If we have
4551 discarded a section, the output_section will
4552 be the absolute section. */
4554 && (bfd_is_abs_section (sec
)
4555 || (sec
->output_section
!= NULL
4556 && bfd_is_abs_section (sec
->output_section
))))
4558 else if (sec
== NULL
|| sec
->owner
== NULL
)
4560 bfd_set_error (bfd_error_bad_value
);
4565 r_symndx
= sec
->output_section
->target_index
;
4566 BFD_ASSERT (r_symndx
!= 0);
4571 if (finfo
->indices
[r_symndx
] == -1)
4577 if (finfo
->info
->strip
== strip_all
)
4579 /* You can't do ld -r -s. */
4580 bfd_set_error (bfd_error_invalid_operation
);
4584 /* This symbol was skipped earlier, but
4585 since it is needed by a reloc, we
4586 must output it now. */
4587 link
= symtab_hdr
->sh_link
;
4588 name
= bfd_elf_string_from_elf_section (input_bfd
,
4594 osec
= sec
->output_section
;
4596 _bfd_elf_section_from_bfd_section (output_bfd
,
4598 if (isym
->st_shndx
== (unsigned short) -1)
4601 isym
->st_value
+= sec
->output_offset
;
4602 if (! finfo
->info
->relocateable
)
4603 isym
->st_value
+= osec
->vma
;
4605 finfo
->indices
[r_symndx
] = output_bfd
->symcount
;
4607 if (! elf_link_output_sym (finfo
, name
, isym
, sec
))
4611 r_symndx
= finfo
->indices
[r_symndx
];
4614 irela
->r_info
= ELF_R_INFO (r_symndx
,
4615 ELF_R_TYPE (irela
->r_info
));
4618 /* Swap out the relocs. */
4619 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
4620 output_rel_hdr
= &elf_section_data (o
->output_section
)->rel_hdr
;
4621 BFD_ASSERT (output_rel_hdr
->sh_entsize
4622 == input_rel_hdr
->sh_entsize
);
4623 irela
= internal_relocs
;
4624 irelaend
= irela
+ o
->reloc_count
;
4625 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4627 Elf_External_Rel
*erel
;
4629 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
4630 + o
->output_section
->reloc_count
);
4631 for (; irela
< irelaend
; irela
++, erel
++)
4633 Elf_Internal_Rel irel
;
4635 irel
.r_offset
= irela
->r_offset
;
4636 irel
.r_info
= irela
->r_info
;
4637 BFD_ASSERT (irela
->r_addend
== 0);
4638 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
4643 Elf_External_Rela
*erela
;
4645 BFD_ASSERT (input_rel_hdr
->sh_entsize
4646 == sizeof (Elf_External_Rela
));
4647 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
4648 + o
->output_section
->reloc_count
);
4649 for (; irela
< irelaend
; irela
++, erela
++)
4650 elf_swap_reloca_out (output_bfd
, irela
, erela
);
4653 o
->output_section
->reloc_count
+= o
->reloc_count
;
4657 /* Write out the modified section contents. */
4658 if (elf_section_data (o
)->stab_info
== NULL
)
4660 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
4661 contents
, o
->output_offset
,
4662 (o
->_cooked_size
!= 0
4669 if (! (_bfd_write_section_stabs
4670 (output_bfd
, &elf_hash_table (finfo
->info
)->stab_info
,
4671 o
, &elf_section_data (o
)->stab_info
, contents
)))
4679 /* Generate a reloc when linking an ELF file. This is a reloc
4680 requested by the linker, and does come from any input file. This
4681 is used to build constructor and destructor tables when linking
4685 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
4687 struct bfd_link_info
*info
;
4688 asection
*output_section
;
4689 struct bfd_link_order
*link_order
;
4691 reloc_howto_type
*howto
;
4695 struct elf_link_hash_entry
**rel_hash_ptr
;
4696 Elf_Internal_Shdr
*rel_hdr
;
4698 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
4701 bfd_set_error (bfd_error_bad_value
);
4705 addend
= link_order
->u
.reloc
.p
->addend
;
4707 /* Figure out the symbol index. */
4708 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
4709 + output_section
->reloc_count
);
4710 if (link_order
->type
== bfd_section_reloc_link_order
)
4712 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
4713 BFD_ASSERT (indx
!= 0);
4714 *rel_hash_ptr
= NULL
;
4718 struct elf_link_hash_entry
*h
;
4720 /* Treat a reloc against a defined symbol as though it were
4721 actually against the section. */
4722 h
= ((struct elf_link_hash_entry
*)
4723 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
4724 link_order
->u
.reloc
.p
->u
.name
,
4725 false, false, true));
4727 && (h
->root
.type
== bfd_link_hash_defined
4728 || h
->root
.type
== bfd_link_hash_defweak
))
4732 section
= h
->root
.u
.def
.section
;
4733 indx
= section
->output_section
->target_index
;
4734 *rel_hash_ptr
= NULL
;
4735 /* It seems that we ought to add the symbol value to the
4736 addend here, but in practice it has already been added
4737 because it was passed to constructor_callback. */
4738 addend
+= section
->output_section
->vma
+ section
->output_offset
;
4742 /* Setting the index to -2 tells elf_link_output_extsym that
4743 this symbol is used by a reloc. */
4750 if (! ((*info
->callbacks
->unattached_reloc
)
4751 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
4752 (asection
*) NULL
, (bfd_vma
) 0)))
4758 /* If this is an inplace reloc, we must write the addend into the
4760 if (howto
->partial_inplace
&& addend
!= 0)
4763 bfd_reloc_status_type rstat
;
4767 size
= bfd_get_reloc_size (howto
);
4768 buf
= (bfd_byte
*) bfd_zmalloc (size
);
4769 if (buf
== (bfd_byte
*) NULL
)
4771 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
4777 case bfd_reloc_outofrange
:
4779 case bfd_reloc_overflow
:
4780 if (! ((*info
->callbacks
->reloc_overflow
)
4782 (link_order
->type
== bfd_section_reloc_link_order
4783 ? bfd_section_name (output_bfd
,
4784 link_order
->u
.reloc
.p
->u
.section
)
4785 : link_order
->u
.reloc
.p
->u
.name
),
4786 howto
->name
, addend
, (bfd
*) NULL
, (asection
*) NULL
,
4794 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
4795 (file_ptr
) link_order
->offset
, size
);
4801 /* The address of a reloc is relative to the section in a
4802 relocateable file, and is a virtual address in an executable
4804 offset
= link_order
->offset
;
4805 if (! info
->relocateable
)
4806 offset
+= output_section
->vma
;
4808 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
4810 if (rel_hdr
->sh_type
== SHT_REL
)
4812 Elf_Internal_Rel irel
;
4813 Elf_External_Rel
*erel
;
4815 irel
.r_offset
= offset
;
4816 irel
.r_info
= ELF_R_INFO (indx
, howto
->type
);
4817 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
4818 + output_section
->reloc_count
);
4819 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
4823 Elf_Internal_Rela irela
;
4824 Elf_External_Rela
*erela
;
4826 irela
.r_offset
= offset
;
4827 irela
.r_info
= ELF_R_INFO (indx
, howto
->type
);
4828 irela
.r_addend
= addend
;
4829 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
4830 + output_section
->reloc_count
);
4831 elf_swap_reloca_out (output_bfd
, &irela
, erela
);
4834 ++output_section
->reloc_count
;
4840 /* Allocate a pointer to live in a linker created section. */
4843 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
4845 struct bfd_link_info
*info
;
4846 elf_linker_section_t
*lsect
;
4847 struct elf_link_hash_entry
*h
;
4848 const Elf_Internal_Rela
*rel
;
4850 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
4851 elf_linker_section_pointers_t
*linker_section_ptr
;
4852 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);;
4854 BFD_ASSERT (lsect
!= NULL
);
4856 /* Is this a global symbol? */
4859 /* Has this symbol already been allocated, if so, our work is done */
4860 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
4865 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
4866 /* Make sure this symbol is output as a dynamic symbol. */
4867 if (h
->dynindx
== -1)
4869 if (! elf_link_record_dynamic_symbol (info
, h
))
4873 if (lsect
->rel_section
)
4874 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
4877 else /* Allocation of a pointer to a local symbol */
4879 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
4881 /* Allocate a table to hold the local symbols if first time */
4884 int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
4885 register unsigned int i
;
4887 ptr
= (elf_linker_section_pointers_t
**)
4888 bfd_alloc (abfd
, num_symbols
* sizeof (elf_linker_section_pointers_t
*));
4893 elf_local_ptr_offsets (abfd
) = ptr
;
4894 for (i
= 0; i
< num_symbols
; i
++)
4895 ptr
[i
] = (elf_linker_section_pointers_t
*)0;
4898 /* Has this symbol already been allocated, if so, our work is done */
4899 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
4904 ptr_linker_section_ptr
= &ptr
[r_symndx
];
4908 /* If we are generating a shared object, we need to
4909 output a R_<xxx>_RELATIVE reloc so that the
4910 dynamic linker can adjust this GOT entry. */
4911 BFD_ASSERT (lsect
->rel_section
!= NULL
);
4912 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
4916 /* Allocate space for a pointer in the linker section, and allocate a new pointer record
4917 from internal memory. */
4918 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
4919 linker_section_ptr
= (elf_linker_section_pointers_t
*)
4920 bfd_alloc (abfd
, sizeof (elf_linker_section_pointers_t
));
4922 if (!linker_section_ptr
)
4925 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
4926 linker_section_ptr
->addend
= rel
->r_addend
;
4927 linker_section_ptr
->which
= lsect
->which
;
4928 linker_section_ptr
->written_address_p
= false;
4929 *ptr_linker_section_ptr
= linker_section_ptr
;
4932 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
4934 linker_section_ptr
->offset
= lsect
->section
->_raw_size
- lsect
->hole_size
+ (ARCH_SIZE
/ 8);
4935 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
4936 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
4937 if (lsect
->sym_hash
) /* Bump up symbol value if needed */
4939 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
4941 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
4942 lsect
->sym_hash
->root
.root
.string
,
4943 (long)ARCH_SIZE
/ 8,
4944 (long)lsect
->sym_hash
->root
.u
.def
.value
);
4950 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
4952 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
4955 fprintf (stderr
, "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
4956 lsect
->name
, (long)linker_section_ptr
->offset
, (long)lsect
->section
->_raw_size
);
4964 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
4967 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
4970 /* Fill in the address for a pointer generated in alinker section. */
4973 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
, relocation
, rel
, relative_reloc
)
4976 struct bfd_link_info
*info
;
4977 elf_linker_section_t
*lsect
;
4978 struct elf_link_hash_entry
*h
;
4980 const Elf_Internal_Rela
*rel
;
4983 elf_linker_section_pointers_t
*linker_section_ptr
;
4985 BFD_ASSERT (lsect
!= NULL
);
4987 if (h
!= NULL
) /* global symbol */
4989 linker_section_ptr
= _bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
4993 BFD_ASSERT (linker_section_ptr
!= NULL
);
4995 if (! elf_hash_table (info
)->dynamic_sections_created
4998 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
5000 /* This is actually a static link, or it is a
5001 -Bsymbolic link and the symbol is defined
5002 locally. We must initialize this entry in the
5005 When doing a dynamic link, we create a .rela.<xxx>
5006 relocation entry to initialize the value. This
5007 is done in the finish_dynamic_symbol routine. */
5008 if (!linker_section_ptr
->written_address_p
)
5010 linker_section_ptr
->written_address_p
= true;
5011 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
5012 lsect
->section
->contents
+ linker_section_ptr
->offset
);
5016 else /* local symbol */
5018 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
5019 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
5020 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
5021 linker_section_ptr
= _bfd_elf_find_pointer_linker_section (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
5025 BFD_ASSERT (linker_section_ptr
!= NULL
);
5027 /* Write out pointer if it hasn't been rewritten out before */
5028 if (!linker_section_ptr
->written_address_p
)
5030 linker_section_ptr
->written_address_p
= true;
5031 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
5032 lsect
->section
->contents
+ linker_section_ptr
->offset
);
5036 asection
*srel
= lsect
->rel_section
;
5037 Elf_Internal_Rela outrel
;
5039 /* We need to generate a relative reloc for the dynamic linker. */
5041 lsect
->rel_section
= srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
5044 BFD_ASSERT (srel
!= NULL
);
5046 outrel
.r_offset
= (lsect
->section
->output_section
->vma
5047 + lsect
->section
->output_offset
5048 + linker_section_ptr
->offset
);
5049 outrel
.r_info
= ELF_R_INFO (0, relative_reloc
);
5050 outrel
.r_addend
= 0;
5051 elf_swap_reloca_out (output_bfd
, &outrel
,
5052 (((Elf_External_Rela
*)
5053 lsect
->section
->contents
)
5054 + lsect
->section
->reloc_count
));
5055 ++lsect
->section
->reloc_count
;
5060 relocation
= (lsect
->section
->output_offset
5061 + linker_section_ptr
->offset
5062 - lsect
->hole_offset
5063 - lsect
->sym_offset
);
5066 fprintf (stderr
, "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
5067 lsect
->name
, (long)relocation
, (long)relocation
);
5070 /* Subtract out the addend, because it will get added back in by the normal
5072 return relocation
- linker_section_ptr
->addend
;