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
);
724 if (! bfd_get_section_contents (abfd
, s
, msg
, (file_ptr
) 0, sz
))
727 if (! (_bfd_generic_link_add_one_symbol
728 (info
, abfd
, name
, BSF_WARNING
, s
, (bfd_vma
) 0, msg
,
729 false, collect
, (struct bfd_link_hash_entry
**) NULL
)))
732 if (! info
->relocateable
)
734 /* Clobber the section size so that the warning does
735 not get copied into the output file. */
742 /* If this is a dynamic object, we always link against the .dynsym
743 symbol table, not the .symtab symbol table. The dynamic linker
744 will only see the .dynsym symbol table, so there is no reason to
745 look at .symtab for a dynamic object. */
747 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
748 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
750 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
754 /* Read in any version definitions. */
756 if (! _bfd_elf_slurp_version_tables (abfd
))
759 /* Read in the symbol versions, but don't bother to convert them
760 to internal format. */
761 if (elf_dynversym (abfd
) != 0)
763 Elf_Internal_Shdr
*versymhdr
;
765 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
766 extversym
= (Elf_External_Versym
*) bfd_malloc (hdr
->sh_size
);
767 if (extversym
== NULL
)
769 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
770 || (bfd_read ((PTR
) extversym
, 1, versymhdr
->sh_size
, abfd
)
771 != versymhdr
->sh_size
))
776 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
778 /* The sh_info field of the symtab header tells us where the
779 external symbols start. We don't care about the local symbols at
781 if (elf_bad_symtab (abfd
))
783 extsymcount
= symcount
;
788 extsymcount
= symcount
- hdr
->sh_info
;
789 extsymoff
= hdr
->sh_info
;
792 buf
= ((Elf_External_Sym
*)
793 bfd_malloc (extsymcount
* sizeof (Elf_External_Sym
)));
794 if (buf
== NULL
&& extsymcount
!= 0)
797 /* We store a pointer to the hash table entry for each external
799 sym_hash
= ((struct elf_link_hash_entry
**)
801 extsymcount
* sizeof (struct elf_link_hash_entry
*)));
802 if (sym_hash
== NULL
)
804 elf_sym_hashes (abfd
) = sym_hash
;
808 /* If we are creating a shared library, create all the dynamic
809 sections immediately. We need to attach them to something,
810 so we attach them to this BFD, provided it is the right
811 format. FIXME: If there are no input BFD's of the same
812 format as the output, we can't make a shared library. */
814 && ! elf_hash_table (info
)->dynamic_sections_created
815 && abfd
->xvec
== info
->hash
->creator
)
817 if (! elf_link_create_dynamic_sections (abfd
, info
))
826 bfd_size_type oldsize
;
827 bfd_size_type strindex
;
829 /* Find the name to use in a DT_NEEDED entry that refers to this
830 object. If the object has a DT_SONAME entry, we use it.
831 Otherwise, if the generic linker stuck something in
832 elf_dt_name, we use that. Otherwise, we just use the file
833 name. If the generic linker put a null string into
834 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
835 there is a DT_SONAME entry. */
837 name
= bfd_get_filename (abfd
);
838 if (elf_dt_name (abfd
) != NULL
)
840 name
= elf_dt_name (abfd
);
844 s
= bfd_get_section_by_name (abfd
, ".dynamic");
847 Elf_External_Dyn
*extdyn
;
848 Elf_External_Dyn
*extdynend
;
852 dynbuf
= (Elf_External_Dyn
*) bfd_malloc ((size_t) s
->_raw_size
);
856 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
857 (file_ptr
) 0, s
->_raw_size
))
860 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
863 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
866 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
867 for (; extdyn
< extdynend
; extdyn
++)
869 Elf_Internal_Dyn dyn
;
871 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
872 if (dyn
.d_tag
== DT_SONAME
)
874 name
= bfd_elf_string_from_elf_section (abfd
, link
,
879 if (dyn
.d_tag
== DT_NEEDED
)
881 struct bfd_link_needed_list
*n
, **pn
;
884 n
= ((struct bfd_link_needed_list
*)
885 bfd_alloc (abfd
, sizeof (struct bfd_link_needed_list
)));
886 fnm
= bfd_elf_string_from_elf_section (abfd
, link
,
888 if (n
== NULL
|| fnm
== NULL
)
890 anm
= bfd_alloc (abfd
, strlen (fnm
) + 1);
897 for (pn
= &elf_hash_table (info
)->needed
;
909 /* We do not want to include any of the sections in a dynamic
910 object in the output file. We hack by simply clobbering the
911 list of sections in the BFD. This could be handled more
912 cleanly by, say, a new section flag; the existing
913 SEC_NEVER_LOAD flag is not the one we want, because that one
914 still implies that the section takes up space in the output
916 abfd
->sections
= NULL
;
917 abfd
->section_count
= 0;
919 /* If this is the first dynamic object found in the link, create
920 the special sections required for dynamic linking. */
921 if (! elf_hash_table (info
)->dynamic_sections_created
)
923 if (! elf_link_create_dynamic_sections (abfd
, info
))
929 /* Add a DT_NEEDED entry for this dynamic object. */
930 oldsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
931 strindex
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, name
,
933 if (strindex
== (bfd_size_type
) -1)
936 if (oldsize
== _bfd_stringtab_size (elf_hash_table (info
)->dynstr
))
939 Elf_External_Dyn
*dyncon
, *dynconend
;
941 /* The hash table size did not change, which means that
942 the dynamic object name was already entered. If we
943 have already included this dynamic object in the
944 link, just ignore it. There is no reason to include
945 a particular dynamic object more than once. */
946 sdyn
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
948 BFD_ASSERT (sdyn
!= NULL
);
950 dyncon
= (Elf_External_Dyn
*) sdyn
->contents
;
951 dynconend
= (Elf_External_Dyn
*) (sdyn
->contents
+
953 for (; dyncon
< dynconend
; dyncon
++)
955 Elf_Internal_Dyn dyn
;
957 elf_swap_dyn_in (elf_hash_table (info
)->dynobj
, dyncon
,
959 if (dyn
.d_tag
== DT_NEEDED
960 && dyn
.d_un
.d_val
== strindex
)
964 if (extversym
!= NULL
)
971 if (! elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
975 /* Save the SONAME, if there is one, because sometimes the
976 linker emulation code will need to know it. */
978 name
= bfd_get_filename (abfd
);
979 elf_dt_name (abfd
) = name
;
983 hdr
->sh_offset
+ extsymoff
* sizeof (Elf_External_Sym
),
985 || (bfd_read ((PTR
) buf
, sizeof (Elf_External_Sym
), extsymcount
, abfd
)
986 != extsymcount
* sizeof (Elf_External_Sym
)))
991 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
992 esymend
= buf
+ extsymcount
;
995 esym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
997 Elf_Internal_Sym sym
;
1003 struct elf_link_hash_entry
*h
;
1005 boolean size_change_ok
, type_change_ok
;
1006 boolean new_weakdef
;
1007 unsigned int old_alignment
;
1009 elf_swap_symbol_in (abfd
, esym
, &sym
);
1011 flags
= BSF_NO_FLAGS
;
1013 value
= sym
.st_value
;
1016 bind
= ELF_ST_BIND (sym
.st_info
);
1017 if (bind
== STB_LOCAL
)
1019 /* This should be impossible, since ELF requires that all
1020 global symbols follow all local symbols, and that sh_info
1021 point to the first global symbol. Unfortunatealy, Irix 5
1025 else if (bind
== STB_GLOBAL
)
1027 if (sym
.st_shndx
!= SHN_UNDEF
1028 && sym
.st_shndx
!= SHN_COMMON
)
1033 else if (bind
== STB_WEAK
)
1037 /* Leave it up to the processor backend. */
1040 if (sym
.st_shndx
== SHN_UNDEF
)
1041 sec
= bfd_und_section_ptr
;
1042 else if (sym
.st_shndx
> 0 && sym
.st_shndx
< SHN_LORESERVE
)
1044 sec
= section_from_elf_index (abfd
, sym
.st_shndx
);
1046 sec
= bfd_abs_section_ptr
;
1047 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
1050 else if (sym
.st_shndx
== SHN_ABS
)
1051 sec
= bfd_abs_section_ptr
;
1052 else if (sym
.st_shndx
== SHN_COMMON
)
1054 sec
= bfd_com_section_ptr
;
1055 /* What ELF calls the size we call the value. What ELF
1056 calls the value we call the alignment. */
1057 value
= sym
.st_size
;
1061 /* Leave it up to the processor backend. */
1064 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
1065 if (name
== (const char *) NULL
)
1068 if (add_symbol_hook
)
1070 if (! (*add_symbol_hook
) (abfd
, info
, &sym
, &name
, &flags
, &sec
,
1074 /* The hook function sets the name to NULL if this symbol
1075 should be skipped for some reason. */
1076 if (name
== (const char *) NULL
)
1080 /* Sanity check that all possibilities were handled. */
1081 if (sec
== (asection
*) NULL
)
1083 bfd_set_error (bfd_error_bad_value
);
1087 if (bfd_is_und_section (sec
)
1088 || bfd_is_com_section (sec
))
1093 size_change_ok
= false;
1094 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
1096 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1098 Elf_Internal_Versym iver
;
1099 unsigned int vernum
= 0;
1104 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
1105 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
1107 /* If this is a hidden symbol, or if it is not version
1108 1, we append the version name to the symbol name.
1109 However, we do not modify a non-hidden absolute
1110 symbol, because it might be the version symbol
1111 itself. FIXME: What if it isn't? */
1112 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
1113 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
1116 int namelen
, newlen
;
1119 if (sym
.st_shndx
!= SHN_UNDEF
)
1121 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
1123 (*_bfd_error_handler
)
1124 (_("%s: %s: invalid version %u (max %d)"),
1125 abfd
->filename
, name
, vernum
,
1126 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
1127 bfd_set_error (bfd_error_bad_value
);
1130 else if (vernum
> 1)
1132 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1138 /* We cannot simply test for the number of
1139 entries in the VERNEED section since the
1140 numbers for the needed versions do not start
1142 Elf_Internal_Verneed
*t
;
1145 for (t
= elf_tdata (abfd
)->verref
;
1149 Elf_Internal_Vernaux
*a
;
1151 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1153 if (a
->vna_other
== vernum
)
1155 verstr
= a
->vna_nodename
;
1164 (*_bfd_error_handler
)
1165 (_("%s: %s: invalid needed version %d"),
1166 abfd
->filename
, name
, vernum
);
1167 bfd_set_error (bfd_error_bad_value
);
1172 namelen
= strlen (name
);
1173 newlen
= namelen
+ strlen (verstr
) + 2;
1174 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
1177 newname
= (char *) bfd_alloc (abfd
, newlen
);
1178 if (newname
== NULL
)
1180 strcpy (newname
, name
);
1181 p
= newname
+ namelen
;
1183 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
1191 if (! elf_merge_symbol (abfd
, info
, name
, &sym
, &sec
, &value
,
1192 sym_hash
, &override
, &type_change_ok
,
1200 while (h
->root
.type
== bfd_link_hash_indirect
1201 || h
->root
.type
== bfd_link_hash_warning
)
1202 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1204 /* Remember the old alignment if this is a common symbol, so
1205 that we don't reduce the alignment later on. We can't
1206 check later, because _bfd_generic_link_add_one_symbol
1207 will set a default for the alignment which we want to
1209 if (h
->root
.type
== bfd_link_hash_common
)
1210 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1212 if (elf_tdata (abfd
)->verdef
!= NULL
1216 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
1219 if (! (_bfd_generic_link_add_one_symbol
1220 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
1221 false, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
1225 while (h
->root
.type
== bfd_link_hash_indirect
1226 || h
->root
.type
== bfd_link_hash_warning
)
1227 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1230 new_weakdef
= false;
1233 && (flags
& BSF_WEAK
) != 0
1234 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
1235 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1236 && h
->weakdef
== NULL
)
1238 /* Keep a list of all weak defined non function symbols from
1239 a dynamic object, using the weakdef field. Later in this
1240 function we will set the weakdef field to the correct
1241 value. We only put non-function symbols from dynamic
1242 objects on this list, because that happens to be the only
1243 time we need to know the normal symbol corresponding to a
1244 weak symbol, and the information is time consuming to
1245 figure out. If the weakdef field is not already NULL,
1246 then this symbol was already defined by some previous
1247 dynamic object, and we will be using that previous
1248 definition anyhow. */
1255 /* Set the alignment of a common symbol. */
1256 if (sym
.st_shndx
== SHN_COMMON
1257 && h
->root
.type
== bfd_link_hash_common
)
1261 align
= bfd_log2 (sym
.st_value
);
1262 if (align
> old_alignment
)
1263 h
->root
.u
.c
.p
->alignment_power
= align
;
1266 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
1272 /* Remember the symbol size and type. */
1273 if (sym
.st_size
!= 0
1274 && (definition
|| h
->size
== 0))
1276 if (h
->size
!= 0 && h
->size
!= sym
.st_size
&& ! size_change_ok
)
1277 (*_bfd_error_handler
)
1278 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1279 name
, (unsigned long) h
->size
, (unsigned long) sym
.st_size
,
1280 bfd_get_filename (abfd
));
1282 h
->size
= sym
.st_size
;
1285 /* If this is a common symbol, then we always want H->SIZE
1286 to be the size of the common symbol. The code just above
1287 won't fix the size if a common symbol becomes larger. We
1288 don't warn about a size change here, because that is
1289 covered by --warn-common. */
1290 if (h
->root
.type
== bfd_link_hash_common
)
1291 h
->size
= h
->root
.u
.c
.size
;
1293 if (ELF_ST_TYPE (sym
.st_info
) != STT_NOTYPE
1294 && (definition
|| h
->type
== STT_NOTYPE
))
1296 if (h
->type
!= STT_NOTYPE
1297 && h
->type
!= ELF_ST_TYPE (sym
.st_info
)
1298 && ! type_change_ok
)
1299 (*_bfd_error_handler
)
1300 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1301 name
, h
->type
, ELF_ST_TYPE (sym
.st_info
),
1302 bfd_get_filename (abfd
));
1304 h
->type
= ELF_ST_TYPE (sym
.st_info
);
1307 if (sym
.st_other
!= 0
1308 && (definition
|| h
->other
== 0))
1309 h
->other
= sym
.st_other
;
1311 /* Set a flag in the hash table entry indicating the type of
1312 reference or definition we just found. Keep a count of
1313 the number of dynamic symbols we find. A dynamic symbol
1314 is one which is referenced or defined by both a regular
1315 object and a shared object. */
1316 old_flags
= h
->elf_link_hash_flags
;
1321 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
1323 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
1325 || (old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
1326 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
1332 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
1334 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
1335 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
1336 | ELF_LINK_HASH_REF_REGULAR
)) != 0
1337 || (h
->weakdef
!= NULL
1339 && h
->weakdef
->dynindx
!= -1))
1343 h
->elf_link_hash_flags
|= new_flag
;
1345 /* If this symbol has a version, and it is the default
1346 version, we create an indirect symbol from the default
1347 name to the fully decorated name. This will cause
1348 external references which do not specify a version to be
1349 bound to this version of the symbol. */
1354 p
= strchr (name
, ELF_VER_CHR
);
1355 if (p
!= NULL
&& p
[1] == ELF_VER_CHR
)
1358 struct elf_link_hash_entry
*hi
;
1361 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1363 if (shortname
== NULL
)
1365 strncpy (shortname
, name
, p
- name
);
1366 shortname
[p
- name
] = '\0';
1368 /* We are going to create a new symbol. Merge it
1369 with any existing symbol with this name. For the
1370 purposes of the merge, act as though we were
1371 defining the symbol we just defined, although we
1372 actually going to define an indirect symbol. */
1373 type_change_ok
= false;
1374 size_change_ok
= false;
1375 if (! elf_merge_symbol (abfd
, info
, shortname
, &sym
, &sec
,
1376 &value
, &hi
, &override
,
1377 &type_change_ok
, &size_change_ok
))
1382 if (! (_bfd_generic_link_add_one_symbol
1383 (info
, abfd
, shortname
, BSF_INDIRECT
,
1384 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1385 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1390 /* In this case the symbol named SHORTNAME is
1391 overriding the indirect symbol we want to
1392 add. We were planning on making SHORTNAME an
1393 indirect symbol referring to NAME. SHORTNAME
1394 is the name without a version. NAME is the
1395 fully versioned name, and it is the default
1398 Overriding means that we already saw a
1399 definition for the symbol SHORTNAME in a
1400 regular object, and it is overriding the
1401 symbol defined in the dynamic object.
1403 When this happens, we actually want to change
1404 NAME, the symbol we just added, to refer to
1405 SHORTNAME. This will cause references to
1406 NAME in the shared object to become
1407 references to SHORTNAME in the regular
1408 object. This is what we expect when we
1409 override a function in a shared object: that
1410 the references in the shared object will be
1411 mapped to the definition in the regular
1414 while (hi
->root
.type
== bfd_link_hash_indirect
1415 || hi
->root
.type
== bfd_link_hash_warning
)
1416 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1418 h
->root
.type
= bfd_link_hash_indirect
;
1419 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1420 if (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
)
1422 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_DEF_DYNAMIC
;
1423 hi
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_DYNAMIC
;
1424 if (! _bfd_elf_link_record_dynamic_symbol (info
, hi
))
1428 /* Now set HI to H, so that the following code
1429 will set the other fields correctly. */
1433 /* If there is a duplicate definition somewhere,
1434 then HI may not point to an indirect symbol. We
1435 will have reported an error to the user in that
1438 if (hi
->root
.type
== bfd_link_hash_indirect
)
1440 struct elf_link_hash_entry
*ht
;
1442 /* If the symbol became indirect, then we assume
1443 that we have not seen a definition before. */
1444 BFD_ASSERT ((hi
->elf_link_hash_flags
1445 & (ELF_LINK_HASH_DEF_DYNAMIC
1446 | ELF_LINK_HASH_DEF_REGULAR
))
1449 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1451 /* Copy down any references that we may have
1452 already seen to the symbol which just became
1454 ht
->elf_link_hash_flags
|=
1455 (hi
->elf_link_hash_flags
1456 & (ELF_LINK_HASH_REF_DYNAMIC
1457 | ELF_LINK_HASH_REF_REGULAR
));
1459 /* Copy over the global and procedure linkage table
1460 offset entries. These may have been already set
1461 up by a check_relocs routine. */
1462 if (ht
->got
.offset
== (bfd_vma
) -1)
1464 ht
->got
.offset
= hi
->got
.offset
;
1465 hi
->got
.offset
= (bfd_vma
) -1;
1467 BFD_ASSERT (hi
->got
.offset
== (bfd_vma
) -1);
1469 if (ht
->plt
.offset
== (bfd_vma
) -1)
1471 ht
->plt
.offset
= hi
->plt
.offset
;
1472 hi
->plt
.offset
= (bfd_vma
) -1;
1474 BFD_ASSERT (hi
->plt
.offset
== (bfd_vma
) -1);
1476 if (ht
->dynindx
== -1)
1478 ht
->dynindx
= hi
->dynindx
;
1479 ht
->dynstr_index
= hi
->dynstr_index
;
1481 hi
->dynstr_index
= 0;
1483 BFD_ASSERT (hi
->dynindx
== -1);
1485 /* FIXME: There may be other information to copy
1486 over for particular targets. */
1488 /* See if the new flags lead us to realize that
1489 the symbol must be dynamic. */
1495 || ((hi
->elf_link_hash_flags
1496 & ELF_LINK_HASH_REF_DYNAMIC
)
1502 if ((hi
->elf_link_hash_flags
1503 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1509 /* We also need to define an indirection from the
1510 nondefault version of the symbol. */
1512 shortname
= bfd_hash_allocate (&info
->hash
->table
,
1514 if (shortname
== NULL
)
1516 strncpy (shortname
, name
, p
- name
);
1517 strcpy (shortname
+ (p
- name
), p
+ 1);
1519 /* Once again, merge with any existing symbol. */
1520 type_change_ok
= false;
1521 size_change_ok
= false;
1522 if (! elf_merge_symbol (abfd
, info
, shortname
, &sym
, &sec
,
1523 &value
, &hi
, &override
,
1524 &type_change_ok
, &size_change_ok
))
1529 /* Here SHORTNAME is a versioned name, so we
1530 don't expect to see the type of override we
1531 do in the case above. */
1532 (*_bfd_error_handler
)
1533 (_("%s: warning: unexpected redefinition of `%s'"),
1534 bfd_get_filename (abfd
), shortname
);
1538 if (! (_bfd_generic_link_add_one_symbol
1539 (info
, abfd
, shortname
, BSF_INDIRECT
,
1540 bfd_ind_section_ptr
, (bfd_vma
) 0, name
, false,
1541 collect
, (struct bfd_link_hash_entry
**) &hi
)))
1544 /* If there is a duplicate definition somewhere,
1545 then HI may not point to an indirect symbol.
1546 We will have reported an error to the user in
1549 if (hi
->root
.type
== bfd_link_hash_indirect
)
1551 /* If the symbol became indirect, then we
1552 assume that we have not seen a definition
1554 BFD_ASSERT ((hi
->elf_link_hash_flags
1555 & (ELF_LINK_HASH_DEF_DYNAMIC
1556 | ELF_LINK_HASH_DEF_REGULAR
))
1559 /* Copy down any references that we may have
1560 already seen to the symbol which just
1562 h
->elf_link_hash_flags
|=
1563 (hi
->elf_link_hash_flags
1564 & (ELF_LINK_HASH_REF_DYNAMIC
1565 | ELF_LINK_HASH_REF_REGULAR
));
1567 /* Copy over the global and procedure linkage
1568 table offset entries. These may have been
1569 already set up by a check_relocs routine. */
1570 if (h
->got
.offset
== (bfd_vma
) -1)
1572 h
->got
.offset
= hi
->got
.offset
;
1573 hi
->got
.offset
= (bfd_vma
) -1;
1575 BFD_ASSERT (hi
->got
.offset
== (bfd_vma
) -1);
1577 if (h
->plt
.offset
== (bfd_vma
) -1)
1579 h
->plt
.offset
= hi
->plt
.offset
;
1580 hi
->plt
.offset
= (bfd_vma
) -1;
1582 BFD_ASSERT (hi
->got
.offset
== (bfd_vma
) -1);
1584 if (h
->dynindx
== -1)
1586 h
->dynindx
= hi
->dynindx
;
1587 h
->dynstr_index
= hi
->dynstr_index
;
1589 hi
->dynstr_index
= 0;
1591 BFD_ASSERT (hi
->dynindx
== -1);
1593 /* FIXME: There may be other information to
1594 copy over for particular targets. */
1596 /* See if the new flags lead us to realize
1597 that the symbol must be dynamic. */
1603 || ((hi
->elf_link_hash_flags
1604 & ELF_LINK_HASH_REF_DYNAMIC
)
1610 if ((hi
->elf_link_hash_flags
1611 & ELF_LINK_HASH_REF_REGULAR
) != 0)
1620 if (dynsym
&& h
->dynindx
== -1)
1622 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1624 if (h
->weakdef
!= NULL
1626 && h
->weakdef
->dynindx
== -1)
1628 if (! _bfd_elf_link_record_dynamic_symbol (info
,
1636 /* Now set the weakdefs field correctly for all the weak defined
1637 symbols we found. The only way to do this is to search all the
1638 symbols. Since we only need the information for non functions in
1639 dynamic objects, that's the only time we actually put anything on
1640 the list WEAKS. We need this information so that if a regular
1641 object refers to a symbol defined weakly in a dynamic object, the
1642 real symbol in the dynamic object is also put in the dynamic
1643 symbols; we also must arrange for both symbols to point to the
1644 same memory location. We could handle the general case of symbol
1645 aliasing, but a general symbol alias can only be generated in
1646 assembler code, handling it correctly would be very time
1647 consuming, and other ELF linkers don't handle general aliasing
1649 while (weaks
!= NULL
)
1651 struct elf_link_hash_entry
*hlook
;
1654 struct elf_link_hash_entry
**hpp
;
1655 struct elf_link_hash_entry
**hppend
;
1658 weaks
= hlook
->weakdef
;
1659 hlook
->weakdef
= NULL
;
1661 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
1662 || hlook
->root
.type
== bfd_link_hash_defweak
1663 || hlook
->root
.type
== bfd_link_hash_common
1664 || hlook
->root
.type
== bfd_link_hash_indirect
);
1665 slook
= hlook
->root
.u
.def
.section
;
1666 vlook
= hlook
->root
.u
.def
.value
;
1668 hpp
= elf_sym_hashes (abfd
);
1669 hppend
= hpp
+ extsymcount
;
1670 for (; hpp
< hppend
; hpp
++)
1672 struct elf_link_hash_entry
*h
;
1675 if (h
!= NULL
&& h
!= hlook
1676 && h
->root
.type
== bfd_link_hash_defined
1677 && h
->root
.u
.def
.section
== slook
1678 && h
->root
.u
.def
.value
== vlook
)
1682 /* If the weak definition is in the list of dynamic
1683 symbols, make sure the real definition is put there
1685 if (hlook
->dynindx
!= -1
1686 && h
->dynindx
== -1)
1688 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1692 /* If the real definition is in the list of dynamic
1693 symbols, make sure the weak definition is put there
1694 as well. If we don't do this, then the dynamic
1695 loader might not merge the entries for the real
1696 definition and the weak definition. */
1697 if (h
->dynindx
!= -1
1698 && hlook
->dynindx
== -1)
1700 if (! _bfd_elf_link_record_dynamic_symbol (info
, hlook
))
1715 if (extversym
!= NULL
)
1721 /* If this object is the same format as the output object, and it is
1722 not a shared library, then let the backend look through the
1725 This is required to build global offset table entries and to
1726 arrange for dynamic relocs. It is not required for the
1727 particular common case of linking non PIC code, even when linking
1728 against shared libraries, but unfortunately there is no way of
1729 knowing whether an object file has been compiled PIC or not.
1730 Looking through the relocs is not particularly time consuming.
1731 The problem is that we must either (1) keep the relocs in memory,
1732 which causes the linker to require additional runtime memory or
1733 (2) read the relocs twice from the input file, which wastes time.
1734 This would be a good case for using mmap.
1736 I have no idea how to handle linking PIC code into a file of a
1737 different format. It probably can't be done. */
1738 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
1740 && abfd
->xvec
== info
->hash
->creator
1741 && check_relocs
!= NULL
)
1745 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1747 Elf_Internal_Rela
*internal_relocs
;
1750 if ((o
->flags
& SEC_RELOC
) == 0
1751 || o
->reloc_count
== 0
1752 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
1753 && (o
->flags
& SEC_DEBUGGING
) != 0)
1754 || bfd_is_abs_section (o
->output_section
))
1757 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
1758 (abfd
, o
, (PTR
) NULL
,
1759 (Elf_Internal_Rela
*) NULL
,
1760 info
->keep_memory
));
1761 if (internal_relocs
== NULL
)
1764 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
1766 if (! info
->keep_memory
)
1767 free (internal_relocs
);
1774 /* If this is a non-traditional, non-relocateable link, try to
1775 optimize the handling of the .stab/.stabstr sections. */
1777 && ! info
->relocateable
1778 && ! info
->traditional_format
1779 && info
->hash
->creator
->flavour
== bfd_target_elf_flavour
1780 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
1782 asection
*stab
, *stabstr
;
1784 stab
= bfd_get_section_by_name (abfd
, ".stab");
1787 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
1789 if (stabstr
!= NULL
)
1791 struct bfd_elf_section_data
*secdata
;
1793 secdata
= elf_section_data (stab
);
1794 if (! _bfd_link_section_stabs (abfd
,
1795 &elf_hash_table (info
)->stab_info
,
1797 &secdata
->stab_info
))
1812 if (extversym
!= NULL
)
1817 /* Create some sections which will be filled in with dynamic linking
1818 information. ABFD is an input file which requires dynamic sections
1819 to be created. The dynamic sections take up virtual memory space
1820 when the final executable is run, so we need to create them before
1821 addresses are assigned to the output sections. We work out the
1822 actual contents and size of these sections later. */
1825 elf_link_create_dynamic_sections (abfd
, info
)
1827 struct bfd_link_info
*info
;
1830 register asection
*s
;
1831 struct elf_link_hash_entry
*h
;
1832 struct elf_backend_data
*bed
;
1834 if (elf_hash_table (info
)->dynamic_sections_created
)
1837 /* Make sure that all dynamic sections use the same input BFD. */
1838 if (elf_hash_table (info
)->dynobj
== NULL
)
1839 elf_hash_table (info
)->dynobj
= abfd
;
1841 abfd
= elf_hash_table (info
)->dynobj
;
1843 /* Note that we set the SEC_IN_MEMORY flag for all of these
1845 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1846 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
1848 /* A dynamically linked executable has a .interp section, but a
1849 shared library does not. */
1852 s
= bfd_make_section (abfd
, ".interp");
1854 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
1858 /* Create sections to hold version informations. These are removed
1859 if they are not needed. */
1860 s
= bfd_make_section (abfd
, ".gnu.version_d");
1862 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1863 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1866 s
= bfd_make_section (abfd
, ".gnu.version");
1868 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1869 || ! bfd_set_section_alignment (abfd
, s
, 1))
1872 s
= bfd_make_section (abfd
, ".gnu.version_r");
1874 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1875 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1878 s
= bfd_make_section (abfd
, ".dynsym");
1880 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1881 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1884 s
= bfd_make_section (abfd
, ".dynstr");
1886 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
1889 /* Create a strtab to hold the dynamic symbol names. */
1890 if (elf_hash_table (info
)->dynstr
== NULL
)
1892 elf_hash_table (info
)->dynstr
= elf_stringtab_init ();
1893 if (elf_hash_table (info
)->dynstr
== NULL
)
1897 s
= bfd_make_section (abfd
, ".dynamic");
1899 || ! bfd_set_section_flags (abfd
, s
, flags
)
1900 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1903 /* The special symbol _DYNAMIC is always set to the start of the
1904 .dynamic section. This call occurs before we have processed the
1905 symbols for any dynamic object, so we don't have to worry about
1906 overriding a dynamic definition. We could set _DYNAMIC in a
1907 linker script, but we only want to define it if we are, in fact,
1908 creating a .dynamic section. We don't want to define it if there
1909 is no .dynamic section, since on some ELF platforms the start up
1910 code examines it to decide how to initialize the process. */
1912 if (! (_bfd_generic_link_add_one_symbol
1913 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, (bfd_vma
) 0,
1914 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
1915 (struct bfd_link_hash_entry
**) &h
)))
1917 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
1918 h
->type
= STT_OBJECT
;
1921 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
1924 s
= bfd_make_section (abfd
, ".hash");
1926 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
1927 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
1930 /* Let the backend create the rest of the sections. This lets the
1931 backend set the right flags. The backend will normally create
1932 the .got and .plt sections. */
1933 bed
= get_elf_backend_data (abfd
);
1934 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
1937 elf_hash_table (info
)->dynamic_sections_created
= true;
1942 /* Add an entry to the .dynamic table. */
1945 elf_add_dynamic_entry (info
, tag
, val
)
1946 struct bfd_link_info
*info
;
1950 Elf_Internal_Dyn dyn
;
1954 bfd_byte
*newcontents
;
1956 dynobj
= elf_hash_table (info
)->dynobj
;
1958 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
1959 BFD_ASSERT (s
!= NULL
);
1961 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
1962 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
1963 if (newcontents
== NULL
)
1967 dyn
.d_un
.d_val
= val
;
1968 elf_swap_dyn_out (dynobj
, &dyn
,
1969 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
1971 s
->_raw_size
= newsize
;
1972 s
->contents
= newcontents
;
1978 /* Read and swap the relocs for a section. They may have been cached.
1979 If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are not NULL,
1980 they are used as buffers to read into. They are known to be large
1981 enough. If the INTERNAL_RELOCS relocs argument is NULL, the return
1982 value is allocated using either malloc or bfd_alloc, according to
1983 the KEEP_MEMORY argument. */
1986 NAME(_bfd_elf
,link_read_relocs
) (abfd
, o
, external_relocs
, internal_relocs
,
1990 PTR external_relocs
;
1991 Elf_Internal_Rela
*internal_relocs
;
1992 boolean keep_memory
;
1994 Elf_Internal_Shdr
*rel_hdr
;
1996 Elf_Internal_Rela
*alloc2
= NULL
;
1998 if (elf_section_data (o
)->relocs
!= NULL
)
1999 return elf_section_data (o
)->relocs
;
2001 if (o
->reloc_count
== 0)
2004 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2006 if (internal_relocs
== NULL
)
2010 size
= o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2012 internal_relocs
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2014 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2015 if (internal_relocs
== NULL
)
2019 if (external_relocs
== NULL
)
2021 alloc1
= (PTR
) bfd_malloc ((size_t) rel_hdr
->sh_size
);
2024 external_relocs
= alloc1
;
2027 if ((bfd_seek (abfd
, rel_hdr
->sh_offset
, SEEK_SET
) != 0)
2028 || (bfd_read (external_relocs
, 1, rel_hdr
->sh_size
, abfd
)
2029 != rel_hdr
->sh_size
))
2032 /* Swap in the relocs. For convenience, we always produce an
2033 Elf_Internal_Rela array; if the relocs are Rel, we set the addend
2035 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
2037 Elf_External_Rel
*erel
;
2038 Elf_External_Rel
*erelend
;
2039 Elf_Internal_Rela
*irela
;
2041 erel
= (Elf_External_Rel
*) external_relocs
;
2042 erelend
= erel
+ o
->reloc_count
;
2043 irela
= internal_relocs
;
2044 for (; erel
< erelend
; erel
++, irela
++)
2046 Elf_Internal_Rel irel
;
2048 elf_swap_reloc_in (abfd
, erel
, &irel
);
2049 irela
->r_offset
= irel
.r_offset
;
2050 irela
->r_info
= irel
.r_info
;
2051 irela
->r_addend
= 0;
2056 Elf_External_Rela
*erela
;
2057 Elf_External_Rela
*erelaend
;
2058 Elf_Internal_Rela
*irela
;
2060 BFD_ASSERT (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rela
));
2062 erela
= (Elf_External_Rela
*) external_relocs
;
2063 erelaend
= erela
+ o
->reloc_count
;
2064 irela
= internal_relocs
;
2065 for (; erela
< erelaend
; erela
++, irela
++)
2066 elf_swap_reloca_in (abfd
, erela
, irela
);
2069 /* Cache the results for next time, if we can. */
2071 elf_section_data (o
)->relocs
= internal_relocs
;
2076 /* Don't free alloc2, since if it was allocated we are passing it
2077 back (under the name of internal_relocs). */
2079 return internal_relocs
;
2090 /* Record an assignment to a symbol made by a linker script. We need
2091 this in case some dynamic object refers to this symbol. */
2095 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
, provide
)
2097 struct bfd_link_info
*info
;
2101 struct elf_link_hash_entry
*h
;
2103 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2106 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, true, false);
2110 if (h
->root
.type
== bfd_link_hash_new
)
2111 h
->elf_link_hash_flags
&=~ ELF_LINK_NON_ELF
;
2113 /* If this symbol is being provided by the linker script, and it is
2114 currently defined by a dynamic object, but not by a regular
2115 object, then mark it as undefined so that the generic linker will
2116 force the correct value. */
2118 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2119 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2120 h
->root
.type
= bfd_link_hash_undefined
;
2122 /* If this symbol is not being provided by the linker script, and it is
2123 currently defined by a dynamic object, but not by a regular object,
2124 then clear out any version information because the symbol will not be
2125 associated with the dynamic object any more. */
2127 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2128 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2129 h
->verinfo
.verdef
= NULL
;
2131 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2132 h
->type
= STT_OBJECT
;
2134 if (((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
2135 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
2137 && h
->dynindx
== -1)
2139 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2142 /* If this is a weak defined symbol, and we know a corresponding
2143 real symbol from the same dynamic object, make sure the real
2144 symbol is also made into a dynamic symbol. */
2145 if (h
->weakdef
!= NULL
2146 && h
->weakdef
->dynindx
== -1)
2148 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
->weakdef
))
2156 /* This structure is used to pass information to
2157 elf_link_assign_sym_version. */
2159 struct elf_assign_sym_version_info
2163 /* General link information. */
2164 struct bfd_link_info
*info
;
2166 struct bfd_elf_version_tree
*verdefs
;
2167 /* Whether we are exporting all dynamic symbols. */
2168 boolean export_dynamic
;
2169 /* Whether we removed any symbols from the dynamic symbol table. */
2170 boolean removed_dynamic
;
2171 /* Whether we had a failure. */
2175 /* This structure is used to pass information to
2176 elf_link_find_version_dependencies. */
2178 struct elf_find_verdep_info
2182 /* General link information. */
2183 struct bfd_link_info
*info
;
2184 /* The number of dependencies. */
2186 /* Whether we had a failure. */
2190 /* Array used to determine the number of hash table buckets to use
2191 based on the number of symbols there are. If there are fewer than
2192 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2193 fewer than 37 we use 17 buckets, and so forth. We never use more
2194 than 32771 buckets. */
2196 static const size_t elf_buckets
[] =
2198 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2202 /* Set up the sizes and contents of the ELF dynamic sections. This is
2203 called by the ELF linker emulation before_allocation routine. We
2204 must set the sizes of the sections before the linker sets the
2205 addresses of the various sections. */
2208 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, soname
, rpath
,
2209 export_dynamic
, filter_shlib
,
2210 auxiliary_filters
, info
, sinterpptr
,
2215 boolean export_dynamic
;
2216 const char *filter_shlib
;
2217 const char * const *auxiliary_filters
;
2218 struct bfd_link_info
*info
;
2219 asection
**sinterpptr
;
2220 struct bfd_elf_version_tree
*verdefs
;
2222 bfd_size_type soname_indx
;
2224 struct elf_backend_data
*bed
;
2225 bfd_size_type old_dynsymcount
;
2226 struct elf_assign_sym_version_info asvinfo
;
2230 soname_indx
= (bfd_size_type
) -1;
2232 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
2235 /* The backend may have to create some sections regardless of whether
2236 we're dynamic or not. */
2237 bed
= get_elf_backend_data (output_bfd
);
2238 if (bed
->elf_backend_always_size_sections
2239 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
2242 dynobj
= elf_hash_table (info
)->dynobj
;
2244 /* If there were no dynamic objects in the link, there is nothing to
2249 /* If we are supposed to export all symbols into the dynamic symbol
2250 table (this is not the normal case), then do so. */
2253 struct elf_info_failed eif
;
2257 elf_link_hash_traverse (elf_hash_table (info
), elf_export_symbol
,
2263 if (elf_hash_table (info
)->dynamic_sections_created
)
2265 struct elf_info_failed eif
;
2266 struct elf_link_hash_entry
*h
;
2267 bfd_size_type strsize
;
2269 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
2270 BFD_ASSERT (*sinterpptr
!= NULL
|| info
->shared
);
2274 soname_indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2275 soname
, true, true);
2276 if (soname_indx
== (bfd_size_type
) -1
2277 || ! elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
2283 if (! elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
2291 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
, rpath
,
2293 if (indx
== (bfd_size_type
) -1
2294 || ! elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
2298 if (filter_shlib
!= NULL
)
2302 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2303 filter_shlib
, true, true);
2304 if (indx
== (bfd_size_type
) -1
2305 || ! elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
2309 if (auxiliary_filters
!= NULL
)
2311 const char * const *p
;
2313 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
2317 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2319 if (indx
== (bfd_size_type
) -1
2320 || ! elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
2325 /* Attach all the symbols to their version information. */
2326 asvinfo
.output_bfd
= output_bfd
;
2327 asvinfo
.info
= info
;
2328 asvinfo
.verdefs
= verdefs
;
2329 asvinfo
.export_dynamic
= export_dynamic
;
2330 asvinfo
.removed_dynamic
= false;
2331 asvinfo
.failed
= false;
2333 elf_link_hash_traverse (elf_hash_table (info
),
2334 elf_link_assign_sym_version
,
2339 /* Find all symbols which were defined in a dynamic object and make
2340 the backend pick a reasonable value for them. */
2343 elf_link_hash_traverse (elf_hash_table (info
),
2344 elf_adjust_dynamic_symbol
,
2349 /* Add some entries to the .dynamic section. We fill in some of the
2350 values later, in elf_bfd_final_link, but we must add the entries
2351 now so that we know the final size of the .dynamic section. */
2352 h
= elf_link_hash_lookup (elf_hash_table (info
), "_init", false,
2355 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2356 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2358 if (! elf_add_dynamic_entry (info
, DT_INIT
, 0))
2361 h
= elf_link_hash_lookup (elf_hash_table (info
), "_fini", false,
2364 && (h
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
2365 | ELF_LINK_HASH_DEF_REGULAR
)) != 0)
2367 if (! elf_add_dynamic_entry (info
, DT_FINI
, 0))
2370 strsize
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
2371 if (! elf_add_dynamic_entry (info
, DT_HASH
, 0)
2372 || ! elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
2373 || ! elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
2374 || ! elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
2375 || ! elf_add_dynamic_entry (info
, DT_SYMENT
,
2376 sizeof (Elf_External_Sym
)))
2380 /* The backend must work out the sizes of all the other dynamic
2382 old_dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2383 if (bed
->elf_backend_size_dynamic_sections
2384 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
2387 if (elf_hash_table (info
)->dynamic_sections_created
)
2392 size_t bucketcount
= 0;
2393 Elf_Internal_Sym isym
;
2395 /* Set up the version definition section. */
2396 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2397 BFD_ASSERT (s
!= NULL
);
2399 /* We may have created additional version definitions if we are
2400 just linking a regular application. */
2401 verdefs
= asvinfo
.verdefs
;
2403 if (verdefs
== NULL
)
2407 /* Don't include this section in the output file. */
2408 for (spp
= &output_bfd
->sections
;
2409 *spp
!= s
->output_section
;
2410 spp
= &(*spp
)->next
)
2412 *spp
= s
->output_section
->next
;
2413 --output_bfd
->section_count
;
2419 struct bfd_elf_version_tree
*t
;
2421 Elf_Internal_Verdef def
;
2422 Elf_Internal_Verdaux defaux
;
2424 if (asvinfo
.removed_dynamic
)
2426 /* Some dynamic symbols were changed to be local
2427 symbols. In this case, we renumber all of the
2428 dynamic symbols, so that we don't have a hole. If
2429 the backend changed dynsymcount, then assume that the
2430 new symbols are at the start. This is the case on
2431 the MIPS. FIXME: The names of the removed symbols
2432 will still be in the dynamic string table, wasting
2434 elf_hash_table (info
)->dynsymcount
=
2435 1 + (elf_hash_table (info
)->dynsymcount
- old_dynsymcount
);
2436 elf_link_hash_traverse (elf_hash_table (info
),
2437 elf_link_renumber_dynsyms
,
2444 /* Make space for the base version. */
2445 size
+= sizeof (Elf_External_Verdef
);
2446 size
+= sizeof (Elf_External_Verdaux
);
2449 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2451 struct bfd_elf_version_deps
*n
;
2453 size
+= sizeof (Elf_External_Verdef
);
2454 size
+= sizeof (Elf_External_Verdaux
);
2457 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2458 size
+= sizeof (Elf_External_Verdaux
);
2461 s
->_raw_size
= size
;
2462 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2463 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2466 /* Fill in the version definition section. */
2470 def
.vd_version
= VER_DEF_CURRENT
;
2471 def
.vd_flags
= VER_FLG_BASE
;
2474 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2475 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2476 + sizeof (Elf_External_Verdaux
));
2478 if (soname_indx
!= (bfd_size_type
) -1)
2480 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) soname
);
2481 defaux
.vda_name
= soname_indx
;
2488 name
= output_bfd
->filename
;
2489 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) name
);
2490 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2492 if (indx
== (bfd_size_type
) -1)
2494 defaux
.vda_name
= indx
;
2496 defaux
.vda_next
= 0;
2498 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2499 (Elf_External_Verdef
*)p
);
2500 p
+= sizeof (Elf_External_Verdef
);
2501 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2502 (Elf_External_Verdaux
*) p
);
2503 p
+= sizeof (Elf_External_Verdaux
);
2505 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
2508 struct bfd_elf_version_deps
*n
;
2509 struct elf_link_hash_entry
*h
;
2512 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2515 /* Add a symbol representing this version. */
2517 if (! (_bfd_generic_link_add_one_symbol
2518 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
2519 (bfd_vma
) 0, (const char *) NULL
, false,
2520 get_elf_backend_data (dynobj
)->collect
,
2521 (struct bfd_link_hash_entry
**) &h
)))
2523 h
->elf_link_hash_flags
&= ~ ELF_LINK_NON_ELF
;
2524 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2525 h
->type
= STT_OBJECT
;
2526 h
->verinfo
.vertree
= t
;
2528 if (! _bfd_elf_link_record_dynamic_symbol (info
, h
))
2531 def
.vd_version
= VER_DEF_CURRENT
;
2533 if (t
->globals
== NULL
&& t
->locals
== NULL
&& ! t
->used
)
2534 def
.vd_flags
|= VER_FLG_WEAK
;
2535 def
.vd_ndx
= t
->vernum
+ 1;
2536 def
.vd_cnt
= cdeps
+ 1;
2537 def
.vd_hash
= bfd_elf_hash ((const unsigned char *) t
->name
);
2538 def
.vd_aux
= sizeof (Elf_External_Verdef
);
2539 if (t
->next
!= NULL
)
2540 def
.vd_next
= (sizeof (Elf_External_Verdef
)
2541 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
2545 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
2546 (Elf_External_Verdef
*) p
);
2547 p
+= sizeof (Elf_External_Verdef
);
2549 defaux
.vda_name
= h
->dynstr_index
;
2550 if (t
->deps
== NULL
)
2551 defaux
.vda_next
= 0;
2553 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2554 t
->name_indx
= defaux
.vda_name
;
2556 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2557 (Elf_External_Verdaux
*) p
);
2558 p
+= sizeof (Elf_External_Verdaux
);
2560 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
2562 if (n
->version_needed
== NULL
)
2564 /* This can happen if there was an error in the
2566 defaux
.vda_name
= 0;
2569 defaux
.vda_name
= n
->version_needed
->name_indx
;
2570 if (n
->next
== NULL
)
2571 defaux
.vda_next
= 0;
2573 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
2575 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
2576 (Elf_External_Verdaux
*) p
);
2577 p
+= sizeof (Elf_External_Verdaux
);
2581 if (! elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
2582 || ! elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
2585 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
2588 /* Work out the size of the version reference section. */
2590 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2591 BFD_ASSERT (s
!= NULL
);
2593 struct elf_find_verdep_info sinfo
;
2595 sinfo
.output_bfd
= output_bfd
;
2597 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
2598 if (sinfo
.vers
== 0)
2600 sinfo
.failed
= false;
2602 elf_link_hash_traverse (elf_hash_table (info
),
2603 elf_link_find_version_dependencies
,
2606 if (elf_tdata (output_bfd
)->verref
== NULL
)
2610 /* We don't have any version definitions, so we can just
2611 remove the section. */
2613 for (spp
= &output_bfd
->sections
;
2614 *spp
!= s
->output_section
;
2615 spp
= &(*spp
)->next
)
2617 *spp
= s
->output_section
->next
;
2618 --output_bfd
->section_count
;
2622 Elf_Internal_Verneed
*t
;
2627 /* Build the version definition section. */
2630 for (t
= elf_tdata (output_bfd
)->verref
;
2634 Elf_Internal_Vernaux
*a
;
2636 size
+= sizeof (Elf_External_Verneed
);
2638 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2639 size
+= sizeof (Elf_External_Vernaux
);
2642 s
->_raw_size
= size
;
2643 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, size
);
2644 if (s
->contents
== NULL
)
2648 for (t
= elf_tdata (output_bfd
)->verref
;
2653 Elf_Internal_Vernaux
*a
;
2657 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2660 t
->vn_version
= VER_NEED_CURRENT
;
2662 if (elf_dt_name (t
->vn_bfd
) != NULL
)
2663 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2664 elf_dt_name (t
->vn_bfd
),
2667 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2668 t
->vn_bfd
->filename
, true, false);
2669 if (indx
== (bfd_size_type
) -1)
2672 t
->vn_aux
= sizeof (Elf_External_Verneed
);
2673 if (t
->vn_nextref
== NULL
)
2676 t
->vn_next
= (sizeof (Elf_External_Verneed
)
2677 + caux
* sizeof (Elf_External_Vernaux
));
2679 _bfd_elf_swap_verneed_out (output_bfd
, t
,
2680 (Elf_External_Verneed
*) p
);
2681 p
+= sizeof (Elf_External_Verneed
);
2683 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2685 a
->vna_hash
= bfd_elf_hash ((const unsigned char *)
2687 indx
= _bfd_stringtab_add (elf_hash_table (info
)->dynstr
,
2688 a
->vna_nodename
, true, false);
2689 if (indx
== (bfd_size_type
) -1)
2692 if (a
->vna_nextptr
== NULL
)
2695 a
->vna_next
= sizeof (Elf_External_Vernaux
);
2697 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
2698 (Elf_External_Vernaux
*) p
);
2699 p
+= sizeof (Elf_External_Vernaux
);
2703 if (! elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
2704 || ! elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
2707 elf_tdata (output_bfd
)->cverrefs
= crefs
;
2711 dynsymcount
= elf_hash_table (info
)->dynsymcount
;
2713 /* Work out the size of the symbol version section. */
2714 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
2715 BFD_ASSERT (s
!= NULL
);
2716 if (dynsymcount
== 0
2717 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
2721 /* We don't need any symbol versions; just discard the
2723 for (spp
= &output_bfd
->sections
;
2724 *spp
!= s
->output_section
;
2725 spp
= &(*spp
)->next
)
2727 *spp
= s
->output_section
->next
;
2728 --output_bfd
->section_count
;
2732 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Versym
);
2733 s
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, s
->_raw_size
);
2734 if (s
->contents
== NULL
)
2737 if (! elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
2741 /* Set the size of the .dynsym and .hash sections. We counted
2742 the number of dynamic symbols in elf_link_add_object_symbols.
2743 We will build the contents of .dynsym and .hash when we build
2744 the final symbol table, because until then we do not know the
2745 correct value to give the symbols. We built the .dynstr
2746 section as we went along in elf_link_add_object_symbols. */
2747 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
2748 BFD_ASSERT (s
!= NULL
);
2749 s
->_raw_size
= dynsymcount
* sizeof (Elf_External_Sym
);
2750 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2751 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
2754 /* The first entry in .dynsym is a dummy symbol. */
2761 elf_swap_symbol_out (output_bfd
, &isym
,
2762 (PTR
) (Elf_External_Sym
*) s
->contents
);
2764 for (i
= 0; elf_buckets
[i
] != 0; i
++)
2766 bucketcount
= elf_buckets
[i
];
2767 if (dynsymcount
< elf_buckets
[i
+ 1])
2771 s
= bfd_get_section_by_name (dynobj
, ".hash");
2772 BFD_ASSERT (s
!= NULL
);
2773 s
->_raw_size
= (2 + bucketcount
+ dynsymcount
) * (ARCH_SIZE
/ 8);
2774 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
2775 if (s
->contents
== NULL
)
2777 memset (s
->contents
, 0, (size_t) s
->_raw_size
);
2779 put_word (output_bfd
, bucketcount
, s
->contents
);
2780 put_word (output_bfd
, dynsymcount
, s
->contents
+ (ARCH_SIZE
/ 8));
2782 elf_hash_table (info
)->bucketcount
= bucketcount
;
2784 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
2785 BFD_ASSERT (s
!= NULL
);
2786 s
->_raw_size
= _bfd_stringtab_size (elf_hash_table (info
)->dynstr
);
2788 if (! elf_add_dynamic_entry (info
, DT_NULL
, 0))
2795 /* Fix up the flags for a symbol. This handles various cases which
2796 can only be fixed after all the input files are seen. This is
2797 currently called by both adjust_dynamic_symbol and
2798 assign_sym_version, which is unnecessary but perhaps more robust in
2799 the face of future changes. */
2802 elf_fix_symbol_flags (h
, eif
)
2803 struct elf_link_hash_entry
*h
;
2804 struct elf_info_failed
*eif
;
2806 /* If this symbol was mentioned in a non-ELF file, try to set
2807 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2808 permit a non-ELF file to correctly refer to a symbol defined in
2809 an ELF dynamic object. */
2810 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) != 0)
2812 if (h
->root
.type
!= bfd_link_hash_defined
2813 && h
->root
.type
!= bfd_link_hash_defweak
)
2814 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2817 if (h
->root
.u
.def
.section
->owner
!= NULL
2818 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2819 == bfd_target_elf_flavour
))
2820 h
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2822 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2825 if (h
->dynindx
== -1
2826 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
2827 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0))
2829 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2837 /* If this is a final link, and the symbol was defined as a common
2838 symbol in a regular object file, and there was no definition in
2839 any dynamic object, then the linker will have allocated space for
2840 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2841 flag will not have been set. */
2842 if (h
->root
.type
== bfd_link_hash_defined
2843 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
2844 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
2845 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2846 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2847 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
2849 /* If -Bsymbolic was used (which means to bind references to global
2850 symbols to the definition within the shared object), and this
2851 symbol was defined in a regular object, then it actually doesn't
2852 need a PLT entry. */
2853 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0
2854 && eif
->info
->shared
2855 && eif
->info
->symbolic
2856 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2858 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
2859 h
->plt
.offset
= (bfd_vma
) -1;
2865 /* Make the backend pick a good value for a dynamic symbol. This is
2866 called via elf_link_hash_traverse, and also calls itself
2870 elf_adjust_dynamic_symbol (h
, data
)
2871 struct elf_link_hash_entry
*h
;
2874 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2876 struct elf_backend_data
*bed
;
2878 /* Ignore indirect symbols. These are added by the versioning code. */
2879 if (h
->root
.type
== bfd_link_hash_indirect
)
2882 /* Fix the symbol flags. */
2883 if (! elf_fix_symbol_flags (h
, eif
))
2886 /* If this symbol does not require a PLT entry, and it is not
2887 defined by a dynamic object, or is not referenced by a regular
2888 object, ignore it. We do have to handle a weak defined symbol,
2889 even if no regular object refers to it, if we decided to add it
2890 to the dynamic symbol table. FIXME: Do we normally need to worry
2891 about symbols which are defined by one dynamic object and
2892 referenced by another one? */
2893 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) == 0
2894 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
2895 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
2896 || ((h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0
2897 && (h
->weakdef
== NULL
|| h
->weakdef
->dynindx
== -1))))
2899 h
->plt
.offset
= (bfd_vma
) -1;
2903 /* If we've already adjusted this symbol, don't do it again. This
2904 can happen via a recursive call. */
2905 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DYNAMIC_ADJUSTED
) != 0)
2908 /* Don't look at this symbol again. Note that we must set this
2909 after checking the above conditions, because we may look at a
2910 symbol once, decide not to do anything, and then get called
2911 recursively later after REF_REGULAR is set below. */
2912 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DYNAMIC_ADJUSTED
;
2914 /* If this is a weak definition, and we know a real definition, and
2915 the real symbol is not itself defined by a regular object file,
2916 then get a good value for the real definition. We handle the
2917 real symbol first, for the convenience of the backend routine.
2919 Note that there is a confusing case here. If the real definition
2920 is defined by a regular object file, we don't get the real symbol
2921 from the dynamic object, but we do get the weak symbol. If the
2922 processor backend uses a COPY reloc, then if some routine in the
2923 dynamic object changes the real symbol, we will not see that
2924 change in the corresponding weak symbol. This is the way other
2925 ELF linkers work as well, and seems to be a result of the shared
2928 I will clarify this issue. Most SVR4 shared libraries define the
2929 variable _timezone and define timezone as a weak synonym. The
2930 tzset call changes _timezone. If you write
2931 extern int timezone;
2933 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2934 you might expect that, since timezone is a synonym for _timezone,
2935 the same number will print both times. However, if the processor
2936 backend uses a COPY reloc, then actually timezone will be copied
2937 into your process image, and, since you define _timezone
2938 yourself, _timezone will not. Thus timezone and _timezone will
2939 wind up at different memory locations. The tzset call will set
2940 _timezone, leaving timezone unchanged. */
2942 if (h
->weakdef
!= NULL
)
2944 struct elf_link_hash_entry
*weakdef
;
2946 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2947 || h
->root
.type
== bfd_link_hash_defweak
);
2948 weakdef
= h
->weakdef
;
2949 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2950 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2951 BFD_ASSERT (weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
);
2952 if ((weakdef
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0)
2954 /* This symbol is defined by a regular object file, so we
2955 will not do anything special. Clear weakdef for the
2956 convenience of the processor backend. */
2961 /* There is an implicit reference by a regular object file
2962 via the weak symbol. */
2963 weakdef
->elf_link_hash_flags
|= ELF_LINK_HASH_REF_REGULAR
;
2964 if (! elf_adjust_dynamic_symbol (weakdef
, (PTR
) eif
))
2969 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2970 bed
= get_elf_backend_data (dynobj
);
2971 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2980 /* This routine is used to export all defined symbols into the dynamic
2981 symbol table. It is called via elf_link_hash_traverse. */
2984 elf_export_symbol (h
, data
)
2985 struct elf_link_hash_entry
*h
;
2988 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2990 /* Ignore indirect symbols. These are added by the versioning code. */
2991 if (h
->root
.type
== bfd_link_hash_indirect
)
2994 if (h
->dynindx
== -1
2995 && (h
->elf_link_hash_flags
2996 & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR
)) != 0)
2998 if (! _bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3008 /* Look through the symbols which are defined in other shared
3009 libraries and referenced here. Update the list of version
3010 dependencies. This will be put into the .gnu.version_r section.
3011 This function is called via elf_link_hash_traverse. */
3014 elf_link_find_version_dependencies (h
, data
)
3015 struct elf_link_hash_entry
*h
;
3018 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
3019 Elf_Internal_Verneed
*t
;
3020 Elf_Internal_Vernaux
*a
;
3022 /* We only care about symbols defined in shared objects with version
3024 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
3025 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
3027 || h
->verinfo
.verdef
== NULL
)
3030 /* See if we already know about this version. */
3031 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
3033 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
3036 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3037 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
3043 /* This is a new version. Add it to tree we are building. */
3047 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *t
);
3050 rinfo
->failed
= true;
3054 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
3055 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
3056 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
3059 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->output_bfd
, sizeof *a
);
3061 /* Note that we are copying a string pointer here, and testing it
3062 above. If bfd_elf_string_from_elf_section is ever changed to
3063 discard the string data when low in memory, this will have to be
3065 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
3067 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
3068 a
->vna_nextptr
= t
->vn_auxptr
;
3070 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
3073 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
3080 /* Figure out appropriate versions for all the symbols. We may not
3081 have the version number script until we have read all of the input
3082 files, so until that point we don't know which symbols should be
3083 local. This function is called via elf_link_hash_traverse. */
3086 elf_link_assign_sym_version (h
, data
)
3087 struct elf_link_hash_entry
*h
;
3090 struct elf_assign_sym_version_info
*sinfo
=
3091 (struct elf_assign_sym_version_info
*) data
;
3092 struct bfd_link_info
*info
= sinfo
->info
;
3093 struct elf_info_failed eif
;
3096 /* Fix the symbol flags. */
3099 if (! elf_fix_symbol_flags (h
, &eif
))
3102 sinfo
->failed
= true;
3106 /* We only need version numbers for symbols defined in regular
3108 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
3111 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3112 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
3114 struct bfd_elf_version_tree
*t
;
3119 /* There are two consecutive ELF_VER_CHR characters if this is
3120 not a hidden symbol. */
3122 if (*p
== ELF_VER_CHR
)
3128 /* If there is no version string, we can just return out. */
3132 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
3136 /* Look for the version. If we find it, it is no longer weak. */
3137 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
3139 if (strcmp (t
->name
, p
) == 0)
3143 struct bfd_elf_version_expr
*d
;
3145 len
= p
- h
->root
.root
.string
;
3146 alc
= bfd_alloc (sinfo
->output_bfd
, len
);
3149 strncpy (alc
, h
->root
.root
.string
, len
- 1);
3150 alc
[len
- 1] = '\0';
3151 if (alc
[len
- 2] == ELF_VER_CHR
)
3152 alc
[len
- 2] = '\0';
3154 h
->verinfo
.vertree
= t
;
3158 if (t
->globals
!= NULL
)
3160 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3162 if ((d
->match
[0] == '*' && d
->match
[1] == '\0')
3163 || fnmatch (d
->match
, alc
, 0) == 0)
3168 /* See if there is anything to force this symbol to
3170 if (d
== NULL
&& t
->locals
!= NULL
)
3172 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3174 if ((d
->match
[0] == '*' && d
->match
[1] == '\0')
3175 || fnmatch (d
->match
, alc
, 0) == 0)
3177 if (h
->dynindx
!= -1
3179 && ! sinfo
->export_dynamic
)
3181 sinfo
->removed_dynamic
= true;
3182 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3183 h
->elf_link_hash_flags
&=~
3184 ELF_LINK_HASH_NEEDS_PLT
;
3186 h
->plt
.offset
= (bfd_vma
) -1;
3187 /* FIXME: The name of the symbol has
3188 already been recorded in the dynamic
3189 string table section. */
3197 bfd_release (sinfo
->output_bfd
, alc
);
3202 /* If we are building an application, we need to create a
3203 version node for this version. */
3204 if (t
== NULL
&& ! info
->shared
)
3206 struct bfd_elf_version_tree
**pp
;
3209 /* If we aren't going to export this symbol, we don't need
3210 to worry about it. */
3211 if (h
->dynindx
== -1)
3214 t
= ((struct bfd_elf_version_tree
*)
3215 bfd_alloc (sinfo
->output_bfd
, sizeof *t
));
3218 sinfo
->failed
= true;
3227 t
->name_indx
= (unsigned int) -1;
3231 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
3233 t
->vernum
= version_index
;
3237 h
->verinfo
.vertree
= t
;
3241 /* We could not find the version for a symbol when
3242 generating a shared archive. Return an error. */
3243 (*_bfd_error_handler
)
3244 (_("%s: undefined versioned symbol name %s"),
3245 bfd_get_filename (sinfo
->output_bfd
), h
->root
.root
.string
);
3246 bfd_set_error (bfd_error_bad_value
);
3247 sinfo
->failed
= true;
3252 h
->elf_link_hash_flags
|= ELF_LINK_HIDDEN
;
3255 /* If we don't have a version for this symbol, see if we can find
3257 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
3259 struct bfd_elf_version_tree
*t
;
3260 struct bfd_elf_version_tree
*deflt
;
3261 struct bfd_elf_version_expr
*d
;
3263 /* See if can find what version this symbol is in. If the
3264 symbol is supposed to be local, then don't actually register
3267 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
3269 if (t
->globals
!= NULL
)
3271 for (d
= t
->globals
; d
!= NULL
; d
= d
->next
)
3273 if (fnmatch (d
->match
, h
->root
.root
.string
, 0) == 0)
3275 h
->verinfo
.vertree
= t
;
3284 if (t
->locals
!= NULL
)
3286 for (d
= t
->locals
; d
!= NULL
; d
= d
->next
)
3288 if (d
->match
[0] == '*' && d
->match
[1] == '\0')
3290 else if (fnmatch (d
->match
, h
->root
.root
.string
, 0) == 0)
3292 h
->verinfo
.vertree
= t
;
3293 if (h
->dynindx
!= -1
3295 && ! sinfo
->export_dynamic
)
3297 sinfo
->removed_dynamic
= true;
3298 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3299 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
3301 h
->plt
.offset
= (bfd_vma
) -1;
3302 /* FIXME: The name of the symbol has already
3303 been recorded in the dynamic string table
3315 if (deflt
!= NULL
&& h
->verinfo
.vertree
== NULL
)
3317 h
->verinfo
.vertree
= deflt
;
3318 if (h
->dynindx
!= -1
3320 && ! sinfo
->export_dynamic
)
3322 sinfo
->removed_dynamic
= true;
3323 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
3324 h
->elf_link_hash_flags
&=~ ELF_LINK_HASH_NEEDS_PLT
;
3326 h
->plt
.offset
= (bfd_vma
) -1;
3327 /* FIXME: The name of the symbol has already been
3328 recorded in the dynamic string table section. */
3336 /* This function is used to renumber the dynamic symbols, if some of
3337 them are removed because they are marked as local. This is called
3338 via elf_link_hash_traverse. */
3341 elf_link_renumber_dynsyms (h
, data
)
3342 struct elf_link_hash_entry
*h
;
3345 struct bfd_link_info
*info
= (struct bfd_link_info
*) data
;
3347 if (h
->dynindx
!= -1)
3349 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
3350 ++elf_hash_table (info
)->dynsymcount
;
3356 /* Final phase of ELF linker. */
3358 /* A structure we use to avoid passing large numbers of arguments. */
3360 struct elf_final_link_info
3362 /* General link information. */
3363 struct bfd_link_info
*info
;
3366 /* Symbol string table. */
3367 struct bfd_strtab_hash
*symstrtab
;
3368 /* .dynsym section. */
3369 asection
*dynsym_sec
;
3370 /* .hash section. */
3372 /* symbol version section (.gnu.version). */
3373 asection
*symver_sec
;
3374 /* Buffer large enough to hold contents of any section. */
3376 /* Buffer large enough to hold external relocs of any section. */
3377 PTR external_relocs
;
3378 /* Buffer large enough to hold internal relocs of any section. */
3379 Elf_Internal_Rela
*internal_relocs
;
3380 /* Buffer large enough to hold external local symbols of any input
3382 Elf_External_Sym
*external_syms
;
3383 /* Buffer large enough to hold internal local symbols of any input
3385 Elf_Internal_Sym
*internal_syms
;
3386 /* Array large enough to hold a symbol index for each local symbol
3387 of any input BFD. */
3389 /* Array large enough to hold a section pointer for each local
3390 symbol of any input BFD. */
3391 asection
**sections
;
3392 /* Buffer to hold swapped out symbols. */
3393 Elf_External_Sym
*symbuf
;
3394 /* Number of swapped out symbols in buffer. */
3395 size_t symbuf_count
;
3396 /* Number of symbols which fit in symbuf. */
3400 static boolean elf_link_output_sym
3401 PARAMS ((struct elf_final_link_info
*, const char *,
3402 Elf_Internal_Sym
*, asection
*));
3403 static boolean elf_link_flush_output_syms
3404 PARAMS ((struct elf_final_link_info
*));
3405 static boolean elf_link_output_extsym
3406 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3407 static boolean elf_link_input_bfd
3408 PARAMS ((struct elf_final_link_info
*, bfd
*));
3409 static boolean elf_reloc_link_order
3410 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
3411 struct bfd_link_order
*));
3413 /* This struct is used to pass information to elf_link_output_extsym. */
3415 struct elf_outext_info
3419 struct elf_final_link_info
*finfo
;
3422 /* Do the final step of an ELF link. */
3425 elf_bfd_final_link (abfd
, info
)
3427 struct bfd_link_info
*info
;
3431 struct elf_final_link_info finfo
;
3432 register asection
*o
;
3433 register struct bfd_link_order
*p
;
3435 size_t max_contents_size
;
3436 size_t max_external_reloc_size
;
3437 size_t max_internal_reloc_count
;
3438 size_t max_sym_count
;
3440 Elf_Internal_Sym elfsym
;
3442 Elf_Internal_Shdr
*symtab_hdr
;
3443 Elf_Internal_Shdr
*symstrtab_hdr
;
3444 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3445 struct elf_outext_info eoinfo
;
3448 abfd
->flags
|= DYNAMIC
;
3450 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
3451 dynobj
= elf_hash_table (info
)->dynobj
;
3454 finfo
.output_bfd
= abfd
;
3455 finfo
.symstrtab
= elf_stringtab_init ();
3456 if (finfo
.symstrtab
== NULL
)
3461 finfo
.dynsym_sec
= NULL
;
3462 finfo
.hash_sec
= NULL
;
3463 finfo
.symver_sec
= NULL
;
3467 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
3468 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
3469 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
3470 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
3471 /* Note that it is OK if symver_sec is NULL. */
3474 finfo
.contents
= NULL
;
3475 finfo
.external_relocs
= NULL
;
3476 finfo
.internal_relocs
= NULL
;
3477 finfo
.external_syms
= NULL
;
3478 finfo
.internal_syms
= NULL
;
3479 finfo
.indices
= NULL
;
3480 finfo
.sections
= NULL
;
3481 finfo
.symbuf
= NULL
;
3482 finfo
.symbuf_count
= 0;
3484 /* Count up the number of relocations we will output for each output
3485 section, so that we know the sizes of the reloc sections. We
3486 also figure out some maximum sizes. */
3487 max_contents_size
= 0;
3488 max_external_reloc_size
= 0;
3489 max_internal_reloc_count
= 0;
3491 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3495 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3497 if (p
->type
== bfd_section_reloc_link_order
3498 || p
->type
== bfd_symbol_reloc_link_order
)
3500 else if (p
->type
== bfd_indirect_link_order
)
3504 sec
= p
->u
.indirect
.section
;
3506 /* Mark all sections which are to be included in the
3507 link. This will normally be every section. We need
3508 to do this so that we can identify any sections which
3509 the linker has decided to not include. */
3510 sec
->linker_mark
= true;
3512 if (info
->relocateable
)
3513 o
->reloc_count
+= sec
->reloc_count
;
3515 if (sec
->_raw_size
> max_contents_size
)
3516 max_contents_size
= sec
->_raw_size
;
3517 if (sec
->_cooked_size
> max_contents_size
)
3518 max_contents_size
= sec
->_cooked_size
;
3520 /* We are interested in just local symbols, not all
3522 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
3523 && (sec
->owner
->flags
& DYNAMIC
) == 0)
3527 if (elf_bad_symtab (sec
->owner
))
3528 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
3529 / sizeof (Elf_External_Sym
));
3531 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
3533 if (sym_count
> max_sym_count
)
3534 max_sym_count
= sym_count
;
3536 if ((sec
->flags
& SEC_RELOC
) != 0)
3540 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
3541 if (ext_size
> max_external_reloc_size
)
3542 max_external_reloc_size
= ext_size
;
3543 if (sec
->reloc_count
> max_internal_reloc_count
)
3544 max_internal_reloc_count
= sec
->reloc_count
;
3550 if (o
->reloc_count
> 0)
3551 o
->flags
|= SEC_RELOC
;
3554 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3555 set it (this is probably a bug) and if it is set
3556 assign_section_numbers will create a reloc section. */
3557 o
->flags
&=~ SEC_RELOC
;
3560 /* If the SEC_ALLOC flag is not set, force the section VMA to
3561 zero. This is done in elf_fake_sections as well, but forcing
3562 the VMA to 0 here will ensure that relocs against these
3563 sections are handled correctly. */
3564 if ((o
->flags
& SEC_ALLOC
) == 0
3565 && ! o
->user_set_vma
)
3569 /* Figure out the file positions for everything but the symbol table
3570 and the relocs. We set symcount to force assign_section_numbers
3571 to create a symbol table. */
3572 abfd
->symcount
= info
->strip
== strip_all
? 0 : 1;
3573 BFD_ASSERT (! abfd
->output_has_begun
);
3574 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
3577 /* That created the reloc sections. Set their sizes, and assign
3578 them file positions, and allocate some buffers. */
3579 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3581 if ((o
->flags
& SEC_RELOC
) != 0)
3583 Elf_Internal_Shdr
*rel_hdr
;
3584 register struct elf_link_hash_entry
**p
, **pend
;
3586 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
3588 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* o
->reloc_count
;
3590 /* The contents field must last into write_object_contents,
3591 so we allocate it with bfd_alloc rather than malloc. */
3592 rel_hdr
->contents
= (PTR
) bfd_alloc (abfd
, rel_hdr
->sh_size
);
3593 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
3596 p
= ((struct elf_link_hash_entry
**)
3597 bfd_malloc (o
->reloc_count
3598 * sizeof (struct elf_link_hash_entry
*)));
3599 if (p
== NULL
&& o
->reloc_count
!= 0)
3601 elf_section_data (o
)->rel_hashes
= p
;
3602 pend
= p
+ o
->reloc_count
;
3603 for (; p
< pend
; p
++)
3606 /* Use the reloc_count field as an index when outputting the
3612 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3614 /* We have now assigned file positions for all the sections except
3615 .symtab and .strtab. We start the .symtab section at the current
3616 file position, and write directly to it. We build the .strtab
3617 section in memory. */
3619 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3620 /* sh_name is set in prep_headers. */
3621 symtab_hdr
->sh_type
= SHT_SYMTAB
;
3622 symtab_hdr
->sh_flags
= 0;
3623 symtab_hdr
->sh_addr
= 0;
3624 symtab_hdr
->sh_size
= 0;
3625 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
3626 /* sh_link is set in assign_section_numbers. */
3627 /* sh_info is set below. */
3628 /* sh_offset is set just below. */
3629 symtab_hdr
->sh_addralign
= 4; /* FIXME: system dependent? */
3631 off
= elf_tdata (abfd
)->next_file_pos
;
3632 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
3634 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3635 incorrect. We do not yet know the size of the .symtab section.
3636 We correct next_file_pos below, after we do know the size. */
3638 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3639 continuously seeking to the right position in the file. */
3640 if (! info
->keep_memory
|| max_sym_count
< 20)
3641 finfo
.symbuf_size
= 20;
3643 finfo
.symbuf_size
= max_sym_count
;
3644 finfo
.symbuf
= ((Elf_External_Sym
*)
3645 bfd_malloc (finfo
.symbuf_size
* sizeof (Elf_External_Sym
)));
3646 if (finfo
.symbuf
== NULL
)
3649 /* Start writing out the symbol table. The first symbol is always a
3651 if (info
->strip
!= strip_all
|| info
->relocateable
)
3653 elfsym
.st_value
= 0;
3656 elfsym
.st_other
= 0;
3657 elfsym
.st_shndx
= SHN_UNDEF
;
3658 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
3659 &elfsym
, bfd_und_section_ptr
))
3664 /* Some standard ELF linkers do this, but we don't because it causes
3665 bootstrap comparison failures. */
3666 /* Output a file symbol for the output file as the second symbol.
3667 We output this even if we are discarding local symbols, although
3668 I'm not sure if this is correct. */
3669 elfsym
.st_value
= 0;
3671 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
3672 elfsym
.st_other
= 0;
3673 elfsym
.st_shndx
= SHN_ABS
;
3674 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
3675 &elfsym
, bfd_abs_section_ptr
))
3679 /* Output a symbol for each section. We output these even if we are
3680 discarding local symbols, since they are used for relocs. These
3681 symbols have no names. We store the index of each one in the
3682 index field of the section, so that we can find it again when
3683 outputting relocs. */
3684 if (info
->strip
!= strip_all
|| info
->relocateable
)
3687 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
3688 elfsym
.st_other
= 0;
3689 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
3691 o
= section_from_elf_index (abfd
, i
);
3693 o
->target_index
= abfd
->symcount
;
3694 elfsym
.st_shndx
= i
;
3695 if (info
->relocateable
|| o
== NULL
)
3696 elfsym
.st_value
= 0;
3698 elfsym
.st_value
= o
->vma
;
3699 if (! elf_link_output_sym (&finfo
, (const char *) NULL
,
3705 /* Allocate some memory to hold information read in from the input
3707 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
3708 finfo
.external_relocs
= (PTR
) bfd_malloc (max_external_reloc_size
);
3709 finfo
.internal_relocs
= ((Elf_Internal_Rela
*)
3710 bfd_malloc (max_internal_reloc_count
3711 * sizeof (Elf_Internal_Rela
)));
3712 finfo
.external_syms
= ((Elf_External_Sym
*)
3713 bfd_malloc (max_sym_count
3714 * sizeof (Elf_External_Sym
)));
3715 finfo
.internal_syms
= ((Elf_Internal_Sym
*)
3716 bfd_malloc (max_sym_count
3717 * sizeof (Elf_Internal_Sym
)));
3718 finfo
.indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
3719 finfo
.sections
= ((asection
**)
3720 bfd_malloc (max_sym_count
* sizeof (asection
*)));
3721 if ((finfo
.contents
== NULL
&& max_contents_size
!= 0)
3722 || (finfo
.external_relocs
== NULL
&& max_external_reloc_size
!= 0)
3723 || (finfo
.internal_relocs
== NULL
&& max_internal_reloc_count
!= 0)
3724 || (finfo
.external_syms
== NULL
&& max_sym_count
!= 0)
3725 || (finfo
.internal_syms
== NULL
&& max_sym_count
!= 0)
3726 || (finfo
.indices
== NULL
&& max_sym_count
!= 0)
3727 || (finfo
.sections
== NULL
&& max_sym_count
!= 0))
3730 /* Since ELF permits relocations to be against local symbols, we
3731 must have the local symbols available when we do the relocations.
3732 Since we would rather only read the local symbols once, and we
3733 would rather not keep them in memory, we handle all the
3734 relocations for a single input file at the same time.
3736 Unfortunately, there is no way to know the total number of local
3737 symbols until we have seen all of them, and the local symbol
3738 indices precede the global symbol indices. This means that when
3739 we are generating relocateable output, and we see a reloc against
3740 a global symbol, we can not know the symbol index until we have
3741 finished examining all the local symbols to see which ones we are
3742 going to output. To deal with this, we keep the relocations in
3743 memory, and don't output them until the end of the link. This is
3744 an unfortunate waste of memory, but I don't see a good way around
3745 it. Fortunately, it only happens when performing a relocateable
3746 link, which is not the common case. FIXME: If keep_memory is set
3747 we could write the relocs out and then read them again; I don't
3748 know how bad the memory loss will be. */
3750 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3751 sub
->output_has_begun
= false;
3752 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3754 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3756 if (p
->type
== bfd_indirect_link_order
3757 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
3758 == bfd_target_elf_flavour
))
3760 sub
= p
->u
.indirect
.section
->owner
;
3761 if (! sub
->output_has_begun
)
3763 if (! elf_link_input_bfd (&finfo
, sub
))
3765 sub
->output_has_begun
= true;
3768 else if (p
->type
== bfd_section_reloc_link_order
3769 || p
->type
== bfd_symbol_reloc_link_order
)
3771 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
3776 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
3782 /* That wrote out all the local symbols. Finish up the symbol table
3783 with the global symbols. */
3785 if (info
->strip
!= strip_all
&& info
->shared
)
3787 /* Output any global symbols that got converted to local in a
3788 version script. We do this in a separate step since ELF
3789 requires all local symbols to appear prior to any global
3790 symbols. FIXME: We should only do this if some global
3791 symbols were, in fact, converted to become local. FIXME:
3792 Will this work correctly with the Irix 5 linker? */
3793 eoinfo
.failed
= false;
3794 eoinfo
.finfo
= &finfo
;
3795 eoinfo
.localsyms
= true;
3796 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3802 /* The sh_info field records the index of the first non local
3804 symtab_hdr
->sh_info
= abfd
->symcount
;
3806 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
= 1;
3808 /* We get the global symbols from the hash table. */
3809 eoinfo
.failed
= false;
3810 eoinfo
.localsyms
= false;
3811 eoinfo
.finfo
= &finfo
;
3812 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
3817 /* Flush all symbols to the file. */
3818 if (! elf_link_flush_output_syms (&finfo
))
3821 /* Now we know the size of the symtab section. */
3822 off
+= symtab_hdr
->sh_size
;
3824 /* Finish up and write out the symbol string table (.strtab)
3826 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3827 /* sh_name was set in prep_headers. */
3828 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
3829 symstrtab_hdr
->sh_flags
= 0;
3830 symstrtab_hdr
->sh_addr
= 0;
3831 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
3832 symstrtab_hdr
->sh_entsize
= 0;
3833 symstrtab_hdr
->sh_link
= 0;
3834 symstrtab_hdr
->sh_info
= 0;
3835 /* sh_offset is set just below. */
3836 symstrtab_hdr
->sh_addralign
= 1;
3838 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, true);
3839 elf_tdata (abfd
)->next_file_pos
= off
;
3841 if (abfd
->symcount
> 0)
3843 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
3844 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
3848 /* Adjust the relocs to have the correct symbol indices. */
3849 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3851 struct elf_link_hash_entry
**rel_hash
;
3852 Elf_Internal_Shdr
*rel_hdr
;
3854 if ((o
->flags
& SEC_RELOC
) == 0)
3857 rel_hash
= elf_section_data (o
)->rel_hashes
;
3858 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
3859 for (i
= 0; i
< o
->reloc_count
; i
++, rel_hash
++)
3861 if (*rel_hash
== NULL
)
3864 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
3866 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
3868 Elf_External_Rel
*erel
;
3869 Elf_Internal_Rel irel
;
3871 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
3872 elf_swap_reloc_in (abfd
, erel
, &irel
);
3873 irel
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
3874 ELF_R_TYPE (irel
.r_info
));
3875 elf_swap_reloc_out (abfd
, &irel
, erel
);
3879 Elf_External_Rela
*erela
;
3880 Elf_Internal_Rela irela
;
3882 BFD_ASSERT (rel_hdr
->sh_entsize
3883 == sizeof (Elf_External_Rela
));
3885 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
3886 elf_swap_reloca_in (abfd
, erela
, &irela
);
3887 irela
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
3888 ELF_R_TYPE (irela
.r_info
));
3889 elf_swap_reloca_out (abfd
, &irela
, erela
);
3893 /* Set the reloc_count field to 0 to prevent write_relocs from
3894 trying to swap the relocs out itself. */
3898 /* If we are linking against a dynamic object, or generating a
3899 shared library, finish up the dynamic linking information. */
3902 Elf_External_Dyn
*dyncon
, *dynconend
;
3904 /* Fix up .dynamic entries. */
3905 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
3906 BFD_ASSERT (o
!= NULL
);
3908 dyncon
= (Elf_External_Dyn
*) o
->contents
;
3909 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
3910 for (; dyncon
< dynconend
; dyncon
++)
3912 Elf_Internal_Dyn dyn
;
3916 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
3923 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on
3924 magic _init and _fini symbols. This is pretty ugly,
3925 but we are compatible. */
3933 struct elf_link_hash_entry
*h
;
3935 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
3936 false, false, true);
3938 && (h
->root
.type
== bfd_link_hash_defined
3939 || h
->root
.type
== bfd_link_hash_defweak
))
3941 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
3942 o
= h
->root
.u
.def
.section
;
3943 if (o
->output_section
!= NULL
)
3944 dyn
.d_un
.d_val
+= (o
->output_section
->vma
3945 + o
->output_offset
);
3948 /* The symbol is imported from another shared
3949 library and does not apply to this one. */
3953 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3968 name
= ".gnu.version_d";
3971 name
= ".gnu.version_r";
3974 name
= ".gnu.version";
3976 o
= bfd_get_section_by_name (abfd
, name
);
3977 BFD_ASSERT (o
!= NULL
);
3978 dyn
.d_un
.d_ptr
= o
->vma
;
3979 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
3986 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
3991 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
3993 Elf_Internal_Shdr
*hdr
;
3995 hdr
= elf_elfsections (abfd
)[i
];
3996 if (hdr
->sh_type
== type
3997 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
3999 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
4000 dyn
.d_un
.d_val
+= hdr
->sh_size
;
4003 if (dyn
.d_un
.d_val
== 0
4004 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
4005 dyn
.d_un
.d_val
= hdr
->sh_addr
;
4009 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
4015 /* If we have created any dynamic sections, then output them. */
4018 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
4021 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
4023 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4024 || o
->_raw_size
== 0)
4026 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
4028 /* At this point, we are only interested in sections
4029 created by elf_link_create_dynamic_sections. */
4032 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
4034 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
4036 if (! bfd_set_section_contents (abfd
, o
->output_section
,
4037 o
->contents
, o
->output_offset
,
4045 /* The contents of the .dynstr section are actually in a
4047 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
4048 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
4049 || ! _bfd_stringtab_emit (abfd
,
4050 elf_hash_table (info
)->dynstr
))
4056 /* If we have optimized stabs strings, output them. */
4057 if (elf_hash_table (info
)->stab_info
!= NULL
)
4059 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
4063 if (finfo
.symstrtab
!= NULL
)
4064 _bfd_stringtab_free (finfo
.symstrtab
);
4065 if (finfo
.contents
!= NULL
)
4066 free (finfo
.contents
);
4067 if (finfo
.external_relocs
!= NULL
)
4068 free (finfo
.external_relocs
);
4069 if (finfo
.internal_relocs
!= NULL
)
4070 free (finfo
.internal_relocs
);
4071 if (finfo
.external_syms
!= NULL
)
4072 free (finfo
.external_syms
);
4073 if (finfo
.internal_syms
!= NULL
)
4074 free (finfo
.internal_syms
);
4075 if (finfo
.indices
!= NULL
)
4076 free (finfo
.indices
);
4077 if (finfo
.sections
!= NULL
)
4078 free (finfo
.sections
);
4079 if (finfo
.symbuf
!= NULL
)
4080 free (finfo
.symbuf
);
4081 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4083 if ((o
->flags
& SEC_RELOC
) != 0
4084 && elf_section_data (o
)->rel_hashes
!= NULL
)
4085 free (elf_section_data (o
)->rel_hashes
);
4088 elf_tdata (abfd
)->linker
= true;
4093 if (finfo
.symstrtab
!= NULL
)
4094 _bfd_stringtab_free (finfo
.symstrtab
);
4095 if (finfo
.contents
!= NULL
)
4096 free (finfo
.contents
);
4097 if (finfo
.external_relocs
!= NULL
)
4098 free (finfo
.external_relocs
);
4099 if (finfo
.internal_relocs
!= NULL
)
4100 free (finfo
.internal_relocs
);
4101 if (finfo
.external_syms
!= NULL
)
4102 free (finfo
.external_syms
);
4103 if (finfo
.internal_syms
!= NULL
)
4104 free (finfo
.internal_syms
);
4105 if (finfo
.indices
!= NULL
)
4106 free (finfo
.indices
);
4107 if (finfo
.sections
!= NULL
)
4108 free (finfo
.sections
);
4109 if (finfo
.symbuf
!= NULL
)
4110 free (finfo
.symbuf
);
4111 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4113 if ((o
->flags
& SEC_RELOC
) != 0
4114 && elf_section_data (o
)->rel_hashes
!= NULL
)
4115 free (elf_section_data (o
)->rel_hashes
);
4121 /* Add a symbol to the output symbol table. */
4124 elf_link_output_sym (finfo
, name
, elfsym
, input_sec
)
4125 struct elf_final_link_info
*finfo
;
4127 Elf_Internal_Sym
*elfsym
;
4128 asection
*input_sec
;
4130 boolean (*output_symbol_hook
) PARAMS ((bfd
*,
4131 struct bfd_link_info
*info
,
4136 output_symbol_hook
= get_elf_backend_data (finfo
->output_bfd
)->
4137 elf_backend_link_output_symbol_hook
;
4138 if (output_symbol_hook
!= NULL
)
4140 if (! ((*output_symbol_hook
)
4141 (finfo
->output_bfd
, finfo
->info
, name
, elfsym
, input_sec
)))
4145 if (name
== (const char *) NULL
|| *name
== '\0')
4146 elfsym
->st_name
= 0;
4149 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
4152 if (elfsym
->st_name
== (unsigned long) -1)
4156 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
4158 if (! elf_link_flush_output_syms (finfo
))
4162 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
,
4163 (PTR
) (finfo
->symbuf
+ finfo
->symbuf_count
));
4164 ++finfo
->symbuf_count
;
4166 ++finfo
->output_bfd
->symcount
;
4171 /* Flush the output symbols to the file. */
4174 elf_link_flush_output_syms (finfo
)
4175 struct elf_final_link_info
*finfo
;
4177 if (finfo
->symbuf_count
> 0)
4179 Elf_Internal_Shdr
*symtab
;
4181 symtab
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
4183 if (bfd_seek (finfo
->output_bfd
, symtab
->sh_offset
+ symtab
->sh_size
,
4185 || (bfd_write ((PTR
) finfo
->symbuf
, finfo
->symbuf_count
,
4186 sizeof (Elf_External_Sym
), finfo
->output_bfd
)
4187 != finfo
->symbuf_count
* sizeof (Elf_External_Sym
)))
4190 symtab
->sh_size
+= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
4192 finfo
->symbuf_count
= 0;
4198 /* Add an external symbol to the symbol table. This is called from
4199 the hash table traversal routine. When generating a shared object,
4200 we go through the symbol table twice. The first time we output
4201 anything that might have been forced to local scope in a version
4202 script. The second time we output the symbols that are still
4206 elf_link_output_extsym (h
, data
)
4207 struct elf_link_hash_entry
*h
;
4210 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
4211 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
4213 Elf_Internal_Sym sym
;
4214 asection
*input_sec
;
4216 /* Decide whether to output this symbol in this pass. */
4217 if (eoinfo
->localsyms
)
4219 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
4224 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4228 /* If we are not creating a shared library, and this symbol is
4229 referenced by a shared library but is not defined anywhere, then
4230 warn that it is undefined. If we do not do this, the runtime
4231 linker will complain that the symbol is undefined when the
4232 program is run. We don't have to worry about symbols that are
4233 referenced by regular files, because we will already have issued
4234 warnings for them. */
4235 if (! finfo
->info
->relocateable
4236 && ! finfo
->info
->shared
4237 && h
->root
.type
== bfd_link_hash_undefined
4238 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0
4239 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
4241 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
4242 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
4243 (asection
*) NULL
, 0)))
4245 eoinfo
->failed
= true;
4250 /* We don't want to output symbols that have never been mentioned by
4251 a regular file, or that we have been told to strip. However, if
4252 h->indx is set to -2, the symbol is used by a reloc and we must
4256 else if (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
4257 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) != 0)
4258 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
4259 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
4261 else if (finfo
->info
->strip
== strip_all
4262 || (finfo
->info
->strip
== strip_some
4263 && bfd_hash_lookup (finfo
->info
->keep_hash
,
4264 h
->root
.root
.string
,
4265 false, false) == NULL
))
4270 /* If we're stripping it, and it's not a dynamic symbol, there's
4271 nothing else to do. */
4272 if (strip
&& h
->dynindx
== -1)
4276 sym
.st_size
= h
->size
;
4277 sym
.st_other
= h
->other
;
4278 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4279 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
4280 else if (h
->root
.type
== bfd_link_hash_undefweak
4281 || h
->root
.type
== bfd_link_hash_defweak
)
4282 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
4284 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
4286 switch (h
->root
.type
)
4289 case bfd_link_hash_new
:
4293 case bfd_link_hash_undefined
:
4294 input_sec
= bfd_und_section_ptr
;
4295 sym
.st_shndx
= SHN_UNDEF
;
4298 case bfd_link_hash_undefweak
:
4299 input_sec
= bfd_und_section_ptr
;
4300 sym
.st_shndx
= SHN_UNDEF
;
4303 case bfd_link_hash_defined
:
4304 case bfd_link_hash_defweak
:
4306 input_sec
= h
->root
.u
.def
.section
;
4307 if (input_sec
->output_section
!= NULL
)
4310 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
4311 input_sec
->output_section
);
4312 if (sym
.st_shndx
== (unsigned short) -1)
4314 (*_bfd_error_handler
)
4315 (_("%s: could not find output section %s for input section %s"),
4316 bfd_get_filename (finfo
->output_bfd
),
4317 input_sec
->output_section
->name
,
4319 eoinfo
->failed
= true;
4323 /* ELF symbols in relocateable files are section relative,
4324 but in nonrelocateable files they are virtual
4326 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
4327 if (! finfo
->info
->relocateable
)
4328 sym
.st_value
+= input_sec
->output_section
->vma
;
4332 BFD_ASSERT (input_sec
->owner
== NULL
4333 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
4334 sym
.st_shndx
= SHN_UNDEF
;
4335 input_sec
= bfd_und_section_ptr
;
4340 case bfd_link_hash_common
:
4341 input_sec
= h
->root
.u
.c
.p
->section
;
4342 sym
.st_shndx
= SHN_COMMON
;
4343 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
4346 case bfd_link_hash_indirect
:
4347 /* These symbols are created by symbol versioning. They point
4348 to the decorated version of the name. For example, if the
4349 symbol foo@@GNU_1.2 is the default, which should be used when
4350 foo is used with no version, then we add an indirect symbol
4351 foo which points to foo@@GNU_1.2. We ignore these symbols,
4352 since the indirected symbol is already in the hash table. If
4353 the indirect symbol is non-ELF, fall through and output it. */
4354 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_ELF
) == 0)
4358 case bfd_link_hash_warning
:
4359 /* We can't represent these symbols in ELF, although a warning
4360 symbol may have come from a .gnu.warning.SYMBOL section. We
4361 just put the target symbol in the hash table. If the target
4362 symbol does not really exist, don't do anything. */
4363 if (h
->root
.u
.i
.link
->type
== bfd_link_hash_new
)
4365 return (elf_link_output_extsym
4366 ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
));
4369 /* Give the processor backend a chance to tweak the symbol value,
4370 and also to finish up anything that needs to be done for this
4372 if ((h
->dynindx
!= -1
4373 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
4374 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4376 struct elf_backend_data
*bed
;
4378 bed
= get_elf_backend_data (finfo
->output_bfd
);
4379 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
4380 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
4382 eoinfo
->failed
= true;
4387 /* If this symbol should be put in the .dynsym section, then put it
4388 there now. We have already know the symbol index. We also fill
4389 in the entry in the .hash section. */
4390 if (h
->dynindx
!= -1
4391 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
4397 bfd_byte
*bucketpos
;
4400 sym
.st_name
= h
->dynstr_index
;
4402 elf_swap_symbol_out (finfo
->output_bfd
, &sym
,
4403 (PTR
) (((Elf_External_Sym
*)
4404 finfo
->dynsym_sec
->contents
)
4407 /* We didn't include the version string in the dynamic string
4408 table, so we must not consider it in the hash table. */
4409 name
= h
->root
.root
.string
;
4410 p
= strchr (name
, ELF_VER_CHR
);
4415 copy
= bfd_alloc (finfo
->output_bfd
, p
- name
+ 1);
4416 strncpy (copy
, name
, p
- name
);
4417 copy
[p
- name
] = '\0';
4421 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
4422 bucket
= bfd_elf_hash ((const unsigned char *) name
) % bucketcount
;
4423 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
4424 + (bucket
+ 2) * (ARCH_SIZE
/ 8));
4425 chain
= get_word (finfo
->output_bfd
, bucketpos
);
4426 put_word (finfo
->output_bfd
, h
->dynindx
, bucketpos
);
4427 put_word (finfo
->output_bfd
, chain
,
4428 ((bfd_byte
*) finfo
->hash_sec
->contents
4429 + (bucketcount
+ 2 + h
->dynindx
) * (ARCH_SIZE
/ 8)));
4432 bfd_release (finfo
->output_bfd
, copy
);
4434 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
4436 Elf_Internal_Versym iversym
;
4438 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4440 if (h
->verinfo
.verdef
== NULL
)
4441 iversym
.vs_vers
= 0;
4443 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
4447 if (h
->verinfo
.vertree
== NULL
)
4448 iversym
.vs_vers
= 1;
4450 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
4453 if ((h
->elf_link_hash_flags
& ELF_LINK_HIDDEN
) != 0)
4454 iversym
.vs_vers
|= VERSYM_HIDDEN
;
4456 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
,
4457 (((Elf_External_Versym
*)
4458 finfo
->symver_sec
->contents
)
4463 /* If we're stripping it, then it was just a dynamic symbol, and
4464 there's nothing else to do. */
4468 h
->indx
= finfo
->output_bfd
->symcount
;
4470 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
))
4472 eoinfo
->failed
= true;
4479 /* Link an input file into the linker output file. This function
4480 handles all the sections and relocations of the input file at once.
4481 This is so that we only have to read the local symbols once, and
4482 don't have to keep them in memory. */
4485 elf_link_input_bfd (finfo
, input_bfd
)
4486 struct elf_final_link_info
*finfo
;
4489 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
4490 bfd
*, asection
*, bfd_byte
*,
4491 Elf_Internal_Rela
*,
4492 Elf_Internal_Sym
*, asection
**));
4494 Elf_Internal_Shdr
*symtab_hdr
;
4497 Elf_External_Sym
*external_syms
;
4498 Elf_External_Sym
*esym
;
4499 Elf_External_Sym
*esymend
;
4500 Elf_Internal_Sym
*isym
;
4502 asection
**ppsection
;
4505 output_bfd
= finfo
->output_bfd
;
4507 get_elf_backend_data (output_bfd
)->elf_backend_relocate_section
;
4509 /* If this is a dynamic object, we don't want to do anything here:
4510 we don't want the local symbols, and we don't want the section
4512 if ((input_bfd
->flags
& DYNAMIC
) != 0)
4515 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
4516 if (elf_bad_symtab (input_bfd
))
4518 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
4523 locsymcount
= symtab_hdr
->sh_info
;
4524 extsymoff
= symtab_hdr
->sh_info
;
4527 /* Read the local symbols. */
4528 if (symtab_hdr
->contents
!= NULL
)
4529 external_syms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
4530 else if (locsymcount
== 0)
4531 external_syms
= NULL
;
4534 external_syms
= finfo
->external_syms
;
4535 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
4536 || (bfd_read (external_syms
, sizeof (Elf_External_Sym
),
4537 locsymcount
, input_bfd
)
4538 != locsymcount
* sizeof (Elf_External_Sym
)))
4542 /* Swap in the local symbols and write out the ones which we know
4543 are going into the output file. */
4544 esym
= external_syms
;
4545 esymend
= esym
+ locsymcount
;
4546 isym
= finfo
->internal_syms
;
4547 pindex
= finfo
->indices
;
4548 ppsection
= finfo
->sections
;
4549 for (; esym
< esymend
; esym
++, isym
++, pindex
++, ppsection
++)
4553 Elf_Internal_Sym osym
;
4555 elf_swap_symbol_in (input_bfd
, esym
, isym
);
4558 if (elf_bad_symtab (input_bfd
))
4560 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
4567 if (isym
->st_shndx
== SHN_UNDEF
)
4568 isec
= bfd_und_section_ptr
;
4569 else if (isym
->st_shndx
> 0 && isym
->st_shndx
< SHN_LORESERVE
)
4570 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
4571 else if (isym
->st_shndx
== SHN_ABS
)
4572 isec
= bfd_abs_section_ptr
;
4573 else if (isym
->st_shndx
== SHN_COMMON
)
4574 isec
= bfd_com_section_ptr
;
4583 /* Don't output the first, undefined, symbol. */
4584 if (esym
== external_syms
)
4587 /* If we are stripping all symbols, we don't want to output this
4589 if (finfo
->info
->strip
== strip_all
)
4592 /* We never output section symbols. Instead, we use the section
4593 symbol of the corresponding section in the output file. */
4594 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
4597 /* If we are discarding all local symbols, we don't want to
4598 output this one. If we are generating a relocateable output
4599 file, then some of the local symbols may be required by
4600 relocs; we output them below as we discover that they are
4602 if (finfo
->info
->discard
== discard_all
)
4605 /* If this symbol is defined in a section which we are
4606 discarding, we don't need to keep it, but note that
4607 linker_mark is only reliable for sections that have contents.
4608 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4609 as well as linker_mark. */
4610 if (isym
->st_shndx
> 0
4611 && isym
->st_shndx
< SHN_LORESERVE
4613 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
4614 || (! finfo
->info
->relocateable
4615 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
4618 /* Get the name of the symbol. */
4619 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
4624 /* See if we are discarding symbols with this name. */
4625 if ((finfo
->info
->strip
== strip_some
4626 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
4628 || (finfo
->info
->discard
== discard_l
4629 && bfd_is_local_label_name (input_bfd
, name
)))
4632 /* If we get here, we are going to output this symbol. */
4636 /* Adjust the section index for the output file. */
4637 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
4638 isec
->output_section
);
4639 if (osym
.st_shndx
== (unsigned short) -1)
4642 *pindex
= output_bfd
->symcount
;
4644 /* ELF symbols in relocateable files are section relative, but
4645 in executable files they are virtual addresses. Note that
4646 this code assumes that all ELF sections have an associated
4647 BFD section with a reasonable value for output_offset; below
4648 we assume that they also have a reasonable value for
4649 output_section. Any special sections must be set up to meet
4650 these requirements. */
4651 osym
.st_value
+= isec
->output_offset
;
4652 if (! finfo
->info
->relocateable
)
4653 osym
.st_value
+= isec
->output_section
->vma
;
4655 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
))
4659 /* Relocate the contents of each section. */
4660 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4664 if (! o
->linker_mark
)
4666 /* This section was omitted from the link. */
4670 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4671 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
4674 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
4676 /* Section was created by elf_link_create_dynamic_sections
4681 /* Get the contents of the section. They have been cached by a
4682 relaxation routine. Note that o is a section in an input
4683 file, so the contents field will not have been set by any of
4684 the routines which work on output files. */
4685 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
4686 contents
= elf_section_data (o
)->this_hdr
.contents
;
4689 contents
= finfo
->contents
;
4690 if (! bfd_get_section_contents (input_bfd
, o
, contents
,
4691 (file_ptr
) 0, o
->_raw_size
))
4695 if ((o
->flags
& SEC_RELOC
) != 0)
4697 Elf_Internal_Rela
*internal_relocs
;
4699 /* Get the swapped relocs. */
4700 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
4701 (input_bfd
, o
, finfo
->external_relocs
,
4702 finfo
->internal_relocs
, false));
4703 if (internal_relocs
== NULL
4704 && o
->reloc_count
> 0)
4707 /* Relocate the section by invoking a back end routine.
4709 The back end routine is responsible for adjusting the
4710 section contents as necessary, and (if using Rela relocs
4711 and generating a relocateable output file) adjusting the
4712 reloc addend as necessary.
4714 The back end routine does not have to worry about setting
4715 the reloc address or the reloc symbol index.
4717 The back end routine is given a pointer to the swapped in
4718 internal symbols, and can access the hash table entries
4719 for the external symbols via elf_sym_hashes (input_bfd).
4721 When generating relocateable output, the back end routine
4722 must handle STB_LOCAL/STT_SECTION symbols specially. The
4723 output symbol is going to be a section symbol
4724 corresponding to the output section, which will require
4725 the addend to be adjusted. */
4727 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
4728 input_bfd
, o
, contents
,
4730 finfo
->internal_syms
,
4734 if (finfo
->info
->relocateable
)
4736 Elf_Internal_Rela
*irela
;
4737 Elf_Internal_Rela
*irelaend
;
4738 struct elf_link_hash_entry
**rel_hash
;
4739 Elf_Internal_Shdr
*input_rel_hdr
;
4740 Elf_Internal_Shdr
*output_rel_hdr
;
4742 /* Adjust the reloc addresses and symbol indices. */
4744 irela
= internal_relocs
;
4745 irelaend
= irela
+ o
->reloc_count
;
4746 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
4747 + o
->output_section
->reloc_count
);
4748 for (; irela
< irelaend
; irela
++, rel_hash
++)
4750 unsigned long r_symndx
;
4751 Elf_Internal_Sym
*isym
;
4754 irela
->r_offset
+= o
->output_offset
;
4756 r_symndx
= ELF_R_SYM (irela
->r_info
);
4761 if (r_symndx
>= locsymcount
4762 || (elf_bad_symtab (input_bfd
)
4763 && finfo
->sections
[r_symndx
] == NULL
))
4765 struct elf_link_hash_entry
*rh
;
4768 /* This is a reloc against a global symbol. We
4769 have not yet output all the local symbols, so
4770 we do not know the symbol index of any global
4771 symbol. We set the rel_hash entry for this
4772 reloc to point to the global hash table entry
4773 for this symbol. The symbol index is then
4774 set at the end of elf_bfd_final_link. */
4775 indx
= r_symndx
- extsymoff
;
4776 rh
= elf_sym_hashes (input_bfd
)[indx
];
4777 while (rh
->root
.type
== bfd_link_hash_indirect
4778 || rh
->root
.type
== bfd_link_hash_warning
)
4779 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
4781 /* Setting the index to -2 tells
4782 elf_link_output_extsym that this symbol is
4784 BFD_ASSERT (rh
->indx
< 0);
4792 /* This is a reloc against a local symbol. */
4795 isym
= finfo
->internal_syms
+ r_symndx
;
4796 sec
= finfo
->sections
[r_symndx
];
4797 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
4799 /* I suppose the backend ought to fill in the
4800 section of any STT_SECTION symbol against a
4801 processor specific section. If we have
4802 discarded a section, the output_section will
4803 be the absolute section. */
4805 && (bfd_is_abs_section (sec
)
4806 || (sec
->output_section
!= NULL
4807 && bfd_is_abs_section (sec
->output_section
))))
4809 else if (sec
== NULL
|| sec
->owner
== NULL
)
4811 bfd_set_error (bfd_error_bad_value
);
4816 r_symndx
= sec
->output_section
->target_index
;
4817 BFD_ASSERT (r_symndx
!= 0);
4822 if (finfo
->indices
[r_symndx
] == -1)
4828 if (finfo
->info
->strip
== strip_all
)
4830 /* You can't do ld -r -s. */
4831 bfd_set_error (bfd_error_invalid_operation
);
4835 /* This symbol was skipped earlier, but
4836 since it is needed by a reloc, we
4837 must output it now. */
4838 link
= symtab_hdr
->sh_link
;
4839 name
= bfd_elf_string_from_elf_section (input_bfd
,
4845 osec
= sec
->output_section
;
4847 _bfd_elf_section_from_bfd_section (output_bfd
,
4849 if (isym
->st_shndx
== (unsigned short) -1)
4852 isym
->st_value
+= sec
->output_offset
;
4853 if (! finfo
->info
->relocateable
)
4854 isym
->st_value
+= osec
->vma
;
4856 finfo
->indices
[r_symndx
] = output_bfd
->symcount
;
4858 if (! elf_link_output_sym (finfo
, name
, isym
, sec
))
4862 r_symndx
= finfo
->indices
[r_symndx
];
4865 irela
->r_info
= ELF_R_INFO (r_symndx
,
4866 ELF_R_TYPE (irela
->r_info
));
4869 /* Swap out the relocs. */
4870 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
4871 output_rel_hdr
= &elf_section_data (o
->output_section
)->rel_hdr
;
4872 BFD_ASSERT (output_rel_hdr
->sh_entsize
4873 == input_rel_hdr
->sh_entsize
);
4874 irela
= internal_relocs
;
4875 irelaend
= irela
+ o
->reloc_count
;
4876 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4878 Elf_External_Rel
*erel
;
4880 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
4881 + o
->output_section
->reloc_count
);
4882 for (; irela
< irelaend
; irela
++, erel
++)
4884 Elf_Internal_Rel irel
;
4886 irel
.r_offset
= irela
->r_offset
;
4887 irel
.r_info
= irela
->r_info
;
4888 BFD_ASSERT (irela
->r_addend
== 0);
4889 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
4894 Elf_External_Rela
*erela
;
4896 BFD_ASSERT (input_rel_hdr
->sh_entsize
4897 == sizeof (Elf_External_Rela
));
4898 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
4899 + o
->output_section
->reloc_count
);
4900 for (; irela
< irelaend
; irela
++, erela
++)
4901 elf_swap_reloca_out (output_bfd
, irela
, erela
);
4904 o
->output_section
->reloc_count
+= o
->reloc_count
;
4908 /* Write out the modified section contents. */
4909 if (elf_section_data (o
)->stab_info
== NULL
)
4911 if (! (o
->flags
& SEC_EXCLUDE
) &&
4912 ! bfd_set_section_contents (output_bfd
, o
->output_section
,
4913 contents
, o
->output_offset
,
4914 (o
->_cooked_size
!= 0
4921 if (! (_bfd_write_section_stabs
4922 (output_bfd
, &elf_hash_table (finfo
->info
)->stab_info
,
4923 o
, &elf_section_data (o
)->stab_info
, contents
)))
4931 /* Generate a reloc when linking an ELF file. This is a reloc
4932 requested by the linker, and does come from any input file. This
4933 is used to build constructor and destructor tables when linking
4937 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
4939 struct bfd_link_info
*info
;
4940 asection
*output_section
;
4941 struct bfd_link_order
*link_order
;
4943 reloc_howto_type
*howto
;
4947 struct elf_link_hash_entry
**rel_hash_ptr
;
4948 Elf_Internal_Shdr
*rel_hdr
;
4950 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
4953 bfd_set_error (bfd_error_bad_value
);
4957 addend
= link_order
->u
.reloc
.p
->addend
;
4959 /* Figure out the symbol index. */
4960 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
4961 + output_section
->reloc_count
);
4962 if (link_order
->type
== bfd_section_reloc_link_order
)
4964 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
4965 BFD_ASSERT (indx
!= 0);
4966 *rel_hash_ptr
= NULL
;
4970 struct elf_link_hash_entry
*h
;
4972 /* Treat a reloc against a defined symbol as though it were
4973 actually against the section. */
4974 h
= ((struct elf_link_hash_entry
*)
4975 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
4976 link_order
->u
.reloc
.p
->u
.name
,
4977 false, false, true));
4979 && (h
->root
.type
== bfd_link_hash_defined
4980 || h
->root
.type
== bfd_link_hash_defweak
))
4984 section
= h
->root
.u
.def
.section
;
4985 indx
= section
->output_section
->target_index
;
4986 *rel_hash_ptr
= NULL
;
4987 /* It seems that we ought to add the symbol value to the
4988 addend here, but in practice it has already been added
4989 because it was passed to constructor_callback. */
4990 addend
+= section
->output_section
->vma
+ section
->output_offset
;
4994 /* Setting the index to -2 tells elf_link_output_extsym that
4995 this symbol is used by a reloc. */
5002 if (! ((*info
->callbacks
->unattached_reloc
)
5003 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
5004 (asection
*) NULL
, (bfd_vma
) 0)))
5010 /* If this is an inplace reloc, we must write the addend into the
5012 if (howto
->partial_inplace
&& addend
!= 0)
5015 bfd_reloc_status_type rstat
;
5019 size
= bfd_get_reloc_size (howto
);
5020 buf
= (bfd_byte
*) bfd_zmalloc (size
);
5021 if (buf
== (bfd_byte
*) NULL
)
5023 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5029 case bfd_reloc_outofrange
:
5031 case bfd_reloc_overflow
:
5032 if (! ((*info
->callbacks
->reloc_overflow
)
5034 (link_order
->type
== bfd_section_reloc_link_order
5035 ? bfd_section_name (output_bfd
,
5036 link_order
->u
.reloc
.p
->u
.section
)
5037 : link_order
->u
.reloc
.p
->u
.name
),
5038 howto
->name
, addend
, (bfd
*) NULL
, (asection
*) NULL
,
5046 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
5047 (file_ptr
) link_order
->offset
, size
);
5053 /* The address of a reloc is relative to the section in a
5054 relocateable file, and is a virtual address in an executable
5056 offset
= link_order
->offset
;
5057 if (! info
->relocateable
)
5058 offset
+= output_section
->vma
;
5060 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
5062 if (rel_hdr
->sh_type
== SHT_REL
)
5064 Elf_Internal_Rel irel
;
5065 Elf_External_Rel
*erel
;
5067 irel
.r_offset
= offset
;
5068 irel
.r_info
= ELF_R_INFO (indx
, howto
->type
);
5069 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
5070 + output_section
->reloc_count
);
5071 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
5075 Elf_Internal_Rela irela
;
5076 Elf_External_Rela
*erela
;
5078 irela
.r_offset
= offset
;
5079 irela
.r_info
= ELF_R_INFO (indx
, howto
->type
);
5080 irela
.r_addend
= addend
;
5081 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
5082 + output_section
->reloc_count
);
5083 elf_swap_reloca_out (output_bfd
, &irela
, erela
);
5086 ++output_section
->reloc_count
;
5092 /* Allocate a pointer to live in a linker created section. */
5095 elf_create_pointer_linker_section (abfd
, info
, lsect
, h
, rel
)
5097 struct bfd_link_info
*info
;
5098 elf_linker_section_t
*lsect
;
5099 struct elf_link_hash_entry
*h
;
5100 const Elf_Internal_Rela
*rel
;
5102 elf_linker_section_pointers_t
**ptr_linker_section_ptr
= NULL
;
5103 elf_linker_section_pointers_t
*linker_section_ptr
;
5104 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);;
5106 BFD_ASSERT (lsect
!= NULL
);
5108 /* Is this a global symbol? */
5111 /* Has this symbol already been allocated, if so, our work is done */
5112 if (_bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
5117 ptr_linker_section_ptr
= &h
->linker_section_pointer
;
5118 /* Make sure this symbol is output as a dynamic symbol. */
5119 if (h
->dynindx
== -1)
5121 if (! elf_link_record_dynamic_symbol (info
, h
))
5125 if (lsect
->rel_section
)
5126 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
5129 else /* Allocation of a pointer to a local symbol */
5131 elf_linker_section_pointers_t
**ptr
= elf_local_ptr_offsets (abfd
);
5133 /* Allocate a table to hold the local symbols if first time */
5136 unsigned int num_symbols
= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
5137 register unsigned int i
;
5139 ptr
= (elf_linker_section_pointers_t
**)
5140 bfd_alloc (abfd
, num_symbols
* sizeof (elf_linker_section_pointers_t
*));
5145 elf_local_ptr_offsets (abfd
) = ptr
;
5146 for (i
= 0; i
< num_symbols
; i
++)
5147 ptr
[i
] = (elf_linker_section_pointers_t
*)0;
5150 /* Has this symbol already been allocated, if so, our work is done */
5151 if (_bfd_elf_find_pointer_linker_section (ptr
[r_symndx
],
5156 ptr_linker_section_ptr
= &ptr
[r_symndx
];
5160 /* If we are generating a shared object, we need to
5161 output a R_<xxx>_RELATIVE reloc so that the
5162 dynamic linker can adjust this GOT entry. */
5163 BFD_ASSERT (lsect
->rel_section
!= NULL
);
5164 lsect
->rel_section
->_raw_size
+= sizeof (Elf_External_Rela
);
5168 /* Allocate space for a pointer in the linker section, and allocate a new pointer record
5169 from internal memory. */
5170 BFD_ASSERT (ptr_linker_section_ptr
!= NULL
);
5171 linker_section_ptr
= (elf_linker_section_pointers_t
*)
5172 bfd_alloc (abfd
, sizeof (elf_linker_section_pointers_t
));
5174 if (!linker_section_ptr
)
5177 linker_section_ptr
->next
= *ptr_linker_section_ptr
;
5178 linker_section_ptr
->addend
= rel
->r_addend
;
5179 linker_section_ptr
->which
= lsect
->which
;
5180 linker_section_ptr
->written_address_p
= false;
5181 *ptr_linker_section_ptr
= linker_section_ptr
;
5184 if (lsect
->hole_size
&& lsect
->hole_offset
< lsect
->max_hole_offset
)
5186 linker_section_ptr
->offset
= lsect
->section
->_raw_size
- lsect
->hole_size
+ (ARCH_SIZE
/ 8);
5187 lsect
->hole_offset
+= ARCH_SIZE
/ 8;
5188 lsect
->sym_offset
+= ARCH_SIZE
/ 8;
5189 if (lsect
->sym_hash
) /* Bump up symbol value if needed */
5191 lsect
->sym_hash
->root
.u
.def
.value
+= ARCH_SIZE
/ 8;
5193 fprintf (stderr
, "Bump up %s by %ld, current value = %ld\n",
5194 lsect
->sym_hash
->root
.root
.string
,
5195 (long)ARCH_SIZE
/ 8,
5196 (long)lsect
->sym_hash
->root
.u
.def
.value
);
5202 linker_section_ptr
->offset
= lsect
->section
->_raw_size
;
5204 lsect
->section
->_raw_size
+= ARCH_SIZE
/ 8;
5207 fprintf (stderr
, "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
5208 lsect
->name
, (long)linker_section_ptr
->offset
, (long)lsect
->section
->_raw_size
);
5216 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
5219 #define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
5222 /* Fill in the address for a pointer generated in alinker section. */
5225 elf_finish_pointer_linker_section (output_bfd
, input_bfd
, info
, lsect
, h
, relocation
, rel
, relative_reloc
)
5228 struct bfd_link_info
*info
;
5229 elf_linker_section_t
*lsect
;
5230 struct elf_link_hash_entry
*h
;
5232 const Elf_Internal_Rela
*rel
;
5235 elf_linker_section_pointers_t
*linker_section_ptr
;
5237 BFD_ASSERT (lsect
!= NULL
);
5239 if (h
!= NULL
) /* global symbol */
5241 linker_section_ptr
= _bfd_elf_find_pointer_linker_section (h
->linker_section_pointer
,
5245 BFD_ASSERT (linker_section_ptr
!= NULL
);
5247 if (! elf_hash_table (info
)->dynamic_sections_created
5250 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
5252 /* This is actually a static link, or it is a
5253 -Bsymbolic link and the symbol is defined
5254 locally. We must initialize this entry in the
5257 When doing a dynamic link, we create a .rela.<xxx>
5258 relocation entry to initialize the value. This
5259 is done in the finish_dynamic_symbol routine. */
5260 if (!linker_section_ptr
->written_address_p
)
5262 linker_section_ptr
->written_address_p
= true;
5263 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
5264 lsect
->section
->contents
+ linker_section_ptr
->offset
);
5268 else /* local symbol */
5270 unsigned long r_symndx
= ELF_R_SYM (rel
->r_info
);
5271 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
) != NULL
);
5272 BFD_ASSERT (elf_local_ptr_offsets (input_bfd
)[r_symndx
] != NULL
);
5273 linker_section_ptr
= _bfd_elf_find_pointer_linker_section (elf_local_ptr_offsets (input_bfd
)[r_symndx
],
5277 BFD_ASSERT (linker_section_ptr
!= NULL
);
5279 /* Write out pointer if it hasn't been rewritten out before */
5280 if (!linker_section_ptr
->written_address_p
)
5282 linker_section_ptr
->written_address_p
= true;
5283 bfd_put_ptr (output_bfd
, relocation
+ linker_section_ptr
->addend
,
5284 lsect
->section
->contents
+ linker_section_ptr
->offset
);
5288 asection
*srel
= lsect
->rel_section
;
5289 Elf_Internal_Rela outrel
;
5291 /* We need to generate a relative reloc for the dynamic linker. */
5293 lsect
->rel_section
= srel
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
,
5296 BFD_ASSERT (srel
!= NULL
);
5298 outrel
.r_offset
= (lsect
->section
->output_section
->vma
5299 + lsect
->section
->output_offset
5300 + linker_section_ptr
->offset
);
5301 outrel
.r_info
= ELF_R_INFO (0, relative_reloc
);
5302 outrel
.r_addend
= 0;
5303 elf_swap_reloca_out (output_bfd
, &outrel
,
5304 (((Elf_External_Rela
*)
5305 lsect
->section
->contents
)
5306 + lsect
->section
->reloc_count
));
5307 ++lsect
->section
->reloc_count
;
5312 relocation
= (lsect
->section
->output_offset
5313 + linker_section_ptr
->offset
5314 - lsect
->hole_offset
5315 - lsect
->sym_offset
);
5318 fprintf (stderr
, "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
5319 lsect
->name
, (long)relocation
, (long)relocation
);
5322 /* Subtract out the addend, because it will get added back in by the normal
5324 return relocation
- linker_section_ptr
->addend
;
5327 /* Garbage collect unused sections. */
5329 static boolean elf_gc_mark
5330 PARAMS ((struct bfd_link_info
*info
, asection
*sec
,
5331 asection
* (*gc_mark_hook
)
5332 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
5333 struct elf_link_hash_entry
*, Elf_Internal_Sym
*))));
5335 static boolean elf_gc_sweep
5336 PARAMS ((struct bfd_link_info
*info
,
5337 boolean (*gc_sweep_hook
)
5338 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
5339 const Elf_Internal_Rela
*relocs
))));
5341 static boolean elf_gc_sweep_symbol
5342 PARAMS ((struct elf_link_hash_entry
*h
, PTR idxptr
));
5344 static boolean elf_gc_allocate_got_offsets
5345 PARAMS ((struct elf_link_hash_entry
*h
, PTR offarg
));
5347 static boolean elf_gc_propagate_vtable_entries_used
5348 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
5350 static boolean elf_gc_smash_unused_vtentry_relocs
5351 PARAMS ((struct elf_link_hash_entry
*h
, PTR dummy
));
5353 /* The mark phase of garbage collection. For a given section, mark
5354 it, and all the sections which define symbols to which it refers. */
5357 elf_gc_mark (info
, sec
, gc_mark_hook
)
5358 struct bfd_link_info
*info
;
5360 asection
* (*gc_mark_hook
)
5361 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
5362 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
5368 /* Look through the section relocs. */
5370 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
5372 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
5373 Elf_Internal_Shdr
*symtab_hdr
;
5374 struct elf_link_hash_entry
**sym_hashes
;
5377 Elf_External_Sym
*locsyms
, *freesyms
= NULL
;
5378 bfd
*input_bfd
= sec
->owner
;
5380 /* GCFIXME: how to arrange so that relocs and symbols are not
5381 reread continually? */
5383 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
5384 sym_hashes
= elf_sym_hashes (input_bfd
);
5386 /* Read the local symbols. */
5387 if (elf_bad_symtab (input_bfd
))
5389 nlocsyms
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
5393 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
5394 if (symtab_hdr
->contents
)
5395 locsyms
= (Elf_External_Sym
*) symtab_hdr
->contents
;
5396 else if (nlocsyms
== 0)
5400 locsyms
= freesyms
=
5401 bfd_malloc (nlocsyms
* sizeof (Elf_External_Sym
));
5402 if (freesyms
== NULL
5403 || bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
5404 || (bfd_read (locsyms
, sizeof (Elf_External_Sym
),
5405 nlocsyms
, input_bfd
)
5406 != nlocsyms
* sizeof (Elf_External_Sym
)))
5413 /* Read the relocations. */
5414 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
5415 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
5416 info
->keep_memory
));
5417 if (relstart
== NULL
)
5422 relend
= relstart
+ sec
->reloc_count
;
5424 for (rel
= relstart
; rel
< relend
; rel
++)
5426 unsigned long r_symndx
;
5428 struct elf_link_hash_entry
*h
;
5431 r_symndx
= ELF_R_SYM (rel
->r_info
);
5435 if (elf_bad_symtab (sec
->owner
))
5437 elf_swap_symbol_in (input_bfd
, &locsyms
[r_symndx
], &s
);
5438 if (ELF_ST_BIND (s
.st_info
) == STB_LOCAL
)
5439 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, NULL
, &s
);
5442 h
= sym_hashes
[r_symndx
- extsymoff
];
5443 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, h
, NULL
);
5446 else if (r_symndx
>= nlocsyms
)
5448 h
= sym_hashes
[r_symndx
- extsymoff
];
5449 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, h
, NULL
);
5453 elf_swap_symbol_in (input_bfd
, &locsyms
[r_symndx
], &s
);
5454 rsec
= (*gc_mark_hook
)(sec
->owner
, info
, rel
, NULL
, &s
);
5457 if (rsec
&& !rsec
->gc_mark
)
5458 if (!elf_gc_mark (info
, rsec
, gc_mark_hook
))
5466 if (!info
->keep_memory
)
5476 /* The sweep phase of garbage collection. Remove all garbage sections. */
5479 elf_gc_sweep (info
, gc_sweep_hook
)
5480 struct bfd_link_info
*info
;
5481 boolean (*gc_sweep_hook
)
5482 PARAMS ((bfd
*abfd
, struct bfd_link_info
*info
, asection
*o
,
5483 const Elf_Internal_Rela
*relocs
));
5487 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5491 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5493 /* Keep special sections. Keep .debug sections. */
5494 if ((o
->flags
& SEC_LINKER_CREATED
)
5495 || (o
->flags
& SEC_DEBUGGING
))
5501 /* Skip sweeping sections already excluded. */
5502 if (o
->flags
& SEC_EXCLUDE
)
5505 /* Since this is early in the link process, it is simple
5506 to remove a section from the output. */
5507 o
->flags
|= SEC_EXCLUDE
;
5509 /* But we also have to update some of the relocation
5510 info we collected before. */
5512 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
5514 Elf_Internal_Rela
*internal_relocs
;
5517 internal_relocs
= (NAME(_bfd_elf
,link_read_relocs
)
5518 (o
->owner
, o
, NULL
, NULL
, info
->keep_memory
));
5519 if (internal_relocs
== NULL
)
5522 r
= (*gc_sweep_hook
)(o
->owner
, info
, o
, internal_relocs
);
5524 if (!info
->keep_memory
)
5525 free (internal_relocs
);
5533 /* Remove the symbols that were in the swept sections from the dynamic
5534 symbol table. GCFIXME: Anyone know how to get them out of the
5535 static symbol table as well? */
5539 elf_link_hash_traverse (elf_hash_table (info
),
5540 elf_gc_sweep_symbol
,
5543 elf_hash_table (info
)->dynsymcount
= i
;
5549 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5552 elf_gc_sweep_symbol (h
, idxptr
)
5553 struct elf_link_hash_entry
*h
;
5556 int *idx
= (int *) idxptr
;
5558 if (h
->dynindx
!= -1
5559 && ((h
->root
.type
!= bfd_link_hash_defined
5560 && h
->root
.type
!= bfd_link_hash_defweak
)
5561 || h
->root
.u
.def
.section
->gc_mark
))
5562 h
->dynindx
= (*idx
)++;
5567 /* Propogate collected vtable information. This is called through
5568 elf_link_hash_traverse. */
5571 elf_gc_propagate_vtable_entries_used (h
, okp
)
5572 struct elf_link_hash_entry
*h
;
5575 /* Those that are not vtables. */
5576 if (h
->vtable_parent
== NULL
)
5579 /* Those vtables that do not have parents, we cannot merge. */
5580 if (h
->vtable_parent
== (struct elf_link_hash_entry
*) -1)
5583 /* If we've already been done, exit. */
5584 if (h
->vtable_entries_used
&& h
->vtable_entries_used
[-1])
5587 /* Make sure the parent's table is up to date. */
5588 elf_gc_propagate_vtable_entries_used (h
->vtable_parent
, okp
);
5590 if (h
->vtable_entries_used
== NULL
)
5592 /* None of this table's entries were referenced. Re-use the
5594 h
->vtable_entries_used
= h
->vtable_parent
->vtable_entries_used
;
5601 /* Or the parent's entries into ours. */
5602 cu
= h
->vtable_entries_used
;
5604 pu
= h
->vtable_parent
->vtable_entries_used
;
5607 n
= h
->vtable_parent
->size
/ FILE_ALIGN
;
5610 if (*pu
) *cu
= true;
5620 elf_gc_smash_unused_vtentry_relocs (h
, okp
)
5621 struct elf_link_hash_entry
*h
;
5625 bfd_vma hstart
, hend
;
5626 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
5628 /* Take care of both those symbols that do not describe vtables as
5629 well as those that are not loaded. */
5630 if (h
->vtable_parent
== NULL
)
5633 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5634 || h
->root
.type
== bfd_link_hash_defweak
);
5636 sec
= h
->root
.u
.def
.section
;
5637 hstart
= h
->root
.u
.def
.value
;
5638 hend
= hstart
+ h
->size
;
5640 relstart
= (NAME(_bfd_elf
,link_read_relocs
)
5641 (sec
->owner
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
, true));
5643 return *(boolean
*)okp
= false;
5644 relend
= relstart
+ sec
->reloc_count
;
5646 for (rel
= relstart
; rel
< relend
; ++rel
)
5647 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
5649 /* If the entry is in use, do nothing. */
5650 if (h
->vtable_entries_used
)
5652 bfd_vma entry
= (rel
->r_offset
- hstart
) / FILE_ALIGN
;
5653 if (h
->vtable_entries_used
[entry
])
5656 /* Otherwise, kill it. */
5657 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
5663 /* Do mark and sweep of unused sections. */
5666 elf_gc_sections (abfd
, info
)
5668 struct bfd_link_info
*info
;
5672 asection
* (*gc_mark_hook
)
5673 PARAMS ((bfd
*abfd
, struct bfd_link_info
*, Elf_Internal_Rela
*,
5674 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*));
5676 if (!get_elf_backend_data (abfd
)->can_gc_sections
5677 || info
->relocateable
)
5680 /* Apply transitive closure to the vtable entry usage info. */
5681 elf_link_hash_traverse (elf_hash_table (info
),
5682 elf_gc_propagate_vtable_entries_used
,
5687 /* Kill the vtable relocations that were not used. */
5688 elf_link_hash_traverse (elf_hash_table (info
),
5689 elf_gc_smash_unused_vtentry_relocs
,
5694 /* Grovel through relocs to find out who stays ... */
5696 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
5697 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5700 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5702 if (o
->flags
& SEC_KEEP
)
5703 if (!elf_gc_mark (info
, o
, gc_mark_hook
))
5708 /* ... and mark SEC_EXCLUDE for those that go. */
5709 if (!elf_gc_sweep(info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
5715 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
5718 elf_gc_record_vtinherit (abfd
, sec
, h
, offset
)
5721 struct elf_link_hash_entry
*h
;
5724 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
5725 struct elf_link_hash_entry
**search
, *child
;
5726 bfd_size_type extsymcount
;
5728 /* The sh_info field of the symtab header tells us where the
5729 external symbols start. We don't care about the local symbols at
5731 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/sizeof (Elf_External_Sym
);
5732 if (!elf_bad_symtab (abfd
))
5733 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
5735 sym_hashes
= elf_sym_hashes (abfd
);
5736 sym_hashes_end
= sym_hashes
+ extsymcount
;
5738 /* Hunt down the child symbol, which is in this section at the same
5739 offset as the relocation. */
5740 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
5742 if ((child
= *search
) != NULL
5743 && (child
->root
.type
== bfd_link_hash_defined
5744 || child
->root
.type
== bfd_link_hash_defweak
)
5745 && child
->root
.u
.def
.section
== sec
5746 && child
->root
.u
.def
.value
== offset
)
5750 (*_bfd_error_handler
) ("%s: %s+%lu: No symbol found for INHERIT",
5751 bfd_get_filename (abfd
), sec
->name
,
5752 (unsigned long)offset
);
5753 bfd_set_error (bfd_error_invalid_operation
);
5759 /* This *should* only be the absolute section. It could potentially
5760 be that someone has defined a non-global vtable though, which
5761 would be bad. It isn't worth paging in the local symbols to be
5762 sure though; that case should simply be handled by the assembler. */
5764 child
->vtable_parent
= (struct elf_link_hash_entry
*) -1;
5767 child
->vtable_parent
= h
;
5772 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
5775 elf_gc_record_vtentry (abfd
, sec
, h
, addend
)
5778 struct elf_link_hash_entry
*h
;
5781 if (h
->vtable_entries_used
== NULL
)
5783 /* Allocate one extra entry for use as a "done" flag for the
5784 consolidation pass. */
5785 size_t size
= (h
->size
/ FILE_ALIGN
+ 1) * sizeof(boolean
);
5786 h
->vtable_entries_used
= (boolean
*) bfd_alloc (abfd
, size
);
5787 if (h
->vtable_entries_used
== NULL
)
5790 /* And arrange for that done flag to be at index -1. */
5791 memset (h
->vtable_entries_used
++, 0, size
);
5793 h
->vtable_entries_used
[addend
/ FILE_ALIGN
] = true;
5798 /* And an accompanying bit to work out final got entry offsets once
5799 we're done. Should be called from final_link. */
5802 elf_gc_common_finalize_got_offsets (abfd
, info
)
5804 struct bfd_link_info
*info
;
5807 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5810 /* The GOT offset is relative to the .got section, but the GOT header is
5811 put into the .got.plt section, if the backend uses it. */
5812 if (bed
->want_got_plt
)
5815 gotoff
= bed
->got_header_size
;
5817 /* Do the local .got entries first. */
5818 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
5820 bfd_signed_vma
*local_got
= elf_local_got_refcounts (i
);
5821 bfd_size_type j
, locsymcount
;
5822 Elf_Internal_Shdr
*symtab_hdr
;
5827 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
5828 if (elf_bad_symtab (i
))
5829 locsymcount
= symtab_hdr
->sh_size
/ sizeof (Elf_External_Sym
);
5831 locsymcount
= symtab_hdr
->sh_info
;
5833 for (j
= 0; j
< locsymcount
; ++j
)
5835 if (local_got
[j
] > 0)
5837 local_got
[j
] = gotoff
;
5838 gotoff
+= ARCH_SIZE
/ 8;
5841 local_got
[j
] = (bfd_vma
) -1;
5845 /* Then the global .got and .plt entries. */
5846 elf_link_hash_traverse (elf_hash_table (info
),
5847 elf_gc_allocate_got_offsets
,
5852 /* We need a special top-level link routine to convert got reference counts
5853 to real got offsets. */
5856 elf_gc_allocate_got_offsets (h
, offarg
)
5857 struct elf_link_hash_entry
*h
;
5860 bfd_vma
*off
= (bfd_vma
*) offarg
;
5862 if (h
->got
.refcount
> 0)
5864 h
->got
.offset
= off
[0];
5865 off
[0] += ARCH_SIZE
/ 8;
5868 h
->got
.offset
= (bfd_vma
) -1;
5873 /* Many folk need no more in the way of final link than this, once
5874 got entry reference counting is enabled. */
5877 elf_gc_common_final_link (abfd
, info
)
5879 struct bfd_link_info
*info
;
5881 if (!elf_gc_common_finalize_got_offsets (abfd
, info
))
5884 /* Invoke the regular ELF backend linker to do all the work. */
5885 return elf_bfd_final_link (abfd
, info
);