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
;
699 elf_swap_symbol_in (abfd
, esym
, &sym
);
701 flags
= BSF_NO_FLAGS
;
703 value
= sym
.st_value
;
706 bind
= ELF_ST_BIND (sym
.st_info
);
707 if (bind
== STB_LOCAL
)
709 /* This should be impossible, since ELF requires that all
710 global symbols follow all local symbols, and that sh_info
711 point to the first global symbol. Unfortunatealy, Irix 5
715 else if (bind
== STB_GLOBAL
)
717 if (sym
.st_shndx
!= SHN_UNDEF
718 && sym
.st_shndx
!= SHN_COMMON
)
723 else if (bind
== STB_WEAK
)
727 /* Leave it up to the processor backend. */
730 if (sym
.st_shndx
== SHN_UNDEF
)
731 sec
= bfd_und_section_ptr
;
732 else if (sym
.st_shndx
> 0 && sym
.st_shndx
< SHN_LORESERVE
)
734 sec
= section_from_elf_index (abfd
, sym
.st_shndx
);
738 sec
= bfd_abs_section_ptr
;
740 else if (sym
.st_shndx
== SHN_ABS
)
741 sec
= bfd_abs_section_ptr
;
742 else if (sym
.st_shndx
== SHN_COMMON
)
744 sec
= bfd_com_section_ptr
;
745 /* What ELF calls the size we call the value. What ELF
746 calls the value we call the alignment. */
751 /* Leave it up to the processor backend. */
754 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
755 if (name
== (const char *) NULL
)
760 if (! (*add_symbol_hook
) (abfd
, info
, &sym
, &name
, &flags
, &sec
,
764 /* The hook function sets the name to NULL if this symbol
765 should be skipped for some reason. */
766 if (name
== (const char *) NULL
)
770 /* Sanity check that all possibilities were handled. */
771 if (sec
== (asection
*) NULL
)
773 bfd_set_error (bfd_error_bad_value
);
777 if (bfd_is_und_section (sec
)
778 || bfd_is_com_section (sec
))
783 size_change_ok
= false;
784 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
785 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
787 Elf_Internal_Versym iver
;
793 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
794 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
796 /* If this is a hidden symbol, or if it is not version
797 1, we append the version name to the symbol name.
798 However, we do not modify a non-hidden absolute
799 symbol, because it might be the version symbol
800 itself. FIXME: What if it isn't? */
801 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
802 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
808 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
810 (*_bfd_error_handler
)
811 ("%s: %s: invalid version %d (max %d)",
812 abfd
->filename
, name
, vernum
,
813 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
814 bfd_set_error (bfd_error_bad_value
);
818 verstr
= elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
822 namelen
= strlen (name
);
823 newlen
= namelen
+ strlen (verstr
) + 2;
824 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
827 newname
= (char *) bfd_alloc (abfd
, newlen
);
830 strcpy (newname
, name
);
831 p
= newname
+ namelen
;
833 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
841 /* We need to look up the symbol now in order to get some of
842 the dynamic object handling right. We pass the hash
843 table entry in to _bfd_generic_link_add_one_symbol so
844 that it does not have to look it up again. */
845 if (! bfd_is_und_section (sec
))
846 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
849 h
= ((struct elf_link_hash_entry
*)
850 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true,
856 if (h
->root
.type
== bfd_link_hash_new
)
857 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
859 while (h
->root
.type
== bfd_link_hash_indirect
860 || h
->root
.type
== bfd_link_hash_warning
)
861 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
863 /* It's OK to change the type if it used to be a weak
864 definition, or if the current definition is weak (and
865 hence might be ignored). */
866 if (h
->root
.type
== bfd_link_hash_defweak
867 || h
->root
.type
== bfd_link_hash_undefweak
869 type_change_ok
= true;
871 /* It's OK to change the size if it used to be a weak
872 definition, or if it used to be undefined, or if we will
873 be overriding an old definition. */
875 || h
->root
.type
== bfd_link_hash_undefined
)
876 size_change_ok
= true;
880 /* If we are looking at a dynamic object, and this is a
881 definition, we need to see if it has already been defined
882 by some other object. If it has, we want to use the
883 existing definition, and we do not want to report a
884 multiple symbol definition error; we do this by
885 clobbering sec to be bfd_und_section_ptr. We treat a
886 common symbol as a definition if the symbol in the shared
887 library is a function, since common symbols always
888 represent variables; this can cause confusion in
889 principle, but any such confusion would seem to indicate
890 an erroneous program or shared library. */
891 if (dynamic
&& definition
)
893 if (h
->root
.type
== bfd_link_hash_defined
894 || h
->root
.type
== bfd_link_hash_defweak
895 || (h
->root
.type
== bfd_link_hash_common
897 || ELF_ST_TYPE (sym
.st_info
) == STT_FUNC
)))
900 sec
= bfd_und_section_ptr
;
902 size_change_ok
= true;
903 if (h
->root
.type
== bfd_link_hash_common
)
904 type_change_ok
= true;
908 /* Similarly, if we are not looking at a dynamic object, and
909 we have a definition, we want to override any definition
910 we may have from a dynamic object. Symbols from regular
911 files always take precedence over symbols from dynamic
912 objects, even if they are defined after the dynamic
913 object in the link. */
916 || (bfd_is_com_section (sec
)
917 && (h
->root
.type
== bfd_link_hash_defweak
918 || h
->type
== STT_FUNC
)))
919 && (h
->root
.type
== bfd_link_hash_defined
920 || h
->root
.type
== bfd_link_hash_defweak
)
921 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
922 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) != 0)
925 /* Change the hash table entry to undefined, and let
926 _bfd_generic_link_add_one_symbol do the right thing
927 with the new definition. */
928 h
->root
.type
= bfd_link_hash_undefined
;
929 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
930 size_change_ok
= true;
931 if (bfd_is_com_section (sec
))
932 type_change_ok
= true;
934 /* This union may have been set to be non-NULL when this
935 symbol was seen in a dynamic object. We must force
936 the union to be NULL, so that it is correct for a
938 h
->verinfo
.vertree
= NULL
;
944 && (h
->verinfo
.verdef
== NULL
|| definition
))
945 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
948 if (! (_bfd_generic_link_add_one_symbol
949 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
950 false, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
954 while (h
->root
.type
== bfd_link_hash_indirect
955 || h
->root
.type
== bfd_link_hash_warning
)
956 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
962 && (flags
& BSF_WEAK
) != 0
963 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
964 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
965 && h
->weakdef
== NULL
)
967 /* Keep a list of all weak defined non function symbols from
968 a dynamic object, using the weakdef field. Later in this
969 function we will set the weakdef field to the correct
970 value. We only put non-function symbols from dynamic
971 objects on this list, because that happens to be the only
972 time we need to know the normal symbol corresponding to a
973 weak symbol, and the information is time consuming to
974 figure out. If the weakdef field is not already NULL,
975 then this symbol was already defined by some previous
976 dynamic object, and we will be using that previous
977 definition anyhow. */
984 /* Get the alignment of a common symbol. */
985 if (sym
.st_shndx
== SHN_COMMON
986 && h
->root
.type
== bfd_link_hash_common
)
987 h
->root
.u
.c
.p
->alignment_power
= bfd_log2 (sym
.st_value
);
989 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
995 /* Remember the symbol size and type. */
997 && (definition
|| h
->size
== 0))
999 if (h
->size
!= 0 && h
->size
!= sym
.st_size
&& ! size_change_ok
)
1000 (*_bfd_error_handler
)
1001 ("Warning: size of symbol `%s' was %lu, but in %s is %lu; using %lu",
1002 name
, (unsigned long) h
->size
,
1003 bfd_get_filename (abfd
), (unsigned long) sym
.st_size
,
1004 (h
->size
< sym
.st_size
1005 ? (unsigned long) sym
.st_size
1006 : (unsigned long) h
->size
));
1008 if (h
->size
< sym
.st_size
)
1009 h
->size
= sym
.st_size
;
1011 if (ELF_ST_TYPE (sym
.st_info
) != STT_NOTYPE
1012 && (definition
|| h
->type
== STT_NOTYPE
))
1014 if (h
->type
!= STT_NOTYPE
1015 && h
->type
!= ELF_ST_TYPE (sym
.st_info
)
1016 && ! type_change_ok
)
1017 (*_bfd_error_handler
)
1018 ("Warning: type of symbol `%s' changed from %d to %d in %s",
1019 name
, h
->type
, ELF_ST_TYPE (sym
.st_info
),
1020 bfd_get_filename (abfd
));
1022 h
->type
= ELF_ST_TYPE (sym
.st_info
);
1025 if (sym
.st_other
!= 0
1026 && (definition
|| h
->other
== 0))
1027 h
->other
= sym
.st_other
;
1029 /* Set a flag in the hash table entry indicating the type of
1030 reference or definition we just found. Keep a count of
1031 the number of dynamic symbols we find. A dynamic symbol
1032 is one which is referenced or defined by both a regular
1033 object and a shared object. */
1034 old_flags
= h
->elf_link_hash_flags
;
1039 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
1041 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
1043 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1044 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
1050 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
1052 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
1053 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
1054 | ELF_LINK_HASH_REF_REGULAR
)) != 0
1055 || (h
->weakdef
!= NULL
1057 && h
->weakdef
->dynindx
!= -1))
1061 h
->elf_link_hash_flags
|= new_flag
;
1063 /* If this symbol has a version, and it is the default
1064 version, we create an indirect symbol from the default
1065 name to the fully decorated name. This will cause
1066 external references which do not specify a version to be
1067 bound to this version of the symbol. */
1072 p
= strchr (name
, ELF_VER_CHR
);
1073 if (p
!= NULL
&& p
[1] == ELF_VER_CHR
)
1076 struct elf_link_hash_entry
*hold
;
1078 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1080 if (shortname
== NULL
)
1082 strncpy (shortname
, name
, p
- name
);
1083 shortname
[p
- name
] = '\0';
1085 /* First look to see if we have an existing symbol
1087 hold
= elf_link_hash_lookup (elf_hash_table (info
),
1088 shortname
, false, false,
1091 /* If we are looking at a normal object, and the
1092 symbol was seen in a shared object, clobber the
1093 definition in the shared object. */
1096 && (hold
->root
.type
== bfd_link_hash_defined
1097 || hold
->root
.type
== bfd_link_hash_defweak
)
1098 && (hold
->elf_link_hash_flags
1099 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1100 && ((hold
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
)
1103 /* Change the hash table entry to undefined, so
1104 that _bfd_generic_link_add_one_symbol will do
1106 hold
->root
.type
= bfd_link_hash_undefined
;
1107 hold
->root
.u
.undef
.abfd
=
1108 hold
->root
.u
.def
.section
->owner
;
1109 hold
->verinfo
.vertree
= NULL
;
1113 /* If we are looking at a shared object, and we have
1114 already seen this symbol defined elsewhere, then
1115 don't try to define it again. */
1118 && (hold
->root
.type
== bfd_link_hash_defined
1119 || hold
->root
.type
== bfd_link_hash_defweak
1120 || hold
->root
.type
== bfd_link_hash_indirect
1121 || (hold
->root
.type
== bfd_link_hash_common
1122 && (bind
== STB_WEAK
1123 || ELF_ST_TYPE (sym
.st_info
) == STT_FUNC
))))
1125 /* Don't add an indirect symbol. */
1129 struct elf_link_hash_entry
*hi
;
1132 if (! (_bfd_generic_link_add_one_symbol
1133 (info
, abfd
, shortname
, BSF_INDIRECT
,
1134 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1135 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1138 /* If there is a duplicate definition somewhere,
1139 then HI may not point to an indirect symbol.
1140 We will have reported an error to the user in
1143 if (hi
->root
.type
== bfd_link_hash_indirect
)
1145 hi
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
1147 /* If the symbol became indirect, then we
1148 assume that we have not seen a definition
1150 BFD_ASSERT ((hi
->elf_link_hash_flags
1151 & (ELF_LINK_HASH_DEF_DYNAMIC
1152 | ELF_LINK_HASH_DEF_REGULAR
))
1155 /* Copy down any references that we may have
1156 already seen to the symbol which just
1158 h
->elf_link_hash_flags
|=
1159 (hi
->elf_link_hash_flags
1160 & (ELF_LINK_HASH_REF_DYNAMIC
1161 | ELF_LINK_HASH_REF_REGULAR
));
1163 /* Copy over the global table offset entry.
1164 This may have been already set up by a
1165 check_relocs routine. */
1166 if (h
->got_offset
== (bfd_vma
) -1)
1168 h
->got_offset
= hi
->got_offset
;
1169 hi
->got_offset
= (bfd_vma
) -1;
1171 BFD_ASSERT (hi
->got_offset
== (bfd_vma
) -1);
1173 if (h
->dynindx
== -1)
1175 h
->dynindx
= hi
->dynindx
;
1176 h
->dynstr_index
= hi
->dynstr_index
;
1178 hi
->dynstr_index
= 0;
1180 BFD_ASSERT (hi
->dynindx
== -1);
1182 /* FIXME: There may be other information to
1183 copy over for particular targets. */
1185 /* See if the new flags lead us to realize
1186 that the symbol must be dynamic. */
1192 || ((hi
->elf_link_hash_flags
1193 & ELF_LINK_HASH_REF_DYNAMIC
)
1199 if ((hi
->elf_link_hash_flags
1200 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1207 /* We also need to define an indirection from the
1208 nondefault version of the symbol. */
1210 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1212 if (shortname
== NULL
)
1214 strncpy (shortname
, name
, p
- name
);
1215 strcpy (shortname
+ (p
- name
), p
+ 1);
1217 /* First look to see if we have an existing symbol
1219 hold
= elf_link_hash_lookup (elf_hash_table (info
),
1220 shortname
, false, false,
1223 /* If we are looking at a normal object, and the
1224 symbol was seen in a shared object, clobber the
1225 definition in the shared object. */
1228 && (hold
->root
.type
== bfd_link_hash_defined
1229 || hold
->root
.type
== bfd_link_hash_defweak
)
1230 && (hold
->elf_link_hash_flags
1231 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1232 && ((hold
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
)
1235 /* Change the hash table entry to undefined, so
1236 that _bfd_generic_link_add_one_symbol will do
1238 hold
->root
.type
= bfd_link_hash_undefined
;
1239 hold
->root
.u
.undef
.abfd
=
1240 hold
->root
.u
.def
.section
->owner
;
1241 hold
->verinfo
.vertree
= NULL
;
1245 /* If we are looking at a shared object, and we have
1246 already seen this symbol defined elsewhere, then
1247 don't try to define it again. */
1250 && (hold
->root
.type
== bfd_link_hash_defined
1251 || hold
->root
.type
== bfd_link_hash_defweak
1252 || hold
->root
.type
== bfd_link_hash_indirect
1253 || (hold
->root
.type
== bfd_link_hash_common
1254 && (bind
== STB_WEAK
1255 || ELF_ST_TYPE (sym
.st_info
) == STT_FUNC
))))
1257 /* Don't add an indirect symbol. */
1261 struct elf_link_hash_entry
*hi
;
1264 if (! (_bfd_generic_link_add_one_symbol
1265 (info
, abfd
, shortname
, BSF_INDIRECT
,
1266 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1267 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1270 /* If there is a duplicate definition somewhere,
1271 then HI may not point to an indirect symbol.
1272 We will have reported an error to the user in
1275 if (hi
->root
.type
== bfd_link_hash_indirect
)
1277 hi
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
1279 /* If the symbol became indirect, then we
1280 assume that we have not seen a definition
1282 BFD_ASSERT ((hi
->elf_link_hash_flags
1283 & (ELF_LINK_HASH_DEF_DYNAMIC
1284 | ELF_LINK_HASH_DEF_REGULAR
))
1287 /* Copy down any references that we may have
1288 already seen to the symbol which just
1290 h
->elf_link_hash_flags
|=
1291 (hi
->elf_link_hash_flags
1292 & (ELF_LINK_HASH_REF_DYNAMIC
1293 | ELF_LINK_HASH_REF_REGULAR
));
1295 /* Copy over the global table offset entry.
1296 This may have been already set up by a
1297 check_relocs routine. */
1298 if (h
->got_offset
== (bfd_vma
) -1)
1300 h
->got_offset
= hi
->got_offset
;
1301 hi
->got_offset
= (bfd_vma
) -1;
1303 BFD_ASSERT (hi
->got_offset
== (bfd_vma
) -1);
1305 if (h
->dynindx
== -1)
1307 h
->dynindx
= hi
->dynindx
;
1308 h
->dynstr_index
= hi
->dynstr_index
;
1310 hi
->dynstr_index
= 0;
1312 BFD_ASSERT (hi
->dynindx
== -1);
1314 /* FIXME: There may be other information to
1315 copy over for particular targets. */
1317 /* See if the new flags lead us to realize
1318 that the symbol must be dynamic. */
1324 || ((hi
->elf_link_hash_flags
1325 & ELF_LINK_HASH_REF_DYNAMIC
)
1331 if ((hi
->elf_link_hash_flags
1332 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1341 if (dynsym
&& h
->dynindx
== -1)
1343 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1345 if (h
->weakdef
!= NULL
1347 && h
->weakdef
->dynindx
== -1)
1349 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1357 /* Now set the weakdefs field correctly for all the weak defined
1358 symbols we found. The only way to do this is to search all the
1359 symbols. Since we only need the information for non functions in
1360 dynamic objects, that's the only time we actually put anything on
1361 the list WEAKS. We need this information so that if a regular
1362 object refers to a symbol defined weakly in a dynamic object, the
1363 real symbol in the dynamic object is also put in the dynamic
1364 symbols; we also must arrange for both symbols to point to the
1365 same memory location. We could handle the general case of symbol
1366 aliasing, but a general symbol alias can only be generated in
1367 assembler code, handling it correctly would be very time
1368 consuming, and other ELF linkers don't handle general aliasing
1370 while (weaks
!= NULL
)
1372 struct elf_link_hash_entry
*hlook
;
1375 struct elf_link_hash_entry
**hpp
;
1376 struct elf_link_hash_entry
**hppend
;
1379 weaks
= hlook
->weakdef
;
1380 hlook
->weakdef
= NULL
;
1382 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
1383 || hlook
->root
.type
== bfd_link_hash_defweak
1384 || hlook
->root
.type
== bfd_link_hash_common
1385 || hlook
->root
.type
== bfd_link_hash_indirect
);
1386 slook
= hlook
->root
.u
.def
.section
;
1387 vlook
= hlook
->root
.u
.def
.value
;
1389 hpp
= elf_sym_hashes (abfd
);
1390 hppend
= hpp
+ extsymcount
;
1391 for (; hpp
< hppend
; hpp
++)
1393 struct elf_link_hash_entry
*h
;
1396 if (h
!= NULL
&& h
!= hlook
1397 && h
->root
.type
== bfd_link_hash_defined
1398 && h
->root
.u
.def
.section
== slook
1399 && h
->root
.u
.def
.value
== vlook
)
1403 /* If the weak definition is in the list of dynamic
1404 symbols, make sure the real definition is put there
1406 if (hlook
->dynindx
!= -1
1407 && h
->dynindx
== -1)
1409 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1413 /* If the real definition is in the list of dynamic
1414 symbols, make sure the weak definition is put there
1415 as well. If we don't do this, then the dynamic
1416 loader might not merge the entries for the real
1417 definition and the weak definition. */
1418 if (h
->dynindx
!= -1
1419 && hlook
->dynindx
== -1)
1421 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
1436 if (extversym
!= NULL
)
1442 /* If this object is the same format as the output object, and it is
1443 not a shared library, then let the backend look through the
1446 This is required to build global offset table entries and to
1447 arrange for dynamic relocs. It is not required for the
1448 particular common case of linking non PIC code, even when linking
1449 against shared libraries, but unfortunately there is no way of
1450 knowing whether an object file has been compiled PIC or not.
1451 Looking through the relocs is not particularly time consuming.
1452 The problem is that we must either (1) keep the relocs in memory,
1453 which causes the linker to require additional runtime memory or
1454 (2) read the relocs twice from the input file, which wastes time.
1455 This would be a good case for using mmap.
1457 I have no idea how to handle linking PIC code into a file of a
1458 different format. It probably can't be done. */
1459 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
1461 && abfd
->xvec
== info
->hash
->creator
1462 && check_relocs
!= NULL
)
1466 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1468 Elf_Internal_Rela
*internal_relocs
;
1471 if ((o
->flags
& SEC_RELOC
) == 0
1472 || o
->reloc_count
== 0)
1475 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
1476 (abfd
, o
, (PTR
) NULL
,
1477 (Elf_Internal_Rela
*) NULL
,
1478 info
->keep_memory
));
1479 if (internal_relocs
== NULL
)
1482 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
1484 if (! info
->keep_memory
)
1485 free (internal_relocs
);
1492 /* If this is a non-traditional, non-relocateable link, try to
1493 optimize the handling of the .stab/.stabstr sections. */
1495 && ! info
->relocateable
1496 && ! info
->traditional_format
1497 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1498 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
1500 asection
*stab
, *stabstr
;
1502 stab
= bfd_get_section_by_name (abfd
, ".stab");
1505 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
1507 if (stabstr
!= NULL
)
1509 struct bfd_elf_section_data
*secdata
;
1511 secdata
= elf_section_data (stab
);
1512 if (! _bfd_link_section_stabs (abfd
,
1513 &elf_hash_table (info
)->stab_info
,
1515 &secdata
->stab_info
))
1530 if (extversym
!= NULL
)
1535 /* Create some sections which will be filled in with dynamic linking
1536 information. ABFD is an input file which requires dynamic sections
1537 to be created. The dynamic sections take up virtual memory space
1538 when the final executable is run, so we need to create them before
1539 addresses are assigned to the output sections. We work out the
1540 actual contents and size of these sections later. */
1543 elf_link_create_dynamic_sections (abfd
, info
)
1545 struct bfd_link_info
*info
;
1548 register asection
*s
;
1549 struct elf_link_hash_entry
*h
;
1550 struct elf_backend_data
*bed
;
1552 if (elf_hash_table (info
)->dynamic_sections_created
)
1555 /* Make sure that all dynamic sections use the same input BFD. */
1556 if (elf_hash_table (info
)->dynobj
== NULL
)
1557 elf_hash_table (info
)->dynobj
= abfd
;
1559 abfd
= elf_hash_table (info
)->dynobj
;
1561 /* Note that we set the SEC_IN_MEMORY flag for all of these
1563 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1564 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
1566 /* A dynamically linked executable has a .interp section, but a
1567 shared library does not. */
1570 s
= bfd_make_section (abfd
, ".interp");
1572 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
1576 /* Create sections to hold version informations. These are removed
1577 if they are not needed. */
1578 s
= bfd_make_section (abfd
, ".gnu.version_d");
1580 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1581 || ! bfd_set_section_alignment (abfd
, s
, 2))
1584 s
= bfd_make_section (abfd
, ".gnu.version");
1586 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1587 || ! bfd_set_section_alignment (abfd
, s
, 1))
1590 s
= bfd_make_section (abfd
, ".gnu.version_r");
1592 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1593 || ! bfd_set_section_alignment (abfd
, s
, 2))
1596 s
= bfd_make_section (abfd
, ".dynsym");
1598 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1599 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1602 s
= bfd_make_section (abfd
, ".dynstr");
1604 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
1607 /* Create a strtab to hold the dynamic symbol names. */
1608 if (elf_hash_table (info
)->dynstr
== NULL
)
1610 elf_hash_table (info
)->dynstr
= elf_stringtab_init ();
1611 if (elf_hash_table (info
)->dynstr
== NULL
)
1615 s
= bfd_make_section (abfd
, ".dynamic");
1617 || ! bfd_set_section_flags (abfd
, s
, flags
)
1618 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1621 /* The special symbol _DYNAMIC is always set to the start of the
1622 .dynamic section. This call occurs before we have processed the
1623 symbols for any dynamic object, so we don't have to worry about
1624 overriding a dynamic definition. We could set _DYNAMIC in a
1625 linker script, but we only want to define it if we are, in fact,
1626 creating a .dynamic section. We don't want to define it if there
1627 is no .dynamic section, since on some ELF platforms the start up
1628 code examines it to decide how to initialize the process. */
1630 if (! (_bfd_generic_link_add_one_symbol
1631 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
1632 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
1633 (struct bfd_link_hash_entry
**) &h
)))
1635 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
1636 h
->type
= STT_OBJECT
;
1639 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1642 s
= bfd_make_section (abfd
, ".hash");
1644 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1645 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1648 /* Let the backend create the rest of the sections. This lets the
1649 backend set the right flags. The backend will normally create
1650 the .got and .plt sections. */
1651 bed
= get_elf_backend_data (abfd
);
1652 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
1655 elf_hash_table (info
)->dynamic_sections_created
= true;
1660 /* Add an entry to the .dynamic table. */
1663 elf_add_dynamic_entry (info
, tag
, val
)
1664 struct bfd_link_info
*info
;
1668 Elf_Internal_Dyn dyn
;
1672 bfd_byte
*newcontents
;
1674 dynobj
= elf_hash_table (info
)->dynobj
;
1676 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
1677 BFD_ASSERT (s
!= NULL
);
1679 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
1680 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
1681 if (newcontents
== NULL
)
1685 dyn
.d_un
.d_val
= val
;
1686 elf_swap_dyn_out (dynobj
, &dyn
,
1687 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
1689 s
->_raw_size
= newsize
;
1690 s
->contents
= newcontents
;
1696 /* Read and swap the relocs for a section. They may have been cached.
1697 If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are not NULL,
1698 they are used as buffers to read into. They are known to be large
1699 enough. If the INTERNAL_RELOCS relocs argument is NULL, the return
1700 value is allocated using either malloc or bfd_alloc, according to
1701 the KEEP_MEMORY argument. */
1704 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
1708 PTR external_relocs
;
1709 Elf_Internal_Rela
*internal_relocs
;
1710 boolean keep_memory
;
1712 Elf_Internal_Shdr
*rel_hdr
;
1714 Elf_Internal_Rela
*alloc2
= NULL
;
1716 if (elf_section_data (o
)->relocs
!= NULL
)
1717 return elf_section_data (o
)->relocs
;
1719 if (o
->reloc_count
== 0)
1722 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
1724 if (internal_relocs
== NULL
)
1728 size
= o
->reloc_count
* sizeof (Elf_Internal_Rela
);
1730 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
1732 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
1733 if (internal_relocs
== NULL
)
1737 if (external_relocs
== NULL
)
1739 alloc1
= (PTR
) bfd_malloc ((size_t) rel_hdr
->sh_size
);
1742 external_relocs
= alloc1
;
1745 if ((bfd_seek (abfd
, rel_hdr
->sh_offset
, SEEK_SET
) != 0)
1746 || (bfd_read (external_relocs
, 1, rel_hdr
->sh_size
, abfd
)
1747 != rel_hdr
->sh_size
))
1750 /* Swap in the relocs. For convenience, we always produce an
1751 Elf_Internal_Rela array; if the relocs are Rel, we set the addend
1753 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
1755 Elf_External_Rel
*erel
;
1756 Elf_External_Rel
*erelend
;
1757 Elf_Internal_Rela
*irela
;
1759 erel
= (Elf_External_Rel
*) external_relocs
;
1760 erelend
= erel
+ o
->reloc_count
;
1761 irela
= internal_relocs
;
1762 for (; erel
< erelend
; erel
++, irela
++)
1764 Elf_Internal_Rel irel
;
1766 elf_swap_reloc_in (abfd
, erel
, &irel
);
1767 irela
->r_offset
= irel
.r_offset
;
1768 irela
->r_info
= irel
.r_info
;
1769 irela
->r_addend
= 0;
1774 Elf_External_Rela
*erela
;
1775 Elf_External_Rela
*erelaend
;
1776 Elf_Internal_Rela
*irela
;
1778 BFD_ASSERT (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
));
1780 erela
= (Elf_External_Rela
*) external_relocs
;
1781 erelaend
= erela
+ o
->reloc_count
;
1782 irela
= internal_relocs
;
1783 for (; erela
< erelaend
; erela
++, irela
++)
1784 elf_swap_reloca_in (abfd
, erela
, irela
);
1787 /* Cache the results for next time, if we can. */
1789 elf_section_data (o
)->relocs
= internal_relocs
;
1794 /* Don't free alloc2, since if it was allocated we are passing it
1795 back (under the name of internal_relocs). */
1797 return internal_relocs
;
1808 /* Record an assignment to a symbol made by a linker script. We need
1809 this in case some dynamic object refers to this symbol. */
1813 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
1815 struct bfd_link_info
*info
;
1819 struct elf_link_hash_entry
*h
;
1821 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1824 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, true, false);
1828 if (h
->root
.type
== bfd_link_hash_new
)
1829 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
1831 /* If this symbol is being provided by the linker script, and it is
1832 currently defined by a dynamic object, but not by a regular
1833 object, then mark it as undefined so that the generic linker will
1834 force the correct value. */
1836 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1837 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1838 h
->root
.type
= bfd_link_hash_undefined
;
1840 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
1841 h
->type
= STT_OBJECT
;
1843 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1844 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
1846 && h
->dynindx
== -1)
1848 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1851 /* If this is a weak defined symbol, and we know a corresponding
1852 real symbol from the same dynamic object, make sure the real
1853 symbol is also made into a dynamic symbol. */
1854 if (h
->weakdef
!= NULL
1855 && h
->weakdef
->dynindx
== -1)
1857 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
1865 /* This structure is used to pass information to
1866 elf_link_assign_sym_version. */
1868 struct elf_assign_sym_version_info
1872 /* General link information. */
1873 struct bfd_link_info
*info
;
1875 struct bfd_elf_version_tree
*verdefs
;
1876 /* Whether we are exporting all dynamic symbols. */
1877 boolean export_dynamic
;
1878 /* Whether we removed any symbols from the dynamic symbol table. */
1879 boolean removed_dynamic
;
1880 /* Whether we had a failure. */
1884 /* This structure is used to pass information to
1885 elf_link_find_version_dependencies. */
1887 struct elf_find_verdep_info
1891 /* General link information. */
1892 struct bfd_link_info
*info
;
1893 /* The number of dependencies. */
1895 /* Whether we had a failure. */
1899 /* Array used to determine the number of hash table buckets to use
1900 based on the number of symbols there are. If there are fewer than
1901 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
1902 fewer than 37 we use 17 buckets, and so forth. We never use more
1903 than 32771 buckets. */
1905 static const size_t elf_buckets
[] =
1907 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
1911 /* Set up the sizes and contents of the ELF dynamic sections. This is
1912 called by the ELF linker emulation before_allocation routine. We
1913 must set the sizes of the sections before the linker sets the
1914 addresses of the various sections. */
1917 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
1918 export_dynamic
, filter_shlib
,
1919 auxiliary_filters
, info
, sinterpptr
,
1924 boolean export_dynamic
;
1925 const char *filter_shlib
;
1926 const char * const *auxiliary_filters
;
1927 struct bfd_link_info
*info
;
1928 asection
**sinterpptr
;
1929 struct bfd_elf_version_tree
*verdefs
;
1931 bfd_size_type soname_indx
;
1933 struct elf_backend_data
*bed
;
1934 bfd_size_type old_dynsymcount
;
1940 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1943 /* The backend may have to create some sections regardless of whether
1944 we're dynamic or not. */
1945 bed
= get_elf_backend_data (output_bfd
);
1946 if (bed
->elf_backend_always_size_sections
1947 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
1950 dynobj
= elf_hash_table (info
)->dynobj
;
1952 /* If there were no dynamic objects in the link, there is nothing to
1957 /* If we are supposed to export all symbols into the dynamic symbol
1958 table (this is not the normal case), then do so. */
1961 struct elf_info_failed eif
;
1965 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
1971 if (elf_hash_table (info
)->dynamic_sections_created
)
1973 struct elf_info_failed eif
;
1974 struct elf_link_hash_entry
*h
;
1975 bfd_size_type strsize
;
1977 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
1978 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
1982 soname_indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
1983 soname
, true, true);
1984 if (soname_indx
== (bfd_size_type
) -1
1985 || ! elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
1991 if (! elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
1999 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, rpath
,
2001 if (indx
== (bfd_size_type
) -1
2002 || ! elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
2006 if (filter_shlib
!= NULL
)
2010 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2011 filter_shlib
, true, true);
2012 if (indx
== (bfd_size_type
) -1
2013 || ! elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
2017 if (auxiliary_filters
!= NULL
)
2019 const char * const *p
;
2021 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
2025 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2027 if (indx
== (bfd_size_type
) -1
2028 || ! elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
2033 /* Find all symbols which were defined in a dynamic object and make
2034 the backend pick a reasonable value for them. */
2037 elf_link_hash_traverse (elf_hash_table (info
),
2038 elf_adjust_dynamic_symbol
,
2043 /* Add some entries to the .dynamic section. We fill in some of the
2044 values later, in elf_bfd_final_link, but we must add the entries
2045 now so that we know the final size of the .dynamic section. */
2046 h
= elf_link_hash_lookup (elf_hash_table (info
), "_init", false,
2049 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2050 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2052 if (! elf_add_dynamic_entry (info
, DT_INIT
, 0))
2055 h
= elf_link_hash_lookup (elf_hash_table (info
), "_fini", false,
2058 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2059 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2061 if (! elf_add_dynamic_entry (info
, DT_FINI
, 0))
2064 strsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
2065 if (! elf_add_dynamic_entry (info
, DT_HASH
, 0)
2066 || ! elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
2067 || ! elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
2068 || ! elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
2069 || ! elf_add_dynamic_entry (info
, DT_SYMENT
,
2070 sizeof (Elf_External_Sym
)))
2074 /* The backend must work out the sizes of all the other dynamic
2076 old_dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2077 if (! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
2080 if (elf_hash_table (info
)->dynamic_sections_created
)
2085 size_t bucketcount
= 0;
2086 Elf_Internal_Sym isym
;
2087 struct elf_assign_sym_version_info sinfo
;
2089 /* Set up the version definition section. */
2090 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2091 BFD_ASSERT (s
!= NULL
);
2093 /* Attach all the symbols to their version information. This
2094 may cause some symbols to be unexported. */
2095 sinfo
.output_bfd
= output_bfd
;
2097 sinfo
.verdefs
= verdefs
;
2098 sinfo
.export_dynamic
= export_dynamic
;
2099 sinfo
.removed_dynamic
= false;
2100 sinfo
.failed
= false;
2102 elf_link_hash_traverse (elf_hash_table (info
),
2103 elf_link_assign_sym_version
,
2108 /* We may have created additional version definitions if we are
2109 just linking a regular application. */
2110 verdefs
= sinfo
.verdefs
;
2112 if (verdefs
== NULL
)
2116 /* Don't include this section in the output file. */
2117 for (spp
= &output_bfd
->sections
;
2118 *spp
!= s
->output_section
;
2119 spp
= &(*spp
)->next
)
2121 *spp
= s
->output_section
->next
;
2122 --output_bfd
->section_count
;
2128 struct bfd_elf_version_tree
*t
;
2130 Elf_Internal_Verdef def
;
2131 Elf_Internal_Verdaux defaux
;
2133 if (sinfo
.removed_dynamic
)
2135 /* Some dynamic symbols were changed to be local
2136 symbols. In this case, we renumber all of the
2137 dynamic symbols, so that we don't have a hole. If
2138 the backend changed dynsymcount, then assume that the
2139 new symbols are at the start. This is the case on
2140 the MIPS. FIXME: The names of the removed symbols
2141 will still be in the dynamic string table, wasting
2143 elf_hash_table (info
)->dynsymcount
=
2144 1 + (elf_hash_table (info
)->dynsymcount
- old_dynsymcount
);
2145 elf_link_hash_traverse (elf_hash_table (info
),
2146 elf_link_renumber_dynsyms
,
2153 /* Make space for the base version. */
2154 size
+= sizeof (Elf_External_Verdef
);
2155 size
+= sizeof (Elf_External_Verdaux
);
2158 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2160 struct bfd_elf_version_deps
*n
;
2162 size
+= sizeof (Elf_External_Verdef
);
2163 size
+= sizeof (Elf_External_Verdaux
);
2166 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2167 size
+= sizeof (Elf_External_Verdaux
);
2170 s
->_raw_size
= size
;
2171 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2172 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2175 /* Fill in the version definition section. */
2179 def
.vd_version
= VER_DEF_CURRENT
;
2180 def
.vd_flags
= VER_FLG_BASE
;
2183 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2184 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2185 + sizeof (Elf_External_Verdaux
));
2187 if (soname_indx
!= -1)
2189 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) soname
);
2190 defaux
.vda_name
= soname_indx
;
2197 name
= output_bfd
->filename
;
2198 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) name
);
2199 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2201 if (indx
== (bfd_size_type
) -1)
2203 defaux
.vda_name
= indx
;
2205 defaux
.vda_next
= 0;
2207 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2208 (Elf_External_Verdef
*)p
);
2209 p
+= sizeof (Elf_External_Verdef
);
2210 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2211 (Elf_External_Verdaux
*) p
);
2212 p
+= sizeof (Elf_External_Verdaux
);
2214 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2217 struct bfd_elf_version_deps
*n
;
2218 struct elf_link_hash_entry
*h
;
2221 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2224 /* Add a symbol representing this version. */
2226 if (! (_bfd_generic_link_add_one_symbol
2227 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
2228 (bfd_vma
) 0, (const char *) NULL
, false,
2229 get_elf_backend_data (dynobj
)->collect
,
2230 (struct bfd_link_hash_entry
**) &h
)))
2232 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
2233 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2234 h
->type
= STT_OBJECT
;
2235 h
->verinfo
.vertree
= t
;
2237 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2240 def
.vd_version
= VER_DEF_CURRENT
;
2242 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
2243 def
.vd_flags
|= VER_FLG_WEAK
;
2244 def
.vd_ndx
= t
->vernum
+ 1;
2245 def
.vd_cnt
= cdeps
+ 1;
2246 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) t
->name
);
2247 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2248 if (t
->next
!= NULL
)
2249 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2250 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
2254 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2255 (Elf_External_Verdef
*) p
);
2256 p
+= sizeof (Elf_External_Verdef
);
2258 defaux
.vda_name
= h
->dynstr_index
;
2259 if (t
->deps
== NULL
)
2260 defaux
.vda_next
= 0;
2262 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2263 t
->name_indx
= defaux
.vda_name
;
2265 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2266 (Elf_External_Verdaux
*) p
);
2267 p
+= sizeof (Elf_External_Verdaux
);
2269 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2271 defaux
.vda_name
= n
->version_needed
->name_indx
;
2272 if (n
->next
== NULL
)
2273 defaux
.vda_next
= 0;
2275 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2277 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2278 (Elf_External_Verdaux
*) p
);
2279 p
+= sizeof (Elf_External_Verdaux
);
2283 if (! elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
2284 || ! elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
2287 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
2290 /* Work out the size of the version reference section. */
2292 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2293 BFD_ASSERT (s
!= NULL
);
2295 struct elf_find_verdep_info sinfo
;
2297 sinfo
.output_bfd
= output_bfd
;
2299 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
2300 if (sinfo
.vers
== 0)
2302 sinfo
.failed
= false;
2304 elf_link_hash_traverse (elf_hash_table (info
),
2305 elf_link_find_version_dependencies
,
2308 if (elf_tdata (output_bfd
)->verref
== NULL
)
2312 /* We don't have any version definitions, so we can just
2313 remove the section. */
2315 for (spp
= &output_bfd
->sections
;
2316 *spp
!= s
->output_section
;
2317 spp
= &(*spp
)->next
)
2319 *spp
= s
->output_section
->next
;
2320 --output_bfd
->section_count
;
2324 Elf_Internal_Verneed
*t
;
2329 /* Build the version definition section. */
2332 for (t
= elf_tdata (output_bfd
)->verref
;
2336 Elf_Internal_Vernaux
*a
;
2338 size
+= sizeof (Elf_External_Verneed
);
2340 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2341 size
+= sizeof (Elf_External_Vernaux
);
2344 s
->_raw_size
= size
;
2345 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, size
);
2346 if (s
->contents
== NULL
)
2350 for (t
= elf_tdata (output_bfd
)->verref
;
2355 Elf_Internal_Vernaux
*a
;
2359 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2362 t
->vn_version
= VER_NEED_CURRENT
;
2364 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2365 t
->vn_bfd
->filename
, true, false);
2366 if (indx
== (bfd_size_type
) -1)
2369 t
->vn_aux
= sizeof (Elf_External_Verneed
);
2370 if (t
->vn_nextref
== NULL
)
2373 t
->vn_next
= (sizeof (Elf_External_Verneed
)
2374 + caux
* sizeof (Elf_External_Vernaux
));
2376 _bfd_elf_swap_verneed_out (output_bfd
, t
,
2377 (Elf_External_Verneed
*) p
);
2378 p
+= sizeof (Elf_External_Verneed
);
2380 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2382 a
->vna_hash
= bfd_elf_hash ((const unsigned char *)
2384 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2385 a
->vna_nodename
, true, false);
2386 if (indx
== (bfd_size_type
) -1)
2389 if (a
->vna_nextptr
== NULL
)
2392 a
->vna_next
= sizeof (Elf_External_Vernaux
);
2394 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
2395 (Elf_External_Vernaux
*) p
);
2396 p
+= sizeof (Elf_External_Vernaux
);
2400 if (! elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
2401 || ! elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
2404 elf_tdata (output_bfd
)->cverrefs
= crefs
;
2408 dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2410 /* Work out the size of the symbol version section. */
2411 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
2412 BFD_ASSERT (s
!= NULL
);
2413 if (dynsymcount
== 0
2414 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
2418 /* We don't need any symbol versions; just discard the
2420 for (spp
= &output_bfd
->sections
;
2421 *spp
!= s
->output_section
;
2422 spp
= &(*spp
)->next
)
2424 *spp
= s
->output_section
->next
;
2425 --output_bfd
->section_count
;
2429 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
2430 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
2431 if (s
->contents
== NULL
)
2434 if (! elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
2438 /* Set the size of the .dynsym and .hash sections. We counted
2439 the number of dynamic symbols in elf_link_add_object_symbols.
2440 We will build the contents of .dynsym and .hash when we build
2441 the final symbol table, because until then we do not know the
2442 correct value to give the symbols. We built the .dynstr
2443 section as we went along in elf_link_add_object_symbols. */
2444 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
2445 BFD_ASSERT (s
!= NULL
);
2446 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
2447 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2448 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2451 /* The first entry in .dynsym is a dummy symbol. */
2458 elf_swap_symbol_out (output_bfd
, &isym
,
2459 (PTR
) (Elf_External_Sym
*) s
->contents
);
2461 for (i
= 0; elf_buckets
[i
] != 0; i
++)
2463 bucketcount
= elf_buckets
[i
];
2464 if (dynsymcount
< elf_buckets
[i
+ 1])
2468 s
= bfd_get_section_by_name (dynobj
, ".hash");
2469 BFD_ASSERT (s
!= NULL
);
2470 s
->_raw_size
= (2 + bucketcount
+ dynsymcount
) * (ARCH_SIZE
/ 8);
2471 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2472 if (s
->contents
== NULL
)
2474 memset (s
->contents
, 0, (size_t) s
->_raw_size
);
2476 put_word (output_bfd
, bucketcount
, s
->contents
);
2477 put_word (output_bfd
, dynsymcount
, s
->contents
+ (ARCH_SIZE
/ 8));
2479 elf_hash_table (info
)->bucketcount
= bucketcount
;
2481 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
2482 BFD_ASSERT (s
!= NULL
);
2483 s
->_raw_size
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
2485 if (! elf_add_dynamic_entry (info
, DT_NULL
, 0))
2492 /* Make the backend pick a good value for a dynamic symbol. This is
2493 called via elf_link_hash_traverse, and also calls itself
2497 elf_adjust_dynamic_symbol (h
, data
)
2498 struct elf_link_hash_entry
*h
;
2501 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2503 struct elf_backend_data
*bed
;
2505 /* Ignore indirect symbols. These are added by the versioning code. */
2506 if (h
->root
.type
== bfd_link_hash_indirect
)
2509 /* If this symbol was mentioned in a non-ELF file, try to set
2510 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2511 permit a non-ELF file to correctly refer to a symbol defined in
2512 an ELF dynamic object. */
2513 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
2515 if (h
->root
.type
!= bfd_link_hash_defined
2516 && h
->root
.type
!= bfd_link_hash_defweak
)
2517 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2520 if (h
->root
.u
.def
.section
->owner
!= NULL
2521 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2522 == bfd_target_elf_flavour
))
2523 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2525 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2528 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2529 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
2531 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2539 /* If this is a final link, and the symbol was defined as a common
2540 symbol in a regular object file, and there was no definition in
2541 any dynamic object, then the linker will have allocated space for
2542 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2543 flag will not have been set. */
2544 if (h
->root
.type
== bfd_link_hash_defined
2545 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2546 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
2547 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2548 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2549 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2551 /* If -Bsymbolic was used (which means to bind references to global
2552 symbols to the definition within the shared object), and this
2553 symbol was defined in a regular object, then it actually doesn't
2554 need a PLT entry. */
2555 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
2556 && eif
->info
->shared
2557 && eif
->info
->symbolic
2558 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2559 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
2561 /* If this symbol does not require a PLT entry, and it is not
2562 defined by a dynamic object, or is not referenced by a regular
2563 object, ignore it. We do have to handle a weak defined symbol,
2564 even if no regular object refers to it, if we decided to add it
2565 to the dynamic symbol table. FIXME: Do we normally need to worry
2566 about symbols which are defined by one dynamic object and
2567 referenced by another one? */
2568 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
2569 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
2570 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2571 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
2572 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
2575 /* If we've already adjusted this symbol, don't do it again. This
2576 can happen via a recursive call. */
2577 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
2580 /* Don't look at this symbol again. Note that we must set this
2581 after checking the above conditions, because we may look at a
2582 symbol once, decide not to do anything, and then get called
2583 recursively later after REF_REGULAR is set below. */
2584 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
2586 /* If this is a weak definition, and we know a real definition, and
2587 the real symbol is not itself defined by a regular object file,
2588 then get a good value for the real definition. We handle the
2589 real symbol first, for the convenience of the backend routine.
2591 Note that there is a confusing case here. If the real definition
2592 is defined by a regular object file, we don't get the real symbol
2593 from the dynamic object, but we do get the weak symbol. If the
2594 processor backend uses a COPY reloc, then if some routine in the
2595 dynamic object changes the real symbol, we will not see that
2596 change in the corresponding weak symbol. This is the way other
2597 ELF linkers work as well, and seems to be a result of the shared
2600 I will clarify this issue. Most SVR4 shared libraries define the
2601 variable _timezone and define timezone as a weak synonym. The
2602 tzset call changes _timezone. If you write
2603 extern int timezone;
2605 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2606 you might expect that, since timezone is a synonym for _timezone,
2607 the same number will print both times. However, if the processor
2608 backend uses a COPY reloc, then actually timezone will be copied
2609 into your process image, and, since you define _timezone
2610 yourself, _timezone will not. Thus timezone and _timezone will
2611 wind up at different memory locations. The tzset call will set
2612 _timezone, leaving timezone unchanged. */
2614 if (h
->weakdef
!= NULL
)
2616 struct elf_link_hash_entry
*weakdef
;
2618 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2619 || h
->root
.type
== bfd_link_hash_defweak
);
2620 weakdef
= h
->weakdef
;
2621 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2622 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2623 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
2624 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2626 /* This symbol is defined by a regular object file, so we
2627 will not do anything special. Clear weakdef for the
2628 convenience of the processor backend. */
2633 /* There is an implicit reference by a regular object file
2634 via the weak symbol. */
2635 weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2636 if (! elf_adjust_dynamic_symbol (weakdef
, (PTR
) eif
))
2641 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2642 bed
= get_elf_backend_data (dynobj
);
2643 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2652 /* This routine is used to export all defined symbols into the dynamic
2653 symbol table. It is called via elf_link_hash_traverse. */
2656 elf_export_symbol (h
, data
)
2657 struct elf_link_hash_entry
*h
;
2660 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2662 /* Ignore indirect symbols. These are added by the versioning code. */
2663 if (h
->root
.type
== bfd_link_hash_indirect
)
2666 if (h
->dynindx
== -1
2667 && (h
->elf_link_hash_flags
2668 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
2670 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2680 /* Look through the symbols which are defined in other shared
2681 libraries and referenced here. Update the list of version
2682 dependencies. This will be put into the .gnu.version_r section.
2683 This function is called via elf_link_hash_traverse. */
2686 elf_link_find_version_dependencies (h
, data
)
2687 struct elf_link_hash_entry
*h
;
2690 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2691 Elf_Internal_Verneed
*t
;
2692 Elf_Internal_Vernaux
*a
;
2694 /* We only care about symbols defined in shared objects with version
2696 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2697 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
2699 || h
->verinfo
.verdef
== NULL
)
2702 /* See if we already know about this version. */
2703 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
2705 if (t
->vn_bfd
== h
->verinfo
.verdef
->vd_bfd
)
2708 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2709 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2715 /* This is a new version. Add it to tree we are building. */
2719 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *t
);
2722 rinfo
->failed
= true;
2726 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2727 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
2728 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
2731 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *a
);
2733 /* Note that we are copying a string pointer here, and testing it
2734 above. If bfd_elf_string_from_elf_section is ever changed to
2735 discard the string data when low in memory, this will have to be
2737 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2739 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2740 a
->vna_nextptr
= t
->vn_auxptr
;
2742 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2745 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2752 /* Figure out appropriate versions for all the symbols. We may not
2753 have the version number script until we have read all of the input
2754 files, so until that point we don't know which symbols should be
2755 local. This function is called via elf_link_hash_traverse. */
2758 elf_link_assign_sym_version (h
, data
)
2759 struct elf_link_hash_entry
*h
;
2762 struct elf_assign_sym_version_info
*sinfo
=
2763 (struct elf_assign_sym_version_info
*) data
;
2764 struct bfd_link_info
*info
= sinfo
->info
;
2767 /* We only need version numbers for symbols defined in regular
2769 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2772 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2773 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2775 struct bfd_elf_version_tree
*t
;
2780 /* There are two consecutive ELF_VER_CHR characters if this is
2781 not a hidden symbol. */
2783 if (*p
== ELF_VER_CHR
)
2789 /* If there is no version string, we can just return out. */
2793 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
2797 /* Look for the version. If we find it, it is no longer weak. */
2798 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
2800 if (strcmp (t
->name
, p
) == 0)
2802 h
->verinfo
.vertree
= t
;
2805 /* See if there is anything to force this symbol to
2807 if (t
->locals
!= NULL
)
2811 struct bfd_elf_version_expr
*d
;
2813 len
= p
- h
->root
.root
.string
;
2814 alc
= bfd_alloc (sinfo
->output_bfd
, len
);
2817 strncpy (alc
, h
->root
.root
.string
, len
- 1);
2818 alc
[len
- 1] = '\0';
2819 if (alc
[len
- 2] == ELF_VER_CHR
)
2820 alc
[len
- 2] = '\0';
2822 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
2824 if ((d
->match
[0] == '*' && d
->match
[1] == '\0')
2825 || fnmatch (d
->match
, alc
, 0) == 0)
2827 if (h
->dynindx
!= -1
2829 && ! sinfo
->export_dynamic
2830 && (h
->elf_link_hash_flags
2831 & ELF_LINK_HASH_NEEDS_PLT
) == 0)
2833 sinfo
->removed_dynamic
= true;
2834 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
2836 /* FIXME: The name of the symbol has
2837 already been recorded in the dynamic
2838 string table section. */
2845 bfd_release (sinfo
->output_bfd
, alc
);
2852 /* If we are building an application, we need to create a
2853 version node for this version. */
2854 if (t
== NULL
&& ! info
->shared
)
2856 struct bfd_elf_version_tree
**pp
;
2859 /* If we aren't going to export this symbol, we don't need
2860 to worry about it. */
2861 if (h
->dynindx
== -1)
2864 t
= ((struct bfd_elf_version_tree
*)
2865 bfd_alloc (sinfo
->output_bfd
, sizeof *t
));
2868 sinfo
->failed
= true;
2877 t
->name_indx
= (unsigned int) -1;
2881 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
2883 t
->vernum
= version_index
;
2887 h
->verinfo
.vertree
= t
;
2891 /* We could not find the version for a symbol when
2892 generating a shared archive. Return an error. */
2893 (*_bfd_error_handler
)
2894 ("%s: undefined version name %s",
2895 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
2896 bfd_set_error (bfd_error_bad_value
);
2897 sinfo
->failed
= true;
2902 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
2905 /* If we don't have a version for this symbol, see if we can find
2907 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
2909 struct bfd_elf_version_tree
*t
;
2910 struct bfd_elf_version_tree
*deflt
;
2911 struct bfd_elf_version_expr
*d
;
2913 /* See if can find what version this symbol is in. If the
2914 symbol is supposed to eb local, then don't actually register
2917 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
2919 if (t
->globals
!= NULL
)
2921 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
2923 if (fnmatch (d
->match
, h
->root
.root
.string
, 0) == 0)
2925 h
->verinfo
.vertree
= t
;
2934 if (t
->locals
!= NULL
)
2936 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
2938 if (d
->match
[0] == '*' && d
->match
[1] == '\0')
2940 else if (fnmatch (d
->match
, h
->root
.root
.string
, 0) == 0)
2942 h
->verinfo
.vertree
= t
;
2943 if (h
->dynindx
!= -1
2945 && ! sinfo
->export_dynamic
2946 && (h
->elf_link_hash_flags
2947 & ELF_LINK_HASH_NEEDS_PLT
) == 0)
2949 sinfo
->removed_dynamic
= true;
2950 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
2952 /* FIXME: The name of the symbol has already
2953 been recorded in the dynamic string table
2965 if (deflt
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2967 h
->verinfo
.vertree
= deflt
;
2968 if (h
->dynindx
!= -1
2970 && ! sinfo
->export_dynamic
2971 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
2973 sinfo
->removed_dynamic
= true;
2974 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
2976 /* FIXME: The name of the symbol has already been
2977 recorded in the dynamic string table section. */
2985 /* This function is used to renumber the dynamic symbols, if some of
2986 them are removed because they are marked as local. This is called
2987 via elf_link_hash_traverse. */
2990 elf_link_renumber_dynsyms (h
, data
)
2991 struct elf_link_hash_entry
*h
;
2994 struct bfd_link_info
*info
= (struct bfd_link_info
*) data
;
2996 if (h
->dynindx
!= -1)
2998 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
2999 ++elf_hash_table (info
)->dynsymcount
;
3005 /* Final phase of ELF linker. */
3007 /* A structure we use to avoid passing large numbers of arguments. */
3009 struct elf_final_link_info
3011 /* General link information. */
3012 struct bfd_link_info
*info
;
3015 /* Symbol string table. */
3016 struct bfd_strtab_hash
*symstrtab
;
3017 /* .dynsym section. */
3018 asection
*dynsym_sec
;
3019 /* .hash section. */
3021 /* symbol version section (.gnu.version). */
3022 asection
*symver_sec
;
3023 /* Buffer large enough to hold contents of any section. */
3025 /* Buffer large enough to hold external relocs of any section. */
3026 PTR external_relocs
;
3027 /* Buffer large enough to hold internal relocs of any section. */
3028 Elf_Internal_Rela
*internal_relocs
;
3029 /* Buffer large enough to hold external local symbols of any input
3031 Elf_External_Sym
*external_syms
;
3032 /* Buffer large enough to hold internal local symbols of any input
3034 Elf_Internal_Sym
*internal_syms
;
3035 /* Array large enough to hold a symbol index for each local symbol
3036 of any input BFD. */
3038 /* Array large enough to hold a section pointer for each local
3039 symbol of any input BFD. */
3040 asection
**sections
;
3041 /* Buffer to hold swapped out symbols. */
3042 Elf_External_Sym
*symbuf
;
3043 /* Number of swapped out symbols in buffer. */
3044 size_t symbuf_count
;
3045 /* Number of symbols which fit in symbuf. */
3049 static boolean elf_link_output_sym
3050 PARAMS ((struct elf_final_link_info
*, const char *,
3051 Elf_Internal_Sym
*, asection
*));
3052 static boolean elf_link_flush_output_syms
3053 PARAMS ((struct elf_final_link_info
*));
3054 static boolean elf_link_output_extsym
3055 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3056 static boolean elf_link_input_bfd
3057 PARAMS ((struct elf_final_link_info
*, bfd
*));
3058 static boolean elf_reloc_link_order
3059 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
3060 struct bfd_link_order
*));
3062 /* This struct is used to pass information to elf_link_output_extsym. */
3064 struct elf_outext_info
3068 struct elf_final_link_info
*finfo
;
3071 /* Do the final step of an ELF link. */
3074 elf_bfd_final_link (abfd
, info
)
3076 struct bfd_link_info
*info
;
3080 struct elf_final_link_info finfo
;
3081 register asection
*o
;
3082 register struct bfd_link_order
*p
;
3084 size_t max_contents_size
;
3085 size_t max_external_reloc_size
;
3086 size_t max_internal_reloc_count
;
3087 size_t max_sym_count
;
3089 Elf_Internal_Sym elfsym
;
3091 Elf_Internal_Shdr
*symtab_hdr
;
3092 Elf_Internal_Shdr
*symstrtab_hdr
;
3093 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3094 struct elf_outext_info eoinfo
;
3097 abfd
->flags
|= DYNAMIC
;
3099 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
3100 dynobj
= elf_hash_table (info
)->dynobj
;
3103 finfo
.output_bfd
= abfd
;
3104 finfo
.symstrtab
= elf_stringtab_init ();
3105 if (finfo
.symstrtab
== NULL
)
3110 finfo
.dynsym_sec
= NULL
;
3111 finfo
.hash_sec
= NULL
;
3112 finfo
.symver_sec
= NULL
;
3116 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
3117 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
3118 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
3119 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3120 /* Note that it is OK if symver_sec is NULL. */
3123 finfo
.contents
= NULL
;
3124 finfo
.external_relocs
= NULL
;
3125 finfo
.internal_relocs
= NULL
;
3126 finfo
.external_syms
= NULL
;
3127 finfo
.internal_syms
= NULL
;
3128 finfo
.indices
= NULL
;
3129 finfo
.sections
= NULL
;
3130 finfo
.symbuf
= NULL
;
3131 finfo
.symbuf_count
= 0;
3133 /* Count up the number of relocations we will output for each output
3134 section, so that we know the sizes of the reloc sections. We
3135 also figure out some maximum sizes. */
3136 max_contents_size
= 0;
3137 max_external_reloc_size
= 0;
3138 max_internal_reloc_count
= 0;
3140 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3144 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3146 if (p
->type
== bfd_section_reloc_link_order
3147 || p
->type
== bfd_symbol_reloc_link_order
)
3149 else if (p
->type
== bfd_indirect_link_order
)
3153 sec
= p
->u
.indirect
.section
;
3155 /* Mark all sections which are to be included in the
3156 link. This will normally be every section. We need
3157 to do this so that we can identify any sections which
3158 the linker has decided to not include. */
3159 sec
->linker_mark
= true;
3161 if (info
->relocateable
)
3162 o
->reloc_count
+= sec
->reloc_count
;
3164 if (sec
->_raw_size
> max_contents_size
)
3165 max_contents_size
= sec
->_raw_size
;
3166 if (sec
->_cooked_size
> max_contents_size
)
3167 max_contents_size
= sec
->_cooked_size
;
3169 /* We are interested in just local symbols, not all
3171 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
3172 && (sec
->owner
->flags
& DYNAMIC
) == 0)
3176 if (elf_bad_symtab (sec
->owner
))
3177 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
3178 / sizeof (Elf_External_Sym
));
3180 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
3182 if (sym_count
> max_sym_count
)
3183 max_sym_count
= sym_count
;
3185 if ((sec
->flags
& SEC_RELOC
) != 0)
3189 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
3190 if (ext_size
> max_external_reloc_size
)
3191 max_external_reloc_size
= ext_size
;
3192 if (sec
->reloc_count
> max_internal_reloc_count
)
3193 max_internal_reloc_count
= sec
->reloc_count
;
3199 if (o
->reloc_count
> 0)
3200 o
->flags
|= SEC_RELOC
;
3203 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3204 set it (this is probably a bug) and if it is set
3205 assign_section_numbers will create a reloc section. */
3206 o
->flags
&=~ SEC_RELOC
;
3209 /* If the SEC_ALLOC flag is not set, force the section VMA to
3210 zero. This is done in elf_fake_sections as well, but forcing
3211 the VMA to 0 here will ensure that relocs against these
3212 sections are handled correctly. */
3213 if ((o
->flags
& SEC_ALLOC
) == 0
3214 && ! o
->user_set_vma
)
3218 /* Figure out the file positions for everything but the symbol table
3219 and the relocs. We set symcount to force assign_section_numbers
3220 to create a symbol table. */
3221 abfd
->symcount
= info
->strip
== strip_all
? 0 : 1;
3222 BFD_ASSERT (! abfd
->output_has_begun
);
3223 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
3226 /* That created the reloc sections. Set their sizes, and assign
3227 them file positions, and allocate some buffers. */
3228 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3230 if ((o
->flags
& SEC_RELOC
) != 0)
3232 Elf_Internal_Shdr
*rel_hdr
;
3233 register struct elf_link_hash_entry
**p
, **pend
;
3235 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
3237 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* o
->reloc_count
;
3239 /* The contents field must last into write_object_contents,
3240 so we allocate it with bfd_alloc rather than malloc. */
3241 rel_hdr
->contents
= (PTR
) bfd_alloc (abfd
, rel_hdr
->sh_size
);
3242 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
3245 p
= ((struct elf_link_hash_entry
**)
3246 bfd_malloc (o
->reloc_count
3247 * sizeof (struct elf_link_hash_entry
*)));
3248 if (p
== NULL
&& o
->reloc_count
!= 0)
3250 elf_section_data (o
)->rel_hashes
= p
;
3251 pend
= p
+ o
->reloc_count
;
3252 for (; p
< pend
; p
++)
3255 /* Use the reloc_count field as an index when outputting the
3261 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3263 /* We have now assigned file positions for all the sections except
3264 .symtab and .strtab. We start the .symtab section at the current
3265 file position, and write directly to it. We build the .strtab
3266 section in memory. */
3268 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3269 /* sh_name is set in prep_headers. */
3270 symtab_hdr
->sh_type
= SHT_SYMTAB
;
3271 symtab_hdr
->sh_flags
= 0;
3272 symtab_hdr
->sh_addr
= 0;
3273 symtab_hdr
->sh_size
= 0;
3274 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
3275 /* sh_link is set in assign_section_numbers. */
3276 /* sh_info is set below. */
3277 /* sh_offset is set just below. */
3278 symtab_hdr
->sh_addralign
= 4; /* FIXME: system dependent? */
3280 off
= elf_tdata (abfd
)->next_file_pos
;
3281 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
3283 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3284 incorrect. We do not yet know the size of the .symtab section.
3285 We correct next_file_pos below, after we do know the size. */
3287 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3288 continuously seeking to the right position in the file. */
3289 if (! info
->keep_memory
|| max_sym_count
< 20)
3290 finfo
.symbuf_size
= 20;
3292 finfo
.symbuf_size
= max_sym_count
;
3293 finfo
.symbuf
= ((Elf_External_Sym
*)
3294 bfd_malloc (finfo
.symbuf_size
* sizeof (Elf_External_Sym
)));
3295 if (finfo
.symbuf
== NULL
)
3298 /* Start writing out the symbol table. The first symbol is always a
3300 if (info
->strip
!= strip_all
|| info
->relocateable
)
3302 elfsym
.st_value
= 0;
3305 elfsym
.st_other
= 0;
3306 elfsym
.st_shndx
= SHN_UNDEF
;
3307 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
3308 &elfsym
, bfd_und_section_ptr
))
3313 /* Some standard ELF linkers do this, but we don't because it causes
3314 bootstrap comparison failures. */
3315 /* Output a file symbol for the output file as the second symbol.
3316 We output this even if we are discarding local symbols, although
3317 I'm not sure if this is correct. */
3318 elfsym
.st_value
= 0;
3320 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
3321 elfsym
.st_other
= 0;
3322 elfsym
.st_shndx
= SHN_ABS
;
3323 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
3324 &elfsym
, bfd_abs_section_ptr
))
3328 /* Output a symbol for each section. We output these even if we are
3329 discarding local symbols, since they are used for relocs. These
3330 symbols have no names. We store the index of each one in the
3331 index field of the section, so that we can find it again when
3332 outputting relocs. */
3333 if (info
->strip
!= strip_all
|| info
->relocateable
)
3336 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
3337 elfsym
.st_other
= 0;
3338 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
3340 o
= section_from_elf_index (abfd
, i
);
3342 o
->target_index
= abfd
->symcount
;
3343 elfsym
.st_shndx
= i
;
3344 if (info
->relocateable
|| o
== NULL
)
3345 elfsym
.st_value
= 0;
3347 elfsym
.st_value
= o
->vma
;
3348 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
3354 /* Allocate some memory to hold information read in from the input
3356 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
3357 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
3358 finfo
.internal_relocs
= ((Elf_Internal_Rela
*)
3359 bfd_malloc (max_internal_reloc_count
3360 * sizeof (Elf_Internal_Rela
)));
3361 finfo
.external_syms
= ((Elf_External_Sym
*)
3362 bfd_malloc (max_sym_count
3363 * sizeof (Elf_External_Sym
)));
3364 finfo
.internal_syms
= ((Elf_Internal_Sym
*)
3365 bfd_malloc (max_sym_count
3366 * sizeof (Elf_Internal_Sym
)));
3367 finfo
.indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
3368 finfo
.sections
= ((asection
**)
3369 bfd_malloc (max_sym_count
* sizeof (asection
*)));
3370 if ((finfo
.contents
== NULL
&& max_contents_size
!= 0)
3371 || (finfo
.external_relocs
== NULL
&& max_external_reloc_size
!= 0)
3372 || (finfo
.internal_relocs
== NULL
&& max_internal_reloc_count
!= 0)
3373 || (finfo
.external_syms
== NULL
&& max_sym_count
!= 0)
3374 || (finfo
.internal_syms
== NULL
&& max_sym_count
!= 0)
3375 || (finfo
.indices
== NULL
&& max_sym_count
!= 0)
3376 || (finfo
.sections
== NULL
&& max_sym_count
!= 0))
3379 /* Since ELF permits relocations to be against local symbols, we
3380 must have the local symbols available when we do the relocations.
3381 Since we would rather only read the local symbols once, and we
3382 would rather not keep them in memory, we handle all the
3383 relocations for a single input file at the same time.
3385 Unfortunately, there is no way to know the total number of local
3386 symbols until we have seen all of them, and the local symbol
3387 indices precede the global symbol indices. This means that when
3388 we are generating relocateable output, and we see a reloc against
3389 a global symbol, we can not know the symbol index until we have
3390 finished examining all the local symbols to see which ones we are
3391 going to output. To deal with this, we keep the relocations in
3392 memory, and don't output them until the end of the link. This is
3393 an unfortunate waste of memory, but I don't see a good way around
3394 it. Fortunately, it only happens when performing a relocateable
3395 link, which is not the common case. FIXME: If keep_memory is set
3396 we could write the relocs out and then read them again; I don't
3397 know how bad the memory loss will be. */
3399 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->next
)
3400 sub
->output_has_begun
= false;
3401 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3403 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3405 if (p
->type
== bfd_indirect_link_order
3406 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
3407 == bfd_target_elf_flavour
))
3409 sub
= p
->u
.indirect
.section
->owner
;
3410 if (! sub
->output_has_begun
)
3412 if (! elf_link_input_bfd (&finfo
, sub
))
3414 sub
->output_has_begun
= true;
3417 else if (p
->type
== bfd_section_reloc_link_order
3418 || p
->type
== bfd_symbol_reloc_link_order
)
3420 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
3425 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
3431 /* That wrote out all the local symbols. Finish up the symbol table
3432 with the global symbols. */
3434 if (info
->strip
!= strip_all
&& info
->shared
)
3436 /* Output any global symbols that got converted to local in a
3437 version script. We do this in a separate step since ELF
3438 requires all local symbols to appear prior to any global
3439 symbols. FIXME: We should only do this if some global
3440 symbols were, in fact, converted to become local. FIXME:
3441 Will this work correctly with the Irix 5 linker? */
3442 eoinfo
.failed
= false;
3443 eoinfo
.finfo
= &finfo
;
3444 eoinfo
.localsyms
= true;
3445 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3451 /* The sh_info field records the index of the first non local
3453 symtab_hdr
->sh_info
= abfd
->symcount
;
3455 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
= 1;
3457 /* We get the global symbols from the hash table. */
3458 eoinfo
.failed
= false;
3459 eoinfo
.localsyms
= false;
3460 eoinfo
.finfo
= &finfo
;
3461 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3466 /* Flush all symbols to the file. */
3467 if (! elf_link_flush_output_syms (&finfo
))
3470 /* Now we know the size of the symtab section. */
3471 off
+= symtab_hdr
->sh_size
;
3473 /* Finish up and write out the symbol string table (.strtab)
3475 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3476 /* sh_name was set in prep_headers. */
3477 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
3478 symstrtab_hdr
->sh_flags
= 0;
3479 symstrtab_hdr
->sh_addr
= 0;
3480 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
3481 symstrtab_hdr
->sh_entsize
= 0;
3482 symstrtab_hdr
->sh_link
= 0;
3483 symstrtab_hdr
->sh_info
= 0;
3484 /* sh_offset is set just below. */
3485 symstrtab_hdr
->sh_addralign
= 1;
3487 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, true);
3488 elf_tdata (abfd
)->next_file_pos
= off
;
3490 if (abfd
->symcount
> 0)
3492 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
3493 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
3497 /* Adjust the relocs to have the correct symbol indices. */
3498 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3500 struct elf_link_hash_entry
**rel_hash
;
3501 Elf_Internal_Shdr
*rel_hdr
;
3503 if ((o
->flags
& SEC_RELOC
) == 0)
3506 rel_hash
= elf_section_data (o
)->rel_hashes
;
3507 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
3508 for (i
= 0; i
< o
->reloc_count
; i
++, rel_hash
++)
3510 if (*rel_hash
== NULL
)
3513 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
3515 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
3517 Elf_External_Rel
*erel
;
3518 Elf_Internal_Rel irel
;
3520 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
3521 elf_swap_reloc_in (abfd
, erel
, &irel
);
3522 irel
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
3523 ELF_R_TYPE (irel
.r_info
));
3524 elf_swap_reloc_out (abfd
, &irel
, erel
);
3528 Elf_External_Rela
*erela
;
3529 Elf_Internal_Rela irela
;
3531 BFD_ASSERT (rel_hdr
->sh_entsize
3532 == sizeof (Elf_External_Rela
));
3534 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
3535 elf_swap_reloca_in (abfd
, erela
, &irela
);
3536 irela
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
3537 ELF_R_TYPE (irela
.r_info
));
3538 elf_swap_reloca_out (abfd
, &irela
, erela
);
3542 /* Set the reloc_count field to 0 to prevent write_relocs from
3543 trying to swap the relocs out itself. */
3547 /* If we are linking against a dynamic object, or generating a
3548 shared library, finish up the dynamic linking information. */
3551 Elf_External_Dyn
*dyncon
, *dynconend
;
3553 /* Fix up .dynamic entries. */
3554 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
3555 BFD_ASSERT (o
!= NULL
);
3557 dyncon
= (Elf_External_Dyn
*) o
->contents
;
3558 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
3559 for (; dyncon
< dynconend
; dyncon
++)
3561 Elf_Internal_Dyn dyn
;
3565 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
3572 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on
3573 magic _init and _fini symbols. This is pretty ugly,
3574 but we are compatible. */
3582 struct elf_link_hash_entry
*h
;
3584 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
3585 false, false, true);
3587 && (h
->root
.type
== bfd_link_hash_defined
3588 || h
->root
.type
== bfd_link_hash_defweak
))
3590 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
3591 o
= h
->root
.u
.def
.section
;
3592 if (o
->output_section
!= NULL
)
3593 dyn
.d_un
.d_val
+= (o
->output_section
->vma
3594 + o
->output_offset
);
3597 /* The symbol is imported from another shared
3598 library and does not apply to this one. */
3602 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3617 name
= ".gnu.version_d";
3620 name
= ".gnu.version_r";
3623 name
= ".gnu.version";
3625 o
= bfd_get_section_by_name (abfd
, name
);
3626 BFD_ASSERT (o
!= NULL
);
3627 dyn
.d_un
.d_ptr
= o
->vma
;
3628 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3635 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
3640 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
3642 Elf_Internal_Shdr
*hdr
;
3644 hdr
= elf_elfsections (abfd
)[i
];
3645 if (hdr
->sh_type
== type
3646 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
3648 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
3649 dyn
.d_un
.d_val
+= hdr
->sh_size
;
3652 if (dyn
.d_un
.d_val
== 0
3653 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
3654 dyn
.d_un
.d_val
= hdr
->sh_addr
;
3658 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3664 /* If we have created any dynamic sections, then output them. */
3667 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
3670 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
3672 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
3673 || o
->_raw_size
== 0)
3675 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
3677 /* At this point, we are only interested in sections
3678 created by elf_link_create_dynamic_sections. */
3681 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
3683 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
3685 if (! bfd_set_section_contents (abfd
, o
->output_section
,
3686 o
->contents
, o
->output_offset
,
3694 /* The contents of the .dynstr section are actually in a
3696 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
3697 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
3698 || ! _bfd_stringtab_emit (abfd
,
3699 elf_hash_table (info
)->dynstr
))
3705 /* If we have optimized stabs strings, output them. */
3706 if (elf_hash_table (info
)->stab_info
!= NULL
)
3708 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
3712 if (finfo
.symstrtab
!= NULL
)
3713 _bfd_stringtab_free (finfo
.symstrtab
);
3714 if (finfo
.contents
!= NULL
)
3715 free (finfo
.contents
);
3716 if (finfo
.external_relocs
!= NULL
)
3717 free (finfo
.external_relocs
);
3718 if (finfo
.internal_relocs
!= NULL
)
3719 free (finfo
.internal_relocs
);
3720 if (finfo
.external_syms
!= NULL
)
3721 free (finfo
.external_syms
);
3722 if (finfo
.internal_syms
!= NULL
)
3723 free (finfo
.internal_syms
);
3724 if (finfo
.indices
!= NULL
)
3725 free (finfo
.indices
);
3726 if (finfo
.sections
!= NULL
)
3727 free (finfo
.sections
);
3728 if (finfo
.symbuf
!= NULL
)
3729 free (finfo
.symbuf
);
3730 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3732 if ((o
->flags
& SEC_RELOC
) != 0
3733 && elf_section_data (o
)->rel_hashes
!= NULL
)
3734 free (elf_section_data (o
)->rel_hashes
);
3737 elf_tdata (abfd
)->linker
= true;
3742 if (finfo
.symstrtab
!= NULL
)
3743 _bfd_stringtab_free (finfo
.symstrtab
);
3744 if (finfo
.contents
!= NULL
)
3745 free (finfo
.contents
);
3746 if (finfo
.external_relocs
!= NULL
)
3747 free (finfo
.external_relocs
);
3748 if (finfo
.internal_relocs
!= NULL
)
3749 free (finfo
.internal_relocs
);
3750 if (finfo
.external_syms
!= NULL
)
3751 free (finfo
.external_syms
);
3752 if (finfo
.internal_syms
!= NULL
)
3753 free (finfo
.internal_syms
);
3754 if (finfo
.indices
!= NULL
)
3755 free (finfo
.indices
);
3756 if (finfo
.sections
!= NULL
)
3757 free (finfo
.sections
);
3758 if (finfo
.symbuf
!= NULL
)
3759 free (finfo
.symbuf
);
3760 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3762 if ((o
->flags
& SEC_RELOC
) != 0
3763 && elf_section_data (o
)->rel_hashes
!= NULL
)
3764 free (elf_section_data (o
)->rel_hashes
);
3770 /* Add a symbol to the output symbol table. */
3773 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
3774 struct elf_final_link_info
*finfo
;
3776 Elf_Internal_Sym
*elfsym
;
3777 asection
*input_sec
;
3779 boolean (*output_symbol_hook
) PARAMS ((bfd
*,
3780 struct bfd_link_info
*info
,
3785 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
3786 elf_backend_link_output_symbol_hook
;
3787 if (output_symbol_hook
!= NULL
)
3789 if (! ((*output_symbol_hook
)
3790 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
3794 if (name
== (const char *) NULL
|| *name
== '\0')
3795 elfsym
->st_name
= 0;
3798 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
3801 if (elfsym
->st_name
== (unsigned long) -1)
3805 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
3807 if (! elf_link_flush_output_syms (finfo
))
3811 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
,
3812 (PTR
) (finfo
->symbuf
+ finfo
->symbuf_count
));
3813 ++finfo
->symbuf_count
;
3815 ++finfo
->output_bfd
->symcount
;
3820 /* Flush the output symbols to the file. */
3823 elf_link_flush_output_syms (finfo
)
3824 struct elf_final_link_info
*finfo
;
3826 if (finfo
->symbuf_count
> 0)
3828 Elf_Internal_Shdr
*symtab
;
3830 symtab
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
3832 if (bfd_seek (finfo
->output_bfd
, symtab
->sh_offset
+ symtab
->sh_size
,
3834 || (bfd_write ((PTR
) finfo
->symbuf
, finfo
->symbuf_count
,
3835 sizeof (Elf_External_Sym
), finfo
->output_bfd
)
3836 != finfo
->symbuf_count
* sizeof (Elf_External_Sym
)))
3839 symtab
->sh_size
+= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
3841 finfo
->symbuf_count
= 0;
3847 /* Add an external symbol to the symbol table. This is called from
3848 the hash table traversal routine. When generating a shared object,
3849 we go through the symbol table twice. The first time we output
3850 anything that might have been forced to local scope in a version
3851 script. The second time we output the symbols that are still
3855 elf_link_output_extsym (h
, data
)
3856 struct elf_link_hash_entry
*h
;
3859 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
3860 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
3862 Elf_Internal_Sym sym
;
3863 asection
*input_sec
;
3865 /* Decide whether to output this symbol in this pass. */
3866 if (eoinfo
->localsyms
)
3868 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
3873 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
3877 /* If we are not creating a shared library, and this symbol is
3878 referenced by a shared library but is not defined anywhere, then
3879 warn that it is undefined. If we do not do this, the runtime
3880 linker will complain that the symbol is undefined when the
3881 program is run. We don't have to worry about symbols that are
3882 referenced by regular files, because we will already have issued
3883 warnings for them. */
3884 if (! finfo
->info
->relocateable
3885 && ! finfo
->info
->shared
3886 && h
->root
.type
== bfd_link_hash_undefined
3887 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
3888 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
3890 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
3891 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
3892 (asection
*) NULL
, 0)))
3894 eoinfo
->failed
= true;
3899 /* We don't want to output symbols that have never been mentioned by
3900 a regular file, or that we have been told to strip. However, if
3901 h->indx is set to -2, the symbol is used by a reloc and we must
3905 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
3906 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
3907 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3908 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
3910 else if (finfo
->info
->strip
== strip_all
3911 || (finfo
->info
->strip
== strip_some
3912 && bfd_hash_lookup (finfo
->info
->keep_hash
,
3913 h
->root
.root
.string
,
3914 false, false) == NULL
))
3919 /* If we're stripping it, and it's not a dynamic symbol, there's
3920 nothing else to do. */
3921 if (strip
&& h
->dynindx
== -1)
3925 sym
.st_size
= h
->size
;
3926 sym
.st_other
= h
->other
;
3927 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
3928 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
3929 else if (h
->root
.type
== bfd_link_hash_undefweak
3930 || h
->root
.type
== bfd_link_hash_defweak
)
3931 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
3933 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
3935 switch (h
->root
.type
)
3938 case bfd_link_hash_new
:
3942 case bfd_link_hash_undefined
:
3943 input_sec
= bfd_und_section_ptr
;
3944 sym
.st_shndx
= SHN_UNDEF
;
3947 case bfd_link_hash_undefweak
:
3948 input_sec
= bfd_und_section_ptr
;
3949 sym
.st_shndx
= SHN_UNDEF
;
3952 case bfd_link_hash_defined
:
3953 case bfd_link_hash_defweak
:
3955 input_sec
= h
->root
.u
.def
.section
;
3956 if (input_sec
->output_section
!= NULL
)
3959 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
3960 input_sec
->output_section
);
3961 if (sym
.st_shndx
== (unsigned short) -1)
3963 eoinfo
->failed
= true;
3967 /* ELF symbols in relocateable files are section relative,
3968 but in nonrelocateable files they are virtual
3970 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
3971 if (! finfo
->info
->relocateable
)
3972 sym
.st_value
+= input_sec
->output_section
->vma
;
3976 BFD_ASSERT (input_sec
->owner
== NULL
3977 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
3978 sym
.st_shndx
= SHN_UNDEF
;
3979 input_sec
= bfd_und_section_ptr
;
3984 case bfd_link_hash_common
:
3985 input_sec
= bfd_com_section_ptr
;
3986 sym
.st_shndx
= SHN_COMMON
;
3987 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
3990 case bfd_link_hash_indirect
:
3991 /* These symbols are created by symbol versioning. They point
3992 to the decorated version of the name. For example, if the
3993 symbol foo@@GNU_1.2 is the default, which should be used when
3994 foo is used with no version, then we add an indirect symbol
3995 foo which points to foo@@GNU_1.2. We ignore these symbols,
3996 since the indirected symbol is already in the hash table. If
3997 the indirect symbol is non-ELF, fall through and output it. */
3998 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) == 0)
4002 case bfd_link_hash_warning
:
4003 /* We can't represent these symbols in ELF, although a warning
4004 symbol may have come from a .gnu.warning.SYMBOL section. We
4005 just put the target symbol in the hash table. If the target
4006 symbol does not really exist, don't do anything. */
4007 if (h
->root
.u
.i
.link
->type
== bfd_link_hash_new
)
4009 return (elf_link_output_extsym
4010 ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
));
4013 /* If this symbol should be put in the .dynsym section, then put it
4014 there now. We have already know the symbol index. We also fill
4015 in the entry in the .hash section. */
4016 if (h
->dynindx
!= -1
4017 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4019 struct elf_backend_data
*bed
;
4024 bfd_byte
*bucketpos
;
4027 sym
.st_name
= h
->dynstr_index
;
4029 /* Give the processor backend a chance to tweak the symbol
4030 value, and also to finish up anything that needs to be done
4032 bed
= get_elf_backend_data (finfo
->output_bfd
);
4033 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
4034 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
4036 eoinfo
->failed
= true;
4040 elf_swap_symbol_out (finfo
->output_bfd
, &sym
,
4041 (PTR
) (((Elf_External_Sym
*)
4042 finfo
->dynsym_sec
->contents
)
4045 /* We didn't include the version string in the dynamic string
4046 table, so we must not consider it in the hash table. */
4047 name
= h
->root
.root
.string
;
4048 p
= strchr (name
, ELF_VER_CHR
);
4053 copy
= bfd_alloc (finfo
->output_bfd
, p
- name
+ 1);
4054 strncpy (copy
, name
, p
- name
);
4055 copy
[p
- name
] = '\0';
4059 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
4060 bucket
= bfd_elf_hash ((const unsigned char *) name
) % bucketcount
;
4061 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
4062 + (bucket
+ 2) * (ARCH_SIZE
/ 8));
4063 chain
= get_word (finfo
->output_bfd
, bucketpos
);
4064 put_word (finfo
->output_bfd
, h
->dynindx
, bucketpos
);
4065 put_word (finfo
->output_bfd
, chain
,
4066 ((bfd_byte
*) finfo
->hash_sec
->contents
4067 + (bucketcount
+ 2 + h
->dynindx
) * (ARCH_SIZE
/ 8)));
4070 bfd_release (finfo
->output_bfd
, copy
);
4072 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
4074 Elf_Internal_Versym iversym
;
4076 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4078 if (h
->verinfo
.verdef
== NULL
)
4079 iversym
.vs_vers
= 0;
4081 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4085 if (h
->verinfo
.vertree
== NULL
)
4086 iversym
.vs_vers
= 1;
4088 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
4091 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
4092 iversym
.vs_vers
|= VERSYM_HIDDEN
;
4094 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
,
4095 (((Elf_External_Versym
*)
4096 finfo
->symver_sec
->contents
)
4101 /* If we're stripping it, then it was just a dynamic symbol, and
4102 there's nothing else to do. */
4106 h
->indx
= finfo
->output_bfd
->symcount
;
4108 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
4110 eoinfo
->failed
= true;
4117 /* Link an input file into the linker output file. This function
4118 handles all the sections and relocations of the input file at once.
4119 This is so that we only have to read the local symbols once, and
4120 don't have to keep them in memory. */
4123 elf_link_input_bfd (finfo
, input_bfd
)
4124 struct elf_final_link_info
*finfo
;
4127 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
4128 bfd
*, asection
*, bfd_byte
*,
4129 Elf_Internal_Rela
*,
4130 Elf_Internal_Sym
*, asection
**));
4132 Elf_Internal_Shdr
*symtab_hdr
;
4135 Elf_External_Sym
*external_syms
;
4136 Elf_External_Sym
*esym
;
4137 Elf_External_Sym
*esymend
;
4138 Elf_Internal_Sym
*isym
;
4140 asection
**ppsection
;
4143 output_bfd
= finfo
->output_bfd
;
4145 get_elf_backend_data (output_bfd
)->elf_backend_relocate_section
;
4147 /* If this is a dynamic object, we don't want to do anything here:
4148 we don't want the local symbols, and we don't want the section
4150 if ((input_bfd
->flags
& DYNAMIC
) != 0)
4153 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
4154 if (elf_bad_symtab (input_bfd
))
4156 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
4161 locsymcount
= symtab_hdr
->sh_info
;
4162 extsymoff
= symtab_hdr
->sh_info
;
4165 /* Read the local symbols. */
4166 if (symtab_hdr
->contents
!= NULL
)
4167 external_syms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
4168 else if (locsymcount
== 0)
4169 external_syms
= NULL
;
4172 external_syms
= finfo
->external_syms
;
4173 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
4174 || (bfd_read (external_syms
, sizeof (Elf_External_Sym
),
4175 locsymcount
, input_bfd
)
4176 != locsymcount
* sizeof (Elf_External_Sym
)))
4180 /* Swap in the local symbols and write out the ones which we know
4181 are going into the output file. */
4182 esym
= external_syms
;
4183 esymend
= esym
+ locsymcount
;
4184 isym
= finfo
->internal_syms
;
4185 pindex
= finfo
->indices
;
4186 ppsection
= finfo
->sections
;
4187 for (; esym
< esymend
; esym
++, isym
++, pindex
++, ppsection
++)
4191 Elf_Internal_Sym osym
;
4193 elf_swap_symbol_in (input_bfd
, esym
, isym
);
4196 if (elf_bad_symtab (input_bfd
))
4198 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
4205 if (isym
->st_shndx
== SHN_UNDEF
)
4206 isec
= bfd_und_section_ptr
;
4207 else if (isym
->st_shndx
> 0 && isym
->st_shndx
< SHN_LORESERVE
)
4208 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
4209 else if (isym
->st_shndx
== SHN_ABS
)
4210 isec
= bfd_abs_section_ptr
;
4211 else if (isym
->st_shndx
== SHN_COMMON
)
4212 isec
= bfd_com_section_ptr
;
4221 /* Don't output the first, undefined, symbol. */
4222 if (esym
== external_syms
)
4225 /* If we are stripping all symbols, we don't want to output this
4227 if (finfo
->info
->strip
== strip_all
)
4230 /* We never output section symbols. Instead, we use the section
4231 symbol of the corresponding section in the output file. */
4232 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
4235 /* If we are discarding all local symbols, we don't want to
4236 output this one. If we are generating a relocateable output
4237 file, then some of the local symbols may be required by
4238 relocs; we output them below as we discover that they are
4240 if (finfo
->info
->discard
== discard_all
)
4243 /* If this symbol is defined in a section which we are
4244 discarding, we don't need to keep it, but note that
4245 linker_mark is only reliable for sections that have contents.
4246 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4247 as well as linker_mark. */
4248 if (isym
->st_shndx
> 0
4249 && isym
->st_shndx
< SHN_LORESERVE
4251 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
4252 || (! finfo
->info
->relocateable
4253 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
4256 /* Get the name of the symbol. */
4257 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
4262 /* See if we are discarding symbols with this name. */
4263 if ((finfo
->info
->strip
== strip_some
4264 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
4266 || (finfo
->info
->discard
== discard_l
4267 && bfd_is_local_label_name (input_bfd
, name
)))
4270 /* If we get here, we are going to output this symbol. */
4274 /* Adjust the section index for the output file. */
4275 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
4276 isec
->output_section
);
4277 if (osym
.st_shndx
== (unsigned short) -1)
4280 *pindex
= output_bfd
->symcount
;
4282 /* ELF symbols in relocateable files are section relative, but
4283 in executable files they are virtual addresses. Note that
4284 this code assumes that all ELF sections have an associated
4285 BFD section with a reasonable value for output_offset; below
4286 we assume that they also have a reasonable value for
4287 output_section. Any special sections must be set up to meet
4288 these requirements. */
4289 osym
.st_value
+= isec
->output_offset
;
4290 if (! finfo
->info
->relocateable
)
4291 osym
.st_value
+= isec
->output_section
->vma
;
4293 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
4297 /* Relocate the contents of each section. */
4298 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4302 if (! o
->linker_mark
)
4304 /* This section was omitted from the link. */
4308 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4309 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
4312 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
4314 /* Section was created by elf_link_create_dynamic_sections
4319 /* Get the contents of the section. They have been cached by a
4320 relaxation routine. Note that o is a section in an input
4321 file, so the contents field will not have been set by any of
4322 the routines which work on output files. */
4323 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
4324 contents
= elf_section_data (o
)->this_hdr
.contents
;
4327 contents
= finfo
->contents
;
4328 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
4329 (file_ptr
) 0, o
->_raw_size
))
4333 if ((o
->flags
& SEC_RELOC
) != 0)
4335 Elf_Internal_Rela
*internal_relocs
;
4337 /* Get the swapped relocs. */
4338 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
4339 (input_bfd
, o
, finfo
->external_relocs
,
4340 finfo
->internal_relocs
, false));
4341 if (internal_relocs
== NULL
4342 && o
->reloc_count
> 0)
4345 /* Relocate the section by invoking a back end routine.
4347 The back end routine is responsible for adjusting the
4348 section contents as necessary, and (if using Rela relocs
4349 and generating a relocateable output file) adjusting the
4350 reloc addend as necessary.
4352 The back end routine does not have to worry about setting
4353 the reloc address or the reloc symbol index.
4355 The back end routine is given a pointer to the swapped in
4356 internal symbols, and can access the hash table entries
4357 for the external symbols via elf_sym_hashes (input_bfd).
4359 When generating relocateable output, the back end routine
4360 must handle STB_LOCAL/STT_SECTION symbols specially. The
4361 output symbol is going to be a section symbol
4362 corresponding to the output section, which will require
4363 the addend to be adjusted. */
4365 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
4366 input_bfd
, o
, contents
,
4368 finfo
->internal_syms
,
4372 if (finfo
->info
->relocateable
)
4374 Elf_Internal_Rela
*irela
;
4375 Elf_Internal_Rela
*irelaend
;
4376 struct elf_link_hash_entry
**rel_hash
;
4377 Elf_Internal_Shdr
*input_rel_hdr
;
4378 Elf_Internal_Shdr
*output_rel_hdr
;
4380 /* Adjust the reloc addresses and symbol indices. */
4382 irela
= internal_relocs
;
4383 irelaend
= irela
+ o
->reloc_count
;
4384 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
4385 + o
->output_section
->reloc_count
);
4386 for (; irela
< irelaend
; irela
++, rel_hash
++)
4388 unsigned long r_symndx
;
4389 Elf_Internal_Sym
*isym
;
4392 irela
->r_offset
+= o
->output_offset
;
4394 r_symndx
= ELF_R_SYM (irela
->r_info
);
4399 if (r_symndx
>= locsymcount
4400 || (elf_bad_symtab (input_bfd
)
4401 && finfo
->sections
[r_symndx
] == NULL
))
4405 /* This is a reloc against a global symbol. We
4406 have not yet output all the local symbols, so
4407 we do not know the symbol index of any global
4408 symbol. We set the rel_hash entry for this
4409 reloc to point to the global hash table entry
4410 for this symbol. The symbol index is then
4411 set at the end of elf_bfd_final_link. */
4412 indx
= r_symndx
- extsymoff
;
4413 *rel_hash
= elf_sym_hashes (input_bfd
)[indx
];
4415 /* Setting the index to -2 tells
4416 elf_link_output_extsym that this symbol is
4418 BFD_ASSERT ((*rel_hash
)->indx
< 0);
4419 (*rel_hash
)->indx
= -2;
4424 /* This is a reloc against a local symbol. */
4427 isym
= finfo
->internal_syms
+ r_symndx
;
4428 sec
= finfo
->sections
[r_symndx
];
4429 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
4431 /* I suppose the backend ought to fill in the
4432 section of any STT_SECTION symbol against a
4433 processor specific section. If we have
4434 discarded a section, the output_section will
4435 be the absolute section. */
4437 && (bfd_is_abs_section (sec
)
4438 || (sec
->output_section
!= NULL
4439 && bfd_is_abs_section (sec
->output_section
))))
4441 else if (sec
== NULL
|| sec
->owner
== NULL
)
4443 bfd_set_error (bfd_error_bad_value
);
4448 r_symndx
= sec
->output_section
->target_index
;
4449 BFD_ASSERT (r_symndx
!= 0);
4454 if (finfo
->indices
[r_symndx
] == -1)
4460 if (finfo
->info
->strip
== strip_all
)
4462 /* You can't do ld -r -s. */
4463 bfd_set_error (bfd_error_invalid_operation
);
4467 /* This symbol was skipped earlier, but
4468 since it is needed by a reloc, we
4469 must output it now. */
4470 link
= symtab_hdr
->sh_link
;
4471 name
= bfd_elf_string_from_elf_section (input_bfd
,
4477 osec
= sec
->output_section
;
4479 _bfd_elf_section_from_bfd_section (output_bfd
,
4481 if (isym
->st_shndx
== (unsigned short) -1)
4484 isym
->st_value
+= sec
->output_offset
;
4485 if (! finfo
->info
->relocateable
)
4486 isym
->st_value
+= osec
->vma
;
4488 finfo
->indices
[r_symndx
] = output_bfd
->symcount
;
4490 if (! elf_link_output_sym (finfo
, name
, isym
, sec
))
4494 r_symndx
= finfo
->indices
[r_symndx
];
4497 irela
->r_info
= ELF_R_INFO (r_symndx
,
4498 ELF_R_TYPE (irela
->r_info
));
4501 /* Swap out the relocs. */
4502 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
4503 output_rel_hdr
= &elf_section_data (o
->output_section
)->rel_hdr
;
4504 BFD_ASSERT (output_rel_hdr
->sh_entsize
4505 == input_rel_hdr
->sh_entsize
);
4506 irela
= internal_relocs
;
4507 irelaend
= irela
+ o
->reloc_count
;
4508 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4510 Elf_External_Rel
*erel
;
4512 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
4513 + o
->output_section
->reloc_count
);
4514 for (; irela
< irelaend
; irela
++, erel
++)
4516 Elf_Internal_Rel irel
;
4518 irel
.r_offset
= irela
->r_offset
;
4519 irel
.r_info
= irela
->r_info
;
4520 BFD_ASSERT (irela
->r_addend
== 0);
4521 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
4526 Elf_External_Rela
*erela
;
4528 BFD_ASSERT (input_rel_hdr
->sh_entsize
4529 == sizeof (Elf_External_Rela
));
4530 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
4531 + o
->output_section
->reloc_count
);
4532 for (; irela
< irelaend
; irela
++, erela
++)
4533 elf_swap_reloca_out (output_bfd
, irela
, erela
);
4536 o
->output_section
->reloc_count
+= o
->reloc_count
;
4540 /* Write out the modified section contents. */
4541 if (elf_section_data (o
)->stab_info
== NULL
)
4543 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
4544 contents
, o
->output_offset
,
4545 (o
->_cooked_size
!= 0
4552 if (! (_bfd_write_section_stabs
4553 (output_bfd
, &elf_hash_table (finfo
->info
)->stab_info
,
4554 o
, &elf_section_data (o
)->stab_info
, contents
)))
4562 /* Generate a reloc when linking an ELF file. This is a reloc
4563 requested by the linker, and does come from any input file. This
4564 is used to build constructor and destructor tables when linking
4568 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
4570 struct bfd_link_info
*info
;
4571 asection
*output_section
;
4572 struct bfd_link_order
*link_order
;
4574 reloc_howto_type
*howto
;
4578 struct elf_link_hash_entry
**rel_hash_ptr
;
4579 Elf_Internal_Shdr
*rel_hdr
;
4581 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
4584 bfd_set_error (bfd_error_bad_value
);
4588 addend
= link_order
->u
.reloc
.p
->addend
;
4590 /* Figure out the symbol index. */
4591 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
4592 + output_section
->reloc_count
);
4593 if (link_order
->type
== bfd_section_reloc_link_order
)
4595 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
4596 BFD_ASSERT (indx
!= 0);
4597 *rel_hash_ptr
= NULL
;
4601 struct elf_link_hash_entry
*h
;
4603 /* Treat a reloc against a defined symbol as though it were
4604 actually against the section. */
4605 h
= ((struct elf_link_hash_entry
*)
4606 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
4607 link_order
->u
.reloc
.p
->u
.name
,
4608 false, false, true));
4610 && (h
->root
.type
== bfd_link_hash_defined
4611 || h
->root
.type
== bfd_link_hash_defweak
))
4615 section
= h
->root
.u
.def
.section
;
4616 indx
= section
->output_section
->target_index
;
4617 *rel_hash_ptr
= NULL
;
4618 /* It seems that we ought to add the symbol value to the
4619 addend here, but in practice it has already been added
4620 because it was passed to constructor_callback. */
4621 addend
+= section
->output_section
->vma
+ section
->output_offset
;
4625 /* Setting the index to -2 tells elf_link_output_extsym that
4626 this symbol is used by a reloc. */
4633 if (! ((*info
->callbacks
->unattached_reloc
)
4634 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
4635 (asection
*) NULL
, (bfd_vma
) 0)))
4641 /* If this is an inplace reloc, we must write the addend into the
4643 if (howto
->partial_inplace
&& addend
!= 0)
4646 bfd_reloc_status_type rstat
;
4650 size
= bfd_get_reloc_size (howto
);
4651 buf
= (bfd_byte
*) bfd_zmalloc (size
);
4652 if (buf
== (bfd_byte
*) NULL
)
4654 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
4660 case bfd_reloc_outofrange
:
4662 case bfd_reloc_overflow
:
4663 if (! ((*info
->callbacks
->reloc_overflow
)
4665 (link_order
->type
== bfd_section_reloc_link_order
4666 ? bfd_section_name (output_bfd
,
4667 link_order
->u
.reloc
.p
->u
.section
)
4668 : link_order
->u
.reloc
.p
->u
.name
),
4669 howto
->name
, addend
, (bfd
*) NULL
, (asection
*) NULL
,
4677 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
4678 (file_ptr
) link_order
->offset
, size
);
4684 /* The address of a reloc is relative to the section in a
4685 relocateable file, and is a virtual address in an executable
4687 offset
= link_order
->offset
;
4688 if (! info
->relocateable
)
4689 offset
+= output_section
->vma
;
4691 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
4693 if (rel_hdr
->sh_type
== SHT_REL
)
4695 Elf_Internal_Rel irel
;
4696 Elf_External_Rel
*erel
;
4698 irel
.r_offset
= offset
;
4699 irel
.r_info
= ELF_R_INFO (indx
, howto
->type
);
4700 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
4701 + output_section
->reloc_count
);
4702 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
4706 Elf_Internal_Rela irela
;
4707 Elf_External_Rela
*erela
;
4709 irela
.r_offset
= offset
;
4710 irela
.r_info
= ELF_R_INFO (indx
, howto
->type
);
4711 irela
.r_addend
= addend
;
4712 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
4713 + output_section
->reloc_count
);
4714 elf_swap_reloca_out (output_bfd
, &irela
, erela
);
4717 ++output_section
->reloc_count
;
4723 /* Allocate a pointer to live in a linker created section. */
4726 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
4728 struct bfd_link_info
*info
;
4729 elf_linker_section_t
*lsect
;
4730 struct elf_link_hash_entry
*h
;
4731 const Elf_Internal_Rela
*rel
;
4733 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
4734 elf_linker_section_pointers_t
*linker_section_ptr
;
4735 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);;
4737 BFD_ASSERT (lsect
!= NULL
);
4739 /* Is this a global symbol? */
4742 /* Has this symbol already been allocated, if so, our work is done */
4743 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
4748 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
4749 /* Make sure this symbol is output as a dynamic symbol. */
4750 if (h
->dynindx
== -1)
4752 if (! elf_link_record_dynamic_symbol (info
, h
))
4756 if (lsect
->rel_section
)
4757 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
4760 else /* Allocation of a pointer to a local symbol */
4762 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
4764 /* Allocate a table to hold the local symbols if first time */
4767 int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
4768 register unsigned int i
;
4770 ptr
= (elf_linker_section_pointers_t
**)
4771 bfd_alloc (abfd
, num_symbols
* sizeof (elf_linker_section_pointers_t
*));
4776 elf_local_ptr_offsets (abfd
) = ptr
;
4777 for (i
= 0; i
< num_symbols
; i
++)
4778 ptr
[i
] = (elf_linker_section_pointers_t
*)0;
4781 /* Has this symbol already been allocated, if so, our work is done */
4782 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
4787 ptr_linker_section_ptr
= &ptr
[r_symndx
];
4791 /* If we are generating a shared object, we need to
4792 output a R_<xxx>_RELATIVE reloc so that the
4793 dynamic linker can adjust this GOT entry. */
4794 BFD_ASSERT (lsect
->rel_section
!= NULL
);
4795 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
4799 /* Allocate space for a pointer in the linker section, and allocate a new pointer record
4800 from internal memory. */
4801 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
4802 linker_section_ptr
= (elf_linker_section_pointers_t
*)
4803 bfd_alloc (abfd
, sizeof (elf_linker_section_pointers_t
));
4805 if (!linker_section_ptr
)
4808 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
4809 linker_section_ptr
->addend
= rel
->r_addend
;
4810 linker_section_ptr
->which
= lsect
->which
;
4811 linker_section_ptr
->written_address_p
= false;
4812 *ptr_linker_section_ptr
= linker_section_ptr
;
4815 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
4817 linker_section_ptr
->offset
= lsect
->section
->_raw_size
- lsect
->hole_size
+ (ARCH_SIZE
/ 8);
4818 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
4819 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
4820 if (lsect
->sym_hash
) /* Bump up symbol value if needed */
4822 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
4824 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
4825 lsect
->sym_hash
->root
.root
.string
,
4826 (long)ARCH_SIZE
/ 8,
4827 (long)lsect
->sym_hash
->root
.u
.def
.value
);
4833 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
4835 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
4838 fprintf (stderr
, "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
4839 lsect
->name
, (long)linker_section_ptr
->offset
, (long)lsect
->section
->_raw_size
);
4847 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
4850 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
4853 /* Fill in the address for a pointer generated in alinker section. */
4856 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
, relocation
, rel
, relative_reloc
)
4859 struct bfd_link_info
*info
;
4860 elf_linker_section_t
*lsect
;
4861 struct elf_link_hash_entry
*h
;
4863 const Elf_Internal_Rela
*rel
;
4866 elf_linker_section_pointers_t
*linker_section_ptr
;
4868 BFD_ASSERT (lsect
!= NULL
);
4870 if (h
!= NULL
) /* global symbol */
4872 linker_section_ptr
= _bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
4876 BFD_ASSERT (linker_section_ptr
!= NULL
);
4878 if (! elf_hash_table (info
)->dynamic_sections_created
4881 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
4883 /* This is actually a static link, or it is a
4884 -Bsymbolic link and the symbol is defined
4885 locally. We must initialize this entry in the
4888 When doing a dynamic link, we create a .rela.<xxx>
4889 relocation entry to initialize the value. This
4890 is done in the finish_dynamic_symbol routine. */
4891 if (!linker_section_ptr
->written_address_p
)
4893 linker_section_ptr
->written_address_p
= true;
4894 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
4895 lsect
->section
->contents
+ linker_section_ptr
->offset
);
4899 else /* local symbol */
4901 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
4902 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
4903 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
4904 linker_section_ptr
= _bfd_elf_find_pointer_linker_section (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
4908 BFD_ASSERT (linker_section_ptr
!= NULL
);
4910 /* Write out pointer if it hasn't been rewritten out before */
4911 if (!linker_section_ptr
->written_address_p
)
4913 linker_section_ptr
->written_address_p
= true;
4914 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
4915 lsect
->section
->contents
+ linker_section_ptr
->offset
);
4919 asection
*srel
= lsect
->rel_section
;
4920 Elf_Internal_Rela outrel
;
4922 /* We need to generate a relative reloc for the dynamic linker. */
4924 lsect
->rel_section
= srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
4927 BFD_ASSERT (srel
!= NULL
);
4929 outrel
.r_offset
= (lsect
->section
->output_section
->vma
4930 + lsect
->section
->output_offset
4931 + linker_section_ptr
->offset
);
4932 outrel
.r_info
= ELF_R_INFO (0, relative_reloc
);
4933 outrel
.r_addend
= 0;
4934 elf_swap_reloca_out (output_bfd
, &outrel
,
4935 (((Elf_External_Rela
*)
4936 lsect
->section
->contents
)
4937 + lsect
->section
->reloc_count
));
4938 ++lsect
->section
->reloc_count
;
4943 relocation
= (lsect
->section
->output_offset
4944 + linker_section_ptr
->offset
4945 - lsect
->hole_offset
4946 - lsect
->sym_offset
);
4949 fprintf (stderr
, "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
4950 lsect
->name
, (long)relocation
, (long)relocation
);
4953 /* Subtract out the addend, because it will get added back in by the normal
4955 return relocation
- linker_section_ptr
->addend
;