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
));
54 /* Given an ELF BFD, add symbols to the global hash table as
58 elf_bfd_link_add_symbols (abfd
, info
)
60 struct bfd_link_info
*info
;
62 switch (bfd_get_format (abfd
))
65 return elf_link_add_object_symbols (abfd
, info
);
67 return elf_link_add_archive_symbols (abfd
, info
);
69 bfd_set_error (bfd_error_wrong_format
);
75 /* Add symbols from an ELF archive file to the linker hash table. We
76 don't use _bfd_generic_link_add_archive_symbols because of a
77 problem which arises on UnixWare. The UnixWare libc.so is an
78 archive which includes an entry libc.so.1 which defines a bunch of
79 symbols. The libc.so archive also includes a number of other
80 object files, which also define symbols, some of which are the same
81 as those defined in libc.so.1. Correct linking requires that we
82 consider each object file in turn, and include it if it defines any
83 symbols we need. _bfd_generic_link_add_archive_symbols does not do
84 this; it looks through the list of undefined symbols, and includes
85 any object file which defines them. When this algorithm is used on
86 UnixWare, it winds up pulling in libc.so.1 early and defining a
87 bunch of symbols. This means that some of the other objects in the
88 archive are not included in the link, which is incorrect since they
89 precede libc.so.1 in the archive.
91 Fortunately, ELF archive handling is simpler than that done by
92 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
93 oddities. In ELF, if we find a symbol in the archive map, and the
94 symbol is currently undefined, we know that we must pull in that
97 Unfortunately, we do have to make multiple passes over the symbol
98 table until nothing further is resolved. */
101 elf_link_add_archive_symbols (abfd
, info
)
103 struct bfd_link_info
*info
;
106 boolean
*defined
= NULL
;
107 boolean
*included
= NULL
;
111 if (! bfd_has_map (abfd
))
113 /* An empty archive is a special case. */
114 if (bfd_openr_next_archived_file (abfd
, (bfd
*) NULL
) == NULL
)
116 bfd_set_error (bfd_error_no_armap
);
120 /* Keep track of all symbols we know to be already defined, and all
121 files we know to be already included. This is to speed up the
122 second and subsequent passes. */
123 c
= bfd_ardata (abfd
)->symdef_count
;
126 defined
= (boolean
*) bfd_malloc (c
* sizeof (boolean
));
127 included
= (boolean
*) bfd_malloc (c
* sizeof (boolean
));
128 if (defined
== (boolean
*) NULL
|| included
== (boolean
*) NULL
)
130 memset (defined
, 0, c
* sizeof (boolean
));
131 memset (included
, 0, c
* sizeof (boolean
));
133 symdefs
= bfd_ardata (abfd
)->symdefs
;
146 symdefend
= symdef
+ c
;
147 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
149 struct elf_link_hash_entry
*h
;
151 struct bfd_link_hash_entry
*undefs_tail
;
154 if (defined
[i
] || included
[i
])
156 if (symdef
->file_offset
== last
)
162 h
= elf_link_hash_lookup (elf_hash_table (info
), symdef
->name
,
163 false, false, false);
169 /* If this is a default version (the name contains @@),
170 look up the symbol again without the version. The
171 effect is that references to the symbol without the
172 version will be matched by the default symbol in the
175 p
= strchr (symdef
->name
, ELF_VER_CHR
);
176 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
179 copy
= bfd_alloc (abfd
, p
- symdef
->name
+ 1);
182 memcpy (copy
, symdef
->name
, p
- symdef
->name
);
183 copy
[p
- symdef
->name
] = '\0';
185 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
186 false, false, false);
188 bfd_release (abfd
, copy
);
194 if (h
->root
.type
!= bfd_link_hash_undefined
)
196 if (h
->root
.type
!= bfd_link_hash_undefweak
)
201 /* We need to include this archive member. */
203 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
204 if (element
== (bfd
*) NULL
)
207 if (! bfd_check_format (element
, bfd_object
))
210 /* Doublecheck that we have not included this object
211 already--it should be impossible, but there may be
212 something wrong with the archive. */
213 if (element
->archive_pass
!= 0)
215 bfd_set_error (bfd_error_bad_value
);
218 element
->archive_pass
= 1;
220 undefs_tail
= info
->hash
->undefs_tail
;
222 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
225 if (! elf_link_add_object_symbols (element
, info
))
228 /* If there are any new undefined symbols, we need to make
229 another pass through the archive in order to see whether
230 they can be defined. FIXME: This isn't perfect, because
231 common symbols wind up on undefs_tail and because an
232 undefined symbol which is defined later on in this pass
233 does not require another pass. This isn't a bug, but it
234 does make the code less efficient than it could be. */
235 if (undefs_tail
!= info
->hash
->undefs_tail
)
238 /* Look backward to mark all symbols from this object file
239 which we have already seen in this pass. */
243 included
[mark
] = true;
248 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
250 /* We mark subsequent symbols from this object file as we go
251 on through the loop. */
252 last
= symdef
->file_offset
;
263 if (defined
!= (boolean
*) NULL
)
265 if (included
!= (boolean
*) NULL
)
270 /* This function is called when we want to define a new symbol. It
271 handles the various cases which arise when we find a definition in
272 a dynamic object, or when there is already a definition in a
273 dynamic object. The new symbol is described by NAME, SYM, PSEC,
274 and PVALUE. We set SYM_HASH to the hash table entry. We set
275 OVERRIDE if the old symbol is overriding a new definition. We set
276 TYPE_CHANGE_OK if it is OK for the type to change. We set
277 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
278 change, we mean that we shouldn't warn if the type or size does
282 elf_merge_symbol (abfd
, info
, name
, sym
, psec
, pvalue
, sym_hash
,
283 override
, type_change_ok
, size_change_ok
)
285 struct bfd_link_info
*info
;
287 Elf_Internal_Sym
*sym
;
290 struct elf_link_hash_entry
**sym_hash
;
292 boolean
*type_change_ok
;
293 boolean
*size_change_ok
;
296 struct elf_link_hash_entry
*h
;
299 boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
304 bind
= ELF_ST_BIND (sym
->st_info
);
306 if (! bfd_is_und_section (sec
))
307 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, false, false);
309 h
= ((struct elf_link_hash_entry
*)
310 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true, false, false));
315 /* This code is for coping with dynamic objects, and is only useful
316 if we are doing an ELF link. */
317 if (info
->hash
->creator
!= abfd
->xvec
)
320 /* For merging, we only care about real symbols. */
322 while (h
->root
.type
== bfd_link_hash_indirect
323 || h
->root
.type
== bfd_link_hash_warning
)
324 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
326 /* If we just created the symbol, mark it as being an ELF symbol.
327 Other than that, there is nothing to do--there is no merge issue
328 with a newly defined symbol--so we just return. */
330 if (h
->root
.type
== bfd_link_hash_new
)
332 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
336 /* OLDBFD is a BFD associated with the existing symbol. */
338 switch (h
->root
.type
)
344 case bfd_link_hash_undefined
:
345 case bfd_link_hash_undefweak
:
346 oldbfd
= h
->root
.u
.undef
.abfd
;
349 case bfd_link_hash_defined
:
350 case bfd_link_hash_defweak
:
351 oldbfd
= h
->root
.u
.def
.section
->owner
;
354 case bfd_link_hash_common
:
355 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
359 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
360 respectively, is from a dynamic object. */
362 if ((abfd
->flags
& DYNAMIC
) != 0)
367 if (oldbfd
== NULL
|| (oldbfd
->flags
& DYNAMIC
) == 0)
372 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
373 respectively, appear to be a definition rather than reference. */
375 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
380 if (h
->root
.type
== bfd_link_hash_undefined
381 || h
->root
.type
== bfd_link_hash_undefweak
382 || h
->root
.type
== bfd_link_hash_common
)
387 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
388 symbol, respectively, appears to be a common symbol in a dynamic
389 object. If a symbol appears in an uninitialized section, and is
390 not weak, and is not a function, then it may be a common symbol
391 which was resolved when the dynamic object was created. We want
392 to treat such symbols specially, because they raise special
393 considerations when setting the symbol size: if the symbol
394 appears as a common symbol in a regular object, and the size in
395 the regular object is larger, we must make sure that we use the
396 larger size. This problematic case can always be avoided in C,
397 but it must be handled correctly when using Fortran shared
400 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
401 likewise for OLDDYNCOMMON and OLDDEF.
403 Note that this test is just a heuristic, and that it is quite
404 possible to have an uninitialized symbol in a shared object which
405 is really a definition, rather than a common symbol. This could
406 lead to some minor confusion when the symbol really is a common
407 symbol in some regular object. However, I think it will be
412 && (sec
->flags
& SEC_ALLOC
) != 0
413 && (sec
->flags
& SEC_LOAD
) == 0
416 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
419 newdyncommon
= false;
423 && h
->root
.type
== bfd_link_hash_defined
424 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
425 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
426 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
428 && h
->type
!= STT_FUNC
)
431 olddyncommon
= false;
433 /* It's OK to change the type if either the existing symbol or the
434 new symbol is weak. */
436 if (h
->root
.type
== bfd_link_hash_defweak
437 || h
->root
.type
== bfd_link_hash_undefweak
439 *type_change_ok
= true;
441 /* It's OK to change the size if either the existing symbol or the
442 new symbol is weak, or if the old symbol is undefined. */
445 || h
->root
.type
== bfd_link_hash_undefined
)
446 *size_change_ok
= true;
448 /* If both the old and the new symbols look like common symbols in a
449 dynamic object, set the size of the symbol to the larger of the
454 && sym
->st_size
!= h
->size
)
456 /* Since we think we have two common symbols, issue a multiple
457 common warning if desired. Note that we only warn if the
458 size is different. If the size is the same, we simply let
459 the old symbol override the new one as normally happens with
460 symbols defined in dynamic objects. */
462 if (! ((*info
->callbacks
->multiple_common
)
463 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
464 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
467 if (sym
->st_size
> h
->size
)
468 h
->size
= sym
->st_size
;
470 *size_change_ok
= true;
473 /* If we are looking at a dynamic object, and we have found a
474 definition, we need to see if the symbol was already defined by
475 some other object. If so, we want to use the existing
476 definition, and we do not want to report a multiple symbol
477 definition error; we do this by clobbering *PSEC to be
480 We treat a common symbol as a definition if the symbol in the
481 shared library is a function, since common symbols always
482 represent variables; this can cause confusion in principle, but
483 any such confusion would seem to indicate an erroneous program or
484 shared library. We also permit a common symbol in a regular
485 object to override a weak symbol in a shared object. */
490 || (h
->root
.type
== bfd_link_hash_common
492 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
496 newdyncommon
= false;
498 *psec
= sec
= bfd_und_section_ptr
;
499 *size_change_ok
= true;
501 /* If we get here when the old symbol is a common symbol, then
502 we are explicitly letting it override a weak symbol or
503 function in a dynamic object, and we don't want to warn about
504 a type change. If the old symbol is a defined symbol, a type
505 change warning may still be appropriate. */
507 if (h
->root
.type
== bfd_link_hash_common
)
508 *type_change_ok
= true;
511 /* Handle the special case of an old common symbol merging with a
512 new symbol which looks like a common symbol in a shared object.
513 We change *PSEC and *PVALUE to make the new symbol look like a
514 common symbol, and let _bfd_generic_link_add_one_symbol will do
518 && h
->root
.type
== bfd_link_hash_common
)
522 newdyncommon
= false;
523 *pvalue
= sym
->st_size
;
524 *psec
= sec
= bfd_com_section_ptr
;
525 *size_change_ok
= true;
528 /* If the old symbol is from a dynamic object, and the new symbol is
529 a definition which is not from a dynamic object, then the new
530 symbol overrides the old symbol. Symbols from regular files
531 always take precedence over symbols from dynamic objects, even if
532 they are defined after the dynamic object in the link.
534 As above, we again permit a common symbol in a regular object to
535 override a definition in a shared object if the shared object
536 symbol is a function or is weak. */
540 || (bfd_is_com_section (sec
)
541 && (h
->root
.type
== bfd_link_hash_defweak
542 || h
->type
== STT_FUNC
)))
545 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0)
547 /* Change the hash table entry to undefined, and let
548 _bfd_generic_link_add_one_symbol do the right thing with the
551 h
->root
.type
= bfd_link_hash_undefined
;
552 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
553 *size_change_ok
= true;
556 olddyncommon
= false;
558 /* We again permit a type change when a common symbol may be
559 overriding a function. */
561 if (bfd_is_com_section (sec
))
562 *type_change_ok
= true;
564 /* This union may have been set to be non-NULL when this symbol
565 was seen in a dynamic object. We must force the union to be
566 NULL, so that it is correct for a regular symbol. */
568 h
->verinfo
.vertree
= NULL
;
570 /* In this special case, if H is the target of an indirection,
571 we want the caller to frob with H rather than with the
572 indirect symbol. That will permit the caller to redefine the
573 target of the indirection, rather than the indirect symbol
574 itself. FIXME: This will break the -y option if we store a
575 symbol with a different name. */
579 /* Handle the special case of a new common symbol merging with an
580 old symbol that looks like it might be a common symbol defined in
581 a shared object. Note that we have already handled the case in
582 which a new common symbol should simply override the definition
583 in the shared library. */
586 && bfd_is_com_section (sec
)
589 /* It would be best if we could set the hash table entry to a
590 common symbol, but we don't know what to use for the section
592 if (! ((*info
->callbacks
->multiple_common
)
593 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
594 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
597 /* If the predumed common symbol in the dynamic object is
598 larger, pretend that the new symbol has its size. */
600 if (h
->size
> *pvalue
)
603 /* FIXME: We no longer know the alignment required by the symbol
604 in the dynamic object, so we just wind up using the one from
605 the regular object. */
608 olddyncommon
= false;
610 h
->root
.type
= bfd_link_hash_undefined
;
611 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
613 *size_change_ok
= true;
614 *type_change_ok
= true;
616 h
->verinfo
.vertree
= NULL
;
622 /* Add symbols from an ELF object file to the linker hash table. */
625 elf_link_add_object_symbols (abfd
, info
)
627 struct bfd_link_info
*info
;
629 boolean (*add_symbol_hook
) PARAMS ((bfd
*, struct bfd_link_info
*,
630 const Elf_Internal_Sym
*,
631 const char **, flagword
*,
632 asection
**, bfd_vma
*));
633 boolean (*check_relocs
) PARAMS ((bfd
*, struct bfd_link_info
*,
634 asection
*, const Elf_Internal_Rela
*));
636 Elf_Internal_Shdr
*hdr
;
640 Elf_External_Sym
*buf
= NULL
;
641 struct elf_link_hash_entry
**sym_hash
;
643 bfd_byte
*dynver
= NULL
;
644 Elf_External_Versym
*extversym
= NULL
;
645 Elf_External_Versym
*ever
;
646 Elf_External_Dyn
*dynbuf
= NULL
;
647 struct elf_link_hash_entry
*weaks
;
648 Elf_External_Sym
*esym
;
649 Elf_External_Sym
*esymend
;
651 add_symbol_hook
= get_elf_backend_data (abfd
)->elf_add_symbol_hook
;
652 collect
= get_elf_backend_data (abfd
)->collect
;
654 if ((abfd
->flags
& DYNAMIC
) == 0)
660 /* You can't use -r against a dynamic object. Also, there's no
661 hope of using a dynamic object which does not exactly match
662 the format of the output file. */
663 if (info
->relocateable
|| info
->hash
->creator
!= abfd
->xvec
)
665 bfd_set_error (bfd_error_invalid_operation
);
670 /* As a GNU extension, any input sections which are named
671 .gnu.warning.SYMBOL are treated as warning symbols for the given
672 symbol. This differs from .gnu.warning sections, which generate
673 warnings when they are included in an output file. */
678 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
682 name
= bfd_get_section_name (abfd
, s
);
683 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
688 name
+= sizeof ".gnu.warning." - 1;
690 /* If this is a shared object, then look up the symbol
691 in the hash table. If it is there, and it is already
692 been defined, then we will not be using the entry
693 from this shared object, so we don't need to warn.
694 FIXME: If we see the definition in a regular object
695 later on, we will warn, but we shouldn't. The only
696 fix is to keep track of what warnings we are supposed
697 to emit, and then handle them all at the end of the
699 if (dynamic
&& abfd
->xvec
== info
->hash
->creator
)
701 struct elf_link_hash_entry
*h
;
703 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
706 /* FIXME: What about bfd_link_hash_common? */
708 && (h
->root
.type
== bfd_link_hash_defined
709 || h
->root
.type
== bfd_link_hash_defweak
))
711 /* We don't want to issue this warning. Clobber
712 the section size so that the warning does not
713 get copied into the output file. */
719 sz
= bfd_section_size (abfd
, s
);
720 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
724 if (! bfd_get_section_contents (abfd
, s
, msg
, (file_ptr
) 0, sz
))
729 if (! (_bfd_generic_link_add_one_symbol
730 (info
, abfd
, name
, BSF_WARNING
, s
, (bfd_vma
) 0, msg
,
731 false, collect
, (struct bfd_link_hash_entry
**) NULL
)))
734 if (! info
->relocateable
)
736 /* Clobber the section size so that the warning does
737 not get copied into the output file. */
744 /* If this is a dynamic object, we always link against the .dynsym
745 symbol table, not the .symtab symbol table. The dynamic linker
746 will only see the .dynsym symbol table, so there is no reason to
747 look at .symtab for a dynamic object. */
749 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
750 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
752 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
756 /* Read in any version definitions. */
758 if (! _bfd_elf_slurp_version_tables (abfd
))
761 /* Read in the symbol versions, but don't bother to convert them
762 to internal format. */
763 if (elf_dynversym (abfd
) != 0)
765 Elf_Internal_Shdr
*versymhdr
;
767 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
768 extversym
= (Elf_External_Versym
*) bfd_malloc (hdr
->sh_size
);
769 if (extversym
== NULL
)
771 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
772 || (bfd_read ((PTR
) extversym
, 1, versymhdr
->sh_size
, abfd
)
773 != versymhdr
->sh_size
))
778 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
780 /* The sh_info field of the symtab header tells us where the
781 external symbols start. We don't care about the local symbols at
783 if (elf_bad_symtab (abfd
))
785 extsymcount
= symcount
;
790 extsymcount
= symcount
- hdr
->sh_info
;
791 extsymoff
= hdr
->sh_info
;
794 buf
= ((Elf_External_Sym
*)
795 bfd_malloc (extsymcount
* sizeof (Elf_External_Sym
)));
796 if (buf
== NULL
&& extsymcount
!= 0)
799 /* We store a pointer to the hash table entry for each external
801 sym_hash
= ((struct elf_link_hash_entry
**)
803 extsymcount
* sizeof (struct elf_link_hash_entry
*)));
804 if (sym_hash
== NULL
)
806 elf_sym_hashes (abfd
) = sym_hash
;
810 /* If we are creating a shared library, create all the dynamic
811 sections immediately. We need to attach them to something,
812 so we attach them to this BFD, provided it is the right
813 format. FIXME: If there are no input BFD's of the same
814 format as the output, we can't make a shared library. */
816 && ! elf_hash_table (info
)->dynamic_sections_created
817 && abfd
->xvec
== info
->hash
->creator
)
819 if (! elf_link_create_dynamic_sections (abfd
, info
))
828 bfd_size_type oldsize
;
829 bfd_size_type strindex
;
831 /* Find the name to use in a DT_NEEDED entry that refers to this
832 object. If the object has a DT_SONAME entry, we use it.
833 Otherwise, if the generic linker stuck something in
834 elf_dt_name, we use that. Otherwise, we just use the file
835 name. If the generic linker put a null string into
836 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
837 there is a DT_SONAME entry. */
839 name
= bfd_get_filename (abfd
);
840 if (elf_dt_name (abfd
) != NULL
)
842 name
= elf_dt_name (abfd
);
846 s
= bfd_get_section_by_name (abfd
, ".dynamic");
849 Elf_External_Dyn
*extdyn
;
850 Elf_External_Dyn
*extdynend
;
854 dynbuf
= (Elf_External_Dyn
*) bfd_malloc ((size_t) s
->_raw_size
);
858 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
859 (file_ptr
) 0, s
->_raw_size
))
862 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
865 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
868 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
869 for (; extdyn
< extdynend
; extdyn
++)
871 Elf_Internal_Dyn dyn
;
873 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
874 if (dyn
.d_tag
== DT_SONAME
)
876 name
= bfd_elf_string_from_elf_section (abfd
, link
,
881 if (dyn
.d_tag
== DT_NEEDED
)
883 struct bfd_link_needed_list
*n
, **pn
;
886 n
= ((struct bfd_link_needed_list
*)
887 bfd_alloc (abfd
, sizeof (struct bfd_link_needed_list
)));
888 fnm
= bfd_elf_string_from_elf_section (abfd
, link
,
890 if (n
== NULL
|| fnm
== NULL
)
892 anm
= bfd_alloc (abfd
, strlen (fnm
) + 1);
899 for (pn
= &elf_hash_table (info
)->needed
;
911 /* We do not want to include any of the sections in a dynamic
912 object in the output file. We hack by simply clobbering the
913 list of sections in the BFD. This could be handled more
914 cleanly by, say, a new section flag; the existing
915 SEC_NEVER_LOAD flag is not the one we want, because that one
916 still implies that the section takes up space in the output
918 abfd
->sections
= NULL
;
919 abfd
->section_count
= 0;
921 /* If this is the first dynamic object found in the link, create
922 the special sections required for dynamic linking. */
923 if (! elf_hash_table (info
)->dynamic_sections_created
)
925 if (! elf_link_create_dynamic_sections (abfd
, info
))
931 /* Add a DT_NEEDED entry for this dynamic object. */
932 oldsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
933 strindex
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, name
,
935 if (strindex
== (bfd_size_type
) -1)
938 if (oldsize
== _bfd_stringtab_size (elf_hash_table (info
)->dynstr
))
941 Elf_External_Dyn
*dyncon
, *dynconend
;
943 /* The hash table size did not change, which means that
944 the dynamic object name was already entered. If we
945 have already included this dynamic object in the
946 link, just ignore it. There is no reason to include
947 a particular dynamic object more than once. */
948 sdyn
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
950 BFD_ASSERT (sdyn
!= NULL
);
952 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
953 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
955 for (; dyncon
< dynconend
; dyncon
++)
957 Elf_Internal_Dyn dyn
;
959 elf_swap_dyn_in (elf_hash_table (info
)->dynobj
, dyncon
,
961 if (dyn
.d_tag
== DT_NEEDED
962 && dyn
.d_un
.d_val
== strindex
)
966 if (extversym
!= NULL
)
973 if (! elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
977 /* Save the SONAME, if there is one, because sometimes the
978 linker emulation code will need to know it. */
980 name
= bfd_get_filename (abfd
);
981 elf_dt_name (abfd
) = name
;
985 hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
),
987 || (bfd_read ((PTR
) buf
, sizeof (Elf_External_Sym
), extsymcount
, abfd
)
988 != extsymcount
* sizeof (Elf_External_Sym
)))
993 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
994 esymend
= buf
+ extsymcount
;
997 esym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
999 Elf_Internal_Sym sym
;
1005 struct elf_link_hash_entry
*h
;
1007 boolean size_change_ok
, type_change_ok
;
1008 boolean new_weakdef
;
1009 unsigned int old_alignment
;
1011 elf_swap_symbol_in (abfd
, esym
, &sym
);
1013 flags
= BSF_NO_FLAGS
;
1015 value
= sym
.st_value
;
1018 bind
= ELF_ST_BIND (sym
.st_info
);
1019 if (bind
== STB_LOCAL
)
1021 /* This should be impossible, since ELF requires that all
1022 global symbols follow all local symbols, and that sh_info
1023 point to the first global symbol. Unfortunatealy, Irix 5
1027 else if (bind
== STB_GLOBAL
)
1029 if (sym
.st_shndx
!= SHN_UNDEF
1030 && sym
.st_shndx
!= SHN_COMMON
)
1035 else if (bind
== STB_WEAK
)
1039 /* Leave it up to the processor backend. */
1042 if (sym
.st_shndx
== SHN_UNDEF
)
1043 sec
= bfd_und_section_ptr
;
1044 else if (sym
.st_shndx
> 0 && sym
.st_shndx
< SHN_LORESERVE
)
1046 sec
= section_from_elf_index (abfd
, sym
.st_shndx
);
1048 sec
= bfd_abs_section_ptr
;
1049 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
1052 else if (sym
.st_shndx
== SHN_ABS
)
1053 sec
= bfd_abs_section_ptr
;
1054 else if (sym
.st_shndx
== SHN_COMMON
)
1056 sec
= bfd_com_section_ptr
;
1057 /* What ELF calls the size we call the value. What ELF
1058 calls the value we call the alignment. */
1059 value
= sym
.st_size
;
1063 /* Leave it up to the processor backend. */
1066 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
1067 if (name
== (const char *) NULL
)
1070 if (add_symbol_hook
)
1072 if (! (*add_symbol_hook
) (abfd
, info
, &sym
, &name
, &flags
, &sec
,
1076 /* The hook function sets the name to NULL if this symbol
1077 should be skipped for some reason. */
1078 if (name
== (const char *) NULL
)
1082 /* Sanity check that all possibilities were handled. */
1083 if (sec
== (asection
*) NULL
)
1085 bfd_set_error (bfd_error_bad_value
);
1089 if (bfd_is_und_section (sec
)
1090 || bfd_is_com_section (sec
))
1095 size_change_ok
= false;
1096 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
1098 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1100 Elf_Internal_Versym iver
;
1101 unsigned int vernum
= 0;
1106 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
1107 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
1109 /* If this is a hidden symbol, or if it is not version
1110 1, we append the version name to the symbol name.
1111 However, we do not modify a non-hidden absolute
1112 symbol, because it might be the version symbol
1113 itself. FIXME: What if it isn't? */
1114 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
1115 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
1118 int namelen
, newlen
;
1121 if (sym
.st_shndx
!= SHN_UNDEF
)
1123 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
1125 (*_bfd_error_handler
)
1126 (_("%s: %s: invalid version %u (max %d)"),
1127 abfd
->filename
, name
, vernum
,
1128 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1129 bfd_set_error (bfd_error_bad_value
);
1132 else if (vernum
> 1)
1134 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1140 /* We cannot simply test for the number of
1141 entries in the VERNEED section since the
1142 numbers for the needed versions do not start
1144 Elf_Internal_Verneed
*t
;
1147 for (t
= elf_tdata (abfd
)->verref
;
1151 Elf_Internal_Vernaux
*a
;
1153 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1155 if (a
->vna_other
== vernum
)
1157 verstr
= a
->vna_nodename
;
1166 (*_bfd_error_handler
)
1167 (_("%s: %s: invalid needed version %d"),
1168 abfd
->filename
, name
, vernum
);
1169 bfd_set_error (bfd_error_bad_value
);
1174 namelen
= strlen (name
);
1175 newlen
= namelen
+ strlen (verstr
) + 2;
1176 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
1179 newname
= (char *) bfd_alloc (abfd
, newlen
);
1180 if (newname
== NULL
)
1182 strcpy (newname
, name
);
1183 p
= newname
+ namelen
;
1185 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
1193 if (! elf_merge_symbol (abfd
, info
, name
, &sym
, &sec
, &value
,
1194 sym_hash
, &override
, &type_change_ok
,
1202 while (h
->root
.type
== bfd_link_hash_indirect
1203 || h
->root
.type
== bfd_link_hash_warning
)
1204 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1206 /* Remember the old alignment if this is a common symbol, so
1207 that we don't reduce the alignment later on. We can't
1208 check later, because _bfd_generic_link_add_one_symbol
1209 will set a default for the alignment which we want to
1211 if (h
->root
.type
== bfd_link_hash_common
)
1212 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1214 if (elf_tdata (abfd
)->verdef
!= NULL
1218 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1221 if (! (_bfd_generic_link_add_one_symbol
1222 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
1223 false, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
1227 while (h
->root
.type
== bfd_link_hash_indirect
1228 || h
->root
.type
== bfd_link_hash_warning
)
1229 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1232 new_weakdef
= false;
1235 && (flags
& BSF_WEAK
) != 0
1236 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
1237 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1238 && h
->weakdef
== NULL
)
1240 /* Keep a list of all weak defined non function symbols from
1241 a dynamic object, using the weakdef field. Later in this
1242 function we will set the weakdef field to the correct
1243 value. We only put non-function symbols from dynamic
1244 objects on this list, because that happens to be the only
1245 time we need to know the normal symbol corresponding to a
1246 weak symbol, and the information is time consuming to
1247 figure out. If the weakdef field is not already NULL,
1248 then this symbol was already defined by some previous
1249 dynamic object, and we will be using that previous
1250 definition anyhow. */
1257 /* Set the alignment of a common symbol. */
1258 if (sym
.st_shndx
== SHN_COMMON
1259 && h
->root
.type
== bfd_link_hash_common
)
1263 align
= bfd_log2 (sym
.st_value
);
1264 if (align
> old_alignment
)
1265 h
->root
.u
.c
.p
->alignment_power
= align
;
1268 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1274 /* Remember the symbol size and type. */
1275 if (sym
.st_size
!= 0
1276 && (definition
|| h
->size
== 0))
1278 if (h
->size
!= 0 && h
->size
!= sym
.st_size
&& ! size_change_ok
)
1279 (*_bfd_error_handler
)
1280 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1281 name
, (unsigned long) h
->size
, (unsigned long) sym
.st_size
,
1282 bfd_get_filename (abfd
));
1284 h
->size
= sym
.st_size
;
1287 /* If this is a common symbol, then we always want H->SIZE
1288 to be the size of the common symbol. The code just above
1289 won't fix the size if a common symbol becomes larger. We
1290 don't warn about a size change here, because that is
1291 covered by --warn-common. */
1292 if (h
->root
.type
== bfd_link_hash_common
)
1293 h
->size
= h
->root
.u
.c
.size
;
1295 if (ELF_ST_TYPE (sym
.st_info
) != STT_NOTYPE
1296 && (definition
|| h
->type
== STT_NOTYPE
))
1298 if (h
->type
!= STT_NOTYPE
1299 && h
->type
!= ELF_ST_TYPE (sym
.st_info
)
1300 && ! type_change_ok
)
1301 (*_bfd_error_handler
)
1302 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1303 name
, h
->type
, ELF_ST_TYPE (sym
.st_info
),
1304 bfd_get_filename (abfd
));
1306 h
->type
= ELF_ST_TYPE (sym
.st_info
);
1309 if (sym
.st_other
!= 0
1310 && (definition
|| h
->other
== 0))
1311 h
->other
= sym
.st_other
;
1313 /* Set a flag in the hash table entry indicating the type of
1314 reference or definition we just found. Keep a count of
1315 the number of dynamic symbols we find. A dynamic symbol
1316 is one which is referenced or defined by both a regular
1317 object and a shared object. */
1318 old_flags
= h
->elf_link_hash_flags
;
1323 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
1325 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
1327 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1328 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
1334 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
1336 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
1337 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
1338 | ELF_LINK_HASH_REF_REGULAR
)) != 0
1339 || (h
->weakdef
!= NULL
1341 && h
->weakdef
->dynindx
!= -1))
1345 h
->elf_link_hash_flags
|= new_flag
;
1347 /* If this symbol has a version, and it is the default
1348 version, we create an indirect symbol from the default
1349 name to the fully decorated name. This will cause
1350 external references which do not specify a version to be
1351 bound to this version of the symbol. */
1356 p
= strchr (name
, ELF_VER_CHR
);
1357 if (p
!= NULL
&& p
[1] == ELF_VER_CHR
)
1360 struct elf_link_hash_entry
*hi
;
1363 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1365 if (shortname
== NULL
)
1367 strncpy (shortname
, name
, p
- name
);
1368 shortname
[p
- name
] = '\0';
1370 /* We are going to create a new symbol. Merge it
1371 with any existing symbol with this name. For the
1372 purposes of the merge, act as though we were
1373 defining the symbol we just defined, although we
1374 actually going to define an indirect symbol. */
1375 type_change_ok
= false;
1376 size_change_ok
= false;
1377 if (! elf_merge_symbol (abfd
, info
, shortname
, &sym
, &sec
,
1378 &value
, &hi
, &override
,
1379 &type_change_ok
, &size_change_ok
))
1384 if (! (_bfd_generic_link_add_one_symbol
1385 (info
, abfd
, shortname
, BSF_INDIRECT
,
1386 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1387 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1392 /* In this case the symbol named SHORTNAME is
1393 overriding the indirect symbol we want to
1394 add. We were planning on making SHORTNAME an
1395 indirect symbol referring to NAME. SHORTNAME
1396 is the name without a version. NAME is the
1397 fully versioned name, and it is the default
1400 Overriding means that we already saw a
1401 definition for the symbol SHORTNAME in a
1402 regular object, and it is overriding the
1403 symbol defined in the dynamic object.
1405 When this happens, we actually want to change
1406 NAME, the symbol we just added, to refer to
1407 SHORTNAME. This will cause references to
1408 NAME in the shared object to become
1409 references to SHORTNAME in the regular
1410 object. This is what we expect when we
1411 override a function in a shared object: that
1412 the references in the shared object will be
1413 mapped to the definition in the regular
1416 while (hi
->root
.type
== bfd_link_hash_indirect
1417 || hi
->root
.type
== bfd_link_hash_warning
)
1418 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1420 h
->root
.type
= bfd_link_hash_indirect
;
1421 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1422 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1424 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1425 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1426 if (! _bfd_elf_link_record_dynamic_symbol (info
, hi
))
1430 /* Now set HI to H, so that the following code
1431 will set the other fields correctly. */
1435 /* If there is a duplicate definition somewhere,
1436 then HI may not point to an indirect symbol. We
1437 will have reported an error to the user in that
1440 if (hi
->root
.type
== bfd_link_hash_indirect
)
1442 struct elf_link_hash_entry
*ht
;
1444 /* If the symbol became indirect, then we assume
1445 that we have not seen a definition before. */
1446 BFD_ASSERT ((hi
->elf_link_hash_flags
1447 & (ELF_LINK_HASH_DEF_DYNAMIC
1448 | ELF_LINK_HASH_DEF_REGULAR
))
1451 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1453 /* Copy down any references that we may have
1454 already seen to the symbol which just became
1456 ht
->elf_link_hash_flags
|=
1457 (hi
->elf_link_hash_flags
1458 & (ELF_LINK_HASH_REF_DYNAMIC
1459 | ELF_LINK_HASH_REF_REGULAR
));
1461 /* Copy over the global and procedure linkage table
1462 offset entries. These may have been already set
1463 up by a check_relocs routine. */
1464 if (ht
->got
.offset
== (bfd_vma
) -1)
1466 ht
->got
.offset
= hi
->got
.offset
;
1467 hi
->got
.offset
= (bfd_vma
) -1;
1469 BFD_ASSERT (hi
->got
.offset
== (bfd_vma
) -1);
1471 if (ht
->plt
.offset
== (bfd_vma
) -1)
1473 ht
->plt
.offset
= hi
->plt
.offset
;
1474 hi
->plt
.offset
= (bfd_vma
) -1;
1476 BFD_ASSERT (hi
->plt
.offset
== (bfd_vma
) -1);
1478 if (ht
->dynindx
== -1)
1480 ht
->dynindx
= hi
->dynindx
;
1481 ht
->dynstr_index
= hi
->dynstr_index
;
1483 hi
->dynstr_index
= 0;
1485 BFD_ASSERT (hi
->dynindx
== -1);
1487 /* FIXME: There may be other information to copy
1488 over for particular targets. */
1490 /* See if the new flags lead us to realize that
1491 the symbol must be dynamic. */
1497 || ((hi
->elf_link_hash_flags
1498 & ELF_LINK_HASH_REF_DYNAMIC
)
1504 if ((hi
->elf_link_hash_flags
1505 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1511 /* We also need to define an indirection from the
1512 nondefault version of the symbol. */
1514 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1516 if (shortname
== NULL
)
1518 strncpy (shortname
, name
, p
- name
);
1519 strcpy (shortname
+ (p
- name
), p
+ 1);
1521 /* Once again, merge with any existing symbol. */
1522 type_change_ok
= false;
1523 size_change_ok
= false;
1524 if (! elf_merge_symbol (abfd
, info
, shortname
, &sym
, &sec
,
1525 &value
, &hi
, &override
,
1526 &type_change_ok
, &size_change_ok
))
1531 /* Here SHORTNAME is a versioned name, so we
1532 don't expect to see the type of override we
1533 do in the case above. */
1534 (*_bfd_error_handler
)
1535 (_("%s: warning: unexpected redefinition of `%s'"),
1536 bfd_get_filename (abfd
), shortname
);
1540 if (! (_bfd_generic_link_add_one_symbol
1541 (info
, abfd
, shortname
, BSF_INDIRECT
,
1542 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1543 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1546 /* If there is a duplicate definition somewhere,
1547 then HI may not point to an indirect symbol.
1548 We will have reported an error to the user in
1551 if (hi
->root
.type
== bfd_link_hash_indirect
)
1553 /* If the symbol became indirect, then we
1554 assume that we have not seen a definition
1556 BFD_ASSERT ((hi
->elf_link_hash_flags
1557 & (ELF_LINK_HASH_DEF_DYNAMIC
1558 | ELF_LINK_HASH_DEF_REGULAR
))
1561 /* Copy down any references that we may have
1562 already seen to the symbol which just
1564 h
->elf_link_hash_flags
|=
1565 (hi
->elf_link_hash_flags
1566 & (ELF_LINK_HASH_REF_DYNAMIC
1567 | ELF_LINK_HASH_REF_REGULAR
));
1569 /* Copy over the global and procedure linkage
1570 table offset entries. These may have been
1571 already set up by a check_relocs routine. */
1572 if (h
->got
.offset
== (bfd_vma
) -1)
1574 h
->got
.offset
= hi
->got
.offset
;
1575 hi
->got
.offset
= (bfd_vma
) -1;
1577 BFD_ASSERT (hi
->got
.offset
== (bfd_vma
) -1);
1579 if (h
->plt
.offset
== (bfd_vma
) -1)
1581 h
->plt
.offset
= hi
->plt
.offset
;
1582 hi
->plt
.offset
= (bfd_vma
) -1;
1584 BFD_ASSERT (hi
->got
.offset
== (bfd_vma
) -1);
1586 if (h
->dynindx
== -1)
1588 h
->dynindx
= hi
->dynindx
;
1589 h
->dynstr_index
= hi
->dynstr_index
;
1591 hi
->dynstr_index
= 0;
1593 BFD_ASSERT (hi
->dynindx
== -1);
1595 /* FIXME: There may be other information to
1596 copy over for particular targets. */
1598 /* See if the new flags lead us to realize
1599 that the symbol must be dynamic. */
1605 || ((hi
->elf_link_hash_flags
1606 & ELF_LINK_HASH_REF_DYNAMIC
)
1612 if ((hi
->elf_link_hash_flags
1613 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1622 if (dynsym
&& h
->dynindx
== -1)
1624 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1626 if (h
->weakdef
!= NULL
1628 && h
->weakdef
->dynindx
== -1)
1630 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1638 /* Now set the weakdefs field correctly for all the weak defined
1639 symbols we found. The only way to do this is to search all the
1640 symbols. Since we only need the information for non functions in
1641 dynamic objects, that's the only time we actually put anything on
1642 the list WEAKS. We need this information so that if a regular
1643 object refers to a symbol defined weakly in a dynamic object, the
1644 real symbol in the dynamic object is also put in the dynamic
1645 symbols; we also must arrange for both symbols to point to the
1646 same memory location. We could handle the general case of symbol
1647 aliasing, but a general symbol alias can only be generated in
1648 assembler code, handling it correctly would be very time
1649 consuming, and other ELF linkers don't handle general aliasing
1651 while (weaks
!= NULL
)
1653 struct elf_link_hash_entry
*hlook
;
1656 struct elf_link_hash_entry
**hpp
;
1657 struct elf_link_hash_entry
**hppend
;
1660 weaks
= hlook
->weakdef
;
1661 hlook
->weakdef
= NULL
;
1663 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
1664 || hlook
->root
.type
== bfd_link_hash_defweak
1665 || hlook
->root
.type
== bfd_link_hash_common
1666 || hlook
->root
.type
== bfd_link_hash_indirect
);
1667 slook
= hlook
->root
.u
.def
.section
;
1668 vlook
= hlook
->root
.u
.def
.value
;
1670 hpp
= elf_sym_hashes (abfd
);
1671 hppend
= hpp
+ extsymcount
;
1672 for (; hpp
< hppend
; hpp
++)
1674 struct elf_link_hash_entry
*h
;
1677 if (h
!= NULL
&& h
!= hlook
1678 && h
->root
.type
== bfd_link_hash_defined
1679 && h
->root
.u
.def
.section
== slook
1680 && h
->root
.u
.def
.value
== vlook
)
1684 /* If the weak definition is in the list of dynamic
1685 symbols, make sure the real definition is put there
1687 if (hlook
->dynindx
!= -1
1688 && h
->dynindx
== -1)
1690 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1694 /* If the real definition is in the list of dynamic
1695 symbols, make sure the weak definition is put there
1696 as well. If we don't do this, then the dynamic
1697 loader might not merge the entries for the real
1698 definition and the weak definition. */
1699 if (h
->dynindx
!= -1
1700 && hlook
->dynindx
== -1)
1702 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
1717 if (extversym
!= NULL
)
1723 /* If this object is the same format as the output object, and it is
1724 not a shared library, then let the backend look through the
1727 This is required to build global offset table entries and to
1728 arrange for dynamic relocs. It is not required for the
1729 particular common case of linking non PIC code, even when linking
1730 against shared libraries, but unfortunately there is no way of
1731 knowing whether an object file has been compiled PIC or not.
1732 Looking through the relocs is not particularly time consuming.
1733 The problem is that we must either (1) keep the relocs in memory,
1734 which causes the linker to require additional runtime memory or
1735 (2) read the relocs twice from the input file, which wastes time.
1736 This would be a good case for using mmap.
1738 I have no idea how to handle linking PIC code into a file of a
1739 different format. It probably can't be done. */
1740 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
1742 && abfd
->xvec
== info
->hash
->creator
1743 && check_relocs
!= NULL
)
1747 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1749 Elf_Internal_Rela
*internal_relocs
;
1752 if ((o
->flags
& SEC_RELOC
) == 0
1753 || o
->reloc_count
== 0
1754 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
1755 && (o
->flags
& SEC_DEBUGGING
) != 0)
1756 || bfd_is_abs_section (o
->output_section
))
1759 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
1760 (abfd
, o
, (PTR
) NULL
,
1761 (Elf_Internal_Rela
*) NULL
,
1762 info
->keep_memory
));
1763 if (internal_relocs
== NULL
)
1766 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
1768 if (! info
->keep_memory
)
1769 free (internal_relocs
);
1776 /* If this is a non-traditional, non-relocateable link, try to
1777 optimize the handling of the .stab/.stabstr sections. */
1779 && ! info
->relocateable
1780 && ! info
->traditional_format
1781 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1782 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
1784 asection
*stab
, *stabstr
;
1786 stab
= bfd_get_section_by_name (abfd
, ".stab");
1789 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
1791 if (stabstr
!= NULL
)
1793 struct bfd_elf_section_data
*secdata
;
1795 secdata
= elf_section_data (stab
);
1796 if (! _bfd_link_section_stabs (abfd
,
1797 &elf_hash_table (info
)->stab_info
,
1799 &secdata
->stab_info
))
1814 if (extversym
!= NULL
)
1819 /* Create some sections which will be filled in with dynamic linking
1820 information. ABFD is an input file which requires dynamic sections
1821 to be created. The dynamic sections take up virtual memory space
1822 when the final executable is run, so we need to create them before
1823 addresses are assigned to the output sections. We work out the
1824 actual contents and size of these sections later. */
1827 elf_link_create_dynamic_sections (abfd
, info
)
1829 struct bfd_link_info
*info
;
1832 register asection
*s
;
1833 struct elf_link_hash_entry
*h
;
1834 struct elf_backend_data
*bed
;
1836 if (elf_hash_table (info
)->dynamic_sections_created
)
1839 /* Make sure that all dynamic sections use the same input BFD. */
1840 if (elf_hash_table (info
)->dynobj
== NULL
)
1841 elf_hash_table (info
)->dynobj
= abfd
;
1843 abfd
= elf_hash_table (info
)->dynobj
;
1845 /* Note that we set the SEC_IN_MEMORY flag for all of these
1847 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1848 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
1850 /* A dynamically linked executable has a .interp section, but a
1851 shared library does not. */
1854 s
= bfd_make_section (abfd
, ".interp");
1856 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
1860 /* Create sections to hold version informations. These are removed
1861 if they are not needed. */
1862 s
= bfd_make_section (abfd
, ".gnu.version_d");
1864 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1865 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1868 s
= bfd_make_section (abfd
, ".gnu.version");
1870 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1871 || ! bfd_set_section_alignment (abfd
, s
, 1))
1874 s
= bfd_make_section (abfd
, ".gnu.version_r");
1876 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1877 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1880 s
= bfd_make_section (abfd
, ".dynsym");
1882 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1883 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1886 s
= bfd_make_section (abfd
, ".dynstr");
1888 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
1891 /* Create a strtab to hold the dynamic symbol names. */
1892 if (elf_hash_table (info
)->dynstr
== NULL
)
1894 elf_hash_table (info
)->dynstr
= elf_stringtab_init ();
1895 if (elf_hash_table (info
)->dynstr
== NULL
)
1899 s
= bfd_make_section (abfd
, ".dynamic");
1901 || ! bfd_set_section_flags (abfd
, s
, flags
)
1902 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1905 /* The special symbol _DYNAMIC is always set to the start of the
1906 .dynamic section. This call occurs before we have processed the
1907 symbols for any dynamic object, so we don't have to worry about
1908 overriding a dynamic definition. We could set _DYNAMIC in a
1909 linker script, but we only want to define it if we are, in fact,
1910 creating a .dynamic section. We don't want to define it if there
1911 is no .dynamic section, since on some ELF platforms the start up
1912 code examines it to decide how to initialize the process. */
1914 if (! (_bfd_generic_link_add_one_symbol
1915 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
1916 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
1917 (struct bfd_link_hash_entry
**) &h
)))
1919 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
1920 h
->type
= STT_OBJECT
;
1923 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1926 s
= bfd_make_section (abfd
, ".hash");
1928 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1929 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1932 /* Let the backend create the rest of the sections. This lets the
1933 backend set the right flags. The backend will normally create
1934 the .got and .plt sections. */
1935 bed
= get_elf_backend_data (abfd
);
1936 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
1939 elf_hash_table (info
)->dynamic_sections_created
= true;
1944 /* Add an entry to the .dynamic table. */
1947 elf_add_dynamic_entry (info
, tag
, val
)
1948 struct bfd_link_info
*info
;
1952 Elf_Internal_Dyn dyn
;
1956 bfd_byte
*newcontents
;
1958 dynobj
= elf_hash_table (info
)->dynobj
;
1960 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
1961 BFD_ASSERT (s
!= NULL
);
1963 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
1964 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
1965 if (newcontents
== NULL
)
1969 dyn
.d_un
.d_val
= val
;
1970 elf_swap_dyn_out (dynobj
, &dyn
,
1971 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
1973 s
->_raw_size
= newsize
;
1974 s
->contents
= newcontents
;
1980 /* Read and swap the relocs for a section. They may have been cached.
1981 If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are not NULL,
1982 they are used as buffers to read into. They are known to be large
1983 enough. If the INTERNAL_RELOCS relocs argument is NULL, the return
1984 value is allocated using either malloc or bfd_alloc, according to
1985 the KEEP_MEMORY argument. */
1988 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
1992 PTR external_relocs
;
1993 Elf_Internal_Rela
*internal_relocs
;
1994 boolean keep_memory
;
1996 Elf_Internal_Shdr
*rel_hdr
;
1998 Elf_Internal_Rela
*alloc2
= NULL
;
2000 if (elf_section_data (o
)->relocs
!= NULL
)
2001 return elf_section_data (o
)->relocs
;
2003 if (o
->reloc_count
== 0)
2006 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2008 if (internal_relocs
== NULL
)
2012 size
= o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2014 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2016 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2017 if (internal_relocs
== NULL
)
2021 if (external_relocs
== NULL
)
2023 alloc1
= (PTR
) bfd_malloc ((size_t) rel_hdr
->sh_size
);
2026 external_relocs
= alloc1
;
2029 if ((bfd_seek (abfd
, rel_hdr
->sh_offset
, SEEK_SET
) != 0)
2030 || (bfd_read (external_relocs
, 1, rel_hdr
->sh_size
, abfd
)
2031 != rel_hdr
->sh_size
))
2034 /* Swap in the relocs. For convenience, we always produce an
2035 Elf_Internal_Rela array; if the relocs are Rel, we set the addend
2037 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2039 Elf_External_Rel
*erel
;
2040 Elf_External_Rel
*erelend
;
2041 Elf_Internal_Rela
*irela
;
2043 erel
= (Elf_External_Rel
*) external_relocs
;
2044 erelend
= erel
+ o
->reloc_count
;
2045 irela
= internal_relocs
;
2046 for (; erel
< erelend
; erel
++, irela
++)
2048 Elf_Internal_Rel irel
;
2050 elf_swap_reloc_in (abfd
, erel
, &irel
);
2051 irela
->r_offset
= irel
.r_offset
;
2052 irela
->r_info
= irel
.r_info
;
2053 irela
->r_addend
= 0;
2058 Elf_External_Rela
*erela
;
2059 Elf_External_Rela
*erelaend
;
2060 Elf_Internal_Rela
*irela
;
2062 BFD_ASSERT (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
));
2064 erela
= (Elf_External_Rela
*) external_relocs
;
2065 erelaend
= erela
+ o
->reloc_count
;
2066 irela
= internal_relocs
;
2067 for (; erela
< erelaend
; erela
++, irela
++)
2068 elf_swap_reloca_in (abfd
, erela
, irela
);
2071 /* Cache the results for next time, if we can. */
2073 elf_section_data (o
)->relocs
= internal_relocs
;
2078 /* Don't free alloc2, since if it was allocated we are passing it
2079 back (under the name of internal_relocs). */
2081 return internal_relocs
;
2092 /* Record an assignment to a symbol made by a linker script. We need
2093 this in case some dynamic object refers to this symbol. */
2097 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
2099 struct bfd_link_info
*info
;
2103 struct elf_link_hash_entry
*h
;
2105 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2108 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, true, false);
2112 if (h
->root
.type
== bfd_link_hash_new
)
2113 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
2115 /* If this symbol is being provided by the linker script, and it is
2116 currently defined by a dynamic object, but not by a regular
2117 object, then mark it as undefined so that the generic linker will
2118 force the correct value. */
2120 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2121 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2122 h
->root
.type
= bfd_link_hash_undefined
;
2124 /* If this symbol is not being provided by the linker script, and it is
2125 currently defined by a dynamic object, but not by a regular object,
2126 then clear out any version information because the symbol will not be
2127 associated with the dynamic object any more. */
2129 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2130 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2131 h
->verinfo
.verdef
= NULL
;
2133 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2134 h
->type
= STT_OBJECT
;
2136 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2137 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
2139 && h
->dynindx
== -1)
2141 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2144 /* If this is a weak defined symbol, and we know a corresponding
2145 real symbol from the same dynamic object, make sure the real
2146 symbol is also made into a dynamic symbol. */
2147 if (h
->weakdef
!= NULL
2148 && h
->weakdef
->dynindx
== -1)
2150 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2158 /* This structure is used to pass information to
2159 elf_link_assign_sym_version. */
2161 struct elf_assign_sym_version_info
2165 /* General link information. */
2166 struct bfd_link_info
*info
;
2168 struct bfd_elf_version_tree
*verdefs
;
2169 /* Whether we are exporting all dynamic symbols. */
2170 boolean export_dynamic
;
2171 /* Whether we removed any symbols from the dynamic symbol table. */
2172 boolean removed_dynamic
;
2173 /* Whether we had a failure. */
2177 /* This structure is used to pass information to
2178 elf_link_find_version_dependencies. */
2180 struct elf_find_verdep_info
2184 /* General link information. */
2185 struct bfd_link_info
*info
;
2186 /* The number of dependencies. */
2188 /* Whether we had a failure. */
2192 /* Array used to determine the number of hash table buckets to use
2193 based on the number of symbols there are. If there are fewer than
2194 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2195 fewer than 37 we use 17 buckets, and so forth. We never use more
2196 than 32771 buckets. */
2198 static const size_t elf_buckets
[] =
2200 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2204 /* Set up the sizes and contents of the ELF dynamic sections. This is
2205 called by the ELF linker emulation before_allocation routine. We
2206 must set the sizes of the sections before the linker sets the
2207 addresses of the various sections. */
2210 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
2211 export_dynamic
, filter_shlib
,
2212 auxiliary_filters
, info
, sinterpptr
,
2217 boolean export_dynamic
;
2218 const char *filter_shlib
;
2219 const char * const *auxiliary_filters
;
2220 struct bfd_link_info
*info
;
2221 asection
**sinterpptr
;
2222 struct bfd_elf_version_tree
*verdefs
;
2224 bfd_size_type soname_indx
;
2226 struct elf_backend_data
*bed
;
2227 bfd_size_type old_dynsymcount
;
2228 struct elf_assign_sym_version_info asvinfo
;
2232 soname_indx
= (bfd_size_type
) -1;
2234 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2237 /* The backend may have to create some sections regardless of whether
2238 we're dynamic or not. */
2239 bed
= get_elf_backend_data (output_bfd
);
2240 if (bed
->elf_backend_always_size_sections
2241 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
2244 dynobj
= elf_hash_table (info
)->dynobj
;
2246 /* If there were no dynamic objects in the link, there is nothing to
2251 /* If we are supposed to export all symbols into the dynamic symbol
2252 table (this is not the normal case), then do so. */
2255 struct elf_info_failed eif
;
2259 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
2265 if (elf_hash_table (info
)->dynamic_sections_created
)
2267 struct elf_info_failed eif
;
2268 struct elf_link_hash_entry
*h
;
2269 bfd_size_type strsize
;
2271 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
2272 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
2276 soname_indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2277 soname
, true, true);
2278 if (soname_indx
== (bfd_size_type
) -1
2279 || ! elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
2285 if (! elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
2293 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, rpath
,
2295 if (indx
== (bfd_size_type
) -1
2296 || ! elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
2300 if (filter_shlib
!= NULL
)
2304 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2305 filter_shlib
, true, true);
2306 if (indx
== (bfd_size_type
) -1
2307 || ! elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
2311 if (auxiliary_filters
!= NULL
)
2313 const char * const *p
;
2315 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
2319 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2321 if (indx
== (bfd_size_type
) -1
2322 || ! elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
2327 /* Attach all the symbols to their version information. */
2328 asvinfo
.output_bfd
= output_bfd
;
2329 asvinfo
.info
= info
;
2330 asvinfo
.verdefs
= verdefs
;
2331 asvinfo
.export_dynamic
= export_dynamic
;
2332 asvinfo
.removed_dynamic
= false;
2333 asvinfo
.failed
= false;
2335 elf_link_hash_traverse (elf_hash_table (info
),
2336 elf_link_assign_sym_version
,
2341 /* Find all symbols which were defined in a dynamic object and make
2342 the backend pick a reasonable value for them. */
2345 elf_link_hash_traverse (elf_hash_table (info
),
2346 elf_adjust_dynamic_symbol
,
2351 /* Add some entries to the .dynamic section. We fill in some of the
2352 values later, in elf_bfd_final_link, but we must add the entries
2353 now so that we know the final size of the .dynamic section. */
2354 h
= elf_link_hash_lookup (elf_hash_table (info
), "_init", false,
2357 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2358 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2360 if (! elf_add_dynamic_entry (info
, DT_INIT
, 0))
2363 h
= elf_link_hash_lookup (elf_hash_table (info
), "_fini", false,
2366 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2367 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2369 if (! elf_add_dynamic_entry (info
, DT_FINI
, 0))
2372 strsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
2373 if (! elf_add_dynamic_entry (info
, DT_HASH
, 0)
2374 || ! elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
2375 || ! elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
2376 || ! elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
2377 || ! elf_add_dynamic_entry (info
, DT_SYMENT
,
2378 sizeof (Elf_External_Sym
)))
2382 /* The backend must work out the sizes of all the other dynamic
2384 old_dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2385 if (bed
->elf_backend_size_dynamic_sections
2386 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
2389 if (elf_hash_table (info
)->dynamic_sections_created
)
2394 size_t bucketcount
= 0;
2395 Elf_Internal_Sym isym
;
2397 /* Set up the version definition section. */
2398 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2399 BFD_ASSERT (s
!= NULL
);
2401 /* We may have created additional version definitions if we are
2402 just linking a regular application. */
2403 verdefs
= asvinfo
.verdefs
;
2405 if (verdefs
== NULL
)
2409 /* Don't include this section in the output file. */
2410 for (spp
= &output_bfd
->sections
;
2411 *spp
!= s
->output_section
;
2412 spp
= &(*spp
)->next
)
2414 *spp
= s
->output_section
->next
;
2415 --output_bfd
->section_count
;
2421 struct bfd_elf_version_tree
*t
;
2423 Elf_Internal_Verdef def
;
2424 Elf_Internal_Verdaux defaux
;
2426 if (asvinfo
.removed_dynamic
)
2428 /* Some dynamic symbols were changed to be local
2429 symbols. In this case, we renumber all of the
2430 dynamic symbols, so that we don't have a hole. If
2431 the backend changed dynsymcount, then assume that the
2432 new symbols are at the start. This is the case on
2433 the MIPS. FIXME: The names of the removed symbols
2434 will still be in the dynamic string table, wasting
2436 elf_hash_table (info
)->dynsymcount
=
2437 1 + (elf_hash_table (info
)->dynsymcount
- old_dynsymcount
);
2438 elf_link_hash_traverse (elf_hash_table (info
),
2439 elf_link_renumber_dynsyms
,
2446 /* Make space for the base version. */
2447 size
+= sizeof (Elf_External_Verdef
);
2448 size
+= sizeof (Elf_External_Verdaux
);
2451 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2453 struct bfd_elf_version_deps
*n
;
2455 size
+= sizeof (Elf_External_Verdef
);
2456 size
+= sizeof (Elf_External_Verdaux
);
2459 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2460 size
+= sizeof (Elf_External_Verdaux
);
2463 s
->_raw_size
= size
;
2464 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2465 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2468 /* Fill in the version definition section. */
2472 def
.vd_version
= VER_DEF_CURRENT
;
2473 def
.vd_flags
= VER_FLG_BASE
;
2476 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2477 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2478 + sizeof (Elf_External_Verdaux
));
2480 if (soname_indx
!= (bfd_size_type
) -1)
2482 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) soname
);
2483 defaux
.vda_name
= soname_indx
;
2490 name
= output_bfd
->filename
;
2491 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) name
);
2492 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2494 if (indx
== (bfd_size_type
) -1)
2496 defaux
.vda_name
= indx
;
2498 defaux
.vda_next
= 0;
2500 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2501 (Elf_External_Verdef
*)p
);
2502 p
+= sizeof (Elf_External_Verdef
);
2503 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2504 (Elf_External_Verdaux
*) p
);
2505 p
+= sizeof (Elf_External_Verdaux
);
2507 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2510 struct bfd_elf_version_deps
*n
;
2511 struct elf_link_hash_entry
*h
;
2514 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2517 /* Add a symbol representing this version. */
2519 if (! (_bfd_generic_link_add_one_symbol
2520 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
2521 (bfd_vma
) 0, (const char *) NULL
, false,
2522 get_elf_backend_data (dynobj
)->collect
,
2523 (struct bfd_link_hash_entry
**) &h
)))
2525 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
2526 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2527 h
->type
= STT_OBJECT
;
2528 h
->verinfo
.vertree
= t
;
2530 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2533 def
.vd_version
= VER_DEF_CURRENT
;
2535 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
2536 def
.vd_flags
|= VER_FLG_WEAK
;
2537 def
.vd_ndx
= t
->vernum
+ 1;
2538 def
.vd_cnt
= cdeps
+ 1;
2539 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) t
->name
);
2540 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2541 if (t
->next
!= NULL
)
2542 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2543 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
2547 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2548 (Elf_External_Verdef
*) p
);
2549 p
+= sizeof (Elf_External_Verdef
);
2551 defaux
.vda_name
= h
->dynstr_index
;
2552 if (t
->deps
== NULL
)
2553 defaux
.vda_next
= 0;
2555 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2556 t
->name_indx
= defaux
.vda_name
;
2558 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2559 (Elf_External_Verdaux
*) p
);
2560 p
+= sizeof (Elf_External_Verdaux
);
2562 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2564 if (n
->version_needed
== NULL
)
2566 /* This can happen if there was an error in the
2568 defaux
.vda_name
= 0;
2571 defaux
.vda_name
= n
->version_needed
->name_indx
;
2572 if (n
->next
== NULL
)
2573 defaux
.vda_next
= 0;
2575 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2577 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2578 (Elf_External_Verdaux
*) p
);
2579 p
+= sizeof (Elf_External_Verdaux
);
2583 if (! elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
2584 || ! elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
2587 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
2590 /* Work out the size of the version reference section. */
2592 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2593 BFD_ASSERT (s
!= NULL
);
2595 struct elf_find_verdep_info sinfo
;
2597 sinfo
.output_bfd
= output_bfd
;
2599 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
2600 if (sinfo
.vers
== 0)
2602 sinfo
.failed
= false;
2604 elf_link_hash_traverse (elf_hash_table (info
),
2605 elf_link_find_version_dependencies
,
2608 if (elf_tdata (output_bfd
)->verref
== NULL
)
2612 /* We don't have any version definitions, so we can just
2613 remove the section. */
2615 for (spp
= &output_bfd
->sections
;
2616 *spp
!= s
->output_section
;
2617 spp
= &(*spp
)->next
)
2619 *spp
= s
->output_section
->next
;
2620 --output_bfd
->section_count
;
2624 Elf_Internal_Verneed
*t
;
2629 /* Build the version definition section. */
2632 for (t
= elf_tdata (output_bfd
)->verref
;
2636 Elf_Internal_Vernaux
*a
;
2638 size
+= sizeof (Elf_External_Verneed
);
2640 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2641 size
+= sizeof (Elf_External_Vernaux
);
2644 s
->_raw_size
= size
;
2645 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, size
);
2646 if (s
->contents
== NULL
)
2650 for (t
= elf_tdata (output_bfd
)->verref
;
2655 Elf_Internal_Vernaux
*a
;
2659 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2662 t
->vn_version
= VER_NEED_CURRENT
;
2664 if (elf_dt_name (t
->vn_bfd
) != NULL
)
2665 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2666 elf_dt_name (t
->vn_bfd
),
2669 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2670 t
->vn_bfd
->filename
, true, false);
2671 if (indx
== (bfd_size_type
) -1)
2674 t
->vn_aux
= sizeof (Elf_External_Verneed
);
2675 if (t
->vn_nextref
== NULL
)
2678 t
->vn_next
= (sizeof (Elf_External_Verneed
)
2679 + caux
* sizeof (Elf_External_Vernaux
));
2681 _bfd_elf_swap_verneed_out (output_bfd
, t
,
2682 (Elf_External_Verneed
*) p
);
2683 p
+= sizeof (Elf_External_Verneed
);
2685 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2687 a
->vna_hash
= bfd_elf_hash ((const unsigned char *)
2689 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2690 a
->vna_nodename
, true, false);
2691 if (indx
== (bfd_size_type
) -1)
2694 if (a
->vna_nextptr
== NULL
)
2697 a
->vna_next
= sizeof (Elf_External_Vernaux
);
2699 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
2700 (Elf_External_Vernaux
*) p
);
2701 p
+= sizeof (Elf_External_Vernaux
);
2705 if (! elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
2706 || ! elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
2709 elf_tdata (output_bfd
)->cverrefs
= crefs
;
2713 dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2715 /* Work out the size of the symbol version section. */
2716 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
2717 BFD_ASSERT (s
!= NULL
);
2718 if (dynsymcount
== 0
2719 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
2723 /* We don't need any symbol versions; just discard the
2725 for (spp
= &output_bfd
->sections
;
2726 *spp
!= s
->output_section
;
2727 spp
= &(*spp
)->next
)
2729 *spp
= s
->output_section
->next
;
2730 --output_bfd
->section_count
;
2734 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
2735 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
2736 if (s
->contents
== NULL
)
2739 if (! elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
2743 /* Set the size of the .dynsym and .hash sections. We counted
2744 the number of dynamic symbols in elf_link_add_object_symbols.
2745 We will build the contents of .dynsym and .hash when we build
2746 the final symbol table, because until then we do not know the
2747 correct value to give the symbols. We built the .dynstr
2748 section as we went along in elf_link_add_object_symbols. */
2749 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
2750 BFD_ASSERT (s
!= NULL
);
2751 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
2752 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2753 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2756 /* The first entry in .dynsym is a dummy symbol. */
2763 elf_swap_symbol_out (output_bfd
, &isym
,
2764 (PTR
) (Elf_External_Sym
*) s
->contents
);
2766 for (i
= 0; elf_buckets
[i
] != 0; i
++)
2768 bucketcount
= elf_buckets
[i
];
2769 if (dynsymcount
< elf_buckets
[i
+ 1])
2773 s
= bfd_get_section_by_name (dynobj
, ".hash");
2774 BFD_ASSERT (s
!= NULL
);
2775 s
->_raw_size
= (2 + bucketcount
+ dynsymcount
) * (ARCH_SIZE
/ 8);
2776 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2777 if (s
->contents
== NULL
)
2779 memset (s
->contents
, 0, (size_t) s
->_raw_size
);
2781 put_word (output_bfd
, bucketcount
, s
->contents
);
2782 put_word (output_bfd
, dynsymcount
, s
->contents
+ (ARCH_SIZE
/ 8));
2784 elf_hash_table (info
)->bucketcount
= bucketcount
;
2786 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
2787 BFD_ASSERT (s
!= NULL
);
2788 s
->_raw_size
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
2790 if (! elf_add_dynamic_entry (info
, DT_NULL
, 0))
2797 /* Fix up the flags for a symbol. This handles various cases which
2798 can only be fixed after all the input files are seen. This is
2799 currently called by both adjust_dynamic_symbol and
2800 assign_sym_version, which is unnecessary but perhaps more robust in
2801 the face of future changes. */
2804 elf_fix_symbol_flags (h
, eif
)
2805 struct elf_link_hash_entry
*h
;
2806 struct elf_info_failed
*eif
;
2808 /* If this symbol was mentioned in a non-ELF file, try to set
2809 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2810 permit a non-ELF file to correctly refer to a symbol defined in
2811 an ELF dynamic object. */
2812 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
2814 if (h
->root
.type
!= bfd_link_hash_defined
2815 && h
->root
.type
!= bfd_link_hash_defweak
)
2816 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2819 if (h
->root
.u
.def
.section
->owner
!= NULL
2820 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2821 == bfd_target_elf_flavour
))
2822 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2824 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2827 if (h
->dynindx
== -1
2828 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2829 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
2831 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2839 /* If this is a final link, and the symbol was defined as a common
2840 symbol in a regular object file, and there was no definition in
2841 any dynamic object, then the linker will have allocated space for
2842 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2843 flag will not have been set. */
2844 if (h
->root
.type
== bfd_link_hash_defined
2845 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2846 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
2847 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2848 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2849 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2851 /* If -Bsymbolic was used (which means to bind references to global
2852 symbols to the definition within the shared object), and this
2853 symbol was defined in a regular object, then it actually doesn't
2854 need a PLT entry. */
2855 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
2856 && eif
->info
->shared
2857 && eif
->info
->symbolic
2858 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2860 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
2861 h
->plt
.offset
= (bfd_vma
) -1;
2867 /* Make the backend pick a good value for a dynamic symbol. This is
2868 called via elf_link_hash_traverse, and also calls itself
2872 elf_adjust_dynamic_symbol (h
, data
)
2873 struct elf_link_hash_entry
*h
;
2876 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2878 struct elf_backend_data
*bed
;
2880 /* Ignore indirect symbols. These are added by the versioning code. */
2881 if (h
->root
.type
== bfd_link_hash_indirect
)
2884 /* Fix the symbol flags. */
2885 if (! elf_fix_symbol_flags (h
, eif
))
2888 /* If this symbol does not require a PLT entry, and it is not
2889 defined by a dynamic object, or is not referenced by a regular
2890 object, ignore it. We do have to handle a weak defined symbol,
2891 even if no regular object refers to it, if we decided to add it
2892 to the dynamic symbol table. FIXME: Do we normally need to worry
2893 about symbols which are defined by one dynamic object and
2894 referenced by another one? */
2895 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
2896 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
2897 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2898 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
2899 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
2901 h
->plt
.offset
= (bfd_vma
) -1;
2905 /* If we've already adjusted this symbol, don't do it again. This
2906 can happen via a recursive call. */
2907 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
2910 /* Don't look at this symbol again. Note that we must set this
2911 after checking the above conditions, because we may look at a
2912 symbol once, decide not to do anything, and then get called
2913 recursively later after REF_REGULAR is set below. */
2914 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
2916 /* If this is a weak definition, and we know a real definition, and
2917 the real symbol is not itself defined by a regular object file,
2918 then get a good value for the real definition. We handle the
2919 real symbol first, for the convenience of the backend routine.
2921 Note that there is a confusing case here. If the real definition
2922 is defined by a regular object file, we don't get the real symbol
2923 from the dynamic object, but we do get the weak symbol. If the
2924 processor backend uses a COPY reloc, then if some routine in the
2925 dynamic object changes the real symbol, we will not see that
2926 change in the corresponding weak symbol. This is the way other
2927 ELF linkers work as well, and seems to be a result of the shared
2930 I will clarify this issue. Most SVR4 shared libraries define the
2931 variable _timezone and define timezone as a weak synonym. The
2932 tzset call changes _timezone. If you write
2933 extern int timezone;
2935 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2936 you might expect that, since timezone is a synonym for _timezone,
2937 the same number will print both times. However, if the processor
2938 backend uses a COPY reloc, then actually timezone will be copied
2939 into your process image, and, since you define _timezone
2940 yourself, _timezone will not. Thus timezone and _timezone will
2941 wind up at different memory locations. The tzset call will set
2942 _timezone, leaving timezone unchanged. */
2944 if (h
->weakdef
!= NULL
)
2946 struct elf_link_hash_entry
*weakdef
;
2948 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2949 || h
->root
.type
== bfd_link_hash_defweak
);
2950 weakdef
= h
->weakdef
;
2951 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2952 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2953 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
2954 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2956 /* This symbol is defined by a regular object file, so we
2957 will not do anything special. Clear weakdef for the
2958 convenience of the processor backend. */
2963 /* There is an implicit reference by a regular object file
2964 via the weak symbol. */
2965 weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2966 if (! elf_adjust_dynamic_symbol (weakdef
, (PTR
) eif
))
2971 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2972 bed
= get_elf_backend_data (dynobj
);
2973 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2982 /* This routine is used to export all defined symbols into the dynamic
2983 symbol table. It is called via elf_link_hash_traverse. */
2986 elf_export_symbol (h
, data
)
2987 struct elf_link_hash_entry
*h
;
2990 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2992 /* Ignore indirect symbols. These are added by the versioning code. */
2993 if (h
->root
.type
== bfd_link_hash_indirect
)
2996 if (h
->dynindx
== -1
2997 && (h
->elf_link_hash_flags
2998 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
3000 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3010 /* Look through the symbols which are defined in other shared
3011 libraries and referenced here. Update the list of version
3012 dependencies. This will be put into the .gnu.version_r section.
3013 This function is called via elf_link_hash_traverse. */
3016 elf_link_find_version_dependencies (h
, data
)
3017 struct elf_link_hash_entry
*h
;
3020 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
3021 Elf_Internal_Verneed
*t
;
3022 Elf_Internal_Vernaux
*a
;
3024 /* We only care about symbols defined in shared objects with version
3026 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3027 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
3029 || h
->verinfo
.verdef
== NULL
)
3032 /* See if we already know about this version. */
3033 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
3035 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
3038 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3039 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
3045 /* This is a new version. Add it to tree we are building. */
3049 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *t
);
3052 rinfo
->failed
= true;
3056 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
3057 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
3058 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
3061 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *a
);
3063 /* Note that we are copying a string pointer here, and testing it
3064 above. If bfd_elf_string_from_elf_section is ever changed to
3065 discard the string data when low in memory, this will have to be
3067 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
3069 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
3070 a
->vna_nextptr
= t
->vn_auxptr
;
3072 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
3075 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
3082 /* Figure out appropriate versions for all the symbols. We may not
3083 have the version number script until we have read all of the input
3084 files, so until that point we don't know which symbols should be
3085 local. This function is called via elf_link_hash_traverse. */
3088 elf_link_assign_sym_version (h
, data
)
3089 struct elf_link_hash_entry
*h
;
3092 struct elf_assign_sym_version_info
*sinfo
=
3093 (struct elf_assign_sym_version_info
*) data
;
3094 struct bfd_link_info
*info
= sinfo
->info
;
3095 struct elf_info_failed eif
;
3098 /* Fix the symbol flags. */
3101 if (! elf_fix_symbol_flags (h
, &eif
))
3104 sinfo
->failed
= true;
3108 /* We only need version numbers for symbols defined in regular
3110 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
3113 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3114 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
3116 struct bfd_elf_version_tree
*t
;
3121 /* There are two consecutive ELF_VER_CHR characters if this is
3122 not a hidden symbol. */
3124 if (*p
== ELF_VER_CHR
)
3130 /* If there is no version string, we can just return out. */
3134 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
3138 /* Look for the version. If we find it, it is no longer weak. */
3139 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
3141 if (strcmp (t
->name
, p
) == 0)
3145 struct bfd_elf_version_expr
*d
;
3147 len
= p
- h
->root
.root
.string
;
3148 alc
= bfd_alloc (sinfo
->output_bfd
, len
);
3151 strncpy (alc
, h
->root
.root
.string
, len
- 1);
3152 alc
[len
- 1] = '\0';
3153 if (alc
[len
- 2] == ELF_VER_CHR
)
3154 alc
[len
- 2] = '\0';
3156 h
->verinfo
.vertree
= t
;
3160 if (t
->globals
!= NULL
)
3162 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3164 if ((d
->match
[0] == '*' && d
->match
[1] == '\0')
3165 || fnmatch (d
->match
, alc
, 0) == 0)
3170 /* See if there is anything to force this symbol to
3172 if (d
== NULL
&& t
->locals
!= NULL
)
3174 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3176 if ((d
->match
[0] == '*' && d
->match
[1] == '\0')
3177 || fnmatch (d
->match
, alc
, 0) == 0)
3179 if (h
->dynindx
!= -1
3181 && ! sinfo
->export_dynamic
)
3183 sinfo
->removed_dynamic
= true;
3184 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3185 h
->elf_link_hash_flags
&=~
3186 ELF_LINK_HASH_NEEDS_PLT
;
3188 h
->plt
.offset
= (bfd_vma
) -1;
3189 /* FIXME: The name of the symbol has
3190 already been recorded in the dynamic
3191 string table section. */
3199 bfd_release (sinfo
->output_bfd
, alc
);
3204 /* If we are building an application, we need to create a
3205 version node for this version. */
3206 if (t
== NULL
&& ! info
->shared
)
3208 struct bfd_elf_version_tree
**pp
;
3211 /* If we aren't going to export this symbol, we don't need
3212 to worry about it. */
3213 if (h
->dynindx
== -1)
3216 t
= ((struct bfd_elf_version_tree
*)
3217 bfd_alloc (sinfo
->output_bfd
, sizeof *t
));
3220 sinfo
->failed
= true;
3229 t
->name_indx
= (unsigned int) -1;
3233 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
3235 t
->vernum
= version_index
;
3239 h
->verinfo
.vertree
= t
;
3243 /* We could not find the version for a symbol when
3244 generating a shared archive. Return an error. */
3245 (*_bfd_error_handler
)
3246 (_("%s: undefined versioned symbol name %s"),
3247 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
3248 bfd_set_error (bfd_error_bad_value
);
3249 sinfo
->failed
= true;
3254 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
3257 /* If we don't have a version for this symbol, see if we can find
3259 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
3261 struct bfd_elf_version_tree
*t
;
3262 struct bfd_elf_version_tree
*deflt
;
3263 struct bfd_elf_version_expr
*d
;
3265 /* See if can find what version this symbol is in. If the
3266 symbol is supposed to be local, then don't actually register
3269 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
3271 if (t
->globals
!= NULL
)
3273 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3275 if (fnmatch (d
->match
, h
->root
.root
.string
, 0) == 0)
3277 h
->verinfo
.vertree
= t
;
3286 if (t
->locals
!= NULL
)
3288 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3290 if (d
->match
[0] == '*' && d
->match
[1] == '\0')
3292 else if (fnmatch (d
->match
, h
->root
.root
.string
, 0) == 0)
3294 h
->verinfo
.vertree
= t
;
3295 if (h
->dynindx
!= -1
3297 && ! sinfo
->export_dynamic
)
3299 sinfo
->removed_dynamic
= true;
3300 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3301 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
3303 h
->plt
.offset
= (bfd_vma
) -1;
3304 /* FIXME: The name of the symbol has already
3305 been recorded in the dynamic string table
3317 if (deflt
!= NULL
&& h
->verinfo
.vertree
== NULL
)
3319 h
->verinfo
.vertree
= deflt
;
3320 if (h
->dynindx
!= -1
3322 && ! sinfo
->export_dynamic
)
3324 sinfo
->removed_dynamic
= true;
3325 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3326 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
3328 h
->plt
.offset
= (bfd_vma
) -1;
3329 /* FIXME: The name of the symbol has already been
3330 recorded in the dynamic string table section. */
3338 /* This function is used to renumber the dynamic symbols, if some of
3339 them are removed because they are marked as local. This is called
3340 via elf_link_hash_traverse. */
3343 elf_link_renumber_dynsyms (h
, data
)
3344 struct elf_link_hash_entry
*h
;
3347 struct bfd_link_info
*info
= (struct bfd_link_info
*) data
;
3349 if (h
->dynindx
!= -1)
3351 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
3352 ++elf_hash_table (info
)->dynsymcount
;
3358 /* Final phase of ELF linker. */
3360 /* A structure we use to avoid passing large numbers of arguments. */
3362 struct elf_final_link_info
3364 /* General link information. */
3365 struct bfd_link_info
*info
;
3368 /* Symbol string table. */
3369 struct bfd_strtab_hash
*symstrtab
;
3370 /* .dynsym section. */
3371 asection
*dynsym_sec
;
3372 /* .hash section. */
3374 /* symbol version section (.gnu.version). */
3375 asection
*symver_sec
;
3376 /* Buffer large enough to hold contents of any section. */
3378 /* Buffer large enough to hold external relocs of any section. */
3379 PTR external_relocs
;
3380 /* Buffer large enough to hold internal relocs of any section. */
3381 Elf_Internal_Rela
*internal_relocs
;
3382 /* Buffer large enough to hold external local symbols of any input
3384 Elf_External_Sym
*external_syms
;
3385 /* Buffer large enough to hold internal local symbols of any input
3387 Elf_Internal_Sym
*internal_syms
;
3388 /* Array large enough to hold a symbol index for each local symbol
3389 of any input BFD. */
3391 /* Array large enough to hold a section pointer for each local
3392 symbol of any input BFD. */
3393 asection
**sections
;
3394 /* Buffer to hold swapped out symbols. */
3395 Elf_External_Sym
*symbuf
;
3396 /* Number of swapped out symbols in buffer. */
3397 size_t symbuf_count
;
3398 /* Number of symbols which fit in symbuf. */
3402 static boolean elf_link_output_sym
3403 PARAMS ((struct elf_final_link_info
*, const char *,
3404 Elf_Internal_Sym
*, asection
*));
3405 static boolean elf_link_flush_output_syms
3406 PARAMS ((struct elf_final_link_info
*));
3407 static boolean elf_link_output_extsym
3408 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3409 static boolean elf_link_input_bfd
3410 PARAMS ((struct elf_final_link_info
*, bfd
*));
3411 static boolean elf_reloc_link_order
3412 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
3413 struct bfd_link_order
*));
3415 /* This struct is used to pass information to elf_link_output_extsym. */
3417 struct elf_outext_info
3421 struct elf_final_link_info
*finfo
;
3424 /* Do the final step of an ELF link. */
3427 elf_bfd_final_link (abfd
, info
)
3429 struct bfd_link_info
*info
;
3433 struct elf_final_link_info finfo
;
3434 register asection
*o
;
3435 register struct bfd_link_order
*p
;
3437 size_t max_contents_size
;
3438 size_t max_external_reloc_size
;
3439 size_t max_internal_reloc_count
;
3440 size_t max_sym_count
;
3442 Elf_Internal_Sym elfsym
;
3444 Elf_Internal_Shdr
*symtab_hdr
;
3445 Elf_Internal_Shdr
*symstrtab_hdr
;
3446 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3447 struct elf_outext_info eoinfo
;
3450 abfd
->flags
|= DYNAMIC
;
3452 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
3453 dynobj
= elf_hash_table (info
)->dynobj
;
3456 finfo
.output_bfd
= abfd
;
3457 finfo
.symstrtab
= elf_stringtab_init ();
3458 if (finfo
.symstrtab
== NULL
)
3463 finfo
.dynsym_sec
= NULL
;
3464 finfo
.hash_sec
= NULL
;
3465 finfo
.symver_sec
= NULL
;
3469 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
3470 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
3471 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
3472 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3473 /* Note that it is OK if symver_sec is NULL. */
3476 finfo
.contents
= NULL
;
3477 finfo
.external_relocs
= NULL
;
3478 finfo
.internal_relocs
= NULL
;
3479 finfo
.external_syms
= NULL
;
3480 finfo
.internal_syms
= NULL
;
3481 finfo
.indices
= NULL
;
3482 finfo
.sections
= NULL
;
3483 finfo
.symbuf
= NULL
;
3484 finfo
.symbuf_count
= 0;
3486 /* Count up the number of relocations we will output for each output
3487 section, so that we know the sizes of the reloc sections. We
3488 also figure out some maximum sizes. */
3489 max_contents_size
= 0;
3490 max_external_reloc_size
= 0;
3491 max_internal_reloc_count
= 0;
3493 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3497 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3499 if (p
->type
== bfd_section_reloc_link_order
3500 || p
->type
== bfd_symbol_reloc_link_order
)
3502 else if (p
->type
== bfd_indirect_link_order
)
3506 sec
= p
->u
.indirect
.section
;
3508 /* Mark all sections which are to be included in the
3509 link. This will normally be every section. We need
3510 to do this so that we can identify any sections which
3511 the linker has decided to not include. */
3512 sec
->linker_mark
= true;
3514 if (info
->relocateable
)
3515 o
->reloc_count
+= sec
->reloc_count
;
3517 if (sec
->_raw_size
> max_contents_size
)
3518 max_contents_size
= sec
->_raw_size
;
3519 if (sec
->_cooked_size
> max_contents_size
)
3520 max_contents_size
= sec
->_cooked_size
;
3522 /* We are interested in just local symbols, not all
3524 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
3525 && (sec
->owner
->flags
& DYNAMIC
) == 0)
3529 if (elf_bad_symtab (sec
->owner
))
3530 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
3531 / sizeof (Elf_External_Sym
));
3533 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
3535 if (sym_count
> max_sym_count
)
3536 max_sym_count
= sym_count
;
3538 if ((sec
->flags
& SEC_RELOC
) != 0)
3542 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
3543 if (ext_size
> max_external_reloc_size
)
3544 max_external_reloc_size
= ext_size
;
3545 if (sec
->reloc_count
> max_internal_reloc_count
)
3546 max_internal_reloc_count
= sec
->reloc_count
;
3552 if (o
->reloc_count
> 0)
3553 o
->flags
|= SEC_RELOC
;
3556 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3557 set it (this is probably a bug) and if it is set
3558 assign_section_numbers will create a reloc section. */
3559 o
->flags
&=~ SEC_RELOC
;
3562 /* If the SEC_ALLOC flag is not set, force the section VMA to
3563 zero. This is done in elf_fake_sections as well, but forcing
3564 the VMA to 0 here will ensure that relocs against these
3565 sections are handled correctly. */
3566 if ((o
->flags
& SEC_ALLOC
) == 0
3567 && ! o
->user_set_vma
)
3571 /* Figure out the file positions for everything but the symbol table
3572 and the relocs. We set symcount to force assign_section_numbers
3573 to create a symbol table. */
3574 abfd
->symcount
= info
->strip
== strip_all
? 0 : 1;
3575 BFD_ASSERT (! abfd
->output_has_begun
);
3576 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
3579 /* That created the reloc sections. Set their sizes, and assign
3580 them file positions, and allocate some buffers. */
3581 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3583 if ((o
->flags
& SEC_RELOC
) != 0)
3585 Elf_Internal_Shdr
*rel_hdr
;
3586 register struct elf_link_hash_entry
**p
, **pend
;
3588 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
3590 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* o
->reloc_count
;
3592 /* The contents field must last into write_object_contents,
3593 so we allocate it with bfd_alloc rather than malloc. */
3594 rel_hdr
->contents
= (PTR
) bfd_alloc (abfd
, rel_hdr
->sh_size
);
3595 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
3598 p
= ((struct elf_link_hash_entry
**)
3599 bfd_malloc (o
->reloc_count
3600 * sizeof (struct elf_link_hash_entry
*)));
3601 if (p
== NULL
&& o
->reloc_count
!= 0)
3603 elf_section_data (o
)->rel_hashes
= p
;
3604 pend
= p
+ o
->reloc_count
;
3605 for (; p
< pend
; p
++)
3608 /* Use the reloc_count field as an index when outputting the
3614 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3616 /* We have now assigned file positions for all the sections except
3617 .symtab and .strtab. We start the .symtab section at the current
3618 file position, and write directly to it. We build the .strtab
3619 section in memory. */
3621 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3622 /* sh_name is set in prep_headers. */
3623 symtab_hdr
->sh_type
= SHT_SYMTAB
;
3624 symtab_hdr
->sh_flags
= 0;
3625 symtab_hdr
->sh_addr
= 0;
3626 symtab_hdr
->sh_size
= 0;
3627 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
3628 /* sh_link is set in assign_section_numbers. */
3629 /* sh_info is set below. */
3630 /* sh_offset is set just below. */
3631 symtab_hdr
->sh_addralign
= 4; /* FIXME: system dependent? */
3633 off
= elf_tdata (abfd
)->next_file_pos
;
3634 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
3636 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3637 incorrect. We do not yet know the size of the .symtab section.
3638 We correct next_file_pos below, after we do know the size. */
3640 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3641 continuously seeking to the right position in the file. */
3642 if (! info
->keep_memory
|| max_sym_count
< 20)
3643 finfo
.symbuf_size
= 20;
3645 finfo
.symbuf_size
= max_sym_count
;
3646 finfo
.symbuf
= ((Elf_External_Sym
*)
3647 bfd_malloc (finfo
.symbuf_size
* sizeof (Elf_External_Sym
)));
3648 if (finfo
.symbuf
== NULL
)
3651 /* Start writing out the symbol table. The first symbol is always a
3653 if (info
->strip
!= strip_all
|| info
->relocateable
)
3655 elfsym
.st_value
= 0;
3658 elfsym
.st_other
= 0;
3659 elfsym
.st_shndx
= SHN_UNDEF
;
3660 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
3661 &elfsym
, bfd_und_section_ptr
))
3666 /* Some standard ELF linkers do this, but we don't because it causes
3667 bootstrap comparison failures. */
3668 /* Output a file symbol for the output file as the second symbol.
3669 We output this even if we are discarding local symbols, although
3670 I'm not sure if this is correct. */
3671 elfsym
.st_value
= 0;
3673 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
3674 elfsym
.st_other
= 0;
3675 elfsym
.st_shndx
= SHN_ABS
;
3676 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
3677 &elfsym
, bfd_abs_section_ptr
))
3681 /* Output a symbol for each section. We output these even if we are
3682 discarding local symbols, since they are used for relocs. These
3683 symbols have no names. We store the index of each one in the
3684 index field of the section, so that we can find it again when
3685 outputting relocs. */
3686 if (info
->strip
!= strip_all
|| info
->relocateable
)
3689 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
3690 elfsym
.st_other
= 0;
3691 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
3693 o
= section_from_elf_index (abfd
, i
);
3695 o
->target_index
= abfd
->symcount
;
3696 elfsym
.st_shndx
= i
;
3697 if (info
->relocateable
|| o
== NULL
)
3698 elfsym
.st_value
= 0;
3700 elfsym
.st_value
= o
->vma
;
3701 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
3707 /* Allocate some memory to hold information read in from the input
3709 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
3710 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
3711 finfo
.internal_relocs
= ((Elf_Internal_Rela
*)
3712 bfd_malloc (max_internal_reloc_count
3713 * sizeof (Elf_Internal_Rela
)));
3714 finfo
.external_syms
= ((Elf_External_Sym
*)
3715 bfd_malloc (max_sym_count
3716 * sizeof (Elf_External_Sym
)));
3717 finfo
.internal_syms
= ((Elf_Internal_Sym
*)
3718 bfd_malloc (max_sym_count
3719 * sizeof (Elf_Internal_Sym
)));
3720 finfo
.indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
3721 finfo
.sections
= ((asection
**)
3722 bfd_malloc (max_sym_count
* sizeof (asection
*)));
3723 if ((finfo
.contents
== NULL
&& max_contents_size
!= 0)
3724 || (finfo
.external_relocs
== NULL
&& max_external_reloc_size
!= 0)
3725 || (finfo
.internal_relocs
== NULL
&& max_internal_reloc_count
!= 0)
3726 || (finfo
.external_syms
== NULL
&& max_sym_count
!= 0)
3727 || (finfo
.internal_syms
== NULL
&& max_sym_count
!= 0)
3728 || (finfo
.indices
== NULL
&& max_sym_count
!= 0)
3729 || (finfo
.sections
== NULL
&& max_sym_count
!= 0))
3732 /* Since ELF permits relocations to be against local symbols, we
3733 must have the local symbols available when we do the relocations.
3734 Since we would rather only read the local symbols once, and we
3735 would rather not keep them in memory, we handle all the
3736 relocations for a single input file at the same time.
3738 Unfortunately, there is no way to know the total number of local
3739 symbols until we have seen all of them, and the local symbol
3740 indices precede the global symbol indices. This means that when
3741 we are generating relocateable output, and we see a reloc against
3742 a global symbol, we can not know the symbol index until we have
3743 finished examining all the local symbols to see which ones we are
3744 going to output. To deal with this, we keep the relocations in
3745 memory, and don't output them until the end of the link. This is
3746 an unfortunate waste of memory, but I don't see a good way around
3747 it. Fortunately, it only happens when performing a relocateable
3748 link, which is not the common case. FIXME: If keep_memory is set
3749 we could write the relocs out and then read them again; I don't
3750 know how bad the memory loss will be. */
3752 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3753 sub
->output_has_begun
= false;
3754 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3756 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3758 if (p
->type
== bfd_indirect_link_order
3759 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
3760 == bfd_target_elf_flavour
))
3762 sub
= p
->u
.indirect
.section
->owner
;
3763 if (! sub
->output_has_begun
)
3765 if (! elf_link_input_bfd (&finfo
, sub
))
3767 sub
->output_has_begun
= true;
3770 else if (p
->type
== bfd_section_reloc_link_order
3771 || p
->type
== bfd_symbol_reloc_link_order
)
3773 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
3778 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
3784 /* That wrote out all the local symbols. Finish up the symbol table
3785 with the global symbols. */
3787 if (info
->strip
!= strip_all
&& info
->shared
)
3789 /* Output any global symbols that got converted to local in a
3790 version script. We do this in a separate step since ELF
3791 requires all local symbols to appear prior to any global
3792 symbols. FIXME: We should only do this if some global
3793 symbols were, in fact, converted to become local. FIXME:
3794 Will this work correctly with the Irix 5 linker? */
3795 eoinfo
.failed
= false;
3796 eoinfo
.finfo
= &finfo
;
3797 eoinfo
.localsyms
= true;
3798 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3804 /* The sh_info field records the index of the first non local
3806 symtab_hdr
->sh_info
= abfd
->symcount
;
3808 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
= 1;
3810 /* We get the global symbols from the hash table. */
3811 eoinfo
.failed
= false;
3812 eoinfo
.localsyms
= false;
3813 eoinfo
.finfo
= &finfo
;
3814 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3819 /* Flush all symbols to the file. */
3820 if (! elf_link_flush_output_syms (&finfo
))
3823 /* Now we know the size of the symtab section. */
3824 off
+= symtab_hdr
->sh_size
;
3826 /* Finish up and write out the symbol string table (.strtab)
3828 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3829 /* sh_name was set in prep_headers. */
3830 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
3831 symstrtab_hdr
->sh_flags
= 0;
3832 symstrtab_hdr
->sh_addr
= 0;
3833 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
3834 symstrtab_hdr
->sh_entsize
= 0;
3835 symstrtab_hdr
->sh_link
= 0;
3836 symstrtab_hdr
->sh_info
= 0;
3837 /* sh_offset is set just below. */
3838 symstrtab_hdr
->sh_addralign
= 1;
3840 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, true);
3841 elf_tdata (abfd
)->next_file_pos
= off
;
3843 if (abfd
->symcount
> 0)
3845 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
3846 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
3850 /* Adjust the relocs to have the correct symbol indices. */
3851 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3853 struct elf_link_hash_entry
**rel_hash
;
3854 Elf_Internal_Shdr
*rel_hdr
;
3856 if ((o
->flags
& SEC_RELOC
) == 0)
3859 rel_hash
= elf_section_data (o
)->rel_hashes
;
3860 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
3861 for (i
= 0; i
< o
->reloc_count
; i
++, rel_hash
++)
3863 if (*rel_hash
== NULL
)
3866 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
3868 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
3870 Elf_External_Rel
*erel
;
3871 Elf_Internal_Rel irel
;
3873 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
3874 elf_swap_reloc_in (abfd
, erel
, &irel
);
3875 irel
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
3876 ELF_R_TYPE (irel
.r_info
));
3877 elf_swap_reloc_out (abfd
, &irel
, erel
);
3881 Elf_External_Rela
*erela
;
3882 Elf_Internal_Rela irela
;
3884 BFD_ASSERT (rel_hdr
->sh_entsize
3885 == sizeof (Elf_External_Rela
));
3887 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
3888 elf_swap_reloca_in (abfd
, erela
, &irela
);
3889 irela
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
3890 ELF_R_TYPE (irela
.r_info
));
3891 elf_swap_reloca_out (abfd
, &irela
, erela
);
3895 /* Set the reloc_count field to 0 to prevent write_relocs from
3896 trying to swap the relocs out itself. */
3900 /* If we are linking against a dynamic object, or generating a
3901 shared library, finish up the dynamic linking information. */
3904 Elf_External_Dyn
*dyncon
, *dynconend
;
3906 /* Fix up .dynamic entries. */
3907 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
3908 BFD_ASSERT (o
!= NULL
);
3910 dyncon
= (Elf_External_Dyn
*) o
->contents
;
3911 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
3912 for (; dyncon
< dynconend
; dyncon
++)
3914 Elf_Internal_Dyn dyn
;
3918 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
3925 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on
3926 magic _init and _fini symbols. This is pretty ugly,
3927 but we are compatible. */
3935 struct elf_link_hash_entry
*h
;
3937 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
3938 false, false, true);
3940 && (h
->root
.type
== bfd_link_hash_defined
3941 || h
->root
.type
== bfd_link_hash_defweak
))
3943 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
3944 o
= h
->root
.u
.def
.section
;
3945 if (o
->output_section
!= NULL
)
3946 dyn
.d_un
.d_val
+= (o
->output_section
->vma
3947 + o
->output_offset
);
3950 /* The symbol is imported from another shared
3951 library and does not apply to this one. */
3955 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3970 name
= ".gnu.version_d";
3973 name
= ".gnu.version_r";
3976 name
= ".gnu.version";
3978 o
= bfd_get_section_by_name (abfd
, name
);
3979 BFD_ASSERT (o
!= NULL
);
3980 dyn
.d_un
.d_ptr
= o
->vma
;
3981 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3988 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
3993 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
3995 Elf_Internal_Shdr
*hdr
;
3997 hdr
= elf_elfsections (abfd
)[i
];
3998 if (hdr
->sh_type
== type
3999 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
4001 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
4002 dyn
.d_un
.d_val
+= hdr
->sh_size
;
4005 if (dyn
.d_un
.d_val
== 0
4006 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
4007 dyn
.d_un
.d_val
= hdr
->sh_addr
;
4011 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
4017 /* If we have created any dynamic sections, then output them. */
4020 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
4023 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4025 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4026 || o
->_raw_size
== 0)
4028 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
4030 /* At this point, we are only interested in sections
4031 created by elf_link_create_dynamic_sections. */
4034 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
4036 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
4038 if (! bfd_set_section_contents (abfd
, o
->output_section
,
4039 o
->contents
, o
->output_offset
,
4047 /* The contents of the .dynstr section are actually in a
4049 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
4050 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
4051 || ! _bfd_stringtab_emit (abfd
,
4052 elf_hash_table (info
)->dynstr
))
4058 /* If we have optimized stabs strings, output them. */
4059 if (elf_hash_table (info
)->stab_info
!= NULL
)
4061 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
4065 if (finfo
.symstrtab
!= NULL
)
4066 _bfd_stringtab_free (finfo
.symstrtab
);
4067 if (finfo
.contents
!= NULL
)
4068 free (finfo
.contents
);
4069 if (finfo
.external_relocs
!= NULL
)
4070 free (finfo
.external_relocs
);
4071 if (finfo
.internal_relocs
!= NULL
)
4072 free (finfo
.internal_relocs
);
4073 if (finfo
.external_syms
!= NULL
)
4074 free (finfo
.external_syms
);
4075 if (finfo
.internal_syms
!= NULL
)
4076 free (finfo
.internal_syms
);
4077 if (finfo
.indices
!= NULL
)
4078 free (finfo
.indices
);
4079 if (finfo
.sections
!= NULL
)
4080 free (finfo
.sections
);
4081 if (finfo
.symbuf
!= NULL
)
4082 free (finfo
.symbuf
);
4083 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4085 if ((o
->flags
& SEC_RELOC
) != 0
4086 && elf_section_data (o
)->rel_hashes
!= NULL
)
4087 free (elf_section_data (o
)->rel_hashes
);
4090 elf_tdata (abfd
)->linker
= true;
4095 if (finfo
.symstrtab
!= NULL
)
4096 _bfd_stringtab_free (finfo
.symstrtab
);
4097 if (finfo
.contents
!= NULL
)
4098 free (finfo
.contents
);
4099 if (finfo
.external_relocs
!= NULL
)
4100 free (finfo
.external_relocs
);
4101 if (finfo
.internal_relocs
!= NULL
)
4102 free (finfo
.internal_relocs
);
4103 if (finfo
.external_syms
!= NULL
)
4104 free (finfo
.external_syms
);
4105 if (finfo
.internal_syms
!= NULL
)
4106 free (finfo
.internal_syms
);
4107 if (finfo
.indices
!= NULL
)
4108 free (finfo
.indices
);
4109 if (finfo
.sections
!= NULL
)
4110 free (finfo
.sections
);
4111 if (finfo
.symbuf
!= NULL
)
4112 free (finfo
.symbuf
);
4113 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4115 if ((o
->flags
& SEC_RELOC
) != 0
4116 && elf_section_data (o
)->rel_hashes
!= NULL
)
4117 free (elf_section_data (o
)->rel_hashes
);
4123 /* Add a symbol to the output symbol table. */
4126 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
4127 struct elf_final_link_info
*finfo
;
4129 Elf_Internal_Sym
*elfsym
;
4130 asection
*input_sec
;
4132 boolean (*output_symbol_hook
) PARAMS ((bfd
*,
4133 struct bfd_link_info
*info
,
4138 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
4139 elf_backend_link_output_symbol_hook
;
4140 if (output_symbol_hook
!= NULL
)
4142 if (! ((*output_symbol_hook
)
4143 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
4147 if (name
== (const char *) NULL
|| *name
== '\0')
4148 elfsym
->st_name
= 0;
4151 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
4154 if (elfsym
->st_name
== (unsigned long) -1)
4158 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
4160 if (! elf_link_flush_output_syms (finfo
))
4164 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
,
4165 (PTR
) (finfo
->symbuf
+ finfo
->symbuf_count
));
4166 ++finfo
->symbuf_count
;
4168 ++finfo
->output_bfd
->symcount
;
4173 /* Flush the output symbols to the file. */
4176 elf_link_flush_output_syms (finfo
)
4177 struct elf_final_link_info
*finfo
;
4179 if (finfo
->symbuf_count
> 0)
4181 Elf_Internal_Shdr
*symtab
;
4183 symtab
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
4185 if (bfd_seek (finfo
->output_bfd
, symtab
->sh_offset
+ symtab
->sh_size
,
4187 || (bfd_write ((PTR
) finfo
->symbuf
, finfo
->symbuf_count
,
4188 sizeof (Elf_External_Sym
), finfo
->output_bfd
)
4189 != finfo
->symbuf_count
* sizeof (Elf_External_Sym
)))
4192 symtab
->sh_size
+= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
4194 finfo
->symbuf_count
= 0;
4200 /* Add an external symbol to the symbol table. This is called from
4201 the hash table traversal routine. When generating a shared object,
4202 we go through the symbol table twice. The first time we output
4203 anything that might have been forced to local scope in a version
4204 script. The second time we output the symbols that are still
4208 elf_link_output_extsym (h
, data
)
4209 struct elf_link_hash_entry
*h
;
4212 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
4213 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
4215 Elf_Internal_Sym sym
;
4216 asection
*input_sec
;
4218 /* Decide whether to output this symbol in this pass. */
4219 if (eoinfo
->localsyms
)
4221 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
4226 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4230 /* If we are not creating a shared library, and this symbol is
4231 referenced by a shared library but is not defined anywhere, then
4232 warn that it is undefined. If we do not do this, the runtime
4233 linker will complain that the symbol is undefined when the
4234 program is run. We don't have to worry about symbols that are
4235 referenced by regular files, because we will already have issued
4236 warnings for them. */
4237 if (! finfo
->info
->relocateable
4238 && ! finfo
->info
->shared
4239 && h
->root
.type
== bfd_link_hash_undefined
4240 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
4241 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
4243 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
4244 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
4245 (asection
*) NULL
, 0)))
4247 eoinfo
->failed
= true;
4252 /* We don't want to output symbols that have never been mentioned by
4253 a regular file, or that we have been told to strip. However, if
4254 h->indx is set to -2, the symbol is used by a reloc and we must
4258 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
4259 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
4260 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
4261 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
4263 else if (finfo
->info
->strip
== strip_all
4264 || (finfo
->info
->strip
== strip_some
4265 && bfd_hash_lookup (finfo
->info
->keep_hash
,
4266 h
->root
.root
.string
,
4267 false, false) == NULL
))
4272 /* If we're stripping it, and it's not a dynamic symbol, there's
4273 nothing else to do. */
4274 if (strip
&& h
->dynindx
== -1)
4278 sym
.st_size
= h
->size
;
4279 sym
.st_other
= h
->other
;
4280 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4281 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
4282 else if (h
->root
.type
== bfd_link_hash_undefweak
4283 || h
->root
.type
== bfd_link_hash_defweak
)
4284 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
4286 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
4288 switch (h
->root
.type
)
4291 case bfd_link_hash_new
:
4295 case bfd_link_hash_undefined
:
4296 input_sec
= bfd_und_section_ptr
;
4297 sym
.st_shndx
= SHN_UNDEF
;
4300 case bfd_link_hash_undefweak
:
4301 input_sec
= bfd_und_section_ptr
;
4302 sym
.st_shndx
= SHN_UNDEF
;
4305 case bfd_link_hash_defined
:
4306 case bfd_link_hash_defweak
:
4308 input_sec
= h
->root
.u
.def
.section
;
4309 if (input_sec
->output_section
!= NULL
)
4312 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
4313 input_sec
->output_section
);
4314 if (sym
.st_shndx
== (unsigned short) -1)
4316 (*_bfd_error_handler
)
4317 (_("%s: could not find output section %s for input section %s"),
4318 bfd_get_filename (finfo
->output_bfd
),
4319 input_sec
->output_section
->name
,
4321 eoinfo
->failed
= true;
4325 /* ELF symbols in relocateable files are section relative,
4326 but in nonrelocateable files they are virtual
4328 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
4329 if (! finfo
->info
->relocateable
)
4330 sym
.st_value
+= input_sec
->output_section
->vma
;
4334 BFD_ASSERT (input_sec
->owner
== NULL
4335 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
4336 sym
.st_shndx
= SHN_UNDEF
;
4337 input_sec
= bfd_und_section_ptr
;
4342 case bfd_link_hash_common
:
4343 input_sec
= h
->root
.u
.c
.p
->section
;
4344 sym
.st_shndx
= SHN_COMMON
;
4345 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
4348 case bfd_link_hash_indirect
:
4349 /* These symbols are created by symbol versioning. They point
4350 to the decorated version of the name. For example, if the
4351 symbol foo@@GNU_1.2 is the default, which should be used when
4352 foo is used with no version, then we add an indirect symbol
4353 foo which points to foo@@GNU_1.2. We ignore these symbols,
4354 since the indirected symbol is already in the hash table. If
4355 the indirect symbol is non-ELF, fall through and output it. */
4356 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) == 0)
4360 case bfd_link_hash_warning
:
4361 /* We can't represent these symbols in ELF, although a warning
4362 symbol may have come from a .gnu.warning.SYMBOL section. We
4363 just put the target symbol in the hash table. If the target
4364 symbol does not really exist, don't do anything. */
4365 if (h
->root
.u
.i
.link
->type
== bfd_link_hash_new
)
4367 return (elf_link_output_extsym
4368 ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
));
4371 /* Give the processor backend a chance to tweak the symbol value,
4372 and also to finish up anything that needs to be done for this
4374 if ((h
->dynindx
!= -1
4375 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4376 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4378 struct elf_backend_data
*bed
;
4380 bed
= get_elf_backend_data (finfo
->output_bfd
);
4381 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
4382 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
4384 eoinfo
->failed
= true;
4389 /* If this symbol should be put in the .dynsym section, then put it
4390 there now. We have already know the symbol index. We also fill
4391 in the entry in the .hash section. */
4392 if (h
->dynindx
!= -1
4393 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4399 bfd_byte
*bucketpos
;
4402 sym
.st_name
= h
->dynstr_index
;
4404 elf_swap_symbol_out (finfo
->output_bfd
, &sym
,
4405 (PTR
) (((Elf_External_Sym
*)
4406 finfo
->dynsym_sec
->contents
)
4409 /* We didn't include the version string in the dynamic string
4410 table, so we must not consider it in the hash table. */
4411 name
= h
->root
.root
.string
;
4412 p
= strchr (name
, ELF_VER_CHR
);
4417 copy
= bfd_alloc (finfo
->output_bfd
, p
- name
+ 1);
4418 strncpy (copy
, name
, p
- name
);
4419 copy
[p
- name
] = '\0';
4423 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
4424 bucket
= bfd_elf_hash ((const unsigned char *) name
) % bucketcount
;
4425 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
4426 + (bucket
+ 2) * (ARCH_SIZE
/ 8));
4427 chain
= get_word (finfo
->output_bfd
, bucketpos
);
4428 put_word (finfo
->output_bfd
, h
->dynindx
, bucketpos
);
4429 put_word (finfo
->output_bfd
, chain
,
4430 ((bfd_byte
*) finfo
->hash_sec
->contents
4431 + (bucketcount
+ 2 + h
->dynindx
) * (ARCH_SIZE
/ 8)));
4434 bfd_release (finfo
->output_bfd
, copy
);
4436 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
4438 Elf_Internal_Versym iversym
;
4440 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4442 if (h
->verinfo
.verdef
== NULL
)
4443 iversym
.vs_vers
= 0;
4445 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4449 if (h
->verinfo
.vertree
== NULL
)
4450 iversym
.vs_vers
= 1;
4452 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
4455 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
4456 iversym
.vs_vers
|= VERSYM_HIDDEN
;
4458 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
,
4459 (((Elf_External_Versym
*)
4460 finfo
->symver_sec
->contents
)
4465 /* If we're stripping it, then it was just a dynamic symbol, and
4466 there's nothing else to do. */
4470 h
->indx
= finfo
->output_bfd
->symcount
;
4472 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
4474 eoinfo
->failed
= true;
4481 /* Link an input file into the linker output file. This function
4482 handles all the sections and relocations of the input file at once.
4483 This is so that we only have to read the local symbols once, and
4484 don't have to keep them in memory. */
4487 elf_link_input_bfd (finfo
, input_bfd
)
4488 struct elf_final_link_info
*finfo
;
4491 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
4492 bfd
*, asection
*, bfd_byte
*,
4493 Elf_Internal_Rela
*,
4494 Elf_Internal_Sym
*, asection
**));
4496 Elf_Internal_Shdr
*symtab_hdr
;
4499 Elf_External_Sym
*external_syms
;
4500 Elf_External_Sym
*esym
;
4501 Elf_External_Sym
*esymend
;
4502 Elf_Internal_Sym
*isym
;
4504 asection
**ppsection
;
4507 output_bfd
= finfo
->output_bfd
;
4509 get_elf_backend_data (output_bfd
)->elf_backend_relocate_section
;
4511 /* If this is a dynamic object, we don't want to do anything here:
4512 we don't want the local symbols, and we don't want the section
4514 if ((input_bfd
->flags
& DYNAMIC
) != 0)
4517 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
4518 if (elf_bad_symtab (input_bfd
))
4520 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
4525 locsymcount
= symtab_hdr
->sh_info
;
4526 extsymoff
= symtab_hdr
->sh_info
;
4529 /* Read the local symbols. */
4530 if (symtab_hdr
->contents
!= NULL
)
4531 external_syms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
4532 else if (locsymcount
== 0)
4533 external_syms
= NULL
;
4536 external_syms
= finfo
->external_syms
;
4537 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
4538 || (bfd_read (external_syms
, sizeof (Elf_External_Sym
),
4539 locsymcount
, input_bfd
)
4540 != locsymcount
* sizeof (Elf_External_Sym
)))
4544 /* Swap in the local symbols and write out the ones which we know
4545 are going into the output file. */
4546 esym
= external_syms
;
4547 esymend
= esym
+ locsymcount
;
4548 isym
= finfo
->internal_syms
;
4549 pindex
= finfo
->indices
;
4550 ppsection
= finfo
->sections
;
4551 for (; esym
< esymend
; esym
++, isym
++, pindex
++, ppsection
++)
4555 Elf_Internal_Sym osym
;
4557 elf_swap_symbol_in (input_bfd
, esym
, isym
);
4560 if (elf_bad_symtab (input_bfd
))
4562 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
4569 if (isym
->st_shndx
== SHN_UNDEF
)
4570 isec
= bfd_und_section_ptr
;
4571 else if (isym
->st_shndx
> 0 && isym
->st_shndx
< SHN_LORESERVE
)
4572 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
4573 else if (isym
->st_shndx
== SHN_ABS
)
4574 isec
= bfd_abs_section_ptr
;
4575 else if (isym
->st_shndx
== SHN_COMMON
)
4576 isec
= bfd_com_section_ptr
;
4585 /* Don't output the first, undefined, symbol. */
4586 if (esym
== external_syms
)
4589 /* If we are stripping all symbols, we don't want to output this
4591 if (finfo
->info
->strip
== strip_all
)
4594 /* We never output section symbols. Instead, we use the section
4595 symbol of the corresponding section in the output file. */
4596 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
4599 /* If we are discarding all local symbols, we don't want to
4600 output this one. If we are generating a relocateable output
4601 file, then some of the local symbols may be required by
4602 relocs; we output them below as we discover that they are
4604 if (finfo
->info
->discard
== discard_all
)
4607 /* If this symbol is defined in a section which we are
4608 discarding, we don't need to keep it, but note that
4609 linker_mark is only reliable for sections that have contents.
4610 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4611 as well as linker_mark. */
4612 if (isym
->st_shndx
> 0
4613 && isym
->st_shndx
< SHN_LORESERVE
4615 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
4616 || (! finfo
->info
->relocateable
4617 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
4620 /* Get the name of the symbol. */
4621 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
4626 /* See if we are discarding symbols with this name. */
4627 if ((finfo
->info
->strip
== strip_some
4628 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
4630 || (finfo
->info
->discard
== discard_l
4631 && bfd_is_local_label_name (input_bfd
, name
)))
4634 /* If we get here, we are going to output this symbol. */
4638 /* Adjust the section index for the output file. */
4639 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
4640 isec
->output_section
);
4641 if (osym
.st_shndx
== (unsigned short) -1)
4644 *pindex
= output_bfd
->symcount
;
4646 /* ELF symbols in relocateable files are section relative, but
4647 in executable files they are virtual addresses. Note that
4648 this code assumes that all ELF sections have an associated
4649 BFD section with a reasonable value for output_offset; below
4650 we assume that they also have a reasonable value for
4651 output_section. Any special sections must be set up to meet
4652 these requirements. */
4653 osym
.st_value
+= isec
->output_offset
;
4654 if (! finfo
->info
->relocateable
)
4655 osym
.st_value
+= isec
->output_section
->vma
;
4657 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
4661 /* Relocate the contents of each section. */
4662 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4666 if (! o
->linker_mark
)
4668 /* This section was omitted from the link. */
4672 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4673 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
4676 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
4678 /* Section was created by elf_link_create_dynamic_sections
4683 /* Get the contents of the section. They have been cached by a
4684 relaxation routine. Note that o is a section in an input
4685 file, so the contents field will not have been set by any of
4686 the routines which work on output files. */
4687 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
4688 contents
= elf_section_data (o
)->this_hdr
.contents
;
4691 contents
= finfo
->contents
;
4692 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
4693 (file_ptr
) 0, o
->_raw_size
))
4697 if ((o
->flags
& SEC_RELOC
) != 0)
4699 Elf_Internal_Rela
*internal_relocs
;
4701 /* Get the swapped relocs. */
4702 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
4703 (input_bfd
, o
, finfo
->external_relocs
,
4704 finfo
->internal_relocs
, false));
4705 if (internal_relocs
== NULL
4706 && o
->reloc_count
> 0)
4709 /* Relocate the section by invoking a back end routine.
4711 The back end routine is responsible for adjusting the
4712 section contents as necessary, and (if using Rela relocs
4713 and generating a relocateable output file) adjusting the
4714 reloc addend as necessary.
4716 The back end routine does not have to worry about setting
4717 the reloc address or the reloc symbol index.
4719 The back end routine is given a pointer to the swapped in
4720 internal symbols, and can access the hash table entries
4721 for the external symbols via elf_sym_hashes (input_bfd).
4723 When generating relocateable output, the back end routine
4724 must handle STB_LOCAL/STT_SECTION symbols specially. The
4725 output symbol is going to be a section symbol
4726 corresponding to the output section, which will require
4727 the addend to be adjusted. */
4729 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
4730 input_bfd
, o
, contents
,
4732 finfo
->internal_syms
,
4736 if (finfo
->info
->relocateable
)
4738 Elf_Internal_Rela
*irela
;
4739 Elf_Internal_Rela
*irelaend
;
4740 struct elf_link_hash_entry
**rel_hash
;
4741 Elf_Internal_Shdr
*input_rel_hdr
;
4742 Elf_Internal_Shdr
*output_rel_hdr
;
4744 /* Adjust the reloc addresses and symbol indices. */
4746 irela
= internal_relocs
;
4747 irelaend
= irela
+ o
->reloc_count
;
4748 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
4749 + o
->output_section
->reloc_count
);
4750 for (; irela
< irelaend
; irela
++, rel_hash
++)
4752 unsigned long r_symndx
;
4753 Elf_Internal_Sym
*isym
;
4756 irela
->r_offset
+= o
->output_offset
;
4758 r_symndx
= ELF_R_SYM (irela
->r_info
);
4763 if (r_symndx
>= locsymcount
4764 || (elf_bad_symtab (input_bfd
)
4765 && finfo
->sections
[r_symndx
] == NULL
))
4767 struct elf_link_hash_entry
*rh
;
4770 /* This is a reloc against a global symbol. We
4771 have not yet output all the local symbols, so
4772 we do not know the symbol index of any global
4773 symbol. We set the rel_hash entry for this
4774 reloc to point to the global hash table entry
4775 for this symbol. The symbol index is then
4776 set at the end of elf_bfd_final_link. */
4777 indx
= r_symndx
- extsymoff
;
4778 rh
= elf_sym_hashes (input_bfd
)[indx
];
4779 while (rh
->root
.type
== bfd_link_hash_indirect
4780 || rh
->root
.type
== bfd_link_hash_warning
)
4781 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
4783 /* Setting the index to -2 tells
4784 elf_link_output_extsym that this symbol is
4786 BFD_ASSERT (rh
->indx
< 0);
4794 /* This is a reloc against a local symbol. */
4797 isym
= finfo
->internal_syms
+ r_symndx
;
4798 sec
= finfo
->sections
[r_symndx
];
4799 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
4801 /* I suppose the backend ought to fill in the
4802 section of any STT_SECTION symbol against a
4803 processor specific section. If we have
4804 discarded a section, the output_section will
4805 be the absolute section. */
4807 && (bfd_is_abs_section (sec
)
4808 || (sec
->output_section
!= NULL
4809 && bfd_is_abs_section (sec
->output_section
))))
4811 else if (sec
== NULL
|| sec
->owner
== NULL
)
4813 bfd_set_error (bfd_error_bad_value
);
4818 r_symndx
= sec
->output_section
->target_index
;
4819 BFD_ASSERT (r_symndx
!= 0);
4824 if (finfo
->indices
[r_symndx
] == -1)
4830 if (finfo
->info
->strip
== strip_all
)
4832 /* You can't do ld -r -s. */
4833 bfd_set_error (bfd_error_invalid_operation
);
4837 /* This symbol was skipped earlier, but
4838 since it is needed by a reloc, we
4839 must output it now. */
4840 link
= symtab_hdr
->sh_link
;
4841 name
= bfd_elf_string_from_elf_section (input_bfd
,
4847 osec
= sec
->output_section
;
4849 _bfd_elf_section_from_bfd_section (output_bfd
,
4851 if (isym
->st_shndx
== (unsigned short) -1)
4854 isym
->st_value
+= sec
->output_offset
;
4855 if (! finfo
->info
->relocateable
)
4856 isym
->st_value
+= osec
->vma
;
4858 finfo
->indices
[r_symndx
] = output_bfd
->symcount
;
4860 if (! elf_link_output_sym (finfo
, name
, isym
, sec
))
4864 r_symndx
= finfo
->indices
[r_symndx
];
4867 irela
->r_info
= ELF_R_INFO (r_symndx
,
4868 ELF_R_TYPE (irela
->r_info
));
4871 /* Swap out the relocs. */
4872 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
4873 output_rel_hdr
= &elf_section_data (o
->output_section
)->rel_hdr
;
4874 BFD_ASSERT (output_rel_hdr
->sh_entsize
4875 == input_rel_hdr
->sh_entsize
);
4876 irela
= internal_relocs
;
4877 irelaend
= irela
+ o
->reloc_count
;
4878 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4880 Elf_External_Rel
*erel
;
4882 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
4883 + o
->output_section
->reloc_count
);
4884 for (; irela
< irelaend
; irela
++, erel
++)
4886 Elf_Internal_Rel irel
;
4888 irel
.r_offset
= irela
->r_offset
;
4889 irel
.r_info
= irela
->r_info
;
4890 BFD_ASSERT (irela
->r_addend
== 0);
4891 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
4896 Elf_External_Rela
*erela
;
4898 BFD_ASSERT (input_rel_hdr
->sh_entsize
4899 == sizeof (Elf_External_Rela
));
4900 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
4901 + o
->output_section
->reloc_count
);
4902 for (; irela
< irelaend
; irela
++, erela
++)
4903 elf_swap_reloca_out (output_bfd
, irela
, erela
);
4906 o
->output_section
->reloc_count
+= o
->reloc_count
;
4910 /* Write out the modified section contents. */
4911 if (elf_section_data (o
)->stab_info
== NULL
)
4913 if (! (o
->flags
& SEC_EXCLUDE
) &&
4914 ! bfd_set_section_contents (output_bfd
, o
->output_section
,
4915 contents
, o
->output_offset
,
4916 (o
->_cooked_size
!= 0
4923 if (! (_bfd_write_section_stabs
4924 (output_bfd
, &elf_hash_table (finfo
->info
)->stab_info
,
4925 o
, &elf_section_data (o
)->stab_info
, contents
)))
4933 /* Generate a reloc when linking an ELF file. This is a reloc
4934 requested by the linker, and does come from any input file. This
4935 is used to build constructor and destructor tables when linking
4939 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
4941 struct bfd_link_info
*info
;
4942 asection
*output_section
;
4943 struct bfd_link_order
*link_order
;
4945 reloc_howto_type
*howto
;
4949 struct elf_link_hash_entry
**rel_hash_ptr
;
4950 Elf_Internal_Shdr
*rel_hdr
;
4952 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
4955 bfd_set_error (bfd_error_bad_value
);
4959 addend
= link_order
->u
.reloc
.p
->addend
;
4961 /* Figure out the symbol index. */
4962 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
4963 + output_section
->reloc_count
);
4964 if (link_order
->type
== bfd_section_reloc_link_order
)
4966 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
4967 BFD_ASSERT (indx
!= 0);
4968 *rel_hash_ptr
= NULL
;
4972 struct elf_link_hash_entry
*h
;
4974 /* Treat a reloc against a defined symbol as though it were
4975 actually against the section. */
4976 h
= ((struct elf_link_hash_entry
*)
4977 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
4978 link_order
->u
.reloc
.p
->u
.name
,
4979 false, false, true));
4981 && (h
->root
.type
== bfd_link_hash_defined
4982 || h
->root
.type
== bfd_link_hash_defweak
))
4986 section
= h
->root
.u
.def
.section
;
4987 indx
= section
->output_section
->target_index
;
4988 *rel_hash_ptr
= NULL
;
4989 /* It seems that we ought to add the symbol value to the
4990 addend here, but in practice it has already been added
4991 because it was passed to constructor_callback. */
4992 addend
+= section
->output_section
->vma
+ section
->output_offset
;
4996 /* Setting the index to -2 tells elf_link_output_extsym that
4997 this symbol is used by a reloc. */
5004 if (! ((*info
->callbacks
->unattached_reloc
)
5005 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
5006 (asection
*) NULL
, (bfd_vma
) 0)))
5012 /* If this is an inplace reloc, we must write the addend into the
5014 if (howto
->partial_inplace
&& addend
!= 0)
5017 bfd_reloc_status_type rstat
;
5021 size
= bfd_get_reloc_size (howto
);
5022 buf
= (bfd_byte
*) bfd_zmalloc (size
);
5023 if (buf
== (bfd_byte
*) NULL
)
5025 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5031 case bfd_reloc_outofrange
:
5033 case bfd_reloc_overflow
:
5034 if (! ((*info
->callbacks
->reloc_overflow
)
5036 (link_order
->type
== bfd_section_reloc_link_order
5037 ? bfd_section_name (output_bfd
,
5038 link_order
->u
.reloc
.p
->u
.section
)
5039 : link_order
->u
.reloc
.p
->u
.name
),
5040 howto
->name
, addend
, (bfd
*) NULL
, (asection
*) NULL
,
5048 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
5049 (file_ptr
) link_order
->offset
, size
);
5055 /* The address of a reloc is relative to the section in a
5056 relocateable file, and is a virtual address in an executable
5058 offset
= link_order
->offset
;
5059 if (! info
->relocateable
)
5060 offset
+= output_section
->vma
;
5062 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
5064 if (rel_hdr
->sh_type
== SHT_REL
)
5066 Elf_Internal_Rel irel
;
5067 Elf_External_Rel
*erel
;
5069 irel
.r_offset
= offset
;
5070 irel
.r_info
= ELF_R_INFO (indx
, howto
->type
);
5071 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
5072 + output_section
->reloc_count
);
5073 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
5077 Elf_Internal_Rela irela
;
5078 Elf_External_Rela
*erela
;
5080 irela
.r_offset
= offset
;
5081 irela
.r_info
= ELF_R_INFO (indx
, howto
->type
);
5082 irela
.r_addend
= addend
;
5083 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
5084 + output_section
->reloc_count
);
5085 elf_swap_reloca_out (output_bfd
, &irela
, erela
);
5088 ++output_section
->reloc_count
;
5094 /* Allocate a pointer to live in a linker created section. */
5097 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
5099 struct bfd_link_info
*info
;
5100 elf_linker_section_t
*lsect
;
5101 struct elf_link_hash_entry
*h
;
5102 const Elf_Internal_Rela
*rel
;
5104 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
5105 elf_linker_section_pointers_t
*linker_section_ptr
;
5106 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);;
5108 BFD_ASSERT (lsect
!= NULL
);
5110 /* Is this a global symbol? */
5113 /* Has this symbol already been allocated, if so, our work is done */
5114 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
5119 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
5120 /* Make sure this symbol is output as a dynamic symbol. */
5121 if (h
->dynindx
== -1)
5123 if (! elf_link_record_dynamic_symbol (info
, h
))
5127 if (lsect
->rel_section
)
5128 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
5131 else /* Allocation of a pointer to a local symbol */
5133 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
5135 /* Allocate a table to hold the local symbols if first time */
5138 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
5139 register unsigned int i
;
5141 ptr
= (elf_linker_section_pointers_t
**)
5142 bfd_alloc (abfd
, num_symbols
* sizeof (elf_linker_section_pointers_t
*));
5147 elf_local_ptr_offsets (abfd
) = ptr
;
5148 for (i
= 0; i
< num_symbols
; i
++)
5149 ptr
[i
] = (elf_linker_section_pointers_t
*)0;
5152 /* Has this symbol already been allocated, if so, our work is done */
5153 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
5158 ptr_linker_section_ptr
= &ptr
[r_symndx
];
5162 /* If we are generating a shared object, we need to
5163 output a R_<xxx>_RELATIVE reloc so that the
5164 dynamic linker can adjust this GOT entry. */
5165 BFD_ASSERT (lsect
->rel_section
!= NULL
);
5166 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
5170 /* Allocate space for a pointer in the linker section, and allocate a new pointer record
5171 from internal memory. */
5172 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
5173 linker_section_ptr
= (elf_linker_section_pointers_t
*)
5174 bfd_alloc (abfd
, sizeof (elf_linker_section_pointers_t
));
5176 if (!linker_section_ptr
)
5179 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
5180 linker_section_ptr
->addend
= rel
->r_addend
;
5181 linker_section_ptr
->which
= lsect
->which
;
5182 linker_section_ptr
->written_address_p
= false;
5183 *ptr_linker_section_ptr
= linker_section_ptr
;
5186 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
5188 linker_section_ptr
->offset
= lsect
->section
->_raw_size
- lsect
->hole_size
+ (ARCH_SIZE
/ 8);
5189 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
5190 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
5191 if (lsect
->sym_hash
) /* Bump up symbol value if needed */
5193 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
5195 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
5196 lsect
->sym_hash
->root
.root
.string
,
5197 (long)ARCH_SIZE
/ 8,
5198 (long)lsect
->sym_hash
->root
.u
.def
.value
);
5204 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
5206 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
5209 fprintf (stderr
, "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
5210 lsect
->name
, (long)linker_section_ptr
->offset
, (long)lsect
->section
->_raw_size
);
5218 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
5221 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
5224 /* Fill in the address for a pointer generated in alinker section. */
5227 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
, relocation
, rel
, relative_reloc
)
5230 struct bfd_link_info
*info
;
5231 elf_linker_section_t
*lsect
;
5232 struct elf_link_hash_entry
*h
;
5234 const Elf_Internal_Rela
*rel
;
5237 elf_linker_section_pointers_t
*linker_section_ptr
;
5239 BFD_ASSERT (lsect
!= NULL
);
5241 if (h
!= NULL
) /* global symbol */
5243 linker_section_ptr
= _bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
5247 BFD_ASSERT (linker_section_ptr
!= NULL
);
5249 if (! elf_hash_table (info
)->dynamic_sections_created
5252 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
5254 /* This is actually a static link, or it is a
5255 -Bsymbolic link and the symbol is defined
5256 locally. We must initialize this entry in the
5259 When doing a dynamic link, we create a .rela.<xxx>
5260 relocation entry to initialize the value. This
5261 is done in the finish_dynamic_symbol routine. */
5262 if (!linker_section_ptr
->written_address_p
)
5264 linker_section_ptr
->written_address_p
= true;
5265 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
5266 lsect
->section
->contents
+ linker_section_ptr
->offset
);
5270 else /* local symbol */
5272 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
5273 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
5274 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
5275 linker_section_ptr
= _bfd_elf_find_pointer_linker_section (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
5279 BFD_ASSERT (linker_section_ptr
!= NULL
);
5281 /* Write out pointer if it hasn't been rewritten out before */
5282 if (!linker_section_ptr
->written_address_p
)
5284 linker_section_ptr
->written_address_p
= true;
5285 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
5286 lsect
->section
->contents
+ linker_section_ptr
->offset
);
5290 asection
*srel
= lsect
->rel_section
;
5291 Elf_Internal_Rela outrel
;
5293 /* We need to generate a relative reloc for the dynamic linker. */
5295 lsect
->rel_section
= srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
5298 BFD_ASSERT (srel
!= NULL
);
5300 outrel
.r_offset
= (lsect
->section
->output_section
->vma
5301 + lsect
->section
->output_offset
5302 + linker_section_ptr
->offset
);
5303 outrel
.r_info
= ELF_R_INFO (0, relative_reloc
);
5304 outrel
.r_addend
= 0;
5305 elf_swap_reloca_out (output_bfd
, &outrel
,
5306 (((Elf_External_Rela
*)
5307 lsect
->section
->contents
)
5308 + lsect
->section
->reloc_count
));
5309 ++lsect
->section
->reloc_count
;
5314 relocation
= (lsect
->section
->output_offset
5315 + linker_section_ptr
->offset
5316 - lsect
->hole_offset
5317 - lsect
->sym_offset
);
5320 fprintf (stderr
, "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
5321 lsect
->name
, (long)relocation
, (long)relocation
);
5324 /* Subtract out the addend, because it will get added back in by the normal
5326 return relocation
- linker_section_ptr
->addend
;
5329 /* Garbage collect unused sections. */
5331 static boolean elf_gc_mark
5332 PARAMS ((struct bfd_link_info
*info
, asection
*sec
,
5333 asection
* (*gc_mark_hook
)
5334 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
5335 struct elf_link_hash_entry
*, Elf_Internal_Sym
*))));
5337 static boolean elf_gc_sweep
5338 PARAMS ((struct bfd_link_info
*info
,
5339 boolean (*gc_sweep_hook
)
5340 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
5341 const Elf_Internal_Rela
*relocs
))));
5343 static boolean elf_gc_sweep_symbol
5344 PARAMS ((struct elf_link_hash_entry
*h
, PTR idxptr
));
5346 static boolean elf_gc_allocate_got_offsets
5347 PARAMS ((struct elf_link_hash_entry
*h
, PTR offarg
));
5349 static boolean elf_gc_propagate_vtable_entries_used
5350 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
5352 static boolean elf_gc_smash_unused_vtentry_relocs
5353 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
5355 /* The mark phase of garbage collection. For a given section, mark
5356 it, and all the sections which define symbols to which it refers. */
5359 elf_gc_mark (info
, sec
, gc_mark_hook
)
5360 struct bfd_link_info
*info
;
5362 asection
* (*gc_mark_hook
)
5363 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
5364 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
5370 /* Look through the section relocs. */
5372 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
5374 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
5375 Elf_Internal_Shdr
*symtab_hdr
;
5376 struct elf_link_hash_entry
**sym_hashes
;
5379 Elf_External_Sym
*locsyms
, *freesyms
= NULL
;
5380 bfd
*input_bfd
= sec
->owner
;
5382 /* GCFIXME: how to arrange so that relocs and symbols are not
5383 reread continually? */
5385 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
5386 sym_hashes
= elf_sym_hashes (input_bfd
);
5388 /* Read the local symbols. */
5389 if (elf_bad_symtab (input_bfd
))
5391 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
5395 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
5396 if (symtab_hdr
->contents
)
5397 locsyms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
5398 else if (nlocsyms
== 0)
5402 locsyms
= freesyms
=
5403 bfd_malloc (nlocsyms
* sizeof (Elf_External_Sym
));
5404 if (freesyms
== NULL
5405 || bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
5406 || (bfd_read (locsyms
, sizeof (Elf_External_Sym
),
5407 nlocsyms
, input_bfd
)
5408 != nlocsyms
* sizeof (Elf_External_Sym
)))
5415 /* Read the relocations. */
5416 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
5417 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
5418 info
->keep_memory
));
5419 if (relstart
== NULL
)
5424 relend
= relstart
+ sec
->reloc_count
;
5426 for (rel
= relstart
; rel
< relend
; rel
++)
5428 unsigned long r_symndx
;
5430 struct elf_link_hash_entry
*h
;
5433 r_symndx
= ELF_R_SYM (rel
->r_info
);
5437 if (elf_bad_symtab (sec
->owner
))
5439 elf_swap_symbol_in (input_bfd
, &locsyms
[r_symndx
], &s
);
5440 if (ELF_ST_BIND (s
.st_info
) == STB_LOCAL
)
5441 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, NULL
, &s
);
5444 h
= sym_hashes
[r_symndx
- extsymoff
];
5445 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, h
, NULL
);
5448 else if (r_symndx
>= nlocsyms
)
5450 h
= sym_hashes
[r_symndx
- extsymoff
];
5451 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, h
, NULL
);
5455 elf_swap_symbol_in (input_bfd
, &locsyms
[r_symndx
], &s
);
5456 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, NULL
, &s
);
5459 if (rsec
&& !rsec
->gc_mark
)
5460 if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
5468 if (!info
->keep_memory
)
5478 /* The sweep phase of garbage collection. Remove all garbage sections. */
5481 elf_gc_sweep (info
, gc_sweep_hook
)
5482 struct bfd_link_info
*info
;
5483 boolean (*gc_sweep_hook
)
5484 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
5485 const Elf_Internal_Rela
*relocs
));
5489 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5493 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5495 /* Keep special sections. Keep .debug sections. */
5496 if ((o
->flags
& SEC_LINKER_CREATED
)
5497 || (o
->flags
& SEC_DEBUGGING
))
5503 /* Skip sweeping sections already excluded. */
5504 if (o
->flags
& SEC_EXCLUDE
)
5507 /* Since this is early in the link process, it is simple
5508 to remove a section from the output. */
5509 o
->flags
|= SEC_EXCLUDE
;
5511 /* But we also have to update some of the relocation
5512 info we collected before. */
5514 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
5516 Elf_Internal_Rela
*internal_relocs
;
5519 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
5520 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
5521 if (internal_relocs
== NULL
)
5524 r
= (*gc_sweep_hook
)(o
->owner
, info
, o
, internal_relocs
);
5526 if (!info
->keep_memory
)
5527 free (internal_relocs
);
5535 /* Remove the symbols that were in the swept sections from the dynamic
5536 symbol table. GCFIXME: Anyone know how to get them out of the
5537 static symbol table as well? */
5541 elf_link_hash_traverse (elf_hash_table (info
),
5542 elf_gc_sweep_symbol
,
5545 elf_hash_table (info
)->dynsymcount
= i
;
5551 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5554 elf_gc_sweep_symbol (h
, idxptr
)
5555 struct elf_link_hash_entry
*h
;
5558 int *idx
= (int *) idxptr
;
5560 if (h
->dynindx
!= -1
5561 && ((h
->root
.type
!= bfd_link_hash_defined
5562 && h
->root
.type
!= bfd_link_hash_defweak
)
5563 || h
->root
.u
.def
.section
->gc_mark
))
5564 h
->dynindx
= (*idx
)++;
5569 /* Propogate collected vtable information. This is called through
5570 elf_link_hash_traverse. */
5573 elf_gc_propagate_vtable_entries_used (h
, okp
)
5574 struct elf_link_hash_entry
*h
;
5577 /* Those that are not vtables. */
5578 if (h
->vtable_parent
== NULL
)
5581 /* Those vtables that do not have parents, we cannot merge. */
5582 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
5585 /* If we've already been done, exit. */
5586 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
5589 /* Make sure the parent's table is up to date. */
5590 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
5592 if (h
->vtable_entries_used
== NULL
)
5594 /* None of this table's entries were referenced. Re-use the
5596 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
5603 /* Or the parent's entries into ours. */
5604 cu
= h
->vtable_entries_used
;
5606 pu
= h
->vtable_parent
->vtable_entries_used
;
5609 n
= h
->vtable_parent
->size
/ FILE_ALIGN
;
5612 if (*pu
) *cu
= true;
5622 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
5623 struct elf_link_hash_entry
*h
;
5627 bfd_vma hstart
, hend
;
5628 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
5630 /* Take care of both those symbols that do not describe vtables as
5631 well as those that are not loaded. */
5632 if (h
->vtable_parent
== NULL
)
5635 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5636 || h
->root
.type
== bfd_link_hash_defweak
);
5638 sec
= h
->root
.u
.def
.section
;
5639 hstart
= h
->root
.u
.def
.value
;
5640 hend
= hstart
+ h
->size
;
5642 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
5643 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, true));
5645 return *(boolean
*)okp
= false;
5646 relend
= relstart
+ sec
->reloc_count
;
5648 for (rel
= relstart
; rel
< relend
; ++rel
)
5649 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
5651 /* If the entry is in use, do nothing. */
5652 if (h
->vtable_entries_used
)
5654 bfd_vma entry
= (rel
->r_offset
- hstart
) / FILE_ALIGN
;
5655 if (h
->vtable_entries_used
[entry
])
5658 /* Otherwise, kill it. */
5659 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
5665 /* Do mark and sweep of unused sections. */
5668 elf_gc_sections (abfd
, info
)
5670 struct bfd_link_info
*info
;
5674 asection
* (*gc_mark_hook
)
5675 PARAMS ((bfd
*abfd
, struct bfd_link_info
*, Elf_Internal_Rela
*,
5676 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
5678 if (!get_elf_backend_data (abfd
)->can_gc_sections
5679 || info
->relocateable
)
5682 /* Apply transitive closure to the vtable entry usage info. */
5683 elf_link_hash_traverse (elf_hash_table (info
),
5684 elf_gc_propagate_vtable_entries_used
,
5689 /* Kill the vtable relocations that were not used. */
5690 elf_link_hash_traverse (elf_hash_table (info
),
5691 elf_gc_smash_unused_vtentry_relocs
,
5696 /* Grovel through relocs to find out who stays ... */
5698 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
5699 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5702 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5704 if (o
->flags
& SEC_KEEP
)
5705 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
5710 /* ... and mark SEC_EXCLUDE for those that go. */
5711 if (!elf_gc_sweep(info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
5717 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
5720 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
5723 struct elf_link_hash_entry
*h
;
5726 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
5727 struct elf_link_hash_entry
**search
, *child
;
5728 bfd_size_type extsymcount
;
5730 /* The sh_info field of the symtab header tells us where the
5731 external symbols start. We don't care about the local symbols at
5733 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
5734 if (!elf_bad_symtab (abfd
))
5735 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
5737 sym_hashes
= elf_sym_hashes (abfd
);
5738 sym_hashes_end
= sym_hashes
+ extsymcount
;
5740 /* Hunt down the child symbol, which is in this section at the same
5741 offset as the relocation. */
5742 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
5744 if ((child
= *search
) != NULL
5745 && (child
->root
.type
== bfd_link_hash_defined
5746 || child
->root
.type
== bfd_link_hash_defweak
)
5747 && child
->root
.u
.def
.section
== sec
5748 && child
->root
.u
.def
.value
== offset
)
5752 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
5753 bfd_get_filename (abfd
), sec
->name
,
5754 (unsigned long)offset
);
5755 bfd_set_error (bfd_error_invalid_operation
);
5761 /* This *should* only be the absolute section. It could potentially
5762 be that someone has defined a non-global vtable though, which
5763 would be bad. It isn't worth paging in the local symbols to be
5764 sure though; that case should simply be handled by the assembler. */
5766 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
5769 child
->vtable_parent
= h
;
5774 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
5777 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
5780 struct elf_link_hash_entry
*h
;
5783 if (h
->vtable_entries_used
== NULL
)
5785 /* Allocate one extra entry for use as a "done" flag for the
5786 consolidation pass. */
5787 size_t size
= (h
->size
/ FILE_ALIGN
+ 1) * sizeof(boolean
);
5788 h
->vtable_entries_used
= (boolean
*) bfd_alloc (abfd
, size
);
5789 if (h
->vtable_entries_used
== NULL
)
5792 /* And arrange for that done flag to be at index -1. */
5793 memset (h
->vtable_entries_used
++, 0, size
);
5795 h
->vtable_entries_used
[addend
/ FILE_ALIGN
] = true;
5800 /* And an accompanying bit to work out final got entry offsets once
5801 we're done. Should be called from final_link. */
5804 elf_gc_common_finalize_got_offsets (abfd
, info
)
5806 struct bfd_link_info
*info
;
5809 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5812 /* The GOT offset is relative to the .got section, but the GOT header is
5813 put into the .got.plt section, if the backend uses it. */
5814 if (bed
->want_got_plt
)
5817 gotoff
= bed
->got_header_size
;
5819 /* Do the local .got entries first. */
5820 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
5822 bfd_signed_vma
*local_got
= elf_local_got_refcounts (i
);
5823 bfd_size_type j
, locsymcount
;
5824 Elf_Internal_Shdr
*symtab_hdr
;
5829 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
5830 if (elf_bad_symtab (i
))
5831 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
5833 locsymcount
= symtab_hdr
->sh_info
;
5835 for (j
= 0; j
< locsymcount
; ++j
)
5837 if (local_got
[j
] > 0)
5839 local_got
[j
] = gotoff
;
5840 gotoff
+= ARCH_SIZE
/ 8;
5843 local_got
[j
] = (bfd_vma
) -1;
5847 /* Then the global .got and .plt entries. */
5848 elf_link_hash_traverse (elf_hash_table (info
),
5849 elf_gc_allocate_got_offsets
,
5854 /* We need a special top-level link routine to convert got reference counts
5855 to real got offsets. */
5858 elf_gc_allocate_got_offsets (h
, offarg
)
5859 struct elf_link_hash_entry
*h
;
5862 bfd_vma
*off
= (bfd_vma
*) offarg
;
5864 if (h
->got
.refcount
> 0)
5866 h
->got
.offset
= off
[0];
5867 off
[0] += ARCH_SIZE
/ 8;
5870 h
->got
.offset
= (bfd_vma
) -1;
5875 /* Many folk need no more in the way of final link than this, once
5876 got entry reference counting is enabled. */
5879 elf_gc_common_final_link (abfd
, info
)
5881 struct bfd_link_info
*info
;
5883 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
5886 /* Invoke the regular ELF backend linker to do all the work. */
5887 return elf_bfd_final_link (abfd
, info
);