2 Copyright 1995, 1996, 1997, 1998 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_link_renumber_dynsyms
52 PARAMS ((struct elf_link_hash_entry
*, PTR
));
53 static boolean elf_collect_hash_codes
54 PARAMS ((struct elf_link_hash_entry
*, PTR
));
56 /* Given an ELF BFD, add symbols to the global hash table as
60 elf_bfd_link_add_symbols (abfd
, info
)
62 struct bfd_link_info
*info
;
64 switch (bfd_get_format (abfd
))
67 return elf_link_add_object_symbols (abfd
, info
);
69 return elf_link_add_archive_symbols (abfd
, info
);
71 bfd_set_error (bfd_error_wrong_format
);
77 /* Add symbols from an ELF archive file to the linker hash table. We
78 don't use _bfd_generic_link_add_archive_symbols because of a
79 problem which arises on UnixWare. The UnixWare libc.so is an
80 archive which includes an entry libc.so.1 which defines a bunch of
81 symbols. The libc.so archive also includes a number of other
82 object files, which also define symbols, some of which are the same
83 as those defined in libc.so.1. Correct linking requires that we
84 consider each object file in turn, and include it if it defines any
85 symbols we need. _bfd_generic_link_add_archive_symbols does not do
86 this; it looks through the list of undefined symbols, and includes
87 any object file which defines them. When this algorithm is used on
88 UnixWare, it winds up pulling in libc.so.1 early and defining a
89 bunch of symbols. This means that some of the other objects in the
90 archive are not included in the link, which is incorrect since they
91 precede libc.so.1 in the archive.
93 Fortunately, ELF archive handling is simpler than that done by
94 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
95 oddities. In ELF, if we find a symbol in the archive map, and the
96 symbol is currently undefined, we know that we must pull in that
99 Unfortunately, we do have to make multiple passes over the symbol
100 table until nothing further is resolved. */
103 elf_link_add_archive_symbols (abfd
, info
)
105 struct bfd_link_info
*info
;
108 boolean
*defined
= NULL
;
109 boolean
*included
= NULL
;
113 if (! bfd_has_map (abfd
))
115 /* An empty archive is a special case. */
116 if (bfd_openr_next_archived_file (abfd
, (bfd
*) NULL
) == NULL
)
118 bfd_set_error (bfd_error_no_armap
);
122 /* Keep track of all symbols we know to be already defined, and all
123 files we know to be already included. This is to speed up the
124 second and subsequent passes. */
125 c
= bfd_ardata (abfd
)->symdef_count
;
128 defined
= (boolean
*) bfd_malloc (c
* sizeof (boolean
));
129 included
= (boolean
*) bfd_malloc (c
* sizeof (boolean
));
130 if (defined
== (boolean
*) NULL
|| included
== (boolean
*) NULL
)
132 memset (defined
, 0, c
* sizeof (boolean
));
133 memset (included
, 0, c
* sizeof (boolean
));
135 symdefs
= bfd_ardata (abfd
)->symdefs
;
148 symdefend
= symdef
+ c
;
149 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
151 struct elf_link_hash_entry
*h
;
153 struct bfd_link_hash_entry
*undefs_tail
;
156 if (defined
[i
] || included
[i
])
158 if (symdef
->file_offset
== last
)
164 h
= elf_link_hash_lookup (elf_hash_table (info
), symdef
->name
,
165 false, false, false);
171 /* If this is a default version (the name contains @@),
172 look up the symbol again without the version. The
173 effect is that references to the symbol without the
174 version will be matched by the default symbol in the
177 p
= strchr (symdef
->name
, ELF_VER_CHR
);
178 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
181 copy
= bfd_alloc (abfd
, p
- symdef
->name
+ 1);
184 memcpy (copy
, symdef
->name
, p
- symdef
->name
);
185 copy
[p
- symdef
->name
] = '\0';
187 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
188 false, false, false);
190 bfd_release (abfd
, copy
);
196 if (h
->root
.type
!= bfd_link_hash_undefined
)
198 if (h
->root
.type
!= bfd_link_hash_undefweak
)
203 /* We need to include this archive member. */
205 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
206 if (element
== (bfd
*) NULL
)
209 if (! bfd_check_format (element
, bfd_object
))
212 /* Doublecheck that we have not included this object
213 already--it should be impossible, but there may be
214 something wrong with the archive. */
215 if (element
->archive_pass
!= 0)
217 bfd_set_error (bfd_error_bad_value
);
220 element
->archive_pass
= 1;
222 undefs_tail
= info
->hash
->undefs_tail
;
224 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
227 if (! elf_link_add_object_symbols (element
, info
))
230 /* If there are any new undefined symbols, we need to make
231 another pass through the archive in order to see whether
232 they can be defined. FIXME: This isn't perfect, because
233 common symbols wind up on undefs_tail and because an
234 undefined symbol which is defined later on in this pass
235 does not require another pass. This isn't a bug, but it
236 does make the code less efficient than it could be. */
237 if (undefs_tail
!= info
->hash
->undefs_tail
)
240 /* Look backward to mark all symbols from this object file
241 which we have already seen in this pass. */
245 included
[mark
] = true;
250 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
252 /* We mark subsequent symbols from this object file as we go
253 on through the loop. */
254 last
= symdef
->file_offset
;
265 if (defined
!= (boolean
*) NULL
)
267 if (included
!= (boolean
*) NULL
)
272 /* This function is called when we want to define a new symbol. It
273 handles the various cases which arise when we find a definition in
274 a dynamic object, or when there is already a definition in a
275 dynamic object. The new symbol is described by NAME, SYM, PSEC,
276 and PVALUE. We set SYM_HASH to the hash table entry. We set
277 OVERRIDE if the old symbol is overriding a new definition. We set
278 TYPE_CHANGE_OK if it is OK for the type to change. We set
279 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
280 change, we mean that we shouldn't warn if the type or size does
284 elf_merge_symbol (abfd
, info
, name
, sym
, psec
, pvalue
, sym_hash
,
285 override
, type_change_ok
, size_change_ok
)
287 struct bfd_link_info
*info
;
289 Elf_Internal_Sym
*sym
;
292 struct elf_link_hash_entry
**sym_hash
;
294 boolean
*type_change_ok
;
295 boolean
*size_change_ok
;
298 struct elf_link_hash_entry
*h
;
301 boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
306 bind
= ELF_ST_BIND (sym
->st_info
);
308 if (! bfd_is_und_section (sec
))
309 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, false, false);
311 h
= ((struct elf_link_hash_entry
*)
312 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true, false, false));
317 /* This code is for coping with dynamic objects, and is only useful
318 if we are doing an ELF link. */
319 if (info
->hash
->creator
!= abfd
->xvec
)
322 /* For merging, we only care about real symbols. */
324 while (h
->root
.type
== bfd_link_hash_indirect
325 || h
->root
.type
== bfd_link_hash_warning
)
326 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
328 /* If we just created the symbol, mark it as being an ELF symbol.
329 Other than that, there is nothing to do--there is no merge issue
330 with a newly defined symbol--so we just return. */
332 if (h
->root
.type
== bfd_link_hash_new
)
334 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
338 /* OLDBFD is a BFD associated with the existing symbol. */
340 switch (h
->root
.type
)
346 case bfd_link_hash_undefined
:
347 case bfd_link_hash_undefweak
:
348 oldbfd
= h
->root
.u
.undef
.abfd
;
351 case bfd_link_hash_defined
:
352 case bfd_link_hash_defweak
:
353 oldbfd
= h
->root
.u
.def
.section
->owner
;
356 case bfd_link_hash_common
:
357 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
361 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
362 respectively, is from a dynamic object. */
364 if ((abfd
->flags
& DYNAMIC
) != 0)
369 if (oldbfd
== NULL
|| (oldbfd
->flags
& DYNAMIC
) == 0)
374 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
375 respectively, appear to be a definition rather than reference. */
377 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
382 if (h
->root
.type
== bfd_link_hash_undefined
383 || h
->root
.type
== bfd_link_hash_undefweak
384 || h
->root
.type
== bfd_link_hash_common
)
389 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
390 symbol, respectively, appears to be a common symbol in a dynamic
391 object. If a symbol appears in an uninitialized section, and is
392 not weak, and is not a function, then it may be a common symbol
393 which was resolved when the dynamic object was created. We want
394 to treat such symbols specially, because they raise special
395 considerations when setting the symbol size: if the symbol
396 appears as a common symbol in a regular object, and the size in
397 the regular object is larger, we must make sure that we use the
398 larger size. This problematic case can always be avoided in C,
399 but it must be handled correctly when using Fortran shared
402 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
403 likewise for OLDDYNCOMMON and OLDDEF.
405 Note that this test is just a heuristic, and that it is quite
406 possible to have an uninitialized symbol in a shared object which
407 is really a definition, rather than a common symbol. This could
408 lead to some minor confusion when the symbol really is a common
409 symbol in some regular object. However, I think it will be
414 && (sec
->flags
& SEC_ALLOC
) != 0
415 && (sec
->flags
& SEC_LOAD
) == 0
418 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
421 newdyncommon
= false;
425 && h
->root
.type
== bfd_link_hash_defined
426 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
427 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
428 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
430 && h
->type
!= STT_FUNC
)
433 olddyncommon
= false;
435 /* It's OK to change the type if either the existing symbol or the
436 new symbol is weak. */
438 if (h
->root
.type
== bfd_link_hash_defweak
439 || h
->root
.type
== bfd_link_hash_undefweak
441 *type_change_ok
= true;
443 /* It's OK to change the size if either the existing symbol or the
444 new symbol is weak, or if the old symbol is undefined. */
447 || h
->root
.type
== bfd_link_hash_undefined
)
448 *size_change_ok
= true;
450 /* If both the old and the new symbols look like common symbols in a
451 dynamic object, set the size of the symbol to the larger of the
456 && sym
->st_size
!= h
->size
)
458 /* Since we think we have two common symbols, issue a multiple
459 common warning if desired. Note that we only warn if the
460 size is different. If the size is the same, we simply let
461 the old symbol override the new one as normally happens with
462 symbols defined in dynamic objects. */
464 if (! ((*info
->callbacks
->multiple_common
)
465 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
466 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
469 if (sym
->st_size
> h
->size
)
470 h
->size
= sym
->st_size
;
472 *size_change_ok
= true;
475 /* If we are looking at a dynamic object, and we have found a
476 definition, we need to see if the symbol was already defined by
477 some other object. If so, we want to use the existing
478 definition, and we do not want to report a multiple symbol
479 definition error; we do this by clobbering *PSEC to be
482 We treat a common symbol as a definition if the symbol in the
483 shared library is a function, since common symbols always
484 represent variables; this can cause confusion in principle, but
485 any such confusion would seem to indicate an erroneous program or
486 shared library. We also permit a common symbol in a regular
487 object to override a weak symbol in a shared object. */
492 || (h
->root
.type
== bfd_link_hash_common
494 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
498 newdyncommon
= false;
500 *psec
= sec
= bfd_und_section_ptr
;
501 *size_change_ok
= true;
503 /* If we get here when the old symbol is a common symbol, then
504 we are explicitly letting it override a weak symbol or
505 function in a dynamic object, and we don't want to warn about
506 a type change. If the old symbol is a defined symbol, a type
507 change warning may still be appropriate. */
509 if (h
->root
.type
== bfd_link_hash_common
)
510 *type_change_ok
= true;
513 /* Handle the special case of an old common symbol merging with a
514 new symbol which looks like a common symbol in a shared object.
515 We change *PSEC and *PVALUE to make the new symbol look like a
516 common symbol, and let _bfd_generic_link_add_one_symbol will do
520 && h
->root
.type
== bfd_link_hash_common
)
524 newdyncommon
= false;
525 *pvalue
= sym
->st_size
;
526 *psec
= sec
= bfd_com_section_ptr
;
527 *size_change_ok
= true;
530 /* If the old symbol is from a dynamic object, and the new symbol is
531 a definition which is not from a dynamic object, then the new
532 symbol overrides the old symbol. Symbols from regular files
533 always take precedence over symbols from dynamic objects, even if
534 they are defined after the dynamic object in the link.
536 As above, we again permit a common symbol in a regular object to
537 override a definition in a shared object if the shared object
538 symbol is a function or is weak. */
542 || (bfd_is_com_section (sec
)
543 && (h
->root
.type
== bfd_link_hash_defweak
544 || h
->type
== STT_FUNC
)))
547 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
549 /* Change the hash table entry to undefined, and let
550 _bfd_generic_link_add_one_symbol do the right thing with the
553 h
->root
.type
= bfd_link_hash_undefined
;
554 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
555 *size_change_ok
= true;
558 olddyncommon
= false;
560 /* We again permit a type change when a common symbol may be
561 overriding a function. */
563 if (bfd_is_com_section (sec
))
564 *type_change_ok
= true;
566 /* This union may have been set to be non-NULL when this symbol
567 was seen in a dynamic object. We must force the union to be
568 NULL, so that it is correct for a regular symbol. */
570 h
->verinfo
.vertree
= NULL
;
572 /* In this special case, if H is the target of an indirection,
573 we want the caller to frob with H rather than with the
574 indirect symbol. That will permit the caller to redefine the
575 target of the indirection, rather than the indirect symbol
576 itself. FIXME: This will break the -y option if we store a
577 symbol with a different name. */
581 /* Handle the special case of a new common symbol merging with an
582 old symbol that looks like it might be a common symbol defined in
583 a shared object. Note that we have already handled the case in
584 which a new common symbol should simply override the definition
585 in the shared library. */
588 && bfd_is_com_section (sec
)
591 /* It would be best if we could set the hash table entry to a
592 common symbol, but we don't know what to use for the section
594 if (! ((*info
->callbacks
->multiple_common
)
595 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
596 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
599 /* If the predumed common symbol in the dynamic object is
600 larger, pretend that the new symbol has its size. */
602 if (h
->size
> *pvalue
)
605 /* FIXME: We no longer know the alignment required by the symbol
606 in the dynamic object, so we just wind up using the one from
607 the regular object. */
610 olddyncommon
= false;
612 h
->root
.type
= bfd_link_hash_undefined
;
613 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
615 *size_change_ok
= true;
616 *type_change_ok
= true;
618 h
->verinfo
.vertree
= NULL
;
624 /* Add symbols from an ELF object file to the linker hash table. */
627 elf_link_add_object_symbols (abfd
, info
)
629 struct bfd_link_info
*info
;
631 boolean (*add_symbol_hook
) PARAMS ((bfd
*, struct bfd_link_info
*,
632 const Elf_Internal_Sym
*,
633 const char **, flagword
*,
634 asection
**, bfd_vma
*));
635 boolean (*check_relocs
) PARAMS ((bfd
*, struct bfd_link_info
*,
636 asection
*, const Elf_Internal_Rela
*));
638 Elf_Internal_Shdr
*hdr
;
642 Elf_External_Sym
*buf
= NULL
;
643 struct elf_link_hash_entry
**sym_hash
;
645 bfd_byte
*dynver
= NULL
;
646 Elf_External_Versym
*extversym
= NULL
;
647 Elf_External_Versym
*ever
;
648 Elf_External_Dyn
*dynbuf
= NULL
;
649 struct elf_link_hash_entry
*weaks
;
650 Elf_External_Sym
*esym
;
651 Elf_External_Sym
*esymend
;
653 add_symbol_hook
= get_elf_backend_data (abfd
)->elf_add_symbol_hook
;
654 collect
= get_elf_backend_data (abfd
)->collect
;
656 if ((abfd
->flags
& DYNAMIC
) == 0)
662 /* You can't use -r against a dynamic object. Also, there's no
663 hope of using a dynamic object which does not exactly match
664 the format of the output file. */
665 if (info
->relocateable
|| info
->hash
->creator
!= abfd
->xvec
)
667 bfd_set_error (bfd_error_invalid_operation
);
672 /* As a GNU extension, any input sections which are named
673 .gnu.warning.SYMBOL are treated as warning symbols for the given
674 symbol. This differs from .gnu.warning sections, which generate
675 warnings when they are included in an output file. */
680 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
684 name
= bfd_get_section_name (abfd
, s
);
685 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
690 name
+= sizeof ".gnu.warning." - 1;
692 /* If this is a shared object, then look up the symbol
693 in the hash table. If it is there, and it is already
694 been defined, then we will not be using the entry
695 from this shared object, so we don't need to warn.
696 FIXME: If we see the definition in a regular object
697 later on, we will warn, but we shouldn't. The only
698 fix is to keep track of what warnings we are supposed
699 to emit, and then handle them all at the end of the
701 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
703 struct elf_link_hash_entry
*h
;
705 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
708 /* FIXME: What about bfd_link_hash_common? */
710 && (h
->root
.type
== bfd_link_hash_defined
711 || h
->root
.type
== bfd_link_hash_defweak
))
713 /* We don't want to issue this warning. Clobber
714 the section size so that the warning does not
715 get copied into the output file. */
721 sz
= bfd_section_size (abfd
, s
);
722 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
726 if (! bfd_get_section_contents (abfd
, s
, msg
, (file_ptr
) 0, sz
))
731 if (! (_bfd_generic_link_add_one_symbol
732 (info
, abfd
, name
, BSF_WARNING
, s
, (bfd_vma
) 0, msg
,
733 false, collect
, (struct bfd_link_hash_entry
**) NULL
)))
736 if (! info
->relocateable
)
738 /* Clobber the section size so that the warning does
739 not get copied into the output file. */
746 /* If this is a dynamic object, we always link against the .dynsym
747 symbol table, not the .symtab symbol table. The dynamic linker
748 will only see the .dynsym symbol table, so there is no reason to
749 look at .symtab for a dynamic object. */
751 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
752 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
754 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
758 /* Read in any version definitions. */
760 if (! _bfd_elf_slurp_version_tables (abfd
))
763 /* Read in the symbol versions, but don't bother to convert them
764 to internal format. */
765 if (elf_dynversym (abfd
) != 0)
767 Elf_Internal_Shdr
*versymhdr
;
769 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
770 extversym
= (Elf_External_Versym
*) bfd_malloc (hdr
->sh_size
);
771 if (extversym
== NULL
)
773 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
774 || (bfd_read ((PTR
) extversym
, 1, versymhdr
->sh_size
, abfd
)
775 != versymhdr
->sh_size
))
780 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
782 /* The sh_info field of the symtab header tells us where the
783 external symbols start. We don't care about the local symbols at
785 if (elf_bad_symtab (abfd
))
787 extsymcount
= symcount
;
792 extsymcount
= symcount
- hdr
->sh_info
;
793 extsymoff
= hdr
->sh_info
;
796 buf
= ((Elf_External_Sym
*)
797 bfd_malloc (extsymcount
* sizeof (Elf_External_Sym
)));
798 if (buf
== NULL
&& extsymcount
!= 0)
801 /* We store a pointer to the hash table entry for each external
803 sym_hash
= ((struct elf_link_hash_entry
**)
805 extsymcount
* sizeof (struct elf_link_hash_entry
*)));
806 if (sym_hash
== NULL
)
808 elf_sym_hashes (abfd
) = sym_hash
;
812 /* If we are creating a shared library, create all the dynamic
813 sections immediately. We need to attach them to something,
814 so we attach them to this BFD, provided it is the right
815 format. FIXME: If there are no input BFD's of the same
816 format as the output, we can't make a shared library. */
818 && ! elf_hash_table (info
)->dynamic_sections_created
819 && abfd
->xvec
== info
->hash
->creator
)
821 if (! elf_link_create_dynamic_sections (abfd
, info
))
830 bfd_size_type oldsize
;
831 bfd_size_type strindex
;
833 /* Find the name to use in a DT_NEEDED entry that refers to this
834 object. If the object has a DT_SONAME entry, we use it.
835 Otherwise, if the generic linker stuck something in
836 elf_dt_name, we use that. Otherwise, we just use the file
837 name. If the generic linker put a null string into
838 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
839 there is a DT_SONAME entry. */
841 name
= bfd_get_filename (abfd
);
842 if (elf_dt_name (abfd
) != NULL
)
844 name
= elf_dt_name (abfd
);
848 s
= bfd_get_section_by_name (abfd
, ".dynamic");
851 Elf_External_Dyn
*extdyn
;
852 Elf_External_Dyn
*extdynend
;
856 dynbuf
= (Elf_External_Dyn
*) bfd_malloc ((size_t) s
->_raw_size
);
860 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
861 (file_ptr
) 0, s
->_raw_size
))
864 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
867 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
870 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
871 for (; extdyn
< extdynend
; extdyn
++)
873 Elf_Internal_Dyn dyn
;
875 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
876 if (dyn
.d_tag
== DT_SONAME
)
878 name
= bfd_elf_string_from_elf_section (abfd
, link
,
883 if (dyn
.d_tag
== DT_NEEDED
)
885 struct bfd_link_needed_list
*n
, **pn
;
888 n
= ((struct bfd_link_needed_list
*)
889 bfd_alloc (abfd
, sizeof (struct bfd_link_needed_list
)));
890 fnm
= bfd_elf_string_from_elf_section (abfd
, link
,
892 if (n
== NULL
|| fnm
== NULL
)
894 anm
= bfd_alloc (abfd
, strlen (fnm
) + 1);
901 for (pn
= &elf_hash_table (info
)->needed
;
913 /* We do not want to include any of the sections in a dynamic
914 object in the output file. We hack by simply clobbering the
915 list of sections in the BFD. This could be handled more
916 cleanly by, say, a new section flag; the existing
917 SEC_NEVER_LOAD flag is not the one we want, because that one
918 still implies that the section takes up space in the output
920 abfd
->sections
= NULL
;
921 abfd
->section_count
= 0;
923 /* If this is the first dynamic object found in the link, create
924 the special sections required for dynamic linking. */
925 if (! elf_hash_table (info
)->dynamic_sections_created
)
927 if (! elf_link_create_dynamic_sections (abfd
, info
))
933 /* Add a DT_NEEDED entry for this dynamic object. */
934 oldsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
935 strindex
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, name
,
937 if (strindex
== (bfd_size_type
) -1)
940 if (oldsize
== _bfd_stringtab_size (elf_hash_table (info
)->dynstr
))
943 Elf_External_Dyn
*dyncon
, *dynconend
;
945 /* The hash table size did not change, which means that
946 the dynamic object name was already entered. If we
947 have already included this dynamic object in the
948 link, just ignore it. There is no reason to include
949 a particular dynamic object more than once. */
950 sdyn
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
952 BFD_ASSERT (sdyn
!= NULL
);
954 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
955 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
957 for (; dyncon
< dynconend
; dyncon
++)
959 Elf_Internal_Dyn dyn
;
961 elf_swap_dyn_in (elf_hash_table (info
)->dynobj
, dyncon
,
963 if (dyn
.d_tag
== DT_NEEDED
964 && dyn
.d_un
.d_val
== strindex
)
968 if (extversym
!= NULL
)
975 if (! elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
979 /* Save the SONAME, if there is one, because sometimes the
980 linker emulation code will need to know it. */
982 name
= bfd_get_filename (abfd
);
983 elf_dt_name (abfd
) = name
;
987 hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
),
989 || (bfd_read ((PTR
) buf
, sizeof (Elf_External_Sym
), extsymcount
, abfd
)
990 != extsymcount
* sizeof (Elf_External_Sym
)))
995 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
996 esymend
= buf
+ extsymcount
;
999 esym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
1001 Elf_Internal_Sym sym
;
1007 struct elf_link_hash_entry
*h
;
1009 boolean size_change_ok
, type_change_ok
;
1010 boolean new_weakdef
;
1011 unsigned int old_alignment
;
1013 elf_swap_symbol_in (abfd
, esym
, &sym
);
1015 flags
= BSF_NO_FLAGS
;
1017 value
= sym
.st_value
;
1020 bind
= ELF_ST_BIND (sym
.st_info
);
1021 if (bind
== STB_LOCAL
)
1023 /* This should be impossible, since ELF requires that all
1024 global symbols follow all local symbols, and that sh_info
1025 point to the first global symbol. Unfortunatealy, Irix 5
1029 else if (bind
== STB_GLOBAL
)
1031 if (sym
.st_shndx
!= SHN_UNDEF
1032 && sym
.st_shndx
!= SHN_COMMON
)
1037 else if (bind
== STB_WEAK
)
1041 /* Leave it up to the processor backend. */
1044 if (sym
.st_shndx
== SHN_UNDEF
)
1045 sec
= bfd_und_section_ptr
;
1046 else if (sym
.st_shndx
> 0 && sym
.st_shndx
< SHN_LORESERVE
)
1048 sec
= section_from_elf_index (abfd
, sym
.st_shndx
);
1050 sec
= bfd_abs_section_ptr
;
1051 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
1054 else if (sym
.st_shndx
== SHN_ABS
)
1055 sec
= bfd_abs_section_ptr
;
1056 else if (sym
.st_shndx
== SHN_COMMON
)
1058 sec
= bfd_com_section_ptr
;
1059 /* What ELF calls the size we call the value. What ELF
1060 calls the value we call the alignment. */
1061 value
= sym
.st_size
;
1065 /* Leave it up to the processor backend. */
1068 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
1069 if (name
== (const char *) NULL
)
1072 if (add_symbol_hook
)
1074 if (! (*add_symbol_hook
) (abfd
, info
, &sym
, &name
, &flags
, &sec
,
1078 /* The hook function sets the name to NULL if this symbol
1079 should be skipped for some reason. */
1080 if (name
== (const char *) NULL
)
1084 /* Sanity check that all possibilities were handled. */
1085 if (sec
== (asection
*) NULL
)
1087 bfd_set_error (bfd_error_bad_value
);
1091 if (bfd_is_und_section (sec
)
1092 || bfd_is_com_section (sec
))
1097 size_change_ok
= false;
1098 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
1100 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1102 Elf_Internal_Versym iver
;
1103 unsigned int vernum
= 0;
1108 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
1109 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
1111 /* If this is a hidden symbol, or if it is not version
1112 1, we append the version name to the symbol name.
1113 However, we do not modify a non-hidden absolute
1114 symbol, because it might be the version symbol
1115 itself. FIXME: What if it isn't? */
1116 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
1117 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
1120 int namelen
, newlen
;
1123 if (sym
.st_shndx
!= SHN_UNDEF
)
1125 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
1127 (*_bfd_error_handler
)
1128 (_("%s: %s: invalid version %u (max %d)"),
1129 abfd
->filename
, name
, vernum
,
1130 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1131 bfd_set_error (bfd_error_bad_value
);
1134 else if (vernum
> 1)
1136 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1142 /* We cannot simply test for the number of
1143 entries in the VERNEED section since the
1144 numbers for the needed versions do not start
1146 Elf_Internal_Verneed
*t
;
1149 for (t
= elf_tdata (abfd
)->verref
;
1153 Elf_Internal_Vernaux
*a
;
1155 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1157 if (a
->vna_other
== vernum
)
1159 verstr
= a
->vna_nodename
;
1168 (*_bfd_error_handler
)
1169 (_("%s: %s: invalid needed version %d"),
1170 abfd
->filename
, name
, vernum
);
1171 bfd_set_error (bfd_error_bad_value
);
1176 namelen
= strlen (name
);
1177 newlen
= namelen
+ strlen (verstr
) + 2;
1178 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
1181 newname
= (char *) bfd_alloc (abfd
, newlen
);
1182 if (newname
== NULL
)
1184 strcpy (newname
, name
);
1185 p
= newname
+ namelen
;
1187 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
1195 if (! elf_merge_symbol (abfd
, info
, name
, &sym
, &sec
, &value
,
1196 sym_hash
, &override
, &type_change_ok
,
1204 while (h
->root
.type
== bfd_link_hash_indirect
1205 || h
->root
.type
== bfd_link_hash_warning
)
1206 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1208 /* Remember the old alignment if this is a common symbol, so
1209 that we don't reduce the alignment later on. We can't
1210 check later, because _bfd_generic_link_add_one_symbol
1211 will set a default for the alignment which we want to
1213 if (h
->root
.type
== bfd_link_hash_common
)
1214 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1216 if (elf_tdata (abfd
)->verdef
!= NULL
1220 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1223 if (! (_bfd_generic_link_add_one_symbol
1224 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
1225 false, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
1229 while (h
->root
.type
== bfd_link_hash_indirect
1230 || h
->root
.type
== bfd_link_hash_warning
)
1231 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1234 new_weakdef
= false;
1237 && (flags
& BSF_WEAK
) != 0
1238 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
1239 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1240 && h
->weakdef
== NULL
)
1242 /* Keep a list of all weak defined non function symbols from
1243 a dynamic object, using the weakdef field. Later in this
1244 function we will set the weakdef field to the correct
1245 value. We only put non-function symbols from dynamic
1246 objects on this list, because that happens to be the only
1247 time we need to know the normal symbol corresponding to a
1248 weak symbol, and the information is time consuming to
1249 figure out. If the weakdef field is not already NULL,
1250 then this symbol was already defined by some previous
1251 dynamic object, and we will be using that previous
1252 definition anyhow. */
1259 /* Set the alignment of a common symbol. */
1260 if (sym
.st_shndx
== SHN_COMMON
1261 && h
->root
.type
== bfd_link_hash_common
)
1265 align
= bfd_log2 (sym
.st_value
);
1266 if (align
> old_alignment
)
1267 h
->root
.u
.c
.p
->alignment_power
= align
;
1270 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1276 /* Remember the symbol size and type. */
1277 if (sym
.st_size
!= 0
1278 && (definition
|| h
->size
== 0))
1280 if (h
->size
!= 0 && h
->size
!= sym
.st_size
&& ! size_change_ok
)
1281 (*_bfd_error_handler
)
1282 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1283 name
, (unsigned long) h
->size
, (unsigned long) sym
.st_size
,
1284 bfd_get_filename (abfd
));
1286 h
->size
= sym
.st_size
;
1289 /* If this is a common symbol, then we always want H->SIZE
1290 to be the size of the common symbol. The code just above
1291 won't fix the size if a common symbol becomes larger. We
1292 don't warn about a size change here, because that is
1293 covered by --warn-common. */
1294 if (h
->root
.type
== bfd_link_hash_common
)
1295 h
->size
= h
->root
.u
.c
.size
;
1297 if (ELF_ST_TYPE (sym
.st_info
) != STT_NOTYPE
1298 && (definition
|| h
->type
== STT_NOTYPE
))
1300 if (h
->type
!= STT_NOTYPE
1301 && h
->type
!= ELF_ST_TYPE (sym
.st_info
)
1302 && ! type_change_ok
)
1303 (*_bfd_error_handler
)
1304 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1305 name
, h
->type
, ELF_ST_TYPE (sym
.st_info
),
1306 bfd_get_filename (abfd
));
1308 h
->type
= ELF_ST_TYPE (sym
.st_info
);
1311 if (sym
.st_other
!= 0
1312 && (definition
|| h
->other
== 0))
1313 h
->other
= sym
.st_other
;
1315 /* Set a flag in the hash table entry indicating the type of
1316 reference or definition we just found. Keep a count of
1317 the number of dynamic symbols we find. A dynamic symbol
1318 is one which is referenced or defined by both a regular
1319 object and a shared object. */
1320 old_flags
= h
->elf_link_hash_flags
;
1325 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
1327 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
1329 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1330 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
1336 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
1338 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
1339 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
1340 | ELF_LINK_HASH_REF_REGULAR
)) != 0
1341 || (h
->weakdef
!= NULL
1343 && h
->weakdef
->dynindx
!= -1))
1347 h
->elf_link_hash_flags
|= new_flag
;
1349 /* If this symbol has a version, and it is the default
1350 version, we create an indirect symbol from the default
1351 name to the fully decorated name. This will cause
1352 external references which do not specify a version to be
1353 bound to this version of the symbol. */
1358 p
= strchr (name
, ELF_VER_CHR
);
1359 if (p
!= NULL
&& p
[1] == ELF_VER_CHR
)
1362 struct elf_link_hash_entry
*hi
;
1365 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1367 if (shortname
== NULL
)
1369 strncpy (shortname
, name
, p
- name
);
1370 shortname
[p
- name
] = '\0';
1372 /* We are going to create a new symbol. Merge it
1373 with any existing symbol with this name. For the
1374 purposes of the merge, act as though we were
1375 defining the symbol we just defined, although we
1376 actually going to define an indirect symbol. */
1377 type_change_ok
= false;
1378 size_change_ok
= false;
1379 if (! elf_merge_symbol (abfd
, info
, shortname
, &sym
, &sec
,
1380 &value
, &hi
, &override
,
1381 &type_change_ok
, &size_change_ok
))
1386 if (! (_bfd_generic_link_add_one_symbol
1387 (info
, abfd
, shortname
, BSF_INDIRECT
,
1388 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1389 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1394 /* In this case the symbol named SHORTNAME is
1395 overriding the indirect symbol we want to
1396 add. We were planning on making SHORTNAME an
1397 indirect symbol referring to NAME. SHORTNAME
1398 is the name without a version. NAME is the
1399 fully versioned name, and it is the default
1402 Overriding means that we already saw a
1403 definition for the symbol SHORTNAME in a
1404 regular object, and it is overriding the
1405 symbol defined in the dynamic object.
1407 When this happens, we actually want to change
1408 NAME, the symbol we just added, to refer to
1409 SHORTNAME. This will cause references to
1410 NAME in the shared object to become
1411 references to SHORTNAME in the regular
1412 object. This is what we expect when we
1413 override a function in a shared object: that
1414 the references in the shared object will be
1415 mapped to the definition in the regular
1418 while (hi
->root
.type
== bfd_link_hash_indirect
1419 || hi
->root
.type
== bfd_link_hash_warning
)
1420 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1422 h
->root
.type
= bfd_link_hash_indirect
;
1423 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1424 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1426 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1427 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1428 if (hi
->elf_link_hash_flags
1429 & (ELF_LINK_HASH_REF_REGULAR
1430 | ELF_LINK_HASH_DEF_REGULAR
))
1432 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1438 /* Now set HI to H, so that the following code
1439 will set the other fields correctly. */
1443 /* If there is a duplicate definition somewhere,
1444 then HI may not point to an indirect symbol. We
1445 will have reported an error to the user in that
1448 if (hi
->root
.type
== bfd_link_hash_indirect
)
1450 struct elf_link_hash_entry
*ht
;
1452 /* If the symbol became indirect, then we assume
1453 that we have not seen a definition before. */
1454 BFD_ASSERT ((hi
->elf_link_hash_flags
1455 & (ELF_LINK_HASH_DEF_DYNAMIC
1456 | ELF_LINK_HASH_DEF_REGULAR
))
1459 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1461 /* Copy down any references that we may have
1462 already seen to the symbol which just became
1464 ht
->elf_link_hash_flags
|=
1465 (hi
->elf_link_hash_flags
1466 & (ELF_LINK_HASH_REF_DYNAMIC
1467 | ELF_LINK_HASH_REF_REGULAR
));
1469 /* Copy over the global and procedure linkage table
1470 offset entries. These may have been already set
1471 up by a check_relocs routine. */
1472 if (ht
->got
.offset
== (bfd_vma
) -1)
1474 ht
->got
.offset
= hi
->got
.offset
;
1475 hi
->got
.offset
= (bfd_vma
) -1;
1477 BFD_ASSERT (hi
->got
.offset
== (bfd_vma
) -1);
1479 if (ht
->plt
.offset
== (bfd_vma
) -1)
1481 ht
->plt
.offset
= hi
->plt
.offset
;
1482 hi
->plt
.offset
= (bfd_vma
) -1;
1484 BFD_ASSERT (hi
->plt
.offset
== (bfd_vma
) -1);
1486 if (ht
->dynindx
== -1)
1488 ht
->dynindx
= hi
->dynindx
;
1489 ht
->dynstr_index
= hi
->dynstr_index
;
1491 hi
->dynstr_index
= 0;
1493 BFD_ASSERT (hi
->dynindx
== -1);
1495 /* FIXME: There may be other information to copy
1496 over for particular targets. */
1498 /* See if the new flags lead us to realize that
1499 the symbol must be dynamic. */
1505 || ((hi
->elf_link_hash_flags
1506 & ELF_LINK_HASH_REF_DYNAMIC
)
1512 if ((hi
->elf_link_hash_flags
1513 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1519 /* We also need to define an indirection from the
1520 nondefault version of the symbol. */
1522 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1524 if (shortname
== NULL
)
1526 strncpy (shortname
, name
, p
- name
);
1527 strcpy (shortname
+ (p
- name
), p
+ 1);
1529 /* Once again, merge with any existing symbol. */
1530 type_change_ok
= false;
1531 size_change_ok
= false;
1532 if (! elf_merge_symbol (abfd
, info
, shortname
, &sym
, &sec
,
1533 &value
, &hi
, &override
,
1534 &type_change_ok
, &size_change_ok
))
1539 /* Here SHORTNAME is a versioned name, so we
1540 don't expect to see the type of override we
1541 do in the case above. */
1542 (*_bfd_error_handler
)
1543 (_("%s: warning: unexpected redefinition of `%s'"),
1544 bfd_get_filename (abfd
), shortname
);
1548 if (! (_bfd_generic_link_add_one_symbol
1549 (info
, abfd
, shortname
, BSF_INDIRECT
,
1550 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1551 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1554 /* If there is a duplicate definition somewhere,
1555 then HI may not point to an indirect symbol.
1556 We will have reported an error to the user in
1559 if (hi
->root
.type
== bfd_link_hash_indirect
)
1561 /* If the symbol became indirect, then we
1562 assume that we have not seen a definition
1564 BFD_ASSERT ((hi
->elf_link_hash_flags
1565 & (ELF_LINK_HASH_DEF_DYNAMIC
1566 | ELF_LINK_HASH_DEF_REGULAR
))
1569 /* Copy down any references that we may have
1570 already seen to the symbol which just
1572 h
->elf_link_hash_flags
|=
1573 (hi
->elf_link_hash_flags
1574 & (ELF_LINK_HASH_REF_DYNAMIC
1575 | ELF_LINK_HASH_REF_REGULAR
));
1577 /* Copy over the global and procedure linkage
1578 table offset entries. These may have been
1579 already set up by a check_relocs routine. */
1580 if (h
->got
.offset
== (bfd_vma
) -1)
1582 h
->got
.offset
= hi
->got
.offset
;
1583 hi
->got
.offset
= (bfd_vma
) -1;
1585 BFD_ASSERT (hi
->got
.offset
== (bfd_vma
) -1);
1587 if (h
->plt
.offset
== (bfd_vma
) -1)
1589 h
->plt
.offset
= hi
->plt
.offset
;
1590 hi
->plt
.offset
= (bfd_vma
) -1;
1592 BFD_ASSERT (hi
->got
.offset
== (bfd_vma
) -1);
1594 if (h
->dynindx
== -1)
1596 h
->dynindx
= hi
->dynindx
;
1597 h
->dynstr_index
= hi
->dynstr_index
;
1599 hi
->dynstr_index
= 0;
1601 BFD_ASSERT (hi
->dynindx
== -1);
1603 /* FIXME: There may be other information to
1604 copy over for particular targets. */
1606 /* See if the new flags lead us to realize
1607 that the symbol must be dynamic. */
1613 || ((hi
->elf_link_hash_flags
1614 & ELF_LINK_HASH_REF_DYNAMIC
)
1620 if ((hi
->elf_link_hash_flags
1621 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1630 if (dynsym
&& h
->dynindx
== -1)
1632 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1634 if (h
->weakdef
!= NULL
1636 && h
->weakdef
->dynindx
== -1)
1638 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1646 /* Now set the weakdefs field correctly for all the weak defined
1647 symbols we found. The only way to do this is to search all the
1648 symbols. Since we only need the information for non functions in
1649 dynamic objects, that's the only time we actually put anything on
1650 the list WEAKS. We need this information so that if a regular
1651 object refers to a symbol defined weakly in a dynamic object, the
1652 real symbol in the dynamic object is also put in the dynamic
1653 symbols; we also must arrange for both symbols to point to the
1654 same memory location. We could handle the general case of symbol
1655 aliasing, but a general symbol alias can only be generated in
1656 assembler code, handling it correctly would be very time
1657 consuming, and other ELF linkers don't handle general aliasing
1659 while (weaks
!= NULL
)
1661 struct elf_link_hash_entry
*hlook
;
1664 struct elf_link_hash_entry
**hpp
;
1665 struct elf_link_hash_entry
**hppend
;
1668 weaks
= hlook
->weakdef
;
1669 hlook
->weakdef
= NULL
;
1671 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
1672 || hlook
->root
.type
== bfd_link_hash_defweak
1673 || hlook
->root
.type
== bfd_link_hash_common
1674 || hlook
->root
.type
== bfd_link_hash_indirect
);
1675 slook
= hlook
->root
.u
.def
.section
;
1676 vlook
= hlook
->root
.u
.def
.value
;
1678 hpp
= elf_sym_hashes (abfd
);
1679 hppend
= hpp
+ extsymcount
;
1680 for (; hpp
< hppend
; hpp
++)
1682 struct elf_link_hash_entry
*h
;
1685 if (h
!= NULL
&& h
!= hlook
1686 && h
->root
.type
== bfd_link_hash_defined
1687 && h
->root
.u
.def
.section
== slook
1688 && h
->root
.u
.def
.value
== vlook
)
1692 /* If the weak definition is in the list of dynamic
1693 symbols, make sure the real definition is put there
1695 if (hlook
->dynindx
!= -1
1696 && h
->dynindx
== -1)
1698 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1702 /* If the real definition is in the list of dynamic
1703 symbols, make sure the weak definition is put there
1704 as well. If we don't do this, then the dynamic
1705 loader might not merge the entries for the real
1706 definition and the weak definition. */
1707 if (h
->dynindx
!= -1
1708 && hlook
->dynindx
== -1)
1710 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
1725 if (extversym
!= NULL
)
1731 /* If this object is the same format as the output object, and it is
1732 not a shared library, then let the backend look through the
1735 This is required to build global offset table entries and to
1736 arrange for dynamic relocs. It is not required for the
1737 particular common case of linking non PIC code, even when linking
1738 against shared libraries, but unfortunately there is no way of
1739 knowing whether an object file has been compiled PIC or not.
1740 Looking through the relocs is not particularly time consuming.
1741 The problem is that we must either (1) keep the relocs in memory,
1742 which causes the linker to require additional runtime memory or
1743 (2) read the relocs twice from the input file, which wastes time.
1744 This would be a good case for using mmap.
1746 I have no idea how to handle linking PIC code into a file of a
1747 different format. It probably can't be done. */
1748 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
1750 && abfd
->xvec
== info
->hash
->creator
1751 && check_relocs
!= NULL
)
1755 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1757 Elf_Internal_Rela
*internal_relocs
;
1760 if ((o
->flags
& SEC_RELOC
) == 0
1761 || o
->reloc_count
== 0
1762 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
1763 && (o
->flags
& SEC_DEBUGGING
) != 0)
1764 || bfd_is_abs_section (o
->output_section
))
1767 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
1768 (abfd
, o
, (PTR
) NULL
,
1769 (Elf_Internal_Rela
*) NULL
,
1770 info
->keep_memory
));
1771 if (internal_relocs
== NULL
)
1774 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
1776 if (! info
->keep_memory
)
1777 free (internal_relocs
);
1784 /* If this is a non-traditional, non-relocateable link, try to
1785 optimize the handling of the .stab/.stabstr sections. */
1787 && ! info
->relocateable
1788 && ! info
->traditional_format
1789 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1790 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
1792 asection
*stab
, *stabstr
;
1794 stab
= bfd_get_section_by_name (abfd
, ".stab");
1797 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
1799 if (stabstr
!= NULL
)
1801 struct bfd_elf_section_data
*secdata
;
1803 secdata
= elf_section_data (stab
);
1804 if (! _bfd_link_section_stabs (abfd
,
1805 &elf_hash_table (info
)->stab_info
,
1807 &secdata
->stab_info
))
1822 if (extversym
!= NULL
)
1827 /* Create some sections which will be filled in with dynamic linking
1828 information. ABFD is an input file which requires dynamic sections
1829 to be created. The dynamic sections take up virtual memory space
1830 when the final executable is run, so we need to create them before
1831 addresses are assigned to the output sections. We work out the
1832 actual contents and size of these sections later. */
1835 elf_link_create_dynamic_sections (abfd
, info
)
1837 struct bfd_link_info
*info
;
1840 register asection
*s
;
1841 struct elf_link_hash_entry
*h
;
1842 struct elf_backend_data
*bed
;
1844 if (elf_hash_table (info
)->dynamic_sections_created
)
1847 /* Make sure that all dynamic sections use the same input BFD. */
1848 if (elf_hash_table (info
)->dynobj
== NULL
)
1849 elf_hash_table (info
)->dynobj
= abfd
;
1851 abfd
= elf_hash_table (info
)->dynobj
;
1853 /* Note that we set the SEC_IN_MEMORY flag for all of these
1855 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1856 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
1858 /* A dynamically linked executable has a .interp section, but a
1859 shared library does not. */
1862 s
= bfd_make_section (abfd
, ".interp");
1864 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
1868 /* Create sections to hold version informations. These are removed
1869 if they are not needed. */
1870 s
= bfd_make_section (abfd
, ".gnu.version_d");
1872 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1873 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1876 s
= bfd_make_section (abfd
, ".gnu.version");
1878 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1879 || ! bfd_set_section_alignment (abfd
, s
, 1))
1882 s
= bfd_make_section (abfd
, ".gnu.version_r");
1884 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1885 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1888 s
= bfd_make_section (abfd
, ".dynsym");
1890 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1891 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1894 s
= bfd_make_section (abfd
, ".dynstr");
1896 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
1899 /* Create a strtab to hold the dynamic symbol names. */
1900 if (elf_hash_table (info
)->dynstr
== NULL
)
1902 elf_hash_table (info
)->dynstr
= elf_stringtab_init ();
1903 if (elf_hash_table (info
)->dynstr
== NULL
)
1907 s
= bfd_make_section (abfd
, ".dynamic");
1909 || ! bfd_set_section_flags (abfd
, s
, flags
)
1910 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1913 /* The special symbol _DYNAMIC is always set to the start of the
1914 .dynamic section. This call occurs before we have processed the
1915 symbols for any dynamic object, so we don't have to worry about
1916 overriding a dynamic definition. We could set _DYNAMIC in a
1917 linker script, but we only want to define it if we are, in fact,
1918 creating a .dynamic section. We don't want to define it if there
1919 is no .dynamic section, since on some ELF platforms the start up
1920 code examines it to decide how to initialize the process. */
1922 if (! (_bfd_generic_link_add_one_symbol
1923 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
1924 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
1925 (struct bfd_link_hash_entry
**) &h
)))
1927 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
1928 h
->type
= STT_OBJECT
;
1931 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1934 s
= bfd_make_section (abfd
, ".hash");
1936 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1937 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1940 /* Let the backend create the rest of the sections. This lets the
1941 backend set the right flags. The backend will normally create
1942 the .got and .plt sections. */
1943 bed
= get_elf_backend_data (abfd
);
1944 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
1947 elf_hash_table (info
)->dynamic_sections_created
= true;
1952 /* Add an entry to the .dynamic table. */
1955 elf_add_dynamic_entry (info
, tag
, val
)
1956 struct bfd_link_info
*info
;
1960 Elf_Internal_Dyn dyn
;
1964 bfd_byte
*newcontents
;
1966 dynobj
= elf_hash_table (info
)->dynobj
;
1968 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
1969 BFD_ASSERT (s
!= NULL
);
1971 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
1972 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
1973 if (newcontents
== NULL
)
1977 dyn
.d_un
.d_val
= val
;
1978 elf_swap_dyn_out (dynobj
, &dyn
,
1979 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
1981 s
->_raw_size
= newsize
;
1982 s
->contents
= newcontents
;
1988 /* Read and swap the relocs for a section. They may have been cached.
1989 If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are not NULL,
1990 they are used as buffers to read into. They are known to be large
1991 enough. If the INTERNAL_RELOCS relocs argument is NULL, the return
1992 value is allocated using either malloc or bfd_alloc, according to
1993 the KEEP_MEMORY argument. */
1996 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
2000 PTR external_relocs
;
2001 Elf_Internal_Rela
*internal_relocs
;
2002 boolean keep_memory
;
2004 Elf_Internal_Shdr
*rel_hdr
;
2006 Elf_Internal_Rela
*alloc2
= NULL
;
2008 if (elf_section_data (o
)->relocs
!= NULL
)
2009 return elf_section_data (o
)->relocs
;
2011 if (o
->reloc_count
== 0)
2014 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2016 if (internal_relocs
== NULL
)
2020 size
= o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2022 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2024 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2025 if (internal_relocs
== NULL
)
2029 if (external_relocs
== NULL
)
2031 alloc1
= (PTR
) bfd_malloc ((size_t) rel_hdr
->sh_size
);
2034 external_relocs
= alloc1
;
2037 if ((bfd_seek (abfd
, rel_hdr
->sh_offset
, SEEK_SET
) != 0)
2038 || (bfd_read (external_relocs
, 1, rel_hdr
->sh_size
, abfd
)
2039 != rel_hdr
->sh_size
))
2042 /* Swap in the relocs. For convenience, we always produce an
2043 Elf_Internal_Rela array; if the relocs are Rel, we set the addend
2045 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2047 Elf_External_Rel
*erel
;
2048 Elf_External_Rel
*erelend
;
2049 Elf_Internal_Rela
*irela
;
2051 erel
= (Elf_External_Rel
*) external_relocs
;
2052 erelend
= erel
+ o
->reloc_count
;
2053 irela
= internal_relocs
;
2054 for (; erel
< erelend
; erel
++, irela
++)
2056 Elf_Internal_Rel irel
;
2058 elf_swap_reloc_in (abfd
, erel
, &irel
);
2059 irela
->r_offset
= irel
.r_offset
;
2060 irela
->r_info
= irel
.r_info
;
2061 irela
->r_addend
= 0;
2066 Elf_External_Rela
*erela
;
2067 Elf_External_Rela
*erelaend
;
2068 Elf_Internal_Rela
*irela
;
2070 BFD_ASSERT (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
));
2072 erela
= (Elf_External_Rela
*) external_relocs
;
2073 erelaend
= erela
+ o
->reloc_count
;
2074 irela
= internal_relocs
;
2075 for (; erela
< erelaend
; erela
++, irela
++)
2076 elf_swap_reloca_in (abfd
, erela
, irela
);
2079 /* Cache the results for next time, if we can. */
2081 elf_section_data (o
)->relocs
= internal_relocs
;
2086 /* Don't free alloc2, since if it was allocated we are passing it
2087 back (under the name of internal_relocs). */
2089 return internal_relocs
;
2100 /* Record an assignment to a symbol made by a linker script. We need
2101 this in case some dynamic object refers to this symbol. */
2105 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
2107 struct bfd_link_info
*info
;
2111 struct elf_link_hash_entry
*h
;
2113 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2116 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, true, false);
2120 if (h
->root
.type
== bfd_link_hash_new
)
2121 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
2123 /* If this symbol is being provided by the linker script, and it is
2124 currently defined by a dynamic object, but not by a regular
2125 object, then mark it as undefined so that the generic linker will
2126 force the correct value. */
2128 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2129 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2130 h
->root
.type
= bfd_link_hash_undefined
;
2132 /* If this symbol is not being provided by the linker script, and it is
2133 currently defined by a dynamic object, but not by a regular object,
2134 then clear out any version information because the symbol will not be
2135 associated with the dynamic object any more. */
2137 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2138 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2139 h
->verinfo
.verdef
= NULL
;
2141 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2142 h
->type
= STT_OBJECT
;
2144 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2145 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
2147 && h
->dynindx
== -1)
2149 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2152 /* If this is a weak defined symbol, and we know a corresponding
2153 real symbol from the same dynamic object, make sure the real
2154 symbol is also made into a dynamic symbol. */
2155 if (h
->weakdef
!= NULL
2156 && h
->weakdef
->dynindx
== -1)
2158 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2166 /* This structure is used to pass information to
2167 elf_link_assign_sym_version. */
2169 struct elf_assign_sym_version_info
2173 /* General link information. */
2174 struct bfd_link_info
*info
;
2176 struct bfd_elf_version_tree
*verdefs
;
2177 /* Whether we are exporting all dynamic symbols. */
2178 boolean export_dynamic
;
2179 /* Whether we removed any symbols from the dynamic symbol table. */
2180 boolean removed_dynamic
;
2181 /* Whether we had a failure. */
2185 /* This structure is used to pass information to
2186 elf_link_find_version_dependencies. */
2188 struct elf_find_verdep_info
2192 /* General link information. */
2193 struct bfd_link_info
*info
;
2194 /* The number of dependencies. */
2196 /* Whether we had a failure. */
2200 /* Array used to determine the number of hash table buckets to use
2201 based on the number of symbols there are. If there are fewer than
2202 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2203 fewer than 37 we use 17 buckets, and so forth. We never use more
2204 than 32771 buckets. */
2206 static const size_t elf_buckets
[] =
2208 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2212 /* Compute bucket count for hashing table. We do not use a static set
2213 of possible tables sizes anymore. Instead we determine for all
2214 possible reasonable sizes of the table the outcome (i.e., the
2215 number of collisions etc) and choose the best solution. The
2216 weighting functions are not too simple to allow the table to grow
2217 without bounds. Instead one of the weighting factors is the size.
2218 Therefore the result is always a good payoff between few collisions
2219 (= short chain lengths) and table size. */
2221 compute_bucket_count (info
)
2222 struct bfd_link_info
*info
;
2224 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2226 unsigned long int *hashcodes
;
2227 unsigned long int *hashcodesp
;
2228 unsigned long int i
;
2230 /* Compute the hash values for all exported symbols. At the same
2231 time store the values in an array so that we could use them for
2233 hashcodes
= (unsigned long int *) bfd_malloc (dynsymcount
2234 * sizeof (unsigned long int));
2235 if (hashcodes
== NULL
)
2237 hashcodesp
= hashcodes
;
2239 /* Put all hash values in HASHCODES. */
2240 elf_link_hash_traverse (elf_hash_table (info
),
2241 elf_collect_hash_codes
, &hashcodesp
);
2243 /* We have a problem here. The following code to optimize the table size
2244 requires an integer type with more the 32 bits. If BFD_HOST_U_64_BIT
2245 is set or GCC 2 is used we know about such a type. */
2246 #if defined BFD_HOST_U_64_BIT || __GNUC__ >= 2
2247 # ifndef BFD_HOST_U_64_BIT
2248 # define BFD_HOST_U_64_BIT unsigned long long int
2250 if (info
->optimize
== true)
2252 unsigned long int nsyms
= hashcodesp
- hashcodes
;
2255 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
2256 unsigned long int *counts
;
2258 /* Possible optimization parameters: if we have NSYMS symbols we say
2259 that the hashing table must at least have NSYMS/4 and at most
2261 minsize
= nsyms
/ 4;
2262 best_size
= maxsize
= nsyms
* 2;
2264 /* Create array where we count the collisions in. We must use bfd_malloc
2265 since the size could be large. */
2266 counts
= (unsigned long int *) bfd_malloc (maxsize
2267 * sizeof (unsigned long int));
2274 /* Compute the "optimal" size for the hash table. The criteria is a
2275 minimal chain length. The minor criteria is (of course) the size
2277 for (i
= minsize
; i
< maxsize
; ++i
)
2279 /* Walk through the array of hashcodes and count the collisions. */
2280 BFD_HOST_U_64_BIT max
;
2281 unsigned long int j
;
2282 unsigned long int fact
;
2284 memset (counts
, '\0', i
* sizeof (unsigned long int));
2286 /* Determine how often each hash bucket is used. */
2287 for (j
= 0; j
< nsyms
; ++j
)
2288 ++counts
[hashcodes
[j
] % i
];
2290 /* For the weight function we need some information about the
2291 pagesize on the target. This is information need not be 100%
2292 accurate. Since this information is not available (so far) we
2293 define it here to a reasonable default value. If it is crucial
2294 to have a better value some day simply define this value. */
2295 # ifndef BFD_TARGET_PAGESIZE
2296 # define BFD_TARGET_PAGESIZE (4096)
2299 /* We in any case need 2 + NSYMS entries for the size values and
2301 max
= (2 + nsyms
) * (ARCH_SIZE
/ 8);
2304 /* Variant 1: optimize for short chains. We add the squares
2305 of all the chain lengths (which favous many small chain
2306 over a few long chains). */
2307 for (j
= 0; j
< i
; ++j
)
2308 max
+= counts
[j
] * counts
[j
];
2310 /* This adds penalties for the overall size of the table. */
2311 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2314 /* Variant 2: Optimize a lot more for small table. Here we
2315 also add squares of the size but we also add penalties for
2316 empty slots (the +1 term). */
2317 for (j
= 0; j
< i
; ++j
)
2318 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
2320 /* The overall size of the table is considered, but not as
2321 strong as in variant 1, where it is squared. */
2322 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (ARCH_SIZE
/ 8)) + 1;
2326 /* Compare with current best results. */
2327 if (max
< best_chlen
)
2339 /* This is the fallback solution if no 64bit type is available or if we
2340 are not supposed to spend much time on optimizations. We select the
2341 bucket count using a fixed set of numbers. */
2342 for (i
= 0; elf_buckets
[i
] != 0; i
++)
2344 best_size
= elf_buckets
[i
];
2345 if (dynsymcount
< elf_buckets
[i
+ 1])
2350 /* Free the arrays we needed. */
2356 /* Set up the sizes and contents of the ELF dynamic sections. This is
2357 called by the ELF linker emulation before_allocation routine. We
2358 must set the sizes of the sections before the linker sets the
2359 addresses of the various sections. */
2362 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
2363 export_dynamic
, filter_shlib
,
2364 auxiliary_filters
, info
, sinterpptr
,
2369 boolean export_dynamic
;
2370 const char *filter_shlib
;
2371 const char * const *auxiliary_filters
;
2372 struct bfd_link_info
*info
;
2373 asection
**sinterpptr
;
2374 struct bfd_elf_version_tree
*verdefs
;
2376 bfd_size_type soname_indx
;
2378 struct elf_backend_data
*bed
;
2379 bfd_size_type old_dynsymcount
;
2380 struct elf_assign_sym_version_info asvinfo
;
2384 soname_indx
= (bfd_size_type
) -1;
2386 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2389 /* The backend may have to create some sections regardless of whether
2390 we're dynamic or not. */
2391 bed
= get_elf_backend_data (output_bfd
);
2392 if (bed
->elf_backend_always_size_sections
2393 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
2396 dynobj
= elf_hash_table (info
)->dynobj
;
2398 /* If there were no dynamic objects in the link, there is nothing to
2403 /* If we are supposed to export all symbols into the dynamic symbol
2404 table (this is not the normal case), then do so. */
2407 struct elf_info_failed eif
;
2411 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
2417 if (elf_hash_table (info
)->dynamic_sections_created
)
2419 struct elf_info_failed eif
;
2420 struct elf_link_hash_entry
*h
;
2421 bfd_size_type strsize
;
2423 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
2424 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
2428 soname_indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2429 soname
, true, true);
2430 if (soname_indx
== (bfd_size_type
) -1
2431 || ! elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
2437 if (! elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
2445 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, rpath
,
2447 if (indx
== (bfd_size_type
) -1
2448 || ! elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
2452 if (filter_shlib
!= NULL
)
2456 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2457 filter_shlib
, true, true);
2458 if (indx
== (bfd_size_type
) -1
2459 || ! elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
2463 if (auxiliary_filters
!= NULL
)
2465 const char * const *p
;
2467 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
2471 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2473 if (indx
== (bfd_size_type
) -1
2474 || ! elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
2479 /* Attach all the symbols to their version information. */
2480 asvinfo
.output_bfd
= output_bfd
;
2481 asvinfo
.info
= info
;
2482 asvinfo
.verdefs
= verdefs
;
2483 asvinfo
.export_dynamic
= export_dynamic
;
2484 asvinfo
.removed_dynamic
= false;
2485 asvinfo
.failed
= false;
2487 elf_link_hash_traverse (elf_hash_table (info
),
2488 elf_link_assign_sym_version
,
2493 /* Find all symbols which were defined in a dynamic object and make
2494 the backend pick a reasonable value for them. */
2497 elf_link_hash_traverse (elf_hash_table (info
),
2498 elf_adjust_dynamic_symbol
,
2503 /* Add some entries to the .dynamic section. We fill in some of the
2504 values later, in elf_bfd_final_link, but we must add the entries
2505 now so that we know the final size of the .dynamic section. */
2506 h
= elf_link_hash_lookup (elf_hash_table (info
), "_init", false,
2509 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2510 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2512 if (! elf_add_dynamic_entry (info
, DT_INIT
, 0))
2515 h
= elf_link_hash_lookup (elf_hash_table (info
), "_fini", false,
2518 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2519 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2521 if (! elf_add_dynamic_entry (info
, DT_FINI
, 0))
2524 strsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
2525 if (! elf_add_dynamic_entry (info
, DT_HASH
, 0)
2526 || ! elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
2527 || ! elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
2528 || ! elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
2529 || ! elf_add_dynamic_entry (info
, DT_SYMENT
,
2530 sizeof (Elf_External_Sym
)))
2534 /* The backend must work out the sizes of all the other dynamic
2536 old_dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2537 if (bed
->elf_backend_size_dynamic_sections
2538 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
2541 if (elf_hash_table (info
)->dynamic_sections_created
)
2546 size_t bucketcount
= 0;
2547 Elf_Internal_Sym isym
;
2549 /* Set up the version definition section. */
2550 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2551 BFD_ASSERT (s
!= NULL
);
2553 /* We may have created additional version definitions if we are
2554 just linking a regular application. */
2555 verdefs
= asvinfo
.verdefs
;
2557 if (verdefs
== NULL
)
2561 /* Don't include this section in the output file. */
2562 for (spp
= &output_bfd
->sections
;
2563 *spp
!= s
->output_section
;
2564 spp
= &(*spp
)->next
)
2566 *spp
= s
->output_section
->next
;
2567 --output_bfd
->section_count
;
2573 struct bfd_elf_version_tree
*t
;
2575 Elf_Internal_Verdef def
;
2576 Elf_Internal_Verdaux defaux
;
2578 if (asvinfo
.removed_dynamic
)
2580 /* Some dynamic symbols were changed to be local
2581 symbols. In this case, we renumber all of the
2582 dynamic symbols, so that we don't have a hole. If
2583 the backend changed dynsymcount, then assume that the
2584 new symbols are at the start. This is the case on
2585 the MIPS. FIXME: The names of the removed symbols
2586 will still be in the dynamic string table, wasting
2588 elf_hash_table (info
)->dynsymcount
=
2589 1 + (elf_hash_table (info
)->dynsymcount
- old_dynsymcount
);
2590 elf_link_hash_traverse (elf_hash_table (info
),
2591 elf_link_renumber_dynsyms
,
2598 /* Make space for the base version. */
2599 size
+= sizeof (Elf_External_Verdef
);
2600 size
+= sizeof (Elf_External_Verdaux
);
2603 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2605 struct bfd_elf_version_deps
*n
;
2607 size
+= sizeof (Elf_External_Verdef
);
2608 size
+= sizeof (Elf_External_Verdaux
);
2611 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2612 size
+= sizeof (Elf_External_Verdaux
);
2615 s
->_raw_size
= size
;
2616 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2617 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2620 /* Fill in the version definition section. */
2624 def
.vd_version
= VER_DEF_CURRENT
;
2625 def
.vd_flags
= VER_FLG_BASE
;
2628 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2629 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2630 + sizeof (Elf_External_Verdaux
));
2632 if (soname_indx
!= (bfd_size_type
) -1)
2634 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) soname
);
2635 defaux
.vda_name
= soname_indx
;
2642 name
= output_bfd
->filename
;
2643 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) name
);
2644 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2646 if (indx
== (bfd_size_type
) -1)
2648 defaux
.vda_name
= indx
;
2650 defaux
.vda_next
= 0;
2652 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2653 (Elf_External_Verdef
*)p
);
2654 p
+= sizeof (Elf_External_Verdef
);
2655 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2656 (Elf_External_Verdaux
*) p
);
2657 p
+= sizeof (Elf_External_Verdaux
);
2659 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2662 struct bfd_elf_version_deps
*n
;
2663 struct elf_link_hash_entry
*h
;
2666 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2669 /* Add a symbol representing this version. */
2671 if (! (_bfd_generic_link_add_one_symbol
2672 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
2673 (bfd_vma
) 0, (const char *) NULL
, false,
2674 get_elf_backend_data (dynobj
)->collect
,
2675 (struct bfd_link_hash_entry
**) &h
)))
2677 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
2678 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2679 h
->type
= STT_OBJECT
;
2680 h
->verinfo
.vertree
= t
;
2682 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2685 def
.vd_version
= VER_DEF_CURRENT
;
2687 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
2688 def
.vd_flags
|= VER_FLG_WEAK
;
2689 def
.vd_ndx
= t
->vernum
+ 1;
2690 def
.vd_cnt
= cdeps
+ 1;
2691 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) t
->name
);
2692 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2693 if (t
->next
!= NULL
)
2694 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2695 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
2699 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2700 (Elf_External_Verdef
*) p
);
2701 p
+= sizeof (Elf_External_Verdef
);
2703 defaux
.vda_name
= h
->dynstr_index
;
2704 if (t
->deps
== NULL
)
2705 defaux
.vda_next
= 0;
2707 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2708 t
->name_indx
= defaux
.vda_name
;
2710 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2711 (Elf_External_Verdaux
*) p
);
2712 p
+= sizeof (Elf_External_Verdaux
);
2714 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2716 if (n
->version_needed
== NULL
)
2718 /* This can happen if there was an error in the
2720 defaux
.vda_name
= 0;
2723 defaux
.vda_name
= n
->version_needed
->name_indx
;
2724 if (n
->next
== NULL
)
2725 defaux
.vda_next
= 0;
2727 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2729 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2730 (Elf_External_Verdaux
*) p
);
2731 p
+= sizeof (Elf_External_Verdaux
);
2735 if (! elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
2736 || ! elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
2739 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
2742 /* Work out the size of the version reference section. */
2744 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2745 BFD_ASSERT (s
!= NULL
);
2747 struct elf_find_verdep_info sinfo
;
2749 sinfo
.output_bfd
= output_bfd
;
2751 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
2752 if (sinfo
.vers
== 0)
2754 sinfo
.failed
= false;
2756 elf_link_hash_traverse (elf_hash_table (info
),
2757 elf_link_find_version_dependencies
,
2760 if (elf_tdata (output_bfd
)->verref
== NULL
)
2764 /* We don't have any version definitions, so we can just
2765 remove the section. */
2767 for (spp
= &output_bfd
->sections
;
2768 *spp
!= s
->output_section
;
2769 spp
= &(*spp
)->next
)
2771 *spp
= s
->output_section
->next
;
2772 --output_bfd
->section_count
;
2776 Elf_Internal_Verneed
*t
;
2781 /* Build the version definition section. */
2784 for (t
= elf_tdata (output_bfd
)->verref
;
2788 Elf_Internal_Vernaux
*a
;
2790 size
+= sizeof (Elf_External_Verneed
);
2792 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2793 size
+= sizeof (Elf_External_Vernaux
);
2796 s
->_raw_size
= size
;
2797 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, size
);
2798 if (s
->contents
== NULL
)
2802 for (t
= elf_tdata (output_bfd
)->verref
;
2807 Elf_Internal_Vernaux
*a
;
2811 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2814 t
->vn_version
= VER_NEED_CURRENT
;
2816 if (elf_dt_name (t
->vn_bfd
) != NULL
)
2817 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2818 elf_dt_name (t
->vn_bfd
),
2821 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2822 t
->vn_bfd
->filename
, true, false);
2823 if (indx
== (bfd_size_type
) -1)
2826 t
->vn_aux
= sizeof (Elf_External_Verneed
);
2827 if (t
->vn_nextref
== NULL
)
2830 t
->vn_next
= (sizeof (Elf_External_Verneed
)
2831 + caux
* sizeof (Elf_External_Vernaux
));
2833 _bfd_elf_swap_verneed_out (output_bfd
, t
,
2834 (Elf_External_Verneed
*) p
);
2835 p
+= sizeof (Elf_External_Verneed
);
2837 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2839 a
->vna_hash
= bfd_elf_hash ((const unsigned char *)
2841 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2842 a
->vna_nodename
, true, false);
2843 if (indx
== (bfd_size_type
) -1)
2846 if (a
->vna_nextptr
== NULL
)
2849 a
->vna_next
= sizeof (Elf_External_Vernaux
);
2851 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
2852 (Elf_External_Vernaux
*) p
);
2853 p
+= sizeof (Elf_External_Vernaux
);
2857 if (! elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
2858 || ! elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
2861 elf_tdata (output_bfd
)->cverrefs
= crefs
;
2865 dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2867 /* Work out the size of the symbol version section. */
2868 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
2869 BFD_ASSERT (s
!= NULL
);
2870 if (dynsymcount
== 0
2871 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
2875 /* We don't need any symbol versions; just discard the
2877 for (spp
= &output_bfd
->sections
;
2878 *spp
!= s
->output_section
;
2879 spp
= &(*spp
)->next
)
2881 *spp
= s
->output_section
->next
;
2882 --output_bfd
->section_count
;
2886 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
2887 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
2888 if (s
->contents
== NULL
)
2891 if (! elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
2895 /* Set the size of the .dynsym and .hash sections. We counted
2896 the number of dynamic symbols in elf_link_add_object_symbols.
2897 We will build the contents of .dynsym and .hash when we build
2898 the final symbol table, because until then we do not know the
2899 correct value to give the symbols. We built the .dynstr
2900 section as we went along in elf_link_add_object_symbols. */
2901 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
2902 BFD_ASSERT (s
!= NULL
);
2903 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
2904 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2905 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2908 /* The first entry in .dynsym is a dummy symbol. */
2915 elf_swap_symbol_out (output_bfd
, &isym
,
2916 (PTR
) (Elf_External_Sym
*) s
->contents
);
2918 /* Compute the size of the hashing table. As a side effect this
2919 computes the hash values for all the names we export. */
2920 bucketcount
= compute_bucket_count (info
);
2922 s
= bfd_get_section_by_name (dynobj
, ".hash");
2923 BFD_ASSERT (s
!= NULL
);
2924 s
->_raw_size
= (2 + bucketcount
+ dynsymcount
) * (ARCH_SIZE
/ 8);
2925 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2926 if (s
->contents
== NULL
)
2928 memset (s
->contents
, 0, (size_t) s
->_raw_size
);
2930 put_word (output_bfd
, bucketcount
, s
->contents
);
2931 put_word (output_bfd
, dynsymcount
, s
->contents
+ (ARCH_SIZE
/ 8));
2933 elf_hash_table (info
)->bucketcount
= bucketcount
;
2935 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
2936 BFD_ASSERT (s
!= NULL
);
2937 s
->_raw_size
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
2939 if (! elf_add_dynamic_entry (info
, DT_NULL
, 0))
2946 /* Fix up the flags for a symbol. This handles various cases which
2947 can only be fixed after all the input files are seen. This is
2948 currently called by both adjust_dynamic_symbol and
2949 assign_sym_version, which is unnecessary but perhaps more robust in
2950 the face of future changes. */
2953 elf_fix_symbol_flags (h
, eif
)
2954 struct elf_link_hash_entry
*h
;
2955 struct elf_info_failed
*eif
;
2957 /* If this symbol was mentioned in a non-ELF file, try to set
2958 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2959 permit a non-ELF file to correctly refer to a symbol defined in
2960 an ELF dynamic object. */
2961 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
2963 if (h
->root
.type
!= bfd_link_hash_defined
2964 && h
->root
.type
!= bfd_link_hash_defweak
)
2965 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2968 if (h
->root
.u
.def
.section
->owner
!= NULL
2969 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2970 == bfd_target_elf_flavour
))
2971 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2973 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2976 if (h
->dynindx
== -1
2977 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2978 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
2980 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2988 /* If this is a final link, and the symbol was defined as a common
2989 symbol in a regular object file, and there was no definition in
2990 any dynamic object, then the linker will have allocated space for
2991 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2992 flag will not have been set. */
2993 if (h
->root
.type
== bfd_link_hash_defined
2994 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2995 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
2996 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2997 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2998 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
3000 /* If -Bsymbolic was used (which means to bind references to global
3001 symbols to the definition within the shared object), and this
3002 symbol was defined in a regular object, then it actually doesn't
3003 need a PLT entry. */
3004 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
3005 && eif
->info
->shared
3006 && eif
->info
->symbolic
3007 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3009 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
3010 h
->plt
.offset
= (bfd_vma
) -1;
3016 /* Make the backend pick a good value for a dynamic symbol. This is
3017 called via elf_link_hash_traverse, and also calls itself
3021 elf_adjust_dynamic_symbol (h
, data
)
3022 struct elf_link_hash_entry
*h
;
3025 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3027 struct elf_backend_data
*bed
;
3029 /* Ignore indirect symbols. These are added by the versioning code. */
3030 if (h
->root
.type
== bfd_link_hash_indirect
)
3033 /* Fix the symbol flags. */
3034 if (! elf_fix_symbol_flags (h
, eif
))
3037 /* If this symbol does not require a PLT entry, and it is not
3038 defined by a dynamic object, or is not referenced by a regular
3039 object, ignore it. We do have to handle a weak defined symbol,
3040 even if no regular object refers to it, if we decided to add it
3041 to the dynamic symbol table. FIXME: Do we normally need to worry
3042 about symbols which are defined by one dynamic object and
3043 referenced by another one? */
3044 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
3045 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
3046 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3047 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
3048 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
3050 h
->plt
.offset
= (bfd_vma
) -1;
3054 /* If we've already adjusted this symbol, don't do it again. This
3055 can happen via a recursive call. */
3056 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
3059 /* Don't look at this symbol again. Note that we must set this
3060 after checking the above conditions, because we may look at a
3061 symbol once, decide not to do anything, and then get called
3062 recursively later after REF_REGULAR is set below. */
3063 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
3065 /* If this is a weak definition, and we know a real definition, and
3066 the real symbol is not itself defined by a regular object file,
3067 then get a good value for the real definition. We handle the
3068 real symbol first, for the convenience of the backend routine.
3070 Note that there is a confusing case here. If the real definition
3071 is defined by a regular object file, we don't get the real symbol
3072 from the dynamic object, but we do get the weak symbol. If the
3073 processor backend uses a COPY reloc, then if some routine in the
3074 dynamic object changes the real symbol, we will not see that
3075 change in the corresponding weak symbol. This is the way other
3076 ELF linkers work as well, and seems to be a result of the shared
3079 I will clarify this issue. Most SVR4 shared libraries define the
3080 variable _timezone and define timezone as a weak synonym. The
3081 tzset call changes _timezone. If you write
3082 extern int timezone;
3084 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3085 you might expect that, since timezone is a synonym for _timezone,
3086 the same number will print both times. However, if the processor
3087 backend uses a COPY reloc, then actually timezone will be copied
3088 into your process image, and, since you define _timezone
3089 yourself, _timezone will not. Thus timezone and _timezone will
3090 wind up at different memory locations. The tzset call will set
3091 _timezone, leaving timezone unchanged. */
3093 if (h
->weakdef
!= NULL
)
3095 struct elf_link_hash_entry
*weakdef
;
3097 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3098 || h
->root
.type
== bfd_link_hash_defweak
);
3099 weakdef
= h
->weakdef
;
3100 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
3101 || weakdef
->root
.type
== bfd_link_hash_defweak
);
3102 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
3103 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
3105 /* This symbol is defined by a regular object file, so we
3106 will not do anything special. Clear weakdef for the
3107 convenience of the processor backend. */
3112 /* There is an implicit reference by a regular object file
3113 via the weak symbol. */
3114 weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
3115 if (! elf_adjust_dynamic_symbol (weakdef
, (PTR
) eif
))
3120 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
3121 bed
= get_elf_backend_data (dynobj
);
3122 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3131 /* This routine is used to export all defined symbols into the dynamic
3132 symbol table. It is called via elf_link_hash_traverse. */
3135 elf_export_symbol (h
, data
)
3136 struct elf_link_hash_entry
*h
;
3139 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3141 /* Ignore indirect symbols. These are added by the versioning code. */
3142 if (h
->root
.type
== bfd_link_hash_indirect
)
3145 if (h
->dynindx
== -1
3146 && (h
->elf_link_hash_flags
3147 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
3149 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3159 /* Look through the symbols which are defined in other shared
3160 libraries and referenced here. Update the list of version
3161 dependencies. This will be put into the .gnu.version_r section.
3162 This function is called via elf_link_hash_traverse. */
3165 elf_link_find_version_dependencies (h
, data
)
3166 struct elf_link_hash_entry
*h
;
3169 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
3170 Elf_Internal_Verneed
*t
;
3171 Elf_Internal_Vernaux
*a
;
3173 /* We only care about symbols defined in shared objects with version
3175 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3176 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
3178 || h
->verinfo
.verdef
== NULL
)
3181 /* See if we already know about this version. */
3182 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
3184 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
3187 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3188 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
3194 /* This is a new version. Add it to tree we are building. */
3198 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *t
);
3201 rinfo
->failed
= true;
3205 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
3206 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
3207 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
3210 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *a
);
3212 /* Note that we are copying a string pointer here, and testing it
3213 above. If bfd_elf_string_from_elf_section is ever changed to
3214 discard the string data when low in memory, this will have to be
3216 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
3218 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
3219 a
->vna_nextptr
= t
->vn_auxptr
;
3221 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
3224 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
3231 /* Figure out appropriate versions for all the symbols. We may not
3232 have the version number script until we have read all of the input
3233 files, so until that point we don't know which symbols should be
3234 local. This function is called via elf_link_hash_traverse. */
3237 elf_link_assign_sym_version (h
, data
)
3238 struct elf_link_hash_entry
*h
;
3241 struct elf_assign_sym_version_info
*sinfo
=
3242 (struct elf_assign_sym_version_info
*) data
;
3243 struct bfd_link_info
*info
= sinfo
->info
;
3244 struct elf_info_failed eif
;
3247 /* Fix the symbol flags. */
3250 if (! elf_fix_symbol_flags (h
, &eif
))
3253 sinfo
->failed
= true;
3257 /* We only need version numbers for symbols defined in regular
3259 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
3262 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3263 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
3265 struct bfd_elf_version_tree
*t
;
3270 /* There are two consecutive ELF_VER_CHR characters if this is
3271 not a hidden symbol. */
3273 if (*p
== ELF_VER_CHR
)
3279 /* If there is no version string, we can just return out. */
3283 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
3287 /* Look for the version. If we find it, it is no longer weak. */
3288 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
3290 if (strcmp (t
->name
, p
) == 0)
3294 struct bfd_elf_version_expr
*d
;
3296 len
= p
- h
->root
.root
.string
;
3297 alc
= bfd_alloc (sinfo
->output_bfd
, len
);
3300 strncpy (alc
, h
->root
.root
.string
, len
- 1);
3301 alc
[len
- 1] = '\0';
3302 if (alc
[len
- 2] == ELF_VER_CHR
)
3303 alc
[len
- 2] = '\0';
3305 h
->verinfo
.vertree
= t
;
3309 if (t
->globals
!= NULL
)
3311 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3313 if ((d
->match
[0] == '*' && d
->match
[1] == '\0')
3314 || fnmatch (d
->match
, alc
, 0) == 0)
3319 /* See if there is anything to force this symbol to
3321 if (d
== NULL
&& t
->locals
!= NULL
)
3323 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3325 if ((d
->match
[0] == '*' && d
->match
[1] == '\0')
3326 || fnmatch (d
->match
, alc
, 0) == 0)
3328 if (h
->dynindx
!= -1
3330 && ! sinfo
->export_dynamic
)
3332 sinfo
->removed_dynamic
= true;
3333 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3334 h
->elf_link_hash_flags
&=~
3335 ELF_LINK_HASH_NEEDS_PLT
;
3337 h
->plt
.offset
= (bfd_vma
) -1;
3338 /* FIXME: The name of the symbol has
3339 already been recorded in the dynamic
3340 string table section. */
3348 bfd_release (sinfo
->output_bfd
, alc
);
3353 /* If we are building an application, we need to create a
3354 version node for this version. */
3355 if (t
== NULL
&& ! info
->shared
)
3357 struct bfd_elf_version_tree
**pp
;
3360 /* If we aren't going to export this symbol, we don't need
3361 to worry about it. */
3362 if (h
->dynindx
== -1)
3365 t
= ((struct bfd_elf_version_tree
*)
3366 bfd_alloc (sinfo
->output_bfd
, sizeof *t
));
3369 sinfo
->failed
= true;
3378 t
->name_indx
= (unsigned int) -1;
3382 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
3384 t
->vernum
= version_index
;
3388 h
->verinfo
.vertree
= t
;
3392 /* We could not find the version for a symbol when
3393 generating a shared archive. Return an error. */
3394 (*_bfd_error_handler
)
3395 (_("%s: undefined versioned symbol name %s"),
3396 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
3397 bfd_set_error (bfd_error_bad_value
);
3398 sinfo
->failed
= true;
3403 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
3406 /* If we don't have a version for this symbol, see if we can find
3408 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
3410 struct bfd_elf_version_tree
*t
;
3411 struct bfd_elf_version_tree
*deflt
;
3412 struct bfd_elf_version_expr
*d
;
3414 /* See if can find what version this symbol is in. If the
3415 symbol is supposed to be local, then don't actually register
3418 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
3420 if (t
->globals
!= NULL
)
3422 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3424 if (fnmatch (d
->match
, h
->root
.root
.string
, 0) == 0)
3426 h
->verinfo
.vertree
= t
;
3435 if (t
->locals
!= NULL
)
3437 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3439 if (d
->match
[0] == '*' && d
->match
[1] == '\0')
3441 else if (fnmatch (d
->match
, h
->root
.root
.string
, 0) == 0)
3443 h
->verinfo
.vertree
= t
;
3444 if (h
->dynindx
!= -1
3446 && ! sinfo
->export_dynamic
)
3448 sinfo
->removed_dynamic
= true;
3449 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3450 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
3452 h
->plt
.offset
= (bfd_vma
) -1;
3453 /* FIXME: The name of the symbol has already
3454 been recorded in the dynamic string table
3466 if (deflt
!= NULL
&& h
->verinfo
.vertree
== NULL
)
3468 h
->verinfo
.vertree
= deflt
;
3469 if (h
->dynindx
!= -1
3471 && ! sinfo
->export_dynamic
)
3473 sinfo
->removed_dynamic
= true;
3474 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3475 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
3477 h
->plt
.offset
= (bfd_vma
) -1;
3478 /* FIXME: The name of the symbol has already been
3479 recorded in the dynamic string table section. */
3487 /* This function is used to renumber the dynamic symbols, if some of
3488 them are removed because they are marked as local. This is called
3489 via elf_link_hash_traverse. */
3492 elf_link_renumber_dynsyms (h
, data
)
3493 struct elf_link_hash_entry
*h
;
3496 struct bfd_link_info
*info
= (struct bfd_link_info
*) data
;
3498 if (h
->dynindx
!= -1)
3500 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
3501 ++elf_hash_table (info
)->dynsymcount
;
3507 /* Final phase of ELF linker. */
3509 /* A structure we use to avoid passing large numbers of arguments. */
3511 struct elf_final_link_info
3513 /* General link information. */
3514 struct bfd_link_info
*info
;
3517 /* Symbol string table. */
3518 struct bfd_strtab_hash
*symstrtab
;
3519 /* .dynsym section. */
3520 asection
*dynsym_sec
;
3521 /* .hash section. */
3523 /* symbol version section (.gnu.version). */
3524 asection
*symver_sec
;
3525 /* Buffer large enough to hold contents of any section. */
3527 /* Buffer large enough to hold external relocs of any section. */
3528 PTR external_relocs
;
3529 /* Buffer large enough to hold internal relocs of any section. */
3530 Elf_Internal_Rela
*internal_relocs
;
3531 /* Buffer large enough to hold external local symbols of any input
3533 Elf_External_Sym
*external_syms
;
3534 /* Buffer large enough to hold internal local symbols of any input
3536 Elf_Internal_Sym
*internal_syms
;
3537 /* Array large enough to hold a symbol index for each local symbol
3538 of any input BFD. */
3540 /* Array large enough to hold a section pointer for each local
3541 symbol of any input BFD. */
3542 asection
**sections
;
3543 /* Buffer to hold swapped out symbols. */
3544 Elf_External_Sym
*symbuf
;
3545 /* Number of swapped out symbols in buffer. */
3546 size_t symbuf_count
;
3547 /* Number of symbols which fit in symbuf. */
3551 static boolean elf_link_output_sym
3552 PARAMS ((struct elf_final_link_info
*, const char *,
3553 Elf_Internal_Sym
*, asection
*));
3554 static boolean elf_link_flush_output_syms
3555 PARAMS ((struct elf_final_link_info
*));
3556 static boolean elf_link_output_extsym
3557 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3558 static boolean elf_link_input_bfd
3559 PARAMS ((struct elf_final_link_info
*, bfd
*));
3560 static boolean elf_reloc_link_order
3561 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
3562 struct bfd_link_order
*));
3564 /* This struct is used to pass information to elf_link_output_extsym. */
3566 struct elf_outext_info
3570 struct elf_final_link_info
*finfo
;
3573 /* Do the final step of an ELF link. */
3576 elf_bfd_final_link (abfd
, info
)
3578 struct bfd_link_info
*info
;
3582 struct elf_final_link_info finfo
;
3583 register asection
*o
;
3584 register struct bfd_link_order
*p
;
3586 size_t max_contents_size
;
3587 size_t max_external_reloc_size
;
3588 size_t max_internal_reloc_count
;
3589 size_t max_sym_count
;
3591 Elf_Internal_Sym elfsym
;
3593 Elf_Internal_Shdr
*symtab_hdr
;
3594 Elf_Internal_Shdr
*symstrtab_hdr
;
3595 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3596 struct elf_outext_info eoinfo
;
3599 abfd
->flags
|= DYNAMIC
;
3601 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
3602 dynobj
= elf_hash_table (info
)->dynobj
;
3605 finfo
.output_bfd
= abfd
;
3606 finfo
.symstrtab
= elf_stringtab_init ();
3607 if (finfo
.symstrtab
== NULL
)
3612 finfo
.dynsym_sec
= NULL
;
3613 finfo
.hash_sec
= NULL
;
3614 finfo
.symver_sec
= NULL
;
3618 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
3619 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
3620 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
3621 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3622 /* Note that it is OK if symver_sec is NULL. */
3625 finfo
.contents
= NULL
;
3626 finfo
.external_relocs
= NULL
;
3627 finfo
.internal_relocs
= NULL
;
3628 finfo
.external_syms
= NULL
;
3629 finfo
.internal_syms
= NULL
;
3630 finfo
.indices
= NULL
;
3631 finfo
.sections
= NULL
;
3632 finfo
.symbuf
= NULL
;
3633 finfo
.symbuf_count
= 0;
3635 /* Count up the number of relocations we will output for each output
3636 section, so that we know the sizes of the reloc sections. We
3637 also figure out some maximum sizes. */
3638 max_contents_size
= 0;
3639 max_external_reloc_size
= 0;
3640 max_internal_reloc_count
= 0;
3642 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3646 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3648 if (p
->type
== bfd_section_reloc_link_order
3649 || p
->type
== bfd_symbol_reloc_link_order
)
3651 else if (p
->type
== bfd_indirect_link_order
)
3655 sec
= p
->u
.indirect
.section
;
3657 /* Mark all sections which are to be included in the
3658 link. This will normally be every section. We need
3659 to do this so that we can identify any sections which
3660 the linker has decided to not include. */
3661 sec
->linker_mark
= true;
3663 if (info
->relocateable
)
3664 o
->reloc_count
+= sec
->reloc_count
;
3666 if (sec
->_raw_size
> max_contents_size
)
3667 max_contents_size
= sec
->_raw_size
;
3668 if (sec
->_cooked_size
> max_contents_size
)
3669 max_contents_size
= sec
->_cooked_size
;
3671 /* We are interested in just local symbols, not all
3673 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
3674 && (sec
->owner
->flags
& DYNAMIC
) == 0)
3678 if (elf_bad_symtab (sec
->owner
))
3679 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
3680 / sizeof (Elf_External_Sym
));
3682 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
3684 if (sym_count
> max_sym_count
)
3685 max_sym_count
= sym_count
;
3687 if ((sec
->flags
& SEC_RELOC
) != 0)
3691 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
3692 if (ext_size
> max_external_reloc_size
)
3693 max_external_reloc_size
= ext_size
;
3694 if (sec
->reloc_count
> max_internal_reloc_count
)
3695 max_internal_reloc_count
= sec
->reloc_count
;
3701 if (o
->reloc_count
> 0)
3702 o
->flags
|= SEC_RELOC
;
3705 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3706 set it (this is probably a bug) and if it is set
3707 assign_section_numbers will create a reloc section. */
3708 o
->flags
&=~ SEC_RELOC
;
3711 /* If the SEC_ALLOC flag is not set, force the section VMA to
3712 zero. This is done in elf_fake_sections as well, but forcing
3713 the VMA to 0 here will ensure that relocs against these
3714 sections are handled correctly. */
3715 if ((o
->flags
& SEC_ALLOC
) == 0
3716 && ! o
->user_set_vma
)
3720 /* Figure out the file positions for everything but the symbol table
3721 and the relocs. We set symcount to force assign_section_numbers
3722 to create a symbol table. */
3723 abfd
->symcount
= info
->strip
== strip_all
? 0 : 1;
3724 BFD_ASSERT (! abfd
->output_has_begun
);
3725 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
3728 /* That created the reloc sections. Set their sizes, and assign
3729 them file positions, and allocate some buffers. */
3730 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3732 if ((o
->flags
& SEC_RELOC
) != 0)
3734 Elf_Internal_Shdr
*rel_hdr
;
3735 register struct elf_link_hash_entry
**p
, **pend
;
3737 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
3739 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* o
->reloc_count
;
3741 /* The contents field must last into write_object_contents,
3742 so we allocate it with bfd_alloc rather than malloc. */
3743 rel_hdr
->contents
= (PTR
) bfd_alloc (abfd
, rel_hdr
->sh_size
);
3744 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
3747 p
= ((struct elf_link_hash_entry
**)
3748 bfd_malloc (o
->reloc_count
3749 * sizeof (struct elf_link_hash_entry
*)));
3750 if (p
== NULL
&& o
->reloc_count
!= 0)
3752 elf_section_data (o
)->rel_hashes
= p
;
3753 pend
= p
+ o
->reloc_count
;
3754 for (; p
< pend
; p
++)
3757 /* Use the reloc_count field as an index when outputting the
3763 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3765 /* We have now assigned file positions for all the sections except
3766 .symtab and .strtab. We start the .symtab section at the current
3767 file position, and write directly to it. We build the .strtab
3768 section in memory. */
3770 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3771 /* sh_name is set in prep_headers. */
3772 symtab_hdr
->sh_type
= SHT_SYMTAB
;
3773 symtab_hdr
->sh_flags
= 0;
3774 symtab_hdr
->sh_addr
= 0;
3775 symtab_hdr
->sh_size
= 0;
3776 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
3777 /* sh_link is set in assign_section_numbers. */
3778 /* sh_info is set below. */
3779 /* sh_offset is set just below. */
3780 symtab_hdr
->sh_addralign
= 4; /* FIXME: system dependent? */
3782 off
= elf_tdata (abfd
)->next_file_pos
;
3783 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
3785 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3786 incorrect. We do not yet know the size of the .symtab section.
3787 We correct next_file_pos below, after we do know the size. */
3789 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3790 continuously seeking to the right position in the file. */
3791 if (! info
->keep_memory
|| max_sym_count
< 20)
3792 finfo
.symbuf_size
= 20;
3794 finfo
.symbuf_size
= max_sym_count
;
3795 finfo
.symbuf
= ((Elf_External_Sym
*)
3796 bfd_malloc (finfo
.symbuf_size
* sizeof (Elf_External_Sym
)));
3797 if (finfo
.symbuf
== NULL
)
3800 /* Start writing out the symbol table. The first symbol is always a
3802 if (info
->strip
!= strip_all
|| info
->relocateable
)
3804 elfsym
.st_value
= 0;
3807 elfsym
.st_other
= 0;
3808 elfsym
.st_shndx
= SHN_UNDEF
;
3809 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
3810 &elfsym
, bfd_und_section_ptr
))
3815 /* Some standard ELF linkers do this, but we don't because it causes
3816 bootstrap comparison failures. */
3817 /* Output a file symbol for the output file as the second symbol.
3818 We output this even if we are discarding local symbols, although
3819 I'm not sure if this is correct. */
3820 elfsym
.st_value
= 0;
3822 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
3823 elfsym
.st_other
= 0;
3824 elfsym
.st_shndx
= SHN_ABS
;
3825 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
3826 &elfsym
, bfd_abs_section_ptr
))
3830 /* Output a symbol for each section. We output these even if we are
3831 discarding local symbols, since they are used for relocs. These
3832 symbols have no names. We store the index of each one in the
3833 index field of the section, so that we can find it again when
3834 outputting relocs. */
3835 if (info
->strip
!= strip_all
|| info
->relocateable
)
3838 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
3839 elfsym
.st_other
= 0;
3840 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
3842 o
= section_from_elf_index (abfd
, i
);
3844 o
->target_index
= abfd
->symcount
;
3845 elfsym
.st_shndx
= i
;
3846 if (info
->relocateable
|| o
== NULL
)
3847 elfsym
.st_value
= 0;
3849 elfsym
.st_value
= o
->vma
;
3850 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
3856 /* Allocate some memory to hold information read in from the input
3858 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
3859 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
3860 finfo
.internal_relocs
= ((Elf_Internal_Rela
*)
3861 bfd_malloc (max_internal_reloc_count
3862 * sizeof (Elf_Internal_Rela
)));
3863 finfo
.external_syms
= ((Elf_External_Sym
*)
3864 bfd_malloc (max_sym_count
3865 * sizeof (Elf_External_Sym
)));
3866 finfo
.internal_syms
= ((Elf_Internal_Sym
*)
3867 bfd_malloc (max_sym_count
3868 * sizeof (Elf_Internal_Sym
)));
3869 finfo
.indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
3870 finfo
.sections
= ((asection
**)
3871 bfd_malloc (max_sym_count
* sizeof (asection
*)));
3872 if ((finfo
.contents
== NULL
&& max_contents_size
!= 0)
3873 || (finfo
.external_relocs
== NULL
&& max_external_reloc_size
!= 0)
3874 || (finfo
.internal_relocs
== NULL
&& max_internal_reloc_count
!= 0)
3875 || (finfo
.external_syms
== NULL
&& max_sym_count
!= 0)
3876 || (finfo
.internal_syms
== NULL
&& max_sym_count
!= 0)
3877 || (finfo
.indices
== NULL
&& max_sym_count
!= 0)
3878 || (finfo
.sections
== NULL
&& max_sym_count
!= 0))
3881 /* Since ELF permits relocations to be against local symbols, we
3882 must have the local symbols available when we do the relocations.
3883 Since we would rather only read the local symbols once, and we
3884 would rather not keep them in memory, we handle all the
3885 relocations for a single input file at the same time.
3887 Unfortunately, there is no way to know the total number of local
3888 symbols until we have seen all of them, and the local symbol
3889 indices precede the global symbol indices. This means that when
3890 we are generating relocateable output, and we see a reloc against
3891 a global symbol, we can not know the symbol index until we have
3892 finished examining all the local symbols to see which ones we are
3893 going to output. To deal with this, we keep the relocations in
3894 memory, and don't output them until the end of the link. This is
3895 an unfortunate waste of memory, but I don't see a good way around
3896 it. Fortunately, it only happens when performing a relocateable
3897 link, which is not the common case. FIXME: If keep_memory is set
3898 we could write the relocs out and then read them again; I don't
3899 know how bad the memory loss will be. */
3901 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3902 sub
->output_has_begun
= false;
3903 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3905 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3907 if (p
->type
== bfd_indirect_link_order
3908 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
3909 == bfd_target_elf_flavour
))
3911 sub
= p
->u
.indirect
.section
->owner
;
3912 if (! sub
->output_has_begun
)
3914 if (! elf_link_input_bfd (&finfo
, sub
))
3916 sub
->output_has_begun
= true;
3919 else if (p
->type
== bfd_section_reloc_link_order
3920 || p
->type
== bfd_symbol_reloc_link_order
)
3922 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
3927 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
3933 /* That wrote out all the local symbols. Finish up the symbol table
3934 with the global symbols. */
3936 if (info
->strip
!= strip_all
&& info
->shared
)
3938 /* Output any global symbols that got converted to local in a
3939 version script. We do this in a separate step since ELF
3940 requires all local symbols to appear prior to any global
3941 symbols. FIXME: We should only do this if some global
3942 symbols were, in fact, converted to become local. FIXME:
3943 Will this work correctly with the Irix 5 linker? */
3944 eoinfo
.failed
= false;
3945 eoinfo
.finfo
= &finfo
;
3946 eoinfo
.localsyms
= true;
3947 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3953 /* The sh_info field records the index of the first non local
3955 symtab_hdr
->sh_info
= abfd
->symcount
;
3957 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
= 1;
3959 /* We get the global symbols from the hash table. */
3960 eoinfo
.failed
= false;
3961 eoinfo
.localsyms
= false;
3962 eoinfo
.finfo
= &finfo
;
3963 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3968 /* Flush all symbols to the file. */
3969 if (! elf_link_flush_output_syms (&finfo
))
3972 /* Now we know the size of the symtab section. */
3973 off
+= symtab_hdr
->sh_size
;
3975 /* Finish up and write out the symbol string table (.strtab)
3977 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3978 /* sh_name was set in prep_headers. */
3979 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
3980 symstrtab_hdr
->sh_flags
= 0;
3981 symstrtab_hdr
->sh_addr
= 0;
3982 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
3983 symstrtab_hdr
->sh_entsize
= 0;
3984 symstrtab_hdr
->sh_link
= 0;
3985 symstrtab_hdr
->sh_info
= 0;
3986 /* sh_offset is set just below. */
3987 symstrtab_hdr
->sh_addralign
= 1;
3989 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, true);
3990 elf_tdata (abfd
)->next_file_pos
= off
;
3992 if (abfd
->symcount
> 0)
3994 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
3995 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
3999 /* Adjust the relocs to have the correct symbol indices. */
4000 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4002 struct elf_link_hash_entry
**rel_hash
;
4003 Elf_Internal_Shdr
*rel_hdr
;
4005 if ((o
->flags
& SEC_RELOC
) == 0)
4008 rel_hash
= elf_section_data (o
)->rel_hashes
;
4009 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
4010 for (i
= 0; i
< o
->reloc_count
; i
++, rel_hash
++)
4012 if (*rel_hash
== NULL
)
4015 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
4017 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4019 Elf_External_Rel
*erel
;
4020 Elf_Internal_Rel irel
;
4022 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
4023 elf_swap_reloc_in (abfd
, erel
, &irel
);
4024 irel
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4025 ELF_R_TYPE (irel
.r_info
));
4026 elf_swap_reloc_out (abfd
, &irel
, erel
);
4030 Elf_External_Rela
*erela
;
4031 Elf_Internal_Rela irela
;
4033 BFD_ASSERT (rel_hdr
->sh_entsize
4034 == sizeof (Elf_External_Rela
));
4036 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
4037 elf_swap_reloca_in (abfd
, erela
, &irela
);
4038 irela
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4039 ELF_R_TYPE (irela
.r_info
));
4040 elf_swap_reloca_out (abfd
, &irela
, erela
);
4044 /* Set the reloc_count field to 0 to prevent write_relocs from
4045 trying to swap the relocs out itself. */
4049 /* If we are linking against a dynamic object, or generating a
4050 shared library, finish up the dynamic linking information. */
4053 Elf_External_Dyn
*dyncon
, *dynconend
;
4055 /* Fix up .dynamic entries. */
4056 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
4057 BFD_ASSERT (o
!= NULL
);
4059 dyncon
= (Elf_External_Dyn
*) o
->contents
;
4060 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
4061 for (; dyncon
< dynconend
; dyncon
++)
4063 Elf_Internal_Dyn dyn
;
4067 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
4074 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on
4075 magic _init and _fini symbols. This is pretty ugly,
4076 but we are compatible. */
4084 struct elf_link_hash_entry
*h
;
4086 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
4087 false, false, true);
4089 && (h
->root
.type
== bfd_link_hash_defined
4090 || h
->root
.type
== bfd_link_hash_defweak
))
4092 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
4093 o
= h
->root
.u
.def
.section
;
4094 if (o
->output_section
!= NULL
)
4095 dyn
.d_un
.d_val
+= (o
->output_section
->vma
4096 + o
->output_offset
);
4099 /* The symbol is imported from another shared
4100 library and does not apply to this one. */
4104 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
4119 name
= ".gnu.version_d";
4122 name
= ".gnu.version_r";
4125 name
= ".gnu.version";
4127 o
= bfd_get_section_by_name (abfd
, name
);
4128 BFD_ASSERT (o
!= NULL
);
4129 dyn
.d_un
.d_ptr
= o
->vma
;
4130 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
4137 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
4142 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
4144 Elf_Internal_Shdr
*hdr
;
4146 hdr
= elf_elfsections (abfd
)[i
];
4147 if (hdr
->sh_type
== type
4148 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
4150 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
4151 dyn
.d_un
.d_val
+= hdr
->sh_size
;
4154 if (dyn
.d_un
.d_val
== 0
4155 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
4156 dyn
.d_un
.d_val
= hdr
->sh_addr
;
4160 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
4166 /* If we have created any dynamic sections, then output them. */
4169 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
4172 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4174 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4175 || o
->_raw_size
== 0)
4177 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
4179 /* At this point, we are only interested in sections
4180 created by elf_link_create_dynamic_sections. */
4183 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
4185 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
4187 if (! bfd_set_section_contents (abfd
, o
->output_section
,
4188 o
->contents
, o
->output_offset
,
4196 /* The contents of the .dynstr section are actually in a
4198 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
4199 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
4200 || ! _bfd_stringtab_emit (abfd
,
4201 elf_hash_table (info
)->dynstr
))
4207 /* If we have optimized stabs strings, output them. */
4208 if (elf_hash_table (info
)->stab_info
!= NULL
)
4210 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
4214 if (finfo
.symstrtab
!= NULL
)
4215 _bfd_stringtab_free (finfo
.symstrtab
);
4216 if (finfo
.contents
!= NULL
)
4217 free (finfo
.contents
);
4218 if (finfo
.external_relocs
!= NULL
)
4219 free (finfo
.external_relocs
);
4220 if (finfo
.internal_relocs
!= NULL
)
4221 free (finfo
.internal_relocs
);
4222 if (finfo
.external_syms
!= NULL
)
4223 free (finfo
.external_syms
);
4224 if (finfo
.internal_syms
!= NULL
)
4225 free (finfo
.internal_syms
);
4226 if (finfo
.indices
!= NULL
)
4227 free (finfo
.indices
);
4228 if (finfo
.sections
!= NULL
)
4229 free (finfo
.sections
);
4230 if (finfo
.symbuf
!= NULL
)
4231 free (finfo
.symbuf
);
4232 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4234 if ((o
->flags
& SEC_RELOC
) != 0
4235 && elf_section_data (o
)->rel_hashes
!= NULL
)
4236 free (elf_section_data (o
)->rel_hashes
);
4239 elf_tdata (abfd
)->linker
= true;
4244 if (finfo
.symstrtab
!= NULL
)
4245 _bfd_stringtab_free (finfo
.symstrtab
);
4246 if (finfo
.contents
!= NULL
)
4247 free (finfo
.contents
);
4248 if (finfo
.external_relocs
!= NULL
)
4249 free (finfo
.external_relocs
);
4250 if (finfo
.internal_relocs
!= NULL
)
4251 free (finfo
.internal_relocs
);
4252 if (finfo
.external_syms
!= NULL
)
4253 free (finfo
.external_syms
);
4254 if (finfo
.internal_syms
!= NULL
)
4255 free (finfo
.internal_syms
);
4256 if (finfo
.indices
!= NULL
)
4257 free (finfo
.indices
);
4258 if (finfo
.sections
!= NULL
)
4259 free (finfo
.sections
);
4260 if (finfo
.symbuf
!= NULL
)
4261 free (finfo
.symbuf
);
4262 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4264 if ((o
->flags
& SEC_RELOC
) != 0
4265 && elf_section_data (o
)->rel_hashes
!= NULL
)
4266 free (elf_section_data (o
)->rel_hashes
);
4272 /* Add a symbol to the output symbol table. */
4275 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
4276 struct elf_final_link_info
*finfo
;
4278 Elf_Internal_Sym
*elfsym
;
4279 asection
*input_sec
;
4281 boolean (*output_symbol_hook
) PARAMS ((bfd
*,
4282 struct bfd_link_info
*info
,
4287 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
4288 elf_backend_link_output_symbol_hook
;
4289 if (output_symbol_hook
!= NULL
)
4291 if (! ((*output_symbol_hook
)
4292 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
4296 if (name
== (const char *) NULL
|| *name
== '\0')
4297 elfsym
->st_name
= 0;
4300 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
4303 if (elfsym
->st_name
== (unsigned long) -1)
4307 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
4309 if (! elf_link_flush_output_syms (finfo
))
4313 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
,
4314 (PTR
) (finfo
->symbuf
+ finfo
->symbuf_count
));
4315 ++finfo
->symbuf_count
;
4317 ++finfo
->output_bfd
->symcount
;
4322 /* Flush the output symbols to the file. */
4325 elf_link_flush_output_syms (finfo
)
4326 struct elf_final_link_info
*finfo
;
4328 if (finfo
->symbuf_count
> 0)
4330 Elf_Internal_Shdr
*symtab
;
4332 symtab
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
4334 if (bfd_seek (finfo
->output_bfd
, symtab
->sh_offset
+ symtab
->sh_size
,
4336 || (bfd_write ((PTR
) finfo
->symbuf
, finfo
->symbuf_count
,
4337 sizeof (Elf_External_Sym
), finfo
->output_bfd
)
4338 != finfo
->symbuf_count
* sizeof (Elf_External_Sym
)))
4341 symtab
->sh_size
+= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
4343 finfo
->symbuf_count
= 0;
4349 /* Add an external symbol to the symbol table. This is called from
4350 the hash table traversal routine. When generating a shared object,
4351 we go through the symbol table twice. The first time we output
4352 anything that might have been forced to local scope in a version
4353 script. The second time we output the symbols that are still
4357 elf_link_output_extsym (h
, data
)
4358 struct elf_link_hash_entry
*h
;
4361 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
4362 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
4364 Elf_Internal_Sym sym
;
4365 asection
*input_sec
;
4367 /* Decide whether to output this symbol in this pass. */
4368 if (eoinfo
->localsyms
)
4370 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
4375 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4379 /* If we are not creating a shared library, and this symbol is
4380 referenced by a shared library but is not defined anywhere, then
4381 warn that it is undefined. If we do not do this, the runtime
4382 linker will complain that the symbol is undefined when the
4383 program is run. We don't have to worry about symbols that are
4384 referenced by regular files, because we will already have issued
4385 warnings for them. */
4386 if (! finfo
->info
->relocateable
4387 && ! finfo
->info
->shared
4388 && h
->root
.type
== bfd_link_hash_undefined
4389 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
4390 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
4392 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
4393 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
4394 (asection
*) NULL
, 0)))
4396 eoinfo
->failed
= true;
4401 /* We don't want to output symbols that have never been mentioned by
4402 a regular file, or that we have been told to strip. However, if
4403 h->indx is set to -2, the symbol is used by a reloc and we must
4407 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
4408 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
4409 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
4410 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
4412 else if (finfo
->info
->strip
== strip_all
4413 || (finfo
->info
->strip
== strip_some
4414 && bfd_hash_lookup (finfo
->info
->keep_hash
,
4415 h
->root
.root
.string
,
4416 false, false) == NULL
))
4421 /* If we're stripping it, and it's not a dynamic symbol, there's
4422 nothing else to do. */
4423 if (strip
&& h
->dynindx
== -1)
4427 sym
.st_size
= h
->size
;
4428 sym
.st_other
= h
->other
;
4429 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4430 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
4431 else if (h
->root
.type
== bfd_link_hash_undefweak
4432 || h
->root
.type
== bfd_link_hash_defweak
)
4433 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
4435 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
4437 switch (h
->root
.type
)
4440 case bfd_link_hash_new
:
4444 case bfd_link_hash_undefined
:
4445 input_sec
= bfd_und_section_ptr
;
4446 sym
.st_shndx
= SHN_UNDEF
;
4449 case bfd_link_hash_undefweak
:
4450 input_sec
= bfd_und_section_ptr
;
4451 sym
.st_shndx
= SHN_UNDEF
;
4454 case bfd_link_hash_defined
:
4455 case bfd_link_hash_defweak
:
4457 input_sec
= h
->root
.u
.def
.section
;
4458 if (input_sec
->output_section
!= NULL
)
4461 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
4462 input_sec
->output_section
);
4463 if (sym
.st_shndx
== (unsigned short) -1)
4465 (*_bfd_error_handler
)
4466 (_("%s: could not find output section %s for input section %s"),
4467 bfd_get_filename (finfo
->output_bfd
),
4468 input_sec
->output_section
->name
,
4470 eoinfo
->failed
= true;
4474 /* ELF symbols in relocateable files are section relative,
4475 but in nonrelocateable files they are virtual
4477 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
4478 if (! finfo
->info
->relocateable
)
4479 sym
.st_value
+= input_sec
->output_section
->vma
;
4483 BFD_ASSERT (input_sec
->owner
== NULL
4484 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
4485 sym
.st_shndx
= SHN_UNDEF
;
4486 input_sec
= bfd_und_section_ptr
;
4491 case bfd_link_hash_common
:
4492 input_sec
= h
->root
.u
.c
.p
->section
;
4493 sym
.st_shndx
= SHN_COMMON
;
4494 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
4497 case bfd_link_hash_indirect
:
4498 /* These symbols are created by symbol versioning. They point
4499 to the decorated version of the name. For example, if the
4500 symbol foo@@GNU_1.2 is the default, which should be used when
4501 foo is used with no version, then we add an indirect symbol
4502 foo which points to foo@@GNU_1.2. We ignore these symbols,
4503 since the indirected symbol is already in the hash table. If
4504 the indirect symbol is non-ELF, fall through and output it. */
4505 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) == 0)
4509 case bfd_link_hash_warning
:
4510 /* We can't represent these symbols in ELF, although a warning
4511 symbol may have come from a .gnu.warning.SYMBOL section. We
4512 just put the target symbol in the hash table. If the target
4513 symbol does not really exist, don't do anything. */
4514 if (h
->root
.u
.i
.link
->type
== bfd_link_hash_new
)
4516 return (elf_link_output_extsym
4517 ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
));
4520 /* Give the processor backend a chance to tweak the symbol value,
4521 and also to finish up anything that needs to be done for this
4523 if ((h
->dynindx
!= -1
4524 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4525 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4527 struct elf_backend_data
*bed
;
4529 bed
= get_elf_backend_data (finfo
->output_bfd
);
4530 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
4531 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
4533 eoinfo
->failed
= true;
4538 /* If this symbol should be put in the .dynsym section, then put it
4539 there now. We have already know the symbol index. We also fill
4540 in the entry in the .hash section. */
4541 if (h
->dynindx
!= -1
4542 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4546 bfd_byte
*bucketpos
;
4549 sym
.st_name
= h
->dynstr_index
;
4551 elf_swap_symbol_out (finfo
->output_bfd
, &sym
,
4552 (PTR
) (((Elf_External_Sym
*)
4553 finfo
->dynsym_sec
->contents
)
4556 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
4557 bucket
= h
->elf_hash_value
% bucketcount
;
4558 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
4559 + (bucket
+ 2) * (ARCH_SIZE
/ 8));
4560 chain
= get_word (finfo
->output_bfd
, bucketpos
);
4561 put_word (finfo
->output_bfd
, h
->dynindx
, bucketpos
);
4562 put_word (finfo
->output_bfd
, chain
,
4563 ((bfd_byte
*) finfo
->hash_sec
->contents
4564 + (bucketcount
+ 2 + h
->dynindx
) * (ARCH_SIZE
/ 8)));
4566 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
4568 Elf_Internal_Versym iversym
;
4570 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4572 if (h
->verinfo
.verdef
== NULL
)
4573 iversym
.vs_vers
= 0;
4575 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4579 if (h
->verinfo
.vertree
== NULL
)
4580 iversym
.vs_vers
= 1;
4582 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
4585 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
4586 iversym
.vs_vers
|= VERSYM_HIDDEN
;
4588 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
,
4589 (((Elf_External_Versym
*)
4590 finfo
->symver_sec
->contents
)
4595 /* If we're stripping it, then it was just a dynamic symbol, and
4596 there's nothing else to do. */
4600 h
->indx
= finfo
->output_bfd
->symcount
;
4602 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
4604 eoinfo
->failed
= true;
4611 /* Link an input file into the linker output file. This function
4612 handles all the sections and relocations of the input file at once.
4613 This is so that we only have to read the local symbols once, and
4614 don't have to keep them in memory. */
4617 elf_link_input_bfd (finfo
, input_bfd
)
4618 struct elf_final_link_info
*finfo
;
4621 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
4622 bfd
*, asection
*, bfd_byte
*,
4623 Elf_Internal_Rela
*,
4624 Elf_Internal_Sym
*, asection
**));
4626 Elf_Internal_Shdr
*symtab_hdr
;
4629 Elf_External_Sym
*external_syms
;
4630 Elf_External_Sym
*esym
;
4631 Elf_External_Sym
*esymend
;
4632 Elf_Internal_Sym
*isym
;
4634 asection
**ppsection
;
4637 output_bfd
= finfo
->output_bfd
;
4639 get_elf_backend_data (output_bfd
)->elf_backend_relocate_section
;
4641 /* If this is a dynamic object, we don't want to do anything here:
4642 we don't want the local symbols, and we don't want the section
4644 if ((input_bfd
->flags
& DYNAMIC
) != 0)
4647 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
4648 if (elf_bad_symtab (input_bfd
))
4650 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
4655 locsymcount
= symtab_hdr
->sh_info
;
4656 extsymoff
= symtab_hdr
->sh_info
;
4659 /* Read the local symbols. */
4660 if (symtab_hdr
->contents
!= NULL
)
4661 external_syms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
4662 else if (locsymcount
== 0)
4663 external_syms
= NULL
;
4666 external_syms
= finfo
->external_syms
;
4667 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
4668 || (bfd_read (external_syms
, sizeof (Elf_External_Sym
),
4669 locsymcount
, input_bfd
)
4670 != locsymcount
* sizeof (Elf_External_Sym
)))
4674 /* Swap in the local symbols and write out the ones which we know
4675 are going into the output file. */
4676 esym
= external_syms
;
4677 esymend
= esym
+ locsymcount
;
4678 isym
= finfo
->internal_syms
;
4679 pindex
= finfo
->indices
;
4680 ppsection
= finfo
->sections
;
4681 for (; esym
< esymend
; esym
++, isym
++, pindex
++, ppsection
++)
4685 Elf_Internal_Sym osym
;
4687 elf_swap_symbol_in (input_bfd
, esym
, isym
);
4690 if (elf_bad_symtab (input_bfd
))
4692 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
4699 if (isym
->st_shndx
== SHN_UNDEF
)
4700 isec
= bfd_und_section_ptr
;
4701 else if (isym
->st_shndx
> 0 && isym
->st_shndx
< SHN_LORESERVE
)
4702 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
4703 else if (isym
->st_shndx
== SHN_ABS
)
4704 isec
= bfd_abs_section_ptr
;
4705 else if (isym
->st_shndx
== SHN_COMMON
)
4706 isec
= bfd_com_section_ptr
;
4715 /* Don't output the first, undefined, symbol. */
4716 if (esym
== external_syms
)
4719 /* If we are stripping all symbols, we don't want to output this
4721 if (finfo
->info
->strip
== strip_all
)
4724 /* We never output section symbols. Instead, we use the section
4725 symbol of the corresponding section in the output file. */
4726 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
4729 /* If we are discarding all local symbols, we don't want to
4730 output this one. If we are generating a relocateable output
4731 file, then some of the local symbols may be required by
4732 relocs; we output them below as we discover that they are
4734 if (finfo
->info
->discard
== discard_all
)
4737 /* If this symbol is defined in a section which we are
4738 discarding, we don't need to keep it, but note that
4739 linker_mark is only reliable for sections that have contents.
4740 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4741 as well as linker_mark. */
4742 if (isym
->st_shndx
> 0
4743 && isym
->st_shndx
< SHN_LORESERVE
4745 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
4746 || (! finfo
->info
->relocateable
4747 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
4750 /* Get the name of the symbol. */
4751 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
4756 /* See if we are discarding symbols with this name. */
4757 if ((finfo
->info
->strip
== strip_some
4758 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
4760 || (finfo
->info
->discard
== discard_l
4761 && bfd_is_local_label_name (input_bfd
, name
)))
4764 /* If we get here, we are going to output this symbol. */
4768 /* Adjust the section index for the output file. */
4769 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
4770 isec
->output_section
);
4771 if (osym
.st_shndx
== (unsigned short) -1)
4774 *pindex
= output_bfd
->symcount
;
4776 /* ELF symbols in relocateable files are section relative, but
4777 in executable files they are virtual addresses. Note that
4778 this code assumes that all ELF sections have an associated
4779 BFD section with a reasonable value for output_offset; below
4780 we assume that they also have a reasonable value for
4781 output_section. Any special sections must be set up to meet
4782 these requirements. */
4783 osym
.st_value
+= isec
->output_offset
;
4784 if (! finfo
->info
->relocateable
)
4785 osym
.st_value
+= isec
->output_section
->vma
;
4787 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
4791 /* Relocate the contents of each section. */
4792 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4796 if (! o
->linker_mark
)
4798 /* This section was omitted from the link. */
4802 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4803 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
4806 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
4808 /* Section was created by elf_link_create_dynamic_sections
4813 /* Get the contents of the section. They have been cached by a
4814 relaxation routine. Note that o is a section in an input
4815 file, so the contents field will not have been set by any of
4816 the routines which work on output files. */
4817 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
4818 contents
= elf_section_data (o
)->this_hdr
.contents
;
4821 contents
= finfo
->contents
;
4822 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
4823 (file_ptr
) 0, o
->_raw_size
))
4827 if ((o
->flags
& SEC_RELOC
) != 0)
4829 Elf_Internal_Rela
*internal_relocs
;
4831 /* Get the swapped relocs. */
4832 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
4833 (input_bfd
, o
, finfo
->external_relocs
,
4834 finfo
->internal_relocs
, false));
4835 if (internal_relocs
== NULL
4836 && o
->reloc_count
> 0)
4839 /* Relocate the section by invoking a back end routine.
4841 The back end routine is responsible for adjusting the
4842 section contents as necessary, and (if using Rela relocs
4843 and generating a relocateable output file) adjusting the
4844 reloc addend as necessary.
4846 The back end routine does not have to worry about setting
4847 the reloc address or the reloc symbol index.
4849 The back end routine is given a pointer to the swapped in
4850 internal symbols, and can access the hash table entries
4851 for the external symbols via elf_sym_hashes (input_bfd).
4853 When generating relocateable output, the back end routine
4854 must handle STB_LOCAL/STT_SECTION symbols specially. The
4855 output symbol is going to be a section symbol
4856 corresponding to the output section, which will require
4857 the addend to be adjusted. */
4859 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
4860 input_bfd
, o
, contents
,
4862 finfo
->internal_syms
,
4866 if (finfo
->info
->relocateable
)
4868 Elf_Internal_Rela
*irela
;
4869 Elf_Internal_Rela
*irelaend
;
4870 struct elf_link_hash_entry
**rel_hash
;
4871 Elf_Internal_Shdr
*input_rel_hdr
;
4872 Elf_Internal_Shdr
*output_rel_hdr
;
4874 /* Adjust the reloc addresses and symbol indices. */
4876 irela
= internal_relocs
;
4877 irelaend
= irela
+ o
->reloc_count
;
4878 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
4879 + o
->output_section
->reloc_count
);
4880 for (; irela
< irelaend
; irela
++, rel_hash
++)
4882 unsigned long r_symndx
;
4883 Elf_Internal_Sym
*isym
;
4886 irela
->r_offset
+= o
->output_offset
;
4888 r_symndx
= ELF_R_SYM (irela
->r_info
);
4893 if (r_symndx
>= locsymcount
4894 || (elf_bad_symtab (input_bfd
)
4895 && finfo
->sections
[r_symndx
] == NULL
))
4897 struct elf_link_hash_entry
*rh
;
4900 /* This is a reloc against a global symbol. We
4901 have not yet output all the local symbols, so
4902 we do not know the symbol index of any global
4903 symbol. We set the rel_hash entry for this
4904 reloc to point to the global hash table entry
4905 for this symbol. The symbol index is then
4906 set at the end of elf_bfd_final_link. */
4907 indx
= r_symndx
- extsymoff
;
4908 rh
= elf_sym_hashes (input_bfd
)[indx
];
4909 while (rh
->root
.type
== bfd_link_hash_indirect
4910 || rh
->root
.type
== bfd_link_hash_warning
)
4911 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
4913 /* Setting the index to -2 tells
4914 elf_link_output_extsym that this symbol is
4916 BFD_ASSERT (rh
->indx
< 0);
4924 /* This is a reloc against a local symbol. */
4927 isym
= finfo
->internal_syms
+ r_symndx
;
4928 sec
= finfo
->sections
[r_symndx
];
4929 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
4931 /* I suppose the backend ought to fill in the
4932 section of any STT_SECTION symbol against a
4933 processor specific section. If we have
4934 discarded a section, the output_section will
4935 be the absolute section. */
4937 && (bfd_is_abs_section (sec
)
4938 || (sec
->output_section
!= NULL
4939 && bfd_is_abs_section (sec
->output_section
))))
4941 else if (sec
== NULL
|| sec
->owner
== NULL
)
4943 bfd_set_error (bfd_error_bad_value
);
4948 r_symndx
= sec
->output_section
->target_index
;
4949 BFD_ASSERT (r_symndx
!= 0);
4954 if (finfo
->indices
[r_symndx
] == -1)
4960 if (finfo
->info
->strip
== strip_all
)
4962 /* You can't do ld -r -s. */
4963 bfd_set_error (bfd_error_invalid_operation
);
4967 /* This symbol was skipped earlier, but
4968 since it is needed by a reloc, we
4969 must output it now. */
4970 link
= symtab_hdr
->sh_link
;
4971 name
= bfd_elf_string_from_elf_section (input_bfd
,
4977 osec
= sec
->output_section
;
4979 _bfd_elf_section_from_bfd_section (output_bfd
,
4981 if (isym
->st_shndx
== (unsigned short) -1)
4984 isym
->st_value
+= sec
->output_offset
;
4985 if (! finfo
->info
->relocateable
)
4986 isym
->st_value
+= osec
->vma
;
4988 finfo
->indices
[r_symndx
] = output_bfd
->symcount
;
4990 if (! elf_link_output_sym (finfo
, name
, isym
, sec
))
4994 r_symndx
= finfo
->indices
[r_symndx
];
4997 irela
->r_info
= ELF_R_INFO (r_symndx
,
4998 ELF_R_TYPE (irela
->r_info
));
5001 /* Swap out the relocs. */
5002 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
5003 output_rel_hdr
= &elf_section_data (o
->output_section
)->rel_hdr
;
5004 BFD_ASSERT (output_rel_hdr
->sh_entsize
5005 == input_rel_hdr
->sh_entsize
);
5006 irela
= internal_relocs
;
5007 irelaend
= irela
+ o
->reloc_count
;
5008 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
5010 Elf_External_Rel
*erel
;
5012 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
5013 + o
->output_section
->reloc_count
);
5014 for (; irela
< irelaend
; irela
++, erel
++)
5016 Elf_Internal_Rel irel
;
5018 irel
.r_offset
= irela
->r_offset
;
5019 irel
.r_info
= irela
->r_info
;
5020 BFD_ASSERT (irela
->r_addend
== 0);
5021 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
5026 Elf_External_Rela
*erela
;
5028 BFD_ASSERT (input_rel_hdr
->sh_entsize
5029 == sizeof (Elf_External_Rela
));
5030 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
5031 + o
->output_section
->reloc_count
);
5032 for (; irela
< irelaend
; irela
++, erela
++)
5033 elf_swap_reloca_out (output_bfd
, irela
, erela
);
5036 o
->output_section
->reloc_count
+= o
->reloc_count
;
5040 /* Write out the modified section contents. */
5041 if (elf_section_data (o
)->stab_info
== NULL
)
5043 if (! (o
->flags
& SEC_EXCLUDE
) &&
5044 ! bfd_set_section_contents (output_bfd
, o
->output_section
,
5045 contents
, o
->output_offset
,
5046 (o
->_cooked_size
!= 0
5053 if (! (_bfd_write_section_stabs
5054 (output_bfd
, &elf_hash_table (finfo
->info
)->stab_info
,
5055 o
, &elf_section_data (o
)->stab_info
, contents
)))
5063 /* Generate a reloc when linking an ELF file. This is a reloc
5064 requested by the linker, and does come from any input file. This
5065 is used to build constructor and destructor tables when linking
5069 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
5071 struct bfd_link_info
*info
;
5072 asection
*output_section
;
5073 struct bfd_link_order
*link_order
;
5075 reloc_howto_type
*howto
;
5079 struct elf_link_hash_entry
**rel_hash_ptr
;
5080 Elf_Internal_Shdr
*rel_hdr
;
5082 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5085 bfd_set_error (bfd_error_bad_value
);
5089 addend
= link_order
->u
.reloc
.p
->addend
;
5091 /* Figure out the symbol index. */
5092 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
5093 + output_section
->reloc_count
);
5094 if (link_order
->type
== bfd_section_reloc_link_order
)
5096 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
5097 BFD_ASSERT (indx
!= 0);
5098 *rel_hash_ptr
= NULL
;
5102 struct elf_link_hash_entry
*h
;
5104 /* Treat a reloc against a defined symbol as though it were
5105 actually against the section. */
5106 h
= ((struct elf_link_hash_entry
*)
5107 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
5108 link_order
->u
.reloc
.p
->u
.name
,
5109 false, false, true));
5111 && (h
->root
.type
== bfd_link_hash_defined
5112 || h
->root
.type
== bfd_link_hash_defweak
))
5116 section
= h
->root
.u
.def
.section
;
5117 indx
= section
->output_section
->target_index
;
5118 *rel_hash_ptr
= NULL
;
5119 /* It seems that we ought to add the symbol value to the
5120 addend here, but in practice it has already been added
5121 because it was passed to constructor_callback. */
5122 addend
+= section
->output_section
->vma
+ section
->output_offset
;
5126 /* Setting the index to -2 tells elf_link_output_extsym that
5127 this symbol is used by a reloc. */
5134 if (! ((*info
->callbacks
->unattached_reloc
)
5135 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
5136 (asection
*) NULL
, (bfd_vma
) 0)))
5142 /* If this is an inplace reloc, we must write the addend into the
5144 if (howto
->partial_inplace
&& addend
!= 0)
5147 bfd_reloc_status_type rstat
;
5151 size
= bfd_get_reloc_size (howto
);
5152 buf
= (bfd_byte
*) bfd_zmalloc (size
);
5153 if (buf
== (bfd_byte
*) NULL
)
5155 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5161 case bfd_reloc_outofrange
:
5163 case bfd_reloc_overflow
:
5164 if (! ((*info
->callbacks
->reloc_overflow
)
5166 (link_order
->type
== bfd_section_reloc_link_order
5167 ? bfd_section_name (output_bfd
,
5168 link_order
->u
.reloc
.p
->u
.section
)
5169 : link_order
->u
.reloc
.p
->u
.name
),
5170 howto
->name
, addend
, (bfd
*) NULL
, (asection
*) NULL
,
5178 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
5179 (file_ptr
) link_order
->offset
, size
);
5185 /* The address of a reloc is relative to the section in a
5186 relocateable file, and is a virtual address in an executable
5188 offset
= link_order
->offset
;
5189 if (! info
->relocateable
)
5190 offset
+= output_section
->vma
;
5192 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
5194 if (rel_hdr
->sh_type
== SHT_REL
)
5196 Elf_Internal_Rel irel
;
5197 Elf_External_Rel
*erel
;
5199 irel
.r_offset
= offset
;
5200 irel
.r_info
= ELF_R_INFO (indx
, howto
->type
);
5201 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
5202 + output_section
->reloc_count
);
5203 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
5207 Elf_Internal_Rela irela
;
5208 Elf_External_Rela
*erela
;
5210 irela
.r_offset
= offset
;
5211 irela
.r_info
= ELF_R_INFO (indx
, howto
->type
);
5212 irela
.r_addend
= addend
;
5213 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
5214 + output_section
->reloc_count
);
5215 elf_swap_reloca_out (output_bfd
, &irela
, erela
);
5218 ++output_section
->reloc_count
;
5224 /* Allocate a pointer to live in a linker created section. */
5227 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
5229 struct bfd_link_info
*info
;
5230 elf_linker_section_t
*lsect
;
5231 struct elf_link_hash_entry
*h
;
5232 const Elf_Internal_Rela
*rel
;
5234 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
5235 elf_linker_section_pointers_t
*linker_section_ptr
;
5236 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);;
5238 BFD_ASSERT (lsect
!= NULL
);
5240 /* Is this a global symbol? */
5243 /* Has this symbol already been allocated, if so, our work is done */
5244 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
5249 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
5250 /* Make sure this symbol is output as a dynamic symbol. */
5251 if (h
->dynindx
== -1)
5253 if (! elf_link_record_dynamic_symbol (info
, h
))
5257 if (lsect
->rel_section
)
5258 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
5261 else /* Allocation of a pointer to a local symbol */
5263 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
5265 /* Allocate a table to hold the local symbols if first time */
5268 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
5269 register unsigned int i
;
5271 ptr
= (elf_linker_section_pointers_t
**)
5272 bfd_alloc (abfd
, num_symbols
* sizeof (elf_linker_section_pointers_t
*));
5277 elf_local_ptr_offsets (abfd
) = ptr
;
5278 for (i
= 0; i
< num_symbols
; i
++)
5279 ptr
[i
] = (elf_linker_section_pointers_t
*)0;
5282 /* Has this symbol already been allocated, if so, our work is done */
5283 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
5288 ptr_linker_section_ptr
= &ptr
[r_symndx
];
5292 /* If we are generating a shared object, we need to
5293 output a R_<xxx>_RELATIVE reloc so that the
5294 dynamic linker can adjust this GOT entry. */
5295 BFD_ASSERT (lsect
->rel_section
!= NULL
);
5296 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
5300 /* Allocate space for a pointer in the linker section, and allocate a new pointer record
5301 from internal memory. */
5302 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
5303 linker_section_ptr
= (elf_linker_section_pointers_t
*)
5304 bfd_alloc (abfd
, sizeof (elf_linker_section_pointers_t
));
5306 if (!linker_section_ptr
)
5309 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
5310 linker_section_ptr
->addend
= rel
->r_addend
;
5311 linker_section_ptr
->which
= lsect
->which
;
5312 linker_section_ptr
->written_address_p
= false;
5313 *ptr_linker_section_ptr
= linker_section_ptr
;
5316 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
5318 linker_section_ptr
->offset
= lsect
->section
->_raw_size
- lsect
->hole_size
+ (ARCH_SIZE
/ 8);
5319 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
5320 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
5321 if (lsect
->sym_hash
) /* Bump up symbol value if needed */
5323 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
5325 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
5326 lsect
->sym_hash
->root
.root
.string
,
5327 (long)ARCH_SIZE
/ 8,
5328 (long)lsect
->sym_hash
->root
.u
.def
.value
);
5334 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
5336 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
5339 fprintf (stderr
, "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
5340 lsect
->name
, (long)linker_section_ptr
->offset
, (long)lsect
->section
->_raw_size
);
5348 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
5351 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
5354 /* Fill in the address for a pointer generated in alinker section. */
5357 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
, relocation
, rel
, relative_reloc
)
5360 struct bfd_link_info
*info
;
5361 elf_linker_section_t
*lsect
;
5362 struct elf_link_hash_entry
*h
;
5364 const Elf_Internal_Rela
*rel
;
5367 elf_linker_section_pointers_t
*linker_section_ptr
;
5369 BFD_ASSERT (lsect
!= NULL
);
5371 if (h
!= NULL
) /* global symbol */
5373 linker_section_ptr
= _bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
5377 BFD_ASSERT (linker_section_ptr
!= NULL
);
5379 if (! elf_hash_table (info
)->dynamic_sections_created
5382 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
5384 /* This is actually a static link, or it is a
5385 -Bsymbolic link and the symbol is defined
5386 locally. We must initialize this entry in the
5389 When doing a dynamic link, we create a .rela.<xxx>
5390 relocation entry to initialize the value. This
5391 is done in the finish_dynamic_symbol routine. */
5392 if (!linker_section_ptr
->written_address_p
)
5394 linker_section_ptr
->written_address_p
= true;
5395 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
5396 lsect
->section
->contents
+ linker_section_ptr
->offset
);
5400 else /* local symbol */
5402 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
5403 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
5404 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
5405 linker_section_ptr
= _bfd_elf_find_pointer_linker_section (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
5409 BFD_ASSERT (linker_section_ptr
!= NULL
);
5411 /* Write out pointer if it hasn't been rewritten out before */
5412 if (!linker_section_ptr
->written_address_p
)
5414 linker_section_ptr
->written_address_p
= true;
5415 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
5416 lsect
->section
->contents
+ linker_section_ptr
->offset
);
5420 asection
*srel
= lsect
->rel_section
;
5421 Elf_Internal_Rela outrel
;
5423 /* We need to generate a relative reloc for the dynamic linker. */
5425 lsect
->rel_section
= srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
5428 BFD_ASSERT (srel
!= NULL
);
5430 outrel
.r_offset
= (lsect
->section
->output_section
->vma
5431 + lsect
->section
->output_offset
5432 + linker_section_ptr
->offset
);
5433 outrel
.r_info
= ELF_R_INFO (0, relative_reloc
);
5434 outrel
.r_addend
= 0;
5435 elf_swap_reloca_out (output_bfd
, &outrel
,
5436 (((Elf_External_Rela
*)
5437 lsect
->section
->contents
)
5438 + lsect
->section
->reloc_count
));
5439 ++lsect
->section
->reloc_count
;
5444 relocation
= (lsect
->section
->output_offset
5445 + linker_section_ptr
->offset
5446 - lsect
->hole_offset
5447 - lsect
->sym_offset
);
5450 fprintf (stderr
, "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
5451 lsect
->name
, (long)relocation
, (long)relocation
);
5454 /* Subtract out the addend, because it will get added back in by the normal
5456 return relocation
- linker_section_ptr
->addend
;
5459 /* Garbage collect unused sections. */
5461 static boolean elf_gc_mark
5462 PARAMS ((struct bfd_link_info
*info
, asection
*sec
,
5463 asection
* (*gc_mark_hook
)
5464 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
5465 struct elf_link_hash_entry
*, Elf_Internal_Sym
*))));
5467 static boolean elf_gc_sweep
5468 PARAMS ((struct bfd_link_info
*info
,
5469 boolean (*gc_sweep_hook
)
5470 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
5471 const Elf_Internal_Rela
*relocs
))));
5473 static boolean elf_gc_sweep_symbol
5474 PARAMS ((struct elf_link_hash_entry
*h
, PTR idxptr
));
5476 static boolean elf_gc_allocate_got_offsets
5477 PARAMS ((struct elf_link_hash_entry
*h
, PTR offarg
));
5479 static boolean elf_gc_propagate_vtable_entries_used
5480 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
5482 static boolean elf_gc_smash_unused_vtentry_relocs
5483 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
5485 /* The mark phase of garbage collection. For a given section, mark
5486 it, and all the sections which define symbols to which it refers. */
5489 elf_gc_mark (info
, sec
, gc_mark_hook
)
5490 struct bfd_link_info
*info
;
5492 asection
* (*gc_mark_hook
)
5493 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
5494 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
5500 /* Look through the section relocs. */
5502 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
5504 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
5505 Elf_Internal_Shdr
*symtab_hdr
;
5506 struct elf_link_hash_entry
**sym_hashes
;
5509 Elf_External_Sym
*locsyms
, *freesyms
= NULL
;
5510 bfd
*input_bfd
= sec
->owner
;
5512 /* GCFIXME: how to arrange so that relocs and symbols are not
5513 reread continually? */
5515 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
5516 sym_hashes
= elf_sym_hashes (input_bfd
);
5518 /* Read the local symbols. */
5519 if (elf_bad_symtab (input_bfd
))
5521 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
5525 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
5526 if (symtab_hdr
->contents
)
5527 locsyms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
5528 else if (nlocsyms
== 0)
5532 locsyms
= freesyms
=
5533 bfd_malloc (nlocsyms
* sizeof (Elf_External_Sym
));
5534 if (freesyms
== NULL
5535 || bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
5536 || (bfd_read (locsyms
, sizeof (Elf_External_Sym
),
5537 nlocsyms
, input_bfd
)
5538 != nlocsyms
* sizeof (Elf_External_Sym
)))
5545 /* Read the relocations. */
5546 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
5547 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
5548 info
->keep_memory
));
5549 if (relstart
== NULL
)
5554 relend
= relstart
+ sec
->reloc_count
;
5556 for (rel
= relstart
; rel
< relend
; rel
++)
5558 unsigned long r_symndx
;
5560 struct elf_link_hash_entry
*h
;
5563 r_symndx
= ELF_R_SYM (rel
->r_info
);
5567 if (elf_bad_symtab (sec
->owner
))
5569 elf_swap_symbol_in (input_bfd
, &locsyms
[r_symndx
], &s
);
5570 if (ELF_ST_BIND (s
.st_info
) == STB_LOCAL
)
5571 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, NULL
, &s
);
5574 h
= sym_hashes
[r_symndx
- extsymoff
];
5575 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, h
, NULL
);
5578 else if (r_symndx
>= nlocsyms
)
5580 h
= sym_hashes
[r_symndx
- extsymoff
];
5581 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, h
, NULL
);
5585 elf_swap_symbol_in (input_bfd
, &locsyms
[r_symndx
], &s
);
5586 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, NULL
, &s
);
5589 if (rsec
&& !rsec
->gc_mark
)
5590 if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
5598 if (!info
->keep_memory
)
5608 /* The sweep phase of garbage collection. Remove all garbage sections. */
5611 elf_gc_sweep (info
, gc_sweep_hook
)
5612 struct bfd_link_info
*info
;
5613 boolean (*gc_sweep_hook
)
5614 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
5615 const Elf_Internal_Rela
*relocs
));
5619 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5623 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5625 /* Keep special sections. Keep .debug sections. */
5626 if ((o
->flags
& SEC_LINKER_CREATED
)
5627 || (o
->flags
& SEC_DEBUGGING
))
5633 /* Skip sweeping sections already excluded. */
5634 if (o
->flags
& SEC_EXCLUDE
)
5637 /* Since this is early in the link process, it is simple
5638 to remove a section from the output. */
5639 o
->flags
|= SEC_EXCLUDE
;
5641 /* But we also have to update some of the relocation
5642 info we collected before. */
5644 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
5646 Elf_Internal_Rela
*internal_relocs
;
5649 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
5650 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
5651 if (internal_relocs
== NULL
)
5654 r
= (*gc_sweep_hook
)(o
->owner
, info
, o
, internal_relocs
);
5656 if (!info
->keep_memory
)
5657 free (internal_relocs
);
5665 /* Remove the symbols that were in the swept sections from the dynamic
5666 symbol table. GCFIXME: Anyone know how to get them out of the
5667 static symbol table as well? */
5671 elf_link_hash_traverse (elf_hash_table (info
),
5672 elf_gc_sweep_symbol
,
5675 elf_hash_table (info
)->dynsymcount
= i
;
5681 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5684 elf_gc_sweep_symbol (h
, idxptr
)
5685 struct elf_link_hash_entry
*h
;
5688 int *idx
= (int *) idxptr
;
5690 if (h
->dynindx
!= -1
5691 && ((h
->root
.type
!= bfd_link_hash_defined
5692 && h
->root
.type
!= bfd_link_hash_defweak
)
5693 || h
->root
.u
.def
.section
->gc_mark
))
5694 h
->dynindx
= (*idx
)++;
5699 /* Propogate collected vtable information. This is called through
5700 elf_link_hash_traverse. */
5703 elf_gc_propagate_vtable_entries_used (h
, okp
)
5704 struct elf_link_hash_entry
*h
;
5707 /* Those that are not vtables. */
5708 if (h
->vtable_parent
== NULL
)
5711 /* Those vtables that do not have parents, we cannot merge. */
5712 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
5715 /* If we've already been done, exit. */
5716 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
5719 /* Make sure the parent's table is up to date. */
5720 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
5722 if (h
->vtable_entries_used
== NULL
)
5724 /* None of this table's entries were referenced. Re-use the
5726 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
5733 /* Or the parent's entries into ours. */
5734 cu
= h
->vtable_entries_used
;
5736 pu
= h
->vtable_parent
->vtable_entries_used
;
5739 n
= h
->vtable_parent
->size
/ FILE_ALIGN
;
5742 if (*pu
) *cu
= true;
5752 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
5753 struct elf_link_hash_entry
*h
;
5757 bfd_vma hstart
, hend
;
5758 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
5760 /* Take care of both those symbols that do not describe vtables as
5761 well as those that are not loaded. */
5762 if (h
->vtable_parent
== NULL
)
5765 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5766 || h
->root
.type
== bfd_link_hash_defweak
);
5768 sec
= h
->root
.u
.def
.section
;
5769 hstart
= h
->root
.u
.def
.value
;
5770 hend
= hstart
+ h
->size
;
5772 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
5773 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, true));
5775 return *(boolean
*)okp
= false;
5776 relend
= relstart
+ sec
->reloc_count
;
5778 for (rel
= relstart
; rel
< relend
; ++rel
)
5779 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
5781 /* If the entry is in use, do nothing. */
5782 if (h
->vtable_entries_used
)
5784 bfd_vma entry
= (rel
->r_offset
- hstart
) / FILE_ALIGN
;
5785 if (h
->vtable_entries_used
[entry
])
5788 /* Otherwise, kill it. */
5789 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
5795 /* Do mark and sweep of unused sections. */
5798 elf_gc_sections (abfd
, info
)
5800 struct bfd_link_info
*info
;
5804 asection
* (*gc_mark_hook
)
5805 PARAMS ((bfd
*abfd
, struct bfd_link_info
*, Elf_Internal_Rela
*,
5806 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
5808 if (!get_elf_backend_data (abfd
)->can_gc_sections
5809 || info
->relocateable
5810 || elf_hash_table (info
)->dynamic_sections_created
)
5813 /* Apply transitive closure to the vtable entry usage info. */
5814 elf_link_hash_traverse (elf_hash_table (info
),
5815 elf_gc_propagate_vtable_entries_used
,
5820 /* Kill the vtable relocations that were not used. */
5821 elf_link_hash_traverse (elf_hash_table (info
),
5822 elf_gc_smash_unused_vtentry_relocs
,
5827 /* Grovel through relocs to find out who stays ... */
5829 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
5830 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5833 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5835 if (o
->flags
& SEC_KEEP
)
5836 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
5841 /* ... and mark SEC_EXCLUDE for those that go. */
5842 if (!elf_gc_sweep(info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
5848 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
5851 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
5854 struct elf_link_hash_entry
*h
;
5857 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
5858 struct elf_link_hash_entry
**search
, *child
;
5859 bfd_size_type extsymcount
;
5861 /* The sh_info field of the symtab header tells us where the
5862 external symbols start. We don't care about the local symbols at
5864 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
5865 if (!elf_bad_symtab (abfd
))
5866 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
5868 sym_hashes
= elf_sym_hashes (abfd
);
5869 sym_hashes_end
= sym_hashes
+ extsymcount
;
5871 /* Hunt down the child symbol, which is in this section at the same
5872 offset as the relocation. */
5873 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
5875 if ((child
= *search
) != NULL
5876 && (child
->root
.type
== bfd_link_hash_defined
5877 || child
->root
.type
== bfd_link_hash_defweak
)
5878 && child
->root
.u
.def
.section
== sec
5879 && child
->root
.u
.def
.value
== offset
)
5883 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
5884 bfd_get_filename (abfd
), sec
->name
,
5885 (unsigned long)offset
);
5886 bfd_set_error (bfd_error_invalid_operation
);
5892 /* This *should* only be the absolute section. It could potentially
5893 be that someone has defined a non-global vtable though, which
5894 would be bad. It isn't worth paging in the local symbols to be
5895 sure though; that case should simply be handled by the assembler. */
5897 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
5900 child
->vtable_parent
= h
;
5905 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
5908 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
5911 struct elf_link_hash_entry
*h
;
5914 if (h
->vtable_entries_used
== NULL
)
5916 /* Allocate one extra entry for use as a "done" flag for the
5917 consolidation pass. */
5918 size_t size
= (h
->size
/ FILE_ALIGN
+ 1) * sizeof(boolean
);
5919 h
->vtable_entries_used
= (boolean
*) bfd_alloc (abfd
, size
);
5920 if (h
->vtable_entries_used
== NULL
)
5923 /* And arrange for that done flag to be at index -1. */
5924 memset (h
->vtable_entries_used
++, 0, size
);
5926 h
->vtable_entries_used
[addend
/ FILE_ALIGN
] = true;
5931 /* And an accompanying bit to work out final got entry offsets once
5932 we're done. Should be called from final_link. */
5935 elf_gc_common_finalize_got_offsets (abfd
, info
)
5937 struct bfd_link_info
*info
;
5940 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5943 /* The GOT offset is relative to the .got section, but the GOT header is
5944 put into the .got.plt section, if the backend uses it. */
5945 if (bed
->want_got_plt
)
5948 gotoff
= bed
->got_header_size
;
5950 /* Do the local .got entries first. */
5951 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
5953 bfd_signed_vma
*local_got
= elf_local_got_refcounts (i
);
5954 bfd_size_type j
, locsymcount
;
5955 Elf_Internal_Shdr
*symtab_hdr
;
5960 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
5961 if (elf_bad_symtab (i
))
5962 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
5964 locsymcount
= symtab_hdr
->sh_info
;
5966 for (j
= 0; j
< locsymcount
; ++j
)
5968 if (local_got
[j
] > 0)
5970 local_got
[j
] = gotoff
;
5971 gotoff
+= ARCH_SIZE
/ 8;
5974 local_got
[j
] = (bfd_vma
) -1;
5978 /* Then the global .got and .plt entries. */
5979 elf_link_hash_traverse (elf_hash_table (info
),
5980 elf_gc_allocate_got_offsets
,
5985 /* We need a special top-level link routine to convert got reference counts
5986 to real got offsets. */
5989 elf_gc_allocate_got_offsets (h
, offarg
)
5990 struct elf_link_hash_entry
*h
;
5993 bfd_vma
*off
= (bfd_vma
*) offarg
;
5995 if (h
->got
.refcount
> 0)
5997 h
->got
.offset
= off
[0];
5998 off
[0] += ARCH_SIZE
/ 8;
6001 h
->got
.offset
= (bfd_vma
) -1;
6006 /* Many folk need no more in the way of final link than this, once
6007 got entry reference counting is enabled. */
6010 elf_gc_common_final_link (abfd
, info
)
6012 struct bfd_link_info
*info
;
6014 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
6017 /* Invoke the regular ELF backend linker to do all the work. */
6018 return elf_bfd_final_link (abfd
, info
);
6021 /* This function will be called though elf_link_hash_traverse to store
6022 all hash value of the exported symbols in an array. */
6025 elf_collect_hash_codes (h
, data
)
6026 struct elf_link_hash_entry
*h
;
6029 unsigned long **valuep
= (unsigned long **) data
;
6035 /* Ignore indirect symbols. These are added by the versioning code. */
6036 if (h
->dynindx
== -1)
6039 name
= h
->root
.root
.string
;
6040 p
= strchr (name
, ELF_VER_CHR
);
6043 alc
= bfd_malloc (p
- name
+ 1);
6044 memcpy (alc
, name
, p
- name
);
6045 alc
[p
- name
] = '\0';
6049 /* Compute the hash value. */
6050 ha
= bfd_elf_hash (name
);
6052 /* Store the found hash value in the array given as the argument. */
6055 /* And store it in the struct so that we can put it in the hash table
6057 h
->elf_hash_value
= ha
;