1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
31 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
35 struct elf_link_hash_entry
*h
;
36 struct bfd_link_hash_entry
*bh
;
37 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
40 /* This function may be called more than once. */
41 s
= bfd_get_section_by_name (abfd
, ".got");
42 if (s
!= NULL
&& (s
->flags
& SEC_LINKER_CREATED
) != 0)
45 switch (bed
->s
->arch_size
)
56 bfd_set_error (bfd_error_bad_value
);
60 flags
= bed
->dynamic_sec_flags
;
62 s
= bfd_make_section (abfd
, ".got");
64 || !bfd_set_section_flags (abfd
, s
, flags
)
65 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
68 if (bed
->want_got_plt
)
70 s
= bfd_make_section (abfd
, ".got.plt");
72 || !bfd_set_section_flags (abfd
, s
, flags
)
73 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
77 if (bed
->want_got_sym
)
79 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
80 (or .got.plt) section. We don't do this in the linker script
81 because we don't want to define the symbol if we are not creating
82 a global offset table. */
84 if (!(_bfd_generic_link_add_one_symbol
85 (info
, abfd
, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL
, s
,
86 bed
->got_symbol_offset
, NULL
, FALSE
, bed
->collect
, &bh
)))
88 h
= (struct elf_link_hash_entry
*) bh
;
92 if (! info
->executable
93 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
96 elf_hash_table (info
)->hgot
= h
;
99 /* The first bit of the global offset table is the header. */
100 s
->size
+= bed
->got_header_size
+ bed
->got_symbol_offset
;
105 /* Create some sections which will be filled in with dynamic linking
106 information. ABFD is an input file which requires dynamic sections
107 to be created. The dynamic sections take up virtual memory space
108 when the final executable is run, so we need to create them before
109 addresses are assigned to the output sections. We work out the
110 actual contents and size of these sections later. */
113 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
116 register asection
*s
;
117 struct elf_link_hash_entry
*h
;
118 struct bfd_link_hash_entry
*bh
;
119 const struct elf_backend_data
*bed
;
121 if (! is_elf_hash_table (info
->hash
))
124 if (elf_hash_table (info
)->dynamic_sections_created
)
127 /* Make sure that all dynamic sections use the same input BFD. */
128 if (elf_hash_table (info
)->dynobj
== NULL
)
129 elf_hash_table (info
)->dynobj
= abfd
;
131 abfd
= elf_hash_table (info
)->dynobj
;
133 bed
= get_elf_backend_data (abfd
);
135 flags
= bed
->dynamic_sec_flags
;
137 /* A dynamically linked executable has a .interp section, but a
138 shared library does not. */
139 if (info
->executable
)
141 s
= bfd_make_section (abfd
, ".interp");
143 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
147 if (! info
->traditional_format
)
149 s
= bfd_make_section (abfd
, ".eh_frame_hdr");
151 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
152 || ! bfd_set_section_alignment (abfd
, s
, 2))
154 elf_hash_table (info
)->eh_info
.hdr_sec
= s
;
157 /* Create sections to hold version informations. These are removed
158 if they are not needed. */
159 s
= bfd_make_section (abfd
, ".gnu.version_d");
161 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
162 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
165 s
= bfd_make_section (abfd
, ".gnu.version");
167 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
168 || ! bfd_set_section_alignment (abfd
, s
, 1))
171 s
= bfd_make_section (abfd
, ".gnu.version_r");
173 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
174 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
177 s
= bfd_make_section (abfd
, ".dynsym");
179 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
180 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
183 s
= bfd_make_section (abfd
, ".dynstr");
185 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
188 /* Create a strtab to hold the dynamic symbol names. */
189 if (elf_hash_table (info
)->dynstr
== NULL
)
191 elf_hash_table (info
)->dynstr
= _bfd_elf_strtab_init ();
192 if (elf_hash_table (info
)->dynstr
== NULL
)
196 s
= bfd_make_section (abfd
, ".dynamic");
198 || ! bfd_set_section_flags (abfd
, s
, flags
)
199 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
202 /* The special symbol _DYNAMIC is always set to the start of the
203 .dynamic section. This call occurs before we have processed the
204 symbols for any dynamic object, so we don't have to worry about
205 overriding a dynamic definition. We could set _DYNAMIC in a
206 linker script, but we only want to define it if we are, in fact,
207 creating a .dynamic section. We don't want to define it if there
208 is no .dynamic section, since on some ELF platforms the start up
209 code examines it to decide how to initialize the process. */
211 if (! (_bfd_generic_link_add_one_symbol
212 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, 0, NULL
, FALSE
,
213 get_elf_backend_data (abfd
)->collect
, &bh
)))
215 h
= (struct elf_link_hash_entry
*) bh
;
217 h
->type
= STT_OBJECT
;
219 if (! info
->executable
220 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
223 s
= bfd_make_section (abfd
, ".hash");
225 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
226 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
228 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
230 /* Let the backend create the rest of the sections. This lets the
231 backend set the right flags. The backend will normally create
232 the .got and .plt sections. */
233 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
236 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
241 /* Create dynamic sections when linking against a dynamic object. */
244 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
246 flagword flags
, pltflags
;
248 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
250 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
251 .rel[a].bss sections. */
252 flags
= bed
->dynamic_sec_flags
;
255 pltflags
|= SEC_CODE
;
256 if (bed
->plt_not_loaded
)
257 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
258 if (bed
->plt_readonly
)
259 pltflags
|= SEC_READONLY
;
261 s
= bfd_make_section (abfd
, ".plt");
263 || ! bfd_set_section_flags (abfd
, s
, pltflags
)
264 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
267 if (bed
->want_plt_sym
)
269 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
271 struct elf_link_hash_entry
*h
;
272 struct bfd_link_hash_entry
*bh
= NULL
;
274 if (! (_bfd_generic_link_add_one_symbol
275 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
, 0, NULL
,
276 FALSE
, get_elf_backend_data (abfd
)->collect
, &bh
)))
278 h
= (struct elf_link_hash_entry
*) bh
;
280 h
->type
= STT_OBJECT
;
282 if (! info
->executable
283 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
287 s
= bfd_make_section (abfd
,
288 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt");
290 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
291 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
294 if (! _bfd_elf_create_got_section (abfd
, info
))
297 if (bed
->want_dynbss
)
299 /* The .dynbss section is a place to put symbols which are defined
300 by dynamic objects, are referenced by regular objects, and are
301 not functions. We must allocate space for them in the process
302 image and use a R_*_COPY reloc to tell the dynamic linker to
303 initialize them at run time. The linker script puts the .dynbss
304 section into the .bss section of the final image. */
305 s
= bfd_make_section (abfd
, ".dynbss");
307 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
| SEC_LINKER_CREATED
))
310 /* The .rel[a].bss section holds copy relocs. This section is not
311 normally needed. We need to create it here, though, so that the
312 linker will map it to an output section. We can't just create it
313 only if we need it, because we will not know whether we need it
314 until we have seen all the input files, and the first time the
315 main linker code calls BFD after examining all the input files
316 (size_dynamic_sections) the input sections have already been
317 mapped to the output sections. If the section turns out not to
318 be needed, we can discard it later. We will never need this
319 section when generating a shared object, since they do not use
323 s
= bfd_make_section (abfd
,
324 (bed
->default_use_rela_p
325 ? ".rela.bss" : ".rel.bss"));
327 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
328 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
336 /* Record a new dynamic symbol. We record the dynamic symbols as we
337 read the input files, since we need to have a list of all of them
338 before we can determine the final sizes of the output sections.
339 Note that we may actually call this function even though we are not
340 going to output any dynamic symbols; in some cases we know that a
341 symbol should be in the dynamic symbol table, but only if there is
345 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
346 struct elf_link_hash_entry
*h
)
348 if (h
->dynindx
== -1)
350 struct elf_strtab_hash
*dynstr
;
355 /* XXX: The ABI draft says the linker must turn hidden and
356 internal symbols into STB_LOCAL symbols when producing the
357 DSO. However, if ld.so honors st_other in the dynamic table,
358 this would not be necessary. */
359 switch (ELF_ST_VISIBILITY (h
->other
))
363 if (h
->root
.type
!= bfd_link_hash_undefined
364 && h
->root
.type
!= bfd_link_hash_undefweak
)
374 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
375 ++elf_hash_table (info
)->dynsymcount
;
377 dynstr
= elf_hash_table (info
)->dynstr
;
380 /* Create a strtab to hold the dynamic symbol names. */
381 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
386 /* We don't put any version information in the dynamic string
388 name
= h
->root
.root
.string
;
389 p
= strchr (name
, ELF_VER_CHR
);
391 /* We know that the p points into writable memory. In fact,
392 there are only a few symbols that have read-only names, being
393 those like _GLOBAL_OFFSET_TABLE_ that are created specially
394 by the backends. Most symbols will have names pointing into
395 an ELF string table read from a file, or to objalloc memory. */
398 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
403 if (indx
== (bfd_size_type
) -1)
405 h
->dynstr_index
= indx
;
411 /* Record an assignment to a symbol made by a linker script. We need
412 this in case some dynamic object refers to this symbol. */
415 bfd_elf_record_link_assignment (bfd
*output_bfd ATTRIBUTE_UNUSED
,
416 struct bfd_link_info
*info
,
420 struct elf_link_hash_entry
*h
;
422 if (!is_elf_hash_table (info
->hash
))
425 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, TRUE
, FALSE
);
429 /* Since we're defining the symbol, don't let it seem to have not
430 been defined. record_dynamic_symbol and size_dynamic_sections
432 ??? Changing bfd_link_hash_undefined to bfd_link_hash_new (or
433 to bfd_link_hash_undefweak, see linker.c:link_action) runs the risk
434 of some later symbol manipulation setting the symbol back to
435 bfd_link_hash_undefined, and the linker trying to add the symbol to
436 the undefs list twice. */
437 if (h
->root
.type
== bfd_link_hash_undefweak
438 || h
->root
.type
== bfd_link_hash_undefined
)
439 h
->root
.type
= bfd_link_hash_new
;
441 if (h
->root
.type
== bfd_link_hash_new
)
444 /* If this symbol is being provided by the linker script, and it is
445 currently defined by a dynamic object, but not by a regular
446 object, then mark it as undefined so that the generic linker will
447 force the correct value. */
451 h
->root
.type
= bfd_link_hash_undefined
;
453 /* If this symbol is not being provided by the linker script, and it is
454 currently defined by a dynamic object, but not by a regular object,
455 then clear out any version information because the symbol will not be
456 associated with the dynamic object any more. */
460 h
->verinfo
.verdef
= NULL
;
469 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
472 /* If this is a weak defined symbol, and we know a corresponding
473 real symbol from the same dynamic object, make sure the real
474 symbol is also made into a dynamic symbol. */
475 if (h
->u
.weakdef
!= NULL
476 && h
->u
.weakdef
->dynindx
== -1)
478 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
486 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
487 success, and 2 on a failure caused by attempting to record a symbol
488 in a discarded section, eg. a discarded link-once section symbol. */
491 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
496 struct elf_link_local_dynamic_entry
*entry
;
497 struct elf_link_hash_table
*eht
;
498 struct elf_strtab_hash
*dynstr
;
499 unsigned long dynstr_index
;
501 Elf_External_Sym_Shndx eshndx
;
502 char esym
[sizeof (Elf64_External_Sym
)];
504 if (! is_elf_hash_table (info
->hash
))
507 /* See if the entry exists already. */
508 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
509 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
512 amt
= sizeof (*entry
);
513 entry
= bfd_alloc (input_bfd
, amt
);
517 /* Go find the symbol, so that we can find it's name. */
518 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
519 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
521 bfd_release (input_bfd
, entry
);
525 if (entry
->isym
.st_shndx
!= SHN_UNDEF
526 && (entry
->isym
.st_shndx
< SHN_LORESERVE
527 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
531 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
532 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
534 /* We can still bfd_release here as nothing has done another
535 bfd_alloc. We can't do this later in this function. */
536 bfd_release (input_bfd
, entry
);
541 name
= (bfd_elf_string_from_elf_section
542 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
543 entry
->isym
.st_name
));
545 dynstr
= elf_hash_table (info
)->dynstr
;
548 /* Create a strtab to hold the dynamic symbol names. */
549 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
554 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
555 if (dynstr_index
== (unsigned long) -1)
557 entry
->isym
.st_name
= dynstr_index
;
559 eht
= elf_hash_table (info
);
561 entry
->next
= eht
->dynlocal
;
562 eht
->dynlocal
= entry
;
563 entry
->input_bfd
= input_bfd
;
564 entry
->input_indx
= input_indx
;
567 /* Whatever binding the symbol had before, it's now local. */
569 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
571 /* The dynindx will be set at the end of size_dynamic_sections. */
576 /* Return the dynindex of a local dynamic symbol. */
579 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
583 struct elf_link_local_dynamic_entry
*e
;
585 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
586 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
591 /* This function is used to renumber the dynamic symbols, if some of
592 them are removed because they are marked as local. This is called
593 via elf_link_hash_traverse. */
596 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
599 size_t *count
= data
;
601 if (h
->root
.type
== bfd_link_hash_warning
)
602 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
604 if (h
->dynindx
!= -1)
605 h
->dynindx
= ++(*count
);
610 /* Return true if the dynamic symbol for a given section should be
611 omitted when creating a shared library. */
613 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
614 struct bfd_link_info
*info
,
617 switch (elf_section_data (p
)->this_hdr
.sh_type
)
621 /* If sh_type is yet undecided, assume it could be
622 SHT_PROGBITS/SHT_NOBITS. */
624 if (strcmp (p
->name
, ".got") == 0
625 || strcmp (p
->name
, ".got.plt") == 0
626 || strcmp (p
->name
, ".plt") == 0)
629 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
632 && (ip
= bfd_get_section_by_name (dynobj
, p
->name
)) != NULL
633 && (ip
->flags
& SEC_LINKER_CREATED
)
634 && ip
->output_section
== p
)
639 /* There shouldn't be section relative relocations
640 against any other section. */
646 /* Assign dynsym indices. In a shared library we generate a section
647 symbol for each output section, which come first. Next come all of
648 the back-end allocated local dynamic syms, followed by the rest of
649 the global symbols. */
652 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
, struct bfd_link_info
*info
)
654 unsigned long dynsymcount
= 0;
658 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
660 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
661 if ((p
->flags
& SEC_EXCLUDE
) == 0
662 && (p
->flags
& SEC_ALLOC
) != 0
663 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
664 elf_section_data (p
)->dynindx
= ++dynsymcount
;
667 if (elf_hash_table (info
)->dynlocal
)
669 struct elf_link_local_dynamic_entry
*p
;
670 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
671 p
->dynindx
= ++dynsymcount
;
674 elf_link_hash_traverse (elf_hash_table (info
),
675 elf_link_renumber_hash_table_dynsyms
,
678 /* There is an unused NULL entry at the head of the table which
679 we must account for in our count. Unless there weren't any
680 symbols, which means we'll have no table at all. */
681 if (dynsymcount
!= 0)
684 return elf_hash_table (info
)->dynsymcount
= dynsymcount
;
687 /* This function is called when we want to define a new symbol. It
688 handles the various cases which arise when we find a definition in
689 a dynamic object, or when there is already a definition in a
690 dynamic object. The new symbol is described by NAME, SYM, PSEC,
691 and PVALUE. We set SYM_HASH to the hash table entry. We set
692 OVERRIDE if the old symbol is overriding a new definition. We set
693 TYPE_CHANGE_OK if it is OK for the type to change. We set
694 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
695 change, we mean that we shouldn't warn if the type or size does
699 _bfd_elf_merge_symbol (bfd
*abfd
,
700 struct bfd_link_info
*info
,
702 Elf_Internal_Sym
*sym
,
705 struct elf_link_hash_entry
**sym_hash
,
707 bfd_boolean
*override
,
708 bfd_boolean
*type_change_ok
,
709 bfd_boolean
*size_change_ok
)
711 asection
*sec
, *oldsec
;
712 struct elf_link_hash_entry
*h
;
713 struct elf_link_hash_entry
*flip
;
716 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
717 bfd_boolean newweak
, oldweak
;
723 bind
= ELF_ST_BIND (sym
->st_info
);
725 if (! bfd_is_und_section (sec
))
726 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
728 h
= ((struct elf_link_hash_entry
*)
729 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
734 /* This code is for coping with dynamic objects, and is only useful
735 if we are doing an ELF link. */
736 if (info
->hash
->creator
!= abfd
->xvec
)
739 /* For merging, we only care about real symbols. */
741 while (h
->root
.type
== bfd_link_hash_indirect
742 || h
->root
.type
== bfd_link_hash_warning
)
743 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
745 /* If we just created the symbol, mark it as being an ELF symbol.
746 Other than that, there is nothing to do--there is no merge issue
747 with a newly defined symbol--so we just return. */
749 if (h
->root
.type
== bfd_link_hash_new
)
755 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
758 switch (h
->root
.type
)
765 case bfd_link_hash_undefined
:
766 case bfd_link_hash_undefweak
:
767 oldbfd
= h
->root
.u
.undef
.abfd
;
771 case bfd_link_hash_defined
:
772 case bfd_link_hash_defweak
:
773 oldbfd
= h
->root
.u
.def
.section
->owner
;
774 oldsec
= h
->root
.u
.def
.section
;
777 case bfd_link_hash_common
:
778 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
779 oldsec
= h
->root
.u
.c
.p
->section
;
783 /* In cases involving weak versioned symbols, we may wind up trying
784 to merge a symbol with itself. Catch that here, to avoid the
785 confusion that results if we try to override a symbol with
786 itself. The additional tests catch cases like
787 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
788 dynamic object, which we do want to handle here. */
790 && ((abfd
->flags
& DYNAMIC
) == 0
794 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
795 respectively, is from a dynamic object. */
797 if ((abfd
->flags
& DYNAMIC
) != 0)
803 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
808 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
809 indices used by MIPS ELF. */
810 switch (h
->root
.type
)
816 case bfd_link_hash_defined
:
817 case bfd_link_hash_defweak
:
818 hsec
= h
->root
.u
.def
.section
;
821 case bfd_link_hash_common
:
822 hsec
= h
->root
.u
.c
.p
->section
;
829 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
832 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
833 respectively, appear to be a definition rather than reference. */
835 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
840 if (h
->root
.type
== bfd_link_hash_undefined
841 || h
->root
.type
== bfd_link_hash_undefweak
842 || h
->root
.type
== bfd_link_hash_common
)
847 /* Check TLS symbol. */
848 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
849 && ELF_ST_TYPE (sym
->st_info
) != h
->type
)
852 bfd_boolean ntdef
, tdef
;
853 asection
*ntsec
, *tsec
;
855 if (h
->type
== STT_TLS
)
875 (*_bfd_error_handler
)
876 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
877 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
878 else if (!tdef
&& !ntdef
)
879 (*_bfd_error_handler
)
880 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
881 tbfd
, ntbfd
, h
->root
.root
.string
);
883 (*_bfd_error_handler
)
884 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
885 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
887 (*_bfd_error_handler
)
888 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
889 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
891 bfd_set_error (bfd_error_bad_value
);
895 /* We need to remember if a symbol has a definition in a dynamic
896 object or is weak in all dynamic objects. Internal and hidden
897 visibility will make it unavailable to dynamic objects. */
898 if (newdyn
&& !h
->dynamic_def
)
900 if (!bfd_is_und_section (sec
))
904 /* Check if this symbol is weak in all dynamic objects. If it
905 is the first time we see it in a dynamic object, we mark
906 if it is weak. Otherwise, we clear it. */
909 if (bind
== STB_WEAK
)
912 else if (bind
!= STB_WEAK
)
917 /* If the old symbol has non-default visibility, we ignore the new
918 definition from a dynamic object. */
920 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
921 && !bfd_is_und_section (sec
))
924 /* Make sure this symbol is dynamic. */
926 /* A protected symbol has external availability. Make sure it is
929 FIXME: Should we check type and size for protected symbol? */
930 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
931 return bfd_elf_link_record_dynamic_symbol (info
, h
);
936 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
939 /* If the new symbol with non-default visibility comes from a
940 relocatable file and the old definition comes from a dynamic
941 object, we remove the old definition. */
942 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
945 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
946 && bfd_is_und_section (sec
))
948 /* If the new symbol is undefined and the old symbol was
949 also undefined before, we need to make sure
950 _bfd_generic_link_add_one_symbol doesn't mess
951 up the linker hash table undefs list. Since the old
952 definition came from a dynamic object, it is still on the
954 h
->root
.type
= bfd_link_hash_undefined
;
955 h
->root
.u
.undef
.abfd
= abfd
;
959 h
->root
.type
= bfd_link_hash_new
;
960 h
->root
.u
.undef
.abfd
= NULL
;
969 /* FIXME: Should we check type and size for protected symbol? */
975 /* Differentiate strong and weak symbols. */
976 newweak
= bind
== STB_WEAK
;
977 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
978 || h
->root
.type
== bfd_link_hash_undefweak
);
980 /* If a new weak symbol definition comes from a regular file and the
981 old symbol comes from a dynamic library, we treat the new one as
982 strong. Similarly, an old weak symbol definition from a regular
983 file is treated as strong when the new symbol comes from a dynamic
984 library. Further, an old weak symbol from a dynamic library is
985 treated as strong if the new symbol is from a dynamic library.
986 This reflects the way glibc's ld.so works.
988 Do this before setting *type_change_ok or *size_change_ok so that
989 we warn properly when dynamic library symbols are overridden. */
991 if (newdef
&& !newdyn
&& olddyn
)
993 if (olddef
&& newdyn
)
996 /* It's OK to change the type if either the existing symbol or the
997 new symbol is weak. A type change is also OK if the old symbol
998 is undefined and the new symbol is defined. */
1003 && h
->root
.type
== bfd_link_hash_undefined
))
1004 *type_change_ok
= TRUE
;
1006 /* It's OK to change the size if either the existing symbol or the
1007 new symbol is weak, or if the old symbol is undefined. */
1010 || h
->root
.type
== bfd_link_hash_undefined
)
1011 *size_change_ok
= TRUE
;
1013 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1014 symbol, respectively, appears to be a common symbol in a dynamic
1015 object. If a symbol appears in an uninitialized section, and is
1016 not weak, and is not a function, then it may be a common symbol
1017 which was resolved when the dynamic object was created. We want
1018 to treat such symbols specially, because they raise special
1019 considerations when setting the symbol size: if the symbol
1020 appears as a common symbol in a regular object, and the size in
1021 the regular object is larger, we must make sure that we use the
1022 larger size. This problematic case can always be avoided in C,
1023 but it must be handled correctly when using Fortran shared
1026 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1027 likewise for OLDDYNCOMMON and OLDDEF.
1029 Note that this test is just a heuristic, and that it is quite
1030 possible to have an uninitialized symbol in a shared object which
1031 is really a definition, rather than a common symbol. This could
1032 lead to some minor confusion when the symbol really is a common
1033 symbol in some regular object. However, I think it will be
1039 && (sec
->flags
& SEC_ALLOC
) != 0
1040 && (sec
->flags
& SEC_LOAD
) == 0
1042 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1043 newdyncommon
= TRUE
;
1045 newdyncommon
= FALSE
;
1049 && h
->root
.type
== bfd_link_hash_defined
1051 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1052 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1054 && h
->type
!= STT_FUNC
)
1055 olddyncommon
= TRUE
;
1057 olddyncommon
= FALSE
;
1059 /* If both the old and the new symbols look like common symbols in a
1060 dynamic object, set the size of the symbol to the larger of the
1065 && sym
->st_size
!= h
->size
)
1067 /* Since we think we have two common symbols, issue a multiple
1068 common warning if desired. Note that we only warn if the
1069 size is different. If the size is the same, we simply let
1070 the old symbol override the new one as normally happens with
1071 symbols defined in dynamic objects. */
1073 if (! ((*info
->callbacks
->multiple_common
)
1074 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1075 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1078 if (sym
->st_size
> h
->size
)
1079 h
->size
= sym
->st_size
;
1081 *size_change_ok
= TRUE
;
1084 /* If we are looking at a dynamic object, and we have found a
1085 definition, we need to see if the symbol was already defined by
1086 some other object. If so, we want to use the existing
1087 definition, and we do not want to report a multiple symbol
1088 definition error; we do this by clobbering *PSEC to be
1089 bfd_und_section_ptr.
1091 We treat a common symbol as a definition if the symbol in the
1092 shared library is a function, since common symbols always
1093 represent variables; this can cause confusion in principle, but
1094 any such confusion would seem to indicate an erroneous program or
1095 shared library. We also permit a common symbol in a regular
1096 object to override a weak symbol in a shared object. */
1101 || (h
->root
.type
== bfd_link_hash_common
1103 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1107 newdyncommon
= FALSE
;
1109 *psec
= sec
= bfd_und_section_ptr
;
1110 *size_change_ok
= TRUE
;
1112 /* If we get here when the old symbol is a common symbol, then
1113 we are explicitly letting it override a weak symbol or
1114 function in a dynamic object, and we don't want to warn about
1115 a type change. If the old symbol is a defined symbol, a type
1116 change warning may still be appropriate. */
1118 if (h
->root
.type
== bfd_link_hash_common
)
1119 *type_change_ok
= TRUE
;
1122 /* Handle the special case of an old common symbol merging with a
1123 new symbol which looks like a common symbol in a shared object.
1124 We change *PSEC and *PVALUE to make the new symbol look like a
1125 common symbol, and let _bfd_generic_link_add_one_symbol will do
1129 && h
->root
.type
== bfd_link_hash_common
)
1133 newdyncommon
= FALSE
;
1134 *pvalue
= sym
->st_size
;
1135 *psec
= sec
= bfd_com_section_ptr
;
1136 *size_change_ok
= TRUE
;
1139 /* If the old symbol is from a dynamic object, and the new symbol is
1140 a definition which is not from a dynamic object, then the new
1141 symbol overrides the old symbol. Symbols from regular files
1142 always take precedence over symbols from dynamic objects, even if
1143 they are defined after the dynamic object in the link.
1145 As above, we again permit a common symbol in a regular object to
1146 override a definition in a shared object if the shared object
1147 symbol is a function or is weak. */
1152 || (bfd_is_com_section (sec
)
1154 || h
->type
== STT_FUNC
)))
1159 /* Change the hash table entry to undefined, and let
1160 _bfd_generic_link_add_one_symbol do the right thing with the
1163 h
->root
.type
= bfd_link_hash_undefined
;
1164 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1165 *size_change_ok
= TRUE
;
1168 olddyncommon
= FALSE
;
1170 /* We again permit a type change when a common symbol may be
1171 overriding a function. */
1173 if (bfd_is_com_section (sec
))
1174 *type_change_ok
= TRUE
;
1176 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1179 /* This union may have been set to be non-NULL when this symbol
1180 was seen in a dynamic object. We must force the union to be
1181 NULL, so that it is correct for a regular symbol. */
1182 h
->verinfo
.vertree
= NULL
;
1185 /* Handle the special case of a new common symbol merging with an
1186 old symbol that looks like it might be a common symbol defined in
1187 a shared object. Note that we have already handled the case in
1188 which a new common symbol should simply override the definition
1189 in the shared library. */
1192 && bfd_is_com_section (sec
)
1195 /* It would be best if we could set the hash table entry to a
1196 common symbol, but we don't know what to use for the section
1197 or the alignment. */
1198 if (! ((*info
->callbacks
->multiple_common
)
1199 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1200 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1203 /* If the presumed common symbol in the dynamic object is
1204 larger, pretend that the new symbol has its size. */
1206 if (h
->size
> *pvalue
)
1209 /* FIXME: We no longer know the alignment required by the symbol
1210 in the dynamic object, so we just wind up using the one from
1211 the regular object. */
1214 olddyncommon
= FALSE
;
1216 h
->root
.type
= bfd_link_hash_undefined
;
1217 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1219 *size_change_ok
= TRUE
;
1220 *type_change_ok
= TRUE
;
1222 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1225 h
->verinfo
.vertree
= NULL
;
1230 /* Handle the case where we had a versioned symbol in a dynamic
1231 library and now find a definition in a normal object. In this
1232 case, we make the versioned symbol point to the normal one. */
1233 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1234 flip
->root
.type
= h
->root
.type
;
1235 h
->root
.type
= bfd_link_hash_indirect
;
1236 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1237 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1238 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1242 flip
->ref_dynamic
= 1;
1249 /* This function is called to create an indirect symbol from the
1250 default for the symbol with the default version if needed. The
1251 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1252 set DYNSYM if the new indirect symbol is dynamic. */
1255 _bfd_elf_add_default_symbol (bfd
*abfd
,
1256 struct bfd_link_info
*info
,
1257 struct elf_link_hash_entry
*h
,
1259 Elf_Internal_Sym
*sym
,
1262 bfd_boolean
*dynsym
,
1263 bfd_boolean override
)
1265 bfd_boolean type_change_ok
;
1266 bfd_boolean size_change_ok
;
1269 struct elf_link_hash_entry
*hi
;
1270 struct bfd_link_hash_entry
*bh
;
1271 const struct elf_backend_data
*bed
;
1272 bfd_boolean collect
;
1273 bfd_boolean dynamic
;
1275 size_t len
, shortlen
;
1278 /* If this symbol has a version, and it is the default version, we
1279 create an indirect symbol from the default name to the fully
1280 decorated name. This will cause external references which do not
1281 specify a version to be bound to this version of the symbol. */
1282 p
= strchr (name
, ELF_VER_CHR
);
1283 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1288 /* We are overridden by an old definition. We need to check if we
1289 need to create the indirect symbol from the default name. */
1290 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1292 BFD_ASSERT (hi
!= NULL
);
1295 while (hi
->root
.type
== bfd_link_hash_indirect
1296 || hi
->root
.type
== bfd_link_hash_warning
)
1298 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1304 bed
= get_elf_backend_data (abfd
);
1305 collect
= bed
->collect
;
1306 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1308 shortlen
= p
- name
;
1309 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1310 if (shortname
== NULL
)
1312 memcpy (shortname
, name
, shortlen
);
1313 shortname
[shortlen
] = '\0';
1315 /* We are going to create a new symbol. Merge it with any existing
1316 symbol with this name. For the purposes of the merge, act as
1317 though we were defining the symbol we just defined, although we
1318 actually going to define an indirect symbol. */
1319 type_change_ok
= FALSE
;
1320 size_change_ok
= FALSE
;
1322 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1323 &hi
, &skip
, &override
, &type_change_ok
,
1333 if (! (_bfd_generic_link_add_one_symbol
1334 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1335 0, name
, FALSE
, collect
, &bh
)))
1337 hi
= (struct elf_link_hash_entry
*) bh
;
1341 /* In this case the symbol named SHORTNAME is overriding the
1342 indirect symbol we want to add. We were planning on making
1343 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1344 is the name without a version. NAME is the fully versioned
1345 name, and it is the default version.
1347 Overriding means that we already saw a definition for the
1348 symbol SHORTNAME in a regular object, and it is overriding
1349 the symbol defined in the dynamic object.
1351 When this happens, we actually want to change NAME, the
1352 symbol we just added, to refer to SHORTNAME. This will cause
1353 references to NAME in the shared object to become references
1354 to SHORTNAME in the regular object. This is what we expect
1355 when we override a function in a shared object: that the
1356 references in the shared object will be mapped to the
1357 definition in the regular object. */
1359 while (hi
->root
.type
== bfd_link_hash_indirect
1360 || hi
->root
.type
== bfd_link_hash_warning
)
1361 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1363 h
->root
.type
= bfd_link_hash_indirect
;
1364 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1368 hi
->ref_dynamic
= 1;
1372 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1377 /* Now set HI to H, so that the following code will set the
1378 other fields correctly. */
1382 /* If there is a duplicate definition somewhere, then HI may not
1383 point to an indirect symbol. We will have reported an error to
1384 the user in that case. */
1386 if (hi
->root
.type
== bfd_link_hash_indirect
)
1388 struct elf_link_hash_entry
*ht
;
1390 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1391 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1393 /* See if the new flags lead us to realize that the symbol must
1405 if (hi
->ref_regular
)
1411 /* We also need to define an indirection from the nondefault version
1415 len
= strlen (name
);
1416 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1417 if (shortname
== NULL
)
1419 memcpy (shortname
, name
, shortlen
);
1420 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1422 /* Once again, merge with any existing symbol. */
1423 type_change_ok
= FALSE
;
1424 size_change_ok
= FALSE
;
1426 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1427 &hi
, &skip
, &override
, &type_change_ok
,
1436 /* Here SHORTNAME is a versioned name, so we don't expect to see
1437 the type of override we do in the case above unless it is
1438 overridden by a versioned definition. */
1439 if (hi
->root
.type
!= bfd_link_hash_defined
1440 && hi
->root
.type
!= bfd_link_hash_defweak
)
1441 (*_bfd_error_handler
)
1442 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1448 if (! (_bfd_generic_link_add_one_symbol
1449 (info
, abfd
, shortname
, BSF_INDIRECT
,
1450 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1452 hi
= (struct elf_link_hash_entry
*) bh
;
1454 /* If there is a duplicate definition somewhere, then HI may not
1455 point to an indirect symbol. We will have reported an error
1456 to the user in that case. */
1458 if (hi
->root
.type
== bfd_link_hash_indirect
)
1460 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1462 /* See if the new flags lead us to realize that the symbol
1474 if (hi
->ref_regular
)
1484 /* This routine is used to export all defined symbols into the dynamic
1485 symbol table. It is called via elf_link_hash_traverse. */
1488 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1490 struct elf_info_failed
*eif
= data
;
1492 /* Ignore indirect symbols. These are added by the versioning code. */
1493 if (h
->root
.type
== bfd_link_hash_indirect
)
1496 if (h
->root
.type
== bfd_link_hash_warning
)
1497 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1499 if (h
->dynindx
== -1
1503 struct bfd_elf_version_tree
*t
;
1504 struct bfd_elf_version_expr
*d
;
1506 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1508 if (t
->globals
.list
!= NULL
)
1510 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1515 if (t
->locals
.list
!= NULL
)
1517 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1526 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1537 /* Look through the symbols which are defined in other shared
1538 libraries and referenced here. Update the list of version
1539 dependencies. This will be put into the .gnu.version_r section.
1540 This function is called via elf_link_hash_traverse. */
1543 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1546 struct elf_find_verdep_info
*rinfo
= data
;
1547 Elf_Internal_Verneed
*t
;
1548 Elf_Internal_Vernaux
*a
;
1551 if (h
->root
.type
== bfd_link_hash_warning
)
1552 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1554 /* We only care about symbols defined in shared objects with version
1559 || h
->verinfo
.verdef
== NULL
)
1562 /* See if we already know about this version. */
1563 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1565 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1568 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1569 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1575 /* This is a new version. Add it to tree we are building. */
1580 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1583 rinfo
->failed
= TRUE
;
1587 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1588 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1589 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1593 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1595 /* Note that we are copying a string pointer here, and testing it
1596 above. If bfd_elf_string_from_elf_section is ever changed to
1597 discard the string data when low in memory, this will have to be
1599 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1601 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1602 a
->vna_nextptr
= t
->vn_auxptr
;
1604 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1607 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1614 /* Figure out appropriate versions for all the symbols. We may not
1615 have the version number script until we have read all of the input
1616 files, so until that point we don't know which symbols should be
1617 local. This function is called via elf_link_hash_traverse. */
1620 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1622 struct elf_assign_sym_version_info
*sinfo
;
1623 struct bfd_link_info
*info
;
1624 const struct elf_backend_data
*bed
;
1625 struct elf_info_failed eif
;
1632 if (h
->root
.type
== bfd_link_hash_warning
)
1633 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1635 /* Fix the symbol flags. */
1638 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1641 sinfo
->failed
= TRUE
;
1645 /* We only need version numbers for symbols defined in regular
1647 if (!h
->def_regular
)
1650 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1651 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1652 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1654 struct bfd_elf_version_tree
*t
;
1659 /* There are two consecutive ELF_VER_CHR characters if this is
1660 not a hidden symbol. */
1662 if (*p
== ELF_VER_CHR
)
1668 /* If there is no version string, we can just return out. */
1676 /* Look for the version. If we find it, it is no longer weak. */
1677 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1679 if (strcmp (t
->name
, p
) == 0)
1683 struct bfd_elf_version_expr
*d
;
1685 len
= p
- h
->root
.root
.string
;
1686 alc
= bfd_malloc (len
);
1689 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1690 alc
[len
- 1] = '\0';
1691 if (alc
[len
- 2] == ELF_VER_CHR
)
1692 alc
[len
- 2] = '\0';
1694 h
->verinfo
.vertree
= t
;
1698 if (t
->globals
.list
!= NULL
)
1699 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1701 /* See if there is anything to force this symbol to
1703 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1705 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1709 && ! info
->export_dynamic
)
1710 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1718 /* If we are building an application, we need to create a
1719 version node for this version. */
1720 if (t
== NULL
&& info
->executable
)
1722 struct bfd_elf_version_tree
**pp
;
1725 /* If we aren't going to export this symbol, we don't need
1726 to worry about it. */
1727 if (h
->dynindx
== -1)
1731 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1734 sinfo
->failed
= TRUE
;
1739 t
->name_indx
= (unsigned int) -1;
1743 /* Don't count anonymous version tag. */
1744 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1746 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1748 t
->vernum
= version_index
;
1752 h
->verinfo
.vertree
= t
;
1756 /* We could not find the version for a symbol when
1757 generating a shared archive. Return an error. */
1758 (*_bfd_error_handler
)
1759 (_("%B: undefined versioned symbol name %s"),
1760 sinfo
->output_bfd
, h
->root
.root
.string
);
1761 bfd_set_error (bfd_error_bad_value
);
1762 sinfo
->failed
= TRUE
;
1770 /* If we don't have a version for this symbol, see if we can find
1772 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1774 struct bfd_elf_version_tree
*t
;
1775 struct bfd_elf_version_tree
*local_ver
;
1776 struct bfd_elf_version_expr
*d
;
1778 /* See if can find what version this symbol is in. If the
1779 symbol is supposed to be local, then don't actually register
1782 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1784 if (t
->globals
.list
!= NULL
)
1786 bfd_boolean matched
;
1790 while ((d
= (*t
->match
) (&t
->globals
, d
,
1791 h
->root
.root
.string
)) != NULL
)
1796 /* There is a version without definition. Make
1797 the symbol the default definition for this
1799 h
->verinfo
.vertree
= t
;
1807 /* There is no undefined version for this symbol. Hide the
1809 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1812 if (t
->locals
.list
!= NULL
)
1815 while ((d
= (*t
->match
) (&t
->locals
, d
,
1816 h
->root
.root
.string
)) != NULL
)
1819 /* If the match is "*", keep looking for a more
1820 explicit, perhaps even global, match.
1821 XXX: Shouldn't this be !d->wildcard instead? */
1822 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1831 if (local_ver
!= NULL
)
1833 h
->verinfo
.vertree
= local_ver
;
1834 if (h
->dynindx
!= -1
1836 && ! info
->export_dynamic
)
1838 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1846 /* Read and swap the relocs from the section indicated by SHDR. This
1847 may be either a REL or a RELA section. The relocations are
1848 translated into RELA relocations and stored in INTERNAL_RELOCS,
1849 which should have already been allocated to contain enough space.
1850 The EXTERNAL_RELOCS are a buffer where the external form of the
1851 relocations should be stored.
1853 Returns FALSE if something goes wrong. */
1856 elf_link_read_relocs_from_section (bfd
*abfd
,
1858 Elf_Internal_Shdr
*shdr
,
1859 void *external_relocs
,
1860 Elf_Internal_Rela
*internal_relocs
)
1862 const struct elf_backend_data
*bed
;
1863 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1864 const bfd_byte
*erela
;
1865 const bfd_byte
*erelaend
;
1866 Elf_Internal_Rela
*irela
;
1867 Elf_Internal_Shdr
*symtab_hdr
;
1870 /* Position ourselves at the start of the section. */
1871 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1874 /* Read the relocations. */
1875 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1878 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1879 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1881 bed
= get_elf_backend_data (abfd
);
1883 /* Convert the external relocations to the internal format. */
1884 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1885 swap_in
= bed
->s
->swap_reloc_in
;
1886 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1887 swap_in
= bed
->s
->swap_reloca_in
;
1890 bfd_set_error (bfd_error_wrong_format
);
1894 erela
= external_relocs
;
1895 erelaend
= erela
+ shdr
->sh_size
;
1896 irela
= internal_relocs
;
1897 while (erela
< erelaend
)
1901 (*swap_in
) (abfd
, erela
, irela
);
1902 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1903 if (bed
->s
->arch_size
== 64)
1905 if ((size_t) r_symndx
>= nsyms
)
1907 (*_bfd_error_handler
)
1908 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1909 " for offset 0x%lx in section `%A'"),
1911 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1912 bfd_set_error (bfd_error_bad_value
);
1915 irela
+= bed
->s
->int_rels_per_ext_rel
;
1916 erela
+= shdr
->sh_entsize
;
1922 /* Read and swap the relocs for a section O. They may have been
1923 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1924 not NULL, they are used as buffers to read into. They are known to
1925 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1926 the return value is allocated using either malloc or bfd_alloc,
1927 according to the KEEP_MEMORY argument. If O has two relocation
1928 sections (both REL and RELA relocations), then the REL_HDR
1929 relocations will appear first in INTERNAL_RELOCS, followed by the
1930 REL_HDR2 relocations. */
1933 _bfd_elf_link_read_relocs (bfd
*abfd
,
1935 void *external_relocs
,
1936 Elf_Internal_Rela
*internal_relocs
,
1937 bfd_boolean keep_memory
)
1939 Elf_Internal_Shdr
*rel_hdr
;
1940 void *alloc1
= NULL
;
1941 Elf_Internal_Rela
*alloc2
= NULL
;
1942 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1944 if (elf_section_data (o
)->relocs
!= NULL
)
1945 return elf_section_data (o
)->relocs
;
1947 if (o
->reloc_count
== 0)
1950 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
1952 if (internal_relocs
== NULL
)
1956 size
= o
->reloc_count
;
1957 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
1959 internal_relocs
= bfd_alloc (abfd
, size
);
1961 internal_relocs
= alloc2
= bfd_malloc (size
);
1962 if (internal_relocs
== NULL
)
1966 if (external_relocs
== NULL
)
1968 bfd_size_type size
= rel_hdr
->sh_size
;
1970 if (elf_section_data (o
)->rel_hdr2
)
1971 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
1972 alloc1
= bfd_malloc (size
);
1975 external_relocs
= alloc1
;
1978 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
1982 if (elf_section_data (o
)->rel_hdr2
1983 && (!elf_link_read_relocs_from_section
1985 elf_section_data (o
)->rel_hdr2
,
1986 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
1987 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
1988 * bed
->s
->int_rels_per_ext_rel
))))
1991 /* Cache the results for next time, if we can. */
1993 elf_section_data (o
)->relocs
= internal_relocs
;
1998 /* Don't free alloc2, since if it was allocated we are passing it
1999 back (under the name of internal_relocs). */
2001 return internal_relocs
;
2011 /* Compute the size of, and allocate space for, REL_HDR which is the
2012 section header for a section containing relocations for O. */
2015 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2016 Elf_Internal_Shdr
*rel_hdr
,
2019 bfd_size_type reloc_count
;
2020 bfd_size_type num_rel_hashes
;
2022 /* Figure out how many relocations there will be. */
2023 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2024 reloc_count
= elf_section_data (o
)->rel_count
;
2026 reloc_count
= elf_section_data (o
)->rel_count2
;
2028 num_rel_hashes
= o
->reloc_count
;
2029 if (num_rel_hashes
< reloc_count
)
2030 num_rel_hashes
= reloc_count
;
2032 /* That allows us to calculate the size of the section. */
2033 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2035 /* The contents field must last into write_object_contents, so we
2036 allocate it with bfd_alloc rather than malloc. Also since we
2037 cannot be sure that the contents will actually be filled in,
2038 we zero the allocated space. */
2039 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2040 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2043 /* We only allocate one set of hash entries, so we only do it the
2044 first time we are called. */
2045 if (elf_section_data (o
)->rel_hashes
== NULL
2048 struct elf_link_hash_entry
**p
;
2050 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2054 elf_section_data (o
)->rel_hashes
= p
;
2060 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2061 originated from the section given by INPUT_REL_HDR) to the
2065 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2066 asection
*input_section
,
2067 Elf_Internal_Shdr
*input_rel_hdr
,
2068 Elf_Internal_Rela
*internal_relocs
)
2070 Elf_Internal_Rela
*irela
;
2071 Elf_Internal_Rela
*irelaend
;
2073 Elf_Internal_Shdr
*output_rel_hdr
;
2074 asection
*output_section
;
2075 unsigned int *rel_countp
= NULL
;
2076 const struct elf_backend_data
*bed
;
2077 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2079 output_section
= input_section
->output_section
;
2080 output_rel_hdr
= NULL
;
2082 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2083 == input_rel_hdr
->sh_entsize
)
2085 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2086 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2088 else if (elf_section_data (output_section
)->rel_hdr2
2089 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2090 == input_rel_hdr
->sh_entsize
))
2092 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2093 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2097 (*_bfd_error_handler
)
2098 (_("%B: relocation size mismatch in %B section %A"),
2099 output_bfd
, input_section
->owner
, input_section
);
2100 bfd_set_error (bfd_error_wrong_object_format
);
2104 bed
= get_elf_backend_data (output_bfd
);
2105 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2106 swap_out
= bed
->s
->swap_reloc_out
;
2107 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2108 swap_out
= bed
->s
->swap_reloca_out
;
2112 erel
= output_rel_hdr
->contents
;
2113 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2114 irela
= internal_relocs
;
2115 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2116 * bed
->s
->int_rels_per_ext_rel
);
2117 while (irela
< irelaend
)
2119 (*swap_out
) (output_bfd
, irela
, erel
);
2120 irela
+= bed
->s
->int_rels_per_ext_rel
;
2121 erel
+= input_rel_hdr
->sh_entsize
;
2124 /* Bump the counter, so that we know where to add the next set of
2126 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2131 /* Fix up the flags for a symbol. This handles various cases which
2132 can only be fixed after all the input files are seen. This is
2133 currently called by both adjust_dynamic_symbol and
2134 assign_sym_version, which is unnecessary but perhaps more robust in
2135 the face of future changes. */
2138 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2139 struct elf_info_failed
*eif
)
2141 /* If this symbol was mentioned in a non-ELF file, try to set
2142 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2143 permit a non-ELF file to correctly refer to a symbol defined in
2144 an ELF dynamic object. */
2147 while (h
->root
.type
== bfd_link_hash_indirect
)
2148 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2150 if (h
->root
.type
!= bfd_link_hash_defined
2151 && h
->root
.type
!= bfd_link_hash_defweak
)
2154 h
->ref_regular_nonweak
= 1;
2158 if (h
->root
.u
.def
.section
->owner
!= NULL
2159 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2160 == bfd_target_elf_flavour
))
2163 h
->ref_regular_nonweak
= 1;
2169 if (h
->dynindx
== -1
2173 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2182 /* Unfortunately, NON_ELF is only correct if the symbol
2183 was first seen in a non-ELF file. Fortunately, if the symbol
2184 was first seen in an ELF file, we're probably OK unless the
2185 symbol was defined in a non-ELF file. Catch that case here.
2186 FIXME: We're still in trouble if the symbol was first seen in
2187 a dynamic object, and then later in a non-ELF regular object. */
2188 if ((h
->root
.type
== bfd_link_hash_defined
2189 || h
->root
.type
== bfd_link_hash_defweak
)
2191 && (h
->root
.u
.def
.section
->owner
!= NULL
2192 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2193 != bfd_target_elf_flavour
)
2194 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2195 && !h
->def_dynamic
)))
2199 /* If this is a final link, and the symbol was defined as a common
2200 symbol in a regular object file, and there was no definition in
2201 any dynamic object, then the linker will have allocated space for
2202 the symbol in a common section but the DEF_REGULAR
2203 flag will not have been set. */
2204 if (h
->root
.type
== bfd_link_hash_defined
2208 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2211 /* If -Bsymbolic was used (which means to bind references to global
2212 symbols to the definition within the shared object), and this
2213 symbol was defined in a regular object, then it actually doesn't
2214 need a PLT entry. Likewise, if the symbol has non-default
2215 visibility. If the symbol has hidden or internal visibility, we
2216 will force it local. */
2218 && eif
->info
->shared
2219 && is_elf_hash_table (eif
->info
->hash
)
2220 && (eif
->info
->symbolic
2221 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2224 const struct elf_backend_data
*bed
;
2225 bfd_boolean force_local
;
2227 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2229 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2230 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2231 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2234 /* If a weak undefined symbol has non-default visibility, we also
2235 hide it from the dynamic linker. */
2236 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2237 && h
->root
.type
== bfd_link_hash_undefweak
)
2239 const struct elf_backend_data
*bed
;
2240 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2241 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2244 /* If this is a weak defined symbol in a dynamic object, and we know
2245 the real definition in the dynamic object, copy interesting flags
2246 over to the real definition. */
2247 if (h
->u
.weakdef
!= NULL
)
2249 struct elf_link_hash_entry
*weakdef
;
2251 weakdef
= h
->u
.weakdef
;
2252 if (h
->root
.type
== bfd_link_hash_indirect
)
2253 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2255 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2256 || h
->root
.type
== bfd_link_hash_defweak
);
2257 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2258 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2259 BFD_ASSERT (weakdef
->def_dynamic
);
2261 /* If the real definition is defined by a regular object file,
2262 don't do anything special. See the longer description in
2263 _bfd_elf_adjust_dynamic_symbol, below. */
2264 if (weakdef
->def_regular
)
2265 h
->u
.weakdef
= NULL
;
2268 const struct elf_backend_data
*bed
;
2270 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2271 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2278 /* Make the backend pick a good value for a dynamic symbol. This is
2279 called via elf_link_hash_traverse, and also calls itself
2283 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2285 struct elf_info_failed
*eif
= data
;
2287 const struct elf_backend_data
*bed
;
2289 if (! is_elf_hash_table (eif
->info
->hash
))
2292 if (h
->root
.type
== bfd_link_hash_warning
)
2294 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2295 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
2297 /* When warning symbols are created, they **replace** the "real"
2298 entry in the hash table, thus we never get to see the real
2299 symbol in a hash traversal. So look at it now. */
2300 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2303 /* Ignore indirect symbols. These are added by the versioning code. */
2304 if (h
->root
.type
== bfd_link_hash_indirect
)
2307 /* Fix the symbol flags. */
2308 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2311 /* If this symbol does not require a PLT entry, and it is not
2312 defined by a dynamic object, or is not referenced by a regular
2313 object, ignore it. We do have to handle a weak defined symbol,
2314 even if no regular object refers to it, if we decided to add it
2315 to the dynamic symbol table. FIXME: Do we normally need to worry
2316 about symbols which are defined by one dynamic object and
2317 referenced by another one? */
2322 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2324 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2328 /* If we've already adjusted this symbol, don't do it again. This
2329 can happen via a recursive call. */
2330 if (h
->dynamic_adjusted
)
2333 /* Don't look at this symbol again. Note that we must set this
2334 after checking the above conditions, because we may look at a
2335 symbol once, decide not to do anything, and then get called
2336 recursively later after REF_REGULAR is set below. */
2337 h
->dynamic_adjusted
= 1;
2339 /* If this is a weak definition, and we know a real definition, and
2340 the real symbol is not itself defined by a regular object file,
2341 then get a good value for the real definition. We handle the
2342 real symbol first, for the convenience of the backend routine.
2344 Note that there is a confusing case here. If the real definition
2345 is defined by a regular object file, we don't get the real symbol
2346 from the dynamic object, but we do get the weak symbol. If the
2347 processor backend uses a COPY reloc, then if some routine in the
2348 dynamic object changes the real symbol, we will not see that
2349 change in the corresponding weak symbol. This is the way other
2350 ELF linkers work as well, and seems to be a result of the shared
2353 I will clarify this issue. Most SVR4 shared libraries define the
2354 variable _timezone and define timezone as a weak synonym. The
2355 tzset call changes _timezone. If you write
2356 extern int timezone;
2358 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2359 you might expect that, since timezone is a synonym for _timezone,
2360 the same number will print both times. However, if the processor
2361 backend uses a COPY reloc, then actually timezone will be copied
2362 into your process image, and, since you define _timezone
2363 yourself, _timezone will not. Thus timezone and _timezone will
2364 wind up at different memory locations. The tzset call will set
2365 _timezone, leaving timezone unchanged. */
2367 if (h
->u
.weakdef
!= NULL
)
2369 /* If we get to this point, we know there is an implicit
2370 reference by a regular object file via the weak symbol H.
2371 FIXME: Is this really true? What if the traversal finds
2372 H->U.WEAKDEF before it finds H? */
2373 h
->u
.weakdef
->ref_regular
= 1;
2375 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2379 /* If a symbol has no type and no size and does not require a PLT
2380 entry, then we are probably about to do the wrong thing here: we
2381 are probably going to create a COPY reloc for an empty object.
2382 This case can arise when a shared object is built with assembly
2383 code, and the assembly code fails to set the symbol type. */
2385 && h
->type
== STT_NOTYPE
2387 (*_bfd_error_handler
)
2388 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2389 h
->root
.root
.string
);
2391 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2392 bed
= get_elf_backend_data (dynobj
);
2393 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2402 /* Adjust all external symbols pointing into SEC_MERGE sections
2403 to reflect the object merging within the sections. */
2406 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2410 if (h
->root
.type
== bfd_link_hash_warning
)
2411 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2413 if ((h
->root
.type
== bfd_link_hash_defined
2414 || h
->root
.type
== bfd_link_hash_defweak
)
2415 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2416 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2418 bfd
*output_bfd
= data
;
2420 h
->root
.u
.def
.value
=
2421 _bfd_merged_section_offset (output_bfd
,
2422 &h
->root
.u
.def
.section
,
2423 elf_section_data (sec
)->sec_info
,
2424 h
->root
.u
.def
.value
);
2430 /* Returns false if the symbol referred to by H should be considered
2431 to resolve local to the current module, and true if it should be
2432 considered to bind dynamically. */
2435 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2436 struct bfd_link_info
*info
,
2437 bfd_boolean ignore_protected
)
2439 bfd_boolean binding_stays_local_p
;
2444 while (h
->root
.type
== bfd_link_hash_indirect
2445 || h
->root
.type
== bfd_link_hash_warning
)
2446 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2448 /* If it was forced local, then clearly it's not dynamic. */
2449 if (h
->dynindx
== -1)
2451 if (h
->forced_local
)
2454 /* Identify the cases where name binding rules say that a
2455 visible symbol resolves locally. */
2456 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2458 switch (ELF_ST_VISIBILITY (h
->other
))
2465 /* Proper resolution for function pointer equality may require
2466 that these symbols perhaps be resolved dynamically, even though
2467 we should be resolving them to the current module. */
2468 if (!ignore_protected
)
2469 binding_stays_local_p
= TRUE
;
2476 /* If it isn't defined locally, then clearly it's dynamic. */
2477 if (!h
->def_regular
)
2480 /* Otherwise, the symbol is dynamic if binding rules don't tell
2481 us that it remains local. */
2482 return !binding_stays_local_p
;
2485 /* Return true if the symbol referred to by H should be considered
2486 to resolve local to the current module, and false otherwise. Differs
2487 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2488 undefined symbols and weak symbols. */
2491 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2492 struct bfd_link_info
*info
,
2493 bfd_boolean local_protected
)
2495 /* If it's a local sym, of course we resolve locally. */
2499 /* Common symbols that become definitions don't get the DEF_REGULAR
2500 flag set, so test it first, and don't bail out. */
2501 if (ELF_COMMON_DEF_P (h
))
2503 /* If we don't have a definition in a regular file, then we can't
2504 resolve locally. The sym is either undefined or dynamic. */
2505 else if (!h
->def_regular
)
2508 /* Forced local symbols resolve locally. */
2509 if (h
->forced_local
)
2512 /* As do non-dynamic symbols. */
2513 if (h
->dynindx
== -1)
2516 /* At this point, we know the symbol is defined and dynamic. In an
2517 executable it must resolve locally, likewise when building symbolic
2518 shared libraries. */
2519 if (info
->executable
|| info
->symbolic
)
2522 /* Now deal with defined dynamic symbols in shared libraries. Ones
2523 with default visibility might not resolve locally. */
2524 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2527 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2528 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2531 /* Function pointer equality tests may require that STV_PROTECTED
2532 symbols be treated as dynamic symbols, even when we know that the
2533 dynamic linker will resolve them locally. */
2534 return local_protected
;
2537 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2538 aligned. Returns the first TLS output section. */
2540 struct bfd_section
*
2541 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2543 struct bfd_section
*sec
, *tls
;
2544 unsigned int align
= 0;
2546 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2547 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2551 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2552 if (sec
->alignment_power
> align
)
2553 align
= sec
->alignment_power
;
2555 elf_hash_table (info
)->tls_sec
= tls
;
2557 /* Ensure the alignment of the first section is the largest alignment,
2558 so that the tls segment starts aligned. */
2560 tls
->alignment_power
= align
;
2565 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2567 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2568 Elf_Internal_Sym
*sym
)
2570 /* Local symbols do not count, but target specific ones might. */
2571 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2572 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2575 /* Function symbols do not count. */
2576 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2579 /* If the section is undefined, then so is the symbol. */
2580 if (sym
->st_shndx
== SHN_UNDEF
)
2583 /* If the symbol is defined in the common section, then
2584 it is a common definition and so does not count. */
2585 if (sym
->st_shndx
== SHN_COMMON
)
2588 /* If the symbol is in a target specific section then we
2589 must rely upon the backend to tell us what it is. */
2590 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2591 /* FIXME - this function is not coded yet:
2593 return _bfd_is_global_symbol_definition (abfd, sym);
2595 Instead for now assume that the definition is not global,
2596 Even if this is wrong, at least the linker will behave
2597 in the same way that it used to do. */
2603 /* Search the symbol table of the archive element of the archive ABFD
2604 whose archive map contains a mention of SYMDEF, and determine if
2605 the symbol is defined in this element. */
2607 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2609 Elf_Internal_Shdr
* hdr
;
2610 bfd_size_type symcount
;
2611 bfd_size_type extsymcount
;
2612 bfd_size_type extsymoff
;
2613 Elf_Internal_Sym
*isymbuf
;
2614 Elf_Internal_Sym
*isym
;
2615 Elf_Internal_Sym
*isymend
;
2618 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2622 if (! bfd_check_format (abfd
, bfd_object
))
2625 /* If we have already included the element containing this symbol in the
2626 link then we do not need to include it again. Just claim that any symbol
2627 it contains is not a definition, so that our caller will not decide to
2628 (re)include this element. */
2629 if (abfd
->archive_pass
)
2632 /* Select the appropriate symbol table. */
2633 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2634 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2636 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2638 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2640 /* The sh_info field of the symtab header tells us where the
2641 external symbols start. We don't care about the local symbols. */
2642 if (elf_bad_symtab (abfd
))
2644 extsymcount
= symcount
;
2649 extsymcount
= symcount
- hdr
->sh_info
;
2650 extsymoff
= hdr
->sh_info
;
2653 if (extsymcount
== 0)
2656 /* Read in the symbol table. */
2657 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2659 if (isymbuf
== NULL
)
2662 /* Scan the symbol table looking for SYMDEF. */
2664 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2668 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2673 if (strcmp (name
, symdef
->name
) == 0)
2675 result
= is_global_data_symbol_definition (abfd
, isym
);
2685 /* Add an entry to the .dynamic table. */
2688 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2692 struct elf_link_hash_table
*hash_table
;
2693 const struct elf_backend_data
*bed
;
2695 bfd_size_type newsize
;
2696 bfd_byte
*newcontents
;
2697 Elf_Internal_Dyn dyn
;
2699 hash_table
= elf_hash_table (info
);
2700 if (! is_elf_hash_table (hash_table
))
2703 if (info
->warn_shared_textrel
&& info
->shared
&& tag
== DT_TEXTREL
)
2705 (_("warning: creating a DT_TEXTREL in a shared object."));
2707 bed
= get_elf_backend_data (hash_table
->dynobj
);
2708 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2709 BFD_ASSERT (s
!= NULL
);
2711 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2712 newcontents
= bfd_realloc (s
->contents
, newsize
);
2713 if (newcontents
== NULL
)
2717 dyn
.d_un
.d_val
= val
;
2718 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2721 s
->contents
= newcontents
;
2726 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2727 otherwise just check whether one already exists. Returns -1 on error,
2728 1 if a DT_NEEDED tag already exists, and 0 on success. */
2731 elf_add_dt_needed_tag (struct bfd_link_info
*info
,
2735 struct elf_link_hash_table
*hash_table
;
2736 bfd_size_type oldsize
;
2737 bfd_size_type strindex
;
2739 hash_table
= elf_hash_table (info
);
2740 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2741 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2742 if (strindex
== (bfd_size_type
) -1)
2745 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2748 const struct elf_backend_data
*bed
;
2751 bed
= get_elf_backend_data (hash_table
->dynobj
);
2752 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2753 BFD_ASSERT (sdyn
!= NULL
);
2755 for (extdyn
= sdyn
->contents
;
2756 extdyn
< sdyn
->contents
+ sdyn
->size
;
2757 extdyn
+= bed
->s
->sizeof_dyn
)
2759 Elf_Internal_Dyn dyn
;
2761 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2762 if (dyn
.d_tag
== DT_NEEDED
2763 && dyn
.d_un
.d_val
== strindex
)
2765 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2773 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2777 /* We were just checking for existence of the tag. */
2778 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2783 /* Sort symbol by value and section. */
2785 elf_sort_symbol (const void *arg1
, const void *arg2
)
2787 const struct elf_link_hash_entry
*h1
;
2788 const struct elf_link_hash_entry
*h2
;
2789 bfd_signed_vma vdiff
;
2791 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2792 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2793 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2795 return vdiff
> 0 ? 1 : -1;
2798 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2800 return sdiff
> 0 ? 1 : -1;
2805 /* This function is used to adjust offsets into .dynstr for
2806 dynamic symbols. This is called via elf_link_hash_traverse. */
2809 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2811 struct elf_strtab_hash
*dynstr
= data
;
2813 if (h
->root
.type
== bfd_link_hash_warning
)
2814 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2816 if (h
->dynindx
!= -1)
2817 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2821 /* Assign string offsets in .dynstr, update all structures referencing
2825 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2827 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2828 struct elf_link_local_dynamic_entry
*entry
;
2829 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2830 bfd
*dynobj
= hash_table
->dynobj
;
2833 const struct elf_backend_data
*bed
;
2836 _bfd_elf_strtab_finalize (dynstr
);
2837 size
= _bfd_elf_strtab_size (dynstr
);
2839 bed
= get_elf_backend_data (dynobj
);
2840 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2841 BFD_ASSERT (sdyn
!= NULL
);
2843 /* Update all .dynamic entries referencing .dynstr strings. */
2844 for (extdyn
= sdyn
->contents
;
2845 extdyn
< sdyn
->contents
+ sdyn
->size
;
2846 extdyn
+= bed
->s
->sizeof_dyn
)
2848 Elf_Internal_Dyn dyn
;
2850 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
2854 dyn
.d_un
.d_val
= size
;
2862 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2867 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
2870 /* Now update local dynamic symbols. */
2871 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
2872 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2873 entry
->isym
.st_name
);
2875 /* And the rest of dynamic symbols. */
2876 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
2878 /* Adjust version definitions. */
2879 if (elf_tdata (output_bfd
)->cverdefs
)
2884 Elf_Internal_Verdef def
;
2885 Elf_Internal_Verdaux defaux
;
2887 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2891 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
2893 p
+= sizeof (Elf_External_Verdef
);
2894 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2896 _bfd_elf_swap_verdaux_in (output_bfd
,
2897 (Elf_External_Verdaux
*) p
, &defaux
);
2898 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2900 _bfd_elf_swap_verdaux_out (output_bfd
,
2901 &defaux
, (Elf_External_Verdaux
*) p
);
2902 p
+= sizeof (Elf_External_Verdaux
);
2905 while (def
.vd_next
);
2908 /* Adjust version references. */
2909 if (elf_tdata (output_bfd
)->verref
)
2914 Elf_Internal_Verneed need
;
2915 Elf_Internal_Vernaux needaux
;
2917 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2921 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
2923 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
2924 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
2925 (Elf_External_Verneed
*) p
);
2926 p
+= sizeof (Elf_External_Verneed
);
2927 for (i
= 0; i
< need
.vn_cnt
; ++i
)
2929 _bfd_elf_swap_vernaux_in (output_bfd
,
2930 (Elf_External_Vernaux
*) p
, &needaux
);
2931 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
2933 _bfd_elf_swap_vernaux_out (output_bfd
,
2935 (Elf_External_Vernaux
*) p
);
2936 p
+= sizeof (Elf_External_Vernaux
);
2939 while (need
.vn_next
);
2945 /* Add symbols from an ELF object file to the linker hash table. */
2948 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2950 bfd_boolean (*add_symbol_hook
)
2951 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
2952 const char **, flagword
*, asection
**, bfd_vma
*);
2953 bfd_boolean (*check_relocs
)
2954 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
2955 bfd_boolean (*check_directives
)
2956 (bfd
*, struct bfd_link_info
*);
2957 bfd_boolean collect
;
2958 Elf_Internal_Shdr
*hdr
;
2959 bfd_size_type symcount
;
2960 bfd_size_type extsymcount
;
2961 bfd_size_type extsymoff
;
2962 struct elf_link_hash_entry
**sym_hash
;
2963 bfd_boolean dynamic
;
2964 Elf_External_Versym
*extversym
= NULL
;
2965 Elf_External_Versym
*ever
;
2966 struct elf_link_hash_entry
*weaks
;
2967 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
2968 bfd_size_type nondeflt_vers_cnt
= 0;
2969 Elf_Internal_Sym
*isymbuf
= NULL
;
2970 Elf_Internal_Sym
*isym
;
2971 Elf_Internal_Sym
*isymend
;
2972 const struct elf_backend_data
*bed
;
2973 bfd_boolean add_needed
;
2974 struct elf_link_hash_table
* hash_table
;
2977 hash_table
= elf_hash_table (info
);
2979 bed
= get_elf_backend_data (abfd
);
2980 add_symbol_hook
= bed
->elf_add_symbol_hook
;
2981 collect
= bed
->collect
;
2983 if ((abfd
->flags
& DYNAMIC
) == 0)
2989 /* You can't use -r against a dynamic object. Also, there's no
2990 hope of using a dynamic object which does not exactly match
2991 the format of the output file. */
2992 if (info
->relocatable
2993 || !is_elf_hash_table (hash_table
)
2994 || hash_table
->root
.creator
!= abfd
->xvec
)
2996 if (info
->relocatable
)
2997 bfd_set_error (bfd_error_invalid_operation
);
2999 bfd_set_error (bfd_error_wrong_format
);
3004 /* As a GNU extension, any input sections which are named
3005 .gnu.warning.SYMBOL are treated as warning symbols for the given
3006 symbol. This differs from .gnu.warning sections, which generate
3007 warnings when they are included in an output file. */
3008 if (info
->executable
)
3012 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3016 name
= bfd_get_section_name (abfd
, s
);
3017 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3021 bfd_size_type prefix_len
;
3022 const char * gnu_warning_prefix
= _("warning: ");
3024 name
+= sizeof ".gnu.warning." - 1;
3026 /* If this is a shared object, then look up the symbol
3027 in the hash table. If it is there, and it is already
3028 been defined, then we will not be using the entry
3029 from this shared object, so we don't need to warn.
3030 FIXME: If we see the definition in a regular object
3031 later on, we will warn, but we shouldn't. The only
3032 fix is to keep track of what warnings we are supposed
3033 to emit, and then handle them all at the end of the
3037 struct elf_link_hash_entry
*h
;
3039 h
= elf_link_hash_lookup (hash_table
, name
,
3040 FALSE
, FALSE
, TRUE
);
3042 /* FIXME: What about bfd_link_hash_common? */
3044 && (h
->root
.type
== bfd_link_hash_defined
3045 || h
->root
.type
== bfd_link_hash_defweak
))
3047 /* We don't want to issue this warning. Clobber
3048 the section size so that the warning does not
3049 get copied into the output file. */
3056 prefix_len
= strlen (gnu_warning_prefix
);
3057 msg
= bfd_alloc (abfd
, prefix_len
+ sz
+ 1);
3061 strcpy (msg
, gnu_warning_prefix
);
3062 if (! bfd_get_section_contents (abfd
, s
, msg
+ prefix_len
, 0, sz
))
3065 msg
[prefix_len
+ sz
] = '\0';
3067 if (! (_bfd_generic_link_add_one_symbol
3068 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3069 FALSE
, collect
, NULL
)))
3072 if (! info
->relocatable
)
3074 /* Clobber the section size so that the warning does
3075 not get copied into the output file. */
3085 /* If we are creating a shared library, create all the dynamic
3086 sections immediately. We need to attach them to something,
3087 so we attach them to this BFD, provided it is the right
3088 format. FIXME: If there are no input BFD's of the same
3089 format as the output, we can't make a shared library. */
3091 && is_elf_hash_table (hash_table
)
3092 && hash_table
->root
.creator
== abfd
->xvec
3093 && ! hash_table
->dynamic_sections_created
)
3095 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3099 else if (!is_elf_hash_table (hash_table
))
3104 const char *soname
= NULL
;
3105 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3108 /* ld --just-symbols and dynamic objects don't mix very well.
3109 Test for --just-symbols by looking at info set up by
3110 _bfd_elf_link_just_syms. */
3111 if ((s
= abfd
->sections
) != NULL
3112 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3115 /* If this dynamic lib was specified on the command line with
3116 --as-needed in effect, then we don't want to add a DT_NEEDED
3117 tag unless the lib is actually used. Similary for libs brought
3118 in by another lib's DT_NEEDED. When --no-add-needed is used
3119 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3120 any dynamic library in DT_NEEDED tags in the dynamic lib at
3122 add_needed
= (elf_dyn_lib_class (abfd
)
3123 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3124 | DYN_NO_NEEDED
)) == 0;
3126 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3132 unsigned long shlink
;
3134 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3135 goto error_free_dyn
;
3137 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3139 goto error_free_dyn
;
3140 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3142 for (extdyn
= dynbuf
;
3143 extdyn
< dynbuf
+ s
->size
;
3144 extdyn
+= bed
->s
->sizeof_dyn
)
3146 Elf_Internal_Dyn dyn
;
3148 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3149 if (dyn
.d_tag
== DT_SONAME
)
3151 unsigned int tagv
= dyn
.d_un
.d_val
;
3152 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3154 goto error_free_dyn
;
3156 if (dyn
.d_tag
== DT_NEEDED
)
3158 struct bfd_link_needed_list
*n
, **pn
;
3160 unsigned int tagv
= dyn
.d_un
.d_val
;
3162 amt
= sizeof (struct bfd_link_needed_list
);
3163 n
= bfd_alloc (abfd
, amt
);
3164 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3165 if (n
== NULL
|| fnm
== NULL
)
3166 goto error_free_dyn
;
3167 amt
= strlen (fnm
) + 1;
3168 anm
= bfd_alloc (abfd
, amt
);
3170 goto error_free_dyn
;
3171 memcpy (anm
, fnm
, amt
);
3175 for (pn
= & hash_table
->needed
;
3181 if (dyn
.d_tag
== DT_RUNPATH
)
3183 struct bfd_link_needed_list
*n
, **pn
;
3185 unsigned int tagv
= dyn
.d_un
.d_val
;
3187 amt
= sizeof (struct bfd_link_needed_list
);
3188 n
= bfd_alloc (abfd
, amt
);
3189 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3190 if (n
== NULL
|| fnm
== NULL
)
3191 goto error_free_dyn
;
3192 amt
= strlen (fnm
) + 1;
3193 anm
= bfd_alloc (abfd
, amt
);
3195 goto error_free_dyn
;
3196 memcpy (anm
, fnm
, amt
);
3200 for (pn
= & runpath
;
3206 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3207 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3209 struct bfd_link_needed_list
*n
, **pn
;
3211 unsigned int tagv
= dyn
.d_un
.d_val
;
3213 amt
= sizeof (struct bfd_link_needed_list
);
3214 n
= bfd_alloc (abfd
, amt
);
3215 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3216 if (n
== NULL
|| fnm
== NULL
)
3217 goto error_free_dyn
;
3218 amt
= strlen (fnm
) + 1;
3219 anm
= bfd_alloc (abfd
, amt
);
3226 memcpy (anm
, fnm
, amt
);
3241 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3242 frees all more recently bfd_alloc'd blocks as well. */
3248 struct bfd_link_needed_list
**pn
;
3249 for (pn
= & hash_table
->runpath
;
3256 /* We do not want to include any of the sections in a dynamic
3257 object in the output file. We hack by simply clobbering the
3258 list of sections in the BFD. This could be handled more
3259 cleanly by, say, a new section flag; the existing
3260 SEC_NEVER_LOAD flag is not the one we want, because that one
3261 still implies that the section takes up space in the output
3263 bfd_section_list_clear (abfd
);
3265 /* If this is the first dynamic object found in the link, create
3266 the special sections required for dynamic linking. */
3267 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3270 /* Find the name to use in a DT_NEEDED entry that refers to this
3271 object. If the object has a DT_SONAME entry, we use it.
3272 Otherwise, if the generic linker stuck something in
3273 elf_dt_name, we use that. Otherwise, we just use the file
3275 if (soname
== NULL
|| *soname
== '\0')
3277 soname
= elf_dt_name (abfd
);
3278 if (soname
== NULL
|| *soname
== '\0')
3279 soname
= bfd_get_filename (abfd
);
3282 /* Save the SONAME because sometimes the linker emulation code
3283 will need to know it. */
3284 elf_dt_name (abfd
) = soname
;
3286 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3290 /* If we have already included this dynamic object in the
3291 link, just ignore it. There is no reason to include a
3292 particular dynamic object more than once. */
3297 /* If this is a dynamic object, we always link against the .dynsym
3298 symbol table, not the .symtab symbol table. The dynamic linker
3299 will only see the .dynsym symbol table, so there is no reason to
3300 look at .symtab for a dynamic object. */
3302 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3303 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3305 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3307 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3309 /* The sh_info field of the symtab header tells us where the
3310 external symbols start. We don't care about the local symbols at
3312 if (elf_bad_symtab (abfd
))
3314 extsymcount
= symcount
;
3319 extsymcount
= symcount
- hdr
->sh_info
;
3320 extsymoff
= hdr
->sh_info
;
3324 if (extsymcount
!= 0)
3326 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3328 if (isymbuf
== NULL
)
3331 /* We store a pointer to the hash table entry for each external
3333 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3334 sym_hash
= bfd_alloc (abfd
, amt
);
3335 if (sym_hash
== NULL
)
3336 goto error_free_sym
;
3337 elf_sym_hashes (abfd
) = sym_hash
;
3342 /* Read in any version definitions. */
3343 if (! _bfd_elf_slurp_version_tables (abfd
))
3344 goto error_free_sym
;
3346 /* Read in the symbol versions, but don't bother to convert them
3347 to internal format. */
3348 if (elf_dynversym (abfd
) != 0)
3350 Elf_Internal_Shdr
*versymhdr
;
3352 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3353 extversym
= bfd_malloc (versymhdr
->sh_size
);
3354 if (extversym
== NULL
)
3355 goto error_free_sym
;
3356 amt
= versymhdr
->sh_size
;
3357 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3358 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3359 goto error_free_vers
;
3365 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3366 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3368 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3375 struct elf_link_hash_entry
*h
;
3376 bfd_boolean definition
;
3377 bfd_boolean size_change_ok
;
3378 bfd_boolean type_change_ok
;
3379 bfd_boolean new_weakdef
;
3380 bfd_boolean override
;
3381 unsigned int old_alignment
;
3386 flags
= BSF_NO_FLAGS
;
3388 value
= isym
->st_value
;
3391 bind
= ELF_ST_BIND (isym
->st_info
);
3392 if (bind
== STB_LOCAL
)
3394 /* This should be impossible, since ELF requires that all
3395 global symbols follow all local symbols, and that sh_info
3396 point to the first global symbol. Unfortunately, Irix 5
3400 else if (bind
== STB_GLOBAL
)
3402 if (isym
->st_shndx
!= SHN_UNDEF
3403 && isym
->st_shndx
!= SHN_COMMON
)
3406 else if (bind
== STB_WEAK
)
3410 /* Leave it up to the processor backend. */
3413 if (isym
->st_shndx
== SHN_UNDEF
)
3414 sec
= bfd_und_section_ptr
;
3415 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3417 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3419 sec
= bfd_abs_section_ptr
;
3420 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3423 else if (isym
->st_shndx
== SHN_ABS
)
3424 sec
= bfd_abs_section_ptr
;
3425 else if (isym
->st_shndx
== SHN_COMMON
)
3427 sec
= bfd_com_section_ptr
;
3428 /* What ELF calls the size we call the value. What ELF
3429 calls the value we call the alignment. */
3430 value
= isym
->st_size
;
3434 /* Leave it up to the processor backend. */
3437 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3440 goto error_free_vers
;
3442 if (isym
->st_shndx
== SHN_COMMON
3443 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3445 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3449 tcomm
= bfd_make_section (abfd
, ".tcommon");
3451 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
3453 | SEC_LINKER_CREATED
3454 | SEC_THREAD_LOCAL
)))
3455 goto error_free_vers
;
3459 else if (add_symbol_hook
)
3461 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3463 goto error_free_vers
;
3465 /* The hook function sets the name to NULL if this symbol
3466 should be skipped for some reason. */
3471 /* Sanity check that all possibilities were handled. */
3474 bfd_set_error (bfd_error_bad_value
);
3475 goto error_free_vers
;
3478 if (bfd_is_und_section (sec
)
3479 || bfd_is_com_section (sec
))
3484 size_change_ok
= FALSE
;
3485 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3489 if (is_elf_hash_table (hash_table
))
3491 Elf_Internal_Versym iver
;
3492 unsigned int vernum
= 0;
3497 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3498 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3500 /* If this is a hidden symbol, or if it is not version
3501 1, we append the version name to the symbol name.
3502 However, we do not modify a non-hidden absolute
3503 symbol, because it might be the version symbol
3504 itself. FIXME: What if it isn't? */
3505 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3506 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
3509 size_t namelen
, verlen
, newlen
;
3512 if (isym
->st_shndx
!= SHN_UNDEF
)
3514 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
3516 (*_bfd_error_handler
)
3517 (_("%B: %s: invalid version %u (max %d)"),
3519 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
3520 bfd_set_error (bfd_error_bad_value
);
3521 goto error_free_vers
;
3523 else if (vernum
> 1)
3525 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3531 /* We cannot simply test for the number of
3532 entries in the VERNEED section since the
3533 numbers for the needed versions do not start
3535 Elf_Internal_Verneed
*t
;
3538 for (t
= elf_tdata (abfd
)->verref
;
3542 Elf_Internal_Vernaux
*a
;
3544 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3546 if (a
->vna_other
== vernum
)
3548 verstr
= a
->vna_nodename
;
3557 (*_bfd_error_handler
)
3558 (_("%B: %s: invalid needed version %d"),
3559 abfd
, name
, vernum
);
3560 bfd_set_error (bfd_error_bad_value
);
3561 goto error_free_vers
;
3565 namelen
= strlen (name
);
3566 verlen
= strlen (verstr
);
3567 newlen
= namelen
+ verlen
+ 2;
3568 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3569 && isym
->st_shndx
!= SHN_UNDEF
)
3572 newname
= bfd_alloc (abfd
, newlen
);
3573 if (newname
== NULL
)
3574 goto error_free_vers
;
3575 memcpy (newname
, name
, namelen
);
3576 p
= newname
+ namelen
;
3578 /* If this is a defined non-hidden version symbol,
3579 we add another @ to the name. This indicates the
3580 default version of the symbol. */
3581 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3582 && isym
->st_shndx
!= SHN_UNDEF
)
3584 memcpy (p
, verstr
, verlen
+ 1);
3590 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
3591 sym_hash
, &skip
, &override
,
3592 &type_change_ok
, &size_change_ok
))
3593 goto error_free_vers
;
3602 while (h
->root
.type
== bfd_link_hash_indirect
3603 || h
->root
.type
== bfd_link_hash_warning
)
3604 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3606 /* Remember the old alignment if this is a common symbol, so
3607 that we don't reduce the alignment later on. We can't
3608 check later, because _bfd_generic_link_add_one_symbol
3609 will set a default for the alignment which we want to
3610 override. We also remember the old bfd where the existing
3611 definition comes from. */
3612 switch (h
->root
.type
)
3617 case bfd_link_hash_defined
:
3618 case bfd_link_hash_defweak
:
3619 old_bfd
= h
->root
.u
.def
.section
->owner
;
3622 case bfd_link_hash_common
:
3623 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3624 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3628 if (elf_tdata (abfd
)->verdef
!= NULL
3632 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3635 if (! (_bfd_generic_link_add_one_symbol
3636 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3637 (struct bfd_link_hash_entry
**) sym_hash
)))
3638 goto error_free_vers
;
3641 while (h
->root
.type
== bfd_link_hash_indirect
3642 || h
->root
.type
== bfd_link_hash_warning
)
3643 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3646 new_weakdef
= FALSE
;
3649 && (flags
& BSF_WEAK
) != 0
3650 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3651 && is_elf_hash_table (hash_table
)
3652 && h
->u
.weakdef
== NULL
)
3654 /* Keep a list of all weak defined non function symbols from
3655 a dynamic object, using the weakdef field. Later in this
3656 function we will set the weakdef field to the correct
3657 value. We only put non-function symbols from dynamic
3658 objects on this list, because that happens to be the only
3659 time we need to know the normal symbol corresponding to a
3660 weak symbol, and the information is time consuming to
3661 figure out. If the weakdef field is not already NULL,
3662 then this symbol was already defined by some previous
3663 dynamic object, and we will be using that previous
3664 definition anyhow. */
3666 h
->u
.weakdef
= weaks
;
3671 /* Set the alignment of a common symbol. */
3672 if (isym
->st_shndx
== SHN_COMMON
3673 && h
->root
.type
== bfd_link_hash_common
)
3677 align
= bfd_log2 (isym
->st_value
);
3678 if (align
> old_alignment
3679 /* Permit an alignment power of zero if an alignment of one
3680 is specified and no other alignments have been specified. */
3681 || (isym
->st_value
== 1 && old_alignment
== 0))
3682 h
->root
.u
.c
.p
->alignment_power
= align
;
3684 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3687 if (is_elf_hash_table (hash_table
))
3691 /* Check the alignment when a common symbol is involved. This
3692 can change when a common symbol is overridden by a normal
3693 definition or a common symbol is ignored due to the old
3694 normal definition. We need to make sure the maximum
3695 alignment is maintained. */
3696 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
3697 && h
->root
.type
!= bfd_link_hash_common
)
3699 unsigned int common_align
;
3700 unsigned int normal_align
;
3701 unsigned int symbol_align
;
3705 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3706 if (h
->root
.u
.def
.section
->owner
!= NULL
3707 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3709 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3710 if (normal_align
> symbol_align
)
3711 normal_align
= symbol_align
;
3714 normal_align
= symbol_align
;
3718 common_align
= old_alignment
;
3719 common_bfd
= old_bfd
;
3724 common_align
= bfd_log2 (isym
->st_value
);
3726 normal_bfd
= old_bfd
;
3729 if (normal_align
< common_align
)
3730 (*_bfd_error_handler
)
3731 (_("Warning: alignment %u of symbol `%s' in %B"
3732 " is smaller than %u in %B"),
3733 normal_bfd
, common_bfd
,
3734 1 << normal_align
, name
, 1 << common_align
);
3737 /* Remember the symbol size and type. */
3738 if (isym
->st_size
!= 0
3739 && (definition
|| h
->size
== 0))
3741 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3742 (*_bfd_error_handler
)
3743 (_("Warning: size of symbol `%s' changed"
3744 " from %lu in %B to %lu in %B"),
3746 name
, (unsigned long) h
->size
,
3747 (unsigned long) isym
->st_size
);
3749 h
->size
= isym
->st_size
;
3752 /* If this is a common symbol, then we always want H->SIZE
3753 to be the size of the common symbol. The code just above
3754 won't fix the size if a common symbol becomes larger. We
3755 don't warn about a size change here, because that is
3756 covered by --warn-common. */
3757 if (h
->root
.type
== bfd_link_hash_common
)
3758 h
->size
= h
->root
.u
.c
.size
;
3760 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3761 && (definition
|| h
->type
== STT_NOTYPE
))
3763 if (h
->type
!= STT_NOTYPE
3764 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3765 && ! type_change_ok
)
3766 (*_bfd_error_handler
)
3767 (_("Warning: type of symbol `%s' changed"
3768 " from %d to %d in %B"),
3769 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3771 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3774 /* If st_other has a processor-specific meaning, specific
3775 code might be needed here. We never merge the visibility
3776 attribute with the one from a dynamic object. */
3777 if (bed
->elf_backend_merge_symbol_attribute
)
3778 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3781 /* If this symbol has default visibility and the user has requested
3782 we not re-export it, then mark it as hidden. */
3783 if (definition
&& !dynamic
3785 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
3786 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
3787 isym
->st_other
= STV_HIDDEN
| (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1));
3789 if (isym
->st_other
!= 0 && !dynamic
)
3791 unsigned char hvis
, symvis
, other
, nvis
;
3793 /* Take the balance of OTHER from the definition. */
3794 other
= (definition
? isym
->st_other
: h
->other
);
3795 other
&= ~ ELF_ST_VISIBILITY (-1);
3797 /* Combine visibilities, using the most constraining one. */
3798 hvis
= ELF_ST_VISIBILITY (h
->other
);
3799 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3805 nvis
= hvis
< symvis
? hvis
: symvis
;
3807 h
->other
= other
| nvis
;
3810 /* Set a flag in the hash table entry indicating the type of
3811 reference or definition we just found. Keep a count of
3812 the number of dynamic symbols we find. A dynamic symbol
3813 is one which is referenced or defined by both a regular
3814 object and a shared object. */
3821 if (bind
!= STB_WEAK
)
3822 h
->ref_regular_nonweak
= 1;
3826 if (! info
->executable
3839 || (h
->u
.weakdef
!= NULL
3841 && h
->u
.weakdef
->dynindx
!= -1))
3845 /* Check to see if we need to add an indirect symbol for
3846 the default name. */
3847 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
3848 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
3849 &sec
, &value
, &dynsym
,
3851 goto error_free_vers
;
3853 if (definition
&& !dynamic
)
3855 char *p
= strchr (name
, ELF_VER_CHR
);
3856 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
3858 /* Queue non-default versions so that .symver x, x@FOO
3859 aliases can be checked. */
3860 if (! nondeflt_vers
)
3862 amt
= (isymend
- isym
+ 1)
3863 * sizeof (struct elf_link_hash_entry
*);
3864 nondeflt_vers
= bfd_malloc (amt
);
3866 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
3870 if (dynsym
&& h
->dynindx
== -1)
3872 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3873 goto error_free_vers
;
3874 if (h
->u
.weakdef
!= NULL
3876 && h
->u
.weakdef
->dynindx
== -1)
3878 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
3879 goto error_free_vers
;
3882 else if (dynsym
&& h
->dynindx
!= -1)
3883 /* If the symbol already has a dynamic index, but
3884 visibility says it should not be visible, turn it into
3886 switch (ELF_ST_VISIBILITY (h
->other
))
3890 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
3901 const char *soname
= elf_dt_name (abfd
);
3903 /* A symbol from a library loaded via DT_NEEDED of some
3904 other library is referenced by a regular object.
3905 Add a DT_NEEDED entry for it. Issue an error if
3906 --no-add-needed is used. */
3907 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
3909 (*_bfd_error_handler
)
3910 (_("%s: invalid DSO for symbol `%s' definition"),
3912 bfd_set_error (bfd_error_bad_value
);
3913 goto error_free_vers
;
3917 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3919 goto error_free_vers
;
3921 BFD_ASSERT (ret
== 0);
3926 /* Now that all the symbols from this input file are created, handle
3927 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3928 if (nondeflt_vers
!= NULL
)
3930 bfd_size_type cnt
, symidx
;
3932 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
3934 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
3935 char *shortname
, *p
;
3937 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3939 || (h
->root
.type
!= bfd_link_hash_defined
3940 && h
->root
.type
!= bfd_link_hash_defweak
))
3943 amt
= p
- h
->root
.root
.string
;
3944 shortname
= bfd_malloc (amt
+ 1);
3945 memcpy (shortname
, h
->root
.root
.string
, amt
);
3946 shortname
[amt
] = '\0';
3948 hi
= (struct elf_link_hash_entry
*)
3949 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
3950 FALSE
, FALSE
, FALSE
);
3952 && hi
->root
.type
== h
->root
.type
3953 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
3954 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
3956 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
3957 hi
->root
.type
= bfd_link_hash_indirect
;
3958 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
3959 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
3960 sym_hash
= elf_sym_hashes (abfd
);
3962 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
3963 if (sym_hash
[symidx
] == hi
)
3965 sym_hash
[symidx
] = h
;
3971 free (nondeflt_vers
);
3972 nondeflt_vers
= NULL
;
3975 if (extversym
!= NULL
)
3981 if (isymbuf
!= NULL
)
3985 /* Now set the weakdefs field correctly for all the weak defined
3986 symbols we found. The only way to do this is to search all the
3987 symbols. Since we only need the information for non functions in
3988 dynamic objects, that's the only time we actually put anything on
3989 the list WEAKS. We need this information so that if a regular
3990 object refers to a symbol defined weakly in a dynamic object, the
3991 real symbol in the dynamic object is also put in the dynamic
3992 symbols; we also must arrange for both symbols to point to the
3993 same memory location. We could handle the general case of symbol
3994 aliasing, but a general symbol alias can only be generated in
3995 assembler code, handling it correctly would be very time
3996 consuming, and other ELF linkers don't handle general aliasing
4000 struct elf_link_hash_entry
**hpp
;
4001 struct elf_link_hash_entry
**hppend
;
4002 struct elf_link_hash_entry
**sorted_sym_hash
;
4003 struct elf_link_hash_entry
*h
;
4006 /* Since we have to search the whole symbol list for each weak
4007 defined symbol, search time for N weak defined symbols will be
4008 O(N^2). Binary search will cut it down to O(NlogN). */
4009 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4010 sorted_sym_hash
= bfd_malloc (amt
);
4011 if (sorted_sym_hash
== NULL
)
4013 sym_hash
= sorted_sym_hash
;
4014 hpp
= elf_sym_hashes (abfd
);
4015 hppend
= hpp
+ extsymcount
;
4017 for (; hpp
< hppend
; hpp
++)
4021 && h
->root
.type
== bfd_link_hash_defined
4022 && h
->type
!= STT_FUNC
)
4030 qsort (sorted_sym_hash
, sym_count
,
4031 sizeof (struct elf_link_hash_entry
*),
4034 while (weaks
!= NULL
)
4036 struct elf_link_hash_entry
*hlook
;
4043 weaks
= hlook
->u
.weakdef
;
4044 hlook
->u
.weakdef
= NULL
;
4046 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4047 || hlook
->root
.type
== bfd_link_hash_defweak
4048 || hlook
->root
.type
== bfd_link_hash_common
4049 || hlook
->root
.type
== bfd_link_hash_indirect
);
4050 slook
= hlook
->root
.u
.def
.section
;
4051 vlook
= hlook
->root
.u
.def
.value
;
4058 bfd_signed_vma vdiff
;
4060 h
= sorted_sym_hash
[idx
];
4061 vdiff
= vlook
- h
->root
.u
.def
.value
;
4068 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4081 /* We didn't find a value/section match. */
4085 for (i
= ilook
; i
< sym_count
; i
++)
4087 h
= sorted_sym_hash
[i
];
4089 /* Stop if value or section doesn't match. */
4090 if (h
->root
.u
.def
.value
!= vlook
4091 || h
->root
.u
.def
.section
!= slook
)
4093 else if (h
!= hlook
)
4095 hlook
->u
.weakdef
= h
;
4097 /* If the weak definition is in the list of dynamic
4098 symbols, make sure the real definition is put
4100 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4102 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4106 /* If the real definition is in the list of dynamic
4107 symbols, make sure the weak definition is put
4108 there as well. If we don't do this, then the
4109 dynamic loader might not merge the entries for the
4110 real definition and the weak definition. */
4111 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4113 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4121 free (sorted_sym_hash
);
4124 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4125 if (check_directives
)
4126 check_directives (abfd
, info
);
4128 /* If this object is the same format as the output object, and it is
4129 not a shared library, then let the backend look through the
4132 This is required to build global offset table entries and to
4133 arrange for dynamic relocs. It is not required for the
4134 particular common case of linking non PIC code, even when linking
4135 against shared libraries, but unfortunately there is no way of
4136 knowing whether an object file has been compiled PIC or not.
4137 Looking through the relocs is not particularly time consuming.
4138 The problem is that we must either (1) keep the relocs in memory,
4139 which causes the linker to require additional runtime memory or
4140 (2) read the relocs twice from the input file, which wastes time.
4141 This would be a good case for using mmap.
4143 I have no idea how to handle linking PIC code into a file of a
4144 different format. It probably can't be done. */
4145 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4147 && is_elf_hash_table (hash_table
)
4148 && hash_table
->root
.creator
== abfd
->xvec
4149 && check_relocs
!= NULL
)
4153 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4155 Elf_Internal_Rela
*internal_relocs
;
4158 if ((o
->flags
& SEC_RELOC
) == 0
4159 || o
->reloc_count
== 0
4160 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4161 && (o
->flags
& SEC_DEBUGGING
) != 0)
4162 || bfd_is_abs_section (o
->output_section
))
4165 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4167 if (internal_relocs
== NULL
)
4170 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4172 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4173 free (internal_relocs
);
4180 /* If this is a non-traditional link, try to optimize the handling
4181 of the .stab/.stabstr sections. */
4183 && ! info
->traditional_format
4184 && is_elf_hash_table (hash_table
)
4185 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4189 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4190 if (stabstr
!= NULL
)
4192 bfd_size_type string_offset
= 0;
4195 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4196 if (strncmp (".stab", stab
->name
, 5) == 0
4197 && (!stab
->name
[5] ||
4198 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4199 && (stab
->flags
& SEC_MERGE
) == 0
4200 && !bfd_is_abs_section (stab
->output_section
))
4202 struct bfd_elf_section_data
*secdata
;
4204 secdata
= elf_section_data (stab
);
4205 if (! _bfd_link_section_stabs (abfd
,
4206 &hash_table
->stab_info
,
4211 if (secdata
->sec_info
)
4212 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4217 if (is_elf_hash_table (hash_table
))
4219 /* Add this bfd to the loaded list. */
4220 struct elf_link_loaded_list
*n
;
4222 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4226 n
->next
= hash_table
->loaded
;
4227 hash_table
->loaded
= n
;
4233 if (nondeflt_vers
!= NULL
)
4234 free (nondeflt_vers
);
4235 if (extversym
!= NULL
)
4238 if (isymbuf
!= NULL
)
4244 /* Return the linker hash table entry of a symbol that might be
4245 satisfied by an archive symbol. Return -1 on error. */
4247 struct elf_link_hash_entry
*
4248 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4249 struct bfd_link_info
*info
,
4252 struct elf_link_hash_entry
*h
;
4256 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4260 /* If this is a default version (the name contains @@), look up the
4261 symbol again with only one `@' as well as without the version.
4262 The effect is that references to the symbol with and without the
4263 version will be matched by the default symbol in the archive. */
4265 p
= strchr (name
, ELF_VER_CHR
);
4266 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4269 /* First check with only one `@'. */
4270 len
= strlen (name
);
4271 copy
= bfd_alloc (abfd
, len
);
4273 return (struct elf_link_hash_entry
*) 0 - 1;
4275 first
= p
- name
+ 1;
4276 memcpy (copy
, name
, first
);
4277 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4279 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4282 /* We also need to check references to the symbol without the
4284 copy
[first
- 1] = '\0';
4285 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4286 FALSE
, FALSE
, FALSE
);
4289 bfd_release (abfd
, copy
);
4293 /* Add symbols from an ELF archive file to the linker hash table. We
4294 don't use _bfd_generic_link_add_archive_symbols because of a
4295 problem which arises on UnixWare. The UnixWare libc.so is an
4296 archive which includes an entry libc.so.1 which defines a bunch of
4297 symbols. The libc.so archive also includes a number of other
4298 object files, which also define symbols, some of which are the same
4299 as those defined in libc.so.1. Correct linking requires that we
4300 consider each object file in turn, and include it if it defines any
4301 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4302 this; it looks through the list of undefined symbols, and includes
4303 any object file which defines them. When this algorithm is used on
4304 UnixWare, it winds up pulling in libc.so.1 early and defining a
4305 bunch of symbols. This means that some of the other objects in the
4306 archive are not included in the link, which is incorrect since they
4307 precede libc.so.1 in the archive.
4309 Fortunately, ELF archive handling is simpler than that done by
4310 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4311 oddities. In ELF, if we find a symbol in the archive map, and the
4312 symbol is currently undefined, we know that we must pull in that
4315 Unfortunately, we do have to make multiple passes over the symbol
4316 table until nothing further is resolved. */
4319 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4322 bfd_boolean
*defined
= NULL
;
4323 bfd_boolean
*included
= NULL
;
4327 const struct elf_backend_data
*bed
;
4328 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4329 (bfd
*, struct bfd_link_info
*, const char *);
4331 if (! bfd_has_map (abfd
))
4333 /* An empty archive is a special case. */
4334 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4336 bfd_set_error (bfd_error_no_armap
);
4340 /* Keep track of all symbols we know to be already defined, and all
4341 files we know to be already included. This is to speed up the
4342 second and subsequent passes. */
4343 c
= bfd_ardata (abfd
)->symdef_count
;
4347 amt
*= sizeof (bfd_boolean
);
4348 defined
= bfd_zmalloc (amt
);
4349 included
= bfd_zmalloc (amt
);
4350 if (defined
== NULL
|| included
== NULL
)
4353 symdefs
= bfd_ardata (abfd
)->symdefs
;
4354 bed
= get_elf_backend_data (abfd
);
4355 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4368 symdefend
= symdef
+ c
;
4369 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4371 struct elf_link_hash_entry
*h
;
4373 struct bfd_link_hash_entry
*undefs_tail
;
4376 if (defined
[i
] || included
[i
])
4378 if (symdef
->file_offset
== last
)
4384 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4385 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4391 if (h
->root
.type
== bfd_link_hash_common
)
4393 /* We currently have a common symbol. The archive map contains
4394 a reference to this symbol, so we may want to include it. We
4395 only want to include it however, if this archive element
4396 contains a definition of the symbol, not just another common
4399 Unfortunately some archivers (including GNU ar) will put
4400 declarations of common symbols into their archive maps, as
4401 well as real definitions, so we cannot just go by the archive
4402 map alone. Instead we must read in the element's symbol
4403 table and check that to see what kind of symbol definition
4405 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4408 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4410 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4415 /* We need to include this archive member. */
4416 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4417 if (element
== NULL
)
4420 if (! bfd_check_format (element
, bfd_object
))
4423 /* Doublecheck that we have not included this object
4424 already--it should be impossible, but there may be
4425 something wrong with the archive. */
4426 if (element
->archive_pass
!= 0)
4428 bfd_set_error (bfd_error_bad_value
);
4431 element
->archive_pass
= 1;
4433 undefs_tail
= info
->hash
->undefs_tail
;
4435 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4438 if (! bfd_link_add_symbols (element
, info
))
4441 /* If there are any new undefined symbols, we need to make
4442 another pass through the archive in order to see whether
4443 they can be defined. FIXME: This isn't perfect, because
4444 common symbols wind up on undefs_tail and because an
4445 undefined symbol which is defined later on in this pass
4446 does not require another pass. This isn't a bug, but it
4447 does make the code less efficient than it could be. */
4448 if (undefs_tail
!= info
->hash
->undefs_tail
)
4451 /* Look backward to mark all symbols from this object file
4452 which we have already seen in this pass. */
4456 included
[mark
] = TRUE
;
4461 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4463 /* We mark subsequent symbols from this object file as we go
4464 on through the loop. */
4465 last
= symdef
->file_offset
;
4476 if (defined
!= NULL
)
4478 if (included
!= NULL
)
4483 /* Given an ELF BFD, add symbols to the global hash table as
4487 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4489 switch (bfd_get_format (abfd
))
4492 return elf_link_add_object_symbols (abfd
, info
);
4494 return elf_link_add_archive_symbols (abfd
, info
);
4496 bfd_set_error (bfd_error_wrong_format
);
4501 /* This function will be called though elf_link_hash_traverse to store
4502 all hash value of the exported symbols in an array. */
4505 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4507 unsigned long **valuep
= data
;
4513 if (h
->root
.type
== bfd_link_hash_warning
)
4514 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4516 /* Ignore indirect symbols. These are added by the versioning code. */
4517 if (h
->dynindx
== -1)
4520 name
= h
->root
.root
.string
;
4521 p
= strchr (name
, ELF_VER_CHR
);
4524 alc
= bfd_malloc (p
- name
+ 1);
4525 memcpy (alc
, name
, p
- name
);
4526 alc
[p
- name
] = '\0';
4530 /* Compute the hash value. */
4531 ha
= bfd_elf_hash (name
);
4533 /* Store the found hash value in the array given as the argument. */
4536 /* And store it in the struct so that we can put it in the hash table
4538 h
->u
.elf_hash_value
= ha
;
4546 /* Array used to determine the number of hash table buckets to use
4547 based on the number of symbols there are. If there are fewer than
4548 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4549 fewer than 37 we use 17 buckets, and so forth. We never use more
4550 than 32771 buckets. */
4552 static const size_t elf_buckets
[] =
4554 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4558 /* Compute bucket count for hashing table. We do not use a static set
4559 of possible tables sizes anymore. Instead we determine for all
4560 possible reasonable sizes of the table the outcome (i.e., the
4561 number of collisions etc) and choose the best solution. The
4562 weighting functions are not too simple to allow the table to grow
4563 without bounds. Instead one of the weighting factors is the size.
4564 Therefore the result is always a good payoff between few collisions
4565 (= short chain lengths) and table size. */
4567 compute_bucket_count (struct bfd_link_info
*info
)
4569 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4570 size_t best_size
= 0;
4571 unsigned long int *hashcodes
;
4572 unsigned long int *hashcodesp
;
4573 unsigned long int i
;
4576 /* Compute the hash values for all exported symbols. At the same
4577 time store the values in an array so that we could use them for
4580 amt
*= sizeof (unsigned long int);
4581 hashcodes
= bfd_malloc (amt
);
4582 if (hashcodes
== NULL
)
4584 hashcodesp
= hashcodes
;
4586 /* Put all hash values in HASHCODES. */
4587 elf_link_hash_traverse (elf_hash_table (info
),
4588 elf_collect_hash_codes
, &hashcodesp
);
4590 /* We have a problem here. The following code to optimize the table
4591 size requires an integer type with more the 32 bits. If
4592 BFD_HOST_U_64_BIT is set we know about such a type. */
4593 #ifdef BFD_HOST_U_64_BIT
4596 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4599 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4600 unsigned long int *counts
;
4601 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4602 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4604 /* Possible optimization parameters: if we have NSYMS symbols we say
4605 that the hashing table must at least have NSYMS/4 and at most
4607 minsize
= nsyms
/ 4;
4610 best_size
= maxsize
= nsyms
* 2;
4612 /* Create array where we count the collisions in. We must use bfd_malloc
4613 since the size could be large. */
4615 amt
*= sizeof (unsigned long int);
4616 counts
= bfd_malloc (amt
);
4623 /* Compute the "optimal" size for the hash table. The criteria is a
4624 minimal chain length. The minor criteria is (of course) the size
4626 for (i
= minsize
; i
< maxsize
; ++i
)
4628 /* Walk through the array of hashcodes and count the collisions. */
4629 BFD_HOST_U_64_BIT max
;
4630 unsigned long int j
;
4631 unsigned long int fact
;
4633 memset (counts
, '\0', i
* sizeof (unsigned long int));
4635 /* Determine how often each hash bucket is used. */
4636 for (j
= 0; j
< nsyms
; ++j
)
4637 ++counts
[hashcodes
[j
] % i
];
4639 /* For the weight function we need some information about the
4640 pagesize on the target. This is information need not be 100%
4641 accurate. Since this information is not available (so far) we
4642 define it here to a reasonable default value. If it is crucial
4643 to have a better value some day simply define this value. */
4644 # ifndef BFD_TARGET_PAGESIZE
4645 # define BFD_TARGET_PAGESIZE (4096)
4648 /* We in any case need 2 + NSYMS entries for the size values and
4650 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4653 /* Variant 1: optimize for short chains. We add the squares
4654 of all the chain lengths (which favors many small chain
4655 over a few long chains). */
4656 for (j
= 0; j
< i
; ++j
)
4657 max
+= counts
[j
] * counts
[j
];
4659 /* This adds penalties for the overall size of the table. */
4660 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4663 /* Variant 2: Optimize a lot more for small table. Here we
4664 also add squares of the size but we also add penalties for
4665 empty slots (the +1 term). */
4666 for (j
= 0; j
< i
; ++j
)
4667 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4669 /* The overall size of the table is considered, but not as
4670 strong as in variant 1, where it is squared. */
4671 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4675 /* Compare with current best results. */
4676 if (max
< best_chlen
)
4686 #endif /* defined (BFD_HOST_U_64_BIT) */
4688 /* This is the fallback solution if no 64bit type is available or if we
4689 are not supposed to spend much time on optimizations. We select the
4690 bucket count using a fixed set of numbers. */
4691 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4693 best_size
= elf_buckets
[i
];
4694 if (dynsymcount
< elf_buckets
[i
+ 1])
4699 /* Free the arrays we needed. */
4705 /* Set up the sizes and contents of the ELF dynamic sections. This is
4706 called by the ELF linker emulation before_allocation routine. We
4707 must set the sizes of the sections before the linker sets the
4708 addresses of the various sections. */
4711 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4714 const char *filter_shlib
,
4715 const char * const *auxiliary_filters
,
4716 struct bfd_link_info
*info
,
4717 asection
**sinterpptr
,
4718 struct bfd_elf_version_tree
*verdefs
)
4720 bfd_size_type soname_indx
;
4722 const struct elf_backend_data
*bed
;
4723 struct elf_assign_sym_version_info asvinfo
;
4727 soname_indx
= (bfd_size_type
) -1;
4729 if (!is_elf_hash_table (info
->hash
))
4732 elf_tdata (output_bfd
)->relro
= info
->relro
;
4733 if (info
->execstack
)
4734 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4735 else if (info
->noexecstack
)
4736 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4740 asection
*notesec
= NULL
;
4743 for (inputobj
= info
->input_bfds
;
4745 inputobj
= inputobj
->link_next
)
4749 if (inputobj
->flags
& DYNAMIC
)
4751 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4754 if (s
->flags
& SEC_CODE
)
4763 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4764 if (exec
&& info
->relocatable
4765 && notesec
->output_section
!= bfd_abs_section_ptr
)
4766 notesec
->output_section
->flags
|= SEC_CODE
;
4770 /* Any syms created from now on start with -1 in
4771 got.refcount/offset and plt.refcount/offset. */
4772 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4774 /* The backend may have to create some sections regardless of whether
4775 we're dynamic or not. */
4776 bed
= get_elf_backend_data (output_bfd
);
4777 if (bed
->elf_backend_always_size_sections
4778 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4781 dynobj
= elf_hash_table (info
)->dynobj
;
4783 /* If there were no dynamic objects in the link, there is nothing to
4788 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
4791 if (elf_hash_table (info
)->dynamic_sections_created
)
4793 struct elf_info_failed eif
;
4794 struct elf_link_hash_entry
*h
;
4796 struct bfd_elf_version_tree
*t
;
4797 struct bfd_elf_version_expr
*d
;
4798 bfd_boolean all_defined
;
4800 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
4801 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
4805 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4807 if (soname_indx
== (bfd_size_type
) -1
4808 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
4814 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
4816 info
->flags
|= DF_SYMBOLIC
;
4823 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
4825 if (indx
== (bfd_size_type
) -1
4826 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
4829 if (info
->new_dtags
)
4831 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
4832 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
4837 if (filter_shlib
!= NULL
)
4841 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4842 filter_shlib
, TRUE
);
4843 if (indx
== (bfd_size_type
) -1
4844 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
4848 if (auxiliary_filters
!= NULL
)
4850 const char * const *p
;
4852 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
4856 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4858 if (indx
== (bfd_size_type
) -1
4859 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
4865 eif
.verdefs
= verdefs
;
4868 /* If we are supposed to export all symbols into the dynamic symbol
4869 table (this is not the normal case), then do so. */
4870 if (info
->export_dynamic
)
4872 elf_link_hash_traverse (elf_hash_table (info
),
4873 _bfd_elf_export_symbol
,
4879 /* Make all global versions with definition. */
4880 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4881 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4882 if (!d
->symver
&& d
->symbol
)
4884 const char *verstr
, *name
;
4885 size_t namelen
, verlen
, newlen
;
4887 struct elf_link_hash_entry
*newh
;
4890 namelen
= strlen (name
);
4892 verlen
= strlen (verstr
);
4893 newlen
= namelen
+ verlen
+ 3;
4895 newname
= bfd_malloc (newlen
);
4896 if (newname
== NULL
)
4898 memcpy (newname
, name
, namelen
);
4900 /* Check the hidden versioned definition. */
4901 p
= newname
+ namelen
;
4903 memcpy (p
, verstr
, verlen
+ 1);
4904 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4905 newname
, FALSE
, FALSE
,
4908 || (newh
->root
.type
!= bfd_link_hash_defined
4909 && newh
->root
.type
!= bfd_link_hash_defweak
))
4911 /* Check the default versioned definition. */
4913 memcpy (p
, verstr
, verlen
+ 1);
4914 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4915 newname
, FALSE
, FALSE
,
4920 /* Mark this version if there is a definition and it is
4921 not defined in a shared object. */
4923 && !newh
->def_dynamic
4924 && (newh
->root
.type
== bfd_link_hash_defined
4925 || newh
->root
.type
== bfd_link_hash_defweak
))
4929 /* Attach all the symbols to their version information. */
4930 asvinfo
.output_bfd
= output_bfd
;
4931 asvinfo
.info
= info
;
4932 asvinfo
.verdefs
= verdefs
;
4933 asvinfo
.failed
= FALSE
;
4935 elf_link_hash_traverse (elf_hash_table (info
),
4936 _bfd_elf_link_assign_sym_version
,
4941 if (!info
->allow_undefined_version
)
4943 /* Check if all global versions have a definition. */
4945 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4946 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4947 if (!d
->symver
&& !d
->script
)
4949 (*_bfd_error_handler
)
4950 (_("%s: undefined version: %s"),
4951 d
->pattern
, t
->name
);
4952 all_defined
= FALSE
;
4957 bfd_set_error (bfd_error_bad_value
);
4962 /* Find all symbols which were defined in a dynamic object and make
4963 the backend pick a reasonable value for them. */
4964 elf_link_hash_traverse (elf_hash_table (info
),
4965 _bfd_elf_adjust_dynamic_symbol
,
4970 /* Add some entries to the .dynamic section. We fill in some of the
4971 values later, in bfd_elf_final_link, but we must add the entries
4972 now so that we know the final size of the .dynamic section. */
4974 /* If there are initialization and/or finalization functions to
4975 call then add the corresponding DT_INIT/DT_FINI entries. */
4976 h
= (info
->init_function
4977 ? elf_link_hash_lookup (elf_hash_table (info
),
4978 info
->init_function
, FALSE
,
4985 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
4988 h
= (info
->fini_function
4989 ? elf_link_hash_lookup (elf_hash_table (info
),
4990 info
->fini_function
, FALSE
,
4997 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5001 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
5003 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5004 if (! info
->executable
)
5009 for (sub
= info
->input_bfds
; sub
!= NULL
;
5010 sub
= sub
->link_next
)
5011 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5012 if (elf_section_data (o
)->this_hdr
.sh_type
5013 == SHT_PREINIT_ARRAY
)
5015 (*_bfd_error_handler
)
5016 (_("%B: .preinit_array section is not allowed in DSO"),
5021 bfd_set_error (bfd_error_nonrepresentable_section
);
5025 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5026 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5029 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
5031 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5032 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5035 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
5037 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5038 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5042 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5043 /* If .dynstr is excluded from the link, we don't want any of
5044 these tags. Strictly, we should be checking each section
5045 individually; This quick check covers for the case where
5046 someone does a /DISCARD/ : { *(*) }. */
5047 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5049 bfd_size_type strsize
;
5051 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5052 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
5053 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5054 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5055 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5056 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5057 bed
->s
->sizeof_sym
))
5062 /* The backend must work out the sizes of all the other dynamic
5064 if (bed
->elf_backend_size_dynamic_sections
5065 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5068 if (elf_hash_table (info
)->dynamic_sections_created
)
5070 bfd_size_type dynsymcount
;
5072 size_t bucketcount
= 0;
5073 size_t hash_entry_size
;
5074 unsigned int dtagcount
;
5076 /* Set up the version definition section. */
5077 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5078 BFD_ASSERT (s
!= NULL
);
5080 /* We may have created additional version definitions if we are
5081 just linking a regular application. */
5082 verdefs
= asvinfo
.verdefs
;
5084 /* Skip anonymous version tag. */
5085 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5086 verdefs
= verdefs
->next
;
5088 if (verdefs
== NULL
)
5089 _bfd_strip_section_from_output (info
, s
);
5094 struct bfd_elf_version_tree
*t
;
5096 Elf_Internal_Verdef def
;
5097 Elf_Internal_Verdaux defaux
;
5102 /* Make space for the base version. */
5103 size
+= sizeof (Elf_External_Verdef
);
5104 size
+= sizeof (Elf_External_Verdaux
);
5107 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5109 struct bfd_elf_version_deps
*n
;
5111 size
+= sizeof (Elf_External_Verdef
);
5112 size
+= sizeof (Elf_External_Verdaux
);
5115 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5116 size
+= sizeof (Elf_External_Verdaux
);
5120 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5121 if (s
->contents
== NULL
&& s
->size
!= 0)
5124 /* Fill in the version definition section. */
5128 def
.vd_version
= VER_DEF_CURRENT
;
5129 def
.vd_flags
= VER_FLG_BASE
;
5132 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5133 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5134 + sizeof (Elf_External_Verdaux
));
5136 if (soname_indx
!= (bfd_size_type
) -1)
5138 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5140 def
.vd_hash
= bfd_elf_hash (soname
);
5141 defaux
.vda_name
= soname_indx
;
5148 name
= basename (output_bfd
->filename
);
5149 def
.vd_hash
= bfd_elf_hash (name
);
5150 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5152 if (indx
== (bfd_size_type
) -1)
5154 defaux
.vda_name
= indx
;
5156 defaux
.vda_next
= 0;
5158 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5159 (Elf_External_Verdef
*) p
);
5160 p
+= sizeof (Elf_External_Verdef
);
5161 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5162 (Elf_External_Verdaux
*) p
);
5163 p
+= sizeof (Elf_External_Verdaux
);
5165 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5168 struct bfd_elf_version_deps
*n
;
5169 struct elf_link_hash_entry
*h
;
5170 struct bfd_link_hash_entry
*bh
;
5173 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5176 /* Add a symbol representing this version. */
5178 if (! (_bfd_generic_link_add_one_symbol
5179 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5181 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5183 h
= (struct elf_link_hash_entry
*) bh
;
5186 h
->type
= STT_OBJECT
;
5187 h
->verinfo
.vertree
= t
;
5189 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5192 def
.vd_version
= VER_DEF_CURRENT
;
5194 if (t
->globals
.list
== NULL
5195 && t
->locals
.list
== NULL
5197 def
.vd_flags
|= VER_FLG_WEAK
;
5198 def
.vd_ndx
= t
->vernum
+ 1;
5199 def
.vd_cnt
= cdeps
+ 1;
5200 def
.vd_hash
= bfd_elf_hash (t
->name
);
5201 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5203 if (t
->next
!= NULL
)
5204 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5205 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5207 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5208 (Elf_External_Verdef
*) p
);
5209 p
+= sizeof (Elf_External_Verdef
);
5211 defaux
.vda_name
= h
->dynstr_index
;
5212 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5214 defaux
.vda_next
= 0;
5215 if (t
->deps
!= NULL
)
5216 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5217 t
->name_indx
= defaux
.vda_name
;
5219 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5220 (Elf_External_Verdaux
*) p
);
5221 p
+= sizeof (Elf_External_Verdaux
);
5223 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5225 if (n
->version_needed
== NULL
)
5227 /* This can happen if there was an error in the
5229 defaux
.vda_name
= 0;
5233 defaux
.vda_name
= n
->version_needed
->name_indx
;
5234 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5237 if (n
->next
== NULL
)
5238 defaux
.vda_next
= 0;
5240 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5242 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5243 (Elf_External_Verdaux
*) p
);
5244 p
+= sizeof (Elf_External_Verdaux
);
5248 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5249 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5252 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5255 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5257 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5260 else if (info
->flags
& DF_BIND_NOW
)
5262 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5268 if (info
->executable
)
5269 info
->flags_1
&= ~ (DF_1_INITFIRST
5272 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5276 /* Work out the size of the version reference section. */
5278 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5279 BFD_ASSERT (s
!= NULL
);
5281 struct elf_find_verdep_info sinfo
;
5283 sinfo
.output_bfd
= output_bfd
;
5285 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5286 if (sinfo
.vers
== 0)
5288 sinfo
.failed
= FALSE
;
5290 elf_link_hash_traverse (elf_hash_table (info
),
5291 _bfd_elf_link_find_version_dependencies
,
5294 if (elf_tdata (output_bfd
)->verref
== NULL
)
5295 _bfd_strip_section_from_output (info
, s
);
5298 Elf_Internal_Verneed
*t
;
5303 /* Build the version definition section. */
5306 for (t
= elf_tdata (output_bfd
)->verref
;
5310 Elf_Internal_Vernaux
*a
;
5312 size
+= sizeof (Elf_External_Verneed
);
5314 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5315 size
+= sizeof (Elf_External_Vernaux
);
5319 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5320 if (s
->contents
== NULL
)
5324 for (t
= elf_tdata (output_bfd
)->verref
;
5329 Elf_Internal_Vernaux
*a
;
5333 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5336 t
->vn_version
= VER_NEED_CURRENT
;
5338 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5339 elf_dt_name (t
->vn_bfd
) != NULL
5340 ? elf_dt_name (t
->vn_bfd
)
5341 : basename (t
->vn_bfd
->filename
),
5343 if (indx
== (bfd_size_type
) -1)
5346 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5347 if (t
->vn_nextref
== NULL
)
5350 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5351 + caux
* sizeof (Elf_External_Vernaux
));
5353 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5354 (Elf_External_Verneed
*) p
);
5355 p
+= sizeof (Elf_External_Verneed
);
5357 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5359 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5360 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5361 a
->vna_nodename
, FALSE
);
5362 if (indx
== (bfd_size_type
) -1)
5365 if (a
->vna_nextptr
== NULL
)
5368 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5370 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5371 (Elf_External_Vernaux
*) p
);
5372 p
+= sizeof (Elf_External_Vernaux
);
5376 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5377 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5380 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5384 /* Assign dynsym indicies. In a shared library we generate a
5385 section symbol for each output section, which come first.
5386 Next come all of the back-end allocated local dynamic syms,
5387 followed by the rest of the global symbols. */
5389 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5391 /* Work out the size of the symbol version section. */
5392 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5393 BFD_ASSERT (s
!= NULL
);
5394 if (dynsymcount
== 0
5395 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
))
5397 _bfd_strip_section_from_output (info
, s
);
5398 /* The DYNSYMCOUNT might have changed if we were going to
5399 output a dynamic symbol table entry for S. */
5400 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5404 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5405 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5406 if (s
->contents
== NULL
)
5409 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5413 /* Set the size of the .dynsym and .hash sections. We counted
5414 the number of dynamic symbols in elf_link_add_object_symbols.
5415 We will build the contents of .dynsym and .hash when we build
5416 the final symbol table, because until then we do not know the
5417 correct value to give the symbols. We built the .dynstr
5418 section as we went along in elf_link_add_object_symbols. */
5419 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5420 BFD_ASSERT (s
!= NULL
);
5421 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5422 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5423 if (s
->contents
== NULL
&& s
->size
!= 0)
5426 if (dynsymcount
!= 0)
5428 Elf_Internal_Sym isym
;
5430 /* The first entry in .dynsym is a dummy symbol. */
5437 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
5440 /* Compute the size of the hashing table. As a side effect this
5441 computes the hash values for all the names we export. */
5442 bucketcount
= compute_bucket_count (info
);
5444 s
= bfd_get_section_by_name (dynobj
, ".hash");
5445 BFD_ASSERT (s
!= NULL
);
5446 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5447 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5448 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5449 if (s
->contents
== NULL
)
5452 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5453 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5454 s
->contents
+ hash_entry_size
);
5456 elf_hash_table (info
)->bucketcount
= bucketcount
;
5458 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5459 BFD_ASSERT (s
!= NULL
);
5461 elf_finalize_dynstr (output_bfd
, info
);
5463 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5465 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5466 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5473 /* Final phase of ELF linker. */
5475 /* A structure we use to avoid passing large numbers of arguments. */
5477 struct elf_final_link_info
5479 /* General link information. */
5480 struct bfd_link_info
*info
;
5483 /* Symbol string table. */
5484 struct bfd_strtab_hash
*symstrtab
;
5485 /* .dynsym section. */
5486 asection
*dynsym_sec
;
5487 /* .hash section. */
5489 /* symbol version section (.gnu.version). */
5490 asection
*symver_sec
;
5491 /* Buffer large enough to hold contents of any section. */
5493 /* Buffer large enough to hold external relocs of any section. */
5494 void *external_relocs
;
5495 /* Buffer large enough to hold internal relocs of any section. */
5496 Elf_Internal_Rela
*internal_relocs
;
5497 /* Buffer large enough to hold external local symbols of any input
5499 bfd_byte
*external_syms
;
5500 /* And a buffer for symbol section indices. */
5501 Elf_External_Sym_Shndx
*locsym_shndx
;
5502 /* Buffer large enough to hold internal local symbols of any input
5504 Elf_Internal_Sym
*internal_syms
;
5505 /* Array large enough to hold a symbol index for each local symbol
5506 of any input BFD. */
5508 /* Array large enough to hold a section pointer for each local
5509 symbol of any input BFD. */
5510 asection
**sections
;
5511 /* Buffer to hold swapped out symbols. */
5513 /* And one for symbol section indices. */
5514 Elf_External_Sym_Shndx
*symshndxbuf
;
5515 /* Number of swapped out symbols in buffer. */
5516 size_t symbuf_count
;
5517 /* Number of symbols which fit in symbuf. */
5519 /* And same for symshndxbuf. */
5520 size_t shndxbuf_size
;
5523 /* This struct is used to pass information to elf_link_output_extsym. */
5525 struct elf_outext_info
5528 bfd_boolean localsyms
;
5529 struct elf_final_link_info
*finfo
;
5532 /* When performing a relocatable link, the input relocations are
5533 preserved. But, if they reference global symbols, the indices
5534 referenced must be updated. Update all the relocations in
5535 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5538 elf_link_adjust_relocs (bfd
*abfd
,
5539 Elf_Internal_Shdr
*rel_hdr
,
5541 struct elf_link_hash_entry
**rel_hash
)
5544 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5546 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5547 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5548 bfd_vma r_type_mask
;
5551 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5553 swap_in
= bed
->s
->swap_reloc_in
;
5554 swap_out
= bed
->s
->swap_reloc_out
;
5556 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5558 swap_in
= bed
->s
->swap_reloca_in
;
5559 swap_out
= bed
->s
->swap_reloca_out
;
5564 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5567 if (bed
->s
->arch_size
== 32)
5574 r_type_mask
= 0xffffffff;
5578 erela
= rel_hdr
->contents
;
5579 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5581 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5584 if (*rel_hash
== NULL
)
5587 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5589 (*swap_in
) (abfd
, erela
, irela
);
5590 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5591 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5592 | (irela
[j
].r_info
& r_type_mask
));
5593 (*swap_out
) (abfd
, irela
, erela
);
5597 struct elf_link_sort_rela
5603 enum elf_reloc_type_class type
;
5604 /* We use this as an array of size int_rels_per_ext_rel. */
5605 Elf_Internal_Rela rela
[1];
5609 elf_link_sort_cmp1 (const void *A
, const void *B
)
5611 const struct elf_link_sort_rela
*a
= A
;
5612 const struct elf_link_sort_rela
*b
= B
;
5613 int relativea
, relativeb
;
5615 relativea
= a
->type
== reloc_class_relative
;
5616 relativeb
= b
->type
== reloc_class_relative
;
5618 if (relativea
< relativeb
)
5620 if (relativea
> relativeb
)
5622 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5624 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5626 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5628 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5634 elf_link_sort_cmp2 (const void *A
, const void *B
)
5636 const struct elf_link_sort_rela
*a
= A
;
5637 const struct elf_link_sort_rela
*b
= B
;
5640 if (a
->u
.offset
< b
->u
.offset
)
5642 if (a
->u
.offset
> b
->u
.offset
)
5644 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5645 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5650 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5652 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5658 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5661 bfd_size_type count
, size
;
5662 size_t i
, ret
, sort_elt
, ext_size
;
5663 bfd_byte
*sort
, *s_non_relative
, *p
;
5664 struct elf_link_sort_rela
*sq
;
5665 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5666 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5667 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5668 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5669 struct bfd_link_order
*lo
;
5672 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5673 if (reldyn
== NULL
|| reldyn
->size
== 0)
5675 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5676 if (reldyn
== NULL
|| reldyn
->size
== 0)
5678 ext_size
= bed
->s
->sizeof_rel
;
5679 swap_in
= bed
->s
->swap_reloc_in
;
5680 swap_out
= bed
->s
->swap_reloc_out
;
5684 ext_size
= bed
->s
->sizeof_rela
;
5685 swap_in
= bed
->s
->swap_reloca_in
;
5686 swap_out
= bed
->s
->swap_reloca_out
;
5688 count
= reldyn
->size
/ ext_size
;
5691 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5692 if (lo
->type
== bfd_indirect_link_order
)
5694 asection
*o
= lo
->u
.indirect
.section
;
5698 if (size
!= reldyn
->size
)
5701 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5702 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5703 sort
= bfd_zmalloc (sort_elt
* count
);
5706 (*info
->callbacks
->warning
)
5707 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5711 if (bed
->s
->arch_size
== 32)
5712 r_sym_mask
= ~(bfd_vma
) 0xff;
5714 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5716 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5717 if (lo
->type
== bfd_indirect_link_order
)
5719 bfd_byte
*erel
, *erelend
;
5720 asection
*o
= lo
->u
.indirect
.section
;
5722 if (o
->contents
== NULL
&& o
->size
!= 0)
5724 /* This is a reloc section that is being handled as a normal
5725 section. See bfd_section_from_shdr. We can't combine
5726 relocs in this case. */
5731 erelend
= o
->contents
+ o
->size
;
5732 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5733 while (erel
< erelend
)
5735 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5736 (*swap_in
) (abfd
, erel
, s
->rela
);
5737 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5738 s
->u
.sym_mask
= r_sym_mask
;
5744 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
5746 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5748 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5749 if (s
->type
!= reloc_class_relative
)
5755 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5756 for (; i
< count
; i
++, p
+= sort_elt
)
5758 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5759 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
5761 sp
->u
.offset
= sq
->rela
->r_offset
;
5764 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
5766 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5767 if (lo
->type
== bfd_indirect_link_order
)
5769 bfd_byte
*erel
, *erelend
;
5770 asection
*o
= lo
->u
.indirect
.section
;
5773 erelend
= o
->contents
+ o
->size
;
5774 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5775 while (erel
< erelend
)
5777 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5778 (*swap_out
) (abfd
, s
->rela
, erel
);
5789 /* Flush the output symbols to the file. */
5792 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
5793 const struct elf_backend_data
*bed
)
5795 if (finfo
->symbuf_count
> 0)
5797 Elf_Internal_Shdr
*hdr
;
5801 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5802 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5803 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5804 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5805 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5808 hdr
->sh_size
+= amt
;
5809 finfo
->symbuf_count
= 0;
5815 /* Add a symbol to the output symbol table. */
5818 elf_link_output_sym (struct elf_final_link_info
*finfo
,
5820 Elf_Internal_Sym
*elfsym
,
5821 asection
*input_sec
,
5822 struct elf_link_hash_entry
*h
)
5825 Elf_External_Sym_Shndx
*destshndx
;
5826 bfd_boolean (*output_symbol_hook
)
5827 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
5828 struct elf_link_hash_entry
*);
5829 const struct elf_backend_data
*bed
;
5831 bed
= get_elf_backend_data (finfo
->output_bfd
);
5832 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
5833 if (output_symbol_hook
!= NULL
)
5835 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
5839 if (name
== NULL
|| *name
== '\0')
5840 elfsym
->st_name
= 0;
5841 else if (input_sec
->flags
& SEC_EXCLUDE
)
5842 elfsym
->st_name
= 0;
5845 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5847 if (elfsym
->st_name
== (unsigned long) -1)
5851 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5853 if (! elf_link_flush_output_syms (finfo
, bed
))
5857 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5858 destshndx
= finfo
->symshndxbuf
;
5859 if (destshndx
!= NULL
)
5861 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
5865 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
5866 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
5867 if (destshndx
== NULL
)
5869 memset ((char *) destshndx
+ amt
, 0, amt
);
5870 finfo
->shndxbuf_size
*= 2;
5872 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
5875 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
5876 finfo
->symbuf_count
+= 1;
5877 bfd_get_symcount (finfo
->output_bfd
) += 1;
5882 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5883 allowing an unsatisfied unversioned symbol in the DSO to match a
5884 versioned symbol that would normally require an explicit version.
5885 We also handle the case that a DSO references a hidden symbol
5886 which may be satisfied by a versioned symbol in another DSO. */
5889 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
5890 const struct elf_backend_data
*bed
,
5891 struct elf_link_hash_entry
*h
)
5894 struct elf_link_loaded_list
*loaded
;
5896 if (!is_elf_hash_table (info
->hash
))
5899 switch (h
->root
.type
)
5905 case bfd_link_hash_undefined
:
5906 case bfd_link_hash_undefweak
:
5907 abfd
= h
->root
.u
.undef
.abfd
;
5908 if ((abfd
->flags
& DYNAMIC
) == 0
5909 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
5913 case bfd_link_hash_defined
:
5914 case bfd_link_hash_defweak
:
5915 abfd
= h
->root
.u
.def
.section
->owner
;
5918 case bfd_link_hash_common
:
5919 abfd
= h
->root
.u
.c
.p
->section
->owner
;
5922 BFD_ASSERT (abfd
!= NULL
);
5924 for (loaded
= elf_hash_table (info
)->loaded
;
5926 loaded
= loaded
->next
)
5929 Elf_Internal_Shdr
*hdr
;
5930 bfd_size_type symcount
;
5931 bfd_size_type extsymcount
;
5932 bfd_size_type extsymoff
;
5933 Elf_Internal_Shdr
*versymhdr
;
5934 Elf_Internal_Sym
*isym
;
5935 Elf_Internal_Sym
*isymend
;
5936 Elf_Internal_Sym
*isymbuf
;
5937 Elf_External_Versym
*ever
;
5938 Elf_External_Versym
*extversym
;
5940 input
= loaded
->abfd
;
5942 /* We check each DSO for a possible hidden versioned definition. */
5944 || (input
->flags
& DYNAMIC
) == 0
5945 || elf_dynversym (input
) == 0)
5948 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
5950 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
5951 if (elf_bad_symtab (input
))
5953 extsymcount
= symcount
;
5958 extsymcount
= symcount
- hdr
->sh_info
;
5959 extsymoff
= hdr
->sh_info
;
5962 if (extsymcount
== 0)
5965 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
5967 if (isymbuf
== NULL
)
5970 /* Read in any version definitions. */
5971 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
5972 extversym
= bfd_malloc (versymhdr
->sh_size
);
5973 if (extversym
== NULL
)
5976 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
5977 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
5978 != versymhdr
->sh_size
))
5986 ever
= extversym
+ extsymoff
;
5987 isymend
= isymbuf
+ extsymcount
;
5988 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
5991 Elf_Internal_Versym iver
;
5992 unsigned short version_index
;
5994 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
5995 || isym
->st_shndx
== SHN_UNDEF
)
5998 name
= bfd_elf_string_from_elf_section (input
,
6001 if (strcmp (name
, h
->root
.root
.string
) != 0)
6004 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6006 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6008 /* If we have a non-hidden versioned sym, then it should
6009 have provided a definition for the undefined sym. */
6013 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6014 if (version_index
== 1 || version_index
== 2)
6016 /* This is the base or first version. We can use it. */
6030 /* Add an external symbol to the symbol table. This is called from
6031 the hash table traversal routine. When generating a shared object,
6032 we go through the symbol table twice. The first time we output
6033 anything that might have been forced to local scope in a version
6034 script. The second time we output the symbols that are still
6038 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6040 struct elf_outext_info
*eoinfo
= data
;
6041 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6043 Elf_Internal_Sym sym
;
6044 asection
*input_sec
;
6045 const struct elf_backend_data
*bed
;
6047 if (h
->root
.type
== bfd_link_hash_warning
)
6049 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6050 if (h
->root
.type
== bfd_link_hash_new
)
6054 /* Decide whether to output this symbol in this pass. */
6055 if (eoinfo
->localsyms
)
6057 if (!h
->forced_local
)
6062 if (h
->forced_local
)
6066 bed
= get_elf_backend_data (finfo
->output_bfd
);
6068 /* If we have an undefined symbol reference here then it must have
6069 come from a shared library that is being linked in. (Undefined
6070 references in regular files have already been handled). If we
6071 are reporting errors for this situation then do so now. */
6072 if (h
->root
.type
== bfd_link_hash_undefined
6075 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6076 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6078 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6079 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6080 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6082 eoinfo
->failed
= TRUE
;
6087 /* We should also warn if a forced local symbol is referenced from
6088 shared libraries. */
6089 if (! finfo
->info
->relocatable
6090 && (! finfo
->info
->shared
)
6095 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6097 (*_bfd_error_handler
)
6098 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6099 finfo
->output_bfd
, h
->root
.u
.def
.section
->owner
,
6100 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6102 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6103 ? "hidden" : "local",
6104 h
->root
.root
.string
);
6105 eoinfo
->failed
= TRUE
;
6109 /* We don't want to output symbols that have never been mentioned by
6110 a regular file, or that we have been told to strip. However, if
6111 h->indx is set to -2, the symbol is used by a reloc and we must
6115 else if ((h
->def_dynamic
6120 else if (finfo
->info
->strip
== strip_all
)
6122 else if (finfo
->info
->strip
== strip_some
6123 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6124 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6126 else if (finfo
->info
->strip_discarded
6127 && (h
->root
.type
== bfd_link_hash_defined
6128 || h
->root
.type
== bfd_link_hash_defweak
)
6129 && elf_discarded_section (h
->root
.u
.def
.section
))
6134 /* If we're stripping it, and it's not a dynamic symbol, there's
6135 nothing else to do unless it is a forced local symbol. */
6138 && !h
->forced_local
)
6142 sym
.st_size
= h
->size
;
6143 sym
.st_other
= h
->other
;
6144 if (h
->forced_local
)
6145 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6146 else if (h
->root
.type
== bfd_link_hash_undefweak
6147 || h
->root
.type
== bfd_link_hash_defweak
)
6148 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6150 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6152 switch (h
->root
.type
)
6155 case bfd_link_hash_new
:
6156 case bfd_link_hash_warning
:
6160 case bfd_link_hash_undefined
:
6161 case bfd_link_hash_undefweak
:
6162 input_sec
= bfd_und_section_ptr
;
6163 sym
.st_shndx
= SHN_UNDEF
;
6166 case bfd_link_hash_defined
:
6167 case bfd_link_hash_defweak
:
6169 input_sec
= h
->root
.u
.def
.section
;
6170 if (input_sec
->output_section
!= NULL
)
6173 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6174 input_sec
->output_section
);
6175 if (sym
.st_shndx
== SHN_BAD
)
6177 (*_bfd_error_handler
)
6178 (_("%B: could not find output section %A for input section %A"),
6179 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6180 eoinfo
->failed
= TRUE
;
6184 /* ELF symbols in relocatable files are section relative,
6185 but in nonrelocatable files they are virtual
6187 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6188 if (! finfo
->info
->relocatable
)
6190 sym
.st_value
+= input_sec
->output_section
->vma
;
6191 if (h
->type
== STT_TLS
)
6193 /* STT_TLS symbols are relative to PT_TLS segment
6195 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6196 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6202 BFD_ASSERT (input_sec
->owner
== NULL
6203 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6204 sym
.st_shndx
= SHN_UNDEF
;
6205 input_sec
= bfd_und_section_ptr
;
6210 case bfd_link_hash_common
:
6211 input_sec
= h
->root
.u
.c
.p
->section
;
6212 sym
.st_shndx
= SHN_COMMON
;
6213 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6216 case bfd_link_hash_indirect
:
6217 /* These symbols are created by symbol versioning. They point
6218 to the decorated version of the name. For example, if the
6219 symbol foo@@GNU_1.2 is the default, which should be used when
6220 foo is used with no version, then we add an indirect symbol
6221 foo which points to foo@@GNU_1.2. We ignore these symbols,
6222 since the indirected symbol is already in the hash table. */
6226 /* Give the processor backend a chance to tweak the symbol value,
6227 and also to finish up anything that needs to be done for this
6228 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6229 forced local syms when non-shared is due to a historical quirk. */
6230 if ((h
->dynindx
!= -1
6232 && ((finfo
->info
->shared
6233 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6234 || h
->root
.type
!= bfd_link_hash_undefweak
))
6235 || !h
->forced_local
)
6236 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6238 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6239 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6241 eoinfo
->failed
= TRUE
;
6246 /* If we are marking the symbol as undefined, and there are no
6247 non-weak references to this symbol from a regular object, then
6248 mark the symbol as weak undefined; if there are non-weak
6249 references, mark the symbol as strong. We can't do this earlier,
6250 because it might not be marked as undefined until the
6251 finish_dynamic_symbol routine gets through with it. */
6252 if (sym
.st_shndx
== SHN_UNDEF
6254 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6255 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6259 if (h
->ref_regular_nonweak
)
6260 bindtype
= STB_GLOBAL
;
6262 bindtype
= STB_WEAK
;
6263 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6266 /* If a non-weak symbol with non-default visibility is not defined
6267 locally, it is a fatal error. */
6268 if (! finfo
->info
->relocatable
6269 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6270 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6271 && h
->root
.type
== bfd_link_hash_undefined
6274 (*_bfd_error_handler
)
6275 (_("%B: %s symbol `%s' isn't defined"),
6277 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6279 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6280 ? "internal" : "hidden",
6281 h
->root
.root
.string
);
6282 eoinfo
->failed
= TRUE
;
6286 /* If this symbol should be put in the .dynsym section, then put it
6287 there now. We already know the symbol index. We also fill in
6288 the entry in the .hash section. */
6289 if (h
->dynindx
!= -1
6290 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6294 size_t hash_entry_size
;
6295 bfd_byte
*bucketpos
;
6299 sym
.st_name
= h
->dynstr_index
;
6300 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6301 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6303 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6304 bucket
= h
->u
.elf_hash_value
% bucketcount
;
6306 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6307 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6308 + (bucket
+ 2) * hash_entry_size
);
6309 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6310 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6311 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6312 ((bfd_byte
*) finfo
->hash_sec
->contents
6313 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6315 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6317 Elf_Internal_Versym iversym
;
6318 Elf_External_Versym
*eversym
;
6320 if (!h
->def_regular
)
6322 if (h
->verinfo
.verdef
== NULL
)
6323 iversym
.vs_vers
= 0;
6325 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6329 if (h
->verinfo
.vertree
== NULL
)
6330 iversym
.vs_vers
= 1;
6332 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6336 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6338 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6339 eversym
+= h
->dynindx
;
6340 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6344 /* If we're stripping it, then it was just a dynamic symbol, and
6345 there's nothing else to do. */
6346 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6349 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6351 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6353 eoinfo
->failed
= TRUE
;
6360 /* Return TRUE if special handling is done for relocs in SEC against
6361 symbols defined in discarded sections. */
6364 elf_section_ignore_discarded_relocs (asection
*sec
)
6366 const struct elf_backend_data
*bed
;
6368 switch (sec
->sec_info_type
)
6370 case ELF_INFO_TYPE_STABS
:
6371 case ELF_INFO_TYPE_EH_FRAME
:
6377 bed
= get_elf_backend_data (sec
->owner
);
6378 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6379 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6385 enum action_discarded
6391 /* Return a mask saying how ld should treat relocations in SEC against
6392 symbols defined in discarded sections. If this function returns
6393 COMPLAIN set, ld will issue a warning message. If this function
6394 returns PRETEND set, and the discarded section was link-once and the
6395 same size as the kept link-once section, ld will pretend that the
6396 symbol was actually defined in the kept section. Otherwise ld will
6397 zero the reloc (at least that is the intent, but some cooperation by
6398 the target dependent code is needed, particularly for REL targets). */
6401 elf_action_discarded (asection
*sec
)
6403 if (sec
->flags
& SEC_DEBUGGING
)
6406 if (strcmp (".eh_frame", sec
->name
) == 0)
6409 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6412 if (strcmp (".PARISC.unwind", sec
->name
) == 0)
6415 if (strcmp (".fixup", sec
->name
) == 0)
6418 return COMPLAIN
| PRETEND
;
6421 /* Find a match between a section and a member of a section group. */
6424 match_group_member (asection
*sec
, asection
*group
)
6426 asection
*first
= elf_next_in_group (group
);
6427 asection
*s
= first
;
6431 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6441 /* Link an input file into the linker output file. This function
6442 handles all the sections and relocations of the input file at once.
6443 This is so that we only have to read the local symbols once, and
6444 don't have to keep them in memory. */
6447 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6449 bfd_boolean (*relocate_section
)
6450 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6451 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6453 Elf_Internal_Shdr
*symtab_hdr
;
6456 Elf_Internal_Sym
*isymbuf
;
6457 Elf_Internal_Sym
*isym
;
6458 Elf_Internal_Sym
*isymend
;
6460 asection
**ppsection
;
6462 const struct elf_backend_data
*bed
;
6463 bfd_boolean emit_relocs
;
6464 struct elf_link_hash_entry
**sym_hashes
;
6466 output_bfd
= finfo
->output_bfd
;
6467 bed
= get_elf_backend_data (output_bfd
);
6468 relocate_section
= bed
->elf_backend_relocate_section
;
6470 /* If this is a dynamic object, we don't want to do anything here:
6471 we don't want the local symbols, and we don't want the section
6473 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6476 emit_relocs
= (finfo
->info
->relocatable
6477 || finfo
->info
->emitrelocations
6478 || bed
->elf_backend_emit_relocs
);
6480 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6481 if (elf_bad_symtab (input_bfd
))
6483 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6488 locsymcount
= symtab_hdr
->sh_info
;
6489 extsymoff
= symtab_hdr
->sh_info
;
6492 /* Read the local symbols. */
6493 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6494 if (isymbuf
== NULL
&& locsymcount
!= 0)
6496 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6497 finfo
->internal_syms
,
6498 finfo
->external_syms
,
6499 finfo
->locsym_shndx
);
6500 if (isymbuf
== NULL
)
6504 /* Find local symbol sections and adjust values of symbols in
6505 SEC_MERGE sections. Write out those local symbols we know are
6506 going into the output file. */
6507 isymend
= isymbuf
+ locsymcount
;
6508 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6510 isym
++, pindex
++, ppsection
++)
6514 Elf_Internal_Sym osym
;
6518 if (elf_bad_symtab (input_bfd
))
6520 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6527 if (isym
->st_shndx
== SHN_UNDEF
)
6528 isec
= bfd_und_section_ptr
;
6529 else if (isym
->st_shndx
< SHN_LORESERVE
6530 || isym
->st_shndx
> SHN_HIRESERVE
)
6532 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6534 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6535 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6537 _bfd_merged_section_offset (output_bfd
, &isec
,
6538 elf_section_data (isec
)->sec_info
,
6541 else if (isym
->st_shndx
== SHN_ABS
)
6542 isec
= bfd_abs_section_ptr
;
6543 else if (isym
->st_shndx
== SHN_COMMON
)
6544 isec
= bfd_com_section_ptr
;
6553 /* Don't output the first, undefined, symbol. */
6554 if (ppsection
== finfo
->sections
)
6557 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6559 /* We never output section symbols. Instead, we use the
6560 section symbol of the corresponding section in the output
6565 /* If we are stripping all symbols, we don't want to output this
6567 if (finfo
->info
->strip
== strip_all
)
6570 /* If we are discarding all local symbols, we don't want to
6571 output this one. If we are generating a relocatable output
6572 file, then some of the local symbols may be required by
6573 relocs; we output them below as we discover that they are
6575 if (finfo
->info
->discard
== discard_all
)
6578 /* If this symbol is defined in a section which we are
6579 discarding, we don't need to keep it, but note that
6580 linker_mark is only reliable for sections that have contents.
6581 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6582 as well as linker_mark. */
6583 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6585 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6586 || (! finfo
->info
->relocatable
6587 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6590 /* Get the name of the symbol. */
6591 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6596 /* See if we are discarding symbols with this name. */
6597 if ((finfo
->info
->strip
== strip_some
6598 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6600 || (((finfo
->info
->discard
== discard_sec_merge
6601 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6602 || finfo
->info
->discard
== discard_l
)
6603 && bfd_is_local_label_name (input_bfd
, name
)))
6606 /* If we get here, we are going to output this symbol. */
6610 /* Adjust the section index for the output file. */
6611 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6612 isec
->output_section
);
6613 if (osym
.st_shndx
== SHN_BAD
)
6616 *pindex
= bfd_get_symcount (output_bfd
);
6618 /* ELF symbols in relocatable files are section relative, but
6619 in executable files they are virtual addresses. Note that
6620 this code assumes that all ELF sections have an associated
6621 BFD section with a reasonable value for output_offset; below
6622 we assume that they also have a reasonable value for
6623 output_section. Any special sections must be set up to meet
6624 these requirements. */
6625 osym
.st_value
+= isec
->output_offset
;
6626 if (! finfo
->info
->relocatable
)
6628 osym
.st_value
+= isec
->output_section
->vma
;
6629 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6631 /* STT_TLS symbols are relative to PT_TLS segment base. */
6632 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6633 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6637 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6641 /* Relocate the contents of each section. */
6642 sym_hashes
= elf_sym_hashes (input_bfd
);
6643 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6647 if (! o
->linker_mark
)
6649 /* This section was omitted from the link. */
6653 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6654 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6657 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6659 /* Section was created by _bfd_elf_link_create_dynamic_sections
6664 /* Get the contents of the section. They have been cached by a
6665 relaxation routine. Note that o is a section in an input
6666 file, so the contents field will not have been set by any of
6667 the routines which work on output files. */
6668 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6669 contents
= elf_section_data (o
)->this_hdr
.contents
;
6672 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6674 contents
= finfo
->contents
;
6675 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6679 if ((o
->flags
& SEC_RELOC
) != 0)
6681 Elf_Internal_Rela
*internal_relocs
;
6682 bfd_vma r_type_mask
;
6685 /* Get the swapped relocs. */
6687 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6688 finfo
->internal_relocs
, FALSE
);
6689 if (internal_relocs
== NULL
6690 && o
->reloc_count
> 0)
6693 if (bed
->s
->arch_size
== 32)
6700 r_type_mask
= 0xffffffff;
6704 /* Run through the relocs looking for any against symbols
6705 from discarded sections and section symbols from
6706 removed link-once sections. Complain about relocs
6707 against discarded sections. Zero relocs against removed
6708 link-once sections. Preserve debug information as much
6710 if (!elf_section_ignore_discarded_relocs (o
))
6712 Elf_Internal_Rela
*rel
, *relend
;
6713 unsigned int action
= elf_action_discarded (o
);
6715 rel
= internal_relocs
;
6716 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6717 for ( ; rel
< relend
; rel
++)
6719 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
6720 asection
**ps
, *sec
;
6721 struct elf_link_hash_entry
*h
= NULL
;
6722 const char *sym_name
;
6724 if (r_symndx
== STN_UNDEF
)
6727 if (r_symndx
>= locsymcount
6728 || (elf_bad_symtab (input_bfd
)
6729 && finfo
->sections
[r_symndx
] == NULL
))
6731 h
= sym_hashes
[r_symndx
- extsymoff
];
6732 while (h
->root
.type
== bfd_link_hash_indirect
6733 || h
->root
.type
== bfd_link_hash_warning
)
6734 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6736 if (h
->root
.type
!= bfd_link_hash_defined
6737 && h
->root
.type
!= bfd_link_hash_defweak
)
6740 ps
= &h
->root
.u
.def
.section
;
6741 sym_name
= h
->root
.root
.string
;
6745 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
6746 ps
= &finfo
->sections
[r_symndx
];
6747 sym_name
= bfd_elf_local_sym_name (input_bfd
, sym
);
6750 /* Complain if the definition comes from a
6751 discarded section. */
6752 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
6756 BFD_ASSERT (r_symndx
!= 0);
6757 if (action
& COMPLAIN
)
6759 (*_bfd_error_handler
)
6760 (_("`%s' referenced in section `%A' of %B: "
6761 "defined in discarded section `%A' of %B\n"),
6762 o
, input_bfd
, sec
, sec
->owner
, sym_name
);
6765 /* Try to do the best we can to support buggy old
6766 versions of gcc. If we've warned, or this is
6767 debugging info, pretend that the symbol is
6768 really defined in the kept linkonce section.
6769 FIXME: This is quite broken. Modifying the
6770 symbol here means we will be changing all later
6771 uses of the symbol, not just in this section.
6772 The only thing that makes this half reasonable
6773 is that we warn in non-debug sections, and
6774 debug sections tend to come after other
6776 kept
= sec
->kept_section
;
6777 if (kept
!= NULL
&& (action
& PRETEND
))
6779 if (elf_sec_group (sec
) != NULL
)
6780 kept
= match_group_member (sec
, kept
);
6782 && sec
->size
== kept
->size
)
6789 /* Remove the symbol reference from the reloc, but
6790 don't kill the reloc completely. This is so that
6791 a zero value will be written into the section,
6792 which may have non-zero contents put there by the
6793 assembler. Zero in things like an eh_frame fde
6794 pc_begin allows stack unwinders to recognize the
6796 rel
->r_info
&= r_type_mask
;
6802 /* Relocate the section by invoking a back end routine.
6804 The back end routine is responsible for adjusting the
6805 section contents as necessary, and (if using Rela relocs
6806 and generating a relocatable output file) adjusting the
6807 reloc addend as necessary.
6809 The back end routine does not have to worry about setting
6810 the reloc address or the reloc symbol index.
6812 The back end routine is given a pointer to the swapped in
6813 internal symbols, and can access the hash table entries
6814 for the external symbols via elf_sym_hashes (input_bfd).
6816 When generating relocatable output, the back end routine
6817 must handle STB_LOCAL/STT_SECTION symbols specially. The
6818 output symbol is going to be a section symbol
6819 corresponding to the output section, which will require
6820 the addend to be adjusted. */
6822 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6823 input_bfd
, o
, contents
,
6831 Elf_Internal_Rela
*irela
;
6832 Elf_Internal_Rela
*irelaend
;
6833 bfd_vma last_offset
;
6834 struct elf_link_hash_entry
**rel_hash
;
6835 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6836 unsigned int next_erel
;
6837 bfd_boolean (*reloc_emitter
)
6838 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
6839 bfd_boolean rela_normal
;
6841 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6842 rela_normal
= (bed
->rela_normal
6843 && (input_rel_hdr
->sh_entsize
6844 == bed
->s
->sizeof_rela
));
6846 /* Adjust the reloc addresses and symbol indices. */
6848 irela
= internal_relocs
;
6849 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6850 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6851 + elf_section_data (o
->output_section
)->rel_count
6852 + elf_section_data (o
->output_section
)->rel_count2
);
6853 last_offset
= o
->output_offset
;
6854 if (!finfo
->info
->relocatable
)
6855 last_offset
+= o
->output_section
->vma
;
6856 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6858 unsigned long r_symndx
;
6860 Elf_Internal_Sym sym
;
6862 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6868 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
6871 if (irela
->r_offset
>= (bfd_vma
) -2)
6873 /* This is a reloc for a deleted entry or somesuch.
6874 Turn it into an R_*_NONE reloc, at the same
6875 offset as the last reloc. elf_eh_frame.c and
6876 elf_bfd_discard_info rely on reloc offsets
6878 irela
->r_offset
= last_offset
;
6880 irela
->r_addend
= 0;
6884 irela
->r_offset
+= o
->output_offset
;
6886 /* Relocs in an executable have to be virtual addresses. */
6887 if (!finfo
->info
->relocatable
)
6888 irela
->r_offset
+= o
->output_section
->vma
;
6890 last_offset
= irela
->r_offset
;
6892 r_symndx
= irela
->r_info
>> r_sym_shift
;
6893 if (r_symndx
== STN_UNDEF
)
6896 if (r_symndx
>= locsymcount
6897 || (elf_bad_symtab (input_bfd
)
6898 && finfo
->sections
[r_symndx
] == NULL
))
6900 struct elf_link_hash_entry
*rh
;
6903 /* This is a reloc against a global symbol. We
6904 have not yet output all the local symbols, so
6905 we do not know the symbol index of any global
6906 symbol. We set the rel_hash entry for this
6907 reloc to point to the global hash table entry
6908 for this symbol. The symbol index is then
6909 set at the end of bfd_elf_final_link. */
6910 indx
= r_symndx
- extsymoff
;
6911 rh
= elf_sym_hashes (input_bfd
)[indx
];
6912 while (rh
->root
.type
== bfd_link_hash_indirect
6913 || rh
->root
.type
== bfd_link_hash_warning
)
6914 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6916 /* Setting the index to -2 tells
6917 elf_link_output_extsym that this symbol is
6919 BFD_ASSERT (rh
->indx
< 0);
6927 /* This is a reloc against a local symbol. */
6930 sym
= isymbuf
[r_symndx
];
6931 sec
= finfo
->sections
[r_symndx
];
6932 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
6934 /* I suppose the backend ought to fill in the
6935 section of any STT_SECTION symbol against a
6936 processor specific section. */
6938 if (bfd_is_abs_section (sec
))
6940 else if (sec
== NULL
|| sec
->owner
== NULL
)
6942 bfd_set_error (bfd_error_bad_value
);
6947 asection
*osec
= sec
->output_section
;
6949 /* If we have discarded a section, the output
6950 section will be the absolute section. In
6951 case of discarded link-once and discarded
6952 SEC_MERGE sections, use the kept section. */
6953 if (bfd_is_abs_section (osec
)
6954 && sec
->kept_section
!= NULL
6955 && sec
->kept_section
->output_section
!= NULL
)
6957 osec
= sec
->kept_section
->output_section
;
6958 irela
->r_addend
-= osec
->vma
;
6961 if (!bfd_is_abs_section (osec
))
6963 r_symndx
= osec
->target_index
;
6964 BFD_ASSERT (r_symndx
!= 0);
6968 /* Adjust the addend according to where the
6969 section winds up in the output section. */
6971 irela
->r_addend
+= sec
->output_offset
;
6975 if (finfo
->indices
[r_symndx
] == -1)
6977 unsigned long shlink
;
6981 if (finfo
->info
->strip
== strip_all
)
6983 /* You can't do ld -r -s. */
6984 bfd_set_error (bfd_error_invalid_operation
);
6988 /* This symbol was skipped earlier, but
6989 since it is needed by a reloc, we
6990 must output it now. */
6991 shlink
= symtab_hdr
->sh_link
;
6992 name
= (bfd_elf_string_from_elf_section
6993 (input_bfd
, shlink
, sym
.st_name
));
6997 osec
= sec
->output_section
;
6999 _bfd_elf_section_from_bfd_section (output_bfd
,
7001 if (sym
.st_shndx
== SHN_BAD
)
7004 sym
.st_value
+= sec
->output_offset
;
7005 if (! finfo
->info
->relocatable
)
7007 sym
.st_value
+= osec
->vma
;
7008 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7010 /* STT_TLS symbols are relative to PT_TLS
7012 BFD_ASSERT (elf_hash_table (finfo
->info
)
7014 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7019 finfo
->indices
[r_symndx
]
7020 = bfd_get_symcount (output_bfd
);
7022 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7027 r_symndx
= finfo
->indices
[r_symndx
];
7030 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7031 | (irela
->r_info
& r_type_mask
));
7034 /* Swap out the relocs. */
7035 if (bed
->elf_backend_emit_relocs
7036 && !(finfo
->info
->relocatable
7037 || finfo
->info
->emitrelocations
))
7038 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7040 reloc_emitter
= _bfd_elf_link_output_relocs
;
7042 if (input_rel_hdr
->sh_size
!= 0
7043 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7047 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7048 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7050 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7051 * bed
->s
->int_rels_per_ext_rel
);
7052 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
7059 /* Write out the modified section contents. */
7060 if (bed
->elf_backend_write_section
7061 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7063 /* Section written out. */
7065 else switch (o
->sec_info_type
)
7067 case ELF_INFO_TYPE_STABS
:
7068 if (! (_bfd_write_section_stabs
7070 &elf_hash_table (finfo
->info
)->stab_info
,
7071 o
, &elf_section_data (o
)->sec_info
, contents
)))
7074 case ELF_INFO_TYPE_MERGE
:
7075 if (! _bfd_write_merged_section (output_bfd
, o
,
7076 elf_section_data (o
)->sec_info
))
7079 case ELF_INFO_TYPE_EH_FRAME
:
7081 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7088 if (! (o
->flags
& SEC_EXCLUDE
)
7089 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7091 (file_ptr
) o
->output_offset
,
7102 /* Generate a reloc when linking an ELF file. This is a reloc
7103 requested by the linker, and does come from any input file. This
7104 is used to build constructor and destructor tables when linking
7108 elf_reloc_link_order (bfd
*output_bfd
,
7109 struct bfd_link_info
*info
,
7110 asection
*output_section
,
7111 struct bfd_link_order
*link_order
)
7113 reloc_howto_type
*howto
;
7117 struct elf_link_hash_entry
**rel_hash_ptr
;
7118 Elf_Internal_Shdr
*rel_hdr
;
7119 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7120 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7124 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7127 bfd_set_error (bfd_error_bad_value
);
7131 addend
= link_order
->u
.reloc
.p
->addend
;
7133 /* Figure out the symbol index. */
7134 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7135 + elf_section_data (output_section
)->rel_count
7136 + elf_section_data (output_section
)->rel_count2
);
7137 if (link_order
->type
== bfd_section_reloc_link_order
)
7139 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7140 BFD_ASSERT (indx
!= 0);
7141 *rel_hash_ptr
= NULL
;
7145 struct elf_link_hash_entry
*h
;
7147 /* Treat a reloc against a defined symbol as though it were
7148 actually against the section. */
7149 h
= ((struct elf_link_hash_entry
*)
7150 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7151 link_order
->u
.reloc
.p
->u
.name
,
7152 FALSE
, FALSE
, TRUE
));
7154 && (h
->root
.type
== bfd_link_hash_defined
7155 || h
->root
.type
== bfd_link_hash_defweak
))
7159 section
= h
->root
.u
.def
.section
;
7160 indx
= section
->output_section
->target_index
;
7161 *rel_hash_ptr
= NULL
;
7162 /* It seems that we ought to add the symbol value to the
7163 addend here, but in practice it has already been added
7164 because it was passed to constructor_callback. */
7165 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7169 /* Setting the index to -2 tells elf_link_output_extsym that
7170 this symbol is used by a reloc. */
7177 if (! ((*info
->callbacks
->unattached_reloc
)
7178 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7184 /* If this is an inplace reloc, we must write the addend into the
7186 if (howto
->partial_inplace
&& addend
!= 0)
7189 bfd_reloc_status_type rstat
;
7192 const char *sym_name
;
7194 size
= bfd_get_reloc_size (howto
);
7195 buf
= bfd_zmalloc (size
);
7198 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7205 case bfd_reloc_outofrange
:
7208 case bfd_reloc_overflow
:
7209 if (link_order
->type
== bfd_section_reloc_link_order
)
7210 sym_name
= bfd_section_name (output_bfd
,
7211 link_order
->u
.reloc
.p
->u
.section
);
7213 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7214 if (! ((*info
->callbacks
->reloc_overflow
)
7215 (info
, sym_name
, howto
->name
, addend
, NULL
, NULL
, 0)))
7222 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7223 link_order
->offset
, size
);
7229 /* The address of a reloc is relative to the section in a
7230 relocatable file, and is a virtual address in an executable
7232 offset
= link_order
->offset
;
7233 if (! info
->relocatable
)
7234 offset
+= output_section
->vma
;
7236 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7238 irel
[i
].r_offset
= offset
;
7240 irel
[i
].r_addend
= 0;
7242 if (bed
->s
->arch_size
== 32)
7243 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7245 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7247 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7248 erel
= rel_hdr
->contents
;
7249 if (rel_hdr
->sh_type
== SHT_REL
)
7251 erel
+= (elf_section_data (output_section
)->rel_count
7252 * bed
->s
->sizeof_rel
);
7253 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7257 irel
[0].r_addend
= addend
;
7258 erel
+= (elf_section_data (output_section
)->rel_count
7259 * bed
->s
->sizeof_rela
);
7260 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7263 ++elf_section_data (output_section
)->rel_count
;
7269 /* Get the output vma of the section pointed to by the sh_link field. */
7272 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7274 Elf_Internal_Shdr
**elf_shdrp
;
7278 s
= p
->u
.indirect
.section
;
7279 elf_shdrp
= elf_elfsections (s
->owner
);
7280 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7281 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7283 The Intel C compiler generates SHT_IA_64_UNWIND with
7284 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7285 sh_info fields. Hence we could get the situation
7286 where elfsec is 0. */
7289 const struct elf_backend_data
*bed
7290 = get_elf_backend_data (s
->owner
);
7291 if (bed
->link_order_error_handler
)
7292 bed
->link_order_error_handler
7293 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7298 s
= elf_shdrp
[elfsec
]->bfd_section
;
7299 return s
->output_section
->vma
+ s
->output_offset
;
7304 /* Compare two sections based on the locations of the sections they are
7305 linked to. Used by elf_fixup_link_order. */
7308 compare_link_order (const void * a
, const void * b
)
7313 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7314 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7321 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7322 order as their linked sections. Returns false if this could not be done
7323 because an output section includes both ordered and unordered
7324 sections. Ideally we'd do this in the linker proper. */
7327 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7332 struct bfd_link_order
*p
;
7334 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7336 struct bfd_link_order
**sections
;
7342 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7344 if (p
->type
== bfd_indirect_link_order
7345 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7346 == bfd_target_elf_flavour
)
7347 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7349 s
= p
->u
.indirect
.section
;
7350 elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
);
7352 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7361 if (!seen_linkorder
)
7364 if (seen_other
&& seen_linkorder
)
7366 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7368 bfd_set_error (bfd_error_bad_value
);
7372 sections
= (struct bfd_link_order
**)
7373 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7376 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7378 sections
[seen_linkorder
++] = p
;
7380 /* Sort the input sections in the order of their linked section. */
7381 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7382 compare_link_order
);
7384 /* Change the offsets of the sections. */
7386 for (n
= 0; n
< seen_linkorder
; n
++)
7388 s
= sections
[n
]->u
.indirect
.section
;
7389 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7390 s
->output_offset
= offset
;
7391 sections
[n
]->offset
= offset
;
7392 offset
+= sections
[n
]->size
;
7399 /* Do the final step of an ELF link. */
7402 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7404 bfd_boolean dynamic
;
7405 bfd_boolean emit_relocs
;
7407 struct elf_final_link_info finfo
;
7408 register asection
*o
;
7409 register struct bfd_link_order
*p
;
7411 bfd_size_type max_contents_size
;
7412 bfd_size_type max_external_reloc_size
;
7413 bfd_size_type max_internal_reloc_count
;
7414 bfd_size_type max_sym_count
;
7415 bfd_size_type max_sym_shndx_count
;
7417 Elf_Internal_Sym elfsym
;
7419 Elf_Internal_Shdr
*symtab_hdr
;
7420 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7421 Elf_Internal_Shdr
*symstrtab_hdr
;
7422 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7423 struct elf_outext_info eoinfo
;
7425 size_t relativecount
= 0;
7426 asection
*reldyn
= 0;
7429 if (! is_elf_hash_table (info
->hash
))
7433 abfd
->flags
|= DYNAMIC
;
7435 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7436 dynobj
= elf_hash_table (info
)->dynobj
;
7438 emit_relocs
= (info
->relocatable
7439 || info
->emitrelocations
7440 || bed
->elf_backend_emit_relocs
);
7443 finfo
.output_bfd
= abfd
;
7444 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7445 if (finfo
.symstrtab
== NULL
)
7450 finfo
.dynsym_sec
= NULL
;
7451 finfo
.hash_sec
= NULL
;
7452 finfo
.symver_sec
= NULL
;
7456 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7457 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7458 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7459 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7460 /* Note that it is OK if symver_sec is NULL. */
7463 finfo
.contents
= NULL
;
7464 finfo
.external_relocs
= NULL
;
7465 finfo
.internal_relocs
= NULL
;
7466 finfo
.external_syms
= NULL
;
7467 finfo
.locsym_shndx
= NULL
;
7468 finfo
.internal_syms
= NULL
;
7469 finfo
.indices
= NULL
;
7470 finfo
.sections
= NULL
;
7471 finfo
.symbuf
= NULL
;
7472 finfo
.symshndxbuf
= NULL
;
7473 finfo
.symbuf_count
= 0;
7474 finfo
.shndxbuf_size
= 0;
7476 /* Count up the number of relocations we will output for each output
7477 section, so that we know the sizes of the reloc sections. We
7478 also figure out some maximum sizes. */
7479 max_contents_size
= 0;
7480 max_external_reloc_size
= 0;
7481 max_internal_reloc_count
= 0;
7483 max_sym_shndx_count
= 0;
7485 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7487 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7490 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7492 unsigned int reloc_count
= 0;
7493 struct bfd_elf_section_data
*esdi
= NULL
;
7494 unsigned int *rel_count1
;
7496 if (p
->type
== bfd_section_reloc_link_order
7497 || p
->type
== bfd_symbol_reloc_link_order
)
7499 else if (p
->type
== bfd_indirect_link_order
)
7503 sec
= p
->u
.indirect
.section
;
7504 esdi
= elf_section_data (sec
);
7506 /* Mark all sections which are to be included in the
7507 link. This will normally be every section. We need
7508 to do this so that we can identify any sections which
7509 the linker has decided to not include. */
7510 sec
->linker_mark
= TRUE
;
7512 if (sec
->flags
& SEC_MERGE
)
7515 if (info
->relocatable
|| info
->emitrelocations
)
7516 reloc_count
= sec
->reloc_count
;
7517 else if (bed
->elf_backend_count_relocs
)
7519 Elf_Internal_Rela
* relocs
;
7521 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7524 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7526 if (elf_section_data (o
)->relocs
!= relocs
)
7530 if (sec
->rawsize
> max_contents_size
)
7531 max_contents_size
= sec
->rawsize
;
7532 if (sec
->size
> max_contents_size
)
7533 max_contents_size
= sec
->size
;
7535 /* We are interested in just local symbols, not all
7537 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7538 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7542 if (elf_bad_symtab (sec
->owner
))
7543 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7544 / bed
->s
->sizeof_sym
);
7546 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7548 if (sym_count
> max_sym_count
)
7549 max_sym_count
= sym_count
;
7551 if (sym_count
> max_sym_shndx_count
7552 && elf_symtab_shndx (sec
->owner
) != 0)
7553 max_sym_shndx_count
= sym_count
;
7555 if ((sec
->flags
& SEC_RELOC
) != 0)
7559 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7560 if (ext_size
> max_external_reloc_size
)
7561 max_external_reloc_size
= ext_size
;
7562 if (sec
->reloc_count
> max_internal_reloc_count
)
7563 max_internal_reloc_count
= sec
->reloc_count
;
7568 if (reloc_count
== 0)
7571 o
->reloc_count
+= reloc_count
;
7573 /* MIPS may have a mix of REL and RELA relocs on sections.
7574 To support this curious ABI we keep reloc counts in
7575 elf_section_data too. We must be careful to add the
7576 relocations from the input section to the right output
7577 count. FIXME: Get rid of one count. We have
7578 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7579 rel_count1
= &esdo
->rel_count
;
7582 bfd_boolean same_size
;
7583 bfd_size_type entsize1
;
7585 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7586 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7587 || entsize1
== bed
->s
->sizeof_rela
);
7588 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7591 rel_count1
= &esdo
->rel_count2
;
7593 if (esdi
->rel_hdr2
!= NULL
)
7595 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7596 unsigned int alt_count
;
7597 unsigned int *rel_count2
;
7599 BFD_ASSERT (entsize2
!= entsize1
7600 && (entsize2
== bed
->s
->sizeof_rel
7601 || entsize2
== bed
->s
->sizeof_rela
));
7603 rel_count2
= &esdo
->rel_count2
;
7605 rel_count2
= &esdo
->rel_count
;
7607 /* The following is probably too simplistic if the
7608 backend counts output relocs unusually. */
7609 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7610 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7611 *rel_count2
+= alt_count
;
7612 reloc_count
-= alt_count
;
7615 *rel_count1
+= reloc_count
;
7618 if (o
->reloc_count
> 0)
7619 o
->flags
|= SEC_RELOC
;
7622 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7623 set it (this is probably a bug) and if it is set
7624 assign_section_numbers will create a reloc section. */
7625 o
->flags
&=~ SEC_RELOC
;
7628 /* If the SEC_ALLOC flag is not set, force the section VMA to
7629 zero. This is done in elf_fake_sections as well, but forcing
7630 the VMA to 0 here will ensure that relocs against these
7631 sections are handled correctly. */
7632 if ((o
->flags
& SEC_ALLOC
) == 0
7633 && ! o
->user_set_vma
)
7637 if (! info
->relocatable
&& merged
)
7638 elf_link_hash_traverse (elf_hash_table (info
),
7639 _bfd_elf_link_sec_merge_syms
, abfd
);
7641 /* Figure out the file positions for everything but the symbol table
7642 and the relocs. We set symcount to force assign_section_numbers
7643 to create a symbol table. */
7644 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7645 BFD_ASSERT (! abfd
->output_has_begun
);
7646 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7649 /* Set sizes, and assign file positions for reloc sections. */
7650 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7652 if ((o
->flags
& SEC_RELOC
) != 0)
7654 if (!(_bfd_elf_link_size_reloc_section
7655 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7658 if (elf_section_data (o
)->rel_hdr2
7659 && !(_bfd_elf_link_size_reloc_section
7660 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7664 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7665 to count upwards while actually outputting the relocations. */
7666 elf_section_data (o
)->rel_count
= 0;
7667 elf_section_data (o
)->rel_count2
= 0;
7670 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7672 /* We have now assigned file positions for all the sections except
7673 .symtab and .strtab. We start the .symtab section at the current
7674 file position, and write directly to it. We build the .strtab
7675 section in memory. */
7676 bfd_get_symcount (abfd
) = 0;
7677 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7678 /* sh_name is set in prep_headers. */
7679 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7680 /* sh_flags, sh_addr and sh_size all start off zero. */
7681 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7682 /* sh_link is set in assign_section_numbers. */
7683 /* sh_info is set below. */
7684 /* sh_offset is set just below. */
7685 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
7687 off
= elf_tdata (abfd
)->next_file_pos
;
7688 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
7690 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7691 incorrect. We do not yet know the size of the .symtab section.
7692 We correct next_file_pos below, after we do know the size. */
7694 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7695 continuously seeking to the right position in the file. */
7696 if (! info
->keep_memory
|| max_sym_count
< 20)
7697 finfo
.symbuf_size
= 20;
7699 finfo
.symbuf_size
= max_sym_count
;
7700 amt
= finfo
.symbuf_size
;
7701 amt
*= bed
->s
->sizeof_sym
;
7702 finfo
.symbuf
= bfd_malloc (amt
);
7703 if (finfo
.symbuf
== NULL
)
7705 if (elf_numsections (abfd
) > SHN_LORESERVE
)
7707 /* Wild guess at number of output symbols. realloc'd as needed. */
7708 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
7709 finfo
.shndxbuf_size
= amt
;
7710 amt
*= sizeof (Elf_External_Sym_Shndx
);
7711 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
7712 if (finfo
.symshndxbuf
== NULL
)
7716 /* Start writing out the symbol table. The first symbol is always a
7718 if (info
->strip
!= strip_all
7721 elfsym
.st_value
= 0;
7724 elfsym
.st_other
= 0;
7725 elfsym
.st_shndx
= SHN_UNDEF
;
7726 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
7732 /* Some standard ELF linkers do this, but we don't because it causes
7733 bootstrap comparison failures. */
7734 /* Output a file symbol for the output file as the second symbol.
7735 We output this even if we are discarding local symbols, although
7736 I'm not sure if this is correct. */
7737 elfsym
.st_value
= 0;
7739 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7740 elfsym
.st_other
= 0;
7741 elfsym
.st_shndx
= SHN_ABS
;
7742 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
7743 &elfsym
, bfd_abs_section_ptr
, NULL
))
7747 /* Output a symbol for each section. We output these even if we are
7748 discarding local symbols, since they are used for relocs. These
7749 symbols have no names. We store the index of each one in the
7750 index field of the section, so that we can find it again when
7751 outputting relocs. */
7752 if (info
->strip
!= strip_all
7756 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7757 elfsym
.st_other
= 0;
7758 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7760 o
= bfd_section_from_elf_index (abfd
, i
);
7762 o
->target_index
= bfd_get_symcount (abfd
);
7763 elfsym
.st_shndx
= i
;
7764 if (info
->relocatable
|| o
== NULL
)
7765 elfsym
.st_value
= 0;
7767 elfsym
.st_value
= o
->vma
;
7768 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
7770 if (i
== SHN_LORESERVE
- 1)
7771 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
7775 /* Allocate some memory to hold information read in from the input
7777 if (max_contents_size
!= 0)
7779 finfo
.contents
= bfd_malloc (max_contents_size
);
7780 if (finfo
.contents
== NULL
)
7784 if (max_external_reloc_size
!= 0)
7786 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
7787 if (finfo
.external_relocs
== NULL
)
7791 if (max_internal_reloc_count
!= 0)
7793 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7794 amt
*= sizeof (Elf_Internal_Rela
);
7795 finfo
.internal_relocs
= bfd_malloc (amt
);
7796 if (finfo
.internal_relocs
== NULL
)
7800 if (max_sym_count
!= 0)
7802 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
7803 finfo
.external_syms
= bfd_malloc (amt
);
7804 if (finfo
.external_syms
== NULL
)
7807 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
7808 finfo
.internal_syms
= bfd_malloc (amt
);
7809 if (finfo
.internal_syms
== NULL
)
7812 amt
= max_sym_count
* sizeof (long);
7813 finfo
.indices
= bfd_malloc (amt
);
7814 if (finfo
.indices
== NULL
)
7817 amt
= max_sym_count
* sizeof (asection
*);
7818 finfo
.sections
= bfd_malloc (amt
);
7819 if (finfo
.sections
== NULL
)
7823 if (max_sym_shndx_count
!= 0)
7825 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
7826 finfo
.locsym_shndx
= bfd_malloc (amt
);
7827 if (finfo
.locsym_shndx
== NULL
)
7831 if (elf_hash_table (info
)->tls_sec
)
7833 bfd_vma base
, end
= 0;
7836 for (sec
= elf_hash_table (info
)->tls_sec
;
7837 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
7840 bfd_vma size
= sec
->size
;
7842 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
7844 struct bfd_link_order
*o
;
7846 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
7847 if (size
< o
->offset
+ o
->size
)
7848 size
= o
->offset
+ o
->size
;
7850 end
= sec
->vma
+ size
;
7852 base
= elf_hash_table (info
)->tls_sec
->vma
;
7853 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
7854 elf_hash_table (info
)->tls_size
= end
- base
;
7857 /* Reorder SHF_LINK_ORDER sections. */
7858 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7860 if (!elf_fixup_link_order (abfd
, o
))
7864 /* Since ELF permits relocations to be against local symbols, we
7865 must have the local symbols available when we do the relocations.
7866 Since we would rather only read the local symbols once, and we
7867 would rather not keep them in memory, we handle all the
7868 relocations for a single input file at the same time.
7870 Unfortunately, there is no way to know the total number of local
7871 symbols until we have seen all of them, and the local symbol
7872 indices precede the global symbol indices. This means that when
7873 we are generating relocatable output, and we see a reloc against
7874 a global symbol, we can not know the symbol index until we have
7875 finished examining all the local symbols to see which ones we are
7876 going to output. To deal with this, we keep the relocations in
7877 memory, and don't output them until the end of the link. This is
7878 an unfortunate waste of memory, but I don't see a good way around
7879 it. Fortunately, it only happens when performing a relocatable
7880 link, which is not the common case. FIXME: If keep_memory is set
7881 we could write the relocs out and then read them again; I don't
7882 know how bad the memory loss will be. */
7884 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7885 sub
->output_has_begun
= FALSE
;
7886 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7888 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7890 if (p
->type
== bfd_indirect_link_order
7891 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7892 == bfd_target_elf_flavour
)
7893 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7895 if (! sub
->output_has_begun
)
7897 if (! elf_link_input_bfd (&finfo
, sub
))
7899 sub
->output_has_begun
= TRUE
;
7902 else if (p
->type
== bfd_section_reloc_link_order
7903 || p
->type
== bfd_symbol_reloc_link_order
)
7905 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
7910 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
7916 /* Output any global symbols that got converted to local in a
7917 version script or due to symbol visibility. We do this in a
7918 separate step since ELF requires all local symbols to appear
7919 prior to any global symbols. FIXME: We should only do this if
7920 some global symbols were, in fact, converted to become local.
7921 FIXME: Will this work correctly with the Irix 5 linker? */
7922 eoinfo
.failed
= FALSE
;
7923 eoinfo
.finfo
= &finfo
;
7924 eoinfo
.localsyms
= TRUE
;
7925 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7930 /* That wrote out all the local symbols. Finish up the symbol table
7931 with the global symbols. Even if we want to strip everything we
7932 can, we still need to deal with those global symbols that got
7933 converted to local in a version script. */
7935 /* The sh_info field records the index of the first non local symbol. */
7936 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
7939 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
7941 Elf_Internal_Sym sym
;
7942 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
7943 long last_local
= 0;
7945 /* Write out the section symbols for the output sections. */
7952 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7955 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7961 dynindx
= elf_section_data (s
)->dynindx
;
7964 indx
= elf_section_data (s
)->this_idx
;
7965 BFD_ASSERT (indx
> 0);
7966 sym
.st_shndx
= indx
;
7967 sym
.st_value
= s
->vma
;
7968 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
7969 if (last_local
< dynindx
)
7970 last_local
= dynindx
;
7971 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
7975 /* Write out the local dynsyms. */
7976 if (elf_hash_table (info
)->dynlocal
)
7978 struct elf_link_local_dynamic_entry
*e
;
7979 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
7984 sym
.st_size
= e
->isym
.st_size
;
7985 sym
.st_other
= e
->isym
.st_other
;
7987 /* Copy the internal symbol as is.
7988 Note that we saved a word of storage and overwrote
7989 the original st_name with the dynstr_index. */
7992 if (e
->isym
.st_shndx
!= SHN_UNDEF
7993 && (e
->isym
.st_shndx
< SHN_LORESERVE
7994 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
7996 s
= bfd_section_from_elf_index (e
->input_bfd
,
8000 elf_section_data (s
->output_section
)->this_idx
;
8001 sym
.st_value
= (s
->output_section
->vma
8003 + e
->isym
.st_value
);
8006 if (last_local
< e
->dynindx
)
8007 last_local
= e
->dynindx
;
8009 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8010 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8014 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8018 /* We get the global symbols from the hash table. */
8019 eoinfo
.failed
= FALSE
;
8020 eoinfo
.localsyms
= FALSE
;
8021 eoinfo
.finfo
= &finfo
;
8022 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8027 /* If backend needs to output some symbols not present in the hash
8028 table, do it now. */
8029 if (bed
->elf_backend_output_arch_syms
)
8031 typedef bfd_boolean (*out_sym_func
)
8032 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8033 struct elf_link_hash_entry
*);
8035 if (! ((*bed
->elf_backend_output_arch_syms
)
8036 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8040 /* Flush all symbols to the file. */
8041 if (! elf_link_flush_output_syms (&finfo
, bed
))
8044 /* Now we know the size of the symtab section. */
8045 off
+= symtab_hdr
->sh_size
;
8047 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8048 if (symtab_shndx_hdr
->sh_name
!= 0)
8050 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8051 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8052 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8053 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8054 symtab_shndx_hdr
->sh_size
= amt
;
8056 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8059 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8060 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8065 /* Finish up and write out the symbol string table (.strtab)
8067 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8068 /* sh_name was set in prep_headers. */
8069 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8070 symstrtab_hdr
->sh_flags
= 0;
8071 symstrtab_hdr
->sh_addr
= 0;
8072 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8073 symstrtab_hdr
->sh_entsize
= 0;
8074 symstrtab_hdr
->sh_link
= 0;
8075 symstrtab_hdr
->sh_info
= 0;
8076 /* sh_offset is set just below. */
8077 symstrtab_hdr
->sh_addralign
= 1;
8079 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8080 elf_tdata (abfd
)->next_file_pos
= off
;
8082 if (bfd_get_symcount (abfd
) > 0)
8084 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8085 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8089 /* Adjust the relocs to have the correct symbol indices. */
8090 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8092 if ((o
->flags
& SEC_RELOC
) == 0)
8095 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8096 elf_section_data (o
)->rel_count
,
8097 elf_section_data (o
)->rel_hashes
);
8098 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8099 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8100 elf_section_data (o
)->rel_count2
,
8101 (elf_section_data (o
)->rel_hashes
8102 + elf_section_data (o
)->rel_count
));
8104 /* Set the reloc_count field to 0 to prevent write_relocs from
8105 trying to swap the relocs out itself. */
8109 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8110 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8112 /* If we are linking against a dynamic object, or generating a
8113 shared library, finish up the dynamic linking information. */
8116 bfd_byte
*dyncon
, *dynconend
;
8118 /* Fix up .dynamic entries. */
8119 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8120 BFD_ASSERT (o
!= NULL
);
8122 dyncon
= o
->contents
;
8123 dynconend
= o
->contents
+ o
->size
;
8124 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8126 Elf_Internal_Dyn dyn
;
8130 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8137 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8139 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8141 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8142 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8145 dyn
.d_un
.d_val
= relativecount
;
8152 name
= info
->init_function
;
8155 name
= info
->fini_function
;
8158 struct elf_link_hash_entry
*h
;
8160 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8161 FALSE
, FALSE
, TRUE
);
8163 && (h
->root
.type
== bfd_link_hash_defined
8164 || h
->root
.type
== bfd_link_hash_defweak
))
8166 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8167 o
= h
->root
.u
.def
.section
;
8168 if (o
->output_section
!= NULL
)
8169 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8170 + o
->output_offset
);
8173 /* The symbol is imported from another shared
8174 library and does not apply to this one. */
8182 case DT_PREINIT_ARRAYSZ
:
8183 name
= ".preinit_array";
8185 case DT_INIT_ARRAYSZ
:
8186 name
= ".init_array";
8188 case DT_FINI_ARRAYSZ
:
8189 name
= ".fini_array";
8191 o
= bfd_get_section_by_name (abfd
, name
);
8194 (*_bfd_error_handler
)
8195 (_("%B: could not find output section %s"), abfd
, name
);
8199 (*_bfd_error_handler
)
8200 (_("warning: %s section has zero size"), name
);
8201 dyn
.d_un
.d_val
= o
->size
;
8204 case DT_PREINIT_ARRAY
:
8205 name
= ".preinit_array";
8208 name
= ".init_array";
8211 name
= ".fini_array";
8224 name
= ".gnu.version_d";
8227 name
= ".gnu.version_r";
8230 name
= ".gnu.version";
8232 o
= bfd_get_section_by_name (abfd
, name
);
8235 (*_bfd_error_handler
)
8236 (_("%B: could not find output section %s"), abfd
, name
);
8239 dyn
.d_un
.d_ptr
= o
->vma
;
8246 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8251 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8253 Elf_Internal_Shdr
*hdr
;
8255 hdr
= elf_elfsections (abfd
)[i
];
8256 if (hdr
->sh_type
== type
8257 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8259 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8260 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8263 if (dyn
.d_un
.d_val
== 0
8264 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8265 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8271 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8275 /* If we have created any dynamic sections, then output them. */
8278 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8281 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8283 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8285 || o
->output_section
== bfd_abs_section_ptr
)
8287 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8289 /* At this point, we are only interested in sections
8290 created by _bfd_elf_link_create_dynamic_sections. */
8293 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8295 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8297 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8299 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8301 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8303 (file_ptr
) o
->output_offset
,
8309 /* The contents of the .dynstr section are actually in a
8311 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8312 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8313 || ! _bfd_elf_strtab_emit (abfd
,
8314 elf_hash_table (info
)->dynstr
))
8320 if (info
->relocatable
)
8322 bfd_boolean failed
= FALSE
;
8324 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8329 /* If we have optimized stabs strings, output them. */
8330 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8332 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8336 if (info
->eh_frame_hdr
)
8338 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8342 if (finfo
.symstrtab
!= NULL
)
8343 _bfd_stringtab_free (finfo
.symstrtab
);
8344 if (finfo
.contents
!= NULL
)
8345 free (finfo
.contents
);
8346 if (finfo
.external_relocs
!= NULL
)
8347 free (finfo
.external_relocs
);
8348 if (finfo
.internal_relocs
!= NULL
)
8349 free (finfo
.internal_relocs
);
8350 if (finfo
.external_syms
!= NULL
)
8351 free (finfo
.external_syms
);
8352 if (finfo
.locsym_shndx
!= NULL
)
8353 free (finfo
.locsym_shndx
);
8354 if (finfo
.internal_syms
!= NULL
)
8355 free (finfo
.internal_syms
);
8356 if (finfo
.indices
!= NULL
)
8357 free (finfo
.indices
);
8358 if (finfo
.sections
!= NULL
)
8359 free (finfo
.sections
);
8360 if (finfo
.symbuf
!= NULL
)
8361 free (finfo
.symbuf
);
8362 if (finfo
.symshndxbuf
!= NULL
)
8363 free (finfo
.symshndxbuf
);
8364 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8366 if ((o
->flags
& SEC_RELOC
) != 0
8367 && elf_section_data (o
)->rel_hashes
!= NULL
)
8368 free (elf_section_data (o
)->rel_hashes
);
8371 elf_tdata (abfd
)->linker
= TRUE
;
8376 if (finfo
.symstrtab
!= NULL
)
8377 _bfd_stringtab_free (finfo
.symstrtab
);
8378 if (finfo
.contents
!= NULL
)
8379 free (finfo
.contents
);
8380 if (finfo
.external_relocs
!= NULL
)
8381 free (finfo
.external_relocs
);
8382 if (finfo
.internal_relocs
!= NULL
)
8383 free (finfo
.internal_relocs
);
8384 if (finfo
.external_syms
!= NULL
)
8385 free (finfo
.external_syms
);
8386 if (finfo
.locsym_shndx
!= NULL
)
8387 free (finfo
.locsym_shndx
);
8388 if (finfo
.internal_syms
!= NULL
)
8389 free (finfo
.internal_syms
);
8390 if (finfo
.indices
!= NULL
)
8391 free (finfo
.indices
);
8392 if (finfo
.sections
!= NULL
)
8393 free (finfo
.sections
);
8394 if (finfo
.symbuf
!= NULL
)
8395 free (finfo
.symbuf
);
8396 if (finfo
.symshndxbuf
!= NULL
)
8397 free (finfo
.symshndxbuf
);
8398 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8400 if ((o
->flags
& SEC_RELOC
) != 0
8401 && elf_section_data (o
)->rel_hashes
!= NULL
)
8402 free (elf_section_data (o
)->rel_hashes
);
8408 /* Garbage collect unused sections. */
8410 /* The mark phase of garbage collection. For a given section, mark
8411 it and any sections in this section's group, and all the sections
8412 which define symbols to which it refers. */
8414 typedef asection
* (*gc_mark_hook_fn
)
8415 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8416 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8419 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8421 gc_mark_hook_fn gc_mark_hook
)
8424 asection
*group_sec
;
8428 /* Mark all the sections in the group. */
8429 group_sec
= elf_section_data (sec
)->next_in_group
;
8430 if (group_sec
&& !group_sec
->gc_mark
)
8431 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8434 /* Look through the section relocs. */
8436 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8438 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8439 Elf_Internal_Shdr
*symtab_hdr
;
8440 struct elf_link_hash_entry
**sym_hashes
;
8443 bfd
*input_bfd
= sec
->owner
;
8444 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8445 Elf_Internal_Sym
*isym
= NULL
;
8448 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8449 sym_hashes
= elf_sym_hashes (input_bfd
);
8451 /* Read the local symbols. */
8452 if (elf_bad_symtab (input_bfd
))
8454 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8458 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8460 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8461 if (isym
== NULL
&& nlocsyms
!= 0)
8463 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8469 /* Read the relocations. */
8470 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8472 if (relstart
== NULL
)
8477 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8479 if (bed
->s
->arch_size
== 32)
8484 for (rel
= relstart
; rel
< relend
; rel
++)
8486 unsigned long r_symndx
;
8488 struct elf_link_hash_entry
*h
;
8490 r_symndx
= rel
->r_info
>> r_sym_shift
;
8494 if (r_symndx
>= nlocsyms
8495 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8497 h
= sym_hashes
[r_symndx
- extsymoff
];
8498 while (h
->root
.type
== bfd_link_hash_indirect
8499 || h
->root
.type
== bfd_link_hash_warning
)
8500 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8501 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8505 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8508 if (rsec
&& !rsec
->gc_mark
)
8510 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8512 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8521 if (elf_section_data (sec
)->relocs
!= relstart
)
8524 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8526 if (! info
->keep_memory
)
8529 symtab_hdr
->contents
= (unsigned char *) isym
;
8536 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8539 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8543 if (h
->root
.type
== bfd_link_hash_warning
)
8544 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8546 if (h
->dynindx
!= -1
8547 && ((h
->root
.type
!= bfd_link_hash_defined
8548 && h
->root
.type
!= bfd_link_hash_defweak
)
8549 || h
->root
.u
.def
.section
->gc_mark
))
8550 h
->dynindx
= (*idx
)++;
8555 /* The sweep phase of garbage collection. Remove all garbage sections. */
8557 typedef bfd_boolean (*gc_sweep_hook_fn
)
8558 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8561 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8565 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8569 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8572 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8574 /* Keep debug and special sections. */
8575 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8576 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8582 /* Skip sweeping sections already excluded. */
8583 if (o
->flags
& SEC_EXCLUDE
)
8586 /* Since this is early in the link process, it is simple
8587 to remove a section from the output. */
8588 o
->flags
|= SEC_EXCLUDE
;
8590 /* But we also have to update some of the relocation
8591 info we collected before. */
8593 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8595 Elf_Internal_Rela
*internal_relocs
;
8599 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8601 if (internal_relocs
== NULL
)
8604 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8606 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8607 free (internal_relocs
);
8615 /* Remove the symbols that were in the swept sections from the dynamic
8616 symbol table. GCFIXME: Anyone know how to get them out of the
8617 static symbol table as well? */
8621 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8623 elf_hash_table (info
)->dynsymcount
= i
;
8629 /* Propagate collected vtable information. This is called through
8630 elf_link_hash_traverse. */
8633 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8635 if (h
->root
.type
== bfd_link_hash_warning
)
8636 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8638 /* Those that are not vtables. */
8639 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8642 /* Those vtables that do not have parents, we cannot merge. */
8643 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
8646 /* If we've already been done, exit. */
8647 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
8650 /* Make sure the parent's table is up to date. */
8651 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
8653 if (h
->vtable
->used
== NULL
)
8655 /* None of this table's entries were referenced. Re-use the
8657 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
8658 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
8663 bfd_boolean
*cu
, *pu
;
8665 /* Or the parent's entries into ours. */
8666 cu
= h
->vtable
->used
;
8668 pu
= h
->vtable
->parent
->vtable
->used
;
8671 const struct elf_backend_data
*bed
;
8672 unsigned int log_file_align
;
8674 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8675 log_file_align
= bed
->s
->log_file_align
;
8676 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
8691 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
8694 bfd_vma hstart
, hend
;
8695 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8696 const struct elf_backend_data
*bed
;
8697 unsigned int log_file_align
;
8699 if (h
->root
.type
== bfd_link_hash_warning
)
8700 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8702 /* Take care of both those symbols that do not describe vtables as
8703 well as those that are not loaded. */
8704 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8707 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8708 || h
->root
.type
== bfd_link_hash_defweak
);
8710 sec
= h
->root
.u
.def
.section
;
8711 hstart
= h
->root
.u
.def
.value
;
8712 hend
= hstart
+ h
->size
;
8714 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
8716 return *(bfd_boolean
*) okp
= FALSE
;
8717 bed
= get_elf_backend_data (sec
->owner
);
8718 log_file_align
= bed
->s
->log_file_align
;
8720 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8722 for (rel
= relstart
; rel
< relend
; ++rel
)
8723 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8725 /* If the entry is in use, do nothing. */
8727 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
8729 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
8730 if (h
->vtable
->used
[entry
])
8733 /* Otherwise, kill it. */
8734 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8740 /* Mark sections containing dynamically referenced symbols. This is called
8741 through elf_link_hash_traverse. */
8744 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
,
8745 void *okp ATTRIBUTE_UNUSED
)
8747 if (h
->root
.type
== bfd_link_hash_warning
)
8748 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8750 if ((h
->root
.type
== bfd_link_hash_defined
8751 || h
->root
.type
== bfd_link_hash_defweak
)
8753 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
8758 /* Do mark and sweep of unused sections. */
8761 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
8763 bfd_boolean ok
= TRUE
;
8765 asection
* (*gc_mark_hook
)
8766 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8767 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
8769 if (!get_elf_backend_data (abfd
)->can_gc_sections
8770 || info
->relocatable
8771 || info
->emitrelocations
8773 || !is_elf_hash_table (info
->hash
))
8775 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
8779 /* Apply transitive closure to the vtable entry usage info. */
8780 elf_link_hash_traverse (elf_hash_table (info
),
8781 elf_gc_propagate_vtable_entries_used
,
8786 /* Kill the vtable relocations that were not used. */
8787 elf_link_hash_traverse (elf_hash_table (info
),
8788 elf_gc_smash_unused_vtentry_relocs
,
8793 /* Mark dynamically referenced symbols. */
8794 if (elf_hash_table (info
)->dynamic_sections_created
)
8795 elf_link_hash_traverse (elf_hash_table (info
),
8796 elf_gc_mark_dynamic_ref_symbol
,
8801 /* Grovel through relocs to find out who stays ... */
8802 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8803 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8807 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8810 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8812 if (o
->flags
& SEC_KEEP
)
8814 /* _bfd_elf_discard_section_eh_frame knows how to discard
8815 orphaned FDEs so don't mark sections referenced by the
8816 EH frame section. */
8817 if (strcmp (o
->name
, ".eh_frame") == 0)
8819 else if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
8825 /* ... and mark SEC_EXCLUDE for those that go. */
8826 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8832 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8835 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
8837 struct elf_link_hash_entry
*h
,
8840 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8841 struct elf_link_hash_entry
**search
, *child
;
8842 bfd_size_type extsymcount
;
8843 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8845 /* The sh_info field of the symtab header tells us where the
8846 external symbols start. We don't care about the local symbols at
8848 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
8849 if (!elf_bad_symtab (abfd
))
8850 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8852 sym_hashes
= elf_sym_hashes (abfd
);
8853 sym_hashes_end
= sym_hashes
+ extsymcount
;
8855 /* Hunt down the child symbol, which is in this section at the same
8856 offset as the relocation. */
8857 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8859 if ((child
= *search
) != NULL
8860 && (child
->root
.type
== bfd_link_hash_defined
8861 || child
->root
.type
== bfd_link_hash_defweak
)
8862 && child
->root
.u
.def
.section
== sec
8863 && child
->root
.u
.def
.value
== offset
)
8867 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
8868 abfd
, sec
, (unsigned long) offset
);
8869 bfd_set_error (bfd_error_invalid_operation
);
8875 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
8881 /* This *should* only be the absolute section. It could potentially
8882 be that someone has defined a non-global vtable though, which
8883 would be bad. It isn't worth paging in the local symbols to be
8884 sure though; that case should simply be handled by the assembler. */
8886 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
8889 child
->vtable
->parent
= h
;
8894 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8897 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
8898 asection
*sec ATTRIBUTE_UNUSED
,
8899 struct elf_link_hash_entry
*h
,
8902 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8903 unsigned int log_file_align
= bed
->s
->log_file_align
;
8907 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
8912 if (addend
>= h
->vtable
->size
)
8914 size_t size
, bytes
, file_align
;
8915 bfd_boolean
*ptr
= h
->vtable
->used
;
8917 /* While the symbol is undefined, we have to be prepared to handle
8919 file_align
= 1 << log_file_align
;
8920 if (h
->root
.type
== bfd_link_hash_undefined
)
8921 size
= addend
+ file_align
;
8927 /* Oops! We've got a reference past the defined end of
8928 the table. This is probably a bug -- shall we warn? */
8929 size
= addend
+ file_align
;
8932 size
= (size
+ file_align
- 1) & -file_align
;
8934 /* Allocate one extra entry for use as a "done" flag for the
8935 consolidation pass. */
8936 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
8940 ptr
= bfd_realloc (ptr
- 1, bytes
);
8946 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
8947 * sizeof (bfd_boolean
));
8948 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
8952 ptr
= bfd_zmalloc (bytes
);
8957 /* And arrange for that done flag to be at index -1. */
8958 h
->vtable
->used
= ptr
+ 1;
8959 h
->vtable
->size
= size
;
8962 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
8967 struct alloc_got_off_arg
{
8969 unsigned int got_elt_size
;
8972 /* We need a special top-level link routine to convert got reference counts
8973 to real got offsets. */
8976 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
8978 struct alloc_got_off_arg
*gofarg
= arg
;
8980 if (h
->root
.type
== bfd_link_hash_warning
)
8981 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8983 if (h
->got
.refcount
> 0)
8985 h
->got
.offset
= gofarg
->gotoff
;
8986 gofarg
->gotoff
+= gofarg
->got_elt_size
;
8989 h
->got
.offset
= (bfd_vma
) -1;
8994 /* And an accompanying bit to work out final got entry offsets once
8995 we're done. Should be called from final_link. */
8998 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
8999 struct bfd_link_info
*info
)
9002 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9004 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
9005 struct alloc_got_off_arg gofarg
;
9007 if (! is_elf_hash_table (info
->hash
))
9010 /* The GOT offset is relative to the .got section, but the GOT header is
9011 put into the .got.plt section, if the backend uses it. */
9012 if (bed
->want_got_plt
)
9015 gotoff
= bed
->got_header_size
;
9017 /* Do the local .got entries first. */
9018 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9020 bfd_signed_vma
*local_got
;
9021 bfd_size_type j
, locsymcount
;
9022 Elf_Internal_Shdr
*symtab_hdr
;
9024 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9027 local_got
= elf_local_got_refcounts (i
);
9031 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9032 if (elf_bad_symtab (i
))
9033 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9035 locsymcount
= symtab_hdr
->sh_info
;
9037 for (j
= 0; j
< locsymcount
; ++j
)
9039 if (local_got
[j
] > 0)
9041 local_got
[j
] = gotoff
;
9042 gotoff
+= got_elt_size
;
9045 local_got
[j
] = (bfd_vma
) -1;
9049 /* Then the global .got entries. .plt refcounts are handled by
9050 adjust_dynamic_symbol */
9051 gofarg
.gotoff
= gotoff
;
9052 gofarg
.got_elt_size
= got_elt_size
;
9053 elf_link_hash_traverse (elf_hash_table (info
),
9054 elf_gc_allocate_got_offsets
,
9059 /* Many folk need no more in the way of final link than this, once
9060 got entry reference counting is enabled. */
9063 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9065 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
9068 /* Invoke the regular ELF backend linker to do all the work. */
9069 return bfd_elf_final_link (abfd
, info
);
9073 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9075 struct elf_reloc_cookie
*rcookie
= cookie
;
9077 if (rcookie
->bad_symtab
)
9078 rcookie
->rel
= rcookie
->rels
;
9080 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9082 unsigned long r_symndx
;
9084 if (! rcookie
->bad_symtab
)
9085 if (rcookie
->rel
->r_offset
> offset
)
9087 if (rcookie
->rel
->r_offset
!= offset
)
9090 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9091 if (r_symndx
== SHN_UNDEF
)
9094 if (r_symndx
>= rcookie
->locsymcount
9095 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9097 struct elf_link_hash_entry
*h
;
9099 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9101 while (h
->root
.type
== bfd_link_hash_indirect
9102 || h
->root
.type
== bfd_link_hash_warning
)
9103 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9105 if ((h
->root
.type
== bfd_link_hash_defined
9106 || h
->root
.type
== bfd_link_hash_defweak
)
9107 && elf_discarded_section (h
->root
.u
.def
.section
))
9114 /* It's not a relocation against a global symbol,
9115 but it could be a relocation against a local
9116 symbol for a discarded section. */
9118 Elf_Internal_Sym
*isym
;
9120 /* Need to: get the symbol; get the section. */
9121 isym
= &rcookie
->locsyms
[r_symndx
];
9122 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9124 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9125 if (isec
!= NULL
&& elf_discarded_section (isec
))
9134 /* Discard unneeded references to discarded sections.
9135 Returns TRUE if any section's size was changed. */
9136 /* This function assumes that the relocations are in sorted order,
9137 which is true for all known assemblers. */
9140 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9142 struct elf_reloc_cookie cookie
;
9143 asection
*stab
, *eh
;
9144 Elf_Internal_Shdr
*symtab_hdr
;
9145 const struct elf_backend_data
*bed
;
9148 bfd_boolean ret
= FALSE
;
9150 if (info
->traditional_format
9151 || !is_elf_hash_table (info
->hash
))
9154 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9156 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9159 bed
= get_elf_backend_data (abfd
);
9161 if ((abfd
->flags
& DYNAMIC
) != 0)
9164 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9165 if (info
->relocatable
9168 || bfd_is_abs_section (eh
->output_section
))))
9171 stab
= bfd_get_section_by_name (abfd
, ".stab");
9174 || bfd_is_abs_section (stab
->output_section
)
9175 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9180 && bed
->elf_backend_discard_info
== NULL
)
9183 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9185 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9186 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9187 if (cookie
.bad_symtab
)
9189 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9190 cookie
.extsymoff
= 0;
9194 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9195 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9198 if (bed
->s
->arch_size
== 32)
9199 cookie
.r_sym_shift
= 8;
9201 cookie
.r_sym_shift
= 32;
9203 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9204 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9206 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9207 cookie
.locsymcount
, 0,
9209 if (cookie
.locsyms
== NULL
)
9216 count
= stab
->reloc_count
;
9218 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9220 if (cookie
.rels
!= NULL
)
9222 cookie
.rel
= cookie
.rels
;
9223 cookie
.relend
= cookie
.rels
;
9224 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9225 if (_bfd_discard_section_stabs (abfd
, stab
,
9226 elf_section_data (stab
)->sec_info
,
9227 bfd_elf_reloc_symbol_deleted_p
,
9230 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9238 count
= eh
->reloc_count
;
9240 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9242 cookie
.rel
= cookie
.rels
;
9243 cookie
.relend
= cookie
.rels
;
9244 if (cookie
.rels
!= NULL
)
9245 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9247 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9248 bfd_elf_reloc_symbol_deleted_p
,
9252 if (cookie
.rels
!= NULL
9253 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9257 if (bed
->elf_backend_discard_info
!= NULL
9258 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9261 if (cookie
.locsyms
!= NULL
9262 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9264 if (! info
->keep_memory
)
9265 free (cookie
.locsyms
);
9267 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9271 if (info
->eh_frame_hdr
9272 && !info
->relocatable
9273 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9280 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9283 const char *name
, *p
;
9284 struct bfd_section_already_linked
*l
;
9285 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9288 /* A single member comdat group section may be discarded by a
9289 linkonce section. See below. */
9290 if (sec
->output_section
== bfd_abs_section_ptr
)
9295 /* Check if it belongs to a section group. */
9296 group
= elf_sec_group (sec
);
9298 /* Return if it isn't a linkonce section nor a member of a group. A
9299 comdat group section also has SEC_LINK_ONCE set. */
9300 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9305 /* If this is the member of a single member comdat group, check if
9306 the group should be discarded. */
9307 if (elf_next_in_group (sec
) == sec
9308 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9314 /* FIXME: When doing a relocatable link, we may have trouble
9315 copying relocations in other sections that refer to local symbols
9316 in the section being discarded. Those relocations will have to
9317 be converted somehow; as of this writing I'm not sure that any of
9318 the backends handle that correctly.
9320 It is tempting to instead not discard link once sections when
9321 doing a relocatable link (technically, they should be discarded
9322 whenever we are building constructors). However, that fails,
9323 because the linker winds up combining all the link once sections
9324 into a single large link once section, which defeats the purpose
9325 of having link once sections in the first place.
9327 Also, not merging link once sections in a relocatable link
9328 causes trouble for MIPS ELF, which relies on link once semantics
9329 to handle the .reginfo section correctly. */
9331 name
= bfd_get_section_name (abfd
, sec
);
9333 if (strncmp (name
, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9334 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
9339 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
9341 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9343 /* We may have 3 different sections on the list: group section,
9344 comdat section and linkonce section. SEC may be a linkonce or
9345 group section. We match a group section with a group section,
9346 a linkonce section with a linkonce section, and ignore comdat
9348 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9349 && strcmp (name
, l
->sec
->name
) == 0
9350 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9352 /* The section has already been linked. See if we should
9354 switch (flags
& SEC_LINK_DUPLICATES
)
9359 case SEC_LINK_DUPLICATES_DISCARD
:
9362 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9363 (*_bfd_error_handler
)
9364 (_("%B: ignoring duplicate section `%A'\n"),
9368 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9369 if (sec
->size
!= l
->sec
->size
)
9370 (*_bfd_error_handler
)
9371 (_("%B: duplicate section `%A' has different size\n"),
9375 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9376 if (sec
->size
!= l
->sec
->size
)
9377 (*_bfd_error_handler
)
9378 (_("%B: duplicate section `%A' has different size\n"),
9380 else if (sec
->size
!= 0)
9382 bfd_byte
*sec_contents
, *l_sec_contents
;
9384 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9385 (*_bfd_error_handler
)
9386 (_("%B: warning: could not read contents of section `%A'\n"),
9388 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9390 (*_bfd_error_handler
)
9391 (_("%B: warning: could not read contents of section `%A'\n"),
9392 l
->sec
->owner
, l
->sec
);
9393 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9394 (*_bfd_error_handler
)
9395 (_("%B: warning: duplicate section `%A' has different contents\n"),
9399 free (sec_contents
);
9401 free (l_sec_contents
);
9406 /* Set the output_section field so that lang_add_section
9407 does not create a lang_input_section structure for this
9408 section. Since there might be a symbol in the section
9409 being discarded, we must retain a pointer to the section
9410 which we are really going to use. */
9411 sec
->output_section
= bfd_abs_section_ptr
;
9412 sec
->kept_section
= l
->sec
;
9414 if (flags
& SEC_GROUP
)
9416 asection
*first
= elf_next_in_group (sec
);
9417 asection
*s
= first
;
9421 s
->output_section
= bfd_abs_section_ptr
;
9422 /* Record which group discards it. */
9423 s
->kept_section
= l
->sec
;
9424 s
= elf_next_in_group (s
);
9425 /* These lists are circular. */
9437 /* If this is the member of a single member comdat group and the
9438 group hasn't be discarded, we check if it matches a linkonce
9439 section. We only record the discarded comdat group. Otherwise
9440 the undiscarded group will be discarded incorrectly later since
9441 itself has been recorded. */
9442 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9443 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9444 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9445 && bfd_elf_match_symbols_in_sections (l
->sec
,
9446 elf_next_in_group (sec
)))
9448 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
9449 elf_next_in_group (sec
)->kept_section
= l
->sec
;
9450 group
->output_section
= bfd_abs_section_ptr
;
9457 /* There is no direct match. But for linkonce section, we should
9458 check if there is a match with comdat group member. We always
9459 record the linkonce section, discarded or not. */
9460 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9461 if (l
->sec
->flags
& SEC_GROUP
)
9463 asection
*first
= elf_next_in_group (l
->sec
);
9466 && elf_next_in_group (first
) == first
9467 && bfd_elf_match_symbols_in_sections (first
, sec
))
9469 sec
->output_section
= bfd_abs_section_ptr
;
9470 sec
->kept_section
= l
->sec
;
9475 /* This is the first section with this name. Record it. */
9476 bfd_section_already_linked_table_insert (already_linked_list
, sec
);