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, hdr
->sh_size
, abfd
)
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
+ hdr
->sh_info
: 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
)))
809 verstr
= elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
813 namelen
= strlen (name
);
814 newlen
= namelen
+ strlen (verstr
) + 2;
815 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
818 newname
= (char *) bfd_alloc (abfd
, newlen
);
821 strcpy (newname
, name
);
822 p
= newname
+ namelen
;
824 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
832 /* We need to look up the symbol now in order to get some of
833 the dynamic object handling right. We pass the hash
834 table entry in to _bfd_generic_link_add_one_symbol so
835 that it does not have to look it up again. */
836 if (! bfd_is_und_section (sec
))
837 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
840 h
= ((struct elf_link_hash_entry
*)
841 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true,
847 if (h
->root
.type
== bfd_link_hash_new
)
848 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
850 while (h
->root
.type
== bfd_link_hash_indirect
851 || h
->root
.type
== bfd_link_hash_warning
)
852 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
854 /* It's OK to change the type if it used to be a weak
856 if (h
->root
.type
== bfd_link_hash_defweak
857 || h
->root
.type
== bfd_link_hash_undefweak
)
858 type_change_ok
= true;
860 /* It's OK to change the size if it used to be a weak
861 definition, or if it used to be undefined, or if we will
862 be overriding an old definition. */
864 || h
->root
.type
== bfd_link_hash_undefined
)
865 size_change_ok
= true;
869 /* If we are looking at a dynamic object, and this is a
870 definition, we need to see if it has already been defined
871 by some other object. If it has, we want to use the
872 existing definition, and we do not want to report a
873 multiple symbol definition error; we do this by
874 clobbering sec to be bfd_und_section_ptr. We treat a
875 common symbol as a definition if the symbol in the shared
876 library is a function, since common symbols always
877 represent variables; this can cause confusion in
878 principle, but any such confusion would seem to indicate
879 an erroneous program or shared library. */
880 if (dynamic
&& definition
)
882 if (h
->root
.type
== bfd_link_hash_defined
883 || h
->root
.type
== bfd_link_hash_defweak
884 || (h
->root
.type
== bfd_link_hash_common
886 || ELF_ST_TYPE (sym
.st_info
) == STT_FUNC
)))
889 sec
= bfd_und_section_ptr
;
891 size_change_ok
= true;
892 if (h
->root
.type
== bfd_link_hash_common
)
893 type_change_ok
= true;
897 /* Similarly, if we are not looking at a dynamic object, and
898 we have a definition, we want to override any definition
899 we may have from a dynamic object. Symbols from regular
900 files always take precedence over symbols from dynamic
901 objects, even if they are defined after the dynamic
902 object in the link. */
905 || (bfd_is_com_section (sec
)
906 && (h
->root
.type
== bfd_link_hash_defweak
907 || h
->type
== STT_FUNC
)))
908 && (h
->root
.type
== bfd_link_hash_defined
909 || h
->root
.type
== bfd_link_hash_defweak
)
910 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
911 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) != 0)
914 /* Change the hash table entry to undefined, and let
915 _bfd_generic_link_add_one_symbol do the right thing
916 with the new definition. */
917 h
->root
.type
= bfd_link_hash_undefined
;
918 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
919 size_change_ok
= true;
920 if (bfd_is_com_section (sec
))
921 type_change_ok
= true;
923 /* This union may have been set to be non-NULL when this
924 symbol was seen in a dynamic object. We must force
925 the union to be NULL, so that it is correct for a
927 h
->verinfo
.vertree
= NULL
;
933 && (h
->verinfo
.verdef
== NULL
|| definition
))
934 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
937 if (! (_bfd_generic_link_add_one_symbol
938 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
939 false, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
943 while (h
->root
.type
== bfd_link_hash_indirect
944 || h
->root
.type
== bfd_link_hash_warning
)
945 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
951 && (flags
& BSF_WEAK
) != 0
952 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
953 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
954 && h
->weakdef
== NULL
)
956 /* Keep a list of all weak defined non function symbols from
957 a dynamic object, using the weakdef field. Later in this
958 function we will set the weakdef field to the correct
959 value. We only put non-function symbols from dynamic
960 objects on this list, because that happens to be the only
961 time we need to know the normal symbol corresponding to a
962 weak symbol, and the information is time consuming to
963 figure out. If the weakdef field is not already NULL,
964 then this symbol was already defined by some previous
965 dynamic object, and we will be using that previous
966 definition anyhow. */
973 /* Get the alignment of a common symbol. */
974 if (sym
.st_shndx
== SHN_COMMON
975 && h
->root
.type
== bfd_link_hash_common
)
976 h
->root
.u
.c
.p
->alignment_power
= bfd_log2 (sym
.st_value
);
978 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
984 /* Remember the symbol size and type. */
986 && (definition
|| h
->size
== 0))
988 if (h
->size
!= 0 && h
->size
!= sym
.st_size
&& ! size_change_ok
)
989 (*_bfd_error_handler
)
990 ("Warning: size of symbol `%s' changed from %lu to %lu in %s",
991 name
, (unsigned long) h
->size
, (unsigned long) sym
.st_size
,
992 bfd_get_filename (abfd
));
994 h
->size
= sym
.st_size
;
996 if (ELF_ST_TYPE (sym
.st_info
) != STT_NOTYPE
997 && (definition
|| h
->type
== STT_NOTYPE
))
999 if (h
->type
!= STT_NOTYPE
1000 && h
->type
!= ELF_ST_TYPE (sym
.st_info
)
1001 && ! type_change_ok
)
1002 (*_bfd_error_handler
)
1003 ("Warning: type of symbol `%s' changed from %d to %d in %s",
1004 name
, h
->type
, ELF_ST_TYPE (sym
.st_info
),
1005 bfd_get_filename (abfd
));
1007 h
->type
= ELF_ST_TYPE (sym
.st_info
);
1010 if (sym
.st_other
!= 0
1011 && (definition
|| h
->other
== 0))
1012 h
->other
= sym
.st_other
;
1014 /* Set a flag in the hash table entry indicating the type of
1015 reference or definition we just found. Keep a count of
1016 the number of dynamic symbols we find. A dynamic symbol
1017 is one which is referenced or defined by both a regular
1018 object and a shared object. */
1019 old_flags
= h
->elf_link_hash_flags
;
1024 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
1026 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
1028 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1029 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
1035 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
1037 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
1038 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
1039 | ELF_LINK_HASH_REF_REGULAR
)) != 0
1040 || (h
->weakdef
!= NULL
1042 && h
->weakdef
->dynindx
!= -1))
1046 h
->elf_link_hash_flags
|= new_flag
;
1048 /* If this symbol has a version, and it is the default
1049 version, we create an indirect symbol from the default
1050 name to the fully decorated name. This will cause
1051 external references which do not specify a version to be
1052 bound to this version of the symbol. */
1057 p
= strchr (name
, ELF_VER_CHR
);
1058 if (p
!= NULL
&& p
[1] == ELF_VER_CHR
)
1061 struct elf_link_hash_entry
*hi
;
1063 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1065 if (shortname
== NULL
)
1067 strncpy (shortname
, name
, p
- name
);
1068 shortname
[p
- name
] = '\0';
1071 if (! (_bfd_generic_link_add_one_symbol
1072 (info
, abfd
, shortname
, BSF_INDIRECT
,
1073 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1074 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1077 if (hi
->root
.type
== bfd_link_hash_indirect
)
1078 hi
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
1082 if (dynsym
&& h
->dynindx
== -1)
1084 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1086 if (h
->weakdef
!= NULL
1088 && h
->weakdef
->dynindx
== -1)
1090 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1098 /* Now set the weakdefs field correctly for all the weak defined
1099 symbols we found. The only way to do this is to search all the
1100 symbols. Since we only need the information for non functions in
1101 dynamic objects, that's the only time we actually put anything on
1102 the list WEAKS. We need this information so that if a regular
1103 object refers to a symbol defined weakly in a dynamic object, the
1104 real symbol in the dynamic object is also put in the dynamic
1105 symbols; we also must arrange for both symbols to point to the
1106 same memory location. We could handle the general case of symbol
1107 aliasing, but a general symbol alias can only be generated in
1108 assembler code, handling it correctly would be very time
1109 consuming, and other ELF linkers don't handle general aliasing
1111 while (weaks
!= NULL
)
1113 struct elf_link_hash_entry
*hlook
;
1116 struct elf_link_hash_entry
**hpp
;
1117 struct elf_link_hash_entry
**hppend
;
1120 weaks
= hlook
->weakdef
;
1121 hlook
->weakdef
= NULL
;
1123 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
1124 || hlook
->root
.type
== bfd_link_hash_defweak
1125 || hlook
->root
.type
== bfd_link_hash_common
1126 || hlook
->root
.type
== bfd_link_hash_indirect
);
1127 slook
= hlook
->root
.u
.def
.section
;
1128 vlook
= hlook
->root
.u
.def
.value
;
1130 hpp
= elf_sym_hashes (abfd
);
1131 hppend
= hpp
+ extsymcount
;
1132 for (; hpp
< hppend
; hpp
++)
1134 struct elf_link_hash_entry
*h
;
1137 if (h
!= NULL
&& h
!= hlook
1138 && h
->root
.type
== bfd_link_hash_defined
1139 && h
->root
.u
.def
.section
== slook
1140 && h
->root
.u
.def
.value
== vlook
)
1144 /* If the weak definition is in the list of dynamic
1145 symbols, make sure the real definition is put there
1147 if (hlook
->dynindx
!= -1
1148 && h
->dynindx
== -1)
1150 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1154 /* If the real definition is in the list of dynamic
1155 symbols, make sure the weak definition is put there
1156 as well. If we don't do this, then the dynamic
1157 loader might not merge the entries for the real
1158 definition and the weak definition. */
1159 if (h
->dynindx
!= -1
1160 && hlook
->dynindx
== -1)
1162 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
1177 if (extversym
!= NULL
)
1183 /* If this object is the same format as the output object, and it is
1184 not a shared library, then let the backend look through the
1187 This is required to build global offset table entries and to
1188 arrange for dynamic relocs. It is not required for the
1189 particular common case of linking non PIC code, even when linking
1190 against shared libraries, but unfortunately there is no way of
1191 knowing whether an object file has been compiled PIC or not.
1192 Looking through the relocs is not particularly time consuming.
1193 The problem is that we must either (1) keep the relocs in memory,
1194 which causes the linker to require additional runtime memory or
1195 (2) read the relocs twice from the input file, which wastes time.
1196 This would be a good case for using mmap.
1198 I have no idea how to handle linking PIC code into a file of a
1199 different format. It probably can't be done. */
1200 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
1202 && abfd
->xvec
== info
->hash
->creator
1203 && check_relocs
!= NULL
)
1207 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1209 Elf_Internal_Rela
*internal_relocs
;
1212 if ((o
->flags
& SEC_RELOC
) == 0
1213 || o
->reloc_count
== 0)
1216 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
1217 (abfd
, o
, (PTR
) NULL
,
1218 (Elf_Internal_Rela
*) NULL
,
1219 info
->keep_memory
));
1220 if (internal_relocs
== NULL
)
1223 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
1225 if (! info
->keep_memory
)
1226 free (internal_relocs
);
1233 /* If this is a non-traditional, non-relocateable link, try to
1234 optimize the handling of the .stab/.stabstr sections. */
1236 && ! info
->relocateable
1237 && ! info
->traditional_format
1238 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1239 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
1241 asection
*stab
, *stabstr
;
1243 stab
= bfd_get_section_by_name (abfd
, ".stab");
1246 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
1248 if (stabstr
!= NULL
)
1250 struct bfd_elf_section_data
*secdata
;
1252 secdata
= elf_section_data (stab
);
1253 if (! _bfd_link_section_stabs (abfd
,
1254 &elf_hash_table (info
)->stab_info
,
1256 &secdata
->stab_info
))
1271 if (extversym
!= NULL
)
1276 /* Create some sections which will be filled in with dynamic linking
1277 information. ABFD is an input file which requires dynamic sections
1278 to be created. The dynamic sections take up virtual memory space
1279 when the final executable is run, so we need to create them before
1280 addresses are assigned to the output sections. We work out the
1281 actual contents and size of these sections later. */
1284 elf_link_create_dynamic_sections (abfd
, info
)
1286 struct bfd_link_info
*info
;
1289 register asection
*s
;
1290 struct elf_link_hash_entry
*h
;
1291 struct elf_backend_data
*bed
;
1293 if (elf_hash_table (info
)->dynamic_sections_created
)
1296 /* Make sure that all dynamic sections use the same input BFD. */
1297 if (elf_hash_table (info
)->dynobj
== NULL
)
1298 elf_hash_table (info
)->dynobj
= abfd
;
1300 abfd
= elf_hash_table (info
)->dynobj
;
1302 /* Note that we set the SEC_IN_MEMORY flag for all of these
1304 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1305 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
1307 /* A dynamically linked executable has a .interp section, but a
1308 shared library does not. */
1311 s
= bfd_make_section (abfd
, ".interp");
1313 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
1317 /* Create sections to hold version informations. These are removed
1318 if they are not needed. */
1319 s
= bfd_make_section (abfd
, ".gnu.version_d");
1321 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1322 || ! bfd_set_section_alignment (abfd
, s
, 2))
1325 s
= bfd_make_section (abfd
, ".gnu.version");
1327 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1328 || ! bfd_set_section_alignment (abfd
, s
, 1))
1331 s
= bfd_make_section (abfd
, ".gnu.version_r");
1333 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1334 || ! bfd_set_section_alignment (abfd
, s
, 2))
1337 s
= bfd_make_section (abfd
, ".dynsym");
1339 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1340 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1343 s
= bfd_make_section (abfd
, ".dynstr");
1345 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
1348 /* Create a strtab to hold the dynamic symbol names. */
1349 if (elf_hash_table (info
)->dynstr
== NULL
)
1351 elf_hash_table (info
)->dynstr
= elf_stringtab_init ();
1352 if (elf_hash_table (info
)->dynstr
== NULL
)
1356 s
= bfd_make_section (abfd
, ".dynamic");
1358 || ! bfd_set_section_flags (abfd
, s
, flags
)
1359 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1362 /* The special symbol _DYNAMIC is always set to the start of the
1363 .dynamic section. This call occurs before we have processed the
1364 symbols for any dynamic object, so we don't have to worry about
1365 overriding a dynamic definition. We could set _DYNAMIC in a
1366 linker script, but we only want to define it if we are, in fact,
1367 creating a .dynamic section. We don't want to define it if there
1368 is no .dynamic section, since on some ELF platforms the start up
1369 code examines it to decide how to initialize the process. */
1371 if (! (_bfd_generic_link_add_one_symbol
1372 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
1373 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
1374 (struct bfd_link_hash_entry
**) &h
)))
1376 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
1377 h
->type
= STT_OBJECT
;
1380 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1383 s
= bfd_make_section (abfd
, ".hash");
1385 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1386 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1389 /* Let the backend create the rest of the sections. This lets the
1390 backend set the right flags. The backend will normally create
1391 the .got and .plt sections. */
1392 bed
= get_elf_backend_data (abfd
);
1393 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
1396 elf_hash_table (info
)->dynamic_sections_created
= true;
1401 /* Add an entry to the .dynamic table. */
1404 elf_add_dynamic_entry (info
, tag
, val
)
1405 struct bfd_link_info
*info
;
1409 Elf_Internal_Dyn dyn
;
1413 bfd_byte
*newcontents
;
1415 dynobj
= elf_hash_table (info
)->dynobj
;
1417 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
1418 BFD_ASSERT (s
!= NULL
);
1420 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
1421 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
1422 if (newcontents
== NULL
)
1426 dyn
.d_un
.d_val
= val
;
1427 elf_swap_dyn_out (dynobj
, &dyn
,
1428 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
1430 s
->_raw_size
= newsize
;
1431 s
->contents
= newcontents
;
1437 /* Read and swap the relocs for a section. They may have been cached.
1438 If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are not NULL,
1439 they are used as buffers to read into. They are known to be large
1440 enough. If the INTERNAL_RELOCS relocs argument is NULL, the return
1441 value is allocated using either malloc or bfd_alloc, according to
1442 the KEEP_MEMORY argument. */
1445 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
1449 PTR external_relocs
;
1450 Elf_Internal_Rela
*internal_relocs
;
1451 boolean keep_memory
;
1453 Elf_Internal_Shdr
*rel_hdr
;
1455 Elf_Internal_Rela
*alloc2
= NULL
;
1457 if (elf_section_data (o
)->relocs
!= NULL
)
1458 return elf_section_data (o
)->relocs
;
1460 if (o
->reloc_count
== 0)
1463 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
1465 if (internal_relocs
== NULL
)
1469 size
= o
->reloc_count
* sizeof (Elf_Internal_Rela
);
1471 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
1473 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
1474 if (internal_relocs
== NULL
)
1478 if (external_relocs
== NULL
)
1480 alloc1
= (PTR
) bfd_malloc ((size_t) rel_hdr
->sh_size
);
1483 external_relocs
= alloc1
;
1486 if ((bfd_seek (abfd
, rel_hdr
->sh_offset
, SEEK_SET
) != 0)
1487 || (bfd_read (external_relocs
, 1, rel_hdr
->sh_size
, abfd
)
1488 != rel_hdr
->sh_size
))
1491 /* Swap in the relocs. For convenience, we always produce an
1492 Elf_Internal_Rela array; if the relocs are Rel, we set the addend
1494 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
1496 Elf_External_Rel
*erel
;
1497 Elf_External_Rel
*erelend
;
1498 Elf_Internal_Rela
*irela
;
1500 erel
= (Elf_External_Rel
*) external_relocs
;
1501 erelend
= erel
+ o
->reloc_count
;
1502 irela
= internal_relocs
;
1503 for (; erel
< erelend
; erel
++, irela
++)
1505 Elf_Internal_Rel irel
;
1507 elf_swap_reloc_in (abfd
, erel
, &irel
);
1508 irela
->r_offset
= irel
.r_offset
;
1509 irela
->r_info
= irel
.r_info
;
1510 irela
->r_addend
= 0;
1515 Elf_External_Rela
*erela
;
1516 Elf_External_Rela
*erelaend
;
1517 Elf_Internal_Rela
*irela
;
1519 BFD_ASSERT (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
));
1521 erela
= (Elf_External_Rela
*) external_relocs
;
1522 erelaend
= erela
+ o
->reloc_count
;
1523 irela
= internal_relocs
;
1524 for (; erela
< erelaend
; erela
++, irela
++)
1525 elf_swap_reloca_in (abfd
, erela
, irela
);
1528 /* Cache the results for next time, if we can. */
1530 elf_section_data (o
)->relocs
= internal_relocs
;
1535 /* Don't free alloc2, since if it was allocated we are passing it
1536 back (under the name of internal_relocs). */
1538 return internal_relocs
;
1549 /* Record an assignment to a symbol made by a linker script. We need
1550 this in case some dynamic object refers to this symbol. */
1554 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
1556 struct bfd_link_info
*info
;
1560 struct elf_link_hash_entry
*h
;
1562 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1565 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, true, false);
1569 if (h
->root
.type
== bfd_link_hash_new
)
1570 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
1572 /* If this symbol is being provided by the linker script, and it is
1573 currently defined by a dynamic object, but not by a regular
1574 object, then mark it as undefined so that the generic linker will
1575 force the correct value. */
1577 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1578 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1579 h
->root
.type
= bfd_link_hash_undefined
;
1581 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
1582 h
->type
= STT_OBJECT
;
1584 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1585 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
1587 && h
->dynindx
== -1)
1589 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1592 /* If this is a weak defined symbol, and we know a corresponding
1593 real symbol from the same dynamic object, make sure the real
1594 symbol is also made into a dynamic symbol. */
1595 if (h
->weakdef
!= NULL
1596 && h
->weakdef
->dynindx
== -1)
1598 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
1606 /* This structure is used to pass information to
1607 elf_link_assign_sym_version. */
1609 struct elf_assign_sym_version_info
1613 /* General link information. */
1614 struct bfd_link_info
*info
;
1616 struct bfd_elf_version_tree
*verdefs
;
1617 /* Whether we are exporting all dynamic symbols. */
1618 boolean export_dynamic
;
1619 /* Whether we removed any symbols from the dynamic symbol table. */
1620 boolean removed_dynamic
;
1621 /* Whether we had a failure. */
1625 /* This structure is used to pass information to
1626 elf_link_find_version_dependencies. */
1628 struct elf_find_verdep_info
1632 /* General link information. */
1633 struct bfd_link_info
*info
;
1634 /* The number of dependencies. */
1636 /* Whether we had a failure. */
1640 /* Array used to determine the number of hash table buckets to use
1641 based on the number of symbols there are. If there are fewer than
1642 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
1643 fewer than 37 we use 17 buckets, and so forth. We never use more
1644 than 32771 buckets. */
1646 static const size_t elf_buckets
[] =
1648 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
1652 /* Set up the sizes and contents of the ELF dynamic sections. This is
1653 called by the ELF linker emulation before_allocation routine. We
1654 must set the sizes of the sections before the linker sets the
1655 addresses of the various sections. */
1658 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
1659 export_dynamic
, filter_shlib
,
1660 auxiliary_filters
, info
, sinterpptr
,
1665 boolean export_dynamic
;
1666 const char *filter_shlib
;
1667 const char * const *auxiliary_filters
;
1668 struct bfd_link_info
*info
;
1669 asection
**sinterpptr
;
1670 struct bfd_elf_version_tree
*verdefs
;
1672 bfd_size_type soname_indx
;
1674 struct elf_backend_data
*bed
;
1680 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1683 /* The backend may have to create some sections regardless of whether
1684 we're dynamic or not. */
1685 bed
= get_elf_backend_data (output_bfd
);
1686 if (bed
->elf_backend_always_size_sections
1687 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
1690 dynobj
= elf_hash_table (info
)->dynobj
;
1692 /* If there were no dynamic objects in the link, there is nothing to
1697 /* If we are supposed to export all symbols into the dynamic symbol
1698 table (this is not the normal case), then do so. */
1701 struct elf_info_failed eif
;
1705 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
1711 if (elf_hash_table (info
)->dynamic_sections_created
)
1713 struct elf_info_failed eif
;
1714 struct elf_link_hash_entry
*h
;
1715 bfd_size_type strsize
;
1717 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
1718 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
1722 soname_indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
1723 soname
, true, true);
1724 if (soname_indx
== (bfd_size_type
) -1
1725 || ! elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
1731 if (! elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
1739 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, rpath
,
1741 if (indx
== (bfd_size_type
) -1
1742 || ! elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
1746 if (filter_shlib
!= NULL
)
1750 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
1751 filter_shlib
, true, true);
1752 if (indx
== (bfd_size_type
) -1
1753 || ! elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
1757 if (auxiliary_filters
!= NULL
)
1759 const char * const *p
;
1761 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
1765 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
1767 if (indx
== (bfd_size_type
) -1
1768 || ! elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
1773 /* Find all symbols which were defined in a dynamic object and make
1774 the backend pick a reasonable value for them. */
1777 elf_link_hash_traverse (elf_hash_table (info
),
1778 elf_adjust_dynamic_symbol
,
1783 /* Add some entries to the .dynamic section. We fill in some of the
1784 values later, in elf_bfd_final_link, but we must add the entries
1785 now so that we know the final size of the .dynamic section. */
1786 h
= elf_link_hash_lookup (elf_hash_table (info
), "_init", false,
1789 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
1790 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
1792 if (! elf_add_dynamic_entry (info
, DT_INIT
, 0))
1795 h
= elf_link_hash_lookup (elf_hash_table (info
), "_fini", false,
1798 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
1799 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
1801 if (! elf_add_dynamic_entry (info
, DT_FINI
, 0))
1804 strsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
1805 if (! elf_add_dynamic_entry (info
, DT_HASH
, 0)
1806 || ! elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
1807 || ! elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
1808 || ! elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
1809 || ! elf_add_dynamic_entry (info
, DT_SYMENT
,
1810 sizeof (Elf_External_Sym
)))
1814 /* The backend must work out the sizes of all the other dynamic
1816 if (! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
1819 if (elf_hash_table (info
)->dynamic_sections_created
)
1824 size_t bucketcount
= 0;
1825 Elf_Internal_Sym isym
;
1827 /* Set up the version definition section. */
1828 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
1829 BFD_ASSERT (s
!= NULL
);
1830 if (verdefs
== NULL
)
1832 struct elf_assign_sym_version_info sinfo
;
1835 /* No version script was used. In this case, we just check
1836 that there were no version overrides for any symbols. */
1837 sinfo
.output_bfd
= output_bfd
;
1839 sinfo
.verdefs
= verdefs
;
1840 sinfo
.removed_dynamic
= false;
1841 sinfo
.export_dynamic
= export_dynamic
;
1842 sinfo
.failed
= false;
1844 elf_link_hash_traverse (elf_hash_table (info
),
1845 elf_link_assign_sym_version
,
1850 /* Don't include this section in the output file. */
1851 for (spp
= &output_bfd
->sections
;
1852 *spp
!= s
->output_section
;
1853 spp
= &(*spp
)->next
)
1855 *spp
= s
->output_section
->next
;
1856 --output_bfd
->section_count
;
1860 struct elf_assign_sym_version_info sinfo
;
1863 struct bfd_elf_version_tree
*t
;
1865 Elf_Internal_Verdef def
;
1866 Elf_Internal_Verdaux defaux
;
1868 /* Attach all of the symbols to their version information.
1869 This may cause some symbols to be unexported. */
1870 sinfo
.output_bfd
= output_bfd
;
1872 sinfo
.verdefs
= verdefs
;
1873 sinfo
.export_dynamic
= export_dynamic
;
1874 sinfo
.removed_dynamic
= false;
1875 sinfo
.failed
= false;
1876 elf_link_hash_traverse (elf_hash_table (info
),
1877 elf_link_assign_sym_version
,
1882 if (sinfo
.removed_dynamic
)
1884 /* Some dynamic symbols were changed to be local
1885 symbols. In this case, we renumber all of the
1886 dynamic symbols, so that we don't have a hole.
1887 FIXME: The names of the removed symbols will still be
1888 in the dynamic string table, wasting space. */
1889 elf_hash_table (info
)->dynsymcount
= 1;
1890 elf_link_hash_traverse (elf_hash_table (info
),
1891 elf_link_renumber_dynsyms
,
1898 /* Make space for the base version. */
1899 size
+= sizeof (Elf_External_Verdef
);
1900 size
+= sizeof (Elf_External_Verdaux
);
1903 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
1905 struct bfd_elf_version_deps
*n
;
1907 size
+= sizeof (Elf_External_Verdef
);
1908 size
+= sizeof (Elf_External_Verdaux
);
1911 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
1912 size
+= sizeof (Elf_External_Verdaux
);
1915 s
->_raw_size
= size
;
1916 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
1917 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
1920 /* Fill in the version definition section. */
1924 def
.vd_version
= VER_DEF_CURRENT
;
1925 def
.vd_flags
= VER_FLG_BASE
;
1928 def
.vd_aux
= sizeof (Elf_External_Verdef
);
1929 def
.vd_next
= (sizeof (Elf_External_Verdef
)
1930 + sizeof (Elf_External_Verdaux
));
1932 if (soname_indx
!= -1)
1934 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) soname
);
1935 defaux
.vda_name
= soname_indx
;
1942 name
= output_bfd
->filename
;
1943 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) name
);
1944 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
1946 if (indx
== (bfd_size_type
) -1)
1948 defaux
.vda_name
= indx
;
1950 defaux
.vda_next
= 0;
1952 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
1953 (Elf_External_Verdef
*)p
);
1954 p
+= sizeof (Elf_External_Verdef
);
1955 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
1956 (Elf_External_Verdaux
*) p
);
1957 p
+= sizeof (Elf_External_Verdaux
);
1959 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
1962 struct bfd_elf_version_deps
*n
;
1963 struct elf_link_hash_entry
*h
;
1966 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
1969 /* Add a symbol representing this version. */
1971 if (! (_bfd_generic_link_add_one_symbol
1972 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
1973 (bfd_vma
) 0, (const char *) NULL
, false,
1974 get_elf_backend_data (dynobj
)->collect
,
1975 (struct bfd_link_hash_entry
**) &h
)))
1977 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
1978 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
1979 h
->type
= STT_OBJECT
;
1980 h
->verinfo
.vertree
= t
;
1984 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1988 def
.vd_version
= VER_DEF_CURRENT
;
1990 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
1991 def
.vd_flags
|= VER_FLG_WEAK
;
1992 def
.vd_ndx
= t
->vernum
+ 1;
1993 def
.vd_cnt
= cdeps
+ 1;
1994 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) t
->name
);
1995 def
.vd_aux
= sizeof (Elf_External_Verdef
);
1996 if (t
->next
!= NULL
)
1997 def
.vd_next
= (sizeof (Elf_External_Verdef
)
1998 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
2002 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2003 (Elf_External_Verdef
*) p
);
2004 p
+= sizeof (Elf_External_Verdef
);
2006 defaux
.vda_name
= h
->dynstr_index
;
2007 if (t
->deps
== NULL
)
2008 defaux
.vda_next
= 0;
2010 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2011 t
->name_indx
= defaux
.vda_name
;
2013 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2014 (Elf_External_Verdaux
*) p
);
2015 p
+= sizeof (Elf_External_Verdaux
);
2017 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2019 defaux
.vda_name
= n
->version_needed
->name_indx
;
2020 if (n
->next
== NULL
)
2021 defaux
.vda_next
= 0;
2023 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2025 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2026 (Elf_External_Verdaux
*) p
);
2027 p
+= sizeof (Elf_External_Verdaux
);
2031 if (! elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
2032 || ! elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
2035 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
2038 /* Work out the size of the version reference section. */
2040 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2041 BFD_ASSERT (s
!= NULL
);
2043 struct elf_find_verdep_info sinfo
;
2045 sinfo
.output_bfd
= output_bfd
;
2047 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
2048 if (sinfo
.vers
== 0)
2050 sinfo
.failed
= false;
2052 elf_link_hash_traverse (elf_hash_table (info
),
2053 elf_link_find_version_dependencies
,
2056 if (elf_tdata (output_bfd
)->verref
== NULL
)
2060 /* We don't have any version definitions, so we can just
2061 remove the section. */
2063 for (spp
= &output_bfd
->sections
;
2064 *spp
!= s
->output_section
;
2065 spp
= &(*spp
)->next
)
2067 *spp
= s
->output_section
->next
;
2068 --output_bfd
->section_count
;
2072 Elf_Internal_Verneed
*t
;
2077 /* Build the version definition section. */
2078 for (t
= elf_tdata (output_bfd
)->verref
;
2082 Elf_Internal_Vernaux
*a
;
2084 size
+= sizeof (Elf_External_Verneed
);
2086 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2087 size
+= sizeof (Elf_External_Vernaux
);
2090 s
->_raw_size
= size
;
2091 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, size
);
2092 if (s
->contents
== NULL
)
2096 for (t
= elf_tdata (output_bfd
)->verref
;
2101 Elf_Internal_Vernaux
*a
;
2105 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2108 t
->vn_version
= VER_NEED_CURRENT
;
2110 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2111 t
->vn_bfd
->filename
, true, false);
2112 if (indx
== (bfd_size_type
) -1)
2115 t
->vn_aux
= sizeof (Elf_External_Verneed
);
2116 if (t
->vn_nextref
== NULL
)
2119 t
->vn_next
= (sizeof (Elf_External_Verneed
)
2120 + caux
* sizeof (Elf_External_Vernaux
));
2122 _bfd_elf_swap_verneed_out (output_bfd
, t
,
2123 (Elf_External_Verneed
*) p
);
2124 p
+= sizeof (Elf_External_Verneed
);
2126 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2128 a
->vna_hash
= bfd_elf_hash ((const unsigned char *)
2130 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2131 a
->vna_nodename
, true, false);
2132 if (indx
== (bfd_size_type
) -1)
2135 if (a
->vna_nextptr
== NULL
)
2138 a
->vna_next
= sizeof (Elf_External_Vernaux
);
2140 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
2141 (Elf_External_Vernaux
*) p
);
2142 p
+= sizeof (Elf_External_Vernaux
);
2146 if (! elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
2147 || ! elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
2150 elf_tdata (output_bfd
)->cverrefs
= crefs
;
2154 dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2156 /* Work out the size of the symbol version section. */
2157 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
2158 BFD_ASSERT (s
!= NULL
);
2159 if (dynsymcount
== 0
2160 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
2164 /* We don't need any symbol versions; just discard the
2166 for (spp
= &output_bfd
->sections
;
2167 *spp
!= s
->output_section
;
2168 spp
= &(*spp
)->next
)
2170 *spp
= s
->output_section
->next
;
2171 --output_bfd
->section_count
;
2175 Elf_Internal_Versym intversym
;
2177 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
2178 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2179 if (s
->contents
== NULL
)
2182 intversym
.vs_vers
= 0;
2183 _bfd_elf_swap_versym_out (output_bfd
, &intversym
,
2184 (Elf_External_Versym
*) s
->contents
);
2186 if (! elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
2190 /* Set the size of the .dynsym and .hash sections. We counted
2191 the number of dynamic symbols in elf_link_add_object_symbols.
2192 We will build the contents of .dynsym and .hash when we build
2193 the final symbol table, because until then we do not know the
2194 correct value to give the symbols. We built the .dynstr
2195 section as we went along in elf_link_add_object_symbols. */
2196 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
2197 BFD_ASSERT (s
!= NULL
);
2198 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
2199 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2200 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2203 /* The first entry in .dynsym is a dummy symbol. */
2210 elf_swap_symbol_out (output_bfd
, &isym
,
2211 (PTR
) (Elf_External_Sym
*) s
->contents
);
2213 for (i
= 0; elf_buckets
[i
] != 0; i
++)
2215 bucketcount
= elf_buckets
[i
];
2216 if (dynsymcount
< elf_buckets
[i
+ 1])
2220 s
= bfd_get_section_by_name (dynobj
, ".hash");
2221 BFD_ASSERT (s
!= NULL
);
2222 s
->_raw_size
= (2 + bucketcount
+ dynsymcount
) * (ARCH_SIZE
/ 8);
2223 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2224 if (s
->contents
== NULL
)
2226 memset (s
->contents
, 0, (size_t) s
->_raw_size
);
2228 put_word (output_bfd
, bucketcount
, s
->contents
);
2229 put_word (output_bfd
, dynsymcount
, s
->contents
+ (ARCH_SIZE
/ 8));
2231 elf_hash_table (info
)->bucketcount
= bucketcount
;
2233 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
2234 BFD_ASSERT (s
!= NULL
);
2235 s
->_raw_size
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
2237 if (! elf_add_dynamic_entry (info
, DT_NULL
, 0))
2244 /* Make the backend pick a good value for a dynamic symbol. This is
2245 called via elf_link_hash_traverse, and also calls itself
2249 elf_adjust_dynamic_symbol (h
, data
)
2250 struct elf_link_hash_entry
*h
;
2253 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2255 struct elf_backend_data
*bed
;
2257 /* Ignore indirect symbols. There are added by the versioning code. */
2258 if (h
->root
.type
== bfd_link_hash_indirect
)
2261 /* If this symbol was mentioned in a non-ELF file, try to set
2262 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2263 permit a non-ELF file to correctly refer to a symbol defined in
2264 an ELF dynamic object. */
2265 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
2267 if (h
->root
.type
!= bfd_link_hash_defined
2268 && h
->root
.type
!= bfd_link_hash_defweak
)
2269 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2272 if (h
->root
.u
.def
.section
->owner
!= NULL
2273 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2274 == bfd_target_elf_flavour
))
2275 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2277 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2280 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2281 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
2283 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2291 /* If this is a final link, and the symbol was defined as a common
2292 symbol in a regular object file, and there was no definition in
2293 any dynamic object, then the linker will have allocated space for
2294 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2295 flag will not have been set. */
2296 if (h
->root
.type
== bfd_link_hash_defined
2297 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2298 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
2299 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2300 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2301 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2303 /* If -Bsymbolic was used (which means to bind references to global
2304 symbols to the definition within the shared object), and this
2305 symbol was defined in a regular object, then it actually doesn't
2306 need a PLT entry. */
2307 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
2308 && eif
->info
->shared
2309 && eif
->info
->symbolic
2310 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2311 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
2313 /* If this symbol does not require a PLT entry, and it is not
2314 defined by a dynamic object, or is not referenced by a regular
2315 object, ignore it. We do have to handle a weak defined symbol,
2316 even if no regular object refers to it, if we decided to add it
2317 to the dynamic symbol table. FIXME: Do we normally need to worry
2318 about symbols which are defined by one dynamic object and
2319 referenced by another one? */
2320 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
2321 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
2322 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2323 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
2324 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
2327 /* If we've already adjusted this symbol, don't do it again. This
2328 can happen via a recursive call. */
2329 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
2332 /* Don't look at this symbol again. Note that we must set this
2333 after checking the above conditions, because we may look at a
2334 symbol once, decide not to do anything, and then get called
2335 recursively later after REF_REGULAR is set below. */
2336 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
2338 /* If this is a weak definition, and we know a real definition, and
2339 the real symbol is not itself defined by a regular object file,
2340 then get a good value for the real definition. We handle the
2341 real symbol first, for the convenience of the backend routine.
2343 Note that there is a confusing case here. If the real definition
2344 is defined by a regular object file, we don't get the real symbol
2345 from the dynamic object, but we do get the weak symbol. If the
2346 processor backend uses a COPY reloc, then if some routine in the
2347 dynamic object changes the real symbol, we will not see that
2348 change in the corresponding weak symbol. This is the way other
2349 ELF linkers work as well, and seems to be a result of the shared
2352 I will clarify this issue. Most SVR4 shared libraries define the
2353 variable _timezone and define timezone as a weak synonym. The
2354 tzset call changes _timezone. If you write
2355 extern int timezone;
2357 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2358 you might expect that, since timezone is a synonym for _timezone,
2359 the same number will print both times. However, if the processor
2360 backend uses a COPY reloc, then actually timezone will be copied
2361 into your process image, and, since you define _timezone
2362 yourself, _timezone will not. Thus timezone and _timezone will
2363 wind up at different memory locations. The tzset call will set
2364 _timezone, leaving timezone unchanged. */
2366 if (h
->weakdef
!= NULL
)
2368 struct elf_link_hash_entry
*weakdef
;
2370 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2371 || h
->root
.type
== bfd_link_hash_defweak
);
2372 weakdef
= h
->weakdef
;
2373 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2374 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2375 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
2376 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2378 /* This symbol is defined by a regular object file, so we
2379 will not do anything special. Clear weakdef for the
2380 convenience of the processor backend. */
2385 /* There is an implicit reference by a regular object file
2386 via the weak symbol. */
2387 weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2388 if (! elf_adjust_dynamic_symbol (weakdef
, (PTR
) eif
))
2393 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2394 bed
= get_elf_backend_data (dynobj
);
2395 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2404 /* This routine is used to export all defined symbols into the dynamic
2405 symbol table. It is called via elf_link_hash_traverse. */
2408 elf_export_symbol (h
, data
)
2409 struct elf_link_hash_entry
*h
;
2412 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2414 if (h
->dynindx
== -1
2415 && (h
->elf_link_hash_flags
2416 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
2418 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2428 /* Look through the symbols which are defined in other shared
2429 libraries and referenced here. Update the list of version
2430 dependencies. This will be put into the .gnu.version_r section.
2431 This function is called via elf_link_hash_traverse. */
2434 elf_link_find_version_dependencies (h
, data
)
2435 struct elf_link_hash_entry
*h
;
2438 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2439 Elf_Internal_Verneed
*t
;
2440 Elf_Internal_Vernaux
*a
;
2442 /* We only care about symbols defined in shared objects with version
2444 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2445 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
2447 || h
->verinfo
.verdef
== NULL
)
2450 /* See if we already know about this version. */
2451 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
2453 if (t
->vn_bfd
== h
->verinfo
.verdef
->vd_bfd
)
2456 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2457 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2463 /* This is a new version. Add it to tree we are building. */
2467 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *t
);
2470 rinfo
->failed
= true;
2474 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2475 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
2476 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
2479 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *a
);
2481 /* Note that we are copying a string pointer here, and testing it
2482 above. If bfd_elf_string_from_elf_section is ever changed to
2483 discard the string data when low in memory, this will have to be
2485 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2487 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2488 a
->vna_nextptr
= t
->vn_auxptr
;
2490 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2493 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2500 /* Figure out appropriate versions for all the symbols. We may not
2501 have the version number script until we have read all of the input
2502 files, so until that point we don't know which symbols should be
2503 local. This function is called via elf_link_hash_traverse. */
2506 elf_link_assign_sym_version (h
, data
)
2507 struct elf_link_hash_entry
*h
;
2510 struct elf_assign_sym_version_info
*sinfo
=
2511 (struct elf_assign_sym_version_info
*) data
;
2512 struct bfd_link_info
*info
= sinfo
->info
;
2515 /* We only need version numbers for symbols defined in regular
2517 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2520 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2521 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2523 struct bfd_elf_version_tree
*t
;
2528 /* There are two consecutive ELF_VER_CHR characters if this is
2529 not a hidden symbol. */
2531 if (*p
== ELF_VER_CHR
)
2537 /* If there is no version string, we can just return out. */
2541 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
2545 /* Look for the version. If we find it, it is no longer weak. */
2546 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
2548 if (strcmp (t
->name
, p
) == 0)
2550 h
->verinfo
.vertree
= t
;
2558 /* We could not find the version. Return an error.
2560 (*_bfd_error_handler
)
2561 ("%s: invalid version %s", bfd_get_filename (sinfo
->output_bfd
),
2562 h
->root
.root
.string
);
2563 bfd_set_error (bfd_error_bad_value
);
2564 sinfo
->failed
= true;
2569 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
2572 /* If we don't have a version for this symbol, see if we can find
2574 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
2576 struct bfd_elf_version_tree
*t
;
2577 struct bfd_elf_version_tree
*deflt
;
2578 struct bfd_elf_version_expr
*d
;
2580 /* See if can find what version this symbol is in. If the
2581 symbol is supposed to eb local, then don't actually register
2584 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
2586 if (t
->globals
!= NULL
)
2588 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
2590 if (fnmatch (d
->match
, h
->root
.root
.string
, 0) == 0)
2592 h
->verinfo
.vertree
= t
;
2601 if (t
->locals
!= NULL
)
2603 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
2605 if (d
->match
[0] == '*' && d
->match
[1] == '\0')
2607 else if (fnmatch (d
->match
, h
->root
.root
.string
, 0) == 0)
2609 h
->verinfo
.vertree
= t
;
2610 if (h
->dynindx
!= -1
2612 && ! sinfo
->export_dynamic
2613 && (h
->elf_link_hash_flags
2614 & ELF_LINK_HASH_NEEDS_PLT
) == 0)
2616 sinfo
->removed_dynamic
= true;
2618 /* FIXME: The name of the symbol has already
2619 been recorded in the dynamic string table
2631 if (deflt
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2633 h
->verinfo
.vertree
= deflt
;
2634 if (h
->dynindx
!= -1
2636 && ! sinfo
->export_dynamic
2637 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
2639 sinfo
->removed_dynamic
= true;
2641 /* FIXME: The name of the symbol has already been
2642 recorded in the dynamic string table section. */
2650 /* This function is used to renumber the dynamic symbols, if some of
2651 them are removed because they are marked as local. This is called
2652 via elf_link_hash_traverse. */
2655 elf_link_renumber_dynsyms (h
, data
)
2656 struct elf_link_hash_entry
*h
;
2659 struct bfd_link_info
*info
= (struct bfd_link_info
*) data
;
2661 if (h
->dynindx
!= -1)
2663 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
2664 ++elf_hash_table (info
)->dynsymcount
;
2670 /* Final phase of ELF linker. */
2672 /* A structure we use to avoid passing large numbers of arguments. */
2674 struct elf_final_link_info
2676 /* General link information. */
2677 struct bfd_link_info
*info
;
2680 /* Symbol string table. */
2681 struct bfd_strtab_hash
*symstrtab
;
2682 /* .dynsym section. */
2683 asection
*dynsym_sec
;
2684 /* .hash section. */
2686 /* symbol version section (.gnu.version). */
2687 asection
*symver_sec
;
2688 /* Buffer large enough to hold contents of any section. */
2690 /* Buffer large enough to hold external relocs of any section. */
2691 PTR external_relocs
;
2692 /* Buffer large enough to hold internal relocs of any section. */
2693 Elf_Internal_Rela
*internal_relocs
;
2694 /* Buffer large enough to hold external local symbols of any input
2696 Elf_External_Sym
*external_syms
;
2697 /* Buffer large enough to hold internal local symbols of any input
2699 Elf_Internal_Sym
*internal_syms
;
2700 /* Array large enough to hold a symbol index for each local symbol
2701 of any input BFD. */
2703 /* Array large enough to hold a section pointer for each local
2704 symbol of any input BFD. */
2705 asection
**sections
;
2706 /* Buffer to hold swapped out symbols. */
2707 Elf_External_Sym
*symbuf
;
2708 /* Number of swapped out symbols in buffer. */
2709 size_t symbuf_count
;
2710 /* Number of symbols which fit in symbuf. */
2714 static boolean elf_link_output_sym
2715 PARAMS ((struct elf_final_link_info
*, const char *,
2716 Elf_Internal_Sym
*, asection
*));
2717 static boolean elf_link_flush_output_syms
2718 PARAMS ((struct elf_final_link_info
*));
2719 static boolean elf_link_output_extsym
2720 PARAMS ((struct elf_link_hash_entry
*, PTR
));
2721 static boolean elf_link_input_bfd
2722 PARAMS ((struct elf_final_link_info
*, bfd
*));
2723 static boolean elf_reloc_link_order
2724 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
2725 struct bfd_link_order
*));
2727 /* This struct is used to pass information to routines called via
2728 elf_link_hash_traverse which must return failure. */
2730 struct elf_finfo_failed
2733 struct elf_final_link_info
*finfo
;
2736 /* Do the final step of an ELF link. */
2739 elf_bfd_final_link (abfd
, info
)
2741 struct bfd_link_info
*info
;
2745 struct elf_final_link_info finfo
;
2746 register asection
*o
;
2747 register struct bfd_link_order
*p
;
2749 size_t max_contents_size
;
2750 size_t max_external_reloc_size
;
2751 size_t max_internal_reloc_count
;
2752 size_t max_sym_count
;
2754 Elf_Internal_Sym elfsym
;
2756 Elf_Internal_Shdr
*symtab_hdr
;
2757 Elf_Internal_Shdr
*symstrtab_hdr
;
2758 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2759 struct elf_finfo_failed eif
;
2762 abfd
->flags
|= DYNAMIC
;
2764 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
2765 dynobj
= elf_hash_table (info
)->dynobj
;
2768 finfo
.output_bfd
= abfd
;
2769 finfo
.symstrtab
= elf_stringtab_init ();
2770 if (finfo
.symstrtab
== NULL
)
2775 finfo
.dynsym_sec
= NULL
;
2776 finfo
.hash_sec
= NULL
;
2777 finfo
.symver_sec
= NULL
;
2781 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
2782 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
2783 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
2784 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
2785 /* Note that it is OK if symver_sec is NULL. */
2788 finfo
.contents
= NULL
;
2789 finfo
.external_relocs
= NULL
;
2790 finfo
.internal_relocs
= NULL
;
2791 finfo
.external_syms
= NULL
;
2792 finfo
.internal_syms
= NULL
;
2793 finfo
.indices
= NULL
;
2794 finfo
.sections
= NULL
;
2795 finfo
.symbuf
= NULL
;
2796 finfo
.symbuf_count
= 0;
2798 /* Count up the number of relocations we will output for each output
2799 section, so that we know the sizes of the reloc sections. We
2800 also figure out some maximum sizes. */
2801 max_contents_size
= 0;
2802 max_external_reloc_size
= 0;
2803 max_internal_reloc_count
= 0;
2805 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
2809 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
2811 if (p
->type
== bfd_section_reloc_link_order
2812 || p
->type
== bfd_symbol_reloc_link_order
)
2814 else if (p
->type
== bfd_indirect_link_order
)
2818 sec
= p
->u
.indirect
.section
;
2820 /* Mark all sections which are to be included in the
2821 link. This will normally be every section. We need
2822 to do this so that we can identify any sections which
2823 the linker has decided to not include. */
2824 sec
->linker_mark
= true;
2826 if (info
->relocateable
)
2827 o
->reloc_count
+= sec
->reloc_count
;
2829 if (sec
->_raw_size
> max_contents_size
)
2830 max_contents_size
= sec
->_raw_size
;
2831 if (sec
->_cooked_size
> max_contents_size
)
2832 max_contents_size
= sec
->_cooked_size
;
2834 /* We are interested in just local symbols, not all
2836 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
2837 && (sec
->owner
->flags
& DYNAMIC
) == 0)
2841 if (elf_bad_symtab (sec
->owner
))
2842 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
2843 / sizeof (Elf_External_Sym
));
2845 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
2847 if (sym_count
> max_sym_count
)
2848 max_sym_count
= sym_count
;
2850 if ((sec
->flags
& SEC_RELOC
) != 0)
2854 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
2855 if (ext_size
> max_external_reloc_size
)
2856 max_external_reloc_size
= ext_size
;
2857 if (sec
->reloc_count
> max_internal_reloc_count
)
2858 max_internal_reloc_count
= sec
->reloc_count
;
2864 if (o
->reloc_count
> 0)
2865 o
->flags
|= SEC_RELOC
;
2868 /* Explicitly clear the SEC_RELOC flag. The linker tends to
2869 set it (this is probably a bug) and if it is set
2870 assign_section_numbers will create a reloc section. */
2871 o
->flags
&=~ SEC_RELOC
;
2874 /* If the SEC_ALLOC flag is not set, force the section VMA to
2875 zero. This is done in elf_fake_sections as well, but forcing
2876 the VMA to 0 here will ensure that relocs against these
2877 sections are handled correctly. */
2878 if ((o
->flags
& SEC_ALLOC
) == 0
2879 && ! o
->user_set_vma
)
2883 /* Figure out the file positions for everything but the symbol table
2884 and the relocs. We set symcount to force assign_section_numbers
2885 to create a symbol table. */
2886 abfd
->symcount
= info
->strip
== strip_all
? 0 : 1;
2887 BFD_ASSERT (! abfd
->output_has_begun
);
2888 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
2891 /* That created the reloc sections. Set their sizes, and assign
2892 them file positions, and allocate some buffers. */
2893 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2895 if ((o
->flags
& SEC_RELOC
) != 0)
2897 Elf_Internal_Shdr
*rel_hdr
;
2898 register struct elf_link_hash_entry
**p
, **pend
;
2900 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2902 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* o
->reloc_count
;
2904 /* The contents field must last into write_object_contents,
2905 so we allocate it with bfd_alloc rather than malloc. */
2906 rel_hdr
->contents
= (PTR
) bfd_alloc (abfd
, rel_hdr
->sh_size
);
2907 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2910 p
= ((struct elf_link_hash_entry
**)
2911 bfd_malloc (o
->reloc_count
2912 * sizeof (struct elf_link_hash_entry
*)));
2913 if (p
== NULL
&& o
->reloc_count
!= 0)
2915 elf_section_data (o
)->rel_hashes
= p
;
2916 pend
= p
+ o
->reloc_count
;
2917 for (; p
< pend
; p
++)
2920 /* Use the reloc_count field as an index when outputting the
2926 _bfd_elf_assign_file_positions_for_relocs (abfd
);
2928 /* We have now assigned file positions for all the sections except
2929 .symtab and .strtab. We start the .symtab section at the current
2930 file position, and write directly to it. We build the .strtab
2931 section in memory. */
2933 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2934 /* sh_name is set in prep_headers. */
2935 symtab_hdr
->sh_type
= SHT_SYMTAB
;
2936 symtab_hdr
->sh_flags
= 0;
2937 symtab_hdr
->sh_addr
= 0;
2938 symtab_hdr
->sh_size
= 0;
2939 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
2940 /* sh_link is set in assign_section_numbers. */
2941 /* sh_info is set below. */
2942 /* sh_offset is set just below. */
2943 symtab_hdr
->sh_addralign
= 4; /* FIXME: system dependent? */
2945 off
= elf_tdata (abfd
)->next_file_pos
;
2946 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
2948 /* Note that at this point elf_tdata (abfd)->next_file_pos is
2949 incorrect. We do not yet know the size of the .symtab section.
2950 We correct next_file_pos below, after we do know the size. */
2952 /* Allocate a buffer to hold swapped out symbols. This is to avoid
2953 continuously seeking to the right position in the file. */
2954 if (! info
->keep_memory
|| max_sym_count
< 20)
2955 finfo
.symbuf_size
= 20;
2957 finfo
.symbuf_size
= max_sym_count
;
2958 finfo
.symbuf
= ((Elf_External_Sym
*)
2959 bfd_malloc (finfo
.symbuf_size
* sizeof (Elf_External_Sym
)));
2960 if (finfo
.symbuf
== NULL
)
2963 /* Start writing out the symbol table. The first symbol is always a
2965 if (info
->strip
!= strip_all
|| info
->relocateable
)
2967 elfsym
.st_value
= 0;
2970 elfsym
.st_other
= 0;
2971 elfsym
.st_shndx
= SHN_UNDEF
;
2972 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
2973 &elfsym
, bfd_und_section_ptr
))
2978 /* Some standard ELF linkers do this, but we don't because it causes
2979 bootstrap comparison failures. */
2980 /* Output a file symbol for the output file as the second symbol.
2981 We output this even if we are discarding local symbols, although
2982 I'm not sure if this is correct. */
2983 elfsym
.st_value
= 0;
2985 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
2986 elfsym
.st_other
= 0;
2987 elfsym
.st_shndx
= SHN_ABS
;
2988 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
2989 &elfsym
, bfd_abs_section_ptr
))
2993 /* Output a symbol for each section. We output these even if we are
2994 discarding local symbols, since they are used for relocs. These
2995 symbols have no names. We store the index of each one in the
2996 index field of the section, so that we can find it again when
2997 outputting relocs. */
2998 if (info
->strip
!= strip_all
|| info
->relocateable
)
3000 elfsym
.st_value
= 0;
3002 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
3003 elfsym
.st_other
= 0;
3004 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
3006 o
= section_from_elf_index (abfd
, i
);
3008 o
->target_index
= abfd
->symcount
;
3009 elfsym
.st_shndx
= i
;
3010 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
3016 /* Allocate some memory to hold information read in from the input
3018 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
3019 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
3020 finfo
.internal_relocs
= ((Elf_Internal_Rela
*)
3021 bfd_malloc (max_internal_reloc_count
3022 * sizeof (Elf_Internal_Rela
)));
3023 finfo
.external_syms
= ((Elf_External_Sym
*)
3024 bfd_malloc (max_sym_count
3025 * sizeof (Elf_External_Sym
)));
3026 finfo
.internal_syms
= ((Elf_Internal_Sym
*)
3027 bfd_malloc (max_sym_count
3028 * sizeof (Elf_Internal_Sym
)));
3029 finfo
.indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
3030 finfo
.sections
= ((asection
**)
3031 bfd_malloc (max_sym_count
* sizeof (asection
*)));
3032 if ((finfo
.contents
== NULL
&& max_contents_size
!= 0)
3033 || (finfo
.external_relocs
== NULL
&& max_external_reloc_size
!= 0)
3034 || (finfo
.internal_relocs
== NULL
&& max_internal_reloc_count
!= 0)
3035 || (finfo
.external_syms
== NULL
&& max_sym_count
!= 0)
3036 || (finfo
.internal_syms
== NULL
&& max_sym_count
!= 0)
3037 || (finfo
.indices
== NULL
&& max_sym_count
!= 0)
3038 || (finfo
.sections
== NULL
&& max_sym_count
!= 0))
3041 /* Since ELF permits relocations to be against local symbols, we
3042 must have the local symbols available when we do the relocations.
3043 Since we would rather only read the local symbols once, and we
3044 would rather not keep them in memory, we handle all the
3045 relocations for a single input file at the same time.
3047 Unfortunately, there is no way to know the total number of local
3048 symbols until we have seen all of them, and the local symbol
3049 indices precede the global symbol indices. This means that when
3050 we are generating relocateable output, and we see a reloc against
3051 a global symbol, we can not know the symbol index until we have
3052 finished examining all the local symbols to see which ones we are
3053 going to output. To deal with this, we keep the relocations in
3054 memory, and don't output them until the end of the link. This is
3055 an unfortunate waste of memory, but I don't see a good way around
3056 it. Fortunately, it only happens when performing a relocateable
3057 link, which is not the common case. FIXME: If keep_memory is set
3058 we could write the relocs out and then read them again; I don't
3059 know how bad the memory loss will be. */
3061 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->next
)
3062 sub
->output_has_begun
= false;
3063 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3065 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3067 if (p
->type
== bfd_indirect_link_order
3068 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
3069 == bfd_target_elf_flavour
))
3071 sub
= p
->u
.indirect
.section
->owner
;
3072 if (! sub
->output_has_begun
)
3074 if (! elf_link_input_bfd (&finfo
, sub
))
3076 sub
->output_has_begun
= true;
3079 else if (p
->type
== bfd_section_reloc_link_order
3080 || p
->type
== bfd_symbol_reloc_link_order
)
3082 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
3087 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
3093 /* That wrote out all the local symbols. Finish up the symbol table
3094 with the global symbols. */
3096 /* The sh_info field records the index of the first non local
3098 symtab_hdr
->sh_info
= abfd
->symcount
;
3100 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
= 1;
3102 /* We get the global symbols from the hash table. */
3105 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3110 /* Flush all symbols to the file. */
3111 if (! elf_link_flush_output_syms (&finfo
))
3114 /* Now we know the size of the symtab section. */
3115 off
+= symtab_hdr
->sh_size
;
3117 /* Finish up and write out the symbol string table (.strtab)
3119 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3120 /* sh_name was set in prep_headers. */
3121 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
3122 symstrtab_hdr
->sh_flags
= 0;
3123 symstrtab_hdr
->sh_addr
= 0;
3124 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
3125 symstrtab_hdr
->sh_entsize
= 0;
3126 symstrtab_hdr
->sh_link
= 0;
3127 symstrtab_hdr
->sh_info
= 0;
3128 /* sh_offset is set just below. */
3129 symstrtab_hdr
->sh_addralign
= 1;
3131 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, true);
3132 elf_tdata (abfd
)->next_file_pos
= off
;
3134 if (abfd
->symcount
> 0)
3136 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
3137 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
3141 /* Adjust the relocs to have the correct symbol indices. */
3142 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3144 struct elf_link_hash_entry
**rel_hash
;
3145 Elf_Internal_Shdr
*rel_hdr
;
3147 if ((o
->flags
& SEC_RELOC
) == 0)
3150 rel_hash
= elf_section_data (o
)->rel_hashes
;
3151 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
3152 for (i
= 0; i
< o
->reloc_count
; i
++, rel_hash
++)
3154 if (*rel_hash
== NULL
)
3157 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
3159 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
3161 Elf_External_Rel
*erel
;
3162 Elf_Internal_Rel irel
;
3164 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
3165 elf_swap_reloc_in (abfd
, erel
, &irel
);
3166 irel
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
3167 ELF_R_TYPE (irel
.r_info
));
3168 elf_swap_reloc_out (abfd
, &irel
, erel
);
3172 Elf_External_Rela
*erela
;
3173 Elf_Internal_Rela irela
;
3175 BFD_ASSERT (rel_hdr
->sh_entsize
3176 == sizeof (Elf_External_Rela
));
3178 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
3179 elf_swap_reloca_in (abfd
, erela
, &irela
);
3180 irela
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
3181 ELF_R_TYPE (irela
.r_info
));
3182 elf_swap_reloca_out (abfd
, &irela
, erela
);
3186 /* Set the reloc_count field to 0 to prevent write_relocs from
3187 trying to swap the relocs out itself. */
3191 /* If we are linking against a dynamic object, or generating a
3192 shared library, finish up the dynamic linking information. */
3195 Elf_External_Dyn
*dyncon
, *dynconend
;
3197 /* Fix up .dynamic entries. */
3198 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
3199 BFD_ASSERT (o
!= NULL
);
3201 dyncon
= (Elf_External_Dyn
*) o
->contents
;
3202 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
3203 for (; dyncon
< dynconend
; dyncon
++)
3205 Elf_Internal_Dyn dyn
;
3209 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
3216 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on
3217 magic _init and _fini symbols. This is pretty ugly,
3218 but we are compatible. */
3226 struct elf_link_hash_entry
*h
;
3228 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
3229 false, false, true);
3231 && (h
->root
.type
== bfd_link_hash_defined
3232 || h
->root
.type
== bfd_link_hash_defweak
))
3234 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
3235 o
= h
->root
.u
.def
.section
;
3236 if (o
->output_section
!= NULL
)
3237 dyn
.d_un
.d_val
+= (o
->output_section
->vma
3238 + o
->output_offset
);
3241 /* The symbol is imported from another shared
3242 library and does not apply to this one. */
3246 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3261 name
= ".gnu.version_d";
3264 name
= ".gnu.version_r";
3267 name
= ".gnu.version";
3269 o
= bfd_get_section_by_name (abfd
, name
);
3270 BFD_ASSERT (o
!= NULL
);
3271 dyn
.d_un
.d_ptr
= o
->vma
;
3272 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3279 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
3284 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
3286 Elf_Internal_Shdr
*hdr
;
3288 hdr
= elf_elfsections (abfd
)[i
];
3289 if (hdr
->sh_type
== type
3290 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
3292 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
3293 dyn
.d_un
.d_val
+= hdr
->sh_size
;
3296 if (dyn
.d_un
.d_val
== 0
3297 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
3298 dyn
.d_un
.d_val
= hdr
->sh_addr
;
3302 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3308 /* If we have created any dynamic sections, then output them. */
3311 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
3314 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
3316 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
3317 || o
->_raw_size
== 0)
3319 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
3321 /* At this point, we are only interested in sections
3322 created by elf_link_create_dynamic_sections. */
3325 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
3327 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
3329 if (! bfd_set_section_contents (abfd
, o
->output_section
,
3330 o
->contents
, o
->output_offset
,
3338 /* The contents of the .dynstr section are actually in a
3340 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
3341 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
3342 || ! _bfd_stringtab_emit (abfd
,
3343 elf_hash_table (info
)->dynstr
))
3349 /* If we have optimized stabs strings, output them. */
3350 if (elf_hash_table (info
)->stab_info
!= NULL
)
3352 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
3356 if (finfo
.symstrtab
!= NULL
)
3357 _bfd_stringtab_free (finfo
.symstrtab
);
3358 if (finfo
.contents
!= NULL
)
3359 free (finfo
.contents
);
3360 if (finfo
.external_relocs
!= NULL
)
3361 free (finfo
.external_relocs
);
3362 if (finfo
.internal_relocs
!= NULL
)
3363 free (finfo
.internal_relocs
);
3364 if (finfo
.external_syms
!= NULL
)
3365 free (finfo
.external_syms
);
3366 if (finfo
.internal_syms
!= NULL
)
3367 free (finfo
.internal_syms
);
3368 if (finfo
.indices
!= NULL
)
3369 free (finfo
.indices
);
3370 if (finfo
.sections
!= NULL
)
3371 free (finfo
.sections
);
3372 if (finfo
.symbuf
!= NULL
)
3373 free (finfo
.symbuf
);
3374 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3376 if ((o
->flags
& SEC_RELOC
) != 0
3377 && elf_section_data (o
)->rel_hashes
!= NULL
)
3378 free (elf_section_data (o
)->rel_hashes
);
3381 elf_tdata (abfd
)->linker
= true;
3386 if (finfo
.symstrtab
!= NULL
)
3387 _bfd_stringtab_free (finfo
.symstrtab
);
3388 if (finfo
.contents
!= NULL
)
3389 free (finfo
.contents
);
3390 if (finfo
.external_relocs
!= NULL
)
3391 free (finfo
.external_relocs
);
3392 if (finfo
.internal_relocs
!= NULL
)
3393 free (finfo
.internal_relocs
);
3394 if (finfo
.external_syms
!= NULL
)
3395 free (finfo
.external_syms
);
3396 if (finfo
.internal_syms
!= NULL
)
3397 free (finfo
.internal_syms
);
3398 if (finfo
.indices
!= NULL
)
3399 free (finfo
.indices
);
3400 if (finfo
.sections
!= NULL
)
3401 free (finfo
.sections
);
3402 if (finfo
.symbuf
!= NULL
)
3403 free (finfo
.symbuf
);
3404 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3406 if ((o
->flags
& SEC_RELOC
) != 0
3407 && elf_section_data (o
)->rel_hashes
!= NULL
)
3408 free (elf_section_data (o
)->rel_hashes
);
3414 /* Add a symbol to the output symbol table. */
3417 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
3418 struct elf_final_link_info
*finfo
;
3420 Elf_Internal_Sym
*elfsym
;
3421 asection
*input_sec
;
3423 boolean (*output_symbol_hook
) PARAMS ((bfd
*,
3424 struct bfd_link_info
*info
,
3429 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
3430 elf_backend_link_output_symbol_hook
;
3431 if (output_symbol_hook
!= NULL
)
3433 if (! ((*output_symbol_hook
)
3434 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
3438 if (name
== (const char *) NULL
|| *name
== '\0')
3439 elfsym
->st_name
= 0;
3442 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
3445 if (elfsym
->st_name
== (unsigned long) -1)
3449 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
3451 if (! elf_link_flush_output_syms (finfo
))
3455 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
,
3456 (PTR
) (finfo
->symbuf
+ finfo
->symbuf_count
));
3457 ++finfo
->symbuf_count
;
3459 ++finfo
->output_bfd
->symcount
;
3464 /* Flush the output symbols to the file. */
3467 elf_link_flush_output_syms (finfo
)
3468 struct elf_final_link_info
*finfo
;
3470 if (finfo
->symbuf_count
> 0)
3472 Elf_Internal_Shdr
*symtab
;
3474 symtab
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
3476 if (bfd_seek (finfo
->output_bfd
, symtab
->sh_offset
+ symtab
->sh_size
,
3478 || (bfd_write ((PTR
) finfo
->symbuf
, finfo
->symbuf_count
,
3479 sizeof (Elf_External_Sym
), finfo
->output_bfd
)
3480 != finfo
->symbuf_count
* sizeof (Elf_External_Sym
)))
3483 symtab
->sh_size
+= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
3485 finfo
->symbuf_count
= 0;
3491 /* Add an external symbol to the symbol table. This is called from
3492 the hash table traversal routine. */
3495 elf_link_output_extsym (h
, data
)
3496 struct elf_link_hash_entry
*h
;
3499 struct elf_finfo_failed
*eif
= (struct elf_finfo_failed
*) data
;
3500 struct elf_final_link_info
*finfo
= eif
->finfo
;
3502 Elf_Internal_Sym sym
;
3503 asection
*input_sec
;
3505 /* If we are not creating a shared library, and this symbol is
3506 referenced by a shared library but is not defined anywhere, then
3507 warn that it is undefined. If we do not do this, the runtime
3508 linker will complain that the symbol is undefined when the
3509 program is run. We don't have to worry about symbols that are
3510 referenced by regular files, because we will already have issued
3511 warnings for them. */
3512 if (! finfo
->info
->relocateable
3513 && ! finfo
->info
->shared
3514 && h
->root
.type
== bfd_link_hash_undefined
3515 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
3516 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
3518 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
3519 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
3520 (asection
*) NULL
, 0)))
3527 /* We don't want to output symbols that have never been mentioned by
3528 a regular file, or that we have been told to strip. However, if
3529 h->indx is set to -2, the symbol is used by a reloc and we must
3533 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
3534 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
3535 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3536 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
3538 else if (finfo
->info
->strip
== strip_all
3539 || (finfo
->info
->strip
== strip_some
3540 && bfd_hash_lookup (finfo
->info
->keep_hash
,
3541 h
->root
.root
.string
,
3542 false, false) == NULL
))
3547 /* If we're stripping it, and it's not a dynamic symbol, there's
3548 nothing else to do. */
3549 if (strip
&& h
->dynindx
== -1)
3553 sym
.st_size
= h
->size
;
3554 sym
.st_other
= h
->other
;
3555 if (h
->root
.type
== bfd_link_hash_undefweak
3556 || h
->root
.type
== bfd_link_hash_defweak
)
3557 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
3559 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
3561 switch (h
->root
.type
)
3564 case bfd_link_hash_new
:
3568 case bfd_link_hash_undefined
:
3569 input_sec
= bfd_und_section_ptr
;
3570 sym
.st_shndx
= SHN_UNDEF
;
3573 case bfd_link_hash_undefweak
:
3574 input_sec
= bfd_und_section_ptr
;
3575 sym
.st_shndx
= SHN_UNDEF
;
3578 case bfd_link_hash_defined
:
3579 case bfd_link_hash_defweak
:
3581 input_sec
= h
->root
.u
.def
.section
;
3582 if (input_sec
->output_section
!= NULL
)
3585 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
3586 input_sec
->output_section
);
3587 if (sym
.st_shndx
== (unsigned short) -1)
3593 /* ELF symbols in relocateable files are section relative,
3594 but in nonrelocateable files they are virtual
3596 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
3597 if (! finfo
->info
->relocateable
)
3598 sym
.st_value
+= input_sec
->output_section
->vma
;
3602 BFD_ASSERT ((input_sec
->owner
->flags
& DYNAMIC
) != 0);
3603 sym
.st_shndx
= SHN_UNDEF
;
3604 input_sec
= bfd_und_section_ptr
;
3609 case bfd_link_hash_common
:
3610 input_sec
= bfd_com_section_ptr
;
3611 sym
.st_shndx
= SHN_COMMON
;
3612 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
3615 case bfd_link_hash_indirect
:
3616 /* These symbols are created by symbol versioning. They point
3617 to the decorated version of the name. For example, if the
3618 symbol foo@@GNU_1.2 is the default, which should be used when
3619 foo is used with no version, then we add an indirect symbol
3620 foo which points to foo@@GNU_1.2. */
3621 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
3625 case bfd_link_hash_warning
:
3626 /* We can't represent these symbols in ELF, although a warning
3627 symbol may have come from a .gnu.warning.SYMBOL section. We
3628 just put the target symbol in the hash table. If the target
3629 symbol does not really exist, don't do anything. */
3630 if (h
->root
.u
.i
.link
->type
== bfd_link_hash_new
)
3632 return (elf_link_output_extsym
3633 ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
));
3636 /* If this symbol should be put in the .dynsym section, then put it
3637 there now. We have already know the symbol index. We also fill
3638 in the entry in the .hash section. */
3639 if (h
->dynindx
!= -1
3640 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
3642 struct elf_backend_data
*bed
;
3647 bfd_byte
*bucketpos
;
3650 sym
.st_name
= h
->dynstr_index
;
3652 /* Give the processor backend a chance to tweak the symbol
3653 value, and also to finish up anything that needs to be done
3655 bed
= get_elf_backend_data (finfo
->output_bfd
);
3656 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
3657 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
3663 elf_swap_symbol_out (finfo
->output_bfd
, &sym
,
3664 (PTR
) (((Elf_External_Sym
*)
3665 finfo
->dynsym_sec
->contents
)
3668 /* We didn't include the version string in the dynamic string
3669 table, so we must not consider it in the hash table. */
3670 name
= h
->root
.root
.string
;
3671 p
= strchr (name
, ELF_VER_CHR
);
3676 copy
= bfd_alloc (finfo
->output_bfd
, p
- name
+ 1);
3677 strncpy (copy
, name
, p
- name
);
3678 copy
[p
- name
] = '\0';
3682 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
3683 bucket
= bfd_elf_hash ((const unsigned char *) name
) % bucketcount
;
3684 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
3685 + (bucket
+ 2) * (ARCH_SIZE
/ 8));
3686 chain
= get_word (finfo
->output_bfd
, bucketpos
);
3687 put_word (finfo
->output_bfd
, h
->dynindx
, bucketpos
);
3688 put_word (finfo
->output_bfd
, chain
,
3689 ((bfd_byte
*) finfo
->hash_sec
->contents
3690 + (bucketcount
+ 2 + h
->dynindx
) * (ARCH_SIZE
/ 8)));
3693 bfd_release (finfo
->output_bfd
, copy
);
3695 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
3697 Elf_Internal_Versym iversym
;
3699 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
3701 if (h
->verinfo
.verdef
== NULL
)
3702 iversym
.vs_vers
= 0;
3704 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
3708 if (h
->verinfo
.vertree
== NULL
)
3709 iversym
.vs_vers
= 1;
3711 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
3714 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
3715 iversym
.vs_vers
|= VERSYM_HIDDEN
;
3717 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
,
3718 (((Elf_External_Versym
*)
3719 finfo
->symver_sec
->contents
)
3724 /* If we're stripping it, then it was just a dynamic symbol, and
3725 there's nothing else to do. */
3729 h
->indx
= finfo
->output_bfd
->symcount
;
3731 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
3740 /* Link an input file into the linker output file. This function
3741 handles all the sections and relocations of the input file at once.
3742 This is so that we only have to read the local symbols once, and
3743 don't have to keep them in memory. */
3746 elf_link_input_bfd (finfo
, input_bfd
)
3747 struct elf_final_link_info
*finfo
;
3750 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
3751 bfd
*, asection
*, bfd_byte
*,
3752 Elf_Internal_Rela
*,
3753 Elf_Internal_Sym
*, asection
**));
3755 Elf_Internal_Shdr
*symtab_hdr
;
3758 Elf_External_Sym
*external_syms
;
3759 Elf_External_Sym
*esym
;
3760 Elf_External_Sym
*esymend
;
3761 Elf_Internal_Sym
*isym
;
3763 asection
**ppsection
;
3766 output_bfd
= finfo
->output_bfd
;
3768 get_elf_backend_data (output_bfd
)->elf_backend_relocate_section
;
3770 /* If this is a dynamic object, we don't want to do anything here:
3771 we don't want the local symbols, and we don't want the section
3773 if ((input_bfd
->flags
& DYNAMIC
) != 0)
3776 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3777 if (elf_bad_symtab (input_bfd
))
3779 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
3784 locsymcount
= symtab_hdr
->sh_info
;
3785 extsymoff
= symtab_hdr
->sh_info
;
3788 /* Read the local symbols. */
3789 if (symtab_hdr
->contents
!= NULL
)
3790 external_syms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
3791 else if (locsymcount
== 0)
3792 external_syms
= NULL
;
3795 external_syms
= finfo
->external_syms
;
3796 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
3797 || (bfd_read (external_syms
, sizeof (Elf_External_Sym
),
3798 locsymcount
, input_bfd
)
3799 != locsymcount
* sizeof (Elf_External_Sym
)))
3803 /* Swap in the local symbols and write out the ones which we know
3804 are going into the output file. */
3805 esym
= external_syms
;
3806 esymend
= esym
+ locsymcount
;
3807 isym
= finfo
->internal_syms
;
3808 pindex
= finfo
->indices
;
3809 ppsection
= finfo
->sections
;
3810 for (; esym
< esymend
; esym
++, isym
++, pindex
++, ppsection
++)
3814 Elf_Internal_Sym osym
;
3816 elf_swap_symbol_in (input_bfd
, esym
, isym
);
3819 if (elf_bad_symtab (input_bfd
))
3821 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
3828 if (isym
->st_shndx
== SHN_UNDEF
)
3829 isec
= bfd_und_section_ptr
;
3830 else if (isym
->st_shndx
> 0 && isym
->st_shndx
< SHN_LORESERVE
)
3831 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
3832 else if (isym
->st_shndx
== SHN_ABS
)
3833 isec
= bfd_abs_section_ptr
;
3834 else if (isym
->st_shndx
== SHN_COMMON
)
3835 isec
= bfd_com_section_ptr
;
3844 /* Don't output the first, undefined, symbol. */
3845 if (esym
== external_syms
)
3848 /* If we are stripping all symbols, we don't want to output this
3850 if (finfo
->info
->strip
== strip_all
)
3853 /* We never output section symbols. Instead, we use the section
3854 symbol of the corresponding section in the output file. */
3855 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
3858 /* If we are discarding all local symbols, we don't want to
3859 output this one. If we are generating a relocateable output
3860 file, then some of the local symbols may be required by
3861 relocs; we output them below as we discover that they are
3863 if (finfo
->info
->discard
== discard_all
)
3866 /* If this symbol is defined in a section which we are
3867 discarding, we don't need to keep it, but note that
3868 linker_mark is only reliable for sections that have contents.
3869 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
3870 as well as linker_mark. */
3871 if (isym
->st_shndx
> 0
3872 && isym
->st_shndx
< SHN_LORESERVE
3874 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
3875 || (! finfo
->info
->relocateable
3876 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
3879 /* Get the name of the symbol. */
3880 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
3885 /* See if we are discarding symbols with this name. */
3886 if ((finfo
->info
->strip
== strip_some
3887 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
3889 || (finfo
->info
->discard
== discard_l
3890 && bfd_is_local_label_name (input_bfd
, name
)))
3893 /* If we get here, we are going to output this symbol. */
3897 /* Adjust the section index for the output file. */
3898 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
3899 isec
->output_section
);
3900 if (osym
.st_shndx
== (unsigned short) -1)
3903 *pindex
= output_bfd
->symcount
;
3905 /* ELF symbols in relocateable files are section relative, but
3906 in executable files they are virtual addresses. Note that
3907 this code assumes that all ELF sections have an associated
3908 BFD section with a reasonable value for output_offset; below
3909 we assume that they also have a reasonable value for
3910 output_section. Any special sections must be set up to meet
3911 these requirements. */
3912 osym
.st_value
+= isec
->output_offset
;
3913 if (! finfo
->info
->relocateable
)
3914 osym
.st_value
+= isec
->output_section
->vma
;
3916 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
3920 /* Relocate the contents of each section. */
3921 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
3925 if (! o
->linker_mark
)
3927 /* This section was omitted from the link. */
3931 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
3932 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
3935 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
3937 /* Section was created by elf_link_create_dynamic_sections
3942 /* Get the contents of the section. They have been cached by a
3943 relaxation routine. Note that o is a section in an input
3944 file, so the contents field will not have been set by any of
3945 the routines which work on output files. */
3946 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
3947 contents
= elf_section_data (o
)->this_hdr
.contents
;
3950 contents
= finfo
->contents
;
3951 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
3952 (file_ptr
) 0, o
->_raw_size
))
3956 if ((o
->flags
& SEC_RELOC
) != 0)
3958 Elf_Internal_Rela
*internal_relocs
;
3960 /* Get the swapped relocs. */
3961 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
3962 (input_bfd
, o
, finfo
->external_relocs
,
3963 finfo
->internal_relocs
, false));
3964 if (internal_relocs
== NULL
3965 && o
->reloc_count
> 0)
3968 /* Relocate the section by invoking a back end routine.
3970 The back end routine is responsible for adjusting the
3971 section contents as necessary, and (if using Rela relocs
3972 and generating a relocateable output file) adjusting the
3973 reloc addend as necessary.
3975 The back end routine does not have to worry about setting
3976 the reloc address or the reloc symbol index.
3978 The back end routine is given a pointer to the swapped in
3979 internal symbols, and can access the hash table entries
3980 for the external symbols via elf_sym_hashes (input_bfd).
3982 When generating relocateable output, the back end routine
3983 must handle STB_LOCAL/STT_SECTION symbols specially. The
3984 output symbol is going to be a section symbol
3985 corresponding to the output section, which will require
3986 the addend to be adjusted. */
3988 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
3989 input_bfd
, o
, contents
,
3991 finfo
->internal_syms
,
3995 if (finfo
->info
->relocateable
)
3997 Elf_Internal_Rela
*irela
;
3998 Elf_Internal_Rela
*irelaend
;
3999 struct elf_link_hash_entry
**rel_hash
;
4000 Elf_Internal_Shdr
*input_rel_hdr
;
4001 Elf_Internal_Shdr
*output_rel_hdr
;
4003 /* Adjust the reloc addresses and symbol indices. */
4005 irela
= internal_relocs
;
4006 irelaend
= irela
+ o
->reloc_count
;
4007 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
4008 + o
->output_section
->reloc_count
);
4009 for (; irela
< irelaend
; irela
++, rel_hash
++)
4011 unsigned long r_symndx
;
4012 Elf_Internal_Sym
*isym
;
4015 irela
->r_offset
+= o
->output_offset
;
4017 r_symndx
= ELF_R_SYM (irela
->r_info
);
4022 if (r_symndx
>= locsymcount
4023 || (elf_bad_symtab (input_bfd
)
4024 && finfo
->sections
[r_symndx
] == NULL
))
4028 /* This is a reloc against a global symbol. We
4029 have not yet output all the local symbols, so
4030 we do not know the symbol index of any global
4031 symbol. We set the rel_hash entry for this
4032 reloc to point to the global hash table entry
4033 for this symbol. The symbol index is then
4034 set at the end of elf_bfd_final_link. */
4035 indx
= r_symndx
- extsymoff
;
4036 *rel_hash
= elf_sym_hashes (input_bfd
)[indx
];
4038 /* Setting the index to -2 tells
4039 elf_link_output_extsym that this symbol is
4041 BFD_ASSERT ((*rel_hash
)->indx
< 0);
4042 (*rel_hash
)->indx
= -2;
4047 /* This is a reloc against a local symbol. */
4050 isym
= finfo
->internal_syms
+ r_symndx
;
4051 sec
= finfo
->sections
[r_symndx
];
4052 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
4054 /* I suppose the backend ought to fill in the
4055 section of any STT_SECTION symbol against a
4056 processor specific section. If we have
4057 discarded a section, the output_section will
4058 be the absolute section. */
4060 && (bfd_is_abs_section (sec
)
4061 || (sec
->output_section
!= NULL
4062 && bfd_is_abs_section (sec
->output_section
))))
4064 else if (sec
== NULL
|| sec
->owner
== NULL
)
4066 bfd_set_error (bfd_error_bad_value
);
4071 r_symndx
= sec
->output_section
->target_index
;
4072 BFD_ASSERT (r_symndx
!= 0);
4077 if (finfo
->indices
[r_symndx
] == -1)
4083 if (finfo
->info
->strip
== strip_all
)
4085 /* You can't do ld -r -s. */
4086 bfd_set_error (bfd_error_invalid_operation
);
4090 /* This symbol was skipped earlier, but
4091 since it is needed by a reloc, we
4092 must output it now. */
4093 link
= symtab_hdr
->sh_link
;
4094 name
= bfd_elf_string_from_elf_section (input_bfd
,
4100 osec
= sec
->output_section
;
4102 _bfd_elf_section_from_bfd_section (output_bfd
,
4104 if (isym
->st_shndx
== (unsigned short) -1)
4107 isym
->st_value
+= sec
->output_offset
;
4108 if (! finfo
->info
->relocateable
)
4109 isym
->st_value
+= osec
->vma
;
4111 finfo
->indices
[r_symndx
] = output_bfd
->symcount
;
4113 if (! elf_link_output_sym (finfo
, name
, isym
, sec
))
4117 r_symndx
= finfo
->indices
[r_symndx
];
4120 irela
->r_info
= ELF_R_INFO (r_symndx
,
4121 ELF_R_TYPE (irela
->r_info
));
4124 /* Swap out the relocs. */
4125 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
4126 output_rel_hdr
= &elf_section_data (o
->output_section
)->rel_hdr
;
4127 BFD_ASSERT (output_rel_hdr
->sh_entsize
4128 == input_rel_hdr
->sh_entsize
);
4129 irela
= internal_relocs
;
4130 irelaend
= irela
+ o
->reloc_count
;
4131 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4133 Elf_External_Rel
*erel
;
4135 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
4136 + o
->output_section
->reloc_count
);
4137 for (; irela
< irelaend
; irela
++, erel
++)
4139 Elf_Internal_Rel irel
;
4141 irel
.r_offset
= irela
->r_offset
;
4142 irel
.r_info
= irela
->r_info
;
4143 BFD_ASSERT (irela
->r_addend
== 0);
4144 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
4149 Elf_External_Rela
*erela
;
4151 BFD_ASSERT (input_rel_hdr
->sh_entsize
4152 == sizeof (Elf_External_Rela
));
4153 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
4154 + o
->output_section
->reloc_count
);
4155 for (; irela
< irelaend
; irela
++, erela
++)
4156 elf_swap_reloca_out (output_bfd
, irela
, erela
);
4159 o
->output_section
->reloc_count
+= o
->reloc_count
;
4163 /* Write out the modified section contents. */
4164 if (elf_section_data (o
)->stab_info
== NULL
)
4166 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
4167 contents
, o
->output_offset
,
4168 (o
->_cooked_size
!= 0
4175 if (! _bfd_write_section_stabs (output_bfd
, o
,
4176 &elf_section_data (o
)->stab_info
,
4185 /* Generate a reloc when linking an ELF file. This is a reloc
4186 requested by the linker, and does come from any input file. This
4187 is used to build constructor and destructor tables when linking
4191 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
4193 struct bfd_link_info
*info
;
4194 asection
*output_section
;
4195 struct bfd_link_order
*link_order
;
4197 reloc_howto_type
*howto
;
4201 struct elf_link_hash_entry
**rel_hash_ptr
;
4202 Elf_Internal_Shdr
*rel_hdr
;
4204 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
4207 bfd_set_error (bfd_error_bad_value
);
4211 addend
= link_order
->u
.reloc
.p
->addend
;
4213 /* Figure out the symbol index. */
4214 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
4215 + output_section
->reloc_count
);
4216 if (link_order
->type
== bfd_section_reloc_link_order
)
4218 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
4219 BFD_ASSERT (indx
!= 0);
4220 *rel_hash_ptr
= NULL
;
4224 struct elf_link_hash_entry
*h
;
4226 /* Treat a reloc against a defined symbol as though it were
4227 actually against the section. */
4228 h
= ((struct elf_link_hash_entry
*)
4229 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
4230 link_order
->u
.reloc
.p
->u
.name
,
4231 false, false, true));
4233 && (h
->root
.type
== bfd_link_hash_defined
4234 || h
->root
.type
== bfd_link_hash_defweak
))
4238 section
= h
->root
.u
.def
.section
;
4239 indx
= section
->output_section
->target_index
;
4240 *rel_hash_ptr
= NULL
;
4241 /* It seems that we ought to add the symbol value to the
4242 addend here, but in practice it has already been added
4243 because it was passed to constructor_callback. */
4244 addend
+= section
->output_section
->vma
+ section
->output_offset
;
4248 /* Setting the index to -2 tells elf_link_output_extsym that
4249 this symbol is used by a reloc. */
4256 if (! ((*info
->callbacks
->unattached_reloc
)
4257 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
4258 (asection
*) NULL
, (bfd_vma
) 0)))
4264 /* If this is an inplace reloc, we must write the addend into the
4266 if (howto
->partial_inplace
&& addend
!= 0)
4269 bfd_reloc_status_type rstat
;
4273 size
= bfd_get_reloc_size (howto
);
4274 buf
= (bfd_byte
*) bfd_zmalloc (size
);
4275 if (buf
== (bfd_byte
*) NULL
)
4277 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
4283 case bfd_reloc_outofrange
:
4285 case bfd_reloc_overflow
:
4286 if (! ((*info
->callbacks
->reloc_overflow
)
4288 (link_order
->type
== bfd_section_reloc_link_order
4289 ? bfd_section_name (output_bfd
,
4290 link_order
->u
.reloc
.p
->u
.section
)
4291 : link_order
->u
.reloc
.p
->u
.name
),
4292 howto
->name
, addend
, (bfd
*) NULL
, (asection
*) NULL
,
4300 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
4301 (file_ptr
) link_order
->offset
, size
);
4307 /* The address of a reloc is relative to the section in a
4308 relocateable file, and is a virtual address in an executable
4310 offset
= link_order
->offset
;
4311 if (! info
->relocateable
)
4312 offset
+= output_section
->vma
;
4314 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
4316 if (rel_hdr
->sh_type
== SHT_REL
)
4318 Elf_Internal_Rel irel
;
4319 Elf_External_Rel
*erel
;
4321 irel
.r_offset
= offset
;
4322 irel
.r_info
= ELF_R_INFO (indx
, howto
->type
);
4323 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
4324 + output_section
->reloc_count
);
4325 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
4329 Elf_Internal_Rela irela
;
4330 Elf_External_Rela
*erela
;
4332 irela
.r_offset
= offset
;
4333 irela
.r_info
= ELF_R_INFO (indx
, howto
->type
);
4334 irela
.r_addend
= addend
;
4335 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
4336 + output_section
->reloc_count
);
4337 elf_swap_reloca_out (output_bfd
, &irela
, erela
);
4340 ++output_section
->reloc_count
;
4346 /* Allocate a pointer to live in a linker created section. */
4349 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
4351 struct bfd_link_info
*info
;
4352 elf_linker_section_t
*lsect
;
4353 struct elf_link_hash_entry
*h
;
4354 const Elf_Internal_Rela
*rel
;
4356 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
4357 elf_linker_section_pointers_t
*linker_section_ptr
;
4358 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);;
4360 BFD_ASSERT (lsect
!= NULL
);
4362 /* Is this a global symbol? */
4365 /* Has this symbol already been allocated, if so, our work is done */
4366 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
4371 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
4372 /* Make sure this symbol is output as a dynamic symbol. */
4373 if (h
->dynindx
== -1)
4375 if (! elf_link_record_dynamic_symbol (info
, h
))
4379 if (lsect
->rel_section
)
4380 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
4383 else /* Allocation of a pointer to a local symbol */
4385 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
4387 /* Allocate a table to hold the local symbols if first time */
4390 int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
4391 register unsigned int i
;
4393 ptr
= (elf_linker_section_pointers_t
**)
4394 bfd_alloc (abfd
, num_symbols
* sizeof (elf_linker_section_pointers_t
*));
4399 elf_local_ptr_offsets (abfd
) = ptr
;
4400 for (i
= 0; i
< num_symbols
; i
++)
4401 ptr
[i
] = (elf_linker_section_pointers_t
*)0;
4404 /* Has this symbol already been allocated, if so, our work is done */
4405 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
4410 ptr_linker_section_ptr
= &ptr
[r_symndx
];
4414 /* If we are generating a shared object, we need to
4415 output a R_<xxx>_RELATIVE reloc so that the
4416 dynamic linker can adjust this GOT entry. */
4417 BFD_ASSERT (lsect
->rel_section
!= NULL
);
4418 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
4422 /* Allocate space for a pointer in the linker section, and allocate a new pointer record
4423 from internal memory. */
4424 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
4425 linker_section_ptr
= (elf_linker_section_pointers_t
*)
4426 bfd_alloc (abfd
, sizeof (elf_linker_section_pointers_t
));
4428 if (!linker_section_ptr
)
4431 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
4432 linker_section_ptr
->addend
= rel
->r_addend
;
4433 linker_section_ptr
->which
= lsect
->which
;
4434 linker_section_ptr
->written_address_p
= false;
4435 *ptr_linker_section_ptr
= linker_section_ptr
;
4438 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
4440 linker_section_ptr
->offset
= lsect
->section
->_raw_size
- lsect
->hole_size
+ (ARCH_SIZE
/ 8);
4441 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
4442 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
4443 if (lsect
->sym_hash
) /* Bump up symbol value if needed */
4445 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
4447 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
4448 lsect
->sym_hash
->root
.root
.string
,
4449 (long)ARCH_SIZE
/ 8,
4450 (long)lsect
->sym_hash
->root
.u
.def
.value
);
4456 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
4458 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
4461 fprintf (stderr
, "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
4462 lsect
->name
, (long)linker_section_ptr
->offset
, (long)lsect
->section
->_raw_size
);
4470 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
4473 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
4476 /* Fill in the address for a pointer generated in alinker section. */
4479 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
, relocation
, rel
, relative_reloc
)
4482 struct bfd_link_info
*info
;
4483 elf_linker_section_t
*lsect
;
4484 struct elf_link_hash_entry
*h
;
4486 const Elf_Internal_Rela
*rel
;
4489 elf_linker_section_pointers_t
*linker_section_ptr
;
4491 BFD_ASSERT (lsect
!= NULL
);
4493 if (h
!= NULL
) /* global symbol */
4495 linker_section_ptr
= _bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
4499 BFD_ASSERT (linker_section_ptr
!= NULL
);
4501 if (! elf_hash_table (info
)->dynamic_sections_created
4504 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
4506 /* This is actually a static link, or it is a
4507 -Bsymbolic link and the symbol is defined
4508 locally. We must initialize this entry in the
4511 When doing a dynamic link, we create a .rela.<xxx>
4512 relocation entry to initialize the value. This
4513 is done in the finish_dynamic_symbol routine. */
4514 if (!linker_section_ptr
->written_address_p
)
4516 linker_section_ptr
->written_address_p
= true;
4517 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
4518 lsect
->section
->contents
+ linker_section_ptr
->offset
);
4522 else /* local symbol */
4524 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
4525 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
4526 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
4527 linker_section_ptr
= _bfd_elf_find_pointer_linker_section (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
4531 BFD_ASSERT (linker_section_ptr
!= NULL
);
4533 /* Write out pointer if it hasn't been rewritten out before */
4534 if (!linker_section_ptr
->written_address_p
)
4536 linker_section_ptr
->written_address_p
= true;
4537 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
4538 lsect
->section
->contents
+ linker_section_ptr
->offset
);
4542 asection
*srel
= lsect
->rel_section
;
4543 Elf_Internal_Rela outrel
;
4545 /* We need to generate a relative reloc for the dynamic linker. */
4547 lsect
->rel_section
= srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
4550 BFD_ASSERT (srel
!= NULL
);
4552 outrel
.r_offset
= (lsect
->section
->output_section
->vma
4553 + lsect
->section
->output_offset
4554 + linker_section_ptr
->offset
);
4555 outrel
.r_info
= ELF_R_INFO (0, relative_reloc
);
4556 outrel
.r_addend
= 0;
4557 elf_swap_reloca_out (output_bfd
, &outrel
,
4558 (((Elf_External_Rela
*)
4559 lsect
->section
->contents
)
4560 + lsect
->section
->reloc_count
));
4561 ++lsect
->section
->reloc_count
;
4566 relocation
= (lsect
->section
->output_offset
4567 + linker_section_ptr
->offset
4568 - lsect
->hole_offset
4569 - lsect
->sym_offset
);
4572 fprintf (stderr
, "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
4573 lsect
->name
, (long)relocation
, (long)relocation
);
4576 /* Subtract out the addend, because it will get added back in by the normal
4578 return relocation
- linker_section_ptr
->addend
;