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 /* The shared libraries distributed with hpux11 have a bogus
959 sh_link field for the ".dynamic" section. This code detects
960 when LINK refers to a section that is not a string table and
961 tries to find the string table for the ".dynsym" section
963 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[link
];
964 if (hdr
->sh_type
!= SHT_STRTAB
)
966 asection
*s
= bfd_get_section_by_name (abfd
, ".dynsym");
967 int elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
970 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
975 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
976 for (; extdyn
< extdynend
; extdyn
++)
978 Elf_Internal_Dyn dyn
;
980 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
981 if (dyn
.d_tag
== DT_SONAME
)
983 name
= bfd_elf_string_from_elf_section (abfd
, link
,
988 if (dyn
.d_tag
== DT_NEEDED
)
990 struct bfd_link_needed_list
*n
, **pn
;
993 n
= ((struct bfd_link_needed_list
*)
994 bfd_alloc (abfd
, sizeof (struct bfd_link_needed_list
)));
995 fnm
= bfd_elf_string_from_elf_section (abfd
, link
,
997 if (n
== NULL
|| fnm
== NULL
)
999 anm
= bfd_alloc (abfd
, strlen (fnm
) + 1);
1006 for (pn
= &elf_hash_table (info
)->needed
;
1018 /* We do not want to include any of the sections in a dynamic
1019 object in the output file. We hack by simply clobbering the
1020 list of sections in the BFD. This could be handled more
1021 cleanly by, say, a new section flag; the existing
1022 SEC_NEVER_LOAD flag is not the one we want, because that one
1023 still implies that the section takes up space in the output
1025 abfd
->sections
= NULL
;
1026 abfd
->section_count
= 0;
1028 /* If this is the first dynamic object found in the link, create
1029 the special sections required for dynamic linking. */
1030 if (! elf_hash_table (info
)->dynamic_sections_created
)
1032 if (! elf_link_create_dynamic_sections (abfd
, info
))
1038 /* Add a DT_NEEDED entry for this dynamic object. */
1039 oldsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
1040 strindex
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, name
,
1042 if (strindex
== (bfd_size_type
) -1)
1045 if (oldsize
== _bfd_stringtab_size (elf_hash_table (info
)->dynstr
))
1048 Elf_External_Dyn
*dyncon
, *dynconend
;
1050 /* The hash table size did not change, which means that
1051 the dynamic object name was already entered. If we
1052 have already included this dynamic object in the
1053 link, just ignore it. There is no reason to include
1054 a particular dynamic object more than once. */
1055 sdyn
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
1057 BFD_ASSERT (sdyn
!= NULL
);
1059 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
1060 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
1062 for (; dyncon
< dynconend
; dyncon
++)
1064 Elf_Internal_Dyn dyn
;
1066 elf_swap_dyn_in (elf_hash_table (info
)->dynobj
, dyncon
,
1068 if (dyn
.d_tag
== DT_NEEDED
1069 && dyn
.d_un
.d_val
== strindex
)
1073 if (extversym
!= NULL
)
1080 if (! elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
1084 /* Save the SONAME, if there is one, because sometimes the
1085 linker emulation code will need to know it. */
1087 name
= bfd_get_filename (abfd
);
1088 elf_dt_name (abfd
) = name
;
1092 hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
),
1094 || (bfd_read ((PTR
) buf
, sizeof (Elf_External_Sym
), extsymcount
, abfd
)
1095 != extsymcount
* sizeof (Elf_External_Sym
)))
1100 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
1101 esymend
= buf
+ extsymcount
;
1104 esym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
1106 Elf_Internal_Sym sym
;
1112 struct elf_link_hash_entry
*h
;
1114 boolean size_change_ok
, type_change_ok
;
1115 boolean new_weakdef
;
1116 unsigned int old_alignment
;
1118 elf_swap_symbol_in (abfd
, esym
, &sym
);
1120 flags
= BSF_NO_FLAGS
;
1122 value
= sym
.st_value
;
1125 bind
= ELF_ST_BIND (sym
.st_info
);
1126 if (bind
== STB_LOCAL
)
1128 /* This should be impossible, since ELF requires that all
1129 global symbols follow all local symbols, and that sh_info
1130 point to the first global symbol. Unfortunatealy, Irix 5
1134 else if (bind
== STB_GLOBAL
)
1136 if (sym
.st_shndx
!= SHN_UNDEF
1137 && sym
.st_shndx
!= SHN_COMMON
)
1142 else if (bind
== STB_WEAK
)
1146 /* Leave it up to the processor backend. */
1149 if (sym
.st_shndx
== SHN_UNDEF
)
1150 sec
= bfd_und_section_ptr
;
1151 else if (sym
.st_shndx
> 0 && sym
.st_shndx
< SHN_LORESERVE
)
1153 sec
= section_from_elf_index (abfd
, sym
.st_shndx
);
1155 sec
= bfd_abs_section_ptr
;
1156 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
1159 else if (sym
.st_shndx
== SHN_ABS
)
1160 sec
= bfd_abs_section_ptr
;
1161 else if (sym
.st_shndx
== SHN_COMMON
)
1163 sec
= bfd_com_section_ptr
;
1164 /* What ELF calls the size we call the value. What ELF
1165 calls the value we call the alignment. */
1166 value
= sym
.st_size
;
1170 /* Leave it up to the processor backend. */
1173 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
1174 if (name
== (const char *) NULL
)
1177 if (add_symbol_hook
)
1179 if (! (*add_symbol_hook
) (abfd
, info
, &sym
, &name
, &flags
, &sec
,
1183 /* The hook function sets the name to NULL if this symbol
1184 should be skipped for some reason. */
1185 if (name
== (const char *) NULL
)
1189 /* Sanity check that all possibilities were handled. */
1190 if (sec
== (asection
*) NULL
)
1192 bfd_set_error (bfd_error_bad_value
);
1196 if (bfd_is_und_section (sec
)
1197 || bfd_is_com_section (sec
))
1202 size_change_ok
= false;
1203 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
1205 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1207 Elf_Internal_Versym iver
;
1208 unsigned int vernum
= 0;
1213 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
1214 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
1216 /* If this is a hidden symbol, or if it is not version
1217 1, we append the version name to the symbol name.
1218 However, we do not modify a non-hidden absolute
1219 symbol, because it might be the version symbol
1220 itself. FIXME: What if it isn't? */
1221 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
1222 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
1225 int namelen
, newlen
;
1228 if (sym
.st_shndx
!= SHN_UNDEF
)
1230 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
1232 (*_bfd_error_handler
)
1233 (_("%s: %s: invalid version %u (max %d)"),
1234 bfd_get_filename (abfd
), name
, vernum
,
1235 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1236 bfd_set_error (bfd_error_bad_value
);
1239 else if (vernum
> 1)
1241 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1247 /* We cannot simply test for the number of
1248 entries in the VERNEED section since the
1249 numbers for the needed versions do not start
1251 Elf_Internal_Verneed
*t
;
1254 for (t
= elf_tdata (abfd
)->verref
;
1258 Elf_Internal_Vernaux
*a
;
1260 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1262 if (a
->vna_other
== vernum
)
1264 verstr
= a
->vna_nodename
;
1273 (*_bfd_error_handler
)
1274 (_("%s: %s: invalid needed version %d"),
1275 bfd_get_filename (abfd
), name
, vernum
);
1276 bfd_set_error (bfd_error_bad_value
);
1281 namelen
= strlen (name
);
1282 newlen
= namelen
+ strlen (verstr
) + 2;
1283 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
1286 newname
= (char *) bfd_alloc (abfd
, newlen
);
1287 if (newname
== NULL
)
1289 strcpy (newname
, name
);
1290 p
= newname
+ namelen
;
1292 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
1300 if (! elf_merge_symbol (abfd
, info
, name
, &sym
, &sec
, &value
,
1301 sym_hash
, &override
, &type_change_ok
,
1309 while (h
->root
.type
== bfd_link_hash_indirect
1310 || h
->root
.type
== bfd_link_hash_warning
)
1311 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1313 /* Remember the old alignment if this is a common symbol, so
1314 that we don't reduce the alignment later on. We can't
1315 check later, because _bfd_generic_link_add_one_symbol
1316 will set a default for the alignment which we want to
1318 if (h
->root
.type
== bfd_link_hash_common
)
1319 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1321 if (elf_tdata (abfd
)->verdef
!= NULL
1325 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1328 if (! (_bfd_generic_link_add_one_symbol
1329 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
1330 false, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
1334 while (h
->root
.type
== bfd_link_hash_indirect
1335 || h
->root
.type
== bfd_link_hash_warning
)
1336 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1339 new_weakdef
= false;
1342 && (flags
& BSF_WEAK
) != 0
1343 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
1344 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1345 && h
->weakdef
== NULL
)
1347 /* Keep a list of all weak defined non function symbols from
1348 a dynamic object, using the weakdef field. Later in this
1349 function we will set the weakdef field to the correct
1350 value. We only put non-function symbols from dynamic
1351 objects on this list, because that happens to be the only
1352 time we need to know the normal symbol corresponding to a
1353 weak symbol, and the information is time consuming to
1354 figure out. If the weakdef field is not already NULL,
1355 then this symbol was already defined by some previous
1356 dynamic object, and we will be using that previous
1357 definition anyhow. */
1364 /* Set the alignment of a common symbol. */
1365 if (sym
.st_shndx
== SHN_COMMON
1366 && h
->root
.type
== bfd_link_hash_common
)
1370 align
= bfd_log2 (sym
.st_value
);
1371 if (align
> old_alignment
)
1372 h
->root
.u
.c
.p
->alignment_power
= align
;
1375 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1381 /* Remember the symbol size and type. */
1382 if (sym
.st_size
!= 0
1383 && (definition
|| h
->size
== 0))
1385 if (h
->size
!= 0 && h
->size
!= sym
.st_size
&& ! size_change_ok
)
1386 (*_bfd_error_handler
)
1387 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1388 name
, (unsigned long) h
->size
, (unsigned long) sym
.st_size
,
1389 bfd_get_filename (abfd
));
1391 h
->size
= sym
.st_size
;
1394 /* If this is a common symbol, then we always want H->SIZE
1395 to be the size of the common symbol. The code just above
1396 won't fix the size if a common symbol becomes larger. We
1397 don't warn about a size change here, because that is
1398 covered by --warn-common. */
1399 if (h
->root
.type
== bfd_link_hash_common
)
1400 h
->size
= h
->root
.u
.c
.size
;
1402 if (ELF_ST_TYPE (sym
.st_info
) != STT_NOTYPE
1403 && (definition
|| h
->type
== STT_NOTYPE
))
1405 if (h
->type
!= STT_NOTYPE
1406 && h
->type
!= ELF_ST_TYPE (sym
.st_info
)
1407 && ! type_change_ok
)
1408 (*_bfd_error_handler
)
1409 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1410 name
, h
->type
, ELF_ST_TYPE (sym
.st_info
),
1411 bfd_get_filename (abfd
));
1413 h
->type
= ELF_ST_TYPE (sym
.st_info
);
1416 if (sym
.st_other
!= 0
1417 && (definition
|| h
->other
== 0))
1418 h
->other
= sym
.st_other
;
1420 /* Set a flag in the hash table entry indicating the type of
1421 reference or definition we just found. Keep a count of
1422 the number of dynamic symbols we find. A dynamic symbol
1423 is one which is referenced or defined by both a regular
1424 object and a shared object. */
1425 old_flags
= h
->elf_link_hash_flags
;
1431 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
1432 if (bind
!= STB_WEAK
)
1433 new_flag
|= ELF_LINK_HASH_REF_REGULAR_NONWEAK
;
1436 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
1438 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1439 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
1445 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
1447 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
1448 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
1449 | ELF_LINK_HASH_REF_REGULAR
)) != 0
1450 || (h
->weakdef
!= NULL
1452 && h
->weakdef
->dynindx
!= -1))
1456 h
->elf_link_hash_flags
|= new_flag
;
1458 /* If this symbol has a version, and it is the default
1459 version, we create an indirect symbol from the default
1460 name to the fully decorated name. This will cause
1461 external references which do not specify a version to be
1462 bound to this version of the symbol. */
1467 p
= strchr (name
, ELF_VER_CHR
);
1468 if (p
!= NULL
&& p
[1] == ELF_VER_CHR
)
1471 struct elf_link_hash_entry
*hi
;
1474 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1476 if (shortname
== NULL
)
1478 strncpy (shortname
, name
, p
- name
);
1479 shortname
[p
- name
] = '\0';
1481 /* We are going to create a new symbol. Merge it
1482 with any existing symbol with this name. For the
1483 purposes of the merge, act as though we were
1484 defining the symbol we just defined, although we
1485 actually going to define an indirect symbol. */
1486 type_change_ok
= false;
1487 size_change_ok
= false;
1488 if (! elf_merge_symbol (abfd
, info
, shortname
, &sym
, &sec
,
1489 &value
, &hi
, &override
,
1490 &type_change_ok
, &size_change_ok
))
1495 if (! (_bfd_generic_link_add_one_symbol
1496 (info
, abfd
, shortname
, BSF_INDIRECT
,
1497 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1498 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1503 /* In this case the symbol named SHORTNAME is
1504 overriding the indirect symbol we want to
1505 add. We were planning on making SHORTNAME an
1506 indirect symbol referring to NAME. SHORTNAME
1507 is the name without a version. NAME is the
1508 fully versioned name, and it is the default
1511 Overriding means that we already saw a
1512 definition for the symbol SHORTNAME in a
1513 regular object, and it is overriding the
1514 symbol defined in the dynamic object.
1516 When this happens, we actually want to change
1517 NAME, the symbol we just added, to refer to
1518 SHORTNAME. This will cause references to
1519 NAME in the shared object to become
1520 references to SHORTNAME in the regular
1521 object. This is what we expect when we
1522 override a function in a shared object: that
1523 the references in the shared object will be
1524 mapped to the definition in the regular
1527 while (hi
->root
.type
== bfd_link_hash_indirect
1528 || hi
->root
.type
== bfd_link_hash_warning
)
1529 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1531 h
->root
.type
= bfd_link_hash_indirect
;
1532 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1533 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1535 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1536 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1537 if (hi
->elf_link_hash_flags
1538 & (ELF_LINK_HASH_REF_REGULAR
1539 | ELF_LINK_HASH_DEF_REGULAR
))
1541 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1547 /* Now set HI to H, so that the following code
1548 will set the other fields correctly. */
1552 /* If there is a duplicate definition somewhere,
1553 then HI may not point to an indirect symbol. We
1554 will have reported an error to the user in that
1557 if (hi
->root
.type
== bfd_link_hash_indirect
)
1559 struct elf_link_hash_entry
*ht
;
1561 /* If the symbol became indirect, then we assume
1562 that we have not seen a definition before. */
1563 BFD_ASSERT ((hi
->elf_link_hash_flags
1564 & (ELF_LINK_HASH_DEF_DYNAMIC
1565 | ELF_LINK_HASH_DEF_REGULAR
))
1568 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1570 /* Copy down any references that we may have
1571 already seen to the symbol which just became
1573 ht
->elf_link_hash_flags
|=
1574 (hi
->elf_link_hash_flags
1575 & (ELF_LINK_HASH_REF_DYNAMIC
1576 | ELF_LINK_HASH_REF_REGULAR
1577 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
));
1579 /* Copy over the global and procedure linkage table
1580 offset entries. These may have been already set
1581 up by a check_relocs routine. */
1582 if (ht
->got
.offset
== (bfd_vma
) -1)
1584 ht
->got
.offset
= hi
->got
.offset
;
1585 hi
->got
.offset
= (bfd_vma
) -1;
1587 BFD_ASSERT (hi
->got
.offset
== (bfd_vma
) -1);
1589 if (ht
->plt
.offset
== (bfd_vma
) -1)
1591 ht
->plt
.offset
= hi
->plt
.offset
;
1592 hi
->plt
.offset
= (bfd_vma
) -1;
1594 BFD_ASSERT (hi
->plt
.offset
== (bfd_vma
) -1);
1596 if (ht
->dynindx
== -1)
1598 ht
->dynindx
= hi
->dynindx
;
1599 ht
->dynstr_index
= hi
->dynstr_index
;
1601 hi
->dynstr_index
= 0;
1603 BFD_ASSERT (hi
->dynindx
== -1);
1605 /* FIXME: There may be other information to copy
1606 over for particular targets. */
1608 /* See if the new flags lead us to realize that
1609 the symbol must be dynamic. */
1615 || ((hi
->elf_link_hash_flags
1616 & ELF_LINK_HASH_REF_DYNAMIC
)
1622 if ((hi
->elf_link_hash_flags
1623 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1629 /* We also need to define an indirection from the
1630 nondefault version of the symbol. */
1632 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1634 if (shortname
== NULL
)
1636 strncpy (shortname
, name
, p
- name
);
1637 strcpy (shortname
+ (p
- name
), p
+ 1);
1639 /* Once again, merge with any existing symbol. */
1640 type_change_ok
= false;
1641 size_change_ok
= false;
1642 if (! elf_merge_symbol (abfd
, info
, shortname
, &sym
, &sec
,
1643 &value
, &hi
, &override
,
1644 &type_change_ok
, &size_change_ok
))
1649 /* Here SHORTNAME is a versioned name, so we
1650 don't expect to see the type of override we
1651 do in the case above. */
1652 (*_bfd_error_handler
)
1653 (_("%s: warning: unexpected redefinition of `%s'"),
1654 bfd_get_filename (abfd
), shortname
);
1658 if (! (_bfd_generic_link_add_one_symbol
1659 (info
, abfd
, shortname
, BSF_INDIRECT
,
1660 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1661 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1664 /* If there is a duplicate definition somewhere,
1665 then HI may not point to an indirect symbol.
1666 We will have reported an error to the user in
1669 if (hi
->root
.type
== bfd_link_hash_indirect
)
1671 /* If the symbol became indirect, then we
1672 assume that we have not seen a definition
1674 BFD_ASSERT ((hi
->elf_link_hash_flags
1675 & (ELF_LINK_HASH_DEF_DYNAMIC
1676 | ELF_LINK_HASH_DEF_REGULAR
))
1679 /* Copy down any references that we may have
1680 already seen to the symbol which just
1682 h
->elf_link_hash_flags
|=
1683 (hi
->elf_link_hash_flags
1684 & (ELF_LINK_HASH_REF_DYNAMIC
1685 | ELF_LINK_HASH_REF_REGULAR
1686 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
));
1688 /* Copy over the global and procedure linkage
1689 table offset entries. These may have been
1690 already set up by a check_relocs routine. */
1691 if (h
->got
.offset
== (bfd_vma
) -1)
1693 h
->got
.offset
= hi
->got
.offset
;
1694 hi
->got
.offset
= (bfd_vma
) -1;
1696 BFD_ASSERT (hi
->got
.offset
== (bfd_vma
) -1);
1698 if (h
->plt
.offset
== (bfd_vma
) -1)
1700 h
->plt
.offset
= hi
->plt
.offset
;
1701 hi
->plt
.offset
= (bfd_vma
) -1;
1703 BFD_ASSERT (hi
->got
.offset
== (bfd_vma
) -1);
1705 if (h
->dynindx
== -1)
1707 h
->dynindx
= hi
->dynindx
;
1708 h
->dynstr_index
= hi
->dynstr_index
;
1710 hi
->dynstr_index
= 0;
1712 BFD_ASSERT (hi
->dynindx
== -1);
1714 /* FIXME: There may be other information to
1715 copy over for particular targets. */
1717 /* See if the new flags lead us to realize
1718 that the symbol must be dynamic. */
1724 || ((hi
->elf_link_hash_flags
1725 & ELF_LINK_HASH_REF_DYNAMIC
)
1731 if ((hi
->elf_link_hash_flags
1732 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1741 if (dynsym
&& h
->dynindx
== -1)
1743 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1745 if (h
->weakdef
!= NULL
1747 && h
->weakdef
->dynindx
== -1)
1749 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1757 /* Now set the weakdefs field correctly for all the weak defined
1758 symbols we found. The only way to do this is to search all the
1759 symbols. Since we only need the information for non functions in
1760 dynamic objects, that's the only time we actually put anything on
1761 the list WEAKS. We need this information so that if a regular
1762 object refers to a symbol defined weakly in a dynamic object, the
1763 real symbol in the dynamic object is also put in the dynamic
1764 symbols; we also must arrange for both symbols to point to the
1765 same memory location. We could handle the general case of symbol
1766 aliasing, but a general symbol alias can only be generated in
1767 assembler code, handling it correctly would be very time
1768 consuming, and other ELF linkers don't handle general aliasing
1770 while (weaks
!= NULL
)
1772 struct elf_link_hash_entry
*hlook
;
1775 struct elf_link_hash_entry
**hpp
;
1776 struct elf_link_hash_entry
**hppend
;
1779 weaks
= hlook
->weakdef
;
1780 hlook
->weakdef
= NULL
;
1782 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
1783 || hlook
->root
.type
== bfd_link_hash_defweak
1784 || hlook
->root
.type
== bfd_link_hash_common
1785 || hlook
->root
.type
== bfd_link_hash_indirect
);
1786 slook
= hlook
->root
.u
.def
.section
;
1787 vlook
= hlook
->root
.u
.def
.value
;
1789 hpp
= elf_sym_hashes (abfd
);
1790 hppend
= hpp
+ extsymcount
;
1791 for (; hpp
< hppend
; hpp
++)
1793 struct elf_link_hash_entry
*h
;
1796 if (h
!= NULL
&& h
!= hlook
1797 && h
->root
.type
== bfd_link_hash_defined
1798 && h
->root
.u
.def
.section
== slook
1799 && h
->root
.u
.def
.value
== vlook
)
1803 /* If the weak definition is in the list of dynamic
1804 symbols, make sure the real definition is put there
1806 if (hlook
->dynindx
!= -1
1807 && h
->dynindx
== -1)
1809 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1813 /* If the real definition is in the list of dynamic
1814 symbols, make sure the weak definition is put there
1815 as well. If we don't do this, then the dynamic
1816 loader might not merge the entries for the real
1817 definition and the weak definition. */
1818 if (h
->dynindx
!= -1
1819 && hlook
->dynindx
== -1)
1821 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
1836 if (extversym
!= NULL
)
1842 /* If this object is the same format as the output object, and it is
1843 not a shared library, then let the backend look through the
1846 This is required to build global offset table entries and to
1847 arrange for dynamic relocs. It is not required for the
1848 particular common case of linking non PIC code, even when linking
1849 against shared libraries, but unfortunately there is no way of
1850 knowing whether an object file has been compiled PIC or not.
1851 Looking through the relocs is not particularly time consuming.
1852 The problem is that we must either (1) keep the relocs in memory,
1853 which causes the linker to require additional runtime memory or
1854 (2) read the relocs twice from the input file, which wastes time.
1855 This would be a good case for using mmap.
1857 I have no idea how to handle linking PIC code into a file of a
1858 different format. It probably can't be done. */
1859 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
1861 && abfd
->xvec
== info
->hash
->creator
1862 && check_relocs
!= NULL
)
1866 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1868 Elf_Internal_Rela
*internal_relocs
;
1871 if ((o
->flags
& SEC_RELOC
) == 0
1872 || o
->reloc_count
== 0
1873 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
1874 && (o
->flags
& SEC_DEBUGGING
) != 0)
1875 || bfd_is_abs_section (o
->output_section
))
1878 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
1879 (abfd
, o
, (PTR
) NULL
,
1880 (Elf_Internal_Rela
*) NULL
,
1881 info
->keep_memory
));
1882 if (internal_relocs
== NULL
)
1885 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
1887 if (! info
->keep_memory
)
1888 free (internal_relocs
);
1895 /* If this is a non-traditional, non-relocateable link, try to
1896 optimize the handling of the .stab/.stabstr sections. */
1898 && ! info
->relocateable
1899 && ! info
->traditional_format
1900 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1901 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
1903 asection
*stab
, *stabstr
;
1905 stab
= bfd_get_section_by_name (abfd
, ".stab");
1908 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
1910 if (stabstr
!= NULL
)
1912 struct bfd_elf_section_data
*secdata
;
1914 secdata
= elf_section_data (stab
);
1915 if (! _bfd_link_section_stabs (abfd
,
1916 &elf_hash_table (info
)->stab_info
,
1918 &secdata
->stab_info
))
1933 if (extversym
!= NULL
)
1938 /* Create some sections which will be filled in with dynamic linking
1939 information. ABFD is an input file which requires dynamic sections
1940 to be created. The dynamic sections take up virtual memory space
1941 when the final executable is run, so we need to create them before
1942 addresses are assigned to the output sections. We work out the
1943 actual contents and size of these sections later. */
1946 elf_link_create_dynamic_sections (abfd
, info
)
1948 struct bfd_link_info
*info
;
1951 register asection
*s
;
1952 struct elf_link_hash_entry
*h
;
1953 struct elf_backend_data
*bed
;
1955 if (elf_hash_table (info
)->dynamic_sections_created
)
1958 /* Make sure that all dynamic sections use the same input BFD. */
1959 if (elf_hash_table (info
)->dynobj
== NULL
)
1960 elf_hash_table (info
)->dynobj
= abfd
;
1962 abfd
= elf_hash_table (info
)->dynobj
;
1964 /* Note that we set the SEC_IN_MEMORY flag for all of these
1966 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1967 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
1969 /* A dynamically linked executable has a .interp section, but a
1970 shared library does not. */
1973 s
= bfd_make_section (abfd
, ".interp");
1975 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
1979 /* Create sections to hold version informations. These are removed
1980 if they are not needed. */
1981 s
= bfd_make_section (abfd
, ".gnu.version_d");
1983 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1984 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1987 s
= bfd_make_section (abfd
, ".gnu.version");
1989 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1990 || ! bfd_set_section_alignment (abfd
, s
, 1))
1993 s
= bfd_make_section (abfd
, ".gnu.version_r");
1995 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1996 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1999 s
= bfd_make_section (abfd
, ".dynsym");
2001 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2002 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2005 s
= bfd_make_section (abfd
, ".dynstr");
2007 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
2010 /* Create a strtab to hold the dynamic symbol names. */
2011 if (elf_hash_table (info
)->dynstr
== NULL
)
2013 elf_hash_table (info
)->dynstr
= elf_stringtab_init ();
2014 if (elf_hash_table (info
)->dynstr
== NULL
)
2018 s
= bfd_make_section (abfd
, ".dynamic");
2020 || ! bfd_set_section_flags (abfd
, s
, flags
)
2021 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2024 /* The special symbol _DYNAMIC is always set to the start of the
2025 .dynamic section. This call occurs before we have processed the
2026 symbols for any dynamic object, so we don't have to worry about
2027 overriding a dynamic definition. We could set _DYNAMIC in a
2028 linker script, but we only want to define it if we are, in fact,
2029 creating a .dynamic section. We don't want to define it if there
2030 is no .dynamic section, since on some ELF platforms the start up
2031 code examines it to decide how to initialize the process. */
2033 if (! (_bfd_generic_link_add_one_symbol
2034 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
2035 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
2036 (struct bfd_link_hash_entry
**) &h
)))
2038 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2039 h
->type
= STT_OBJECT
;
2042 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2045 bed
= get_elf_backend_data (abfd
);
2047 s
= bfd_make_section (abfd
, ".hash");
2049 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
2050 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
2052 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
2054 /* Let the backend create the rest of the sections. This lets the
2055 backend set the right flags. The backend will normally create
2056 the .got and .plt sections. */
2057 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
2060 elf_hash_table (info
)->dynamic_sections_created
= true;
2065 /* Add an entry to the .dynamic table. */
2068 elf_add_dynamic_entry (info
, tag
, val
)
2069 struct bfd_link_info
*info
;
2073 Elf_Internal_Dyn dyn
;
2077 bfd_byte
*newcontents
;
2079 dynobj
= elf_hash_table (info
)->dynobj
;
2081 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
2082 BFD_ASSERT (s
!= NULL
);
2084 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
2085 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
2086 if (newcontents
== NULL
)
2090 dyn
.d_un
.d_val
= val
;
2091 elf_swap_dyn_out (dynobj
, &dyn
,
2092 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
2094 s
->_raw_size
= newsize
;
2095 s
->contents
= newcontents
;
2100 /* Record a new local dynamic symbol. */
2103 elf_link_record_local_dynamic_symbol (info
, input_bfd
, input_indx
)
2104 struct bfd_link_info
*info
;
2108 struct elf_link_local_dynamic_entry
*entry
;
2109 struct elf_link_hash_table
*eht
;
2110 struct bfd_strtab_hash
*dynstr
;
2111 Elf_External_Sym esym
;
2112 unsigned long dynstr_index
;
2115 /* See if the entry exists already. */
2116 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
2117 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
2120 entry
= (struct elf_link_local_dynamic_entry
*)
2121 bfd_alloc (input_bfd
, sizeof (*entry
));
2125 /* Go find the symbol, so that we can find it's name. */
2126 if (bfd_seek (input_bfd
,
2127 (elf_tdata (input_bfd
)->symtab_hdr
.sh_offset
2128 + input_indx
* sizeof (Elf_External_Sym
)),
2130 || (bfd_read (&esym
, sizeof (Elf_External_Sym
), 1, input_bfd
)
2131 != sizeof (Elf_External_Sym
)))
2133 elf_swap_symbol_in (input_bfd
, &esym
, &entry
->isym
);
2135 name
= (bfd_elf_string_from_elf_section
2136 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
2137 entry
->isym
.st_name
));
2139 dynstr
= elf_hash_table (info
)->dynstr
;
2142 /* Create a strtab to hold the dynamic symbol names. */
2143 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_stringtab_init ();
2148 dynstr_index
= _bfd_stringtab_add (dynstr
, name
, true, false);
2149 if (dynstr_index
== (unsigned long) -1)
2151 entry
->isym
.st_name
= dynstr_index
;
2153 eht
= elf_hash_table (info
);
2155 entry
->next
= eht
->dynlocal
;
2156 eht
->dynlocal
= entry
;
2157 entry
->input_bfd
= input_bfd
;
2158 entry
->input_indx
= input_indx
;
2161 /* Whatever binding the symbol had before, it's now local. */
2163 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
2165 /* The dynindx will be set at the end of size_dynamic_sections. */
2171 /* Read and swap the relocs from the section indicated by SHDR. This
2172 may be either a REL or a RELA section. The relocations are
2173 translated into RELA relocations and stored in INTERNAL_RELOCS,
2174 which should have already been allocated to contain enough space.
2175 The EXTERNAL_RELOCS are a buffer where the external form of the
2176 relocations should be stored.
2178 Returns false if something goes wrong. */
2181 elf_link_read_relocs_from_section (abfd
, shdr
, external_relocs
,
2184 Elf_Internal_Shdr
*shdr
;
2185 PTR external_relocs
;
2186 Elf_Internal_Rela
*internal_relocs
;
2188 struct elf_backend_data
*bed
;
2190 /* If there aren't any relocations, that's OK. */
2194 /* Position ourselves at the start of the section. */
2195 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2198 /* Read the relocations. */
2199 if (bfd_read (external_relocs
, 1, shdr
->sh_size
, abfd
)
2203 bed
= get_elf_backend_data (abfd
);
2205 /* Convert the external relocations to the internal format. */
2206 if (shdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2208 Elf_External_Rel
*erel
;
2209 Elf_External_Rel
*erelend
;
2210 Elf_Internal_Rela
*irela
;
2211 Elf_Internal_Rel
*irel
;
2213 erel
= (Elf_External_Rel
*) external_relocs
;
2214 erelend
= erel
+ shdr
->sh_size
/ shdr
->sh_entsize
;
2215 irela
= internal_relocs
;
2216 irel
= bfd_alloc (abfd
, (bed
->s
->int_rels_per_ext_rel
2217 * sizeof (Elf_Internal_Rel
)));
2218 for (; erel
< erelend
; erel
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2222 if (bed
->s
->swap_reloc_in
)
2223 (*bed
->s
->swap_reloc_in
) (abfd
, (bfd_byte
*) erel
, irel
);
2225 elf_swap_reloc_in (abfd
, erel
, irel
);
2227 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; ++i
)
2229 irela
[i
].r_offset
= irel
[i
].r_offset
;
2230 irela
[i
].r_info
= irel
[i
].r_info
;
2231 irela
[i
].r_addend
= 0;
2237 Elf_External_Rela
*erela
;
2238 Elf_External_Rela
*erelaend
;
2239 Elf_Internal_Rela
*irela
;
2241 BFD_ASSERT (shdr
->sh_entsize
== sizeof (Elf_External_Rela
));
2243 erela
= (Elf_External_Rela
*) external_relocs
;
2244 erelaend
= erela
+ shdr
->sh_size
/ shdr
->sh_entsize
;
2245 irela
= internal_relocs
;
2246 for (; erela
< erelaend
; erela
++, irela
+= bed
->s
->int_rels_per_ext_rel
)
2248 if (bed
->s
->swap_reloca_in
)
2249 (*bed
->s
->swap_reloca_in
) (abfd
, (bfd_byte
*) erela
, irela
);
2251 elf_swap_reloca_in (abfd
, erela
, irela
);
2258 /* Read and swap the relocs for a section O. They may have been
2259 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2260 not NULL, they are used as buffers to read into. They are known to
2261 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2262 the return value is allocated using either malloc or bfd_alloc,
2263 according to the KEEP_MEMORY argument. If O has two relocation
2264 sections (both REL and RELA relocations), then the REL_HDR
2265 relocations will appear first in INTERNAL_RELOCS, followed by the
2266 REL_HDR2 relocations. */
2269 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
2273 PTR external_relocs
;
2274 Elf_Internal_Rela
*internal_relocs
;
2275 boolean keep_memory
;
2277 Elf_Internal_Shdr
*rel_hdr
;
2279 Elf_Internal_Rela
*alloc2
= NULL
;
2280 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2282 if (elf_section_data (o
)->relocs
!= NULL
)
2283 return elf_section_data (o
)->relocs
;
2285 if (o
->reloc_count
== 0)
2288 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2290 if (internal_relocs
== NULL
)
2294 size
= (o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
2295 * sizeof (Elf_Internal_Rela
));
2297 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2299 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2300 if (internal_relocs
== NULL
)
2304 if (external_relocs
== NULL
)
2306 size_t size
= (size_t) rel_hdr
->sh_size
;
2308 if (elf_section_data (o
)->rel_hdr2
)
2309 size
+= (size_t) elf_section_data (o
)->rel_hdr2
->sh_size
;
2310 alloc1
= (PTR
) bfd_malloc (size
);
2313 external_relocs
= alloc1
;
2316 if (!elf_link_read_relocs_from_section (abfd
, rel_hdr
,
2320 if (!elf_link_read_relocs_from_section
2322 elf_section_data (o
)->rel_hdr2
,
2323 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2324 internal_relocs
+ (rel_hdr
->sh_size
/ rel_hdr
->sh_entsize
2325 * bed
->s
->int_rels_per_ext_rel
)))
2328 /* Cache the results for next time, if we can. */
2330 elf_section_data (o
)->relocs
= internal_relocs
;
2335 /* Don't free alloc2, since if it was allocated we are passing it
2336 back (under the name of internal_relocs). */
2338 return internal_relocs
;
2349 /* Record an assignment to a symbol made by a linker script. We need
2350 this in case some dynamic object refers to this symbol. */
2354 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
2355 bfd
*output_bfd ATTRIBUTE_UNUSED
;
2356 struct bfd_link_info
*info
;
2360 struct elf_link_hash_entry
*h
;
2362 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2365 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, true, false);
2369 if (h
->root
.type
== bfd_link_hash_new
)
2370 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
2372 /* If this symbol is being provided by the linker script, and it is
2373 currently defined by a dynamic object, but not by a regular
2374 object, then mark it as undefined so that the generic linker will
2375 force the correct value. */
2377 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2378 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2379 h
->root
.type
= bfd_link_hash_undefined
;
2381 /* If this symbol is not being provided by the linker script, and it is
2382 currently defined by a dynamic object, but not by a regular object,
2383 then clear out any version information because the symbol will not be
2384 associated with the dynamic object any more. */
2386 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2387 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2388 h
->verinfo
.verdef
= NULL
;
2390 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2392 /* When possible, keep the original type of the symbol */
2393 if (h
->type
== STT_NOTYPE
)
2394 h
->type
= STT_OBJECT
;
2396 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2397 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
2399 && h
->dynindx
== -1)
2401 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2404 /* If this is a weak defined symbol, and we know a corresponding
2405 real symbol from the same dynamic object, make sure the real
2406 symbol is also made into a dynamic symbol. */
2407 if (h
->weakdef
!= NULL
2408 && h
->weakdef
->dynindx
== -1)
2410 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2418 /* This structure is used to pass information to
2419 elf_link_assign_sym_version. */
2421 struct elf_assign_sym_version_info
2425 /* General link information. */
2426 struct bfd_link_info
*info
;
2428 struct bfd_elf_version_tree
*verdefs
;
2429 /* Whether we are exporting all dynamic symbols. */
2430 boolean export_dynamic
;
2431 /* Whether we had a failure. */
2435 /* This structure is used to pass information to
2436 elf_link_find_version_dependencies. */
2438 struct elf_find_verdep_info
2442 /* General link information. */
2443 struct bfd_link_info
*info
;
2444 /* The number of dependencies. */
2446 /* Whether we had a failure. */
2450 /* Array used to determine the number of hash table buckets to use
2451 based on the number of symbols there are. If there are fewer than
2452 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2453 fewer than 37 we use 17 buckets, and so forth. We never use more
2454 than 32771 buckets. */
2456 static const size_t elf_buckets
[] =
2458 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2462 /* Compute bucket count for hashing table. We do not use a static set
2463 of possible tables sizes anymore. Instead we determine for all
2464 possible reasonable sizes of the table the outcome (i.e., the
2465 number of collisions etc) and choose the best solution. The
2466 weighting functions are not too simple to allow the table to grow
2467 without bounds. Instead one of the weighting factors is the size.
2468 Therefore the result is always a good payoff between few collisions
2469 (= short chain lengths) and table size. */
2471 compute_bucket_count (info
)
2472 struct bfd_link_info
*info
;
2474 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2475 size_t best_size
= 0;
2476 unsigned long int *hashcodes
;
2477 unsigned long int *hashcodesp
;
2478 unsigned long int i
;
2480 /* Compute the hash values for all exported symbols. At the same
2481 time store the values in an array so that we could use them for
2483 hashcodes
= (unsigned long int *) bfd_malloc (dynsymcount
2484 * sizeof (unsigned long int));
2485 if (hashcodes
== NULL
)
2487 hashcodesp
= hashcodes
;
2489 /* Put all hash values in HASHCODES. */
2490 elf_link_hash_traverse (elf_hash_table (info
),
2491 elf_collect_hash_codes
, &hashcodesp
);
2493 /* We have a problem here. The following code to optimize the table
2494 size requires an integer type with more the 32 bits. If
2495 BFD_HOST_U_64_BIT is set we know about such a type. */
2496 #ifdef BFD_HOST_U_64_BIT
2497 if (info
->optimize
== true)
2499 unsigned long int nsyms
= hashcodesp
- hashcodes
;
2502 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
2503 unsigned long int *counts
;
2505 /* Possible optimization parameters: if we have NSYMS symbols we say
2506 that the hashing table must at least have NSYMS/4 and at most
2508 minsize
= nsyms
/ 4;
2511 best_size
= maxsize
= nsyms
* 2;
2513 /* Create array where we count the collisions in. We must use bfd_malloc
2514 since the size could be large. */
2515 counts
= (unsigned long int *) bfd_malloc (maxsize
2516 * sizeof (unsigned long int));
2523 /* Compute the "optimal" size for the hash table. The criteria is a
2524 minimal chain length. The minor criteria is (of course) the size
2526 for (i
= minsize
; i
< maxsize
; ++i
)
2528 /* Walk through the array of hashcodes and count the collisions. */
2529 BFD_HOST_U_64_BIT max
;
2530 unsigned long int j
;
2531 unsigned long int fact
;
2533 memset (counts
, '\0', i
* sizeof (unsigned long int));
2535 /* Determine how often each hash bucket is used. */
2536 for (j
= 0; j
< nsyms
; ++j
)
2537 ++counts
[hashcodes
[j
] % i
];
2539 /* For the weight function we need some information about the
2540 pagesize on the target. This is information need not be 100%
2541 accurate. Since this information is not available (so far) we
2542 define it here to a reasonable default value. If it is crucial
2543 to have a better value some day simply define this value. */
2544 # ifndef BFD_TARGET_PAGESIZE
2545 # define BFD_TARGET_PAGESIZE (4096)
2548 /* We in any case need 2 + NSYMS entries for the size values and
2550 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
2553 /* Variant 1: optimize for short chains. We add the squares
2554 of all the chain lengths (which favous many small chain
2555 over a few long chains). */
2556 for (j
= 0; j
< i
; ++j
)
2557 max
+= counts
[j
] * counts
[j
];
2559 /* This adds penalties for the overall size of the table. */
2560 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2563 /* Variant 2: Optimize a lot more for small table. Here we
2564 also add squares of the size but we also add penalties for
2565 empty slots (the +1 term). */
2566 for (j
= 0; j
< i
; ++j
)
2567 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
2569 /* The overall size of the table is considered, but not as
2570 strong as in variant 1, where it is squared. */
2571 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2575 /* Compare with current best results. */
2576 if (max
< best_chlen
)
2586 #endif /* defined (BFD_HOST_U_64_BIT) */
2588 /* This is the fallback solution if no 64bit type is available or if we
2589 are not supposed to spend much time on optimizations. We select the
2590 bucket count using a fixed set of numbers. */
2591 for (i
= 0; elf_buckets
[i
] != 0; i
++)
2593 best_size
= elf_buckets
[i
];
2594 if (dynsymcount
< elf_buckets
[i
+ 1])
2599 /* Free the arrays we needed. */
2605 /* Set up the sizes and contents of the ELF dynamic sections. This is
2606 called by the ELF linker emulation before_allocation routine. We
2607 must set the sizes of the sections before the linker sets the
2608 addresses of the various sections. */
2611 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
2612 export_dynamic
, filter_shlib
,
2613 auxiliary_filters
, info
, sinterpptr
,
2618 boolean export_dynamic
;
2619 const char *filter_shlib
;
2620 const char * const *auxiliary_filters
;
2621 struct bfd_link_info
*info
;
2622 asection
**sinterpptr
;
2623 struct bfd_elf_version_tree
*verdefs
;
2625 bfd_size_type soname_indx
;
2627 struct elf_backend_data
*bed
;
2628 struct elf_assign_sym_version_info asvinfo
;
2632 soname_indx
= (bfd_size_type
) -1;
2634 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2637 /* The backend may have to create some sections regardless of whether
2638 we're dynamic or not. */
2639 bed
= get_elf_backend_data (output_bfd
);
2640 if (bed
->elf_backend_always_size_sections
2641 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
2644 dynobj
= elf_hash_table (info
)->dynobj
;
2646 /* If there were no dynamic objects in the link, there is nothing to
2651 /* If we are supposed to export all symbols into the dynamic symbol
2652 table (this is not the normal case), then do so. */
2655 struct elf_info_failed eif
;
2659 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
2665 if (elf_hash_table (info
)->dynamic_sections_created
)
2667 struct elf_info_failed eif
;
2668 struct elf_link_hash_entry
*h
;
2669 bfd_size_type strsize
;
2671 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
2672 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
2676 soname_indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2677 soname
, true, true);
2678 if (soname_indx
== (bfd_size_type
) -1
2679 || ! elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
2685 if (! elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
2693 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, rpath
,
2695 if (indx
== (bfd_size_type
) -1
2696 || ! elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
2700 if (filter_shlib
!= NULL
)
2704 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2705 filter_shlib
, true, true);
2706 if (indx
== (bfd_size_type
) -1
2707 || ! elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
2711 if (auxiliary_filters
!= NULL
)
2713 const char * const *p
;
2715 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
2719 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2721 if (indx
== (bfd_size_type
) -1
2722 || ! elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
2727 /* Attach all the symbols to their version information. */
2728 asvinfo
.output_bfd
= output_bfd
;
2729 asvinfo
.info
= info
;
2730 asvinfo
.verdefs
= verdefs
;
2731 asvinfo
.export_dynamic
= export_dynamic
;
2732 asvinfo
.failed
= false;
2734 elf_link_hash_traverse (elf_hash_table (info
),
2735 elf_link_assign_sym_version
,
2740 /* Find all symbols which were defined in a dynamic object and make
2741 the backend pick a reasonable value for them. */
2744 elf_link_hash_traverse (elf_hash_table (info
),
2745 elf_adjust_dynamic_symbol
,
2750 /* Add some entries to the .dynamic section. We fill in some of the
2751 values later, in elf_bfd_final_link, but we must add the entries
2752 now so that we know the final size of the .dynamic section. */
2754 /* If there are initialization and/or finalization functions to
2755 call then add the corresponding DT_INIT/DT_FINI entries. */
2756 h
= (info
->init_function
2757 ? elf_link_hash_lookup (elf_hash_table (info
),
2758 info
->init_function
, false,
2762 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2763 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2765 if (! elf_add_dynamic_entry (info
, DT_INIT
, 0))
2768 h
= (info
->fini_function
2769 ? elf_link_hash_lookup (elf_hash_table (info
),
2770 info
->fini_function
, false,
2774 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2775 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2777 if (! elf_add_dynamic_entry (info
, DT_FINI
, 0))
2781 strsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
2782 if (! elf_add_dynamic_entry (info
, DT_HASH
, 0)
2783 || ! elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
2784 || ! elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
2785 || ! elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
2786 || ! elf_add_dynamic_entry (info
, DT_SYMENT
,
2787 sizeof (Elf_External_Sym
)))
2791 /* The backend must work out the sizes of all the other dynamic
2793 if (bed
->elf_backend_size_dynamic_sections
2794 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
2797 if (elf_hash_table (info
)->dynamic_sections_created
)
2801 size_t bucketcount
= 0;
2802 Elf_Internal_Sym isym
;
2803 size_t hash_entry_size
;
2805 /* Set up the version definition section. */
2806 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2807 BFD_ASSERT (s
!= NULL
);
2809 /* We may have created additional version definitions if we are
2810 just linking a regular application. */
2811 verdefs
= asvinfo
.verdefs
;
2813 if (verdefs
== NULL
)
2814 _bfd_strip_section_from_output (s
);
2819 struct bfd_elf_version_tree
*t
;
2821 Elf_Internal_Verdef def
;
2822 Elf_Internal_Verdaux defaux
;
2827 /* Make space for the base version. */
2828 size
+= sizeof (Elf_External_Verdef
);
2829 size
+= sizeof (Elf_External_Verdaux
);
2832 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2834 struct bfd_elf_version_deps
*n
;
2836 size
+= sizeof (Elf_External_Verdef
);
2837 size
+= sizeof (Elf_External_Verdaux
);
2840 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2841 size
+= sizeof (Elf_External_Verdaux
);
2844 s
->_raw_size
= size
;
2845 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2846 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2849 /* Fill in the version definition section. */
2853 def
.vd_version
= VER_DEF_CURRENT
;
2854 def
.vd_flags
= VER_FLG_BASE
;
2857 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2858 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2859 + sizeof (Elf_External_Verdaux
));
2861 if (soname_indx
!= (bfd_size_type
) -1)
2863 def
.vd_hash
= bfd_elf_hash (soname
);
2864 defaux
.vda_name
= soname_indx
;
2871 name
= output_bfd
->filename
;
2872 def
.vd_hash
= bfd_elf_hash (name
);
2873 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2875 if (indx
== (bfd_size_type
) -1)
2877 defaux
.vda_name
= indx
;
2879 defaux
.vda_next
= 0;
2881 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2882 (Elf_External_Verdef
*)p
);
2883 p
+= sizeof (Elf_External_Verdef
);
2884 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2885 (Elf_External_Verdaux
*) p
);
2886 p
+= sizeof (Elf_External_Verdaux
);
2888 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2891 struct bfd_elf_version_deps
*n
;
2892 struct elf_link_hash_entry
*h
;
2895 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2898 /* Add a symbol representing this version. */
2900 if (! (_bfd_generic_link_add_one_symbol
2901 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
2902 (bfd_vma
) 0, (const char *) NULL
, false,
2903 get_elf_backend_data (dynobj
)->collect
,
2904 (struct bfd_link_hash_entry
**) &h
)))
2906 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
2907 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2908 h
->type
= STT_OBJECT
;
2909 h
->verinfo
.vertree
= t
;
2911 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2914 def
.vd_version
= VER_DEF_CURRENT
;
2916 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
2917 def
.vd_flags
|= VER_FLG_WEAK
;
2918 def
.vd_ndx
= t
->vernum
+ 1;
2919 def
.vd_cnt
= cdeps
+ 1;
2920 def
.vd_hash
= bfd_elf_hash (t
->name
);
2921 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2922 if (t
->next
!= NULL
)
2923 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2924 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
2928 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2929 (Elf_External_Verdef
*) p
);
2930 p
+= sizeof (Elf_External_Verdef
);
2932 defaux
.vda_name
= h
->dynstr_index
;
2933 if (t
->deps
== NULL
)
2934 defaux
.vda_next
= 0;
2936 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2937 t
->name_indx
= defaux
.vda_name
;
2939 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2940 (Elf_External_Verdaux
*) p
);
2941 p
+= sizeof (Elf_External_Verdaux
);
2943 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2945 if (n
->version_needed
== NULL
)
2947 /* This can happen if there was an error in the
2949 defaux
.vda_name
= 0;
2952 defaux
.vda_name
= n
->version_needed
->name_indx
;
2953 if (n
->next
== NULL
)
2954 defaux
.vda_next
= 0;
2956 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2958 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2959 (Elf_External_Verdaux
*) p
);
2960 p
+= sizeof (Elf_External_Verdaux
);
2964 if (! elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
2965 || ! elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
2968 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
2971 /* Work out the size of the version reference section. */
2973 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2974 BFD_ASSERT (s
!= NULL
);
2976 struct elf_find_verdep_info sinfo
;
2978 sinfo
.output_bfd
= output_bfd
;
2980 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
2981 if (sinfo
.vers
== 0)
2983 sinfo
.failed
= false;
2985 elf_link_hash_traverse (elf_hash_table (info
),
2986 elf_link_find_version_dependencies
,
2989 if (elf_tdata (output_bfd
)->verref
== NULL
)
2990 _bfd_strip_section_from_output (s
);
2993 Elf_Internal_Verneed
*t
;
2998 /* Build the version definition section. */
3001 for (t
= elf_tdata (output_bfd
)->verref
;
3005 Elf_Internal_Vernaux
*a
;
3007 size
+= sizeof (Elf_External_Verneed
);
3009 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3010 size
+= sizeof (Elf_External_Vernaux
);
3013 s
->_raw_size
= size
;
3014 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, size
);
3015 if (s
->contents
== NULL
)
3019 for (t
= elf_tdata (output_bfd
)->verref
;
3024 Elf_Internal_Vernaux
*a
;
3028 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3031 t
->vn_version
= VER_NEED_CURRENT
;
3033 if (elf_dt_name (t
->vn_bfd
) != NULL
)
3034 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3035 elf_dt_name (t
->vn_bfd
),
3038 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3039 t
->vn_bfd
->filename
, true, false);
3040 if (indx
== (bfd_size_type
) -1)
3043 t
->vn_aux
= sizeof (Elf_External_Verneed
);
3044 if (t
->vn_nextref
== NULL
)
3047 t
->vn_next
= (sizeof (Elf_External_Verneed
)
3048 + caux
* sizeof (Elf_External_Vernaux
));
3050 _bfd_elf_swap_verneed_out (output_bfd
, t
,
3051 (Elf_External_Verneed
*) p
);
3052 p
+= sizeof (Elf_External_Verneed
);
3054 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3056 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
3057 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
3058 a
->vna_nodename
, true, false);
3059 if (indx
== (bfd_size_type
) -1)
3062 if (a
->vna_nextptr
== NULL
)
3065 a
->vna_next
= sizeof (Elf_External_Vernaux
);
3067 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
3068 (Elf_External_Vernaux
*) p
);
3069 p
+= sizeof (Elf_External_Vernaux
);
3073 if (! elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
3074 || ! elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
3077 elf_tdata (output_bfd
)->cverrefs
= crefs
;
3081 /* Assign dynsym indicies. In a shared library we generate a
3082 section symbol for each output section, which come first.
3083 Next come all of the back-end allocated local dynamic syms,
3084 followed by the rest of the global symbols. */
3086 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3088 /* Work out the size of the symbol version section. */
3089 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3090 BFD_ASSERT (s
!= NULL
);
3091 if (dynsymcount
== 0
3092 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
3094 _bfd_strip_section_from_output (s
);
3095 /* The DYNSYMCOUNT might have changed if we were going to
3096 output a dynamic symbol table entry for S. */
3097 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
3101 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
3102 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
3103 if (s
->contents
== NULL
)
3106 if (! elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
3110 /* Set the size of the .dynsym and .hash sections. We counted
3111 the number of dynamic symbols in elf_link_add_object_symbols.
3112 We will build the contents of .dynsym and .hash when we build
3113 the final symbol table, because until then we do not know the
3114 correct value to give the symbols. We built the .dynstr
3115 section as we went along in elf_link_add_object_symbols. */
3116 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
3117 BFD_ASSERT (s
!= NULL
);
3118 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
3119 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3120 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
3123 /* The first entry in .dynsym is a dummy symbol. */
3130 elf_swap_symbol_out (output_bfd
, &isym
,
3131 (PTR
) (Elf_External_Sym
*) s
->contents
);
3133 /* Compute the size of the hashing table. As a side effect this
3134 computes the hash values for all the names we export. */
3135 bucketcount
= compute_bucket_count (info
);
3137 s
= bfd_get_section_by_name (dynobj
, ".hash");
3138 BFD_ASSERT (s
!= NULL
);
3139 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
3140 s
->_raw_size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
3141 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
3142 if (s
->contents
== NULL
)
3144 memset (s
->contents
, 0, (size_t) s
->_raw_size
);
3146 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
3147 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
3148 s
->contents
+ hash_entry_size
);
3150 elf_hash_table (info
)->bucketcount
= bucketcount
;
3152 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
3153 BFD_ASSERT (s
!= NULL
);
3154 s
->_raw_size
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
3156 if (! elf_add_dynamic_entry (info
, DT_NULL
, 0))
3163 /* Fix up the flags for a symbol. This handles various cases which
3164 can only be fixed after all the input files are seen. This is
3165 currently called by both adjust_dynamic_symbol and
3166 assign_sym_version, which is unnecessary but perhaps more robust in
3167 the face of future changes. */
3170 elf_fix_symbol_flags (h
, eif
)
3171 struct elf_link_hash_entry
*h
;
3172 struct elf_info_failed
*eif
;
3174 /* If this symbol was mentioned in a non-ELF file, try to set
3175 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3176 permit a non-ELF file to correctly refer to a symbol defined in
3177 an ELF dynamic object. */
3178 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
3180 if (h
->root
.type
!= bfd_link_hash_defined
3181 && h
->root
.type
!= bfd_link_hash_defweak
)
3182 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3183 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3186 if (h
->root
.u
.def
.section
->owner
!= NULL
3187 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3188 == bfd_target_elf_flavour
))
3189 h
->elf_link_hash_flags
|= (ELF_LINK_HASH_REF_REGULAR
3190 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
);
3192 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3195 if (h
->dynindx
== -1
3196 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
3197 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
3199 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3208 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
3209 was first seen in a non-ELF file. Fortunately, if the symbol
3210 was first seen in an ELF file, we're probably OK unless the
3211 symbol was defined in a non-ELF file. Catch that case here.
3212 FIXME: We're still in trouble if the symbol was first seen in
3213 a dynamic object, and then later in a non-ELF regular object. */
3214 if ((h
->root
.type
== bfd_link_hash_defined
3215 || h
->root
.type
== bfd_link_hash_defweak
)
3216 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3217 && (h
->root
.u
.def
.section
->owner
!= NULL
3218 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3219 != bfd_target_elf_flavour
)
3220 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3221 && (h
->elf_link_hash_flags
3222 & ELF_LINK_HASH_DEF_DYNAMIC
) == 0)))
3223 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3226 /* If this is a final link, and the symbol was defined as a common
3227 symbol in a regular object file, and there was no definition in
3228 any dynamic object, then the linker will have allocated space for
3229 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3230 flag will not have been set. */
3231 if (h
->root
.type
== bfd_link_hash_defined
3232 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
3233 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
3234 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3235 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3236 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3238 /* If -Bsymbolic was used (which means to bind references to global
3239 symbols to the definition within the shared object), and this
3240 symbol was defined in a regular object, then it actually doesn't
3241 need a PLT entry. */
3242 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
3243 && eif
->info
->shared
3244 && eif
->info
->symbolic
3245 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3247 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
3248 h
->plt
.offset
= (bfd_vma
) -1;
3254 /* Make the backend pick a good value for a dynamic symbol. This is
3255 called via elf_link_hash_traverse, and also calls itself
3259 elf_adjust_dynamic_symbol (h
, data
)
3260 struct elf_link_hash_entry
*h
;
3263 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3265 struct elf_backend_data
*bed
;
3267 /* Ignore indirect symbols. These are added by the versioning code. */
3268 if (h
->root
.type
== bfd_link_hash_indirect
)
3271 /* Fix the symbol flags. */
3272 if (! elf_fix_symbol_flags (h
, eif
))
3275 /* If this symbol does not require a PLT entry, and it is not
3276 defined by a dynamic object, or is not referenced by a regular
3277 object, ignore it. We do have to handle a weak defined symbol,
3278 even if no regular object refers to it, if we decided to add it
3279 to the dynamic symbol table. FIXME: Do we normally need to worry
3280 about symbols which are defined by one dynamic object and
3281 referenced by another one? */
3282 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
3283 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
3284 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3285 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
3286 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
3288 h
->plt
.offset
= (bfd_vma
) -1;
3292 /* If we've already adjusted this symbol, don't do it again. This
3293 can happen via a recursive call. */
3294 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
3297 /* Don't look at this symbol again. Note that we must set this
3298 after checking the above conditions, because we may look at a
3299 symbol once, decide not to do anything, and then get called
3300 recursively later after REF_REGULAR is set below. */
3301 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
3303 /* If this is a weak definition, and we know a real definition, and
3304 the real symbol is not itself defined by a regular object file,
3305 then get a good value for the real definition. We handle the
3306 real symbol first, for the convenience of the backend routine.
3308 Note that there is a confusing case here. If the real definition
3309 is defined by a regular object file, we don't get the real symbol
3310 from the dynamic object, but we do get the weak symbol. If the
3311 processor backend uses a COPY reloc, then if some routine in the
3312 dynamic object changes the real symbol, we will not see that
3313 change in the corresponding weak symbol. This is the way other
3314 ELF linkers work as well, and seems to be a result of the shared
3317 I will clarify this issue. Most SVR4 shared libraries define the
3318 variable _timezone and define timezone as a weak synonym. The
3319 tzset call changes _timezone. If you write
3320 extern int timezone;
3322 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3323 you might expect that, since timezone is a synonym for _timezone,
3324 the same number will print both times. However, if the processor
3325 backend uses a COPY reloc, then actually timezone will be copied
3326 into your process image, and, since you define _timezone
3327 yourself, _timezone will not. Thus timezone and _timezone will
3328 wind up at different memory locations. The tzset call will set
3329 _timezone, leaving timezone unchanged. */
3331 if (h
->weakdef
!= NULL
)
3333 struct elf_link_hash_entry
*weakdef
;
3335 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3336 || h
->root
.type
== bfd_link_hash_defweak
);
3337 weakdef
= h
->weakdef
;
3338 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
3339 || weakdef
->root
.type
== bfd_link_hash_defweak
);
3340 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
3341 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3343 /* This symbol is defined by a regular object file, so we
3344 will not do anything special. Clear weakdef for the
3345 convenience of the processor backend. */
3350 /* There is an implicit reference by a regular object file
3351 via the weak symbol. */
3352 weakdef
->elf_link_hash_flags
|=
3353 (ELF_LINK_HASH_REF_REGULAR
3354 | (h
->elf_link_hash_flags
3355 & (ELF_LINK_HASH_REF_REGULAR_NONWEAK
3356 | ELF_LINK_NON_GOT_REF
)));
3357 if (! elf_adjust_dynamic_symbol (weakdef
, (PTR
) eif
))
3362 /* If a symbol has no type and no size and does not require a PLT
3363 entry, then we are probably about to do the wrong thing here: we
3364 are probably going to create a COPY reloc for an empty object.
3365 This case can arise when a shared object is built with assembly
3366 code, and the assembly code fails to set the symbol type. */
3368 && h
->type
== STT_NOTYPE
3369 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0)
3370 (*_bfd_error_handler
)
3371 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3372 h
->root
.root
.string
);
3374 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
3375 bed
= get_elf_backend_data (dynobj
);
3376 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3385 /* This routine is used to export all defined symbols into the dynamic
3386 symbol table. It is called via elf_link_hash_traverse. */
3389 elf_export_symbol (h
, data
)
3390 struct elf_link_hash_entry
*h
;
3393 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3395 /* Ignore indirect symbols. These are added by the versioning code. */
3396 if (h
->root
.type
== bfd_link_hash_indirect
)
3399 if (h
->dynindx
== -1
3400 && (h
->elf_link_hash_flags
3401 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
3403 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3413 /* Look through the symbols which are defined in other shared
3414 libraries and referenced here. Update the list of version
3415 dependencies. This will be put into the .gnu.version_r section.
3416 This function is called via elf_link_hash_traverse. */
3419 elf_link_find_version_dependencies (h
, data
)
3420 struct elf_link_hash_entry
*h
;
3423 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
3424 Elf_Internal_Verneed
*t
;
3425 Elf_Internal_Vernaux
*a
;
3427 /* We only care about symbols defined in shared objects with version
3429 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3430 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
3432 || h
->verinfo
.verdef
== NULL
)
3435 /* See if we already know about this version. */
3436 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
3438 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
3441 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3442 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
3448 /* This is a new version. Add it to tree we are building. */
3452 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *t
);
3455 rinfo
->failed
= true;
3459 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
3460 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
3461 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
3464 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *a
);
3466 /* Note that we are copying a string pointer here, and testing it
3467 above. If bfd_elf_string_from_elf_section is ever changed to
3468 discard the string data when low in memory, this will have to be
3470 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
3472 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
3473 a
->vna_nextptr
= t
->vn_auxptr
;
3475 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
3478 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
3485 /* Figure out appropriate versions for all the symbols. We may not
3486 have the version number script until we have read all of the input
3487 files, so until that point we don't know which symbols should be
3488 local. This function is called via elf_link_hash_traverse. */
3491 elf_link_assign_sym_version (h
, data
)
3492 struct elf_link_hash_entry
*h
;
3495 struct elf_assign_sym_version_info
*sinfo
=
3496 (struct elf_assign_sym_version_info
*) data
;
3497 struct bfd_link_info
*info
= sinfo
->info
;
3498 struct elf_info_failed eif
;
3501 /* Fix the symbol flags. */
3504 if (! elf_fix_symbol_flags (h
, &eif
))
3507 sinfo
->failed
= true;
3511 /* We only need version numbers for symbols defined in regular
3513 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
3516 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3517 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
3519 struct bfd_elf_version_tree
*t
;
3524 /* There are two consecutive ELF_VER_CHR characters if this is
3525 not a hidden symbol. */
3527 if (*p
== ELF_VER_CHR
)
3533 /* If there is no version string, we can just return out. */
3537 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
3541 /* Look for the version. If we find it, it is no longer weak. */
3542 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
3544 if (strcmp (t
->name
, p
) == 0)
3548 struct bfd_elf_version_expr
*d
;
3550 len
= p
- h
->root
.root
.string
;
3551 alc
= bfd_alloc (sinfo
->output_bfd
, len
);
3554 strncpy (alc
, h
->root
.root
.string
, len
- 1);
3555 alc
[len
- 1] = '\0';
3556 if (alc
[len
- 2] == ELF_VER_CHR
)
3557 alc
[len
- 2] = '\0';
3559 h
->verinfo
.vertree
= t
;
3563 if (t
->globals
!= NULL
)
3565 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3566 if ((*d
->match
) (d
, alc
))
3570 /* See if there is anything to force this symbol to
3572 if (d
== NULL
&& t
->locals
!= NULL
)
3574 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3576 if ((*d
->match
) (d
, alc
))
3578 if (h
->dynindx
!= -1
3580 && ! sinfo
->export_dynamic
)
3582 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3583 h
->elf_link_hash_flags
&=~
3584 ELF_LINK_HASH_NEEDS_PLT
;
3586 h
->plt
.offset
= (bfd_vma
) -1;
3587 /* FIXME: The name of the symbol has
3588 already been recorded in the dynamic
3589 string table section. */
3597 bfd_release (sinfo
->output_bfd
, alc
);
3602 /* If we are building an application, we need to create a
3603 version node for this version. */
3604 if (t
== NULL
&& ! info
->shared
)
3606 struct bfd_elf_version_tree
**pp
;
3609 /* If we aren't going to export this symbol, we don't need
3610 to worry about it. */
3611 if (h
->dynindx
== -1)
3614 t
= ((struct bfd_elf_version_tree
*)
3615 bfd_alloc (sinfo
->output_bfd
, sizeof *t
));
3618 sinfo
->failed
= true;
3627 t
->name_indx
= (unsigned int) -1;
3631 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
3633 t
->vernum
= version_index
;
3637 h
->verinfo
.vertree
= t
;
3641 /* We could not find the version for a symbol when
3642 generating a shared archive. Return an error. */
3643 (*_bfd_error_handler
)
3644 (_("%s: undefined versioned symbol name %s"),
3645 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
3646 bfd_set_error (bfd_error_bad_value
);
3647 sinfo
->failed
= true;
3652 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
3655 /* If we don't have a version for this symbol, see if we can find
3657 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
3659 struct bfd_elf_version_tree
*t
;
3660 struct bfd_elf_version_tree
*deflt
;
3661 struct bfd_elf_version_expr
*d
;
3663 /* See if can find what version this symbol is in. If the
3664 symbol is supposed to be local, then don't actually register
3667 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
3669 if (t
->globals
!= NULL
)
3671 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3673 if ((*d
->match
) (d
, h
->root
.root
.string
))
3675 h
->verinfo
.vertree
= t
;
3684 if (t
->locals
!= NULL
)
3686 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3688 if (d
->pattern
[0] == '*' && d
->pattern
[1] == '\0')
3690 else if ((*d
->match
) (d
, h
->root
.root
.string
))
3692 h
->verinfo
.vertree
= t
;
3693 if (h
->dynindx
!= -1
3695 && ! sinfo
->export_dynamic
)
3697 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3698 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
3700 h
->plt
.offset
= (bfd_vma
) -1;
3701 /* FIXME: The name of the symbol has already
3702 been recorded in the dynamic string table
3714 if (deflt
!= NULL
&& h
->verinfo
.vertree
== NULL
)
3716 h
->verinfo
.vertree
= deflt
;
3717 if (h
->dynindx
!= -1
3719 && ! sinfo
->export_dynamic
)
3721 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3722 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
3724 h
->plt
.offset
= (bfd_vma
) -1;
3725 /* FIXME: The name of the symbol has already been
3726 recorded in the dynamic string table section. */
3734 /* Final phase of ELF linker. */
3736 /* A structure we use to avoid passing large numbers of arguments. */
3738 struct elf_final_link_info
3740 /* General link information. */
3741 struct bfd_link_info
*info
;
3744 /* Symbol string table. */
3745 struct bfd_strtab_hash
*symstrtab
;
3746 /* .dynsym section. */
3747 asection
*dynsym_sec
;
3748 /* .hash section. */
3750 /* symbol version section (.gnu.version). */
3751 asection
*symver_sec
;
3752 /* Buffer large enough to hold contents of any section. */
3754 /* Buffer large enough to hold external relocs of any section. */
3755 PTR external_relocs
;
3756 /* Buffer large enough to hold internal relocs of any section. */
3757 Elf_Internal_Rela
*internal_relocs
;
3758 /* Buffer large enough to hold external local symbols of any input
3760 Elf_External_Sym
*external_syms
;
3761 /* Buffer large enough to hold internal local symbols of any input
3763 Elf_Internal_Sym
*internal_syms
;
3764 /* Array large enough to hold a symbol index for each local symbol
3765 of any input BFD. */
3767 /* Array large enough to hold a section pointer for each local
3768 symbol of any input BFD. */
3769 asection
**sections
;
3770 /* Buffer to hold swapped out symbols. */
3771 Elf_External_Sym
*symbuf
;
3772 /* Number of swapped out symbols in buffer. */
3773 size_t symbuf_count
;
3774 /* Number of symbols which fit in symbuf. */
3778 static boolean elf_link_output_sym
3779 PARAMS ((struct elf_final_link_info
*, const char *,
3780 Elf_Internal_Sym
*, asection
*));
3781 static boolean elf_link_flush_output_syms
3782 PARAMS ((struct elf_final_link_info
*));
3783 static boolean elf_link_output_extsym
3784 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3785 static boolean elf_link_input_bfd
3786 PARAMS ((struct elf_final_link_info
*, bfd
*));
3787 static boolean elf_reloc_link_order
3788 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
3789 struct bfd_link_order
*));
3791 /* This struct is used to pass information to elf_link_output_extsym. */
3793 struct elf_outext_info
3797 struct elf_final_link_info
*finfo
;
3800 /* Compute the size of, and allocate space for, REL_HDR which is the
3801 section header for a section containing relocations for O. */
3804 elf_link_size_reloc_section (abfd
, rel_hdr
, o
)
3806 Elf_Internal_Shdr
*rel_hdr
;
3809 register struct elf_link_hash_entry
**p
, **pend
;
3810 unsigned reloc_count
;
3812 /* Figure out how many relocations there will be. */
3813 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
3814 reloc_count
= elf_section_data (o
)->rel_count
;
3816 reloc_count
= elf_section_data (o
)->rel_count2
;
3818 /* That allows us to calculate the size of the section. */
3819 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
3821 /* The contents field must last into write_object_contents, so we
3822 allocate it with bfd_alloc rather than malloc. */
3823 rel_hdr
->contents
= (PTR
) bfd_alloc (abfd
, rel_hdr
->sh_size
);
3824 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
3827 /* We only allocate one set of hash entries, so we only do it the
3828 first time we are called. */
3829 if (elf_section_data (o
)->rel_hashes
== NULL
)
3831 p
= ((struct elf_link_hash_entry
**)
3832 bfd_malloc (o
->reloc_count
3833 * sizeof (struct elf_link_hash_entry
*)));
3834 if (p
== NULL
&& o
->reloc_count
!= 0)
3837 elf_section_data (o
)->rel_hashes
= p
;
3838 pend
= p
+ o
->reloc_count
;
3839 for (; p
< pend
; p
++)
3846 /* When performing a relocateable link, the input relocations are
3847 preserved. But, if they reference global symbols, the indices
3848 referenced must be updated. Update all the relocations in
3849 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
3852 elf_link_adjust_relocs (abfd
, rel_hdr
, count
, rel_hash
)
3854 Elf_Internal_Shdr
*rel_hdr
;
3856 struct elf_link_hash_entry
**rel_hash
;
3860 for (i
= 0; i
< count
; i
++, rel_hash
++)
3862 if (*rel_hash
== NULL
)
3865 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
3867 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
3869 Elf_External_Rel
*erel
;
3870 Elf_Internal_Rel irel
;
3872 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
3873 elf_swap_reloc_in (abfd
, erel
, &irel
);
3874 irel
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
3875 ELF_R_TYPE (irel
.r_info
));
3876 elf_swap_reloc_out (abfd
, &irel
, erel
);
3880 Elf_External_Rela
*erela
;
3881 Elf_Internal_Rela irela
;
3883 BFD_ASSERT (rel_hdr
->sh_entsize
3884 == sizeof (Elf_External_Rela
));
3886 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
3887 elf_swap_reloca_in (abfd
, erela
, &irela
);
3888 irela
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
3889 ELF_R_TYPE (irela
.r_info
));
3890 elf_swap_reloca_out (abfd
, &irela
, erela
);
3895 /* Do the final step of an ELF link. */
3898 elf_bfd_final_link (abfd
, info
)
3900 struct bfd_link_info
*info
;
3904 struct elf_final_link_info finfo
;
3905 register asection
*o
;
3906 register struct bfd_link_order
*p
;
3908 size_t max_contents_size
;
3909 size_t max_external_reloc_size
;
3910 size_t max_internal_reloc_count
;
3911 size_t max_sym_count
;
3913 Elf_Internal_Sym elfsym
;
3915 Elf_Internal_Shdr
*symtab_hdr
;
3916 Elf_Internal_Shdr
*symstrtab_hdr
;
3917 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3918 struct elf_outext_info eoinfo
;
3921 abfd
->flags
|= DYNAMIC
;
3923 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
3924 dynobj
= elf_hash_table (info
)->dynobj
;
3927 finfo
.output_bfd
= abfd
;
3928 finfo
.symstrtab
= elf_stringtab_init ();
3929 if (finfo
.symstrtab
== NULL
)
3934 finfo
.dynsym_sec
= NULL
;
3935 finfo
.hash_sec
= NULL
;
3936 finfo
.symver_sec
= NULL
;
3940 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
3941 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
3942 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
3943 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3944 /* Note that it is OK if symver_sec is NULL. */
3947 finfo
.contents
= NULL
;
3948 finfo
.external_relocs
= NULL
;
3949 finfo
.internal_relocs
= NULL
;
3950 finfo
.external_syms
= NULL
;
3951 finfo
.internal_syms
= NULL
;
3952 finfo
.indices
= NULL
;
3953 finfo
.sections
= NULL
;
3954 finfo
.symbuf
= NULL
;
3955 finfo
.symbuf_count
= 0;
3957 /* Count up the number of relocations we will output for each output
3958 section, so that we know the sizes of the reloc sections. We
3959 also figure out some maximum sizes. */
3960 max_contents_size
= 0;
3961 max_external_reloc_size
= 0;
3962 max_internal_reloc_count
= 0;
3964 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3968 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3970 if (p
->type
== bfd_section_reloc_link_order
3971 || p
->type
== bfd_symbol_reloc_link_order
)
3973 else if (p
->type
== bfd_indirect_link_order
)
3977 sec
= p
->u
.indirect
.section
;
3979 /* Mark all sections which are to be included in the
3980 link. This will normally be every section. We need
3981 to do this so that we can identify any sections which
3982 the linker has decided to not include. */
3983 sec
->linker_mark
= true;
3985 if (info
->relocateable
)
3986 o
->reloc_count
+= sec
->reloc_count
;
3988 if (sec
->_raw_size
> max_contents_size
)
3989 max_contents_size
= sec
->_raw_size
;
3990 if (sec
->_cooked_size
> max_contents_size
)
3991 max_contents_size
= sec
->_cooked_size
;
3993 /* We are interested in just local symbols, not all
3995 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
3996 && (sec
->owner
->flags
& DYNAMIC
) == 0)
4000 if (elf_bad_symtab (sec
->owner
))
4001 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
4002 / sizeof (Elf_External_Sym
));
4004 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
4006 if (sym_count
> max_sym_count
)
4007 max_sym_count
= sym_count
;
4009 if ((sec
->flags
& SEC_RELOC
) != 0)
4013 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
4014 if (ext_size
> max_external_reloc_size
)
4015 max_external_reloc_size
= ext_size
;
4016 if (sec
->reloc_count
> max_internal_reloc_count
)
4017 max_internal_reloc_count
= sec
->reloc_count
;
4023 if (o
->reloc_count
> 0)
4024 o
->flags
|= SEC_RELOC
;
4027 /* Explicitly clear the SEC_RELOC flag. The linker tends to
4028 set it (this is probably a bug) and if it is set
4029 assign_section_numbers will create a reloc section. */
4030 o
->flags
&=~ SEC_RELOC
;
4033 /* If the SEC_ALLOC flag is not set, force the section VMA to
4034 zero. This is done in elf_fake_sections as well, but forcing
4035 the VMA to 0 here will ensure that relocs against these
4036 sections are handled correctly. */
4037 if ((o
->flags
& SEC_ALLOC
) == 0
4038 && ! o
->user_set_vma
)
4042 /* Figure out the file positions for everything but the symbol table
4043 and the relocs. We set symcount to force assign_section_numbers
4044 to create a symbol table. */
4045 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
4046 BFD_ASSERT (! abfd
->output_has_begun
);
4047 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
4050 /* Figure out how many relocations we will have in each section.
4051 Just using RELOC_COUNT isn't good enough since that doesn't
4052 maintain a separate value for REL vs. RELA relocations. */
4053 if (info
->relocateable
)
4054 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
4055 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
4057 asection
*output_section
;
4059 if (! o
->linker_mark
)
4061 /* This section was omitted from the link. */
4065 output_section
= o
->output_section
;
4067 if (output_section
!= NULL
4068 && (o
->flags
& SEC_RELOC
) != 0)
4070 struct bfd_elf_section_data
*esdi
4071 = elf_section_data (o
);
4072 struct bfd_elf_section_data
*esdo
4073 = elf_section_data (output_section
);
4074 unsigned int *rel_count
;
4075 unsigned int *rel_count2
;
4077 /* We must be careful to add the relocation froms the
4078 input section to the right output count. */
4079 if (esdi
->rel_hdr
.sh_entsize
== esdo
->rel_hdr
.sh_entsize
)
4081 rel_count
= &esdo
->rel_count
;
4082 rel_count2
= &esdo
->rel_count2
;
4086 rel_count
= &esdo
->rel_count2
;
4087 rel_count2
= &esdo
->rel_count
;
4090 *rel_count
+= (esdi
->rel_hdr
.sh_size
4091 / esdi
->rel_hdr
.sh_entsize
);
4093 *rel_count2
+= (esdi
->rel_hdr2
->sh_size
4094 / esdi
->rel_hdr2
->sh_entsize
);
4098 /* That created the reloc sections. Set their sizes, and assign
4099 them file positions, and allocate some buffers. */
4100 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4102 if ((o
->flags
& SEC_RELOC
) != 0)
4104 if (!elf_link_size_reloc_section (abfd
,
4105 &elf_section_data (o
)->rel_hdr
,
4109 if (elf_section_data (o
)->rel_hdr2
4110 && !elf_link_size_reloc_section (abfd
,
4111 elf_section_data (o
)->rel_hdr2
,
4116 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
4117 to count upwards while actually outputting the relocations. */
4118 elf_section_data (o
)->rel_count
= 0;
4119 elf_section_data (o
)->rel_count2
= 0;
4122 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4124 /* We have now assigned file positions for all the sections except
4125 .symtab and .strtab. We start the .symtab section at the current
4126 file position, and write directly to it. We build the .strtab
4127 section in memory. */
4128 bfd_get_symcount (abfd
) = 0;
4129 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4130 /* sh_name is set in prep_headers. */
4131 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4132 symtab_hdr
->sh_flags
= 0;
4133 symtab_hdr
->sh_addr
= 0;
4134 symtab_hdr
->sh_size
= 0;
4135 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
4136 /* sh_link is set in assign_section_numbers. */
4137 /* sh_info is set below. */
4138 /* sh_offset is set just below. */
4139 symtab_hdr
->sh_addralign
= 4; /* FIXME: system dependent? */
4141 off
= elf_tdata (abfd
)->next_file_pos
;
4142 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
4144 /* Note that at this point elf_tdata (abfd)->next_file_pos is
4145 incorrect. We do not yet know the size of the .symtab section.
4146 We correct next_file_pos below, after we do know the size. */
4148 /* Allocate a buffer to hold swapped out symbols. This is to avoid
4149 continuously seeking to the right position in the file. */
4150 if (! info
->keep_memory
|| max_sym_count
< 20)
4151 finfo
.symbuf_size
= 20;
4153 finfo
.symbuf_size
= max_sym_count
;
4154 finfo
.symbuf
= ((Elf_External_Sym
*)
4155 bfd_malloc (finfo
.symbuf_size
* sizeof (Elf_External_Sym
)));
4156 if (finfo
.symbuf
== NULL
)
4159 /* Start writing out the symbol table. The first symbol is always a
4161 if (info
->strip
!= strip_all
|| info
->relocateable
)
4163 elfsym
.st_value
= 0;
4166 elfsym
.st_other
= 0;
4167 elfsym
.st_shndx
= SHN_UNDEF
;
4168 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
4169 &elfsym
, bfd_und_section_ptr
))
4174 /* Some standard ELF linkers do this, but we don't because it causes
4175 bootstrap comparison failures. */
4176 /* Output a file symbol for the output file as the second symbol.
4177 We output this even if we are discarding local symbols, although
4178 I'm not sure if this is correct. */
4179 elfsym
.st_value
= 0;
4181 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4182 elfsym
.st_other
= 0;
4183 elfsym
.st_shndx
= SHN_ABS
;
4184 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
4185 &elfsym
, bfd_abs_section_ptr
))
4189 /* Output a symbol for each section. We output these even if we are
4190 discarding local symbols, since they are used for relocs. These
4191 symbols have no names. We store the index of each one in the
4192 index field of the section, so that we can find it again when
4193 outputting relocs. */
4194 if (info
->strip
!= strip_all
|| info
->relocateable
)
4197 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4198 elfsym
.st_other
= 0;
4199 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
4201 o
= section_from_elf_index (abfd
, i
);
4203 o
->target_index
= bfd_get_symcount (abfd
);
4204 elfsym
.st_shndx
= i
;
4205 if (info
->relocateable
|| o
== NULL
)
4206 elfsym
.st_value
= 0;
4208 elfsym
.st_value
= o
->vma
;
4209 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
4215 /* Allocate some memory to hold information read in from the input
4217 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
4218 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
4219 finfo
.internal_relocs
= ((Elf_Internal_Rela
*)
4220 bfd_malloc (max_internal_reloc_count
4221 * sizeof (Elf_Internal_Rela
)
4222 * bed
->s
->int_rels_per_ext_rel
));
4223 finfo
.external_syms
= ((Elf_External_Sym
*)
4224 bfd_malloc (max_sym_count
4225 * sizeof (Elf_External_Sym
)));
4226 finfo
.internal_syms
= ((Elf_Internal_Sym
*)
4227 bfd_malloc (max_sym_count
4228 * sizeof (Elf_Internal_Sym
)));
4229 finfo
.indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
4230 finfo
.sections
= ((asection
**)
4231 bfd_malloc (max_sym_count
* sizeof (asection
*)));
4232 if ((finfo
.contents
== NULL
&& max_contents_size
!= 0)
4233 || (finfo
.external_relocs
== NULL
&& max_external_reloc_size
!= 0)
4234 || (finfo
.internal_relocs
== NULL
&& max_internal_reloc_count
!= 0)
4235 || (finfo
.external_syms
== NULL
&& max_sym_count
!= 0)
4236 || (finfo
.internal_syms
== NULL
&& max_sym_count
!= 0)
4237 || (finfo
.indices
== NULL
&& max_sym_count
!= 0)
4238 || (finfo
.sections
== NULL
&& max_sym_count
!= 0))
4241 /* Since ELF permits relocations to be against local symbols, we
4242 must have the local symbols available when we do the relocations.
4243 Since we would rather only read the local symbols once, and we
4244 would rather not keep them in memory, we handle all the
4245 relocations for a single input file at the same time.
4247 Unfortunately, there is no way to know the total number of local
4248 symbols until we have seen all of them, and the local symbol
4249 indices precede the global symbol indices. This means that when
4250 we are generating relocateable output, and we see a reloc against
4251 a global symbol, we can not know the symbol index until we have
4252 finished examining all the local symbols to see which ones we are
4253 going to output. To deal with this, we keep the relocations in
4254 memory, and don't output them until the end of the link. This is
4255 an unfortunate waste of memory, but I don't see a good way around
4256 it. Fortunately, it only happens when performing a relocateable
4257 link, which is not the common case. FIXME: If keep_memory is set
4258 we could write the relocs out and then read them again; I don't
4259 know how bad the memory loss will be. */
4261 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
4262 sub
->output_has_begun
= false;
4263 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4265 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
4267 if (p
->type
== bfd_indirect_link_order
4268 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
4269 == bfd_target_elf_flavour
))
4271 sub
= p
->u
.indirect
.section
->owner
;
4272 if (! sub
->output_has_begun
)
4274 if (! elf_link_input_bfd (&finfo
, sub
))
4276 sub
->output_has_begun
= true;
4279 else if (p
->type
== bfd_section_reloc_link_order
4280 || p
->type
== bfd_symbol_reloc_link_order
)
4282 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
4287 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
4293 /* That wrote out all the local symbols. Finish up the symbol table
4294 with the global symbols. */
4296 if (info
->strip
!= strip_all
&& info
->shared
)
4298 /* Output any global symbols that got converted to local in a
4299 version script. We do this in a separate step since ELF
4300 requires all local symbols to appear prior to any global
4301 symbols. FIXME: We should only do this if some global
4302 symbols were, in fact, converted to become local. FIXME:
4303 Will this work correctly with the Irix 5 linker? */
4304 eoinfo
.failed
= false;
4305 eoinfo
.finfo
= &finfo
;
4306 eoinfo
.localsyms
= true;
4307 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
4313 /* The sh_info field records the index of the first non local symbol. */
4314 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
4318 Elf_Internal_Sym sym
;
4319 Elf_External_Sym
*dynsym
=
4320 (Elf_External_Sym
*)finfo
.dynsym_sec
->contents
;
4321 unsigned long last_local
= 0;
4323 /* Write out the section symbols for the output sections. */
4330 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4333 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4336 indx
= elf_section_data (s
)->this_idx
;
4337 BFD_ASSERT (indx
> 0);
4338 sym
.st_shndx
= indx
;
4339 sym
.st_value
= s
->vma
;
4341 elf_swap_symbol_out (abfd
, &sym
,
4342 dynsym
+ elf_section_data (s
)->dynindx
);
4345 last_local
= bfd_count_sections (abfd
);
4348 /* Write out the local dynsyms. */
4349 if (elf_hash_table (info
)->dynlocal
)
4351 struct elf_link_local_dynamic_entry
*e
;
4352 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
4356 sym
.st_size
= e
->isym
.st_size
;
4357 sym
.st_other
= e
->isym
.st_other
;
4359 /* Copy the internal symbol as is.
4360 Note that we saved a word of storage and overwrote
4361 the original st_name with the dynstr_index. */
4364 if (e
->isym
.st_shndx
> 0 && e
->isym
.st_shndx
< SHN_LORESERVE
)
4366 s
= bfd_section_from_elf_index (e
->input_bfd
,
4370 elf_section_data (s
->output_section
)->this_idx
;
4371 sym
.st_value
= (s
->output_section
->vma
4373 + e
->isym
.st_value
);
4376 if (last_local
< e
->dynindx
)
4377 last_local
= e
->dynindx
;
4379 elf_swap_symbol_out (abfd
, &sym
, dynsym
+ e
->dynindx
);
4383 elf_section_data (finfo
.dynsym_sec
->output_section
)
4384 ->this_hdr
.sh_info
= last_local
+ 1;
4387 /* We get the global symbols from the hash table. */
4388 eoinfo
.failed
= false;
4389 eoinfo
.localsyms
= false;
4390 eoinfo
.finfo
= &finfo
;
4391 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
4396 /* If backend needs to output some symbols not present in the hash
4397 table, do it now. */
4398 if (bed
->elf_backend_output_arch_syms
)
4400 if (! (*bed
->elf_backend_output_arch_syms
)
4401 (abfd
, info
, (PTR
) &finfo
,
4402 (boolean (*) PARAMS ((PTR
, const char *,
4403 Elf_Internal_Sym
*, asection
*)))
4404 elf_link_output_sym
))
4408 /* Flush all symbols to the file. */
4409 if (! elf_link_flush_output_syms (&finfo
))
4412 /* Now we know the size of the symtab section. */
4413 off
+= symtab_hdr
->sh_size
;
4415 /* Finish up and write out the symbol string table (.strtab)
4417 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4418 /* sh_name was set in prep_headers. */
4419 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4420 symstrtab_hdr
->sh_flags
= 0;
4421 symstrtab_hdr
->sh_addr
= 0;
4422 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
4423 symstrtab_hdr
->sh_entsize
= 0;
4424 symstrtab_hdr
->sh_link
= 0;
4425 symstrtab_hdr
->sh_info
= 0;
4426 /* sh_offset is set just below. */
4427 symstrtab_hdr
->sh_addralign
= 1;
4429 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, true);
4430 elf_tdata (abfd
)->next_file_pos
= off
;
4432 if (bfd_get_symcount (abfd
) > 0)
4434 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
4435 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
4439 /* Adjust the relocs to have the correct symbol indices. */
4440 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4442 if ((o
->flags
& SEC_RELOC
) == 0)
4445 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
4446 elf_section_data (o
)->rel_count
,
4447 elf_section_data (o
)->rel_hashes
);
4448 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
4449 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
4450 elf_section_data (o
)->rel_count2
,
4451 (elf_section_data (o
)->rel_hashes
4452 + elf_section_data (o
)->rel_count
));
4454 /* Set the reloc_count field to 0 to prevent write_relocs from
4455 trying to swap the relocs out itself. */
4459 /* If we are linking against a dynamic object, or generating a
4460 shared library, finish up the dynamic linking information. */
4463 Elf_External_Dyn
*dyncon
, *dynconend
;
4465 /* Fix up .dynamic entries. */
4466 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
4467 BFD_ASSERT (o
!= NULL
);
4469 dyncon
= (Elf_External_Dyn
*) o
->contents
;
4470 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
4471 for (; dyncon
< dynconend
; dyncon
++)
4473 Elf_Internal_Dyn dyn
;
4477 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
4484 name
= info
->init_function
;
4487 name
= info
->fini_function
;
4490 struct elf_link_hash_entry
*h
;
4492 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
4493 false, false, true);
4495 && (h
->root
.type
== bfd_link_hash_defined
4496 || h
->root
.type
== bfd_link_hash_defweak
))
4498 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
4499 o
= h
->root
.u
.def
.section
;
4500 if (o
->output_section
!= NULL
)
4501 dyn
.d_un
.d_val
+= (o
->output_section
->vma
4502 + o
->output_offset
);
4505 /* The symbol is imported from another shared
4506 library and does not apply to this one. */
4510 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
4525 name
= ".gnu.version_d";
4528 name
= ".gnu.version_r";
4531 name
= ".gnu.version";
4533 o
= bfd_get_section_by_name (abfd
, name
);
4534 BFD_ASSERT (o
!= NULL
);
4535 dyn
.d_un
.d_ptr
= o
->vma
;
4536 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
4543 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
4548 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
4550 Elf_Internal_Shdr
*hdr
;
4552 hdr
= elf_elfsections (abfd
)[i
];
4553 if (hdr
->sh_type
== type
4554 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
4556 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
4557 dyn
.d_un
.d_val
+= hdr
->sh_size
;
4560 if (dyn
.d_un
.d_val
== 0
4561 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
4562 dyn
.d_un
.d_val
= hdr
->sh_addr
;
4566 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
4572 /* If we have created any dynamic sections, then output them. */
4575 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
4578 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4580 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4581 || o
->_raw_size
== 0)
4583 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
4585 /* At this point, we are only interested in sections
4586 created by elf_link_create_dynamic_sections. */
4589 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
4591 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
4593 if (! bfd_set_section_contents (abfd
, o
->output_section
,
4594 o
->contents
, o
->output_offset
,
4602 /* The contents of the .dynstr section are actually in a
4604 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
4605 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
4606 || ! _bfd_stringtab_emit (abfd
,
4607 elf_hash_table (info
)->dynstr
))
4613 /* If we have optimized stabs strings, output them. */
4614 if (elf_hash_table (info
)->stab_info
!= NULL
)
4616 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
4620 if (finfo
.symstrtab
!= NULL
)
4621 _bfd_stringtab_free (finfo
.symstrtab
);
4622 if (finfo
.contents
!= NULL
)
4623 free (finfo
.contents
);
4624 if (finfo
.external_relocs
!= NULL
)
4625 free (finfo
.external_relocs
);
4626 if (finfo
.internal_relocs
!= NULL
)
4627 free (finfo
.internal_relocs
);
4628 if (finfo
.external_syms
!= NULL
)
4629 free (finfo
.external_syms
);
4630 if (finfo
.internal_syms
!= NULL
)
4631 free (finfo
.internal_syms
);
4632 if (finfo
.indices
!= NULL
)
4633 free (finfo
.indices
);
4634 if (finfo
.sections
!= NULL
)
4635 free (finfo
.sections
);
4636 if (finfo
.symbuf
!= NULL
)
4637 free (finfo
.symbuf
);
4638 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4640 if ((o
->flags
& SEC_RELOC
) != 0
4641 && elf_section_data (o
)->rel_hashes
!= NULL
)
4642 free (elf_section_data (o
)->rel_hashes
);
4645 elf_tdata (abfd
)->linker
= true;
4650 if (finfo
.symstrtab
!= NULL
)
4651 _bfd_stringtab_free (finfo
.symstrtab
);
4652 if (finfo
.contents
!= NULL
)
4653 free (finfo
.contents
);
4654 if (finfo
.external_relocs
!= NULL
)
4655 free (finfo
.external_relocs
);
4656 if (finfo
.internal_relocs
!= NULL
)
4657 free (finfo
.internal_relocs
);
4658 if (finfo
.external_syms
!= NULL
)
4659 free (finfo
.external_syms
);
4660 if (finfo
.internal_syms
!= NULL
)
4661 free (finfo
.internal_syms
);
4662 if (finfo
.indices
!= NULL
)
4663 free (finfo
.indices
);
4664 if (finfo
.sections
!= NULL
)
4665 free (finfo
.sections
);
4666 if (finfo
.symbuf
!= NULL
)
4667 free (finfo
.symbuf
);
4668 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4670 if ((o
->flags
& SEC_RELOC
) != 0
4671 && elf_section_data (o
)->rel_hashes
!= NULL
)
4672 free (elf_section_data (o
)->rel_hashes
);
4678 /* Add a symbol to the output symbol table. */
4681 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
4682 struct elf_final_link_info
*finfo
;
4684 Elf_Internal_Sym
*elfsym
;
4685 asection
*input_sec
;
4687 boolean (*output_symbol_hook
) PARAMS ((bfd
*,
4688 struct bfd_link_info
*info
,
4693 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
4694 elf_backend_link_output_symbol_hook
;
4695 if (output_symbol_hook
!= NULL
)
4697 if (! ((*output_symbol_hook
)
4698 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
4702 if (name
== (const char *) NULL
|| *name
== '\0')
4703 elfsym
->st_name
= 0;
4704 else if (input_sec
->flags
& SEC_EXCLUDE
)
4705 elfsym
->st_name
= 0;
4708 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
4711 if (elfsym
->st_name
== (unsigned long) -1)
4715 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
4717 if (! elf_link_flush_output_syms (finfo
))
4721 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
,
4722 (PTR
) (finfo
->symbuf
+ finfo
->symbuf_count
));
4723 ++finfo
->symbuf_count
;
4725 ++ bfd_get_symcount (finfo
->output_bfd
);
4730 /* Flush the output symbols to the file. */
4733 elf_link_flush_output_syms (finfo
)
4734 struct elf_final_link_info
*finfo
;
4736 if (finfo
->symbuf_count
> 0)
4738 Elf_Internal_Shdr
*symtab
;
4740 symtab
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
4742 if (bfd_seek (finfo
->output_bfd
, symtab
->sh_offset
+ symtab
->sh_size
,
4744 || (bfd_write ((PTR
) finfo
->symbuf
, finfo
->symbuf_count
,
4745 sizeof (Elf_External_Sym
), finfo
->output_bfd
)
4746 != finfo
->symbuf_count
* sizeof (Elf_External_Sym
)))
4749 symtab
->sh_size
+= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
4751 finfo
->symbuf_count
= 0;
4757 /* Add an external symbol to the symbol table. This is called from
4758 the hash table traversal routine. When generating a shared object,
4759 we go through the symbol table twice. The first time we output
4760 anything that might have been forced to local scope in a version
4761 script. The second time we output the symbols that are still
4765 elf_link_output_extsym (h
, data
)
4766 struct elf_link_hash_entry
*h
;
4769 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
4770 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
4772 Elf_Internal_Sym sym
;
4773 asection
*input_sec
;
4775 /* Decide whether to output this symbol in this pass. */
4776 if (eoinfo
->localsyms
)
4778 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
4783 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4787 /* If we are not creating a shared library, and this symbol is
4788 referenced by a shared library but is not defined anywhere, then
4789 warn that it is undefined. If we do not do this, the runtime
4790 linker will complain that the symbol is undefined when the
4791 program is run. We don't have to worry about symbols that are
4792 referenced by regular files, because we will already have issued
4793 warnings for them. */
4794 if (! finfo
->info
->relocateable
4795 && ! (finfo
->info
->shared
4796 && !finfo
->info
->no_undefined
)
4797 && h
->root
.type
== bfd_link_hash_undefined
4798 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
4799 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
4801 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
4802 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
4803 (asection
*) NULL
, 0)))
4805 eoinfo
->failed
= true;
4810 /* We don't want to output symbols that have never been mentioned by
4811 a regular file, or that we have been told to strip. However, if
4812 h->indx is set to -2, the symbol is used by a reloc and we must
4816 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
4817 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
4818 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
4819 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
4821 else if (finfo
->info
->strip
== strip_all
4822 || (finfo
->info
->strip
== strip_some
4823 && bfd_hash_lookup (finfo
->info
->keep_hash
,
4824 h
->root
.root
.string
,
4825 false, false) == NULL
))
4830 /* If we're stripping it, and it's not a dynamic symbol, there's
4831 nothing else to do. */
4832 if (strip
&& h
->dynindx
== -1)
4836 sym
.st_size
= h
->size
;
4837 sym
.st_other
= h
->other
;
4838 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4839 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
4840 else if (h
->root
.type
== bfd_link_hash_undefweak
4841 || h
->root
.type
== bfd_link_hash_defweak
)
4842 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
4844 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
4846 switch (h
->root
.type
)
4849 case bfd_link_hash_new
:
4853 case bfd_link_hash_undefined
:
4854 input_sec
= bfd_und_section_ptr
;
4855 sym
.st_shndx
= SHN_UNDEF
;
4858 case bfd_link_hash_undefweak
:
4859 input_sec
= bfd_und_section_ptr
;
4860 sym
.st_shndx
= SHN_UNDEF
;
4863 case bfd_link_hash_defined
:
4864 case bfd_link_hash_defweak
:
4866 input_sec
= h
->root
.u
.def
.section
;
4867 if (input_sec
->output_section
!= NULL
)
4870 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
4871 input_sec
->output_section
);
4872 if (sym
.st_shndx
== (unsigned short) -1)
4874 (*_bfd_error_handler
)
4875 (_("%s: could not find output section %s for input section %s"),
4876 bfd_get_filename (finfo
->output_bfd
),
4877 input_sec
->output_section
->name
,
4879 eoinfo
->failed
= true;
4883 /* ELF symbols in relocateable files are section relative,
4884 but in nonrelocateable files they are virtual
4886 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
4887 if (! finfo
->info
->relocateable
)
4888 sym
.st_value
+= input_sec
->output_section
->vma
;
4892 BFD_ASSERT (input_sec
->owner
== NULL
4893 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
4894 sym
.st_shndx
= SHN_UNDEF
;
4895 input_sec
= bfd_und_section_ptr
;
4900 case bfd_link_hash_common
:
4901 input_sec
= h
->root
.u
.c
.p
->section
;
4902 sym
.st_shndx
= SHN_COMMON
;
4903 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
4906 case bfd_link_hash_indirect
:
4907 /* These symbols are created by symbol versioning. They point
4908 to the decorated version of the name. For example, if the
4909 symbol foo@@GNU_1.2 is the default, which should be used when
4910 foo is used with no version, then we add an indirect symbol
4911 foo which points to foo@@GNU_1.2. We ignore these symbols,
4912 since the indirected symbol is already in the hash table. If
4913 the indirect symbol is non-ELF, fall through and output it. */
4914 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) == 0)
4918 case bfd_link_hash_warning
:
4919 /* We can't represent these symbols in ELF, although a warning
4920 symbol may have come from a .gnu.warning.SYMBOL section. We
4921 just put the target symbol in the hash table. If the target
4922 symbol does not really exist, don't do anything. */
4923 if (h
->root
.u
.i
.link
->type
== bfd_link_hash_new
)
4925 return (elf_link_output_extsym
4926 ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
));
4929 /* Give the processor backend a chance to tweak the symbol value,
4930 and also to finish up anything that needs to be done for this
4932 if ((h
->dynindx
!= -1
4933 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4934 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4936 struct elf_backend_data
*bed
;
4938 bed
= get_elf_backend_data (finfo
->output_bfd
);
4939 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
4940 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
4942 eoinfo
->failed
= true;
4947 /* If we are marking the symbol as undefined, and there are no
4948 non-weak references to this symbol from a regular object, then
4949 mark the symbol as weak undefined; if there are non-weak
4950 references, mark the symbol as strong. We can't do this earlier,
4951 because it might not be marked as undefined until the
4952 finish_dynamic_symbol routine gets through with it. */
4953 if (sym
.st_shndx
== SHN_UNDEF
4954 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
4955 && (ELF_ST_BIND(sym
.st_info
) == STB_GLOBAL
4956 || ELF_ST_BIND(sym
.st_info
) == STB_WEAK
))
4960 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR_NONWEAK
) != 0)
4961 bindtype
= STB_GLOBAL
;
4963 bindtype
= STB_WEAK
;
4964 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
4967 /* If this symbol should be put in the .dynsym section, then put it
4968 there now. We have already know the symbol index. We also fill
4969 in the entry in the .hash section. */
4970 if (h
->dynindx
!= -1
4971 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4975 size_t hash_entry_size
;
4976 bfd_byte
*bucketpos
;
4979 sym
.st_name
= h
->dynstr_index
;
4981 elf_swap_symbol_out (finfo
->output_bfd
, &sym
,
4982 (PTR
) (((Elf_External_Sym
*)
4983 finfo
->dynsym_sec
->contents
)
4986 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
4987 bucket
= h
->elf_hash_value
% bucketcount
;
4989 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
4990 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
4991 + (bucket
+ 2) * hash_entry_size
);
4992 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
4993 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
4994 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
4995 ((bfd_byte
*) finfo
->hash_sec
->contents
4996 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
4998 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
5000 Elf_Internal_Versym iversym
;
5002 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
5004 if (h
->verinfo
.verdef
== NULL
)
5005 iversym
.vs_vers
= 0;
5007 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
5011 if (h
->verinfo
.vertree
== NULL
)
5012 iversym
.vs_vers
= 1;
5014 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
5017 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
5018 iversym
.vs_vers
|= VERSYM_HIDDEN
;
5020 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
,
5021 (((Elf_External_Versym
*)
5022 finfo
->symver_sec
->contents
)
5027 /* If we're stripping it, then it was just a dynamic symbol, and
5028 there's nothing else to do. */
5032 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
5034 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
5036 eoinfo
->failed
= true;
5043 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
5044 originated from the section given by INPUT_REL_HDR) to the
5048 elf_link_output_relocs (output_bfd
, input_section
, input_rel_hdr
,
5051 asection
*input_section
;
5052 Elf_Internal_Shdr
*input_rel_hdr
;
5053 Elf_Internal_Rela
*internal_relocs
;
5055 Elf_Internal_Rela
*irela
;
5056 Elf_Internal_Rela
*irelaend
;
5057 Elf_Internal_Shdr
*output_rel_hdr
;
5058 asection
*output_section
;
5059 unsigned int *rel_countp
= NULL
;
5061 output_section
= input_section
->output_section
;
5062 output_rel_hdr
= NULL
;
5064 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
5065 == input_rel_hdr
->sh_entsize
)
5067 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
5068 rel_countp
= &elf_section_data (output_section
)->rel_count
;
5070 else if (elf_section_data (output_section
)->rel_hdr2
5071 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
5072 == input_rel_hdr
->sh_entsize
))
5074 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
5075 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
5078 BFD_ASSERT (output_rel_hdr
!= NULL
);
5080 irela
= internal_relocs
;
5081 irelaend
= irela
+ input_rel_hdr
->sh_size
/ input_rel_hdr
->sh_entsize
;
5082 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
5084 Elf_External_Rel
*erel
;
5086 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
+ *rel_countp
);
5087 for (; irela
< irelaend
; irela
++, erel
++)
5089 Elf_Internal_Rel irel
;
5091 irel
.r_offset
= irela
->r_offset
;
5092 irel
.r_info
= irela
->r_info
;
5093 BFD_ASSERT (irela
->r_addend
== 0);
5094 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
5099 Elf_External_Rela
*erela
;
5101 BFD_ASSERT (input_rel_hdr
->sh_entsize
5102 == sizeof (Elf_External_Rela
));
5103 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
+ *rel_countp
);
5104 for (; irela
< irelaend
; irela
++, erela
++)
5105 elf_swap_reloca_out (output_bfd
, irela
, erela
);
5108 /* Bump the counter, so that we know where to add the next set of
5110 *rel_countp
+= input_rel_hdr
->sh_size
/ input_rel_hdr
->sh_entsize
;
5113 /* Link an input file into the linker output file. This function
5114 handles all the sections and relocations of the input file at once.
5115 This is so that we only have to read the local symbols once, and
5116 don't have to keep them in memory. */
5119 elf_link_input_bfd (finfo
, input_bfd
)
5120 struct elf_final_link_info
*finfo
;
5123 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
5124 bfd
*, asection
*, bfd_byte
*,
5125 Elf_Internal_Rela
*,
5126 Elf_Internal_Sym
*, asection
**));
5128 Elf_Internal_Shdr
*symtab_hdr
;
5131 Elf_External_Sym
*external_syms
;
5132 Elf_External_Sym
*esym
;
5133 Elf_External_Sym
*esymend
;
5134 Elf_Internal_Sym
*isym
;
5136 asection
**ppsection
;
5138 struct elf_backend_data
*bed
;
5140 output_bfd
= finfo
->output_bfd
;
5141 bed
= get_elf_backend_data (output_bfd
);
5142 relocate_section
= bed
->elf_backend_relocate_section
;
5144 /* If this is a dynamic object, we don't want to do anything here:
5145 we don't want the local symbols, and we don't want the section
5147 if ((input_bfd
->flags
& DYNAMIC
) != 0)
5150 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
5151 if (elf_bad_symtab (input_bfd
))
5153 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
5158 locsymcount
= symtab_hdr
->sh_info
;
5159 extsymoff
= symtab_hdr
->sh_info
;
5162 /* Read the local symbols. */
5163 if (symtab_hdr
->contents
!= NULL
)
5164 external_syms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
5165 else if (locsymcount
== 0)
5166 external_syms
= NULL
;
5169 external_syms
= finfo
->external_syms
;
5170 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
5171 || (bfd_read (external_syms
, sizeof (Elf_External_Sym
),
5172 locsymcount
, input_bfd
)
5173 != locsymcount
* sizeof (Elf_External_Sym
)))
5177 /* Swap in the local symbols and write out the ones which we know
5178 are going into the output file. */
5179 esym
= external_syms
;
5180 esymend
= esym
+ locsymcount
;
5181 isym
= finfo
->internal_syms
;
5182 pindex
= finfo
->indices
;
5183 ppsection
= finfo
->sections
;
5184 for (; esym
< esymend
; esym
++, isym
++, pindex
++, ppsection
++)
5188 Elf_Internal_Sym osym
;
5190 elf_swap_symbol_in (input_bfd
, esym
, isym
);
5193 if (elf_bad_symtab (input_bfd
))
5195 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
5202 if (isym
->st_shndx
== SHN_UNDEF
)
5203 isec
= bfd_und_section_ptr
;
5204 else if (isym
->st_shndx
> 0 && isym
->st_shndx
< SHN_LORESERVE
)
5205 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
5206 else if (isym
->st_shndx
== SHN_ABS
)
5207 isec
= bfd_abs_section_ptr
;
5208 else if (isym
->st_shndx
== SHN_COMMON
)
5209 isec
= bfd_com_section_ptr
;
5218 /* Don't output the first, undefined, symbol. */
5219 if (esym
== external_syms
)
5222 /* If we are stripping all symbols, we don't want to output this
5224 if (finfo
->info
->strip
== strip_all
)
5227 /* We never output section symbols. Instead, we use the section
5228 symbol of the corresponding section in the output file. */
5229 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
5232 /* If we are discarding all local symbols, we don't want to
5233 output this one. If we are generating a relocateable output
5234 file, then some of the local symbols may be required by
5235 relocs; we output them below as we discover that they are
5237 if (finfo
->info
->discard
== discard_all
)
5240 /* If this symbol is defined in a section which we are
5241 discarding, we don't need to keep it, but note that
5242 linker_mark is only reliable for sections that have contents.
5243 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
5244 as well as linker_mark. */
5245 if (isym
->st_shndx
> 0
5246 && isym
->st_shndx
< SHN_LORESERVE
5248 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
5249 || (! finfo
->info
->relocateable
5250 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
5253 /* Get the name of the symbol. */
5254 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
5259 /* See if we are discarding symbols with this name. */
5260 if ((finfo
->info
->strip
== strip_some
5261 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
5263 || (finfo
->info
->discard
== discard_l
5264 && bfd_is_local_label_name (input_bfd
, name
)))
5267 /* If we get here, we are going to output this symbol. */
5271 /* Adjust the section index for the output file. */
5272 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
5273 isec
->output_section
);
5274 if (osym
.st_shndx
== (unsigned short) -1)
5277 *pindex
= bfd_get_symcount (output_bfd
);
5279 /* ELF symbols in relocateable files are section relative, but
5280 in executable files they are virtual addresses. Note that
5281 this code assumes that all ELF sections have an associated
5282 BFD section with a reasonable value for output_offset; below
5283 we assume that they also have a reasonable value for
5284 output_section. Any special sections must be set up to meet
5285 these requirements. */
5286 osym
.st_value
+= isec
->output_offset
;
5287 if (! finfo
->info
->relocateable
)
5288 osym
.st_value
+= isec
->output_section
->vma
;
5290 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
5294 /* Relocate the contents of each section. */
5295 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
5299 if (! o
->linker_mark
)
5301 /* This section was omitted from the link. */
5305 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5306 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
5309 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
5311 /* Section was created by elf_link_create_dynamic_sections
5316 /* Get the contents of the section. They have been cached by a
5317 relaxation routine. Note that o is a section in an input
5318 file, so the contents field will not have been set by any of
5319 the routines which work on output files. */
5320 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
5321 contents
= elf_section_data (o
)->this_hdr
.contents
;
5324 contents
= finfo
->contents
;
5325 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
5326 (file_ptr
) 0, o
->_raw_size
))
5330 if ((o
->flags
& SEC_RELOC
) != 0)
5332 Elf_Internal_Rela
*internal_relocs
;
5334 /* Get the swapped relocs. */
5335 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
5336 (input_bfd
, o
, finfo
->external_relocs
,
5337 finfo
->internal_relocs
, false));
5338 if (internal_relocs
== NULL
5339 && o
->reloc_count
> 0)
5342 /* Relocate the section by invoking a back end routine.
5344 The back end routine is responsible for adjusting the
5345 section contents as necessary, and (if using Rela relocs
5346 and generating a relocateable output file) adjusting the
5347 reloc addend as necessary.
5349 The back end routine does not have to worry about setting
5350 the reloc address or the reloc symbol index.
5352 The back end routine is given a pointer to the swapped in
5353 internal symbols, and can access the hash table entries
5354 for the external symbols via elf_sym_hashes (input_bfd).
5356 When generating relocateable output, the back end routine
5357 must handle STB_LOCAL/STT_SECTION symbols specially. The
5358 output symbol is going to be a section symbol
5359 corresponding to the output section, which will require
5360 the addend to be adjusted. */
5362 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
5363 input_bfd
, o
, contents
,
5365 finfo
->internal_syms
,
5369 if (finfo
->info
->relocateable
)
5371 Elf_Internal_Rela
*irela
;
5372 Elf_Internal_Rela
*irelaend
;
5373 struct elf_link_hash_entry
**rel_hash
;
5374 Elf_Internal_Shdr
*input_rel_hdr
;
5376 /* Adjust the reloc addresses and symbol indices. */
5378 irela
= internal_relocs
;
5380 irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
5381 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
5382 + elf_section_data (o
->output_section
)->rel_count
5383 + elf_section_data (o
->output_section
)->rel_count2
);
5384 for (; irela
< irelaend
; irela
++, rel_hash
++)
5386 unsigned long r_symndx
;
5387 Elf_Internal_Sym
*isym
;
5390 irela
->r_offset
+= o
->output_offset
;
5392 r_symndx
= ELF_R_SYM (irela
->r_info
);
5397 if (r_symndx
>= locsymcount
5398 || (elf_bad_symtab (input_bfd
)
5399 && finfo
->sections
[r_symndx
] == NULL
))
5401 struct elf_link_hash_entry
*rh
;
5404 /* This is a reloc against a global symbol. We
5405 have not yet output all the local symbols, so
5406 we do not know the symbol index of any global
5407 symbol. We set the rel_hash entry for this
5408 reloc to point to the global hash table entry
5409 for this symbol. The symbol index is then
5410 set at the end of elf_bfd_final_link. */
5411 indx
= r_symndx
- extsymoff
;
5412 rh
= elf_sym_hashes (input_bfd
)[indx
];
5413 while (rh
->root
.type
== bfd_link_hash_indirect
5414 || rh
->root
.type
== bfd_link_hash_warning
)
5415 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
5417 /* Setting the index to -2 tells
5418 elf_link_output_extsym that this symbol is
5420 BFD_ASSERT (rh
->indx
< 0);
5428 /* This is a reloc against a local symbol. */
5431 isym
= finfo
->internal_syms
+ r_symndx
;
5432 sec
= finfo
->sections
[r_symndx
];
5433 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
5435 /* I suppose the backend ought to fill in the
5436 section of any STT_SECTION symbol against a
5437 processor specific section. If we have
5438 discarded a section, the output_section will
5439 be the absolute section. */
5441 && (bfd_is_abs_section (sec
)
5442 || (sec
->output_section
!= NULL
5443 && bfd_is_abs_section (sec
->output_section
))))
5445 else if (sec
== NULL
|| sec
->owner
== NULL
)
5447 bfd_set_error (bfd_error_bad_value
);
5452 r_symndx
= sec
->output_section
->target_index
;
5453 BFD_ASSERT (r_symndx
!= 0);
5458 if (finfo
->indices
[r_symndx
] == -1)
5464 if (finfo
->info
->strip
== strip_all
)
5466 /* You can't do ld -r -s. */
5467 bfd_set_error (bfd_error_invalid_operation
);
5471 /* This symbol was skipped earlier, but
5472 since it is needed by a reloc, we
5473 must output it now. */
5474 link
= symtab_hdr
->sh_link
;
5475 name
= bfd_elf_string_from_elf_section (input_bfd
,
5481 osec
= sec
->output_section
;
5483 _bfd_elf_section_from_bfd_section (output_bfd
,
5485 if (isym
->st_shndx
== (unsigned short) -1)
5488 isym
->st_value
+= sec
->output_offset
;
5489 if (! finfo
->info
->relocateable
)
5490 isym
->st_value
+= osec
->vma
;
5492 finfo
->indices
[r_symndx
] = bfd_get_symcount (output_bfd
);
5494 if (! elf_link_output_sym (finfo
, name
, isym
, sec
))
5498 r_symndx
= finfo
->indices
[r_symndx
];
5501 irela
->r_info
= ELF_R_INFO (r_symndx
,
5502 ELF_R_TYPE (irela
->r_info
));
5505 /* Swap out the relocs. */
5506 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
5507 elf_link_output_relocs (output_bfd
, o
,
5511 += input_rel_hdr
->sh_size
/ input_rel_hdr
->sh_entsize
;
5512 input_rel_hdr
= elf_section_data (o
)->rel_hdr2
;
5514 elf_link_output_relocs (output_bfd
, o
,
5520 /* Write out the modified section contents. */
5521 if (elf_section_data (o
)->stab_info
== NULL
)
5523 if (! (o
->flags
& SEC_EXCLUDE
) &&
5524 ! bfd_set_section_contents (output_bfd
, o
->output_section
,
5525 contents
, o
->output_offset
,
5526 (o
->_cooked_size
!= 0
5533 if (! (_bfd_write_section_stabs
5534 (output_bfd
, &elf_hash_table (finfo
->info
)->stab_info
,
5535 o
, &elf_section_data (o
)->stab_info
, contents
)))
5543 /* Generate a reloc when linking an ELF file. This is a reloc
5544 requested by the linker, and does come from any input file. This
5545 is used to build constructor and destructor tables when linking
5549 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
5551 struct bfd_link_info
*info
;
5552 asection
*output_section
;
5553 struct bfd_link_order
*link_order
;
5555 reloc_howto_type
*howto
;
5559 struct elf_link_hash_entry
**rel_hash_ptr
;
5560 Elf_Internal_Shdr
*rel_hdr
;
5562 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5565 bfd_set_error (bfd_error_bad_value
);
5569 addend
= link_order
->u
.reloc
.p
->addend
;
5571 /* Figure out the symbol index. */
5572 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
5573 + elf_section_data (output_section
)->rel_count
5574 + elf_section_data (output_section
)->rel_count2
);
5575 if (link_order
->type
== bfd_section_reloc_link_order
)
5577 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
5578 BFD_ASSERT (indx
!= 0);
5579 *rel_hash_ptr
= NULL
;
5583 struct elf_link_hash_entry
*h
;
5585 /* Treat a reloc against a defined symbol as though it were
5586 actually against the section. */
5587 h
= ((struct elf_link_hash_entry
*)
5588 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
5589 link_order
->u
.reloc
.p
->u
.name
,
5590 false, false, true));
5592 && (h
->root
.type
== bfd_link_hash_defined
5593 || h
->root
.type
== bfd_link_hash_defweak
))
5597 section
= h
->root
.u
.def
.section
;
5598 indx
= section
->output_section
->target_index
;
5599 *rel_hash_ptr
= NULL
;
5600 /* It seems that we ought to add the symbol value to the
5601 addend here, but in practice it has already been added
5602 because it was passed to constructor_callback. */
5603 addend
+= section
->output_section
->vma
+ section
->output_offset
;
5607 /* Setting the index to -2 tells elf_link_output_extsym that
5608 this symbol is used by a reloc. */
5615 if (! ((*info
->callbacks
->unattached_reloc
)
5616 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
5617 (asection
*) NULL
, (bfd_vma
) 0)))
5623 /* If this is an inplace reloc, we must write the addend into the
5625 if (howto
->partial_inplace
&& addend
!= 0)
5628 bfd_reloc_status_type rstat
;
5632 size
= bfd_get_reloc_size (howto
);
5633 buf
= (bfd_byte
*) bfd_zmalloc (size
);
5634 if (buf
== (bfd_byte
*) NULL
)
5636 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5642 case bfd_reloc_outofrange
:
5644 case bfd_reloc_overflow
:
5645 if (! ((*info
->callbacks
->reloc_overflow
)
5647 (link_order
->type
== bfd_section_reloc_link_order
5648 ? bfd_section_name (output_bfd
,
5649 link_order
->u
.reloc
.p
->u
.section
)
5650 : link_order
->u
.reloc
.p
->u
.name
),
5651 howto
->name
, addend
, (bfd
*) NULL
, (asection
*) NULL
,
5659 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
5660 (file_ptr
) link_order
->offset
, size
);
5666 /* The address of a reloc is relative to the section in a
5667 relocateable file, and is a virtual address in an executable
5669 offset
= link_order
->offset
;
5670 if (! info
->relocateable
)
5671 offset
+= output_section
->vma
;
5673 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
5675 if (rel_hdr
->sh_type
== SHT_REL
)
5677 Elf_Internal_Rel irel
;
5678 Elf_External_Rel
*erel
;
5680 irel
.r_offset
= offset
;
5681 irel
.r_info
= ELF_R_INFO (indx
, howto
->type
);
5682 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
5683 + elf_section_data (output_section
)->rel_count
);
5684 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
5688 Elf_Internal_Rela irela
;
5689 Elf_External_Rela
*erela
;
5691 irela
.r_offset
= offset
;
5692 irela
.r_info
= ELF_R_INFO (indx
, howto
->type
);
5693 irela
.r_addend
= addend
;
5694 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
5695 + elf_section_data (output_section
)->rel_count
);
5696 elf_swap_reloca_out (output_bfd
, &irela
, erela
);
5699 ++elf_section_data (output_section
)->rel_count
;
5705 /* Allocate a pointer to live in a linker created section. */
5708 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
5710 struct bfd_link_info
*info
;
5711 elf_linker_section_t
*lsect
;
5712 struct elf_link_hash_entry
*h
;
5713 const Elf_Internal_Rela
*rel
;
5715 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
5716 elf_linker_section_pointers_t
*linker_section_ptr
;
5717 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);;
5719 BFD_ASSERT (lsect
!= NULL
);
5721 /* Is this a global symbol? */
5724 /* Has this symbol already been allocated, if so, our work is done */
5725 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
5730 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
5731 /* Make sure this symbol is output as a dynamic symbol. */
5732 if (h
->dynindx
== -1)
5734 if (! elf_link_record_dynamic_symbol (info
, h
))
5738 if (lsect
->rel_section
)
5739 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
5742 else /* Allocation of a pointer to a local symbol */
5744 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
5746 /* Allocate a table to hold the local symbols if first time */
5749 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
5750 register unsigned int i
;
5752 ptr
= (elf_linker_section_pointers_t
**)
5753 bfd_alloc (abfd
, num_symbols
* sizeof (elf_linker_section_pointers_t
*));
5758 elf_local_ptr_offsets (abfd
) = ptr
;
5759 for (i
= 0; i
< num_symbols
; i
++)
5760 ptr
[i
] = (elf_linker_section_pointers_t
*)0;
5763 /* Has this symbol already been allocated, if so, our work is done */
5764 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
5769 ptr_linker_section_ptr
= &ptr
[r_symndx
];
5773 /* If we are generating a shared object, we need to
5774 output a R_<xxx>_RELATIVE reloc so that the
5775 dynamic linker can adjust this GOT entry. */
5776 BFD_ASSERT (lsect
->rel_section
!= NULL
);
5777 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
5781 /* Allocate space for a pointer in the linker section, and allocate a new pointer record
5782 from internal memory. */
5783 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
5784 linker_section_ptr
= (elf_linker_section_pointers_t
*)
5785 bfd_alloc (abfd
, sizeof (elf_linker_section_pointers_t
));
5787 if (!linker_section_ptr
)
5790 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
5791 linker_section_ptr
->addend
= rel
->r_addend
;
5792 linker_section_ptr
->which
= lsect
->which
;
5793 linker_section_ptr
->written_address_p
= false;
5794 *ptr_linker_section_ptr
= linker_section_ptr
;
5797 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
5799 linker_section_ptr
->offset
= lsect
->section
->_raw_size
- lsect
->hole_size
+ (ARCH_SIZE
/ 8);
5800 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
5801 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
5802 if (lsect
->sym_hash
) /* Bump up symbol value if needed */
5804 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
5806 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
5807 lsect
->sym_hash
->root
.root
.string
,
5808 (long)ARCH_SIZE
/ 8,
5809 (long)lsect
->sym_hash
->root
.u
.def
.value
);
5815 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
5817 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
5820 fprintf (stderr
, "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
5821 lsect
->name
, (long)linker_section_ptr
->offset
, (long)lsect
->section
->_raw_size
);
5829 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
5832 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
5835 /* Fill in the address for a pointer generated in alinker section. */
5838 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
, relocation
, rel
, relative_reloc
)
5841 struct bfd_link_info
*info
;
5842 elf_linker_section_t
*lsect
;
5843 struct elf_link_hash_entry
*h
;
5845 const Elf_Internal_Rela
*rel
;
5848 elf_linker_section_pointers_t
*linker_section_ptr
;
5850 BFD_ASSERT (lsect
!= NULL
);
5852 if (h
!= NULL
) /* global symbol */
5854 linker_section_ptr
= _bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
5858 BFD_ASSERT (linker_section_ptr
!= NULL
);
5860 if (! elf_hash_table (info
)->dynamic_sections_created
5863 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
5865 /* This is actually a static link, or it is a
5866 -Bsymbolic link and the symbol is defined
5867 locally. We must initialize this entry in the
5870 When doing a dynamic link, we create a .rela.<xxx>
5871 relocation entry to initialize the value. This
5872 is done in the finish_dynamic_symbol routine. */
5873 if (!linker_section_ptr
->written_address_p
)
5875 linker_section_ptr
->written_address_p
= true;
5876 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
5877 lsect
->section
->contents
+ linker_section_ptr
->offset
);
5881 else /* local symbol */
5883 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
5884 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
5885 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
5886 linker_section_ptr
= _bfd_elf_find_pointer_linker_section (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
5890 BFD_ASSERT (linker_section_ptr
!= NULL
);
5892 /* Write out pointer if it hasn't been rewritten out before */
5893 if (!linker_section_ptr
->written_address_p
)
5895 linker_section_ptr
->written_address_p
= true;
5896 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
5897 lsect
->section
->contents
+ linker_section_ptr
->offset
);
5901 asection
*srel
= lsect
->rel_section
;
5902 Elf_Internal_Rela outrel
;
5904 /* We need to generate a relative reloc for the dynamic linker. */
5906 lsect
->rel_section
= srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
5909 BFD_ASSERT (srel
!= NULL
);
5911 outrel
.r_offset
= (lsect
->section
->output_section
->vma
5912 + lsect
->section
->output_offset
5913 + linker_section_ptr
->offset
);
5914 outrel
.r_info
= ELF_R_INFO (0, relative_reloc
);
5915 outrel
.r_addend
= 0;
5916 elf_swap_reloca_out (output_bfd
, &outrel
,
5917 (((Elf_External_Rela
*)
5918 lsect
->section
->contents
)
5919 + elf_section_data (lsect
->section
)->rel_count
));
5920 ++elf_section_data (lsect
->section
)->rel_count
;
5925 relocation
= (lsect
->section
->output_offset
5926 + linker_section_ptr
->offset
5927 - lsect
->hole_offset
5928 - lsect
->sym_offset
);
5931 fprintf (stderr
, "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
5932 lsect
->name
, (long)relocation
, (long)relocation
);
5935 /* Subtract out the addend, because it will get added back in by the normal
5937 return relocation
- linker_section_ptr
->addend
;
5940 /* Garbage collect unused sections. */
5942 static boolean elf_gc_mark
5943 PARAMS ((struct bfd_link_info
*info
, asection
*sec
,
5944 asection
* (*gc_mark_hook
)
5945 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
5946 struct elf_link_hash_entry
*, Elf_Internal_Sym
*))));
5948 static boolean elf_gc_sweep
5949 PARAMS ((struct bfd_link_info
*info
,
5950 boolean (*gc_sweep_hook
)
5951 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
5952 const Elf_Internal_Rela
*relocs
))));
5954 static boolean elf_gc_sweep_symbol
5955 PARAMS ((struct elf_link_hash_entry
*h
, PTR idxptr
));
5957 static boolean elf_gc_allocate_got_offsets
5958 PARAMS ((struct elf_link_hash_entry
*h
, PTR offarg
));
5960 static boolean elf_gc_propagate_vtable_entries_used
5961 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
5963 static boolean elf_gc_smash_unused_vtentry_relocs
5964 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
5966 /* The mark phase of garbage collection. For a given section, mark
5967 it, and all the sections which define symbols to which it refers. */
5970 elf_gc_mark (info
, sec
, gc_mark_hook
)
5971 struct bfd_link_info
*info
;
5973 asection
* (*gc_mark_hook
)
5974 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
5975 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
5981 /* Look through the section relocs. */
5983 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
5985 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
5986 Elf_Internal_Shdr
*symtab_hdr
;
5987 struct elf_link_hash_entry
**sym_hashes
;
5990 Elf_External_Sym
*locsyms
, *freesyms
= NULL
;
5991 bfd
*input_bfd
= sec
->owner
;
5992 struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
5994 /* GCFIXME: how to arrange so that relocs and symbols are not
5995 reread continually? */
5997 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
5998 sym_hashes
= elf_sym_hashes (input_bfd
);
6000 /* Read the local symbols. */
6001 if (elf_bad_symtab (input_bfd
))
6003 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6007 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
6008 if (symtab_hdr
->contents
)
6009 locsyms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
6010 else if (nlocsyms
== 0)
6014 locsyms
= freesyms
=
6015 bfd_malloc (nlocsyms
* sizeof (Elf_External_Sym
));
6016 if (freesyms
== NULL
6017 || bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
6018 || (bfd_read (locsyms
, sizeof (Elf_External_Sym
),
6019 nlocsyms
, input_bfd
)
6020 != nlocsyms
* sizeof (Elf_External_Sym
)))
6027 /* Read the relocations. */
6028 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
6029 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
6030 info
->keep_memory
));
6031 if (relstart
== NULL
)
6036 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6038 for (rel
= relstart
; rel
< relend
; rel
++)
6040 unsigned long r_symndx
;
6042 struct elf_link_hash_entry
*h
;
6045 r_symndx
= ELF_R_SYM (rel
->r_info
);
6049 if (elf_bad_symtab (sec
->owner
))
6051 elf_swap_symbol_in (input_bfd
, &locsyms
[r_symndx
], &s
);
6052 if (ELF_ST_BIND (s
.st_info
) == STB_LOCAL
)
6053 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, NULL
, &s
);
6056 h
= sym_hashes
[r_symndx
- extsymoff
];
6057 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, h
, NULL
);
6060 else if (r_symndx
>= nlocsyms
)
6062 h
= sym_hashes
[r_symndx
- extsymoff
];
6063 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, h
, NULL
);
6067 elf_swap_symbol_in (input_bfd
, &locsyms
[r_symndx
], &s
);
6068 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, NULL
, &s
);
6071 if (rsec
&& !rsec
->gc_mark
)
6072 if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
6080 if (!info
->keep_memory
)
6090 /* The sweep phase of garbage collection. Remove all garbage sections. */
6093 elf_gc_sweep (info
, gc_sweep_hook
)
6094 struct bfd_link_info
*info
;
6095 boolean (*gc_sweep_hook
)
6096 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
6097 const Elf_Internal_Rela
*relocs
));
6101 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
6105 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6107 /* Keep special sections. Keep .debug sections. */
6108 if ((o
->flags
& SEC_LINKER_CREATED
)
6109 || (o
->flags
& SEC_DEBUGGING
))
6115 /* Skip sweeping sections already excluded. */
6116 if (o
->flags
& SEC_EXCLUDE
)
6119 /* Since this is early in the link process, it is simple
6120 to remove a section from the output. */
6121 o
->flags
|= SEC_EXCLUDE
;
6123 /* But we also have to update some of the relocation
6124 info we collected before. */
6126 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
6128 Elf_Internal_Rela
*internal_relocs
;
6131 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
6132 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
6133 if (internal_relocs
== NULL
)
6136 r
= (*gc_sweep_hook
)(o
->owner
, info
, o
, internal_relocs
);
6138 if (!info
->keep_memory
)
6139 free (internal_relocs
);
6147 /* Remove the symbols that were in the swept sections from the dynamic
6148 symbol table. GCFIXME: Anyone know how to get them out of the
6149 static symbol table as well? */
6153 elf_link_hash_traverse (elf_hash_table (info
),
6154 elf_gc_sweep_symbol
,
6157 elf_hash_table (info
)->dynsymcount
= i
;
6163 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6166 elf_gc_sweep_symbol (h
, idxptr
)
6167 struct elf_link_hash_entry
*h
;
6170 int *idx
= (int *) idxptr
;
6172 if (h
->dynindx
!= -1
6173 && ((h
->root
.type
!= bfd_link_hash_defined
6174 && h
->root
.type
!= bfd_link_hash_defweak
)
6175 || h
->root
.u
.def
.section
->gc_mark
))
6176 h
->dynindx
= (*idx
)++;
6181 /* Propogate collected vtable information. This is called through
6182 elf_link_hash_traverse. */
6185 elf_gc_propagate_vtable_entries_used (h
, okp
)
6186 struct elf_link_hash_entry
*h
;
6189 /* Those that are not vtables. */
6190 if (h
->vtable_parent
== NULL
)
6193 /* Those vtables that do not have parents, we cannot merge. */
6194 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
6197 /* If we've already been done, exit. */
6198 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
6201 /* Make sure the parent's table is up to date. */
6202 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
6204 if (h
->vtable_entries_used
== NULL
)
6206 /* None of this table's entries were referenced. Re-use the
6208 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
6209 h
->vtable_entries_size
= h
->vtable_parent
->vtable_entries_size
;
6216 /* Or the parent's entries into ours. */
6217 cu
= h
->vtable_entries_used
;
6219 pu
= h
->vtable_parent
->vtable_entries_used
;
6222 n
= h
->vtable_parent
->vtable_entries_size
/ FILE_ALIGN
;
6225 if (*pu
) *cu
= true;
6235 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
6236 struct elf_link_hash_entry
*h
;
6240 bfd_vma hstart
, hend
;
6241 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
6242 struct elf_backend_data
*bed
;
6244 /* Take care of both those symbols that do not describe vtables as
6245 well as those that are not loaded. */
6246 if (h
->vtable_parent
== NULL
)
6249 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
6250 || h
->root
.type
== bfd_link_hash_defweak
);
6252 sec
= h
->root
.u
.def
.section
;
6253 hstart
= h
->root
.u
.def
.value
;
6254 hend
= hstart
+ h
->size
;
6256 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
6257 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, true));
6259 return *(boolean
*)okp
= false;
6260 bed
= get_elf_backend_data (sec
->owner
);
6261 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6263 for (rel
= relstart
; rel
< relend
; ++rel
)
6264 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
6266 /* If the entry is in use, do nothing. */
6267 if (h
->vtable_entries_used
6268 && (rel
->r_offset
- hstart
) < h
->vtable_entries_size
)
6270 bfd_vma entry
= (rel
->r_offset
- hstart
) / FILE_ALIGN
;
6271 if (h
->vtable_entries_used
[entry
])
6274 /* Otherwise, kill it. */
6275 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
6281 /* Do mark and sweep of unused sections. */
6284 elf_gc_sections (abfd
, info
)
6286 struct bfd_link_info
*info
;
6290 asection
* (*gc_mark_hook
)
6291 PARAMS ((bfd
*abfd
, struct bfd_link_info
*, Elf_Internal_Rela
*,
6292 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
6294 if (!get_elf_backend_data (abfd
)->can_gc_sections
6295 || info
->relocateable
6296 || elf_hash_table (info
)->dynamic_sections_created
)
6299 /* Apply transitive closure to the vtable entry usage info. */
6300 elf_link_hash_traverse (elf_hash_table (info
),
6301 elf_gc_propagate_vtable_entries_used
,
6306 /* Kill the vtable relocations that were not used. */
6307 elf_link_hash_traverse (elf_hash_table (info
),
6308 elf_gc_smash_unused_vtentry_relocs
,
6313 /* Grovel through relocs to find out who stays ... */
6315 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
6316 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
6319 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6321 if (o
->flags
& SEC_KEEP
)
6322 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
6327 /* ... and mark SEC_EXCLUDE for those that go. */
6328 if (!elf_gc_sweep(info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
6334 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
6337 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
6340 struct elf_link_hash_entry
*h
;
6343 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
6344 struct elf_link_hash_entry
**search
, *child
;
6345 bfd_size_type extsymcount
;
6347 /* The sh_info field of the symtab header tells us where the
6348 external symbols start. We don't care about the local symbols at
6350 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
6351 if (!elf_bad_symtab (abfd
))
6352 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
6354 sym_hashes
= elf_sym_hashes (abfd
);
6355 sym_hashes_end
= sym_hashes
+ extsymcount
;
6357 /* Hunt down the child symbol, which is in this section at the same
6358 offset as the relocation. */
6359 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
6361 if ((child
= *search
) != NULL
6362 && (child
->root
.type
== bfd_link_hash_defined
6363 || child
->root
.type
== bfd_link_hash_defweak
)
6364 && child
->root
.u
.def
.section
== sec
6365 && child
->root
.u
.def
.value
== offset
)
6369 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
6370 bfd_get_filename (abfd
), sec
->name
,
6371 (unsigned long)offset
);
6372 bfd_set_error (bfd_error_invalid_operation
);
6378 /* This *should* only be the absolute section. It could potentially
6379 be that someone has defined a non-global vtable though, which
6380 would be bad. It isn't worth paging in the local symbols to be
6381 sure though; that case should simply be handled by the assembler. */
6383 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
6386 child
->vtable_parent
= h
;
6391 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
6394 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
6395 bfd
*abfd ATTRIBUTE_UNUSED
;
6396 asection
*sec ATTRIBUTE_UNUSED
;
6397 struct elf_link_hash_entry
*h
;
6400 if (addend
>= h
->vtable_entries_size
)
6403 boolean
*ptr
= h
->vtable_entries_used
;
6405 /* While the symbol is undefined, we have to be prepared to handle
6407 if (h
->root
.type
== bfd_link_hash_undefined
)
6414 /* Oops! We've got a reference past the defined end of
6415 the table. This is probably a bug -- shall we warn? */
6420 /* Allocate one extra entry for use as a "done" flag for the
6421 consolidation pass. */
6422 bytes
= (size
/ FILE_ALIGN
+ 1) * sizeof(boolean
);
6428 ptr
= realloc (ptr
-1, bytes
);
6432 oldbytes
= (h
->vtable_entries_size
/FILE_ALIGN
+ 1) * sizeof(boolean
);
6433 memset (((char *)ptr
) + oldbytes
, 0, bytes
- oldbytes
);
6437 ptr
= calloc (1, bytes
);
6442 /* And arrange for that done flag to be at index -1. */
6443 h
->vtable_entries_used
= ptr
+1;
6444 h
->vtable_entries_size
= size
;
6446 h
->vtable_entries_used
[addend
/ FILE_ALIGN
] = true;
6451 /* And an accompanying bit to work out final got entry offsets once
6452 we're done. Should be called from final_link. */
6455 elf_gc_common_finalize_got_offsets (abfd
, info
)
6457 struct bfd_link_info
*info
;
6460 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6463 /* The GOT offset is relative to the .got section, but the GOT header is
6464 put into the .got.plt section, if the backend uses it. */
6465 if (bed
->want_got_plt
)
6468 gotoff
= bed
->got_header_size
;
6470 /* Do the local .got entries first. */
6471 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
6473 bfd_signed_vma
*local_got
= elf_local_got_refcounts (i
);
6474 bfd_size_type j
, locsymcount
;
6475 Elf_Internal_Shdr
*symtab_hdr
;
6480 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
6481 if (elf_bad_symtab (i
))
6482 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
6484 locsymcount
= symtab_hdr
->sh_info
;
6486 for (j
= 0; j
< locsymcount
; ++j
)
6488 if (local_got
[j
] > 0)
6490 local_got
[j
] = gotoff
;
6491 gotoff
+= ARCH_SIZE
/ 8;
6494 local_got
[j
] = (bfd_vma
) -1;
6498 /* Then the global .got and .plt entries. */
6499 elf_link_hash_traverse (elf_hash_table (info
),
6500 elf_gc_allocate_got_offsets
,
6505 /* We need a special top-level link routine to convert got reference counts
6506 to real got offsets. */
6509 elf_gc_allocate_got_offsets (h
, offarg
)
6510 struct elf_link_hash_entry
*h
;
6513 bfd_vma
*off
= (bfd_vma
*) offarg
;
6515 if (h
->got
.refcount
> 0)
6517 h
->got
.offset
= off
[0];
6518 off
[0] += ARCH_SIZE
/ 8;
6521 h
->got
.offset
= (bfd_vma
) -1;
6526 /* Many folk need no more in the way of final link than this, once
6527 got entry reference counting is enabled. */
6530 elf_gc_common_final_link (abfd
, info
)
6532 struct bfd_link_info
*info
;
6534 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
6537 /* Invoke the regular ELF backend linker to do all the work. */
6538 return elf_bfd_final_link (abfd
, info
);
6541 /* This function will be called though elf_link_hash_traverse to store
6542 all hash value of the exported symbols in an array. */
6545 elf_collect_hash_codes (h
, data
)
6546 struct elf_link_hash_entry
*h
;
6549 unsigned long **valuep
= (unsigned long **) data
;
6555 /* Ignore indirect symbols. These are added by the versioning code. */
6556 if (h
->dynindx
== -1)
6559 name
= h
->root
.root
.string
;
6560 p
= strchr (name
, ELF_VER_CHR
);
6563 alc
= bfd_malloc (p
- name
+ 1);
6564 memcpy (alc
, name
, p
- name
);
6565 alc
[p
- name
] = '\0';
6569 /* Compute the hash value. */
6570 ha
= bfd_elf_hash (name
);
6572 /* Store the found hash value in the array given as the argument. */
6575 /* And store it in the struct so that we can put it in the hash table
6577 h
->elf_hash_value
= ha
;