2 Copyright 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* ELF linker code. */
22 /* This struct is used to pass information to routines called via
23 elf_link_hash_traverse which must return failure. */
25 struct elf_info_failed
28 struct bfd_link_info
*info
;
31 static boolean elf_link_add_object_symbols
32 PARAMS ((bfd
*, struct bfd_link_info
*));
33 static boolean elf_link_add_archive_symbols
34 PARAMS ((bfd
*, struct bfd_link_info
*));
35 static boolean elf_merge_symbol
36 PARAMS ((bfd
*, struct bfd_link_info
*, const char *, Elf_Internal_Sym
*,
37 asection
**, bfd_vma
*, struct elf_link_hash_entry
**,
38 boolean
*, boolean
*, boolean
*));
39 static boolean elf_export_symbol
40 PARAMS ((struct elf_link_hash_entry
*, PTR
));
41 static boolean elf_fix_symbol_flags
42 PARAMS ((struct elf_link_hash_entry
*, struct elf_info_failed
*));
43 static boolean elf_adjust_dynamic_symbol
44 PARAMS ((struct elf_link_hash_entry
*, PTR
));
45 static boolean elf_link_find_version_dependencies
46 PARAMS ((struct elf_link_hash_entry
*, PTR
));
47 static boolean elf_link_find_version_dependencies
48 PARAMS ((struct elf_link_hash_entry
*, PTR
));
49 static boolean elf_link_assign_sym_version
50 PARAMS ((struct elf_link_hash_entry
*, PTR
));
51 static boolean elf_collect_hash_codes
52 PARAMS ((struct elf_link_hash_entry
*, PTR
));
53 static boolean elf_link_read_relocs_from_section
54 PARAMS ((bfd
*, Elf_Internal_Shdr
*, PTR
, Elf_Internal_Rela
*));
55 static void elf_link_output_relocs
56 PARAMS ((bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*));
57 static boolean elf_link_size_reloc_section
58 PARAMS ((bfd
*, Elf_Internal_Shdr
*, asection
*));
59 static void elf_link_adjust_relocs
60 PARAMS ((bfd
*, Elf_Internal_Shdr
*, unsigned int,
61 struct elf_link_hash_entry
**));
63 /* Given an ELF BFD, add symbols to the global hash table as
67 elf_bfd_link_add_symbols (abfd
, info
)
69 struct bfd_link_info
*info
;
71 switch (bfd_get_format (abfd
))
74 return elf_link_add_object_symbols (abfd
, info
);
76 return elf_link_add_archive_symbols (abfd
, info
);
78 bfd_set_error (bfd_error_wrong_format
);
84 /* Add symbols from an ELF archive file to the linker hash table. We
85 don't use _bfd_generic_link_add_archive_symbols because of a
86 problem which arises on UnixWare. The UnixWare libc.so is an
87 archive which includes an entry libc.so.1 which defines a bunch of
88 symbols. The libc.so archive also includes a number of other
89 object files, which also define symbols, some of which are the same
90 as those defined in libc.so.1. Correct linking requires that we
91 consider each object file in turn, and include it if it defines any
92 symbols we need. _bfd_generic_link_add_archive_symbols does not do
93 this; it looks through the list of undefined symbols, and includes
94 any object file which defines them. When this algorithm is used on
95 UnixWare, it winds up pulling in libc.so.1 early and defining a
96 bunch of symbols. This means that some of the other objects in the
97 archive are not included in the link, which is incorrect since they
98 precede libc.so.1 in the archive.
100 Fortunately, ELF archive handling is simpler than that done by
101 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
102 oddities. In ELF, if we find a symbol in the archive map, and the
103 symbol is currently undefined, we know that we must pull in that
106 Unfortunately, we do have to make multiple passes over the symbol
107 table until nothing further is resolved. */
110 elf_link_add_archive_symbols (abfd
, info
)
112 struct bfd_link_info
*info
;
115 boolean
*defined
= NULL
;
116 boolean
*included
= NULL
;
120 if (! bfd_has_map (abfd
))
122 /* An empty archive is a special case. */
123 if (bfd_openr_next_archived_file (abfd
, (bfd
*) NULL
) == NULL
)
125 bfd_set_error (bfd_error_no_armap
);
129 /* Keep track of all symbols we know to be already defined, and all
130 files we know to be already included. This is to speed up the
131 second and subsequent passes. */
132 c
= bfd_ardata (abfd
)->symdef_count
;
135 defined
= (boolean
*) bfd_malloc (c
* sizeof (boolean
));
136 included
= (boolean
*) bfd_malloc (c
* sizeof (boolean
));
137 if (defined
== (boolean
*) NULL
|| included
== (boolean
*) NULL
)
139 memset (defined
, 0, c
* sizeof (boolean
));
140 memset (included
, 0, c
* sizeof (boolean
));
142 symdefs
= bfd_ardata (abfd
)->symdefs
;
155 symdefend
= symdef
+ c
;
156 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
158 struct elf_link_hash_entry
*h
;
160 struct bfd_link_hash_entry
*undefs_tail
;
163 if (defined
[i
] || included
[i
])
165 if (symdef
->file_offset
== last
)
171 h
= elf_link_hash_lookup (elf_hash_table (info
), symdef
->name
,
172 false, false, false);
178 /* If this is a default version (the name contains @@),
179 look up the symbol again without the version. The
180 effect is that references to the symbol without the
181 version will be matched by the default symbol in the
184 p
= strchr (symdef
->name
, ELF_VER_CHR
);
185 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
188 copy
= bfd_alloc (abfd
, p
- symdef
->name
+ 1);
191 memcpy (copy
, symdef
->name
, p
- symdef
->name
);
192 copy
[p
- symdef
->name
] = '\0';
194 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
195 false, false, false);
197 bfd_release (abfd
, copy
);
203 if (h
->root
.type
!= bfd_link_hash_undefined
)
205 if (h
->root
.type
!= bfd_link_hash_undefweak
)
210 /* We need to include this archive member. */
212 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
213 if (element
== (bfd
*) NULL
)
216 if (! bfd_check_format (element
, bfd_object
))
219 /* Doublecheck that we have not included this object
220 already--it should be impossible, but there may be
221 something wrong with the archive. */
222 if (element
->archive_pass
!= 0)
224 bfd_set_error (bfd_error_bad_value
);
227 element
->archive_pass
= 1;
229 undefs_tail
= info
->hash
->undefs_tail
;
231 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
234 if (! elf_link_add_object_symbols (element
, info
))
237 /* If there are any new undefined symbols, we need to make
238 another pass through the archive in order to see whether
239 they can be defined. FIXME: This isn't perfect, because
240 common symbols wind up on undefs_tail and because an
241 undefined symbol which is defined later on in this pass
242 does not require another pass. This isn't a bug, but it
243 does make the code less efficient than it could be. */
244 if (undefs_tail
!= info
->hash
->undefs_tail
)
247 /* Look backward to mark all symbols from this object file
248 which we have already seen in this pass. */
252 included
[mark
] = true;
257 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
259 /* We mark subsequent symbols from this object file as we go
260 on through the loop. */
261 last
= symdef
->file_offset
;
272 if (defined
!= (boolean
*) NULL
)
274 if (included
!= (boolean
*) NULL
)
279 /* This function is called when we want to define a new symbol. It
280 handles the various cases which arise when we find a definition in
281 a dynamic object, or when there is already a definition in a
282 dynamic object. The new symbol is described by NAME, SYM, PSEC,
283 and PVALUE. We set SYM_HASH to the hash table entry. We set
284 OVERRIDE if the old symbol is overriding a new definition. We set
285 TYPE_CHANGE_OK if it is OK for the type to change. We set
286 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
287 change, we mean that we shouldn't warn if the type or size does
291 elf_merge_symbol (abfd
, info
, name
, sym
, psec
, pvalue
, sym_hash
,
292 override
, type_change_ok
, size_change_ok
)
294 struct bfd_link_info
*info
;
296 Elf_Internal_Sym
*sym
;
299 struct elf_link_hash_entry
**sym_hash
;
301 boolean
*type_change_ok
;
302 boolean
*size_change_ok
;
305 struct elf_link_hash_entry
*h
;
308 boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
313 bind
= ELF_ST_BIND (sym
->st_info
);
315 if (! bfd_is_und_section (sec
))
316 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, false, false);
318 h
= ((struct elf_link_hash_entry
*)
319 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true, false, false));
324 /* This code is for coping with dynamic objects, and is only useful
325 if we are doing an ELF link. */
326 if (info
->hash
->creator
!= abfd
->xvec
)
329 /* For merging, we only care about real symbols. */
331 while (h
->root
.type
== bfd_link_hash_indirect
332 || h
->root
.type
== bfd_link_hash_warning
)
333 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
335 /* If we just created the symbol, mark it as being an ELF symbol.
336 Other than that, there is nothing to do--there is no merge issue
337 with a newly defined symbol--so we just return. */
339 if (h
->root
.type
== bfd_link_hash_new
)
341 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
345 /* OLDBFD is a BFD associated with the existing symbol. */
347 switch (h
->root
.type
)
353 case bfd_link_hash_undefined
:
354 case bfd_link_hash_undefweak
:
355 oldbfd
= h
->root
.u
.undef
.abfd
;
358 case bfd_link_hash_defined
:
359 case bfd_link_hash_defweak
:
360 oldbfd
= h
->root
.u
.def
.section
->owner
;
363 case bfd_link_hash_common
:
364 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
368 /* In cases involving weak versioned symbols, we may wind up trying
369 to merge a symbol with itself. Catch that here, to avoid the
370 confusion that results if we try to override a symbol with
371 itself. The additional tests catch cases like
372 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
373 dynamic object, which we do want to handle here. */
375 && ((abfd
->flags
& DYNAMIC
) == 0
376 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0))
379 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
380 respectively, is from a dynamic object. */
382 if ((abfd
->flags
& DYNAMIC
) != 0)
388 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
393 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
394 indices used by MIPS ELF. */
395 switch (h
->root
.type
)
401 case bfd_link_hash_defined
:
402 case bfd_link_hash_defweak
:
403 hsec
= h
->root
.u
.def
.section
;
406 case bfd_link_hash_common
:
407 hsec
= h
->root
.u
.c
.p
->section
;
414 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
417 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
418 respectively, appear to be a definition rather than reference. */
420 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
425 if (h
->root
.type
== bfd_link_hash_undefined
426 || h
->root
.type
== bfd_link_hash_undefweak
427 || h
->root
.type
== bfd_link_hash_common
)
432 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
433 symbol, respectively, appears to be a common symbol in a dynamic
434 object. If a symbol appears in an uninitialized section, and is
435 not weak, and is not a function, then it may be a common symbol
436 which was resolved when the dynamic object was created. We want
437 to treat such symbols specially, because they raise special
438 considerations when setting the symbol size: if the symbol
439 appears as a common symbol in a regular object, and the size in
440 the regular object is larger, we must make sure that we use the
441 larger size. This problematic case can always be avoided in C,
442 but it must be handled correctly when using Fortran shared
445 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
446 likewise for OLDDYNCOMMON and OLDDEF.
448 Note that this test is just a heuristic, and that it is quite
449 possible to have an uninitialized symbol in a shared object which
450 is really a definition, rather than a common symbol. This could
451 lead to some minor confusion when the symbol really is a common
452 symbol in some regular object. However, I think it will be
457 && (sec
->flags
& SEC_ALLOC
) != 0
458 && (sec
->flags
& SEC_LOAD
) == 0
461 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
464 newdyncommon
= false;
468 && h
->root
.type
== bfd_link_hash_defined
469 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
470 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
471 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
473 && h
->type
!= STT_FUNC
)
476 olddyncommon
= false;
478 /* It's OK to change the type if either the existing symbol or the
479 new symbol is weak. */
481 if (h
->root
.type
== bfd_link_hash_defweak
482 || h
->root
.type
== bfd_link_hash_undefweak
484 *type_change_ok
= true;
486 /* It's OK to change the size if either the existing symbol or the
487 new symbol is weak, or if the old symbol is undefined. */
490 || h
->root
.type
== bfd_link_hash_undefined
)
491 *size_change_ok
= true;
493 /* If both the old and the new symbols look like common symbols in a
494 dynamic object, set the size of the symbol to the larger of the
499 && sym
->st_size
!= h
->size
)
501 /* Since we think we have two common symbols, issue a multiple
502 common warning if desired. Note that we only warn if the
503 size is different. If the size is the same, we simply let
504 the old symbol override the new one as normally happens with
505 symbols defined in dynamic objects. */
507 if (! ((*info
->callbacks
->multiple_common
)
508 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
509 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
512 if (sym
->st_size
> h
->size
)
513 h
->size
= sym
->st_size
;
515 *size_change_ok
= true;
518 /* If we are looking at a dynamic object, and we have found a
519 definition, we need to see if the symbol was already defined by
520 some other object. If so, we want to use the existing
521 definition, and we do not want to report a multiple symbol
522 definition error; we do this by clobbering *PSEC to be
525 We treat a common symbol as a definition if the symbol in the
526 shared library is a function, since common symbols always
527 represent variables; this can cause confusion in principle, but
528 any such confusion would seem to indicate an erroneous program or
529 shared library. We also permit a common symbol in a regular
530 object to override a weak symbol in a shared object.
532 We prefer a non-weak definition in a shared library to a weak
533 definition in the executable. */
538 || (h
->root
.type
== bfd_link_hash_common
540 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)))
541 && (h
->root
.type
!= bfd_link_hash_defweak
542 || bind
== STB_WEAK
))
546 newdyncommon
= false;
548 *psec
= sec
= bfd_und_section_ptr
;
549 *size_change_ok
= true;
551 /* If we get here when the old symbol is a common symbol, then
552 we are explicitly letting it override a weak symbol or
553 function in a dynamic object, and we don't want to warn about
554 a type change. If the old symbol is a defined symbol, a type
555 change warning may still be appropriate. */
557 if (h
->root
.type
== bfd_link_hash_common
)
558 *type_change_ok
= true;
561 /* Handle the special case of an old common symbol merging with a
562 new symbol which looks like a common symbol in a shared object.
563 We change *PSEC and *PVALUE to make the new symbol look like a
564 common symbol, and let _bfd_generic_link_add_one_symbol will do
568 && h
->root
.type
== bfd_link_hash_common
)
572 newdyncommon
= false;
573 *pvalue
= sym
->st_size
;
574 *psec
= sec
= bfd_com_section_ptr
;
575 *size_change_ok
= true;
578 /* If the old symbol is from a dynamic object, and the new symbol is
579 a definition which is not from a dynamic object, then the new
580 symbol overrides the old symbol. Symbols from regular files
581 always take precedence over symbols from dynamic objects, even if
582 they are defined after the dynamic object in the link.
584 As above, we again permit a common symbol in a regular object to
585 override a definition in a shared object if the shared object
586 symbol is a function or is weak.
588 As above, we permit a non-weak definition in a shared object to
589 override a weak definition in a regular object. */
593 || (bfd_is_com_section (sec
)
594 && (h
->root
.type
== bfd_link_hash_defweak
595 || h
->type
== STT_FUNC
)))
598 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
600 || h
->root
.type
== bfd_link_hash_defweak
))
602 /* Change the hash table entry to undefined, and let
603 _bfd_generic_link_add_one_symbol do the right thing with the
606 h
->root
.type
= bfd_link_hash_undefined
;
607 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
608 *size_change_ok
= true;
611 olddyncommon
= false;
613 /* We again permit a type change when a common symbol may be
614 overriding a function. */
616 if (bfd_is_com_section (sec
))
617 *type_change_ok
= true;
619 /* This union may have been set to be non-NULL when this symbol
620 was seen in a dynamic object. We must force the union to be
621 NULL, so that it is correct for a regular symbol. */
623 h
->verinfo
.vertree
= NULL
;
625 /* In this special case, if H is the target of an indirection,
626 we want the caller to frob with H rather than with the
627 indirect symbol. That will permit the caller to redefine the
628 target of the indirection, rather than the indirect symbol
629 itself. FIXME: This will break the -y option if we store a
630 symbol with a different name. */
634 /* Handle the special case of a new common symbol merging with an
635 old symbol that looks like it might be a common symbol defined in
636 a shared object. Note that we have already handled the case in
637 which a new common symbol should simply override the definition
638 in the shared library. */
641 && bfd_is_com_section (sec
)
644 /* It would be best if we could set the hash table entry to a
645 common symbol, but we don't know what to use for the section
647 if (! ((*info
->callbacks
->multiple_common
)
648 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
649 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
652 /* If the predumed common symbol in the dynamic object is
653 larger, pretend that the new symbol has its size. */
655 if (h
->size
> *pvalue
)
658 /* FIXME: We no longer know the alignment required by the symbol
659 in the dynamic object, so we just wind up using the one from
660 the regular object. */
663 olddyncommon
= false;
665 h
->root
.type
= bfd_link_hash_undefined
;
666 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
668 *size_change_ok
= true;
669 *type_change_ok
= true;
671 h
->verinfo
.vertree
= NULL
;
674 /* Handle the special case of a weak definition in a regular object
675 followed by a non-weak definition in a shared object. In this
676 case, we prefer the definition in the shared object. */
678 && h
->root
.type
== bfd_link_hash_defweak
683 /* To make this work we have to frob the flags so that the rest
684 of the code does not think we are using the regular
686 h
->elf_link_hash_flags
&= ~ ELF_LINK_HASH_DEF_REGULAR
;
687 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
689 /* If H is the target of an indirection, we want the caller to
690 use H rather than the indirect symbol. Otherwise if we are
691 defining a new indirect symbol we will wind up attaching it
692 to the entry we are overriding. */
696 /* Handle the special case of a non-weak definition in a shared
697 object followed by a weak definition in a regular object. In
698 this case we prefer to definition in the shared object. To make
699 this work we have to tell the caller to not treat the new symbol
703 && h
->root
.type
!= bfd_link_hash_defweak
712 /* Add symbols from an ELF object file to the linker hash table. */
715 elf_link_add_object_symbols (abfd
, info
)
717 struct bfd_link_info
*info
;
719 boolean (*add_symbol_hook
) PARAMS ((bfd
*, struct bfd_link_info
*,
720 const Elf_Internal_Sym
*,
721 const char **, flagword
*,
722 asection
**, bfd_vma
*));
723 boolean (*check_relocs
) PARAMS ((bfd
*, struct bfd_link_info
*,
724 asection
*, const Elf_Internal_Rela
*));
726 Elf_Internal_Shdr
*hdr
;
730 Elf_External_Sym
*buf
= NULL
;
731 struct elf_link_hash_entry
**sym_hash
;
733 bfd_byte
*dynver
= NULL
;
734 Elf_External_Versym
*extversym
= NULL
;
735 Elf_External_Versym
*ever
;
736 Elf_External_Dyn
*dynbuf
= NULL
;
737 struct elf_link_hash_entry
*weaks
;
738 Elf_External_Sym
*esym
;
739 Elf_External_Sym
*esymend
;
741 add_symbol_hook
= get_elf_backend_data (abfd
)->elf_add_symbol_hook
;
742 collect
= get_elf_backend_data (abfd
)->collect
;
744 if ((abfd
->flags
& DYNAMIC
) == 0)
750 /* You can't use -r against a dynamic object. Also, there's no
751 hope of using a dynamic object which does not exactly match
752 the format of the output file. */
753 if (info
->relocateable
|| info
->hash
->creator
!= abfd
->xvec
)
755 bfd_set_error (bfd_error_invalid_operation
);
760 /* As a GNU extension, any input sections which are named
761 .gnu.warning.SYMBOL are treated as warning symbols for the given
762 symbol. This differs from .gnu.warning sections, which generate
763 warnings when they are included in an output file. */
768 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
772 name
= bfd_get_section_name (abfd
, s
);
773 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
778 name
+= sizeof ".gnu.warning." - 1;
780 /* If this is a shared object, then look up the symbol
781 in the hash table. If it is there, and it is already
782 been defined, then we will not be using the entry
783 from this shared object, so we don't need to warn.
784 FIXME: If we see the definition in a regular object
785 later on, we will warn, but we shouldn't. The only
786 fix is to keep track of what warnings we are supposed
787 to emit, and then handle them all at the end of the
789 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
791 struct elf_link_hash_entry
*h
;
793 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
796 /* FIXME: What about bfd_link_hash_common? */
798 && (h
->root
.type
== bfd_link_hash_defined
799 || h
->root
.type
== bfd_link_hash_defweak
))
801 /* We don't want to issue this warning. Clobber
802 the section size so that the warning does not
803 get copied into the output file. */
809 sz
= bfd_section_size (abfd
, s
);
810 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
814 if (! bfd_get_section_contents (abfd
, s
, msg
, (file_ptr
) 0, sz
))
819 if (! (_bfd_generic_link_add_one_symbol
820 (info
, abfd
, name
, BSF_WARNING
, s
, (bfd_vma
) 0, msg
,
821 false, collect
, (struct bfd_link_hash_entry
**) NULL
)))
824 if (! info
->relocateable
)
826 /* Clobber the section size so that the warning does
827 not get copied into the output file. */
834 /* If this is a dynamic object, we always link against the .dynsym
835 symbol table, not the .symtab symbol table. The dynamic linker
836 will only see the .dynsym symbol table, so there is no reason to
837 look at .symtab for a dynamic object. */
839 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
840 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
842 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
846 /* Read in any version definitions. */
848 if (! _bfd_elf_slurp_version_tables (abfd
))
851 /* Read in the symbol versions, but don't bother to convert them
852 to internal format. */
853 if (elf_dynversym (abfd
) != 0)
855 Elf_Internal_Shdr
*versymhdr
;
857 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
858 extversym
= (Elf_External_Versym
*) bfd_malloc (hdr
->sh_size
);
859 if (extversym
== NULL
)
861 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
862 || (bfd_read ((PTR
) extversym
, 1, versymhdr
->sh_size
, abfd
)
863 != versymhdr
->sh_size
))
868 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
870 /* The sh_info field of the symtab header tells us where the
871 external symbols start. We don't care about the local symbols at
873 if (elf_bad_symtab (abfd
))
875 extsymcount
= symcount
;
880 extsymcount
= symcount
- hdr
->sh_info
;
881 extsymoff
= hdr
->sh_info
;
884 buf
= ((Elf_External_Sym
*)
885 bfd_malloc (extsymcount
* sizeof (Elf_External_Sym
)));
886 if (buf
== NULL
&& extsymcount
!= 0)
889 /* We store a pointer to the hash table entry for each external
891 sym_hash
= ((struct elf_link_hash_entry
**)
893 extsymcount
* sizeof (struct elf_link_hash_entry
*)));
894 if (sym_hash
== NULL
)
896 elf_sym_hashes (abfd
) = sym_hash
;
900 /* If we are creating a shared library, create all the dynamic
901 sections immediately. We need to attach them to something,
902 so we attach them to this BFD, provided it is the right
903 format. FIXME: If there are no input BFD's of the same
904 format as the output, we can't make a shared library. */
906 && ! elf_hash_table (info
)->dynamic_sections_created
907 && abfd
->xvec
== info
->hash
->creator
)
909 if (! elf_link_create_dynamic_sections (abfd
, info
))
918 bfd_size_type oldsize
;
919 bfd_size_type strindex
;
921 /* Find the name to use in a DT_NEEDED entry that refers to this
922 object. If the object has a DT_SONAME entry, we use it.
923 Otherwise, if the generic linker stuck something in
924 elf_dt_name, we use that. Otherwise, we just use the file
925 name. If the generic linker put a null string into
926 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
927 there is a DT_SONAME entry. */
929 name
= bfd_get_filename (abfd
);
930 if (elf_dt_name (abfd
) != NULL
)
932 name
= elf_dt_name (abfd
);
936 s
= bfd_get_section_by_name (abfd
, ".dynamic");
939 Elf_External_Dyn
*extdyn
;
940 Elf_External_Dyn
*extdynend
;
944 dynbuf
= (Elf_External_Dyn
*) bfd_malloc ((size_t) s
->_raw_size
);
948 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
949 (file_ptr
) 0, s
->_raw_size
))
952 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
955 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
958 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
959 for (; extdyn
< extdynend
; extdyn
++)
961 Elf_Internal_Dyn dyn
;
963 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
964 if (dyn
.d_tag
== DT_SONAME
)
966 name
= bfd_elf_string_from_elf_section (abfd
, link
,
971 if (dyn
.d_tag
== DT_NEEDED
)
973 struct bfd_link_needed_list
*n
, **pn
;
976 n
= ((struct bfd_link_needed_list
*)
977 bfd_alloc (abfd
, sizeof (struct bfd_link_needed_list
)));
978 fnm
= bfd_elf_string_from_elf_section (abfd
, link
,
980 if (n
== NULL
|| fnm
== NULL
)
982 anm
= bfd_alloc (abfd
, strlen (fnm
) + 1);
989 for (pn
= &elf_hash_table (info
)->needed
;
1001 /* We do not want to include any of the sections in a dynamic
1002 object in the output file. We hack by simply clobbering the
1003 list of sections in the BFD. This could be handled more
1004 cleanly by, say, a new section flag; the existing
1005 SEC_NEVER_LOAD flag is not the one we want, because that one
1006 still implies that the section takes up space in the output
1008 abfd
->sections
= NULL
;
1009 abfd
->section_count
= 0;
1011 /* If this is the first dynamic object found in the link, create
1012 the special sections required for dynamic linking. */
1013 if (! elf_hash_table (info
)->dynamic_sections_created
)
1015 if (! elf_link_create_dynamic_sections (abfd
, info
))
1021 /* Add a DT_NEEDED entry for this dynamic object. */
1022 oldsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
1023 strindex
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, name
,
1025 if (strindex
== (bfd_size_type
) -1)
1028 if (oldsize
== _bfd_stringtab_size (elf_hash_table (info
)->dynstr
))
1031 Elf_External_Dyn
*dyncon
, *dynconend
;
1033 /* The hash table size did not change, which means that
1034 the dynamic object name was already entered. If we
1035 have already included this dynamic object in the
1036 link, just ignore it. There is no reason to include
1037 a particular dynamic object more than once. */
1038 sdyn
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
1040 BFD_ASSERT (sdyn
!= NULL
);
1042 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1043 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1045 for (; dyncon
< dynconend
; dyncon
++)
1047 Elf_Internal_Dyn dyn
;
1049 elf_swap_dyn_in (elf_hash_table (info
)->dynobj
, dyncon
,
1051 if (dyn
.d_tag
== DT_NEEDED
1052 && dyn
.d_un
.d_val
== strindex
)
1056 if (extversym
!= NULL
)
1063 if (! elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
1067 /* Save the SONAME, if there is one, because sometimes the
1068 linker emulation code will need to know it. */
1070 name
= bfd_get_filename (abfd
);
1071 elf_dt_name (abfd
) = name
;
1075 hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
),
1077 || (bfd_read ((PTR
) buf
, sizeof (Elf_External_Sym
), extsymcount
, abfd
)
1078 != extsymcount
* sizeof (Elf_External_Sym
)))
1083 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
1084 esymend
= buf
+ extsymcount
;
1087 esym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
1089 Elf_Internal_Sym sym
;
1095 struct elf_link_hash_entry
*h
;
1097 boolean size_change_ok
, type_change_ok
;
1098 boolean new_weakdef
;
1099 unsigned int old_alignment
;
1101 elf_swap_symbol_in (abfd
, esym
, &sym
);
1103 flags
= BSF_NO_FLAGS
;
1105 value
= sym
.st_value
;
1108 bind
= ELF_ST_BIND (sym
.st_info
);
1109 if (bind
== STB_LOCAL
)
1111 /* This should be impossible, since ELF requires that all
1112 global symbols follow all local symbols, and that sh_info
1113 point to the first global symbol. Unfortunatealy, Irix 5
1117 else if (bind
== STB_GLOBAL
)
1119 if (sym
.st_shndx
!= SHN_UNDEF
1120 && sym
.st_shndx
!= SHN_COMMON
)
1125 else if (bind
== STB_WEAK
)
1129 /* Leave it up to the processor backend. */
1132 if (sym
.st_shndx
== SHN_UNDEF
)
1133 sec
= bfd_und_section_ptr
;
1134 else if (sym
.st_shndx
> 0 && sym
.st_shndx
< SHN_LORESERVE
)
1136 sec
= section_from_elf_index (abfd
, sym
.st_shndx
);
1138 sec
= bfd_abs_section_ptr
;
1139 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
1142 else if (sym
.st_shndx
== SHN_ABS
)
1143 sec
= bfd_abs_section_ptr
;
1144 else if (sym
.st_shndx
== SHN_COMMON
)
1146 sec
= bfd_com_section_ptr
;
1147 /* What ELF calls the size we call the value. What ELF
1148 calls the value we call the alignment. */
1149 value
= sym
.st_size
;
1153 /* Leave it up to the processor backend. */
1156 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
1157 if (name
== (const char *) NULL
)
1160 if (add_symbol_hook
)
1162 if (! (*add_symbol_hook
) (abfd
, info
, &sym
, &name
, &flags
, &sec
,
1166 /* The hook function sets the name to NULL if this symbol
1167 should be skipped for some reason. */
1168 if (name
== (const char *) NULL
)
1172 /* Sanity check that all possibilities were handled. */
1173 if (sec
== (asection
*) NULL
)
1175 bfd_set_error (bfd_error_bad_value
);
1179 if (bfd_is_und_section (sec
)
1180 || bfd_is_com_section (sec
))
1185 size_change_ok
= false;
1186 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
1188 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1190 Elf_Internal_Versym iver
;
1191 unsigned int vernum
= 0;
1196 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
1197 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
1199 /* If this is a hidden symbol, or if it is not version
1200 1, we append the version name to the symbol name.
1201 However, we do not modify a non-hidden absolute
1202 symbol, because it might be the version symbol
1203 itself. FIXME: What if it isn't? */
1204 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
1205 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
1208 int namelen
, newlen
;
1211 if (sym
.st_shndx
!= SHN_UNDEF
)
1213 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
1215 (*_bfd_error_handler
)
1216 (_("%s: %s: invalid version %u (max %d)"),
1217 bfd_get_filename (abfd
), name
, vernum
,
1218 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1219 bfd_set_error (bfd_error_bad_value
);
1222 else if (vernum
> 1)
1224 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1230 /* We cannot simply test for the number of
1231 entries in the VERNEED section since the
1232 numbers for the needed versions do not start
1234 Elf_Internal_Verneed
*t
;
1237 for (t
= elf_tdata (abfd
)->verref
;
1241 Elf_Internal_Vernaux
*a
;
1243 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1245 if (a
->vna_other
== vernum
)
1247 verstr
= a
->vna_nodename
;
1256 (*_bfd_error_handler
)
1257 (_("%s: %s: invalid needed version %d"),
1258 bfd_get_filename (abfd
), name
, vernum
);
1259 bfd_set_error (bfd_error_bad_value
);
1264 namelen
= strlen (name
);
1265 newlen
= namelen
+ strlen (verstr
) + 2;
1266 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
1269 newname
= (char *) bfd_alloc (abfd
, newlen
);
1270 if (newname
== NULL
)
1272 strcpy (newname
, name
);
1273 p
= newname
+ namelen
;
1275 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
1283 if (! elf_merge_symbol (abfd
, info
, name
, &sym
, &sec
, &value
,
1284 sym_hash
, &override
, &type_change_ok
,
1292 while (h
->root
.type
== bfd_link_hash_indirect
1293 || h
->root
.type
== bfd_link_hash_warning
)
1294 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1296 /* Remember the old alignment if this is a common symbol, so
1297 that we don't reduce the alignment later on. We can't
1298 check later, because _bfd_generic_link_add_one_symbol
1299 will set a default for the alignment which we want to
1301 if (h
->root
.type
== bfd_link_hash_common
)
1302 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1304 if (elf_tdata (abfd
)->verdef
!= NULL
1308 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1311 if (! (_bfd_generic_link_add_one_symbol
1312 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
1313 false, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
1317 while (h
->root
.type
== bfd_link_hash_indirect
1318 || h
->root
.type
== bfd_link_hash_warning
)
1319 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1322 new_weakdef
= false;
1325 && (flags
& BSF_WEAK
) != 0
1326 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
1327 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1328 && h
->weakdef
== NULL
)
1330 /* Keep a list of all weak defined non function symbols from
1331 a dynamic object, using the weakdef field. Later in this
1332 function we will set the weakdef field to the correct
1333 value. We only put non-function symbols from dynamic
1334 objects on this list, because that happens to be the only
1335 time we need to know the normal symbol corresponding to a
1336 weak symbol, and the information is time consuming to
1337 figure out. If the weakdef field is not already NULL,
1338 then this symbol was already defined by some previous
1339 dynamic object, and we will be using that previous
1340 definition anyhow. */
1347 /* Set the alignment of a common symbol. */
1348 if (sym
.st_shndx
== SHN_COMMON
1349 && h
->root
.type
== bfd_link_hash_common
)
1353 align
= bfd_log2 (sym
.st_value
);
1354 if (align
> old_alignment
)
1355 h
->root
.u
.c
.p
->alignment_power
= align
;
1358 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1364 /* Remember the symbol size and type. */
1365 if (sym
.st_size
!= 0
1366 && (definition
|| h
->size
== 0))
1368 if (h
->size
!= 0 && h
->size
!= sym
.st_size
&& ! size_change_ok
)
1369 (*_bfd_error_handler
)
1370 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1371 name
, (unsigned long) h
->size
, (unsigned long) sym
.st_size
,
1372 bfd_get_filename (abfd
));
1374 h
->size
= sym
.st_size
;
1377 /* If this is a common symbol, then we always want H->SIZE
1378 to be the size of the common symbol. The code just above
1379 won't fix the size if a common symbol becomes larger. We
1380 don't warn about a size change here, because that is
1381 covered by --warn-common. */
1382 if (h
->root
.type
== bfd_link_hash_common
)
1383 h
->size
= h
->root
.u
.c
.size
;
1385 if (ELF_ST_TYPE (sym
.st_info
) != STT_NOTYPE
1386 && (definition
|| h
->type
== STT_NOTYPE
))
1388 if (h
->type
!= STT_NOTYPE
1389 && h
->type
!= ELF_ST_TYPE (sym
.st_info
)
1390 && ! type_change_ok
)
1391 (*_bfd_error_handler
)
1392 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1393 name
, h
->type
, ELF_ST_TYPE (sym
.st_info
),
1394 bfd_get_filename (abfd
));
1396 h
->type
= ELF_ST_TYPE (sym
.st_info
);
1399 if (sym
.st_other
!= 0
1400 && (definition
|| h
->other
== 0))
1401 h
->other
= sym
.st_other
;
1403 /* Set a flag in the hash table entry indicating the type of
1404 reference or definition we just found. Keep a count of
1405 the number of dynamic symbols we find. A dynamic symbol
1406 is one which is referenced or defined by both a regular
1407 object and a shared object. */
1408 old_flags
= h
->elf_link_hash_flags
;
1414 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
1415 if (bind
!= STB_WEAK
)
1416 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
1419 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
1421 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1422 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
1428 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
1430 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
1431 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
1432 | ELF_LINK_HASH_REF_REGULAR
)) != 0
1433 || (h
->weakdef
!= NULL
1435 && h
->weakdef
->dynindx
!= -1))
1439 h
->elf_link_hash_flags
|= new_flag
;
1441 /* If this symbol has a version, and it is the default
1442 version, we create an indirect symbol from the default
1443 name to the fully decorated name. This will cause
1444 external references which do not specify a version to be
1445 bound to this version of the symbol. */
1450 p
= strchr (name
, ELF_VER_CHR
);
1451 if (p
!= NULL
&& p
[1] == ELF_VER_CHR
)
1454 struct elf_link_hash_entry
*hi
;
1457 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1459 if (shortname
== NULL
)
1461 strncpy (shortname
, name
, p
- name
);
1462 shortname
[p
- name
] = '\0';
1464 /* We are going to create a new symbol. Merge it
1465 with any existing symbol with this name. For the
1466 purposes of the merge, act as though we were
1467 defining the symbol we just defined, although we
1468 actually going to define an indirect symbol. */
1469 type_change_ok
= false;
1470 size_change_ok
= false;
1471 if (! elf_merge_symbol (abfd
, info
, shortname
, &sym
, &sec
,
1472 &value
, &hi
, &override
,
1473 &type_change_ok
, &size_change_ok
))
1478 if (! (_bfd_generic_link_add_one_symbol
1479 (info
, abfd
, shortname
, BSF_INDIRECT
,
1480 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1481 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1486 /* In this case the symbol named SHORTNAME is
1487 overriding the indirect symbol we want to
1488 add. We were planning on making SHORTNAME an
1489 indirect symbol referring to NAME. SHORTNAME
1490 is the name without a version. NAME is the
1491 fully versioned name, and it is the default
1494 Overriding means that we already saw a
1495 definition for the symbol SHORTNAME in a
1496 regular object, and it is overriding the
1497 symbol defined in the dynamic object.
1499 When this happens, we actually want to change
1500 NAME, the symbol we just added, to refer to
1501 SHORTNAME. This will cause references to
1502 NAME in the shared object to become
1503 references to SHORTNAME in the regular
1504 object. This is what we expect when we
1505 override a function in a shared object: that
1506 the references in the shared object will be
1507 mapped to the definition in the regular
1510 while (hi
->root
.type
== bfd_link_hash_indirect
1511 || hi
->root
.type
== bfd_link_hash_warning
)
1512 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1514 h
->root
.type
= bfd_link_hash_indirect
;
1515 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1516 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1518 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1519 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1520 if (hi
->elf_link_hash_flags
1521 & (ELF_LINK_HASH_REF_REGULAR
1522 | ELF_LINK_HASH_DEF_REGULAR
))
1524 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1530 /* Now set HI to H, so that the following code
1531 will set the other fields correctly. */
1535 /* If there is a duplicate definition somewhere,
1536 then HI may not point to an indirect symbol. We
1537 will have reported an error to the user in that
1540 if (hi
->root
.type
== bfd_link_hash_indirect
)
1542 struct elf_link_hash_entry
*ht
;
1544 /* If the symbol became indirect, then we assume
1545 that we have not seen a definition before. */
1546 BFD_ASSERT ((hi
->elf_link_hash_flags
1547 & (ELF_LINK_HASH_DEF_DYNAMIC
1548 | ELF_LINK_HASH_DEF_REGULAR
))
1551 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1553 /* Copy down any references that we may have
1554 already seen to the symbol which just became
1556 ht
->elf_link_hash_flags
|=
1557 (hi
->elf_link_hash_flags
1558 & (ELF_LINK_HASH_REF_DYNAMIC
1559 | ELF_LINK_HASH_REF_REGULAR
1560 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
));
1562 /* Copy over the global and procedure linkage table
1563 offset entries. These may have been already set
1564 up by a check_relocs routine. */
1565 if (ht
->got
.offset
== (bfd_vma
) -1)
1567 ht
->got
.offset
= hi
->got
.offset
;
1568 hi
->got
.offset
= (bfd_vma
) -1;
1570 BFD_ASSERT (hi
->got
.offset
== (bfd_vma
) -1);
1572 if (ht
->plt
.offset
== (bfd_vma
) -1)
1574 ht
->plt
.offset
= hi
->plt
.offset
;
1575 hi
->plt
.offset
= (bfd_vma
) -1;
1577 BFD_ASSERT (hi
->plt
.offset
== (bfd_vma
) -1);
1579 if (ht
->dynindx
== -1)
1581 ht
->dynindx
= hi
->dynindx
;
1582 ht
->dynstr_index
= hi
->dynstr_index
;
1584 hi
->dynstr_index
= 0;
1586 BFD_ASSERT (hi
->dynindx
== -1);
1588 /* FIXME: There may be other information to copy
1589 over for particular targets. */
1591 /* See if the new flags lead us to realize that
1592 the symbol must be dynamic. */
1598 || ((hi
->elf_link_hash_flags
1599 & ELF_LINK_HASH_REF_DYNAMIC
)
1605 if ((hi
->elf_link_hash_flags
1606 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1612 /* We also need to define an indirection from the
1613 nondefault version of the symbol. */
1615 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1617 if (shortname
== NULL
)
1619 strncpy (shortname
, name
, p
- name
);
1620 strcpy (shortname
+ (p
- name
), p
+ 1);
1622 /* Once again, merge with any existing symbol. */
1623 type_change_ok
= false;
1624 size_change_ok
= false;
1625 if (! elf_merge_symbol (abfd
, info
, shortname
, &sym
, &sec
,
1626 &value
, &hi
, &override
,
1627 &type_change_ok
, &size_change_ok
))
1632 /* Here SHORTNAME is a versioned name, so we
1633 don't expect to see the type of override we
1634 do in the case above. */
1635 (*_bfd_error_handler
)
1636 (_("%s: warning: unexpected redefinition of `%s'"),
1637 bfd_get_filename (abfd
), shortname
);
1641 if (! (_bfd_generic_link_add_one_symbol
1642 (info
, abfd
, shortname
, BSF_INDIRECT
,
1643 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1644 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1647 /* If there is a duplicate definition somewhere,
1648 then HI may not point to an indirect symbol.
1649 We will have reported an error to the user in
1652 if (hi
->root
.type
== bfd_link_hash_indirect
)
1654 /* If the symbol became indirect, then we
1655 assume that we have not seen a definition
1657 BFD_ASSERT ((hi
->elf_link_hash_flags
1658 & (ELF_LINK_HASH_DEF_DYNAMIC
1659 | ELF_LINK_HASH_DEF_REGULAR
))
1662 /* Copy down any references that we may have
1663 already seen to the symbol which just
1665 h
->elf_link_hash_flags
|=
1666 (hi
->elf_link_hash_flags
1667 & (ELF_LINK_HASH_REF_DYNAMIC
1668 | ELF_LINK_HASH_REF_REGULAR
1669 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
));
1671 /* Copy over the global and procedure linkage
1672 table offset entries. These may have been
1673 already set up by a check_relocs routine. */
1674 if (h
->got
.offset
== (bfd_vma
) -1)
1676 h
->got
.offset
= hi
->got
.offset
;
1677 hi
->got
.offset
= (bfd_vma
) -1;
1679 BFD_ASSERT (hi
->got
.offset
== (bfd_vma
) -1);
1681 if (h
->plt
.offset
== (bfd_vma
) -1)
1683 h
->plt
.offset
= hi
->plt
.offset
;
1684 hi
->plt
.offset
= (bfd_vma
) -1;
1686 BFD_ASSERT (hi
->got
.offset
== (bfd_vma
) -1);
1688 if (h
->dynindx
== -1)
1690 h
->dynindx
= hi
->dynindx
;
1691 h
->dynstr_index
= hi
->dynstr_index
;
1693 hi
->dynstr_index
= 0;
1695 BFD_ASSERT (hi
->dynindx
== -1);
1697 /* FIXME: There may be other information to
1698 copy over for particular targets. */
1700 /* See if the new flags lead us to realize
1701 that the symbol must be dynamic. */
1707 || ((hi
->elf_link_hash_flags
1708 & ELF_LINK_HASH_REF_DYNAMIC
)
1714 if ((hi
->elf_link_hash_flags
1715 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1724 if (dynsym
&& h
->dynindx
== -1)
1726 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1728 if (h
->weakdef
!= NULL
1730 && h
->weakdef
->dynindx
== -1)
1732 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1740 /* Now set the weakdefs field correctly for all the weak defined
1741 symbols we found. The only way to do this is to search all the
1742 symbols. Since we only need the information for non functions in
1743 dynamic objects, that's the only time we actually put anything on
1744 the list WEAKS. We need this information so that if a regular
1745 object refers to a symbol defined weakly in a dynamic object, the
1746 real symbol in the dynamic object is also put in the dynamic
1747 symbols; we also must arrange for both symbols to point to the
1748 same memory location. We could handle the general case of symbol
1749 aliasing, but a general symbol alias can only be generated in
1750 assembler code, handling it correctly would be very time
1751 consuming, and other ELF linkers don't handle general aliasing
1753 while (weaks
!= NULL
)
1755 struct elf_link_hash_entry
*hlook
;
1758 struct elf_link_hash_entry
**hpp
;
1759 struct elf_link_hash_entry
**hppend
;
1762 weaks
= hlook
->weakdef
;
1763 hlook
->weakdef
= NULL
;
1765 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
1766 || hlook
->root
.type
== bfd_link_hash_defweak
1767 || hlook
->root
.type
== bfd_link_hash_common
1768 || hlook
->root
.type
== bfd_link_hash_indirect
);
1769 slook
= hlook
->root
.u
.def
.section
;
1770 vlook
= hlook
->root
.u
.def
.value
;
1772 hpp
= elf_sym_hashes (abfd
);
1773 hppend
= hpp
+ extsymcount
;
1774 for (; hpp
< hppend
; hpp
++)
1776 struct elf_link_hash_entry
*h
;
1779 if (h
!= NULL
&& h
!= hlook
1780 && h
->root
.type
== bfd_link_hash_defined
1781 && h
->root
.u
.def
.section
== slook
1782 && h
->root
.u
.def
.value
== vlook
)
1786 /* If the weak definition is in the list of dynamic
1787 symbols, make sure the real definition is put there
1789 if (hlook
->dynindx
!= -1
1790 && h
->dynindx
== -1)
1792 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1796 /* If the real definition is in the list of dynamic
1797 symbols, make sure the weak definition is put there
1798 as well. If we don't do this, then the dynamic
1799 loader might not merge the entries for the real
1800 definition and the weak definition. */
1801 if (h
->dynindx
!= -1
1802 && hlook
->dynindx
== -1)
1804 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
1819 if (extversym
!= NULL
)
1825 /* If this object is the same format as the output object, and it is
1826 not a shared library, then let the backend look through the
1829 This is required to build global offset table entries and to
1830 arrange for dynamic relocs. It is not required for the
1831 particular common case of linking non PIC code, even when linking
1832 against shared libraries, but unfortunately there is no way of
1833 knowing whether an object file has been compiled PIC or not.
1834 Looking through the relocs is not particularly time consuming.
1835 The problem is that we must either (1) keep the relocs in memory,
1836 which causes the linker to require additional runtime memory or
1837 (2) read the relocs twice from the input file, which wastes time.
1838 This would be a good case for using mmap.
1840 I have no idea how to handle linking PIC code into a file of a
1841 different format. It probably can't be done. */
1842 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
1844 && abfd
->xvec
== info
->hash
->creator
1845 && check_relocs
!= NULL
)
1849 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1851 Elf_Internal_Rela
*internal_relocs
;
1854 if ((o
->flags
& SEC_RELOC
) == 0
1855 || o
->reloc_count
== 0
1856 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
1857 && (o
->flags
& SEC_DEBUGGING
) != 0)
1858 || bfd_is_abs_section (o
->output_section
))
1861 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
1862 (abfd
, o
, (PTR
) NULL
,
1863 (Elf_Internal_Rela
*) NULL
,
1864 info
->keep_memory
));
1865 if (internal_relocs
== NULL
)
1868 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
1870 if (! info
->keep_memory
)
1871 free (internal_relocs
);
1878 /* If this is a non-traditional, non-relocateable link, try to
1879 optimize the handling of the .stab/.stabstr sections. */
1881 && ! info
->relocateable
1882 && ! info
->traditional_format
1883 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1884 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
1886 asection
*stab
, *stabstr
;
1888 stab
= bfd_get_section_by_name (abfd
, ".stab");
1891 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
1893 if (stabstr
!= NULL
)
1895 struct bfd_elf_section_data
*secdata
;
1897 secdata
= elf_section_data (stab
);
1898 if (! _bfd_link_section_stabs (abfd
,
1899 &elf_hash_table (info
)->stab_info
,
1901 &secdata
->stab_info
))
1916 if (extversym
!= NULL
)
1921 /* Create some sections which will be filled in with dynamic linking
1922 information. ABFD is an input file which requires dynamic sections
1923 to be created. The dynamic sections take up virtual memory space
1924 when the final executable is run, so we need to create them before
1925 addresses are assigned to the output sections. We work out the
1926 actual contents and size of these sections later. */
1929 elf_link_create_dynamic_sections (abfd
, info
)
1931 struct bfd_link_info
*info
;
1934 register asection
*s
;
1935 struct elf_link_hash_entry
*h
;
1936 struct elf_backend_data
*bed
;
1938 if (elf_hash_table (info
)->dynamic_sections_created
)
1941 /* Make sure that all dynamic sections use the same input BFD. */
1942 if (elf_hash_table (info
)->dynobj
== NULL
)
1943 elf_hash_table (info
)->dynobj
= abfd
;
1945 abfd
= elf_hash_table (info
)->dynobj
;
1947 /* Note that we set the SEC_IN_MEMORY flag for all of these
1949 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1950 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
1952 /* A dynamically linked executable has a .interp section, but a
1953 shared library does not. */
1956 s
= bfd_make_section (abfd
, ".interp");
1958 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
1962 /* Create sections to hold version informations. These are removed
1963 if they are not needed. */
1964 s
= bfd_make_section (abfd
, ".gnu.version_d");
1966 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1967 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1970 s
= bfd_make_section (abfd
, ".gnu.version");
1972 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1973 || ! bfd_set_section_alignment (abfd
, s
, 1))
1976 s
= bfd_make_section (abfd
, ".gnu.version_r");
1978 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1979 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1982 s
= bfd_make_section (abfd
, ".dynsym");
1984 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1985 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1988 s
= bfd_make_section (abfd
, ".dynstr");
1990 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
1993 /* Create a strtab to hold the dynamic symbol names. */
1994 if (elf_hash_table (info
)->dynstr
== NULL
)
1996 elf_hash_table (info
)->dynstr
= elf_stringtab_init ();
1997 if (elf_hash_table (info
)->dynstr
== NULL
)
2001 s
= bfd_make_section (abfd
, ".dynamic");
2003 || ! bfd_set_section_flags (abfd
, s
, flags
)
2004 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2007 /* The special symbol _DYNAMIC is always set to the start of the
2008 .dynamic section. This call occurs before we have processed the
2009 symbols for any dynamic object, so we don't have to worry about
2010 overriding a dynamic definition. We could set _DYNAMIC in a
2011 linker script, but we only want to define it if we are, in fact,
2012 creating a .dynamic section. We don't want to define it if there
2013 is no .dynamic section, since on some ELF platforms the start up
2014 code examines it to decide how to initialize the process. */
2016 if (! (_bfd_generic_link_add_one_symbol
2017 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
2018 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
2019 (struct bfd_link_hash_entry
**) &h
)))
2021 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2022 h
->type
= STT_OBJECT
;
2025 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2028 bed
= get_elf_backend_data (abfd
);
2030 s
= bfd_make_section (abfd
, ".hash");
2032 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2033 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2035 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
2037 /* Let the backend create the rest of the sections. This lets the
2038 backend set the right flags. The backend will normally create
2039 the .got and .plt sections. */
2040 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
2043 elf_hash_table (info
)->dynamic_sections_created
= true;
2048 /* Add an entry to the .dynamic table. */
2051 elf_add_dynamic_entry (info
, tag
, val
)
2052 struct bfd_link_info
*info
;
2056 Elf_Internal_Dyn dyn
;
2060 bfd_byte
*newcontents
;
2062 dynobj
= elf_hash_table (info
)->dynobj
;
2064 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
2065 BFD_ASSERT (s
!= NULL
);
2067 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
2068 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
2069 if (newcontents
== NULL
)
2073 dyn
.d_un
.d_val
= val
;
2074 elf_swap_dyn_out (dynobj
, &dyn
,
2075 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
2077 s
->_raw_size
= newsize
;
2078 s
->contents
= newcontents
;
2083 /* Record a new local dynamic symbol. */
2086 elf_link_record_local_dynamic_symbol (info
, input_bfd
, input_indx
)
2087 struct bfd_link_info
*info
;
2091 struct elf_link_local_dynamic_entry
*entry
;
2092 struct elf_link_hash_table
*eht
;
2093 struct bfd_strtab_hash
*dynstr
;
2094 Elf_External_Sym esym
;
2095 unsigned long dynstr_index
;
2098 /* See if the entry exists already. */
2099 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
2100 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
2103 entry
= (struct elf_link_local_dynamic_entry
*)
2104 bfd_alloc (input_bfd
, sizeof (*entry
));
2108 /* Go find the symbol, so that we can find it's name. */
2109 if (bfd_seek (input_bfd
,
2110 (elf_tdata (input_bfd
)->symtab_hdr
.sh_offset
2111 + input_indx
* sizeof (Elf_External_Sym
)),
2113 || (bfd_read (&esym
, sizeof (Elf_External_Sym
), 1, input_bfd
)
2114 != sizeof (Elf_External_Sym
)))
2116 elf_swap_symbol_in (input_bfd
, &esym
, &entry
->isym
);
2118 name
= (bfd_elf_string_from_elf_section
2119 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
2120 entry
->isym
.st_name
));
2122 dynstr
= elf_hash_table (info
)->dynstr
;
2125 /* Create a strtab to hold the dynamic symbol names. */
2126 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_stringtab_init ();
2131 dynstr_index
= _bfd_stringtab_add (dynstr
, name
, true, false);
2132 if (dynstr_index
== (unsigned long) -1)
2134 entry
->isym
.st_name
= dynstr_index
;
2136 eht
= elf_hash_table (info
);
2138 entry
->next
= eht
->dynlocal
;
2139 eht
->dynlocal
= entry
;
2140 entry
->input_bfd
= input_bfd
;
2141 entry
->input_indx
= input_indx
;
2144 /* Whatever binding the symbol had before, it's now local. */
2146 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
2148 /* The dynindx will be set at the end of size_dynamic_sections. */
2154 /* Read and swap the relocs from the section indicated by SHDR. This
2155 may be either a REL or a RELA section. The relocations are
2156 translated into RELA relocations and stored in INTERNAL_RELOCS,
2157 which should have already been allocated to contain enough space.
2158 The EXTERNAL_RELOCS are a buffer where the external form of the
2159 relocations should be stored.
2161 Returns false if something goes wrong. */
2164 elf_link_read_relocs_from_section (abfd
, shdr
, external_relocs
,
2167 Elf_Internal_Shdr
*shdr
;
2168 PTR external_relocs
;
2169 Elf_Internal_Rela
*internal_relocs
;
2171 struct elf_backend_data
*bed
;
2173 /* If there aren't any relocations, that's OK. */
2177 /* Position ourselves at the start of the section. */
2178 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2181 /* Read the relocations. */
2182 if (bfd_read (external_relocs
, 1, shdr
->sh_size
, abfd
)
2186 bed
= get_elf_backend_data (abfd
);
2188 /* Convert the external relocations to the internal format. */
2189 if (shdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2191 Elf_External_Rel
*erel
;
2192 Elf_External_Rel
*erelend
;
2193 Elf_Internal_Rela
*irela
;
2194 Elf_Internal_Rel
*irel
;
2196 erel
= (Elf_External_Rel
*) external_relocs
;
2197 erelend
= erel
+ shdr
->sh_size
/ shdr
->sh_entsize
;
2198 irela
= internal_relocs
;
2199 irel
= bfd_alloc (abfd
, (bed
->s
->int_rels_per_ext_rel
2200 * sizeof (Elf_Internal_Rel
)));
2201 for (; erel
< erelend
; erel
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2205 if (bed
->s
->swap_reloc_in
)
2206 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
2208 elf_swap_reloc_in (abfd
, erel
, irel
);
2210 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; ++i
)
2212 irela
[i
].r_offset
= irel
[i
].r_offset
;
2213 irela
[i
].r_info
= irel
[i
].r_info
;
2214 irela
[i
].r_addend
= 0;
2220 Elf_External_Rela
*erela
;
2221 Elf_External_Rela
*erelaend
;
2222 Elf_Internal_Rela
*irela
;
2224 BFD_ASSERT (shdr
->sh_entsize
== sizeof (Elf_External_Rela
));
2226 erela
= (Elf_External_Rela
*) external_relocs
;
2227 erelaend
= erela
+ shdr
->sh_size
/ shdr
->sh_entsize
;
2228 irela
= internal_relocs
;
2229 for (; erela
< erelaend
; erela
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2231 if (bed
->s
->swap_reloca_in
)
2232 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
2234 elf_swap_reloca_in (abfd
, erela
, irela
);
2241 /* Read and swap the relocs for a section O. They may have been
2242 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2243 not NULL, they are used as buffers to read into. They are known to
2244 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2245 the return value is allocated using either malloc or bfd_alloc,
2246 according to the KEEP_MEMORY argument. If O has two relocation
2247 sections (both REL and RELA relocations), then the REL_HDR
2248 relocations will appear first in INTERNAL_RELOCS, followed by the
2249 REL_HDR2 relocations. */
2252 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
2256 PTR external_relocs
;
2257 Elf_Internal_Rela
*internal_relocs
;
2258 boolean keep_memory
;
2260 Elf_Internal_Shdr
*rel_hdr
;
2262 Elf_Internal_Rela
*alloc2
= NULL
;
2263 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2265 if (elf_section_data (o
)->relocs
!= NULL
)
2266 return elf_section_data (o
)->relocs
;
2268 if (o
->reloc_count
== 0)
2271 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2273 if (internal_relocs
== NULL
)
2277 size
= (o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
2278 * sizeof (Elf_Internal_Rela
));
2280 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2282 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2283 if (internal_relocs
== NULL
)
2287 if (external_relocs
== NULL
)
2289 size_t size
= (size_t) rel_hdr
->sh_size
;
2291 if (elf_section_data (o
)->rel_hdr2
)
2292 size
+= (size_t) elf_section_data (o
)->rel_hdr2
->sh_size
;
2293 alloc1
= (PTR
) bfd_malloc (size
);
2296 external_relocs
= alloc1
;
2299 if (!elf_link_read_relocs_from_section (abfd
, rel_hdr
,
2303 if (!elf_link_read_relocs_from_section
2305 elf_section_data (o
)->rel_hdr2
,
2306 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2307 internal_relocs
+ (rel_hdr
->sh_size
/ rel_hdr
->sh_entsize
2308 * bed
->s
->int_rels_per_ext_rel
)))
2311 /* Cache the results for next time, if we can. */
2313 elf_section_data (o
)->relocs
= internal_relocs
;
2318 /* Don't free alloc2, since if it was allocated we are passing it
2319 back (under the name of internal_relocs). */
2321 return internal_relocs
;
2332 /* Record an assignment to a symbol made by a linker script. We need
2333 this in case some dynamic object refers to this symbol. */
2337 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
2338 bfd
*output_bfd ATTRIBUTE_UNUSED
;
2339 struct bfd_link_info
*info
;
2343 struct elf_link_hash_entry
*h
;
2345 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2348 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, true, false);
2352 if (h
->root
.type
== bfd_link_hash_new
)
2353 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
2355 /* If this symbol is being provided by the linker script, and it is
2356 currently defined by a dynamic object, but not by a regular
2357 object, then mark it as undefined so that the generic linker will
2358 force the correct value. */
2360 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2361 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2362 h
->root
.type
= bfd_link_hash_undefined
;
2364 /* If this symbol is not being provided by the linker script, and it is
2365 currently defined by a dynamic object, but not by a regular object,
2366 then clear out any version information because the symbol will not be
2367 associated with the dynamic object any more. */
2369 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2370 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2371 h
->verinfo
.verdef
= NULL
;
2373 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2375 /* When possible, keep the original type of the symbol */
2376 if (h
->type
== STT_NOTYPE
)
2377 h
->type
= STT_OBJECT
;
2379 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2380 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
2382 && h
->dynindx
== -1)
2384 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2387 /* If this is a weak defined symbol, and we know a corresponding
2388 real symbol from the same dynamic object, make sure the real
2389 symbol is also made into a dynamic symbol. */
2390 if (h
->weakdef
!= NULL
2391 && h
->weakdef
->dynindx
== -1)
2393 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2401 /* This structure is used to pass information to
2402 elf_link_assign_sym_version. */
2404 struct elf_assign_sym_version_info
2408 /* General link information. */
2409 struct bfd_link_info
*info
;
2411 struct bfd_elf_version_tree
*verdefs
;
2412 /* Whether we are exporting all dynamic symbols. */
2413 boolean export_dynamic
;
2414 /* Whether we had a failure. */
2418 /* This structure is used to pass information to
2419 elf_link_find_version_dependencies. */
2421 struct elf_find_verdep_info
2425 /* General link information. */
2426 struct bfd_link_info
*info
;
2427 /* The number of dependencies. */
2429 /* Whether we had a failure. */
2433 /* Array used to determine the number of hash table buckets to use
2434 based on the number of symbols there are. If there are fewer than
2435 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2436 fewer than 37 we use 17 buckets, and so forth. We never use more
2437 than 32771 buckets. */
2439 static const size_t elf_buckets
[] =
2441 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2445 /* Compute bucket count for hashing table. We do not use a static set
2446 of possible tables sizes anymore. Instead we determine for all
2447 possible reasonable sizes of the table the outcome (i.e., the
2448 number of collisions etc) and choose the best solution. The
2449 weighting functions are not too simple to allow the table to grow
2450 without bounds. Instead one of the weighting factors is the size.
2451 Therefore the result is always a good payoff between few collisions
2452 (= short chain lengths) and table size. */
2454 compute_bucket_count (info
)
2455 struct bfd_link_info
*info
;
2457 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2458 size_t best_size
= 0;
2459 unsigned long int *hashcodes
;
2460 unsigned long int *hashcodesp
;
2461 unsigned long int i
;
2463 /* Compute the hash values for all exported symbols. At the same
2464 time store the values in an array so that we could use them for
2466 hashcodes
= (unsigned long int *) bfd_malloc (dynsymcount
2467 * sizeof (unsigned long int));
2468 if (hashcodes
== NULL
)
2470 hashcodesp
= hashcodes
;
2472 /* Put all hash values in HASHCODES. */
2473 elf_link_hash_traverse (elf_hash_table (info
),
2474 elf_collect_hash_codes
, &hashcodesp
);
2476 /* We have a problem here. The following code to optimize the table
2477 size requires an integer type with more the 32 bits. If
2478 BFD_HOST_U_64_BIT is set we know about such a type. */
2479 #ifdef BFD_HOST_U_64_BIT
2480 if (info
->optimize
== true)
2482 unsigned long int nsyms
= hashcodesp
- hashcodes
;
2485 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
2486 unsigned long int *counts
;
2488 /* Possible optimization parameters: if we have NSYMS symbols we say
2489 that the hashing table must at least have NSYMS/4 and at most
2491 minsize
= nsyms
/ 4;
2494 best_size
= maxsize
= nsyms
* 2;
2496 /* Create array where we count the collisions in. We must use bfd_malloc
2497 since the size could be large. */
2498 counts
= (unsigned long int *) bfd_malloc (maxsize
2499 * sizeof (unsigned long int));
2506 /* Compute the "optimal" size for the hash table. The criteria is a
2507 minimal chain length. The minor criteria is (of course) the size
2509 for (i
= minsize
; i
< maxsize
; ++i
)
2511 /* Walk through the array of hashcodes and count the collisions. */
2512 BFD_HOST_U_64_BIT max
;
2513 unsigned long int j
;
2514 unsigned long int fact
;
2516 memset (counts
, '\0', i
* sizeof (unsigned long int));
2518 /* Determine how often each hash bucket is used. */
2519 for (j
= 0; j
< nsyms
; ++j
)
2520 ++counts
[hashcodes
[j
] % i
];
2522 /* For the weight function we need some information about the
2523 pagesize on the target. This is information need not be 100%
2524 accurate. Since this information is not available (so far) we
2525 define it here to a reasonable default value. If it is crucial
2526 to have a better value some day simply define this value. */
2527 # ifndef BFD_TARGET_PAGESIZE
2528 # define BFD_TARGET_PAGESIZE (4096)
2531 /* We in any case need 2 + NSYMS entries for the size values and
2533 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
2536 /* Variant 1: optimize for short chains. We add the squares
2537 of all the chain lengths (which favous many small chain
2538 over a few long chains). */
2539 for (j
= 0; j
< i
; ++j
)
2540 max
+= counts
[j
] * counts
[j
];
2542 /* This adds penalties for the overall size of the table. */
2543 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2546 /* Variant 2: Optimize a lot more for small table. Here we
2547 also add squares of the size but we also add penalties for
2548 empty slots (the +1 term). */
2549 for (j
= 0; j
< i
; ++j
)
2550 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
2552 /* The overall size of the table is considered, but not as
2553 strong as in variant 1, where it is squared. */
2554 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2558 /* Compare with current best results. */
2559 if (max
< best_chlen
)
2569 #endif /* defined (BFD_HOST_U_64_BIT) */
2571 /* This is the fallback solution if no 64bit type is available or if we
2572 are not supposed to spend much time on optimizations. We select the
2573 bucket count using a fixed set of numbers. */
2574 for (i
= 0; elf_buckets
[i
] != 0; i
++)
2576 best_size
= elf_buckets
[i
];
2577 if (dynsymcount
< elf_buckets
[i
+ 1])
2582 /* Free the arrays we needed. */
2588 /* Set up the sizes and contents of the ELF dynamic sections. This is
2589 called by the ELF linker emulation before_allocation routine. We
2590 must set the sizes of the sections before the linker sets the
2591 addresses of the various sections. */
2594 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
2595 export_dynamic
, filter_shlib
,
2596 auxiliary_filters
, info
, sinterpptr
,
2601 boolean export_dynamic
;
2602 const char *filter_shlib
;
2603 const char * const *auxiliary_filters
;
2604 struct bfd_link_info
*info
;
2605 asection
**sinterpptr
;
2606 struct bfd_elf_version_tree
*verdefs
;
2608 bfd_size_type soname_indx
;
2610 struct elf_backend_data
*bed
;
2611 struct elf_assign_sym_version_info asvinfo
;
2615 soname_indx
= (bfd_size_type
) -1;
2617 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2620 /* The backend may have to create some sections regardless of whether
2621 we're dynamic or not. */
2622 bed
= get_elf_backend_data (output_bfd
);
2623 if (bed
->elf_backend_always_size_sections
2624 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
2627 dynobj
= elf_hash_table (info
)->dynobj
;
2629 /* If there were no dynamic objects in the link, there is nothing to
2634 /* If we are supposed to export all symbols into the dynamic symbol
2635 table (this is not the normal case), then do so. */
2638 struct elf_info_failed eif
;
2642 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
2648 if (elf_hash_table (info
)->dynamic_sections_created
)
2650 struct elf_info_failed eif
;
2651 struct elf_link_hash_entry
*h
;
2652 bfd_size_type strsize
;
2654 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
2655 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
2659 soname_indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2660 soname
, true, true);
2661 if (soname_indx
== (bfd_size_type
) -1
2662 || ! elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
2668 if (! elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
2676 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, rpath
,
2678 if (indx
== (bfd_size_type
) -1
2679 || ! elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
2683 if (filter_shlib
!= NULL
)
2687 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2688 filter_shlib
, true, true);
2689 if (indx
== (bfd_size_type
) -1
2690 || ! elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
2694 if (auxiliary_filters
!= NULL
)
2696 const char * const *p
;
2698 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
2702 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2704 if (indx
== (bfd_size_type
) -1
2705 || ! elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
2710 /* Attach all the symbols to their version information. */
2711 asvinfo
.output_bfd
= output_bfd
;
2712 asvinfo
.info
= info
;
2713 asvinfo
.verdefs
= verdefs
;
2714 asvinfo
.export_dynamic
= export_dynamic
;
2715 asvinfo
.failed
= false;
2717 elf_link_hash_traverse (elf_hash_table (info
),
2718 elf_link_assign_sym_version
,
2723 /* Find all symbols which were defined in a dynamic object and make
2724 the backend pick a reasonable value for them. */
2727 elf_link_hash_traverse (elf_hash_table (info
),
2728 elf_adjust_dynamic_symbol
,
2733 /* Add some entries to the .dynamic section. We fill in some of the
2734 values later, in elf_bfd_final_link, but we must add the entries
2735 now so that we know the final size of the .dynamic section. */
2737 /* If there are initialization and/or finalization functions to
2738 call then add the corresponding DT_INIT/DT_FINI entries. */
2739 h
= (info
->init_function
2740 ? elf_link_hash_lookup (elf_hash_table (info
),
2741 info
->init_function
, false,
2745 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2746 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2748 if (! elf_add_dynamic_entry (info
, DT_INIT
, 0))
2751 h
= (info
->fini_function
2752 ? elf_link_hash_lookup (elf_hash_table (info
),
2753 info
->fini_function
, false,
2757 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2758 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2760 if (! elf_add_dynamic_entry (info
, DT_FINI
, 0))
2764 strsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
2765 if (! elf_add_dynamic_entry (info
, DT_HASH
, 0)
2766 || ! elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
2767 || ! elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
2768 || ! elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
2769 || ! elf_add_dynamic_entry (info
, DT_SYMENT
,
2770 sizeof (Elf_External_Sym
)))
2774 /* The backend must work out the sizes of all the other dynamic
2776 if (bed
->elf_backend_size_dynamic_sections
2777 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
2780 if (elf_hash_table (info
)->dynamic_sections_created
)
2784 size_t bucketcount
= 0;
2785 Elf_Internal_Sym isym
;
2786 size_t hash_entry_size
;
2788 /* Set up the version definition section. */
2789 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2790 BFD_ASSERT (s
!= NULL
);
2792 /* We may have created additional version definitions if we are
2793 just linking a regular application. */
2794 verdefs
= asvinfo
.verdefs
;
2796 if (verdefs
== NULL
)
2797 _bfd_strip_section_from_output (s
);
2802 struct bfd_elf_version_tree
*t
;
2804 Elf_Internal_Verdef def
;
2805 Elf_Internal_Verdaux defaux
;
2810 /* Make space for the base version. */
2811 size
+= sizeof (Elf_External_Verdef
);
2812 size
+= sizeof (Elf_External_Verdaux
);
2815 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2817 struct bfd_elf_version_deps
*n
;
2819 size
+= sizeof (Elf_External_Verdef
);
2820 size
+= sizeof (Elf_External_Verdaux
);
2823 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2824 size
+= sizeof (Elf_External_Verdaux
);
2827 s
->_raw_size
= size
;
2828 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2829 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2832 /* Fill in the version definition section. */
2836 def
.vd_version
= VER_DEF_CURRENT
;
2837 def
.vd_flags
= VER_FLG_BASE
;
2840 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2841 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2842 + sizeof (Elf_External_Verdaux
));
2844 if (soname_indx
!= (bfd_size_type
) -1)
2846 def
.vd_hash
= bfd_elf_hash (soname
);
2847 defaux
.vda_name
= soname_indx
;
2854 name
= output_bfd
->filename
;
2855 def
.vd_hash
= bfd_elf_hash (name
);
2856 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2858 if (indx
== (bfd_size_type
) -1)
2860 defaux
.vda_name
= indx
;
2862 defaux
.vda_next
= 0;
2864 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2865 (Elf_External_Verdef
*)p
);
2866 p
+= sizeof (Elf_External_Verdef
);
2867 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2868 (Elf_External_Verdaux
*) p
);
2869 p
+= sizeof (Elf_External_Verdaux
);
2871 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2874 struct bfd_elf_version_deps
*n
;
2875 struct elf_link_hash_entry
*h
;
2878 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2881 /* Add a symbol representing this version. */
2883 if (! (_bfd_generic_link_add_one_symbol
2884 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
2885 (bfd_vma
) 0, (const char *) NULL
, false,
2886 get_elf_backend_data (dynobj
)->collect
,
2887 (struct bfd_link_hash_entry
**) &h
)))
2889 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
2890 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2891 h
->type
= STT_OBJECT
;
2892 h
->verinfo
.vertree
= t
;
2894 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2897 def
.vd_version
= VER_DEF_CURRENT
;
2899 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
2900 def
.vd_flags
|= VER_FLG_WEAK
;
2901 def
.vd_ndx
= t
->vernum
+ 1;
2902 def
.vd_cnt
= cdeps
+ 1;
2903 def
.vd_hash
= bfd_elf_hash (t
->name
);
2904 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2905 if (t
->next
!= NULL
)
2906 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2907 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
2911 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2912 (Elf_External_Verdef
*) p
);
2913 p
+= sizeof (Elf_External_Verdef
);
2915 defaux
.vda_name
= h
->dynstr_index
;
2916 if (t
->deps
== NULL
)
2917 defaux
.vda_next
= 0;
2919 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2920 t
->name_indx
= defaux
.vda_name
;
2922 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2923 (Elf_External_Verdaux
*) p
);
2924 p
+= sizeof (Elf_External_Verdaux
);
2926 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2928 if (n
->version_needed
== NULL
)
2930 /* This can happen if there was an error in the
2932 defaux
.vda_name
= 0;
2935 defaux
.vda_name
= n
->version_needed
->name_indx
;
2936 if (n
->next
== NULL
)
2937 defaux
.vda_next
= 0;
2939 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2941 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2942 (Elf_External_Verdaux
*) p
);
2943 p
+= sizeof (Elf_External_Verdaux
);
2947 if (! elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
2948 || ! elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
2951 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
2954 /* Work out the size of the version reference section. */
2956 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2957 BFD_ASSERT (s
!= NULL
);
2959 struct elf_find_verdep_info sinfo
;
2961 sinfo
.output_bfd
= output_bfd
;
2963 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
2964 if (sinfo
.vers
== 0)
2966 sinfo
.failed
= false;
2968 elf_link_hash_traverse (elf_hash_table (info
),
2969 elf_link_find_version_dependencies
,
2972 if (elf_tdata (output_bfd
)->verref
== NULL
)
2973 _bfd_strip_section_from_output (s
);
2976 Elf_Internal_Verneed
*t
;
2981 /* Build the version definition section. */
2984 for (t
= elf_tdata (output_bfd
)->verref
;
2988 Elf_Internal_Vernaux
*a
;
2990 size
+= sizeof (Elf_External_Verneed
);
2992 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2993 size
+= sizeof (Elf_External_Vernaux
);
2996 s
->_raw_size
= size
;
2997 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, size
);
2998 if (s
->contents
== NULL
)
3002 for (t
= elf_tdata (output_bfd
)->verref
;
3007 Elf_Internal_Vernaux
*a
;
3011 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3014 t
->vn_version
= VER_NEED_CURRENT
;
3016 if (elf_dt_name (t
->vn_bfd
) != NULL
)
3017 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3018 elf_dt_name (t
->vn_bfd
),
3021 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3022 t
->vn_bfd
->filename
, true, false);
3023 if (indx
== (bfd_size_type
) -1)
3026 t
->vn_aux
= sizeof (Elf_External_Verneed
);
3027 if (t
->vn_nextref
== NULL
)
3030 t
->vn_next
= (sizeof (Elf_External_Verneed
)
3031 + caux
* sizeof (Elf_External_Vernaux
));
3033 _bfd_elf_swap_verneed_out (output_bfd
, t
,
3034 (Elf_External_Verneed
*) p
);
3035 p
+= sizeof (Elf_External_Verneed
);
3037 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3039 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
3040 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3041 a
->vna_nodename
, true, false);
3042 if (indx
== (bfd_size_type
) -1)
3045 if (a
->vna_nextptr
== NULL
)
3048 a
->vna_next
= sizeof (Elf_External_Vernaux
);
3050 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
3051 (Elf_External_Vernaux
*) p
);
3052 p
+= sizeof (Elf_External_Vernaux
);
3056 if (! elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
3057 || ! elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
3060 elf_tdata (output_bfd
)->cverrefs
= crefs
;
3064 /* Assign dynsym indicies. In a shared library we generate a
3065 section symbol for each output section, which come first.
3066 Next come all of the back-end allocated local dynamic syms,
3067 followed by the rest of the global symbols. */
3069 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3071 /* Work out the size of the symbol version section. */
3072 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3073 BFD_ASSERT (s
!= NULL
);
3074 if (dynsymcount
== 0
3075 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
3077 _bfd_strip_section_from_output (s
);
3078 /* The DYNSYMCOUNT might have changed if we were going to
3079 output a dynamic symbol table entry for S. */
3080 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3084 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
3085 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3086 if (s
->contents
== NULL
)
3089 if (! elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
3093 /* Set the size of the .dynsym and .hash sections. We counted
3094 the number of dynamic symbols in elf_link_add_object_symbols.
3095 We will build the contents of .dynsym and .hash when we build
3096 the final symbol table, because until then we do not know the
3097 correct value to give the symbols. We built the .dynstr
3098 section as we went along in elf_link_add_object_symbols. */
3099 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
3100 BFD_ASSERT (s
!= NULL
);
3101 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
3102 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3103 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3106 /* The first entry in .dynsym is a dummy symbol. */
3113 elf_swap_symbol_out (output_bfd
, &isym
,
3114 (PTR
) (Elf_External_Sym
*) s
->contents
);
3116 /* Compute the size of the hashing table. As a side effect this
3117 computes the hash values for all the names we export. */
3118 bucketcount
= compute_bucket_count (info
);
3120 s
= bfd_get_section_by_name (dynobj
, ".hash");
3121 BFD_ASSERT (s
!= NULL
);
3122 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
3123 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
3124 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3125 if (s
->contents
== NULL
)
3127 memset (s
->contents
, 0, (size_t) s
->_raw_size
);
3129 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
3130 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
3131 s
->contents
+ hash_entry_size
);
3133 elf_hash_table (info
)->bucketcount
= bucketcount
;
3135 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
3136 BFD_ASSERT (s
!= NULL
);
3137 s
->_raw_size
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
3139 if (! elf_add_dynamic_entry (info
, DT_NULL
, 0))
3146 /* Fix up the flags for a symbol. This handles various cases which
3147 can only be fixed after all the input files are seen. This is
3148 currently called by both adjust_dynamic_symbol and
3149 assign_sym_version, which is unnecessary but perhaps more robust in
3150 the face of future changes. */
3153 elf_fix_symbol_flags (h
, eif
)
3154 struct elf_link_hash_entry
*h
;
3155 struct elf_info_failed
*eif
;
3157 /* If this symbol was mentioned in a non-ELF file, try to set
3158 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3159 permit a non-ELF file to correctly refer to a symbol defined in
3160 an ELF dynamic object. */
3161 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
3163 if (h
->root
.type
!= bfd_link_hash_defined
3164 && h
->root
.type
!= bfd_link_hash_defweak
)
3165 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3166 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3169 if (h
->root
.u
.def
.section
->owner
!= NULL
3170 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3171 == bfd_target_elf_flavour
))
3172 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3173 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3175 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3178 if (h
->dynindx
== -1
3179 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
3180 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
3182 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3191 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3192 was first seen in a non-ELF file. Fortunately, if the symbol
3193 was first seen in an ELF file, we're probably OK unless the
3194 symbol was defined in a non-ELF file. Catch that case here.
3195 FIXME: We're still in trouble if the symbol was first seen in
3196 a dynamic object, and then later in a non-ELF regular object. */
3197 if ((h
->root
.type
== bfd_link_hash_defined
3198 || h
->root
.type
== bfd_link_hash_defweak
)
3199 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3200 && (h
->root
.u
.def
.section
->owner
!= NULL
3201 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3202 != bfd_target_elf_flavour
)
3203 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3204 && (h
->elf_link_hash_flags
3205 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
3206 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3209 /* If this is a final link, and the symbol was defined as a common
3210 symbol in a regular object file, and there was no definition in
3211 any dynamic object, then the linker will have allocated space for
3212 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3213 flag will not have been set. */
3214 if (h
->root
.type
== bfd_link_hash_defined
3215 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3216 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
3217 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3218 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3219 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3221 /* If -Bsymbolic was used (which means to bind references to global
3222 symbols to the definition within the shared object), and this
3223 symbol was defined in a regular object, then it actually doesn't
3224 need a PLT entry. */
3225 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
3226 && eif
->info
->shared
3227 && eif
->info
->symbolic
3228 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3230 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
3231 h
->plt
.offset
= (bfd_vma
) -1;
3237 /* Make the backend pick a good value for a dynamic symbol. This is
3238 called via elf_link_hash_traverse, and also calls itself
3242 elf_adjust_dynamic_symbol (h
, data
)
3243 struct elf_link_hash_entry
*h
;
3246 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3248 struct elf_backend_data
*bed
;
3250 /* Ignore indirect symbols. These are added by the versioning code. */
3251 if (h
->root
.type
== bfd_link_hash_indirect
)
3254 /* Fix the symbol flags. */
3255 if (! elf_fix_symbol_flags (h
, eif
))
3258 /* If this symbol does not require a PLT entry, and it is not
3259 defined by a dynamic object, or is not referenced by a regular
3260 object, ignore it. We do have to handle a weak defined symbol,
3261 even if no regular object refers to it, if we decided to add it
3262 to the dynamic symbol table. FIXME: Do we normally need to worry
3263 about symbols which are defined by one dynamic object and
3264 referenced by another one? */
3265 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
3266 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
3267 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3268 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
3269 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
3271 h
->plt
.offset
= (bfd_vma
) -1;
3275 /* If we've already adjusted this symbol, don't do it again. This
3276 can happen via a recursive call. */
3277 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
3280 /* Don't look at this symbol again. Note that we must set this
3281 after checking the above conditions, because we may look at a
3282 symbol once, decide not to do anything, and then get called
3283 recursively later after REF_REGULAR is set below. */
3284 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
3286 /* If this is a weak definition, and we know a real definition, and
3287 the real symbol is not itself defined by a regular object file,
3288 then get a good value for the real definition. We handle the
3289 real symbol first, for the convenience of the backend routine.
3291 Note that there is a confusing case here. If the real definition
3292 is defined by a regular object file, we don't get the real symbol
3293 from the dynamic object, but we do get the weak symbol. If the
3294 processor backend uses a COPY reloc, then if some routine in the
3295 dynamic object changes the real symbol, we will not see that
3296 change in the corresponding weak symbol. This is the way other
3297 ELF linkers work as well, and seems to be a result of the shared
3300 I will clarify this issue. Most SVR4 shared libraries define the
3301 variable _timezone and define timezone as a weak synonym. The
3302 tzset call changes _timezone. If you write
3303 extern int timezone;
3305 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3306 you might expect that, since timezone is a synonym for _timezone,
3307 the same number will print both times. However, if the processor
3308 backend uses a COPY reloc, then actually timezone will be copied
3309 into your process image, and, since you define _timezone
3310 yourself, _timezone will not. Thus timezone and _timezone will
3311 wind up at different memory locations. The tzset call will set
3312 _timezone, leaving timezone unchanged. */
3314 if (h
->weakdef
!= NULL
)
3316 struct elf_link_hash_entry
*weakdef
;
3318 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3319 || h
->root
.type
== bfd_link_hash_defweak
);
3320 weakdef
= h
->weakdef
;
3321 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
3322 || weakdef
->root
.type
== bfd_link_hash_defweak
);
3323 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
3324 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3326 /* This symbol is defined by a regular object file, so we
3327 will not do anything special. Clear weakdef for the
3328 convenience of the processor backend. */
3333 /* There is an implicit reference by a regular object file
3334 via the weak symbol. */
3335 weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
3336 if (h
->weakdef
->elf_link_hash_flags
3337 & ELF_LINK_HASH_REF_REGULAR_NONWEAK
)
3338 weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
3339 if (! elf_adjust_dynamic_symbol (weakdef
, (PTR
) eif
))
3344 /* If a symbol has no type and no size and does not require a PLT
3345 entry, then we are probably about to do the wrong thing here: we
3346 are probably going to create a COPY reloc for an empty object.
3347 This case can arise when a shared object is built with assembly
3348 code, and the assembly code fails to set the symbol type. */
3350 && h
->type
== STT_NOTYPE
3351 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
3352 (*_bfd_error_handler
)
3353 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3354 h
->root
.root
.string
);
3356 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
3357 bed
= get_elf_backend_data (dynobj
);
3358 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3367 /* This routine is used to export all defined symbols into the dynamic
3368 symbol table. It is called via elf_link_hash_traverse. */
3371 elf_export_symbol (h
, data
)
3372 struct elf_link_hash_entry
*h
;
3375 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3377 /* Ignore indirect symbols. These are added by the versioning code. */
3378 if (h
->root
.type
== bfd_link_hash_indirect
)
3381 if (h
->dynindx
== -1
3382 && (h
->elf_link_hash_flags
3383 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
3385 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3395 /* Look through the symbols which are defined in other shared
3396 libraries and referenced here. Update the list of version
3397 dependencies. This will be put into the .gnu.version_r section.
3398 This function is called via elf_link_hash_traverse. */
3401 elf_link_find_version_dependencies (h
, data
)
3402 struct elf_link_hash_entry
*h
;
3405 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
3406 Elf_Internal_Verneed
*t
;
3407 Elf_Internal_Vernaux
*a
;
3409 /* We only care about symbols defined in shared objects with version
3411 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3412 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
3414 || h
->verinfo
.verdef
== NULL
)
3417 /* See if we already know about this version. */
3418 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
3420 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
3423 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3424 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
3430 /* This is a new version. Add it to tree we are building. */
3434 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *t
);
3437 rinfo
->failed
= true;
3441 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
3442 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
3443 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
3446 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *a
);
3448 /* Note that we are copying a string pointer here, and testing it
3449 above. If bfd_elf_string_from_elf_section is ever changed to
3450 discard the string data when low in memory, this will have to be
3452 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
3454 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
3455 a
->vna_nextptr
= t
->vn_auxptr
;
3457 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
3460 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
3467 /* Figure out appropriate versions for all the symbols. We may not
3468 have the version number script until we have read all of the input
3469 files, so until that point we don't know which symbols should be
3470 local. This function is called via elf_link_hash_traverse. */
3473 elf_link_assign_sym_version (h
, data
)
3474 struct elf_link_hash_entry
*h
;
3477 struct elf_assign_sym_version_info
*sinfo
=
3478 (struct elf_assign_sym_version_info
*) data
;
3479 struct bfd_link_info
*info
= sinfo
->info
;
3480 struct elf_info_failed eif
;
3483 /* Fix the symbol flags. */
3486 if (! elf_fix_symbol_flags (h
, &eif
))
3489 sinfo
->failed
= true;
3493 /* We only need version numbers for symbols defined in regular
3495 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
3498 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3499 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
3501 struct bfd_elf_version_tree
*t
;
3506 /* There are two consecutive ELF_VER_CHR characters if this is
3507 not a hidden symbol. */
3509 if (*p
== ELF_VER_CHR
)
3515 /* If there is no version string, we can just return out. */
3519 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
3523 /* Look for the version. If we find it, it is no longer weak. */
3524 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
3526 if (strcmp (t
->name
, p
) == 0)
3530 struct bfd_elf_version_expr
*d
;
3532 len
= p
- h
->root
.root
.string
;
3533 alc
= bfd_alloc (sinfo
->output_bfd
, len
);
3536 strncpy (alc
, h
->root
.root
.string
, len
- 1);
3537 alc
[len
- 1] = '\0';
3538 if (alc
[len
- 2] == ELF_VER_CHR
)
3539 alc
[len
- 2] = '\0';
3541 h
->verinfo
.vertree
= t
;
3545 if (t
->globals
!= NULL
)
3547 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3548 if ((*d
->match
) (d
, alc
))
3552 /* See if there is anything to force this symbol to
3554 if (d
== NULL
&& t
->locals
!= NULL
)
3556 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3558 if ((*d
->match
) (d
, alc
))
3560 if (h
->dynindx
!= -1
3562 && ! sinfo
->export_dynamic
)
3564 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3565 h
->elf_link_hash_flags
&=~
3566 ELF_LINK_HASH_NEEDS_PLT
;
3568 h
->plt
.offset
= (bfd_vma
) -1;
3569 /* FIXME: The name of the symbol has
3570 already been recorded in the dynamic
3571 string table section. */
3579 bfd_release (sinfo
->output_bfd
, alc
);
3584 /* If we are building an application, we need to create a
3585 version node for this version. */
3586 if (t
== NULL
&& ! info
->shared
)
3588 struct bfd_elf_version_tree
**pp
;
3591 /* If we aren't going to export this symbol, we don't need
3592 to worry about it. */
3593 if (h
->dynindx
== -1)
3596 t
= ((struct bfd_elf_version_tree
*)
3597 bfd_alloc (sinfo
->output_bfd
, sizeof *t
));
3600 sinfo
->failed
= true;
3609 t
->name_indx
= (unsigned int) -1;
3613 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
3615 t
->vernum
= version_index
;
3619 h
->verinfo
.vertree
= t
;
3623 /* We could not find the version for a symbol when
3624 generating a shared archive. Return an error. */
3625 (*_bfd_error_handler
)
3626 (_("%s: undefined versioned symbol name %s"),
3627 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
3628 bfd_set_error (bfd_error_bad_value
);
3629 sinfo
->failed
= true;
3634 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
3637 /* If we don't have a version for this symbol, see if we can find
3639 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
3641 struct bfd_elf_version_tree
*t
;
3642 struct bfd_elf_version_tree
*deflt
;
3643 struct bfd_elf_version_expr
*d
;
3645 /* See if can find what version this symbol is in. If the
3646 symbol is supposed to be local, then don't actually register
3649 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
3651 if (t
->globals
!= NULL
)
3653 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3655 if ((*d
->match
) (d
, h
->root
.root
.string
))
3657 h
->verinfo
.vertree
= t
;
3666 if (t
->locals
!= NULL
)
3668 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3670 if (d
->pattern
[0] == '*' && d
->pattern
[1] == '\0')
3672 else if ((*d
->match
) (d
, h
->root
.root
.string
))
3674 h
->verinfo
.vertree
= t
;
3675 if (h
->dynindx
!= -1
3677 && ! sinfo
->export_dynamic
)
3679 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3680 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
3682 h
->plt
.offset
= (bfd_vma
) -1;
3683 /* FIXME: The name of the symbol has already
3684 been recorded in the dynamic string table
3696 if (deflt
!= NULL
&& h
->verinfo
.vertree
== NULL
)
3698 h
->verinfo
.vertree
= deflt
;
3699 if (h
->dynindx
!= -1
3701 && ! sinfo
->export_dynamic
)
3703 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3704 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
3706 h
->plt
.offset
= (bfd_vma
) -1;
3707 /* FIXME: The name of the symbol has already been
3708 recorded in the dynamic string table section. */
3716 /* Final phase of ELF linker. */
3718 /* A structure we use to avoid passing large numbers of arguments. */
3720 struct elf_final_link_info
3722 /* General link information. */
3723 struct bfd_link_info
*info
;
3726 /* Symbol string table. */
3727 struct bfd_strtab_hash
*symstrtab
;
3728 /* .dynsym section. */
3729 asection
*dynsym_sec
;
3730 /* .hash section. */
3732 /* symbol version section (.gnu.version). */
3733 asection
*symver_sec
;
3734 /* Buffer large enough to hold contents of any section. */
3736 /* Buffer large enough to hold external relocs of any section. */
3737 PTR external_relocs
;
3738 /* Buffer large enough to hold internal relocs of any section. */
3739 Elf_Internal_Rela
*internal_relocs
;
3740 /* Buffer large enough to hold external local symbols of any input
3742 Elf_External_Sym
*external_syms
;
3743 /* Buffer large enough to hold internal local symbols of any input
3745 Elf_Internal_Sym
*internal_syms
;
3746 /* Array large enough to hold a symbol index for each local symbol
3747 of any input BFD. */
3749 /* Array large enough to hold a section pointer for each local
3750 symbol of any input BFD. */
3751 asection
**sections
;
3752 /* Buffer to hold swapped out symbols. */
3753 Elf_External_Sym
*symbuf
;
3754 /* Number of swapped out symbols in buffer. */
3755 size_t symbuf_count
;
3756 /* Number of symbols which fit in symbuf. */
3760 static boolean elf_link_output_sym
3761 PARAMS ((struct elf_final_link_info
*, const char *,
3762 Elf_Internal_Sym
*, asection
*));
3763 static boolean elf_link_flush_output_syms
3764 PARAMS ((struct elf_final_link_info
*));
3765 static boolean elf_link_output_extsym
3766 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3767 static boolean elf_link_input_bfd
3768 PARAMS ((struct elf_final_link_info
*, bfd
*));
3769 static boolean elf_reloc_link_order
3770 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
3771 struct bfd_link_order
*));
3773 /* This struct is used to pass information to elf_link_output_extsym. */
3775 struct elf_outext_info
3779 struct elf_final_link_info
*finfo
;
3782 /* Compute the size of, and allocate space for, REL_HDR which is the
3783 section header for a section containing relocations for O. */
3786 elf_link_size_reloc_section (abfd
, rel_hdr
, o
)
3788 Elf_Internal_Shdr
*rel_hdr
;
3791 register struct elf_link_hash_entry
**p
, **pend
;
3792 unsigned reloc_count
;
3794 /* Figure out how many relocations there will be. */
3795 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
3796 reloc_count
= elf_section_data (o
)->rel_count
;
3798 reloc_count
= elf_section_data (o
)->rel_count2
;
3800 /* That allows us to calculate the size of the section. */
3801 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
3803 /* The contents field must last into write_object_contents, so we
3804 allocate it with bfd_alloc rather than malloc. */
3805 rel_hdr
->contents
= (PTR
) bfd_alloc (abfd
, rel_hdr
->sh_size
);
3806 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
3809 /* We only allocate one set of hash entries, so we only do it the
3810 first time we are called. */
3811 if (elf_section_data (o
)->rel_hashes
== NULL
)
3813 p
= ((struct elf_link_hash_entry
**)
3814 bfd_malloc (o
->reloc_count
3815 * sizeof (struct elf_link_hash_entry
*)));
3816 if (p
== NULL
&& o
->reloc_count
!= 0)
3819 elf_section_data (o
)->rel_hashes
= p
;
3820 pend
= p
+ o
->reloc_count
;
3821 for (; p
< pend
; p
++)
3828 /* When performing a relocateable link, the input relocations are
3829 preserved. But, if they reference global symbols, the indices
3830 referenced must be updated. Update all the relocations in
3831 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
3834 elf_link_adjust_relocs (abfd
, rel_hdr
, count
, rel_hash
)
3836 Elf_Internal_Shdr
*rel_hdr
;
3838 struct elf_link_hash_entry
**rel_hash
;
3842 for (i
= 0; i
< count
; i
++, rel_hash
++)
3844 if (*rel_hash
== NULL
)
3847 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
3849 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
3851 Elf_External_Rel
*erel
;
3852 Elf_Internal_Rel irel
;
3854 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
3855 elf_swap_reloc_in (abfd
, erel
, &irel
);
3856 irel
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
3857 ELF_R_TYPE (irel
.r_info
));
3858 elf_swap_reloc_out (abfd
, &irel
, erel
);
3862 Elf_External_Rela
*erela
;
3863 Elf_Internal_Rela irela
;
3865 BFD_ASSERT (rel_hdr
->sh_entsize
3866 == sizeof (Elf_External_Rela
));
3868 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
3869 elf_swap_reloca_in (abfd
, erela
, &irela
);
3870 irela
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
3871 ELF_R_TYPE (irela
.r_info
));
3872 elf_swap_reloca_out (abfd
, &irela
, erela
);
3877 /* Do the final step of an ELF link. */
3880 elf_bfd_final_link (abfd
, info
)
3882 struct bfd_link_info
*info
;
3886 struct elf_final_link_info finfo
;
3887 register asection
*o
;
3888 register struct bfd_link_order
*p
;
3890 size_t max_contents_size
;
3891 size_t max_external_reloc_size
;
3892 size_t max_internal_reloc_count
;
3893 size_t max_sym_count
;
3895 Elf_Internal_Sym elfsym
;
3897 Elf_Internal_Shdr
*symtab_hdr
;
3898 Elf_Internal_Shdr
*symstrtab_hdr
;
3899 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3900 struct elf_outext_info eoinfo
;
3903 abfd
->flags
|= DYNAMIC
;
3905 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
3906 dynobj
= elf_hash_table (info
)->dynobj
;
3909 finfo
.output_bfd
= abfd
;
3910 finfo
.symstrtab
= elf_stringtab_init ();
3911 if (finfo
.symstrtab
== NULL
)
3916 finfo
.dynsym_sec
= NULL
;
3917 finfo
.hash_sec
= NULL
;
3918 finfo
.symver_sec
= NULL
;
3922 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
3923 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
3924 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
3925 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3926 /* Note that it is OK if symver_sec is NULL. */
3929 finfo
.contents
= NULL
;
3930 finfo
.external_relocs
= NULL
;
3931 finfo
.internal_relocs
= NULL
;
3932 finfo
.external_syms
= NULL
;
3933 finfo
.internal_syms
= NULL
;
3934 finfo
.indices
= NULL
;
3935 finfo
.sections
= NULL
;
3936 finfo
.symbuf
= NULL
;
3937 finfo
.symbuf_count
= 0;
3939 /* Count up the number of relocations we will output for each output
3940 section, so that we know the sizes of the reloc sections. We
3941 also figure out some maximum sizes. */
3942 max_contents_size
= 0;
3943 max_external_reloc_size
= 0;
3944 max_internal_reloc_count
= 0;
3946 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3950 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3952 if (p
->type
== bfd_section_reloc_link_order
3953 || p
->type
== bfd_symbol_reloc_link_order
)
3955 else if (p
->type
== bfd_indirect_link_order
)
3959 sec
= p
->u
.indirect
.section
;
3961 /* Mark all sections which are to be included in the
3962 link. This will normally be every section. We need
3963 to do this so that we can identify any sections which
3964 the linker has decided to not include. */
3965 sec
->linker_mark
= true;
3967 if (info
->relocateable
)
3968 o
->reloc_count
+= sec
->reloc_count
;
3970 if (sec
->_raw_size
> max_contents_size
)
3971 max_contents_size
= sec
->_raw_size
;
3972 if (sec
->_cooked_size
> max_contents_size
)
3973 max_contents_size
= sec
->_cooked_size
;
3975 /* We are interested in just local symbols, not all
3977 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
3978 && (sec
->owner
->flags
& DYNAMIC
) == 0)
3982 if (elf_bad_symtab (sec
->owner
))
3983 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
3984 / sizeof (Elf_External_Sym
));
3986 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
3988 if (sym_count
> max_sym_count
)
3989 max_sym_count
= sym_count
;
3991 if ((sec
->flags
& SEC_RELOC
) != 0)
3995 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
3996 if (ext_size
> max_external_reloc_size
)
3997 max_external_reloc_size
= ext_size
;
3998 if (sec
->reloc_count
> max_internal_reloc_count
)
3999 max_internal_reloc_count
= sec
->reloc_count
;
4005 if (o
->reloc_count
> 0)
4006 o
->flags
|= SEC_RELOC
;
4009 /* Explicitly clear the SEC_RELOC flag. The linker tends to
4010 set it (this is probably a bug) and if it is set
4011 assign_section_numbers will create a reloc section. */
4012 o
->flags
&=~ SEC_RELOC
;
4015 /* If the SEC_ALLOC flag is not set, force the section VMA to
4016 zero. This is done in elf_fake_sections as well, but forcing
4017 the VMA to 0 here will ensure that relocs against these
4018 sections are handled correctly. */
4019 if ((o
->flags
& SEC_ALLOC
) == 0
4020 && ! o
->user_set_vma
)
4024 /* Figure out the file positions for everything but the symbol table
4025 and the relocs. We set symcount to force assign_section_numbers
4026 to create a symbol table. */
4027 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
4028 BFD_ASSERT (! abfd
->output_has_begun
);
4029 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
4032 /* Figure out how many relocations we will have in each section.
4033 Just using RELOC_COUNT isn't good enough since that doesn't
4034 maintain a separate value for REL vs. RELA relocations. */
4035 if (info
->relocateable
)
4036 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
4037 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
4039 asection
* output_section
= o
->output_section
;
4041 if (output_section
&& (o
->flags
& SEC_RELOC
) != 0)
4043 struct bfd_elf_section_data
*esdi
4044 = elf_section_data (o
);
4045 struct bfd_elf_section_data
*esdo
4046 = elf_section_data (output_section
);
4047 unsigned int *rel_count
;
4048 unsigned int *rel_count2
;
4050 /* We must be careful to add the relocation froms the
4051 input section to the right output count. */
4052 if (esdi
->rel_hdr
.sh_entsize
== esdo
->rel_hdr
.sh_entsize
)
4054 rel_count
= &esdo
->rel_count
;
4055 rel_count2
= &esdo
->rel_count2
;
4059 rel_count
= &esdo
->rel_count2
;
4060 rel_count2
= &esdo
->rel_count
;
4063 *rel_count
+= (esdi
->rel_hdr
.sh_size
4064 / esdi
->rel_hdr
.sh_entsize
);
4066 *rel_count2
+= (esdi
->rel_hdr2
->sh_size
4067 / esdi
->rel_hdr2
->sh_entsize
);
4071 /* That created the reloc sections. Set their sizes, and assign
4072 them file positions, and allocate some buffers. */
4073 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4075 if ((o
->flags
& SEC_RELOC
) != 0)
4077 if (!elf_link_size_reloc_section (abfd
,
4078 &elf_section_data (o
)->rel_hdr
,
4082 if (elf_section_data (o
)->rel_hdr2
4083 && !elf_link_size_reloc_section (abfd
,
4084 elf_section_data (o
)->rel_hdr2
,
4089 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
4090 to count upwards while actually outputting the relocations. */
4091 elf_section_data (o
)->rel_count
= 0;
4092 elf_section_data (o
)->rel_count2
= 0;
4095 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4097 /* We have now assigned file positions for all the sections except
4098 .symtab and .strtab. We start the .symtab section at the current
4099 file position, and write directly to it. We build the .strtab
4100 section in memory. */
4101 bfd_get_symcount (abfd
) = 0;
4102 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4103 /* sh_name is set in prep_headers. */
4104 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4105 symtab_hdr
->sh_flags
= 0;
4106 symtab_hdr
->sh_addr
= 0;
4107 symtab_hdr
->sh_size
= 0;
4108 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
4109 /* sh_link is set in assign_section_numbers. */
4110 /* sh_info is set below. */
4111 /* sh_offset is set just below. */
4112 symtab_hdr
->sh_addralign
= 4; /* FIXME: system dependent? */
4114 off
= elf_tdata (abfd
)->next_file_pos
;
4115 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
4117 /* Note that at this point elf_tdata (abfd)->next_file_pos is
4118 incorrect. We do not yet know the size of the .symtab section.
4119 We correct next_file_pos below, after we do know the size. */
4121 /* Allocate a buffer to hold swapped out symbols. This is to avoid
4122 continuously seeking to the right position in the file. */
4123 if (! info
->keep_memory
|| max_sym_count
< 20)
4124 finfo
.symbuf_size
= 20;
4126 finfo
.symbuf_size
= max_sym_count
;
4127 finfo
.symbuf
= ((Elf_External_Sym
*)
4128 bfd_malloc (finfo
.symbuf_size
* sizeof (Elf_External_Sym
)));
4129 if (finfo
.symbuf
== NULL
)
4132 /* Start writing out the symbol table. The first symbol is always a
4134 if (info
->strip
!= strip_all
|| info
->relocateable
)
4136 elfsym
.st_value
= 0;
4139 elfsym
.st_other
= 0;
4140 elfsym
.st_shndx
= SHN_UNDEF
;
4141 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
4142 &elfsym
, bfd_und_section_ptr
))
4147 /* Some standard ELF linkers do this, but we don't because it causes
4148 bootstrap comparison failures. */
4149 /* Output a file symbol for the output file as the second symbol.
4150 We output this even if we are discarding local symbols, although
4151 I'm not sure if this is correct. */
4152 elfsym
.st_value
= 0;
4154 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4155 elfsym
.st_other
= 0;
4156 elfsym
.st_shndx
= SHN_ABS
;
4157 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
4158 &elfsym
, bfd_abs_section_ptr
))
4162 /* Output a symbol for each section. We output these even if we are
4163 discarding local symbols, since they are used for relocs. These
4164 symbols have no names. We store the index of each one in the
4165 index field of the section, so that we can find it again when
4166 outputting relocs. */
4167 if (info
->strip
!= strip_all
|| info
->relocateable
)
4170 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4171 elfsym
.st_other
= 0;
4172 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
4174 o
= section_from_elf_index (abfd
, i
);
4176 o
->target_index
= bfd_get_symcount (abfd
);
4177 elfsym
.st_shndx
= i
;
4178 if (info
->relocateable
|| o
== NULL
)
4179 elfsym
.st_value
= 0;
4181 elfsym
.st_value
= o
->vma
;
4182 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
4188 /* Allocate some memory to hold information read in from the input
4190 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
4191 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
4192 finfo
.internal_relocs
= ((Elf_Internal_Rela
*)
4193 bfd_malloc (max_internal_reloc_count
4194 * sizeof (Elf_Internal_Rela
)
4195 * bed
->s
->int_rels_per_ext_rel
));
4196 finfo
.external_syms
= ((Elf_External_Sym
*)
4197 bfd_malloc (max_sym_count
4198 * sizeof (Elf_External_Sym
)));
4199 finfo
.internal_syms
= ((Elf_Internal_Sym
*)
4200 bfd_malloc (max_sym_count
4201 * sizeof (Elf_Internal_Sym
)));
4202 finfo
.indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
4203 finfo
.sections
= ((asection
**)
4204 bfd_malloc (max_sym_count
* sizeof (asection
*)));
4205 if ((finfo
.contents
== NULL
&& max_contents_size
!= 0)
4206 || (finfo
.external_relocs
== NULL
&& max_external_reloc_size
!= 0)
4207 || (finfo
.internal_relocs
== NULL
&& max_internal_reloc_count
!= 0)
4208 || (finfo
.external_syms
== NULL
&& max_sym_count
!= 0)
4209 || (finfo
.internal_syms
== NULL
&& max_sym_count
!= 0)
4210 || (finfo
.indices
== NULL
&& max_sym_count
!= 0)
4211 || (finfo
.sections
== NULL
&& max_sym_count
!= 0))
4214 /* Since ELF permits relocations to be against local symbols, we
4215 must have the local symbols available when we do the relocations.
4216 Since we would rather only read the local symbols once, and we
4217 would rather not keep them in memory, we handle all the
4218 relocations for a single input file at the same time.
4220 Unfortunately, there is no way to know the total number of local
4221 symbols until we have seen all of them, and the local symbol
4222 indices precede the global symbol indices. This means that when
4223 we are generating relocateable output, and we see a reloc against
4224 a global symbol, we can not know the symbol index until we have
4225 finished examining all the local symbols to see which ones we are
4226 going to output. To deal with this, we keep the relocations in
4227 memory, and don't output them until the end of the link. This is
4228 an unfortunate waste of memory, but I don't see a good way around
4229 it. Fortunately, it only happens when performing a relocateable
4230 link, which is not the common case. FIXME: If keep_memory is set
4231 we could write the relocs out and then read them again; I don't
4232 know how bad the memory loss will be. */
4234 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
4235 sub
->output_has_begun
= false;
4236 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4238 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
4240 if (p
->type
== bfd_indirect_link_order
4241 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
4242 == bfd_target_elf_flavour
))
4244 sub
= p
->u
.indirect
.section
->owner
;
4245 if (! sub
->output_has_begun
)
4247 if (! elf_link_input_bfd (&finfo
, sub
))
4249 sub
->output_has_begun
= true;
4252 else if (p
->type
== bfd_section_reloc_link_order
4253 || p
->type
== bfd_symbol_reloc_link_order
)
4255 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
4260 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
4266 /* That wrote out all the local symbols. Finish up the symbol table
4267 with the global symbols. */
4269 if (info
->strip
!= strip_all
&& info
->shared
)
4271 /* Output any global symbols that got converted to local in a
4272 version script. We do this in a separate step since ELF
4273 requires all local symbols to appear prior to any global
4274 symbols. FIXME: We should only do this if some global
4275 symbols were, in fact, converted to become local. FIXME:
4276 Will this work correctly with the Irix 5 linker? */
4277 eoinfo
.failed
= false;
4278 eoinfo
.finfo
= &finfo
;
4279 eoinfo
.localsyms
= true;
4280 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
4286 /* The sh_info field records the index of the first non local symbol. */
4287 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
4291 Elf_Internal_Sym sym
;
4292 Elf_External_Sym
*dynsym
=
4293 (Elf_External_Sym
*)finfo
.dynsym_sec
->contents
;
4294 unsigned long last_local
= 0;
4296 /* Write out the section symbols for the output sections. */
4303 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4306 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4309 indx
= elf_section_data (s
)->this_idx
;
4310 BFD_ASSERT (indx
> 0);
4311 sym
.st_shndx
= indx
;
4312 sym
.st_value
= s
->vma
;
4314 elf_swap_symbol_out (abfd
, &sym
,
4315 dynsym
+ elf_section_data (s
)->dynindx
);
4318 last_local
= bfd_count_sections (abfd
);
4321 /* Write out the local dynsyms. */
4322 if (elf_hash_table (info
)->dynlocal
)
4324 struct elf_link_local_dynamic_entry
*e
;
4325 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
4329 sym
.st_size
= e
->isym
.st_size
;
4330 sym
.st_other
= e
->isym
.st_other
;
4332 /* Copy the internal symbol as is.
4333 Note that we saved a word of storage and overwrote
4334 the original st_name with the dynstr_index. */
4337 if (e
->isym
.st_shndx
> 0 && e
->isym
.st_shndx
< SHN_LORESERVE
)
4339 s
= bfd_section_from_elf_index (e
->input_bfd
,
4343 elf_section_data (s
->output_section
)->this_idx
;
4344 sym
.st_value
= (s
->output_section
->vma
4346 + e
->isym
.st_value
);
4349 if (last_local
< e
->dynindx
)
4350 last_local
= e
->dynindx
;
4352 elf_swap_symbol_out (abfd
, &sym
, dynsym
+ e
->dynindx
);
4356 elf_section_data (finfo
.dynsym_sec
->output_section
)
4357 ->this_hdr
.sh_info
= last_local
+ 1;
4360 /* We get the global symbols from the hash table. */
4361 eoinfo
.failed
= false;
4362 eoinfo
.localsyms
= false;
4363 eoinfo
.finfo
= &finfo
;
4364 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
4369 /* If backend needs to output some symbols not present in the hash
4370 table, do it now. */
4371 if (bed
->elf_backend_output_arch_syms
)
4373 if (! (*bed
->elf_backend_output_arch_syms
)
4374 (abfd
, info
, (PTR
) &finfo
,
4375 (boolean (*) PARAMS ((PTR
, const char *,
4376 Elf_Internal_Sym
*, asection
*)))
4377 elf_link_output_sym
))
4381 /* Flush all symbols to the file. */
4382 if (! elf_link_flush_output_syms (&finfo
))
4385 /* Now we know the size of the symtab section. */
4386 off
+= symtab_hdr
->sh_size
;
4388 /* Finish up and write out the symbol string table (.strtab)
4390 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4391 /* sh_name was set in prep_headers. */
4392 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4393 symstrtab_hdr
->sh_flags
= 0;
4394 symstrtab_hdr
->sh_addr
= 0;
4395 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
4396 symstrtab_hdr
->sh_entsize
= 0;
4397 symstrtab_hdr
->sh_link
= 0;
4398 symstrtab_hdr
->sh_info
= 0;
4399 /* sh_offset is set just below. */
4400 symstrtab_hdr
->sh_addralign
= 1;
4402 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, true);
4403 elf_tdata (abfd
)->next_file_pos
= off
;
4405 if (bfd_get_symcount (abfd
) > 0)
4407 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
4408 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
4412 /* Adjust the relocs to have the correct symbol indices. */
4413 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4415 if ((o
->flags
& SEC_RELOC
) == 0)
4418 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
4419 elf_section_data (o
)->rel_count
,
4420 elf_section_data (o
)->rel_hashes
);
4421 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
4422 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
4423 elf_section_data (o
)->rel_count2
,
4424 (elf_section_data (o
)->rel_hashes
4425 + elf_section_data (o
)->rel_count
));
4427 /* Set the reloc_count field to 0 to prevent write_relocs from
4428 trying to swap the relocs out itself. */
4432 /* If we are linking against a dynamic object, or generating a
4433 shared library, finish up the dynamic linking information. */
4436 Elf_External_Dyn
*dyncon
, *dynconend
;
4438 /* Fix up .dynamic entries. */
4439 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
4440 BFD_ASSERT (o
!= NULL
);
4442 dyncon
= (Elf_External_Dyn
*) o
->contents
;
4443 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
4444 for (; dyncon
< dynconend
; dyncon
++)
4446 Elf_Internal_Dyn dyn
;
4450 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
4457 name
= info
->init_function
;
4460 name
= info
->fini_function
;
4463 struct elf_link_hash_entry
*h
;
4465 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
4466 false, false, true);
4468 && (h
->root
.type
== bfd_link_hash_defined
4469 || h
->root
.type
== bfd_link_hash_defweak
))
4471 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
4472 o
= h
->root
.u
.def
.section
;
4473 if (o
->output_section
!= NULL
)
4474 dyn
.d_un
.d_val
+= (o
->output_section
->vma
4475 + o
->output_offset
);
4478 /* The symbol is imported from another shared
4479 library and does not apply to this one. */
4483 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
4498 name
= ".gnu.version_d";
4501 name
= ".gnu.version_r";
4504 name
= ".gnu.version";
4506 o
= bfd_get_section_by_name (abfd
, name
);
4507 BFD_ASSERT (o
!= NULL
);
4508 dyn
.d_un
.d_ptr
= o
->vma
;
4509 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
4516 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
4521 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
4523 Elf_Internal_Shdr
*hdr
;
4525 hdr
= elf_elfsections (abfd
)[i
];
4526 if (hdr
->sh_type
== type
4527 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
4529 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
4530 dyn
.d_un
.d_val
+= hdr
->sh_size
;
4533 if (dyn
.d_un
.d_val
== 0
4534 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
4535 dyn
.d_un
.d_val
= hdr
->sh_addr
;
4539 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
4545 /* If we have created any dynamic sections, then output them. */
4548 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
4551 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4553 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4554 || o
->_raw_size
== 0)
4556 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
4558 /* At this point, we are only interested in sections
4559 created by elf_link_create_dynamic_sections. */
4562 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
4564 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
4566 if (! bfd_set_section_contents (abfd
, o
->output_section
,
4567 o
->contents
, o
->output_offset
,
4575 /* The contents of the .dynstr section are actually in a
4577 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
4578 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
4579 || ! _bfd_stringtab_emit (abfd
,
4580 elf_hash_table (info
)->dynstr
))
4586 /* If we have optimized stabs strings, output them. */
4587 if (elf_hash_table (info
)->stab_info
!= NULL
)
4589 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
4593 if (finfo
.symstrtab
!= NULL
)
4594 _bfd_stringtab_free (finfo
.symstrtab
);
4595 if (finfo
.contents
!= NULL
)
4596 free (finfo
.contents
);
4597 if (finfo
.external_relocs
!= NULL
)
4598 free (finfo
.external_relocs
);
4599 if (finfo
.internal_relocs
!= NULL
)
4600 free (finfo
.internal_relocs
);
4601 if (finfo
.external_syms
!= NULL
)
4602 free (finfo
.external_syms
);
4603 if (finfo
.internal_syms
!= NULL
)
4604 free (finfo
.internal_syms
);
4605 if (finfo
.indices
!= NULL
)
4606 free (finfo
.indices
);
4607 if (finfo
.sections
!= NULL
)
4608 free (finfo
.sections
);
4609 if (finfo
.symbuf
!= NULL
)
4610 free (finfo
.symbuf
);
4611 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4613 if ((o
->flags
& SEC_RELOC
) != 0
4614 && elf_section_data (o
)->rel_hashes
!= NULL
)
4615 free (elf_section_data (o
)->rel_hashes
);
4618 elf_tdata (abfd
)->linker
= true;
4623 if (finfo
.symstrtab
!= NULL
)
4624 _bfd_stringtab_free (finfo
.symstrtab
);
4625 if (finfo
.contents
!= NULL
)
4626 free (finfo
.contents
);
4627 if (finfo
.external_relocs
!= NULL
)
4628 free (finfo
.external_relocs
);
4629 if (finfo
.internal_relocs
!= NULL
)
4630 free (finfo
.internal_relocs
);
4631 if (finfo
.external_syms
!= NULL
)
4632 free (finfo
.external_syms
);
4633 if (finfo
.internal_syms
!= NULL
)
4634 free (finfo
.internal_syms
);
4635 if (finfo
.indices
!= NULL
)
4636 free (finfo
.indices
);
4637 if (finfo
.sections
!= NULL
)
4638 free (finfo
.sections
);
4639 if (finfo
.symbuf
!= NULL
)
4640 free (finfo
.symbuf
);
4641 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4643 if ((o
->flags
& SEC_RELOC
) != 0
4644 && elf_section_data (o
)->rel_hashes
!= NULL
)
4645 free (elf_section_data (o
)->rel_hashes
);
4651 /* Add a symbol to the output symbol table. */
4654 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
4655 struct elf_final_link_info
*finfo
;
4657 Elf_Internal_Sym
*elfsym
;
4658 asection
*input_sec
;
4660 boolean (*output_symbol_hook
) PARAMS ((bfd
*,
4661 struct bfd_link_info
*info
,
4666 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
4667 elf_backend_link_output_symbol_hook
;
4668 if (output_symbol_hook
!= NULL
)
4670 if (! ((*output_symbol_hook
)
4671 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
4675 if (name
== (const char *) NULL
|| *name
== '\0')
4676 elfsym
->st_name
= 0;
4677 else if (input_sec
->flags
& SEC_EXCLUDE
)
4678 elfsym
->st_name
= 0;
4681 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
4684 if (elfsym
->st_name
== (unsigned long) -1)
4688 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
4690 if (! elf_link_flush_output_syms (finfo
))
4694 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
,
4695 (PTR
) (finfo
->symbuf
+ finfo
->symbuf_count
));
4696 ++finfo
->symbuf_count
;
4698 ++ bfd_get_symcount (finfo
->output_bfd
);
4703 /* Flush the output symbols to the file. */
4706 elf_link_flush_output_syms (finfo
)
4707 struct elf_final_link_info
*finfo
;
4709 if (finfo
->symbuf_count
> 0)
4711 Elf_Internal_Shdr
*symtab
;
4713 symtab
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
4715 if (bfd_seek (finfo
->output_bfd
, symtab
->sh_offset
+ symtab
->sh_size
,
4717 || (bfd_write ((PTR
) finfo
->symbuf
, finfo
->symbuf_count
,
4718 sizeof (Elf_External_Sym
), finfo
->output_bfd
)
4719 != finfo
->symbuf_count
* sizeof (Elf_External_Sym
)))
4722 symtab
->sh_size
+= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
4724 finfo
->symbuf_count
= 0;
4730 /* Add an external symbol to the symbol table. This is called from
4731 the hash table traversal routine. When generating a shared object,
4732 we go through the symbol table twice. The first time we output
4733 anything that might have been forced to local scope in a version
4734 script. The second time we output the symbols that are still
4738 elf_link_output_extsym (h
, data
)
4739 struct elf_link_hash_entry
*h
;
4742 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
4743 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
4745 Elf_Internal_Sym sym
;
4746 asection
*input_sec
;
4748 /* Decide whether to output this symbol in this pass. */
4749 if (eoinfo
->localsyms
)
4751 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
4756 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4760 /* If we are not creating a shared library, and this symbol is
4761 referenced by a shared library but is not defined anywhere, then
4762 warn that it is undefined. If we do not do this, the runtime
4763 linker will complain that the symbol is undefined when the
4764 program is run. We don't have to worry about symbols that are
4765 referenced by regular files, because we will already have issued
4766 warnings for them. */
4767 if (! finfo
->info
->relocateable
4768 && ! (finfo
->info
->shared
4769 && !finfo
->info
->no_undefined
)
4770 && h
->root
.type
== bfd_link_hash_undefined
4771 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
4772 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
4774 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
4775 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
4776 (asection
*) NULL
, 0)))
4778 eoinfo
->failed
= true;
4783 /* We don't want to output symbols that have never been mentioned by
4784 a regular file, or that we have been told to strip. However, if
4785 h->indx is set to -2, the symbol is used by a reloc and we must
4789 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
4790 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
4791 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
4792 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
4794 else if (finfo
->info
->strip
== strip_all
4795 || (finfo
->info
->strip
== strip_some
4796 && bfd_hash_lookup (finfo
->info
->keep_hash
,
4797 h
->root
.root
.string
,
4798 false, false) == NULL
))
4803 /* If we're stripping it, and it's not a dynamic symbol, there's
4804 nothing else to do. */
4805 if (strip
&& h
->dynindx
== -1)
4809 sym
.st_size
= h
->size
;
4810 sym
.st_other
= h
->other
;
4811 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4812 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
4813 else if (h
->root
.type
== bfd_link_hash_undefweak
4814 || h
->root
.type
== bfd_link_hash_defweak
)
4815 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
4817 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
4819 switch (h
->root
.type
)
4822 case bfd_link_hash_new
:
4826 case bfd_link_hash_undefined
:
4827 input_sec
= bfd_und_section_ptr
;
4828 sym
.st_shndx
= SHN_UNDEF
;
4831 case bfd_link_hash_undefweak
:
4832 input_sec
= bfd_und_section_ptr
;
4833 sym
.st_shndx
= SHN_UNDEF
;
4836 case bfd_link_hash_defined
:
4837 case bfd_link_hash_defweak
:
4839 input_sec
= h
->root
.u
.def
.section
;
4840 if (input_sec
->output_section
!= NULL
)
4843 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
4844 input_sec
->output_section
);
4845 if (sym
.st_shndx
== (unsigned short) -1)
4847 (*_bfd_error_handler
)
4848 (_("%s: could not find output section %s for input section %s"),
4849 bfd_get_filename (finfo
->output_bfd
),
4850 input_sec
->output_section
->name
,
4852 eoinfo
->failed
= true;
4856 /* ELF symbols in relocateable files are section relative,
4857 but in nonrelocateable files they are virtual
4859 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
4860 if (! finfo
->info
->relocateable
)
4861 sym
.st_value
+= input_sec
->output_section
->vma
;
4865 BFD_ASSERT (input_sec
->owner
== NULL
4866 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
4867 sym
.st_shndx
= SHN_UNDEF
;
4868 input_sec
= bfd_und_section_ptr
;
4873 case bfd_link_hash_common
:
4874 input_sec
= h
->root
.u
.c
.p
->section
;
4875 sym
.st_shndx
= SHN_COMMON
;
4876 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
4879 case bfd_link_hash_indirect
:
4880 /* These symbols are created by symbol versioning. They point
4881 to the decorated version of the name. For example, if the
4882 symbol foo@@GNU_1.2 is the default, which should be used when
4883 foo is used with no version, then we add an indirect symbol
4884 foo which points to foo@@GNU_1.2. We ignore these symbols,
4885 since the indirected symbol is already in the hash table. If
4886 the indirect symbol is non-ELF, fall through and output it. */
4887 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) == 0)
4891 case bfd_link_hash_warning
:
4892 /* We can't represent these symbols in ELF, although a warning
4893 symbol may have come from a .gnu.warning.SYMBOL section. We
4894 just put the target symbol in the hash table. If the target
4895 symbol does not really exist, don't do anything. */
4896 if (h
->root
.u
.i
.link
->type
== bfd_link_hash_new
)
4898 return (elf_link_output_extsym
4899 ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
));
4902 /* Give the processor backend a chance to tweak the symbol value,
4903 and also to finish up anything that needs to be done for this
4905 if ((h
->dynindx
!= -1
4906 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4907 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4909 struct elf_backend_data
*bed
;
4911 bed
= get_elf_backend_data (finfo
->output_bfd
);
4912 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
4913 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
4915 eoinfo
->failed
= true;
4920 /* If we are marking the symbol as undefined, and there are no
4921 non-weak references to this symbol from a regular object, then
4922 mark the symbol as weak undefined; if there are non-weak
4923 references, mark the symbol as strong. We can't do this earlier,
4924 because it might not be marked as undefined until the
4925 finish_dynamic_symbol routine gets through with it. */
4926 if (sym
.st_shndx
== SHN_UNDEF
4927 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
4928 && (ELF_ST_BIND(sym
.st_info
) == STB_GLOBAL
4929 || ELF_ST_BIND(sym
.st_info
) == STB_WEAK
))
4933 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
4934 bindtype
= STB_GLOBAL
;
4936 bindtype
= STB_WEAK
;
4937 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
4940 /* If this symbol should be put in the .dynsym section, then put it
4941 there now. We have already know the symbol index. We also fill
4942 in the entry in the .hash section. */
4943 if (h
->dynindx
!= -1
4944 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4948 size_t hash_entry_size
;
4949 bfd_byte
*bucketpos
;
4952 sym
.st_name
= h
->dynstr_index
;
4954 elf_swap_symbol_out (finfo
->output_bfd
, &sym
,
4955 (PTR
) (((Elf_External_Sym
*)
4956 finfo
->dynsym_sec
->contents
)
4959 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
4960 bucket
= h
->elf_hash_value
% bucketcount
;
4962 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
4963 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
4964 + (bucket
+ 2) * hash_entry_size
);
4965 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
4966 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
4967 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
4968 ((bfd_byte
*) finfo
->hash_sec
->contents
4969 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
4971 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
4973 Elf_Internal_Versym iversym
;
4975 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4977 if (h
->verinfo
.verdef
== NULL
)
4978 iversym
.vs_vers
= 0;
4980 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4984 if (h
->verinfo
.vertree
== NULL
)
4985 iversym
.vs_vers
= 1;
4987 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
4990 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
4991 iversym
.vs_vers
|= VERSYM_HIDDEN
;
4993 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
,
4994 (((Elf_External_Versym
*)
4995 finfo
->symver_sec
->contents
)
5000 /* If we're stripping it, then it was just a dynamic symbol, and
5001 there's nothing else to do. */
5005 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
5007 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
5009 eoinfo
->failed
= true;
5016 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
5017 originated from the section given by INPUT_REL_HDR) to the
5021 elf_link_output_relocs (output_bfd
, input_section
, input_rel_hdr
,
5024 asection
*input_section
;
5025 Elf_Internal_Shdr
*input_rel_hdr
;
5026 Elf_Internal_Rela
*internal_relocs
;
5028 Elf_Internal_Rela
*irela
;
5029 Elf_Internal_Rela
*irelaend
;
5030 Elf_Internal_Shdr
*output_rel_hdr
;
5031 asection
*output_section
;
5032 unsigned int *rel_countp
= NULL
;
5034 output_section
= input_section
->output_section
;
5035 output_rel_hdr
= NULL
;
5037 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
5038 == input_rel_hdr
->sh_entsize
)
5040 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
5041 rel_countp
= &elf_section_data (output_section
)->rel_count
;
5043 else if (elf_section_data (output_section
)->rel_hdr2
5044 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
5045 == input_rel_hdr
->sh_entsize
))
5047 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
5048 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
5051 BFD_ASSERT (output_rel_hdr
!= NULL
);
5053 irela
= internal_relocs
;
5054 irelaend
= irela
+ input_rel_hdr
->sh_size
/ input_rel_hdr
->sh_entsize
;
5055 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
5057 Elf_External_Rel
*erel
;
5059 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
+ *rel_countp
);
5060 for (; irela
< irelaend
; irela
++, erel
++)
5062 Elf_Internal_Rel irel
;
5064 irel
.r_offset
= irela
->r_offset
;
5065 irel
.r_info
= irela
->r_info
;
5066 BFD_ASSERT (irela
->r_addend
== 0);
5067 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
5072 Elf_External_Rela
*erela
;
5074 BFD_ASSERT (input_rel_hdr
->sh_entsize
5075 == sizeof (Elf_External_Rela
));
5076 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
+ *rel_countp
);
5077 for (; irela
< irelaend
; irela
++, erela
++)
5078 elf_swap_reloca_out (output_bfd
, irela
, erela
);
5081 /* Bump the counter, so that we know where to add the next set of
5083 *rel_countp
+= input_rel_hdr
->sh_size
/ input_rel_hdr
->sh_entsize
;
5086 /* Link an input file into the linker output file. This function
5087 handles all the sections and relocations of the input file at once.
5088 This is so that we only have to read the local symbols once, and
5089 don't have to keep them in memory. */
5092 elf_link_input_bfd (finfo
, input_bfd
)
5093 struct elf_final_link_info
*finfo
;
5096 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
5097 bfd
*, asection
*, bfd_byte
*,
5098 Elf_Internal_Rela
*,
5099 Elf_Internal_Sym
*, asection
**));
5101 Elf_Internal_Shdr
*symtab_hdr
;
5104 Elf_External_Sym
*external_syms
;
5105 Elf_External_Sym
*esym
;
5106 Elf_External_Sym
*esymend
;
5107 Elf_Internal_Sym
*isym
;
5109 asection
**ppsection
;
5111 struct elf_backend_data
*bed
;
5113 output_bfd
= finfo
->output_bfd
;
5114 bed
= get_elf_backend_data (output_bfd
);
5115 relocate_section
= bed
->elf_backend_relocate_section
;
5117 /* If this is a dynamic object, we don't want to do anything here:
5118 we don't want the local symbols, and we don't want the section
5120 if ((input_bfd
->flags
& DYNAMIC
) != 0)
5123 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
5124 if (elf_bad_symtab (input_bfd
))
5126 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
5131 locsymcount
= symtab_hdr
->sh_info
;
5132 extsymoff
= symtab_hdr
->sh_info
;
5135 /* Read the local symbols. */
5136 if (symtab_hdr
->contents
!= NULL
)
5137 external_syms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
5138 else if (locsymcount
== 0)
5139 external_syms
= NULL
;
5142 external_syms
= finfo
->external_syms
;
5143 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
5144 || (bfd_read (external_syms
, sizeof (Elf_External_Sym
),
5145 locsymcount
, input_bfd
)
5146 != locsymcount
* sizeof (Elf_External_Sym
)))
5150 /* Swap in the local symbols and write out the ones which we know
5151 are going into the output file. */
5152 esym
= external_syms
;
5153 esymend
= esym
+ locsymcount
;
5154 isym
= finfo
->internal_syms
;
5155 pindex
= finfo
->indices
;
5156 ppsection
= finfo
->sections
;
5157 for (; esym
< esymend
; esym
++, isym
++, pindex
++, ppsection
++)
5161 Elf_Internal_Sym osym
;
5163 elf_swap_symbol_in (input_bfd
, esym
, isym
);
5166 if (elf_bad_symtab (input_bfd
))
5168 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
5175 if (isym
->st_shndx
== SHN_UNDEF
)
5176 isec
= bfd_und_section_ptr
;
5177 else if (isym
->st_shndx
> 0 && isym
->st_shndx
< SHN_LORESERVE
)
5178 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
5179 else if (isym
->st_shndx
== SHN_ABS
)
5180 isec
= bfd_abs_section_ptr
;
5181 else if (isym
->st_shndx
== SHN_COMMON
)
5182 isec
= bfd_com_section_ptr
;
5191 /* Don't output the first, undefined, symbol. */
5192 if (esym
== external_syms
)
5195 /* If we are stripping all symbols, we don't want to output this
5197 if (finfo
->info
->strip
== strip_all
)
5200 /* We never output section symbols. Instead, we use the section
5201 symbol of the corresponding section in the output file. */
5202 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
5205 /* If we are discarding all local symbols, we don't want to
5206 output this one. If we are generating a relocateable output
5207 file, then some of the local symbols may be required by
5208 relocs; we output them below as we discover that they are
5210 if (finfo
->info
->discard
== discard_all
)
5213 /* If this symbol is defined in a section which we are
5214 discarding, we don't need to keep it, but note that
5215 linker_mark is only reliable for sections that have contents.
5216 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
5217 as well as linker_mark. */
5218 if (isym
->st_shndx
> 0
5219 && isym
->st_shndx
< SHN_LORESERVE
5221 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
5222 || (! finfo
->info
->relocateable
5223 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
5226 /* Get the name of the symbol. */
5227 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
5232 /* See if we are discarding symbols with this name. */
5233 if ((finfo
->info
->strip
== strip_some
5234 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
5236 || (finfo
->info
->discard
== discard_l
5237 && bfd_is_local_label_name (input_bfd
, name
)))
5240 /* If we get here, we are going to output this symbol. */
5244 /* Adjust the section index for the output file. */
5245 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
5246 isec
->output_section
);
5247 if (osym
.st_shndx
== (unsigned short) -1)
5250 *pindex
= bfd_get_symcount (output_bfd
);
5252 /* ELF symbols in relocateable files are section relative, but
5253 in executable files they are virtual addresses. Note that
5254 this code assumes that all ELF sections have an associated
5255 BFD section with a reasonable value for output_offset; below
5256 we assume that they also have a reasonable value for
5257 output_section. Any special sections must be set up to meet
5258 these requirements. */
5259 osym
.st_value
+= isec
->output_offset
;
5260 if (! finfo
->info
->relocateable
)
5261 osym
.st_value
+= isec
->output_section
->vma
;
5263 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
5267 /* Relocate the contents of each section. */
5268 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
5272 if (! o
->linker_mark
)
5274 /* This section was omitted from the link. */
5278 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5279 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
5282 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
5284 /* Section was created by elf_link_create_dynamic_sections
5289 /* Get the contents of the section. They have been cached by a
5290 relaxation routine. Note that o is a section in an input
5291 file, so the contents field will not have been set by any of
5292 the routines which work on output files. */
5293 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
5294 contents
= elf_section_data (o
)->this_hdr
.contents
;
5297 contents
= finfo
->contents
;
5298 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
5299 (file_ptr
) 0, o
->_raw_size
))
5303 if ((o
->flags
& SEC_RELOC
) != 0)
5305 Elf_Internal_Rela
*internal_relocs
;
5307 /* Get the swapped relocs. */
5308 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
5309 (input_bfd
, o
, finfo
->external_relocs
,
5310 finfo
->internal_relocs
, false));
5311 if (internal_relocs
== NULL
5312 && o
->reloc_count
> 0)
5315 /* Relocate the section by invoking a back end routine.
5317 The back end routine is responsible for adjusting the
5318 section contents as necessary, and (if using Rela relocs
5319 and generating a relocateable output file) adjusting the
5320 reloc addend as necessary.
5322 The back end routine does not have to worry about setting
5323 the reloc address or the reloc symbol index.
5325 The back end routine is given a pointer to the swapped in
5326 internal symbols, and can access the hash table entries
5327 for the external symbols via elf_sym_hashes (input_bfd).
5329 When generating relocateable output, the back end routine
5330 must handle STB_LOCAL/STT_SECTION symbols specially. The
5331 output symbol is going to be a section symbol
5332 corresponding to the output section, which will require
5333 the addend to be adjusted. */
5335 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
5336 input_bfd
, o
, contents
,
5338 finfo
->internal_syms
,
5342 if (finfo
->info
->relocateable
)
5344 Elf_Internal_Rela
*irela
;
5345 Elf_Internal_Rela
*irelaend
;
5346 struct elf_link_hash_entry
**rel_hash
;
5347 Elf_Internal_Shdr
*input_rel_hdr
;
5349 /* Adjust the reloc addresses and symbol indices. */
5351 irela
= internal_relocs
;
5353 irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5354 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
5355 + elf_section_data (o
->output_section
)->rel_count
5356 + elf_section_data (o
->output_section
)->rel_count2
);
5357 for (; irela
< irelaend
; irela
++, rel_hash
++)
5359 unsigned long r_symndx
;
5360 Elf_Internal_Sym
*isym
;
5363 irela
->r_offset
+= o
->output_offset
;
5365 r_symndx
= ELF_R_SYM (irela
->r_info
);
5370 if (r_symndx
>= locsymcount
5371 || (elf_bad_symtab (input_bfd
)
5372 && finfo
->sections
[r_symndx
] == NULL
))
5374 struct elf_link_hash_entry
*rh
;
5377 /* This is a reloc against a global symbol. We
5378 have not yet output all the local symbols, so
5379 we do not know the symbol index of any global
5380 symbol. We set the rel_hash entry for this
5381 reloc to point to the global hash table entry
5382 for this symbol. The symbol index is then
5383 set at the end of elf_bfd_final_link. */
5384 indx
= r_symndx
- extsymoff
;
5385 rh
= elf_sym_hashes (input_bfd
)[indx
];
5386 while (rh
->root
.type
== bfd_link_hash_indirect
5387 || rh
->root
.type
== bfd_link_hash_warning
)
5388 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
5390 /* Setting the index to -2 tells
5391 elf_link_output_extsym that this symbol is
5393 BFD_ASSERT (rh
->indx
< 0);
5401 /* This is a reloc against a local symbol. */
5404 isym
= finfo
->internal_syms
+ r_symndx
;
5405 sec
= finfo
->sections
[r_symndx
];
5406 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
5408 /* I suppose the backend ought to fill in the
5409 section of any STT_SECTION symbol against a
5410 processor specific section. If we have
5411 discarded a section, the output_section will
5412 be the absolute section. */
5414 && (bfd_is_abs_section (sec
)
5415 || (sec
->output_section
!= NULL
5416 && bfd_is_abs_section (sec
->output_section
))))
5418 else if (sec
== NULL
|| sec
->owner
== NULL
)
5420 bfd_set_error (bfd_error_bad_value
);
5425 r_symndx
= sec
->output_section
->target_index
;
5426 BFD_ASSERT (r_symndx
!= 0);
5431 if (finfo
->indices
[r_symndx
] == -1)
5437 if (finfo
->info
->strip
== strip_all
)
5439 /* You can't do ld -r -s. */
5440 bfd_set_error (bfd_error_invalid_operation
);
5444 /* This symbol was skipped earlier, but
5445 since it is needed by a reloc, we
5446 must output it now. */
5447 link
= symtab_hdr
->sh_link
;
5448 name
= bfd_elf_string_from_elf_section (input_bfd
,
5454 osec
= sec
->output_section
;
5456 _bfd_elf_section_from_bfd_section (output_bfd
,
5458 if (isym
->st_shndx
== (unsigned short) -1)
5461 isym
->st_value
+= sec
->output_offset
;
5462 if (! finfo
->info
->relocateable
)
5463 isym
->st_value
+= osec
->vma
;
5465 finfo
->indices
[r_symndx
] = bfd_get_symcount (output_bfd
);
5467 if (! elf_link_output_sym (finfo
, name
, isym
, sec
))
5471 r_symndx
= finfo
->indices
[r_symndx
];
5474 irela
->r_info
= ELF_R_INFO (r_symndx
,
5475 ELF_R_TYPE (irela
->r_info
));
5478 /* Swap out the relocs. */
5479 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
5480 elf_link_output_relocs (output_bfd
, o
,
5484 += input_rel_hdr
->sh_size
/ input_rel_hdr
->sh_entsize
;
5485 input_rel_hdr
= elf_section_data (o
)->rel_hdr2
;
5487 elf_link_output_relocs (output_bfd
, o
,
5493 /* Write out the modified section contents. */
5494 if (elf_section_data (o
)->stab_info
== NULL
)
5496 if (! (o
->flags
& SEC_EXCLUDE
) &&
5497 ! bfd_set_section_contents (output_bfd
, o
->output_section
,
5498 contents
, o
->output_offset
,
5499 (o
->_cooked_size
!= 0
5506 if (! (_bfd_write_section_stabs
5507 (output_bfd
, &elf_hash_table (finfo
->info
)->stab_info
,
5508 o
, &elf_section_data (o
)->stab_info
, contents
)))
5516 /* Generate a reloc when linking an ELF file. This is a reloc
5517 requested by the linker, and does come from any input file. This
5518 is used to build constructor and destructor tables when linking
5522 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
5524 struct bfd_link_info
*info
;
5525 asection
*output_section
;
5526 struct bfd_link_order
*link_order
;
5528 reloc_howto_type
*howto
;
5532 struct elf_link_hash_entry
**rel_hash_ptr
;
5533 Elf_Internal_Shdr
*rel_hdr
;
5535 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5538 bfd_set_error (bfd_error_bad_value
);
5542 addend
= link_order
->u
.reloc
.p
->addend
;
5544 /* Figure out the symbol index. */
5545 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
5546 + elf_section_data (output_section
)->rel_count
5547 + elf_section_data (output_section
)->rel_count2
);
5548 if (link_order
->type
== bfd_section_reloc_link_order
)
5550 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
5551 BFD_ASSERT (indx
!= 0);
5552 *rel_hash_ptr
= NULL
;
5556 struct elf_link_hash_entry
*h
;
5558 /* Treat a reloc against a defined symbol as though it were
5559 actually against the section. */
5560 h
= ((struct elf_link_hash_entry
*)
5561 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
5562 link_order
->u
.reloc
.p
->u
.name
,
5563 false, false, true));
5565 && (h
->root
.type
== bfd_link_hash_defined
5566 || h
->root
.type
== bfd_link_hash_defweak
))
5570 section
= h
->root
.u
.def
.section
;
5571 indx
= section
->output_section
->target_index
;
5572 *rel_hash_ptr
= NULL
;
5573 /* It seems that we ought to add the symbol value to the
5574 addend here, but in practice it has already been added
5575 because it was passed to constructor_callback. */
5576 addend
+= section
->output_section
->vma
+ section
->output_offset
;
5580 /* Setting the index to -2 tells elf_link_output_extsym that
5581 this symbol is used by a reloc. */
5588 if (! ((*info
->callbacks
->unattached_reloc
)
5589 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
5590 (asection
*) NULL
, (bfd_vma
) 0)))
5596 /* If this is an inplace reloc, we must write the addend into the
5598 if (howto
->partial_inplace
&& addend
!= 0)
5601 bfd_reloc_status_type rstat
;
5605 size
= bfd_get_reloc_size (howto
);
5606 buf
= (bfd_byte
*) bfd_zmalloc (size
);
5607 if (buf
== (bfd_byte
*) NULL
)
5609 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5615 case bfd_reloc_outofrange
:
5617 case bfd_reloc_overflow
:
5618 if (! ((*info
->callbacks
->reloc_overflow
)
5620 (link_order
->type
== bfd_section_reloc_link_order
5621 ? bfd_section_name (output_bfd
,
5622 link_order
->u
.reloc
.p
->u
.section
)
5623 : link_order
->u
.reloc
.p
->u
.name
),
5624 howto
->name
, addend
, (bfd
*) NULL
, (asection
*) NULL
,
5632 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
5633 (file_ptr
) link_order
->offset
, size
);
5639 /* The address of a reloc is relative to the section in a
5640 relocateable file, and is a virtual address in an executable
5642 offset
= link_order
->offset
;
5643 if (! info
->relocateable
)
5644 offset
+= output_section
->vma
;
5646 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
5648 if (rel_hdr
->sh_type
== SHT_REL
)
5650 Elf_Internal_Rel irel
;
5651 Elf_External_Rel
*erel
;
5653 irel
.r_offset
= offset
;
5654 irel
.r_info
= ELF_R_INFO (indx
, howto
->type
);
5655 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
5656 + elf_section_data (output_section
)->rel_count
);
5657 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
5661 Elf_Internal_Rela irela
;
5662 Elf_External_Rela
*erela
;
5664 irela
.r_offset
= offset
;
5665 irela
.r_info
= ELF_R_INFO (indx
, howto
->type
);
5666 irela
.r_addend
= addend
;
5667 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
5668 + elf_section_data (output_section
)->rel_count
);
5669 elf_swap_reloca_out (output_bfd
, &irela
, erela
);
5672 ++elf_section_data (output_section
)->rel_count
;
5678 /* Allocate a pointer to live in a linker created section. */
5681 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
5683 struct bfd_link_info
*info
;
5684 elf_linker_section_t
*lsect
;
5685 struct elf_link_hash_entry
*h
;
5686 const Elf_Internal_Rela
*rel
;
5688 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
5689 elf_linker_section_pointers_t
*linker_section_ptr
;
5690 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);;
5692 BFD_ASSERT (lsect
!= NULL
);
5694 /* Is this a global symbol? */
5697 /* Has this symbol already been allocated, if so, our work is done */
5698 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
5703 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
5704 /* Make sure this symbol is output as a dynamic symbol. */
5705 if (h
->dynindx
== -1)
5707 if (! elf_link_record_dynamic_symbol (info
, h
))
5711 if (lsect
->rel_section
)
5712 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
5715 else /* Allocation of a pointer to a local symbol */
5717 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
5719 /* Allocate a table to hold the local symbols if first time */
5722 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
5723 register unsigned int i
;
5725 ptr
= (elf_linker_section_pointers_t
**)
5726 bfd_alloc (abfd
, num_symbols
* sizeof (elf_linker_section_pointers_t
*));
5731 elf_local_ptr_offsets (abfd
) = ptr
;
5732 for (i
= 0; i
< num_symbols
; i
++)
5733 ptr
[i
] = (elf_linker_section_pointers_t
*)0;
5736 /* Has this symbol already been allocated, if so, our work is done */
5737 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
5742 ptr_linker_section_ptr
= &ptr
[r_symndx
];
5746 /* If we are generating a shared object, we need to
5747 output a R_<xxx>_RELATIVE reloc so that the
5748 dynamic linker can adjust this GOT entry. */
5749 BFD_ASSERT (lsect
->rel_section
!= NULL
);
5750 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
5754 /* Allocate space for a pointer in the linker section, and allocate a new pointer record
5755 from internal memory. */
5756 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
5757 linker_section_ptr
= (elf_linker_section_pointers_t
*)
5758 bfd_alloc (abfd
, sizeof (elf_linker_section_pointers_t
));
5760 if (!linker_section_ptr
)
5763 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
5764 linker_section_ptr
->addend
= rel
->r_addend
;
5765 linker_section_ptr
->which
= lsect
->which
;
5766 linker_section_ptr
->written_address_p
= false;
5767 *ptr_linker_section_ptr
= linker_section_ptr
;
5770 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
5772 linker_section_ptr
->offset
= lsect
->section
->_raw_size
- lsect
->hole_size
+ (ARCH_SIZE
/ 8);
5773 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
5774 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
5775 if (lsect
->sym_hash
) /* Bump up symbol value if needed */
5777 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
5779 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
5780 lsect
->sym_hash
->root
.root
.string
,
5781 (long)ARCH_SIZE
/ 8,
5782 (long)lsect
->sym_hash
->root
.u
.def
.value
);
5788 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
5790 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
5793 fprintf (stderr
, "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
5794 lsect
->name
, (long)linker_section_ptr
->offset
, (long)lsect
->section
->_raw_size
);
5802 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
5805 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
5808 /* Fill in the address for a pointer generated in alinker section. */
5811 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
, relocation
, rel
, relative_reloc
)
5814 struct bfd_link_info
*info
;
5815 elf_linker_section_t
*lsect
;
5816 struct elf_link_hash_entry
*h
;
5818 const Elf_Internal_Rela
*rel
;
5821 elf_linker_section_pointers_t
*linker_section_ptr
;
5823 BFD_ASSERT (lsect
!= NULL
);
5825 if (h
!= NULL
) /* global symbol */
5827 linker_section_ptr
= _bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
5831 BFD_ASSERT (linker_section_ptr
!= NULL
);
5833 if (! elf_hash_table (info
)->dynamic_sections_created
5836 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
5838 /* This is actually a static link, or it is a
5839 -Bsymbolic link and the symbol is defined
5840 locally. We must initialize this entry in the
5843 When doing a dynamic link, we create a .rela.<xxx>
5844 relocation entry to initialize the value. This
5845 is done in the finish_dynamic_symbol routine. */
5846 if (!linker_section_ptr
->written_address_p
)
5848 linker_section_ptr
->written_address_p
= true;
5849 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
5850 lsect
->section
->contents
+ linker_section_ptr
->offset
);
5854 else /* local symbol */
5856 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
5857 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
5858 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
5859 linker_section_ptr
= _bfd_elf_find_pointer_linker_section (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
5863 BFD_ASSERT (linker_section_ptr
!= NULL
);
5865 /* Write out pointer if it hasn't been rewritten out before */
5866 if (!linker_section_ptr
->written_address_p
)
5868 linker_section_ptr
->written_address_p
= true;
5869 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
5870 lsect
->section
->contents
+ linker_section_ptr
->offset
);
5874 asection
*srel
= lsect
->rel_section
;
5875 Elf_Internal_Rela outrel
;
5877 /* We need to generate a relative reloc for the dynamic linker. */
5879 lsect
->rel_section
= srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
5882 BFD_ASSERT (srel
!= NULL
);
5884 outrel
.r_offset
= (lsect
->section
->output_section
->vma
5885 + lsect
->section
->output_offset
5886 + linker_section_ptr
->offset
);
5887 outrel
.r_info
= ELF_R_INFO (0, relative_reloc
);
5888 outrel
.r_addend
= 0;
5889 elf_swap_reloca_out (output_bfd
, &outrel
,
5890 (((Elf_External_Rela
*)
5891 lsect
->section
->contents
)
5892 + elf_section_data (lsect
->section
)->rel_count
));
5893 ++elf_section_data (lsect
->section
)->rel_count
;
5898 relocation
= (lsect
->section
->output_offset
5899 + linker_section_ptr
->offset
5900 - lsect
->hole_offset
5901 - lsect
->sym_offset
);
5904 fprintf (stderr
, "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
5905 lsect
->name
, (long)relocation
, (long)relocation
);
5908 /* Subtract out the addend, because it will get added back in by the normal
5910 return relocation
- linker_section_ptr
->addend
;
5913 /* Garbage collect unused sections. */
5915 static boolean elf_gc_mark
5916 PARAMS ((struct bfd_link_info
*info
, asection
*sec
,
5917 asection
* (*gc_mark_hook
)
5918 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
5919 struct elf_link_hash_entry
*, Elf_Internal_Sym
*))));
5921 static boolean elf_gc_sweep
5922 PARAMS ((struct bfd_link_info
*info
,
5923 boolean (*gc_sweep_hook
)
5924 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
5925 const Elf_Internal_Rela
*relocs
))));
5927 static boolean elf_gc_sweep_symbol
5928 PARAMS ((struct elf_link_hash_entry
*h
, PTR idxptr
));
5930 static boolean elf_gc_allocate_got_offsets
5931 PARAMS ((struct elf_link_hash_entry
*h
, PTR offarg
));
5933 static boolean elf_gc_propagate_vtable_entries_used
5934 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
5936 static boolean elf_gc_smash_unused_vtentry_relocs
5937 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
5939 /* The mark phase of garbage collection. For a given section, mark
5940 it, and all the sections which define symbols to which it refers. */
5943 elf_gc_mark (info
, sec
, gc_mark_hook
)
5944 struct bfd_link_info
*info
;
5946 asection
* (*gc_mark_hook
)
5947 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
5948 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
5954 /* Look through the section relocs. */
5956 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
5958 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
5959 Elf_Internal_Shdr
*symtab_hdr
;
5960 struct elf_link_hash_entry
**sym_hashes
;
5963 Elf_External_Sym
*locsyms
, *freesyms
= NULL
;
5964 bfd
*input_bfd
= sec
->owner
;
5965 struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
5967 /* GCFIXME: how to arrange so that relocs and symbols are not
5968 reread continually? */
5970 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
5971 sym_hashes
= elf_sym_hashes (input_bfd
);
5973 /* Read the local symbols. */
5974 if (elf_bad_symtab (input_bfd
))
5976 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
5980 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
5981 if (symtab_hdr
->contents
)
5982 locsyms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
5983 else if (nlocsyms
== 0)
5987 locsyms
= freesyms
=
5988 bfd_malloc (nlocsyms
* sizeof (Elf_External_Sym
));
5989 if (freesyms
== NULL
5990 || bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
5991 || (bfd_read (locsyms
, sizeof (Elf_External_Sym
),
5992 nlocsyms
, input_bfd
)
5993 != nlocsyms
* sizeof (Elf_External_Sym
)))
6000 /* Read the relocations. */
6001 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
6002 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
6003 info
->keep_memory
));
6004 if (relstart
== NULL
)
6009 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6011 for (rel
= relstart
; rel
< relend
; rel
++)
6013 unsigned long r_symndx
;
6015 struct elf_link_hash_entry
*h
;
6018 r_symndx
= ELF_R_SYM (rel
->r_info
);
6022 if (elf_bad_symtab (sec
->owner
))
6024 elf_swap_symbol_in (input_bfd
, &locsyms
[r_symndx
], &s
);
6025 if (ELF_ST_BIND (s
.st_info
) == STB_LOCAL
)
6026 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, NULL
, &s
);
6029 h
= sym_hashes
[r_symndx
- extsymoff
];
6030 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, h
, NULL
);
6033 else if (r_symndx
>= nlocsyms
)
6035 h
= sym_hashes
[r_symndx
- extsymoff
];
6036 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, h
, NULL
);
6040 elf_swap_symbol_in (input_bfd
, &locsyms
[r_symndx
], &s
);
6041 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, NULL
, &s
);
6044 if (rsec
&& !rsec
->gc_mark
)
6045 if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
6053 if (!info
->keep_memory
)
6063 /* The sweep phase of garbage collection. Remove all garbage sections. */
6066 elf_gc_sweep (info
, gc_sweep_hook
)
6067 struct bfd_link_info
*info
;
6068 boolean (*gc_sweep_hook
)
6069 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
6070 const Elf_Internal_Rela
*relocs
));
6074 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
6078 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6080 /* Keep special sections. Keep .debug sections. */
6081 if ((o
->flags
& SEC_LINKER_CREATED
)
6082 || (o
->flags
& SEC_DEBUGGING
))
6088 /* Skip sweeping sections already excluded. */
6089 if (o
->flags
& SEC_EXCLUDE
)
6092 /* Since this is early in the link process, it is simple
6093 to remove a section from the output. */
6094 o
->flags
|= SEC_EXCLUDE
;
6096 /* But we also have to update some of the relocation
6097 info we collected before. */
6099 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
6101 Elf_Internal_Rela
*internal_relocs
;
6104 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
6105 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
6106 if (internal_relocs
== NULL
)
6109 r
= (*gc_sweep_hook
)(o
->owner
, info
, o
, internal_relocs
);
6111 if (!info
->keep_memory
)
6112 free (internal_relocs
);
6120 /* Remove the symbols that were in the swept sections from the dynamic
6121 symbol table. GCFIXME: Anyone know how to get them out of the
6122 static symbol table as well? */
6126 elf_link_hash_traverse (elf_hash_table (info
),
6127 elf_gc_sweep_symbol
,
6130 elf_hash_table (info
)->dynsymcount
= i
;
6136 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6139 elf_gc_sweep_symbol (h
, idxptr
)
6140 struct elf_link_hash_entry
*h
;
6143 int *idx
= (int *) idxptr
;
6145 if (h
->dynindx
!= -1
6146 && ((h
->root
.type
!= bfd_link_hash_defined
6147 && h
->root
.type
!= bfd_link_hash_defweak
)
6148 || h
->root
.u
.def
.section
->gc_mark
))
6149 h
->dynindx
= (*idx
)++;
6154 /* Propogate collected vtable information. This is called through
6155 elf_link_hash_traverse. */
6158 elf_gc_propagate_vtable_entries_used (h
, okp
)
6159 struct elf_link_hash_entry
*h
;
6162 /* Those that are not vtables. */
6163 if (h
->vtable_parent
== NULL
)
6166 /* Those vtables that do not have parents, we cannot merge. */
6167 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
6170 /* If we've already been done, exit. */
6171 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
6174 /* Make sure the parent's table is up to date. */
6175 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
6177 if (h
->vtable_entries_used
== NULL
)
6179 /* None of this table's entries were referenced. Re-use the
6181 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
6182 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
6189 /* Or the parent's entries into ours. */
6190 cu
= h
->vtable_entries_used
;
6192 pu
= h
->vtable_parent
->vtable_entries_used
;
6195 n
= h
->vtable_parent
->vtable_entries_size
/ FILE_ALIGN
;
6198 if (*pu
) *cu
= true;
6208 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
6209 struct elf_link_hash_entry
*h
;
6213 bfd_vma hstart
, hend
;
6214 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
6215 struct elf_backend_data
*bed
;
6217 /* Take care of both those symbols that do not describe vtables as
6218 well as those that are not loaded. */
6219 if (h
->vtable_parent
== NULL
)
6222 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
6223 || h
->root
.type
== bfd_link_hash_defweak
);
6225 sec
= h
->root
.u
.def
.section
;
6226 hstart
= h
->root
.u
.def
.value
;
6227 hend
= hstart
+ h
->size
;
6229 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
6230 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, true));
6232 return *(boolean
*)okp
= false;
6233 bed
= get_elf_backend_data (sec
->owner
);
6234 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6236 for (rel
= relstart
; rel
< relend
; ++rel
)
6237 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
6239 /* If the entry is in use, do nothing. */
6240 if (h
->vtable_entries_used
6241 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
6243 bfd_vma entry
= (rel
->r_offset
- hstart
) / FILE_ALIGN
;
6244 if (h
->vtable_entries_used
[entry
])
6247 /* Otherwise, kill it. */
6248 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
6254 /* Do mark and sweep of unused sections. */
6257 elf_gc_sections (abfd
, info
)
6259 struct bfd_link_info
*info
;
6263 asection
* (*gc_mark_hook
)
6264 PARAMS ((bfd
*abfd
, struct bfd_link_info
*, Elf_Internal_Rela
*,
6265 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
6267 if (!get_elf_backend_data (abfd
)->can_gc_sections
6268 || info
->relocateable
6269 || elf_hash_table (info
)->dynamic_sections_created
)
6272 /* Apply transitive closure to the vtable entry usage info. */
6273 elf_link_hash_traverse (elf_hash_table (info
),
6274 elf_gc_propagate_vtable_entries_used
,
6279 /* Kill the vtable relocations that were not used. */
6280 elf_link_hash_traverse (elf_hash_table (info
),
6281 elf_gc_smash_unused_vtentry_relocs
,
6286 /* Grovel through relocs to find out who stays ... */
6288 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
6289 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
6292 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6294 if (o
->flags
& SEC_KEEP
)
6295 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
6300 /* ... and mark SEC_EXCLUDE for those that go. */
6301 if (!elf_gc_sweep(info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
6307 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
6310 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
6313 struct elf_link_hash_entry
*h
;
6316 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
6317 struct elf_link_hash_entry
**search
, *child
;
6318 bfd_size_type extsymcount
;
6320 /* The sh_info field of the symtab header tells us where the
6321 external symbols start. We don't care about the local symbols at
6323 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
6324 if (!elf_bad_symtab (abfd
))
6325 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
6327 sym_hashes
= elf_sym_hashes (abfd
);
6328 sym_hashes_end
= sym_hashes
+ extsymcount
;
6330 /* Hunt down the child symbol, which is in this section at the same
6331 offset as the relocation. */
6332 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
6334 if ((child
= *search
) != NULL
6335 && (child
->root
.type
== bfd_link_hash_defined
6336 || child
->root
.type
== bfd_link_hash_defweak
)
6337 && child
->root
.u
.def
.section
== sec
6338 && child
->root
.u
.def
.value
== offset
)
6342 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
6343 bfd_get_filename (abfd
), sec
->name
,
6344 (unsigned long)offset
);
6345 bfd_set_error (bfd_error_invalid_operation
);
6351 /* This *should* only be the absolute section. It could potentially
6352 be that someone has defined a non-global vtable though, which
6353 would be bad. It isn't worth paging in the local symbols to be
6354 sure though; that case should simply be handled by the assembler. */
6356 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
6359 child
->vtable_parent
= h
;
6364 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
6367 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
6368 bfd
*abfd ATTRIBUTE_UNUSED
;
6369 asection
*sec ATTRIBUTE_UNUSED
;
6370 struct elf_link_hash_entry
*h
;
6373 if (addend
>= h
->vtable_entries_size
)
6376 boolean
*ptr
= h
->vtable_entries_used
;
6378 /* While the symbol is undefined, we have to be prepared to handle
6380 if (h
->root
.type
== bfd_link_hash_undefined
)
6387 /* Oops! We've got a reference past the defined end of
6388 the table. This is probably a bug -- shall we warn? */
6393 /* Allocate one extra entry for use as a "done" flag for the
6394 consolidation pass. */
6395 bytes
= (size
/ FILE_ALIGN
+ 1) * sizeof(boolean
);
6401 ptr
= realloc (ptr
-1, bytes
);
6405 oldbytes
= (h
->vtable_entries_size
/FILE_ALIGN
+ 1) * sizeof(boolean
);
6406 memset (ptr
+ oldbytes
, 0, bytes
- oldbytes
);
6410 ptr
= calloc (1, bytes
);
6415 /* And arrange for that done flag to be at index -1. */
6416 h
->vtable_entries_used
= ptr
+1;
6417 h
->vtable_entries_size
= size
;
6419 h
->vtable_entries_used
[addend
/ FILE_ALIGN
] = true;
6424 /* And an accompanying bit to work out final got entry offsets once
6425 we're done. Should be called from final_link. */
6428 elf_gc_common_finalize_got_offsets (abfd
, info
)
6430 struct bfd_link_info
*info
;
6433 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6436 /* The GOT offset is relative to the .got section, but the GOT header is
6437 put into the .got.plt section, if the backend uses it. */
6438 if (bed
->want_got_plt
)
6441 gotoff
= bed
->got_header_size
;
6443 /* Do the local .got entries first. */
6444 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
6446 bfd_signed_vma
*local_got
= elf_local_got_refcounts (i
);
6447 bfd_size_type j
, locsymcount
;
6448 Elf_Internal_Shdr
*symtab_hdr
;
6453 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
6454 if (elf_bad_symtab (i
))
6455 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6457 locsymcount
= symtab_hdr
->sh_info
;
6459 for (j
= 0; j
< locsymcount
; ++j
)
6461 if (local_got
[j
] > 0)
6463 local_got
[j
] = gotoff
;
6464 gotoff
+= ARCH_SIZE
/ 8;
6467 local_got
[j
] = (bfd_vma
) -1;
6471 /* Then the global .got and .plt entries. */
6472 elf_link_hash_traverse (elf_hash_table (info
),
6473 elf_gc_allocate_got_offsets
,
6478 /* We need a special top-level link routine to convert got reference counts
6479 to real got offsets. */
6482 elf_gc_allocate_got_offsets (h
, offarg
)
6483 struct elf_link_hash_entry
*h
;
6486 bfd_vma
*off
= (bfd_vma
*) offarg
;
6488 if (h
->got
.refcount
> 0)
6490 h
->got
.offset
= off
[0];
6491 off
[0] += ARCH_SIZE
/ 8;
6494 h
->got
.offset
= (bfd_vma
) -1;
6499 /* Many folk need no more in the way of final link than this, once
6500 got entry reference counting is enabled. */
6503 elf_gc_common_final_link (abfd
, info
)
6505 struct bfd_link_info
*info
;
6507 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
6510 /* Invoke the regular ELF backend linker to do all the work. */
6511 return elf_bfd_final_link (abfd
, info
);
6514 /* This function will be called though elf_link_hash_traverse to store
6515 all hash value of the exported symbols in an array. */
6518 elf_collect_hash_codes (h
, data
)
6519 struct elf_link_hash_entry
*h
;
6522 unsigned long **valuep
= (unsigned long **) data
;
6528 /* Ignore indirect symbols. These are added by the versioning code. */
6529 if (h
->dynindx
== -1)
6532 name
= h
->root
.root
.string
;
6533 p
= strchr (name
, ELF_VER_CHR
);
6536 alc
= bfd_malloc (p
- name
+ 1);
6537 memcpy (alc
, name
, p
- name
);
6538 alc
[p
- name
] = '\0';
6542 /* Compute the hash value. */
6543 ha
= bfd_elf_hash (name
);
6545 /* Store the found hash value in the array given as the argument. */
6548 /* And store it in the struct so that we can put it in the hash table
6550 h
->elf_hash_value
= ha
;